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