WinRT: fixed bug: SDL_RenderCopy was always filling the entire screen
authorDavid Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 20:49:26 -0500
changeset 84269775bb101357
parent 8425 99448fee696f
child 8427 61794d3b1d89
WinRT: fixed bug: SDL_RenderCopy was always filling the entire screen
src/render/direct3d11/SDL_render_d3d11.cpp
src/render/direct3d11/SDL_render_d3d11_cpp.h
src/video/windowsrt/SimpleVertexShader.hlsl
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.cpp	Tue Feb 12 19:08:35 2013 -0500
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Tue Feb 12 20:49:26 2013 -0500
     1.3 @@ -385,6 +385,27 @@
     1.4      }
     1.5  
     1.6      //
     1.7 +    // Setup the Direct3D rasterizer
     1.8 +    //
     1.9 +    D3D11_RASTERIZER_DESC rasterDesc;
    1.10 +    memset(&rasterDesc, 0, sizeof(rasterDesc));
    1.11 +	rasterDesc.AntialiasedLineEnable = false;
    1.12 +	rasterDesc.CullMode = D3D11_CULL_NONE;
    1.13 +	rasterDesc.DepthBias = 0;
    1.14 +	rasterDesc.DepthBiasClamp = 0.0f;
    1.15 +	rasterDesc.DepthClipEnable = true;
    1.16 +	rasterDesc.FillMode = D3D11_FILL_SOLID;
    1.17 +	rasterDesc.FrontCounterClockwise = false;
    1.18 +	rasterDesc.MultisampleEnable = false;
    1.19 +	rasterDesc.ScissorEnable = false;
    1.20 +	rasterDesc.SlopeScaledDepthBias = 0.0f;
    1.21 +	result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->mainRasterizer);
    1.22 +	if (FAILED(result)) {
    1.23 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
    1.24 +        return result;
    1.25 +    }
    1.26 +
    1.27 +    //
    1.28      // All done!
    1.29      //
    1.30      data->loadingComplete = true;       // This variable can probably be factored-out
    1.31 @@ -595,6 +616,17 @@
    1.32      }
    1.33  #endif
    1.34  
    1.35 +    //
    1.36 +    // Update the view matrix
    1.37 +    //
    1.38 +    XMStoreFloat4x4(&data->vertexShaderConstantsData.view,  // (4)
    1.39 +        XMMatrixMultiply(
    1.40 +            XMMatrixScaling(2.0f / windowWidth, 2.0f / windowHeight, 1.0f),
    1.41 +            XMMatrixMultiply(
    1.42 +                XMMatrixTranslation(-1, -1, 0),
    1.43 +                XMMatrixRotationX(XM_PI)
    1.44 +                )));
    1.45 +
    1.46      // Create a render target view of the swap chain back buffer.
    1.47      ComPtr<ID3D11Texture2D> backBuffer;
    1.48      result = data->swapChain->GetBuffer(
    1.49 @@ -893,12 +925,20 @@
    1.50      //
    1.51      // Create or update the vertex buffer:
    1.52      //
    1.53 -    VertexPositionColor vertices[] = 
    1.54 +
    1.55 +    // WinRT, TODO: get srcrect working in tandem with SDL_RenderCopy, etc.
    1.56 +    //SDL_FRect fSrcRect;
    1.57 +    //fSrcRect.x = (float)srcrect->x;
    1.58 +    //fSrcRect.y = (float)srcrect->y;
    1.59 +    //fSrcRect.w = (float)srcrect->w;
    1.60 +    //fSrcRect.h = (float)srcrect->h;
    1.61 +
    1.62 +    VertexPositionColor vertices[] =
    1.63      {
    1.64 -        {XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
    1.65 -        {XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
    1.66 -        {XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
    1.67 -        {XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
    1.68 +        {XMFLOAT3(dstrect->x, dstrect->y, 0.0f),                           XMFLOAT2(0.0f, 0.0f)},
    1.69 +        {XMFLOAT3(dstrect->x, dstrect->y + dstrect->h, 0.0f),              XMFLOAT2(0.0f, 1.0f)},
    1.70 +        {XMFLOAT3(dstrect->x + dstrect->h, dstrect->y, 0.0f),              XMFLOAT2(1.0f, 0.0f)},
    1.71 +        {XMFLOAT3(dstrect->x + dstrect->h, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(1.0f, 1.0f)},
    1.72      };
    1.73  
    1.74      if (rendererData->vertexBuffer) {
    1.75 @@ -956,6 +996,8 @@
    1.76  
    1.77      rendererData->d3dContext->PSSetSamplers(0, 1, rendererData->mainSampler.GetAddressOf());
    1.78  
    1.79 +    rendererData->d3dContext->RSSetState(rendererData->mainRasterizer.Get());
    1.80 +
    1.81      rendererData->d3dContext->Draw(4, 0);
    1.82  
    1.83      return 0;
     2.1 --- a/src/render/direct3d11/SDL_render_d3d11_cpp.h	Tue Feb 12 19:08:35 2013 -0500
     2.2 +++ b/src/render/direct3d11/SDL_render_d3d11_cpp.h	Tue Feb 12 20:49:26 2013 -0500
     2.3 @@ -26,6 +26,7 @@
     2.4  
     2.5  struct SDL_VertexShaderConstants
     2.6  {
     2.7 +	DirectX::XMFLOAT4X4 view;
     2.8  	DirectX::XMFLOAT4X4 projection;
     2.9  };
    2.10  
    2.11 @@ -40,6 +41,7 @@
    2.12      Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
    2.13      Microsoft::WRL::ComPtr<ID3D11PixelShader> pixelShader;
    2.14      Microsoft::WRL::ComPtr<ID3D11SamplerState> mainSampler;
    2.15 +    Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer;
    2.16      D3D_FEATURE_LEVEL featureLevel;
    2.17      bool loadingComplete;
    2.18  
     3.1 --- a/src/video/windowsrt/SimpleVertexShader.hlsl	Tue Feb 12 19:08:35 2013 -0500
     3.2 +++ b/src/video/windowsrt/SimpleVertexShader.hlsl	Tue Feb 12 20:49:26 2013 -0500
     3.3 @@ -1,8 +1,9 @@
     3.4  
     3.5 -//#pragma pack_matrix( row_major )
     3.6 +#pragma pack_matrix( row_major )
     3.7  
     3.8  cbuffer SDL_VertexShaderConstants : register(b0)
     3.9  {
    3.10 +	matrix view;
    3.11  	matrix projection;
    3.12  };
    3.13  
    3.14 @@ -24,6 +25,7 @@
    3.15      float4 pos = float4(input.pos, 1.0f);
    3.16  
    3.17      // Transform the vertex position into projected space.
    3.18 +    pos = mul(pos, view);
    3.19      pos = mul(pos, projection);
    3.20      output.pos = pos;
    3.21