GteDX11RawBuffer.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 {
16 }
17 
18 DX11RawBuffer::DX11RawBuffer(ID3D11Device* device, RawBuffer const* rbuffer)
19  :
20  DX11Buffer(rbuffer),
21  mSRView(nullptr),
22  mUAView(nullptr)
23 {
24  // Specify the buffer description.
25  D3D11_BUFFER_DESC desc;
26  desc.ByteWidth = rbuffer->GetNumBytes();
27  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
28  desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
29  desc.StructureByteStride = 0;
30  Resource::Usage usage = rbuffer->GetUsage();
31  if (usage == Resource::IMMUTABLE)
32  {
33  desc.Usage = D3D11_USAGE_IMMUTABLE;
34  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
35  }
36  else if (usage == Resource::DYNAMIC_UPDATE)
37  {
38  desc.Usage = D3D11_USAGE_DYNAMIC;
39  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
40  }
41  else // usage == Resource::SHADER_OUTPUT
42  {
43  desc.Usage = D3D11_USAGE_DEFAULT;
44  desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
45  desc.CPUAccessFlags = D3D11_CPU_ACCESS_NONE;
46  }
47 
48  // Create the buffer.
49  ID3D11Buffer* buffer = nullptr;
50  HRESULT hr;
51  if (rbuffer->GetData())
52  {
53  D3D11_SUBRESOURCE_DATA data;
54  data.pSysMem = rbuffer->GetData();
55  data.SysMemPitch = 0;
56  data.SysMemSlicePitch = 0;
57  hr = device->CreateBuffer(&desc, &data, &buffer);
58  }
59  else
60  {
61  hr = device->CreateBuffer(&desc, nullptr, &buffer);
62  }
63  CHECK_HR_RETURN_NONE("Failed to create byte-address buffer");
64  mDXObject = buffer;
65 
66  // Create views of the buffer.
67  CreateSRView(device);
68  if (usage == Resource::SHADER_OUTPUT)
69  {
70  CreateUAView(device);
71  }
72 
73  // Create a staging buffer if requested.
74  if (rbuffer->GetCopyType() != Resource::COPY_NONE)
75  {
76  CreateStaging(device, desc);
77  }
78 }
79 
80 std::shared_ptr<GEObject> DX11RawBuffer::Create(void* device, GraphicsObject const* object)
81 {
82  if (object->GetType() == GT_RAW_BUFFER)
83  {
84  return std::make_shared<DX11RawBuffer>(
85  reinterpret_cast<ID3D11Device*>(device),
86  static_cast<RawBuffer const*>(object));
87  }
88 
89  LogError("Invalid object type.");
90  return nullptr;
91 }
92 
94 {
95  DX11Buffer::SetName(name);
96  HRESULT hr = SetPrivateName(mSRView, name);
97  CHECK_HR_RETURN_NONE("Failed to set private name");
98  hr = SetPrivateName(mUAView, name);
99  CHECK_HR_RETURN_NONE("Failed to set private name");
100 }
101 
102 void DX11RawBuffer::CreateSRView(ID3D11Device* device)
103 {
105  ID3D11Buffer* dxBuffer = GetDXBuffer();
106 
107  D3D11_SHADER_RESOURCE_VIEW_DESC desc;
108  desc.Format = DXGI_FORMAT_R32_TYPELESS;
109  desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
110  desc.BufferEx.FirstElement = 0;
111  desc.BufferEx.NumElements = buffer->GetNumElements();
112  desc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW;
113  HRESULT hr = device->CreateShaderResourceView(dxBuffer, &desc, &mSRView);
114  CHECK_HR_RETURN_NONE("Failed to shader resource view");
115 }
116 
117 void DX11RawBuffer::CreateUAView(ID3D11Device* device)
118 {
120  ID3D11Buffer* dxBuffer = GetDXBuffer();
121 
122  D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
123  desc.Format = DXGI_FORMAT_R32_TYPELESS;
124  desc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
125  desc.Buffer.FirstElement = 0;
126  desc.Buffer.NumElements = buffer->GetNumElements();
127  desc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
128  HRESULT hr = device->CreateUnorderedAccessView(dxBuffer, &desc, &mUAView);
129  CHECK_HR_RETURN_NONE("Failed to unordered access view");
130 }
131 
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
unsigned int GetNumElements() const
Definition: GteResource.h:106
static std::shared_ptr< GEObject > Create(void *device, GraphicsObject const *object)
#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)
void CreateSRView(ID3D11Device *device)
Usage GetUsage() const
Definition: GteResource.h:126
ID3D11UnorderedAccessView * mUAView
ID3D11ShaderResourceView * mSRView
GraphicsObjectType GetType() const
GLuint const GLchar * name
Definition: glcorearb.h:781
ULONG FinalRelease(T *&object)
void CreateUAView(ID3D11Device *device)
RawBuffer * GetRawBuffer() const
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
virtual void SetName(std::string const &name)
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
#define D3D11_CPU_ACCESS_NONE
DX11RawBuffer(ID3D11Device *device, RawBuffer const *rbuffer)
CopyType GetCopyType() const
Definition: GteResource.h:136
GLuint buffer
Definition: glcorearb.h:655
virtual void SetName(std::string const &name) override
GT_RAW_BUFFER


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