2
0

Texture.cpp 10 KB

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