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/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/readpix.h"
58 #include "main/scissor.h"
59 #include "main/shaderapi.h"
60 #include "main/shaderobj.h"
61 #include "main/state.h"
62 #include "main/stencil.h"
63 #include "main/texobj.h"
64 #include "main/texenv.h"
65 #include "main/texgetimage.h"
66 #include "main/teximage.h"
67 #include "main/texparam.h"
68 #include "main/texstate.h"
69 #include "main/transformfeedback.h"
70 #include "main/uniforms.h"
71 #include "main/varray.h"
72 #include "main/viewport.h"
73 #include "main/samplerobj.h"
74 #include "program/program.h"
75 #include "swrast/swrast.h"
76 #include "drivers/common/meta.h"
77 #include "main/enums.h"
78 #include "main/glformats.h"
79 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_ALPHA_TEST */
94 GLboolean AlphaEnabled
;
98 /** MESA_META_BLEND */
99 GLbitfield BlendEnabled
;
100 GLboolean ColorLogicOpEnabled
;
102 /** MESA_META_COLOR_MASK */
103 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
105 /** MESA_META_DEPTH_TEST */
106 struct gl_depthbuffer_attrib Depth
;
111 /** MESA_META_PIXEL_STORE */
112 struct gl_pixelstore_attrib Pack
, Unpack
;
114 /** MESA_META_PIXEL_TRANSFER */
115 GLfloat RedBias
, RedScale
;
116 GLfloat GreenBias
, GreenScale
;
117 GLfloat BlueBias
, BlueScale
;
118 GLfloat AlphaBias
, AlphaScale
;
119 GLfloat DepthBias
, DepthScale
;
120 GLboolean MapColorFlag
;
122 /** MESA_META_RASTERIZATION */
123 GLenum FrontPolygonMode
, BackPolygonMode
;
124 GLboolean PolygonOffset
;
125 GLboolean PolygonSmooth
;
126 GLboolean PolygonStipple
;
127 GLboolean PolygonCull
;
129 /** MESA_META_SCISSOR */
130 struct gl_scissor_attrib Scissor
;
132 /** MESA_META_SHADER */
133 GLboolean VertexProgramEnabled
;
134 struct gl_vertex_program
*VertexProgram
;
135 GLboolean FragmentProgramEnabled
;
136 struct gl_fragment_program
*FragmentProgram
;
137 struct gl_shader_program
*VertexShader
;
138 struct gl_shader_program
*GeometryShader
;
139 struct gl_shader_program
*FragmentShader
;
140 struct gl_shader_program
*ActiveShader
;
142 /** MESA_META_STENCIL_TEST */
143 struct gl_stencil_attrib Stencil
;
145 /** MESA_META_TRANSFORM */
147 GLfloat ModelviewMatrix
[16];
148 GLfloat ProjectionMatrix
[16];
149 GLfloat TextureMatrix
[16];
151 /** MESA_META_CLIP */
152 GLbitfield ClipPlanesEnabled
;
154 /** MESA_META_TEXTURE */
156 GLuint ClientActiveUnit
;
157 /** for unit[0] only */
158 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
159 /** mask of TEXTURE_2D_BIT, etc */
160 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
161 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
162 GLuint EnvMode
; /* unit[0] only */
164 /** MESA_META_VERTEX */
165 struct gl_array_object
*ArrayObj
;
166 struct gl_buffer_object
*ArrayBufferObj
;
168 /** MESA_META_VIEWPORT */
169 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
170 GLclampd DepthNear
, DepthFar
;
172 /** MESA_META_CLAMP_FRAGMENT_COLOR */
173 GLenum ClampFragmentColor
;
175 /** MESA_META_CLAMP_VERTEX_COLOR */
176 GLenum ClampVertexColor
;
178 /** MESA_META_CONDITIONAL_RENDER */
179 struct gl_query_object
*CondRenderQuery
;
180 GLenum CondRenderMode
;
182 /** MESA_META_SELECT_FEEDBACK */
184 struct gl_selection Select
;
185 struct gl_feedback Feedback
;
187 /** MESA_META_MULTISAMPLE */
188 GLboolean MultisampleEnabled
;
190 /** Miscellaneous (always disabled) */
192 GLboolean RasterDiscard
;
193 GLboolean TransformFeedbackNeedsResume
;
197 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
198 * This is currently shared by all the meta ops. But we could create a
199 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
204 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
205 GLsizei MinSize
; /**< Min texture size to allocate */
206 GLsizei MaxSize
; /**< Max possible texture size */
207 GLboolean NPOT
; /**< Non-power of two size OK? */
208 GLsizei Width
, Height
; /**< Current texture size */
210 GLfloat Sright
, Ttop
; /**< right, top texcoords */
215 * State for glBlitFramebufer()
226 * State for glClear()
235 GLuint IntegerShaderProg
;
236 GLint IntegerColorLocation
;
241 * State for glCopyPixels()
251 * State for glDrawPixels()
257 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
258 GLuint DepthFP
; /**< Fragment program for drawing depth images */
263 * State for glBitmap()
269 struct temp_texture Tex
; /**< separate texture from other meta ops */
274 * State for _mesa_meta_generate_mipmap()
276 struct gen_mipmap_state
283 GLuint IntegerShaderProg
;
287 * State for GLSL texture sampler which is used to generate fragment
288 * shader in _mesa_meta_generate_mipmap().
290 struct glsl_sampler
{
293 const char *texcoords
;
297 * State for texture decompression
299 struct decompress_state
302 GLuint VBO
, FBO
, RBO
, Sampler
;
307 * State for glDrawTex()
315 #define MAX_META_OPS_DEPTH 8
317 * All per-context meta state.
321 /** Stack of state saved during meta-ops */
322 struct save_state Save
[MAX_META_OPS_DEPTH
];
323 /** Save stack depth */
324 GLuint SaveStackDepth
;
326 struct temp_texture TempTex
;
328 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
329 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
330 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
331 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
332 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
333 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
334 struct decompress_state Decompress
; /**< For texture decompression */
335 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
338 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
339 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
340 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
341 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
342 struct gen_mipmap_state
*mipmap
);
345 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
351 shader
= _mesa_CreateShaderObjectARB(target
);
352 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
353 _mesa_CompileShaderARB(shader
);
355 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
359 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
361 _mesa_DeleteObjectARB(shader
);
367 _mesa_DeleteObjectARB(shader
);
371 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
373 "meta program compile failed:\n%s\n"
378 _mesa_DeleteObjectARB(shader
);
384 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
389 _mesa_LinkProgramARB(program
);
391 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
395 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
403 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
404 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
412 * Initialize meta-ops for a context.
413 * To be called once during context creation.
416 _mesa_meta_init(struct gl_context
*ctx
)
420 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
425 * Free context meta-op state.
426 * To be called once during context destruction.
429 _mesa_meta_free(struct gl_context
*ctx
)
431 GET_CURRENT_CONTEXT(old_context
);
432 _mesa_make_current(ctx
, NULL
, NULL
);
433 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
434 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
435 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
436 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
438 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
440 _mesa_make_current(NULL
, NULL
, NULL
);
447 * Enter meta state. This is like a light-weight version of glPushAttrib
448 * but it also resets most GL state back to default values.
450 * \param state bitmask of MESA_META_* flags indicating which attribute groups
451 * to save and reset to their defaults
454 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
456 struct save_state
*save
;
458 /* hope MAX_META_OPS_DEPTH is large enough */
459 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
461 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
462 memset(save
, 0, sizeof(*save
));
463 save
->SavedState
= state
;
465 /* Pausing transform feedback needs to be done early, or else we won't be
466 * able to change other state.
468 save
->TransformFeedbackNeedsResume
=
469 ctx
->TransformFeedback
.CurrentObject
->Active
&&
470 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
471 if (save
->TransformFeedbackNeedsResume
)
472 _mesa_PauseTransformFeedback();
474 if (state
& MESA_META_ALPHA_TEST
) {
475 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
476 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
477 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
478 if (ctx
->Color
.AlphaEnabled
)
479 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
482 if (state
& MESA_META_BLEND
) {
483 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
484 if (ctx
->Color
.BlendEnabled
) {
485 if (ctx
->Extensions
.EXT_draw_buffers2
) {
487 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
488 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
492 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
495 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
496 if (ctx
->Color
.ColorLogicOpEnabled
)
497 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
500 if (state
& MESA_META_COLOR_MASK
) {
501 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
502 sizeof(ctx
->Color
.ColorMask
));
503 if (!ctx
->Color
.ColorMask
[0][0] ||
504 !ctx
->Color
.ColorMask
[0][1] ||
505 !ctx
->Color
.ColorMask
[0][2] ||
506 !ctx
->Color
.ColorMask
[0][3])
507 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
510 if (state
& MESA_META_DEPTH_TEST
) {
511 save
->Depth
= ctx
->Depth
; /* struct copy */
513 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
516 if ((state
& MESA_META_FOG
)
517 && ctx
->API
!= API_OPENGL_CORE
518 && ctx
->API
!= API_OPENGLES2
) {
519 save
->Fog
= ctx
->Fog
.Enabled
;
520 if (ctx
->Fog
.Enabled
)
521 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
524 if (state
& MESA_META_PIXEL_STORE
) {
525 save
->Pack
= ctx
->Pack
;
526 save
->Unpack
= ctx
->Unpack
;
527 ctx
->Pack
= ctx
->DefaultPacking
;
528 ctx
->Unpack
= ctx
->DefaultPacking
;
531 if (state
& MESA_META_PIXEL_TRANSFER
) {
532 save
->RedScale
= ctx
->Pixel
.RedScale
;
533 save
->RedBias
= ctx
->Pixel
.RedBias
;
534 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
535 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
536 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
537 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
538 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
539 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
540 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
541 ctx
->Pixel
.RedScale
= 1.0F
;
542 ctx
->Pixel
.RedBias
= 0.0F
;
543 ctx
->Pixel
.GreenScale
= 1.0F
;
544 ctx
->Pixel
.GreenBias
= 0.0F
;
545 ctx
->Pixel
.BlueScale
= 1.0F
;
546 ctx
->Pixel
.BlueBias
= 0.0F
;
547 ctx
->Pixel
.AlphaScale
= 1.0F
;
548 ctx
->Pixel
.AlphaBias
= 0.0F
;
549 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
551 ctx
->NewState
|=_NEW_PIXEL
;
554 if (state
& MESA_META_RASTERIZATION
) {
555 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
556 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
557 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
558 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
559 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
560 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
561 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
562 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
563 if (ctx
->API
== API_OPENGL
) {
564 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
565 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
567 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
570 if (state
& MESA_META_SCISSOR
) {
571 save
->Scissor
= ctx
->Scissor
; /* struct copy */
572 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
575 if (state
& MESA_META_SHADER
) {
576 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_vertex_program
) {
577 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
578 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
579 ctx
->VertexProgram
.Current
);
580 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
583 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_fragment_program
) {
584 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
585 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
586 ctx
->FragmentProgram
.Current
);
587 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
590 if (ctx
->Extensions
.ARB_shader_objects
) {
591 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
592 ctx
->Shader
.CurrentVertexProgram
);
593 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
594 ctx
->Shader
.CurrentGeometryProgram
);
595 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
596 ctx
->Shader
.CurrentFragmentProgram
);
597 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
598 ctx
->Shader
.ActiveProgram
);
600 _mesa_UseProgramObjectARB(0);
604 if (state
& MESA_META_STENCIL_TEST
) {
605 save
->Stencil
= ctx
->Stencil
; /* struct copy */
606 if (ctx
->Stencil
.Enabled
)
607 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
608 /* NOTE: other stencil state not reset */
611 if (state
& MESA_META_TEXTURE
) {
614 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
615 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
616 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
618 /* Disable all texture units */
619 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
620 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
621 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
622 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
623 if (ctx
->Texture
.Unit
[u
].Enabled
||
624 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
625 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
626 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
627 if (ctx
->Extensions
.ARB_texture_cube_map
)
628 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
629 if (ctx
->Extensions
.OES_EGL_image_external
)
630 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
632 if (ctx
->API
== API_OPENGL
) {
633 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
634 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
635 if (ctx
->Extensions
.NV_texture_rectangle
)
636 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
637 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
638 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
639 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
640 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
642 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
648 /* save current texture objects for unit[0] only */
649 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
650 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
651 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
654 /* set defaults for unit[0] */
655 _mesa_ActiveTextureARB(GL_TEXTURE0
);
656 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
657 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
658 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
662 if (state
& MESA_META_TRANSFORM
) {
663 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
664 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
665 16 * sizeof(GLfloat
));
666 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
667 16 * sizeof(GLfloat
));
668 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
669 16 * sizeof(GLfloat
));
670 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
671 /* set 1:1 vertex:pixel coordinate transform */
672 _mesa_ActiveTextureARB(GL_TEXTURE0
);
673 _mesa_MatrixMode(GL_TEXTURE
);
674 _mesa_LoadIdentity();
675 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
676 _mesa_MatrixMode(GL_MODELVIEW
);
677 _mesa_LoadIdentity();
678 _mesa_MatrixMode(GL_PROJECTION
);
679 _mesa_LoadIdentity();
680 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
681 0.0, ctx
->DrawBuffer
->Height
,
685 if (state
& MESA_META_CLIP
) {
686 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
687 if (ctx
->Transform
.ClipPlanesEnabled
) {
689 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
690 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
695 if (state
& MESA_META_VERTEX
) {
696 /* save vertex array object state */
697 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
698 ctx
->Array
.ArrayObj
);
699 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
700 ctx
->Array
.ArrayBufferObj
);
701 /* set some default state? */
704 if (state
& MESA_META_VIEWPORT
) {
705 /* save viewport state */
706 save
->ViewportX
= ctx
->Viewport
.X
;
707 save
->ViewportY
= ctx
->Viewport
.Y
;
708 save
->ViewportW
= ctx
->Viewport
.Width
;
709 save
->ViewportH
= ctx
->Viewport
.Height
;
710 /* set viewport to match window size */
711 if (ctx
->Viewport
.X
!= 0 ||
712 ctx
->Viewport
.Y
!= 0 ||
713 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
714 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
715 _mesa_set_viewport(ctx
, 0, 0,
716 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
718 /* save depth range state */
719 save
->DepthNear
= ctx
->Viewport
.Near
;
720 save
->DepthFar
= ctx
->Viewport
.Far
;
721 /* set depth range to default */
722 _mesa_DepthRange(0.0, 1.0);
725 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
726 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
728 /* Generally in here we want to do clamping according to whether
729 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
730 * regardless of the internal implementation of the metaops.
732 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
733 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
736 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
737 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
739 /* Generally in here we never want vertex color clamping --
740 * result clamping is only dependent on fragment clamping.
742 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
745 if (state
& MESA_META_CONDITIONAL_RENDER
) {
746 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
747 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
749 if (ctx
->Query
.CondRenderQuery
)
750 _mesa_EndConditionalRender();
753 if (state
& MESA_META_SELECT_FEEDBACK
) {
754 save
->RenderMode
= ctx
->RenderMode
;
755 if (ctx
->RenderMode
== GL_SELECT
) {
756 save
->Select
= ctx
->Select
; /* struct copy */
757 _mesa_RenderMode(GL_RENDER
);
758 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
759 save
->Feedback
= ctx
->Feedback
; /* struct copy */
760 _mesa_RenderMode(GL_RENDER
);
764 if (state
& MESA_META_MULTISAMPLE
) {
765 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
766 if (ctx
->Multisample
.Enabled
)
767 _mesa_set_multisample(ctx
, GL_FALSE
);
772 save
->Lighting
= ctx
->Light
.Enabled
;
773 if (ctx
->Light
.Enabled
)
774 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
775 save
->RasterDiscard
= ctx
->RasterDiscard
;
776 if (ctx
->RasterDiscard
)
777 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
783 * Leave meta state. This is like a light-weight version of glPopAttrib().
786 _mesa_meta_end(struct gl_context
*ctx
)
788 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
789 const GLbitfield state
= save
->SavedState
;
791 if (state
& MESA_META_ALPHA_TEST
) {
792 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
793 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
794 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
797 if (state
& MESA_META_BLEND
) {
798 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
799 if (ctx
->Extensions
.EXT_draw_buffers2
) {
801 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
802 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
806 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
809 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
810 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
813 if (state
& MESA_META_COLOR_MASK
) {
815 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
816 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
818 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
819 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
822 _mesa_ColorMaskIndexed(i
,
823 save
->ColorMask
[i
][0],
824 save
->ColorMask
[i
][1],
825 save
->ColorMask
[i
][2],
826 save
->ColorMask
[i
][3]);
832 if (state
& MESA_META_DEPTH_TEST
) {
833 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
834 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
835 _mesa_DepthFunc(save
->Depth
.Func
);
836 _mesa_DepthMask(save
->Depth
.Mask
);
839 if ((state
& MESA_META_FOG
)
840 && ctx
->API
!= API_OPENGL_CORE
841 && ctx
->API
!= API_OPENGLES2
) {
842 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
845 if (state
& MESA_META_PIXEL_STORE
) {
846 ctx
->Pack
= save
->Pack
;
847 ctx
->Unpack
= save
->Unpack
;
850 if (state
& MESA_META_PIXEL_TRANSFER
) {
851 ctx
->Pixel
.RedScale
= save
->RedScale
;
852 ctx
->Pixel
.RedBias
= save
->RedBias
;
853 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
854 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
855 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
856 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
857 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
858 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
859 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
861 ctx
->NewState
|=_NEW_PIXEL
;
864 if (state
& MESA_META_RASTERIZATION
) {
865 /* Core context requires that front and back mode be the same.
867 if (ctx
->API
== API_OPENGL_CORE
) {
868 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
870 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
871 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
873 if (ctx
->API
== API_OPENGL
) {
874 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
875 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
877 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
878 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
881 if (state
& MESA_META_SCISSOR
) {
882 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
883 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
884 save
->Scissor
.Width
, save
->Scissor
.Height
);
887 if (state
& MESA_META_SHADER
) {
888 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_vertex_program
) {
889 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
890 save
->VertexProgramEnabled
);
891 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
892 save
->VertexProgram
);
893 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
896 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_fragment_program
) {
897 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
898 save
->FragmentProgramEnabled
);
899 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
900 save
->FragmentProgram
);
901 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
904 if (ctx
->Extensions
.ARB_vertex_shader
)
905 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
907 if (ctx
->Extensions
.ARB_geometry_shader4
)
908 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
909 save
->GeometryShader
);
911 if (ctx
->Extensions
.ARB_fragment_shader
)
912 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
913 save
->FragmentShader
);
915 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
918 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
919 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
920 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
921 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
924 if (state
& MESA_META_STENCIL_TEST
) {
925 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
927 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
928 _mesa_ClearStencil(stencil
->Clear
);
929 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_stencil_two_side
) {
930 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
931 stencil
->TestTwoSide
);
932 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
933 ? GL_BACK
: GL_FRONT
);
936 _mesa_StencilFuncSeparate(GL_FRONT
,
937 stencil
->Function
[0],
939 stencil
->ValueMask
[0]);
940 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
941 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
942 stencil
->ZFailFunc
[0],
943 stencil
->ZPassFunc
[0]);
945 _mesa_StencilFuncSeparate(GL_BACK
,
946 stencil
->Function
[1],
948 stencil
->ValueMask
[1]);
949 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
950 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
951 stencil
->ZFailFunc
[1],
952 stencil
->ZPassFunc
[1]);
955 if (state
& MESA_META_TEXTURE
) {
958 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
960 /* restore texenv for unit[0] */
961 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
962 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
965 /* restore texture objects for unit[0] only */
966 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
967 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
968 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
969 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
970 save
->CurrentTexture
[tgt
]);
972 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
975 /* Restore fixed function texture enables, texgen */
976 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
977 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
978 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
979 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
980 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
983 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
984 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
985 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
990 /* restore current unit state */
991 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
992 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
995 if (state
& MESA_META_TRANSFORM
) {
996 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
997 _mesa_ActiveTextureARB(GL_TEXTURE0
);
998 _mesa_MatrixMode(GL_TEXTURE
);
999 _mesa_LoadMatrixf(save
->TextureMatrix
);
1000 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
1002 _mesa_MatrixMode(GL_MODELVIEW
);
1003 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1005 _mesa_MatrixMode(GL_PROJECTION
);
1006 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1008 _mesa_MatrixMode(save
->MatrixMode
);
1011 if (state
& MESA_META_CLIP
) {
1012 if (save
->ClipPlanesEnabled
) {
1014 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1015 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1016 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1022 if (state
& MESA_META_VERTEX
) {
1023 /* restore vertex buffer object */
1024 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1025 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1027 /* restore vertex array object */
1028 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1029 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1032 if (state
& MESA_META_VIEWPORT
) {
1033 if (save
->ViewportX
!= ctx
->Viewport
.X
||
1034 save
->ViewportY
!= ctx
->Viewport
.Y
||
1035 save
->ViewportW
!= ctx
->Viewport
.Width
||
1036 save
->ViewportH
!= ctx
->Viewport
.Height
) {
1037 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
1038 save
->ViewportW
, save
->ViewportH
);
1040 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1043 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
1044 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1047 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
1048 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1051 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1052 if (save
->CondRenderQuery
)
1053 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1054 save
->CondRenderMode
);
1057 if (state
& MESA_META_SELECT_FEEDBACK
) {
1058 if (save
->RenderMode
== GL_SELECT
) {
1059 _mesa_RenderMode(GL_SELECT
);
1060 ctx
->Select
= save
->Select
;
1061 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1062 _mesa_RenderMode(GL_FEEDBACK
);
1063 ctx
->Feedback
= save
->Feedback
;
1067 if (state
& MESA_META_MULTISAMPLE
) {
1068 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1069 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1073 if (save
->Lighting
) {
1074 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1076 if (save
->RasterDiscard
) {
1077 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1079 if (save
->TransformFeedbackNeedsResume
)
1080 _mesa_ResumeTransformFeedback();
1082 ctx
->Meta
->SaveStackDepth
--;
1087 * Determine whether Mesa is currently in a meta state.
1090 _mesa_meta_in_progress(struct gl_context
*ctx
)
1092 return ctx
->Meta
->SaveStackDepth
!= 0;
1097 * Convert Z from a normalized value in the range [0, 1] to an object-space
1098 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1099 * default/identity ortho projection results in the original Z value.
1100 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1101 * value comes from the clear value or raster position.
1103 static INLINE GLfloat
1104 invert_z(GLfloat normZ
)
1106 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1112 * One-time init for a temp_texture object.
1113 * Choose tex target, compute max tex size, etc.
1116 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1118 /* prefer texture rectangle */
1119 if (ctx
->Extensions
.NV_texture_rectangle
) {
1120 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1121 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1122 tex
->NPOT
= GL_TRUE
;
1125 /* use 2D texture, NPOT if possible */
1126 tex
->Target
= GL_TEXTURE_2D
;
1127 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1128 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1130 tex
->MinSize
= 16; /* 16 x 16 at least */
1131 assert(tex
->MaxSize
> 0);
1133 _mesa_GenTextures(1, &tex
->TexObj
);
1137 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1141 _mesa_DeleteTextures(1, &tex
->TexObj
);
1147 * Return pointer to temp_texture info for non-bitmap ops.
1148 * This does some one-time init if needed.
1150 static struct temp_texture
*
1151 get_temp_texture(struct gl_context
*ctx
)
1153 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1156 init_temp_texture(ctx
, tex
);
1164 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1165 * We use a separate texture for bitmaps to reduce texture
1166 * allocation/deallocation.
1168 static struct temp_texture
*
1169 get_bitmap_temp_texture(struct gl_context
*ctx
)
1171 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1174 init_temp_texture(ctx
, tex
);
1182 * Compute the width/height of texture needed to draw an image of the
1183 * given size. Return a flag indicating whether the current texture
1184 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1185 * allocated (glTexImage2D).
1186 * Also, compute s/t texcoords for drawing.
1188 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1191 alloc_texture(struct temp_texture
*tex
,
1192 GLsizei width
, GLsizei height
, GLenum intFormat
)
1194 GLboolean newTex
= GL_FALSE
;
1196 ASSERT(width
<= tex
->MaxSize
);
1197 ASSERT(height
<= tex
->MaxSize
);
1199 if (width
> tex
->Width
||
1200 height
> tex
->Height
||
1201 intFormat
!= tex
->IntFormat
) {
1202 /* alloc new texture (larger or different format) */
1205 /* use non-power of two size */
1206 tex
->Width
= MAX2(tex
->MinSize
, width
);
1207 tex
->Height
= MAX2(tex
->MinSize
, height
);
1210 /* find power of two size */
1212 w
= h
= tex
->MinSize
;
1221 tex
->IntFormat
= intFormat
;
1226 /* compute texcoords */
1227 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1228 tex
->Sright
= (GLfloat
) width
;
1229 tex
->Ttop
= (GLfloat
) height
;
1232 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1233 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1241 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1244 setup_copypix_texture(struct temp_texture
*tex
,
1246 GLint srcX
, GLint srcY
,
1247 GLsizei width
, GLsizei height
, GLenum intFormat
,
1250 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1251 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1252 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1253 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1255 /* copy framebuffer image to texture */
1257 /* create new tex image */
1258 if (tex
->Width
== width
&& tex
->Height
== height
) {
1259 /* create new tex with framebuffer data */
1260 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1261 srcX
, srcY
, width
, height
, 0);
1264 /* create empty texture */
1265 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1266 tex
->Width
, tex
->Height
, 0,
1267 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1269 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1270 0, 0, srcX
, srcY
, width
, height
);
1274 /* replace existing tex image */
1275 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1276 0, 0, srcX
, srcY
, width
, height
);
1282 * Setup/load texture for glDrawPixels.
1285 setup_drawpix_texture(struct gl_context
*ctx
,
1286 struct temp_texture
*tex
,
1288 GLenum texIntFormat
,
1289 GLsizei width
, GLsizei height
,
1290 GLenum format
, GLenum type
,
1291 const GLvoid
*pixels
)
1293 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1294 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1295 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1296 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1298 /* copy pixel data to texture */
1300 /* create new tex image */
1301 if (tex
->Width
== width
&& tex
->Height
== height
) {
1302 /* create new tex and load image data */
1303 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1304 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1307 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1309 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1310 ctx
->Unpack
.BufferObj
);
1311 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1312 /* create empty texture */
1313 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1314 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1315 if (save_unpack_obj
!= NULL
)
1316 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1317 save_unpack_obj
->Name
);
1319 _mesa_TexSubImage2D(tex
->Target
, 0,
1320 0, 0, width
, height
, format
, type
, pixels
);
1324 /* replace existing tex image */
1325 _mesa_TexSubImage2D(tex
->Target
, 0,
1326 0, 0, width
, height
, format
, type
, pixels
);
1333 * One-time init for drawing depth pixels.
1336 init_blit_depth_pixels(struct gl_context
*ctx
)
1338 static const char *program
=
1340 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1343 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1344 struct temp_texture
*tex
= get_temp_texture(ctx
);
1345 const char *texTarget
;
1347 assert(blit
->DepthFP
== 0);
1349 /* replace %s with "RECT" or "2D" */
1350 assert(strlen(program
) + 4 < sizeof(program2
));
1351 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1355 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1357 _mesa_GenPrograms(1, &blit
->DepthFP
);
1358 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1359 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1360 strlen(program2
), (const GLubyte
*) program2
);
1365 * Try to do a glBlitFramebuffer using no-copy texturing.
1366 * We can do this when the src renderbuffer is actually a texture.
1367 * But if the src buffer == dst buffer we cannot do this.
1369 * \return new buffer mask indicating the buffers left to blit using the
1373 blitframebuffer_texture(struct gl_context
*ctx
,
1374 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1375 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1376 GLbitfield mask
, GLenum filter
)
1378 if (mask
& GL_COLOR_BUFFER_BIT
) {
1379 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1380 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1381 const struct gl_renderbuffer_attachment
*drawAtt
=
1382 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1383 const struct gl_renderbuffer_attachment
*readAtt
=
1384 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1386 if (readAtt
&& readAtt
->Texture
) {
1387 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1388 const GLuint srcLevel
= readAtt
->TextureLevel
;
1389 const GLint baseLevelSave
= texObj
->BaseLevel
;
1390 const GLint maxLevelSave
= texObj
->MaxLevel
;
1391 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1392 const GLenum target
= texObj
->Target
;
1393 GLuint sampler
, samplerSave
=
1394 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1395 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1397 if (drawAtt
->Texture
== readAtt
->Texture
) {
1398 /* Can't use same texture as both the source and dest. We need
1399 * to handle overlapping blits and besides, some hw may not
1405 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1406 /* Can't handle other texture types at this time */
1410 _mesa_GenSamplers(1, &sampler
);
1411 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1414 printf("Blit from texture!\n");
1415 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1416 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1419 /* Prepare src texture state */
1420 _mesa_BindTexture(target
, texObj
->Name
);
1421 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1422 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1423 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1424 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1425 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1427 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1428 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1430 /* Always do our blits with no sRGB decode or encode.*/
1431 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1432 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1433 GL_SKIP_DECODE_EXT
);
1435 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_framebuffer_sRGB
)
1436 || _mesa_is_gles3(ctx
)) {
1437 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1440 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1441 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1443 /* Prepare vertex data (the VBO was previously created and bound) */
1448 struct vertex verts
[4];
1449 GLfloat s0
, t0
, s1
, t1
;
1451 if (target
== GL_TEXTURE_2D
) {
1452 const struct gl_texture_image
*texImage
1453 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1454 s0
= srcX0
/ (float) texImage
->Width
;
1455 s1
= srcX1
/ (float) texImage
->Width
;
1456 t0
= srcY0
/ (float) texImage
->Height
;
1457 t1
= srcY1
/ (float) texImage
->Height
;
1460 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1467 verts
[0].x
= (GLfloat
) dstX0
;
1468 verts
[0].y
= (GLfloat
) dstY0
;
1469 verts
[1].x
= (GLfloat
) dstX1
;
1470 verts
[1].y
= (GLfloat
) dstY0
;
1471 verts
[2].x
= (GLfloat
) dstX1
;
1472 verts
[2].y
= (GLfloat
) dstY1
;
1473 verts
[3].x
= (GLfloat
) dstX0
;
1474 verts
[3].y
= (GLfloat
) dstY1
;
1485 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1488 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1490 /* Restore texture object state, the texture binding will
1491 * be restored by _mesa_meta_end().
1493 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1494 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1495 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1497 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1498 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1501 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1502 _mesa_DeleteSamplers(1, &sampler
);
1504 /* Done with color buffer */
1505 mask
&= ~GL_COLOR_BUFFER_BIT
;
1514 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1515 * of texture mapping and polygon rendering.
1518 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1519 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1520 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1521 GLbitfield mask
, GLenum filter
)
1523 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1524 struct temp_texture
*tex
= get_temp_texture(ctx
);
1525 const GLsizei maxTexSize
= tex
->MaxSize
;
1526 const GLint srcX
= MIN2(srcX0
, srcX1
);
1527 const GLint srcY
= MIN2(srcY0
, srcY1
);
1528 const GLint srcW
= abs(srcX1
- srcX0
);
1529 const GLint srcH
= abs(srcY1
- srcY0
);
1530 const GLboolean srcFlipX
= srcX1
< srcX0
;
1531 const GLboolean srcFlipY
= srcY1
< srcY0
;
1535 struct vertex verts
[4];
1538 /* In addition to falling back if the blit size is larger than the maximum
1539 * texture size, fallback if the source is multisampled. This fallback can
1540 * be removed once Mesa gets support ARB_texture_multisample.
1542 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1543 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1544 /* XXX avoid this fallback */
1545 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1546 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1562 /* only scissor effects blit so save/clear all other relevant state */
1563 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1565 if (blit
->ArrayObj
== 0) {
1566 /* one-time setup */
1568 /* create vertex array object */
1569 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1570 _mesa_BindVertexArray(blit
->ArrayObj
);
1572 /* create vertex array buffer */
1573 _mesa_GenBuffersARB(1, &blit
->VBO
);
1574 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1575 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1576 NULL
, GL_DYNAMIC_DRAW_ARB
);
1578 /* setup vertex arrays */
1579 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1580 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1581 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1582 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1585 _mesa_BindVertexArray(blit
->ArrayObj
);
1586 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1589 /* Try faster, direct texture approach first */
1590 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1591 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1593 _mesa_meta_end(ctx
);
1597 /* Continue with "normal" approach which involves copying the src rect
1598 * into a temporary texture and is "blitted" by drawing a textured quad.
1601 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1603 /* vertex positions/texcoords (after texture allocation!) */
1605 verts
[0].x
= (GLfloat
) dstX0
;
1606 verts
[0].y
= (GLfloat
) dstY0
;
1607 verts
[1].x
= (GLfloat
) dstX1
;
1608 verts
[1].y
= (GLfloat
) dstY0
;
1609 verts
[2].x
= (GLfloat
) dstX1
;
1610 verts
[2].y
= (GLfloat
) dstY1
;
1611 verts
[3].x
= (GLfloat
) dstX0
;
1612 verts
[3].y
= (GLfloat
) dstY1
;
1616 verts
[1].s
= tex
->Sright
;
1618 verts
[2].s
= tex
->Sright
;
1619 verts
[2].t
= tex
->Ttop
;
1621 verts
[3].t
= tex
->Ttop
;
1623 /* upload new vertex data */
1624 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1627 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1629 if (mask
& GL_COLOR_BUFFER_BIT
) {
1630 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1632 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1633 mask
&= ~GL_COLOR_BUFFER_BIT
;
1636 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1637 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1640 init_blit_depth_pixels(ctx
);
1642 /* maybe change tex format here */
1643 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1645 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1646 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1648 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1649 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1651 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1652 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1653 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1654 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1655 _mesa_DepthFunc(GL_ALWAYS
);
1656 _mesa_DepthMask(GL_TRUE
);
1658 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1659 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1665 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1666 /* XXX can't easily do stencil */
1669 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1671 _mesa_meta_end(ctx
);
1674 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1675 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1680 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1682 if (blit
->ArrayObj
) {
1683 _mesa_DeleteVertexArraysAPPLE(1, &blit
->ArrayObj
);
1685 _mesa_DeleteBuffersARB(1, &blit
->VBO
);
1688 if (blit
->DepthFP
) {
1689 _mesa_DeletePrograms(1, &blit
->DepthFP
);
1696 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1699 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1701 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1703 GLfloat x
, y
, z
, r
, g
, b
, a
;
1705 struct vertex verts
[4];
1706 /* save all state but scissor, pixel pack/unpack */
1707 GLbitfield metaSave
= (MESA_META_ALL
-
1709 MESA_META_PIXEL_STORE
-
1710 MESA_META_CONDITIONAL_RENDER
);
1711 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1713 if (buffers
& BUFFER_BITS_COLOR
) {
1714 /* if clearing color buffers, don't save/restore colormask */
1715 metaSave
-= MESA_META_COLOR_MASK
;
1718 _mesa_meta_begin(ctx
, metaSave
);
1720 if (clear
->ArrayObj
== 0) {
1721 /* one-time setup */
1723 /* create vertex array object */
1724 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1725 _mesa_BindVertexArray(clear
->ArrayObj
);
1727 /* create vertex array buffer */
1728 _mesa_GenBuffersARB(1, &clear
->VBO
);
1729 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1731 /* setup vertex arrays */
1732 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1733 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1734 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1735 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1738 _mesa_BindVertexArray(clear
->ArrayObj
);
1739 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1742 /* GL_COLOR_BUFFER_BIT */
1743 if (buffers
& BUFFER_BITS_COLOR
) {
1744 /* leave colormask, glDrawBuffer state as-is */
1746 /* Clears never have the color clamped. */
1747 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1750 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1751 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1754 /* GL_DEPTH_BUFFER_BIT */
1755 if (buffers
& BUFFER_BIT_DEPTH
) {
1756 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1757 _mesa_DepthFunc(GL_ALWAYS
);
1758 _mesa_DepthMask(GL_TRUE
);
1761 assert(!ctx
->Depth
.Test
);
1764 /* GL_STENCIL_BUFFER_BIT */
1765 if (buffers
& BUFFER_BIT_STENCIL
) {
1766 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1767 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1768 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1769 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1770 ctx
->Stencil
.Clear
& stencilMax
,
1771 ctx
->Stencil
.WriteMask
[0]);
1774 assert(!ctx
->Stencil
.Enabled
);
1777 /* vertex positions/colors */
1779 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1780 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1781 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1782 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1783 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1800 for (i
= 0; i
< 4; i
++) {
1801 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1802 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1803 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1804 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1807 /* upload new vertex data */
1808 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1809 GL_DYNAMIC_DRAW_ARB
);
1813 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1815 _mesa_meta_end(ctx
);
1819 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1821 const char *vs_source
=
1822 "attribute vec4 position;\n"
1825 " gl_Position = position;\n"
1827 const char *fs_source
=
1828 "uniform vec4 color;\n"
1831 " gl_FragColor = color;\n"
1833 const char *vs_int_source
=
1835 "in vec4 position;\n"
1838 " gl_Position = position;\n"
1840 const char *fs_int_source
=
1842 "uniform ivec4 color;\n"
1843 "out ivec4 out_color;\n"
1847 " out_color = color;\n"
1851 if (clear
->ArrayObj
!= 0)
1854 /* create vertex array object */
1855 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1856 _mesa_BindVertexArray(clear
->ArrayObj
);
1858 /* create vertex array buffer */
1859 _mesa_GenBuffersARB(1, &clear
->VBO
);
1860 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1862 /* setup vertex arrays */
1863 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1864 _mesa_EnableVertexAttribArrayARB(0);
1866 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1867 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1868 _mesa_CompileShaderARB(vs
);
1870 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1871 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1872 _mesa_CompileShaderARB(fs
);
1874 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1875 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1876 _mesa_DeleteObjectARB(fs
);
1877 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1878 _mesa_DeleteObjectARB(vs
);
1879 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1880 _mesa_LinkProgramARB(clear
->ShaderProg
);
1882 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1885 if (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) {
1886 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1887 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1889 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1890 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1891 _mesa_DeleteObjectARB(fs
);
1892 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1893 _mesa_DeleteObjectARB(vs
);
1894 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1896 /* Note that user-defined out attributes get automatically assigned
1897 * locations starting from 0, so we don't need to explicitly
1898 * BindFragDataLocation to 0.
1901 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1903 clear
->IntegerColorLocation
=
1904 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1909 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1911 if (clear
->ArrayObj
== 0)
1913 _mesa_DeleteVertexArraysAPPLE(1, &clear
->ArrayObj
);
1914 clear
->ArrayObj
= 0;
1915 _mesa_DeleteBuffersARB(1, &clear
->VBO
);
1917 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1918 clear
->ShaderProg
= 0;
1920 if (clear
->IntegerShaderProg
) {
1921 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1922 clear
->IntegerShaderProg
= 0;
1927 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1930 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1932 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1933 GLbitfield metaSave
;
1934 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1935 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1936 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1937 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1938 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1939 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1940 const float z
= -invert_z(ctx
->Depth
.Clear
);
1945 metaSave
= (MESA_META_ALPHA_TEST
|
1947 MESA_META_DEPTH_TEST
|
1948 MESA_META_RASTERIZATION
|
1950 MESA_META_STENCIL_TEST
|
1952 MESA_META_VIEWPORT
|
1954 MESA_META_CLAMP_FRAGMENT_COLOR
|
1955 MESA_META_MULTISAMPLE
);
1957 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1958 /* We'll use colormask to disable color writes. Otherwise,
1959 * respect color mask
1961 metaSave
|= MESA_META_COLOR_MASK
;
1964 _mesa_meta_begin(ctx
, metaSave
);
1966 meta_glsl_clear_init(ctx
, clear
);
1968 if (fb
->_IntegerColor
) {
1969 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1970 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1971 ctx
->Color
.ClearColor
.i
);
1973 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1974 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1975 ctx
->Color
.ClearColor
.f
);
1978 _mesa_BindVertexArray(clear
->ArrayObj
);
1979 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1981 /* GL_COLOR_BUFFER_BIT */
1982 if (buffers
& BUFFER_BITS_COLOR
) {
1983 /* leave colormask, glDrawBuffer state as-is */
1985 /* Clears never have the color clamped. */
1986 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1989 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1990 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1993 /* GL_DEPTH_BUFFER_BIT */
1994 if (buffers
& BUFFER_BIT_DEPTH
) {
1995 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1996 _mesa_DepthFunc(GL_ALWAYS
);
1997 _mesa_DepthMask(GL_TRUE
);
2000 assert(!ctx
->Depth
.Test
);
2003 /* GL_STENCIL_BUFFER_BIT */
2004 if (buffers
& BUFFER_BIT_STENCIL
) {
2005 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2006 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2007 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2008 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2009 ctx
->Stencil
.Clear
& stencilMax
,
2010 ctx
->Stencil
.WriteMask
[0]);
2013 assert(!ctx
->Stencil
.Enabled
);
2016 /* vertex positions */
2030 /* upload new vertex data */
2031 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2032 GL_DYNAMIC_DRAW_ARB
);
2035 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2037 _mesa_meta_end(ctx
);
2041 * Meta implementation of ctx->Driver.CopyPixels() in terms
2042 * of texture mapping and polygon rendering and GLSL shaders.
2045 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2046 GLsizei width
, GLsizei height
,
2047 GLint dstX
, GLint dstY
, GLenum type
)
2049 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2050 struct temp_texture
*tex
= get_temp_texture(ctx
);
2052 GLfloat x
, y
, z
, s
, t
;
2054 struct vertex verts
[4];
2056 GLenum intFormat
= GL_RGBA
;
2058 if (type
!= GL_COLOR
||
2059 ctx
->_ImageTransferState
||
2061 width
> tex
->MaxSize
||
2062 height
> tex
->MaxSize
) {
2063 /* XXX avoid this fallback */
2064 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2068 /* Most GL state applies to glCopyPixels, but a there's a few things
2069 * we need to override:
2071 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2074 MESA_META_TRANSFORM
|
2077 MESA_META_VIEWPORT
));
2079 if (copypix
->ArrayObj
== 0) {
2080 /* one-time setup */
2082 /* create vertex array object */
2083 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2084 _mesa_BindVertexArray(copypix
->ArrayObj
);
2086 /* create vertex array buffer */
2087 _mesa_GenBuffersARB(1, ©pix
->VBO
);
2088 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2089 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2090 NULL
, GL_DYNAMIC_DRAW_ARB
);
2092 /* setup vertex arrays */
2093 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2094 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2095 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2096 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2099 _mesa_BindVertexArray(copypix
->ArrayObj
);
2100 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2103 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2105 /* vertex positions, texcoords (after texture allocation!) */
2107 const GLfloat dstX0
= (GLfloat
) dstX
;
2108 const GLfloat dstY0
= (GLfloat
) dstY
;
2109 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2110 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2111 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2121 verts
[1].s
= tex
->Sright
;
2126 verts
[2].s
= tex
->Sright
;
2127 verts
[2].t
= tex
->Ttop
;
2132 verts
[3].t
= tex
->Ttop
;
2134 /* upload new vertex data */
2135 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2138 /* Alloc/setup texture */
2139 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2140 GL_RGBA
, GL_NEAREST
);
2142 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2144 /* draw textured quad */
2145 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2147 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2149 _mesa_meta_end(ctx
);
2155 * When the glDrawPixels() image size is greater than the max rectangle
2156 * texture size we use this function to break the glDrawPixels() image
2157 * into tiles which fit into the max texture size.
2160 tiled_draw_pixels(struct gl_context
*ctx
,
2162 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2163 GLenum format
, GLenum type
,
2164 const struct gl_pixelstore_attrib
*unpack
,
2165 const GLvoid
*pixels
)
2167 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2170 if (tileUnpack
.RowLength
== 0)
2171 tileUnpack
.RowLength
= width
;
2173 for (i
= 0; i
< width
; i
+= tileSize
) {
2174 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2175 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2177 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2179 for (j
= 0; j
< height
; j
+= tileSize
) {
2180 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2181 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2183 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2185 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2186 format
, type
, &tileUnpack
, pixels
);
2193 * One-time init for drawing stencil pixels.
2196 init_draw_stencil_pixels(struct gl_context
*ctx
)
2198 /* This program is run eight times, once for each stencil bit.
2199 * The stencil values to draw are found in an 8-bit alpha texture.
2200 * We read the texture/stencil value and test if bit 'b' is set.
2201 * If the bit is not set, use KIL to kill the fragment.
2202 * Finally, we use the stencil test to update the stencil buffer.
2204 * The basic algorithm for checking if a bit is set is:
2205 * if (is_odd(value / (1 << bit)))
2206 * result is one (or non-zero).
2209 * The program parameter contains three values:
2210 * parm.x = 255 / (1 << bit)
2214 static const char *program
=
2216 "PARAM parm = program.local[0]; \n"
2218 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2219 "# t = t * 255 / bit \n"
2220 "MUL t.x, t.a, parm.x; \n"
2223 "SUB t.x, t.x, t.y; \n"
2225 "MUL t.x, t.x, parm.y; \n"
2226 "# t = fract(t.x) \n"
2227 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2228 "# t.x = (t.x == 0 ? 1 : 0) \n"
2229 "SGE t.x, -t.x, parm.z; \n"
2231 "# for debug only \n"
2232 "#MOV result.color, t.x; \n"
2234 char program2
[1000];
2235 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2236 struct temp_texture
*tex
= get_temp_texture(ctx
);
2237 const char *texTarget
;
2239 assert(drawpix
->StencilFP
== 0);
2241 /* replace %s with "RECT" or "2D" */
2242 assert(strlen(program
) + 4 < sizeof(program2
));
2243 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2247 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2249 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2250 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2251 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2252 strlen(program2
), (const GLubyte
*) program2
);
2257 * One-time init for drawing depth pixels.
2260 init_draw_depth_pixels(struct gl_context
*ctx
)
2262 static const char *program
=
2264 "PARAM color = program.local[0]; \n"
2265 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2266 "MOV result.color, color; \n"
2269 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2270 struct temp_texture
*tex
= get_temp_texture(ctx
);
2271 const char *texTarget
;
2273 assert(drawpix
->DepthFP
== 0);
2275 /* replace %s with "RECT" or "2D" */
2276 assert(strlen(program
) + 4 < sizeof(program2
));
2277 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2281 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2283 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2284 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2285 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2286 strlen(program2
), (const GLubyte
*) program2
);
2291 * Meta implementation of ctx->Driver.DrawPixels() in terms
2292 * of texture mapping and polygon rendering.
2295 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2296 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2297 GLenum format
, GLenum type
,
2298 const struct gl_pixelstore_attrib
*unpack
,
2299 const GLvoid
*pixels
)
2301 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2302 struct temp_texture
*tex
= get_temp_texture(ctx
);
2303 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2304 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2306 GLfloat x
, y
, z
, s
, t
;
2308 struct vertex verts
[4];
2309 GLenum texIntFormat
;
2310 GLboolean fallback
, newTex
;
2311 GLbitfield metaExtraSave
= 0x0;
2315 * Determine if we can do the glDrawPixels with texture mapping.
2317 fallback
= GL_FALSE
;
2318 if (ctx
->Fog
.Enabled
) {
2322 if (_mesa_is_color_format(format
)) {
2323 /* use more compact format when possible */
2324 /* XXX disable special case for GL_LUMINANCE for now to work around
2325 * apparent i965 driver bug (see bug #23670).
2327 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2328 texIntFormat
= format
;
2330 texIntFormat
= GL_RGBA
;
2332 /* If we're not supposed to clamp the resulting color, then just
2333 * promote our texture to fully float. We could do better by
2334 * just going for the matching set of channels, in floating
2337 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2338 ctx
->Extensions
.ARB_texture_float
)
2339 texIntFormat
= GL_RGBA32F
;
2341 else if (_mesa_is_stencil_format(format
)) {
2342 if (ctx
->Extensions
.ARB_fragment_program
&&
2343 ctx
->Pixel
.IndexShift
== 0 &&
2344 ctx
->Pixel
.IndexOffset
== 0 &&
2345 type
== GL_UNSIGNED_BYTE
) {
2346 /* We'll store stencil as alpha. This only works for GLubyte
2347 * image data because of how incoming values are mapped to alpha
2350 texIntFormat
= GL_ALPHA
;
2351 metaExtraSave
= (MESA_META_COLOR_MASK
|
2352 MESA_META_DEPTH_TEST
|
2353 MESA_META_PIXEL_TRANSFER
|
2355 MESA_META_STENCIL_TEST
);
2361 else if (_mesa_is_depth_format(format
)) {
2362 if (ctx
->Extensions
.ARB_depth_texture
&&
2363 ctx
->Extensions
.ARB_fragment_program
) {
2364 texIntFormat
= GL_DEPTH_COMPONENT
;
2365 metaExtraSave
= (MESA_META_SHADER
);
2376 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2377 format
, type
, unpack
, pixels
);
2382 * Check image size against max texture size, draw as tiles if needed.
2384 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2385 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2386 format
, type
, unpack
, pixels
);
2390 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2391 * but a there's a few things we need to override:
2393 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2396 MESA_META_TRANSFORM
|
2399 MESA_META_VIEWPORT
|
2402 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2404 /* vertex positions, texcoords (after texture allocation!) */
2406 const GLfloat x0
= (GLfloat
) x
;
2407 const GLfloat y0
= (GLfloat
) y
;
2408 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2409 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2410 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2420 verts
[1].s
= tex
->Sright
;
2425 verts
[2].s
= tex
->Sright
;
2426 verts
[2].t
= tex
->Ttop
;
2431 verts
[3].t
= tex
->Ttop
;
2434 if (drawpix
->ArrayObj
== 0) {
2435 /* one-time setup: create vertex array object */
2436 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2438 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2440 /* create vertex array buffer */
2441 _mesa_GenBuffersARB(1, &vbo
);
2442 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2443 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2444 verts
, GL_DYNAMIC_DRAW_ARB
);
2446 /* setup vertex arrays */
2447 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2448 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2449 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2450 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2452 /* set given unpack params */
2453 ctx
->Unpack
= *unpack
;
2455 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2457 if (_mesa_is_stencil_format(format
)) {
2458 /* Drawing stencil */
2461 if (!drawpix
->StencilFP
)
2462 init_draw_stencil_pixels(ctx
);
2464 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2465 GL_ALPHA
, type
, pixels
);
2467 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2469 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2471 /* set all stencil bits to 0 */
2472 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2473 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2474 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2476 /* set stencil bits to 1 where needed */
2477 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2479 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2480 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2482 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2483 const GLuint mask
= 1 << bit
;
2484 if (mask
& origStencilMask
) {
2485 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2486 _mesa_StencilMask(mask
);
2488 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2489 255.0 / mask
, 0.5, 0.0, 0.0);
2491 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2495 else if (_mesa_is_depth_format(format
)) {
2497 if (!drawpix
->DepthFP
)
2498 init_draw_depth_pixels(ctx
);
2500 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2501 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2503 /* polygon color = current raster color */
2504 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2505 ctx
->Current
.RasterColor
);
2507 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2508 format
, type
, pixels
);
2510 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2514 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2515 format
, type
, pixels
);
2516 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2519 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2521 _mesa_DeleteBuffersARB(1, &vbo
);
2523 /* restore unpack params */
2524 ctx
->Unpack
= unpackSave
;
2526 _mesa_meta_end(ctx
);
2530 alpha_test_raster_color(struct gl_context
*ctx
)
2532 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2533 GLfloat ref
= ctx
->Color
.AlphaRef
;
2535 switch (ctx
->Color
.AlphaFunc
) {
2541 return alpha
== ref
;
2543 return alpha
<= ref
;
2547 return alpha
!= ref
;
2549 return alpha
>= ref
;
2559 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2560 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2561 * tracker would improve performance a lot.
2564 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2565 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2566 const struct gl_pixelstore_attrib
*unpack
,
2567 const GLubyte
*bitmap1
)
2569 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2570 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2571 const GLenum texIntFormat
= GL_ALPHA
;
2572 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2575 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2577 struct vertex verts
[4];
2582 * Check if swrast fallback is needed.
2584 if (ctx
->_ImageTransferState
||
2585 ctx
->FragmentProgram
._Enabled
||
2587 ctx
->Texture
._EnabledUnits
||
2588 width
> tex
->MaxSize
||
2589 height
> tex
->MaxSize
) {
2590 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2594 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2597 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2598 * but a there's a few things we need to override:
2600 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2601 MESA_META_PIXEL_STORE
|
2602 MESA_META_RASTERIZATION
|
2605 MESA_META_TRANSFORM
|
2608 MESA_META_VIEWPORT
));
2610 if (bitmap
->ArrayObj
== 0) {
2611 /* one-time setup */
2613 /* create vertex array object */
2614 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2615 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2617 /* create vertex array buffer */
2618 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2619 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2620 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2621 NULL
, GL_DYNAMIC_DRAW_ARB
);
2623 /* setup vertex arrays */
2624 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2625 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2626 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2627 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2628 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2629 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2632 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2633 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2636 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2638 /* vertex positions, texcoords, colors (after texture allocation!) */
2640 const GLfloat x0
= (GLfloat
) x
;
2641 const GLfloat y0
= (GLfloat
) y
;
2642 const GLfloat x1
= (GLfloat
) (x
+ width
);
2643 const GLfloat y1
= (GLfloat
) (y
+ height
);
2644 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2655 verts
[1].s
= tex
->Sright
;
2660 verts
[2].s
= tex
->Sright
;
2661 verts
[2].t
= tex
->Ttop
;
2666 verts
[3].t
= tex
->Ttop
;
2668 for (i
= 0; i
< 4; i
++) {
2669 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2670 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2671 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2672 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2675 /* upload new vertex data */
2676 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2679 /* choose different foreground/background alpha values */
2680 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2681 bg
= (fg
> 127 ? 0 : 255);
2683 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2685 _mesa_meta_end(ctx
);
2689 bitmap8
= malloc(width
* height
);
2691 memset(bitmap8
, bg
, width
* height
);
2692 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2693 bitmap8
, width
, fg
);
2695 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2697 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2698 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2700 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2701 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2703 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2705 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2710 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2712 _mesa_meta_end(ctx
);
2717 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2718 * software fallback. The fallback path will require that the texture
2719 * images are mapped.
2720 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2723 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2724 struct gl_texture_object
*texObj
)
2726 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2727 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2728 struct gl_texture_image
*baseImage
;
2732 /* check for fallbacks */
2733 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2734 target
== GL_TEXTURE_3D
||
2735 target
== GL_TEXTURE_1D_ARRAY
||
2736 target
== GL_TEXTURE_2D_ARRAY
) {
2740 srcLevel
= texObj
->BaseLevel
;
2741 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2742 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2746 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2747 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2748 /* The texture format is sRGB but we can't turn off sRGB->linear
2749 * texture sample conversion. So we won't be able to generate the
2750 * right colors when rendering. Need to use a fallback.
2756 * Test that we can actually render in the texture's format.
2759 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2760 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2762 if (target
== GL_TEXTURE_1D
) {
2763 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2764 GL_COLOR_ATTACHMENT0_EXT
,
2765 target
, texObj
->Name
, srcLevel
);
2768 /* other work is needed to enable 3D mipmap generation */
2769 else if (target
== GL_TEXTURE_3D
) {
2771 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2772 GL_COLOR_ATTACHMENT0_EXT
,
2773 target
, texObj
->Name
, srcLevel
, zoffset
);
2778 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2779 GL_COLOR_ATTACHMENT0_EXT
,
2780 target
, texObj
->Name
, srcLevel
);
2783 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2785 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2787 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2796 * Compute the texture coordinates for the four vertices of a quad for
2797 * drawing a 2D texture image or slice of a cube/3D texture.
2798 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2799 * \param slice slice of a 1D/2D array texture or 3D texture
2800 * \param width width of the texture image
2801 * \param height height of the texture image
2802 * \param coords0/1/2/3 returns the computed texcoords
2805 setup_texture_coords(GLenum faceTarget
,
2814 static const GLfloat st
[4][2] = {
2815 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2820 switch (faceTarget
) {
2824 case GL_TEXTURE_2D_ARRAY
:
2825 if (faceTarget
== GL_TEXTURE_3D
)
2827 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2831 coords0
[0] = 0.0F
; /* s */
2832 coords0
[1] = 0.0F
; /* t */
2833 coords0
[2] = r
; /* r */
2844 case GL_TEXTURE_RECTANGLE_ARB
:
2845 coords0
[0] = 0.0F
; /* s */
2846 coords0
[1] = 0.0F
; /* t */
2847 coords0
[2] = 0.0F
; /* r */
2852 coords2
[1] = height
;
2855 coords3
[1] = height
;
2858 case GL_TEXTURE_1D_ARRAY
:
2859 coords0
[0] = 0.0F
; /* s */
2860 coords0
[1] = slice
; /* t */
2861 coords0
[2] = 0.0F
; /* r */
2873 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2874 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2875 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2876 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2877 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2878 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2879 /* loop over quad verts */
2880 for (i
= 0; i
< 4; i
++) {
2881 /* Compute sc = +/-scale and tc = +/-scale.
2882 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2883 * though that can still sometimes happen with this scale factor...
2885 const GLfloat scale
= 0.9999f
;
2886 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2887 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2907 switch (faceTarget
) {
2908 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2913 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2918 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2923 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2928 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2933 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2944 assert(0 && "unexpected target in meta setup_texture_coords()");
2950 setup_ff_generate_mipmap(struct gl_context
*ctx
,
2951 struct gen_mipmap_state
*mipmap
)
2954 GLfloat x
, y
, tex
[3];
2957 if (mipmap
->ArrayObj
== 0) {
2958 /* one-time setup */
2959 /* create vertex array object */
2960 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2961 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2963 /* create vertex array buffer */
2964 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2965 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2966 /* setup vertex arrays */
2967 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2968 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2969 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2970 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2973 /* setup projection matrix */
2974 _mesa_MatrixMode(GL_PROJECTION
);
2975 _mesa_LoadIdentity();
2976 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
2981 setup_texture_sampler(GLenum target
, struct glsl_sampler
*sampler
)
2985 sampler
->type
= "sampler1D";
2986 sampler
->func
= "texture1D";
2987 sampler
->texcoords
= "texCoords.x";
2990 sampler
->type
= "sampler2D";
2991 sampler
->func
= "texture2D";
2992 sampler
->texcoords
= "texCoords.xy";
2995 /* Code for mipmap generation with 3D textures is not used yet.
2996 * It's a sw fallback.
2998 sampler
->type
= "sampler3D";
2999 sampler
->func
= "texture3D";
3000 sampler
->texcoords
= "texCoords";
3002 case GL_TEXTURE_CUBE_MAP
:
3003 sampler
->type
= "samplerCube";
3004 sampler
->func
= "textureCube";
3005 sampler
->texcoords
= "texCoords";
3007 case GL_TEXTURE_1D_ARRAY
:
3008 sampler
->type
= "sampler1DArray";
3009 sampler
->func
= "texture1DArray";
3010 sampler
->texcoords
= "texCoords.xy";
3012 case GL_TEXTURE_2D_ARRAY
:
3013 sampler
->type
= "sampler2DArray";
3014 sampler
->func
= "texture2DArray";
3015 sampler
->texcoords
= "texCoords";
3018 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3019 " setup_texture_sampler()\n", target
);
3025 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3026 struct gen_mipmap_state
*mipmap
,
3030 GLfloat x
, y
, tex
[3];
3032 struct glsl_sampler sampler
;
3033 const char *vs_source
;
3034 const char *fs_template
;
3036 static const char *vs_int_source
=
3038 "in vec2 position;\n"
3039 "in vec3 textureCoords;\n"
3040 "out vec3 texCoords;\n"
3043 " texCoords = textureCoords;\n"
3044 " gl_Position = gl_Vertex;\n"
3046 static const char *fs_int_source
=
3048 "uniform isampler2D tex2d;\n"
3049 "in vec3 texCoords;\n"
3050 "out ivec4 out_color;\n"
3054 " out_color = texture(tex2d, texCoords.xy);\n"
3057 const char *extension_mode
;
3060 if (ctx
->Const
.GLSLVersion
< 130) {
3062 "attribute vec2 position;\n"
3063 "attribute vec3 textureCoords;\n"
3064 "varying vec3 texCoords;\n"
3067 " texCoords = textureCoords;\n"
3068 " gl_Position = vec4(position, 0.0, 1.0);\n"
3071 "#extension GL_EXT_texture_array : %s\n"
3072 "uniform %s texSampler;\n"
3073 "varying vec3 texCoords;\n"
3076 " gl_FragColor = %s(texSampler, %s);\n"
3081 "in vec2 position;\n"
3082 "in vec3 textureCoords;\n"
3083 "out vec3 texCoords;\n"
3086 " texCoords = textureCoords;\n"
3087 " gl_Position = vec4(position, 0.0, 1.0);\n"
3091 "uniform %s texSampler;\n"
3092 "in vec3 texCoords;\n"
3093 "out %s out_color;\n"
3097 " out_color = texture(texSampler, %s);\n"
3101 /* Check if already initialized */
3102 if (mipmap
->ArrayObj
!= 0)
3104 /* create vertex array object */
3105 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3106 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3108 /* create vertex array buffer */
3109 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
3110 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3112 /* setup vertex arrays */
3113 _mesa_VertexAttribPointerARB(0, 2, GL_FLOAT
, GL_FALSE
,
3114 sizeof(struct vertex
), OFFSET(x
));
3115 _mesa_VertexAttribPointerARB(1, 3, GL_FLOAT
, GL_FALSE
,
3116 sizeof(struct vertex
), OFFSET(tex
));
3118 /* Generate a fragment shader program appropriate for the texture target */
3119 setup_texture_sampler(target
, &sampler
);
3120 mem_ctx
= ralloc_context(NULL
);
3122 if (ctx
->Const
.GLSLVersion
< 130) {
3123 extension_mode
= ((target
== GL_TEXTURE_1D_ARRAY
) ||
3124 (target
== GL_TEXTURE_2D_ARRAY
)) ?
3125 "require" : "disable";
3127 fs_source
= ralloc_asprintf(mem_ctx
, fs_template
,
3128 extension_mode
, sampler
.type
,
3129 sampler
.func
, sampler
.texcoords
);
3132 fs_source
= ralloc_asprintf(mem_ctx
, fs_template
,
3133 sampler
.type
, "vec4",
3137 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3138 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3140 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3141 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3142 _mesa_DeleteObjectARB(fs
);
3143 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3144 _mesa_DeleteObjectARB(vs
);
3145 _mesa_BindAttribLocationARB(mipmap
->ShaderProg
, 0, "position");
3146 _mesa_BindAttribLocationARB(mipmap
->ShaderProg
, 1, "texcoords");
3147 _mesa_EnableVertexAttribArrayARB(0);
3148 _mesa_EnableVertexAttribArrayARB(1);
3149 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3150 ralloc_free(mem_ctx
);
3152 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) ||
3153 _mesa_is_gles3(ctx
)){
3154 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
3155 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
3157 mipmap
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
3158 _mesa_AttachShader(mipmap
->IntegerShaderProg
, fs
);
3159 _mesa_DeleteObjectARB(fs
);
3160 _mesa_AttachShader(mipmap
->IntegerShaderProg
, vs
);
3161 _mesa_DeleteObjectARB(vs
);
3162 _mesa_BindAttribLocationARB(mipmap
->IntegerShaderProg
, 0, "position");
3163 _mesa_BindAttribLocationARB(mipmap
->IntegerShaderProg
, 1, "texcoords");
3165 /* Note that user-defined out attributes get automatically assigned
3166 * locations starting from 0, so we don't need to explicitly
3167 * BindFragDataLocation to 0.
3169 link_program_with_debug(ctx
, mipmap
->IntegerShaderProg
);
3175 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3176 struct gen_mipmap_state
*mipmap
)
3178 if (mipmap
->ArrayObj
== 0)
3180 _mesa_DeleteVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3181 mipmap
->ArrayObj
= 0;
3182 _mesa_DeleteBuffersARB(1, &mipmap
->VBO
);
3184 _mesa_DeleteObjectARB(mipmap
->ShaderProg
);
3185 mipmap
->ShaderProg
= 0;
3187 if (mipmap
->IntegerShaderProg
) {
3188 _mesa_DeleteObjectARB(mipmap
->IntegerShaderProg
);
3189 mipmap
->IntegerShaderProg
= 0;
3195 * Called via ctx->Driver.GenerateMipmap()
3196 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3200 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3201 struct gl_texture_object
*texObj
)
3203 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3205 GLfloat x
, y
, tex
[3];
3207 struct vertex verts
[4];
3208 const GLuint baseLevel
= texObj
->BaseLevel
;
3209 const GLuint maxLevel
= texObj
->MaxLevel
;
3210 const GLint maxLevelSave
= texObj
->MaxLevel
;
3211 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3212 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
3213 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3214 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3215 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3216 ctx
->Extensions
.ARB_fragment_shader
&&
3217 (ctx
->API
!= API_OPENGLES
);
3220 const GLint slice
= 0;
3223 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3224 _mesa_generate_mipmap(ctx
, target
, texObj
);
3228 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3229 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3230 faceTarget
= target
;
3231 target
= GL_TEXTURE_CUBE_MAP
;
3234 faceTarget
= target
;
3237 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3239 /* Choose between glsl version and fixed function version of
3240 * GenerateMipmap function.
3242 if (use_glsl_version
) {
3243 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3245 if (texObj
->_IsIntegerFormat
)
3246 _mesa_UseProgramObjectARB(mipmap
->IntegerShaderProg
);
3248 _mesa_UseProgramObjectARB(mipmap
->ShaderProg
);
3251 setup_ff_generate_mipmap(ctx
, mipmap
);
3252 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3255 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3256 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3258 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3259 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3261 if (currentTexUnitSave
!= 0)
3262 _mesa_BindTexture(target
, texObj
->Name
);
3265 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
3268 if (!mipmap
->Sampler
) {
3269 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3270 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3272 if (use_glsl_version
&& texObj
->_IsIntegerFormat
)
3273 _mesa_SamplerParameteri(mipmap
->Sampler
,
3274 GL_TEXTURE_MIN_FILTER
,
3275 GL_NEAREST_MIPMAP_NEAREST
);
3277 _mesa_SamplerParameteri(mipmap
->Sampler
,
3278 GL_TEXTURE_MIN_FILTER
,
3279 GL_LINEAR_MIPMAP_LINEAR
);
3281 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3282 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3283 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3284 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3286 /* We don't want to encode or decode sRGB values; treat them as linear.
3287 * This is not technically correct for GLES3 but we don't get any API
3288 * error at the moment.
3290 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3291 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3292 GL_SKIP_DECODE_EXT
);
3296 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3299 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3301 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
)
3302 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3304 assert(!genMipmapSave
);
3306 if ((ctx
->Extensions
.EXT_framebuffer_sRGB
&&
3307 _mesa_is_desktop_gl(ctx
)) ||
3308 _mesa_is_gles3(ctx
)) {
3309 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3312 /* Setup texture coordinates */
3313 setup_texture_coords(faceTarget
,
3315 0, 0, /* width, height never used here */
3321 /* setup vertex positions */
3331 /* upload vertex data */
3332 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3333 verts
, GL_DYNAMIC_DRAW_ARB
);
3335 /* texture is already locked, unlock now */
3336 _mesa_unlock_texture(ctx
, texObj
);
3338 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3339 const struct gl_texture_image
*srcImage
;
3340 const GLuint srcLevel
= dstLevel
- 1;
3341 GLsizei srcWidth
, srcHeight
, srcDepth
;
3342 GLsizei dstWidth
, dstHeight
, dstDepth
;
3345 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3346 assert(srcImage
->Border
== 0);
3349 srcWidth
= srcImage
->Width
;
3350 srcHeight
= srcImage
->Height
;
3351 srcDepth
= srcImage
->Depth
;
3354 dstWidth
= MAX2(1, srcWidth
/ 2);
3355 dstHeight
= MAX2(1, srcHeight
/ 2);
3356 dstDepth
= MAX2(1, srcDepth
/ 2);
3358 if (dstWidth
== srcImage
->Width
&&
3359 dstHeight
== srcImage
->Height
&&
3360 dstDepth
== srcImage
->Depth
) {
3365 /* Allocate storage for the destination mipmap image(s) */
3367 /* Set MaxLevel large enough to hold the new level when we allocate it */
3368 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3370 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3371 dstWidth
, dstHeight
, dstDepth
,
3373 srcImage
->InternalFormat
,
3374 srcImage
->TexFormat
)) {
3375 /* All done. We either ran out of memory or we would go beyond the
3376 * last valid level of an immutable texture if we continued.
3381 /* limit minification to src level */
3382 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3384 /* Set to draw into the current dstLevel */
3385 if (target
== GL_TEXTURE_1D
) {
3386 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
3387 GL_COLOR_ATTACHMENT0_EXT
,
3392 else if (target
== GL_TEXTURE_3D
) {
3393 GLint zoffset
= 0; /* XXX unfinished */
3394 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
3395 GL_COLOR_ATTACHMENT0_EXT
,
3402 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
3403 GL_COLOR_ATTACHMENT0_EXT
,
3409 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3412 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3413 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3414 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3415 "_mesa_meta_GenerateMipmap()");
3419 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3420 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3422 /* setup viewport */
3423 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3425 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3428 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3429 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3432 _mesa_lock_texture(ctx
, texObj
); /* relock */
3434 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3436 _mesa_meta_end(ctx
);
3438 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3440 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3442 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3447 * Determine the GL data type to use for the temporary image read with
3448 * ReadPixels() and passed to Tex[Sub]Image().
3451 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3455 baseFormat
= _mesa_get_format_base_format(format
);
3457 switch (baseFormat
) {
3464 case GL_LUMINANCE_ALPHA
:
3466 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3467 return GL_UNSIGNED_BYTE
;
3468 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3469 return GL_UNSIGNED_SHORT
;
3471 return _mesa_get_format_datatype(format
);
3472 case GL_DEPTH_COMPONENT
:
3473 return GL_UNSIGNED_INT
;
3474 case GL_DEPTH_STENCIL
:
3475 return GL_UNSIGNED_INT_24_8
;
3477 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3485 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3486 * Have to be careful with locking and meta state for pixel transfer.
3489 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3490 struct gl_texture_image
*texImage
,
3491 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3492 struct gl_renderbuffer
*rb
,
3494 GLsizei width
, GLsizei height
)
3496 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3497 GLenum format
, type
;
3501 /* Choose format/type for temporary image buffer */
3502 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3503 if (format
== GL_LUMINANCE
||
3504 format
== GL_LUMINANCE_ALPHA
||
3505 format
== GL_INTENSITY
) {
3506 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3507 * temp image buffer because glReadPixels will do L=R+G+B which is
3508 * not what we want (should be L=R).
3513 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3514 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3515 format
= _mesa_base_format_to_integer_format(format
);
3517 bpp
= _mesa_bytes_per_pixel(format
, type
);
3519 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3524 * Alloc image buffer (XXX could use a PBO)
3526 buf
= malloc(width
* height
* bpp
);
3528 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3532 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3535 * Read image from framebuffer (disable pixel transfer ops)
3537 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3538 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3539 format
, type
, &ctx
->Pack
, buf
);
3540 _mesa_meta_end(ctx
);
3542 _mesa_update_state(ctx
); /* to update pixel transfer state */
3545 * Store texture data (with pixel transfer ops)
3547 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3549 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3550 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3551 format
, type
, buf
, &ctx
->Unpack
);
3553 _mesa_meta_end(ctx
);
3555 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3562 * Decompress a texture image by drawing a quad with the compressed
3563 * texture and reading the pixels out of the color buffer.
3564 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3565 * \param destFormat format, ala glReadPixels
3566 * \param destType type, ala glReadPixels
3567 * \param dest destination buffer
3568 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3571 decompress_texture_image(struct gl_context
*ctx
,
3572 struct gl_texture_image
*texImage
,
3574 GLenum destFormat
, GLenum destType
,
3577 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3578 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3579 const GLint width
= texImage
->Width
;
3580 const GLint height
= texImage
->Height
;
3581 const GLenum target
= texObj
->Target
;
3584 GLfloat x
, y
, tex
[3];
3586 struct vertex verts
[4];
3587 GLuint fboDrawSave
, fboReadSave
;
3592 assert(target
== GL_TEXTURE_3D
||
3593 target
== GL_TEXTURE_2D_ARRAY
);
3596 if (target
== GL_TEXTURE_CUBE_MAP
) {
3597 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3600 faceTarget
= target
;
3603 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3604 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3605 fboReadSave
= ctx
->ReadBuffer
->Name
;
3606 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3608 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3610 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3611 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3613 /* Create/bind FBO/renderbuffer */
3614 if (decompress
->FBO
== 0) {
3615 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3616 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3617 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3618 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3619 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3620 GL_COLOR_ATTACHMENT0_EXT
,
3621 GL_RENDERBUFFER_EXT
,
3625 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3628 /* alloc dest surface */
3629 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3630 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3631 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3633 decompress
->Width
= width
;
3634 decompress
->Height
= height
;
3637 /* setup VBO data */
3638 if (decompress
->ArrayObj
== 0) {
3639 /* create vertex array object */
3640 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3641 _mesa_BindVertexArray(decompress
->ArrayObj
);
3643 /* create vertex array buffer */
3644 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3645 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3646 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3647 NULL
, GL_DYNAMIC_DRAW_ARB
);
3649 /* setup vertex arrays */
3650 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3651 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3652 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3653 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3656 _mesa_BindVertexArray(decompress
->ArrayObj
);
3657 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3660 if (!decompress
->Sampler
) {
3661 _mesa_GenSamplers(1, &decompress
->Sampler
);
3662 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3663 /* nearest filtering */
3664 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3665 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3666 /* No sRGB decode or encode.*/
3667 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3668 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3669 GL_SKIP_DECODE_EXT
);
3673 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3676 setup_texture_coords(faceTarget
, slice
, width
, height
,
3682 /* setup vertex positions */
3688 verts
[2].y
= height
;
3690 verts
[3].y
= height
;
3692 /* upload new vertex data */
3693 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3695 /* setup texture state */
3696 _mesa_BindTexture(target
, texObj
->Name
);
3697 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3700 /* save texture object state */
3701 const GLint baseLevelSave
= texObj
->BaseLevel
;
3702 const GLint maxLevelSave
= texObj
->MaxLevel
;
3704 /* restrict sampling to the texture level of interest */
3705 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3706 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3707 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3710 /* No sRGB decode or encode.*/
3711 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_framebuffer_sRGB
)
3712 || _mesa_is_gles3(ctx
)) {
3713 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3716 /* render quad w/ texture into renderbuffer */
3717 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3719 /* Restore texture object state, the texture binding will
3720 * be restored by _mesa_meta_end().
3722 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3723 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3724 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3729 /* read pixels from renderbuffer */
3731 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3733 /* The pixel transfer state will be set to default values at this point
3734 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3735 * turned off (as required by glGetTexImage) but we need to handle some
3736 * special cases. In particular, single-channel texture values are
3737 * returned as red and two-channel texture values are returned as
3740 if (baseTexFormat
== GL_LUMINANCE
||
3741 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3742 baseTexFormat
== GL_INTENSITY
) {
3743 /* Green and blue must be zero */
3744 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3745 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3748 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3751 /* disable texture unit */
3752 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3754 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3756 _mesa_meta_end(ctx
);
3758 /* restore fbo bindings */
3759 if (fboDrawSave
== fboReadSave
) {
3760 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3763 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3764 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3766 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, rbSave
);
3771 * This is just a wrapper around _mesa_get_tex_image() and
3772 * decompress_texture_image(). Meta functions should not be directly called
3776 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3777 GLenum format
, GLenum type
, GLvoid
*pixels
,
3778 struct gl_texture_image
*texImage
)
3780 /* We can only use the decompress-with-blit method here if the texels are
3781 * unsigned, normalized values. We could handle signed and unnormalized
3782 * with floating point renderbuffers...
3784 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3785 _mesa_get_format_datatype(texImage
->TexFormat
)
3786 == GL_UNSIGNED_NORMALIZED
) {
3787 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3788 const GLuint slice
= 0; /* only 2D compressed textures for now */
3789 /* Need to unlock the texture here to prevent deadlock... */
3790 _mesa_unlock_texture(ctx
, texObj
);
3791 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3792 /* ... and relock it */
3793 _mesa_lock_texture(ctx
, texObj
);
3796 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3802 * Meta implementation of ctx->Driver.DrawTex() in terms
3803 * of polygon rendering.
3806 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3807 GLfloat width
, GLfloat height
)
3809 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3811 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3813 struct vertex verts
[4];
3816 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3818 MESA_META_TRANSFORM
|
3820 MESA_META_VIEWPORT
));
3822 if (drawtex
->ArrayObj
== 0) {
3823 /* one-time setup */
3824 GLint active_texture
;
3826 /* create vertex array object */
3827 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3828 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3830 /* create vertex array buffer */
3831 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3832 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3833 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3834 NULL
, GL_DYNAMIC_DRAW_ARB
);
3836 /* client active texture is not part of the array object */
3837 active_texture
= ctx
->Array
.ActiveTexture
;
3839 /* setup vertex arrays */
3840 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3841 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3842 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3843 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3844 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3845 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3848 /* restore client active texture */
3849 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3852 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3853 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3856 /* vertex positions, texcoords */
3858 const GLfloat x1
= x
+ width
;
3859 const GLfloat y1
= y
+ height
;
3861 z
= CLAMP(z
, 0.0, 1.0);
3880 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3881 const struct gl_texture_object
*texObj
;
3882 const struct gl_texture_image
*texImage
;
3883 GLfloat s
, t
, s1
, t1
;
3886 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3888 for (j
= 0; j
< 4; j
++) {
3889 verts
[j
].st
[i
][0] = 0.0f
;
3890 verts
[j
].st
[i
][1] = 0.0f
;
3895 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3896 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3897 tw
= texImage
->Width2
;
3898 th
= texImage
->Height2
;
3900 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3901 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3902 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3903 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3905 verts
[0].st
[i
][0] = s
;
3906 verts
[0].st
[i
][1] = t
;
3908 verts
[1].st
[i
][0] = s1
;
3909 verts
[1].st
[i
][1] = t
;
3911 verts
[2].st
[i
][0] = s1
;
3912 verts
[2].st
[i
][1] = t1
;
3914 verts
[3].st
[i
][0] = s
;
3915 verts
[3].st
[i
][1] = t1
;
3918 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3921 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3923 _mesa_meta_end(ctx
);