GteDX11TextureBuffer.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 DX11TextureBuffer::DX11TextureBuffer(ID3D11Device* device, TextureBuffer const* tbuffer)
18  :
19  DX11Buffer(tbuffer),
20  mSRView(nullptr)
21 {
22  // Specify the buffer description.
23  D3D11_BUFFER_DESC desc;
24  desc.ByteWidth = tbuffer->GetNumBytes();
25  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
26  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
27  desc.StructureByteStride = 0;
28 
29  Resource::Usage usage = tbuffer->GetUsage();
30  if (usage == Resource::IMMUTABLE)
31  {
32  desc.Usage = D3D11_USAGE_IMMUTABLE;
33  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
34  }
35  else if (usage == Resource::DYNAMIC_UPDATE)
36  {
37  desc.Usage = D3D11_USAGE_DYNAMIC;
38  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
39  }
40  else
41  {
42  desc.Usage = D3D11_USAGE_DEFAULT;
43  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
44  }
45 
46  // Create the buffer.
47  ID3D11Buffer* buffer = nullptr;
48  HRESULT hr;
49  if (tbuffer->GetData())
50  {
51  D3D11_SUBRESOURCE_DATA data;
52  data.pSysMem = tbuffer->GetData();
53  data.SysMemPitch = 0;
54  data.SysMemSlicePitch = 0;
55  hr = device->CreateBuffer(&desc, &data, &buffer);
56  }
57  else
58  {
59  hr = device->CreateBuffer(&desc, nullptr, &buffer);
60  }
61  CHECK_HR_RETURN_NONE("Failed to create texture buffer");
62  mDXObject = buffer;
63 
64  // Create a SRV for the texture to be readable.
65  CreateSRView(device);
66 
67  // Create a staging buffer if requested.
68  if (tbuffer->GetCopyType() != Resource::COPY_NONE)
69  {
70  CreateStaging(device, desc);
71  }
72 }
73 
74 std::shared_ptr<GEObject> DX11TextureBuffer::Create(void* device, GraphicsObject const* object)
75 {
76  if (object->GetType() == GT_TEXTURE_BUFFER)
77  {
78  return std::make_shared<DX11TextureBuffer>(
79  reinterpret_cast<ID3D11Device*>(device),
80  static_cast<TextureBuffer const*>(object));
81  }
82 
83  LogError("Invalid object type.");
84  return nullptr;
85 }
86 
88 {
89  DX11Buffer::SetName(name);
90  HRESULT hr = SetPrivateName(mSRView, name);
91  CHECK_HR_RETURN_NONE("Failed to set private name");
92 }
93 
94 void DX11TextureBuffer::CreateSRView(ID3D11Device* device)
95 {
96  TextureBuffer* tbuffer = GetTextureBuffer();
97  ID3D11Buffer* dxBuffer = GetDXBuffer();
98 
99  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
100  desc.Format = static_cast<DXGI_FORMAT>(tbuffer->GetFormat());
101  desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
102  desc.Buffer.FirstElement = 0;
103  desc.Buffer.NumElements = tbuffer->GetNumElements();
104  HRESULT hr = device->CreateShaderResourceView(dxBuffer, &desc, &mSRView);
105  CHECK_HR_RETURN_NONE("Failed to create shader resource view");
106 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
unsigned int GetNumElements() const
Definition: GteResource.h:106
DX11TextureBuffer(ID3D11Device *device, TextureBuffer const *tbuffer)
#define D3D11_RESOURCE_MISC_NONE
ID3D11ShaderResourceView * mSRView
#define CHECK_HR_RETURN_NONE(msg)
IMMUTABLE
Definition: GteResource.h:42
ID3D11Buffer * GetDXBuffer() const
Definition: GteDX11Buffer.h:74
HRESULT SetPrivateName(ID3D11DeviceChild *object, std::string const &name)
Usage GetUsage() const
Definition: GteResource.h:126
GraphicsObjectType GetType() const
GLuint const GLchar * name
Definition: glcorearb.h:781
DFType GetFormat() const
ULONG FinalRelease(T *&object)
void CreateSRView(ID3D11Device *device)
virtual void SetName(std::string const &name)
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
ID3D11DeviceChild * mDXObject
unsigned int GetNumBytes() const
Definition: GteResource.h:116
void CreateStaging(ID3D11Device *device, D3D11_BUFFER_DESC const &bf)
char const * GetData() const
Definition: GteResource.h:151
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
#define D3D11_CPU_ACCESS_NONE
CopyType GetCopyType() const
Definition: GteResource.h:136
GT_TEXTURE_BUFFER
GLuint buffer
Definition: glcorearb.h:655
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
TextureBuffer * GetTextureBuffer() const
virtual void SetName(std::string const &name) override


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