meta: Always restore the framebuffers and current renderbuffer.
[mesa.git] / src / mesa / drivers / common / meta.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #ifndef META_H
27 #define META_H
28
29 #include "main/mtypes.h"
30
31 /**
32 * \name Flags for meta operations
33 * \{
34 *
35 * These flags are passed to _mesa_meta_begin().
36 */
37 #define MESA_META_ALL ~0x0
38 #define MESA_META_ALPHA_TEST 0x1
39 #define MESA_META_BLEND 0x2 /**< includes logicop */
40 #define MESA_META_COLOR_MASK 0x4
41 #define MESA_META_DEPTH_TEST 0x8
42 #define MESA_META_FOG 0x10
43 #define MESA_META_PIXEL_STORE 0x20
44 #define MESA_META_PIXEL_TRANSFER 0x40
45 #define MESA_META_RASTERIZATION 0x80
46 #define MESA_META_SCISSOR 0x100
47 #define MESA_META_SHADER 0x200
48 #define MESA_META_STENCIL_TEST 0x400
49 #define MESA_META_TRANSFORM 0x800 /**< modelview/projection matrix state */
50 #define MESA_META_TEXTURE 0x1000
51 #define MESA_META_VERTEX 0x2000
52 #define MESA_META_VIEWPORT 0x4000
53 #define MESA_META_CLAMP_FRAGMENT_COLOR 0x8000
54 #define MESA_META_CLAMP_VERTEX_COLOR 0x10000
55 #define MESA_META_CONDITIONAL_RENDER 0x20000
56 #define MESA_META_CLIP 0x40000
57 #define MESA_META_SELECT_FEEDBACK 0x80000
58 #define MESA_META_MULTISAMPLE 0x100000
59 #define MESA_META_FRAMEBUFFER_SRGB 0x200000
60 #define MESA_META_OCCLUSION_QUERY 0x400000
61 /**\}*/
62
63 /**
64 * State which we may save/restore across meta ops.
65 * XXX this may be incomplete...
66 */
67 struct save_state
68 {
69 GLbitfield SavedState; /**< bitmask of MESA_META_* flags */
70
71 /* Always saved/restored with meta. */
72 gl_api API;
73
74 /** MESA_META_CLEAR (and others?) */
75 struct gl_query_object *CurrentOcclusionObject;
76
77 /** MESA_META_ALPHA_TEST */
78 GLboolean AlphaEnabled;
79 GLenum AlphaFunc;
80 GLclampf AlphaRef;
81
82 /** MESA_META_BLEND */
83 GLbitfield BlendEnabled;
84 GLboolean ColorLogicOpEnabled;
85
86 /** MESA_META_COLOR_MASK */
87 GLubyte ColorMask[MAX_DRAW_BUFFERS][4];
88
89 /** MESA_META_DEPTH_TEST */
90 struct gl_depthbuffer_attrib Depth;
91
92 /** MESA_META_FOG */
93 GLboolean Fog;
94
95 /** MESA_META_PIXEL_STORE */
96 struct gl_pixelstore_attrib Pack, Unpack;
97
98 /** MESA_META_PIXEL_TRANSFER */
99 GLfloat RedBias, RedScale;
100 GLfloat GreenBias, GreenScale;
101 GLfloat BlueBias, BlueScale;
102 GLfloat AlphaBias, AlphaScale;
103 GLfloat DepthBias, DepthScale;
104 GLboolean MapColorFlag;
105
106 /** MESA_META_RASTERIZATION */
107 GLenum FrontPolygonMode, BackPolygonMode;
108 GLboolean PolygonOffset;
109 GLboolean PolygonSmooth;
110 GLboolean PolygonStipple;
111 GLboolean PolygonCull;
112
113 /** MESA_META_SCISSOR */
114 struct gl_scissor_attrib Scissor;
115
116 /** MESA_META_SHADER */
117 GLboolean VertexProgramEnabled;
118 struct gl_vertex_program *VertexProgram;
119 GLboolean FragmentProgramEnabled;
120 struct gl_fragment_program *FragmentProgram;
121 GLboolean ATIFragmentShaderEnabled;
122 struct gl_shader_program *Shader[MESA_SHADER_STAGES];
123 struct gl_shader_program *ActiveShader;
124
125 /** MESA_META_STENCIL_TEST */
126 struct gl_stencil_attrib Stencil;
127
128 /** MESA_META_TRANSFORM */
129 GLenum MatrixMode;
130 GLfloat ModelviewMatrix[16];
131 GLfloat ProjectionMatrix[16];
132 GLfloat TextureMatrix[16];
133
134 /** MESA_META_CLIP */
135 GLbitfield ClipPlanesEnabled;
136
137 /** MESA_META_TEXTURE */
138 GLuint ActiveUnit;
139 GLuint ClientActiveUnit;
140 /** for unit[0] only */
141 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
142 /** mask of TEXTURE_2D_BIT, etc */
143 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
144 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
145 GLuint EnvMode; /* unit[0] only */
146
147 /** MESA_META_VERTEX */
148 struct gl_vertex_array_object *VAO;
149 struct gl_buffer_object *ArrayBufferObj;
150
151 /** MESA_META_VIEWPORT */
152 GLfloat ViewportX, ViewportY, ViewportW, ViewportH;
153 GLclampd DepthNear, DepthFar;
154
155 /** MESA_META_CLAMP_FRAGMENT_COLOR */
156 GLenum ClampFragmentColor;
157
158 /** MESA_META_CLAMP_VERTEX_COLOR */
159 GLenum ClampVertexColor;
160
161 /** MESA_META_CONDITIONAL_RENDER */
162 struct gl_query_object *CondRenderQuery;
163 GLenum CondRenderMode;
164
165 /** MESA_META_SELECT_FEEDBACK */
166 GLenum RenderMode;
167 struct gl_selection Select;
168 struct gl_feedback Feedback;
169
170 /** MESA_META_MULTISAMPLE */
171 struct gl_multisample_attrib Multisample;
172
173 /** MESA_META_FRAMEBUFFER_SRGB */
174 GLboolean sRGBEnabled;
175
176 /** Miscellaneous (always disabled) */
177 GLboolean Lighting;
178 GLboolean RasterDiscard;
179 GLboolean TransformFeedbackNeedsResume;
180
181 GLuint DrawBufferName, ReadBufferName, RenderbufferName;
182 };
183
184 /**
185 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
186 * This is currently shared by all the meta ops. But we could create a
187 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
188 */
189 struct temp_texture
190 {
191 GLuint TexObj;
192 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
193 GLsizei MinSize; /**< Min texture size to allocate */
194 GLsizei MaxSize; /**< Max possible texture size */
195 GLboolean NPOT; /**< Non-power of two size OK? */
196 GLsizei Width, Height; /**< Current texture size */
197 GLenum IntFormat;
198 GLfloat Sright, Ttop; /**< right, top texcoords */
199 };
200
201 /**
202 * State for GLSL texture sampler which is used to generate fragment
203 * shader in _mesa_meta_generate_mipmap().
204 */
205 struct blit_shader {
206 const char *type;
207 const char *func;
208 const char *texcoords;
209 GLuint shader_prog;
210 };
211
212 /**
213 * Table of all sampler types and shaders for accessing them.
214 */
215 struct blit_shader_table {
216 struct blit_shader sampler_1d;
217 struct blit_shader sampler_2d;
218 struct blit_shader sampler_3d;
219 struct blit_shader sampler_rect;
220 struct blit_shader sampler_cubemap;
221 struct blit_shader sampler_1d_array;
222 struct blit_shader sampler_2d_array;
223 struct blit_shader sampler_cubemap_array;
224 };
225
226 /**
227 * Indices in the blit_state->msaa_shaders[] array
228 *
229 * Note that setup_glsl_msaa_blit_shader() assumes that the _INT enums are one
230 * more than the non-_INT version and _UINT is one beyond that.
231 */
232 enum blit_msaa_shader {
233 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE,
234 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT,
235 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT,
236 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY,
237 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_INT,
238 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_UINT,
239 BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE,
240 BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY,
241 BLIT_MSAA_SHADER_COUNT,
242 };
243
244 /**
245 * State for glBlitFramebufer()
246 */
247 struct blit_state
248 {
249 GLuint VAO;
250 GLuint VBO;
251 GLuint DepthFP;
252 struct blit_shader_table shaders;
253 GLuint msaa_shaders[BLIT_MSAA_SHADER_COUNT];
254 struct temp_texture depthTex;
255 };
256
257
258 /**
259 * State for glClear()
260 */
261 struct clear_state
262 {
263 GLuint VAO;
264 GLuint VBO;
265 GLuint ShaderProg;
266 GLint ColorLocation;
267 GLint LayerLocation;
268
269 GLuint IntegerShaderProg;
270 GLint IntegerColorLocation;
271 GLint IntegerLayerLocation;
272 };
273
274
275 /**
276 * State for glCopyPixels()
277 */
278 struct copypix_state
279 {
280 GLuint VAO;
281 GLuint VBO;
282 };
283
284
285 /**
286 * State for glDrawPixels()
287 */
288 struct drawpix_state
289 {
290 GLuint VAO;
291 GLuint VBO;
292
293 GLuint StencilFP; /**< Fragment program for drawing stencil images */
294 GLuint DepthFP; /**< Fragment program for drawing depth images */
295 };
296
297
298 /**
299 * State for glBitmap()
300 */
301 struct bitmap_state
302 {
303 GLuint VAO;
304 GLuint VBO;
305 struct temp_texture Tex; /**< separate texture from other meta ops */
306 };
307
308 /**
309 * State for _mesa_meta_generate_mipmap()
310 */
311 struct gen_mipmap_state
312 {
313 GLuint VAO;
314 GLuint VBO;
315 GLuint FBO;
316 GLuint Sampler;
317
318 struct blit_shader_table shaders;
319 };
320
321 /**
322 * State for texture decompression
323 */
324 struct decompress_state
325 {
326 GLuint VAO;
327 GLuint VBO, FBO, RBO, Sampler;
328 GLint Width, Height;
329
330 struct blit_shader_table shaders;
331 };
332
333 /**
334 * State for glDrawTex()
335 */
336 struct drawtex_state
337 {
338 GLuint VAO;
339 GLuint VBO;
340 };
341
342 #define MAX_META_OPS_DEPTH 8
343 /**
344 * All per-context meta state.
345 */
346 struct gl_meta_state
347 {
348 /** Stack of state saved during meta-ops */
349 struct save_state Save[MAX_META_OPS_DEPTH];
350 /** Save stack depth */
351 GLuint SaveStackDepth;
352
353 struct temp_texture TempTex;
354
355 struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
356 struct clear_state Clear; /**< For _mesa_meta_Clear() */
357 struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
358 struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
359 struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
360 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
361 struct decompress_state Decompress; /**< For texture decompression */
362 struct drawtex_state DrawTex; /**< For _mesa_meta_DrawTex() */
363 };
364
365 struct vertex {
366 GLfloat x, y, z, tex[4];
367 GLfloat r, g, b, a;
368 };
369
370 extern void
371 _mesa_meta_init(struct gl_context *ctx);
372
373 extern void
374 _mesa_meta_free(struct gl_context *ctx);
375
376 extern void
377 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state);
378
379 extern void
380 _mesa_meta_end(struct gl_context *ctx);
381
382 extern GLboolean
383 _mesa_meta_in_progress(struct gl_context *ctx);
384
385 extern void
386 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
387 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
388 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
389 GLbitfield mask, GLenum filter);
390
391 extern void
392 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers);
393
394 extern void
395 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers);
396
397 extern void
398 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
399 GLsizei width, GLsizei height,
400 GLint dstx, GLint dsty, GLenum type);
401
402 extern void
403 _mesa_meta_DrawPixels(struct gl_context *ctx,
404 GLint x, GLint y, GLsizei width, GLsizei height,
405 GLenum format, GLenum type,
406 const struct gl_pixelstore_attrib *unpack,
407 const GLvoid *pixels);
408
409 extern void
410 _mesa_meta_Bitmap(struct gl_context *ctx,
411 GLint x, GLint y, GLsizei width, GLsizei height,
412 const struct gl_pixelstore_attrib *unpack,
413 const GLubyte *bitmap);
414
415 extern void
416 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
417 struct gl_texture_object *texObj);
418
419 extern void
420 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
421 struct gl_texture_image *texImage,
422 GLint xoffset, GLint yoffset, GLint slice,
423 struct gl_renderbuffer *rb,
424 GLint x, GLint y,
425 GLsizei width, GLsizei height);
426
427 extern void
428 _mesa_meta_GetTexImage(struct gl_context *ctx,
429 GLenum format, GLenum type, GLvoid *pixels,
430 struct gl_texture_image *texImage);
431
432 extern void
433 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
434 GLfloat width, GLfloat height);
435
436 /* meta-internal functions */
437 GLuint
438 _mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum target,
439 const GLcharARB *source);
440
441
442 GLuint
443 _mesa_meta_link_program_with_debug(struct gl_context *ctx, GLuint program);
444
445 GLboolean
446 _mesa_meta_alloc_texture(struct temp_texture *tex,
447 GLsizei width, GLsizei height, GLenum intFormat);
448
449 void
450 _mesa_meta_setup_texture_coords(GLenum faceTarget,
451 GLint slice,
452 GLint width,
453 GLint height,
454 GLint depth,
455 GLfloat coords0[4],
456 GLfloat coords1[4],
457 GLfloat coords2[4],
458 GLfloat coords3[4]);
459
460 struct temp_texture *
461 _mesa_meta_get_temp_texture(struct gl_context *ctx);
462
463 struct temp_texture *
464 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx);
465
466 void
467 _mesa_meta_setup_vertex_objects(GLuint *VAO, GLuint *VBO,
468 bool use_generic_attributes,
469 unsigned vertex_size, unsigned texcoord_size,
470 unsigned color_size);
471
472 void
473 _mesa_meta_setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO,
474 unsigned texcoord_size);
475
476 void
477 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
478 struct temp_texture *tex,
479 GLboolean newTex,
480 GLsizei width, GLsizei height,
481 GLenum format, GLenum type,
482 const GLvoid *pixels);
483
484 void
485 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
486 struct temp_texture *tex,
487 GLint srcX, GLint srcY,
488 GLsizei width, GLsizei height,
489 GLenum intFormat,
490 GLenum filter);
491
492 void
493 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
494 GLenum target,
495 struct blit_shader_table *table);
496
497 void
498 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit);
499
500 void
501 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table *table);
502
503 void
504 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap);
505
506 #endif /* META_H */