Commit e481261173f84cd810b62a4bf4a9b208c661a21e

Sylvain 2021-04-01T09:49:16

Move to SDL_RenderGeometryRaw prototype with separate xy/uv/color pointer parameters

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
433
434
435
436
437
438
diff --git a/include/SDL_render.h b/include/SDL_render.h
index 797274c..1022f9c 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -137,17 +137,6 @@ typedef struct SDL_Renderer SDL_Renderer;
 struct SDL_Texture;
 typedef struct SDL_Texture SDL_Texture;
 
-/**
- *  \brief Vertex structure
- */
-typedef struct SDL_Vertex
-{
-    SDL_FPoint position;        /**< Vertex position, in SDL_Renderer coordinates  */
-    SDL_Color  color;           /**< Vertex color */
-    SDL_FPoint tex_coord;       /**< Normalized texture coordinates, if needed */
-} SDL_Vertex;
-
-
 /* Function prototypes */
 
 /**
@@ -1453,23 +1442,30 @@ extern DECLSPEC int SDLCALL SDL_RenderCopyExF(SDL_Renderer * renderer,
                                             const SDL_RendererFlip flip);
 
 /**
- *  \brief Render a list of triangles, optionally using a texture and indices into the vertex array
+ *  \brief Render a list of triangles, optionally using a texture and indices into the vertex arrays
  *  Color and alpha modulation is done per vertex (SDL_SetTextureColorMod and SDL_SetTextureAlphaMod are ignored).
  *
  *  \param texture      (optional) The SDL texture to use.
- *  \param vertices     Vertices.
+ *  \param xy           Vertex positions
+ *  \param xy_stride    Byte size to move from one element to the next element
+ *  \param color        Vertex colors (as SDL_Color)
+ *  \param color_stride Byte size to move from one element to the next element
+ *  \param uv           Vertex normalized texture coordinates
+ *  \param uv_stride    Byte size to move from one element to the next element
  *  \param num_vertices Number of vertices.
- *  \param indices      (optional) An array of integer indices into the 'vertices' array, if NULL all vertices will be rendered in sequential order.
+ *  \param indices      (optional) An array of indices into the 'vertices' arrays, if NULL all vertices will be rendered in sequential order.
  *  \param num_indices  Number of indices.
- *
- *  \sa SDL_Vertex
+ *  \param size_indice  Indice size: 1 (byte), 2 (short), 4 (int)
  *
  *  \return 0 on success, or -1 if the operation is not supported
  */
