2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/condrender.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/feedback.h"
48 #include "main/formats.h"
49 #include "main/glformats.h"
50 #include "main/image.h"
51 #include "main/macros.h"
52 #include "main/matrix.h"
53 #include "main/mipmap.h"
54 #include "main/pixel.h"
56 #include "main/polygon.h"
57 #include "main/queryobj.h"
58 #include "main/readpix.h"
59 #include "main/scissor.h"
60 #include "main/shaderapi.h"
61 #include "main/shaderobj.h"
62 #include "main/state.h"
63 #include "main/stencil.h"
64 #include "main/texobj.h"
65 #include "main/texenv.h"
66 #include "main/texgetimage.h"
67 #include "main/teximage.h"
68 #include "main/texparam.h"
69 #include "main/texstate.h"
70 #include "main/transformfeedback.h"
71 #include "main/uniforms.h"
72 #include "main/varray.h"
73 #include "main/viewport.h"
74 #include "main/samplerobj.h"
75 #include "program/program.h"
76 #include "swrast/swrast.h"
77 #include "drivers/common/meta.h"
78 #include "main/enums.h"
79 #include "main/glformats.h"
80 #include "../glsl/ralloc.h"
82 /** Return offset in bytes of the field within a vertex struct */
83 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
86 * State which we may save/restore across meta ops.
87 * XXX this may be incomplete...
91 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
93 /** MESA_META_CLEAR (and others?) */
94 struct gl_query_object
*CurrentOcclusionObject
;
96 /** MESA_META_ALPHA_TEST */
97 GLboolean AlphaEnabled
;
101 /** MESA_META_BLEND */
102 GLbitfield BlendEnabled
;
103 GLboolean ColorLogicOpEnabled
;
105 /** MESA_META_COLOR_MASK */
106 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
108 /** MESA_META_DEPTH_TEST */
109 struct gl_depthbuffer_attrib Depth
;
114 /** MESA_META_PIXEL_STORE */
115 struct gl_pixelstore_attrib Pack
, Unpack
;
117 /** MESA_META_PIXEL_TRANSFER */
118 GLfloat RedBias
, RedScale
;
119 GLfloat GreenBias
, GreenScale
;
120 GLfloat BlueBias
, BlueScale
;
121 GLfloat AlphaBias
, AlphaScale
;
122 GLfloat DepthBias
, DepthScale
;
123 GLboolean MapColorFlag
;
125 /** MESA_META_RASTERIZATION */
126 GLenum FrontPolygonMode
, BackPolygonMode
;
127 GLboolean PolygonOffset
;
128 GLboolean PolygonSmooth
;
129 GLboolean PolygonStipple
;
130 GLboolean PolygonCull
;
132 /** MESA_META_SCISSOR */
133 struct gl_scissor_attrib Scissor
;
135 /** MESA_META_SHADER */
136 GLboolean VertexProgramEnabled
;
137 struct gl_vertex_program
*VertexProgram
;
138 GLboolean FragmentProgramEnabled
;
139 struct gl_fragment_program
*FragmentProgram
;
140 GLboolean ATIFragmentShaderEnabled
;
141 struct gl_shader_program
*Shader
[MESA_SHADER_STAGES
];
142 struct gl_shader_program
*ActiveShader
;
144 /** MESA_META_STENCIL_TEST */
145 struct gl_stencil_attrib Stencil
;
147 /** MESA_META_TRANSFORM */
149 GLfloat ModelviewMatrix
[16];
150 GLfloat ProjectionMatrix
[16];
151 GLfloat TextureMatrix
[16];
153 /** MESA_META_CLIP */
154 GLbitfield ClipPlanesEnabled
;
156 /** MESA_META_TEXTURE */
158 GLuint ClientActiveUnit
;
159 /** for unit[0] only */
160 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
161 /** mask of TEXTURE_2D_BIT, etc */
162 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
163 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
164 GLuint EnvMode
; /* unit[0] only */
166 /** MESA_META_VERTEX */
167 struct gl_vertex_array_object
*VAO
;
168 struct gl_buffer_object
*ArrayBufferObj
;
170 /** MESA_META_VIEWPORT */
171 GLfloat ViewportX
, ViewportY
, ViewportW
, ViewportH
;
172 GLclampd DepthNear
, DepthFar
;
174 /** MESA_META_CLAMP_FRAGMENT_COLOR */
175 GLenum ClampFragmentColor
;
177 /** MESA_META_CLAMP_VERTEX_COLOR */
178 GLenum ClampVertexColor
;
180 /** MESA_META_CONDITIONAL_RENDER */
181 struct gl_query_object
*CondRenderQuery
;
182 GLenum CondRenderMode
;
184 /** MESA_META_SELECT_FEEDBACK */
186 struct gl_selection Select
;
187 struct gl_feedback Feedback
;
189 /** MESA_META_MULTISAMPLE */
190 GLboolean MultisampleEnabled
;
192 /** MESA_META_FRAMEBUFFER_SRGB */
193 GLboolean sRGBEnabled
;
195 /** Miscellaneous (always disabled) */
197 GLboolean RasterDiscard
;
198 GLboolean TransformFeedbackNeedsResume
;
202 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
203 * This is currently shared by all the meta ops. But we could create a
204 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
209 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
210 GLsizei MinSize
; /**< Min texture size to allocate */
211 GLsizei MaxSize
; /**< Max possible texture size */
212 GLboolean NPOT
; /**< Non-power of two size OK? */
213 GLsizei Width
, Height
; /**< Current texture size */
215 GLfloat Sright
, Ttop
; /**< right, top texcoords */
220 * State for glBlitFramebufer()
228 GLuint RectShaderProg
;
229 struct temp_texture depthTex
;
234 * State for glClear()
244 GLuint IntegerShaderProg
;
245 GLint IntegerColorLocation
;
246 GLint IntegerLayerLocation
;
251 * State for glCopyPixels()
261 * State for glDrawPixels()
267 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
268 GLuint DepthFP
; /**< Fragment program for drawing depth images */
273 * State for glBitmap()
279 struct temp_texture Tex
; /**< separate texture from other meta ops */
283 * State for GLSL texture sampler which is used to generate fragment
284 * shader in _mesa_meta_generate_mipmap().
286 struct glsl_sampler
{
289 const char *texcoords
;
294 * State for _mesa_meta_generate_mipmap()
296 struct gen_mipmap_state
303 struct glsl_sampler sampler_1d
;
304 struct glsl_sampler sampler_2d
;
305 struct glsl_sampler sampler_3d
;
306 struct glsl_sampler sampler_cubemap
;
307 struct glsl_sampler sampler_1d_array
;
308 struct glsl_sampler sampler_2d_array
;
312 * State for texture decompression
314 struct decompress_state
317 GLuint VBO
, FBO
, RBO
, Sampler
;
322 * State for glDrawTex()
330 #define MAX_META_OPS_DEPTH 8
332 * All per-context meta state.
336 /** Stack of state saved during meta-ops */
337 struct save_state Save
[MAX_META_OPS_DEPTH
];
338 /** Save stack depth */
339 GLuint SaveStackDepth
;
341 struct temp_texture TempTex
;
343 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
344 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
345 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
346 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
347 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
348 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
349 struct decompress_state Decompress
; /**< For texture decompression */
350 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
353 static void meta_glsl_blit_cleanup(struct blit_state
*blit
);
354 static void cleanup_temp_texture(struct temp_texture
*tex
);
355 static void meta_glsl_clear_cleanup(struct clear_state
*clear
);
356 static void meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state
*mipmap
);
357 static void meta_decompress_cleanup(struct decompress_state
*decompress
);
358 static void meta_drawpix_cleanup(struct drawpix_state
*drawpix
);
361 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
367 shader
= _mesa_CreateShaderObjectARB(target
);
368 _mesa_ShaderSource(shader
, 1, &source
, NULL
);
369 _mesa_CompileShader(shader
);
371 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
375 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
377 _mesa_DeleteObjectARB(shader
);
383 _mesa_DeleteObjectARB(shader
);
387 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
389 "meta program compile failed:\n%s\n"
394 _mesa_DeleteObjectARB(shader
);
400 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
405 _mesa_LinkProgram(program
);
407 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
411 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
419 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
420 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
428 * Initialize meta-ops for a context.
429 * To be called once during context creation.
432 _mesa_meta_init(struct gl_context
*ctx
)
436 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
441 * Free context meta-op state.
442 * To be called once during context destruction.
445 _mesa_meta_free(struct gl_context
*ctx
)
447 GET_CURRENT_CONTEXT(old_context
);
448 _mesa_make_current(ctx
, NULL
, NULL
);
449 meta_glsl_blit_cleanup(&ctx
->Meta
->Blit
);
450 meta_glsl_clear_cleanup(&ctx
->Meta
->Clear
);
451 meta_glsl_generate_mipmap_cleanup(&ctx
->Meta
->Mipmap
);
452 cleanup_temp_texture(&ctx
->Meta
->TempTex
);
453 meta_decompress_cleanup(&ctx
->Meta
->Decompress
);
454 meta_drawpix_cleanup(&ctx
->Meta
->DrawPix
);
456 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
458 _mesa_make_current(NULL
, NULL
, NULL
);
465 * Enter meta state. This is like a light-weight version of glPushAttrib
466 * but it also resets most GL state back to default values.
468 * \param state bitmask of MESA_META_* flags indicating which attribute groups
469 * to save and reset to their defaults
472 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
474 struct save_state
*save
;
476 /* hope MAX_META_OPS_DEPTH is large enough */
477 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
479 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
480 memset(save
, 0, sizeof(*save
));
481 save
->SavedState
= state
;
483 /* Pausing transform feedback needs to be done early, or else we won't be
484 * able to change other state.
486 save
->TransformFeedbackNeedsResume
=
487 _mesa_is_xfb_active_and_unpaused(ctx
);
488 if (save
->TransformFeedbackNeedsResume
)
489 _mesa_PauseTransformFeedback();
491 /* After saving the current occlusion object, call EndQuery so that no
492 * occlusion querying will be active during the meta-operation.
494 if (state
& MESA_META_OCCLUSION_QUERY
) {
495 save
->CurrentOcclusionObject
= ctx
->Query
.CurrentOcclusionObject
;
496 if (save
->CurrentOcclusionObject
)
497 _mesa_EndQuery(save
->CurrentOcclusionObject
->Target
);
500 if (state
& MESA_META_ALPHA_TEST
) {
501 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
502 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
503 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
504 if (ctx
->Color
.AlphaEnabled
)
505 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
508 if (state
& MESA_META_BLEND
) {
509 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
510 if (ctx
->Color
.BlendEnabled
) {
511 if (ctx
->Extensions
.EXT_draw_buffers2
) {
513 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
514 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
518 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
521 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
522 if (ctx
->Color
.ColorLogicOpEnabled
)
523 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
526 if (state
& MESA_META_COLOR_MASK
) {
527 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
528 sizeof(ctx
->Color
.ColorMask
));
529 if (!ctx
->Color
.ColorMask
[0][0] ||
530 !ctx
->Color
.ColorMask
[0][1] ||
531 !ctx
->Color
.ColorMask
[0][2] ||
532 !ctx
->Color
.ColorMask
[0][3])
533 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
536 if (state
& MESA_META_DEPTH_TEST
) {
537 save
->Depth
= ctx
->Depth
; /* struct copy */
539 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
542 if ((state
& MESA_META_FOG
)
543 && ctx
->API
!= API_OPENGL_CORE
544 && ctx
->API
!= API_OPENGLES2
) {
545 save
->Fog
= ctx
->Fog
.Enabled
;
546 if (ctx
->Fog
.Enabled
)
547 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
550 if (state
& MESA_META_PIXEL_STORE
) {
551 save
->Pack
= ctx
->Pack
;
552 save
->Unpack
= ctx
->Unpack
;
553 ctx
->Pack
= ctx
->DefaultPacking
;
554 ctx
->Unpack
= ctx
->DefaultPacking
;
557 if (state
& MESA_META_PIXEL_TRANSFER
) {
558 save
->RedScale
= ctx
->Pixel
.RedScale
;
559 save
->RedBias
= ctx
->Pixel
.RedBias
;
560 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
561 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
562 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
563 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
564 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
565 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
566 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
567 ctx
->Pixel
.RedScale
= 1.0F
;
568 ctx
->Pixel
.RedBias
= 0.0F
;
569 ctx
->Pixel
.GreenScale
= 1.0F
;
570 ctx
->Pixel
.GreenBias
= 0.0F
;
571 ctx
->Pixel
.BlueScale
= 1.0F
;
572 ctx
->Pixel
.BlueBias
= 0.0F
;
573 ctx
->Pixel
.AlphaScale
= 1.0F
;
574 ctx
->Pixel
.AlphaBias
= 0.0F
;
575 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
577 ctx
->NewState
|=_NEW_PIXEL
;
580 if (state
& MESA_META_RASTERIZATION
) {
581 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
582 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
583 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
584 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
585 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
586 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
587 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
588 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
589 if (ctx
->API
== API_OPENGL_COMPAT
) {
590 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
591 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
593 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
596 if (state
& MESA_META_SCISSOR
) {
597 save
->Scissor
= ctx
->Scissor
; /* struct copy */
598 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
601 if (state
& MESA_META_SHADER
) {
604 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
605 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
606 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
607 ctx
->VertexProgram
.Current
);
608 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
611 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
612 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
613 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
614 ctx
->FragmentProgram
.Current
);
615 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
618 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
619 save
->ATIFragmentShaderEnabled
= ctx
->ATIFragmentShader
.Enabled
;
620 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
, GL_FALSE
);
623 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
624 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
],
625 ctx
->Shader
.CurrentProgram
[i
]);
627 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
628 ctx
->Shader
.ActiveProgram
);
633 if (state
& MESA_META_STENCIL_TEST
) {
634 save
->Stencil
= ctx
->Stencil
; /* struct copy */
635 if (ctx
->Stencil
.Enabled
)
636 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
637 /* NOTE: other stencil state not reset */
640 if (state
& MESA_META_TEXTURE
) {
643 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
644 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
645 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
647 /* Disable all texture units */
648 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
649 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
650 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
651 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
652 if (ctx
->Texture
.Unit
[u
].Enabled
||
653 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
654 _mesa_ActiveTexture(GL_TEXTURE0
+ u
);
655 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
656 if (ctx
->Extensions
.ARB_texture_cube_map
)
657 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
658 if (_mesa_is_gles(ctx
) &&
659 ctx
->Extensions
.OES_EGL_image_external
)
660 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
662 if (ctx
->API
== API_OPENGL_COMPAT
) {
663 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
664 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
665 if (ctx
->Extensions
.NV_texture_rectangle
)
666 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
667 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
668 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
669 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
670 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
672 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_STR_OES
, GL_FALSE
);
678 /* save current texture objects for unit[0] only */
679 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
680 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
681 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
684 /* set defaults for unit[0] */
685 _mesa_ActiveTexture(GL_TEXTURE0
);
686 _mesa_ClientActiveTexture(GL_TEXTURE0
);
687 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
688 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
692 if (state
& MESA_META_TRANSFORM
) {
693 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
694 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
695 16 * sizeof(GLfloat
));
696 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
697 16 * sizeof(GLfloat
));
698 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
699 16 * sizeof(GLfloat
));
700 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
701 /* set 1:1 vertex:pixel coordinate transform */
702 _mesa_ActiveTexture(GL_TEXTURE0
);
703 _mesa_MatrixMode(GL_TEXTURE
);
704 _mesa_LoadIdentity();
705 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
706 _mesa_MatrixMode(GL_MODELVIEW
);
707 _mesa_LoadIdentity();
708 _mesa_MatrixMode(GL_PROJECTION
);
709 _mesa_LoadIdentity();
711 /* glOrtho with width = 0 or height = 0 generates GL_INVALID_VALUE.
712 * This can occur when there is no draw buffer.
714 if (ctx
->DrawBuffer
->Width
!= 0 && ctx
->DrawBuffer
->Height
!= 0)
715 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
716 0.0, ctx
->DrawBuffer
->Height
,
720 if (state
& MESA_META_CLIP
) {
721 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
722 if (ctx
->Transform
.ClipPlanesEnabled
) {
724 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
725 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
730 if (state
& MESA_META_VERTEX
) {
731 /* save vertex array object state */
732 _mesa_reference_array_object(ctx
, &save
->VAO
,
734 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
735 ctx
->Array
.ArrayBufferObj
);
736 /* set some default state? */
739 if (state
& MESA_META_VIEWPORT
) {
740 /* save viewport state */
741 save
->ViewportX
= ctx
->ViewportArray
[0].X
;
742 save
->ViewportY
= ctx
->ViewportArray
[0].Y
;
743 save
->ViewportW
= ctx
->ViewportArray
[0].Width
;
744 save
->ViewportH
= ctx
->ViewportArray
[0].Height
;
745 /* set viewport to match window size */
746 if (ctx
->ViewportArray
[0].X
!= 0 ||
747 ctx
->ViewportArray
[0].Y
!= 0 ||
748 ctx
->ViewportArray
[0].Width
!= (float) ctx
->DrawBuffer
->Width
||
749 ctx
->ViewportArray
[0].Height
!= (float) ctx
->DrawBuffer
->Height
) {
750 _mesa_set_viewport(ctx
, 0, 0, 0,
751 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
753 /* save depth range state */
754 save
->DepthNear
= ctx
->ViewportArray
[0].Near
;
755 save
->DepthFar
= ctx
->ViewportArray
[0].Far
;
756 /* set depth range to default */
757 _mesa_DepthRange(0.0, 1.0);
760 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
761 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
763 /* Generally in here we want to do clamping according to whether
764 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
765 * regardless of the internal implementation of the metaops.
767 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
768 ctx
->Extensions
.ARB_color_buffer_float
)
769 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
772 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
773 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
775 /* Generally in here we never want vertex color clamping --
776 * result clamping is only dependent on fragment clamping.
778 if (ctx
->Extensions
.ARB_color_buffer_float
)
779 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
782 if (state
& MESA_META_CONDITIONAL_RENDER
) {
783 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
784 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
786 if (ctx
->Query
.CondRenderQuery
)
787 _mesa_EndConditionalRender();
790 if (state
& MESA_META_SELECT_FEEDBACK
) {
791 save
->RenderMode
= ctx
->RenderMode
;
792 if (ctx
->RenderMode
== GL_SELECT
) {
793 save
->Select
= ctx
->Select
; /* struct copy */
794 _mesa_RenderMode(GL_RENDER
);
795 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
796 save
->Feedback
= ctx
->Feedback
; /* struct copy */
797 _mesa_RenderMode(GL_RENDER
);
801 if (state
& MESA_META_MULTISAMPLE
) {
802 save
->MultisampleEnabled
= ctx
->Multisample
.Enabled
;
803 if (ctx
->Multisample
.Enabled
)
804 _mesa_set_multisample(ctx
, GL_FALSE
);
807 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
808 save
->sRGBEnabled
= ctx
->Color
.sRGBEnabled
;
809 if (ctx
->Color
.sRGBEnabled
)
810 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
815 save
->Lighting
= ctx
->Light
.Enabled
;
816 if (ctx
->Light
.Enabled
)
817 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
818 save
->RasterDiscard
= ctx
->RasterDiscard
;
819 if (ctx
->RasterDiscard
)
820 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
826 * Leave meta state. This is like a light-weight version of glPopAttrib().
829 _mesa_meta_end(struct gl_context
*ctx
)
831 struct save_state
*save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
- 1];
832 const GLbitfield state
= save
->SavedState
;
835 /* After starting a new occlusion query, initialize the results to the
836 * values saved previously. The driver will then continue to increment
839 if (state
& MESA_META_OCCLUSION_QUERY
) {
840 if (save
->CurrentOcclusionObject
) {
841 _mesa_BeginQuery(save
->CurrentOcclusionObject
->Target
,
842 save
->CurrentOcclusionObject
->Id
);
843 ctx
->Query
.CurrentOcclusionObject
->Result
= save
->CurrentOcclusionObject
->Result
;
847 if (state
& MESA_META_ALPHA_TEST
) {
848 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
849 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
850 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
853 if (state
& MESA_META_BLEND
) {
854 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
855 if (ctx
->Extensions
.EXT_draw_buffers2
) {
857 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
858 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
862 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
865 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
866 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
869 if (state
& MESA_META_COLOR_MASK
) {
871 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
872 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
874 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
875 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
879 save
->ColorMask
[i
][0],
880 save
->ColorMask
[i
][1],
881 save
->ColorMask
[i
][2],
882 save
->ColorMask
[i
][3]);
888 if (state
& MESA_META_DEPTH_TEST
) {
889 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
890 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
891 _mesa_DepthFunc(save
->Depth
.Func
);
892 _mesa_DepthMask(save
->Depth
.Mask
);
895 if ((state
& MESA_META_FOG
)
896 && ctx
->API
!= API_OPENGL_CORE
897 && ctx
->API
!= API_OPENGLES2
) {
898 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
901 if (state
& MESA_META_PIXEL_STORE
) {
902 ctx
->Pack
= save
->Pack
;
903 ctx
->Unpack
= save
->Unpack
;
906 if (state
& MESA_META_PIXEL_TRANSFER
) {
907 ctx
->Pixel
.RedScale
= save
->RedScale
;
908 ctx
->Pixel
.RedBias
= save
->RedBias
;
909 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
910 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
911 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
912 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
913 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
914 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
915 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
917 ctx
->NewState
|=_NEW_PIXEL
;
920 if (state
& MESA_META_RASTERIZATION
) {
921 /* Core context requires that front and back mode be the same.
923 if (ctx
->API
== API_OPENGL_CORE
) {
924 _mesa_PolygonMode(GL_FRONT_AND_BACK
, save
->FrontPolygonMode
);
926 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
927 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
929 if (ctx
->API
== API_OPENGL_COMPAT
) {
930 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
931 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
933 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
934 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
937 if (state
& MESA_META_SCISSOR
) {
940 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++) {
941 _mesa_set_scissor(ctx
, i
,
942 save
->Scissor
.ScissorArray
[i
].X
,
943 save
->Scissor
.ScissorArray
[i
].Y
,
944 save
->Scissor
.ScissorArray
[i
].Width
,
945 save
->Scissor
.ScissorArray
[i
].Height
);
946 _mesa_set_enablei(ctx
, GL_SCISSOR_TEST
, i
,
947 (save
->Scissor
.EnableFlags
>> i
) & 1);
951 if (state
& MESA_META_SHADER
) {
952 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_vertex_program
) {
953 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
954 save
->VertexProgramEnabled
);
955 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
956 save
->VertexProgram
);
957 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
960 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ARB_fragment_program
) {
961 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
962 save
->FragmentProgramEnabled
);
963 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
964 save
->FragmentProgram
);
965 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
968 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.ATI_fragment_shader
) {
969 _mesa_set_enable(ctx
, GL_FRAGMENT_SHADER_ATI
,
970 save
->ATIFragmentShaderEnabled
);
973 if (ctx
->Extensions
.ARB_vertex_shader
) {
974 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
,
975 save
->Shader
[MESA_SHADER_VERTEX
]);
978 if (_mesa_has_geometry_shaders(ctx
))
979 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
980 save
->Shader
[MESA_SHADER_GEOMETRY
]);
982 if (ctx
->Extensions
.ARB_fragment_shader
)
983 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
984 save
->Shader
[MESA_SHADER_FRAGMENT
]);
986 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
989 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
990 _mesa_reference_shader_program(ctx
, &save
->Shader
[i
], NULL
);
991 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
994 if (state
& MESA_META_STENCIL_TEST
) {
995 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
997 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
998 _mesa_ClearStencil(stencil
->Clear
);
999 if (ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Extensions
.EXT_stencil_two_side
) {
1000 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
1001 stencil
->TestTwoSide
);
1002 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
1003 ? GL_BACK
: GL_FRONT
);
1006 _mesa_StencilFuncSeparate(GL_FRONT
,
1007 stencil
->Function
[0],
1009 stencil
->ValueMask
[0]);
1010 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
1011 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
1012 stencil
->ZFailFunc
[0],
1013 stencil
->ZPassFunc
[0]);
1015 _mesa_StencilFuncSeparate(GL_BACK
,
1016 stencil
->Function
[1],
1018 stencil
->ValueMask
[1]);
1019 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
1020 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
1021 stencil
->ZFailFunc
[1],
1022 stencil
->ZPassFunc
[1]);
1025 if (state
& MESA_META_TEXTURE
) {
1028 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
1030 /* restore texenv for unit[0] */
1031 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1032 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
1035 /* restore texture objects for unit[0] only */
1036 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1037 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
1038 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1039 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
1040 save
->CurrentTexture
[tgt
]);
1042 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
1045 /* Restore fixed function texture enables, texgen */
1046 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1047 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1048 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
1049 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1050 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
1053 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
1054 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1055 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
1060 /* restore current unit state */
1061 _mesa_ActiveTexture(GL_TEXTURE0
+ save
->ActiveUnit
);
1062 _mesa_ClientActiveTexture(GL_TEXTURE0
+ save
->ClientActiveUnit
);
1065 if (state
& MESA_META_TRANSFORM
) {
1066 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
1067 _mesa_ActiveTexture(GL_TEXTURE0
);
1068 _mesa_MatrixMode(GL_TEXTURE
);
1069 _mesa_LoadMatrixf(save
->TextureMatrix
);
1070 _mesa_ActiveTexture(GL_TEXTURE0
+ activeTexture
);
1072 _mesa_MatrixMode(GL_MODELVIEW
);
1073 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
1075 _mesa_MatrixMode(GL_PROJECTION
);
1076 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
1078 _mesa_MatrixMode(save
->MatrixMode
);
1081 if (state
& MESA_META_CLIP
) {
1082 if (save
->ClipPlanesEnabled
) {
1084 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
1085 if (save
->ClipPlanesEnabled
& (1 << i
)) {
1086 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
1092 if (state
& MESA_META_VERTEX
) {
1093 /* restore vertex buffer object */
1094 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
1095 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
1097 /* restore vertex array object */
1098 _mesa_BindVertexArray(save
->VAO
->Name
);
1099 _mesa_reference_array_object(ctx
, &save
->VAO
, NULL
);
1102 if (state
& MESA_META_VIEWPORT
) {
1103 if (save
->ViewportX
!= ctx
->ViewportArray
[0].X
||
1104 save
->ViewportY
!= ctx
->ViewportArray
[0].Y
||
1105 save
->ViewportW
!= ctx
->ViewportArray
[0].Width
||
1106 save
->ViewportH
!= ctx
->ViewportArray
[0].Height
) {
1107 _mesa_set_viewport(ctx
, 0, save
->ViewportX
, save
->ViewportY
,
1108 save
->ViewportW
, save
->ViewportH
);
1110 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
1113 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
&&
1114 ctx
->Extensions
.ARB_color_buffer_float
) {
1115 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
1118 if (state
& MESA_META_CLAMP_VERTEX_COLOR
&&
1119 ctx
->Extensions
.ARB_color_buffer_float
) {
1120 _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
1123 if (state
& MESA_META_CONDITIONAL_RENDER
) {
1124 if (save
->CondRenderQuery
)
1125 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
1126 save
->CondRenderMode
);
1129 if (state
& MESA_META_SELECT_FEEDBACK
) {
1130 if (save
->RenderMode
== GL_SELECT
) {
1131 _mesa_RenderMode(GL_SELECT
);
1132 ctx
->Select
= save
->Select
;
1133 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1134 _mesa_RenderMode(GL_FEEDBACK
);
1135 ctx
->Feedback
= save
->Feedback
;
1139 if (state
& MESA_META_MULTISAMPLE
) {
1140 if (ctx
->Multisample
.Enabled
!= save
->MultisampleEnabled
)
1141 _mesa_set_multisample(ctx
, save
->MultisampleEnabled
);
1144 if (state
& MESA_META_FRAMEBUFFER_SRGB
) {
1145 if (ctx
->Color
.sRGBEnabled
!= save
->sRGBEnabled
)
1146 _mesa_set_framebuffer_srgb(ctx
, save
->sRGBEnabled
);
1150 if (save
->Lighting
) {
1151 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1153 if (save
->RasterDiscard
) {
1154 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1156 if (save
->TransformFeedbackNeedsResume
)
1157 _mesa_ResumeTransformFeedback();
1159 ctx
->Meta
->SaveStackDepth
--;
1164 * Determine whether Mesa is currently in a meta state.
1167 _mesa_meta_in_progress(struct gl_context
*ctx
)
1169 return ctx
->Meta
->SaveStackDepth
!= 0;
1174 * Convert Z from a normalized value in the range [0, 1] to an object-space
1175 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1176 * default/identity ortho projection results in the original Z value.
1177 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1178 * value comes from the clear value or raster position.
1180 static INLINE GLfloat
1181 invert_z(GLfloat normZ
)
1183 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1189 * One-time init for a temp_texture object.
1190 * Choose tex target, compute max tex size, etc.
1193 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1195 /* prefer texture rectangle */
1196 if (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
) {
1197 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1198 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1199 tex
->NPOT
= GL_TRUE
;
1202 /* use 2D texture, NPOT if possible */
1203 tex
->Target
= GL_TEXTURE_2D
;
1204 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1205 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1207 tex
->MinSize
= 16; /* 16 x 16 at least */
1208 assert(tex
->MaxSize
> 0);
1210 _mesa_GenTextures(1, &tex
->TexObj
);
1214 cleanup_temp_texture(struct temp_texture
*tex
)
1218 _mesa_DeleteTextures(1, &tex
->TexObj
);
1224 * Return pointer to temp_texture info for non-bitmap ops.
1225 * This does some one-time init if needed.
1227 static struct temp_texture
*
1228 get_temp_texture(struct gl_context
*ctx
)
1230 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1233 init_temp_texture(ctx
, tex
);
1241 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1242 * We use a separate texture for bitmaps to reduce texture
1243 * allocation/deallocation.
1245 static struct temp_texture
*
1246 get_bitmap_temp_texture(struct gl_context
*ctx
)
1248 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1251 init_temp_texture(ctx
, tex
);
1258 * Return pointer to depth temp_texture.
1259 * This does some one-time init if needed.
1261 static struct temp_texture
*
1262 get_temp_depth_texture(struct gl_context
*ctx
)
1264 struct temp_texture
*tex
= &ctx
->Meta
->Blit
.depthTex
;
1267 init_temp_texture(ctx
, tex
);
1274 * Compute the width/height of texture needed to draw an image of the
1275 * given size. Return a flag indicating whether the current texture
1276 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1277 * allocated (glTexImage2D).
1278 * Also, compute s/t texcoords for drawing.
1280 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1283 alloc_texture(struct temp_texture
*tex
,
1284 GLsizei width
, GLsizei height
, GLenum intFormat
)
1286 GLboolean newTex
= GL_FALSE
;
1288 ASSERT(width
<= tex
->MaxSize
);
1289 ASSERT(height
<= tex
->MaxSize
);
1291 if (width
> tex
->Width
||
1292 height
> tex
->Height
||
1293 intFormat
!= tex
->IntFormat
) {
1294 /* alloc new texture (larger or different format) */
1297 /* use non-power of two size */
1298 tex
->Width
= MAX2(tex
->MinSize
, width
);
1299 tex
->Height
= MAX2(tex
->MinSize
, height
);
1302 /* find power of two size */
1304 w
= h
= tex
->MinSize
;
1313 tex
->IntFormat
= intFormat
;
1318 /* compute texcoords */
1319 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1320 tex
->Sright
= (GLfloat
) width
;
1321 tex
->Ttop
= (GLfloat
) height
;
1324 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1325 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1333 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1336 setup_copypix_texture(struct gl_context
*ctx
,
1337 struct temp_texture
*tex
,
1339 GLint srcX
, GLint srcY
,
1340 GLsizei width
, GLsizei height
, GLenum intFormat
,
1343 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1344 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1345 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1346 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1347 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1349 /* copy framebuffer image to texture */
1351 /* create new tex image */
1352 if (tex
->Width
== width
&& tex
->Height
== height
) {
1353 /* create new tex with framebuffer data */
1354 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1355 srcX
, srcY
, width
, height
, 0);
1358 /* create empty texture */
1359 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1360 tex
->Width
, tex
->Height
, 0,
1361 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1363 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1364 0, 0, srcX
, srcY
, width
, height
);
1368 /* replace existing tex image */
1369 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1370 0, 0, srcX
, srcY
, width
, height
);
1376 * Setup/load texture for glDrawPixels.
1379 setup_drawpix_texture(struct gl_context
*ctx
,
1380 struct temp_texture
*tex
,
1382 GLenum texIntFormat
,
1383 GLsizei width
, GLsizei height
,
1384 GLenum format
, GLenum type
,
1385 const GLvoid
*pixels
)
1387 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1388 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1389 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1390 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
1391 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1393 /* copy pixel data to texture */
1395 /* create new tex image */
1396 if (tex
->Width
== width
&& tex
->Height
== height
) {
1397 /* create new tex and load image data */
1398 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1399 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1402 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1404 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1405 ctx
->Unpack
.BufferObj
);
1406 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1407 /* create empty texture */
1408 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1409 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1410 if (save_unpack_obj
!= NULL
)
1411 _mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB
,
1412 save_unpack_obj
->Name
);
1414 _mesa_TexSubImage2D(tex
->Target
, 0,
1415 0, 0, width
, height
, format
, type
, pixels
);
1419 /* replace existing tex image */
1420 _mesa_TexSubImage2D(tex
->Target
, 0,
1421 0, 0, width
, height
, format
, type
, pixels
);
1428 * One-time init for drawing depth pixels.
1431 init_blit_depth_pixels(struct gl_context
*ctx
)
1433 static const char *program
=
1435 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1438 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1439 struct temp_texture
*tex
= get_temp_texture(ctx
);
1440 const char *texTarget
;
1442 assert(blit
->DepthFP
== 0);
1444 /* replace %s with "RECT" or "2D" */
1445 assert(strlen(program
) + 4 < sizeof(program2
));
1446 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1450 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1452 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
1453 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1454 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1455 strlen(program2
), (const GLubyte
*) program2
);
1459 setup_ff_blit_framebuffer(struct blit_state
*blit
)
1464 struct vertex verts
[4];
1466 if (blit
->VAO
== 0) {
1467 /* one-time setup */
1469 /* create vertex array object */
1470 _mesa_GenVertexArrays(1, &blit
->VAO
);
1471 _mesa_BindVertexArray(blit
->VAO
);
1473 /* create vertex array buffer */
1474 _mesa_GenBuffers(1, &blit
->VBO
);
1475 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1476 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1477 NULL
, GL_DYNAMIC_DRAW_ARB
);
1479 /* setup vertex arrays */
1480 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1481 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1482 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1483 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1486 /* setup projection matrix */
1487 _mesa_MatrixMode(GL_PROJECTION
);
1488 _mesa_LoadIdentity();
1489 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1494 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
1495 struct blit_state
*blit
,
1501 struct vertex verts
[4];
1502 const char *vs_source
;
1507 GLboolean texture_2d
= (target
== GL_TEXTURE_2D
);
1509 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
1510 assert(_mesa_is_desktop_gl(ctx
) || texture_2d
);
1512 /* Check if already initialized */
1513 if (blit
->VAO
== 0) {
1515 /* create vertex array object */
1516 _mesa_GenVertexArrays(1, &blit
->VAO
);
1517 _mesa_BindVertexArray(blit
->VAO
);
1519 /* create vertex array buffer */
1520 _mesa_GenBuffers(1, &blit
->VBO
);
1521 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1522 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1523 NULL
, GL_DYNAMIC_DRAW_ARB
);
1525 /* setup vertex arrays */
1526 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
1527 sizeof(struct vertex
), OFFSET(x
));
1528 _mesa_VertexAttribPointer(1, 2, GL_FLOAT
, GL_FALSE
,
1529 sizeof(struct vertex
), OFFSET(s
));
1531 _mesa_EnableVertexAttribArray(0);
1532 _mesa_EnableVertexAttribArray(1);
1535 /* Generate a relevant fragment shader program for the texture target */
1536 if ((target
== GL_TEXTURE_2D
&& blit
->ShaderProg
!= 0) ||
1537 (target
== GL_TEXTURE_RECTANGLE
&& blit
->RectShaderProg
!= 0)) {
1541 mem_ctx
= ralloc_context(NULL
);
1543 if (ctx
->Const
.GLSLVersion
< 130) {
1545 "attribute vec2 position;\n"
1546 "attribute vec2 textureCoords;\n"
1547 "varying vec2 texCoords;\n"
1550 " texCoords = textureCoords;\n"
1551 " gl_Position = vec4(position, 0.0, 1.0);\n"
1554 fs_source
= ralloc_asprintf(mem_ctx
,
1556 "precision highp float;\n"
1558 "uniform %s texSampler;\n"
1559 "varying vec2 texCoords;\n"
1562 " gl_FragColor = %s(texSampler, texCoords);\n"
1563 " gl_FragDepth = gl_FragColor.r;\n"
1565 texture_2d
? "sampler2D" : "sampler2DRect",
1566 texture_2d
? "texture2D" : "texture2DRect");
1569 vs_source
= ralloc_asprintf(mem_ctx
,
1571 "in vec2 position;\n"
1572 "in vec2 textureCoords;\n"
1573 "out vec2 texCoords;\n"
1576 " texCoords = textureCoords;\n"
1577 " gl_Position = vec4(position, 0.0, 1.0);\n"
1579 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
1580 fs_source
= ralloc_asprintf(mem_ctx
,
1583 "precision highp float;\n"
1585 "uniform %s texSampler;\n"
1586 "in vec2 texCoords;\n"
1587 "out vec4 out_color;\n"
1591 " out_color = %s(texSampler, texCoords);\n"
1592 " gl_FragDepth = out_color.r;\n"
1594 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
1595 texture_2d
? "sampler2D" : "sampler2DRect",
1596 texture_2d
? "texture" : "texture2DRect");
1599 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
1600 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
1602 ShaderProg
= _mesa_CreateProgramObjectARB();
1603 _mesa_AttachShader(ShaderProg
, fs
);
1604 _mesa_DeleteObjectARB(fs
);
1605 _mesa_AttachShader(ShaderProg
, vs
);
1606 _mesa_DeleteObjectARB(vs
);
1607 _mesa_BindAttribLocation(ShaderProg
, 0, "position");
1608 _mesa_BindAttribLocation(ShaderProg
, 1, "texcoords");
1609 link_program_with_debug(ctx
, ShaderProg
);
1610 ralloc_free(mem_ctx
);
1612 blit
->ShaderProg
= ShaderProg
;
1614 blit
->RectShaderProg
= ShaderProg
;
1618 * Try to do a glBlitFramebuffer using no-copy texturing.
1619 * We can do this when the src renderbuffer is actually a texture.
1620 * But if the src buffer == dst buffer we cannot do this.
1622 * \return new buffer mask indicating the buffers left to blit using the
1626 blitframebuffer_texture(struct gl_context
*ctx
,
1627 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1628 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1629 GLbitfield mask
, GLenum filter
, GLint flipX
,
1630 GLint flipY
, GLboolean glsl_version
)
1632 if (mask
& GL_COLOR_BUFFER_BIT
) {
1633 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1634 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1635 const struct gl_renderbuffer_attachment
*drawAtt
;
1636 const struct gl_renderbuffer_attachment
*readAtt
=
1637 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1639 if (readAtt
&& readAtt
->Texture
) {
1640 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1641 const GLint dstX
= MIN2(dstX0
, dstX1
);
1642 const GLint dstY
= MIN2(dstY0
, dstY1
);
1643 const GLint dstW
= abs(dstX1
- dstX0
);
1644 const GLint dstH
= abs(dstY1
- dstY0
);
1645 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1646 const GLuint srcLevel
= readAtt
->TextureLevel
;
1647 const GLint baseLevelSave
= texObj
->BaseLevel
;
1648 const GLint maxLevelSave
= texObj
->MaxLevel
;
1649 const GLenum target
= texObj
->Target
;
1650 GLuint sampler
, samplerSave
=
1651 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
1652 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
1655 /* Iterate through all draw buffers */
1656 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
1657 int idx
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
1660 drawAtt
= &drawFb
->Attachment
[idx
];
1662 if (drawAtt
->Texture
== readAtt
->Texture
) {
1663 /* Can't use same texture as both the source and dest. We need
1664 * to handle overlapping blits and besides, some hw may not
1671 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1672 /* Can't handle other texture types at this time */
1676 /* Choose between glsl version and fixed function version of
1677 * BlitFramebuffer function.
1680 setup_glsl_blit_framebuffer(ctx
, blit
, target
);
1681 if (target
== GL_TEXTURE_2D
)
1682 _mesa_UseProgram(blit
->ShaderProg
);
1684 _mesa_UseProgram(blit
->RectShaderProg
);
1687 setup_ff_blit_framebuffer(&ctx
->Meta
->Blit
);
1690 _mesa_BindVertexArray(blit
->VAO
);
1691 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1693 _mesa_GenSamplers(1, &sampler
);
1694 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
1697 printf("Blit from texture!\n");
1698 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1699 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1702 /* Prepare src texture state */
1703 _mesa_BindTexture(target
, texObj
->Name
);
1704 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
1705 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
1706 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1707 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1708 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1710 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1711 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1713 /* Always do our blits with no sRGB decode or encode. Note that
1714 * GL_FRAMEBUFFER_SRGB has already been disabled by
1715 * _mesa_meta_begin().
1717 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1718 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
1719 GL_SKIP_DECODE_EXT
);
1722 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
) {
1723 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1724 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1727 /* Prepare vertex data (the VBO was previously created and bound) */
1732 struct vertex verts
[4];
1733 GLfloat s0
, t0
, s1
, t1
;
1735 if (target
== GL_TEXTURE_2D
) {
1736 const struct gl_texture_image
*texImage
1737 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1738 s0
= srcX0
/ (float) texImage
->Width
;
1739 s1
= srcX1
/ (float) texImage
->Width
;
1740 t0
= srcY0
/ (float) texImage
->Height
;
1741 t1
= srcY1
/ (float) texImage
->Height
;
1744 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1751 /* setup vertex positions */
1752 verts
[0].x
= -1.0F
* flipX
;
1753 verts
[0].y
= -1.0F
* flipY
;
1754 verts
[1].x
= 1.0F
* flipX
;
1755 verts
[1].y
= -1.0F
* flipY
;
1756 verts
[2].x
= 1.0F
* flipX
;
1757 verts
[2].y
= 1.0F
* flipY
;
1758 verts
[3].x
= -1.0F
* flipX
;
1759 verts
[3].y
= 1.0F
* flipY
;
1770 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1773 /* setup viewport */
1774 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1775 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1776 _mesa_DepthMask(GL_FALSE
);
1777 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1779 /* Restore texture object state, the texture binding will
1780 * be restored by _mesa_meta_end().
1782 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1783 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1784 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1787 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
1788 _mesa_DeleteSamplers(1, &sampler
);
1790 /* Done with color buffer */
1791 mask
&= ~GL_COLOR_BUFFER_BIT
;
1800 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1801 * of texture mapping and polygon rendering.
1804 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1805 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1806 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1807 GLbitfield mask
, GLenum filter
)
1809 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1810 struct temp_texture
*tex
= get_temp_texture(ctx
);
1811 struct temp_texture
*depthTex
= get_temp_depth_texture(ctx
);
1812 const GLsizei maxTexSize
= tex
->MaxSize
;
1813 const GLint srcX
= MIN2(srcX0
, srcX1
);
1814 const GLint srcY
= MIN2(srcY0
, srcY1
);
1815 const GLint srcW
= abs(srcX1
- srcX0
);
1816 const GLint srcH
= abs(srcY1
- srcY0
);
1817 const GLint dstX
= MIN2(dstX0
, dstX1
);
1818 const GLint dstY
= MIN2(dstY0
, dstY1
);
1819 const GLint dstW
= abs(dstX1
- dstX0
);
1820 const GLint dstH
= abs(dstY1
- dstY0
);
1821 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
1822 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
1823 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
1824 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
1825 const GLint flipX
= srcFlipX
* dstFlipX
;
1826 const GLint flipY
= srcFlipY
* dstFlipY
;
1831 struct vertex verts
[4];
1833 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
1834 ctx
->Extensions
.ARB_fragment_shader
&&
1835 (ctx
->API
!= API_OPENGLES
);
1837 /* In addition to falling back if the blit size is larger than the maximum
1838 * texture size, fallback if the source is multisampled. This fallback can
1839 * be removed once Mesa gets support ARB_texture_multisample.
1841 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1842 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1843 /* XXX avoid this fallback */
1844 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1845 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1849 /* only scissor effects blit so save/clear all other relevant state */
1850 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1852 /* Try faster, direct texture approach first */
1853 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1854 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
,
1855 dstFlipX
, dstFlipY
, use_glsl_version
);
1857 _mesa_meta_end(ctx
);
1861 /* Choose between glsl version and fixed function version of
1862 * BlitFramebuffer function.
1864 if (use_glsl_version
) {
1865 setup_glsl_blit_framebuffer(ctx
, blit
, tex
->Target
);
1866 if (tex
->Target
== GL_TEXTURE_2D
)
1867 _mesa_UseProgram(blit
->ShaderProg
);
1869 _mesa_UseProgram(blit
->RectShaderProg
);
1872 setup_ff_blit_framebuffer(blit
);
1875 _mesa_BindVertexArray(blit
->VAO
);
1876 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1878 /* Continue with "normal" approach which involves copying the src rect
1879 * into a temporary texture and is "blitted" by drawing a textured quad.
1882 /* setup vertex positions */
1883 verts
[0].x
= -1.0F
* flipX
;
1884 verts
[0].y
= -1.0F
* flipY
;
1885 verts
[1].x
= 1.0F
* flipX
;
1886 verts
[1].y
= -1.0F
* flipY
;
1887 verts
[2].x
= 1.0F
* flipX
;
1888 verts
[2].y
= 1.0F
* flipY
;
1889 verts
[3].x
= -1.0F
* flipX
;
1890 verts
[3].y
= 1.0F
* flipY
;
1894 /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
1897 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1898 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1900 if (mask
& GL_COLOR_BUFFER_BIT
) {
1901 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1902 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
1903 const GLenum rb_base_format
=
1904 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
1906 /* Using the exact source rectangle to create the texture does incorrect
1907 * linear filtering along the edges. So, allocate the texture extended along
1908 * edges by one pixel in x, y directions.
1910 newTex
= alloc_texture(tex
, srcW
+ 2, srcH
+ 2, rb_base_format
);
1911 setup_copypix_texture(ctx
, tex
, newTex
,
1912 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
1913 rb_base_format
, filter
);
1914 /* texcoords (after texture allocation!) */
1918 verts
[1].s
= tex
->Sright
- 1.0F
;
1920 verts
[2].s
= tex
->Sright
- 1.0F
;
1921 verts
[2].t
= tex
->Ttop
- 1.0F
;
1923 verts
[3].t
= tex
->Ttop
- 1.0F
;
1925 /* upload new vertex data */
1926 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1929 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1930 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1931 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1932 _mesa_DepthMask(GL_FALSE
);
1933 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1934 mask
&= ~GL_COLOR_BUFFER_BIT
;
1937 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
1938 _mesa_is_desktop_gl(ctx
) &&
1939 ctx
->Extensions
.ARB_depth_texture
&&
1940 ctx
->Extensions
.ARB_fragment_program
) {
1942 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
1946 newTex
= alloc_texture(depthTex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1947 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
1948 GL_UNSIGNED_INT
, tmp
);
1949 setup_drawpix_texture(ctx
, depthTex
, newTex
, GL_DEPTH_COMPONENT
,
1950 srcW
, srcH
, GL_DEPTH_COMPONENT
,
1951 GL_UNSIGNED_INT
, tmp
);
1953 /* texcoords (after texture allocation!) */
1957 verts
[1].s
= depthTex
->Sright
;
1959 verts
[2].s
= depthTex
->Sright
;
1960 verts
[2].t
= depthTex
->Ttop
;
1962 verts
[3].t
= depthTex
->Ttop
;
1964 /* upload new vertex data */
1965 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1969 init_blit_depth_pixels(ctx
);
1971 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1972 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1973 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1974 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1975 _mesa_DepthFunc(GL_ALWAYS
);
1976 _mesa_DepthMask(GL_TRUE
);
1978 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
1979 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1980 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1981 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1987 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1988 /* XXX can't easily do stencil */
1991 if (_mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES
)
1992 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1994 _mesa_meta_end(ctx
);
1997 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1998 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
2003 meta_glsl_blit_cleanup(struct blit_state
*blit
)
2006 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
2008 _mesa_DeleteBuffers(1, &blit
->VBO
);
2011 if (blit
->DepthFP
) {
2012 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
2016 _mesa_DeleteObjectARB(blit
->ShaderProg
);
2017 blit
->ShaderProg
= 0;
2018 _mesa_DeleteObjectARB(blit
->RectShaderProg
);
2019 blit
->RectShaderProg
= 0;
2021 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
2022 blit
->depthTex
.TexObj
= 0;
2027 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2030 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2032 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2034 GLfloat x
, y
, z
, r
, g
, b
, a
;
2036 struct vertex verts
[4];
2037 /* save all state but scissor, pixel pack/unpack */
2038 GLbitfield metaSave
= (MESA_META_ALL
-
2040 MESA_META_PIXEL_STORE
-
2041 MESA_META_CONDITIONAL_RENDER
-
2042 MESA_META_FRAMEBUFFER_SRGB
);
2043 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2045 if (buffers
& BUFFER_BITS_COLOR
) {
2046 /* if clearing color buffers, don't save/restore colormask */
2047 metaSave
-= MESA_META_COLOR_MASK
;
2050 _mesa_meta_begin(ctx
, metaSave
);
2052 if (clear
->VAO
== 0) {
2053 /* one-time setup */
2055 /* create vertex array object */
2056 _mesa_GenVertexArrays(1, &clear
->VAO
);
2057 _mesa_BindVertexArray(clear
->VAO
);
2059 /* create vertex array buffer */
2060 _mesa_GenBuffers(1, &clear
->VBO
);
2061 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2063 /* setup vertex arrays */
2064 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2065 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2066 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2067 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2070 _mesa_BindVertexArray(clear
->VAO
);
2071 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2074 /* GL_COLOR_BUFFER_BIT */
2075 if (buffers
& BUFFER_BITS_COLOR
) {
2076 /* leave colormask, glDrawBuffer state as-is */
2078 /* Clears never have the color clamped. */
2079 if (ctx
->Extensions
.ARB_color_buffer_float
)
2080 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2083 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2084 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2087 /* GL_DEPTH_BUFFER_BIT */
2088 if (buffers
& BUFFER_BIT_DEPTH
) {
2089 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2090 _mesa_DepthFunc(GL_ALWAYS
);
2091 _mesa_DepthMask(GL_TRUE
);
2094 assert(!ctx
->Depth
.Test
);
2097 /* GL_STENCIL_BUFFER_BIT */
2098 if (buffers
& BUFFER_BIT_STENCIL
) {
2099 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2100 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2101 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2102 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2103 ctx
->Stencil
.Clear
& stencilMax
,
2104 ctx
->Stencil
.WriteMask
[0]);
2107 assert(!ctx
->Stencil
.Enabled
);
2110 /* vertex positions/colors */
2112 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
2113 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
2114 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
2115 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
2116 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
2133 for (i
= 0; i
< 4; i
++) {
2134 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
2135 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
2136 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
2137 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
2140 /* upload new vertex data */
2141 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2142 GL_DYNAMIC_DRAW_ARB
);
2146 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2148 _mesa_meta_end(ctx
);
2152 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
2154 const char *vs_source
=
2155 "attribute vec4 position;\n"
2158 " gl_Position = position;\n"
2160 const char *gs_source
=
2162 "layout(triangles) in;\n"
2163 "layout(triangle_strip, max_vertices = 4) out;\n"
2164 "uniform int layer;\n"
2167 " for (int i = 0; i < 3; i++) {\n"
2168 " gl_Layer = layer;\n"
2169 " gl_Position = gl_in[i].gl_Position;\n"
2173 const char *fs_source
=
2175 "precision highp float;\n"
2177 "uniform vec4 color;\n"
2180 " gl_FragColor = color;\n"
2182 GLuint vs
, gs
= 0, fs
;
2183 bool has_integer_textures
;
2185 if (clear
->VAO
!= 0)
2188 /* create vertex array object */
2189 _mesa_GenVertexArrays(1, &clear
->VAO
);
2190 _mesa_BindVertexArray(clear
->VAO
);
2192 /* create vertex array buffer */
2193 _mesa_GenBuffers(1, &clear
->VBO
);
2194 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2196 /* setup vertex arrays */
2197 _mesa_VertexAttribPointer(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
2198 _mesa_EnableVertexAttribArray(0);
2200 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
2201 _mesa_ShaderSource(vs
, 1, &vs_source
, NULL
);
2202 _mesa_CompileShader(vs
);
2204 if (_mesa_has_geometry_shaders(ctx
)) {
2205 gs
= _mesa_CreateShaderObjectARB(GL_GEOMETRY_SHADER
);
2206 _mesa_ShaderSource(gs
, 1, &gs_source
, NULL
);
2207 _mesa_CompileShader(gs
);
2210 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
2211 _mesa_ShaderSource(fs
, 1, &fs_source
, NULL
);
2212 _mesa_CompileShader(fs
);
2214 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
2215 _mesa_AttachShader(clear
->ShaderProg
, fs
);
2216 _mesa_DeleteObjectARB(fs
);
2218 _mesa_AttachShader(clear
->ShaderProg
, gs
);
2219 _mesa_AttachShader(clear
->ShaderProg
, vs
);
2220 _mesa_DeleteObjectARB(vs
);
2221 _mesa_BindAttribLocation(clear
->ShaderProg
, 0, "position");
2222 _mesa_LinkProgram(clear
->ShaderProg
);
2224 clear
->ColorLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2227 clear
->LayerLocation
= _mesa_GetUniformLocation(clear
->ShaderProg
,
2231 has_integer_textures
= _mesa_is_gles3(ctx
) ||
2232 (_mesa_is_desktop_gl(ctx
) && ctx
->Const
.GLSLVersion
>= 130);
2234 if (has_integer_textures
) {
2235 void *shader_source_mem_ctx
= ralloc_context(NULL
);
2236 const char *vs_int_source
=
2237 ralloc_asprintf(shader_source_mem_ctx
,
2239 "in vec4 position;\n"
2242 " gl_Position = position;\n"
2244 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2245 const char *fs_int_source
=
2246 ralloc_asprintf(shader_source_mem_ctx
,
2249 "precision highp float;\n"
2251 "uniform ivec4 color;\n"
2252 "out ivec4 out_color;\n"
2256 " out_color = color;\n"
2258 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
2260 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
2261 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
2262 ralloc_free(shader_source_mem_ctx
);
2264 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
2265 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
2266 _mesa_DeleteObjectARB(fs
);
2268 _mesa_AttachShader(clear
->IntegerShaderProg
, gs
);
2269 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
2270 _mesa_DeleteObjectARB(vs
);
2271 _mesa_BindAttribLocation(clear
->IntegerShaderProg
, 0, "position");
2273 /* Note that user-defined out attributes get automatically assigned
2274 * locations starting from 0, so we don't need to explicitly
2275 * BindFragDataLocation to 0.
2278 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
2280 clear
->IntegerColorLocation
=
2281 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "color");
2283 clear
->IntegerLayerLocation
=
2284 _mesa_GetUniformLocation(clear
->IntegerShaderProg
, "layer");
2288 _mesa_DeleteObjectARB(gs
);
2292 meta_glsl_clear_cleanup(struct clear_state
*clear
)
2294 if (clear
->VAO
== 0)
2296 _mesa_DeleteVertexArrays(1, &clear
->VAO
);
2298 _mesa_DeleteBuffers(1, &clear
->VBO
);
2300 _mesa_DeleteObjectARB(clear
->ShaderProg
);
2301 clear
->ShaderProg
= 0;
2303 if (clear
->IntegerShaderProg
) {
2304 _mesa_DeleteObjectARB(clear
->IntegerShaderProg
);
2305 clear
->IntegerShaderProg
= 0;
2310 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
2313 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
2315 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
2316 GLbitfield metaSave
;
2317 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
2318 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
2319 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
2320 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
2321 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
2322 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
2323 const float z
= -invert_z(ctx
->Depth
.Clear
);
2328 metaSave
= (MESA_META_ALPHA_TEST
|
2330 MESA_META_DEPTH_TEST
|
2331 MESA_META_RASTERIZATION
|
2333 MESA_META_STENCIL_TEST
|
2335 MESA_META_VIEWPORT
|
2337 MESA_META_CLAMP_FRAGMENT_COLOR
|
2338 MESA_META_MULTISAMPLE
|
2339 MESA_META_OCCLUSION_QUERY
);
2341 if (!(buffers
& BUFFER_BITS_COLOR
)) {
2342 /* We'll use colormask to disable color writes. Otherwise,
2343 * respect color mask
2345 metaSave
|= MESA_META_COLOR_MASK
;
2348 _mesa_meta_begin(ctx
, metaSave
);
2350 meta_glsl_clear_init(ctx
, clear
);
2352 if (fb
->_IntegerColor
) {
2353 _mesa_UseProgram(clear
->IntegerShaderProg
);
2354 _mesa_Uniform4iv(clear
->IntegerColorLocation
, 1,
2355 ctx
->Color
.ClearColor
.i
);
2357 _mesa_UseProgram(clear
->ShaderProg
);
2358 _mesa_Uniform4fv(clear
->ColorLocation
, 1,
2359 ctx
->Color
.ClearColor
.f
);
2362 _mesa_BindVertexArray(clear
->VAO
);
2363 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
2365 /* GL_COLOR_BUFFER_BIT */
2366 if (buffers
& BUFFER_BITS_COLOR
) {
2367 /* leave colormask, glDrawBuffer state as-is */
2369 /* Clears never have the color clamped. */
2370 if (ctx
->Extensions
.ARB_color_buffer_float
)
2371 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
2374 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
2375 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2378 /* GL_DEPTH_BUFFER_BIT */
2379 if (buffers
& BUFFER_BIT_DEPTH
) {
2380 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
2381 _mesa_DepthFunc(GL_ALWAYS
);
2382 _mesa_DepthMask(GL_TRUE
);
2385 assert(!ctx
->Depth
.Test
);
2388 /* GL_STENCIL_BUFFER_BIT */
2389 if (buffers
& BUFFER_BIT_STENCIL
) {
2390 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2391 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
2392 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2393 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
2394 ctx
->Stencil
.Clear
& stencilMax
,
2395 ctx
->Stencil
.WriteMask
[0]);
2398 assert(!ctx
->Stencil
.Enabled
);
2401 /* vertex positions */
2415 /* upload new vertex data */
2416 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
2417 GL_DYNAMIC_DRAW_ARB
);
2420 if (fb
->MaxNumLayers
> 0) {
2422 for (layer
= 0; layer
< fb
->MaxNumLayers
; layer
++) {
2423 if (fb
->_IntegerColor
)
2424 _mesa_Uniform1i(clear
->IntegerLayerLocation
, layer
);
2426 _mesa_Uniform1i(clear
->LayerLocation
, layer
);
2427 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2430 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2433 _mesa_meta_end(ctx
);
2437 * Meta implementation of ctx->Driver.CopyPixels() in terms
2438 * of texture mapping and polygon rendering and GLSL shaders.
2441 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
2442 GLsizei width
, GLsizei height
,
2443 GLint dstX
, GLint dstY
, GLenum type
)
2445 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
2446 struct temp_texture
*tex
= get_temp_texture(ctx
);
2448 GLfloat x
, y
, z
, s
, t
;
2450 struct vertex verts
[4];
2452 GLenum intFormat
= GL_RGBA
;
2454 if (type
!= GL_COLOR
||
2455 ctx
->_ImageTransferState
||
2457 width
> tex
->MaxSize
||
2458 height
> tex
->MaxSize
) {
2459 /* XXX avoid this fallback */
2460 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
2464 /* Most GL state applies to glCopyPixels, but a there's a few things
2465 * we need to override:
2467 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2470 MESA_META_TRANSFORM
|
2473 MESA_META_VIEWPORT
));
2475 if (copypix
->VAO
== 0) {
2476 /* one-time setup */
2478 /* create vertex array object */
2479 _mesa_GenVertexArrays(1, ©pix
->VAO
);
2480 _mesa_BindVertexArray(copypix
->VAO
);
2482 /* create vertex array buffer */
2483 _mesa_GenBuffers(1, ©pix
->VBO
);
2484 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2485 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2486 NULL
, GL_DYNAMIC_DRAW_ARB
);
2488 /* setup vertex arrays */
2489 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2490 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2491 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2492 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2495 _mesa_BindVertexArray(copypix
->VAO
);
2496 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
2499 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
2501 /* vertex positions, texcoords (after texture allocation!) */
2503 const GLfloat dstX0
= (GLfloat
) dstX
;
2504 const GLfloat dstY0
= (GLfloat
) dstY
;
2505 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2506 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2507 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2517 verts
[1].s
= tex
->Sright
;
2522 verts
[2].s
= tex
->Sright
;
2523 verts
[2].t
= tex
->Ttop
;
2528 verts
[3].t
= tex
->Ttop
;
2530 /* upload new vertex data */
2531 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2534 /* Alloc/setup texture */
2535 setup_copypix_texture(ctx
, tex
, newTex
, srcX
, srcY
, width
, height
,
2536 GL_RGBA
, GL_NEAREST
);
2538 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2540 /* draw textured quad */
2541 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2543 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2545 _mesa_meta_end(ctx
);
2549 meta_drawpix_cleanup(struct drawpix_state
*drawpix
)
2551 if (drawpix
->VAO
!= 0) {
2552 _mesa_DeleteVertexArrays(1, &drawpix
->VAO
);
2556 if (drawpix
->StencilFP
!= 0) {
2557 _mesa_DeleteProgramsARB(1, &drawpix
->StencilFP
);
2558 drawpix
->StencilFP
= 0;
2561 if (drawpix
->DepthFP
!= 0) {
2562 _mesa_DeleteProgramsARB(1, &drawpix
->DepthFP
);
2563 drawpix
->DepthFP
= 0;
2568 * When the glDrawPixels() image size is greater than the max rectangle
2569 * texture size we use this function to break the glDrawPixels() image
2570 * into tiles which fit into the max texture size.
2573 tiled_draw_pixels(struct gl_context
*ctx
,
2575 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2576 GLenum format
, GLenum type
,
2577 const struct gl_pixelstore_attrib
*unpack
,
2578 const GLvoid
*pixels
)
2580 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2583 if (tileUnpack
.RowLength
== 0)
2584 tileUnpack
.RowLength
= width
;
2586 for (i
= 0; i
< width
; i
+= tileSize
) {
2587 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2588 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2590 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2592 for (j
= 0; j
< height
; j
+= tileSize
) {
2593 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2594 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2596 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2598 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2599 format
, type
, &tileUnpack
, pixels
);
2606 * One-time init for drawing stencil pixels.
2609 init_draw_stencil_pixels(struct gl_context
*ctx
)
2611 /* This program is run eight times, once for each stencil bit.
2612 * The stencil values to draw are found in an 8-bit alpha texture.
2613 * We read the texture/stencil value and test if bit 'b' is set.
2614 * If the bit is not set, use KIL to kill the fragment.
2615 * Finally, we use the stencil test to update the stencil buffer.
2617 * The basic algorithm for checking if a bit is set is:
2618 * if (is_odd(value / (1 << bit)))
2619 * result is one (or non-zero).
2622 * The program parameter contains three values:
2623 * parm.x = 255 / (1 << bit)
2627 static const char *program
=
2629 "PARAM parm = program.local[0]; \n"
2631 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2632 "# t = t * 255 / bit \n"
2633 "MUL t.x, t.a, parm.x; \n"
2636 "SUB t.x, t.x, t.y; \n"
2638 "MUL t.x, t.x, parm.y; \n"
2639 "# t = fract(t.x) \n"
2640 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2641 "# t.x = (t.x == 0 ? 1 : 0) \n"
2642 "SGE t.x, -t.x, parm.z; \n"
2644 "# for debug only \n"
2645 "#MOV result.color, t.x; \n"
2647 char program2
[1000];
2648 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2649 struct temp_texture
*tex
= get_temp_texture(ctx
);
2650 const char *texTarget
;
2652 assert(drawpix
->StencilFP
== 0);
2654 /* replace %s with "RECT" or "2D" */
2655 assert(strlen(program
) + 4 < sizeof(program2
));
2656 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2660 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2662 _mesa_GenProgramsARB(1, &drawpix
->StencilFP
);
2663 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2664 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2665 strlen(program2
), (const GLubyte
*) program2
);
2670 * One-time init for drawing depth pixels.
2673 init_draw_depth_pixels(struct gl_context
*ctx
)
2675 static const char *program
=
2677 "PARAM color = program.local[0]; \n"
2678 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2679 "MOV result.color, color; \n"
2682 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2683 struct temp_texture
*tex
= get_temp_texture(ctx
);
2684 const char *texTarget
;
2686 assert(drawpix
->DepthFP
== 0);
2688 /* replace %s with "RECT" or "2D" */
2689 assert(strlen(program
) + 4 < sizeof(program2
));
2690 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2694 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2696 _mesa_GenProgramsARB(1, &drawpix
->DepthFP
);
2697 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2698 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2699 strlen(program2
), (const GLubyte
*) program2
);
2704 * Meta implementation of ctx->Driver.DrawPixels() in terms
2705 * of texture mapping and polygon rendering.
2708 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2709 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2710 GLenum format
, GLenum type
,
2711 const struct gl_pixelstore_attrib
*unpack
,
2712 const GLvoid
*pixels
)
2714 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2715 struct temp_texture
*tex
= get_temp_texture(ctx
);
2716 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2717 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2719 GLfloat x
, y
, z
, s
, t
;
2721 struct vertex verts
[4];
2722 GLenum texIntFormat
;
2723 GLboolean fallback
, newTex
;
2724 GLbitfield metaExtraSave
= 0x0;
2728 * Determine if we can do the glDrawPixels with texture mapping.
2730 fallback
= GL_FALSE
;
2731 if (ctx
->Fog
.Enabled
) {
2735 if (_mesa_is_color_format(format
)) {
2736 /* use more compact format when possible */
2737 /* XXX disable special case for GL_LUMINANCE for now to work around
2738 * apparent i965 driver bug (see bug #23670).
2740 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2741 texIntFormat
= format
;
2743 texIntFormat
= GL_RGBA
;
2745 /* If we're not supposed to clamp the resulting color, then just
2746 * promote our texture to fully float. We could do better by
2747 * just going for the matching set of channels, in floating
2750 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2751 ctx
->Extensions
.ARB_texture_float
)
2752 texIntFormat
= GL_RGBA32F
;
2754 else if (_mesa_is_stencil_format(format
)) {
2755 if (ctx
->Extensions
.ARB_fragment_program
&&
2756 ctx
->Pixel
.IndexShift
== 0 &&
2757 ctx
->Pixel
.IndexOffset
== 0 &&
2758 type
== GL_UNSIGNED_BYTE
) {
2759 /* We'll store stencil as alpha. This only works for GLubyte
2760 * image data because of how incoming values are mapped to alpha
2763 texIntFormat
= GL_ALPHA
;
2764 metaExtraSave
= (MESA_META_COLOR_MASK
|
2765 MESA_META_DEPTH_TEST
|
2766 MESA_META_PIXEL_TRANSFER
|
2768 MESA_META_STENCIL_TEST
);
2774 else if (_mesa_is_depth_format(format
)) {
2775 if (ctx
->Extensions
.ARB_depth_texture
&&
2776 ctx
->Extensions
.ARB_fragment_program
) {
2777 texIntFormat
= GL_DEPTH_COMPONENT
;
2778 metaExtraSave
= (MESA_META_SHADER
);
2789 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2790 format
, type
, unpack
, pixels
);
2795 * Check image size against max texture size, draw as tiles if needed.
2797 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2798 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2799 format
, type
, unpack
, pixels
);
2803 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2804 * but a there's a few things we need to override:
2806 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2809 MESA_META_TRANSFORM
|
2812 MESA_META_VIEWPORT
|
2815 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2817 /* vertex positions, texcoords (after texture allocation!) */
2819 const GLfloat x0
= (GLfloat
) x
;
2820 const GLfloat y0
= (GLfloat
) y
;
2821 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2822 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2823 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2833 verts
[1].s
= tex
->Sright
;
2838 verts
[2].s
= tex
->Sright
;
2839 verts
[2].t
= tex
->Ttop
;
2844 verts
[3].t
= tex
->Ttop
;
2847 if (drawpix
->VAO
== 0) {
2848 /* one-time setup: create vertex array object */
2849 _mesa_GenVertexArrays(1, &drawpix
->VAO
);
2851 _mesa_BindVertexArray(drawpix
->VAO
);
2853 /* create vertex array buffer */
2854 _mesa_GenBuffers(1, &vbo
);
2855 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, vbo
);
2856 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2857 verts
, GL_DYNAMIC_DRAW_ARB
);
2859 /* setup vertex arrays */
2860 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2861 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2862 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2863 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2865 /* set given unpack params */
2866 ctx
->Unpack
= *unpack
;
2868 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2870 if (_mesa_is_stencil_format(format
)) {
2871 /* Drawing stencil */
2874 if (!drawpix
->StencilFP
)
2875 init_draw_stencil_pixels(ctx
);
2877 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2878 GL_ALPHA
, type
, pixels
);
2880 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2882 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2884 /* set all stencil bits to 0 */
2885 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2886 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2887 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2889 /* set stencil bits to 1 where needed */
2890 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2892 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2893 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2895 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2896 const GLuint mask
= 1 << bit
;
2897 if (mask
& origStencilMask
) {
2898 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2899 _mesa_StencilMask(mask
);
2901 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2902 255.0f
/ mask
, 0.5f
, 0.0f
, 0.0f
);
2904 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2908 else if (_mesa_is_depth_format(format
)) {
2910 if (!drawpix
->DepthFP
)
2911 init_draw_depth_pixels(ctx
);
2913 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2914 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2916 /* polygon color = current raster color */
2917 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2918 ctx
->Current
.RasterColor
);
2920 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2921 format
, type
, pixels
);
2923 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2927 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2928 format
, type
, pixels
);
2929 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2932 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2934 _mesa_DeleteBuffers(1, &vbo
);
2936 /* restore unpack params */
2937 ctx
->Unpack
= unpackSave
;
2939 _mesa_meta_end(ctx
);
2943 alpha_test_raster_color(struct gl_context
*ctx
)
2945 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2946 GLfloat ref
= ctx
->Color
.AlphaRef
;
2948 switch (ctx
->Color
.AlphaFunc
) {
2954 return alpha
== ref
;
2956 return alpha
<= ref
;
2960 return alpha
!= ref
;
2962 return alpha
>= ref
;
2972 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2973 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2974 * tracker would improve performance a lot.
2977 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2978 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2979 const struct gl_pixelstore_attrib
*unpack
,
2980 const GLubyte
*bitmap1
)
2982 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2983 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2984 const GLenum texIntFormat
= GL_ALPHA
;
2985 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2988 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2990 struct vertex verts
[4];
2995 * Check if swrast fallback is needed.
2997 if (ctx
->_ImageTransferState
||
2998 ctx
->FragmentProgram
._Enabled
||
3000 ctx
->Texture
._EnabledUnits
||
3001 width
> tex
->MaxSize
||
3002 height
> tex
->MaxSize
) {
3003 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
3007 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
3010 /* Most GL state applies to glBitmap (like blending, stencil, etc),
3011 * but a there's a few things we need to override:
3013 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
3014 MESA_META_PIXEL_STORE
|
3015 MESA_META_RASTERIZATION
|
3018 MESA_META_TRANSFORM
|
3021 MESA_META_VIEWPORT
));
3023 if (bitmap
->VAO
== 0) {
3024 /* one-time setup */
3026 /* create vertex array object */
3027 _mesa_GenVertexArrays(1, &bitmap
->VAO
);
3028 _mesa_BindVertexArray(bitmap
->VAO
);
3030 /* create vertex array buffer */
3031 _mesa_GenBuffers(1, &bitmap
->VBO
);
3032 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3033 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3034 NULL
, GL_DYNAMIC_DRAW_ARB
);
3036 /* setup vertex arrays */
3037 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3038 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
3039 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
3040 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3041 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3042 _mesa_EnableClientState(GL_COLOR_ARRAY
);
3045 _mesa_BindVertexArray(bitmap
->VAO
);
3046 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
3049 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
3051 /* vertex positions, texcoords, colors (after texture allocation!) */
3053 const GLfloat x0
= (GLfloat
) x
;
3054 const GLfloat y0
= (GLfloat
) y
;
3055 const GLfloat x1
= (GLfloat
) (x
+ width
);
3056 const GLfloat y1
= (GLfloat
) (y
+ height
);
3057 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
3068 verts
[1].s
= tex
->Sright
;
3073 verts
[2].s
= tex
->Sright
;
3074 verts
[2].t
= tex
->Ttop
;
3079 verts
[3].t
= tex
->Ttop
;
3081 for (i
= 0; i
< 4; i
++) {
3082 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
3083 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
3084 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
3085 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
3088 /* upload new vertex data */
3089 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3092 /* choose different foreground/background alpha values */
3093 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
3094 bg
= (fg
> 127 ? 0 : 255);
3096 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
3098 _mesa_meta_end(ctx
);
3102 bitmap8
= malloc(width
* height
);
3104 memset(bitmap8
, bg
, width
* height
);
3105 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
3106 bitmap8
, width
, fg
);
3108 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
3110 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
3111 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
3113 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
3114 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
3116 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3118 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
3123 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
3125 _mesa_meta_end(ctx
);
3130 * Check if the call to _mesa_meta_GenerateMipmap() will require a
3131 * software fallback. The fallback path will require that the texture
3132 * images are mapped.
3133 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
3136 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
3137 struct gl_texture_object
*texObj
)
3139 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3140 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3141 struct gl_texture_image
*baseImage
;
3145 /* check for fallbacks */
3146 if (target
== GL_TEXTURE_3D
||
3147 target
== GL_TEXTURE_1D_ARRAY
||
3148 target
== GL_TEXTURE_2D_ARRAY
) {
3149 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3150 "glGenerateMipmap() to %s target\n",
3151 _mesa_lookup_enum_by_nr(target
));
3155 srcLevel
= texObj
->BaseLevel
;
3156 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
3158 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3159 "glGenerateMipmap() couldn't find base teximage\n");
3163 if (_mesa_is_format_compressed(baseImage
->TexFormat
)) {
3164 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3165 "glGenerateMipmap() with %s format\n",
3166 _mesa_get_format_name(baseImage
->TexFormat
));
3170 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
3171 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3172 /* The texture format is sRGB but we can't turn off sRGB->linear
3173 * texture sample conversion. So we won't be able to generate the
3174 * right colors when rendering. Need to use a fallback.
3176 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3177 "glGenerateMipmap() of sRGB texture without "
3183 * Test that we can actually render in the texture's format.
3186 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3187 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3189 if (target
== GL_TEXTURE_1D
) {
3190 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3191 GL_COLOR_ATTACHMENT0_EXT
,
3192 target
, texObj
->Name
, srcLevel
);
3195 /* other work is needed to enable 3D mipmap generation */
3196 else if (target
== GL_TEXTURE_3D
) {
3198 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3199 GL_COLOR_ATTACHMENT0_EXT
,
3200 target
, texObj
->Name
, srcLevel
, zoffset
);
3205 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3206 GL_COLOR_ATTACHMENT0_EXT
,
3207 target
, texObj
->Name
, srcLevel
);
3210 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3212 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3214 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3215 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
3216 "glGenerateMipmap() got incomplete FBO\n");
3225 * Compute the texture coordinates for the four vertices of a quad for
3226 * drawing a 2D texture image or slice of a cube/3D texture.
3227 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
3228 * \param slice slice of a 1D/2D array texture or 3D texture
3229 * \param width width of the texture image
3230 * \param height height of the texture image
3231 * \param coords0/1/2/3 returns the computed texcoords
3234 setup_texture_coords(GLenum faceTarget
,
3244 static const GLfloat st
[4][2] = {
3245 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
3250 switch (faceTarget
) {
3254 case GL_TEXTURE_2D_ARRAY
:
3255 if (faceTarget
== GL_TEXTURE_3D
) {
3256 assert(slice
< depth
);
3258 r
= (slice
+ 0.5f
) / depth
;
3260 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
3264 coords0
[0] = 0.0F
; /* s */
3265 coords0
[1] = 0.0F
; /* t */
3266 coords0
[2] = r
; /* r */
3277 case GL_TEXTURE_RECTANGLE_ARB
:
3278 coords0
[0] = 0.0F
; /* s */
3279 coords0
[1] = 0.0F
; /* t */
3280 coords0
[2] = 0.0F
; /* r */
3281 coords1
[0] = (float) width
;
3284 coords2
[0] = (float) width
;
3285 coords2
[1] = (float) height
;
3288 coords3
[1] = (float) height
;
3291 case GL_TEXTURE_1D_ARRAY
:
3292 coords0
[0] = 0.0F
; /* s */
3293 coords0
[1] = (float) slice
; /* t */
3294 coords0
[2] = 0.0F
; /* r */
3296 coords1
[1] = (float) slice
;
3299 coords2
[1] = (float) slice
;
3302 coords3
[1] = (float) slice
;
3306 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3307 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3308 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3311 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3312 /* loop over quad verts */
3313 for (i
= 0; i
< 4; i
++) {
3314 /* Compute sc = +/-scale and tc = +/-scale.
3315 * Not +/-1 to avoid cube face selection ambiguity near the edges,
3316 * though that can still sometimes happen with this scale factor...
3318 const GLfloat scale
= 0.9999f
;
3319 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
3320 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
3340 switch (faceTarget
) {
3341 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3346 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3351 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3356 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3361 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3366 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3377 assert(0 && "unexpected target in meta setup_texture_coords()");
3383 setup_ff_generate_mipmap(struct gen_mipmap_state
*mipmap
)
3386 GLfloat x
, y
, tex
[3];
3389 if (mipmap
->VAO
== 0) {
3390 /* one-time setup */
3391 /* create vertex array object */
3392 _mesa_GenVertexArrays(1, &mipmap
->VAO
);
3393 _mesa_BindVertexArray(mipmap
->VAO
);
3395 /* create vertex array buffer */
3396 _mesa_GenBuffers(1, &mipmap
->VBO
);
3397 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3398 /* setup vertex arrays */
3399 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3400 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3401 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3402 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3405 /* setup projection matrix */
3406 _mesa_MatrixMode(GL_PROJECTION
);
3407 _mesa_LoadIdentity();
3408 _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
3412 static struct glsl_sampler
*
3413 setup_texture_sampler(GLenum target
, struct gen_mipmap_state
*mipmap
)
3417 mipmap
->sampler_1d
.type
= "sampler1D";
3418 mipmap
->sampler_1d
.func
= "texture1D";
3419 mipmap
->sampler_1d
.texcoords
= "texCoords.x";
3420 return &mipmap
->sampler_1d
;
3422 mipmap
->sampler_2d
.type
= "sampler2D";
3423 mipmap
->sampler_2d
.func
= "texture2D";
3424 mipmap
->sampler_2d
.texcoords
= "texCoords.xy";
3425 return &mipmap
->sampler_2d
;
3427 /* Code for mipmap generation with 3D textures is not used yet.
3428 * It's a sw fallback.
3430 mipmap
->sampler_3d
.type
= "sampler3D";
3431 mipmap
->sampler_3d
.func
= "texture3D";
3432 mipmap
->sampler_3d
.texcoords
= "texCoords";
3433 return &mipmap
->sampler_3d
;
3434 case GL_TEXTURE_CUBE_MAP
:
3435 mipmap
->sampler_cubemap
.type
= "samplerCube";
3436 mipmap
->sampler_cubemap
.func
= "textureCube";
3437 mipmap
->sampler_cubemap
.texcoords
= "texCoords";
3438 return &mipmap
->sampler_cubemap
;
3439 case GL_TEXTURE_1D_ARRAY
:
3440 mipmap
->sampler_1d_array
.type
= "sampler1DArray";
3441 mipmap
->sampler_1d_array
.func
= "texture1DArray";
3442 mipmap
->sampler_1d_array
.texcoords
= "texCoords.xy";
3443 return &mipmap
->sampler_1d_array
;
3444 case GL_TEXTURE_2D_ARRAY
:
3445 mipmap
->sampler_2d_array
.type
= "sampler2DArray";
3446 mipmap
->sampler_2d_array
.func
= "texture2DArray";
3447 mipmap
->sampler_2d_array
.texcoords
= "texCoords";
3448 return &mipmap
->sampler_2d_array
;
3450 _mesa_problem(NULL
, "Unexpected texture target 0x%x in"
3451 " setup_texture_sampler()\n", target
);
3458 setup_glsl_generate_mipmap(struct gl_context
*ctx
,
3459 struct gen_mipmap_state
*mipmap
,
3463 GLfloat x
, y
, tex
[3];
3465 struct glsl_sampler
*sampler
;
3466 const char *vs_source
;
3471 /* Check if already initialized */
3472 if (mipmap
->VAO
== 0) {
3474 /* create vertex array object */
3475 _mesa_GenVertexArrays(1, &mipmap
->VAO
);
3476 _mesa_BindVertexArray(mipmap
->VAO
);
3478 /* create vertex array buffer */
3479 _mesa_GenBuffers(1, &mipmap
->VBO
);
3480 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3482 /* setup vertex arrays */
3483 _mesa_VertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
,
3484 sizeof(struct vertex
), OFFSET(x
));
3485 _mesa_VertexAttribPointer(1, 3, GL_FLOAT
, GL_FALSE
,
3486 sizeof(struct vertex
), OFFSET(tex
));
3487 _mesa_EnableVertexAttribArray(0);
3488 _mesa_EnableVertexAttribArray(1);
3491 /* Generate a fragment shader program appropriate for the texture target */
3492 sampler
= setup_texture_sampler(target
, mipmap
);
3493 assert(sampler
!= NULL
);
3494 if (sampler
->shader_prog
!= 0) {
3495 mipmap
->ShaderProg
= sampler
->shader_prog
;
3499 mem_ctx
= ralloc_context(NULL
);
3501 if (ctx
->API
== API_OPENGLES2
|| ctx
->Const
.GLSLVersion
< 130) {
3503 "attribute vec2 position;\n"
3504 "attribute vec3 textureCoords;\n"
3505 "varying vec3 texCoords;\n"
3508 " texCoords = textureCoords;\n"
3509 " gl_Position = vec4(position, 0.0, 1.0);\n"
3512 fs_source
= ralloc_asprintf(mem_ctx
,
3513 "#extension GL_EXT_texture_array : enable\n"
3515 "precision highp float;\n"
3517 "uniform %s texSampler;\n"
3518 "varying vec3 texCoords;\n"
3521 " gl_FragColor = %s(texSampler, %s);\n"
3524 sampler
->func
, sampler
->texcoords
);
3527 vs_source
= ralloc_asprintf(mem_ctx
,
3529 "in vec2 position;\n"
3530 "in vec3 textureCoords;\n"
3531 "out vec3 texCoords;\n"
3534 " texCoords = textureCoords;\n"
3535 " gl_Position = vec4(position, 0.0, 1.0);\n"
3537 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es");
3538 fs_source
= ralloc_asprintf(mem_ctx
,
3541 "precision highp float;\n"
3543 "uniform %s texSampler;\n"
3544 "in vec3 texCoords;\n"
3545 "out vec4 out_color;\n"
3549 " out_color = texture(texSampler, %s);\n"
3551 _mesa_is_desktop_gl(ctx
) ? "130" : "300 es",
3553 sampler
->texcoords
);
3556 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
3557 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
3559 mipmap
->ShaderProg
= _mesa_CreateProgramObjectARB();
3560 _mesa_AttachShader(mipmap
->ShaderProg
, fs
);
3561 _mesa_DeleteObjectARB(fs
);
3562 _mesa_AttachShader(mipmap
->ShaderProg
, vs
);
3563 _mesa_DeleteObjectARB(vs
);
3564 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 0, "position");
3565 _mesa_BindAttribLocation(mipmap
->ShaderProg
, 1, "texcoords");
3566 link_program_with_debug(ctx
, mipmap
->ShaderProg
);
3567 sampler
->shader_prog
= mipmap
->ShaderProg
;
3568 ralloc_free(mem_ctx
);
3573 meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state
*mipmap
)
3575 if (mipmap
->VAO
== 0)
3577 _mesa_DeleteVertexArrays(1, &mipmap
->VAO
);
3579 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
3582 _mesa_DeleteObjectARB(mipmap
->sampler_1d
.shader_prog
);
3583 _mesa_DeleteObjectARB(mipmap
->sampler_2d
.shader_prog
);
3584 _mesa_DeleteObjectARB(mipmap
->sampler_3d
.shader_prog
);
3585 _mesa_DeleteObjectARB(mipmap
->sampler_cubemap
.shader_prog
);
3586 _mesa_DeleteObjectARB(mipmap
->sampler_1d_array
.shader_prog
);
3587 _mesa_DeleteObjectARB(mipmap
->sampler_2d_array
.shader_prog
);
3589 mipmap
->sampler_1d
.shader_prog
= 0;
3590 mipmap
->sampler_2d
.shader_prog
= 0;
3591 mipmap
->sampler_3d
.shader_prog
= 0;
3592 mipmap
->sampler_cubemap
.shader_prog
= 0;
3593 mipmap
->sampler_1d_array
.shader_prog
= 0;
3594 mipmap
->sampler_2d_array
.shader_prog
= 0;
3599 * Called via ctx->Driver.GenerateMipmap()
3600 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
3604 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
3605 struct gl_texture_object
*texObj
)
3607 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
3609 GLfloat x
, y
, tex
[3];
3611 struct vertex verts
[4];
3612 const GLuint baseLevel
= texObj
->BaseLevel
;
3613 const GLuint maxLevel
= texObj
->MaxLevel
;
3614 const GLint maxLevelSave
= texObj
->MaxLevel
;
3615 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
3616 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
3617 const GLuint currentTexUnitSave
= ctx
->Texture
.CurrentUnit
;
3618 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
3619 ctx
->Extensions
.ARB_fragment_shader
&&
3620 (ctx
->API
!= API_OPENGLES
);
3623 const GLint slice
= 0;
3626 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
3627 _mesa_generate_mipmap(ctx
, target
, texObj
);
3631 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
3632 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
3633 faceTarget
= target
;
3634 target
= GL_TEXTURE_CUBE_MAP
;
3637 faceTarget
= target
;
3640 _mesa_meta_begin(ctx
, MESA_META_ALL
);
3642 /* Choose between glsl version and fixed function version of
3643 * GenerateMipmap function.
3645 if (use_glsl_version
) {
3646 setup_glsl_generate_mipmap(ctx
, mipmap
, target
);
3647 _mesa_UseProgram(mipmap
->ShaderProg
);
3650 setup_ff_generate_mipmap(mipmap
);
3651 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3654 _mesa_BindVertexArray(mipmap
->VAO
);
3655 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
3657 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
3658 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
3660 if (currentTexUnitSave
!= 0)
3661 _mesa_BindTexture(target
, texObj
->Name
);
3664 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
3667 if (!mipmap
->Sampler
) {
3668 _mesa_GenSamplers(1, &mipmap
->Sampler
);
3669 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3671 _mesa_SamplerParameteri(mipmap
->Sampler
,
3672 GL_TEXTURE_MIN_FILTER
,
3673 GL_LINEAR_MIPMAP_LINEAR
);
3674 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3675 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
3676 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
3677 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
3679 /* We don't want to encode or decode sRGB values; treat them as linear.
3680 * This is not technically correct for GLES3 but we don't get any API
3681 * error at the moment.
3683 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3684 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
3685 GL_SKIP_DECODE_EXT
);
3689 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
3692 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
3694 if (ctx
->API
== API_OPENGL_COMPAT
|| ctx
->API
== API_OPENGLES
)
3695 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
3697 assert(!genMipmapSave
);
3699 /* Setup texture coordinates */
3700 setup_texture_coords(faceTarget
,
3702 0, 0, 1, /* width, height never used here */
3708 /* setup vertex positions */
3718 /* upload vertex data */
3719 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3720 verts
, GL_DYNAMIC_DRAW_ARB
);
3722 /* texture is already locked, unlock now */
3723 _mesa_unlock_texture(ctx
, texObj
);
3725 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
3726 const struct gl_texture_image
*srcImage
;
3727 const GLuint srcLevel
= dstLevel
- 1;
3728 GLsizei srcWidth
, srcHeight
, srcDepth
;
3729 GLsizei dstWidth
, dstHeight
, dstDepth
;
3732 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
3733 assert(srcImage
->Border
== 0);
3736 srcWidth
= srcImage
->Width
;
3737 srcHeight
= srcImage
->Height
;
3738 srcDepth
= srcImage
->Depth
;
3741 dstWidth
= MAX2(1, srcWidth
/ 2);
3742 dstHeight
= MAX2(1, srcHeight
/ 2);
3743 dstDepth
= MAX2(1, srcDepth
/ 2);
3745 if (dstWidth
== srcImage
->Width
&&
3746 dstHeight
== srcImage
->Height
&&
3747 dstDepth
== srcImage
->Depth
) {
3752 /* Allocate storage for the destination mipmap image(s) */
3754 /* Set MaxLevel large enough to hold the new level when we allocate it */
3755 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3757 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3758 dstWidth
, dstHeight
, dstDepth
,
3760 srcImage
->InternalFormat
,
3761 srcImage
->TexFormat
)) {
3762 /* All done. We either ran out of memory or we would go beyond the
3763 * last valid level of an immutable texture if we continued.
3768 /* limit minification to src level */
3769 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3771 /* Set to draw into the current dstLevel */
3772 if (target
== GL_TEXTURE_1D
) {
3773 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT
,
3774 GL_COLOR_ATTACHMENT0_EXT
,
3779 else if (target
== GL_TEXTURE_3D
) {
3780 GLint zoffset
= 0; /* XXX unfinished */
3781 _mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT
,
3782 GL_COLOR_ATTACHMENT0_EXT
,
3789 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT
,
3790 GL_COLOR_ATTACHMENT0_EXT
,
3796 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3799 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
3800 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3801 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3802 "_mesa_meta_GenerateMipmap()");
3806 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3807 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3809 /* setup viewport */
3810 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
3812 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3815 _mesa_lock_texture(ctx
, texObj
); /* relock */
3817 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
3819 _mesa_meta_end(ctx
);
3821 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3823 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3825 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
3830 * Determine the GL data type to use for the temporary image read with
3831 * ReadPixels() and passed to Tex[Sub]Image().
3834 get_temp_image_type(struct gl_context
*ctx
, mesa_format format
)
3838 baseFormat
= _mesa_get_format_base_format(format
);
3840 switch (baseFormat
) {
3847 case GL_LUMINANCE_ALPHA
:
3849 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8) {
3850 return GL_UNSIGNED_BYTE
;
3851 } else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16) {
3852 return GL_UNSIGNED_SHORT
;
3854 GLenum datatype
= _mesa_get_format_datatype(format
);
3855 if (datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
)
3859 case GL_DEPTH_COMPONENT
: {
3860 GLenum datatype
= _mesa_get_format_datatype(format
);
3861 if (datatype
== GL_FLOAT
)
3864 return GL_UNSIGNED_INT
;
3866 case GL_DEPTH_STENCIL
: {
3867 GLenum datatype
= _mesa_get_format_datatype(format
);
3868 if (datatype
== GL_FLOAT
)
3869 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV
;
3871 return GL_UNSIGNED_INT_24_8
;
3874 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3882 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3883 * Have to be careful with locking and meta state for pixel transfer.
3886 _mesa_meta_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
3887 struct gl_texture_image
*texImage
,
3888 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3889 struct gl_renderbuffer
*rb
,
3891 GLsizei width
, GLsizei height
)
3893 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3894 GLenum format
, type
;
3898 /* Choose format/type for temporary image buffer */
3899 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3900 if (format
== GL_LUMINANCE
||
3901 format
== GL_LUMINANCE_ALPHA
||
3902 format
== GL_INTENSITY
) {
3903 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3904 * temp image buffer because glReadPixels will do L=R+G+B which is
3905 * not what we want (should be L=R).
3910 type
= get_temp_image_type(ctx
, texImage
->TexFormat
);
3911 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3912 format
= _mesa_base_format_to_integer_format(format
);
3914 bpp
= _mesa_bytes_per_pixel(format
, type
);
3916 _mesa_problem(ctx
, "Bad bpp in _mesa_meta_CopyTexSubImage()");
3921 * Alloc image buffer (XXX could use a PBO)
3923 buf
= malloc(width
* height
* bpp
);
3925 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3929 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3932 * Read image from framebuffer (disable pixel transfer ops)
3934 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3935 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3936 format
, type
, &ctx
->Pack
, buf
);
3937 _mesa_meta_end(ctx
);
3939 _mesa_update_state(ctx
); /* to update pixel transfer state */
3942 * Store texture data (with pixel transfer ops)
3944 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3946 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3947 assert(yoffset
== 0);
3948 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3949 xoffset
, zoffset
, 0, width
, 1, 1,
3950 format
, type
, buf
, &ctx
->Unpack
);
3952 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3953 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3954 format
, type
, buf
, &ctx
->Unpack
);
3957 _mesa_meta_end(ctx
);
3959 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3966 meta_decompress_cleanup(struct decompress_state
*decompress
)
3968 if (decompress
->FBO
!= 0) {
3969 _mesa_DeleteFramebuffers(1, &decompress
->FBO
);
3970 _mesa_DeleteRenderbuffers(1, &decompress
->RBO
);
3973 if (decompress
->VAO
!= 0) {
3974 _mesa_DeleteVertexArrays(1, &decompress
->VAO
);
3975 _mesa_DeleteBuffers(1, &decompress
->VBO
);
3978 if (decompress
->Sampler
!= 0)
3979 _mesa_DeleteSamplers(1, &decompress
->Sampler
);
3981 memset(decompress
, 0, sizeof(*decompress
));
3985 * Decompress a texture image by drawing a quad with the compressed
3986 * texture and reading the pixels out of the color buffer.
3987 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3988 * \param destFormat format, ala glReadPixels
3989 * \param destType type, ala glReadPixels
3990 * \param dest destination buffer
3991 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3994 decompress_texture_image(struct gl_context
*ctx
,
3995 struct gl_texture_image
*texImage
,
3997 GLenum destFormat
, GLenum destType
,
4000 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
4001 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4002 const GLint width
= texImage
->Width
;
4003 const GLint height
= texImage
->Height
;
4004 const GLint depth
= texImage
->Height
;
4005 const GLenum target
= texObj
->Target
;
4008 GLfloat x
, y
, tex
[3];
4010 struct vertex verts
[4];
4011 GLuint fboDrawSave
, fboReadSave
;
4016 assert(target
== GL_TEXTURE_3D
||
4017 target
== GL_TEXTURE_2D_ARRAY
);
4022 case GL_TEXTURE_1D_ARRAY
:
4023 assert(!"No compressed 1D textures.");
4027 assert(!"No compressed 3D textures.");
4030 case GL_TEXTURE_2D_ARRAY
:
4031 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4032 /* These targets are just broken currently. */
4035 case GL_TEXTURE_CUBE_MAP
:
4036 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
4040 faceTarget
= target
;
4044 /* save fbo bindings (not saved by _mesa_meta_begin()) */
4045 fboDrawSave
= ctx
->DrawBuffer
->Name
;
4046 fboReadSave
= ctx
->ReadBuffer
->Name
;
4047 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
4049 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
4051 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
4052 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
4054 /* Create/bind FBO/renderbuffer */
4055 if (decompress
->FBO
== 0) {
4056 _mesa_GenFramebuffers(1, &decompress
->FBO
);
4057 _mesa_GenRenderbuffers(1, &decompress
->RBO
);
4058 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4059 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4060 _mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT
,
4061 GL_COLOR_ATTACHMENT0_EXT
,
4062 GL_RENDERBUFFER_EXT
,
4066 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
4069 /* alloc dest surface */
4070 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
4071 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
4072 _mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT
, GL_RGBA
,
4074 decompress
->Width
= width
;
4075 decompress
->Height
= height
;
4078 /* setup VBO data */
4079 if (decompress
->VAO
== 0) {
4080 /* create vertex array object */
4081 _mesa_GenVertexArrays(1, &decompress
->VAO
);
4082 _mesa_BindVertexArray(decompress
->VAO
);
4084 /* create vertex array buffer */
4085 _mesa_GenBuffers(1, &decompress
->VBO
);
4086 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4087 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4088 NULL
, GL_DYNAMIC_DRAW_ARB
);
4090 /* setup vertex arrays */
4091 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4092 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
4093 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4094 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4097 _mesa_BindVertexArray(decompress
->VAO
);
4098 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
4101 if (!decompress
->Sampler
) {
4102 _mesa_GenSamplers(1, &decompress
->Sampler
);
4103 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4104 /* nearest filtering */
4105 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
4106 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
4107 /* No sRGB decode or encode.*/
4108 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
4109 _mesa_SamplerParameteri(decompress
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
4110 GL_SKIP_DECODE_EXT
);
4114 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, decompress
->Sampler
);
4117 setup_texture_coords(faceTarget
, slice
, width
, height
, depth
,
4123 /* setup vertex positions */
4133 _mesa_MatrixMode(GL_PROJECTION
);
4134 _mesa_LoadIdentity();
4135 _mesa_set_viewport(ctx
, 0, 0, 0, width
, height
);
4137 /* upload new vertex data */
4138 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4140 /* setup texture state */
4141 _mesa_BindTexture(target
, texObj
->Name
);
4142 _mesa_set_enable(ctx
, target
, GL_TRUE
);
4145 /* save texture object state */
4146 const GLint baseLevelSave
= texObj
->BaseLevel
;
4147 const GLint maxLevelSave
= texObj
->MaxLevel
;
4149 /* restrict sampling to the texture level of interest */
4150 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4151 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
4152 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
4155 /* render quad w/ texture into renderbuffer */
4156 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4158 /* Restore texture object state, the texture binding will
4159 * be restored by _mesa_meta_end().
4161 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
4162 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
4163 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
4168 /* read pixels from renderbuffer */
4170 GLenum baseTexFormat
= texImage
->_BaseFormat
;
4171 GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, destFormat
);
4173 /* The pixel transfer state will be set to default values at this point
4174 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
4175 * turned off (as required by glGetTexImage) but we need to handle some
4176 * special cases. In particular, single-channel texture values are
4177 * returned as red and two-channel texture values are returned as
4180 if ((baseTexFormat
== GL_LUMINANCE
||
4181 baseTexFormat
== GL_LUMINANCE_ALPHA
||
4182 baseTexFormat
== GL_INTENSITY
) ||
4183 /* If we're reading back an RGB(A) texture (using glGetTexImage) as
4184 * luminance then we need to return L=tex(R).
4186 ((baseTexFormat
== GL_RGBA
||
4187 baseTexFormat
== GL_RGB
||
4188 baseTexFormat
== GL_RG
) &&
4189 (destBaseFormat
== GL_LUMINANCE
||
4190 destBaseFormat
== GL_LUMINANCE_ALPHA
||
4191 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
4192 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
))) {
4193 /* Green and blue must be zero */
4194 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
4195 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
4198 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
4201 /* disable texture unit */
4202 _mesa_set_enable(ctx
, target
, GL_FALSE
);
4204 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
4206 _mesa_meta_end(ctx
);
4208 /* restore fbo bindings */
4209 if (fboDrawSave
== fboReadSave
) {
4210 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
4213 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
4214 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
4216 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, rbSave
);
4221 * This is just a wrapper around _mesa_get_tex_image() and
4222 * decompress_texture_image(). Meta functions should not be directly called
4226 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
4227 GLenum format
, GLenum type
, GLvoid
*pixels
,
4228 struct gl_texture_image
*texImage
)
4230 /* We can only use the decompress-with-blit method here if the texels are
4231 * unsigned, normalized values. We could handle signed and unnormalized
4232 * with floating point renderbuffers...
4234 if (texImage
->TexObject
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
4235 && _mesa_is_format_compressed(texImage
->TexFormat
) &&
4236 _mesa_get_format_datatype(texImage
->TexFormat
)
4237 == GL_UNSIGNED_NORMALIZED
) {
4238 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4240 /* Need to unlock the texture here to prevent deadlock... */
4241 _mesa_unlock_texture(ctx
, texObj
);
4242 for (slice
= 0; slice
< texImage
->Depth
; slice
++) {
4244 if (texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
4245 /* Setup pixel packing. SkipPixels and SkipRows will be applied
4246 * in the decompress_texture_image() function's call to
4247 * glReadPixels but we need to compute the dest slice's address
4248 * here (according to SkipImages and ImageHeight).
4250 struct gl_pixelstore_attrib packing
= ctx
->Pack
;
4251 packing
.SkipPixels
= 0;
4252 packing
.SkipRows
= 0;
4253 dst
= _mesa_image_address3d(&packing
, pixels
, texImage
->Width
,
4254 texImage
->Height
, format
, type
,
4260 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, dst
);
4262 /* ... and relock it */
4263 _mesa_lock_texture(ctx
, texObj
);
4266 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
4272 * Meta implementation of ctx->Driver.DrawTex() in terms
4273 * of polygon rendering.
4276 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
4277 GLfloat width
, GLfloat height
)
4279 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
4281 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
4283 struct vertex verts
[4];
4286 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
4288 MESA_META_TRANSFORM
|
4290 MESA_META_VIEWPORT
));
4292 if (drawtex
->VAO
== 0) {
4293 /* one-time setup */
4294 GLint active_texture
;
4296 /* create vertex array object */
4297 _mesa_GenVertexArrays(1, &drawtex
->VAO
);
4298 _mesa_BindVertexArray(drawtex
->VAO
);
4300 /* create vertex array buffer */
4301 _mesa_GenBuffers(1, &drawtex
->VBO
);
4302 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4303 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
4304 NULL
, GL_DYNAMIC_DRAW_ARB
);
4306 /* client active texture is not part of the array object */
4307 active_texture
= ctx
->Array
.ActiveTexture
;
4309 /* setup vertex arrays */
4310 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
4311 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
4312 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4313 _mesa_ClientActiveTexture(GL_TEXTURE0
+ i
);
4314 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
4315 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
4318 /* restore client active texture */
4319 _mesa_ClientActiveTexture(GL_TEXTURE0
+ active_texture
);
4322 _mesa_BindVertexArray(drawtex
->VAO
);
4323 _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
4326 /* vertex positions, texcoords */
4328 const GLfloat x1
= x
+ width
;
4329 const GLfloat y1
= y
+ height
;
4331 z
= CLAMP(z
, 0.0f
, 1.0f
);
4350 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
4351 const struct gl_texture_object
*texObj
;
4352 const struct gl_texture_image
*texImage
;
4353 GLfloat s
, t
, s1
, t1
;
4356 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
4358 for (j
= 0; j
< 4; j
++) {
4359 verts
[j
].st
[i
][0] = 0.0f
;
4360 verts
[j
].st
[i
][1] = 0.0f
;
4365 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
4366 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
4367 tw
= texImage
->Width2
;
4368 th
= texImage
->Height2
;
4370 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
4371 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
4372 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
4373 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
4375 verts
[0].st
[i
][0] = s
;
4376 verts
[0].st
[i
][1] = t
;
4378 verts
[1].st
[i
][0] = s1
;
4379 verts
[1].st
[i
][1] = t
;
4381 verts
[2].st
[i
][0] = s1
;
4382 verts
[2].st
[i
][1] = t1
;
4384 verts
[3].st
[i
][0] = s
;
4385 verts
[3].st
[i
][1] = t1
;
4388 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
4391 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
4393 _mesa_meta_end(ctx
);