Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] remove std::vector usage in render pass vk. #57132

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions impeller/renderer/backend/vulkan/render_pass_builder_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "impeller/renderer/backend/vulkan/render_pass_builder_vk.h"

#include <vector>

#include "impeller/core/formats.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
#include "vulkan/vulkan_enums.hpp"
Expand Down Expand Up @@ -119,57 +117,59 @@ vk::UniqueRenderPass RenderPassBuilderVK::Build(
color_attachments_count++;
}

std::vector<vk::AttachmentDescription> attachments;

std::vector<vk::AttachmentReference> color_refs(color_attachments_count,
kUnusedAttachmentReference);
std::vector<vk::AttachmentReference> resolve_refs(color_attachments_count,
kUnusedAttachmentReference);
std::array<vk::AttachmentDescription, kMaxAttachments> attachments;
std::array<vk::AttachmentReference, kMaxColorAttachments> color_refs;
std::array<vk::AttachmentReference, kMaxColorAttachments> resolve_refs;
vk::AttachmentReference depth_stencil_ref = kUnusedAttachmentReference;
size_t attachments_index = 0;
size_t color_index = 0;
size_t resolve_index = 0;

if (color0_.has_value()) {
vk::AttachmentReference color_ref;
color_ref.attachment = attachments.size();
color_ref.attachment = attachments_index;
color_ref.layout = vk::ImageLayout::eGeneral;
color_refs[0] = color_ref;
attachments.push_back(color0_.value());
color_refs[color_index++] = color_ref;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using .at(color_index++) instead of [color_index++]. The former performs range checking in debug for throwing the exceptions (which we disable). The latter is free to write into garbage memory. Some safety net in debug should be good to have IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a break if we hit more than 16 attachments. otherwise there shouldn't (TM) need to be bounds checks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added at

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other places where the bounds checks would be harder to add so I made them use .at() too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, its hard to imagine cases where we could blow the limit. But the .at() to me also reads better since it implies no reallocations may occur if say the collection was a map.

attachments[attachments_index++] = color0_.value();

if (color0_resolve_.has_value()) {
vk::AttachmentReference resolve_ref;
resolve_ref.attachment = attachments.size();
resolve_ref.attachment = attachments_index;
resolve_ref.layout = vk::ImageLayout::eGeneral;
resolve_refs[0] = resolve_ref;
attachments.push_back(color0_resolve_.value());
resolve_refs[resolve_index++] = resolve_ref;
attachments[attachments_index++] = color0_resolve_.value();
}
}

for (const auto& color : colors_) {
vk::AttachmentReference color_ref;
color_ref.attachment = attachments.size();
color_ref.attachment = attachments_index;
color_ref.layout = vk::ImageLayout::eGeneral;
color_refs[color.first] = color_ref;
attachments.push_back(color.second);
color_refs[color_index++] = color_ref;
attachments[attachments_index++] = color.second;

if (auto found = resolves_.find(color.first); found != resolves_.end()) {
vk::AttachmentReference resolve_ref;
resolve_ref.attachment = attachments.size();
resolve_ref.attachment = attachments_index;
resolve_ref.layout = vk::ImageLayout::eGeneral;
resolve_refs[color.first] = resolve_ref;
attachments.push_back(found->second);
resolve_refs[resolve_index++] = resolve_ref;
attachments[attachments_index++] = found->second;
}
}

if (depth_stencil_.has_value()) {
depth_stencil_ref.attachment = attachments.size();
depth_stencil_ref.attachment = attachments_index;
depth_stencil_ref.layout = vk::ImageLayout::eGeneral;
attachments.push_back(depth_stencil_.value());
attachments[attachments_index++] = depth_stencil_.value();
}

vk::SubpassDescription subpass0;
subpass0.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
subpass0.setInputAttachments(color_refs);
subpass0.setColorAttachments(color_refs);
subpass0.setResolveAttachments(resolve_refs);
subpass0.setPInputAttachments(color_refs.data());
subpass0.setInputAttachmentCount(color_index);
subpass0.setPColorAttachments(color_refs.data());
subpass0.setColorAttachmentCount(color_index);
subpass0.setPResolveAttachments(resolve_refs.data());
subpass0.setPDepthStencilAttachment(&depth_stencil_ref);

vk::SubpassDependency self_dep;
Expand Down
5 changes: 4 additions & 1 deletion impeller/renderer/backend/vulkan/render_pass_builder_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
#include <map>
#include <optional>

#include "flutter/fml/macros.h"
#include "impeller/core/formats.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"

