QuadRenderer.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "pch.h"
  2. #include "QuadRenderer.h"
  3. using namespace DirectX;
  4. using namespace Microsoft::WRL;
  5. using namespace Windows::Foundation;
  6. using namespace Windows::UI::Core;
  7. QuadRenderer::QuadRenderer() :
  8. m_loadingComplete(false),
  9. m_indexCount(0)
  10. {
  11. }
  12. void QuadRenderer::CreateTextureFromByte(byte* buffer,int width,int height)
  13. {
  14. int pixelSize = 4;
  15. if (m_Texture.Get() == nullptr)
  16. {
  17. CD3D11_TEXTURE2D_DESC textureDesc(
  18. DXGI_FORMAT_B8G8R8A8_UNORM, // format
  19. static_cast<UINT>(width), // width
  20. static_cast<UINT>(height), // height
  21. 1, // arraySize
  22. 1, // mipLevels
  23. D3D11_BIND_SHADER_RESOURCE, // bindFlags
  24. D3D11_USAGE_DYNAMIC, // usage
  25. D3D11_CPU_ACCESS_WRITE, // cpuaccessFlags
  26. 1, // sampleCount
  27. 0, // sampleQuality
  28. 0 // miscFlags
  29. );
  30. D3D11_SUBRESOURCE_DATA data;
  31. data.pSysMem = buffer;
  32. data.SysMemPitch = pixelSize*width;
  33. data.SysMemSlicePitch = pixelSize*width*height;
  34. DX::ThrowIfFailed(
  35. m_d3dDevice->CreateTexture2D(
  36. &textureDesc,
  37. &data,
  38. m_Texture.ReleaseAndGetAddressOf()
  39. )
  40. );
  41. m_d3dDevice->CreateShaderResourceView(m_Texture.Get(), NULL, m_SRV.ReleaseAndGetAddressOf());
  42. D3D11_SAMPLER_DESC sampDesc;
  43. ZeroMemory(&sampDesc, sizeof(sampDesc));
  44. sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  45. sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  46. sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  47. sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  48. sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
  49. sampDesc.MinLOD = 0;
  50. sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
  51. m_d3dDevice->CreateSamplerState(&sampDesc, m_QuadsTexSamplerState.ReleaseAndGetAddressOf());
  52. }
  53. else
  54. {
  55. int nRowSpan = width * pixelSize;
  56. D3D11_MAPPED_SUBRESOURCE mappedResource;
  57. HRESULT hr = m_d3dContext->Map(m_Texture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
  58. BYTE* mappedData = static_cast<BYTE*>(mappedResource.pData);
  59. for (int i = 0; i < height; ++i)
  60. {
  61. memcpy(mappedData + (i*mappedResource.RowPitch), buffer + (i*nRowSpan), nRowSpan);
  62. }
  63. m_d3dContext->Unmap(m_Texture.Get(), 0);
  64. }
  65. }
  66. void QuadRenderer::CreateDeviceResources()
  67. {
  68. Direct3DBase::CreateDeviceResources();
  69. D3D11_BLEND_DESC blendDesc;
  70. ZeroMemory( &blendDesc, sizeof(blendDesc) );
  71. D3D11_RENDER_TARGET_BLEND_DESC rtbd;
  72. ZeroMemory( &rtbd, sizeof(rtbd) );
  73. rtbd.BlendEnable = TRUE;
  74. rtbd.SrcBlend = D3D11_BLEND_SRC_ALPHA;
  75. rtbd.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  76. rtbd.BlendOp = D3D11_BLEND_OP_ADD;
  77. rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;
  78. rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;
  79. rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;
  80. rtbd.RenderTargetWriteMask = 0x0f;
  81. blendDesc.AlphaToCoverageEnable = false;
  82. blendDesc.RenderTarget[0] = rtbd;
  83. m_d3dDevice->CreateBlendState(&blendDesc, &m_Transparency);
  84. D3D11_RASTERIZER_DESC cmdesc;
  85. ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));
  86. cmdesc.FillMode = D3D11_FILL_SOLID;
  87. cmdesc.CullMode = D3D11_CULL_BACK;
  88. cmdesc.DepthClipEnable = TRUE;
  89. cmdesc.FrontCounterClockwise = true;
  90. m_d3dDevice->CreateRasterizerState(&cmdesc, &CCWcullMode);
  91. cmdesc.FrontCounterClockwise = false;
  92. m_d3dDevice->CreateRasterizerState(&cmdesc, &CWcullMode);
  93. auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
  94. auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
  95. auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData)
  96. {
  97. DX::ThrowIfFailed(
  98. m_d3dDevice->CreateVertexShader(
  99. fileData->Data,
  100. fileData->Length,
  101. nullptr,
  102. &m_vertexShader
  103. )
  104. );
  105. const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
  106. {
  107. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  108. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  109. };
  110. DX::ThrowIfFailed(
  111. m_d3dDevice->CreateInputLayout(
  112. vertexDesc,
  113. ARRAYSIZE(vertexDesc),
  114. fileData->Data,
  115. fileData->Length,
  116. &m_inputLayout
  117. )
  118. );
  119. });
  120. auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData)
  121. {
  122. DX::ThrowIfFailed(
  123. m_d3dDevice->CreatePixelShader(
  124. fileData->Data,
  125. fileData->Length,
  126. nullptr,
  127. &m_pixelShader
  128. )
  129. );
  130. CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
  131. DX::ThrowIfFailed(
  132. m_d3dDevice->CreateBuffer(
  133. &constantBufferDesc,
  134. nullptr,
  135. &m_constantBuffer
  136. )
  137. );
  138. });
  139. auto createCubeTask = (createPSTask && createVSTask).then([this] ()
  140. {
  141. Vertex v[] =
  142. {
  143. Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f),
  144. Vertex(1.0f, -1.0f, 1.0f, 0.0f, 1.0f),
  145. Vertex(1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
  146. Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f)
  147. };
  148. D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
  149. vertexBufferData.pSysMem = v;
  150. vertexBufferData.SysMemPitch = 0;
  151. vertexBufferData.SysMemSlicePitch = 0;
  152. CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(v), D3D11_BIND_VERTEX_BUFFER);
  153. DX::ThrowIfFailed(
  154. m_d3dDevice->CreateBuffer(
  155. &vertexBufferDesc,
  156. &vertexBufferData,
  157. &m_vertexBuffer
  158. )
  159. );
  160. DWORD indices[] =
  161. {
  162. // Front Face
  163. 0, 2, 1,
  164. 0, 3, 2,
  165. };
  166. m_indexCount = ARRAYSIZE(indices);
  167. D3D11_SUBRESOURCE_DATA indexBufferData = {0};
  168. indexBufferData.pSysMem = indices;
  169. indexBufferData.SysMemPitch = 0;
  170. indexBufferData.SysMemSlicePitch = 0;
  171. CD3D11_BUFFER_DESC indexBufferDesc(sizeof(indices), D3D11_BIND_INDEX_BUFFER);
  172. DX::ThrowIfFailed(
  173. m_d3dDevice->CreateBuffer(
  174. &indexBufferDesc,
  175. &indexBufferData,
  176. &m_indexBuffer
  177. )
  178. );
  179. });
  180. createCubeTask.then([this] ()
  181. {
  182. m_loadingComplete = true;
  183. });
  184. }
  185. void QuadRenderer::CreateWindowSizeDependentResources()
  186. {
  187. Direct3DBase::CreateWindowSizeDependentResources();
  188. float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
  189. float fovAngleY = 60.0f * (XM_PI / 180.0f);
  190. if (aspectRatio < 1.0f)
  191. {
  192. fovAngleY /= aspectRatio;
  193. }
  194. XMStoreFloat4x4(
  195. &m_constantBufferData.projection,
  196. XMMatrixTranspose(
  197. XMMatrixPerspectiveFovRH(
  198. fovAngleY,
  199. aspectRatio,
  200. 0.01f,
  201. 100.0f
  202. )
  203. )
  204. );
  205. }
  206. void QuadRenderer::Update(float timeTotal, float timeDelta)
  207. {
  208. (void) timeDelta; // Unused parameter.
  209. XMVECTOR X = XMVectorSet(0.0f, 0.0f, .3f, 0.0f);
  210. XMVECTOR Y = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
  211. XMVECTOR Z = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
  212. XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtLH(X, Y, Z)));
  213. XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
  214. }
  215. void QuadRenderer::Render()
  216. {
  217. Render(m_renderTargetView, m_depthStencilView);
  218. }
  219. void QuadRenderer::Render(Microsoft::WRL::ComPtr<ID3D11RenderTargetView> renderTargetView, Microsoft::WRL::ComPtr<ID3D11DepthStencilView> depthStencilView)
  220. {
  221. const float black[] = {0, 0, 0, 1.0 };
  222. m_d3dContext->ClearRenderTargetView(
  223. renderTargetView.Get(),
  224. black
  225. );
  226. m_d3dContext->ClearDepthStencilView(
  227. depthStencilView.Get(),
  228. D3D11_CLEAR_DEPTH,
  229. 1.0f,
  230. 0
  231. );
  232. if (m_SRV && m_loadingComplete) // Only draw the cube once it is loaded (loading is asynchronous).
  233. {
  234. m_d3dContext->OMSetRenderTargets(
  235. 1,
  236. renderTargetView.GetAddressOf(),
  237. depthStencilView.Get()
  238. );
  239. m_d3dContext->UpdateSubresource(
  240. m_constantBuffer.Get(),
  241. 0,
  242. NULL,
  243. &m_constantBufferData,
  244. 0,
  245. 0
  246. );
  247. UINT stride = sizeof(Vertex);
  248. UINT offset = 0;
  249. m_d3dContext->IASetVertexBuffers(
  250. 0,
  251. 1,
  252. m_vertexBuffer.GetAddressOf(),
  253. &stride,
  254. &offset
  255. );
  256. m_d3dContext->IASetIndexBuffer(
  257. m_indexBuffer.Get(),
  258. DXGI_FORMAT_R32_UINT,
  259. 0
  260. );
  261. m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  262. m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  263. m_d3dContext->VSSetShader(
  264. m_vertexShader.Get(),
  265. nullptr,
  266. 0
  267. );
  268. m_d3dContext->VSSetConstantBuffers(
  269. 0,
  270. 1,
  271. m_constantBuffer.GetAddressOf()
  272. );
  273. m_d3dContext->PSSetShader(
  274. m_pixelShader.Get(),
  275. nullptr,
  276. 0
  277. );
  278. m_d3dContext->PSSetShaderResources(0, 1, m_SRV.GetAddressOf());
  279. m_d3dContext->PSSetSamplers(0, 1, m_QuadsTexSamplerState.GetAddressOf());
  280. m_d3dContext->OMSetBlendState(m_Transparency.Get(), nullptr, 0xffffffff);
  281. m_d3dContext->RSSetState(CCWcullMode.Get());
  282. m_d3dContext->DrawIndexed(
  283. m_indexCount,
  284. 0,
  285. 0
  286. );
  287. }
  288. }