1 /* 2 * Copyright 2016-2019 Robert Konrad 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SPIRV_HLSL_HPP 18 #define SPIRV_HLSL_HPP 19 20 #include "spirv_glsl.hpp" 21 #include <utility> 22 23 namespace SPIRV_CROSS_NAMESPACE 24 { 25 // Interface which remaps vertex inputs to a fixed semantic name to make linking easier. 26 struct HLSLVertexAttributeRemap 27 { 28 uint32_t location; 29 std::string semantic; 30 }; 31 // Specifying a root constant (d3d12) or push constant range (vulkan). 32 // 33 // `start` and `end` denotes the range of the root constant in bytes. 34 // Both values need to be multiple of 4. 35 struct RootConstants 36 { 37 uint32_t start; 38 uint32_t end; 39 40 uint32_t binding; 41 uint32_t space; 42 }; 43 44 class CompilerHLSL : public CompilerGLSL 45 { 46 public: 47 struct Options 48 { 49 uint32_t shader_model = 30; // TODO: map ps_4_0_level_9_0,... somehow 50 51 // Allows the PointSize builtin, and ignores it, as PointSize is not supported in HLSL. 52 bool point_size_compat = false; 53 54 // Allows the PointCoord builtin, returns float2(0.5, 0.5), as PointCoord is not supported in HLSL. 55 bool point_coord_compat = false; 56 57 // If true, the backend will assume that VertexIndex and InstanceIndex will need to apply 58 // a base offset, and you will need to fill in a cbuffer with offsets. 59 // Set to false if you know you will never use base instance or base vertex 60 // functionality as it might remove an internal cbuffer. 61 bool support_nonzero_base_vertex_base_instance = false; 62 }; 63 CompilerHLSL(std::vector<uint32_t> spirv_)64 explicit CompilerHLSL(std::vector<uint32_t> spirv_) 65 : CompilerGLSL(std::move(spirv_)) 66 { 67 } 68 CompilerHLSL(const uint32_t * ir_,size_t size)69 CompilerHLSL(const uint32_t *ir_, size_t size) 70 : CompilerGLSL(ir_, size) 71 { 72 } 73 CompilerHLSL(const ParsedIR & ir_)74 explicit CompilerHLSL(const ParsedIR &ir_) 75 : CompilerGLSL(ir_) 76 { 77 } 78 CompilerHLSL(ParsedIR && ir_)79 explicit CompilerHLSL(ParsedIR &&ir_) 80 : CompilerGLSL(std::move(ir_)) 81 { 82 } 83 get_hlsl_options() const84 const Options &get_hlsl_options() const 85 { 86 return hlsl_options; 87 } 88 set_hlsl_options(const Options & opts)89 void set_hlsl_options(const Options &opts) 90 { 91 hlsl_options = opts; 92 } 93 94 // Optionally specify a custom root constant layout. 95 // 96 // Push constants ranges will be split up according to the 97 // layout specified. 98 void set_root_constant_layouts(std::vector<RootConstants> layout); 99 100 // Compiles and remaps vertex attributes at specific locations to a fixed semantic. 101 // The default is TEXCOORD# where # denotes location. 102 // Matrices are unrolled to vectors with notation ${SEMANTIC}_#, where # denotes row. 103 // $SEMANTIC is either TEXCOORD# or a semantic name specified here. 104 void add_vertex_attribute_remap(const HLSLVertexAttributeRemap &vertex_attributes); 105 std::string compile() override; 106 107 // This is a special HLSL workaround for the NumWorkGroups builtin. 108 // This does not exist in HLSL, so the calling application must create a dummy cbuffer in 109 // which the application will store this builtin. 110 // The cbuffer layout will be: 111 // cbuffer SPIRV_Cross_NumWorkgroups : register(b#, space#) { uint3 SPIRV_Cross_NumWorkgroups_count; }; 112 // This must be called before compile(). 113 // The function returns 0 if NumWorkGroups builtin is not statically used in the shader from the current entry point. 114 // If non-zero, this returns the variable ID of a cbuffer which corresponds to 115 // the cbuffer declared above. By default, no binding or descriptor set decoration is set, 116 // so the calling application should declare explicit bindings on this ID before calling compile(). 117 uint32_t remap_num_workgroups_builtin(); 118 119 private: 120 std::string type_to_glsl(const SPIRType &type, uint32_t id = 0) override; 121 std::string image_type_hlsl(const SPIRType &type, uint32_t id); 122 std::string image_type_hlsl_modern(const SPIRType &type, uint32_t id); 123 std::string image_type_hlsl_legacy(const SPIRType &type, uint32_t id); 124 void emit_function_prototype(SPIRFunction &func, const Bitset &return_flags) override; 125 void emit_hlsl_entry_point(); 126 void emit_header() override; 127 void emit_resources(); 128 void emit_interface_block_globally(const SPIRVariable &type); 129 void emit_interface_block_in_struct(const SPIRVariable &type, std::unordered_set<uint32_t> &active_locations); 130 void emit_builtin_inputs_in_struct(); 131 void emit_builtin_outputs_in_struct(); 132 void emit_texture_op(const Instruction &i) override; 133 void emit_instruction(const Instruction &instruction) override; 134 void emit_glsl_op(uint32_t result_type, uint32_t result_id, uint32_t op, const uint32_t *args, 135 uint32_t count) override; 136 void emit_buffer_block(const SPIRVariable &type) override; 137 void emit_push_constant_block(const SPIRVariable &var) override; 138 void emit_uniform(const SPIRVariable &var) override; 139 void emit_modern_uniform(const SPIRVariable &var); 140 void emit_legacy_uniform(const SPIRVariable &var); 141 void emit_specialization_constants_and_structs(); 142 void emit_composite_constants(); 143 void emit_fixup() override; 144 std::string builtin_to_glsl(spv::BuiltIn builtin, spv::StorageClass storage) override; 145 std::string layout_for_member(const SPIRType &type, uint32_t index) override; 146 std::string to_interpolation_qualifiers(const Bitset &flags) override; 147 std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type) override; 148 std::string to_func_call_arg(uint32_t id) override; 149 std::string to_sampler_expression(uint32_t id); 150 std::string to_resource_binding(const SPIRVariable &var); 151 std::string to_resource_binding_sampler(const SPIRVariable &var); 152 std::string to_resource_register(char space, uint32_t binding, uint32_t set); 153 void emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) override; 154 void emit_access_chain(const Instruction &instruction); 155 void emit_load(const Instruction &instruction); 156 std::string read_access_chain(const SPIRAccessChain &chain); 157 void write_access_chain(const SPIRAccessChain &chain, uint32_t value); 158 void emit_store(const Instruction &instruction); 159 void emit_atomic(const uint32_t *ops, uint32_t length, spv::Op op); 160 void emit_subgroup_op(const Instruction &i) override; 161 void emit_block_hints(const SPIRBlock &block) override; 162 163 void emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, const std::string &qualifier, 164 uint32_t base_offset = 0) override; 165 166 const char *to_storage_qualifiers_glsl(const SPIRVariable &var) override; 167 void replace_illegal_names() override; 168 169 Options hlsl_options; 170 171 // TODO: Refactor this to be more similar to MSL, maybe have some common system in place? 172 bool requires_op_fmod = false; 173 bool requires_fp16_packing = false; 174 bool requires_explicit_fp16_packing = false; 175 bool requires_unorm8_packing = false; 176 bool requires_snorm8_packing = false; 177 bool requires_unorm16_packing = false; 178 bool requires_snorm16_packing = false; 179 bool requires_bitfield_insert = false; 180 bool requires_bitfield_extract = false; 181 bool requires_inverse_2x2 = false; 182 bool requires_inverse_3x3 = false; 183 bool requires_inverse_4x4 = false; 184 bool requires_scalar_reflect = false; 185 bool requires_scalar_refract = false; 186 uint64_t required_textureSizeVariants = 0; 187 void require_texture_query_variant(const SPIRType &type); 188 189 enum TextureQueryVariantDim 190 { 191 Query1D = 0, 192 Query1DArray, 193 Query2D, 194 Query2DArray, 195 Query3D, 196 QueryBuffer, 197 QueryCube, 198 QueryCubeArray, 199 Query2DMS, 200 Query2DMSArray, 201 QueryDimCount 202 }; 203 204 enum TextureQueryVariantType 205 { 206 QueryTypeFloat = 0, 207 QueryTypeInt = 16, 208 QueryTypeUInt = 32, 209 QueryTypeCount = 3 210 }; 211 212 void emit_builtin_variables(); 213 bool require_output = false; 214 bool require_input = false; 215 SmallVector<HLSLVertexAttributeRemap> remap_vertex_attributes; 216 217 uint32_t type_to_consumed_locations(const SPIRType &type) const; 218 219 void emit_io_block(const SPIRVariable &var); 220 std::string to_semantic(uint32_t location, spv::ExecutionModel em, spv::StorageClass sc); 221 222 uint32_t num_workgroups_builtin = 0; 223 224 // Custom root constant layout, which should be emitted 225 // when translating push constant ranges. 226 std::vector<RootConstants> root_constants_layout; 227 228 void validate_shader_model(); 229 }; 230 } // namespace SPIRV_CROSS_NAMESPACE 231 232 #endif 233