CubeRenderer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include "pch.h"
  2. #include "CubeRenderer.h"
  3. using namespace DirectX;
  4. using namespace Microsoft::WRL;
  5. using namespace Windows::Foundation;
  6. using namespace Windows::UI::Core;
  7. CubeRenderer::CubeRenderer() :
  8. m_loadingComplete(false),
  9. m_indexCount(0)
  10. {
  11. }
  12. void CubeRenderer::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_cubesTexSamplerState.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 CubeRenderer::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, &m_CCWcullMode);
  91. cmdesc.FrontCounterClockwise = false;
  92. m_d3dDevice->CreateRasterizerState(&cmdesc, &m_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. DX::ThrowIfFailed(
  97. m_d3dDevice->CreateVertexShader(
  98. fileData->Data,
  99. fileData->Length,
  100. nullptr,
  101. &m_vertexShader
  102. )
  103. );
  104. const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
  105. {
  106. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  107. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  108. };
  109. DX::ThrowIfFailed(
  110. m_d3dDevice->CreateInputLayout(
  111. vertexDesc,
  112. ARRAYSIZE(vertexDesc),
  113. fileData->Data,
  114. fileData->Length,
  115. &m_inputLayout
  116. )
  117. );
  118. });
  119. auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
  120. DX::ThrowIfFailed(
  121. m_d3dDevice->CreatePixelShader(
  122. fileData->Data,
  123. fileData->Length,
  124. nullptr,
  125. &m_pixelShader
  126. )
  127. );
  128. CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
  129. DX::ThrowIfFailed(
  130. m_d3dDevice->CreateBuffer(
  131. &constantBufferDesc,
  132. nullptr,
  133. &m_constantBuffer
  134. )
  135. );
  136. });
  137. auto createCubeTask = (createPSTask && createVSTask).then([this] () {
  138. Vertex v[] =
  139. {
  140. // Front Face
  141. Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f),
  142. Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f),
  143. Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f),
  144. Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 1.0f),
  145. // Back Face
  146. Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f),
  147. Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 1.0f),
  148. Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
  149. Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f),
  150. // Top Face
  151. Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f),
  152. Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
  153. Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f),
  154. Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 1.0f),
  155. // Bottom Face
  156. Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),
  157. Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),
  158. Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f),
  159. Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f),
  160. // Left Face
  161. Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f),
  162. Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
  163. Vertex(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f),
  164. Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),
  165. // Right Face
  166. Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),
  167. Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f),
  168. Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f),
  169. Vertex( 1.0f, -1.0f, 1.0f, 1.0f, 1.0f),
  170. };
  171. D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
  172. vertexBufferData.pSysMem = v;
  173. vertexBufferData.SysMemPitch = 0;
  174. vertexBufferData.SysMemSlicePitch = 0;
  175. CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(v), D3D11_BIND_VERTEX_BUFFER);
  176. DX::ThrowIfFailed(
  177. m_d3dDevice->CreateBuffer(
  178. &vertexBufferDesc,
  179. &vertexBufferData,
  180. &m_vertexBuffer
  181. )
  182. );
  183. DWORD indices[] = {
  184. // Front Face
  185. 0, 2, 1,
  186. 0, 3, 2,
  187. // Back Face
  188. 4, 6, 5,
  189. 4, 7, 6,
  190. // Top Face
  191. 8, 10, 9,
  192. 8, 11, 10,
  193. // Bottom Face
  194. 12, 14, 13,
  195. 12, 15, 14,
  196. // Left Face
  197. 16, 18, 17,
  198. 16, 19, 18,
  199. // Right Face
  200. 20, 22, 21,
  201. 20, 23, 22
  202. };
  203. m_indexCount = ARRAYSIZE(indices);
  204. D3D11_SUBRESOURCE_DATA indexBufferData = {0};
  205. indexBufferData.pSysMem = indices;
  206. indexBufferData.SysMemPitch = 0;
  207. indexBufferData.SysMemSlicePitch = 0;
  208. CD3D11_BUFFER_DESC indexBufferDesc(sizeof(indices), D3D11_BIND_INDEX_BUFFER);
  209. DX::ThrowIfFailed(
  210. m_d3dDevice->CreateBuffer(
  211. &indexBufferDesc,
  212. &indexBufferData,
  213. &m_indexBuffer
  214. )
  215. );
  216. });
  217. createCubeTask.then([this] () {
  218. m_loadingComplete = true;
  219. });
  220. }
  221. void CubeRenderer::CreateWindowSizeDependentResources()
  222. {
  223. Direct3DBase::CreateWindowSizeDependentResources();
  224. float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
  225. float fovAngleY = 70.0f * XM_PI / 180.0f;
  226. if (aspectRatio < 1.0f)
  227. {
  228. fovAngleY /= aspectRatio;
  229. }
  230. XMStoreFloat4x4(
  231. &m_constantBufferData.projection,
  232. XMMatrixTranspose(
  233. XMMatrixPerspectiveFovRH(
  234. fovAngleY,
  235. aspectRatio,
  236. 0.01f,
  237. 100.0f
  238. )
  239. )
  240. );
  241. }
  242. void CubeRenderer::Update(float timeTotal, float timeDelta)
  243. {
  244. (void) timeDelta; // Unused parameter.
  245. XMVECTOR eye = XMVectorSet(0.0f, 0.0f, 3.f, 0.0f);
  246. XMVECTOR at = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
  247. XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  248. XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
  249. XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
  250. }
  251. void CubeRenderer::Render()
  252. {
  253. std::lock_guard<std::mutex> lock(m_mutex);
  254. Render(m_renderTargetView, m_depthStencilView);
  255. }
  256. void CubeRenderer::Render(Microsoft::WRL::ComPtr<ID3D11RenderTargetView> renderTargetView, Microsoft::WRL::ComPtr<ID3D11DepthStencilView> depthStencilView)
  257. {
  258. const float black[] = {0, 0, 0, 1.0 };
  259. m_d3dContext->ClearRenderTargetView(
  260. renderTargetView.Get(),
  261. black
  262. );
  263. m_d3dContext->ClearDepthStencilView(
  264. depthStencilView.Get(),
  265. D3D11_CLEAR_DEPTH,
  266. 1.0f,
  267. 0
  268. );
  269. // Only draw the cube once it is loaded (loading is asynchronous).
  270. if (!m_SRV || !m_loadingComplete)
  271. {
  272. return;
  273. }
  274. m_d3dContext->OMSetRenderTargets(
  275. 1,
  276. renderTargetView.GetAddressOf(),
  277. depthStencilView.Get()
  278. );
  279. m_d3dContext->UpdateSubresource(
  280. m_constantBuffer.Get(),
  281. 0,
  282. NULL,
  283. &m_constantBufferData,
  284. 0,
  285. 0
  286. );
  287. UINT stride = sizeof(Vertex);
  288. UINT offset = 0;
  289. m_d3dContext->IASetVertexBuffers(
  290. 0,
  291. 1,
  292. m_vertexBuffer.GetAddressOf(),
  293. &stride,
  294. &offset
  295. );
  296. m_d3dContext->IASetIndexBuffer(
  297. m_indexBuffer.Get(),
  298. DXGI_FORMAT_R32_UINT,
  299. 0
  300. );
  301. m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  302. m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  303. m_d3dContext->VSSetShader(
  304. m_vertexShader.Get(),
  305. nullptr,
  306. 0
  307. );
  308. m_d3dContext->VSSetConstantBuffers(
  309. 0,
  310. 1,
  311. m_constantBuffer.GetAddressOf()
  312. );
  313. m_d3dContext->PSSetShader(
  314. m_pixelShader.Get(),
  315. nullptr,
  316. 0
  317. );
  318. m_d3dContext->PSSetShaderResources( 0, 1, m_SRV.GetAddressOf());
  319. m_d3dContext->PSSetSamplers( 0, 1, m_cubesTexSamplerState.GetAddressOf());
  320. //float blendFactor[] = {0.75f, 0.75f, 0.75f, 1.0f};
  321. m_d3dContext->OMSetBlendState(m_transparency.Get(), nullptr, 0xffffffff);
  322. m_d3dContext->RSSetState(m_CCWcullMode.Get());
  323. m_d3dContext->DrawIndexed(
  324. m_indexCount,
  325. 0,
  326. 0
  327. );
  328. m_d3dContext->RSSetState(m_CWcullMode.Get());
  329. m_d3dContext->DrawIndexed(
  330. m_indexCount,
  331. 0,
  332. 0
  333. );
  334. }