gallium: Add the ability to query a single pipeline statistics counter
[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/mtypes.h"
32 #include "state_tracker/st_api.h"
33 #include "main/fbobject.h"
34 #include "state_tracker/st_atom.h"
35 #include "util/u_helpers.h"
36 #include "util/u_inlines.h"
37 #include "util/list.h"
38 #include "vbo/vbo.h"
39
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45
46 struct dd_function_table;
47 struct draw_context;
48 struct draw_stage;
49 struct gen_mipmap_state;
50 struct st_context;
51 struct st_fragment_program;
52 struct st_perf_monitor_group;
53 struct u_upload_mgr;
54
55
56 /** For drawing quads for glClear, glDraw/CopyPixels, glBitmap, etc. */
57 struct st_util_vertex
58 {
59 float x, y, z;
60 float r, g, b, a;
61 float s, t;
62 };
63
64 struct st_bitmap_cache
65 {
66 /** Window pos to render the cached image */
67 GLint xpos, ypos;
68 /** Bounds of region used in window coords */
69 GLint xmin, ymin, xmax, ymax;
70
71 GLfloat color[4];
72
73 /** Bitmap's Z position */
74 GLfloat zpos;
75
76 struct pipe_resource *texture;
77 struct pipe_transfer *trans;
78
79 GLboolean empty;
80
81 /** An I8 texture image: */
82 ubyte *buffer;
83 };
84
85 struct st_bound_handles
86 {
87 unsigned num_handles;
88 uint64_t *handles;
89 };
90
91
92 #define NUM_DRAWPIX_CACHE_ENTRIES 4
93
94 struct drawpix_cache_entry
95 {
96 GLsizei width, height;
97 GLenum format, type;
98 const void *user_pointer; /**< Last user 'pixels' pointer */
99 void *image; /**< Copy of the glDrawPixels image data */
100 struct pipe_resource *texture;
101 unsigned age;
102 };
103
104
105 struct st_context
106 {
107 struct st_context_iface iface;
108
109 struct gl_context *ctx;
110
111 struct pipe_context *pipe;
112
113 struct draw_context *draw; /**< For selection/feedback/rastpos only */
114 struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */
115 struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */
116 struct draw_stage *rastpos_stage; /**< For glRasterPos */
117 GLboolean clamp_frag_color_in_shader;
118 GLboolean clamp_vert_color_in_shader;
119 boolean has_stencil_export; /**< can do shader stencil export? */
120 boolean has_time_elapsed;
121 boolean has_shader_model3;
122 boolean has_etc1;
123 boolean has_etc2;
124 boolean has_astc_2d_ldr;
125 boolean prefer_blit_based_texture_transfer;
126 boolean force_persample_in_shader;
127 boolean has_shareable_shaders;
128 boolean has_half_float_packing;
129 boolean has_multi_draw_indirect;
130 boolean has_single_pipe_stat;
131 boolean can_bind_const_buffer_as_vertex;
132
133 /**
134 * If a shader can be created when we get its source.
135 * This means it has only 1 variant, not counting glBitmap and
136 * glDrawPixels.
137 */
138 boolean shader_has_one_variant[MESA_SHADER_STAGES];
139
140 boolean needs_texcoord_semantic;
141 boolean apply_texture_swizzle_to_border_color;
142
143 /* On old libGL's for linux we need to invalidate the drawables
144 * on glViewpport calls, this is set via a option.
145 */
146 boolean invalidate_on_gl_viewport;
147 boolean draw_needs_minmax_index;
148 boolean vertex_array_out_of_memory;
149 boolean has_hw_atomics;
150
151 /* Some state is contained in constant objects.
152 * Other state is just parameter values.
153 */
154 struct {
155 struct pipe_blend_state blend;
156 struct pipe_depth_stencil_alpha_state depth_stencil;
157 struct pipe_rasterizer_state rasterizer;
158 struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
159 GLuint num_frag_samplers;
160 struct pipe_sampler_view *frag_sampler_views[PIPE_MAX_SAMPLERS];
161 GLuint num_sampler_views[PIPE_SHADER_TYPES];
162 struct pipe_clip_state clip;
163 struct {
164 void *ptr;
165 unsigned size;
166 } constants[PIPE_SHADER_TYPES];
167 unsigned fb_width;
168 unsigned fb_height;
169 unsigned fb_num_samples;
170 unsigned fb_num_layers;
171 unsigned fb_num_cb;
172 unsigned num_viewports;
173 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
174 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
175 struct {
176 unsigned num;
177 boolean include;
178 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
179 } window_rects;
180
181 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
182
183 GLuint fb_orientation;
184
185 bool enable_sample_locations;
186 unsigned sample_locations_samples;
187 uint8_t sample_locations[
188 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
189 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
190 } state;
191
192 uint64_t dirty; /**< dirty states */
193
194 /** This masks out unused shader resources. Only valid in draw calls. */
195 uint64_t active_states;
196
197 unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
198
199 /* If true, further analysis of states is required to know if something
200 * has changed. Used mainly for shaders.
201 */
202 bool gfx_shaders_may_be_dirty;
203 bool compute_shader_may_be_dirty;
204
205 GLboolean vertdata_edgeflags;
206 GLboolean edgeflag_culls_prims;
207
208 struct st_vertex_program *vp; /**< Currently bound vertex program */
209 struct st_fragment_program *fp; /**< Currently bound fragment program */
210 struct st_common_program *gp; /**< Currently bound geometry program */
211 struct st_common_program *tcp; /**< Currently bound tess control program */
212 struct st_common_program *tep; /**< Currently bound tess eval program */
213 struct st_compute_program *cp; /**< Currently bound compute program */
214
215 struct st_vp_variant *vp_variant;
216
217 struct {
218 struct pipe_resource *pixelmap_texture;
219 struct pipe_sampler_view *pixelmap_sampler_view;
220 } pixel_xfer;
221
222 /** for glBitmap */
223 struct {
224 struct pipe_rasterizer_state rasterizer;
225 struct pipe_sampler_state sampler;
226 struct pipe_sampler_state atlas_sampler;
227 enum pipe_format tex_format;
228 struct st_bitmap_cache cache;
229 } bitmap;
230
231 /** for glDraw/CopyPixels */
232 struct {
233 void *zs_shaders[4];
234 } drawpix;
235
236 /** Cache of glDrawPixels images */
237 struct {
238 struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
239 unsigned age;
240 } drawpix_cache;
241
242 /** for glReadPixels */
243 struct {
244 struct pipe_resource *src;
245 struct pipe_resource *cache;
246 enum pipe_format dst_format;
247 unsigned level;
248 unsigned layer;
249 unsigned hits;
250 } readpix_cache;
251
252 /** for glClear */
253 struct {
254 struct pipe_rasterizer_state raster;
255 struct pipe_viewport_state viewport;
256 void *vs;
257 void *fs;
258 void *vs_layered;
259 void *gs_layered;
260 } clear;
261
262 /* For gl(Compressed)Tex(Sub)Image */
263 struct {
264 struct pipe_rasterizer_state raster;
265 struct pipe_blend_state upload_blend;
266 void *vs;
267 void *gs;
268 void *upload_fs[3];
269 void *download_fs[3][PIPE_MAX_TEXTURE_TYPES];
270 bool upload_enabled;
271 bool download_enabled;
272 bool rgba_only;
273 bool layers;
274 bool use_gs;
275 } pbo;
276
277 /** for drawing with st_util_vertex */
278 struct pipe_vertex_element util_velems[3];
279
280 /** passthrough vertex shader matching the util_velem attributes */
281 void *passthrough_vs;
282
283 enum pipe_texture_target internal_target;
284
285 struct cso_context *cso_context;
286
287 void *winsys_drawable_handle;
288
289 /* The number of vertex buffers from the last call of validate_arrays. */
290 unsigned last_num_vbuffers;
291
292 int32_t draw_stamp;
293 int32_t read_stamp;
294
295 struct st_config_options options;
296
297 struct st_perf_monitor_group *perfmon;
298
299 enum pipe_reset_status reset_status;
300
301 /* Array of bound texture/image handles which are resident in the context.
302 */
303 struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
304 struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
305
306 /* Winsys buffers */
307 struct list_head winsys_buffers;
308
309 /* Throttling for texture uploads and similar operations to limit memory
310 * usage by limiting the number of in-flight operations based on
311 * the estimated allocated size needed to execute those operations.
312 */
313 struct util_throttle throttle;
314 };
315
316
317 /* Need this so that we can implement Mesa callbacks in this module.
318 */
319 static inline struct st_context *st_context(struct gl_context *ctx)
320 {
321 return ctx->st;
322 }
323
324
325 /**
326 * Wrapper for struct gl_framebuffer.
327 * This is an opaque type to the outside world.
328 */
329 struct st_framebuffer
330 {
331 struct gl_framebuffer Base;
332
333 struct st_framebuffer_iface *iface;
334 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
335 unsigned num_statts;
336 int32_t stamp;
337 int32_t iface_stamp;
338 uint32_t iface_ID;
339
340 /* list of framebuffer objects */
341 struct list_head head;
342 };
343
344
345 extern void st_init_driver_functions(struct pipe_screen *screen,
346 struct dd_function_table *functions);
347
348 void
349 st_invalidate_buffers(struct st_context *st);
350
351 /* Invalidate the readpixels cache to ensure we don't read stale data.
352 */
353 static inline void
354 st_invalidate_readpix_cache(struct st_context *st)
355 {
356 if (unlikely(st->readpix_cache.src)) {
357 pipe_resource_reference(&st->readpix_cache.src, NULL);
358 pipe_resource_reference(&st->readpix_cache.cache, NULL);
359 }
360 }
361
362
363 #define Y_0_TOP 1
364 #define Y_0_BOTTOM 2
365
366 static inline GLuint
367 st_fb_orientation(const struct gl_framebuffer *fb)
368 {
369 if (fb && _mesa_is_winsys_fbo(fb)) {
370 /* Drawing into a window (on-screen buffer).
371 *
372 * Negate Y scale to flip image vertically.
373 * The NDC Y coords prior to viewport transformation are in the range
374 * [y=-1=bottom, y=1=top]
375 * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
376 * H is the window height.
377 * Use the viewport transformation to invert Y.
378 */
379 return Y_0_TOP;
380 }
381 else {
382 /* Drawing into user-created FBO (very likely a texture).
383 *
384 * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
385 */
386 return Y_0_BOTTOM;
387 }
388 }
389
390
391 static inline bool
392 st_user_clip_planes_enabled(struct gl_context *ctx)
393 {
394 return (ctx->API == API_OPENGL_COMPAT ||
395 ctx->API == API_OPENGLES) && /* only ES 1.x */
396 ctx->Transform.ClipPlanesEnabled;
397 }
398
399 /** clear-alloc a struct-sized object, with casting */
400 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
401
402
403 extern struct st_context *
404 st_create_context(gl_api api, struct pipe_context *pipe,
405 const struct gl_config *visual,
406 struct st_context *share,
407 const struct st_config_options *options,
408 bool no_error);
409
410 extern void
411 st_destroy_context(struct st_context *st);
412
413 uint64_t
414 st_get_active_states(struct gl_context *ctx);
415
416
417 #ifdef __cplusplus
418 }
419 #endif
420
421 #endif