Commit 502e9c3b45dd261fae6e264b5bd67f430f7682e3

Sylvain 2021-10-24T17:16:49

SDL_Renderer simplifications: - Factorize PrepQueueCmdDraw{,DrawTexture,Solid) into one single function - Change SDL_Texture/Renderer r,g,b,a Uint8 into an SDL_Color, so that it can be passed directly to RenderGeometry - Don't automatically queue a SET_DRAW_COLOR cmd for RenderGeometry (and update GLES2 renderer)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 00cab91..a3ffd08 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -394,9 +394,9 @@ QueueCmdSetClipRect(SDL_Renderer *renderer)
 }
 
 static int
-QueueCmdSetDrawColor(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
+QueueCmdSetDrawColor(SDL_Renderer *renderer, SDL_Color *col)
 {
-    const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
+    const Uint32 color = ((col->a << 24) | (col->r << 16) | (col->g << 8) | col->b);
     int retval = 0;
 
     if (!renderer->color_queued || (color != renderer->last_queued_color)) {
@@ -406,10 +406,10 @@ QueueCmdSetDrawColor(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const
         if (cmd != NULL) {
             cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
             cmd->data.color.first = 0;  /* render backend will fill this in. */
-            cmd->data.color.r = r;
-            cmd->data.color.g = g;
-            cmd->data.color.b = b;
-            cmd->data.color.a = a;
+            cmd->data.color.r = col->r;
+            cmd->data.color.g = col->g;
+            cmd->data.color.b = col->b;
+            cmd->data.color.a = col->a;
             retval = renderer->QueueSetDrawColor(renderer, cmd);
             if (retval < 0) {
                 cmd->command = SDL_RENDERCMD_NO_OP;
@@ -432,17 +432,33 @@ QueueCmdClear(SDL_Renderer *renderer)
 
     cmd->command = SDL_RENDERCMD_CLEAR;
     cmd->data.color.first = 0;
-    cmd->data.color.r = renderer->r;
-    cmd->data.color.g = renderer->g;
-    cmd->data.color.b = renderer->b;
-    cmd->data.color.a = renderer->a;
+    cmd->data.color.r = renderer->color.r;
+    cmd->data.color.g = renderer->color.g;
+    cmd->data.color.b = renderer->color.b;
+    cmd->data.color.a = renderer->color.a;
     return 0;
 }
 
-static int
-PrepQueueCmdDraw(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
+static SDL_RenderCommand *
+PrepQueueCmdDraw(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype, SDL_Texture *texture)
 {
-    int retval = QueueCmdSetDrawColor(renderer, r, g, b, a);
+    SDL_RenderCommand *cmd = NULL;
+    int retval = 0;
+    SDL_Color *color;
+    SDL_BlendMode blendMode;
+
+    if (texture) {
+        color = &texture->color;
+        blendMode = texture->blendMode;
+    } else {
+        color = &renderer->color;
+        blendMode = renderer->blendMode;
+    }
+
+    if (cmdtype != SDL_RENDERCMD_GEOMETRY) {
+        /* !!! FIXME: drop this draw if viewport w or h is zero. */
+        retval = QueueCmdSetDrawColor(renderer, color);
+    }
 
     /* Set the viewport and clip rect directly before draws, so the backends
      * don't have to worry about that state not being valid at draw time. */
@@ -452,26 +468,19 @@ PrepQueueCmdDraw(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uin
     if (retval == 0 && !renderer->cliprect_queued) {
         retval = QueueCmdSetClipRect(renderer);
     }
-    return retval;
-}
 
-static SDL_RenderCommand *
-PrepQueueCmdDrawSolid(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype)
-{
-    /* !!! FIXME: drop this draw if viewport w or h is zero. */
-    SDL_RenderCommand *cmd = NULL;
-    if (PrepQueueCmdDraw(renderer, renderer->r, renderer->g, renderer->b, renderer->a) == 0) {
+    if (retval == 0) {
         cmd = AllocateRenderCommand(renderer);
         if (cmd != NULL) {
             cmd->command = cmdtype;
             cmd->data.draw.first = 0;  /* render backend will fill this in. */
             cmd->data.draw.count = 0;  /* render backend will fill this in. */
-            cmd->data.draw.r = renderer->r;
-            cmd->data.draw.g = renderer->g;
-            cmd->data.draw.b = renderer->b;
-            cmd->data.draw.a = renderer->a;
-            cmd->data.draw.blend = renderer->blendMode;
-            cmd->data.draw.texture = NULL;  /* no texture. */
+            cmd->data.draw.r = color->r;
+            cmd->data.draw.g = color->g;
+            cmd->data.draw.b = color->b;
+            cmd->data.draw.a = color->a;
+            cmd->data.draw.blend = blendMode;
+            cmd->data.draw.texture = texture;
         }
     }
     return cmd;
@@ -480,7 +489,7 @@ PrepQueueCmdDrawSolid(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtyp
 static int
 QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
 {
-    SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_DRAW_POINTS);
+    SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_POINTS, NULL);
     int retval = -1;
     if (cmd != NULL) {
         retval = renderer->QueueDrawPoints(renderer, cmd, points, count);
@@ -494,7 +503,7 @@ QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint * points, const int 
 static int
 QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
 {
-    SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_DRAW_LINES);
+    SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_LINES, NULL);
     int retval = -1;
     if (cmd != NULL) {
         retval = renderer->QueueDrawLines(renderer, cmd, points, count);
@@ -512,7 +521,7 @@ QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int cou
     int retval = -1;
     int use_geometry = renderer->QueueGeometry && ! (renderer->info.flags & SDL_RENDERER_SOFTWARE);
 
-    cmd = PrepQueueCmdDrawSolid(renderer, (use_geometry ? SDL_RENDERCMD_GEOMETRY : SDL_RENDERCMD_FILL_RECTS));
+    cmd = PrepQueueCmdDraw(renderer, (use_geometry ? SDL_RENDERCMD_GEOMETRY : SDL_RENDERCMD_FILL_RECTS), NULL);
 
     if (cmd != NULL) {
         if (use_geometry) {
@@ -525,16 +534,12 @@ QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int cou
                 int i;
                 float *ptr_xy = xy;
                 int *ptr_indices = indices;
-                SDL_Color col;
                 const int xy_stride = 2 * sizeof (float);
-                const int color_stride = 0;
                 const int num_vertices = 4 * count;
                 const int num_indices = 6 * count;
                 const int size_indices = 4;
                 int cur_indice = 0;
 
-                SDL_GetRenderDrawColor(renderer, &col.r, &col.g, &col.b, &col.a);
-                
                 for (i = 0; i < count; ++i) {
                     float minx, miny, maxx, maxy;
 
@@ -562,7 +567,7 @@ QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int cou
                 }
 
                 retval = renderer->QueueGeometry(renderer, cmd, NULL,
-                        xy, xy_stride, &col, color_stride, NULL, 0,
+                        xy, xy_stride, (int *) &renderer->color, 0 /* color_stride */, NULL, 0,
                         num_vertices, indices, num_indices, size_indices,
                         1.0f, 1.0f);
 
@@ -583,32 +588,10 @@ QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int cou
     return retval;
 }
 
-static SDL_RenderCommand *
-PrepQueueCmdDrawTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_RenderCommandType cmdtype)
-{
-    /* !!! FIXME: drop this draw if viewport w or h is zero. */
-    SDL_RenderCommand *cmd = NULL;
-    if (PrepQueueCmdDraw(renderer, texture->r, texture->g, texture->b, texture->a) == 0) {
-        cmd = AllocateRenderCommand(renderer);
-        if (cmd != NULL) {
-            cmd->command = cmdtype;
-            cmd->data.draw.first = 0;  /* render backend will fill this in. */
-            cmd->data.draw.count = 0;  /* render backend will fill this in. */
-            cmd->data.draw.r = texture->r;
-            cmd->data.draw.g = texture->g;
-            cmd->data.draw.b = texture->b;
-            cmd->data.draw.a = texture->a;
-            cmd->data.draw.blend = texture->blendMode;
-            cmd->data.draw.texture = texture;
-        }
-    }
-    return cmd;
-}
-
 static int
 QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect)
 {
-    SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY);
+    SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY, texture);
     int retval = -1;
     if (cmd != NULL) {
         retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
@@ -624,7 +607,7 @@ QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
                const SDL_Rect * srcquad, const SDL_FRect * dstrect,
                const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
 {
-    SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY_EX);
+    SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY_EX, texture);
     int retval = -1;
     SDL_assert(renderer->QueueCopyEx != NULL);  /* should have caught at higher level. */
     if (cmd != NULL) {
@@ -647,11 +630,7 @@ QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
 {
     SDL_RenderCommand *cmd;
     int retval = -1;
-    if (texture) {
-        cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_GEOMETRY);
-    } else {
-        cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_GEOMETRY);
-    }
+    cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_GEOMETRY, texture);
     if (cmd != NULL) {
         retval = renderer->QueueGeometry(renderer, cmd, texture,
                 xy, xy_stride, color, color_stride, uv, uv_stride,
@@ -1232,10 +1211,10 @@ SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int
     texture->access = access;
     texture->w = w;
     texture->h = h;
-    texture->r = 255;
-    texture->g = 255;
-    texture->b = 255;
-    texture->a = 255;
+    texture->color.r = 255;
+    texture->color.g = 255;
+    texture->color.b = 255;
+    texture->color.a = 255;
     texture->scaleMode = SDL_GetScaleMode();
     texture->renderer = renderer;
     texture->next = renderer->textures;
@@ -1490,9 +1469,9 @@ SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
     } else {
         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
     }
-    texture->r = r;
-    texture->g = g;
-    texture->b = b;
+    texture->color.r = r;
+    texture->color.g = g;
+    texture->color.b = b;
     if (texture->native) {
         return SDL_SetTextureColorMod(texture->native, r, g, b);
     }
@@ -1506,13 +1485,13 @@ SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
     CHECK_TEXTURE_MAGIC(texture, -1);
 
     if (r) {
-        *r = texture->r;
+        *r = texture->color.r;
     }
     if (g) {
-        *g = texture->g;
+        *g = texture->color.g;
     }
     if (b) {
-        *b = texture->b;
+        *b = texture->color.b;
     }
     return 0;
 }
@@ -1527,7 +1506,7 @@ SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
     } else {
         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
     }
-    texture->a = alpha;
+    texture->color.a = alpha;
     if (texture->native) {
         return SDL_SetTextureAlphaMod(texture->native, alpha);
     }
@@ -1540,7 +1519,7 @@ SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
     CHECK_TEXTURE_MAGIC(texture, -1);
 
     if (alpha) {
-        *alpha = texture->a;
+        *alpha = texture->color.a;
     }
     return 0;
 }
@@ -2519,10 +2498,10 @@ SDL_SetRenderDrawColor(SDL_Renderer * renderer,
 {
     CHECK_RENDERER_MAGIC(renderer, -1);
 
-    renderer->r = r;
-    renderer->g = g;
-    renderer->b = b;
-    renderer->a = a;
+    renderer->color.r = r;
+    renderer->color.g = g;
+    renderer->color.b = b;
+    renderer->color.a = a;
     return 0;
 }
 
@@ -2533,16 +2512,16 @@ SDL_GetRenderDrawColor(SDL_Renderer * renderer,
     CHECK_RENDERER_MAGIC(renderer, -1);
 
     if (r) {
-        *r = renderer->r;
+        *r = renderer->color.r;
     }
     if (g) {
-        *g = renderer->g;
+        *g = renderer->color.g;
     }
     if (b) {
-        *b = renderer->b;
+        *b = renderer->color.b;
     }
     if (a) {
-        *a = renderer->a;
+        *a = renderer->color.a;
     }
     return 0;
 }
@@ -3312,8 +3291,6 @@ SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
     if (renderer->QueueGeometry && renderer->QueueCopy == NULL) {
         float xy[8];
         const int xy_stride = 2 * sizeof (float);
-        SDL_Color color;
-        const int color_stride = 0;
         float uv[8];
         const int uv_stride = 2 * sizeof (float);
         const int num_vertices = 4;
@@ -3323,9 +3300,6 @@ SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
         float minu, minv, maxu, maxv;
         float minx, miny, maxx, maxy;
 
-        SDL_GetTextureColorMod(texture, &color.r, &color.g, &color.b);
-        SDL_GetTextureAlphaMod(texture, &color.a);
-
         minu = (float) (real_srcrect.x) / (float) texture->w;
         minv = (float) (real_srcrect.y) / (float) texture->h;
         maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
@@ -3355,7 +3329,7 @@ SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
         xy[7] = maxy;
 
         retval = QueueCmdGeometry(renderer, texture,
-                xy, xy_stride, (int *)&color, color_stride, uv, uv_stride,
+                xy, xy_stride, (int *)&texture->color, 0 /* color_stride */, uv, uv_stride,
                 num_vertices,
                 indices, num_indices, size_indices,
                 renderer->scale.x, renderer->scale.y);
@@ -3462,8 +3436,6 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
     if (renderer->QueueGeometry && renderer->QueueCopyEx == NULL) {
         float xy[8];
         const int xy_stride = 2 * sizeof (float);
-        SDL_Color color;
-        const int color_stride = 0;
         float uv[8];
         const int uv_stride = 2 * sizeof (float);
         const int num_vertices = 4;
@@ -3481,9 +3453,6 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
         const float s = SDL_sin(radian_angle);
         const float c = SDL_cos(radian_angle);
 
-        SDL_GetTextureColorMod(texture, &color.r, &color.g, &color.b);
-        SDL_GetTextureAlphaMod(texture, &color.a);
-
         minu = (float) (real_srcrect.x) / (float) texture->w;
         minv = (float) (real_srcrect.y) / (float) texture->h;
         maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
@@ -3542,7 +3511,7 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
         xy[7] = (s_minx + c_maxy) + centery;
 
         retval = QueueCmdGeometry(renderer, texture,
-                xy, xy_stride, (int *)&color, color_stride, uv, uv_stride,
+                xy, xy_stride, (int *)&texture->color, 0 /* color_stride */, uv, uv_stride,
                 num_vertices,
                 indices, num_indices, size_indices,
                 renderer->scale.x, renderer->scale.y);
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index a8ed019..dfee484 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -43,7 +43,7 @@ struct SDL_Texture
     int modMode;                /**< The texture modulation mode */
     SDL_BlendMode blendMode;    /**< The texture blend mode */
     SDL_ScaleMode scaleMode;    /**< The texture scale mode */
-    Uint8 r, g, b, a;           /**< Texture modulation values */
+    SDL_Color color;            /**< Texture modulation values */
 
     SDL_Renderer *renderer;
 
@@ -216,7 +216,7 @@ struct SDL_Renderer
     SDL_Texture *target;
     SDL_mutex *target_mutex;
 
-    Uint8 r, g, b, a;                   /**< Color for drawing operations values */
+    SDL_Color color;                    /**< Color for drawing operations values */
     SDL_BlendMode blendMode;            /**< The drawing blend mode */
 
     SDL_bool always_batch;
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 57cf6a9..972f11f 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -1032,7 +1032,6 @@ GLES2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *ver
     const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
     const int vboidx = data->current_vertex_buffer;
     const GLuint vbo = data->vertex_buffers[vboidx];
-    size_t i;
 
     if (GLES2_ActivateRenderer(renderer) < 0) {
         return -1;
@@ -1142,10 +1141,7 @@ GLES2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *ver
                 while (nextcmd != NULL) {
                     const SDL_RenderCommandType nextcmdtype = nextcmd->command;
                     if (nextcmdtype != thiscmdtype) {
-                        /* SETDRAWCOLOR commands are safe because copy color is set in the vbo per-vertex. */
-                        if ((nextcmdtype != SDL_RENDERCMD_SETDRAWCOLOR) && (nextcmdtype != SDL_RENDERCMD_NO_OP)) {
-                            break;  /* can't go any further on this draw call, different render command up next. */
-                        }
+                        break;  /* can't go any further on this draw call, different render command up next. */
                     } else if (nextcmd->data.draw.texture != thistexture || nextcmd->data.draw.blend != thisblend) {
                         break;  /* can't go any further on this draw call, different texture/blendmode copy up next. */
                     } else {
diff --git a/src/render/software/SDL_render_sw.c b/src/render/software/SDL_render_sw.c
index 029b387..e8136a2 100644
--- a/src/render/software/SDL_render_sw.c
+++ b/src/render/software/SDL_render_sw.c
@@ -117,9 +117,8 @@ SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
     texture->driverdata =
         SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
                              Bmask, Amask);
-    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
-                           texture->b);
-    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
+    SDL_SetSurfaceColorMod(texture->driverdata, texture->color.r, texture->color.g, texture->color.b);
+    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->color.a);
     SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
 
     /* Only RLE encode textures without an alpha channel since the RLE coder