namespace impeller {

static constexpr size_t kMaxColorAttachments = 16;
static constexpr size_t kMaxAttachments =
(kMaxColorAttachments * 2) + 1; // MSAA + resolve plus depth/stencil

class RenderPassBuilderVK {
public:
RenderPassBuilderVK();
Expand Down
58 changes: 31 additions & 27 deletions impeller/renderer/backend/vulkan/render_pass_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <array>
#include <cstdint>
#include <vector>

#include "fml/status.h"
#include "impeller/base/validation.h"
Expand Down Expand Up @@ -52,15 +51,16 @@ static vk::ClearDepthStencilValue VKClearValueFromDepthStencil(uint32_t stencil,
return value;
}

static std::vector<vk::ClearValue> GetVKClearValues(
const RenderTarget& target) {
std::vector<vk::ClearValue> clears;

static size_t GetVKClearValues(
const RenderTarget& target,
std::array<vk::ClearValue, kMaxAttachments>& values) {
size_t offset = 0u;
target.IterateAllColorAttachments(
[&clears](size_t index, const ColorAttachment& attachment) -> bool {
clears.emplace_back(VKClearValueFromColor(attachment.clear_color));
[&values, &offset](size_t index,
const ColorAttachment& attachment) -> bool {
values[offset++] = VKClearValueFromColor(attachment.clear_color);
if (attachment.resolve_texture) {
clears.emplace_back(VKClearValueFromColor(attachment.clear_color));
values[offset++] = VKClearValueFromColor(attachment.clear_color);
}
return true;
});
Expand All @@ -69,14 +69,13 @@ static std::vector<vk::ClearValue> GetVKClearValues(
const auto& stencil = target.GetStencilAttachment();

if (depth.has_value()) {
clears.emplace_back(VKClearValueFromDepthStencil(
stencil ? stencil->clear_stencil : 0u, depth->clear_depth));
values[offset++] = VKClearValueFromDepthStencil(
stencil ? stencil->clear_stencil : 0u, depth->clear_depth);
} else if (stencil.has_value()) {
clears.emplace_back(VKClearValueFromDepthStencil(
stencil->clear_stencil, depth ? depth->clear_depth : 0.0f));
values[offset++] = VKClearValueFromDepthStencil(
stencil->clear_stencil, depth ? depth->clear_depth : 0.0f);
}

return clears;
return offset;
}

SharedHandleVK<vk::RenderPass> RenderPassVK::CreateVKRenderPass(
Expand Down Expand Up @@ -191,15 +190,17 @@ RenderPassVK::RenderPassVK(const std::shared_ptr<const Context>& context,
TextureVK::Cast(*resolve_image_vk_).SetCachedRenderPass(render_pass_);
}

auto clear_values = GetVKClearValues(render_target_);
std::array<vk::ClearValue, kMaxAttachments> clears;
size_t clear_count = GetVKClearValues(render_target_, clears);

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);
pass_info.setPClearValues(clears.data());
pass_info.setClearValueCount(clear_count);

command_buffer_vk_.beginRenderPass(pass_info, vk::SubpassContents::eInline);

Expand Down Expand Up @@ -252,34 +253,37 @@ SharedHandleVK<vk::Framebuffer> RenderPassVK::CreateVKFramebuffer(
fb_info.height = target_size.height;
fb_info.layers = 1u;

std::vector<vk::ImageView> attachments;
std::array<vk::ImageView, kMaxAttachments> attachments;
size_t count = 0;

// This bit must be consistent to ensure compatibility with the pass created
// earlier. Follow this order: Color attachments, then depth-stencil, then
// stencil.
render_target_.IterateAllColorAttachments(
[&attachments](size_t index, const ColorAttachment& attachment) -> bool {
[&attachments, &count](size_t index,
const ColorAttachment& attachment) -> bool {
// The bind point doesn't matter here since that information is present
// in the render pass.
attachments.emplace_back(
TextureVK::Cast(*attachment.texture).GetRenderTargetView());
attachments[count++] =
TextureVK::Cast(*attachment.texture).GetRenderTargetView();
if (attachment.resolve_texture) {
attachments.emplace_back(TextureVK::Cast(*attachment.resolve_texture)
.GetRenderTargetView());
attachments[count++] = TextureVK::Cast(*attachment.resolve_texture)
.GetRenderTargetView();
}
return true;
});

if (auto depth = render_target_.GetDepthAttachment(); depth.has_value()) {
attachments.emplace_back(
TextureVK::Cast(*depth->texture).GetRenderTargetView());
attachments[count++] =
TextureVK::Cast(*depth->texture).GetRenderTargetView();
} else if (auto stencil = render_target_.GetStencilAttachment();
stencil.has_value()) {
attachments.emplace_back(
TextureVK::Cast(*stencil->texture).GetRenderTargetView());
attachments[count++] =
TextureVK::Cast(*stencil->texture).GetRenderTargetView();
}

fb_info.setAttachments(attachments);
fb_info.setPAttachments(attachments.data());
fb_info.setAttachmentCount(count);

auto [result, framebuffer] =
context.GetDevice().createFramebufferUnique(fb_info);
Expand Down
Loading