Generate code for simple named binders.

This commit is contained in:
Chinmay Garde
2021-07-11 17:49:59 -07:00
committed by Dan Field
parent 593080dd03
commit ef4981b4d5
10 changed files with 231 additions and 90 deletions

View File

@@ -13,7 +13,13 @@ constexpr std::string_view kReflectionHeaderTemplate =
#pragma once
#include "shader_types.h"
// Note: The nogncheck decorations are only to make GN not mad at the template.
// There are no GN rule violations in the generated file itself.
#include "impeller/compositor/buffer_view.h" // nogncheck
#include "impeller/compositor/command.h" // nogncheck
#include "impeller/compositor/sampler.h" // nogncheck
#include "impeller/compositor/texture.h" // nogncheck
#include "impeller/shader_glue/shader_types.h" // nogncheck
namespace impeller {
@@ -43,7 +49,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader {
// ===========================================================================
{% for uniform in uniform_buffers %}
static constexpr auto kUniform{{camel_case(uniform.name)}} = ShaderUniformSlot<{{uniform.name}}> { // {{uniform.name}}
static constexpr auto kResource{{camel_case(uniform.name)}} = ShaderUniformSlot<{{uniform.name}}> { // {{uniform.name}}
"{{uniform.name}}", // name
{{uniform.binding}}u, // binding
};
@@ -80,7 +86,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader {
// ===========================================================================
{% for sampled_image in sampled_images %}
static constexpr auto kSampledImage{{camel_case(sampled_image.name)}} = SampledImageSlot { // {{sampled_image.name}}
static constexpr auto kResource{{camel_case(sampled_image.name)}} = SampledImageSlot { // {{sampled_image.name}}
"{{sampled_image.name}}", // name
{{sampled_image.msl_res_0}}u, // texture
{{sampled_image.msl_res_1}}u, // sampler
@@ -110,6 +116,21 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader {
};
{% endif %}
{% for proto in bind_prototypes %}
/// {{proto.docstring}}
static {{proto.return_type}} Bind{{proto.name}}({% for arg in proto.args %}
{{arg.type_name}} {{arg.argument_name}}{% if not loop.is_last %}, {% endif %}
{% 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 }}, {% else %}
std::move({{ arg.argument_name }}){% if not loop.is_last %}, {% endif %}
{% endif %}
{% endfor %});
}
{% endfor %}
}; // struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Shader
} // namespace impeller

View File

@@ -239,7 +239,7 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
});
}
root["bind_prototype"] = EmitBindPrototypes(shader_resources);
root["bind_prototypes"] = EmitBindPrototypes(shader_resources);
return root;
}
@@ -661,11 +661,29 @@ std::string Reflector::GetMemberNameAtIndex(
std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
const spirv_cross::ShaderResources& resources) const {
std::vector<BindPrototype> prototypes;
for (const auto& uniform_buffer : resources.uniform_buffers) {
auto& proto = prototypes.emplace_back(BindPrototype{});
proto.return_type = "bool";
proto.name = ConvertToCamelCase(uniform_buffer.name);
{
std::stringstream stream;
stream << "Bind uniform buffer for resource named " << uniform_buffer.name
<< ".";
proto.docstring = stream.str();
}
proto.args.push_back(BindPrototypeArgument{
.type_name = "Command&",
.argument_name = "command",
});
proto.args.push_back(BindPrototypeArgument{
.type_name = "BufferView",
.argument_name = "view",
});
}
for (const auto& sampled_image : resources.sampled_images) {
auto& proto = prototypes.emplace_back(BindPrototype{});
proto.return_type = "void";
proto.name =
SPrintF("Bind%s", ConvertToCamelCase(sampled_image.name).c_str());
proto.return_type = "bool";
proto.name = ConvertToCamelCase(sampled_image.name);
{
std::stringstream stream;
stream << "Bind combined image sampler for resource named "
@@ -677,11 +695,49 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
.argument_name = "command",
});
proto.args.push_back(BindPrototypeArgument{
.type_name = "Texture&",
.type_name = "std::shared_ptr<const Texture>",
.argument_name = "texture",
});
proto.args.push_back(BindPrototypeArgument{
.type_name = "Sampler&",
.type_name = "std::shared_ptr<const Sampler>",
.argument_name = "sampler",
});
}
for (const auto& separate_image : resources.separate_images) {
auto& proto = prototypes.emplace_back(BindPrototype{});
proto.return_type = "bool";
proto.name = ConvertToCamelCase(separate_image.name);
{
std::stringstream stream;
stream << "Bind separate image for resource named " << separate_image.name
<< ".";
proto.docstring = stream.str();
}
proto.args.push_back(BindPrototypeArgument{
.type_name = "Command&",
.argument_name = "command",
});
proto.args.push_back(BindPrototypeArgument{
.type_name = "std::shared_ptr<const Texture>",
.argument_name = "texture",
});
}
for (const auto& separate_sampler : resources.separate_samplers) {
auto& proto = prototypes.emplace_back(BindPrototype{});
proto.return_type = "bool";
proto.name = ConvertToCamelCase(separate_sampler.name);
{
std::stringstream stream;
stream << "Bind separate sampler for resource named "
<< separate_sampler.name << ".";
proto.docstring = stream.str();
}
proto.args.push_back(BindPrototypeArgument{
.type_name = "Command&",
.argument_name = "command",
});
proto.args.push_back(BindPrototypeArgument{
.type_name = "std::shared_ptr<const Sampler>",
.argument_name = "sampler",
});
}

