GteDX11TextureDS.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.1 (2016/11/13)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
13 {
15 }
16 
17 DX11TextureDS::DX11TextureDS(ID3D11Device* device, TextureDS const* texture)
18  :
19  DX11Texture2(texture),
20  mDSView(nullptr)
21 {
22  // Specify the texture description.
23  D3D11_TEXTURE2D_DESC desc;
24  desc.Width = texture->GetWidth();
25  desc.Height = texture->GetHeight();
26  desc.MipLevels = 1;
27  desc.ArraySize = 1;
28  desc.Format = GetDepthResourceFormat(static_cast<DXGI_FORMAT>(texture->GetFormat()));
29  desc.SampleDesc.Count = 1;
30  desc.SampleDesc.Quality = 0;
31  desc.Usage = D3D11_USAGE_DEFAULT;
32  desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
33  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
34  desc.MiscFlags = (texture->IsShared() ?
35  D3D11_RESOURCE_MISC_SHARED : D3D11_RESOURCE_MISC_NONE);
36  if (texture->IsShaderInput())
37  {
38  desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
39  }
40 
41  // Create the texture. Depth-stencil textures are not initialized by
42  // system memory data.
43  ID3D11Texture2D* dxTexture = nullptr;
44  HRESULT hr = device->CreateTexture2D(&desc, nullptr, &dxTexture);
45  CHECK_HR_RETURN_VOID("Failed to create depth-stencil texture");
46  mDXObject = dxTexture;
47 
48  // Create a view of the texture.
49  CreateDSView(device);
50 
51  // Create a shader resource view if the depth-stencil is to be used as an
52  // input to shaders.
53  if (texture->IsShaderInput())
54  {
55  CreateDSSRView(device);
56  }
57 
58  // Create a staging texture if requested.
59  if (texture->GetCopyType() != Resource::COPY_NONE)
60  {
61  CreateStaging(device, desc);
62  }
63 }
64 
65 DX11TextureDS::DX11TextureDS(ID3D11Device* device, DX11TextureDS const* dxSharedTexture)
66  :
67  DX11Texture2(dxSharedTexture->GetTexture()),
68  mDSView(nullptr)
69 {
70  ID3D11Texture2D* dxShared = dxSharedTexture->CreateSharedDXObject(device);
71  mDXObject = dxShared;
72  CreateDSView(device);
73 }
74 
75 std::shared_ptr<GEObject> DX11TextureDS::Create(void* device, GraphicsObject const* object)
76 {
77  if (object->GetType() == GT_TEXTURE_DS)
78  {
79  return std::make_shared<DX11TextureDS>(
80  reinterpret_cast<ID3D11Device*>(device),
81  static_cast<TextureDS const*>(object));
82  }
83 
84  LogError("Invalid object type.");
85  return nullptr;
86 }
87 
89 {
91  HRESULT hr = SetPrivateName(mDSView, name);
92  CHECK_HR_RETURN_NONE("Failed to set private name");
93 }
94 
95 void DX11TextureDS::CreateDSView(ID3D11Device* device)
96 {
97  D3D11_DEPTH_STENCIL_VIEW_DESC desc;
98  desc.Format = static_cast<DXGI_FORMAT>(GetTexture()->GetFormat());
99  desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
100  desc.Flags = 0;
101  desc.Texture2D.MipSlice = 0;
102  HRESULT hr = device->CreateDepthStencilView(GetDXTexture(), &desc, &mDSView);
103  CHECK_HR_RETURN_NONE("Failed to create depth-stencil view");
104 }
105 
106 void DX11TextureDS::CreateDSSRView(ID3D11Device* device)
107 {
108  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
109  desc.Format = GetDepthSRVFormat(static_cast<DXGI_FORMAT>(GetTexture()->GetFormat()));
110  desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
111  desc.Texture2D.MostDetailedMip = 0;
112  desc.Texture2D.MipLevels = 1;
113  HRESULT hr = device->CreateShaderResourceView(GetDXTexture(), &desc, &mSRView);
114  CHECK_HR_RETURN_NONE("Failed to create shader resource view");
115 }
116 
117 DXGI_FORMAT DX11TextureDS::GetDepthResourceFormat(DXGI_FORMAT depthFormat)
118 {
119  if (depthFormat == DXGI_FORMAT_D16_UNORM)
120  {
121  return DXGI_FORMAT_R16_TYPELESS;
122  }
123 
124  if (depthFormat == DXGI_FORMAT_D24_UNORM_S8_UINT)
125  {
126  return DXGI_FORMAT_R24G8_TYPELESS;
127  }
128 
129  if (depthFormat == DXGI_FORMAT_D32_FLOAT)
130  {
131  return DXGI_FORMAT_R32_TYPELESS;
132  }
133 
134  if (depthFormat == DXGI_FORMAT_D32_FLOAT_S8X24_UINT)
135  {
136  return DXGI_FORMAT_R32G8X24_TYPELESS;
137  }
138 
139  LogError("Invalid depth format.");
140  return DXGI_FORMAT_UNKNOWN;
141 }
142 
143 DXGI_FORMAT DX11TextureDS::GetDepthSRVFormat(DXGI_FORMAT depthFormat)
144 {
145  if (depthFormat == DXGI_FORMAT_D16_UNORM)
146  {
147  return DXGI_FORMAT_R16_UNORM;
148  }
149 
150  if (depthFormat == DXGI_FORMAT_D24_UNORM_S8_UINT)
151  {
152  return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
153  }
154 
155  if (depthFormat == DXGI_FORMAT_D32_FLOAT)
156  {
157  return DXGI_FORMAT_R32_FLOAT;
158  }
159 
160  if (depthFormat == DXGI_FORMAT_D32_FLOAT_S8X24_UINT)
161  {
162  return DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS;
163  }
164 
165  LogError("Invalid depth format.");
166  return DXGI_FORMAT_UNKNOWN;
167 }
bool IsShared() const
Definition: GteTexture2.cpp:39
COPY_NONE
Definition: GteResource.h:55
virtual void SetName(std::string const &name) override
unsigned int GetWidth() const
Definition: GteTexture2.cpp:22
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
#define D3D11_RESOURCE_MISC_NONE
#define CHECK_HR_RETURN_NONE(msg)
HRESULT SetPrivateName(ID3D11DeviceChild *object, std::string const &name)
DXGI_FORMAT GetDepthSRVFormat(DXGI_FORMAT depthFormat)
Texture2 * GetTexture() const
DXGI_FORMAT GetDepthResourceFormat(DXGI_FORMAT depthFormat)
GraphicsObjectType GetType() const
GLuint const GLchar * name
Definition: glcorearb.h:781
ULONG FinalRelease(T *&object)
DX11TextureDS(ID3D11Device *device, TextureDS const *texture)
virtual void SetName(std::string const &name)
ID3D11Texture2D * GetDXTexture() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
#define LogError(message)
Definition: GteLogger.h:92
unsigned int GetHeight() const
Definition: GteTexture2.cpp:27
GLuint texture
Definition: glcorearb.h:410
ID3D11DeviceChild * mDXObject
GT_TEXTURE_DS
void CreateDSSRView(ID3D11Device *device)
void CreateStaging(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
void CreateDSView(ID3D11Device *device)
#define D3D11_CPU_ACCESS_NONE
#define CHECK_HR_RETURN_VOID(msg)
bool IsShaderInput() const
CopyType GetCopyType() const
Definition: GteResource.h:136
ID3D11Texture2D * CreateSharedDXObject(ID3D11Device *device) const
ID3D11DepthStencilView * mDSView
ID3D11ShaderResourceView * mSRView
DFType GetFormat() const
Definition: GteTexture.h:100


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59