render: Set the D3D9 stream source once and choose offsets during draw calls. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Mon, 01 Oct 2018 11:32:08 -0400
branchSDL-ryan-batching-renderer
changeset 12280412a6f2078d4
parent 12279 4b8721c990f9
child 12281 838c5562d05f
render: Set the D3D9 stream source once and choose offsets during draw calls.

This is _much_ faster than setting the offsets with SetStreamSource!
src/render/direct3d/SDL_render_d3d.c
     1.1 --- a/src/render/direct3d/SDL_render_d3d.c	Mon Oct 01 03:02:54 2018 -0400
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Mon Oct 01 11:32:08 2018 -0400
     1.3 @@ -1208,7 +1208,7 @@
     1.4                  IDirect3DVertexBuffer9_Release(vbo);
     1.5              }
     1.6  
     1.7 -            if (FAILED(IDirect3DDevice9_CreateVertexBuffer(data->device, vertsize, usage, fvf, D3DPOOL_MANAGED, &vbo, NULL))) {
     1.8 +            if (FAILED(IDirect3DDevice9_CreateVertexBuffer(data->device, vertsize, usage, fvf, D3DPOOL_DEFAULT, &vbo, NULL))) {
     1.9                  vbo = NULL;
    1.10              }
    1.11              data->vertexBuffers[vboidx] = vbo;
    1.12 @@ -1244,6 +1244,8 @@
    1.13          data->reportedVboProblem = SDL_TRUE;
    1.14      }
    1.15  
    1.16 +    IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, 0, sizeof (Vertex));
    1.17 +
    1.18      while (cmd) {
    1.19          switch (cmd->command) {
    1.20              case SDL_RENDERCMD_SETDRAWCOLOR: {
    1.21 @@ -1306,8 +1308,7 @@
    1.22                  const size_t first = cmd->data.draw.first;
    1.23                  SetDrawState(data, cmd);
    1.24                  if (vbo) {
    1.25 -                    IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, (UINT) first, sizeof (Vertex));
    1.26 -                    IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_POINTLIST, 0, count);
    1.27 +                    IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_POINTLIST, first / sizeof (Vertex), count);
    1.28                  } else {
    1.29                      const Vertex *verts = (Vertex *) (((Uint8 *) vertices) + first);
    1.30                      IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count, vertices, sizeof (Vertex));
    1.31 @@ -1327,10 +1328,9 @@
    1.32                  SetDrawState(data, cmd);
    1.33  
    1.34                  if (vbo) {
    1.35 -                    IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, (UINT) first, sizeof (Vertex));
    1.36 -                    IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_LINESTRIP, 0, count - 1);
    1.37 +                    IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_LINESTRIP, first / sizeof (Vertex), count - 1);
    1.38                      if (close_endpoint) {
    1.39 -                        IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_POINTLIST, count - 1, 1);
    1.40 +                        IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_POINTLIST, (first / sizeof (Vertex)) + (count - 1), 1);
    1.41                      }
    1.42                  } else {
    1.43                      IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count - 1, verts, sizeof (Vertex));
    1.44 @@ -1347,9 +1347,8 @@
    1.45                  SetDrawState(data, cmd);
    1.46                  if (vbo) {
    1.47                      size_t offset = 0;
    1.48 -                    IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, (UINT) first, sizeof (Vertex));
    1.49                      for (i = 0; i < count; ++i, offset += 4) {
    1.50 -                        IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLEFAN, offset, 2);
    1.51 +                        IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLEFAN, (first / sizeof (Vertex)) + offset, 2);
    1.52                      }
    1.53                  } else {
    1.54                      const Vertex *verts = (Vertex *) (((Uint8 *) vertices) + first);
    1.55 @@ -1366,9 +1365,8 @@
    1.56                  SetDrawState(data, cmd);
    1.57                  if (vbo) {
    1.58                      size_t offset = 0;
    1.59 -                    IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, (UINT) first, sizeof (Vertex));
    1.60                      for (i = 0; i < count; ++i, offset += 4) {
    1.61 -                        IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLEFAN, offset, 2);
    1.62 +                        IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLEFAN, (first / sizeof (Vertex)) + offset, 2);
    1.63                      }
    1.64                  } else {
    1.65                      const Vertex *verts = (Vertex *) (((Uint8 *) vertices) + first);
    1.66 @@ -1392,8 +1390,7 @@
    1.67                  IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)&d3dmatrix);
    1.68  
    1.69                  if (vbo) {
    1.70 -                    IDirect3DDevice9_SetStreamSource(data->device, 0, vbo, (UINT) first, sizeof (Vertex));
    1.71 -                    IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLEFAN, 0, 2);
    1.72 +                    IDirect3DDevice9_DrawPrimitive(data->device, D3DPT_TRIANGLEFAN, first / sizeof (Vertex), 2);
    1.73                  } else {
    1.74                      IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2, verts, sizeof (Vertex));
    1.75                  }