GteDX11TextureCubeArray.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 DX11TextureCubeArray::DX11TextureCubeArray(ID3D11Device* device, TextureCubeArray const* textureCubeArray)
13  :
14  DX11TextureArray(textureCubeArray)
15 {
16  // Specify the texture description. TODO: Support texture cube RTs?
17  D3D11_TEXTURE2D_DESC desc;
18  desc.Width = textureCubeArray->GetLength();
19  desc.Height = textureCubeArray->GetLength();
20  desc.MipLevels = textureCubeArray->GetNumLevels();
21  desc.ArraySize = textureCubeArray->GetNumItems();
22  desc.Format = static_cast<DXGI_FORMAT>(textureCubeArray->GetFormat());
23  desc.SampleDesc.Count = 1;
24  desc.SampleDesc.Quality = 0;
25  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
26  desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
27  Resource::Usage usage = textureCubeArray->GetUsage();
28  if (usage == Resource::IMMUTABLE)
29  {
30  desc.Usage = D3D11_USAGE_IMMUTABLE;
31  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
32  }
33  else if (usage == Resource::DYNAMIC_UPDATE)
34  {
35  // DX11 does not allow a cube map to be a dynamic-update resource.
36  desc.Usage = D3D11_USAGE_DEFAULT;
37  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
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 (textureCubeArray->WantAutogenerateMipmaps())
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 (textureCubeArray->GetData())
58  {
59  unsigned int const numSubresources = textureCubeArray->GetNumSubresources();
60  std::vector<D3D11_SUBRESOURCE_DATA> data(numSubresources);
61  for (unsigned int index = 0; index < numSubresources; ++index)
62  {
63  auto sr = textureCubeArray->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_NONE("Failed to create texture array");
75  mDXObject = dxTexture;
76 
77  // Create views of the texture.
78  CreateSRView(device, desc);
79  if (textureCubeArray->GetUsage() == Resource::SHADER_OUTPUT)
80  {
81  CreateUAView(device, desc);
82  }
83 
84  // Create a staging texture if requested.
85  if (textureCubeArray->GetCopyType() != Resource::COPY_NONE)
86  {
87  CreateStaging(device, desc);
88  }
89 
90  // Generate mipmaps if requested.
91  if (textureCubeArray->WantAutogenerateMipmaps() && mSRView)
92  {
93  ID3D11DeviceContext* context;
94  device->GetImmediateContext(&context);
95  context->GenerateMips(mSRView);
96  context->Release();
97  }
98 }
99 
100 std::shared_ptr<GEObject> DX11TextureCubeArray::Create(void* device, GraphicsObject const* object)
101 {
102  if (object->GetType() == GT_TEXTURE_CUBE_ARRAY)
103  {
104  return std::make_shared<DX11TextureCubeArray>(
105  reinterpret_cast<ID3D11Device*>(device),
106  static_cast<TextureCubeArray const*>(object));
107  }
108 
109  LogError("Invalid object type.");
110  return nullptr;
111 }
112 
113 void DX11TextureCubeArray::CreateStaging(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
114 {
115  D3D11_TEXTURE2D_DESC desc;
116  desc.Width = tx.Width;
117  desc.Height = tx.Height;
118  desc.MipLevels = tx.MipLevels;
119  desc.ArraySize = tx.ArraySize;
120  desc.Format = tx.Format;
121  desc.SampleDesc.Count = tx.SampleDesc.Count;
122  desc.SampleDesc.Quality = tx.SampleDesc.Quality;
123  desc.Usage = D3D11_USAGE_STAGING;
124  desc.BindFlags = D3D11_BIND_NONE;
125  desc.CPUAccessFlags = msStagingAccess[GetTextureArray()->GetCopyType()];
126  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
127 
128  HRESULT hr = device->CreateTexture2D(&desc, nullptr,
129  reinterpret_cast<ID3D11Texture2D**>(&mStaging));
130  CHECK_HR_RETURN_NONE("Failed to create staging texture");
131 }
132 
133 void DX11TextureCubeArray::CreateSRView(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
134 {
135  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
136  desc.Format = tx.Format;
137  desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY;
138  desc.TextureCubeArray.MostDetailedMip = 0;
139  desc.TextureCubeArray.MipLevels = tx.MipLevels;
140  desc.TextureCubeArray.First2DArrayFace = 0;
141  desc.TextureCubeArray.NumCubes = tx.ArraySize / 6;
142  HRESULT hr = device->CreateShaderResourceView(GetDXTextureArray(), &desc, &mSRView);
143  CHECK_HR_RETURN_NONE("Failed to create shader resource view");
144 }
145 
146 void DX11TextureCubeArray::CreateUAView(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
147 {
148  D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
149  desc.Format = tx.Format;
150  desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY;
151  desc.Texture2DArray.MipSlice = 0;
152  desc.Texture2DArray.FirstArraySlice = 0;
153  desc.Texture2DArray.ArraySize = tx.ArraySize;
154 
155  HRESULT hr = device->CreateUnorderedAccessView(GetDXTextureArray(), &desc, &mUAView);
156  CHECK_HR_RETURN_NONE("Failed to create unordered access view");
157 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
ID3D11Texture2D * GetDXTextureArray() const
#define D3D11_RESOURCE_MISC_NONE
void CreateUAView(ID3D11Device *device, D3D11_TEXTURE2D_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
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
ID3D11UnorderedAccessView * mUAView
ID3D11Resource * mStaging
#define LogError(message)
Definition: GteLogger.h:92
GT_TEXTURE_CUBE_ARRAY
GLboolean * data
Definition: glcorearb.h:126
ID3D11DeviceChild * mDXObject
char const * GetData() const
Definition: GteResource.h:151
unsigned int GetLength() const
unsigned int GetNumItems() const
Definition: GteTexture.h:95
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
void CreateStaging(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
GLuint index
Definition: glcorearb.h:781
static UINT const msStagingAccess[]
CopyType GetCopyType() const
Definition: GteResource.h:136
TextureCubeArray * GetTextureArray() const
void CreateSRView(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
bool WantAutogenerateMipmaps() const
Definition: GteTexture.h:175
ID3D11ShaderResourceView * mSRView
DX11TextureCubeArray(ID3D11Device *device, TextureCubeArray const *textureCubeArray)
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