render: Make opengl backend take advantage of new high-level features. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Mon, 24 Sep 2018 02:08:34 -0400
branchSDL-ryan-batching-renderer
changeset 12217c471497be83e
parent 12216 acf4b9f4573f
child 12218 dbce97a40237
render: Make opengl backend take advantage of new high-level features.
src/render/opengl/SDL_render_gl.c
     1.1 --- a/src/render/opengl/SDL_render_gl.c	Mon Sep 24 02:07:35 2018 -0400
     1.2 +++ b/src/render/opengl/SDL_render_gl.c	Mon Sep 24 02:08:34 2018 -0400
     1.3 @@ -1161,23 +1161,12 @@
     1.4  }
     1.5  
     1.6  static void
     1.7 -SetDrawState(const GL_RenderData *data, const SDL_RenderCommand *cmd, const GL_Shader shader,
     1.8 -             Uint32 *current_color, SDL_BlendMode *current_blend, GL_Shader *current_shader,
     1.9 -             SDL_bool *current_texturing)
    1.10 +SetDrawState(const GL_RenderData *data, const SDL_RenderCommand *cmd,
    1.11 +             const GL_Shader shader, SDL_BlendMode *current_blend,
    1.12 +             GL_Shader *current_shader, SDL_bool *current_texturing)
    1.13  {
    1.14 -    const Uint8 r = cmd->data.draw.r;
    1.15 -    const Uint8 g = cmd->data.draw.g;
    1.16 -    const Uint8 b = cmd->data.draw.b;
    1.17 -    const Uint8 a = cmd->data.draw.a;
    1.18 -    const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
    1.19      const SDL_BlendMode blend = cmd->data.draw.blend;
    1.20  
    1.21 -    if (color != *current_color) {
    1.22 -        data->glColor4f((GLfloat) r * inv255f, (GLfloat) g * inv255f,
    1.23 -                        (GLfloat) b * inv255f, (GLfloat) a * inv255f);
    1.24 -        *current_color = color;
    1.25 -    }
    1.26 -
    1.27      if (blend != *current_blend) {
    1.28          if (blend == SDL_BLENDMODE_NONE) {
    1.29              data->glDisable(GL_BLEND);
    1.30 @@ -1210,7 +1199,7 @@
    1.31  
    1.32  static void
    1.33  SetCopyState(const GL_RenderData *data, const SDL_RenderCommand *cmd,
    1.34 -             Uint32 *current_color, SDL_BlendMode *current_blend, GL_Shader *current_shader,
    1.35 +             SDL_BlendMode *current_blend, GL_Shader *current_shader,
    1.36               SDL_bool *current_texturing, SDL_Texture **current_texture)
    1.37  {
    1.38      SDL_Texture *texture = cmd->data.draw.texture;
    1.39 @@ -1254,7 +1243,7 @@
    1.40          }
    1.41      }
    1.42  
    1.43 -    SetDrawState(data, cmd, shader, current_color, current_blend, current_shader, current_texturing);
    1.44 +    SetDrawState(data, cmd, shader, current_blend, current_shader, current_texturing);
    1.45  
    1.46      if (texture != *current_texture) {
    1.47          const GLenum textype = data->textype;
    1.48 @@ -1281,23 +1270,16 @@
    1.49  {
    1.50      /* !!! FIXME: it'd be nice to use a vertex buffer instead of immediate mode... */
    1.51      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    1.52 -    SDL_bool clipping_enabled = renderer->clipping_enabled;
    1.53      SDL_Rect viewport;
    1.54      SDL_Texture *bound_texture = NULL;
    1.55      SDL_BlendMode blend = SDL_BLENDMODE_INVALID;
    1.56      GL_Shader shader = SHADER_INVALID;
    1.57      int drawablew = 0, drawableh = 0;
    1.58      SDL_bool cliprect_enabled = SDL_FALSE;
    1.59 -    SDL_Rect cliprect;
    1.60      const SDL_bool istarget = renderer->target != NULL;
    1.61 -    Uint32 clear_color = ((renderer->a << 24) | (renderer->r << 16) | (renderer->g << 8) | renderer->b);
    1.62 -    Uint32 draw_color = clear_color;
    1.63      SDL_bool texturing = SDL_FALSE;
    1.64      size_t i;
    1.65  
    1.66 -    /* !!! FIXME: if we could guarantee the app isn't messing with the GL, too, we wouldn't
    1.67 -       !!! FIXME:  have to default a bunch of this state at the start. */
    1.68 -
    1.69      if (GL_ActivateRenderer(renderer) < 0) {
    1.70          return -1;
    1.71      }
    1.72 @@ -1306,85 +1288,47 @@
    1.73          SDL_GL_GetDrawableSize(renderer->window, &drawablew, &drawableh);
    1.74      }
    1.75  
    1.76 -    data->glClearColor((GLfloat) renderer->r * inv255f,
    1.77 -                       (GLfloat) renderer->g * inv255f,
    1.78 -                       (GLfloat) renderer->b * inv255f,
    1.79 -                       (GLfloat) renderer->a * inv255f);
    1.80 -
    1.81 -    data->glColor4f((GLfloat) renderer->r * inv255f,
    1.82 -                    (GLfloat) renderer->g * inv255f,
    1.83 -                    (GLfloat) renderer->b * inv255f,
    1.84 -                    (GLfloat) renderer->a * inv255f);
    1.85 -
    1.86 -    SDL_memcpy(&viewport, &renderer->viewport, sizeof (viewport));
    1.87 -    data->glMatrixMode(GL_PROJECTION);
    1.88 -    data->glLoadIdentity();
    1.89 -    data->glViewport(viewport.x,
    1.90 -            istarget ? viewport.y : (drawableh - viewport.y - viewport.h),
    1.91 -            viewport.w, viewport.h);
    1.92 -    if (viewport.w && viewport.h) {
    1.93 -        data->glOrtho((GLdouble) 0, (GLdouble) renderer->viewport.w,
    1.94 -                      (GLdouble) istarget ? 0 : renderer->viewport.h,
    1.95 -                      (GLdouble) istarget ? renderer->viewport.h : 0,
    1.96 -                      0.0, 1.0);
    1.97 -    }
    1.98 +    data->glDisable(data->textype);
    1.99      data->glMatrixMode(GL_MODELVIEW);
   1.100      data->glLoadIdentity();
   1.101  
   1.102 -    SDL_memcpy(&cliprect, &renderer->clip_rect, sizeof (cliprect));
   1.103 -    cliprect_enabled = renderer->clipping_enabled;
   1.104 -    if (cliprect_enabled) {
   1.105 -        data->glEnable(GL_SCISSOR_TEST);
   1.106 -    } else {
   1.107 -        data->glDisable(GL_SCISSOR_TEST);
   1.108 -    }
   1.109 -
   1.110 -    data->glDisable(data->textype);
   1.111 -
   1.112 -    data->glScissor(viewport.x + cliprect.x,
   1.113 -        istarget ? viewport.y + cliprect.y : drawableh - viewport.y - cliprect.y - cliprect.h,
   1.114 -        cliprect.w, cliprect.h);
   1.115 -
   1.116      while (cmd) {
   1.117          switch (cmd->command) {
   1.118              case SDL_RENDERCMD_SETDRAWCOLOR: {
   1.119 -                // !!! FIXME: use this.
   1.120 +                data->glColor4f((GLfloat) cmd->data.color.r * inv255f,
   1.121 +                                (GLfloat) cmd->data.color.g * inv255f,
   1.122 +                                (GLfloat) cmd->data.color.b * inv255f,
   1.123 +                                (GLfloat) cmd->data.color.a * inv255f);
   1.124                  break;
   1.125              }
   1.126  
   1.127              case SDL_RENDERCMD_SETVIEWPORT: {
   1.128 -                if (SDL_memcmp(&cmd->data.viewport.rect, &viewport, sizeof (viewport)) != 0) {
   1.129 -                    SDL_memcpy(&viewport, &cmd->data.viewport.rect, sizeof (viewport));
   1.130 -                    data->glMatrixMode(GL_PROJECTION);
   1.131 -                    data->glLoadIdentity();
   1.132 -                    data->glViewport(viewport.x,
   1.133 +                SDL_memcpy(&viewport, &cmd->data.viewport.rect, sizeof (viewport));
   1.134 +                data->glMatrixMode(GL_PROJECTION);
   1.135 +                data->glLoadIdentity();
   1.136 +                data->glViewport(viewport.x,
   1.137                          istarget ? viewport.y : (drawableh - viewport.y - viewport.h),
   1.138                          viewport.w, viewport.h);
   1.139 -                    if (viewport.w && viewport.h) {
   1.140 -                        data->glOrtho((GLdouble) 0, (GLdouble) renderer->viewport.w,
   1.141 -                                      (GLdouble) istarget ? 0 : renderer->viewport.h,
   1.142 -                                      (GLdouble) istarget ? renderer->viewport.h : 0,
   1.143 -                                      0.0, 1.0);
   1.144 -                    }
   1.145 -                    data->glMatrixMode(GL_MODELVIEW);
   1.146 +                if (viewport.w && viewport.h) {
   1.147 +                    data->glOrtho((GLdouble) 0, (GLdouble) renderer->viewport.w,
   1.148 +                                  (GLdouble) istarget ? 0 : renderer->viewport.h,
   1.149 +                                  (GLdouble) istarget ? renderer->viewport.h : 0,
   1.150 +                                  0.0, 1.0);
   1.151                  }
   1.152 +                data->glMatrixMode(GL_MODELVIEW);
   1.153                  break;
   1.154              }
   1.155  
   1.156              case SDL_RENDERCMD_SETCLIPRECT: {
   1.157                  const SDL_Rect *rect = &cmd->data.cliprect.rect;
   1.158 -                const SDL_bool changed = (SDL_memcmp(&cliprect, rect, sizeof (SDL_Rect)) != 0);
   1.159 -                if (cliprect_enabled != cmd->data.cliprect.enabled) {
   1.160 -                    cliprect_enabled = cmd->data.cliprect.enabled;
   1.161 -                    if (cliprect_enabled) {
   1.162 -                        data->glEnable(GL_SCISSOR_TEST);
   1.163 -                    } else {
   1.164 -                        data->glDisable(GL_SCISSOR_TEST);
   1.165 -                    }
   1.166 +                cliprect_enabled = cmd->data.cliprect.enabled;
   1.167 +                if (cliprect_enabled) {
   1.168 +                    data->glEnable(GL_SCISSOR_TEST);
   1.169 +                } else {
   1.170 +                    data->glDisable(GL_SCISSOR_TEST);
   1.171                  }
   1.172  
   1.173 -                if (cliprect_enabled && changed) {
   1.174 -                    SDL_memcpy(&cliprect, rect, sizeof (SDL_Rect));
   1.175 +                if (cliprect_enabled) {
   1.176                      data->glScissor(viewport.x + rect->x,
   1.177                                      istarget ? viewport.y + rect->y : drawableh - viewport.y - rect->y - rect->h,
   1.178                                      rect->w, rect->h);
   1.179 @@ -1393,24 +1337,19 @@
   1.180              }
   1.181  
   1.182              case SDL_RENDERCMD_CLEAR: {
   1.183 -                const Uint8 r = cmd->data.color.r;
   1.184 -                const Uint8 g = cmd->data.color.g;
   1.185 -                const Uint8 b = cmd->data.color.b;
   1.186 -                const Uint8 a = cmd->data.color.a;
   1.187 -                const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
   1.188 -                if (color != clear_color) {
   1.189 -                    data->glClearColor((GLfloat) r * inv255f, (GLfloat) g * inv255f,
   1.190 -                                       (GLfloat) b * inv255f, (GLfloat) a * inv255f);
   1.191 -                    clear_color = color;
   1.192 -                }
   1.193 +                const GLfloat r = ((GLfloat) cmd->data.color.r) * inv255f;
   1.194 +                const GLfloat g = ((GLfloat) cmd->data.color.g) * inv255f;
   1.195 +                const GLfloat b = ((GLfloat) cmd->data.color.b) * inv255f;
   1.196 +                const GLfloat a = ((GLfloat) cmd->data.color.a) * inv255f;
   1.197 +                data->glClearColor(r, g, b, a);
   1.198  
   1.199 -                if (clipping_enabled) {
   1.200 +                if (cliprect_enabled) {
   1.201                      data->glDisable(GL_SCISSOR_TEST);
   1.202                  }
   1.203  
   1.204                  data->glClear(GL_COLOR_BUFFER_BIT);
   1.205  
   1.206 -                if (clipping_enabled) {
   1.207 +                if (cliprect_enabled) {
   1.208                      data->glEnable(GL_SCISSOR_TEST);
   1.209                  }
   1.210                  break;
   1.211 @@ -1419,7 +1358,7 @@
   1.212              case SDL_RENDERCMD_DRAW_POINTS: {
   1.213                  const size_t count = cmd->data.draw.count;
   1.214                  const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
   1.215 -                SetDrawState(data, cmd, SHADER_SOLID, &draw_color, &blend, &shader, &texturing);
   1.216 +                SetDrawState(data, cmd, SHADER_SOLID, &blend, &shader, &texturing);
   1.217                  data->glBegin(GL_POINTS);
   1.218                  for (i = 0; i < count; i++, verts += 2) {
   1.219                      data->glVertex2f(verts[0], verts[1]);
   1.220 @@ -1431,7 +1370,7 @@
   1.221              case SDL_RENDERCMD_DRAW_LINES: {
   1.222                  const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
   1.223                  size_t count = cmd->data.draw.count;
   1.224 -                SetDrawState(data, cmd, SHADER_SOLID, &draw_color, &blend, &shader, &texturing);
   1.225 +                SetDrawState(data, cmd, SHADER_SOLID, &blend, &shader, &texturing);
   1.226                  if (count > 2 && (verts[0] == verts[(count-1)*2]) && (verts[1] == verts[(count*2)-1])) {
   1.227                      --count;  /* GL_LINE_LOOP takes care of the final segment */
   1.228                      data->glBegin(GL_LINE_LOOP);
   1.229 @@ -1489,7 +1428,7 @@
   1.230              case SDL_RENDERCMD_FILL_RECTS: {
   1.231                  const size_t count = cmd->data.draw.count;
   1.232                  const GLfloat *verts = (GLfloat *) (((Uint8 *) vertices) + cmd->data.draw.first);
   1.233 -                SetDrawState(data, cmd, SHADER_SOLID, &draw_color, &blend, &shader, &texturing);
   1.234 +                SetDrawState(data, cmd, SHADER_SOLID, &blend, &shader, &texturing);
   1.235                  for (i = 0; i < count; ++i, verts += 4) {
   1.236                      data->glRectf(verts[0], verts[1], verts[2], verts[3]);
   1.237                  }
   1.238 @@ -1506,7 +1445,7 @@
   1.239                  const GLfloat maxu = verts[5];
   1.240                  const GLfloat minv = verts[6];
   1.241                  const GLfloat maxv = verts[7];
   1.242 -                SetCopyState(data, cmd, &draw_color, &blend, &shader, &texturing, &bound_texture);
   1.243 +                SetCopyState(data, cmd, &blend, &shader, &texturing, &bound_texture);
   1.244                  data->glBegin(GL_TRIANGLE_STRIP);
   1.245                  data->glTexCoord2f(minu, minv);
   1.246                  data->glVertex2f(minx, miny);
   1.247 @@ -1533,7 +1472,7 @@
   1.248                  const GLfloat translatex = verts[8];
   1.249                  const GLfloat translatey = verts[9];
   1.250                  const GLdouble angle = verts[10];
   1.251 -                SetCopyState(data, cmd, &draw_color, &blend, &shader, &texturing, &bound_texture);
   1.252 +                SetCopyState(data, cmd, &blend, &shader, &texturing, &bound_texture);
   1.253  
   1.254                  /* Translate to flip, rotate, translate to position */
   1.255                  data->glPushMatrix();