WinRT: started work on renderer by getting Direct3D 11.1 to display a single, fullscreen rectangle
authorDavid Ludwig <dludwig@pobox.com>
Sun, 18 Nov 2012 23:29:52 -0500
changeset 834205d7aa70928e
parent 8341 99c7c87774d5
child 8343 f593baeea84c
WinRT: started work on renderer by getting Direct3D 11.1 to display a single, fullscreen rectangle
VisualC/SDL/SDL_VS2012_WinRT.vcxproj
src/video/windowsrt/CubeRenderer.cpp
src/video/windowsrt/CubeRenderer.h
src/video/windowsrt/SDL_WinRTApp.cpp
src/video/windowsrt/SDL_WinRTApp.h
src/video/windowsrt/SDL_winrtframebuffer.cpp
src/video/windowsrt/SDL_winrtrenderer.cpp
src/video/windowsrt/SDL_winrtrenderer.h
     1.1 --- a/VisualC/SDL/SDL_VS2012_WinRT.vcxproj	Sun Nov 04 13:26:53 2012 -0500
     1.2 +++ b/VisualC/SDL/SDL_VS2012_WinRT.vcxproj	Sun Nov 18 23:29:52 2012 -0500
     1.3 @@ -101,14 +101,6 @@
     1.4      <ClCompile Include="..\..\src\video\SDL_stretch.c" />
     1.5      <ClCompile Include="..\..\src\video\SDL_surface.c" />
     1.6      <ClCompile Include="..\..\src\video\SDL_video.c" />
     1.7 -    <ClCompile Include="..\..\src\video\windowsrt\CubeRenderer.cpp">
     1.8 -      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
     1.9 -      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    1.10 -      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
    1.11 -      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
    1.12 -      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
    1.13 -      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
    1.14 -    </ClCompile>
    1.15      <ClCompile Include="..\..\src\video\windowsrt\Direct3DBase.cpp">
    1.16        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    1.17        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    1.18 @@ -133,7 +125,22 @@
    1.19        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
    1.20        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
    1.21      </ClCompile>
    1.22 -    <ClCompile Include="..\..\src\video\windowsrt\SDL_winrtframebuffer.cpp" />
    1.23 +    <ClCompile Include="..\..\src\video\windowsrt\SDL_winrtframebuffer.cpp">
    1.24 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
    1.25 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
    1.26 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    1.27 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    1.28 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
    1.29 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
    1.30 +    </ClCompile>
    1.31 +    <ClCompile Include="..\..\src\video\windowsrt\SDL_winrtrenderer.cpp">
    1.32 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
    1.33 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
    1.34 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    1.35 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    1.36 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
    1.37 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
    1.38 +    </ClCompile>
    1.39      <ClCompile Include="..\..\src\video\windowsrt\SDL_winrtvideo.cpp">
    1.40        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    1.41        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    1.42 @@ -245,13 +252,13 @@
    1.43      <ClInclude Include="..\..\src\video\SDL_RLEaccel_c.h" />
    1.44      <ClInclude Include="..\..\src\video\SDL_shape_internals.h" />
    1.45      <ClInclude Include="..\..\src\video\SDL_sysvideo.h" />
    1.46 -    <ClInclude Include="..\..\src\video\windowsrt\CubeRenderer.h" />
    1.47      <ClInclude Include="..\..\src\video\windowsrt\Direct3DBase.h" />
    1.48      <ClInclude Include="..\..\src\video\windowsrt\DirectXHelper.h" />
    1.49      <ClInclude Include="..\..\src\video\windowsrt\SDLmain_WinRT_common.h" />
    1.50      <ClInclude Include="..\..\src\video\windowsrt\SDL_WinRTApp.h" />
    1.51      <ClInclude Include="..\..\src\video\windowsrt\SDL_winrtevents_c.h" />
    1.52      <ClInclude Include="..\..\src\video\windowsrt\SDL_winrtframebuffer_c.h" />
    1.53 +    <ClInclude Include="..\..\src\video\windowsrt\SDL_winrtrenderer.h" />
    1.54      <ClInclude Include="..\..\src\video\windowsrt\SDL_winrtvideo.h" />
    1.55    </ItemGroup>
    1.56    <PropertyGroup Label="Globals">
     2.1 --- a/src/video/windowsrt/CubeRenderer.cpp	Sun Nov 04 13:26:53 2012 -0500
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,256 +0,0 @@
     2.4 -#include "SDLmain_WinRT_common.h"
     2.5 -#include "CubeRenderer.h"
     2.6 -
     2.7 -using namespace DirectX;
     2.8 -using namespace Microsoft::WRL;
     2.9 -using namespace Windows::Foundation;
    2.10 -using namespace Windows::UI::Core;
    2.11 -
    2.12 -CubeRenderer::CubeRenderer() :
    2.13 -	m_loadingComplete(false),
    2.14 -	m_indexCount(0)
    2.15 -{
    2.16 -}
    2.17 -
    2.18 -void CubeRenderer::CreateDeviceResources()
    2.19 -{
    2.20 -	Direct3DBase::CreateDeviceResources();
    2.21 -
    2.22 -	auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
    2.23 -	auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
    2.24 -
    2.25 -	auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
    2.26 -		DX::ThrowIfFailed(
    2.27 -			m_d3dDevice->CreateVertexShader(
    2.28 - 				fileData->Data,
    2.29 -				fileData->Length,
    2.30 -				nullptr,
    2.31 -				&m_vertexShader
    2.32 -				)
    2.33 -			);
    2.34 -
    2.35 -		const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    2.36 -		{
    2.37 -			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    2.38 -			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    2.39 -		};
    2.40 -
    2.41 -		DX::ThrowIfFailed(
    2.42 -			m_d3dDevice->CreateInputLayout(
    2.43 -				vertexDesc,
    2.44 -				ARRAYSIZE(vertexDesc),
    2.45 -				fileData->Data,
    2.46 -				fileData->Length,
    2.47 -				&m_inputLayout
    2.48 -				)
    2.49 -			);
    2.50 -	});
    2.51 -
    2.52 -	auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
    2.53 -		DX::ThrowIfFailed(
    2.54 -			m_d3dDevice->CreatePixelShader(
    2.55 -				fileData->Data,
    2.56 -				fileData->Length,
    2.57 -				nullptr,
    2.58 -				&m_pixelShader
    2.59 -				)
    2.60 -			);
    2.61 -
    2.62 -		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    2.63 -		DX::ThrowIfFailed(
    2.64 -			m_d3dDevice->CreateBuffer(
    2.65 -				&constantBufferDesc,
    2.66 -				nullptr,
    2.67 -				&m_constantBuffer
    2.68 -				)
    2.69 -			);
    2.70 -	});
    2.71 -
    2.72 -	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    2.73 -		VertexPositionColor cubeVertices[] = 
    2.74 -		{
    2.75 -			{XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
    2.76 -			{XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    2.77 -			{XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    2.78 -			{XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
    2.79 -			{XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    2.80 -			{XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
    2.81 -			{XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
    2.82 -			{XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    2.83 -		};
    2.84 -
    2.85 -		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    2.86 -		vertexBufferData.pSysMem = cubeVertices;
    2.87 -		vertexBufferData.SysMemPitch = 0;
    2.88 -		vertexBufferData.SysMemSlicePitch = 0;
    2.89 -		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    2.90 -		DX::ThrowIfFailed(
    2.91 -			m_d3dDevice->CreateBuffer(
    2.92 -				&vertexBufferDesc,
    2.93 -				&vertexBufferData,
    2.94 -				&m_vertexBuffer
    2.95 -				)
    2.96 -			);
    2.97 -
    2.98 -		unsigned short cubeIndices[] = 
    2.99 -		{
   2.100 -			0,2,1, // -x
   2.101 -			1,2,3,
   2.102 -
   2.103 -			4,5,6, // +x
   2.104 -			5,7,6,
   2.105 -
   2.106 -			0,1,5, // -y
   2.107 -			0,5,4,
   2.108 -
   2.109 -			2,6,7, // +y
   2.110 -			2,7,3,
   2.111 -
   2.112 -			0,4,6, // -z
   2.113 -			0,6,2,
   2.114 -
   2.115 -			1,3,7, // +z
   2.116 -			1,7,5,
   2.117 -		};
   2.118 -
   2.119 -		m_indexCount = ARRAYSIZE(cubeIndices);
   2.120 -
   2.121 -		D3D11_SUBRESOURCE_DATA indexBufferData = {0};
   2.122 -		indexBufferData.pSysMem = cubeIndices;
   2.123 -		indexBufferData.SysMemPitch = 0;
   2.124 -		indexBufferData.SysMemSlicePitch = 0;
   2.125 -		CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER);
   2.126 -		DX::ThrowIfFailed(
   2.127 -			m_d3dDevice->CreateBuffer(
   2.128 -				&indexBufferDesc,
   2.129 -				&indexBufferData,
   2.130 -				&m_indexBuffer
   2.131 -				)
   2.132 -			);
   2.133 -	});
   2.134 -
   2.135 -	createCubeTask.then([this] () {
   2.136 -		m_loadingComplete = true;
   2.137 -	});
   2.138 -}
   2.139 -
   2.140 -void CubeRenderer::CreateWindowSizeDependentResources()
   2.141 -{
   2.142 -	Direct3DBase::CreateWindowSizeDependentResources();
   2.143 -
   2.144 -	float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
   2.145 -	float fovAngleY = 70.0f * XM_PI / 180.0f;
   2.146 -
   2.147 -	// Note that the m_orientationTransform3D matrix is post-multiplied here
   2.148 -	// in order to correctly orient the scene to match the display orientation.
   2.149 -	// This post-multiplication step is required for any draw calls that are
   2.150 -	// made to the swap chain render target. For draw calls to other targets,
   2.151 -	// this transform should not be applied.
   2.152 -	XMStoreFloat4x4(
   2.153 -		&m_constantBufferData.projection,
   2.154 -		XMMatrixTranspose(
   2.155 -			XMMatrixMultiply(
   2.156 -				XMMatrixPerspectiveFovRH(
   2.157 -					fovAngleY,
   2.158 -					aspectRatio,
   2.159 -					0.01f,
   2.160 -					100.0f
   2.161 -					),
   2.162 -				XMLoadFloat4x4(&m_orientationTransform3D)
   2.163 -				)
   2.164 -			)
   2.165 -		);
   2.166 -}
   2.167 -
   2.168 -void CubeRenderer::Update(float timeTotal, float timeDelta)
   2.169 -{
   2.170 -	(void) timeDelta; // Unused parameter.
   2.171 -
   2.172 -	XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
   2.173 -	XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
   2.174 -	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
   2.175 -
   2.176 -	XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
   2.177 -	XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
   2.178 -}
   2.179 -
   2.180 -void CubeRenderer::Render()
   2.181 -{
   2.182 -	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
   2.183 -	m_d3dContext->ClearRenderTargetView(
   2.184 -		m_renderTargetView.Get(),
   2.185 -		midnightBlue
   2.186 -		);
   2.187 -
   2.188 -	m_d3dContext->ClearDepthStencilView(
   2.189 -		m_depthStencilView.Get(),
   2.190 -		D3D11_CLEAR_DEPTH,
   2.191 -		1.0f,
   2.192 -		0
   2.193 -		);
   2.194 -
   2.195 -	// Only draw the cube once it is loaded (loading is asynchronous).
   2.196 -	if (!m_loadingComplete)
   2.197 -	{
   2.198 -		return;
   2.199 -	}
   2.200 -
   2.201 -	m_d3dContext->OMSetRenderTargets(
   2.202 -		1,
   2.203 -		m_renderTargetView.GetAddressOf(),
   2.204 -		m_depthStencilView.Get()
   2.205 -		);
   2.206 -
   2.207 -	m_d3dContext->UpdateSubresource(
   2.208 -		m_constantBuffer.Get(),
   2.209 -		0,
   2.210 -		NULL,
   2.211 -		&m_constantBufferData,
   2.212 -		0,
   2.213 -		0
   2.214 -		);
   2.215 -
   2.216 -	UINT stride = sizeof(VertexPositionColor);
   2.217 -	UINT offset = 0;
   2.218 -	m_d3dContext->IASetVertexBuffers(
   2.219 -		0,
   2.220 -		1,
   2.221 -		m_vertexBuffer.GetAddressOf(),
   2.222 -		&stride,
   2.223 -		&offset
   2.224 -		);
   2.225 -
   2.226 -	m_d3dContext->IASetIndexBuffer(
   2.227 -		m_indexBuffer.Get(),
   2.228 -		DXGI_FORMAT_R16_UINT,
   2.229 -		0
   2.230 -		);
   2.231 -
   2.232 -	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
   2.233 -
   2.234 -	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
   2.235 -
   2.236 -	m_d3dContext->VSSetShader(
   2.237 -		m_vertexShader.Get(),
   2.238 -		nullptr,
   2.239 -		0
   2.240 -		);
   2.241 -
   2.242 -	m_d3dContext->VSSetConstantBuffers(
   2.243 -		0,
   2.244 -		1,
   2.245 -		m_constantBuffer.GetAddressOf()
   2.246 -		);
   2.247 -
   2.248 -	m_d3dContext->PSSetShader(
   2.249 -		m_pixelShader.Get(),
   2.250 -		nullptr,
   2.251 -		0
   2.252 -		);
   2.253 -
   2.254 -	m_d3dContext->DrawIndexed(
   2.255 -		m_indexCount,
   2.256 -		0,
   2.257 -		0
   2.258 -		);
   2.259 -}
     3.1 --- a/src/video/windowsrt/CubeRenderer.h	Sun Nov 04 13:26:53 2012 -0500
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,44 +0,0 @@
     3.4 -#pragma once
     3.5 -
     3.6 -#include "Direct3DBase.h"
     3.7 -
     3.8 -struct ModelViewProjectionConstantBuffer
     3.9 -{
    3.10 -	DirectX::XMFLOAT4X4 model;
    3.11 -	DirectX::XMFLOAT4X4 view;
    3.12 -	DirectX::XMFLOAT4X4 projection;
    3.13 -};
    3.14 -
    3.15 -struct VertexPositionColor
    3.16 -{
    3.17 -	DirectX::XMFLOAT3 pos;
    3.18 -	DirectX::XMFLOAT3 color;
    3.19 -};
    3.20 -
    3.21 -// This class renders a simple spinning cube.
    3.22 -ref class CubeRenderer sealed : public Direct3DBase
    3.23 -{
    3.24 -public:
    3.25 -	CubeRenderer();
    3.26 -
    3.27 -	// Direct3DBase methods.
    3.28 -	virtual void CreateDeviceResources() override;
    3.29 -	virtual void CreateWindowSizeDependentResources() override;
    3.30 -	virtual void Render() override;
    3.31 -	
    3.32 -	// Method for updating time-dependent objects.
    3.33 -	void Update(float timeTotal, float timeDelta);
    3.34 -
    3.35 -private:
    3.36 -	bool m_loadingComplete;
    3.37 -
    3.38 -	Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
    3.39 -	Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
    3.40 -	Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
    3.41 -	Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
    3.42 -	Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
    3.43 -	Microsoft::WRL::ComPtr<ID3D11Buffer> m_constantBuffer;
    3.44 -
    3.45 -	uint32 m_indexCount;
    3.46 -	ModelViewProjectionConstantBuffer m_constantBufferData;
    3.47 -};
     4.1 --- a/src/video/windowsrt/SDL_WinRTApp.cpp	Sun Nov 04 13:26:53 2012 -0500
     4.2 +++ b/src/video/windowsrt/SDL_WinRTApp.cpp	Sun Nov 18 23:29:52 2012 -0500
     4.3 @@ -11,6 +11,9 @@
     4.4  #include "SDL_log.h"
     4.5  }
     4.6  
     4.7 +// TODO, WinRT: Remove reference(s) to BasicTimer.h
     4.8 +#include "BasicTimer.h"
     4.9 +
    4.10  // HACK, DLudwig: The C-style main() will get loaded via the app's
    4.11  // WinRT-styled main(), which is part of SDLmain_for_WinRT.cpp.
    4.12  // This seems wrong on some level, but does seem to work.
    4.13 @@ -54,7 +57,7 @@
    4.14  	CoreApplication::Resuming +=
    4.15          ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
    4.16  
    4.17 -	m_renderer = ref new CubeRenderer();
    4.18 +	m_renderer = ref new SDL_winrtrenderer();
    4.19  }
    4.20  
    4.21  void SDL_WinRTApp::SetWindow(CoreWindow^ window)
    4.22 @@ -112,9 +115,6 @@
    4.23  		if (m_windowVisible)
    4.24  		{
    4.25  			CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
    4.26 -			m_renderer->Update(0.0f, 0.0f);
    4.27 -			m_renderer->Render();
    4.28 -			m_renderer->Present(); // This call is synchronized to the display frame rate.
    4.29  		}
    4.30  		else
    4.31  		{
    4.32 @@ -123,6 +123,16 @@
    4.33  	}
    4.34  }
    4.35  
    4.36 +void SDL_WinRTApp::UpdateWindowFramebuffer(SDL_Surface * surface, SDL_Rect * rects, int numrects)
    4.37 +{
    4.38 +    if (!m_windowClosed && m_windowVisible)
    4.39 +	{
    4.40 +		m_renderer->Update(0.0f, 0.0f);
    4.41 +		m_renderer->Render();
    4.42 +		m_renderer->Present(); // This call is synchronized to the display frame rate.
    4.43 +	}
    4.44 +}
    4.45 +
    4.46  void SDL_WinRTApp::Uninitialize()
    4.47  {
    4.48  }
     5.1 --- a/src/video/windowsrt/SDL_WinRTApp.h	Sun Nov 04 13:26:53 2012 -0500
     5.2 +++ b/src/video/windowsrt/SDL_WinRTApp.h	Sun Nov 18 23:29:52 2012 -0500
     5.3 @@ -2,7 +2,7 @@
     5.4  
     5.5  #include "SDLmain_WinRT_common.h"
     5.6  #include "SDL_winrtvideo.h"
     5.7 -#include "CubeRenderer.h"
     5.8 +#include "SDL_winrtrenderer.h"
     5.9  #include <vector>
    5.10  
    5.11  using namespace Windows::UI::Core;
    5.12 @@ -26,6 +26,7 @@
    5.13      const SDL_WindowData * GetSDLWindowData() const;
    5.14      bool HasSDLWindowData() const;
    5.15      void SetSDLWindowData(const SDL_WindowData * windowData);
    5.16 +    void UpdateWindowFramebuffer(SDL_Surface * surface, SDL_Rect * rects, int numrects);
    5.17  
    5.18  protected:
    5.19  	// Event Handlers.
    5.20 @@ -43,7 +44,7 @@
    5.21      void OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);
    5.22  
    5.23  private:
    5.24 -	CubeRenderer^ m_renderer;
    5.25 +	SDL_winrtrenderer^ m_renderer;
    5.26  	bool m_windowClosed;
    5.27  	bool m_windowVisible;
    5.28      const SDL_WindowData* m_sdlWindowData;
     6.1 --- a/src/video/windowsrt/SDL_winrtframebuffer.cpp	Sun Nov 04 13:26:53 2012 -0500
     6.2 +++ b/src/video/windowsrt/SDL_winrtframebuffer.cpp	Sun Nov 18 23:29:52 2012 -0500
     6.3 @@ -24,6 +24,9 @@
     6.4  
     6.5  #include "../SDL_sysvideo.h"
     6.6  #include "SDL_winrtframebuffer_c.h"
     6.7 +#include "SDL_WinRTApp.h"
     6.8 +
     6.9 +extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
    6.10  
    6.11  
    6.12  #define WINRT_SURFACE   "_SDL_WinRTSurface"
    6.13 @@ -76,6 +79,9 @@
    6.14                       SDL_GetWindowID(window), ++frame_number);
    6.15          SDL_SaveBMP(surface, file);
    6.16      }
    6.17 +
    6.18 +    SDL_WinRTGlobalApp->UpdateWindowFramebuffer(surface, rects, numrects);
    6.19 +
    6.20      return 0;
    6.21  }
    6.22  
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.cpp	Sun Nov 18 23:29:52 2012 -0500
     7.3 @@ -0,0 +1,182 @@
     7.4 +#include "SDLmain_WinRT_common.h"
     7.5 +#include "SDL_winrtrenderer.h"
     7.6 +
     7.7 +using namespace DirectX;
     7.8 +using namespace Microsoft::WRL;
     7.9 +using namespace Windows::Foundation;
    7.10 +using namespace Windows::UI::Core;
    7.11 +
    7.12 +SDL_winrtrenderer::SDL_winrtrenderer() :
    7.13 +	m_loadingComplete(false),
    7.14 +	m_vertexCount(0)
    7.15 +{
    7.16 +}
    7.17 +
    7.18 +void SDL_winrtrenderer::CreateDeviceResources()
    7.19 +{
    7.20 +	Direct3DBase::CreateDeviceResources();
    7.21 +
    7.22 +	auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
    7.23 +	auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
    7.24 +
    7.25 +	auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
    7.26 +		DX::ThrowIfFailed(
    7.27 +			m_d3dDevice->CreateVertexShader(
    7.28 + 				fileData->Data,
    7.29 +				fileData->Length,
    7.30 +				nullptr,
    7.31 +				&m_vertexShader
    7.32 +				)
    7.33 +			);
    7.34 +
    7.35 +		const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    7.36 +		{
    7.37 +			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    7.38 +			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    7.39 +		};
    7.40 +
    7.41 +		DX::ThrowIfFailed(
    7.42 +			m_d3dDevice->CreateInputLayout(
    7.43 +				vertexDesc,
    7.44 +				ARRAYSIZE(vertexDesc),
    7.45 +				fileData->Data,
    7.46 +				fileData->Length,
    7.47 +				&m_inputLayout
    7.48 +				)
    7.49 +			);
    7.50 +	});
    7.51 +
    7.52 +	auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
    7.53 +		DX::ThrowIfFailed(
    7.54 +			m_d3dDevice->CreatePixelShader(
    7.55 +				fileData->Data,
    7.56 +				fileData->Length,
    7.57 +				nullptr,
    7.58 +				&m_pixelShader
    7.59 +				)
    7.60 +			);
    7.61 +
    7.62 +		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    7.63 +		DX::ThrowIfFailed(
    7.64 +			m_d3dDevice->CreateBuffer(
    7.65 +				&constantBufferDesc,
    7.66 +				nullptr,
    7.67 +				&m_constantBuffer
    7.68 +				)
    7.69 +			);
    7.70 +	});
    7.71 +
    7.72 +	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    7.73 +		VertexPositionColor cubeVertices[] = 
    7.74 +		{
    7.75 +			{XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    7.76 +			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    7.77 +			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    7.78 +			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    7.79 +		};
    7.80 +
    7.81 +		m_vertexCount = ARRAYSIZE(cubeVertices);
    7.82 +
    7.83 +		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    7.84 +		vertexBufferData.pSysMem = cubeVertices;
    7.85 +		vertexBufferData.SysMemPitch = 0;
    7.86 +		vertexBufferData.SysMemSlicePitch = 0;
    7.87 +		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    7.88 +		DX::ThrowIfFailed(
    7.89 +			m_d3dDevice->CreateBuffer(
    7.90 +				&vertexBufferDesc,
    7.91 +				&vertexBufferData,
    7.92 +				&m_vertexBuffer
    7.93 +				)
    7.94 +			);
    7.95 +	});
    7.96 +
    7.97 +	createCubeTask.then([this] () {
    7.98 +		m_loadingComplete = true;
    7.99 +	});
   7.100 +}
   7.101 +
   7.102 +void SDL_winrtrenderer::Update(float timeTotal, float timeDelta)
   7.103 +{
   7.104 +	(void) timeDelta; // Unused parameter.
   7.105 +
   7.106 +	XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
   7.107 +	XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
   7.108 +	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
   7.109 +
   7.110 +	XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixIdentity());
   7.111 +	XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixIdentity());
   7.112 +}
   7.113 +
   7.114 +void SDL_winrtrenderer::Render()
   7.115 +{
   7.116 +	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
   7.117 +	m_d3dContext->ClearRenderTargetView(
   7.118 +		m_renderTargetView.Get(),
   7.119 +		midnightBlue
   7.120 +		);
   7.121 +
   7.122 +	m_d3dContext->ClearDepthStencilView(
   7.123 +		m_depthStencilView.Get(),
   7.124 +		D3D11_CLEAR_DEPTH,
   7.125 +		1.0f,
   7.126 +		0
   7.127 +		);
   7.128 +
   7.129 +	// Only draw the cube once it is loaded (loading is asynchronous).
   7.130 +	if (!m_loadingComplete)
   7.131 +	{
   7.132 +		return;
   7.133 +	}
   7.134 +
   7.135 +	m_d3dContext->RSSetState(m_rasterState.Get());
   7.136 +
   7.137 +	m_d3dContext->OMSetRenderTargets(
   7.138 +		1,
   7.139 +		m_renderTargetView.GetAddressOf(),
   7.140 +		m_depthStencilView.Get()
   7.141 +		);
   7.142 +
   7.143 +	m_d3dContext->UpdateSubresource(
   7.144 +		m_constantBuffer.Get(),
   7.145 +		0,
   7.146 +		NULL,
   7.147 +		&m_constantBufferData,
   7.148 +		0,
   7.149 +		0
   7.150 +		);
   7.151 +
   7.152 +	UINT stride = sizeof(VertexPositionColor);
   7.153 +	UINT offset = 0;
   7.154 +	m_d3dContext->IASetVertexBuffers(
   7.155 +		0,
   7.156 +		1,
   7.157 +		m_vertexBuffer.GetAddressOf(),
   7.158 +		&stride,
   7.159 +		&offset
   7.160 +		);
   7.161 +
   7.162 +	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
   7.163 +
   7.164 +	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
   7.165 +
   7.166 +	m_d3dContext->VSSetShader(
   7.167 +		m_vertexShader.Get(),
   7.168 +		nullptr,
   7.169 +		0
   7.170 +		);
   7.171 +
   7.172 +	m_d3dContext->VSSetConstantBuffers(
   7.173 +		0,
   7.174 +		1,
   7.175 +		m_constantBuffer.GetAddressOf()
   7.176 +		);
   7.177 +
   7.178 +	m_d3dContext->PSSetShader(
   7.179 +		m_pixelShader.Get(),
   7.180 +		nullptr,
   7.181 +		0
   7.182 +		);
   7.183 +
   7.184 +	m_d3dContext->Draw(4, 0);
   7.185 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.h	Sun Nov 18 23:29:52 2012 -0500
     8.3 @@ -0,0 +1,43 @@
     8.4 +#pragma once
     8.5 +
     8.6 +#include "Direct3DBase.h"
     8.7 +
     8.8 +struct ModelViewProjectionConstantBuffer
     8.9 +{
    8.10 +	DirectX::XMFLOAT4X4 model;
    8.11 +	DirectX::XMFLOAT4X4 view;
    8.12 +	DirectX::XMFLOAT4X4 projection;
    8.13 +};
    8.14 +
    8.15 +struct VertexPositionColor
    8.16 +{
    8.17 +	DirectX::XMFLOAT3 pos;
    8.18 +	DirectX::XMFLOAT3 color;
    8.19 +};
    8.20 +
    8.21 +// This class renders a simple spinning cube.
    8.22 +ref class SDL_winrtrenderer sealed : public Direct3DBase
    8.23 +{
    8.24 +public:
    8.25 +	SDL_winrtrenderer();
    8.26 +
    8.27 +	// Direct3DBase methods.
    8.28 +	virtual void CreateDeviceResources() override;
    8.29 +	virtual void Render() override;
    8.30 +	
    8.31 +	// Method for updating time-dependent objects.
    8.32 +	void Update(float timeTotal, float timeDelta);
    8.33 +
    8.34 +private:
    8.35 +	bool m_loadingComplete;
    8.36 +
    8.37 +	Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
    8.38 +	Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
    8.39 +	Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
    8.40 +	Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
    8.41 +	Microsoft::WRL::ComPtr<ID3D11Buffer> m_constantBuffer;
    8.42 +	Microsoft::WRL::ComPtr<ID3D11RasterizerState> m_rasterState;
    8.43 +
    8.44 +	uint32 m_vertexCount;
    8.45 +	ModelViewProjectionConstantBuffer m_constantBufferData;
    8.46 +};