• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // Buffer11.cpp Defines the Buffer11 class.
8 
9 #include "libANGLE/renderer/d3d/d3d11/Buffer11.h"
10 
11 #include <memory>
12 
13 #include "common/MemoryBuffer.h"
14 #include "libANGLE/Context.h"
15 #include "libANGLE/renderer/d3d/IndexDataManager.h"
16 #include "libANGLE/renderer/d3d/VertexDataManager.h"
17 #include "libANGLE/renderer/d3d/d3d11/Context11.h"
18 #include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
19 #include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
20 #include "libANGLE/renderer/d3d/d3d11/formatutils11.h"
21 #include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
22 #include "libANGLE/renderer/renderer_utils.h"
23 
24 namespace rx
25 {
26 
27 namespace
28 {
29 
30 template <typename T>
ReadIndexValueFromIndices(const uint8_t * data,size_t index)31 GLuint ReadIndexValueFromIndices(const uint8_t *data, size_t index)
32 {
33     return reinterpret_cast<const T *>(data)[index];
34 }
35 typedef GLuint (*ReadIndexValueFunction)(const uint8_t *data, size_t index);
36 
37 enum class CopyResult
38 {
39     RECREATED,
40     NOT_RECREATED,
41 };
42 
CalculateConstantBufferParams(GLintptr offset,GLsizeiptr size,UINT * outFirstConstant,UINT * outNumConstants)43 void CalculateConstantBufferParams(GLintptr offset,
44                                    GLsizeiptr size,
45                                    UINT *outFirstConstant,
46                                    UINT *outNumConstants)
47 {
48     // The offset must be aligned to 256 bytes (should have been enforced by glBindBufferRange).
49     ASSERT(offset % 256 == 0);
50 
51     // firstConstant and numConstants are expressed in constants of 16-bytes. Furthermore they must
52     // be a multiple of 16 constants.
53     *outFirstConstant = static_cast<UINT>(offset / 16);
54 
55     // The GL size is not required to be aligned to a 256 bytes boundary.
56     // Round the size up to a 256 bytes boundary then express the results in constants of 16-bytes.
57     *outNumConstants = static_cast<UINT>(rx::roundUpPow2(size, static_cast<GLsizeiptr>(256)) / 16);
58 
59     // Since the size is rounded up, firstConstant + numConstants may be bigger than the actual size
60     // of the buffer. This behaviour is explictly allowed according to the documentation on
61     // ID3D11DeviceContext1::PSSetConstantBuffers1
62     // https://msdn.microsoft.com/en-us/library/windows/desktop/hh404649%28v=vs.85%29.aspx
63 }
64 
65 }  // anonymous namespace
66 
67 namespace gl_d3d11
68 {
69 
GetD3DMapTypeFromBits(BufferUsage usage,GLbitfield access)70 D3D11_MAP GetD3DMapTypeFromBits(BufferUsage usage, GLbitfield access)
71 {
72     bool readBit  = ((access & GL_MAP_READ_BIT) != 0);
73     bool writeBit = ((access & GL_MAP_WRITE_BIT) != 0);
74 
75     ASSERT(readBit || writeBit);
76 
77     // Note : we ignore the discard bit, because in D3D11, staging buffers
78     //  don't accept the map-discard flag (discard only works for DYNAMIC usage)
79 
80     if (readBit && !writeBit)
81     {
82         return D3D11_MAP_READ;
83     }
84     else if (writeBit && !readBit)
85     {
86         // Special case for uniform storage - we only allow full buffer updates.
87         return usage == BUFFER_USAGE_UNIFORM || usage == BUFFER_USAGE_STRUCTURED
88                    ? D3D11_MAP_WRITE_DISCARD
89                    : D3D11_MAP_WRITE;
90     }
91     else if (writeBit && readBit)
92     {
93         return D3D11_MAP_READ_WRITE;
94     }
95     else
96     {
97         UNREACHABLE();
98         return D3D11_MAP_READ;
99     }
100 }
101 }  // namespace gl_d3d11
102 
103 // Each instance of Buffer11::BufferStorage is specialized for a class of D3D binding points
104 // - vertex/transform feedback buffers
105 // - index buffers
106 // - pixel unpack buffers
107 // - uniform buffers
108 class Buffer11::BufferStorage : angle::NonCopyable
109 {
110   public:
~BufferStorage()111     virtual ~BufferStorage() {}
112 
getDataRevision() const113     DataRevision getDataRevision() const { return mRevision; }
getUsage() const114     BufferUsage getUsage() const { return mUsage; }
getSize() const115     size_t getSize() const { return mBufferSize; }
setDataRevision(DataRevision rev)116     void setDataRevision(DataRevision rev) { mRevision = rev; }
117 
118     virtual bool isCPUAccessible(GLbitfield access) const = 0;
119 
120     virtual bool isGPUAccessible() const = 0;
121 
122     virtual angle::Result copyFromStorage(const gl::Context *context,
123                                           BufferStorage *source,
124                                           size_t sourceOffset,
125                                           size_t size,
126                                           size_t destOffset,
127                                           CopyResult *resultOut)                             = 0;
128     virtual angle::Result resize(const gl::Context *context, size_t size, bool preserveData) = 0;
129 
130     virtual angle::Result map(const gl::Context *context,
131                               size_t offset,
132                               size_t length,
133                               GLbitfield access,
134                               uint8_t **mapPointerOut) = 0;
135     virtual void unmap()                               = 0;
136 
137     angle::Result setData(const gl::Context *context,
138                           const uint8_t *data,
139                           size_t offset,
140                           size_t size);
141 
142     void setStructureByteStride(unsigned int structureByteStride);
143 
144   protected:
145     BufferStorage(Renderer11 *renderer, BufferUsage usage);
146 
147     Renderer11 *mRenderer;
148     DataRevision mRevision;
149     const BufferUsage mUsage;
150     size_t mBufferSize;
151 };
152 
153 // A native buffer storage represents an underlying D3D11 buffer for a particular
154 // type of storage.
155 class Buffer11::NativeStorage : public Buffer11::BufferStorage
156 {
157   public:
158     NativeStorage(Renderer11 *renderer, BufferUsage usage, const angle::Subject *onStorageChanged);
159     ~NativeStorage() override;
160 
161     bool isCPUAccessible(GLbitfield access) const override;
162 
isGPUAccessible() const163     bool isGPUAccessible() const override { return true; }
164 
getBuffer() const165     const d3d11::Buffer &getBuffer() const { return mBuffer; }
166     angle::Result copyFromStorage(const gl::Context *context,
167                                   BufferStorage *source,
168                                   size_t sourceOffset,
169                                   size_t size,
170                                   size_t destOffset,
171                                   CopyResult *resultOut) override;
172     angle::Result resize(const gl::Context *context, size_t size, bool preserveData) override;
173 
174     angle::Result map(const gl::Context *context,
175                       size_t offset,
176                       size_t length,
177                       GLbitfield access,
178                       uint8_t **mapPointerOut) override;
179     void unmap() override;
180 
181     angle::Result getSRVForFormat(const gl::Context *context,
182                                   DXGI_FORMAT srvFormat,
183                                   const d3d11::ShaderResourceView **srvOut);
184     angle::Result getRawUAV(const gl::Context *context,
185                             unsigned int offset,
186                             unsigned int size,
187                             d3d11::UnorderedAccessView **uavOut);
188 
189   protected:
190     d3d11::Buffer mBuffer;
191     const angle::Subject *mOnStorageChanged;
192 
193   private:
194     static void FillBufferDesc(D3D11_BUFFER_DESC *bufferDesc,
195                                Renderer11 *renderer,
196                                BufferUsage usage,
197                                unsigned int bufferSize);
198     void clearSRVs();
199     void clearUAVs();
200 
201     std::map<DXGI_FORMAT, d3d11::ShaderResourceView> mBufferResourceViews;
202     std::map<std::pair<unsigned int, unsigned int>, d3d11::UnorderedAccessView> mBufferRawUAVs;
203 };
204 
205 class Buffer11::StructuredBufferStorage : public Buffer11::NativeStorage
206 {
207   public:
208     StructuredBufferStorage(Renderer11 *renderer,
209                             BufferUsage usage,
210                             const angle::Subject *onStorageChanged);
211     ~StructuredBufferStorage() override;
212     angle::Result resizeStructuredBuffer(const gl::Context *context,
213                                          unsigned int size,
214                                          unsigned int structureByteStride);
215     angle::Result getStructuredBufferRangeSRV(const gl::Context *context,
216                                               unsigned int offset,
217                                               unsigned int size,
218                                               unsigned int structureByteStride,
219                                               const d3d11::ShaderResourceView **bufferOut);
220 
221   private:
222     d3d11::ShaderResourceView mStructuredBufferResourceView;
223 };
224 
225 // A emulated indexed buffer storage represents an underlying D3D11 buffer for data
226 // that has been expanded to match the indices list used. This storage is only
227 // used for FL9_3 pointsprite rendering emulation.
228 class Buffer11::EmulatedIndexedStorage : public Buffer11::BufferStorage
229 {
230   public:
231     EmulatedIndexedStorage(Renderer11 *renderer);
232     ~EmulatedIndexedStorage() override;
233 
isCPUAccessible(GLbitfield access) const234     bool isCPUAccessible(GLbitfield access) const override { return true; }
235 
isGPUAccessible() const236     bool isGPUAccessible() const override { return false; }
237 
238     angle::Result getBuffer(const gl::Context *context,
239                             SourceIndexData *indexInfo,
240                             const TranslatedAttribute &attribute,
241                             GLint startVertex,
242                             const d3d11::Buffer **bufferOut);
243 
244     angle::Result copyFromStorage(const gl::Context *context,
245                                   BufferStorage *source,
246                                   size_t sourceOffset,
247                                   size_t size,
248                                   size_t destOffset,
249                                   CopyResult *resultOut) override;
250 
251     angle::Result resize(const gl::Context *context, size_t size, bool preserveData) override;
252 
253     angle::Result map(const gl::Context *context,
254                       size_t offset,
255                       size_t length,
256                       GLbitfield access,
257                       uint8_t **mapPointerOut) override;
258     void unmap() override;
259 
260   private:
261     d3d11::Buffer mBuffer;                     // contains expanded data for use by D3D
262     angle::MemoryBuffer mMemoryBuffer;         // original data (not expanded)
263     angle::MemoryBuffer mIndicesMemoryBuffer;  // indices data
264 };
265 
266 // Pack storage represents internal storage for pack buffers. We implement pack buffers
267 // as CPU memory, tied to a staging texture, for asynchronous texture readback.
268 class Buffer11::PackStorage : public Buffer11::BufferStorage
269 {
270   public:
271     explicit PackStorage(Renderer11 *renderer);
272     ~PackStorage() override;
273 
isCPUAccessible(GLbitfield access) const274     bool isCPUAccessible(GLbitfield access) const override { return true; }
275 
isGPUAccessible() const276     bool isGPUAccessible() const override { return false; }
277 
278     angle::Result copyFromStorage(const gl::Context *context,
279                                   BufferStorage *source,
280                                   size_t sourceOffset,
281                                   size_t size,
282                                   size_t destOffset,
283                                   CopyResult *resultOut) override;
284     angle::Result resize(const gl::Context *context, size_t size, bool preserveData) override;
285 
286     angle::Result map(const gl::Context *context,
287                       size_t offset,
288                       size_t length,
289                       GLbitfield access,
290                       uint8_t **mapPointerOut) override;
291     void unmap() override;
292 
293     angle::Result packPixels(const gl::Context *context,
294                              const gl::FramebufferAttachment &readAttachment,
295                              const PackPixelsParams &params);
296 
297   private:
298     angle::Result flushQueuedPackCommand(const gl::Context *context);
299 
300     TextureHelper11 mStagingTexture;
301     angle::MemoryBuffer mMemoryBuffer;
302     std::unique_ptr<PackPixelsParams> mQueuedPackCommand;
303     PackPixelsParams mPackParams;
304     bool mDataModified;
305 };
306 
307 // System memory storage stores a CPU memory buffer with our buffer data.
308 // For dynamic data, it's much faster to update the CPU memory buffer than
309 // it is to update a D3D staging buffer and read it back later.
310 class Buffer11::SystemMemoryStorage : public Buffer11::BufferStorage
311 {
312   public:
313     explicit SystemMemoryStorage(Renderer11 *renderer);
~SystemMemoryStorage()314     ~SystemMemoryStorage() override {}
315 
isCPUAccessible(GLbitfield access) const316     bool isCPUAccessible(GLbitfield access) const override { return true; }
317 
isGPUAccessible() const318     bool isGPUAccessible() const override { return false; }
319 
320     angle::Result copyFromStorage(const gl::Context *context,
321                                   BufferStorage *source,
322                                   size_t sourceOffset,
323                                   size_t size,
324                                   size_t destOffset,
325                                   CopyResult *resultOut) override;
326     angle::Result resize(const gl::Context *context, size_t size, bool preserveData) override;
327 
328     angle::Result map(const gl::Context *context,
329                       size_t offset,
330                       size_t length,
331                       GLbitfield access,
332                       uint8_t **mapPointerOut) override;
333     void unmap() override;
334 
getSystemCopy()335     angle::MemoryBuffer *getSystemCopy() { return &mSystemCopy; }
336 
337   protected:
338     angle::MemoryBuffer mSystemCopy;
339 };
340 
Buffer11(const gl::BufferState & state,Renderer11 * renderer)341 Buffer11::Buffer11(const gl::BufferState &state, Renderer11 *renderer)
342     : BufferD3D(state, renderer),
343       mRenderer(renderer),
344       mSize(0),
345       mMappedStorage(nullptr),
346       mBufferStorages({}),
347       mLatestBufferStorage(nullptr),
348       mDeallocThresholds({}),
349       mIdleness({}),
350       mConstantBufferStorageAdditionalSize(0),
351       mMaxConstantBufferLruCount(0),
352       mStructuredBufferStorageAdditionalSize(0),
353       mMaxStructuredBufferLruCount(0)
354 {}
355 
~Buffer11()356 Buffer11::~Buffer11()
357 {
358     for (BufferStorage *&storage : mBufferStorages)
359     {
360         SafeDelete(storage);
361     }
362 
363     for (auto &p : mConstantBufferRangeStoragesCache)
364     {
365         SafeDelete(p.second.storage);
366     }
367 
368     for (auto &p : mStructuredBufferRangeStoragesCache)
369     {
370         SafeDelete(p.second.storage);
371     }
372 
373     mRenderer->onBufferDelete(this);
374 }
375 
setData(const gl::Context * context,gl::BufferBinding target,const void * data,size_t size,gl::BufferUsage usage)376 angle::Result Buffer11::setData(const gl::Context *context,
377                                 gl::BufferBinding target,
378                                 const void *data,
379                                 size_t size,
380                                 gl::BufferUsage usage)
381 {
382     updateD3DBufferUsage(context, usage);
383     return setSubData(context, target, data, size, 0);
384 }
385 
getData(const gl::Context * context,const uint8_t ** outData)386 angle::Result Buffer11::getData(const gl::Context *context, const uint8_t **outData)
387 {
388     if (mSize == 0)
389     {
390         // TODO(http://anglebug.com/2840): This ensures that we don't crash or assert in robust
391         // buffer access behavior mode if there are buffers without any data. However, technically
392         // it should still be possible to draw, with fetches from this buffer returning zero.
393         return angle::Result::Stop;
394     }
395 
396     SystemMemoryStorage *systemMemoryStorage = nullptr;
397     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_SYSTEM_MEMORY, &systemMemoryStorage));
398 
399     ASSERT(systemMemoryStorage->getSize() >= mSize);
400 
401     *outData = systemMemoryStorage->getSystemCopy()->data();
402     return angle::Result::Continue;
403 }
404 
setSubData(const gl::Context * context,gl::BufferBinding target,const void * data,size_t size,size_t offset)405 angle::Result Buffer11::setSubData(const gl::Context *context,
406                                    gl::BufferBinding target,
407                                    const void *data,
408                                    size_t size,
409                                    size_t offset)
410 {
411     size_t requiredSize = size + offset;
412 
413     if (data && size > 0)
414     {
415         // Use system memory storage for dynamic buffers.
416         // Try using a constant storage for constant buffers
417         BufferStorage *writeBuffer = nullptr;
418         if (target == gl::BufferBinding::Uniform)
419         {
420             // If we are a very large uniform buffer, keep system memory storage around so that we
421             // aren't forced to read back from a constant buffer. We also check the workaround for
422             // Intel - this requires us to use system memory so we don't end up having to copy from
423             // a constant buffer to a staging buffer.
424             // TODO(jmadill): Use Context caps.
425             if (offset == 0 && size >= mSize &&
426                 size <= static_cast<UINT>(mRenderer->getNativeCaps().maxUniformBlockSize) &&
427                 !mRenderer->getFeatures().useSystemMemoryForConstantBuffers.enabled)
428             {
429                 BufferStorage *latestStorage = nullptr;
430                 ANGLE_TRY(getLatestBufferStorage(context, &latestStorage));
431                 if (latestStorage && (latestStorage->getUsage() == BUFFER_USAGE_STRUCTURED))
432                 {
433                     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_STRUCTURED, &writeBuffer));
434                 }
435                 else
436                 {
437                     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_UNIFORM, &writeBuffer));
438                 }
439             }
440             else
441             {
442                 ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_SYSTEM_MEMORY, &writeBuffer));
443             }
444         }
445         else if (supportsDirectBinding())
446         {
447             ANGLE_TRY(getStagingStorage(context, &writeBuffer));
448         }
449         else
450         {
451             ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_SYSTEM_MEMORY, &writeBuffer));
452         }
453 
454         ASSERT(writeBuffer);
455 
456         // Explicitly resize the staging buffer, preserving data if the new data will not
457         // completely fill the buffer
458         if (writeBuffer->getSize() < requiredSize)
459         {
460             bool preserveData = (offset > 0);
461             ANGLE_TRY(writeBuffer->resize(context, requiredSize, preserveData));
462         }
463 
464         ANGLE_TRY(writeBuffer->setData(context, static_cast<const uint8_t *>(data), offset, size));
465         onStorageUpdate(writeBuffer);
466     }
467 
468     mSize = std::max(mSize, requiredSize);
469     invalidateStaticData(context);
470 
471     return angle::Result::Continue;
472 }
473 
copySubData(const gl::Context * context,BufferImpl * source,GLintptr sourceOffset,GLintptr destOffset,GLsizeiptr size)474 angle::Result Buffer11::copySubData(const gl::Context *context,
475                                     BufferImpl *source,
476                                     GLintptr sourceOffset,
477                                     GLintptr destOffset,
478                                     GLsizeiptr size)
479 {
480     Buffer11 *sourceBuffer = GetAs<Buffer11>(source);
481     ASSERT(sourceBuffer != nullptr);
482 
483     BufferStorage *copyDest = nullptr;
484     ANGLE_TRY(getLatestBufferStorage(context, &copyDest));
485 
486     if (!copyDest)
487     {
488         ANGLE_TRY(getStagingStorage(context, &copyDest));
489     }
490 
491     BufferStorage *copySource = nullptr;
492     ANGLE_TRY(sourceBuffer->getLatestBufferStorage(context, &copySource));
493 
494     if (!copySource)
495     {
496         ANGLE_TRY(sourceBuffer->getStagingStorage(context, &copySource));
497     }
498 
499     ASSERT(copySource && copyDest);
500 
501     // A staging buffer is needed if there is no cpu-cpu or gpu-gpu copy path avaiable.
502     if (!copyDest->isGPUAccessible() && !copySource->isCPUAccessible(GL_MAP_READ_BIT))
503     {
504         ANGLE_TRY(sourceBuffer->getStagingStorage(context, &copySource));
505     }
506     else if (!copySource->isGPUAccessible() && !copyDest->isCPUAccessible(GL_MAP_WRITE_BIT))
507     {
508         ANGLE_TRY(getStagingStorage(context, &copyDest));
509     }
510 
511     // D3D11 does not allow overlapped copies until 11.1, and only if the
512     // device supports D3D11_FEATURE_DATA_D3D11_OPTIONS::CopyWithOverlap
513     // Get around this via a different source buffer
514     if (copySource == copyDest)
515     {
516         if (copySource->getUsage() == BUFFER_USAGE_STAGING)
517         {
518             ANGLE_TRY(
519                 getBufferStorage(context, BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK, &copySource));
520         }
521         else
522         {
523             ANGLE_TRY(getStagingStorage(context, &copySource));
524         }
525     }
526 
527     CopyResult copyResult = CopyResult::NOT_RECREATED;
528     ANGLE_TRY(copyDest->copyFromStorage(context, copySource, sourceOffset, size, destOffset,
529                                         &copyResult));
530     onStorageUpdate(copyDest);
531 
532     mSize = std::max<size_t>(mSize, destOffset + size);
533     invalidateStaticData(context);
534 
535     return angle::Result::Continue;
536 }
537 
map(const gl::Context * context,GLenum access,void ** mapPtr)538 angle::Result Buffer11::map(const gl::Context *context, GLenum access, void **mapPtr)
539 {
540     // GL_OES_mapbuffer uses an enum instead of a bitfield for it's access, convert to a bitfield
541     // and call mapRange.
542     ASSERT(access == GL_WRITE_ONLY_OES);
543     return mapRange(context, 0, mSize, GL_MAP_WRITE_BIT, mapPtr);
544 }
545 
mapRange(const gl::Context * context,size_t offset,size_t length,GLbitfield access,void ** mapPtr)546 angle::Result Buffer11::mapRange(const gl::Context *context,
547                                  size_t offset,
548                                  size_t length,
549                                  GLbitfield access,
550                                  void **mapPtr)
551 {
552     ASSERT(!mMappedStorage);
553 
554     BufferStorage *latestStorage = nullptr;
555     ANGLE_TRY(getLatestBufferStorage(context, &latestStorage));
556 
557     if (latestStorage && (latestStorage->getUsage() == BUFFER_USAGE_PIXEL_PACK ||
558                           latestStorage->getUsage() == BUFFER_USAGE_STAGING))
559     {
560         // Latest storage is mappable.
561         mMappedStorage = latestStorage;
562     }
563     else
564     {
565         // Fall back to using the staging buffer if the latest storage does not exist or is not
566         // CPU-accessible.
567         ANGLE_TRY(getStagingStorage(context, &mMappedStorage));
568     }
569 
570     Context11 *context11 = GetImplAs<Context11>(context);
571     ANGLE_CHECK_GL_ALLOC(context11, mMappedStorage);
572 
573     if ((access & GL_MAP_WRITE_BIT) > 0)
574     {
575         // Update the data revision immediately, since the data might be changed at any time
576         onStorageUpdate(mMappedStorage);
577         invalidateStaticData(context);
578     }
579 
580     uint8_t *mappedBuffer = nullptr;
581     ANGLE_TRY(mMappedStorage->map(context, offset, length, access, &mappedBuffer));
582     ASSERT(mappedBuffer);
583 
584     *mapPtr = static_cast<void *>(mappedBuffer);
585     return angle::Result::Continue;
586 }
587 
unmap(const gl::Context * context,GLboolean * result)588 angle::Result Buffer11::unmap(const gl::Context *context, GLboolean *result)
589 {
590     ASSERT(mMappedStorage);
591     mMappedStorage->unmap();
592     mMappedStorage = nullptr;
593 
594     // TODO: detect if we had corruption. if so, return false.
595     *result = GL_TRUE;
596 
597     return angle::Result::Continue;
598 }
599 
markTransformFeedbackUsage(const gl::Context * context)600 angle::Result Buffer11::markTransformFeedbackUsage(const gl::Context *context)
601 {
602     ANGLE_TRY(markBufferUsage(context, BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK));
603     return angle::Result::Continue;
604 }
605 
updateDeallocThreshold(BufferUsage usage)606 void Buffer11::updateDeallocThreshold(BufferUsage usage)
607 {
608     // The following strategy was tuned on the Oort online benchmark (http://oortonline.gl/)
609     // as well as a custom microbenchmark (IndexConversionPerfTest.Run/index_range_d3d11)
610 
611     // First readback: 8 unmodified uses before we free buffer memory.
612     // After that, double the threshold each time until we reach the max.
613     if (mDeallocThresholds[usage] == 0)
614     {
615         mDeallocThresholds[usage] = 8;
616     }
617     else if (mDeallocThresholds[usage] < std::numeric_limits<unsigned int>::max() / 2u)
618     {
619         mDeallocThresholds[usage] *= 2u;
620     }
621     else
622     {
623         mDeallocThresholds[usage] = std::numeric_limits<unsigned int>::max();
624     }
625 }
626 
627 // Free the storage if we decide it isn't being used very often.
checkForDeallocation(const gl::Context * context,BufferUsage usage)628 angle::Result Buffer11::checkForDeallocation(const gl::Context *context, BufferUsage usage)
629 {
630     mIdleness[usage]++;
631 
632     BufferStorage *&storage = mBufferStorages[usage];
633     if (storage != nullptr && mIdleness[usage] > mDeallocThresholds[usage])
634     {
635         BufferStorage *latestStorage = nullptr;
636         ANGLE_TRY(getLatestBufferStorage(context, &latestStorage));
637         if (latestStorage != storage)
638         {
639             SafeDelete(storage);
640         }
641     }
642 
643     return angle::Result::Continue;
644 }
645 
646 // Keep system memory when we are using it for the canonical version of data.
canDeallocateSystemMemory() const647 bool Buffer11::canDeallocateSystemMemory() const
648 {
649     // Must keep system memory on Intel.
650     if (mRenderer->getFeatures().useSystemMemoryForConstantBuffers.enabled)
651     {
652         return false;
653     }
654 
655     return (!mBufferStorages[BUFFER_USAGE_UNIFORM] ||
656             mSize <= static_cast<size_t>(mRenderer->getNativeCaps().maxUniformBlockSize));
657 }
658 
markBufferUsage(BufferUsage usage)659 void Buffer11::markBufferUsage(BufferUsage usage)
660 {
661     mIdleness[usage] = 0;
662 }
663 
markBufferUsage(const gl::Context * context,BufferUsage usage)664 angle::Result Buffer11::markBufferUsage(const gl::Context *context, BufferUsage usage)
665 {
666     BufferStorage *bufferStorage = nullptr;
667     ANGLE_TRY(getBufferStorage(context, usage, &bufferStorage));
668 
669     if (bufferStorage)
670     {
671         onStorageUpdate(bufferStorage);
672     }
673 
674     invalidateStaticData(context);
675     return angle::Result::Continue;
676 }
677 
garbageCollection(const gl::Context * context,BufferUsage currentUsage)678 angle::Result Buffer11::garbageCollection(const gl::Context *context, BufferUsage currentUsage)
679 {
680     if (currentUsage != BUFFER_USAGE_SYSTEM_MEMORY && canDeallocateSystemMemory())
681     {
682         ANGLE_TRY(checkForDeallocation(context, BUFFER_USAGE_SYSTEM_MEMORY));
683     }
684 
685     if (currentUsage != BUFFER_USAGE_STAGING)
686     {
687         ANGLE_TRY(checkForDeallocation(context, BUFFER_USAGE_STAGING));
688     }
689 
690     return angle::Result::Continue;
691 }
692 
getBuffer(const gl::Context * context,BufferUsage usage,ID3D11Buffer ** bufferOut)693 angle::Result Buffer11::getBuffer(const gl::Context *context,
694                                   BufferUsage usage,
695                                   ID3D11Buffer **bufferOut)
696 {
697     NativeStorage *storage = nullptr;
698     ANGLE_TRY(getBufferStorage(context, usage, &storage));
699     *bufferOut = storage->getBuffer().get();
700     return angle::Result::Continue;
701 }
702 
getEmulatedIndexedBuffer(const gl::Context * context,SourceIndexData * indexInfo,const TranslatedAttribute & attribute,GLint startVertex,ID3D11Buffer ** bufferOut)703 angle::Result Buffer11::getEmulatedIndexedBuffer(const gl::Context *context,
704                                                  SourceIndexData *indexInfo,
705                                                  const TranslatedAttribute &attribute,
706                                                  GLint startVertex,
707                                                  ID3D11Buffer **bufferOut)
708 {
709     ASSERT(indexInfo);
710 
711     EmulatedIndexedStorage *emulatedStorage = nullptr;
712     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_EMULATED_INDEXED_VERTEX, &emulatedStorage));
713 
714     const d3d11::Buffer *nativeBuffer = nullptr;
715     ANGLE_TRY(
716         emulatedStorage->getBuffer(context, indexInfo, attribute, startVertex, &nativeBuffer));
717     *bufferOut = nativeBuffer->get();
718     return angle::Result::Continue;
719 }
720 
getConstantBufferRange(const gl::Context * context,GLintptr offset,GLsizeiptr size,const d3d11::Buffer ** bufferOut,UINT * firstConstantOut,UINT * numConstantsOut)721 angle::Result Buffer11::getConstantBufferRange(const gl::Context *context,
722                                                GLintptr offset,
723                                                GLsizeiptr size,
724                                                const d3d11::Buffer **bufferOut,
725                                                UINT *firstConstantOut,
726                                                UINT *numConstantsOut)
727 {
728     NativeStorage *bufferStorage = nullptr;
729     if ((offset == 0 &&
730          size < static_cast<GLsizeiptr>(mRenderer->getNativeCaps().maxUniformBlockSize)) ||
731         mRenderer->getRenderer11DeviceCaps().supportsConstantBufferOffsets)
732     {
733         ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_UNIFORM, &bufferStorage));
734         CalculateConstantBufferParams(offset, size, firstConstantOut, numConstantsOut);
735     }
736     else
737     {
738         ANGLE_TRY(getConstantBufferRangeStorage(context, offset, size, &bufferStorage));
739         *firstConstantOut = 0;
740         *numConstantsOut  = 0;
741     }
742 
743     *bufferOut = &bufferStorage->getBuffer();
744     return angle::Result::Continue;
745 }
746 
markRawBufferUsage(const gl::Context * context)747 angle::Result Buffer11::markRawBufferUsage(const gl::Context *context)
748 {
749     ANGLE_TRY(markBufferUsage(context, BUFFER_USAGE_RAW_UAV));
750     return angle::Result::Continue;
751 }
752 
getRawUAVRange(const gl::Context * context,GLintptr offset,GLsizeiptr size,d3d11::UnorderedAccessView ** uavOut)753 angle::Result Buffer11::getRawUAVRange(const gl::Context *context,
754                                        GLintptr offset,
755                                        GLsizeiptr size,
756                                        d3d11::UnorderedAccessView **uavOut)
757 {
758     NativeStorage *nativeStorage = nullptr;
759     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_RAW_UAV, &nativeStorage));
760 
761     return nativeStorage->getRawUAV(context, static_cast<unsigned int>(offset),
762                                     static_cast<unsigned int>(size), uavOut);
763 }
764 
getSRV(const gl::Context * context,DXGI_FORMAT srvFormat,const d3d11::ShaderResourceView ** srvOut)765 angle::Result Buffer11::getSRV(const gl::Context *context,
766                                DXGI_FORMAT srvFormat,
767                                const d3d11::ShaderResourceView **srvOut)
768 {
769     NativeStorage *nativeStorage = nullptr;
770     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_PIXEL_UNPACK, &nativeStorage));
771     return nativeStorage->getSRVForFormat(context, srvFormat, srvOut);
772 }
773 
packPixels(const gl::Context * context,const gl::FramebufferAttachment & readAttachment,const PackPixelsParams & params)774 angle::Result Buffer11::packPixels(const gl::Context *context,
775                                    const gl::FramebufferAttachment &readAttachment,
776                                    const PackPixelsParams &params)
777 {
778     PackStorage *packStorage = nullptr;
779     ANGLE_TRY(getBufferStorage(context, BUFFER_USAGE_PIXEL_PACK, &packStorage));
780 
781     ASSERT(packStorage);
782     ANGLE_TRY(packStorage->packPixels(context, readAttachment, params));
783     onStorageUpdate(packStorage);
784 
785     return angle::Result::Continue;
786 }
787 
getTotalCPUBufferMemoryBytes() const788 size_t Buffer11::getTotalCPUBufferMemoryBytes() const
789 {
790     size_t allocationSize = 0;
791 
792     BufferStorage *staging = mBufferStorages[BUFFER_USAGE_STAGING];
793     allocationSize += staging ? staging->getSize() : 0;
794 
795     BufferStorage *sysMem = mBufferStorages[BUFFER_USAGE_SYSTEM_MEMORY];
796     allocationSize += sysMem ? sysMem->getSize() : 0;
797 
798     return allocationSize;
799 }
800 
801 template <typename StorageOutT>
getBufferStorage(const gl::Context * context,BufferUsage usage,StorageOutT ** storageOut)802 angle::Result Buffer11::getBufferStorage(const gl::Context *context,
803                                          BufferUsage usage,
804                                          StorageOutT **storageOut)
805 {
806     ASSERT(0 <= usage && usage < BUFFER_USAGE_COUNT);
807     BufferStorage *&newStorage = mBufferStorages[usage];
808 
809     if (!newStorage)
810     {
811         newStorage = allocateStorage(usage);
812     }
813 
814     markBufferUsage(usage);
815 
816     // resize buffer
817     if (newStorage->getSize() < mSize)
818     {
819         ANGLE_TRY(newStorage->resize(context, mSize, true));
820     }
821 
822     ASSERT(newStorage);
823 
824     ANGLE_TRY(updateBufferStorage(context, newStorage, 0, mSize));
825     ANGLE_TRY(garbageCollection(context, usage));
826 
827     *storageOut = GetAs<StorageOutT>(newStorage);
828     return angle::Result::Continue;
829 }
830 
allocateStorage(BufferUsage usage)831 Buffer11::BufferStorage *Buffer11::allocateStorage(BufferUsage usage)
832 {
833     updateDeallocThreshold(usage);
834     switch (usage)
835     {
836         case BUFFER_USAGE_PIXEL_PACK:
837             return new PackStorage(mRenderer);
838         case BUFFER_USAGE_SYSTEM_MEMORY:
839             return new SystemMemoryStorage(mRenderer);
840         case BUFFER_USAGE_EMULATED_INDEXED_VERTEX:
841             return new EmulatedIndexedStorage(mRenderer);
842         case BUFFER_USAGE_INDEX:
843         case BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK:
844             return new NativeStorage(mRenderer, usage, this);
845         case BUFFER_USAGE_STRUCTURED:
846             return new StructuredBufferStorage(mRenderer, usage, nullptr);
847         default:
848             return new NativeStorage(mRenderer, usage, nullptr);
849     }
850 }
851 
getConstantBufferRangeStorage(const gl::Context * context,GLintptr offset,GLsizeiptr size,Buffer11::NativeStorage ** storageOut)852 angle::Result Buffer11::getConstantBufferRangeStorage(const gl::Context *context,
853                                                       GLintptr offset,
854                                                       GLsizeiptr size,
855                                                       Buffer11::NativeStorage **storageOut)
856 {
857     BufferStorage *newStorage;
858     {
859         // Keep the cacheEntry in a limited scope because it may be invalidated later in the code if
860         // we need to reclaim some space.
861         BufferCacheEntry *cacheEntry = &mConstantBufferRangeStoragesCache[offset];
862 
863         if (!cacheEntry->storage)
864         {
865             cacheEntry->storage  = allocateStorage(BUFFER_USAGE_UNIFORM);
866             cacheEntry->lruCount = ++mMaxConstantBufferLruCount;
867         }
868 
869         cacheEntry->lruCount = ++mMaxConstantBufferLruCount;
870         newStorage           = cacheEntry->storage;
871     }
872 
873     markBufferUsage(BUFFER_USAGE_UNIFORM);
874 
875     if (newStorage->getSize() < static_cast<size_t>(size))
876     {
877         size_t maximumAllowedAdditionalSize = 2 * getSize();
878 
879         size_t sizeDelta = size - newStorage->getSize();
880 
881         while (mConstantBufferStorageAdditionalSize + sizeDelta > maximumAllowedAdditionalSize)
882         {
883             auto iter = std::min_element(
884                 std::begin(mConstantBufferRangeStoragesCache),
885                 std::end(mConstantBufferRangeStoragesCache),
886                 [](const BufferCache::value_type &a, const BufferCache::value_type &b) {
887                     return a.second.lruCount < b.second.lruCount;
888                 });
889 
890             ASSERT(iter->second.storage != newStorage);
891             ASSERT(mConstantBufferStorageAdditionalSize >= iter->second.storage->getSize());
892 
893             mConstantBufferStorageAdditionalSize -= iter->second.storage->getSize();
894             SafeDelete(iter->second.storage);
895             mConstantBufferRangeStoragesCache.erase(iter);
896         }
897 
898         ANGLE_TRY(newStorage->resize(context, size, false));
899         mConstantBufferStorageAdditionalSize += sizeDelta;
900 
901         // We don't copy the old data when resizing the constant buffer because the data may be
902         // out-of-date therefore we reset the data revision and let updateBufferStorage() handle the
903         // copy.
904         newStorage->setDataRevision(0);
905     }
906 
907     ANGLE_TRY(updateBufferStorage(context, newStorage, offset, size));
908     ANGLE_TRY(garbageCollection(context, BUFFER_USAGE_UNIFORM));
909     *storageOut = GetAs<NativeStorage>(newStorage);
910     return angle::Result::Continue;
911 }
912 
getStructuredBufferRangeSRV(const gl::Context * context,unsigned int offset,unsigned int size,unsigned int structureByteStride,const d3d11::ShaderResourceView ** srvOut)913 angle::Result Buffer11::getStructuredBufferRangeSRV(const gl::Context *context,
914                                                     unsigned int offset,
915                                                     unsigned int size,
916                                                     unsigned int structureByteStride,
917                                                     const d3d11::ShaderResourceView **srvOut)
918 {
919     BufferStorage *newStorage;
920 
921     {
922         // Keep the cacheEntry in a limited scope because it may be invalidated later in the code if
923         // we need to reclaim some space.
924         StructuredBufferKey structuredBufferKey = StructuredBufferKey(offset, structureByteStride);
925         BufferCacheEntry *cacheEntry = &mStructuredBufferRangeStoragesCache[structuredBufferKey];
926 
927         if (!cacheEntry->storage)
928         {
929             cacheEntry->storage  = allocateStorage(BUFFER_USAGE_STRUCTURED);
930             cacheEntry->lruCount = ++mMaxStructuredBufferLruCount;
931         }
932 
933         cacheEntry->lruCount = ++mMaxStructuredBufferLruCount;
934         newStorage           = cacheEntry->storage;
935     }
936 
937     StructuredBufferStorage *structuredBufferStorage = GetAs<StructuredBufferStorage>(newStorage);
938 
939     markBufferUsage(BUFFER_USAGE_STRUCTURED);
940 
941     if (newStorage->getSize() < static_cast<size_t>(size))
942     {
943         size_t maximumAllowedAdditionalSize = 2 * getSize();
944 
945         size_t sizeDelta = static_cast<size_t>(size) - newStorage->getSize();
946 
947         while (mStructuredBufferStorageAdditionalSize + sizeDelta > maximumAllowedAdditionalSize)
948         {
949             auto iter = std::min_element(std::begin(mStructuredBufferRangeStoragesCache),
950                                          std::end(mStructuredBufferRangeStoragesCache),
951                                          [](const StructuredBufferCache::value_type &a,
952                                             const StructuredBufferCache::value_type &b) {
953                                              return a.second.lruCount < b.second.lruCount;
954                                          });
955 
956             ASSERT(iter->second.storage != newStorage);
957             ASSERT(mStructuredBufferStorageAdditionalSize >= iter->second.storage->getSize());
958 
959             mStructuredBufferStorageAdditionalSize -= iter->second.storage->getSize();
960             SafeDelete(iter->second.storage);
961             mStructuredBufferRangeStoragesCache.erase(iter);
962         }
963 
964         ANGLE_TRY(
965             structuredBufferStorage->resizeStructuredBuffer(context, size, structureByteStride));
966         mStructuredBufferStorageAdditionalSize += sizeDelta;
967 
968         // We don't copy the old data when resizing the structured buffer because the data may be
969         // out-of-date therefore we reset the data revision and let updateBufferStorage() handle the
970         // copy.
971         newStorage->setDataRevision(0);
972     }
973 
974     ANGLE_TRY(updateBufferStorage(context, newStorage, offset, static_cast<size_t>(size)));
975     ANGLE_TRY(garbageCollection(context, BUFFER_USAGE_STRUCTURED));
976     ANGLE_TRY(structuredBufferStorage->getStructuredBufferRangeSRV(context, offset, size,
977                                                                    structureByteStride, srvOut));
978     return angle::Result::Continue;
979 }
980 
updateBufferStorage(const gl::Context * context,BufferStorage * storage,size_t sourceOffset,size_t storageSize)981 angle::Result Buffer11::updateBufferStorage(const gl::Context *context,
982                                             BufferStorage *storage,
983                                             size_t sourceOffset,
984                                             size_t storageSize)
985 {
986     BufferStorage *latestBuffer = nullptr;
987     ANGLE_TRY(getLatestBufferStorage(context, &latestBuffer));
988 
989     ASSERT(storage);
990 
991     if (!latestBuffer)
992     {
993         onStorageUpdate(storage);
994         return angle::Result::Continue;
995     }
996 
997     if (latestBuffer->getDataRevision() <= storage->getDataRevision())
998     {
999         return angle::Result::Continue;
1000     }
1001 
1002     // Copy through a staging buffer if we're copying from or to a non-staging, mappable
1003     // buffer storage. This is because we can't map a GPU buffer, and copy CPU
1004     // data directly. If we're already using a staging buffer we're fine.
1005     if (latestBuffer->getUsage() != BUFFER_USAGE_STAGING &&
1006         storage->getUsage() != BUFFER_USAGE_STAGING &&
1007         (!latestBuffer->isCPUAccessible(GL_MAP_READ_BIT) ||
1008          !storage->isCPUAccessible(GL_MAP_WRITE_BIT)))
1009     {
1010         NativeStorage *stagingBuffer = nullptr;
1011         ANGLE_TRY(getStagingStorage(context, &stagingBuffer));
1012 
1013         CopyResult copyResult = CopyResult::NOT_RECREATED;
1014         ANGLE_TRY(stagingBuffer->copyFromStorage(context, latestBuffer, 0, latestBuffer->getSize(),
1015                                                  0, &copyResult));
1016         onCopyStorage(stagingBuffer, latestBuffer);
1017 
1018         latestBuffer = stagingBuffer;
1019     }
1020 
1021     CopyResult copyResult = CopyResult::NOT_RECREATED;
1022     ANGLE_TRY(
1023         storage->copyFromStorage(context, latestBuffer, sourceOffset, storageSize, 0, &copyResult));
1024     // If the D3D buffer has been recreated, we should update our serial.
1025     if (copyResult == CopyResult::RECREATED)
1026     {
1027         updateSerial();
1028     }
1029     onCopyStorage(storage, latestBuffer);
1030     return angle::Result::Continue;
1031 }
1032 
getLatestBufferStorage(const gl::Context * context,Buffer11::BufferStorage ** storageOut) const1033 angle::Result Buffer11::getLatestBufferStorage(const gl::Context *context,
1034                                                Buffer11::BufferStorage **storageOut) const
1035 {
1036     // resize buffer
1037     if (mLatestBufferStorage && mLatestBufferStorage->getSize() < mSize)
1038     {
1039         ANGLE_TRY(mLatestBufferStorage->resize(context, mSize, true));
1040     }
1041 
1042     *storageOut = mLatestBufferStorage;
1043     return angle::Result::Continue;
1044 }
1045 
1046 template <typename StorageOutT>
getStagingStorage(const gl::Context * context,StorageOutT ** storageOut)1047 angle::Result Buffer11::getStagingStorage(const gl::Context *context, StorageOutT **storageOut)
1048 {
1049     return getBufferStorage(context, BUFFER_USAGE_STAGING, storageOut);
1050 }
1051 
getSize() const1052 size_t Buffer11::getSize() const
1053 {
1054     return mSize;
1055 }
1056 
supportsDirectBinding() const1057 bool Buffer11::supportsDirectBinding() const
1058 {
1059     // Do not support direct buffers for dynamic data. The streaming buffer
1060     // offers better performance for data which changes every frame.
1061     return (mUsage == D3DBufferUsage::STATIC);
1062 }
1063 
initializeStaticData(const gl::Context * context)1064 void Buffer11::initializeStaticData(const gl::Context *context)
1065 {
1066     BufferD3D::initializeStaticData(context);
1067     onStateChange(angle::SubjectMessage::SubjectChanged);
1068 }
1069 
invalidateStaticData(const gl::Context * context)1070 void Buffer11::invalidateStaticData(const gl::Context *context)
1071 {
1072     BufferD3D::invalidateStaticData(context);
1073     onStateChange(angle::SubjectMessage::SubjectChanged);
1074 }
1075 
onCopyStorage(BufferStorage * dest,BufferStorage * source)1076 void Buffer11::onCopyStorage(BufferStorage *dest, BufferStorage *source)
1077 {
1078     ASSERT(source && mLatestBufferStorage);
1079     dest->setDataRevision(source->getDataRevision());
1080 
1081     // Only update the latest buffer storage if our usage index is lower. See comment in header.
1082     if (dest->getUsage() < mLatestBufferStorage->getUsage())
1083     {
1084         mLatestBufferStorage = dest;
1085     }
1086 }
1087 
onStorageUpdate(BufferStorage * updatedStorage)1088 void Buffer11::onStorageUpdate(BufferStorage *updatedStorage)
1089 {
1090     updatedStorage->setDataRevision(updatedStorage->getDataRevision() + 1);
1091     mLatestBufferStorage = updatedStorage;
1092 }
1093 
1094 // Buffer11::BufferStorage implementation
1095 
BufferStorage(Renderer11 * renderer,BufferUsage usage)1096 Buffer11::BufferStorage::BufferStorage(Renderer11 *renderer, BufferUsage usage)
1097     : mRenderer(renderer), mRevision(0), mUsage(usage), mBufferSize(0)
1098 {}
1099 
setData(const gl::Context * context,const uint8_t * data,size_t offset,size_t size)1100 angle::Result Buffer11::BufferStorage::setData(const gl::Context *context,
1101                                                const uint8_t *data,
1102                                                size_t offset,
1103                                                size_t size)
1104 {
1105     ASSERT(isCPUAccessible(GL_MAP_WRITE_BIT));
1106 
1107     // Uniform storage can have a different internal size than the buffer size. Ensure we don't
1108     // overflow.
1109     size_t mapSize = std::min(size, mBufferSize - offset);
1110 
1111     uint8_t *writePointer = nullptr;
1112     ANGLE_TRY(map(context, offset, mapSize, GL_MAP_WRITE_BIT, &writePointer));
1113 
1114     memcpy(writePointer, data, mapSize);
1115 
1116     unmap();
1117 
1118     return angle::Result::Continue;
1119 }
1120 
1121 // Buffer11::NativeStorage implementation
1122 
NativeStorage(Renderer11 * renderer,BufferUsage usage,const angle::Subject * onStorageChanged)1123 Buffer11::NativeStorage::NativeStorage(Renderer11 *renderer,
1124                                        BufferUsage usage,
1125                                        const angle::Subject *onStorageChanged)
1126     : BufferStorage(renderer, usage), mBuffer(), mOnStorageChanged(onStorageChanged)
1127 {}
1128 
~NativeStorage()1129 Buffer11::NativeStorage::~NativeStorage()
1130 {
1131     clearSRVs();
1132     clearUAVs();
1133 }
1134 
isCPUAccessible(GLbitfield access) const1135 bool Buffer11::NativeStorage::isCPUAccessible(GLbitfield access) const
1136 {
1137     if ((access & GL_MAP_READ_BIT) != 0)
1138     {
1139         // Read is more exclusive than write mappability.
1140         return (mUsage == BUFFER_USAGE_STAGING);
1141     }
1142     ASSERT((access & GL_MAP_WRITE_BIT) != 0);
1143     return (mUsage == BUFFER_USAGE_STAGING || mUsage == BUFFER_USAGE_UNIFORM ||
1144             mUsage == BUFFER_USAGE_STRUCTURED);
1145 }
1146 
1147 // Returns true if it recreates the direct buffer
copyFromStorage(const gl::Context * context,BufferStorage * source,size_t sourceOffset,size_t size,size_t destOffset,CopyResult * resultOut)1148 angle::Result Buffer11::NativeStorage::copyFromStorage(const gl::Context *context,
1149                                                        BufferStorage *source,
1150                                                        size_t sourceOffset,
1151                                                        size_t size,
1152                                                        size_t destOffset,
1153                                                        CopyResult *resultOut)
1154 {
1155     size_t requiredSize = destOffset + size;
1156 
1157     // (Re)initialize D3D buffer if needed
1158     bool preserveData = (destOffset > 0);
1159     if (!mBuffer.valid() || mBufferSize < requiredSize)
1160     {
1161         ANGLE_TRY(resize(context, requiredSize, preserveData));
1162         *resultOut = CopyResult::RECREATED;
1163     }
1164     else
1165     {
1166         *resultOut = CopyResult::NOT_RECREATED;
1167     }
1168 
1169     size_t clampedSize = size;
1170     if (mUsage == BUFFER_USAGE_UNIFORM)
1171     {
1172         clampedSize = std::min(clampedSize, mBufferSize - destOffset);
1173     }
1174 
1175     if (clampedSize == 0)
1176     {
1177         return angle::Result::Continue;
1178     }
1179 
1180     if (source->getUsage() == BUFFER_USAGE_PIXEL_PACK ||
1181         source->getUsage() == BUFFER_USAGE_SYSTEM_MEMORY)
1182     {
1183         ASSERT(source->isCPUAccessible(GL_MAP_READ_BIT) && isCPUAccessible(GL_MAP_WRITE_BIT));
1184 
1185         // Uniform buffers must be mapped with write/discard.
1186         ASSERT(!(preserveData && mUsage == BUFFER_USAGE_UNIFORM));
1187 
1188         uint8_t *sourcePointer = nullptr;
1189         ANGLE_TRY(source->map(context, sourceOffset, clampedSize, GL_MAP_READ_BIT, &sourcePointer));
1190 
1191         auto err = setData(context, sourcePointer, destOffset, clampedSize);
1192         source->unmap();
1193         ANGLE_TRY(err);
1194     }
1195     else
1196     {
1197         D3D11_BOX srcBox;
1198         srcBox.left   = static_cast<unsigned int>(sourceOffset);
1199         srcBox.right  = static_cast<unsigned int>(sourceOffset + clampedSize);
1200         srcBox.top    = 0;
1201         srcBox.bottom = 1;
1202         srcBox.front  = 0;
1203         srcBox.back   = 1;
1204 
1205         const d3d11::Buffer *sourceBuffer = &GetAs<NativeStorage>(source)->getBuffer();
1206 
1207         ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
1208         deviceContext->CopySubresourceRegion(mBuffer.get(), 0,
1209                                              static_cast<unsigned int>(destOffset), 0, 0,
1210                                              sourceBuffer->get(), 0, &srcBox);
1211     }
1212 
1213     return angle::Result::Continue;
1214 }
1215 
resize(const gl::Context * context,size_t size,bool preserveData)1216 angle::Result Buffer11::NativeStorage::resize(const gl::Context *context,
1217                                               size_t size,
1218                                               bool preserveData)
1219 {
1220     if (size == 0)
1221     {
1222         mBuffer.reset();
1223         mBufferSize = 0;
1224         return angle::Result::Continue;
1225     }
1226 
1227     D3D11_BUFFER_DESC bufferDesc;
1228     FillBufferDesc(&bufferDesc, mRenderer, mUsage, static_cast<unsigned int>(size));
1229 
1230     d3d11::Buffer newBuffer;
1231     ANGLE_TRY(
1232         mRenderer->allocateResource(SafeGetImplAs<Context11>(context), bufferDesc, &newBuffer));
1233     newBuffer.setDebugName("Buffer11::NativeStorage");
1234 
1235     if (mBuffer.valid() && preserveData)
1236     {
1237         // We don't call resize if the buffer is big enough already.
1238         ASSERT(mBufferSize <= size);
1239 
1240         D3D11_BOX srcBox;
1241         srcBox.left   = 0;
1242         srcBox.right  = static_cast<unsigned int>(mBufferSize);
1243         srcBox.top    = 0;
1244         srcBox.bottom = 1;
1245         srcBox.front  = 0;
1246         srcBox.back   = 1;
1247 
1248         ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
1249         deviceContext->CopySubresourceRegion(newBuffer.get(), 0, 0, 0, 0, mBuffer.get(), 0,
1250                                              &srcBox);
1251     }
1252 
1253     // No longer need the old buffer
1254     mBuffer = std::move(newBuffer);
1255 
1256     mBufferSize = bufferDesc.ByteWidth;
1257 
1258     // Free the SRVs.
1259     clearSRVs();
1260 
1261     // Free the UAVs.
1262     clearUAVs();
1263 
1264     // Notify that the storage has changed.
1265     if (mOnStorageChanged)
1266     {
1267         mOnStorageChanged->onStateChange(angle::SubjectMessage::SubjectChanged);
1268     }
1269 
1270     return angle::Result::Continue;
1271 }
1272 
1273 // static
FillBufferDesc(D3D11_BUFFER_DESC * bufferDesc,Renderer11 * renderer,BufferUsage usage,unsigned int bufferSize)1274 void Buffer11::NativeStorage::FillBufferDesc(D3D11_BUFFER_DESC *bufferDesc,
1275                                              Renderer11 *renderer,
1276                                              BufferUsage usage,
1277                                              unsigned int bufferSize)
1278 {
1279     bufferDesc->ByteWidth           = bufferSize;
1280     bufferDesc->MiscFlags           = 0;
1281     bufferDesc->StructureByteStride = 0;
1282 
1283     switch (usage)
1284     {
1285         case BUFFER_USAGE_STAGING:
1286             bufferDesc->Usage          = D3D11_USAGE_STAGING;
1287             bufferDesc->BindFlags      = 0;
1288             bufferDesc->CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
1289             break;
1290 
1291         case BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK:
1292             bufferDesc->Usage     = D3D11_USAGE_DEFAULT;
1293             bufferDesc->BindFlags = D3D11_BIND_VERTEX_BUFFER;
1294 
1295             if (renderer->isES3Capable())
1296             {
1297                 bufferDesc->BindFlags |= D3D11_BIND_STREAM_OUTPUT;
1298             }
1299 
1300             bufferDesc->CPUAccessFlags = 0;
1301             break;
1302 
1303         case BUFFER_USAGE_INDEX:
1304             bufferDesc->Usage          = D3D11_USAGE_DEFAULT;
1305             bufferDesc->BindFlags      = D3D11_BIND_INDEX_BUFFER;
1306             bufferDesc->CPUAccessFlags = 0;
1307             break;
1308 
1309         case BUFFER_USAGE_INDIRECT:
1310             bufferDesc->MiscFlags      = D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS;
1311             bufferDesc->Usage          = D3D11_USAGE_DEFAULT;
1312             bufferDesc->BindFlags      = 0;
1313             bufferDesc->CPUAccessFlags = 0;
1314             break;
1315 
1316         case BUFFER_USAGE_PIXEL_UNPACK:
1317             bufferDesc->Usage          = D3D11_USAGE_DEFAULT;
1318             bufferDesc->BindFlags      = D3D11_BIND_SHADER_RESOURCE;
1319             bufferDesc->CPUAccessFlags = 0;
1320             break;
1321 
1322         case BUFFER_USAGE_UNIFORM:
1323             bufferDesc->Usage          = D3D11_USAGE_DYNAMIC;
1324             bufferDesc->BindFlags      = D3D11_BIND_CONSTANT_BUFFER;
1325             bufferDesc->CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1326 
1327             // Constant buffers must be of a limited size, and aligned to 16 byte boundaries
1328             // For our purposes we ignore any buffer data past the maximum constant buffer size
1329             bufferDesc->ByteWidth = roundUpPow2(bufferDesc->ByteWidth, 16u);
1330 
1331             // Note: it seems that D3D11 allows larger buffers on some platforms, but not all.
1332             // (Windows 10 seems to allow larger constant buffers, but not Windows 7)
1333             if (!renderer->getRenderer11DeviceCaps().supportsConstantBufferOffsets)
1334             {
1335                 bufferDesc->ByteWidth = std::min<UINT>(
1336                     bufferDesc->ByteWidth,
1337                     static_cast<UINT>(renderer->getNativeCaps().maxUniformBlockSize));
1338             }
1339             break;
1340 
1341         case BUFFER_USAGE_RAW_UAV:
1342             bufferDesc->MiscFlags      = D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
1343             bufferDesc->BindFlags      = D3D11_BIND_UNORDERED_ACCESS;
1344             bufferDesc->Usage          = D3D11_USAGE_DEFAULT;
1345             bufferDesc->CPUAccessFlags = 0;
1346             break;
1347 
1348         default:
1349             UNREACHABLE();
1350     }
1351 }
1352 
map(const gl::Context * context,size_t offset,size_t length,GLbitfield access,uint8_t ** mapPointerOut)1353 angle::Result Buffer11::NativeStorage::map(const gl::Context *context,
1354                                            size_t offset,
1355                                            size_t length,
1356                                            GLbitfield access,
1357                                            uint8_t **mapPointerOut)
1358 {
1359     ASSERT(isCPUAccessible(access));
1360 
1361     D3D11_MAPPED_SUBRESOURCE mappedResource;
1362     D3D11_MAP d3dMapType = gl_d3d11::GetD3DMapTypeFromBits(mUsage, access);
1363     UINT d3dMapFlag = ((access & GL_MAP_UNSYNCHRONIZED_BIT) != 0 ? D3D11_MAP_FLAG_DO_NOT_WAIT : 0);
1364 
1365     ANGLE_TRY(
1366         mRenderer->mapResource(context, mBuffer.get(), 0, d3dMapType, d3dMapFlag, &mappedResource));
1367     ASSERT(mappedResource.pData);
1368     *mapPointerOut = static_cast<uint8_t *>(mappedResource.pData) + offset;
1369     return angle::Result::Continue;
1370 }
1371 
unmap()1372 void Buffer11::NativeStorage::unmap()
1373 {
1374     ASSERT(isCPUAccessible(GL_MAP_WRITE_BIT) || isCPUAccessible(GL_MAP_READ_BIT));
1375     ID3D11DeviceContext *context = mRenderer->getDeviceContext();
1376     context->Unmap(mBuffer.get(), 0);
1377 }
1378 
getSRVForFormat(const gl::Context * context,DXGI_FORMAT srvFormat,const d3d11::ShaderResourceView ** srvOut)1379 angle::Result Buffer11::NativeStorage::getSRVForFormat(const gl::Context *context,
1380                                                        DXGI_FORMAT srvFormat,
1381                                                        const d3d11::ShaderResourceView **srvOut)
1382 {
1383     auto bufferSRVIt = mBufferResourceViews.find(srvFormat);
1384 
1385     if (bufferSRVIt != mBufferResourceViews.end())
1386     {
1387         *srvOut = &bufferSRVIt->second;
1388         return angle::Result::Continue;
1389     }
1390 
1391     const d3d11::DXGIFormatSize &dxgiFormatInfo = d3d11::GetDXGIFormatSizeInfo(srvFormat);
1392 
1393     D3D11_SHADER_RESOURCE_VIEW_DESC bufferSRVDesc;
1394     bufferSRVDesc.Buffer.ElementOffset = 0;
1395     bufferSRVDesc.Buffer.ElementWidth  = static_cast<UINT>(mBufferSize) / dxgiFormatInfo.pixelBytes;
1396     bufferSRVDesc.ViewDimension        = D3D11_SRV_DIMENSION_BUFFER;
1397     bufferSRVDesc.Format               = srvFormat;
1398 
1399     ANGLE_TRY(mRenderer->allocateResource(GetImplAs<Context11>(context), bufferSRVDesc,
1400                                           mBuffer.get(), &mBufferResourceViews[srvFormat]));
1401 
1402     *srvOut = &mBufferResourceViews[srvFormat];
1403     return angle::Result::Continue;
1404 }
1405 
getRawUAV(const gl::Context * context,unsigned int offset,unsigned int size,d3d11::UnorderedAccessView ** uavOut)1406 angle::Result Buffer11::NativeStorage::getRawUAV(const gl::Context *context,
1407                                                  unsigned int offset,
1408                                                  unsigned int size,
1409                                                  d3d11::UnorderedAccessView **uavOut)
1410 {
1411     ASSERT(offset + size <= mBufferSize);
1412 
1413     auto bufferRawUAV = mBufferRawUAVs.find({offset, size});
1414     if (bufferRawUAV != mBufferRawUAVs.end())
1415     {
1416         *uavOut = &bufferRawUAV->second;
1417         return angle::Result::Continue;
1418     }
1419 
1420     D3D11_UNORDERED_ACCESS_VIEW_DESC bufferUAVDesc;
1421 
1422     // DXGI_FORMAT_R32_TYPELESS uses 4 bytes per element
1423     constexpr int kBytesToElement     = 4;
1424     bufferUAVDesc.Buffer.FirstElement = offset / kBytesToElement;
1425     bufferUAVDesc.Buffer.NumElements  = size / kBytesToElement;
1426     bufferUAVDesc.Buffer.Flags        = D3D11_BUFFER_UAV_FLAG_RAW;
1427     bufferUAVDesc.Format = DXGI_FORMAT_R32_TYPELESS;  // Format must be DXGI_FORMAT_R32_TYPELESS,
1428                                                       // when creating Raw Unordered Access View
1429     bufferUAVDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
1430 
1431     ANGLE_TRY(mRenderer->allocateResource(GetImplAs<Context11>(context), bufferUAVDesc,
1432                                           mBuffer.get(), &mBufferRawUAVs[{offset, size}]));
1433     *uavOut = &mBufferRawUAVs[{offset, size}];
1434     return angle::Result::Continue;
1435 }
1436 
clearSRVs()1437 void Buffer11::NativeStorage::clearSRVs()
1438 {
1439     mBufferResourceViews.clear();
1440 }
1441 
clearUAVs()1442 void Buffer11::NativeStorage::clearUAVs()
1443 {
1444     mBufferRawUAVs.clear();
1445 }
1446 
StructuredBufferStorage(Renderer11 * renderer,BufferUsage usage,const angle::Subject * onStorageChanged)1447 Buffer11::StructuredBufferStorage::StructuredBufferStorage(Renderer11 *renderer,
1448                                                            BufferUsage usage,
1449                                                            const angle::Subject *onStorageChanged)
1450     : NativeStorage(renderer, usage, onStorageChanged), mStructuredBufferResourceView()
1451 {}
1452 
~StructuredBufferStorage()1453 Buffer11::StructuredBufferStorage::~StructuredBufferStorage()
1454 {
1455     mStructuredBufferResourceView.reset();
1456 }
1457 
resizeStructuredBuffer(const gl::Context * context,unsigned int size,unsigned int structureByteStride)1458 angle::Result Buffer11::StructuredBufferStorage::resizeStructuredBuffer(
1459     const gl::Context *context,
1460     unsigned int size,
1461     unsigned int structureByteStride)
1462 {
1463     if (size == 0)
1464     {
1465         mBuffer.reset();
1466         mBufferSize = 0;
1467         return angle::Result::Continue;
1468     }
1469 
1470     D3D11_BUFFER_DESC bufferDesc;
1471     bufferDesc.ByteWidth           = size;
1472     bufferDesc.MiscFlags           = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
1473     bufferDesc.StructureByteStride = structureByteStride;
1474     bufferDesc.Usage               = D3D11_USAGE_DYNAMIC;
1475     bufferDesc.BindFlags           = D3D11_BIND_SHADER_RESOURCE;
1476     bufferDesc.CPUAccessFlags      = D3D11_CPU_ACCESS_WRITE;
1477 
1478     d3d11::Buffer newBuffer;
1479     ANGLE_TRY(
1480         mRenderer->allocateResource(SafeGetImplAs<Context11>(context), bufferDesc, &newBuffer));
1481     newBuffer.setDebugName("Buffer11::StructuredBufferStorage");
1482 
1483     // No longer need the old buffer
1484     mBuffer = std::move(newBuffer);
1485 
1486     mBufferSize = static_cast<size_t>(bufferDesc.ByteWidth);
1487 
1488     mStructuredBufferResourceView.reset();
1489 
1490     // Notify that the storage has changed.
1491     if (mOnStorageChanged)
1492     {
1493         mOnStorageChanged->onStateChange(angle::SubjectMessage::SubjectChanged);
1494     }
1495 
1496     return angle::Result::Continue;
1497 }
1498 
getStructuredBufferRangeSRV(const gl::Context * context,unsigned int offset,unsigned int size,unsigned int structureByteStride,const d3d11::ShaderResourceView ** srvOut)1499 angle::Result Buffer11::StructuredBufferStorage::getStructuredBufferRangeSRV(
1500     const gl::Context *context,
1501     unsigned int offset,
1502     unsigned int size,
1503     unsigned int structureByteStride,
1504     const d3d11::ShaderResourceView **srvOut)
1505 {
1506     if (mStructuredBufferResourceView.valid())
1507     {
1508         *srvOut = &mStructuredBufferResourceView;
1509         return angle::Result::Continue;
1510     }
1511 
1512     D3D11_SHADER_RESOURCE_VIEW_DESC bufferSRVDesc = {};
1513     bufferSRVDesc.BufferEx.NumElements = structureByteStride == 0u ? 1 : size / structureByteStride;
1514     bufferSRVDesc.BufferEx.FirstElement = 0;
1515     bufferSRVDesc.BufferEx.Flags        = 0;
1516     bufferSRVDesc.ViewDimension         = D3D11_SRV_DIMENSION_BUFFEREX;
1517     bufferSRVDesc.Format                = DXGI_FORMAT_UNKNOWN;
1518 
1519     ANGLE_TRY(mRenderer->allocateResource(GetImplAs<Context11>(context), bufferSRVDesc,
1520                                           mBuffer.get(), &mStructuredBufferResourceView));
1521 
1522     *srvOut = &mStructuredBufferResourceView;
1523     return angle::Result::Continue;
1524 }
1525 
1526 // Buffer11::EmulatedIndexStorage implementation
EmulatedIndexedStorage(Renderer11 * renderer)1527 Buffer11::EmulatedIndexedStorage::EmulatedIndexedStorage(Renderer11 *renderer)
1528     : BufferStorage(renderer, BUFFER_USAGE_EMULATED_INDEXED_VERTEX), mBuffer()
1529 {}
1530 
~EmulatedIndexedStorage()1531 Buffer11::EmulatedIndexedStorage::~EmulatedIndexedStorage() {}
1532 
getBuffer(const gl::Context * context,SourceIndexData * indexInfo,const TranslatedAttribute & attribute,GLint startVertex,const d3d11::Buffer ** bufferOut)1533 angle::Result Buffer11::EmulatedIndexedStorage::getBuffer(const gl::Context *context,
1534                                                           SourceIndexData *indexInfo,
1535                                                           const TranslatedAttribute &attribute,
1536                                                           GLint startVertex,
1537                                                           const d3d11::Buffer **bufferOut)
1538 {
1539     Context11 *context11 = GetImplAs<Context11>(context);
1540 
1541     // If a change in the indices applied from the last draw call is detected, then the emulated
1542     // indexed buffer needs to be invalidated.  After invalidation, the change detected flag should
1543     // be cleared to avoid unnecessary recreation of the buffer.
1544     if (!mBuffer.valid() || indexInfo->srcIndicesChanged)
1545     {
1546         mBuffer.reset();
1547 
1548         // Copy the source index data. This ensures that the lifetime of the indices pointer
1549         // stays with this storage until the next time we invalidate.
1550         size_t indicesDataSize = 0;
1551         switch (indexInfo->srcIndexType)
1552         {
1553             case gl::DrawElementsType::UnsignedInt:
1554                 indicesDataSize = sizeof(GLuint) * indexInfo->srcCount;
1555                 break;
1556             case gl::DrawElementsType::UnsignedShort:
1557                 indicesDataSize = sizeof(GLushort) * indexInfo->srcCount;
1558                 break;
1559             case gl::DrawElementsType::UnsignedByte:
1560                 indicesDataSize = sizeof(GLubyte) * indexInfo->srcCount;
1561                 break;
1562             default:
1563                 indicesDataSize = sizeof(GLushort) * indexInfo->srcCount;
1564                 break;
1565         }
1566 
1567         ANGLE_CHECK_GL_ALLOC(context11, mIndicesMemoryBuffer.resize(indicesDataSize));
1568 
1569         memcpy(mIndicesMemoryBuffer.data(), indexInfo->srcIndices, indicesDataSize);
1570 
1571         indexInfo->srcIndicesChanged = false;
1572     }
1573 
1574     if (!mBuffer.valid())
1575     {
1576         unsigned int offset = 0;
1577         ANGLE_TRY(attribute.computeOffset(context, startVertex, &offset));
1578 
1579         // Expand the memory storage upon request and cache the results.
1580         unsigned int expandedDataSize =
1581             static_cast<unsigned int>((indexInfo->srcCount * attribute.stride) + offset);
1582         angle::MemoryBuffer expandedData;
1583         ANGLE_CHECK_GL_ALLOC(context11, expandedData.resize(expandedDataSize));
1584 
1585         // Clear the contents of the allocated buffer
1586         ZeroMemory(expandedData.data(), expandedDataSize);
1587 
1588         uint8_t *curr      = expandedData.data();
1589         const uint8_t *ptr = static_cast<const uint8_t *>(indexInfo->srcIndices);
1590 
1591         // Ensure that we start in the correct place for the emulated data copy operation to
1592         // maintain offset behaviors.
1593         curr += offset;
1594 
1595         ReadIndexValueFunction readIndexValue = ReadIndexValueFromIndices<GLushort>;
1596 
1597         switch (indexInfo->srcIndexType)
1598         {
1599             case gl::DrawElementsType::UnsignedInt:
1600                 readIndexValue = ReadIndexValueFromIndices<GLuint>;
1601                 break;
1602             case gl::DrawElementsType::UnsignedShort:
1603                 readIndexValue = ReadIndexValueFromIndices<GLushort>;
1604                 break;
1605             case gl::DrawElementsType::UnsignedByte:
1606                 readIndexValue = ReadIndexValueFromIndices<GLubyte>;
1607                 break;
1608             default:
1609                 UNREACHABLE();
1610                 return angle::Result::Stop;
1611         }
1612 
1613         // Iterate over the cached index data and copy entries indicated into the emulated buffer.
1614         for (GLuint i = 0; i < indexInfo->srcCount; i++)
1615         {
1616             GLuint idx = readIndexValue(ptr, i);
1617             memcpy(curr, mMemoryBuffer.data() + (attribute.stride * idx), attribute.stride);
1618             curr += attribute.stride;
1619         }
1620 
1621         // Finally, initialize the emulated indexed native storage object with the newly copied data
1622         // and free the temporary buffers used.
1623         D3D11_BUFFER_DESC bufferDesc;
1624         bufferDesc.ByteWidth           = expandedDataSize;
1625         bufferDesc.MiscFlags           = 0;
1626         bufferDesc.StructureByteStride = 0;
1627         bufferDesc.Usage               = D3D11_USAGE_DEFAULT;
1628         bufferDesc.BindFlags           = D3D11_BIND_VERTEX_BUFFER;
1629         bufferDesc.CPUAccessFlags      = 0;
1630 
1631         D3D11_SUBRESOURCE_DATA subResourceData = {expandedData.data(), 0, 0};
1632 
1633         ANGLE_TRY(mRenderer->allocateResource(GetImplAs<Context11>(context), bufferDesc,
1634                                               &subResourceData, &mBuffer));
1635         mBuffer.setDebugName("Buffer11::EmulatedIndexedStorage");
1636     }
1637 
1638     *bufferOut = &mBuffer;
1639     return angle::Result::Continue;
1640 }
1641 
copyFromStorage(const gl::Context * context,BufferStorage * source,size_t sourceOffset,size_t size,size_t destOffset,CopyResult * resultOut)1642 angle::Result Buffer11::EmulatedIndexedStorage::copyFromStorage(const gl::Context *context,
1643                                                                 BufferStorage *source,
1644                                                                 size_t sourceOffset,
1645                                                                 size_t size,
1646                                                                 size_t destOffset,
1647                                                                 CopyResult *resultOut)
1648 {
1649     ASSERT(source->isCPUAccessible(GL_MAP_READ_BIT));
1650     uint8_t *sourceData = nullptr;
1651     ANGLE_TRY(source->map(context, sourceOffset, size, GL_MAP_READ_BIT, &sourceData));
1652     ASSERT(destOffset + size <= mMemoryBuffer.size());
1653     memcpy(mMemoryBuffer.data() + destOffset, sourceData, size);
1654     source->unmap();
1655     *resultOut = CopyResult::RECREATED;
1656     return angle::Result::Continue;
1657 }
1658 
resize(const gl::Context * context,size_t size,bool preserveData)1659 angle::Result Buffer11::EmulatedIndexedStorage::resize(const gl::Context *context,
1660                                                        size_t size,
1661                                                        bool preserveData)
1662 {
1663     if (mMemoryBuffer.size() < size)
1664     {
1665         Context11 *context11 = GetImplAs<Context11>(context);
1666         ANGLE_CHECK_GL_ALLOC(context11, mMemoryBuffer.resize(size));
1667         mBufferSize = size;
1668     }
1669 
1670     return angle::Result::Continue;
1671 }
1672 
map(const gl::Context * context,size_t offset,size_t length,GLbitfield access,uint8_t ** mapPointerOut)1673 angle::Result Buffer11::EmulatedIndexedStorage::map(const gl::Context *context,
1674                                                     size_t offset,
1675                                                     size_t length,
1676                                                     GLbitfield access,
1677                                                     uint8_t **mapPointerOut)
1678 {
1679     ASSERT(!mMemoryBuffer.empty() && offset + length <= mMemoryBuffer.size());
1680     *mapPointerOut = mMemoryBuffer.data() + offset;
1681     return angle::Result::Continue;
1682 }
1683 
unmap()1684 void Buffer11::EmulatedIndexedStorage::unmap()
1685 {
1686     // No-op
1687 }
1688 
1689 // Buffer11::PackStorage implementation
1690 
PackStorage(Renderer11 * renderer)1691 Buffer11::PackStorage::PackStorage(Renderer11 *renderer)
1692     : BufferStorage(renderer, BUFFER_USAGE_PIXEL_PACK), mStagingTexture(), mDataModified(false)
1693 {}
1694 
~PackStorage()1695 Buffer11::PackStorage::~PackStorage() {}
1696 
copyFromStorage(const gl::Context * context,BufferStorage * source,size_t sourceOffset,size_t size,size_t destOffset,CopyResult * resultOut)1697 angle::Result Buffer11::PackStorage::copyFromStorage(const gl::Context *context,
1698                                                      BufferStorage *source,
1699                                                      size_t sourceOffset,
1700                                                      size_t size,
1701                                                      size_t destOffset,
1702                                                      CopyResult *resultOut)
1703 {
1704     ANGLE_TRY(flushQueuedPackCommand(context));
1705 
1706     // For all use cases of pack buffers, we must copy through a readable buffer.
1707     ASSERT(source->isCPUAccessible(GL_MAP_READ_BIT));
1708     uint8_t *sourceData = nullptr;
1709     ANGLE_TRY(source->map(context, sourceOffset, size, GL_MAP_READ_BIT, &sourceData));
1710     ASSERT(destOffset + size <= mMemoryBuffer.size());
1711     memcpy(mMemoryBuffer.data() + destOffset, sourceData, size);
1712     source->unmap();
1713     *resultOut = CopyResult::NOT_RECREATED;
1714     return angle::Result::Continue;
1715 }
1716 
resize(const gl::Context * context,size_t size,bool preserveData)1717 angle::Result Buffer11::PackStorage::resize(const gl::Context *context,
1718                                             size_t size,
1719                                             bool preserveData)
1720 {
1721     if (size != mBufferSize)
1722     {
1723         Context11 *context11 = GetImplAs<Context11>(context);
1724         ANGLE_CHECK_GL_ALLOC(context11, mMemoryBuffer.resize(size));
1725         mBufferSize = size;
1726     }
1727 
1728     return angle::Result::Continue;
1729 }
1730 
map(const gl::Context * context,size_t offset,size_t length,GLbitfield access,uint8_t ** mapPointerOut)1731 angle::Result Buffer11::PackStorage::map(const gl::Context *context,
1732                                          size_t offset,
1733                                          size_t length,
1734                                          GLbitfield access,
1735                                          uint8_t **mapPointerOut)
1736 {
1737     ASSERT(offset + length <= getSize());
1738     // TODO: fast path
1739     //  We might be able to optimize out one or more memcpy calls by detecting when
1740     //  and if D3D packs the staging texture memory identically to how we would fill
1741     //  the pack buffer according to the current pack state.
1742 
1743     ANGLE_TRY(flushQueuedPackCommand(context));
1744 
1745     mDataModified = (mDataModified || (access & GL_MAP_WRITE_BIT) != 0);
1746 
1747     *mapPointerOut = mMemoryBuffer.data() + offset;
1748     return angle::Result::Continue;
1749 }
1750 
unmap()1751 void Buffer11::PackStorage::unmap()
1752 {
1753     // No-op
1754 }
1755 
packPixels(const gl::Context * context,const gl::FramebufferAttachment & readAttachment,const PackPixelsParams & params)1756 angle::Result Buffer11::PackStorage::packPixels(const gl::Context *context,
1757                                                 const gl::FramebufferAttachment &readAttachment,
1758                                                 const PackPixelsParams &params)
1759 {
1760     ANGLE_TRY(flushQueuedPackCommand(context));
1761 
1762     RenderTarget11 *renderTarget = nullptr;
1763     ANGLE_TRY(readAttachment.getRenderTarget(context, 0, &renderTarget));
1764 
1765     const TextureHelper11 &srcTexture = renderTarget->getTexture();
1766     ASSERT(srcTexture.valid());
1767     unsigned int srcSubresource = renderTarget->getSubresourceIndex();
1768 
1769     mQueuedPackCommand.reset(new PackPixelsParams(params));
1770 
1771     gl::Extents srcTextureSize(params.area.width, params.area.height, 1);
1772     if (!mStagingTexture.get() || mStagingTexture.getFormat() != srcTexture.getFormat() ||
1773         mStagingTexture.getExtents() != srcTextureSize)
1774     {
1775         ANGLE_TRY(mRenderer->createStagingTexture(context, srcTexture.getTextureType(),
1776                                                   srcTexture.getFormatSet(), srcTextureSize,
1777                                                   StagingAccess::READ, &mStagingTexture));
1778     }
1779 
1780     // ReadPixels from multisampled FBOs isn't supported in current GL
1781     ASSERT(srcTexture.getSampleCount() <= 1);
1782 
1783     ID3D11DeviceContext *immediateContext = mRenderer->getDeviceContext();
1784     D3D11_BOX srcBox;
1785     srcBox.left   = params.area.x;
1786     srcBox.right  = params.area.x + params.area.width;
1787     srcBox.top    = params.area.y;
1788     srcBox.bottom = params.area.y + params.area.height;
1789 
1790     // Select the correct layer from a 3D attachment
1791     srcBox.front = 0;
1792     if (mStagingTexture.is3D())
1793     {
1794         srcBox.front = static_cast<UINT>(readAttachment.layer());
1795     }
1796     srcBox.back = srcBox.front + 1;
1797 
1798     // Asynchronous copy
1799     immediateContext->CopySubresourceRegion(mStagingTexture.get(), 0, 0, 0, 0, srcTexture.get(),
1800                                             srcSubresource, &srcBox);
1801 
1802     return angle::Result::Continue;
1803 }
1804 
flushQueuedPackCommand(const gl::Context * context)1805 angle::Result Buffer11::PackStorage::flushQueuedPackCommand(const gl::Context *context)
1806 {
1807     ASSERT(mMemoryBuffer.size() > 0);
1808 
1809     if (mQueuedPackCommand)
1810     {
1811         ANGLE_TRY(mRenderer->packPixels(context, mStagingTexture, *mQueuedPackCommand,
1812                                         mMemoryBuffer.data()));
1813         mQueuedPackCommand.reset(nullptr);
1814     }
1815 
1816     return angle::Result::Continue;
1817 }
1818 
1819 // Buffer11::SystemMemoryStorage implementation
1820 
SystemMemoryStorage(Renderer11 * renderer)1821 Buffer11::SystemMemoryStorage::SystemMemoryStorage(Renderer11 *renderer)
1822     : Buffer11::BufferStorage(renderer, BUFFER_USAGE_SYSTEM_MEMORY)
1823 {}
1824 
copyFromStorage(const gl::Context * context,BufferStorage * source,size_t sourceOffset,size_t size,size_t destOffset,CopyResult * resultOut)1825 angle::Result Buffer11::SystemMemoryStorage::copyFromStorage(const gl::Context *context,
1826                                                              BufferStorage *source,
1827                                                              size_t sourceOffset,
1828                                                              size_t size,
1829                                                              size_t destOffset,
1830                                                              CopyResult *resultOut)
1831 {
1832     ASSERT(source->isCPUAccessible(GL_MAP_READ_BIT));
1833     uint8_t *sourceData = nullptr;
1834     ANGLE_TRY(source->map(context, sourceOffset, size, GL_MAP_READ_BIT, &sourceData));
1835     ASSERT(destOffset + size <= mSystemCopy.size());
1836     memcpy(mSystemCopy.data() + destOffset, sourceData, size);
1837     source->unmap();
1838     *resultOut = CopyResult::RECREATED;
1839     return angle::Result::Continue;
1840 }
1841 
resize(const gl::Context * context,size_t size,bool preserveData)1842 angle::Result Buffer11::SystemMemoryStorage::resize(const gl::Context *context,
1843                                                     size_t size,
1844                                                     bool preserveData)
1845 {
1846     if (mSystemCopy.size() < size)
1847     {
1848         Context11 *context11 = GetImplAs<Context11>(context);
1849         ANGLE_CHECK_GL_ALLOC(context11, mSystemCopy.resize(size));
1850         mBufferSize = size;
1851     }
1852 
1853     return angle::Result::Continue;
1854 }
1855 
map(const gl::Context * context,size_t offset,size_t length,GLbitfield access,uint8_t ** mapPointerOut)1856 angle::Result Buffer11::SystemMemoryStorage::map(const gl::Context *context,
1857                                                  size_t offset,
1858                                                  size_t length,
1859                                                  GLbitfield access,
1860                                                  uint8_t **mapPointerOut)
1861 {
1862     ASSERT(!mSystemCopy.empty() && offset + length <= mSystemCopy.size());
1863     *mapPointerOut = mSystemCopy.data() + offset;
1864     return angle::Result::Continue;
1865 }
1866 
unmap()1867 void Buffer11::SystemMemoryStorage::unmap()
1868 {
1869     // No-op
1870 }
1871 }  // namespace rx
1872