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


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