st/mesa: add readpix_cache structure
[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 "pipe/p_state.h"
33 #include "state_tracker/st_api.h"
34 #include "main/fbobject.h"
35
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 struct bitmap_cache;
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 /* gap */
54 #define ST_NEW_FRAGMENT_PROGRAM (1 << 1)
55 #define ST_NEW_VERTEX_PROGRAM (1 << 2)
56 #define ST_NEW_FRAMEBUFFER (1 << 3)
57 #define ST_NEW_TESS_STATE (1 << 4)
58 #define ST_NEW_GEOMETRY_PROGRAM (1 << 5)
59 #define ST_NEW_VERTEX_ARRAYS (1 << 6)
60 #define ST_NEW_RASTERIZER (1 << 7)
61 #define ST_NEW_UNIFORM_BUFFER (1 << 8)
62 #define ST_NEW_TESSCTRL_PROGRAM (1 << 9)
63 #define ST_NEW_TESSEVAL_PROGRAM (1 << 10)
64 #define ST_NEW_SAMPLER_VIEWS (1 << 11)
65 #define ST_NEW_ATOMIC_BUFFER (1 << 12)
66 #define ST_NEW_STORAGE_BUFFER (1 << 13)
67 #define ST_NEW_COMPUTE_PROGRAM (1 << 14)
68 #define ST_NEW_IMAGE_UNITS (1 << 15)
69
70
71 struct st_state_flags {
72 GLbitfield mesa; /**< Mask of _NEW_x flags */
73 uint64_t st; /**< Mask of ST_NEW_x flags */
74 };
75
76 struct st_tracked_state {
77 const char *name;
78 struct st_state_flags dirty;
79 void (*update)( struct st_context *st );
80 };
81
82
83 /**
84 * Enumeration of state tracker pipelines.
85 */
86 enum st_pipeline {
87 ST_PIPELINE_RENDER,
88 ST_PIPELINE_COMPUTE,
89 };
90
91
92 /** For drawing quads for glClear, glDraw/CopyPixels, glBitmap, etc. */
93 struct st_util_vertex
94 {
95 float x, y, z;
96 float r, g, b, a;
97 float s, t;
98 };
99
100
101 struct st_context
102 {
103 struct st_context_iface iface;
104
105 struct gl_context *ctx;
106
107 struct pipe_context *pipe;
108
109 struct u_upload_mgr *uploader, *indexbuf_uploader, *constbuf_uploader;
110
111 struct draw_context *draw; /**< For selection/feedback/rastpos only */
112 struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */
113 struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */
114 struct draw_stage *rastpos_stage; /**< For glRasterPos */
115 GLboolean clamp_frag_color_in_shader;
116 GLboolean clamp_vert_color_in_shader;
117 boolean has_stencil_export; /**< can do shader stencil export? */
118 boolean has_time_elapsed;
119 boolean has_shader_model3;
120 boolean has_etc1;
121 boolean has_etc2;
122 boolean prefer_blit_based_texture_transfer;
123 boolean force_persample_in_shader;
124 boolean has_shareable_shaders;
125 boolean has_half_float_packing;
126 boolean has_multi_draw_indirect;
127
128 /**
129 * If a shader can be created when we get its source.
130 * This means it has only 1 variant, not counting glBitmap and
131 * glDrawPixels.
132 */
133 boolean shader_has_one_variant[MESA_SHADER_STAGES];
134
135 boolean needs_texcoord_semantic;
136 boolean apply_texture_swizzle_to_border_color;
137
138 /* On old libGL's for linux we need to invalidate the drawables
139 * on glViewpport calls, this is set via a option.
140 */
141 boolean invalidate_on_gl_viewport;
142
143 boolean vertex_array_out_of_memory;
144
145 /* Some state is contained in constant objects.
146 * Other state is just parameter values.
147 */
148 struct {
149 struct pipe_blend_state blend;
150 struct pipe_depth_stencil_alpha_state depth_stencil;
151 struct pipe_rasterizer_state rasterizer;
152 struct pipe_sampler_state samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
153 GLuint num_samplers[PIPE_SHADER_TYPES];
154 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
155 GLuint num_sampler_views[PIPE_SHADER_TYPES];
156 struct pipe_clip_state clip;
157 struct {
158 void *ptr;
159 unsigned size;
160 } constants[PIPE_SHADER_TYPES];
161 struct pipe_framebuffer_state framebuffer;
162 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
163 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
164 struct {
165 unsigned num;
166 boolean include;
167 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
168 } window_rects;
169 unsigned sample_mask;
170
171 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
172
173 GLuint fb_orientation;
174 } state;
175
176 char vendor[100];
177 char renderer[100];
178
179 struct st_state_flags dirty;
180 struct st_state_flags dirty_cp;
181
182 GLboolean vertdata_edgeflags;
183 GLboolean edgeflag_culls_prims;
184
185 /** Mapping from VARYING_SLOT_x to post-transformed vertex slot */
186 const GLuint *vertex_result_to_slot;
187
188 struct st_vertex_program *vp; /**< Currently bound vertex program */
189 struct st_fragment_program *fp; /**< Currently bound fragment program */
190 struct st_geometry_program *gp; /**< Currently bound geometry program */
191 struct st_tessctrl_program *tcp; /**< Currently bound tess control program */
192 struct st_tesseval_program *tep; /**< Currently bound tess eval program */
193 struct st_compute_program *cp; /**< Currently bound compute program */
194
195 struct st_vp_variant *vp_variant;
196 struct st_fp_variant *fp_variant;
197 struct st_basic_variant *gp_variant;
198 struct st_basic_variant *tcp_variant;
199 struct st_basic_variant *tep_variant;
200 struct st_basic_variant *cp_variant;
201
202 struct {
203 struct pipe_resource *pixelmap_texture;
204 struct pipe_sampler_view *pixelmap_sampler_view;
205 } pixel_xfer;
206
207 /** for glBitmap */
208 struct {
209 struct pipe_rasterizer_state rasterizer;
210 struct pipe_sampler_state sampler;
211 struct pipe_sampler_state atlas_sampler;
212 enum pipe_format tex_format;
213 void *vs;
214 struct bitmap_cache *cache;
215 } bitmap;
216
217 /** for glDraw/CopyPixels */
218 struct {
219 void *zs_shaders[4];
220 void *vert_shaders[2]; /**< ureg shaders */
221 } drawpix;
222
223 struct {
224 GLsizei width, height;
225 GLenum format, type;
226 const void *user_pointer; /**< Last user 'pixels' pointer */
227 void *image; /**< Copy of the glDrawPixels image data */
228 struct pipe_resource *texture;
229 } drawpix_cache;
230
231 /** for glReadPixels */
232 struct {
233 struct pipe_resource *src;
234 struct pipe_resource *cache;
235 enum pipe_format dst_format;
236 unsigned level;
237 unsigned layer;
238 unsigned hits;
239 } readpix_cache;
240
241 /** for glClear */
242 struct {
243 struct pipe_rasterizer_state raster;
244 struct pipe_viewport_state viewport;
245 void *vs;
246 void *fs;
247 void *vs_layered;
248 void *gs_layered;
249 } clear;
250
251 /* For gl(Compressed)Tex(Sub)Image */
252 struct {
253 struct pipe_rasterizer_state raster;
254 struct pipe_blend_state upload_blend;
255 void *vs;
256 void *gs;
257 void *upload_fs;
258 void *download_fs[PIPE_MAX_TEXTURE_TYPES];
259 bool upload_enabled;
260 bool download_enabled;
261 bool rgba_only;
262 bool layers;
263 bool use_gs;
264 } pbo;
265
266 /** for drawing with st_util_vertex */
267 struct pipe_vertex_element util_velems[3];
268
269 void *passthrough_fs; /**< simple pass-through frag shader */
270
271 enum pipe_texture_target internal_target;
272
273 struct cso_context *cso_context;
274
275 void *winsys_drawable_handle;
276
277 /* The number of vertex buffers from the last call of validate_arrays. */
278 unsigned last_num_vbuffers;
279
280 int32_t draw_stamp;
281 int32_t read_stamp;
282
283 struct st_config_options options;
284
285 struct st_perf_monitor_group *perfmon;
286 };
287
288
289 /* Need this so that we can implement Mesa callbacks in this module.
290 */
291 static inline struct st_context *st_context(struct gl_context *ctx)
292 {
293 return ctx->st;
294 }
295
296
297 /**
298 * Wrapper for struct gl_framebuffer.
299 * This is an opaque type to the outside world.
300 */
301 struct st_framebuffer
302 {
303 struct gl_framebuffer Base;
304 void *Private;
305
306 struct st_framebuffer_iface *iface;
307 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
308 unsigned num_statts;
309 int32_t stamp;
310 int32_t iface_stamp;
311 };
312
313
314 extern void st_init_driver_functions(struct pipe_screen *screen,
315 struct dd_function_table *functions);
316
317 void st_invalidate_state(struct gl_context * ctx, GLbitfield new_state);
318
319 void st_invalidate_readpix_cache(struct st_context *st);
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 unsigned
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
373 /** clear-alloc a struct-sized object, with casting */
374 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
375
376
377 extern struct st_context *
378 st_create_context(gl_api api, struct pipe_context *pipe,
379 const struct gl_config *visual,
380 struct st_context *share,
381 const struct st_config_options *options);
382
383 extern void
384 st_destroy_context(struct st_context *st);
385
386
387 #ifdef __cplusplus
388 }
389 #endif
390
391 #endif