render: D3D11 now cycles through 8 vertex buffers. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Wed, 03 Oct 2018 19:05:20 -0400
branchSDL-ryan-batching-renderer
changeset 12287dff7e6ba9137
parent 12286 75abb8e13a91
child 12288 005e1d1bcf05
render: D3D11 now cycles through 8 vertex buffers.

This means it doesn't have to block while the current frame finishes using the
vertex buffer; it just moves on to the next, probably-not-in-use buffer.
src/render/direct3d11/SDL_render_d3d11.c
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.c	Wed Oct 03 18:23:53 2018 -0400
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.c	Wed Oct 03 19:05:20 2018 -0400
     1.3 @@ -123,7 +123,8 @@
     1.4      ID3D11RenderTargetView *mainRenderTargetView;
     1.5      ID3D11RenderTargetView *currentOffscreenRenderTargetView;
     1.6      ID3D11InputLayout *inputLayout;
     1.7 -    ID3D11Buffer *vertexBuffer;
     1.8 +    ID3D11Buffer *vertexBuffers[8];
     1.9 +    size_t vertexBufferSizes[8];
    1.10      ID3D11VertexShader *vertexShader;
    1.11      ID3D11PixelShader *pixelShaders[NUM_SHADERS];
    1.12      int blendModesCount;
    1.13 @@ -155,6 +156,7 @@
    1.14      int currentViewportRotation;
    1.15      SDL_bool viewportDirty;
    1.16      Float4X4 identity;
    1.17 +    int currentVertexBuffer;
    1.18  } D3D11_RenderData;
    1.19  
    1.20  
    1.21 @@ -242,7 +244,9 @@
    1.22          SAFE_RELEASE(data->mainRenderTargetView);
    1.23          SAFE_RELEASE(data->currentOffscreenRenderTargetView);
    1.24          SAFE_RELEASE(data->inputLayout);
    1.25 -        SAFE_RELEASE(data->vertexBuffer);
    1.26 +        for (i = 0; i < SDL_arraysize(data->vertexBuffers); ++i) {
    1.27 +            SAFE_RELEASE(data->vertexBuffers[i]);
    1.28 +        }
    1.29          SAFE_RELEASE(data->vertexShader);
    1.30          for (i = 0; i < SDL_arraysize(data->pixelShaders); ++i) {
    1.31              SAFE_RELEASE(data->pixelShaders[i]);
    1.32 @@ -1798,28 +1802,18 @@
    1.33  }
    1.34  
    1.35  
    1.36 -/* !!! FIXME: rotate through a few vertex buffers so the GPU has time to finish using them */
    1.37  static int
    1.38  D3D11_UpdateVertexBuffer(SDL_Renderer *renderer,
    1.39                           const void * vertexData, size_t dataSizeInBytes)
    1.40  {
    1.41      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
    1.42 -    D3D11_BUFFER_DESC vertexBufferDesc;
    1.43      HRESULT result = S_OK;
    1.44 -    D3D11_SUBRESOURCE_DATA vertexBufferData;
    1.45 -    const UINT stride = sizeof(VertexPositionColor);
    1.46 -    const UINT offset = 0;
    1.47 +    const int vbidx = rendererData->currentVertexBuffer;
    1.48  
    1.49 -    if (rendererData->vertexBuffer) {
    1.50 -        ID3D11Buffer_GetDesc(rendererData->vertexBuffer, &vertexBufferDesc);
    1.51 -    } else {
    1.52 -        SDL_zero(vertexBufferDesc);
    1.53 -    }
    1.54 -
    1.55 -    if (rendererData->vertexBuffer && vertexBufferDesc.ByteWidth >= dataSizeInBytes) {
    1.56 +    if (rendererData->vertexBuffers[vbidx] && rendererData->vertexBufferSizes[vbidx] >= dataSizeInBytes) {
    1.57          D3D11_MAPPED_SUBRESOURCE mappedResource;
    1.58          result = ID3D11DeviceContext_Map(rendererData->d3dContext,
    1.59 -            (ID3D11Resource *)rendererData->vertexBuffer,
    1.60 +            (ID3D11Resource *)rendererData->vertexBuffers[vbidx],
    1.61              0,
    1.62              D3D11_MAP_WRITE_DISCARD,
    1.63              0,
    1.64 @@ -1830,10 +1824,16 @@
    1.65              return -1;
    1.66          }
    1.67          SDL_memcpy(mappedResource.pData, vertexData, dataSizeInBytes);
    1.68 -        ID3D11DeviceContext_Unmap(rendererData->d3dContext, (ID3D11Resource *)rendererData->vertexBuffer, 0);
    1.69 +        ID3D11DeviceContext_Unmap(rendererData->d3dContext, (ID3D11Resource *)rendererData->vertexBuffers[vbidx], 0);
    1.70      } else {
    1.71 -        SAFE_RELEASE(rendererData->vertexBuffer);
    1.72 +        D3D11_BUFFER_DESC vertexBufferDesc;
    1.73 +        D3D11_SUBRESOURCE_DATA vertexBufferData;
    1.74 +        const UINT stride = sizeof(VertexPositionColor);
    1.75 +        const UINT offset = 0;
    1.76  
    1.77 +        SAFE_RELEASE(rendererData->vertexBuffers[vbidx]);
    1.78 +
    1.79 +        SDL_zero(vertexBufferDesc);
    1.80          vertexBufferDesc.ByteWidth = (UINT) dataSizeInBytes;
    1.81          vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
    1.82          vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    1.83 @@ -1847,7 +1847,7 @@
    1.84          result = ID3D11Device_CreateBuffer(rendererData->d3dDevice,
    1.85              &vertexBufferDesc,
    1.86              &vertexBufferData,
    1.87 -            &rendererData->vertexBuffer
    1.88 +            &rendererData->vertexBuffers[vbidx]
    1.89              );
    1.90          if (FAILED(result)) {
    1.91              WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateBuffer [vertex buffer]"), result);
    1.92 @@ -1857,12 +1857,17 @@
    1.93          ID3D11DeviceContext_IASetVertexBuffers(rendererData->d3dContext,
    1.94              0,
    1.95              1,
    1.96 -            &rendererData->vertexBuffer,
    1.97 +            &rendererData->vertexBuffers[vbidx],
    1.98              &stride,
    1.99              &offset
   1.100              );
   1.101      }
   1.102  
   1.103 +    rendererData->currentVertexBuffer++;
   1.104 +    if (rendererData->currentVertexBuffer >= SDL_arraysize(rendererData->vertexBuffers)) {
   1.105 +        rendererData->currentVertexBuffer = 0;
   1.106 +    }
   1.107 +
   1.108      return 0;
   1.109  }
   1.110