home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 18 Cube Mapping / DynamicCube / CubeRenderTarget.cpp next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  3.9 KB  |  139 lines

  1. //***************************************************************************************
  2. // CubeRenderTarget.cpp by Frank Luna (C) 2011 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. #include "CubeRenderTarget.h"
  6.  
  7. CubeRenderTarget::CubeRenderTarget(ID3D12Device* device, 
  8.                            UINT width, UINT height,
  9.                            DXGI_FORMAT format)
  10. {
  11.     md3dDevice = device;
  12.  
  13.     mWidth = width;
  14.     mHeight = height;
  15.     mFormat = format;
  16.  
  17.     mViewport = { 0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f };
  18.     mScissorRect = { 0, 0, (int)width, (int)height };
  19.  
  20.     BuildResource();
  21. }
  22.  
  23. ID3D12Resource*  CubeRenderTarget::Resource()
  24. {
  25.     return mCubeMap.Get();
  26. }
  27.  
  28. CD3DX12_GPU_DESCRIPTOR_HANDLE CubeRenderTarget::Srv()
  29. {
  30.     return mhGpuSrv;
  31. }
  32.  
  33. CD3DX12_CPU_DESCRIPTOR_HANDLE CubeRenderTarget::Rtv(int faceIndex)
  34. {
  35.     return mhCpuRtv[faceIndex];
  36. }
  37.  
  38. D3D12_VIEWPORT CubeRenderTarget::Viewport()const
  39. {
  40.     return mViewport;
  41. }
  42.  
  43. D3D12_RECT CubeRenderTarget::ScissorRect()const
  44. {
  45.     return mScissorRect;
  46. }
  47.  
  48. void CubeRenderTarget::BuildDescriptors(CD3DX12_CPU_DESCRIPTOR_HANDLE hCpuSrv,
  49.                                     CD3DX12_GPU_DESCRIPTOR_HANDLE hGpuSrv,
  50.                                     CD3DX12_CPU_DESCRIPTOR_HANDLE hCpuRtv[6])
  51. {
  52.     // Save references to the descriptors. 
  53.     mhCpuSrv = hCpuSrv;
  54.     mhGpuSrv = hGpuSrv;
  55.  
  56.     for(int i = 0; i < 6; ++i)
  57.         mhCpuRtv[i] = hCpuRtv[i];
  58.  
  59.     //  Create the descriptors
  60.     BuildDescriptors();
  61. }
  62.  
  63. void CubeRenderTarget::OnResize(UINT newWidth, UINT newHeight)
  64. {
  65.     if((mWidth != newWidth) || (mHeight != newHeight))
  66.     {
  67.         mWidth = newWidth;
  68.         mHeight = newHeight;
  69.  
  70.         BuildResource();
  71.  
  72.         // New resource, so we need new descriptors to that resource.
  73.         BuildDescriptors();
  74.     }
  75. }
  76.  
  77. void CubeRenderTarget::BuildDescriptors()
  78. {
  79.     D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
  80.     srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  81.     srvDesc.Format = mFormat;
  82.     srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE;
  83.     srvDesc.TextureCube.MostDetailedMip = 0;
  84.     srvDesc.TextureCube.MipLevels = 1;
  85.     srvDesc.TextureCube.ResourceMinLODClamp = 0.0f;
  86.  
  87.     // Create SRV to the entire cubemap resource.
  88.     md3dDevice->CreateShaderResourceView(mCubeMap.Get(), &srvDesc, mhCpuSrv);
  89.  
  90.     // Create RTV to each cube face.
  91.     for(int i = 0; i < 6; ++i)
  92.     {
  93.         D3D12_RENDER_TARGET_VIEW_DESC rtvDesc; 
  94.         rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
  95.         rtvDesc.Format = mFormat;
  96.         rtvDesc.Texture2DArray.MipSlice = 0;
  97.         rtvDesc.Texture2DArray.PlaneSlice = 0;
  98.  
  99.         // Render target to ith element.
  100.         rtvDesc.Texture2DArray.FirstArraySlice = i;
  101.  
  102.         // Only view one element of the array.
  103.         rtvDesc.Texture2DArray.ArraySize = 1;
  104.  
  105.         // Create RTV to ith cubemap face.
  106.         md3dDevice->CreateRenderTargetView(mCubeMap.Get(), &rtvDesc, mhCpuRtv[i]);
  107.     }
  108. }
  109.  
  110. void CubeRenderTarget::BuildResource()
  111. {
  112.     // Note, compressed formats cannot be used for UAV.  We get error like:
  113.     // ERROR: ID3D11Device::CreateTexture2D: The format (0x4d, BC3_UNORM) 
  114.     // cannot be bound as an UnorderedAccessView, or cast to a format that
  115.     // could be bound as an UnorderedAccessView.  Therefore this format 
  116.     // does not support D3D11_BIND_UNORDERED_ACCESS.
  117.  
  118.     D3D12_RESOURCE_DESC texDesc;
  119.     ZeroMemory(&texDesc, sizeof(D3D12_RESOURCE_DESC));
  120.     texDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  121.     texDesc.Alignment = 0;
  122.     texDesc.Width = mWidth;
  123.     texDesc.Height = mHeight;
  124.     texDesc.DepthOrArraySize = 6;
  125.     texDesc.MipLevels = 1;
  126.     texDesc.Format = mFormat;
  127.     texDesc.SampleDesc.Count = 1;
  128.     texDesc.SampleDesc.Quality = 0;
  129.     texDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  130.     texDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
  131.  
  132.     ThrowIfFailed(md3dDevice->CreateCommittedResource(
  133.         &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  134.         D3D12_HEAP_FLAG_NONE,
  135.         &texDesc,
  136.         D3D12_RESOURCE_STATE_GENERIC_READ,
  137.         nullptr,
  138.         IID_PPV_ARGS(&mCubeMap)));
  139. }