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
*VertexShader
;
142 struct gl_shader_program
*GeometryShader
;
143 struct gl_shader_program
*FragmentShader
;
144 struct gl_shader_program
*ActiveShader
;
146 /** MESA_META_STENCIL_TEST */
147 struct gl_stencil_attrib Stencil
;
149 /** MESA_META_TRANSFORM */
151 GLfloat ModelviewMatrix
[16];
152 GLfloat ProjectionMatrix
[16];
153 GLfloat TextureMatrix
[16];
155 /** MESA_META_CLIP */
156 GLbitfield ClipPlanesEnabled
;
158 /** MESA_META_TEXTURE */
160 GLuint ClientActiveUnit
;
161 /** for unit[0] only */
162 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
163 /** mask of TEXTURE_2D_BIT, etc */
164 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
165 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
166 GLuint EnvMode
; /* unit[0] only */
168 /** MESA_META_VERTEX */
169 struct gl_array_object
*ArrayObj
;
170 struct gl_buffer_object
*ArrayBufferObj
;
172 /** MESA_META_VIEWPORT */
173 GLfloat ViewportX
, ViewportY
, ViewportW
, ViewportH
;
174 GLclampd DepthNear
, DepthFar
;
176 /** MESA_META_CLAMP_FRAGMENT_COLOR */
177 GLenum ClampFragmentColor
;
179 /** MESA_META_CLAMP_VERTEX_COLOR */
180 GLenum ClampVertexColor
;
182 /** MESA_META_CONDITIONAL_RENDER */
183 struct gl_query_object
*CondRenderQuery
;
184 GLenum CondRenderMode
;
186 /** MESA_META_SELECT_FEEDBACK */
188 struct gl_selection Select
;
189 struct gl_feedback Feedback
;
191 /** MESA_META_MULTISAMPLE */
192 GLboolean MultisampleEnabled
;
194 /** MESA_META_FRAMEBUFFER_SRGB */
195 GLboolean sRGBEnabled
;
197 /** Miscellaneous (always disabled) */
199 GLboolean RasterDiscard
;
200 GLboolean TransformFeedbackNeedsResume
;
204 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
205 * This is currently shared by all the meta ops. But we could create a
206 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
211 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
212 GLsizei MinSize
; /**< Min texture size to allocate */
213 GLsizei MaxSize
; /**< Max possible texture size */
214 GLboolean NPOT
; /**< Non-power of two size OK? */
215 GLsizei Width
, Height
; /**< Current texture size */
217 GLfloat Sright
, Ttop
; /**< right, top texcoords */
222 * State for glBlitFramebufer()
230 GLuint RectShaderProg
;
231 struct temp_texture depthTex
;
236 * State for glClear()
246 GLuint IntegerShaderProg
;
247 GLint IntegerColorLocation
;
248 GLint IntegerLayerLocation
;
253 * State for glCopyPixels()
263 * State for glDrawPixels()
269 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
270 GLuint DepthFP
; /**< Fragment program for drawing depth images */
275 * State for glBitmap()
281 struct temp_texture Tex
; /**< separate texture from other meta ops */
285 * State for GLSL texture sampler which is used to generate fragment
286 * shader in _mesa_meta_generate_mipmap().
288 struct glsl_sampler
{
291 const char *texcoords
;
296 * State for _mesa_meta_generate_mipmap()
298 struct gen_mipmap_state
305 struct glsl_sampler sampler_1d
;
306 struct glsl_sampler sampler_2d
;
307 struct glsl_sampler sampler_3d
;
308 struct glsl_sampler sampler_cubemap
;
309 struct glsl_sampler sampler_1d_array
;
310 struct glsl_sampler sampler_2d_array
;
314 * State for texture decompression
316 struct decompress_state
319 GLuint VBO
, FBO
, RBO
, Sampler
;
324 * State for glDrawTex()
332 #define MAX_META_OPS_DEPTH 8
334 * All per-context meta state.
338 /** Stack of state saved during meta-ops */
339 struct save_state Save
[MAX_META_OPS_DEPTH
];
340 /** Save stack depth */
341 GLuint SaveStackDepth
;
343 struct temp_texture TempTex
;
345 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
346 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
347 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
348 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
349 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
350 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
351 struct decompress_state Decompress
; /**< For texture decompression */
352 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
355 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
356 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
357 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
358 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
359 struct gen_mipmap_state
*mipmap
);
362 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
368 shader
= _mesa_CreateShaderObjectARB(target
);
369 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
370 _mesa_CompileShader(shader
);
372 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
376 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
378 _mesa_DeleteObjectARB(shader
);
384 _mesa_DeleteObjectARB(shader
);
388 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
390 "meta program compile failed:\n%s\n"
395 _mesa_DeleteObjectARB(shader
);
401 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
406 _mesa_LinkProgram(program
);
408 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
412 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
420 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
421 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
429 * Initialize meta-ops for a context.
430 * To be called once during context creation.
433 _mesa_meta_init(struct gl_context
*ctx
)
437 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
442 * Free context meta-op state.
443 * To be called once during context destruction.
446 _mesa_meta_free(struct gl_context
*ctx
)
448 GET_CURRENT_CONTEXT(old_context
);
449 _mesa_make_current(ctx
, NULL
, NULL
);
450 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
451 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
452 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
453 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
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
) {
601 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
602 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
603 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
604 ctx
->VertexProgram
.Current
);
605 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
608 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
609 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
610 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
611 ctx
->FragmentProgram
.Current
);
612 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
615 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
616 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
617 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
620 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
621 ctx
->Shader
.CurrentProgram
[MESA_SHADER_VERTEX
]);
622 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
623 ctx
->Shader
.CurrentProgram
[MESA_SHADER_GEOMETRY
]);
624 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
625 ctx
->Shader
.CurrentProgram
[MESA_SHADER_FRAGMENT
]);
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
;
833 /* After starting a new occlusion query, initialize the results to the
834 * values saved previously. The driver will then continue to increment
837 if (state
& MESA_META_OCCLUSION_QUERY
) {
838 if (save
->CurrentOcclusionObject
) {
839 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
840 save
->CurrentOcclusionObject
->Id
);
841 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
845 if (state
& MESA_META_ALPHA_TEST
) {
846 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
847 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
848 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
851 if (state
& MESA_META_BLEND
) {
852 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
853 if (ctx
->Extensions
.EXT_draw_buffers2
) {
855 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
856 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
860 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
863 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
864 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
867 if (state
& MESA_META_COLOR_MASK
) {
869 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
870 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
872 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
873 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
877 save
->ColorMask
[i
][0],
878 save
->ColorMask
[i
][1],
879 save
->ColorMask
[i
][2],
880 save
->ColorMask
[i
][3]);
886 if (state
& MESA_META_DEPTH_TEST
) {
887 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
888 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
889 _mesa_DepthFunc(save
->Depth
.Func
);
890 _mesa_DepthMask(save
->Depth
.Mask
);
893 if ((state
& MESA_META_FOG
)
894 && ctx
->API
!= API_OPENGL_CORE
895 && ctx
->API
!= API_OPENGLES2
) {
896 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
899 if (state
& MESA_META_PIXEL_STORE
) {
900 ctx
->Pack
= save
->Pack
;
901 ctx
->Unpack
= save
->Unpack
;
904 if (state
& MESA_META_PIXEL_TRANSFER
) {
905 ctx
->Pixel
.RedScale
= save
->RedScale
;
906 ctx
->Pixel
.RedBias
= save
->RedBias
;
907 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
908 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
909 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
910 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
911 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
912 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
913 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
915 ctx
->NewState
|=_NEW_PIXEL
;
918 if (state
& MESA_META_RASTERIZATION
) {
919 /* Core context requires that front and back mode be the same.
921 if (ctx
->API
== API_OPENGL_CORE
) {
922 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
924 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
925 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
927 if (ctx
->API
== API_OPENGL_COMPAT
) {
928 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
929 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
931 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
932 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
935 if (state
& MESA_META_SCISSOR
) {
938 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
939 _mesa_set_scissor(ctx
, i
,
940 save
->Scissor
.ScissorArray
[i
].X
,
941 save
->Scissor
.ScissorArray
[i
].Y
,
942 save
->Scissor
.ScissorArray
[i
].Width
,
943 save
->Scissor
.ScissorArray
[i
].Height
);
944 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
945 (save
->Scissor
.EnableFlags
>> i
) & 1);
949 if (state
& MESA_META_SHADER
) {
950 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
951 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
952 save
->VertexProgramEnabled
);
953 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
954 save
->VertexProgram
);
955 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
958 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
959 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
960 save
->FragmentProgramEnabled
);
961 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
962 save
->FragmentProgram
);
963 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
966 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
967 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
968 save
->ATIFragmentShaderEnabled
);
971 if (ctx
->Extensions
.ARB_vertex_shader
)
972 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
974 if (_mesa_has_geometry_shaders(ctx
))
975 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
976 save
->GeometryShader
);
978 if (ctx
->Extensions
.ARB_fragment_shader
)
979 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
980 save
->FragmentShader
);
982 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
985 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
986 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
987 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
988 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
991 if (state
& MESA_META_STENCIL_TEST
) {
992 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
994 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
995 _mesa_ClearStencil(stencil
->Clear
);
996 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
997 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
998 stencil
->TestTwoSide
);
999 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1000 ? GL_BACK
: GL_FRONT
);
1003 _mesa_StencilFuncSeparate(GL_FRONT
,
1004 stencil
->Function
[0],
1006 stencil
->ValueMask
[0]);
1007 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1008 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1009 stencil
->ZFailFunc
[0],
1010 stencil
->ZPassFunc
[0]);
1012 _mesa_StencilFuncSeparate(GL_BACK
,
1013 stencil
->Function
[1],
1015 stencil
->ValueMask
[1]);
1016 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1017 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1018 stencil
->ZFailFunc
[1],
1019 stencil
->ZPassFunc
[1]);
1022 if (state
& MESA_META_TEXTURE
) {
1025 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1027 /* restore texenv for unit[0] */
1028 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1029 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1032 /* restore texture objects for unit[0] only */
1033 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1034 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1035 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1036 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1037 save
->CurrentTexture
[tgt
]);
1039 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1042 /* Restore fixed function texture enables, texgen */
1043 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1044 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1045 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1046 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1047 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1050 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1051 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1052 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1057 /* restore current unit state */
1058 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1059 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1062 if (state
& MESA_META_TRANSFORM
) {
1063 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1064 _mesa_ActiveTexture(GL_TEXTURE0
);
1065 _mesa_MatrixMode(GL_TEXTURE
);
1066 _mesa_LoadMatrixf(save
->TextureMatrix
);
1067 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1069 _mesa_MatrixMode(GL_MODELVIEW
);
1070 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1072 _mesa_MatrixMode(GL_PROJECTION
);
1073 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1075 _mesa_MatrixMode(save
->MatrixMode
);
1078 if (state
& MESA_META_CLIP
) {
1079 if (save
->ClipPlanesEnabled
) {
1081 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1082 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1083 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1089 if (state
& MESA_META_VERTEX
) {
1090 /* restore vertex buffer object */
1091 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1092 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1094 /* restore vertex array object */
1095 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1096 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1099 if (state
& MESA_META_VIEWPORT
) {
1100 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1101 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1102 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1103 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1104 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1105 save
->ViewportW
, save
->ViewportH
);
1107 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1110 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1111 ctx
->Extensions
.ARB_color_buffer_float
) {
1112 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1115 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1116 ctx
->Extensions
.ARB_color_buffer_float
) {
1117 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1120 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1121 if (save
->CondRenderQuery
)
1122 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1123 save
->CondRenderMode
);
1126 if (state
& MESA_META_SELECT_FEEDBACK
) {
1127 if (save
->RenderMode
== GL_SELECT
) {
1128 _mesa_RenderMode(GL_SELECT
);
1129 ctx
->Select
= save
->Select
;
1130 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1131 _mesa_RenderMode(GL_FEEDBACK
);
1132 ctx
->Feedback
= save
->Feedback
;
1136 if (state
& MESA_META_MULTISAMPLE
) {
1137 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1138 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1141 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1142 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1143 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1147 if (save
->Lighting
) {
1148 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1150 if (save
->RasterDiscard
) {
1151 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1153 if (save
->TransformFeedbackNeedsResume
)
1154 _mesa_ResumeTransformFeedback();
1156 ctx
->Meta
->SaveStackDepth
--;
1161 * Determine whether Mesa is currently in a meta state.
1164 _mesa_meta_in_progress(struct gl_context
*ctx
)
1166 return ctx
->Meta
->SaveStackDepth
!= 0;
1171 * Convert Z from a normalized value in the range [0, 1] to an object-space
1172 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1173 * default/identity ortho projection results in the original Z value.
1174 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1175 * value comes from the clear value or raster position.
1177 static INLINE GLfloat
1178 invert_z(GLfloat normZ
)
1180 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1186 * One-time init for a temp_texture object.
1187 * Choose tex target, compute max tex size, etc.
1190 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1192 /* prefer texture rectangle */
1193 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1194 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1195 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1196 tex
->NPOT
= GL_TRUE
;
1199 /* use 2D texture, NPOT if possible */
1200 tex
->Target
= GL_TEXTURE_2D
;
1201 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1202 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1204 tex
->MinSize
= 16; /* 16 x 16 at least */
1205 assert(tex
->MaxSize
> 0);
1207 _mesa_GenTextures(1, &tex
->TexObj
);
1211 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1215 _mesa_DeleteTextures(1, &tex
->TexObj
);
1221 * Return pointer to temp_texture info for non-bitmap ops.
1222 * This does some one-time init if needed.
1224 static struct temp_texture
*
1225 get_temp_texture(struct gl_context
*ctx
)
1227 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1230 init_temp_texture(ctx
, tex
);
1238 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1239 * We use a separate texture for bitmaps to reduce texture
1240 * allocation/deallocation.
1242 static struct temp_texture
*
1243 get_bitmap_temp_texture(struct gl_context
*ctx
)
1245 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1248 init_temp_texture(ctx
, tex
);
1255 * Return pointer to depth temp_texture.
1256 * This does some one-time init if needed.
1258 static struct temp_texture
*
1259 get_temp_depth_texture(struct gl_context
*ctx
)
1261 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1264 init_temp_texture(ctx
, tex
);
1271 * Compute the width/height of texture needed to draw an image of the
1272 * given size. Return a flag indicating whether the current texture
1273 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1274 * allocated (glTexImage2D).
1275 * Also, compute s/t texcoords for drawing.
1277 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1280 alloc_texture(struct temp_texture
*tex
,
1281 GLsizei width
, GLsizei height
, GLenum intFormat
)
1283 GLboolean newTex
= GL_FALSE
;
1285 ASSERT(width
<= tex
->MaxSize
);
1286 ASSERT(height
<= tex
->MaxSize
);
1288 if (width
> tex
->Width
||
1289 height
> tex
->Height
||
1290 intFormat
!= tex
->IntFormat
) {
1291 /* alloc new texture (larger or different format) */
1294 /* use non-power of two size */
1295 tex
->Width
= MAX2(tex
->MinSize
, width
);
1296 tex
->Height
= MAX2(tex
->MinSize
, height
);
1299 /* find power of two size */
1301 w
= h
= tex
->MinSize
;
1310 tex
->IntFormat
= intFormat
;
1315 /* compute texcoords */
1316 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1317 tex
->Sright
= (GLfloat
) width
;
1318 tex
->Ttop
= (GLfloat
) height
;
1321 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1322 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1330 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1333 setup_copypix_texture(struct gl_context
*ctx
,
1334 struct temp_texture
*tex
,
1336 GLint srcX
, GLint srcY
,
1337 GLsizei width
, GLsizei height
, GLenum intFormat
,
1340 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1341 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1342 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1343 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1344 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1346 /* copy framebuffer image to texture */
1348 /* create new tex image */
1349 if (tex
->Width
== width
&& tex
->Height
== height
) {
1350 /* create new tex with framebuffer data */
1351 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1352 srcX
, srcY
, width
, height
, 0);
1355 /* create empty texture */
1356 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1357 tex
->Width
, tex
->Height
, 0,
1358 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1360 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1361 0, 0, srcX
, srcY
, width
, height
);
1365 /* replace existing tex image */
1366 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1367 0, 0, srcX
, srcY
, width
, height
);
1373 * Setup/load texture for glDrawPixels.
1376 setup_drawpix_texture(struct gl_context
*ctx
,
1377 struct temp_texture
*tex
,
1379 GLenum texIntFormat
,
1380 GLsizei width
, GLsizei height
,
1381 GLenum format
, GLenum type
,
1382 const GLvoid
*pixels
)
1384 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1385 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1386 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1387 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1388 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1390 /* copy pixel data to texture */
1392 /* create new tex image */
1393 if (tex
->Width
== width
&& tex
->Height
== height
) {
1394 /* create new tex and load image data */
1395 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1396 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1399 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1401 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1402 ctx
->Unpack
.BufferObj
);
1403 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1404 /* create empty texture */
1405 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1406 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1407 if (save_unpack_obj
!= NULL
)
1408 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1409 save_unpack_obj
->Name
);
1411 _mesa_TexSubImage2D(tex
->Target
, 0,
1412 0, 0, width
, height
, format
, type
, pixels
);
1416 /* replace existing tex image */
1417 _mesa_TexSubImage2D(tex
->Target
, 0,
1418 0, 0, width
, height
, format
, type
, pixels
);
1425 * One-time init for drawing depth pixels.
1428 init_blit_depth_pixels(struct gl_context
*ctx
)
1430 static const char *program
=
1432 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1435 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1436 struct temp_texture
*tex
= get_temp_texture(ctx
);
1437 const char *texTarget
;
1439 assert(blit
->DepthFP
== 0);
1441 /* replace %s with "RECT" or "2D" */
1442 assert(strlen(program
) + 4 < sizeof(program2
));
1443 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1447 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1449 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1450 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1451 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1452 strlen(program2
), (const GLubyte
*) program2
);
1456 setup_ff_blit_framebuffer(struct gl_context
*ctx
,
1457 struct blit_state
*blit
)
1462 struct vertex verts
[4];
1464 if (blit
->ArrayObj
== 0) {
1465 /* one-time setup */
1467 /* create vertex array object */
1468 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1469 _mesa_BindVertexArray(blit
->ArrayObj
);
1471 /* create vertex array buffer */
1472 _mesa_GenBuffers(1, &blit
->VBO
);
1473 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1474 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1475 NULL
, GL_DYNAMIC_DRAW_ARB
);
1477 /* setup vertex arrays */
1478 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1479 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1480 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1481 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1484 /* setup projection matrix */
1485 _mesa_MatrixMode(GL_PROJECTION
);
1486 _mesa_LoadIdentity();
1487 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1492 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1493 struct blit_state
*blit
,
1499 struct vertex verts
[4];
1500 const char *vs_source
;
1505 GLboolean texture_2d
= (target
== GL_TEXTURE_2D
);
1507 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1508 assert(_mesa_is_desktop_gl(ctx
) || texture_2d
);
1510 /* Check if already initialized */
1511 if (blit
->ArrayObj
== 0) {
1513 /* create vertex array object */
1514 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1515 _mesa_BindVertexArray(blit
->ArrayObj
);
1517 /* create vertex array buffer */
1518 _mesa_GenBuffers(1, &blit
->VBO
);
1519 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1520 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1521 NULL
, GL_DYNAMIC_DRAW_ARB
);
1523 /* setup vertex arrays */
1524 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
1525 sizeof(struct vertex
), OFFSET(x
));
1526 _mesa_VertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
,
1527 sizeof(struct vertex
), OFFSET(s
));
1529 _mesa_EnableVertexAttribArray(0);
1530 _mesa_EnableVertexAttribArray(1);
1533 /* Generate a relevant fragment shader program for the texture target */
1534 if ((target
== GL_TEXTURE_2D
&& blit
->ShaderProg
!= 0) ||
1535 (target
== GL_TEXTURE_RECTANGLE
&& blit
->RectShaderProg
!= 0)) {
1539 mem_ctx
= ralloc_context(NULL
);
1541 if (ctx
->Const
.GLSLVersion
< 130) {
1543 "attribute vec2 position;\n"
1544 "attribute vec2 textureCoords;\n"
1545 "varying vec2 texCoords;\n"
1548 " texCoords = textureCoords;\n"
1549 " gl_Position = vec4(position, 0.0, 1.0);\n"
1552 fs_source
= ralloc_asprintf(mem_ctx
,
1554 "precision highp float;\n"
1556 "uniform %s texSampler;\n"
1557 "varying vec2 texCoords;\n"
1560 " gl_FragColor = %s(texSampler, texCoords);\n"
1561 " gl_FragDepth = gl_FragColor.r;\n"
1563 texture_2d
? "sampler2D" : "sampler2DRect",
1564 texture_2d
? "texture2D" : "texture2DRect");
1567 vs_source
= ralloc_asprintf(mem_ctx
,
1569 "in vec2 position;\n"
1570 "in vec2 textureCoords;\n"
1571 "out vec2 texCoords;\n"
1574 " texCoords = textureCoords;\n"
1575 " gl_Position = vec4(position, 0.0, 1.0);\n"
1577 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1578 fs_source
= ralloc_asprintf(mem_ctx
,
1581 "precision highp float;\n"
1583 "uniform %s texSampler;\n"
1584 "in vec2 texCoords;\n"
1585 "out vec4 out_color;\n"
1589 " out_color = %s(texSampler, texCoords);\n"
1590 " gl_FragDepth = out_color.r;\n"
1592 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
1593 texture_2d
? "sampler2D" : "sampler2DRect",
1594 texture_2d
? "texture" : "texture2DRect");
1597 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
1598 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
1600 ShaderProg
= _mesa_CreateProgramObjectARB();
1601 _mesa_AttachShader(ShaderProg
, fs
);
1602 _mesa_DeleteObjectARB(fs
);
1603 _mesa_AttachShader(ShaderProg
, vs
);
1604 _mesa_DeleteObjectARB(vs
);
1605 _mesa_BindAttribLocation(ShaderProg
, 0, "position");
1606 _mesa_BindAttribLocation(ShaderProg
, 1, "texcoords");
1607 link_program_with_debug(ctx
, ShaderProg
);
1608 ralloc_free(mem_ctx
);
1610 blit
->ShaderProg
= ShaderProg
;
1612 blit
->RectShaderProg
= ShaderProg
;
1616 * Try to do a glBlitFramebuffer using no-copy texturing.
1617 * We can do this when the src renderbuffer is actually a texture.
1618 * But if the src buffer == dst buffer we cannot do this.
1620 * \return new buffer mask indicating the buffers left to blit using the
1624 blitframebuffer_texture(struct gl_context
*ctx
,
1625 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1626 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1627 GLbitfield mask
, GLenum filter
, GLint flipX
,
1628 GLint flipY
, GLboolean glsl_version
)
1630 if (mask
& GL_COLOR_BUFFER_BIT
) {
1631 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1632 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1633 const struct gl_renderbuffer_attachment
*drawAtt
;
1634 const struct gl_renderbuffer_attachment
*readAtt
=
1635 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1637 if (readAtt
&& readAtt
->Texture
) {
1638 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1639 const GLint dstX
= MIN2(dstX0
, dstX1
);
1640 const GLint dstY
= MIN2(dstY0
, dstY1
);
1641 const GLint dstW
= abs(dstX1
- dstX0
);
1642 const GLint dstH
= abs(dstY1
- dstY0
);
1643 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1644 const GLuint srcLevel
= readAtt
->TextureLevel
;
1645 const GLint baseLevelSave
= texObj
->BaseLevel
;
1646 const GLint maxLevelSave
= texObj
->MaxLevel
;
1647 const GLenum target
= texObj
->Target
;
1648 GLuint sampler
, samplerSave
=
1649 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1650 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1653 /* Iterate through all draw buffers */
1654 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1655 int idx
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1658 drawAtt
= &drawFb
->Attachment
[idx
];
1660 if (drawAtt
->Texture
== readAtt
->Texture
) {
1661 /* Can't use same texture as both the source and dest. We need
1662 * to handle overlapping blits and besides, some hw may not
1669 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1670 /* Can't handle other texture types at this time */
1674 /* Choose between glsl version and fixed function version of
1675 * BlitFramebuffer function.
1678 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1679 if (target
== GL_TEXTURE_2D
)
1680 _mesa_UseProgram(blit
->ShaderProg
);
1682 _mesa_UseProgram(blit
->RectShaderProg
);
1685 setup_ff_blit_framebuffer(ctx
, &ctx
->Meta
->Blit
);
1688 _mesa_BindVertexArray(blit
->ArrayObj
);
1689 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1691 _mesa_GenSamplers(1, &sampler
);
1692 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1695 printf("Blit from texture!\n");
1696 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1697 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1700 /* Prepare src texture state */
1701 _mesa_BindTexture(target
, texObj
->Name
);
1702 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1703 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1704 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1705 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1706 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1708 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1709 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1711 /* Always do our blits with no sRGB decode or encode. Note that
1712 * GL_FRAMEBUFFER_SRGB has already been disabled by
1713 * _mesa_meta_begin().
1715 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1716 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1717 GL_SKIP_DECODE_EXT
);
1720 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1721 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1722 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1725 /* Prepare vertex data (the VBO was previously created and bound) */
1730 struct vertex verts
[4];
1731 GLfloat s0
, t0
, s1
, t1
;
1733 if (target
== GL_TEXTURE_2D
) {
1734 const struct gl_texture_image
*texImage
1735 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1736 s0
= srcX0
/ (float) texImage
->Width
;
1737 s1
= srcX1
/ (float) texImage
->Width
;
1738 t0
= srcY0
/ (float) texImage
->Height
;
1739 t1
= srcY1
/ (float) texImage
->Height
;
1742 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1749 /* setup vertex positions */
1750 verts
[0].x
= -1.0F
* flipX
;
1751 verts
[0].y
= -1.0F
* flipY
;
1752 verts
[1].x
= 1.0F
* flipX
;
1753 verts
[1].y
= -1.0F
* flipY
;
1754 verts
[2].x
= 1.0F
* flipX
;
1755 verts
[2].y
= 1.0F
* flipY
;
1756 verts
[3].x
= -1.0F
* flipX
;
1757 verts
[3].y
= 1.0F
* flipY
;
1768 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1771 /* setup viewport */
1772 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1773 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1774 _mesa_DepthMask(GL_FALSE
);
1775 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1777 /* Restore texture object state, the texture binding will
1778 * be restored by _mesa_meta_end().
1780 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1781 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1782 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1785 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1786 _mesa_DeleteSamplers(1, &sampler
);
1788 /* Done with color buffer */
1789 mask
&= ~GL_COLOR_BUFFER_BIT
;
1798 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1799 * of texture mapping and polygon rendering.
1802 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1803 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1804 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1805 GLbitfield mask
, GLenum filter
)
1807 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1808 struct temp_texture
*tex
= get_temp_texture(ctx
);
1809 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1810 const GLsizei maxTexSize
= tex
->MaxSize
;
1811 const GLint srcX
= MIN2(srcX0
, srcX1
);
1812 const GLint srcY
= MIN2(srcY0
, srcY1
);
1813 const GLint srcW
= abs(srcX1
- srcX0
);
1814 const GLint srcH
= abs(srcY1
- srcY0
);
1815 const GLint dstX
= MIN2(dstX0
, dstX1
);
1816 const GLint dstY
= MIN2(dstY0
, dstY1
);
1817 const GLint dstW
= abs(dstX1
- dstX0
);
1818 const GLint dstH
= abs(dstY1
- dstY0
);
1819 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1820 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1821 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1822 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1823 const GLint flipX
= srcFlipX
* dstFlipX
;
1824 const GLint flipY
= srcFlipY
* dstFlipY
;
1829 struct vertex verts
[4];
1831 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1832 ctx
->Extensions
.ARB_fragment_shader
&&
1833 (ctx
->API
!= API_OPENGLES
);
1835 /* In addition to falling back if the blit size is larger than the maximum
1836 * texture size, fallback if the source is multisampled. This fallback can
1837 * be removed once Mesa gets support ARB_texture_multisample.
1839 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1840 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1841 /* XXX avoid this fallback */
1842 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1843 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1847 /* only scissor effects blit so save/clear all other relevant state */
1848 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1850 /* Try faster, direct texture approach first */
1851 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1852 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1853 dstFlipX
, dstFlipY
, use_glsl_version
);
1855 _mesa_meta_end(ctx
);
1859 /* Choose between glsl version and fixed function version of
1860 * BlitFramebuffer function.
1862 if (use_glsl_version
) {
1863 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1864 if (tex
->Target
== GL_TEXTURE_2D
)
1865 _mesa_UseProgram(blit
->ShaderProg
);
1867 _mesa_UseProgram(blit
->RectShaderProg
);
1870 setup_ff_blit_framebuffer(ctx
, blit
);
1873 _mesa_BindVertexArray(blit
->ArrayObj
);
1874 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1876 /* Continue with "normal" approach which involves copying the src rect
1877 * into a temporary texture and is "blitted" by drawing a textured quad.
1880 /* setup vertex positions */
1881 verts
[0].x
= -1.0F
* flipX
;
1882 verts
[0].y
= -1.0F
* flipY
;
1883 verts
[1].x
= 1.0F
* flipX
;
1884 verts
[1].y
= -1.0F
* flipY
;
1885 verts
[2].x
= 1.0F
* flipX
;
1886 verts
[2].y
= 1.0F
* flipY
;
1887 verts
[3].x
= -1.0F
* flipX
;
1888 verts
[3].y
= 1.0F
* flipY
;
1892 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1895 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1896 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1898 if (mask
& GL_COLOR_BUFFER_BIT
) {
1899 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1900 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1901 const GLenum rb_base_format
=
1902 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1904 /* Using the exact source rectangle to create the texture does incorrect
1905 * linear filtering along the edges. So, allocate the texture extended along
1906 * edges by one pixel in x, y directions.
1908 newTex
= alloc_texture(tex
, srcW
+ 2, srcH
+ 2, rb_base_format
);
1909 setup_copypix_texture(ctx
, tex
, newTex
,
1910 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1911 rb_base_format
, filter
);
1912 /* texcoords (after texture allocation!) */
1916 verts
[1].s
= tex
->Sright
- 1.0F
;
1918 verts
[2].s
= tex
->Sright
- 1.0F
;
1919 verts
[2].t
= tex
->Ttop
- 1.0F
;
1921 verts
[3].t
= tex
->Ttop
- 1.0F
;
1923 /* upload new vertex data */
1924 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1927 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1928 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1929 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1930 _mesa_DepthMask(GL_FALSE
);
1931 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1932 mask
&= ~GL_COLOR_BUFFER_BIT
;
1935 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1936 _mesa_is_desktop_gl(ctx
) &&
1937 ctx
->Extensions
.ARB_depth_texture
&&
1938 ctx
->Extensions
.ARB_fragment_program
) {
1940 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1944 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1945 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1946 GL_UNSIGNED_INT
, tmp
);
1947 setup_drawpix_texture(ctx
, depthTex
, newTex
, GL_DEPTH_COMPONENT
,
1948 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1949 GL_UNSIGNED_INT
, tmp
);
1951 /* texcoords (after texture allocation!) */
1955 verts
[1].s
= depthTex
->Sright
;
1957 verts
[2].s
= depthTex
->Sright
;
1958 verts
[2].t
= depthTex
->Ttop
;
1960 verts
[3].t
= depthTex
->Ttop
;
1962 /* upload new vertex data */
1963 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1967 init_blit_depth_pixels(ctx
);
1969 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1970 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1971 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1972 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1973 _mesa_DepthFunc(GL_ALWAYS
);
1974 _mesa_DepthMask(GL_TRUE
);
1976 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1977 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1978 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1979 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1985 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1986 /* XXX can't easily do stencil */
1989 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1990 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1992 _mesa_meta_end(ctx
);
1995 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1996 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
2001 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
2003 if (blit
->ArrayObj
) {
2004 _mesa_DeleteVertexArrays(1, &blit
->ArrayObj
);
2006 _mesa_DeleteBuffers(1, &blit
->VBO
);
2009 if (blit
->DepthFP
) {
2010 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
2014 _mesa_DeleteObjectARB(blit
->ShaderProg
);
2015 blit
->ShaderProg
= 0;
2016 _mesa_DeleteObjectARB(blit
->RectShaderProg
);
2017 blit
->RectShaderProg
= 0;
2019 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
2020 blit
->depthTex
.TexObj
= 0;
2025 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2028 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2030 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2032 GLfloat x
, y
, z
, r
, g
, b
, a
;
2034 struct vertex verts
[4];
2035 /* save all state but scissor, pixel pack/unpack */
2036 GLbitfield metaSave
= (MESA_META_ALL
-
2038 MESA_META_PIXEL_STORE
-
2039 MESA_META_CONDITIONAL_RENDER
-
2040 MESA_META_FRAMEBUFFER_SRGB
);
2041 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2043 if (buffers
& BUFFER_BITS_COLOR
) {
2044 /* if clearing color buffers, don't save/restore colormask */
2045 metaSave
-= MESA_META_COLOR_MASK
;
2048 _mesa_meta_begin(ctx
, metaSave
);
2050 if (clear
->ArrayObj
== 0) {
2051 /* one-time setup */
2053 /* create vertex array object */
2054 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2055 _mesa_BindVertexArray(clear
->ArrayObj
);
2057 /* create vertex array buffer */
2058 _mesa_GenBuffers(1, &clear
->VBO
);
2059 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2061 /* setup vertex arrays */
2062 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2063 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2064 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2065 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2068 _mesa_BindVertexArray(clear
->ArrayObj
);
2069 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2072 /* GL_COLOR_BUFFER_BIT */
2073 if (buffers
& BUFFER_BITS_COLOR
) {
2074 /* leave colormask, glDrawBuffer state as-is */
2076 /* Clears never have the color clamped. */
2077 if (ctx
->Extensions
.ARB_color_buffer_float
)
2078 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2081 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2082 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2085 /* GL_DEPTH_BUFFER_BIT */
2086 if (buffers
& BUFFER_BIT_DEPTH
) {
2087 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2088 _mesa_DepthFunc(GL_ALWAYS
);
2089 _mesa_DepthMask(GL_TRUE
);
2092 assert(!ctx
->Depth
.Test
);
2095 /* GL_STENCIL_BUFFER_BIT */
2096 if (buffers
& BUFFER_BIT_STENCIL
) {
2097 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2098 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2099 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2100 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2101 ctx
->Stencil
.Clear
& stencilMax
,
2102 ctx
->Stencil
.WriteMask
[0]);
2105 assert(!ctx
->Stencil
.Enabled
);
2108 /* vertex positions/colors */
2110 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
2111 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
2112 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
2113 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
2114 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
2131 for (i
= 0; i
< 4; i
++) {
2132 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
2133 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
2134 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
2135 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
2138 /* upload new vertex data */
2139 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2140 GL_DYNAMIC_DRAW_ARB
);
2144 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2146 _mesa_meta_end(ctx
);
2150 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
2152 const char *vs_source
=
2153 "attribute vec4 position;\n"
2156 " gl_Position = position;\n"
2158 const char *gs_source
=
2160 "layout(triangles) in;\n"
2161 "layout(triangle_strip, max_vertices = 4) out;\n"
2162 "uniform int layer;\n"
2165 " for (int i = 0; i < 3; i++) {\n"
2166 " gl_Layer = layer;\n"
2167 " gl_Position = gl_in[i].gl_Position;\n"
2171 const char *fs_source
=
2173 "precision highp float;\n"
2175 "uniform vec4 color;\n"
2178 " gl_FragColor = color;\n"
2180 GLuint vs
, gs
= 0, fs
;
2181 bool has_integer_textures
;
2183 if (clear
->ArrayObj
!= 0)
2186 /* create vertex array object */
2187 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2188 _mesa_BindVertexArray(clear
->ArrayObj
);
2190 /* create vertex array buffer */
2191 _mesa_GenBuffers(1, &clear
->VBO
);
2192 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2194 /* setup vertex arrays */
2195 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
2196 _mesa_EnableVertexAttribArray(0);
2198 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
2199 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
2200 _mesa_CompileShader(vs
);
2202 if (_mesa_has_geometry_shaders(ctx
)) {
2203 gs
= _mesa_CreateShaderObjectARB(GL_GEOMETRY_SHADER
);
2204 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
2205 _mesa_CompileShader(gs
);
2208 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
2209 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
2210 _mesa_CompileShader(fs
);
2212 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
2213 _mesa_AttachShader(clear
->ShaderProg
, fs
);
2214 _mesa_DeleteObjectARB(fs
);
2216 _mesa_AttachShader(clear
->ShaderProg
, gs
);
2217 _mesa_AttachShader(clear
->ShaderProg
, vs
);
2218 _mesa_DeleteObjectARB(vs
);
2219 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
2220 _mesa_LinkProgram(clear
->ShaderProg
);
2222 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2225 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2229 has_integer_textures
= _mesa_is_gles3(ctx
) ||
2230 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
2232 if (has_integer_textures
) {
2233 void *shader_source_mem_ctx
= ralloc_context(NULL
);
2234 const char *vs_int_source
=
2235 ralloc_asprintf(shader_source_mem_ctx
,
2237 "in vec4 position;\n"
2240 " gl_Position = position;\n"
2242 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2243 const char *fs_int_source
=
2244 ralloc_asprintf(shader_source_mem_ctx
,
2247 "precision highp float;\n"
2249 "uniform ivec4 color;\n"
2250 "out ivec4 out_color;\n"
2254 " out_color = color;\n"
2256 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2258 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
2259 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
2260 ralloc_free(shader_source_mem_ctx
);
2262 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
2263 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
2264 _mesa_DeleteObjectARB(fs
);
2266 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
2267 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
2268 _mesa_DeleteObjectARB(vs
);
2269 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
2271 /* Note that user-defined out attributes get automatically assigned
2272 * locations starting from 0, so we don't need to explicitly
2273 * BindFragDataLocation to 0.
2276 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
2278 clear
->IntegerColorLocation
=
2279 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2281 clear
->IntegerLayerLocation
=
2282 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
2286 _mesa_DeleteObjectARB(gs
);
2290 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
2292 if (clear
->ArrayObj
== 0)
2294 _mesa_DeleteVertexArrays(1, &clear
->ArrayObj
);
2295 clear
->ArrayObj
= 0;
2296 _mesa_DeleteBuffers(1, &clear
->VBO
);
2298 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2299 clear
->ShaderProg
= 0;
2301 if (clear
->IntegerShaderProg
) {
2302 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2303 clear
->IntegerShaderProg
= 0;
2308 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2311 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2313 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2314 GLbitfield metaSave
;
2315 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2316 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2317 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2318 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2319 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2320 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2321 const float z
= -invert_z(ctx
->Depth
.Clear
);
2326 metaSave
= (MESA_META_ALPHA_TEST
|
2328 MESA_META_DEPTH_TEST
|
2329 MESA_META_RASTERIZATION
|
2331 MESA_META_STENCIL_TEST
|
2333 MESA_META_VIEWPORT
|
2335 MESA_META_CLAMP_FRAGMENT_COLOR
|
2336 MESA_META_MULTISAMPLE
|
2337 MESA_META_OCCLUSION_QUERY
);
2339 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2340 /* We'll use colormask to disable color writes. Otherwise,
2341 * respect color mask
2343 metaSave
|= MESA_META_COLOR_MASK
;
2346 _mesa_meta_begin(ctx
, metaSave
);
2348 meta_glsl_clear_init(ctx
, clear
);
2350 if (fb
->_IntegerColor
) {
2351 _mesa_UseProgram(clear
->IntegerShaderProg
);
2352 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2353 ctx
->Color
.ClearColor
.i
);
2355 _mesa_UseProgram(clear
->ShaderProg
);
2356 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2357 ctx
->Color
.ClearColor
.f
);
2360 _mesa_BindVertexArray(clear
->ArrayObj
);
2361 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2363 /* GL_COLOR_BUFFER_BIT */
2364 if (buffers
& BUFFER_BITS_COLOR
) {
2365 /* leave colormask, glDrawBuffer state as-is */
2367 /* Clears never have the color clamped. */
2368 if (ctx
->Extensions
.ARB_color_buffer_float
)
2369 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2372 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2373 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2376 /* GL_DEPTH_BUFFER_BIT */
2377 if (buffers
& BUFFER_BIT_DEPTH
) {
2378 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2379 _mesa_DepthFunc(GL_ALWAYS
);
2380 _mesa_DepthMask(GL_TRUE
);
2383 assert(!ctx
->Depth
.Test
);
2386 /* GL_STENCIL_BUFFER_BIT */
2387 if (buffers
& BUFFER_BIT_STENCIL
) {
2388 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2389 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2390 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2391 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2392 ctx
->Stencil
.Clear
& stencilMax
,
2393 ctx
->Stencil
.WriteMask
[0]);
2396 assert(!ctx
->Stencil
.Enabled
);
2399 /* vertex positions */
2413 /* upload new vertex data */
2414 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2415 GL_DYNAMIC_DRAW_ARB
);
2418 if (fb
->MaxNumLayers
> 0) {
2420 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
2421 if (fb
->_IntegerColor
)
2422 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
2424 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
2425 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2428 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2431 _mesa_meta_end(ctx
);
2435 * Meta implementation of ctx->Driver.CopyPixels() in terms
2436 * of texture mapping and polygon rendering and GLSL shaders.
2439 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2440 GLsizei width
, GLsizei height
,
2441 GLint dstX
, GLint dstY
, GLenum type
)
2443 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2444 struct temp_texture
*tex
= get_temp_texture(ctx
);
2446 GLfloat x
, y
, z
, s
, t
;
2448 struct vertex verts
[4];
2450 GLenum intFormat
= GL_RGBA
;
2452 if (type
!= GL_COLOR
||
2453 ctx
->_ImageTransferState
||
2455 width
> tex
->MaxSize
||
2456 height
> tex
->MaxSize
) {
2457 /* XXX avoid this fallback */
2458 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2462 /* Most GL state applies to glCopyPixels, but a there's a few things
2463 * we need to override:
2465 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2468 MESA_META_TRANSFORM
|
2471 MESA_META_VIEWPORT
));
2473 if (copypix
->ArrayObj
== 0) {
2474 /* one-time setup */
2476 /* create vertex array object */
2477 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2478 _mesa_BindVertexArray(copypix
->ArrayObj
);
2480 /* create vertex array buffer */
2481 _mesa_GenBuffers(1, ©pix
->VBO
);
2482 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2483 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2484 NULL
, GL_DYNAMIC_DRAW_ARB
);
2486 /* setup vertex arrays */
2487 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2488 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2489 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2490 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2493 _mesa_BindVertexArray(copypix
->ArrayObj
);
2494 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2497 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2499 /* vertex positions, texcoords (after texture allocation!) */
2501 const GLfloat dstX0
= (GLfloat
) dstX
;
2502 const GLfloat dstY0
= (GLfloat
) dstY
;
2503 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2504 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2505 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2515 verts
[1].s
= tex
->Sright
;
2520 verts
[2].s
= tex
->Sright
;
2521 verts
[2].t
= tex
->Ttop
;
2526 verts
[3].t
= tex
->Ttop
;
2528 /* upload new vertex data */
2529 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2532 /* Alloc/setup texture */
2533 setup_copypix_texture(ctx
, tex
, newTex
, srcX
, srcY
, width
, height
,
2534 GL_RGBA
, GL_NEAREST
);
2536 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2538 /* draw textured quad */
2539 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2541 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2543 _mesa_meta_end(ctx
);
2549 * When the glDrawPixels() image size is greater than the max rectangle
2550 * texture size we use this function to break the glDrawPixels() image
2551 * into tiles which fit into the max texture size.
2554 tiled_draw_pixels(struct gl_context
*ctx
,
2556 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2557 GLenum format
, GLenum type
,
2558 const struct gl_pixelstore_attrib
*unpack
,
2559 const GLvoid
*pixels
)
2561 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2564 if (tileUnpack
.RowLength
== 0)
2565 tileUnpack
.RowLength
= width
;
2567 for (i
= 0; i
< width
; i
+= tileSize
) {
2568 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2569 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2571 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2573 for (j
= 0; j
< height
; j
+= tileSize
) {
2574 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2575 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2577 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2579 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2580 format
, type
, &tileUnpack
, pixels
);
2587 * One-time init for drawing stencil pixels.
2590 init_draw_stencil_pixels(struct gl_context
*ctx
)
2592 /* This program is run eight times, once for each stencil bit.
2593 * The stencil values to draw are found in an 8-bit alpha texture.
2594 * We read the texture/stencil value and test if bit 'b' is set.
2595 * If the bit is not set, use KIL to kill the fragment.
2596 * Finally, we use the stencil test to update the stencil buffer.
2598 * The basic algorithm for checking if a bit is set is:
2599 * if (is_odd(value / (1 << bit)))
2600 * result is one (or non-zero).
2603 * The program parameter contains three values:
2604 * parm.x = 255 / (1 << bit)
2608 static const char *program
=
2610 "PARAM parm = program.local[0]; \n"
2612 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2613 "# t = t * 255 / bit \n"
2614 "MUL t.x, t.a, parm.x; \n"
2617 "SUB t.x, t.x, t.y; \n"
2619 "MUL t.x, t.x, parm.y; \n"
2620 "# t = fract(t.x) \n"
2621 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2622 "# t.x = (t.x == 0 ? 1 : 0) \n"
2623 "SGE t.x, -t.x, parm.z; \n"
2625 "# for debug only \n"
2626 "#MOV result.color, t.x; \n"
2628 char program2
[1000];
2629 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2630 struct temp_texture
*tex
= get_temp_texture(ctx
);
2631 const char *texTarget
;
2633 assert(drawpix
->StencilFP
== 0);
2635 /* replace %s with "RECT" or "2D" */
2636 assert(strlen(program
) + 4 < sizeof(program2
));
2637 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2641 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2643 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2644 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2645 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2646 strlen(program2
), (const GLubyte
*) program2
);
2651 * One-time init for drawing depth pixels.
2654 init_draw_depth_pixels(struct gl_context
*ctx
)
2656 static const char *program
=
2658 "PARAM color = program.local[0]; \n"
2659 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2660 "MOV result.color, color; \n"
2663 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2664 struct temp_texture
*tex
= get_temp_texture(ctx
);
2665 const char *texTarget
;
2667 assert(drawpix
->DepthFP
== 0);
2669 /* replace %s with "RECT" or "2D" */
2670 assert(strlen(program
) + 4 < sizeof(program2
));
2671 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2675 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2677 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2678 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2679 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2680 strlen(program2
), (const GLubyte
*) program2
);
2685 * Meta implementation of ctx->Driver.DrawPixels() in terms
2686 * of texture mapping and polygon rendering.
2689 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2690 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2691 GLenum format
, GLenum type
,
2692 const struct gl_pixelstore_attrib
*unpack
,
2693 const GLvoid
*pixels
)
2695 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2696 struct temp_texture
*tex
= get_temp_texture(ctx
);
2697 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2698 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2700 GLfloat x
, y
, z
, s
, t
;
2702 struct vertex verts
[4];
2703 GLenum texIntFormat
;
2704 GLboolean fallback
, newTex
;
2705 GLbitfield metaExtraSave
= 0x0;
2709 * Determine if we can do the glDrawPixels with texture mapping.
2711 fallback
= GL_FALSE
;
2712 if (ctx
->Fog
.Enabled
) {
2716 if (_mesa_is_color_format(format
)) {
2717 /* use more compact format when possible */
2718 /* XXX disable special case for GL_LUMINANCE for now to work around
2719 * apparent i965 driver bug (see bug #23670).
2721 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2722 texIntFormat
= format
;
2724 texIntFormat
= GL_RGBA
;
2726 /* If we're not supposed to clamp the resulting color, then just
2727 * promote our texture to fully float. We could do better by
2728 * just going for the matching set of channels, in floating
2731 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2732 ctx
->Extensions
.ARB_texture_float
)
2733 texIntFormat
= GL_RGBA32F
;
2735 else if (_mesa_is_stencil_format(format
)) {
2736 if (ctx
->Extensions
.ARB_fragment_program
&&
2737 ctx
->Pixel
.IndexShift
== 0 &&
2738 ctx
->Pixel
.IndexOffset
== 0 &&
2739 type
== GL_UNSIGNED_BYTE
) {
2740 /* We'll store stencil as alpha. This only works for GLubyte
2741 * image data because of how incoming values are mapped to alpha
2744 texIntFormat
= GL_ALPHA
;
2745 metaExtraSave
= (MESA_META_COLOR_MASK
|
2746 MESA_META_DEPTH_TEST
|
2747 MESA_META_PIXEL_TRANSFER
|
2749 MESA_META_STENCIL_TEST
);
2755 else if (_mesa_is_depth_format(format
)) {
2756 if (ctx
->Extensions
.ARB_depth_texture
&&
2757 ctx
->Extensions
.ARB_fragment_program
) {
2758 texIntFormat
= GL_DEPTH_COMPONENT
;
2759 metaExtraSave
= (MESA_META_SHADER
);
2770 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2771 format
, type
, unpack
, pixels
);
2776 * Check image size against max texture size, draw as tiles if needed.
2778 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2779 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2780 format
, type
, unpack
, pixels
);
2784 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2785 * but a there's a few things we need to override:
2787 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2790 MESA_META_TRANSFORM
|
2793 MESA_META_VIEWPORT
|
2796 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2798 /* vertex positions, texcoords (after texture allocation!) */
2800 const GLfloat x0
= (GLfloat
) x
;
2801 const GLfloat y0
= (GLfloat
) y
;
2802 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2803 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2804 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2814 verts
[1].s
= tex
->Sright
;
2819 verts
[2].s
= tex
->Sright
;
2820 verts
[2].t
= tex
->Ttop
;
2825 verts
[3].t
= tex
->Ttop
;
2828 if (drawpix
->ArrayObj
== 0) {
2829 /* one-time setup: create vertex array object */
2830 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2832 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2834 /* create vertex array buffer */
2835 _mesa_GenBuffers(1, &vbo
);
2836 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2837 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2838 verts
, GL_DYNAMIC_DRAW_ARB
);
2840 /* setup vertex arrays */
2841 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2842 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2843 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2844 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2846 /* set given unpack params */
2847 ctx
->Unpack
= *unpack
;
2849 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2851 if (_mesa_is_stencil_format(format
)) {
2852 /* Drawing stencil */
2855 if (!drawpix
->StencilFP
)
2856 init_draw_stencil_pixels(ctx
);
2858 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2859 GL_ALPHA
, type
, pixels
);
2861 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2863 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2865 /* set all stencil bits to 0 */
2866 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2867 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2868 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2870 /* set stencil bits to 1 where needed */
2871 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2873 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2874 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2876 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2877 const GLuint mask
= 1 << bit
;
2878 if (mask
& origStencilMask
) {
2879 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2880 _mesa_StencilMask(mask
);
2882 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2883 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2885 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2889 else if (_mesa_is_depth_format(format
)) {
2891 if (!drawpix
->DepthFP
)
2892 init_draw_depth_pixels(ctx
);
2894 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2895 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2897 /* polygon color = current raster color */
2898 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2899 ctx
->Current
.RasterColor
);
2901 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2902 format
, type
, pixels
);
2904 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2908 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2909 format
, type
, pixels
);
2910 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2913 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2915 _mesa_DeleteBuffers(1, &vbo
);
2917 /* restore unpack params */
2918 ctx
->Unpack
= unpackSave
;
2920 _mesa_meta_end(ctx
);
2924 alpha_test_raster_color(struct gl_context
*ctx
)
2926 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2927 GLfloat ref
= ctx
->Color
.AlphaRef
;
2929 switch (ctx
->Color
.AlphaFunc
) {
2935 return alpha
== ref
;
2937 return alpha
<= ref
;
2941 return alpha
!= ref
;
2943 return alpha
>= ref
;
2953 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2954 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2955 * tracker would improve performance a lot.
2958 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2959 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2960 const struct gl_pixelstore_attrib
*unpack
,
2961 const GLubyte
*bitmap1
)
2963 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2964 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2965 const GLenum texIntFormat
= GL_ALPHA
;
2966 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2969 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2971 struct vertex verts
[4];
2976 * Check if swrast fallback is needed.
2978 if (ctx
->_ImageTransferState
||
2979 ctx
->FragmentProgram
._Enabled
||
2981 ctx
->Texture
._EnabledUnits
||
2982 width
> tex
->MaxSize
||
2983 height
> tex
->MaxSize
) {
2984 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2988 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2991 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2992 * but a there's a few things we need to override:
2994 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2995 MESA_META_PIXEL_STORE
|
2996 MESA_META_RASTERIZATION
|
2999 MESA_META_TRANSFORM
|
3002 MESA_META_VIEWPORT
));
3004 if (bitmap
->ArrayObj
== 0) {
3005 /* one-time setup */
3007 /* create vertex array object */
3008 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
3009 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
3011 /* create vertex array buffer */
3012 _mesa_GenBuffers(1, &bitmap
->VBO
);
3013 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3014 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3015 NULL
, GL_DYNAMIC_DRAW_ARB
);
3017 /* setup vertex arrays */
3018 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3019 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
3020 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
3021 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3022 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3023 _mesa_EnableClientState(GL_COLOR_ARRAY
);
3026 _mesa_BindVertexArray(bitmap
->ArrayObj
);
3027 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3030 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
3032 /* vertex positions, texcoords, colors (after texture allocation!) */
3034 const GLfloat x0
= (GLfloat
) x
;
3035 const GLfloat y0
= (GLfloat
) y
;
3036 const GLfloat x1
= (GLfloat
) (x
+ width
);
3037 const GLfloat y1
= (GLfloat
) (y
+ height
);
3038 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
3049 verts
[1].s
= tex
->Sright
;
3054 verts
[2].s
= tex
->Sright
;
3055 verts
[2].t
= tex
->Ttop
;
3060 verts
[3].t
= tex
->Ttop
;
3062 for (i
= 0; i
< 4; i
++) {
3063 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
3064 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
3065 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
3066 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
3069 /* upload new vertex data */
3070 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3073 /* choose different foreground/background alpha values */
3074 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
3075 bg
= (fg
> 127 ? 0 : 255);
3077 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
3079 _mesa_meta_end(ctx
);
3083 bitmap8
= malloc(width
* height
);
3085 memset(bitmap8
, bg
, width
* height
);
3086 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
3087 bitmap8
, width
, fg
);
3089 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
3091 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
3092 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
3094 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
3095 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
3097 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3099 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
3104 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
3106 _mesa_meta_end(ctx
);
3111 * Check if the call to _mesa_meta_GenerateMipmap() will require a
3112 * software fallback. The fallback path will require that the texture
3113 * images are mapped.
3114 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
3117 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
3118 struct gl_texture_object
*texObj
)
3120 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3121 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3122 struct gl_texture_image
*baseImage
;
3126 /* check for fallbacks */
3127 if (target
== GL_TEXTURE_3D
||
3128 target
== GL_TEXTURE_1D_ARRAY
||
3129 target
== GL_TEXTURE_2D_ARRAY
) {
3130 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3131 "glGenerateMipmap() to %s target\n",
3132 _mesa_lookup_enum_by_nr(target
));
3136 srcLevel
= texObj
->BaseLevel
;
3137 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
3139 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3140 "glGenerateMipmap() couldn't find base teximage\n");
3144 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
3145 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3146 "glGenerateMipmap() with %s format\n",
3147 _mesa_get_format_name(baseImage
->TexFormat
));
3151 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
3152 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3153 /* The texture format is sRGB but we can't turn off sRGB->linear
3154 * texture sample conversion. So we won't be able to generate the
3155 * right colors when rendering. Need to use a fallback.
3157 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3158 "glGenerateMipmap() of sRGB texture without "
3164 * Test that we can actually render in the texture's format.
3167 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3168 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3170 if (target
== GL_TEXTURE_1D
) {
3171 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3172 GL_COLOR_ATTACHMENT0_EXT
,
3173 target
, texObj
->Name
, srcLevel
);
3176 /* other work is needed to enable 3D mipmap generation */
3177 else if (target
== GL_TEXTURE_3D
) {
3179 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3180 GL_COLOR_ATTACHMENT0_EXT
,
3181 target
, texObj
->Name
, srcLevel
, zoffset
);
3186 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3187 GL_COLOR_ATTACHMENT0_EXT
,
3188 target
, texObj
->Name
, srcLevel
);
3191 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3193 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3195 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3196 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3197 "glGenerateMipmap() got incomplete FBO\n");
3206 * Compute the texture coordinates for the four vertices of a quad for
3207 * drawing a 2D texture image or slice of a cube/3D texture.
3208 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
3209 * \param slice slice of a 1D/2D array texture or 3D texture
3210 * \param width width of the texture image
3211 * \param height height of the texture image
3212 * \param coords0/1/2/3 returns the computed texcoords
3215 setup_texture_coords(GLenum faceTarget
,
3225 static const GLfloat st
[4][2] = {
3226 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
3231 switch (faceTarget
) {
3235 case GL_TEXTURE_2D_ARRAY
:
3236 if (faceTarget
== GL_TEXTURE_3D
) {
3237 assert(slice
< depth
);
3239 r
= (slice
+ 0.5f
) / depth
;
3241 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
3245 coords0
[0] = 0.0F
; /* s */
3246 coords0
[1] = 0.0F
; /* t */
3247 coords0
[2] = r
; /* r */
3258 case GL_TEXTURE_RECTANGLE_ARB
:
3259 coords0
[0] = 0.0F
; /* s */
3260 coords0
[1] = 0.0F
; /* t */
3261 coords0
[2] = 0.0F
; /* r */
3262 coords1
[0] = (float) width
;
3265 coords2
[0] = (float) width
;
3266 coords2
[1] = (float) height
;
3269 coords3
[1] = (float) height
;
3272 case GL_TEXTURE_1D_ARRAY
:
3273 coords0
[0] = 0.0F
; /* s */
3274 coords0
[1] = (float) slice
; /* t */
3275 coords0
[2] = 0.0F
; /* r */
3277 coords1
[1] = (float) slice
;
3280 coords2
[1] = (float) slice
;
3283 coords3
[1] = (float) slice
;
3287 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3288 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3289 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3290 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3291 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3292 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3293 /* loop over quad verts */
3294 for (i
= 0; i
< 4; i
++) {
3295 /* Compute sc = +/-scale and tc = +/-scale.
3296 * Not +/-1 to avoid cube face selection ambiguity near the edges,
3297 * though that can still sometimes happen with this scale factor...
3299 const GLfloat scale
= 0.9999f
;
3300 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
3301 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
3321 switch (faceTarget
) {
3322 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3327 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3332 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3337 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3342 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3347 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3358 assert(0 && "unexpected target in meta setup_texture_coords()");
3364 setup_ff_generate_mipmap(struct gl_context
*ctx
,
3365 struct gen_mipmap_state
*mipmap
)
3368 GLfloat x
, y
, tex
[3];
3371 if (mipmap
->ArrayObj
== 0) {
3372 /* one-time setup */
3373 /* create vertex array object */
3374 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3375 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
3377 /* create vertex array buffer */
3378 _mesa_GenBuffers(1, &mipmap
->VBO
);
3379 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3380 /* setup vertex arrays */
3381 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3382 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3383 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3384 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3387 /* setup projection matrix */
3388 _mesa_MatrixMode(GL_PROJECTION
);
3389 _mesa_LoadIdentity();
3390 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3394 static struct glsl_sampler
*
3395 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3399 mipmap
->sampler_1d
.type
= "sampler1D";
3400 mipmap
->sampler_1d
.func
= "texture1D";
3401 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3402 return &mipmap
->sampler_1d
;
3404 mipmap
->sampler_2d
.type
= "sampler2D";
3405 mipmap
->sampler_2d
.func
= "texture2D";
3406 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3407 return &mipmap
->sampler_2d
;
3409 /* Code for mipmap generation with 3D textures is not used yet.
3410 * It's a sw fallback.
3412 mipmap
->sampler_3d
.type
= "sampler3D";
3413 mipmap
->sampler_3d
.func
= "texture3D";
3414 mipmap
->sampler_3d
.texcoords
= "texCoords";
3415 return &mipmap
->sampler_3d
;
3416 case GL_TEXTURE_CUBE_MAP
:
3417 mipmap
->sampler_cubemap
.type
= "samplerCube";
3418 mipmap
->sampler_cubemap
.func
= "textureCube";
3419 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3420 return &mipmap
->sampler_cubemap
;
3421 case GL_TEXTURE_1D_ARRAY
:
3422 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3423 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3424 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3425 return &mipmap
->sampler_1d_array
;
3426 case GL_TEXTURE_2D_ARRAY
:
3427 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3428 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3429 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3430 return &mipmap
->sampler_2d_array
;
3432 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3433 " setup_texture_sampler()\n", target
);
3440 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3441 struct gen_mipmap_state
*mipmap
,
3445 GLfloat x
, y
, tex
[3];
3447 struct glsl_sampler
*sampler
;
3448 const char *vs_source
;
3453 /* Check if already initialized */
3454 if (mipmap
->ArrayObj
== 0) {
3456 /* create vertex array object */
3457 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3458 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3460 /* create vertex array buffer */
3461 _mesa_GenBuffers(1, &mipmap
->VBO
);
3462 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3464 /* setup vertex arrays */
3465 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3466 sizeof(struct vertex
), OFFSET(x
));
3467 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3468 sizeof(struct vertex
), OFFSET(tex
));
3469 _mesa_EnableVertexAttribArray(0);
3470 _mesa_EnableVertexAttribArray(1);
3473 /* Generate a fragment shader program appropriate for the texture target */
3474 sampler
= setup_texture_sampler(target
, mipmap
);
3475 assert(sampler
!= NULL
);
3476 if (sampler
->shader_prog
!= 0) {
3477 mipmap
->ShaderProg
= sampler
->shader_prog
;
3481 mem_ctx
= ralloc_context(NULL
);
3483 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3485 "attribute vec2 position;\n"
3486 "attribute vec3 textureCoords;\n"
3487 "varying vec3 texCoords;\n"
3490 " texCoords = textureCoords;\n"
3491 " gl_Position = vec4(position, 0.0, 1.0);\n"
3494 fs_source
= ralloc_asprintf(mem_ctx
,
3495 "#extension GL_EXT_texture_array : enable\n"
3497 "precision highp float;\n"
3499 "uniform %s texSampler;\n"
3500 "varying vec3 texCoords;\n"
3503 " gl_FragColor = %s(texSampler, %s);\n"
3506 sampler
->func
, sampler
->texcoords
);
3509 vs_source
= ralloc_asprintf(mem_ctx
,
3511 "in vec2 position;\n"
3512 "in vec3 textureCoords;\n"
3513 "out vec3 texCoords;\n"
3516 " texCoords = textureCoords;\n"
3517 " gl_Position = vec4(position, 0.0, 1.0);\n"
3519 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
3520 fs_source
= ralloc_asprintf(mem_ctx
,
3523 "precision highp float;\n"
3525 "uniform %s texSampler;\n"
3526 "in vec3 texCoords;\n"
3527 "out vec4 out_color;\n"
3531 " out_color = texture(texSampler, %s);\n"
3533 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
3535 sampler
->texcoords
);
3538 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3539 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3541 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3542 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3543 _mesa_DeleteObjectARB(fs
);
3544 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3545 _mesa_DeleteObjectARB(vs
);
3546 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3547 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3548 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3549 sampler
->shader_prog
= mipmap
->ShaderProg
;
3550 ralloc_free(mem_ctx
);
3555 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3556 struct gen_mipmap_state
*mipmap
)
3558 if (mipmap
->ArrayObj
== 0)
3560 _mesa_DeleteVertexArrays(1, &mipmap
->ArrayObj
);
3561 mipmap
->ArrayObj
= 0;
3562 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3565 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3566 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3567 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3568 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3569 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3570 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3572 mipmap
->sampler_1d
.shader_prog
= 0;
3573 mipmap
->sampler_2d
.shader_prog
= 0;
3574 mipmap
->sampler_3d
.shader_prog
= 0;
3575 mipmap
->sampler_cubemap
.shader_prog
= 0;
3576 mipmap
->sampler_1d_array
.shader_prog
= 0;
3577 mipmap
->sampler_2d_array
.shader_prog
= 0;
3582 * Called via ctx->Driver.GenerateMipmap()
3583 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3587 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3588 struct gl_texture_object
*texObj
)
3590 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3592 GLfloat x
, y
, tex
[3];
3594 struct vertex verts
[4];
3595 const GLuint baseLevel
= texObj
->BaseLevel
;
3596 const GLuint maxLevel
= texObj
->MaxLevel
;
3597 const GLint maxLevelSave
= texObj
->MaxLevel
;
3598 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3599 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3600 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3601 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3602 ctx
->Extensions
.ARB_fragment_shader
&&
3603 (ctx
->API
!= API_OPENGLES
);
3606 const GLint slice
= 0;
3609 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3610 _mesa_generate_mipmap(ctx
, target
, texObj
);
3614 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3615 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3616 faceTarget
= target
;
3617 target
= GL_TEXTURE_CUBE_MAP
;
3620 faceTarget
= target
;
3623 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3625 /* Choose between glsl version and fixed function version of
3626 * GenerateMipmap function.
3628 if (use_glsl_version
) {
3629 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3630 _mesa_UseProgram(mipmap
->ShaderProg
);
3633 setup_ff_generate_mipmap(ctx
, mipmap
);
3634 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3637 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3638 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3640 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3641 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3643 if (currentTexUnitSave
!= 0)
3644 _mesa_BindTexture(target
, texObj
->Name
);
3647 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3650 if (!mipmap
->Sampler
) {
3651 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3652 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3654 _mesa_SamplerParameteri(mipmap
->Sampler
,
3655 GL_TEXTURE_MIN_FILTER
,
3656 GL_LINEAR_MIPMAP_LINEAR
);
3657 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3658 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3659 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3660 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3662 /* We don't want to encode or decode sRGB values; treat them as linear.
3663 * This is not technically correct for GLES3 but we don't get any API
3664 * error at the moment.
3666 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3667 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3668 GL_SKIP_DECODE_EXT
);
3672 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3675 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3677 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3678 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3680 assert(!genMipmapSave
);
3682 /* Setup texture coordinates */
3683 setup_texture_coords(faceTarget
,
3685 0, 0, 1, /* width, height never used here */
3691 /* setup vertex positions */
3701 /* upload vertex data */
3702 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3703 verts
, GL_DYNAMIC_DRAW_ARB
);
3705 /* texture is already locked, unlock now */
3706 _mesa_unlock_texture(ctx
, texObj
);
3708 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3709 const struct gl_texture_image
*srcImage
;
3710 const GLuint srcLevel
= dstLevel
- 1;
3711 GLsizei srcWidth
, srcHeight
, srcDepth
;
3712 GLsizei dstWidth
, dstHeight
, dstDepth
;
3715 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3716 assert(srcImage
->Border
== 0);
3719 srcWidth
= srcImage
->Width
;
3720 srcHeight
= srcImage
->Height
;
3721 srcDepth
= srcImage
->Depth
;
3724 dstWidth
= MAX2(1, srcWidth
/ 2);
3725 dstHeight
= MAX2(1, srcHeight
/ 2);
3726 dstDepth
= MAX2(1, srcDepth
/ 2);
3728 if (dstWidth
== srcImage
->Width
&&
3729 dstHeight
== srcImage
->Height
&&
3730 dstDepth
== srcImage
->Depth
) {
3735 /* Allocate storage for the destination mipmap image(s) */
3737 /* Set MaxLevel large enough to hold the new level when we allocate it */
3738 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3740 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3741 dstWidth
, dstHeight
, dstDepth
,
3743 srcImage
->InternalFormat
,
3744 srcImage
->TexFormat
)) {
3745 /* All done. We either ran out of memory or we would go beyond the
3746 * last valid level of an immutable texture if we continued.
3751 /* limit minification to src level */
3752 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3754 /* Set to draw into the current dstLevel */
3755 if (target
== GL_TEXTURE_1D
) {
3756 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3757 GL_COLOR_ATTACHMENT0_EXT
,
3762 else if (target
== GL_TEXTURE_3D
) {
3763 GLint zoffset
= 0; /* XXX unfinished */
3764 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3765 GL_COLOR_ATTACHMENT0_EXT
,
3772 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3773 GL_COLOR_ATTACHMENT0_EXT
,
3779 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3782 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3783 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3784 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3785 "_mesa_meta_GenerateMipmap()");
3789 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3790 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3792 /* setup viewport */
3793 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
3795 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3798 _mesa_lock_texture(ctx
, texObj
); /* relock */
3800 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3802 _mesa_meta_end(ctx
);
3804 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3806 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3808 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3813 * Determine the GL data type to use for the temporary image read with
3814 * ReadPixels() and passed to Tex[Sub]Image().
3817 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3821 baseFormat
= _mesa_get_format_base_format(format
);
3823 switch (baseFormat
) {
3830 case GL_LUMINANCE_ALPHA
:
3832 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3833 return GL_UNSIGNED_BYTE
;
3834 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3835 return GL_UNSIGNED_SHORT
;
3837 GLenum datatype
= _mesa_get_format_datatype(format
);
3838 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3842 case GL_DEPTH_COMPONENT
: {
3843 GLenum datatype
= _mesa_get_format_datatype(format
);
3844 if (datatype
== GL_FLOAT
)
3847 return GL_UNSIGNED_INT
;
3849 case GL_DEPTH_STENCIL
: {
3850 GLenum datatype
= _mesa_get_format_datatype(format
);
3851 if (datatype
== GL_FLOAT
)
3852 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3854 return GL_UNSIGNED_INT_24_8
;
3857 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3865 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3866 * Have to be careful with locking and meta state for pixel transfer.
3869 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3870 struct gl_texture_image
*texImage
,
3871 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3872 struct gl_renderbuffer
*rb
,
3874 GLsizei width
, GLsizei height
)
3876 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3877 GLenum format
, type
;
3881 /* Choose format/type for temporary image buffer */
3882 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3883 if (format
== GL_LUMINANCE
||
3884 format
== GL_LUMINANCE_ALPHA
||
3885 format
== GL_INTENSITY
) {
3886 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3887 * temp image buffer because glReadPixels will do L=R+G+B which is
3888 * not what we want (should be L=R).
3893 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3894 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3895 format
= _mesa_base_format_to_integer_format(format
);
3897 bpp
= _mesa_bytes_per_pixel(format
, type
);
3899 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3904 * Alloc image buffer (XXX could use a PBO)
3906 buf
= malloc(width
* height
* bpp
);
3908 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3912 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3915 * Read image from framebuffer (disable pixel transfer ops)
3917 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3918 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3919 format
, type
, &ctx
->Pack
, buf
);
3920 _mesa_meta_end(ctx
);
3922 _mesa_update_state(ctx
); /* to update pixel transfer state */
3925 * Store texture data (with pixel transfer ops)
3927 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3929 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3930 assert(yoffset
== 0);
3931 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3932 xoffset
, zoffset
, 0, width
, 1, 1,
3933 format
, type
, buf
, &ctx
->Unpack
);
3935 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3936 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3937 format
, type
, buf
, &ctx
->Unpack
);
3940 _mesa_meta_end(ctx
);
3942 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3949 * Decompress a texture image by drawing a quad with the compressed
3950 * texture and reading the pixels out of the color buffer.
3951 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3952 * \param destFormat format, ala glReadPixels
3953 * \param destType type, ala glReadPixels
3954 * \param dest destination buffer
3955 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3958 decompress_texture_image(struct gl_context
*ctx
,
3959 struct gl_texture_image
*texImage
,
3961 GLenum destFormat
, GLenum destType
,
3964 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3965 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3966 const GLint width
= texImage
->Width
;
3967 const GLint height
= texImage
->Height
;
3968 const GLint depth
= texImage
->Height
;
3969 const GLenum target
= texObj
->Target
;
3972 GLfloat x
, y
, tex
[3];
3974 struct vertex verts
[4];
3975 GLuint fboDrawSave
, fboReadSave
;
3980 assert(target
== GL_TEXTURE_3D
||
3981 target
== GL_TEXTURE_2D_ARRAY
);
3984 if (target
== GL_TEXTURE_CUBE_MAP
) {
3985 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3988 faceTarget
= target
;
3991 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3992 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3993 fboReadSave
= ctx
->ReadBuffer
->Name
;
3994 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3996 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3998 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3999 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
4001 /* Create/bind FBO/renderbuffer */
4002 if (decompress
->FBO
== 0) {
4003 _mesa_GenFramebuffers(1, &decompress
->FBO
);
4004 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
4005 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4006 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4007 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
4008 GL_COLOR_ATTACHMENT0_EXT
,
4009 GL_RENDERBUFFER_EXT
,
4013 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4016 /* alloc dest surface */
4017 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
4018 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4019 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
4021 decompress
->Width
= width
;
4022 decompress
->Height
= height
;
4025 /* setup VBO data */
4026 if (decompress
->ArrayObj
== 0) {
4027 /* create vertex array object */
4028 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
4029 _mesa_BindVertexArray(decompress
->ArrayObj
);
4031 /* create vertex array buffer */
4032 _mesa_GenBuffers(1, &decompress
->VBO
);
4033 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4034 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4035 NULL
, GL_DYNAMIC_DRAW_ARB
);
4037 /* setup vertex arrays */
4038 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4039 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
4040 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4041 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4044 _mesa_BindVertexArray(decompress
->ArrayObj
);
4045 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4048 if (!decompress
->Sampler
) {
4049 _mesa_GenSamplers(1, &decompress
->Sampler
);
4050 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4051 /* nearest filtering */
4052 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4053 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4054 /* No sRGB decode or encode.*/
4055 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
4056 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
4057 GL_SKIP_DECODE_EXT
);
4061 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4064 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
4070 /* setup vertex positions */
4076 verts
[2].y
= height
;
4078 verts
[3].y
= height
;
4080 _mesa_MatrixMode(GL_PROJECTION
);
4081 _mesa_LoadIdentity();
4082 _mesa_Ortho(0.0, width
, 0.0, height
, -1.0, 1.0);
4083 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
4085 /* upload new vertex data */
4086 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4088 /* setup texture state */
4089 _mesa_BindTexture(target
, texObj
->Name
);
4090 _mesa_set_enable(ctx
, target
, GL_TRUE
);
4093 /* save texture object state */
4094 const GLint baseLevelSave
= texObj
->BaseLevel
;
4095 const GLint maxLevelSave
= texObj
->MaxLevel
;
4097 /* restrict sampling to the texture level of interest */
4098 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4099 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
4100 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
4103 /* render quad w/ texture into renderbuffer */
4104 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4106 /* Restore texture object state, the texture binding will
4107 * be restored by _mesa_meta_end().
4109 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4110 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
4111 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
4116 /* read pixels from renderbuffer */
4118 GLenum baseTexFormat
= texImage
->_BaseFormat
;
4119 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
4121 /* The pixel transfer state will be set to default values at this point
4122 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
4123 * turned off (as required by glGetTexImage) but we need to handle some
4124 * special cases. In particular, single-channel texture values are
4125 * returned as red and two-channel texture values are returned as
4128 if ((baseTexFormat
== GL_LUMINANCE
||
4129 baseTexFormat
== GL_LUMINANCE_ALPHA
||
4130 baseTexFormat
== GL_INTENSITY
) ||
4131 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
4132 * luminance then we need to return L=tex(R).
4134 ((baseTexFormat
== GL_RGBA
||
4135 baseTexFormat
== GL_RGB
||
4136 baseTexFormat
== GL_RG
) &&
4137 (destBaseFormat
== GL_LUMINANCE
||
4138 destBaseFormat
== GL_LUMINANCE_ALPHA
||
4139 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
4140 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
4141 /* Green and blue must be zero */
4142 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
4143 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
4146 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
4149 /* disable texture unit */
4150 _mesa_set_enable(ctx
, target
, GL_FALSE
);
4152 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
4154 _mesa_meta_end(ctx
);
4156 /* restore fbo bindings */
4157 if (fboDrawSave
== fboReadSave
) {
4158 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
4161 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
4162 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
4164 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
4169 * This is just a wrapper around _mesa_get_tex_image() and
4170 * decompress_texture_image(). Meta functions should not be directly called
4174 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
4175 GLenum format
, GLenum type
, GLvoid
*pixels
,
4176 struct gl_texture_image
*texImage
)
4178 /* We can only use the decompress-with-blit method here if the texels are
4179 * unsigned, normalized values. We could handle signed and unnormalized
4180 * with floating point renderbuffers...
4182 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
4183 _mesa_get_format_datatype(texImage
->TexFormat
)
4184 == GL_UNSIGNED_NORMALIZED
) {
4185 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4187 /* Need to unlock the texture here to prevent deadlock... */
4188 _mesa_unlock_texture(ctx
, texObj
);
4189 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
4191 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
4192 /* Setup pixel packing. SkipPixels and SkipRows will be applied
4193 * in the decompress_texture_image() function's call to
4194 * glReadPixels but we need to compute the dest slice's address
4195 * here (according to SkipImages and ImageHeight).
4197 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
4198 packing
.SkipPixels
= 0;
4199 packing
.SkipRows
= 0;
4200 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
4201 texImage
->Height
, format
, type
,
4207 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
4209 /* ... and relock it */
4210 _mesa_lock_texture(ctx
, texObj
);
4213 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
4219 * Meta implementation of ctx->Driver.DrawTex() in terms
4220 * of polygon rendering.
4223 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
4224 GLfloat width
, GLfloat height
)
4226 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
4228 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
4230 struct vertex verts
[4];
4233 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
4235 MESA_META_TRANSFORM
|
4237 MESA_META_VIEWPORT
));
4239 if (drawtex
->ArrayObj
== 0) {
4240 /* one-time setup */
4241 GLint active_texture
;
4243 /* create vertex array object */
4244 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
4245 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4247 /* create vertex array buffer */
4248 _mesa_GenBuffers(1, &drawtex
->VBO
);
4249 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4250 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4251 NULL
, GL_DYNAMIC_DRAW_ARB
);
4253 /* client active texture is not part of the array object */
4254 active_texture
= ctx
->Array
.ActiveTexture
;
4256 /* setup vertex arrays */
4257 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4258 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4259 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4260 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
4261 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
4262 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4265 /* restore client active texture */
4266 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
4269 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4270 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4273 /* vertex positions, texcoords */
4275 const GLfloat x1
= x
+ width
;
4276 const GLfloat y1
= y
+ height
;
4278 z
= CLAMP(z
, 0.0f
, 1.0f
);
4297 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4298 const struct gl_texture_object
*texObj
;
4299 const struct gl_texture_image
*texImage
;
4300 GLfloat s
, t
, s1
, t1
;
4303 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
4305 for (j
= 0; j
< 4; j
++) {
4306 verts
[j
].st
[i
][0] = 0.0f
;
4307 verts
[j
].st
[i
][1] = 0.0f
;
4312 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
4313 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
4314 tw
= texImage
->Width2
;
4315 th
= texImage
->Height2
;
4317 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
4318 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
4319 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
4320 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
4322 verts
[0].st
[i
][0] = s
;
4323 verts
[0].st
[i
][1] = t
;
4325 verts
[1].st
[i
][0] = s1
;
4326 verts
[1].st
[i
][1] = t
;
4328 verts
[2].st
[i
][0] = s1
;
4329 verts
[2].st
[i
][1] = t1
;
4331 verts
[3].st
[i
][0] = s
;
4332 verts
[3].st
[i
][1] = t1
;
4335 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4338 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4340 _mesa_meta_end(ctx
);