From 9793900968d1e715dd2bd09841c2e767682c8091 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 24 May 2021 21:36:42 -0700 Subject: [PATCH] Setup vertex attributes. --- .../impeller/compiler/code_gen_template.h | 4 +- .../impeller/host/impeller_renderer.mm | 7 +++- .../impeller/impeller/compositor/context.mm | 10 +++++ .../compositor/pipeline_descriptor.mm | 5 +++ .../impeller/compositor/pipeline_library.h | 4 ++ .../impeller/impeller/compositor/renderer.h | 2 + .../impeller/impeller/compositor/renderer.mm | 4 ++ .../impeller/compositor/vertex_descriptor.h | 8 ++-- .../impeller/compositor/vertex_descriptor.mm | 36 ++++++++++------ .../impeller/impeller/primitives/box.h | 2 +- .../impeller/impeller/primitives/box.mm | 42 +++++++++++++++---- .../impeller/impeller/primitives/box.vert | 33 +++------------ .../impeller/shader_glue/shader_types.h | 1 + 13 files changed, 101 insertions(+), 57 deletions(-) diff --git a/engine/src/flutter/impeller/compiler/code_gen_template.h b/engine/src/flutter/impeller/compiler/code_gen_template.h index ff03f6789e..5948b29631 100644 --- a/engine/src/flutter/impeller/compiler/code_gen_template.h +++ b/engine/src/flutter/impeller/compiler/code_gen_template.h @@ -34,9 +34,9 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info { }; {% endfor %} - static constexpr const ShaderStageInput* kAllShaderStageInputs[] = { + static constexpr std::array kAllShaderStageInputs[] = { {% for stage_input in stage_inputs %} - &kInput{{camel_case(stage_input.name)}}, // {{stage_input.name}} + { &kInput{{camel_case(stage_input.name)}} }, // {{stage_input.name}} {% endfor %} }; diff --git a/engine/src/flutter/impeller/host/impeller_renderer.mm b/engine/src/flutter/impeller/host/impeller_renderer.mm index 51ea49a43c..7129193df1 100644 --- a/engine/src/flutter/impeller/host/impeller_renderer.mm +++ b/engine/src/flutter/impeller/host/impeller_renderer.mm @@ -8,9 +8,9 @@ #import "assets_location.h" #include "flutter/fml/logging.h" #import "impeller/compositor/renderer.h" +#import "impeller/primitives/box.h" #import "impeller_renderer.h" #import "shaders_location.h" - // Include header shared between C code here, which executes Metal API commands, // and .metal files #import "ShaderTypes.h" @@ -55,10 +55,15 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100; renderer_ = std::make_unique( impeller::ImpellerShadersDirectory()); + if (!renderer_->IsValid()) { FML_LOG(ERROR) << "Impeller Renderer is not valid."; } + if (!impeller::RenderBox(renderer_->GetContext())) { + FML_LOG(ERROR) << "Could not render box."; + } + return self; } diff --git a/engine/src/flutter/impeller/impeller/compositor/context.mm b/engine/src/flutter/impeller/impeller/compositor/context.mm index cbba8d7c5b..81f9fa0d90 100644 --- a/engine/src/flutter/impeller/impeller/compositor/context.mm +++ b/engine/src/flutter/impeller/impeller/compositor/context.mm @@ -47,6 +47,12 @@ Context::Context(std::string shaders_directory) std::shared_ptr(new ShaderLibrary(library)); } + // Setup the pipeline library. + { // + pipeline_library_ = + std::shared_ptr(new PipelineLibrary(device_)); + } + is_valid_ = true; } @@ -68,4 +74,8 @@ std::shared_ptr Context::GetShaderLibrary() const { return shader_library_; } +std::shared_ptr Context::GetPipelineLibrary() const { + return pipeline_library_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm index bcfb38c67f..7cb9098f6c 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm @@ -83,6 +83,11 @@ PipelineDescriptor::GetMTLRenderPipelineDescriptor() const { descriptor.fragmentFunction = entry.second->GetMTLFunction(); } } + + if (vertex_descriptor_) { + descriptor.vertexDescriptor = vertex_descriptor_->GetMTLVertexDescriptor(); + } + return descriptor; } diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h index e3b000a483..bf8b7eda55 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h @@ -16,6 +16,8 @@ namespace impeller { +class Context; + class PipelineLibrary : public std::enable_shared_from_this { public: ~PipelineLibrary(); @@ -24,6 +26,8 @@ class PipelineLibrary : public std::enable_shared_from_this { PipelineDescriptor descriptor); private: + friend Context; + using Pipelines = std::unordered_map, ComparableHash, diff --git a/engine/src/flutter/impeller/impeller/compositor/renderer.h b/engine/src/flutter/impeller/impeller/compositor/renderer.h index 7765af8764..3afa939242 100644 --- a/engine/src/flutter/impeller/impeller/compositor/renderer.h +++ b/engine/src/flutter/impeller/impeller/compositor/renderer.h @@ -25,6 +25,8 @@ class Renderer { bool Render(); + std::shared_ptr GetContext() const; + private: std::shared_ptr context_; std::unique_ptr surface_; diff --git a/engine/src/flutter/impeller/impeller/compositor/renderer.mm b/engine/src/flutter/impeller/impeller/compositor/renderer.mm index 8ff3f03f53..0b5cf2696b 100644 --- a/engine/src/flutter/impeller/impeller/compositor/renderer.mm +++ b/engine/src/flutter/impeller/impeller/compositor/renderer.mm @@ -49,4 +49,8 @@ bool Renderer::Render() { return surface_->Render(); } +std::shared_ptr Renderer::GetContext() const { + return context_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h index 1796ccf38b..ae785c5825 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h @@ -20,7 +20,8 @@ class VertexDescriptor final : public Comparable { virtual ~VertexDescriptor(); - bool SetStageInputs(const ShaderStageInput* stage_inputs[], size_t count); + bool SetStageInputs(const ShaderStageInput* const stage_inputs[], + size_t count); MTLVertexDescriptor* GetMTLVertexDescriptor() const; @@ -34,9 +35,10 @@ class VertexDescriptor final : public Comparable { struct StageInput { size_t location; MTLVertexFormat format; + size_t stride; - StageInput(size_t p_location, MTLVertexFormat p_format) - : location(p_location), format(p_format) {} + StageInput(size_t p_location, MTLVertexFormat p_format, size_t p_stride) + : location(p_location), format(p_format), stride(p_stride) {} constexpr bool operator==(const StageInput& other) const { return location == other.location && format == other.format; diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm index ed5fc797ef..448a3b19a3 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm @@ -20,7 +20,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { switch (input.type) { case ShaderType::kFloat: { - if (input.bit_width == sizeof(float)) { + if (input.bit_width == 8 * sizeof(float)) { switch (input.vec_size) { case 1: return MTLVertexFormatFloat; @@ -35,7 +35,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kHalfFloat: { - if (input.bit_width == sizeof(float) / 2) { + if (input.bit_width == 8 * sizeof(float) / 2) { switch (input.vec_size) { case 1: return MTLVertexFormatHalf; @@ -54,13 +54,13 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kBoolean: { - if (input.bit_width == sizeof(bool) && input.vec_size == 1) { + if (input.bit_width == 8 * sizeof(bool) && input.vec_size == 1) { return MTLVertexFormatChar; } return MTLVertexFormatInvalid; } case ShaderType::kSignedByte: { - if (input.bit_width == sizeof(char)) { + if (input.bit_width == 8 * sizeof(char)) { switch (input.vec_size) { case 1: return MTLVertexFormatChar; @@ -75,7 +75,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kUnsignedByte: { - if (input.bit_width == sizeof(char)) { + if (input.bit_width == 8 * sizeof(char)) { switch (input.vec_size) { case 1: return MTLVertexFormatUChar; @@ -90,7 +90,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kSignedShort: { - if (input.bit_width == sizeof(short)) { + if (input.bit_width == 8 * sizeof(short)) { switch (input.vec_size) { case 1: return MTLVertexFormatShort; @@ -105,7 +105,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kUnsignedShort: { - if (input.bit_width == sizeof(ushort)) { + if (input.bit_width == 8 * sizeof(ushort)) { switch (input.vec_size) { case 1: return MTLVertexFormatUShort; @@ -120,7 +120,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kSignedInt: { - if (input.bit_width == sizeof(int32_t)) { + if (input.bit_width == 8 * sizeof(int32_t)) { switch (input.vec_size) { case 1: return MTLVertexFormatInt; @@ -135,7 +135,7 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { return MTLVertexFormatInvalid; } case ShaderType::kUnsignedInt: { - if (input.bit_width == sizeof(uint32_t)) { + if (input.bit_width == 8 * sizeof(uint32_t)) { switch (input.vec_size) { case 1: return MTLVertexFormatUInt; @@ -168,8 +168,9 @@ static MTLVertexFormat ReadStageInputFormat(const ShaderStageInput& input) { } } -bool VertexDescriptor::SetStageInputs(const ShaderStageInput* stage_inputs[], - size_t count) { +bool VertexDescriptor::SetStageInputs( + const ShaderStageInput* const stage_inputs[], + size_t count) { stage_inputs_.clear(); for (size_t i = 0; i < count; i++) { @@ -179,7 +180,8 @@ bool VertexDescriptor::SetStageInputs(const ShaderStageInput* stage_inputs[], FML_LOG(ERROR) << "Format for input " << input->name << " not supported."; return false; } - stage_inputs_.emplace_back(StageInput{input->location, vertex_format}); + stage_inputs_.emplace_back( + StageInput{input->location, vertex_format, input->bit_width / 8}); } return true; @@ -188,13 +190,21 @@ bool VertexDescriptor::SetStageInputs(const ShaderStageInput* stage_inputs[], MTLVertexDescriptor* VertexDescriptor::GetMTLVertexDescriptor() const { auto descriptor = [MTLVertexDescriptor vertexDescriptor]; + size_t stride = 0u; for (const auto& input : stage_inputs_) { auto attrib = descriptor.attributes[input.location]; attrib.format = input.format; - attrib.offset = 0u; + attrib.offset = stride; + // All vertex inputs are interleaved and tightly packed in one buffer at + // zero index. attrib.bufferIndex = 0u; + stride += input.stride; } + descriptor.layouts[0].stride = stride; + descriptor.layouts[0].stepRate = 1u; + descriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex; + return descriptor; } diff --git a/engine/src/flutter/impeller/impeller/primitives/box.h b/engine/src/flutter/impeller/impeller/primitives/box.h index 08a079f08b..32b2776006 100644 --- a/engine/src/flutter/impeller/impeller/primitives/box.h +++ b/engine/src/flutter/impeller/impeller/primitives/box.h @@ -6,6 +6,6 @@ namespace impeller { -void RenderBox(std::shared_ptr context); +bool RenderBox(std::shared_ptr context); } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/primitives/box.mm b/engine/src/flutter/impeller/impeller/primitives/box.mm index e3e3fa9d7b..251b93a698 100644 --- a/engine/src/flutter/impeller/impeller/primitives/box.mm +++ b/engine/src/flutter/impeller/impeller/primitives/box.mm @@ -4,23 +4,47 @@ #include "impeller/primitives/box.h" +#include + #include "box.frag.h" #include "box.vert.h" #include "impeller/compositor/pipeline_descriptor.h" #include "impeller/compositor/shader_library.h" +#include "impeller/compositor/vertex_descriptor.h" namespace impeller { -void RenderBox(std::shared_ptr context) { - auto fragment_function = context->GetShaderLibrary()->GetFunction( - shader::BoxFragmentInfo::kEntrypointName, ShaderStage::kFragment); - auto vertex_function = context->GetShaderLibrary()->GetFunction( - shader::BoxVertexInfo::kEntrypointName, ShaderStage::kVertex); +bool RenderBox(std::shared_ptr context) { + PipelineDescriptor desc; + desc.SetLabel(shader::BoxVertexInfo::kLabel); - PipelineDescriptor builder; - builder.SetLabel(shader::BoxVertexInfo::kLabel); - builder.AddStageEntrypoint(vertex_function); - builder.AddStageEntrypoint(fragment_function); + { + auto fragment_function = context->GetShaderLibrary()->GetFunction( + shader::BoxFragmentInfo::kEntrypointName, ShaderStage::kFragment); + auto vertex_function = context->GetShaderLibrary()->GetFunction( + shader::BoxVertexInfo::kEntrypointName, ShaderStage::kVertex); + + desc.AddStageEntrypoint(vertex_function); + desc.AddStageEntrypoint(fragment_function); + } + + { + auto vertex_descriptor = std::make_shared(); + if (!vertex_descriptor->SetStageInputs( + shader::BoxVertexInfo::kAllShaderStageInputs->data(), + shader::BoxVertexInfo::kAllShaderStageInputs->size())) { + return false; + } + desc.SetVertexDescriptor(std::move(vertex_descriptor)); + } + + auto pipeline = + context->GetPipelineLibrary()->GetRenderPipeline(std::move(desc)).get(); + if (!pipeline) { + return false; + } + + return true; } } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/primitives/box.vert b/engine/src/flutter/impeller/impeller/primitives/box.vert index 0a9d4524ab..ce7ea2c38a 100644 --- a/engine/src/flutter/impeller/impeller/primitives/box.vert +++ b/engine/src/flutter/impeller/impeller/primitives/box.vert @@ -2,35 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -struct MyAwesomeStruct { - float mvp0; - mat4x2 mvp1; - mat4 mvp2; - mat4 mvp3; -}; - -uniform UniformBuffer1 { - MyAwesomeStruct s1; - MyAwesomeStruct s2; - MyAwesomeStruct s3; -} uniforms1; - -uniform UniformBuffer2 { - MyAwesomeStruct s11; - float hello12; - MyAwesomeStruct s13; -} uniforms2; +uniform UniformBuffer { + mat4 mvp; +} uniforms; in vec3 vertexPosition; -in vec3 vertexColor; -in dvec4 hello; -in dmat4x2 hello12; -in uvec4 hello2; -out vec3 color; - -void main() -{ - gl_Position = uniforms1.s1.mvp2 * uniforms2.s13.mvp3 * vec4(vertexPosition, 1.0); - color = vertexColor; +void main() { + gl_Position = uniforms.mvp * vec4(vertexPosition, 1.0); } diff --git a/engine/src/flutter/impeller/impeller/shader_glue/shader_types.h b/engine/src/flutter/impeller/impeller/shader_glue/shader_types.h index a280577540..b9c5233e0c 100644 --- a/engine/src/flutter/impeller/impeller/shader_glue/shader_types.h +++ b/engine/src/flutter/impeller/impeller/shader_glue/shader_types.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include