// Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #pragma once #include #include #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "impeller/compiler/compiler_backend.h" #include "impeller/compiler/runtime_stage_data.h" #include "inja/inja.hpp" #include "spirv_msl.hpp" #include "spirv_parser.hpp" namespace impeller { namespace compiler { struct StructMember { std::string type; std::string base_type; std::string name; size_t offset = 0u; size_t size = 0u; size_t byte_length = 0u; std::optional array_elements = std::nullopt; size_t element_padding = 0u; StructMember(std::string p_type, std::string p_base_type, std::string p_name, size_t p_offset, size_t p_size, size_t p_byte_length, std::optional p_array_elements, size_t p_element_padding) : type(std::move(p_type)), base_type(std::move(p_base_type)), name(std::move(p_name)), offset(p_offset), size(p_size), byte_length(p_byte_length), array_elements(p_array_elements), element_padding(p_element_padding) {} }; class Reflector { public: struct Options { TargetPlatform target_platform = TargetPlatform::kUnknown; std::string entry_point_name; std::string shader_name; std::string header_file_name; }; Reflector(Options options, std::shared_ptr ir, std::shared_ptr shader_data, CompilerBackend compiler); ~Reflector(); bool IsValid() const; std::shared_ptr GetReflectionJSON() const; std::shared_ptr GetReflectionHeader() const; std::shared_ptr GetReflectionCC() const; std::shared_ptr GetRuntimeStageData() const; private: struct StructDefinition { std::string name; size_t byte_length = 0u; std::vector members; }; struct BindPrototypeArgument { std::string type_name; std::string argument_name; }; struct BindPrototype { std::string name; std::string return_type; std::string docstring; std::vector args; }; const Options options_; const std::shared_ptr ir_; const std::shared_ptr shader_data_; const std::shared_ptr sksl_data_; const CompilerBackend compiler_; std::unique_ptr template_arguments_; std::shared_ptr reflection_header_; std::shared_ptr reflection_cc_; std::shared_ptr runtime_stage_data_; bool is_valid_ = false; std::optional GenerateTemplateArguments() const; std::shared_ptr GenerateReflectionHeader() const; std::shared_ptr GenerateReflectionCC() const; std::shared_ptr GenerateRuntimeStageData() const; std::shared_ptr InflateTemplate(std::string_view tmpl) const; std::optional ReflectResource( const spirv_cross::Resource& resource, std::optional offset) const; std::optional ReflectResources( const spirv_cross::SmallVector& resources, bool compute_offsets = false) const; std::vector ComputeOffsets( const spirv_cross::SmallVector& resources) const; std::optional ReflectType( const spirv_cross::TypeID& type_id) const; nlohmann::json::object_t EmitStructDefinition( std::optional struc) const; std::optional ReflectStructDefinition( const spirv_cross::TypeID& type_id) const; std::vector ReflectBindPrototypes( const spirv_cross::ShaderResources& resources, spv::ExecutionModel execution_model) const; nlohmann::json::array_t EmitBindPrototypes( const spirv_cross::ShaderResources& resources, spv::ExecutionModel execution_model) const; std::optional ReflectPerVertexStructDefinition( const spirv_cross::SmallVector& stage_inputs) const; std::optional GetMemberNameAtIndexIfExists( const spirv_cross::SPIRType& parent_type, size_t index) const; std::string GetMemberNameAtIndex(const spirv_cross::SPIRType& parent_type, size_t index, std::string suffix = "") const; std::vector ReadStructMembers( const spirv_cross::TypeID& type_id) const; std::optional GetArrayElements( const spirv_cross::SPIRType& type) const; template uint32_t GetArrayStride(const spirv_cross::SPIRType& struct_type, const spirv_cross::SPIRType& member_type, uint32_t index) const { auto element_count = GetArrayElements(member_type).value_or(1); if (element_count <= 1) { return Size; } return compiler_->type_struct_member_array_stride(struct_type, index); }; FML_DISALLOW_COPY_AND_ASSIGN(Reflector); }; } // namespace compiler } // namespace impeller