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/condrender.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/transformfeedback.h"
69 #include "main/uniforms.h"
70 #include "main/varray.h"
71 #include "main/viewport.h"
72 #include "program/program.h"
73 #include "swrast/swrast.h"
74 #include "drivers/common/meta.h"
77 /** Return offset in bytes of the field within a vertex struct */
78 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
81 * State which we may save/restore across meta ops.
82 * XXX this may be incomplete...
86 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
88 /** MESA_META_ALPHA_TEST */
89 GLboolean AlphaEnabled
;
93 /** MESA_META_BLEND */
94 GLbitfield BlendEnabled
;
95 GLboolean ColorLogicOpEnabled
;
97 /** MESA_META_COLOR_MASK */
98 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
100 /** MESA_META_DEPTH_TEST */
101 struct gl_depthbuffer_attrib Depth
;
106 /** MESA_META_PIXEL_STORE */
107 struct gl_pixelstore_attrib Pack
, Unpack
;
109 /** MESA_META_PIXEL_TRANSFER */
110 GLfloat RedBias
, RedScale
;
111 GLfloat GreenBias
, GreenScale
;
112 GLfloat BlueBias
, BlueScale
;
113 GLfloat AlphaBias
, AlphaScale
;
114 GLfloat DepthBias
, DepthScale
;
115 GLboolean MapColorFlag
;
117 /** MESA_META_RASTERIZATION */
118 GLenum FrontPolygonMode
, BackPolygonMode
;
119 GLboolean PolygonOffset
;
120 GLboolean PolygonSmooth
;
121 GLboolean PolygonStipple
;
122 GLboolean PolygonCull
;
124 /** MESA_META_SCISSOR */
125 struct gl_scissor_attrib Scissor
;
127 /** MESA_META_SHADER */
128 GLboolean VertexProgramEnabled
;
129 struct gl_vertex_program
*VertexProgram
;
130 GLboolean FragmentProgramEnabled
;
131 struct gl_fragment_program
*FragmentProgram
;
132 struct gl_shader_program
*VertexShader
;
133 struct gl_shader_program
*GeometryShader
;
134 struct gl_shader_program
*FragmentShader
;
135 struct gl_shader_program
*ActiveShader
;
137 /** MESA_META_STENCIL_TEST */
138 struct gl_stencil_attrib Stencil
;
140 /** MESA_META_TRANSFORM */
142 GLfloat ModelviewMatrix
[16];
143 GLfloat ProjectionMatrix
[16];
144 GLfloat TextureMatrix
[16];
146 /** MESA_META_CLIP */
147 GLbitfield ClipPlanesEnabled
;
149 /** MESA_META_TEXTURE */
151 GLuint ClientActiveUnit
;
152 /** for unit[0] only */
153 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
154 /** mask of TEXTURE_2D_BIT, etc */
155 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
156 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
157 GLuint EnvMode
; /* unit[0] only */
159 /** MESA_META_VERTEX */
160 struct gl_array_object
*ArrayObj
;
161 struct gl_buffer_object
*ArrayBufferObj
;
163 /** MESA_META_VIEWPORT */
164 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
165 GLclampd DepthNear
, DepthFar
;
167 /** MESA_META_CLAMP_FRAGMENT_COLOR */
168 GLenum ClampFragmentColor
;
170 /** MESA_META_CLAMP_VERTEX_COLOR */
171 GLenum ClampVertexColor
;
173 /** MESA_META_CONDITIONAL_RENDER */
174 struct gl_query_object
*CondRenderQuery
;
175 GLenum CondRenderMode
;
178 /** MESA_META_SELECT_FEEDBACK */
180 struct gl_selection Select
;
181 struct gl_feedback Feedback
;
184 /** Miscellaneous (always disabled) */
186 GLboolean RasterDiscard
;
187 #if FEATURE_EXT_transform_feedback
188 GLboolean TransformFeedbackNeedsResume
;
193 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
194 * This is currently shared by all the meta ops. But we could create a
195 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
200 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
201 GLsizei MinSize
; /**< Min texture size to allocate */
202 GLsizei MaxSize
; /**< Max possible texture size */
203 GLboolean NPOT
; /**< Non-power of two size OK? */
204 GLsizei Width
, Height
; /**< Current texture size */
206 GLfloat Sright
, Ttop
; /**< right, top texcoords */
211 * State for glBlitFramebufer()
222 * State for glClear()
231 GLuint IntegerShaderProg
;
232 GLint IntegerColorLocation
;
237 * State for glCopyPixels()
247 * State for glDrawPixels()
253 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
254 GLuint DepthFP
; /**< Fragment program for drawing depth images */
259 * State for glBitmap()
265 struct temp_texture Tex
; /**< separate texture from other meta ops */
270 * State for _mesa_meta_generate_mipmap()
272 struct gen_mipmap_state
281 * State for texture decompression
283 struct decompress_state
286 GLuint VBO
, FBO
, RBO
;
291 * State for glDrawTex()
299 #define MAX_META_OPS_DEPTH 8
301 * All per-context meta state.
305 /** Stack of state saved during meta-ops */
306 struct save_state Save
[MAX_META_OPS_DEPTH
];
307 /** Save stack depth */
308 GLuint SaveStackDepth
;
310 struct temp_texture TempTex
;
312 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
313 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
314 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
315 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
316 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
317 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
318 struct decompress_state Decompress
; /**< For texture decompression */
319 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
322 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
323 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
324 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
327 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
333 shader
= _mesa_CreateShaderObjectARB(target
);
334 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
335 _mesa_CompileShaderARB(shader
);
337 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
341 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
343 _mesa_DeleteObjectARB(shader
);
349 _mesa_DeleteObjectARB(shader
);
353 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
355 "meta program compile failed:\n%s\n"
360 _mesa_DeleteObjectARB(shader
);
366 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
371 _mesa_LinkProgramARB(program
);
373 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
377 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
385 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
386 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
394 * Initialize meta-ops for a context.
395 * To be called once during context creation.
398 _mesa_meta_init(struct gl_context
*ctx
)
402 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
407 * Free context meta-op state.
408 * To be called once during context destruction.
411 _mesa_meta_free(struct gl_context
*ctx
)
413 GET_CURRENT_CONTEXT(old_context
);
414 _mesa_make_current(ctx
, NULL
, NULL
);
415 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
416 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
417 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
419 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
421 _mesa_make_current(NULL
, NULL
, NULL
);
428 * Enter meta state. This is like a light-weight version of glPushAttrib
429 * but it also resets most GL state back to default values.
431 * \param state bitmask of MESA_META_* flags indicating which attribute groups
432 * to save and reset to their defaults
435 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
437 struct save_state
*save
;
439 /* hope MAX_META_OPS_DEPTH is large enough */
440 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
442 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
443 memset(save
, 0, sizeof(*save
));
444 save
->SavedState
= state
;
446 #if FEATURE_EXT_transform_feedback
447 /* Pausing transform feedback needs to be done early, or else we won't be
448 * able to change other state.
450 save
->TransformFeedbackNeedsResume
=
451 ctx
->TransformFeedback
.CurrentObject
->Active
&&
452 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
453 if (save
->TransformFeedbackNeedsResume
)
454 _mesa_PauseTransformFeedback();
457 if (state
& MESA_META_ALPHA_TEST
) {
458 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
459 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
460 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
461 if (ctx
->Color
.AlphaEnabled
)
462 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
465 if (state
& MESA_META_BLEND
) {
466 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
467 if (ctx
->Color
.BlendEnabled
) {
468 if (ctx
->Extensions
.EXT_draw_buffers2
) {
470 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
471 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
475 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
478 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
479 if (ctx
->Color
.ColorLogicOpEnabled
)
480 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
483 if (state
& MESA_META_COLOR_MASK
) {
484 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
485 sizeof(ctx
->Color
.ColorMask
));
486 if (!ctx
->Color
.ColorMask
[0][0] ||
487 !ctx
->Color
.ColorMask
[0][1] ||
488 !ctx
->Color
.ColorMask
[0][2] ||
489 !ctx
->Color
.ColorMask
[0][3])
490 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
493 if (state
& MESA_META_DEPTH_TEST
) {
494 save
->Depth
= ctx
->Depth
; /* struct copy */
496 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
499 if (state
& MESA_META_FOG
) {
500 save
->Fog
= ctx
->Fog
.Enabled
;
501 if (ctx
->Fog
.Enabled
)
502 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
505 if (state
& MESA_META_PIXEL_STORE
) {
506 save
->Pack
= ctx
->Pack
;
507 save
->Unpack
= ctx
->Unpack
;
508 ctx
->Pack
= ctx
->DefaultPacking
;
509 ctx
->Unpack
= ctx
->DefaultPacking
;
512 if (state
& MESA_META_PIXEL_TRANSFER
) {
513 save
->RedScale
= ctx
->Pixel
.RedScale
;
514 save
->RedBias
= ctx
->Pixel
.RedBias
;
515 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
516 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
517 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
518 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
519 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
520 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
521 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
522 ctx
->Pixel
.RedScale
= 1.0F
;
523 ctx
->Pixel
.RedBias
= 0.0F
;
524 ctx
->Pixel
.GreenScale
= 1.0F
;
525 ctx
->Pixel
.GreenBias
= 0.0F
;
526 ctx
->Pixel
.BlueScale
= 1.0F
;
527 ctx
->Pixel
.BlueBias
= 0.0F
;
528 ctx
->Pixel
.AlphaScale
= 1.0F
;
529 ctx
->Pixel
.AlphaBias
= 0.0F
;
530 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
532 ctx
->NewState
|=_NEW_PIXEL
;
535 if (state
& MESA_META_RASTERIZATION
) {
536 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
537 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
538 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
539 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
540 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
541 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
542 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
543 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
544 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
545 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
546 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
549 if (state
& MESA_META_SCISSOR
) {
550 save
->Scissor
= ctx
->Scissor
; /* struct copy */
551 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
554 if (state
& MESA_META_SHADER
) {
555 if (ctx
->Extensions
.ARB_vertex_program
) {
556 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
557 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
558 ctx
->VertexProgram
.Current
);
559 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
562 if (ctx
->Extensions
.ARB_fragment_program
) {
563 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
564 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
565 ctx
->FragmentProgram
.Current
);
566 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
569 if (ctx
->Extensions
.ARB_shader_objects
) {
570 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
571 ctx
->Shader
.CurrentVertexProgram
);
572 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
573 ctx
->Shader
.CurrentGeometryProgram
);
574 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
575 ctx
->Shader
.CurrentFragmentProgram
);
576 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
577 ctx
->Shader
.ActiveProgram
);
579 _mesa_UseProgramObjectARB(0);
583 if (state
& MESA_META_STENCIL_TEST
) {
584 save
->Stencil
= ctx
->Stencil
; /* struct copy */
585 if (ctx
->Stencil
.Enabled
)
586 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
587 /* NOTE: other stencil state not reset */
590 if (state
& MESA_META_TEXTURE
) {
593 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
594 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
595 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
597 /* Disable all texture units */
598 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
599 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
600 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
601 if (ctx
->Texture
.Unit
[u
].Enabled
||
602 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
603 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
604 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
605 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
606 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
607 if (ctx
->Extensions
.ARB_texture_cube_map
)
608 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
609 if (ctx
->Extensions
.NV_texture_rectangle
)
610 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
611 if (ctx
->Extensions
.OES_EGL_image_external
)
612 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
613 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
614 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
615 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
616 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
620 /* save current texture objects for unit[0] only */
621 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
622 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
623 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
626 /* set defaults for unit[0] */
627 _mesa_ActiveTextureARB(GL_TEXTURE0
);
628 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
629 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
632 if (state
& MESA_META_TRANSFORM
) {
633 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
634 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
635 16 * sizeof(GLfloat
));
636 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
637 16 * sizeof(GLfloat
));
638 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
639 16 * sizeof(GLfloat
));
640 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
641 /* set 1:1 vertex:pixel coordinate transform */
642 _mesa_ActiveTextureARB(GL_TEXTURE0
);
643 _mesa_MatrixMode(GL_TEXTURE
);
644 _mesa_LoadIdentity();
645 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
646 _mesa_MatrixMode(GL_MODELVIEW
);
647 _mesa_LoadIdentity();
648 _mesa_MatrixMode(GL_PROJECTION
);
649 _mesa_LoadIdentity();
650 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
651 0.0, ctx
->DrawBuffer
->Height
,
655 if (state
& MESA_META_CLIP
) {
656 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
657 if (ctx
->Transform
.ClipPlanesEnabled
) {
659 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
660 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
665 if (state
& MESA_META_VERTEX
) {
666 /* save vertex array object state */
667 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
668 ctx
->Array
.ArrayObj
);
669 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
670 ctx
->Array
.ArrayBufferObj
);
671 /* set some default state? */
674 if (state
& MESA_META_VIEWPORT
) {
675 /* save viewport state */
676 save
->ViewportX
= ctx
->Viewport
.X
;
677 save
->ViewportY
= ctx
->Viewport
.Y
;
678 save
->ViewportW
= ctx
->Viewport
.Width
;
679 save
->ViewportH
= ctx
->Viewport
.Height
;
680 /* set viewport to match window size */
681 if (ctx
->Viewport
.X
!= 0 ||
682 ctx
->Viewport
.Y
!= 0 ||
683 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
684 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
685 _mesa_set_viewport(ctx
, 0, 0,
686 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
688 /* save depth range state */
689 save
->DepthNear
= ctx
->Viewport
.Near
;
690 save
->DepthFar
= ctx
->Viewport
.Far
;
691 /* set depth range to default */
692 _mesa_DepthRange(0.0, 1.0);
695 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
696 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
698 /* Generally in here we want to do clamping according to whether
699 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
700 * regardless of the internal implementation of the metaops.
702 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
703 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
706 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
707 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
709 /* Generally in here we never want vertex color clamping --
710 * result clamping is only dependent on fragment clamping.
712 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
715 if (state
& MESA_META_CONDITIONAL_RENDER
) {
716 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
717 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
719 if (ctx
->Query
.CondRenderQuery
)
720 _mesa_EndConditionalRender();
724 if (state
& MESA_META_SELECT_FEEDBACK
) {
725 save
->RenderMode
= ctx
->RenderMode
;
726 if (ctx
->RenderMode
== GL_SELECT
) {
727 save
->Select
= ctx
->Select
; /* struct copy */
728 _mesa_RenderMode(GL_RENDER
);
729 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
730 save
->Feedback
= ctx
->Feedback
; /* struct copy */
731 _mesa_RenderMode(GL_RENDER
);
738 save
->Lighting
= ctx
->Light
.Enabled
;
739 if (ctx
->Light
.Enabled
)
740 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
741 save
->RasterDiscard
= ctx
->RasterDiscard
;
742 if (ctx
->RasterDiscard
)
743 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
749 * Leave meta state. This is like a light-weight version of glPopAttrib().
752 _mesa_meta_end(struct gl_context
*ctx
)
754 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
755 const GLbitfield state
= save
->SavedState
;
757 if (state
& MESA_META_ALPHA_TEST
) {
758 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
759 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
760 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
763 if (state
& MESA_META_BLEND
) {
764 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
765 if (ctx
->Extensions
.EXT_draw_buffers2
) {
767 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
768 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
772 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
775 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
776 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
779 if (state
& MESA_META_COLOR_MASK
) {
781 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
782 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
784 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
785 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
788 _mesa_ColorMaskIndexed(i
,
789 save
->ColorMask
[i
][0],
790 save
->ColorMask
[i
][1],
791 save
->ColorMask
[i
][2],
792 save
->ColorMask
[i
][3]);
798 if (state
& MESA_META_DEPTH_TEST
) {
799 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
800 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
801 _mesa_DepthFunc(save
->Depth
.Func
);
802 _mesa_DepthMask(save
->Depth
.Mask
);
805 if (state
& MESA_META_FOG
) {
806 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
809 if (state
& MESA_META_PIXEL_STORE
) {
810 ctx
->Pack
= save
->Pack
;
811 ctx
->Unpack
= save
->Unpack
;
814 if (state
& MESA_META_PIXEL_TRANSFER
) {
815 ctx
->Pixel
.RedScale
= save
->RedScale
;
816 ctx
->Pixel
.RedBias
= save
->RedBias
;
817 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
818 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
819 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
820 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
821 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
822 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
823 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
825 ctx
->NewState
|=_NEW_PIXEL
;
828 if (state
& MESA_META_RASTERIZATION
) {
829 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
830 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
831 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
832 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
833 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
834 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
837 if (state
& MESA_META_SCISSOR
) {
838 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
839 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
840 save
->Scissor
.Width
, save
->Scissor
.Height
);
843 if (state
& MESA_META_SHADER
) {
844 if (ctx
->Extensions
.ARB_vertex_program
) {
845 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
846 save
->VertexProgramEnabled
);
847 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
848 save
->VertexProgram
);
849 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
852 if (ctx
->Extensions
.ARB_fragment_program
) {
853 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
854 save
->FragmentProgramEnabled
);
855 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
856 save
->FragmentProgram
);
857 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
860 if (ctx
->Extensions
.ARB_vertex_shader
)
861 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
863 if (ctx
->Extensions
.ARB_geometry_shader4
)
864 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
865 save
->GeometryShader
);
867 if (ctx
->Extensions
.ARB_fragment_shader
)
868 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
869 save
->FragmentShader
);
871 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
874 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
875 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
876 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
877 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
880 if (state
& MESA_META_STENCIL_TEST
) {
881 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
883 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
884 _mesa_ClearStencil(stencil
->Clear
);
885 if (ctx
->Extensions
.EXT_stencil_two_side
) {
886 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
887 stencil
->TestTwoSide
);
888 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
889 ? GL_BACK
: GL_FRONT
);
892 _mesa_StencilFuncSeparate(GL_FRONT
,
893 stencil
->Function
[0],
895 stencil
->ValueMask
[0]);
896 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
897 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
898 stencil
->ZFailFunc
[0],
899 stencil
->ZPassFunc
[0]);
901 _mesa_StencilFuncSeparate(GL_BACK
,
902 stencil
->Function
[1],
904 stencil
->ValueMask
[1]);
905 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
906 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
907 stencil
->ZFailFunc
[1],
908 stencil
->ZPassFunc
[1]);
911 if (state
& MESA_META_TEXTURE
) {
914 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
916 /* restore texenv for unit[0] */
917 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
919 /* restore texture objects for unit[0] only */
920 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
921 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
922 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
923 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
924 save
->CurrentTexture
[tgt
]);
926 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
929 /* Restore fixed function texture enables, texgen */
930 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
931 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
932 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
933 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
936 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
937 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
938 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
942 /* restore current unit state */
943 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
944 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
947 if (state
& MESA_META_TRANSFORM
) {
948 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
949 _mesa_ActiveTextureARB(GL_TEXTURE0
);
950 _mesa_MatrixMode(GL_TEXTURE
);
951 _mesa_LoadMatrixf(save
->TextureMatrix
);
952 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
954 _mesa_MatrixMode(GL_MODELVIEW
);
955 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
957 _mesa_MatrixMode(GL_PROJECTION
);
958 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
960 _mesa_MatrixMode(save
->MatrixMode
);
963 if (state
& MESA_META_CLIP
) {
964 if (save
->ClipPlanesEnabled
) {
966 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
967 if (save
->ClipPlanesEnabled
& (1 << i
)) {
968 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
974 if (state
& MESA_META_VERTEX
) {
975 /* restore vertex buffer object */
976 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
977 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
979 /* restore vertex array object */
980 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
981 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
984 if (state
& MESA_META_VIEWPORT
) {
985 if (save
->ViewportX
!= ctx
->Viewport
.X
||
986 save
->ViewportY
!= ctx
->Viewport
.Y
||
987 save
->ViewportW
!= ctx
->Viewport
.Width
||
988 save
->ViewportH
!= ctx
->Viewport
.Height
) {
989 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
990 save
->ViewportW
, save
->ViewportH
);
992 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
995 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
996 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
999 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
1000 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1003 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1004 if (save
->CondRenderQuery
)
1005 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1006 save
->CondRenderMode
);
1009 #if FEATURE_feedback
1010 if (state
& MESA_META_SELECT_FEEDBACK
) {
1011 if (save
->RenderMode
== GL_SELECT
) {
1012 _mesa_RenderMode(GL_SELECT
);
1013 ctx
->Select
= save
->Select
;
1014 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1015 _mesa_RenderMode(GL_FEEDBACK
);
1016 ctx
->Feedback
= save
->Feedback
;
1022 if (save
->Lighting
) {
1023 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1025 if (save
->RasterDiscard
) {
1026 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1028 #if FEATURE_EXT_transform_feedback
1029 if (save
->TransformFeedbackNeedsResume
)
1030 _mesa_ResumeTransformFeedback();
1036 * Determine whether Mesa is currently in a meta state.
1039 _mesa_meta_in_progress(struct gl_context
*ctx
)
1041 return ctx
->Meta
->SaveStackDepth
!= 0;
1046 * Convert Z from a normalized value in the range [0, 1] to an object-space
1047 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1048 * default/identity ortho projection results in the original Z value.
1049 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1050 * value comes from the clear value or raster position.
1052 static INLINE GLfloat
1053 invert_z(GLfloat normZ
)
1055 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1061 * One-time init for a temp_texture object.
1062 * Choose tex target, compute max tex size, etc.
1065 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1067 /* prefer texture rectangle */
1068 if (ctx
->Extensions
.NV_texture_rectangle
) {
1069 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1070 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1071 tex
->NPOT
= GL_TRUE
;
1074 /* use 2D texture, NPOT if possible */
1075 tex
->Target
= GL_TEXTURE_2D
;
1076 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1077 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1079 tex
->MinSize
= 16; /* 16 x 16 at least */
1080 assert(tex
->MaxSize
> 0);
1082 _mesa_GenTextures(1, &tex
->TexObj
);
1086 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1090 _mesa_DeleteTextures(1, &tex
->TexObj
);
1096 * Return pointer to temp_texture info for non-bitmap ops.
1097 * This does some one-time init if needed.
1099 static struct temp_texture
*
1100 get_temp_texture(struct gl_context
*ctx
)
1102 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1105 init_temp_texture(ctx
, tex
);
1113 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1114 * We use a separate texture for bitmaps to reduce texture
1115 * allocation/deallocation.
1117 static struct temp_texture
*
1118 get_bitmap_temp_texture(struct gl_context
*ctx
)
1120 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1123 init_temp_texture(ctx
, tex
);
1131 * Compute the width/height of texture needed to draw an image of the
1132 * given size. Return a flag indicating whether the current texture
1133 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1134 * allocated (glTexImage2D).
1135 * Also, compute s/t texcoords for drawing.
1137 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1140 alloc_texture(struct temp_texture
*tex
,
1141 GLsizei width
, GLsizei height
, GLenum intFormat
)
1143 GLboolean newTex
= GL_FALSE
;
1145 ASSERT(width
<= tex
->MaxSize
);
1146 ASSERT(height
<= tex
->MaxSize
);
1148 if (width
> tex
->Width
||
1149 height
> tex
->Height
||
1150 intFormat
!= tex
->IntFormat
) {
1151 /* alloc new texture (larger or different format) */
1154 /* use non-power of two size */
1155 tex
->Width
= MAX2(tex
->MinSize
, width
);
1156 tex
->Height
= MAX2(tex
->MinSize
, height
);
1159 /* find power of two size */
1161 w
= h
= tex
->MinSize
;
1170 tex
->IntFormat
= intFormat
;
1175 /* compute texcoords */
1176 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1177 tex
->Sright
= (GLfloat
) width
;
1178 tex
->Ttop
= (GLfloat
) height
;
1181 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1182 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1190 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1193 setup_copypix_texture(struct temp_texture
*tex
,
1195 GLint srcX
, GLint srcY
,
1196 GLsizei width
, GLsizei height
, GLenum intFormat
,
1199 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1200 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1201 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1202 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1204 /* copy framebuffer image to texture */
1206 /* create new tex image */
1207 if (tex
->Width
== width
&& tex
->Height
== height
) {
1208 /* create new tex with framebuffer data */
1209 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1210 srcX
, srcY
, width
, height
, 0);
1213 /* create empty texture */
1214 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1215 tex
->Width
, tex
->Height
, 0,
1216 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1218 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1219 0, 0, srcX
, srcY
, width
, height
);
1223 /* replace existing tex image */
1224 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1225 0, 0, srcX
, srcY
, width
, height
);
1231 * Setup/load texture for glDrawPixels.
1234 setup_drawpix_texture(struct gl_context
*ctx
,
1235 struct temp_texture
*tex
,
1237 GLenum texIntFormat
,
1238 GLsizei width
, GLsizei height
,
1239 GLenum format
, GLenum type
,
1240 const GLvoid
*pixels
)
1242 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1243 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1244 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1245 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1247 /* copy pixel data to texture */
1249 /* create new tex image */
1250 if (tex
->Width
== width
&& tex
->Height
== height
) {
1251 /* create new tex and load image data */
1252 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1253 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1256 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1258 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1259 ctx
->Unpack
.BufferObj
);
1260 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1261 /* create empty texture */
1262 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1263 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1264 if (save_unpack_obj
!= NULL
)
1265 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1266 save_unpack_obj
->Name
);
1268 _mesa_TexSubImage2D(tex
->Target
, 0,
1269 0, 0, width
, height
, format
, type
, pixels
);
1273 /* replace existing tex image */
1274 _mesa_TexSubImage2D(tex
->Target
, 0,
1275 0, 0, width
, height
, format
, type
, pixels
);
1282 * One-time init for drawing depth pixels.
1285 init_blit_depth_pixels(struct gl_context
*ctx
)
1287 static const char *program
=
1289 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1292 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1293 struct temp_texture
*tex
= get_temp_texture(ctx
);
1294 const char *texTarget
;
1296 assert(blit
->DepthFP
== 0);
1298 /* replace %s with "RECT" or "2D" */
1299 assert(strlen(program
) + 4 < sizeof(program2
));
1300 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1304 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1306 _mesa_GenPrograms(1, &blit
->DepthFP
);
1307 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1308 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1309 strlen(program2
), (const GLubyte
*) program2
);
1314 * Try to do a glBlitFramebuffer using no-copy texturing.
1315 * We can do this when the src renderbuffer is actually a texture.
1316 * But if the src buffer == dst buffer we cannot do this.
1318 * \return new buffer mask indicating the buffers left to blit using the
1322 blitframebuffer_texture(struct gl_context
*ctx
,
1323 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1324 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1325 GLbitfield mask
, GLenum filter
)
1327 if (mask
& GL_COLOR_BUFFER_BIT
) {
1328 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1329 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1330 const struct gl_renderbuffer_attachment
*drawAtt
=
1331 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1332 const struct gl_renderbuffer_attachment
*readAtt
=
1333 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1335 if (readAtt
&& readAtt
->Texture
) {
1336 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1337 const GLuint srcLevel
= readAtt
->TextureLevel
;
1338 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1339 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1340 const GLint baseLevelSave
= texObj
->BaseLevel
;
1341 const GLint maxLevelSave
= texObj
->MaxLevel
;
1342 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1343 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1344 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1345 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1346 const GLenum target
= texObj
->Target
;
1348 if (drawAtt
->Texture
== readAtt
->Texture
) {
1349 /* Can't use same texture as both the source and dest. We need
1350 * to handle overlapping blits and besides, some hw may not
1356 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1357 /* Can't handle other texture types at this time */
1362 printf("Blit from texture!\n");
1363 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1364 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1367 /* Prepare src texture state */
1368 _mesa_BindTexture(target
, texObj
->Name
);
1369 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1370 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1371 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1372 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1373 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1375 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1376 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1378 /* Always do our blits with no sRGB decode or encode.*/
1379 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1380 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1381 GL_SKIP_DECODE_EXT
);
1383 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1384 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1387 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1388 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1390 /* Prepare vertex data (the VBO was previously created and bound) */
1395 struct vertex verts
[4];
1396 GLfloat s0
, t0
, s1
, t1
;
1398 if (target
== GL_TEXTURE_2D
) {
1399 const struct gl_texture_image
*texImage
1400 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1401 s0
= srcX0
/ (float) texImage
->Width
;
1402 s1
= srcX1
/ (float) texImage
->Width
;
1403 t0
= srcY0
/ (float) texImage
->Height
;
1404 t1
= srcY1
/ (float) texImage
->Height
;
1407 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1414 verts
[0].x
= (GLfloat
) dstX0
;
1415 verts
[0].y
= (GLfloat
) dstY0
;
1416 verts
[1].x
= (GLfloat
) dstX1
;
1417 verts
[1].y
= (GLfloat
) dstY0
;
1418 verts
[2].x
= (GLfloat
) dstX1
;
1419 verts
[2].y
= (GLfloat
) dstY1
;
1420 verts
[3].x
= (GLfloat
) dstX0
;
1421 verts
[3].y
= (GLfloat
) dstY1
;
1432 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1435 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1437 /* Restore texture object state, the texture binding will
1438 * be restored by _mesa_meta_end().
1440 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1441 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1442 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1443 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1444 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1446 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1447 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1448 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1449 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1451 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1452 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1455 /* Done with color buffer */
1456 mask
&= ~GL_COLOR_BUFFER_BIT
;
1465 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1466 * of texture mapping and polygon rendering.
1469 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1470 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1471 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1472 GLbitfield mask
, GLenum filter
)
1474 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1475 struct temp_texture
*tex
= get_temp_texture(ctx
);
1476 const GLsizei maxTexSize
= tex
->MaxSize
;
1477 const GLint srcX
= MIN2(srcX0
, srcX1
);
1478 const GLint srcY
= MIN2(srcY0
, srcY1
);
1479 const GLint srcW
= abs(srcX1
- srcX0
);
1480 const GLint srcH
= abs(srcY1
- srcY0
);
1481 const GLboolean srcFlipX
= srcX1
< srcX0
;
1482 const GLboolean srcFlipY
= srcY1
< srcY0
;
1486 struct vertex verts
[4];
1489 /* In addition to falling back if the blit size is larger than the maximum
1490 * texture size, fallback if the source is multisampled. This fallback can
1491 * be removed once Mesa gets support ARB_texture_multisample.
1493 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1494 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1495 /* XXX avoid this fallback */
1496 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1497 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1513 /* only scissor effects blit so save/clear all other relevant state */
1514 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1516 if (blit
->ArrayObj
== 0) {
1517 /* one-time setup */
1519 /* create vertex array object */
1520 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1521 _mesa_BindVertexArray(blit
->ArrayObj
);
1523 /* create vertex array buffer */
1524 _mesa_GenBuffersARB(1, &blit
->VBO
);
1525 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1526 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1527 NULL
, GL_DYNAMIC_DRAW_ARB
);
1529 /* setup vertex arrays */
1530 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1531 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1532 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1533 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1536 _mesa_BindVertexArray(blit
->ArrayObj
);
1537 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1540 /* Try faster, direct texture approach first */
1541 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1542 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1544 _mesa_meta_end(ctx
);
1548 /* Continue with "normal" approach which involves copying the src rect
1549 * into a temporary texture and is "blitted" by drawing a textured quad.
1552 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1554 /* vertex positions/texcoords (after texture allocation!) */
1556 verts
[0].x
= (GLfloat
) dstX0
;
1557 verts
[0].y
= (GLfloat
) dstY0
;
1558 verts
[1].x
= (GLfloat
) dstX1
;
1559 verts
[1].y
= (GLfloat
) dstY0
;
1560 verts
[2].x
= (GLfloat
) dstX1
;
1561 verts
[2].y
= (GLfloat
) dstY1
;
1562 verts
[3].x
= (GLfloat
) dstX0
;
1563 verts
[3].y
= (GLfloat
) dstY1
;
1567 verts
[1].s
= tex
->Sright
;
1569 verts
[2].s
= tex
->Sright
;
1570 verts
[2].t
= tex
->Ttop
;
1572 verts
[3].t
= tex
->Ttop
;
1574 /* upload new vertex data */
1575 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1578 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1580 if (mask
& GL_COLOR_BUFFER_BIT
) {
1581 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1583 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1584 mask
&= ~GL_COLOR_BUFFER_BIT
;
1587 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1588 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1591 init_blit_depth_pixels(ctx
);
1593 /* maybe change tex format here */
1594 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1596 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1597 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1599 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1600 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1602 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1603 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1604 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1605 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1606 _mesa_DepthFunc(GL_ALWAYS
);
1607 _mesa_DepthMask(GL_TRUE
);
1609 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1610 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1616 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1617 /* XXX can't easily do stencil */
1620 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1622 _mesa_meta_end(ctx
);
1625 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1626 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1631 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1633 if (blit
->ArrayObj
) {
1634 _mesa_DeleteVertexArraysAPPLE(1, &blit
->ArrayObj
);
1636 _mesa_DeleteBuffersARB(1, &blit
->VBO
);
1639 if (blit
->DepthFP
) {
1640 _mesa_DeletePrograms(1, &blit
->DepthFP
);
1647 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1650 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1652 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1654 GLfloat x
, y
, z
, r
, g
, b
, a
;
1656 struct vertex verts
[4];
1657 /* save all state but scissor, pixel pack/unpack */
1658 GLbitfield metaSave
= (MESA_META_ALL
-
1660 MESA_META_PIXEL_STORE
-
1661 MESA_META_CONDITIONAL_RENDER
);
1662 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1664 if (buffers
& BUFFER_BITS_COLOR
) {
1665 /* if clearing color buffers, don't save/restore colormask */
1666 metaSave
-= MESA_META_COLOR_MASK
;
1669 _mesa_meta_begin(ctx
, metaSave
);
1671 if (clear
->ArrayObj
== 0) {
1672 /* one-time setup */
1674 /* create vertex array object */
1675 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1676 _mesa_BindVertexArray(clear
->ArrayObj
);
1678 /* create vertex array buffer */
1679 _mesa_GenBuffersARB(1, &clear
->VBO
);
1680 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1682 /* setup vertex arrays */
1683 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1684 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1685 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1686 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1689 _mesa_BindVertexArray(clear
->ArrayObj
);
1690 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1693 /* GL_COLOR_BUFFER_BIT */
1694 if (buffers
& BUFFER_BITS_COLOR
) {
1695 /* leave colormask, glDrawBuffer state as-is */
1697 /* Clears never have the color clamped. */
1698 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1701 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1702 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1705 /* GL_DEPTH_BUFFER_BIT */
1706 if (buffers
& BUFFER_BIT_DEPTH
) {
1707 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1708 _mesa_DepthFunc(GL_ALWAYS
);
1709 _mesa_DepthMask(GL_TRUE
);
1712 assert(!ctx
->Depth
.Test
);
1715 /* GL_STENCIL_BUFFER_BIT */
1716 if (buffers
& BUFFER_BIT_STENCIL
) {
1717 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1718 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1719 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1720 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1721 ctx
->Stencil
.Clear
& stencilMax
,
1722 ctx
->Stencil
.WriteMask
[0]);
1725 assert(!ctx
->Stencil
.Enabled
);
1728 /* vertex positions/colors */
1730 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1731 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1732 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1733 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1734 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1751 for (i
= 0; i
< 4; i
++) {
1752 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1753 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1754 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1755 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1758 /* upload new vertex data */
1759 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1760 GL_DYNAMIC_DRAW_ARB
);
1764 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1766 _mesa_meta_end(ctx
);
1770 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1772 const char *vs_source
=
1773 "attribute vec4 position;\n"
1776 " gl_Position = position;\n"
1778 const char *fs_source
=
1779 "uniform vec4 color;\n"
1782 " gl_FragColor = color;\n"
1784 const char *vs_int_source
=
1786 "attribute vec4 position;\n"
1789 " gl_Position = position;\n"
1791 const char *fs_int_source
=
1793 "uniform ivec4 color;\n"
1794 "out ivec4 out_color;\n"
1798 " out_color = color;\n"
1802 if (clear
->ArrayObj
!= 0)
1805 /* create vertex array object */
1806 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1807 _mesa_BindVertexArray(clear
->ArrayObj
);
1809 /* create vertex array buffer */
1810 _mesa_GenBuffersARB(1, &clear
->VBO
);
1811 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1813 /* setup vertex arrays */
1814 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1815 _mesa_EnableVertexAttribArrayARB(0);
1817 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1818 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1819 _mesa_CompileShaderARB(vs
);
1821 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1822 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1823 _mesa_CompileShaderARB(fs
);
1825 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1826 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1827 _mesa_DeleteObjectARB(fs
);
1828 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1829 _mesa_DeleteObjectARB(vs
);
1830 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1831 _mesa_LinkProgramARB(clear
->ShaderProg
);
1833 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1836 if (ctx
->API
== API_OPENGL
&& ctx
->Const
.GLSLVersion
>= 130) {
1837 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1838 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1840 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1841 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1842 _mesa_DeleteObjectARB(fs
);
1843 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1844 _mesa_DeleteObjectARB(vs
);
1845 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1847 /* Note that user-defined out attributes get automatically assigned
1848 * locations starting from 0, so we don't need to explicitly
1849 * BindFragDataLocation to 0.
1852 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1854 clear
->IntegerColorLocation
=
1855 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1860 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1862 if (clear
->ArrayObj
== 0)
1864 _mesa_DeleteVertexArraysAPPLE(1, &clear
->ArrayObj
);
1865 clear
->ArrayObj
= 0;
1866 _mesa_DeleteBuffersARB(1, &clear
->VBO
);
1868 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1869 clear
->ShaderProg
= 0;
1871 if (clear
->IntegerShaderProg
) {
1872 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1873 clear
->IntegerShaderProg
= 0;
1878 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1881 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1883 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1884 GLbitfield metaSave
;
1885 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1886 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1887 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1888 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1889 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1890 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1891 const float z
= -invert_z(ctx
->Depth
.Clear
);
1896 metaSave
= (MESA_META_ALPHA_TEST
|
1898 MESA_META_DEPTH_TEST
|
1899 MESA_META_RASTERIZATION
|
1901 MESA_META_STENCIL_TEST
|
1903 MESA_META_VIEWPORT
|
1905 MESA_META_CLAMP_FRAGMENT_COLOR
);
1907 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1908 /* We'll use colormask to disable color writes. Otherwise,
1909 * respect color mask
1911 metaSave
|= MESA_META_COLOR_MASK
;
1914 _mesa_meta_begin(ctx
, metaSave
);
1916 meta_glsl_clear_init(ctx
, clear
);
1918 if (fb
->_IntegerColor
) {
1919 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1920 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1921 ctx
->Color
.ClearColor
.i
);
1923 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1924 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1925 ctx
->Color
.ClearColor
.f
);
1928 _mesa_BindVertexArray(clear
->ArrayObj
);
1929 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1931 /* GL_COLOR_BUFFER_BIT */
1932 if (buffers
& BUFFER_BITS_COLOR
) {
1933 /* leave colormask, glDrawBuffer state as-is */
1935 /* Clears never have the color clamped. */
1936 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1939 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1940 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1943 /* GL_DEPTH_BUFFER_BIT */
1944 if (buffers
& BUFFER_BIT_DEPTH
) {
1945 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1946 _mesa_DepthFunc(GL_ALWAYS
);
1947 _mesa_DepthMask(GL_TRUE
);
1950 assert(!ctx
->Depth
.Test
);
1953 /* GL_STENCIL_BUFFER_BIT */
1954 if (buffers
& BUFFER_BIT_STENCIL
) {
1955 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1956 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1957 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1958 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1959 ctx
->Stencil
.Clear
& stencilMax
,
1960 ctx
->Stencil
.WriteMask
[0]);
1963 assert(!ctx
->Stencil
.Enabled
);
1966 /* vertex positions */
1980 /* upload new vertex data */
1981 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1982 GL_DYNAMIC_DRAW_ARB
);
1985 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1987 _mesa_meta_end(ctx
);
1991 * Meta implementation of ctx->Driver.CopyPixels() in terms
1992 * of texture mapping and polygon rendering and GLSL shaders.
1995 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1996 GLsizei width
, GLsizei height
,
1997 GLint dstX
, GLint dstY
, GLenum type
)
1999 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2000 struct temp_texture
*tex
= get_temp_texture(ctx
);
2002 GLfloat x
, y
, z
, s
, t
;
2004 struct vertex verts
[4];
2006 GLenum intFormat
= GL_RGBA
;
2008 if (type
!= GL_COLOR
||
2009 ctx
->_ImageTransferState
||
2011 width
> tex
->MaxSize
||
2012 height
> tex
->MaxSize
) {
2013 /* XXX avoid this fallback */
2014 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2018 /* Most GL state applies to glCopyPixels, but a there's a few things
2019 * we need to override:
2021 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2024 MESA_META_TRANSFORM
|
2027 MESA_META_VIEWPORT
));
2029 if (copypix
->ArrayObj
== 0) {
2030 /* one-time setup */
2032 /* create vertex array object */
2033 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2034 _mesa_BindVertexArray(copypix
->ArrayObj
);
2036 /* create vertex array buffer */
2037 _mesa_GenBuffersARB(1, ©pix
->VBO
);
2038 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2039 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2040 NULL
, GL_DYNAMIC_DRAW_ARB
);
2042 /* setup vertex arrays */
2043 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2044 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2045 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2046 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2049 _mesa_BindVertexArray(copypix
->ArrayObj
);
2050 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2053 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2055 /* vertex positions, texcoords (after texture allocation!) */
2057 const GLfloat dstX0
= (GLfloat
) dstX
;
2058 const GLfloat dstY0
= (GLfloat
) dstY
;
2059 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2060 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2061 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2071 verts
[1].s
= tex
->Sright
;
2076 verts
[2].s
= tex
->Sright
;
2077 verts
[2].t
= tex
->Ttop
;
2082 verts
[3].t
= tex
->Ttop
;
2084 /* upload new vertex data */
2085 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2088 /* Alloc/setup texture */
2089 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2090 GL_RGBA
, GL_NEAREST
);
2092 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2094 /* draw textured quad */
2095 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2097 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2099 _mesa_meta_end(ctx
);
2105 * When the glDrawPixels() image size is greater than the max rectangle
2106 * texture size we use this function to break the glDrawPixels() image
2107 * into tiles which fit into the max texture size.
2110 tiled_draw_pixels(struct gl_context
*ctx
,
2112 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2113 GLenum format
, GLenum type
,
2114 const struct gl_pixelstore_attrib
*unpack
,
2115 const GLvoid
*pixels
)
2117 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2120 if (tileUnpack
.RowLength
== 0)
2121 tileUnpack
.RowLength
= width
;
2123 for (i
= 0; i
< width
; i
+= tileSize
) {
2124 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2125 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2127 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2129 for (j
= 0; j
< height
; j
+= tileSize
) {
2130 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2131 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2133 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2135 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2136 format
, type
, &tileUnpack
, pixels
);
2143 * One-time init for drawing stencil pixels.
2146 init_draw_stencil_pixels(struct gl_context
*ctx
)
2148 /* This program is run eight times, once for each stencil bit.
2149 * The stencil values to draw are found in an 8-bit alpha texture.
2150 * We read the texture/stencil value and test if bit 'b' is set.
2151 * If the bit is not set, use KIL to kill the fragment.
2152 * Finally, we use the stencil test to update the stencil buffer.
2154 * The basic algorithm for checking if a bit is set is:
2155 * if (is_odd(value / (1 << bit)))
2156 * result is one (or non-zero).
2159 * The program parameter contains three values:
2160 * parm.x = 255 / (1 << bit)
2164 static const char *program
=
2166 "PARAM parm = program.local[0]; \n"
2168 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2169 "# t = t * 255 / bit \n"
2170 "MUL t.x, t.a, parm.x; \n"
2173 "SUB t.x, t.x, t.y; \n"
2175 "MUL t.x, t.x, parm.y; \n"
2176 "# t = fract(t.x) \n"
2177 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2178 "# t.x = (t.x == 0 ? 1 : 0) \n"
2179 "SGE t.x, -t.x, parm.z; \n"
2181 "# for debug only \n"
2182 "#MOV result.color, t.x; \n"
2184 char program2
[1000];
2185 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2186 struct temp_texture
*tex
= get_temp_texture(ctx
);
2187 const char *texTarget
;
2189 assert(drawpix
->StencilFP
== 0);
2191 /* replace %s with "RECT" or "2D" */
2192 assert(strlen(program
) + 4 < sizeof(program2
));
2193 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2197 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2199 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2200 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2201 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2202 strlen(program2
), (const GLubyte
*) program2
);
2207 * One-time init for drawing depth pixels.
2210 init_draw_depth_pixels(struct gl_context
*ctx
)
2212 static const char *program
=
2214 "PARAM color = program.local[0]; \n"
2215 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2216 "MOV result.color, color; \n"
2219 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2220 struct temp_texture
*tex
= get_temp_texture(ctx
);
2221 const char *texTarget
;
2223 assert(drawpix
->DepthFP
== 0);
2225 /* replace %s with "RECT" or "2D" */
2226 assert(strlen(program
) + 4 < sizeof(program2
));
2227 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2231 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2233 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2234 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2235 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2236 strlen(program2
), (const GLubyte
*) program2
);
2241 * Meta implementation of ctx->Driver.DrawPixels() in terms
2242 * of texture mapping and polygon rendering.
2245 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2246 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2247 GLenum format
, GLenum type
,
2248 const struct gl_pixelstore_attrib
*unpack
,
2249 const GLvoid
*pixels
)
2251 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2252 struct temp_texture
*tex
= get_temp_texture(ctx
);
2253 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2254 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2256 GLfloat x
, y
, z
, s
, t
;
2258 struct vertex verts
[4];
2259 GLenum texIntFormat
;
2260 GLboolean fallback
, newTex
;
2261 GLbitfield metaExtraSave
= 0x0;
2265 * Determine if we can do the glDrawPixels with texture mapping.
2267 fallback
= GL_FALSE
;
2268 if (ctx
->_ImageTransferState
||
2273 if (_mesa_is_color_format(format
)) {
2274 /* use more compact format when possible */
2275 /* XXX disable special case for GL_LUMINANCE for now to work around
2276 * apparent i965 driver bug (see bug #23670).
2278 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2279 texIntFormat
= format
;
2281 texIntFormat
= GL_RGBA
;
2283 /* If we're not supposed to clamp the resulting color, then just
2284 * promote our texture to fully float. We could do better by
2285 * just going for the matching set of channels, in floating
2288 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2289 ctx
->Extensions
.ARB_texture_float
)
2290 texIntFormat
= GL_RGBA32F
;
2292 else if (_mesa_is_stencil_format(format
)) {
2293 if (ctx
->Extensions
.ARB_fragment_program
&&
2294 ctx
->Pixel
.IndexShift
== 0 &&
2295 ctx
->Pixel
.IndexOffset
== 0 &&
2296 type
== GL_UNSIGNED_BYTE
) {
2297 /* We'll store stencil as alpha. This only works for GLubyte
2298 * image data because of how incoming values are mapped to alpha
2301 texIntFormat
= GL_ALPHA
;
2302 metaExtraSave
= (MESA_META_COLOR_MASK
|
2303 MESA_META_DEPTH_TEST
|
2305 MESA_META_STENCIL_TEST
);
2311 else if (_mesa_is_depth_format(format
)) {
2312 if (ctx
->Extensions
.ARB_depth_texture
&&
2313 ctx
->Extensions
.ARB_fragment_program
) {
2314 texIntFormat
= GL_DEPTH_COMPONENT
;
2315 metaExtraSave
= (MESA_META_SHADER
);
2326 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2327 format
, type
, unpack
, pixels
);
2332 * Check image size against max texture size, draw as tiles if needed.
2334 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2335 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2336 format
, type
, unpack
, pixels
);
2340 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2341 * but a there's a few things we need to override:
2343 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2346 MESA_META_TRANSFORM
|
2349 MESA_META_VIEWPORT
|
2352 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2354 /* vertex positions, texcoords (after texture allocation!) */
2356 const GLfloat x0
= (GLfloat
) x
;
2357 const GLfloat y0
= (GLfloat
) y
;
2358 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2359 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2360 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2370 verts
[1].s
= tex
->Sright
;
2375 verts
[2].s
= tex
->Sright
;
2376 verts
[2].t
= tex
->Ttop
;
2381 verts
[3].t
= tex
->Ttop
;
2384 if (drawpix
->ArrayObj
== 0) {
2385 /* one-time setup: create vertex array object */
2386 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2388 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2390 /* create vertex array buffer */
2391 _mesa_GenBuffersARB(1, &vbo
);
2392 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2393 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2394 verts
, GL_DYNAMIC_DRAW_ARB
);
2396 /* setup vertex arrays */
2397 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2398 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2399 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2400 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2402 /* set given unpack params */
2403 ctx
->Unpack
= *unpack
;
2405 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2407 if (_mesa_is_stencil_format(format
)) {
2408 /* Drawing stencil */
2411 if (!drawpix
->StencilFP
)
2412 init_draw_stencil_pixels(ctx
);
2414 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2415 GL_ALPHA
, type
, pixels
);
2417 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2419 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2421 /* set all stencil bits to 0 */
2422 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2423 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2424 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2426 /* set stencil bits to 1 where needed */
2427 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2429 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2430 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2432 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2433 const GLuint mask
= 1 << bit
;
2434 if (mask
& origStencilMask
) {
2435 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2436 _mesa_StencilMask(mask
);
2438 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2439 255.0 / mask
, 0.5, 0.0, 0.0);
2441 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2445 else if (_mesa_is_depth_format(format
)) {
2447 if (!drawpix
->DepthFP
)
2448 init_draw_depth_pixels(ctx
);
2450 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2451 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2453 /* polygon color = current raster color */
2454 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2455 ctx
->Current
.RasterColor
);
2457 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2458 format
, type
, pixels
);
2460 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2464 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2465 format
, type
, pixels
);
2466 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2469 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2471 _mesa_DeleteBuffersARB(1, &vbo
);
2473 /* restore unpack params */
2474 ctx
->Unpack
= unpackSave
;
2476 _mesa_meta_end(ctx
);
2480 alpha_test_raster_color(struct gl_context
*ctx
)
2482 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2483 GLfloat ref
= ctx
->Color
.AlphaRef
;
2485 switch (ctx
->Color
.AlphaFunc
) {
2491 return alpha
== ref
;
2493 return alpha
<= ref
;
2497 return alpha
!= ref
;
2499 return alpha
>= ref
;
2509 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2510 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2511 * tracker would improve performance a lot.
2514 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2515 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2516 const struct gl_pixelstore_attrib
*unpack
,
2517 const GLubyte
*bitmap1
)
2519 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2520 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2521 const GLenum texIntFormat
= GL_ALPHA
;
2522 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2525 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2527 struct vertex verts
[4];
2532 * Check if swrast fallback is needed.
2534 if (ctx
->_ImageTransferState
||
2535 ctx
->FragmentProgram
._Enabled
||
2537 ctx
->Texture
._EnabledUnits
||
2538 width
> tex
->MaxSize
||
2539 height
> tex
->MaxSize
) {
2540 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2544 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2547 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2548 * but a there's a few things we need to override:
2550 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2551 MESA_META_PIXEL_STORE
|
2552 MESA_META_RASTERIZATION
|
2555 MESA_META_TRANSFORM
|
2558 MESA_META_VIEWPORT
));
2560 if (bitmap
->ArrayObj
== 0) {
2561 /* one-time setup */
2563 /* create vertex array object */
2564 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2565 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2567 /* create vertex array buffer */
2568 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2569 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2570 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2571 NULL
, GL_DYNAMIC_DRAW_ARB
);
2573 /* setup vertex arrays */
2574 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2575 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2576 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2577 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2578 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2579 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2582 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2583 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2586 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2588 /* vertex positions, texcoords, colors (after texture allocation!) */
2590 const GLfloat x0
= (GLfloat
) x
;
2591 const GLfloat y0
= (GLfloat
) y
;
2592 const GLfloat x1
= (GLfloat
) (x
+ width
);
2593 const GLfloat y1
= (GLfloat
) (y
+ height
);
2594 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2605 verts
[1].s
= tex
->Sright
;
2610 verts
[2].s
= tex
->Sright
;
2611 verts
[2].t
= tex
->Ttop
;
2616 verts
[3].t
= tex
->Ttop
;
2618 for (i
= 0; i
< 4; i
++) {
2619 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2620 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2621 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2622 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2625 /* upload new vertex data */
2626 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2629 /* choose different foreground/background alpha values */
2630 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2631 bg
= (fg
> 127 ? 0 : 255);
2633 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2635 _mesa_meta_end(ctx
);
2639 bitmap8
= (GLubyte
*) malloc(width
* height
);
2641 memset(bitmap8
, bg
, width
* height
);
2642 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2643 bitmap8
, width
, fg
);
2645 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2647 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2648 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2650 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2651 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2653 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2655 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2660 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2662 _mesa_meta_end(ctx
);
2667 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2668 * software fallback. The fallback path will require that the texture
2669 * images are mapped.
2670 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2673 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2674 struct gl_texture_object
*texObj
)
2676 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2677 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2678 struct gl_texture_image
*baseImage
;
2682 /* check for fallbacks */
2683 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2684 target
== GL_TEXTURE_3D
||
2685 target
== GL_TEXTURE_1D_ARRAY
||
2686 target
== GL_TEXTURE_2D_ARRAY
) {
2690 srcLevel
= texObj
->BaseLevel
;
2691 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2692 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2696 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2697 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2698 /* The texture format is sRGB but we can't turn off sRGB->linear
2699 * texture sample conversion. So we won't be able to generate the
2700 * right colors when rendering. Need to use a fallback.
2706 * Test that we can actually render in the texture's format.
2709 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2710 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2712 if (target
== GL_TEXTURE_1D
) {
2713 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2714 GL_COLOR_ATTACHMENT0_EXT
,
2715 target
, texObj
->Name
, srcLevel
);
2718 /* other work is needed to enable 3D mipmap generation */
2719 else if (target
== GL_TEXTURE_3D
) {
2721 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2722 GL_COLOR_ATTACHMENT0_EXT
,
2723 target
, texObj
->Name
, srcLevel
, zoffset
);
2728 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2729 GL_COLOR_ATTACHMENT0_EXT
,
2730 target
, texObj
->Name
, srcLevel
);
2733 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2735 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2737 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2746 * Compute the texture coordinates for the four vertices of a quad for
2747 * drawing a 2D texture image or slice of a cube/3D texture.
2748 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2749 * \param slice slice of a 1D/2D array texture or 3D texture
2750 * \param width width of the texture image
2751 * \param height height of the texture image
2752 * \param coords0/1/2/3 returns the computed texcoords
2755 setup_texture_coords(GLenum faceTarget
,
2764 static const GLfloat st
[4][2] = {
2765 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2770 switch (faceTarget
) {
2774 case GL_TEXTURE_2D_ARRAY
:
2775 if (faceTarget
== GL_TEXTURE_3D
)
2777 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2781 coords0
[0] = 0.0F
; /* s */
2782 coords0
[1] = 0.0F
; /* t */
2783 coords0
[2] = r
; /* r */
2794 case GL_TEXTURE_RECTANGLE_ARB
:
2795 coords0
[0] = 0.0F
; /* s */
2796 coords0
[1] = 0.0F
; /* t */
2797 coords0
[2] = 0.0F
; /* r */
2802 coords2
[1] = height
;
2805 coords3
[1] = height
;
2808 case GL_TEXTURE_1D_ARRAY
:
2809 coords0
[0] = 0.0F
; /* s */
2810 coords0
[1] = slice
; /* t */
2811 coords0
[2] = 0.0F
; /* r */
2823 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2824 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2825 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2826 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2827 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2828 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2829 /* loop over quad verts */
2830 for (i
= 0; i
< 4; i
++) {
2831 /* Compute sc = +/-scale and tc = +/-scale.
2832 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2833 * though that can still sometimes happen with this scale factor...
2835 const GLfloat scale
= 0.9999f
;
2836 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2837 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2857 switch (faceTarget
) {
2858 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2863 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2868 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2873 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2878 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2883 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2894 assert(0 && "unexpected target in meta setup_texture_coords()");
2900 * Called via ctx->Driver.GenerateMipmap()
2901 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2905 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2906 struct gl_texture_object
*texObj
)
2908 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2910 GLfloat x
, y
, tex
[3];
2912 struct vertex verts
[4];
2913 const GLuint baseLevel
= texObj
->BaseLevel
;
2914 const GLuint maxLevel
= texObj
->MaxLevel
;
2915 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2916 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2917 const GLint maxLevelSave
= texObj
->MaxLevel
;
2918 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2919 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2920 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2921 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2922 const GLenum srgbDecodeSave
= texObj
->Sampler
.sRGBDecode
;
2923 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2924 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2925 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2928 const GLuint border
= 0;
2929 const GLint slice
= 0;
2931 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2932 _mesa_generate_mipmap(ctx
, target
, texObj
);
2936 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2937 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2938 faceTarget
= target
;
2939 target
= GL_TEXTURE_CUBE_MAP
;
2942 faceTarget
= target
;
2945 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2947 if (original_active_unit
!= 0)
2948 _mesa_BindTexture(target
, texObj
->Name
);
2950 if (mipmap
->ArrayObj
== 0) {
2951 /* one-time setup */
2953 /* create vertex array object */
2954 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2955 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2957 /* create vertex array buffer */
2958 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2959 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2960 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2961 NULL
, GL_DYNAMIC_DRAW_ARB
);
2963 /* setup vertex arrays */
2964 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2965 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2966 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2967 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2970 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2971 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2975 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2977 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2979 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2980 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2981 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2982 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2983 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2984 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2986 /* We don't want to encode or decode sRGB values; treat them as linear */
2987 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2988 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
2989 GL_SKIP_DECODE_EXT
);
2991 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2992 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
2995 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2997 /* setup texcoords (XXX what about border?) */
2998 setup_texture_coords(faceTarget
,
3000 0, 0, /* width, height never used here */
3006 /* setup vertex positions */
3016 /* upload new vertex data */
3017 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3019 /* setup projection matrix */
3020 _mesa_MatrixMode(GL_PROJECTION
);
3021 _mesa_LoadIdentity();
3022 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
3024 /* texture is already locked, unlock now */
3025 _mesa_unlock_texture(ctx
, texObj
);
3027 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3028 const struct gl_texture_image
*srcImage
;
3029 const GLuint srcLevel
= dstLevel
- 1;
3030 GLsizei srcWidth
, srcHeight
, srcDepth
;
3031 GLsizei dstWidth
, dstHeight
, dstDepth
;
3034 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3035 assert(srcImage
->Border
== 0); /* XXX we can fix this */
3037 /* src size w/out border */
3038 srcWidth
= srcImage
->Width
- 2 * border
;
3039 srcHeight
= srcImage
->Height
- 2 * border
;
3040 srcDepth
= srcImage
->Depth
- 2 * border
;
3042 /* new dst size w/ border */
3043 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
3044 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
3045 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
3047 if (dstWidth
== srcImage
->Width
&&
3048 dstHeight
== srcImage
->Height
&&
3049 dstDepth
== srcImage
->Depth
) {
3054 /* Allocate storage for the destination mipmap image(s) */
3056 /* Set MaxLevel large enough to hold the new level when we allocate it */
3057 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3059 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3060 dstWidth
, dstHeight
, dstDepth
,
3062 srcImage
->InternalFormat
,
3063 srcImage
->TexFormat
)) {
3064 /* All done. We either ran out of memory or we would go beyond the
3065 * last valid level of an immutable texture if we continued.
3070 /* limit minification to src level */
3071 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3073 /* Set to draw into the current dstLevel */
3074 if (target
== GL_TEXTURE_1D
) {
3075 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
3076 GL_COLOR_ATTACHMENT0_EXT
,
3081 else if (target
== GL_TEXTURE_3D
) {
3082 GLint zoffset
= 0; /* XXX unfinished */
3083 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
3084 GL_COLOR_ATTACHMENT0_EXT
,
3091 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
3092 GL_COLOR_ATTACHMENT0_EXT
,
3098 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3101 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3102 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3103 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3104 "_mesa_meta_GenerateMipmap()");
3108 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3109 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3111 /* setup viewport */
3112 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3114 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3117 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3118 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3121 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3122 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3125 _mesa_lock_texture(ctx
, texObj
); /* relock */
3127 _mesa_meta_end(ctx
);
3129 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3130 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3131 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3132 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3133 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3134 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3135 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
3137 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3142 * Determine the GL data type to use for the temporary image read with
3143 * ReadPixels() and passed to Tex[Sub]Image().
3146 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
3148 switch (baseFormat
) {
3155 case GL_LUMINANCE_ALPHA
:
3157 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3158 return GL_UNSIGNED_BYTE
;
3159 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3160 return GL_UNSIGNED_SHORT
;
3163 case GL_DEPTH_COMPONENT
:
3164 return GL_UNSIGNED_INT
;
3165 case GL_DEPTH_STENCIL
:
3166 return GL_UNSIGNED_INT_24_8
;
3168 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3176 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3177 * Have to be careful with locking and meta state for pixel transfer.
3180 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3181 struct gl_texture_image
*texImage
,
3182 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3183 struct gl_renderbuffer
*rb
,
3185 GLsizei width
, GLsizei height
)
3187 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3188 GLenum format
, type
;
3192 /* Choose format/type for temporary image buffer */
3193 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3194 if (format
== GL_LUMINANCE
||
3195 format
== GL_LUMINANCE_ALPHA
||
3196 format
== GL_INTENSITY
) {
3197 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3198 * temp image buffer because glReadPixels will do L=R+G+B which is
3199 * not what we want (should be L=R).
3204 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3205 _mesa_problem(ctx
, "unsupported integer color copyteximage");
3209 type
= get_temp_image_type(ctx
, format
);
3210 bpp
= _mesa_bytes_per_pixel(format
, type
);
3212 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3217 * Alloc image buffer (XXX could use a PBO)
3219 buf
= malloc(width
* height
* bpp
);
3221 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3225 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3228 * Read image from framebuffer (disable pixel transfer ops)
3230 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3231 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3232 format
, type
, &ctx
->Pack
, buf
);
3233 _mesa_meta_end(ctx
);
3235 _mesa_update_state(ctx
); /* to update pixel transfer state */
3238 * Store texture data (with pixel transfer ops)
3240 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3242 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3243 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3244 format
, type
, buf
, &ctx
->Unpack
);
3246 _mesa_meta_end(ctx
);
3248 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3255 * Decompress a texture image by drawing a quad with the compressed
3256 * texture and reading the pixels out of the color buffer.
3257 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3258 * \param destFormat format, ala glReadPixels
3259 * \param destType type, ala glReadPixels
3260 * \param dest destination buffer
3261 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3264 decompress_texture_image(struct gl_context
*ctx
,
3265 struct gl_texture_image
*texImage
,
3267 GLenum destFormat
, GLenum destType
,
3270 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3271 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3272 const GLint width
= texImage
->Width
;
3273 const GLint height
= texImage
->Height
;
3274 const GLenum target
= texObj
->Target
;
3277 GLfloat x
, y
, tex
[3];
3279 struct vertex verts
[4];
3280 GLuint fboDrawSave
, fboReadSave
;
3284 assert(target
== GL_TEXTURE_3D
||
3285 target
== GL_TEXTURE_2D_ARRAY
);
3288 if (target
== GL_TEXTURE_CUBE_MAP
) {
3289 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3292 faceTarget
= target
;
3295 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3296 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3297 fboReadSave
= ctx
->ReadBuffer
->Name
;
3298 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3300 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3302 /* Create/bind FBO/renderbuffer */
3303 if (decompress
->FBO
== 0) {
3304 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3305 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3306 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3307 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3308 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3309 GL_COLOR_ATTACHMENT0_EXT
,
3310 GL_RENDERBUFFER_EXT
,
3314 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3317 /* alloc dest surface */
3318 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3319 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3320 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3322 decompress
->Width
= width
;
3323 decompress
->Height
= height
;
3326 /* setup VBO data */
3327 if (decompress
->ArrayObj
== 0) {
3328 /* create vertex array object */
3329 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3330 _mesa_BindVertexArray(decompress
->ArrayObj
);
3332 /* create vertex array buffer */
3333 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3334 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3335 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3336 NULL
, GL_DYNAMIC_DRAW_ARB
);
3338 /* setup vertex arrays */
3339 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3340 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3341 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3342 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3345 _mesa_BindVertexArray(decompress
->ArrayObj
);
3346 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3349 setup_texture_coords(faceTarget
, slice
, width
, height
,
3355 /* setup vertex positions */
3361 verts
[2].y
= height
;
3363 verts
[3].y
= height
;
3365 /* upload new vertex data */
3366 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3368 /* setup texture state */
3369 _mesa_BindTexture(target
, texObj
->Name
);
3370 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3373 /* save texture object state */
3374 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3375 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3376 const GLint baseLevelSave
= texObj
->BaseLevel
;
3377 const GLint maxLevelSave
= texObj
->MaxLevel
;
3378 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3379 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3380 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3382 /* restrict sampling to the texture level of interest */
3383 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3384 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3385 /* nearest filtering */
3386 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3387 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3389 /* No sRGB decode or encode.*/
3390 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3391 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3392 GL_SKIP_DECODE_EXT
);
3394 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3395 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3398 /* render quad w/ texture into renderbuffer */
3399 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3401 /* Restore texture object state, the texture binding will
3402 * be restored by _mesa_meta_end().
3404 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3405 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3406 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3407 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3408 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3410 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3411 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3412 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3413 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3417 /* read pixels from renderbuffer */
3419 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3421 /* The pixel transfer state will be set to default values at this point
3422 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3423 * turned off (as required by glGetTexImage) but we need to handle some
3424 * special cases. In particular, single-channel texture values are
3425 * returned as red and two-channel texture values are returned as
3428 if (baseTexFormat
== GL_LUMINANCE
||
3429 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3430 baseTexFormat
== GL_INTENSITY
) {
3431 /* Green and blue must be zero */
3432 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3433 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3436 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3439 /* disable texture unit */
3440 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3442 _mesa_meta_end(ctx
);
3444 /* restore fbo bindings */
3445 if (fboDrawSave
== fboReadSave
) {
3446 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3449 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3450 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3452 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, rbSave
);
3457 * This is just a wrapper around _mesa_get_tex_image() and
3458 * decompress_texture_image(). Meta functions should not be directly called
3462 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3463 GLenum format
, GLenum type
, GLvoid
*pixels
,
3464 struct gl_texture_image
*texImage
)
3466 /* We can only use the decompress-with-blit method here if the texels are
3467 * unsigned, normalized values. We could handle signed and unnormalized
3468 * with floating point renderbuffers...
3470 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3471 _mesa_get_format_datatype(texImage
->TexFormat
)
3472 == GL_UNSIGNED_NORMALIZED
) {
3473 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3474 const GLuint slice
= 0; /* only 2D compressed textures for now */
3475 /* Need to unlock the texture here to prevent deadlock... */
3476 _mesa_unlock_texture(ctx
, texObj
);
3477 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3478 /* ... and relock it */
3479 _mesa_lock_texture(ctx
, texObj
);
3482 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3488 * Meta implementation of ctx->Driver.DrawTex() in terms
3489 * of polygon rendering.
3492 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3493 GLfloat width
, GLfloat height
)
3495 #if FEATURE_OES_draw_texture
3496 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3498 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3500 struct vertex verts
[4];
3503 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3505 MESA_META_TRANSFORM
|
3507 MESA_META_VIEWPORT
));
3509 if (drawtex
->ArrayObj
== 0) {
3510 /* one-time setup */
3511 GLint active_texture
;
3513 /* create vertex array object */
3514 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3515 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3517 /* create vertex array buffer */
3518 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3519 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3520 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3521 NULL
, GL_DYNAMIC_DRAW_ARB
);
3523 /* client active texture is not part of the array object */
3524 active_texture
= ctx
->Array
.ActiveTexture
;
3526 /* setup vertex arrays */
3527 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3528 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3529 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3530 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3531 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3532 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3535 /* restore client active texture */
3536 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3539 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3540 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3543 /* vertex positions, texcoords */
3545 const GLfloat x1
= x
+ width
;
3546 const GLfloat y1
= y
+ height
;
3548 z
= CLAMP(z
, 0.0, 1.0);
3567 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3568 const struct gl_texture_object
*texObj
;
3569 const struct gl_texture_image
*texImage
;
3570 GLfloat s
, t
, s1
, t1
;
3573 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3575 for (j
= 0; j
< 4; j
++) {
3576 verts
[j
].st
[i
][0] = 0.0f
;
3577 verts
[j
].st
[i
][1] = 0.0f
;
3582 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3583 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3584 tw
= texImage
->Width2
;
3585 th
= texImage
->Height2
;
3587 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3588 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3589 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3590 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3592 verts
[0].st
[i
][0] = s
;
3593 verts
[0].st
[i
][1] = t
;
3595 verts
[1].st
[i
][0] = s1
;
3596 verts
[1].st
[i
][1] = t
;
3598 verts
[2].st
[i
][0] = s1
;
3599 verts
[2].st
[i
][1] = t1
;
3601 verts
[3].st
[i
][0] = s
;
3602 verts
[3].st
[i
][1] = t1
;
3605 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3608 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3610 _mesa_meta_end(ctx
);
3611 #endif /* FEATURE_OES_draw_texture */