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