GteDX11Texture3.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 DX11Texture3::DX11Texture3(ID3D11Device* device, Texture3 const* texture)
13  :
14  DX11TextureSingle(texture)
15 {
16  // Specify the texture description.
17  D3D11_TEXTURE3D_DESC desc;
18  desc.Width = texture->GetWidth();
19  desc.Height = texture->GetHeight();
20  desc.Depth = texture->GetThickness();
21  desc.MipLevels = texture->GetNumLevels();
22  desc.Format = static_cast<DXGI_FORMAT>(texture->GetFormat());
23  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
24  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
25  Resource::Usage usage = texture->GetUsage();
26  if (usage == Resource::IMMUTABLE)
27  {
28  desc.Usage = D3D11_USAGE_IMMUTABLE;
29  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
30  }
31  else if (usage == Resource::DYNAMIC_UPDATE)
32  {
33  desc.Usage = D3D11_USAGE_DYNAMIC;
34  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
35  }
36  else // usage == Resource::SHADER_OUTPUT
37  {
38  desc.Usage = D3D11_USAGE_DEFAULT;
39  desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
40  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
41  }
42 
43  if (texture->WantAutogenerateMipmaps())
44  {
45  desc.Usage = D3D11_USAGE_DEFAULT;
46  desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
47  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
48  desc.MiscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
49  }
50 
51  // Create the texture.
52  ID3D11Texture3D* dxTexture = nullptr;
53  HRESULT hr;
54  if (texture->GetData())
55  {
56  unsigned int const numSubresources = texture->GetNumSubresources();
57  std::vector<D3D11_SUBRESOURCE_DATA> data(numSubresources);
58  for (unsigned int index = 0; index < numSubresources; ++index)
59  {
60  auto sr = texture->GetSubresource(index);
61  data[index].pSysMem = sr.data;
62  data[index].SysMemPitch = sr.rowPitch;
63  data[index].SysMemSlicePitch = sr.slicePitch;
64  }
65  hr = device->CreateTexture3D(&desc, &data[0], &dxTexture);
66  }
67  else
68  {
69  hr = device->CreateTexture3D(&desc, nullptr, &dxTexture);
70  }
71  CHECK_HR_RETURN_VOID("Failed to create texture");
72  mDXObject = dxTexture;
73 
74  // Create views of the texture.
75  CreateSRView(device, desc);
76  if (usage == Resource::SHADER_OUTPUT)
77  {
78  CreateUAView(device, desc);
79  }
80 
81  // Create a staging texture if requested.
82  if (texture->GetCopyType() != Resource::COPY_NONE)
83  {
84  CreateStaging(device, desc);
85  }
86 
87  // Generate mipmaps if requested.
88  if (texture->WantAutogenerateMipmaps() && mSRView)
89  {
90  ID3D11DeviceContext* context;
91  device->GetImmediateContext(&context);
92  context->GenerateMips(mSRView);
93  context->Release();
94  }
95 }
96 
97 std::shared_ptr<GEObject> DX11Texture3::Create(void* device, GraphicsObject const* object)
98 {
99  if (object->GetType() == GT_TEXTURE3)
100  {
101  return std::make_shared<DX11Texture3>(reinterpret_cast<ID3D11Device*>(device),
102  static_cast<Texture3 const*>(object));
103  }
104 
105  LogError("Invalid object type.");
106  return nullptr;
107 }
108 
109 void DX11Texture3::CreateStaging(ID3D11Device* device, D3D11_TEXTURE3D_DESC const& tx)
110 {
111  D3D11_TEXTURE3D_DESC desc;
112  desc.Width = tx.Width;
113  desc.Height = tx.Height;
114  desc.Depth = tx.Depth;
115  desc.MipLevels = tx.MipLevels;
116  desc.Format = tx.Format;
117  desc.Usage = D3D11_USAGE_STAGING;
118  desc.BindFlags = D3D11_BIND_NONE;
119  desc.CPUAccessFlags = msStagingAccess[GetTexture()->GetCopyType()];
120  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
121 
122  HRESULT hr = device->CreateTexture3D(&desc, nullptr,
123  reinterpret_cast<ID3D11Texture3D**>(&mStaging));
124  CHECK_HR_RETURN_NONE("Failed to create staging texture");
125 }
126 
127 void DX11Texture3::CreateSRView(ID3D11Device* device, D3D11_TEXTURE3D_DESC const& tx)
128 {
129  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
130  desc.Format = tx.Format;
131  desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
132  desc.Texture3D.MostDetailedMip = 0;
133  desc.Texture3D.MipLevels = tx.MipLevels;
134  HRESULT hr = device->CreateShaderResourceView(GetDXTexture(), &desc, &mSRView);
135  CHECK_HR_RETURN_NONE("Failed to create shader resource view");
136 }
137 
138 void DX11Texture3::CreateUAView(ID3D11Device* device, D3D11_TEXTURE3D_DESC const& tx)
139 {
140  D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
141  desc.Format = tx.Format;
142  desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D;
143  desc.Texture3D.MipSlice = 0;
144  desc.Texture3D.FirstWSlice = 0;
145  desc.Texture3D.WSize = tx.Depth;
146  HRESULT hr = device->CreateUnorderedAccessView(GetDXTexture(), &desc, &mUAView);
147  CHECK_HR_RETURN_NONE("Failed to create unordered access view");
148 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
#define D3D11_RESOURCE_MISC_NONE
void CreateSRView(ID3D11Device *device, D3D11_TEXTURE3D_DESC const &tx)
#define CHECK_HR_RETURN_NONE(msg)
IMMUTABLE
Definition: GteResource.h:42
Usage GetUsage() const
Definition: GteResource.h:126
#define D3D11_BIND_NONE
GraphicsObjectType GetType() const
ID3D11Texture3D * GetDXTexture() const
ID3D11UnorderedAccessView * mUAView
ID3D11Resource * mStaging
unsigned int GetHeight() const
Definition: GteTexture3.cpp:27
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
void CreateStaging(ID3D11Device *device, D3D11_TEXTURE3D_DESC const &tx)
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
GLuint texture
Definition: glcorearb.h:410
ID3D11DeviceChild * mDXObject
void CreateUAView(ID3D11Device *device, D3D11_TEXTURE3D_DESC const &tx)
unsigned int GetThickness() const
Definition: GteTexture3.cpp:32
char const * GetData() const
Definition: GteResource.h:151
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[]
Texture3 * GetTexture() const
CopyType GetCopyType() const
Definition: GteResource.h:136
GT_TEXTURE3
bool WantAutogenerateMipmaps() const
Definition: GteTexture.h:175
ID3D11ShaderResourceView * mSRView
DX11Texture3(ID3D11Device *device, Texture3 const *texture)
unsigned int GetWidth() const
Definition: GteTexture3.cpp:22
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