meta: Push into desktop GL mode when doing meta operations.
[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 GLboolean MultisampleEnabled;
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
182 /**
183 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
184 * This is currently shared by all the meta ops. But we could create a
185 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
186 */
187 struct temp_texture
188 {
189 GLuint TexObj;
190 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
191 GLsizei MinSize; /**< Min texture size to allocate */
192 GLsizei MaxSize; /**< Max possible texture size */
193 GLboolean NPOT; /**< Non-power of two size OK? */
194 GLsizei Width, Height; /**< Current texture size */
195 GLenum IntFormat;
196 GLfloat Sright, Ttop; /**< right, top texcoords */
197 };
198
199 /**
200 * State for GLSL texture sampler which is used to generate fragment
201 * shader in _mesa_meta_generate_mipmap().
202 */
203 struct blit_shader {
204 const char *type;
205 const char *func;
206 const char *texcoords;
207 GLuint shader_prog;
208 };
209
210 /**
211 * Table of all sampler types and shaders for accessing them.
212 */
213 struct blit_shader_table {
214 struct blit_shader sampler_1d;
215 struct blit_shader sampler_2d;
216 struct blit_shader sampler_3d;
217 struct blit_shader sampler_rect;
218 struct blit_shader sampler_cubemap;
219 struct blit_shader sampler_1d_array;
220 struct blit_shader sampler_2d_array;
221 struct blit_shader sampler_cubemap_array;
222 };
223
224 /**
225 * State for glBlitFramebufer()
226 */
227 struct blit_state
228 {
229 GLuint VAO;
230 GLuint VBO;
231 GLuint DepthFP;
232 struct blit_shader_table shaders;
233 struct temp_texture depthTex;
234 };
235
236
237 /**
238 * State for glClear()
239 */
240 struct clear_state
241 {
242 GLuint VAO;
243 GLuint VBO;
244 GLuint ShaderProg;
245 GLint ColorLocation;
246 GLint LayerLocation;
247
248 GLuint IntegerShaderProg;
249 GLint IntegerColorLocation;
250 GLint IntegerLayerLocation;
251 };
252
253
254 /**
255 * State for glCopyPixels()
256 */
257 struct copypix_state
258 {
259 GLuint VAO;
260 GLuint VBO;
261 };
262
263
264 /**
265 * State for glDrawPixels()
266 */
267 struct drawpix_state
268 {
269 GLuint VAO;
270 GLuint VBO;
271
272 GLuint StencilFP; /**< Fragment program for drawing stencil images */
273 GLuint DepthFP; /**< Fragment program for drawing depth images */
274 };
275
276
277 /**
278 * State for glBitmap()
279 */
280 struct bitmap_state
281 {
282 GLuint VAO;
283 GLuint VBO;
284 struct temp_texture Tex; /**< separate texture from other meta ops */
285 };
286
287 /**
288 * State for _mesa_meta_generate_mipmap()
289 */
290 struct gen_mipmap_state
291 {
292 GLuint VAO;
293 GLuint VBO;
294 GLuint FBO;
295 GLuint Sampler;
296
297 struct blit_shader_table shaders;
298 };
299
300 /**
301 * State for texture decompression
302 */
303 struct decompress_state
304 {
305 GLuint VAO;
306 GLuint VBO, FBO, RBO, Sampler;
307 GLint Width, Height;
308
309 struct blit_shader_table shaders;
310 };
311
312 /**
313 * State for glDrawTex()
314 */
315 struct drawtex_state
316 {
317 GLuint VAO;
318 GLuint VBO;
319 };
320
321 #define MAX_META_OPS_DEPTH 8
322 /**
323 * All per-context meta state.
324 */
325 struct gl_meta_state
326 {
327 /** Stack of state saved during meta-ops */
328 struct save_state Save[MAX_META_OPS_DEPTH];
329 /** Save stack depth */
330 GLuint SaveStackDepth;
331
332 struct temp_texture TempTex;
333
334 struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
335 struct clear_state Clear; /**< For _mesa_meta_Clear() */
336 struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
337 struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
338 struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
339 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
340 struct decompress_state Decompress; /**< For texture decompression */
341 struct drawtex_state DrawTex; /**< For _mesa_meta_DrawTex() */
342 };
343
344 struct vertex {
345 GLfloat x, y, z, tex[4];
346 GLfloat r, g, b, a;
347 };
348
349 extern void
350 _mesa_meta_init(struct gl_context *ctx);
351
352 extern void
353 _mesa_meta_free(struct gl_context *ctx);
354
355 extern void
356 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state);
357
358 extern void
359 _mesa_meta_end(struct gl_context *ctx);
360
361 extern GLboolean
362 _mesa_meta_in_progress(struct gl_context *ctx);
363
364 extern void
365 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
366 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
367 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
368 GLbitfield mask, GLenum filter);
369
370 extern void
371 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers);
372
373 extern void
374 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers);
375
376 extern void
377 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
378 GLsizei width, GLsizei height,
379 GLint dstx, GLint dsty, GLenum type);
380
381 extern void
382 _mesa_meta_DrawPixels(struct gl_context *ctx,
383 GLint x, GLint y, GLsizei width, GLsizei height,
384 GLenum format, GLenum type,
385 const struct gl_pixelstore_attrib *unpack,
386 const GLvoid *pixels);
387
388 extern void
389 _mesa_meta_Bitmap(struct gl_context *ctx,
390 GLint x, GLint y, GLsizei width, GLsizei height,
391 const struct gl_pixelstore_attrib *unpack,
392 const GLubyte *bitmap);
393
394 extern GLboolean
395 _mesa_meta_check_generate_mipmap_fallback(struct gl_context *ctx, GLenum target,
396 struct gl_texture_object *texObj);
397
398 extern void
399 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
400 struct gl_texture_object *texObj);
401
402 extern void
403 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
404 struct gl_texture_image *texImage,
405 GLint xoffset, GLint yoffset, GLint slice,
406 struct gl_renderbuffer *rb,
407 GLint x, GLint y,
408 GLsizei width, GLsizei height);
409
410 extern void
411 _mesa_meta_GetTexImage(struct gl_context *ctx,
412 GLenum format, GLenum type, GLvoid *pixels,
413 struct gl_texture_image *texImage);
414
415 extern void
416 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
417 GLfloat width, GLfloat height);
418
419 /* meta-internal functions */
420 GLuint
421 _mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum target,
422 const GLcharARB *source);
423
424
425 GLuint
426 _mesa_meta_link_program_with_debug(struct gl_context *ctx, GLuint program);
427
428 GLboolean
429 _mesa_meta_alloc_texture(struct temp_texture *tex,
430 GLsizei width, GLsizei height, GLenum intFormat);
431
432 struct temp_texture *
433 _mesa_meta_get_temp_texture(struct gl_context *ctx);
434
435 struct temp_texture *
436 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx);
437
438 void
439 _mesa_meta_setup_vertex_objects(GLuint *VAO, GLuint *VBO,
440 bool use_generic_attributes,
441 unsigned vertex_size, unsigned texcoord_size,
442 unsigned color_size);
443
444 void
445 _mesa_meta_setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO,
446 unsigned texcoord_size);
447
448 void
449 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
450 struct temp_texture *tex,
451 GLboolean newTex,
452 GLsizei width, GLsizei height,
453 GLenum format, GLenum type,
454 const GLvoid *pixels);
455
456 void
457 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
458 struct temp_texture *tex,
459 GLint srcX, GLint srcY,
460 GLsizei width, GLsizei height,
461 GLenum intFormat,
462 GLenum filter);
463
464 void
465 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
466 GLenum target,
467 struct blit_shader_table *table);
468
469 void
470 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit);
471
472 void
473 _mesa_meta_blit_shader_table_cleanup(struct blit_shader_table *table);
474
475 #endif /* META_H */