GteDX11Buffer.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>
9 #include <LowLevel/GteWrapper.h>
11 using namespace gte;
12 
14  :
15  DX11Resource(buffer),
16  mUpdateMapMode(D3D11_MAP_WRITE_DISCARD)
17 {
18 }
19 
20 bool DX11Buffer::Update(ID3D11DeviceContext* context)
21 {
22  Buffer* buffer = GetBuffer();
23  if (buffer->GetUsage() != Resource::DYNAMIC_UPDATE)
24  {
25  LogWarning("Buffer usage is not DYNAMIC_UPDATE.");
26  return false;
27  }
28 
29  UINT numActiveBytes = buffer->GetNumActiveBytes();
30  if (numActiveBytes > 0)
31  {
32  // Map the buffer.
33  ID3D11Buffer* dxBuffer = GetDXBuffer();
34  D3D11_MAPPED_SUBRESOURCE sub;
35  HRESULT hr = context->Map(dxBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sub);
36  CHECK_HR_RETURN("Failed to map buffer", false);
37 
38  // Copy from CPU memory.
39  if (mUpdateMapMode != D3D11_MAP_WRITE_DISCARD)
40  {
41  unsigned int offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
42  char const* source = buffer->GetData() + offsetInBytes;
43  char* target = (char*)sub.pData + offsetInBytes;
44  Memcpy(target, source, numActiveBytes);
45  }
46  else
47  {
48  Memcpy(sub.pData, buffer->GetData(), buffer->GetNumBytes());
49  }
50  context->Unmap(dxBuffer, 0);
51  }
52  else
53  {
54  LogInformation("Buffer has zero active bytes.");
55  }
56  return true;
57 }
58 
59 bool DX11Buffer::CopyCpuToGpu(ID3D11DeviceContext* context)
60 {
61  if (!PreparedForCopy(D3D11_CPU_ACCESS_WRITE))
62  {
63  return false;
64  }
65 
66  Buffer* buffer = GetBuffer();
67  UINT numActiveBytes = buffer->GetNumActiveBytes();
68  if (numActiveBytes > 0)
69  {
70  // Map the staging buffer.
71  D3D11_MAPPED_SUBRESOURCE sub;
72  HRESULT hr = context->Map(mStaging, 0, D3D11_MAP_WRITE, 0, &sub);
73  CHECK_HR_RETURN("Failed to map staging buffer", false);
74 
75  // Copy from CPU memory to staging buffer. For buffers, the
76  // 'box' members are specified in number of bytes. The inputs
77  // 'DstX', 'DstY', and 'DstZ' are also specified in number of bytes.
78  unsigned int offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
79  char const* source = buffer->GetData() + offsetInBytes;
80  char* target = (char*)sub.pData + offsetInBytes;
81  Memcpy(target, source, numActiveBytes);
82  context->Unmap(mStaging, 0);
83 
84  // Copy from staging buffer to GPU memory.
85  //unsigned int offset = buffer->GetOffset();
86  D3D11_BOX box = { offsetInBytes, 0, 0, offsetInBytes + numActiveBytes, 1, 1 };
87  context->CopySubresourceRegion(
88  GetDXBuffer(), 0, offsetInBytes, 0, 0, mStaging, 0, &box);
89  }
90  else
91  {
92  LogInformation("Buffer has zero active bytes.");
93  }
94  return true;
95 }
96 
97 bool DX11Buffer::CopyGpuToCpu(ID3D11DeviceContext* context)
98 {
99  if (!PreparedForCopy(D3D11_CPU_ACCESS_READ))
100  {
101  return false;
102  }
103 
104  Buffer* buffer = GetBuffer();
105  UINT numActiveBytes = buffer->GetNumActiveBytes();
106  if (numActiveBytes > 0)
107  {
108  // Copy from GPU memory to staging buffer.
109  unsigned int offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
110  D3D11_BOX box = { offsetInBytes, 0, 0, offsetInBytes + numActiveBytes, 1, 1 };
111  context->CopySubresourceRegion(
112  mStaging, 0, offsetInBytes, 0, 0, GetDXBuffer(), 0, &box);
113 
114  // Map the staging buffer.
115  D3D11_MAPPED_SUBRESOURCE sub;
116  HRESULT hr = context->Map(mStaging, 0, D3D11_MAP_READ, 0, &sub);
117  CHECK_HR_RETURN("Failed to map staging buffer", false);
118 
119  // Copy from staging buffer to CPU memory.
120  char const* source = (char*)sub.pData + offsetInBytes;
121  char* target = buffer->GetData() + offsetInBytes;
122  Memcpy(target, source, numActiveBytes);
123  context->Unmap(mStaging, 0);
124  }
125  else
126  {
127  LogInformation("Buffer has zero active bytes.");
128  }
129  return true;
130 }
131 
132 void DX11Buffer::CopyGpuToGpu(ID3D11DeviceContext* context, ID3D11Resource* target)
133 {
134  Buffer* buffer = GetBuffer();
135  UINT numActiveBytes = buffer->GetNumActiveBytes();
136  if (numActiveBytes > 0)
137  {
138  // Copy from GPU memory to staging buffer.
139  unsigned int offsetInBytes = buffer->GetOffset() * buffer->GetElementSize();
140  D3D11_BOX box = { offsetInBytes, 0, 0, offsetInBytes + numActiveBytes, 1, 1 };
141  context->CopySubresourceRegion(
142  target, 0, offsetInBytes, 0, 0, GetDXBuffer(), 0, &box);
143  }
144  else
145  {
146  LogInformation("Buffer has zero active bytes.");
147  }
148 }
149 
150 bool DX11Buffer::Update(ID3D11DeviceContext*, unsigned int)
151 {
152  LogError("This was called polymorphically through DX11Resource.");
153  return false;
154 }
155 
156 bool DX11Buffer::CopyCpuToGpu(ID3D11DeviceContext*, unsigned int)
157 {
158  LogError("This was called polymorphically through DX11Resource.");
159  return false;
160 }
161 
162 bool DX11Buffer::CopyGpuToCpu(ID3D11DeviceContext*, unsigned int)
163 {
164  LogError("This was called polymorphically through DX11Resource.");
165  return false;
166 }
167 
168 void DX11Buffer::CopyGpuToGpu(ID3D11DeviceContext*, ID3D11Resource*, unsigned int)
169 {
170  LogError("This was called polymorphically through DX11Resource.");
171 }
172 
173 void DX11Buffer::CreateStaging(ID3D11Device* device,
174  D3D11_BUFFER_DESC const& bf)
175 {
176  D3D11_BUFFER_DESC desc;
177  desc.ByteWidth = bf.ByteWidth;
178  desc.Usage = D3D11_USAGE_STAGING;
179  desc.BindFlags = D3D11_BIND_NONE;
180  desc.CPUAccessFlags = msStagingAccess[GetBuffer()->GetCopyType()];
181  desc.MiscFlags = D3D11_RESOURCE_MISC_NONE;
182  desc.StructureByteStride = 0;
183 
184  HRESULT hr = device->CreateBuffer(&desc, nullptr,
185  reinterpret_cast<ID3D11Buffer**>(&mStaging));
186  CHECK_HR_RETURN_NONE("Failed to create staging buffer");
187 }
188 
DYNAMIC_UPDATE
Definition: GteResource.h:42
virtual void CopyGpuToGpu(ID3D11DeviceContext *context, ID3D11Resource *target) override
virtual bool CopyCpuToGpu(ID3D11DeviceContext *context) override
#define D3D11_RESOURCE_MISC_NONE
DX11Buffer(Buffer const *buffer)
#define CHECK_HR_RETURN_NONE(msg)
unsigned int GetOffset() const
Definition: GteResource.h:173
#define LogInformation(message)
Definition: GteLogger.h:98
ID3D11Buffer * GetDXBuffer() const
Definition: GteDX11Buffer.h:74
Usage GetUsage() const
Definition: GteResource.h:126
#define D3D11_BIND_NONE
GLenum target
Definition: glcorearb.h:1662
ID3D11Resource * mStaging
unsigned int GetElementSize() const
Definition: GteResource.h:111
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:798
#define LogError(message)
Definition: GteLogger.h:92
#define LogWarning(message)
Definition: GteLogger.h:95
unsigned int GetNumBytes() const
Definition: GteResource.h:116
void CreateStaging(ID3D11Device *device, D3D11_BUFFER_DESC const &bf)
Buffer * GetBuffer() const
Definition: GteDX11Buffer.h:69
char const * GetData() const
Definition: GteResource.h:151
bool PreparedForCopy(D3D11_CPU_ACCESS_FLAG access) const
static UINT const msStagingAccess[]
virtual bool Update(ID3D11DeviceContext *context) override
void Memcpy(void *target, void const *source, size_t count)
Definition: GteWrapper.cpp:16
CopyType GetCopyType() const
Definition: GteResource.h:136
typedef UINT(WINAPI *PFNWGLGETGPUIDSAMDPROC)(UINT maxCount
#define CHECK_HR_RETURN(msg, value)
GLuint buffer
Definition: glcorearb.h:655
virtual bool CopyGpuToCpu(ID3D11DeviceContext *context) override
D3D11_MAP mUpdateMapMode
Definition: GteDX11Buffer.h:66
unsigned int GetNumActiveBytes() const
Definition: GteResource.h:183


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