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
|
|
|
|
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
|
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
Buffer::Buffer(Device* device, const BufferDescriptor* descriptor)
|
|
|
|
: BufferBase(device, descriptor) {
|
2020-06-11 11:07:05 +00:00
|
|
|
// 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));
|
|
|
|
|
2019-06-13 10:22:32 +00:00
|
|
|
device->gl.GenBuffers(1, &mBuffer);
|
|
|
|
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
2020-06-11 11:07:05 +00:00
|
|
|
device->gl.BufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-05 18:35:31 +00:00
|
|
|
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.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:55:22 +00:00
|
|
|
MaybeError Buffer::MapAtCreationImpl(uint8_t** mappedPointer) {
|
2019-06-13 10:22:32 +00:00
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
|
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
2020-05-19 01:29:32 +00:00
|
|
|
mMappedData = gl.MapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
|
|
|
*mappedPointer = reinterpret_cast<uint8_t*>(mMappedData);
|
2019-05-15 18:55:22 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-23 00:04:59 +00:00
|
|
|
MaybeError Buffer::MapReadAsyncImpl(uint32_t serial) {
|
2019-06-13 10:22:32 +00:00
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
|
2017-07-14 21:38:17 +00:00
|
|
|
// TODO(cwallez@chromium.org): this does GPU->CPU synchronization, we could require a high
|
|
|
|
// version of OpenGL that would let us map the buffer unsynchronized.
|
2019-06-13 10:22:32 +00:00
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
2020-05-19 01:29:32 +00:00
|
|
|
mMappedData = gl.MapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
|
2019-07-23 00:04:59 +00:00
|
|
|
return {};
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 00:04:59 +00:00
|
|
|
MaybeError Buffer::MapWriteAsyncImpl(uint32_t serial) {
|
2019-06-13 10:22:32 +00:00
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
|
2018-03-21 00:56:39 +00:00
|
|
|
// TODO(cwallez@chromium.org): this does GPU->CPU synchronization, we could require a high
|
|
|
|
// version of OpenGL that would let us map the buffer unsynchronized.
|
2019-06-13 10:22:32 +00:00
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
2020-05-19 01:29:32 +00:00
|
|
|
mMappedData = gl.MapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2019-07-23 00:04:59 +00:00
|
|
|
return {};
|
2018-03-21 00:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 01:29:32 +00:00
|
|
|
void* Buffer::GetMappedPointerImpl() {
|
|
|
|
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
|