2019-07-20 01:34:56 +00:00
|
|
|
// Copyright 2019 The Dawn Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "dawn_native/RenderEncoderBase.h"
|
|
|
|
|
|
|
|
#include "common/Constants.h"
|
|
|
|
#include "dawn_native/Buffer.h"
|
|
|
|
#include "dawn_native/CommandEncoder.h"
|
2021-01-22 09:57:38 +00:00
|
|
|
#include "dawn_native/CommandValidation.h"
|
2019-07-20 01:34:56 +00:00
|
|
|
#include "dawn_native/Commands.h"
|
|
|
|
#include "dawn_native/Device.h"
|
|
|
|
#include "dawn_native/RenderPipeline.h"
|
2020-09-01 18:32:49 +00:00
|
|
|
#include "dawn_native/ValidationUtils_autogen.h"
|
2019-07-20 01:34:56 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
2019-07-23 17:04:34 +00:00
|
|
|
#include <cstring>
|
2019-07-20 01:34:56 +00:00
|
|
|
|
|
|
|
namespace dawn_native {
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
RenderEncoderBase::RenderEncoderBase(DeviceBase* device, EncodingContext* encodingContext)
|
2020-04-22 00:55:43 +00:00
|
|
|
: ProgrammablePassEncoder(device, encodingContext, PassType::Render),
|
2020-02-26 08:24:20 +00:00
|
|
|
mDisableBaseVertex(device->IsToggleEnabled(Toggle::DisableBaseVertex)),
|
|
|
|
mDisableBaseInstance(device->IsToggleEnabled(Toggle::DisableBaseInstance)) {
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderEncoderBase::RenderEncoderBase(DeviceBase* device,
|
2019-07-24 18:15:24 +00:00
|
|
|
EncodingContext* encodingContext,
|
2019-07-20 01:34:56 +00:00
|
|
|
ErrorTag errorTag)
|
2020-04-22 00:55:43 +00:00
|
|
|
: ProgrammablePassEncoder(device, encodingContext, errorTag, PassType::Render),
|
2020-02-26 08:24:20 +00:00
|
|
|
mDisableBaseVertex(device->IsToggleEnabled(Toggle::DisableBaseVertex)),
|
|
|
|
mDisableBaseInstance(device->IsToggleEnabled(Toggle::DisableBaseInstance)) {
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::Draw(uint32_t vertexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstVertex,
|
|
|
|
uint32_t firstInstance) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
if (mDisableBaseInstance && firstInstance != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Non-zero first instance not supported");
|
|
|
|
}
|
2020-02-26 08:24:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
DrawCmd* draw = allocator->Allocate<DrawCmd>(Command::Draw);
|
|
|
|
draw->vertexCount = vertexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstVertex = firstVertex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::DrawIndexed(uint32_t indexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstIndex,
|
|
|
|
int32_t baseVertex,
|
|
|
|
uint32_t firstInstance) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
if (mDisableBaseInstance && firstInstance != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Non-zero first instance not supported");
|
|
|
|
}
|
|
|
|
if (mDisableBaseVertex && baseVertex != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Non-zero base vertex not supported");
|
|
|
|
}
|
2020-02-26 08:24:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
DrawIndexedCmd* draw = allocator->Allocate<DrawIndexedCmd>(Command::DrawIndexed);
|
|
|
|
draw->indexCount = indexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstIndex = firstIndex;
|
|
|
|
draw->baseVertex = baseVertex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(indirectBuffer));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(indirectBuffer, wgpu::BufferUsage::Indirect));
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
if (indirectOffset % 4 != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Indirect offset must be a multiple of 4");
|
|
|
|
}
|
2020-10-10 00:06:30 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
if (indirectOffset >= indirectBuffer->GetSize() ||
|
|
|
|
indirectOffset + kDrawIndirectSize > indirectBuffer->GetSize()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Indirect offset out of bounds");
|
|
|
|
}
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawIndirectCmd* cmd = allocator->Allocate<DrawIndirectCmd>(Command::DrawIndirect);
|
|
|
|
cmd->indirectBuffer = indirectBuffer;
|
|
|
|
cmd->indirectOffset = indirectOffset;
|
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
mUsageTracker.BufferUsedAs(indirectBuffer, wgpu::BufferUsage::Indirect);
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::DrawIndexedIndirect(BufferBase* indirectBuffer,
|
|
|
|
uint64_t indirectOffset) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(indirectBuffer));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(indirectBuffer, wgpu::BufferUsage::Indirect));
|
|
|
|
|
|
|
|
// Indexed indirect draws need a compute-shader based validation check that the
|
|
|
|
// range of indices is contained inside the index buffer on Metal. Disallow them as
|
|
|
|
// unsafe until the validation is implemented.
|
|
|
|
if (GetDevice()->IsToggleEnabled(Toggle::DisallowUnsafeAPIs)) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"DrawIndexedIndirect is disallowed because it doesn't validate that the "
|
|
|
|
"index "
|
|
|
|
"range is valid yet.");
|
|
|
|
}
|
2020-11-03 10:54:56 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
if (indirectOffset % 4 != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Indirect offset must be a multiple of 4");
|
|
|
|
}
|
2020-10-10 00:06:30 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
if ((indirectOffset >= indirectBuffer->GetSize() ||
|
|
|
|
indirectOffset + kDrawIndexedIndirectSize > indirectBuffer->GetSize())) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Indirect offset out of bounds");
|
|
|
|
}
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawIndexedIndirectCmd* cmd =
|
|
|
|
allocator->Allocate<DrawIndexedIndirectCmd>(Command::DrawIndexedIndirect);
|
|
|
|
cmd->indirectBuffer = indirectBuffer;
|
|
|
|
cmd->indirectOffset = indirectOffset;
|
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
mUsageTracker.BufferUsedAs(indirectBuffer, wgpu::BufferUsage::Indirect);
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::SetPipeline(RenderPipelineBase* pipeline) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(pipeline));
|
|
|
|
}
|
2019-07-24 18:15:24 +00:00
|
|
|
|
|
|
|
SetRenderPipelineCmd* cmd =
|
|
|
|
allocator->Allocate<SetRenderPipelineCmd>(Command::SetRenderPipeline);
|
|
|
|
cmd->pipeline = pipeline;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 01:13:35 +00:00
|
|
|
void RenderEncoderBase::SetIndexBufferWithFormat(BufferBase* buffer, wgpu::IndexFormat format,
|
|
|
|
uint64_t offset, uint64_t size) {
|
2020-11-25 08:54:14 +00:00
|
|
|
GetDevice()->EmitDeprecationWarning(
|
|
|
|
"RenderEncoderBase::SetIndexBufferWithFormat is deprecated. Use "
|
|
|
|
"RenderEncoderBase::SetIndexBuffer instead.");
|
|
|
|
SetIndexBuffer(buffer, format, offset, size);
|
2020-08-27 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 08:54:14 +00:00
|
|
|
void RenderEncoderBase::SetIndexBuffer(BufferBase* buffer,
|
|
|
|
wgpu::IndexFormat format,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t size) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(buffer));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(buffer, wgpu::BufferUsage::Index));
|
2020-11-25 08:54:14 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
DAWN_TRY(ValidateIndexFormat(format));
|
|
|
|
if (format == wgpu::IndexFormat::Undefined) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Index format must be specified");
|
|
|
|
}
|
2020-04-24 09:42:03 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
uint64_t bufferSize = buffer->GetSize();
|
|
|
|
if (offset > bufferSize) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Offset larger than the buffer size");
|
|
|
|
}
|
|
|
|
uint64_t remainingSize = bufferSize - offset;
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
size = remainingSize;
|
|
|
|
} else {
|
|
|
|
if (size > remainingSize) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Size + offset larger than the buffer size");
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 09:42:03 +00:00
|
|
|
} else {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (size == 0) {
|
|
|
|
size = buffer->GetSize() - offset;
|
2020-04-24 09:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
SetIndexBufferCmd* cmd =
|
|
|
|
allocator->Allocate<SetIndexBufferCmd>(Command::SetIndexBuffer);
|
|
|
|
cmd->buffer = buffer;
|
2020-08-27 01:13:35 +00:00
|
|
|
cmd->format = format;
|
2019-07-24 18:15:24 +00:00
|
|
|
cmd->offset = offset;
|
2020-04-24 09:42:03 +00:00
|
|
|
cmd->size = size;
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
mUsageTracker.BufferUsedAs(buffer, wgpu::BufferUsage::Index);
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 09:42:03 +00:00
|
|
|
void RenderEncoderBase::SetVertexBuffer(uint32_t slot,
|
|
|
|
BufferBase* buffer,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t size) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(buffer));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(buffer, wgpu::BufferUsage::Vertex));
|
2019-07-20 01:34:56 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
if (slot >= kMaxVertexBuffers) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Vertex buffer slot out of bounds");
|
|
|
|
}
|
2020-04-24 09:42:03 +00:00
|
|
|
|
2021-01-23 00:07:46 +00:00
|
|
|
uint64_t bufferSize = buffer->GetSize();
|
|
|
|
if (offset > bufferSize) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Offset larger than the buffer size");
|
|
|
|
}
|
|
|
|
uint64_t remainingSize = bufferSize - offset;
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
size = remainingSize;
|
|
|
|
} else {
|
|
|
|
if (size > remainingSize) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Size + offset larger than the buffer size");
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 09:42:03 +00:00
|
|
|
} else {
|
2021-01-23 00:07:46 +00:00
|
|
|
if (size == 0) {
|
|
|
|
size = buffer->GetSize() - offset;
|
2020-04-24 09:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 07:29:58 +00:00
|
|
|
SetVertexBufferCmd* cmd =
|
|
|
|
allocator->Allocate<SetVertexBufferCmd>(Command::SetVertexBuffer);
|
2020-09-17 19:07:00 +00:00
|
|
|
cmd->slot = VertexBufferSlot(static_cast<uint8_t>(slot));
|
2019-10-10 07:29:58 +00:00
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->offset = offset;
|
2020-04-24 09:42:03 +00:00
|
|
|
cmd->size = size;
|
2019-07-20 01:34:56 +00:00
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
mUsageTracker.BufferUsedAs(buffer, wgpu::BufferUsage::Vertex);
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dawn_native
|