src/video/windowsrt/SDL_winrtrenderer.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 20 Nov 2012 08:46:10 -0500
changeset 8346 a15d524f1d2e
parent 8345 a7bbc0d7f381
child 8347 f0e61c2638ad
permissions -rw-r--r--
WinRT: included shaders in SDL library (moved from SDLWinRTTestApp)
     1 #include "SDLmain_WinRT_common.h"
     2 #include "SDL_winrtrenderer.h"
     3 
     4 using namespace DirectX;
     5 using namespace Microsoft::WRL;
     6 using namespace Windows::Foundation;
     7 using namespace Windows::UI::Core;
     8 
     9 SDL_winrtrenderer::SDL_winrtrenderer() :
    10 	m_loadingComplete(false),
    11 	m_vertexCount(0)
    12 {
    13 }
    14 
    15 void SDL_winrtrenderer::CreateDeviceResources()
    16 {
    17 	Direct3DBase::CreateDeviceResources();
    18 
    19 	auto loadVSTask = DX::ReadDataAsync("SDL_VS2012_WinRT\\SimpleVertexShader.cso");
    20 	auto loadPSTask = DX::ReadDataAsync("SDL_VS2012_WinRT\\SimplePixelShader.cso");
    21 
    22 	auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
    23 		DX::ThrowIfFailed(
    24 			m_d3dDevice->CreateVertexShader(
    25  				fileData->Data,
    26 				fileData->Length,
    27 				nullptr,
    28 				&m_vertexShader
    29 				)
    30 			);
    31 
    32 		const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    33 		{
    34 			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    35 			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    36 		};
    37 
    38 		DX::ThrowIfFailed(
    39 			m_d3dDevice->CreateInputLayout(
    40 				vertexDesc,
    41 				ARRAYSIZE(vertexDesc),
    42 				fileData->Data,
    43 				fileData->Length,
    44 				&m_inputLayout
    45 				)
    46 			);
    47 	});
    48 
    49 	auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
    50 		DX::ThrowIfFailed(
    51 			m_d3dDevice->CreatePixelShader(
    52 				fileData->Data,
    53 				fileData->Length,
    54 				nullptr,
    55 				&m_pixelShader
    56 				)
    57 			);
    58 	});
    59 
    60 	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    61 		VertexPositionColor cubeVertices[] = 
    62 		{
    63 			{XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    64 			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    65 			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    66 			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    67 		};
    68 
    69 		m_vertexCount = ARRAYSIZE(cubeVertices);
    70 
    71 		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    72 		vertexBufferData.pSysMem = cubeVertices;
    73 		vertexBufferData.SysMemPitch = 0;
    74 		vertexBufferData.SysMemSlicePitch = 0;
    75 		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    76 		DX::ThrowIfFailed(
    77 			m_d3dDevice->CreateBuffer(
    78 				&vertexBufferDesc,
    79 				&vertexBufferData,
    80 				&m_vertexBuffer
    81 				)
    82 			);
    83 	});
    84 
    85 	createCubeTask.then([this] () {
    86 		m_loadingComplete = true;
    87 	});
    88 }
    89 
    90 void SDL_winrtrenderer::Render()
    91 {
    92 	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
    93 	m_d3dContext->ClearRenderTargetView(
    94 		m_renderTargetView.Get(),
    95 		midnightBlue
    96 		);
    97 
    98 	m_d3dContext->ClearDepthStencilView(
    99 		m_depthStencilView.Get(),
   100 		D3D11_CLEAR_DEPTH,
   101 		1.0f,
   102 		0
   103 		);
   104 
   105 	// Only draw the cube once it is loaded (loading is asynchronous).
   106 	if (!m_loadingComplete)
   107 	{
   108 		return;
   109 	}
   110 
   111 	m_d3dContext->OMSetRenderTargets(
   112 		1,
   113 		m_renderTargetView.GetAddressOf(),
   114 		m_depthStencilView.Get()
   115 		);
   116 
   117 	UINT stride = sizeof(VertexPositionColor);
   118 	UINT offset = 0;
   119 	m_d3dContext->IASetVertexBuffers(
   120 		0,
   121 		1,
   122 		m_vertexBuffer.GetAddressOf(),
   123 		&stride,
   124 		&offset
   125 		);
   126 
   127 	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
   128 
   129 	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
   130 
   131 	m_d3dContext->VSSetShader(
   132 		m_vertexShader.Get(),
   133 		nullptr,
   134 		0
   135 		);
   136 
   137 	m_d3dContext->PSSetShader(
   138 		m_pixelShader.Get(),
   139 		nullptr,
   140 		0
   141 		);
   142 
   143 	m_d3dContext->Draw(4, 0);
   144 }