meta: Refactor state save/restore for framebuffer texture blits
[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 #define MESA_META_DRAW_BUFFERS 0x800000
62 /**\}*/
63
64 /**
65 * State which we may save/restore across meta ops.
66 * XXX this may be incomplete...
67 */
68 struct save_state
69 {
70 GLbitfield SavedState; /**< bitmask of MESA_META_* flags */
71
72 /* Always saved/restored with meta. */
73 gl_api API;
74
75 /** MESA_META_CLEAR (and others?) */
76 struct gl_query_object *CurrentOcclusionObject;
77
78 /** MESA_META_ALPHA_TEST */
79 GLboolean AlphaEnabled;
80 GLenum AlphaFunc;
81 GLclampf AlphaRef;
82
83 /** MESA_META_BLEND */
84 GLbitfield BlendEnabled;
85 GLboolean ColorLogicOpEnabled;
86
87 /** MESA_META_COLOR_MASK */
88 GLubyte ColorMask[MAX_DRAW_BUFFERS][4];
89
90 /** MESA_META_DEPTH_TEST */
91 struct gl_depthbuffer_attrib Depth;
92
93 /** MESA_META_FOG */
94 GLboolean Fog;
95
96 /** MESA_META_PIXEL_STORE */
97 struct gl_pixelstore_attrib Pack, Unpack;
98
99 /** MESA_META_PIXEL_TRANSFER */
100 GLfloat RedBias, RedScale;
101 GLfloat GreenBias, GreenScale;
102 GLfloat BlueBias, BlueScale;
103 GLfloat AlphaBias, AlphaScale;
104 GLfloat DepthBias, DepthScale;
105 GLboolean MapColorFlag;
106
107 /** MESA_META_RASTERIZATION */
108 GLenum FrontPolygonMode, BackPolygonMode;
109 GLboolean PolygonOffset;
110 GLboolean PolygonSmooth;
111 GLboolean PolygonStipple;
112 GLboolean PolygonCull;
113
114 /** MESA_META_SCISSOR */
115 struct gl_scissor_attrib Scissor;
116
117 /** MESA_META_SHADER */
118 GLboolean VertexProgramEnabled;
119 struct gl_vertex_program *VertexProgram;
120 GLboolean FragmentProgramEnabled;
121 struct gl_fragment_program *FragmentProgram;
122 GLboolean ATIFragmentShaderEnabled;
123 struct gl_shader_program *Shader[MESA_SHADER_STAGES];
124 struct gl_shader_program *ActiveShader;
125 struct gl_pipeline_object *Pipeline;
126
127 /** MESA_META_STENCIL_TEST */
128 struct gl_stencil_attrib Stencil;
129
130 /** MESA_META_TRANSFORM */
131 GLenum MatrixMode;
132 GLfloat ModelviewMatrix[16];
133 GLfloat ProjectionMatrix[16];
134 GLfloat TextureMatrix[16];
135
136 /** MESA_META_CLIP */
137 GLbitfield ClipPlanesEnabled;
138
139 /** MESA_META_TEXTURE */
140 GLuint ActiveUnit;
141 GLuint ClientActiveUnit;
142 /** for unit[0] only */
143 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
144 /** mask of TEXTURE_2D_BIT, etc */
145 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
146 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
147 GLuint EnvMode; /* unit[0] only */
148
149 /** MESA_META_VERTEX */
150 struct gl_vertex_array_object *VAO;
151 struct gl_buffer_object *ArrayBufferObj;
152
153 /** MESA_META_VIEWPORT */
154 GLfloat ViewportX, ViewportY, ViewportW, ViewportH;
155 GLclampd DepthNear, DepthFar;
156
157 /** MESA_META_CLAMP_FRAGMENT_COLOR */
158 GLenum ClampFragmentColor;
159
160 /** MESA_META_CLAMP_VERTEX_COLOR */
161 GLenum ClampVertexColor;
162
163 /** MESA_META_CONDITIONAL_RENDER */
164 struct gl_query_object *CondRenderQuery;
165 GLenum CondRenderMode;
166
167 /** MESA_META_SELECT_FEEDBACK */
168 GLenum RenderMode;
169 struct gl_selection Select;
170 struct gl_feedback Feedback;
171
172 /** MESA_META_MULTISAMPLE */
173 struct gl_multisample_attrib Multisample;
174
175 /** MESA_META_FRAMEBUFFER_SRGB */
176 GLboolean sRGBEnabled;
177
178 /** Miscellaneous (always disabled) */
179 GLboolean Lighting;
180 GLboolean RasterDiscard;
181 GLboolean TransformFeedbackNeedsResume;
182
183 GLuint DrawBufferName, ReadBufferName, RenderbufferName;
184
185 /** MESA_META_DRAW_BUFFERS */
186 GLenum ColorDrawBuffers[MAX_DRAW_BUFFERS];
187 };
188
189 /**
190 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
191 * This is currently shared by all the meta ops. But we could create a
192 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
193 */
194 struct temp_texture
195 {
196 GLuint TexObj;
197 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
198 GLsizei MinSize; /**< Min texture size to allocate */
199 GLsizei MaxSize; /**< Max possible texture size */
200 GLboolean NPOT; /**< Non-power of two size OK? */
201 GLsizei Width, Height; /**< Current texture size */
202 GLenum IntFormat;
203 GLfloat Sright, Ttop; /**< right, top texcoords */
204 };
205
206 /**
207 * State for GLSL texture sampler which is used to generate fragment
208 * shader in _mesa_meta_generate_mipmap().
209 */
210 struct blit_shader {
211 const char *type;
212 const char *func;
213 const char *texcoords;
214 GLuint shader_prog;
215 };
216
217 /**
218 * Table of all sampler types and shaders for accessing them.
219 */
220 struct blit_shader_table {
221 struct blit_shader sampler_1d;
222 struct blit_shader sampler_2d;
223 struct blit_shader sampler_3d;
224 struct blit_shader sampler_rect;
225 struct blit_shader sampler_cubemap;
226 struct blit_shader sampler_1d_array;
227 struct blit_shader sampler_2d_array;
228 struct blit_shader sampler_cubemap_array;
229 };
230
231 /**
232 * Indices in the blit_state->msaa_shaders[] array
233 *
234 * Note that setup_glsl_msaa_blit_shader() assumes that the _INT enums are one
235 * more than the non-_INT version and _UINT is one beyond that.
236 */
237 enum blit_msaa_shader {
238 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE,
239 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT,
240 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT,
241 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY,
242 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_INT,
243 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_UINT,
244 BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE,
245 BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY,
246 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE,
247 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE_INT,
248 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE_UINT,
249 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_COPY,
250 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_COPY_INT,
251 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_COPY_UINT,
252 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE,
253 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY,
254 BLIT_MSAA_SHADER_COUNT,
255 };
256
257 /**
258 * State for glBlitFramebufer()
259 */
260 struct blit_state
261 {
262 GLuint VAO;
263 GLuint VBO;
264 struct blit_shader_table shaders;
265 GLuint msaa_shaders[BLIT_MSAA_SHADER_COUNT];
266 struct temp_texture depthTex;
267 bool no_ctsi_fallback;
268 };
269
270 struct fb_tex_blit_state
271 {
272 GLint baseLevelSave, maxLevelSave;
273 GLuint sampler, samplerSave;
274 GLuint tempTex;
275 };
276
277
278 /**
279 * State for glClear()
280 */
281 struct clear_state
282 {
283 GLuint VAO;
284 GLuint VBO;
285 GLuint ShaderProg;
286 GLint ColorLocation;
287 GLint LayerLocation;
288
289 GLuint IntegerShaderProg;
290 GLint IntegerColorLocation;
291 GLint IntegerLayerLocation;
292 };
293
294
295 /**
296 * State for glCopyPixels()
297 */
298 struct copypix_state
299 {
300 GLuint VAO;
301 GLuint VBO;
302 };
303
304
305 /**
306 * State for glDrawPixels()
307 */
308 struct drawpix_state
309 {
310 GLuint VAO;
311 GLuint VBO;
312
313 GLuint StencilFP; /**< Fragment program for drawing stencil images */
314 GLuint DepthFP; /**< Fragment program for drawing depth images */
315 };
316
317
318 /**
319 * State for glBitmap()
320 */
321 struct bitmap_state
322 {
323 GLuint VAO;
324 GLuint VBO;
325 struct temp_texture Tex; /**< separate texture from other meta ops */
326 };
327
328 /**
329 * State for _mesa_meta_generate_mipmap()
330 */
331 struct gen_mipmap_state
332 {
333 GLuint VAO;
334 GLuint VBO;
335 GLuint FBO;
336 GLuint Sampler;
337
338 struct blit_shader_table shaders;
339 };
340
341 /**
342 * State for texture decompression
343 */
344 struct decompress_state
345 {
346 GLuint VAO;
347 GLuint VBO, FBO, RBO, Sampler;
348 GLint Width, Height;
349
350 struct blit_shader_table shaders;
351 };
352
353 /**
354 * State for glDrawTex()
355 */
356 struct drawtex_state
357 {
358 GLuint VAO;
359 GLuint VBO;
360 };
361
362 #define MAX_META_OPS_DEPTH 8
363 /**
364 * All per-context meta state.
365 */
366 struct gl_meta_state
367 {
368 /** Stack of state saved during meta-ops */
369 struct save_state Save[MAX_META_OPS_DEPTH];
370 /** Save stack depth */
371 GLuint SaveStackDepth;
372
373 struct temp_texture TempTex;
374
375 struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
376 struct clear_state Clear; /**< For _mesa_meta_Clear() */
377 struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
378 struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
379 struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
380 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
381 struct decompress_state Decompress; /**< For texture decompression */
382 struct drawtex_state DrawTex; /**< For _mesa_meta_DrawTex() */
383 };
384
385 struct vertex {
386 GLfloat x, y, z, tex[4];
387 GLfloat r, g, b, a;
388 };
389
390 extern void
391 _mesa_meta_init(struct gl_context *ctx);
392
393 extern void
394 _mesa_meta_free(struct gl_context *ctx);
395
396 extern void
397 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state);
398
399 extern void
400 _mesa_meta_end(struct gl_context *ctx);
401
402 extern GLboolean
403 _mesa_meta_in_progress(struct gl_context *ctx);
404
405 extern void
406 _mesa_meta_fb_tex_blit_begin(const struct gl_context *ctx,
407 struct fb_tex_blit_state *blit);
408
409 extern void
410 _mesa_meta_fb_tex_blit_end(const struct gl_context *ctx, GLenum target,
411 struct fb_tex_blit_state *blit);
412
413 extern GLboolean
414 _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
415 struct gl_renderbuffer *rb,
416 GLuint *tex,
417 struct gl_texture_object **texObj,
418 GLenum *target);
419
420 GLuint
421 _mesa_meta_setup_sampler(struct gl_context *ctx,
422 const struct gl_texture_object *texObj,
423 GLenum target, GLenum filter, GLuint srcLevel);
424
425 extern void
426 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
427 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
428 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
429 GLbitfield mask, GLenum filter);
430
431 extern void
432 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers);
433
434 extern void
435 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers);
436
437 extern void
438 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
439 GLsizei width, GLsizei height,
440 GLint dstx, GLint dsty, GLenum type);
441
442 extern void
443 _mesa_meta_DrawPixels(struct gl_context *ctx,
444 GLint x, GLint y, GLsizei width, GLsizei height,
445 GLenum format, GLenum type,
446 const struct gl_pixelstore_attrib *unpack,
447 const GLvoid *pixels);
448
449 extern void
450 _mesa_meta_Bitmap(struct gl_context *ctx,
451 GLint x, GLint y, GLsizei width, GLsizei height,
452 const struct gl_pixelstore_attrib *unpack,
453 const GLubyte *bitmap);
454
455 extern void
456 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
457 struct gl_texture_object *texObj);
458
459 extern void
460 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
461 struct gl_texture_image *texImage,
462 GLint xoffset, GLint yoffset, GLint slice,
463 struct gl_renderbuffer *rb,
464 GLint x, GLint y,
465 GLsizei width, GLsizei height);
466
467 extern void
468 _mesa_meta_GetTexImage(struct gl_context *ctx,
469 GLenum format, GLenum type, GLvoid *pixels,
470 struct gl_texture_image *texImage);
471
472 extern void
473 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
474 GLfloat width, GLfloat height);
475
476 /* meta-internal functions */
477 GLuint
478 _mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum target,
479 const GLcharARB *source);
480
481
482 GLuint
483 _mesa_meta_link_program_with_debug(struct gl_context *ctx, GLuint program);
484
485 void
486 _mesa_meta_compile_and_link_program(struct gl_context *ctx,
487 const char *vs_source,
488 const char *fs_source,
489 const char *name,
490 GLuint *program);
491
492 GLboolean
493 _mesa_meta_alloc_texture(struct temp_texture *tex,
494 GLsizei width, GLsizei height, GLenum intFormat);
495
496 void
497 _mesa_meta_setup_texture_coords(GLenum faceTarget,
498 GLint slice,
499 GLint width,
500 GLint height,
501 GLint depth,
502 GLfloat coords0[4],
503 GLfloat coords1[4],
504 GLfloat coords2[4],
505 GLfloat coords3[4]);
506
507 struct temp_texture *
508 _mesa_meta_get_temp_texture(struct gl_context *ctx);
509
510 struct temp_texture *
511 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx);
512
513 void
514 _mesa_meta_setup_vertex_objects(GLuint *VAO, GLuint *VBO,
515 bool use_generic_attributes,
516 unsigned vertex_size, unsigned texcoord_size,
517 unsigned color_size);
518
519 void
520 _mesa_meta_setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO,
521 unsigned texcoord_size);
522
523 void
524 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
525 struct temp_texture *tex,
526 GLboolean newTex,
527 GLsizei width, GLsizei height,
528 GLenum format, GLenum type,
529 const GLvoid *pixels);
530
531 void
532 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
533 struct temp_texture *tex,
534 GLint srcX, GLint srcY,
535 GLsizei width, GLsizei height,
536 GLenum intFormat,
537 GLenum filter);
538
539 void
540 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
541 GLenum target,
542 struct blit_shader_table *table);
543
544 void
545 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit);
546
547 void
548 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table *table);
549
550 void
551 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap);
552
553 void
554 _mesa_meta_bind_fbo_image(GLenum attachment,
555 struct gl_texture_image *texImage, GLuint layer);
556
557 #endif /* META_H */