2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +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/Buffer.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-10 17:46:05 +00:00
|
|
|
#include "common/Assert.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Device.h"
|
2018-08-22 13:37:29 +00:00
|
|
|
#include "dawn_native/ValidationUtils_autogen.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
2017-11-24 18:59:42 +00:00
|
|
|
#include <utility>
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
MaybeError ValidateBufferDescriptor(DeviceBase*, const BufferDescriptor* descriptor) {
|
2018-09-10 14:17:24 +00:00
|
|
|
if (descriptor->nextInChain != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
|
|
|
|
}
|
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
DAWN_TRY(ValidateBufferUsageBit(descriptor->usage));
|
|
|
|
|
|
|
|
dawn::BufferUsageBit usage = descriptor->usage;
|
|
|
|
|
|
|
|
const dawn::BufferUsageBit kMapWriteAllowedUsages =
|
|
|
|
dawn::BufferUsageBit::MapWrite | dawn::BufferUsageBit::TransferSrc;
|
|
|
|
if (usage & dawn::BufferUsageBit::MapWrite && (usage & kMapWriteAllowedUsages) != usage) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Only TransferSrc is allowed with MapWrite");
|
2018-08-22 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const dawn::BufferUsageBit kMapReadAllowedUsages =
|
|
|
|
dawn::BufferUsageBit::MapRead | dawn::BufferUsageBit::TransferDst;
|
|
|
|
if (usage & dawn::BufferUsageBit::MapRead && (usage & kMapReadAllowedUsages) != usage) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Only TransferDst is allowed with MapRead");
|
2018-08-22 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
// Buffer
|
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
BufferBase::BufferBase(DeviceBase* device, const BufferDescriptor* descriptor)
|
2018-10-15 12:54:30 +00:00
|
|
|
: ObjectBase(device), mSize(descriptor->size), mUsage(descriptor->usage) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 14:51:29 +00:00
|
|
|
BufferBase::~BufferBase() {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (mIsMapped) {
|
2018-07-18 13:12:52 +00:00
|
|
|
CallMapReadCallback(mMapSerial, DAWN_BUFFER_MAP_ASYNC_STATUS_UNKNOWN, nullptr);
|
|
|
|
CallMapWriteCallback(mMapSerial, DAWN_BUFFER_MAP_ASYNC_STATUS_UNKNOWN, nullptr);
|
2017-06-09 14:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
uint32_t BufferBase::GetSize() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mSize;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 13:08:02 +00:00
|
|
|
dawn::BufferUsageBit BufferBase::GetUsage() const {
|
|
|
|
return mUsage;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 10:02:43 +00:00
|
|
|
MaybeError BufferBase::ValidateCanUseInSubmitNow() const {
|
|
|
|
if (mIsMapped) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Buffer used in a submit while mapped");
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BufferBase::CallMapReadCallback(uint32_t serial,
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnBufferMapAsyncStatus status,
|
2017-11-24 18:59:42 +00:00
|
|
|
const void* pointer) {
|
2018-03-21 00:56:39 +00:00
|
|
|
if (mMapReadCallback != nullptr && serial == mMapSerial) {
|
|
|
|
ASSERT(mMapWriteCallback == nullptr);
|
2018-03-20 23:11:41 +00:00
|
|
|
// Tag the callback as fired before firing it, otherwise it could fire a second time if
|
|
|
|
// for example buffer.Unmap() is called inside the application-provided callback.
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnBufferMapReadCallback callback = mMapReadCallback;
|
2017-11-23 18:32:51 +00:00
|
|
|
mMapReadCallback = nullptr;
|
2018-03-21 00:56:39 +00:00
|
|
|
callback(status, pointer, mMapUserdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferBase::CallMapWriteCallback(uint32_t serial,
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnBufferMapAsyncStatus status,
|
2018-03-21 00:56:39 +00:00
|
|
|
void* pointer) {
|
|
|
|
if (mMapWriteCallback != nullptr && serial == mMapSerial) {
|
|
|
|
ASSERT(mMapReadCallback == nullptr);
|
|
|
|
// Tag the callback as fired before firing it, otherwise it could fire a second time if
|
|
|
|
// for example buffer.Unmap() is called inside the application-provided callback.
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnBufferMapWriteCallback callback = mMapWriteCallback;
|
2018-03-21 00:56:39 +00:00
|
|
|
mMapWriteCallback = nullptr;
|
|
|
|
callback(status, pointer, mMapUserdata);
|
2017-06-09 14:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 16:07:02 +00:00
|
|
|
void BufferBase::SetSubData(uint32_t start, uint32_t count, const uint8_t* data) {
|
2018-10-15 12:54:30 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidateSetSubData(start, count))) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetSubDataImpl(start, count, data);
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BufferBase::MapReadAsync(uint32_t start,
|
|
|
|
uint32_t size,
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnBufferMapReadCallback callback,
|
|
|
|
dawnCallbackUserdata userdata) {
|
2018-10-15 12:54:30 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidateMap(start, size, dawn::BufferUsageBit::MapRead))) {
|
2018-07-18 13:12:52 +00:00
|
|
|
callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
|
2017-06-09 14:51:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:56:39 +00:00
|
|
|
ASSERT(mMapWriteCallback == nullptr);
|
2017-06-09 14:51:29 +00:00
|
|
|
|
2018-03-21 00:56:39 +00:00
|
|
|
// TODO(cwallez@chromium.org): what to do on wraparound? Could cause crashes.
|
|
|
|
mMapSerial++;
|
|
|
|
mMapReadCallback = callback;
|
|
|
|
mMapUserdata = userdata;
|
|
|
|
mIsMapped = true;
|
|
|
|
|
|
|
|
MapReadAsyncImpl(mMapSerial, start, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferBase::MapWriteAsync(uint32_t start,
|
|
|
|
uint32_t size,
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnBufferMapWriteCallback callback,
|
|
|
|
dawnCallbackUserdata userdata) {
|
2018-10-15 12:54:30 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidateMap(start, size, dawn::BufferUsageBit::MapWrite))) {
|
2018-07-18 13:12:52 +00:00
|
|
|
callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
|
2017-06-09 14:51:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:56:39 +00:00
|
|
|
ASSERT(mMapReadCallback == nullptr);
|
|
|
|
|
2017-06-09 14:51:29 +00:00
|
|
|
// TODO(cwallez@chromium.org): what to do on wraparound? Could cause crashes.
|
2018-03-21 00:56:39 +00:00
|
|
|
mMapSerial++;
|
|
|
|
mMapWriteCallback = callback;
|
|
|
|
mMapUserdata = userdata;
|
2017-11-23 18:32:51 +00:00
|
|
|
mIsMapped = true;
|
2018-03-21 00:56:39 +00:00
|
|
|
|
|
|
|
MapWriteAsyncImpl(mMapSerial, start, size);
|
2017-06-09 14:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BufferBase::Unmap() {
|
2018-10-15 12:54:30 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidateUnmap())) {
|
2017-06-09 14:51:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A map request can only be called once, so this will fire only if the request wasn't
|
|
|
|
// completed before the Unmap
|
2018-07-18 13:12:52 +00:00
|
|
|
CallMapReadCallback(mMapSerial, DAWN_BUFFER_MAP_ASYNC_STATUS_UNKNOWN, nullptr);
|
|
|
|
CallMapWriteCallback(mMapSerial, DAWN_BUFFER_MAP_ASYNC_STATUS_UNKNOWN, nullptr);
|
2017-06-09 14:51:29 +00:00
|
|
|
UnmapImpl();
|
2017-11-23 18:32:51 +00:00
|
|
|
mIsMapped = false;
|
2018-03-21 00:56:39 +00:00
|
|
|
mMapReadCallback = nullptr;
|
|
|
|
mMapWriteCallback = nullptr;
|
|
|
|
mMapUserdata = 0;
|
2017-06-09 14:51:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 19:20:07 +00:00
|
|
|
MaybeError BufferBase::ValidateSetSubData(uint32_t start, uint32_t count) const {
|
2018-12-17 17:08:41 +00:00
|
|
|
if (count > GetSize()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Buffer subdata with too much data");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that no overflow can happen because we already checked for GetSize() >= count
|
|
|
|
if (start > GetSize() - count) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Buffer subdata out of range");
|
2018-07-18 19:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 13:08:02 +00:00
|
|
|
if (!(mUsage & dawn::BufferUsageBit::TransferDst)) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Buffer needs the transfer dst usage bit");
|
2018-07-18 19:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError BufferBase::ValidateMap(uint32_t start,
|
|
|
|
uint32_t size,
|
|
|
|
dawn::BufferUsageBit requiredUsage) const {
|
2018-12-17 17:08:41 +00:00
|
|
|
if (size > GetSize()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Buffer mapping with too big a region");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that no overflow can happen because we already checked for GetSize() >= size
|
|
|
|
if (start > GetSize() - size) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Buffer mapping out of range");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 13:15:07 +00:00
|
|
|
if (mIsMapped) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Buffer already mapped");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 13:08:02 +00:00
|
|
|
if (!(mUsage & requiredUsage)) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Buffer needs the correct map usage bit");
|
2018-07-18 19:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError BufferBase::ValidateUnmap() const {
|
|
|
|
if (!mIsMapped) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Buffer wasn't mapped");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2018-07-09 13:15:07 +00:00
|
|
|
|
2018-07-18 19:20:07 +00:00
|
|
|
return {};
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|