2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/readpix.h"
58 #include "main/scissor.h"
59 #include "main/shaderapi.h"
60 #include "main/shaderobj.h"
61 #include "main/state.h"
62 #include "main/stencil.h"
63 #include "main/texobj.h"
64 #include "main/texenv.h"
65 #include "main/texgetimage.h"
66 #include "main/teximage.h"
67 #include "main/texparam.h"
68 #include "main/texstate.h"
69 #include "main/transformfeedback.h"
70 #include "main/uniforms.h"
71 #include "main/varray.h"
72 #include "main/viewport.h"
73 #include "main/samplerobj.h"
74 #include "program/program.h"
75 #include "swrast/swrast.h"
76 #include "drivers/common/meta.h"
77 #include "main/enums.h"
78 #include "main/glformats.h"
79 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_ALPHA_TEST */
94 GLboolean AlphaEnabled
;
98 /** MESA_META_BLEND */
99 GLbitfield BlendEnabled
;
100 GLboolean ColorLogicOpEnabled
;
102 /** MESA_META_COLOR_MASK */
103 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
105 /** MESA_META_DEPTH_TEST */
106 struct gl_depthbuffer_attrib Depth
;
111 /** MESA_META_PIXEL_STORE */
112 struct gl_pixelstore_attrib Pack
, Unpack
;
114 /** MESA_META_PIXEL_TRANSFER */
115 GLfloat RedBias
, RedScale
;
116 GLfloat GreenBias
, GreenScale
;
117 GLfloat BlueBias
, BlueScale
;
118 GLfloat AlphaBias
, AlphaScale
;
119 GLfloat DepthBias
, DepthScale
;
120 GLboolean MapColorFlag
;
122 /** MESA_META_RASTERIZATION */
123 GLenum FrontPolygonMode
, BackPolygonMode
;
124 GLboolean PolygonOffset
;
125 GLboolean PolygonSmooth
;
126 GLboolean PolygonStipple
;
127 GLboolean PolygonCull
;
129 /** MESA_META_SCISSOR */
130 struct gl_scissor_attrib Scissor
;
132 /** MESA_META_SHADER */
133 GLboolean VertexProgramEnabled
;
134 struct gl_vertex_program
*VertexProgram
;
135 GLboolean FragmentProgramEnabled
;
136 struct gl_fragment_program
*FragmentProgram
;
137 struct gl_shader_program
*VertexShader
;
138 struct gl_shader_program
*GeometryShader
;
139 struct gl_shader_program
*FragmentShader
;
140 struct gl_shader_program
*ActiveShader
;
142 /** MESA_META_STENCIL_TEST */
143 struct gl_stencil_attrib Stencil
;
145 /** MESA_META_TRANSFORM */
147 GLfloat ModelviewMatrix
[16];
148 GLfloat ProjectionMatrix
[16];
149 GLfloat TextureMatrix
[16];
151 /** MESA_META_CLIP */
152 GLbitfield ClipPlanesEnabled
;
154 /** MESA_META_TEXTURE */
156 GLuint ClientActiveUnit
;
157 /** for unit[0] only */
158 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
159 /** mask of TEXTURE_2D_BIT, etc */
160 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
161 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
162 GLuint EnvMode
; /* unit[0] only */
164 /** MESA_META_VERTEX */
165 struct gl_array_object
*ArrayObj
;
166 struct gl_buffer_object
*ArrayBufferObj
;
168 /** MESA_META_VIEWPORT */
169 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
170 GLclampd DepthNear
, DepthFar
;
172 /** MESA_META_CLAMP_FRAGMENT_COLOR */
173 GLenum ClampFragmentColor
;
175 /** MESA_META_CLAMP_VERTEX_COLOR */
176 GLenum ClampVertexColor
;
178 /** MESA_META_CONDITIONAL_RENDER */
179 struct gl_query_object
*CondRenderQuery
;
180 GLenum CondRenderMode
;
183 /** MESA_META_SELECT_FEEDBACK */
185 struct gl_selection Select
;
186 struct gl_feedback Feedback
;
189 /** MESA_META_MULTISAMPLE */
190 GLboolean MultisampleEnabled
;
192 /** Miscellaneous (always disabled) */
194 GLboolean RasterDiscard
;
195 #if FEATURE_EXT_transform_feedback
196 GLboolean TransformFeedbackNeedsResume
;
201 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
202 * This is currently shared by all the meta ops. But we could create a
203 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
208 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
209 GLsizei MinSize
; /**< Min texture size to allocate */
210 GLsizei MaxSize
; /**< Max possible texture size */
211 GLboolean NPOT
; /**< Non-power of two size OK? */
212 GLsizei Width
, Height
; /**< Current texture size */
214 GLfloat Sright
, Ttop
; /**< right, top texcoords */
219 * State for glBlitFramebufer()
230 * State for glClear()
239 GLuint IntegerShaderProg
;
240 GLint IntegerColorLocation
;
245 * State for glCopyPixels()
255 * State for glDrawPixels()
261 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
262 GLuint DepthFP
; /**< Fragment program for drawing depth images */
267 * State for glBitmap()
273 struct temp_texture Tex
; /**< separate texture from other meta ops */
278 * State for _mesa_meta_generate_mipmap()
280 struct gen_mipmap_state
287 GLuint IntegerShaderProg
;
291 * State for GLSL texture sampler which is used to generate fragment
292 * shader in _mesa_meta_generate_mipmap().
294 struct glsl_sampler
{
297 const char *texcoords
;
301 * State for texture decompression
303 struct decompress_state
306 GLuint VBO
, FBO
, RBO
, Sampler
;
311 * State for glDrawTex()
319 #define MAX_META_OPS_DEPTH 8
321 * All per-context meta state.
325 /** Stack of state saved during meta-ops */
326 struct save_state Save
[MAX_META_OPS_DEPTH
];
327 /** Save stack depth */
328 GLuint SaveStackDepth
;
330 struct temp_texture TempTex
;
332 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
333 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
334 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
335 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
336 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
337 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
338 struct decompress_state Decompress
; /**< For texture decompression */
339 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
342 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
343 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
344 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
345 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
346 struct gen_mipmap_state
*mipmap
);
349 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
355 shader
= _mesa_CreateShaderObjectARB(target
);
356 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
357 _mesa_CompileShaderARB(shader
);
359 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
363 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
365 _mesa_DeleteObjectARB(shader
);
371 _mesa_DeleteObjectARB(shader
);
375 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
377 "meta program compile failed:\n%s\n"
382 _mesa_DeleteObjectARB(shader
);
388 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
393 _mesa_LinkProgramARB(program
);
395 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
399 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
407 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
408 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
416 * Initialize meta-ops for a context.
417 * To be called once during context creation.
420 _mesa_meta_init(struct gl_context
*ctx
)
424 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
429 * Free context meta-op state.
430 * To be called once during context destruction.
433 _mesa_meta_free(struct gl_context
*ctx
)
435 GET_CURRENT_CONTEXT(old_context
);
436 _mesa_make_current(ctx
, NULL
, NULL
);
437 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
438 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
439 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
440 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
442 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
444 _mesa_make_current(NULL
, NULL
, NULL
);
451 * This is an alternative to _mesa_set_enable() to handle some special cases.
452 * See comments inside.
455 meta_set_enable(struct gl_context
*ctx
, GLenum cap
, GLboolean state
)
459 /* We need to enable/disable multisample when using GLES but this enum
460 * is not supported there.
462 if (ctx
->Multisample
.Enabled
== state
)
464 FLUSH_VERTICES(ctx
, _NEW_MULTISAMPLE
);
465 ctx
->Multisample
.Enabled
= state
;
468 _mesa_problem(ctx
, "Unexpected cap in _meta_set_enable()");
472 if (ctx
->Driver
.Enable
) {
473 ctx
->Driver
.Enable(ctx
, cap
, state
);
480 * Enter meta state. This is like a light-weight version of glPushAttrib
481 * but it also resets most GL state back to default values.
483 * \param state bitmask of MESA_META_* flags indicating which attribute groups
484 * to save and reset to their defaults
487 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
489 struct save_state
*save
;
491 /* hope MAX_META_OPS_DEPTH is large enough */
492 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
494 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
495 memset(save
, 0, sizeof(*save
));
496 save
->SavedState
= state
;
498 #if FEATURE_EXT_transform_feedback
499 /* Pausing transform feedback needs to be done early, or else we won't be
500 * able to change other state.
502 save
->TransformFeedbackNeedsResume
=
503 ctx
->TransformFeedback
.CurrentObject
->Active
&&
504 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
505 if (save
->TransformFeedbackNeedsResume
)
506 _mesa_PauseTransformFeedback();
509 if (state
& MESA_META_ALPHA_TEST
) {
510 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
511 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
512 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
513 if (ctx
->Color
.AlphaEnabled
)
514 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
517 if (state
& MESA_META_BLEND
) {
518 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
519 if (ctx
->Color
.BlendEnabled
) {
520 if (ctx
->Extensions
.EXT_draw_buffers2
) {
522 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
523 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
527 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
530 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
531 if (ctx
->Color
.ColorLogicOpEnabled
)
532 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
535 if (state
& MESA_META_COLOR_MASK
) {
536 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
537 sizeof(ctx
->Color
.ColorMask
));
538 if (!ctx
->Color
.ColorMask
[0][0] ||
539 !ctx
->Color
.ColorMask
[0][1] ||
540 !ctx
->Color
.ColorMask
[0][2] ||
541 !ctx
->Color
.ColorMask
[0][3])
542 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
545 if (state
& MESA_META_DEPTH_TEST
) {
546 save
->Depth
= ctx
->Depth
; /* struct copy */
548 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
551 if ((state
& MESA_META_FOG
)
552 && ctx
->API
!= API_OPENGL_CORE
553 && ctx
->API
!= API_OPENGLES2
) {
554 save
->Fog
= ctx
->Fog
.Enabled
;
555 if (ctx
->Fog
.Enabled
)
556 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
559 if (state
& MESA_META_PIXEL_STORE
) {
560 save
->Pack
= ctx
->Pack
;
561 save
->Unpack
= ctx
->Unpack
;
562 ctx
->Pack
= ctx
->DefaultPacking
;
563 ctx
->Unpack
= ctx
->DefaultPacking
;
566 if (state
& MESA_META_PIXEL_TRANSFER
) {
567 save
->RedScale
= ctx
->Pixel
.RedScale
;
568 save
->RedBias
= ctx
->Pixel
.RedBias
;
569 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
570 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
571 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
572 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
573 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
574 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
575 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
576 ctx
->Pixel
.RedScale
= 1.0F
;
577 ctx
->Pixel
.RedBias
= 0.0F
;
578 ctx
->Pixel
.GreenScale
= 1.0F
;
579 ctx
->Pixel
.GreenBias
= 0.0F
;
580 ctx
->Pixel
.BlueScale
= 1.0F
;
581 ctx
->Pixel
.BlueBias
= 0.0F
;
582 ctx
->Pixel
.AlphaScale
= 1.0F
;
583 ctx
->Pixel
.AlphaBias
= 0.0F
;
584 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
586 ctx
->NewState
|=_NEW_PIXEL
;
589 if (state
& MESA_META_RASTERIZATION
) {
590 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
591 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
592 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
593 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
594 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
595 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
596 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
597 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
598 if (ctx
->API
== API_OPENGL
) {
599 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
600 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
602 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
605 if (state
& MESA_META_SCISSOR
) {
606 save
->Scissor
= ctx
->Scissor
; /* struct copy */
607 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
610 if (state
& MESA_META_SHADER
) {
611 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_vertex_program
) {
612 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
613 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
614 ctx
->VertexProgram
.Current
);
615 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
618 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_fragment_program
) {
619 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
620 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
621 ctx
->FragmentProgram
.Current
);
622 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
625 if (ctx
->Extensions
.ARB_shader_objects
) {
626 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
627 ctx
->Shader
.CurrentVertexProgram
);
628 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
629 ctx
->Shader
.CurrentGeometryProgram
);
630 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
631 ctx
->Shader
.CurrentFragmentProgram
);
632 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
633 ctx
->Shader
.ActiveProgram
);
635 _mesa_UseProgramObjectARB(0);
639 if (state
& MESA_META_STENCIL_TEST
) {
640 save
->Stencil
= ctx
->Stencil
; /* struct copy */
641 if (ctx
->Stencil
.Enabled
)
642 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
643 /* NOTE: other stencil state not reset */
646 if (state
& MESA_META_TEXTURE
) {
649 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
650 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
651 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
653 /* Disable all texture units */
654 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
655 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
656 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
657 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
658 if (ctx
->Texture
.Unit
[u
].Enabled
||
659 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
660 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
661 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
662 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
663 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
664 if (ctx
->Extensions
.ARB_texture_cube_map
)
665 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
666 if (ctx
->Extensions
.NV_texture_rectangle
)
667 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
668 if (ctx
->Extensions
.OES_EGL_image_external
)
669 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
670 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
671 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
672 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
673 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
678 /* save current texture objects for unit[0] only */
679 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
680 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
681 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
684 /* set defaults for unit[0] */
685 _mesa_ActiveTextureARB(GL_TEXTURE0
);
686 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
687 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
688 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
692 if (state
& MESA_META_TRANSFORM
) {
693 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
694 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
695 16 * sizeof(GLfloat
));
696 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
697 16 * sizeof(GLfloat
));
698 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
699 16 * sizeof(GLfloat
));
700 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
701 /* set 1:1 vertex:pixel coordinate transform */
702 _mesa_ActiveTextureARB(GL_TEXTURE0
);
703 _mesa_MatrixMode(GL_TEXTURE
);
704 _mesa_LoadIdentity();
705 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
706 _mesa_MatrixMode(GL_MODELVIEW
);
707 _mesa_LoadIdentity();
708 _mesa_MatrixMode(GL_PROJECTION
);
709 _mesa_LoadIdentity();
710 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
711 0.0, ctx
->DrawBuffer
->Height
,
715 if (state
& MESA_META_CLIP
) {
716 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
717 if (ctx
->Transform
.ClipPlanesEnabled
) {
719 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
720 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
725 if (state
& MESA_META_VERTEX
) {
726 /* save vertex array object state */
727 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
728 ctx
->Array
.ArrayObj
);
729 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
730 ctx
->Array
.ArrayBufferObj
);
731 /* set some default state? */
734 if (state
& MESA_META_VIEWPORT
) {
735 /* save viewport state */
736 save
->ViewportX
= ctx
->Viewport
.X
;
737 save
->ViewportY
= ctx
->Viewport
.Y
;
738 save
->ViewportW
= ctx
->Viewport
.Width
;
739 save
->ViewportH
= ctx
->Viewport
.Height
;
740 /* set viewport to match window size */
741 if (ctx
->Viewport
.X
!= 0 ||
742 ctx
->Viewport
.Y
!= 0 ||
743 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
744 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
745 _mesa_set_viewport(ctx
, 0, 0,
746 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
748 /* save depth range state */
749 save
->DepthNear
= ctx
->Viewport
.Near
;
750 save
->DepthFar
= ctx
->Viewport
.Far
;
751 /* set depth range to default */
752 _mesa_DepthRange(0.0, 1.0);
755 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
756 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
758 /* Generally in here we want to do clamping according to whether
759 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
760 * regardless of the internal implementation of the metaops.
762 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
763 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
766 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
767 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
769 /* Generally in here we never want vertex color clamping --
770 * result clamping is only dependent on fragment clamping.
772 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
775 if (state
& MESA_META_CONDITIONAL_RENDER
) {
776 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
777 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
779 if (ctx
->Query
.CondRenderQuery
)
780 _mesa_EndConditionalRender();
784 if (state
& MESA_META_SELECT_FEEDBACK
) {
785 save
->RenderMode
= ctx
->RenderMode
;
786 if (ctx
->RenderMode
== GL_SELECT
) {
787 save
->Select
= ctx
->Select
; /* struct copy */
788 _mesa_RenderMode(GL_RENDER
);
789 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
790 save
->Feedback
= ctx
->Feedback
; /* struct copy */
791 _mesa_RenderMode(GL_RENDER
);
796 if (state
& MESA_META_MULTISAMPLE
) {
797 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
798 if (ctx
->Multisample
.Enabled
)
799 meta_set_enable(ctx
, GL_MULTISAMPLE
, GL_FALSE
);
804 save
->Lighting
= ctx
->Light
.Enabled
;
805 if (ctx
->Light
.Enabled
)
806 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
807 save
->RasterDiscard
= ctx
->RasterDiscard
;
808 if (ctx
->RasterDiscard
)
809 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
815 * Leave meta state. This is like a light-weight version of glPopAttrib().
818 _mesa_meta_end(struct gl_context
*ctx
)
820 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
821 const GLbitfield state
= save
->SavedState
;
823 if (state
& MESA_META_ALPHA_TEST
) {
824 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
825 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
826 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
829 if (state
& MESA_META_BLEND
) {
830 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
831 if (ctx
->Extensions
.EXT_draw_buffers2
) {
833 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
834 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
838 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
841 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
842 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
845 if (state
& MESA_META_COLOR_MASK
) {
847 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
848 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
850 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
851 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
854 _mesa_ColorMaskIndexed(i
,
855 save
->ColorMask
[i
][0],
856 save
->ColorMask
[i
][1],
857 save
->ColorMask
[i
][2],
858 save
->ColorMask
[i
][3]);
864 if (state
& MESA_META_DEPTH_TEST
) {
865 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
866 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
867 _mesa_DepthFunc(save
->Depth
.Func
);
868 _mesa_DepthMask(save
->Depth
.Mask
);
871 if ((state
& MESA_META_FOG
)
872 && ctx
->API
!= API_OPENGL_CORE
873 && ctx
->API
!= API_OPENGLES2
) {
874 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
877 if (state
& MESA_META_PIXEL_STORE
) {
878 ctx
->Pack
= save
->Pack
;
879 ctx
->Unpack
= save
->Unpack
;
882 if (state
& MESA_META_PIXEL_TRANSFER
) {
883 ctx
->Pixel
.RedScale
= save
->RedScale
;
884 ctx
->Pixel
.RedBias
= save
->RedBias
;
885 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
886 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
887 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
888 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
889 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
890 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
891 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
893 ctx
->NewState
|=_NEW_PIXEL
;
896 if (state
& MESA_META_RASTERIZATION
) {
897 /* Core context requires that front and back mode be the same.
899 if (ctx
->API
== API_OPENGL_CORE
) {
900 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
902 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
903 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
905 if (ctx
->API
== API_OPENGL
) {
906 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
907 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
909 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
910 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
913 if (state
& MESA_META_SCISSOR
) {
914 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
915 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
916 save
->Scissor
.Width
, save
->Scissor
.Height
);
919 if (state
& MESA_META_SHADER
) {
920 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_vertex_program
) {
921 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
922 save
->VertexProgramEnabled
);
923 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
924 save
->VertexProgram
);
925 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
928 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.ARB_fragment_program
) {
929 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
930 save
->FragmentProgramEnabled
);
931 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
932 save
->FragmentProgram
);
933 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
936 if (ctx
->Extensions
.ARB_vertex_shader
)
937 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
939 if (ctx
->Extensions
.ARB_geometry_shader4
)
940 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
941 save
->GeometryShader
);
943 if (ctx
->Extensions
.ARB_fragment_shader
)
944 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
945 save
->FragmentShader
);
947 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
950 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
951 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
952 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
953 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
956 if (state
& MESA_META_STENCIL_TEST
) {
957 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
959 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
960 _mesa_ClearStencil(stencil
->Clear
);
961 if (ctx
->API
== API_OPENGL
&& ctx
->Extensions
.EXT_stencil_two_side
) {
962 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
963 stencil
->TestTwoSide
);
964 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
965 ? GL_BACK
: GL_FRONT
);
968 _mesa_StencilFuncSeparate(GL_FRONT
,
969 stencil
->Function
[0],
971 stencil
->ValueMask
[0]);
972 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
973 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
974 stencil
->ZFailFunc
[0],
975 stencil
->ZPassFunc
[0]);
977 _mesa_StencilFuncSeparate(GL_BACK
,
978 stencil
->Function
[1],
980 stencil
->ValueMask
[1]);
981 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
982 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
983 stencil
->ZFailFunc
[1],
984 stencil
->ZPassFunc
[1]);
987 if (state
& MESA_META_TEXTURE
) {
990 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
992 /* restore texenv for unit[0] */
993 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
994 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
997 /* restore texture objects for unit[0] only */
998 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
999 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1000 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1001 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1002 save
->CurrentTexture
[tgt
]);
1004 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1007 /* Restore fixed function texture enables, texgen */
1008 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
) {
1009 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1010 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1011 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1012 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1015 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1016 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1017 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1022 /* restore current unit state */
1023 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
1024 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1027 if (state
& MESA_META_TRANSFORM
) {
1028 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1029 _mesa_ActiveTextureARB(GL_TEXTURE0
);
1030 _mesa_MatrixMode(GL_TEXTURE
);
1031 _mesa_LoadMatrixf(save
->TextureMatrix
);
1032 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
1034 _mesa_MatrixMode(GL_MODELVIEW
);
1035 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1037 _mesa_MatrixMode(GL_PROJECTION
);
1038 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1040 _mesa_MatrixMode(save
->MatrixMode
);
1043 if (state
& MESA_META_CLIP
) {
1044 if (save
->ClipPlanesEnabled
) {
1046 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1047 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1048 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1054 if (state
& MESA_META_VERTEX
) {
1055 /* restore vertex buffer object */
1056 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1057 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1059 /* restore vertex array object */
1060 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1061 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1064 if (state
& MESA_META_VIEWPORT
) {
1065 if (save
->ViewportX
!= ctx
->Viewport
.X
||
1066 save
->ViewportY
!= ctx
->Viewport
.Y
||
1067 save
->ViewportW
!= ctx
->Viewport
.Width
||
1068 save
->ViewportH
!= ctx
->Viewport
.Height
) {
1069 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
1070 save
->ViewportW
, save
->ViewportH
);
1072 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1075 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
1076 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1079 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
1080 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1083 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1084 if (save
->CondRenderQuery
)
1085 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1086 save
->CondRenderMode
);
1089 #if FEATURE_feedback
1090 if (state
& MESA_META_SELECT_FEEDBACK
) {
1091 if (save
->RenderMode
== GL_SELECT
) {
1092 _mesa_RenderMode(GL_SELECT
);
1093 ctx
->Select
= save
->Select
;
1094 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1095 _mesa_RenderMode(GL_FEEDBACK
);
1096 ctx
->Feedback
= save
->Feedback
;
1101 if (state
& MESA_META_MULTISAMPLE
) {
1102 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1103 meta_set_enable(ctx
, GL_MULTISAMPLE
, save
->MultisampleEnabled
);
1107 if (save
->Lighting
) {
1108 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1110 if (save
->RasterDiscard
) {
1111 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1113 #if FEATURE_EXT_transform_feedback
1114 if (save
->TransformFeedbackNeedsResume
)
1115 _mesa_ResumeTransformFeedback();
1118 ctx
->Meta
->SaveStackDepth
--;
1123 * Determine whether Mesa is currently in a meta state.
1126 _mesa_meta_in_progress(struct gl_context
*ctx
)
1128 return ctx
->Meta
->SaveStackDepth
!= 0;
1133 * Convert Z from a normalized value in the range [0, 1] to an object-space
1134 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1135 * default/identity ortho projection results in the original Z value.
1136 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1137 * value comes from the clear value or raster position.
1139 static INLINE GLfloat
1140 invert_z(GLfloat normZ
)
1142 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1148 * One-time init for a temp_texture object.
1149 * Choose tex target, compute max tex size, etc.
1152 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1154 /* prefer texture rectangle */
1155 if (ctx
->Extensions
.NV_texture_rectangle
) {
1156 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1157 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1158 tex
->NPOT
= GL_TRUE
;
1161 /* use 2D texture, NPOT if possible */
1162 tex
->Target
= GL_TEXTURE_2D
;
1163 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1164 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1166 tex
->MinSize
= 16; /* 16 x 16 at least */
1167 assert(tex
->MaxSize
> 0);
1169 _mesa_GenTextures(1, &tex
->TexObj
);
1173 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1177 _mesa_DeleteTextures(1, &tex
->TexObj
);
1183 * Return pointer to temp_texture info for non-bitmap ops.
1184 * This does some one-time init if needed.
1186 static struct temp_texture
*
1187 get_temp_texture(struct gl_context
*ctx
)
1189 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1192 init_temp_texture(ctx
, tex
);
1200 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1201 * We use a separate texture for bitmaps to reduce texture
1202 * allocation/deallocation.
1204 static struct temp_texture
*
1205 get_bitmap_temp_texture(struct gl_context
*ctx
)
1207 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1210 init_temp_texture(ctx
, tex
);
1218 * Compute the width/height of texture needed to draw an image of the
1219 * given size. Return a flag indicating whether the current texture
1220 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1221 * allocated (glTexImage2D).
1222 * Also, compute s/t texcoords for drawing.
1224 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1227 alloc_texture(struct temp_texture
*tex
,
1228 GLsizei width
, GLsizei height
, GLenum intFormat
)
1230 GLboolean newTex
= GL_FALSE
;
1232 ASSERT(width
<= tex
->MaxSize
);
1233 ASSERT(height
<= tex
->MaxSize
);
1235 if (width
> tex
->Width
||
1236 height
> tex
->Height
||
1237 intFormat
!= tex
->IntFormat
) {
1238 /* alloc new texture (larger or different format) */
1241 /* use non-power of two size */
1242 tex
->Width
= MAX2(tex
->MinSize
, width
);
1243 tex
->Height
= MAX2(tex
->MinSize
, height
);
1246 /* find power of two size */
1248 w
= h
= tex
->MinSize
;
1257 tex
->IntFormat
= intFormat
;
1262 /* compute texcoords */
1263 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1264 tex
->Sright
= (GLfloat
) width
;
1265 tex
->Ttop
= (GLfloat
) height
;
1268 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1269 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1277 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1280 setup_copypix_texture(struct temp_texture
*tex
,
1282 GLint srcX
, GLint srcY
,
1283 GLsizei width
, GLsizei height
, GLenum intFormat
,
1286 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1287 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1288 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1289 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1291 /* copy framebuffer image to texture */
1293 /* create new tex image */
1294 if (tex
->Width
== width
&& tex
->Height
== height
) {
1295 /* create new tex with framebuffer data */
1296 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1297 srcX
, srcY
, width
, height
, 0);
1300 /* create empty texture */
1301 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1302 tex
->Width
, tex
->Height
, 0,
1303 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1305 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1306 0, 0, srcX
, srcY
, width
, height
);
1310 /* replace existing tex image */
1311 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1312 0, 0, srcX
, srcY
, width
, height
);
1318 * Setup/load texture for glDrawPixels.
1321 setup_drawpix_texture(struct gl_context
*ctx
,
1322 struct temp_texture
*tex
,
1324 GLenum texIntFormat
,
1325 GLsizei width
, GLsizei height
,
1326 GLenum format
, GLenum type
,
1327 const GLvoid
*pixels
)
1329 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1330 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1331 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1332 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1334 /* copy pixel data to texture */
1336 /* create new tex image */
1337 if (tex
->Width
== width
&& tex
->Height
== height
) {
1338 /* create new tex and load image data */
1339 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1340 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1343 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1345 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1346 ctx
->Unpack
.BufferObj
);
1347 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1348 /* create empty texture */
1349 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1350 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1351 if (save_unpack_obj
!= NULL
)
1352 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1353 save_unpack_obj
->Name
);
1355 _mesa_TexSubImage2D(tex
->Target
, 0,
1356 0, 0, width
, height
, format
, type
, pixels
);
1360 /* replace existing tex image */
1361 _mesa_TexSubImage2D(tex
->Target
, 0,
1362 0, 0, width
, height
, format
, type
, pixels
);
1369 * One-time init for drawing depth pixels.
1372 init_blit_depth_pixels(struct gl_context
*ctx
)
1374 static const char *program
=
1376 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1379 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1380 struct temp_texture
*tex
= get_temp_texture(ctx
);
1381 const char *texTarget
;
1383 assert(blit
->DepthFP
== 0);
1385 /* replace %s with "RECT" or "2D" */
1386 assert(strlen(program
) + 4 < sizeof(program2
));
1387 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1391 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1393 _mesa_GenPrograms(1, &blit
->DepthFP
);
1394 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1395 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1396 strlen(program2
), (const GLubyte
*) program2
);
1401 * Try to do a glBlitFramebuffer using no-copy texturing.
1402 * We can do this when the src renderbuffer is actually a texture.
1403 * But if the src buffer == dst buffer we cannot do this.
1405 * \return new buffer mask indicating the buffers left to blit using the
1409 blitframebuffer_texture(struct gl_context
*ctx
,
1410 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1411 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1412 GLbitfield mask
, GLenum filter
)
1414 if (mask
& GL_COLOR_BUFFER_BIT
) {
1415 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1416 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1417 const struct gl_renderbuffer_attachment
*drawAtt
=
1418 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1419 const struct gl_renderbuffer_attachment
*readAtt
=
1420 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1422 if (readAtt
&& readAtt
->Texture
) {
1423 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1424 const GLuint srcLevel
= readAtt
->TextureLevel
;
1425 const GLint baseLevelSave
= texObj
->BaseLevel
;
1426 const GLint maxLevelSave
= texObj
->MaxLevel
;
1427 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1428 const GLenum target
= texObj
->Target
;
1429 GLuint sampler
, samplerSave
=
1430 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1431 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1433 if (drawAtt
->Texture
== readAtt
->Texture
) {
1434 /* Can't use same texture as both the source and dest. We need
1435 * to handle overlapping blits and besides, some hw may not
1441 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1442 /* Can't handle other texture types at this time */
1446 _mesa_GenSamplers(1, &sampler
);
1447 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1450 printf("Blit from texture!\n");
1451 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1452 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1455 /* Prepare src texture state */
1456 _mesa_BindTexture(target
, texObj
->Name
);
1457 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1458 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1459 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1460 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1461 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1463 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1464 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1466 /* Always do our blits with no sRGB decode or encode.*/
1467 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1468 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1469 GL_SKIP_DECODE_EXT
);
1471 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_framebuffer_sRGB
)
1472 || _mesa_is_gles3(ctx
)) {
1473 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1476 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1477 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1479 /* Prepare vertex data (the VBO was previously created and bound) */
1484 struct vertex verts
[4];
1485 GLfloat s0
, t0
, s1
, t1
;
1487 if (target
== GL_TEXTURE_2D
) {
1488 const struct gl_texture_image
*texImage
1489 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1490 s0
= srcX0
/ (float) texImage
->Width
;
1491 s1
= srcX1
/ (float) texImage
->Width
;
1492 t0
= srcY0
/ (float) texImage
->Height
;
1493 t1
= srcY1
/ (float) texImage
->Height
;
1496 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1503 verts
[0].x
= (GLfloat
) dstX0
;
1504 verts
[0].y
= (GLfloat
) dstY0
;
1505 verts
[1].x
= (GLfloat
) dstX1
;
1506 verts
[1].y
= (GLfloat
) dstY0
;
1507 verts
[2].x
= (GLfloat
) dstX1
;
1508 verts
[2].y
= (GLfloat
) dstY1
;
1509 verts
[3].x
= (GLfloat
) dstX0
;
1510 verts
[3].y
= (GLfloat
) dstY1
;
1521 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1524 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1526 /* Restore texture object state, the texture binding will
1527 * be restored by _mesa_meta_end().
1529 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1530 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1531 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1533 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1534 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1537 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1538 _mesa_DeleteSamplers(1, &sampler
);
1540 /* Done with color buffer */
1541 mask
&= ~GL_COLOR_BUFFER_BIT
;
1550 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1551 * of texture mapping and polygon rendering.
1554 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1555 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1556 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1557 GLbitfield mask
, GLenum filter
)
1559 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1560 struct temp_texture
*tex
= get_temp_texture(ctx
);
1561 const GLsizei maxTexSize
= tex
->MaxSize
;
1562 const GLint srcX
= MIN2(srcX0
, srcX1
);
1563 const GLint srcY
= MIN2(srcY0
, srcY1
);
1564 const GLint srcW
= abs(srcX1
- srcX0
);
1565 const GLint srcH
= abs(srcY1
- srcY0
);
1566 const GLboolean srcFlipX
= srcX1
< srcX0
;
1567 const GLboolean srcFlipY
= srcY1
< srcY0
;
1571 struct vertex verts
[4];
1574 /* In addition to falling back if the blit size is larger than the maximum
1575 * texture size, fallback if the source is multisampled. This fallback can
1576 * be removed once Mesa gets support ARB_texture_multisample.
1578 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1579 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1580 /* XXX avoid this fallback */
1581 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1582 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1598 /* only scissor effects blit so save/clear all other relevant state */
1599 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1601 if (blit
->ArrayObj
== 0) {
1602 /* one-time setup */
1604 /* create vertex array object */
1605 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1606 _mesa_BindVertexArray(blit
->ArrayObj
);
1608 /* create vertex array buffer */
1609 _mesa_GenBuffersARB(1, &blit
->VBO
);
1610 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1611 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1612 NULL
, GL_DYNAMIC_DRAW_ARB
);
1614 /* setup vertex arrays */
1615 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1616 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1617 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1618 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1621 _mesa_BindVertexArray(blit
->ArrayObj
);
1622 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1625 /* Try faster, direct texture approach first */
1626 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1627 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1629 _mesa_meta_end(ctx
);
1633 /* Continue with "normal" approach which involves copying the src rect
1634 * into a temporary texture and is "blitted" by drawing a textured quad.
1637 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1639 /* vertex positions/texcoords (after texture allocation!) */
1641 verts
[0].x
= (GLfloat
) dstX0
;
1642 verts
[0].y
= (GLfloat
) dstY0
;
1643 verts
[1].x
= (GLfloat
) dstX1
;
1644 verts
[1].y
= (GLfloat
) dstY0
;
1645 verts
[2].x
= (GLfloat
) dstX1
;
1646 verts
[2].y
= (GLfloat
) dstY1
;
1647 verts
[3].x
= (GLfloat
) dstX0
;
1648 verts
[3].y
= (GLfloat
) dstY1
;
1652 verts
[1].s
= tex
->Sright
;
1654 verts
[2].s
= tex
->Sright
;
1655 verts
[2].t
= tex
->Ttop
;
1657 verts
[3].t
= tex
->Ttop
;
1659 /* upload new vertex data */
1660 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1663 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1665 if (mask
& GL_COLOR_BUFFER_BIT
) {
1666 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1668 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1669 mask
&= ~GL_COLOR_BUFFER_BIT
;
1672 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1673 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1676 init_blit_depth_pixels(ctx
);
1678 /* maybe change tex format here */
1679 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1681 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1682 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1684 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1685 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1687 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1688 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1689 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1690 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1691 _mesa_DepthFunc(GL_ALWAYS
);
1692 _mesa_DepthMask(GL_TRUE
);
1694 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1695 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1701 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1702 /* XXX can't easily do stencil */
1705 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1707 _mesa_meta_end(ctx
);
1710 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1711 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1716 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1718 if (blit
->ArrayObj
) {
1719 _mesa_DeleteVertexArraysAPPLE(1, &blit
->ArrayObj
);
1721 _mesa_DeleteBuffersARB(1, &blit
->VBO
);
1724 if (blit
->DepthFP
) {
1725 _mesa_DeletePrograms(1, &blit
->DepthFP
);
1732 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1735 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1737 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1739 GLfloat x
, y
, z
, r
, g
, b
, a
;
1741 struct vertex verts
[4];
1742 /* save all state but scissor, pixel pack/unpack */
1743 GLbitfield metaSave
= (MESA_META_ALL
-
1745 MESA_META_PIXEL_STORE
-
1746 MESA_META_CONDITIONAL_RENDER
);
1747 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1749 if (buffers
& BUFFER_BITS_COLOR
) {
1750 /* if clearing color buffers, don't save/restore colormask */
1751 metaSave
-= MESA_META_COLOR_MASK
;
1754 _mesa_meta_begin(ctx
, metaSave
);
1756 if (clear
->ArrayObj
== 0) {
1757 /* one-time setup */
1759 /* create vertex array object */
1760 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1761 _mesa_BindVertexArray(clear
->ArrayObj
);
1763 /* create vertex array buffer */
1764 _mesa_GenBuffersARB(1, &clear
->VBO
);
1765 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1767 /* setup vertex arrays */
1768 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1769 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1770 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1771 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1774 _mesa_BindVertexArray(clear
->ArrayObj
);
1775 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1778 /* GL_COLOR_BUFFER_BIT */
1779 if (buffers
& BUFFER_BITS_COLOR
) {
1780 /* leave colormask, glDrawBuffer state as-is */
1782 /* Clears never have the color clamped. */
1783 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1786 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1787 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1790 /* GL_DEPTH_BUFFER_BIT */
1791 if (buffers
& BUFFER_BIT_DEPTH
) {
1792 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1793 _mesa_DepthFunc(GL_ALWAYS
);
1794 _mesa_DepthMask(GL_TRUE
);
1797 assert(!ctx
->Depth
.Test
);
1800 /* GL_STENCIL_BUFFER_BIT */
1801 if (buffers
& BUFFER_BIT_STENCIL
) {
1802 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1803 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1804 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1805 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1806 ctx
->Stencil
.Clear
& stencilMax
,
1807 ctx
->Stencil
.WriteMask
[0]);
1810 assert(!ctx
->Stencil
.Enabled
);
1813 /* vertex positions/colors */
1815 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1816 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1817 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1818 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1819 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1836 for (i
= 0; i
< 4; i
++) {
1837 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1838 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1839 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1840 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1843 /* upload new vertex data */
1844 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1845 GL_DYNAMIC_DRAW_ARB
);
1849 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1851 _mesa_meta_end(ctx
);
1855 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1857 const char *vs_source
=
1858 "attribute vec4 position;\n"
1861 " gl_Position = position;\n"
1863 const char *fs_source
=
1864 "uniform vec4 color;\n"
1867 " gl_FragColor = color;\n"
1869 const char *vs_int_source
=
1871 "in vec4 position;\n"
1874 " gl_Position = position;\n"
1876 const char *fs_int_source
=
1878 "uniform ivec4 color;\n"
1879 "out ivec4 out_color;\n"
1883 " out_color = color;\n"
1887 if (clear
->ArrayObj
!= 0)
1890 /* create vertex array object */
1891 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1892 _mesa_BindVertexArray(clear
->ArrayObj
);
1894 /* create vertex array buffer */
1895 _mesa_GenBuffersARB(1, &clear
->VBO
);
1896 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1898 /* setup vertex arrays */
1899 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1900 _mesa_EnableVertexAttribArrayARB(0);
1902 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1903 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1904 _mesa_CompileShaderARB(vs
);
1906 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1907 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1908 _mesa_CompileShaderARB(fs
);
1910 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1911 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1912 _mesa_DeleteObjectARB(fs
);
1913 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1914 _mesa_DeleteObjectARB(vs
);
1915 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1916 _mesa_LinkProgramARB(clear
->ShaderProg
);
1918 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1921 if (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) {
1922 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1923 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1925 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1926 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1927 _mesa_DeleteObjectARB(fs
);
1928 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1929 _mesa_DeleteObjectARB(vs
);
1930 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1932 /* Note that user-defined out attributes get automatically assigned
1933 * locations starting from 0, so we don't need to explicitly
1934 * BindFragDataLocation to 0.
1937 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1939 clear
->IntegerColorLocation
=
1940 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1945 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
1947 if (clear
->ArrayObj
== 0)
1949 _mesa_DeleteVertexArraysAPPLE(1, &clear
->ArrayObj
);
1950 clear
->ArrayObj
= 0;
1951 _mesa_DeleteBuffersARB(1, &clear
->VBO
);
1953 _mesa_DeleteObjectARB(clear
->ShaderProg
);
1954 clear
->ShaderProg
= 0;
1956 if (clear
->IntegerShaderProg
) {
1957 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
1958 clear
->IntegerShaderProg
= 0;
1963 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1966 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1968 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1969 GLbitfield metaSave
;
1970 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1971 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1972 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1973 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1974 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1975 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1976 const float z
= -invert_z(ctx
->Depth
.Clear
);
1981 metaSave
= (MESA_META_ALPHA_TEST
|
1983 MESA_META_DEPTH_TEST
|
1984 MESA_META_RASTERIZATION
|
1986 MESA_META_STENCIL_TEST
|
1988 MESA_META_VIEWPORT
|
1990 MESA_META_CLAMP_FRAGMENT_COLOR
|
1991 MESA_META_MULTISAMPLE
);
1993 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1994 /* We'll use colormask to disable color writes. Otherwise,
1995 * respect color mask
1997 metaSave
|= MESA_META_COLOR_MASK
;
2000 _mesa_meta_begin(ctx
, metaSave
);
2002 meta_glsl_clear_init(ctx
, clear
);
2004 if (fb
->_IntegerColor
) {
2005 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
2006 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
2007 ctx
->Color
.ClearColor
.i
);
2009 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
2010 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
2011 ctx
->Color
.ClearColor
.f
);
2014 _mesa_BindVertexArray(clear
->ArrayObj
);
2015 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2017 /* GL_COLOR_BUFFER_BIT */
2018 if (buffers
& BUFFER_BITS_COLOR
) {
2019 /* leave colormask, glDrawBuffer state as-is */
2021 /* Clears never have the color clamped. */
2022 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2025 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2026 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2029 /* GL_DEPTH_BUFFER_BIT */
2030 if (buffers
& BUFFER_BIT_DEPTH
) {
2031 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2032 _mesa_DepthFunc(GL_ALWAYS
);
2033 _mesa_DepthMask(GL_TRUE
);
2036 assert(!ctx
->Depth
.Test
);
2039 /* GL_STENCIL_BUFFER_BIT */
2040 if (buffers
& BUFFER_BIT_STENCIL
) {
2041 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2042 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2043 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2044 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2045 ctx
->Stencil
.Clear
& stencilMax
,
2046 ctx
->Stencil
.WriteMask
[0]);
2049 assert(!ctx
->Stencil
.Enabled
);
2052 /* vertex positions */
2066 /* upload new vertex data */
2067 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2068 GL_DYNAMIC_DRAW_ARB
);
2071 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2073 _mesa_meta_end(ctx
);
2077 * Meta implementation of ctx->Driver.CopyPixels() in terms
2078 * of texture mapping and polygon rendering and GLSL shaders.
2081 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2082 GLsizei width
, GLsizei height
,
2083 GLint dstX
, GLint dstY
, GLenum type
)
2085 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2086 struct temp_texture
*tex
= get_temp_texture(ctx
);
2088 GLfloat x
, y
, z
, s
, t
;
2090 struct vertex verts
[4];
2092 GLenum intFormat
= GL_RGBA
;
2094 if (type
!= GL_COLOR
||
2095 ctx
->_ImageTransferState
||
2097 width
> tex
->MaxSize
||
2098 height
> tex
->MaxSize
) {
2099 /* XXX avoid this fallback */
2100 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2104 /* Most GL state applies to glCopyPixels, but a there's a few things
2105 * we need to override:
2107 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2110 MESA_META_TRANSFORM
|
2113 MESA_META_VIEWPORT
));
2115 if (copypix
->ArrayObj
== 0) {
2116 /* one-time setup */
2118 /* create vertex array object */
2119 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2120 _mesa_BindVertexArray(copypix
->ArrayObj
);
2122 /* create vertex array buffer */
2123 _mesa_GenBuffersARB(1, ©pix
->VBO
);
2124 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2125 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2126 NULL
, GL_DYNAMIC_DRAW_ARB
);
2128 /* setup vertex arrays */
2129 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2130 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2131 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2132 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2135 _mesa_BindVertexArray(copypix
->ArrayObj
);
2136 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2139 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2141 /* vertex positions, texcoords (after texture allocation!) */
2143 const GLfloat dstX0
= (GLfloat
) dstX
;
2144 const GLfloat dstY0
= (GLfloat
) dstY
;
2145 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2146 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2147 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2157 verts
[1].s
= tex
->Sright
;
2162 verts
[2].s
= tex
->Sright
;
2163 verts
[2].t
= tex
->Ttop
;
2168 verts
[3].t
= tex
->Ttop
;
2170 /* upload new vertex data */
2171 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2174 /* Alloc/setup texture */
2175 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2176 GL_RGBA
, GL_NEAREST
);
2178 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2180 /* draw textured quad */
2181 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2183 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2185 _mesa_meta_end(ctx
);
2191 * When the glDrawPixels() image size is greater than the max rectangle
2192 * texture size we use this function to break the glDrawPixels() image
2193 * into tiles which fit into the max texture size.
2196 tiled_draw_pixels(struct gl_context
*ctx
,
2198 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2199 GLenum format
, GLenum type
,
2200 const struct gl_pixelstore_attrib
*unpack
,
2201 const GLvoid
*pixels
)
2203 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2206 if (tileUnpack
.RowLength
== 0)
2207 tileUnpack
.RowLength
= width
;
2209 for (i
= 0; i
< width
; i
+= tileSize
) {
2210 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2211 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2213 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2215 for (j
= 0; j
< height
; j
+= tileSize
) {
2216 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2217 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2219 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2221 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2222 format
, type
, &tileUnpack
, pixels
);
2229 * One-time init for drawing stencil pixels.
2232 init_draw_stencil_pixels(struct gl_context
*ctx
)
2234 /* This program is run eight times, once for each stencil bit.
2235 * The stencil values to draw are found in an 8-bit alpha texture.
2236 * We read the texture/stencil value and test if bit 'b' is set.
2237 * If the bit is not set, use KIL to kill the fragment.
2238 * Finally, we use the stencil test to update the stencil buffer.
2240 * The basic algorithm for checking if a bit is set is:
2241 * if (is_odd(value / (1 << bit)))
2242 * result is one (or non-zero).
2245 * The program parameter contains three values:
2246 * parm.x = 255 / (1 << bit)
2250 static const char *program
=
2252 "PARAM parm = program.local[0]; \n"
2254 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2255 "# t = t * 255 / bit \n"
2256 "MUL t.x, t.a, parm.x; \n"
2259 "SUB t.x, t.x, t.y; \n"
2261 "MUL t.x, t.x, parm.y; \n"
2262 "# t = fract(t.x) \n"
2263 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2264 "# t.x = (t.x == 0 ? 1 : 0) \n"
2265 "SGE t.x, -t.x, parm.z; \n"
2267 "# for debug only \n"
2268 "#MOV result.color, t.x; \n"
2270 char program2
[1000];
2271 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2272 struct temp_texture
*tex
= get_temp_texture(ctx
);
2273 const char *texTarget
;
2275 assert(drawpix
->StencilFP
== 0);
2277 /* replace %s with "RECT" or "2D" */
2278 assert(strlen(program
) + 4 < sizeof(program2
));
2279 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2283 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2285 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2286 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2287 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2288 strlen(program2
), (const GLubyte
*) program2
);
2293 * One-time init for drawing depth pixels.
2296 init_draw_depth_pixels(struct gl_context
*ctx
)
2298 static const char *program
=
2300 "PARAM color = program.local[0]; \n"
2301 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2302 "MOV result.color, color; \n"
2305 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2306 struct temp_texture
*tex
= get_temp_texture(ctx
);
2307 const char *texTarget
;
2309 assert(drawpix
->DepthFP
== 0);
2311 /* replace %s with "RECT" or "2D" */
2312 assert(strlen(program
) + 4 < sizeof(program2
));
2313 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2317 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2319 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2320 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2321 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2322 strlen(program2
), (const GLubyte
*) program2
);
2327 * Meta implementation of ctx->Driver.DrawPixels() in terms
2328 * of texture mapping and polygon rendering.
2331 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2332 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2333 GLenum format
, GLenum type
,
2334 const struct gl_pixelstore_attrib
*unpack
,
2335 const GLvoid
*pixels
)
2337 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2338 struct temp_texture
*tex
= get_temp_texture(ctx
);
2339 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2340 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2342 GLfloat x
, y
, z
, s
, t
;
2344 struct vertex verts
[4];
2345 GLenum texIntFormat
;
2346 GLboolean fallback
, newTex
;
2347 GLbitfield metaExtraSave
= 0x0;
2351 * Determine if we can do the glDrawPixels with texture mapping.
2353 fallback
= GL_FALSE
;
2354 if (ctx
->Fog
.Enabled
) {
2358 if (_mesa_is_color_format(format
)) {
2359 /* use more compact format when possible */
2360 /* XXX disable special case for GL_LUMINANCE for now to work around
2361 * apparent i965 driver bug (see bug #23670).
2363 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2364 texIntFormat
= format
;
2366 texIntFormat
= GL_RGBA
;
2368 /* If we're not supposed to clamp the resulting color, then just
2369 * promote our texture to fully float. We could do better by
2370 * just going for the matching set of channels, in floating
2373 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2374 ctx
->Extensions
.ARB_texture_float
)
2375 texIntFormat
= GL_RGBA32F
;
2377 else if (_mesa_is_stencil_format(format
)) {
2378 if (ctx
->Extensions
.ARB_fragment_program
&&
2379 ctx
->Pixel
.IndexShift
== 0 &&
2380 ctx
->Pixel
.IndexOffset
== 0 &&
2381 type
== GL_UNSIGNED_BYTE
) {
2382 /* We'll store stencil as alpha. This only works for GLubyte
2383 * image data because of how incoming values are mapped to alpha
2386 texIntFormat
= GL_ALPHA
;
2387 metaExtraSave
= (MESA_META_COLOR_MASK
|
2388 MESA_META_DEPTH_TEST
|
2389 MESA_META_PIXEL_TRANSFER
|
2391 MESA_META_STENCIL_TEST
);
2397 else if (_mesa_is_depth_format(format
)) {
2398 if (ctx
->Extensions
.ARB_depth_texture
&&
2399 ctx
->Extensions
.ARB_fragment_program
) {
2400 texIntFormat
= GL_DEPTH_COMPONENT
;
2401 metaExtraSave
= (MESA_META_SHADER
);
2412 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2413 format
, type
, unpack
, pixels
);
2418 * Check image size against max texture size, draw as tiles if needed.
2420 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2421 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2422 format
, type
, unpack
, pixels
);
2426 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2427 * but a there's a few things we need to override:
2429 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2432 MESA_META_TRANSFORM
|
2435 MESA_META_VIEWPORT
|
2438 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2440 /* vertex positions, texcoords (after texture allocation!) */
2442 const GLfloat x0
= (GLfloat
) x
;
2443 const GLfloat y0
= (GLfloat
) y
;
2444 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2445 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2446 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2456 verts
[1].s
= tex
->Sright
;
2461 verts
[2].s
= tex
->Sright
;
2462 verts
[2].t
= tex
->Ttop
;
2467 verts
[3].t
= tex
->Ttop
;
2470 if (drawpix
->ArrayObj
== 0) {
2471 /* one-time setup: create vertex array object */
2472 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2474 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2476 /* create vertex array buffer */
2477 _mesa_GenBuffersARB(1, &vbo
);
2478 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2479 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2480 verts
, GL_DYNAMIC_DRAW_ARB
);
2482 /* setup vertex arrays */
2483 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2484 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2485 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2486 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2488 /* set given unpack params */
2489 ctx
->Unpack
= *unpack
;
2491 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2493 if (_mesa_is_stencil_format(format
)) {
2494 /* Drawing stencil */
2497 if (!drawpix
->StencilFP
)
2498 init_draw_stencil_pixels(ctx
);
2500 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2501 GL_ALPHA
, type
, pixels
);
2503 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2505 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2507 /* set all stencil bits to 0 */
2508 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2509 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2510 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2512 /* set stencil bits to 1 where needed */
2513 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2515 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2516 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2518 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2519 const GLuint mask
= 1 << bit
;
2520 if (mask
& origStencilMask
) {
2521 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2522 _mesa_StencilMask(mask
);
2524 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2525 255.0 / mask
, 0.5, 0.0, 0.0);
2527 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2531 else if (_mesa_is_depth_format(format
)) {
2533 if (!drawpix
->DepthFP
)
2534 init_draw_depth_pixels(ctx
);
2536 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2537 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2539 /* polygon color = current raster color */
2540 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2541 ctx
->Current
.RasterColor
);
2543 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2544 format
, type
, pixels
);
2546 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2550 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2551 format
, type
, pixels
);
2552 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2555 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2557 _mesa_DeleteBuffersARB(1, &vbo
);
2559 /* restore unpack params */
2560 ctx
->Unpack
= unpackSave
;
2562 _mesa_meta_end(ctx
);
2566 alpha_test_raster_color(struct gl_context
*ctx
)
2568 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2569 GLfloat ref
= ctx
->Color
.AlphaRef
;
2571 switch (ctx
->Color
.AlphaFunc
) {
2577 return alpha
== ref
;
2579 return alpha
<= ref
;
2583 return alpha
!= ref
;
2585 return alpha
>= ref
;
2595 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2596 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2597 * tracker would improve performance a lot.
2600 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2601 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2602 const struct gl_pixelstore_attrib
*unpack
,
2603 const GLubyte
*bitmap1
)
2605 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2606 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2607 const GLenum texIntFormat
= GL_ALPHA
;
2608 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2611 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2613 struct vertex verts
[4];
2618 * Check if swrast fallback is needed.
2620 if (ctx
->_ImageTransferState
||
2621 ctx
->FragmentProgram
._Enabled
||
2623 ctx
->Texture
._EnabledUnits
||
2624 width
> tex
->MaxSize
||
2625 height
> tex
->MaxSize
) {
2626 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2630 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2633 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2634 * but a there's a few things we need to override:
2636 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2637 MESA_META_PIXEL_STORE
|
2638 MESA_META_RASTERIZATION
|
2641 MESA_META_TRANSFORM
|
2644 MESA_META_VIEWPORT
));
2646 if (bitmap
->ArrayObj
== 0) {
2647 /* one-time setup */
2649 /* create vertex array object */
2650 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2651 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2653 /* create vertex array buffer */
2654 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2655 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2656 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2657 NULL
, GL_DYNAMIC_DRAW_ARB
);
2659 /* setup vertex arrays */
2660 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2661 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2662 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2663 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2664 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2665 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2668 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2669 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2672 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2674 /* vertex positions, texcoords, colors (after texture allocation!) */
2676 const GLfloat x0
= (GLfloat
) x
;
2677 const GLfloat y0
= (GLfloat
) y
;
2678 const GLfloat x1
= (GLfloat
) (x
+ width
);
2679 const GLfloat y1
= (GLfloat
) (y
+ height
);
2680 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2691 verts
[1].s
= tex
->Sright
;
2696 verts
[2].s
= tex
->Sright
;
2697 verts
[2].t
= tex
->Ttop
;
2702 verts
[3].t
= tex
->Ttop
;
2704 for (i
= 0; i
< 4; i
++) {
2705 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2706 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2707 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2708 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2711 /* upload new vertex data */
2712 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2715 /* choose different foreground/background alpha values */
2716 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2717 bg
= (fg
> 127 ? 0 : 255);
2719 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2721 _mesa_meta_end(ctx
);
2725 bitmap8
= malloc(width
* height
);
2727 memset(bitmap8
, bg
, width
* height
);
2728 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2729 bitmap8
, width
, fg
);
2731 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2733 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2734 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2736 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2737 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2739 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2741 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2746 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2748 _mesa_meta_end(ctx
);
2753 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2754 * software fallback. The fallback path will require that the texture
2755 * images are mapped.
2756 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2759 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2760 struct gl_texture_object
*texObj
)
2762 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2763 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2764 struct gl_texture_image
*baseImage
;
2768 /* check for fallbacks */
2769 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2770 target
== GL_TEXTURE_3D
||
2771 target
== GL_TEXTURE_1D_ARRAY
||
2772 target
== GL_TEXTURE_2D_ARRAY
) {
2776 srcLevel
= texObj
->BaseLevel
;
2777 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2778 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2782 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2783 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2784 /* The texture format is sRGB but we can't turn off sRGB->linear
2785 * texture sample conversion. So we won't be able to generate the
2786 * right colors when rendering. Need to use a fallback.
2792 * Test that we can actually render in the texture's format.
2795 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2796 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2798 if (target
== GL_TEXTURE_1D
) {
2799 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2800 GL_COLOR_ATTACHMENT0_EXT
,
2801 target
, texObj
->Name
, srcLevel
);
2804 /* other work is needed to enable 3D mipmap generation */
2805 else if (target
== GL_TEXTURE_3D
) {
2807 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2808 GL_COLOR_ATTACHMENT0_EXT
,
2809 target
, texObj
->Name
, srcLevel
, zoffset
);
2814 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2815 GL_COLOR_ATTACHMENT0_EXT
,
2816 target
, texObj
->Name
, srcLevel
);
2819 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2821 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2823 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2832 * Compute the texture coordinates for the four vertices of a quad for
2833 * drawing a 2D texture image or slice of a cube/3D texture.
2834 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2835 * \param slice slice of a 1D/2D array texture or 3D texture
2836 * \param width width of the texture image
2837 * \param height height of the texture image
2838 * \param coords0/1/2/3 returns the computed texcoords
2841 setup_texture_coords(GLenum faceTarget
,
2850 static const GLfloat st
[4][2] = {
2851 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2856 switch (faceTarget
) {
2860 case GL_TEXTURE_2D_ARRAY
:
2861 if (faceTarget
== GL_TEXTURE_3D
)
2863 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2867 coords0
[0] = 0.0F
; /* s */
2868 coords0
[1] = 0.0F
; /* t */
2869 coords0
[2] = r
; /* r */
2880 case GL_TEXTURE_RECTANGLE_ARB
:
2881 coords0
[0] = 0.0F
; /* s */
2882 coords0
[1] = 0.0F
; /* t */
2883 coords0
[2] = 0.0F
; /* r */
2888 coords2
[1] = height
;
2891 coords3
[1] = height
;
2894 case GL_TEXTURE_1D_ARRAY
:
2895 coords0
[0] = 0.0F
; /* s */
2896 coords0
[1] = slice
; /* t */
2897 coords0
[2] = 0.0F
; /* r */
2909 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2910 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2911 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2912 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2913 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2914 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2915 /* loop over quad verts */
2916 for (i
= 0; i
< 4; i
++) {
2917 /* Compute sc = +/-scale and tc = +/-scale.
2918 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2919 * though that can still sometimes happen with this scale factor...
2921 const GLfloat scale
= 0.9999f
;
2922 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2923 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2943 switch (faceTarget
) {
2944 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2949 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2954 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2959 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2964 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2969 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2980 assert(0 && "unexpected target in meta setup_texture_coords()");
2986 setup_ff_generate_mipmap(struct gl_context
*ctx
,
2987 struct gen_mipmap_state
*mipmap
)
2990 GLfloat x
, y
, tex
[3];
2993 if (mipmap
->ArrayObj
== 0) {
2994 /* one-time setup */
2995 /* create vertex array object */
2996 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2997 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2999 /* create vertex array buffer */
3000 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
3001 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3002 /* setup vertex arrays */
3003 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3004 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3005 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3006 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3009 /* setup projection matrix */
3010 _mesa_MatrixMode(GL_PROJECTION
);
3011 _mesa_LoadIdentity();
3012 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3017 setup_texture_sampler(GLenum target
, struct glsl_sampler
*sampler
)
3021 sampler
->type
= "sampler1D";
3022 sampler
->func
= "texture1D";
3023 sampler
->texcoords
= "texCoords.x";
3026 sampler
->type
= "sampler2D";
3027 sampler
->func
= "texture2D";
3028 sampler
->texcoords
= "texCoords.xy";
3031 /* Code for mipmap generation with 3D textures is not used yet.
3032 * It's a sw fallback.
3034 sampler
->type
= "sampler3D";
3035 sampler
->func
= "texture3D";
3036 sampler
->texcoords
= "texCoords";
3038 case GL_TEXTURE_CUBE_MAP
:
3039 sampler
->type
= "samplerCube";
3040 sampler
->func
= "textureCube";
3041 sampler
->texcoords
= "texCoords";
3043 case GL_TEXTURE_1D_ARRAY
:
3044 sampler
->type
= "sampler1DArray";
3045 sampler
->func
= "texture1DArray";
3046 sampler
->texcoords
= "texCoords.xy";
3048 case GL_TEXTURE_2D_ARRAY
:
3049 sampler
->type
= "sampler2DArray";
3050 sampler
->func
= "texture2DArray";
3051 sampler
->texcoords
= "texCoords";
3054 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3055 " setup_texture_sampler()\n", target
);
3061 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3062 struct gen_mipmap_state
*mipmap
,
3066 GLfloat x
, y
, tex
[3];
3068 struct glsl_sampler sampler
;
3069 const char *vs_source
;
3070 const char *fs_template
;
3072 static const char *vs_int_source
=
3074 "in vec2 position;\n"
3075 "in vec3 textureCoords;\n"
3076 "out vec3 texCoords;\n"
3079 " texCoords = textureCoords;\n"
3080 " gl_Position = gl_Vertex;\n"
3082 static const char *fs_int_source
=
3084 "uniform isampler2D tex2d;\n"
3085 "in vec3 texCoords;\n"
3086 "out ivec4 out_color;\n"
3090 " out_color = texture(tex2d, texCoords.xy);\n"
3093 const char *extension_mode
;
3096 if (ctx
->Const
.GLSLVersion
< 130) {
3098 "attribute vec2 position;\n"
3099 "attribute vec3 textureCoords;\n"
3100 "varying vec3 texCoords;\n"
3103 " texCoords = textureCoords;\n"
3104 " gl_Position = vec4(position, 0.0, 1.0);\n"
3107 "#extension GL_EXT_texture_array : %s\n"
3108 "uniform %s texSampler;\n"
3109 "varying vec3 texCoords;\n"
3112 " gl_FragColor = %s(texSampler, %s);\n"
3117 "in vec2 position;\n"
3118 "in vec3 textureCoords;\n"
3119 "out vec3 texCoords;\n"
3122 " texCoords = textureCoords;\n"
3123 " gl_Position = vec4(position, 0.0, 1.0);\n"
3127 "uniform %s texSampler;\n"
3128 "in vec3 texCoords;\n"
3129 "out %s out_color;\n"
3133 " out_color = texture(texSampler, %s);\n"
3137 /* Check if already initialized */
3138 if (mipmap
->ArrayObj
!= 0)
3140 /* create vertex array object */
3141 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3142 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3144 /* create vertex array buffer */
3145 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
3146 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3148 /* setup vertex arrays */
3149 _mesa_VertexAttribPointerARB(0, 2, GL_FLOAT
, GL_FALSE
,
3150 sizeof(struct vertex
), OFFSET(x
));
3151 _mesa_VertexAttribPointerARB(1, 3, GL_FLOAT
, GL_FALSE
,
3152 sizeof(struct vertex
), OFFSET(tex
));
3154 /* Generate a fragment shader program appropriate for the texture target */
3155 setup_texture_sampler(target
, &sampler
);
3156 mem_ctx
= ralloc_context(NULL
);
3158 if (ctx
->Const
.GLSLVersion
< 130) {
3159 extension_mode
= ((target
== GL_TEXTURE_1D_ARRAY
) ||
3160 (target
== GL_TEXTURE_2D_ARRAY
)) ?
3161 "require" : "disable";
3163 fs_source
= ralloc_asprintf(mem_ctx
, fs_template
,
3164 extension_mode
, sampler
.type
,
3165 sampler
.func
, sampler
.texcoords
);
3168 fs_source
= ralloc_asprintf(mem_ctx
, fs_template
,
3169 sampler
.type
, "vec4",
3173 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3174 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3176 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3177 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3178 _mesa_DeleteObjectARB(fs
);
3179 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3180 _mesa_DeleteObjectARB(vs
);
3181 _mesa_BindAttribLocationARB(mipmap
->ShaderProg
, 0, "position");
3182 _mesa_BindAttribLocationARB(mipmap
->ShaderProg
, 1, "texcoords");
3183 _mesa_EnableVertexAttribArrayARB(0);
3184 _mesa_EnableVertexAttribArrayARB(1);
3185 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3186 ralloc_free(mem_ctx
);
3188 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130) ||
3189 _mesa_is_gles3(ctx
)){
3190 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
3191 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
3193 mipmap
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
3194 _mesa_AttachShader(mipmap
->IntegerShaderProg
, fs
);
3195 _mesa_DeleteObjectARB(fs
);
3196 _mesa_AttachShader(mipmap
->IntegerShaderProg
, vs
);
3197 _mesa_DeleteObjectARB(vs
);
3198 _mesa_BindAttribLocationARB(mipmap
->IntegerShaderProg
, 0, "position");
3199 _mesa_BindAttribLocationARB(mipmap
->IntegerShaderProg
, 1, "texcoords");
3201 /* Note that user-defined out attributes get automatically assigned
3202 * locations starting from 0, so we don't need to explicitly
3203 * BindFragDataLocation to 0.
3205 link_program_with_debug(ctx
, mipmap
->IntegerShaderProg
);
3211 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3212 struct gen_mipmap_state
*mipmap
)
3214 if (mipmap
->ArrayObj
== 0)
3216 _mesa_DeleteVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3217 mipmap
->ArrayObj
= 0;
3218 _mesa_DeleteBuffersARB(1, &mipmap
->VBO
);
3220 _mesa_DeleteObjectARB(mipmap
->ShaderProg
);
3221 mipmap
->ShaderProg
= 0;
3223 if (mipmap
->IntegerShaderProg
) {
3224 _mesa_DeleteObjectARB(mipmap
->IntegerShaderProg
);
3225 mipmap
->IntegerShaderProg
= 0;
3231 * Called via ctx->Driver.GenerateMipmap()
3232 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3236 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3237 struct gl_texture_object
*texObj
)
3239 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3241 GLfloat x
, y
, tex
[3];
3243 struct vertex verts
[4];
3244 const GLuint baseLevel
= texObj
->BaseLevel
;
3245 const GLuint maxLevel
= texObj
->MaxLevel
;
3246 const GLint maxLevelSave
= texObj
->MaxLevel
;
3247 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3248 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
3249 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3250 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3251 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3252 ctx
->Extensions
.ARB_fragment_shader
&&
3253 (ctx
->API
!= API_OPENGLES
);
3256 const GLint slice
= 0;
3259 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3260 _mesa_generate_mipmap(ctx
, target
, texObj
);
3264 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3265 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3266 faceTarget
= target
;
3267 target
= GL_TEXTURE_CUBE_MAP
;
3270 faceTarget
= target
;
3273 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3275 /* Choose between glsl version and fixed function version of
3276 * GenerateMipmap function.
3278 if (use_glsl_version
) {
3279 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3281 if (texObj
->_IsIntegerFormat
)
3282 _mesa_UseProgramObjectARB(mipmap
->IntegerShaderProg
);
3284 _mesa_UseProgramObjectARB(mipmap
->ShaderProg
);
3287 setup_ff_generate_mipmap(ctx
, mipmap
);
3288 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3291 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3292 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3294 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3295 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3297 if (currentTexUnitSave
!= 0)
3298 _mesa_BindTexture(target
, texObj
->Name
);
3301 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
3304 if (!mipmap
->Sampler
) {
3305 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3306 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3308 if (use_glsl_version
&& texObj
->_IsIntegerFormat
)
3309 _mesa_SamplerParameteri(mipmap
->Sampler
,
3310 GL_TEXTURE_MIN_FILTER
,
3311 GL_NEAREST_MIPMAP_NEAREST
);
3313 _mesa_SamplerParameteri(mipmap
->Sampler
,
3314 GL_TEXTURE_MIN_FILTER
,
3315 GL_LINEAR_MIPMAP_LINEAR
);
3317 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3318 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3319 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3320 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3322 /* We don't want to encode or decode sRGB values; treat them as linear.
3323 * This is not technically correct for GLES3 but we don't get any API
3324 * error at the moment.
3326 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3327 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3328 GL_SKIP_DECODE_EXT
);
3332 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3335 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3337 if (ctx
->API
== API_OPENGL
|| ctx
->API
== API_OPENGLES
)
3338 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3340 assert(!genMipmapSave
);
3342 if ((ctx
->Extensions
.EXT_framebuffer_sRGB
&&
3343 _mesa_is_desktop_gl(ctx
)) ||
3344 _mesa_is_gles3(ctx
)) {
3345 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3348 /* Setup texture coordinates */
3349 setup_texture_coords(faceTarget
,
3351 0, 0, /* width, height never used here */
3357 /* setup vertex positions */
3367 /* upload vertex data */
3368 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3369 verts
, GL_DYNAMIC_DRAW_ARB
);
3371 /* texture is already locked, unlock now */
3372 _mesa_unlock_texture(ctx
, texObj
);
3374 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3375 const struct gl_texture_image
*srcImage
;
3376 const GLuint srcLevel
= dstLevel
- 1;
3377 GLsizei srcWidth
, srcHeight
, srcDepth
;
3378 GLsizei dstWidth
, dstHeight
, dstDepth
;
3381 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3382 assert(srcImage
->Border
== 0);
3385 srcWidth
= srcImage
->Width
;
3386 srcHeight
= srcImage
->Height
;
3387 srcDepth
= srcImage
->Depth
;
3390 dstWidth
= MAX2(1, srcWidth
/ 2);
3391 dstHeight
= MAX2(1, srcHeight
/ 2);
3392 dstDepth
= MAX2(1, srcDepth
/ 2);
3394 if (dstWidth
== srcImage
->Width
&&
3395 dstHeight
== srcImage
->Height
&&
3396 dstDepth
== srcImage
->Depth
) {
3401 /* Allocate storage for the destination mipmap image(s) */
3403 /* Set MaxLevel large enough to hold the new level when we allocate it */
3404 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3406 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3407 dstWidth
, dstHeight
, dstDepth
,
3409 srcImage
->InternalFormat
,
3410 srcImage
->TexFormat
)) {
3411 /* All done. We either ran out of memory or we would go beyond the
3412 * last valid level of an immutable texture if we continued.
3417 /* limit minification to src level */
3418 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3420 /* Set to draw into the current dstLevel */
3421 if (target
== GL_TEXTURE_1D
) {
3422 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
3423 GL_COLOR_ATTACHMENT0_EXT
,
3428 else if (target
== GL_TEXTURE_3D
) {
3429 GLint zoffset
= 0; /* XXX unfinished */
3430 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
3431 GL_COLOR_ATTACHMENT0_EXT
,
3438 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
3439 GL_COLOR_ATTACHMENT0_EXT
,
3445 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3448 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3449 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3450 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3451 "_mesa_meta_GenerateMipmap()");
3455 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3456 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3458 /* setup viewport */
3459 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3461 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3464 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3465 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3468 _mesa_lock_texture(ctx
, texObj
); /* relock */
3470 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3472 _mesa_meta_end(ctx
);
3474 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3476 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3478 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3483 * Determine the GL data type to use for the temporary image read with
3484 * ReadPixels() and passed to Tex[Sub]Image().
3487 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3491 baseFormat
= _mesa_get_format_base_format(format
);
3493 switch (baseFormat
) {
3500 case GL_LUMINANCE_ALPHA
:
3502 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3503 return GL_UNSIGNED_BYTE
;
3504 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3505 return GL_UNSIGNED_SHORT
;
3507 return _mesa_get_format_datatype(format
);
3508 case GL_DEPTH_COMPONENT
:
3509 return GL_UNSIGNED_INT
;
3510 case GL_DEPTH_STENCIL
:
3511 return GL_UNSIGNED_INT_24_8
;
3513 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3521 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3522 * Have to be careful with locking and meta state for pixel transfer.
3525 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3526 struct gl_texture_image
*texImage
,
3527 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3528 struct gl_renderbuffer
*rb
,
3530 GLsizei width
, GLsizei height
)
3532 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3533 GLenum format
, type
;
3537 /* Choose format/type for temporary image buffer */
3538 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3539 if (format
== GL_LUMINANCE
||
3540 format
== GL_LUMINANCE_ALPHA
||
3541 format
== GL_INTENSITY
) {
3542 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3543 * temp image buffer because glReadPixels will do L=R+G+B which is
3544 * not what we want (should be L=R).
3549 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3550 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3551 format
= _mesa_base_format_to_integer_format(format
);
3553 bpp
= _mesa_bytes_per_pixel(format
, type
);
3555 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3560 * Alloc image buffer (XXX could use a PBO)
3562 buf
= malloc(width
* height
* bpp
);
3564 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3568 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3571 * Read image from framebuffer (disable pixel transfer ops)
3573 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3574 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3575 format
, type
, &ctx
->Pack
, buf
);
3576 _mesa_meta_end(ctx
);
3578 _mesa_update_state(ctx
); /* to update pixel transfer state */
3581 * Store texture data (with pixel transfer ops)
3583 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3585 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3586 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3587 format
, type
, buf
, &ctx
->Unpack
);
3589 _mesa_meta_end(ctx
);
3591 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3598 * Decompress a texture image by drawing a quad with the compressed
3599 * texture and reading the pixels out of the color buffer.
3600 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3601 * \param destFormat format, ala glReadPixels
3602 * \param destType type, ala glReadPixels
3603 * \param dest destination buffer
3604 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3607 decompress_texture_image(struct gl_context
*ctx
,
3608 struct gl_texture_image
*texImage
,
3610 GLenum destFormat
, GLenum destType
,
3613 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3614 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3615 const GLint width
= texImage
->Width
;
3616 const GLint height
= texImage
->Height
;
3617 const GLenum target
= texObj
->Target
;
3620 GLfloat x
, y
, tex
[3];
3622 struct vertex verts
[4];
3623 GLuint fboDrawSave
, fboReadSave
;
3628 assert(target
== GL_TEXTURE_3D
||
3629 target
== GL_TEXTURE_2D_ARRAY
);
3632 if (target
== GL_TEXTURE_CUBE_MAP
) {
3633 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3636 faceTarget
= target
;
3639 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3640 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3641 fboReadSave
= ctx
->ReadBuffer
->Name
;
3642 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3644 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3646 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3647 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3649 /* Create/bind FBO/renderbuffer */
3650 if (decompress
->FBO
== 0) {
3651 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3652 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3653 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3654 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3655 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3656 GL_COLOR_ATTACHMENT0_EXT
,
3657 GL_RENDERBUFFER_EXT
,
3661 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3664 /* alloc dest surface */
3665 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3666 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3667 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3669 decompress
->Width
= width
;
3670 decompress
->Height
= height
;
3673 /* setup VBO data */
3674 if (decompress
->ArrayObj
== 0) {
3675 /* create vertex array object */
3676 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3677 _mesa_BindVertexArray(decompress
->ArrayObj
);
3679 /* create vertex array buffer */
3680 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3681 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3682 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3683 NULL
, GL_DYNAMIC_DRAW_ARB
);
3685 /* setup vertex arrays */
3686 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3687 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3688 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3689 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3692 _mesa_BindVertexArray(decompress
->ArrayObj
);
3693 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3696 if (!decompress
->Sampler
) {
3697 _mesa_GenSamplers(1, &decompress
->Sampler
);
3698 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3699 /* nearest filtering */
3700 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3701 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3702 /* No sRGB decode or encode.*/
3703 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3704 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3705 GL_SKIP_DECODE_EXT
);
3709 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3712 setup_texture_coords(faceTarget
, slice
, width
, height
,
3718 /* setup vertex positions */
3724 verts
[2].y
= height
;
3726 verts
[3].y
= height
;
3728 /* upload new vertex data */
3729 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3731 /* setup texture state */
3732 _mesa_BindTexture(target
, texObj
->Name
);
3733 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3736 /* save texture object state */
3737 const GLint baseLevelSave
= texObj
->BaseLevel
;
3738 const GLint maxLevelSave
= texObj
->MaxLevel
;
3740 /* restrict sampling to the texture level of interest */
3741 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3742 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3743 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3746 /* No sRGB decode or encode.*/
3747 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_framebuffer_sRGB
)
3748 || _mesa_is_gles3(ctx
)) {
3749 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3752 /* render quad w/ texture into renderbuffer */
3753 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3755 /* Restore texture object state, the texture binding will
3756 * be restored by _mesa_meta_end().
3758 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3759 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3760 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3765 /* read pixels from renderbuffer */
3767 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3769 /* The pixel transfer state will be set to default values at this point
3770 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3771 * turned off (as required by glGetTexImage) but we need to handle some
3772 * special cases. In particular, single-channel texture values are
3773 * returned as red and two-channel texture values are returned as
3776 if (baseTexFormat
== GL_LUMINANCE
||
3777 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3778 baseTexFormat
== GL_INTENSITY
) {
3779 /* Green and blue must be zero */
3780 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3781 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3784 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3787 /* disable texture unit */
3788 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3790 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3792 _mesa_meta_end(ctx
);
3794 /* restore fbo bindings */
3795 if (fboDrawSave
== fboReadSave
) {
3796 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3799 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3800 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3802 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, rbSave
);
3807 * This is just a wrapper around _mesa_get_tex_image() and
3808 * decompress_texture_image(). Meta functions should not be directly called
3812 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3813 GLenum format
, GLenum type
, GLvoid
*pixels
,
3814 struct gl_texture_image
*texImage
)
3816 /* We can only use the decompress-with-blit method here if the texels are
3817 * unsigned, normalized values. We could handle signed and unnormalized
3818 * with floating point renderbuffers...
3820 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3821 _mesa_get_format_datatype(texImage
->TexFormat
)
3822 == GL_UNSIGNED_NORMALIZED
) {
3823 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3824 const GLuint slice
= 0; /* only 2D compressed textures for now */
3825 /* Need to unlock the texture here to prevent deadlock... */
3826 _mesa_unlock_texture(ctx
, texObj
);
3827 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3828 /* ... and relock it */
3829 _mesa_lock_texture(ctx
, texObj
);
3832 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3838 * Meta implementation of ctx->Driver.DrawTex() in terms
3839 * of polygon rendering.
3842 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3843 GLfloat width
, GLfloat height
)
3845 #if FEATURE_OES_draw_texture
3846 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3848 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3850 struct vertex verts
[4];
3853 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3855 MESA_META_TRANSFORM
|
3857 MESA_META_VIEWPORT
));
3859 if (drawtex
->ArrayObj
== 0) {
3860 /* one-time setup */
3861 GLint active_texture
;
3863 /* create vertex array object */
3864 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3865 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3867 /* create vertex array buffer */
3868 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3869 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3870 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3871 NULL
, GL_DYNAMIC_DRAW_ARB
);
3873 /* client active texture is not part of the array object */
3874 active_texture
= ctx
->Array
.ActiveTexture
;
3876 /* setup vertex arrays */
3877 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3878 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3879 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3880 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3881 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3882 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3885 /* restore client active texture */
3886 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3889 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3890 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3893 /* vertex positions, texcoords */
3895 const GLfloat x1
= x
+ width
;
3896 const GLfloat y1
= y
+ height
;
3898 z
= CLAMP(z
, 0.0, 1.0);
3917 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3918 const struct gl_texture_object
*texObj
;
3919 const struct gl_texture_image
*texImage
;
3920 GLfloat s
, t
, s1
, t1
;
3923 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3925 for (j
= 0; j
< 4; j
++) {
3926 verts
[j
].st
[i
][0] = 0.0f
;
3927 verts
[j
].st
[i
][1] = 0.0f
;
3932 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3933 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3934 tw
= texImage
->Width2
;
3935 th
= texImage
->Height2
;
3937 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3938 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3939 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3940 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3942 verts
[0].st
[i
][0] = s
;
3943 verts
[0].st
[i
][1] = t
;
3945 verts
[1].st
[i
][0] = s1
;
3946 verts
[1].st
[i
][1] = t
;
3948 verts
[2].st
[i
][0] = s1
;
3949 verts
[2].st
[i
][1] = t1
;
3951 verts
[3].st
[i
][0] = s
;
3952 verts
[3].st
[i
][1] = t1
;
3955 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3958 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3960 _mesa_meta_end(ctx
);
3961 #endif /* FEATURE_OES_draw_texture */