1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2003 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#ifndef ST_CONTEXT_H
29848b8605Smrg#define ST_CONTEXT_H
30848b8605Smrg
31b8e80941Smrg#include "main/arrayobj.h"
32848b8605Smrg#include "main/mtypes.h"
33848b8605Smrg#include "state_tracker/st_api.h"
34848b8605Smrg#include "main/fbobject.h"
35b8e80941Smrg#include "state_tracker/st_atom.h"
36b8e80941Smrg#include "util/u_helpers.h"
37b8e80941Smrg#include "util/u_inlines.h"
38b8e80941Smrg#include "util/list.h"
39b8e80941Smrg#include "vbo/vbo.h"
40b8e80941Smrg#include "util/list.h"
41b8e80941Smrg
42b8e80941Smrg
43b8e80941Smrg#ifdef __cplusplus
44b8e80941Smrgextern "C" {
45b8e80941Smrg#endif
46b8e80941Smrg
47848b8605Smrg
48848b8605Smrgstruct dd_function_table;
49848b8605Smrgstruct draw_context;
50848b8605Smrgstruct draw_stage;
51848b8605Smrgstruct gen_mipmap_state;
52848b8605Smrgstruct st_context;
53848b8605Smrgstruct st_fragment_program;
54b8e80941Smrgstruct st_perf_monitor_group;
55848b8605Smrgstruct u_upload_mgr;
56848b8605Smrg
57848b8605Smrg
58b8e80941Smrgstruct st_bitmap_cache
59b8e80941Smrg{
60b8e80941Smrg   /** Window pos to render the cached image */
61b8e80941Smrg   GLint xpos, ypos;
62b8e80941Smrg   /** Bounds of region used in window coords */
63b8e80941Smrg   GLint xmin, ymin, xmax, ymax;
64b8e80941Smrg
65b8e80941Smrg   GLfloat color[4];
66b8e80941Smrg
67b8e80941Smrg   /** Bitmap's Z position */
68b8e80941Smrg   GLfloat zpos;
69b8e80941Smrg
70b8e80941Smrg   struct pipe_resource *texture;
71b8e80941Smrg   struct pipe_transfer *trans;
72b8e80941Smrg
73b8e80941Smrg   GLboolean empty;
74b8e80941Smrg
75b8e80941Smrg   /** An I8 texture image: */
76b8e80941Smrg   ubyte *buffer;
77b8e80941Smrg};
78b8e80941Smrg
79b8e80941Smrgstruct st_bound_handles
80b8e80941Smrg{
81b8e80941Smrg   unsigned num_handles;
82b8e80941Smrg   uint64_t *handles;
83b8e80941Smrg};
84b8e80941Smrg
85848b8605Smrg
86b8e80941Smrg#define NUM_DRAWPIX_CACHE_ENTRIES 4
87848b8605Smrg
88b8e80941Smrgstruct drawpix_cache_entry
89b8e80941Smrg{
90b8e80941Smrg   GLsizei width, height;
91b8e80941Smrg   GLenum format, type;
92b8e80941Smrg   const void *user_pointer;  /**< Last user 'pixels' pointer */
93b8e80941Smrg   void *image;               /**< Copy of the glDrawPixels image data */
94b8e80941Smrg   struct pipe_resource *texture;
95b8e80941Smrg   unsigned age;
96848b8605Smrg};
97848b8605Smrg
98b8e80941Smrg
99b8e80941Smrg/*
100b8e80941Smrg * Node for a linked list of dead sampler views.
101b8e80941Smrg */
102b8e80941Smrgstruct st_zombie_sampler_view_node
103b8e80941Smrg{
104b8e80941Smrg   struct pipe_sampler_view *view;
105b8e80941Smrg   struct list_head node;
106848b8605Smrg};
107848b8605Smrg
108848b8605Smrg
109b8e80941Smrg/*
110b8e80941Smrg * Node for a linked list of dead shaders.
111b8e80941Smrg */
112b8e80941Smrgstruct st_zombie_shader_node
113b8e80941Smrg{
114b8e80941Smrg   void *shader;
115b8e80941Smrg   enum pipe_shader_type type;
116b8e80941Smrg   struct list_head node;
117b8e80941Smrg};
118b8e80941Smrg
119848b8605Smrg
120848b8605Smrgstruct st_context
121848b8605Smrg{
122848b8605Smrg   struct st_context_iface iface;
123848b8605Smrg
124848b8605Smrg   struct gl_context *ctx;
125848b8605Smrg
126848b8605Smrg   struct pipe_context *pipe;
127848b8605Smrg
128848b8605Smrg   struct draw_context *draw;  /**< For selection/feedback/rastpos only */
129848b8605Smrg   struct draw_stage *feedback_stage;  /**< For GL_FEEDBACK rendermode */
130848b8605Smrg   struct draw_stage *selection_stage;  /**< For GL_SELECT rendermode */
131848b8605Smrg   struct draw_stage *rastpos_stage;  /**< For glRasterPos */
132848b8605Smrg   GLboolean clamp_frag_color_in_shader;
133848b8605Smrg   GLboolean clamp_vert_color_in_shader;
134848b8605Smrg   boolean has_stencil_export; /**< can do shader stencil export? */
135848b8605Smrg   boolean has_time_elapsed;
136848b8605Smrg   boolean has_shader_model3;
137848b8605Smrg   boolean has_etc1;
138b8e80941Smrg   boolean has_etc2;
139b8e80941Smrg   boolean has_astc_2d_ldr;
140848b8605Smrg   boolean prefer_blit_based_texture_transfer;
141b8e80941Smrg   boolean force_persample_in_shader;
142b8e80941Smrg   boolean has_shareable_shaders;
143b8e80941Smrg   boolean has_half_float_packing;
144b8e80941Smrg   boolean has_multi_draw_indirect;
145b8e80941Smrg   boolean has_single_pipe_stat;
146b8e80941Smrg   boolean has_indep_blend_func;
147b8e80941Smrg   boolean needs_rgb_dst_alpha_override;
148b8e80941Smrg   boolean can_bind_const_buffer_as_vertex;
149b8e80941Smrg
150b8e80941Smrg   /**
151b8e80941Smrg    * If a shader can be created when we get its source.
152b8e80941Smrg    * This means it has only 1 variant, not counting glBitmap and
153b8e80941Smrg    * glDrawPixels.
154b8e80941Smrg    */
155b8e80941Smrg   boolean shader_has_one_variant[MESA_SHADER_STAGES];
156848b8605Smrg
157848b8605Smrg   boolean needs_texcoord_semantic;
158848b8605Smrg   boolean apply_texture_swizzle_to_border_color;
159848b8605Smrg
160848b8605Smrg   /* On old libGL's for linux we need to invalidate the drawables
161848b8605Smrg    * on glViewpport calls, this is set via a option.
162848b8605Smrg    */
163848b8605Smrg   boolean invalidate_on_gl_viewport;
164b8e80941Smrg   boolean draw_needs_minmax_index;
165b8e80941Smrg   boolean has_hw_atomics;
166848b8605Smrg
167848b8605Smrg   /* Some state is contained in constant objects.
168848b8605Smrg    * Other state is just parameter values.
169848b8605Smrg    */
170848b8605Smrg   struct {
171848b8605Smrg      struct pipe_blend_state               blend;
172848b8605Smrg      struct pipe_depth_stencil_alpha_state depth_stencil;
173848b8605Smrg      struct pipe_rasterizer_state          rasterizer;
174b8e80941Smrg      struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
175b8e80941Smrg      GLuint num_frag_samplers;
176b8e80941Smrg      struct pipe_sampler_view *frag_sampler_views[PIPE_MAX_SAMPLERS];
177848b8605Smrg      GLuint num_sampler_views[PIPE_SHADER_TYPES];
178848b8605Smrg      struct pipe_clip_state clip;
179848b8605Smrg      struct {
180848b8605Smrg         void *ptr;
181848b8605Smrg         unsigned size;
182848b8605Smrg      } constants[PIPE_SHADER_TYPES];
183b8e80941Smrg      unsigned fb_width;
184b8e80941Smrg      unsigned fb_height;
185b8e80941Smrg      unsigned fb_num_samples;
186b8e80941Smrg      unsigned fb_num_layers;
187b8e80941Smrg      unsigned fb_num_cb;
188b8e80941Smrg      unsigned num_viewports;
189848b8605Smrg      struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
190848b8605Smrg      struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
191b8e80941Smrg      struct {
192b8e80941Smrg         unsigned num;
193b8e80941Smrg         boolean include;
194b8e80941Smrg         struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
195b8e80941Smrg      } window_rects;
196848b8605Smrg
197848b8605Smrg      GLuint poly_stipple[32];  /**< In OpenGL's bottom-to-top order */
198848b8605Smrg
199848b8605Smrg      GLuint fb_orientation;
200b8e80941Smrg
201b8e80941Smrg      bool enable_sample_locations;
202b8e80941Smrg      unsigned sample_locations_samples;
203b8e80941Smrg      uint8_t sample_locations[
204b8e80941Smrg         PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
205b8e80941Smrg         PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
206848b8605Smrg   } state;
207848b8605Smrg
208b8e80941Smrg   uint64_t dirty; /**< dirty states */
209b8e80941Smrg
210b8e80941Smrg   /** This masks out unused shader resources. Only valid in draw calls. */
211b8e80941Smrg   uint64_t active_states;
212b8e80941Smrg
213b8e80941Smrg   unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
214848b8605Smrg
215b8e80941Smrg   /* If true, further analysis of states is required to know if something
216b8e80941Smrg    * has changed. Used mainly for shaders.
217b8e80941Smrg    */
218b8e80941Smrg   bool gfx_shaders_may_be_dirty;
219b8e80941Smrg   bool compute_shader_may_be_dirty;
220848b8605Smrg
221848b8605Smrg   GLboolean vertdata_edgeflags;
222848b8605Smrg   GLboolean edgeflag_culls_prims;
223848b8605Smrg
224848b8605Smrg   struct st_vertex_program *vp;    /**< Currently bound vertex program */
225848b8605Smrg   struct st_fragment_program *fp;  /**< Currently bound fragment program */
226b8e80941Smrg   struct st_common_program *gp;  /**< Currently bound geometry program */
227b8e80941Smrg   struct st_common_program *tcp; /**< Currently bound tess control program */
228b8e80941Smrg   struct st_common_program *tep; /**< Currently bound tess eval program */
229b8e80941Smrg   struct st_compute_program *cp;   /**< Currently bound compute program */
230848b8605Smrg
231848b8605Smrg   struct st_vp_variant *vp_variant;
232848b8605Smrg
233848b8605Smrg   struct {
234848b8605Smrg      struct pipe_resource *pixelmap_texture;
235848b8605Smrg      struct pipe_sampler_view *pixelmap_sampler_view;
236848b8605Smrg   } pixel_xfer;
237848b8605Smrg
238848b8605Smrg   /** for glBitmap */
239848b8605Smrg   struct {
240848b8605Smrg      struct pipe_rasterizer_state rasterizer;
241b8e80941Smrg      struct pipe_sampler_state sampler;
242b8e80941Smrg      struct pipe_sampler_state atlas_sampler;
243848b8605Smrg      enum pipe_format tex_format;
244b8e80941Smrg      struct st_bitmap_cache cache;
245848b8605Smrg   } bitmap;
246848b8605Smrg
247848b8605Smrg   /** for glDraw/CopyPixels */
248848b8605Smrg   struct {
249b8e80941Smrg      void *zs_shaders[4];
250848b8605Smrg   } drawpix;
251848b8605Smrg
252b8e80941Smrg   /** Cache of glDrawPixels images */
253b8e80941Smrg   struct {
254b8e80941Smrg      struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
255b8e80941Smrg      unsigned age;
256b8e80941Smrg   } drawpix_cache;
257b8e80941Smrg
258b8e80941Smrg   /** for glReadPixels */
259b8e80941Smrg   struct {
260b8e80941Smrg      struct pipe_resource *src;
261b8e80941Smrg      struct pipe_resource *cache;
262b8e80941Smrg      enum pipe_format dst_format;
263b8e80941Smrg      unsigned level;
264b8e80941Smrg      unsigned layer;
265b8e80941Smrg      unsigned hits;
266b8e80941Smrg   } readpix_cache;
267b8e80941Smrg
268848b8605Smrg   /** for glClear */
269848b8605Smrg   struct {
270848b8605Smrg      struct pipe_rasterizer_state raster;
271848b8605Smrg      struct pipe_viewport_state viewport;
272848b8605Smrg      void *vs;
273848b8605Smrg      void *fs;
274848b8605Smrg      void *vs_layered;
275848b8605Smrg      void *gs_layered;
276848b8605Smrg   } clear;
277848b8605Smrg
278b8e80941Smrg   /* For gl(Compressed)Tex(Sub)Image */
279b8e80941Smrg   struct {
280b8e80941Smrg      struct pipe_rasterizer_state raster;
281b8e80941Smrg      struct pipe_blend_state upload_blend;
282b8e80941Smrg      void *vs;
283b8e80941Smrg      void *gs;
284b8e80941Smrg      void *upload_fs[3];
285b8e80941Smrg      void *download_fs[3][PIPE_MAX_TEXTURE_TYPES];
286b8e80941Smrg      bool upload_enabled;
287b8e80941Smrg      bool download_enabled;
288b8e80941Smrg      bool rgba_only;
289b8e80941Smrg      bool layers;
290b8e80941Smrg      bool use_gs;
291b8e80941Smrg   } pbo;
292b8e80941Smrg
293b8e80941Smrg   /** for drawing with st_util_vertex */
294b8e80941Smrg   struct pipe_vertex_element util_velems[3];
295b8e80941Smrg
296b8e80941Smrg   /** passthrough vertex shader matching the util_velem attributes */
297b8e80941Smrg   void *passthrough_vs;
298848b8605Smrg
299848b8605Smrg   enum pipe_texture_target internal_target;
300848b8605Smrg
301848b8605Smrg   struct cso_context *cso_context;
302848b8605Smrg
303848b8605Smrg   void *winsys_drawable_handle;
304848b8605Smrg
305848b8605Smrg   /* The number of vertex buffers from the last call of validate_arrays. */
306848b8605Smrg   unsigned last_num_vbuffers;
307848b8605Smrg
308848b8605Smrg   int32_t draw_stamp;
309848b8605Smrg   int32_t read_stamp;
310848b8605Smrg
311848b8605Smrg   struct st_config_options options;
312b8e80941Smrg
313b8e80941Smrg   struct st_perf_monitor_group *perfmon;
314b8e80941Smrg
315b8e80941Smrg   enum pipe_reset_status reset_status;
316b8e80941Smrg
317b8e80941Smrg   /* Array of bound texture/image handles which are resident in the context.
318b8e80941Smrg    */
319b8e80941Smrg   struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
320b8e80941Smrg   struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
321b8e80941Smrg
322b8e80941Smrg   /* Winsys buffers */
323b8e80941Smrg   struct list_head winsys_buffers;
324b8e80941Smrg
325b8e80941Smrg   /* Throttling for texture uploads and similar operations to limit memory
326b8e80941Smrg    * usage by limiting the number of in-flight operations based on
327b8e80941Smrg    * the estimated allocated size needed to execute those operations.
328b8e80941Smrg    */
329b8e80941Smrg   struct util_throttle throttle;
330b8e80941Smrg
331b8e80941Smrg   struct {
332b8e80941Smrg      struct st_zombie_sampler_view_node list;
333b8e80941Smrg      mtx_t mutex;
334b8e80941Smrg   } zombie_sampler_views;
335b8e80941Smrg
336b8e80941Smrg   struct {
337b8e80941Smrg      struct st_zombie_shader_node list;
338b8e80941Smrg      mtx_t mutex;
339b8e80941Smrg   } zombie_shaders;
340b8e80941Smrg
341848b8605Smrg};
342848b8605Smrg
343848b8605Smrg
344b8e80941Smrg/*
345b8e80941Smrg * Get the state tracker context for the given Mesa context.
346848b8605Smrg */
347b8e80941Smrgstatic inline struct st_context *
348b8e80941Smrgst_context(struct gl_context *ctx)
349848b8605Smrg{
350848b8605Smrg   return ctx->st;
351848b8605Smrg}
352848b8605Smrg
353848b8605Smrg
354b8e80941Smrgextern struct st_context *
355b8e80941Smrgst_create_context(gl_api api, struct pipe_context *pipe,
356b8e80941Smrg                  const struct gl_config *visual,
357b8e80941Smrg                  struct st_context *share,
358b8e80941Smrg                  const struct st_config_options *options,
359b8e80941Smrg                  bool no_error);
360b8e80941Smrg
361b8e80941Smrgextern void
362b8e80941Smrgst_destroy_context(struct st_context *st);
363b8e80941Smrg
364b8e80941Smrg
365b8e80941Smrgextern void
366b8e80941Smrgst_invalidate_buffers(struct st_context *st);
367b8e80941Smrg
368b8e80941Smrg
369b8e80941Smrgextern void
370b8e80941Smrgst_save_zombie_sampler_view(struct st_context *st,
371b8e80941Smrg                            struct pipe_sampler_view *view);
372b8e80941Smrg
373b8e80941Smrgextern void
374b8e80941Smrgst_save_zombie_shader(struct st_context *st,
375b8e80941Smrg                      enum pipe_shader_type type,
376b8e80941Smrg                      struct pipe_shader_state *shader);
377b8e80941Smrg
378b8e80941Smrg
379b8e80941Smrgvoid
380b8e80941Smrgst_context_free_zombie_objects(struct st_context *st);
381b8e80941Smrg
382b8e80941Smrg
383b8e80941Smrg
384848b8605Smrg/**
385848b8605Smrg * Wrapper for struct gl_framebuffer.
386848b8605Smrg * This is an opaque type to the outside world.
387848b8605Smrg */
388848b8605Smrgstruct st_framebuffer
389848b8605Smrg{
390848b8605Smrg   struct gl_framebuffer Base;
391848b8605Smrg
392848b8605Smrg   struct st_framebuffer_iface *iface;
393848b8605Smrg   enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
394848b8605Smrg   unsigned num_statts;
395848b8605Smrg   int32_t stamp;
396848b8605Smrg   int32_t iface_stamp;
397b8e80941Smrg   uint32_t iface_ID;
398848b8605Smrg
399b8e80941Smrg   /* list of framebuffer objects */
400b8e80941Smrg   struct list_head head;
401b8e80941Smrg};
402848b8605Smrg
403848b8605Smrg
404b8e80941Smrg#ifdef __cplusplus
405848b8605Smrg}
406b8e80941Smrg#endif
407848b8605Smrg
408848b8605Smrg#endif
409