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) {
|
2019-06-13 10:22:32 +00:00
|
|
|
device->gl.GenBuffers(1, &mBuffer);
|
|
|
|
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
|
|
|
device->gl.BufferData(GL_ARRAY_BUFFER, GetSize(), 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);
|
|
|
|
void* data = gl.MapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2019-05-15 18:55:22 +00:00
|
|
|
*mappedPointer = reinterpret_cast<uint8_t*>(data);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:19:15 +00:00
|
|
|
MaybeError Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const void* data) {
|
2019-06-13 10:22:32 +00:00
|
|
|
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
|
|
|
|
|
|
|
|
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
|
|
|
|
gl.BufferSubData(GL_ARRAY_BUFFER, start, count, data);
|
2019-01-29 00:10:07 +00:00
|
|
|
return {};
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 19:31:17 +00:00
|
|
|
void 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);
|
|
|
|
void* data = gl.MapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
|
2019-02-14 19:31:17 +00:00
|
|
|
CallMapReadCallback(serial, DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS, data, GetSize());
|
2017-07-14 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 19:31:17 +00:00
|
|
|
void 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);
|
|
|
|
void* data = gl.MapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2019-02-14 19:31:17 +00:00
|
|
|
CallMapWriteCallback(serial, DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS, data, GetSize());
|
2018-03-21 00:56:39 +00:00
|
|
|
}
|
|
|
|
|
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);
|
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
|