1 /*
2  * Copyright 2017 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef AC_SHADER_ABI_H
25 #define AC_SHADER_ABI_H
26 
27 #include "ac_shader_args.h"
28 #include "ac_shader_util.h"
29 #include "compiler/shader_enums.h"
30 #include <llvm-c/Core.h>
31 
32 #include <assert.h>
33 
34 #define AC_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
35 
36 #define AC_MAX_INLINE_PUSH_CONSTS 8
37 
38 /* Document the shader ABI during compilation. This is what allows radeonsi and
39  * radv to share a compiler backend.
40  */
41 struct ac_shader_abi {
42    LLVMValueRef outputs[AC_LLVM_MAX_OUTPUTS * 4];
43 
44    /* These input registers sometimes need to be fixed up. */
45    LLVMValueRef vertex_id;
46    LLVMValueRef instance_id;
47    LLVMValueRef persp_centroid, linear_centroid;
48    LLVMValueRef color0, color1;
49    LLVMValueRef user_data;
50 
51    /* Varying -> attribute number mapping. Also NIR-only */
52    unsigned fs_input_attr_indices[MAX_VARYING];
53 
54    void (*export_vertex)(struct ac_shader_abi *abi);
55 
56    void (*emit_outputs)(struct ac_shader_abi *abi);
57 
58    void (*emit_vertex)(struct ac_shader_abi *abi, unsigned stream, LLVMValueRef *addrs);
59 
60    void (*emit_primitive)(struct ac_shader_abi *abi, unsigned stream);
61 
62    void (*emit_vertex_with_counter)(struct ac_shader_abi *abi, unsigned stream,
63                                     LLVMValueRef vertexidx, LLVMValueRef *addrs);
64 
65    LLVMValueRef (*load_inputs)(struct ac_shader_abi *abi,
66                                unsigned driver_location, unsigned component,
67                                unsigned num_components, unsigned vertex_index,
68                                LLVMTypeRef type);
69 
70    LLVMValueRef (*load_tess_varyings)(struct ac_shader_abi *abi, LLVMTypeRef type,
71                                       LLVMValueRef vertex_index, LLVMValueRef param_index,
72                                       unsigned driver_location, unsigned component,
73                                       unsigned num_components,
74                                       bool load_inputs, bool vertex_index_is_invoc_id);
75 
76    void (*store_tcs_outputs)(struct ac_shader_abi *abi,
77                              LLVMValueRef vertex_index, LLVMValueRef param_index,
78                              LLVMValueRef src, unsigned writemask,
79                              unsigned component, unsigned location, unsigned driver_location);
80 
81    LLVMValueRef (*load_patch_vertices_in)(struct ac_shader_abi *abi);
82 
83    LLVMValueRef (*load_ring_tess_offchip)(struct ac_shader_abi *abi);
84 
85    LLVMValueRef (*load_ring_tess_factors)(struct ac_shader_abi *abi);
86 
87    LLVMValueRef (*load_ring_esgs)(struct ac_shader_abi *abi);
88 
89    LLVMValueRef (*load_tess_level)(struct ac_shader_abi *abi, unsigned varying_id,
90                                    bool load_default_state);
91 
92    LLVMValueRef (*load_ubo)(struct ac_shader_abi *abi, LLVMValueRef index);
93 
94    /**
95     * Load the descriptor for the given buffer.
96     *
97     * \param buffer the buffer as presented in NIR: this is the descriptor
98     *               in Vulkan, and the buffer index in OpenGL/Gallium
99     * \param write whether buffer contents will be written
100     * \param non_uniform whether the buffer descriptor is not assumed to be uniform
101     */
102    LLVMValueRef (*load_ssbo)(struct ac_shader_abi *abi, LLVMValueRef buffer, bool write, bool non_uniform);
103 
104    /**
105     * Load a descriptor associated to a sampler.
106     *
107     * \param descriptor_set the descriptor set index (only for Vulkan)
108     * \param base_index the base index of the sampler variable
109     * \param constant_index constant part of an array index (or 0, if the
110     *                       sampler variable is not an array)
111     * \param index non-constant part of an array index (may be NULL)
112     * \param desc_type the type of descriptor to load
113     * \param image whether the descriptor is loaded for an image operation
114     */
115    LLVMValueRef (*load_sampler_desc)(struct ac_shader_abi *abi, unsigned descriptor_set,
116                                      unsigned base_index, unsigned constant_index,
117                                      LLVMValueRef index, enum ac_descriptor_type desc_type,
118                                      bool image, bool write, bool bindless);
119 
120    LLVMValueRef (*load_sample_position)(struct ac_shader_abi *abi, LLVMValueRef sample_id);
121 
122    LLVMValueRef (*load_local_group_size)(struct ac_shader_abi *abi);
123 
124    LLVMValueRef (*load_sample_mask_in)(struct ac_shader_abi *abi);
125 
126    LLVMValueRef (*load_base_vertex)(struct ac_shader_abi *abi, bool non_indexed_is_zero);
127 
128    LLVMValueRef (*emit_fbfetch)(struct ac_shader_abi *abi);
129 
130    /* Whether to clamp the shadow reference value to [0,1]on GFX8. Radeonsi currently
131     * uses it due to promoting D16 to D32, but radv needs it off. */
132    bool clamp_shadow_reference;
133    bool interp_at_sample_force_center;
134 
135    /* Whether bounds checks are required */
136    bool robust_buffer_access;
137 
138    /* Check for Inf interpolation coeff */
139    bool kill_ps_if_inf_interp;
140 
141    /* Whether undef values must be converted to zero */
142    bool convert_undef_to_zero;
143 
144    /* Clamp div by 0 (so it won't produce NaN) */
145    bool clamp_div_by_zero;
146 
147    /* Whether to inline the compute dispatch size in user sgprs. */
148    bool load_grid_size_from_user_sgpr;
149 };
150 
151 #endif /* AC_SHADER_ABI_H */
152