src/video/windowsrt/SDL_winrtrenderer.cpp
author David Ludwig <dludwig@pobox.com>
Wed, 21 Nov 2012 17:38:17 -0500
changeset 8349 020b8b65bcfc
parent 8348 7cdcd6d28c85
child 8350 ecc8c88ed82e
permissions -rw-r--r--
WinRT: added code to update the main texture on each frame with dummy data
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@8348
    35
			{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_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@8348
    63
			{XMFLOAT3(-1.0f, -1.0f, 0.0f),  XMFLOAT2(0.0f, 0.0f)},
dludwig@8348
    64
			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
dludwig@8348
    65
			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
dludwig@8348
    66
			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT2(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@8347
    85
	auto createMainTextureTask = createCubeTask.then([this] () {
dludwig@8347
    86
		D3D11_TEXTURE2D_DESC textureDesc = {0};
dludwig@8347
    87
		textureDesc.Width = (int)m_windowBounds.Width;
dludwig@8347
    88
		textureDesc.Height = (int)m_windowBounds.Height;
dludwig@8347
    89
		textureDesc.MipLevels = 1;
dludwig@8347
    90
		textureDesc.ArraySize = 1;
dludwig@8347
    91
		textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@8347
    92
		textureDesc.SampleDesc.Count = 1;
dludwig@8347
    93
		textureDesc.SampleDesc.Quality = 0;
dludwig@8347
    94
		textureDesc.Usage = D3D11_USAGE_DYNAMIC;
dludwig@8347
    95
		textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
dludwig@8347
    96
		textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dludwig@8347
    97
		textureDesc.MiscFlags = 0;
dludwig@8347
    98
dludwig@8347
    99
		int numPixels = (int)m_windowBounds.Width * (int)m_windowBounds.Height;
dludwig@8347
   100
		std::vector<uint8> initialTexturePixels(numPixels * 4);
dludwig@8347
   101
		for (int i = 0; i < (numPixels * 4); i += 4) {
dludwig@8347
   102
			initialTexturePixels[i+0] = 0xFF;
dludwig@8347
   103
			initialTexturePixels[i+1] = 0x00;
dludwig@8347
   104
			initialTexturePixels[i+2] = 0x00;
dludwig@8347
   105
			initialTexturePixels[i+3] = 0xFF;
dludwig@8347
   106
		}
dludwig@8347
   107
		D3D11_SUBRESOURCE_DATA initialTextureData = {0};
dludwig@8347
   108
		initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
dludwig@8347
   109
		initialTextureData.SysMemPitch = (int)m_windowBounds.Width * 4;
dludwig@8347
   110
		initialTextureData.SysMemSlicePitch = numPixels * 4;
dludwig@8347
   111
		DX::ThrowIfFailed(
dludwig@8347
   112
			m_d3dDevice->CreateTexture2D(
dludwig@8347
   113
				&textureDesc,
dludwig@8347
   114
				&initialTextureData,
dludwig@8347
   115
				&m_mainTexture
dludwig@8347
   116
				)
dludwig@8347
   117
			);
dludwig@8347
   118
dludwig@8347
   119
		D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
dludwig@8347
   120
		resourceViewDesc.Format = textureDesc.Format;
dludwig@8347
   121
		resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
dludwig@8347
   122
		resourceViewDesc.Texture2D.MostDetailedMip = 0;
dludwig@8347
   123
		resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
dludwig@8347
   124
		DX::ThrowIfFailed(
dludwig@8347
   125
			m_d3dDevice->CreateShaderResourceView(
dludwig@8347
   126
				m_mainTexture.Get(),
dludwig@8347
   127
				&resourceViewDesc,
dludwig@8347
   128
				&m_mainTextureResourceView)
dludwig@8347
   129
			);
dludwig@8347
   130
	});
dludwig@8347
   131
dludwig@8347
   132
	auto createMainSamplerTask = createMainTextureTask.then([this] () {
dludwig@8347
   133
		D3D11_SAMPLER_DESC samplerDesc;
dludwig@8347
   134
		samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
dludwig@8347
   135
		samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8347
   136
		samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8347
   137
		samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
dludwig@8347
   138
		samplerDesc.MipLODBias = 0.0f;
dludwig@8347
   139
		samplerDesc.MaxAnisotropy = 1;
dludwig@8347
   140
		samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
dludwig@8347
   141
		samplerDesc.BorderColor[0] = 0.0f;
dludwig@8347
   142
		samplerDesc.BorderColor[1] = 0.0f;
dludwig@8347
   143
		samplerDesc.BorderColor[2] = 0.0f;
dludwig@8347
   144
		samplerDesc.BorderColor[3] = 0.0f;
dludwig@8347
   145
		samplerDesc.MinLOD = 0.0f;
dludwig@8347
   146
		samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
dludwig@8347
   147
		DX::ThrowIfFailed(
dludwig@8347
   148
			m_d3dDevice->CreateSamplerState(
dludwig@8347
   149
				&samplerDesc,
dludwig@8347
   150
				&m_mainSampler
dludwig@8347
   151
				)
dludwig@8347
   152
			);
dludwig@8347
   153
	});
dludwig@8347
   154
dludwig@8347
   155
	createMainSamplerTask.then([this] () {
dludwig@8322
   156
		m_loadingComplete = true;
dludwig@8322
   157
	});
dludwig@8322
   158
}
dludwig@8322
   159
dludwig@8342
   160
void SDL_winrtrenderer::Render()
dludwig@8322
   161
{
dludwig@8322
   162
	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
dludwig@8322
   163
	m_d3dContext->ClearRenderTargetView(
dludwig@8322
   164
		m_renderTargetView.Get(),
dludwig@8322
   165
		midnightBlue
dludwig@8322
   166
		);
dludwig@8322
   167
dludwig@8322
   168
	m_d3dContext->ClearDepthStencilView(
dludwig@8322
   169
		m_depthStencilView.Get(),
dludwig@8322
   170
		D3D11_CLEAR_DEPTH,
dludwig@8322
   171
		1.0f,
dludwig@8322
   172
		0
dludwig@8322
   173
		);
dludwig@8322
   174
dludwig@8322
   175
	// Only draw the cube once it is loaded (loading is asynchronous).
dludwig@8322
   176
	if (!m_loadingComplete)
dludwig@8322
   177
	{
dludwig@8322
   178
		return;
dludwig@8322
   179
	}
dludwig@8322
   180
dludwig@8349
   181
	// Update the main texture (for SDL usage):
dludwig@8349
   182
	D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
dludwig@8349
   183
	DX::ThrowIfFailed(
dludwig@8349
   184
		m_d3dContext->Map(
dludwig@8349
   185
			m_mainTexture.Get(),
dludwig@8349
   186
			0,
dludwig@8349
   187
			D3D11_MAP_WRITE_DISCARD,
dludwig@8349
   188
			0,
dludwig@8349
   189
			&textureMemory)
dludwig@8349
   190
		);
dludwig@8349
   191
dludwig@8349
   192
	const int max = (int)m_windowBounds.Width * (int)m_windowBounds.Height * 4;
dludwig@8349
   193
	uint8 * buf = (uint8 *)textureMemory.pData;
dludwig@8349
   194
	for (int i = 0; i < max; i += 4) {
dludwig@8349
   195
		buf[i+0] = 0x00;
dludwig@8349
   196
		buf[i+1] = 0xFF;
dludwig@8349
   197
		buf[i+2] = 0x00;
dludwig@8349
   198
		buf[i+3] = 0xFF;
dludwig@8349
   199
	}
dludwig@8349
   200
dludwig@8349
   201
	m_d3dContext->Unmap(
dludwig@8349
   202
		m_mainTexture.Get(),
dludwig@8349
   203
		0);
dludwig@8349
   204
dludwig@8322
   205
	m_d3dContext->OMSetRenderTargets(
dludwig@8322
   206
		1,
dludwig@8322
   207
		m_renderTargetView.GetAddressOf(),
dludwig@8322
   208
		m_depthStencilView.Get()
dludwig@8322
   209
		);
dludwig@8322
   210
dludwig@8322
   211
	UINT stride = sizeof(VertexPositionColor);
dludwig@8322
   212
	UINT offset = 0;
dludwig@8322
   213
	m_d3dContext->IASetVertexBuffers(
dludwig@8322
   214
		0,
dludwig@8322
   215
		1,
dludwig@8322
   216
		m_vertexBuffer.GetAddressOf(),
dludwig@8322
   217
		&stride,
dludwig@8322
   218
		&offset
dludwig@8322
   219
		);
dludwig@8322
   220
dludwig@8342
   221
	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
dludwig@8322
   222
dludwig@8322
   223
	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
dludwig@8322
   224
dludwig@8322
   225
	m_d3dContext->VSSetShader(
dludwig@8322
   226
		m_vertexShader.Get(),
dludwig@8322
   227
		nullptr,
dludwig@8322
   228
		0
dludwig@8322
   229
		);
dludwig@8322
   230
dludwig@8322
   231
	m_d3dContext->PSSetShader(
dludwig@8322
   232
		m_pixelShader.Get(),
dludwig@8322
   233
		nullptr,
dludwig@8322
   234
		0
dludwig@8322
   235
		);
dludwig@8322
   236
dludwig@8347
   237
	m_d3dContext->PSSetShaderResources(0, 1, m_mainTextureResourceView.GetAddressOf());
dludwig@8347
   238
dludwig@8347
   239
	m_d3dContext->PSSetSamplers(0, 1, m_mainSampler.GetAddressOf());
dludwig@8347
   240
dludwig@8342
   241
	m_d3dContext->Draw(4, 0);
dludwig@8322
   242
}