WinRT: made sure the device orientation transform gets applied (by the D3D 11.1 renderer) when drawing
authorDavid Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 20:26:39 -0500
changeset 84185230bba62cf2
parent 8417 43f837ba544b
child 8419 480504efcbc3
WinRT: made sure the device orientation transform gets applied (by the D3D 11.1 renderer) when drawing
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	Sat Feb 09 19:39:42 2013 -0500
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 09 20:26:39 2013 -0500
     1.3 @@ -340,6 +340,20 @@
     1.4      }
     1.5  
     1.6      //
     1.7 +    // Setup space to hold vertex shader constants:
     1.8 +    //
     1.9 +    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(SDL_VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
    1.10 +    result = data->d3dDevice->CreateBuffer(
    1.11 +		&constantBufferDesc,
    1.12 +		nullptr,
    1.13 +        &data->vertexShaderConstants
    1.14 +		);
    1.15 +    if (FAILED(result)) {
    1.16 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
    1.17 +        return result;
    1.18 +    }
    1.19 +
    1.20 +    //
    1.21      // Create a vertex buffer:
    1.22      //
    1.23      VertexPositionColor vertices[] = 
    1.24 @@ -553,7 +567,7 @@
    1.25      {
    1.26          case DisplayOrientations::Landscape:
    1.27              rotation = DXGI_MODE_ROTATION_IDENTITY;
    1.28 -            data->orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
    1.29 +            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 0-degree Z-rotation
    1.30                  1.0f, 0.0f, 0.0f, 0.0f,
    1.31                  0.0f, 1.0f, 0.0f, 0.0f,
    1.32                  0.0f, 0.0f, 1.0f, 0.0f,
    1.33 @@ -563,9 +577,9 @@
    1.34  
    1.35          case DisplayOrientations::Portrait:
    1.36              rotation = DXGI_MODE_ROTATION_ROTATE270;
    1.37 -            data->orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
    1.38 -                0.0f, 1.0f, 0.0f, 0.0f,
    1.39 -                -1.0f, 0.0f, 0.0f, 0.0f,
    1.40 +            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 270-degree Z-rotation
    1.41 +                0.0f, -1.0f, 0.0f, 0.0f,
    1.42 +                1.0f, 0.0f, 0.0f, 0.0f,
    1.43                  0.0f, 0.0f, 1.0f, 0.0f,
    1.44                  0.0f, 0.0f, 0.0f, 1.0f
    1.45                  );
    1.46 @@ -573,7 +587,7 @@
    1.47  
    1.48          case DisplayOrientations::LandscapeFlipped:
    1.49              rotation = DXGI_MODE_ROTATION_ROTATE180;
    1.50 -            data->orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
    1.51 +            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 180-degree Z-rotation
    1.52                  -1.0f, 0.0f, 0.0f, 0.0f,
    1.53                  0.0f, -1.0f, 0.0f, 0.0f,
    1.54                  0.0f, 0.0f, 1.0f, 0.0f,
    1.55 @@ -583,9 +597,9 @@
    1.56  
    1.57          case DisplayOrientations::PortraitFlipped:
    1.58              rotation = DXGI_MODE_ROTATION_ROTATE90;
    1.59 -            data->orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
    1.60 -                0.0f, -1.0f, 0.0f, 0.0f,
    1.61 -                1.0f, 0.0f, 0.0f, 0.0f,
    1.62 +            data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 90-degree Z-rotation
    1.63 +                0.0f, 1.0f, 0.0f, 0.0f,
    1.64 +                -1.0f, 0.0f, 0.0f, 0.0f,
    1.65                  0.0f, 0.0f, 1.0f, 0.0f,
    1.66                  0.0f, 0.0f, 0.0f, 1.0f
    1.67                  );
    1.68 @@ -891,6 +905,15 @@
    1.69          nullptr
    1.70          );
    1.71  
    1.72 +    rendererData->d3dContext->UpdateSubresource(
    1.73 +        rendererData->vertexShaderConstants.Get(),
    1.74 +		0,
    1.75 +		NULL,
    1.76 +		&rendererData->vertexShaderConstantsData,
    1.77 +		0,
    1.78 +		0
    1.79 +		);
    1.80 +
    1.81      UINT stride = sizeof(VertexPositionColor);
    1.82      UINT offset = 0;
    1.83      rendererData->d3dContext->IASetVertexBuffers(
    1.84 @@ -911,6 +934,12 @@
    1.85          0
    1.86          );
    1.87  
    1.88 +    rendererData->d3dContext->VSSetConstantBuffers(
    1.89 +		0,
    1.90 +		1,
    1.91 +        rendererData->vertexShaderConstants.GetAddressOf()
    1.92 +		);
    1.93 +
    1.94      rendererData->d3dContext->PSSetShader(
    1.95          rendererData->pixelShader.Get(),
    1.96          nullptr,
     2.1 --- a/src/render/direct3d11/SDL_render_d3d11_cpp.h	Sat Feb 09 19:39:42 2013 -0500
     2.2 +++ b/src/render/direct3d11/SDL_render_d3d11_cpp.h	Sat Feb 09 20:26:39 2013 -0500
     2.3 @@ -24,6 +24,11 @@
     2.4  #include <DirectXMath.h>
     2.5  #include <wrl/client.h>
     2.6  
     2.7 +struct SDL_VertexShaderConstants
     2.8 +{
     2.9 +	DirectX::XMFLOAT4X4 projection;
    2.10 +};
    2.11 +
    2.12  typedef struct
    2.13  {
    2.14      Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice;
    2.15 @@ -39,6 +44,10 @@
    2.16      unsigned int vertexCount;
    2.17      bool loadingComplete;
    2.18  
    2.19 +    // Vertex buffer constants:
    2.20 +    SDL_VertexShaderConstants vertexShaderConstantsData;
    2.21 +    Microsoft::WRL::ComPtr<ID3D11Buffer> vertexShaderConstants;
    2.22 +
    2.23      // Cached renderer properties.
    2.24      DirectX::XMFLOAT2 windowSizeInDIPs;
    2.25      DirectX::XMFLOAT2 renderTargetSize;
     3.1 --- a/src/video/windowsrt/SimpleVertexShader.hlsl	Sat Feb 09 19:39:42 2013 -0500
     3.2 +++ b/src/video/windowsrt/SimpleVertexShader.hlsl	Sat Feb 09 20:26:39 2013 -0500
     3.3 @@ -1,6 +1,11 @@
     3.4  
     3.5  //#pragma pack_matrix( row_major )
     3.6  
     3.7 +cbuffer SDL_VertexShaderConstants : register(b0)
     3.8 +{
     3.9 +	matrix projection;
    3.10 +};
    3.11 +
    3.12  struct VertexShaderInput
    3.13  {
    3.14      float3 pos : POSITION;
    3.15 @@ -16,7 +21,14 @@
    3.16  VertexShaderOutput main(VertexShaderInput input)
    3.17  {
    3.18      VertexShaderOutput output;
    3.19 -    output.pos = float4(input.pos, 1.0f);
    3.20 +    float4 pos = float4(input.pos, 1.0f);
    3.21 +
    3.22 +    // Transform the vertex position into projected space.
    3.23 +    pos = mul(pos, projection);
    3.24 +    output.pos = pos;
    3.25 +
    3.26 +    // Pass through the texture's color without modification.
    3.27      output.tex = input.tex;
    3.28 +
    3.29      return output;
    3.30  }