Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Move primitive type to pipeline descriptor #37315

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions impeller/entity/contents/clip_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ bool ClipContents::Render(const ContentContext& renderer,
{
cmd.label = "Difference Clip (Increment)";

cmd.primitive_type = PrimitiveType::kTriangleStrip;
Copy link
Member

Choose a reason for hiding this comment

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

How did we manage to get rid of the strip here without reworking the vertex buffer? Was it just incorrect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chinmaygarde it was always overwritten, see line: 123. I think there was a refactor at some point to use the geometry_result but this line never got removed.

Copy link
Member

Choose a reason for hiding this comment

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

Got it.

auto points = Rect(Size(pass.GetRenderTargetSize())).GetPoints();
auto vertices =
VertexBufferBuilder<VS::PerVertexData>{}
Expand All @@ -104,7 +103,6 @@ bool ClipContents::Render(const ContentContext& renderer,
{
cmd.label = "Difference Clip (Punch)";

cmd.primitive_type = PrimitiveType::kTriangle;
cmd.stencil_reference = entity.GetStencilDepth() + 1;
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kDecrementClamp;
Expand All @@ -115,12 +113,13 @@ bool ClipContents::Render(const ContentContext& renderer,
options.stencil_operation = StencilOperation::kIncrementClamp;
}

auto geometry_result = geometry_->GetPositionBuffer(renderer, entity, pass);
options.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetClipPipeline(options);

auto allocator = renderer.GetContext()->GetResourceAllocator();
auto geometry_result = geometry_->GetPositionBuffer(renderer, entity, pass);
cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;

info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
Expand Down
2 changes: 2 additions & 0 deletions impeller/entity/contents/content_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ void ContentContextOptions::ApplyToPipelineDescriptor(
stencil.depth_stencil_pass = stencil_operation;
desc.SetStencilAttachmentDescriptors(stencil);
}

desc.SetPrimitiveType(primitive_type);
}

template <typename PipelineT>
Expand Down
6 changes: 4 additions & 2 deletions impeller/entity/contents/content_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ struct ContentContextOptions {
BlendMode blend_mode = BlendMode::kSourceOver;
CompareFunction stencil_compare = CompareFunction::kEqual;
StencilOperation stencil_operation = StencilOperation::kKeep;
PrimitiveType primitive_type = PrimitiveType::kTriangle;

struct Hash {
constexpr std::size_t operator()(const ContentContextOptions& o) const {
return fml::HashCombine(o.sample_count, o.blend_mode, o.stencil_compare,
o.stencil_operation);
o.stencil_operation, o.primitive_type);
}
};

Expand All @@ -184,7 +185,8 @@ struct ContentContextOptions {
return lhs.sample_count == rhs.sample_count &&
lhs.blend_mode == rhs.blend_mode &&
lhs.stencil_compare == rhs.stencil_compare &&
lhs.stencil_operation == rhs.stencil_operation;
lhs.stencil_operation == rhs.stencil_operation &&
lhs.primitive_type == rhs.primitive_type;
}
};

Expand Down
1 change: 1 addition & 0 deletions impeller/entity/contents/contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fml/logging.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/render_pass.h"

namespace impeller {
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/linear_gradient_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ bool LinearGradientContents::Render(const ContentContext& renderer,
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kIncrementClamp;
}
options.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetLinearGradientFillPipeline(options);

cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));
SamplerDescriptor sampler_desc;
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/radial_gradient_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ bool RadialGradientContents::Render(const ContentContext& renderer,
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kIncrementClamp;
}
options.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetRadialGradientFillPipeline(options);

cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));
SamplerDescriptor sampler_desc;
Expand Down
6 changes: 3 additions & 3 deletions impeller/entity/contents/rrect_shadow_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ bool RRectShadowContents::Render(const ContentContext& renderer,

Command cmd;
cmd.label = "RRect Shadow";
cmd.pipeline =
renderer.GetRRectBlurPipeline(OptionsFromPassAndEntity(pass, entity));
auto opts = OptionsFromPassAndEntity(pass, entity);
opts.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline = renderer.GetRRectBlurPipeline(opts);
cmd.stencil_reference = entity.GetStencilDepth();

cmd.primitive_type = PrimitiveType::kTriangle;
cmd.BindVertices(vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));

