GteDX11Texture1.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 DX11Texture1::DX11Texture1(ID3D11Device* device, Texture1 const* texture)
13  :
14  DX11TextureSingle(texture)
15 {
16  // Specify the texture description.
17  D3D11_TEXTURE1D_DESC desc;
18  desc.Width = texture->GetLength();
19  desc.MipLevels = texture->GetNumLevels();
20  desc.ArraySize = 1;
21  desc.Format = static_cast<DXGI_FORMAT>(texture->GetFormat());
22  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
23  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
24  Resource::Usage usage = texture->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 (texture->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 (texture->GetData())
54  {
55  unsigned int const numSubresources = texture->GetNumSubresources();
56  std::vector<D3D11_SUBRESOURCE_DATA> data(numSubresources);
57  for (unsigned int index = 0; index < numSubresources; ++index)
58  {
59  auto sr = texture->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_VOID("Failed to create texture");
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 (texture->GetCopyType() != Resource::COPY_NONE)
82  {
83  CreateStaging(device, desc);
84  }
85 
86  // Generate mipmaps if requested.
87  if (texture->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> DX11Texture1::Create(void* device, GraphicsObject const* object)
97 {
98  if (object->GetType() == GT_TEXTURE1)
99  {
100  return std::make_shared<DX11Texture1>(
101  reinterpret_cast<ID3D11Device*>(device),
102  static_cast<Texture1 const*>(object));
103  }
104 
105  LogError("Invalid object type.");
106  return nullptr;
107 }
108 
109 void DX11Texture1::CreateStaging(ID3D11Device* device, D3D11_TEXTURE1D_DESC const& tx)
110 {
111  D3D11_TEXTURE1D_DESC desc;
112  desc.Width = tx.Width;
113  desc.MipLevels = tx.MipLevels;
114  desc.ArraySize = tx.ArraySize;
115  desc.Format = tx.Format;
116  desc.Usage = D3D11_USAGE_STAGING;
117  desc.BindFlags = D3D11_BIND_NONE;
118  desc.CPUAccessFlags = msStagingAccess[GetTexture()->GetCopyType()];
119  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
120 
121  HRESULT hr = device->CreateTexture1D(&desc, nullptr,
122  reinterpret_cast<ID3D11Texture1D**>(&mStaging));
123  CHECK_HR_RETURN_NONE("Failed to create staging texture");
124 }
125 
126 void DX11Texture1::CreateSRView(ID3D11Device* device, D3D11_TEXTURE1D_DESC const& tx)
127 {
128  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
129  desc.Format = tx.Format;
130  desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
131  desc.Texture1D.MostDetailedMip = 0;
132  desc.Texture1D.MipLevels = tx.MipLevels;
133  HRESULT hr = device->CreateShaderResourceView(GetDXTexture(), &desc, &mSRView);
134  CHECK_HR_RETURN_NONE("Failed to create shader resource view");
135 }
136 
137 void DX11Texture1::CreateUAView(ID3D11Device* device, D3D11_TEXTURE1D_DESC const& tx)
138 {
139  D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
140  desc.Format = tx.Format;
141  desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE1D;
142  desc.Texture1D.MipSlice = 0;
143  HRESULT hr = device->CreateUnorderedAccessView(GetDXTexture(), &desc, &mUAView);
144  CHECK_HR_RETURN_NONE("Failed to create unordered access view");
145 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
Texture1 * GetTexture() const
#define D3D11_RESOURCE_MISC_NONE
#define CHECK_HR_RETURN_NONE(msg)
IMMUTABLE
Definition: GteResource.h:42
unsigned int GetLength() const
Definition: GteTexture1.cpp:21
Usage GetUsage() const
Definition: GteResource.h:126
void CreateSRView(ID3D11Device *device, D3D11_TEXTURE1D_DESC const &tx)
#define D3D11_BIND_NONE
GraphicsObjectType GetType() const
ID3D11UnorderedAccessView * mUAView
ID3D11Resource * mStaging
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
void CreateUAView(ID3D11Device *device, D3D11_TEXTURE1D_DESC const &tx)
GLuint texture
Definition: glcorearb.h:410
ID3D11DeviceChild * mDXObject
char const * GetData() const
Definition: GteResource.h:151
void CreateStaging(ID3D11Device *device, D3D11_TEXTURE1D_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
ID3D11Texture1D * GetDXTexture() const
bool WantAutogenerateMipmaps() const
Definition: GteTexture.h:175
ID3D11ShaderResourceView * mSRView
GT_TEXTURE1
DX11Texture1(ID3D11Device *device, Texture1 const *texture)
unsigned int GetNumSubresources() const
Definition: GteTexture.h:170
DFType GetFormat() const
Definition: GteTexture.h:100
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)


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