1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef SI_SHADER_PRIVATE_H
26 #define SI_SHADER_PRIVATE_H
27
28 #include "ac_shader_abi.h"
29 #include "si_shader.h"
30
31 struct pipe_debug_callback;
32
33 #define RADEON_LLVM_MAX_INPUTS 32 * 4
34
35 /* Ideally pass the sample mask input to the PS epilog as v14, which
36 * is its usual location, so that the shader doesn't have to add v_mov.
37 */
38 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 14
39
40 struct si_shader_output_values {
41 LLVMValueRef values[4];
42 ubyte vertex_stream[4];
43 ubyte semantic;
44 };
45
46 struct si_shader_context {
47 struct ac_llvm_context ac;
48 struct si_shader *shader;
49 struct si_screen *screen;
50
51 gl_shader_stage stage;
52
53 /* For clamping the non-constant index in resource indexing: */
54 unsigned num_const_buffers;
55 unsigned num_shader_buffers;
56 unsigned num_images;
57 unsigned num_samplers;
58
59 struct ac_shader_args args;
60 struct ac_shader_abi abi;
61
62 LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS];
63
64 LLVMBasicBlockRef merged_wrap_if_entry_block;
65 int merged_wrap_if_label;
66
67 LLVMValueRef main_fn;
68 LLVMTypeRef return_type;
69
70 struct ac_arg const_and_shader_buffers;
71 struct ac_arg samplers_and_images;
72
73 /* For merged shaders, the per-stage descriptors for the stage other
74 * than the one we're processing, used to pass them through from the
75 * first stage to the second.
76 */
77 struct ac_arg other_const_and_shader_buffers;
78 struct ac_arg other_samplers_and_images;
79
80 struct ac_arg rw_buffers;
81 struct ac_arg bindless_samplers_and_images;
82 /* Common inputs for merged shaders. */
83 struct ac_arg merged_wave_info;
84 struct ac_arg merged_scratch_offset;
85 struct ac_arg small_prim_cull_info;
86 /* API VS */
87 struct ac_arg vertex_buffers;
88 struct ac_arg vb_descriptors[5];
89 struct ac_arg rel_auto_id;
90 struct ac_arg vs_prim_id;
91 struct ac_arg vertex_index0;
92 /* VS states and layout of LS outputs / TCS inputs at the end
93 * [0] = clamp vertex color
94 * [1] = indexed
95 * [2:3] = NGG: output primitive type
96 * [4:5] = NGG: provoking vertex index
97 * [6] = NGG: streamout queries enabled
98 * [7:10] = NGG: small prim filter precision = num_samples / quant_mode,
99 * but in reality it's: 1/2^n, from 1/16 to 1/4096 = 1/2^4 to 1/2^12
100 * Only the first 4 bits of the exponent are stored.
101 * Set it like this: (fui(num_samples / quant_mode) >> 23)
102 * Expand to FP32 like this: ((0x70 | value) << 23);
103 * With 0x70 = 112, we get 2^(112 + value - 127) = 2^(value - 15)
104 * = 1/2^(15 - value) in FP32
105 * [11:23] = stride between patches in DW = num_inputs * num_vertices * 4
106 * max = 32*32*4 + 32*4
107 * [24:31] = stride between vertices in DW = num_inputs * 4
108 * max = 32*4
109 */
110 struct ac_arg vs_state_bits;
111 struct ac_arg vs_blit_inputs;
112 /* HW VS */
113 struct ac_arg streamout_config;
114 struct ac_arg streamout_write_index;
115 struct ac_arg streamout_offset[4];
116
117 /* API TCS & TES */
118 /* Layout of TCS outputs in the offchip buffer
119 * # 6 bits
120 * [0:5] = the number of patches per threadgroup, max = NUM_PATCHES (40)
121 * # 6 bits
122 * [6:11] = the number of output vertices per patch, max = 32
123 * # 20 bits
124 * [12:31] = the offset of per patch attributes in the buffer in bytes.
125 * max = NUM_PATCHES*32*32*16
126 */
127 struct ac_arg tcs_offchip_layout;
128
129 /* API TCS */
130 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
131 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
132 * [16:31] = TCS output patch0 offset for per-patch / 16
133 * max = (NUM_PATCHES + 1) * 32*32
134 */
135 struct ac_arg tcs_out_lds_offsets;
136 /* Layout of TCS outputs / TES inputs:
137 * [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
138 * max = 32*32*4 + 32*4
139 * [13:18] = gl_PatchVerticesIn, max = 32
140 * [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
141 */
142 struct ac_arg tcs_out_lds_layout;
143 struct ac_arg tcs_offchip_offset;
144 struct ac_arg tcs_factor_offset;
145
146 /* API TES */
147 struct ac_arg tes_offchip_addr;
148 struct ac_arg tes_u;
149 struct ac_arg tes_v;
150 struct ac_arg tes_rel_patch_id;
151 /* HW ES */
152 struct ac_arg es2gs_offset;
153 /* HW GS */
154 /* On gfx10:
155 * - bits 0..11: ordered_wave_id
156 * - bits 12..20: number of vertices in group
157 * - bits 22..30: number of primitives in group
158 */
159 struct ac_arg gs_tg_info;
160 /* API GS */
161 struct ac_arg gs2vs_offset;
162 struct ac_arg gs_wave_id; /* GFX6 */
163 struct ac_arg gs_vtx_offset[6]; /* in dwords (GFX6) */
164 struct ac_arg gs_vtx01_offset; /* in dwords (GFX9) */
165 struct ac_arg gs_vtx23_offset; /* in dwords (GFX9) */
166 struct ac_arg gs_vtx45_offset; /* in dwords (GFX9) */
167 /* PS */
168 struct ac_arg pos_fixed_pt;
169 /* CS */
170 struct ac_arg block_size;
171 struct ac_arg cs_user_data;
172 struct ac_arg cs_shaderbuf[3];
173 struct ac_arg cs_image[3];
174
175 struct ac_llvm_compiler *compiler;
176
177 /* Preloaded descriptors. */
178 LLVMValueRef esgs_ring;
179 LLVMValueRef gsvs_ring[4];
180 LLVMValueRef tess_offchip_ring;
181
182 LLVMValueRef invoc0_tess_factors[6]; /* outer[4], inner[2] */
183 LLVMValueRef gs_next_vertex[4];
184 LLVMValueRef gs_curprim_verts[4];
185 LLVMValueRef gs_generated_prims[4];
186 LLVMValueRef gs_ngg_emit;
187 LLVMValueRef gs_ngg_scratch;
188 LLVMValueRef return_value;
189 };
190
si_shader_context_from_abi(struct ac_shader_abi * abi)191 static inline struct si_shader_context *si_shader_context_from_abi(struct ac_shader_abi *abi)
192 {
193 struct si_shader_context *ctx = NULL;
194 return container_of(abi, ctx, abi);
195 }
196
197 bool si_is_multi_part_shader(struct si_shader *shader);
198 bool si_is_merged_shader(struct si_shader *shader);
199 void si_add_arg_checked(struct ac_shader_args *args, enum ac_arg_regfile file, unsigned registers,
200 enum ac_arg_type type, struct ac_arg *arg, unsigned idx);
201 unsigned si_get_max_workgroup_size(const struct si_shader *shader);
202 bool si_need_ps_prolog(const union si_shader_part_key *key);
203 void si_get_ps_prolog_key(struct si_shader *shader, union si_shader_part_key *key,
204 bool separate_prolog);
205 void si_get_ps_epilog_key(struct si_shader *shader, union si_shader_part_key *key);
206 void si_fix_resource_usage(struct si_screen *sscreen, struct si_shader *shader);
207 void si_create_function(struct si_shader_context *ctx, bool ngg_cull_shader);
208
209 bool gfx10_ngg_export_prim_early(struct si_shader *shader);
210 void gfx10_ngg_build_sendmsg_gs_alloc_req(struct si_shader_context *ctx);
211 void gfx10_ngg_build_export_prim(struct si_shader_context *ctx, LLVMValueRef user_edgeflags[3],
212 LLVMValueRef prim_passthrough);
213 void gfx10_emit_ngg_culling_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
214 LLVMValueRef *addrs);
215 void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi, unsigned max_outputs, LLVMValueRef *addrs);
216 void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx, unsigned stream, LLVMValueRef *addrs);
217 void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx);
218 void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx);
219 unsigned gfx10_ngg_get_scratch_dw_size(struct si_shader *shader);
220 bool gfx10_ngg_calculate_subgroup_info(struct si_shader *shader);
221
222 /* si_shader_llvm.c */
223 bool si_compile_llvm(struct si_screen *sscreen, struct si_shader_binary *binary,
224 struct ac_shader_config *conf, struct ac_llvm_compiler *compiler,
225 struct ac_llvm_context *ac, struct pipe_debug_callback *debug,
226 gl_shader_stage stage, const char *name, bool less_optimized);
227 void si_llvm_context_init(struct si_shader_context *ctx, struct si_screen *sscreen,
228 struct ac_llvm_compiler *compiler, unsigned wave_size);
229 void si_llvm_create_func(struct si_shader_context *ctx, const char *name, LLVMTypeRef *return_types,
230 unsigned num_return_elems, unsigned max_workgroup_size);
231 void si_llvm_optimize_module(struct si_shader_context *ctx);
232 void si_llvm_dispose(struct si_shader_context *ctx);
233 LLVMValueRef si_buffer_load_const(struct si_shader_context *ctx, LLVMValueRef resource,
234 LLVMValueRef offset);
235 void si_llvm_build_ret(struct si_shader_context *ctx, LLVMValueRef ret);
236 LLVMValueRef si_insert_input_ret(struct si_shader_context *ctx, LLVMValueRef ret,
237 struct ac_arg param, unsigned return_index);
238 LLVMValueRef si_insert_input_ret_float(struct si_shader_context *ctx, LLVMValueRef ret,
239 struct ac_arg param, unsigned return_index);
240 LLVMValueRef si_insert_input_ptr(struct si_shader_context *ctx, LLVMValueRef ret,
241 struct ac_arg param, unsigned return_index);
242 LLVMValueRef si_prolog_get_rw_buffers(struct si_shader_context *ctx);
243 void si_llvm_emit_barrier(struct si_shader_context *ctx);
244 void si_llvm_declare_esgs_ring(struct si_shader_context *ctx);
245 void si_init_exec_from_input(struct si_shader_context *ctx, struct ac_arg param,
246 unsigned bitoffset);
247 LLVMValueRef si_unpack_param(struct si_shader_context *ctx, struct ac_arg param, unsigned rshift,
248 unsigned bitwidth);
249 LLVMValueRef si_get_primitive_id(struct si_shader_context *ctx, unsigned swizzle);
250 LLVMValueRef si_llvm_get_block_size(struct ac_shader_abi *abi);
251 void si_llvm_declare_compute_memory(struct si_shader_context *ctx);
252 bool si_nir_build_llvm(struct si_shader_context *ctx, struct nir_shader *nir);
253 void si_build_wrapper_function(struct si_shader_context *ctx, LLVMValueRef *parts,
254 unsigned num_parts, unsigned main_part,
255 unsigned next_shader_first_part);
256
257 /* si_shader_llvm_gs.c */
258 LLVMValueRef si_is_es_thread(struct si_shader_context *ctx);
259 LLVMValueRef si_is_gs_thread(struct si_shader_context *ctx);
260 void si_llvm_emit_es_epilogue(struct ac_shader_abi *abi, unsigned max_outputs, LLVMValueRef *addrs);
261 void si_preload_esgs_ring(struct si_shader_context *ctx);
262 void si_preload_gs_rings(struct si_shader_context *ctx);
263 void si_llvm_build_gs_prolog(struct si_shader_context *ctx, union si_shader_part_key *key);
264 void si_llvm_init_gs_callbacks(struct si_shader_context *ctx);
265
266 /* si_shader_llvm_tess.c */
267 void si_llvm_preload_tes_rings(struct si_shader_context *ctx);
268 void si_llvm_emit_ls_epilogue(struct ac_shader_abi *abi, unsigned max_outputs, LLVMValueRef *addrs);
269 void si_llvm_build_tcs_epilog(struct si_shader_context *ctx, union si_shader_part_key *key);
270 void si_llvm_init_tcs_callbacks(struct si_shader_context *ctx);
271 void si_llvm_init_tes_callbacks(struct si_shader_context *ctx, bool ngg_cull_shader);
272
273 /* si_shader_llvm_ps.c */
274 LLVMValueRef si_get_sample_id(struct si_shader_context *ctx);
275 void si_llvm_build_ps_prolog(struct si_shader_context *ctx, union si_shader_part_key *key);
276 void si_llvm_build_ps_epilog(struct si_shader_context *ctx, union si_shader_part_key *key);
277 void si_llvm_build_monolithic_ps(struct si_shader_context *ctx, struct si_shader *shader);
278 void si_llvm_init_ps_callbacks(struct si_shader_context *ctx);
279
280 /* si_shader_llvm_resources.c */
281 void si_llvm_init_resource_callbacks(struct si_shader_context *ctx);
282
283 /* si_shader_llvm_vs.c */
284 void si_llvm_load_vs_inputs(struct si_shader_context *ctx, struct nir_shader *nir);
285 void si_llvm_streamout_store_output(struct si_shader_context *ctx, LLVMValueRef const *so_buffers,
286 LLVMValueRef const *so_write_offsets,
287 struct pipe_stream_output *stream_out,
288 struct si_shader_output_values *shader_out);
289 void si_llvm_emit_streamout(struct si_shader_context *ctx, struct si_shader_output_values *outputs,
290 unsigned noutput, unsigned stream);
291 void si_llvm_build_vs_exports(struct si_shader_context *ctx,
292 struct si_shader_output_values *outputs, unsigned noutput);
293 void si_llvm_emit_vs_epilogue(struct ac_shader_abi *abi, unsigned max_outputs, LLVMValueRef *addrs);
294 void si_llvm_build_vs_prolog(struct si_shader_context *ctx, union si_shader_part_key *key);
295 void si_llvm_init_vs_callbacks(struct si_shader_context *ctx, bool ngg_cull_shader);
296
297 #endif
298