src/video/windowsrt/SDL_winrtrenderer.cpp
changeset 8342 05d7aa70928e
parent 8322 839cd578a474
child 8344 11fd2e01a070
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.cpp	Sun Nov 18 23:29:52 2012 -0500
     1.3 @@ -0,0 +1,182 @@
     1.4 +#include "SDLmain_WinRT_common.h"
     1.5 +#include "SDL_winrtrenderer.h"
     1.6 +
     1.7 +using namespace DirectX;
     1.8 +using namespace Microsoft::WRL;
     1.9 +using namespace Windows::Foundation;
    1.10 +using namespace Windows::UI::Core;
    1.11 +
    1.12 +SDL_winrtrenderer::SDL_winrtrenderer() :
    1.13 +	m_loadingComplete(false),
    1.14 +	m_vertexCount(0)
    1.15 +{
    1.16 +}
    1.17 +
    1.18 +void SDL_winrtrenderer::CreateDeviceResources()
    1.19 +{
    1.20 +	Direct3DBase::CreateDeviceResources();
    1.21 +
    1.22 +	auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
    1.23 +	auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
    1.24 +
    1.25 +	auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
    1.26 +		DX::ThrowIfFailed(
    1.27 +			m_d3dDevice->CreateVertexShader(
    1.28 + 				fileData->Data,
    1.29 +				fileData->Length,
    1.30 +				nullptr,
    1.31 +				&m_vertexShader
    1.32 +				)
    1.33 +			);
    1.34 +
    1.35 +		const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    1.36 +		{
    1.37 +			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.38 +			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.39 +		};
    1.40 +
    1.41 +		DX::ThrowIfFailed(
    1.42 +			m_d3dDevice->CreateInputLayout(
    1.43 +				vertexDesc,
    1.44 +				ARRAYSIZE(vertexDesc),
    1.45 +				fileData->Data,
    1.46 +				fileData->Length,
    1.47 +				&m_inputLayout
    1.48 +				)
    1.49 +			);
    1.50 +	});
    1.51 +
    1.52 +	auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
    1.53 +		DX::ThrowIfFailed(
    1.54 +			m_d3dDevice->CreatePixelShader(
    1.55 +				fileData->Data,
    1.56 +				fileData->Length,
    1.57 +				nullptr,
    1.58 +				&m_pixelShader
    1.59 +				)
    1.60 +			);
    1.61 +
    1.62 +		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    1.63 +		DX::ThrowIfFailed(
    1.64 +			m_d3dDevice->CreateBuffer(
    1.65 +				&constantBufferDesc,
    1.66 +				nullptr,
    1.67 +				&m_constantBuffer
    1.68 +				)
    1.69 +			);
    1.70 +	});
    1.71 +
    1.72 +	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    1.73 +		VertexPositionColor cubeVertices[] = 
    1.74 +		{
    1.75 +			{XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    1.76 +			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    1.77 +			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    1.78 +			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    1.79 +		};
    1.80 +
    1.81 +		m_vertexCount = ARRAYSIZE(cubeVertices);
    1.82 +
    1.83 +		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    1.84 +		vertexBufferData.pSysMem = cubeVertices;
    1.85 +		vertexBufferData.SysMemPitch = 0;
    1.86 +		vertexBufferData.SysMemSlicePitch = 0;
    1.87 +		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    1.88 +		DX::ThrowIfFailed(
    1.89 +			m_d3dDevice->CreateBuffer(
    1.90 +				&vertexBufferDesc,
    1.91 +				&vertexBufferData,
    1.92 +				&m_vertexBuffer
    1.93 +				)
    1.94 +			);
    1.95 +	});
    1.96 +
    1.97 +	createCubeTask.then([this] () {
    1.98 +		m_loadingComplete = true;
    1.99 +	});
   1.100 +}
   1.101 +
   1.102 +void SDL_winrtrenderer::Update(float timeTotal, float timeDelta)
   1.103 +{
   1.104 +	(void) timeDelta; // Unused parameter.
   1.105 +
   1.106 +	XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
   1.107 +	XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
   1.108 +	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
   1.109 +
   1.110 +	XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixIdentity());
   1.111 +	XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixIdentity());
   1.112 +}
   1.113 +
   1.114 +void SDL_winrtrenderer::Render()
   1.115 +{
   1.116 +	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
   1.117 +	m_d3dContext->ClearRenderTargetView(
   1.118 +		m_renderTargetView.Get(),
   1.119 +		midnightBlue
   1.120 +		);
   1.121 +
   1.122 +	m_d3dContext->ClearDepthStencilView(
   1.123 +		m_depthStencilView.Get(),
   1.124 +		D3D11_CLEAR_DEPTH,
   1.125 +		1.0f,
   1.126 +		0
   1.127 +		);
   1.128 +
   1.129 +	// Only draw the cube once it is loaded (loading is asynchronous).
   1.130 +	if (!m_loadingComplete)
   1.131 +	{
   1.132 +		return;
   1.133 +	}
   1.134 +
   1.135 +	m_d3dContext->RSSetState(m_rasterState.Get());
   1.136 +
   1.137 +	m_d3dContext->OMSetRenderTargets(
   1.138 +		1,
   1.139 +		m_renderTargetView.GetAddressOf(),
   1.140 +		m_depthStencilView.Get()
   1.141 +		);
   1.142 +
   1.143 +	m_d3dContext->UpdateSubresource(
   1.144 +		m_constantBuffer.Get(),
   1.145 +		0,
   1.146 +		NULL,
   1.147 +		&m_constantBufferData,
   1.148 +		0,
   1.149 +		0
   1.150 +		);
   1.151 +
   1.152 +	UINT stride = sizeof(VertexPositionColor);
   1.153 +	UINT offset = 0;
   1.154 +	m_d3dContext->IASetVertexBuffers(
   1.155 +		0,
   1.156 +		1,
   1.157 +		m_vertexBuffer.GetAddressOf(),
   1.158 +		&stride,
   1.159 +		&offset
   1.160 +		);
   1.161 +
   1.162 +	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
   1.163 +
   1.164 +	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
   1.165 +
   1.166 +	m_d3dContext->VSSetShader(
   1.167 +		m_vertexShader.Get(),
   1.168 +		nullptr,
   1.169 +		0
   1.170 +		);
   1.171 +
   1.172 +	m_d3dContext->VSSetConstantBuffers(
   1.173 +		0,
   1.174 +		1,
   1.175 +		m_constantBuffer.GetAddressOf()
   1.176 +		);
   1.177 +
   1.178 +	m_d3dContext->PSSetShader(
   1.179 +		m_pixelShader.Get(),
   1.180 +		nullptr,
   1.181 +		0
   1.182 +		);
   1.183 +
   1.184 +	m_d3dContext->Draw(4, 0);
   1.185 +}