Reland: Encode directly to command buffer. (flutter/engine#49821)
The original change was reverted as it broke the GPU tracer reset logic. The previous logic assumed that the first started command buffer would also be the first submitted command buffer. With the direct encoding, this is no longer the case - so we shift the logic so that we reset query pools once at startup and then after the queries are finished being recorded. This might actually be better in general since we should be doing less work in the frame workload.
This commit is contained in:
@@ -76,6 +76,7 @@ impeller_component("context_spy") {
|
||||
"testing/context_spy.h",
|
||||
]
|
||||
deps = [
|
||||
"//flutter/impeller/entity:entity_test_helpers",
|
||||
"//flutter/impeller/renderer",
|
||||
"//flutter/testing:testing_lib",
|
||||
]
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "impeller/aiks/testing/context_spy.h"
|
||||
|
||||
namespace impeller {
|
||||
@@ -64,14 +66,17 @@ std::shared_ptr<ContextMock> ContextSpy::MakeContext(
|
||||
});
|
||||
|
||||
ON_CALL(*spy, OnCreateRenderPass)
|
||||
.WillByDefault(
|
||||
[real_buffer, shared_this](const RenderTarget& render_target) {
|
||||
std::shared_ptr<RenderPass> result =
|
||||
CommandBufferMock::ForwardOnCreateRenderPass(
|
||||
real_buffer.get(), render_target);
|
||||
shared_this->render_passes_.push_back(result);
|
||||
return result;
|
||||
});
|
||||
.WillByDefault([real_buffer, shared_this,
|
||||
real_context](const RenderTarget& render_target) {
|
||||
std::shared_ptr<RenderPass> result =
|
||||
CommandBufferMock::ForwardOnCreateRenderPass(
|
||||
real_buffer.get(), render_target);
|
||||
std::shared_ptr<RecordingRenderPass> recorder =
|
||||
std::make_shared<RecordingRenderPass>(result, real_context,
|
||||
render_target);
|
||||
shared_this->render_passes_.push_back(recorder);
|
||||
return recorder;
|
||||
});
|
||||
|
||||
ON_CALL(*spy, OnCreateBlitPass).WillByDefault([real_buffer]() {
|
||||
return CommandBufferMock::ForwardOnCreateBlitPass(real_buffer.get());
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
#define FLUTTER_IMPELLER_AIKS_TESTING_CONTEXT_SPY_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "impeller/aiks/testing/context_mock.h"
|
||||
#include "impeller/entity/contents/test/recording_render_pass.h"
|
||||
|
||||
namespace impeller {
|
||||
namespace testing {
|
||||
@@ -20,7 +22,7 @@ class ContextSpy : public std::enable_shared_from_this<ContextSpy> {
|
||||
std::shared_ptr<ContextMock> MakeContext(
|
||||
const std::shared_ptr<Context>& real_context);
|
||||
|
||||
std::vector<std::shared_ptr<RenderPass>> render_passes_;
|
||||
std::vector<std::shared_ptr<RecordingRenderPass>> render_passes_;
|
||||
|
||||
private:
|
||||
ContextSpy() = default;
|
||||
|
||||
@@ -161,7 +161,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader {
|
||||
{% endfor %}) {
|
||||
return {{ proto.args.0.argument_name }}.BindResource({% for arg in proto.args %}
|
||||
{% if loop.is_first %}
|
||||
{{to_shader_stage(shader_stage)}}, kResource{{ proto.name }}, kMetadata{{ proto.name }}, {% else %}
|
||||
{{to_shader_stage(shader_stage)}}, {{ proto.descriptor_type }}, kResource{{ proto.name }}, kMetadata{{ proto.name }}, {% else %}
|
||||
std::move({{ arg.argument_name }}){% if not loop.is_last %}, {% endif %}
|
||||
{% endif %}
|
||||
{% endfor %});
|
||||
|
||||
@@ -1308,6 +1308,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
|
||||
auto& proto = prototypes.emplace_back(BindPrototype{});
|
||||
proto.return_type = "bool";
|
||||
proto.name = ToCamelCase(uniform_buffer.name);
|
||||
proto.descriptor_type = "DescriptorType::kUniformBuffer";
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "Bind uniform buffer for resource named " << uniform_buffer.name
|
||||
@@ -1327,6 +1328,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
|
||||
auto& proto = prototypes.emplace_back(BindPrototype{});
|
||||
proto.return_type = "bool";
|
||||
proto.name = ToCamelCase(storage_buffer.name);
|
||||
proto.descriptor_type = "DescriptorType::kStorageBuffer";
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "Bind storage buffer for resource named " << storage_buffer.name
|
||||
@@ -1346,6 +1348,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
|
||||
auto& proto = prototypes.emplace_back(BindPrototype{});
|
||||
proto.return_type = "bool";
|
||||
proto.name = ToCamelCase(sampled_image.name);
|
||||
proto.descriptor_type = "DescriptorType::kSampledImage";
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "Bind combined image sampler for resource named "
|
||||
@@ -1369,6 +1372,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
|
||||
auto& proto = prototypes.emplace_back(BindPrototype{});
|
||||
proto.return_type = "bool";
|
||||
proto.name = ToCamelCase(separate_image.name);
|
||||
proto.descriptor_type = "DescriptorType::kImage";
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "Bind separate image for resource named " << separate_image.name
|
||||
@@ -1388,6 +1392,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
|
||||
auto& proto = prototypes.emplace_back(BindPrototype{});
|
||||
proto.return_type = "bool";
|
||||
proto.name = ToCamelCase(separate_sampler.name);
|
||||
proto.descriptor_type = "DescriptorType::kSampler";
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "Bind separate sampler for resource named "
|
||||
@@ -1416,6 +1421,7 @@ nlohmann::json::array_t Reflector::EmitBindPrototypes(
|
||||
item["return_type"] = res.return_type;
|
||||
item["name"] = res.name;
|
||||
item["docstring"] = res.docstring;
|
||||
item["descriptor_type"] = res.descriptor_type;
|
||||
auto& args = item["args"] = nlohmann::json::array_t{};
|
||||
for (const auto& arg : res.args) {
|
||||
auto& json_arg = args.emplace_back(nlohmann::json::object_t{});
|
||||
|
||||
@@ -187,6 +187,7 @@ class Reflector {
|
||||
std::string name;
|
||||
std::string return_type;
|
||||
std::string docstring;
|
||||
std::string descriptor_type = "";
|
||||
std::vector<BindPrototypeArgument> args;
|
||||
};
|
||||
|
||||
|
||||
@@ -24,11 +24,13 @@ struct ResourceBinder {
|
||||
virtual ~ResourceBinder() = default;
|
||||
|
||||
virtual bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) = 0;
|
||||
|
||||
virtual bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
|
||||
@@ -257,6 +257,8 @@ impeller_component("entity_test_helpers") {
|
||||
sources = [
|
||||
"contents/test/contents_test_helpers.cc",
|
||||
"contents/test/contents_test_helpers.h",
|
||||
"contents/test/recording_render_pass.cc",
|
||||
"contents/test/recording_render_pass.h",
|
||||
]
|
||||
|
||||
deps = [ ":entity" ]
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "impeller/entity/contents/checkerboard_contents.h"
|
||||
#include "impeller/entity/contents/contents.h"
|
||||
#include "impeller/entity/contents/test/recording_render_pass.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
#include "impeller/entity/entity_playground.h"
|
||||
#include "impeller/renderer/render_target.h"
|
||||
@@ -38,11 +39,14 @@ TEST_P(EntityTest, RendersWithoutError) {
|
||||
*GetContentContext()->GetRenderTargetCache(), {100, 100},
|
||||
/*mip_count=*/1);
|
||||
auto render_pass = buffer->CreateRenderPass(render_target);
|
||||
auto recording_pass = std::make_shared<RecordingRenderPass>(
|
||||
render_pass, GetContext(), render_target);
|
||||
|
||||
Entity entity;
|
||||
|
||||
ASSERT_TRUE(render_pass->GetCommands().empty());
|
||||
ASSERT_TRUE(contents->Render(*content_context, entity, *render_pass));
|
||||
ASSERT_FALSE(render_pass->GetCommands().empty());
|
||||
ASSERT_TRUE(recording_pass->GetCommands().empty());
|
||||
ASSERT_TRUE(contents->Render(*content_context, entity, *recording_pass));
|
||||
ASSERT_FALSE(recording_pass->GetCommands().empty());
|
||||
}
|
||||
#endif // IMPELLER_DEBUG
|
||||
|
||||
|
||||
@@ -199,8 +199,9 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
|
||||
ShaderUniformSlot uniform_slot;
|
||||
uniform_slot.name = uniform.name.c_str();
|
||||
uniform_slot.ext_res_0 = uniform.location;
|
||||
pass.BindResource(ShaderStage::kFragment, uniform_slot, metadata,
|
||||
buffer_view);
|
||||
pass.BindResource(ShaderStage::kFragment,
|
||||
DescriptorType::kUniformBuffer, uniform_slot,
|
||||
metadata, buffer_view);
|
||||
buffer_index++;
|
||||
buffer_offset += uniform.GetSize();
|
||||
break;
|
||||
@@ -237,7 +238,8 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
|
||||
auto buffer_view = renderer.GetTransientsBuffer().Emplace(
|
||||
reinterpret_cast<const void*>(uniform_buffer.data()),
|
||||
sizeof(float) * uniform_buffer.size(), alignment);
|
||||
pass.BindResource(ShaderStage::kFragment, uniform_slot,
|
||||
pass.BindResource(ShaderStage::kFragment,
|
||||
DescriptorType::kUniformBuffer, uniform_slot,
|
||||
ShaderMetadata{}, buffer_view);
|
||||
}
|
||||
}
|
||||
@@ -271,8 +273,8 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
|
||||
|
||||
image_slot.binding = sampler_binding_location;
|
||||
image_slot.texture_index = uniform.location - minimum_sampler_index;
|
||||
pass.BindResource(ShaderStage::kFragment, image_slot, *metadata,
|
||||
input.texture, sampler);
|
||||
pass.BindResource(ShaderStage::kFragment, DescriptorType::kSampledImage,
|
||||
image_slot, *metadata, input.texture, sampler);
|
||||
|
||||
sampler_index++;
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/entity/contents/test/recording_render_pass.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace impeller {
|
||||
|
||||
RecordingRenderPass::RecordingRenderPass(
|
||||
std::shared_ptr<RenderPass> delegate,
|
||||
const std::shared_ptr<Context>& context,
|
||||
const RenderTarget& render_target)
|
||||
: RenderPass(context, render_target), delegate_(std::move(delegate)) {}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::SetPipeline(
|
||||
const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline) {
|
||||
pending_.pipeline = pipeline;
|
||||
delegate_->SetPipeline(pipeline);
|
||||
}
|
||||
|
||||
void RecordingRenderPass::SetCommandLabel(std::string_view label) {
|
||||
#ifdef IMPELLER_DEBUG
|
||||
pending_.label = std::string(label);
|
||||
#endif // IMPELLER_DEBUG
|
||||
delegate_->SetCommandLabel(label);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::SetStencilReference(uint32_t value) {
|
||||
pending_.stencil_reference = value;
|
||||
delegate_->SetStencilReference(value);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::SetBaseVertex(uint64_t value) {
|
||||
pending_.base_vertex = value;
|
||||
delegate_->SetBaseVertex(value);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::SetViewport(Viewport viewport) {
|
||||
pending_.viewport = viewport;
|
||||
delegate_->SetViewport(viewport);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::SetScissor(IRect scissor) {
|
||||
pending_.scissor = scissor;
|
||||
delegate_->SetScissor(scissor);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::SetInstanceCount(size_t count) {
|
||||
pending_.instance_count = count;
|
||||
delegate_->SetInstanceCount(count);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
bool RecordingRenderPass::SetVertexBuffer(VertexBuffer buffer) {
|
||||
pending_.vertex_buffer = buffer;
|
||||
return delegate_->SetVertexBuffer(buffer);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
fml::Status RecordingRenderPass::Draw() {
|
||||
commands_.emplace_back(std::move(pending_));
|
||||
pending_ = {};
|
||||
return delegate_->Draw();
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
void RecordingRenderPass::OnSetLabel(std::string label) {
|
||||
return;
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
bool RecordingRenderPass::OnEncodeCommands(const Context& context) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
bool RecordingRenderPass::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) {
|
||||
pending_.BindResource(stage, type, slot, metadata, view);
|
||||
return delegate_->BindResource(stage, type, slot, metadata, view);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
bool RecordingRenderPass::BindResource(
|
||||
ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view) {
|
||||
pending_.BindResource(stage, type, slot, metadata, view);
|
||||
return delegate_->BindResource(stage, type, slot, metadata, view);
|
||||
}
|
||||
|
||||
// |RenderPass|
|
||||
bool RecordingRenderPass::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) {
|
||||
pending_.BindResource(stage, type, slot, metadata, texture, sampler);
|
||||
return delegate_->BindResource(stage, type, slot, metadata, texture, sampler);
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -0,0 +1,87 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_TEST_RECORDING_RENDER_PASS_H_
|
||||
#define FLUTTER_IMPELLER_ENTITY_CONTENTS_TEST_RECORDING_RENDER_PASS_H_
|
||||
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class RecordingRenderPass : public RenderPass {
|
||||
public:
|
||||
explicit RecordingRenderPass(std::shared_ptr<RenderPass> delegate,
|
||||
const std::shared_ptr<Context>& context,
|
||||
const RenderTarget& render_target);
|
||||
|
||||
~RecordingRenderPass() = default;
|
||||
|
||||
const std::vector<Command>& GetCommands() const override { return commands_; }
|
||||
|
||||
// |RenderPass|
|
||||
void SetPipeline(
|
||||
const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline) override;
|
||||
|
||||
void SetCommandLabel(std::string_view label) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetStencilReference(uint32_t value) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetBaseVertex(uint64_t value) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetViewport(Viewport viewport) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetScissor(IRect scissor) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetInstanceCount(size_t count) override;
|
||||
|
||||
// |RenderPass|
|
||||
bool SetVertexBuffer(VertexBuffer buffer) override;
|
||||
|
||||
// |RenderPass|
|
||||
fml::Status Draw() override;
|
||||
|
||||
// |RenderPass|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
// |RenderPass|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
// |RenderPass|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) override;
|
||||
|
||||
// |RenderPass|
|
||||
void OnSetLabel(std::string label) override;
|
||||
|
||||
// |RenderPass|
|
||||
bool OnEncodeCommands(const Context& context) const override;
|
||||
|
||||
bool IsValid() const override { return true; }
|
||||
|
||||
private:
|
||||
Command pending_;
|
||||
std::shared_ptr<RenderPass> delegate_;
|
||||
std::vector<Command> commands_;
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
#endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_TEST_RECORDING_RENDER_PASS_H_
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/texture_descriptor.h"
|
||||
#include "impeller/entity/contents/test/recording_render_pass.h"
|
||||
#include "impeller/entity/contents/tiled_texture_contents.h"
|
||||
#include "impeller/entity/entity_playground.h"
|
||||
#include "impeller/playground/playground_test.h"
|
||||
@@ -36,9 +37,11 @@ TEST_P(EntityTest, TiledTextureContentsRendersWithCorrectPipeline) {
|
||||
*GetContentContext()->GetRenderTargetCache(), {100, 100},
|
||||
/*mip_count=*/1);
|
||||
auto render_pass = buffer->CreateRenderPass(render_target);
|
||||
auto recording_pass = std::make_shared<RecordingRenderPass>(
|
||||
render_pass, GetContext(), render_target);
|
||||
|
||||
ASSERT_TRUE(contents.Render(*GetContentContext(), {}, *render_pass));
|
||||
const std::vector<Command>& commands = render_pass->GetCommands();
|
||||
ASSERT_TRUE(contents.Render(*GetContentContext(), {}, *recording_pass));
|
||||
const std::vector<Command>& commands = recording_pass->GetCommands();
|
||||
|
||||
ASSERT_EQ(commands.size(), 1u);
|
||||
ASSERT_STREQ(commands[0].pipeline->GetDescriptor().GetLabel().c_str(),
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "impeller/entity/contents/contents.h"
|
||||
#include "impeller/entity/contents/solid_color_contents.h"
|
||||
#include "impeller/entity/contents/test/contents_test_helpers.h"
|
||||
#include "impeller/entity/contents/test/recording_render_pass.h"
|
||||
#include "impeller/entity/contents/vertices_contents.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
#include "impeller/entity/entity_playground.h"
|
||||
@@ -64,12 +65,15 @@ TEST_P(EntityTest, RendersDstPerColorWithAlpha) {
|
||||
*GetContentContext()->GetRenderTargetCache(), {100, 100},
|
||||
/*mip_count=*/1);
|
||||
auto render_pass = buffer->CreateRenderPass(render_target);
|
||||
auto recording_pass = std::make_shared<RecordingRenderPass>(
|
||||
render_pass, GetContext(), render_target);
|
||||
Entity entity;
|
||||
|
||||
ASSERT_TRUE(render_pass->GetCommands().empty());
|
||||
ASSERT_TRUE(contents->Render(*content_context, entity, *render_pass));
|
||||
ASSERT_TRUE(recording_pass->GetCommands().empty());
|
||||
ASSERT_TRUE(contents->Render(*content_context, entity, *recording_pass));
|
||||
|
||||
const auto& cmd = render_pass->GetCommands()[0];
|
||||
ASSERT_TRUE(recording_pass->GetCommands().size() > 0);
|
||||
const auto& cmd = recording_pass->GetCommands()[0];
|
||||
auto* frag_uniforms = GetFragInfo<FS>(cmd);
|
||||
|
||||
ASSERT_TRUE(frag_uniforms);
|
||||
|
||||
@@ -126,7 +126,7 @@ void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
|
||||
return; // Nothing to render.
|
||||
}
|
||||
auto host_buffer = impeller::HostBuffer::Create(
|
||||
render_pass.GetContext().lock()->GetResourceAllocator());
|
||||
render_pass.GetContext()->GetResourceAllocator());
|
||||
|
||||
using VS = impeller::ImguiRasterVertexShader;
|
||||
using FS = impeller::ImguiRasterFragmentShader;
|
||||
|
||||
@@ -49,8 +49,12 @@ std::shared_ptr<RenderPass> CommandBufferGLES::OnCreateRenderPass(
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto context = context_.lock();
|
||||
if (!context) {
|
||||
return nullptr;
|
||||
}
|
||||
auto pass = std::shared_ptr<RenderPassGLES>(
|
||||
new RenderPassGLES(context_, target, reactor_));
|
||||
new RenderPassGLES(context, target, reactor_));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
RenderPassGLES::RenderPassGLES(std::weak_ptr<const Context> context,
|
||||
RenderPassGLES::RenderPassGLES(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target,
|
||||
ReactorGLES::Ref reactor)
|
||||
: RenderPass(std::move(context), target),
|
||||
|
||||
@@ -26,7 +26,7 @@ class RenderPassGLES final
|
||||
std::string label_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
RenderPassGLES(std::weak_ptr<const Context> context,
|
||||
RenderPassGLES(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target,
|
||||
ReactorGLES::Ref reactor);
|
||||
|
||||
|
||||
@@ -275,8 +275,12 @@ std::shared_ptr<RenderPass> CommandBufferMTL::OnCreateRenderPass(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto context = context_.lock();
|
||||
if (!context) {
|
||||
return nullptr;
|
||||
}
|
||||
auto pass = std::shared_ptr<RenderPassMTL>(
|
||||
new RenderPassMTL(context_, target, buffer_));
|
||||
new RenderPassMTL(context, target, buffer_));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class RenderPassMTL final : public RenderPass {
|
||||
std::string label_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
RenderPassMTL(std::weak_ptr<const Context> context,
|
||||
RenderPassMTL(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target,
|
||||
id<MTLCommandBuffer> buffer);
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ static MTLRenderPassDescriptor* ToMTLRenderPassDescriptor(
|
||||
return result;
|
||||
}
|
||||
|
||||
RenderPassMTL::RenderPassMTL(std::weak_ptr<const Context> context,
|
||||
RenderPassMTL::RenderPassMTL(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target,
|
||||
id<MTLCommandBuffer> buffer)
|
||||
: RenderPass(std::move(context), target),
|
||||
|
||||
@@ -20,11 +20,6 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
// Warning: if any of the constant values or layouts are changed in the
|
||||
// framebuffer fetch shader, then this input binding may need to be
|
||||
// manually changed.
|
||||
static constexpr size_t kMagicSubpassInputBinding = 64;
|
||||
|
||||
static bool BindImages(
|
||||
const Bindings& bindings,
|
||||
Allocator& allocator,
|
||||
@@ -125,67 +120,6 @@ static bool BindBuffers(
|
||||
return true;
|
||||
}
|
||||
|
||||
fml::StatusOr<vk::DescriptorSet> AllocateAndBindDescriptorSets(
|
||||
const ContextVK& context,
|
||||
const std::shared_ptr<CommandEncoderVK>& encoder,
|
||||
Allocator& allocator,
|
||||
const Command& command,
|
||||
const TextureVK& input_attachment,
|
||||
std::array<vk::DescriptorImageInfo, kMaxBindings>& image_workspace,
|
||||
std::array<vk::DescriptorBufferInfo, kMaxBindings>& buffer_workspace,
|
||||
std::array<vk::WriteDescriptorSet, kMaxBindings + kMaxBindings>&
|
||||
write_workspace) {
|
||||
auto descriptor_result = encoder->AllocateDescriptorSets(
|
||||
PipelineVK::Cast(*command.pipeline).GetDescriptorSetLayout(), context);
|
||||
if (!descriptor_result.ok()) {
|
||||
return descriptor_result.status();
|
||||
}
|
||||
vk::DescriptorSet descriptor_set = descriptor_result.value();
|
||||
|
||||
size_t buffer_offset = 0u;
|
||||
size_t image_offset = 0u;
|
||||
size_t write_offset = 0u;
|
||||
|
||||
auto& pipeline_descriptor = command.pipeline->GetDescriptor();
|
||||
auto& desc_set =
|
||||
pipeline_descriptor.GetVertexDescriptor()->GetDescriptorSetLayouts();
|
||||
|
||||
if (!BindBuffers(command.vertex_bindings, allocator, encoder, descriptor_set,
|
||||
desc_set, buffer_workspace, buffer_offset, write_workspace,
|
||||
write_offset) ||
|
||||
!BindBuffers(command.fragment_bindings, allocator, encoder,
|
||||
descriptor_set, desc_set, buffer_workspace, buffer_offset,
|
||||
write_workspace, write_offset) ||
|
||||
!BindImages(command.fragment_bindings, allocator, encoder, descriptor_set,
|
||||
image_workspace, image_offset, write_workspace,
|
||||
write_offset)) {
|
||||
return fml::Status(fml::StatusCode::kUnknown,
|
||||
"Failed to bind texture or buffer.");
|
||||
}
|
||||
|
||||
if (pipeline_descriptor.UsesSubpassInput()) {
|
||||
vk::DescriptorImageInfo image_info;
|
||||
image_info.imageLayout = vk::ImageLayout::eGeneral;
|
||||
image_info.sampler = VK_NULL_HANDLE;
|
||||
image_info.imageView = input_attachment.GetImageView();
|
||||
image_workspace[image_offset++] = image_info;
|
||||
|
||||
vk::WriteDescriptorSet write_set;
|
||||
write_set.dstSet = descriptor_set;
|
||||
write_set.dstBinding = kMagicSubpassInputBinding;
|
||||
write_set.descriptorCount = 1u;
|
||||
write_set.descriptorType = vk::DescriptorType::eInputAttachment;
|
||||
write_set.pImageInfo = &image_workspace[image_offset - 1];
|
||||
|
||||
write_workspace[write_offset++] = write_set;
|
||||
}
|
||||
|
||||
context.GetDevice().updateDescriptorSets(write_offset, write_workspace.data(),
|
||||
0u, {});
|
||||
|
||||
return descriptor_set;
|
||||
}
|
||||
|
||||
fml::StatusOr<vk::DescriptorSet> AllocateAndBindDescriptorSets(
|
||||
const ContextVK& context,
|
||||
const std::shared_ptr<CommandEncoderVK>& encoder,
|
||||
|
||||
@@ -17,17 +17,6 @@ namespace impeller {
|
||||
// backend to avoid dynamic heap allocations.
|
||||
static constexpr size_t kMaxBindings = 32;
|
||||
|
||||
fml::StatusOr<vk::DescriptorSet> AllocateAndBindDescriptorSets(
|
||||
const ContextVK& context,
|
||||
const std::shared_ptr<CommandEncoderVK>& encoder,
|
||||
Allocator& allocator,
|
||||
const Command& command,
|
||||
const TextureVK& input_attachment,
|
||||
std::array<vk::DescriptorImageInfo, kMaxBindings>& image_workspace,
|
||||
std::array<vk::DescriptorBufferInfo, kMaxBindings>& buffer_workspace,
|
||||
std::array<vk::WriteDescriptorSet, kMaxBindings + kMaxBindings>&
|
||||
write_workspace);
|
||||
|
||||
fml::StatusOr<vk::DescriptorSet> AllocateAndBindDescriptorSets(
|
||||
const ContextVK& context,
|
||||
const std::shared_ptr<CommandEncoderVK>& encoder,
|
||||
|
||||
@@ -7,13 +7,10 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/renderer/backend/vulkan/blit_pass_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/compute_pass_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/formats_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/render_pass_vk.h"
|
||||
#include "impeller/renderer/command_buffer.h"
|
||||
#include "impeller/renderer/render_target.h"
|
||||
@@ -73,9 +70,9 @@ std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
|
||||
return nullptr;
|
||||
}
|
||||
auto pass =
|
||||
std::shared_ptr<RenderPassVK>(new RenderPassVK(context, //
|
||||
target, //
|
||||
weak_from_this() //
|
||||
std::shared_ptr<RenderPassVK>(new RenderPassVK(context, //
|
||||
target, //
|
||||
shared_from_this() //
|
||||
));
|
||||
if (!pass->IsValid()) {
|
||||
return nullptr;
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_COMMAND_BUFFER_VK_H_
|
||||
#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_COMMAND_BUFFER_VK_H_
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/base/backend_cast.h"
|
||||
#include "impeller/renderer/backend/vulkan/vk.h"
|
||||
#include "impeller/renderer/command_buffer.h"
|
||||
|
||||
@@ -449,7 +449,8 @@ void ContextVK::Setup(Settings settings) {
|
||||
|
||||
// Create the GPU Tracer later because it depends on state from
|
||||
// the ContextVK.
|
||||
gpu_tracer_ = std::make_shared<GPUTracerVK>(GetDeviceHolder());
|
||||
gpu_tracer_ = std::make_shared<GPUTracerVK>(weak_from_this());
|
||||
gpu_tracer_->InitializeQueryPool(*this);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// Label all the relevant objects. This happens after setup so that the
|
||||
|
||||
@@ -8,19 +8,25 @@
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
#include "fml/logging.h"
|
||||
#include "fml/trace_event.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
|
||||
#include "vulkan/vulkan.hpp"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
static constexpr uint32_t kPoolSize = 1024u;
|
||||
static constexpr uint32_t kPoolSize = 128u;
|
||||
|
||||
GPUTracerVK::GPUTracerVK(const std::shared_ptr<DeviceHolder>& device_holder)
|
||||
: device_holder_(device_holder) {
|
||||
timestamp_period_ = device_holder_->GetPhysicalDevice()
|
||||
GPUTracerVK::GPUTracerVK(std::weak_ptr<ContextVK> context)
|
||||
: context_(std::move(context)) {
|
||||
timestamp_period_ = context_.lock()
|
||||
->GetDeviceHolder()
|
||||
->GetPhysicalDevice()
|
||||
.getProperties()
|
||||
.limits.timestampPeriod;
|
||||
if (timestamp_period_ <= 0) {
|
||||
@@ -30,7 +36,35 @@ GPUTracerVK::GPUTracerVK(const std::shared_ptr<DeviceHolder>& device_holder)
|
||||
// Disable tracing in release mode.
|
||||
#ifdef IMPELLER_DEBUG
|
||||
enabled_ = true;
|
||||
#endif
|
||||
#endif // IMPELLER_DEBUG
|
||||
}
|
||||
|
||||
void GPUTracerVK::InitializeQueryPool(const ContextVK& context) {
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
Lock lock(trace_state_mutex_);
|
||||
std::shared_ptr<CommandBuffer> buffer = context.CreateCommandBuffer();
|
||||
CommandBufferVK& buffer_vk = CommandBufferVK::Cast(*buffer);
|
||||
|
||||
for (auto i = 0u; i < kTraceStatesSize; i++) {
|
||||
vk::QueryPoolCreateInfo info;
|
||||
info.queryCount = kPoolSize;
|
||||
info.queryType = vk::QueryType::eTimestamp;
|
||||
|
||||
auto [status, pool] = context.GetDevice().createQueryPoolUnique(info);
|
||||
if (status != vk::Result::eSuccess) {
|
||||
VALIDATION_LOG << "Failed to create query pool.";
|
||||
return;
|
||||
}
|
||||
trace_states_[i].query_pool = std::move(pool);
|
||||
buffer_vk.GetEncoder()->GetCommandBuffer().resetQueryPool(
|
||||
trace_states_[i].query_pool.get(), 0, kPoolSize);
|
||||
}
|
||||
if (!buffer->SubmitCommands()) {
|
||||
VALIDATION_LOG << "Failed to reset query pool for trace events.";
|
||||
enabled_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool GPUTracerVK::IsEnabled() const {
|
||||
@@ -58,7 +92,6 @@ void GPUTracerVK::MarkFrameEnd() {
|
||||
// encoder logic. We set it to zero anyway to prevent a validation error
|
||||
// from becoming a memory leak.
|
||||
FML_DCHECK(state.pending_buffers == 0u);
|
||||
|
||||
state.pending_buffers = 0;
|
||||
state.current_index = 0;
|
||||
in_frame_ = false;
|
||||
@@ -77,21 +110,13 @@ void GPUTracerVK::RecordCmdBufferStart(const vk::CommandBuffer& buffer,
|
||||
Lock lock(trace_state_mutex_);
|
||||
auto& state = trace_states_[current_state_];
|
||||
|
||||
// Initialize the query pool for the first query on each frame.
|
||||
if (state.pending_buffers == 0) {
|
||||
vk::QueryPoolCreateInfo info;
|
||||
info.queryCount = kPoolSize;
|
||||
info.queryType = vk::QueryType::eTimestamp;
|
||||
|
||||
auto [status, pool] =
|
||||
device_holder_->GetDevice().createQueryPoolUnique(info);
|
||||
if (status != vk::Result::eSuccess) {
|
||||
VALIDATION_LOG << "Failed to create query pool.";
|
||||
return;
|
||||
// Reset previously completed queries.
|
||||
if (!states_to_reset_.empty()) {
|
||||
for (auto i = 0u; i < states_to_reset_.size(); i++) {
|
||||
buffer.resetQueryPool(trace_states_[states_to_reset_[i]].query_pool.get(),
|
||||
0, kPoolSize);
|
||||
}
|
||||
trace_states_[current_state_].query_pool = std::move(pool);
|
||||
buffer.resetQueryPool(trace_states_[current_state_].query_pool.get(), 0,
|
||||
kPoolSize);
|
||||
states_to_reset_.clear();
|
||||
}
|
||||
|
||||
// We size the query pool to kPoolSize, but Flutter applications can create an
|
||||
@@ -105,16 +130,14 @@ void GPUTracerVK::RecordCmdBufferStart(const vk::CommandBuffer& buffer,
|
||||
trace_states_[current_state_].query_pool.get(),
|
||||
state.current_index);
|
||||
state.current_index += 1;
|
||||
if (!probe.index_.has_value()) {
|
||||
state.pending_buffers += 1;
|
||||
probe.index_ = current_state_;
|
||||
}
|
||||
probe.index_ = current_state_;
|
||||
state.pending_buffers += 1;
|
||||
}
|
||||
|
||||
void GPUTracerVK::RecordCmdBufferEnd(const vk::CommandBuffer& buffer,
|
||||
GPUProbe& probe) {
|
||||
if (!enabled_ || std::this_thread::get_id() != raster_thread_id_ ||
|
||||
!in_frame_) {
|
||||
!in_frame_ || !probe.index_.has_value()) {
|
||||
return;
|
||||
}
|
||||
Lock lock(trace_state_mutex_);
|
||||
@@ -128,30 +151,37 @@ void GPUTracerVK::RecordCmdBufferEnd(const vk::CommandBuffer& buffer,
|
||||
state.query_pool.get(), state.current_index);
|
||||
|
||||
state.current_index += 1;
|
||||
if (!probe.index_.has_value()) {
|
||||
state.pending_buffers += 1;
|
||||
probe.index_ = current_state_;
|
||||
}
|
||||
}
|
||||
|
||||
void GPUTracerVK::OnFenceComplete(size_t frame_index) {
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
Lock lock(trace_state_mutex_);
|
||||
GPUTraceState& state = trace_states_[frame_index];
|
||||
|
||||
FML_DCHECK(state.pending_buffers > 0);
|
||||
state.pending_buffers -= 1;
|
||||
size_t pending = 0;
|
||||
size_t query_count = 0;
|
||||
vk::QueryPool pool;
|
||||
{
|
||||
Lock lock(trace_state_mutex_);
|
||||
GPUTraceState& state = trace_states_[frame_index];
|
||||
|
||||
if (state.pending_buffers == 0) {
|
||||
auto buffer_count = state.current_index;
|
||||
std::vector<uint64_t> bits(buffer_count);
|
||||
FML_DCHECK(state.pending_buffers > 0);
|
||||
state.pending_buffers -= 1;
|
||||
pending = state.pending_buffers;
|
||||
query_count = state.current_index;
|
||||
pool = state.query_pool.get();
|
||||
}
|
||||
|
||||
auto result = device_holder_->GetDevice().getQueryPoolResults(
|
||||
state.query_pool.get(), 0, state.current_index,
|
||||
buffer_count * sizeof(uint64_t), bits.data(), sizeof(uint64_t),
|
||||
vk::QueryResultFlagBits::e64);
|
||||
if (pending == 0) {
|
||||
std::vector<uint64_t> bits(query_count);
|
||||
std::shared_ptr<ContextVK> context = context_.lock();
|
||||
if (!context) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto result = context->GetDevice().getQueryPoolResults(
|
||||
pool, 0, query_count, query_count * sizeof(uint64_t), bits.data(),
|
||||
sizeof(uint64_t), vk::QueryResultFlagBits::e64);
|
||||
// This may return VK_NOT_READY if the query couldn't be completed, or if
|
||||
// there are queries still pending. From local testing, this happens
|
||||
// occassionally on very expensive frames. Its unclear if we can do anything
|
||||
@@ -160,22 +190,24 @@ void GPUTracerVK::OnFenceComplete(size_t frame_index) {
|
||||
// passed to getQueryPoolResults seems like it would fix this, but actually
|
||||
// seems to result in more stuck query errors. Better to just drop them and
|
||||
// move on.
|
||||
if (result != vk::Result::eSuccess) {
|
||||
return;
|
||||
if (result == vk::Result::eSuccess) {
|
||||
uint64_t smallest_timestamp = std::numeric_limits<uint64_t>::max();
|
||||
uint64_t largest_timestamp = 0;
|
||||
for (auto i = 0u; i < bits.size(); i++) {
|
||||
smallest_timestamp = std::min(smallest_timestamp, bits[i]);
|
||||
largest_timestamp = std::max(largest_timestamp, bits[i]);
|
||||
}
|
||||
auto gpu_ms =
|
||||
(((largest_timestamp - smallest_timestamp) * timestamp_period_) /
|
||||
1000000);
|
||||
FML_TRACE_COUNTER("flutter", "GPUTracer",
|
||||
reinterpret_cast<int64_t>(this), // Trace Counter ID
|
||||
"FrameTimeMS", gpu_ms);
|
||||
}
|
||||
|
||||
uint64_t smallest_timestamp = std::numeric_limits<uint64_t>::max();
|
||||
uint64_t largest_timestamp = 0;
|
||||
for (auto i = 0u; i < bits.size(); i++) {
|
||||
smallest_timestamp = std::min(smallest_timestamp, bits[i]);
|
||||
largest_timestamp = std::max(largest_timestamp, bits[i]);
|
||||
}
|
||||
auto gpu_ms =
|
||||
(((largest_timestamp - smallest_timestamp) * timestamp_period_) /
|
||||
1000000);
|
||||
FML_TRACE_COUNTER("flutter", "GPUTracer",
|
||||
reinterpret_cast<int64_t>(this), // Trace Counter ID
|
||||
"FrameTimeMS", gpu_ms);
|
||||
// Record this query to be reset the next time a command is recorded.
|
||||
Lock lock(trace_state_mutex_);
|
||||
states_to_reset_.push_back(frame_index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class GPUProbe;
|
||||
/// execution time.
|
||||
class GPUTracerVK : public std::enable_shared_from_this<GPUTracerVK> {
|
||||
public:
|
||||
explicit GPUTracerVK(const std::shared_ptr<DeviceHolder>& device_holder);
|
||||
explicit GPUTracerVK(std::weak_ptr<ContextVK> context);
|
||||
|
||||
~GPUTracerVK() = default;
|
||||
|
||||
@@ -37,10 +37,13 @@ class GPUTracerVK : public std::enable_shared_from_this<GPUTracerVK> {
|
||||
// visible for testing.
|
||||
bool IsEnabled() const;
|
||||
|
||||
/// Initialize the set of query pools.
|
||||
void InitializeQueryPool(const ContextVK& context);
|
||||
|
||||
private:
|
||||
friend class GPUProbe;
|
||||
|
||||
static const constexpr size_t kTraceStatesSize = 32u;
|
||||
static const constexpr size_t kTraceStatesSize = 16u;
|
||||
|
||||
/// @brief Signal that the cmd buffer is completed.
|
||||
///
|
||||
@@ -55,7 +58,7 @@ class GPUTracerVK : public std::enable_shared_from_this<GPUTracerVK> {
|
||||
/// time.
|
||||
void RecordCmdBufferEnd(const vk::CommandBuffer& buffer, GPUProbe& probe);
|
||||
|
||||
const std::shared_ptr<DeviceHolder> device_holder_;
|
||||
std::weak_ptr<ContextVK> context_;
|
||||
|
||||
struct GPUTraceState {
|
||||
size_t current_index = 0;
|
||||
@@ -67,6 +70,7 @@ class GPUTracerVK : public std::enable_shared_from_this<GPUTracerVK> {
|
||||
GPUTraceState trace_states_[kTraceStatesSize] IPLR_GUARDED_BY(
|
||||
trace_state_mutex_);
|
||||
size_t current_state_ IPLR_GUARDED_BY(trace_state_mutex_) = 0u;
|
||||
std::vector<size_t> IPLR_GUARDED_BY(trace_state_mutex_) states_to_reset_ = {};
|
||||
|
||||
// The number of nanoseconds for each timestamp unit.
|
||||
float timestamp_period_ = 1;
|
||||
|
||||
@@ -8,10 +8,11 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "fml/status.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/core/device_buffer.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/texture.h"
|
||||
#include "impeller/renderer/backend/vulkan/barrier_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/binding_helpers_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
|
||||
@@ -20,15 +21,65 @@
|
||||
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/formats_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/pipeline_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/sampler_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/texture_vk.h"
|
||||
#include "impeller/renderer/command.h"
|
||||
|
||||
#include "vulkan/vulkan_enums.hpp"
|
||||
#include "vulkan/vulkan_handles.hpp"
|
||||
#include "vulkan/vulkan_to_string.hpp"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
// Warning: if any of the constant values or layouts are changed in the
|
||||
// framebuffer fetch shader, then this input binding may need to be
|
||||
// manually changed.
|
||||
//
|
||||
// See: impeller/entity/shaders/blending/framebuffer_blend.frag
|
||||
static constexpr size_t kMagicSubpassInputBinding = 64;
|
||||
|
||||
static vk::ClearColorValue VKClearValueFromColor(Color color) {
|
||||
vk::ClearColorValue value;
|
||||
value.setFloat32(
|
||||
std::array<float, 4>{color.red, color.green, color.blue, color.alpha});
|
||||
return value;
|
||||
}
|
||||
|
||||
static vk::ClearDepthStencilValue VKClearValueFromDepthStencil(uint32_t stencil,
|
||||
Scalar depth) {
|
||||
vk::ClearDepthStencilValue value;
|
||||
value.depth = depth;
|
||||
value.stencil = stencil;
|
||||
return value;
|
||||
}
|
||||
|
||||
static std::vector<vk::ClearValue> GetVKClearValues(
|
||||
const RenderTarget& target) {
|
||||
std::vector<vk::ClearValue> clears;
|
||||
|
||||
for (const auto& [_, color] : target.GetColorAttachments()) {
|
||||
clears.emplace_back(VKClearValueFromColor(color.clear_color));
|
||||
if (color.resolve_texture) {
|
||||
clears.emplace_back(VKClearValueFromColor(color.clear_color));
|
||||
}
|
||||
}
|
||||
|
||||
const auto& depth = target.GetDepthAttachment();
|
||||
const auto& stencil = target.GetStencilAttachment();
|
||||
|
||||
if (depth.has_value()) {
|
||||
clears.emplace_back(VKClearValueFromDepthStencil(
|
||||
stencil ? stencil->clear_stencil : 0u, depth->clear_depth));
|
||||
}
|
||||
|
||||
if (stencil.has_value()) {
|
||||
clears.emplace_back(VKClearValueFromDepthStencil(
|
||||
stencil->clear_stencil, depth ? depth->clear_depth : 0.0f));
|
||||
}
|
||||
|
||||
return clears;
|
||||
}
|
||||
|
||||
static vk::AttachmentDescription CreateAttachmentDescription(
|
||||
const Attachment& attachment,
|
||||
const std::shared_ptr<Texture> Attachment::*texture_ptr,
|
||||
@@ -193,14 +244,81 @@ SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
|
||||
VALIDATION_LOG << "Failed to create render pass: " << vk::to_string(result);
|
||||
return {};
|
||||
}
|
||||
context.SetDebugName(pass.get(), debug_label_.c_str());
|
||||
return MakeSharedVK(std::move(pass));
|
||||
}
|
||||
|
||||
RenderPassVK::RenderPassVK(const std::shared_ptr<const Context>& context,
|
||||
const RenderTarget& target,
|
||||
std::weak_ptr<CommandBufferVK> command_buffer)
|
||||
std::shared_ptr<CommandBufferVK> command_buffer)
|
||||
: RenderPass(context, target), command_buffer_(std::move(command_buffer)) {
|
||||
const auto& vk_context = ContextVK::Cast(*context);
|
||||
const std::shared_ptr<CommandEncoderVK>& encoder =
|
||||
command_buffer_->GetEncoder();
|
||||
command_buffer_vk_ = encoder->GetCommandBuffer();
|
||||
render_target_.IterateAllAttachments(
|
||||
[&encoder](const auto& attachment) -> bool {
|
||||
encoder->Track(attachment.texture);
|
||||
encoder->Track(attachment.resolve_texture);
|
||||
return true;
|
||||
});
|
||||
|
||||
const auto& target_size = render_target_.GetRenderTargetSize();
|
||||
|
||||
render_pass_ = CreateVKRenderPass(
|
||||
vk_context, command_buffer_,
|
||||
vk_context.GetCapabilities()->SupportsFramebufferFetch());
|
||||
if (!render_pass_) {
|
||||
VALIDATION_LOG << "Could not create renderpass.";
|
||||
is_valid_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
auto framebuffer = CreateVKFramebuffer(vk_context, *render_pass_);
|
||||
if (!framebuffer) {
|
||||
VALIDATION_LOG << "Could not create framebuffer.";
|
||||
is_valid_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!encoder->Track(framebuffer) || !encoder->Track(render_pass_)) {
|
||||
is_valid_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
auto clear_values = GetVKClearValues(render_target_);
|
||||
|
||||
vk::RenderPassBeginInfo pass_info;
|
||||
pass_info.renderPass = *render_pass_;
|
||||
pass_info.framebuffer = *framebuffer;
|
||||
pass_info.renderArea.extent.width = static_cast<uint32_t>(target_size.width);
|
||||
pass_info.renderArea.extent.height =
|
||||
static_cast<uint32_t>(target_size.height);
|
||||
pass_info.setClearValues(clear_values);
|
||||
|
||||
command_buffer_vk_.beginRenderPass(pass_info, vk::SubpassContents::eInline);
|
||||
|
||||
// Set the initial viewport and scissors.
|
||||
const auto vp = Viewport{.rect = Rect::MakeSize(target_size)};
|
||||
vk::Viewport viewport = vk::Viewport()
|
||||
.setWidth(vp.rect.GetWidth())
|
||||
.setHeight(-vp.rect.GetHeight())
|
||||
.setY(vp.rect.GetHeight())
|
||||
.setMinDepth(0.0f)
|
||||
.setMaxDepth(1.0f);
|
||||
command_buffer_vk_.setViewport(0, 1, &viewport);
|
||||
|
||||
// Set the initial scissor rect.
|
||||
const auto sc = IRect::MakeSize(target_size);
|
||||
vk::Rect2D scissor =
|
||||
vk::Rect2D()
|
||||
.setOffset(vk::Offset2D(sc.GetX(), sc.GetY()))
|
||||
.setExtent(vk::Extent2D(sc.GetWidth(), sc.GetHeight()));
|
||||
command_buffer_vk_.setScissor(0, 1, &scissor);
|
||||
|
||||
color_image_vk_ =
|
||||
render_target_.GetColorAttachments().find(0u)->second.texture;
|
||||
resolve_image_vk_ =
|
||||
render_target_.GetColorAttachments().find(0u)->second.resolve_texture;
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -211,49 +329,10 @@ bool RenderPassVK::IsValid() const {
|
||||
}
|
||||
|
||||
void RenderPassVK::OnSetLabel(std::string label) {
|
||||
debug_label_ = std::move(label);
|
||||
}
|
||||
|
||||
static vk::ClearColorValue VKClearValueFromColor(Color color) {
|
||||
vk::ClearColorValue value;
|
||||
value.setFloat32(
|
||||
std::array<float, 4>{color.red, color.green, color.blue, color.alpha});
|
||||
return value;
|
||||
}
|
||||
|
||||
static vk::ClearDepthStencilValue VKClearValueFromDepthStencil(uint32_t stencil,
|
||||
Scalar depth) {
|
||||
vk::ClearDepthStencilValue value;
|
||||
value.depth = depth;
|
||||
value.stencil = stencil;
|
||||
return value;
|
||||
}
|
||||
|
||||
static std::vector<vk::ClearValue> GetVKClearValues(
|
||||
const RenderTarget& target) {
|
||||
std::vector<vk::ClearValue> clears;
|
||||
|
||||
for (const auto& [_, color] : target.GetColorAttachments()) {
|
||||
clears.emplace_back(VKClearValueFromColor(color.clear_color));
|
||||
if (color.resolve_texture) {
|
||||
clears.emplace_back(VKClearValueFromColor(color.clear_color));
|
||||
}
|
||||
}
|
||||
|
||||
const auto& depth = target.GetDepthAttachment();
|
||||
const auto& stencil = target.GetStencilAttachment();
|
||||
|
||||
if (depth.has_value()) {
|
||||
clears.emplace_back(VKClearValueFromDepthStencil(
|
||||
stencil ? stencil->clear_stencil : 0u, depth->clear_depth));
|
||||
}
|
||||
|
||||
if (stencil.has_value()) {
|
||||
clears.emplace_back(VKClearValueFromDepthStencil(
|
||||
stencil->clear_stencil, depth ? depth->clear_depth : 0.0f));
|
||||
}
|
||||
|
||||
return clears;
|
||||
#ifdef IMPELLER_DEBUG
|
||||
ContextVK::Cast(*context_).SetDebugName(render_pass_->Get(),
|
||||
std::string(label).c_str());
|
||||
#endif // IMPELLER_DEBUG
|
||||
}
|
||||
|
||||
SharedHandleVK<vk::Framebuffer> RenderPassVK::CreateVKFramebuffer(
|
||||
@@ -302,134 +381,114 @@ SharedHandleVK<vk::Framebuffer> RenderPassVK::CreateVKFramebuffer(
|
||||
return MakeSharedVK(std::move(framebuffer));
|
||||
}
|
||||
|
||||
static bool UpdateBindingLayouts(const Bindings& bindings,
|
||||
const vk::CommandBuffer& buffer) {
|
||||
// All previous writes via a render or blit pass must be done before another
|
||||
// shader attempts to read the resource.
|
||||
BarrierVK barrier;
|
||||
barrier.cmd_buffer = buffer;
|
||||
barrier.src_access = vk::AccessFlagBits::eColorAttachmentWrite |
|
||||
vk::AccessFlagBits::eTransferWrite;
|
||||
barrier.src_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput |
|
||||
vk::PipelineStageFlagBits::eTransfer;
|
||||
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
|
||||
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetPipeline(
|
||||
const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline) {
|
||||
PipelineVK& pipeline_vk = PipelineVK::Cast(*pipeline);
|
||||
|
||||
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
||||
|
||||
for (const TextureAndSampler& data : bindings.sampled_images) {
|
||||
if (!TextureVK::Cast(*data.texture.resource).SetLayout(barrier)) {
|
||||
return false;
|
||||
}
|
||||
auto descriptor_result =
|
||||
command_buffer_->GetEncoder()->AllocateDescriptorSets(
|
||||
pipeline_vk.GetDescriptorSetLayout(), ContextVK::Cast(*context_));
|
||||
if (!descriptor_result.ok()) {
|
||||
return;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
pipeline_valid_ = true;
|
||||
descriptor_set_ = descriptor_result.value();
|
||||
pipeline_layout_ = pipeline_vk.GetPipelineLayout();
|
||||
command_buffer_vk_.bindPipeline(vk::PipelineBindPoint::eGraphics,
|
||||
pipeline_vk.GetPipeline());
|
||||
|
||||
static bool UpdateBindingLayouts(const Command& command,
|
||||
const vk::CommandBuffer& buffer) {
|
||||
return UpdateBindingLayouts(command.vertex_bindings, buffer) &&
|
||||
UpdateBindingLayouts(command.fragment_bindings, buffer);
|
||||
}
|
||||
|
||||
static bool UpdateBindingLayouts(const std::vector<Command>& commands,
|
||||
const vk::CommandBuffer& buffer) {
|
||||
for (const Command& command : commands) {
|
||||
if (!UpdateBindingLayouts(command, buffer)) {
|
||||
return false;
|
||||
if (pipeline->GetDescriptor().UsesSubpassInput()) {
|
||||
if (bound_image_offset_ >= kMaxBindings) {
|
||||
pipeline_valid_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
vk::DescriptorImageInfo image_info;
|
||||
image_info.imageLayout = vk::ImageLayout::eGeneral;
|
||||
image_info.sampler = VK_NULL_HANDLE;
|
||||
image_info.imageView = TextureVK::Cast(*color_image_vk_).GetImageView();
|
||||
image_workspace_[bound_image_offset_++] = image_info;
|
||||
|
||||
vk::WriteDescriptorSet write_set;
|
||||
write_set.dstBinding = kMagicSubpassInputBinding;
|
||||
write_set.descriptorCount = 1u;
|
||||
write_set.descriptorType = vk::DescriptorType::eInputAttachment;
|
||||
write_set.pImageInfo = &image_workspace_[bound_image_offset_ - 1];
|
||||
|
||||
write_workspace_[descriptor_write_offset_++] = write_set;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void SetViewportAndScissor(const Command& command,
|
||||
const vk::CommandBuffer& cmd_buffer,
|
||||
PassBindingsCache& cmd_buffer_cache,
|
||||
const ISize& target_size) {
|
||||
// Set the viewport.
|
||||
const auto& vp = command.viewport.value_or<Viewport>(
|
||||
{.rect = Rect::MakeSize(target_size)});
|
||||
vk::Viewport viewport = vk::Viewport()
|
||||
.setWidth(vp.rect.GetWidth())
|
||||
.setHeight(-vp.rect.GetHeight())
|
||||
.setY(vp.rect.GetHeight())
|
||||
.setMinDepth(0.0f)
|
||||
.setMaxDepth(1.0f);
|
||||
cmd_buffer_cache.SetViewport(cmd_buffer, 0, 1, &viewport);
|
||||
|
||||
// Set the scissor rect.
|
||||
const auto& sc = command.scissor.value_or(IRect::MakeSize(target_size));
|
||||
vk::Rect2D scissor =
|
||||
vk::Rect2D()
|
||||
.setOffset(vk::Offset2D(sc.GetX(), sc.GetY()))
|
||||
.setExtent(vk::Extent2D(sc.GetWidth(), sc.GetHeight()));
|
||||
cmd_buffer_cache.SetScissor(cmd_buffer, 0, 1, &scissor);
|
||||
}
|
||||
|
||||
static bool EncodeCommand(const Context& context,
|
||||
const Command& command,
|
||||
CommandEncoderVK& encoder,
|
||||
PassBindingsCache& command_buffer_cache,
|
||||
const ISize& target_size,
|
||||
const vk::DescriptorSet vk_desc_set,
|
||||
const vk::CommandBuffer& cmd_buffer) {
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetCommandLabel(std::string_view label) {
|
||||
#ifdef IMPELLER_DEBUG
|
||||
fml::ScopedCleanupClosure pop_marker(
|
||||
[&encoder]() { encoder.PopDebugGroup(); });
|
||||
if (!command.label.empty()) {
|
||||
encoder.PushDebugGroup(command.label.c_str());
|
||||
} else {
|
||||
pop_marker.Release();
|
||||
}
|
||||
std::string label_copy(label);
|
||||
command_buffer_->GetEncoder()->PushDebugGroup(label_copy.c_str());
|
||||
has_label_ = true;
|
||||
#endif // IMPELLER_DEBUG
|
||||
}
|
||||
|
||||
const PipelineVK& pipeline_vk = PipelineVK::Cast(*command.pipeline);
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetStencilReference(uint32_t value) {
|
||||
command_buffer_vk_.setStencilReference(
|
||||
vk::StencilFaceFlagBits::eVkStencilFrontAndBack, value);
|
||||
}
|
||||
|
||||
cmd_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, // bind point
|
||||
pipeline_vk.GetPipelineLayout(), // layout
|
||||
0, // first set
|
||||
1, // set count
|
||||
&vk_desc_set, // sets
|
||||
0, // offset count
|
||||
nullptr // offsets
|
||||
);
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetBaseVertex(uint64_t value) {
|
||||
base_vertex_ = value;
|
||||
}
|
||||
|
||||
command_buffer_cache.BindPipeline(
|
||||
cmd_buffer, vk::PipelineBindPoint::eGraphics, pipeline_vk.GetPipeline());
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetViewport(Viewport viewport) {
|
||||
vk::Viewport viewport_vk = vk::Viewport()
|
||||
.setWidth(viewport.rect.GetWidth())
|
||||
.setHeight(-viewport.rect.GetHeight())
|
||||
.setY(viewport.rect.GetHeight())
|
||||
.setMinDepth(0.0f)
|
||||
.setMaxDepth(1.0f);
|
||||
command_buffer_vk_.setViewport(0, 1, &viewport_vk);
|
||||
}
|
||||
|
||||
// Set the viewport and scissors.
|
||||
SetViewportAndScissor(command, cmd_buffer, command_buffer_cache, target_size);
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetScissor(IRect scissor) {
|
||||
vk::Rect2D scissor_vk =
|
||||
vk::Rect2D()
|
||||
.setOffset(vk::Offset2D(scissor.GetX(), scissor.GetY()))
|
||||
.setExtent(vk::Extent2D(scissor.GetWidth(), scissor.GetHeight()));
|
||||
command_buffer_vk_.setScissor(0, 1, &scissor_vk);
|
||||
}
|
||||
|
||||
// Set the stencil reference.
|
||||
command_buffer_cache.SetStencilReference(
|
||||
cmd_buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack,
|
||||
command.stencil_reference);
|
||||
// |RenderPass|
|
||||
void RenderPassVK::SetInstanceCount(size_t count) {
|
||||
instance_count_ = count;
|
||||
}
|
||||
|
||||
// Configure vertex and index and buffers for binding.
|
||||
if (!command.vertex_buffer.vertex_buffer) {
|
||||
VALIDATION_LOG << "Failed to acquire device buffer"
|
||||
<< " for vertex buffer view";
|
||||
// |RenderPass|
|
||||
bool RenderPassVK::SetVertexBuffer(VertexBuffer buffer) {
|
||||
vertex_count_ = buffer.vertex_count;
|
||||
if (buffer.index_type == IndexType::kUnknown) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& allocator = *context.GetResourceAllocator();
|
||||
const std::shared_ptr<const DeviceBuffer>& vertex_buffer =
|
||||
command.vertex_buffer.vertex_buffer.buffer;
|
||||
|
||||
if (!encoder.Track(vertex_buffer)) {
|
||||
if (!command_buffer_->GetEncoder()->Track(buffer.vertex_buffer.buffer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Bind the vertex buffer.
|
||||
vk::Buffer vertex_buffer_handle =
|
||||
DeviceBufferVK::Cast(*vertex_buffer).GetBuffer();
|
||||
DeviceBufferVK::Cast(*buffer.vertex_buffer.buffer).GetBuffer();
|
||||
vk::Buffer vertex_buffers[] = {vertex_buffer_handle};
|
||||
vk::DeviceSize vertex_buffer_offsets[] = {
|
||||
command.vertex_buffer.vertex_buffer.range.offset};
|
||||
cmd_buffer.bindVertexBuffers(0u, 1u, vertex_buffers, vertex_buffer_offsets);
|
||||
vk::DeviceSize vertex_buffer_offsets[] = {buffer.vertex_buffer.range.offset};
|
||||
|
||||
if (command.vertex_buffer.index_type != IndexType::kNone) {
|
||||
// Bind the index buffer.
|
||||
const BufferView& index_buffer_view = command.vertex_buffer.index_buffer;
|
||||
command_buffer_vk_.bindVertexBuffers(0u, 1u, vertex_buffers,
|
||||
vertex_buffer_offsets);
|
||||
|
||||
// Bind the index buffer.
|
||||
if (buffer.index_type != IndexType::kNone) {
|
||||
has_index_buffer_ = true;
|
||||
const BufferView& index_buffer_view = buffer.index_buffer;
|
||||
if (!index_buffer_view) {
|
||||
return false;
|
||||
}
|
||||
@@ -442,128 +501,187 @@ static bool EncodeCommand(const Context& context,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!encoder.Track(index_buffer)) {
|
||||
if (!command_buffer_->GetEncoder()->Track(index_buffer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
vk::Buffer index_buffer_handle =
|
||||
DeviceBufferVK::Cast(*index_buffer).GetBuffer();
|
||||
cmd_buffer.bindIndexBuffer(index_buffer_handle,
|
||||
index_buffer_view.range.offset,
|
||||
ToVKIndexType(command.vertex_buffer.index_type));
|
||||
|
||||
// Engage!
|
||||
cmd_buffer.drawIndexed(command.vertex_buffer.vertex_count, // index count
|
||||
command.instance_count, // instance count
|
||||
0u, // first index
|
||||
command.base_vertex, // vertex offset
|
||||
0u // first instance
|
||||
);
|
||||
command_buffer_vk_.bindIndexBuffer(index_buffer_handle,
|
||||
index_buffer_view.range.offset,
|
||||
ToVKIndexType(buffer.index_type));
|
||||
} else {
|
||||
cmd_buffer.draw(command.vertex_buffer.vertex_count, // vertex count
|
||||
command.instance_count, // instance count
|
||||
command.base_vertex, // vertex offset
|
||||
0u // first instance
|
||||
);
|
||||
has_index_buffer_ = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderPassVK::OnEncodeCommands(const Context& context) const {
|
||||
TRACE_EVENT0("impeller", "RenderPassVK::OnEncodeCommands");
|
||||
if (!IsValid()) {
|
||||
return false;
|
||||
// |RenderPass|
|
||||
fml::Status RenderPassVK::Draw() {
|
||||
if (!pipeline_valid_) {
|
||||
return fml::Status(fml::StatusCode::kCancelled,
|
||||
"No valid pipeline is bound to the RenderPass.");
|
||||
}
|
||||
|
||||
const auto& vk_context = ContextVK::Cast(context);
|
||||
|
||||
std::shared_ptr<CommandBufferVK> command_buffer = command_buffer_.lock();
|
||||
if (!command_buffer) {
|
||||
VALIDATION_LOG << "Command buffer died before commands could be encoded.";
|
||||
return false;
|
||||
const ContextVK& context_vk = ContextVK::Cast(*context_);
|
||||
for (auto i = 0u; i < descriptor_write_offset_; i++) {
|
||||
write_workspace_[i].dstSet = descriptor_set_;
|
||||
}
|
||||
const std::shared_ptr<CommandEncoderVK>& encoder =
|
||||
command_buffer->GetEncoder();
|
||||
if (!encoder) {
|
||||
return false;
|
||||
|
||||
context_vk.GetDevice().updateDescriptorSets(descriptor_write_offset_,
|
||||
write_workspace_.data(), 0u, {});
|
||||
|
||||
command_buffer_vk_.bindDescriptorSets(
|
||||
vk::PipelineBindPoint::eGraphics, // bind point
|
||||
pipeline_layout_, // layout
|
||||
0, // first set
|
||||
1, // set count
|
||||
&descriptor_set_, // sets
|
||||
0, // offset count
|
||||
nullptr // offsets
|
||||
);
|
||||
|
||||
if (has_index_buffer_) {
|
||||
command_buffer_vk_.drawIndexed(vertex_count_, // index count
|
||||
instance_count_, // instance count
|
||||
0u, // first index
|
||||
base_vertex_, // vertex offset
|
||||
0u // first instance
|
||||
);
|
||||
} else {
|
||||
command_buffer_vk_.draw(vertex_count_, // vertex count
|
||||
instance_count_, // instance count
|
||||
base_vertex_, // vertex offset
|
||||
0u // first instance
|
||||
);
|
||||
}
|
||||
|
||||
#ifdef IMPELLER_DEBUG
|
||||
fml::ScopedCleanupClosure pop_marker(
|
||||
[&encoder]() { encoder->PopDebugGroup(); });
|
||||
if (!debug_label_.empty()) {
|
||||
encoder->PushDebugGroup(debug_label_.c_str());
|
||||
} else {
|
||||
pop_marker.Release();
|
||||
if (has_label_) {
|
||||
command_buffer_->GetEncoder()->PopDebugGroup();
|
||||
}
|
||||
#endif // IMPELLER_DEBUG
|
||||
has_label_ = false;
|
||||
has_index_buffer_ = false;
|
||||
bound_image_offset_ = 0u;
|
||||
bound_buffer_offset_ = 0u;
|
||||
descriptor_write_offset_ = 0u;
|
||||
instance_count_ = 1u;
|
||||
base_vertex_ = 0u;
|
||||
vertex_count_ = 0u;
|
||||
pipeline_valid_ = false;
|
||||
return fml::Status();
|
||||
}
|
||||
|
||||
vk::CommandBuffer cmd_buffer = encoder->GetCommandBuffer();
|
||||
// The RenderPassVK binding methods only need the binding, set, and buffer type
|
||||
// information.
|
||||
bool RenderPassVK::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) {
|
||||
return BindResource(slot.binding, type, view);
|
||||
}
|
||||
|
||||
if (!UpdateBindingLayouts(commands_, cmd_buffer)) {
|
||||
bool RenderPassVK::BindResource(
|
||||
ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view) {
|
||||
return BindResource(slot.binding, type, view);
|
||||
}
|
||||
|
||||
bool RenderPassVK::BindResource(size_t binding,
|
||||
DescriptorType type,
|
||||
const BufferView& view) {
|
||||
if (bound_buffer_offset_ >= kMaxBindings) {
|
||||
return false;
|
||||
}
|
||||
|
||||
render_target_.IterateAllAttachments(
|
||||
[&encoder](const auto& attachment) -> bool {
|
||||
encoder->Track(attachment.texture);
|
||||
encoder->Track(attachment.resolve_texture);
|
||||
return true;
|
||||
});
|
||||
|
||||
const auto& target_size = render_target_.GetRenderTargetSize();
|
||||
|
||||
SharedHandleVK<vk::RenderPass> render_pass = CreateVKRenderPass(
|
||||
vk_context, command_buffer,
|
||||
vk_context.GetCapabilities()->SupportsFramebufferFetch());
|
||||
if (!render_pass) {
|
||||
VALIDATION_LOG << "Could not create renderpass.";
|
||||
const std::shared_ptr<const DeviceBuffer>& device_buffer = view.buffer;
|
||||
auto buffer = DeviceBufferVK::Cast(*device_buffer).GetBuffer();
|
||||
if (!buffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto framebuffer = CreateVKFramebuffer(vk_context, *render_pass);
|
||||
if (!framebuffer) {
|
||||
VALIDATION_LOG << "Could not create framebuffer.";
|
||||
if (!command_buffer_->GetEncoder()->Track(device_buffer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!encoder->Track(framebuffer) || !encoder->Track(render_pass)) {
|
||||
uint32_t offset = view.range.offset;
|
||||
|
||||
vk::DescriptorBufferInfo buffer_info;
|
||||
buffer_info.buffer = buffer;
|
||||
buffer_info.offset = offset;
|
||||
buffer_info.range = view.range.length;
|
||||
buffer_workspace_[bound_buffer_offset_++] = buffer_info;
|
||||
|
||||
vk::WriteDescriptorSet write_set;
|
||||
write_set.dstBinding = binding;
|
||||
write_set.descriptorCount = 1u;
|
||||
write_set.descriptorType = ToVKDescriptorType(type);
|
||||
write_set.pBufferInfo = &buffer_workspace_[bound_buffer_offset_ - 1];
|
||||
|
||||
write_workspace_[descriptor_write_offset_++] = write_set;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderPassVK::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) {
|
||||
if (bound_buffer_offset_ >= kMaxBindings) {
|
||||
return false;
|
||||
}
|
||||
const TextureVK& texture_vk = TextureVK::Cast(*texture);
|
||||
const SamplerVK& sampler_vk = SamplerVK::Cast(*sampler);
|
||||
|
||||
if (!command_buffer_->GetEncoder()->Track(texture) ||
|
||||
!command_buffer_->GetEncoder()->Track(sampler_vk.GetSharedSampler())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto clear_values = GetVKClearValues(render_target_);
|
||||
vk::DescriptorImageInfo image_info;
|
||||
image_info.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
||||
image_info.sampler = sampler_vk.GetSampler();
|
||||
image_info.imageView = texture_vk.GetImageView();
|
||||
image_workspace_[bound_image_offset_++] = image_info;
|
||||
|
||||
vk::RenderPassBeginInfo pass_info;
|
||||
pass_info.renderPass = *render_pass;
|
||||
pass_info.framebuffer = *framebuffer;
|
||||
pass_info.renderArea.extent.width = static_cast<uint32_t>(target_size.width);
|
||||
pass_info.renderArea.extent.height =
|
||||
static_cast<uint32_t>(target_size.height);
|
||||
pass_info.setClearValues(clear_values);
|
||||
vk::WriteDescriptorSet write_set;
|
||||
write_set.dstBinding = slot.binding;
|
||||
write_set.descriptorCount = 1u;
|
||||
write_set.descriptorType = vk::DescriptorType::eCombinedImageSampler;
|
||||
write_set.pImageInfo = &image_workspace_[bound_image_offset_ - 1];
|
||||
|
||||
const TextureVK& color_image_vk = TextureVK::Cast(
|
||||
*render_target_.GetColorAttachments().find(0u)->second.texture);
|
||||
Allocator& allocator = *context.GetResourceAllocator();
|
||||
write_workspace_[descriptor_write_offset_++] = write_set;
|
||||
return true;
|
||||
}
|
||||
|
||||
{
|
||||
TRACE_EVENT0("impeller", "EncodeRenderPassCommands");
|
||||
cmd_buffer.beginRenderPass(pass_info, vk::SubpassContents::eInline);
|
||||
bool RenderPassVK::OnEncodeCommands(const Context& context) const {
|
||||
command_buffer_->GetEncoder()->GetCommandBuffer().endRenderPass();
|
||||
|
||||
fml::ScopedCleanupClosure end_render_pass(
|
||||
[cmd_buffer]() { cmd_buffer.endRenderPass(); });
|
||||
// If this render target will be consumed by a subsequent render pass,
|
||||
// perform a layout transition to a shader read state.
|
||||
const std::shared_ptr<Texture>& result_texture =
|
||||
resolve_image_vk_ ? resolve_image_vk_ : color_image_vk_;
|
||||
if (result_texture->GetTextureDescriptor().usage &
|
||||
static_cast<TextureUsageMask>(TextureUsage::kShaderRead)) {
|
||||
BarrierVK barrier;
|
||||
barrier.cmd_buffer = command_buffer_vk_;
|
||||
barrier.src_access = vk::AccessFlagBits::eColorAttachmentWrite |
|
||||
vk::AccessFlagBits::eTransferWrite;
|
||||
barrier.src_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput |
|
||||
vk::PipelineStageFlagBits::eTransfer;
|
||||
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
|
||||
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
|
||||
|
||||
for (const auto& command : commands_) {
|
||||
fml::StatusOr<vk::DescriptorSet> desc_set_result =
|
||||
AllocateAndBindDescriptorSets(vk_context, encoder, allocator, command,
|
||||
color_image_vk, image_workspace_,
|
||||
buffer_workspace_, write_workspace_);
|
||||
if (!desc_set_result.ok()) {
|
||||
return false;
|
||||
}
|
||||
if (!EncodeCommand(context, command, *encoder, pass_bindings_cache_,
|
||||
target_size, desc_set_result.value(), cmd_buffer)) {
|
||||
return false;
|
||||
}
|
||||
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
||||
|
||||
if (!TextureVK::Cast(*result_texture).SetLayout(barrier)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_RENDER_PASS_VK_H_
|
||||
#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_RENDER_PASS_VK_H_
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/core/buffer_view.h"
|
||||
#include "impeller/renderer/backend/vulkan/binding_helpers_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/pass_bindings_cache.h"
|
||||
#include "impeller/renderer/backend/vulkan/pipeline_vk.h"
|
||||
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
#include "impeller/renderer/render_target.h"
|
||||
@@ -25,20 +25,93 @@ class RenderPassVK final : public RenderPass {
|
||||
private:
|
||||
friend class CommandBufferVK;
|
||||
|
||||
std::weak_ptr<CommandBufferVK> command_buffer_;
|
||||
std::shared_ptr<CommandBufferVK> command_buffer_;
|
||||
std::string debug_label_;
|
||||
SharedHandleVK<vk::RenderPass> render_pass_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
mutable std::array<vk::DescriptorImageInfo, kMaxBindings> image_workspace_;
|
||||
mutable std::array<vk::DescriptorBufferInfo, kMaxBindings> buffer_workspace_;
|
||||
mutable std::array<vk::WriteDescriptorSet, kMaxBindings + kMaxBindings>
|
||||
write_workspace_;
|
||||
vk::CommandBuffer command_buffer_vk_;
|
||||
std::shared_ptr<Texture> color_image_vk_;
|
||||
std::shared_ptr<Texture> resolve_image_vk_;
|
||||
|
||||
mutable PassBindingsCache pass_bindings_cache_;
|
||||
// Per-command state.
|
||||
std::array<vk::DescriptorImageInfo, kMaxBindings> image_workspace_;
|
||||
std::array<vk::DescriptorBufferInfo, kMaxBindings> buffer_workspace_;
|
||||
std::array<vk::WriteDescriptorSet, kMaxBindings + kMaxBindings>
|
||||
write_workspace_;
|
||||
size_t bound_image_offset_ = 0u;
|
||||
size_t bound_buffer_offset_ = 0u;
|
||||
size_t descriptor_write_offset_ = 0u;
|
||||
size_t instance_count_ = 1u;
|
||||
size_t base_vertex_ = 0u;
|
||||
size_t vertex_count_ = 0u;
|
||||
bool has_index_buffer_ = false;
|
||||
bool has_label_ = false;
|
||||
bool pipeline_valid_ = false;
|
||||
vk::Pipeline last_pipeline_;
|
||||
vk::DescriptorSet descriptor_set_ = {};
|
||||
vk::PipelineLayout pipeline_layout_ = {};
|
||||
|
||||
RenderPassVK(const std::shared_ptr<const Context>& context,
|
||||
const RenderTarget& target,
|
||||
std::weak_ptr<CommandBufferVK> command_buffer);
|
||||
std::shared_ptr<CommandBufferVK> command_buffer);
|
||||
|
||||
// |RenderPass|
|
||||
void SetPipeline(
|
||||
const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetCommandLabel(std::string_view label) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetStencilReference(uint32_t value) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetBaseVertex(uint64_t value) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetViewport(Viewport viewport) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetScissor(IRect scissor) override;
|
||||
|
||||
// |RenderPass|
|
||||
void SetInstanceCount(size_t count) override;
|
||||
|
||||
// |RenderPass|
|
||||
bool SetVertexBuffer(VertexBuffer buffer) override;
|
||||
|
||||
// |RenderPass|
|
||||
fml::Status Draw() override;
|
||||
|
||||
// |RenderPass|
|
||||
void ReserveCommands(size_t command_count) override {}
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
// |RenderPass|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) override;
|
||||
|
||||
bool BindResource(size_t binding,
|
||||
DescriptorType type,
|
||||
const BufferView& view);
|
||||
|
||||
// |RenderPass|
|
||||
bool IsValid() const override;
|
||||
|
||||
@@ -66,8 +66,6 @@ TEST(GPUTracerVK, DoesNotTraceOutsideOfFrameWorkload) {
|
||||
auto called = GetMockVulkanFunctions(context->GetDevice());
|
||||
|
||||
ASSERT_NE(called, nullptr);
|
||||
ASSERT_TRUE(std::find(called->begin(), called->end(), "vkCreateQueryPool") ==
|
||||
called->end());
|
||||
ASSERT_TRUE(std::find(called->begin(), called->end(),
|
||||
"vkGetQueryPoolResults") == called->end());
|
||||
}
|
||||
|
||||
@@ -107,6 +107,24 @@ bool TextureVK::OnSetContents(const uint8_t* contents,
|
||||
© // regions
|
||||
);
|
||||
|
||||
// Transition to shader-read.
|
||||
{
|
||||
BarrierVK barrier;
|
||||
barrier.cmd_buffer = vk_cmd_buffer;
|
||||
barrier.src_access = vk::AccessFlagBits::eColorAttachmentWrite |
|
||||
vk::AccessFlagBits::eTransferWrite;
|
||||
barrier.src_stage = vk::PipelineStageFlagBits::eColorAttachmentOutput |
|
||||
vk::PipelineStageFlagBits::eTransfer;
|
||||
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
|
||||
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
|
||||
|
||||
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
||||
|
||||
if (!SetLayout(barrier)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return cmd_buffer->SubmitCommands();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,6 @@
|
||||
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEXTURE_VK_H_
|
||||
#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEXTURE_VK_H_
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/base/backend_cast.h"
|
||||
#include "impeller/core/texture.h"
|
||||
#include "impeller/renderer/backend/vulkan/context_vk.h"
|
||||
|
||||
@@ -23,6 +23,7 @@ bool Command::BindVertices(VertexBuffer buffer) {
|
||||
}
|
||||
|
||||
bool Command::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) {
|
||||
@@ -31,6 +32,7 @@ bool Command::BindResource(ShaderStage stage,
|
||||
|
||||
bool Command::BindResource(
|
||||
ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view) {
|
||||
@@ -66,6 +68,7 @@ bool Command::DoBindResource(ShaderStage stage,
|
||||
}
|
||||
|
||||
bool Command::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
|
||||
@@ -162,17 +162,20 @@ struct Command : public ResourceBinder {
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view);
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
|
||||
@@ -38,7 +38,6 @@ void CommandBuffer::WaitUntilScheduled() {
|
||||
|
||||
bool CommandBuffer::EncodeAndSubmit(
|
||||
const std::shared_ptr<RenderPass>& render_pass) {
|
||||
TRACE_EVENT0("impeller", "CommandBuffer::EncodeAndSubmit");
|
||||
if (!render_pass->IsValid() || !IsValid()) {
|
||||
return false;
|
||||
}
|
||||
@@ -52,7 +51,6 @@ bool CommandBuffer::EncodeAndSubmit(
|
||||
bool CommandBuffer::EncodeAndSubmit(
|
||||
const std::shared_ptr<BlitPass>& blit_pass,
|
||||
const std::shared_ptr<Allocator>& allocator) {
|
||||
TRACE_EVENT0("impeller", "CommandBuffer::EncodeAndSubmit");
|
||||
if (!blit_pass->IsValid() || !IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,12 @@
|
||||
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/shader_types.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
bool ComputeCommand::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) {
|
||||
@@ -29,6 +31,7 @@ bool ComputeCommand::BindResource(ShaderStage stage,
|
||||
}
|
||||
|
||||
bool ComputeCommand::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
|
||||
@@ -53,12 +53,14 @@ struct ComputeCommand : public ResourceBinder {
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
RenderPass::RenderPass(std::weak_ptr<const Context> context,
|
||||
RenderPass::RenderPass(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target)
|
||||
: context_(std::move(context)),
|
||||
sample_count_(target.GetSampleCount()),
|
||||
@@ -77,15 +77,10 @@ bool RenderPass::AddCommand(Command&& command) {
|
||||
}
|
||||
|
||||
bool RenderPass::EncodeCommands() const {
|
||||
auto context = context_.lock();
|
||||
// The context could have been collected in the meantime.
|
||||
if (!context) {
|
||||
return false;
|
||||
}
|
||||
return OnEncodeCommands(*context);
|
||||
return OnEncodeCommands(*context_);
|
||||
}
|
||||
|
||||
const std::weak_ptr<const Context>& RenderPass::GetContext() const {
|
||||
const std::shared_ptr<const Context>& RenderPass::GetContext() const {
|
||||
return context_;
|
||||
}
|
||||
|
||||
@@ -136,27 +131,30 @@ fml::Status RenderPass::Draw() {
|
||||
|
||||
// |ResourceBinder|
|
||||
bool RenderPass::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) {
|
||||
return pending_.BindResource(stage, slot, metadata, view);
|
||||
return pending_.BindResource(stage, type, slot, metadata, view);
|
||||
}
|
||||
|
||||
bool RenderPass::BindResource(
|
||||
ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view) {
|
||||
return pending_.BindResource(stage, slot, metadata, std::move(view));
|
||||
return pending_.BindResource(stage, type, slot, metadata, std::move(view));
|
||||
}
|
||||
|
||||
// |ResourceBinder|
|
||||
bool RenderPass::BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) {
|
||||
return pending_.BindResource(stage, slot, metadata, std::move(texture),
|
||||
return pending_.BindResource(stage, type, slot, metadata, std::move(texture),
|
||||
std::move(sampler));
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ class RenderPass : public ResourceBinder {
|
||||
public:
|
||||
virtual ~RenderPass();
|
||||
|
||||
const std::weak_ptr<const Context>& GetContext() const;
|
||||
const std::shared_ptr<const Context>& GetContext() const;
|
||||
|
||||
const RenderTarget& GetRenderTarget() const;
|
||||
|
||||
@@ -46,21 +46,18 @@ class RenderPass : public ResourceBinder {
|
||||
|
||||
void SetLabel(std::string label);
|
||||
|
||||
/// @brief Reserve [command_count] commands in the HAL command buffer.
|
||||
///
|
||||
/// Note: this is not the native command buffer.
|
||||
void ReserveCommands(size_t command_count) {
|
||||
virtual void ReserveCommands(size_t command_count) {
|
||||
commands_.reserve(command_count);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// The pipeline to use for this command.
|
||||
void SetPipeline(
|
||||
virtual void SetPipeline(
|
||||
const std::shared_ptr<Pipeline<PipelineDescriptor>>& pipeline);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// The debugging label to use for the command.
|
||||
void SetCommandLabel(std::string_view label);
|
||||
virtual void SetCommandLabel(std::string_view label);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// The reference value to use in stenciling operations. Stencil configuration
|
||||
@@ -69,9 +66,9 @@ class RenderPass : public ResourceBinder {
|
||||
/// @see `Pipeline`
|
||||
/// @see `PipelineDescriptor`
|
||||
///
|
||||
void SetStencilReference(uint32_t value);
|
||||
virtual void SetStencilReference(uint32_t value);
|
||||
|
||||
void SetBaseVertex(uint64_t value);
|
||||
virtual void SetBaseVertex(uint64_t value);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// The viewport coordinates that the rasterizer linearly maps normalized
|
||||
@@ -79,14 +76,14 @@ class RenderPass : public ResourceBinder {
|
||||
/// If unset, the viewport is the size of the render target with a zero
|
||||
/// origin, znear=0, and zfar=1.
|
||||
///
|
||||
void SetViewport(Viewport viewport);
|
||||
virtual void SetViewport(Viewport viewport);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// The scissor rect to use for clipping writes to the render target. The
|
||||
/// scissor rect must lie entirely within the render target.
|
||||
/// If unset, no scissor is applied.
|
||||
///
|
||||
void SetScissor(IRect scissor);
|
||||
virtual void SetScissor(IRect scissor);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// The number of instances of the given set of vertices to render. Not all
|
||||
@@ -95,7 +92,7 @@ class RenderPass : public ResourceBinder {
|
||||
/// @warning Setting this to more than one will limit the availability of
|
||||
/// backends to use with this command.
|
||||
///
|
||||
void SetInstanceCount(size_t count);
|
||||
virtual void SetInstanceCount(size_t count);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Specify the vertex and index buffer to use for this command.
|
||||
@@ -105,28 +102,32 @@ class RenderPass : public ResourceBinder {
|
||||
///
|
||||
/// @return returns if the binding was updated.
|
||||
///
|
||||
bool SetVertexBuffer(VertexBuffer buffer);
|
||||
virtual bool SetVertexBuffer(VertexBuffer buffer);
|
||||
|
||||
/// Record the currently pending command.
|
||||
fml::Status Draw();
|
||||
virtual fml::Status Draw();
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) override;
|
||||
virtual bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
BufferView view) override;
|
||||
|
||||
bool BindResource(ShaderStage stage,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view);
|
||||
virtual bool BindResource(
|
||||
ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const ShaderUniformSlot& slot,
|
||||
const std::shared_ptr<const ShaderMetadata>& metadata,
|
||||
BufferView view);
|
||||
|
||||
// |ResourceBinder|
|
||||
bool BindResource(ShaderStage stage,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) override;
|
||||
virtual bool BindResource(ShaderStage stage,
|
||||
DescriptorType type,
|
||||
const SampledImageSlot& slot,
|
||||
const ShaderMetadata& metadata,
|
||||
std::shared_ptr<const Texture> texture,
|
||||
std::shared_ptr<const Sampler> sampler) override;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Encode the recorded commands to the underlying command buffer.
|
||||
@@ -141,7 +142,7 @@ class RenderPass : public ResourceBinder {
|
||||
///
|
||||
/// @details Visible for testing.
|
||||
///
|
||||
const std::vector<Command>& GetCommands() const { return commands_; }
|
||||
virtual const std::vector<Command>& GetCommands() const { return commands_; }
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief The sample count of the attached render target.
|
||||
@@ -156,7 +157,7 @@ class RenderPass : public ResourceBinder {
|
||||
bool HasStencilAttachment() const;
|
||||
|
||||
protected:
|
||||
const std::weak_ptr<const Context> context_;
|
||||
const std::shared_ptr<const Context> context_;
|
||||
// The following properties: sample_count, pixel_format,
|
||||
// has_stencil_attachment, and render_target_size are cached on the
|
||||
// RenderTarget to speed up numerous lookups during rendering. This is safe as
|
||||
@@ -181,7 +182,8 @@ class RenderPass : public ResourceBinder {
|
||||
///
|
||||
bool AddCommand(Command&& command);
|
||||
|
||||
RenderPass(std::weak_ptr<const Context> context, const RenderTarget& target);
|
||||
RenderPass(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target);
|
||||
|
||||
virtual void OnSetLabel(std::string label) = 0;
|
||||
|
||||
|
||||
@@ -1255,21 +1255,6 @@ TEST_P(RendererTest, StencilMask) {
|
||||
OpenPlaygroundHere(callback);
|
||||
}
|
||||
|
||||
TEST_P(RendererTest, CanPreAllocateCommands) {
|
||||
auto context = GetContext();
|
||||
auto cmd_buffer = context->CreateCommandBuffer();
|
||||
auto render_target_cache = std::make_shared<RenderTargetAllocator>(
|
||||
GetContext()->GetResourceAllocator());
|
||||
|
||||
auto render_target = RenderTarget::CreateOffscreen(
|
||||
*context, *render_target_cache, {100, 100}, /*mip_count=*/1);
|
||||
auto render_pass = cmd_buffer->CreateRenderPass(render_target);
|
||||
|
||||
render_pass->ReserveCommands(100u);
|
||||
|
||||
EXPECT_EQ(render_pass->GetCommands().capacity(), 100u);
|
||||
}
|
||||
|
||||
TEST_P(RendererTest, CanLookupRenderTargetProperties) {
|
||||
auto context = GetContext();
|
||||
auto cmd_buffer = context->CreateCommandBuffer();
|
||||
|
||||
@@ -92,7 +92,7 @@ class MockBlitPass : public BlitPass {
|
||||
|
||||
class MockRenderPass : public RenderPass {
|
||||
public:
|
||||
MockRenderPass(std::weak_ptr<const Context> context,
|
||||
MockRenderPass(std::shared_ptr<const Context> context,
|
||||
const RenderTarget& target)
|
||||
: RenderPass(std::move(context), target) {}
|
||||
MOCK_METHOD(bool, IsValid, (), (const, override));
|
||||
|
||||
@@ -28,7 +28,7 @@ RenderPass::RenderPass()
|
||||
|
||||
RenderPass::~RenderPass() = default;
|
||||
|
||||
const std::weak_ptr<const impeller::Context>& RenderPass::GetContext() const {
|
||||
const std::shared_ptr<const impeller::Context>& RenderPass::GetContext() const {
|
||||
return render_pass_->GetContext();
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ RenderPass::GetOrCreatePipeline() {
|
||||
}
|
||||
}
|
||||
|
||||
auto& context = *GetContext().lock();
|
||||
auto& context = *GetContext();
|
||||
|
||||
render_pipeline_->BindToPipelineDescriptor(*context.GetShaderLibrary(),
|
||||
pipeline_desc);
|
||||
@@ -151,23 +151,27 @@ bool RenderPass::Draw() {
|
||||
}
|
||||
render_pass_->SetVertexBuffer(result.vertex_buffer);
|
||||
for (const auto& buffer : result.vertex_bindings.buffers) {
|
||||
render_pass_->BindResource(impeller::ShaderStage::kVertex, buffer.slot,
|
||||
*buffer.view.GetMetadata(),
|
||||
render_pass_->BindResource(impeller::ShaderStage::kVertex,
|
||||
impeller::DescriptorType::kUniformBuffer,
|
||||
buffer.slot, *buffer.view.GetMetadata(),
|
||||
buffer.view.resource);
|
||||
}
|
||||
for (const auto& texture : result.vertex_bindings.sampled_images) {
|
||||
render_pass_->BindResource(impeller::ShaderStage::kVertex, texture.slot,
|
||||
*texture.texture.GetMetadata(),
|
||||
render_pass_->BindResource(impeller::ShaderStage::kVertex,
|
||||
impeller::DescriptorType::kSampledImage,
|
||||
texture.slot, *texture.texture.GetMetadata(),
|
||||
texture.texture.resource, texture.sampler);
|
||||
}
|
||||
for (const auto& buffer : result.fragment_bindings.buffers) {
|
||||
render_pass_->BindResource(impeller::ShaderStage::kFragment, buffer.slot,
|
||||
*buffer.view.GetMetadata(),
|
||||
render_pass_->BindResource(impeller::ShaderStage::kFragment,
|
||||
impeller::DescriptorType::kUniformBuffer,
|
||||
buffer.slot, *buffer.view.GetMetadata(),
|
||||
buffer.view.resource);
|
||||
}
|
||||
for (const auto& texture : result.fragment_bindings.sampled_images) {
|
||||
render_pass_->BindResource(impeller::ShaderStage::kFragment, texture.slot,
|
||||
*texture.texture.GetMetadata(),
|
||||
render_pass_->BindResource(impeller::ShaderStage::kFragment,
|
||||
impeller::DescriptorType::kSampledImage,
|
||||
texture.slot, *texture.texture.GetMetadata(),
|
||||
texture.texture.resource, texture.sampler);
|
||||
}
|
||||
return render_pass_->Draw().ok();
|
||||
@@ -377,7 +381,8 @@ static bool BindUniform(flutter::gpu::RenderPass* wrapper,
|
||||
}
|
||||
|
||||
return command.BindResource(
|
||||
shader->GetShaderStage(), uniform_struct->slot, uniform_struct->metadata,
|
||||
shader->GetShaderStage(), impeller::DescriptorType::kUniformBuffer,
|
||||
uniform_struct->slot, uniform_struct->metadata,
|
||||
impeller::BufferView{
|
||||
.buffer = buffer,
|
||||
.range = impeller::Range(offset_in_bytes, length_in_bytes),
|
||||
@@ -443,12 +448,12 @@ bool InternalFlutterGpu_RenderPass_BindTexture(
|
||||
flutter::gpu::ToImpellerSamplerAddressMode(width_address_mode);
|
||||
sampler_desc.height_address_mode =
|
||||
flutter::gpu::ToImpellerSamplerAddressMode(height_address_mode);
|
||||
auto sampler = wrapper->GetContext().lock()->GetSamplerLibrary()->GetSampler(
|
||||
sampler_desc);
|
||||
auto sampler =
|
||||
wrapper->GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
|
||||
|
||||
return command.BindResource(shader->GetShaderStage(), *image_slot,
|
||||
impeller::ShaderMetadata{}, texture->GetTexture(),
|
||||
sampler);
|
||||
return command.BindResource(
|
||||
shader->GetShaderStage(), impeller::DescriptorType::kSampledImage,
|
||||
*image_slot, impeller::ShaderMetadata{}, texture->GetTexture(), sampler);
|
||||
}
|
||||
|
||||
void InternalFlutterGpu_RenderPass_ClearBindings(
|
||||
|
||||
@@ -33,7 +33,7 @@ class RenderPass : public RefCountedDartWrappable<RenderPass> {
|
||||
|
||||
~RenderPass() override;
|
||||
|
||||
const std::weak_ptr<const impeller::Context>& GetContext() const;
|
||||
const std::shared_ptr<const impeller::Context>& GetContext() const;
|
||||
|
||||
impeller::Command& GetCommand();
|
||||
const impeller::Command& GetCommand() const;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "flutter/impeller/core/texture_descriptor.h"
|
||||
#include "flutter/impeller/display_list/dl_image_impeller.h"
|
||||
#include "flutter/impeller/renderer/backend/vulkan/android_hardware_buffer_texture_source_vk.h"
|
||||
#include "flutter/impeller/renderer/backend/vulkan/command_buffer_vk.h"
|
||||
#include "flutter/impeller/renderer/backend/vulkan/command_encoder_vk.h"
|
||||
#include "flutter/impeller/renderer/backend/vulkan/texture_vk.h"
|
||||
#include "flutter/shell/platform/android/ndk_helpers.h"
|
||||
|
||||
@@ -59,6 +61,31 @@ void ImageExternalTextureVK::ProcessFrame(PaintContext& context,
|
||||
|
||||
auto texture =
|
||||
std::make_shared<impeller::TextureVK>(impeller_context_, texture_source);
|
||||
// Transition the layout to shader read.
|
||||
{
|
||||
auto buffer = impeller_context_->CreateCommandBuffer();
|
||||
impeller::CommandBufferVK& buffer_vk =
|
||||
impeller::CommandBufferVK::Cast(*buffer);
|
||||
|
||||
impeller::BarrierVK barrier;
|
||||
barrier.cmd_buffer = buffer_vk.GetEncoder()->GetCommandBuffer();
|
||||
barrier.src_access = impeller::vk::AccessFlagBits::eColorAttachmentWrite |
|
||||
impeller::vk::AccessFlagBits::eTransferWrite;
|
||||
barrier.src_stage =
|
||||
impeller::vk::PipelineStageFlagBits::eColorAttachmentOutput |
|
||||
impeller::vk::PipelineStageFlagBits::eTransfer;
|
||||
barrier.dst_access = impeller::vk::AccessFlagBits::eShaderRead;
|
||||
barrier.dst_stage = impeller::vk::PipelineStageFlagBits::eFragmentShader;
|
||||
|
||||
barrier.new_layout = impeller::vk::ImageLayout::eShaderReadOnlyOptimal;
|
||||
|
||||
if (!texture->SetLayout(barrier)) {
|
||||
return;
|
||||
}
|
||||
if (!buffer->SubmitCommands()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
dl_image_ = impeller::DlImageImpeller::Make(texture);
|
||||
CloseHardwareBuffer(hardware_buffer);
|
||||
|
||||
Reference in New Issue
Block a user