cf2f636df6ed11af911d5a00f645547df728f7e9
[mesa.git] / src / mesa / state_tracker / st_context.h
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 "state_tracker/st_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
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 struct dd_function_table;
49 struct draw_context;
50 struct draw_stage;
51 struct gen_mipmap_state;
52 struct st_context;
53 struct st_common_program;
54 struct st_perf_monitor_group;
55 struct u_upload_mgr;
56
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
126 struct pipe_context *pipe;
127
128 struct draw_context *draw; /**< For selection/feedback/rastpos only */
129 struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */
130 struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */
131 struct draw_stage *rastpos_stage; /**< For glRasterPos */
132 GLboolean clamp_frag_color_in_shader;
133 GLboolean clamp_vert_color_in_shader;
134 boolean clamp_frag_depth_in_shader;
135 boolean has_stencil_export; /**< can do shader stencil export? */
136 boolean has_time_elapsed;
137 boolean has_etc1;
138 boolean has_etc2;
139 boolean has_astc_2d_ldr;
140 boolean prefer_blit_based_texture_transfer;
141 boolean force_persample_in_shader;
142 boolean has_shareable_shaders;
143 boolean has_half_float_packing;
144 boolean has_multi_draw_indirect;
145 boolean has_single_pipe_stat;
146 boolean has_indep_blend_func;
147 boolean needs_rgb_dst_alpha_override;
148 boolean can_bind_const_buffer_as_vertex;
149 boolean has_signed_vertex_buffer_offset;
150 boolean lower_flatshade;
151 boolean lower_alpha_test;
152 boolean lower_point_size;
153 boolean lower_two_sided_color;
154 boolean lower_ucp;
155
156 /* There are consequences for drivers wanting to call st_finalize_nir
157 * twice, once before shader caching and once after lowering for shader
158 * variants. If shader variants use lowering passes that are not ready
159 * for that, things can blow up.
160 *
161 * If this is true, st_finalize_nir and pipe_screen::finalize_nir will be
162 * called before the result is stored in the shader cache. If lowering for
163 * shader variants is invoked, the functions will be called again.
164 */
165 boolean allow_st_finalize_nir_twice;
166
167 /**
168 * If a shader can be created when we get its source.
169 * This means it has only 1 variant, not counting glBitmap and
170 * glDrawPixels.
171 */
172 boolean shader_has_one_variant[MESA_SHADER_STAGES];
173
174 boolean needs_texcoord_semantic;
175 boolean apply_texture_swizzle_to_border_color;
176
177 /* On old libGL's for linux we need to invalidate the drawables
178 * on glViewpport calls, this is set via a option.
179 */
180 boolean invalidate_on_gl_viewport;
181 boolean draw_needs_minmax_index;
182 boolean has_hw_atomics;
183
184 /* Some state is contained in constant objects.
185 * Other state is just parameter values.
186 */
187 struct {
188 struct pipe_blend_state blend;
189 struct pipe_depth_stencil_alpha_state depth_stencil;
190 struct pipe_rasterizer_state rasterizer;
191 struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
192 GLuint num_frag_samplers;
193 struct pipe_sampler_view *frag_sampler_views[PIPE_MAX_SAMPLERS];
194 GLuint num_sampler_views[PIPE_SHADER_TYPES];
195 struct pipe_clip_state clip;
196 struct {
197 void *ptr;
198 unsigned size;
199 } constants[PIPE_SHADER_TYPES];
200 unsigned fb_width;
201 unsigned fb_height;
202 unsigned fb_num_samples;
203 unsigned fb_num_layers;
204 unsigned fb_num_cb;
205 unsigned num_viewports;
206 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
207 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
208 struct {
209 unsigned num;
210 boolean include;
211 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
212 } window_rects;
213
214 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
215
216 GLuint fb_orientation;
217
218 bool enable_sample_locations;
219 unsigned sample_locations_samples;
220 uint8_t sample_locations[
221 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
222 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
223 } state;
224
225 uint64_t dirty; /**< dirty states */
226
227 /** This masks out unused shader resources. Only valid in draw calls. */
228 uint64_t active_states;
229
230 unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
231
232 /* If true, further analysis of states is required to know if something
233 * has changed. Used mainly for shaders.
234 */
235 bool gfx_shaders_may_be_dirty;
236 bool compute_shader_may_be_dirty;
237
238 GLboolean vertdata_edgeflags;
239 GLboolean edgeflag_culls_prims;
240
241 /**
242 * The number of currently active queries (excluding timer queries).
243 * This is used to know if we need to pause any queries for meta ops.
244 */
245 unsigned active_queries;
246
247 union {
248 struct {
249 struct st_vertex_program *vp; /**< Currently bound vertex program */
250 struct st_common_program *tcp; /**< Currently bound tess control program */
251 struct st_common_program *tep; /**< Currently bound tess eval program */
252 struct st_common_program *gp; /**< Currently bound geometry program */
253 struct st_common_program *fp; /**< Currently bound fragment program */
254 struct st_common_program *cp; /**< Currently bound compute program */
255 };
256 struct gl_program *current_program[MESA_SHADER_STAGES];
257 };
258
259 struct st_vp_variant *vp_variant;
260
261 struct {
262 struct pipe_resource *pixelmap_texture;
263 struct pipe_sampler_view *pixelmap_sampler_view;
264 } pixel_xfer;
265
266 /** for glBitmap */
267 struct {
268 struct pipe_rasterizer_state rasterizer;
269 struct pipe_sampler_state sampler;
270 struct pipe_sampler_state atlas_sampler;
271 enum pipe_format tex_format;
272 struct st_bitmap_cache cache;
273 } bitmap;
274
275 /** for glDraw/CopyPixels */
276 struct {
277 void *zs_shaders[4];
278 } drawpix;
279
280 /** Cache of glDrawPixels images */
281 struct {
282 struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
283 unsigned age;
284 } drawpix_cache;
285
286 /** for glReadPixels */
287 struct {
288 struct pipe_resource *src;
289 struct pipe_resource *cache;
290 enum pipe_format dst_format;
291 unsigned level;
292 unsigned layer;
293 unsigned hits;
294 } readpix_cache;
295
296 /** for glClear */
297 struct {
298 struct pipe_rasterizer_state raster;
299 struct pipe_viewport_state viewport;
300 void *vs;
301 void *fs;
302 void *vs_layered;
303 void *gs_layered;
304 } clear;
305
306 /* For gl(Compressed)Tex(Sub)Image */
307 struct {
308 struct pipe_rasterizer_state raster;
309 struct pipe_blend_state upload_blend;
310 void *vs;
311 void *gs;
312 void *upload_fs[3];
313 void *download_fs[3][PIPE_MAX_TEXTURE_TYPES];
314 bool upload_enabled;
315 bool download_enabled;
316 bool rgba_only;
317 bool layers;
318 bool use_gs;
319 } pbo;
320
321 /** for drawing with st_util_vertex */
322 struct pipe_vertex_element util_velems[3];
323
324 /** passthrough vertex shader matching the util_velem attributes */
325 void *passthrough_vs;
326
327 enum pipe_texture_target internal_target;
328
329 struct cso_context *cso_context;
330
331 void *winsys_drawable_handle;
332
333 /* The number of vertex buffers from the last call of validate_arrays. */
334 unsigned last_num_vbuffers;
335
336 int32_t draw_stamp;
337 int32_t read_stamp;
338
339 struct st_config_options options;
340
341 struct st_perf_monitor_group *perfmon;
342
343 enum pipe_reset_status reset_status;
344
345 /* Array of bound texture/image handles which are resident in the context.
346 */
347 struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
348 struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
349
350 /* Winsys buffers */
351 struct list_head winsys_buffers;
352
353 /* Throttling for texture uploads and similar operations to limit memory
354 * usage by limiting the number of in-flight operations based on
355 * the estimated allocated size needed to execute those operations.
356 */
357 struct util_throttle throttle;
358
359 struct {
360 struct st_zombie_sampler_view_node list;
361 simple_mtx_t mutex;
362 } zombie_sampler_views;
363
364 struct {
365 struct st_zombie_shader_node list;
366 simple_mtx_t mutex;
367 } zombie_shaders;
368
369 };
370
371
372 /*
373 * Get the state tracker context for the given Mesa context.
374 */
375 static inline struct st_context *
376 st_context(struct gl_context *ctx)
377 {
378 return ctx->st;
379 }
380
381
382 extern struct st_context *
383 st_create_context(gl_api api, struct pipe_context *pipe,
384 const struct gl_config *visual,
385 struct st_context *share,
386 const struct st_config_options *options,
387 bool no_error);
388
389 extern void
390 st_destroy_context(struct st_context *st);
391
392
393 extern void
394 st_invalidate_buffers(struct st_context *st);
395
396
397 extern void
398 st_save_zombie_sampler_view(struct st_context *st,
399 struct pipe_sampler_view *view);
400
401 extern void
402 st_save_zombie_shader(struct st_context *st,
403 enum pipe_shader_type type,
404 struct pipe_shader_state *shader);
405
406
407 void
408 st_context_free_zombie_objects(struct st_context *st);
409
410
411
412 /**
413 * Wrapper for struct gl_framebuffer.
414 * This is an opaque type to the outside world.
415 */
416 struct st_framebuffer
417 {
418 struct gl_framebuffer Base;
419
420 struct st_framebuffer_iface *iface;
421 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
422 unsigned num_statts;
423 int32_t stamp;
424 int32_t iface_stamp;
425 uint32_t iface_ID;
426
427 /* list of framebuffer objects */
428 struct list_head head;
429 };
430
431
432 #ifdef __cplusplus
433 }
434 #endif
435
436 #endif