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