2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-07-14 21:27:32 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/opengl/BufferGL.h"
|
2017-07-14 21:27:32 +00:00
|
|
|
|
2020-07-28 01:58:50 +00:00
|
|
|
#include "dawn_native/CommandBuffer.h"
|
2018-08-22 13:37:29 +00:00
|
|
|
#include "dawn_native/opengl/DeviceGL.h"
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native { namespace opengl {
|
2017-07-14 21:27:32 +00:00
|
|
|
|
|
|
|
// Buffer
|
|
|
|
|
2020-08-23 06:08:05 +00:00
|
|
|
// static
|
|
|
|
ResultOrError<Ref<Buffer>> Buffer::CreateInternalBuffer(Device* device,
|
|
|
|
const BufferDescriptor* descriptor,
|
|
|
|
bool shouldLazyClear) {
|
|
|
|
Ref<Buffer> buffer = AcquireRef(new Buffer(device, descriptor, shouldLazyClear));
|
|
|
|
if (descriptor->mappedAtCreation) {
|
2020-09-01 08:08:57 +00:00
|
|
|
DAWN_TRY(buffer->MapAtCreationInternal());
|
2020-08-23 06:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(buffer);
|
|
|
|
}
|
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
Buffer::Buffer(Device* device, const BufferDescriptor* descriptor)
|
|
|
|
: BufferBase(device, descriptor) {
|
2020-07-06 08:24:30 +00:00
|
|
|
uint64_t size = GetAppliedSize();
|
2020-06-11 11:07:05 +00:00
|
|
|
|
2019-06-13 10:22:32 +00:00
|
|
|
device->gl.GenBuffers(1, &mBuffer);
|
|
|
|
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
2020-06-13 01:04:53 +00:00
|
|
|
|
2020-09-01 08:08:57 +00:00
|
|
|
// The buffers with mappedAtCreation == true will be initialized in
|
|
|
|
// BufferBase::MapAtCreation().
|
|
|
|
if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting) &&
|
|
|
|
!descriptor->mappedAtCreation) {
|
2020-06-13 01:04:53 +00:00
|
|
|
std::vector<uint8_t> clearValues(size, 1u);
|
|
|
|
device->gl.BufferData(GL_ARRAY_BUFFER, size, clearValues.data(), GL_STATIC_DRAW);
|
|
|
|
} else {
|
|
|
|
device->gl.BufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
|
|
|
|
}
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 06:08:05 +00:00
|
|
|
Buffer::Buffer(Device* device, const BufferDescriptor* descriptor, bool shouldLazyClear)
|
|
|
|
: Buffer(device, descriptor) {
|
|
|
|
if (!shouldLazyClear) {
|
|
|
|
SetIsDataInitialized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:05:22 +00:00
|
|
|
Buffer::~Buffer() {
|
2019-04-01 19:49:04 +00:00
|
|
|
DestroyInternal();
|
2019-03-11 17:05:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:27:32 +00:00
|
|
|
GLuint Buffer::GetHandle() const {
|
2017-11-23 19:24:20 +00:00
|
|
|
return mBuffer;
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:24:30 +00:00
|
|
|
uint64_t Buffer::GetAppliedSize() const {
|
|
|
|
return std::max(GetSize(), uint64_t(4u));
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:15:22 +00:00
|
|
|
void Buffer::EnsureDataInitialized() {
|
|
|
|
if (IsDataInitialized() ||
|
2020-09-02 00:21:08 +00:00
|
|
|
!GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
|
2020-07-09 09:15:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeToZero();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Buffer::EnsureDataInitializedAsDestination(uint64_t offset, uint64_t size) {
|
|
|
|
if (IsDataInitialized() ||
|
2020-09-02 00:21:08 +00:00
|
|
|
!GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
|
2020-07-09 09:15:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsFullBufferRange(offset, size)) {
|
|
|
|
SetIsDataInitialized();
|
|
|
|
} else {
|
|
|
|
InitializeToZero();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 01:58:50 +00:00
|
|
|
void Buffer::EnsureDataInitializedAsDestination(const CopyTextureToBufferCmd* copy) {
|
|
|
|
if (IsDataInitialized() ||
|
2020-09-02 00:21:08 +00:00
|
|
|
!GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
|
2020-07-28 01:58:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsFullBufferOverwrittenInTextureToBufferCopy(copy)) {
|
|
|
|
SetIsDataInitialized();
|
|
|
|
} else {
|
|
|
|
InitializeToZero();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:15:22 +00:00
|
|
|
void Buffer::InitializeToZero() {
|
2020-09-02 00:21:08 +00:00
|
|
|
ASSERT(GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse));
|
2020-07-06 08:24:30 +00:00
|
|
|
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());
|
2020-07-09 09:15:22 +00:00
|
|
|
device->IncrementLazyClearCountForTesting();
|
2020-07-06 08:24:30 +00:00
|
|
|
|
|
|
|
SetIsDataInitialized();
|
|
|
|
}
|
|
|
|
|
2020-09-01 08:08:57 +00:00
|
|
|
bool Buffer::IsCPUWritableAtCreation() const {
|
2019-06-05 18:35:31 +00:00
|
|
|
// TODO(enga): All buffers in GL can be mapped. Investigate if mapping them will cause the
|
|
|
|
// driver to migrate it to shared memory.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:21:51 +00:00
|
|
|
MaybeError Buffer::MapAtCreationImpl() {
|
2019-06-13 10:22:32 +00:00
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
2020-11-24 20:57:23 +00:00
|
|
|
mMappedData = gl.MapBufferRange(GL_ARRAY_BUFFER, 0, GetSize(), GL_MAP_WRITE_BIT);
|
2019-05-15 18:55:22 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:30:14 +00:00
|
|
|
MaybeError Buffer::MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) {
|
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
|
|
|
|
// It is an error to map an empty range in OpenGL. We always have at least a 4-byte buffer
|
|
|
|
// so we extend the range to be 4 bytes.
|
|
|
|
if (size == 0) {
|
|
|
|
if (offset != 0) {
|
|
|
|
offset -= 4;
|
|
|
|
}
|
|
|
|
size = 4;
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:01:26 +00:00
|
|
|
EnsureDataInitialized();
|
|
|
|
|
2021-06-07 18:23:52 +00:00
|
|
|
// This does GPU->CPU synchronization, we could require a high
|
2020-07-14 12:30:14 +00:00
|
|
|
// version of OpenGL that would let us map the buffer unsynchronized.
|
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
|
|
|
void* mappedData = nullptr;
|
|
|
|
if (mode & wgpu::MapMode::Read) {
|
|
|
|
mappedData = gl.MapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_READ_BIT);
|
|
|
|
} else {
|
|
|
|
ASSERT(mode & wgpu::MapMode::Write);
|
|
|
|
mappedData = gl.MapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The frontend asks that the pointer returned by GetMappedPointerImpl is from the start of
|
|
|
|
// the resource but OpenGL gives us the pointer at offset. Remove the offset.
|
|
|
|
mMappedData = static_cast<uint8_t*>(mappedData) - offset;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-05-19 01:29:32 +00:00
|
|
|
void* Buffer::GetMappedPointerImpl() {
|
2020-07-14 12:30:14 +00:00
|
|
|
// The mapping offset has already been removed.
|
2020-05-19 01:29:32 +00:00
|
|
|
return mMappedData;
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:27:32 +00:00
|
|
|
void Buffer::UnmapImpl() {
|
2019-06-13 10:22:32 +00:00
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
|
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
|
|
|
gl.UnmapBuffer(GL_ARRAY_BUFFER);
|
2020-05-19 01:29:32 +00:00
|
|
|
mMappedData = nullptr;
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 17:05:22 +00:00
|
|
|
void Buffer::DestroyImpl() {
|
2019-06-13 10:22:32 +00:00
|
|
|
ToBackend(GetDevice())->gl.DeleteBuffers(1, &mBuffer);
|
2019-03-11 17:05:22 +00:00
|
|
|
mBuffer = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
}} // namespace dawn_native::opengl
|