xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/llvmpipe/lp_state_fs.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  **************************************************************************/
27 
28 
29 #ifndef LP_STATE_FS_H_
30 #define LP_STATE_FS_H_
31 
32 
33 #include "util/list.h"
34 #include "util/compiler.h"
35 #include "pipe/p_state.h"
36 #include "gallivm/lp_bld_sample.h" /* for struct lp_sampler_static_state */
37 #include "gallivm/lp_bld_jit_sample.h"
38 #include "gallivm/lp_bld_tgsi.h" /* for lp_tgsi_info */
39 #include "lp_bld_interp.h" /* for struct lp_shader_input */
40 #include "util/u_inlines.h"
41 #include "lp_jit.h"
42 
43 struct lp_fragment_shader;
44 
45 
46 /** Indexes into jit_function[] array */
47 #define RAST_WHOLE 0
48 #define RAST_EDGE_TEST 1
49 
50 
51 enum lp_fs_kind
52 {
53    LP_FS_KIND_GENERAL = 0,
54    LP_FS_KIND_BLIT_RGBA,
55    LP_FS_KIND_BLIT_RGB1,
56    LP_FS_KIND_AERO_MINIFICATION,
57    LP_FS_KIND_LLVM_LINEAR
58 };
59 
60 
61 struct lp_depth_state
62 {
63    unsigned enabled:1;         /**< depth test enabled? */
64    unsigned writemask:1;       /**< allow depth buffer writes? */
65    unsigned func:3;            /**< depth test func (PIPE_FUNC_x) */
66 };
67 
68 struct lp_fragment_shader_variant_key
69 {
70    struct lp_depth_state depth;
71    struct pipe_stencil_state stencil[2];
72    struct pipe_blend_state blend;
73 
74    struct {
75       unsigned enabled:1;
76       unsigned func:3;
77    } alpha;
78 
79    unsigned nr_cbufs:8;
80    unsigned nr_samplers:8;      /* actually derivable from just the shader */
81    unsigned nr_sampler_views:8; /* actually derivable from just the shader */
82    unsigned nr_images:8;        /* actually derivable from just the shader */
83    unsigned flatshade:1;
84    unsigned occlusion_count:1;
85    unsigned resource_1d:1;
86    unsigned depth_clamp:1;
87    unsigned multisample:1;
88    unsigned no_ms_sample_mask_out:1;
89    unsigned restrict_depth_values:1;
90 
91    enum pipe_format zsbuf_format;
92    enum pipe_format cbuf_format[PIPE_MAX_COLOR_BUFS];
93 
94    uint8_t cbuf_nr_samples[PIPE_MAX_COLOR_BUFS];
95    uint8_t zsbuf_nr_samples;
96    uint8_t coverage_samples;
97    uint8_t min_samples;
98    /* followed by variable number of samplers + images */
99 };
100 
101 #define LP_FS_MAX_VARIANT_KEY_SIZE                                      \
102    (sizeof(struct lp_fragment_shader_variant_key) +                     \
103     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct lp_sampler_static_state) +\
104     PIPE_MAX_SHADER_IMAGES * sizeof(struct lp_image_static_state))
105 
106 static inline size_t
lp_fs_variant_key_size(unsigned nr_samplers,unsigned nr_images)107 lp_fs_variant_key_size(unsigned nr_samplers, unsigned nr_images)
108 {
109    return (sizeof(struct lp_fragment_shader_variant_key) +
110            nr_samplers * sizeof(struct lp_sampler_static_state) +
111            nr_images * sizeof(struct lp_image_static_state));
112 }
113 
114 static inline struct lp_sampler_static_state *
lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key * key)115 lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key *key)
116 {
117    return (struct lp_sampler_static_state *)&(key[1]);
118 }
119 
120 static inline struct lp_sampler_static_state *
lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key * key,int idx)121 lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key *key, int idx)
122 {
123    if (idx >= key->nr_samplers)
124       return NULL;
125    return &lp_fs_variant_key_samplers(key)[idx];
126 }
127 
128 static inline struct lp_image_static_state *
lp_fs_variant_key_images(struct lp_fragment_shader_variant_key * key)129 lp_fs_variant_key_images(struct lp_fragment_shader_variant_key *key)
130 {
131    return (struct lp_image_static_state *)
132       &(lp_fs_variant_key_samplers(key)[MAX2(key->nr_samplers,
133                                              key->nr_sampler_views)]);
134 }
135 
136 /** doubly-linked list item */
137 struct lp_fs_variant_list_item
138 {
139    struct list_head list;
140    struct lp_fragment_shader_variant *base;
141 };
142 
143 
144 struct lp_fragment_shader_variant
145 {
146    /*
147     * Whether some primitives can be opaque.
148     */
149    unsigned potentially_opaque:1;
150 
151    unsigned opaque:1;
152    unsigned blit:1;
153    unsigned linear_input_mask:16;
154    struct pipe_reference reference;
155 
156    struct gallivm_state *gallivm;
157 
158    LLVMTypeRef jit_context_type;
159    LLVMTypeRef jit_context_ptr_type;
160    LLVMTypeRef jit_thread_data_type;
161    LLVMTypeRef jit_resources_type;
162    LLVMTypeRef jit_resources_ptr_type;
163    LLVMTypeRef jit_thread_data_ptr_type;
164    LLVMTypeRef jit_linear_context_type;
165    LLVMTypeRef jit_linear_context_ptr_type;
166    LLVMTypeRef jit_linear_func_type;
167    LLVMTypeRef jit_linear_inputs_type;
168    LLVMTypeRef jit_linear_textures_type;
169 
170    LLVMValueRef function[2]; // [RAST_WHOLE], [RAST_EDGE_TEST]
171    char *function_name[2];
172 
173    lp_jit_frag_func jit_function[2]; // [RAST_WHOLE], [RAST_EDGE_TEST]
174 
175    lp_jit_linear_func jit_linear;
176    lp_jit_linear_func jit_linear_blit;
177 
178    /* Functions within the linear path:
179     */
180    LLVMValueRef linear_function;
181    char *linear_function_name;
182    lp_jit_linear_llvm_func jit_linear_llvm;
183 
184    /* Bitmask to say what cbufs are unswizzled */
185    unsigned unswizzled_cbufs;
186 
187    /* Total number of LLVM instructions generated */
188    unsigned nr_instrs;
189 
190    struct lp_fs_variant_list_item list_item_global, list_item_local;
191    struct lp_fragment_shader *shader;
192 
193    /* For debugging/profiling purposes */
194    unsigned no;
195 
196    /* key is variable-sized, must be last */
197    struct lp_fragment_shader_variant_key key;
198 };
199 
200 
201 /** Subclass of pipe_shader_state */
202 struct lp_fragment_shader
203 {
204    struct pipe_shader_state base;
205 
206    struct pipe_reference reference;
207    struct lp_tgsi_info info;
208 
209    /* Analysis results */
210    enum lp_fs_kind kind;
211 
212    struct lp_fs_variant_list_item variants;
213 
214    struct draw_fragment_shader *draw_data;
215 
216    /* For debugging/profiling purposes */
217    unsigned variant_key_size;
218    unsigned no;
219    unsigned variants_created;
220    unsigned variants_cached;
221 
222    /** Fragment shader input interpolation info */
223    struct lp_shader_input inputs[PIPE_MAX_SHADER_INPUTS];
224 };
225 
226 
227 void
228 llvmpipe_fs_analyse_nir(struct lp_fragment_shader *shader);
229 
230 void
231 llvmpipe_fs_variant_fastpath(struct lp_fragment_shader_variant *variant);
232 
233 void
234 llvmpipe_fs_variant_linear_fastpath(struct lp_fragment_shader_variant *variant);
235 
236 void
237 llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context *lp,
238                                 struct lp_fragment_shader *shader,
239                                 struct lp_fragment_shader_variant *variant);
240 
241 void
242 lp_debug_fs_variant(struct lp_fragment_shader_variant *variant);
243 
244 const char *
245 lp_debug_fs_kind(enum lp_fs_kind kind);
246 
247 void
248 lp_linear_check_variant(struct lp_fragment_shader_variant *variant);
249 
250 void
251 llvmpipe_destroy_fs(struct llvmpipe_context *llvmpipe,
252                     struct lp_fragment_shader *shader);
253 
254 static inline void
lp_fs_reference(struct llvmpipe_context * llvmpipe,struct lp_fragment_shader ** ptr,struct lp_fragment_shader * shader)255 lp_fs_reference(struct llvmpipe_context *llvmpipe,
256                 struct lp_fragment_shader **ptr,
257                 struct lp_fragment_shader *shader)
258 {
259    struct lp_fragment_shader *old_ptr = *ptr;
260    if (pipe_reference(old_ptr ? &(*ptr)->reference : NULL,
261                       shader ? &shader->reference : NULL)) {
262       llvmpipe_destroy_fs(llvmpipe, old_ptr);
263    }
264    *ptr = shader;
265 }
266 
267 void
268 llvmpipe_destroy_shader_variant(struct llvmpipe_context *lp,
269                                 struct lp_fragment_shader_variant *variant);
270 
271 static inline void
lp_fs_variant_reference(struct llvmpipe_context * llvmpipe,struct lp_fragment_shader_variant ** ptr,struct lp_fragment_shader_variant * variant)272 lp_fs_variant_reference(struct llvmpipe_context *llvmpipe,
273                         struct lp_fragment_shader_variant **ptr,
274                         struct lp_fragment_shader_variant *variant)
275 {
276    struct lp_fragment_shader_variant *old_ptr = *ptr;
277    if (pipe_reference(old_ptr ? &(*ptr)->reference : NULL,
278                       variant ? &variant->reference : NULL)) {
279       llvmpipe_destroy_shader_variant(llvmpipe, old_ptr);
280    }
281    *ptr = variant;
282 }
283 
284 #endif /* LP_STATE_FS_H_ */
285