From 69f1db72481b4cde9d7ec887e68ecc7a7d3cc878 Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Sat, 20 Jul 2019 01:34:56 +0000 Subject: [PATCH] Add RenderEncoderBase to match WebGPU class hierarchy. This is needed so that RenderBundleEncoder and RenderPassEncoder can share code. This patch also moves EndPass out of ProgrammablePassEncoder and into both RenderPassEncoder and ComputePassEncoder. Render bundles do not have EndPass. Bug: dawn:154 Change-Id: Ib7126b2ba718b0b93e3d6f15c429ac910c0d5d31 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/9180 Commit-Queue: Austin Eng Reviewed-by: Kai Ninomiya --- BUILD.gn | 2 + src/dawn_native/ComputePassEncoder.cpp | 9 ++ src/dawn_native/ComputePassEncoder.h | 2 + src/dawn_native/ProgrammablePassEncoder.cpp | 9 -- src/dawn_native/ProgrammablePassEncoder.h | 2 - src/dawn_native/RenderEncoderBase.cpp | 159 ++++++++++++++++++++ src/dawn_native/RenderEncoderBase.h | 67 +++++++++ src/dawn_native/RenderPassEncoder.cpp | 119 +-------------- src/dawn_native/RenderPassEncoder.h | 33 +--- 9 files changed, 247 insertions(+), 155 deletions(-) create mode 100644 src/dawn_native/RenderEncoderBase.cpp create mode 100644 src/dawn_native/RenderEncoderBase.h diff --git a/BUILD.gn b/BUILD.gn index ead3c39b52..3f443c07ca 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -153,6 +153,8 @@ source_set("libdawn_native_sources") { "src/dawn_native/Queue.h", "src/dawn_native/RefCounted.cpp", "src/dawn_native/RefCounted.h", + "src/dawn_native/RenderEncoderBase.cpp", + "src/dawn_native/RenderEncoderBase.h", "src/dawn_native/RenderPassEncoder.cpp", "src/dawn_native/RenderPassEncoder.h", "src/dawn_native/RenderPipeline.cpp", diff --git a/src/dawn_native/ComputePassEncoder.cpp b/src/dawn_native/ComputePassEncoder.cpp index 708b2cb01b..e03924157a 100644 --- a/src/dawn_native/ComputePassEncoder.cpp +++ b/src/dawn_native/ComputePassEncoder.cpp @@ -39,6 +39,15 @@ namespace dawn_native { return new ComputePassEncoderBase(device, topLevelEncoder, ObjectBase::kError); } + void ComputePassEncoderBase::EndPass() { + if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { + return; + } + + mTopLevelEncoder->PassEnded(); + mAllocator = nullptr; + } + void ComputePassEncoderBase::Dispatch(uint32_t x, uint32_t y, uint32_t z) { if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { return; diff --git a/src/dawn_native/ComputePassEncoder.h b/src/dawn_native/ComputePassEncoder.h index 36a7e31e64..3e645bba8a 100644 --- a/src/dawn_native/ComputePassEncoder.h +++ b/src/dawn_native/ComputePassEncoder.h @@ -33,6 +33,8 @@ namespace dawn_native { static ComputePassEncoderBase* MakeError(DeviceBase* device, CommandEncoderBase* topLevelEncoder); + void EndPass(); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); void DispatchIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset); void SetPipeline(ComputePipelineBase* pipeline); diff --git a/src/dawn_native/ProgrammablePassEncoder.cpp b/src/dawn_native/ProgrammablePassEncoder.cpp index c46639aca8..da2f145c41 100644 --- a/src/dawn_native/ProgrammablePassEncoder.cpp +++ b/src/dawn_native/ProgrammablePassEncoder.cpp @@ -38,15 +38,6 @@ namespace dawn_native { : ObjectBase(device, errorTag), mTopLevelEncoder(topLevelEncoder), mAllocator(nullptr) { } - void ProgrammablePassEncoder::EndPass() { - if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { - return; - } - - mTopLevelEncoder->PassEnded(); - mAllocator = nullptr; - } - void ProgrammablePassEncoder::InsertDebugMarker(const char* groupLabel) { if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { return; diff --git a/src/dawn_native/ProgrammablePassEncoder.h b/src/dawn_native/ProgrammablePassEncoder.h index 0389447571..e64592cef2 100644 --- a/src/dawn_native/ProgrammablePassEncoder.h +++ b/src/dawn_native/ProgrammablePassEncoder.h @@ -33,8 +33,6 @@ namespace dawn_native { CommandEncoderBase* topLevelEncoder, CommandAllocator* allocator); - void EndPass(); - void InsertDebugMarker(const char* groupLabel); void PopDebugGroup(); void PushDebugGroup(const char* groupLabel); diff --git a/src/dawn_native/RenderEncoderBase.cpp b/src/dawn_native/RenderEncoderBase.cpp new file mode 100644 index 0000000000..060e300654 --- /dev/null +++ b/src/dawn_native/RenderEncoderBase.cpp @@ -0,0 +1,159 @@ +// 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" +#include "dawn_native/Commands.h" +#include "dawn_native/Device.h" +#include "dawn_native/RenderPipeline.h" + +#include +#include + +namespace dawn_native { + + RenderEncoderBase::RenderEncoderBase(DeviceBase* device, + CommandEncoderBase* topLevelEncoder, + CommandAllocator* allocator) + : ProgrammablePassEncoder(device, topLevelEncoder, allocator) { + } + + RenderEncoderBase::RenderEncoderBase(DeviceBase* device, + CommandEncoderBase* topLevelEncoder, + ErrorTag errorTag) + : ProgrammablePassEncoder(device, topLevelEncoder, errorTag) { + } + + void RenderEncoderBase::Draw(uint32_t vertexCount, + uint32_t instanceCount, + uint32_t firstVertex, + uint32_t firstInstance) { + if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { + return; + } + + DrawCmd* draw = mAllocator->Allocate(Command::Draw); + draw->vertexCount = vertexCount; + draw->instanceCount = instanceCount; + draw->firstVertex = firstVertex; + draw->firstInstance = firstInstance; + } + + void RenderEncoderBase::DrawIndexed(uint32_t indexCount, + uint32_t instanceCount, + uint32_t firstIndex, + int32_t baseVertex, + uint32_t firstInstance) { + if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { + return; + } + + DrawIndexedCmd* draw = mAllocator->Allocate(Command::DrawIndexed); + draw->indexCount = indexCount; + draw->instanceCount = instanceCount; + draw->firstIndex = firstIndex; + draw->baseVertex = baseVertex; + draw->firstInstance = firstInstance; + } + + void RenderEncoderBase::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(Command::DrawIndirect); + cmd->indirectBuffer = indirectBuffer; + cmd->indirectOffset = indirectOffset; + } + + void RenderEncoderBase::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(Command::DrawIndexedIndirect); + cmd->indirectBuffer = indirectBuffer; + cmd->indirectOffset = indirectOffset; + } + + void RenderEncoderBase::SetPipeline(RenderPipelineBase* pipeline) { + if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) || + mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(pipeline))) { + return; + } + + SetRenderPipelineCmd* cmd = + mAllocator->Allocate(Command::SetRenderPipeline); + cmd->pipeline = pipeline; + } + + void RenderEncoderBase::SetIndexBuffer(BufferBase* buffer, uint64_t offset) { + if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) || + mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffer))) { + return; + } + + SetIndexBufferCmd* cmd = mAllocator->Allocate(Command::SetIndexBuffer); + cmd->buffer = buffer; + cmd->offset = offset; + } + + void RenderEncoderBase::SetVertexBuffers(uint32_t startSlot, + uint32_t count, + BufferBase* const* buffers, + uint64_t const* offsets) { + if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { + return; + } + + for (size_t i = 0; i < count; ++i) { + if (mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffers[i]))) { + return; + } + } + + SetVertexBuffersCmd* cmd = + mAllocator->Allocate(Command::SetVertexBuffers); + cmd->startSlot = startSlot; + cmd->count = count; + + Ref* cmdBuffers = mAllocator->AllocateData>(count); + for (size_t i = 0; i < count; ++i) { + cmdBuffers[i] = buffers[i]; + } + + uint64_t* cmdOffsets = mAllocator->AllocateData(count); + memcpy(cmdOffsets, offsets, count * sizeof(uint64_t)); + } + +} // namespace dawn_native diff --git a/src/dawn_native/RenderEncoderBase.h b/src/dawn_native/RenderEncoderBase.h new file mode 100644 index 0000000000..adeead3f2f --- /dev/null +++ b/src/dawn_native/RenderEncoderBase.h @@ -0,0 +1,67 @@ +// 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. + +#ifndef DAWNNATIVE_RENDERENCODERBASE_H_ +#define DAWNNATIVE_RENDERENCODERBASE_H_ + +#include "dawn_native/Error.h" +#include "dawn_native/ProgrammablePassEncoder.h" + +namespace dawn_native { + + class RenderEncoderBase : public ProgrammablePassEncoder { + public: + RenderEncoderBase(DeviceBase* device, + CommandEncoderBase* topLevelEncoder, + CommandAllocator* allocator); + + void Draw(uint32_t vertexCount, + uint32_t instanceCount, + uint32_t firstVertex, + uint32_t firstInstance); + void DrawIndexed(uint32_t vertexCount, + uint32_t instanceCount, + uint32_t firstIndex, + int32_t baseVertex, + uint32_t firstInstance); + + void DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset); + void DrawIndexedIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset); + + void SetPipeline(RenderPipelineBase* pipeline); + + template + void SetVertexBuffers(uint32_t startSlot, + uint32_t count, + T* const* buffers, + uint64_t const* offsets) { + static_assert(std::is_base_of::value, ""); + SetVertexBuffers(startSlot, count, buffers, offsets); + } + void SetVertexBuffers(uint32_t startSlot, + uint32_t count, + BufferBase* const* buffers, + uint64_t const* offsets); + void SetIndexBuffer(BufferBase* buffer, uint64_t offset); + + protected: + // Construct an "error" render encoder base. + RenderEncoderBase(DeviceBase* device, + CommandEncoderBase* topLevelEncoder, + ErrorTag errorTag); + }; + +} // namespace dawn_native + +#endif // DAWNNATIVE_RENDERENCODERBASE_H_ diff --git a/src/dawn_native/RenderPassEncoder.cpp b/src/dawn_native/RenderPassEncoder.cpp index 026a71a80a..830a6b76a5 100644 --- a/src/dawn_native/RenderPassEncoder.cpp +++ b/src/dawn_native/RenderPassEncoder.cpp @@ -29,13 +29,13 @@ namespace dawn_native { RenderPassEncoderBase::RenderPassEncoderBase(DeviceBase* device, CommandEncoderBase* topLevelEncoder, CommandAllocator* allocator) - : ProgrammablePassEncoder(device, topLevelEncoder, allocator) { + : RenderEncoderBase(device, topLevelEncoder, allocator) { } RenderPassEncoderBase::RenderPassEncoderBase(DeviceBase* device, CommandEncoderBase* topLevelEncoder, ErrorTag errorTag) - : ProgrammablePassEncoder(device, topLevelEncoder, errorTag) { + : RenderEncoderBase(device, topLevelEncoder, errorTag) { } RenderPassEncoderBase* RenderPassEncoderBase::MakeError(DeviceBase* device, @@ -43,83 +43,13 @@ namespace dawn_native { return new RenderPassEncoderBase(device, topLevelEncoder, ObjectBase::kError); } - void RenderPassEncoderBase::Draw(uint32_t vertexCount, - uint32_t instanceCount, - uint32_t firstVertex, - uint32_t firstInstance) { + void RenderPassEncoderBase::EndPass() { if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { return; } - DrawCmd* draw = mAllocator->Allocate(Command::Draw); - draw->vertexCount = vertexCount; - draw->instanceCount = instanceCount; - draw->firstVertex = firstVertex; - draw->firstInstance = firstInstance; - } - - void RenderPassEncoderBase::DrawIndexed(uint32_t indexCount, - uint32_t instanceCount, - uint32_t firstIndex, - int32_t baseVertex, - uint32_t firstInstance) { - if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { - return; - } - - DrawIndexedCmd* draw = mAllocator->Allocate(Command::DrawIndexed); - draw->indexCount = indexCount; - draw->instanceCount = instanceCount; - draw->firstIndex = firstIndex; - draw->baseVertex = baseVertex; - draw->firstInstance = firstInstance; - } - - 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(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(Command::DrawIndexedIndirect); - cmd->indirectBuffer = indirectBuffer; - cmd->indirectOffset = indirectOffset; - } - - void RenderPassEncoderBase::SetPipeline(RenderPipelineBase* pipeline) { - if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) || - mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(pipeline))) { - return; - } - - SetRenderPipelineCmd* cmd = - mAllocator->Allocate(Command::SetRenderPipeline); - cmd->pipeline = pipeline; + mTopLevelEncoder->PassEnded(); + mAllocator = nullptr; } void RenderPassEncoderBase::SetStencilReference(uint32_t reference) { @@ -198,43 +128,4 @@ namespace dawn_native { cmd->height = height; } - void RenderPassEncoderBase::SetIndexBuffer(BufferBase* buffer, uint64_t offset) { - if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands()) || - mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffer))) { - return; - } - - SetIndexBufferCmd* cmd = mAllocator->Allocate(Command::SetIndexBuffer); - cmd->buffer = buffer; - cmd->offset = offset; - } - - void RenderPassEncoderBase::SetVertexBuffers(uint32_t startSlot, - uint32_t count, - BufferBase* const* buffers, - uint64_t const* offsets) { - if (mTopLevelEncoder->ConsumedError(ValidateCanRecordCommands())) { - return; - } - - for (size_t i = 0; i < count; ++i) { - if (mTopLevelEncoder->ConsumedError(GetDevice()->ValidateObject(buffers[i]))) { - return; - } - } - - SetVertexBuffersCmd* cmd = - mAllocator->Allocate(Command::SetVertexBuffers); - cmd->startSlot = startSlot; - cmd->count = count; - - Ref* cmdBuffers = mAllocator->AllocateData>(count); - for (size_t i = 0; i < count; ++i) { - cmdBuffers[i] = buffers[i]; - } - - uint64_t* cmdOffsets = mAllocator->AllocateData(count); - memcpy(cmdOffsets, offsets, count * sizeof(uint64_t)); - } - } // namespace dawn_native diff --git a/src/dawn_native/RenderPassEncoder.h b/src/dawn_native/RenderPassEncoder.h index 8250464c5d..ed947e5e5e 100644 --- a/src/dawn_native/RenderPassEncoder.h +++ b/src/dawn_native/RenderPassEncoder.h @@ -16,7 +16,7 @@ #define DAWNNATIVE_RENDERPASSENCODER_H_ #include "dawn_native/Error.h" -#include "dawn_native/ProgrammablePassEncoder.h" +#include "dawn_native/RenderEncoderBase.h" namespace dawn_native { @@ -24,7 +24,7 @@ namespace dawn_native { // is a pure frontend type to record in its parent CommandEncoder and never has a backend // implementation. // TODO(cwallez@chromium.org): Remove that generator limitation and rename to ComputePassEncoder - class RenderPassEncoderBase : public ProgrammablePassEncoder { + class RenderPassEncoderBase : public RenderEncoderBase { public: RenderPassEncoderBase(DeviceBase* device, CommandEncoderBase* topLevelEncoder, @@ -33,20 +33,7 @@ namespace dawn_native { static RenderPassEncoderBase* MakeError(DeviceBase* device, CommandEncoderBase* topLevelEncoder); - void Draw(uint32_t vertexCount, - uint32_t instanceCount, - uint32_t firstVertex, - uint32_t firstInstance); - void DrawIndexed(uint32_t vertexCount, - uint32_t instanceCount, - uint32_t firstIndex, - int32_t baseVertex, - uint32_t firstInstance); - - void DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset); - void DrawIndexedIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset); - - void SetPipeline(RenderPipelineBase* pipeline); + void EndPass(); void SetStencilReference(uint32_t reference); void SetBlendColor(const Color* color); @@ -58,20 +45,6 @@ namespace dawn_native { float maxDepth); void SetScissorRect(uint32_t x, uint32_t y, uint32_t width, uint32_t height); - template - void SetVertexBuffers(uint32_t startSlot, - uint32_t count, - T* const* buffers, - uint64_t const* offsets) { - static_assert(std::is_base_of::value, ""); - SetVertexBuffers(startSlot, count, buffers, offsets); - } - void SetVertexBuffers(uint32_t startSlot, - uint32_t count, - BufferBase* const* buffers, - uint64_t const* offsets); - void SetIndexBuffer(BufferBase* buffer, uint64_t offset); - protected: RenderPassEncoderBase(DeviceBase* device, CommandEncoderBase* topLevelEncoder,