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