From e909d4efe1067a1cc5b27e4f90d472b062bfc690 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 4 Jul 2018 14:10:20 +0200 Subject: [PATCH] Move FreeCommands and SkipCommand to their own file --- src/backend/CMakeLists.txt | 2 + src/backend/CommandBuffer.cpp | 192 ----------------------------- src/backend/Commands.cpp | 220 ++++++++++++++++++++++++++++++++++ src/backend/Commands.h | 3 + 4 files changed, 225 insertions(+), 192 deletions(-) create mode 100644 src/backend/Commands.cpp diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index 17b0bf79e4..20faf92a6a 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -345,6 +345,8 @@ list(APPEND BACKEND_SOURCES ${BACKEND_DIR}/CommandAllocator.h ${BACKEND_DIR}/CommandBuffer.cpp ${BACKEND_DIR}/CommandBuffer.h + ${BACKEND_DIR}/Commands.cpp + ${BACKEND_DIR}/Commands.h ${BACKEND_DIR}/ComputePipeline.cpp ${BACKEND_DIR}/ComputePipeline.h ${BACKEND_DIR}/DepthStencilState.cpp diff --git a/src/backend/CommandBuffer.cpp b/src/backend/CommandBuffer.cpp index 2ee9c1fce7..88bdd73882 100644 --- a/src/backend/CommandBuffer.cpp +++ b/src/backend/CommandBuffer.cpp @@ -150,198 +150,6 @@ namespace backend { return mDevice; } - void FreeCommands(CommandIterator* commands) { - Command type; - while (commands->NextCommandId(&type)) { - switch (type) { - case Command::BeginComputePass: { - BeginComputePassCmd* begin = commands->NextCommand(); - begin->~BeginComputePassCmd(); - } break; - case Command::BeginRenderPass: { - BeginRenderPassCmd* begin = commands->NextCommand(); - begin->~BeginRenderPassCmd(); - } break; - case Command::CopyBufferToBuffer: { - CopyBufferToBufferCmd* copy = commands->NextCommand(); - copy->~CopyBufferToBufferCmd(); - } break; - case Command::CopyBufferToTexture: { - CopyBufferToTextureCmd* copy = commands->NextCommand(); - copy->~CopyBufferToTextureCmd(); - } break; - case Command::CopyTextureToBuffer: { - CopyTextureToBufferCmd* copy = commands->NextCommand(); - copy->~CopyTextureToBufferCmd(); - } break; - case Command::Dispatch: { - DispatchCmd* dispatch = commands->NextCommand(); - dispatch->~DispatchCmd(); - } break; - case Command::DrawArrays: { - DrawArraysCmd* draw = commands->NextCommand(); - draw->~DrawArraysCmd(); - } break; - case Command::DrawElements: { - DrawElementsCmd* draw = commands->NextCommand(); - draw->~DrawElementsCmd(); - } break; - case Command::EndComputePass: { - EndComputePassCmd* cmd = commands->NextCommand(); - cmd->~EndComputePassCmd(); - } break; - case Command::EndRenderPass: { - EndRenderPassCmd* cmd = commands->NextCommand(); - cmd->~EndRenderPassCmd(); - } break; - case Command::SetComputePipeline: { - SetComputePipelineCmd* cmd = commands->NextCommand(); - cmd->~SetComputePipelineCmd(); - } break; - case Command::SetRenderPipeline: { - SetRenderPipelineCmd* cmd = commands->NextCommand(); - cmd->~SetRenderPipelineCmd(); - } break; - case Command::SetPushConstants: { - SetPushConstantsCmd* cmd = commands->NextCommand(); - commands->NextData(cmd->count); - cmd->~SetPushConstantsCmd(); - } break; - case Command::SetStencilReference: { - SetStencilReferenceCmd* cmd = commands->NextCommand(); - cmd->~SetStencilReferenceCmd(); - } break; - case Command::SetScissorRect: { - SetScissorRectCmd* cmd = commands->NextCommand(); - cmd->~SetScissorRectCmd(); - } break; - case Command::SetBlendColor: { - SetBlendColorCmd* cmd = commands->NextCommand(); - cmd->~SetBlendColorCmd(); - } break; - case Command::SetBindGroup: { - SetBindGroupCmd* cmd = commands->NextCommand(); - cmd->~SetBindGroupCmd(); - } break; - case Command::SetIndexBuffer: { - SetIndexBufferCmd* cmd = commands->NextCommand(); - cmd->~SetIndexBufferCmd(); - } break; - case Command::SetVertexBuffers: { - SetVertexBuffersCmd* cmd = commands->NextCommand(); - auto buffers = commands->NextData>(cmd->count); - for (size_t i = 0; i < cmd->count; ++i) { - (&buffers[i])->~Ref(); - } - commands->NextData(cmd->count); - cmd->~SetVertexBuffersCmd(); - } break; - case Command::TransitionBufferUsage: { - TransitionBufferUsageCmd* cmd = - commands->NextCommand(); - cmd->~TransitionBufferUsageCmd(); - } break; - case Command::TransitionTextureUsage: { - TransitionTextureUsageCmd* cmd = - commands->NextCommand(); - cmd->~TransitionTextureUsageCmd(); - } break; - } - } - commands->DataWasDestroyed(); - } - - void SkipCommand(CommandIterator* commands, Command type) { - switch (type) { - case Command::BeginComputePass: - commands->NextCommand(); - break; - - case Command::BeginRenderPass: - commands->NextCommand(); - break; - - case Command::CopyBufferToBuffer: - commands->NextCommand(); - break; - - case Command::CopyBufferToTexture: - commands->NextCommand(); - break; - - case Command::CopyTextureToBuffer: - commands->NextCommand(); - break; - - case Command::Dispatch: - commands->NextCommand(); - break; - - case Command::DrawArrays: - commands->NextCommand(); - break; - - case Command::DrawElements: - commands->NextCommand(); - break; - - case Command::EndComputePass: - commands->NextCommand(); - break; - - case Command::EndRenderPass: - commands->NextCommand(); - break; - - case Command::SetComputePipeline: - commands->NextCommand(); - break; - - case Command::SetRenderPipeline: - commands->NextCommand(); - break; - - case Command::SetPushConstants: { - auto* cmd = commands->NextCommand(); - commands->NextData(cmd->count); - } break; - - case Command::SetStencilReference: - commands->NextCommand(); - break; - - case Command::SetScissorRect: - commands->NextCommand(); - break; - - case Command::SetBlendColor: - commands->NextCommand(); - break; - - case Command::SetBindGroup: - commands->NextCommand(); - break; - - case Command::SetIndexBuffer: - commands->NextCommand(); - break; - - case Command::SetVertexBuffers: { - auto* cmd = commands->NextCommand(); - commands->NextData>(cmd->count); - commands->NextData(cmd->count); - } break; - - case Command::TransitionBufferUsage: - commands->NextCommand(); - break; - - case Command::TransitionTextureUsage: - commands->NextCommand(); - break; - } - } - CommandBufferBuilder::CommandBufferBuilder(DeviceBase* device) : Builder(device), mState(std::make_unique(this)) { } diff --git a/src/backend/Commands.cpp b/src/backend/Commands.cpp new file mode 100644 index 0000000000..c4b77f1815 --- /dev/null +++ b/src/backend/Commands.cpp @@ -0,0 +1,220 @@ +// Copyright 2018 The NXT 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 "backend/Commands.h" + +#include "backend/BindGroup.h" +#include "backend/Buffer.h" +#include "backend/CommandAllocator.h" +#include "backend/ComputePipeline.h" +#include "backend/RenderPipeline.h" +#include "backend/Texture.h" + +namespace backend { + + void FreeCommands(CommandIterator* commands) { + commands->Reset(); + + Command type; + while (commands->NextCommandId(&type)) { + switch (type) { + case Command::BeginComputePass: { + BeginComputePassCmd* begin = commands->NextCommand(); + begin->~BeginComputePassCmd(); + } break; + case Command::BeginRenderPass: { + BeginRenderPassCmd* begin = commands->NextCommand(); + begin->~BeginRenderPassCmd(); + } break; + case Command::CopyBufferToBuffer: { + CopyBufferToBufferCmd* copy = commands->NextCommand(); + copy->~CopyBufferToBufferCmd(); + } break; + case Command::CopyBufferToTexture: { + CopyBufferToTextureCmd* copy = commands->NextCommand(); + copy->~CopyBufferToTextureCmd(); + } break; + case Command::CopyTextureToBuffer: { + CopyTextureToBufferCmd* copy = commands->NextCommand(); + copy->~CopyTextureToBufferCmd(); + } break; + case Command::Dispatch: { + DispatchCmd* dispatch = commands->NextCommand(); + dispatch->~DispatchCmd(); + } break; + case Command::DrawArrays: { + DrawArraysCmd* draw = commands->NextCommand(); + draw->~DrawArraysCmd(); + } break; + case Command::DrawElements: { + DrawElementsCmd* draw = commands->NextCommand(); + draw->~DrawElementsCmd(); + } break; + case Command::EndComputePass: { + EndComputePassCmd* cmd = commands->NextCommand(); + cmd->~EndComputePassCmd(); + } break; + case Command::EndRenderPass: { + EndRenderPassCmd* cmd = commands->NextCommand(); + cmd->~EndRenderPassCmd(); + } break; + case Command::SetComputePipeline: { + SetComputePipelineCmd* cmd = commands->NextCommand(); + cmd->~SetComputePipelineCmd(); + } break; + case Command::SetRenderPipeline: { + SetRenderPipelineCmd* cmd = commands->NextCommand(); + cmd->~SetRenderPipelineCmd(); + } break; + case Command::SetPushConstants: { + SetPushConstantsCmd* cmd = commands->NextCommand(); + commands->NextData(cmd->count); + cmd->~SetPushConstantsCmd(); + } break; + case Command::SetStencilReference: { + SetStencilReferenceCmd* cmd = commands->NextCommand(); + cmd->~SetStencilReferenceCmd(); + } break; + case Command::SetScissorRect: { + SetScissorRectCmd* cmd = commands->NextCommand(); + cmd->~SetScissorRectCmd(); + } break; + case Command::SetBlendColor: { + SetBlendColorCmd* cmd = commands->NextCommand(); + cmd->~SetBlendColorCmd(); + } break; + case Command::SetBindGroup: { + SetBindGroupCmd* cmd = commands->NextCommand(); + cmd->~SetBindGroupCmd(); + } break; + case Command::SetIndexBuffer: { + SetIndexBufferCmd* cmd = commands->NextCommand(); + cmd->~SetIndexBufferCmd(); + } break; + case Command::SetVertexBuffers: { + SetVertexBuffersCmd* cmd = commands->NextCommand(); + auto buffers = commands->NextData>(cmd->count); + for (size_t i = 0; i < cmd->count; ++i) { + (&buffers[i])->~Ref(); + } + commands->NextData(cmd->count); + cmd->~SetVertexBuffersCmd(); + } break; + case Command::TransitionBufferUsage: { + TransitionBufferUsageCmd* cmd = + commands->NextCommand(); + cmd->~TransitionBufferUsageCmd(); + } break; + case Command::TransitionTextureUsage: { + TransitionTextureUsageCmd* cmd = + commands->NextCommand(); + cmd->~TransitionTextureUsageCmd(); + } break; + } + } + commands->DataWasDestroyed(); + } + + void SkipCommand(CommandIterator* commands, Command type) { + switch (type) { + case Command::BeginComputePass: + commands->NextCommand(); + break; + + case Command::BeginRenderPass: + commands->NextCommand(); + break; + + case Command::CopyBufferToBuffer: + commands->NextCommand(); + break; + + case Command::CopyBufferToTexture: + commands->NextCommand(); + break; + + case Command::CopyTextureToBuffer: + commands->NextCommand(); + break; + + case Command::Dispatch: + commands->NextCommand(); + break; + + case Command::DrawArrays: + commands->NextCommand(); + break; + + case Command::DrawElements: + commands->NextCommand(); + break; + + case Command::EndComputePass: + commands->NextCommand(); + break; + + case Command::EndRenderPass: + commands->NextCommand(); + break; + + case Command::SetComputePipeline: + commands->NextCommand(); + break; + + case Command::SetRenderPipeline: + commands->NextCommand(); + break; + + case Command::SetPushConstants: { + auto* cmd = commands->NextCommand(); + commands->NextData(cmd->count); + } break; + + case Command::SetStencilReference: + commands->NextCommand(); + break; + + case Command::SetScissorRect: + commands->NextCommand(); + break; + + case Command::SetBlendColor: + commands->NextCommand(); + break; + + case Command::SetBindGroup: + commands->NextCommand(); + break; + + case Command::SetIndexBuffer: + commands->NextCommand(); + break; + + case Command::SetVertexBuffers: { + auto* cmd = commands->NextCommand(); + commands->NextData>(cmd->count); + commands->NextData(cmd->count); + } break; + + case Command::TransitionBufferUsage: + commands->NextCommand(); + break; + + case Command::TransitionTextureUsage: + commands->NextCommand(); + break; + } + } + +} // namespace backend diff --git a/src/backend/Commands.h b/src/backend/Commands.h index 2d70a1ac04..b10882341b 100644 --- a/src/backend/Commands.h +++ b/src/backend/Commands.h @@ -167,6 +167,9 @@ namespace backend { // the commands have a chance to run their destructor and remove internal references. class CommandIterator; void FreeCommands(CommandIterator* commands); + + // Helper function to allow skipping over a command when it is unimplemented, while still + // consuming the correct amount of data from the command iterator. void SkipCommand(CommandIterator* commands, Command type); } // namespace backend