src/video/windowsrt/SDL_winrtrenderer.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 18 Nov 2012 23:29:52 -0500
changeset 8342 05d7aa70928e
parent 8322 src/video/windowsrt/CubeRenderer.cpp@839cd578a474
child 8344 11fd2e01a070
permissions -rw-r--r--
WinRT: started work on renderer by getting Direct3D 11.1 to display a single, fullscreen rectangle
     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("SimpleVertexShader.cso");
    20 	auto loadPSTask = DX::ReadDataAsync("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 		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    60 		DX::ThrowIfFailed(
    61 			m_d3dDevice->CreateBuffer(
    62 				&constantBufferDesc,
    63 				nullptr,
    64 				&m_constantBuffer
    65 				)
    66 			);
    67 	});
    68 
    69 	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    70 		VertexPositionColor cubeVertices[] = 
    71 		{
    72 			{XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    73 			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    74 			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    75 			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    76 		};
    77 
    78 		m_vertexCount = ARRAYSIZE(cubeVertices);
    79 
    80 		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    81 		vertexBufferData.pSysMem = cubeVertices;
    82 		vertexBufferData.SysMemPitch = 0;
    83 		vertexBufferData.SysMemSlicePitch = 0;
    84 		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    85 		DX::ThrowIfFailed(
    86 			m_d3dDevice->CreateBuffer(
    87 				&vertexBufferDesc,
    88 				&vertexBufferData,
    89 				&m_vertexBuffer
    90 				)
    91 			);
    92 	});
    93 
    94 	createCubeTask.then([this] () {
    95 		m_loadingComplete = true;
    96 	});
    97 }
    98 
    99 void SDL_winrtrenderer::Update(float timeTotal, float timeDelta)
   100 {
   101 	(void) timeDelta; // Unused parameter.
   102 
   103 	XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
   104 	XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
   105 	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
   106 
   107 	XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixIdentity());
   108 	XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixIdentity());
   109 }
   110 
   111 void SDL_winrtrenderer::Render()
   112 {
   113 	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
   114 	m_d3dContext->ClearRenderTargetView(
   115 		m_renderTargetView.Get(),
   116 		midnightBlue
   117 		);
   118 
   119 	m_d3dContext->ClearDepthStencilView(
   120 		m_depthStencilView.Get(),
   121 		D3D11_CLEAR_DEPTH,
   122 		1.0f,
   123 		0
   124 		);
   125 
   126 	// Only draw the cube once it is loaded (loading is asynchronous).
   127 	if (!m_loadingComplete)
   128 	{
   129 		return;
   130 	}
   131 
   132 	m_d3dContext->RSSetState(m_rasterState.Get());
   133 
   134 	m_d3dContext->OMSetRenderTargets(
   135 		1,
   136 		m_renderTargetView.GetAddressOf(),
   137 		m_depthStencilView.Get()
   138 		);
   139 
   140 	m_d3dContext->UpdateSubresource(
   141 		m_constantBuffer.Get(),
   142 		0,
   143 		NULL,
   144 		&m_constantBufferData,
   145 		0,
   146 		0
   147 		);
   148 
   149 	UINT stride = sizeof(VertexPositionColor);
   150 	UINT offset = 0;
   151 	m_d3dContext->IASetVertexBuffers(
   152 		0,
   153 		1,
   154 		m_vertexBuffer.GetAddressOf(),
   155 		&stride,
   156 		&offset
   157 		);
   158 
   159 	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
   160 
   161 	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
   162 
   163 	m_d3dContext->VSSetShader(
   164 		m_vertexShader.Get(),
   165 		nullptr,
   166 		0
   167 		);
   168 
   169 	m_d3dContext->VSSetConstantBuffers(
   170 		0,
   171 		1,
   172 		m_constantBuffer.GetAddressOf()
   173 		);
   174 
   175 	m_d3dContext->PSSetShader(
   176 		m_pixelShader.Get(),
   177 		nullptr,
   178 		0
   179 		);
   180 
   181 	m_d3dContext->Draw(4, 0);
   182 }