GteDX11Texture.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 {
17 }
18 
20  :
21  DX11Resource(gtTexture),
22  mSRView(nullptr),
23  mUAView(nullptr)
24 {
25 }
26 
27 bool DX11Texture::Update(ID3D11DeviceContext* context, unsigned int sri)
28 {
30  if (sri >= texture->GetNumSubresources())
31  {
32  LogWarning("Subresource index out of range.");
33  return false;
34  }
35 
36  if (texture->GetUsage() != Resource::DYNAMIC_UPDATE)
37  {
38  LogWarning("Texture usage is not DYNAMIC_UPDATE.");
39  return false;
40  }
41 
42  // Map the texture.
43  ID3D11Resource* dxTexture = GetDXResource();
44  D3D11_MAPPED_SUBRESOURCE sub;
45  HRESULT hr = context->Map(dxTexture, sri, D3D11_MAP_WRITE_DISCARD, 0, &sub);
46  CHECK_HR_RETURN("Failed to map staging texture", false);
47 
48  // Copy from CPU memory.
49  auto sr = texture->GetSubresource(sri);
50  unsigned int numDimensions = texture->GetNumDimensions();
51  if (numDimensions == 1)
52  {
53  Memcpy(sub.pData, sr.data, texture->GetNumBytesFor(sr.level));
54  }
55  else if (numDimensions == 2)
56  {
57  CopyPitched2(texture->GetDimensionFor(sr.level, 1), sr.rowPitch,
58  sr.data, sub.RowPitch, sub.pData);
59  }
60  else // numDimensions == 3
61  {
62  CopyPitched3(texture->GetDimensionFor(sr.level, 1),
63  texture->GetDimensionFor(sr.level, 2), sr.rowPitch, sr.slicePitch,
64  sr.data, sub.RowPitch, sub.DepthPitch, sub.pData);
65  }
66  context->Unmap(dxTexture, sri);
67  return true;
68 }
69 
70 bool DX11Texture::Update(ID3D11DeviceContext* context)
71 {
73  unsigned int const numSubresources = texture->GetNumSubresources();
74  for (unsigned int index = 0; index < numSubresources; ++index)
75  {
76  if (!Update(context, index))
77  {
78  return false;
79  }
80  }
81  return true;
82 }
83 
84 bool DX11Texture::CopyCpuToGpu(ID3D11DeviceContext* context,
85  unsigned int sri)
86 {
88  if (sri >= texture->GetNumSubresources())
89  {
90  LogWarning("Subresource index out of range.");
91  return false;
92  }
93 
94  if (!PreparedForCopy(D3D11_CPU_ACCESS_WRITE))
95  {
96  return false;
97  }
98 
99  // Map the staging texture.
100  D3D11_MAPPED_SUBRESOURCE sub;
101  HRESULT hr = context->Map(mStaging, sri, D3D11_MAP_WRITE, 0, &sub);
102  CHECK_HR_RETURN("Failed to map staging texture", false);
103 
104  // Copy from CPU memory to staging texture.
105  auto sr = texture->GetSubresource(sri);
106  unsigned int numDimensions = texture->GetNumDimensions();
107  if (numDimensions == 1)
108  {
109  Memcpy(sub.pData, sr.data, texture->GetNumBytesFor(sr.level));
110  }
111  else if (numDimensions == 2)
112  {
113  CopyPitched2(texture->GetDimensionFor(sr.level, 1), sr.rowPitch,
114  sr.data, sub.RowPitch, sub.pData);
115  }
116  else // numDimensions == 3
117  {
118  CopyPitched3(texture->GetDimensionFor(sr.level, 1),
119  texture->GetDimensionFor(sr.level, 2), sr.rowPitch,
120  sr.slicePitch, sr.data, sub.RowPitch, sub.DepthPitch,
121  sub.pData);
122  }
123  context->Unmap(mStaging, sri);
124 
125  // Copy from staging texture to GPU memory.
126  ID3D11Resource* dxTexture = GetDXResource();
127  context->CopySubresourceRegion(dxTexture, sri, 0, 0, 0, mStaging, sri, nullptr);
128  return true;
129 }
130 
131 bool DX11Texture::CopyCpuToGpu(ID3D11DeviceContext* context)
132 {
134  unsigned int const numSubresources = texture->GetNumSubresources();
135  for (unsigned int index = 0; index < numSubresources; ++index)
136  {
137  if (!CopyCpuToGpu(context, index))
138  {
139  return false;
140  }
141  }
142 
143  // Generate mipmaps (when they exist).
144  if (texture->WantAutogenerateMipmaps() && mSRView)
145  {
146  context->GenerateMips(mSRView);
147  }
148  return true;
149 }
150 
151 bool DX11Texture::CopyGpuToCpu(ID3D11DeviceContext* context, unsigned int sri)
152 {
154  if (sri >= texture->GetNumSubresources())
155  {
156  LogWarning("Subresource index out of range.");
157  return false;
158  }
159 
160  if (!PreparedForCopy(D3D11_CPU_ACCESS_READ))
161  {
162  return false;
163  }
164 
165  // Copy from GPU memory to staging texture.
166  ID3D11Resource* dxTexture = GetDXResource();
167  context->CopySubresourceRegion(mStaging, sri, 0, 0, 0, dxTexture, sri, nullptr);
168 
169  // Map the staging texture.
170  D3D11_MAPPED_SUBRESOURCE sub;
171  HRESULT hr = context->Map(mStaging, sri, D3D11_MAP_READ, 0, &sub);
172  CHECK_HR_RETURN("Failed to map staging texture", false);
173 
174  // Copy from staging texture to CPU memory.
175  auto sr = texture->GetSubresource(sri);
176  unsigned int numDimensions = texture->GetNumDimensions();
177  if (numDimensions == 1)
178  {
179  Memcpy(sr.data, sub.pData, texture->GetNumBytesFor(sr.level));
180  }
181  else if (numDimensions == 2)
182  {
183  CopyPitched2(texture->GetDimensionFor(sr.level, 1), sub.RowPitch,
184  sub.pData, sr.rowPitch, sr.data);
185  }
186  else // numDimensions == 3
187  {
188  CopyPitched3(texture->GetDimensionFor(sr.level, 1),
189  texture->GetDimensionFor(sr.level, 2), sub.RowPitch,
190  sub.DepthPitch, sub.pData, sr.rowPitch, sr.slicePitch, sr.data);
191  }
192  context->Unmap(mStaging, sri);
193  return true;
194 }
195 
196 bool DX11Texture::CopyGpuToCpu(ID3D11DeviceContext* context)
197 {
199  unsigned int const numSubresources = texture->GetNumSubresources();
200  for (unsigned int index = 0; index < numSubresources; ++index)
201  {
202  if (!CopyGpuToCpu(context, index))
203  {
204  return false;
205  }
206  }
207  return true;
208 }
209 
210 void DX11Texture::CopyGpuToGpu(ID3D11DeviceContext* context,
211  ID3D11Resource* target, unsigned int sri)
212 {
214  if (sri >= texture->GetNumSubresources())
215  {
216  LogWarning("Subresource index out of range.");
217  return;
218  }
219 
220  // Copy from GPU memory to staging texture.
221  ID3D11Resource* dxTexture = GetDXResource();
222  context->CopySubresourceRegion(target, sri, 0, 0, 0, dxTexture, sri, nullptr);
223 }
224 
225 void DX11Texture::CopyGpuToGpu(ID3D11DeviceContext* context, ID3D11Resource* target)
226 {
228  unsigned int const numSubresources = texture->GetNumSubresources();
229  for (unsigned int index = 0; index < numSubresources; ++index)
230  {
231  CopyGpuToGpu(context, target, index);
232  }
233 }
234 
236 {
237  DX11Resource::SetName(name);
238  HRESULT hr = SetPrivateName(mSRView, name);
239  CHECK_HR_RETURN_NONE("Failed to set private name");
240  hr = SetPrivateName(mUAView, name);
241  CHECK_HR_RETURN_NONE("Failed to set private name");
242 }
243 
244 void DX11Texture::CopyPitched2(unsigned int numRows,
245  unsigned int srcRowPitch, void const* srcData, unsigned int trgRowPitch,
246  void* trgData)
247 {
248  if (srcRowPitch == trgRowPitch)
249  {
250  // The memory is contiguous.
251  Memcpy(trgData, srcData, trgRowPitch*numRows);
252  }
253  else
254  {
255  // Padding was added to each row of the texture, so we must
256  // copy a row at a time to compensate for differing pitches.
257  unsigned int numRowBytes = std::min(srcRowPitch, trgRowPitch);
258  char const* srcRow = static_cast<char const*>(srcData);
259  char* trgRow = static_cast<char*>(trgData);
260  for (unsigned int row = 0; row < numRows; ++row)
261  {
262  Memcpy(trgRow, srcRow, numRowBytes);
263  srcRow += srcRowPitch;
264  trgRow += trgRowPitch;
265  }
266  }
267 }
268 
269 void DX11Texture::CopyPitched3(unsigned int numRows,
270  unsigned int numSlices, unsigned int srcRowPitch,
271  unsigned int srcSlicePitch, void const* srcData, unsigned int trgRowPitch,
272  unsigned int trgSlicePitch, void* trgData)
273 {
274  if (srcRowPitch == trgRowPitch && srcSlicePitch == trgSlicePitch)
275  {
276  // The memory is contiguous.
277  Memcpy(trgData, srcData, trgSlicePitch*numSlices);
278  }
279  else
280  {
281  // Padding was added to each row and/or slice of the texture, so
282  // we must copy the data to compensate for differing pitches.
283  unsigned int numRowBytes = std::min(srcRowPitch, trgRowPitch);
284  char const* srcSlice = static_cast<char const*>(srcData);
285  char* trgSlice = static_cast<char*>(trgData);
286  for (unsigned int slice = 0; slice < numSlices; ++slice)
287  {
288  char const* srcRow = srcSlice;
289  char* trgRow = trgSlice;
290  for (unsigned int row = 0; row < numRows; ++row)
291  {
292  Memcpy(trgRow, srcRow, numRowBytes);
293  srcRow += srcRowPitch;
294  trgRow += trgRowPitch;
295  }
296  srcSlice += srcSlicePitch;
297  trgSlice += trgSlicePitch;
298  }
299  }
300 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
virtual void SetName(std::string const &name) override
DX11Texture(Texture const *gtTexture)
#define CHECK_HR_RETURN_NONE(msg)
virtual bool CopyGpuToCpu(ID3D11DeviceContext *context, unsigned int sri) override
static void CopyPitched3(unsigned int numRows, unsigned int numSlices, unsigned int srcRowPitch, unsigned int srcSlicePitch, void const *srcData, unsigned int trgRowPitch, unsigned int trgSlicePitch, void *trgData)
HRESULT SetPrivateName(ID3D11DeviceChild *object, std::string const &name)
Usage GetUsage() const
Definition: GteResource.h:126
GLuint const GLchar * name
Definition: glcorearb.h:781
ID3D11UnorderedAccessView * mUAView
GLenum target
Definition: glcorearb.h:1662
ULONG FinalRelease(T *&object)
unsigned int GetNumDimensions() const
Definition: GteTexture.h:105
ID3D11Resource * mStaging
virtual bool CopyCpuToGpu(ID3D11DeviceContext *context, unsigned int sri) override
GLenum GLenum GLsizei void * row
Definition: glext.h:2725
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
virtual void CopyGpuToGpu(ID3D11DeviceContext *context, ID3D11Resource *target, unsigned int sri) override
static void CopyPitched2(unsigned int numRows, unsigned int srcRowPitch, void const *srcData, unsigned int trgRowPitch, void *trgData)
virtual bool Update(ID3D11DeviceContext *context, unsigned int sri) override
GLuint texture
Definition: glcorearb.h:410
#define LogWarning(message)
Definition: GteLogger.h:95
unsigned int GetNumBytesFor(unsigned int level) const
Definition: GteTexture.h:136
unsigned int GetDimensionFor(unsigned int level, int i) const
Definition: GteTexture.h:125
bool PreparedForCopy(D3D11_CPU_ACCESS_FLAG access) const
Subresource GetSubresource(unsigned int index) const
Definition: GteTexture.cpp:110
ID3D11Resource * GetDXResource() const
GLuint index
Definition: glcorearb.h:781
void Memcpy(void *target, void const *source, size_t count)
Definition: GteWrapper.cpp:16
virtual ~DX11Texture()
#define CHECK_HR_RETURN(msg, value)
Texture * GetTexture() const
bool WantAutogenerateMipmaps() const
Definition: GteTexture.h:175
ID3D11ShaderResourceView * mSRView
unsigned int GetNumSubresources() const
Definition: GteTexture.h:170
virtual void SetName(std::string const &name) override


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