WinRT: made the fullscreen display be backed by a texture
authorDavid Ludwig <dludwig@pobox.com>
Wed, 21 Nov 2012 17:07:48 -0500
changeset 8347f0e61c2638ad
parent 8346 a15d524f1d2e
child 8348 7cdcd6d28c85
WinRT: made the fullscreen display be backed by a texture
src/video/windowsrt/SDL_winrtrenderer.cpp
src/video/windowsrt/SDL_winrtrenderer.h
src/video/windowsrt/SimplePixelShader.hlsl
src/video/windowsrt/SimpleVertexShader.hlsl
     1.1 --- a/src/video/windowsrt/SDL_winrtrenderer.cpp	Tue Nov 20 08:46:10 2012 -0500
     1.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.cpp	Wed Nov 21 17:07:48 2012 -0500
     1.3 @@ -33,6 +33,7 @@
     1.4  		{
     1.5  			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
     1.6  			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
     1.7 +			{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
     1.8  		};
     1.9  
    1.10  		DX::ThrowIfFailed(
    1.11 @@ -60,10 +61,10 @@
    1.12  	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    1.13  		VertexPositionColor cubeVertices[] = 
    1.14  		{
    1.15 -			{XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    1.16 -			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    1.17 -			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    1.18 -			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    1.19 +			{XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT3(1.0f, 0.0f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
    1.20 +			{XMFLOAT3(-1.0f, 1.0f, 0.0f), XMFLOAT3(0.0f, 1.0f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
    1.21 +			{XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT3(0.0f, 0.0f, 1.0f), XMFLOAT2(1.0f, 0.0f)},
    1.22 +			{XMFLOAT3(1.0f, 1.0f, 0.0f), XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f)},
    1.23  		};
    1.24  
    1.25  		m_vertexCount = ARRAYSIZE(cubeVertices);
    1.26 @@ -82,7 +83,77 @@
    1.27  			);
    1.28  	});
    1.29  
    1.30 -	createCubeTask.then([this] () {
    1.31 +	auto createMainTextureTask = createCubeTask.then([this] () {
    1.32 +		D3D11_TEXTURE2D_DESC textureDesc = {0};
    1.33 +		textureDesc.Width = (int)m_windowBounds.Width;
    1.34 +		textureDesc.Height = (int)m_windowBounds.Height;
    1.35 +		textureDesc.MipLevels = 1;
    1.36 +		textureDesc.ArraySize = 1;
    1.37 +		textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    1.38 +		textureDesc.SampleDesc.Count = 1;
    1.39 +		textureDesc.SampleDesc.Quality = 0;
    1.40 +		textureDesc.Usage = D3D11_USAGE_DYNAMIC;
    1.41 +		textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    1.42 +		textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    1.43 +		textureDesc.MiscFlags = 0;
    1.44 +
    1.45 +		int numPixels = (int)m_windowBounds.Width * (int)m_windowBounds.Height;
    1.46 +		std::vector<uint8> initialTexturePixels(numPixels * 4);
    1.47 +		for (int i = 0; i < (numPixels * 4); i += 4) {
    1.48 +			initialTexturePixels[i+0] = 0xFF;
    1.49 +			initialTexturePixels[i+1] = 0x00;
    1.50 +			initialTexturePixels[i+2] = 0x00;
    1.51 +			initialTexturePixels[i+3] = 0xFF;
    1.52 +		}
    1.53 +		D3D11_SUBRESOURCE_DATA initialTextureData = {0};
    1.54 +		initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
    1.55 +		initialTextureData.SysMemPitch = (int)m_windowBounds.Width * 4;
    1.56 +		initialTextureData.SysMemSlicePitch = numPixels * 4;
    1.57 +		DX::ThrowIfFailed(
    1.58 +			m_d3dDevice->CreateTexture2D(
    1.59 +				&textureDesc,
    1.60 +				&initialTextureData,
    1.61 +				&m_mainTexture
    1.62 +				)
    1.63 +			);
    1.64 +
    1.65 +		D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
    1.66 +		resourceViewDesc.Format = textureDesc.Format;
    1.67 +		resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    1.68 +		resourceViewDesc.Texture2D.MostDetailedMip = 0;
    1.69 +		resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
    1.70 +		DX::ThrowIfFailed(
    1.71 +			m_d3dDevice->CreateShaderResourceView(
    1.72 +				m_mainTexture.Get(),
    1.73 +				&resourceViewDesc,
    1.74 +				&m_mainTextureResourceView)
    1.75 +			);
    1.76 +	});
    1.77 +
    1.78 +	auto createMainSamplerTask = createMainTextureTask.then([this] () {
    1.79 +		D3D11_SAMPLER_DESC samplerDesc;
    1.80 +		samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
    1.81 +		samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
    1.82 +		samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
    1.83 +		samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
    1.84 +		samplerDesc.MipLODBias = 0.0f;
    1.85 +		samplerDesc.MaxAnisotropy = 1;
    1.86 +		samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
    1.87 +		samplerDesc.BorderColor[0] = 0.0f;
    1.88 +		samplerDesc.BorderColor[1] = 0.0f;
    1.89 +		samplerDesc.BorderColor[2] = 0.0f;
    1.90 +		samplerDesc.BorderColor[3] = 0.0f;
    1.91 +		samplerDesc.MinLOD = 0.0f;
    1.92 +		samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
    1.93 +		DX::ThrowIfFailed(
    1.94 +			m_d3dDevice->CreateSamplerState(
    1.95 +				&samplerDesc,
    1.96 +				&m_mainSampler
    1.97 +				)
    1.98 +			);
    1.99 +	});
   1.100 +
   1.101 +	createMainSamplerTask.then([this] () {
   1.102  		m_loadingComplete = true;
   1.103  	});
   1.104  }
   1.105 @@ -140,5 +211,9 @@
   1.106  		0
   1.107  		);
   1.108  
   1.109 +	m_d3dContext->PSSetShaderResources(0, 1, m_mainTextureResourceView.GetAddressOf());
   1.110 +
   1.111 +	m_d3dContext->PSSetSamplers(0, 1, m_mainSampler.GetAddressOf());
   1.112 +
   1.113  	m_d3dContext->Draw(4, 0);
   1.114  }
     2.1 --- a/src/video/windowsrt/SDL_winrtrenderer.h	Tue Nov 20 08:46:10 2012 -0500
     2.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.h	Wed Nov 21 17:07:48 2012 -0500
     2.3 @@ -6,6 +6,7 @@
     2.4  {
     2.5  	DirectX::XMFLOAT3 pos;
     2.6  	DirectX::XMFLOAT3 color;
     2.7 +	DirectX::XMFLOAT2 tex;
     2.8  };
     2.9  
    2.10  // This class renders a simple spinning cube.
    2.11 @@ -25,6 +26,9 @@
    2.12  	Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
    2.13  	Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
    2.14  	Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
    2.15 +	Microsoft::WRL::ComPtr<ID3D11Texture2D> m_mainTexture;
    2.16 +	Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_mainTextureResourceView;
    2.17 +	Microsoft::WRL::ComPtr<ID3D11SamplerState> m_mainSampler;
    2.18  
    2.19  	uint32 m_vertexCount;
    2.20  };
     3.1 --- a/src/video/windowsrt/SimplePixelShader.hlsl	Tue Nov 20 08:46:10 2012 -0500
     3.2 +++ b/src/video/windowsrt/SimplePixelShader.hlsl	Wed Nov 21 17:07:48 2012 -0500
     3.3 @@ -1,10 +1,15 @@
     3.4 +Texture2D theTexture : register(t0);
     3.5 +SamplerState theSampler : register(s0);
     3.6 +
     3.7  struct PixelShaderInput
     3.8  {
     3.9  	float4 pos : SV_POSITION;
    3.10  	float3 color : COLOR0;
    3.11 +	float2 tex : TEXCOORD0;
    3.12  };
    3.13  
    3.14  float4 main(PixelShaderInput input) : SV_TARGET
    3.15  {
    3.16 -	return float4(input.color,1.0f);
    3.17 +	//return float4(input.color,1.0f);
    3.18 +	return theTexture.Sample(theSampler, input.tex);
    3.19  }
     4.1 --- a/src/video/windowsrt/SimpleVertexShader.hlsl	Tue Nov 20 08:46:10 2012 -0500
     4.2 +++ b/src/video/windowsrt/SimpleVertexShader.hlsl	Wed Nov 21 17:07:48 2012 -0500
     4.3 @@ -5,12 +5,14 @@
     4.4  {
     4.5  	float3 pos : POSITION;
     4.6  	float3 color : COLOR0;
     4.7 +	float2 tex : TEXCOORD0;
     4.8  };
     4.9  
    4.10  struct VertexShaderOutput
    4.11  {
    4.12  	float4 pos : SV_POSITION;
    4.13  	float3 color : COLOR0;
    4.14 +	float2 tex : TEXCOORD0;
    4.15  };
    4.16  
    4.17  VertexShaderOutput main(VertexShaderInput input)
    4.18 @@ -18,5 +20,6 @@
    4.19  	VertexShaderOutput output;
    4.20  	output.pos = float4(input.pos, 1.0f);
    4.21  	output.color = input.color;
    4.22 +	output.tex = input.tex;
    4.23  	return output;
    4.24  }