View File

@@ -117,8 +117,6 @@ class Reflector {
std::vector<StructMember> ReadStructMembers(
const spirv_cross::TypeID& type_id) const;
bool IsVertexShader() const;
FML_DISALLOW_COPY_AND_ASSIGN(Reflector);
};

View File

@@ -8,16 +8,18 @@
#include <memory>
#include <string>
#include "flutter/fml/logging.h"
#include "flutter/fml/macros.h"
#include "impeller/compositor/buffer_view.h"
#include "impeller/compositor/formats.h"
#include "impeller/compositor/pipeline.h"
#include "impeller/compositor/sampler.h"
#include "impeller/compositor/texture.h"
#include "impeller/compositor/vertex_buffer.h"
#include "impeller/shader_glue/shader_types.h"
namespace impeller {
class Texture;
class Sampler;
struct Bindings {
std::map<size_t, BufferView> buffers;
std::map<size_t, std::shared_ptr<const Texture>> textures;
@@ -33,6 +35,30 @@ struct Command {
std::string label;
PrimitiveType primitive_type;
bool BindVertices(const VertexBuffer& buffer);
template <class T>
bool BindResource(ShaderStage stage,
const ShaderUniformSlot<T> slot,
BufferView view) {
return BindResource(stage, slot.binding, std::move(view));
}
bool BindResource(ShaderStage stage, size_t binding, BufferView view);
bool BindResource(ShaderStage stage,
const SampledImageSlot& slot,
std::shared_ptr<const Texture> texture);
bool BindResource(ShaderStage stage,
const SampledImageSlot& slot,
std::shared_ptr<const Sampler> sampler);
bool BindResource(ShaderStage stage,
const SampledImageSlot& slot,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler);
constexpr operator bool() const { return pipeline && pipeline->IsValid(); }
};

View File

@@ -6,8 +6,97 @@
#include <Metal/Metal.h>
#include "impeller/compositor/vertex_descriptor.h"
namespace impeller {
//
bool Command::BindVertices(const VertexBuffer& buffer) {
vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] =
buffer.vertex_buffer;
index_buffer = buffer.index_buffer;
index_count = buffer.index_count;
return true;
}
bool Command::BindResource(ShaderStage stage, size_t binding, BufferView view) {
if (!view) {
return false;
}
switch (stage) {
case ShaderStage::kVertex:
vertex_bindings.buffers[binding] = view;
return true;
case ShaderStage::kFragment:
fragment_bindings.buffers[binding] = view;
return true;
case ShaderStage::kUnknown:
return false;
}
return false;
}
bool Command::BindResource(ShaderStage stage,
const SampledImageSlot& slot,
std::shared_ptr<const Texture> texture) {
if (!texture || !texture->IsValid()) {
return false;
}
if (!slot.HasTexture()) {
FML_DLOG(WARNING) << "Texture named " << slot.name
<< " is not actually used in the shader.";
return true;
}
switch (stage) {
case ShaderStage::kVertex:
vertex_bindings.textures[slot.texture_index] = texture;
return true;
case ShaderStage::kFragment:
fragment_bindings.textures[slot.texture_index] = texture;
return true;
case ShaderStage::kUnknown:
return false;
}
return false;
}
bool Command::BindResource(ShaderStage stage,
const SampledImageSlot& slot,
std::shared_ptr<const Sampler> sampler) {
if (!sampler || !sampler->IsValid()) {
return false;
}
if (!slot.HasSampler()) {
FML_DLOG(WARNING) << "Sampler named " << slot.name
<< " is not actually used in the shader.";
return true;
}
switch (stage) {
case ShaderStage::kVertex:
vertex_bindings.samplers[slot.sampler_index] = sampler;
return true;
case ShaderStage::kFragment:
fragment_bindings.samplers[slot.sampler_index] = sampler;
return true;
case ShaderStage::kUnknown:
return false;
}
return false;
}
bool Command::BindResource(ShaderStage stage,
const SampledImageSlot& slot,
std::shared_ptr<const Texture> texture,
std::shared_ptr<const Sampler> sampler) {
return BindResource(stage, slot, texture) &&
BindResource(stage, slot, sampler);
}
} // namespace impeller

View File

@@ -37,9 +37,6 @@ struct TextureDescriptor {
mip_count >= 1u //
;
}
static std::optional<TextureDescriptor> MakeFromImageResult(
const Image& result);
};
} // namespace impeller

