WinRT: added rotation support to SDL_RenderCopyEx via D3D 11.1
authorDavid Ludwig <dludwig@pobox.com>
Tue, 02 Apr 2013 00:09:49 -0400
changeset 8456 f36fc6624569
parent 8455 14d4b48cc07f
child 8457 e0a74ebf9aba
WinRT: added rotation support to SDL_RenderCopyEx via D3D 11.1
src/render/direct3d11/SDL_D3D11_VertexShader_Default.hlsl
src/render/direct3d11/SDL_render_d3d11.cpp
src/render/direct3d11/SDL_render_d3d11_cpp.h
--- a/src/render/direct3d11/SDL_D3D11_VertexShader_Default.hlsl	Mon Apr 01 22:33:37 2013 -0400
+++ b/src/render/direct3d11/SDL_D3D11_VertexShader_Default.hlsl	Tue Apr 02 00:09:49 2013 -0400
@@ -3,8 +3,9 @@
 
 cbuffer SDL_VertexShaderConstants : register(b0)
 {
-	matrix view;
-	matrix projection;
+    matrix model;
+    matrix view;
+    matrix projection;
 };
 
 struct VertexShaderInput
@@ -27,6 +28,7 @@
     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;
--- a/src/render/direct3d11/SDL_render_d3d11.cpp	Mon Apr 01 22:33:37 2013 -0400
+++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Tue Apr 02 00:09:49 2013 -0400
@@ -1130,6 +1130,11 @@
 #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:
     //
@@ -1250,15 +1255,6 @@
         rendererData->renderTargetView.GetAddressOf(),
         nullptr
         );
-
-    rendererData->d3dContext->UpdateSubresource(
-        rendererData->vertexShaderConstants.Get(),
-		0,
-		NULL,
-		&rendererData->vertexShaderConstantsData,
-		0,
-		0
-		);
 }
 
 static void
@@ -1299,6 +1295,16 @@
                          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);
@@ -1504,12 +1510,25 @@
         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;
@@ -1522,6 +1541,8 @@
         rendererData->mainSampler.Get());
 
     D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
+
+    rendererData->vertexShaderConstantsData.model = oldModelMatrix;
 
     return 0;
 }
--- a/src/render/direct3d11/SDL_render_d3d11_cpp.h	Mon Apr 01 22:33:37 2013 -0400
+++ b/src/render/direct3d11/SDL_render_d3d11_cpp.h	Tue Apr 02 00:09:49 2013 -0400
@@ -27,8 +27,9 @@
 
 struct SDL_VertexShaderConstants
 {
-	DirectX::XMFLOAT4X4 view;
-	DirectX::XMFLOAT4X4 projection;
+    DirectX::XMFLOAT4X4 model;
+    DirectX::XMFLOAT4X4 view;
+    DirectX::XMFLOAT4X4 projection;
 };
 
 typedef struct