2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/queryobj.h"
58 #include "main/readpix.h"
59 #include "main/scissor.h"
60 #include "main/shaderapi.h"
61 #include "main/shaderobj.h"
62 #include "main/state.h"
63 #include "main/stencil.h"
64 #include "main/texobj.h"
65 #include "main/texenv.h"
66 #include "main/texgetimage.h"
67 #include "main/teximage.h"
68 #include "main/texparam.h"
69 #include "main/texstate.h"
70 #include "main/transformfeedback.h"
71 #include "main/uniforms.h"
72 #include "main/varray.h"
73 #include "main/viewport.h"
74 #include "main/samplerobj.h"
75 #include "program/program.h"
76 #include "swrast/swrast.h"
77 #include "drivers/common/meta.h"
78 #include "main/enums.h"
79 #include "main/glformats.h"
80 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_CLEAR (and others?) */
94 struct gl_query_object
*CurrentOcclusionObject
;
96 /** MESA_META_ALPHA_TEST */
97 GLboolean AlphaEnabled
;
101 /** MESA_META_BLEND */
102 GLbitfield BlendEnabled
;
103 GLboolean ColorLogicOpEnabled
;
105 /** MESA_META_COLOR_MASK */
106 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
108 /** MESA_META_DEPTH_TEST */
109 struct gl_depthbuffer_attrib Depth
;
114 /** MESA_META_PIXEL_STORE */
115 struct gl_pixelstore_attrib Pack
, Unpack
;
117 /** MESA_META_PIXEL_TRANSFER */
118 GLfloat RedBias
, RedScale
;
119 GLfloat GreenBias
, GreenScale
;
120 GLfloat BlueBias
, BlueScale
;
121 GLfloat AlphaBias
, AlphaScale
;
122 GLfloat DepthBias
, DepthScale
;
123 GLboolean MapColorFlag
;
125 /** MESA_META_RASTERIZATION */
126 GLenum FrontPolygonMode
, BackPolygonMode
;
127 GLboolean PolygonOffset
;
128 GLboolean PolygonSmooth
;
129 GLboolean PolygonStipple
;
130 GLboolean PolygonCull
;
132 /** MESA_META_SCISSOR */
133 struct gl_scissor_attrib Scissor
;
135 /** MESA_META_SHADER */
136 GLboolean VertexProgramEnabled
;
137 struct gl_vertex_program
*VertexProgram
;
138 GLboolean FragmentProgramEnabled
;
139 struct gl_fragment_program
*FragmentProgram
;
140 GLboolean ATIFragmentShaderEnabled
;
141 struct gl_shader_program
*Shader
[MESA_SHADER_STAGES
];
142 struct gl_shader_program
*ActiveShader
;
144 /** MESA_META_STENCIL_TEST */
145 struct gl_stencil_attrib Stencil
;
147 /** MESA_META_TRANSFORM */
149 GLfloat ModelviewMatrix
[16];
150 GLfloat ProjectionMatrix
[16];
151 GLfloat TextureMatrix
[16];
153 /** MESA_META_CLIP */
154 GLbitfield ClipPlanesEnabled
;
156 /** MESA_META_TEXTURE */
158 GLuint ClientActiveUnit
;
159 /** for unit[0] only */
160 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
161 /** mask of TEXTURE_2D_BIT, etc */
162 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
163 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
164 GLuint EnvMode
; /* unit[0] only */
166 /** MESA_META_VERTEX */
167 struct gl_array_object
*ArrayObj
;
168 struct gl_buffer_object
*ArrayBufferObj
;
170 /** MESA_META_VIEWPORT */
171 GLfloat ViewportX
, ViewportY
, ViewportW
, ViewportH
;
172 GLclampd DepthNear
, DepthFar
;
174 /** MESA_META_CLAMP_FRAGMENT_COLOR */
175 GLenum ClampFragmentColor
;
177 /** MESA_META_CLAMP_VERTEX_COLOR */
178 GLenum ClampVertexColor
;
180 /** MESA_META_CONDITIONAL_RENDER */
181 struct gl_query_object
*CondRenderQuery
;
182 GLenum CondRenderMode
;
184 /** MESA_META_SELECT_FEEDBACK */
186 struct gl_selection Select
;
187 struct gl_feedback Feedback
;
189 /** MESA_META_MULTISAMPLE */
190 GLboolean MultisampleEnabled
;
192 /** MESA_META_FRAMEBUFFER_SRGB */
193 GLboolean sRGBEnabled
;
195 /** Miscellaneous (always disabled) */
197 GLboolean RasterDiscard
;
198 GLboolean TransformFeedbackNeedsResume
;
202 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
203 * This is currently shared by all the meta ops. But we could create a
204 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
209 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
210 GLsizei MinSize
; /**< Min texture size to allocate */
211 GLsizei MaxSize
; /**< Max possible texture size */
212 GLboolean NPOT
; /**< Non-power of two size OK? */
213 GLsizei Width
, Height
; /**< Current texture size */
215 GLfloat Sright
, Ttop
; /**< right, top texcoords */
220 * State for glBlitFramebufer()
228 GLuint RectShaderProg
;
229 struct temp_texture depthTex
;
234 * State for glClear()
244 GLuint IntegerShaderProg
;
245 GLint IntegerColorLocation
;
246 GLint IntegerLayerLocation
;
251 * State for glCopyPixels()
261 * State for glDrawPixels()
267 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
268 GLuint DepthFP
; /**< Fragment program for drawing depth images */
273 * State for glBitmap()
279 struct temp_texture Tex
; /**< separate texture from other meta ops */
283 * State for GLSL texture sampler which is used to generate fragment
284 * shader in _mesa_meta_generate_mipmap().
286 struct glsl_sampler
{
289 const char *texcoords
;
294 * State for _mesa_meta_generate_mipmap()
296 struct gen_mipmap_state
303 struct glsl_sampler sampler_1d
;
304 struct glsl_sampler sampler_2d
;
305 struct glsl_sampler sampler_3d
;
306 struct glsl_sampler sampler_cubemap
;
307 struct glsl_sampler sampler_1d_array
;
308 struct glsl_sampler sampler_2d_array
;
312 * State for texture decompression
314 struct decompress_state
317 GLuint VBO
, FBO
, RBO
, Sampler
;
322 * State for glDrawTex()
330 #define MAX_META_OPS_DEPTH 8
332 * All per-context meta state.
336 /** Stack of state saved during meta-ops */
337 struct save_state Save
[MAX_META_OPS_DEPTH
];
338 /** Save stack depth */
339 GLuint SaveStackDepth
;
341 struct temp_texture TempTex
;
343 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
344 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
345 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
346 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
347 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
348 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
349 struct decompress_state Decompress
; /**< For texture decompression */
350 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
353 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
354 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
355 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
356 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
357 struct gen_mipmap_state
*mipmap
);
358 static void meta_decompress_cleanup(struct decompress_state
*decompress
);
359 static void meta_drawpix_cleanup(struct drawpix_state
*drawpix
);
362 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
368 shader
= _mesa_CreateShaderObjectARB(target
);
369 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
370 _mesa_CompileShader(shader
);
372 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
376 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
378 _mesa_DeleteObjectARB(shader
);
384 _mesa_DeleteObjectARB(shader
);
388 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
390 "meta program compile failed:\n%s\n"
395 _mesa_DeleteObjectARB(shader
);
401 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
406 _mesa_LinkProgram(program
);
408 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
412 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
420 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
421 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
429 * Initialize meta-ops for a context.
430 * To be called once during context creation.
433 _mesa_meta_init(struct gl_context
*ctx
)
437 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
442 * Free context meta-op state.
443 * To be called once during context destruction.
446 _mesa_meta_free(struct gl_context
*ctx
)
448 GET_CURRENT_CONTEXT(old_context
);
449 _mesa_make_current(ctx
, NULL
, NULL
);
450 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
451 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
452 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
453 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
454 meta_decompress_cleanup(&ctx
->Meta
->Decompress
);
455 meta_drawpix_cleanup(&ctx
->Meta
->DrawPix
);
457 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
459 _mesa_make_current(NULL
, NULL
, NULL
);
466 * Enter meta state. This is like a light-weight version of glPushAttrib
467 * but it also resets most GL state back to default values.
469 * \param state bitmask of MESA_META_* flags indicating which attribute groups
470 * to save and reset to their defaults
473 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
475 struct save_state
*save
;
477 /* hope MAX_META_OPS_DEPTH is large enough */
478 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
480 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
481 memset(save
, 0, sizeof(*save
));
482 save
->SavedState
= state
;
484 /* Pausing transform feedback needs to be done early, or else we won't be
485 * able to change other state.
487 save
->TransformFeedbackNeedsResume
=
488 _mesa_is_xfb_active_and_unpaused(ctx
);
489 if (save
->TransformFeedbackNeedsResume
)
490 _mesa_PauseTransformFeedback();
492 /* After saving the current occlusion object, call EndQuery so that no
493 * occlusion querying will be active during the meta-operation.
495 if (state
& MESA_META_OCCLUSION_QUERY
) {
496 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
497 if (save
->CurrentOcclusionObject
)
498 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
501 if (state
& MESA_META_ALPHA_TEST
) {
502 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
503 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
504 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
505 if (ctx
->Color
.AlphaEnabled
)
506 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
509 if (state
& MESA_META_BLEND
) {
510 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
511 if (ctx
->Color
.BlendEnabled
) {
512 if (ctx
->Extensions
.EXT_draw_buffers2
) {
514 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
515 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
519 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
522 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
523 if (ctx
->Color
.ColorLogicOpEnabled
)
524 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
527 if (state
& MESA_META_COLOR_MASK
) {
528 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
529 sizeof(ctx
->Color
.ColorMask
));
530 if (!ctx
->Color
.ColorMask
[0][0] ||
531 !ctx
->Color
.ColorMask
[0][1] ||
532 !ctx
->Color
.ColorMask
[0][2] ||
533 !ctx
->Color
.ColorMask
[0][3])
534 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
537 if (state
& MESA_META_DEPTH_TEST
) {
538 save
->Depth
= ctx
->Depth
; /* struct copy */
540 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
543 if ((state
& MESA_META_FOG
)
544 && ctx
->API
!= API_OPENGL_CORE
545 && ctx
->API
!= API_OPENGLES2
) {
546 save
->Fog
= ctx
->Fog
.Enabled
;
547 if (ctx
->Fog
.Enabled
)
548 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
551 if (state
& MESA_META_PIXEL_STORE
) {
552 save
->Pack
= ctx
->Pack
;
553 save
->Unpack
= ctx
->Unpack
;
554 ctx
->Pack
= ctx
->DefaultPacking
;
555 ctx
->Unpack
= ctx
->DefaultPacking
;
558 if (state
& MESA_META_PIXEL_TRANSFER
) {
559 save
->RedScale
= ctx
->Pixel
.RedScale
;
560 save
->RedBias
= ctx
->Pixel
.RedBias
;
561 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
562 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
563 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
564 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
565 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
566 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
567 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
568 ctx
->Pixel
.RedScale
= 1.0F
;
569 ctx
->Pixel
.RedBias
= 0.0F
;
570 ctx
->Pixel
.GreenScale
= 1.0F
;
571 ctx
->Pixel
.GreenBias
= 0.0F
;
572 ctx
->Pixel
.BlueScale
= 1.0F
;
573 ctx
->Pixel
.BlueBias
= 0.0F
;
574 ctx
->Pixel
.AlphaScale
= 1.0F
;
575 ctx
->Pixel
.AlphaBias
= 0.0F
;
576 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
578 ctx
->NewState
|=_NEW_PIXEL
;
581 if (state
& MESA_META_RASTERIZATION
) {
582 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
583 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
584 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
585 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
586 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
587 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
588 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
589 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
590 if (ctx
->API
== API_OPENGL_COMPAT
) {
591 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
592 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
594 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
597 if (state
& MESA_META_SCISSOR
) {
598 save
->Scissor
= ctx
->Scissor
; /* struct copy */
599 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
602 if (state
& MESA_META_SHADER
) {
605 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
606 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
607 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
608 ctx
->VertexProgram
.Current
);
609 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
612 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
613 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
614 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
615 ctx
->FragmentProgram
.Current
);
616 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
619 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
620 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
621 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
624 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
625 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
626 ctx
->Shader
.CurrentProgram
[i
]);
628 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
629 ctx
->Shader
.ActiveProgram
);
634 if (state
& MESA_META_STENCIL_TEST
) {
635 save
->Stencil
= ctx
->Stencil
; /* struct copy */
636 if (ctx
->Stencil
.Enabled
)
637 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
638 /* NOTE: other stencil state not reset */
641 if (state
& MESA_META_TEXTURE
) {
644 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
645 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
646 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
648 /* Disable all texture units */
649 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
650 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
651 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
652 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
653 if (ctx
->Texture
.Unit
[u
].Enabled
||
654 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
655 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
656 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
657 if (ctx
->Extensions
.ARB_texture_cube_map
)
658 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
659 if (_mesa_is_gles(ctx
) &&
660 ctx
->Extensions
.OES_EGL_image_external
)
661 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
663 if (ctx
->API
== API_OPENGL_COMPAT
) {
664 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
665 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
666 if (ctx
->Extensions
.NV_texture_rectangle
)
667 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
668 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
669 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
670 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
671 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
673 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
679 /* save current texture objects for unit[0] only */
680 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
681 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
682 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
685 /* set defaults for unit[0] */
686 _mesa_ActiveTexture(GL_TEXTURE0
);
687 _mesa_ClientActiveTexture(GL_TEXTURE0
);
688 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
689 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
693 if (state
& MESA_META_TRANSFORM
) {
694 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
695 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
696 16 * sizeof(GLfloat
));
697 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
698 16 * sizeof(GLfloat
));
699 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
700 16 * sizeof(GLfloat
));
701 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
702 /* set 1:1 vertex:pixel coordinate transform */
703 _mesa_ActiveTexture(GL_TEXTURE0
);
704 _mesa_MatrixMode(GL_TEXTURE
);
705 _mesa_LoadIdentity();
706 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
707 _mesa_MatrixMode(GL_MODELVIEW
);
708 _mesa_LoadIdentity();
709 _mesa_MatrixMode(GL_PROJECTION
);
710 _mesa_LoadIdentity();
712 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
713 * This can occur when there is no draw buffer.
715 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
716 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
717 0.0, ctx
->DrawBuffer
->Height
,
721 if (state
& MESA_META_CLIP
) {
722 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
723 if (ctx
->Transform
.ClipPlanesEnabled
) {
725 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
726 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
731 if (state
& MESA_META_VERTEX
) {
732 /* save vertex array object state */
733 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
734 ctx
->Array
.ArrayObj
);
735 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
736 ctx
->Array
.ArrayBufferObj
);
737 /* set some default state? */
740 if (state
& MESA_META_VIEWPORT
) {
741 /* save viewport state */
742 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
743 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
744 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
745 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
746 /* set viewport to match window size */
747 if (ctx
->ViewportArray
[0].X
!= 0 ||
748 ctx
->ViewportArray
[0].Y
!= 0 ||
749 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
750 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
751 _mesa_set_viewport(ctx
, 0, 0, 0,
752 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
754 /* save depth range state */
755 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
756 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
757 /* set depth range to default */
758 _mesa_DepthRange(0.0, 1.0);
761 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
762 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
764 /* Generally in here we want to do clamping according to whether
765 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
766 * regardless of the internal implementation of the metaops.
768 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
769 ctx
->Extensions
.ARB_color_buffer_float
)
770 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
773 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
774 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
776 /* Generally in here we never want vertex color clamping --
777 * result clamping is only dependent on fragment clamping.
779 if (ctx
->Extensions
.ARB_color_buffer_float
)
780 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
783 if (state
& MESA_META_CONDITIONAL_RENDER
) {
784 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
785 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
787 if (ctx
->Query
.CondRenderQuery
)
788 _mesa_EndConditionalRender();
791 if (state
& MESA_META_SELECT_FEEDBACK
) {
792 save
->RenderMode
= ctx
->RenderMode
;
793 if (ctx
->RenderMode
== GL_SELECT
) {
794 save
->Select
= ctx
->Select
; /* struct copy */
795 _mesa_RenderMode(GL_RENDER
);
796 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
797 save
->Feedback
= ctx
->Feedback
; /* struct copy */
798 _mesa_RenderMode(GL_RENDER
);
802 if (state
& MESA_META_MULTISAMPLE
) {
803 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
804 if (ctx
->Multisample
.Enabled
)
805 _mesa_set_multisample(ctx
, GL_FALSE
);
808 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
809 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
810 if (ctx
->Color
.sRGBEnabled
)
811 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
816 save
->Lighting
= ctx
->Light
.Enabled
;
817 if (ctx
->Light
.Enabled
)
818 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
819 save
->RasterDiscard
= ctx
->RasterDiscard
;
820 if (ctx
->RasterDiscard
)
821 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
827 * Leave meta state. This is like a light-weight version of glPopAttrib().
830 _mesa_meta_end(struct gl_context
*ctx
)
832 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
833 const GLbitfield state
= save
->SavedState
;
836 /* After starting a new occlusion query, initialize the results to the
837 * values saved previously. The driver will then continue to increment
840 if (state
& MESA_META_OCCLUSION_QUERY
) {
841 if (save
->CurrentOcclusionObject
) {
842 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
843 save
->CurrentOcclusionObject
->Id
);
844 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
848 if (state
& MESA_META_ALPHA_TEST
) {
849 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
850 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
851 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
854 if (state
& MESA_META_BLEND
) {
855 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
856 if (ctx
->Extensions
.EXT_draw_buffers2
) {
858 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
859 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
863 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
866 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
867 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
870 if (state
& MESA_META_COLOR_MASK
) {
872 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
873 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
875 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
876 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
880 save
->ColorMask
[i
][0],
881 save
->ColorMask
[i
][1],
882 save
->ColorMask
[i
][2],
883 save
->ColorMask
[i
][3]);
889 if (state
& MESA_META_DEPTH_TEST
) {
890 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
891 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
892 _mesa_DepthFunc(save
->Depth
.Func
);
893 _mesa_DepthMask(save
->Depth
.Mask
);
896 if ((state
& MESA_META_FOG
)
897 && ctx
->API
!= API_OPENGL_CORE
898 && ctx
->API
!= API_OPENGLES2
) {
899 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
902 if (state
& MESA_META_PIXEL_STORE
) {
903 ctx
->Pack
= save
->Pack
;
904 ctx
->Unpack
= save
->Unpack
;
907 if (state
& MESA_META_PIXEL_TRANSFER
) {
908 ctx
->Pixel
.RedScale
= save
->RedScale
;
909 ctx
->Pixel
.RedBias
= save
->RedBias
;
910 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
911 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
912 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
913 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
914 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
915 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
916 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
918 ctx
->NewState
|=_NEW_PIXEL
;
921 if (state
& MESA_META_RASTERIZATION
) {
922 /* Core context requires that front and back mode be the same.
924 if (ctx
->API
== API_OPENGL_CORE
) {
925 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
927 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
928 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
930 if (ctx
->API
== API_OPENGL_COMPAT
) {
931 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
932 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
934 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
935 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
938 if (state
& MESA_META_SCISSOR
) {
941 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
942 _mesa_set_scissor(ctx
, i
,
943 save
->Scissor
.ScissorArray
[i
].X
,
944 save
->Scissor
.ScissorArray
[i
].Y
,
945 save
->Scissor
.ScissorArray
[i
].Width
,
946 save
->Scissor
.ScissorArray
[i
].Height
);
947 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
948 (save
->Scissor
.EnableFlags
>> i
) & 1);
952 if (state
& MESA_META_SHADER
) {
953 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
954 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
955 save
->VertexProgramEnabled
);
956 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
957 save
->VertexProgram
);
958 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
961 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
962 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
963 save
->FragmentProgramEnabled
);
964 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
965 save
->FragmentProgram
);
966 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
969 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
970 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
971 save
->ATIFragmentShaderEnabled
);
974 if (ctx
->Extensions
.ARB_vertex_shader
) {
975 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
,
976 save
->Shader
[MESA_SHADER_VERTEX
]);
979 if (_mesa_has_geometry_shaders(ctx
))
980 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
981 save
->Shader
[MESA_SHADER_GEOMETRY
]);
983 if (ctx
->Extensions
.ARB_fragment_shader
)
984 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
985 save
->Shader
[MESA_SHADER_FRAGMENT
]);
987 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
990 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
991 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
992 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
995 if (state
& MESA_META_STENCIL_TEST
) {
996 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
998 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
999 _mesa_ClearStencil(stencil
->Clear
);
1000 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
1001 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1002 stencil
->TestTwoSide
);
1003 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1004 ? GL_BACK
: GL_FRONT
);
1007 _mesa_StencilFuncSeparate(GL_FRONT
,
1008 stencil
->Function
[0],
1010 stencil
->ValueMask
[0]);
1011 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1012 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1013 stencil
->ZFailFunc
[0],
1014 stencil
->ZPassFunc
[0]);
1016 _mesa_StencilFuncSeparate(GL_BACK
,
1017 stencil
->Function
[1],
1019 stencil
->ValueMask
[1]);
1020 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1021 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1022 stencil
->ZFailFunc
[1],
1023 stencil
->ZPassFunc
[1]);
1026 if (state
& MESA_META_TEXTURE
) {
1029 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1031 /* restore texenv for unit[0] */
1032 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1033 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1036 /* restore texture objects for unit[0] only */
1037 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1038 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1039 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1040 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1041 save
->CurrentTexture
[tgt
]);
1043 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1046 /* Restore fixed function texture enables, texgen */
1047 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1048 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1049 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1050 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1051 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1054 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1055 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1056 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1061 /* restore current unit state */
1062 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1063 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1066 if (state
& MESA_META_TRANSFORM
) {
1067 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1068 _mesa_ActiveTexture(GL_TEXTURE0
);
1069 _mesa_MatrixMode(GL_TEXTURE
);
1070 _mesa_LoadMatrixf(save
->TextureMatrix
);
1071 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1073 _mesa_MatrixMode(GL_MODELVIEW
);
1074 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1076 _mesa_MatrixMode(GL_PROJECTION
);
1077 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1079 _mesa_MatrixMode(save
->MatrixMode
);
1082 if (state
& MESA_META_CLIP
) {
1083 if (save
->ClipPlanesEnabled
) {
1085 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1086 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1087 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1093 if (state
& MESA_META_VERTEX
) {
1094 /* restore vertex buffer object */
1095 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1096 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1098 /* restore vertex array object */
1099 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1100 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1103 if (state
& MESA_META_VIEWPORT
) {
1104 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1105 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1106 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1107 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1108 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1109 save
->ViewportW
, save
->ViewportH
);
1111 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1114 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1115 ctx
->Extensions
.ARB_color_buffer_float
) {
1116 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1119 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1120 ctx
->Extensions
.ARB_color_buffer_float
) {
1121 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1124 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1125 if (save
->CondRenderQuery
)
1126 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1127 save
->CondRenderMode
);
1130 if (state
& MESA_META_SELECT_FEEDBACK
) {
1131 if (save
->RenderMode
== GL_SELECT
) {
1132 _mesa_RenderMode(GL_SELECT
);
1133 ctx
->Select
= save
->Select
;
1134 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1135 _mesa_RenderMode(GL_FEEDBACK
);
1136 ctx
->Feedback
= save
->Feedback
;
1140 if (state
& MESA_META_MULTISAMPLE
) {
1141 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1142 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1145 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1146 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1147 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1151 if (save
->Lighting
) {
1152 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1154 if (save
->RasterDiscard
) {
1155 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1157 if (save
->TransformFeedbackNeedsResume
)
1158 _mesa_ResumeTransformFeedback();
1160 ctx
->Meta
->SaveStackDepth
--;
1165 * Determine whether Mesa is currently in a meta state.
1168 _mesa_meta_in_progress(struct gl_context
*ctx
)
1170 return ctx
->Meta
->SaveStackDepth
!= 0;
1175 * Convert Z from a normalized value in the range [0, 1] to an object-space
1176 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1177 * default/identity ortho projection results in the original Z value.
1178 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1179 * value comes from the clear value or raster position.
1181 static INLINE GLfloat
1182 invert_z(GLfloat normZ
)
1184 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1190 * One-time init for a temp_texture object.
1191 * Choose tex target, compute max tex size, etc.
1194 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1196 /* prefer texture rectangle */
1197 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1198 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1199 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1200 tex
->NPOT
= GL_TRUE
;
1203 /* use 2D texture, NPOT if possible */
1204 tex
->Target
= GL_TEXTURE_2D
;
1205 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1206 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1208 tex
->MinSize
= 16; /* 16 x 16 at least */
1209 assert(tex
->MaxSize
> 0);
1211 _mesa_GenTextures(1, &tex
->TexObj
);
1215 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1219 _mesa_DeleteTextures(1, &tex
->TexObj
);
1225 * Return pointer to temp_texture info for non-bitmap ops.
1226 * This does some one-time init if needed.
1228 static struct temp_texture
*
1229 get_temp_texture(struct gl_context
*ctx
)
1231 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1234 init_temp_texture(ctx
, tex
);
1242 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1243 * We use a separate texture for bitmaps to reduce texture
1244 * allocation/deallocation.
1246 static struct temp_texture
*
1247 get_bitmap_temp_texture(struct gl_context
*ctx
)
1249 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1252 init_temp_texture(ctx
, tex
);
1259 * Return pointer to depth temp_texture.
1260 * This does some one-time init if needed.
1262 static struct temp_texture
*
1263 get_temp_depth_texture(struct gl_context
*ctx
)
1265 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1268 init_temp_texture(ctx
, tex
);
1275 * Compute the width/height of texture needed to draw an image of the
1276 * given size. Return a flag indicating whether the current texture
1277 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1278 * allocated (glTexImage2D).
1279 * Also, compute s/t texcoords for drawing.
1281 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1284 alloc_texture(struct temp_texture
*tex
,
1285 GLsizei width
, GLsizei height
, GLenum intFormat
)
1287 GLboolean newTex
= GL_FALSE
;
1289 ASSERT(width
<= tex
->MaxSize
);
1290 ASSERT(height
<= tex
->MaxSize
);
1292 if (width
> tex
->Width
||
1293 height
> tex
->Height
||
1294 intFormat
!= tex
->IntFormat
) {
1295 /* alloc new texture (larger or different format) */
1298 /* use non-power of two size */
1299 tex
->Width
= MAX2(tex
->MinSize
, width
);
1300 tex
->Height
= MAX2(tex
->MinSize
, height
);
1303 /* find power of two size */
1305 w
= h
= tex
->MinSize
;
1314 tex
->IntFormat
= intFormat
;
1319 /* compute texcoords */
1320 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1321 tex
->Sright
= (GLfloat
) width
;
1322 tex
->Ttop
= (GLfloat
) height
;
1325 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1326 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1334 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1337 setup_copypix_texture(struct gl_context
*ctx
,
1338 struct temp_texture
*tex
,
1340 GLint srcX
, GLint srcY
,
1341 GLsizei width
, GLsizei height
, GLenum intFormat
,
1344 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1345 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1346 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1347 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1348 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1350 /* copy framebuffer image to texture */
1352 /* create new tex image */
1353 if (tex
->Width
== width
&& tex
->Height
== height
) {
1354 /* create new tex with framebuffer data */
1355 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1356 srcX
, srcY
, width
, height
, 0);
1359 /* create empty texture */
1360 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1361 tex
->Width
, tex
->Height
, 0,
1362 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1364 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1365 0, 0, srcX
, srcY
, width
, height
);
1369 /* replace existing tex image */
1370 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1371 0, 0, srcX
, srcY
, width
, height
);
1377 * Setup/load texture for glDrawPixels.
1380 setup_drawpix_texture(struct gl_context
*ctx
,
1381 struct temp_texture
*tex
,
1383 GLenum texIntFormat
,
1384 GLsizei width
, GLsizei height
,
1385 GLenum format
, GLenum type
,
1386 const GLvoid
*pixels
)
1388 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1389 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1390 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1391 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1392 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1394 /* copy pixel data to texture */
1396 /* create new tex image */
1397 if (tex
->Width
== width
&& tex
->Height
== height
) {
1398 /* create new tex and load image data */
1399 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1400 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1403 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1405 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1406 ctx
->Unpack
.BufferObj
);
1407 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1408 /* create empty texture */
1409 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1410 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1411 if (save_unpack_obj
!= NULL
)
1412 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1413 save_unpack_obj
->Name
);
1415 _mesa_TexSubImage2D(tex
->Target
, 0,
1416 0, 0, width
, height
, format
, type
, pixels
);
1420 /* replace existing tex image */
1421 _mesa_TexSubImage2D(tex
->Target
, 0,
1422 0, 0, width
, height
, format
, type
, pixels
);
1429 * One-time init for drawing depth pixels.
1432 init_blit_depth_pixels(struct gl_context
*ctx
)
1434 static const char *program
=
1436 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1439 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1440 struct temp_texture
*tex
= get_temp_texture(ctx
);
1441 const char *texTarget
;
1443 assert(blit
->DepthFP
== 0);
1445 /* replace %s with "RECT" or "2D" */
1446 assert(strlen(program
) + 4 < sizeof(program2
));
1447 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1451 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1453 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1454 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1455 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1456 strlen(program2
), (const GLubyte
*) program2
);
1460 setup_ff_blit_framebuffer(struct gl_context
*ctx
,
1461 struct blit_state
*blit
)
1466 struct vertex verts
[4];
1468 if (blit
->ArrayObj
== 0) {
1469 /* one-time setup */
1471 /* create vertex array object */
1472 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1473 _mesa_BindVertexArray(blit
->ArrayObj
);
1475 /* create vertex array buffer */
1476 _mesa_GenBuffers(1, &blit
->VBO
);
1477 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1478 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1479 NULL
, GL_DYNAMIC_DRAW_ARB
);
1481 /* setup vertex arrays */
1482 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1483 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1484 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1485 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1488 /* setup projection matrix */
1489 _mesa_MatrixMode(GL_PROJECTION
);
1490 _mesa_LoadIdentity();
1491 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1496 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1497 struct blit_state
*blit
,
1503 struct vertex verts
[4];
1504 const char *vs_source
;
1509 GLboolean texture_2d
= (target
== GL_TEXTURE_2D
);
1511 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1512 assert(_mesa_is_desktop_gl(ctx
) || texture_2d
);
1514 /* Check if already initialized */
1515 if (blit
->ArrayObj
== 0) {
1517 /* create vertex array object */
1518 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1519 _mesa_BindVertexArray(blit
->ArrayObj
);
1521 /* create vertex array buffer */
1522 _mesa_GenBuffers(1, &blit
->VBO
);
1523 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1524 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1525 NULL
, GL_DYNAMIC_DRAW_ARB
);
1527 /* setup vertex arrays */
1528 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
1529 sizeof(struct vertex
), OFFSET(x
));
1530 _mesa_VertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
,
1531 sizeof(struct vertex
), OFFSET(s
));
1533 _mesa_EnableVertexAttribArray(0);
1534 _mesa_EnableVertexAttribArray(1);
1537 /* Generate a relevant fragment shader program for the texture target */
1538 if ((target
== GL_TEXTURE_2D
&& blit
->ShaderProg
!= 0) ||
1539 (target
== GL_TEXTURE_RECTANGLE
&& blit
->RectShaderProg
!= 0)) {
1543 mem_ctx
= ralloc_context(NULL
);
1545 if (ctx
->Const
.GLSLVersion
< 130) {
1547 "attribute vec2 position;\n"
1548 "attribute vec2 textureCoords;\n"
1549 "varying vec2 texCoords;\n"
1552 " texCoords = textureCoords;\n"
1553 " gl_Position = vec4(position, 0.0, 1.0);\n"
1556 fs_source
= ralloc_asprintf(mem_ctx
,
1558 "precision highp float;\n"
1560 "uniform %s texSampler;\n"
1561 "varying vec2 texCoords;\n"
1564 " gl_FragColor = %s(texSampler, texCoords);\n"
1565 " gl_FragDepth = gl_FragColor.r;\n"
1567 texture_2d
? "sampler2D" : "sampler2DRect",
1568 texture_2d
? "texture2D" : "texture2DRect");
1571 vs_source
= ralloc_asprintf(mem_ctx
,
1573 "in vec2 position;\n"
1574 "in vec2 textureCoords;\n"
1575 "out vec2 texCoords;\n"
1578 " texCoords = textureCoords;\n"
1579 " gl_Position = vec4(position, 0.0, 1.0);\n"
1581 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1582 fs_source
= ralloc_asprintf(mem_ctx
,
1585 "precision highp float;\n"
1587 "uniform %s texSampler;\n"
1588 "in vec2 texCoords;\n"
1589 "out vec4 out_color;\n"
1593 " out_color = %s(texSampler, texCoords);\n"
1594 " gl_FragDepth = out_color.r;\n"
1596 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
1597 texture_2d
? "sampler2D" : "sampler2DRect",
1598 texture_2d
? "texture" : "texture2DRect");
1601 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
1602 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
1604 ShaderProg
= _mesa_CreateProgramObjectARB();
1605 _mesa_AttachShader(ShaderProg
, fs
);
1606 _mesa_DeleteObjectARB(fs
);
1607 _mesa_AttachShader(ShaderProg
, vs
);
1608 _mesa_DeleteObjectARB(vs
);
1609 _mesa_BindAttribLocation(ShaderProg
, 0, "position");
1610 _mesa_BindAttribLocation(ShaderProg
, 1, "texcoords");
1611 link_program_with_debug(ctx
, ShaderProg
);
1612 ralloc_free(mem_ctx
);
1614 blit
->ShaderProg
= ShaderProg
;
1616 blit
->RectShaderProg
= ShaderProg
;
1620 * Try to do a glBlitFramebuffer using no-copy texturing.
1621 * We can do this when the src renderbuffer is actually a texture.
1622 * But if the src buffer == dst buffer we cannot do this.
1624 * \return new buffer mask indicating the buffers left to blit using the
1628 blitframebuffer_texture(struct gl_context
*ctx
,
1629 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1630 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1631 GLbitfield mask
, GLenum filter
, GLint flipX
,
1632 GLint flipY
, GLboolean glsl_version
)
1634 if (mask
& GL_COLOR_BUFFER_BIT
) {
1635 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1636 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1637 const struct gl_renderbuffer_attachment
*drawAtt
;
1638 const struct gl_renderbuffer_attachment
*readAtt
=
1639 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1641 if (readAtt
&& readAtt
->Texture
) {
1642 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1643 const GLint dstX
= MIN2(dstX0
, dstX1
);
1644 const GLint dstY
= MIN2(dstY0
, dstY1
);
1645 const GLint dstW
= abs(dstX1
- dstX0
);
1646 const GLint dstH
= abs(dstY1
- dstY0
);
1647 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1648 const GLuint srcLevel
= readAtt
->TextureLevel
;
1649 const GLint baseLevelSave
= texObj
->BaseLevel
;
1650 const GLint maxLevelSave
= texObj
->MaxLevel
;
1651 const GLenum target
= texObj
->Target
;
1652 GLuint sampler
, samplerSave
=
1653 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1654 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1657 /* Iterate through all draw buffers */
1658 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1659 int idx
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1662 drawAtt
= &drawFb
->Attachment
[idx
];
1664 if (drawAtt
->Texture
== readAtt
->Texture
) {
1665 /* Can't use same texture as both the source and dest. We need
1666 * to handle overlapping blits and besides, some hw may not
1673 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1674 /* Can't handle other texture types at this time */
1678 /* Choose between glsl version and fixed function version of
1679 * BlitFramebuffer function.
1682 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1683 if (target
== GL_TEXTURE_2D
)
1684 _mesa_UseProgram(blit
->ShaderProg
);
1686 _mesa_UseProgram(blit
->RectShaderProg
);
1689 setup_ff_blit_framebuffer(ctx
, &ctx
->Meta
->Blit
);
1692 _mesa_BindVertexArray(blit
->ArrayObj
);
1693 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1695 _mesa_GenSamplers(1, &sampler
);
1696 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1699 printf("Blit from texture!\n");
1700 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1701 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1704 /* Prepare src texture state */
1705 _mesa_BindTexture(target
, texObj
->Name
);
1706 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1707 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1708 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1709 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1710 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1712 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1713 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1715 /* Always do our blits with no sRGB decode or encode. Note that
1716 * GL_FRAMEBUFFER_SRGB has already been disabled by
1717 * _mesa_meta_begin().
1719 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1720 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1721 GL_SKIP_DECODE_EXT
);
1724 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1725 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1726 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1729 /* Prepare vertex data (the VBO was previously created and bound) */
1734 struct vertex verts
[4];
1735 GLfloat s0
, t0
, s1
, t1
;
1737 if (target
== GL_TEXTURE_2D
) {
1738 const struct gl_texture_image
*texImage
1739 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1740 s0
= srcX0
/ (float) texImage
->Width
;
1741 s1
= srcX1
/ (float) texImage
->Width
;
1742 t0
= srcY0
/ (float) texImage
->Height
;
1743 t1
= srcY1
/ (float) texImage
->Height
;
1746 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1753 /* setup vertex positions */
1754 verts
[0].x
= -1.0F
* flipX
;
1755 verts
[0].y
= -1.0F
* flipY
;
1756 verts
[1].x
= 1.0F
* flipX
;
1757 verts
[1].y
= -1.0F
* flipY
;
1758 verts
[2].x
= 1.0F
* flipX
;
1759 verts
[2].y
= 1.0F
* flipY
;
1760 verts
[3].x
= -1.0F
* flipX
;
1761 verts
[3].y
= 1.0F
* flipY
;
1772 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1775 /* setup viewport */
1776 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1777 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1778 _mesa_DepthMask(GL_FALSE
);
1779 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1781 /* Restore texture object state, the texture binding will
1782 * be restored by _mesa_meta_end().
1784 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1785 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1786 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1789 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1790 _mesa_DeleteSamplers(1, &sampler
);
1792 /* Done with color buffer */
1793 mask
&= ~GL_COLOR_BUFFER_BIT
;
1802 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1803 * of texture mapping and polygon rendering.
1806 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1807 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1808 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1809 GLbitfield mask
, GLenum filter
)
1811 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1812 struct temp_texture
*tex
= get_temp_texture(ctx
);
1813 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1814 const GLsizei maxTexSize
= tex
->MaxSize
;
1815 const GLint srcX
= MIN2(srcX0
, srcX1
);
1816 const GLint srcY
= MIN2(srcY0
, srcY1
);
1817 const GLint srcW
= abs(srcX1
- srcX0
);
1818 const GLint srcH
= abs(srcY1
- srcY0
);
1819 const GLint dstX
= MIN2(dstX0
, dstX1
);
1820 const GLint dstY
= MIN2(dstY0
, dstY1
);
1821 const GLint dstW
= abs(dstX1
- dstX0
);
1822 const GLint dstH
= abs(dstY1
- dstY0
);
1823 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1824 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1825 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1826 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1827 const GLint flipX
= srcFlipX
* dstFlipX
;
1828 const GLint flipY
= srcFlipY
* dstFlipY
;
1833 struct vertex verts
[4];
1835 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1836 ctx
->Extensions
.ARB_fragment_shader
&&
1837 (ctx
->API
!= API_OPENGLES
);
1839 /* In addition to falling back if the blit size is larger than the maximum
1840 * texture size, fallback if the source is multisampled. This fallback can
1841 * be removed once Mesa gets support ARB_texture_multisample.
1843 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1844 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1845 /* XXX avoid this fallback */
1846 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1847 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1851 /* only scissor effects blit so save/clear all other relevant state */
1852 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1854 /* Try faster, direct texture approach first */
1855 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1856 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1857 dstFlipX
, dstFlipY
, use_glsl_version
);
1859 _mesa_meta_end(ctx
);
1863 /* Choose between glsl version and fixed function version of
1864 * BlitFramebuffer function.
1866 if (use_glsl_version
) {
1867 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1868 if (tex
->Target
== GL_TEXTURE_2D
)
1869 _mesa_UseProgram(blit
->ShaderProg
);
1871 _mesa_UseProgram(blit
->RectShaderProg
);
1874 setup_ff_blit_framebuffer(ctx
, blit
);
1877 _mesa_BindVertexArray(blit
->ArrayObj
);
1878 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1880 /* Continue with "normal" approach which involves copying the src rect
1881 * into a temporary texture and is "blitted" by drawing a textured quad.
1884 /* setup vertex positions */
1885 verts
[0].x
= -1.0F
* flipX
;
1886 verts
[0].y
= -1.0F
* flipY
;
1887 verts
[1].x
= 1.0F
* flipX
;
1888 verts
[1].y
= -1.0F
* flipY
;
1889 verts
[2].x
= 1.0F
* flipX
;
1890 verts
[2].y
= 1.0F
* flipY
;
1891 verts
[3].x
= -1.0F
* flipX
;
1892 verts
[3].y
= 1.0F
* flipY
;
1896 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1899 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1900 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1902 if (mask
& GL_COLOR_BUFFER_BIT
) {
1903 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1904 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1905 const GLenum rb_base_format
=
1906 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1908 /* Using the exact source rectangle to create the texture does incorrect
1909 * linear filtering along the edges. So, allocate the texture extended along
1910 * edges by one pixel in x, y directions.
1912 newTex
= alloc_texture(tex
, srcW
+ 2, srcH
+ 2, rb_base_format
);
1913 setup_copypix_texture(ctx
, tex
, newTex
,
1914 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1915 rb_base_format
, filter
);
1916 /* texcoords (after texture allocation!) */
1920 verts
[1].s
= tex
->Sright
- 1.0F
;
1922 verts
[2].s
= tex
->Sright
- 1.0F
;
1923 verts
[2].t
= tex
->Ttop
- 1.0F
;
1925 verts
[3].t
= tex
->Ttop
- 1.0F
;
1927 /* upload new vertex data */
1928 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1931 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1932 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1933 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1934 _mesa_DepthMask(GL_FALSE
);
1935 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1936 mask
&= ~GL_COLOR_BUFFER_BIT
;
1939 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1940 _mesa_is_desktop_gl(ctx
) &&
1941 ctx
->Extensions
.ARB_depth_texture
&&
1942 ctx
->Extensions
.ARB_fragment_program
) {
1944 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1948 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1949 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1950 GL_UNSIGNED_INT
, tmp
);
1951 setup_drawpix_texture(ctx
, depthTex
, newTex
, GL_DEPTH_COMPONENT
,
1952 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1953 GL_UNSIGNED_INT
, tmp
);
1955 /* texcoords (after texture allocation!) */
1959 verts
[1].s
= depthTex
->Sright
;
1961 verts
[2].s
= depthTex
->Sright
;
1962 verts
[2].t
= depthTex
->Ttop
;
1964 verts
[3].t
= depthTex
->Ttop
;
1966 /* upload new vertex data */
1967 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1971 init_blit_depth_pixels(ctx
);
1973 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1974 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1975 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1976 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1977 _mesa_DepthFunc(GL_ALWAYS
);
1978 _mesa_DepthMask(GL_TRUE
);
1980 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1981 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1982 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1983 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1989 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1990 /* XXX can't easily do stencil */
1993 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1994 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1996 _mesa_meta_end(ctx
);
1999 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
2000 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
2005 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
2007 if (blit
->ArrayObj
) {
2008 _mesa_DeleteVertexArrays(1, &blit
->ArrayObj
);
2010 _mesa_DeleteBuffers(1, &blit
->VBO
);
2013 if (blit
->DepthFP
) {
2014 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
2018 _mesa_DeleteObjectARB(blit
->ShaderProg
);
2019 blit
->ShaderProg
= 0;
2020 _mesa_DeleteObjectARB(blit
->RectShaderProg
);
2021 blit
->RectShaderProg
= 0;
2023 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
2024 blit
->depthTex
.TexObj
= 0;
2029 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2032 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2034 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2036 GLfloat x
, y
, z
, r
, g
, b
, a
;
2038 struct vertex verts
[4];
2039 /* save all state but scissor, pixel pack/unpack */
2040 GLbitfield metaSave
= (MESA_META_ALL
-
2042 MESA_META_PIXEL_STORE
-
2043 MESA_META_CONDITIONAL_RENDER
-
2044 MESA_META_FRAMEBUFFER_SRGB
);
2045 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2047 if (buffers
& BUFFER_BITS_COLOR
) {
2048 /* if clearing color buffers, don't save/restore colormask */
2049 metaSave
-= MESA_META_COLOR_MASK
;
2052 _mesa_meta_begin(ctx
, metaSave
);
2054 if (clear
->ArrayObj
== 0) {
2055 /* one-time setup */
2057 /* create vertex array object */
2058 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2059 _mesa_BindVertexArray(clear
->ArrayObj
);
2061 /* create vertex array buffer */
2062 _mesa_GenBuffers(1, &clear
->VBO
);
2063 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2065 /* setup vertex arrays */
2066 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2067 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2068 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2069 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2072 _mesa_BindVertexArray(clear
->ArrayObj
);
2073 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2076 /* GL_COLOR_BUFFER_BIT */
2077 if (buffers
& BUFFER_BITS_COLOR
) {
2078 /* leave colormask, glDrawBuffer state as-is */
2080 /* Clears never have the color clamped. */
2081 if (ctx
->Extensions
.ARB_color_buffer_float
)
2082 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2085 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2086 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2089 /* GL_DEPTH_BUFFER_BIT */
2090 if (buffers
& BUFFER_BIT_DEPTH
) {
2091 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2092 _mesa_DepthFunc(GL_ALWAYS
);
2093 _mesa_DepthMask(GL_TRUE
);
2096 assert(!ctx
->Depth
.Test
);
2099 /* GL_STENCIL_BUFFER_BIT */
2100 if (buffers
& BUFFER_BIT_STENCIL
) {
2101 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2102 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2103 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2104 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2105 ctx
->Stencil
.Clear
& stencilMax
,
2106 ctx
->Stencil
.WriteMask
[0]);
2109 assert(!ctx
->Stencil
.Enabled
);
2112 /* vertex positions/colors */
2114 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
2115 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
2116 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
2117 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
2118 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
2135 for (i
= 0; i
< 4; i
++) {
2136 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
2137 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
2138 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
2139 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
2142 /* upload new vertex data */
2143 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2144 GL_DYNAMIC_DRAW_ARB
);
2148 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2150 _mesa_meta_end(ctx
);
2154 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
2156 const char *vs_source
=
2157 "attribute vec4 position;\n"
2160 " gl_Position = position;\n"
2162 const char *gs_source
=
2164 "layout(triangles) in;\n"
2165 "layout(triangle_strip, max_vertices = 4) out;\n"
2166 "uniform int layer;\n"
2169 " for (int i = 0; i < 3; i++) {\n"
2170 " gl_Layer = layer;\n"
2171 " gl_Position = gl_in[i].gl_Position;\n"
2175 const char *fs_source
=
2177 "precision highp float;\n"
2179 "uniform vec4 color;\n"
2182 " gl_FragColor = color;\n"
2184 GLuint vs
, gs
= 0, fs
;
2185 bool has_integer_textures
;
2187 if (clear
->ArrayObj
!= 0)
2190 /* create vertex array object */
2191 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2192 _mesa_BindVertexArray(clear
->ArrayObj
);
2194 /* create vertex array buffer */
2195 _mesa_GenBuffers(1, &clear
->VBO
);
2196 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2198 /* setup vertex arrays */
2199 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
2200 _mesa_EnableVertexAttribArray(0);
2202 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
2203 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
2204 _mesa_CompileShader(vs
);
2206 if (_mesa_has_geometry_shaders(ctx
)) {
2207 gs
= _mesa_CreateShaderObjectARB(GL_GEOMETRY_SHADER
);
2208 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
2209 _mesa_CompileShader(gs
);
2212 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
2213 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
2214 _mesa_CompileShader(fs
);
2216 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
2217 _mesa_AttachShader(clear
->ShaderProg
, fs
);
2218 _mesa_DeleteObjectARB(fs
);
2220 _mesa_AttachShader(clear
->ShaderProg
, gs
);
2221 _mesa_AttachShader(clear
->ShaderProg
, vs
);
2222 _mesa_DeleteObjectARB(vs
);
2223 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
2224 _mesa_LinkProgram(clear
->ShaderProg
);
2226 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2229 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2233 has_integer_textures
= _mesa_is_gles3(ctx
) ||
2234 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
2236 if (has_integer_textures
) {
2237 void *shader_source_mem_ctx
= ralloc_context(NULL
);
2238 const char *vs_int_source
=
2239 ralloc_asprintf(shader_source_mem_ctx
,
2241 "in vec4 position;\n"
2244 " gl_Position = position;\n"
2246 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2247 const char *fs_int_source
=
2248 ralloc_asprintf(shader_source_mem_ctx
,
2251 "precision highp float;\n"
2253 "uniform ivec4 color;\n"
2254 "out ivec4 out_color;\n"
2258 " out_color = color;\n"
2260 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2262 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
2263 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
2264 ralloc_free(shader_source_mem_ctx
);
2266 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
2267 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
2268 _mesa_DeleteObjectARB(fs
);
2270 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
2271 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
2272 _mesa_DeleteObjectARB(vs
);
2273 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
2275 /* Note that user-defined out attributes get automatically assigned
2276 * locations starting from 0, so we don't need to explicitly
2277 * BindFragDataLocation to 0.
2280 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
2282 clear
->IntegerColorLocation
=
2283 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2285 clear
->IntegerLayerLocation
=
2286 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
2290 _mesa_DeleteObjectARB(gs
);
2294 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
2296 if (clear
->ArrayObj
== 0)
2298 _mesa_DeleteVertexArrays(1, &clear
->ArrayObj
);
2299 clear
->ArrayObj
= 0;
2300 _mesa_DeleteBuffers(1, &clear
->VBO
);
2302 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2303 clear
->ShaderProg
= 0;
2305 if (clear
->IntegerShaderProg
) {
2306 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2307 clear
->IntegerShaderProg
= 0;
2312 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2315 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2317 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2318 GLbitfield metaSave
;
2319 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2320 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2321 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2322 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2323 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2324 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2325 const float z
= -invert_z(ctx
->Depth
.Clear
);
2330 metaSave
= (MESA_META_ALPHA_TEST
|
2332 MESA_META_DEPTH_TEST
|
2333 MESA_META_RASTERIZATION
|
2335 MESA_META_STENCIL_TEST
|
2337 MESA_META_VIEWPORT
|
2339 MESA_META_CLAMP_FRAGMENT_COLOR
|
2340 MESA_META_MULTISAMPLE
|
2341 MESA_META_OCCLUSION_QUERY
);
2343 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2344 /* We'll use colormask to disable color writes. Otherwise,
2345 * respect color mask
2347 metaSave
|= MESA_META_COLOR_MASK
;
2350 _mesa_meta_begin(ctx
, metaSave
);
2352 meta_glsl_clear_init(ctx
, clear
);
2354 if (fb
->_IntegerColor
) {
2355 _mesa_UseProgram(clear
->IntegerShaderProg
);
2356 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2357 ctx
->Color
.ClearColor
.i
);
2359 _mesa_UseProgram(clear
->ShaderProg
);
2360 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2361 ctx
->Color
.ClearColor
.f
);
2364 _mesa_BindVertexArray(clear
->ArrayObj
);
2365 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2367 /* GL_COLOR_BUFFER_BIT */
2368 if (buffers
& BUFFER_BITS_COLOR
) {
2369 /* leave colormask, glDrawBuffer state as-is */
2371 /* Clears never have the color clamped. */
2372 if (ctx
->Extensions
.ARB_color_buffer_float
)
2373 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2376 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2377 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2380 /* GL_DEPTH_BUFFER_BIT */
2381 if (buffers
& BUFFER_BIT_DEPTH
) {
2382 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2383 _mesa_DepthFunc(GL_ALWAYS
);
2384 _mesa_DepthMask(GL_TRUE
);
2387 assert(!ctx
->Depth
.Test
);
2390 /* GL_STENCIL_BUFFER_BIT */
2391 if (buffers
& BUFFER_BIT_STENCIL
) {
2392 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2393 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2394 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2395 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2396 ctx
->Stencil
.Clear
& stencilMax
,
2397 ctx
->Stencil
.WriteMask
[0]);
2400 assert(!ctx
->Stencil
.Enabled
);
2403 /* vertex positions */
2417 /* upload new vertex data */
2418 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2419 GL_DYNAMIC_DRAW_ARB
);
2422 if (fb
->MaxNumLayers
> 0) {
2424 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
2425 if (fb
->_IntegerColor
)
2426 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
2428 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
2429 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2432 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2435 _mesa_meta_end(ctx
);
2439 * Meta implementation of ctx->Driver.CopyPixels() in terms
2440 * of texture mapping and polygon rendering and GLSL shaders.
2443 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2444 GLsizei width
, GLsizei height
,
2445 GLint dstX
, GLint dstY
, GLenum type
)
2447 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2448 struct temp_texture
*tex
= get_temp_texture(ctx
);
2450 GLfloat x
, y
, z
, s
, t
;
2452 struct vertex verts
[4];
2454 GLenum intFormat
= GL_RGBA
;
2456 if (type
!= GL_COLOR
||
2457 ctx
->_ImageTransferState
||
2459 width
> tex
->MaxSize
||
2460 height
> tex
->MaxSize
) {
2461 /* XXX avoid this fallback */
2462 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2466 /* Most GL state applies to glCopyPixels, but a there's a few things
2467 * we need to override:
2469 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2472 MESA_META_TRANSFORM
|
2475 MESA_META_VIEWPORT
));
2477 if (copypix
->ArrayObj
== 0) {
2478 /* one-time setup */
2480 /* create vertex array object */
2481 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2482 _mesa_BindVertexArray(copypix
->ArrayObj
);
2484 /* create vertex array buffer */
2485 _mesa_GenBuffers(1, ©pix
->VBO
);
2486 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2487 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2488 NULL
, GL_DYNAMIC_DRAW_ARB
);
2490 /* setup vertex arrays */
2491 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2492 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2493 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2494 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2497 _mesa_BindVertexArray(copypix
->ArrayObj
);
2498 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2501 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2503 /* vertex positions, texcoords (after texture allocation!) */
2505 const GLfloat dstX0
= (GLfloat
) dstX
;
2506 const GLfloat dstY0
= (GLfloat
) dstY
;
2507 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2508 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2509 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2519 verts
[1].s
= tex
->Sright
;
2524 verts
[2].s
= tex
->Sright
;
2525 verts
[2].t
= tex
->Ttop
;
2530 verts
[3].t
= tex
->Ttop
;
2532 /* upload new vertex data */
2533 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2536 /* Alloc/setup texture */
2537 setup_copypix_texture(ctx
, tex
, newTex
, srcX
, srcY
, width
, height
,
2538 GL_RGBA
, GL_NEAREST
);
2540 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2542 /* draw textured quad */
2543 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2545 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2547 _mesa_meta_end(ctx
);
2551 meta_drawpix_cleanup(struct drawpix_state
*drawpix
)
2553 if (drawpix
->ArrayObj
!= 0) {
2554 _mesa_DeleteVertexArrays(1, &drawpix
->ArrayObj
);
2555 drawpix
->ArrayObj
= 0;
2558 if (drawpix
->StencilFP
!= 0) {
2559 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
2560 drawpix
->StencilFP
= 0;
2563 if (drawpix
->DepthFP
!= 0) {
2564 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
2565 drawpix
->DepthFP
= 0;
2570 * When the glDrawPixels() image size is greater than the max rectangle
2571 * texture size we use this function to break the glDrawPixels() image
2572 * into tiles which fit into the max texture size.
2575 tiled_draw_pixels(struct gl_context
*ctx
,
2577 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2578 GLenum format
, GLenum type
,
2579 const struct gl_pixelstore_attrib
*unpack
,
2580 const GLvoid
*pixels
)
2582 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2585 if (tileUnpack
.RowLength
== 0)
2586 tileUnpack
.RowLength
= width
;
2588 for (i
= 0; i
< width
; i
+= tileSize
) {
2589 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2590 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2592 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2594 for (j
= 0; j
< height
; j
+= tileSize
) {
2595 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2596 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2598 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2600 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2601 format
, type
, &tileUnpack
, pixels
);
2608 * One-time init for drawing stencil pixels.
2611 init_draw_stencil_pixels(struct gl_context
*ctx
)
2613 /* This program is run eight times, once for each stencil bit.
2614 * The stencil values to draw are found in an 8-bit alpha texture.
2615 * We read the texture/stencil value and test if bit 'b' is set.
2616 * If the bit is not set, use KIL to kill the fragment.
2617 * Finally, we use the stencil test to update the stencil buffer.
2619 * The basic algorithm for checking if a bit is set is:
2620 * if (is_odd(value / (1 << bit)))
2621 * result is one (or non-zero).
2624 * The program parameter contains three values:
2625 * parm.x = 255 / (1 << bit)
2629 static const char *program
=
2631 "PARAM parm = program.local[0]; \n"
2633 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2634 "# t = t * 255 / bit \n"
2635 "MUL t.x, t.a, parm.x; \n"
2638 "SUB t.x, t.x, t.y; \n"
2640 "MUL t.x, t.x, parm.y; \n"
2641 "# t = fract(t.x) \n"
2642 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2643 "# t.x = (t.x == 0 ? 1 : 0) \n"
2644 "SGE t.x, -t.x, parm.z; \n"
2646 "# for debug only \n"
2647 "#MOV result.color, t.x; \n"
2649 char program2
[1000];
2650 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2651 struct temp_texture
*tex
= get_temp_texture(ctx
);
2652 const char *texTarget
;
2654 assert(drawpix
->StencilFP
== 0);
2656 /* replace %s with "RECT" or "2D" */
2657 assert(strlen(program
) + 4 < sizeof(program2
));
2658 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2662 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2664 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2665 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2666 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2667 strlen(program2
), (const GLubyte
*) program2
);
2672 * One-time init for drawing depth pixels.
2675 init_draw_depth_pixels(struct gl_context
*ctx
)
2677 static const char *program
=
2679 "PARAM color = program.local[0]; \n"
2680 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2681 "MOV result.color, color; \n"
2684 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2685 struct temp_texture
*tex
= get_temp_texture(ctx
);
2686 const char *texTarget
;
2688 assert(drawpix
->DepthFP
== 0);
2690 /* replace %s with "RECT" or "2D" */
2691 assert(strlen(program
) + 4 < sizeof(program2
));
2692 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2696 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2698 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2699 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2700 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2701 strlen(program2
), (const GLubyte
*) program2
);
2706 * Meta implementation of ctx->Driver.DrawPixels() in terms
2707 * of texture mapping and polygon rendering.
2710 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2711 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2712 GLenum format
, GLenum type
,
2713 const struct gl_pixelstore_attrib
*unpack
,
2714 const GLvoid
*pixels
)
2716 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2717 struct temp_texture
*tex
= get_temp_texture(ctx
);
2718 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2719 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2721 GLfloat x
, y
, z
, s
, t
;
2723 struct vertex verts
[4];
2724 GLenum texIntFormat
;
2725 GLboolean fallback
, newTex
;
2726 GLbitfield metaExtraSave
= 0x0;
2730 * Determine if we can do the glDrawPixels with texture mapping.
2732 fallback
= GL_FALSE
;
2733 if (ctx
->Fog
.Enabled
) {
2737 if (_mesa_is_color_format(format
)) {
2738 /* use more compact format when possible */
2739 /* XXX disable special case for GL_LUMINANCE for now to work around
2740 * apparent i965 driver bug (see bug #23670).
2742 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2743 texIntFormat
= format
;
2745 texIntFormat
= GL_RGBA
;
2747 /* If we're not supposed to clamp the resulting color, then just
2748 * promote our texture to fully float. We could do better by
2749 * just going for the matching set of channels, in floating
2752 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2753 ctx
->Extensions
.ARB_texture_float
)
2754 texIntFormat
= GL_RGBA32F
;
2756 else if (_mesa_is_stencil_format(format
)) {
2757 if (ctx
->Extensions
.ARB_fragment_program
&&
2758 ctx
->Pixel
.IndexShift
== 0 &&
2759 ctx
->Pixel
.IndexOffset
== 0 &&
2760 type
== GL_UNSIGNED_BYTE
) {
2761 /* We'll store stencil as alpha. This only works for GLubyte
2762 * image data because of how incoming values are mapped to alpha
2765 texIntFormat
= GL_ALPHA
;
2766 metaExtraSave
= (MESA_META_COLOR_MASK
|
2767 MESA_META_DEPTH_TEST
|
2768 MESA_META_PIXEL_TRANSFER
|
2770 MESA_META_STENCIL_TEST
);
2776 else if (_mesa_is_depth_format(format
)) {
2777 if (ctx
->Extensions
.ARB_depth_texture
&&
2778 ctx
->Extensions
.ARB_fragment_program
) {
2779 texIntFormat
= GL_DEPTH_COMPONENT
;
2780 metaExtraSave
= (MESA_META_SHADER
);
2791 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2792 format
, type
, unpack
, pixels
);
2797 * Check image size against max texture size, draw as tiles if needed.
2799 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2800 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2801 format
, type
, unpack
, pixels
);
2805 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2806 * but a there's a few things we need to override:
2808 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2811 MESA_META_TRANSFORM
|
2814 MESA_META_VIEWPORT
|
2817 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2819 /* vertex positions, texcoords (after texture allocation!) */
2821 const GLfloat x0
= (GLfloat
) x
;
2822 const GLfloat y0
= (GLfloat
) y
;
2823 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2824 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2825 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2835 verts
[1].s
= tex
->Sright
;
2840 verts
[2].s
= tex
->Sright
;
2841 verts
[2].t
= tex
->Ttop
;
2846 verts
[3].t
= tex
->Ttop
;
2849 if (drawpix
->ArrayObj
== 0) {
2850 /* one-time setup: create vertex array object */
2851 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2853 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2855 /* create vertex array buffer */
2856 _mesa_GenBuffers(1, &vbo
);
2857 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2858 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2859 verts
, GL_DYNAMIC_DRAW_ARB
);
2861 /* setup vertex arrays */
2862 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2863 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2864 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2865 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2867 /* set given unpack params */
2868 ctx
->Unpack
= *unpack
;
2870 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2872 if (_mesa_is_stencil_format(format
)) {
2873 /* Drawing stencil */
2876 if (!drawpix
->StencilFP
)
2877 init_draw_stencil_pixels(ctx
);
2879 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2880 GL_ALPHA
, type
, pixels
);
2882 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2884 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2886 /* set all stencil bits to 0 */
2887 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2888 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2889 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2891 /* set stencil bits to 1 where needed */
2892 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2894 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2895 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2897 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2898 const GLuint mask
= 1 << bit
;
2899 if (mask
& origStencilMask
) {
2900 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2901 _mesa_StencilMask(mask
);
2903 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2904 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2906 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2910 else if (_mesa_is_depth_format(format
)) {
2912 if (!drawpix
->DepthFP
)
2913 init_draw_depth_pixels(ctx
);
2915 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2916 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2918 /* polygon color = current raster color */
2919 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2920 ctx
->Current
.RasterColor
);
2922 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2923 format
, type
, pixels
);
2925 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2929 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2930 format
, type
, pixels
);
2931 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2934 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2936 _mesa_DeleteBuffers(1, &vbo
);
2938 /* restore unpack params */
2939 ctx
->Unpack
= unpackSave
;
2941 _mesa_meta_end(ctx
);
2945 alpha_test_raster_color(struct gl_context
*ctx
)
2947 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2948 GLfloat ref
= ctx
->Color
.AlphaRef
;
2950 switch (ctx
->Color
.AlphaFunc
) {
2956 return alpha
== ref
;
2958 return alpha
<= ref
;
2962 return alpha
!= ref
;
2964 return alpha
>= ref
;
2974 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2975 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2976 * tracker would improve performance a lot.
2979 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2980 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2981 const struct gl_pixelstore_attrib
*unpack
,
2982 const GLubyte
*bitmap1
)
2984 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2985 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2986 const GLenum texIntFormat
= GL_ALPHA
;
2987 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2990 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2992 struct vertex verts
[4];
2997 * Check if swrast fallback is needed.
2999 if (ctx
->_ImageTransferState
||
3000 ctx
->FragmentProgram
._Enabled
||
3002 ctx
->Texture
._EnabledUnits
||
3003 width
> tex
->MaxSize
||
3004 height
> tex
->MaxSize
) {
3005 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
3009 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
3012 /* Most GL state applies to glBitmap (like blending, stencil, etc),
3013 * but a there's a few things we need to override:
3015 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
3016 MESA_META_PIXEL_STORE
|
3017 MESA_META_RASTERIZATION
|
3020 MESA_META_TRANSFORM
|
3023 MESA_META_VIEWPORT
));
3025 if (bitmap
->ArrayObj
== 0) {
3026 /* one-time setup */
3028 /* create vertex array object */
3029 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
3030 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
3032 /* create vertex array buffer */
3033 _mesa_GenBuffers(1, &bitmap
->VBO
);
3034 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3035 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3036 NULL
, GL_DYNAMIC_DRAW_ARB
);
3038 /* setup vertex arrays */
3039 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3040 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
3041 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
3042 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3043 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3044 _mesa_EnableClientState(GL_COLOR_ARRAY
);
3047 _mesa_BindVertexArray(bitmap
->ArrayObj
);
3048 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3051 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
3053 /* vertex positions, texcoords, colors (after texture allocation!) */
3055 const GLfloat x0
= (GLfloat
) x
;
3056 const GLfloat y0
= (GLfloat
) y
;
3057 const GLfloat x1
= (GLfloat
) (x
+ width
);
3058 const GLfloat y1
= (GLfloat
) (y
+ height
);
3059 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
3070 verts
[1].s
= tex
->Sright
;
3075 verts
[2].s
= tex
->Sright
;
3076 verts
[2].t
= tex
->Ttop
;
3081 verts
[3].t
= tex
->Ttop
;
3083 for (i
= 0; i
< 4; i
++) {
3084 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
3085 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
3086 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
3087 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
3090 /* upload new vertex data */
3091 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3094 /* choose different foreground/background alpha values */
3095 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
3096 bg
= (fg
> 127 ? 0 : 255);
3098 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
3100 _mesa_meta_end(ctx
);
3104 bitmap8
= malloc(width
* height
);
3106 memset(bitmap8
, bg
, width
* height
);
3107 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
3108 bitmap8
, width
, fg
);
3110 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
3112 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
3113 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
3115 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
3116 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
3118 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3120 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
3125 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
3127 _mesa_meta_end(ctx
);
3132 * Check if the call to _mesa_meta_GenerateMipmap() will require a
3133 * software fallback. The fallback path will require that the texture
3134 * images are mapped.
3135 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
3138 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
3139 struct gl_texture_object
*texObj
)
3141 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3142 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3143 struct gl_texture_image
*baseImage
;
3147 /* check for fallbacks */
3148 if (target
== GL_TEXTURE_3D
||
3149 target
== GL_TEXTURE_1D_ARRAY
||
3150 target
== GL_TEXTURE_2D_ARRAY
) {
3151 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3152 "glGenerateMipmap() to %s target\n",
3153 _mesa_lookup_enum_by_nr(target
));
3157 srcLevel
= texObj
->BaseLevel
;
3158 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
3160 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3161 "glGenerateMipmap() couldn't find base teximage\n");
3165 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
3166 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3167 "glGenerateMipmap() with %s format\n",
3168 _mesa_get_format_name(baseImage
->TexFormat
));
3172 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
3173 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3174 /* The texture format is sRGB but we can't turn off sRGB->linear
3175 * texture sample conversion. So we won't be able to generate the
3176 * right colors when rendering. Need to use a fallback.
3178 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3179 "glGenerateMipmap() of sRGB texture without "
3185 * Test that we can actually render in the texture's format.
3188 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3189 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3191 if (target
== GL_TEXTURE_1D
) {
3192 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3193 GL_COLOR_ATTACHMENT0_EXT
,
3194 target
, texObj
->Name
, srcLevel
);
3197 /* other work is needed to enable 3D mipmap generation */
3198 else if (target
== GL_TEXTURE_3D
) {
3200 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3201 GL_COLOR_ATTACHMENT0_EXT
,
3202 target
, texObj
->Name
, srcLevel
, zoffset
);
3207 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3208 GL_COLOR_ATTACHMENT0_EXT
,
3209 target
, texObj
->Name
, srcLevel
);
3212 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3214 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3216 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3217 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3218 "glGenerateMipmap() got incomplete FBO\n");
3227 * Compute the texture coordinates for the four vertices of a quad for
3228 * drawing a 2D texture image or slice of a cube/3D texture.
3229 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
3230 * \param slice slice of a 1D/2D array texture or 3D texture
3231 * \param width width of the texture image
3232 * \param height height of the texture image
3233 * \param coords0/1/2/3 returns the computed texcoords
3236 setup_texture_coords(GLenum faceTarget
,
3246 static const GLfloat st
[4][2] = {
3247 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
3252 switch (faceTarget
) {
3256 case GL_TEXTURE_2D_ARRAY
:
3257 if (faceTarget
== GL_TEXTURE_3D
) {
3258 assert(slice
< depth
);
3260 r
= (slice
+ 0.5f
) / depth
;
3262 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
3266 coords0
[0] = 0.0F
; /* s */
3267 coords0
[1] = 0.0F
; /* t */
3268 coords0
[2] = r
; /* r */
3279 case GL_TEXTURE_RECTANGLE_ARB
:
3280 coords0
[0] = 0.0F
; /* s */
3281 coords0
[1] = 0.0F
; /* t */
3282 coords0
[2] = 0.0F
; /* r */
3283 coords1
[0] = (float) width
;
3286 coords2
[0] = (float) width
;
3287 coords2
[1] = (float) height
;
3290 coords3
[1] = (float) height
;
3293 case GL_TEXTURE_1D_ARRAY
:
3294 coords0
[0] = 0.0F
; /* s */
3295 coords0
[1] = (float) slice
; /* t */
3296 coords0
[2] = 0.0F
; /* r */
3298 coords1
[1] = (float) slice
;
3301 coords2
[1] = (float) slice
;
3304 coords3
[1] = (float) slice
;
3308 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3311 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3312 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3313 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3314 /* loop over quad verts */
3315 for (i
= 0; i
< 4; i
++) {
3316 /* Compute sc = +/-scale and tc = +/-scale.
3317 * Not +/-1 to avoid cube face selection ambiguity near the edges,
3318 * though that can still sometimes happen with this scale factor...
3320 const GLfloat scale
= 0.9999f
;
3321 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
3322 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
3342 switch (faceTarget
) {
3343 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3348 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3353 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3358 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3363 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3368 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3379 assert(0 && "unexpected target in meta setup_texture_coords()");
3385 setup_ff_generate_mipmap(struct gl_context
*ctx
,
3386 struct gen_mipmap_state
*mipmap
)
3389 GLfloat x
, y
, tex
[3];
3392 if (mipmap
->ArrayObj
== 0) {
3393 /* one-time setup */
3394 /* create vertex array object */
3395 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3396 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
3398 /* create vertex array buffer */
3399 _mesa_GenBuffers(1, &mipmap
->VBO
);
3400 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3401 /* setup vertex arrays */
3402 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3403 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3404 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3405 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3408 /* setup projection matrix */
3409 _mesa_MatrixMode(GL_PROJECTION
);
3410 _mesa_LoadIdentity();
3411 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3415 static struct glsl_sampler
*
3416 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3420 mipmap
->sampler_1d
.type
= "sampler1D";
3421 mipmap
->sampler_1d
.func
= "texture1D";
3422 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3423 return &mipmap
->sampler_1d
;
3425 mipmap
->sampler_2d
.type
= "sampler2D";
3426 mipmap
->sampler_2d
.func
= "texture2D";
3427 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3428 return &mipmap
->sampler_2d
;
3430 /* Code for mipmap generation with 3D textures is not used yet.
3431 * It's a sw fallback.
3433 mipmap
->sampler_3d
.type
= "sampler3D";
3434 mipmap
->sampler_3d
.func
= "texture3D";
3435 mipmap
->sampler_3d
.texcoords
= "texCoords";
3436 return &mipmap
->sampler_3d
;
3437 case GL_TEXTURE_CUBE_MAP
:
3438 mipmap
->sampler_cubemap
.type
= "samplerCube";
3439 mipmap
->sampler_cubemap
.func
= "textureCube";
3440 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3441 return &mipmap
->sampler_cubemap
;
3442 case GL_TEXTURE_1D_ARRAY
:
3443 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3444 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3445 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3446 return &mipmap
->sampler_1d_array
;
3447 case GL_TEXTURE_2D_ARRAY
:
3448 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3449 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3450 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3451 return &mipmap
->sampler_2d_array
;
3453 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3454 " setup_texture_sampler()\n", target
);
3461 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3462 struct gen_mipmap_state
*mipmap
,
3466 GLfloat x
, y
, tex
[3];
3468 struct glsl_sampler
*sampler
;
3469 const char *vs_source
;
3474 /* Check if already initialized */
3475 if (mipmap
->ArrayObj
== 0) {
3477 /* create vertex array object */
3478 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3479 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3481 /* create vertex array buffer */
3482 _mesa_GenBuffers(1, &mipmap
->VBO
);
3483 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3485 /* setup vertex arrays */
3486 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3487 sizeof(struct vertex
), OFFSET(x
));
3488 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3489 sizeof(struct vertex
), OFFSET(tex
));
3490 _mesa_EnableVertexAttribArray(0);
3491 _mesa_EnableVertexAttribArray(1);
3494 /* Generate a fragment shader program appropriate for the texture target */
3495 sampler
= setup_texture_sampler(target
, mipmap
);
3496 assert(sampler
!= NULL
);
3497 if (sampler
->shader_prog
!= 0) {
3498 mipmap
->ShaderProg
= sampler
->shader_prog
;
3502 mem_ctx
= ralloc_context(NULL
);
3504 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3506 "attribute vec2 position;\n"
3507 "attribute vec3 textureCoords;\n"
3508 "varying vec3 texCoords;\n"
3511 " texCoords = textureCoords;\n"
3512 " gl_Position = vec4(position, 0.0, 1.0);\n"
3515 fs_source
= ralloc_asprintf(mem_ctx
,
3516 "#extension GL_EXT_texture_array : enable\n"
3518 "precision highp float;\n"
3520 "uniform %s texSampler;\n"
3521 "varying vec3 texCoords;\n"
3524 " gl_FragColor = %s(texSampler, %s);\n"
3527 sampler
->func
, sampler
->texcoords
);
3530 vs_source
= ralloc_asprintf(mem_ctx
,
3532 "in vec2 position;\n"
3533 "in vec3 textureCoords;\n"
3534 "out vec3 texCoords;\n"
3537 " texCoords = textureCoords;\n"
3538 " gl_Position = vec4(position, 0.0, 1.0);\n"
3540 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
3541 fs_source
= ralloc_asprintf(mem_ctx
,
3544 "precision highp float;\n"
3546 "uniform %s texSampler;\n"
3547 "in vec3 texCoords;\n"
3548 "out vec4 out_color;\n"
3552 " out_color = texture(texSampler, %s);\n"
3554 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
3556 sampler
->texcoords
);
3559 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3560 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3562 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3563 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3564 _mesa_DeleteObjectARB(fs
);
3565 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3566 _mesa_DeleteObjectARB(vs
);
3567 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3568 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3569 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3570 sampler
->shader_prog
= mipmap
->ShaderProg
;
3571 ralloc_free(mem_ctx
);
3576 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3577 struct gen_mipmap_state
*mipmap
)
3579 if (mipmap
->ArrayObj
== 0)
3581 _mesa_DeleteVertexArrays(1, &mipmap
->ArrayObj
);
3582 mipmap
->ArrayObj
= 0;
3583 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3586 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3587 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3588 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3589 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3590 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3591 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3593 mipmap
->sampler_1d
.shader_prog
= 0;
3594 mipmap
->sampler_2d
.shader_prog
= 0;
3595 mipmap
->sampler_3d
.shader_prog
= 0;
3596 mipmap
->sampler_cubemap
.shader_prog
= 0;
3597 mipmap
->sampler_1d_array
.shader_prog
= 0;
3598 mipmap
->sampler_2d_array
.shader_prog
= 0;
3603 * Called via ctx->Driver.GenerateMipmap()
3604 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3608 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3609 struct gl_texture_object
*texObj
)
3611 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3613 GLfloat x
, y
, tex
[3];
3615 struct vertex verts
[4];
3616 const GLuint baseLevel
= texObj
->BaseLevel
;
3617 const GLuint maxLevel
= texObj
->MaxLevel
;
3618 const GLint maxLevelSave
= texObj
->MaxLevel
;
3619 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3620 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3621 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3622 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3623 ctx
->Extensions
.ARB_fragment_shader
&&
3624 (ctx
->API
!= API_OPENGLES
);
3627 const GLint slice
= 0;
3630 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3631 _mesa_generate_mipmap(ctx
, target
, texObj
);
3635 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3636 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3637 faceTarget
= target
;
3638 target
= GL_TEXTURE_CUBE_MAP
;
3641 faceTarget
= target
;
3644 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3646 /* Choose between glsl version and fixed function version of
3647 * GenerateMipmap function.
3649 if (use_glsl_version
) {
3650 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3651 _mesa_UseProgram(mipmap
->ShaderProg
);
3654 setup_ff_generate_mipmap(ctx
, mipmap
);
3655 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3658 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3659 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3661 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3662 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3664 if (currentTexUnitSave
!= 0)
3665 _mesa_BindTexture(target
, texObj
->Name
);
3668 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3671 if (!mipmap
->Sampler
) {
3672 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3673 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3675 _mesa_SamplerParameteri(mipmap
->Sampler
,
3676 GL_TEXTURE_MIN_FILTER
,
3677 GL_LINEAR_MIPMAP_LINEAR
);
3678 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3679 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3680 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3681 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3683 /* We don't want to encode or decode sRGB values; treat them as linear.
3684 * This is not technically correct for GLES3 but we don't get any API
3685 * error at the moment.
3687 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3688 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3689 GL_SKIP_DECODE_EXT
);
3693 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3696 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3698 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3699 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3701 assert(!genMipmapSave
);
3703 /* Setup texture coordinates */
3704 setup_texture_coords(faceTarget
,
3706 0, 0, 1, /* width, height never used here */
3712 /* setup vertex positions */
3722 /* upload vertex data */
3723 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3724 verts
, GL_DYNAMIC_DRAW_ARB
);
3726 /* texture is already locked, unlock now */
3727 _mesa_unlock_texture(ctx
, texObj
);
3729 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3730 const struct gl_texture_image
*srcImage
;
3731 const GLuint srcLevel
= dstLevel
- 1;
3732 GLsizei srcWidth
, srcHeight
, srcDepth
;
3733 GLsizei dstWidth
, dstHeight
, dstDepth
;
3736 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3737 assert(srcImage
->Border
== 0);
3740 srcWidth
= srcImage
->Width
;
3741 srcHeight
= srcImage
->Height
;
3742 srcDepth
= srcImage
->Depth
;
3745 dstWidth
= MAX2(1, srcWidth
/ 2);
3746 dstHeight
= MAX2(1, srcHeight
/ 2);
3747 dstDepth
= MAX2(1, srcDepth
/ 2);
3749 if (dstWidth
== srcImage
->Width
&&
3750 dstHeight
== srcImage
->Height
&&
3751 dstDepth
== srcImage
->Depth
) {
3756 /* Allocate storage for the destination mipmap image(s) */
3758 /* Set MaxLevel large enough to hold the new level when we allocate it */
3759 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3761 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3762 dstWidth
, dstHeight
, dstDepth
,
3764 srcImage
->InternalFormat
,
3765 srcImage
->TexFormat
)) {
3766 /* All done. We either ran out of memory or we would go beyond the
3767 * last valid level of an immutable texture if we continued.
3772 /* limit minification to src level */
3773 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3775 /* Set to draw into the current dstLevel */
3776 if (target
== GL_TEXTURE_1D
) {
3777 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3778 GL_COLOR_ATTACHMENT0_EXT
,
3783 else if (target
== GL_TEXTURE_3D
) {
3784 GLint zoffset
= 0; /* XXX unfinished */
3785 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3786 GL_COLOR_ATTACHMENT0_EXT
,
3793 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3794 GL_COLOR_ATTACHMENT0_EXT
,
3800 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3803 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3804 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3805 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3806 "_mesa_meta_GenerateMipmap()");
3810 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3811 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3813 /* setup viewport */
3814 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
3816 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3819 _mesa_lock_texture(ctx
, texObj
); /* relock */
3821 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3823 _mesa_meta_end(ctx
);
3825 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3827 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3829 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3834 * Determine the GL data type to use for the temporary image read with
3835 * ReadPixels() and passed to Tex[Sub]Image().
3838 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
3842 baseFormat
= _mesa_get_format_base_format(format
);
3844 switch (baseFormat
) {
3851 case GL_LUMINANCE_ALPHA
:
3853 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3854 return GL_UNSIGNED_BYTE
;
3855 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3856 return GL_UNSIGNED_SHORT
;
3858 GLenum datatype
= _mesa_get_format_datatype(format
);
3859 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3863 case GL_DEPTH_COMPONENT
: {
3864 GLenum datatype
= _mesa_get_format_datatype(format
);
3865 if (datatype
== GL_FLOAT
)
3868 return GL_UNSIGNED_INT
;
3870 case GL_DEPTH_STENCIL
: {
3871 GLenum datatype
= _mesa_get_format_datatype(format
);
3872 if (datatype
== GL_FLOAT
)
3873 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3875 return GL_UNSIGNED_INT_24_8
;
3878 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3886 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3887 * Have to be careful with locking and meta state for pixel transfer.
3890 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3891 struct gl_texture_image
*texImage
,
3892 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3893 struct gl_renderbuffer
*rb
,
3895 GLsizei width
, GLsizei height
)
3897 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3898 GLenum format
, type
;
3902 /* Choose format/type for temporary image buffer */
3903 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3904 if (format
== GL_LUMINANCE
||
3905 format
== GL_LUMINANCE_ALPHA
||
3906 format
== GL_INTENSITY
) {
3907 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3908 * temp image buffer because glReadPixels will do L=R+G+B which is
3909 * not what we want (should be L=R).
3914 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3915 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3916 format
= _mesa_base_format_to_integer_format(format
);
3918 bpp
= _mesa_bytes_per_pixel(format
, type
);
3920 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3925 * Alloc image buffer (XXX could use a PBO)
3927 buf
= malloc(width
* height
* bpp
);
3929 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3933 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3936 * Read image from framebuffer (disable pixel transfer ops)
3938 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3939 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3940 format
, type
, &ctx
->Pack
, buf
);
3941 _mesa_meta_end(ctx
);
3943 _mesa_update_state(ctx
); /* to update pixel transfer state */
3946 * Store texture data (with pixel transfer ops)
3948 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3950 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3951 assert(yoffset
== 0);
3952 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3953 xoffset
, zoffset
, 0, width
, 1, 1,
3954 format
, type
, buf
, &ctx
->Unpack
);
3956 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3957 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3958 format
, type
, buf
, &ctx
->Unpack
);
3961 _mesa_meta_end(ctx
);
3963 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3970 meta_decompress_cleanup(struct decompress_state
*decompress
)
3972 if (decompress
->FBO
!= 0) {
3973 _mesa_DeleteFramebuffers(1, &decompress
->FBO
);
3974 _mesa_DeleteRenderbuffers(1, &decompress
->RBO
);
3977 if (decompress
->ArrayObj
!= 0) {
3978 _mesa_DeleteVertexArrays(1, &decompress
->ArrayObj
);
3979 _mesa_DeleteBuffers(1, &decompress
->VBO
);
3982 if (decompress
->Sampler
!= 0)
3983 _mesa_DeleteSamplers(1, &decompress
->Sampler
);
3985 memset(decompress
, 0, sizeof(*decompress
));
3989 * Decompress a texture image by drawing a quad with the compressed
3990 * texture and reading the pixels out of the color buffer.
3991 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3992 * \param destFormat format, ala glReadPixels
3993 * \param destType type, ala glReadPixels
3994 * \param dest destination buffer
3995 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3998 decompress_texture_image(struct gl_context
*ctx
,
3999 struct gl_texture_image
*texImage
,
4001 GLenum destFormat
, GLenum destType
,
4004 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
4005 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4006 const GLint width
= texImage
->Width
;
4007 const GLint height
= texImage
->Height
;
4008 const GLint depth
= texImage
->Height
;
4009 const GLenum target
= texObj
->Target
;
4012 GLfloat x
, y
, tex
[3];
4014 struct vertex verts
[4];
4015 GLuint fboDrawSave
, fboReadSave
;
4020 assert(target
== GL_TEXTURE_3D
||
4021 target
== GL_TEXTURE_2D_ARRAY
);
4024 if (target
== GL_TEXTURE_CUBE_MAP
) {
4025 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
4028 faceTarget
= target
;
4031 /* save fbo bindings (not saved by _mesa_meta_begin()) */
4032 fboDrawSave
= ctx
->DrawBuffer
->Name
;
4033 fboReadSave
= ctx
->ReadBuffer
->Name
;
4034 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
4036 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
4038 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
4039 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
4041 /* Create/bind FBO/renderbuffer */
4042 if (decompress
->FBO
== 0) {
4043 _mesa_GenFramebuffers(1, &decompress
->FBO
);
4044 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
4045 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4046 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4047 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
4048 GL_COLOR_ATTACHMENT0_EXT
,
4049 GL_RENDERBUFFER_EXT
,
4053 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4056 /* alloc dest surface */
4057 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
4058 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4059 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
4061 decompress
->Width
= width
;
4062 decompress
->Height
= height
;
4065 /* setup VBO data */
4066 if (decompress
->ArrayObj
== 0) {
4067 /* create vertex array object */
4068 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
4069 _mesa_BindVertexArray(decompress
->ArrayObj
);
4071 /* create vertex array buffer */
4072 _mesa_GenBuffers(1, &decompress
->VBO
);
4073 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4074 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4075 NULL
, GL_DYNAMIC_DRAW_ARB
);
4077 /* setup vertex arrays */
4078 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4079 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
4080 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4081 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4084 _mesa_BindVertexArray(decompress
->ArrayObj
);
4085 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4088 if (!decompress
->Sampler
) {
4089 _mesa_GenSamplers(1, &decompress
->Sampler
);
4090 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4091 /* nearest filtering */
4092 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4093 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4094 /* No sRGB decode or encode.*/
4095 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
4096 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
4097 GL_SKIP_DECODE_EXT
);
4101 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4104 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
4110 /* setup vertex positions */
4116 verts
[2].y
= height
;
4118 verts
[3].y
= height
;
4120 _mesa_MatrixMode(GL_PROJECTION
);
4121 _mesa_LoadIdentity();
4122 _mesa_Ortho(0.0, width
, 0.0, height
, -1.0, 1.0);
4123 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
4125 /* upload new vertex data */
4126 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4128 /* setup texture state */
4129 _mesa_BindTexture(target
, texObj
->Name
);
4130 _mesa_set_enable(ctx
, target
, GL_TRUE
);
4133 /* save texture object state */
4134 const GLint baseLevelSave
= texObj
->BaseLevel
;
4135 const GLint maxLevelSave
= texObj
->MaxLevel
;
4137 /* restrict sampling to the texture level of interest */
4138 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4139 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
4140 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
4143 /* render quad w/ texture into renderbuffer */
4144 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4146 /* Restore texture object state, the texture binding will
4147 * be restored by _mesa_meta_end().
4149 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4150 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
4151 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
4156 /* read pixels from renderbuffer */
4158 GLenum baseTexFormat
= texImage
->_BaseFormat
;
4159 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
4161 /* The pixel transfer state will be set to default values at this point
4162 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
4163 * turned off (as required by glGetTexImage) but we need to handle some
4164 * special cases. In particular, single-channel texture values are
4165 * returned as red and two-channel texture values are returned as
4168 if ((baseTexFormat
== GL_LUMINANCE
||
4169 baseTexFormat
== GL_LUMINANCE_ALPHA
||
4170 baseTexFormat
== GL_INTENSITY
) ||
4171 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
4172 * luminance then we need to return L=tex(R).
4174 ((baseTexFormat
== GL_RGBA
||
4175 baseTexFormat
== GL_RGB
||
4176 baseTexFormat
== GL_RG
) &&
4177 (destBaseFormat
== GL_LUMINANCE
||
4178 destBaseFormat
== GL_LUMINANCE_ALPHA
||
4179 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
4180 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
4181 /* Green and blue must be zero */
4182 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
4183 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
4186 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
4189 /* disable texture unit */
4190 _mesa_set_enable(ctx
, target
, GL_FALSE
);
4192 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
4194 _mesa_meta_end(ctx
);
4196 /* restore fbo bindings */
4197 if (fboDrawSave
== fboReadSave
) {
4198 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
4201 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
4202 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
4204 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
4209 * This is just a wrapper around _mesa_get_tex_image() and
4210 * decompress_texture_image(). Meta functions should not be directly called
4214 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
4215 GLenum format
, GLenum type
, GLvoid
*pixels
,
4216 struct gl_texture_image
*texImage
)
4218 /* We can only use the decompress-with-blit method here if the texels are
4219 * unsigned, normalized values. We could handle signed and unnormalized
4220 * with floating point renderbuffers...
4222 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
4223 _mesa_get_format_datatype(texImage
->TexFormat
)
4224 == GL_UNSIGNED_NORMALIZED
) {
4225 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4227 /* Need to unlock the texture here to prevent deadlock... */
4228 _mesa_unlock_texture(ctx
, texObj
);
4229 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
4231 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
4232 /* Setup pixel packing. SkipPixels and SkipRows will be applied
4233 * in the decompress_texture_image() function's call to
4234 * glReadPixels but we need to compute the dest slice's address
4235 * here (according to SkipImages and ImageHeight).
4237 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
4238 packing
.SkipPixels
= 0;
4239 packing
.SkipRows
= 0;
4240 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
4241 texImage
->Height
, format
, type
,
4247 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
4249 /* ... and relock it */
4250 _mesa_lock_texture(ctx
, texObj
);
4253 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
4259 * Meta implementation of ctx->Driver.DrawTex() in terms
4260 * of polygon rendering.
4263 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
4264 GLfloat width
, GLfloat height
)
4266 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
4268 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
4270 struct vertex verts
[4];
4273 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
4275 MESA_META_TRANSFORM
|
4277 MESA_META_VIEWPORT
));
4279 if (drawtex
->ArrayObj
== 0) {
4280 /* one-time setup */
4281 GLint active_texture
;
4283 /* create vertex array object */
4284 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
4285 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4287 /* create vertex array buffer */
4288 _mesa_GenBuffers(1, &drawtex
->VBO
);
4289 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4290 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4291 NULL
, GL_DYNAMIC_DRAW_ARB
);
4293 /* client active texture is not part of the array object */
4294 active_texture
= ctx
->Array
.ActiveTexture
;
4296 /* setup vertex arrays */
4297 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4298 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4299 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4300 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
4301 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
4302 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4305 /* restore client active texture */
4306 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
4309 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4310 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4313 /* vertex positions, texcoords */
4315 const GLfloat x1
= x
+ width
;
4316 const GLfloat y1
= y
+ height
;
4318 z
= CLAMP(z
, 0.0f
, 1.0f
);
4337 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4338 const struct gl_texture_object
*texObj
;
4339 const struct gl_texture_image
*texImage
;
4340 GLfloat s
, t
, s1
, t1
;
4343 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
4345 for (j
= 0; j
< 4; j
++) {
4346 verts
[j
].st
[i
][0] = 0.0f
;
4347 verts
[j
].st
[i
][1] = 0.0f
;
4352 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
4353 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
4354 tw
= texImage
->Width2
;
4355 th
= texImage
->Height2
;
4357 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
4358 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
4359 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
4360 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
4362 verts
[0].st
[i
][0] = s
;
4363 verts
[0].st
[i
][1] = t
;
4365 verts
[1].st
[i
][0] = s1
;
4366 verts
[1].st
[i
][1] = t
;
4368 verts
[2].st
[i
][0] = s1
;
4369 verts
[2].st
[i
][1] = t1
;
4371 verts
[3].st
[i
][0] = s
;
4372 verts
[3].st
[i
][1] = t1
;
4375 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4378 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4380 _mesa_meta_end(ctx
);