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