GteDX11TextureRT.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 
13 {
15 }
16 
17 DX11TextureRT::DX11TextureRT(ID3D11Device* device, TextureRT const* texture)
18  :
19  DX11Texture2(texture),
20  mRTView(nullptr)
21 {
22  // Specify the texture description.
23  D3D11_TEXTURE2D_DESC desc;
24  desc.Width = texture->GetWidth();
25  desc.Height = texture->GetHeight();
26  desc.MipLevels = texture->GetNumLevels();
27  desc.ArraySize = 1;
28  desc.Format = static_cast<DXGI_FORMAT>(texture->GetFormat());
29  desc.SampleDesc.Count = 1;
30  desc.SampleDesc.Quality = 0;
31  desc.Usage = D3D11_USAGE_DEFAULT;
32  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
33  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
34  desc.MiscFlags = (texture->IsShared() ?
35  D3D11_RESOURCE_MISC_SHARED : D3D11_RESOURCE_MISC_NONE);
36 
37  if (texture->GetUsage() == Resource::SHADER_OUTPUT)
38  {
39  desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
40  }
41 
42  if (texture->WantAutogenerateMipmaps() && !texture->IsShared())
43  {
44  desc.MiscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
45  }
46 
47  // Create the texture.
48  ID3D11Texture2D* dxTexture = nullptr;
49  HRESULT hr;
50  if (texture->GetData())
51  {
52  unsigned int const numSubresources = texture->GetNumSubresources();
53  std::vector<D3D11_SUBRESOURCE_DATA> data(numSubresources);
54  for (unsigned int index = 0; index < numSubresources; ++index)
55  {
56  auto sr = texture->GetSubresource(index);
57  data[index].pSysMem = sr.data;
58  data[index].SysMemPitch = sr.rowPitch;
59  data[index].SysMemSlicePitch = 0;
60  }
61  hr = device->CreateTexture2D(&desc, &data[0], &dxTexture);
62  }
63  else
64  {
65  hr = device->CreateTexture2D(&desc, nullptr, &dxTexture);
66  }
67  CHECK_HR_RETURN_VOID("Failed to map create texture");
68  mDXObject = dxTexture;
69 
70  // Create views of the texture.
71  CreateSRView(device, desc);
72  CreateRTView(device, desc);
73  if (texture->GetUsage() == Resource::SHADER_OUTPUT)
74  {
75  CreateUAView(device, desc);
76  }
77 
78  // Create a staging texture if requested.
79  if (texture->GetCopyType() != Resource::COPY_NONE)
80  {
81  CreateStaging(device, desc);
82  }
83 
84  // Generate mipmaps if requested.
85  if (texture->WantAutogenerateMipmaps() && mSRView)
86  {
87  ID3D11DeviceContext* context;
88  device->GetImmediateContext(&context);
89  context->GenerateMips(mSRView);
90  context->Release();
91  }
92 }
93 
94 DX11TextureRT::DX11TextureRT(ID3D11Device* device, DX11TextureRT const* dxSharedTexture)
95  :
96  DX11Texture2(dxSharedTexture->GetTexture()),
97  mRTView(nullptr)
98 {
99  ID3D11Texture2D* dxShared = dxSharedTexture->CreateSharedDXObject(device);
100  mDXObject = dxShared;
101  D3D11_TEXTURE2D_DESC desc;
102  dxShared->GetDesc(&desc);
103  CreateRTView(device, desc);
104 }
105 
106 std::shared_ptr<GEObject> DX11TextureRT::Create(void* device, GraphicsObject const* object)
107 {
108  if (object->GetType() == GT_TEXTURE_RT)
109  {
110  return std::make_shared<DX11TextureRT>(
111  reinterpret_cast<ID3D11Device*>(device),
112  static_cast<TextureRT const*>(object));
113  }
114 
115  LogError("Invalid object type.");
116  return nullptr;
117 }
118 
120 {
121  DX11Texture2::SetName(name);
122  HRESULT hr = SetPrivateName(mRTView, name);
123  CHECK_HR_RETURN_NONE("Failed to set private name");
124 }
125 
126 void DX11TextureRT::CreateRTView(ID3D11Device* device, D3D11_TEXTURE2D_DESC const& tx)
127 {
128  D3D11_RENDER_TARGET_VIEW_DESC desc;
129  desc.Format = tx.Format;
130  desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
131  desc.Texture2D.MipSlice = 0;
132 
133  HRESULT hr = device->CreateRenderTargetView(GetDXTexture(), &desc, &mRTView);
134  CHECK_HR_RETURN_NONE("Failed to create render-target view");
135 }
bool IsShared() const
Definition: GteTexture2.cpp:39
COPY_NONE
Definition: GteResource.h:55
virtual void SetName(std::string const &name) override
unsigned int GetWidth() const
Definition: GteTexture2.cpp:22
void CreateRTView(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
#define D3D11_RESOURCE_MISC_NONE
DX11TextureRT(ID3D11Device *device, TextureRT const *texture)
#define CHECK_HR_RETURN_NONE(msg)
void CreateSRView(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
HRESULT SetPrivateName(ID3D11DeviceChild *object, std::string const &name)
Usage GetUsage() const
Definition: GteResource.h:126
GT_TEXTURE_RT
Texture2 * GetTexture() const
GraphicsObjectType GetType() const
GLuint const GLchar * name
Definition: glcorearb.h:781
ULONG FinalRelease(T *&object)
ID3D11RenderTargetView * mRTView
void CreateUAView(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
ID3D11Texture2D * GetDXTexture() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
virtual void SetName(std::string const &name)
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
unsigned int GetHeight() const
Definition: GteTexture2.cpp:27
GLuint texture
Definition: glcorearb.h:410
ID3D11DeviceChild * mDXObject
char const * GetData() const
Definition: GteResource.h:151
void CreateStaging(ID3D11Device *device, D3D11_TEXTURE2D_DESC const &tx)
unsigned int GetNumLevels() const
Definition: GteTexture.h:120
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
CopyType GetCopyType() const
Definition: GteResource.h:136
ID3D11Texture2D * CreateSharedDXObject(ID3D11Device *device) 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


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