2016-04-06 21:42:27 +00:00
|
|
|
/*
|
2017-01-26 03:33:32 +00:00
|
|
|
* Copyright 2016-2017 The Brenwill Workshop Ltd.
|
2016-04-06 21:42:27 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-11-11 17:04:14 +00:00
|
|
|
#ifndef SPIRV_CROSS_MSL_HPP
|
|
|
|
#define SPIRV_CROSS_MSL_HPP
|
2016-04-06 21:42:27 +00:00
|
|
|
|
|
|
|
#include "spirv_glsl.hpp"
|
2017-01-29 18:28:20 +00:00
|
|
|
#include <map>
|
2016-04-06 21:42:27 +00:00
|
|
|
#include <set>
|
2016-12-19 02:42:10 +00:00
|
|
|
#include <unordered_map>
|
2016-12-18 23:48:15 +00:00
|
|
|
#include <unordered_set>
|
2016-05-23 11:30:02 +00:00
|
|
|
#include <vector>
|
2016-04-06 21:42:27 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
namespace spirv_cross
|
|
|
|
{
|
|
|
|
|
|
|
|
// Defines MSL characteristics of a vertex attribute at a particular location.
|
|
|
|
// The used_by_shader flag is set to true during compilation of SPIR-V to MSL
|
|
|
|
// if the shader makes use of this vertex attribute.
|
|
|
|
struct MSLVertexAttr
|
|
|
|
{
|
|
|
|
uint32_t location = 0;
|
2017-01-29 18:28:20 +00:00
|
|
|
uint32_t msl_buffer = 0;
|
|
|
|
uint32_t msl_offset = 0;
|
|
|
|
uint32_t msl_stride = 0;
|
|
|
|
bool per_instance = false;
|
2016-05-05 07:33:18 +00:00
|
|
|
bool used_by_shader = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Matches the binding index of a MSL resource for a binding within a descriptor set.
|
|
|
|
// Taken together, the stage, desc_set and binding combine to form a reference to a resource
|
|
|
|
// descriptor used in a particular shading stage. Generally, only one of the buffer, texture,
|
|
|
|
// or sampler elements will be populated. The used_by_shader flag is set to true during
|
|
|
|
// compilation of SPIR-V to MSL if the shader makes use of this vertex attribute.
|
|
|
|
struct MSLResourceBinding
|
|
|
|
{
|
|
|
|
spv::ExecutionModel stage;
|
|
|
|
uint32_t desc_set = 0;
|
|
|
|
uint32_t binding = 0;
|
|
|
|
|
|
|
|
uint32_t msl_buffer = 0;
|
|
|
|
uint32_t msl_texture = 0;
|
|
|
|
uint32_t msl_sampler = 0;
|
|
|
|
|
|
|
|
bool used_by_shader = false;
|
|
|
|
};
|
|
|
|
|
2017-03-01 02:44:36 +00:00
|
|
|
// Tracks the type ID and member index of a struct member
|
2017-03-11 17:17:22 +00:00
|
|
|
using MSLStructMemberKey = uint64_t;
|
2017-03-01 02:44:36 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
// Special constant used in a MSLResourceBinding desc_set
|
|
|
|
// element to indicate the bindings for the push constants.
|
|
|
|
static const uint32_t kPushConstDescSet = UINT32_MAX;
|
|
|
|
|
|
|
|
// Special constant used in a MSLResourceBinding binding
|
|
|
|
// element to indicate the bindings for the push constants.
|
|
|
|
static const uint32_t kPushConstBinding = 0;
|
|
|
|
|
|
|
|
// Decompiles SPIR-V to Metal Shading Language
|
|
|
|
class CompilerMSL : public CompilerGLSL
|
|
|
|
{
|
|
|
|
public:
|
2017-03-12 21:42:51 +00:00
|
|
|
// Options for compiling to Metal Shading Language
|
|
|
|
struct Options
|
|
|
|
{
|
|
|
|
bool flip_vert_y = false;
|
2017-05-05 20:13:55 +00:00
|
|
|
bool is_rendering_points = true;
|
2017-03-12 21:42:51 +00:00
|
|
|
bool pad_and_pack_uniform_structs = false;
|
|
|
|
std::string entry_point_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Options &get_options() const
|
|
|
|
{
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_options(Options &opts)
|
|
|
|
{
|
|
|
|
options = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs an instance to compile the SPIR-V code into Metal Shading Language,
|
|
|
|
// using the configuration parameters, if provided:
|
2016-05-05 07:33:18 +00:00
|
|
|
// - p_vtx_attrs is an optional list of vertex attribute bindings used to match
|
|
|
|
// vertex content locations to MSL attributes. If vertex attributes are provided,
|
|
|
|
// the compiler will set the used_by_shader flag to true in any vertex attribute
|
|
|
|
// actually used by the MSL code.
|
|
|
|
// - p_res_bindings is a list of resource bindings to indicate the MSL buffer,
|
|
|
|
// texture or sampler index to use for a particular SPIR-V description set
|
|
|
|
// and binding. If resource bindings are provided, the compiler will set the
|
|
|
|
// used_by_shader flag to true in any resource binding actually used by the MSL code.
|
2017-03-12 21:42:51 +00:00
|
|
|
CompilerMSL(std::vector<uint32_t> spirv, std::vector<MSLVertexAttr> *p_vtx_attrs = nullptr,
|
|
|
|
std::vector<MSLResourceBinding> *p_res_bindings = nullptr);
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2017-04-04 20:38:17 +00:00
|
|
|
// Alternate constructor avoiding use of std::vectors.
|
|
|
|
CompilerMSL(const uint32_t *ir, size_t word_count, MSLVertexAttr *p_vtx_attrs = nullptr, size_t vtx_attrs_count = 0,
|
|
|
|
MSLResourceBinding *p_res_bindings = nullptr, size_t res_bindings_count = 0);
|
2017-04-01 14:08:19 +00:00
|
|
|
|
2017-03-12 21:42:51 +00:00
|
|
|
// Compiles the SPIR-V code into Metal Shading Language.
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string compile() override;
|
2017-01-15 15:39:03 +00:00
|
|
|
|
2017-03-12 21:42:51 +00:00
|
|
|
// Compiles the SPIR-V code into Metal Shading Language, overriding configuration parameters.
|
|
|
|
// Any of the parameters here may be null to indicate that the configuration provided in the
|
|
|
|
// constructor should be used. They are not declared as optional to avoid a conflict with the
|
|
|
|
// inherited and overridden zero-parameter compile() function.
|
|
|
|
std::string compile(std::vector<MSLVertexAttr> *p_vtx_attrs, std::vector<MSLResourceBinding> *p_res_bindings);
|
|
|
|
|
|
|
|
// This legacy method is deprecated.
|
2017-03-20 01:06:21 +00:00
|
|
|
typedef Options MSLConfiguration;
|
2017-03-22 15:15:08 +00:00
|
|
|
SPIRV_CROSS_DEPRECATED("Please use get_options() and set_options() instead.")
|
2017-03-12 21:42:51 +00:00
|
|
|
std::string compile(MSLConfiguration &msl_cfg, std::vector<MSLVertexAttr> *p_vtx_attrs = nullptr,
|
|
|
|
std::vector<MSLResourceBinding> *p_res_bindings = nullptr);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
protected:
|
2016-10-27 22:47:17 +00:00
|
|
|
void emit_instruction(const Instruction &instr) override;
|
2016-11-12 09:04:50 +00:00
|
|
|
void emit_glsl_op(uint32_t result_type, uint32_t result_id, uint32_t op, const uint32_t *args,
|
|
|
|
uint32_t count) override;
|
2016-05-05 07:33:18 +00:00
|
|
|
void emit_header() override;
|
|
|
|
void emit_function_prototype(SPIRFunction &func, uint64_t return_flags) override;
|
|
|
|
void emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) override;
|
|
|
|
void emit_fixup() override;
|
2017-03-11 17:17:22 +00:00
|
|
|
void emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index,
|
|
|
|
const std::string &qualifier = "") override;
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string type_to_glsl(const SPIRType &type) override;
|
|
|
|
std::string image_type_glsl(const SPIRType &type) override;
|
|
|
|
std::string builtin_to_glsl(spv::BuiltIn builtin) override;
|
|
|
|
std::string constant_expression(const SPIRConstant &c) override;
|
|
|
|
size_t get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const override;
|
2016-10-24 13:24:24 +00:00
|
|
|
std::string to_func_call_arg(uint32_t id) override;
|
2017-03-07 12:27:04 +00:00
|
|
|
std::string to_name(uint32_t id, bool allow_alias = true) const override;
|
2016-12-28 23:36:42 +00:00
|
|
|
std::string to_function_name(uint32_t img, const SPIRType &imgtype, bool is_fetch, bool is_gather, bool is_proj,
|
2017-03-24 08:26:02 +00:00
|
|
|
bool has_array_offsets, bool has_offset, bool has_grad, bool has_dref,
|
|
|
|
uint32_t lod) override;
|
2016-12-28 23:36:42 +00:00
|
|
|
std::string to_function_args(uint32_t img, const SPIRType &imgtype, bool is_fetch, bool is_gather, bool is_proj,
|
|
|
|
uint32_t coord, uint32_t coord_components, uint32_t dref, uint32_t grad_x,
|
|
|
|
uint32_t grad_y, uint32_t lod, uint32_t coffset, uint32_t offset, uint32_t bias,
|
|
|
|
uint32_t comp, uint32_t sample, bool *p_forward) override;
|
2017-03-01 18:07:40 +00:00
|
|
|
std::string unpack_expression_type(std::string expr_str, const SPIRType &type) override;
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2017-01-27 00:40:56 +00:00
|
|
|
std::string get_argument_address_space(const SPIRVariable &argument);
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2017-01-20 16:24:44 +00:00
|
|
|
void preprocess_op_codes();
|
2016-12-19 02:42:10 +00:00
|
|
|
void emit_custom_functions();
|
2016-05-05 07:33:18 +00:00
|
|
|
void localize_global_variables();
|
2016-10-24 13:24:24 +00:00
|
|
|
void extract_global_variables_from_functions();
|
2017-01-15 15:39:03 +00:00
|
|
|
|
2017-01-31 16:02:44 +00:00
|
|
|
std::unordered_map<uint32_t, std::set<uint32_t>> function_global_vars;
|
|
|
|
void extract_global_variables_from_function(uint32_t func_id, std::set<uint32_t> &added_arg_ids,
|
2016-12-18 23:48:15 +00:00
|
|
|
std::unordered_set<uint32_t> &global_var_ids,
|
|
|
|
std::unordered_set<uint32_t> &processed_func_ids);
|
2017-01-01 19:43:20 +00:00
|
|
|
uint32_t add_interface_block(spv::StorageClass storage);
|
|
|
|
void mark_location_as_used_by_shader(uint32_t location, spv::StorageClass storage);
|
2017-01-15 15:39:03 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
void emit_resources();
|
|
|
|
void emit_interface_block(uint32_t ib_var_id);
|
2017-01-08 13:52:57 +00:00
|
|
|
void populate_func_name_overrides();
|
2017-01-23 09:00:40 +00:00
|
|
|
void populate_var_name_overrides();
|
2016-05-05 07:33:18 +00:00
|
|
|
|
|
|
|
std::string func_type_decl(SPIRType &type);
|
2017-01-15 15:14:21 +00:00
|
|
|
std::string clean_func_name(std::string func_name) override;
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string entry_point_args(bool append_comma);
|
|
|
|
std::string get_entry_point_name();
|
2016-10-24 13:24:24 +00:00
|
|
|
std::string to_qualified_member_name(const SPIRType &type, uint32_t index);
|
2016-11-27 20:00:06 +00:00
|
|
|
std::string ensure_valid_name(std::string name, std::string pfx);
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string to_sampler_expression(uint32_t id);
|
|
|
|
std::string builtin_qualifier(spv::BuiltIn builtin);
|
|
|
|
std::string builtin_type_decl(spv::BuiltIn builtin);
|
2017-01-29 18:28:20 +00:00
|
|
|
std::string built_in_func_arg(spv::BuiltIn builtin, bool prefix_comma);
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string member_attribute_qualifier(const SPIRType &type, uint32_t index);
|
|
|
|
std::string argument_decl(const SPIRFunction::Parameter &arg);
|
2017-04-25 20:32:16 +00:00
|
|
|
std::string round_fp_tex_coords(std::string tex_coords, bool coord_is_fp);
|
2016-05-05 07:33:18 +00:00
|
|
|
uint32_t get_metal_resource_index(SPIRVariable &var, SPIRType::BaseType basetype);
|
|
|
|
uint32_t get_ordered_member_location(uint32_t type_id, uint32_t index);
|
2016-12-17 22:07:53 +00:00
|
|
|
size_t get_declared_type_size(uint32_t type_id) const;
|
|
|
|
size_t get_declared_type_size(uint32_t type_id, uint64_t dec_mask) const;
|
2017-03-06 16:00:23 +00:00
|
|
|
size_t get_declared_struct_member_alignment(const SPIRType &struct_type, uint32_t index) const;
|
|
|
|
size_t get_declared_type_alignment(uint32_t type_id, uint64_t dec_mask) const;
|
2016-12-28 23:36:42 +00:00
|
|
|
std::string to_component_argument(uint32_t id);
|
2017-01-29 18:28:20 +00:00
|
|
|
void exclude_from_stage_in(SPIRVariable &var);
|
|
|
|
void exclude_member_from_stage_in(const SPIRType &type, uint32_t index);
|
|
|
|
std::string add_input_buffer_block_member(uint32_t mbr_type_id, std::string mbr_name, uint32_t mbr_locn);
|
|
|
|
uint32_t get_input_buffer_block_var_id(uint32_t msl_buffer);
|
2017-03-01 02:44:36 +00:00
|
|
|
void align_struct(SPIRType &ib_type);
|
2017-03-03 01:57:26 +00:00
|
|
|
bool is_member_packable(SPIRType &ib_type, uint32_t index);
|
2017-03-01 02:44:36 +00:00
|
|
|
MSLStructMemberKey get_struct_member_key(uint32_t type_id, uint32_t index);
|
2017-05-07 10:36:14 +00:00
|
|
|
bool skip_argument(uint32_t id) const override;
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2017-03-12 21:42:51 +00:00
|
|
|
Options options;
|
2017-01-08 13:52:57 +00:00
|
|
|
std::unordered_map<std::string, std::string> func_name_overrides;
|
2017-01-23 09:00:40 +00:00
|
|
|
std::unordered_map<std::string, std::string> var_name_overrides;
|
2016-12-21 21:31:13 +00:00
|
|
|
std::set<uint32_t> custom_function_ops;
|
2016-05-05 07:33:18 +00:00
|
|
|
std::unordered_map<uint32_t, MSLVertexAttr *> vtx_attrs_by_location;
|
2017-01-29 18:28:20 +00:00
|
|
|
std::map<uint32_t, uint32_t> non_stage_in_input_var_ids;
|
2017-03-01 02:44:36 +00:00
|
|
|
std::unordered_map<MSLStructMemberKey, uint32_t> struct_member_padding;
|
2016-05-05 07:33:18 +00:00
|
|
|
std::vector<MSLResourceBinding *> resource_bindings;
|
|
|
|
MSLResourceBinding next_metal_resource_index;
|
2017-01-01 19:43:20 +00:00
|
|
|
uint32_t stage_in_var_id = 0;
|
2016-05-05 07:33:18 +00:00
|
|
|
uint32_t stage_out_var_id = 0;
|
2017-01-15 15:14:21 +00:00
|
|
|
uint32_t stage_uniforms_var_id = 0;
|
2017-01-29 18:28:20 +00:00
|
|
|
bool needs_vertex_idx_arg = false;
|
|
|
|
bool needs_instance_idx_arg = false;
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string qual_pos_var_name;
|
|
|
|
std::string stage_in_var_name = "in";
|
|
|
|
std::string stage_out_var_name = "out";
|
2017-01-15 15:14:21 +00:00
|
|
|
std::string stage_uniform_var_name = "uniforms";
|
2016-05-05 07:33:18 +00:00
|
|
|
std::string sampler_name_suffix = "Smplr";
|
|
|
|
|
2017-01-20 16:24:44 +00:00
|
|
|
// OpcodeHandler that handles several MSL preprocessing operations.
|
2017-01-20 16:33:59 +00:00
|
|
|
struct OpCodePreprocessor : OpcodeHandler
|
2016-12-04 17:32:58 +00:00
|
|
|
{
|
2017-01-20 16:33:59 +00:00
|
|
|
OpCodePreprocessor(CompilerMSL &compiler_)
|
2016-12-21 21:31:13 +00:00
|
|
|
: compiler(compiler_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override;
|
|
|
|
|
2017-01-20 16:33:59 +00:00
|
|
|
CompilerMSL &compiler;
|
|
|
|
bool suppress_missing_prototypes = false;
|
2016-12-04 17:32:58 +00:00
|
|
|
};
|
|
|
|
|
2016-12-21 21:31:13 +00:00
|
|
|
// Sorts the members of a SPIRType and associated Meta info based on a settable sorting
|
|
|
|
// aspect, which defines which aspect of the struct members will be used to sort them.
|
|
|
|
// Regardless of the sorting aspect, built-in members always appear at the end of the struct.
|
|
|
|
struct MemberSorter
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
2016-12-21 21:31:13 +00:00
|
|
|
enum SortAspect
|
|
|
|
{
|
|
|
|
Location,
|
|
|
|
LocationReverse,
|
|
|
|
Offset,
|
|
|
|
OffsetThenLocationReverse,
|
2017-01-15 15:14:21 +00:00
|
|
|
Alphabetical
|
2016-12-21 21:31:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void sort();
|
|
|
|
bool operator()(uint32_t mbr_idx1, uint32_t mbr_idx2);
|
2017-03-01 02:44:36 +00:00
|
|
|
MemberSorter(SPIRType &t, Meta &m, SortAspect sa);
|
|
|
|
|
2016-12-21 21:31:13 +00:00
|
|
|
SPIRType &type;
|
|
|
|
Meta &meta;
|
|
|
|
SortAspect sort_aspect;
|
|
|
|
};
|
2016-05-05 07:33:18 +00:00
|
|
|
};
|
2016-04-06 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|