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