st/mesa: expand glDrawPixels cache to handle multiple images
[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
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43
44 struct dd_function_table;
45 struct draw_context;
46 struct draw_stage;
47 struct gen_mipmap_state;
48 struct st_context;
49 struct st_fragment_program;
50 struct st_perf_monitor_group;
51 struct u_upload_mgr;
52
53
54 /** For drawing quads for glClear, glDraw/CopyPixels, glBitmap, etc. */
55 struct st_util_vertex
56 {
57 float x, y, z;
58 float r, g, b, a;
59 float s, t;
60 };
61
62 struct st_bitmap_cache
63 {
64 /** Window pos to render the cached image */
65 GLint xpos, ypos;
66 /** Bounds of region used in window coords */
67 GLint xmin, ymin, xmax, ymax;
68
69 GLfloat color[4];
70
71 /** Bitmap's Z position */
72 GLfloat zpos;
73
74 struct pipe_resource *texture;
75 struct pipe_transfer *trans;
76
77 GLboolean empty;
78
79 /** An I8 texture image: */
80 ubyte *buffer;
81 };
82
83 struct st_bound_handles
84 {
85 unsigned num_handles;
86 uint64_t *handles;
87 };
88
89
90 #define NUM_DRAWPIX_CACHE_ENTRIES 4
91
92 struct drawpix_cache_entry
93 {
94 GLsizei width, height;
95 GLenum format, type;
96 const void *user_pointer; /**< Last user 'pixels' pointer */
97 void *image; /**< Copy of the glDrawPixels image data */
98 struct pipe_resource *texture;
99 unsigned age;
100 };
101
102
103 struct st_context
104 {
105 struct st_context_iface iface;
106
107 struct gl_context *ctx;
108
109 struct pipe_context *pipe;
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 boolean can_bind_const_buffer_as_vertex;
128
129 /**
130 * If a shader can be created when we get its source.
131 * This means it has only 1 variant, not counting glBitmap and
132 * glDrawPixels.
133 */
134 boolean shader_has_one_variant[MESA_SHADER_STAGES];
135
136 boolean needs_texcoord_semantic;
137 boolean apply_texture_swizzle_to_border_color;
138
139 /* On old libGL's for linux we need to invalidate the drawables
140 * on glViewpport calls, this is set via a option.
141 */
142 boolean invalidate_on_gl_viewport;
143 boolean draw_needs_minmax_index;
144 boolean vertex_array_out_of_memory;
145 boolean has_hw_atomics;
146
147 /* Some state is contained in constant objects.
148 * Other state is just parameter values.
149 */
150 struct {
151 struct pipe_blend_state blend;
152 struct pipe_depth_stencil_alpha_state depth_stencil;
153 struct pipe_rasterizer_state rasterizer;
154 struct pipe_sampler_state samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
155 GLuint num_samplers[PIPE_SHADER_TYPES];
156 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
157 GLuint num_sampler_views[PIPE_SHADER_TYPES];
158 struct pipe_clip_state clip;
159 struct {
160 void *ptr;
161 unsigned size;
162 } constants[PIPE_SHADER_TYPES];
163 unsigned fb_width;
164 unsigned fb_height;
165 unsigned fb_num_samples;
166 unsigned fb_num_layers;
167 unsigned num_viewports;
168 struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
169 struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
170 struct {
171 unsigned num;
172 boolean include;
173 struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
174 } window_rects;
175
176 GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
177
178 GLuint fb_orientation;
179 } state;
180
181 uint64_t dirty; /**< dirty states */
182
183 /** This masks out unused shader resources. Only valid in draw calls. */
184 uint64_t active_states;
185
186 /* If true, further analysis of states is required to know if something
187 * has changed. Used mainly for shaders.
188 */
189 bool gfx_shaders_may_be_dirty;
190 bool compute_shader_may_be_dirty;
191
192 GLboolean vertdata_edgeflags;
193 GLboolean edgeflag_culls_prims;
194
195 struct st_vertex_program *vp; /**< Currently bound vertex program */
196 struct st_fragment_program *fp; /**< Currently bound fragment program */
197 struct st_common_program *gp; /**< Currently bound geometry program */
198 struct st_common_program *tcp; /**< Currently bound tess control program */
199 struct st_common_program *tep; /**< Currently bound tess eval program */
200 struct st_compute_program *cp; /**< Currently bound compute program */
201
202 struct st_vp_variant *vp_variant;
203
204 struct {
205 struct pipe_resource *pixelmap_texture;
206 struct pipe_sampler_view *pixelmap_sampler_view;
207 } pixel_xfer;
208
209 /** for glBitmap */
210 struct {
211 struct pipe_rasterizer_state rasterizer;
212 struct pipe_sampler_state sampler;
213 struct pipe_sampler_state atlas_sampler;
214 enum pipe_format tex_format;
215 void *vs;
216 struct st_bitmap_cache cache;
217 } bitmap;
218
219 /** for glDraw/CopyPixels */
220 struct {
221 void *zs_shaders[4];
222 void *vert_shaders[2]; /**< ureg shaders */
223 } drawpix;
224
225 /** Cache of glDrawPixels images */
226 struct {
227 struct drawpix_cache_entry entries[NUM_DRAWPIX_CACHE_ENTRIES];
228 unsigned age;
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[3];
258 void *download_fs[3][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 enum pipe_reset_status reset_status;
288
289 /* Array of bound texture/image handles which are resident in the context.
290 */
291 struct st_bound_handles bound_texture_handles[PIPE_SHADER_TYPES];
292 struct st_bound_handles bound_image_handles[PIPE_SHADER_TYPES];
293
294 /* Winsys buffers */
295 struct list_head winsys_buffers;
296 };
297
298
299 /* Need this so that we can implement Mesa callbacks in this module.
300 */
301 static inline struct st_context *st_context(struct gl_context *ctx)
302 {
303 return ctx->st;
304 }
305
306
307 /**
308 * Wrapper for struct gl_framebuffer.
309 * This is an opaque type to the outside world.
310 */
311 struct st_framebuffer
312 {
313 struct gl_framebuffer Base;
314
315 struct st_framebuffer_iface *iface;
316 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
317 unsigned num_statts;
318 int32_t stamp;
319 int32_t iface_stamp;
320 uint32_t iface_ID;
321
322 /* list of framebuffer objects */
323 struct list_head head;
324 };
325
326
327 extern void st_init_driver_functions(struct pipe_screen *screen,
328 struct dd_function_table *functions);
329
330 void
331 st_invalidate_buffers(struct st_context *st);
332
333 /* Invalidate the readpixels cache to ensure we don't read stale data.
334 */
335 static inline void
336 st_invalidate_readpix_cache(struct st_context *st)
337 {
338 if (unlikely(st->readpix_cache.src)) {
339 pipe_resource_reference(&st->readpix_cache.src, NULL);
340 pipe_resource_reference(&st->readpix_cache.cache, NULL);
341 }
342 }
343
344
345 #define Y_0_TOP 1
346 #define Y_0_BOTTOM 2
347
348 static inline GLuint
349 st_fb_orientation(const struct gl_framebuffer *fb)
350 {
351 if (fb && _mesa_is_winsys_fbo(fb)) {
352 /* Drawing into a window (on-screen buffer).
353 *
354 * Negate Y scale to flip image vertically.
355 * The NDC Y coords prior to viewport transformation are in the range
356 * [y=-1=bottom, y=1=top]
357 * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
358 * H is the window height.
359 * Use the viewport transformation to invert Y.
360 */
361 return Y_0_TOP;
362 }
363 else {
364 /* Drawing into user-created FBO (very likely a texture).
365 *
366 * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
367 */
368 return Y_0_BOTTOM;
369 }
370 }
371
372
373 static inline bool
374 st_user_clip_planes_enabled(struct gl_context *ctx)
375 {
376 return (ctx->API == API_OPENGL_COMPAT ||
377 ctx->API == API_OPENGLES) && /* only ES 1.x */
378 ctx->Transform.ClipPlanesEnabled;
379 }
380
381 /** clear-alloc a struct-sized object, with casting */
382 #define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
383
384
385 extern struct st_context *
386 st_create_context(gl_api api, struct pipe_context *pipe,
387 const struct gl_config *visual,
388 struct st_context *share,
389 const struct st_config_options *options,
390 bool no_error);
391
392 extern void
393 st_destroy_context(struct st_context *st);
394
395 uint64_t
396 st_get_active_states(struct gl_context *ctx);
397
398
399 #ifdef __cplusplus
400 }
401 #endif
402
403 #endif