-extern DECLSPEC int SDLCALL SDL_RenderGeometry(SDL_Renderer *renderer,
+extern DECLSPEC int SDLCALL SDL_RenderGeometryRaw(SDL_Renderer *renderer,
                                                SDL_Texture *texture,
-                                               SDL_Vertex *vertices, int num_vertices,
-                                               int *indices, int num_indices);
+                                               const float *xy, int xy_stride,
+                                               const int *color, int color_stride,
+                                               const float *uv, int uv_stride,
+                                               int num_vertices,
+                                               const void *indices, int num_indices, int size_indice);
 
 /**
  * Read pixels from the current rendering target to an array of pixels.
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 1175365..6a849c4 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -818,3 +818,4 @@
 #define SDL_SetTextureUserData SDL_SetTextureUserData_REAL
 #define SDL_GetTextureUserData SDL_GetTextureUserData_REAL
 #define SDL_RenderGeometry SDL_RenderGeometry_REAL
+#define SDL_RenderGeometryRaw SDL_RenderGeometryRaw_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 7c721f8..88ecb99 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -883,3 +883,4 @@ SDL_DYNAPI_PROC(float,SDL_GameControllerGetSensorDataRate,(SDL_GameController *a
 SDL_DYNAPI_PROC(int,SDL_SetTextureUserData,(SDL_Texture *a, void *b),(a,b),return)
 SDL_DYNAPI_PROC(void*,SDL_GetTextureUserData,(SDL_Texture *a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_RenderGeometry,(SDL_Renderer *a, SDL_Texture *b, SDL_Vertex *c, int d, int *e, int f),(a,b,c,d,e,f),return)
+SDL_DYNAPI_PROC(int,SDL_RenderGeometryRaw,(SDL_Renderer *a, SDL_Texture *b, const float *c, int d, const int *e, int f, const float *g, int h, int i, const void *j, int k, int l),(a,b,c,d,e,f,g,h,i,j,k,l),return)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 746bdf0..d8c49ba 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -564,7 +564,12 @@ QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
 
 static int
 QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
-        SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y)
+        const float *xy, int xy_stride,
+        const int *color, int color_stride,
+        const float *uv, int uv_stride,
+        int num_vertices,
+        const void *indices, int num_indices, int size_indice,
+        float scale_x, float scale_y)
 {
     SDL_RenderCommand *cmd;
     int retval = -1;
@@ -574,7 +579,10 @@ QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
         cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_GEOMETRY);
     }
     if (cmd != NULL) {
-        retval = renderer->QueueGeometry(renderer, cmd, texture, vertices, num_vertices, indices, num_indices, scale_x, scale_y);
+        retval = renderer->QueueGeometry(renderer, cmd, texture,
+                xy, xy_stride, color, color_stride, uv, uv_stride,
+                num_vertices, indices, num_indices, size_indice,
+                scale_x, scale_y);
         if (retval < 0) {
             cmd->command = SDL_RENDERCMD_NO_OP;
         }
@@ -3323,13 +3331,16 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
 }
 
 int
-SDL_RenderGeometry(SDL_Renderer *renderer,
-                   SDL_Texture *texture,
-                   SDL_Vertex *vertices, int num_vertices,
-                   int *indices, int num_indices)
+SDL_RenderGeometryRaw(SDL_Renderer *renderer,
+                                  SDL_Texture *texture,
+                                  const float *xy, int xy_stride,
+                                  const int *color, int color_stride,
+                                  const float *uv, int uv_stride,
+                                  int num_vertices,
+                                  const void *indices, int num_indices, int size_indice)
 {
     int i;
-    int retval;
+    int retval = 0;
     int count = indices ? num_indices : num_vertices;
 
     CHECK_RENDERER_MAGIC(renderer, -1);
@@ -3346,14 +3357,30 @@ SDL_RenderGeometry(SDL_Renderer *renderer,
         }
     }
 
-    if (!vertices) {
-        return SDL_InvalidParamError("points");
+    if (!xy) {
+        return SDL_InvalidParamError("xy");
+    }
+
+    if (!color) {
+        return SDL_InvalidParamError("color");
+    }
+
+    if (texture && !uv) {
+        return SDL_InvalidParamError("uv");
     }
 
     if (count % 3 != 0) {
         return SDL_InvalidParamError(indices ? "num_indices" : "num_vertices");
     }
 
+    if (indices) {
+        if (size_indice != 1 && size_indice != 2 && size_indice != 4) {
+            return SDL_InvalidParamError("size_indice");
+        }
+    } else {
+        size_indice = 0;
+    }
+
     /* Don't draw while we're hidden */
     if (renderer->hidden) {
         return 0;
@@ -3369,26 +3396,40 @@ SDL_RenderGeometry(SDL_Renderer *renderer,
 
     if (texture) {
         for (i = 0; i < num_vertices; ++i) {
-            if (vertices[i].tex_coord.x < 0.0f || vertices[i].tex_coord.y < 0.0f || vertices[i].tex_coord.x > 1.0f || vertices[i].tex_coord.y > 1.0f) {
-                return SDL_SetError("Values of 'vertices' out of bounds");
+            const float *uv_ = (const float *)((const char*)uv + i * uv_stride);
+            float u = uv_[0];
+            float v = uv_[1];
+            if (u < 0.0f || v < 0.0f || u > 1.0f || v > 1.0f) {
+                return SDL_SetError("Values of 'uv' out of bounds %f %f at %d/%d", u, v, i, num_vertices);
             }
         }
     }
 
     if (indices) {
         for (i = 0; i < num_indices; ++i) {
-            if (indices[i] < 0 || indices[i] >= num_vertices) {
+            int j;
+            if (size_indice == 4) {
+                j = ((const Uint32 *)indices)[i];
+            } else if (size_indice == 2) {
+                j = ((const Uint16 *)indices)[i];
+            } else {
+                j = ((const Uint8 *)indices)[i];
+            }
+            if (j < 0 || j >= num_vertices) {
                 return SDL_SetError("Values of 'indices' out of bounds");
             }
         }
     }
 
-
     if (texture) {
         texture->last_command_generation = renderer->render_command_generation;
     }
 
-    retval = QueueCmdGeometry(renderer, texture, vertices, num_vertices, indices, num_indices, renderer->scale.x, renderer->scale.y);
+    retval = QueueCmdGeometry(renderer, texture,
+            xy, xy_stride, color, color_stride, uv, uv_stride,
+            num_vertices,
+            indices, num_indices, size_indice,
+            renderer->scale.x, renderer->scale.y);
 
     return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
 }
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 1beb4b2..5c2add1 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -131,7 +131,9 @@ struct SDL_Renderer
                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
 
     int (*QueueGeometry) (SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
-                          SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y);
+                          const float *xy, int xy_stride, const int *color, int color_stride, const float *uv, int uv_stride,
+                          int num_vertices, const void *indices, int num_indices, int size_indice,
+                          float scale_x, float scale_y);
 
     int (*RunCommandQueue) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize);
     int (*UpdateTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index cd531dc..d9caf84 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -1054,7 +1054,9 @@ GL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * te
 
 static int
 GL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
-        SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y)
+        const float *xy, int xy_stride, const int *color, int color_stride, const float *uv, int uv_stride,
+        int num_vertices, const void *indices, int num_indices, int size_indice,
+        float scale_x, float scale_y)
 {
     GL_TextureData *texturedata = NULL;
     int i;
@@ -1074,19 +1076,32 @@ GL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te
     cmd->data.draw.count = count;
 
     for (i = 0; i < count; i++) {
-        SDL_Vertex *v = &vertices[indices ? indices[i] : i];
+        int j;
+        if (size_indice == 4) {
+            j = ((const Uint32 *)indices)[i];
+        } else if (size_indice == 2) {
+            j = ((const Uint16 *)indices)[i];
+        } else if (size_indice == 1) {
+            j = ((const Uint8 *)indices)[i];
+        } else {
+            j = i;
+        }
+
+        float *xy_ = (float *)((char*)xy + j * xy_stride);
+        SDL_Color col_ = *(SDL_Color *)((char*)color + j * color_stride);
 
-        *(verts++) = v->position.x * scale_x;
-        *(verts++) = v->position.y * scale_y;
+        *(verts++) = xy_[0] * scale_x;
+        *(verts++) = xy_[1] * scale_y;
 
-        *(verts++) = v->color.r * inv255f;
-        *(verts++) = v->color.g * inv255f;
-        *(verts++) = v->color.b * inv255f;
-        *(verts++) = v->color.a * inv255f;
+        *(verts++) = col_.r * inv255f;
+        *(verts++) = col_.g * inv255f;
+        *(verts++) = col_.b * inv255f;
+        *(verts++) = col_.a * inv255f;
 
         if (texture) {
-            *(verts++) = v->tex_coord.x * texturedata->texw;
-            *(verts++) = v->tex_coord.y * texturedata->texh;
+            float *uv_ = (float *)((char*)uv + j * uv_stride);
+            *(verts++) = uv_[0] * texturedata->texw;
+            *(verts++) = uv_[1] * texturedata->texh;
         }
     }
     return 0;
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 775c0f4..6b99758 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -960,7 +960,9 @@ GLES2_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture *
 
 static int
 GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
-        SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y)
+        const float *xy, int xy_stride, const int *color, int color_stride, const float *uv, int uv_stride,
+        int num_vertices, const void *indices, int num_indices, int size_indice,
+        float scale_x, float scale_y)
 {
     int i;
     const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
@@ -976,20 +978,35 @@ GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture 
     cmd->data.draw.count = count;
 
     for (i = 0; i < count; i++) {
-        SDL_Vertex *v = &vertices[indices ? indices[i] : i];
+        int j;
+        if (size_indice == 4) {
+            j = ((const Uint32 *)indices)[i];
+        } else if (size_indice == 2) {
+            j = ((const Uint16 *)indices)[i];
+        } else if (size_indice == 1) {
+            j = ((const Uint8 *)indices)[i];
+        } else {
+            j = i;
+        }
+
+        float *xy_ = (float *)((char*)xy + j * xy_stride);
+        SDL_Color col_ = *(SDL_Color *)((char*)color + j * color_stride);
 
-        *(verts++) = v->position.x * scale_x;
-        *(verts++) = v->position.y * scale_y;
-        *(verts++) = (colorswap ? v->color.b : v->color.r) * inv255f;
-        *(verts++) = v->color.g * inv255f;
-        *(verts++) = (colorswap ? v->color.r : v->color.b) * inv255f;
-        *(verts++) = v->color.a * inv255f;
+        *(verts++) = xy_[0] * scale_x;
+        *(verts++) = xy_[1] * scale_y;
+
+        *(verts++) = (colorswap ? col_.b : col_.r) * inv255f;
+        *(verts++) = col_.g * inv255f;
+        *(verts++) = (colorswap ? col_.r : col_.b) * inv255f;
+        *(verts++) = col_.a * inv255f;
 
         if (texture) {
-            *(verts++) = v->tex_coord.x;
-            *(verts++) = v->tex_coord.y;
+            float *uv_ = (float *)((char*)uv + j * uv_stride);
+            *(verts++) = uv_[0];
+            *(verts++) = uv_[1];
         }
     }
+
     return 0;
 }
 
