2018-09-21 00:24:37 +00:00
|
|
|
// Copyright 2018 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/RenderPassEncoder.h"
|
|
|
|
|
2019-06-10 20:56:27 +00:00
|
|
|
#include "common/Constants.h"
|
2018-09-21 00:24:37 +00:00
|
|
|
#include "dawn_native/Buffer.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/CommandEncoder.h"
|
2018-09-21 00:24:37 +00:00
|
|
|
#include "dawn_native/Commands.h"
|
2019-02-13 13:09:18 +00:00
|
|
|
#include "dawn_native/Device.h"
|
2018-09-21 00:24:37 +00:00
|
|
|
#include "dawn_native/RenderPipeline.h"
|
|
|
|
|
2018-12-03 14:28:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
namespace dawn_native {
|
|
|
|
|
|
|
|
RenderPassEncoderBase::RenderPassEncoderBase(DeviceBase* device,
|
2019-02-20 11:46:16 +00:00
|
|
|
CommandEncoderBase* topLevelEncoder,
|
2018-09-21 00:24:37 +00:00
|
|
|
CommandAllocator* allocator)
|
2019-02-20 11:46:16 +00:00
|
|
|
: ProgrammablePassEncoder(device, topLevelEncoder, allocator) {
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 01:02:47 +00:00
|
|
|
RenderPassEncoderBase::RenderPassEncoderBase(DeviceBase* device,
|
|
|
|
CommandEncoderBase* topLevelEncoder,
|
|
|
|
ErrorTag errorTag)
|
|
|
|
: ProgrammablePassEncoder(device, topLevelEncoder, errorTag) {
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderPassEncoderBase* RenderPassEncoderBase::MakeError(DeviceBase* device,
|
|
|
|
CommandEncoderBase* topLevelEncoder) {
|
|
|
|
return new RenderPassEncoderBase(device, topLevelEncoder, ObjectBase::kError);
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:20:19 +00:00
|
|
|
void RenderPassEncoderBase::Draw(uint32_t vertexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstVertex,
|
|
|
|
uint32_t firstInstance) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
|
2018-11-21 11:19:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:20:19 +00:00
|
|
|
DrawCmd* draw = mAllocator->Allocate<DrawCmd>(Command::Draw);
|
2018-09-21 00:24:37 +00:00
|
|
|
draw->vertexCount = vertexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstVertex = firstVertex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:20:19 +00:00
|
|
|
void RenderPassEncoderBase::DrawIndexed(uint32_t indexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstIndex,
|
2019-04-12 07:58:44 +00:00
|
|
|
int32_t baseVertex,
|
2018-12-10 05:20:19 +00:00
|
|
|
uint32_t firstInstance) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
|
2018-11-21 11:19:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:20:19 +00:00
|
|
|
DrawIndexedCmd* draw = mAllocator->Allocate<DrawIndexedCmd>(Command::DrawIndexed);
|
2018-09-21 00:24:37 +00:00
|
|
|
draw->indexCount = indexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstIndex = firstIndex;
|
2018-12-13 01:05:26 +00:00
|
|
|
draw->baseVertex = baseVertex;
|
2018-09-21 00:24:37 +00:00
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:56:27 +00:00
|
|
|
void RenderPassEncoderBase::DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset) {
|
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
|
|
|
|
mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(indirectBuffer))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indirectOffset >= indirectBuffer->GetSize() ||
|
|
|
|
indirectOffset + kDrawIndirectSize > indirectBuffer->GetSize()) {
|
|
|
|
mTopLevelEncoder->HandleError("Indirect offset out of bounds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawIndirectCmd* cmd = mAllocator->Allocate<DrawIndirectCmd>(Command::DrawIndirect);
|
|
|
|
cmd->indirectBuffer = indirectBuffer;
|
|
|
|
cmd->indirectOffset = indirectOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPassEncoderBase::DrawIndexedIndirect(BufferBase* indirectBuffer,
|
|
|
|
uint64_t indirectOffset) {
|
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
|
|
|
|
mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(indirectBuffer))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indirectOffset >= indirectBuffer->GetSize() ||
|
|
|
|
indirectOffset + kDrawIndexedIndirectSize > indirectBuffer->GetSize()) {
|
|
|
|
mTopLevelEncoder->HandleError("Indirect offset out of bounds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawIndexedIndirectCmd* cmd =
|
|
|
|
mAllocator->Allocate<DrawIndexedIndirectCmd>(Command::DrawIndexedIndirect);
|
|
|
|
cmd->indirectBuffer = indirectBuffer;
|
|
|
|
cmd->indirectOffset = indirectOffset;
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:40:26 +00:00
|
|
|
void RenderPassEncoderBase::SetPipeline(RenderPipelineBase* pipeline) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
|
|
|
|
mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(pipeline))) {
|
2018-12-10 10:03:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
SetRenderPipelineCmd* cmd =
|
|
|
|
mAllocator->Allocate<SetRenderPipelineCmd>(Command::SetRenderPipeline);
|
|
|
|
cmd->pipeline = pipeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPassEncoderBase::SetStencilReference(uint32_t reference) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
|
2018-11-21 11:19:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
SetStencilReferenceCmd* cmd =
|
|
|
|
mAllocator->Allocate<SetStencilReferenceCmd>(Command::SetStencilReference);
|
|
|
|
cmd->reference = reference;
|
|
|
|
}
|
|
|
|
|
2019-02-05 12:13:10 +00:00
|
|
|
void RenderPassEncoderBase::SetBlendColor(const Color* color) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
|
2018-11-21 11:19:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
SetBlendColorCmd* cmd = mAllocator->Allocate<SetBlendColorCmd>(Command::SetBlendColor);
|
2019-02-05 12:13:10 +00:00
|
|
|
cmd->color = *color;
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPassEncoderBase::SetScissorRect(uint32_t x,
|
|
|
|
uint32_t y,
|
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
|
2018-11-21 11:19:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-02 07:42:18 +00:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
mTopLevelEncoder->HandleError("Width and height must be greater than 0.");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-21 11:19:36 +00:00
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
SetScissorRectCmd* cmd = mAllocator->Allocate<SetScissorRectCmd>(Command::SetScissorRect);
|
|
|
|
cmd->x = x;
|
|
|
|
cmd->y = y;
|
|
|
|
cmd->width = width;
|
|
|
|
cmd->height = height;
|
|
|
|
}
|
|
|
|
|
2019-04-05 20:51:29 +00:00
|
|
|
void RenderPassEncoderBase::SetIndexBuffer(BufferBase* buffer, uint64_t offset) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) ||
|
|
|
|
mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffer))) {
|
2018-12-10 10:03:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
SetIndexBufferCmd* cmd = mAllocator->Allocate<SetIndexBufferCmd>(Command::SetIndexBuffer);
|
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPassEncoderBase::SetVertexBuffers(uint32_t startSlot,
|
|
|
|
uint32_t count,
|
|
|
|
BufferBase* const* buffers,
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t const* offsets) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) {
|
2018-11-21 11:19:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-21 00:24:37 +00:00
|
|
|
|
2018-12-10 10:03:08 +00:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2019-02-20 11:46:16 +00:00
|
|
|
if (mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffers[i]))) {
|
2018-12-10 10:03:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
SetVertexBuffersCmd* cmd =
|
|
|
|
mAllocator->Allocate<SetVertexBuffersCmd>(Command::SetVertexBuffers);
|
|
|
|
cmd->startSlot = startSlot;
|
|
|
|
cmd->count = count;
|
|
|
|
|
|
|
|
Ref<BufferBase>* cmdBuffers = mAllocator->AllocateData<Ref<BufferBase>>(count);
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2019-05-14 03:53:26 +00:00
|
|
|
cmdBuffers[i] = buffers[i];
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t* cmdOffsets = mAllocator->AllocateData<uint64_t>(count);
|
|
|
|
memcpy(cmdOffsets, offsets, count * sizeof(uint64_t));
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dawn_native
|