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_L3_PINNING_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    GLfloat color[4];
66 
67    /** Bitmap's Z position */
68    GLfloat zpos;
69 
70    struct pipe_resource *texture;
71    struct pipe_transfer *trans;
72 
73    GLboolean empty;
74 
75    /** An I8 texture image: */
76    ubyte *buffer;
77 };
78 
79 struct st_bound_handles
80 {
81    unsigned num_handles;
82    uint64_t *handles;
83 };
84 
85 
86 #define NUM_DRAWPIX_CACHE_ENTRIES 4
87 
88 struct drawpix_cache_entry
89 {
90    GLsizei width, height;
91    GLenum format, type;
92    const void *user_pointer;  /**< Last user 'pixels' pointer */
93    void *image;               /**< Copy of the glDrawPixels image data */
94    struct pipe_resource *texture;
95    unsigned age;
96 };
97 
98 
99 /*
100  * Node for a linked list of dead sampler views.
101  */
102 struct st_zombie_sampler_view_node
103 {
104    struct pipe_sampler_view *view;
105    struct list_head node;
106 };
107 
108 
109 /*
110  * Node for a linked list of dead shaders.
111  */
112 struct st_zombie_shader_node
113 {
114    void *shader;
115    enum pipe_shader_type type;
116    struct list_head node;
117 };
118 
119 
120 struct st_context
121 {
122    struct st_context_iface iface;
123 
124    struct gl_context *ctx;
125    struct pipe_screen *screen;
126    struct pipe_context *pipe;
127    struct cso_context *cso_context;
128 
129    struct draw_context *draw;  /**< For selection/feedback/rastpos only */
130    struct draw_stage *feedback_stage;  /**< For GL_FEEDBACK rendermode */
131    struct draw_stage *selection_stage;  /**< For GL_SELECT rendermode */
132    struct draw_stage *rastpos_stage;  /**< For glRasterPos */
133 
134    unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
135 
136    GLboolean clamp_frag_color_in_shader;
137    GLboolean clamp_vert_color_in_shader;
138    boolean has_stencil_export; /**< can do shader stencil export? */
139    boolean has_time_elapsed;
140    boolean has_etc1;
141    boolean has_etc2;
142    boolean transcode_etc;
143    boolean transcode_astc;
144    boolean has_astc_2d_ldr;
145    boolean has_astc_5x5_ldr;
146    boolean prefer_blit_based_texture_transfer;
147    boolean allow_compute_based_texture_transfer;
148    boolean force_persample_in_shader;
149    boolean has_shareable_shaders;
150    boolean has_half_float_packing;
151    boolean has_multi_draw_indirect;
152    boolean has_single_pipe_stat;
153    boolean has_indep_blend_func;
154    boolean needs_rgb_dst_alpha_override;
155    boolean can_bind_const_buffer_as_vertex;
156    boolean lower_flatshade;
157    boolean lower_alpha_test;
158    boolean lower_point_size;
159    boolean lower_two_sided_color;
160    boolean lower_ucp;
161    boolean prefer_real_buffer_in_constbuf0;
162    boolean has_conditional_render;
163    boolean lower_texcoord_replace;
164    boolean lower_rect_tex;
165 
166    /* There are consequences for drivers wanting to call st_finalize_nir
167     * twice, once before shader caching and once after lowering for shader
168     * variants. If shader variants use lowering passes that are not ready
169     * for that, things can blow up.
170     *
171     * If this is true, st_finalize_nir and pipe_screen::finalize_nir will be
172     * called before the result is stored in the shader cache. If lowering for
173     * shader variants is invoked, the functions will be called again.
174     */
175    boolean allow_st_finalize_nir_twice;
176 
177    /**
178     * If a shader can be created when we get its source.
179     * This means it has only 1 variant, not counting glBitmap and
180     * glDrawPixels.
181     */
182    boolean shader_has_one_variant[MESA_SHADER_STAGES];
183 
184    boolean needs_texcoord_semantic;
185    boolean apply_texture_swizzle_to_border_color;
186    boolean emulate_gl_clamp;
187    boolean texture_buffer_sampler;
188 
189    boolean draw_needs_minmax_index;
190    boolean has_hw_atomics;
191 
192 
193    /* driver supports scissored clears */
194    boolean can_scissor_clear;
195 
196    /* Some state is contained in constant objects.
197     * Other state is just parameter values.
198     */
199    struct {
200       struct pipe_blend_state               blend;
201       struct pipe_depth_stencil_alpha_state depth_stencil;
202       struct pipe_rasterizer_state          rasterizer;
203       struct pipe_sampler_state vert_samplers[PIPE_MAX_SAMPLERS];
204       struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
205       GLuint num_vert_samplers;
206       GLuint num_frag_samplers;
207       GLuint num_sampler_views[PIPE_SHADER_TYPES];
208       unsigned num_images[PIPE_SHADER_TYPES];
209       struct pipe_clip_state clip;
210       unsigned constbuf0_enabled_shader_mask;
211       unsigned fb_width;
212       unsigned fb_height;
213       unsigned fb_num_samples;
214       unsigned fb_num_layers;
215       unsigned fb_num_cb;
216       unsigned num_viewports;
217       struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
218       struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
219       struct {
220          unsigned num;
221          boolean include;
222          struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
223       } window_rects;
224 
225       GLuint poly_stipple[32];  /**< In OpenGL's bottom-to-top order */
226 
227       GLuint fb_orientation;
228 
229       bool enable_sample_locations;
230       unsigned sample_locations_samples;
231       uint8_t sample_locations[
232          PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
233          PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
234    } state;
235 
236    uint64_t dirty; /**< dirty states */
237 
238    /** This masks out unused shader resources. Only valid in draw calls. */
239    uint64_t active_states;
240 
241    /* If true, further analysis of states is required to know if something
242     * has changed. Used mainly for shaders.
243     */
244    bool gfx_shaders_may_be_dirty;
245    bool compute_shader_may_be_dirty;
246 
247    GLboolean vertdata_edgeflags;
248    GLboolean edgeflag_culls_prims;
249 
250    /**
251     * The number of currently active queries (excluding timer queries).
252     * This is used to know if we need to pause any queries for meta ops.
253     */
254    unsigned active_queries;
255 
256    union {
257       struct {
258          struct gl_program *vp;    /**< Currently bound vertex program */
259          struct gl_program *tcp; /**< Currently bound tess control program */
260          struct gl_program *tep; /**< Currently bound tess eval program */
261          struct gl_program *gp;  /**< Currently bound geometry program */
262          struct gl_program *fp;  /**< Currently bound fragment program */
263          struct gl_program *cp;   /**< Currently bound compute program */
264       };
265       struct gl_program *current_program[MESA_SHADER_STAGES];
266    };
267 
268    struct st_common_variant *vp_variant;
269 
270    struct {
271       struct pipe_resource *pixelmap_texture;
272       struct pipe_sampler_view *pixelmap_sampler_view;
273    } pixel_xfer;
274 
275    /** for glBitmap */
276    struct {
277       struct pipe_rasterizer_state rasterizer;
278       struct pipe_sampler_state sampler;
279       struct pipe_sampler_state atlas_sampler;
280       enum pipe_format tex_format;
281       struct st_bitmap_cache cache;
282    } bitmap;
283 
284    /** for glDraw/CopyPixels */
285    struct {
286       void *zs_shaders[6];
287    } drawpix;
288 
289    /** Cache of glDrawPixels images */
290    struct {
291       struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
292       unsigned age;
293    } drawpix_cache;
294 
295    /** for glReadPixels */
296    struct {
297       struct pipe_resource *src;
298       struct pipe_resource *cache;
299       enum pipe_format dst_format;
300       unsigned level;
301       unsigned layer;
302       unsigned hits;
303    } readpix_cache;
304 
305    /** for glClear */
306    struct {
307       struct pipe_rasterizer_state raster;
308       struct pipe_viewport_state viewport;
309       void *vs;
310       void *fs;
311       void *vs_layered;
312       void *gs_layered;
313    } clear;
314 
315    /* For gl(Compressed)Tex(Sub)Image */
316    struct {
317       struct pipe_rasterizer_state raster;
318       struct pipe_blend_state upload_blend;
319       void *vs;
320       void *gs;
321       void *upload_fs[5][2];
322       /**
323        * For drivers supporting formatless storing
324        * (PIPE_CAP_IMAGE_STORE_FORMATTED) it is a pointer to the download FS;
325        * for those not supporting it, it is a pointer to an array of
326        * PIPE_FORMAT_COUNT elements, where each element is a pointer to the
327        * download FS using that PIPE_FORMAT as the storing format.
328        */
329       void *download_fs[5][PIPE_MAX_TEXTURE_TYPES][2];
330       struct hash_table *shaders;
331       bool upload_enabled;
332       bool download_enabled;
333       bool rgba_only;
334       bool layers;
335       bool use_gs;
336    } pbo;
337 
338    /** for drawing with st_util_vertex */
339    struct cso_velems_state util_velems;
340 
341    /** passthrough vertex shader matching the util_velem attributes */
342    void *passthrough_vs;
343 
344    enum pipe_texture_target internal_target;
345 
346    void *winsys_drawable_handle;
347 
348    /* The number of vertex buffers from the last call of validate_arrays. */
349    unsigned last_num_vbuffers;
350    bool uses_user_vertex_buffers;
351 
352    unsigned last_used_atomic_bindings[PIPE_SHADER_TYPES];
353    unsigned last_num_ssbos[PIPE_SHADER_TYPES];
354 
355    int32_t draw_stamp;
356    int32_t read_stamp;
357 
358    struct st_config_options options;
359 
360    enum pipe_reset_status reset_status;
361 
362    /* Array of bound texture/image handles which are resident in the context.
363     */
364    struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
365    struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
366 
367    /* Winsys buffers */
368    struct list_head winsys_buffers;
369 
370    /* Throttling for texture uploads and similar operations to limit memory
371     * usage by limiting the number of in-flight operations based on
372     * the estimated allocated size needed to execute those operations.
373     */
374    struct util_throttle throttle;
375 
376    struct {
377       struct st_zombie_sampler_view_node list;
378       simple_mtx_t mutex;
379    } zombie_sampler_views;
380 
381    struct {
382       struct st_zombie_shader_node list;
383       simple_mtx_t mutex;
384    } zombie_shaders;
385 };
386 
387 
388 /*
389  * Get the state tracker context for the given Mesa context.
390  */
391 static inline struct st_context *
st_context(struct gl_context * ctx)392 st_context(struct gl_context *ctx)
393 {
394    return ctx->st;
395 }
396 
397 
398 extern struct st_context *
399 st_create_context(gl_api api, struct pipe_context *pipe,
400                   const struct gl_config *visual,
401                   struct st_context *share,
402                   const struct st_config_options *options,
403                   bool no_error, bool has_egl_image_validate);
404 
405 extern void
406 st_destroy_context(struct st_context *st);
407 
408 
409 extern void
410 st_invalidate_buffers(struct st_context *st);
411 
412 
413 extern void
414 st_save_zombie_sampler_view(struct st_context *st,
415                             struct pipe_sampler_view *view);
416 
417 extern void
418 st_save_zombie_shader(struct st_context *st,
419                       enum pipe_shader_type type,
420                       struct pipe_shader_state *shader);
421 
422 
423 void
424 st_context_free_zombie_objects(struct st_context *st);
425 
426 const struct nir_shader_compiler_options *
427 st_get_nir_compiler_options(struct st_context *st, gl_shader_stage stage);
428 
429 
430 void st_invalidate_state(struct gl_context *ctx);
431 void st_set_background_context(struct gl_context *ctx,
432                                struct util_queue_monitoring *queue_info);
433 #ifdef __cplusplus
434 }
435 #endif
436 
437 #endif
438