Texture.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Renderer/Texture.h>
  6. #include <Image/BlitSurface.h>
  7. #include <Renderer/Renderer.h>
  8. #include <Renderer/FatalErrorIfFailed.h>
  9. Texture::Texture(Renderer *inRenderer, const Surface *inSurface) :
  10. mRenderer(inRenderer)
  11. {
  12. // Store dimensions
  13. mWidth = inSurface->GetWidth();
  14. mHeight = inSurface->GetHeight();
  15. // Create description
  16. D3D12_RESOURCE_DESC desc = {};
  17. desc.MipLevels = 1;
  18. ESurfaceFormat format = inSurface->GetFormat();
  19. switch (format)
  20. {
  21. case ESurfaceFormat::A4L4: desc.Format = DXGI_FORMAT_R8G8_UNORM; break;
  22. case ESurfaceFormat::L8: desc.Format = DXGI_FORMAT_R8_UNORM; break;
  23. case ESurfaceFormat::A8: desc.Format = DXGI_FORMAT_A8_UNORM; break;
  24. case ESurfaceFormat::A8L8: desc.Format = DXGI_FORMAT_R8G8_UNORM; break;
  25. case ESurfaceFormat::R5G6B5: desc.Format = DXGI_FORMAT_B5G6R5_UNORM; break;
  26. case ESurfaceFormat::X1R5G5B5: desc.Format = DXGI_FORMAT_B5G5R5A1_UNORM; format = ESurfaceFormat::A1R5G5B5; break;
  27. case ESurfaceFormat::X4R4G4B4: desc.Format = DXGI_FORMAT_B4G4R4A4_UNORM; format = ESurfaceFormat::A4R4G4B4; break;
  28. case ESurfaceFormat::A1R5G5B5: desc.Format = DXGI_FORMAT_B5G5R5A1_UNORM; break;
  29. case ESurfaceFormat::A4R4G4B4: desc.Format = DXGI_FORMAT_B4G4R4A4_UNORM; break;
  30. case ESurfaceFormat::R8G8B8: desc.Format = DXGI_FORMAT_B8G8R8X8_UNORM; format = ESurfaceFormat::X8R8G8B8; break;
  31. case ESurfaceFormat::B8G8R8: desc.Format = DXGI_FORMAT_B8G8R8X8_UNORM; format = ESurfaceFormat::X8R8G8B8; break;
  32. case ESurfaceFormat::X8R8G8B8: desc.Format = DXGI_FORMAT_B8G8R8X8_UNORM; break;
  33. case ESurfaceFormat::X8B8G8R8: desc.Format = DXGI_FORMAT_B8G8R8X8_UNORM; format = ESurfaceFormat::X8R8G8B8; break;
  34. case ESurfaceFormat::A8R8G8B8: desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; break;
  35. case ESurfaceFormat::A8B8G8R8: desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; format = ESurfaceFormat::A8R8G8B8; break;
  36. case ESurfaceFormat::Invalid:
  37. default: JPH_ASSERT(false); break;
  38. }
  39. desc.Width = mWidth;
  40. desc.Height = mHeight;
  41. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  42. desc.DepthOrArraySize = 1;
  43. desc.SampleDesc.Count = 1;
  44. desc.SampleDesc.Quality = 0;
  45. desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  46. // Blit the surface to another temporary surface if the format changed
  47. const Surface *surface = inSurface;
  48. Ref<Surface> tmp;
  49. if (format != inSurface->GetFormat())
  50. {
  51. tmp = new SoftwareSurface(mWidth, mHeight, format);
  52. BlitSurface(inSurface, tmp);
  53. surface = tmp;
  54. }
  55. // Create texture in default heap
  56. D3D12_HEAP_PROPERTIES heap_properties = {};
  57. heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT;
  58. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  59. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  60. heap_properties.CreationNodeMask = 1;
  61. heap_properties.VisibleNodeMask = 1;
  62. FatalErrorIfFailed(inRenderer->GetDevice()->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&mTexture)));
  63. JPH_IF_DEBUG(mTexture->SetName(L"Texture");)
  64. // Determine required size of data to copy
  65. D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint;
  66. UINT64 row_size_in_bytes;
  67. UINT64 required_size = 0;
  68. inRenderer->GetDevice()->GetCopyableFootprints(&desc, 0, 1, 0, &footprint, nullptr, &row_size_in_bytes, &required_size);
  69. // Create the GPU upload buffer
  70. ComPtr<ID3D12Resource> upload_resource = mRenderer->CreateD3DResourceOnUploadHeap(required_size);
  71. JPH_IF_DEBUG(upload_resource->SetName(L"Texture Upload");)
  72. // Copy data to upload texture
  73. surface->Lock(ESurfaceLockMode::Read);
  74. uint8 *upload_data;
  75. D3D12_RANGE range = { 0, 0 }; // We're not going to read
  76. FatalErrorIfFailed(upload_resource->Map(0, &range, (void **)&upload_data));
  77. for (int y = 0; y < mHeight; ++y)
  78. memcpy(upload_data + y * row_size_in_bytes, surface->GetData() + y * surface->GetStride(), surface->GetBytesPerPixel() * mWidth);
  79. upload_resource->Unmap(0, nullptr);
  80. surface->UnLock();
  81. // Start a commandlist for the upload
  82. ID3D12GraphicsCommandList *list = inRenderer->GetUploadQueue().Start();
  83. // Copy the texture from our upload buffer to our final texture
  84. D3D12_TEXTURE_COPY_LOCATION copy_dst;
  85. copy_dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  86. copy_dst.pResource = mTexture.Get();
  87. copy_dst.SubresourceIndex = 0;
  88. D3D12_TEXTURE_COPY_LOCATION copy_src;
  89. copy_src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  90. copy_src.pResource = upload_resource.Get();
  91. copy_src.PlacedFootprint = footprint;
  92. list->CopyTextureRegion(&copy_dst, 0, 0, 0, &copy_src, nullptr);
  93. // Indicate that the texture is now ready to be used by a pixel shader
  94. D3D12_RESOURCE_BARRIER barrier;
  95. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  96. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  97. barrier.Transition.pResource = mTexture.Get();
  98. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  99. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  100. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  101. list->ResourceBarrier(1, &barrier);
  102. // Create a SRV for the texture
  103. mSRV = inRenderer->GetSRVHeap().Allocate();
  104. D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};
  105. srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  106. srv_desc.Format = desc.Format;
  107. srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  108. srv_desc.Texture2D.MipLevels = 1;
  109. inRenderer->GetDevice()->CreateShaderResourceView(mTexture.Get(), &srv_desc, mSRV);
  110. // Wait for copying to finish so we can destroy the upload texture
  111. inRenderer->GetUploadQueue().ExecuteAndWait();
  112. // Recycle the upload buffer
  113. inRenderer->RecycleD3DResourceOnUploadHeap(upload_resource.Get(), required_size);
  114. }
  115. Texture::Texture(Renderer *inRenderer, int inWidth, int inHeight) :
  116. mRenderer(inRenderer)
  117. {
  118. // Store dimensions
  119. mWidth = inWidth;
  120. mHeight = inHeight;
  121. // Allocate depth stencil buffer
  122. D3D12_CLEAR_VALUE clear_value = {};
  123. clear_value.Format = DXGI_FORMAT_D32_FLOAT;
  124. clear_value.DepthStencil.Depth = 1.0f;
  125. clear_value.DepthStencil.Stencil = 0;
  126. D3D12_HEAP_PROPERTIES heap_properties = {};
  127. heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT;
  128. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  129. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  130. heap_properties.CreationNodeMask = 1;
  131. heap_properties.VisibleNodeMask = 1;
  132. D3D12_RESOURCE_DESC depth_stencil_desc = {};
  133. depth_stencil_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  134. depth_stencil_desc.Alignment = 0;
  135. depth_stencil_desc.Width = mWidth;
  136. depth_stencil_desc.Height = mHeight;
  137. depth_stencil_desc.DepthOrArraySize = 1;
  138. depth_stencil_desc.MipLevels = 1;
  139. depth_stencil_desc.Format = DXGI_FORMAT_D32_FLOAT;
  140. depth_stencil_desc.SampleDesc.Count = 1;
  141. depth_stencil_desc.SampleDesc.Quality = 0;
  142. depth_stencil_desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  143. depth_stencil_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
  144. FatalErrorIfFailed(inRenderer->GetDevice()->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &depth_stencil_desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, &clear_value, IID_PPV_ARGS(&mTexture)));
  145. JPH_IF_DEBUG(mTexture->SetName(L"Render Target Texture");)
  146. // Create DSV for the texture
  147. mDSV = inRenderer->GetDSVHeap().Allocate();
  148. D3D12_DEPTH_STENCIL_VIEW_DESC dsv_desc = {};
  149. dsv_desc.Format = DXGI_FORMAT_D32_FLOAT;
  150. dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  151. dsv_desc.Flags = D3D12_DSV_FLAG_NONE;
  152. inRenderer->GetDevice()->CreateDepthStencilView(mTexture.Get(), &dsv_desc, mDSV);
  153. // Create a SRV for the texture
  154. mSRV = inRenderer->GetSRVHeap().Allocate();
  155. D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};
  156. srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  157. srv_desc.Format = DXGI_FORMAT_R32_FLOAT;
  158. srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  159. srv_desc.Texture2D.MipLevels = 1;
  160. inRenderer->GetDevice()->CreateShaderResourceView(mTexture.Get(), &srv_desc, mSRV);
  161. }
  162. Texture::~Texture()
  163. {
  164. if (mSRV.ptr != 0)
  165. mRenderer->GetSRVHeap().Free(mSRV);
  166. if (mDSV.ptr != 0)
  167. mRenderer->GetDSVHeap().Free(mDSV);
  168. if (mTexture != nullptr)
  169. mRenderer->RecycleD3DObject(mTexture.Get());
  170. }
  171. void Texture::Bind(int inSlot) const
  172. {
  173. mRenderer->GetCommandList()->SetGraphicsRootDescriptorTable(inSlot, mRenderer->GetSRVHeap().ConvertToGPUHandle(mSRV));
  174. }
  175. void Texture::ClearRenderTarget()
  176. {
  177. mRenderer->GetCommandList()->ClearDepthStencilView(mDSV, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
  178. }
  179. void Texture::SetAsRenderTarget(bool inSet) const
  180. {
  181. if (inSet)
  182. {
  183. // Indicate make the texture ready for rendering to
  184. D3D12_RESOURCE_BARRIER barrier;
  185. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  186. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  187. barrier.Transition.pResource = mTexture.Get();
  188. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  189. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_DEPTH_WRITE;
  190. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  191. mRenderer->GetCommandList()->ResourceBarrier(1, &barrier);
  192. // Set as render target
  193. mRenderer->GetCommandList()->OMSetRenderTargets(0, nullptr, FALSE, &mDSV);
  194. // Set view port
  195. D3D12_VIEWPORT viewport = { 0.0f, 0.0f, static_cast<float>(mWidth), static_cast<float>(mHeight), 0.0f, 1.0f };
  196. mRenderer->GetCommandList()->RSSetViewports(1, &viewport);
  197. // Set scissor rect
  198. D3D12_RECT scissor_rect = { 0, 0, static_cast<LONG>(mWidth), static_cast<LONG>(mHeight) };
  199. mRenderer->GetCommandList()->RSSetScissorRects(1, &scissor_rect);
  200. }
  201. else
  202. {
  203. // Indicate that the texture is now ready to be used by a pixel shader
  204. D3D12_RESOURCE_BARRIER barrier;
  205. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  206. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  207. barrier.Transition.pResource = mTexture.Get();
  208. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_DEPTH_WRITE;
  209. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  210. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  211. mRenderer->GetCommandList()->ResourceBarrier(1, &barrier);
  212. }
  213. }