RetroArch
spirv_hlsl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2018 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 #include <vector>
23 
24 namespace spirv_cross
25 {
26 // Interface which remaps vertex inputs to a fixed semantic name to make linking easier.
28 {
31 };
32 // Specifying a root constant (d3d12) or push constant range (vulkan).
33 //
34 // `start` and `end` denotes the range of the root constant in bytes.
35 // Both values need to be multiple of 4.
37 {
40 
43 };
44 
45 class CompilerHLSL : public CompilerGLSL
46 {
47 public:
48  struct Options
49  {
50  uint32_t shader_model = 30; // TODO: map ps_4_0_level_9_0,... somehow
51 
52  // Allows the PointSize builtin, and ignores it, as PointSize is not supported in HLSL.
53  bool point_size_compat = false;
54 
55  // Allows the PointCoord builtin, returns float2(0.5, 0.5), as PointCoord is not supported in HLSL.
56  bool point_coord_compat = false;
57  };
58 
59  CompilerHLSL(std::vector<uint32_t> spirv_)
60  : CompilerGLSL(move(spirv_))
61  {
62  }
63 
64  CompilerHLSL(const uint32_t *ir, size_t size)
65  : CompilerGLSL(ir, size)
66  {
67  }
68 
69  SPIRV_CROSS_DEPRECATED("CompilerHLSL::get_options() is obsolete, use get_hlsl_options() instead.")
70  const Options &get_options() const
71  {
72  return hlsl_options;
73  }
74 
75  const Options &get_hlsl_options() const
76  {
77  return hlsl_options;
78  }
79 
80  SPIRV_CROSS_DEPRECATED("CompilerHLSL::get_options() is obsolete, use set_hlsl_options() instead.")
81  void set_options(Options &opts)
82  {
83  hlsl_options = opts;
84  }
85 
86  void set_hlsl_options(const Options &opts)
87  {
88  hlsl_options = opts;
89  }
90 
91  // Optionally specify a custom root constant layout.
92  //
93  // Push constants ranges will be split up according to the
94  // layout specified.
95  void set_root_constant_layouts(std::vector<RootConstants> layout)
96  {
97  root_constants_layout = std::move(layout);
98  }
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  std::string compile(std::vector<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().
118 
119 private:
120  std::string type_to_glsl(const SPIRType &type, uint32_t id = 0) override;
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();
129  void emit_interface_block_in_struct(const SPIRVariable &type, std::unordered_set<uint32_t> &active_locations);
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);
143  void emit_fixup() override;
147  std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type) override;
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);
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 
170  bool requires_op_fmod = false;
171  bool requires_textureProj = false;
172  bool requires_fp16_packing = false;
180  bool requires_inverse_2x2 = false;
181  bool requires_inverse_3x3 = false;
182  bool requires_inverse_4x4 = false;
185 
187  {
188  Query1D = 0,
199  };
200 
202  {
207  };
208 
209  void emit_builtin_variables();
210  bool require_output = false;
211  bool require_input = false;
212  std::vector<HLSLVertexAttributeRemap> remap_vertex_attributes;
213 
215 
216  void emit_io_block(const SPIRVariable &var);
217  std::string to_semantic(uint32_t vertex_location);
218 
220 
221  // Custom root constant layout, which should be emitted
222  // when translating push constant ranges.
223  std::vector<RootConstants> root_constants_layout;
224 };
225 } // namespace spirv_cross
226 
227 #endif
bool require_input
Definition: spirv_hlsl.hpp:211
Definition: spirv_hlsl.hpp:194
Definition: spirv_hlsl.hpp:193
Definition: spirv_hlsl.hpp:36
void emit_legacy_uniform(const SPIRVariable &var)
Definition: spirv_hlsl.cpp:2960
void emit_buffer_block(const SPIRVariable &type) override
Definition: spirv_hlsl.cpp:1831
bool requires_inverse_3x3
Definition: spirv_hlsl.hpp:181
uint32_t shader_model
Definition: spirv_hlsl.hpp:50
Definition: spirv_common.hpp:283
bool requires_textureProj
Definition: spirv_hlsl.hpp:171
bool requires_bitfield_insert
Definition: spirv_hlsl.hpp:178
#define const
Definition: zconf.h:217
Definition: spirv_glsl.hpp:56
std::string type_to_glsl(const SPIRType &type, uint32_t id=0) override
Definition: spirv_hlsl.cpp:358
set set set set set set set macro pixldst1 op
Definition: pixman-arm-neon-asm.h:54
uint32_t end
Definition: spirv_hlsl.hpp:39
SPIRV_CROSS_DEPRECATED("CompilerHLSL::get_options() is obsolete, use set_hlsl_options() instead.") void set_options(Options &opts)
Definition: spirv_hlsl.hpp:80
void emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) override
Definition: spirv_hlsl.cpp:1969
void emit_push_constant_block(const SPIRVariable &var) override
Definition: spirv_hlsl.cpp:1898
Definition: spirv_hlsl.hpp:204
bool point_size_compat
Definition: spirv_hlsl.hpp:53
Definition: spirv_hlsl.hpp:206
StorageClass
Definition: spirv.hpp:137
void emit_modern_uniform(const SPIRVariable &var)
Definition: spirv_hlsl.cpp:2918
Definition: spirv_hlsl.hpp:48
Definition: spirv_common.hpp:369
Definition: spirv_hlsl.hpp:198
Definition: spirv_hlsl.hpp:27
GLsizeiptr size
Definition: glext.h:6559
Definition: spirv_hlsl.hpp:189
CompilerHLSL(std::vector< uint32_t > spirv_)
Definition: spirv_hlsl.hpp:59
Definition: spirv_hlsl.hpp:195
void emit_uniform(const SPIRVariable &var) override
Definition: spirv_hlsl.cpp:2975
std::string to_sampler_expression(uint32_t id)
Definition: spirv_hlsl.cpp:1954
void emit_builtin_inputs_in_struct()
Definition: spirv_hlsl.cpp:565
void require_texture_query_variant(const SPIRType &type)
Definition: spirv_hlsl.cpp:4440
GLsizei const GLchar *const * string
Definition: glext.h:6699
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
void emit_instruction(const Instruction &instruction) override
Definition: spirv_hlsl.cpp:3832
bool requires_inverse_2x2
Definition: spirv_hlsl.hpp:180
void emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, const std::string &qualifier, uint32_t base_offset=0) override
Definition: spirv_hlsl.cpp:1796
GLuint GLuint GLsizei count
Definition: glext.h:6292
TextureQueryVariantType
Definition: spirv_hlsl.hpp:201
uint32_t location
Definition: spirv_hlsl.hpp:29
void set_root_constant_layouts(std::vector< RootConstants > layout)
Definition: spirv_hlsl.hpp:95
void emit_interface_block_globally(const SPIRVariable &type)
Definition: spirv_hlsl.cpp:469
void emit_subgroup_op(const Instruction &i) override
Definition: spirv_hlsl.cpp:3658
uint32_t remap_num_workgroups_builtin()
Definition: spirv_hlsl.cpp:4504
std::string layout_for_member(const SPIRType &type, uint32_t index) override
Definition: spirv_hlsl.cpp:1779
bool requires_unorm8_packing
Definition: spirv_hlsl.hpp:174
bool requires_inverse_4x4
Definition: spirv_hlsl.hpp:182
GLenum type
Definition: glext.h:6233
TextureQueryVariantDim
Definition: spirv_hlsl.hpp:186
bool requires_fp16_packing
Definition: spirv_hlsl.hpp:172
void emit_store(const Instruction &instruction)
Definition: spirv_hlsl.cpp:3485
Definition: barrier.hpp:23
void emit_header() override
Definition: spirv_hlsl.cpp:458
std::string to_resource_register(char space, uint32_t binding, uint32_t set)
Definition: spirv_hlsl.cpp:2910
Op
Definition: spirv.hpp:714
void emit_io_block(const SPIRVariable &var)
Definition: spirv_hlsl.cpp:742
Options hlsl_options
Definition: spirv_hlsl.hpp:169
std::vector< HLSLVertexAttributeRemap > remap_vertex_attributes
Definition: spirv_hlsl.hpp:212
GLenum func
Definition: glext.h:6668
void emit_specialization_constants()
Definition: spirv_hlsl.cpp:1015
Definition: spirv_hlsl.hpp:197
bool requires_unorm16_packing
Definition: spirv_hlsl.hpp:176
void replace_illegal_names() override
Definition: spirv_hlsl.cpp:1057
Definition: spirv_hlsl.hpp:190
void set_hlsl_options(const Options &opts)
Definition: spirv_hlsl.hpp:86
void emit_hlsl_entry_point()
Definition: spirv_hlsl.cpp:2080
std::string semantic
Definition: spirv_hlsl.hpp:30
std::string builtin_to_glsl(spv::BuiltIn builtin, spv::StorageClass storage) override
Definition: spirv_hlsl.cpp:853
const Options & get_hlsl_options() const
Definition: spirv_hlsl.hpp:75
void emit_resources()
Definition: spirv_hlsl.cpp:1081
dictionary args
Definition: test_shaders.py:20
std::string image_type_hlsl(const SPIRType &type, uint32_t id)
Definition: spirv_hlsl.cpp:347
Definition: spirv_hlsl.hpp:192
T & set(uint32_t id, P &&... args)
Definition: spirv_cross.hpp:530
std::string image_type_hlsl_legacy(const SPIRType &type, uint32_t id)
Definition: spirv_hlsl.cpp:278
void emit_function_prototype(SPIRFunction &func, const Bitset &return_flags) override
Definition: spirv_hlsl.cpp:1993
SPIRV_CROSS_DEPRECATED("CompilerHLSL::get_options() is obsolete, use get_hlsl_options() instead.") const Options &get_options() const
Definition: spirv_hlsl.hpp:69
static int block
Definition: psp2.c:31
std::string image_type_hlsl_modern(const SPIRType &type, uint32_t id)
Definition: spirv_hlsl.cpp:227
std::string to_semantic(uint32_t vertex_location)
Definition: spirv_hlsl.cpp:733
bool requires_snorm16_packing
Definition: spirv_hlsl.hpp:177
void emit_atomic(const uint32_t *ops, uint32_t length, spv::Op op)
Definition: spirv_hlsl.cpp:3573
CompilerHLSL(const uint32_t *ir, size_t size)
Definition: spirv_hlsl.hpp:64
GLuint index
Definition: glext.h:6671
uint32_t start
Definition: spirv_hlsl.hpp:38
bool requires_explicit_fp16_packing
Definition: spirv_hlsl.hpp:173
argument_type
Definition: query.c:50
uint32_t space
Definition: spirv_hlsl.hpp:42
std::string compile() override
Definition: spirv_hlsl.cpp:4550
void emit_texture_op(const Instruction &i) override
Definition: spirv_hlsl.cpp:2408
void emit_builtin_variables()
Definition: spirv_hlsl.cpp:884
Definition: spirv_hlsl.hpp:203
bool requires_op_fmod
Definition: spirv_hlsl.hpp:170
std::vector< RootConstants > root_constants_layout
Definition: spirv_hlsl.hpp:223
Definition: spirv_common.hpp:803
void emit_composite_constants()
Definition: spirv_hlsl.cpp:987
Definition: spirv_hlsl.hpp:45
void emit_load(const Instruction &instruction)
Definition: spirv_hlsl.cpp:3343
void emit_access_chain(const Instruction &instruction)
Definition: spirv_hlsl.cpp:3495
uint32_t type_to_consumed_locations(const SPIRType &type) const
Definition: spirv_hlsl.cpp:687
const char * to_storage_qualifiers_glsl(const SPIRVariable &var) override
Definition: spirv_hlsl.cpp:482
bool requires_snorm8_packing
Definition: spirv_hlsl.hpp:175
bool requires_bitfield_extract
Definition: spirv_hlsl.hpp:179
std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type) override
Definition: spirv_hlsl.cpp:2984
Definition: spirv_hlsl.hpp:191
std::string to_resource_binding_sampler(const SPIRVariable &var)
Definition: spirv_hlsl.cpp:2900
Definition: spirv_common.hpp:97
GLsizei const GLfloat * value
Definition: glext.h:6709
Definition: spirv_common.hpp:554
void emit_builtin_outputs_in_struct()
Definition: spirv_hlsl.cpp:495
Unknown compiler Device disconnected from port File already exists Saving to backup buffer Got connection No arguments supplied and no menu builtin
Definition: msg_hash_eo.h:34
BuiltIn
Definition: spirv.hpp:402
void emit_block_hints(const SPIRBlock &block) override
Definition: spirv_hlsl.cpp:4609
Definition: spirv_hlsl.hpp:188
Definition: spirv_common.hpp:769
void emit_interface_block_in_struct(const SPIRVariable &type, std::unordered_set< uint32_t > &active_locations)
void emit_fixup() override
Definition: spirv_hlsl.cpp:2388
Definition: spirv_common.hpp:686
bool point_coord_compat
Definition: spirv_hlsl.hpp:56
std::string read_access_chain(const SPIRAccessChain &chain)
Definition: spirv_hlsl.cpp:3216
GLbitfield flags
Definition: glext.h:7828
uint32_t num_workgroups_builtin
Definition: spirv_hlsl.hpp:219
bool require_output
Definition: spirv_hlsl.hpp:210
void write_access_chain(const SPIRAccessChain &chain, uint32_t value)
Definition: spirv_hlsl.cpp:3371
std::string to_resource_binding(const SPIRVariable &var)
Definition: spirv_hlsl.cpp:2840
unsigned __int64 uint64_t
Definition: stdint.h:136
GLenum GLuint GLenum GLsizei length
Definition: glext.h:6233
unsigned int uint32_t
Definition: stdint.h:126
Definition: spirv_hlsl.hpp:196
Definition: spirv_hlsl.hpp:205
void emit_glsl_op(uint32_t result_type, uint32_t result_id, uint32_t op, const uint32_t *args, uint32_t count) override
Definition: spirv_hlsl.cpp:3032
uint32_t binding
Definition: spirv_hlsl.hpp:41
std::string to_interpolation_qualifiers(const Bitset &flags) override
Definition: spirv_hlsl.cpp:712
std::string to_func_call_arg(uint32_t id) override
Definition: spirv_hlsl.cpp:1974
uint64_t required_textureSizeVariants
Definition: spirv_hlsl.hpp:183