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