radeonsi/gfx10: export primitives at the beginning of VS/TES
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_internal.h
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 "si_shader.h"
29 #include "ac_shader_abi.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 unsigned semantic_name;
43 unsigned semantic_index;
44 ubyte vertex_stream[4];
45 };
46
47 struct si_shader_context {
48 struct ac_llvm_context ac;
49 struct si_shader *shader;
50 struct si_screen *screen;
51
52 unsigned type; /* PIPE_SHADER_* specifies the type of shader. */
53
54 /* For clamping the non-constant index in resource indexing: */
55 unsigned num_const_buffers;
56 unsigned num_shader_buffers;
57 unsigned num_images;
58 unsigned num_samplers;
59
60 struct ac_shader_args args;
61 struct ac_shader_abi abi;
62
63 LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS];
64
65 LLVMBasicBlockRef merged_wrap_if_entry_block;
66 int merged_wrap_if_label;
67
68 LLVMValueRef main_fn;
69 LLVMTypeRef return_type;
70
71 struct ac_arg const_and_shader_buffers;
72 struct ac_arg samplers_and_images;
73
74 /* For merged shaders, the per-stage descriptors for the stage other
75 * than the one we're processing, used to pass them through from the
76 * first stage to the second.
77 */
78 struct ac_arg other_const_and_shader_buffers;
79 struct ac_arg other_samplers_and_images;
80
81 struct ac_arg rw_buffers;
82 struct ac_arg bindless_samplers_and_images;
83 /* Common inputs for merged shaders. */
84 struct ac_arg merged_wave_info;
85 struct ac_arg merged_scratch_offset;
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 * [11:23] = stride between patches in DW = num_inputs * num_vertices * 4
99 * max = 32*32*4 + 32*4
100 * [24:31] = stride between vertices in DW = num_inputs * 4
101 * max = 32*4
102 */
103 struct ac_arg vs_state_bits;
104 struct ac_arg vs_blit_inputs;
105 /* HW VS */
106 struct ac_arg streamout_config;
107 struct ac_arg streamout_write_index;
108 struct ac_arg streamout_offset[4];
109
110 /* API TCS & TES */
111 /* Layout of TCS outputs in the offchip buffer
112 * # 6 bits
113 * [0:5] = the number of patches per threadgroup, max = NUM_PATCHES (40)
114 * # 6 bits
115 * [6:11] = the number of output vertices per patch, max = 32
116 * # 20 bits
117 * [12:31] = the offset of per patch attributes in the buffer in bytes.
118 * max = NUM_PATCHES*32*32*16
119 */
120 struct ac_arg tcs_offchip_layout;
121
122 /* API TCS */
123 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
124 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
125 * [16:31] = TCS output patch0 offset for per-patch / 16
126 * max = (NUM_PATCHES + 1) * 32*32
127 */
128 struct ac_arg tcs_out_lds_offsets;
129 /* Layout of TCS outputs / TES inputs:
130 * [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
131 * max = 32*32*4 + 32*4
132 * [13:18] = gl_PatchVerticesIn, max = 32
133 * [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
134 */
135 struct ac_arg tcs_out_lds_layout;
136 struct ac_arg tcs_offchip_offset;
137 struct ac_arg tcs_factor_offset;
138
139 /* API TES */
140 struct ac_arg tes_offchip_addr;
141 struct ac_arg tes_u;
142 struct ac_arg tes_v;
143 struct ac_arg tes_rel_patch_id;
144 /* HW ES */
145 struct ac_arg es2gs_offset;
146 /* HW GS */
147 /* On gfx10:
148 * - bits 0..11: ordered_wave_id
149 * - bits 12..20: number of vertices in group
150 * - bits 22..30: number of primitives in group
151 */
152 struct ac_arg gs_tg_info;
153 /* API GS */
154 struct ac_arg gs2vs_offset;
155 struct ac_arg gs_wave_id; /* GFX6 */
156 struct ac_arg gs_vtx_offset[6]; /* in dwords (GFX6) */
157 struct ac_arg gs_vtx01_offset; /* in dwords (GFX9) */
158 struct ac_arg gs_vtx23_offset; /* in dwords (GFX9) */
159 struct ac_arg gs_vtx45_offset; /* in dwords (GFX9) */
160 /* PS */
161 struct ac_arg pos_fixed_pt;
162 /* CS */
163 struct ac_arg block_size;
164 struct ac_arg cs_user_data;
165
166 struct ac_llvm_compiler *compiler;
167
168 /* Preloaded descriptors. */
169 LLVMValueRef esgs_ring;
170 LLVMValueRef gsvs_ring[4];
171 LLVMValueRef tess_offchip_ring;
172
173 LLVMValueRef invoc0_tess_factors[6]; /* outer[4], inner[2] */
174 LLVMValueRef gs_next_vertex[4];
175 LLVMValueRef gs_curprim_verts[4];
176 LLVMValueRef gs_generated_prims[4];
177 LLVMValueRef gs_ngg_emit;
178 LLVMValueRef gs_ngg_scratch;
179 LLVMValueRef postponed_kill;
180 LLVMValueRef return_value;
181
182 LLVMTypeRef voidt;
183 LLVMTypeRef i1;
184 LLVMTypeRef i8;
185 LLVMTypeRef i32;
186 LLVMTypeRef i64;
187 LLVMTypeRef i128;
188 LLVMTypeRef f32;
189 LLVMTypeRef v2i32;
190 LLVMTypeRef v4i32;
191 LLVMTypeRef v4f32;
192 LLVMTypeRef v8i32;
193
194 LLVMValueRef i32_0;
195 LLVMValueRef i32_1;
196 LLVMValueRef i1false;
197 LLVMValueRef i1true;
198 };
199
200 static inline struct si_shader_context *
201 si_shader_context_from_abi(struct ac_shader_abi *abi)
202 {
203 struct si_shader_context *ctx = NULL;
204 return container_of(abi, ctx, abi);
205 }
206
207 void si_llvm_context_init(struct si_shader_context *ctx,
208 struct si_screen *sscreen,
209 struct ac_llvm_compiler *compiler,
210 unsigned wave_size);
211 void si_llvm_context_set_ir(struct si_shader_context *ctx,
212 struct si_shader *shader);
213
214 void si_llvm_create_func(struct si_shader_context *ctx, const char *name,
215 LLVMTypeRef *return_types, unsigned num_return_elems,
216 unsigned max_workgroup_size);
217
218 void si_llvm_dispose(struct si_shader_context *ctx);
219
220 void si_llvm_optimize_module(struct si_shader_context *ctx);
221
222 LLVMValueRef si_nir_load_input_tes(struct ac_shader_abi *abi,
223 LLVMTypeRef type,
224 LLVMValueRef vertex_index,
225 LLVMValueRef param_index,
226 unsigned const_index,
227 unsigned location,
228 unsigned driver_location,
229 unsigned component,
230 unsigned num_components,
231 bool is_patch,
232 bool is_compact,
233 bool load_input);
234 bool si_is_merged_shader(struct si_shader_context *ctx);
235 LLVMValueRef si_get_sample_id(struct si_shader_context *ctx);
236 LLVMValueRef si_buffer_load_const(struct si_shader_context *ctx,
237 LLVMValueRef resource, LLVMValueRef offset);
238 void si_llvm_build_ret(struct si_shader_context *ctx, LLVMValueRef ret);
239 LLVMValueRef si_prolog_get_rw_buffers(struct si_shader_context *ctx);
240 LLVMValueRef si_build_gather_64bit(struct si_shader_context *ctx,
241 LLVMTypeRef type, LLVMValueRef val1,
242 LLVMValueRef val2);
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_declare_compute_memory(struct si_shader_context *ctx);
246 LLVMValueRef si_get_primitive_id(struct si_shader_context *ctx,
247 unsigned swizzle);
248 void si_llvm_export_vs(struct si_shader_context *ctx,
249 struct si_shader_output_values *outputs,
250 unsigned noutput);
251 void si_emit_streamout_output(struct si_shader_context *ctx,
252 LLVMValueRef const *so_buffers,
253 LLVMValueRef const *so_write_offsets,
254 struct pipe_stream_output *stream_out,
255 struct si_shader_output_values *shader_out);
256 void si_add_arg_checked(struct ac_shader_args *args,
257 enum ac_arg_regfile file,
258 unsigned registers, enum ac_arg_type type,
259 struct ac_arg *arg,
260 unsigned idx);
261
262 void si_llvm_load_input_vs(
263 struct si_shader_context *ctx,
264 unsigned input_index,
265 LLVMValueRef out[4]);
266
267 bool si_nir_build_llvm(struct si_shader_context *ctx, struct nir_shader *nir);
268
269 LLVMValueRef si_unpack_param(struct si_shader_context *ctx,
270 struct ac_arg param, unsigned rshift,
271 unsigned bitwidth);
272 void si_build_wrapper_function(struct si_shader_context *ctx, LLVMValueRef *parts,
273 unsigned num_parts, unsigned main_part,
274 unsigned next_shader_first_part);
275 bool si_need_ps_prolog(const union si_shader_part_key *key);
276 void si_get_ps_prolog_key(struct si_shader *shader,
277 union si_shader_part_key *key,
278 bool separate_prolog);
279 void si_get_ps_epilog_key(struct si_shader *shader,
280 union si_shader_part_key *key);
281 LLVMValueRef si_insert_input_ret(struct si_shader_context *ctx, LLVMValueRef ret,
282 struct ac_arg param, unsigned return_index);
283 LLVMValueRef si_insert_input_ret_float(struct si_shader_context *ctx, LLVMValueRef ret,
284 struct ac_arg param, unsigned return_index);
285 LLVMValueRef si_insert_input_ptr(struct si_shader_context *ctx, LLVMValueRef ret,
286 struct ac_arg param, unsigned return_index);
287 int si_compile_llvm(struct si_screen *sscreen,
288 struct si_shader_binary *binary,
289 struct ac_shader_config *conf,
290 struct ac_llvm_compiler *compiler,
291 struct ac_llvm_context *ac,
292 struct pipe_debug_callback *debug,
293 enum pipe_shader_type shader_type,
294 const char *name,
295 bool less_optimized);
296 void si_fix_resource_usage(struct si_screen *sscreen, struct si_shader *shader);
297 void si_llvm_emit_streamout(struct si_shader_context *ctx,
298 struct si_shader_output_values *outputs,
299 unsigned noutput, unsigned stream);
300 void si_create_function(struct si_shader_context *ctx);
301
302 bool gfx10_ngg_export_prim_early(struct si_shader *shader);
303 void gfx10_ngg_build_sendmsg_gs_alloc_req(struct si_shader_context *ctx);
304 void gfx10_ngg_build_export_prim(struct si_shader_context *ctx,
305 LLVMValueRef user_edgeflags[3]);
306 void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
307 unsigned max_outputs,
308 LLVMValueRef *addrs);
309 void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
310 unsigned stream,
311 LLVMValueRef *addrs);
312 void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx);
313 void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx);
314 void gfx10_ngg_calculate_subgroup_info(struct si_shader *shader);
315
316 /* si_shader_llvm_gs.c */
317 LLVMValueRef si_is_es_thread(struct si_shader_context *ctx);
318 LLVMValueRef si_is_gs_thread(struct si_shader_context *ctx);
319 void si_llvm_emit_es_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
320 LLVMValueRef *addrs);
321 void si_preload_esgs_ring(struct si_shader_context *ctx);
322 void si_preload_gs_rings(struct si_shader_context *ctx);
323 void si_llvm_build_gs_prolog(struct si_shader_context *ctx,
324 union si_shader_part_key *key);
325 void si_llvm_init_gs_callbacks(struct si_shader_context *ctx);
326
327 /* si_shader_llvm_tess.c */
328 void si_llvm_preload_tes_rings(struct si_shader_context *ctx);
329 void si_llvm_emit_ls_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
330 LLVMValueRef *addrs);
331 void si_llvm_build_tcs_epilog(struct si_shader_context *ctx,
332 union si_shader_part_key *key);
333 void si_llvm_init_tcs_callbacks(struct si_shader_context *ctx);
334 void si_llvm_init_tes_callbacks(struct si_shader_context *ctx);
335
336 /* si_shader_llvm_ps.c */
337 void si_llvm_build_ps_prolog(struct si_shader_context *ctx,
338 union si_shader_part_key *key);
339 void si_llvm_build_ps_epilog(struct si_shader_context *ctx,
340 union si_shader_part_key *key);
341 void si_llvm_build_monolithic_ps(struct si_shader_context *ctx,
342 struct si_shader *shader);
343 void si_llvm_init_ps_callbacks(struct si_shader_context *ctx);
344
345 /* si_shader_llvm_resources.c */
346 void si_llvm_init_resource_callbacks(struct si_shader_context *ctx);
347
348 #endif