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

Commit

Permalink
WinRT: added rotation support to SDL_RenderCopyEx via D3D 11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLudwig committed Apr 2, 2013
1 parent 726c889 commit 05ccd27
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/render/direct3d11/SDL_D3D11_VertexShader_Default.hlsl
Expand Up @@ -3,8 +3,9 @@

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

struct VertexShaderInput
Expand All @@ -27,6 +28,7 @@ VertexShaderOutput main(VertexShaderInput input)
float4 pos = float4(input.pos, 1.0f);

// Transform the vertex position into projected space.
pos = mul(pos, model);
pos = mul(pos, view);
pos = mul(pos, projection);
output.pos = pos;
Expand Down
47 changes: 34 additions & 13 deletions src/render/direct3d11/SDL_render_d3d11.cpp
Expand Up @@ -1129,6 +1129,11 @@ D3D11_UpdateViewport(SDL_Renderer * renderer)
data->vertexShaderConstantsData.view = XMMatrixIdentity();
#endif

//
// Reset the model matrix
//
XMStoreFloat4x4(&data->vertexShaderConstantsData.model, XMMatrixIdentity());

//
// Update the Direct3D viewport, which seems to be aligned to the
// swap buffer's coordinate space, which is always in landscape:
Expand Down Expand Up @@ -1250,15 +1255,6 @@ D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
rendererData->renderTargetView.GetAddressOf(),
nullptr
);

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

static void
Expand Down Expand Up @@ -1299,6 +1295,16 @@ D3D11_RenderFinishDrawOp(SDL_Renderer * renderer,
UINT vertexCount)
{
D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;

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

rendererData->d3dContext->IASetPrimitiveTopology(primitiveTopology);
rendererData->d3dContext->IASetInputLayout(rendererData->inputLayout.Get());
rendererData->d3dContext->VSSetShader(rendererData->vertexShader.Get(), nullptr, 0);
Expand Down Expand Up @@ -1504,12 +1510,25 @@ D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
maxv = minv;
minv = tmp;
}

XMFLOAT4X4 oldModelMatrix = rendererData->vertexShaderConstantsData.model;
XMStoreFloat4x4(
&rendererData->vertexShaderConstantsData.model,
XMMatrixMultiply(
XMMatrixRotationZ((float)(XM_PI * (float) angle / 180.0f)),
XMMatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
));

const float minx = -center->x;
const float maxx = dstrect->w - center->x;
const float miny = -center->y;
const float maxy = dstrect->h - center->y;

VertexPositionColor vertices[] = {
{XMFLOAT3(dstrect->x, dstrect->y, 0.0f), XMFLOAT2(minu, minv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(dstrect->x, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(minu, maxv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(dstrect->x + dstrect->w, dstrect->y, 0.0f), XMFLOAT2(maxu, minv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(minx, miny, 0.0f), XMFLOAT2(minu, minv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(minx, maxy, 0.0f), XMFLOAT2(minu, maxv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(maxx, miny, 0.0f), XMFLOAT2(maxu, minv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
{XMFLOAT3(maxx, maxy, 0.0f), XMFLOAT2(maxu, maxv), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)},
};
if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
return -1;
Expand All @@ -1522,6 +1541,8 @@ D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
rendererData->mainSampler.Get());

D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));

rendererData->vertexShaderConstantsData.model = oldModelMatrix;

return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/render/direct3d11/SDL_render_d3d11_cpp.h
Expand Up @@ -27,8 +27,9 @@

struct SDL_VertexShaderConstants
{
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
DirectX::XMFLOAT4X4 model;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
};

typedef struct
Expand Down

0 comments on commit 05ccd27

Please sign in to comment.