Implement buffer lazy initialization before writeBuffer

This patch implements buffer lazy initialization before writeBuffer():
if the buffer is not initialized and writeBuffer() doesn't cover the
whole buffer, the buffer will be cleared to 0, otherwise the buffer
shouldn't be cleared.

This patch also introduces a toggle LazyClearBufferOnFirstUse for the
development of buffer lazy initialization: before buffer lazy
initialization being completely supported, all the related code will
only be enabled behind this toggle to prevent the buffers with valid
content being unexpectedly cleared.

BUG=dawn:414
TEST=dawn_end2end_tests

Change-Id: I99a2aa98ca4b9b21d69c6b32080afb525e2c4ad3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/24041
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Jiawei Shao
2020-07-06 08:24:30 +00:00
committed by Commit Bot service account
parent 59ccb1f6de
commit 80f927d763
20 changed files with 392 additions and 142 deletions

View File

@@ -24,7 +24,7 @@ namespace dawn_native { namespace opengl {
: BufferBase(device, descriptor) {
// TODO(cwallez@chromium.org): Have a global "zero" buffer instead of creating a new 4-byte
// buffer?
uint64_t size = std::max(GetSize(), uint64_t(4u));
uint64_t size = GetAppliedSize();
device->gl.GenBuffers(1, &mBuffer);
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
@@ -45,6 +45,27 @@ namespace dawn_native { namespace opengl {
return mBuffer;
}
uint64_t Buffer::GetAppliedSize() const {
// TODO(cwallez@chromium.org): Have a global "zero" buffer instead of creating a new 4-byte
// buffer?
return std::max(GetSize(), uint64_t(4u));
}
void Buffer::ClearBufferContentsToZero() {
ASSERT(GetDevice()->IsToggleEnabled(Toggle::LazyClearBufferOnFirstUse));
ASSERT(!IsDataInitialized());
const uint64_t size = GetAppliedSize();
Device* device = ToBackend(GetDevice());
const std::vector<uint8_t> clearValues(size, 0u);
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
device->gl.BufferSubData(GL_ARRAY_BUFFER, 0, size, clearValues.data());
SetIsDataInitialized();
device->IncrementLazyClearCountForTesting();
}
bool Buffer::IsMapWritable() const {
// TODO(enga): All buffers in GL can be mapped. Investigate if mapping them will cause the
// driver to migrate it to shared memory.

View File

@@ -29,6 +29,8 @@ namespace dawn_native { namespace opengl {
GLuint GetHandle() const;
void ClearBufferContentsToZero();
private:
~Buffer() override;
// Dawn API
@@ -40,6 +42,7 @@ namespace dawn_native { namespace opengl {
bool IsMapWritable() const override;
MaybeError MapAtCreationImpl(uint8_t** mappedPointer) override;
void* GetMappedPointerImpl() override;
uint64_t GetAppliedSize() const;
GLuint mBuffer = 0;
void* mMappedData = nullptr;

View File

@@ -44,6 +44,17 @@ namespace dawn_native { namespace opengl {
size_t size) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
// TODO(jiawei.shao@intel.com): use Toggle::LazyClearResourceOnFirstUse when the support of
// buffer lazy initialization is completed.
if (GetDevice()->IsToggleEnabled(Toggle::LazyClearBufferOnFirstUse) &&
!buffer->IsDataInitialized()) {
if (buffer->IsFullBufferRange(bufferOffset, size)) {
buffer->SetIsDataInitialized();
} else {
ToBackend(buffer)->ClearBufferContentsToZero();
}
}
gl.BindBuffer(GL_ARRAY_BUFFER, ToBackend(buffer)->GetHandle());
gl.BufferSubData(GL_ARRAY_BUFFER, bufferOffset, size, data);
return {};