GteDX11Texture2.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.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
12 DX11Texture2::DX11Texture2(ID3D11Device* device, Texture2 const* texture)
13  :
14  DX11TextureSingle(texture)
15 {
16  // Specify the texture description.
17  D3D11_TEXTURE2D_DESC desc;
18  desc.Width = texture->GetWidth();
19  desc.Height = texture->GetHeight();
20  desc.MipLevels = texture->GetNumLevels();
21  desc.ArraySize = 1;
22  desc.Format = static_cast<DXGI_FORMAT>(texture->GetFormat());
23  desc.SampleDesc.Count = 1;
24  desc.SampleDesc.Quality = 0;
25  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
26  desc.MiscFlags = (texture->IsShared() ?
27  D3D11_RESOURCE_MISC_SHARED : D3D11_RESOURCE_MISC_NONE);
28  Resource::Usage usage = texture->GetUsage();
29  if (usage == Resource::IMMUTABLE)
30  {
31  desc.Usage = D3D11_USAGE_IMMUTABLE;
32  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
33  }
34  else if (usage == Resource::DYNAMIC_UPDATE)
35  {
36  desc.Usage = D3D11_USAGE_DYNAMIC;
37  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
38  }
39  else // usage == Resource::SHADER_OUTPUT
40  {
41  desc.Usage = D3D11_USAGE_DEFAULT;
42  desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
43  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
44  }
45 
46  if (texture->WantAutogenerateMipmaps() && !texture->IsShared())
47  {
48  desc.Usage = D3D11_USAGE_DEFAULT;
49  desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
50  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
51  desc.MiscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
52  }
53 
54  // Create the texture.
55  ID3D11Texture2D* dxTexture = nullptr;
56  HRESULT hr;
57  if (texture->GetData())
58  {
59  unsigned int const numSubresources = texture->GetNumSubresources();
60  std::vector<D3D11_SUBRESOURCE_DATA> data(numSubresources);
61  for (unsigned int index = 0; index < numSubresources; ++index)
62  {
63  auto sr = texture->GetSubresource(index);
64  data[index].pSysMem = sr.data;
65  data[index].SysMemPitch = sr.rowPitch;
66  data[index].SysMemSlicePitch = 0;
67  }
68  hr = device->CreateTexture2D(&desc, &data[0], &dxTexture);
69  }
70  else
71  {
72  hr = device->CreateTexture2D(&desc, nullptr, &dxTexture);
73  }
74  CHECK_HR_RETURN_VOID("Failed to create texture");
75  mDXObject = dxTexture;
76 
77  // Create views of the texture.
78  CreateSRView(device, desc);
79  if (texture->GetUsage() == Resource::SHADER_OUTPUT)
80  {
81  CreateUAView(device, desc);
82  }
83 
84  // Create a staging texture if requested.
85  if (texture->GetCopyType() != Resource::COPY_NONE)
86  {
87  CreateStaging(device, desc);
88  }
89 
90  // Generate mipmaps if requested.
91  if (texture->WantAutogenerateMipmaps() && mSRView)
92  {
93  ID3D11DeviceContext* context;
94  device->GetImmediateContext(&context);
95  context->GenerateMips(mSRView);
96  context->Release();
97  }
98 }
99 
100 DX11Texture2::DX11Texture2(ID3D11Device* device, DX11Texture2 const* dxSharedTexture)
101  :
102  DX11TextureSingle(dxSharedTexture->GetTexture())
103 {
104  ID3D11Texture2D* dxShared = dxSharedTexture->CreateSharedDXObject(device);
105  mDXObject = dxShared;
106 
107  // Create view of the shared texture.
108  auto gtTexture = dxSharedTexture->GetTexture();
109  D3D11_TEXTURE2D_DESC desc;
110  dxShared->GetDesc(&desc);
111  CreateSRView(device, desc);
112  if (gtTexture->GetUsage() == Resource::SHADER_OUTPUT)
113  {
114  CreateUAView(device, desc);
115  }
116 
117  // Create a staging texture for the shared texture if the original texture
118  // also has ones.
119  if (gtTexture->GetCopyType() != Resource::COPY_NONE)
120  {
121  CreateStaging(device, desc);
122  }
123 }
124 
125 DX11Texture2::DX11Texture2(Texture2 const* texture, ID3D11Texture2D* dxTexture,
126  ID3D11ShaderResourceView* dxSRView)
127  :
128  DX11TextureSingle(texture)
129 {
130  mDXObject = dxTexture;
131  mSRView = dxSRView;
132 }
133 
134 std::shared_ptr<GEObject> DX11Texture2::Create(void* device, GraphicsObject const* object)
135 {
136  if (object->GetType() == GT_TEXTURE2)
137  {
138  return std::make_shared<DX11Texture2>(
139  reinterpret_cast<ID3D11Device*>(device),
140  static_cast<Texture2 const*>(object));
141  }
142 
143  LogError("Invalid object type.");
144  return nullptr;
145 }
146 
148  :
149  DX11TextureSingle(texture)
150 {
151 }
152 
153 ID3D11Texture2D* DX11Texture2::CreateSharedDXObject(ID3D11Device* device) const
154 {
155  IDXGIResource* resource = nullptr;
156  HRESULT hr = mDXObject->QueryInterface(__uuidof(IDXGIResource), (void**)&resource);
157  CHECK_HR_RETURN("QueryInterface failed", nullptr);
158 
159  HANDLE handle = nullptr;
160  hr = resource->GetSharedHandle(&handle);
161  resource->Release();
162  CHECK_HR_RETURN("GetSharedHandle failed", nullptr);
163 
164  ID3D11Texture2D* dxShared = nullptr;
165  hr = device->OpenSharedResource(handle, __uuidof(ID3D11Texture2D), (void**)&dxShared);
166  CHECK_HR_RETURN("OpenSharedResource failed", nullptr);
167  return dxShared;
168 }
169 
170 void DX11Texture2::CreateStaging(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
171 {
172  D3D11_TEXTURE2D_DESC desc;
173  desc.Width = tx.Width;
174  desc.Height = tx.Height;
175  desc.MipLevels = tx.MipLevels;
176  desc.ArraySize = tx.ArraySize;
177  desc.Format = tx.Format;
178  desc.SampleDesc.Count = tx.SampleDesc.Count;
179  desc.SampleDesc.Quality = tx.SampleDesc.Quality;
180  desc.Usage = D3D11_USAGE_STAGING;
181  desc.BindFlags = D3D11_BIND_NONE;
182  desc.CPUAccessFlags = msStagingAccess[GetTexture()->GetCopyType()];
183  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
184 
185  HRESULT hr = device->CreateTexture2D(&desc, nullptr, reinterpret_cast<ID3D11Texture2D**>(&mStaging));
186  CHECK_HR_RETURN_NONE("Failed to create staging texture");
187 }
188 
189 void DX11Texture2::CreateSRView(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
190 {
191  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
192  desc.Format = tx.Format;
193  desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
194  desc.Texture2D.MostDetailedMip = 0;
195  desc.Texture2D.MipLevels = tx.MipLevels;
196 
197  HRESULT hr = device->CreateShaderResourceView(GetDXTexture(), &desc, &mSRView);
198  CHECK_HR_RETURN_NONE("Failed to create shader resource view");
199 }
200 
201 void DX11Texture2::CreateUAView(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
202 {
203  D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
204  desc.Format = tx.Format;
205  desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
206  desc.Texture2D.MipSlice = 0;
207 
208  HRESULT hr = device->CreateUnorderedAccessView(GetDXTexture(), &desc, &mUAView);
209  CHECK_HR_RETURN_NONE("Failed to create unordered access view");
210 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
bool IsShared() const
Definition: GteTexture2.cpp:39
COPY_NONE
Definition: GteResource.h:55
unsigned int GetWidth() const
Definition: GteTexture2.cpp:22
GT_TEXTURE2
#define D3D11_RESOURCE_MISC_NONE
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
#define CHECK_HR_RETURN_NONE(msg)
void CreateSRView(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
IMMUTABLE
Definition: GteResource.h:42
typedef HANDLE(WINAPI *PFNWGLCREATEBUFFERREGIONARBPROC)(HDC hDC
Usage GetUsage() const
Definition: GteResource.h:126
Texture2 * GetTexture() const
#define D3D11_BIND_NONE
GraphicsObjectType GetType() const
ID3D11UnorderedAccessView * mUAView
ID3D11Resource * mStaging
void CreateUAView(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
ID3D11Texture2D * GetDXTexture() const
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
unsigned int GetHeight() const
Definition: GteTexture2.cpp:27
GLuint texture
Definition: glcorearb.h:410
ID3D11DeviceChild * mDXObject
char const * GetData() const
Definition: GteResource.h:151
void CreateStaging(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
unsigned int GetNumLevels() const
Definition: GteTexture.h:120
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
Subresource GetSubresource(unsigned int index) const
Definition: GteTexture.cpp:110
#define D3D11_CPU_ACCESS_NONE
#define CHECK_HR_RETURN_VOID(msg)
GLuint index
Definition: glcorearb.h:781
static UINT const msStagingAccess[]
CopyType GetCopyType() const
Definition: GteResource.h:136
ID3D11Texture2D * CreateSharedDXObject(ID3D11Device *device) const
#define CHECK_HR_RETURN(msg, value)
DX11Texture2(ID3D11Device *device, Texture2 const *texture)
bool WantAutogenerateMipmaps() const
Definition: GteTexture.h:175
ID3D11ShaderResourceView * mSRView
unsigned int GetNumSubresources() const
Definition: GteTexture.h:170
DFType GetFormat() const
Definition: GteTexture.h:100


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