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 dd_function_table;
50struct draw_context;
51struct draw_stage;
52struct gen_mipmap_state;
53struct st_context;
54struct st_program;
55struct st_perf_monitor_group;
56struct u_upload_mgr;
57
58#define ST_L3_PINNING_DISABLED 0xffffffff
59
60struct st_bitmap_cache
61{
62   /** Window pos to render the cached image */
63   GLint xpos, ypos;
64   /** Bounds of region used in window coords */
65   GLint xmin, ymin, xmax, ymax;
66
67   GLfloat color[4];
68
69   /** Bitmap's Z position */
70   GLfloat zpos;
71
72   struct pipe_resource *texture;
73   struct pipe_transfer *trans;
74
75   GLboolean empty;
76
77   /** An I8 texture image: */
78   ubyte *buffer;
79};
80
81struct st_bound_handles
82{
83   unsigned num_handles;
84   uint64_t *handles;
85};
86
87
88#define NUM_DRAWPIX_CACHE_ENTRIES 4
89
90struct drawpix_cache_entry
91{
92   GLsizei width, height;
93   GLenum format, type;
94   const void *user_pointer;  /**< Last user 'pixels' pointer */
95   void *image;               /**< Copy of the glDrawPixels image data */
96   struct pipe_resource *texture;
97   unsigned age;
98};
99
100
101/*
102 * Node for a linked list of dead sampler views.
103 */
104struct st_zombie_sampler_view_node
105{
106   struct pipe_sampler_view *view;
107   struct list_head node;
108};
109
110
111/*
112 * Node for a linked list of dead shaders.
113 */
114struct st_zombie_shader_node
115{
116   void *shader;
117   enum pipe_shader_type type;
118   struct list_head node;
119};
120
121
122struct st_context
123{
124   struct st_context_iface iface;
125
126   struct gl_context *ctx;
127   struct pipe_screen *screen;
128   struct pipe_context *pipe;
129   struct cso_context *cso_context;
130
131   struct draw_context *draw;  /**< For selection/feedback/rastpos only */
132   struct draw_stage *feedback_stage;  /**< For GL_FEEDBACK rendermode */
133   struct draw_stage *selection_stage;  /**< For GL_SELECT rendermode */
134   struct draw_stage *rastpos_stage;  /**< For glRasterPos */
135
136   unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
137
138   GLboolean clamp_frag_color_in_shader;
139   GLboolean clamp_vert_color_in_shader;
140   boolean clamp_frag_depth_in_shader;
141   boolean has_stencil_export; /**< can do shader stencil export? */
142   boolean has_time_elapsed;
143   boolean has_etc1;
144   boolean has_etc2;
145   boolean transcode_etc;
146   boolean transcode_astc;
147   boolean has_astc_2d_ldr;
148   boolean has_astc_5x5_ldr;
149   boolean prefer_blit_based_texture_transfer;
150   boolean force_persample_in_shader;
151   boolean has_shareable_shaders;
152   boolean has_half_float_packing;
153   boolean has_multi_draw_indirect;
154   boolean has_single_pipe_stat;
155   boolean has_indep_blend_func;
156   boolean needs_rgb_dst_alpha_override;
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 emulate_gl_clamp;
189   boolean texture_buffer_sampler;
190
191   /* On old libGL's for linux we need to invalidate the drawables
192    * on glViewpport calls, this is set via a option.
193    */
194   boolean invalidate_on_gl_viewport;
195   boolean draw_needs_minmax_index;
196   boolean has_hw_atomics;
197
198
199   /* driver supports scissored clears */
200   boolean can_scissor_clear;
201
202   /* Some state is contained in constant objects.
203    * Other state is just parameter values.
204    */
205   struct {
206      struct pipe_blend_state               blend;
207      struct pipe_depth_stencil_alpha_state depth_stencil;
208      struct pipe_rasterizer_state          rasterizer;
209      struct pipe_sampler_state vert_samplers[PIPE_MAX_SAMPLERS];
210      struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
211      GLuint num_vert_samplers;
212      GLuint num_frag_samplers;
213      GLuint num_sampler_views[PIPE_SHADER_TYPES];
214      unsigned num_images[PIPE_SHADER_TYPES];
215      struct pipe_clip_state clip;
216      unsigned constbuf0_enabled_shader_mask;
217      unsigned fb_width;
218      unsigned fb_height;
219      unsigned fb_num_samples;
220      unsigned fb_num_layers;
221      unsigned fb_num_cb;
222      unsigned num_viewports;
223      struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
224      struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
225      struct {
226         unsigned num;
227         boolean include;
228         struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
229      } window_rects;
230
231      GLuint poly_stipple[32];  /**< In OpenGL's bottom-to-top order */
232
233      GLuint fb_orientation;
234
235      bool enable_sample_locations;
236      unsigned sample_locations_samples;
237      uint8_t sample_locations[
238         PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
239         PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
240   } state;
241
242   uint64_t dirty; /**< dirty states */
243
244   /** This masks out unused shader resources. Only valid in draw calls. */
245   uint64_t active_states;
246
247   /* If true, further analysis of states is required to know if something
248    * has changed. Used mainly for shaders.
249    */
250   bool gfx_shaders_may_be_dirty;
251   bool compute_shader_may_be_dirty;
252
253   GLboolean vertdata_edgeflags;
254   GLboolean edgeflag_culls_prims;
255
256   /**
257    * The number of currently active queries (excluding timer queries).
258    * This is used to know if we need to pause any queries for meta ops.
259    */
260   unsigned active_queries;
261
262   union {
263      struct {
264         struct st_program *vp;    /**< Currently bound vertex program */
265         struct st_program *tcp; /**< Currently bound tess control program */
266         struct st_program *tep; /**< Currently bound tess eval program */
267         struct st_program *gp;  /**< Currently bound geometry program */
268         struct st_program *fp;  /**< Currently bound fragment program */
269         struct st_program *cp;   /**< Currently bound compute program */
270      };
271      struct gl_program *current_program[MESA_SHADER_STAGES];
272   };
273
274   struct st_common_variant *vp_variant;
275
276   struct {
277      struct pipe_resource *pixelmap_texture;
278      struct pipe_sampler_view *pixelmap_sampler_view;
279   } pixel_xfer;
280
281   /** for glBitmap */
282   struct {
283      struct pipe_rasterizer_state rasterizer;
284      struct pipe_sampler_state sampler;
285      struct pipe_sampler_state atlas_sampler;
286      enum pipe_format tex_format;
287      struct st_bitmap_cache cache;
288   } bitmap;
289
290   /** for glDraw/CopyPixels */
291   struct {
292      void *zs_shaders[6];
293   } drawpix;
294
295   /** Cache of glDrawPixels images */
296   struct {
297      struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
298      unsigned age;
299   } drawpix_cache;
300
301   /** for glReadPixels */
302   struct {
303      struct pipe_resource *src;
304      struct pipe_resource *cache;
305      enum pipe_format dst_format;
306      unsigned level;
307      unsigned layer;
308      unsigned hits;
309   } readpix_cache;
310
311   /** for glClear */
312   struct {
313      struct pipe_rasterizer_state raster;
314      struct pipe_viewport_state viewport;
315      void *vs;
316      void *fs;
317      void *vs_layered;
318      void *gs_layered;
319   } clear;
320
321   /* For gl(Compressed)Tex(Sub)Image */
322   struct {
323      struct pipe_rasterizer_state raster;
324      struct pipe_blend_state upload_blend;
325      void *vs;
326      void *gs;
327      void *upload_fs[5][2];
328      void *download_fs[5][PIPE_MAX_TEXTURE_TYPES][2];
329      bool upload_enabled;
330      bool download_enabled;
331      bool rgba_only;
332      bool layers;
333      bool use_gs;
334   } pbo;
335
336   /** for drawing with st_util_vertex */
337   struct cso_velems_state util_velems;
338
339   /** passthrough vertex shader matching the util_velem attributes */
340   void *passthrough_vs;
341
342   enum pipe_texture_target internal_target;
343
344   void *winsys_drawable_handle;
345
346   /* The number of vertex buffers from the last call of validate_arrays. */
347   unsigned last_num_vbuffers;
348
349   unsigned last_used_atomic_bindings[PIPE_SHADER_TYPES];
350   unsigned last_num_ssbos[PIPE_SHADER_TYPES];
351
352   int32_t draw_stamp;
353   int32_t read_stamp;
354
355   struct st_config_options options;
356
357   struct st_perf_monitor_group *perfmon;
358
359   enum pipe_reset_status reset_status;
360
361   /* Array of bound texture/image handles which are resident in the context.
362    */
363   struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
364   struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
365
366   /* Winsys buffers */
367   struct list_head winsys_buffers;
368
369   /* Throttling for texture uploads and similar operations to limit memory
370    * usage by limiting the number of in-flight operations based on
371    * the estimated allocated size needed to execute those operations.
372    */
373   struct util_throttle throttle;
374
375   struct {
376      struct st_zombie_sampler_view_node list;
377      simple_mtx_t mutex;
378   } zombie_sampler_views;
379
380   struct {
381      struct st_zombie_shader_node list;
382      simple_mtx_t mutex;
383   } zombie_shaders;
384};
385
386
387/*
388 * Get the state tracker context for the given Mesa context.
389 */
390static inline struct st_context *
391st_context(struct gl_context *ctx)
392{
393   return ctx->st;
394}
395
396
397extern struct st_context *
398st_create_context(gl_api api, struct pipe_context *pipe,
399                  const struct gl_config *visual,
400                  struct st_context *share,
401                  const struct st_config_options *options,
402                  bool no_error, bool has_egl_image_validate);
403
404extern void
405st_destroy_context(struct st_context *st);
406
407
408extern void
409st_invalidate_buffers(struct st_context *st);
410
411
412extern void
413st_save_zombie_sampler_view(struct st_context *st,
414                            struct pipe_sampler_view *view);
415
416extern void
417st_save_zombie_shader(struct st_context *st,
418                      enum pipe_shader_type type,
419                      struct pipe_shader_state *shader);
420
421
422void
423st_context_free_zombie_objects(struct st_context *st);
424
425const struct nir_shader_compiler_options *
426st_get_nir_compiler_options(struct st_context *st, gl_shader_stage stage);
427
428
429/**
430 * Wrapper for struct gl_framebuffer.
431 * This is an opaque type to the outside world.
432 */
433struct st_framebuffer
434{
435   struct gl_framebuffer Base;
436
437   struct st_framebuffer_iface *iface;
438   enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
439   unsigned num_statts;
440   int32_t stamp;
441   int32_t iface_stamp;
442   uint32_t iface_ID;
443
444   /* list of framebuffer objects */
445   struct list_head head;
446};
447
448
449#ifdef __cplusplus
450}
451#endif
452
453#endif
454