2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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/image.h"
50 #include "main/macros.h"
51 #include "main/matrix.h"
52 #include "main/mipmap.h"
53 #include "main/pixel.h"
55 #include "main/polygon.h"
56 #include "main/readpix.h"
57 #include "main/scissor.h"
58 #include "main/shaderapi.h"
59 #include "main/shaderobj.h"
60 #include "main/state.h"
61 #include "main/stencil.h"
62 #include "main/texobj.h"
63 #include "main/texenv.h"
64 #include "main/texgetimage.h"
65 #include "main/teximage.h"
66 #include "main/texparam.h"
67 #include "main/texstate.h"
68 #include "main/transformfeedback.h"
69 #include "main/uniforms.h"
70 #include "main/varray.h"
71 #include "main/viewport.h"
72 #include "program/program.h"
73 #include "swrast/swrast.h"
74 #include "drivers/common/meta.h"
77 /** Return offset in bytes of the field within a vertex struct */
78 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
81 * State which we may save/restore across meta ops.
82 * XXX this may be incomplete...
86 GLbitfield SavedState
; /**< bitmask of MESA_META_* flags */
88 /** MESA_META_ALPHA_TEST */
89 GLboolean AlphaEnabled
;
93 /** MESA_META_BLEND */
94 GLbitfield BlendEnabled
;
95 GLboolean ColorLogicOpEnabled
;
97 /** MESA_META_COLOR_MASK */
98 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
100 /** MESA_META_DEPTH_TEST */
101 struct gl_depthbuffer_attrib Depth
;
106 /** MESA_META_PIXEL_STORE */
107 struct gl_pixelstore_attrib Pack
, Unpack
;
109 /** MESA_META_PIXEL_TRANSFER */
110 GLfloat RedBias
, RedScale
;
111 GLfloat GreenBias
, GreenScale
;
112 GLfloat BlueBias
, BlueScale
;
113 GLfloat AlphaBias
, AlphaScale
;
114 GLfloat DepthBias
, DepthScale
;
115 GLboolean MapColorFlag
;
117 /** MESA_META_RASTERIZATION */
118 GLenum FrontPolygonMode
, BackPolygonMode
;
119 GLboolean PolygonOffset
;
120 GLboolean PolygonSmooth
;
121 GLboolean PolygonStipple
;
122 GLboolean PolygonCull
;
124 /** MESA_META_SCISSOR */
125 struct gl_scissor_attrib Scissor
;
127 /** MESA_META_SHADER */
128 GLboolean VertexProgramEnabled
;
129 struct gl_vertex_program
*VertexProgram
;
130 GLboolean FragmentProgramEnabled
;
131 struct gl_fragment_program
*FragmentProgram
;
132 struct gl_shader_program
*VertexShader
;
133 struct gl_shader_program
*GeometryShader
;
134 struct gl_shader_program
*FragmentShader
;
135 struct gl_shader_program
*ActiveShader
;
137 /** MESA_META_STENCIL_TEST */
138 struct gl_stencil_attrib Stencil
;
140 /** MESA_META_TRANSFORM */
142 GLfloat ModelviewMatrix
[16];
143 GLfloat ProjectionMatrix
[16];
144 GLfloat TextureMatrix
[16];
146 /** MESA_META_CLIP */
147 GLbitfield ClipPlanesEnabled
;
149 /** MESA_META_TEXTURE */
151 GLuint ClientActiveUnit
;
152 /** for unit[0] only */
153 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
154 /** mask of TEXTURE_2D_BIT, etc */
155 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
156 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
157 GLuint EnvMode
; /* unit[0] only */
159 /** MESA_META_VERTEX */
160 struct gl_array_object
*ArrayObj
;
161 struct gl_buffer_object
*ArrayBufferObj
;
163 /** MESA_META_VIEWPORT */
164 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
165 GLclampd DepthNear
, DepthFar
;
167 /** MESA_META_CLAMP_FRAGMENT_COLOR */
168 GLenum ClampFragmentColor
;
170 /** MESA_META_CLAMP_VERTEX_COLOR */
171 GLenum ClampVertexColor
;
173 /** MESA_META_CONDITIONAL_RENDER */
174 struct gl_query_object
*CondRenderQuery
;
175 GLenum CondRenderMode
;
178 /** MESA_META_SELECT_FEEDBACK */
180 struct gl_selection Select
;
181 struct gl_feedback Feedback
;
184 /** Miscellaneous (always disabled) */
186 GLboolean RasterDiscard
;
187 #if FEATURE_EXT_transform_feedback
188 GLboolean TransformFeedbackNeedsResume
;
193 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
194 * This is currently shared by all the meta ops. But we could create a
195 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
200 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
201 GLsizei MinSize
; /**< Min texture size to allocate */
202 GLsizei MaxSize
; /**< Max possible texture size */
203 GLboolean NPOT
; /**< Non-power of two size OK? */
204 GLsizei Width
, Height
; /**< Current texture size */
206 GLfloat Sright
, Ttop
; /**< right, top texcoords */
211 * State for glBlitFramebufer()
222 * State for glClear()
231 GLuint IntegerShaderProg
;
232 GLint IntegerColorLocation
;
237 * State for glCopyPixels()
247 * State for glDrawPixels()
253 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
254 GLuint DepthFP
; /**< Fragment program for drawing depth images */
259 * State for glBitmap()
265 struct temp_texture Tex
; /**< separate texture from other meta ops */
270 * State for _mesa_meta_generate_mipmap()
272 struct gen_mipmap_state
281 * State for texture decompression
283 struct decompress_state
286 GLuint VBO
, FBO
, RBO
;
291 * State for glDrawTex()
299 #define MAX_META_OPS_DEPTH 8
301 * All per-context meta state.
305 /** Stack of state saved during meta-ops */
306 struct save_state Save
[MAX_META_OPS_DEPTH
];
307 /** Save stack depth */
308 GLuint SaveStackDepth
;
310 struct temp_texture TempTex
;
312 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
313 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
314 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
315 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
316 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
317 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
318 struct decompress_state Decompress
; /**< For texture decompression */
319 struct drawtex_state DrawTex
; /**< For _mesa_meta_DrawTex() */
323 compile_shader_with_debug(struct gl_context
*ctx
, GLenum target
, const GLcharARB
*source
)
329 shader
= _mesa_CreateShaderObjectARB(target
);
330 _mesa_ShaderSourceARB(shader
, 1, &source
, NULL
);
331 _mesa_CompileShaderARB(shader
);
333 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &ok
);
337 _mesa_GetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &size
);
345 _mesa_GetProgramInfoLog(shader
, size
, NULL
, info
);
347 "meta program compile failed:\n%s\n"
357 link_program_with_debug(struct gl_context
*ctx
, GLuint program
)
362 _mesa_LinkProgramARB(program
);
364 _mesa_GetProgramiv(program
, GL_LINK_STATUS
, &ok
);
368 _mesa_GetProgramiv(program
, GL_INFO_LOG_LENGTH
, &size
);
376 _mesa_GetProgramInfoLog(program
, size
, NULL
, info
);
377 _mesa_problem(ctx
, "meta program link failed:\n%s", info
);
385 * Initialize meta-ops for a context.
386 * To be called once during context creation.
389 _mesa_meta_init(struct gl_context
*ctx
)
393 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
398 * Free context meta-op state.
399 * To be called once during context destruction.
402 _mesa_meta_free(struct gl_context
*ctx
)
404 /* Note: Any textures, VBOs, etc, that we allocate should get
405 * freed by the normal context destruction code. But this would be
406 * the place to free other meta data someday.
414 * Enter meta state. This is like a light-weight version of glPushAttrib
415 * but it also resets most GL state back to default values.
417 * \param state bitmask of MESA_META_* flags indicating which attribute groups
418 * to save and reset to their defaults
421 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
423 struct save_state
*save
;
425 /* hope MAX_META_OPS_DEPTH is large enough */
426 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
428 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
429 memset(save
, 0, sizeof(*save
));
430 save
->SavedState
= state
;
432 #if FEATURE_EXT_transform_feedback
433 /* Pausing transform feedback needs to be done early, or else we won't be
434 * able to change other state.
436 save
->TransformFeedbackNeedsResume
=
437 ctx
->TransformFeedback
.CurrentObject
->Active
&&
438 !ctx
->TransformFeedback
.CurrentObject
->Paused
;
439 if (save
->TransformFeedbackNeedsResume
)
440 _mesa_PauseTransformFeedback();
443 if (state
& MESA_META_ALPHA_TEST
) {
444 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
445 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
446 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
447 if (ctx
->Color
.AlphaEnabled
)
448 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
451 if (state
& MESA_META_BLEND
) {
452 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
453 if (ctx
->Color
.BlendEnabled
) {
454 if (ctx
->Extensions
.EXT_draw_buffers2
) {
456 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
457 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
461 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
464 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
465 if (ctx
->Color
.ColorLogicOpEnabled
)
466 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
469 if (state
& MESA_META_COLOR_MASK
) {
470 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
471 sizeof(ctx
->Color
.ColorMask
));
472 if (!ctx
->Color
.ColorMask
[0][0] ||
473 !ctx
->Color
.ColorMask
[0][1] ||
474 !ctx
->Color
.ColorMask
[0][2] ||
475 !ctx
->Color
.ColorMask
[0][3])
476 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
479 if (state
& MESA_META_DEPTH_TEST
) {
480 save
->Depth
= ctx
->Depth
; /* struct copy */
482 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
485 if (state
& MESA_META_FOG
) {
486 save
->Fog
= ctx
->Fog
.Enabled
;
487 if (ctx
->Fog
.Enabled
)
488 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
491 if (state
& MESA_META_PIXEL_STORE
) {
492 save
->Pack
= ctx
->Pack
;
493 save
->Unpack
= ctx
->Unpack
;
494 ctx
->Pack
= ctx
->DefaultPacking
;
495 ctx
->Unpack
= ctx
->DefaultPacking
;
498 if (state
& MESA_META_PIXEL_TRANSFER
) {
499 save
->RedScale
= ctx
->Pixel
.RedScale
;
500 save
->RedBias
= ctx
->Pixel
.RedBias
;
501 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
502 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
503 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
504 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
505 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
506 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
507 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
508 ctx
->Pixel
.RedScale
= 1.0F
;
509 ctx
->Pixel
.RedBias
= 0.0F
;
510 ctx
->Pixel
.GreenScale
= 1.0F
;
511 ctx
->Pixel
.GreenBias
= 0.0F
;
512 ctx
->Pixel
.BlueScale
= 1.0F
;
513 ctx
->Pixel
.BlueBias
= 0.0F
;
514 ctx
->Pixel
.AlphaScale
= 1.0F
;
515 ctx
->Pixel
.AlphaBias
= 0.0F
;
516 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
518 ctx
->NewState
|=_NEW_PIXEL
;
521 if (state
& MESA_META_RASTERIZATION
) {
522 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
523 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
524 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
525 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
526 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
527 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
528 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
529 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
530 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
531 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
532 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
535 if (state
& MESA_META_SCISSOR
) {
536 save
->Scissor
= ctx
->Scissor
; /* struct copy */
537 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
540 if (state
& MESA_META_SHADER
) {
541 if (ctx
->Extensions
.ARB_vertex_program
) {
542 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
543 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
544 ctx
->VertexProgram
.Current
);
545 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
548 if (ctx
->Extensions
.ARB_fragment_program
) {
549 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
550 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
551 ctx
->FragmentProgram
.Current
);
552 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
555 if (ctx
->Extensions
.ARB_shader_objects
) {
556 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
557 ctx
->Shader
.CurrentVertexProgram
);
558 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
559 ctx
->Shader
.CurrentGeometryProgram
);
560 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
561 ctx
->Shader
.CurrentFragmentProgram
);
562 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
563 ctx
->Shader
.ActiveProgram
);
565 _mesa_UseProgramObjectARB(0);
569 if (state
& MESA_META_STENCIL_TEST
) {
570 save
->Stencil
= ctx
->Stencil
; /* struct copy */
571 if (ctx
->Stencil
.Enabled
)
572 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
573 /* NOTE: other stencil state not reset */
576 if (state
& MESA_META_TEXTURE
) {
579 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
580 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
581 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
583 /* Disable all texture units */
584 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
585 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
586 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
587 if (ctx
->Texture
.Unit
[u
].Enabled
||
588 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
589 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
590 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
591 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
592 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
593 if (ctx
->Extensions
.ARB_texture_cube_map
)
594 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
595 if (ctx
->Extensions
.NV_texture_rectangle
)
596 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
597 if (ctx
->Extensions
.OES_EGL_image_external
)
598 _mesa_set_enable(ctx
, GL_TEXTURE_EXTERNAL_OES
, GL_FALSE
);
599 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
600 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
601 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
602 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
606 /* save current texture objects for unit[0] only */
607 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
608 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
609 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
612 /* set defaults for unit[0] */
613 _mesa_ActiveTextureARB(GL_TEXTURE0
);
614 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
615 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
618 if (state
& MESA_META_TRANSFORM
) {
619 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
620 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
621 16 * sizeof(GLfloat
));
622 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
623 16 * sizeof(GLfloat
));
624 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
625 16 * sizeof(GLfloat
));
626 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
627 /* set 1:1 vertex:pixel coordinate transform */
628 _mesa_ActiveTextureARB(GL_TEXTURE0
);
629 _mesa_MatrixMode(GL_TEXTURE
);
630 _mesa_LoadIdentity();
631 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
632 _mesa_MatrixMode(GL_MODELVIEW
);
633 _mesa_LoadIdentity();
634 _mesa_MatrixMode(GL_PROJECTION
);
635 _mesa_LoadIdentity();
636 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
637 0.0, ctx
->DrawBuffer
->Height
,
641 if (state
& MESA_META_CLIP
) {
642 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
643 if (ctx
->Transform
.ClipPlanesEnabled
) {
645 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
646 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
651 if (state
& MESA_META_VERTEX
) {
652 /* save vertex array object state */
653 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
654 ctx
->Array
.ArrayObj
);
655 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
656 ctx
->Array
.ArrayBufferObj
);
657 /* set some default state? */
660 if (state
& MESA_META_VIEWPORT
) {
661 /* save viewport state */
662 save
->ViewportX
= ctx
->Viewport
.X
;
663 save
->ViewportY
= ctx
->Viewport
.Y
;
664 save
->ViewportW
= ctx
->Viewport
.Width
;
665 save
->ViewportH
= ctx
->Viewport
.Height
;
666 /* set viewport to match window size */
667 if (ctx
->Viewport
.X
!= 0 ||
668 ctx
->Viewport
.Y
!= 0 ||
669 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
670 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
671 _mesa_set_viewport(ctx
, 0, 0,
672 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
674 /* save depth range state */
675 save
->DepthNear
= ctx
->Viewport
.Near
;
676 save
->DepthFar
= ctx
->Viewport
.Far
;
677 /* set depth range to default */
678 _mesa_DepthRange(0.0, 1.0);
681 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
682 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
684 /* Generally in here we want to do clamping according to whether
685 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
686 * regardless of the internal implementation of the metaops.
688 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
689 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
692 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
693 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
695 /* Generally in here we never want vertex color clamping --
696 * result clamping is only dependent on fragment clamping.
698 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
701 if (state
& MESA_META_CONDITIONAL_RENDER
) {
702 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
703 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
705 if (ctx
->Query
.CondRenderQuery
)
706 _mesa_EndConditionalRender();
710 if (state
& MESA_META_SELECT_FEEDBACK
) {
711 save
->RenderMode
= ctx
->RenderMode
;
712 if (ctx
->RenderMode
== GL_SELECT
) {
713 save
->Select
= ctx
->Select
; /* struct copy */
714 _mesa_RenderMode(GL_RENDER
);
715 } else if (ctx
->RenderMode
== GL_FEEDBACK
) {
716 save
->Feedback
= ctx
->Feedback
; /* struct copy */
717 _mesa_RenderMode(GL_RENDER
);
724 save
->Lighting
= ctx
->Light
.Enabled
;
725 if (ctx
->Light
.Enabled
)
726 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
727 save
->RasterDiscard
= ctx
->RasterDiscard
;
728 if (ctx
->RasterDiscard
)
729 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_FALSE
);
735 * Leave meta state. This is like a light-weight version of glPopAttrib().
738 _mesa_meta_end(struct gl_context
*ctx
)
740 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
741 const GLbitfield state
= save
->SavedState
;
743 if (state
& MESA_META_ALPHA_TEST
) {
744 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
745 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
746 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
749 if (state
& MESA_META_BLEND
) {
750 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
751 if (ctx
->Extensions
.EXT_draw_buffers2
) {
753 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
754 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
758 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
761 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
762 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
765 if (state
& MESA_META_COLOR_MASK
) {
767 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
768 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
770 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
771 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
774 _mesa_ColorMaskIndexed(i
,
775 save
->ColorMask
[i
][0],
776 save
->ColorMask
[i
][1],
777 save
->ColorMask
[i
][2],
778 save
->ColorMask
[i
][3]);
784 if (state
& MESA_META_DEPTH_TEST
) {
785 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
786 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
787 _mesa_DepthFunc(save
->Depth
.Func
);
788 _mesa_DepthMask(save
->Depth
.Mask
);
791 if (state
& MESA_META_FOG
) {
792 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
795 if (state
& MESA_META_PIXEL_STORE
) {
796 ctx
->Pack
= save
->Pack
;
797 ctx
->Unpack
= save
->Unpack
;
800 if (state
& MESA_META_PIXEL_TRANSFER
) {
801 ctx
->Pixel
.RedScale
= save
->RedScale
;
802 ctx
->Pixel
.RedBias
= save
->RedBias
;
803 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
804 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
805 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
806 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
807 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
808 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
809 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
811 ctx
->NewState
|=_NEW_PIXEL
;
814 if (state
& MESA_META_RASTERIZATION
) {
815 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
816 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
817 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
818 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
819 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
820 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
823 if (state
& MESA_META_SCISSOR
) {
824 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
825 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
826 save
->Scissor
.Width
, save
->Scissor
.Height
);
829 if (state
& MESA_META_SHADER
) {
830 if (ctx
->Extensions
.ARB_vertex_program
) {
831 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
832 save
->VertexProgramEnabled
);
833 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
834 save
->VertexProgram
);
835 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
838 if (ctx
->Extensions
.ARB_fragment_program
) {
839 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
840 save
->FragmentProgramEnabled
);
841 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
842 save
->FragmentProgram
);
843 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
846 if (ctx
->Extensions
.ARB_vertex_shader
)
847 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
849 if (ctx
->Extensions
.ARB_geometry_shader4
)
850 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
851 save
->GeometryShader
);
853 if (ctx
->Extensions
.ARB_fragment_shader
)
854 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
855 save
->FragmentShader
);
857 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
860 _mesa_reference_shader_program(ctx
, &save
->VertexShader
, NULL
);
861 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
, NULL
);
862 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
, NULL
);
863 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
, NULL
);
866 if (state
& MESA_META_STENCIL_TEST
) {
867 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
869 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
870 _mesa_ClearStencil(stencil
->Clear
);
871 if (ctx
->Extensions
.EXT_stencil_two_side
) {
872 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
873 stencil
->TestTwoSide
);
874 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
875 ? GL_BACK
: GL_FRONT
);
878 _mesa_StencilFuncSeparate(GL_FRONT
,
879 stencil
->Function
[0],
881 stencil
->ValueMask
[0]);
882 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
883 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
884 stencil
->ZFailFunc
[0],
885 stencil
->ZPassFunc
[0]);
887 _mesa_StencilFuncSeparate(GL_BACK
,
888 stencil
->Function
[1],
890 stencil
->ValueMask
[1]);
891 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
892 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
893 stencil
->ZFailFunc
[1],
894 stencil
->ZPassFunc
[1]);
897 if (state
& MESA_META_TEXTURE
) {
900 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
902 /* restore texenv for unit[0] */
903 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
905 /* restore texture objects for unit[0] only */
906 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
907 if (ctx
->Texture
.Unit
[0].CurrentTex
[tgt
] != save
->CurrentTexture
[tgt
]) {
908 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
909 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
910 save
->CurrentTexture
[tgt
]);
912 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
915 /* Restore fixed function texture enables, texgen */
916 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
917 if (ctx
->Texture
.Unit
[u
].Enabled
!= save
->TexEnabled
[u
]) {
918 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
919 ctx
->Texture
.Unit
[u
].Enabled
= save
->TexEnabled
[u
];
922 if (ctx
->Texture
.Unit
[u
].TexGenEnabled
!= save
->TexGenEnabled
[u
]) {
923 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
924 ctx
->Texture
.Unit
[u
].TexGenEnabled
= save
->TexGenEnabled
[u
];
928 /* restore current unit state */
929 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
930 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
933 if (state
& MESA_META_TRANSFORM
) {
934 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
935 _mesa_ActiveTextureARB(GL_TEXTURE0
);
936 _mesa_MatrixMode(GL_TEXTURE
);
937 _mesa_LoadMatrixf(save
->TextureMatrix
);
938 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
940 _mesa_MatrixMode(GL_MODELVIEW
);
941 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
943 _mesa_MatrixMode(GL_PROJECTION
);
944 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
946 _mesa_MatrixMode(save
->MatrixMode
);
949 if (state
& MESA_META_CLIP
) {
950 if (save
->ClipPlanesEnabled
) {
952 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
953 if (save
->ClipPlanesEnabled
& (1 << i
)) {
954 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
960 if (state
& MESA_META_VERTEX
) {
961 /* restore vertex buffer object */
962 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
963 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
965 /* restore vertex array object */
966 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
967 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
970 if (state
& MESA_META_VIEWPORT
) {
971 if (save
->ViewportX
!= ctx
->Viewport
.X
||
972 save
->ViewportY
!= ctx
->Viewport
.Y
||
973 save
->ViewportW
!= ctx
->Viewport
.Width
||
974 save
->ViewportH
!= ctx
->Viewport
.Height
) {
975 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
976 save
->ViewportW
, save
->ViewportH
);
978 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
981 if (state
& MESA_META_CLAMP_FRAGMENT_COLOR
) {
982 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
985 if (state
& MESA_META_CLAMP_VERTEX_COLOR
) {
986 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
989 if (state
& MESA_META_CONDITIONAL_RENDER
) {
990 if (save
->CondRenderQuery
)
991 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
992 save
->CondRenderMode
);
996 if (state
& MESA_META_SELECT_FEEDBACK
) {
997 if (save
->RenderMode
== GL_SELECT
) {
998 _mesa_RenderMode(GL_SELECT
);
999 ctx
->Select
= save
->Select
;
1000 } else if (save
->RenderMode
== GL_FEEDBACK
) {
1001 _mesa_RenderMode(GL_FEEDBACK
);
1002 ctx
->Feedback
= save
->Feedback
;
1008 if (save
->Lighting
) {
1009 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
1011 if (save
->RasterDiscard
) {
1012 _mesa_set_enable(ctx
, GL_RASTERIZER_DISCARD
, GL_TRUE
);
1014 #if FEATURE_EXT_transform_feedback
1015 if (save
->TransformFeedbackNeedsResume
)
1016 _mesa_ResumeTransformFeedback();
1022 * Determine whether Mesa is currently in a meta state.
1025 _mesa_meta_in_progress(struct gl_context
*ctx
)
1027 return ctx
->Meta
->SaveStackDepth
!= 0;
1032 * Convert Z from a normalized value in the range [0, 1] to an object-space
1033 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1034 * default/identity ortho projection results in the original Z value.
1035 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1036 * value comes from the clear value or raster position.
1038 static INLINE GLfloat
1039 invert_z(GLfloat normZ
)
1041 GLfloat objZ
= 1.0f
- 2.0f
* normZ
;
1047 * One-time init for a temp_texture object.
1048 * Choose tex target, compute max tex size, etc.
1051 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
1053 /* prefer texture rectangle */
1054 if (ctx
->Extensions
.NV_texture_rectangle
) {
1055 tex
->Target
= GL_TEXTURE_RECTANGLE
;
1056 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
1057 tex
->NPOT
= GL_TRUE
;
1060 /* use 2D texture, NPOT if possible */
1061 tex
->Target
= GL_TEXTURE_2D
;
1062 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1063 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
1065 tex
->MinSize
= 16; /* 16 x 16 at least */
1066 assert(tex
->MaxSize
> 0);
1068 _mesa_GenTextures(1, &tex
->TexObj
);
1073 * Return pointer to temp_texture info for non-bitmap ops.
1074 * This does some one-time init if needed.
1076 static struct temp_texture
*
1077 get_temp_texture(struct gl_context
*ctx
)
1079 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
1082 init_temp_texture(ctx
, tex
);
1090 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1091 * We use a separate texture for bitmaps to reduce texture
1092 * allocation/deallocation.
1094 static struct temp_texture
*
1095 get_bitmap_temp_texture(struct gl_context
*ctx
)
1097 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
1100 init_temp_texture(ctx
, tex
);
1108 * Compute the width/height of texture needed to draw an image of the
1109 * given size. Return a flag indicating whether the current texture
1110 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1111 * allocated (glTexImage2D).
1112 * Also, compute s/t texcoords for drawing.
1114 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1117 alloc_texture(struct temp_texture
*tex
,
1118 GLsizei width
, GLsizei height
, GLenum intFormat
)
1120 GLboolean newTex
= GL_FALSE
;
1122 ASSERT(width
<= tex
->MaxSize
);
1123 ASSERT(height
<= tex
->MaxSize
);
1125 if (width
> tex
->Width
||
1126 height
> tex
->Height
||
1127 intFormat
!= tex
->IntFormat
) {
1128 /* alloc new texture (larger or different format) */
1131 /* use non-power of two size */
1132 tex
->Width
= MAX2(tex
->MinSize
, width
);
1133 tex
->Height
= MAX2(tex
->MinSize
, height
);
1136 /* find power of two size */
1138 w
= h
= tex
->MinSize
;
1147 tex
->IntFormat
= intFormat
;
1152 /* compute texcoords */
1153 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1154 tex
->Sright
= (GLfloat
) width
;
1155 tex
->Ttop
= (GLfloat
) height
;
1158 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1159 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1167 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1170 setup_copypix_texture(struct temp_texture
*tex
,
1172 GLint srcX
, GLint srcY
,
1173 GLsizei width
, GLsizei height
, GLenum intFormat
,
1176 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1177 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1178 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1179 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1181 /* copy framebuffer image to texture */
1183 /* create new tex image */
1184 if (tex
->Width
== width
&& tex
->Height
== height
) {
1185 /* create new tex with framebuffer data */
1186 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1187 srcX
, srcY
, width
, height
, 0);
1190 /* create empty texture */
1191 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1192 tex
->Width
, tex
->Height
, 0,
1193 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1195 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1196 0, 0, srcX
, srcY
, width
, height
);
1200 /* replace existing tex image */
1201 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1202 0, 0, srcX
, srcY
, width
, height
);
1208 * Setup/load texture for glDrawPixels.
1211 setup_drawpix_texture(struct gl_context
*ctx
,
1212 struct temp_texture
*tex
,
1214 GLenum texIntFormat
,
1215 GLsizei width
, GLsizei height
,
1216 GLenum format
, GLenum type
,
1217 const GLvoid
*pixels
)
1219 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1220 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1221 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1222 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1224 /* copy pixel data to texture */
1226 /* create new tex image */
1227 if (tex
->Width
== width
&& tex
->Height
== height
) {
1228 /* create new tex and load image data */
1229 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1230 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1233 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1235 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1236 ctx
->Unpack
.BufferObj
);
1237 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1238 /* create empty texture */
1239 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1240 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1241 if (save_unpack_obj
!= NULL
)
1242 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1243 save_unpack_obj
->Name
);
1245 _mesa_TexSubImage2D(tex
->Target
, 0,
1246 0, 0, width
, height
, format
, type
, pixels
);
1250 /* replace existing tex image */
1251 _mesa_TexSubImage2D(tex
->Target
, 0,
1252 0, 0, width
, height
, format
, type
, pixels
);
1259 * One-time init for drawing depth pixels.
1262 init_blit_depth_pixels(struct gl_context
*ctx
)
1264 static const char *program
=
1266 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1269 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1270 struct temp_texture
*tex
= get_temp_texture(ctx
);
1271 const char *texTarget
;
1273 assert(blit
->DepthFP
== 0);
1275 /* replace %s with "RECT" or "2D" */
1276 assert(strlen(program
) + 4 < sizeof(program2
));
1277 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1281 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1283 _mesa_GenPrograms(1, &blit
->DepthFP
);
1284 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1285 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1286 strlen(program2
), (const GLubyte
*) program2
);
1291 * Try to do a glBlitFramebuffer using no-copy texturing.
1292 * We can do this when the src renderbuffer is actually a texture.
1293 * But if the src buffer == dst buffer we cannot do this.
1295 * \return new buffer mask indicating the buffers left to blit using the
1299 blitframebuffer_texture(struct gl_context
*ctx
,
1300 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1301 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1302 GLbitfield mask
, GLenum filter
)
1304 if (mask
& GL_COLOR_BUFFER_BIT
) {
1305 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1306 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1307 const struct gl_renderbuffer_attachment
*drawAtt
=
1308 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1309 const struct gl_renderbuffer_attachment
*readAtt
=
1310 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1312 if (readAtt
&& readAtt
->Texture
) {
1313 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1314 const GLuint srcLevel
= readAtt
->TextureLevel
;
1315 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1316 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1317 const GLint baseLevelSave
= texObj
->BaseLevel
;
1318 const GLint maxLevelSave
= texObj
->MaxLevel
;
1319 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1320 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1321 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1322 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1323 const GLenum target
= texObj
->Target
;
1325 if (drawAtt
->Texture
== readAtt
->Texture
) {
1326 /* Can't use same texture as both the source and dest. We need
1327 * to handle overlapping blits and besides, some hw may not
1333 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1334 /* Can't handle other texture types at this time */
1339 printf("Blit from texture!\n");
1340 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1341 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1344 /* Prepare src texture state */
1345 _mesa_BindTexture(target
, texObj
->Name
);
1346 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1347 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1348 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1349 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1350 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1352 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1353 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1355 /* Always do our blits with no sRGB decode or encode.*/
1356 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1357 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1358 GL_SKIP_DECODE_EXT
);
1360 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
1361 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
1364 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1365 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1367 /* Prepare vertex data (the VBO was previously created and bound) */
1372 struct vertex verts
[4];
1373 GLfloat s0
, t0
, s1
, t1
;
1375 if (target
== GL_TEXTURE_2D
) {
1376 const struct gl_texture_image
*texImage
1377 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1378 s0
= srcX0
/ (float) texImage
->Width
;
1379 s1
= srcX1
/ (float) texImage
->Width
;
1380 t0
= srcY0
/ (float) texImage
->Height
;
1381 t1
= srcY1
/ (float) texImage
->Height
;
1384 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1391 verts
[0].x
= (GLfloat
) dstX0
;
1392 verts
[0].y
= (GLfloat
) dstY0
;
1393 verts
[1].x
= (GLfloat
) dstX1
;
1394 verts
[1].y
= (GLfloat
) dstY0
;
1395 verts
[2].x
= (GLfloat
) dstX1
;
1396 verts
[2].y
= (GLfloat
) dstY1
;
1397 verts
[3].x
= (GLfloat
) dstX0
;
1398 verts
[3].y
= (GLfloat
) dstY1
;
1409 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1412 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1414 /* Restore texture object state, the texture binding will
1415 * be restored by _mesa_meta_end().
1417 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1418 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1419 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1420 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1421 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1423 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1424 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1425 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1426 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1428 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& fbo_srgb_save
) {
1429 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
1432 /* Done with color buffer */
1433 mask
&= ~GL_COLOR_BUFFER_BIT
;
1442 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1443 * of texture mapping and polygon rendering.
1446 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1447 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1448 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1449 GLbitfield mask
, GLenum filter
)
1451 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1452 struct temp_texture
*tex
= get_temp_texture(ctx
);
1453 const GLsizei maxTexSize
= tex
->MaxSize
;
1454 const GLint srcX
= MIN2(srcX0
, srcX1
);
1455 const GLint srcY
= MIN2(srcY0
, srcY1
);
1456 const GLint srcW
= abs(srcX1
- srcX0
);
1457 const GLint srcH
= abs(srcY1
- srcY0
);
1458 const GLboolean srcFlipX
= srcX1
< srcX0
;
1459 const GLboolean srcFlipY
= srcY1
< srcY0
;
1463 struct vertex verts
[4];
1466 /* In addition to falling back if the blit size is larger than the maximum
1467 * texture size, fallback if the source is multisampled. This fallback can
1468 * be removed once Mesa gets support ARB_texture_multisample.
1470 if (srcW
> maxTexSize
|| srcH
> maxTexSize
1471 || ctx
->ReadBuffer
->Visual
.samples
> 0) {
1472 /* XXX avoid this fallback */
1473 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1474 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1490 /* only scissor effects blit so save/clear all other relevant state */
1491 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
1493 if (blit
->ArrayObj
== 0) {
1494 /* one-time setup */
1496 /* create vertex array object */
1497 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1498 _mesa_BindVertexArray(blit
->ArrayObj
);
1500 /* create vertex array buffer */
1501 _mesa_GenBuffersARB(1, &blit
->VBO
);
1502 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1503 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1504 NULL
, GL_DYNAMIC_DRAW_ARB
);
1506 /* setup vertex arrays */
1507 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1508 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1509 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1510 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1513 _mesa_BindVertexArray(blit
->ArrayObj
);
1514 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1517 /* Try faster, direct texture approach first */
1518 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1519 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1521 _mesa_meta_end(ctx
);
1525 /* Continue with "normal" approach which involves copying the src rect
1526 * into a temporary texture and is "blitted" by drawing a textured quad.
1529 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1531 /* vertex positions/texcoords (after texture allocation!) */
1533 verts
[0].x
= (GLfloat
) dstX0
;
1534 verts
[0].y
= (GLfloat
) dstY0
;
1535 verts
[1].x
= (GLfloat
) dstX1
;
1536 verts
[1].y
= (GLfloat
) dstY0
;
1537 verts
[2].x
= (GLfloat
) dstX1
;
1538 verts
[2].y
= (GLfloat
) dstY1
;
1539 verts
[3].x
= (GLfloat
) dstX0
;
1540 verts
[3].y
= (GLfloat
) dstY1
;
1544 verts
[1].s
= tex
->Sright
;
1546 verts
[2].s
= tex
->Sright
;
1547 verts
[2].t
= tex
->Ttop
;
1549 verts
[3].t
= tex
->Ttop
;
1551 /* upload new vertex data */
1552 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1555 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1557 if (mask
& GL_COLOR_BUFFER_BIT
) {
1558 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1560 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1561 mask
&= ~GL_COLOR_BUFFER_BIT
;
1564 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1565 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1568 init_blit_depth_pixels(ctx
);
1570 /* maybe change tex format here */
1571 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1573 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1574 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1576 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1577 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1579 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1580 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1581 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1582 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1583 _mesa_DepthFunc(GL_ALWAYS
);
1584 _mesa_DepthMask(GL_TRUE
);
1586 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1587 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1593 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1594 /* XXX can't easily do stencil */
1597 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1599 _mesa_meta_end(ctx
);
1602 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1603 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1609 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1612 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1614 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1616 GLfloat x
, y
, z
, r
, g
, b
, a
;
1618 struct vertex verts
[4];
1619 /* save all state but scissor, pixel pack/unpack */
1620 GLbitfield metaSave
= (MESA_META_ALL
-
1622 MESA_META_PIXEL_STORE
-
1623 MESA_META_CONDITIONAL_RENDER
);
1624 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1626 if (buffers
& BUFFER_BITS_COLOR
) {
1627 /* if clearing color buffers, don't save/restore colormask */
1628 metaSave
-= MESA_META_COLOR_MASK
;
1631 _mesa_meta_begin(ctx
, metaSave
);
1633 if (clear
->ArrayObj
== 0) {
1634 /* one-time setup */
1636 /* create vertex array object */
1637 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1638 _mesa_BindVertexArray(clear
->ArrayObj
);
1640 /* create vertex array buffer */
1641 _mesa_GenBuffersARB(1, &clear
->VBO
);
1642 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1644 /* setup vertex arrays */
1645 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1646 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1647 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1648 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1651 _mesa_BindVertexArray(clear
->ArrayObj
);
1652 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1655 /* GL_COLOR_BUFFER_BIT */
1656 if (buffers
& BUFFER_BITS_COLOR
) {
1657 /* leave colormask, glDrawBuffer state as-is */
1659 /* Clears never have the color clamped. */
1660 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1663 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1664 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1667 /* GL_DEPTH_BUFFER_BIT */
1668 if (buffers
& BUFFER_BIT_DEPTH
) {
1669 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1670 _mesa_DepthFunc(GL_ALWAYS
);
1671 _mesa_DepthMask(GL_TRUE
);
1674 assert(!ctx
->Depth
.Test
);
1677 /* GL_STENCIL_BUFFER_BIT */
1678 if (buffers
& BUFFER_BIT_STENCIL
) {
1679 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1680 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1681 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1682 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1683 ctx
->Stencil
.Clear
& stencilMax
,
1684 ctx
->Stencil
.WriteMask
[0]);
1687 assert(!ctx
->Stencil
.Enabled
);
1690 /* vertex positions/colors */
1692 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1693 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1694 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1695 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1696 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1713 for (i
= 0; i
< 4; i
++) {
1714 verts
[i
].r
= ctx
->Color
.ClearColor
.f
[0];
1715 verts
[i
].g
= ctx
->Color
.ClearColor
.f
[1];
1716 verts
[i
].b
= ctx
->Color
.ClearColor
.f
[2];
1717 verts
[i
].a
= ctx
->Color
.ClearColor
.f
[3];
1720 /* upload new vertex data */
1721 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1722 GL_DYNAMIC_DRAW_ARB
);
1726 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1728 _mesa_meta_end(ctx
);
1732 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1734 const char *vs_source
=
1735 "attribute vec4 position;\n"
1738 " gl_Position = position;\n"
1740 const char *fs_source
=
1741 "uniform vec4 color;\n"
1744 " gl_FragColor = color;\n"
1746 const char *vs_int_source
=
1748 "attribute vec4 position;\n"
1751 " gl_Position = position;\n"
1753 const char *fs_int_source
=
1755 "uniform ivec4 color;\n"
1756 "out ivec4 out_color;\n"
1760 " out_color = color;\n"
1764 if (clear
->ArrayObj
!= 0)
1767 /* create vertex array object */
1768 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1769 _mesa_BindVertexArray(clear
->ArrayObj
);
1771 /* create vertex array buffer */
1772 _mesa_GenBuffersARB(1, &clear
->VBO
);
1773 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1775 /* setup vertex arrays */
1776 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1777 _mesa_EnableVertexAttribArrayARB(0);
1779 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1780 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1781 _mesa_CompileShaderARB(vs
);
1783 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1784 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1785 _mesa_CompileShaderARB(fs
);
1787 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1788 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1789 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1790 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1791 _mesa_LinkProgramARB(clear
->ShaderProg
);
1793 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1796 if (ctx
->API
== API_OPENGL
&& ctx
->Const
.GLSLVersion
>= 130) {
1797 vs
= compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_int_source
);
1798 fs
= compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_int_source
);
1800 clear
->IntegerShaderProg
= _mesa_CreateProgramObjectARB();
1801 _mesa_AttachShader(clear
->IntegerShaderProg
, fs
);
1802 _mesa_AttachShader(clear
->IntegerShaderProg
, vs
);
1803 _mesa_BindAttribLocationARB(clear
->IntegerShaderProg
, 0, "position");
1805 /* Note that user-defined out attributes get automatically assigned
1806 * locations starting from 0, so we don't need to explicitly
1807 * BindFragDataLocation to 0.
1810 link_program_with_debug(ctx
, clear
->IntegerShaderProg
);
1812 clear
->IntegerColorLocation
=
1813 _mesa_GetUniformLocationARB(clear
->IntegerShaderProg
, "color");
1818 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1821 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1823 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1824 GLbitfield metaSave
;
1825 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1826 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1827 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1828 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1829 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1830 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1831 const float z
= -invert_z(ctx
->Depth
.Clear
);
1836 metaSave
= (MESA_META_ALPHA_TEST
|
1838 MESA_META_DEPTH_TEST
|
1839 MESA_META_RASTERIZATION
|
1841 MESA_META_STENCIL_TEST
|
1843 MESA_META_VIEWPORT
|
1845 MESA_META_CLAMP_FRAGMENT_COLOR
);
1847 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1848 /* We'll use colormask to disable color writes. Otherwise,
1849 * respect color mask
1851 metaSave
|= MESA_META_COLOR_MASK
;
1854 _mesa_meta_begin(ctx
, metaSave
);
1856 meta_glsl_clear_init(ctx
, clear
);
1858 if (fb
->_IntegerColor
) {
1859 _mesa_UseProgramObjectARB(clear
->IntegerShaderProg
);
1860 _mesa_Uniform4ivARB(clear
->IntegerColorLocation
, 1,
1861 ctx
->Color
.ClearColor
.i
);
1863 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1864 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1865 ctx
->Color
.ClearColor
.f
);
1868 _mesa_BindVertexArray(clear
->ArrayObj
);
1869 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1871 /* GL_COLOR_BUFFER_BIT */
1872 if (buffers
& BUFFER_BITS_COLOR
) {
1873 /* leave colormask, glDrawBuffer state as-is */
1875 /* Clears never have the color clamped. */
1876 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1879 ASSERT(metaSave
& MESA_META_COLOR_MASK
);
1880 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1883 /* GL_DEPTH_BUFFER_BIT */
1884 if (buffers
& BUFFER_BIT_DEPTH
) {
1885 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1886 _mesa_DepthFunc(GL_ALWAYS
);
1887 _mesa_DepthMask(GL_TRUE
);
1890 assert(!ctx
->Depth
.Test
);
1893 /* GL_STENCIL_BUFFER_BIT */
1894 if (buffers
& BUFFER_BIT_STENCIL
) {
1895 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1896 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1897 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1898 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1899 ctx
->Stencil
.Clear
& stencilMax
,
1900 ctx
->Stencil
.WriteMask
[0]);
1903 assert(!ctx
->Stencil
.Enabled
);
1906 /* vertex positions */
1920 /* upload new vertex data */
1921 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1922 GL_DYNAMIC_DRAW_ARB
);
1925 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1927 _mesa_meta_end(ctx
);
1931 * Meta implementation of ctx->Driver.CopyPixels() in terms
1932 * of texture mapping and polygon rendering and GLSL shaders.
1935 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1936 GLsizei width
, GLsizei height
,
1937 GLint dstX
, GLint dstY
, GLenum type
)
1939 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1940 struct temp_texture
*tex
= get_temp_texture(ctx
);
1942 GLfloat x
, y
, z
, s
, t
;
1944 struct vertex verts
[4];
1946 GLenum intFormat
= GL_RGBA
;
1948 if (type
!= GL_COLOR
||
1949 ctx
->_ImageTransferState
||
1951 width
> tex
->MaxSize
||
1952 height
> tex
->MaxSize
) {
1953 /* XXX avoid this fallback */
1954 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1958 /* Most GL state applies to glCopyPixels, but a there's a few things
1959 * we need to override:
1961 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
1964 MESA_META_TRANSFORM
|
1967 MESA_META_VIEWPORT
));
1969 if (copypix
->ArrayObj
== 0) {
1970 /* one-time setup */
1972 /* create vertex array object */
1973 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1974 _mesa_BindVertexArray(copypix
->ArrayObj
);
1976 /* create vertex array buffer */
1977 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1978 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1979 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1980 NULL
, GL_DYNAMIC_DRAW_ARB
);
1982 /* setup vertex arrays */
1983 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1984 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1985 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1986 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1989 _mesa_BindVertexArray(copypix
->ArrayObj
);
1990 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1993 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1995 /* vertex positions, texcoords (after texture allocation!) */
1997 const GLfloat dstX0
= (GLfloat
) dstX
;
1998 const GLfloat dstY0
= (GLfloat
) dstY
;
1999 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
2000 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
2001 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2011 verts
[1].s
= tex
->Sright
;
2016 verts
[2].s
= tex
->Sright
;
2017 verts
[2].t
= tex
->Ttop
;
2022 verts
[3].t
= tex
->Ttop
;
2024 /* upload new vertex data */
2025 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2028 /* Alloc/setup texture */
2029 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
2030 GL_RGBA
, GL_NEAREST
);
2032 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2034 /* draw textured quad */
2035 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2037 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2039 _mesa_meta_end(ctx
);
2045 * When the glDrawPixels() image size is greater than the max rectangle
2046 * texture size we use this function to break the glDrawPixels() image
2047 * into tiles which fit into the max texture size.
2050 tiled_draw_pixels(struct gl_context
*ctx
,
2052 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2053 GLenum format
, GLenum type
,
2054 const struct gl_pixelstore_attrib
*unpack
,
2055 const GLvoid
*pixels
)
2057 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
2060 if (tileUnpack
.RowLength
== 0)
2061 tileUnpack
.RowLength
= width
;
2063 for (i
= 0; i
< width
; i
+= tileSize
) {
2064 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
2065 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
2067 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
2069 for (j
= 0; j
< height
; j
+= tileSize
) {
2070 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
2071 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
2073 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
2075 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
2076 format
, type
, &tileUnpack
, pixels
);
2083 * One-time init for drawing stencil pixels.
2086 init_draw_stencil_pixels(struct gl_context
*ctx
)
2088 /* This program is run eight times, once for each stencil bit.
2089 * The stencil values to draw are found in an 8-bit alpha texture.
2090 * We read the texture/stencil value and test if bit 'b' is set.
2091 * If the bit is not set, use KIL to kill the fragment.
2092 * Finally, we use the stencil test to update the stencil buffer.
2094 * The basic algorithm for checking if a bit is set is:
2095 * if (is_odd(value / (1 << bit)))
2096 * result is one (or non-zero).
2099 * The program parameter contains three values:
2100 * parm.x = 255 / (1 << bit)
2104 static const char *program
=
2106 "PARAM parm = program.local[0]; \n"
2108 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2109 "# t = t * 255 / bit \n"
2110 "MUL t.x, t.a, parm.x; \n"
2113 "SUB t.x, t.x, t.y; \n"
2115 "MUL t.x, t.x, parm.y; \n"
2116 "# t = fract(t.x) \n"
2117 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2118 "# t.x = (t.x == 0 ? 1 : 0) \n"
2119 "SGE t.x, -t.x, parm.z; \n"
2121 "# for debug only \n"
2122 "#MOV result.color, t.x; \n"
2124 char program2
[1000];
2125 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2126 struct temp_texture
*tex
= get_temp_texture(ctx
);
2127 const char *texTarget
;
2129 assert(drawpix
->StencilFP
== 0);
2131 /* replace %s with "RECT" or "2D" */
2132 assert(strlen(program
) + 4 < sizeof(program2
));
2133 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2137 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2139 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
2140 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2141 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2142 strlen(program2
), (const GLubyte
*) program2
);
2147 * One-time init for drawing depth pixels.
2150 init_draw_depth_pixels(struct gl_context
*ctx
)
2152 static const char *program
=
2154 "PARAM color = program.local[0]; \n"
2155 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2156 "MOV result.color, color; \n"
2159 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2160 struct temp_texture
*tex
= get_temp_texture(ctx
);
2161 const char *texTarget
;
2163 assert(drawpix
->DepthFP
== 0);
2165 /* replace %s with "RECT" or "2D" */
2166 assert(strlen(program
) + 4 < sizeof(program2
));
2167 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
2171 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
2173 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
2174 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2175 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
2176 strlen(program2
), (const GLubyte
*) program2
);
2181 * Meta implementation of ctx->Driver.DrawPixels() in terms
2182 * of texture mapping and polygon rendering.
2185 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2186 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2187 GLenum format
, GLenum type
,
2188 const struct gl_pixelstore_attrib
*unpack
,
2189 const GLvoid
*pixels
)
2191 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2192 struct temp_texture
*tex
= get_temp_texture(ctx
);
2193 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2194 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2196 GLfloat x
, y
, z
, s
, t
;
2198 struct vertex verts
[4];
2199 GLenum texIntFormat
;
2200 GLboolean fallback
, newTex
;
2201 GLbitfield metaExtraSave
= 0x0;
2205 * Determine if we can do the glDrawPixels with texture mapping.
2207 fallback
= GL_FALSE
;
2208 if (ctx
->_ImageTransferState
||
2213 if (_mesa_is_color_format(format
)) {
2214 /* use more compact format when possible */
2215 /* XXX disable special case for GL_LUMINANCE for now to work around
2216 * apparent i965 driver bug (see bug #23670).
2218 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2219 texIntFormat
= format
;
2221 texIntFormat
= GL_RGBA
;
2223 /* If we're not supposed to clamp the resulting color, then just
2224 * promote our texture to fully float. We could do better by
2225 * just going for the matching set of channels, in floating
2228 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2229 ctx
->Extensions
.ARB_texture_float
)
2230 texIntFormat
= GL_RGBA32F
;
2232 else if (_mesa_is_stencil_format(format
)) {
2233 if (ctx
->Extensions
.ARB_fragment_program
&&
2234 ctx
->Pixel
.IndexShift
== 0 &&
2235 ctx
->Pixel
.IndexOffset
== 0 &&
2236 type
== GL_UNSIGNED_BYTE
) {
2237 /* We'll store stencil as alpha. This only works for GLubyte
2238 * image data because of how incoming values are mapped to alpha
2241 texIntFormat
= GL_ALPHA
;
2242 metaExtraSave
= (MESA_META_COLOR_MASK
|
2243 MESA_META_DEPTH_TEST
|
2245 MESA_META_STENCIL_TEST
);
2251 else if (_mesa_is_depth_format(format
)) {
2252 if (ctx
->Extensions
.ARB_depth_texture
&&
2253 ctx
->Extensions
.ARB_fragment_program
) {
2254 texIntFormat
= GL_DEPTH_COMPONENT
;
2255 metaExtraSave
= (MESA_META_SHADER
);
2266 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2267 format
, type
, unpack
, pixels
);
2272 * Check image size against max texture size, draw as tiles if needed.
2274 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2275 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2276 format
, type
, unpack
, pixels
);
2280 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2281 * but a there's a few things we need to override:
2283 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
2286 MESA_META_TRANSFORM
|
2289 MESA_META_VIEWPORT
|
2290 MESA_META_CLAMP_FRAGMENT_COLOR
|
2293 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2295 /* vertex positions, texcoords (after texture allocation!) */
2297 const GLfloat x0
= (GLfloat
) x
;
2298 const GLfloat y0
= (GLfloat
) y
;
2299 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2300 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2301 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2311 verts
[1].s
= tex
->Sright
;
2316 verts
[2].s
= tex
->Sright
;
2317 verts
[2].t
= tex
->Ttop
;
2322 verts
[3].t
= tex
->Ttop
;
2325 if (drawpix
->ArrayObj
== 0) {
2326 /* one-time setup: create vertex array object */
2327 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2329 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2331 /* create vertex array buffer */
2332 _mesa_GenBuffersARB(1, &vbo
);
2333 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2334 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2335 verts
, GL_DYNAMIC_DRAW_ARB
);
2337 /* setup vertex arrays */
2338 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2339 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2340 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2341 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2343 /* set given unpack params */
2344 ctx
->Unpack
= *unpack
;
2346 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2348 if (_mesa_is_stencil_format(format
)) {
2349 /* Drawing stencil */
2352 if (!drawpix
->StencilFP
)
2353 init_draw_stencil_pixels(ctx
);
2355 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2356 GL_ALPHA
, type
, pixels
);
2358 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2360 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2362 /* set all stencil bits to 0 */
2363 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2364 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2365 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2367 /* set stencil bits to 1 where needed */
2368 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2370 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2371 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2373 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2374 const GLuint mask
= 1 << bit
;
2375 if (mask
& origStencilMask
) {
2376 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2377 _mesa_StencilMask(mask
);
2379 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2380 255.0 / mask
, 0.5, 0.0, 0.0);
2382 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2386 else if (_mesa_is_depth_format(format
)) {
2388 if (!drawpix
->DepthFP
)
2389 init_draw_depth_pixels(ctx
);
2391 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2392 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2394 /* polygon color = current raster color */
2395 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2396 ctx
->Current
.RasterColor
);
2398 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2399 format
, type
, pixels
);
2401 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2405 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2406 format
, type
, pixels
);
2407 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2410 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2412 _mesa_DeleteBuffersARB(1, &vbo
);
2414 /* restore unpack params */
2415 ctx
->Unpack
= unpackSave
;
2417 _mesa_meta_end(ctx
);
2421 alpha_test_raster_color(struct gl_context
*ctx
)
2423 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2424 GLfloat ref
= ctx
->Color
.AlphaRef
;
2426 switch (ctx
->Color
.AlphaFunc
) {
2432 return alpha
== ref
;
2434 return alpha
<= ref
;
2438 return alpha
!= ref
;
2440 return alpha
>= ref
;
2450 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2451 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2452 * tracker would improve performance a lot.
2455 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2456 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2457 const struct gl_pixelstore_attrib
*unpack
,
2458 const GLubyte
*bitmap1
)
2460 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2461 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2462 const GLenum texIntFormat
= GL_ALPHA
;
2463 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2466 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2468 struct vertex verts
[4];
2473 * Check if swrast fallback is needed.
2475 if (ctx
->_ImageTransferState
||
2476 ctx
->FragmentProgram
._Enabled
||
2478 ctx
->Texture
._EnabledUnits
||
2479 width
> tex
->MaxSize
||
2480 height
> tex
->MaxSize
) {
2481 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2485 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2488 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2489 * but a there's a few things we need to override:
2491 _mesa_meta_begin(ctx
, (MESA_META_ALPHA_TEST
|
2492 MESA_META_PIXEL_STORE
|
2493 MESA_META_RASTERIZATION
|
2496 MESA_META_TRANSFORM
|
2499 MESA_META_VIEWPORT
));
2501 if (bitmap
->ArrayObj
== 0) {
2502 /* one-time setup */
2504 /* create vertex array object */
2505 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2506 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2508 /* create vertex array buffer */
2509 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2510 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2511 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2512 NULL
, GL_DYNAMIC_DRAW_ARB
);
2514 /* setup vertex arrays */
2515 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2516 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2517 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2518 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2519 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2520 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2523 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2524 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2527 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2529 /* vertex positions, texcoords, colors (after texture allocation!) */
2531 const GLfloat x0
= (GLfloat
) x
;
2532 const GLfloat y0
= (GLfloat
) y
;
2533 const GLfloat x1
= (GLfloat
) (x
+ width
);
2534 const GLfloat y1
= (GLfloat
) (y
+ height
);
2535 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2546 verts
[1].s
= tex
->Sright
;
2551 verts
[2].s
= tex
->Sright
;
2552 verts
[2].t
= tex
->Ttop
;
2557 verts
[3].t
= tex
->Ttop
;
2559 for (i
= 0; i
< 4; i
++) {
2560 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2561 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2562 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2563 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2566 /* upload new vertex data */
2567 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2570 /* choose different foreground/background alpha values */
2571 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2572 bg
= (fg
> 127 ? 0 : 255);
2574 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2576 _mesa_meta_end(ctx
);
2580 bitmap8
= (GLubyte
*) malloc(width
* height
);
2582 memset(bitmap8
, bg
, width
* height
);
2583 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2584 bitmap8
, width
, fg
);
2586 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2588 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2589 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2591 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2592 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2594 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2596 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2601 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2603 _mesa_meta_end(ctx
);
2608 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2609 * software fallback. The fallback path will require that the texture
2610 * images are mapped.
2611 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2614 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2615 struct gl_texture_object
*texObj
)
2617 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2618 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2619 struct gl_texture_image
*baseImage
;
2623 /* check for fallbacks */
2624 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2625 target
== GL_TEXTURE_3D
||
2626 target
== GL_TEXTURE_1D_ARRAY
||
2627 target
== GL_TEXTURE_2D_ARRAY
) {
2631 srcLevel
= texObj
->BaseLevel
;
2632 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2633 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2637 if (_mesa_get_format_color_encoding(baseImage
->TexFormat
) == GL_SRGB
&&
2638 !ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2639 /* The texture format is sRGB but we can't turn off sRGB->linear
2640 * texture sample conversion. So we won't be able to generate the
2641 * right colors when rendering. Need to use a fallback.
2647 * Test that we can actually render in the texture's format.
2650 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2651 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2653 if (target
== GL_TEXTURE_1D
) {
2654 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2655 GL_COLOR_ATTACHMENT0_EXT
,
2656 target
, texObj
->Name
, srcLevel
);
2659 /* other work is needed to enable 3D mipmap generation */
2660 else if (target
== GL_TEXTURE_3D
) {
2662 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2663 GL_COLOR_ATTACHMENT0_EXT
,
2664 target
, texObj
->Name
, srcLevel
, zoffset
);
2669 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2670 GL_COLOR_ATTACHMENT0_EXT
,
2671 target
, texObj
->Name
, srcLevel
);
2674 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2676 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2678 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2687 * Compute the texture coordinates for the four vertices of a quad for
2688 * drawing a 2D texture image or slice of a cube/3D texture.
2689 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2690 * \param slice slice of a 1D/2D array texture or 3D texture
2691 * \param width width of the texture image
2692 * \param height height of the texture image
2693 * \param coords0/1/2/3 returns the computed texcoords
2696 setup_texture_coords(GLenum faceTarget
,
2705 static const GLfloat st
[4][2] = {
2706 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2711 switch (faceTarget
) {
2715 case GL_TEXTURE_2D_ARRAY
:
2716 if (faceTarget
== GL_TEXTURE_3D
)
2718 else if (faceTarget
== GL_TEXTURE_2D_ARRAY
)
2722 coords0
[0] = 0.0F
; /* s */
2723 coords0
[1] = 0.0F
; /* t */
2724 coords0
[2] = r
; /* r */
2735 case GL_TEXTURE_RECTANGLE_ARB
:
2736 coords0
[0] = 0.0F
; /* s */
2737 coords0
[1] = 0.0F
; /* t */
2738 coords0
[2] = 0.0F
; /* r */
2743 coords2
[1] = height
;
2746 coords3
[1] = height
;
2749 case GL_TEXTURE_1D_ARRAY
:
2750 coords0
[0] = 0.0F
; /* s */
2751 coords0
[1] = slice
; /* t */
2752 coords0
[2] = 0.0F
; /* r */
2764 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2765 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2766 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2767 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2768 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2769 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2770 /* loop over quad verts */
2771 for (i
= 0; i
< 4; i
++) {
2772 /* Compute sc = +/-scale and tc = +/-scale.
2773 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2774 * though that can still sometimes happen with this scale factor...
2776 const GLfloat scale
= 0.9999f
;
2777 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2778 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2798 switch (faceTarget
) {
2799 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2809 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2814 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2819 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2824 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2835 assert(0 && "unexpected target in meta setup_texture_coords()");
2841 * Called via ctx->Driver.GenerateMipmap()
2842 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2846 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2847 struct gl_texture_object
*texObj
)
2849 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2851 GLfloat x
, y
, tex
[3];
2853 struct vertex verts
[4];
2854 const GLuint baseLevel
= texObj
->BaseLevel
;
2855 const GLuint maxLevel
= texObj
->MaxLevel
;
2856 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2857 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2858 const GLint maxLevelSave
= texObj
->MaxLevel
;
2859 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2860 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2861 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2862 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2863 const GLenum srgbDecodeSave
= texObj
->Sampler
.sRGBDecode
;
2864 const GLenum srgbBufferSave
= ctx
->Color
.sRGBEnabled
;
2865 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2866 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2869 const GLuint border
= 0;
2870 const GLint slice
= 0;
2872 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2873 _mesa_generate_mipmap(ctx
, target
, texObj
);
2877 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2878 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2879 faceTarget
= target
;
2880 target
= GL_TEXTURE_CUBE_MAP
;
2883 faceTarget
= target
;
2886 _mesa_meta_begin(ctx
, MESA_META_ALL
);
2888 if (original_active_unit
!= 0)
2889 _mesa_BindTexture(target
, texObj
->Name
);
2891 if (mipmap
->ArrayObj
== 0) {
2892 /* one-time setup */
2894 /* create vertex array object */
2895 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2896 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2898 /* create vertex array buffer */
2899 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2900 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2901 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2902 NULL
, GL_DYNAMIC_DRAW_ARB
);
2904 /* setup vertex arrays */
2905 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2906 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
2907 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2908 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2911 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2912 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2916 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2918 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2920 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2921 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2922 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2923 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2924 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2925 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2927 /* We don't want to encode or decode sRGB values; treat them as linear */
2928 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
2929 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
2930 GL_SKIP_DECODE_EXT
);
2932 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
2933 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
2936 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2938 /* setup texcoords (XXX what about border?) */
2939 setup_texture_coords(faceTarget
,
2941 0, 0, /* width, height never used here */
2947 /* setup vertex positions */
2957 /* upload new vertex data */
2958 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2960 /* setup projection matrix */
2961 _mesa_MatrixMode(GL_PROJECTION
);
2962 _mesa_LoadIdentity();
2963 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2965 /* texture is already locked, unlock now */
2966 _mesa_unlock_texture(ctx
, texObj
);
2968 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2969 const struct gl_texture_image
*srcImage
;
2970 const GLuint srcLevel
= dstLevel
- 1;
2971 GLsizei srcWidth
, srcHeight
, srcDepth
;
2972 GLsizei dstWidth
, dstHeight
, dstDepth
;
2975 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2976 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2978 /* src size w/out border */
2979 srcWidth
= srcImage
->Width
- 2 * border
;
2980 srcHeight
= srcImage
->Height
- 2 * border
;
2981 srcDepth
= srcImage
->Depth
- 2 * border
;
2983 /* new dst size w/ border */
2984 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2985 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2986 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2988 if (dstWidth
== srcImage
->Width
&&
2989 dstHeight
== srcImage
->Height
&&
2990 dstDepth
== srcImage
->Depth
) {
2995 /* Allocate storage for the destination mipmap image(s) */
2997 /* Set MaxLevel large enough to hold the new level when we allocate it */
2998 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
3000 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, dstLevel
,
3001 dstWidth
, dstHeight
, dstDepth
,
3003 srcImage
->InternalFormat
,
3004 srcImage
->TexFormat
)) {
3005 /* All done. We either ran out of memory or we would go beyond the
3006 * last valid level of an immutable texture if we continued.
3011 /* limit minification to src level */
3012 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
3014 /* Set to draw into the current dstLevel */
3015 if (target
== GL_TEXTURE_1D
) {
3016 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
3017 GL_COLOR_ATTACHMENT0_EXT
,
3022 else if (target
== GL_TEXTURE_3D
) {
3023 GLint zoffset
= 0; /* XXX unfinished */
3024 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
3025 GL_COLOR_ATTACHMENT0_EXT
,
3032 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
3033 GL_COLOR_ATTACHMENT0_EXT
,
3039 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
3042 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
3043 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
3044 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
3045 "_mesa_meta_GenerateMipmap()");
3049 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
3050 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
3052 /* setup viewport */
3053 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
3055 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3058 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3059 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3062 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&& srgbBufferSave
) {
3063 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_TRUE
);
3066 _mesa_lock_texture(ctx
, texObj
); /* relock */
3068 _mesa_meta_end(ctx
);
3070 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3071 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3072 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3073 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
3074 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3075 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3076 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
3078 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
3083 * Determine the GL data type to use for the temporary image read with
3084 * ReadPixels() and passed to Tex[Sub]Image().
3087 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
3089 switch (baseFormat
) {
3096 case GL_LUMINANCE_ALPHA
:
3098 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
3099 return GL_UNSIGNED_BYTE
;
3100 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 16)
3101 return GL_UNSIGNED_SHORT
;
3104 case GL_DEPTH_COMPONENT
:
3105 return GL_UNSIGNED_INT
;
3106 case GL_DEPTH_STENCIL
:
3107 return GL_UNSIGNED_INT_24_8
;
3109 _mesa_problem(ctx
, "Unexpected format %d in get_temp_image_type()",
3117 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3118 * Have to be careful with locking and meta state for pixel transfer.
3121 copy_tex_sub_image(struct gl_context
*ctx
,
3123 struct gl_texture_image
*texImage
,
3124 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3125 struct gl_renderbuffer
*rb
,
3127 GLsizei width
, GLsizei height
)
3129 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3130 const GLenum target
= texObj
->Target
;
3131 GLenum format
, type
;
3135 /* Choose format/type for temporary image buffer */
3136 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
3137 if (format
== GL_LUMINANCE
||
3138 format
== GL_LUMINANCE_ALPHA
||
3139 format
== GL_INTENSITY
) {
3140 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3141 * temp image buffer because glReadPixels will do L=R+G+B which is
3142 * not what we want (should be L=R).
3147 if (_mesa_is_format_integer_color(texImage
->TexFormat
)) {
3148 _mesa_problem(ctx
, "unsupported integer color copyteximage");
3152 type
= get_temp_image_type(ctx
, format
);
3153 bpp
= _mesa_bytes_per_pixel(format
, type
);
3155 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
3160 * Alloc image buffer (XXX could use a PBO)
3162 buf
= malloc(width
* height
* bpp
);
3164 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
3168 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
3171 * Read image from framebuffer (disable pixel transfer ops)
3173 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
| MESA_META_PIXEL_TRANSFER
);
3174 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
3175 format
, type
, &ctx
->Pack
, buf
);
3176 _mesa_meta_end(ctx
);
3178 _mesa_update_state(ctx
); /* to update pixel transfer state */
3181 * Store texture data (with pixel transfer ops)
3183 _mesa_meta_begin(ctx
, MESA_META_PIXEL_STORE
);
3184 if (target
== GL_TEXTURE_1D
) {
3185 ctx
->Driver
.TexSubImage1D(ctx
, texImage
,
3187 format
, type
, buf
, &ctx
->Unpack
);
3189 else if (target
== GL_TEXTURE_3D
) {
3190 ctx
->Driver
.TexSubImage3D(ctx
, texImage
,
3191 xoffset
, yoffset
, zoffset
, width
, height
, 1,
3192 format
, type
, buf
, &ctx
->Unpack
);
3195 ctx
->Driver
.TexSubImage2D(ctx
, texImage
,
3196 xoffset
, yoffset
, width
, height
,
3197 format
, type
, buf
, &ctx
->Unpack
);
3199 _mesa_meta_end(ctx
);
3201 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
3208 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
,
3209 struct gl_texture_image
*texImage
,
3211 struct gl_renderbuffer
*rb
,
3212 GLint x
, GLint y
, GLsizei width
)
3214 copy_tex_sub_image(ctx
, 1, texImage
, xoffset
, 0, 0,
3215 rb
, x
, y
, width
, 1);
3220 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
,
3221 struct gl_texture_image
*texImage
,
3222 GLint xoffset
, GLint yoffset
,
3223 struct gl_renderbuffer
*rb
,
3225 GLsizei width
, GLsizei height
)
3227 copy_tex_sub_image(ctx
, 2, texImage
, xoffset
, yoffset
, 0,
3228 rb
, x
, y
, width
, height
);
3233 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
,
3234 struct gl_texture_image
*texImage
,
3235 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3236 struct gl_renderbuffer
*rb
,
3238 GLsizei width
, GLsizei height
)
3240 copy_tex_sub_image(ctx
, 3, texImage
, xoffset
, yoffset
, zoffset
,
3241 rb
, x
, y
, width
, height
);
3246 * Decompress a texture image by drawing a quad with the compressed
3247 * texture and reading the pixels out of the color buffer.
3248 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3249 * \param destFormat format, ala glReadPixels
3250 * \param destType type, ala glReadPixels
3251 * \param dest destination buffer
3252 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3255 decompress_texture_image(struct gl_context
*ctx
,
3256 struct gl_texture_image
*texImage
,
3258 GLenum destFormat
, GLenum destType
,
3261 struct decompress_state
*decompress
= &ctx
->Meta
->Decompress
;
3262 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3263 const GLint width
= texImage
->Width
;
3264 const GLint height
= texImage
->Height
;
3265 const GLenum target
= texObj
->Target
;
3268 GLfloat x
, y
, tex
[3];
3270 struct vertex verts
[4];
3271 GLuint fboDrawSave
, fboReadSave
;
3275 assert(target
== GL_TEXTURE_3D
||
3276 target
== GL_TEXTURE_2D_ARRAY
);
3279 if (target
== GL_TEXTURE_CUBE_MAP
) {
3280 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ texImage
->Face
;
3283 faceTarget
= target
;
3286 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3287 fboDrawSave
= ctx
->DrawBuffer
->Name
;
3288 fboReadSave
= ctx
->ReadBuffer
->Name
;
3289 rbSave
= ctx
->CurrentRenderbuffer
? ctx
->CurrentRenderbuffer
->Name
: 0;
3291 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_PIXEL_STORE
);
3293 /* Create/bind FBO/renderbuffer */
3294 if (decompress
->FBO
== 0) {
3295 _mesa_GenFramebuffersEXT(1, &decompress
->FBO
);
3296 _mesa_GenRenderbuffersEXT(1, &decompress
->RBO
);
3297 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3298 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3299 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
3300 GL_COLOR_ATTACHMENT0_EXT
,
3301 GL_RENDERBUFFER_EXT
,
3305 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, decompress
->FBO
);
3308 /* alloc dest surface */
3309 if (width
> decompress
->Width
|| height
> decompress
->Height
) {
3310 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, decompress
->RBO
);
3311 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGBA
,
3313 decompress
->Width
= width
;
3314 decompress
->Height
= height
;
3317 /* setup VBO data */
3318 if (decompress
->ArrayObj
== 0) {
3319 /* create vertex array object */
3320 _mesa_GenVertexArrays(1, &decompress
->ArrayObj
);
3321 _mesa_BindVertexArray(decompress
->ArrayObj
);
3323 /* create vertex array buffer */
3324 _mesa_GenBuffersARB(1, &decompress
->VBO
);
3325 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3326 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3327 NULL
, GL_DYNAMIC_DRAW_ARB
);
3329 /* setup vertex arrays */
3330 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3331 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(tex
));
3332 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3333 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3336 _mesa_BindVertexArray(decompress
->ArrayObj
);
3337 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, decompress
->VBO
);
3340 setup_texture_coords(faceTarget
, slice
, width
, height
,
3346 /* setup vertex positions */
3352 verts
[2].y
= height
;
3354 verts
[3].y
= height
;
3356 /* upload new vertex data */
3357 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3359 /* setup texture state */
3360 _mesa_BindTexture(target
, texObj
->Name
);
3361 _mesa_set_enable(ctx
, target
, GL_TRUE
);
3364 /* save texture object state */
3365 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
3366 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
3367 const GLint baseLevelSave
= texObj
->BaseLevel
;
3368 const GLint maxLevelSave
= texObj
->MaxLevel
;
3369 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
3370 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
3371 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
3373 /* restrict sampling to the texture level of interest */
3374 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, texImage
->Level
);
3375 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texImage
->Level
);
3376 /* nearest filtering */
3377 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
3378 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
3380 /* No sRGB decode or encode.*/
3381 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3382 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
3383 GL_SKIP_DECODE_EXT
);
3385 if (ctx
->Extensions
.EXT_framebuffer_sRGB
) {
3386 _mesa_set_enable(ctx
, GL_FRAMEBUFFER_SRGB_EXT
, GL_FALSE
);
3389 /* render quad w/ texture into renderbuffer */
3390 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3392 /* Restore texture object state, the texture binding will
3393 * be restored by _mesa_meta_end().
3395 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
3396 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
3397 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
3398 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
3399 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
3401 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
3402 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
3403 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
3404 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
3408 /* read pixels from renderbuffer */
3410 GLenum baseTexFormat
= texImage
->_BaseFormat
;
3412 /* The pixel transfer state will be set to default values at this point
3413 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3414 * turned off (as required by glGetTexImage) but we need to handle some
3415 * special cases. In particular, single-channel texture values are
3416 * returned as red and two-channel texture values are returned as
3419 if (baseTexFormat
== GL_LUMINANCE
||
3420 baseTexFormat
== GL_LUMINANCE_ALPHA
||
3421 baseTexFormat
== GL_INTENSITY
) {
3422 /* Green and blue must be zero */
3423 _mesa_PixelTransferf(GL_GREEN_SCALE
, 0.0f
);
3424 _mesa_PixelTransferf(GL_BLUE_SCALE
, 0.0f
);
3427 _mesa_ReadPixels(0, 0, width
, height
, destFormat
, destType
, dest
);
3430 /* disable texture unit */
3431 _mesa_set_enable(ctx
, target
, GL_FALSE
);
3433 _mesa_meta_end(ctx
);
3435 /* restore fbo bindings */
3436 if (fboDrawSave
== fboReadSave
) {
3437 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboDrawSave
);
3440 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT
, fboDrawSave
);
3441 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT
, fboReadSave
);
3443 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT
, rbSave
);
3448 * This is just a wrapper around _mesa_get_tex_image() and
3449 * decompress_texture_image(). Meta functions should not be directly called
3453 _mesa_meta_GetTexImage(struct gl_context
*ctx
,
3454 GLenum format
, GLenum type
, GLvoid
*pixels
,
3455 struct gl_texture_image
*texImage
)
3457 /* We can only use the decompress-with-blit method here if the texels are
3458 * unsigned, normalized values. We could handle signed and unnormalized
3459 * with floating point renderbuffers...
3461 if (_mesa_is_format_compressed(texImage
->TexFormat
) &&
3462 _mesa_get_format_datatype(texImage
->TexFormat
)
3463 == GL_UNSIGNED_NORMALIZED
) {
3464 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3465 const GLuint slice
= 0; /* only 2D compressed textures for now */
3466 /* Need to unlock the texture here to prevent deadlock... */
3467 _mesa_unlock_texture(ctx
, texObj
);
3468 decompress_texture_image(ctx
, texImage
, slice
, format
, type
, pixels
);
3469 /* ... and relock it */
3470 _mesa_lock_texture(ctx
, texObj
);
3473 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
3479 * Meta implementation of ctx->Driver.DrawTex() in terms
3480 * of polygon rendering.
3483 _mesa_meta_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
3484 GLfloat width
, GLfloat height
)
3486 #if FEATURE_OES_draw_texture
3487 struct drawtex_state
*drawtex
= &ctx
->Meta
->DrawTex
;
3489 GLfloat x
, y
, z
, st
[MAX_TEXTURE_UNITS
][2];
3491 struct vertex verts
[4];
3494 _mesa_meta_begin(ctx
, (MESA_META_RASTERIZATION
|
3496 MESA_META_TRANSFORM
|
3498 MESA_META_VIEWPORT
));
3500 if (drawtex
->ArrayObj
== 0) {
3501 /* one-time setup */
3502 GLint active_texture
;
3504 /* create vertex array object */
3505 _mesa_GenVertexArrays(1, &drawtex
->ArrayObj
);
3506 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3508 /* create vertex array buffer */
3509 _mesa_GenBuffersARB(1, &drawtex
->VBO
);
3510 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3511 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
3512 NULL
, GL_DYNAMIC_DRAW_ARB
);
3514 /* client active texture is not part of the array object */
3515 active_texture
= ctx
->Array
.ActiveTexture
;
3517 /* setup vertex arrays */
3518 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
3519 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
3520 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3521 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ i
);
3522 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(st
[i
]));
3523 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
3526 /* restore client active texture */
3527 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ active_texture
);
3530 _mesa_BindVertexArray(drawtex
->ArrayObj
);
3531 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawtex
->VBO
);
3534 /* vertex positions, texcoords */
3536 const GLfloat x1
= x
+ width
;
3537 const GLfloat y1
= y
+ height
;
3539 z
= CLAMP(z
, 0.0, 1.0);
3558 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
3559 const struct gl_texture_object
*texObj
;
3560 const struct gl_texture_image
*texImage
;
3561 GLfloat s
, t
, s1
, t1
;
3564 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
3566 for (j
= 0; j
< 4; j
++) {
3567 verts
[j
].st
[i
][0] = 0.0f
;
3568 verts
[j
].st
[i
][1] = 0.0f
;
3573 texObj
= ctx
->Texture
.Unit
[i
]._Current
;
3574 texImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3575 tw
= texImage
->Width2
;
3576 th
= texImage
->Height2
;
3578 s
= (GLfloat
) texObj
->CropRect
[0] / tw
;
3579 t
= (GLfloat
) texObj
->CropRect
[1] / th
;
3580 s1
= (GLfloat
) (texObj
->CropRect
[0] + texObj
->CropRect
[2]) / tw
;
3581 t1
= (GLfloat
) (texObj
->CropRect
[1] + texObj
->CropRect
[3]) / th
;
3583 verts
[0].st
[i
][0] = s
;
3584 verts
[0].st
[i
][1] = t
;
3586 verts
[1].st
[i
][0] = s1
;
3587 verts
[1].st
[i
][1] = t
;
3589 verts
[2].st
[i
][0] = s1
;
3590 verts
[2].st
[i
][1] = t1
;
3592 verts
[3].st
[i
][0] = s
;
3593 verts
[3].st
[i
][1] = t1
;
3596 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
3599 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
3601 _mesa_meta_end(ctx
);
3602 #endif /* FEATURE_OES_draw_texture */