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
31struct util_debug_callback;
32
33struct si_shader_output_values {
34   LLVMValueRef values[4];
35   ubyte vertex_streams;
36   ubyte semantic;
37};
38
39struct si_shader_context {
40   struct ac_llvm_context ac;
41   struct si_shader *shader;
42   struct si_screen *screen;
43   struct pipe_stream_output_info so;
44
45   gl_shader_stage stage;
46
47   /* For clamping the non-constant index in resource indexing: */
48   unsigned num_const_buffers;
49   unsigned num_shader_buffers;
50   unsigned num_images;
51   unsigned num_samplers;
52
53   struct ac_shader_args args;
54   struct ac_shader_abi abi;
55
56   LLVMBasicBlockRef merged_wrap_if_entry_block;
57   int merged_wrap_if_label;
58
59   LLVMValueRef main_fn;
60   LLVMTypeRef return_type;
61
62   struct ac_arg const_and_shader_buffers;
63   struct ac_arg samplers_and_images;
64
65   /* For merged shaders, the per-stage descriptors for the stage other
66    * than the one we're processing, used to pass them through from the
67    * first stage to the second.
68    */
69   struct ac_arg other_const_and_shader_buffers;
70   struct ac_arg other_samplers_and_images;
71
72   struct ac_arg internal_bindings;
73   struct ac_arg bindless_samplers_and_images;
74   struct ac_arg small_prim_cull_info;
75   struct ac_arg gs_attr_address;
76   /* API VS */
77   struct ac_arg vb_descriptors[5];
78   struct ac_arg vertex_index0;
79   /* VS states and layout of LS outputs / TCS inputs at the end
80    *   [0] = clamp vertex color
81    *   [1] = indexed
82    *   [2:3] = NGG: output primitive type
83    *   [4:5] = NGG: provoking vertex index
84    *   [6]   = NGG: streamout queries enabled
85    *   [7:10] = NGG: small prim filter precision = num_samples / quant_mode,
86    *            but in reality it's: 1/2^n, from 1/16 to 1/4096 = 1/2^4 to 1/2^12
87    *            Only the first 4 bits of the exponent are stored.
88    *            Set it like this: (fui(num_samples / quant_mode) >> 23)
89    *            Expand to FP32 like this: ((0x70 | value) << 23);
90    *            With 0x70 = 112, we get 2^(112 + value - 127) = 2^(value - 15)
91    *            = 1/2^(15 - value) in FP32
92    *   [11:23] = stride between patches in DW = num_inputs * num_vertices * 4
93    *             max = 32*32*4 + 32*4
94    *   [24:31] = stride between vertices in DW = num_inputs * 4
95    *             max = 32*4
96    */
97   struct ac_arg vs_state_bits;
98   struct ac_arg vs_blit_inputs;
99
100   /* API TCS & TES */
101   /* Layout of TCS outputs in the offchip buffer
102    * # 6 bits
103    *   [0:5] = the number of patches per threadgroup - 1, max = 63
104    * # 5 bits
105    *   [6:10] = the number of output vertices per patch - 1, max = 31
106    * # 21 bits
107    *   [11:31] = the offset of per patch attributes in the buffer in bytes.
108    *             max = NUM_PATCHES*32*32*16 = 1M
109    */
110   struct ac_arg tcs_offchip_layout;
111
112   /* API TCS */
113   /* Offsets where TCS outputs and TCS patch outputs live in LDS (<= 16K):
114    *   [0:15] = TCS output patch0 offset / 4, max = 16K / 4 = 4K
115    *   [16:31] = TCS output patch0 offset for per-patch / 4, max = 16K / 4 = 4K
116    */
117   struct ac_arg tcs_out_lds_offsets;
118   /* Layout of TCS outputs / TES inputs:
119    *   [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
120    *            max = 32*32*4 + 32*4 = 4224
121    *   [13:18] = gl_PatchVerticesIn, max = 32
122    *   [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
123    */
124   struct ac_arg tcs_out_lds_layout;
125
126   /* API TES */
127   struct ac_arg tes_offchip_addr;
128   /* PS */
129   struct ac_arg pos_fixed_pt;
130   /* CS */
131   struct ac_arg block_size;
132   struct ac_arg cs_user_data;
133   struct ac_arg cs_shaderbuf[3];
134   struct ac_arg cs_image[3];
135
136   struct ac_llvm_compiler *compiler;
137
138   /* Preloaded descriptors. */
139   LLVMValueRef esgs_ring;
140   LLVMValueRef gsvs_ring[4];
141   LLVMValueRef tess_offchip_ring;
142
143   LLVMValueRef gs_next_vertex[4];
144   LLVMValueRef gs_curprim_verts[4];
145   LLVMValueRef gs_generated_prims[4];
146   LLVMValueRef gs_ngg_emit;
147   LLVMValueRef gs_ngg_scratch;
148   LLVMValueRef return_value;
149
150   LLVMValueRef gs_emitted_vertices;
151};
152
153static inline struct si_shader_context *si_shader_context_from_abi(struct ac_shader_abi *abi)
154{
155   return container_of(abi, struct si_shader_context, abi);
156}
157
158/* si_shader.c */
159bool si_is_multi_part_shader(struct si_shader *shader);
160bool si_is_merged_shader(struct si_shader *shader);
161void si_add_arg_checked(struct ac_shader_args *args, enum ac_arg_regfile file, unsigned registers,
162                        enum ac_arg_type type, struct ac_arg *arg, unsigned idx);
163void si_init_shader_args(struct si_shader_context *ctx, bool ngg_cull_shader);
164unsigned si_get_max_workgroup_size(const struct si_shader *shader);
165bool si_vs_needs_prolog(const struct si_shader_selector *sel,
166                        const struct si_vs_prolog_bits *prolog_key,
167                        const union si_shader_key *key, bool ngg_cull_shader, bool is_gs);
168void si_get_vs_prolog_key(const struct si_shader_info *info, unsigned num_input_sgprs,
169                          bool ngg_cull_shader, const struct si_vs_prolog_bits *prolog_key,
170                          struct si_shader *shader_out, union si_shader_part_key *key);
171struct nir_shader *si_get_nir_shader(struct si_shader *shader, bool *free_nir,
172                                     uint64_t tcs_vgpr_only_inputs);
173void si_get_tcs_epilog_key(struct si_shader *shader, union si_shader_part_key *key);
174bool si_need_ps_prolog(const union si_shader_part_key *key);
175void si_get_ps_prolog_key(struct si_shader *shader, union si_shader_part_key *key,
176                          bool separate_prolog);
177void si_get_ps_epilog_key(struct si_shader *shader, union si_shader_part_key *key);
178void si_fix_resource_usage(struct si_screen *sscreen, struct si_shader *shader);
179
180/* gfx10_shader_ngg.c */
181LLVMValueRef gfx10_get_thread_id_in_tg(struct si_shader_context *ctx);
182bool gfx10_ngg_export_prim_early(struct si_shader *shader);
183void gfx10_ngg_build_sendmsg_gs_alloc_req(struct si_shader_context *ctx);
184void gfx10_ngg_build_export_prim(struct si_shader_context *ctx, LLVMValueRef user_edgeflags[3],
185                                 LLVMValueRef prim_passthrough);
186void gfx10_ngg_culling_build_end(struct si_shader_context *ctx);
187void gfx10_ngg_build_end(struct si_shader_context *ctx);
188void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx, unsigned stream, LLVMValueRef *addrs);
189void gfx10_ngg_gs_emit_begin(struct si_shader_context *ctx);
190void gfx10_ngg_gs_build_end(struct si_shader_context *ctx);
191unsigned gfx10_ngg_get_scratch_dw_size(struct si_shader *shader);
192bool gfx10_ngg_calculate_subgroup_info(struct si_shader *shader);
193
194/* si_shader_llvm.c */
195bool si_compile_llvm(struct si_screen *sscreen, struct si_shader_binary *binary,
196                     struct ac_shader_config *conf, struct ac_llvm_compiler *compiler,
197                     struct ac_llvm_context *ac, struct util_debug_callback *debug,
198                     gl_shader_stage stage, const char *name, bool less_optimized);
199void si_llvm_context_init(struct si_shader_context *ctx, struct si_screen *sscreen,
200                          struct ac_llvm_compiler *compiler, unsigned wave_size);
201void si_llvm_create_func(struct si_shader_context *ctx, const char *name, LLVMTypeRef *return_types,
202                         unsigned num_return_elems, unsigned max_workgroup_size);
203void si_llvm_create_main_func(struct si_shader_context *ctx, bool ngg_cull_shader);
204void si_llvm_optimize_module(struct si_shader_context *ctx);
205void si_llvm_dispose(struct si_shader_context *ctx);
206LLVMValueRef si_buffer_load_const(struct si_shader_context *ctx, LLVMValueRef resource,
207                                  LLVMValueRef offset);
208void si_llvm_build_ret(struct si_shader_context *ctx, LLVMValueRef ret);
209LLVMValueRef si_insert_input_ret(struct si_shader_context *ctx, LLVMValueRef ret,
210                                 struct ac_arg param, unsigned return_index);
211LLVMValueRef si_insert_input_ret_float(struct si_shader_context *ctx, LLVMValueRef ret,
212                                       struct ac_arg param, unsigned return_index);
213LLVMValueRef si_insert_input_ptr(struct si_shader_context *ctx, LLVMValueRef ret,
214                                 struct ac_arg param, unsigned return_index);
215LLVMValueRef si_prolog_get_internal_bindings(struct si_shader_context *ctx);
216void si_llvm_declare_esgs_ring(struct si_shader_context *ctx);
217LLVMValueRef si_unpack_param(struct si_shader_context *ctx, struct ac_arg param, unsigned rshift,
218                             unsigned bitwidth);
219LLVMValueRef si_get_primitive_id(struct si_shader_context *ctx, unsigned swizzle);
220void si_build_wrapper_function(struct si_shader_context *ctx, LLVMValueRef *parts,
221                               unsigned num_parts, unsigned main_part,
222                               unsigned next_shader_first_part, bool same_thread_count);
223bool si_llvm_translate_nir(struct si_shader_context *ctx, struct si_shader *shader,
224                           struct nir_shader *nir, bool free_nir, bool ngg_cull_shader);
225bool si_llvm_compile_shader(struct si_screen *sscreen, struct ac_llvm_compiler *compiler,
226                            struct si_shader *shader, const struct pipe_stream_output_info *so,
227                            struct util_debug_callback *debug, struct nir_shader *nir,
228                            bool free_nir);
229
230/* si_shader_llvm_gs.c */
231LLVMValueRef si_is_es_thread(struct si_shader_context *ctx);
232LLVMValueRef si_is_gs_thread(struct si_shader_context *ctx);
233void si_llvm_es_build_end(struct si_shader_context *ctx);
234void si_preload_esgs_ring(struct si_shader_context *ctx);
235void si_preload_gs_rings(struct si_shader_context *ctx);
236void si_llvm_gs_build_end(struct si_shader_context *ctx);
237void si_llvm_init_gs_callbacks(struct si_shader_context *ctx);
238
239/* si_shader_llvm_tess.c */
240LLVMValueRef si_get_rel_patch_id(struct si_shader_context *ctx);
241LLVMValueRef si_get_tcs_in_vertex_dw_stride(struct si_shader_context *ctx);
242LLVMValueRef si_get_num_tcs_out_vertices(struct si_shader_context *ctx);
243void si_llvm_preload_tess_rings(struct si_shader_context *ctx);
244void si_llvm_ls_build_end(struct si_shader_context *ctx);
245void si_llvm_build_tcs_epilog(struct si_shader_context *ctx, union si_shader_part_key *key);
246void si_llvm_tcs_build_end(struct si_shader_context *ctx);
247void si_llvm_init_tcs_callbacks(struct si_shader_context *ctx);
248
249/* si_shader_llvm_ps.c */
250LLVMValueRef si_get_sample_id(struct si_shader_context *ctx);
251void si_llvm_build_ps_prolog(struct si_shader_context *ctx, union si_shader_part_key *key);
252void si_llvm_build_ps_epilog(struct si_shader_context *ctx, union si_shader_part_key *key);
253void si_llvm_build_monolithic_ps(struct si_shader_context *ctx, struct si_shader *shader);
254void si_llvm_ps_build_end(struct si_shader_context *ctx);
255void si_llvm_init_ps_callbacks(struct si_shader_context *ctx);
256
257/* si_shader_llvm_resources.c */
258void si_llvm_init_resource_callbacks(struct si_shader_context *ctx);
259
260/* si_shader_llvm_vs.c */
261void si_llvm_clipvertex_to_clipdist(struct si_shader_context *ctx,
262                                    struct ac_export_args clipdist[2], LLVMValueRef clipvertex[4]);
263void si_llvm_streamout_store_output(struct si_shader_context *ctx, LLVMValueRef const *so_buffers,
264                                    LLVMValueRef const *so_write_offsets,
265                                    struct pipe_stream_output *stream_out,
266                                    struct si_shader_output_values *shader_out);
267void si_llvm_emit_streamout(struct si_shader_context *ctx, struct si_shader_output_values *outputs,
268                            unsigned noutput, unsigned stream);
269void si_llvm_build_vs_exports(struct si_shader_context *ctx, LLVMValueRef num_export_threads,
270                              struct si_shader_output_values *outputs, unsigned noutput);
271void si_llvm_vs_build_end(struct si_shader_context *ctx);
272void si_llvm_build_vs_prolog(struct si_shader_context *ctx, union si_shader_part_key *key);
273void si_llvm_init_vs_callbacks(struct si_shader_context *ctx, bool ngg_cull_shader);
274
275#endif
276