2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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/queryobj.h"
58 #include "main/readpix.h"
59 #include "main/scissor.h"
60 #include "main/shaderapi.h"
61 #include "main/shaderobj.h"
62 #include "main/state.h"
63 #include "main/stencil.h"
64 #include "main/texobj.h"
65 #include "main/texenv.h"
66 #include "main/texgetimage.h"
67 #include "main/teximage.h"
68 #include "main/texparam.h"
69 #include "main/texstate.h"
70 #include "main/transformfeedback.h"
71 #include "main/uniforms.h"
72 #include "main/varray.h"
73 #include "main/viewport.h"
74 #include "main/samplerobj.h"
75 #include "program/program.h"
76 #include "swrast/swrast.h"
77 #include "drivers/common/meta.h"
78 #include "main/enums.h"
79 #include "main/glformats.h"
80 #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_CLEAR (and others?) */
94 struct gl_query_object
*CurrentOcclusionObject
;
96 /** MESA_META_ALPHA_TEST */
97 GLboolean AlphaEnabled
;
101 /** MESA_META_BLEND */
102 GLbitfield BlendEnabled
;
103 GLboolean ColorLogicOpEnabled
;
105 /** MESA_META_COLOR_MASK */
106 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
108 /** MESA_META_DEPTH_TEST */
109 struct gl_depthbuffer_attrib Depth
;
114 /** MESA_META_PIXEL_STORE */
115 struct gl_pixelstore_attrib Pack
, Unpack
;
117 /** MESA_META_PIXEL_TRANSFER */
118 GLfloat RedBias
, RedScale
;
119 GLfloat GreenBias
, GreenScale
;
120 GLfloat BlueBias
, BlueScale
;
121 GLfloat AlphaBias
, AlphaScale
;
122 GLfloat DepthBias
, DepthScale
;
123 GLboolean MapColorFlag
;
125 /** MESA_META_RASTERIZATION */
126 GLenum FrontPolygonMode
, BackPolygonMode
;
127 GLboolean PolygonOffset
;
128 GLboolean PolygonSmooth
;
129 GLboolean PolygonStipple
;
130 GLboolean PolygonCull
;
132 /** MESA_META_SCISSOR */
133 struct gl_scissor_attrib Scissor
;
135 /** MESA_META_SHADER */
136 GLboolean VertexProgramEnabled
;
137 struct gl_vertex_program
*VertexProgram
;
138 GLboolean FragmentProgramEnabled
;
139 struct gl_fragment_program
*FragmentProgram
;
140 GLboolean ATIFragmentShaderEnabled
;
141 struct gl_shader_program
*Shader
[MESA_SHADER_STAGES
];
142 struct gl_shader_program
*ActiveShader
;
144 /** MESA_META_STENCIL_TEST */
145 struct gl_stencil_attrib Stencil
;
147 /** MESA_META_TRANSFORM */
149 GLfloat ModelviewMatrix
[16];
150 GLfloat ProjectionMatrix
[16];
151 GLfloat TextureMatrix
[16];
153 /** MESA_META_CLIP */
154 GLbitfield ClipPlanesEnabled
;
156 /** MESA_META_TEXTURE */
158 GLuint ClientActiveUnit
;
159 /** for unit[0] only */
160 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
161 /** mask of TEXTURE_2D_BIT, etc */
162 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
163 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
164 GLuint EnvMode
; /* unit[0] only */
166 /** MESA_META_VERTEX */
167 struct gl_array_object
*ArrayObj
;
168 struct gl_buffer_object
*ArrayBufferObj
;
170 /** MESA_META_VIEWPORT */
171 GLfloat ViewportX
, ViewportY
, ViewportW
, ViewportH
;
172 GLclampd DepthNear
, DepthFar
;
174 /** MESA_META_CLAMP_FRAGMENT_COLOR */
175 GLenum ClampFragmentColor
;
177 /** MESA_META_CLAMP_VERTEX_COLOR */
178 GLenum ClampVertexColor
;
180 /** MESA_META_CONDITIONAL_RENDER */
181 struct gl_query_object
*CondRenderQuery
;
182 GLenum CondRenderMode
;
184 /** MESA_META_SELECT_FEEDBACK */
186 struct gl_selection Select
;
187 struct gl_feedback Feedback
;
189 /** MESA_META_MULTISAMPLE */
190 GLboolean MultisampleEnabled
;
192 /** MESA_META_FRAMEBUFFER_SRGB */
193 GLboolean sRGBEnabled
;
195 /** Miscellaneous (always disabled) */
197 GLboolean RasterDiscard
;
198 GLboolean TransformFeedbackNeedsResume
;
202 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
203 * This is currently shared by all the meta ops. But we could create a
204 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
209 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
210 GLsizei MinSize
; /**< Min texture size to allocate */
211 GLsizei MaxSize
; /**< Max possible texture size */
212 GLboolean NPOT
; /**< Non-power of two size OK? */
213 GLsizei Width
, Height
; /**< Current texture size */
215 GLfloat Sright
, Ttop
; /**< right, top texcoords */
220 * State for glBlitFramebufer()
228 GLuint RectShaderProg
;
229 struct temp_texture depthTex
;
234 * State for glClear()
244 GLuint IntegerShaderProg
;
245 GLint IntegerColorLocation
;
246 GLint IntegerLayerLocation
;
251 * State for glCopyPixels()
261 * State for glDrawPixels()
267 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
268 GLuint DepthFP
; /**< Fragment program for drawing depth images */
273 * State for glBitmap()
279 struct temp_texture Tex
; /**< separate texture from other meta ops */
283 * State for GLSL texture sampler which is used to generate fragment
284 * shader in _mesa_meta_generate_mipmap().
286 struct glsl_sampler
{
289 const char *texcoords
;
294 * State for _mesa_meta_generate_mipmap()
296 struct gen_mipmap_state
303 struct glsl_sampler sampler_1d
;
304 struct glsl_sampler sampler_2d
;
305 struct glsl_sampler sampler_3d
;
306 struct glsl_sampler sampler_cubemap
;
307 struct glsl_sampler sampler_1d_array
;
308 struct glsl_sampler sampler_2d_array
;
312 * State for texture decompression
314 struct decompress_state
317 GLuint VBO
, FBO
, RBO
, Sampler
;
322 * State for glDrawTex()
330 #define MAX_META_OPS_DEPTH 8
332 * All per-context meta state.
336 /** Stack of state saved during meta-ops */
337 struct save_state Save
[MAX_META_OPS_DEPTH
];
338 /** Save stack depth */
339 GLuint SaveStackDepth
;
341 struct temp_texture TempTex
;
343 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
344 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
345 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
346 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
347 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
348 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
349 struct decompress_state Decompress
; /**< For texture decompression */
350 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
353 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
354 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
355 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
356 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
357 struct gen_mipmap_state
*mipmap
);
358 static void meta_decompress_cleanup(struct decompress_state
*decompress
);
361 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
367 shader
= _mesa_CreateShaderObjectARB(target
);
368 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
369 _mesa_CompileShader(shader
);
371 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
375 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
377 _mesa_DeleteObjectARB(shader
);
383 _mesa_DeleteObjectARB(shader
);
387 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
389 "meta program compile failed:\n%s\n"
394 _mesa_DeleteObjectARB(shader
);
400 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
405 _mesa_LinkProgram(program
);
407 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
411 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
419 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
420 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
428 * Initialize meta-ops for a context.
429 * To be called once during context creation.
432 _mesa_meta_init(struct gl_context
*ctx
)
436 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
441 * Free context meta-op state.
442 * To be called once during context destruction.
445 _mesa_meta_free(struct gl_context
*ctx
)
447 GET_CURRENT_CONTEXT(old_context
);
448 _mesa_make_current(ctx
, NULL
, NULL
);
449 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
450 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
451 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
452 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
453 meta_decompress_cleanup(&ctx
->Meta
->Decompress
);
455 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
457 _mesa_make_current(NULL
, NULL
, NULL
);
464 * Enter meta state. This is like a light-weight version of glPushAttrib
465 * but it also resets most GL state back to default values.
467 * \param state bitmask of MESA_META_* flags indicating which attribute groups
468 * to save and reset to their defaults
471 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
473 struct save_state
*save
;
475 /* hope MAX_META_OPS_DEPTH is large enough */
476 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
478 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
479 memset(save
, 0, sizeof(*save
));
480 save
->SavedState
= state
;
482 /* Pausing transform feedback needs to be done early, or else we won't be
483 * able to change other state.
485 save
->TransformFeedbackNeedsResume
=
486 _mesa_is_xfb_active_and_unpaused(ctx
);
487 if (save
->TransformFeedbackNeedsResume
)
488 _mesa_PauseTransformFeedback();
490 /* After saving the current occlusion object, call EndQuery so that no
491 * occlusion querying will be active during the meta-operation.
493 if (state
& MESA_META_OCCLUSION_QUERY
) {
494 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
495 if (save
->CurrentOcclusionObject
)
496 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
499 if (state
& MESA_META_ALPHA_TEST
) {
500 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
501 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
502 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
503 if (ctx
->Color
.AlphaEnabled
)
504 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
507 if (state
& MESA_META_BLEND
) {
508 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
509 if (ctx
->Color
.BlendEnabled
) {
510 if (ctx
->Extensions
.EXT_draw_buffers2
) {
512 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
513 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
517 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
520 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
521 if (ctx
->Color
.ColorLogicOpEnabled
)
522 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
525 if (state
& MESA_META_COLOR_MASK
) {
526 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
527 sizeof(ctx
->Color
.ColorMask
));
528 if (!ctx
->Color
.ColorMask
[0][0] ||
529 !ctx
->Color
.ColorMask
[0][1] ||
530 !ctx
->Color
.ColorMask
[0][2] ||
531 !ctx
->Color
.ColorMask
[0][3])
532 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
535 if (state
& MESA_META_DEPTH_TEST
) {
536 save
->Depth
= ctx
->Depth
; /* struct copy */
538 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
541 if ((state
& MESA_META_FOG
)
542 && ctx
->API
!= API_OPENGL_CORE
543 && ctx
->API
!= API_OPENGLES2
) {
544 save
->Fog
= ctx
->Fog
.Enabled
;
545 if (ctx
->Fog
.Enabled
)
546 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
549 if (state
& MESA_META_PIXEL_STORE
) {
550 save
->Pack
= ctx
->Pack
;
551 save
->Unpack
= ctx
->Unpack
;
552 ctx
->Pack
= ctx
->DefaultPacking
;
553 ctx
->Unpack
= ctx
->DefaultPacking
;
556 if (state
& MESA_META_PIXEL_TRANSFER
) {
557 save
->RedScale
= ctx
->Pixel
.RedScale
;
558 save
->RedBias
= ctx
->Pixel
.RedBias
;
559 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
560 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
561 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
562 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
563 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
564 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
565 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
566 ctx
->Pixel
.RedScale
= 1.0F
;
567 ctx
->Pixel
.RedBias
= 0.0F
;
568 ctx
->Pixel
.GreenScale
= 1.0F
;
569 ctx
->Pixel
.GreenBias
= 0.0F
;
570 ctx
->Pixel
.BlueScale
= 1.0F
;
571 ctx
->Pixel
.BlueBias
= 0.0F
;
572 ctx
->Pixel
.AlphaScale
= 1.0F
;
573 ctx
->Pixel
.AlphaBias
= 0.0F
;
574 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
576 ctx
->NewState
|=_NEW_PIXEL
;
579 if (state
& MESA_META_RASTERIZATION
) {
580 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
581 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
582 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
583 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
584 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
585 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
586 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
587 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
588 if (ctx
->API
== API_OPENGL_COMPAT
) {
589 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
590 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
592 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
595 if (state
& MESA_META_SCISSOR
) {
596 save
->Scissor
= ctx
->Scissor
; /* struct copy */
597 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
600 if (state
& MESA_META_SHADER
) {
603 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
604 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
605 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
606 ctx
->VertexProgram
.Current
);
607 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
610 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
611 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
612 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
613 ctx
->FragmentProgram
.Current
);
614 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
617 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
618 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
619 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
622 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
623 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
624 ctx
->Shader
.CurrentProgram
[i
]);
626 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
627 ctx
->Shader
.ActiveProgram
);
632 if (state
& MESA_META_STENCIL_TEST
) {
633 save
->Stencil
= ctx
->Stencil
; /* struct copy */
634 if (ctx
->Stencil
.Enabled
)
635 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
636 /* NOTE: other stencil state not reset */
639 if (state
& MESA_META_TEXTURE
) {
642 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
643 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
644 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
646 /* Disable all texture units */
647 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
648 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
649 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
650 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
651 if (ctx
->Texture
.Unit
[u
].Enabled
||
652 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
653 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
654 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
655 if (ctx
->Extensions
.ARB_texture_cube_map
)
656 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
657 if (_mesa_is_gles(ctx
) &&
658 ctx
->Extensions
.OES_EGL_image_external
)
659 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
661 if (ctx
->API
== API_OPENGL_COMPAT
) {
662 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
663 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
664 if (ctx
->Extensions
.NV_texture_rectangle
)
665 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
666 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
667 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
668 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
669 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
671 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
677 /* save current texture objects for unit[0] only */
678 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
679 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
680 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
683 /* set defaults for unit[0] */
684 _mesa_ActiveTexture(GL_TEXTURE0
);
685 _mesa_ClientActiveTexture(GL_TEXTURE0
);
686 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
687 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
691 if (state
& MESA_META_TRANSFORM
) {
692 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
693 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
694 16 * sizeof(GLfloat
));
695 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
696 16 * sizeof(GLfloat
));
697 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
698 16 * sizeof(GLfloat
));
699 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
700 /* set 1:1 vertex:pixel coordinate transform */
701 _mesa_ActiveTexture(GL_TEXTURE0
);
702 _mesa_MatrixMode(GL_TEXTURE
);
703 _mesa_LoadIdentity();
704 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
705 _mesa_MatrixMode(GL_MODELVIEW
);
706 _mesa_LoadIdentity();
707 _mesa_MatrixMode(GL_PROJECTION
);
708 _mesa_LoadIdentity();
710 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
711 * This can occur when there is no draw buffer.
713 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
714 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
715 0.0, ctx
->DrawBuffer
->Height
,
719 if (state
& MESA_META_CLIP
) {
720 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
721 if (ctx
->Transform
.ClipPlanesEnabled
) {
723 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
724 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
729 if (state
& MESA_META_VERTEX
) {
730 /* save vertex array object state */
731 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
732 ctx
->Array
.ArrayObj
);
733 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
734 ctx
->Array
.ArrayBufferObj
);
735 /* set some default state? */
738 if (state
& MESA_META_VIEWPORT
) {
739 /* save viewport state */
740 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
741 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
742 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
743 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
744 /* set viewport to match window size */
745 if (ctx
->ViewportArray
[0].X
!= 0 ||
746 ctx
->ViewportArray
[0].Y
!= 0 ||
747 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
748 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
749 _mesa_set_viewport(ctx
, 0, 0, 0,
750 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
752 /* save depth range state */
753 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
754 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
755 /* set depth range to default */
756 _mesa_DepthRange(0.0, 1.0);
759 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
760 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
762 /* Generally in here we want to do clamping according to whether
763 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
764 * regardless of the internal implementation of the metaops.
766 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
767 ctx
->Extensions
.ARB_color_buffer_float
)
768 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
771 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
772 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
774 /* Generally in here we never want vertex color clamping --
775 * result clamping is only dependent on fragment clamping.
777 if (ctx
->Extensions
.ARB_color_buffer_float
)
778 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
781 if (state
& MESA_META_CONDITIONAL_RENDER
) {
782 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
783 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
785 if (ctx
->Query
.CondRenderQuery
)
786 _mesa_EndConditionalRender();
789 if (state
& MESA_META_SELECT_FEEDBACK
) {
790 save
->RenderMode
= ctx
->RenderMode
;
791 if (ctx
->RenderMode
== GL_SELECT
) {
792 save
->Select
= ctx
->Select
; /* struct copy */
793 _mesa_RenderMode(GL_RENDER
);
794 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
795 save
->Feedback
= ctx
->Feedback
; /* struct copy */
796 _mesa_RenderMode(GL_RENDER
);
800 if (state
& MESA_META_MULTISAMPLE
) {
801 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
802 if (ctx
->Multisample
.Enabled
)
803 _mesa_set_multisample(ctx
, GL_FALSE
);
806 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
807 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
808 if (ctx
->Color
.sRGBEnabled
)
809 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
814 save
->Lighting
= ctx
->Light
.Enabled
;
815 if (ctx
->Light
.Enabled
)
816 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
817 save
->RasterDiscard
= ctx
->RasterDiscard
;
818 if (ctx
->RasterDiscard
)
819 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
825 * Leave meta state. This is like a light-weight version of glPopAttrib().
828 _mesa_meta_end(struct gl_context
*ctx
)
830 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
831 const GLbitfield state
= save
->SavedState
;
834 /* After starting a new occlusion query, initialize the results to the
835 * values saved previously. The driver will then continue to increment
838 if (state
& MESA_META_OCCLUSION_QUERY
) {
839 if (save
->CurrentOcclusionObject
) {
840 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
841 save
->CurrentOcclusionObject
->Id
);
842 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
846 if (state
& MESA_META_ALPHA_TEST
) {
847 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
848 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
849 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
852 if (state
& MESA_META_BLEND
) {
853 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
854 if (ctx
->Extensions
.EXT_draw_buffers2
) {
856 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
857 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
861 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
864 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
865 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
868 if (state
& MESA_META_COLOR_MASK
) {
870 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
871 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
873 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
874 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
878 save
->ColorMask
[i
][0],
879 save
->ColorMask
[i
][1],
880 save
->ColorMask
[i
][2],
881 save
->ColorMask
[i
][3]);
887 if (state
& MESA_META_DEPTH_TEST
) {
888 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
889 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
890 _mesa_DepthFunc(save
->Depth
.Func
);
891 _mesa_DepthMask(save
->Depth
.Mask
);
894 if ((state
& MESA_META_FOG
)
895 && ctx
->API
!= API_OPENGL_CORE
896 && ctx
->API
!= API_OPENGLES2
) {
897 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
900 if (state
& MESA_META_PIXEL_STORE
) {
901 ctx
->Pack
= save
->Pack
;
902 ctx
->Unpack
= save
->Unpack
;
905 if (state
& MESA_META_PIXEL_TRANSFER
) {
906 ctx
->Pixel
.RedScale
= save
->RedScale
;
907 ctx
->Pixel
.RedBias
= save
->RedBias
;
908 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
909 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
910 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
911 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
912 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
913 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
914 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
916 ctx
->NewState
|=_NEW_PIXEL
;
919 if (state
& MESA_META_RASTERIZATION
) {
920 /* Core context requires that front and back mode be the same.
922 if (ctx
->API
== API_OPENGL_CORE
) {
923 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
925 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
926 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
928 if (ctx
->API
== API_OPENGL_COMPAT
) {
929 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
930 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
932 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
933 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
936 if (state
& MESA_META_SCISSOR
) {
939 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
940 _mesa_set_scissor(ctx
, i
,
941 save
->Scissor
.ScissorArray
[i
].X
,
942 save
->Scissor
.ScissorArray
[i
].Y
,
943 save
->Scissor
.ScissorArray
[i
].Width
,
944 save
->Scissor
.ScissorArray
[i
].Height
);
945 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
946 (save
->Scissor
.EnableFlags
>> i
) & 1);
950 if (state
& MESA_META_SHADER
) {
951 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
952 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
953 save
->VertexProgramEnabled
);
954 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
955 save
->VertexProgram
);
956 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
959 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
960 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
961 save
->FragmentProgramEnabled
);
962 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
963 save
->FragmentProgram
);
964 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
967 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
968 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
969 save
->ATIFragmentShaderEnabled
);
972 if (ctx
->Extensions
.ARB_vertex_shader
) {
973 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
,
974 save
->Shader
[MESA_SHADER_VERTEX
]);
977 if (_mesa_has_geometry_shaders(ctx
))
978 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
979 save
->Shader
[MESA_SHADER_GEOMETRY
]);
981 if (ctx
->Extensions
.ARB_fragment_shader
)
982 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
983 save
->Shader
[MESA_SHADER_FRAGMENT
]);
985 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
988 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
989 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
990 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
993 if (state
& MESA_META_STENCIL_TEST
) {
994 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
996 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
997 _mesa_ClearStencil(stencil
->Clear
);
998 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
999 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1000 stencil
->TestTwoSide
);
1001 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1002 ? GL_BACK
: GL_FRONT
);
1005 _mesa_StencilFuncSeparate(GL_FRONT
,
1006 stencil
->Function
[0],
1008 stencil
->ValueMask
[0]);
1009 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1010 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1011 stencil
->ZFailFunc
[0],
1012 stencil
->ZPassFunc
[0]);
1014 _mesa_StencilFuncSeparate(GL_BACK
,
1015 stencil
->Function
[1],
1017 stencil
->ValueMask
[1]);
1018 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1019 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1020 stencil
->ZFailFunc
[1],
1021 stencil
->ZPassFunc
[1]);
1024 if (state
& MESA_META_TEXTURE
) {
1027 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1029 /* restore texenv for unit[0] */
1030 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1031 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1034 /* restore texture objects for unit[0] only */
1035 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1036 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1037 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1038 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1039 save
->CurrentTexture
[tgt
]);
1041 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1044 /* Restore fixed function texture enables, texgen */
1045 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1046 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1047 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1048 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1049 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1052 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1053 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1054 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1059 /* restore current unit state */
1060 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1061 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1064 if (state
& MESA_META_TRANSFORM
) {
1065 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1066 _mesa_ActiveTexture(GL_TEXTURE0
);
1067 _mesa_MatrixMode(GL_TEXTURE
);
1068 _mesa_LoadMatrixf(save
->TextureMatrix
);
1069 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1071 _mesa_MatrixMode(GL_MODELVIEW
);
1072 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1074 _mesa_MatrixMode(GL_PROJECTION
);
1075 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1077 _mesa_MatrixMode(save
->MatrixMode
);
1080 if (state
& MESA_META_CLIP
) {
1081 if (save
->ClipPlanesEnabled
) {
1083 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1084 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1085 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1091 if (state
& MESA_META_VERTEX
) {
1092 /* restore vertex buffer object */
1093 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1094 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1096 /* restore vertex array object */
1097 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1098 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1101 if (state
& MESA_META_VIEWPORT
) {
1102 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1103 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1104 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1105 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1106 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1107 save
->ViewportW
, save
->ViewportH
);
1109 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1112 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1113 ctx
->Extensions
.ARB_color_buffer_float
) {
1114 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1117 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1118 ctx
->Extensions
.ARB_color_buffer_float
) {
1119 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1122 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1123 if (save
->CondRenderQuery
)
1124 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1125 save
->CondRenderMode
);
1128 if (state
& MESA_META_SELECT_FEEDBACK
) {
1129 if (save
->RenderMode
== GL_SELECT
) {
1130 _mesa_RenderMode(GL_SELECT
);
1131 ctx
->Select
= save
->Select
;
1132 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1133 _mesa_RenderMode(GL_FEEDBACK
);
1134 ctx
->Feedback
= save
->Feedback
;
1138 if (state
& MESA_META_MULTISAMPLE
) {
1139 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1140 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1143 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1144 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1145 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1149 if (save
->Lighting
) {
1150 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1152 if (save
->RasterDiscard
) {
1153 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1155 if (save
->TransformFeedbackNeedsResume
)
1156 _mesa_ResumeTransformFeedback();
1158 ctx
->Meta
->SaveStackDepth
--;
1163 * Determine whether Mesa is currently in a meta state.
1166 _mesa_meta_in_progress(struct gl_context
*ctx
)
1168 return ctx
->Meta
->SaveStackDepth
!= 0;
1173 * Convert Z from a normalized value in the range [0, 1] to an object-space
1174 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1175 * default/identity ortho projection results in the original Z value.
1176 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1177 * value comes from the clear value or raster position.
1179 static INLINE GLfloat
1180 invert_z(GLfloat normZ
)
1182 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1188 * One-time init for a temp_texture object.
1189 * Choose tex target, compute max tex size, etc.
1192 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1194 /* prefer texture rectangle */
1195 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1196 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1197 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1198 tex
->NPOT
= GL_TRUE
;
1201 /* use 2D texture, NPOT if possible */
1202 tex
->Target
= GL_TEXTURE_2D
;
1203 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1204 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1206 tex
->MinSize
= 16; /* 16 x 16 at least */
1207 assert(tex
->MaxSize
> 0);
1209 _mesa_GenTextures(1, &tex
->TexObj
);
1213 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1217 _mesa_DeleteTextures(1, &tex
->TexObj
);
1223 * Return pointer to temp_texture info for non-bitmap ops.
1224 * This does some one-time init if needed.
1226 static struct temp_texture
*
1227 get_temp_texture(struct gl_context
*ctx
)
1229 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1232 init_temp_texture(ctx
, tex
);
1240 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1241 * We use a separate texture for bitmaps to reduce texture
1242 * allocation/deallocation.
1244 static struct temp_texture
*
1245 get_bitmap_temp_texture(struct gl_context
*ctx
)
1247 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1250 init_temp_texture(ctx
, tex
);
1257 * Return pointer to depth temp_texture.
1258 * This does some one-time init if needed.
1260 static struct temp_texture
*
1261 get_temp_depth_texture(struct gl_context
*ctx
)
1263 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1266 init_temp_texture(ctx
, tex
);
1273 * Compute the width/height of texture needed to draw an image of the
1274 * given size. Return a flag indicating whether the current texture
1275 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1276 * allocated (glTexImage2D).
1277 * Also, compute s/t texcoords for drawing.
1279 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1282 alloc_texture(struct temp_texture
*tex
,
1283 GLsizei width
, GLsizei height
, GLenum intFormat
)
1285 GLboolean newTex
= GL_FALSE
;
1287 ASSERT(width
<= tex
->MaxSize
);
1288 ASSERT(height
<= tex
->MaxSize
);
1290 if (width
> tex
->Width
||
1291 height
> tex
->Height
||
1292 intFormat
!= tex
->IntFormat
) {
1293 /* alloc new texture (larger or different format) */
1296 /* use non-power of two size */
1297 tex
->Width
= MAX2(tex
->MinSize
, width
);
1298 tex
->Height
= MAX2(tex
->MinSize
, height
);
1301 /* find power of two size */
1303 w
= h
= tex
->MinSize
;
1312 tex
->IntFormat
= intFormat
;
1317 /* compute texcoords */
1318 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1319 tex
->Sright
= (GLfloat
) width
;
1320 tex
->Ttop
= (GLfloat
) height
;
1323 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1324 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1332 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1335 setup_copypix_texture(struct gl_context
*ctx
,
1336 struct temp_texture
*tex
,
1338 GLint srcX
, GLint srcY
,
1339 GLsizei width
, GLsizei height
, GLenum intFormat
,
1342 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1343 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1344 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1345 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1346 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1348 /* copy framebuffer image to texture */
1350 /* create new tex image */
1351 if (tex
->Width
== width
&& tex
->Height
== height
) {
1352 /* create new tex with framebuffer data */
1353 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1354 srcX
, srcY
, width
, height
, 0);
1357 /* create empty texture */
1358 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1359 tex
->Width
, tex
->Height
, 0,
1360 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1362 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1363 0, 0, srcX
, srcY
, width
, height
);
1367 /* replace existing tex image */
1368 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1369 0, 0, srcX
, srcY
, width
, height
);
1375 * Setup/load texture for glDrawPixels.
1378 setup_drawpix_texture(struct gl_context
*ctx
,
1379 struct temp_texture
*tex
,
1381 GLenum texIntFormat
,
1382 GLsizei width
, GLsizei height
,
1383 GLenum format
, GLenum type
,
1384 const GLvoid
*pixels
)
1386 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1387 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1388 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1389 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1390 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1392 /* copy pixel data to texture */
1394 /* create new tex image */
1395 if (tex
->Width
== width
&& tex
->Height
== height
) {
1396 /* create new tex and load image data */
1397 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1398 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1401 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1403 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1404 ctx
->Unpack
.BufferObj
);
1405 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1406 /* create empty texture */
1407 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1408 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1409 if (save_unpack_obj
!= NULL
)
1410 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1411 save_unpack_obj
->Name
);
1413 _mesa_TexSubImage2D(tex
->Target
, 0,
1414 0, 0, width
, height
, format
, type
, pixels
);
1418 /* replace existing tex image */
1419 _mesa_TexSubImage2D(tex
->Target
, 0,
1420 0, 0, width
, height
, format
, type
, pixels
);
1427 * One-time init for drawing depth pixels.
1430 init_blit_depth_pixels(struct gl_context
*ctx
)
1432 static const char *program
=
1434 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1437 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1438 struct temp_texture
*tex
= get_temp_texture(ctx
);
1439 const char *texTarget
;
1441 assert(blit
->DepthFP
== 0);
1443 /* replace %s with "RECT" or "2D" */
1444 assert(strlen(program
) + 4 < sizeof(program2
));
1445 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1449 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1451 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1452 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1453 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1454 strlen(program2
), (const GLubyte
*) program2
);
1458 setup_ff_blit_framebuffer(struct gl_context
*ctx
,
1459 struct blit_state
*blit
)
1464 struct vertex verts
[4];
1466 if (blit
->ArrayObj
== 0) {
1467 /* one-time setup */
1469 /* create vertex array object */
1470 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1471 _mesa_BindVertexArray(blit
->ArrayObj
);
1473 /* create vertex array buffer */
1474 _mesa_GenBuffers(1, &blit
->VBO
);
1475 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1476 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1477 NULL
, GL_DYNAMIC_DRAW_ARB
);
1479 /* setup vertex arrays */
1480 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1481 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1482 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1483 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1486 /* setup projection matrix */
1487 _mesa_MatrixMode(GL_PROJECTION
);
1488 _mesa_LoadIdentity();
1489 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1494 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1495 struct blit_state
*blit
,
1501 struct vertex verts
[4];
1502 const char *vs_source
;
1507 GLboolean texture_2d
= (target
== GL_TEXTURE_2D
);
1509 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1510 assert(_mesa_is_desktop_gl(ctx
) || texture_2d
);
1512 /* Check if already initialized */
1513 if (blit
->ArrayObj
== 0) {
1515 /* create vertex array object */
1516 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1517 _mesa_BindVertexArray(blit
->ArrayObj
);
1519 /* create vertex array buffer */
1520 _mesa_GenBuffers(1, &blit
->VBO
);
1521 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1522 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1523 NULL
, GL_DYNAMIC_DRAW_ARB
);
1525 /* setup vertex arrays */
1526 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
1527 sizeof(struct vertex
), OFFSET(x
));
1528 _mesa_VertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
,
1529 sizeof(struct vertex
), OFFSET(s
));
1531 _mesa_EnableVertexAttribArray(0);
1532 _mesa_EnableVertexAttribArray(1);
1535 /* Generate a relevant fragment shader program for the texture target */
1536 if ((target
== GL_TEXTURE_2D
&& blit
->ShaderProg
!= 0) ||
1537 (target
== GL_TEXTURE_RECTANGLE
&& blit
->RectShaderProg
!= 0)) {
1541 mem_ctx
= ralloc_context(NULL
);
1543 if (ctx
->Const
.GLSLVersion
< 130) {
1545 "attribute vec2 position;\n"
1546 "attribute vec2 textureCoords;\n"
1547 "varying vec2 texCoords;\n"
1550 " texCoords = textureCoords;\n"
1551 " gl_Position = vec4(position, 0.0, 1.0);\n"
1554 fs_source
= ralloc_asprintf(mem_ctx
,
1556 "precision highp float;\n"
1558 "uniform %s texSampler;\n"
1559 "varying vec2 texCoords;\n"
1562 " gl_FragColor = %s(texSampler, texCoords);\n"
1563 " gl_FragDepth = gl_FragColor.r;\n"
1565 texture_2d
? "sampler2D" : "sampler2DRect",
1566 texture_2d
? "texture2D" : "texture2DRect");
1569 vs_source
= ralloc_asprintf(mem_ctx
,
1571 "in vec2 position;\n"
1572 "in vec2 textureCoords;\n"
1573 "out vec2 texCoords;\n"
1576 " texCoords = textureCoords;\n"
1577 " gl_Position = vec4(position, 0.0, 1.0);\n"
1579 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1580 fs_source
= ralloc_asprintf(mem_ctx
,
1583 "precision highp float;\n"
1585 "uniform %s texSampler;\n"
1586 "in vec2 texCoords;\n"
1587 "out vec4 out_color;\n"
1591 " out_color = %s(texSampler, texCoords);\n"
1592 " gl_FragDepth = out_color.r;\n"
1594 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
1595 texture_2d
? "sampler2D" : "sampler2DRect",
1596 texture_2d
? "texture" : "texture2DRect");
1599 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
1600 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
1602 ShaderProg
= _mesa_CreateProgramObjectARB();
1603 _mesa_AttachShader(ShaderProg
, fs
);
1604 _mesa_DeleteObjectARB(fs
);
1605 _mesa_AttachShader(ShaderProg
, vs
);
1606 _mesa_DeleteObjectARB(vs
);
1607 _mesa_BindAttribLocation(ShaderProg
, 0, "position");
1608 _mesa_BindAttribLocation(ShaderProg
, 1, "texcoords");
1609 link_program_with_debug(ctx
, ShaderProg
);
1610 ralloc_free(mem_ctx
);
1612 blit
->ShaderProg
= ShaderProg
;
1614 blit
->RectShaderProg
= ShaderProg
;
1618 * Try to do a glBlitFramebuffer using no-copy texturing.
1619 * We can do this when the src renderbuffer is actually a texture.
1620 * But if the src buffer == dst buffer we cannot do this.
1622 * \return new buffer mask indicating the buffers left to blit using the
1626 blitframebuffer_texture(struct gl_context
*ctx
,
1627 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1628 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1629 GLbitfield mask
, GLenum filter
, GLint flipX
,
1630 GLint flipY
, GLboolean glsl_version
)
1632 if (mask
& GL_COLOR_BUFFER_BIT
) {
1633 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1634 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1635 const struct gl_renderbuffer_attachment
*drawAtt
;
1636 const struct gl_renderbuffer_attachment
*readAtt
=
1637 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1639 if (readAtt
&& readAtt
->Texture
) {
1640 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1641 const GLint dstX
= MIN2(dstX0
, dstX1
);
1642 const GLint dstY
= MIN2(dstY0
, dstY1
);
1643 const GLint dstW
= abs(dstX1
- dstX0
);
1644 const GLint dstH
= abs(dstY1
- dstY0
);
1645 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1646 const GLuint srcLevel
= readAtt
->TextureLevel
;
1647 const GLint baseLevelSave
= texObj
->BaseLevel
;
1648 const GLint maxLevelSave
= texObj
->MaxLevel
;
1649 const GLenum target
= texObj
->Target
;
1650 GLuint sampler
, samplerSave
=
1651 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1652 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1655 /* Iterate through all draw buffers */
1656 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1657 int idx
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1660 drawAtt
= &drawFb
->Attachment
[idx
];
1662 if (drawAtt
->Texture
== readAtt
->Texture
) {
1663 /* Can't use same texture as both the source and dest. We need
1664 * to handle overlapping blits and besides, some hw may not
1671 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1672 /* Can't handle other texture types at this time */
1676 /* Choose between glsl version and fixed function version of
1677 * BlitFramebuffer function.
1680 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1681 if (target
== GL_TEXTURE_2D
)
1682 _mesa_UseProgram(blit
->ShaderProg
);
1684 _mesa_UseProgram(blit
->RectShaderProg
);
1687 setup_ff_blit_framebuffer(ctx
, &ctx
->Meta
->Blit
);
1690 _mesa_BindVertexArray(blit
->ArrayObj
);
1691 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1693 _mesa_GenSamplers(1, &sampler
);
1694 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1697 printf("Blit from texture!\n");
1698 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1699 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1702 /* Prepare src texture state */
1703 _mesa_BindTexture(target
, texObj
->Name
);
1704 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1705 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1706 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1707 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1708 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1710 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1711 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1713 /* Always do our blits with no sRGB decode or encode. Note that
1714 * GL_FRAMEBUFFER_SRGB has already been disabled by
1715 * _mesa_meta_begin().
1717 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1718 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1719 GL_SKIP_DECODE_EXT
);
1722 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1723 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1724 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1727 /* Prepare vertex data (the VBO was previously created and bound) */
1732 struct vertex verts
[4];
1733 GLfloat s0
, t0
, s1
, t1
;
1735 if (target
== GL_TEXTURE_2D
) {
1736 const struct gl_texture_image
*texImage
1737 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1738 s0
= srcX0
/ (float) texImage
->Width
;
1739 s1
= srcX1
/ (float) texImage
->Width
;
1740 t0
= srcY0
/ (float) texImage
->Height
;
1741 t1
= srcY1
/ (float) texImage
->Height
;
1744 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1751 /* setup vertex positions */
1752 verts
[0].x
= -1.0F
* flipX
;
1753 verts
[0].y
= -1.0F
* flipY
;
1754 verts
[1].x
= 1.0F
* flipX
;
1755 verts
[1].y
= -1.0F
* flipY
;
1756 verts
[2].x
= 1.0F
* flipX
;
1757 verts
[2].y
= 1.0F
* flipY
;
1758 verts
[3].x
= -1.0F
* flipX
;
1759 verts
[3].y
= 1.0F
* flipY
;
1770 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1773 /* setup viewport */
1774 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1775 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1776 _mesa_DepthMask(GL_FALSE
);
1777 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1779 /* Restore texture object state, the texture binding will
1780 * be restored by _mesa_meta_end().
1782 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1783 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1784 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1787 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1788 _mesa_DeleteSamplers(1, &sampler
);
1790 /* Done with color buffer */
1791 mask
&= ~GL_COLOR_BUFFER_BIT
;
1800 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1801 * of texture mapping and polygon rendering.
1804 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1805 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1806 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1807 GLbitfield mask
, GLenum filter
)
1809 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1810 struct temp_texture
*tex
= get_temp_texture(ctx
);
1811 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1812 const GLsizei maxTexSize
= tex
->MaxSize
;
1813 const GLint srcX
= MIN2(srcX0
, srcX1
);
1814 const GLint srcY
= MIN2(srcY0
, srcY1
);
1815 const GLint srcW
= abs(srcX1
- srcX0
);
1816 const GLint srcH
= abs(srcY1
- srcY0
);
1817 const GLint dstX
= MIN2(dstX0
, dstX1
);
1818 const GLint dstY
= MIN2(dstY0
, dstY1
);
1819 const GLint dstW
= abs(dstX1
- dstX0
);
1820 const GLint dstH
= abs(dstY1
- dstY0
);
1821 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1822 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1823 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1824 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1825 const GLint flipX
= srcFlipX
* dstFlipX
;
1826 const GLint flipY
= srcFlipY
* dstFlipY
;
1831 struct vertex verts
[4];
1833 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1834 ctx
->Extensions
.ARB_fragment_shader
&&
1835 (ctx
->API
!= API_OPENGLES
);
1837 /* In addition to falling back if the blit size is larger than the maximum
1838 * texture size, fallback if the source is multisampled. This fallback can
1839 * be removed once Mesa gets support ARB_texture_multisample.
1841 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1842 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1843 /* XXX avoid this fallback */
1844 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1845 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1849 /* only scissor effects blit so save/clear all other relevant state */
1850 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1852 /* Try faster, direct texture approach first */
1853 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1854 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1855 dstFlipX
, dstFlipY
, use_glsl_version
);
1857 _mesa_meta_end(ctx
);
1861 /* Choose between glsl version and fixed function version of
1862 * BlitFramebuffer function.
1864 if (use_glsl_version
) {
1865 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1866 if (tex
->Target
== GL_TEXTURE_2D
)
1867 _mesa_UseProgram(blit
->ShaderProg
);
1869 _mesa_UseProgram(blit
->RectShaderProg
);
1872 setup_ff_blit_framebuffer(ctx
, blit
);
1875 _mesa_BindVertexArray(blit
->ArrayObj
);
1876 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1878 /* Continue with "normal" approach which involves copying the src rect
1879 * into a temporary texture and is "blitted" by drawing a textured quad.
1882 /* setup vertex positions */
1883 verts
[0].x
= -1.0F
* flipX
;
1884 verts
[0].y
= -1.0F
* flipY
;
1885 verts
[1].x
= 1.0F
* flipX
;
1886 verts
[1].y
= -1.0F
* flipY
;
1887 verts
[2].x
= 1.0F
* flipX
;
1888 verts
[2].y
= 1.0F
* flipY
;
1889 verts
[3].x
= -1.0F
* flipX
;
1890 verts
[3].y
= 1.0F
* flipY
;
1894 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1897 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1898 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1900 if (mask
& GL_COLOR_BUFFER_BIT
) {
1901 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1902 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1903 const GLenum rb_base_format
=
1904 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1906 /* Using the exact source rectangle to create the texture does incorrect
1907 * linear filtering along the edges. So, allocate the texture extended along
1908 * edges by one pixel in x, y directions.
1910 newTex
= alloc_texture(tex
, srcW
+ 2, srcH
+ 2, rb_base_format
);
1911 setup_copypix_texture(ctx
, tex
, newTex
,
1912 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1913 rb_base_format
, filter
);
1914 /* texcoords (after texture allocation!) */
1918 verts
[1].s
= tex
->Sright
- 1.0F
;
1920 verts
[2].s
= tex
->Sright
- 1.0F
;
1921 verts
[2].t
= tex
->Ttop
- 1.0F
;
1923 verts
[3].t
= tex
->Ttop
- 1.0F
;
1925 /* upload new vertex data */
1926 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1929 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1930 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1931 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1932 _mesa_DepthMask(GL_FALSE
);
1933 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1934 mask
&= ~GL_COLOR_BUFFER_BIT
;
1937 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1938 _mesa_is_desktop_gl(ctx
) &&
1939 ctx
->Extensions
.ARB_depth_texture
&&
1940 ctx
->Extensions
.ARB_fragment_program
) {
1942 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1946 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1947 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1948 GL_UNSIGNED_INT
, tmp
);
1949 setup_drawpix_texture(ctx
, depthTex
, newTex
, GL_DEPTH_COMPONENT
,
1950 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1951 GL_UNSIGNED_INT
, tmp
);
1953 /* texcoords (after texture allocation!) */
1957 verts
[1].s
= depthTex
->Sright
;
1959 verts
[2].s
= depthTex
->Sright
;
1960 verts
[2].t
= depthTex
->Ttop
;
1962 verts
[3].t
= depthTex
->Ttop
;
1964 /* upload new vertex data */
1965 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1969 init_blit_depth_pixels(ctx
);
1971 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1972 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1973 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1974 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1975 _mesa_DepthFunc(GL_ALWAYS
);
1976 _mesa_DepthMask(GL_TRUE
);
1978 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1979 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1980 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1981 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1987 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1988 /* XXX can't easily do stencil */
1991 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1992 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1994 _mesa_meta_end(ctx
);
1997 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1998 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
2003 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
2005 if (blit
->ArrayObj
) {
2006 _mesa_DeleteVertexArrays(1, &blit
->ArrayObj
);
2008 _mesa_DeleteBuffers(1, &blit
->VBO
);
2011 if (blit
->DepthFP
) {
2012 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
2016 _mesa_DeleteObjectARB(blit
->ShaderProg
);
2017 blit
->ShaderProg
= 0;
2018 _mesa_DeleteObjectARB(blit
->RectShaderProg
);
2019 blit
->RectShaderProg
= 0;
2021 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
2022 blit
->depthTex
.TexObj
= 0;
2027 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2030 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2032 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2034 GLfloat x
, y
, z
, r
, g
, b
, a
;
2036 struct vertex verts
[4];
2037 /* save all state but scissor, pixel pack/unpack */
2038 GLbitfield metaSave
= (MESA_META_ALL
-
2040 MESA_META_PIXEL_STORE
-
2041 MESA_META_CONDITIONAL_RENDER
-
2042 MESA_META_FRAMEBUFFER_SRGB
);
2043 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2045 if (buffers
& BUFFER_BITS_COLOR
) {
2046 /* if clearing color buffers, don't save/restore colormask */
2047 metaSave
-= MESA_META_COLOR_MASK
;
2050 _mesa_meta_begin(ctx
, metaSave
);
2052 if (clear
->ArrayObj
== 0) {
2053 /* one-time setup */
2055 /* create vertex array object */
2056 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2057 _mesa_BindVertexArray(clear
->ArrayObj
);
2059 /* create vertex array buffer */
2060 _mesa_GenBuffers(1, &clear
->VBO
);
2061 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2063 /* setup vertex arrays */
2064 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2065 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2066 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2067 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2070 _mesa_BindVertexArray(clear
->ArrayObj
);
2071 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2074 /* GL_COLOR_BUFFER_BIT */
2075 if (buffers
& BUFFER_BITS_COLOR
) {
2076 /* leave colormask, glDrawBuffer state as-is */
2078 /* Clears never have the color clamped. */
2079 if (ctx
->Extensions
.ARB_color_buffer_float
)
2080 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2083 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2084 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2087 /* GL_DEPTH_BUFFER_BIT */
2088 if (buffers
& BUFFER_BIT_DEPTH
) {
2089 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2090 _mesa_DepthFunc(GL_ALWAYS
);
2091 _mesa_DepthMask(GL_TRUE
);
2094 assert(!ctx
->Depth
.Test
);
2097 /* GL_STENCIL_BUFFER_BIT */
2098 if (buffers
& BUFFER_BIT_STENCIL
) {
2099 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2100 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2101 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2102 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2103 ctx
->Stencil
.Clear
& stencilMax
,
2104 ctx
->Stencil
.WriteMask
[0]);
2107 assert(!ctx
->Stencil
.Enabled
);
2110 /* vertex positions/colors */
2112 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
2113 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
2114 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
2115 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
2116 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
2133 for (i
= 0; i
< 4; i
++) {
2134 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
2135 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
2136 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
2137 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
2140 /* upload new vertex data */
2141 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2142 GL_DYNAMIC_DRAW_ARB
);
2146 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2148 _mesa_meta_end(ctx
);
2152 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
2154 const char *vs_source
=
2155 "attribute vec4 position;\n"
2158 " gl_Position = position;\n"
2160 const char *gs_source
=
2162 "layout(triangles) in;\n"
2163 "layout(triangle_strip, max_vertices = 4) out;\n"
2164 "uniform int layer;\n"
2167 " for (int i = 0; i < 3; i++) {\n"
2168 " gl_Layer = layer;\n"
2169 " gl_Position = gl_in[i].gl_Position;\n"
2173 const char *fs_source
=
2175 "precision highp float;\n"
2177 "uniform vec4 color;\n"
2180 " gl_FragColor = color;\n"
2182 GLuint vs
, gs
= 0, fs
;
2183 bool has_integer_textures
;
2185 if (clear
->ArrayObj
!= 0)
2188 /* create vertex array object */
2189 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2190 _mesa_BindVertexArray(clear
->ArrayObj
);
2192 /* create vertex array buffer */
2193 _mesa_GenBuffers(1, &clear
->VBO
);
2194 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2196 /* setup vertex arrays */
2197 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
2198 _mesa_EnableVertexAttribArray(0);
2200 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
2201 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
2202 _mesa_CompileShader(vs
);
2204 if (_mesa_has_geometry_shaders(ctx
)) {
2205 gs
= _mesa_CreateShaderObjectARB(GL_GEOMETRY_SHADER
);
2206 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
2207 _mesa_CompileShader(gs
);
2210 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
2211 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
2212 _mesa_CompileShader(fs
);
2214 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
2215 _mesa_AttachShader(clear
->ShaderProg
, fs
);
2216 _mesa_DeleteObjectARB(fs
);
2218 _mesa_AttachShader(clear
->ShaderProg
, gs
);
2219 _mesa_AttachShader(clear
->ShaderProg
, vs
);
2220 _mesa_DeleteObjectARB(vs
);
2221 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
2222 _mesa_LinkProgram(clear
->ShaderProg
);
2224 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2227 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2231 has_integer_textures
= _mesa_is_gles3(ctx
) ||
2232 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
2234 if (has_integer_textures
) {
2235 void *shader_source_mem_ctx
= ralloc_context(NULL
);
2236 const char *vs_int_source
=
2237 ralloc_asprintf(shader_source_mem_ctx
,
2239 "in vec4 position;\n"
2242 " gl_Position = position;\n"
2244 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2245 const char *fs_int_source
=
2246 ralloc_asprintf(shader_source_mem_ctx
,
2249 "precision highp float;\n"
2251 "uniform ivec4 color;\n"
2252 "out ivec4 out_color;\n"
2256 " out_color = color;\n"
2258 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2260 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
2261 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
2262 ralloc_free(shader_source_mem_ctx
);
2264 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
2265 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
2266 _mesa_DeleteObjectARB(fs
);
2268 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
2269 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
2270 _mesa_DeleteObjectARB(vs
);
2271 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
2273 /* Note that user-defined out attributes get automatically assigned
2274 * locations starting from 0, so we don't need to explicitly
2275 * BindFragDataLocation to 0.
2278 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
2280 clear
->IntegerColorLocation
=
2281 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2283 clear
->IntegerLayerLocation
=
2284 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
2288 _mesa_DeleteObjectARB(gs
);
2292 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
2294 if (clear
->ArrayObj
== 0)
2296 _mesa_DeleteVertexArrays(1, &clear
->ArrayObj
);
2297 clear
->ArrayObj
= 0;
2298 _mesa_DeleteBuffers(1, &clear
->VBO
);
2300 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2301 clear
->ShaderProg
= 0;
2303 if (clear
->IntegerShaderProg
) {
2304 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2305 clear
->IntegerShaderProg
= 0;
2310 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2313 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2315 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2316 GLbitfield metaSave
;
2317 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2318 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2319 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2320 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2321 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2322 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2323 const float z
= -invert_z(ctx
->Depth
.Clear
);
2328 metaSave
= (MESA_META_ALPHA_TEST
|
2330 MESA_META_DEPTH_TEST
|
2331 MESA_META_RASTERIZATION
|
2333 MESA_META_STENCIL_TEST
|
2335 MESA_META_VIEWPORT
|
2337 MESA_META_CLAMP_FRAGMENT_COLOR
|
2338 MESA_META_MULTISAMPLE
|
2339 MESA_META_OCCLUSION_QUERY
);
2341 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2342 /* We'll use colormask to disable color writes. Otherwise,
2343 * respect color mask
2345 metaSave
|= MESA_META_COLOR_MASK
;
2348 _mesa_meta_begin(ctx
, metaSave
);
2350 meta_glsl_clear_init(ctx
, clear
);
2352 if (fb
->_IntegerColor
) {
2353 _mesa_UseProgram(clear
->IntegerShaderProg
);
2354 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2355 ctx
->Color
.ClearColor
.i
);
2357 _mesa_UseProgram(clear
->ShaderProg
);
2358 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2359 ctx
->Color
.ClearColor
.f
);
2362 _mesa_BindVertexArray(clear
->ArrayObj
);
2363 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2365 /* GL_COLOR_BUFFER_BIT */
2366 if (buffers
& BUFFER_BITS_COLOR
) {
2367 /* leave colormask, glDrawBuffer state as-is */
2369 /* Clears never have the color clamped. */
2370 if (ctx
->Extensions
.ARB_color_buffer_float
)
2371 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2374 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2375 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2378 /* GL_DEPTH_BUFFER_BIT */
2379 if (buffers
& BUFFER_BIT_DEPTH
) {
2380 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2381 _mesa_DepthFunc(GL_ALWAYS
);
2382 _mesa_DepthMask(GL_TRUE
);
2385 assert(!ctx
->Depth
.Test
);
2388 /* GL_STENCIL_BUFFER_BIT */
2389 if (buffers
& BUFFER_BIT_STENCIL
) {
2390 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2391 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2392 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2393 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2394 ctx
->Stencil
.Clear
& stencilMax
,
2395 ctx
->Stencil
.WriteMask
[0]);
2398 assert(!ctx
->Stencil
.Enabled
);
2401 /* vertex positions */
2415 /* upload new vertex data */
2416 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2417 GL_DYNAMIC_DRAW_ARB
);
2420 if (fb
->MaxNumLayers
> 0) {
2422 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
2423 if (fb
->_IntegerColor
)
2424 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
2426 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
2427 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2430 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2433 _mesa_meta_end(ctx
);
2437 * Meta implementation of ctx->Driver.CopyPixels() in terms
2438 * of texture mapping and polygon rendering and GLSL shaders.
2441 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2442 GLsizei width
, GLsizei height
,
2443 GLint dstX
, GLint dstY
, GLenum type
)
2445 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2446 struct temp_texture
*tex
= get_temp_texture(ctx
);
2448 GLfloat x
, y
, z
, s
, t
;
2450 struct vertex verts
[4];
2452 GLenum intFormat
= GL_RGBA
;
2454 if (type
!= GL_COLOR
||
2455 ctx
->_ImageTransferState
||
2457 width
> tex
->MaxSize
||
2458 height
> tex
->MaxSize
) {
2459 /* XXX avoid this fallback */
2460 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2464 /* Most GL state applies to glCopyPixels, but a there's a few things
2465 * we need to override:
2467 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2470 MESA_META_TRANSFORM
|
2473 MESA_META_VIEWPORT
));
2475 if (copypix
->ArrayObj
== 0) {
2476 /* one-time setup */
2478 /* create vertex array object */
2479 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2480 _mesa_BindVertexArray(copypix
->ArrayObj
);
2482 /* create vertex array buffer */
2483 _mesa_GenBuffers(1, ©pix
->VBO
);
2484 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2485 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2486 NULL
, GL_DYNAMIC_DRAW_ARB
);
2488 /* setup vertex arrays */
2489 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2490 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2491 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2492 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2495 _mesa_BindVertexArray(copypix
->ArrayObj
);
2496 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2499 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2501 /* vertex positions, texcoords (after texture allocation!) */
2503 const GLfloat dstX0
= (GLfloat
) dstX
;
2504 const GLfloat dstY0
= (GLfloat
) dstY
;
2505 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2506 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2507 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2517 verts
[1].s
= tex
->Sright
;
2522 verts
[2].s
= tex
->Sright
;
2523 verts
[2].t
= tex
->Ttop
;
2528 verts
[3].t
= tex
->Ttop
;
2530 /* upload new vertex data */
2531 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2534 /* Alloc/setup texture */
2535 setup_copypix_texture(ctx
, tex
, newTex
, srcX
, srcY
, width
, height
,
2536 GL_RGBA
, GL_NEAREST
);
2538 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2540 /* draw textured quad */
2541 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2543 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2545 _mesa_meta_end(ctx
);
2551 * When the glDrawPixels() image size is greater than the max rectangle
2552 * texture size we use this function to break the glDrawPixels() image
2553 * into tiles which fit into the max texture size.
2556 tiled_draw_pixels(struct gl_context
*ctx
,
2558 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2559 GLenum format
, GLenum type
,
2560 const struct gl_pixelstore_attrib
*unpack
,
2561 const GLvoid
*pixels
)
2563 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2566 if (tileUnpack
.RowLength
== 0)
2567 tileUnpack
.RowLength
= width
;
2569 for (i
= 0; i
< width
; i
+= tileSize
) {
2570 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2571 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2573 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2575 for (j
= 0; j
< height
; j
+= tileSize
) {
2576 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2577 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2579 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2581 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2582 format
, type
, &tileUnpack
, pixels
);
2589 * One-time init for drawing stencil pixels.
2592 init_draw_stencil_pixels(struct gl_context
*ctx
)
2594 /* This program is run eight times, once for each stencil bit.
2595 * The stencil values to draw are found in an 8-bit alpha texture.
2596 * We read the texture/stencil value and test if bit 'b' is set.
2597 * If the bit is not set, use KIL to kill the fragment.
2598 * Finally, we use the stencil test to update the stencil buffer.
2600 * The basic algorithm for checking if a bit is set is:
2601 * if (is_odd(value / (1 << bit)))
2602 * result is one (or non-zero).
2605 * The program parameter contains three values:
2606 * parm.x = 255 / (1 << bit)
2610 static const char *program
=
2612 "PARAM parm = program.local[0]; \n"
2614 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2615 "# t = t * 255 / bit \n"
2616 "MUL t.x, t.a, parm.x; \n"
2619 "SUB t.x, t.x, t.y; \n"
2621 "MUL t.x, t.x, parm.y; \n"
2622 "# t = fract(t.x) \n"
2623 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2624 "# t.x = (t.x == 0 ? 1 : 0) \n"
2625 "SGE t.x, -t.x, parm.z; \n"
2627 "# for debug only \n"
2628 "#MOV result.color, t.x; \n"
2630 char program2
[1000];
2631 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2632 struct temp_texture
*tex
= get_temp_texture(ctx
);
2633 const char *texTarget
;
2635 assert(drawpix
->StencilFP
== 0);
2637 /* replace %s with "RECT" or "2D" */
2638 assert(strlen(program
) + 4 < sizeof(program2
));
2639 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2643 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2645 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2646 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2647 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2648 strlen(program2
), (const GLubyte
*) program2
);
2653 * One-time init for drawing depth pixels.
2656 init_draw_depth_pixels(struct gl_context
*ctx
)
2658 static const char *program
=
2660 "PARAM color = program.local[0]; \n"
2661 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2662 "MOV result.color, color; \n"
2665 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2666 struct temp_texture
*tex
= get_temp_texture(ctx
);
2667 const char *texTarget
;
2669 assert(drawpix
->DepthFP
== 0);
2671 /* replace %s with "RECT" or "2D" */
2672 assert(strlen(program
) + 4 < sizeof(program2
));
2673 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2677 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2679 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2680 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2681 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2682 strlen(program2
), (const GLubyte
*) program2
);
2687 * Meta implementation of ctx->Driver.DrawPixels() in terms
2688 * of texture mapping and polygon rendering.
2691 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2692 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2693 GLenum format
, GLenum type
,
2694 const struct gl_pixelstore_attrib
*unpack
,
2695 const GLvoid
*pixels
)
2697 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2698 struct temp_texture
*tex
= get_temp_texture(ctx
);
2699 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2700 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2702 GLfloat x
, y
, z
, s
, t
;
2704 struct vertex verts
[4];
2705 GLenum texIntFormat
;
2706 GLboolean fallback
, newTex
;
2707 GLbitfield metaExtraSave
= 0x0;
2711 * Determine if we can do the glDrawPixels with texture mapping.
2713 fallback
= GL_FALSE
;
2714 if (ctx
->Fog
.Enabled
) {
2718 if (_mesa_is_color_format(format
)) {
2719 /* use more compact format when possible */
2720 /* XXX disable special case for GL_LUMINANCE for now to work around
2721 * apparent i965 driver bug (see bug #23670).
2723 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2724 texIntFormat
= format
;
2726 texIntFormat
= GL_RGBA
;
2728 /* If we're not supposed to clamp the resulting color, then just
2729 * promote our texture to fully float. We could do better by
2730 * just going for the matching set of channels, in floating
2733 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2734 ctx
->Extensions
.ARB_texture_float
)
2735 texIntFormat
= GL_RGBA32F
;
2737 else if (_mesa_is_stencil_format(format
)) {
2738 if (ctx
->Extensions
.ARB_fragment_program
&&
2739 ctx
->Pixel
.IndexShift
== 0 &&
2740 ctx
->Pixel
.IndexOffset
== 0 &&
2741 type
== GL_UNSIGNED_BYTE
) {
2742 /* We'll store stencil as alpha. This only works for GLubyte
2743 * image data because of how incoming values are mapped to alpha
2746 texIntFormat
= GL_ALPHA
;
2747 metaExtraSave
= (MESA_META_COLOR_MASK
|
2748 MESA_META_DEPTH_TEST
|
2749 MESA_META_PIXEL_TRANSFER
|
2751 MESA_META_STENCIL_TEST
);
2757 else if (_mesa_is_depth_format(format
)) {
2758 if (ctx
->Extensions
.ARB_depth_texture
&&
2759 ctx
->Extensions
.ARB_fragment_program
) {
2760 texIntFormat
= GL_DEPTH_COMPONENT
;
2761 metaExtraSave
= (MESA_META_SHADER
);
2772 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2773 format
, type
, unpack
, pixels
);
2778 * Check image size against max texture size, draw as tiles if needed.
2780 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2781 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2782 format
, type
, unpack
, pixels
);
2786 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2787 * but a there's a few things we need to override:
2789 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2792 MESA_META_TRANSFORM
|
2795 MESA_META_VIEWPORT
|
2798 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2800 /* vertex positions, texcoords (after texture allocation!) */
2802 const GLfloat x0
= (GLfloat
) x
;
2803 const GLfloat y0
= (GLfloat
) y
;
2804 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2805 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2806 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2816 verts
[1].s
= tex
->Sright
;
2821 verts
[2].s
= tex
->Sright
;
2822 verts
[2].t
= tex
->Ttop
;
2827 verts
[3].t
= tex
->Ttop
;
2830 if (drawpix
->ArrayObj
== 0) {
2831 /* one-time setup: create vertex array object */
2832 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2834 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2836 /* create vertex array buffer */
2837 _mesa_GenBuffers(1, &vbo
);
2838 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2839 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2840 verts
, GL_DYNAMIC_DRAW_ARB
);
2842 /* setup vertex arrays */
2843 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2844 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2845 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2846 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2848 /* set given unpack params */
2849 ctx
->Unpack
= *unpack
;
2851 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2853 if (_mesa_is_stencil_format(format
)) {
2854 /* Drawing stencil */
2857 if (!drawpix
->StencilFP
)
2858 init_draw_stencil_pixels(ctx
);
2860 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2861 GL_ALPHA
, type
, pixels
);
2863 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2865 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2867 /* set all stencil bits to 0 */
2868 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2869 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2870 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2872 /* set stencil bits to 1 where needed */
2873 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2875 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2876 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2878 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2879 const GLuint mask
= 1 << bit
;
2880 if (mask
& origStencilMask
) {
2881 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2882 _mesa_StencilMask(mask
);
2884 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2885 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2887 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2891 else if (_mesa_is_depth_format(format
)) {
2893 if (!drawpix
->DepthFP
)
2894 init_draw_depth_pixels(ctx
);
2896 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2897 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2899 /* polygon color = current raster color */
2900 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2901 ctx
->Current
.RasterColor
);
2903 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2904 format
, type
, pixels
);
2906 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2910 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2911 format
, type
, pixels
);
2912 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2915 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2917 _mesa_DeleteBuffers(1, &vbo
);
2919 /* restore unpack params */
2920 ctx
->Unpack
= unpackSave
;
2922 _mesa_meta_end(ctx
);
2926 alpha_test_raster_color(struct gl_context
*ctx
)
2928 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2929 GLfloat ref
= ctx
->Color
.AlphaRef
;
2931 switch (ctx
->Color
.AlphaFunc
) {
2937 return alpha
== ref
;
2939 return alpha
<= ref
;
2943 return alpha
!= ref
;
2945 return alpha
>= ref
;
2955 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2956 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2957 * tracker would improve performance a lot.
2960 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2961 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2962 const struct gl_pixelstore_attrib
*unpack
,
2963 const GLubyte
*bitmap1
)
2965 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2966 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2967 const GLenum texIntFormat
= GL_ALPHA
;
2968 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2971 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2973 struct vertex verts
[4];
2978 * Check if swrast fallback is needed.
2980 if (ctx
->_ImageTransferState
||
2981 ctx
->FragmentProgram
._Enabled
||
2983 ctx
->Texture
._EnabledUnits
||
2984 width
> tex
->MaxSize
||
2985 height
> tex
->MaxSize
) {
2986 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2990 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2993 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2994 * but a there's a few things we need to override:
2996 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2997 MESA_META_PIXEL_STORE
|
2998 MESA_META_RASTERIZATION
|
3001 MESA_META_TRANSFORM
|
3004 MESA_META_VIEWPORT
));
3006 if (bitmap
->ArrayObj
== 0) {
3007 /* one-time setup */
3009 /* create vertex array object */
3010 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
3011 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
3013 /* create vertex array buffer */
3014 _mesa_GenBuffers(1, &bitmap
->VBO
);
3015 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3016 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3017 NULL
, GL_DYNAMIC_DRAW_ARB
);
3019 /* setup vertex arrays */
3020 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3021 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
3022 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
3023 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3024 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3025 _mesa_EnableClientState(GL_COLOR_ARRAY
);
3028 _mesa_BindVertexArray(bitmap
->ArrayObj
);
3029 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3032 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
3034 /* vertex positions, texcoords, colors (after texture allocation!) */
3036 const GLfloat x0
= (GLfloat
) x
;
3037 const GLfloat y0
= (GLfloat
) y
;
3038 const GLfloat x1
= (GLfloat
) (x
+ width
);
3039 const GLfloat y1
= (GLfloat
) (y
+ height
);
3040 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
3051 verts
[1].s
= tex
->Sright
;
3056 verts
[2].s
= tex
->Sright
;
3057 verts
[2].t
= tex
->Ttop
;
3062 verts
[3].t
= tex
->Ttop
;
3064 for (i
= 0; i
< 4; i
++) {
3065 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
3066 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
3067 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
3068 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
3071 /* upload new vertex data */
3072 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3075 /* choose different foreground/background alpha values */
3076 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
3077 bg
= (fg
> 127 ? 0 : 255);
3079 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
3081 _mesa_meta_end(ctx
);
3085 bitmap8
= malloc(width
* height
);
3087 memset(bitmap8
, bg
, width
* height
);
3088 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
3089 bitmap8
, width
, fg
);
3091 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
3093 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
3094 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
3096 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
3097 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
3099 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3101 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
3106 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
3108 _mesa_meta_end(ctx
);
3113 * Check if the call to _mesa_meta_GenerateMipmap() will require a
3114 * software fallback. The fallback path will require that the texture
3115 * images are mapped.
3116 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
3119 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
3120 struct gl_texture_object
*texObj
)
3122 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3123 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3124 struct gl_texture_image
*baseImage
;
3128 /* check for fallbacks */
3129 if (target
== GL_TEXTURE_3D
||
3130 target
== GL_TEXTURE_1D_ARRAY
||
3131 target
== GL_TEXTURE_2D_ARRAY
) {
3132 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3133 "glGenerateMipmap() to %s target\n",
3134 _mesa_lookup_enum_by_nr(target
));
3138 srcLevel
= texObj
->BaseLevel
;
3139 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
3141 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3142 "glGenerateMipmap() couldn't find base teximage\n");
3146 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
3147 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3148 "glGenerateMipmap() with %s format\n",
3149 _mesa_get_format_name(baseImage
->TexFormat
));
3153 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
3154 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3155 /* The texture format is sRGB but we can't turn off sRGB->linear
3156 * texture sample conversion. So we won't be able to generate the
3157 * right colors when rendering. Need to use a fallback.
3159 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3160 "glGenerateMipmap() of sRGB texture without "
3166 * Test that we can actually render in the texture's format.
3169 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3170 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3172 if (target
== GL_TEXTURE_1D
) {
3173 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3174 GL_COLOR_ATTACHMENT0_EXT
,
3175 target
, texObj
->Name
, srcLevel
);
3178 /* other work is needed to enable 3D mipmap generation */
3179 else if (target
== GL_TEXTURE_3D
) {
3181 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3182 GL_COLOR_ATTACHMENT0_EXT
,
3183 target
, texObj
->Name
, srcLevel
, zoffset
);
3188 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3189 GL_COLOR_ATTACHMENT0_EXT
,
3190 target
, texObj
->Name
, srcLevel
);
3193 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3195 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3197 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3198 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3199 "glGenerateMipmap() got incomplete FBO\n");
3208 * Compute the texture coordinates for the four vertices of a quad for
3209 * drawing a 2D texture image or slice of a cube/3D texture.
3210 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
3211 * \param slice slice of a 1D/2D array texture or 3D texture
3212 * \param width width of the texture image
3213 * \param height height of the texture image
3214 * \param coords0/1/2/3 returns the computed texcoords
3217 setup_texture_coords(GLenum faceTarget
,
3227 static const GLfloat st
[4][2] = {
3228 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
3233 switch (faceTarget
) {
3237 case GL_TEXTURE_2D_ARRAY
:
3238 if (faceTarget
== GL_TEXTURE_3D
) {
3239 assert(slice
< depth
);
3241 r
= (slice
+ 0.5f
) / depth
;
3243 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
3247 coords0
[0] = 0.0F
; /* s */
3248 coords0
[1] = 0.0F
; /* t */
3249 coords0
[2] = r
; /* r */
3260 case GL_TEXTURE_RECTANGLE_ARB
:
3261 coords0
[0] = 0.0F
; /* s */
3262 coords0
[1] = 0.0F
; /* t */
3263 coords0
[2] = 0.0F
; /* r */
3264 coords1
[0] = (float) width
;
3267 coords2
[0] = (float) width
;
3268 coords2
[1] = (float) height
;
3271 coords3
[1] = (float) height
;
3274 case GL_TEXTURE_1D_ARRAY
:
3275 coords0
[0] = 0.0F
; /* s */
3276 coords0
[1] = (float) slice
; /* t */
3277 coords0
[2] = 0.0F
; /* r */
3279 coords1
[1] = (float) slice
;
3282 coords2
[1] = (float) slice
;
3285 coords3
[1] = (float) slice
;
3289 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3290 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3291 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3292 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3293 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3294 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3295 /* loop over quad verts */
3296 for (i
= 0; i
< 4; i
++) {
3297 /* Compute sc = +/-scale and tc = +/-scale.
3298 * Not +/-1 to avoid cube face selection ambiguity near the edges,
3299 * though that can still sometimes happen with this scale factor...
3301 const GLfloat scale
= 0.9999f
;
3302 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
3303 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
3323 switch (faceTarget
) {
3324 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3329 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3334 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3339 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3344 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3349 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3360 assert(0 && "unexpected target in meta setup_texture_coords()");
3366 setup_ff_generate_mipmap(struct gl_context
*ctx
,
3367 struct gen_mipmap_state
*mipmap
)
3370 GLfloat x
, y
, tex
[3];
3373 if (mipmap
->ArrayObj
== 0) {
3374 /* one-time setup */
3375 /* create vertex array object */
3376 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3377 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
3379 /* create vertex array buffer */
3380 _mesa_GenBuffers(1, &mipmap
->VBO
);
3381 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3382 /* setup vertex arrays */
3383 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3384 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3385 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3386 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3389 /* setup projection matrix */
3390 _mesa_MatrixMode(GL_PROJECTION
);
3391 _mesa_LoadIdentity();
3392 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3396 static struct glsl_sampler
*
3397 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3401 mipmap
->sampler_1d
.type
= "sampler1D";
3402 mipmap
->sampler_1d
.func
= "texture1D";
3403 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3404 return &mipmap
->sampler_1d
;
3406 mipmap
->sampler_2d
.type
= "sampler2D";
3407 mipmap
->sampler_2d
.func
= "texture2D";
3408 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3409 return &mipmap
->sampler_2d
;
3411 /* Code for mipmap generation with 3D textures is not used yet.
3412 * It's a sw fallback.
3414 mipmap
->sampler_3d
.type
= "sampler3D";
3415 mipmap
->sampler_3d
.func
= "texture3D";
3416 mipmap
->sampler_3d
.texcoords
= "texCoords";
3417 return &mipmap
->sampler_3d
;
3418 case GL_TEXTURE_CUBE_MAP
:
3419 mipmap
->sampler_cubemap
.type
= "samplerCube";
3420 mipmap
->sampler_cubemap
.func
= "textureCube";
3421 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3422 return &mipmap
->sampler_cubemap
;
3423 case GL_TEXTURE_1D_ARRAY
:
3424 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3425 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3426 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3427 return &mipmap
->sampler_1d_array
;
3428 case GL_TEXTURE_2D_ARRAY
:
3429 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3430 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3431 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3432 return &mipmap
->sampler_2d_array
;
3434 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3435 " setup_texture_sampler()\n", target
);
3442 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3443 struct gen_mipmap_state
*mipmap
,
3447 GLfloat x
, y
, tex
[3];
3449 struct glsl_sampler
*sampler
;
3450 const char *vs_source
;
3455 /* Check if already initialized */
3456 if (mipmap
->ArrayObj
== 0) {
3458 /* create vertex array object */
3459 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3460 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3462 /* create vertex array buffer */
3463 _mesa_GenBuffers(1, &mipmap
->VBO
);
3464 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3466 /* setup vertex arrays */
3467 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3468 sizeof(struct vertex
), OFFSET(x
));
3469 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3470 sizeof(struct vertex
), OFFSET(tex
));
3471 _mesa_EnableVertexAttribArray(0);
3472 _mesa_EnableVertexAttribArray(1);
3475 /* Generate a fragment shader program appropriate for the texture target */
3476 sampler
= setup_texture_sampler(target
, mipmap
);
3477 assert(sampler
!= NULL
);
3478 if (sampler
->shader_prog
!= 0) {
3479 mipmap
->ShaderProg
= sampler
->shader_prog
;
3483 mem_ctx
= ralloc_context(NULL
);
3485 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3487 "attribute vec2 position;\n"
3488 "attribute vec3 textureCoords;\n"
3489 "varying vec3 texCoords;\n"
3492 " texCoords = textureCoords;\n"
3493 " gl_Position = vec4(position, 0.0, 1.0);\n"
3496 fs_source
= ralloc_asprintf(mem_ctx
,
3497 "#extension GL_EXT_texture_array : enable\n"
3499 "precision highp float;\n"
3501 "uniform %s texSampler;\n"
3502 "varying vec3 texCoords;\n"
3505 " gl_FragColor = %s(texSampler, %s);\n"
3508 sampler
->func
, sampler
->texcoords
);
3511 vs_source
= ralloc_asprintf(mem_ctx
,
3513 "in vec2 position;\n"
3514 "in vec3 textureCoords;\n"
3515 "out vec3 texCoords;\n"
3518 " texCoords = textureCoords;\n"
3519 " gl_Position = vec4(position, 0.0, 1.0);\n"
3521 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
3522 fs_source
= ralloc_asprintf(mem_ctx
,
3525 "precision highp float;\n"
3527 "uniform %s texSampler;\n"
3528 "in vec3 texCoords;\n"
3529 "out vec4 out_color;\n"
3533 " out_color = texture(texSampler, %s);\n"
3535 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
3537 sampler
->texcoords
);
3540 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3541 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3543 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3544 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3545 _mesa_DeleteObjectARB(fs
);
3546 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3547 _mesa_DeleteObjectARB(vs
);
3548 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3549 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3550 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3551 sampler
->shader_prog
= mipmap
->ShaderProg
;
3552 ralloc_free(mem_ctx
);
3557 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3558 struct gen_mipmap_state
*mipmap
)
3560 if (mipmap
->ArrayObj
== 0)
3562 _mesa_DeleteVertexArrays(1, &mipmap
->ArrayObj
);
3563 mipmap
->ArrayObj
= 0;
3564 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3567 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3568 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3569 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3570 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3571 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3572 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3574 mipmap
->sampler_1d
.shader_prog
= 0;
3575 mipmap
->sampler_2d
.shader_prog
= 0;
3576 mipmap
->sampler_3d
.shader_prog
= 0;
3577 mipmap
->sampler_cubemap
.shader_prog
= 0;
3578 mipmap
->sampler_1d_array
.shader_prog
= 0;
3579 mipmap
->sampler_2d_array
.shader_prog
= 0;
3584 * Called via ctx->Driver.GenerateMipmap()
3585 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3589 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3590 struct gl_texture_object
*texObj
)
3592 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3594 GLfloat x
, y
, tex
[3];
3596 struct vertex verts
[4];
3597 const GLuint baseLevel
= texObj
->BaseLevel
;
3598 const GLuint maxLevel
= texObj
->MaxLevel
;
3599 const GLint maxLevelSave
= texObj
->MaxLevel
;
3600 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3601 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3602 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3603 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3604 ctx
->Extensions
.ARB_fragment_shader
&&
3605 (ctx
->API
!= API_OPENGLES
);
3608 const GLint slice
= 0;
3611 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3612 _mesa_generate_mipmap(ctx
, target
, texObj
);
3616 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3617 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3618 faceTarget
= target
;
3619 target
= GL_TEXTURE_CUBE_MAP
;
3622 faceTarget
= target
;
3625 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3627 /* Choose between glsl version and fixed function version of
3628 * GenerateMipmap function.
3630 if (use_glsl_version
) {
3631 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3632 _mesa_UseProgram(mipmap
->ShaderProg
);
3635 setup_ff_generate_mipmap(ctx
, mipmap
);
3636 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3639 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3640 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3642 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3643 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3645 if (currentTexUnitSave
!= 0)
3646 _mesa_BindTexture(target
, texObj
->Name
);
3649 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3652 if (!mipmap
->Sampler
) {
3653 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3654 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3656 _mesa_SamplerParameteri(mipmap
->Sampler
,
3657 GL_TEXTURE_MIN_FILTER
,
3658 GL_LINEAR_MIPMAP_LINEAR
);
3659 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3660 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3661 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3662 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3664 /* We don't want to encode or decode sRGB values; treat them as linear.
3665 * This is not technically correct for GLES3 but we don't get any API
3666 * error at the moment.
3668 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3669 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3670 GL_SKIP_DECODE_EXT
);
3674 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3677 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3679 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3680 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3682 assert(!genMipmapSave
);
3684 /* Setup texture coordinates */
3685 setup_texture_coords(faceTarget
,
3687 0, 0, 1, /* width, height never used here */
3693 /* setup vertex positions */
3703 /* upload vertex data */
3704 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3705 verts
, GL_DYNAMIC_DRAW_ARB
);
3707 /* texture is already locked, unlock now */
3708 _mesa_unlock_texture(ctx
, texObj
);
3710 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3711 const struct gl_texture_image
*srcImage
;
3712 const GLuint srcLevel
= dstLevel
- 1;
3713 GLsizei srcWidth
, srcHeight
, srcDepth
;
3714 GLsizei dstWidth
, dstHeight
, dstDepth
;
3717 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3718 assert(srcImage
->Border
== 0);
3721 srcWidth
= srcImage
->Width
;
3722 srcHeight
= srcImage
->Height
;
3723 srcDepth
= srcImage
->Depth
;
3726 dstWidth
= MAX2(1, srcWidth
/ 2);
3727 dstHeight
= MAX2(1, srcHeight
/ 2);
3728 dstDepth
= MAX2(1, srcDepth
/ 2);
3730 if (dstWidth
== srcImage
->Width
&&
3731 dstHeight
== srcImage
->Height
&&
3732 dstDepth
== srcImage
->Depth
) {
3737 /* Allocate storage for the destination mipmap image(s) */
3739 /* Set MaxLevel large enough to hold the new level when we allocate it */
3740 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3742 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3743 dstWidth
, dstHeight
, dstDepth
,
3745 srcImage
->InternalFormat
,
3746 srcImage
->TexFormat
)) {
3747 /* All done. We either ran out of memory or we would go beyond the
3748 * last valid level of an immutable texture if we continued.
3753 /* limit minification to src level */
3754 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3756 /* Set to draw into the current dstLevel */
3757 if (target
== GL_TEXTURE_1D
) {
3758 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3759 GL_COLOR_ATTACHMENT0_EXT
,
3764 else if (target
== GL_TEXTURE_3D
) {
3765 GLint zoffset
= 0; /* XXX unfinished */
3766 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3767 GL_COLOR_ATTACHMENT0_EXT
,
3774 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3775 GL_COLOR_ATTACHMENT0_EXT
,
3781 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3784 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3785 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3786 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3787 "_mesa_meta_GenerateMipmap()");
3791 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3792 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3794 /* setup viewport */
3795 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
3797 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3800 _mesa_lock_texture(ctx
, texObj
); /* relock */
3802 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3804 _mesa_meta_end(ctx
);
3806 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3808 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3810 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3815 * Determine the GL data type to use for the temporary image read with
3816 * ReadPixels() and passed to Tex[Sub]Image().
3819 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
3823 baseFormat
= _mesa_get_format_base_format(format
);
3825 switch (baseFormat
) {
3832 case GL_LUMINANCE_ALPHA
:
3834 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3835 return GL_UNSIGNED_BYTE
;
3836 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3837 return GL_UNSIGNED_SHORT
;
3839 GLenum datatype
= _mesa_get_format_datatype(format
);
3840 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3844 case GL_DEPTH_COMPONENT
: {
3845 GLenum datatype
= _mesa_get_format_datatype(format
);
3846 if (datatype
== GL_FLOAT
)
3849 return GL_UNSIGNED_INT
;
3851 case GL_DEPTH_STENCIL
: {
3852 GLenum datatype
= _mesa_get_format_datatype(format
);
3853 if (datatype
== GL_FLOAT
)
3854 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3856 return GL_UNSIGNED_INT_24_8
;
3859 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3867 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3868 * Have to be careful with locking and meta state for pixel transfer.
3871 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3872 struct gl_texture_image
*texImage
,
3873 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3874 struct gl_renderbuffer
*rb
,
3876 GLsizei width
, GLsizei height
)
3878 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3879 GLenum format
, type
;
3883 /* Choose format/type for temporary image buffer */
3884 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3885 if (format
== GL_LUMINANCE
||
3886 format
== GL_LUMINANCE_ALPHA
||
3887 format
== GL_INTENSITY
) {
3888 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3889 * temp image buffer because glReadPixels will do L=R+G+B which is
3890 * not what we want (should be L=R).
3895 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3896 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3897 format
= _mesa_base_format_to_integer_format(format
);
3899 bpp
= _mesa_bytes_per_pixel(format
, type
);
3901 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3906 * Alloc image buffer (XXX could use a PBO)
3908 buf
= malloc(width
* height
* bpp
);
3910 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3914 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3917 * Read image from framebuffer (disable pixel transfer ops)
3919 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3920 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3921 format
, type
, &ctx
->Pack
, buf
);
3922 _mesa_meta_end(ctx
);
3924 _mesa_update_state(ctx
); /* to update pixel transfer state */
3927 * Store texture data (with pixel transfer ops)
3929 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3931 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3932 assert(yoffset
== 0);
3933 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3934 xoffset
, zoffset
, 0, width
, 1, 1,
3935 format
, type
, buf
, &ctx
->Unpack
);
3937 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3938 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3939 format
, type
, buf
, &ctx
->Unpack
);
3942 _mesa_meta_end(ctx
);
3944 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3951 meta_decompress_cleanup(struct decompress_state
*decompress
)
3953 if (decompress
->FBO
!= 0) {
3954 _mesa_DeleteFramebuffers(1, &decompress
->FBO
);
3955 _mesa_DeleteRenderbuffers(1, &decompress
->RBO
);
3958 if (decompress
->ArrayObj
!= 0) {
3959 _mesa_DeleteVertexArrays(1, &decompress
->ArrayObj
);
3960 _mesa_DeleteBuffers(1, &decompress
->VBO
);
3963 if (decompress
->Sampler
!= 0)
3964 _mesa_DeleteSamplers(1, &decompress
->Sampler
);
3966 memset(decompress
, 0, sizeof(*decompress
));
3970 * Decompress a texture image by drawing a quad with the compressed
3971 * texture and reading the pixels out of the color buffer.
3972 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3973 * \param destFormat format, ala glReadPixels
3974 * \param destType type, ala glReadPixels
3975 * \param dest destination buffer
3976 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3979 decompress_texture_image(struct gl_context
*ctx
,
3980 struct gl_texture_image
*texImage
,
3982 GLenum destFormat
, GLenum destType
,
3985 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3986 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3987 const GLint width
= texImage
->Width
;
3988 const GLint height
= texImage
->Height
;
3989 const GLint depth
= texImage
->Height
;
3990 const GLenum target
= texObj
->Target
;
3993 GLfloat x
, y
, tex
[3];
3995 struct vertex verts
[4];
3996 GLuint fboDrawSave
, fboReadSave
;
4001 assert(target
== GL_TEXTURE_3D
||
4002 target
== GL_TEXTURE_2D_ARRAY
);
4005 if (target
== GL_TEXTURE_CUBE_MAP
) {
4006 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
4009 faceTarget
= target
;
4012 /* save fbo bindings (not saved by _mesa_meta_begin()) */
4013 fboDrawSave
= ctx
->DrawBuffer
->Name
;
4014 fboReadSave
= ctx
->ReadBuffer
->Name
;
4015 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
4017 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
4019 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
4020 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
4022 /* Create/bind FBO/renderbuffer */
4023 if (decompress
->FBO
== 0) {
4024 _mesa_GenFramebuffers(1, &decompress
->FBO
);
4025 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
4026 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4027 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4028 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
4029 GL_COLOR_ATTACHMENT0_EXT
,
4030 GL_RENDERBUFFER_EXT
,
4034 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4037 /* alloc dest surface */
4038 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
4039 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4040 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
4042 decompress
->Width
= width
;
4043 decompress
->Height
= height
;
4046 /* setup VBO data */
4047 if (decompress
->ArrayObj
== 0) {
4048 /* create vertex array object */
4049 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
4050 _mesa_BindVertexArray(decompress
->ArrayObj
);
4052 /* create vertex array buffer */
4053 _mesa_GenBuffers(1, &decompress
->VBO
);
4054 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4055 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4056 NULL
, GL_DYNAMIC_DRAW_ARB
);
4058 /* setup vertex arrays */
4059 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4060 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
4061 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4062 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4065 _mesa_BindVertexArray(decompress
->ArrayObj
);
4066 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4069 if (!decompress
->Sampler
) {
4070 _mesa_GenSamplers(1, &decompress
->Sampler
);
4071 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4072 /* nearest filtering */
4073 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4074 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4075 /* No sRGB decode or encode.*/
4076 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
4077 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
4078 GL_SKIP_DECODE_EXT
);
4082 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4085 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
4091 /* setup vertex positions */
4097 verts
[2].y
= height
;
4099 verts
[3].y
= height
;
4101 _mesa_MatrixMode(GL_PROJECTION
);
4102 _mesa_LoadIdentity();
4103 _mesa_Ortho(0.0, width
, 0.0, height
, -1.0, 1.0);
4104 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
4106 /* upload new vertex data */
4107 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4109 /* setup texture state */
4110 _mesa_BindTexture(target
, texObj
->Name
);
4111 _mesa_set_enable(ctx
, target
, GL_TRUE
);
4114 /* save texture object state */
4115 const GLint baseLevelSave
= texObj
->BaseLevel
;
4116 const GLint maxLevelSave
= texObj
->MaxLevel
;
4118 /* restrict sampling to the texture level of interest */
4119 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4120 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
4121 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
4124 /* render quad w/ texture into renderbuffer */
4125 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4127 /* Restore texture object state, the texture binding will
4128 * be restored by _mesa_meta_end().
4130 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4131 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
4132 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
4137 /* read pixels from renderbuffer */
4139 GLenum baseTexFormat
= texImage
->_BaseFormat
;
4140 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
4142 /* The pixel transfer state will be set to default values at this point
4143 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
4144 * turned off (as required by glGetTexImage) but we need to handle some
4145 * special cases. In particular, single-channel texture values are
4146 * returned as red and two-channel texture values are returned as
4149 if ((baseTexFormat
== GL_LUMINANCE
||
4150 baseTexFormat
== GL_LUMINANCE_ALPHA
||
4151 baseTexFormat
== GL_INTENSITY
) ||
4152 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
4153 * luminance then we need to return L=tex(R).
4155 ((baseTexFormat
== GL_RGBA
||
4156 baseTexFormat
== GL_RGB
||
4157 baseTexFormat
== GL_RG
) &&
4158 (destBaseFormat
== GL_LUMINANCE
||
4159 destBaseFormat
== GL_LUMINANCE_ALPHA
||
4160 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
4161 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
4162 /* Green and blue must be zero */
4163 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
4164 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
4167 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
4170 /* disable texture unit */
4171 _mesa_set_enable(ctx
, target
, GL_FALSE
);
4173 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
4175 _mesa_meta_end(ctx
);
4177 /* restore fbo bindings */
4178 if (fboDrawSave
== fboReadSave
) {
4179 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
4182 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
4183 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
4185 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
4190 * This is just a wrapper around _mesa_get_tex_image() and
4191 * decompress_texture_image(). Meta functions should not be directly called
4195 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
4196 GLenum format
, GLenum type
, GLvoid
*pixels
,
4197 struct gl_texture_image
*texImage
)
4199 /* We can only use the decompress-with-blit method here if the texels are
4200 * unsigned, normalized values. We could handle signed and unnormalized
4201 * with floating point renderbuffers...
4203 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
4204 _mesa_get_format_datatype(texImage
->TexFormat
)
4205 == GL_UNSIGNED_NORMALIZED
) {
4206 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4208 /* Need to unlock the texture here to prevent deadlock... */
4209 _mesa_unlock_texture(ctx
, texObj
);
4210 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
4212 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
4213 /* Setup pixel packing. SkipPixels and SkipRows will be applied
4214 * in the decompress_texture_image() function's call to
4215 * glReadPixels but we need to compute the dest slice's address
4216 * here (according to SkipImages and ImageHeight).
4218 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
4219 packing
.SkipPixels
= 0;
4220 packing
.SkipRows
= 0;
4221 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
4222 texImage
->Height
, format
, type
,
4228 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
4230 /* ... and relock it */
4231 _mesa_lock_texture(ctx
, texObj
);
4234 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
4240 * Meta implementation of ctx->Driver.DrawTex() in terms
4241 * of polygon rendering.
4244 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
4245 GLfloat width
, GLfloat height
)
4247 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
4249 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
4251 struct vertex verts
[4];
4254 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
4256 MESA_META_TRANSFORM
|
4258 MESA_META_VIEWPORT
));
4260 if (drawtex
->ArrayObj
== 0) {
4261 /* one-time setup */
4262 GLint active_texture
;
4264 /* create vertex array object */
4265 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
4266 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4268 /* create vertex array buffer */
4269 _mesa_GenBuffers(1, &drawtex
->VBO
);
4270 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4271 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4272 NULL
, GL_DYNAMIC_DRAW_ARB
);
4274 /* client active texture is not part of the array object */
4275 active_texture
= ctx
->Array
.ActiveTexture
;
4277 /* setup vertex arrays */
4278 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4279 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4280 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4281 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
4282 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
4283 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4286 /* restore client active texture */
4287 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
4290 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4291 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4294 /* vertex positions, texcoords */
4296 const GLfloat x1
= x
+ width
;
4297 const GLfloat y1
= y
+ height
;
4299 z
= CLAMP(z
, 0.0f
, 1.0f
);
4318 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4319 const struct gl_texture_object
*texObj
;
4320 const struct gl_texture_image
*texImage
;
4321 GLfloat s
, t
, s1
, t1
;
4324 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
4326 for (j
= 0; j
< 4; j
++) {
4327 verts
[j
].st
[i
][0] = 0.0f
;
4328 verts
[j
].st
[i
][1] = 0.0f
;
4333 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
4334 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
4335 tw
= texImage
->Width2
;
4336 th
= texImage
->Height2
;
4338 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
4339 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
4340 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
4341 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
4343 verts
[0].st
[i
][0] = s
;
4344 verts
[0].st
[i
][1] = t
;
4346 verts
[1].st
[i
][0] = s1
;
4347 verts
[1].st
[i
][1] = t
;
4349 verts
[2].st
[i
][0] = s1
;
4350 verts
[2].st
[i
][1] = t1
;
4352 verts
[3].st
[i
][0] = s
;
4353 verts
[3].st
[i
][1] = t1
;
4356 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4359 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4361 _mesa_meta_end(ctx
);