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