GLES2 backend: cast with SDL_Vertex and SDL_VertexSolid
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
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 2c49c15..d3992dd 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -107,6 +107,13 @@ typedef struct SDL_RenderCommand
} SDL_RenderCommand;
+typedef struct SDL_VertexSolid
+{
+ SDL_FPoint position;
+ SDL_Color color;
+} SDL_VertexSolid;
+
+
/* Define the SDL renderer structure */
struct SDL_Renderer
{
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index ae49503..167d8ee 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -663,8 +663,7 @@ static int
GLES2_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
- const size_t vertlen = (2 * sizeof (float) + sizeof (int)) * count;
- GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
+ SDL_VertexSolid *verts = (SDL_VertexSolid *) SDL_AllocateRenderVertices(renderer, count * sizeof(*verts), 0, &cmd->data.draw.first);
int i;
SDL_Color color;
color.r = cmd->data.draw.r;
@@ -684,9 +683,10 @@ GLES2_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL
cmd->data.draw.count = count;
for (i = 0; i < count; i++) {
- *(verts++) = 0.5f + points[i].x;
- *(verts++) = 0.5f + points[i].y;
- *((SDL_Color *)verts++) = color;
+ verts->position.y = 0.5f + points[i].x;
+ verts->position.x = 0.5f + points[i].y;
+ verts->color = color;
+ verts++;
}
return 0;
@@ -698,8 +698,7 @@ GLES2_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
int i;
GLfloat prevx, prevy;
- const size_t vertlen = ((2 * sizeof (float)) + sizeof (int)) * count;
- GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
+ SDL_VertexSolid *verts = (SDL_VertexSolid *) SDL_AllocateRenderVertices(renderer, count * sizeof(*verts), 0, &cmd->data.draw.first);
SDL_Color color;
color.r = cmd->data.draw.r;
color.g = cmd->data.draw.g;
@@ -721,9 +720,10 @@ GLES2_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
/* 0.5f offset to hit the center of the pixel. */
prevx = 0.5f + points->x;
prevy = 0.5f + points->y;
- *(verts++) = prevx;
- *(verts++) = prevy;
- *((SDL_Color*)verts++) = color;
+ verts->position.x = prevx;
+ verts->position.y = prevy;
+ verts->color = color;
+ verts++;
/* bump the end of each line segment out a quarter of a pixel, to provoke
the diamond-exit rule. Without this, you won't just drop the last
@@ -740,9 +740,10 @@ GLES2_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
const GLfloat angle = SDL_atan2f(deltay, deltax);
prevx = xend + (SDL_cosf(angle) * 0.25f);
prevy = yend + (SDL_sinf(angle) * 0.25f);
- *(verts++) = prevx;
- *(verts++) = prevy;
- *((SDL_Color*)verts++) = color;
+ verts->position.x = prevx;
+ verts->position.y = prevy;
+ verts->color = color;
+ verts++;
}
return 0;
@@ -757,48 +758,85 @@ GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
int i;
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888));
int count = indices ? num_indices : num_vertices;
- const size_t vertlen = (2 * sizeof (float) + sizeof (int) + (texture ? 2 : 0) * sizeof (float)) * count;
- GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
-
- if (!verts) {
- return -1;
- }
cmd->data.draw.count = count;
size_indices = indices ? size_indices : 0;
- for (i = 0; i < count; i++) {
- int j;
- float *xy_;
- SDL_Color col_;
- if (size_indices == 4) {
- j = ((const Uint32 *)indices)[i];
- } else if (size_indices == 2) {
- j = ((const Uint16 *)indices)[i];
- } else if (size_indices == 1) {
- j = ((const Uint8 *)indices)[i];
- } else {
- j = i;
+ if (texture) {
+ SDL_Vertex *verts = (SDL_Vertex *) SDL_AllocateRenderVertices(renderer, count * sizeof (*verts), 0, &cmd->data.draw.first);
+ if (!verts) {
+ return -1;
}
- xy_ = (float *)((char*)xy + j * xy_stride);
- col_ = *(SDL_Color *)((char*)color + j * color_stride);
+ for (i = 0; i < count; i++) {
+ int j;
+ float *xy_;
+ SDL_Color col_;
+ float *uv_;
+ if (size_indices == 4) {
+ j = ((const Uint32 *)indices)[i];
+ } else if (size_indices == 2) {
+ j = ((const Uint16 *)indices)[i];
+ } else if (size_indices == 1) {
+ j = ((const Uint8 *)indices)[i];
+ } else {
+ j = i;
+ }
+
+ xy_ = (float *)((char*)xy + j * xy_stride);
+ col_ = *(SDL_Color *)((char*)color + j * color_stride);
+ uv_ = (float *)((char*)uv + j * uv_stride);
- *(verts++) = xy_[0] * scale_x;
- *(verts++) = xy_[1] * scale_y;
+ verts->position.x = xy_[0] * scale_x;
+ verts->position.y = xy_[1] * scale_y;
- if (colorswap) {
- Uint8 r = col_.r;
- col_.r = col_.b;
- col_.b = r;
+ if (colorswap) {
+ Uint8 r = col_.r;
+ col_.r = col_.b;
+ col_.b = r;
+ }
+
+ verts->color = col_;
+ verts->tex_coord.x = uv_[0];
+ verts->tex_coord.y = uv_[1];
+ verts++;
}
- *((SDL_Color *)verts++) = col_;
+ } else {
+ SDL_VertexSolid *verts = (SDL_VertexSolid *) SDL_AllocateRenderVertices(renderer, count * sizeof (*verts), 0, &cmd->data.draw.first);
+ if (!verts) {
+ return -1;
+ }
- if (texture) {
- float *uv_ = (float *)((char*)uv + j * uv_stride);
- *(verts++) = uv_[0];
- *(verts++) = uv_[1];
+ for (i = 0; i < count; i++) {
+ int j;
+ float *xy_;
+ SDL_Color col_;
+
+ if (size_indices == 4) {
+ j = ((const Uint32 *)indices)[i];
+ } else if (size_indices == 2) {
+ j = ((const Uint16 *)indices)[i];
+ } else if (size_indices == 1) {
+ j = ((const Uint8 *)indices)[i];
+ } else {
+ j = i;
+ }
+
+ xy_ = (float *)((char*)xy + j * xy_stride);
+ col_ = *(SDL_Color *)((char*)color + j * color_stride);
+
+ verts->position.x = xy_[0] * scale_x;
+ verts->position.y = xy_[1] * scale_y;
+
+ if (colorswap) {
+ Uint8 r = col_.r;
+ col_.r = col_.b;
+ col_.b = r;
+ }
+
+ verts->color = col_;
+ verts++;
}
}
@@ -811,7 +849,7 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
SDL_Texture *texture = cmd->data.draw.texture;
const SDL_BlendMode blend = cmd->data.draw.blend;
GLES2_ProgramCacheEntry *program;
- int stride = sizeof (GLfloat) * 2 /* position */ + sizeof (int) /* color */;
+ int stride;
SDL_assert((texture != NULL) == (imgsrc != GLES2_IMAGESOURCE_SOLID));
@@ -882,11 +920,14 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
}
if (texture) {
- stride += sizeof (GLfloat) * 2; /* tex coord */
+ stride = sizeof(SDL_Vertex);
+ } else {
+ stride = sizeof(SDL_VertexSolid);
}
if (texture) {
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) (uintptr_t) (cmd->data.draw.first + sizeof (GLfloat) * (2 + 1)));
+ SDL_Vertex *verts = (SDL_Vertex *) (cmd->data.draw.first);
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *)&verts->tex_coord);
}
if (GLES2_SelectProgram(data, imgsrc, texture ? texture->w : 0, texture ? texture->h : 0) < 0) {
@@ -918,8 +959,11 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
}
/* all drawing commands use this */
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) (uintptr_t) cmd->data.draw.first);
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE /* Normalized */, stride, (const GLvoid *) (uintptr_t) (cmd->data.draw.first + sizeof (GLfloat) * 2));
+ {
+ SDL_VertexSolid *verts = (SDL_VertexSolid *) (cmd->data.draw.first);
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, stride, (const GLvoid *) &verts->position);
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE /* Normalized */, stride, (const GLvoid *) &verts->color);
+ }
return 0;
}
@@ -1206,7 +1250,7 @@ GLES2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *ver
if (ret == 0) {
int op = GL_TRIANGLES; /* SDL_RENDERCMD_GEOMETRY */
if (thiscmdtype == SDL_RENDERCMD_DRAW_POINTS) {
- op = GL_POINTS;
+ op = GL_POINTS;
}
data->glDrawArrays(op, 0, (GLsizei) count);
}