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