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