2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/context.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/image.h"
50 #include "main/macros.h"
51 #include "main/matrix.h"
52 #include "main/mipmap.h"
53 #include "main/pixel.h"
55 #include "main/polygon.h"
56 #include "main/readpix.h"
57 #include "main/scissor.h"
58 #include "main/shaderapi.h"
59 #include "main/shaderobj.h"
60 #include "main/state.h"
61 #include "main/stencil.h"
62 #include "main/texobj.h"
63 #include "main/texenv.h"
64 #include "main/texgetimage.h"
65 #include "main/teximage.h"
66 #include "main/texparam.h"
67 #include "main/texstate.h"
68 #include "main/uniforms.h"
69 #include "main/varray.h"
70 #include "main/viewport.h"
71 #include "program/program.h"
72 #include "swrast/swrast.h"
73 #include "drivers/common/meta.h"
76 /** Return offset in bytes of the field within a vertex struct */
77 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
80 * State which we may save/restore across meta ops.
81 * XXX this may be incomplete...
85 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
87 /** MESA_META_ALPHA_TEST */
88 GLboolean AlphaEnabled
;
92 /** MESA_META_BLEND */
93 GLbitfield BlendEnabled
;
94 GLboolean ColorLogicOpEnabled
;
96 /** MESA_META_COLOR_MASK */
97 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
99 /** MESA_META_DEPTH_TEST */
100 struct gl_depthbuffer_attrib Depth
;
105 /** MESA_META_PIXEL_STORE */
106 struct gl_pixelstore_attrib Pack
, Unpack
;
108 /** MESA_META_PIXEL_TRANSFER */
109 GLfloat RedBias
, RedScale
;
110 GLfloat GreenBias
, GreenScale
;
111 GLfloat BlueBias
, BlueScale
;
112 GLfloat AlphaBias
, AlphaScale
;
113 GLfloat DepthBias
, DepthScale
;
114 GLboolean MapColorFlag
;
116 /** MESA_META_RASTERIZATION */
117 GLenum FrontPolygonMode
, BackPolygonMode
;
118 GLboolean PolygonOffset
;
119 GLboolean PolygonSmooth
;
120 GLboolean PolygonStipple
;
121 GLboolean PolygonCull
;
123 /** MESA_META_SCISSOR */
124 struct gl_scissor_attrib Scissor
;
126 /** MESA_META_SHADER */
127 GLboolean VertexProgramEnabled
;
128 struct gl_vertex_program
*VertexProgram
;
129 GLboolean FragmentProgramEnabled
;
130 struct gl_fragment_program
*FragmentProgram
;
131 struct gl_shader_program
*VertexShader
;
132 struct gl_shader_program
*FragmentShader
;
133 struct gl_shader_program
*ActiveShader
;
135 /** MESA_META_STENCIL_TEST */
136 struct gl_stencil_attrib Stencil
;
138 /** MESA_META_TRANSFORM */
140 GLfloat ModelviewMatrix
[16];
141 GLfloat ProjectionMatrix
[16];
142 GLfloat TextureMatrix
[16];
144 /** MESA_META_CLIP */
145 GLbitfield ClipPlanesEnabled
;
147 /** MESA_META_TEXTURE */
149 GLuint ClientActiveUnit
;
150 /** for unit[0] only */
151 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
152 /** mask of TEXTURE_2D_BIT, etc */
153 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
154 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
155 GLuint EnvMode
; /* unit[0] only */
157 /** MESA_META_VERTEX */
158 struct gl_array_object
*ArrayObj
;
159 struct gl_buffer_object
*ArrayBufferObj
;
161 /** MESA_META_VIEWPORT */
162 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
163 GLclampd DepthNear
, DepthFar
;
165 /** MESA_META_CLAMP_FRAGMENT_COLOR */
166 GLenum ClampFragmentColor
;
168 /** MESA_META_CLAMP_VERTEX_COLOR */
169 GLenum ClampVertexColor
;
171 /** MESA_META_SELECT_FEEDBACK */
173 struct gl_selection Select
;
174 struct gl_feedback Feedback
;
176 /** Miscellaneous (always disabled) */
178 GLboolean RasterDiscard
;
182 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
183 * This is currently shared by all the meta ops. But we could create a
184 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
189 GLenum Target
; /**< GL_TEXTURE_2D */
190 GLsizei MinSize
; /**< Min texture size to allocate */
191 GLsizei MaxSize
; /**< Max possible texture size */
192 GLboolean NPOT
; /**< Non-power of two size OK? */
193 GLsizei Width
, Height
; /**< Current texture size */
195 GLfloat Sright
, Ttop
; /**< right, top texcoords */
200 * State for glBlitFramebufer()
211 * State for glClear()
220 GLuint IntegerShaderProg
;
221 GLint IntegerColorLocation
;
226 * State for glCopyPixels()
236 * State for glDrawPixels()
242 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
243 GLuint DepthFP
; /**< Fragment program for drawing depth images */
248 * State for glBitmap()
254 struct temp_texture Tex
; /**< separate texture from other meta ops */
259 * State for _mesa_meta_generate_mipmap()
261 struct gen_mipmap_state
270 * State for texture decompression
272 struct decompress_state
275 GLuint VBO
, FBO
, RBO
;
280 * State for glDrawTex()
288 #define MAX_META_OPS_DEPTH 8
290 * All per-context meta state.
294 /** Stack of state saved during meta-ops */
295 struct save_state Save
[MAX_META_OPS_DEPTH
];
296 /** Save stack depth */
297 GLuint SaveStackDepth
;
299 struct temp_texture TempTex
;
301 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
302 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
303 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
304 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
305 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
306 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
307 struct decompress_state Decompress
; /**< For texture decompression */
308 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
311 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
312 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
313 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
316 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
322 shader
= _mesa_CreateShaderObjectARB(target
);
323 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
324 _mesa_CompileShaderARB(shader
);
326 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
330 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
332 _mesa_DeleteObjectARB(shader
);
338 _mesa_DeleteObjectARB(shader
);
342 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
344 "meta program compile failed:\n%s\n"
349 _mesa_DeleteObjectARB(shader
);
355 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
360 _mesa_LinkProgramARB(program
);
362 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
366 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
374 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
375 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
383 * Initialize meta-ops for a context.
384 * To be called once during context creation.
387 _mesa_meta_init(struct gl_context
*ctx
)
391 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
396 * Free context meta-op state.
397 * To be called once during context destruction.
400 _mesa_meta_free(struct gl_context
*ctx
)
402 GET_CURRENT_CONTEXT(old_context
);
403 _mesa_make_current(ctx
, NULL
, NULL
);
404 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
405 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
406 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
408 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
410 _mesa_make_current(NULL
, NULL
, NULL
);
417 * Enter meta state. This is like a light-weight version of glPushAttrib
418 * but it also resets most GL state back to default values.
420 * \param state bitmask of MESA_META_* flags indicating which attribute groups
421 * to save and reset to their defaults
424 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
426 struct save_state
*save
;
428 /* hope MAX_META_OPS_DEPTH is large enough */
429 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
431 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
432 memset(save
, 0, sizeof(*save
));
433 save
->SavedState
= state
;
435 if (state
& MESA_META_ALPHA_TEST
) {
436 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
437 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
438 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
439 if (ctx
->Color
.AlphaEnabled
)
440 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
443 if (state
& MESA_META_BLEND
) {
444 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
445 if (ctx
->Color
.BlendEnabled
) {
446 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
448 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
449 if (ctx
->Color
.ColorLogicOpEnabled
)
450 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
453 if (state
& MESA_META_COLOR_MASK
) {
454 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
455 sizeof(ctx
->Color
.ColorMask
));
456 if (!ctx
->Color
.ColorMask
[0][0] ||
457 !ctx
->Color
.ColorMask
[0][1] ||
458 !ctx
->Color
.ColorMask
[0][2] ||
459 !ctx
->Color
.ColorMask
[0][3])
460 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
463 if (state
& MESA_META_DEPTH_TEST
) {
464 save
->Depth
= ctx
->Depth
; /* struct copy */
466 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
469 if (state
& MESA_META_FOG
) {
470 save
->Fog
= ctx
->Fog
.Enabled
;
471 if (ctx
->Fog
.Enabled
)
472 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
475 if (state
& MESA_META_PIXEL_STORE
) {
476 save
->Pack
= ctx
->Pack
;
477 save
->Unpack
= ctx
->Unpack
;
478 ctx
->Pack
= ctx
->DefaultPacking
;
479 ctx
->Unpack
= ctx
->DefaultPacking
;
482 if (state
& MESA_META_PIXEL_TRANSFER
) {
483 save
->RedScale
= ctx
->Pixel
.RedScale
;
484 save
->RedBias
= ctx
->Pixel
.RedBias
;
485 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
486 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
487 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
488 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
489 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
490 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
491 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
492 ctx
->Pixel
.RedScale
= 1.0F
;
493 ctx
->Pixel
.RedBias
= 0.0F
;
494 ctx
->Pixel
.GreenScale
= 1.0F
;
495 ctx
->Pixel
.GreenBias
= 0.0F
;
496 ctx
->Pixel
.BlueScale
= 1.0F
;
497 ctx
->Pixel
.BlueBias
= 0.0F
;
498 ctx
->Pixel
.AlphaScale
= 1.0F
;
499 ctx
->Pixel
.AlphaBias
= 0.0F
;
500 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
502 ctx
->NewState
|=_NEW_PIXEL
;
505 if (state
& MESA_META_RASTERIZATION
) {
506 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
507 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
508 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
509 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
510 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
511 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
512 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
513 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
514 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
515 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
516 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
519 if (state
& MESA_META_SCISSOR
) {
520 save
->Scissor
= ctx
->Scissor
; /* struct copy */
521 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
524 if (state
& MESA_META_SHADER
) {
525 if (ctx
->Extensions
.ARB_vertex_program
) {
526 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
527 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
528 ctx
->VertexProgram
.Current
);
529 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
532 if (ctx
->Extensions
.ARB_fragment_program
) {
533 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
534 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
535 ctx
->FragmentProgram
.Current
);
536 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
539 if (ctx
->Extensions
.ARB_shader_objects
) {
540 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
541 ctx
->Shader
.CurrentVertexProgram
);
542 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
543 ctx
->Shader
.CurrentFragmentProgram
);
544 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
545 ctx
->Shader
.ActiveProgram
);
547 _mesa_UseProgramObjectARB(0);
551 if (state
& MESA_META_STENCIL_TEST
) {
552 save
->Stencil
= ctx
->Stencil
; /* struct copy */
553 if (ctx
->Stencil
.Enabled
)
554 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
555 /* NOTE: other stencil state not reset */
558 if (state
& MESA_META_TEXTURE
) {
561 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
562 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
563 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
565 /* Disable all texture units */
566 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
567 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
568 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
569 if (ctx
->Texture
.Unit
[u
].Enabled
||
570 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
571 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
572 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
573 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
574 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
575 if (ctx
->Extensions
.ARB_texture_cube_map
)
576 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
577 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
578 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
579 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
580 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
584 /* save current texture objects for unit[0] only */
585 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
586 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
587 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
590 /* set defaults for unit[0] */
591 _mesa_ActiveTextureARB(GL_TEXTURE0
);
592 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
593 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
596 if (state
& MESA_META_TRANSFORM
) {
597 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
598 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
599 16 * sizeof(GLfloat
));
600 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
601 16 * sizeof(GLfloat
));
602 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
603 16 * sizeof(GLfloat
));
604 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
605 /* set 1:1 vertex:pixel coordinate transform */
606 _mesa_ActiveTextureARB(GL_TEXTURE0
);
607 _mesa_MatrixMode(GL_TEXTURE
);
608 _mesa_LoadIdentity();
609 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
610 _mesa_MatrixMode(GL_MODELVIEW
);
611 _mesa_LoadIdentity();
612 _mesa_MatrixMode(GL_PROJECTION
);
613 _mesa_LoadIdentity();
614 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
615 0.0, ctx
->DrawBuffer
->Height
,
619 if (state
& MESA_META_CLIP
) {
620 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
621 if (ctx
->Transform
.ClipPlanesEnabled
) {
623 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
624 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
629 if (state
& MESA_META_VERTEX
) {
630 /* save vertex array object state */
631 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
632 ctx
->Array
.ArrayObj
);
633 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
634 ctx
->Array
.ArrayBufferObj
);
635 /* set some default state? */
638 if (state
& MESA_META_VIEWPORT
) {
639 /* save viewport state */
640 save
->ViewportX
= ctx
->Viewport
.X
;
641 save
->ViewportY
= ctx
->Viewport
.Y
;
642 save
->ViewportW
= ctx
->Viewport
.Width
;
643 save
->ViewportH
= ctx
->Viewport
.Height
;
644 /* set viewport to match window size */
645 if (ctx
->Viewport
.X
!= 0 ||
646 ctx
->Viewport
.Y
!= 0 ||
647 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
648 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
649 _mesa_set_viewport(ctx
, 0, 0,
650 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
652 /* save depth range state */
653 save
->DepthNear
= ctx
->Viewport
.Near
;
654 save
->DepthFar
= ctx
->Viewport
.Far
;
655 /* set depth range to default */
656 _mesa_DepthRange(0.0, 1.0);
659 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
660 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
662 /* Generally in here we want to do clamping according to whether
663 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
664 * regardless of the internal implementation of the metaops.
666 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
667 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
670 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
671 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
673 /* Generally in here we never want vertex color clamping --
674 * result clamping is only dependent on fragment clamping.
676 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
679 if (state
& MESA_META_SELECT_FEEDBACK
) {
680 save
->RenderMode
= ctx
->RenderMode
;
681 if (ctx
->RenderMode
== GL_SELECT
) {
682 save
->Select
= ctx
->Select
; /* struct copy */
683 _mesa_RenderMode(GL_RENDER
);
684 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
685 save
->Feedback
= ctx
->Feedback
; /* struct copy */
686 _mesa_RenderMode(GL_RENDER
);
692 save
->Lighting
= ctx
->Light
.Enabled
;
693 if (ctx
->Light
.Enabled
)
694 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
695 save
->RasterDiscard
= ctx
->RasterDiscard
;
696 if (ctx
->RasterDiscard
)
697 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
703 * Leave meta state. This is like a light-weight version of glPopAttrib().
706 _mesa_meta_end(struct gl_context
*ctx
)
708 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
709 const GLbitfield state
= save
->SavedState
;
711 if (state
& MESA_META_ALPHA_TEST
) {
712 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
713 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
714 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
717 if (state
& MESA_META_BLEND
) {
718 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
719 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
721 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
722 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
725 if (state
& MESA_META_COLOR_MASK
) {
727 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
728 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
730 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
731 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
734 _mesa_ColorMaskIndexed(i
,
735 save
->ColorMask
[i
][0],
736 save
->ColorMask
[i
][1],
737 save
->ColorMask
[i
][2],
738 save
->ColorMask
[i
][3]);
744 if (state
& MESA_META_DEPTH_TEST
) {
745 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
746 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
747 _mesa_DepthFunc(save
->Depth
.Func
);
748 _mesa_DepthMask(save
->Depth
.Mask
);
751 if (state
& MESA_META_FOG
) {
752 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
755 if (state
& MESA_META_PIXEL_STORE
) {
756 ctx
->Pack
= save
->Pack
;
757 ctx
->Unpack
= save
->Unpack
;
760 if (state
& MESA_META_PIXEL_TRANSFER
) {
761 ctx
->Pixel
.RedScale
= save
->RedScale
;
762 ctx
->Pixel
.RedBias
= save
->RedBias
;
763 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
764 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
765 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
766 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
767 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
768 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
769 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
771 ctx
->NewState
|=_NEW_PIXEL
;
774 if (state
& MESA_META_RASTERIZATION
) {
775 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
776 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
777 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
778 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
779 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
780 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
783 if (state
& MESA_META_SCISSOR
) {
784 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
785 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
786 save
->Scissor
.Width
, save
->Scissor
.Height
);
789 if (state
& MESA_META_SHADER
) {
790 if (ctx
->Extensions
.ARB_vertex_program
) {
791 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
792 save
->VertexProgramEnabled
);
793 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
794 save
->VertexProgram
);
795 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
798 if (ctx
->Extensions
.ARB_fragment_program
) {
799 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
800 save
->FragmentProgramEnabled
);
801 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
802 save
->FragmentProgram
);
803 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
806 if (ctx
->Extensions
.ARB_vertex_shader
)
807 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
809 if (ctx
->Extensions
.ARB_fragment_shader
)
810 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
811 save
->FragmentShader
);
813 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
816 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
817 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
818 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
821 if (state
& MESA_META_STENCIL_TEST
) {
822 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
824 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
825 _mesa_ClearStencil(stencil
->Clear
);
826 if (ctx
->Extensions
.EXT_stencil_two_side
) {
827 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
828 stencil
->TestTwoSide
);
829 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
830 ? GL_BACK
: GL_FRONT
);
833 _mesa_StencilFuncSeparate(GL_FRONT
,
834 stencil
->Function
[0],
836 stencil
->ValueMask
[0]);
837 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
838 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
839 stencil
->ZFailFunc
[0],
840 stencil
->ZPassFunc
[0]);
842 _mesa_StencilFuncSeparate(GL_BACK
,
843 stencil
->Function
[1],
845 stencil
->ValueMask
[1]);
846 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
847 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
848 stencil
->ZFailFunc
[1],
849 stencil
->ZPassFunc
[1]);
852 if (state
& MESA_META_TEXTURE
) {
855 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
857 /* restore texenv for unit[0] */
858 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
860 /* restore texture objects for unit[0] only */
861 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
862 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
863 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
864 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
865 save
->CurrentTexture
[tgt
]);
867 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
870 /* Restore fixed function texture enables, texgen */
871 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
872 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
873 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
874 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
877 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
878 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
879 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
883 /* restore current unit state */
884 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
885 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
888 if (state
& MESA_META_TRANSFORM
) {
889 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
890 _mesa_ActiveTextureARB(GL_TEXTURE0
);
891 _mesa_MatrixMode(GL_TEXTURE
);
892 _mesa_LoadMatrixf(save
->TextureMatrix
);
893 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
895 _mesa_MatrixMode(GL_MODELVIEW
);
896 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
898 _mesa_MatrixMode(GL_PROJECTION
);
899 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
901 _mesa_MatrixMode(save
->MatrixMode
);
904 if (state
& MESA_META_CLIP
) {
905 if (save
->ClipPlanesEnabled
) {
907 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
908 if (save
->ClipPlanesEnabled
& (1 << i
)) {
909 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
915 if (state
& MESA_META_VERTEX
) {
916 /* restore vertex buffer object */
917 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
918 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
920 /* restore vertex array object */
921 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
922 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
925 if (state
& MESA_META_VIEWPORT
) {
926 if (save
->ViewportX
!= ctx
->Viewport
.X
||
927 save
->ViewportY
!= ctx
->Viewport
.Y
||
928 save
->ViewportW
!= ctx
->Viewport
.Width
||
929 save
->ViewportH
!= ctx
->Viewport
.Height
) {
930 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
931 save
->ViewportW
, save
->ViewportH
);
933 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
936 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
937 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
940 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
941 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
945 if (save
->Lighting
) {
946 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
948 if (save
->RasterDiscard
) {
949 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
955 * Determine whether Mesa is currently in a meta state.
958 _mesa_meta_in_progress(struct gl_context
*ctx
)
960 return ctx
->Meta
->SaveStackDepth
!= 0;
965 * Convert Z from a normalized value in the range [0, 1] to an object-space
966 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
967 * default/identity ortho projection results in the original Z value.
968 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
969 * value comes from the clear value or raster position.
971 static INLINE GLfloat
972 invert_z(GLfloat normZ
)
974 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
980 * One-time init for a temp_texture object.
981 * Choose tex target, compute max tex size, etc.
984 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
986 /* use 2D texture, NPOT if possible */
987 tex
->Target
= GL_TEXTURE_2D
;
988 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
989 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
990 tex
->MinSize
= 16; /* 16 x 16 at least */
991 assert(tex
->MaxSize
> 0);
993 _mesa_GenTextures(1, &tex
->TexObj
);
997 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1001 _mesa_DeleteTextures(1, &tex
->TexObj
);
1007 * Return pointer to temp_texture info for non-bitmap ops.
1008 * This does some one-time init if needed.
1010 static struct temp_texture
*
1011 get_temp_texture(struct gl_context
*ctx
)
1013 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1016 init_temp_texture(ctx
, tex
);
1024 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1025 * We use a separate texture for bitmaps to reduce texture
1026 * allocation/deallocation.
1028 static struct temp_texture
*
1029 get_bitmap_temp_texture(struct gl_context
*ctx
)
1031 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1034 init_temp_texture(ctx
, tex
);
1042 * Compute the width/height of texture needed to draw an image of the
1043 * given size. Return a flag indicating whether the current texture
1044 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1045 * allocated (glTexImage2D).
1046 * Also, compute s/t texcoords for drawing.
1048 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1051 alloc_texture(struct temp_texture
*tex
,
1052 GLsizei width
, GLsizei height
, GLenum intFormat
)
1054 GLboolean newTex
= GL_FALSE
;
1056 ASSERT(width
<= tex
->MaxSize
);
1057 ASSERT(height
<= tex
->MaxSize
);
1059 if (width
> tex
->Width
||
1060 height
> tex
->Height
||
1061 intFormat
!= tex
->IntFormat
) {
1062 /* alloc new texture (larger or different format) */
1065 /* use non-power of two size */
1066 tex
->Width
= MAX2(tex
->MinSize
, width
);
1067 tex
->Height
= MAX2(tex
->MinSize
, height
);
1070 /* find power of two size */
1072 w
= h
= tex
->MinSize
;
1081 tex
->IntFormat
= intFormat
;
1086 /* compute texcoords */
1087 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1088 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1095 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1098 setup_copypix_texture(struct temp_texture
*tex
,
1100 GLint srcX
, GLint srcY
,
1101 GLsizei width
, GLsizei height
, GLenum intFormat
,
1104 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1105 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1106 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1107 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1109 /* copy framebuffer image to texture */
1111 /* create new tex image */
1112 if (tex
->Width
== width
&& tex
->Height
== height
) {
1113 /* create new tex with framebuffer data */
1114 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1115 srcX
, srcY
, width
, height
, 0);
1118 /* create empty texture */
1119 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1120 tex
->Width
, tex
->Height
, 0,
1121 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1123 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1124 0, 0, srcX
, srcY
, width
, height
);
1128 /* replace existing tex image */
1129 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1130 0, 0, srcX
, srcY
, width
, height
);
1136 * Setup/load texture for glDrawPixels.
1139 setup_drawpix_texture(struct gl_context
*ctx
,
1140 struct temp_texture
*tex
,
1142 GLenum texIntFormat
,
1143 GLsizei width
, GLsizei height
,
1144 GLenum format
, GLenum type
,
1145 const GLvoid
*pixels
)
1147 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1148 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1149 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1150 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1152 /* copy pixel data to texture */
1154 /* create new tex image */
1155 if (tex
->Width
== width
&& tex
->Height
== height
) {
1156 /* create new tex and load image data */
1157 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1158 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1161 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1163 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1164 ctx
->Unpack
.BufferObj
);
1165 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1166 /* create empty texture */
1167 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1168 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1169 if (save_unpack_obj
!= NULL
)
1170 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1171 save_unpack_obj
->Name
);
1173 _mesa_TexSubImage2D(tex
->Target
, 0,
1174 0, 0, width
, height
, format
, type
, pixels
);
1178 /* replace existing tex image */
1179 _mesa_TexSubImage2D(tex
->Target
, 0,
1180 0, 0, width
, height
, format
, type
, pixels
);
1187 * One-time init for drawing depth pixels.
1190 init_blit_depth_pixels(struct gl_context
*ctx
)
1192 static const char *program
=
1194 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1197 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1198 const char *texTarget
;
1200 assert(blit
->DepthFP
== 0);
1202 /* replace %s with "RECT" or "2D" */
1203 assert(strlen(program
) + 4 < sizeof(program2
));
1205 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1207 _mesa_GenPrograms(1, &blit
->DepthFP
);
1208 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1209 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1210 strlen(program2
), (const GLubyte
*) program2
);
1215 * Try to do a glBlitFramebuffer using no-copy texturing.
1216 * We can do this when the src renderbuffer is actually a texture.
1217 * But if the src buffer == dst buffer we cannot do this.
1219 * \return new buffer mask indicating the buffers left to blit using the
1223 blitframebuffer_texture(struct gl_context
*ctx
,
1224 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1225 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1226 GLbitfield mask
, GLenum filter
)
1228 if (mask
& GL_COLOR_BUFFER_BIT
) {
1229 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1230 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1231 const struct gl_renderbuffer_attachment
*drawAtt
=
1232 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1233 const struct gl_renderbuffer_attachment
*readAtt
=
1234 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1236 if (readAtt
&& readAtt
->Texture
) {
1237 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1238 const GLuint srcLevel
= readAtt
->TextureLevel
;
1239 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1240 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1241 const GLint baseLevelSave
= texObj
->BaseLevel
;
1242 const GLint maxLevelSave
= texObj
->MaxLevel
;
1243 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1244 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1245 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1246 const GLenum target
= texObj
->Target
;
1248 if (drawAtt
->Texture
== readAtt
->Texture
) {
1249 /* Can't use same texture as both the source and dest. We need
1250 * to handle overlapping blits and besides, some hw may not
1256 if (target
!= GL_TEXTURE_2D
) {
1257 /* Can't handle other texture types at this time */
1262 printf("Blit from texture!\n");
1263 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1264 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1267 /* Prepare src texture state */
1268 _mesa_BindTexture(target
, texObj
->Name
);
1269 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1270 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1271 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1272 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1273 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1274 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1276 /* Always do our blits with no sRGB decode or encode.*/
1277 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1278 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1281 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1282 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1284 /* Prepare vertex data (the VBO was previously created and bound) */
1289 struct vertex verts
[4];
1290 GLfloat s0
, t0
, s1
, t1
;
1292 const struct gl_texture_image
*texImage
1293 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1294 s0
= srcX0
/ (float) texImage
->Width
;
1295 s1
= srcX1
/ (float) texImage
->Width
;
1296 t0
= srcY0
/ (float) texImage
->Height
;
1297 t1
= srcY1
/ (float) texImage
->Height
;
1299 verts
[0].x
= (GLfloat
) dstX0
;
1300 verts
[0].y
= (GLfloat
) dstY0
;
1301 verts
[1].x
= (GLfloat
) dstX1
;
1302 verts
[1].y
= (GLfloat
) dstY0
;
1303 verts
[2].x
= (GLfloat
) dstX1
;
1304 verts
[2].y
= (GLfloat
) dstY1
;
1305 verts
[3].x
= (GLfloat
) dstX0
;
1306 verts
[3].y
= (GLfloat
) dstY1
;
1317 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1320 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1322 /* Restore texture object state, the texture binding will
1323 * be restored by _mesa_meta_end().
1325 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1326 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1327 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1328 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1329 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1330 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1331 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1332 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1335 /* Done with color buffer */
1336 mask
&= ~GL_COLOR_BUFFER_BIT
;
1345 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1346 * of texture mapping and polygon rendering.
1349 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1350 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1351 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1352 GLbitfield mask
, GLenum filter
)
1354 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1355 struct temp_texture
*tex
= get_temp_texture(ctx
);
1356 const GLsizei maxTexSize
= tex
->MaxSize
;
1357 const GLint srcX
= MIN2(srcX0
, srcX1
);
1358 const GLint srcY
= MIN2(srcY0
, srcY1
);
1359 const GLint srcW
= abs(srcX1
- srcX0
);
1360 const GLint srcH
= abs(srcY1
- srcY0
);
1361 const GLboolean srcFlipX
= srcX1
< srcX0
;
1362 const GLboolean srcFlipY
= srcY1
< srcY0
;
1366 struct vertex verts
[4];
1369 /* In addition to falling back if the blit size is larger than the maximum
1370 * texture size, fallback if the source is multisampled. This fallback can
1371 * be removed once Mesa gets support ARB_texture_multisample.
1373 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1374 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1375 /* XXX avoid this fallback */
1376 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1377 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1393 /* only scissor effects blit so save/clear all other relevant state */
1394 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1396 if (blit
->ArrayObj
== 0) {
1397 /* one-time setup */
1399 /* create vertex array object */
1400 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1401 _mesa_BindVertexArray(blit
->ArrayObj
);
1403 /* create vertex array buffer */
1404 _mesa_GenBuffersARB(1, &blit
->VBO
);
1405 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1406 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1407 NULL
, GL_DYNAMIC_DRAW_ARB
);
1409 /* setup vertex arrays */
1410 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1411 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1412 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1413 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1416 _mesa_BindVertexArray(blit
->ArrayObj
);
1417 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1420 /* Try faster, direct texture approach first */
1421 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1422 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1424 _mesa_meta_end(ctx
);
1428 /* Continue with "normal" approach which involves copying the src rect
1429 * into a temporary texture and is "blitted" by drawing a textured quad.
1432 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1434 /* vertex positions/texcoords (after texture allocation!) */
1436 verts
[0].x
= (GLfloat
) dstX0
;
1437 verts
[0].y
= (GLfloat
) dstY0
;
1438 verts
[1].x
= (GLfloat
) dstX1
;
1439 verts
[1].y
= (GLfloat
) dstY0
;
1440 verts
[2].x
= (GLfloat
) dstX1
;
1441 verts
[2].y
= (GLfloat
) dstY1
;
1442 verts
[3].x
= (GLfloat
) dstX0
;
1443 verts
[3].y
= (GLfloat
) dstY1
;
1447 verts
[1].s
= tex
->Sright
;
1449 verts
[2].s
= tex
->Sright
;
1450 verts
[2].t
= tex
->Ttop
;
1452 verts
[3].t
= tex
->Ttop
;
1454 /* upload new vertex data */
1455 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1458 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1460 if (mask
& GL_COLOR_BUFFER_BIT
) {
1461 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1463 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1464 mask
&= ~GL_COLOR_BUFFER_BIT
;
1467 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1468 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1471 init_blit_depth_pixels(ctx
);
1473 /* maybe change tex format here */
1474 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1476 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1477 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1479 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1480 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1482 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1483 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1484 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1485 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1486 _mesa_DepthFunc(GL_ALWAYS
);
1487 _mesa_DepthMask(GL_TRUE
);
1489 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1490 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1496 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1497 /* XXX can't easily do stencil */
1500 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1502 _mesa_meta_end(ctx
);
1505 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1506 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1511 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1513 if (blit
->ArrayObj
) {
1514 _mesa_DeleteVertexArraysAPPLE(1, &blit
->ArrayObj
);
1516 _mesa_DeleteBuffersARB(1, &blit
->VBO
);
1519 if (blit
->DepthFP
) {
1520 _mesa_DeletePrograms(1, &blit
->DepthFP
);
1527 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1530 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1532 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1534 GLfloat x
, y
, z
, r
, g
, b
, a
;
1536 struct vertex verts
[4];
1537 /* save all state but scissor, pixel pack/unpack */
1538 GLbitfield metaSave
= (MESA_META_ALL
-
1540 MESA_META_PIXEL_STORE
);
1541 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1543 if (buffers
& BUFFER_BITS_COLOR
) {
1544 /* if clearing color buffers, don't save/restore colormask */
1545 metaSave
-= MESA_META_COLOR_MASK
;
1548 _mesa_meta_begin(ctx
, metaSave
);
1550 if (clear
->ArrayObj
== 0) {
1551 /* one-time setup */
1553 /* create vertex array object */
1554 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1555 _mesa_BindVertexArray(clear
->ArrayObj
);
1557 /* create vertex array buffer */
1558 _mesa_GenBuffersARB(1, &clear
->VBO
);
1559 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1561 /* setup vertex arrays */
1562 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1563 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1564 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1565 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1568 _mesa_BindVertexArray(clear
->ArrayObj
);
1569 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1572 /* GL_COLOR_BUFFER_BIT */
1573 if (buffers
& BUFFER_BITS_COLOR
) {
1574 /* leave colormask, glDrawBuffer state as-is */
1576 /* Clears never have the color clamped. */
1577 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1580 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1581 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1584 /* GL_DEPTH_BUFFER_BIT */
1585 if (buffers
& BUFFER_BIT_DEPTH
) {
1586 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1587 _mesa_DepthFunc(GL_ALWAYS
);
1588 _mesa_DepthMask(GL_TRUE
);
1591 assert(!ctx
->Depth
.Test
);
1594 /* GL_STENCIL_BUFFER_BIT */
1595 if (buffers
& BUFFER_BIT_STENCIL
) {
1596 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1597 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1598 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1599 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1600 ctx
->Stencil
.Clear
& stencilMax
,
1601 ctx
->Stencil
.WriteMask
[0]);
1604 assert(!ctx
->Stencil
.Enabled
);
1607 /* vertex positions/colors */
1609 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1610 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1611 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1612 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1613 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1630 for (i
= 0; i
< 4; i
++) {
1631 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1632 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1633 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1634 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1637 /* upload new vertex data */
1638 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1639 GL_DYNAMIC_DRAW_ARB
);
1643 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1645 _mesa_meta_end(ctx
);
1649 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1651 const char *vs_source
=
1652 "attribute vec4 position;\n"
1655 " gl_Position = position;\n"
1657 const char *fs_source
=
1658 "uniform vec4 color;\n"
1661 " gl_FragColor = color;\n"
1663 const char *vs_int_source
=
1665 "attribute vec4 position;\n"
1668 " gl_Position = position;\n"
1670 const char *fs_int_source
=
1672 "uniform ivec4 color;\n"
1673 "out ivec4 out_color;\n"
1677 " out_color = color;\n"
1681 if (clear
->ArrayObj
!= 0)
1684 /* create vertex array object */
1685 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1686 _mesa_BindVertexArray(clear
->ArrayObj
);
1688 /* create vertex array buffer */
1689 _mesa_GenBuffersARB(1, &clear
->VBO
);
1690 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1692 /* setup vertex arrays */
1693 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1694 _mesa_EnableVertexAttribArrayARB(0);
1696 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1697 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1698 _mesa_CompileShaderARB(vs
);
1700 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1701 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1702 _mesa_CompileShaderARB(fs
);
1704 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1705 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1706 _mesa_DeleteObjectARB(fs
);
1707 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1708 _mesa_DeleteObjectARB(vs
);
1709 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1710 _mesa_LinkProgramARB(clear
->ShaderProg
);
1712 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1715 if (ctx
->Const
.GLSLVersion
>= 130) {
1716 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1717 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1719 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1720 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1721 _mesa_DeleteObjectARB(fs
);
1722 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1723 _mesa_DeleteObjectARB(vs
);
1724 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1726 /* Note that user-defined out attributes get automatically assigned
1727 * locations starting from 0, so we don't need to explicitly
1728 * BindFragDataLocation to 0.
1731 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1733 clear
->IntegerColorLocation
=
1734 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1739 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1741 if (clear
->ArrayObj
== 0)
1743 _mesa_DeleteVertexArraysAPPLE(1, &clear
->ArrayObj
);
1744 clear
->ArrayObj
= 0;
1745 _mesa_DeleteBuffersARB(1, &clear
->VBO
);
1747 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1748 clear
->ShaderProg
= 0;
1750 if (clear
->IntegerShaderProg
) {
1751 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1752 clear
->IntegerShaderProg
= 0;
1757 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1760 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1762 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1763 GLbitfield metaSave
;
1764 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1765 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1766 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1767 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1768 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1769 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1770 const float z
= -invert_z(ctx
->Depth
.Clear
);
1775 metaSave
= (MESA_META_ALPHA_TEST
|
1777 MESA_META_DEPTH_TEST
|
1778 MESA_META_RASTERIZATION
|
1780 MESA_META_STENCIL_TEST
|
1782 MESA_META_VIEWPORT
|
1784 MESA_META_CLAMP_FRAGMENT_COLOR
);
1786 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1787 /* We'll use colormask to disable color writes. Otherwise,
1788 * respect color mask
1790 metaSave
|= MESA_META_COLOR_MASK
;
1793 _mesa_meta_begin(ctx
, metaSave
);
1795 meta_glsl_clear_init(ctx
, clear
);
1797 if (fb
->_IntegerColor
) {
1798 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1799 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1800 ctx
->Color
.ClearColor
.i
);
1802 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1803 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1804 ctx
->Color
.ClearColor
.f
);
1807 _mesa_BindVertexArray(clear
->ArrayObj
);
1808 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1810 /* GL_COLOR_BUFFER_BIT */
1811 if (buffers
& BUFFER_BITS_COLOR
) {
1812 /* leave colormask, glDrawBuffer state as-is */
1814 /* Clears never have the color clamped. */
1815 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1818 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1819 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1822 /* GL_DEPTH_BUFFER_BIT */
1823 if (buffers
& BUFFER_BIT_DEPTH
) {
1824 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1825 _mesa_DepthFunc(GL_ALWAYS
);
1826 _mesa_DepthMask(GL_TRUE
);
1829 assert(!ctx
->Depth
.Test
);
1832 /* GL_STENCIL_BUFFER_BIT */
1833 if (buffers
& BUFFER_BIT_STENCIL
) {
1834 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1835 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1836 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1837 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1838 ctx
->Stencil
.Clear
& stencilMax
,
1839 ctx
->Stencil
.WriteMask
[0]);
1842 assert(!ctx
->Stencil
.Enabled
);
1845 /* vertex positions */
1859 /* upload new vertex data */
1860 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1861 GL_DYNAMIC_DRAW_ARB
);
1864 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1866 _mesa_meta_end(ctx
);
1870 * Meta implementation of ctx->Driver.CopyPixels() in terms
1871 * of texture mapping and polygon rendering and GLSL shaders.
1874 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1875 GLsizei width
, GLsizei height
,
1876 GLint dstX
, GLint dstY
, GLenum type
)
1878 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1879 struct temp_texture
*tex
= get_temp_texture(ctx
);
1881 GLfloat x
, y
, z
, s
, t
;
1883 struct vertex verts
[4];
1885 GLenum intFormat
= GL_RGBA
;
1887 if (type
!= GL_COLOR
||
1888 ctx
->_ImageTransferState
||
1890 width
> tex
->MaxSize
||
1891 height
> tex
->MaxSize
) {
1892 /* XXX avoid this fallback */
1893 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1897 /* Most GL state applies to glCopyPixels, but a there's a few things
1898 * we need to override:
1900 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1903 MESA_META_TRANSFORM
|
1906 MESA_META_VIEWPORT
));
1908 if (copypix
->ArrayObj
== 0) {
1909 /* one-time setup */
1911 /* create vertex array object */
1912 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1913 _mesa_BindVertexArray(copypix
->ArrayObj
);
1915 /* create vertex array buffer */
1916 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1917 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1918 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1919 NULL
, GL_DYNAMIC_DRAW_ARB
);
1921 /* setup vertex arrays */
1922 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1923 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1924 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1925 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1928 _mesa_BindVertexArray(copypix
->ArrayObj
);
1929 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1932 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1934 /* vertex positions, texcoords (after texture allocation!) */
1936 const GLfloat dstX0
= (GLfloat
) dstX
;
1937 const GLfloat dstY0
= (GLfloat
) dstY
;
1938 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1939 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1940 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1950 verts
[1].s
= tex
->Sright
;
1955 verts
[2].s
= tex
->Sright
;
1956 verts
[2].t
= tex
->Ttop
;
1961 verts
[3].t
= tex
->Ttop
;
1963 /* upload new vertex data */
1964 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1967 /* Alloc/setup texture */
1968 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1969 GL_RGBA
, GL_NEAREST
);
1971 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1973 /* draw textured quad */
1974 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1976 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1978 _mesa_meta_end(ctx
);
1984 * When the glDrawPixels() image size is greater than the max rectangle
1985 * texture size we use this function to break the glDrawPixels() image
1986 * into tiles which fit into the max texture size.
1989 tiled_draw_pixels(struct gl_context
*ctx
,
1991 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1992 GLenum format
, GLenum type
,
1993 const struct gl_pixelstore_attrib
*unpack
,
1994 const GLvoid
*pixels
)
1996 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1999 if (tileUnpack
.RowLength
== 0)
2000 tileUnpack
.RowLength
= width
;
2002 for (i
= 0; i
< width
; i
+= tileSize
) {
2003 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2004 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2006 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2008 for (j
= 0; j
< height
; j
+= tileSize
) {
2009 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2010 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2012 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2014 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2015 format
, type
, &tileUnpack
, pixels
);
2022 * One-time init for drawing stencil pixels.
2025 init_draw_stencil_pixels(struct gl_context
*ctx
)
2027 /* This program is run eight times, once for each stencil bit.
2028 * The stencil values to draw are found in an 8-bit alpha texture.
2029 * We read the texture/stencil value and test if bit 'b' is set.
2030 * If the bit is not set, use KIL to kill the fragment.
2031 * Finally, we use the stencil test to update the stencil buffer.
2033 * The basic algorithm for checking if a bit is set is:
2034 * if (is_odd(value / (1 << bit)))
2035 * result is one (or non-zero).
2038 * The program parameter contains three values:
2039 * parm.x = 255 / (1 << bit)
2043 static const char *program
=
2045 "PARAM parm = program.local[0]; \n"
2047 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2048 "# t = t * 255 / bit \n"
2049 "MUL t.x, t.a, parm.x; \n"
2052 "SUB t.x, t.x, t.y; \n"
2054 "MUL t.x, t.x, parm.y; \n"
2055 "# t = fract(t.x) \n"
2056 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2057 "# t.x = (t.x == 0 ? 1 : 0) \n"
2058 "SGE t.x, -t.x, parm.z; \n"
2060 "# for debug only \n"
2061 "#MOV result.color, t.x; \n"
2063 char program2
[1000];
2064 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2065 const char *texTarget
;
2067 assert(drawpix
->StencilFP
== 0);
2069 /* replace %s with "RECT" or "2D" */
2070 assert(strlen(program
) + 4 < sizeof(program2
));
2072 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2074 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2075 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2076 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2077 strlen(program2
), (const GLubyte
*) program2
);
2082 * One-time init for drawing depth pixels.
2085 init_draw_depth_pixels(struct gl_context
*ctx
)
2087 static const char *program
=
2089 "PARAM color = program.local[0]; \n"
2090 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2091 "MOV result.color, color; \n"
2094 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2095 const char *texTarget
;
2097 assert(drawpix
->DepthFP
== 0);
2099 /* replace %s with "RECT" or "2D" */
2100 assert(strlen(program
) + 4 < sizeof(program2
));
2102 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2104 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2105 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2106 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2107 strlen(program2
), (const GLubyte
*) program2
);
2112 * Meta implementation of ctx->Driver.DrawPixels() in terms
2113 * of texture mapping and polygon rendering.
2116 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2117 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2118 GLenum format
, GLenum type
,
2119 const struct gl_pixelstore_attrib
*unpack
,
2120 const GLvoid
*pixels
)
2122 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2123 struct temp_texture
*tex
= get_temp_texture(ctx
);
2124 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2125 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2127 GLfloat x
, y
, z
, s
, t
;
2129 struct vertex verts
[4];
2130 GLenum texIntFormat
;
2131 GLboolean fallback
, newTex
;
2132 GLbitfield metaExtraSave
= 0x0;
2136 * Determine if we can do the glDrawPixels with texture mapping.
2138 fallback
= GL_FALSE
;
2139 if (ctx
->_ImageTransferState
||
2144 if (_mesa_is_color_format(format
)) {
2145 /* use more compact format when possible */
2146 /* XXX disable special case for GL_LUMINANCE for now to work around
2147 * apparent i965 driver bug (see bug #23670).
2149 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2150 texIntFormat
= format
;
2152 texIntFormat
= GL_RGBA
;
2154 /* If we're not supposed to clamp the resulting color, then just
2155 * promote our texture to fully float. We could do better by
2156 * just going for the matching set of channels, in floating
2159 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2160 ctx
->Extensions
.ARB_texture_float
)
2161 texIntFormat
= GL_RGBA32F
;
2163 else if (_mesa_is_stencil_format(format
)) {
2164 if (ctx
->Extensions
.ARB_fragment_program
&&
2165 ctx
->Pixel
.IndexShift
== 0 &&
2166 ctx
->Pixel
.IndexOffset
== 0 &&
2167 type
== GL_UNSIGNED_BYTE
) {
2168 /* We'll store stencil as alpha. This only works for GLubyte
2169 * image data because of how incoming values are mapped to alpha
2172 texIntFormat
= GL_ALPHA
;
2173 metaExtraSave
= (MESA_META_COLOR_MASK
|
2174 MESA_META_DEPTH_TEST
|
2176 MESA_META_STENCIL_TEST
);
2182 else if (_mesa_is_depth_format(format
)) {
2190 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2191 format
, type
, unpack
, pixels
);
2196 * Check image size against max texture size, draw as tiles if needed.
2198 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2199 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2200 format
, type
, unpack
, pixels
);
2204 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2205 * but a there's a few things we need to override:
2207 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2210 MESA_META_TRANSFORM
|
2213 MESA_META_VIEWPORT
|
2214 MESA_META_CLAMP_FRAGMENT_COLOR
|
2217 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2219 /* vertex positions, texcoords (after texture allocation!) */
2221 const GLfloat x0
= (GLfloat
) x
;
2222 const GLfloat y0
= (GLfloat
) y
;
2223 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2224 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2225 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2235 verts
[1].s
= tex
->Sright
;
2240 verts
[2].s
= tex
->Sright
;
2241 verts
[2].t
= tex
->Ttop
;
2246 verts
[3].t
= tex
->Ttop
;
2249 if (drawpix
->ArrayObj
== 0) {
2250 /* one-time setup: create vertex array object */
2251 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2253 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2255 /* create vertex array buffer */
2256 _mesa_GenBuffersARB(1, &vbo
);
2257 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2258 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2259 verts
, GL_DYNAMIC_DRAW_ARB
);
2261 /* setup vertex arrays */
2262 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2263 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2264 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2265 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2267 /* set given unpack params */
2268 ctx
->Unpack
= *unpack
;
2270 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2272 if (_mesa_is_stencil_format(format
)) {
2273 /* Drawing stencil */
2276 if (!drawpix
->StencilFP
)
2277 init_draw_stencil_pixels(ctx
);
2279 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2280 GL_ALPHA
, type
, pixels
);
2282 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2284 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2286 /* set all stencil bits to 0 */
2287 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2288 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2289 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2291 /* set stencil bits to 1 where needed */
2292 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2294 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2295 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2297 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2298 const GLuint mask
= 1 << bit
;
2299 if (mask
& origStencilMask
) {
2300 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2301 _mesa_StencilMask(mask
);
2303 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2304 255.0 / mask
, 0.5, 0.0, 0.0);
2306 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2310 else if (_mesa_is_depth_format(format
)) {
2312 if (!drawpix
->DepthFP
)
2313 init_draw_depth_pixels(ctx
);
2315 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2316 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2318 /* polygon color = current raster color */
2319 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2320 ctx
->Current
.RasterColor
);
2322 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2323 format
, type
, pixels
);
2325 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2329 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2330 format
, type
, pixels
);
2331 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2334 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2336 _mesa_DeleteBuffersARB(1, &vbo
);
2338 /* restore unpack params */
2339 ctx
->Unpack
= unpackSave
;
2341 _mesa_meta_end(ctx
);
2345 alpha_test_raster_color(struct gl_context
*ctx
)
2347 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2348 GLfloat ref
= ctx
->Color
.AlphaRef
;
2350 switch (ctx
->Color
.AlphaFunc
) {
2356 return alpha
== ref
;
2358 return alpha
<= ref
;
2362 return alpha
!= ref
;
2364 return alpha
>= ref
;
2374 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2375 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2376 * tracker would improve performance a lot.
2379 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2380 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2381 const struct gl_pixelstore_attrib
*unpack
,
2382 const GLubyte
*bitmap1
)
2384 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2385 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2386 const GLenum texIntFormat
= GL_ALPHA
;
2387 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2390 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2392 struct vertex verts
[4];
2397 * Check if swrast fallback is needed.
2399 if (ctx
->_ImageTransferState
||
2400 ctx
->FragmentProgram
._Enabled
||
2402 ctx
->Texture
._EnabledUnits
||
2403 width
> tex
->MaxSize
||
2404 height
> tex
->MaxSize
) {
2405 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2409 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2412 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2413 * but a there's a few things we need to override:
2415 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2416 MESA_META_PIXEL_STORE
|
2417 MESA_META_RASTERIZATION
|
2420 MESA_META_TRANSFORM
|
2423 MESA_META_VIEWPORT
));
2425 if (bitmap
->ArrayObj
== 0) {
2426 /* one-time setup */
2428 /* create vertex array object */
2429 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2430 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2432 /* create vertex array buffer */
2433 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2434 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2435 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2436 NULL
, GL_DYNAMIC_DRAW_ARB
);
2438 /* setup vertex arrays */
2439 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2440 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2441 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2442 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2443 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2444 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2447 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2448 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2451 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2453 /* vertex positions, texcoords, colors (after texture allocation!) */
2455 const GLfloat x0
= (GLfloat
) x
;
2456 const GLfloat y0
= (GLfloat
) y
;
2457 const GLfloat x1
= (GLfloat
) (x
+ width
);
2458 const GLfloat y1
= (GLfloat
) (y
+ height
);
2459 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2470 verts
[1].s
= tex
->Sright
;
2475 verts
[2].s
= tex
->Sright
;
2476 verts
[2].t
= tex
->Ttop
;
2481 verts
[3].t
= tex
->Ttop
;
2483 for (i
= 0; i
< 4; i
++) {
2484 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2485 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2486 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2487 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2490 /* upload new vertex data */
2491 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2494 /* choose different foreground/background alpha values */
2495 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2496 bg
= (fg
> 127 ? 0 : 255);
2498 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2500 _mesa_meta_end(ctx
);
2504 bitmap8
= (GLubyte
*) malloc(width
* height
);
2506 memset(bitmap8
, bg
, width
* height
);
2507 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2508 bitmap8
, width
, fg
);
2510 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2512 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2513 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2515 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2516 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2518 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2520 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2525 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2527 _mesa_meta_end(ctx
);
2532 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2533 * software fallback. The fallback path will require that the texture
2534 * images are mapped.
2535 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2538 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2539 struct gl_texture_object
*texObj
)
2541 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2542 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2543 struct gl_texture_image
*baseImage
;
2547 /* check for fallbacks */
2548 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2549 target
== GL_TEXTURE_3D
||
2550 target
== GL_TEXTURE_1D_ARRAY
||
2551 target
== GL_TEXTURE_2D_ARRAY
) {
2555 srcLevel
= texObj
->BaseLevel
;
2556 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2561 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
) {
2562 /* The texture format is sRGB but we can't turn off sRGB->linear
2563 * texture sample conversion. So we won't be able to generate the
2564 * right colors when rendering. Need to use a fallback.
2570 * Test that we can actually render in the texture's format.
2573 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2574 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2576 if (target
== GL_TEXTURE_1D
) {
2577 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2578 GL_COLOR_ATTACHMENT0_EXT
,
2579 target
, texObj
->Name
, srcLevel
);
2582 /* other work is needed to enable 3D mipmap generation */
2583 else if (target
== GL_TEXTURE_3D
) {
2585 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2586 GL_COLOR_ATTACHMENT0_EXT
,
2587 target
, texObj
->Name
, srcLevel
, zoffset
);
2592 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2593 GL_COLOR_ATTACHMENT0_EXT
,
2594 target
, texObj
->Name
, srcLevel
);
2597 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2599 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2601 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2610 * Compute the texture coordinates for the four vertices of a quad for
2611 * drawing a 2D texture image or slice of a cube/3D texture.
2612 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2613 * \param slice slice of a 1D/2D array texture or 3D texture
2614 * \param width width of the texture image
2615 * \param height height of the texture image
2616 * \param coords0/1/2/3 returns the computed texcoords
2619 setup_texture_coords(GLenum faceTarget
,
2628 static const GLfloat st
[4][2] = {
2629 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2634 switch (faceTarget
) {
2638 case GL_TEXTURE_2D_ARRAY
:
2639 if (faceTarget
== GL_TEXTURE_3D
)
2641 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2645 coords0
[0] = 0.0F
; /* s */
2646 coords0
[1] = 0.0F
; /* t */
2647 coords0
[2] = r
; /* r */
2658 case GL_TEXTURE_1D_ARRAY
:
2659 coords0
[0] = 0.0F
; /* s */
2660 coords0
[1] = slice
; /* t */
2661 coords0
[2] = 0.0F
; /* r */
2673 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2674 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2675 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2676 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2677 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2678 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2679 /* loop over quad verts */
2680 for (i
= 0; i
< 4; i
++) {
2681 /* Compute sc = +/-scale and tc = +/-scale.
2682 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2683 * though that can still sometimes happen with this scale factor...
2685 const GLfloat scale
= 0.9999f
;
2686 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2687 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2707 switch (faceTarget
) {
2708 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2713 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2718 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2723 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2728 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2733 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2744 assert(0 && "unexpected target in meta setup_texture_coords()");
2750 * Called via ctx->Driver.GenerateMipmap()
2751 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2755 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2756 struct gl_texture_object
*texObj
)
2758 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2760 GLfloat x
, y
, tex
[3];
2762 struct vertex verts
[4];
2763 const GLuint baseLevel
= texObj
->BaseLevel
;
2764 const GLuint maxLevel
= texObj
->MaxLevel
;
2765 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2766 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2767 const GLint maxLevelSave
= texObj
->MaxLevel
;
2768 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2769 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2770 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2771 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2772 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2773 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2774 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2777 const GLuint border
= 0;
2778 const GLint slice
= 0;
2780 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2781 _mesa_generate_mipmap(ctx
, target
, texObj
);
2785 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2786 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2787 faceTarget
= target
;
2788 target
= GL_TEXTURE_CUBE_MAP
;
2791 faceTarget
= target
;
2794 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2796 if (original_active_unit
!= 0)
2797 _mesa_BindTexture(target
, texObj
->Name
);
2799 if (mipmap
->ArrayObj
== 0) {
2800 /* one-time setup */
2802 /* create vertex array object */
2803 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2804 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2806 /* create vertex array buffer */
2807 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2808 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2809 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2810 NULL
, GL_DYNAMIC_DRAW_ARB
);
2812 /* setup vertex arrays */
2813 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2814 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2815 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2816 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2819 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2820 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2824 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2826 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2828 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2829 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2830 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2831 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2832 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2833 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2835 /* We don't want to encode or decode sRGB values; treat them as linear */
2836 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2837 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
2840 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2842 /* setup texcoords (XXX what about border?) */
2843 setup_texture_coords(faceTarget
,
2845 0, 0, /* width, height never used here */
2851 /* setup vertex positions */
2861 /* upload new vertex data */
2862 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2864 /* setup projection matrix */
2865 _mesa_MatrixMode(GL_PROJECTION
);
2866 _mesa_LoadIdentity();
2867 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2869 /* texture is already locked, unlock now */
2870 _mesa_unlock_texture(ctx
, texObj
);
2872 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2873 const struct gl_texture_image
*srcImage
;
2874 const GLuint srcLevel
= dstLevel
- 1;
2875 GLsizei srcWidth
, srcHeight
, srcDepth
;
2876 GLsizei dstWidth
, dstHeight
, dstDepth
;
2879 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2880 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2882 /* src size w/out border */
2883 srcWidth
= srcImage
->Width
- 2 * border
;
2884 srcHeight
= srcImage
->Height
- 2 * border
;
2885 srcDepth
= srcImage
->Depth
- 2 * border
;
2887 /* new dst size w/ border */
2888 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2889 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2890 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2892 if (dstWidth
== srcImage
->Width
&&
2893 dstHeight
== srcImage
->Height
&&
2894 dstDepth
== srcImage
->Depth
) {
2899 /* Allocate storage for the destination mipmap image(s) */
2901 /* Set MaxLevel large enough to hold the new level when we allocate it */
2902 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2904 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
2905 dstWidth
, dstHeight
, dstDepth
,
2907 srcImage
->InternalFormat
,
2908 srcImage
->TexFormat
)) {
2909 /* All done. We either ran out of memory or we would go beyond the
2910 * last valid level of an immutable texture if we continued.
2915 /* limit minification to src level */
2916 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2918 /* Set to draw into the current dstLevel */
2919 if (target
== GL_TEXTURE_1D
) {
2920 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2921 GL_COLOR_ATTACHMENT0_EXT
,
2926 else if (target
== GL_TEXTURE_3D
) {
2927 GLint zoffset
= 0; /* XXX unfinished */
2928 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2929 GL_COLOR_ATTACHMENT0_EXT
,
2936 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2937 GL_COLOR_ATTACHMENT0_EXT
,
2943 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2946 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2947 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2952 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2953 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2955 /* setup viewport */
2956 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2958 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2961 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
2962 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
2965 _mesa_lock_texture(ctx
, texObj
); /* relock */
2967 _mesa_meta_end(ctx
);
2969 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2970 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2971 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2972 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2973 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2974 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2975 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2977 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2982 * Determine the GL data type to use for the temporary image read with
2983 * ReadPixels() and passed to Tex[Sub]Image().
2986 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2988 switch (baseFormat
) {
2995 case GL_LUMINANCE_ALPHA
:
2997 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2998 return GL_UNSIGNED_BYTE
;
2999 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3000 return GL_UNSIGNED_SHORT
;
3003 case GL_DEPTH_COMPONENT
:
3004 return GL_UNSIGNED_INT
;
3005 case GL_DEPTH_STENCIL
:
3006 return GL_UNSIGNED_INT_24_8
;
3008 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3016 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3017 * Have to be careful with locking and meta state for pixel transfer.
3020 copy_tex_sub_image(struct gl_context
*ctx
,
3022 struct gl_texture_image
*texImage
,
3023 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3024 struct gl_renderbuffer
*rb
,
3026 GLsizei width
, GLsizei height
)
3028 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3029 const GLenum target
= texObj
->Target
;
3030 GLenum format
, type
;
3034 /* Choose format/type for temporary image buffer */
3035 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3036 if (format
== GL_LUMINANCE
||
3037 format
== GL_LUMINANCE_ALPHA
||
3038 format
== GL_INTENSITY
) {
3039 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3040 * temp image buffer because glReadPixels will do L=R+G+B which is
3041 * not what we want (should be L=R).
3046 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3047 _mesa_problem(ctx
, "unsupported integer color copyteximage");
3051 type
= get_temp_image_type(ctx
, format
);
3052 bpp
= _mesa_bytes_per_pixel(format
, type
);
3054 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
3059 * Alloc image buffer (XXX could use a PBO)
3061 buf
= malloc(width
* height
* bpp
);
3063 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3067 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3070 * Read image from framebuffer (disable pixel transfer ops)
3072 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3073 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3074 format
, type
, &ctx
->Pack
, buf
);
3075 _mesa_meta_end(ctx
);
3077 _mesa_update_state(ctx
); /* to update pixel transfer state */
3080 * Store texture data (with pixel transfer ops)
3082 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3083 if (target
== GL_TEXTURE_1D
) {
3084 ctx
->Driver
.TexSubImage1D(ctx
, texImage
,
3086 format
, type
, buf
, &ctx
->Unpack
);
3088 else if (target
== GL_TEXTURE_3D
) {
3089 ctx
->Driver
.TexSubImage3D(ctx
, texImage
,
3090 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3091 format
, type
, buf
, &ctx
->Unpack
);
3094 ctx
->Driver
.TexSubImage2D(ctx
, texImage
,
3095 xoffset
, yoffset
, width
, height
,
3096 format
, type
, buf
, &ctx
->Unpack
);
3098 _mesa_meta_end(ctx
);
3100 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3107 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
,
3108 struct gl_texture_image
*texImage
,
3110 struct gl_renderbuffer
*rb
,
3111 GLint x
, GLint y
, GLsizei width
)
3113 copy_tex_sub_image(ctx
, 1, texImage
, xoffset
, 0, 0,
3114 rb
, x
, y
, width
, 1);
3119 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
,
3120 struct gl_texture_image
*texImage
,
3121 GLint xoffset
, GLint yoffset
,
3122 struct gl_renderbuffer
*rb
,
3124 GLsizei width
, GLsizei height
)
3126 copy_tex_sub_image(ctx
, 2, texImage
, xoffset
, yoffset
, 0,
3127 rb
, x
, y
, width
, height
);
3132 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
,
3133 struct gl_texture_image
*texImage
,
3134 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3135 struct gl_renderbuffer
*rb
,
3137 GLsizei width
, GLsizei height
)
3139 copy_tex_sub_image(ctx
, 3, texImage
, xoffset
, yoffset
, zoffset
,
3140 rb
, x
, y
, width
, height
);