render: Patched to compile. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Mon, 01 Oct 2018 03:02:54 -0400
branchSDL-ryan-batching-renderer
changeset 122794b8721c990f9
parent 12278 f0e949a32e2f
child 12280 412a6f2078d4
render: Patched to compile.
src/render/direct3d/SDL_render_d3d.c
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/direct3d/SDL_render_d3d.c	Mon Oct 01 01:23:02 2018 -0400
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Mon Oct 01 03:02:54 2018 -0400
     1.3 @@ -30,6 +30,8 @@
     1.4  #include "SDL_hints.h"
     1.5  #include "SDL_loadso.h"
     1.6  #include "SDL_syswm.h"
     1.7 +#include "SDL_log.h"
     1.8 +#include "SDL_assert.h"
     1.9  #include "../SDL_sysrender.h"
    1.10  #include "../SDL_d3dmath.h"
    1.11  #include "../../video/windows/SDL_windowsvideo.h"
    1.12 @@ -74,8 +76,8 @@
    1.13      IDirect3DSurface9 *currentRenderTarget;
    1.14      void* d3dxDLL;
    1.15      LPDIRECT3DPIXELSHADER9 shaders[NUM_SHADERS];
    1.16 -    IDirect3DVertexBuffer9 vertexBuffers[8];
    1.17 -    GLsizeiptr vertexBufferSize[8];
    1.18 +    LPDIRECT3DVERTEXBUFFER9 vertexBuffers[8];
    1.19 +    size_t vertexBufferSize[8];
    1.20      int currentVertexBuffer;
    1.21      SDL_bool reportedVboProblem;
    1.22      D3D_DrawStateCache drawstate;
    1.23 @@ -282,63 +284,7 @@
    1.24      data->beginScene = SDL_TRUE;
    1.25  }
    1.26  
    1.27 -static int
    1.28 -D3D_Reset(SDL_Renderer * renderer)
    1.29 -{
    1.30 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
    1.31 -    HRESULT result;
    1.32 -    SDL_Texture *texture;
    1.33 -
    1.34 -    /* Release the default render target before reset */
    1.35 -    if (data->defaultRenderTarget) {
    1.36 -        IDirect3DSurface9_Release(data->defaultRenderTarget);
    1.37 -        data->defaultRenderTarget = NULL;
    1.38 -    }
    1.39 -    if (data->currentRenderTarget != NULL) {
    1.40 -        IDirect3DSurface9_Release(data->currentRenderTarget);
    1.41 -        data->currentRenderTarget = NULL;
    1.42 -    }
    1.43 -
    1.44 -    /* Release application render targets */
    1.45 -    for (texture = renderer->textures; texture; texture = texture->next) {
    1.46 -        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
    1.47 -            D3D_DestroyTexture(renderer, texture);
    1.48 -        } else {
    1.49 -            D3D_RecreateTexture(renderer, texture);
    1.50 -        }
    1.51 -    }
    1.52 -
    1.53 -    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
    1.54 -    if (FAILED(result)) {
    1.55 -        if (result == D3DERR_DEVICELOST) {
    1.56 -            /* Don't worry about it, we'll reset later... */
    1.57 -            return 0;
    1.58 -        } else {
    1.59 -            return D3D_SetError("Reset()", result);
    1.60 -        }
    1.61 -    }
    1.62 -
    1.63 -    /* Allocate application render targets */
    1.64 -    for (texture = renderer->textures; texture; texture = texture->next) {
    1.65 -        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
    1.66 -            D3D_CreateTexture(renderer, texture);
    1.67 -        }
    1.68 -    }
    1.69 -
    1.70 -    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
    1.71 -    D3D_InitRenderState(data);
    1.72 -    D3D_SetRenderTargetInternal(renderer, renderer->target);
    1.73 -    D3D_UpdateViewport(renderer);
    1.74 -
    1.75 -    /* Let the application know that render targets were reset */
    1.76 -    {
    1.77 -        SDL_Event event;
    1.78 -        event.type = SDL_RENDER_TARGETS_RESET;
    1.79 -        SDL_PushEvent(&event);
    1.80 -    }
    1.81 -
    1.82 -    return 0;
    1.83 -}
    1.84 +static int D3D_Reset(SDL_Renderer * renderer);
    1.85  
    1.86  static int
    1.87  D3D_ActivateRenderer(SDL_Renderer * renderer)
    1.88 @@ -933,8 +879,6 @@
    1.89      minv = (float) srcrect->y / texture->h;
    1.90      maxv = (float) (srcrect->y + srcrect->h) / texture->h;
    1.91  
    1.92 -    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
    1.93 -
    1.94      verts->x = minx;
    1.95      verts->y = miny;
    1.96      verts->z = 0.0f;
    1.97 @@ -1141,7 +1085,7 @@
    1.98  }
    1.99  
   1.100  static int
   1.101 -SetDrawState(D3D_RenderData *data, const SDL_RenderCommand *cmd, const D3D_ImageSource imgsrc)
   1.102 +SetDrawState(D3D_RenderData *data, const SDL_RenderCommand *cmd)
   1.103  {
   1.104      const SDL_bool was_copy_ex = data->drawstate.is_copy_ex;
   1.105      const SDL_bool is_copy_ex = (cmd->command == SDL_RENDERCMD_COPY_EX);
   1.106 @@ -1161,12 +1105,13 @@
   1.107              IDirect3DDevice9_SetTexture(data->device, 1, NULL);
   1.108              IDirect3DDevice9_SetTexture(data->device, 2, NULL);
   1.109          }
   1.110 -        if (texture && RenderSetupTextureState(renderer, texture, &shader) < 0) {
   1.111 +        if (texture && SetupTextureState(data, texture, &shader) < 0) {
   1.112              return -1;
   1.113          }
   1.114  
   1.115 -        if (shader != data->drawstate.shader)
   1.116 -            if (FAILED(IDirect3DDevice9_SetPixelShader(data->device, shader))) {
   1.117 +        if (shader != data->drawstate.shader) {
   1.118 +            const HRESULT result = IDirect3DDevice9_SetPixelShader(data->device, shader);
   1.119 +            if (FAILED(result)) {
   1.120                  return D3D_SetError("IDirect3DDevice9_SetPixelShader()", result);
   1.121              }
   1.122              data->drawstate.shader = shader;
   1.123 @@ -1181,14 +1126,14 @@
   1.124          } else {
   1.125              IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, TRUE);
   1.126              IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   1.127 -                                            GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blendMode)));
   1.128 +                                            GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)));
   1.129              IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   1.130 -                                            GetBlendFunc(SDL_GetBlendModeDstColorFactor(blendMode)));
   1.131 +                                            GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)));
   1.132              if (data->enableSeparateAlphaBlend) {
   1.133                  IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
   1.134 -                                                GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blendMode)));
   1.135 +                                                GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blend)));
   1.136                  IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
   1.137 -                                                GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blendMode)));
   1.138 +                                                GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blend)));
   1.139              }
   1.140          }
   1.141  
   1.142 @@ -1263,16 +1208,16 @@
   1.143                  IDirect3DVertexBuffer9_Release(vbo);
   1.144              }
   1.145  
   1.146 -            if (FAILED(IDirect3DVertexBuffer9_CreateVertexBuffer(vertsize, usage, fvf, D3DPOOL_DEFAULT, &vbo, NULL))) {
   1.147 +            if (FAILED(IDirect3DDevice9_CreateVertexBuffer(data->device, vertsize, usage, fvf, D3DPOOL_MANAGED, &vbo, NULL))) {
   1.148                  vbo = NULL;
   1.149              }
   1.150              data->vertexBuffers[vboidx] = vbo;
   1.151 -            data->vertex_buffer_size[vboidx] = vbo ? vertsize : 0;
   1.152 +            data->vertexBufferSize[vboidx] = vbo ? vertsize : 0;
   1.153          }
   1.154  
   1.155          if (vbo) {
   1.156              void *ptr;
   1.157 -            if (FAILED(IDirect3DVertexBuffer9_Lock(vbo, 0, vertsize, &ptr, D3DLOCK_DISCARD)) {
   1.158 +            if (FAILED(IDirect3DVertexBuffer9_Lock(vbo, 0, vertsize, &ptr, D3DLOCK_DISCARD))) {
   1.159                  vbo = NULL;  /* oh well, we'll do immediate mode drawing.  :(  */
   1.160              } else {
   1.161                  SDL_memcpy(ptr, vertices, vertsize);
   1.162 @@ -1321,7 +1266,7 @@
   1.163                  const SDL_Rect *rect = &cmd->data.cliprect.rect;
   1.164                  if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
   1.165                      data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
   1.166 -                    data->drawstate.cliprect_enabled_dirty = true;
   1.167 +                    data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
   1.168                  }
   1.169  
   1.170                  if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof (SDL_Rect)) != 0) {
   1.171 @@ -1335,16 +1280,16 @@
   1.172                  const DWORD color = D3DCOLOR_ARGB(cmd->data.color.a, cmd->data.color.r, cmd->data.color.g, cmd->data.color.b);
   1.173                  const SDL_Rect *viewport = &data->drawstate.viewport;
   1.174                  const int backw = istarget ? renderer->target->w : data->pparams.BackBufferWidth;
   1.175 -                const int backh = istarget ? renderer->target->h : data->pparams.backh;
   1.176 +                const int backh = istarget ? renderer->target->h : data->pparams.BackBufferHeight;
   1.177  
   1.178 -                if (data->drawstate.clipping_enabled) {
   1.179 +                if (data->drawstate.cliprect_enabled) {
   1.180                      IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, FALSE);
   1.181                      data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
   1.182                  }
   1.183  
   1.184                  /* Don't reset the viewport if we don't have to! */
   1.185 -                if (!viewport->.x && !viewport->y && (viewport->w == backw) && (viewport->h == backh)) {
   1.186 -                    result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
   1.187 +                if (!viewport->x && !viewport->y && (viewport->w == backw) && (viewport->h == backh)) {
   1.188 +                    IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
   1.189                  } else {
   1.190                      /* Clear is defined to clear the entire render target */
   1.191                      const D3DVIEWPORT9 wholeviewport = { 0, 0, backw, backh, 0.0f, 1.0f };
   1.192 @@ -1371,9 +1316,9 @@
   1.193              }
   1.194  
   1.195              case SDL_RENDERCMD_DRAW_LINES: {
   1.196 -                const Vertex *verts = (Vertex *) (((Uint8 *) vertices) + first);
   1.197                  const size_t count = cmd->data.draw.count;
   1.198                  const size_t first = cmd->data.draw.first;
   1.199 +                const Vertex *verts = (Vertex *) (((Uint8 *) vertices) + first);
   1.200  
   1.201                  /* DirectX 9 has the same line rasterization semantics as GDI,
   1.202                     so we need to close the endpoint of the line with a second draw call. */
   1.203 @@ -1435,6 +1380,7 @@
   1.204              }
   1.205  
   1.206              case SDL_RENDERCMD_COPY_EX: {
   1.207 +                const size_t first = cmd->data.draw.first;
   1.208                  const Vertex *verts = (Vertex *) (((Uint8 *) vertices) + first);
   1.209                  const Vertex *transvert = verts + 4;
   1.210                  const float translatex = transvert->x;
   1.211 @@ -1602,6 +1548,64 @@
   1.212      SDL_free(renderer);
   1.213  }
   1.214  
   1.215 +static int
   1.216 +D3D_Reset(SDL_Renderer * renderer)
   1.217 +{
   1.218 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.219 +    HRESULT result;
   1.220 +    SDL_Texture *texture;
   1.221 +
   1.222 +    /* Release the default render target before reset */
   1.223 +    if (data->defaultRenderTarget) {
   1.224 +        IDirect3DSurface9_Release(data->defaultRenderTarget);
   1.225 +        data->defaultRenderTarget = NULL;
   1.226 +    }
   1.227 +    if (data->currentRenderTarget != NULL) {
   1.228 +        IDirect3DSurface9_Release(data->currentRenderTarget);
   1.229 +        data->currentRenderTarget = NULL;
   1.230 +    }
   1.231 +
   1.232 +    /* Release application render targets */
   1.233 +    for (texture = renderer->textures; texture; texture = texture->next) {
   1.234 +        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
   1.235 +            D3D_DestroyTexture(renderer, texture);
   1.236 +        } else {
   1.237 +            D3D_RecreateTexture(renderer, texture);
   1.238 +        }
   1.239 +    }
   1.240 +
   1.241 +    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
   1.242 +    if (FAILED(result)) {
   1.243 +        if (result == D3DERR_DEVICELOST) {
   1.244 +            /* Don't worry about it, we'll reset later... */
   1.245 +            return 0;
   1.246 +        } else {
   1.247 +            return D3D_SetError("Reset()", result);
   1.248 +        }
   1.249 +    }
   1.250 +
   1.251 +    /* Allocate application render targets */
   1.252 +    for (texture = renderer->textures; texture; texture = texture->next) {
   1.253 +        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
   1.254 +            D3D_CreateTexture(renderer, texture);
   1.255 +        }
   1.256 +    }
   1.257 +
   1.258 +    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
   1.259 +    D3D_InitRenderState(data);
   1.260 +    D3D_SetRenderTargetInternal(renderer, renderer->target);
   1.261 +    data->drawstate.viewport_dirty = SDL_TRUE;
   1.262 +
   1.263 +    /* Let the application know that render targets were reset */
   1.264 +    {
   1.265 +        SDL_Event event;
   1.266 +        event.type = SDL_RENDER_TARGETS_RESET;
   1.267 +        SDL_PushEvent(&event);
   1.268 +    }
   1.269 +
   1.270 +    return 0;
   1.271 +}
   1.272 +
   1.273  SDL_Renderer *
   1.274  D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.275  {
   1.276 @@ -1708,7 +1712,7 @@
   1.277          device_flags |= D3DCREATE_MULTITHREADED;
   1.278      }
   1.279  
   1.280 -    data->supportsStreamOffset = ((caps.Caps2 & D3DDEVCAPS2_STREAMOFFSET) == D3DDEVCAPS2_STREAMOFFSET);
   1.281 +    data->supportsStreamOffset = ((caps.DevCaps2 & D3DDEVCAPS2_STREAMOFFSET) == D3DDEVCAPS2_STREAMOFFSET);
   1.282  
   1.283      result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
   1.284                                       D3DDEVTYPE_HAL,
     2.1 --- a/src/render/opengles2/SDL_render_gles2.c	Mon Oct 01 01:23:02 2018 -0400
     2.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Mon Oct 01 03:02:54 2018 -0400
     2.3 @@ -951,7 +951,7 @@
     2.4                  data->drawstate.texturing = SDL_FALSE;
     2.5              } else {
     2.6                  data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
     2.7 -                data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) cmd->data.draw.first + (sizeof (GLfloat) * 8));
     2.8 +                data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (cmd->data.draw.first + (sizeof (GLfloat) * 8)));
     2.9                  data->drawstate.texturing = SDL_TRUE;
    2.10              }
    2.11          }