View File

@@ -6,44 +6,6 @@
namespace impeller {
std::optional<PixelFormat> FormatForImageResultComponents(Image::Format comp) {
switch (comp) {
case Image::Format::Invalid:
return std::nullopt;
case Image::Format::Grey:
return std::nullopt;
case Image::Format::GreyAlpha:
return std::nullopt;
case Image::Format::RGB:
return std::nullopt;
case Image::Format::RGBA:
return PixelFormat::kPixelFormat_R8G8B8A8_UNormInt;
}
return std::nullopt;
}
std::optional<TextureDescriptor> TextureDescriptor::MakeFromImageResult(
const Image& result) {
if (!result.IsValid()) {
return std::nullopt;
}
const auto pixel_format = FormatForImageResultComponents(result.GetFormat());
if (!pixel_format.has_value()) {
FML_DLOG(ERROR) << "Unknown image format.";
return std::nullopt;
}
TextureDescriptor desc;
desc.format = pixel_format.value();
desc.size = result.GetSize();
desc.mip_count = result.GetSize().MipCount();
if (!desc.IsValid()) {
return std::nullopt;
}
return desc;
}
//
} // namespace impeller

View File

@@ -169,20 +169,25 @@ std::shared_ptr<Texture> Playground::CreateTextureForFixture(
const char* fixture_name) const {
CompressedImage compressed_image(
flutter::testing::OpenFixtureAsMapping(fixture_name));
// The decoded image is immediately converted into RGBA as that format is
// known to be supported everywhere. For image sources that don't need 32 bit
// pixel strides, this is overkill. Since this is a test fixture we aren't
// necessarily trying to eke out memory savings here and instead favor
// simplicity.
auto image = compressed_image.Decode().ConvertToRGBA();
if (!image.IsValid()) {
FML_LOG(ERROR) << "Could not find fixture named " << fixture_name;
return nullptr;
}
auto texture_descriptor = TextureDescriptor::MakeFromImageResult(image);
if (!texture_descriptor.has_value()) {
FML_LOG(ERROR) << "Could not figure out texture descriptor for fixture "
<< fixture_name;
return nullptr;
}
auto texture_descriptor = TextureDescriptor{};
texture_descriptor.format = PixelFormat::kPixelFormat_R8G8B8A8_UNormInt;
texture_descriptor.size = image.GetSize();
texture_descriptor.mip_count = 1u;
auto texture =
renderer_.GetContext()->GetPermanentsAllocator()->CreateTexture(
StorageMode::kHostVisible, texture_descriptor.value());
StorageMode::kHostVisible, texture_descriptor);
if (!texture) {
FML_LOG(ERROR) << "Could not allocate texture for fixture " << fixture_name;
return nullptr;

View File

@@ -54,18 +54,17 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
ASSERT_TRUE(sampler);
Renderer::RenderCallback callback = [&](const Surface& surface,
RenderPass& pass) {
BoxVertexShader::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize().width,
surface.GetSize().height);
Command cmd;
cmd.label = "Box";
cmd.pipeline = box_pipeline;
cmd.vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] =
vertex_buffer.vertex_buffer;
cmd.vertex_bindings
.buffers[BoxVertexShader::kUniformUniformBuffer.binding] =
pass.GetTransientsBuffer().EmplaceUniform(uniforms);
cmd.BindVertices(vertex_buffer);
BoxVertexShader::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize().width,
surface.GetSize().height);
BoxVertexShader::BindUniformBuffer(
cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms));
BoxFragmentShader::FrameInfo frame_info;
frame_info.current_time = fml::TimePoint::Now().ToEpochDelta().ToSecondsF();
@@ -73,24 +72,11 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
frame_info.window_size.x = GetWindowSize().width;
frame_info.window_size.y = GetWindowSize().height;
cmd.fragment_bindings
.buffers[BoxFragmentShader::kUniformFrameInfo.binding] =
pass.GetTransientsBuffer().EmplaceUniform(frame_info);
cmd.fragment_bindings
.textures[BoxFragmentShader::kSampledImageContents1Texture
.texture_index] = bridge;
cmd.fragment_bindings
.samplers[BoxFragmentShader::kSampledImageContents1Texture
.sampler_index] = sampler;
cmd.fragment_bindings
.textures[BoxFragmentShader::kSampledImageContents2Texture
.texture_index] = boston;
cmd.fragment_bindings
.samplers[BoxFragmentShader::kSampledImageContents2Texture
.sampler_index] = sampler;
BoxFragmentShader::BindFrameInfo(
cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
BoxFragmentShader::BindContents1Texture(cmd, boston, sampler);
BoxFragmentShader::BindContents2Texture(cmd, bridge, sampler);
cmd.index_buffer = vertex_buffer.index_buffer;
cmd.index_count = vertex_buffer.index_count;
cmd.primitive_type = PrimitiveType::kTriangle;
if (!pass.RecordCommand(std::move(cmd))) {
return false;

View File

@@ -98,7 +98,7 @@ template("impeller_shaders") {
spirv_intermediate = "$target_gen_dir/{{source_file_part}}.spirv"
reflection_json_intermediate = "$target_gen_dir/{{source_file_part}}.json"
reflection_header_intermediate = "$target_gen_dir/{{source_file_part}}.h"
reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.cc"
reflection_cc_intermediate = "$target_gen_dir/{{source_file_part}}.mm"
outputs = [
metal_intermediate,
@@ -164,6 +164,7 @@ template("impeller_shaders") {
[
"*.h",
"*.cc",
"*.mm",
])
deps = [