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