Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
WinRT: made sure the device orientation transform gets applied (by th…
Browse files Browse the repository at this point in the history
…e D3D 11.1 renderer) when drawing
  • Loading branch information
DavidLudwig committed Feb 10, 2013
1 parent 73440f6 commit 5c926ee
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
45 changes: 37 additions & 8 deletions src/render/direct3d11/SDL_render_d3d11.cpp
Expand Up @@ -339,6 +339,20 @@ D3D11_CreateDeviceResources(SDL_Renderer * renderer)
return result;
}

//
// Setup space to hold vertex shader constants:
//
CD3D11_BUFFER_DESC constantBufferDesc(sizeof(SDL_VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
result = data->d3dDevice->CreateBuffer(
&constantBufferDesc,
nullptr,
&data->vertexShaderConstants
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}

//
// Create a vertex buffer:
//
Expand Down Expand Up @@ -553,7 +567,7 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
{
case DisplayOrientations::Landscape:
rotation = DXGI_MODE_ROTATION_IDENTITY;
data->orientationTransform3D = XMFLOAT4X4( // 0-degree Z-rotation
data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 0-degree Z-rotation
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
Expand All @@ -563,17 +577,17 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)

case DisplayOrientations::Portrait:
rotation = DXGI_MODE_ROTATION_ROTATE270;
data->orientationTransform3D = XMFLOAT4X4( // 90-degree Z-rotation
0.0f, 1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f, 0.0f,
data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 270-degree Z-rotation
0.0f, -1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
break;

case DisplayOrientations::LandscapeFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE180;
data->orientationTransform3D = XMFLOAT4X4( // 180-degree Z-rotation
data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 180-degree Z-rotation
-1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
Expand All @@ -583,9 +597,9 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)

case DisplayOrientations::PortraitFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE90;
data->orientationTransform3D = XMFLOAT4X4( // 270-degree Z-rotation
0.0f, -1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 0.0f,
data->vertexShaderConstantsData.projection = XMFLOAT4X4( // 90-degree Z-rotation
0.0f, 1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
Expand Down Expand Up @@ -891,6 +905,15 @@ static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
nullptr
);

rendererData->d3dContext->UpdateSubresource(
rendererData->vertexShaderConstants.Get(),
0,
NULL,
&rendererData->vertexShaderConstantsData,
0,
0
);

UINT stride = sizeof(VertexPositionColor);
UINT offset = 0;
rendererData->d3dContext->IASetVertexBuffers(
Expand All @@ -911,6 +934,12 @@ static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
0
);

rendererData->d3dContext->VSSetConstantBuffers(
0,
1,
rendererData->vertexShaderConstants.GetAddressOf()
);

rendererData->d3dContext->PSSetShader(
rendererData->pixelShader.Get(),
nullptr,
Expand Down
9 changes: 9 additions & 0 deletions src/render/direct3d11/SDL_render_d3d11_cpp.h
Expand Up @@ -24,6 +24,11 @@
#include <DirectXMath.h>
#include <wrl/client.h>

struct SDL_VertexShaderConstants
{
DirectX::XMFLOAT4X4 projection;
};

typedef struct
{
Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice;
Expand All @@ -39,6 +44,10 @@ typedef struct
unsigned int vertexCount;
bool loadingComplete;

// Vertex buffer constants:
SDL_VertexShaderConstants vertexShaderConstantsData;
Microsoft::WRL::ComPtr<ID3D11Buffer> vertexShaderConstants;

// Cached renderer properties.
DirectX::XMFLOAT2 windowSizeInDIPs;
DirectX::XMFLOAT2 renderTargetSize;
Expand Down
14 changes: 13 additions & 1 deletion src/video/windowsrt/SimpleVertexShader.hlsl
@@ -1,6 +1,11 @@

//#pragma pack_matrix( row_major )

cbuffer SDL_VertexShaderConstants : register(b0)
{
matrix projection;
};

struct VertexShaderInput
{
float3 pos : POSITION;
Expand All @@ -16,7 +21,14 @@ struct VertexShaderOutput
VertexShaderOutput main(VertexShaderInput input)
{
VertexShaderOutput output;
output.pos = float4(input.pos, 1.0f);
float4 pos = float4(input.pos, 1.0f);

// Transform the vertex position into projected space.
pos = mul(pos, projection);
output.pos = pos;

// Pass through the texture's color without modification.
output.tex = input.tex;

return output;
}

0 comments on commit 5c926ee

Please sign in to comment.