1 /**************************************************************************
3 * Copyright 2010 Luca Barbieri
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
27 /* Header for Shader Model 4.0, 4.1 and 5.0 */
43 extern const char* sm4_opcode_names
[];
44 extern const char* sm4_file_names
[];
45 extern const char* sm4_file_ms_names
[];
46 extern const char* sm4_target_names
[];
47 extern const char* sm4_interpolation_names
[];
48 extern const char* sm4_sv_names
[];
50 struct sm4_token_version
58 struct sm4_token_instruction
60 // we don't make it an union directly because unions can't be inherited from
63 // length and extended are always present, but they are only here to reduce duplication
69 unsigned extended
: 1;
74 unsigned resinfo_return_type
: 2;
77 unsigned test_nz
: 1; // bit 18
78 unsigned precise_mask
: 4;
81 unsigned extended
: 1;
86 unsigned threads_in_group
: 1;
87 unsigned shared_memory
: 1;
88 unsigned uav_group
: 1;
89 unsigned uav_global
: 1;
95 unsigned allow_refactoring
: 1;
97 unsigned early_depth_stencil
: 1;
98 unsigned enable_raw_and_structured_in_non_cs
: 1;
102 unsigned opcode
: 11;
104 unsigned nr_samples
: 7;
108 unsigned opcode
: 11;
114 unsigned opcode
: 11;
115 unsigned interpolation
: 5;
119 unsigned opcode
: 11;
120 unsigned dynamic
: 1;
121 } dcl_constant_buffer
;
124 unsigned opcode
: 11;
125 unsigned primitive
: 6;
126 } dcl_gs_input_primitive
;
129 unsigned opcode
: 11;
130 unsigned primitive_topology
: 7;
131 } dcl_gs_output_primitive_topology
;
134 unsigned opcode
: 11;
135 unsigned control_points
: 6;
136 } dcl_input_control_point_count
;
139 unsigned opcode
: 11;
140 unsigned control_points
: 6;
141 } dcl_output_control_point_count
;
144 unsigned opcode
: 11;
145 unsigned domain
: 3; /* D3D_TESSELLATOR_DOMAIN */
149 unsigned opcode
: 11;
150 unsigned partitioning
: 3; /* D3D_TESSELLATOR_PARTITIONING */
151 } dcl_tess_partitioning
;
154 unsigned opcode
: 11;
155 unsigned primitive
: 3; /* D3D_TESSELLATOR_OUTPUT_PRIMITIVE */
156 } dcl_tess_output_primitive
;
160 union sm4_token_instruction_extended
166 unsigned extended
:1;
188 } resource_return_type
;
191 struct sm4_token_resource_return_type
199 struct sm4_token_operand
201 unsigned comps_enum
: 2; /* sm4_operands_comps */
202 unsigned mode
: 2; /* sm4_operand_mode */
204 unsigned file
: 8; /* sm4_file */
205 unsigned num_indices
: 2;
206 unsigned index0_repr
: 3; /* sm4_operand_index_repr */
207 unsigned index1_repr
: 3; /* sm4_operand_index_repr */
208 unsigned index2_repr
: 3; /* sm4_operand_index_repr */
209 unsigned extended
: 1;
212 #define SM4_OPERAND_SEL_MASK(sel) ((sel) & 0xf)
213 #define SM4_OPERAND_SEL_SWZ(sel, i) (((sel) >> ((i) * 2)) & 3)
214 #define SM4_OPERAND_SEL_SCALAR(sel) ((sel) & 3)
216 struct sm4_token_operand_extended
237 std::ostream
& operator <<(std::ostream
& out
, const sm4_op
& op
);
238 std::ostream
& operator <<(std::ostream
& out
, const sm4_insn
& op
);
239 std::ostream
& operator <<(std::ostream
& out
, const sm4_dcl
& op
);
240 std::ostream
& operator <<(std::ostream
& out
, const sm4_program
& op
);
250 sm4_any imm_values
[4];
256 std::auto_ptr
<sm4_op
> reg
;
259 bool is_index_simple(unsigned i
) const
261 return !indices
[i
].reg
.get() && indices
[i
].disp
>= 0 && (int64_t)(int32_t)indices
[i
].disp
== indices
[i
].disp
;
264 bool has_simple_index() const
266 return num_indices
== 1 && is_index_simple(0);
271 memset(this, 0, sizeof(*this));
277 sm4_op(const sm4_op
& op
)
282 #define SM4_MAX_OPS 6
284 struct sm4_insn
: public sm4_token_instruction
286 int8_t sample_offset
[3];
287 uint8_t resource_target
;
288 uint8_t resource_return_type
[4];
292 std::auto_ptr
<sm4_op
> ops
[SM4_MAX_OPS
];
296 memset(this, 0, sizeof(*this));
302 sm4_insn(const sm4_insn
& op
)
306 struct sm4_dcl
: public sm4_token_instruction
308 std::auto_ptr
<sm4_op
> op
;
317 unsigned expected_function_table_length
;
318 unsigned table_length
;
319 unsigned array_length
;
321 unsigned thread_group_size
[3];
322 sm4_token_resource_return_type rrt
;
339 memset(this, 0, sizeof(*this));
350 sm4_dcl(const sm4_dcl
& op
)
356 sm4_token_version version
;
357 std::vector
<sm4_dcl
*> dcls
;
358 std::vector
<sm4_insn
*> insns
;
360 /* for ifs, the insn number of the else or endif if there is no else
361 * for elses, the insn number of the endif
362 * for endifs, the insn number of the if
363 * for loops, the insn number of the endloop
364 * for endloops, the insn number of the loop
367 std::vector
<int> cf_insn_linked
;
369 /* NOTE: sampler 0 is the unnormalized nearest sampler for LD/LD_MS, while
370 * sampler 1 is user-specified sampler 0
372 bool resource_sampler_slots_assigned
;
373 std::vector
<int> slot_to_resource
;
374 std::vector
<int> slot_to_sampler
;
375 std::map
<std::pair
<int, int>, int> resource_sampler_to_slot
;
376 std::map
<int, int> resource_to_slot
;
379 std::vector
<int> label_to_insn_num
;
383 memset(&version
, 0, sizeof(version
));
384 labels_found
= false;
385 resource_sampler_slots_assigned
= false;
390 for(std::vector
<sm4_dcl
*>::iterator i
= dcls
.begin(), e
= dcls
.end(); i
!= e
; ++i
)
392 for(std::vector
<sm4_insn
*>::iterator i
= insns
.begin(), e
= insns
.end(); i
!= e
; ++i
)
399 sm4_program(const sm4_dcl
& op
)
403 sm4_program
* sm4_parse(void* tokens
, int size
);
405 bool sm4_link_cf_insns(sm4_program
& program
);
406 bool sm4_find_labels(sm4_program
& program
);
407 bool sm4_allocate_resource_sampler_pairs(sm4_program
& program
);