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/formats.h"
48 #include "main/image.h"
49 #include "main/macros.h"
50 #include "main/matrix.h"
51 #include "main/mipmap.h"
53 #include "main/polygon.h"
54 #include "main/readpix.h"
55 #include "main/scissor.h"
56 #include "main/shaderapi.h"
57 #include "main/shaderobj.h"
58 #include "main/state.h"
59 #include "main/stencil.h"
60 #include "main/texobj.h"
61 #include "main/texenv.h"
62 #include "main/teximage.h"
63 #include "main/texparam.h"
64 #include "main/texstate.h"
65 #include "main/uniforms.h"
66 #include "main/varray.h"
67 #include "main/viewport.h"
68 #include "program/program.h"
69 #include "swrast/swrast.h"
70 #include "drivers/common/meta.h"
73 /** Return offset in bytes of the field within a vertex struct */
74 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
78 * Flags passed to _mesa_meta_begin().
82 #define META_ALPHA_TEST 0x1
83 #define META_BLEND 0x2 /**< includes logicop */
84 #define META_COLOR_MASK 0x4
85 #define META_DEPTH_TEST 0x8
87 #define META_PIXEL_STORE 0x20
88 #define META_PIXEL_TRANSFER 0x40
89 #define META_RASTERIZATION 0x80
90 #define META_SCISSOR 0x100
91 #define META_SHADER 0x200
92 #define META_STENCIL_TEST 0x400
93 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
94 #define META_TEXTURE 0x1000
95 #define META_VERTEX 0x2000
96 #define META_VIEWPORT 0x4000
97 #define META_CLAMP_FRAGMENT_COLOR 0x8000
98 #define META_CLAMP_VERTEX_COLOR 0x10000
99 #define META_CONDITIONAL_RENDER 0x20000
104 * State which we may save/restore across meta ops.
105 * XXX this may be incomplete...
109 GLbitfield SavedState
; /**< bitmask of META_* flags */
111 /** META_ALPHA_TEST */
112 GLboolean AlphaEnabled
;
117 GLbitfield BlendEnabled
;
118 GLboolean ColorLogicOpEnabled
;
120 /** META_COLOR_MASK */
121 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
123 /** META_DEPTH_TEST */
124 struct gl_depthbuffer_attrib Depth
;
129 /** META_PIXEL_STORE */
130 struct gl_pixelstore_attrib Pack
, Unpack
;
132 /** META_PIXEL_TRANSFER */
133 GLfloat RedBias
, RedScale
;
134 GLfloat GreenBias
, GreenScale
;
135 GLfloat BlueBias
, BlueScale
;
136 GLfloat AlphaBias
, AlphaScale
;
137 GLfloat DepthBias
, DepthScale
;
138 GLboolean MapColorFlag
;
140 /** META_RASTERIZATION */
141 GLenum FrontPolygonMode
, BackPolygonMode
;
142 GLboolean PolygonOffset
;
143 GLboolean PolygonSmooth
;
144 GLboolean PolygonStipple
;
145 GLboolean PolygonCull
;
148 struct gl_scissor_attrib Scissor
;
151 GLboolean VertexProgramEnabled
;
152 struct gl_vertex_program
*VertexProgram
;
153 GLboolean FragmentProgramEnabled
;
154 struct gl_fragment_program
*FragmentProgram
;
155 struct gl_shader_program
*VertexShader
;
156 struct gl_shader_program
*GeometryShader
;
157 struct gl_shader_program
*FragmentShader
;
158 struct gl_shader_program
*ActiveShader
;
160 /** META_STENCIL_TEST */
161 struct gl_stencil_attrib Stencil
;
163 /** META_TRANSFORM */
165 GLfloat ModelviewMatrix
[16];
166 GLfloat ProjectionMatrix
[16];
167 GLfloat TextureMatrix
[16];
168 GLbitfield ClipPlanesEnabled
;
172 GLuint ClientActiveUnit
;
173 /** for unit[0] only */
174 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
175 /** mask of TEXTURE_2D_BIT, etc */
176 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
177 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
178 GLuint EnvMode
; /* unit[0] only */
181 struct gl_array_object
*ArrayObj
;
182 struct gl_buffer_object
*ArrayBufferObj
;
185 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
186 GLclampd DepthNear
, DepthFar
;
188 /** META_CLAMP_FRAGMENT_COLOR */
189 GLenum ClampFragmentColor
;
191 /** META_CLAMP_VERTEX_COLOR */
192 GLenum ClampVertexColor
;
194 /** META_CONDITIONAL_RENDER */
195 struct gl_query_object
*CondRenderQuery
;
196 GLenum CondRenderMode
;
198 /** Miscellaneous (always disabled) */
204 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
205 * This is currently shared by all the meta ops. But we could create a
206 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
211 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
212 GLsizei MinSize
; /**< Min texture size to allocate */
213 GLsizei MaxSize
; /**< Max possible texture size */
214 GLboolean NPOT
; /**< Non-power of two size OK? */
215 GLsizei Width
, Height
; /**< Current texture size */
217 GLfloat Sright
, Ttop
; /**< right, top texcoords */
222 * State for glBlitFramebufer()
233 * State for glClear()
245 * State for glCopyPixels()
255 * State for glDrawPixels()
261 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
262 GLuint DepthFP
; /**< Fragment program for drawing depth images */
267 * State for glBitmap()
273 struct temp_texture Tex
; /**< separate texture from other meta ops */
278 * State for _mesa_meta_generate_mipmap()
280 struct gen_mipmap_state
287 #define MAX_META_OPS_DEPTH 2
289 * All per-context meta state.
293 /** Stack of state saved during meta-ops */
294 struct save_state Save
[MAX_META_OPS_DEPTH
];
295 /** Save stack depth */
296 GLuint SaveStackDepth
;
298 struct temp_texture TempTex
;
300 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
301 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
302 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
303 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
304 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
305 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
310 * Initialize meta-ops for a context.
311 * To be called once during context creation.
314 _mesa_meta_init(struct gl_context
*ctx
)
318 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
323 * Free context meta-op state.
324 * To be called once during context destruction.
327 _mesa_meta_free(struct gl_context
*ctx
)
329 /* Note: Any textures, VBOs, etc, that we allocate should get
330 * freed by the normal context destruction code. But this would be
331 * the place to free other meta data someday.
339 * Enter meta state. This is like a light-weight version of glPushAttrib
340 * but it also resets most GL state back to default values.
342 * \param state bitmask of META_* flags indicating which attribute groups
343 * to save and reset to their defaults
346 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
348 struct save_state
*save
;
350 /* hope MAX_META_OPS_DEPTH is large enough */
351 assert(ctx
->Meta
->SaveStackDepth
< MAX_META_OPS_DEPTH
);
353 save
= &ctx
->Meta
->Save
[ctx
->Meta
->SaveStackDepth
++];
354 memset(save
, 0, sizeof(*save
));
355 save
->SavedState
= state
;
357 if (state
& META_ALPHA_TEST
) {
358 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
359 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
360 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
361 if (ctx
->Color
.AlphaEnabled
)
362 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
365 if (state
& META_BLEND
) {
366 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
367 if (ctx
->Color
.BlendEnabled
) {
368 if (ctx
->Extensions
.EXT_draw_buffers2
) {
370 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
371 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
375 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
378 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
379 if (ctx
->Color
.ColorLogicOpEnabled
)
380 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
383 if (state
& META_COLOR_MASK
) {
384 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
385 sizeof(ctx
->Color
.ColorMask
));
386 if (!ctx
->Color
.ColorMask
[0][0] ||
387 !ctx
->Color
.ColorMask
[0][1] ||
388 !ctx
->Color
.ColorMask
[0][2] ||
389 !ctx
->Color
.ColorMask
[0][3])
390 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
393 if (state
& META_DEPTH_TEST
) {
394 save
->Depth
= ctx
->Depth
; /* struct copy */
396 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
399 if (state
& META_FOG
) {
400 save
->Fog
= ctx
->Fog
.Enabled
;
401 if (ctx
->Fog
.Enabled
)
402 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
405 if (state
& META_PIXEL_STORE
) {
406 save
->Pack
= ctx
->Pack
;
407 save
->Unpack
= ctx
->Unpack
;
408 ctx
->Pack
= ctx
->DefaultPacking
;
409 ctx
->Unpack
= ctx
->DefaultPacking
;
412 if (state
& META_PIXEL_TRANSFER
) {
413 save
->RedScale
= ctx
->Pixel
.RedScale
;
414 save
->RedBias
= ctx
->Pixel
.RedBias
;
415 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
416 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
417 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
418 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
419 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
420 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
421 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
422 ctx
->Pixel
.RedScale
= 1.0F
;
423 ctx
->Pixel
.RedBias
= 0.0F
;
424 ctx
->Pixel
.GreenScale
= 1.0F
;
425 ctx
->Pixel
.GreenBias
= 0.0F
;
426 ctx
->Pixel
.BlueScale
= 1.0F
;
427 ctx
->Pixel
.BlueBias
= 0.0F
;
428 ctx
->Pixel
.AlphaScale
= 1.0F
;
429 ctx
->Pixel
.AlphaBias
= 0.0F
;
430 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
432 ctx
->NewState
|=_NEW_PIXEL
;
435 if (state
& META_RASTERIZATION
) {
436 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
437 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
438 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
439 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
440 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
441 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
442 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
443 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
444 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
445 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
446 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
449 if (state
& META_SCISSOR
) {
450 save
->Scissor
= ctx
->Scissor
; /* struct copy */
451 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
454 if (state
& META_SHADER
) {
455 if (ctx
->Extensions
.ARB_vertex_program
) {
456 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
457 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
458 ctx
->VertexProgram
.Current
);
459 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
462 if (ctx
->Extensions
.ARB_fragment_program
) {
463 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
464 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
465 ctx
->FragmentProgram
.Current
);
466 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
469 if (ctx
->Extensions
.ARB_shader_objects
) {
470 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
471 ctx
->Shader
.CurrentVertexProgram
);
472 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
473 ctx
->Shader
.CurrentGeometryProgram
);
474 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
475 ctx
->Shader
.CurrentFragmentProgram
);
476 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
477 ctx
->Shader
.CurrentFragmentProgram
);
479 _mesa_UseProgramObjectARB(0);
483 if (state
& META_STENCIL_TEST
) {
484 save
->Stencil
= ctx
->Stencil
; /* struct copy */
485 if (ctx
->Stencil
.Enabled
)
486 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
487 /* NOTE: other stencil state not reset */
490 if (state
& META_TEXTURE
) {
493 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
494 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
495 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
497 /* Disable all texture units */
498 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
499 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
500 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
501 if (ctx
->Texture
.Unit
[u
].Enabled
||
502 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
503 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
504 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
505 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
506 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
507 if (ctx
->Extensions
.ARB_texture_cube_map
)
508 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
509 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
510 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
511 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
512 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
513 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
517 /* save current texture objects for unit[0] only */
518 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
519 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
520 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
523 /* set defaults for unit[0] */
524 _mesa_ActiveTextureARB(GL_TEXTURE0
);
525 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
526 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
529 if (state
& META_TRANSFORM
) {
530 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
531 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
532 16 * sizeof(GLfloat
));
533 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
534 16 * sizeof(GLfloat
));
535 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
536 16 * sizeof(GLfloat
));
537 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
538 /* set 1:1 vertex:pixel coordinate transform */
539 _mesa_ActiveTextureARB(GL_TEXTURE0
);
540 _mesa_MatrixMode(GL_TEXTURE
);
541 _mesa_LoadIdentity();
542 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
543 _mesa_MatrixMode(GL_MODELVIEW
);
544 _mesa_LoadIdentity();
545 _mesa_MatrixMode(GL_PROJECTION
);
546 _mesa_LoadIdentity();
547 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
548 0.0, ctx
->DrawBuffer
->Height
,
550 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
551 if (ctx
->Transform
.ClipPlanesEnabled
) {
553 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
554 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
559 if (state
& META_VERTEX
) {
560 /* save vertex array object state */
561 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
562 ctx
->Array
.ArrayObj
);
563 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
564 ctx
->Array
.ArrayBufferObj
);
565 /* set some default state? */
568 if (state
& META_VIEWPORT
) {
569 /* save viewport state */
570 save
->ViewportX
= ctx
->Viewport
.X
;
571 save
->ViewportY
= ctx
->Viewport
.Y
;
572 save
->ViewportW
= ctx
->Viewport
.Width
;
573 save
->ViewportH
= ctx
->Viewport
.Height
;
574 /* set viewport to match window size */
575 if (ctx
->Viewport
.X
!= 0 ||
576 ctx
->Viewport
.Y
!= 0 ||
577 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
578 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
579 _mesa_set_viewport(ctx
, 0, 0,
580 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
582 /* save depth range state */
583 save
->DepthNear
= ctx
->Viewport
.Near
;
584 save
->DepthFar
= ctx
->Viewport
.Far
;
585 /* set depth range to default */
586 _mesa_DepthRange(0.0, 1.0);
589 if (state
& META_CLAMP_FRAGMENT_COLOR
) {
590 save
->ClampFragmentColor
= ctx
->Color
.ClampFragmentColor
;
592 /* Generally in here we want to do clamping according to whether
593 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
594 * regardless of the internal implementation of the metaops.
596 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
)
597 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
600 if (state
& META_CLAMP_VERTEX_COLOR
) {
601 save
->ClampVertexColor
= ctx
->Light
.ClampVertexColor
;
603 /* Generally in here we never want vertex color clamping --
604 * result clamping is only dependent on fragment clamping.
606 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, GL_FALSE
);
609 if (state
& META_CONDITIONAL_RENDER
) {
610 save
->CondRenderQuery
= ctx
->Query
.CondRenderQuery
;
611 save
->CondRenderMode
= ctx
->Query
.CondRenderMode
;
613 if (ctx
->Query
.CondRenderQuery
)
614 _mesa_EndConditionalRender();
619 save
->Lighting
= ctx
->Light
.Enabled
;
620 if (ctx
->Light
.Enabled
)
621 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
627 * Leave meta state. This is like a light-weight version of glPopAttrib().
630 _mesa_meta_end(struct gl_context
*ctx
)
632 struct save_state
*save
= &ctx
->Meta
->Save
[--ctx
->Meta
->SaveStackDepth
];
633 const GLbitfield state
= save
->SavedState
;
635 if (state
& META_ALPHA_TEST
) {
636 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
637 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
638 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
641 if (state
& META_BLEND
) {
642 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
643 if (ctx
->Extensions
.EXT_draw_buffers2
) {
645 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
646 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
650 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
653 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
654 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
657 if (state
& META_COLOR_MASK
) {
659 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
660 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
662 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
663 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
666 _mesa_ColorMaskIndexed(i
,
667 save
->ColorMask
[i
][0],
668 save
->ColorMask
[i
][1],
669 save
->ColorMask
[i
][2],
670 save
->ColorMask
[i
][3]);
676 if (state
& META_DEPTH_TEST
) {
677 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
678 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
679 _mesa_DepthFunc(save
->Depth
.Func
);
680 _mesa_DepthMask(save
->Depth
.Mask
);
683 if (state
& META_FOG
) {
684 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
687 if (state
& META_PIXEL_STORE
) {
688 ctx
->Pack
= save
->Pack
;
689 ctx
->Unpack
= save
->Unpack
;
692 if (state
& META_PIXEL_TRANSFER
) {
693 ctx
->Pixel
.RedScale
= save
->RedScale
;
694 ctx
->Pixel
.RedBias
= save
->RedBias
;
695 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
696 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
697 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
698 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
699 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
700 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
701 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
703 ctx
->NewState
|=_NEW_PIXEL
;
706 if (state
& META_RASTERIZATION
) {
707 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
708 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
709 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
710 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
711 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
712 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
715 if (state
& META_SCISSOR
) {
716 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
717 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
718 save
->Scissor
.Width
, save
->Scissor
.Height
);
721 if (state
& META_SHADER
) {
722 if (ctx
->Extensions
.ARB_vertex_program
) {
723 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
724 save
->VertexProgramEnabled
);
725 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
726 save
->VertexProgram
);
727 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
730 if (ctx
->Extensions
.ARB_fragment_program
) {
731 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
732 save
->FragmentProgramEnabled
);
733 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
734 save
->FragmentProgram
);
735 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
738 if (ctx
->Extensions
.ARB_vertex_shader
)
739 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
741 if (ctx
->Extensions
.ARB_geometry_shader4
)
742 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
743 save
->GeometryShader
);
745 if (ctx
->Extensions
.ARB_fragment_shader
)
746 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
747 save
->FragmentShader
);
749 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
753 if (state
& META_STENCIL_TEST
) {
754 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
756 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
757 _mesa_ClearStencil(stencil
->Clear
);
758 if (ctx
->Extensions
.EXT_stencil_two_side
) {
759 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
760 stencil
->TestTwoSide
);
761 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
762 ? GL_BACK
: GL_FRONT
);
765 _mesa_StencilFuncSeparate(GL_FRONT
,
766 stencil
->Function
[0],
768 stencil
->ValueMask
[0]);
769 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
770 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
771 stencil
->ZFailFunc
[0],
772 stencil
->ZPassFunc
[0]);
774 _mesa_StencilFuncSeparate(GL_BACK
,
775 stencil
->Function
[1],
777 stencil
->ValueMask
[1]);
778 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
779 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
780 stencil
->ZFailFunc
[1],
781 stencil
->ZPassFunc
[1]);
784 if (state
& META_TEXTURE
) {
787 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
789 /* restore texenv for unit[0] */
790 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
792 /* restore texture objects for unit[0] only */
793 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
794 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
795 save
->CurrentTexture
[tgt
]);
796 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
799 /* Re-enable textures, texgen */
800 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
801 if (save
->TexEnabled
[u
]) {
802 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
804 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
805 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
806 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
807 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
808 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
809 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
810 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
811 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
812 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
813 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
816 if (save
->TexGenEnabled
[u
]) {
817 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
819 if (save
->TexGenEnabled
[u
] & S_BIT
)
820 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
821 if (save
->TexGenEnabled
[u
] & T_BIT
)
822 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
823 if (save
->TexGenEnabled
[u
] & R_BIT
)
824 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
825 if (save
->TexGenEnabled
[u
] & Q_BIT
)
826 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
830 /* restore current unit state */
831 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
832 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
835 if (state
& META_TRANSFORM
) {
836 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
837 _mesa_ActiveTextureARB(GL_TEXTURE0
);
838 _mesa_MatrixMode(GL_TEXTURE
);
839 _mesa_LoadMatrixf(save
->TextureMatrix
);
840 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
842 _mesa_MatrixMode(GL_MODELVIEW
);
843 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
845 _mesa_MatrixMode(GL_PROJECTION
);
846 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
848 _mesa_MatrixMode(save
->MatrixMode
);
850 if (save
->ClipPlanesEnabled
) {
852 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
853 if (save
->ClipPlanesEnabled
& (1 << i
)) {
854 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
860 if (state
& META_VERTEX
) {
861 /* restore vertex buffer object */
862 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
863 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
865 /* restore vertex array object */
866 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
867 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
870 if (state
& META_VIEWPORT
) {
871 if (save
->ViewportX
!= ctx
->Viewport
.X
||
872 save
->ViewportY
!= ctx
->Viewport
.Y
||
873 save
->ViewportW
!= ctx
->Viewport
.Width
||
874 save
->ViewportH
!= ctx
->Viewport
.Height
) {
875 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
876 save
->ViewportW
, save
->ViewportH
);
878 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
881 if (state
& META_CLAMP_FRAGMENT_COLOR
) {
882 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, save
->ClampFragmentColor
);
885 if (state
& META_CLAMP_VERTEX_COLOR
) {
886 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR
, save
->ClampVertexColor
);
889 if (state
& META_CONDITIONAL_RENDER
) {
890 if (save
->CondRenderQuery
)
891 _mesa_BeginConditionalRender(save
->CondRenderQuery
->Id
,
892 save
->CondRenderMode
);
896 if (save
->Lighting
) {
897 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
903 * Convert Z from a normalized value in the range [0, 1] to an object-space
904 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
905 * default/identity ortho projection results in the original Z value.
906 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
907 * value comes from the clear value or raster position.
909 static INLINE GLfloat
910 invert_z(GLfloat normZ
)
912 GLfloat objZ
= 1.0 - 2.0 * normZ
;
918 * One-time init for a temp_texture object.
919 * Choose tex target, compute max tex size, etc.
922 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
924 /* prefer texture rectangle */
925 if (ctx
->Extensions
.NV_texture_rectangle
) {
926 tex
->Target
= GL_TEXTURE_RECTANGLE
;
927 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
931 /* use 2D texture, NPOT if possible */
932 tex
->Target
= GL_TEXTURE_2D
;
933 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
934 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
936 tex
->MinSize
= 16; /* 16 x 16 at least */
937 assert(tex
->MaxSize
> 0);
939 _mesa_GenTextures(1, &tex
->TexObj
);
944 * Return pointer to temp_texture info for non-bitmap ops.
945 * This does some one-time init if needed.
947 static struct temp_texture
*
948 get_temp_texture(struct gl_context
*ctx
)
950 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
953 init_temp_texture(ctx
, tex
);
961 * Return pointer to temp_texture info for _mesa_meta_bitmap().
962 * We use a separate texture for bitmaps to reduce texture
963 * allocation/deallocation.
965 static struct temp_texture
*
966 get_bitmap_temp_texture(struct gl_context
*ctx
)
968 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
971 init_temp_texture(ctx
, tex
);
979 * Compute the width/height of texture needed to draw an image of the
980 * given size. Return a flag indicating whether the current texture
981 * can be re-used (glTexSubImage2D) or if a new texture needs to be
982 * allocated (glTexImage2D).
983 * Also, compute s/t texcoords for drawing.
985 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
988 alloc_texture(struct temp_texture
*tex
,
989 GLsizei width
, GLsizei height
, GLenum intFormat
)
991 GLboolean newTex
= GL_FALSE
;
993 ASSERT(width
<= tex
->MaxSize
);
994 ASSERT(height
<= tex
->MaxSize
);
996 if (width
> tex
->Width
||
997 height
> tex
->Height
||
998 intFormat
!= tex
->IntFormat
) {
999 /* alloc new texture (larger or different format) */
1002 /* use non-power of two size */
1003 tex
->Width
= MAX2(tex
->MinSize
, width
);
1004 tex
->Height
= MAX2(tex
->MinSize
, height
);
1007 /* find power of two size */
1009 w
= h
= tex
->MinSize
;
1018 tex
->IntFormat
= intFormat
;
1023 /* compute texcoords */
1024 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
1025 tex
->Sright
= (GLfloat
) width
;
1026 tex
->Ttop
= (GLfloat
) height
;
1029 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
1030 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
1038 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1041 setup_copypix_texture(struct temp_texture
*tex
,
1043 GLint srcX
, GLint srcY
,
1044 GLsizei width
, GLsizei height
, GLenum intFormat
,
1047 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1048 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
1049 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
1050 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1052 /* copy framebuffer image to texture */
1054 /* create new tex image */
1055 if (tex
->Width
== width
&& tex
->Height
== height
) {
1056 /* create new tex with framebuffer data */
1057 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1058 srcX
, srcY
, width
, height
, 0);
1061 /* create empty texture */
1062 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1063 tex
->Width
, tex
->Height
, 0,
1064 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
1066 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1067 0, 0, srcX
, srcY
, width
, height
);
1071 /* replace existing tex image */
1072 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1073 0, 0, srcX
, srcY
, width
, height
);
1079 * Setup/load texture for glDrawPixels.
1082 setup_drawpix_texture(struct gl_context
*ctx
,
1083 struct temp_texture
*tex
,
1085 GLenum texIntFormat
,
1086 GLsizei width
, GLsizei height
,
1087 GLenum format
, GLenum type
,
1088 const GLvoid
*pixels
)
1090 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1091 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1092 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1093 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1095 /* copy pixel data to texture */
1097 /* create new tex image */
1098 if (tex
->Width
== width
&& tex
->Height
== height
) {
1099 /* create new tex and load image data */
1100 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1101 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1104 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1106 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1107 ctx
->Unpack
.BufferObj
);
1108 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1109 /* create empty texture */
1110 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1111 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1112 if (save_unpack_obj
!= NULL
)
1113 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1114 save_unpack_obj
->Name
);
1116 _mesa_TexSubImage2D(tex
->Target
, 0,
1117 0, 0, width
, height
, format
, type
, pixels
);
1121 /* replace existing tex image */
1122 _mesa_TexSubImage2D(tex
->Target
, 0,
1123 0, 0, width
, height
, format
, type
, pixels
);
1130 * One-time init for drawing depth pixels.
1133 init_blit_depth_pixels(struct gl_context
*ctx
)
1135 static const char *program
=
1137 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1140 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1141 struct temp_texture
*tex
= get_temp_texture(ctx
);
1142 const char *texTarget
;
1144 assert(blit
->DepthFP
== 0);
1146 /* replace %s with "RECT" or "2D" */
1147 assert(strlen(program
) + 4 < sizeof(program2
));
1148 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1152 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1154 _mesa_GenPrograms(1, &blit
->DepthFP
);
1155 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1156 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1157 strlen(program2
), (const GLubyte
*) program2
);
1162 * Try to do a glBlitFramebuffer using no-copy texturing.
1163 * We can do this when the src renderbuffer is actually a texture.
1164 * But if the src buffer == dst buffer we cannot do this.
1166 * \return new buffer mask indicating the buffers left to blit using the
1170 blitframebuffer_texture(struct gl_context
*ctx
,
1171 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1172 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1173 GLbitfield mask
, GLenum filter
)
1175 if (mask
& GL_COLOR_BUFFER_BIT
) {
1176 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1177 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1178 const struct gl_renderbuffer_attachment
*drawAtt
=
1179 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1180 const struct gl_renderbuffer_attachment
*readAtt
=
1181 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1183 if (readAtt
&& readAtt
->Texture
) {
1184 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1185 const GLuint srcLevel
= readAtt
->TextureLevel
;
1186 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
1187 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
1188 const GLint baseLevelSave
= texObj
->BaseLevel
;
1189 const GLint maxLevelSave
= texObj
->MaxLevel
;
1190 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
1191 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
1192 const GLenum srgbSave
= texObj
->Sampler
.sRGBDecode
;
1193 const GLenum fbo_srgb_save
= ctx
->Color
.sRGBEnabled
;
1194 const GLenum target
= texObj
->Target
;
1196 if (drawAtt
->Texture
== readAtt
->Texture
) {
1197 /* Can't use same texture as both the source and dest. We need
1198 * to handle overlapping blits and besides, some hw may not
1204 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1205 /* Can't handle other texture types at this time */
1210 printf("Blit from texture!\n");
1211 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1212 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1215 /* Prepare src texture state */
1216 _mesa_BindTexture(target
, texObj
->Name
);
1217 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1218 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1219 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1220 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1221 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1223 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1224 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1226 /* Always do our blits with no sRGB decode or encode.*/
1227 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1228 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1229 GL_SKIP_DECODE_EXT
);
1231 _mesa_Disable(GL_FRAMEBUFFER_SRGB_EXT
);
1233 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1234 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1236 /* Prepare vertex data (the VBO was previously created and bound) */
1241 struct vertex verts
[4];
1242 GLfloat s0
, t0
, s1
, t1
;
1244 if (target
== GL_TEXTURE_2D
) {
1245 const struct gl_texture_image
*texImage
1246 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1247 s0
= srcX0
/ (float) texImage
->Width
;
1248 s1
= srcX1
/ (float) texImage
->Width
;
1249 t0
= srcY0
/ (float) texImage
->Height
;
1250 t1
= srcY1
/ (float) texImage
->Height
;
1253 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1260 verts
[0].x
= (GLfloat
) dstX0
;
1261 verts
[0].y
= (GLfloat
) dstY0
;
1262 verts
[1].x
= (GLfloat
) dstX1
;
1263 verts
[1].y
= (GLfloat
) dstY0
;
1264 verts
[2].x
= (GLfloat
) dstX1
;
1265 verts
[2].y
= (GLfloat
) dstY1
;
1266 verts
[3].x
= (GLfloat
) dstX0
;
1267 verts
[3].y
= (GLfloat
) dstY1
;
1278 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1281 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1283 /* Restore texture object state, the texture binding will
1284 * be restored by _mesa_meta_end().
1286 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1287 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1288 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1289 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1290 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1292 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1293 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1294 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
1295 _mesa_TexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, srgbSave
);
1297 if (ctx
->Extensions
.EXT_texture_sRGB_decode
&& fbo_srgb_save
) {
1298 _mesa_Enable(GL_FRAMEBUFFER_SRGB_EXT
);
1301 /* Done with color buffer */
1302 mask
&= ~GL_COLOR_BUFFER_BIT
;
1311 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1312 * of texture mapping and polygon rendering.
1315 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1316 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1317 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1318 GLbitfield mask
, GLenum filter
)
1320 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1321 struct temp_texture
*tex
= get_temp_texture(ctx
);
1322 const GLsizei maxTexSize
= tex
->MaxSize
;
1323 const GLint srcX
= MIN2(srcX0
, srcX1
);
1324 const GLint srcY
= MIN2(srcY0
, srcY1
);
1325 const GLint srcW
= abs(srcX1
- srcX0
);
1326 const GLint srcH
= abs(srcY1
- srcY0
);
1327 const GLboolean srcFlipX
= srcX1
< srcX0
;
1328 const GLboolean srcFlipY
= srcY1
< srcY0
;
1332 struct vertex verts
[4];
1335 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1336 /* XXX avoid this fallback */
1337 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1338 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1354 /* only scissor effects blit so save/clear all other relevant state */
1355 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1357 if (blit
->ArrayObj
== 0) {
1358 /* one-time setup */
1360 /* create vertex array object */
1361 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1362 _mesa_BindVertexArray(blit
->ArrayObj
);
1364 /* create vertex array buffer */
1365 _mesa_GenBuffersARB(1, &blit
->VBO
);
1366 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1367 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1368 NULL
, GL_DYNAMIC_DRAW_ARB
);
1370 /* setup vertex arrays */
1371 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1372 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1373 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1374 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1377 _mesa_BindVertexArray(blit
->ArrayObj
);
1378 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1381 /* Try faster, direct texture approach first */
1382 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1383 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1385 _mesa_meta_end(ctx
);
1389 /* Continue with "normal" approach which involves copying the src rect
1390 * into a temporary texture and is "blitted" by drawing a textured quad.
1393 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1395 /* vertex positions/texcoords (after texture allocation!) */
1397 verts
[0].x
= (GLfloat
) dstX0
;
1398 verts
[0].y
= (GLfloat
) dstY0
;
1399 verts
[1].x
= (GLfloat
) dstX1
;
1400 verts
[1].y
= (GLfloat
) dstY0
;
1401 verts
[2].x
= (GLfloat
) dstX1
;
1402 verts
[2].y
= (GLfloat
) dstY1
;
1403 verts
[3].x
= (GLfloat
) dstX0
;
1404 verts
[3].y
= (GLfloat
) dstY1
;
1408 verts
[1].s
= tex
->Sright
;
1410 verts
[2].s
= tex
->Sright
;
1411 verts
[2].t
= tex
->Ttop
;
1413 verts
[3].t
= tex
->Ttop
;
1415 /* upload new vertex data */
1416 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1419 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1421 if (mask
& GL_COLOR_BUFFER_BIT
) {
1422 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1424 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1425 mask
&= ~GL_COLOR_BUFFER_BIT
;
1428 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1429 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1432 init_blit_depth_pixels(ctx
);
1434 /* maybe change tex format here */
1435 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1437 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1438 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1440 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1441 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1443 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1444 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1445 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1446 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1447 _mesa_DepthFunc(GL_ALWAYS
);
1448 _mesa_DepthMask(GL_TRUE
);
1450 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1451 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1457 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1458 /* XXX can't easily do stencil */
1461 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1463 _mesa_meta_end(ctx
);
1466 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1467 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1473 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1476 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1478 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1480 GLfloat x
, y
, z
, r
, g
, b
, a
;
1482 struct vertex verts
[4];
1483 /* save all state but scissor, pixel pack/unpack */
1484 GLbitfield metaSave
= (META_ALL
-
1487 META_CONDITIONAL_RENDER
);
1488 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1490 if (buffers
& BUFFER_BITS_COLOR
) {
1491 /* if clearing color buffers, don't save/restore colormask */
1492 metaSave
-= META_COLOR_MASK
;
1495 _mesa_meta_begin(ctx
, metaSave
);
1497 if (clear
->ArrayObj
== 0) {
1498 /* one-time setup */
1500 /* create vertex array object */
1501 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1502 _mesa_BindVertexArray(clear
->ArrayObj
);
1504 /* create vertex array buffer */
1505 _mesa_GenBuffersARB(1, &clear
->VBO
);
1506 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1508 /* setup vertex arrays */
1509 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1510 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1511 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1512 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1515 _mesa_BindVertexArray(clear
->ArrayObj
);
1516 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1519 /* GL_COLOR_BUFFER_BIT */
1520 if (buffers
& BUFFER_BITS_COLOR
) {
1521 /* leave colormask, glDrawBuffer state as-is */
1523 /* Clears never have the color clamped. */
1524 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1527 ASSERT(metaSave
& META_COLOR_MASK
);
1528 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1531 /* GL_DEPTH_BUFFER_BIT */
1532 if (buffers
& BUFFER_BIT_DEPTH
) {
1533 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1534 _mesa_DepthFunc(GL_ALWAYS
);
1535 _mesa_DepthMask(GL_TRUE
);
1538 assert(!ctx
->Depth
.Test
);
1541 /* GL_STENCIL_BUFFER_BIT */
1542 if (buffers
& BUFFER_BIT_STENCIL
) {
1543 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1544 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1545 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1546 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1547 ctx
->Stencil
.Clear
& stencilMax
,
1548 ctx
->Stencil
.WriteMask
[0]);
1551 assert(!ctx
->Stencil
.Enabled
);
1554 /* vertex positions/colors */
1556 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1557 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1558 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1559 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1560 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1577 for (i
= 0; i
< 4; i
++) {
1578 verts
[i
].r
= ctx
->Color
.ClearColorUnclamped
[0];
1579 verts
[i
].g
= ctx
->Color
.ClearColorUnclamped
[1];
1580 verts
[i
].b
= ctx
->Color
.ClearColorUnclamped
[2];
1581 verts
[i
].a
= ctx
->Color
.ClearColorUnclamped
[3];
1584 /* upload new vertex data */
1585 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1586 GL_DYNAMIC_DRAW_ARB
);
1590 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1592 _mesa_meta_end(ctx
);
1596 meta_glsl_clear_init(struct gl_context
*ctx
, struct clear_state
*clear
)
1598 const char *vs_source
=
1599 "attribute vec4 position;\n"
1602 " gl_Position = position;\n"
1604 const char *fs_source
=
1605 "uniform vec4 color;\n"
1608 " gl_FragColor = color;\n"
1612 if (clear
->ArrayObj
!= 0)
1615 /* create vertex array object */
1616 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1617 _mesa_BindVertexArray(clear
->ArrayObj
);
1619 /* create vertex array buffer */
1620 _mesa_GenBuffersARB(1, &clear
->VBO
);
1621 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1623 /* setup vertex arrays */
1624 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT
, GL_FALSE
, 0, (void *)0);
1625 _mesa_EnableVertexAttribArrayARB(0);
1627 vs
= _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER
);
1628 _mesa_ShaderSourceARB(vs
, 1, &vs_source
, NULL
);
1629 _mesa_CompileShaderARB(vs
);
1631 fs
= _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER
);
1632 _mesa_ShaderSourceARB(fs
, 1, &fs_source
, NULL
);
1633 _mesa_CompileShaderARB(fs
);
1635 clear
->ShaderProg
= _mesa_CreateProgramObjectARB();
1636 _mesa_AttachShader(clear
->ShaderProg
, fs
);
1637 _mesa_AttachShader(clear
->ShaderProg
, vs
);
1638 _mesa_BindAttribLocationARB(clear
->ShaderProg
, 0, "position");
1639 _mesa_LinkProgramARB(clear
->ShaderProg
);
1641 clear
->ColorLocation
= _mesa_GetUniformLocationARB(clear
->ShaderProg
,
1646 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1649 _mesa_meta_glsl_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1651 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1652 GLbitfield metaSave
;
1653 const GLuint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
1654 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1655 const float x0
= ((float)fb
->_Xmin
/ fb
->Width
) * 2.0f
- 1.0f
;
1656 const float y0
= ((float)fb
->_Ymin
/ fb
->Height
) * 2.0f
- 1.0f
;
1657 const float x1
= ((float)fb
->_Xmax
/ fb
->Width
) * 2.0f
- 1.0f
;
1658 const float y1
= ((float)fb
->_Ymax
/ fb
->Height
) * 2.0f
- 1.0f
;
1659 const float z
= -invert_z(ctx
->Depth
.Clear
);
1664 metaSave
= (META_ALPHA_TEST
|
1667 META_RASTERIZATION
|
1672 META_CLAMP_FRAGMENT_COLOR
);
1674 if (!(buffers
& BUFFER_BITS_COLOR
)) {
1675 /* We'll use colormask to disable color writes. Otherwise,
1676 * respect color mask
1678 metaSave
|= META_COLOR_MASK
;
1681 _mesa_meta_begin(ctx
, metaSave
);
1683 meta_glsl_clear_init(ctx
, clear
);
1685 _mesa_UseProgramObjectARB(clear
->ShaderProg
);
1686 _mesa_Uniform4fvARB(clear
->ColorLocation
, 1,
1687 ctx
->Color
.ClearColorUnclamped
);
1689 _mesa_BindVertexArray(clear
->ArrayObj
);
1690 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1692 /* GL_COLOR_BUFFER_BIT */
1693 if (buffers
& BUFFER_BITS_COLOR
) {
1694 /* leave colormask, glDrawBuffer state as-is */
1696 /* Clears never have the color clamped. */
1697 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
1700 ASSERT(metaSave
& META_COLOR_MASK
);
1701 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1704 /* GL_DEPTH_BUFFER_BIT */
1705 if (buffers
& BUFFER_BIT_DEPTH
) {
1706 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1707 _mesa_DepthFunc(GL_ALWAYS
);
1708 _mesa_DepthMask(GL_TRUE
);
1711 assert(!ctx
->Depth
.Test
);
1714 /* GL_STENCIL_BUFFER_BIT */
1715 if (buffers
& BUFFER_BIT_STENCIL
) {
1716 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1717 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1718 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1719 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1720 ctx
->Stencil
.Clear
& stencilMax
,
1721 ctx
->Stencil
.WriteMask
[0]);
1724 assert(!ctx
->Stencil
.Enabled
);
1727 /* vertex positions */
1741 /* upload new vertex data */
1742 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1743 GL_DYNAMIC_DRAW_ARB
);
1746 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1748 _mesa_meta_end(ctx
);
1752 * Meta implementation of ctx->Driver.CopyPixels() in terms
1753 * of texture mapping and polygon rendering and GLSL shaders.
1756 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1757 GLsizei width
, GLsizei height
,
1758 GLint dstX
, GLint dstY
, GLenum type
)
1760 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1761 struct temp_texture
*tex
= get_temp_texture(ctx
);
1763 GLfloat x
, y
, z
, s
, t
;
1765 struct vertex verts
[4];
1767 GLenum intFormat
= GL_RGBA
;
1769 if (type
!= GL_COLOR
||
1770 ctx
->_ImageTransferState
||
1772 width
> tex
->MaxSize
||
1773 height
> tex
->MaxSize
) {
1774 /* XXX avoid this fallback */
1775 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1779 /* Most GL state applies to glCopyPixels, but a there's a few things
1780 * we need to override:
1782 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1789 if (copypix
->ArrayObj
== 0) {
1790 /* one-time setup */
1792 /* create vertex array object */
1793 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1794 _mesa_BindVertexArray(copypix
->ArrayObj
);
1796 /* create vertex array buffer */
1797 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1798 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1799 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1800 NULL
, GL_DYNAMIC_DRAW_ARB
);
1802 /* setup vertex arrays */
1803 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1804 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1805 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1806 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1809 _mesa_BindVertexArray(copypix
->ArrayObj
);
1810 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1813 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1815 /* vertex positions, texcoords (after texture allocation!) */
1817 const GLfloat dstX0
= (GLfloat
) dstX
;
1818 const GLfloat dstY0
= (GLfloat
) dstY
;
1819 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1820 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1821 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1831 verts
[1].s
= tex
->Sright
;
1836 verts
[2].s
= tex
->Sright
;
1837 verts
[2].t
= tex
->Ttop
;
1842 verts
[3].t
= tex
->Ttop
;
1844 /* upload new vertex data */
1845 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1848 /* Alloc/setup texture */
1849 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1850 GL_RGBA
, GL_NEAREST
);
1852 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1854 /* draw textured quad */
1855 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1857 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1859 _mesa_meta_end(ctx
);
1865 * When the glDrawPixels() image size is greater than the max rectangle
1866 * texture size we use this function to break the glDrawPixels() image
1867 * into tiles which fit into the max texture size.
1870 tiled_draw_pixels(struct gl_context
*ctx
,
1872 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1873 GLenum format
, GLenum type
,
1874 const struct gl_pixelstore_attrib
*unpack
,
1875 const GLvoid
*pixels
)
1877 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1880 if (tileUnpack
.RowLength
== 0)
1881 tileUnpack
.RowLength
= width
;
1883 for (i
= 0; i
< width
; i
+= tileSize
) {
1884 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1885 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1887 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1889 for (j
= 0; j
< height
; j
+= tileSize
) {
1890 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1891 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1893 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1895 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1896 format
, type
, &tileUnpack
, pixels
);
1903 * One-time init for drawing stencil pixels.
1906 init_draw_stencil_pixels(struct gl_context
*ctx
)
1908 /* This program is run eight times, once for each stencil bit.
1909 * The stencil values to draw are found in an 8-bit alpha texture.
1910 * We read the texture/stencil value and test if bit 'b' is set.
1911 * If the bit is not set, use KIL to kill the fragment.
1912 * Finally, we use the stencil test to update the stencil buffer.
1914 * The basic algorithm for checking if a bit is set is:
1915 * if (is_odd(value / (1 << bit)))
1916 * result is one (or non-zero).
1919 * The program parameter contains three values:
1920 * parm.x = 255 / (1 << bit)
1924 static const char *program
=
1926 "PARAM parm = program.local[0]; \n"
1928 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1929 "# t = t * 255 / bit \n"
1930 "MUL t.x, t.a, parm.x; \n"
1933 "SUB t.x, t.x, t.y; \n"
1935 "MUL t.x, t.x, parm.y; \n"
1936 "# t = fract(t.x) \n"
1937 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1938 "# t.x = (t.x == 0 ? 1 : 0) \n"
1939 "SGE t.x, -t.x, parm.z; \n"
1941 "# for debug only \n"
1942 "#MOV result.color, t.x; \n"
1944 char program2
[1000];
1945 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1946 struct temp_texture
*tex
= get_temp_texture(ctx
);
1947 const char *texTarget
;
1949 assert(drawpix
->StencilFP
== 0);
1951 /* replace %s with "RECT" or "2D" */
1952 assert(strlen(program
) + 4 < sizeof(program2
));
1953 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1957 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1959 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1960 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1961 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1962 strlen(program2
), (const GLubyte
*) program2
);
1967 * One-time init for drawing depth pixels.
1970 init_draw_depth_pixels(struct gl_context
*ctx
)
1972 static const char *program
=
1974 "PARAM color = program.local[0]; \n"
1975 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1976 "MOV result.color, color; \n"
1979 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1980 struct temp_texture
*tex
= get_temp_texture(ctx
);
1981 const char *texTarget
;
1983 assert(drawpix
->DepthFP
== 0);
1985 /* replace %s with "RECT" or "2D" */
1986 assert(strlen(program
) + 4 < sizeof(program2
));
1987 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1991 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1993 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1994 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1995 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1996 strlen(program2
), (const GLubyte
*) program2
);
2001 * Meta implementation of ctx->Driver.DrawPixels() in terms
2002 * of texture mapping and polygon rendering.
2005 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
2006 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2007 GLenum format
, GLenum type
,
2008 const struct gl_pixelstore_attrib
*unpack
,
2009 const GLvoid
*pixels
)
2011 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
2012 struct temp_texture
*tex
= get_temp_texture(ctx
);
2013 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
2014 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
2016 GLfloat x
, y
, z
, s
, t
;
2018 struct vertex verts
[4];
2019 GLenum texIntFormat
;
2020 GLboolean fallback
, newTex
;
2021 GLbitfield metaExtraSave
= 0x0;
2025 * Determine if we can do the glDrawPixels with texture mapping.
2027 fallback
= GL_FALSE
;
2028 if (ctx
->_ImageTransferState
||
2033 if (_mesa_is_color_format(format
)) {
2034 /* use more compact format when possible */
2035 /* XXX disable special case for GL_LUMINANCE for now to work around
2036 * apparent i965 driver bug (see bug #23670).
2038 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
2039 texIntFormat
= format
;
2041 texIntFormat
= GL_RGBA
;
2043 /* If we're not supposed to clamp the resulting color, then just
2044 * promote our texture to fully float. We could do better by
2045 * just going for the matching set of channels, in floating
2048 if (ctx
->Color
.ClampFragmentColor
!= GL_TRUE
&&
2049 ctx
->Extensions
.ARB_texture_float
)
2050 texIntFormat
= GL_RGBA32F
;
2052 else if (_mesa_is_stencil_format(format
)) {
2053 if (ctx
->Extensions
.ARB_fragment_program
&&
2054 ctx
->Pixel
.IndexShift
== 0 &&
2055 ctx
->Pixel
.IndexOffset
== 0 &&
2056 type
== GL_UNSIGNED_BYTE
) {
2057 /* We'll store stencil as alpha. This only works for GLubyte
2058 * image data because of how incoming values are mapped to alpha
2061 texIntFormat
= GL_ALPHA
;
2062 metaExtraSave
= (META_COLOR_MASK
|
2071 else if (_mesa_is_depth_format(format
)) {
2072 if (ctx
->Extensions
.ARB_depth_texture
&&
2073 ctx
->Extensions
.ARB_fragment_program
) {
2074 texIntFormat
= GL_DEPTH_COMPONENT
;
2075 metaExtraSave
= (META_SHADER
);
2086 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
2087 format
, type
, unpack
, pixels
);
2092 * Check image size against max texture size, draw as tiles if needed.
2094 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
2095 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
2096 format
, type
, unpack
, pixels
);
2100 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2101 * but a there's a few things we need to override:
2103 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
2109 META_CLAMP_FRAGMENT_COLOR
|
2112 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2114 /* vertex positions, texcoords (after texture allocation!) */
2116 const GLfloat x0
= (GLfloat
) x
;
2117 const GLfloat y0
= (GLfloat
) y
;
2118 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
2119 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
2120 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2130 verts
[1].s
= tex
->Sright
;
2135 verts
[2].s
= tex
->Sright
;
2136 verts
[2].t
= tex
->Ttop
;
2141 verts
[3].t
= tex
->Ttop
;
2144 if (drawpix
->ArrayObj
== 0) {
2145 /* one-time setup: create vertex array object */
2146 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
2148 _mesa_BindVertexArray(drawpix
->ArrayObj
);
2150 /* create vertex array buffer */
2151 _mesa_GenBuffersARB(1, &vbo
);
2152 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
2153 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2154 verts
, GL_DYNAMIC_DRAW_ARB
);
2156 /* setup vertex arrays */
2157 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2158 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2159 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2160 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2162 /* set given unpack params */
2163 ctx
->Unpack
= *unpack
;
2165 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2167 if (_mesa_is_stencil_format(format
)) {
2168 /* Drawing stencil */
2171 if (!drawpix
->StencilFP
)
2172 init_draw_stencil_pixels(ctx
);
2174 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2175 GL_ALPHA
, type
, pixels
);
2177 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
2179 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
2181 /* set all stencil bits to 0 */
2182 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
2183 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
2184 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2186 /* set stencil bits to 1 where needed */
2187 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
2189 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
2190 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2192 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
2193 const GLuint mask
= 1 << bit
;
2194 if (mask
& origStencilMask
) {
2195 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
2196 _mesa_StencilMask(mask
);
2198 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2199 255.0 / mask
, 0.5, 0.0, 0.0);
2201 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2205 else if (_mesa_is_depth_format(format
)) {
2207 if (!drawpix
->DepthFP
)
2208 init_draw_depth_pixels(ctx
);
2210 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
2211 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
2213 /* polygon color = current raster color */
2214 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
2215 ctx
->Current
.RasterColor
);
2217 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2218 format
, type
, pixels
);
2220 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2224 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2225 format
, type
, pixels
);
2226 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2229 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2231 _mesa_DeleteBuffersARB(1, &vbo
);
2233 /* restore unpack params */
2234 ctx
->Unpack
= unpackSave
;
2236 _mesa_meta_end(ctx
);
2240 alpha_test_raster_color(struct gl_context
*ctx
)
2242 GLfloat alpha
= ctx
->Current
.RasterColor
[ACOMP
];
2243 GLfloat ref
= ctx
->Color
.AlphaRef
;
2245 switch (ctx
->Color
.AlphaFunc
) {
2251 return alpha
== ref
;
2253 return alpha
<= ref
;
2257 return alpha
!= ref
;
2259 return alpha
>= ref
;
2269 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2270 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2271 * tracker would improve performance a lot.
2274 _mesa_meta_Bitmap(struct gl_context
*ctx
,
2275 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2276 const struct gl_pixelstore_attrib
*unpack
,
2277 const GLubyte
*bitmap1
)
2279 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
2280 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
2281 const GLenum texIntFormat
= GL_ALPHA
;
2282 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2285 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2287 struct vertex verts
[4];
2292 * Check if swrast fallback is needed.
2294 if (ctx
->_ImageTransferState
||
2295 ctx
->FragmentProgram
._Enabled
||
2297 ctx
->Texture
._EnabledUnits
||
2298 width
> tex
->MaxSize
||
2299 height
> tex
->MaxSize
) {
2300 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2304 if (ctx
->Color
.AlphaEnabled
&& !alpha_test_raster_color(ctx
))
2307 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2308 * but a there's a few things we need to override:
2310 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2312 META_RASTERIZATION
|
2319 if (bitmap
->ArrayObj
== 0) {
2320 /* one-time setup */
2322 /* create vertex array object */
2323 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2324 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2326 /* create vertex array buffer */
2327 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2328 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2329 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2330 NULL
, GL_DYNAMIC_DRAW_ARB
);
2332 /* setup vertex arrays */
2333 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2334 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2335 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2336 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2337 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2338 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2341 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2342 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2345 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2347 /* vertex positions, texcoords, colors (after texture allocation!) */
2349 const GLfloat x0
= (GLfloat
) x
;
2350 const GLfloat y0
= (GLfloat
) y
;
2351 const GLfloat x1
= (GLfloat
) (x
+ width
);
2352 const GLfloat y1
= (GLfloat
) (y
+ height
);
2353 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2364 verts
[1].s
= tex
->Sright
;
2369 verts
[2].s
= tex
->Sright
;
2370 verts
[2].t
= tex
->Ttop
;
2375 verts
[3].t
= tex
->Ttop
;
2377 for (i
= 0; i
< 4; i
++) {
2378 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2379 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2380 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2381 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2384 /* upload new vertex data */
2385 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2388 /* choose different foreground/background alpha values */
2389 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2390 bg
= (fg
> 127 ? 0 : 255);
2392 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2394 _mesa_meta_end(ctx
);
2398 bitmap8
= (GLubyte
*) malloc(width
* height
);
2400 memset(bitmap8
, bg
, width
* height
);
2401 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2402 bitmap8
, width
, fg
);
2404 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2406 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2407 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2409 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2410 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2412 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2414 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2419 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2421 _mesa_meta_end(ctx
);
2426 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2427 * software fallback. The fallback path will require that the texture
2428 * images are mapped.
2429 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2432 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2433 struct gl_texture_object
*texObj
)
2435 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2436 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2437 struct gl_texture_image
*baseImage
;
2441 /* check for fallbacks */
2442 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2443 target
== GL_TEXTURE_3D
) {
2447 srcLevel
= texObj
->BaseLevel
;
2448 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2449 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2454 * Test that we can actually render in the texture's format.
2457 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2458 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2460 if (target
== GL_TEXTURE_1D
) {
2461 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2462 GL_COLOR_ATTACHMENT0_EXT
,
2463 target
, texObj
->Name
, srcLevel
);
2466 /* other work is needed to enable 3D mipmap generation */
2467 else if (target
== GL_TEXTURE_3D
) {
2469 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2470 GL_COLOR_ATTACHMENT0_EXT
,
2471 target
, texObj
->Name
, srcLevel
, zoffset
);
2476 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2477 GL_COLOR_ATTACHMENT0_EXT
,
2478 target
, texObj
->Name
, srcLevel
);
2481 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2483 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2485 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2494 * Called via ctx->Driver.GenerateMipmap()
2495 * Note: texture borders and 3D texture support not yet complete.
2498 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2499 struct gl_texture_object
*texObj
)
2501 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2503 GLfloat x
, y
, s
, t
, r
;
2505 struct vertex verts
[4];
2506 const GLuint baseLevel
= texObj
->BaseLevel
;
2507 const GLuint maxLevel
= texObj
->MaxLevel
;
2508 const GLenum minFilterSave
= texObj
->Sampler
.MinFilter
;
2509 const GLenum magFilterSave
= texObj
->Sampler
.MagFilter
;
2510 const GLint maxLevelSave
= texObj
->MaxLevel
;
2511 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2512 const GLenum wrapSSave
= texObj
->Sampler
.WrapS
;
2513 const GLenum wrapTSave
= texObj
->Sampler
.WrapT
;
2514 const GLenum wrapRSave
= texObj
->Sampler
.WrapR
;
2515 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2516 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2521 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2522 _mesa_generate_mipmap(ctx
, target
, texObj
);
2526 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2527 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2528 faceTarget
= target
;
2529 target
= GL_TEXTURE_CUBE_MAP
;
2532 faceTarget
= target
;
2535 _mesa_meta_begin(ctx
, META_ALL
);
2537 if (original_active_unit
!= 0)
2538 _mesa_BindTexture(target
, texObj
->Name
);
2540 if (mipmap
->ArrayObj
== 0) {
2541 /* one-time setup */
2543 /* create vertex array object */
2544 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2545 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2547 /* create vertex array buffer */
2548 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2549 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2550 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2551 NULL
, GL_DYNAMIC_DRAW_ARB
);
2553 /* setup vertex arrays */
2554 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2555 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2556 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2557 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2560 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2561 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2565 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2567 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2569 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
2570 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2571 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2572 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2573 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2574 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2576 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2578 /* setup texcoords once (XXX what about border?) */
2579 switch (faceTarget
) {
2601 static const GLfloat st
[4][2] = {
2602 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2606 /* loop over quad verts */
2607 for (i
= 0; i
< 4; i
++) {
2608 /* Compute sc = +/-scale and tc = +/-scale.
2609 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2610 * though that can still sometimes happen with this scale factor...
2612 const GLfloat scale
= 0.9999f
;
2613 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2614 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2616 switch (faceTarget
) {
2617 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2622 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2627 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2632 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2637 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2642 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2654 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2656 /* setup vertex positions */
2667 /* upload new vertex data */
2668 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2671 /* setup projection matrix */
2672 _mesa_MatrixMode(GL_PROJECTION
);
2673 _mesa_LoadIdentity();
2674 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2676 /* texture is already locked, unlock now */
2677 _mesa_unlock_texture(ctx
, texObj
);
2679 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2680 const struct gl_texture_image
*srcImage
;
2681 const GLuint srcLevel
= dstLevel
- 1;
2682 GLsizei srcWidth
, srcHeight
, srcDepth
;
2683 GLsizei dstWidth
, dstHeight
, dstDepth
;
2686 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2687 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2689 /* src size w/out border */
2690 srcWidth
= srcImage
->Width
- 2 * border
;
2691 srcHeight
= srcImage
->Height
- 2 * border
;
2692 srcDepth
= srcImage
->Depth
- 2 * border
;
2694 /* new dst size w/ border */
2695 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2696 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2697 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2699 if (dstWidth
== srcImage
->Width
&&
2700 dstHeight
== srcImage
->Height
&&
2701 dstDepth
== srcImage
->Depth
) {
2706 /* Set MaxLevel large enough to hold the new level when we allocate it */
2707 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2709 /* Create empty dest image */
2710 if (target
== GL_TEXTURE_1D
) {
2711 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2713 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2715 else if (target
== GL_TEXTURE_3D
) {
2716 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2717 dstWidth
, dstHeight
, dstDepth
, border
,
2718 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2722 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2723 dstWidth
, dstHeight
, border
,
2724 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2726 if (target
== GL_TEXTURE_CUBE_MAP
) {
2727 /* If texturing from a cube, we need to make sure all src faces
2728 * have been defined (even if we're not sampling from them.)
2729 * Otherwise the texture object will be 'incomplete' and
2730 * texturing from it will not be allowed.
2733 for (face
= 0; face
< 6; face
++) {
2734 if (!texObj
->Image
[face
][srcLevel
] ||
2735 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2736 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2737 srcLevel
, srcImage
->InternalFormat
,
2738 srcWidth
, srcHeight
, border
,
2739 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2745 /* limit minification to src level */
2746 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2748 /* Set to draw into the current dstLevel */
2749 if (target
== GL_TEXTURE_1D
) {
2750 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2751 GL_COLOR_ATTACHMENT0_EXT
,
2756 else if (target
== GL_TEXTURE_3D
) {
2757 GLint zoffset
= 0; /* XXX unfinished */
2758 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2759 GL_COLOR_ATTACHMENT0_EXT
,
2766 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2767 GL_COLOR_ATTACHMENT0_EXT
,
2773 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2776 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2777 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2782 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2783 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2785 /* setup viewport */
2786 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2788 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2791 _mesa_lock_texture(ctx
, texObj
); /* relock */
2793 _mesa_meta_end(ctx
);
2795 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2796 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2797 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2798 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2799 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2800 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2801 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2803 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2808 * Determine the GL data type to use for the temporary image read with
2809 * ReadPixels() and passed to Tex[Sub]Image().
2812 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2814 switch (baseFormat
) {
2819 case GL_LUMINANCE_ALPHA
:
2821 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2822 return GL_UNSIGNED_BYTE
;
2823 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2824 return GL_UNSIGNED_SHORT
;
2827 case GL_DEPTH_COMPONENT
:
2828 return GL_UNSIGNED_INT
;
2829 case GL_DEPTH_STENCIL
:
2830 return GL_UNSIGNED_INT_24_8
;
2832 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2839 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2840 * Have to be careful with locking and meta state for pixel transfer.
2843 copy_tex_sub_image(struct gl_context
*ctx
,
2844 GLuint dims
, GLenum target
, GLint level
,
2845 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2847 GLsizei width
, GLsizei height
)
2849 struct gl_texture_object
*texObj
;
2850 struct gl_texture_image
*texImage
;
2851 GLenum format
, type
;
2855 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2856 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2858 /* Choose format/type for temporary image buffer */
2859 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2860 type
= get_temp_image_type(ctx
, format
);
2861 bpp
= _mesa_bytes_per_pixel(format
, type
);
2863 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2868 * Alloc image buffer (XXX could use a PBO)
2870 buf
= malloc(width
* height
* bpp
);
2872 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2876 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2879 * Read image from framebuffer (disable pixel transfer ops)
2881 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2882 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2883 format
, type
, &ctx
->Pack
, buf
);
2884 _mesa_meta_end(ctx
);
2886 _mesa_update_state(ctx
); /* to update pixel transfer state */
2889 * Store texture data (with pixel transfer ops)
2891 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2892 if (target
== GL_TEXTURE_1D
) {
2893 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2894 width
, format
, type
, buf
,
2895 &ctx
->Unpack
, texObj
, texImage
);
2897 else if (target
== GL_TEXTURE_3D
) {
2898 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2899 width
, height
, 1, format
, type
, buf
,
2900 &ctx
->Unpack
, texObj
, texImage
);
2903 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2904 width
, height
, format
, type
, buf
,
2905 &ctx
->Unpack
, texObj
, texImage
);
2907 _mesa_meta_end(ctx
);
2909 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2916 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2918 GLint x
, GLint y
, GLsizei width
)
2920 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2926 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2927 GLint xoffset
, GLint yoffset
,
2929 GLsizei width
, GLsizei height
)
2931 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2932 x
, y
, width
, height
);
2937 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2938 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2940 GLsizei width
, GLsizei height
)
2942 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2943 x
, y
, width
, height
);
2948 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2949 GLenum target
, GLenum internalformat
,
2950 GLint x
, GLint y
, GLsizei width
)
2954 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2956 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2961 * Read image from framebuffer (disable pixel transfer ops)
2963 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2964 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2965 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2967 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2969 _mesa_meta_end(ctx
);
2976 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2977 GLint x
, GLint y
, GLsizei width
)
2981 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2983 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2988 * Read image from framebuffer (disable pixel transfer ops)
2990 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2991 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2992 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2994 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2996 _mesa_meta_end(ctx
);