diff --git a/src/render/software/SDL_render_sw.c b/src/render/software/SDL_render_sw.c
index fbc948e..0dcdc2a 100644
--- a/src/render/software/SDL_render_sw.c
+++ b/src/render/software/SDL_render_sw.c
@@ -577,7 +577,9 @@ typedef struct GeometryCopyData
 
 static int
 SW_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
-        SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y)
+        const float *xy, int xy_stride, const int *color, int color_stride, const float *uv, int uv_stride,
+        int num_vertices, const void *indices, int num_indices, int size_indice,
+        float scale_x, float scale_y)
 {
     int i;
     int count = indices ? num_indices : num_vertices;
@@ -594,16 +596,30 @@ SW_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te
     if (texture) {
         GeometryCopyData *ptr = (GeometryCopyData *) verts;
         for (i = 0; i < count; i++) {
-            SDL_Vertex *v = &vertices[indices ? indices[i] : i];
+            int j;
+            if (size_indice == 4) {
+                j = ((const Uint32 *)indices)[i];
+            } else if (size_indice == 2) {
+                j = ((const Uint16 *)indices)[i];
+            } else if (size_indice == 1) {
+                j = ((const Uint8 *)indices)[i];
+            } else {
+                j = i;
+            }
+
+            float *xy_ = (float *)((char*)xy + j * xy_stride);
+            SDL_Color col_ = *(SDL_Color *)((char*)color + j * color_stride);
+
+            float *uv_ = (float *)((char*)uv + j * uv_stride);
 
-            ptr->src.x = v->tex_coord.x * texture->w;
-            ptr->src.y = v->tex_coord.y * texture->h;
+            ptr->src.x = uv_[0] * texture->w;
+            ptr->src.y = uv_[1] * texture->h;
 
-            ptr->dst.x = v->position.x * scale_x + renderer->viewport.x;
-            ptr->dst.y = v->position.y * scale_y + renderer->viewport.y;
+            ptr->dst.x = xy_[0] * scale_x + renderer->viewport.x;
+            ptr->dst.y = xy_[1] * scale_y + renderer->viewport.y;
             trianglepoint_2_fixedpoint(&ptr->dst);
 
-            ptr->color = v->color;
+            ptr->color = col_;
 
             ptr++;
        }
@@ -611,18 +627,29 @@ SW_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te
         GeometryFillData *ptr = (GeometryFillData *) verts;
 
         for (i = 0; i < count; i++) {
-            SDL_Vertex *v = &vertices[indices ? indices[i] : i];
+            int j;
+            if (size_indice == 4) {
+                j = ((const Uint32 *)indices)[i];
+            } else if (size_indice == 2) {
+                j = ((const Uint16 *)indices)[i];
+            } else if (size_indice == 1) {
+                j = ((const Uint8 *)indices)[i];
+            } else {
+                j = i;
+            }
 
-            ptr->dst.x = v->position.x * scale_x + renderer->viewport.x;
-            ptr->dst.y = v->position.y * scale_y + renderer->viewport.y;
+            float *xy_ = (float *)((char*)xy + j * xy_stride);
+            SDL_Color col_ = *(SDL_Color *)((char*)color + j * color_stride);
+
+            ptr->dst.x = xy_[0] * scale_x + renderer->viewport.x;
+            ptr->dst.y = xy_[1] * scale_y + renderer->viewport.y;
             trianglepoint_2_fixedpoint(&ptr->dst);
 
-            ptr->color = v->color;
+            ptr->color = col_;
 
             ptr++;
        }
     }
-
     return 0;
 }
 
@@ -700,7 +727,7 @@ SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic
             }
 
             case SDL_RENDERCMD_SETCLIPRECT: {
-                drawstate.cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;                
+                drawstate.cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;
                 drawstate.surface_cliprect_dirty = SDL_TRUE;
                 break;
             }