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 #define MESA_META_DITHER              0x1000000
63 /**\}*/
64 
65 /**
66  * State which we may save/restore across meta ops.
67  * XXX this may be incomplete...
68  */
69 struct save_state
70 {
71    GLbitfield SavedState;  /**< bitmask of MESA_META_* flags */
72 
73    /* Always saved/restored with meta. */
74    gl_api API;
75    uint8_t ExtensionsVersion;
76 
77    /** MESA_META_CLEAR (and others?) */
78    struct gl_query_object *CurrentOcclusionObject;
79 
80    /** MESA_META_ALPHA_TEST */
81    GLboolean AlphaEnabled;
82    GLenum AlphaFunc;
83    GLclampf AlphaRef;
84 
85    /** MESA_META_BLEND */
86    GLbitfield BlendEnabled;
87    GLboolean ColorLogicOpEnabled;
88 
89    /** MESA_META_DITHER */
90    GLboolean DitherFlag;
91 
92    /** MESA_META_COLOR_MASK */
93    GLbitfield ColorMask;
94 
95    /** MESA_META_DEPTH_TEST */
96    struct gl_depthbuffer_attrib Depth;
97 
98    /** MESA_META_FOG */
99    GLboolean Fog;
100 
101    /** MESA_META_PIXEL_STORE */
102    struct gl_pixelstore_attrib Pack, Unpack;
103 
104    /** MESA_META_PIXEL_TRANSFER */
105    GLfloat RedBias, RedScale;
106    GLfloat GreenBias, GreenScale;
107    GLfloat BlueBias, BlueScale;
108    GLfloat AlphaBias, AlphaScale;
109    GLfloat DepthBias, DepthScale;
110    GLboolean MapColorFlag;
111 
112    /** MESA_META_RASTERIZATION */
113    GLenum FrontPolygonMode, BackPolygonMode;
114    GLboolean PolygonOffset;
115    GLboolean PolygonSmooth;
116    GLboolean PolygonStipple;
117    GLboolean PolygonCull;
118 
119    /** MESA_META_SCISSOR */
120    struct gl_scissor_attrib Scissor;
121 
122    /** MESA_META_SHADER */
123    GLboolean VertexProgramEnabled;
124    struct gl_program *VertexProgram;
125    GLboolean FragmentProgramEnabled;
126    struct gl_program *FragmentProgram;
127    GLboolean ATIFragmentShaderEnabled;
128    struct gl_program *Program[MESA_SHADER_STAGES];
129    struct gl_shader_program *ActiveShader;
130    struct gl_pipeline_object   *Pipeline;
131 
132    /** MESA_META_STENCIL_TEST */
133    struct gl_stencil_attrib Stencil;
134 
135    /** MESA_META_TRANSFORM */
136    GLfloat ModelviewMatrix[16];
137    GLfloat ProjectionMatrix[16];
138    GLfloat TextureMatrix[16];
139    /** GL_ARB_clip_control */
140    GLenum ClipOrigin;     /**< GL_LOWER_LEFT or GL_UPPER_LEFT */
141    GLenum ClipDepthMode;  /**< GL_NEGATIVE_ONE_TO_ONE or GL_ZERO_TO_ONE */
142 
143    /** MESA_META_CLIP */
144    GLbitfield ClipPlanesEnabled;
145 
146    /** MESA_META_TEXTURE */
147    GLuint ActiveUnit;
148    /** for unit[0] only */
149    struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
150    /** mask of TEXTURE_2D_BIT, etc */
151    GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
152    GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
153    GLuint EnvMode;  /* unit[0] only */
154 
155    /** MESA_META_VERTEX */
156    struct gl_vertex_array_object *VAO;
157 
158    /** MESA_META_VIEWPORT */
159    GLfloat ViewportX, ViewportY, ViewportW, ViewportH;
160    GLclampf DepthNear, DepthFar;
161 
162    /** MESA_META_CLAMP_FRAGMENT_COLOR */
163    GLenum ClampFragmentColor;
164 
165    /** MESA_META_CLAMP_VERTEX_COLOR */
166    GLenum ClampVertexColor;
167 
168    /** MESA_META_CONDITIONAL_RENDER */
169    struct gl_query_object *CondRenderQuery;
170    GLenum CondRenderMode;
171 
172    /** MESA_META_SELECT_FEEDBACK */
173    GLenum RenderMode;
174    struct gl_selection Select;
175    struct gl_feedback Feedback;
176 
177    /** MESA_META_MULTISAMPLE */
178    struct gl_multisample_attrib Multisample;
179 
180    /** MESA_META_FRAMEBUFFER_SRGB */
181    GLboolean sRGBEnabled;
182 
183    /** Miscellaneous (always disabled) */
184    GLboolean Lighting;
185    GLboolean RasterDiscard;
186    GLboolean TransformFeedbackNeedsResume;
187 
188    struct gl_framebuffer *DrawBuffer;
189    struct gl_framebuffer *ReadBuffer;
190 
191    /** MESA_META_DRAW_BUFFERS */
192    GLenum16 ColorDrawBuffers[MAX_DRAW_BUFFERS];
193 };
194 
195 /**
196  * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
197  * This is currently shared by all the meta ops.  But we could create a
198  * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
199  */
200 struct temp_texture
201 {
202    struct gl_texture_object *tex_obj;
203    GLenum Target;         /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
204    GLsizei MinSize;       /**< Min texture size to allocate */
205    GLsizei MaxSize;       /**< Max possible texture size */
206    GLboolean NPOT;        /**< Non-power of two size OK? */
207    GLsizei Width, Height; /**< Current texture size */
208    GLenum IntFormat;
209    GLfloat Sright, Ttop;  /**< right, top texcoords */
210 };
211 
212 /**
213  * State for GLSL texture sampler which is used to generate fragment
214  * shader in _mesa_meta_generate_mipmap().
215  */
216 struct blit_shader {
217    const char *type;
218    const char *func;
219    const char *texcoords;
220    struct gl_shader_program *shader_prog;
221 };
222 
223 /**
224  * Table of all sampler types and shaders for accessing them.
225  */
226 struct blit_shader_table {
227    struct blit_shader sampler_1d;
228    struct blit_shader sampler_2d;
229    struct blit_shader sampler_3d;
230    struct blit_shader sampler_rect;
231    struct blit_shader sampler_cubemap;
232    struct blit_shader sampler_1d_array;
233    struct blit_shader sampler_2d_array;
234    struct blit_shader sampler_cubemap_array;
235 };
236 
237 /**
238  * State for glBlitFramebufer()
239  */
240 struct blit_state
241 {
242    GLuint VAO;
243    struct gl_buffer_object *buf_obj;
244    struct blit_shader_table shaders_with_depth;
245    struct blit_shader_table shaders_without_depth;
246    struct temp_texture depthTex;
247    bool no_ctsi_fallback;
248 };
249 
250 struct fb_tex_blit_state
251 {
252    GLint baseLevelSave, maxLevelSave;
253    struct gl_sampler_object *samp_obj;
254    struct gl_sampler_object *samp_obj_save;
255    struct gl_texture_object *tex_obj;
256    struct gl_texture_object *temp_tex_obj;
257    GLuint stencilSamplingSave;
258 };
259 
260 
261 /**
262  * State for glClear()
263  */
264 struct clear_state
265 {
266    GLuint VAO;
267    struct gl_buffer_object *buf_obj;
268    struct gl_shader_program *ShaderProg;
269 };
270 
271 
272 /**
273  * State for glCopyPixels()
274  */
275 struct copypix_state
276 {
277    GLuint VAO;
278    struct gl_buffer_object *buf_obj;
279 };
280 
281 
282 /**
283  * State for glDrawPixels()
284  */
285 struct drawpix_state
286 {
287    GLuint VAO;
288    struct gl_buffer_object *buf_obj;
289 
290    GLuint StencilFP;  /**< Fragment program for drawing stencil images */
291    GLuint DepthFP;  /**< Fragment program for drawing depth images */
292 };
293 
294 
295 /**
296  * State for glBitmap()
297  */
298 struct bitmap_state
299 {
300    GLuint VAO;
301    struct gl_buffer_object *buf_obj;
302    struct temp_texture Tex;  /**< separate texture from other meta ops */
303 };
304 
305 /**
306  * State for _mesa_meta_generate_mipmap()
307  */
308 struct gen_mipmap_state
309 {
310    GLuint VAO;
311    struct gl_buffer_object *buf_obj;
312    struct gl_framebuffer *fb;
313    struct gl_sampler_object *samp_obj;
314 
315    struct blit_shader_table shaders;
316 };
317 
318 /**
319  * One of the FBO states for decompress_state. There will be one for each
320  * required renderbuffer format.
321  */
322 struct decompress_fbo_state
323 {
324    struct gl_renderbuffer *rb;
325    struct gl_framebuffer *fb;
326    GLint Width, Height;
327 };
328 
329 /**
330  * State for texture decompression
331  */
332 struct decompress_state
333 {
334    GLuint VAO;
335    struct decompress_fbo_state byteFBO, floatFBO;
336    struct gl_buffer_object *buf_obj;
337    struct gl_sampler_object *samp_obj;
338 
339    struct blit_shader_table shaders;
340 };
341 
342 /**
343  * State for glDrawTex()
344  */
345 struct drawtex_state
346 {
347    GLuint VAO;
348    struct gl_buffer_object *buf_obj;
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 static inline bool
_mesa_meta_in_progress(struct gl_context * ctx)392 _mesa_meta_in_progress(struct gl_context *ctx)
393 {
394    return ctx->Meta->SaveStackDepth != 0;
395 }
396 
397 extern void
398 _mesa_meta_fb_tex_blit_begin(struct gl_context *ctx,
399                              struct fb_tex_blit_state *blit);
400 
401 extern void
402 _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
403                            struct fb_tex_blit_state *blit);
404 
405 extern GLbitfield
406 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
407                            const struct gl_framebuffer *readFb,
408                            const struct gl_framebuffer *drawFb,
409                            GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
410                            GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
411                            GLbitfield mask, GLenum filter);
412 
413 extern void
414 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
415                                       struct gl_framebuffer *readFb,
416                                       struct gl_framebuffer *drawFb,
417                                       GLint srcX0, GLint srcY0,
418                                       GLint srcX1, GLint srcY1,
419                                       GLint dstX0, GLint dstY0,
420                                       GLint dstX1, GLint dstY1,
421                                       GLbitfield mask, GLenum filter);
422 
423 extern void
424 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers);
425 
426 extern void
427 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers);
428 
429 extern void
430 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
431                       GLsizei width, GLsizei height,
432                       GLint dstx, GLint dsty, GLenum type);
433 
434 extern void
435 _mesa_meta_DrawPixels(struct gl_context *ctx,
436                       GLint x, GLint y, GLsizei width, GLsizei height,
437                       GLenum format, GLenum type,
438                       const struct gl_pixelstore_attrib *unpack,
439                       const GLvoid *pixels);
440 
441 extern void
442 _mesa_meta_Bitmap(struct gl_context *ctx,
443                   GLint x, GLint y, GLsizei width, GLsizei height,
444                   const struct gl_pixelstore_attrib *unpack,
445                   const GLubyte *bitmap);
446 
447 extern void
448 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
449                           struct gl_texture_object *texObj);
450 
451 extern void
452 _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
453                            struct gl_texture_image *texImage,
454                            GLint xoffset, GLint yoffset, GLint slice,
455                            struct gl_renderbuffer *rb,
456                            GLint x, GLint y,
457                            GLsizei width, GLsizei height);
458 
459 extern void
460 _mesa_meta_ClearTexSubImage(struct gl_context *ctx,
461                             struct gl_texture_image *texImage,
462                             GLint xoffset, GLint yoffset, GLint zoffset,
463                             GLsizei width, GLsizei height, GLsizei depth,
464                             const GLvoid *clearValue);
465 
466 extern void
467 _mesa_meta_GetTexSubImage(struct gl_context *ctx,
468                           GLint xoffset, GLint yoffset, GLint zoffset,
469                           GLsizei width, GLsizei height, GLsizei depth,
470                           GLenum format, GLenum type, GLvoid *pixels,
471                           struct gl_texture_image *texImage);
472 
473 extern void
474 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
475                    GLfloat width, GLfloat height);
476 
477 /* meta-internal functions */
478 void
479 _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits);
480 
481 void
482 _mesa_meta_link_program_with_debug(struct gl_context *ctx,
483                                    struct gl_shader_program *sh_prog);
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                                     struct gl_shader_program **sh_prog_ptr);
491 
492 extern void
493 _mesa_meta_use_program(struct gl_context *ctx,
494                        struct gl_shader_program *sh_prog);
495 
496 GLboolean
497 _mesa_meta_alloc_texture(struct temp_texture *tex,
498                          GLsizei width, GLsizei height, GLenum intFormat);
499 
500 void
501 _mesa_meta_setup_texture_coords(GLenum faceTarget,
502                                 GLint slice,
503                                 GLint xoffset,
504                                 GLint yoffset,
505                                 GLint width,
506                                 GLint height,
507                                 GLint total_width,
508                                 GLint total_height,
509                                 GLint total_depth,
510                                 GLfloat coords0[4],
511                                 GLfloat coords1[4],
512                                 GLfloat coords2[4],
513                                 GLfloat coords3[4]);
514 
515 struct temp_texture *
516 _mesa_meta_get_temp_texture(struct gl_context *ctx);
517 
518 struct temp_texture *
519 _mesa_meta_get_temp_depth_texture(struct gl_context *ctx);
520 
521 void
522 _mesa_meta_setup_vertex_objects(struct gl_context *ctx,
523                                 GLuint *VAO, struct gl_buffer_object **buf_obj,
524                                 bool use_generic_attributes,
525                                 unsigned vertex_size, unsigned texcoord_size,
526                                 unsigned color_size);
527 
528 void
529 _mesa_meta_setup_ff_tnl_for_blit(struct gl_context *ctx,
530                                  GLuint *VAO, struct gl_buffer_object **buf_obj,
531                                  unsigned texcoord_size);
532 
533 void
534 _mesa_meta_setup_drawpix_texture(struct gl_context *ctx,
535                                  struct temp_texture *tex,
536                                  GLboolean newTex,
537                                  GLsizei width, GLsizei height,
538                                  GLenum format, GLenum type,
539                                  const GLvoid *pixels);
540 
541 void
542 _mesa_meta_setup_copypix_texture(struct gl_context *ctx,
543                                  struct temp_texture *tex,
544                                  GLint srcX, GLint srcY,
545                                  GLsizei width, GLsizei height,
546                                  GLenum intFormat,
547                                  GLenum filter);
548 
549 void
550 _mesa_meta_setup_blit_shader(struct gl_context *ctx,
551                              GLenum target,
552                              bool do_depth,
553                              struct blit_shader_table *table);
554 
555 void
556 _mesa_meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit);
557 
558 void
559 _mesa_meta_blit_shader_table_cleanup(struct gl_context *ctx,
560                                      struct blit_shader_table *table);
561 
562 void
563 _mesa_meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
564                                         struct gen_mipmap_state *mipmap);
565 
566 void
567 _mesa_meta_framebuffer_texture_image(struct gl_context *ctx,
568                                      struct gl_framebuffer *fb,
569                                      GLenum attachment,
570                                      struct gl_texture_image *texImage,
571                                      GLuint layer);
572 
573 #endif /* META_H */
574