ed69e3d4873806a584ed7d52a149f666b5e52f6c
[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 has_indep_blend_func;
132 boolean needs_rgb_dst_alpha_override;
133 boolean can_bind_const_buffer_as_vertex;
134
135 /**
136 * If a shader can be created when we get its source.
137 * This means it has only 1 variant, not counting glBitmap and
138 * glDrawPixels.
139 */
140 boolean shader_has_one_variant[MESA_SHADER_STAGES];
141
142 boolean needs_texcoord_semantic;
143 boolean apply_texture_swizzle_to_border_color;
144
145 /* On old libGL's for linux we need to invalidate the drawables
146 * on glViewpport calls, this is set via a option.
147 */
148 boolean invalidate_on_gl_viewport;
149 boolean draw_needs_minmax_index;
150 boolean vertex_array_out_of_memory;
151 boolean has_hw_atomics;
152
153 /* Some state is contained in constant objects.
154 * Other state is just parameter values.
155 */
156 struct {
157 struct pipe_blend_state blend;
158 struct pipe_depth_stencil_alpha_state depth_stencil;
159 struct pipe_rasterizer_state rasterizer;
160 struct pipe_sampler_state frag_samplers[PIPE_MAX_SAMPLERS];
161 GLuint num_frag_samplers;
162 struct pipe_sampler_view *frag_sampler_views[PIPE_MAX_SAMPLERS];
163 GLuint num_sampler_views[PIPE_SHADER_TYPES];
164 struct pipe_clip_state clip;
165 struct {
166 void *ptr;
167 unsigned size;
168 } constants[PIPE_SHADER_TYPES];
169 unsigned fb_width;
170 unsigned fb_height;
171 unsigned fb_num_samples;
172 unsigned fb_num_layers;
173 unsigned fb_num_cb;
174 unsigned num_viewports;
175 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
176 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
177 struct {
178 unsigned num;
179 boolean include;
180 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
181 } window_rects;
182
183 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
184
185 GLuint fb_orientation;
186
187 bool enable_sample_locations;
188 unsigned sample_locations_samples;
189 uint8_t sample_locations[
190 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
191 PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
192 } state;
193
194 uint64_t dirty; /**< dirty states */
195
196 /** This masks out unused shader resources. Only valid in draw calls. */
197 uint64_t active_states;
198
199 unsigned pin_thread_counter; /* for L3 thread pinning on AMD Zen */
200
201 /* If true, further analysis of states is required to know if something
202 * has changed. Used mainly for shaders.
203 */
204 bool gfx_shaders_may_be_dirty;
205 bool compute_shader_may_be_dirty;
206
207 GLboolean vertdata_edgeflags;
208 GLboolean edgeflag_culls_prims;
209
210 struct st_vertex_program *vp; /**< Currently bound vertex program */
211 struct st_fragment_program *fp; /**< Currently bound fragment program */
212 struct st_common_program *gp; /**< Currently bound geometry program */
213 struct st_common_program *tcp; /**< Currently bound tess control program */
214 struct st_common_program *tep; /**< Currently bound tess eval program */
215 struct st_compute_program *cp; /**< Currently bound compute program */
216
217 struct st_vp_variant *vp_variant;
218
219 struct {
220 struct pipe_resource *pixelmap_texture;
221 struct pipe_sampler_view *pixelmap_sampler_view;
222 } pixel_xfer;
223
224 /** for glBitmap */
225 struct {
226 struct pipe_rasterizer_state rasterizer;
227 struct pipe_sampler_state sampler;
228 struct pipe_sampler_state atlas_sampler;
229 enum pipe_format tex_format;
230 struct st_bitmap_cache cache;
231 } bitmap;
232
233 /** for glDraw/CopyPixels */
234 struct {
235 void *zs_shaders[4];
236 } drawpix;
237
238 /** Cache of glDrawPixels images */
239 struct {
240 struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
241 unsigned age;
242 } drawpix_cache;
243
244 /** for glReadPixels */
245 struct {
246 struct pipe_resource *src;
247 struct pipe_resource *cache;
248 enum pipe_format dst_format;
249 unsigned level;
250 unsigned layer;
251 unsigned hits;
252 } readpix_cache;
253
254 /** for glClear */
255 struct {
256 struct pipe_rasterizer_state raster;
257 struct pipe_viewport_state viewport;
258 void *vs;
259 void *fs;
260 void *vs_layered;
261 void *gs_layered;
262 } clear;
263
264 /* For gl(Compressed)Tex(Sub)Image */
265 struct {
266 struct pipe_rasterizer_state raster;
267 struct pipe_blend_state upload_blend;
268 void *vs;
269 void *gs;
270 void *upload_fs[3];
271 void *download_fs[3][PIPE_MAX_TEXTURE_TYPES];
272 bool upload_enabled;
273 bool download_enabled;
274 bool rgba_only;
275 bool layers;
276 bool use_gs;
277 } pbo;
278
279 /** for drawing with st_util_vertex */
280 struct pipe_vertex_element util_velems[3];
281
282 /** passthrough vertex shader matching the util_velem attributes */
283 void *passthrough_vs;
284
285 enum pipe_texture_target internal_target;
286
287 struct cso_context *cso_context;
288
289 void *winsys_drawable_handle;
290
291 /* The number of vertex buffers from the last call of validate_arrays. */
292 unsigned last_num_vbuffers;
293
294 int32_t draw_stamp;
295 int32_t read_stamp;
296
297 struct st_config_options options;
298
299 struct st_perf_monitor_group *perfmon;
300
301 enum pipe_reset_status reset_status;
302
303 /* Array of bound texture/image handles which are resident in the context.
304 */
305 struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
306 struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
307
308 /* Winsys buffers */
309 struct list_head winsys_buffers;
310
311 /* Throttling for texture uploads and similar operations to limit memory
312 * usage by limiting the number of in-flight operations based on
313 * the estimated allocated size needed to execute those operations.
314 */
315 struct util_throttle throttle;
316 };
317
318
319 /* Need this so that we can implement Mesa callbacks in this module.
320 */
321 static inline struct st_context *st_context(struct gl_context *ctx)
322 {
323 return ctx->st;
324 }
325
326
327 /**
328 * Wrapper for struct gl_framebuffer.
329 * This is an opaque type to the outside world.
330 */
331 struct st_framebuffer
332 {
333 struct gl_framebuffer Base;
334
335 struct st_framebuffer_iface *iface;
336 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
337 unsigned num_statts;
338 int32_t stamp;
339 int32_t iface_stamp;
340 uint32_t iface_ID;
341
342 /* list of framebuffer objects */
343 struct list_head head;
344 };
345
346
347 extern void st_init_driver_functions(struct pipe_screen *screen,
348 struct dd_function_table *functions);
349
350 void
351 st_invalidate_buffers(struct st_context *st);
352
353 /* Invalidate the readpixels cache to ensure we don't read stale data.
354 */
355 static inline void
356 st_invalidate_readpix_cache(struct st_context *st)
357 {
358 if (unlikely(st->readpix_cache.src)) {
359 pipe_resource_reference(&st->readpix_cache.src, NULL);
360 pipe_resource_reference(&st->readpix_cache.cache, NULL);
361 }
362 }
363
364
365 #define Y_0_TOP 1
366 #define Y_0_BOTTOM 2
367
368 static inline GLuint
369 st_fb_orientation(const struct gl_framebuffer *fb)
370 {
371 if (fb && _mesa_is_winsys_fbo(fb)) {
372 /* Drawing into a window (on-screen buffer).
373 *
374 * Negate Y scale to flip image vertically.
375 * The NDC Y coords prior to viewport transformation are in the range
376 * [y=-1=bottom, y=1=top]
377 * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
378 * H is the window height.
379 * Use the viewport transformation to invert Y.
380 */
381 return Y_0_TOP;
382 }
383 else {
384 /* Drawing into user-created FBO (very likely a texture).
385 *
386 * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
387 */
388 return Y_0_BOTTOM;
389 }
390 }
391
392
393 static inline bool
394 st_user_clip_planes_enabled(struct gl_context *ctx)
395 {
396 return (ctx->API == API_OPENGL_COMPAT ||
397 ctx->API == API_OPENGLES) && /* only ES 1.x */
398 ctx->Transform.ClipPlanesEnabled;
399 }
400
401 /** clear-alloc a struct-sized object, with casting */
402 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
403
404
405 extern struct st_context *
406 st_create_context(gl_api api, struct pipe_context *pipe,
407 const struct gl_config *visual,
408 struct st_context *share,
409 const struct st_config_options *options,
410 bool no_error);
411
412 extern void
413 st_destroy_context(struct st_context *st);
414
415 uint64_t
416 st_get_active_states(struct gl_context *ctx);
417
418
419 #ifdef __cplusplus
420 }
421 #endif
422
423 #endif