2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/queryobj.h"
58 #include "main/readpix.h"
59 #include "main/scissor.h"
60 #include "main/shaderapi.h"
61 #include "main/shaderobj.h"
62 #include "main/state.h"
63 #include "main/stencil.h"
64 #include "main/texobj.h"
65 #include "main/texenv.h"
66 #include "main/texgetimage.h"
67 #include "main/teximage.h"
68 #include "main/texparam.h"
69 #include "main/texstate.h"
70 #include "main/transformfeedback.h"
71 #include "main/uniforms.h"
72 #include "main/varray.h"
73 #include "main/viewport.h"
74 #include "main/samplerobj.h"
75 #include "program/program.h"
76 #include "swrast/swrast.h"
77 #include "drivers/common/meta.h"
78 #include "main/enums.h"
79 #include "main/glformats.h"
80 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_CLEAR (and others?) */
94 struct gl_query_object
*CurrentOcclusionObject
;
96 /** MESA_META_ALPHA_TEST */
97 GLboolean AlphaEnabled
;
101 /** MESA_META_BLEND */
102 GLbitfield BlendEnabled
;
103 GLboolean ColorLogicOpEnabled
;
105 /** MESA_META_COLOR_MASK */
106 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
108 /** MESA_META_DEPTH_TEST */
109 struct gl_depthbuffer_attrib Depth
;
114 /** MESA_META_PIXEL_STORE */
115 struct gl_pixelstore_attrib Pack
, Unpack
;
117 /** MESA_META_PIXEL_TRANSFER */
118 GLfloat RedBias
, RedScale
;
119 GLfloat GreenBias
, GreenScale
;
120 GLfloat BlueBias
, BlueScale
;
121 GLfloat AlphaBias
, AlphaScale
;
122 GLfloat DepthBias
, DepthScale
;
123 GLboolean MapColorFlag
;
125 /** MESA_META_RASTERIZATION */
126 GLenum FrontPolygonMode
, BackPolygonMode
;
127 GLboolean PolygonOffset
;
128 GLboolean PolygonSmooth
;
129 GLboolean PolygonStipple
;
130 GLboolean PolygonCull
;
132 /** MESA_META_SCISSOR */
133 struct gl_scissor_attrib Scissor
;
135 /** MESA_META_SHADER */
136 GLboolean VertexProgramEnabled
;
137 struct gl_vertex_program
*VertexProgram
;
138 GLboolean FragmentProgramEnabled
;
139 struct gl_fragment_program
*FragmentProgram
;
140 GLboolean ATIFragmentShaderEnabled
;
141 struct gl_shader_program
*VertexShader
;
142 struct gl_shader_program
*GeometryShader
;
143 struct gl_shader_program
*FragmentShader
;
144 struct gl_shader_program
*ActiveShader
;
146 /** MESA_META_STENCIL_TEST */
147 struct gl_stencil_attrib Stencil
;
149 /** MESA_META_TRANSFORM */
151 GLfloat ModelviewMatrix
[16];
152 GLfloat ProjectionMatrix
[16];
153 GLfloat TextureMatrix
[16];
155 /** MESA_META_CLIP */
156 GLbitfield ClipPlanesEnabled
;
158 /** MESA_META_TEXTURE */
160 GLuint ClientActiveUnit
;
161 /** for unit[0] only */
162 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
163 /** mask of TEXTURE_2D_BIT, etc */
164 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
165 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
166 GLuint EnvMode
; /* unit[0] only */
168 /** MESA_META_VERTEX */
169 struct gl_array_object
*ArrayObj
;
170 struct gl_buffer_object
*ArrayBufferObj
;
172 /** MESA_META_VIEWPORT */
173 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
174 GLclampd DepthNear
, DepthFar
;
176 /** MESA_META_CLAMP_FRAGMENT_COLOR */
177 GLenum ClampFragmentColor
;
179 /** MESA_META_CLAMP_VERTEX_COLOR */
180 GLenum ClampVertexColor
;
182 /** MESA_META_CONDITIONAL_RENDER */
183 struct gl_query_object
*CondRenderQuery
;
184 GLenum CondRenderMode
;
186 /** MESA_META_SELECT_FEEDBACK */
188 struct gl_selection Select
;
189 struct gl_feedback Feedback
;
191 /** MESA_META_MULTISAMPLE */
192 GLboolean MultisampleEnabled
;
194 /** MESA_META_FRAMEBUFFER_SRGB */
195 GLboolean sRGBEnabled
;
197 /** Miscellaneous (always disabled) */
199 GLboolean RasterDiscard
;
200 GLboolean TransformFeedbackNeedsResume
;
204 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
205 * This is currently shared by all the meta ops. But we could create a
206 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
211 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
212 GLsizei MinSize
; /**< Min texture size to allocate */
213 GLsizei MaxSize
; /**< Max possible texture size */
214 GLboolean NPOT
; /**< Non-power of two size OK? */
215 GLsizei Width
, Height
; /**< Current texture size */
217 GLfloat Sright
, Ttop
; /**< right, top texcoords */
222 * State for glBlitFramebufer()
230 GLuint RectShaderProg
;
231 struct temp_texture depthTex
;
236 * State for glClear()
245 GLuint IntegerShaderProg
;
246 GLint IntegerColorLocation
;
251 * State for glCopyPixels()
261 * State for glDrawPixels()
267 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
268 GLuint DepthFP
; /**< Fragment program for drawing depth images */
273 * State for glBitmap()
279 struct temp_texture Tex
; /**< separate texture from other meta ops */
283 * State for GLSL texture sampler which is used to generate fragment
284 * shader in _mesa_meta_generate_mipmap().
286 struct glsl_sampler
{
289 const char *texcoords
;
294 * State for _mesa_meta_generate_mipmap()
296 struct gen_mipmap_state
303 struct glsl_sampler sampler_1d
;
304 struct glsl_sampler sampler_2d
;
305 struct glsl_sampler sampler_3d
;
306 struct glsl_sampler sampler_cubemap
;
307 struct glsl_sampler sampler_1d_array
;
308 struct glsl_sampler sampler_2d_array
;
312 * State for texture decompression
314 struct decompress_state
317 GLuint VBO
, FBO
, RBO
, Sampler
;
322 * State for glDrawTex()
330 #define MAX_META_OPS_DEPTH 8
332 * All per-context meta state.
336 /** Stack of state saved during meta-ops */
337 struct save_state Save
[MAX_META_OPS_DEPTH
];
338 /** Save stack depth */
339 GLuint SaveStackDepth
;
341 struct temp_texture TempTex
;
343 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
344 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
345 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
346 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
347 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
348 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
349 struct decompress_state Decompress
; /**< For texture decompression */
350 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
353 static void meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
);
354 static void cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
);
355 static void meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
);
356 static void meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
357 struct gen_mipmap_state
*mipmap
);
360 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
366 shader
= _mesa_CreateShaderObjectARB(target
);
367 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
368 _mesa_CompileShader(shader
);
370 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
374 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
376 _mesa_DeleteObjectARB(shader
);
382 _mesa_DeleteObjectARB(shader
);
386 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
388 "meta program compile failed:\n%s\n"
393 _mesa_DeleteObjectARB(shader
);
399 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
404 _mesa_LinkProgram(program
);
406 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
410 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
418 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
419 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
427 * Initialize meta-ops for a context.
428 * To be called once during context creation.
431 _mesa_meta_init(struct gl_context
*ctx
)
435 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
440 * Free context meta-op state.
441 * To be called once during context destruction.
444 _mesa_meta_free(struct gl_context
*ctx
)
446 GET_CURRENT_CONTEXT(old_context
);
447 _mesa_make_current(ctx
, NULL
, NULL
);
448 meta_glsl_blit_cleanup(ctx
, &ctx
->Meta
->Blit
);
449 meta_glsl_clear_cleanup(ctx
, &ctx
->Meta
->Clear
);
450 meta_glsl_generate_mipmap_cleanup(ctx
, &ctx
->Meta
->Mipmap
);
451 cleanup_temp_texture(ctx
, &ctx
->Meta
->TempTex
);
453 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
455 _mesa_make_current(NULL
, NULL
, NULL
);
462 * Enter meta state. This is like a light-weight version of glPushAttrib
463 * but it also resets most GL state back to default values.
465 * \param state bitmask of MESA_META_* flags indicating which attribute groups
466 * to save and reset to their defaults
469 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
471 struct save_state
*save
;
473 /* hope MAX_META_OPS_DEPTH is large enough */
474 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
476 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
477 memset(save
, 0, sizeof(*save
));
478 save
->SavedState
= state
;
480 /* Pausing transform feedback needs to be done early, or else we won't be
481 * able to change other state.
483 save
->TransformFeedbackNeedsResume
=
484 _mesa_is_xfb_active_and_unpaused(ctx
);
485 if (save
->TransformFeedbackNeedsResume
)
486 _mesa_PauseTransformFeedback();
488 /* After saving the current occlusion object, call EndQuery so that no
489 * occlusion querying will be active during the meta-operation.
491 if (state
& MESA_META_OCCLUSION_QUERY
) {
492 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
493 if (save
->CurrentOcclusionObject
)
494 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
497 if (state
& MESA_META_ALPHA_TEST
) {
498 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
499 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
500 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
501 if (ctx
->Color
.AlphaEnabled
)
502 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
505 if (state
& MESA_META_BLEND
) {
506 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
507 if (ctx
->Color
.BlendEnabled
) {
508 if (ctx
->Extensions
.EXT_draw_buffers2
) {
510 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
511 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
515 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
518 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
519 if (ctx
->Color
.ColorLogicOpEnabled
)
520 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
523 if (state
& MESA_META_COLOR_MASK
) {
524 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
525 sizeof(ctx
->Color
.ColorMask
));
526 if (!ctx
->Color
.ColorMask
[0][0] ||
527 !ctx
->Color
.ColorMask
[0][1] ||
528 !ctx
->Color
.ColorMask
[0][2] ||
529 !ctx
->Color
.ColorMask
[0][3])
530 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
533 if (state
& MESA_META_DEPTH_TEST
) {
534 save
->Depth
= ctx
->Depth
; /* struct copy */
536 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
539 if ((state
& MESA_META_FOG
)
540 && ctx
->API
!= API_OPENGL_CORE
541 && ctx
->API
!= API_OPENGLES2
) {
542 save
->Fog
= ctx
->Fog
.Enabled
;
543 if (ctx
->Fog
.Enabled
)
544 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
547 if (state
& MESA_META_PIXEL_STORE
) {
548 save
->Pack
= ctx
->Pack
;
549 save
->Unpack
= ctx
->Unpack
;
550 ctx
->Pack
= ctx
->DefaultPacking
;
551 ctx
->Unpack
= ctx
->DefaultPacking
;
554 if (state
& MESA_META_PIXEL_TRANSFER
) {
555 save
->RedScale
= ctx
->Pixel
.RedScale
;
556 save
->RedBias
= ctx
->Pixel
.RedBias
;
557 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
558 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
559 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
560 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
561 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
562 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
563 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
564 ctx
->Pixel
.RedScale
= 1.0F
;
565 ctx
->Pixel
.RedBias
= 0.0F
;
566 ctx
->Pixel
.GreenScale
= 1.0F
;
567 ctx
->Pixel
.GreenBias
= 0.0F
;
568 ctx
->Pixel
.BlueScale
= 1.0F
;
569 ctx
->Pixel
.BlueBias
= 0.0F
;
570 ctx
->Pixel
.AlphaScale
= 1.0F
;
571 ctx
->Pixel
.AlphaBias
= 0.0F
;
572 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
574 ctx
->NewState
|=_NEW_PIXEL
;
577 if (state
& MESA_META_RASTERIZATION
) {
578 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
579 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
580 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
581 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
582 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
583 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
584 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
585 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
586 if (ctx
->API
== API_OPENGL_COMPAT
) {
587 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
588 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
590 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
593 if (state
& MESA_META_SCISSOR
) {
594 save
->Scissor
= ctx
->Scissor
; /* struct copy */
595 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
598 if (state
& MESA_META_SHADER
) {
599 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
600 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
601 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
602 ctx
->VertexProgram
.Current
);
603 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
606 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
607 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
608 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
609 ctx
->FragmentProgram
.Current
);
610 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
613 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
614 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
615 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
618 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
619 ctx
->Shader
.CurrentVertexProgram
);
620 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
621 ctx
->Shader
.CurrentGeometryProgram
);
622 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
623 ctx
->Shader
.CurrentFragmentProgram
);
624 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
625 ctx
->Shader
.ActiveProgram
);
630 if (state
& MESA_META_STENCIL_TEST
) {
631 save
->Stencil
= ctx
->Stencil
; /* struct copy */
632 if (ctx
->Stencil
.Enabled
)
633 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
634 /* NOTE: other stencil state not reset */
637 if (state
& MESA_META_TEXTURE
) {
640 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
641 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
642 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
644 /* Disable all texture units */
645 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
646 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
647 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
648 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
649 if (ctx
->Texture
.Unit
[u
].Enabled
||
650 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
651 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
652 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
653 if (ctx
->Extensions
.ARB_texture_cube_map
)
654 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
655 if (_mesa_is_gles(ctx
) &&
656 ctx
->Extensions
.OES_EGL_image_external
)
657 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
659 if (ctx
->API
== API_OPENGL_COMPAT
) {
660 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
661 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
662 if (ctx
->Extensions
.NV_texture_rectangle
)
663 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
664 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
665 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
666 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
667 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
669 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
675 /* save current texture objects for unit[0] only */
676 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
677 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
678 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
681 /* set defaults for unit[0] */
682 _mesa_ActiveTexture(GL_TEXTURE0
);
683 _mesa_ClientActiveTexture(GL_TEXTURE0
);
684 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
685 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
689 if (state
& MESA_META_TRANSFORM
) {
690 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
691 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
692 16 * sizeof(GLfloat
));
693 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
694 16 * sizeof(GLfloat
));
695 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
696 16 * sizeof(GLfloat
));
697 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
698 /* set 1:1 vertex:pixel coordinate transform */
699 _mesa_ActiveTexture(GL_TEXTURE0
);
700 _mesa_MatrixMode(GL_TEXTURE
);
701 _mesa_LoadIdentity();
702 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
703 _mesa_MatrixMode(GL_MODELVIEW
);
704 _mesa_LoadIdentity();
705 _mesa_MatrixMode(GL_PROJECTION
);
706 _mesa_LoadIdentity();
708 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
709 * This can occur when there is no draw buffer.
711 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
712 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
713 0.0, ctx
->DrawBuffer
->Height
,
717 if (state
& MESA_META_CLIP
) {
718 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
719 if (ctx
->Transform
.ClipPlanesEnabled
) {
721 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
722 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
727 if (state
& MESA_META_VERTEX
) {
728 /* save vertex array object state */
729 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
730 ctx
->Array
.ArrayObj
);
731 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
732 ctx
->Array
.ArrayBufferObj
);
733 /* set some default state? */
736 if (state
& MESA_META_VIEWPORT
) {
737 /* save viewport state */
738 save
->ViewportX
= ctx
->Viewport
.X
;
739 save
->ViewportY
= ctx
->Viewport
.Y
;
740 save
->ViewportW
= ctx
->Viewport
.Width
;
741 save
->ViewportH
= ctx
->Viewport
.Height
;
742 /* set viewport to match window size */
743 if (ctx
->Viewport
.X
!= 0 ||
744 ctx
->Viewport
.Y
!= 0 ||
745 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
746 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
747 _mesa_set_viewport(ctx
, 0, 0,
748 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
750 /* save depth range state */
751 save
->DepthNear
= ctx
->Viewport
.Near
;
752 save
->DepthFar
= ctx
->Viewport
.Far
;
753 /* set depth range to default */
754 _mesa_DepthRange(0.0, 1.0);
757 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
758 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
760 /* Generally in here we want to do clamping according to whether
761 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
762 * regardless of the internal implementation of the metaops.
764 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
765 ctx
->Extensions
.ARB_color_buffer_float
)
766 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
769 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
770 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
772 /* Generally in here we never want vertex color clamping --
773 * result clamping is only dependent on fragment clamping.
775 if (ctx
->Extensions
.ARB_color_buffer_float
)
776 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
779 if (state
& MESA_META_CONDITIONAL_RENDER
) {
780 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
781 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
783 if (ctx
->Query
.CondRenderQuery
)
784 _mesa_EndConditionalRender();
787 if (state
& MESA_META_SELECT_FEEDBACK
) {
788 save
->RenderMode
= ctx
->RenderMode
;
789 if (ctx
->RenderMode
== GL_SELECT
) {
790 save
->Select
= ctx
->Select
; /* struct copy */
791 _mesa_RenderMode(GL_RENDER
);
792 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
793 save
->Feedback
= ctx
->Feedback
; /* struct copy */
794 _mesa_RenderMode(GL_RENDER
);
798 if (state
& MESA_META_MULTISAMPLE
) {
799 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
800 if (ctx
->Multisample
.Enabled
)
801 _mesa_set_multisample(ctx
, GL_FALSE
);
804 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
805 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
806 if (ctx
->Color
.sRGBEnabled
)
807 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
812 save
->Lighting
= ctx
->Light
.Enabled
;
813 if (ctx
->Light
.Enabled
)
814 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
815 save
->RasterDiscard
= ctx
->RasterDiscard
;
816 if (ctx
->RasterDiscard
)
817 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
823 * Leave meta state. This is like a light-weight version of glPopAttrib().
826 _mesa_meta_end(struct gl_context
*ctx
)
828 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
829 const GLbitfield state
= save
->SavedState
;
831 /* After starting a new occlusion query, initialize the results to the
832 * values saved previously. The driver will then continue to increment
835 if (state
& MESA_META_OCCLUSION_QUERY
) {
836 if (save
->CurrentOcclusionObject
) {
837 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
838 save
->CurrentOcclusionObject
->Id
);
839 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
843 if (state
& MESA_META_ALPHA_TEST
) {
844 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
845 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
846 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
849 if (state
& MESA_META_BLEND
) {
850 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
851 if (ctx
->Extensions
.EXT_draw_buffers2
) {
853 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
854 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
858 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
861 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
862 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
865 if (state
& MESA_META_COLOR_MASK
) {
867 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
868 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
870 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
871 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
875 save
->ColorMask
[i
][0],
876 save
->ColorMask
[i
][1],
877 save
->ColorMask
[i
][2],
878 save
->ColorMask
[i
][3]);
884 if (state
& MESA_META_DEPTH_TEST
) {
885 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
886 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
887 _mesa_DepthFunc(save
->Depth
.Func
);
888 _mesa_DepthMask(save
->Depth
.Mask
);
891 if ((state
& MESA_META_FOG
)
892 && ctx
->API
!= API_OPENGL_CORE
893 && ctx
->API
!= API_OPENGLES2
) {
894 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
897 if (state
& MESA_META_PIXEL_STORE
) {
898 ctx
->Pack
= save
->Pack
;
899 ctx
->Unpack
= save
->Unpack
;
902 if (state
& MESA_META_PIXEL_TRANSFER
) {
903 ctx
->Pixel
.RedScale
= save
->RedScale
;
904 ctx
->Pixel
.RedBias
= save
->RedBias
;
905 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
906 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
907 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
908 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
909 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
910 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
911 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
913 ctx
->NewState
|=_NEW_PIXEL
;
916 if (state
& MESA_META_RASTERIZATION
) {
917 /* Core context requires that front and back mode be the same.
919 if (ctx
->API
== API_OPENGL_CORE
) {
920 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
922 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
923 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
925 if (ctx
->API
== API_OPENGL_COMPAT
) {
926 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
927 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
929 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
930 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
933 if (state
& MESA_META_SCISSOR
) {
934 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
935 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
936 save
->Scissor
.Width
, save
->Scissor
.Height
);
939 if (state
& MESA_META_SHADER
) {
940 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
941 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
942 save
->VertexProgramEnabled
);
943 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
944 save
->VertexProgram
);
945 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
948 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
949 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
950 save
->FragmentProgramEnabled
);
951 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
952 save
->FragmentProgram
);
953 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
956 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
957 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
958 save
->ATIFragmentShaderEnabled
);
961 if (ctx
->Extensions
.ARB_vertex_shader
)
962 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
964 if (_mesa_has_geometry_shaders(ctx
))
965 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
966 save
->GeometryShader
);
968 if (ctx
->Extensions
.ARB_fragment_shader
)
969 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
970 save
->FragmentShader
);
972 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
975 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
976 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
977 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
978 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
981 if (state
& MESA_META_STENCIL_TEST
) {
982 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
984 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
985 _mesa_ClearStencil(stencil
->Clear
);
986 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
987 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
988 stencil
->TestTwoSide
);
989 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
990 ? GL_BACK
: GL_FRONT
);
993 _mesa_StencilFuncSeparate(GL_FRONT
,
994 stencil
->Function
[0],
996 stencil
->ValueMask
[0]);
997 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
998 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
999 stencil
->ZFailFunc
[0],
1000 stencil
->ZPassFunc
[0]);
1002 _mesa_StencilFuncSeparate(GL_BACK
,
1003 stencil
->Function
[1],
1005 stencil
->ValueMask
[1]);
1006 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1007 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1008 stencil
->ZFailFunc
[1],
1009 stencil
->ZPassFunc
[1]);
1012 if (state
& MESA_META_TEXTURE
) {
1015 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1017 /* restore texenv for unit[0] */
1018 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1019 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1022 /* restore texture objects for unit[0] only */
1023 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1024 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1025 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1026 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1027 save
->CurrentTexture
[tgt
]);
1029 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1032 /* Restore fixed function texture enables, texgen */
1033 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1034 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1035 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1036 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1037 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1040 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1041 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1042 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1047 /* restore current unit state */
1048 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1049 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1052 if (state
& MESA_META_TRANSFORM
) {
1053 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1054 _mesa_ActiveTexture(GL_TEXTURE0
);
1055 _mesa_MatrixMode(GL_TEXTURE
);
1056 _mesa_LoadMatrixf(save
->TextureMatrix
);
1057 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1059 _mesa_MatrixMode(GL_MODELVIEW
);
1060 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1062 _mesa_MatrixMode(GL_PROJECTION
);
1063 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1065 _mesa_MatrixMode(save
->MatrixMode
);
1068 if (state
& MESA_META_CLIP
) {
1069 if (save
->ClipPlanesEnabled
) {
1071 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1072 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1073 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1079 if (state
& MESA_META_VERTEX
) {
1080 /* restore vertex buffer object */
1081 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1082 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1084 /* restore vertex array object */
1085 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
1086 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
1089 if (state
& MESA_META_VIEWPORT
) {
1090 if (save
->ViewportX
!= ctx
->Viewport
.X
||
1091 save
->ViewportY
!= ctx
->Viewport
.Y
||
1092 save
->ViewportW
!= ctx
->Viewport
.Width
||
1093 save
->ViewportH
!= ctx
->Viewport
.Height
) {
1094 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
1095 save
->ViewportW
, save
->ViewportH
);
1097 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1100 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1101 ctx
->Extensions
.ARB_color_buffer_float
) {
1102 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1105 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1106 ctx
->Extensions
.ARB_color_buffer_float
) {
1107 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1110 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1111 if (save
->CondRenderQuery
)
1112 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1113 save
->CondRenderMode
);
1116 if (state
& MESA_META_SELECT_FEEDBACK
) {
1117 if (save
->RenderMode
== GL_SELECT
) {
1118 _mesa_RenderMode(GL_SELECT
);
1119 ctx
->Select
= save
->Select
;
1120 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1121 _mesa_RenderMode(GL_FEEDBACK
);
1122 ctx
->Feedback
= save
->Feedback
;
1126 if (state
& MESA_META_MULTISAMPLE
) {
1127 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1128 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1131 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1132 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1133 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1137 if (save
->Lighting
) {
1138 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1140 if (save
->RasterDiscard
) {
1141 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1143 if (save
->TransformFeedbackNeedsResume
)
1144 _mesa_ResumeTransformFeedback();
1146 ctx
->Meta
->SaveStackDepth
--;
1151 * Determine whether Mesa is currently in a meta state.
1154 _mesa_meta_in_progress(struct gl_context
*ctx
)
1156 return ctx
->Meta
->SaveStackDepth
!= 0;
1161 * Convert Z from a normalized value in the range [0, 1] to an object-space
1162 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1163 * default/identity ortho projection results in the original Z value.
1164 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1165 * value comes from the clear value or raster position.
1167 static INLINE GLfloat
1168 invert_z(GLfloat normZ
)
1170 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1176 * One-time init for a temp_texture object.
1177 * Choose tex target, compute max tex size, etc.
1180 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1182 /* prefer texture rectangle */
1183 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1184 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1185 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1186 tex
->NPOT
= GL_TRUE
;
1189 /* use 2D texture, NPOT if possible */
1190 tex
->Target
= GL_TEXTURE_2D
;
1191 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1192 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1194 tex
->MinSize
= 16; /* 16 x 16 at least */
1195 assert(tex
->MaxSize
> 0);
1197 _mesa_GenTextures(1, &tex
->TexObj
);
1201 cleanup_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1205 _mesa_DeleteTextures(1, &tex
->TexObj
);
1211 * Return pointer to temp_texture info for non-bitmap ops.
1212 * This does some one-time init if needed.
1214 static struct temp_texture
*
1215 get_temp_texture(struct gl_context
*ctx
)
1217 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1220 init_temp_texture(ctx
, tex
);
1228 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1229 * We use a separate texture for bitmaps to reduce texture
1230 * allocation/deallocation.
1232 static struct temp_texture
*
1233 get_bitmap_temp_texture(struct gl_context
*ctx
)
1235 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1238 init_temp_texture(ctx
, tex
);
1245 * Return pointer to depth temp_texture.
1246 * This does some one-time init if needed.
1248 static struct temp_texture
*
1249 get_temp_depth_texture(struct gl_context
*ctx
)
1251 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1254 init_temp_texture(ctx
, tex
);
1261 * Compute the width/height of texture needed to draw an image of the
1262 * given size. Return a flag indicating whether the current texture
1263 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1264 * allocated (glTexImage2D).
1265 * Also, compute s/t texcoords for drawing.
1267 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1270 alloc_texture(struct temp_texture
*tex
,
1271 GLsizei width
, GLsizei height
, GLenum intFormat
)
1273 GLboolean newTex
= GL_FALSE
;
1275 ASSERT(width
<= tex
->MaxSize
);
1276 ASSERT(height
<= tex
->MaxSize
);
1278 if (width
> tex
->Width
||
1279 height
> tex
->Height
||
1280 intFormat
!= tex
->IntFormat
) {
1281 /* alloc new texture (larger or different format) */
1284 /* use non-power of two size */
1285 tex
->Width
= MAX2(tex
->MinSize
, width
);
1286 tex
->Height
= MAX2(tex
->MinSize
, height
);
1289 /* find power of two size */
1291 w
= h
= tex
->MinSize
;
1300 tex
->IntFormat
= intFormat
;
1305 /* compute texcoords */
1306 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1307 tex
->Sright
= (GLfloat
) width
;
1308 tex
->Ttop
= (GLfloat
) height
;
1311 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1312 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1320 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1323 setup_copypix_texture(struct gl_context
*ctx
,
1324 struct temp_texture
*tex
,
1326 GLint srcX
, GLint srcY
,
1327 GLsizei width
, GLsizei height
, GLenum intFormat
,
1330 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1331 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1332 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1333 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1334 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1336 /* copy framebuffer image to texture */
1338 /* create new tex image */
1339 if (tex
->Width
== width
&& tex
->Height
== height
) {
1340 /* create new tex with framebuffer data */
1341 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1342 srcX
, srcY
, width
, height
, 0);
1345 /* create empty texture */
1346 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1347 tex
->Width
, tex
->Height
, 0,
1348 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1350 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1351 0, 0, srcX
, srcY
, width
, height
);
1355 /* replace existing tex image */
1356 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1357 0, 0, srcX
, srcY
, width
, height
);
1363 * Setup/load texture for glDrawPixels.
1366 setup_drawpix_texture(struct gl_context
*ctx
,
1367 struct temp_texture
*tex
,
1369 GLenum texIntFormat
,
1370 GLsizei width
, GLsizei height
,
1371 GLenum format
, GLenum type
,
1372 const GLvoid
*pixels
)
1374 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1375 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1376 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1377 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1378 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1380 /* copy pixel data to texture */
1382 /* create new tex image */
1383 if (tex
->Width
== width
&& tex
->Height
== height
) {
1384 /* create new tex and load image data */
1385 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1386 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1389 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1391 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1392 ctx
->Unpack
.BufferObj
);
1393 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1394 /* create empty texture */
1395 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1396 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1397 if (save_unpack_obj
!= NULL
)
1398 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1399 save_unpack_obj
->Name
);
1401 _mesa_TexSubImage2D(tex
->Target
, 0,
1402 0, 0, width
, height
, format
, type
, pixels
);
1406 /* replace existing tex image */
1407 _mesa_TexSubImage2D(tex
->Target
, 0,
1408 0, 0, width
, height
, format
, type
, pixels
);
1415 * One-time init for drawing depth pixels.
1418 init_blit_depth_pixels(struct gl_context
*ctx
)
1420 static const char *program
=
1422 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1425 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1426 struct temp_texture
*tex
= get_temp_texture(ctx
);
1427 const char *texTarget
;
1429 assert(blit
->DepthFP
== 0);
1431 /* replace %s with "RECT" or "2D" */
1432 assert(strlen(program
) + 4 < sizeof(program2
));
1433 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1437 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1439 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1440 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1441 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1442 strlen(program2
), (const GLubyte
*) program2
);
1446 setup_ff_blit_framebuffer(struct gl_context
*ctx
,
1447 struct blit_state
*blit
)
1452 struct vertex verts
[4];
1454 if (blit
->ArrayObj
== 0) {
1455 /* one-time setup */
1457 /* create vertex array object */
1458 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1459 _mesa_BindVertexArray(blit
->ArrayObj
);
1461 /* create vertex array buffer */
1462 _mesa_GenBuffers(1, &blit
->VBO
);
1463 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1464 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1465 NULL
, GL_DYNAMIC_DRAW_ARB
);
1467 /* setup vertex arrays */
1468 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1469 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1470 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1471 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1474 /* setup projection matrix */
1475 _mesa_MatrixMode(GL_PROJECTION
);
1476 _mesa_LoadIdentity();
1477 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1482 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1483 struct blit_state
*blit
,
1489 struct vertex verts
[4];
1490 const char *vs_source
;
1495 GLboolean texture_2d
= (target
== GL_TEXTURE_2D
);
1497 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1498 assert(_mesa_is_desktop_gl(ctx
) || texture_2d
);
1500 /* Check if already initialized */
1501 if (blit
->ArrayObj
== 0) {
1503 /* create vertex array object */
1504 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1505 _mesa_BindVertexArray(blit
->ArrayObj
);
1507 /* create vertex array buffer */
1508 _mesa_GenBuffers(1, &blit
->VBO
);
1509 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1510 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1511 NULL
, GL_DYNAMIC_DRAW_ARB
);
1513 /* setup vertex arrays */
1514 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
1515 sizeof(struct vertex
), OFFSET(x
));
1516 _mesa_VertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
,
1517 sizeof(struct vertex
), OFFSET(s
));
1520 /* Generate a relevant fragment shader program for the texture target */
1521 if ((target
== GL_TEXTURE_2D
&& blit
->ShaderProg
!= 0) ||
1522 (target
== GL_TEXTURE_RECTANGLE
&& blit
->RectShaderProg
!= 0)) {
1526 mem_ctx
= ralloc_context(NULL
);
1528 if (ctx
->Const
.GLSLVersion
< 130) {
1530 "attribute vec2 position;\n"
1531 "attribute vec2 textureCoords;\n"
1532 "varying vec2 texCoords;\n"
1535 " texCoords = textureCoords;\n"
1536 " gl_Position = vec4(position, 0.0, 1.0);\n"
1539 fs_source
= ralloc_asprintf(mem_ctx
,
1540 "uniform %s texSampler;\n"
1541 "varying vec2 texCoords;\n"
1544 " gl_FragColor = %s(texSampler, texCoords);\n"
1545 " gl_FragDepth = gl_FragColor.r;\n"
1547 texture_2d
? "sampler2D" : "sampler2DRect",
1548 texture_2d
? "texture2D" : "texture2DRect");
1551 vs_source
= ralloc_asprintf(mem_ctx
,
1553 "in vec2 position;\n"
1554 "in vec2 textureCoords;\n"
1555 "out vec2 texCoords;\n"
1558 " texCoords = textureCoords;\n"
1559 " gl_Position = vec4(position, 0.0, 1.0);\n"
1561 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1562 fs_source
= ralloc_asprintf(mem_ctx
,
1564 "uniform %s texSampler;\n"
1565 "in vec2 texCoords;\n"
1566 "out vec4 out_color;\n"
1570 " out_color = %s(texSampler, texCoords);\n"
1571 " gl_FragDepth = out_color.r;\n"
1573 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
1574 texture_2d
? "sampler2D" : "sampler2DRect",
1575 texture_2d
? "texture" : "texture2DRect");
1578 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
1579 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
1581 ShaderProg
= _mesa_CreateProgramObjectARB();
1582 _mesa_AttachShader(ShaderProg
, fs
);
1583 _mesa_DeleteObjectARB(fs
);
1584 _mesa_AttachShader(ShaderProg
, vs
);
1585 _mesa_DeleteObjectARB(vs
);
1586 _mesa_BindAttribLocation(ShaderProg
, 0, "position");
1587 _mesa_BindAttribLocation(ShaderProg
, 1, "texcoords");
1588 _mesa_EnableVertexAttribArray(0);
1589 _mesa_EnableVertexAttribArray(1);
1590 link_program_with_debug(ctx
, ShaderProg
);
1591 ralloc_free(mem_ctx
);
1593 blit
->ShaderProg
= ShaderProg
;
1595 blit
->RectShaderProg
= ShaderProg
;
1599 * Try to do a glBlitFramebuffer using no-copy texturing.
1600 * We can do this when the src renderbuffer is actually a texture.
1601 * But if the src buffer == dst buffer we cannot do this.
1603 * \return new buffer mask indicating the buffers left to blit using the
1607 blitframebuffer_texture(struct gl_context
*ctx
,
1608 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1609 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1610 GLbitfield mask
, GLenum filter
, GLint flipX
,
1611 GLint flipY
, GLboolean glsl_version
)
1613 if (mask
& GL_COLOR_BUFFER_BIT
) {
1614 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1615 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1616 const struct gl_renderbuffer_attachment
*drawAtt
;
1617 const struct gl_renderbuffer_attachment
*readAtt
=
1618 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1620 if (readAtt
&& readAtt
->Texture
) {
1621 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1622 const GLint dstX
= MIN2(dstX0
, dstX1
);
1623 const GLint dstY
= MIN2(dstY0
, dstY1
);
1624 const GLint dstW
= abs(dstX1
- dstX0
);
1625 const GLint dstH
= abs(dstY1
- dstY0
);
1626 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1627 const GLuint srcLevel
= readAtt
->TextureLevel
;
1628 const GLint baseLevelSave
= texObj
->BaseLevel
;
1629 const GLint maxLevelSave
= texObj
->MaxLevel
;
1630 const GLenum target
= texObj
->Target
;
1631 GLuint sampler
, samplerSave
=
1632 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1633 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1636 /* Iterate through all draw buffers */
1637 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1638 int idx
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1641 drawAtt
= &drawFb
->Attachment
[idx
];
1643 if (drawAtt
->Texture
== readAtt
->Texture
) {
1644 /* Can't use same texture as both the source and dest. We need
1645 * to handle overlapping blits and besides, some hw may not
1652 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1653 /* Can't handle other texture types at this time */
1657 /* Choose between glsl version and fixed function version of
1658 * BlitFramebuffer function.
1661 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1662 if (target
== GL_TEXTURE_2D
)
1663 _mesa_UseProgram(blit
->ShaderProg
);
1665 _mesa_UseProgram(blit
->RectShaderProg
);
1668 setup_ff_blit_framebuffer(ctx
, &ctx
->Meta
->Blit
);
1671 _mesa_BindVertexArray(blit
->ArrayObj
);
1672 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1674 _mesa_GenSamplers(1, &sampler
);
1675 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1678 printf("Blit from texture!\n");
1679 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1680 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1683 /* Prepare src texture state */
1684 _mesa_BindTexture(target
, texObj
->Name
);
1685 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1686 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1687 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1688 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1689 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1691 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1692 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1694 /* Always do our blits with no sRGB decode or encode. Note that
1695 * GL_FRAMEBUFFER_SRGB has already been disabled by
1696 * _mesa_meta_begin().
1698 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1699 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1700 GL_SKIP_DECODE_EXT
);
1703 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1704 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1705 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1708 /* Prepare vertex data (the VBO was previously created and bound) */
1713 struct vertex verts
[4];
1714 GLfloat s0
, t0
, s1
, t1
;
1716 if (target
== GL_TEXTURE_2D
) {
1717 const struct gl_texture_image
*texImage
1718 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1719 s0
= srcX0
/ (float) texImage
->Width
;
1720 s1
= srcX1
/ (float) texImage
->Width
;
1721 t0
= srcY0
/ (float) texImage
->Height
;
1722 t1
= srcY1
/ (float) texImage
->Height
;
1725 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1732 /* setup vertex positions */
1733 verts
[0].x
= -1.0F
* flipX
;
1734 verts
[0].y
= -1.0F
* flipY
;
1735 verts
[1].x
= 1.0F
* flipX
;
1736 verts
[1].y
= -1.0F
* flipY
;
1737 verts
[2].x
= 1.0F
* flipX
;
1738 verts
[2].y
= 1.0F
* flipY
;
1739 verts
[3].x
= -1.0F
* flipX
;
1740 verts
[3].y
= 1.0F
* flipY
;
1751 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1754 /* setup viewport */
1755 _mesa_set_viewport(ctx
, dstX
, dstY
, dstW
, dstH
);
1756 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1757 _mesa_DepthMask(GL_FALSE
);
1758 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1760 /* Restore texture object state, the texture binding will
1761 * be restored by _mesa_meta_end().
1763 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1764 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1765 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1768 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1769 _mesa_DeleteSamplers(1, &sampler
);
1771 /* Done with color buffer */
1772 mask
&= ~GL_COLOR_BUFFER_BIT
;
1781 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1782 * of texture mapping and polygon rendering.
1785 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1786 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1787 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1788 GLbitfield mask
, GLenum filter
)
1790 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1791 struct temp_texture
*tex
= get_temp_texture(ctx
);
1792 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1793 const GLsizei maxTexSize
= tex
->MaxSize
;
1794 const GLint srcX
= MIN2(srcX0
, srcX1
);
1795 const GLint srcY
= MIN2(srcY0
, srcY1
);
1796 const GLint srcW
= abs(srcX1
- srcX0
);
1797 const GLint srcH
= abs(srcY1
- srcY0
);
1798 const GLint dstX
= MIN2(dstX0
, dstX1
);
1799 const GLint dstY
= MIN2(dstY0
, dstY1
);
1800 const GLint dstW
= abs(dstX1
- dstX0
);
1801 const GLint dstH
= abs(dstY1
- dstY0
);
1802 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1803 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1804 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1805 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1806 const GLint flipX
= srcFlipX
* dstFlipX
;
1807 const GLint flipY
= srcFlipY
* dstFlipY
;
1812 struct vertex verts
[4];
1814 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1815 ctx
->Extensions
.ARB_fragment_shader
&&
1816 (ctx
->API
!= API_OPENGLES
);
1818 /* In addition to falling back if the blit size is larger than the maximum
1819 * texture size, fallback if the source is multisampled. This fallback can
1820 * be removed once Mesa gets support ARB_texture_multisample.
1822 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1823 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1824 /* XXX avoid this fallback */
1825 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1826 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1830 /* only scissor effects blit so save/clear all other relevant state */
1831 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1833 /* Try faster, direct texture approach first */
1834 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1835 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1836 dstFlipX
, dstFlipY
, use_glsl_version
);
1838 _mesa_meta_end(ctx
);
1842 /* Choose between glsl version and fixed function version of
1843 * BlitFramebuffer function.
1845 if (use_glsl_version
) {
1846 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1847 if (tex
->Target
== GL_TEXTURE_2D
)
1848 _mesa_UseProgram(blit
->ShaderProg
);
1850 _mesa_UseProgram(blit
->RectShaderProg
);
1853 setup_ff_blit_framebuffer(ctx
, blit
);
1856 _mesa_BindVertexArray(blit
->ArrayObj
);
1857 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1859 /* Continue with "normal" approach which involves copying the src rect
1860 * into a temporary texture and is "blitted" by drawing a textured quad.
1863 /* setup vertex positions */
1864 verts
[0].x
= -1.0F
* flipX
;
1865 verts
[0].y
= -1.0F
* flipY
;
1866 verts
[1].x
= 1.0F
* flipX
;
1867 verts
[1].y
= -1.0F
* flipY
;
1868 verts
[2].x
= 1.0F
* flipX
;
1869 verts
[2].y
= 1.0F
* flipY
;
1870 verts
[3].x
= -1.0F
* flipX
;
1871 verts
[3].y
= 1.0F
* flipY
;
1875 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1878 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1879 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1881 if (mask
& GL_COLOR_BUFFER_BIT
) {
1882 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1883 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1884 const GLenum rb_base_format
=
1885 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1887 /* Using the exact source rectangle to create the texture does incorrect
1888 * linear filtering along the edges. So, allocate the texture extended along
1889 * edges by one pixel in x, y directions.
1891 newTex
= alloc_texture(tex
, srcW
+ 2, srcH
+ 2, rb_base_format
);
1892 setup_copypix_texture(ctx
, tex
, newTex
,
1893 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1894 rb_base_format
, filter
);
1895 /* texcoords (after texture allocation!) */
1899 verts
[1].s
= tex
->Sright
- 1.0F
;
1901 verts
[2].s
= tex
->Sright
- 1.0F
;
1902 verts
[2].t
= tex
->Ttop
- 1.0F
;
1904 verts
[3].t
= tex
->Ttop
- 1.0F
;
1906 /* upload new vertex data */
1907 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1910 _mesa_set_viewport(ctx
, dstX
, dstY
, dstW
, dstH
);
1911 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1912 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1913 _mesa_DepthMask(GL_FALSE
);
1914 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1915 mask
&= ~GL_COLOR_BUFFER_BIT
;
1918 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1919 _mesa_is_desktop_gl(ctx
) &&
1920 ctx
->Extensions
.ARB_depth_texture
&&
1921 ctx
->Extensions
.ARB_fragment_program
) {
1923 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1927 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1928 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1929 GL_UNSIGNED_INT
, tmp
);
1930 setup_drawpix_texture(ctx
, depthTex
, newTex
, GL_DEPTH_COMPONENT
,
1931 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1932 GL_UNSIGNED_INT
, tmp
);
1934 /* texcoords (after texture allocation!) */
1938 verts
[1].s
= depthTex
->Sright
;
1940 verts
[2].s
= depthTex
->Sright
;
1941 verts
[2].t
= depthTex
->Ttop
;
1943 verts
[3].t
= depthTex
->Ttop
;
1945 /* upload new vertex data */
1946 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1950 init_blit_depth_pixels(ctx
);
1952 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1953 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1954 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1955 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1956 _mesa_DepthFunc(GL_ALWAYS
);
1957 _mesa_DepthMask(GL_TRUE
);
1959 _mesa_set_viewport(ctx
, dstX
, dstY
, dstW
, dstH
);
1960 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1961 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1962 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1968 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1969 /* XXX can't easily do stencil */
1972 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1973 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1975 _mesa_meta_end(ctx
);
1978 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1979 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1984 meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1986 if (blit
->ArrayObj
) {
1987 _mesa_DeleteVertexArrays(1, &blit
->ArrayObj
);
1989 _mesa_DeleteBuffers(1, &blit
->VBO
);
1992 if (blit
->DepthFP
) {
1993 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
1997 _mesa_DeleteObjectARB(blit
->ShaderProg
);
1998 blit
->ShaderProg
= 0;
1999 _mesa_DeleteObjectARB(blit
->RectShaderProg
);
2000 blit
->RectShaderProg
= 0;
2002 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
2003 blit
->depthTex
.TexObj
= 0;
2008 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2011 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2013 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2015 GLfloat x
, y
, z
, r
, g
, b
, a
;
2017 struct vertex verts
[4];
2018 /* save all state but scissor, pixel pack/unpack */
2019 GLbitfield metaSave
= (MESA_META_ALL
-
2021 MESA_META_PIXEL_STORE
-
2022 MESA_META_CONDITIONAL_RENDER
-
2023 MESA_META_FRAMEBUFFER_SRGB
);
2024 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2026 if (buffers
& BUFFER_BITS_COLOR
) {
2027 /* if clearing color buffers, don't save/restore colormask */
2028 metaSave
-= MESA_META_COLOR_MASK
;
2031 _mesa_meta_begin(ctx
, metaSave
);
2033 if (clear
->ArrayObj
== 0) {
2034 /* one-time setup */
2036 /* create vertex array object */
2037 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2038 _mesa_BindVertexArray(clear
->ArrayObj
);
2040 /* create vertex array buffer */
2041 _mesa_GenBuffers(1, &clear
->VBO
);
2042 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2044 /* setup vertex arrays */
2045 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2046 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2047 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2048 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2051 _mesa_BindVertexArray(clear
->ArrayObj
);
2052 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2055 /* GL_COLOR_BUFFER_BIT */
2056 if (buffers
& BUFFER_BITS_COLOR
) {
2057 /* leave colormask, glDrawBuffer state as-is */
2059 /* Clears never have the color clamped. */
2060 if (ctx
->Extensions
.ARB_color_buffer_float
)
2061 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2064 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2065 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2068 /* GL_DEPTH_BUFFER_BIT */
2069 if (buffers
& BUFFER_BIT_DEPTH
) {
2070 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2071 _mesa_DepthFunc(GL_ALWAYS
);
2072 _mesa_DepthMask(GL_TRUE
);
2075 assert(!ctx
->Depth
.Test
);
2078 /* GL_STENCIL_BUFFER_BIT */
2079 if (buffers
& BUFFER_BIT_STENCIL
) {
2080 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2081 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2082 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2083 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2084 ctx
->Stencil
.Clear
& stencilMax
,
2085 ctx
->Stencil
.WriteMask
[0]);
2088 assert(!ctx
->Stencil
.Enabled
);
2091 /* vertex positions/colors */
2093 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
2094 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
2095 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
2096 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
2097 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
2114 for (i
= 0; i
< 4; i
++) {
2115 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
2116 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
2117 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
2118 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
2121 /* upload new vertex data */
2122 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2123 GL_DYNAMIC_DRAW_ARB
);
2127 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2129 _mesa_meta_end(ctx
);
2133 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
2135 const char *vs_source
=
2136 "attribute vec4 position;\n"
2139 " gl_Position = position;\n"
2141 const char *fs_source
=
2142 "uniform vec4 color;\n"
2145 " gl_FragColor = color;\n"
2148 bool has_integer_textures
;
2150 if (clear
->ArrayObj
!= 0)
2153 /* create vertex array object */
2154 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
2155 _mesa_BindVertexArray(clear
->ArrayObj
);
2157 /* create vertex array buffer */
2158 _mesa_GenBuffers(1, &clear
->VBO
);
2159 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2161 /* setup vertex arrays */
2162 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
2163 _mesa_EnableVertexAttribArray(0);
2165 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
2166 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
2167 _mesa_CompileShader(vs
);
2169 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
2170 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
2171 _mesa_CompileShader(fs
);
2173 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
2174 _mesa_AttachShader(clear
->ShaderProg
, fs
);
2175 _mesa_DeleteObjectARB(fs
);
2176 _mesa_AttachShader(clear
->ShaderProg
, vs
);
2177 _mesa_DeleteObjectARB(vs
);
2178 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
2179 _mesa_LinkProgram(clear
->ShaderProg
);
2181 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2184 has_integer_textures
= _mesa_is_gles3(ctx
) ||
2185 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
2187 if (has_integer_textures
) {
2188 void *shader_source_mem_ctx
= ralloc_context(NULL
);
2189 const char *vs_int_source
=
2190 ralloc_asprintf(shader_source_mem_ctx
,
2192 "in vec4 position;\n"
2195 " gl_Position = position;\n"
2197 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2198 const char *fs_int_source
=
2199 ralloc_asprintf(shader_source_mem_ctx
,
2201 "uniform ivec4 color;\n"
2202 "out ivec4 out_color;\n"
2206 " out_color = color;\n"
2208 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2210 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
2211 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
2212 ralloc_free(shader_source_mem_ctx
);
2214 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
2215 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
2216 _mesa_DeleteObjectARB(fs
);
2217 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
2218 _mesa_DeleteObjectARB(vs
);
2219 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
2221 /* Note that user-defined out attributes get automatically assigned
2222 * locations starting from 0, so we don't need to explicitly
2223 * BindFragDataLocation to 0.
2226 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
2228 clear
->IntegerColorLocation
=
2229 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2234 meta_glsl_clear_cleanup(struct gl_context
*ctx
, struct clear_state
*clear
)
2236 if (clear
->ArrayObj
== 0)
2238 _mesa_DeleteVertexArrays(1, &clear
->ArrayObj
);
2239 clear
->ArrayObj
= 0;
2240 _mesa_DeleteBuffers(1, &clear
->VBO
);
2242 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2243 clear
->ShaderProg
= 0;
2245 if (clear
->IntegerShaderProg
) {
2246 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2247 clear
->IntegerShaderProg
= 0;
2252 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2255 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2257 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2258 GLbitfield metaSave
;
2259 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2260 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2261 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2262 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2263 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2264 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2265 const float z
= -invert_z(ctx
->Depth
.Clear
);
2270 metaSave
= (MESA_META_ALPHA_TEST
|
2272 MESA_META_DEPTH_TEST
|
2273 MESA_META_RASTERIZATION
|
2275 MESA_META_STENCIL_TEST
|
2277 MESA_META_VIEWPORT
|
2279 MESA_META_CLAMP_FRAGMENT_COLOR
|
2280 MESA_META_MULTISAMPLE
|
2281 MESA_META_OCCLUSION_QUERY
);
2283 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2284 /* We'll use colormask to disable color writes. Otherwise,
2285 * respect color mask
2287 metaSave
|= MESA_META_COLOR_MASK
;
2290 _mesa_meta_begin(ctx
, metaSave
);
2292 meta_glsl_clear_init(ctx
, clear
);
2294 if (fb
->_IntegerColor
) {
2295 _mesa_UseProgram(clear
->IntegerShaderProg
);
2296 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2297 ctx
->Color
.ClearColor
.i
);
2299 _mesa_UseProgram(clear
->ShaderProg
);
2300 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2301 ctx
->Color
.ClearColor
.f
);
2304 _mesa_BindVertexArray(clear
->ArrayObj
);
2305 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2307 /* GL_COLOR_BUFFER_BIT */
2308 if (buffers
& BUFFER_BITS_COLOR
) {
2309 /* leave colormask, glDrawBuffer state as-is */
2311 /* Clears never have the color clamped. */
2312 if (ctx
->Extensions
.ARB_color_buffer_float
)
2313 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2316 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2317 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2320 /* GL_DEPTH_BUFFER_BIT */
2321 if (buffers
& BUFFER_BIT_DEPTH
) {
2322 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2323 _mesa_DepthFunc(GL_ALWAYS
);
2324 _mesa_DepthMask(GL_TRUE
);
2327 assert(!ctx
->Depth
.Test
);
2330 /* GL_STENCIL_BUFFER_BIT */
2331 if (buffers
& BUFFER_BIT_STENCIL
) {
2332 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2333 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2334 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2335 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2336 ctx
->Stencil
.Clear
& stencilMax
,
2337 ctx
->Stencil
.WriteMask
[0]);
2340 assert(!ctx
->Stencil
.Enabled
);
2343 /* vertex positions */
2357 /* upload new vertex data */
2358 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2359 GL_DYNAMIC_DRAW_ARB
);
2362 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2364 _mesa_meta_end(ctx
);
2368 * Meta implementation of ctx->Driver.CopyPixels() in terms
2369 * of texture mapping and polygon rendering and GLSL shaders.
2372 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2373 GLsizei width
, GLsizei height
,
2374 GLint dstX
, GLint dstY
, GLenum type
)
2376 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2377 struct temp_texture
*tex
= get_temp_texture(ctx
);
2379 GLfloat x
, y
, z
, s
, t
;
2381 struct vertex verts
[4];
2383 GLenum intFormat
= GL_RGBA
;
2385 if (type
!= GL_COLOR
||
2386 ctx
->_ImageTransferState
||
2388 width
> tex
->MaxSize
||
2389 height
> tex
->MaxSize
) {
2390 /* XXX avoid this fallback */
2391 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2395 /* Most GL state applies to glCopyPixels, but a there's a few things
2396 * we need to override:
2398 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2401 MESA_META_TRANSFORM
|
2404 MESA_META_VIEWPORT
));
2406 if (copypix
->ArrayObj
== 0) {
2407 /* one-time setup */
2409 /* create vertex array object */
2410 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
2411 _mesa_BindVertexArray(copypix
->ArrayObj
);
2413 /* create vertex array buffer */
2414 _mesa_GenBuffers(1, ©pix
->VBO
);
2415 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2416 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2417 NULL
, GL_DYNAMIC_DRAW_ARB
);
2419 /* setup vertex arrays */
2420 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2421 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2422 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2423 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2426 _mesa_BindVertexArray(copypix
->ArrayObj
);
2427 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2430 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2432 /* vertex positions, texcoords (after texture allocation!) */
2434 const GLfloat dstX0
= (GLfloat
) dstX
;
2435 const GLfloat dstY0
= (GLfloat
) dstY
;
2436 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2437 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2438 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2448 verts
[1].s
= tex
->Sright
;
2453 verts
[2].s
= tex
->Sright
;
2454 verts
[2].t
= tex
->Ttop
;
2459 verts
[3].t
= tex
->Ttop
;
2461 /* upload new vertex data */
2462 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2465 /* Alloc/setup texture */
2466 setup_copypix_texture(ctx
, tex
, newTex
, srcX
, srcY
, width
, height
,
2467 GL_RGBA
, GL_NEAREST
);
2469 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2471 /* draw textured quad */
2472 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2474 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2476 _mesa_meta_end(ctx
);
2482 * When the glDrawPixels() image size is greater than the max rectangle
2483 * texture size we use this function to break the glDrawPixels() image
2484 * into tiles which fit into the max texture size.
2487 tiled_draw_pixels(struct gl_context
*ctx
,
2489 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2490 GLenum format
, GLenum type
,
2491 const struct gl_pixelstore_attrib
*unpack
,
2492 const GLvoid
*pixels
)
2494 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2497 if (tileUnpack
.RowLength
== 0)
2498 tileUnpack
.RowLength
= width
;
2500 for (i
= 0; i
< width
; i
+= tileSize
) {
2501 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2502 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2504 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2506 for (j
= 0; j
< height
; j
+= tileSize
) {
2507 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2508 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2510 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2512 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2513 format
, type
, &tileUnpack
, pixels
);
2520 * One-time init for drawing stencil pixels.
2523 init_draw_stencil_pixels(struct gl_context
*ctx
)
2525 /* This program is run eight times, once for each stencil bit.
2526 * The stencil values to draw are found in an 8-bit alpha texture.
2527 * We read the texture/stencil value and test if bit 'b' is set.
2528 * If the bit is not set, use KIL to kill the fragment.
2529 * Finally, we use the stencil test to update the stencil buffer.
2531 * The basic algorithm for checking if a bit is set is:
2532 * if (is_odd(value / (1 << bit)))
2533 * result is one (or non-zero).
2536 * The program parameter contains three values:
2537 * parm.x = 255 / (1 << bit)
2541 static const char *program
=
2543 "PARAM parm = program.local[0]; \n"
2545 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2546 "# t = t * 255 / bit \n"
2547 "MUL t.x, t.a, parm.x; \n"
2550 "SUB t.x, t.x, t.y; \n"
2552 "MUL t.x, t.x, parm.y; \n"
2553 "# t = fract(t.x) \n"
2554 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2555 "# t.x = (t.x == 0 ? 1 : 0) \n"
2556 "SGE t.x, -t.x, parm.z; \n"
2558 "# for debug only \n"
2559 "#MOV result.color, t.x; \n"
2561 char program2
[1000];
2562 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2563 struct temp_texture
*tex
= get_temp_texture(ctx
);
2564 const char *texTarget
;
2566 assert(drawpix
->StencilFP
== 0);
2568 /* replace %s with "RECT" or "2D" */
2569 assert(strlen(program
) + 4 < sizeof(program2
));
2570 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2574 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2576 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2577 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2578 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2579 strlen(program2
), (const GLubyte
*) program2
);
2584 * One-time init for drawing depth pixels.
2587 init_draw_depth_pixels(struct gl_context
*ctx
)
2589 static const char *program
=
2591 "PARAM color = program.local[0]; \n"
2592 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2593 "MOV result.color, color; \n"
2596 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2597 struct temp_texture
*tex
= get_temp_texture(ctx
);
2598 const char *texTarget
;
2600 assert(drawpix
->DepthFP
== 0);
2602 /* replace %s with "RECT" or "2D" */
2603 assert(strlen(program
) + 4 < sizeof(program2
));
2604 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2608 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2610 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2611 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2612 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2613 strlen(program2
), (const GLubyte
*) program2
);
2618 * Meta implementation of ctx->Driver.DrawPixels() in terms
2619 * of texture mapping and polygon rendering.
2622 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2623 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2624 GLenum format
, GLenum type
,
2625 const struct gl_pixelstore_attrib
*unpack
,
2626 const GLvoid
*pixels
)
2628 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2629 struct temp_texture
*tex
= get_temp_texture(ctx
);
2630 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2631 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2633 GLfloat x
, y
, z
, s
, t
;
2635 struct vertex verts
[4];
2636 GLenum texIntFormat
;
2637 GLboolean fallback
, newTex
;
2638 GLbitfield metaExtraSave
= 0x0;
2642 * Determine if we can do the glDrawPixels with texture mapping.
2644 fallback
= GL_FALSE
;
2645 if (ctx
->Fog
.Enabled
) {
2649 if (_mesa_is_color_format(format
)) {
2650 /* use more compact format when possible */
2651 /* XXX disable special case for GL_LUMINANCE for now to work around
2652 * apparent i965 driver bug (see bug #23670).
2654 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2655 texIntFormat
= format
;
2657 texIntFormat
= GL_RGBA
;
2659 /* If we're not supposed to clamp the resulting color, then just
2660 * promote our texture to fully float. We could do better by
2661 * just going for the matching set of channels, in floating
2664 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2665 ctx
->Extensions
.ARB_texture_float
)
2666 texIntFormat
= GL_RGBA32F
;
2668 else if (_mesa_is_stencil_format(format
)) {
2669 if (ctx
->Extensions
.ARB_fragment_program
&&
2670 ctx
->Pixel
.IndexShift
== 0 &&
2671 ctx
->Pixel
.IndexOffset
== 0 &&
2672 type
== GL_UNSIGNED_BYTE
) {
2673 /* We'll store stencil as alpha. This only works for GLubyte
2674 * image data because of how incoming values are mapped to alpha
2677 texIntFormat
= GL_ALPHA
;
2678 metaExtraSave
= (MESA_META_COLOR_MASK
|
2679 MESA_META_DEPTH_TEST
|
2680 MESA_META_PIXEL_TRANSFER
|
2682 MESA_META_STENCIL_TEST
);
2688 else if (_mesa_is_depth_format(format
)) {
2689 if (ctx
->Extensions
.ARB_depth_texture
&&
2690 ctx
->Extensions
.ARB_fragment_program
) {
2691 texIntFormat
= GL_DEPTH_COMPONENT
;
2692 metaExtraSave
= (MESA_META_SHADER
);
2703 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2704 format
, type
, unpack
, pixels
);
2709 * Check image size against max texture size, draw as tiles if needed.
2711 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2712 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2713 format
, type
, unpack
, pixels
);
2717 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2718 * but a there's a few things we need to override:
2720 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2723 MESA_META_TRANSFORM
|
2726 MESA_META_VIEWPORT
|
2729 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2731 /* vertex positions, texcoords (after texture allocation!) */
2733 const GLfloat x0
= (GLfloat
) x
;
2734 const GLfloat y0
= (GLfloat
) y
;
2735 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2736 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2737 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2747 verts
[1].s
= tex
->Sright
;
2752 verts
[2].s
= tex
->Sright
;
2753 verts
[2].t
= tex
->Ttop
;
2758 verts
[3].t
= tex
->Ttop
;
2761 if (drawpix
->ArrayObj
== 0) {
2762 /* one-time setup: create vertex array object */
2763 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2765 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2767 /* create vertex array buffer */
2768 _mesa_GenBuffers(1, &vbo
);
2769 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2770 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2771 verts
, GL_DYNAMIC_DRAW_ARB
);
2773 /* setup vertex arrays */
2774 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2775 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2776 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2777 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2779 /* set given unpack params */
2780 ctx
->Unpack
= *unpack
;
2782 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2784 if (_mesa_is_stencil_format(format
)) {
2785 /* Drawing stencil */
2788 if (!drawpix
->StencilFP
)
2789 init_draw_stencil_pixels(ctx
);
2791 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2792 GL_ALPHA
, type
, pixels
);
2794 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2796 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2798 /* set all stencil bits to 0 */
2799 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2800 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2801 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2803 /* set stencil bits to 1 where needed */
2804 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2806 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2807 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2809 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2810 const GLuint mask
= 1 << bit
;
2811 if (mask
& origStencilMask
) {
2812 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2813 _mesa_StencilMask(mask
);
2815 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2816 255.0 / mask
, 0.5, 0.0, 0.0);
2818 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2822 else if (_mesa_is_depth_format(format
)) {
2824 if (!drawpix
->DepthFP
)
2825 init_draw_depth_pixels(ctx
);
2827 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2828 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2830 /* polygon color = current raster color */
2831 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2832 ctx
->Current
.RasterColor
);
2834 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2835 format
, type
, pixels
);
2837 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2841 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2842 format
, type
, pixels
);
2843 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2846 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2848 _mesa_DeleteBuffers(1, &vbo
);
2850 /* restore unpack params */
2851 ctx
->Unpack
= unpackSave
;
2853 _mesa_meta_end(ctx
);
2857 alpha_test_raster_color(struct gl_context
*ctx
)
2859 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2860 GLfloat ref
= ctx
->Color
.AlphaRef
;
2862 switch (ctx
->Color
.AlphaFunc
) {
2868 return alpha
== ref
;
2870 return alpha
<= ref
;
2874 return alpha
!= ref
;
2876 return alpha
>= ref
;
2886 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2887 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2888 * tracker would improve performance a lot.
2891 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2892 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2893 const struct gl_pixelstore_attrib
*unpack
,
2894 const GLubyte
*bitmap1
)
2896 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2897 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2898 const GLenum texIntFormat
= GL_ALPHA
;
2899 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2902 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2904 struct vertex verts
[4];
2909 * Check if swrast fallback is needed.
2911 if (ctx
->_ImageTransferState
||
2912 ctx
->FragmentProgram
._Enabled
||
2914 ctx
->Texture
._EnabledUnits
||
2915 width
> tex
->MaxSize
||
2916 height
> tex
->MaxSize
) {
2917 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2921 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2924 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2925 * but a there's a few things we need to override:
2927 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2928 MESA_META_PIXEL_STORE
|
2929 MESA_META_RASTERIZATION
|
2932 MESA_META_TRANSFORM
|
2935 MESA_META_VIEWPORT
));
2937 if (bitmap
->ArrayObj
== 0) {
2938 /* one-time setup */
2940 /* create vertex array object */
2941 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2942 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2944 /* create vertex array buffer */
2945 _mesa_GenBuffers(1, &bitmap
->VBO
);
2946 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2947 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2948 NULL
, GL_DYNAMIC_DRAW_ARB
);
2950 /* setup vertex arrays */
2951 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2952 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2953 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2954 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2955 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2956 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2959 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2960 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2963 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2965 /* vertex positions, texcoords, colors (after texture allocation!) */
2967 const GLfloat x0
= (GLfloat
) x
;
2968 const GLfloat y0
= (GLfloat
) y
;
2969 const GLfloat x1
= (GLfloat
) (x
+ width
);
2970 const GLfloat y1
= (GLfloat
) (y
+ height
);
2971 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2982 verts
[1].s
= tex
->Sright
;
2987 verts
[2].s
= tex
->Sright
;
2988 verts
[2].t
= tex
->Ttop
;
2993 verts
[3].t
= tex
->Ttop
;
2995 for (i
= 0; i
< 4; i
++) {
2996 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2997 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2998 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2999 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
3002 /* upload new vertex data */
3003 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3006 /* choose different foreground/background alpha values */
3007 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
3008 bg
= (fg
> 127 ? 0 : 255);
3010 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
3012 _mesa_meta_end(ctx
);
3016 bitmap8
= malloc(width
* height
);
3018 memset(bitmap8
, bg
, width
* height
);
3019 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
3020 bitmap8
, width
, fg
);
3022 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
3024 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
3025 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
3027 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
3028 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
3030 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3032 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
3037 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
3039 _mesa_meta_end(ctx
);
3044 * Check if the call to _mesa_meta_GenerateMipmap() will require a
3045 * software fallback. The fallback path will require that the texture
3046 * images are mapped.
3047 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
3050 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
3051 struct gl_texture_object
*texObj
)
3053 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3054 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3055 struct gl_texture_image
*baseImage
;
3059 /* check for fallbacks */
3060 if (target
== GL_TEXTURE_3D
||
3061 target
== GL_TEXTURE_1D_ARRAY
||
3062 target
== GL_TEXTURE_2D_ARRAY
) {
3063 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3064 "glGenerateMipmap() to %s target\n",
3065 _mesa_lookup_enum_by_nr(target
));
3069 srcLevel
= texObj
->BaseLevel
;
3070 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
3072 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3073 "glGenerateMipmap() couldn't find base teximage\n");
3077 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
3078 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3079 "glGenerateMipmap() with %s format\n",
3080 _mesa_get_format_name(baseImage
->TexFormat
));
3084 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
3085 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3086 /* The texture format is sRGB but we can't turn off sRGB->linear
3087 * texture sample conversion. So we won't be able to generate the
3088 * right colors when rendering. Need to use a fallback.
3090 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3091 "glGenerateMipmap() of sRGB texture without "
3097 * Test that we can actually render in the texture's format.
3100 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3101 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3103 if (target
== GL_TEXTURE_1D
) {
3104 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3105 GL_COLOR_ATTACHMENT0_EXT
,
3106 target
, texObj
->Name
, srcLevel
);
3109 /* other work is needed to enable 3D mipmap generation */
3110 else if (target
== GL_TEXTURE_3D
) {
3112 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3113 GL_COLOR_ATTACHMENT0_EXT
,
3114 target
, texObj
->Name
, srcLevel
, zoffset
);
3119 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3120 GL_COLOR_ATTACHMENT0_EXT
,
3121 target
, texObj
->Name
, srcLevel
);
3124 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3126 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3128 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3129 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3130 "glGenerateMipmap() got incomplete FBO\n");
3139 * Compute the texture coordinates for the four vertices of a quad for
3140 * drawing a 2D texture image or slice of a cube/3D texture.
3141 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
3142 * \param slice slice of a 1D/2D array texture or 3D texture
3143 * \param width width of the texture image
3144 * \param height height of the texture image
3145 * \param coords0/1/2/3 returns the computed texcoords
3148 setup_texture_coords(GLenum faceTarget
,
3158 static const GLfloat st
[4][2] = {
3159 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
3164 switch (faceTarget
) {
3168 case GL_TEXTURE_2D_ARRAY
:
3169 if (faceTarget
== GL_TEXTURE_3D
) {
3170 assert(slice
< depth
);
3172 r
= (slice
+ 0.5f
) / depth
;
3174 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
3178 coords0
[0] = 0.0F
; /* s */
3179 coords0
[1] = 0.0F
; /* t */
3180 coords0
[2] = r
; /* r */
3191 case GL_TEXTURE_RECTANGLE_ARB
:
3192 coords0
[0] = 0.0F
; /* s */
3193 coords0
[1] = 0.0F
; /* t */
3194 coords0
[2] = 0.0F
; /* r */
3199 coords2
[1] = height
;
3202 coords3
[1] = height
;
3205 case GL_TEXTURE_1D_ARRAY
:
3206 coords0
[0] = 0.0F
; /* s */
3207 coords0
[1] = slice
; /* t */
3208 coords0
[2] = 0.0F
; /* r */
3220 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3221 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3222 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3223 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3224 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3225 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3226 /* loop over quad verts */
3227 for (i
= 0; i
< 4; i
++) {
3228 /* Compute sc = +/-scale and tc = +/-scale.
3229 * Not +/-1 to avoid cube face selection ambiguity near the edges,
3230 * though that can still sometimes happen with this scale factor...
3232 const GLfloat scale
= 0.9999f
;
3233 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
3234 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
3254 switch (faceTarget
) {
3255 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3260 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3265 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3270 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3275 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3280 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3291 assert(0 && "unexpected target in meta setup_texture_coords()");
3297 setup_ff_generate_mipmap(struct gl_context
*ctx
,
3298 struct gen_mipmap_state
*mipmap
)
3301 GLfloat x
, y
, tex
[3];
3304 if (mipmap
->ArrayObj
== 0) {
3305 /* one-time setup */
3306 /* create vertex array object */
3307 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
3308 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
3310 /* create vertex array buffer */
3311 _mesa_GenBuffers(1, &mipmap
->VBO
);
3312 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3313 /* setup vertex arrays */
3314 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3315 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3316 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3317 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3320 /* setup projection matrix */
3321 _mesa_MatrixMode(GL_PROJECTION
);
3322 _mesa_LoadIdentity();
3323 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3327 static struct glsl_sampler
*
3328 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3332 mipmap
->sampler_1d
.type
= "sampler1D";
3333 mipmap
->sampler_1d
.func
= "texture1D";
3334 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3335 return &mipmap
->sampler_1d
;
3337 mipmap
->sampler_2d
.type
= "sampler2D";
3338 mipmap
->sampler_2d
.func
= "texture2D";
3339 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3340 return &mipmap
->sampler_2d
;
3342 /* Code for mipmap generation with 3D textures is not used yet.
3343 * It's a sw fallback.
3345 mipmap
->sampler_3d
.type
= "sampler3D";
3346 mipmap
->sampler_3d
.func
= "texture3D";
3347 mipmap
->sampler_3d
.texcoords
= "texCoords";
3348 return &mipmap
->sampler_3d
;
3349 case GL_TEXTURE_CUBE_MAP
:
3350 mipmap
->sampler_cubemap
.type
= "samplerCube";
3351 mipmap
->sampler_cubemap
.func
= "textureCube";
3352 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3353 return &mipmap
->sampler_cubemap
;
3354 case GL_TEXTURE_1D_ARRAY
:
3355 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3356 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3357 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3358 return &mipmap
->sampler_1d_array
;
3359 case GL_TEXTURE_2D_ARRAY
:
3360 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3361 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3362 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3363 return &mipmap
->sampler_2d_array
;
3365 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3366 " setup_texture_sampler()\n", target
);
3373 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3374 struct gen_mipmap_state
*mipmap
,
3378 GLfloat x
, y
, tex
[3];
3380 struct glsl_sampler
*sampler
;
3381 const char *vs_source
;
3386 /* Check if already initialized */
3387 if (mipmap
->ArrayObj
== 0) {
3389 /* create vertex array object */
3390 _mesa_GenVertexArrays(1, &mipmap
->ArrayObj
);
3391 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3393 /* create vertex array buffer */
3394 _mesa_GenBuffers(1, &mipmap
->VBO
);
3395 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3397 /* setup vertex arrays */
3398 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3399 sizeof(struct vertex
), OFFSET(x
));
3400 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3401 sizeof(struct vertex
), OFFSET(tex
));
3402 _mesa_EnableVertexAttribArray(0);
3403 _mesa_EnableVertexAttribArray(1);
3406 /* Generate a fragment shader program appropriate for the texture target */
3407 sampler
= setup_texture_sampler(target
, mipmap
);
3408 assert(sampler
!= NULL
);
3409 if (sampler
->shader_prog
!= 0) {
3410 mipmap
->ShaderProg
= sampler
->shader_prog
;
3414 mem_ctx
= ralloc_context(NULL
);
3416 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3418 "attribute vec2 position;\n"
3419 "attribute vec3 textureCoords;\n"
3420 "varying vec3 texCoords;\n"
3423 " texCoords = textureCoords;\n"
3424 " gl_Position = vec4(position, 0.0, 1.0);\n"
3427 fs_source
= ralloc_asprintf(mem_ctx
,
3428 "#extension GL_EXT_texture_array : enable\n"
3429 "uniform %s texSampler;\n"
3430 "varying vec3 texCoords;\n"
3433 " gl_FragColor = %s(texSampler, %s);\n"
3436 sampler
->func
, sampler
->texcoords
);
3439 vs_source
= ralloc_asprintf(mem_ctx
,
3441 "in vec2 position;\n"
3442 "in vec3 textureCoords;\n"
3443 "out vec3 texCoords;\n"
3446 " texCoords = textureCoords;\n"
3447 " gl_Position = vec4(position, 0.0, 1.0);\n"
3449 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
3450 fs_source
= ralloc_asprintf(mem_ctx
,
3452 "uniform %s texSampler;\n"
3453 "in vec3 texCoords;\n"
3454 "out vec4 out_color;\n"
3458 " out_color = texture(texSampler, %s);\n"
3460 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
3462 sampler
->texcoords
);
3465 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3466 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3468 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3469 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3470 _mesa_DeleteObjectARB(fs
);
3471 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3472 _mesa_DeleteObjectARB(vs
);
3473 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3474 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3475 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3476 sampler
->shader_prog
= mipmap
->ShaderProg
;
3477 ralloc_free(mem_ctx
);
3482 meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
3483 struct gen_mipmap_state
*mipmap
)
3485 if (mipmap
->ArrayObj
== 0)
3487 _mesa_DeleteVertexArrays(1, &mipmap
->ArrayObj
);
3488 mipmap
->ArrayObj
= 0;
3489 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3492 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3493 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3494 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3495 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3496 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3497 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3499 mipmap
->sampler_1d
.shader_prog
= 0;
3500 mipmap
->sampler_2d
.shader_prog
= 0;
3501 mipmap
->sampler_3d
.shader_prog
= 0;
3502 mipmap
->sampler_cubemap
.shader_prog
= 0;
3503 mipmap
->sampler_1d_array
.shader_prog
= 0;
3504 mipmap
->sampler_2d_array
.shader_prog
= 0;
3509 * Called via ctx->Driver.GenerateMipmap()
3510 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3514 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3515 struct gl_texture_object
*texObj
)
3517 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3519 GLfloat x
, y
, tex
[3];
3521 struct vertex verts
[4];
3522 const GLuint baseLevel
= texObj
->BaseLevel
;
3523 const GLuint maxLevel
= texObj
->MaxLevel
;
3524 const GLint maxLevelSave
= texObj
->MaxLevel
;
3525 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3526 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3527 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3528 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3529 ctx
->Extensions
.ARB_fragment_shader
&&
3530 (ctx
->API
!= API_OPENGLES
);
3533 const GLint slice
= 0;
3536 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3537 _mesa_generate_mipmap(ctx
, target
, texObj
);
3541 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3542 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3543 faceTarget
= target
;
3544 target
= GL_TEXTURE_CUBE_MAP
;
3547 faceTarget
= target
;
3550 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3552 /* Choose between glsl version and fixed function version of
3553 * GenerateMipmap function.
3555 if (use_glsl_version
) {
3556 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3557 _mesa_UseProgram(mipmap
->ShaderProg
);
3560 setup_ff_generate_mipmap(ctx
, mipmap
);
3561 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3564 _mesa_BindVertexArray(mipmap
->ArrayObj
);
3565 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3567 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3568 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3570 if (currentTexUnitSave
!= 0)
3571 _mesa_BindTexture(target
, texObj
->Name
);
3574 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3577 if (!mipmap
->Sampler
) {
3578 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3579 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3581 _mesa_SamplerParameteri(mipmap
->Sampler
,
3582 GL_TEXTURE_MIN_FILTER
,
3583 GL_LINEAR_MIPMAP_LINEAR
);
3584 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3585 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3586 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3587 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3589 /* We don't want to encode or decode sRGB values; treat them as linear.
3590 * This is not technically correct for GLES3 but we don't get any API
3591 * error at the moment.
3593 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3594 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3595 GL_SKIP_DECODE_EXT
);
3599 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3602 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3604 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3605 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3607 assert(!genMipmapSave
);
3609 /* Setup texture coordinates */
3610 setup_texture_coords(faceTarget
,
3612 0, 0, 1, /* width, height never used here */
3618 /* setup vertex positions */
3628 /* upload vertex data */
3629 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3630 verts
, GL_DYNAMIC_DRAW_ARB
);
3632 /* texture is already locked, unlock now */
3633 _mesa_unlock_texture(ctx
, texObj
);
3635 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3636 const struct gl_texture_image
*srcImage
;
3637 const GLuint srcLevel
= dstLevel
- 1;
3638 GLsizei srcWidth
, srcHeight
, srcDepth
;
3639 GLsizei dstWidth
, dstHeight
, dstDepth
;
3642 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3643 assert(srcImage
->Border
== 0);
3646 srcWidth
= srcImage
->Width
;
3647 srcHeight
= srcImage
->Height
;
3648 srcDepth
= srcImage
->Depth
;
3651 dstWidth
= MAX2(1, srcWidth
/ 2);
3652 dstHeight
= MAX2(1, srcHeight
/ 2);
3653 dstDepth
= MAX2(1, srcDepth
/ 2);
3655 if (dstWidth
== srcImage
->Width
&&
3656 dstHeight
== srcImage
->Height
&&
3657 dstDepth
== srcImage
->Depth
) {
3662 /* Allocate storage for the destination mipmap image(s) */
3664 /* Set MaxLevel large enough to hold the new level when we allocate it */
3665 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3667 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3668 dstWidth
, dstHeight
, dstDepth
,
3670 srcImage
->InternalFormat
,
3671 srcImage
->TexFormat
)) {
3672 /* All done. We either ran out of memory or we would go beyond the
3673 * last valid level of an immutable texture if we continued.
3678 /* limit minification to src level */
3679 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3681 /* Set to draw into the current dstLevel */
3682 if (target
== GL_TEXTURE_1D
) {
3683 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3684 GL_COLOR_ATTACHMENT0_EXT
,
3689 else if (target
== GL_TEXTURE_3D
) {
3690 GLint zoffset
= 0; /* XXX unfinished */
3691 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3692 GL_COLOR_ATTACHMENT0_EXT
,
3699 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3700 GL_COLOR_ATTACHMENT0_EXT
,
3706 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3709 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3710 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3711 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3712 "_mesa_meta_GenerateMipmap()");
3716 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3717 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3719 /* setup viewport */
3720 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3722 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3725 _mesa_lock_texture(ctx
, texObj
); /* relock */
3727 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3729 _mesa_meta_end(ctx
);
3731 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3733 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3735 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3740 * Determine the GL data type to use for the temporary image read with
3741 * ReadPixels() and passed to Tex[Sub]Image().
3744 get_temp_image_type(struct gl_context
*ctx
, gl_format format
)
3748 baseFormat
= _mesa_get_format_base_format(format
);
3750 switch (baseFormat
) {
3757 case GL_LUMINANCE_ALPHA
:
3759 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3760 return GL_UNSIGNED_BYTE
;
3761 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3762 return GL_UNSIGNED_SHORT
;
3764 GLenum datatype
= _mesa_get_format_datatype(format
);
3765 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3769 case GL_DEPTH_COMPONENT
: {
3770 GLenum datatype
= _mesa_get_format_datatype(format
);
3771 if (datatype
== GL_FLOAT
)
3774 return GL_UNSIGNED_INT
;
3776 case GL_DEPTH_STENCIL
: {
3777 GLenum datatype
= _mesa_get_format_datatype(format
);
3778 if (datatype
== GL_FLOAT
)
3779 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3781 return GL_UNSIGNED_INT_24_8
;
3784 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3792 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3793 * Have to be careful with locking and meta state for pixel transfer.
3796 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3797 struct gl_texture_image
*texImage
,
3798 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3799 struct gl_renderbuffer
*rb
,
3801 GLsizei width
, GLsizei height
)
3803 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3804 GLenum format
, type
;
3808 /* Choose format/type for temporary image buffer */
3809 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3810 if (format
== GL_LUMINANCE
||
3811 format
== GL_LUMINANCE_ALPHA
||
3812 format
== GL_INTENSITY
) {
3813 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3814 * temp image buffer because glReadPixels will do L=R+G+B which is
3815 * not what we want (should be L=R).
3820 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3821 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3822 format
= _mesa_base_format_to_integer_format(format
);
3824 bpp
= _mesa_bytes_per_pixel(format
, type
);
3826 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3831 * Alloc image buffer (XXX could use a PBO)
3833 buf
= malloc(width
* height
* bpp
);
3835 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3839 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3842 * Read image from framebuffer (disable pixel transfer ops)
3844 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3845 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3846 format
, type
, &ctx
->Pack
, buf
);
3847 _mesa_meta_end(ctx
);
3849 _mesa_update_state(ctx
); /* to update pixel transfer state */
3852 * Store texture data (with pixel transfer ops)
3854 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3856 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3857 assert(yoffset
== 0);
3858 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3859 xoffset
, zoffset
, 0, width
, 1, 1,
3860 format
, type
, buf
, &ctx
->Unpack
);
3862 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3863 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3864 format
, type
, buf
, &ctx
->Unpack
);
3867 _mesa_meta_end(ctx
);
3869 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3876 * Decompress a texture image by drawing a quad with the compressed
3877 * texture and reading the pixels out of the color buffer.
3878 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3879 * \param destFormat format, ala glReadPixels
3880 * \param destType type, ala glReadPixels
3881 * \param dest destination buffer
3882 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3885 decompress_texture_image(struct gl_context
*ctx
,
3886 struct gl_texture_image
*texImage
,
3888 GLenum destFormat
, GLenum destType
,
3891 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3892 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3893 const GLint width
= texImage
->Width
;
3894 const GLint height
= texImage
->Height
;
3895 const GLint depth
= texImage
->Height
;
3896 const GLenum target
= texObj
->Target
;
3899 GLfloat x
, y
, tex
[3];
3901 struct vertex verts
[4];
3902 GLuint fboDrawSave
, fboReadSave
;
3907 assert(target
== GL_TEXTURE_3D
||
3908 target
== GL_TEXTURE_2D_ARRAY
);
3911 if (target
== GL_TEXTURE_CUBE_MAP
) {
3912 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3915 faceTarget
= target
;
3918 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3919 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3920 fboReadSave
= ctx
->ReadBuffer
->Name
;
3921 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3923 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3925 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3926 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3928 /* Create/bind FBO/renderbuffer */
3929 if (decompress
->FBO
== 0) {
3930 _mesa_GenFramebuffers(1, &decompress
->FBO
);
3931 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
3932 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3933 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3934 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
3935 GL_COLOR_ATTACHMENT0_EXT
,
3936 GL_RENDERBUFFER_EXT
,
3940 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3943 /* alloc dest surface */
3944 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3945 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3946 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3948 decompress
->Width
= width
;
3949 decompress
->Height
= height
;
3952 /* setup VBO data */
3953 if (decompress
->ArrayObj
== 0) {
3954 /* create vertex array object */
3955 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3956 _mesa_BindVertexArray(decompress
->ArrayObj
);
3958 /* create vertex array buffer */
3959 _mesa_GenBuffers(1, &decompress
->VBO
);
3960 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3961 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3962 NULL
, GL_DYNAMIC_DRAW_ARB
);
3964 /* setup vertex arrays */
3965 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3966 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3967 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3968 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3971 _mesa_BindVertexArray(decompress
->ArrayObj
);
3972 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3975 if (!decompress
->Sampler
) {
3976 _mesa_GenSamplers(1, &decompress
->Sampler
);
3977 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3978 /* nearest filtering */
3979 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3980 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3981 /* No sRGB decode or encode.*/
3982 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3983 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3984 GL_SKIP_DECODE_EXT
);
3988 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
3991 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
3997 /* setup vertex positions */
4003 verts
[2].y
= height
;
4005 verts
[3].y
= height
;
4007 /* upload new vertex data */
4008 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4010 /* setup texture state */
4011 _mesa_BindTexture(target
, texObj
->Name
);
4012 _mesa_set_enable(ctx
, target
, GL_TRUE
);
4015 /* save texture object state */
4016 const GLint baseLevelSave
= texObj
->BaseLevel
;
4017 const GLint maxLevelSave
= texObj
->MaxLevel
;
4019 /* restrict sampling to the texture level of interest */
4020 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4021 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
4022 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
4025 /* render quad w/ texture into renderbuffer */
4026 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4028 /* Restore texture object state, the texture binding will
4029 * be restored by _mesa_meta_end().
4031 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4032 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
4033 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
4038 /* read pixels from renderbuffer */
4040 GLenum baseTexFormat
= texImage
->_BaseFormat
;
4041 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
4043 /* The pixel transfer state will be set to default values at this point
4044 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
4045 * turned off (as required by glGetTexImage) but we need to handle some
4046 * special cases. In particular, single-channel texture values are
4047 * returned as red and two-channel texture values are returned as
4050 if ((baseTexFormat
== GL_LUMINANCE
||
4051 baseTexFormat
== GL_LUMINANCE_ALPHA
||
4052 baseTexFormat
== GL_INTENSITY
) ||
4053 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
4054 * luminance then we need to return L=tex(R).
4056 ((baseTexFormat
== GL_RGBA
||
4057 baseTexFormat
== GL_RGB
||
4058 baseTexFormat
== GL_RG
) &&
4059 (destBaseFormat
== GL_LUMINANCE
||
4060 destBaseFormat
== GL_LUMINANCE_ALPHA
||
4061 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
4062 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
4063 /* Green and blue must be zero */
4064 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
4065 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
4068 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
4071 /* disable texture unit */
4072 _mesa_set_enable(ctx
, target
, GL_FALSE
);
4074 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
4076 _mesa_meta_end(ctx
);
4078 /* restore fbo bindings */
4079 if (fboDrawSave
== fboReadSave
) {
4080 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
4083 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
4084 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
4086 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
4091 * This is just a wrapper around _mesa_get_tex_image() and
4092 * decompress_texture_image(). Meta functions should not be directly called
4096 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
4097 GLenum format
, GLenum type
, GLvoid
*pixels
,
4098 struct gl_texture_image
*texImage
)
4100 /* We can only use the decompress-with-blit method here if the texels are
4101 * unsigned, normalized values. We could handle signed and unnormalized
4102 * with floating point renderbuffers...
4104 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
4105 _mesa_get_format_datatype(texImage
->TexFormat
)
4106 == GL_UNSIGNED_NORMALIZED
) {
4107 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4109 /* Need to unlock the texture here to prevent deadlock... */
4110 _mesa_unlock_texture(ctx
, texObj
);
4111 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
4113 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
4114 /* Setup pixel packing. SkipPixels and SkipRows will be applied
4115 * in the decompress_texture_image() function's call to
4116 * glReadPixels but we need to compute the dest slice's address
4117 * here (according to SkipImages and ImageHeight).
4119 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
4120 packing
.SkipPixels
= 0;
4121 packing
.SkipRows
= 0;
4122 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
4123 texImage
->Height
, format
, type
,
4129 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
4131 /* ... and relock it */
4132 _mesa_lock_texture(ctx
, texObj
);
4135 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
4141 * Meta implementation of ctx->Driver.DrawTex() in terms
4142 * of polygon rendering.
4145 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
4146 GLfloat width
, GLfloat height
)
4148 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
4150 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
4152 struct vertex verts
[4];
4155 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
4157 MESA_META_TRANSFORM
|
4159 MESA_META_VIEWPORT
));
4161 if (drawtex
->ArrayObj
== 0) {
4162 /* one-time setup */
4163 GLint active_texture
;
4165 /* create vertex array object */
4166 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
4167 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4169 /* create vertex array buffer */
4170 _mesa_GenBuffers(1, &drawtex
->VBO
);
4171 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4172 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4173 NULL
, GL_DYNAMIC_DRAW_ARB
);
4175 /* client active texture is not part of the array object */
4176 active_texture
= ctx
->Array
.ActiveTexture
;
4178 /* setup vertex arrays */
4179 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4180 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4181 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4182 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
4183 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
4184 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4187 /* restore client active texture */
4188 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
4191 _mesa_BindVertexArray(drawtex
->ArrayObj
);
4192 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4195 /* vertex positions, texcoords */
4197 const GLfloat x1
= x
+ width
;
4198 const GLfloat y1
= y
+ height
;
4200 z
= CLAMP(z
, 0.0f
, 1.0f
);
4219 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4220 const struct gl_texture_object
*texObj
;
4221 const struct gl_texture_image
*texImage
;
4222 GLfloat s
, t
, s1
, t1
;
4225 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
4227 for (j
= 0; j
< 4; j
++) {
4228 verts
[j
].st
[i
][0] = 0.0f
;
4229 verts
[j
].st
[i
][1] = 0.0f
;
4234 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
4235 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
4236 tw
= texImage
->Width2
;
4237 th
= texImage
->Height2
;
4239 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
4240 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
4241 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
4242 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
4244 verts
[0].st
[i
][0] = s
;
4245 verts
[0].st
[i
][1] = t
;
4247 verts
[1].st
[i
][0] = s1
;
4248 verts
[1].st
[i
][1] = t
;
4250 verts
[2].st
[i
][0] = s1
;
4251 verts
[2].st
[i
][1] = t1
;
4253 verts
[3].st
[i
][0] = s
;
4254 verts
[3].st
[i
][1] = t1
;
4257 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4260 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4262 _mesa_meta_end(ctx
);