VS::VertInfo vert_info;
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/runtime_effect_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kIncrementClamp;
}
options.primitive_type = geometry_result.type;
options.ApplyToPipelineDescriptor(desc);

auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).get();
Expand All @@ -135,7 +136,6 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
cmd.pipeline = pipeline;
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;

//--------------------------------------------------------------------------
/// Vertex stage uniforms.
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/solid_color_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ bool SolidColorContents::Render(const ContentContext& renderer,
options.stencil_operation = StencilOperation::kIncrementClamp;
}

options.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetSolidFillPipeline(options);
cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;

VS::VertInfo vert_info;
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/sweep_gradient_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ bool SweepGradientContents::Render(const ContentContext& renderer,
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kIncrementClamp;
}
options.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetSweepGradientFillPipeline(options);

cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;
FS::BindGradientInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info));
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
Expand Down
12 changes: 6 additions & 6 deletions impeller/entity/contents/text_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ bool TextContents::RenderSdf(const ContentContext& renderer,
// Information shared by all glyph draw calls.
Command cmd;
cmd.label = "TextFrameSDF";
cmd.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline =
renderer.GetGlyphAtlasSdfPipeline(OptionsFromPassAndEntity(pass, entity));
auto opts = OptionsFromPassAndEntity(pass, entity);
opts.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline = renderer.GetGlyphAtlasSdfPipeline(opts);
cmd.stencil_reference = entity.GetStencilDepth();

return CommonRender<GlyphAtlasSdfPipeline>(renderer, entity, pass, color_,
Expand Down Expand Up @@ -224,9 +224,9 @@ bool TextContents::Render(const ContentContext& renderer,
// Information shared by all glyph draw calls.
Command cmd;
cmd.label = "TextFrame";
cmd.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline =
renderer.GetGlyphAtlasPipeline(OptionsFromPassAndEntity(pass, entity));
auto opts = OptionsFromPassAndEntity(pass, entity);
opts.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline = renderer.GetGlyphAtlasPipeline(opts);
cmd.stencil_reference = entity.GetStencilDepth();

return CommonRender<GlyphAtlasPipeline>(renderer, entity, pass, color_,
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/tiled_texture_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ bool TiledTextureContents::Render(const ContentContext& renderer,
options.stencil_compare = CompareFunction::kEqual;
options.stencil_operation = StencilOperation::kIncrementClamp;
}
options.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetTiledTexturePipeline(options);

cmd.BindVertices(geometry_result.vertex_buffer);
cmd.primitive_type = geometry_result.type;
VS::BindVertInfo(cmd, host_buffer.EmplaceUniform(vert_info));
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
FS::BindTextureSampler(cmd, texture_,
Expand Down
12 changes: 6 additions & 6 deletions impeller/entity/contents/vertices_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ bool VerticesContents::Render(const ContentContext& renderer,
cmd.label = "Vertices";
cmd.stencil_reference = entity.GetStencilDepth();

auto opts = OptionsFromPassAndEntity(pass, entity);

switch (vertex_type) {
case GeometryVertexType::kColor: {
using VS = GeometryColorPipeline::VertexShader;

auto geometry_result = geometry_->GetPositionColorBuffer(
renderer, entity, pass, color_, blend_mode_);
cmd.pipeline = renderer.GetGeometryColorPipeline(
OptionsFromPassAndEntity(pass, entity));
cmd.primitive_type = geometry_result.type;
opts.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetGeometryColorPipeline(opts);
cmd.BindVertices(geometry_result.vertex_buffer);

VS::VertInfo vert_info;
Expand All @@ -70,9 +71,8 @@ bool VerticesContents::Render(const ContentContext& renderer,

auto geometry_result =
geometry_->GetPositionBuffer(renderer, entity, pass);
cmd.pipeline = renderer.GetGeometryPositionPipeline(
OptionsFromPassAndEntity(pass, entity));
cmd.primitive_type = geometry_result.type;
opts.primitive_type = geometry_result.type;
cmd.pipeline = renderer.GetGeometryPositionPipeline(opts);
cmd.BindVertices(geometry_result.vertex_buffer);

VS::VertInfo vert_info;
Expand Down
3 changes: 1 addition & 2 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ TEST_P(EntityTest, BlendingModeOptions) {
cmd.label = "Blended Rectangle";
auto options = OptionsFromPass(pass);
options.blend_mode = blend_mode;
options.primitive_type = PrimitiveType::kTriangle;
cmd.pipeline = context.GetSolidFillPipeline(options);
cmd.BindVertices(
vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
Expand All @@ -772,8 +773,6 @@ TEST_P(EntityTest, BlendingModeOptions) {
FS::BindFragInfo(cmd,
pass.GetTransientsBuffer().EmplaceUniform(frag_info));

cmd.primitive_type = PrimitiveType::kTriangle;

return pass.AddCommand(std::move(cmd));
};

Expand Down
1 change: 0 additions & 1 deletion impeller/playground/imgui/imgui_impl_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
vertex_buffer.index_type = impeller::IndexType::k16bit;
cmd.BindVertices(vertex_buffer);
cmd.base_vertex = pcmd->VtxOffset;
cmd.primitive_type = impeller::PrimitiveType::kTriangle;

render_pass.AddCommand(std::move(cmd));
}
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/gles/render_pass_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ struct RenderPassData {
//--------------------------------------------------------------------------
/// Finally! Invoke the draw call.
///
gl.DrawElements(ToMode(command.primitive_type), // mode
PrimitiveType primitive_type = pipeline.GetDescriptor().GetPrimitiveType();
gl.DrawElements(ToMode(primitive_type), // mode
command.index_count, // count
ToIndexType(command.index_type), // type
reinterpret_cast<const GLvoid*>(static_cast<GLsizei>(
Expand Down
6 changes: 4 additions & 2 deletions impeller/renderer/backend/metal/render_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ static bool Bind(PassBindingsCache& pass,
return false;
}

const PrimitiveType primitive_type = pipeline_desc.GetPrimitiveType();
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Could just convert it to MTLPrimitiveType here.


FML_DCHECK(command.index_count *
(command.index_type == IndexType::k16bit ? 2 : 4) ==
command.index_buffer.range.length);
Expand All @@ -503,7 +505,7 @@ static bool Bind(PassBindingsCache& pass,
VALIDATION_LOG << "iOS Simulator does not support instanced rendering.";
return false;
#endif
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(command.primitive_type)
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(primitive_type)
indexCount:command.index_count
indexType:ToMTLIndexType(command.index_type)
indexBuffer:mtl_index_buffer
Expand All @@ -512,7 +514,7 @@ static bool Bind(PassBindingsCache& pass,
baseVertex:command.base_vertex
baseInstance:0u];
} else {
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(command.primitive_type)
[encoder drawIndexedPrimitives:ToMTLPrimitiveType(primitive_type)
indexCount:command.index_count
indexType:ToMTLIndexType(command.index_type)
indexBuffer:mtl_index_buffer
Expand Down
15 changes: 15 additions & 0 deletions impeller/renderer/backend/vulkan/formats_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,19 @@ constexpr vk::IndexType ToVKIndexType(IndexType index_type) {
}
}

constexpr vk::PrimitiveTopology ToVKPrimitiveTopology(PrimitiveType primitive) {
switch (primitive) {
case PrimitiveType::kTriangle:
return vk::PrimitiveTopology::eTriangleList;
case PrimitiveType::kTriangleStrip:
return vk::PrimitiveTopology::eTriangleStrip;
case PrimitiveType::kLine:
return vk::PrimitiveTopology::eLineList;
case PrimitiveType::kLineStrip:
return vk::PrimitiveTopology::eLineStrip;
case PrimitiveType::kPoint:
return vk::PrimitiveTopology::ePointList;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we add FML_UNREACHABLE just to better guarantee exhaustive switches? The functions earlier in this TU do this as do all the other backends. For consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call! done: #37324


} // namespace impeller
6 changes: 2 additions & 4 deletions impeller/renderer/backend/vulkan/pipeline_library_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,9 @@ std::unique_ptr<PipelineCreateInfoVK> PipelineLibraryVK::CreatePipeline(

//----------------------------------------------------------------------------
/// Primitive Input Assembly State
/// TODO(106379): Move primitive topology to the the pipeline instead of it
/// being on the draw call. This is hard-coded right now.
///
vk::PipelineInputAssemblyStateCreateInfo input_assembly;
input_assembly.setTopology(vk::PrimitiveTopology::eTriangleList);
const auto topology = ToVKPrimitiveTopology(desc.GetPrimitiveType());
input_assembly.setTopology(topology);
pipeline_info.setPInputAssemblyState(&input_assembly);

//----------------------------------------------------------------------------
Expand Down
7 changes: 0 additions & 7 deletions impeller/renderer/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ struct Command {
///
std::string label;
//----------------------------------------------------------------------------
/// The type of primitives in the vertex buffer. Set the vertex and index
/// buffers using a call to `BindVertices`.
///
/// @see `BindVertices`
///
PrimitiveType primitive_type = PrimitiveType::kTriangle;
//----------------------------------------------------------------------------
/// The reference value to use in stenciling operations. Stencil configuration
/// is part of pipeline setup and can be read from the pipelines descriptor.
///
Expand Down
8 changes: 8 additions & 0 deletions impeller/renderer/pipeline_descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,12 @@ WindingOrder PipelineDescriptor::GetWindingOrder() const {
return winding_order_;
}

void PipelineDescriptor::SetPrimitiveType(PrimitiveType type) {
primitive_type_ = type;
}

PrimitiveType PipelineDescriptor::GetPrimitiveType() const {
return primitive_type_;
}

} // namespace impeller
5 changes: 5 additions & 0 deletions impeller/renderer/pipeline_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class PipelineDescriptor final : public Comparable<PipelineDescriptor> {

WindingOrder GetWindingOrder() const;

void SetPrimitiveType(PrimitiveType type);
Copy link
Member

Choose a reason for hiding this comment

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

The setters return a reference to the descriptor so we can chain them. Though I don't see that pattern followed a lot.


PrimitiveType GetPrimitiveType() const;

private:
std::string label_;
SampleCount sample_count_ = SampleCount::kCount1;
Expand All @@ -131,6 +135,7 @@ class PipelineDescriptor final : public Comparable<PipelineDescriptor> {
front_stencil_attachment_descriptor_;
std::optional<StencilAttachmentDescriptor>
back_stencil_attachment_descriptor_;
PrimitiveType primitive_type_ = PrimitiveType::kTriangle;
};

using PipelineMap = std::unordered_map<
Expand Down
8 changes: 0 additions & 8 deletions impeller/renderer/renderer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ TEST_P(RendererTest, CanCreateBoxPrimitive) {
pass.GetTransientsBuffer().EmplaceUniform(frame_info));
FS::BindContents1(cmd, boston, sampler);
FS::BindContents2(cmd, bridge, sampler);

cmd.primitive_type = PrimitiveType::kTriangle;
if (!pass.AddCommand(std::move(cmd))) {
return false;
}
Expand Down Expand Up @@ -192,8 +190,6 @@ TEST_P(RendererTest, CanRenderPerspectiveCube) {
Matrix::MakeRotationZ(Radians(euler_angles.z));
VS::BindUniformBuffer(cmd,
pass.GetTransientsBuffer().EmplaceUniform(uniforms));

cmd.primitive_type = PrimitiveType::kTriangle;
if (!pass.AddCommand(std::move(cmd))) {
return false;
}
Expand Down Expand Up @@ -254,8 +250,6 @@ TEST_P(RendererTest, CanRenderMultiplePrimitives) {
FS::BindContents1(cmd, boston, sampler);
FS::BindContents2(cmd, bridge, sampler);

cmd.primitive_type = PrimitiveType::kTriangle;

for (size_t i = 0; i < 1; i++) {
for (size_t j = 0; j < 1; j++) {
VS::UniformBuffer uniforms;
Expand Down Expand Up @@ -369,8 +363,6 @@ TEST_P(RendererTest, CanRenderToTexture) {
FS::BindContents1(cmd, boston, sampler);
FS::BindContents2(cmd, bridge, sampler);

cmd.primitive_type = PrimitiveType::kTriangle;

VS::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic(ISize{1024, 768}) *
Matrix::MakeTranslation({50.0f, 50.0f, 0.0f});
Expand Down