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"
81 /** Return offset in bytes of the field within a vertex struct */
82 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
85 * State which we may save/restore across meta ops.
86 * XXX this may be incomplete...
90 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
92 /** MESA_META_ALPHA_TEST */
93 GLboolean AlphaEnabled
;
97 /** MESA_META_BLEND */
98 GLbitfield BlendEnabled
;
99 GLboolean ColorLogicOpEnabled
;
101 /** MESA_META_COLOR_MASK */
102 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
104 /** MESA_META_DEPTH_TEST */
105 struct gl_depthbuffer_attrib Depth
;
110 /** MESA_META_PIXEL_STORE */
111 struct gl_pixelstore_attrib Pack
, Unpack
;
113 /** MESA_META_PIXEL_TRANSFER */
114 GLfloat RedBias
, RedScale
;
115 GLfloat GreenBias
, GreenScale
;
116 GLfloat BlueBias
, BlueScale
;
117 GLfloat AlphaBias
, AlphaScale
;
118 GLfloat DepthBias
, DepthScale
;
119 GLboolean MapColorFlag
;
121 /** MESA_META_RASTERIZATION */
122 GLenum FrontPolygonMode
, BackPolygonMode
;
123 GLboolean PolygonOffset
;
124 GLboolean PolygonSmooth
;
125 GLboolean PolygonStipple
;
126 GLboolean PolygonCull
;
128 /** MESA_META_SCISSOR */
129 struct gl_scissor_attrib Scissor
;
131 /** MESA_META_SHADER */
132 GLboolean VertexProgramEnabled
;
133 struct gl_vertex_program
*VertexProgram
;
134 GLboolean FragmentProgramEnabled
;
135 struct gl_fragment_program
*FragmentProgram
;
136 struct gl_shader_program
*VertexShader
;
137 struct gl_shader_program
*GeometryShader
;
138 struct gl_shader_program
*FragmentShader
;
139 struct gl_shader_program
*ActiveShader
;
141 /** MESA_META_STENCIL_TEST */
142 struct gl_stencil_attrib Stencil
;
144 /** MESA_META_TRANSFORM */
146 GLfloat ModelviewMatrix
[16];
147 GLfloat ProjectionMatrix
[16];
148 GLfloat TextureMatrix
[16];
150 /** MESA_META_CLIP */
151 GLbitfield ClipPlanesEnabled
;
153 /** MESA_META_TEXTURE */
155 GLuint ClientActiveUnit
;
156 /** for unit[0] only */
157 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
158 /** mask of TEXTURE_2D_BIT, etc */
159 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
160 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
161 GLuint EnvMode
; /* unit[0] only */
163 /** MESA_META_VERTEX */
164 struct gl_array_object
*ArrayObj
;
165 struct gl_buffer_object
*ArrayBufferObj
;
167 /** MESA_META_VIEWPORT */
168 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
169 GLclampd DepthNear
, DepthFar
;
171 /** MESA_META_CLAMP_FRAGMENT_COLOR */
172 GLenum ClampFragmentColor
;
174 /** MESA_META_CLAMP_VERTEX_COLOR */
175 GLenum ClampVertexColor
;
177 /** MESA_META_CONDITIONAL_RENDER */
178 struct gl_query_object
*CondRenderQuery
;
179 GLenum CondRenderMode
;
181 /** MESA_META_SELECT_FEEDBACK */
183 struct gl_selection Select
;
184 struct gl_feedback Feedback
;
186 /** MESA_META_MULTISAMPLE */
187 GLboolean MultisampleEnabled
;
189 /** MESA_META_FRAMEBUFFER_SRGB */
190 GLboolean sRGBEnabled
;
192 /** Miscellaneous (always disabled) */
194 GLboolean RasterDiscard
;
195 GLboolean TransformFeedbackNeedsResume
;
199 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
200 * This is currently shared by all the meta ops. But we could create a
201 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
206 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
207 GLsizei MinSize
; /**< Min texture size to allocate */
208 GLsizei MaxSize
; /**< Max possible texture size */
209 GLboolean NPOT
; /**< Non-power of two size OK? */
210 GLsizei Width
, Height
; /**< Current texture size */
212 GLfloat Sright
, Ttop
; /**< right, top texcoords */
217 * State for glBlitFramebufer()
228 * State for glClear()
237 GLuint IntegerShaderProg
;
238 GLint IntegerColorLocation
;
243 * State for glCopyPixels()
253 * State for glDrawPixels()
259 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
260 GLuint DepthFP
; /**< Fragment program for drawing depth images */
265 * State for glBitmap()
271 struct temp_texture Tex
; /**< separate texture from other meta ops */
275 * State for GLSL texture sampler which is used to generate fragment
276 * shader in _mesa_meta_generate_mipmap().
278 struct glsl_sampler
{
281 const char *texcoords
;
286 * State for _mesa_meta_generate_mipmap()
288 struct gen_mipmap_state
295 struct glsl_sampler sampler_1d
;
296 struct glsl_sampler sampler_2d
;
297 struct glsl_sampler sampler_3d
;
298 struct glsl_sampler sampler_cubemap
;
299 struct glsl_sampler sampler_1d_array
;
300 struct glsl_sampler sampler_2d_array
;
304 * State for texture decompression
306 struct decompress_state
309 GLuint VBO
, FBO
, RBO
, Sampler
;
314 * State for glDrawTex()
322 #define MAX_META_OPS_DEPTH 8
324 * All per-context meta state.
328 /** Stack of state saved during meta-ops */
329 struct save_state Save
[MAX_META_OPS_DEPTH
];
330 /** Save stack depth */
331 GLuint SaveStackDepth
;
333 struct temp_texture TempTex
;
335 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
336 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
337 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
338 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
339 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
340 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
341 struct decompress_state Decompress
; /**< For texture decompression */
342 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
345 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
346 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
347 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
348 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
349 struct gen_mipmap_state
*mipmap
);
352 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
358 shader
= _mesa_CreateShaderObjectARB(target
);
359 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
360 _mesa_CompileShader(shader
);
362 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
366 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
368 _mesa_DeleteObjectARB(shader
);
374 _mesa_DeleteObjectARB(shader
);
378 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
380 "meta program compile failed:\n%s\n"
385 _mesa_DeleteObjectARB(shader
);
391 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
396 _mesa_LinkProgram(program
);
398 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
402 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
410 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
411 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
419 * Initialize meta-ops for a context.
420 * To be called once during context creation.
423 _mesa_meta_init(struct gl_context
*ctx
)
427 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
432 * Free context meta-op state.
433 * To be called once during context destruction.
436 _mesa_meta_free(struct gl_context
*ctx
)
438 GET_CURRENT_CONTEXT(old_context
);
439 _mesa_make_current(ctx
, NULL
, NULL
);
440 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
441 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
442 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
443 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
445 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
447 _mesa_make_current(NULL
, NULL
, NULL
);
454 * Enter meta state. This is like a light-weight version of glPushAttrib
455 * but it also resets most GL state back to default values.
457 * \param state bitmask of MESA_META_* flags indicating which attribute groups
458 * to save and reset to their defaults
461 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
463 struct save_state
*save
;
465 /* hope MAX_META_OPS_DEPTH is large enough */
466 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
468 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
469 memset(save
, 0, sizeof(*save
));
470 save
->SavedState
= state
;
472 /* Pausing transform feedback needs to be done early, or else we won't be
473 * able to change other state.
475 save
->TransformFeedbackNeedsResume
=
476 ctx
->TransformFeedback
.CurrentObject
->Active
&&
477 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
478 if (save
->TransformFeedbackNeedsResume
)
479 _mesa_PauseTransformFeedback();
481 if (state
& MESA_META_ALPHA_TEST
) {
482 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
483 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
484 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
485 if (ctx
->Color
.AlphaEnabled
)
486 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
489 if (state
& MESA_META_BLEND
) {
490 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
491 if (ctx
->Color
.BlendEnabled
) {
492 if (ctx
->Extensions
.EXT_draw_buffers2
) {
494 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
495 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
499 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
502 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
503 if (ctx
->Color
.ColorLogicOpEnabled
)
504 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
507 if (state
& MESA_META_COLOR_MASK
) {
508 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
509 sizeof(ctx
->Color
.ColorMask
));
510 if (!ctx
->Color
.ColorMask
[0][0] ||
511 !ctx
->Color
.ColorMask
[0][1] ||
512 !ctx
->Color
.ColorMask
[0][2] ||
513 !ctx
->Color
.ColorMask
[0][3])
514 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
517 if (state
& MESA_META_DEPTH_TEST
) {
518 save
->Depth
= ctx
->Depth
; /* struct copy */
520 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
523 if ((state
& MESA_META_FOG
)
524 && ctx
->API
!= API_OPENGL_CORE
525 && ctx
->API
!= API_OPENGLES2
) {
526 save
->Fog
= ctx
->Fog
.Enabled
;
527 if (ctx
->Fog
.Enabled
)
528 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
531 if (state
& MESA_META_PIXEL_STORE
) {
532 save
->Pack
= ctx
->Pack
;
533 save
->Unpack
= ctx
->Unpack
;
534 ctx
->Pack
= ctx
->DefaultPacking
;
535 ctx
->Unpack
= ctx
->DefaultPacking
;
538 if (state
& MESA_META_PIXEL_TRANSFER
) {
539 save
->RedScale
= ctx
->Pixel
.RedScale
;
540 save
->RedBias
= ctx
->Pixel
.RedBias
;
541 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
542 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
543 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
544 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
545 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
546 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
547 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
548 ctx
->Pixel
.RedScale
= 1.0F
;
549 ctx
->Pixel
.RedBias
= 0.0F
;
550 ctx
->Pixel
.GreenScale
= 1.0F
;
551 ctx
->Pixel
.GreenBias
= 0.0F
;
552 ctx
->Pixel
.BlueScale
= 1.0F
;
553 ctx
->Pixel
.BlueBias
= 0.0F
;
554 ctx
->Pixel
.AlphaScale
= 1.0F
;
555 ctx
->Pixel
.AlphaBias
= 0.0F
;
556 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
558 ctx
->NewState
|=_NEW_PIXEL
;
561 if (state
& MESA_META_RASTERIZATION
) {
562 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
563 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
564 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
565 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
566 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
567 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
568 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
569 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
570 if (ctx
->API
== API_OPENGL_COMPAT
) {
571 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
572 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
574 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
577 if (state
& MESA_META_SCISSOR
) {
578 save
->Scissor
= ctx
->Scissor
; /* struct copy */
579 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
582 if (state
& MESA_META_SHADER
) {
583 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
584 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
585 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
586 ctx
->VertexProgram
.Current
);
587 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
590 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
591 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
592 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
593 ctx
->FragmentProgram
.Current
);
594 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
597 if (ctx
->Extensions
.ARB_shader_objects
) {
598 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
599 ctx
->Shader
.CurrentVertexProgram
);
600 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
601 ctx
->Shader
.CurrentGeometryProgram
);
602 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
603 ctx
->Shader
.CurrentFragmentProgram
);
604 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
605 ctx
->Shader
.ActiveProgram
);
611 if (state
& MESA_META_STENCIL_TEST
) {
612 save
->Stencil
= ctx
->Stencil
; /* struct copy */
613 if (ctx
->Stencil
.Enabled
)
614 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
615 /* NOTE: other stencil state not reset */
618 if (state
& MESA_META_TEXTURE
) {
621 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
622 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
623 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
625 /* Disable all texture units */
626 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
627 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
628 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
629 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
630 if (ctx
->Texture
.Unit
[u
].Enabled
||
631 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
632 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
633 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
634 if (ctx
->Extensions
.ARB_texture_cube_map
)
635 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
636 if (ctx
->Extensions
.OES_EGL_image_external
)
637 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
639 if (ctx
->API
== API_OPENGL_COMPAT
) {
640 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
641 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
642 if (ctx
->Extensions
.NV_texture_rectangle
)
643 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
644 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
645 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
646 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
647 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
649 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
655 /* save current texture objects for unit[0] only */
656 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
657 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
658 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
661 /* set defaults for unit[0] */
662 _mesa_ActiveTexture(GL_TEXTURE0
);
663 _mesa_ClientActiveTexture(GL_TEXTURE0
);
664 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
665 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
669 if (state
& MESA_META_TRANSFORM
) {
670 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
671 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
672 16 * sizeof(GLfloat
));
673 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
674 16 * sizeof(GLfloat
));
675 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
676 16 * sizeof(GLfloat
));
677 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
678 /* set 1:1 vertex:pixel coordinate transform */
679 _mesa_ActiveTexture(GL_TEXTURE0
);
680 _mesa_MatrixMode(GL_TEXTURE
);
681 _mesa_LoadIdentity();
682 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
683 _mesa_MatrixMode(GL_MODELVIEW
);
684 _mesa_LoadIdentity();
685 _mesa_MatrixMode(GL_PROJECTION
);
686 _mesa_LoadIdentity();
687 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
688 0.0, ctx
->DrawBuffer
->Height
,
692 if (state
& MESA_META_CLIP
) {
693 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
694 if (ctx
->Transform
.ClipPlanesEnabled
) {
696 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
697 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
702 if (state
& MESA_META_VERTEX
) {
703 /* save vertex array object state */
704 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
705 ctx
->Array
.ArrayObj
);
706 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
707 ctx
->Array
.ArrayBufferObj
);
708 /* set some default state? */
711 if (state
& MESA_META_VIEWPORT
) {
712 /* save viewport state */
713 save
->ViewportX
= ctx
->Viewport
.X
;
714 save
->ViewportY
= ctx
->Viewport
.Y
;
715 save
->ViewportW
= ctx
->Viewport
.Width
;
716 save
->ViewportH
= ctx
->Viewport
.Height
;
717 /* set viewport to match window size */
718 if (ctx
->Viewport
.X
!= 0 ||
719 ctx
->Viewport
.Y
!= 0 ||
720 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
721 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
722 _mesa_set_viewport(ctx
, 0, 0,
723 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
725 /* save depth range state */
726 save
->DepthNear
= ctx
->Viewport
.Near
;
727 save
->DepthFar
= ctx
->Viewport
.Far
;
728 /* set depth range to default */
729 _mesa_DepthRange(0.0, 1.0);
732 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
733 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
735 /* Generally in here we want to do clamping according to whether
736 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
737 * regardless of the internal implementation of the metaops.
739 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
740 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
743 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
744 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
746 /* Generally in here we never want vertex color clamping --
747 * result clamping is only dependent on fragment clamping.
749 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
752 if (state
& MESA_META_CONDITIONAL_RENDER
) {
753 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
754 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
756 if (ctx
->Query
.CondRenderQuery
)
757 _mesa_EndConditionalRender();
760 if (state
& MESA_META_SELECT_FEEDBACK
) {
761 save
->RenderMode
= ctx
->RenderMode
;
762 if (ctx
->RenderMode
== GL_SELECT
) {
763 save
->Select
= ctx
->Select
; /* struct copy */
764 _mesa_RenderMode(GL_RENDER
);
765 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
766 save
->Feedback
= ctx
->Feedback
; /* struct copy */
767 _mesa_RenderMode(GL_RENDER
);
771 if (state
& MESA_META_MULTISAMPLE
) {
772 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
773 if (ctx
->Multisample
.Enabled
)
774 _mesa_set_multisample(ctx
, GL_FALSE
);
777 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
778 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
779 if (ctx
->Color
.sRGBEnabled
)
780 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
785 save
->Lighting
= ctx
->Light
.Enabled
;
786 if (ctx
->Light
.Enabled
)
787 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
788 save
->RasterDiscard
= ctx
->RasterDiscard
;
789 if (ctx
->RasterDiscard
)
790 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
796 * Leave meta state. This is like a light-weight version of glPopAttrib().
799 _mesa_meta_end(struct gl_context
*ctx
)
801 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
802 const GLbitfield state
= save
->SavedState
;
804 if (state
& MESA_META_ALPHA_TEST
) {
805 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
806 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
807 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
810 if (state
& MESA_META_BLEND
) {
811 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
812 if (ctx
->Extensions
.EXT_draw_buffers2
) {
814 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
815 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
819 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
822 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
823 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
826 if (state
& MESA_META_COLOR_MASK
) {
828 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
829 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
831 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
832 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
836 save
->ColorMask
[i
][0],
837 save
->ColorMask
[i
][1],
838 save
->ColorMask
[i
][2],
839 save
->ColorMask
[i
][3]);
845 if (state
& MESA_META_DEPTH_TEST
) {
846 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
847 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
848 _mesa_DepthFunc(save
->Depth
.Func
);
849 _mesa_DepthMask(save
->Depth
.Mask
);
852 if ((state
& MESA_META_FOG
)
853 && ctx
->API
!= API_OPENGL_CORE
854 && ctx
->API
!= API_OPENGLES2
) {
855 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
858 if (state
& MESA_META_PIXEL_STORE
) {
859 ctx
->Pack
= save
->Pack
;
860 ctx
->Unpack
= save
->Unpack
;
863 if (state
& MESA_META_PIXEL_TRANSFER
) {
864 ctx
->Pixel
.RedScale
= save
->RedScale
;
865 ctx
->Pixel
.RedBias
= save
->RedBias
;
866 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
867 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
868 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
869 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
870 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
871 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
872 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
874 ctx
->NewState
|=_NEW_PIXEL
;
877 if (state
& MESA_META_RASTERIZATION
) {
878 /* Core context requires that front and back mode be the same.
880 if (ctx
->API
== API_OPENGL_CORE
) {
881 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
883 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
884 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
886 if (ctx
->API
== API_OPENGL_COMPAT
) {
887 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
888 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
890 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
891 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
894 if (state
& MESA_META_SCISSOR
) {
895 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
896 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
897 save
->Scissor
.Width
, save
->Scissor
.Height
);
900 if (state
& MESA_META_SHADER
) {
901 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
902 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
903 save
->VertexProgramEnabled
);
904 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
905 save
->VertexProgram
);
906 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
909 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
910 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
911 save
->FragmentProgramEnabled
);
912 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
913 save
->FragmentProgram
);
914 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
917 if (ctx
->Extensions
.ARB_vertex_shader
)
918 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
920 if (ctx
->Extensions
.ARB_geometry_shader4
)
921 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
922 save
->GeometryShader
);
924 if (ctx
->Extensions
.ARB_fragment_shader
)
925 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
926 save
->FragmentShader
);
928 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
931 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
932 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
933 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
934 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
937 if (state
& MESA_META_STENCIL_TEST
) {
938 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
940 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
941 _mesa_ClearStencil(stencil
->Clear
);
942 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
943 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
944 stencil
->TestTwoSide
);
945 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
946 ? GL_BACK
: GL_FRONT
);
949 _mesa_StencilFuncSeparate(GL_FRONT
,
950 stencil
->Function
[0],
952 stencil
->ValueMask
[0]);
953 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
954 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
955 stencil
->ZFailFunc
[0],
956 stencil
->ZPassFunc
[0]);
958 _mesa_StencilFuncSeparate(GL_BACK
,
959 stencil
->Function
[1],
961 stencil
->ValueMask
[1]);
962 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
963 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
964 stencil
->ZFailFunc
[1],
965 stencil
->ZPassFunc
[1]);
968 if (state
& MESA_META_TEXTURE
) {
971 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
973 /* restore texenv for unit[0] */
974 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
975 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
978 /* restore texture objects for unit[0] only */
979 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
980 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
981 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
982 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
983 save
->CurrentTexture
[tgt
]);
985 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
988 /* Restore fixed function texture enables, texgen */
989 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
990 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
991 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
992 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
993 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
996 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
997 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
998 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1003 /* restore current unit state */
1004 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1005 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1008 if (state
& MESA_META_TRANSFORM
) {
1009 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1010 _mesa_ActiveTexture(GL_TEXTURE0
);
1011 _mesa_MatrixMode(GL_TEXTURE
);
1012 _mesa_LoadMatrixf(save
->TextureMatrix
);
1013 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1015 _mesa_MatrixMode(GL_MODELVIEW
);
1016 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1018 _mesa_MatrixMode(GL_PROJECTION
);
1019 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1021 _mesa_MatrixMode(save
->MatrixMode
);
1024 if (state
& MESA_META_CLIP
) {
1025 if (save
->ClipPlanesEnabled
) {
1027 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1028 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1029 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1035 if (state
& MESA_META_VERTEX
) {
1036 /* restore vertex buffer object */
1037 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1038 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1040 /* restore vertex array object */
1041 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1042 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1045 if (state
& MESA_META_VIEWPORT
) {
1046 if (save
->ViewportX
!= ctx
->Viewport
.X
||
1047 save
->ViewportY
!= ctx
->Viewport
.Y
||
1048 save
->ViewportW
!= ctx
->Viewport
.Width
||
1049 save
->ViewportH
!= ctx
->Viewport
.Height
) {
1050 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
1051 save
->ViewportW
, save
->ViewportH
);
1053 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1056 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
1057 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1060 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
1061 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1064 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1065 if (save
->CondRenderQuery
)
1066 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1067 save
->CondRenderMode
);
1070 if (state
& MESA_META_SELECT_FEEDBACK
) {
1071 if (save
->RenderMode
== GL_SELECT
) {
1072 _mesa_RenderMode(GL_SELECT
);
1073 ctx
->Select
= save
->Select
;
1074 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1075 _mesa_RenderMode(GL_FEEDBACK
);
1076 ctx
->Feedback
= save
->Feedback
;
1080 if (state
& MESA_META_MULTISAMPLE
) {
1081 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1082 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1085 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1086 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1087 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1091 if (save
->Lighting
) {
1092 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1094 if (save
->RasterDiscard
) {
1095 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1097 if (save
->TransformFeedbackNeedsResume
)
1098 _mesa_ResumeTransformFeedback();
1100 ctx
->Meta
->SaveStackDepth
--;
1105 * Determine whether Mesa is currently in a meta state.
1108 _mesa_meta_in_progress(struct gl_context
*ctx
)
1110 return ctx
->Meta
->SaveStackDepth
!= 0;
1115 * Convert Z from a normalized value in the range [0, 1] to an object-space
1116 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1117 * default/identity ortho projection results in the original Z value.
1118 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1119 * value comes from the clear value or raster position.
1121 static INLINE GLfloat
1122 invert_z(GLfloat normZ
)
1124 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1130 * One-time init for a temp_texture object.
1131 * Choose tex target, compute max tex size, etc.
1134 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1136 /* prefer texture rectangle */
1137 if (ctx
->Extensions
.NV_texture_rectangle
) {
1138 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1139 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1140 tex
->NPOT
= GL_TRUE
;
1143 /* use 2D texture, NPOT if possible */
1144 tex
->Target
= GL_TEXTURE_2D
;
1145 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1146 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1148 tex
->MinSize
= 16; /* 16 x 16 at least */
1149 assert(tex
->MaxSize
> 0);
1151 _mesa_GenTextures(1, &tex
->TexObj
);
1155 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1159 _mesa_DeleteTextures(1, &tex
->TexObj
);
1165 * Return pointer to temp_texture info for non-bitmap ops.
1166 * This does some one-time init if needed.
1168 static struct temp_texture
*
1169 get_temp_texture(struct gl_context
*ctx
)
1171 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1174 init_temp_texture(ctx
, tex
);
1182 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1183 * We use a separate texture for bitmaps to reduce texture
1184 * allocation/deallocation.
1186 static struct temp_texture
*
1187 get_bitmap_temp_texture(struct gl_context
*ctx
)
1189 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1192 init_temp_texture(ctx
, tex
);
1200 * Compute the width/height of texture needed to draw an image of the
1201 * given size. Return a flag indicating whether the current texture
1202 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1203 * allocated (glTexImage2D).
1204 * Also, compute s/t texcoords for drawing.
1206 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1209 alloc_texture(struct temp_texture
*tex
,
1210 GLsizei width
, GLsizei height
, GLenum intFormat
)
1212 GLboolean newTex
= GL_FALSE
;
1214 ASSERT(width
<= tex
->MaxSize
);
1215 ASSERT(height
<= tex
->MaxSize
);
1217 if (width
> tex
->Width
||
1218 height
> tex
->Height
||
1219 intFormat
!= tex
->IntFormat
) {
1220 /* alloc new texture (larger or different format) */
1223 /* use non-power of two size */
1224 tex
->Width
= MAX2(tex
->MinSize
, width
);
1225 tex
->Height
= MAX2(tex
->MinSize
, height
);
1228 /* find power of two size */
1230 w
= h
= tex
->MinSize
;
1239 tex
->IntFormat
= intFormat
;
1244 /* compute texcoords */
1245 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1246 tex
->Sright
= (GLfloat
) width
;
1247 tex
->Ttop
= (GLfloat
) height
;
1250 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1251 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1259 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1262 setup_copypix_texture(struct temp_texture
*tex
,
1264 GLint srcX
, GLint srcY
,
1265 GLsizei width
, GLsizei height
, GLenum intFormat
,
1268 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1269 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1270 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1271 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1273 /* copy framebuffer image to texture */
1275 /* create new tex image */
1276 if (tex
->Width
== width
&& tex
->Height
== height
) {
1277 /* create new tex with framebuffer data */
1278 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1279 srcX
, srcY
, width
, height
, 0);
1282 /* create empty texture */
1283 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1284 tex
->Width
, tex
->Height
, 0,
1285 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1287 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1288 0, 0, srcX
, srcY
, width
, height
);
1292 /* replace existing tex image */
1293 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1294 0, 0, srcX
, srcY
, width
, height
);
1300 * Setup/load texture for glDrawPixels.
1303 setup_drawpix_texture(struct gl_context
*ctx
,
1304 struct temp_texture
*tex
,
1306 GLenum texIntFormat
,
1307 GLsizei width
, GLsizei height
,
1308 GLenum format
, GLenum type
,
1309 const GLvoid
*pixels
)
1311 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1312 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1313 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1314 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1316 /* copy pixel data to texture */
1318 /* create new tex image */
1319 if (tex
->Width
== width
&& tex
->Height
== height
) {
1320 /* create new tex and load image data */
1321 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1322 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1325 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1327 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1328 ctx
->Unpack
.BufferObj
);
1329 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1330 /* create empty texture */
1331 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1332 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1333 if (save_unpack_obj
!= NULL
)
1334 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1335 save_unpack_obj
->Name
);
1337 _mesa_TexSubImage2D(tex
->Target
, 0,
1338 0, 0, width
, height
, format
, type
, pixels
);
1342 /* replace existing tex image */
1343 _mesa_TexSubImage2D(tex
->Target
, 0,
1344 0, 0, width
, height
, format
, type
, pixels
);
1351 * One-time init for drawing depth pixels.
1354 init_blit_depth_pixels(struct gl_context
*ctx
)
1356 static const char *program
=
1358 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1361 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1362 struct temp_texture
*tex
= get_temp_texture(ctx
);
1363 const char *texTarget
;
1365 assert(blit
->DepthFP
== 0);
1367 /* replace %s with "RECT" or "2D" */
1368 assert(strlen(program
) + 4 < sizeof(program2
));
1369 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1373 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1375 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1376 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1377 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1378 strlen(program2
), (const GLubyte
*) program2
);
1383 * Try to do a glBlitFramebuffer using no-copy texturing.
1384 * We can do this when the src renderbuffer is actually a texture.
1385 * But if the src buffer == dst buffer we cannot do this.
1387 * \return new buffer mask indicating the buffers left to blit using the
1391 blitframebuffer_texture(struct gl_context
*ctx
,
1392 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1393 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1394 GLbitfield mask
, GLenum filter
)
1396 if (mask
& GL_COLOR_BUFFER_BIT
) {
1397 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1398 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1399 const struct gl_renderbuffer_attachment
*drawAtt
=
1400 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1401 const struct gl_renderbuffer_attachment
*readAtt
=
1402 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1404 if (readAtt
&& readAtt
->Texture
) {
1405 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1406 const GLuint srcLevel
= readAtt
->TextureLevel
;
1407 const GLint baseLevelSave
= texObj
->BaseLevel
;
1408 const GLint maxLevelSave
= texObj
->MaxLevel
;
1409 const GLenum target
= texObj
->Target
;
1410 GLuint sampler
, samplerSave
=
1411 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1412 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1414 if (drawAtt
->Texture
== readAtt
->Texture
) {
1415 /* Can't use same texture as both the source and dest. We need
1416 * to handle overlapping blits and besides, some hw may not
1422 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1423 /* Can't handle other texture types at this time */
1427 _mesa_GenSamplers(1, &sampler
);
1428 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1431 printf("Blit from texture!\n");
1432 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1433 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1436 /* Prepare src texture state */
1437 _mesa_BindTexture(target
, texObj
->Name
);
1438 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1439 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1440 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1441 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1442 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1444 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1445 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1447 /* Always do our blits with no sRGB decode or encode. Note that
1448 * GL_FRAMEBUFFER_SRGB has already been disabled by
1449 * _mesa_meta_begin().
1451 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1452 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1453 GL_SKIP_DECODE_EXT
);
1456 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1457 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1459 /* Prepare vertex data (the VBO was previously created and bound) */
1464 struct vertex verts
[4];
1465 GLfloat s0
, t0
, s1
, t1
;
1467 if (target
== GL_TEXTURE_2D
) {
1468 const struct gl_texture_image
*texImage
1469 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1470 s0
= srcX0
/ (float) texImage
->Width
;
1471 s1
= srcX1
/ (float) texImage
->Width
;
1472 t0
= srcY0
/ (float) texImage
->Height
;
1473 t1
= srcY1
/ (float) texImage
->Height
;
1476 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1483 verts
[0].x
= (GLfloat
) dstX0
;
1484 verts
[0].y
= (GLfloat
) dstY0
;
1485 verts
[1].x
= (GLfloat
) dstX1
;
1486 verts
[1].y
= (GLfloat
) dstY0
;
1487 verts
[2].x
= (GLfloat
) dstX1
;
1488 verts
[2].y
= (GLfloat
) dstY1
;
1489 verts
[3].x
= (GLfloat
) dstX0
;
1490 verts
[3].y
= (GLfloat
) dstY1
;
1501 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1504 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1506 /* Restore texture object state, the texture binding will
1507 * be restored by _mesa_meta_end().
1509 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1510 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1511 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1514 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1515 _mesa_DeleteSamplers(1, &sampler
);
1517 /* Done with color buffer */
1518 mask
&= ~GL_COLOR_BUFFER_BIT
;
1527 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1528 * of texture mapping and polygon rendering.
1531 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1532 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1533 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1534 GLbitfield mask
, GLenum filter
)
1536 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1537 struct temp_texture
*tex
= get_temp_texture(ctx
);
1538 const GLsizei maxTexSize
= tex
->MaxSize
;
1539 const GLint srcX
= MIN2(srcX0
, srcX1
);
1540 const GLint srcY
= MIN2(srcY0
, srcY1
);
1541 const GLint srcW
= abs(srcX1
- srcX0
);
1542 const GLint srcH
= abs(srcY1
- srcY0
);
1543 const GLboolean srcFlipX
= srcX1
< srcX0
;
1544 const GLboolean srcFlipY
= srcY1
< srcY0
;
1548 struct vertex verts
[4];
1551 /* In addition to falling back if the blit size is larger than the maximum
1552 * texture size, fallback if the source is multisampled. This fallback can
1553 * be removed once Mesa gets support ARB_texture_multisample.
1555 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1556 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1557 /* XXX avoid this fallback */
1558 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1559 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1575 /* only scissor effects blit so save/clear all other relevant state */
1576 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1578 if (blit
->ArrayObj
== 0) {
1579 /* one-time setup */
1581 /* create vertex array object */
1582 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1583 _mesa_BindVertexArray(blit
->ArrayObj
);
1585 /* create vertex array buffer */
1586 _mesa_GenBuffers(1, &blit
->VBO
);
1587 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1588 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1589 NULL
, GL_DYNAMIC_DRAW_ARB
);
1591 /* setup vertex arrays */
1592 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1593 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1594 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1595 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1598 _mesa_BindVertexArray(blit
->ArrayObj
);
1599 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1602 /* Try faster, direct texture approach first */
1603 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1604 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1606 _mesa_meta_end(ctx
);
1610 /* Continue with "normal" approach which involves copying the src rect
1611 * into a temporary texture and is "blitted" by drawing a textured quad.
1614 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1616 /* vertex positions/texcoords (after texture allocation!) */
1618 verts
[0].x
= (GLfloat
) dstX0
;
1619 verts
[0].y
= (GLfloat
) dstY0
;
1620 verts
[1].x
= (GLfloat
) dstX1
;
1621 verts
[1].y
= (GLfloat
) dstY0
;
1622 verts
[2].x
= (GLfloat
) dstX1
;
1623 verts
[2].y
= (GLfloat
) dstY1
;
1624 verts
[3].x
= (GLfloat
) dstX0
;
1625 verts
[3].y
= (GLfloat
) dstY1
;
1629 verts
[1].s
= tex
->Sright
;
1631 verts
[2].s
= tex
->Sright
;
1632 verts
[2].t
= tex
->Ttop
;
1634 verts
[3].t
= tex
->Ttop
;
1636 /* upload new vertex data */
1637 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1640 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1642 if (mask
& GL_COLOR_BUFFER_BIT
) {
1643 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1645 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1646 mask
&= ~GL_COLOR_BUFFER_BIT
;
1649 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1650 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1653 init_blit_depth_pixels(ctx
);
1655 /* maybe change tex format here */
1656 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1658 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1659 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1661 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1662 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1664 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1665 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1666 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1667 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1668 _mesa_DepthFunc(GL_ALWAYS
);
1669 _mesa_DepthMask(GL_TRUE
);
1671 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1672 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1678 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1679 /* XXX can't easily do stencil */
1682 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1684 _mesa_meta_end(ctx
);
1687 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1688 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1693 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1695 if (blit
->ArrayObj
) {
1696 _mesa_DeleteVertexArrays(1, &blit
->ArrayObj
);
1698 _mesa_DeleteBuffers(1, &blit
->VBO
);
1701 if (blit
->DepthFP
) {
1702 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
1709 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1712 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1714 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1716 GLfloat x
, y
, z
, r
, g
, b
, a
;
1718 struct vertex verts
[4];
1719 /* save all state but scissor, pixel pack/unpack */
1720 GLbitfield metaSave
= (MESA_META_ALL
-
1722 MESA_META_PIXEL_STORE
-
1723 MESA_META_CONDITIONAL_RENDER
-
1724 MESA_META_FRAMEBUFFER_SRGB
);
1725 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1727 if (buffers
& BUFFER_BITS_COLOR
) {
1728 /* if clearing color buffers, don't save/restore colormask */
1729 metaSave
-= MESA_META_COLOR_MASK
;
1732 _mesa_meta_begin(ctx
, metaSave
);
1734 if (clear
->ArrayObj
== 0) {
1735 /* one-time setup */
1737 /* create vertex array object */
1738 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1739 _mesa_BindVertexArray(clear
->ArrayObj
);
1741 /* create vertex array buffer */
1742 _mesa_GenBuffers(1, &clear
->VBO
);
1743 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1745 /* setup vertex arrays */
1746 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1747 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1748 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1749 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1752 _mesa_BindVertexArray(clear
->ArrayObj
);
1753 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1756 /* GL_COLOR_BUFFER_BIT */
1757 if (buffers
& BUFFER_BITS_COLOR
) {
1758 /* leave colormask, glDrawBuffer state as-is */
1760 /* Clears never have the color clamped. */
1761 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1764 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1765 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1768 /* GL_DEPTH_BUFFER_BIT */
1769 if (buffers
& BUFFER_BIT_DEPTH
) {
1770 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1771 _mesa_DepthFunc(GL_ALWAYS
);
1772 _mesa_DepthMask(GL_TRUE
);
1775 assert(!ctx
->Depth
.Test
);
1778 /* GL_STENCIL_BUFFER_BIT */
1779 if (buffers
& BUFFER_BIT_STENCIL
) {
1780 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1781 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1782 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1783 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1784 ctx
->Stencil
.Clear
& stencilMax
,
1785 ctx
->Stencil
.WriteMask
[0]);
1788 assert(!ctx
->Stencil
.Enabled
);
1791 /* vertex positions/colors */
1793 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1794 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1795 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1796 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1797 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1814 for (i
= 0; i
< 4; i
++) {
1815 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1816 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1817 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1818 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1821 /* upload new vertex data */
1822 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1823 GL_DYNAMIC_DRAW_ARB
);
1827 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1829 _mesa_meta_end(ctx
);
1833 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1835 const char *vs_source
=
1836 "attribute vec4 position;\n"
1839 " gl_Position = position;\n"
1841 const char *fs_source
=
1842 "uniform vec4 color;\n"
1845 " gl_FragColor = color;\n"
1847 const char *vs_int_source
=
1849 "in vec4 position;\n"
1852 " gl_Position = position;\n"
1854 const char *fs_int_source
=
1856 "uniform ivec4 color;\n"
1857 "out ivec4 out_color;\n"
1861 " out_color = color;\n"
1865 if (clear
->ArrayObj
!= 0)
1868 /* create vertex array object */
1869 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1870 _mesa_BindVertexArray(clear
->ArrayObj
);
1872 /* create vertex array buffer */
1873 _mesa_GenBuffers(1, &clear
->VBO
);
1874 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1876 /* setup vertex arrays */
1877 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1878 _mesa_EnableVertexAttribArray(0);
1880 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1881 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
1882 _mesa_CompileShader(vs
);
1884 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1885 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
1886 _mesa_CompileShader(fs
);
1888 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1889 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1890 _mesa_DeleteObjectARB(fs
);
1891 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1892 _mesa_DeleteObjectARB(vs
);
1893 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
1894 _mesa_LinkProgram(clear
->ShaderProg
);
1896 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
1899 if (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) {
1900 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1901 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1903 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1904 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1905 _mesa_DeleteObjectARB(fs
);
1906 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1907 _mesa_DeleteObjectARB(vs
);
1908 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
1910 /* Note that user-defined out attributes get automatically assigned
1911 * locations starting from 0, so we don't need to explicitly
1912 * BindFragDataLocation to 0.
1915 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1917 clear
->IntegerColorLocation
=
1918 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
1923 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1925 if (clear
->ArrayObj
== 0)
1927 _mesa_DeleteVertexArrays(1, &clear
->ArrayObj
);
1928 clear
->ArrayObj
= 0;
1929 _mesa_DeleteBuffers(1, &clear
->VBO
);
1931 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1932 clear
->ShaderProg
= 0;
1934 if (clear
->IntegerShaderProg
) {
1935 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1936 clear
->IntegerShaderProg
= 0;
1941 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1944 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1946 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1947 GLbitfield metaSave
;
1948 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1949 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1950 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1951 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1952 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1953 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1954 const float z
= -invert_z(ctx
->Depth
.Clear
);
1959 metaSave
= (MESA_META_ALPHA_TEST
|
1961 MESA_META_DEPTH_TEST
|
1962 MESA_META_RASTERIZATION
|
1964 MESA_META_STENCIL_TEST
|
1966 MESA_META_VIEWPORT
|
1968 MESA_META_CLAMP_FRAGMENT_COLOR
|
1969 MESA_META_MULTISAMPLE
);
1971 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1972 /* We'll use colormask to disable color writes. Otherwise,
1973 * respect color mask
1975 metaSave
|= MESA_META_COLOR_MASK
;
1978 _mesa_meta_begin(ctx
, metaSave
);
1980 meta_glsl_clear_init(ctx
, clear
);
1982 if (fb
->_IntegerColor
) {
1983 _mesa_UseProgram(clear
->IntegerShaderProg
);
1984 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
1985 ctx
->Color
.ClearColor
.i
);
1987 _mesa_UseProgram(clear
->ShaderProg
);
1988 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
1989 ctx
->Color
.ClearColor
.f
);
1992 _mesa_BindVertexArray(clear
->ArrayObj
);
1993 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1995 /* GL_COLOR_BUFFER_BIT */
1996 if (buffers
& BUFFER_BITS_COLOR
) {
1997 /* leave colormask, glDrawBuffer state as-is */
1999 /* Clears never have the color clamped. */
2000 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2003 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2004 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2007 /* GL_DEPTH_BUFFER_BIT */
2008 if (buffers
& BUFFER_BIT_DEPTH
) {
2009 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2010 _mesa_DepthFunc(GL_ALWAYS
);
2011 _mesa_DepthMask(GL_TRUE
);
2014 assert(!ctx
->Depth
.Test
);
2017 /* GL_STENCIL_BUFFER_BIT */
2018 if (buffers
& BUFFER_BIT_STENCIL
) {
2019 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2020 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2021 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2022 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2023 ctx
->Stencil
.Clear
& stencilMax
,
2024 ctx
->Stencil
.WriteMask
[0]);
2027 assert(!ctx
->Stencil
.Enabled
);
2030 /* vertex positions */
2044 /* upload new vertex data */
2045 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2046 GL_DYNAMIC_DRAW_ARB
);
2049 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2051 _mesa_meta_end(ctx
);
2055 * Meta implementation of ctx->Driver.CopyPixels() in terms
2056 * of texture mapping and polygon rendering and GLSL shaders.
2059 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2060 GLsizei width
, GLsizei height
,
2061 GLint dstX
, GLint dstY
, GLenum type
)
2063 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2064 struct temp_texture
*tex
= get_temp_texture(ctx
);
2066 GLfloat x
, y
, z
, s
, t
;
2068 struct vertex verts
[4];
2070 GLenum intFormat
= GL_RGBA
;
2072 if (type
!= GL_COLOR
||
2073 ctx
->_ImageTransferState
||
2075 width
> tex
->MaxSize
||
2076 height
> tex
->MaxSize
) {
2077 /* XXX avoid this fallback */
2078 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2082 /* Most GL state applies to glCopyPixels, but a there's a few things
2083 * we need to override:
2085 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2088 MESA_META_TRANSFORM
|
2091 MESA_META_VIEWPORT
));
2093 if (copypix
->ArrayObj
== 0) {
2094 /* one-time setup */
2096 /* create vertex array object */
2097 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2098 _mesa_BindVertexArray(copypix
->ArrayObj
);
2100 /* create vertex array buffer */
2101 _mesa_GenBuffers(1, ©pix
->VBO
);
2102 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2103 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2104 NULL
, GL_DYNAMIC_DRAW_ARB
);
2106 /* setup vertex arrays */
2107 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2108 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2109 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2110 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2113 _mesa_BindVertexArray(copypix
->ArrayObj
);
2114 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2117 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2119 /* vertex positions, texcoords (after texture allocation!) */
2121 const GLfloat dstX0
= (GLfloat
) dstX
;
2122 const GLfloat dstY0
= (GLfloat
) dstY
;
2123 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2124 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2125 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2135 verts
[1].s
= tex
->Sright
;
2140 verts
[2].s
= tex
->Sright
;
2141 verts
[2].t
= tex
->Ttop
;
2146 verts
[3].t
= tex
->Ttop
;
2148 /* upload new vertex data */
2149 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2152 /* Alloc/setup texture */
2153 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2154 GL_RGBA
, GL_NEAREST
);
2156 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2158 /* draw textured quad */
2159 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2161 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2163 _mesa_meta_end(ctx
);
2169 * When the glDrawPixels() image size is greater than the max rectangle
2170 * texture size we use this function to break the glDrawPixels() image
2171 * into tiles which fit into the max texture size.
2174 tiled_draw_pixels(struct gl_context
*ctx
,
2176 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2177 GLenum format
, GLenum type
,
2178 const struct gl_pixelstore_attrib
*unpack
,
2179 const GLvoid
*pixels
)
2181 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2184 if (tileUnpack
.RowLength
== 0)
2185 tileUnpack
.RowLength
= width
;
2187 for (i
= 0; i
< width
; i
+= tileSize
) {
2188 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2189 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2191 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2193 for (j
= 0; j
< height
; j
+= tileSize
) {
2194 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2195 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2197 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2199 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2200 format
, type
, &tileUnpack
, pixels
);
2207 * One-time init for drawing stencil pixels.
2210 init_draw_stencil_pixels(struct gl_context
*ctx
)
2212 /* This program is run eight times, once for each stencil bit.
2213 * The stencil values to draw are found in an 8-bit alpha texture.
2214 * We read the texture/stencil value and test if bit 'b' is set.
2215 * If the bit is not set, use KIL to kill the fragment.
2216 * Finally, we use the stencil test to update the stencil buffer.
2218 * The basic algorithm for checking if a bit is set is:
2219 * if (is_odd(value / (1 << bit)))
2220 * result is one (or non-zero).
2223 * The program parameter contains three values:
2224 * parm.x = 255 / (1 << bit)
2228 static const char *program
=
2230 "PARAM parm = program.local[0]; \n"
2232 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2233 "# t = t * 255 / bit \n"
2234 "MUL t.x, t.a, parm.x; \n"
2237 "SUB t.x, t.x, t.y; \n"
2239 "MUL t.x, t.x, parm.y; \n"
2240 "# t = fract(t.x) \n"
2241 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2242 "# t.x = (t.x == 0 ? 1 : 0) \n"
2243 "SGE t.x, -t.x, parm.z; \n"
2245 "# for debug only \n"
2246 "#MOV result.color, t.x; \n"
2248 char program2
[1000];
2249 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2250 struct temp_texture
*tex
= get_temp_texture(ctx
);
2251 const char *texTarget
;
2253 assert(drawpix
->StencilFP
== 0);
2255 /* replace %s with "RECT" or "2D" */
2256 assert(strlen(program
) + 4 < sizeof(program2
));
2257 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2261 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2263 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2264 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2265 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2266 strlen(program2
), (const GLubyte
*) program2
);
2271 * One-time init for drawing depth pixels.
2274 init_draw_depth_pixels(struct gl_context
*ctx
)
2276 static const char *program
=
2278 "PARAM color = program.local[0]; \n"
2279 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2280 "MOV result.color, color; \n"
2283 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2284 struct temp_texture
*tex
= get_temp_texture(ctx
);
2285 const char *texTarget
;
2287 assert(drawpix
->DepthFP
== 0);
2289 /* replace %s with "RECT" or "2D" */
2290 assert(strlen(program
) + 4 < sizeof(program2
));
2291 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2295 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2297 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2298 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2299 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2300 strlen(program2
), (const GLubyte
*) program2
);
2305 * Meta implementation of ctx->Driver.DrawPixels() in terms
2306 * of texture mapping and polygon rendering.
2309 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2310 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2311 GLenum format
, GLenum type
,
2312 const struct gl_pixelstore_attrib
*unpack
,
2313 const GLvoid
*pixels
)
2315 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2316 struct temp_texture
*tex
= get_temp_texture(ctx
);
2317 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2318 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2320 GLfloat x
, y
, z
, s
, t
;
2322 struct vertex verts
[4];
2323 GLenum texIntFormat
;
2324 GLboolean fallback
, newTex
;
2325 GLbitfield metaExtraSave
= 0x0;
2329 * Determine if we can do the glDrawPixels with texture mapping.
2331 fallback
= GL_FALSE
;
2332 if (ctx
->Fog
.Enabled
) {
2336 if (_mesa_is_color_format(format
)) {
2337 /* use more compact format when possible */
2338 /* XXX disable special case for GL_LUMINANCE for now to work around
2339 * apparent i965 driver bug (see bug #23670).
2341 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2342 texIntFormat
= format
;
2344 texIntFormat
= GL_RGBA
;
2346 /* If we're not supposed to clamp the resulting color, then just
2347 * promote our texture to fully float. We could do better by
2348 * just going for the matching set of channels, in floating
2351 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2352 ctx
->Extensions
.ARB_texture_float
)
2353 texIntFormat
= GL_RGBA32F
;
2355 else if (_mesa_is_stencil_format(format
)) {
2356 if (ctx
->Extensions
.ARB_fragment_program
&&
2357 ctx
->Pixel
.IndexShift
== 0 &&
2358 ctx
->Pixel
.IndexOffset
== 0 &&
2359 type
== GL_UNSIGNED_BYTE
) {
2360 /* We'll store stencil as alpha. This only works for GLubyte
2361 * image data because of how incoming values are mapped to alpha
2364 texIntFormat
= GL_ALPHA
;
2365 metaExtraSave
= (MESA_META_COLOR_MASK
|
2366 MESA_META_DEPTH_TEST
|
2367 MESA_META_PIXEL_TRANSFER
|
2369 MESA_META_STENCIL_TEST
);
2375 else if (_mesa_is_depth_format(format
)) {
2376 if (ctx
->Extensions
.ARB_depth_texture
&&
2377 ctx
->Extensions
.ARB_fragment_program
) {
2378 texIntFormat
= GL_DEPTH_COMPONENT
;
2379 metaExtraSave
= (MESA_META_SHADER
);
2390 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2391 format
, type
, unpack
, pixels
);
2396 * Check image size against max texture size, draw as tiles if needed.
2398 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2399 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2400 format
, type
, unpack
, pixels
);
2404 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2405 * but a there's a few things we need to override:
2407 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2410 MESA_META_TRANSFORM
|
2413 MESA_META_VIEWPORT
|
2416 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2418 /* vertex positions, texcoords (after texture allocation!) */
2420 const GLfloat x0
= (GLfloat
) x
;
2421 const GLfloat y0
= (GLfloat
) y
;
2422 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2423 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2424 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2434 verts
[1].s
= tex
->Sright
;
2439 verts
[2].s
= tex
->Sright
;
2440 verts
[2].t
= tex
->Ttop
;
2445 verts
[3].t
= tex
->Ttop
;
2448 if (drawpix
->ArrayObj
== 0) {
2449 /* one-time setup: create vertex array object */
2450 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2452 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2454 /* create vertex array buffer */
2455 _mesa_GenBuffers(1, &vbo
);
2456 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2457 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2458 verts
, GL_DYNAMIC_DRAW_ARB
);
2460 /* setup vertex arrays */
2461 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2462 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2463 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2464 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2466 /* set given unpack params */
2467 ctx
->Unpack
= *unpack
;
2469 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2471 if (_mesa_is_stencil_format(format
)) {
2472 /* Drawing stencil */
2475 if (!drawpix
->StencilFP
)
2476 init_draw_stencil_pixels(ctx
);
2478 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2479 GL_ALPHA
, type
, pixels
);
2481 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2483 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2485 /* set all stencil bits to 0 */
2486 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2487 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2488 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2490 /* set stencil bits to 1 where needed */
2491 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2493 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2494 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2496 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2497 const GLuint mask
= 1 << bit
;
2498 if (mask
& origStencilMask
) {
2499 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2500 _mesa_StencilMask(mask
);
2502 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2503 255.0 / mask
, 0.5, 0.0, 0.0);
2505 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2509 else if (_mesa_is_depth_format(format
)) {
2511 if (!drawpix
->DepthFP
)
2512 init_draw_depth_pixels(ctx
);
2514 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2515 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2517 /* polygon color = current raster color */
2518 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2519 ctx
->Current
.RasterColor
);
2521 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2522 format
, type
, pixels
);
2524 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2528 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2529 format
, type
, pixels
);
2530 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2533 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2535 _mesa_DeleteBuffers(1, &vbo
);
2537 /* restore unpack params */
2538 ctx
->Unpack
= unpackSave
;
2540 _mesa_meta_end(ctx
);
2544 alpha_test_raster_color(struct gl_context
*ctx
)
2546 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2547 GLfloat ref
= ctx
->Color
.AlphaRef
;
2549 switch (ctx
->Color
.AlphaFunc
) {
2555 return alpha
== ref
;
2557 return alpha
<= ref
;
2561 return alpha
!= ref
;
2563 return alpha
>= ref
;
2573 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2574 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2575 * tracker would improve performance a lot.
2578 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2579 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2580 const struct gl_pixelstore_attrib
*unpack
,
2581 const GLubyte
*bitmap1
)
2583 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2584 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2585 const GLenum texIntFormat
= GL_ALPHA
;
2586 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2589 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2591 struct vertex verts
[4];
2596 * Check if swrast fallback is needed.
2598 if (ctx
->_ImageTransferState
||
2599 ctx
->FragmentProgram
._Enabled
||
2601 ctx
->Texture
._EnabledUnits
||
2602 width
> tex
->MaxSize
||
2603 height
> tex
->MaxSize
) {
2604 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2608 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2611 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2612 * but a there's a few things we need to override:
2614 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2615 MESA_META_PIXEL_STORE
|
2616 MESA_META_RASTERIZATION
|
2619 MESA_META_TRANSFORM
|
2622 MESA_META_VIEWPORT
));
2624 if (bitmap
->ArrayObj
== 0) {
2625 /* one-time setup */
2627 /* create vertex array object */
2628 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2629 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2631 /* create vertex array buffer */
2632 _mesa_GenBuffers(1, &bitmap
->VBO
);
2633 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2634 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2635 NULL
, GL_DYNAMIC_DRAW_ARB
);
2637 /* setup vertex arrays */
2638 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2639 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2640 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2641 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2642 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2643 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2646 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2647 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2650 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2652 /* vertex positions, texcoords, colors (after texture allocation!) */
2654 const GLfloat x0
= (GLfloat
) x
;
2655 const GLfloat y0
= (GLfloat
) y
;
2656 const GLfloat x1
= (GLfloat
) (x
+ width
);
2657 const GLfloat y1
= (GLfloat
) (y
+ height
);
2658 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2669 verts
[1].s
= tex
->Sright
;
2674 verts
[2].s
= tex
->Sright
;
2675 verts
[2].t
= tex
->Ttop
;
2680 verts
[3].t
= tex
->Ttop
;
2682 for (i
= 0; i
< 4; i
++) {
2683 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2684 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2685 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2686 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2689 /* upload new vertex data */
2690 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2693 /* choose different foreground/background alpha values */
2694 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2695 bg
= (fg
> 127 ? 0 : 255);
2697 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2699 _mesa_meta_end(ctx
);
2703 bitmap8
= malloc(width
* height
);
2705 memset(bitmap8
, bg
, width
* height
);
2706 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2707 bitmap8
, width
, fg
);
2709 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2711 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2712 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2714 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2715 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2717 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2719 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2724 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2726 _mesa_meta_end(ctx
);
2731 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2732 * software fallback. The fallback path will require that the texture
2733 * images are mapped.
2734 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2737 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2738 struct gl_texture_object
*texObj
)
2740 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2741 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2742 struct gl_texture_image
*baseImage
;
2746 /* check for fallbacks */
2747 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2748 target
== GL_TEXTURE_3D
||
2749 target
== GL_TEXTURE_1D_ARRAY
||
2750 target
== GL_TEXTURE_2D_ARRAY
) {
2754 srcLevel
= texObj
->BaseLevel
;
2755 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2756 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2760 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2761 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2762 /* The texture format is sRGB but we can't turn off sRGB->linear
2763 * texture sample conversion. So we won't be able to generate the
2764 * right colors when rendering. Need to use a fallback.
2770 * Test that we can actually render in the texture's format.
2773 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
2774 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2776 if (target
== GL_TEXTURE_1D
) {
2777 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
2778 GL_COLOR_ATTACHMENT0_EXT
,
2779 target
, texObj
->Name
, srcLevel
);
2782 /* other work is needed to enable 3D mipmap generation */
2783 else if (target
== GL_TEXTURE_3D
) {
2785 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
2786 GL_COLOR_ATTACHMENT0_EXT
,
2787 target
, texObj
->Name
, srcLevel
, zoffset
);
2792 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
2793 GL_COLOR_ATTACHMENT0_EXT
,
2794 target
, texObj
->Name
, srcLevel
);
2797 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
2799 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
2801 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2810 * Compute the texture coordinates for the four vertices of a quad for
2811 * drawing a 2D texture image or slice of a cube/3D texture.
2812 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2813 * \param slice slice of a 1D/2D array texture or 3D texture
2814 * \param width width of the texture image
2815 * \param height height of the texture image
2816 * \param coords0/1/2/3 returns the computed texcoords
2819 setup_texture_coords(GLenum faceTarget
,
2828 static const GLfloat st
[4][2] = {
2829 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2834 switch (faceTarget
) {
2838 case GL_TEXTURE_2D_ARRAY
:
2839 if (faceTarget
== GL_TEXTURE_3D
)
2841 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2845 coords0
[0] = 0.0F
; /* s */
2846 coords0
[1] = 0.0F
; /* t */
2847 coords0
[2] = r
; /* r */
2858 case GL_TEXTURE_RECTANGLE_ARB
:
2859 coords0
[0] = 0.0F
; /* s */
2860 coords0
[1] = 0.0F
; /* t */
2861 coords0
[2] = 0.0F
; /* r */
2866 coords2
[1] = height
;
2869 coords3
[1] = height
;
2872 case GL_TEXTURE_1D_ARRAY
:
2873 coords0
[0] = 0.0F
; /* s */
2874 coords0
[1] = slice
; /* t */
2875 coords0
[2] = 0.0F
; /* r */
2887 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2888 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2889 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2890 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2891 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2892 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2893 /* loop over quad verts */
2894 for (i
= 0; i
< 4; i
++) {
2895 /* Compute sc = +/-scale and tc = +/-scale.
2896 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2897 * though that can still sometimes happen with this scale factor...
2899 const GLfloat scale
= 0.9999f
;
2900 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2901 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2921 switch (faceTarget
) {
2922 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2927 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2932 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2937 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2942 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2947 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2958 assert(0 && "unexpected target in meta setup_texture_coords()");
2964 setup_ff_generate_mipmap(struct gl_context
*ctx
,
2965 struct gen_mipmap_state
*mipmap
)
2968 GLfloat x
, y
, tex
[3];
2971 if (mipmap
->ArrayObj
== 0) {
2972 /* one-time setup */
2973 /* create vertex array object */
2974 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2975 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2977 /* create vertex array buffer */
2978 _mesa_GenBuffers(1, &mipmap
->VBO
);
2979 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2980 /* setup vertex arrays */
2981 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2982 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2983 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2984 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2987 /* setup projection matrix */
2988 _mesa_MatrixMode(GL_PROJECTION
);
2989 _mesa_LoadIdentity();
2990 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
2994 static struct glsl_sampler
*
2995 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
2999 mipmap
->sampler_1d
.type
= "sampler1D";
3000 mipmap
->sampler_1d
.func
= "texture1D";
3001 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3002 return &mipmap
->sampler_1d
;
3004 mipmap
->sampler_2d
.type
= "sampler2D";
3005 mipmap
->sampler_2d
.func
= "texture2D";
3006 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3007 return &mipmap
->sampler_2d
;
3009 /* Code for mipmap generation with 3D textures is not used yet.
3010 * It's a sw fallback.
3012 mipmap
->sampler_3d
.type
= "sampler3D";
3013 mipmap
->sampler_3d
.func
= "texture3D";
3014 mipmap
->sampler_3d
.texcoords
= "texCoords";
3015 return &mipmap
->sampler_3d
;
3016 case GL_TEXTURE_CUBE_MAP
:
3017 mipmap
->sampler_cubemap
.type
= "samplerCube";
3018 mipmap
->sampler_cubemap
.func
= "textureCube";
3019 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3020 return &mipmap
->sampler_cubemap
;
3021 case GL_TEXTURE_1D_ARRAY
:
3022 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3023 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3024 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3025 return &mipmap
->sampler_1d_array
;
3026 case GL_TEXTURE_2D_ARRAY
:
3027 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3028 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3029 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3030 return &mipmap
->sampler_2d_array
;
3032 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3033 " setup_texture_sampler()\n", target
);
3040 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3041 struct gen_mipmap_state
*mipmap
,
3045 GLfloat x
, y
, tex
[3];
3047 struct glsl_sampler
*sampler
;
3048 const char *vs_source
;
3053 /* Check if already initialized */
3054 if (mipmap
->ArrayObj
== 0) {
3056 /* create vertex array object */
3057 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3058 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3060 /* create vertex array buffer */
3061 _mesa_GenBuffers(1, &mipmap
->VBO
);
3062 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3064 /* setup vertex arrays */
3065 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3066 sizeof(struct vertex
), OFFSET(x
));
3067 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3068 sizeof(struct vertex
), OFFSET(tex
));
3071 /* Generate a fragment shader program appropriate for the texture target */
3072 sampler
= setup_texture_sampler(target
, mipmap
);
3073 assert(sampler
!= NULL
);
3074 if (sampler
->shader_prog
!= 0) {
3075 mipmap
->ShaderProg
= sampler
->shader_prog
;
3079 mem_ctx
= ralloc_context(NULL
);
3081 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3083 "attribute vec2 position;\n"
3084 "attribute vec3 textureCoords;\n"
3085 "varying vec3 texCoords;\n"
3088 " texCoords = textureCoords;\n"
3089 " gl_Position = vec4(position, 0.0, 1.0);\n"
3092 fs_source
= ralloc_asprintf(mem_ctx
,
3093 "#extension GL_EXT_texture_array : enable\n"
3094 "uniform %s texSampler;\n"
3095 "varying vec3 texCoords;\n"
3098 " gl_FragColor = %s(texSampler, %s);\n"
3101 sampler
->func
, sampler
->texcoords
);
3106 "in vec2 position;\n"
3107 "in vec3 textureCoords;\n"
3108 "out vec3 texCoords;\n"
3111 " texCoords = textureCoords;\n"
3112 " gl_Position = vec4(position, 0.0, 1.0);\n"
3114 fs_source
= ralloc_asprintf(mem_ctx
,
3116 "uniform %s texSampler;\n"
3117 "in vec3 texCoords;\n"
3118 "out vec4 out_color;\n"
3122 " out_color = texture(texSampler, %s);\n"
3125 sampler
->texcoords
);
3128 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3129 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3131 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3132 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3133 _mesa_DeleteObjectARB(fs
);
3134 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3135 _mesa_DeleteObjectARB(vs
);
3136 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3137 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3138 _mesa_EnableVertexAttribArray(0);
3139 _mesa_EnableVertexAttribArray(1);
3140 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3141 sampler
->shader_prog
= mipmap
->ShaderProg
;
3142 ralloc_free(mem_ctx
);
3147 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3148 struct gen_mipmap_state
*mipmap
)
3150 if (mipmap
->ArrayObj
== 0)
3152 _mesa_DeleteVertexArrays(1, &mipmap
->ArrayObj
);
3153 mipmap
->ArrayObj
= 0;
3154 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3157 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3158 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3159 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3160 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3161 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3162 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3164 mipmap
->sampler_1d
.shader_prog
= 0;
3165 mipmap
->sampler_2d
.shader_prog
= 0;
3166 mipmap
->sampler_3d
.shader_prog
= 0;
3167 mipmap
->sampler_cubemap
.shader_prog
= 0;
3168 mipmap
->sampler_1d_array
.shader_prog
= 0;
3169 mipmap
->sampler_2d_array
.shader_prog
= 0;
3174 * Called via ctx->Driver.GenerateMipmap()
3175 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3179 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3180 struct gl_texture_object
*texObj
)
3182 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3184 GLfloat x
, y
, tex
[3];
3186 struct vertex verts
[4];
3187 const GLuint baseLevel
= texObj
->BaseLevel
;
3188 const GLuint maxLevel
= texObj
->MaxLevel
;
3189 const GLint maxLevelSave
= texObj
->MaxLevel
;
3190 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3191 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3192 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3193 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3194 ctx
->Extensions
.ARB_fragment_shader
&&
3195 (ctx
->API
!= API_OPENGLES
);
3198 const GLint slice
= 0;
3201 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3202 _mesa_generate_mipmap(ctx
, target
, texObj
);
3206 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3207 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3208 faceTarget
= target
;
3209 target
= GL_TEXTURE_CUBE_MAP
;
3212 faceTarget
= target
;
3215 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3217 /* Choose between glsl version and fixed function version of
3218 * GenerateMipmap function.
3220 if (use_glsl_version
) {
3221 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3222 _mesa_UseProgram(mipmap
->ShaderProg
);
3225 setup_ff_generate_mipmap(ctx
, mipmap
);
3226 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3229 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3230 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3232 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3233 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3235 if (currentTexUnitSave
!= 0)
3236 _mesa_BindTexture(target
, texObj
->Name
);
3239 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3242 if (!mipmap
->Sampler
) {
3243 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3244 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3246 _mesa_SamplerParameteri(mipmap
->Sampler
,
3247 GL_TEXTURE_MIN_FILTER
,
3248 GL_LINEAR_MIPMAP_LINEAR
);
3249 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3250 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3251 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3252 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3254 /* We don't want to encode or decode sRGB values; treat them as linear.
3255 * This is not technically correct for GLES3 but we don't get any API
3256 * error at the moment.
3258 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3259 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3260 GL_SKIP_DECODE_EXT
);
3264 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3267 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3269 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3270 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3272 assert(!genMipmapSave
);
3274 /* Setup texture coordinates */
3275 setup_texture_coords(faceTarget
,
3277 0, 0, /* width, height never used here */
3283 /* setup vertex positions */
3293 /* upload vertex data */
3294 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3295 verts
, GL_DYNAMIC_DRAW_ARB
);
3297 /* texture is already locked, unlock now */
3298 _mesa_unlock_texture(ctx
, texObj
);
3300 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3301 const struct gl_texture_image
*srcImage
;
3302 const GLuint srcLevel
= dstLevel
- 1;
3303 GLsizei srcWidth
, srcHeight
, srcDepth
;
3304 GLsizei dstWidth
, dstHeight
, dstDepth
;
3307 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3308 assert(srcImage
->Border
== 0);
3311 srcWidth
= srcImage
->Width
;
3312 srcHeight
= srcImage
->Height
;
3313 srcDepth
= srcImage
->Depth
;
3316 dstWidth
= MAX2(1, srcWidth
/ 2);
3317 dstHeight
= MAX2(1, srcHeight
/ 2);
3318 dstDepth
= MAX2(1, srcDepth
/ 2);
3320 if (dstWidth
== srcImage
->Width
&&
3321 dstHeight
== srcImage
->Height
&&
3322 dstDepth
== srcImage
->Depth
) {
3327 /* Allocate storage for the destination mipmap image(s) */
3329 /* Set MaxLevel large enough to hold the new level when we allocate it */
3330 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3332 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3333 dstWidth
, dstHeight
, dstDepth
,
3335 srcImage
->InternalFormat
,
3336 srcImage
->TexFormat
)) {
3337 /* All done. We either ran out of memory or we would go beyond the
3338 * last valid level of an immutable texture if we continued.
3343 /* limit minification to src level */
3344 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3346 /* Set to draw into the current dstLevel */
3347 if (target
== GL_TEXTURE_1D
) {
3348 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3349 GL_COLOR_ATTACHMENT0_EXT
,
3354 else if (target
== GL_TEXTURE_3D
) {
3355 GLint zoffset
= 0; /* XXX unfinished */
3356 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3357 GL_COLOR_ATTACHMENT0_EXT
,
3364 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3365 GL_COLOR_ATTACHMENT0_EXT
,
3371 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3374 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3375 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3376 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3377 "_mesa_meta_GenerateMipmap()");
3381 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3382 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3384 /* setup viewport */
3385 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3387 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3390 _mesa_lock_texture(ctx
, texObj
); /* relock */
3392 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3394 _mesa_meta_end(ctx
);
3396 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3398 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3400 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3405 * Determine the GL data type to use for the temporary image read with
3406 * ReadPixels() and passed to Tex[Sub]Image().
3409 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3413 baseFormat
= _mesa_get_format_base_format(format
);
3415 switch (baseFormat
) {
3422 case GL_LUMINANCE_ALPHA
:
3424 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3425 return GL_UNSIGNED_BYTE
;
3426 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3427 return GL_UNSIGNED_SHORT
;
3429 GLenum datatype
= _mesa_get_format_datatype(format
);
3430 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3434 case GL_DEPTH_COMPONENT
:
3435 return GL_UNSIGNED_INT
;
3436 case GL_DEPTH_STENCIL
:
3437 return GL_UNSIGNED_INT_24_8
;
3439 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3447 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3448 * Have to be careful with locking and meta state for pixel transfer.
3451 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3452 struct gl_texture_image
*texImage
,
3453 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3454 struct gl_renderbuffer
*rb
,
3456 GLsizei width
, GLsizei height
)
3458 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3459 GLenum format
, type
;
3463 /* Choose format/type for temporary image buffer */
3464 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3465 if (format
== GL_LUMINANCE
||
3466 format
== GL_LUMINANCE_ALPHA
||
3467 format
== GL_INTENSITY
) {
3468 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3469 * temp image buffer because glReadPixels will do L=R+G+B which is
3470 * not what we want (should be L=R).
3475 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3476 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3477 format
= _mesa_base_format_to_integer_format(format
);
3479 bpp
= _mesa_bytes_per_pixel(format
, type
);
3481 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3486 * Alloc image buffer (XXX could use a PBO)
3488 buf
= malloc(width
* height
* bpp
);
3490 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3494 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3497 * Read image from framebuffer (disable pixel transfer ops)
3499 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3500 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3501 format
, type
, &ctx
->Pack
, buf
);
3502 _mesa_meta_end(ctx
);
3504 _mesa_update_state(ctx
); /* to update pixel transfer state */
3507 * Store texture data (with pixel transfer ops)
3509 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3511 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3512 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3513 format
, type
, buf
, &ctx
->Unpack
);
3515 _mesa_meta_end(ctx
);
3517 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3524 * Decompress a texture image by drawing a quad with the compressed
3525 * texture and reading the pixels out of the color buffer.
3526 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3527 * \param destFormat format, ala glReadPixels
3528 * \param destType type, ala glReadPixels
3529 * \param dest destination buffer
3530 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3533 decompress_texture_image(struct gl_context
*ctx
,
3534 struct gl_texture_image
*texImage
,
3536 GLenum destFormat
, GLenum destType
,
3539 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3540 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3541 const GLint width
= texImage
->Width
;
3542 const GLint height
= texImage
->Height
;
3543 const GLenum target
= texObj
->Target
;
3546 GLfloat x
, y
, tex
[3];
3548 struct vertex verts
[4];
3549 GLuint fboDrawSave
, fboReadSave
;
3554 assert(target
== GL_TEXTURE_3D
||
3555 target
== GL_TEXTURE_2D_ARRAY
);
3558 if (target
== GL_TEXTURE_CUBE_MAP
) {
3559 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3562 faceTarget
= target
;
3565 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3566 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3567 fboReadSave
= ctx
->ReadBuffer
->Name
;
3568 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3570 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3572 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3573 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3575 /* Create/bind FBO/renderbuffer */
3576 if (decompress
->FBO
== 0) {
3577 _mesa_GenFramebuffers(1, &decompress
->FBO
);
3578 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
3579 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3580 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3581 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
3582 GL_COLOR_ATTACHMENT0_EXT
,
3583 GL_RENDERBUFFER_EXT
,
3587 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3590 /* alloc dest surface */
3591 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3592 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3593 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3595 decompress
->Width
= width
;
3596 decompress
->Height
= height
;
3599 /* setup VBO data */
3600 if (decompress
->ArrayObj
== 0) {
3601 /* create vertex array object */
3602 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3603 _mesa_BindVertexArray(decompress
->ArrayObj
);
3605 /* create vertex array buffer */
3606 _mesa_GenBuffers(1, &decompress
->VBO
);
3607 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3608 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3609 NULL
, GL_DYNAMIC_DRAW_ARB
);
3611 /* setup vertex arrays */
3612 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3613 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3614 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3615 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3618 _mesa_BindVertexArray(decompress
->ArrayObj
);
3619 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3622 if (!decompress
->Sampler
) {
3623 _mesa_GenSamplers(1, &decompress
->Sampler
);
3624 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3625 /* nearest filtering */
3626 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3627 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3628 /* No sRGB decode or encode.*/
3629 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3630 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3631 GL_SKIP_DECODE_EXT
);
3635 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3638 setup_texture_coords(faceTarget
, slice
, width
, height
,
3644 /* setup vertex positions */
3650 verts
[2].y
= height
;
3652 verts
[3].y
= height
;
3654 /* upload new vertex data */
3655 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3657 /* setup texture state */
3658 _mesa_BindTexture(target
, texObj
->Name
);
3659 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3662 /* save texture object state */
3663 const GLint baseLevelSave
= texObj
->BaseLevel
;
3664 const GLint maxLevelSave
= texObj
->MaxLevel
;
3666 /* restrict sampling to the texture level of interest */
3667 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3668 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3669 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3672 /* render quad w/ texture into renderbuffer */
3673 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3675 /* Restore texture object state, the texture binding will
3676 * be restored by _mesa_meta_end().
3678 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3679 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3680 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3685 /* read pixels from renderbuffer */
3687 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3688 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
3690 /* The pixel transfer state will be set to default values at this point
3691 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3692 * turned off (as required by glGetTexImage) but we need to handle some
3693 * special cases. In particular, single-channel texture values are
3694 * returned as red and two-channel texture values are returned as
3697 if ((baseTexFormat
== GL_LUMINANCE
||
3698 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3699 baseTexFormat
== GL_INTENSITY
) ||
3700 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
3701 * luminance then we need to return L=tex(R).
3703 ((baseTexFormat
== GL_RGBA
||
3704 baseTexFormat
== GL_RGB
||
3705 baseTexFormat
== GL_RG
) &&
3706 (destBaseFormat
== GL_LUMINANCE
||
3707 destBaseFormat
== GL_LUMINANCE_ALPHA
||
3708 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
3709 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
3710 /* Green and blue must be zero */
3711 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3712 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3715 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3718 /* disable texture unit */
3719 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3721 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3723 _mesa_meta_end(ctx
);
3725 /* restore fbo bindings */
3726 if (fboDrawSave
== fboReadSave
) {
3727 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3730 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3731 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3733 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
3738 * This is just a wrapper around _mesa_get_tex_image() and
3739 * decompress_texture_image(). Meta functions should not be directly called
3743 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3744 GLenum format
, GLenum type
, GLvoid
*pixels
,
3745 struct gl_texture_image
*texImage
)
3747 /* We can only use the decompress-with-blit method here if the texels are
3748 * unsigned, normalized values. We could handle signed and unnormalized
3749 * with floating point renderbuffers...
3751 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3752 _mesa_get_format_datatype(texImage
->TexFormat
)
3753 == GL_UNSIGNED_NORMALIZED
) {
3754 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3755 const GLuint slice
= 0; /* only 2D compressed textures for now */
3756 /* Need to unlock the texture here to prevent deadlock... */
3757 _mesa_unlock_texture(ctx
, texObj
);
3758 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3759 /* ... and relock it */
3760 _mesa_lock_texture(ctx
, texObj
);
3763 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3769 * Meta implementation of ctx->Driver.DrawTex() in terms
3770 * of polygon rendering.
3773 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3774 GLfloat width
, GLfloat height
)
3776 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3778 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3780 struct vertex verts
[4];
3783 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3785 MESA_META_TRANSFORM
|
3787 MESA_META_VIEWPORT
));
3789 if (drawtex
->ArrayObj
== 0) {
3790 /* one-time setup */
3791 GLint active_texture
;
3793 /* create vertex array object */
3794 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3795 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3797 /* create vertex array buffer */
3798 _mesa_GenBuffers(1, &drawtex
->VBO
);
3799 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3800 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3801 NULL
, GL_DYNAMIC_DRAW_ARB
);
3803 /* client active texture is not part of the array object */
3804 active_texture
= ctx
->Array
.ActiveTexture
;
3806 /* setup vertex arrays */
3807 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3808 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3809 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3810 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
3811 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3812 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3815 /* restore client active texture */
3816 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
3819 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3820 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3823 /* vertex positions, texcoords */
3825 const GLfloat x1
= x
+ width
;
3826 const GLfloat y1
= y
+ height
;
3828 z
= CLAMP(z
, 0.0, 1.0);
3847 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3848 const struct gl_texture_object
*texObj
;
3849 const struct gl_texture_image
*texImage
;
3850 GLfloat s
, t
, s1
, t1
;
3853 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3855 for (j
= 0; j
< 4; j
++) {
3856 verts
[j
].st
[i
][0] = 0.0f
;
3857 verts
[j
].st
[i
][1] = 0.0f
;
3862 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3863 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3864 tw
= texImage
->Width2
;
3865 th
= texImage
->Height2
;
3867 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3868 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3869 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3870 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3872 verts
[0].st
[i
][0] = s
;
3873 verts
[0].st
[i
][1] = t
;
3875 verts
[1].st
[i
][0] = s1
;
3876 verts
[1].st
[i
][1] = t
;
3878 verts
[2].st
[i
][0] = s1
;
3879 verts
[2].st
[i
][1] = t1
;
3881 verts
[3].st
[i
][0] = s
;
3882 verts
[3].st
[i
][1] = t1
;
3885 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3888 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3890 _mesa_meta_end(ctx
);