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