xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_context.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2003 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 above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef ST_CONTEXT_H
29 #define ST_CONTEXT_H
30 
31 #include "main/arrayobj.h"
32 #include "main/mtypes.h"
33 #include "frontend/api.h"
34 #include "main/fbobject.h"
35 #include "state_tracker/st_atom.h"
36 #include "util/u_helpers.h"
37 #include "util/u_inlines.h"
38 #include "util/list.h"
39 #include "vbo/vbo.h"
40 #include "util/list.h"
41 #include "cso_cache/cso_context.h"
42 
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
49 struct draw_context;
50 struct draw_stage;
51 struct gen_mipmap_state;
52 struct st_context;
53 struct st_program;
54 struct u_upload_mgr;
55 
56 #define ST_THREAD_SCHEDULER_DISABLED 0xffffffff
57 
58 struct st_bitmap_cache
59 {
60    /** Window pos to render the cached image */
61    GLint xpos, ypos;
62    /** Bounds of region used in window coords */
63    GLint xmin, ymin, xmax, ymax;
64 
65    /** GL states */
66    struct gl_program *fp;
67    bool scissor_enabled;
68    bool clamp_frag_color;
69    GLfloat color[4];
70 
71    /** Bitmap's Z position */
72    GLfloat zpos;
73 
74    struct pipe_resource *texture;
75    struct pipe_transfer *trans;
76 
77    GLboolean empty;
78 
79    /** An I8 texture image: */
80    uint8_t *buffer;
81 };
82 
83 struct st_bound_handles
84 {
85    unsigned num_handles;
86    uint64_t *handles;
87 };
88 
89 
90 #define NUM_DRAWPIX_CACHE_ENTRIES 4
91 
92 struct drawpix_cache_entry
93 {
94    GLsizei width, height;
95    GLenum format, type;
96    const void *user_pointer;  /**< Last user 'pixels' pointer */
97    void *image;               /**< Copy of the glDrawPixels image data */
98    struct pipe_resource *texture;
99    unsigned age;
100 };
101 
102 
103 /*
104  * Node for a linked list of dead sampler views.
105  */
106 struct st_zombie_sampler_view_node
107 {
108    struct pipe_sampler_view *view;
109    struct list_head node;
110 };
111 
112 
113 /*
114  * Node for a linked list of dead shaders.
115  */
116 struct st_zombie_shader_node
117 {
118    void *shader;
119    enum pipe_shader_type type;
120    struct list_head node;
121 };
122 
123 typedef void (*st_update_func_t)(struct st_context *st);
124 
125 struct st_context
126 {
127    struct gl_context *ctx;
128    struct pipe_screen *screen;
129    struct pipe_context *pipe;
130    struct cso_context *cso_context;
131 
132    /* The list of state update functions. */
133    st_update_func_t update_functions[ST_NUM_ATOMS];
134 
135    struct pipe_frontend_screen *frontend_screen; /* e.g. dri_screen */
136    void *frontend_context; /* e.g. dri_context */
137 
138    struct draw_context *draw;  /**< For selection/feedback/rastpos only */
139    struct draw_stage *feedback_stage;  /**< For GL_FEEDBACK rendermode */
140    struct draw_stage *selection_stage;  /**< For GL_SELECT rendermode */
141    struct draw_stage *rastpos_stage;  /**< For glRasterPos */
142 
143    unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
144 
145    GLboolean clamp_frag_color_in_shader;
146    GLboolean clamp_vert_color_in_shader;
147    bool has_stencil_export; /**< can do shader stencil export? */
148    bool has_time_elapsed;
149    bool has_etc1;
150    bool has_etc2;
151    bool transcode_etc;
152    bool transcode_astc;
153    bool has_astc_2d_ldr;
154    bool has_astc_5x5_ldr;
155    bool astc_void_extents_need_denorm_flush;
156    bool has_s3tc;
157    bool has_rgtc;
158    bool has_latc;
159    bool has_bptc;
160    bool prefer_blit_based_texture_transfer;
161    bool allow_compute_based_texture_transfer;
162    bool force_compute_based_texture_transfer;
163    bool force_specialized_compute_transfer;
164    bool force_persample_in_shader;
165    bool has_shareable_shaders;
166    bool has_multi_draw_indirect;
167    bool has_indirect_partial_stride;
168    bool has_occlusion_query;
169    bool has_single_pipe_stat;
170    bool has_pipeline_stat;
171    bool has_indep_blend_enable;
172    bool has_indep_blend_func;
173    bool can_dither;
174    bool can_bind_const_buffer_as_vertex;
175    bool lower_flatshade;
176    bool lower_alpha_test;
177    bool lower_point_size;
178    bool add_point_size;
179    bool lower_two_sided_color;
180    bool lower_ucp;
181    bool prefer_real_buffer_in_constbuf0;
182    bool has_conditional_render;
183    bool lower_rect_tex;
184 
185    /* There are consequences for drivers wanting to call st_finalize_nir
186     * twice, once before shader caching and once after lowering for shader
187     * variants. If shader variants use lowering passes that are not ready
188     * for that, things can blow up.
189     *
190     * If this is true, st_finalize_nir and pipe_screen::finalize_nir will be
191     * called before the result is stored in the shader cache. If lowering for
192     * shader variants is invoked, the functions will be called again.
193     */
194    bool allow_st_finalize_nir_twice;
195 
196    /**
197     * If a shader can be created when we get its source.
198     * This means it has only 1 variant, not counting glBitmap and
199     * glDrawPixels.
200     */
201    bool shader_has_one_variant[MESA_SHADER_STAGES];
202 
203    bool needs_texcoord_semantic;
204    bool apply_texture_swizzle_to_border_color;
205    bool use_format_with_border_color;
206    bool alpha_border_color_is_not_w;
207    bool emulate_gl_clamp;
208 
209    bool draw_needs_minmax_index;
210    bool has_hw_atomics;
211 
212    bool validate_all_dirty_states;
213    bool can_null_texture;
214 
215    /* driver supports scissored clears */
216    bool can_scissor_clear;
217 
218    /* Some state is contained in constant objects.
219     * Other state is just parameter values.
220     */
221    struct {
222       struct pipe_blend_state               blend;
223       struct pipe_depth_stencil_alpha_state depth_stencil;
224       struct pipe_rasterizer_state          rasterizer;
225       struct pipe_sampler_state vert_samplers[PIPE_MAX_SAMPLERS];
226       struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
227       GLuint num_vert_samplers;
228       GLuint num_frag_samplers;
229       GLuint num_sampler_views[PIPE_SHADER_TYPES];
230       unsigned num_images[PIPE_SHADER_TYPES];
231       struct pipe_clip_state clip;
232       unsigned constbuf0_enabled_shader_mask;
233       unsigned fb_width;
234       unsigned fb_height;
235       unsigned fb_num_samples;
236       unsigned fb_num_layers;
237       unsigned fb_num_cb;
238       unsigned num_viewports;
239       struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
240       struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
241       struct {
242          unsigned num;
243          bool include;
244          struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
245       } window_rects;
246 
247       GLuint poly_stipple[32];  /**< In OpenGL's bottom-to-top order */
248 
249       GLuint fb_orientation;
250 
251       bool enable_sample_locations;
252       unsigned sample_locations_samples;
253       uint8_t sample_locations[
254          PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
255          PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
256    } state;
257 
258    /** This masks out unused shader resources. Only valid in draw calls. */
259    uint64_t active_states;
260 
261    /**
262     * The number of currently active queries (excluding timer queries).
263     * This is used to know if we need to pause any queries for meta ops.
264     */
265    unsigned active_queries;
266 
267    union {
268       struct {
269          struct gl_program *vp;    /**< Currently bound vertex program */
270          struct gl_program *tcp; /**< Currently bound tess control program */
271          struct gl_program *tep; /**< Currently bound tess eval program */
272          struct gl_program *gp;  /**< Currently bound geometry program */
273          struct gl_program *fp;  /**< Currently bound fragment program */
274          struct gl_program *cp;   /**< Currently bound compute program */
275       };
276       struct gl_program *current_program[MESA_SHADER_STAGES];
277    };
278 
279    struct st_common_variant *vp_variant;
280 
281    struct {
282       struct pipe_resource *pixelmap_texture;
283       struct pipe_sampler_view *pixelmap_sampler_view;
284    } pixel_xfer;
285 
286    /** for glBitmap */
287    struct {
288       struct pipe_rasterizer_state rasterizer;
289       struct pipe_sampler_state sampler;
290       enum pipe_format tex_format;
291       struct st_bitmap_cache cache;
292    } bitmap;
293 
294    /** for glDraw/CopyPixels */
295    struct {
296       void *zs_shaders[6];
297    } drawpix;
298 
299    /** Cache of glDrawPixels images */
300    struct {
301       struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
302       unsigned age;
303    } drawpix_cache;
304 
305    /** for glReadPixels */
306    struct {
307       struct pipe_resource *src;
308       struct pipe_resource *cache;
309       enum pipe_format dst_format;
310       unsigned level;
311       unsigned layer;
312       unsigned hits;
313    } readpix_cache;
314 
315    /** for glClear */
316    struct {
317       struct pipe_rasterizer_state raster;
318       struct pipe_viewport_state viewport;
319       void *vs;
320       void *fs;
321       void *vs_layered;
322       void *gs_layered;
323    } clear;
324 
325    /* For gl(Compressed)Tex(Sub)Image */
326    struct {
327       struct pipe_rasterizer_state raster;
328       struct pipe_blend_state upload_blend;
329       void *vs;
330       void *gs;
331       void *upload_fs[5][2];
332       /**
333        * For drivers supporting formatless storing
334        * (PIPE_CAP_IMAGE_STORE_FORMATTED) it is a pointer to the download FS;
335        * for those not supporting it, it is a pointer to an array of
336        * PIPE_FORMAT_COUNT elements, where each element is a pointer to the
337        * download FS using that PIPE_FORMAT as the storing format.
338        */
339       void *download_fs[5][PIPE_MAX_TEXTURE_TYPES][2];
340       struct hash_table *shaders;
341       bool upload_enabled;
342       bool download_enabled;
343       bool rgba_only;
344       bool layers;
345       bool use_gs;
346    } pbo;
347 
348    struct {
349       struct gl_program **progs;
350       struct pipe_resource *bc1_endpoint_buf;
351       struct pipe_sampler_view *astc_luts[5];
352       struct hash_table *astc_partition_tables;
353    } texcompress_compute;
354 
355    /** for drawing with st_util_vertex */
356    struct cso_velems_state util_velems;
357 
358    /** passthrough vertex shader matching the util_velem attributes */
359    void *passthrough_vs;
360 
361    enum pipe_texture_target internal_target;
362 
363    void *winsys_drawable_handle;
364 
365    bool uses_user_vertex_buffers;
366 
367    unsigned last_used_atomic_bindings[PIPE_SHADER_TYPES];
368    unsigned last_num_ssbos[PIPE_SHADER_TYPES];
369 
370    int32_t draw_stamp;
371    int32_t read_stamp;
372 
373    struct st_config_options options;
374 
375    enum pipe_reset_status reset_status;
376 
377    /* Array of bound texture/image handles which are resident in the context.
378     */
379    struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
380    struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
381 
382    /* Winsys buffers */
383    struct list_head winsys_buffers;
384 
385    /* Throttling for texture uploads and similar operations to limit memory
386     * usage by limiting the number of in-flight operations based on
387     * the estimated allocated size needed to execute those operations.
388     */
389    struct util_throttle throttle;
390 
391    struct {
392       struct st_zombie_sampler_view_node list;
393       simple_mtx_t mutex;
394    } zombie_sampler_views;
395 
396    struct {
397       struct st_zombie_shader_node list;
398       simple_mtx_t mutex;
399    } zombie_shaders;
400 
401    struct hash_table *hw_select_shaders;
402 };
403 
404 /**
405  * Represent the attributes of a context.
406  */
407 struct st_context_attribs
408 {
409    /**
410     * The profile and minimal version to support.
411     *
412     * The valid profiles and versions are rendering API dependent.  The latest
413     * version satisfying the request should be returned.
414     */
415    gl_api profile;
416    int major, minor;
417 
418    /** Mask of ST_CONTEXT_FLAG_x bits */
419    unsigned flags;
420 
421    /** Mask of PIPE_CONTEXT_x bits */
422    unsigned context_flags;
423 
424    /**
425     * The visual of the framebuffers the context will be bound to.
426     */
427    struct st_visual visual;
428 
429    /**
430     * Configuration options.
431     */
432    struct st_config_options options;
433 };
434 
435 
436 /*
437  * Get the state tracker context for the given Mesa context.
438  */
439 static inline struct st_context *
st_context(struct gl_context * ctx)440 st_context(struct gl_context *ctx)
441 {
442    return ctx->st;
443 }
444 
445 
446 extern struct st_context *
447 st_create_context(gl_api api, struct pipe_context *pipe,
448                   const struct gl_config *visual,
449                   struct st_context *share,
450                   const struct st_config_options *options,
451                   bool no_error, bool has_egl_image_validate);
452 
453 extern void
454 st_destroy_context(struct st_context *st);
455 
456 extern void
457 st_context_flush(struct st_context *st, unsigned flags,
458                  struct pipe_fence_handle **fence,
459                  void (*before_flush_cb) (void*), void* args);
460 
461 extern bool
462 st_context_teximage(struct st_context *st, GLenum target,
463                     int level, enum pipe_format pipe_format,
464                     struct pipe_resource *tex, bool mipmap);
465 
466 extern void
467 st_context_invalidate_state(struct st_context *st, unsigned flags);
468 
469 extern void
470 st_invalidate_buffers(struct st_context *st);
471 
472 
473 extern void
474 st_save_zombie_sampler_view(struct st_context *st,
475                             struct pipe_sampler_view *view);
476 
477 extern void
478 st_save_zombie_shader(struct st_context *st,
479                       enum pipe_shader_type type,
480                       struct pipe_shader_state *shader);
481 
482 
483 void
484 st_context_free_zombie_objects(struct st_context *st);
485 
486 const struct nir_shader_compiler_options *
487 st_get_nir_compiler_options(struct st_context *st, gl_shader_stage stage);
488 
489 
490 void st_invalidate_state(struct gl_context *ctx);
491 void st_set_background_context(struct gl_context *ctx,
492                                struct util_queue_monitoring *queue_info);
493 
494 void
495 st_api_query_versions(struct pipe_frontend_screen *fscreen,
496                       struct st_config_options *options,
497                       int *gl_core_version,
498                       int *gl_compat_version,
499                       int *gl_es1_version,
500                       int *gl_es2_version);
501 
502 struct st_context *
503 st_api_create_context(struct pipe_frontend_screen *fscreen,
504                       const struct st_context_attribs *attribs,
505                       enum st_context_error *error,
506                       struct st_context *shared_ctx);
507 
508 bool
509 st_api_make_current(struct st_context *st,
510                     struct pipe_frontend_drawable *stdrawi,
511                     struct pipe_frontend_drawable *streadi);
512 
513 struct st_context *
514 st_api_get_current(void);
515 
516 void
517 st_api_destroy_drawable(struct pipe_frontend_drawable *drawable);
518 
519 void
520 st_screen_destroy(struct pipe_frontend_screen *fscreen);
521 
522 #ifdef __cplusplus
523 }
524 #endif
525 
526 #endif
527