From 9d71cba8d5b838fccca0ad0b39a4d789d89848b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Suonp=C3=A4=C3=A4?= Date: Wed, 8 Apr 2020 08:17:21 +0300 Subject: [PATCH 1/4] Add depth/stencil image readback to Amber buffer --- src/amberscript/parser.cc | 22 ++++---- src/amberscript/parser_bind_test.cc | 5 +- src/amberscript/parser_pipeline_test.cc | 8 +-- src/buffer.h | 2 +- src/format.h | 8 +++ src/pipeline.cc | 38 ++++++------- src/pipeline.h | 29 +++++----- src/pipeline_test.cc | 24 ++++----- src/vkscript/parser.cc | 6 +-- src/vulkan/device.cc | 2 +- src/vulkan/engine_vulkan.cc | 16 +++--- src/vulkan/frame_buffer.cc | 58 ++++++++++++++------ src/vulkan/frame_buffer.h | 10 ++-- src/vulkan/graphics_pipeline.cc | 72 +++++++++++++------------ src/vulkan/graphics_pipeline.h | 6 +-- src/vulkan/image_descriptor.cc | 19 +++++-- src/vulkan/transfer_image.cc | 42 ++++++++++++--- src/vulkan/transfer_image.h | 5 +- 18 files changed, 223 insertions(+), 149 deletions(-) diff --git a/src/amberscript/parser.cc b/src/amberscript/parser.cc index 10162b814..3550faed5 100644 --- a/src/amberscript/parser.cc +++ b/src/amberscript/parser.cc @@ -720,7 +720,7 @@ Result Parser::ToBufferType(const std::string& name, BufferType* type) { if (name == "color") *type = BufferType::kColor; else if (name == "depth_stencil") - *type = BufferType::kDepth; + *type = BufferType::kDepthStencil; else if (name == "push_constant") *type = BufferType::kPushConstant; else if (name == "combined_image_sampler") @@ -801,8 +801,8 @@ Result Parser::ParsePipelineBind(Pipeline* pipeline) { if (!r.IsSuccess()) return r; - } else if (buffer_type == BufferType::kDepth) { - r = pipeline->SetDepthBuffer(buffer); + } else if (buffer_type == BufferType::kDepthStencil) { + r = pipeline->SetDepthStencilBuffer(buffer); if (!r.IsSuccess()) return r; @@ -1083,11 +1083,11 @@ Result Parser::ParsePipelinePolygonMode(Pipeline* pipeline) { auto mode = token->AsString(); if (mode == "fill") - pipeline->SetPolygonMode(PolygonMode::kFill); + pipeline->GetPipelineData()->SetPolygonMode(PolygonMode::kFill); else if (mode == "line") - pipeline->SetPolygonMode(PolygonMode::kLine); + pipeline->GetPipelineData()->SetPolygonMode(PolygonMode::kLine); else if (mode == "point") - pipeline->SetPolygonMode(PolygonMode::kPoint); + pipeline->GetPipelineData()->SetPolygonMode(PolygonMode::kPoint); else return Result("invalid polygon mode: " + mode); @@ -1735,10 +1735,10 @@ Result Parser::ParseRun() { if (!token->IsInteger()) return Result("missing X position for RUN command"); - auto cmd = MakeUnique(pipeline, PipelineData{}); + auto cmd = + MakeUnique(pipeline, *pipeline->GetPipelineData()); cmd->SetLine(line); cmd->EnableOrtho(); - cmd->SetPolygonMode(pipeline->GetPolygonMode()); Result r = token->ConvertToDouble(); if (!r.IsSuccess()) @@ -1807,7 +1807,7 @@ Result Parser::ParseRun() { auto cmd = MakeUnique(pipeline); cmd->SetLine(line); - cmd->SetPolygonMode(pipeline->GetPolygonMode()); + cmd->SetPolygonMode(pipeline->GetPipelineData()->GetPolygonMode()); Result r = token->ConvertToDouble(); if (!r.IsSuccess()) @@ -1949,12 +1949,12 @@ Result Parser::ParseRun() { return Result("START_IDX plus COUNT exceeds vertex buffer data size"); } - auto cmd = MakeUnique(pipeline, PipelineData{}); + auto cmd = + MakeUnique(pipeline, *pipeline->GetPipelineData()); cmd->SetLine(line); cmd->SetTopology(topo); cmd->SetFirstVertexIndex(start_idx); cmd->SetVertexCount(count); - cmd->SetPolygonMode(pipeline->GetPolygonMode()); if (indexed) cmd->EnableIndexed(); diff --git a/src/amberscript/parser_bind_test.cc b/src/amberscript/parser_bind_test.cc index 0adbacbe9..f027b7065 100644 --- a/src/amberscript/parser_bind_test.cc +++ b/src/amberscript/parser_bind_test.cc @@ -513,7 +513,7 @@ END)"; ASSERT_EQ(1U, pipelines.size()); const auto* pipeline = pipelines[0].get(); - const auto& buf = pipeline->GetDepthBuffer(); + const auto& buf = pipeline->GetDepthStencilBuffer(); ASSERT_TRUE(buf.buffer != nullptr); EXPECT_EQ(90 * 180, buf.buffer->ElementCount()); EXPECT_EQ(90 * 180 * 4, buf.buffer->ValueCount()); @@ -649,7 +649,8 @@ END)"; Parser parser; Result r = parser.Parse(in); ASSERT_FALSE(r.IsSuccess()); - EXPECT_EQ("14: can only bind one depth buffer in a PIPELINE", r.Error()); + EXPECT_EQ("14: can only bind one depth/stencil buffer in a PIPELINE", + r.Error()); } TEST_F(AmberScriptParserTest, BindVertexData) { diff --git a/src/amberscript/parser_pipeline_test.cc b/src/amberscript/parser_pipeline_test.cc index 3e2922b51..197f1a198 100644 --- a/src/amberscript/parser_pipeline_test.cc +++ b/src/amberscript/parser_pipeline_test.cc @@ -279,13 +279,13 @@ END)"; const auto& pipelines = script->GetPipelines(); ASSERT_EQ(4U, pipelines.size()); - auto mode0 = pipelines[0]->GetPolygonMode(); + auto mode0 = pipelines[0]->GetPipelineData()->GetPolygonMode(); ASSERT_EQ(mode0, PolygonMode::kFill); - auto mode1 = pipelines[1]->GetPolygonMode(); + auto mode1 = pipelines[1]->GetPipelineData()->GetPolygonMode(); ASSERT_EQ(mode1, PolygonMode::kFill); - auto mode2 = pipelines[2]->GetPolygonMode(); + auto mode2 = pipelines[2]->GetPipelineData()->GetPolygonMode(); ASSERT_EQ(mode2, PolygonMode::kLine); - auto mode3 = pipelines[3]->GetPolygonMode(); + auto mode3 = pipelines[3]->GetPipelineData()->GetPolygonMode(); ASSERT_EQ(mode3, PolygonMode::kPoint); } diff --git a/src/buffer.h b/src/buffer.h index 9d6f3d6e0..82d77050d 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -37,7 +37,7 @@ enum class BufferType : int8_t { /// A color buffer. kColor = 0, /// A depth/stencil buffer. - kDepth, + kDepthStencil, /// An index buffer. kIndex, /// A sampled image. diff --git a/src/format.h b/src/format.h index e6e68ba59..bfef06d53 100644 --- a/src/format.h +++ b/src/format.h @@ -114,6 +114,14 @@ class Format { uint32_t SizeInBytes() const; bool IsFormatKnown() const { return format_type_ != FormatType::kUnknown; } + bool HasDepthComponent() const { + return format_type_ == FormatType::kD16_UNORM || + format_type_ == FormatType::kD16_UNORM_S8_UINT || + format_type_ == FormatType::kD24_UNORM_S8_UINT || + format_type_ == FormatType::kD32_SFLOAT || + format_type_ == FormatType::kD32_SFLOAT_S8_UINT || + format_type_ == FormatType::kX8_D24_UNORM_PACK32; + } bool HasStencilComponent() const { return format_type_ == FormatType::kD24_UNORM_S8_UINT || format_type_ == FormatType::kD16_UNORM_S8_UINT || diff --git a/src/pipeline.cc b/src/pipeline.cc index 612fdbdf8..8dd06de67 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -65,11 +65,12 @@ std::unique_ptr Pipeline::Clone() const { clone->color_attachments_ = color_attachments_; clone->vertex_buffers_ = vertex_buffers_; clone->buffers_ = buffers_; - clone->depth_buffer_ = depth_buffer_; + clone->depth_stencil_buffer_ = depth_stencil_buffer_; clone->index_buffer_ = index_buffer_; clone->fb_width_ = fb_width_; clone->fb_height_ = fb_height_; clone->set_arg_values_ = set_arg_values_; + clone->pipeline_data_ = pipeline_data_; if (!opencl_pod_buffers_.empty()) { // Generate specific buffers for the clone. @@ -184,16 +185,6 @@ Result Pipeline::SetShaderType(const Shader* shader, ShaderType type) { shader->GetName()); } -Result Pipeline::SetPolygonMode(PolygonMode mode) { - if (mode != PolygonMode::kFill && mode != PolygonMode::kLine && - mode != PolygonMode::kPoint) - return Result("invalid polygon mode specified for pipeline"); - - polygon_mode_ = mode; - - return {}; -} - Result Pipeline::Validate() const { for (const auto& attachment : color_attachments_) { if (attachment.buffer->ElementCount() != @@ -204,8 +195,8 @@ Result Pipeline::Validate() const { } } - if (depth_buffer_.buffer && - depth_buffer_.buffer->ElementCount() != fb_width_ * fb_height_) { + if (depth_stencil_buffer_.buffer && + depth_stencil_buffer_.buffer->ElementCount() != fb_width_ * fb_height_) { return Result("shared depth buffer must have same size over all PIPELINES"); } @@ -280,10 +271,10 @@ void Pipeline::UpdateFramebufferSizes() { attachment.buffer->SetElementCount(mip0_width * mip0_height); } - if (depth_buffer_.buffer) { - depth_buffer_.buffer->SetWidth(fb_width_); - depth_buffer_.buffer->SetHeight(fb_height_); - depth_buffer_.buffer->SetElementCount(size); + if (depth_stencil_buffer_.buffer) { + depth_stencil_buffer_.buffer->SetWidth(fb_width_); + depth_stencil_buffer_.buffer->SetHeight(fb_height_); + depth_stencil_buffer_.buffer->SetElementCount(size); } } @@ -323,12 +314,12 @@ Result Pipeline::GetLocationForColorAttachment(Buffer* buf, return Result("Unable to find requested buffer"); } -Result Pipeline::SetDepthBuffer(Buffer* buf) { - if (depth_buffer_.buffer != nullptr) - return Result("can only bind one depth buffer in a PIPELINE"); +Result Pipeline::SetDepthStencilBuffer(Buffer* buf) { + if (depth_stencil_buffer_.buffer != nullptr) + return Result("can only bind one depth/stencil buffer in a PIPELINE"); - depth_buffer_.buffer = buf; - depth_buffer_.type = BufferType::kDepth; + depth_stencil_buffer_.buffer = buf; + depth_stencil_buffer_.type = BufferType::kDepthStencil; buf->SetWidth(fb_width_); buf->SetHeight(fb_height_); @@ -403,7 +394,8 @@ std::unique_ptr Pipeline::GenerateDefaultColorAttachmentBuffer() { return buf; } -std::unique_ptr Pipeline::GenerateDefaultDepthAttachmentBuffer() { +std::unique_ptr +Pipeline::GenerateDefaultDepthStencilAttachmentBuffer() { TypeParser parser; auto type = parser.Parse(kDefaultDepthBufferFormat); auto fmt = MakeUnique(type.get()); diff --git a/src/pipeline.h b/src/pipeline.h index 36c87aaf2..2da926356 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -25,6 +25,7 @@ #include "amber/result.h" #include "src/buffer.h" #include "src/command_data.h" +#include "src/pipeline_data.h" #include "src/sampler.h" #include "src/shader.h" @@ -245,11 +246,17 @@ class Pipeline { /// something goes wrong. Result GetLocationForColorAttachment(Buffer* buf, uint32_t* loc) const; - /// Sets |buf| as the depth buffer for this pipeline. - Result SetDepthBuffer(Buffer* buf); - /// Returns information on the depth buffer bound to the pipeline. If no - /// depth buffer is bound the |BufferInfo::buffer| parameter will be nullptr. - const BufferInfo& GetDepthBuffer() const { return depth_buffer_; } + /// Sets |buf| as the depth/stencil buffer for this pipeline. + Result SetDepthStencilBuffer(Buffer* buf); + /// Returns information on the depth/stencil buffer bound to the pipeline. If + /// no depth buffer is bound the |BufferInfo::buffer| parameter will be + /// nullptr. + const BufferInfo& GetDepthStencilBuffer() const { + return depth_stencil_buffer_; + } + + /// Returns pipeline data. + PipelineData* GetPipelineData() { return &pipeline_data_; } /// Returns information on all vertex buffers bound to the pipeline. const std::vector& GetVertexBuffers() const { @@ -306,16 +313,13 @@ class Pipeline { return push_constant_buffer_; } - Result SetPolygonMode(PolygonMode mode); - PolygonMode GetPolygonMode() const { return polygon_mode_; } - /// Validates that the pipeline has been created correctly. Result Validate() const; /// Generates a default color attachment in B8G8R8A8_UNORM. std::unique_ptr GenerateDefaultColorAttachmentBuffer(); - /// Generates a default depth attachment in D32_SFLOAT_S8_UINT format. - std::unique_ptr GenerateDefaultDepthAttachmentBuffer(); + /// Generates a default depth/stencil attachment in D32_SFLOAT_S8_UINT format. + std::unique_ptr GenerateDefaultDepthStencilAttachmentBuffer(); /// Information on values set for OpenCL-C plain-old-data args. struct ArgSetInfo { @@ -359,11 +363,10 @@ class Pipeline { std::vector> types_; std::vector samplers_; std::vector> formats_; - BufferInfo depth_buffer_; + BufferInfo depth_stencil_buffer_; BufferInfo push_constant_buffer_; Buffer* index_buffer_ = nullptr; - PolygonMode polygon_mode_ = PolygonMode::kFill; - + PipelineData pipeline_data_; uint32_t fb_width_ = 250; uint32_t fb_height_ = 250; diff --git a/src/pipeline_test.cc b/src/pipeline_test.cc index 160d95143..28443c07d 100644 --- a/src/pipeline_test.cc +++ b/src/pipeline_test.cc @@ -31,7 +31,7 @@ class PipelineTest : public testing::Test { public: void TearDown() override { color_buffer_ = nullptr; - depth_buffer_ = nullptr; + depth_stencil_buffer_ = nullptr; } void SetupColorAttachment(Pipeline* p, uint32_t location) { @@ -41,16 +41,16 @@ class PipelineTest : public testing::Test { p->AddColorAttachment(color_buffer_.get(), location, 0); } - void SetupDepthAttachment(Pipeline* p) { - if (!depth_buffer_) - depth_buffer_ = p->GenerateDefaultDepthAttachmentBuffer(); + void SetupDepthStencilAttachment(Pipeline* p) { + if (!depth_stencil_buffer_) + depth_stencil_buffer_ = p->GenerateDefaultDepthStencilAttachmentBuffer(); - p->SetDepthBuffer(depth_buffer_.get()); + p->SetDepthStencilBuffer(depth_stencil_buffer_.get()); } private: std::unique_ptr color_buffer_; - std::unique_ptr depth_buffer_; + std::unique_ptr depth_stencil_buffer_; }; TEST_F(PipelineTest, AddShader) { @@ -185,7 +185,7 @@ TEST_F(PipelineTest, SetOptimizationForInvalidShader) { TEST_F(PipelineTest, GraphicsPipelineRequiresColorAttachment) { Pipeline p(PipelineType::kGraphics); - SetupDepthAttachment(&p); + SetupDepthStencilAttachment(&p); Result r = p.Validate(); ASSERT_FALSE(r.IsSuccess()); @@ -199,7 +199,7 @@ TEST_F(PipelineTest, GraphicsPipelineRequiresVertexAndFragmentShader) { Pipeline p(PipelineType::kGraphics); SetupColorAttachment(&p, 0); - SetupDepthAttachment(&p); + SetupDepthStencilAttachment(&p); Result r = p.AddShader(&v, kShaderTypeVertex); EXPECT_TRUE(r.IsSuccess()) << r.Error(); @@ -220,7 +220,7 @@ TEST_F(PipelineTest, GraphicsPipelineMissingVertexShader) { Pipeline p(PipelineType::kGraphics); SetupColorAttachment(&p, 0); - SetupDepthAttachment(&p); + SetupDepthStencilAttachment(&p); Result r = p.AddShader(&g, kShaderTypeGeometry); EXPECT_TRUE(r.IsSuccess()) << r.Error(); @@ -238,7 +238,7 @@ TEST_F(PipelineTest, ComputePipelineRequiresComputeShader) { Pipeline p(PipelineType::kCompute); SetupColorAttachment(&p, 0); - SetupDepthAttachment(&p); + SetupDepthStencilAttachment(&p); Result r = p.AddShader(&c, kShaderTypeCompute); EXPECT_TRUE(r.IsSuccess()) << r.Error(); @@ -250,7 +250,7 @@ TEST_F(PipelineTest, ComputePipelineRequiresComputeShader) { TEST_F(PipelineTest, ComputePipelineWithoutShader) { Pipeline p(PipelineType::kCompute); SetupColorAttachment(&p, 0); - SetupDepthAttachment(&p); + SetupDepthStencilAttachment(&p); Result r = p.Validate(); EXPECT_FALSE(r.IsSuccess()) << r.Error(); @@ -343,7 +343,7 @@ TEST_F(PipelineTest, Clone) { p.SetFramebufferHeight(600); SetupColorAttachment(&p, 0); - SetupDepthAttachment(&p); + SetupDepthStencilAttachment(&p); Shader f(kShaderTypeFragment); p.AddShader(&f, kShaderTypeFragment); diff --git a/src/vkscript/parser.cc b/src/vkscript/parser.cc index 7a173d032..3434a305d 100644 --- a/src/vkscript/parser.cc +++ b/src/vkscript/parser.cc @@ -198,17 +198,17 @@ Result Parser::ProcessRequireBlock(const SectionParser::Section& section) { } auto* pipeline = script_->GetPipeline(kDefaultPipelineName); - if (pipeline->GetDepthBuffer().buffer != nullptr) + if (pipeline->GetDepthStencilBuffer().buffer != nullptr) return Result("Only one depthstencil command allowed"); auto fmt = MakeUnique(type.get()); // Generate and add a depth buffer - auto depth_buf = pipeline->GenerateDefaultDepthAttachmentBuffer(); + auto depth_buf = pipeline->GenerateDefaultDepthStencilAttachmentBuffer(); depth_buf->SetFormat(fmt.get()); script_->RegisterFormat(std::move(fmt)); script_->RegisterType(std::move(type)); - Result r = pipeline->SetDepthBuffer(depth_buf.get()); + Result r = pipeline->SetDepthStencilBuffer(depth_buf.get()); if (!r.IsSuccess()) return r; diff --git a/src/vulkan/device.cc b/src/vulkan/device.cc index 74fade950..edbc69e89 100644 --- a/src/vulkan/device.cc +++ b/src/vulkan/device.cc @@ -565,7 +565,7 @@ bool Device::IsFormatSupportedByPhysicalDevice(const Format& format, flag = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT; is_buffer_type_image = true; break; - case BufferType::kDepth: + case BufferType::kDepthStencil: flag = VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT; is_buffer_type_image = true; break; diff --git a/src/vulkan/engine_vulkan.cc b/src/vulkan/engine_vulkan.cc index d4a402802..8db66813a 100644 --- a/src/vulkan/engine_vulkan.cc +++ b/src/vulkan/engine_vulkan.cc @@ -158,13 +158,12 @@ Result EngineVulkan::CreatePipeline(amber::Pipeline* pipeline) { return Result("Vulkan color attachment format is not supported"); } - Format* depth_fmt = nullptr; - if (pipeline->GetDepthBuffer().buffer) { - const auto& depth_info = pipeline->GetDepthBuffer(); + if (pipeline->GetDepthStencilBuffer().buffer) { + const auto& depth_stencil_info = pipeline->GetDepthStencilBuffer(); - depth_fmt = depth_info.buffer->GetFormat(); - if (!device_->IsFormatSupportedByPhysicalDevice(*depth_fmt, - depth_info.type)) { + auto fmt = depth_stencil_info.buffer->GetFormat(); + if (!device_->IsFormatSupportedByPhysicalDevice(*fmt, + depth_stencil_info.type)) { return Result("Vulkan depth attachment format is not supported"); } } @@ -184,8 +183,9 @@ Result EngineVulkan::CreatePipeline(amber::Pipeline* pipeline) { return r; } else { vk_pipeline = MakeUnique( - device_.get(), pipeline->GetColorAttachments(), depth_fmt, - engine_data.fence_timeout_ms, stage_create_info); + device_.get(), pipeline->GetColorAttachments(), + pipeline->GetDepthStencilBuffer(), engine_data.fence_timeout_ms, + stage_create_info); r = vk_pipeline->AsGraphics()->Initialize(pipeline->GetFramebufferWidth(), pipeline->GetFramebufferHeight(), diff --git a/src/vulkan/frame_buffer.cc b/src/vulkan/frame_buffer.cc index d042bd4b8..816b2a4e9 100644 --- a/src/vulkan/frame_buffer.cc +++ b/src/vulkan/frame_buffer.cc @@ -29,10 +29,12 @@ namespace vulkan { FrameBuffer::FrameBuffer( Device* device, const std::vector& color_attachments, + amber::Pipeline::BufferInfo depth_stencil_attachment, uint32_t width, uint32_t height) : device_(device), color_attachments_(color_attachments), + depth_stencil_attachment_(depth_stencil_attachment), width_(width), height_(height) {} @@ -43,8 +45,7 @@ FrameBuffer::~FrameBuffer() { } } -Result FrameBuffer::Initialize(VkRenderPass render_pass, - const Format* depth_format) { +Result FrameBuffer::Initialize(VkRenderPass render_pass) { std::vector attachments; if (!color_attachments_.empty()) { @@ -77,22 +78,26 @@ Result FrameBuffer::Initialize(VkRenderPass render_pass, } } - if (depth_format && depth_format->IsFormatKnown()) { - depth_image_ = MakeUnique( - device_, *depth_format, - static_cast(depth_format->HasStencilComponent() - ? VK_IMAGE_ASPECT_DEPTH_BIT | - VK_IMAGE_ASPECT_STENCIL_BIT - : VK_IMAGE_ASPECT_DEPTH_BIT), + if (depth_stencil_attachment_.buffer && + depth_stencil_attachment_.buffer->GetFormat()->IsFormatKnown()) { + VkImageAspectFlags aspect = 0; + if (depth_stencil_attachment_.buffer->GetFormat()->HasDepthComponent()) + aspect |= VK_IMAGE_ASPECT_DEPTH_BIT; + if (depth_stencil_attachment_.buffer->GetFormat()->HasStencilComponent()) + aspect |= VK_IMAGE_ASPECT_STENCIL_BIT; + assert(aspect != 0); + + depth_stencil_image_ = MakeUnique( + device_, *depth_stencil_attachment_.buffer->GetFormat(), aspect, VK_IMAGE_TYPE_2D, width_, height_, depth_, 1u, 0u, 1u); - Result r = depth_image_->Initialize( + Result r = depth_stencil_image_->Initialize( VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT); if (!r.IsSuccess()) return r; - attachments.push_back(depth_image_->GetVkImageView()); + attachments.push_back(depth_stencil_image_->GetVkImageView()); } VkFramebufferCreateInfo frame_buffer_info = VkFramebufferCreateInfo(); @@ -121,8 +126,8 @@ void FrameBuffer::ChangeFrameLayout(CommandBuffer* command, for (auto& img : color_images_) img->ImageBarrier(command, color_layout, color_stage); - if (depth_image_) - depth_image_->ImageBarrier(command, depth_layout, depth_stage); + if (depth_stencil_image_) + depth_stencil_image_->ImageBarrier(command, depth_layout, depth_stage); } void FrameBuffer::ChangeFrameToDrawLayout(CommandBuffer* command) { @@ -153,9 +158,12 @@ void FrameBuffer::ChangeFrameToWriteLayout(CommandBuffer* command) { VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT); } -void FrameBuffer::TransferColorImagesToHost(CommandBuffer* command) { +void FrameBuffer::TransferImagesToHost(CommandBuffer* command) { for (auto& img : color_images_) img->CopyToHost(command); + + if (depth_stencil_image_) + depth_stencil_image_->CopyToHost(command); } void FrameBuffer::CopyImagesToBuffers() { @@ -167,11 +175,21 @@ void FrameBuffer::CopyImagesToBuffers() { std::memcpy(values->data(), img->HostAccessibleMemoryPtr(), info->buffer->GetSizeInBytes()); } + + if (depth_stencil_image_) { + auto* values = depth_stencil_attachment_.buffer->ValuePtr(); + values->resize(depth_stencil_attachment_.buffer->GetSizeInBytes()); + std::memcpy(values->data(), depth_stencil_image_->HostAccessibleMemoryPtr(), + depth_stencil_attachment_.buffer->GetSizeInBytes()); + } } -void FrameBuffer::TransferColorImagesToDevice(CommandBuffer* command) { +void FrameBuffer::TransferImagesToDevice(CommandBuffer* command) { for (auto& img : color_images_) img->CopyToDevice(command); + + if (depth_stencil_image_) + depth_stencil_image_->CopyToDevice(command); } void FrameBuffer::CopyBuffersToImages() { @@ -186,6 +204,16 @@ void FrameBuffer::CopyBuffersToImages() { std::memcpy(img->HostAccessibleMemoryPtr(), values->data(), info->buffer->GetSizeInBytes()); } + + if (depth_stencil_image_) { + auto* values = depth_stencil_attachment_.buffer->ValuePtr(); + // Nothing to do if our local buffer is empty + if (!values->empty()) { + std::memcpy(depth_stencil_image_->HostAccessibleMemoryPtr(), + values->data(), + depth_stencil_attachment_.buffer->GetSizeInBytes()); + } + } } } // namespace vulkan diff --git a/src/vulkan/frame_buffer.h b/src/vulkan/frame_buffer.h index 202726b1c..064b6d389 100644 --- a/src/vulkan/frame_buffer.h +++ b/src/vulkan/frame_buffer.h @@ -33,11 +33,12 @@ class FrameBuffer { FrameBuffer( Device* device, const std::vector& color_attachments, + amber::Pipeline::BufferInfo depth_stencil_attachment, uint32_t width, uint32_t height); ~FrameBuffer(); - Result Initialize(VkRenderPass render_pass, const Format* depth_format); + Result Initialize(VkRenderPass render_pass); void ChangeFrameToDrawLayout(CommandBuffer* command); void ChangeFrameToProbeLayout(CommandBuffer* command); @@ -51,8 +52,8 @@ class FrameBuffer { // Only record the command for copying the image that backs this // framebuffer to the host accessible buffer. The actual submission // of the command must be done later. - void TransferColorImagesToHost(CommandBuffer* command); - void TransferColorImagesToDevice(CommandBuffer* command); + void TransferImagesToHost(CommandBuffer* command); + void TransferImagesToDevice(CommandBuffer* command); void CopyImagesToBuffers(); void CopyBuffersToImages(); @@ -69,9 +70,10 @@ class FrameBuffer { Device* device_ = nullptr; std::vector color_attachments_; + amber::Pipeline::BufferInfo depth_stencil_attachment_; VkFramebuffer frame_ = VK_NULL_HANDLE; std::vector> color_images_; - std::unique_ptr depth_image_; + std::unique_ptr depth_stencil_image_; uint32_t width_ = 0; uint32_t height_ = 0; uint32_t depth_ = 1; diff --git a/src/vulkan/graphics_pipeline.cc b/src/vulkan/graphics_pipeline.cc index a63819a34..6c7245bc0 100644 --- a/src/vulkan/graphics_pipeline.cc +++ b/src/vulkan/graphics_pipeline.cc @@ -381,14 +381,14 @@ class RenderPassGuard { GraphicsPipeline::GraphicsPipeline( Device* device, const std::vector& color_buffers, - Format* depth_stencil_format, + amber::Pipeline::BufferInfo depth_stencil_buffer, uint32_t fence_timeout_ms, const std::vector& shader_stage_info) : Pipeline(PipelineType::kGraphics, device, fence_timeout_ms, shader_stage_info), - depth_stencil_format_(depth_stencil_format) { + depth_stencil_buffer_(depth_stencil_buffer) { for (const auto& info : color_buffers) color_buffers_.push_back(&info); } @@ -426,10 +426,11 @@ Result GraphicsPipeline::CreateRenderPass() { subpass_desc.colorAttachmentCount = static_cast(color_refer.size()); subpass_desc.pColorAttachments = color_refer.data(); - if (depth_stencil_format_ && depth_stencil_format_->IsFormatKnown()) { + if (depth_stencil_buffer_.buffer && + depth_stencil_buffer_.buffer->GetFormat()->IsFormatKnown()) { attachment_desc.push_back(kDefaultAttachmentDesc); attachment_desc.back().format = - device_->GetVkFormat(*depth_stencil_format_); + device_->GetVkFormat(*depth_stencil_buffer_.buffer->GetFormat()); attachment_desc.back().initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; attachment_desc.back().finalLayout = @@ -650,7 +651,8 @@ Result GraphicsPipeline::CreateVkGraphicsPipeline( } VkPipelineDepthStencilStateCreateInfo depthstencil_info; - if (depth_stencil_format_ && depth_stencil_format_->IsFormatKnown()) { + if (depth_stencil_buffer_.buffer && + depth_stencil_buffer_.buffer->GetFormat()->IsFormatKnown()) { depthstencil_info = GetVkPipelineDepthStencilInfo(pipeline_data); pipeline_info.pDepthStencilState = &depthstencil_info; } @@ -694,8 +696,9 @@ Result GraphicsPipeline::Initialize(uint32_t width, if (!r.IsSuccess()) return r; - frame_ = MakeUnique(device_, color_buffers_, width, height); - r = frame_->Initialize(render_pass_, depth_stencil_format_); + frame_ = MakeUnique(device_, color_buffers_, + depth_stencil_buffer_, width, height); + r = frame_->Initialize(render_pass_); if (!r.IsSuccess()) return r; @@ -741,7 +744,8 @@ Result GraphicsPipeline::SetClearColor(float r, float g, float b, float a) { } Result GraphicsPipeline::SetClearStencil(uint32_t stencil) { - if (!depth_stencil_format_ || !depth_stencil_format_->IsFormatKnown()) { + if (!depth_stencil_buffer_.buffer || + !depth_stencil_buffer_.buffer->GetFormat()->IsFormatKnown()) { return Result( "Vulkan::ClearStencilCommand No DepthStencil Buffer for FrameBuffer " "Exists"); @@ -752,7 +756,8 @@ Result GraphicsPipeline::SetClearStencil(uint32_t stencil) { } Result GraphicsPipeline::SetClearDepth(float depth) { - if (!depth_stencil_format_ || !depth_stencil_format_->IsFormatKnown()) { + if (!depth_stencil_buffer_.buffer || + !depth_stencil_buffer_.buffer->GetFormat()->IsFormatKnown()) { return Result( "Vulkan::ClearStencilCommand No DepthStencil Buffer for FrameBuffer " "Exists"); @@ -767,32 +772,13 @@ Result GraphicsPipeline::Clear() { colour_clear.color = { {clear_color_r_, clear_color_g_, clear_color_b_, clear_color_a_}}; - Result r = ClearBuffer(colour_clear, VK_IMAGE_ASPECT_COLOR_BIT); - if (!r.IsSuccess()) - return r; - - if (!depth_stencil_format_ || !depth_stencil_format_->IsFormatKnown()) - return {}; - - VkClearValue depth_clear; - depth_clear.depthStencil = {clear_depth_, clear_stencil_}; - - return ClearBuffer( - depth_clear, - depth_stencil_format_ && depth_stencil_format_->HasStencilComponent() - ? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT - : VK_IMAGE_ASPECT_DEPTH_BIT); -} - -Result GraphicsPipeline::ClearBuffer(const VkClearValue& clear_value, - VkImageAspectFlags aspect) { CommandBufferGuard cmd_buf_guard(GetCommandBuffer()); if (!cmd_buf_guard.IsRecording()) return cmd_buf_guard.GetResult(); frame_->ChangeFrameToWriteLayout(GetCommandBuffer()); frame_->CopyBuffersToImages(); - frame_->TransferColorImagesToDevice(GetCommandBuffer()); + frame_->TransferImagesToDevice(GetCommandBuffer()); { RenderPassGuard render_pass_guard(this); @@ -800,9 +786,27 @@ Result GraphicsPipeline::ClearBuffer(const VkClearValue& clear_value, std::vector clears; for (size_t i = 0; i < color_buffers_.size(); ++i) { VkClearAttachment clear_attachment = VkClearAttachment(); - clear_attachment.aspectMask = aspect; + clear_attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; clear_attachment.colorAttachment = static_cast(i); - clear_attachment.clearValue = clear_value; + clear_attachment.clearValue = colour_clear; + + clears.push_back(clear_attachment); + } + + if (depth_stencil_buffer_.buffer && + depth_stencil_buffer_.buffer->GetFormat()->IsFormatKnown()) { + VkClearValue depth_stencil_clear; + depth_stencil_clear.depthStencil = {clear_depth_, clear_stencil_}; + + VkImageAspectFlags aspect = VK_IMAGE_ASPECT_DEPTH_BIT; + if (depth_stencil_buffer_.buffer->GetFormat()->HasStencilComponent()) + aspect |= VK_IMAGE_ASPECT_STENCIL_BIT; + + VkClearAttachment clear_attachment = VkClearAttachment(); + clear_attachment.aspectMask = aspect; + clear_attachment.colorAttachment = + static_cast(color_buffers_.size()); + clear_attachment.clearValue = depth_stencil_clear; clears.push_back(clear_attachment); } @@ -817,7 +821,7 @@ Result GraphicsPipeline::ClearBuffer(const VkClearValue& clear_value, clears.data(), 1, &clear_rect); } - frame_->TransferColorImagesToHost(command_.get()); + frame_->TransferImagesToHost(command_.get()); Result r = cmd_buf_guard.Submit(GetFenceTimeout()); if (!r.IsSuccess()) @@ -861,7 +865,7 @@ Result GraphicsPipeline::Draw(const DrawArraysCommand* command, frame_->ChangeFrameToWriteLayout(GetCommandBuffer()); frame_->CopyBuffersToImages(); - frame_->TransferColorImagesToDevice(GetCommandBuffer()); + frame_->TransferImagesToDevice(GetCommandBuffer()); { RenderPassGuard render_pass_guard(this); @@ -909,7 +913,7 @@ Result GraphicsPipeline::Draw(const DrawArraysCommand* command, } } - frame_->TransferColorImagesToHost(command_.get()); + frame_->TransferImagesToHost(command_.get()); r = cmd_buf_guard.Submit(GetFenceTimeout()); if (!r.IsSuccess()) diff --git a/src/vulkan/graphics_pipeline.h b/src/vulkan/graphics_pipeline.h index e08bdc377..7076231d3 100644 --- a/src/vulkan/graphics_pipeline.h +++ b/src/vulkan/graphics_pipeline.h @@ -42,7 +42,7 @@ class GraphicsPipeline : public Pipeline { GraphicsPipeline( Device* device, const std::vector& color_buffers, - Format* depth_stencil_format, + amber::Pipeline::BufferInfo depth_stencil_buffer, uint32_t fence_timeout_ms, const std::vector&); ~GraphicsPipeline() override; @@ -52,8 +52,6 @@ class GraphicsPipeline : public Pipeline { Result SetIndexBuffer(Buffer* buffer); Result Clear(); - Result ClearBuffer(const VkClearValue& clear_value, - VkImageAspectFlags aspect); Result SetClearColor(float r, float g, float b, float a); Result SetClearStencil(uint32_t stencil); @@ -90,7 +88,7 @@ class GraphicsPipeline : public Pipeline { // color buffers are owned by the amber::Pipeline. std::vector color_buffers_; - Format* depth_stencil_format_; + amber::Pipeline::BufferInfo depth_stencil_buffer_; std::unique_ptr index_buffer_; uint32_t frame_width_ = 0; diff --git a/src/vulkan/image_descriptor.cc b/src/vulkan/image_descriptor.cc index ee52787a6..bcb744d52 100644 --- a/src/vulkan/image_descriptor.cc +++ b/src/vulkan/image_descriptor.cc @@ -70,11 +70,22 @@ Result ImageDescriptor::CreateResourceIfNeeded() { break; } + Format* fmt = amber_buffer->GetFormat(); + VkImageAspectFlags aspect = 0; + if (fmt->HasDepthComponent() && fmt->HasStencilComponent()) { + aspect = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; + } else if (fmt->HasDepthComponent()) { + aspect = VK_IMAGE_ASPECT_DEPTH_BIT; + } else if (fmt->HasStencilComponent()) { + aspect = VK_IMAGE_ASPECT_DEPTH_BIT; + } else { + aspect = VK_IMAGE_ASPECT_COLOR_BIT; + } + transfer_image_ = MakeUnique( - device_, *amber_buffer->GetFormat(), VK_IMAGE_ASPECT_COLOR_BIT, - image_type, amber_buffer->GetWidth(), amber_buffer->GetHeight(), - amber_buffer->GetDepth(), amber_buffer->GetMipLevels(), base_mip_level_, - VK_REMAINING_MIP_LEVELS); + device_, *fmt, aspect, image_type, amber_buffer->GetWidth(), + amber_buffer->GetHeight(), amber_buffer->GetDepth(), + amber_buffer->GetMipLevels(), base_mip_level_, VK_REMAINING_MIP_LEVELS); VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; diff --git a/src/vulkan/transfer_image.cc b/src/vulkan/transfer_image.cc index c7d0058b5..4b3509b85 100644 --- a/src/vulkan/transfer_image.cc +++ b/src/vulkan/transfer_image.cc @@ -109,7 +109,15 @@ Result TransferImage::Initialize(VkImageUsageFlags usage) { if (!r.IsSuccess()) return r; - r = CreateVkImageView(); + if (aspect_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) && + !(usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) { + // Combined depth/stencil image used as a descriptor. Only one aspect can be + // used for the image view. + r = CreateVkImageView(VK_IMAGE_ASPECT_DEPTH_BIT); + } else { + r = CreateVkImageView(aspect_); + } + if (!r.IsSuccess()) return r; @@ -153,7 +161,7 @@ VkImageViewType TransferImage::GetImageViewType() const { return VK_IMAGE_VIEW_TYPE_2D; } -Result TransferImage::CreateVkImageView() { +Result TransferImage::CreateVkImageView(VkImageAspectFlags aspect) { VkImageViewCreateInfo image_view_info = VkImageViewCreateInfo(); image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; image_view_info.image = image_; @@ -166,7 +174,7 @@ Result TransferImage::CreateVkImageView() { VK_COMPONENT_SWIZZLE_A, }; image_view_info.subresourceRange = { - aspect_, /* aspectMask */ + aspect, /* aspectMask */ base_mip_level_, /* baseMipLevel */ used_mip_levels_, /* levelCount */ 0, /* baseArrayLayer */ @@ -182,15 +190,23 @@ Result TransferImage::CreateVkImageView() { return {}; } -VkBufferImageCopy TransferImage::CreateBufferImageCopy(uint32_t mip_level) { +VkBufferImageCopy TransferImage::CreateBufferImageCopy( + VkImageAspectFlags aspect, + uint32_t mip_level) { VkBufferImageCopy copy_region = VkBufferImageCopy(); - copy_region.bufferOffset = 0; + if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) { + // Store stencil data at the end of the buffer after depth data. + copy_region.bufferOffset = + GetSizeInBytes() - image_info_.extent.width * image_info_.extent.height; + } else { + copy_region.bufferOffset = 0; + } // Row length of 0 results in tight packing of rows, so the row stride // is the number of texels times the texel stride. copy_region.bufferRowLength = 0; copy_region.bufferImageHeight = 0; copy_region.imageSubresource = { - aspect_, /* aspectMask */ + aspect, /* aspectMask */ mip_level, /* mipLevel */ 0, /* baseArrayLayer */ 1, /* layerCount */ @@ -203,12 +219,17 @@ VkBufferImageCopy TransferImage::CreateBufferImageCopy(uint32_t mip_level) { } void TransferImage::CopyToHost(CommandBuffer* command_buffer) { + const VkImageAspectFlagBits aspects[] = {VK_IMAGE_ASPECT_COLOR_BIT, + VK_IMAGE_ASPECT_DEPTH_BIT, + VK_IMAGE_ASPECT_STENCIL_BIT}; std::vector copy_regions; uint32_t last_mip_level = used_mip_levels_ == VK_REMAINING_MIP_LEVELS ? mip_levels_ : base_mip_level_ + used_mip_levels_; for (uint32_t i = base_mip_level_; i < last_mip_level; i++) - copy_regions.push_back(CreateBufferImageCopy(i)); + for (auto aspect : aspects) + if (aspect_ & aspect) + copy_regions.push_back(CreateBufferImageCopy(aspect, i)); device_->GetPtrs()->vkCmdCopyImageToBuffer( command_buffer->GetVkCommandBuffer(), image_, @@ -219,12 +240,17 @@ void TransferImage::CopyToHost(CommandBuffer* command_buffer) { } void TransferImage::CopyToDevice(CommandBuffer* command_buffer) { + const VkImageAspectFlagBits aspects[] = {VK_IMAGE_ASPECT_COLOR_BIT, + VK_IMAGE_ASPECT_DEPTH_BIT, + VK_IMAGE_ASPECT_STENCIL_BIT}; std::vector copy_regions; uint32_t last_mip_level = used_mip_levels_ == VK_REMAINING_MIP_LEVELS ? mip_levels_ : base_mip_level_ + used_mip_levels_; for (uint32_t i = base_mip_level_; i < last_mip_level; i++) - copy_regions.push_back(CreateBufferImageCopy(i)); + for (auto aspect : aspects) + if (aspect_ & aspect) + copy_regions.push_back(CreateBufferImageCopy(aspect, i)); device_->GetPtrs()->vkCmdCopyBufferToImage( command_buffer->GetVkCommandBuffer(), host_accessible_buffer_, image_, diff --git a/src/vulkan/transfer_image.h b/src/vulkan/transfer_image.h index e68b88e04..fbdfa2346 100644 --- a/src/vulkan/transfer_image.h +++ b/src/vulkan/transfer_image.h @@ -56,13 +56,14 @@ class TransferImage : public Resource { void CopyToHost(CommandBuffer* command_buffer) override; private: - Result CreateVkImageView(); + Result CreateVkImageView(VkImageAspectFlags aspect); Result AllocateAndBindMemoryToVkImage(VkImage image, VkDeviceMemory* memory, VkMemoryPropertyFlags flags, bool force_flags, uint32_t* memory_type_index); - VkBufferImageCopy CreateBufferImageCopy(uint32_t mip_level); + VkBufferImageCopy CreateBufferImageCopy(VkImageAspectFlags aspect, + uint32_t mip_level); VkImageViewType GetImageViewType() const; From 7ab278416faabe79c22f9b64ff9075979ce69909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Suonp=C3=A4=C3=A4?= Date: Wed, 8 Apr 2020 09:08:04 +0300 Subject: [PATCH 2/4] Define copy constructor for PipelineData. --- src/pipeline_data.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pipeline_data.h b/src/pipeline_data.h index e44dd4ec9..dc67c03e1 100644 --- a/src/pipeline_data.h +++ b/src/pipeline_data.h @@ -28,6 +28,8 @@ class PipelineData { ~PipelineData(); PipelineData(const PipelineData&); + PipelineData& operator=(const PipelineData&) = default; + void SetTopology(Topology topo) { topology_ = topo; } Topology GetTopology() const { return topology_; } From ddb8afe2edcfe00ee27499ce9cff5bb1702e31d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Suonp=C3=A4=C3=A4?= Date: Thu, 9 Apr 2020 08:38:21 +0300 Subject: [PATCH 3/4] Addressed review comments. --- src/amberscript/parser.cc | 4 ++-- src/command.cc | 4 ++-- src/command.h | 13 ++++--------- src/vulkan/engine_vulkan.cc | 3 +-- src/vulkan/transfer_image.cc | 18 ++++++++++++------ 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/amberscript/parser.cc b/src/amberscript/parser.cc index 3550faed5..74b68b017 100644 --- a/src/amberscript/parser.cc +++ b/src/amberscript/parser.cc @@ -1805,9 +1805,9 @@ Result Parser::ParseRun() { if (!token->IsInteger()) return Result("missing X position for RUN command"); - auto cmd = MakeUnique(pipeline); + auto cmd = + MakeUnique(pipeline, *pipeline->GetPipelineData()); cmd->SetLine(line); - cmd->SetPolygonMode(pipeline->GetPipelineData()->GetPolygonMode()); Result r = token->ConvertToDouble(); if (!r.IsSuccess()) diff --git a/src/command.cc b/src/command.cc index 24e2f986c..ea242b728 100644 --- a/src/command.cc +++ b/src/command.cc @@ -96,8 +96,8 @@ DrawRectCommand::DrawRectCommand(Pipeline* pipeline, PipelineData data) DrawRectCommand::~DrawRectCommand() = default; -DrawGridCommand::DrawGridCommand(Pipeline* pipeline) - : PipelineCommand(Type::kDrawGrid, pipeline) {} +DrawGridCommand::DrawGridCommand(Pipeline* pipeline, PipelineData data) + : PipelineCommand(Type::kDrawGrid, pipeline), data_(data) {} DrawGridCommand::~DrawGridCommand() = default; diff --git a/src/command.h b/src/command.h index 048196296..6f119caaf 100644 --- a/src/command.h +++ b/src/command.h @@ -175,8 +175,6 @@ class DrawRectCommand : public PipelineCommand { void SetHeight(float h) { height_ = h; } float GetHeight() const { return height_; } - void SetPolygonMode(PolygonMode mode) { data_.SetPolygonMode(mode); } - std::string ToString() const override { return "DrawRectCommand"; } private: @@ -192,9 +190,11 @@ class DrawRectCommand : public PipelineCommand { /// Command to draw a grid of recrangles on screen. class DrawGridCommand : public PipelineCommand { public: - explicit DrawGridCommand(Pipeline* pipeline); + explicit DrawGridCommand(Pipeline* pipeline, PipelineData data); ~DrawGridCommand() override; + const PipelineData* GetPipelineData() const { return &data_; } + void SetX(float x) { x_ = x; } float GetX() const { return x_; } @@ -213,19 +213,16 @@ class DrawGridCommand : public PipelineCommand { void SetRows(uint32_t r) { rows_ = r; } uint32_t GetRows() const { return rows_; } - void SetPolygonMode(PolygonMode mode) { polygon_mode_ = mode; } - PolygonMode GetPolygonMode() const { return polygon_mode_; } - std::string ToString() const override { return "DrawGridCommand"; } private: + PipelineData data_; float x_ = 0.0; float y_ = 0.0; float width_ = 0.0; float height_ = 0.0; uint32_t columns_ = 0; uint32_t rows_ = 0; - PolygonMode polygon_mode_ = PolygonMode::kFill; }; /// Command to draw from a vertex and index buffer. @@ -245,8 +242,6 @@ class DrawArraysCommand : public PipelineCommand { void SetTopology(Topology topo) { topology_ = topo; } Topology GetTopology() const { return topology_; } - void SetPolygonMode(PolygonMode mode) { data_.SetPolygonMode(mode); } - void SetFirstVertexIndex(uint32_t idx) { first_vertex_index_ = idx; } uint32_t GetFirstVertexIndex() const { return first_vertex_index_; } diff --git a/src/vulkan/engine_vulkan.cc b/src/vulkan/engine_vulkan.cc index 8db66813a..561df16a0 100644 --- a/src/vulkan/engine_vulkan.cc +++ b/src/vulkan/engine_vulkan.cc @@ -526,12 +526,11 @@ Result EngineVulkan::DoDrawGrid(const DrawGridCommand* command) { auto vertex_buffer = MakeUnique(device_.get()); vertex_buffer->SetData(0, buf.get()); - DrawArraysCommand draw(command->GetPipeline(), PipelineData{}); + DrawArraysCommand draw(command->GetPipeline(), *command->GetPipelineData()); draw.SetTopology(Topology::kTriangleList); draw.SetFirstVertexIndex(0); draw.SetVertexCount(vertices); draw.SetInstanceCount(1); - draw.SetPolygonMode(command->GetPolygonMode()); Result r = graphics->Draw(&draw, vertex_buffer.get()); if (!r.IsSuccess()) diff --git a/src/vulkan/transfer_image.cc b/src/vulkan/transfer_image.cc index 4b3509b85..d510b7922 100644 --- a/src/vulkan/transfer_image.cc +++ b/src/vulkan/transfer_image.cc @@ -226,10 +226,13 @@ void TransferImage::CopyToHost(CommandBuffer* command_buffer) { uint32_t last_mip_level = used_mip_levels_ == VK_REMAINING_MIP_LEVELS ? mip_levels_ : base_mip_level_ + used_mip_levels_; - for (uint32_t i = base_mip_level_; i < last_mip_level; i++) - for (auto aspect : aspects) - if (aspect_ & aspect) + for (uint32_t i = base_mip_level_; i < last_mip_level; i++) { + for (auto aspect : aspects) { + if (aspect_ & aspect) { copy_regions.push_back(CreateBufferImageCopy(aspect, i)); + } + } + } device_->GetPtrs()->vkCmdCopyImageToBuffer( command_buffer->GetVkCommandBuffer(), image_, @@ -247,10 +250,13 @@ void TransferImage::CopyToDevice(CommandBuffer* command_buffer) { uint32_t last_mip_level = used_mip_levels_ == VK_REMAINING_MIP_LEVELS ? mip_levels_ : base_mip_level_ + used_mip_levels_; - for (uint32_t i = base_mip_level_; i < last_mip_level; i++) - for (auto aspect : aspects) - if (aspect_ & aspect) + for (uint32_t i = base_mip_level_; i < last_mip_level; i++) { + for (auto aspect : aspects) { + if (aspect_ & aspect) { copy_regions.push_back(CreateBufferImageCopy(aspect, i)); + } + } + } device_->GetPtrs()->vkCmdCopyBufferToImage( command_buffer->GetVkCommandBuffer(), host_accessible_buffer_, image_, From 3c881583b87f056b368eb3447700709de8c2223a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Suonp=C3=A4=C3=A4?= Date: Wed, 15 Apr 2020 08:16:13 +0300 Subject: [PATCH 4/4] Removed explicit. --- src/command.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/command.h b/src/command.h index 6f119caaf..1c91e08cc 100644 --- a/src/command.h +++ b/src/command.h @@ -190,7 +190,7 @@ class DrawRectCommand : public PipelineCommand { /// Command to draw a grid of recrangles on screen. class DrawGridCommand : public PipelineCommand { public: - explicit DrawGridCommand(Pipeline* pipeline, PipelineData data); + DrawGridCommand(Pipeline* pipeline, PipelineData data); ~DrawGridCommand() override; const PipelineData* GetPipelineData() const { return &data_; }