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