Setup vertex attributes.

This commit is contained in:
Chinmay Garde
2021-05-24 21:36:42 -07:00
committed by Dan Field
parent 956973b17b
commit 9793900968
13 changed files with 101 additions and 57 deletions

View File

@@ -34,9 +34,9 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info {
};
{% endfor %}
static constexpr const ShaderStageInput* kAllShaderStageInputs[] = {
static constexpr std::array<const ShaderStageInput*, {{length(stage_inputs)}}> 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 %}
};

View File

@@ -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::Renderer>(
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;
}

View File

@@ -47,6 +47,12 @@ Context::Context(std::string shaders_directory)
std::shared_ptr<ShaderLibrary>(new ShaderLibrary(library));
}
// Setup the pipeline library.
{ //
pipeline_library_ =
std::shared_ptr<PipelineLibrary>(new PipelineLibrary(device_));
}
is_valid_ = true;
}
@@ -68,4 +74,8 @@ std::shared_ptr<ShaderLibrary> Context::GetShaderLibrary() const {
return shader_library_;
}
std::shared_ptr<PipelineLibrary> Context::GetPipelineLibrary() const {
return pipeline_library_;
}
} // namespace impeller

View File

@@ -83,6 +83,11 @@ PipelineDescriptor::GetMTLRenderPipelineDescriptor() const {
descriptor.fragmentFunction = entry.second->GetMTLFunction();
}
}
if (vertex_descriptor_) {
descriptor.vertexDescriptor = vertex_descriptor_->GetMTLVertexDescriptor();
}
return descriptor;
}

View File

@@ -16,6 +16,8 @@
namespace impeller {
class Context;
class PipelineLibrary : public std::enable_shared_from_this<PipelineLibrary> {
public:
~PipelineLibrary();
@@ -24,6 +26,8 @@ class PipelineLibrary : public std::enable_shared_from_this<PipelineLibrary> {
PipelineDescriptor descriptor);
private:
friend Context;
using Pipelines = std::unordered_map<PipelineDescriptor,
std::shared_ptr<const Pipeline>,
ComparableHash<PipelineDescriptor>,

View File

@@ -25,6 +25,8 @@ class Renderer {
bool Render();
std::shared_ptr<Context> GetContext() const;
private:
std::shared_ptr<Context> context_;
std::unique_ptr<Surface> surface_;

View File

@@ -49,4 +49,8 @@ bool Renderer::Render() {
return surface_->Render();
}
std::shared_ptr<Context> Renderer::GetContext() const {
return context_;
}
} // namespace impeller

View File

@@ -20,7 +20,8 @@ class VertexDescriptor final : public Comparable<VertexDescriptor> {
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<VertexDescriptor> {
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;

View File

@@ -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;
}

View File

@@ -6,6 +6,6 @@
namespace impeller {
void RenderBox(std::shared_ptr<Context> context);
bool RenderBox(std::shared_ptr<Context> context);
} // namespace impeller

View File

@@ -4,23 +4,47 @@
#include "impeller/primitives/box.h"
#include <memory>
#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> 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> 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<VertexDescriptor>();
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

View File

@@ -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);
}

View File

@@ -4,6 +4,7 @@
#pragma once
#include <array>
#include <cstddef>
#include <string_view>