xref: /aosp_15_r20/external/virglrenderer/src/vrend_shader.h (revision bbecb9d118dfdb95f99bd754f8fa9be01f189df3)
1 /**************************************************************************
2  *
3  * Copyright (C) 2014 Red Hat Inc.
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 #ifndef VREND_SHADER_H
26 #define VREND_SHADER_H
27 
28 #include "pipe/p_state.h"
29 #include "pipe/p_shader_tokens.h"
30 
31 #include "vrend_strbuf.h"
32 
33 #define VIRGL_NUM_CLIP_PLANES 8
34 
35 #define VREND_POLYGON_STIPPLE_SIZE 32
36 
37 #define VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH \
38    ((PIPE_MAX_SHADER_SAMPLER_VIEWS + 63) / 64)
39 
40 enum gl_advanced_blend_mode
41 {
42    BLEND_NONE = 0,
43    BLEND_MULTIPLY,
44    BLEND_SCREEN,
45    BLEND_OVERLAY,
46    BLEND_DARKEN,
47    BLEND_LIGHTEN,
48    BLEND_COLORDODGE,
49    BLEND_COLORBURN,
50    BLEND_HARDLIGHT,
51    BLEND_SOFTLIGHT,
52    BLEND_DIFFERENCE,
53    BLEND_EXCLUSION,
54    BLEND_HSL_HUE,
55    BLEND_HSL_SATURATION,
56    BLEND_HSL_COLOR,
57    BLEND_HSL_LUMINOSITY,
58    BLEND_ALL
59 };
60 
61 
62 /* need to store patching info for interpolation */
63 struct vrend_interp_info {
64    unsigned semantic_name : 6;
65    unsigned semantic_index : 16;
66    unsigned interpolate : 3;
67    unsigned location : 3;
68 };
69 
70 struct vrend_array {
71    int first;
72    int array_size;
73 };
74 
75 struct vrend_layout_info {
76    unsigned name : 6;
77    unsigned sid : 16 ;
78    unsigned location : 16 ;
79    unsigned array_id : 16 ;
80 };
81 
82 struct vrend_fs_shader_info {
83    int num_interps;
84    int glsl_ver;
85    bool has_sample_input;
86    bool has_noperspective;
87    struct vrend_interp_info interpinfo[PIPE_MAX_SHADER_INPUTS];
88 };
89 
90 struct vrend_shader_info_out {
91    uint8_t num_generic_and_patch;
92    uint8_t num_indirect_generic;
93    uint8_t num_indirect_patch;
94    bool guest_sent_io_arrays;
95 };
96 
97 struct vrend_shader_info_in {
98    uint64_t generic_emitted_mask;
99    uint64_t texcoord_emitted_mask;
100    bool indirect_generic_or_patch : 1;
101    bool use_pervertex : 1;
102 };
103 
104 struct vrend_shader_io_array {
105    enum tgsi_semantic name : 6;
106    uint32_t sid : 6;
107    uint32_t size : 6;
108    uint32_t array_id : 6;
109    uint32_t padding : 8;
110 };
111 
112 struct vrend_shader_io_array_info {
113    uint32_t num_arrays;
114    struct vrend_shader_io_array layout[16];
115 };
116 
117 struct vrend_shader_info {
118    uint32_t invariant_outputs[4];
119    uint64_t in_generic_emitted_mask;
120    uint64_t in_texcoord_emitted_mask;
121 
122    uint64_t out_generic_emitted_mask;
123    uint64_t out_patch_emitted_mask;
124 
125    struct vrend_shader_io_array_info output_arrays;
126 
127    struct vrend_array *sampler_arrays;
128    struct vrend_array *image_arrays;
129    char **so_names;
130    struct pipe_stream_output_info so_info;
131 
132    /* 8 cbufs + depth + stencil + samplemask */
133    int8_t fs_output_layout[12];
134 
135    uint32_t samplers_used_mask;
136    uint32_t images_used_mask;
137    uint32_t ubo_used_mask;
138    uint32_t ssbo_used_mask;
139    uint32_t shadow_samp_mask;
140    uint32_t attrib_input_mask;
141    uint32_t fs_blend_equation_advanced;
142    uint32_t fog_input_mask;
143    uint32_t fog_output_mask;
144 
145    int num_consts;
146    int num_inputs;
147    int num_outputs;
148    int gs_out_prim;
149    int tes_prim;
150    int num_sampler_arrays;
151    int num_image_arrays;
152 
153    uint8_t out_texcoord_emitted_mask;
154    uint8_t ubo_indirect : 1;
155    uint8_t tes_point_mode : 1;
156    uint8_t gles_use_tex_query_level : 1;
157    uint8_t separable_program : 1;
158    uint8_t has_input_arrays : 1;
159    uint8_t has_output_arrays : 1;
160    uint8_t use_pervertex_in : 1;
161 };
162 
163 struct vrend_variable_shader_info {
164    struct vrend_fs_shader_info fs_info;
165    uint32_t num_in_clip:4;
166    uint32_t num_in_cull:4;
167    uint32_t num_out_clip:4;
168    uint32_t num_out_cull:4;
169    int num_ucp;
170    int legacy_color_bits;
171 };
172 
173 struct vrend_shader_key {
174    uint64_t out_generic_expected_mask;
175    uint64_t out_texcoord_expected_mask;
176 
177    uint64_t in_generic_expected_mask;
178    uint64_t in_texcoord_expected_mask;
179    uint64_t in_patch_expected_mask;
180 
181    uint32_t force_invariant_inputs[4];
182 
183    struct vrend_fs_shader_info fs_info;
184    struct vrend_shader_io_array_info in_arrays;
185 
186    union {
187       struct {
188          uint8_t surface_component_bits[PIPE_MAX_COLOR_BUFS];
189          uint32_t coord_replace;
190          uint8_t swizzle_output_rgb_to_bgr;
191          uint8_t needs_manual_srgb_encode_bitmask;
192          uint8_t cbufs_are_a8_bitmask;
193          uint8_t cbufs_signed_int_bitmask;
194          uint8_t cbufs_unsigned_int_bitmask;
195          uint32_t logicop_func : 4;
196          uint32_t logicop_enabled : 1;
197          uint32_t prim_is_points : 1;
198          uint32_t lower_left_origin : 1;
199          uint32_t available_color_in_bits : 4;
200       } fs;
201 
202       struct {
203          uint32_t attrib_signed_int_bitmask;
204          uint32_t attrib_unsigned_int_bitmask;
205          uint32_t attrib_zyxw_bitmask;
206          uint32_t fog_fixup_mask;
207       } vs;
208 
209       struct {
210          uint32_t emit_clip_distance : 1;
211       } gs;
212    };
213 
214    uint64_t sampler_views_lower_swizzle_mask[VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH];
215    uint64_t sampler_views_emulated_rect_mask[VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH];
216    uint16_t tex_swizzle[PIPE_MAX_SHADER_SAMPLER_VIEWS];
217 
218    uint8_t alpha_test;
219    uint8_t num_in_cull : 4;
220    uint8_t num_in_clip : 4;
221    uint8_t num_out_cull : 4;
222    uint8_t num_out_clip : 4;
223    uint8_t pstipple_enabled : 1;
224    uint8_t add_alpha_test : 1;
225    uint8_t color_two_side : 1;
226    uint8_t gs_present : 1;
227    uint8_t tcs_present : 1;
228    uint8_t tes_present : 1;
229    uint8_t flatshade : 1;
230    uint8_t require_input_arrays : 1;
231    uint8_t require_output_arrays : 1;
232    uint8_t use_pervertex_in : 1;
233 };
234 
235 struct vrend_shader_cfg {
236    uint32_t glsl_version : 12;
237    uint32_t max_draw_buffers : 4;
238    uint32_t max_shader_patch_varyings : 6;
239    uint32_t use_gles : 1;
240    uint32_t use_core_profile : 1;
241    uint32_t use_explicit_locations : 1;
242    uint32_t has_arrays_of_arrays : 1;
243    uint32_t has_gpu_shader5 : 1;
244    uint32_t has_es31_compat : 1;
245    uint32_t has_conservative_depth : 1;
246    uint32_t use_integer : 1;
247    uint32_t has_dual_src_blend : 1;
248    uint32_t has_fbfetch_coherent : 1;
249    uint32_t has_cull_distance : 1;
250    uint32_t has_nopersective : 1;
251    uint32_t has_texture_shadow_lod : 1;
252 };
253 
254 struct vrend_context;
255 
256 #define SHADER_MAX_STRINGS 3
257 #define SHADER_STRING_VER_EXT 0
258 #define SHADER_STRING_HDR 1
259 
260 bool vrend_convert_shader(const struct vrend_context *rctx,
261                           const struct vrend_shader_cfg *cfg,
262                           const struct tgsi_token *tokens,
263                           uint32_t req_local_mem,
264                           const struct vrend_shader_key *key,
265                           struct vrend_shader_info *sinfo,
266                           struct vrend_variable_shader_info *var_sinfo,
267                           struct vrend_strarray *shader);
268 
269 const char *vrend_shader_samplertypeconv(bool use_gles, int sampler_type);
270 
271 char vrend_shader_samplerreturnconv(enum tgsi_return_type type);
272 
273 int vrend_shader_lookup_sampler_array(const struct vrend_shader_info *sinfo, int index);
274 
275 bool vrend_shader_create_passthrough_tcs(const struct vrend_context *ctx,
276                                          const struct vrend_shader_cfg *cfg,
277                                          const struct tgsi_token *vs_info,
278                                          const struct vrend_shader_key *key,
279                                          const float tess_factors[6],
280                                          struct vrend_shader_info *sinfo,
281                                          struct vrend_strarray *shader,
282                                          int vertices_per_patch);
283 
284 bool vrend_shader_needs_alpha_func(const struct vrend_shader_key *key);
285 
286 bool vrend_shader_query_separable_program(const struct tgsi_token *tokens,
287                                           const struct vrend_shader_cfg *cfg);
288 
vrend_shader_sampler_views_mask_get(const uint64_t mask[static VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH],int index)289 static inline bool vrend_shader_sampler_views_mask_get(
290    const uint64_t mask[static VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH],
291    int index)
292 {
293    return (mask[index / 64] >> (index % 64)) & 1;
294 }
295 
vrend_shader_sampler_views_mask_set(uint64_t mask[static VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH],int index)296 static inline void vrend_shader_sampler_views_mask_set(
297    uint64_t mask[static VREND_SHADER_SAMPLER_VIEWS_MASK_LENGTH],
298    int index)
299 {
300    mask[index / 64] |= 1ull << (index % 64);
301 }
302 
303 #endif
304