2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/queryobj.h"
58 #include "main/readpix.h"
59 #include "main/scissor.h"
60 #include "main/shaderapi.h"
61 #include "main/shaderobj.h"
62 #include "main/state.h"
63 #include "main/stencil.h"
64 #include "main/texobj.h"
65 #include "main/texenv.h"
66 #include "main/texgetimage.h"
67 #include "main/teximage.h"
68 #include "main/texparam.h"
69 #include "main/texstate.h"
70 #include "main/transformfeedback.h"
71 #include "main/uniforms.h"
72 #include "main/varray.h"
73 #include "main/viewport.h"
74 #include "main/samplerobj.h"
75 #include "program/program.h"
76 #include "swrast/swrast.h"
77 #include "drivers/common/meta.h"
78 #include "main/enums.h"
79 #include "main/glformats.h"
80 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_CLEAR (and others?) */
94 struct gl_query_object
*CurrentOcclusionObject
;
96 /** MESA_META_ALPHA_TEST */
97 GLboolean AlphaEnabled
;
101 /** MESA_META_BLEND */
102 GLbitfield BlendEnabled
;
103 GLboolean ColorLogicOpEnabled
;
105 /** MESA_META_COLOR_MASK */
106 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
108 /** MESA_META_DEPTH_TEST */
109 struct gl_depthbuffer_attrib Depth
;
114 /** MESA_META_PIXEL_STORE */
115 struct gl_pixelstore_attrib Pack
, Unpack
;
117 /** MESA_META_PIXEL_TRANSFER */
118 GLfloat RedBias
, RedScale
;
119 GLfloat GreenBias
, GreenScale
;
120 GLfloat BlueBias
, BlueScale
;
121 GLfloat AlphaBias
, AlphaScale
;
122 GLfloat DepthBias
, DepthScale
;
123 GLboolean MapColorFlag
;
125 /** MESA_META_RASTERIZATION */
126 GLenum FrontPolygonMode
, BackPolygonMode
;
127 GLboolean PolygonOffset
;
128 GLboolean PolygonSmooth
;
129 GLboolean PolygonStipple
;
130 GLboolean PolygonCull
;
132 /** MESA_META_SCISSOR */
133 struct gl_scissor_attrib Scissor
;
135 /** MESA_META_SHADER */
136 GLboolean VertexProgramEnabled
;
137 struct gl_vertex_program
*VertexProgram
;
138 GLboolean FragmentProgramEnabled
;
139 struct gl_fragment_program
*FragmentProgram
;
140 GLboolean ATIFragmentShaderEnabled
;
141 struct gl_shader_program
*Shader
[MESA_SHADER_STAGES
];
142 struct gl_shader_program
*ActiveShader
;
144 /** MESA_META_STENCIL_TEST */
145 struct gl_stencil_attrib Stencil
;
147 /** MESA_META_TRANSFORM */
149 GLfloat ModelviewMatrix
[16];
150 GLfloat ProjectionMatrix
[16];
151 GLfloat TextureMatrix
[16];
153 /** MESA_META_CLIP */
154 GLbitfield ClipPlanesEnabled
;
156 /** MESA_META_TEXTURE */
158 GLuint ClientActiveUnit
;
159 /** for unit[0] only */
160 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
161 /** mask of TEXTURE_2D_BIT, etc */
162 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
163 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
164 GLuint EnvMode
; /* unit[0] only */
166 /** MESA_META_VERTEX */
167 struct gl_array_object
*ArrayObj
;
168 struct gl_buffer_object
*ArrayBufferObj
;
170 /** MESA_META_VIEWPORT */
171 GLfloat ViewportX
, ViewportY
, ViewportW
, ViewportH
;
172 GLclampd DepthNear
, DepthFar
;
174 /** MESA_META_CLAMP_FRAGMENT_COLOR */
175 GLenum ClampFragmentColor
;
177 /** MESA_META_CLAMP_VERTEX_COLOR */
178 GLenum ClampVertexColor
;
180 /** MESA_META_CONDITIONAL_RENDER */
181 struct gl_query_object
*CondRenderQuery
;
182 GLenum CondRenderMode
;
184 /** MESA_META_SELECT_FEEDBACK */
186 struct gl_selection Select
;
187 struct gl_feedback Feedback
;
189 /** MESA_META_MULTISAMPLE */
190 GLboolean MultisampleEnabled
;
192 /** MESA_META_FRAMEBUFFER_SRGB */
193 GLboolean sRGBEnabled
;
195 /** Miscellaneous (always disabled) */
197 GLboolean RasterDiscard
;
198 GLboolean TransformFeedbackNeedsResume
;
202 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
203 * This is currently shared by all the meta ops. But we could create a
204 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
209 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
210 GLsizei MinSize
; /**< Min texture size to allocate */
211 GLsizei MaxSize
; /**< Max possible texture size */
212 GLboolean NPOT
; /**< Non-power of two size OK? */
213 GLsizei Width
, Height
; /**< Current texture size */
215 GLfloat Sright
, Ttop
; /**< right, top texcoords */
220 * State for glBlitFramebufer()
228 GLuint RectShaderProg
;
229 struct temp_texture depthTex
;
234 * State for glClear()
244 GLuint IntegerShaderProg
;
245 GLint IntegerColorLocation
;
246 GLint IntegerLayerLocation
;
251 * State for glCopyPixels()
261 * State for glDrawPixels()
267 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
268 GLuint DepthFP
; /**< Fragment program for drawing depth images */
273 * State for glBitmap()
279 struct temp_texture Tex
; /**< separate texture from other meta ops */
283 * State for GLSL texture sampler which is used to generate fragment
284 * shader in _mesa_meta_generate_mipmap().
286 struct glsl_sampler
{
289 const char *texcoords
;
294 * State for _mesa_meta_generate_mipmap()
296 struct gen_mipmap_state
303 struct glsl_sampler sampler_1d
;
304 struct glsl_sampler sampler_2d
;
305 struct glsl_sampler sampler_3d
;
306 struct glsl_sampler sampler_cubemap
;
307 struct glsl_sampler sampler_1d_array
;
308 struct glsl_sampler sampler_2d_array
;
312 * State for texture decompression
314 struct decompress_state
317 GLuint VBO
, FBO
, RBO
, Sampler
;
322 * State for glDrawTex()
330 #define MAX_META_OPS_DEPTH 8
332 * All per-context meta state.
336 /** Stack of state saved during meta-ops */
337 struct save_state Save
[MAX_META_OPS_DEPTH
];
338 /** Save stack depth */
339 GLuint SaveStackDepth
;
341 struct temp_texture TempTex
;
343 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
344 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
345 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
346 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
347 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
348 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
349 struct decompress_state Decompress
; /**< For texture decompression */
350 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
353 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
354 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
355 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
356 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
357 struct gen_mipmap_state
*mipmap
);
360 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
366 shader
= _mesa_CreateShaderObjectARB(target
);
367 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
368 _mesa_CompileShader(shader
);
370 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
374 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
376 _mesa_DeleteObjectARB(shader
);
382 _mesa_DeleteObjectARB(shader
);
386 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
388 "meta program compile failed:\n%s\n"
393 _mesa_DeleteObjectARB(shader
);
399 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
404 _mesa_LinkProgram(program
);
406 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
410 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
418 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
419 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
427 * Initialize meta-ops for a context.
428 * To be called once during context creation.
431 _mesa_meta_init(struct gl_context
*ctx
)
435 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
440 * Free context meta-op state.
441 * To be called once during context destruction.
444 _mesa_meta_free(struct gl_context
*ctx
)
446 GET_CURRENT_CONTEXT(old_context
);
447 _mesa_make_current(ctx
, NULL
, NULL
);
448 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
449 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
450 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
451 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
453 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
455 _mesa_make_current(NULL
, NULL
, NULL
);
462 * Enter meta state. This is like a light-weight version of glPushAttrib
463 * but it also resets most GL state back to default values.
465 * \param state bitmask of MESA_META_* flags indicating which attribute groups
466 * to save and reset to their defaults
469 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
471 struct save_state
*save
;
473 /* hope MAX_META_OPS_DEPTH is large enough */
474 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
476 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
477 memset(save
, 0, sizeof(*save
));
478 save
->SavedState
= state
;
480 /* Pausing transform feedback needs to be done early, or else we won't be
481 * able to change other state.
483 save
->TransformFeedbackNeedsResume
=
484 _mesa_is_xfb_active_and_unpaused(ctx
);
485 if (save
->TransformFeedbackNeedsResume
)
486 _mesa_PauseTransformFeedback();
488 /* After saving the current occlusion object, call EndQuery so that no
489 * occlusion querying will be active during the meta-operation.
491 if (state
& MESA_META_OCCLUSION_QUERY
) {
492 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
493 if (save
->CurrentOcclusionObject
)
494 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
497 if (state
& MESA_META_ALPHA_TEST
) {
498 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
499 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
500 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
501 if (ctx
->Color
.AlphaEnabled
)
502 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
505 if (state
& MESA_META_BLEND
) {
506 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
507 if (ctx
->Color
.BlendEnabled
) {
508 if (ctx
->Extensions
.EXT_draw_buffers2
) {
510 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
511 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
515 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
518 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
519 if (ctx
->Color
.ColorLogicOpEnabled
)
520 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
523 if (state
& MESA_META_COLOR_MASK
) {
524 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
525 sizeof(ctx
->Color
.ColorMask
));
526 if (!ctx
->Color
.ColorMask
[0][0] ||
527 !ctx
->Color
.ColorMask
[0][1] ||
528 !ctx
->Color
.ColorMask
[0][2] ||
529 !ctx
->Color
.ColorMask
[0][3])
530 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
533 if (state
& MESA_META_DEPTH_TEST
) {
534 save
->Depth
= ctx
->Depth
; /* struct copy */
536 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
539 if ((state
& MESA_META_FOG
)
540 && ctx
->API
!= API_OPENGL_CORE
541 && ctx
->API
!= API_OPENGLES2
) {
542 save
->Fog
= ctx
->Fog
.Enabled
;
543 if (ctx
->Fog
.Enabled
)
544 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
547 if (state
& MESA_META_PIXEL_STORE
) {
548 save
->Pack
= ctx
->Pack
;
549 save
->Unpack
= ctx
->Unpack
;
550 ctx
->Pack
= ctx
->DefaultPacking
;
551 ctx
->Unpack
= ctx
->DefaultPacking
;
554 if (state
& MESA_META_PIXEL_TRANSFER
) {
555 save
->RedScale
= ctx
->Pixel
.RedScale
;
556 save
->RedBias
= ctx
->Pixel
.RedBias
;
557 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
558 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
559 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
560 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
561 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
562 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
563 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
564 ctx
->Pixel
.RedScale
= 1.0F
;
565 ctx
->Pixel
.RedBias
= 0.0F
;
566 ctx
->Pixel
.GreenScale
= 1.0F
;
567 ctx
->Pixel
.GreenBias
= 0.0F
;
568 ctx
->Pixel
.BlueScale
= 1.0F
;
569 ctx
->Pixel
.BlueBias
= 0.0F
;
570 ctx
->Pixel
.AlphaScale
= 1.0F
;
571 ctx
->Pixel
.AlphaBias
= 0.0F
;
572 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
574 ctx
->NewState
|=_NEW_PIXEL
;
577 if (state
& MESA_META_RASTERIZATION
) {
578 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
579 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
580 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
581 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
582 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
583 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
584 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
585 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
586 if (ctx
->API
== API_OPENGL_COMPAT
) {
587 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
588 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
590 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
593 if (state
& MESA_META_SCISSOR
) {
594 save
->Scissor
= ctx
->Scissor
; /* struct copy */
595 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
598 if (state
& MESA_META_SHADER
) {
599 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
600 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
601 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
602 ctx
->VertexProgram
.Current
);
603 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
606 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
607 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
608 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
609 ctx
->FragmentProgram
.Current
);
610 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
613 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
614 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
615 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
618 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
619 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
620 ctx
->Shader
.CurrentProgram
[i
]);
622 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
623 ctx
->Shader
.ActiveProgram
);
628 if (state
& MESA_META_STENCIL_TEST
) {
629 save
->Stencil
= ctx
->Stencil
; /* struct copy */
630 if (ctx
->Stencil
.Enabled
)
631 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
632 /* NOTE: other stencil state not reset */
635 if (state
& MESA_META_TEXTURE
) {
638 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
639 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
640 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
642 /* Disable all texture units */
643 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
644 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
645 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
646 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
647 if (ctx
->Texture
.Unit
[u
].Enabled
||
648 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
649 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
650 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
651 if (ctx
->Extensions
.ARB_texture_cube_map
)
652 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
653 if (_mesa_is_gles(ctx
) &&
654 ctx
->Extensions
.OES_EGL_image_external
)
655 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
657 if (ctx
->API
== API_OPENGL_COMPAT
) {
658 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
659 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
660 if (ctx
->Extensions
.NV_texture_rectangle
)
661 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
662 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
663 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
664 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
665 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
667 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
673 /* save current texture objects for unit[0] only */
674 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
675 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
676 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
679 /* set defaults for unit[0] */
680 _mesa_ActiveTexture(GL_TEXTURE0
);
681 _mesa_ClientActiveTexture(GL_TEXTURE0
);
682 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
683 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
687 if (state
& MESA_META_TRANSFORM
) {
688 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
689 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
690 16 * sizeof(GLfloat
));
691 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
692 16 * sizeof(GLfloat
));
693 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
694 16 * sizeof(GLfloat
));
695 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
696 /* set 1:1 vertex:pixel coordinate transform */
697 _mesa_ActiveTexture(GL_TEXTURE0
);
698 _mesa_MatrixMode(GL_TEXTURE
);
699 _mesa_LoadIdentity();
700 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
701 _mesa_MatrixMode(GL_MODELVIEW
);
702 _mesa_LoadIdentity();
703 _mesa_MatrixMode(GL_PROJECTION
);
704 _mesa_LoadIdentity();
706 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
707 * This can occur when there is no draw buffer.
709 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
710 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
711 0.0, ctx
->DrawBuffer
->Height
,
715 if (state
& MESA_META_CLIP
) {
716 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
717 if (ctx
->Transform
.ClipPlanesEnabled
) {
719 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
720 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
725 if (state
& MESA_META_VERTEX
) {
726 /* save vertex array object state */
727 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
728 ctx
->Array
.ArrayObj
);
729 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
730 ctx
->Array
.ArrayBufferObj
);
731 /* set some default state? */
734 if (state
& MESA_META_VIEWPORT
) {
735 /* save viewport state */
736 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
737 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
738 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
739 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
740 /* set viewport to match window size */
741 if (ctx
->ViewportArray
[0].X
!= 0 ||
742 ctx
->ViewportArray
[0].Y
!= 0 ||
743 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
744 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
745 _mesa_set_viewport(ctx
, 0, 0, 0,
746 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
748 /* save depth range state */
749 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
750 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
751 /* set depth range to default */
752 _mesa_DepthRange(0.0, 1.0);
755 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
756 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
758 /* Generally in here we want to do clamping according to whether
759 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
760 * regardless of the internal implementation of the metaops.
762 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
763 ctx
->Extensions
.ARB_color_buffer_float
)
764 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
767 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
768 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
770 /* Generally in here we never want vertex color clamping --
771 * result clamping is only dependent on fragment clamping.
773 if (ctx
->Extensions
.ARB_color_buffer_float
)
774 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
777 if (state
& MESA_META_CONDITIONAL_RENDER
) {
778 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
779 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
781 if (ctx
->Query
.CondRenderQuery
)
782 _mesa_EndConditionalRender();
785 if (state
& MESA_META_SELECT_FEEDBACK
) {
786 save
->RenderMode
= ctx
->RenderMode
;
787 if (ctx
->RenderMode
== GL_SELECT
) {
788 save
->Select
= ctx
->Select
; /* struct copy */
789 _mesa_RenderMode(GL_RENDER
);
790 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
791 save
->Feedback
= ctx
->Feedback
; /* struct copy */
792 _mesa_RenderMode(GL_RENDER
);
796 if (state
& MESA_META_MULTISAMPLE
) {
797 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
798 if (ctx
->Multisample
.Enabled
)
799 _mesa_set_multisample(ctx
, GL_FALSE
);
802 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
803 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
804 if (ctx
->Color
.sRGBEnabled
)
805 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
810 save
->Lighting
= ctx
->Light
.Enabled
;
811 if (ctx
->Light
.Enabled
)
812 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
813 save
->RasterDiscard
= ctx
->RasterDiscard
;
814 if (ctx
->RasterDiscard
)
815 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
821 * Leave meta state. This is like a light-weight version of glPopAttrib().
824 _mesa_meta_end(struct gl_context
*ctx
)
826 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
827 const GLbitfield state
= save
->SavedState
;
830 /* After starting a new occlusion query, initialize the results to the
831 * values saved previously. The driver will then continue to increment
834 if (state
& MESA_META_OCCLUSION_QUERY
) {
835 if (save
->CurrentOcclusionObject
) {
836 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
837 save
->CurrentOcclusionObject
->Id
);
838 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
842 if (state
& MESA_META_ALPHA_TEST
) {
843 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
844 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
845 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
848 if (state
& MESA_META_BLEND
) {
849 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
850 if (ctx
->Extensions
.EXT_draw_buffers2
) {
852 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
853 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
857 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
860 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
861 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
864 if (state
& MESA_META_COLOR_MASK
) {
866 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
867 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
869 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
870 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
874 save
->ColorMask
[i
][0],
875 save
->ColorMask
[i
][1],
876 save
->ColorMask
[i
][2],
877 save
->ColorMask
[i
][3]);
883 if (state
& MESA_META_DEPTH_TEST
) {
884 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
885 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
886 _mesa_DepthFunc(save
->Depth
.Func
);
887 _mesa_DepthMask(save
->Depth
.Mask
);
890 if ((state
& MESA_META_FOG
)
891 && ctx
->API
!= API_OPENGL_CORE
892 && ctx
->API
!= API_OPENGLES2
) {
893 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
896 if (state
& MESA_META_PIXEL_STORE
) {
897 ctx
->Pack
= save
->Pack
;
898 ctx
->Unpack
= save
->Unpack
;
901 if (state
& MESA_META_PIXEL_TRANSFER
) {
902 ctx
->Pixel
.RedScale
= save
->RedScale
;
903 ctx
->Pixel
.RedBias
= save
->RedBias
;
904 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
905 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
906 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
907 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
908 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
909 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
910 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
912 ctx
->NewState
|=_NEW_PIXEL
;
915 if (state
& MESA_META_RASTERIZATION
) {
916 /* Core context requires that front and back mode be the same.
918 if (ctx
->API
== API_OPENGL_CORE
) {
919 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
921 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
922 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
924 if (ctx
->API
== API_OPENGL_COMPAT
) {
925 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
926 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
928 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
929 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
932 if (state
& MESA_META_SCISSOR
) {
935 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
936 _mesa_set_scissor(ctx
, i
,
937 save
->Scissor
.ScissorArray
[i
].X
,
938 save
->Scissor
.ScissorArray
[i
].Y
,
939 save
->Scissor
.ScissorArray
[i
].Width
,
940 save
->Scissor
.ScissorArray
[i
].Height
);
941 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
942 (save
->Scissor
.EnableFlags
>> i
) & 1);
946 if (state
& MESA_META_SHADER
) {
947 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
948 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
949 save
->VertexProgramEnabled
);
950 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
951 save
->VertexProgram
);
952 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
955 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
956 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
957 save
->FragmentProgramEnabled
);
958 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
959 save
->FragmentProgram
);
960 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
963 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
964 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
965 save
->ATIFragmentShaderEnabled
);
968 if (ctx
->Extensions
.ARB_vertex_shader
) {
969 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
,
970 save
->Shader
[MESA_SHADER_VERTEX
]);
973 if (_mesa_has_geometry_shaders(ctx
))
974 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
975 save
->Shader
[MESA_SHADER_GEOMETRY
]);
977 if (ctx
->Extensions
.ARB_fragment_shader
)
978 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
979 save
->Shader
[MESA_SHADER_FRAGMENT
]);
981 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
984 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
985 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
986 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
989 if (state
& MESA_META_STENCIL_TEST
) {
990 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
992 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
993 _mesa_ClearStencil(stencil
->Clear
);
994 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
995 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
996 stencil
->TestTwoSide
);
997 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
998 ? GL_BACK
: GL_FRONT
);
1001 _mesa_StencilFuncSeparate(GL_FRONT
,
1002 stencil
->Function
[0],
1004 stencil
->ValueMask
[0]);
1005 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1006 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1007 stencil
->ZFailFunc
[0],
1008 stencil
->ZPassFunc
[0]);
1010 _mesa_StencilFuncSeparate(GL_BACK
,
1011 stencil
->Function
[1],
1013 stencil
->ValueMask
[1]);
1014 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1015 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1016 stencil
->ZFailFunc
[1],
1017 stencil
->ZPassFunc
[1]);
1020 if (state
& MESA_META_TEXTURE
) {
1023 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1025 /* restore texenv for unit[0] */
1026 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1027 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1030 /* restore texture objects for unit[0] only */
1031 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1032 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1033 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1034 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1035 save
->CurrentTexture
[tgt
]);
1037 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1040 /* Restore fixed function texture enables, texgen */
1041 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1042 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1043 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1044 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1045 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1048 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1049 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1050 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1055 /* restore current unit state */
1056 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1057 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1060 if (state
& MESA_META_TRANSFORM
) {
1061 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1062 _mesa_ActiveTexture(GL_TEXTURE0
);
1063 _mesa_MatrixMode(GL_TEXTURE
);
1064 _mesa_LoadMatrixf(save
->TextureMatrix
);
1065 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1067 _mesa_MatrixMode(GL_MODELVIEW
);
1068 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1070 _mesa_MatrixMode(GL_PROJECTION
);
1071 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1073 _mesa_MatrixMode(save
->MatrixMode
);
1076 if (state
& MESA_META_CLIP
) {
1077 if (save
->ClipPlanesEnabled
) {
1079 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1080 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1081 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1087 if (state
& MESA_META_VERTEX
) {
1088 /* restore vertex buffer object */
1089 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1090 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1092 /* restore vertex array object */
1093 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1094 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1097 if (state
& MESA_META_VIEWPORT
) {
1098 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1099 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1100 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1101 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1102 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1103 save
->ViewportW
, save
->ViewportH
);
1105 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1108 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1109 ctx
->Extensions
.ARB_color_buffer_float
) {
1110 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1113 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1114 ctx
->Extensions
.ARB_color_buffer_float
) {
1115 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1118 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1119 if (save
->CondRenderQuery
)
1120 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1121 save
->CondRenderMode
);
1124 if (state
& MESA_META_SELECT_FEEDBACK
) {
1125 if (save
->RenderMode
== GL_SELECT
) {
1126 _mesa_RenderMode(GL_SELECT
);
1127 ctx
->Select
= save
->Select
;
1128 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1129 _mesa_RenderMode(GL_FEEDBACK
);
1130 ctx
->Feedback
= save
->Feedback
;
1134 if (state
& MESA_META_MULTISAMPLE
) {
1135 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1136 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1139 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1140 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1141 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1145 if (save
->Lighting
) {
1146 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1148 if (save
->RasterDiscard
) {
1149 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1151 if (save
->TransformFeedbackNeedsResume
)
1152 _mesa_ResumeTransformFeedback();
1154 ctx
->Meta
->SaveStackDepth
--;
1159 * Determine whether Mesa is currently in a meta state.
1162 _mesa_meta_in_progress(struct gl_context
*ctx
)
1164 return ctx
->Meta
->SaveStackDepth
!= 0;
1169 * Convert Z from a normalized value in the range [0, 1] to an object-space
1170 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1171 * default/identity ortho projection results in the original Z value.
1172 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1173 * value comes from the clear value or raster position.
1175 static INLINE GLfloat
1176 invert_z(GLfloat normZ
)
1178 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1184 * One-time init for a temp_texture object.
1185 * Choose tex target, compute max tex size, etc.
1188 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1190 /* prefer texture rectangle */
1191 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1192 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1193 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1194 tex
->NPOT
= GL_TRUE
;
1197 /* use 2D texture, NPOT if possible */
1198 tex
->Target
= GL_TEXTURE_2D
;
1199 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1200 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1202 tex
->MinSize
= 16; /* 16 x 16 at least */
1203 assert(tex
->MaxSize
> 0);
1205 _mesa_GenTextures(1, &tex
->TexObj
);
1209 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1213 _mesa_DeleteTextures(1, &tex
->TexObj
);
1219 * Return pointer to temp_texture info for non-bitmap ops.
1220 * This does some one-time init if needed.
1222 static struct temp_texture
*
1223 get_temp_texture(struct gl_context
*ctx
)
1225 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1228 init_temp_texture(ctx
, tex
);
1236 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1237 * We use a separate texture for bitmaps to reduce texture
1238 * allocation/deallocation.
1240 static struct temp_texture
*
1241 get_bitmap_temp_texture(struct gl_context
*ctx
)
1243 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1246 init_temp_texture(ctx
, tex
);
1253 * Return pointer to depth temp_texture.
1254 * This does some one-time init if needed.
1256 static struct temp_texture
*
1257 get_temp_depth_texture(struct gl_context
*ctx
)
1259 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1262 init_temp_texture(ctx
, tex
);
1269 * Compute the width/height of texture needed to draw an image of the
1270 * given size. Return a flag indicating whether the current texture
1271 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1272 * allocated (glTexImage2D).
1273 * Also, compute s/t texcoords for drawing.
1275 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1278 alloc_texture(struct temp_texture
*tex
,
1279 GLsizei width
, GLsizei height
, GLenum intFormat
)
1281 GLboolean newTex
= GL_FALSE
;
1283 ASSERT(width
<= tex
->MaxSize
);
1284 ASSERT(height
<= tex
->MaxSize
);
1286 if (width
> tex
->Width
||
1287 height
> tex
->Height
||
1288 intFormat
!= tex
->IntFormat
) {
1289 /* alloc new texture (larger or different format) */
1292 /* use non-power of two size */
1293 tex
->Width
= MAX2(tex
->MinSize
, width
);
1294 tex
->Height
= MAX2(tex
->MinSize
, height
);
1297 /* find power of two size */
1299 w
= h
= tex
->MinSize
;
1308 tex
->IntFormat
= intFormat
;
1313 /* compute texcoords */
1314 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1315 tex
->Sright
= (GLfloat
) width
;
1316 tex
->Ttop
= (GLfloat
) height
;
1319 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1320 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1328 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1331 setup_copypix_texture(struct gl_context
*ctx
,
1332 struct temp_texture
*tex
,
1334 GLint srcX
, GLint srcY
,
1335 GLsizei width
, GLsizei height
, GLenum intFormat
,
1338 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1339 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1340 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1341 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1342 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1344 /* copy framebuffer image to texture */
1346 /* create new tex image */
1347 if (tex
->Width
== width
&& tex
->Height
== height
) {
1348 /* create new tex with framebuffer data */
1349 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1350 srcX
, srcY
, width
, height
, 0);
1353 /* create empty texture */
1354 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1355 tex
->Width
, tex
->Height
, 0,
1356 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1358 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1359 0, 0, srcX
, srcY
, width
, height
);
1363 /* replace existing tex image */
1364 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1365 0, 0, srcX
, srcY
, width
, height
);
1371 * Setup/load texture for glDrawPixels.
1374 setup_drawpix_texture(struct gl_context
*ctx
,
1375 struct temp_texture
*tex
,
1377 GLenum texIntFormat
,
1378 GLsizei width
, GLsizei height
,
1379 GLenum format
, GLenum type
,
1380 const GLvoid
*pixels
)
1382 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1383 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1384 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1385 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1386 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1388 /* copy pixel data to texture */
1390 /* create new tex image */
1391 if (tex
->Width
== width
&& tex
->Height
== height
) {
1392 /* create new tex and load image data */
1393 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1394 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1397 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1399 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1400 ctx
->Unpack
.BufferObj
);
1401 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1402 /* create empty texture */
1403 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1404 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1405 if (save_unpack_obj
!= NULL
)
1406 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1407 save_unpack_obj
->Name
);
1409 _mesa_TexSubImage2D(tex
->Target
, 0,
1410 0, 0, width
, height
, format
, type
, pixels
);
1414 /* replace existing tex image */
1415 _mesa_TexSubImage2D(tex
->Target
, 0,
1416 0, 0, width
, height
, format
, type
, pixels
);
1423 * One-time init for drawing depth pixels.
1426 init_blit_depth_pixels(struct gl_context
*ctx
)
1428 static const char *program
=
1430 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1433 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1434 struct temp_texture
*tex
= get_temp_texture(ctx
);
1435 const char *texTarget
;
1437 assert(blit
->DepthFP
== 0);
1439 /* replace %s with "RECT" or "2D" */
1440 assert(strlen(program
) + 4 < sizeof(program2
));
1441 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1445 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1447 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1448 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1449 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1450 strlen(program2
), (const GLubyte
*) program2
);
1454 setup_ff_blit_framebuffer(struct gl_context
*ctx
,
1455 struct blit_state
*blit
)
1460 struct vertex verts
[4];
1462 if (blit
->ArrayObj
== 0) {
1463 /* one-time setup */
1465 /* create vertex array object */
1466 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1467 _mesa_BindVertexArray(blit
->ArrayObj
);
1469 /* create vertex array buffer */
1470 _mesa_GenBuffers(1, &blit
->VBO
);
1471 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1472 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1473 NULL
, GL_DYNAMIC_DRAW_ARB
);
1475 /* setup vertex arrays */
1476 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1477 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1478 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1479 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1482 /* setup projection matrix */
1483 _mesa_MatrixMode(GL_PROJECTION
);
1484 _mesa_LoadIdentity();
1485 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1490 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1491 struct blit_state
*blit
,
1497 struct vertex verts
[4];
1498 const char *vs_source
;
1503 GLboolean texture_2d
= (target
== GL_TEXTURE_2D
);
1505 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1506 assert(_mesa_is_desktop_gl(ctx
) || texture_2d
);
1508 /* Check if already initialized */
1509 if (blit
->ArrayObj
== 0) {
1511 /* create vertex array object */
1512 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1513 _mesa_BindVertexArray(blit
->ArrayObj
);
1515 /* create vertex array buffer */
1516 _mesa_GenBuffers(1, &blit
->VBO
);
1517 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1518 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1519 NULL
, GL_DYNAMIC_DRAW_ARB
);
1521 /* setup vertex arrays */
1522 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
1523 sizeof(struct vertex
), OFFSET(x
));
1524 _mesa_VertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
,
1525 sizeof(struct vertex
), OFFSET(s
));
1527 _mesa_EnableVertexAttribArray(0);
1528 _mesa_EnableVertexAttribArray(1);
1531 /* Generate a relevant fragment shader program for the texture target */
1532 if ((target
== GL_TEXTURE_2D
&& blit
->ShaderProg
!= 0) ||
1533 (target
== GL_TEXTURE_RECTANGLE
&& blit
->RectShaderProg
!= 0)) {
1537 mem_ctx
= ralloc_context(NULL
);
1539 if (ctx
->Const
.GLSLVersion
< 130) {
1541 "attribute vec2 position;\n"
1542 "attribute vec2 textureCoords;\n"
1543 "varying vec2 texCoords;\n"
1546 " texCoords = textureCoords;\n"
1547 " gl_Position = vec4(position, 0.0, 1.0);\n"
1550 fs_source
= ralloc_asprintf(mem_ctx
,
1552 "precision highp float;\n"
1554 "uniform %s texSampler;\n"
1555 "varying vec2 texCoords;\n"
1558 " gl_FragColor = %s(texSampler, texCoords);\n"
1559 " gl_FragDepth = gl_FragColor.r;\n"
1561 texture_2d
? "sampler2D" : "sampler2DRect",
1562 texture_2d
? "texture2D" : "texture2DRect");
1565 vs_source
= ralloc_asprintf(mem_ctx
,
1567 "in vec2 position;\n"
1568 "in vec2 textureCoords;\n"
1569 "out vec2 texCoords;\n"
1572 " texCoords = textureCoords;\n"
1573 " gl_Position = vec4(position, 0.0, 1.0);\n"
1575 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1576 fs_source
= ralloc_asprintf(mem_ctx
,
1579 "precision highp float;\n"
1581 "uniform %s texSampler;\n"
1582 "in vec2 texCoords;\n"
1583 "out vec4 out_color;\n"
1587 " out_color = %s(texSampler, texCoords);\n"
1588 " gl_FragDepth = out_color.r;\n"
1590 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
1591 texture_2d
? "sampler2D" : "sampler2DRect",
1592 texture_2d
? "texture" : "texture2DRect");
1595 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
1596 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
1598 ShaderProg
= _mesa_CreateProgramObjectARB();
1599 _mesa_AttachShader(ShaderProg
, fs
);
1600 _mesa_DeleteObjectARB(fs
);
1601 _mesa_AttachShader(ShaderProg
, vs
);
1602 _mesa_DeleteObjectARB(vs
);
1603 _mesa_BindAttribLocation(ShaderProg
, 0, "position");
1604 _mesa_BindAttribLocation(ShaderProg
, 1, "texcoords");
1605 link_program_with_debug(ctx
, ShaderProg
);
1606 ralloc_free(mem_ctx
);
1608 blit
->ShaderProg
= ShaderProg
;
1610 blit
->RectShaderProg
= ShaderProg
;
1614 * Try to do a glBlitFramebuffer using no-copy texturing.
1615 * We can do this when the src renderbuffer is actually a texture.
1616 * But if the src buffer == dst buffer we cannot do this.
1618 * \return new buffer mask indicating the buffers left to blit using the
1622 blitframebuffer_texture(struct gl_context
*ctx
,
1623 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1624 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1625 GLbitfield mask
, GLenum filter
, GLint flipX
,
1626 GLint flipY
, GLboolean glsl_version
)
1628 if (mask
& GL_COLOR_BUFFER_BIT
) {
1629 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1630 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1631 const struct gl_renderbuffer_attachment
*drawAtt
;
1632 const struct gl_renderbuffer_attachment
*readAtt
=
1633 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1635 if (readAtt
&& readAtt
->Texture
) {
1636 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1637 const GLint dstX
= MIN2(dstX0
, dstX1
);
1638 const GLint dstY
= MIN2(dstY0
, dstY1
);
1639 const GLint dstW
= abs(dstX1
- dstX0
);
1640 const GLint dstH
= abs(dstY1
- dstY0
);
1641 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1642 const GLuint srcLevel
= readAtt
->TextureLevel
;
1643 const GLint baseLevelSave
= texObj
->BaseLevel
;
1644 const GLint maxLevelSave
= texObj
->MaxLevel
;
1645 const GLenum target
= texObj
->Target
;
1646 GLuint sampler
, samplerSave
=
1647 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1648 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1651 /* Iterate through all draw buffers */
1652 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1653 int idx
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1656 drawAtt
= &drawFb
->Attachment
[idx
];
1658 if (drawAtt
->Texture
== readAtt
->Texture
) {
1659 /* Can't use same texture as both the source and dest. We need
1660 * to handle overlapping blits and besides, some hw may not
1667 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1668 /* Can't handle other texture types at this time */
1672 /* Choose between glsl version and fixed function version of
1673 * BlitFramebuffer function.
1676 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1677 if (target
== GL_TEXTURE_2D
)
1678 _mesa_UseProgram(blit
->ShaderProg
);
1680 _mesa_UseProgram(blit
->RectShaderProg
);
1683 setup_ff_blit_framebuffer(ctx
, &ctx
->Meta
->Blit
);
1686 _mesa_BindVertexArray(blit
->ArrayObj
);
1687 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1689 _mesa_GenSamplers(1, &sampler
);
1690 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1693 printf("Blit from texture!\n");
1694 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1695 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1698 /* Prepare src texture state */
1699 _mesa_BindTexture(target
, texObj
->Name
);
1700 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1701 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1702 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1703 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1704 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1706 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1707 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1709 /* Always do our blits with no sRGB decode or encode. Note that
1710 * GL_FRAMEBUFFER_SRGB has already been disabled by
1711 * _mesa_meta_begin().
1713 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1714 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1715 GL_SKIP_DECODE_EXT
);
1718 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1719 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1720 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1723 /* Prepare vertex data (the VBO was previously created and bound) */
1728 struct vertex verts
[4];
1729 GLfloat s0
, t0
, s1
, t1
;
1731 if (target
== GL_TEXTURE_2D
) {
1732 const struct gl_texture_image
*texImage
1733 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1734 s0
= srcX0
/ (float) texImage
->Width
;
1735 s1
= srcX1
/ (float) texImage
->Width
;
1736 t0
= srcY0
/ (float) texImage
->Height
;
1737 t1
= srcY1
/ (float) texImage
->Height
;
1740 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1747 /* setup vertex positions */
1748 verts
[0].x
= -1.0F
* flipX
;
1749 verts
[0].y
= -1.0F
* flipY
;
1750 verts
[1].x
= 1.0F
* flipX
;
1751 verts
[1].y
= -1.0F
* flipY
;
1752 verts
[2].x
= 1.0F
* flipX
;
1753 verts
[2].y
= 1.0F
* flipY
;
1754 verts
[3].x
= -1.0F
* flipX
;
1755 verts
[3].y
= 1.0F
* flipY
;
1766 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1769 /* setup viewport */
1770 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1771 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1772 _mesa_DepthMask(GL_FALSE
);
1773 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1775 /* Restore texture object state, the texture binding will
1776 * be restored by _mesa_meta_end().
1778 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1779 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1780 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1783 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1784 _mesa_DeleteSamplers(1, &sampler
);
1786 /* Done with color buffer */
1787 mask
&= ~GL_COLOR_BUFFER_BIT
;
1796 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1797 * of texture mapping and polygon rendering.
1800 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1801 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1802 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1803 GLbitfield mask
, GLenum filter
)
1805 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1806 struct temp_texture
*tex
= get_temp_texture(ctx
);
1807 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1808 const GLsizei maxTexSize
= tex
->MaxSize
;
1809 const GLint srcX
= MIN2(srcX0
, srcX1
);
1810 const GLint srcY
= MIN2(srcY0
, srcY1
);
1811 const GLint srcW
= abs(srcX1
- srcX0
);
1812 const GLint srcH
= abs(srcY1
- srcY0
);
1813 const GLint dstX
= MIN2(dstX0
, dstX1
);
1814 const GLint dstY
= MIN2(dstY0
, dstY1
);
1815 const GLint dstW
= abs(dstX1
- dstX0
);
1816 const GLint dstH
= abs(dstY1
- dstY0
);
1817 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1818 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1819 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1820 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1821 const GLint flipX
= srcFlipX
* dstFlipX
;
1822 const GLint flipY
= srcFlipY
* dstFlipY
;
1827 struct vertex verts
[4];
1829 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1830 ctx
->Extensions
.ARB_fragment_shader
&&
1831 (ctx
->API
!= API_OPENGLES
);
1833 /* In addition to falling back if the blit size is larger than the maximum
1834 * texture size, fallback if the source is multisampled. This fallback can
1835 * be removed once Mesa gets support ARB_texture_multisample.
1837 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1838 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1839 /* XXX avoid this fallback */
1840 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1841 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1845 /* only scissor effects blit so save/clear all other relevant state */
1846 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1848 /* Try faster, direct texture approach first */
1849 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1850 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1851 dstFlipX
, dstFlipY
, use_glsl_version
);
1853 _mesa_meta_end(ctx
);
1857 /* Choose between glsl version and fixed function version of
1858 * BlitFramebuffer function.
1860 if (use_glsl_version
) {
1861 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1862 if (tex
->Target
== GL_TEXTURE_2D
)
1863 _mesa_UseProgram(blit
->ShaderProg
);
1865 _mesa_UseProgram(blit
->RectShaderProg
);
1868 setup_ff_blit_framebuffer(ctx
, blit
);
1871 _mesa_BindVertexArray(blit
->ArrayObj
);
1872 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1874 /* Continue with "normal" approach which involves copying the src rect
1875 * into a temporary texture and is "blitted" by drawing a textured quad.
1878 /* setup vertex positions */
1879 verts
[0].x
= -1.0F
* flipX
;
1880 verts
[0].y
= -1.0F
* flipY
;
1881 verts
[1].x
= 1.0F
* flipX
;
1882 verts
[1].y
= -1.0F
* flipY
;
1883 verts
[2].x
= 1.0F
* flipX
;
1884 verts
[2].y
= 1.0F
* flipY
;
1885 verts
[3].x
= -1.0F
* flipX
;
1886 verts
[3].y
= 1.0F
* flipY
;
1890 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1893 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1894 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1896 if (mask
& GL_COLOR_BUFFER_BIT
) {
1897 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1898 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1899 const GLenum rb_base_format
=
1900 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1902 /* Using the exact source rectangle to create the texture does incorrect
1903 * linear filtering along the edges. So, allocate the texture extended along
1904 * edges by one pixel in x, y directions.
1906 newTex
= alloc_texture(tex
, srcW
+ 2, srcH
+ 2, rb_base_format
);
1907 setup_copypix_texture(ctx
, tex
, newTex
,
1908 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1909 rb_base_format
, filter
);
1910 /* texcoords (after texture allocation!) */
1914 verts
[1].s
= tex
->Sright
- 1.0F
;
1916 verts
[2].s
= tex
->Sright
- 1.0F
;
1917 verts
[2].t
= tex
->Ttop
- 1.0F
;
1919 verts
[3].t
= tex
->Ttop
- 1.0F
;
1921 /* upload new vertex data */
1922 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1925 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1926 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1927 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1928 _mesa_DepthMask(GL_FALSE
);
1929 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1930 mask
&= ~GL_COLOR_BUFFER_BIT
;
1933 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1934 _mesa_is_desktop_gl(ctx
) &&
1935 ctx
->Extensions
.ARB_depth_texture
&&
1936 ctx
->Extensions
.ARB_fragment_program
) {
1938 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1942 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1943 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1944 GL_UNSIGNED_INT
, tmp
);
1945 setup_drawpix_texture(ctx
, depthTex
, newTex
, GL_DEPTH_COMPONENT
,
1946 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1947 GL_UNSIGNED_INT
, tmp
);
1949 /* texcoords (after texture allocation!) */
1953 verts
[1].s
= depthTex
->Sright
;
1955 verts
[2].s
= depthTex
->Sright
;
1956 verts
[2].t
= depthTex
->Ttop
;
1958 verts
[3].t
= depthTex
->Ttop
;
1960 /* upload new vertex data */
1961 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1965 init_blit_depth_pixels(ctx
);
1967 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1968 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1969 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1970 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1971 _mesa_DepthFunc(GL_ALWAYS
);
1972 _mesa_DepthMask(GL_TRUE
);
1974 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1975 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1976 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1977 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1983 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1984 /* XXX can't easily do stencil */
1987 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1988 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1990 _mesa_meta_end(ctx
);
1993 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1994 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1999 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
2001 if (blit
->ArrayObj
) {
2002 _mesa_DeleteVertexArrays(1, &blit
->ArrayObj
);
2004 _mesa_DeleteBuffers(1, &blit
->VBO
);
2007 if (blit
->DepthFP
) {
2008 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
2012 _mesa_DeleteObjectARB(blit
->ShaderProg
);
2013 blit
->ShaderProg
= 0;
2014 _mesa_DeleteObjectARB(blit
->RectShaderProg
);
2015 blit
->RectShaderProg
= 0;
2017 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
2018 blit
->depthTex
.TexObj
= 0;
2023 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2026 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2028 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2030 GLfloat x
, y
, z
, r
, g
, b
, a
;
2032 struct vertex verts
[4];
2033 /* save all state but scissor, pixel pack/unpack */
2034 GLbitfield metaSave
= (MESA_META_ALL
-
2036 MESA_META_PIXEL_STORE
-
2037 MESA_META_CONDITIONAL_RENDER
-
2038 MESA_META_FRAMEBUFFER_SRGB
);
2039 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2041 if (buffers
& BUFFER_BITS_COLOR
) {
2042 /* if clearing color buffers, don't save/restore colormask */
2043 metaSave
-= MESA_META_COLOR_MASK
;
2046 _mesa_meta_begin(ctx
, metaSave
);
2048 if (clear
->ArrayObj
== 0) {
2049 /* one-time setup */
2051 /* create vertex array object */
2052 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2053 _mesa_BindVertexArray(clear
->ArrayObj
);
2055 /* create vertex array buffer */
2056 _mesa_GenBuffers(1, &clear
->VBO
);
2057 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2059 /* setup vertex arrays */
2060 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2061 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2062 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2063 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2066 _mesa_BindVertexArray(clear
->ArrayObj
);
2067 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2070 /* GL_COLOR_BUFFER_BIT */
2071 if (buffers
& BUFFER_BITS_COLOR
) {
2072 /* leave colormask, glDrawBuffer state as-is */
2074 /* Clears never have the color clamped. */
2075 if (ctx
->Extensions
.ARB_color_buffer_float
)
2076 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2079 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2080 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2083 /* GL_DEPTH_BUFFER_BIT */
2084 if (buffers
& BUFFER_BIT_DEPTH
) {
2085 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2086 _mesa_DepthFunc(GL_ALWAYS
);
2087 _mesa_DepthMask(GL_TRUE
);
2090 assert(!ctx
->Depth
.Test
);
2093 /* GL_STENCIL_BUFFER_BIT */
2094 if (buffers
& BUFFER_BIT_STENCIL
) {
2095 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2096 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2097 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2098 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2099 ctx
->Stencil
.Clear
& stencilMax
,
2100 ctx
->Stencil
.WriteMask
[0]);
2103 assert(!ctx
->Stencil
.Enabled
);
2106 /* vertex positions/colors */
2108 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
2109 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
2110 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
2111 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
2112 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
2129 for (i
= 0; i
< 4; i
++) {
2130 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
2131 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
2132 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
2133 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
2136 /* upload new vertex data */
2137 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2138 GL_DYNAMIC_DRAW_ARB
);
2142 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2144 _mesa_meta_end(ctx
);
2148 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
2150 const char *vs_source
=
2151 "attribute vec4 position;\n"
2154 " gl_Position = position;\n"
2156 const char *gs_source
=
2158 "layout(triangles) in;\n"
2159 "layout(triangle_strip, max_vertices = 4) out;\n"
2160 "uniform int layer;\n"
2163 " for (int i = 0; i < 3; i++) {\n"
2164 " gl_Layer = layer;\n"
2165 " gl_Position = gl_in[i].gl_Position;\n"
2169 const char *fs_source
=
2171 "precision highp float;\n"
2173 "uniform vec4 color;\n"
2176 " gl_FragColor = color;\n"
2178 GLuint vs
, gs
= 0, fs
;
2179 bool has_integer_textures
;
2181 if (clear
->ArrayObj
!= 0)
2184 /* create vertex array object */
2185 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2186 _mesa_BindVertexArray(clear
->ArrayObj
);
2188 /* create vertex array buffer */
2189 _mesa_GenBuffers(1, &clear
->VBO
);
2190 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2192 /* setup vertex arrays */
2193 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
2194 _mesa_EnableVertexAttribArray(0);
2196 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
2197 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
2198 _mesa_CompileShader(vs
);
2200 if (_mesa_has_geometry_shaders(ctx
)) {
2201 gs
= _mesa_CreateShaderObjectARB(GL_GEOMETRY_SHADER
);
2202 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
2203 _mesa_CompileShader(gs
);
2206 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
2207 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
2208 _mesa_CompileShader(fs
);
2210 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
2211 _mesa_AttachShader(clear
->ShaderProg
, fs
);
2212 _mesa_DeleteObjectARB(fs
);
2214 _mesa_AttachShader(clear
->ShaderProg
, gs
);
2215 _mesa_AttachShader(clear
->ShaderProg
, vs
);
2216 _mesa_DeleteObjectARB(vs
);
2217 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
2218 _mesa_LinkProgram(clear
->ShaderProg
);
2220 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2223 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2227 has_integer_textures
= _mesa_is_gles3(ctx
) ||
2228 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
2230 if (has_integer_textures
) {
2231 void *shader_source_mem_ctx
= ralloc_context(NULL
);
2232 const char *vs_int_source
=
2233 ralloc_asprintf(shader_source_mem_ctx
,
2235 "in vec4 position;\n"
2238 " gl_Position = position;\n"
2240 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2241 const char *fs_int_source
=
2242 ralloc_asprintf(shader_source_mem_ctx
,
2245 "precision highp float;\n"
2247 "uniform ivec4 color;\n"
2248 "out ivec4 out_color;\n"
2252 " out_color = color;\n"
2254 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2256 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
2257 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
2258 ralloc_free(shader_source_mem_ctx
);
2260 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
2261 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
2262 _mesa_DeleteObjectARB(fs
);
2264 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
2265 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
2266 _mesa_DeleteObjectARB(vs
);
2267 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
2269 /* Note that user-defined out attributes get automatically assigned
2270 * locations starting from 0, so we don't need to explicitly
2271 * BindFragDataLocation to 0.
2274 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
2276 clear
->IntegerColorLocation
=
2277 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2279 clear
->IntegerLayerLocation
=
2280 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
2284 _mesa_DeleteObjectARB(gs
);
2288 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
2290 if (clear
->ArrayObj
== 0)
2292 _mesa_DeleteVertexArrays(1, &clear
->ArrayObj
);
2293 clear
->ArrayObj
= 0;
2294 _mesa_DeleteBuffers(1, &clear
->VBO
);
2296 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2297 clear
->ShaderProg
= 0;
2299 if (clear
->IntegerShaderProg
) {
2300 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2301 clear
->IntegerShaderProg
= 0;
2306 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2309 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2311 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2312 GLbitfield metaSave
;
2313 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2314 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2315 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2316 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2317 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2318 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2319 const float z
= -invert_z(ctx
->Depth
.Clear
);
2324 metaSave
= (MESA_META_ALPHA_TEST
|
2326 MESA_META_DEPTH_TEST
|
2327 MESA_META_RASTERIZATION
|
2329 MESA_META_STENCIL_TEST
|
2331 MESA_META_VIEWPORT
|
2333 MESA_META_CLAMP_FRAGMENT_COLOR
|
2334 MESA_META_MULTISAMPLE
|
2335 MESA_META_OCCLUSION_QUERY
);
2337 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2338 /* We'll use colormask to disable color writes. Otherwise,
2339 * respect color mask
2341 metaSave
|= MESA_META_COLOR_MASK
;
2344 _mesa_meta_begin(ctx
, metaSave
);
2346 meta_glsl_clear_init(ctx
, clear
);
2348 if (fb
->_IntegerColor
) {
2349 _mesa_UseProgram(clear
->IntegerShaderProg
);
2350 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2351 ctx
->Color
.ClearColor
.i
);
2353 _mesa_UseProgram(clear
->ShaderProg
);
2354 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2355 ctx
->Color
.ClearColor
.f
);
2358 _mesa_BindVertexArray(clear
->ArrayObj
);
2359 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2361 /* GL_COLOR_BUFFER_BIT */
2362 if (buffers
& BUFFER_BITS_COLOR
) {
2363 /* leave colormask, glDrawBuffer state as-is */
2365 /* Clears never have the color clamped. */
2366 if (ctx
->Extensions
.ARB_color_buffer_float
)
2367 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2370 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2371 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2374 /* GL_DEPTH_BUFFER_BIT */
2375 if (buffers
& BUFFER_BIT_DEPTH
) {
2376 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2377 _mesa_DepthFunc(GL_ALWAYS
);
2378 _mesa_DepthMask(GL_TRUE
);
2381 assert(!ctx
->Depth
.Test
);
2384 /* GL_STENCIL_BUFFER_BIT */
2385 if (buffers
& BUFFER_BIT_STENCIL
) {
2386 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2387 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2388 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2389 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2390 ctx
->Stencil
.Clear
& stencilMax
,
2391 ctx
->Stencil
.WriteMask
[0]);
2394 assert(!ctx
->Stencil
.Enabled
);
2397 /* vertex positions */
2411 /* upload new vertex data */
2412 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2413 GL_DYNAMIC_DRAW_ARB
);
2416 if (fb
->MaxNumLayers
> 0) {
2418 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
2419 if (fb
->_IntegerColor
)
2420 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
2422 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
2423 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2426 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2429 _mesa_meta_end(ctx
);
2433 * Meta implementation of ctx->Driver.CopyPixels() in terms
2434 * of texture mapping and polygon rendering and GLSL shaders.
2437 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2438 GLsizei width
, GLsizei height
,
2439 GLint dstX
, GLint dstY
, GLenum type
)
2441 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2442 struct temp_texture
*tex
= get_temp_texture(ctx
);
2444 GLfloat x
, y
, z
, s
, t
;
2446 struct vertex verts
[4];
2448 GLenum intFormat
= GL_RGBA
;
2450 if (type
!= GL_COLOR
||
2451 ctx
->_ImageTransferState
||
2453 width
> tex
->MaxSize
||
2454 height
> tex
->MaxSize
) {
2455 /* XXX avoid this fallback */
2456 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2460 /* Most GL state applies to glCopyPixels, but a there's a few things
2461 * we need to override:
2463 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2466 MESA_META_TRANSFORM
|
2469 MESA_META_VIEWPORT
));
2471 if (copypix
->ArrayObj
== 0) {
2472 /* one-time setup */
2474 /* create vertex array object */
2475 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2476 _mesa_BindVertexArray(copypix
->ArrayObj
);
2478 /* create vertex array buffer */
2479 _mesa_GenBuffers(1, ©pix
->VBO
);
2480 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2481 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2482 NULL
, GL_DYNAMIC_DRAW_ARB
);
2484 /* setup vertex arrays */
2485 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2486 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2487 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2488 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2491 _mesa_BindVertexArray(copypix
->ArrayObj
);
2492 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2495 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2497 /* vertex positions, texcoords (after texture allocation!) */
2499 const GLfloat dstX0
= (GLfloat
) dstX
;
2500 const GLfloat dstY0
= (GLfloat
) dstY
;
2501 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2502 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2503 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2513 verts
[1].s
= tex
->Sright
;
2518 verts
[2].s
= tex
->Sright
;
2519 verts
[2].t
= tex
->Ttop
;
2524 verts
[3].t
= tex
->Ttop
;
2526 /* upload new vertex data */
2527 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2530 /* Alloc/setup texture */
2531 setup_copypix_texture(ctx
, tex
, newTex
, srcX
, srcY
, width
, height
,
2532 GL_RGBA
, GL_NEAREST
);
2534 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2536 /* draw textured quad */
2537 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2539 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2541 _mesa_meta_end(ctx
);
2547 * When the glDrawPixels() image size is greater than the max rectangle
2548 * texture size we use this function to break the glDrawPixels() image
2549 * into tiles which fit into the max texture size.
2552 tiled_draw_pixels(struct gl_context
*ctx
,
2554 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2555 GLenum format
, GLenum type
,
2556 const struct gl_pixelstore_attrib
*unpack
,
2557 const GLvoid
*pixels
)
2559 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2562 if (tileUnpack
.RowLength
== 0)
2563 tileUnpack
.RowLength
= width
;
2565 for (i
= 0; i
< width
; i
+= tileSize
) {
2566 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2567 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2569 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2571 for (j
= 0; j
< height
; j
+= tileSize
) {
2572 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2573 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2575 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2577 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2578 format
, type
, &tileUnpack
, pixels
);
2585 * One-time init for drawing stencil pixels.
2588 init_draw_stencil_pixels(struct gl_context
*ctx
)
2590 /* This program is run eight times, once for each stencil bit.
2591 * The stencil values to draw are found in an 8-bit alpha texture.
2592 * We read the texture/stencil value and test if bit 'b' is set.
2593 * If the bit is not set, use KIL to kill the fragment.
2594 * Finally, we use the stencil test to update the stencil buffer.
2596 * The basic algorithm for checking if a bit is set is:
2597 * if (is_odd(value / (1 << bit)))
2598 * result is one (or non-zero).
2601 * The program parameter contains three values:
2602 * parm.x = 255 / (1 << bit)
2606 static const char *program
=
2608 "PARAM parm = program.local[0]; \n"
2610 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2611 "# t = t * 255 / bit \n"
2612 "MUL t.x, t.a, parm.x; \n"
2615 "SUB t.x, t.x, t.y; \n"
2617 "MUL t.x, t.x, parm.y; \n"
2618 "# t = fract(t.x) \n"
2619 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2620 "# t.x = (t.x == 0 ? 1 : 0) \n"
2621 "SGE t.x, -t.x, parm.z; \n"
2623 "# for debug only \n"
2624 "#MOV result.color, t.x; \n"
2626 char program2
[1000];
2627 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2628 struct temp_texture
*tex
= get_temp_texture(ctx
);
2629 const char *texTarget
;
2631 assert(drawpix
->StencilFP
== 0);
2633 /* replace %s with "RECT" or "2D" */
2634 assert(strlen(program
) + 4 < sizeof(program2
));
2635 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2639 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2641 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2642 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2643 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2644 strlen(program2
), (const GLubyte
*) program2
);
2649 * One-time init for drawing depth pixels.
2652 init_draw_depth_pixels(struct gl_context
*ctx
)
2654 static const char *program
=
2656 "PARAM color = program.local[0]; \n"
2657 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2658 "MOV result.color, color; \n"
2661 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2662 struct temp_texture
*tex
= get_temp_texture(ctx
);
2663 const char *texTarget
;
2665 assert(drawpix
->DepthFP
== 0);
2667 /* replace %s with "RECT" or "2D" */
2668 assert(strlen(program
) + 4 < sizeof(program2
));
2669 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2673 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2675 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2676 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2677 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2678 strlen(program2
), (const GLubyte
*) program2
);
2683 * Meta implementation of ctx->Driver.DrawPixels() in terms
2684 * of texture mapping and polygon rendering.
2687 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2688 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2689 GLenum format
, GLenum type
,
2690 const struct gl_pixelstore_attrib
*unpack
,
2691 const GLvoid
*pixels
)
2693 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2694 struct temp_texture
*tex
= get_temp_texture(ctx
);
2695 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2696 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2698 GLfloat x
, y
, z
, s
, t
;
2700 struct vertex verts
[4];
2701 GLenum texIntFormat
;
2702 GLboolean fallback
, newTex
;
2703 GLbitfield metaExtraSave
= 0x0;
2707 * Determine if we can do the glDrawPixels with texture mapping.
2709 fallback
= GL_FALSE
;
2710 if (ctx
->Fog
.Enabled
) {
2714 if (_mesa_is_color_format(format
)) {
2715 /* use more compact format when possible */
2716 /* XXX disable special case for GL_LUMINANCE for now to work around
2717 * apparent i965 driver bug (see bug #23670).
2719 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2720 texIntFormat
= format
;
2722 texIntFormat
= GL_RGBA
;
2724 /* If we're not supposed to clamp the resulting color, then just
2725 * promote our texture to fully float. We could do better by
2726 * just going for the matching set of channels, in floating
2729 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2730 ctx
->Extensions
.ARB_texture_float
)
2731 texIntFormat
= GL_RGBA32F
;
2733 else if (_mesa_is_stencil_format(format
)) {
2734 if (ctx
->Extensions
.ARB_fragment_program
&&
2735 ctx
->Pixel
.IndexShift
== 0 &&
2736 ctx
->Pixel
.IndexOffset
== 0 &&
2737 type
== GL_UNSIGNED_BYTE
) {
2738 /* We'll store stencil as alpha. This only works for GLubyte
2739 * image data because of how incoming values are mapped to alpha
2742 texIntFormat
= GL_ALPHA
;
2743 metaExtraSave
= (MESA_META_COLOR_MASK
|
2744 MESA_META_DEPTH_TEST
|
2745 MESA_META_PIXEL_TRANSFER
|
2747 MESA_META_STENCIL_TEST
);
2753 else if (_mesa_is_depth_format(format
)) {
2754 if (ctx
->Extensions
.ARB_depth_texture
&&
2755 ctx
->Extensions
.ARB_fragment_program
) {
2756 texIntFormat
= GL_DEPTH_COMPONENT
;
2757 metaExtraSave
= (MESA_META_SHADER
);
2768 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2769 format
, type
, unpack
, pixels
);
2774 * Check image size against max texture size, draw as tiles if needed.
2776 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2777 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2778 format
, type
, unpack
, pixels
);
2782 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2783 * but a there's a few things we need to override:
2785 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2788 MESA_META_TRANSFORM
|
2791 MESA_META_VIEWPORT
|
2794 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2796 /* vertex positions, texcoords (after texture allocation!) */
2798 const GLfloat x0
= (GLfloat
) x
;
2799 const GLfloat y0
= (GLfloat
) y
;
2800 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2801 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2802 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2812 verts
[1].s
= tex
->Sright
;
2817 verts
[2].s
= tex
->Sright
;
2818 verts
[2].t
= tex
->Ttop
;
2823 verts
[3].t
= tex
->Ttop
;
2826 if (drawpix
->ArrayObj
== 0) {
2827 /* one-time setup: create vertex array object */
2828 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2830 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2832 /* create vertex array buffer */
2833 _mesa_GenBuffers(1, &vbo
);
2834 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2835 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2836 verts
, GL_DYNAMIC_DRAW_ARB
);
2838 /* setup vertex arrays */
2839 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2840 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2841 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2842 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2844 /* set given unpack params */
2845 ctx
->Unpack
= *unpack
;
2847 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2849 if (_mesa_is_stencil_format(format
)) {
2850 /* Drawing stencil */
2853 if (!drawpix
->StencilFP
)
2854 init_draw_stencil_pixels(ctx
);
2856 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2857 GL_ALPHA
, type
, pixels
);
2859 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2861 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2863 /* set all stencil bits to 0 */
2864 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2865 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2866 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2868 /* set stencil bits to 1 where needed */
2869 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2871 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2872 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2874 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2875 const GLuint mask
= 1 << bit
;
2876 if (mask
& origStencilMask
) {
2877 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2878 _mesa_StencilMask(mask
);
2880 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2881 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2883 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2887 else if (_mesa_is_depth_format(format
)) {
2889 if (!drawpix
->DepthFP
)
2890 init_draw_depth_pixels(ctx
);
2892 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2893 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2895 /* polygon color = current raster color */
2896 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2897 ctx
->Current
.RasterColor
);
2899 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2900 format
, type
, pixels
);
2902 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2906 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2907 format
, type
, pixels
);
2908 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2911 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2913 _mesa_DeleteBuffers(1, &vbo
);
2915 /* restore unpack params */
2916 ctx
->Unpack
= unpackSave
;
2918 _mesa_meta_end(ctx
);
2922 alpha_test_raster_color(struct gl_context
*ctx
)
2924 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2925 GLfloat ref
= ctx
->Color
.AlphaRef
;
2927 switch (ctx
->Color
.AlphaFunc
) {
2933 return alpha
== ref
;
2935 return alpha
<= ref
;
2939 return alpha
!= ref
;
2941 return alpha
>= ref
;
2951 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2952 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2953 * tracker would improve performance a lot.
2956 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2957 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2958 const struct gl_pixelstore_attrib
*unpack
,
2959 const GLubyte
*bitmap1
)
2961 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2962 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2963 const GLenum texIntFormat
= GL_ALPHA
;
2964 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2967 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2969 struct vertex verts
[4];
2974 * Check if swrast fallback is needed.
2976 if (ctx
->_ImageTransferState
||
2977 ctx
->FragmentProgram
._Enabled
||
2979 ctx
->Texture
._EnabledUnits
||
2980 width
> tex
->MaxSize
||
2981 height
> tex
->MaxSize
) {
2982 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2986 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2989 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2990 * but a there's a few things we need to override:
2992 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2993 MESA_META_PIXEL_STORE
|
2994 MESA_META_RASTERIZATION
|
2997 MESA_META_TRANSFORM
|
3000 MESA_META_VIEWPORT
));
3002 if (bitmap
->ArrayObj
== 0) {
3003 /* one-time setup */
3005 /* create vertex array object */
3006 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
3007 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
3009 /* create vertex array buffer */
3010 _mesa_GenBuffers(1, &bitmap
->VBO
);
3011 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3012 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3013 NULL
, GL_DYNAMIC_DRAW_ARB
);
3015 /* setup vertex arrays */
3016 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3017 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
3018 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
3019 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3020 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3021 _mesa_EnableClientState(GL_COLOR_ARRAY
);
3024 _mesa_BindVertexArray(bitmap
->ArrayObj
);
3025 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3028 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
3030 /* vertex positions, texcoords, colors (after texture allocation!) */
3032 const GLfloat x0
= (GLfloat
) x
;
3033 const GLfloat y0
= (GLfloat
) y
;
3034 const GLfloat x1
= (GLfloat
) (x
+ width
);
3035 const GLfloat y1
= (GLfloat
) (y
+ height
);
3036 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
3047 verts
[1].s
= tex
->Sright
;
3052 verts
[2].s
= tex
->Sright
;
3053 verts
[2].t
= tex
->Ttop
;
3058 verts
[3].t
= tex
->Ttop
;
3060 for (i
= 0; i
< 4; i
++) {
3061 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
3062 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
3063 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
3064 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
3067 /* upload new vertex data */
3068 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3071 /* choose different foreground/background alpha values */
3072 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
3073 bg
= (fg
> 127 ? 0 : 255);
3075 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
3077 _mesa_meta_end(ctx
);
3081 bitmap8
= malloc(width
* height
);
3083 memset(bitmap8
, bg
, width
* height
);
3084 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
3085 bitmap8
, width
, fg
);
3087 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
3089 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
3090 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
3092 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
3093 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
3095 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3097 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
3102 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
3104 _mesa_meta_end(ctx
);
3109 * Check if the call to _mesa_meta_GenerateMipmap() will require a
3110 * software fallback. The fallback path will require that the texture
3111 * images are mapped.
3112 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
3115 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
3116 struct gl_texture_object
*texObj
)
3118 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3119 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3120 struct gl_texture_image
*baseImage
;
3124 /* check for fallbacks */
3125 if (target
== GL_TEXTURE_3D
||
3126 target
== GL_TEXTURE_1D_ARRAY
||
3127 target
== GL_TEXTURE_2D_ARRAY
) {
3128 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3129 "glGenerateMipmap() to %s target\n",
3130 _mesa_lookup_enum_by_nr(target
));
3134 srcLevel
= texObj
->BaseLevel
;
3135 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
3137 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3138 "glGenerateMipmap() couldn't find base teximage\n");
3142 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
3143 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3144 "glGenerateMipmap() with %s format\n",
3145 _mesa_get_format_name(baseImage
->TexFormat
));
3149 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
3150 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3151 /* The texture format is sRGB but we can't turn off sRGB->linear
3152 * texture sample conversion. So we won't be able to generate the
3153 * right colors when rendering. Need to use a fallback.
3155 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3156 "glGenerateMipmap() of sRGB texture without "
3162 * Test that we can actually render in the texture's format.
3165 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3166 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3168 if (target
== GL_TEXTURE_1D
) {
3169 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3170 GL_COLOR_ATTACHMENT0_EXT
,
3171 target
, texObj
->Name
, srcLevel
);
3174 /* other work is needed to enable 3D mipmap generation */
3175 else if (target
== GL_TEXTURE_3D
) {
3177 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3178 GL_COLOR_ATTACHMENT0_EXT
,
3179 target
, texObj
->Name
, srcLevel
, zoffset
);
3184 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3185 GL_COLOR_ATTACHMENT0_EXT
,
3186 target
, texObj
->Name
, srcLevel
);
3189 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3191 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3193 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3194 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3195 "glGenerateMipmap() got incomplete FBO\n");
3204 * Compute the texture coordinates for the four vertices of a quad for
3205 * drawing a 2D texture image or slice of a cube/3D texture.
3206 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
3207 * \param slice slice of a 1D/2D array texture or 3D texture
3208 * \param width width of the texture image
3209 * \param height height of the texture image
3210 * \param coords0/1/2/3 returns the computed texcoords
3213 setup_texture_coords(GLenum faceTarget
,
3223 static const GLfloat st
[4][2] = {
3224 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
3229 switch (faceTarget
) {
3233 case GL_TEXTURE_2D_ARRAY
:
3234 if (faceTarget
== GL_TEXTURE_3D
) {
3235 assert(slice
< depth
);
3237 r
= (slice
+ 0.5f
) / depth
;
3239 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
3243 coords0
[0] = 0.0F
; /* s */
3244 coords0
[1] = 0.0F
; /* t */
3245 coords0
[2] = r
; /* r */
3256 case GL_TEXTURE_RECTANGLE_ARB
:
3257 coords0
[0] = 0.0F
; /* s */
3258 coords0
[1] = 0.0F
; /* t */
3259 coords0
[2] = 0.0F
; /* r */
3260 coords1
[0] = (float) width
;
3263 coords2
[0] = (float) width
;
3264 coords2
[1] = (float) height
;
3267 coords3
[1] = (float) height
;
3270 case GL_TEXTURE_1D_ARRAY
:
3271 coords0
[0] = 0.0F
; /* s */
3272 coords0
[1] = (float) slice
; /* t */
3273 coords0
[2] = 0.0F
; /* r */
3275 coords1
[1] = (float) slice
;
3278 coords2
[1] = (float) slice
;
3281 coords3
[1] = (float) slice
;
3285 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3286 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3287 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3288 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3289 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3290 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3291 /* loop over quad verts */
3292 for (i
= 0; i
< 4; i
++) {
3293 /* Compute sc = +/-scale and tc = +/-scale.
3294 * Not +/-1 to avoid cube face selection ambiguity near the edges,
3295 * though that can still sometimes happen with this scale factor...
3297 const GLfloat scale
= 0.9999f
;
3298 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
3299 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
3319 switch (faceTarget
) {
3320 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3325 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3330 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3335 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3340 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3345 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3356 assert(0 && "unexpected target in meta setup_texture_coords()");
3362 setup_ff_generate_mipmap(struct gl_context
*ctx
,
3363 struct gen_mipmap_state
*mipmap
)
3366 GLfloat x
, y
, tex
[3];
3369 if (mipmap
->ArrayObj
== 0) {
3370 /* one-time setup */
3371 /* create vertex array object */
3372 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3373 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
3375 /* create vertex array buffer */
3376 _mesa_GenBuffers(1, &mipmap
->VBO
);
3377 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3378 /* setup vertex arrays */
3379 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3380 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3381 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3382 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3385 /* setup projection matrix */
3386 _mesa_MatrixMode(GL_PROJECTION
);
3387 _mesa_LoadIdentity();
3388 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3392 static struct glsl_sampler
*
3393 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3397 mipmap
->sampler_1d
.type
= "sampler1D";
3398 mipmap
->sampler_1d
.func
= "texture1D";
3399 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3400 return &mipmap
->sampler_1d
;
3402 mipmap
->sampler_2d
.type
= "sampler2D";
3403 mipmap
->sampler_2d
.func
= "texture2D";
3404 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3405 return &mipmap
->sampler_2d
;
3407 /* Code for mipmap generation with 3D textures is not used yet.
3408 * It's a sw fallback.
3410 mipmap
->sampler_3d
.type
= "sampler3D";
3411 mipmap
->sampler_3d
.func
= "texture3D";
3412 mipmap
->sampler_3d
.texcoords
= "texCoords";
3413 return &mipmap
->sampler_3d
;
3414 case GL_TEXTURE_CUBE_MAP
:
3415 mipmap
->sampler_cubemap
.type
= "samplerCube";
3416 mipmap
->sampler_cubemap
.func
= "textureCube";
3417 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3418 return &mipmap
->sampler_cubemap
;
3419 case GL_TEXTURE_1D_ARRAY
:
3420 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3421 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3422 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3423 return &mipmap
->sampler_1d_array
;
3424 case GL_TEXTURE_2D_ARRAY
:
3425 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3426 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3427 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3428 return &mipmap
->sampler_2d_array
;
3430 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3431 " setup_texture_sampler()\n", target
);
3438 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3439 struct gen_mipmap_state
*mipmap
,
3443 GLfloat x
, y
, tex
[3];
3445 struct glsl_sampler
*sampler
;
3446 const char *vs_source
;
3451 /* Check if already initialized */
3452 if (mipmap
->ArrayObj
== 0) {
3454 /* create vertex array object */
3455 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3456 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3458 /* create vertex array buffer */
3459 _mesa_GenBuffers(1, &mipmap
->VBO
);
3460 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3462 /* setup vertex arrays */
3463 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3464 sizeof(struct vertex
), OFFSET(x
));
3465 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3466 sizeof(struct vertex
), OFFSET(tex
));
3467 _mesa_EnableVertexAttribArray(0);
3468 _mesa_EnableVertexAttribArray(1);
3471 /* Generate a fragment shader program appropriate for the texture target */
3472 sampler
= setup_texture_sampler(target
, mipmap
);
3473 assert(sampler
!= NULL
);
3474 if (sampler
->shader_prog
!= 0) {
3475 mipmap
->ShaderProg
= sampler
->shader_prog
;
3479 mem_ctx
= ralloc_context(NULL
);
3481 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3483 "attribute vec2 position;\n"
3484 "attribute vec3 textureCoords;\n"
3485 "varying vec3 texCoords;\n"
3488 " texCoords = textureCoords;\n"
3489 " gl_Position = vec4(position, 0.0, 1.0);\n"
3492 fs_source
= ralloc_asprintf(mem_ctx
,
3493 "#extension GL_EXT_texture_array : enable\n"
3495 "precision highp float;\n"
3497 "uniform %s texSampler;\n"
3498 "varying vec3 texCoords;\n"
3501 " gl_FragColor = %s(texSampler, %s);\n"
3504 sampler
->func
, sampler
->texcoords
);
3507 vs_source
= ralloc_asprintf(mem_ctx
,
3509 "in vec2 position;\n"
3510 "in vec3 textureCoords;\n"
3511 "out vec3 texCoords;\n"
3514 " texCoords = textureCoords;\n"
3515 " gl_Position = vec4(position, 0.0, 1.0);\n"
3517 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
3518 fs_source
= ralloc_asprintf(mem_ctx
,
3521 "precision highp float;\n"
3523 "uniform %s texSampler;\n"
3524 "in vec3 texCoords;\n"
3525 "out vec4 out_color;\n"
3529 " out_color = texture(texSampler, %s);\n"
3531 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
3533 sampler
->texcoords
);
3536 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3537 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3539 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3540 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3541 _mesa_DeleteObjectARB(fs
);
3542 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3543 _mesa_DeleteObjectARB(vs
);
3544 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3545 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3546 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3547 sampler
->shader_prog
= mipmap
->ShaderProg
;
3548 ralloc_free(mem_ctx
);
3553 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3554 struct gen_mipmap_state
*mipmap
)
3556 if (mipmap
->ArrayObj
== 0)
3558 _mesa_DeleteVertexArrays(1, &mipmap
->ArrayObj
);
3559 mipmap
->ArrayObj
= 0;
3560 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3563 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3564 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3565 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3566 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3567 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3568 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3570 mipmap
->sampler_1d
.shader_prog
= 0;
3571 mipmap
->sampler_2d
.shader_prog
= 0;
3572 mipmap
->sampler_3d
.shader_prog
= 0;
3573 mipmap
->sampler_cubemap
.shader_prog
= 0;
3574 mipmap
->sampler_1d_array
.shader_prog
= 0;
3575 mipmap
->sampler_2d_array
.shader_prog
= 0;
3580 * Called via ctx->Driver.GenerateMipmap()
3581 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3585 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3586 struct gl_texture_object
*texObj
)
3588 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3590 GLfloat x
, y
, tex
[3];
3592 struct vertex verts
[4];
3593 const GLuint baseLevel
= texObj
->BaseLevel
;
3594 const GLuint maxLevel
= texObj
->MaxLevel
;
3595 const GLint maxLevelSave
= texObj
->MaxLevel
;
3596 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3597 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3598 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3599 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3600 ctx
->Extensions
.ARB_fragment_shader
&&
3601 (ctx
->API
!= API_OPENGLES
);
3604 const GLint slice
= 0;
3607 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3608 _mesa_generate_mipmap(ctx
, target
, texObj
);
3612 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3613 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3614 faceTarget
= target
;
3615 target
= GL_TEXTURE_CUBE_MAP
;
3618 faceTarget
= target
;
3621 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3623 /* Choose between glsl version and fixed function version of
3624 * GenerateMipmap function.
3626 if (use_glsl_version
) {
3627 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3628 _mesa_UseProgram(mipmap
->ShaderProg
);
3631 setup_ff_generate_mipmap(ctx
, mipmap
);
3632 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3635 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3636 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3638 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3639 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3641 if (currentTexUnitSave
!= 0)
3642 _mesa_BindTexture(target
, texObj
->Name
);
3645 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3648 if (!mipmap
->Sampler
) {
3649 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3650 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3652 _mesa_SamplerParameteri(mipmap
->Sampler
,
3653 GL_TEXTURE_MIN_FILTER
,
3654 GL_LINEAR_MIPMAP_LINEAR
);
3655 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3656 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3657 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3658 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3660 /* We don't want to encode or decode sRGB values; treat them as linear.
3661 * This is not technically correct for GLES3 but we don't get any API
3662 * error at the moment.
3664 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3665 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3666 GL_SKIP_DECODE_EXT
);
3670 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3673 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3675 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3676 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3678 assert(!genMipmapSave
);
3680 /* Setup texture coordinates */
3681 setup_texture_coords(faceTarget
,
3683 0, 0, 1, /* width, height never used here */
3689 /* setup vertex positions */
3699 /* upload vertex data */
3700 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3701 verts
, GL_DYNAMIC_DRAW_ARB
);
3703 /* texture is already locked, unlock now */
3704 _mesa_unlock_texture(ctx
, texObj
);
3706 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3707 const struct gl_texture_image
*srcImage
;
3708 const GLuint srcLevel
= dstLevel
- 1;
3709 GLsizei srcWidth
, srcHeight
, srcDepth
;
3710 GLsizei dstWidth
, dstHeight
, dstDepth
;
3713 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3714 assert(srcImage
->Border
== 0);
3717 srcWidth
= srcImage
->Width
;
3718 srcHeight
= srcImage
->Height
;
3719 srcDepth
= srcImage
->Depth
;
3722 dstWidth
= MAX2(1, srcWidth
/ 2);
3723 dstHeight
= MAX2(1, srcHeight
/ 2);
3724 dstDepth
= MAX2(1, srcDepth
/ 2);
3726 if (dstWidth
== srcImage
->Width
&&
3727 dstHeight
== srcImage
->Height
&&
3728 dstDepth
== srcImage
->Depth
) {
3733 /* Allocate storage for the destination mipmap image(s) */
3735 /* Set MaxLevel large enough to hold the new level when we allocate it */
3736 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3738 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3739 dstWidth
, dstHeight
, dstDepth
,
3741 srcImage
->InternalFormat
,
3742 srcImage
->TexFormat
)) {
3743 /* All done. We either ran out of memory or we would go beyond the
3744 * last valid level of an immutable texture if we continued.
3749 /* limit minification to src level */
3750 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3752 /* Set to draw into the current dstLevel */
3753 if (target
== GL_TEXTURE_1D
) {
3754 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3755 GL_COLOR_ATTACHMENT0_EXT
,
3760 else if (target
== GL_TEXTURE_3D
) {
3761 GLint zoffset
= 0; /* XXX unfinished */
3762 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3763 GL_COLOR_ATTACHMENT0_EXT
,
3770 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3771 GL_COLOR_ATTACHMENT0_EXT
,
3777 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3780 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3781 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3782 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3783 "_mesa_meta_GenerateMipmap()");
3787 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3788 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3790 /* setup viewport */
3791 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
3793 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3796 _mesa_lock_texture(ctx
, texObj
); /* relock */
3798 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3800 _mesa_meta_end(ctx
);
3802 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3804 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3806 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3811 * Determine the GL data type to use for the temporary image read with
3812 * ReadPixels() and passed to Tex[Sub]Image().
3815 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3819 baseFormat
= _mesa_get_format_base_format(format
);
3821 switch (baseFormat
) {
3828 case GL_LUMINANCE_ALPHA
:
3830 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3831 return GL_UNSIGNED_BYTE
;
3832 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3833 return GL_UNSIGNED_SHORT
;
3835 GLenum datatype
= _mesa_get_format_datatype(format
);
3836 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3840 case GL_DEPTH_COMPONENT
: {
3841 GLenum datatype
= _mesa_get_format_datatype(format
);
3842 if (datatype
== GL_FLOAT
)
3845 return GL_UNSIGNED_INT
;
3847 case GL_DEPTH_STENCIL
: {
3848 GLenum datatype
= _mesa_get_format_datatype(format
);
3849 if (datatype
== GL_FLOAT
)
3850 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3852 return GL_UNSIGNED_INT_24_8
;
3855 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3863 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3864 * Have to be careful with locking and meta state for pixel transfer.
3867 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3868 struct gl_texture_image
*texImage
,
3869 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3870 struct gl_renderbuffer
*rb
,
3872 GLsizei width
, GLsizei height
)
3874 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3875 GLenum format
, type
;
3879 /* Choose format/type for temporary image buffer */
3880 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3881 if (format
== GL_LUMINANCE
||
3882 format
== GL_LUMINANCE_ALPHA
||
3883 format
== GL_INTENSITY
) {
3884 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3885 * temp image buffer because glReadPixels will do L=R+G+B which is
3886 * not what we want (should be L=R).
3891 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3892 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3893 format
= _mesa_base_format_to_integer_format(format
);
3895 bpp
= _mesa_bytes_per_pixel(format
, type
);
3897 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3902 * Alloc image buffer (XXX could use a PBO)
3904 buf
= malloc(width
* height
* bpp
);
3906 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3910 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3913 * Read image from framebuffer (disable pixel transfer ops)
3915 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3916 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3917 format
, type
, &ctx
->Pack
, buf
);
3918 _mesa_meta_end(ctx
);
3920 _mesa_update_state(ctx
); /* to update pixel transfer state */
3923 * Store texture data (with pixel transfer ops)
3925 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3927 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3928 assert(yoffset
== 0);
3929 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3930 xoffset
, zoffset
, 0, width
, 1, 1,
3931 format
, type
, buf
, &ctx
->Unpack
);
3933 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3934 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3935 format
, type
, buf
, &ctx
->Unpack
);
3938 _mesa_meta_end(ctx
);
3940 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3947 * Decompress a texture image by drawing a quad with the compressed
3948 * texture and reading the pixels out of the color buffer.
3949 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3950 * \param destFormat format, ala glReadPixels
3951 * \param destType type, ala glReadPixels
3952 * \param dest destination buffer
3953 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3956 decompress_texture_image(struct gl_context
*ctx
,
3957 struct gl_texture_image
*texImage
,
3959 GLenum destFormat
, GLenum destType
,
3962 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3963 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3964 const GLint width
= texImage
->Width
;
3965 const GLint height
= texImage
->Height
;
3966 const GLint depth
= texImage
->Height
;
3967 const GLenum target
= texObj
->Target
;
3970 GLfloat x
, y
, tex
[3];
3972 struct vertex verts
[4];
3973 GLuint fboDrawSave
, fboReadSave
;
3978 assert(target
== GL_TEXTURE_3D
||
3979 target
== GL_TEXTURE_2D_ARRAY
);
3982 if (target
== GL_TEXTURE_CUBE_MAP
) {
3983 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3986 faceTarget
= target
;
3989 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3990 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3991 fboReadSave
= ctx
->ReadBuffer
->Name
;
3992 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3994 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3996 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3997 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3999 /* Create/bind FBO/renderbuffer */
4000 if (decompress
->FBO
== 0) {
4001 _mesa_GenFramebuffers(1, &decompress
->FBO
);
4002 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
4003 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4004 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4005 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
4006 GL_COLOR_ATTACHMENT0_EXT
,
4007 GL_RENDERBUFFER_EXT
,
4011 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4014 /* alloc dest surface */
4015 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
4016 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4017 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
4019 decompress
->Width
= width
;
4020 decompress
->Height
= height
;
4023 /* setup VBO data */
4024 if (decompress
->ArrayObj
== 0) {
4025 /* create vertex array object */
4026 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
4027 _mesa_BindVertexArray(decompress
->ArrayObj
);
4029 /* create vertex array buffer */
4030 _mesa_GenBuffers(1, &decompress
->VBO
);
4031 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4032 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4033 NULL
, GL_DYNAMIC_DRAW_ARB
);
4035 /* setup vertex arrays */
4036 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4037 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
4038 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4039 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4042 _mesa_BindVertexArray(decompress
->ArrayObj
);
4043 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4046 if (!decompress
->Sampler
) {
4047 _mesa_GenSamplers(1, &decompress
->Sampler
);
4048 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4049 /* nearest filtering */
4050 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4051 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4052 /* No sRGB decode or encode.*/
4053 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
4054 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
4055 GL_SKIP_DECODE_EXT
);
4059 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4062 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
4068 /* setup vertex positions */
4074 verts
[2].y
= height
;
4076 verts
[3].y
= height
;
4078 _mesa_MatrixMode(GL_PROJECTION
);
4079 _mesa_LoadIdentity();
4080 _mesa_Ortho(0.0, width
, 0.0, height
, -1.0, 1.0);
4081 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
4083 /* upload new vertex data */
4084 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4086 /* setup texture state */
4087 _mesa_BindTexture(target
, texObj
->Name
);
4088 _mesa_set_enable(ctx
, target
, GL_TRUE
);
4091 /* save texture object state */
4092 const GLint baseLevelSave
= texObj
->BaseLevel
;
4093 const GLint maxLevelSave
= texObj
->MaxLevel
;
4095 /* restrict sampling to the texture level of interest */
4096 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4097 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
4098 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
4101 /* render quad w/ texture into renderbuffer */
4102 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4104 /* Restore texture object state, the texture binding will
4105 * be restored by _mesa_meta_end().
4107 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4108 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
4109 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
4114 /* read pixels from renderbuffer */
4116 GLenum baseTexFormat
= texImage
->_BaseFormat
;
4117 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
4119 /* The pixel transfer state will be set to default values at this point
4120 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
4121 * turned off (as required by glGetTexImage) but we need to handle some
4122 * special cases. In particular, single-channel texture values are
4123 * returned as red and two-channel texture values are returned as
4126 if ((baseTexFormat
== GL_LUMINANCE
||
4127 baseTexFormat
== GL_LUMINANCE_ALPHA
||
4128 baseTexFormat
== GL_INTENSITY
) ||
4129 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
4130 * luminance then we need to return L=tex(R).
4132 ((baseTexFormat
== GL_RGBA
||
4133 baseTexFormat
== GL_RGB
||
4134 baseTexFormat
== GL_RG
) &&
4135 (destBaseFormat
== GL_LUMINANCE
||
4136 destBaseFormat
== GL_LUMINANCE_ALPHA
||
4137 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
4138 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
4139 /* Green and blue must be zero */
4140 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
4141 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
4144 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
4147 /* disable texture unit */
4148 _mesa_set_enable(ctx
, target
, GL_FALSE
);
4150 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
4152 _mesa_meta_end(ctx
);
4154 /* restore fbo bindings */
4155 if (fboDrawSave
== fboReadSave
) {
4156 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
4159 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
4160 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
4162 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
4167 * This is just a wrapper around _mesa_get_tex_image() and
4168 * decompress_texture_image(). Meta functions should not be directly called
4172 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
4173 GLenum format
, GLenum type
, GLvoid
*pixels
,
4174 struct gl_texture_image
*texImage
)
4176 /* We can only use the decompress-with-blit method here if the texels are
4177 * unsigned, normalized values. We could handle signed and unnormalized
4178 * with floating point renderbuffers...
4180 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
4181 _mesa_get_format_datatype(texImage
->TexFormat
)
4182 == GL_UNSIGNED_NORMALIZED
) {
4183 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4185 /* Need to unlock the texture here to prevent deadlock... */
4186 _mesa_unlock_texture(ctx
, texObj
);
4187 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
4189 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
4190 /* Setup pixel packing. SkipPixels and SkipRows will be applied
4191 * in the decompress_texture_image() function's call to
4192 * glReadPixels but we need to compute the dest slice's address
4193 * here (according to SkipImages and ImageHeight).
4195 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
4196 packing
.SkipPixels
= 0;
4197 packing
.SkipRows
= 0;
4198 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
4199 texImage
->Height
, format
, type
,
4205 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
4207 /* ... and relock it */
4208 _mesa_lock_texture(ctx
, texObj
);
4211 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
4217 * Meta implementation of ctx->Driver.DrawTex() in terms
4218 * of polygon rendering.
4221 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
4222 GLfloat width
, GLfloat height
)
4224 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
4226 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
4228 struct vertex verts
[4];
4231 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
4233 MESA_META_TRANSFORM
|
4235 MESA_META_VIEWPORT
));
4237 if (drawtex
->ArrayObj
== 0) {
4238 /* one-time setup */
4239 GLint active_texture
;
4241 /* create vertex array object */
4242 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
4243 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4245 /* create vertex array buffer */
4246 _mesa_GenBuffers(1, &drawtex
->VBO
);
4247 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4248 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4249 NULL
, GL_DYNAMIC_DRAW_ARB
);
4251 /* client active texture is not part of the array object */
4252 active_texture
= ctx
->Array
.ActiveTexture
;
4254 /* setup vertex arrays */
4255 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4256 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4257 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4258 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
4259 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
4260 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4263 /* restore client active texture */
4264 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
4267 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4268 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4271 /* vertex positions, texcoords */
4273 const GLfloat x1
= x
+ width
;
4274 const GLfloat y1
= y
+ height
;
4276 z
= CLAMP(z
, 0.0f
, 1.0f
);
4295 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4296 const struct gl_texture_object
*texObj
;
4297 const struct gl_texture_image
*texImage
;
4298 GLfloat s
, t
, s1
, t1
;
4301 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
4303 for (j
= 0; j
< 4; j
++) {
4304 verts
[j
].st
[i
][0] = 0.0f
;
4305 verts
[j
].st
[i
][1] = 0.0f
;
4310 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
4311 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
4312 tw
= texImage
->Width2
;
4313 th
= texImage
->Height2
;
4315 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
4316 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
4317 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
4318 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
4320 verts
[0].st
[i
][0] = s
;
4321 verts
[0].st
[i
][1] = t
;
4323 verts
[1].st
[i
][0] = s1
;
4324 verts
[1].st
[i
][1] = t
;
4326 verts
[2].st
[i
][0] = s1
;
4327 verts
[2].st
[i
][1] = t1
;
4329 verts
[3].st
[i
][0] = s
;
4330 verts
[3].st
[i
][1] = t1
;
4333 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4336 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4338 _mesa_meta_end(ctx
);