meta: Fix saving the program pipeline state
[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 struct gl_pipeline_object *Pipeline;
125
126 /** MESA_META_STENCIL_TEST */
127 struct gl_stencil_attrib Stencil;
128
129 /** MESA_META_TRANSFORM */
130 GLenum MatrixMode;
131 GLfloat ModelviewMatrix[16];
132 GLfloat ProjectionMatrix[16];
133 GLfloat TextureMatrix[16];
134
135 /** MESA_META_CLIP */
136 GLbitfield ClipPlanesEnabled;
137
138 /** MESA_META_TEXTURE */
139 GLuint ActiveUnit;
140 GLuint ClientActiveUnit;
141 /** for unit[0] only */
142 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
143 /** mask of TEXTURE_2D_BIT, etc */
144 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
145 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
146 GLuint EnvMode; /* unit[0] only */
147
148 /** MESA_META_VERTEX */
149 struct gl_vertex_array_object *VAO;
150 struct gl_buffer_object *ArrayBufferObj;
151
152 /** MESA_META_VIEWPORT */
153 GLfloat ViewportX, ViewportY, ViewportW, ViewportH;
154 GLclampd DepthNear, DepthFar;
155
156 /** MESA_META_CLAMP_FRAGMENT_COLOR */
157 GLenum ClampFragmentColor;
158
159 /** MESA_META_CLAMP_VERTEX_COLOR */
160 GLenum ClampVertexColor;
161
162 /** MESA_META_CONDITIONAL_RENDER */
163 struct gl_query_object *CondRenderQuery;
164 GLenum CondRenderMode;
165
166 /** MESA_META_SELECT_FEEDBACK */
167 GLenum RenderMode;
168 struct gl_selection Select;
169 struct gl_feedback Feedback;
170
171 /** MESA_META_MULTISAMPLE */
172 struct gl_multisample_attrib Multisample;
173
174 /** MESA_META_FRAMEBUFFER_SRGB */
175 GLboolean sRGBEnabled;
176
177 /** Miscellaneous (always disabled) */
178 GLboolean Lighting;
179 GLboolean RasterDiscard;
180 GLboolean TransformFeedbackNeedsResume;
181
182 GLuint DrawBufferName, ReadBufferName, RenderbufferName;
183 };
184
185 /**
186 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
187 * This is currently shared by all the meta ops. But we could create a
188 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
189 */
190 struct temp_texture
191 {
192 GLuint TexObj;
193 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
194 GLsizei MinSize; /**< Min texture size to allocate */
195 GLsizei MaxSize; /**< Max possible texture size */
196 GLboolean NPOT; /**< Non-power of two size OK? */
197 GLsizei Width, Height; /**< Current texture size */
198 GLenum IntFormat;
199 GLfloat Sright, Ttop; /**< right, top texcoords */
200 };
201
202 /**
203 * State for GLSL texture sampler which is used to generate fragment
204 * shader in _mesa_meta_generate_mipmap().
205 */
206 struct blit_shader {
207 const char *type;
208 const char *func;
209 const char *texcoords;
210 GLuint shader_prog;
211 };
212
213 /**
214 * Table of all sampler types and shaders for accessing them.
215 */
216 struct blit_shader_table {
217 struct blit_shader sampler_1d;
218 struct blit_shader sampler_2d;
219 struct blit_shader sampler_3d;
220 struct blit_shader sampler_rect;
221 struct blit_shader sampler_cubemap;
222 struct blit_shader sampler_1d_array;
223 struct blit_shader sampler_2d_array;
224 struct blit_shader sampler_cubemap_array;
225 };
226
227 /**
228 * Indices in the blit_state->msaa_shaders[] array
229 *
230 * Note that setup_glsl_msaa_blit_shader() assumes that the _INT enums are one
231 * more than the non-_INT version and _UINT is one beyond that.
232 */
233 enum blit_msaa_shader {
234 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE,
235 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT,
236 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT,
237 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY,
238 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_INT,
239 BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_UINT,
240 BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE,
241 BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY,
242 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE,
243 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE_INT,
244 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE_UINT,
245 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_COPY,
246 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_COPY_INT,
247 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_COPY_UINT,
248 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE,
249 BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY,
250 BLIT_MSAA_SHADER_COUNT,
251 };
252
253 /**
254 * State for glBlitFramebufer()
255 */
256 struct blit_state
257 {
258 GLuint VAO;
259 GLuint VBO;
260 struct blit_shader_table shaders;
261 GLuint msaa_shaders[BLIT_MSAA_SHADER_COUNT];
262 struct temp_texture depthTex;
263 bool no_ctsi_fallback;
264 };
265
266
267 /**
268 * State for glClear()
269 */
270 struct clear_state
271 {
272 GLuint VAO;
273 GLuint VBO;
274 GLuint ShaderProg;
275 GLint ColorLocation;
276 GLint LayerLocation;
277
278 GLuint IntegerShaderProg;
279 GLint IntegerColorLocation;
280 GLint IntegerLayerLocation;
281 };
282
283
284 /**
285 * State for glCopyPixels()
286 */
287 struct copypix_state
288 {
289 GLuint VAO;
290 GLuint VBO;
291 };
292
293
294 /**
295 * State for glDrawPixels()
296 */
297 struct drawpix_state
298 {
299 GLuint VAO;
300 GLuint VBO;
301
302 GLuint StencilFP; /**< Fragment program for drawing stencil images */
303 GLuint DepthFP; /**< Fragment program for drawing depth images */
304 };
305
306
307 /**
308 * State for glBitmap()
309 */
310 struct bitmap_state
311 {
312 GLuint VAO;
313 GLuint VBO;
314 struct temp_texture Tex; /**< separate texture from other meta ops */
315 };
316
317 /**
318 * State for _mesa_meta_generate_mipmap()
319 */
320 struct gen_mipmap_state
321 {
322 GLuint VAO;
323 GLuint VBO;
324 GLuint FBO;
325 GLuint Sampler;
326
327 struct blit_shader_table shaders;
328 };
329
330 /**
331 * State for texture decompression
332 */
333 struct decompress_state
334 {
335 GLuint VAO;
336 GLuint VBO, FBO, RBO, Sampler;
337 GLint Width, Height;
338
339 struct blit_shader_table shaders;
340 };
341
342 /**
343 * State for glDrawTex()
344 */
345 struct drawtex_state
346 {
347 GLuint VAO;
348 GLuint VBO;
349 };
350
351 #define MAX_META_OPS_DEPTH 8
352 /**
353 * All per-context meta state.
354 */
355 struct gl_meta_state
356 {
357 /** Stack of state saved during meta-ops */
358 struct save_state Save[MAX_META_OPS_DEPTH];
359 /** Save stack depth */
360 GLuint SaveStackDepth;
361
362 struct temp_texture TempTex;
363
364 struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
365 struct clear_state Clear; /**< For _mesa_meta_Clear() */
366 struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
367 struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
368 struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
369 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
370 struct decompress_state Decompress; /**< For texture decompression */
371 struct drawtex_state DrawTex; /**< For _mesa_meta_DrawTex() */
372 };
373
374 struct vertex {
375 GLfloat x, y, z, tex[4];
376 GLfloat r, g, b, a;
377 };
378
379 extern void
380 _mesa_meta_init(struct gl_context *ctx);
381
382 extern void
383 _mesa_meta_free(struct gl_context *ctx);
384
385 extern void
386 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state);
387
388 extern void
389 _mesa_meta_end(struct gl_context *ctx);
390
391 extern GLboolean
392 _mesa_meta_in_progress(struct gl_context *ctx);
393
394 extern void
395 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
396 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
397 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
398 GLbitfield mask, GLenum filter);
399
400 extern void
401 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers);
402
403 extern void
404 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers);
405
406 extern void
407 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
408 GLsizei width, GLsizei height,
409 GLint dstx, GLint dsty, GLenum type);
410
411 extern void
412 _mesa_meta_DrawPixels(struct gl_context *ctx,
413 GLint x, GLint y, GLsizei width, GLsizei height,
414 GLenum format, GLenum type,
415 const struct gl_pixelstore_attrib *unpack,
416 const GLvoid *pixels);
417
418 extern void
419 _mesa_meta_Bitmap(struct gl_context *ctx,
420 GLint x, GLint y, GLsizei width, GLsizei height,
421 const struct gl_pixelstore_attrib *unpack,
422 const GLubyte *bitmap);
423
424 extern void
425 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
426 struct gl_texture_object *texObj);
427
428 extern void
429 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
430 struct gl_texture_image *texImage,
431 GLint xoffset, GLint yoffset, GLint slice,
432 struct gl_renderbuffer *rb,
433 GLint x, GLint y,
434 GLsizei width, GLsizei height);
435
436 extern void
437 _mesa_meta_GetTexImage(struct gl_context *ctx,
438 GLenum format, GLenum type, GLvoid *pixels,
439 struct gl_texture_image *texImage);
440
441 extern void
442 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
443 GLfloat width, GLfloat height);
444
445 /* meta-internal functions */
446 GLuint
447 _mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum target,
448 const GLcharARB *source);
449
450
451 GLuint
452 _mesa_meta_link_program_with_debug(struct gl_context *ctx, GLuint program);
453
454 GLboolean
455 _mesa_meta_alloc_texture(struct temp_texture *tex,
456 GLsizei width, GLsizei height, GLenum intFormat);
457
458 void
459 _mesa_meta_setup_texture_coords(GLenum faceTarget,
460 GLint slice,
461 GLint width,
462 GLint height,
463 GLint depth,
464 GLfloat coords0[4],
465 GLfloat coords1[4],
466 GLfloat coords2[4],
467 GLfloat coords3[4]);
468
469 struct temp_texture *
470 _mesa_meta_get_temp_texture(struct gl_context *ctx);
471
472 struct temp_texture *
473 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx);
474
475 void
476 _mesa_meta_setup_vertex_objects(GLuint *VAO, GLuint *VBO,
477 bool use_generic_attributes,
478 unsigned vertex_size, unsigned texcoord_size,
479 unsigned color_size);
480
481 void
482 _mesa_meta_setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO,
483 unsigned texcoord_size);
484
485 void
486 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
487 struct temp_texture *tex,
488 GLboolean newTex,
489 GLsizei width, GLsizei height,
490 GLenum format, GLenum type,
491 const GLvoid *pixels);
492
493 void
494 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
495 struct temp_texture *tex,
496 GLint srcX, GLint srcY,
497 GLsizei width, GLsizei height,
498 GLenum intFormat,
499 GLenum filter);
500
501 void
502 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
503 GLenum target,
504 struct blit_shader_table *table);
505
506 void
507 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit);
508
509 void
510 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table *table);
511
512 void
513 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap);
514
515 void
516 _mesa_meta_bind_fbo_image(GLenum attachment,
517 struct gl_texture_image *texImage, GLuint layer);
518
519 #endif /* META_H */