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/arrayobj.h"
38 #include "main/blend.h"
39 #include "main/bufferobj.h"
40 #include "main/buffers.h"
41 #include "main/depth.h"
42 #include "main/enable.h"
43 #include "main/fbobject.h"
44 #include "main/image.h"
45 #include "main/macros.h"
46 #include "main/matrix.h"
47 #include "main/mipmap.h"
48 #include "main/polygon.h"
49 #include "main/readpix.h"
50 #include "main/scissor.h"
51 #include "main/shaders.h"
52 #include "main/stencil.h"
53 #include "main/texobj.h"
54 #include "main/texenv.h"
55 #include "main/teximage.h"
56 #include "main/texparam.h"
57 #include "main/texstate.h"
58 #include "main/varray.h"
59 #include "main/viewport.h"
60 #include "shader/program.h"
61 #include "shader/arbprogram.h"
62 #include "swrast/swrast.h"
63 #include "drivers/common/meta.h"
66 /** Return offset in bytes of the field within a vertex struct */
67 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
71 * State which we may save/restore across meta ops.
72 * XXX this may be incomplete...
76 GLbitfield SavedState
; /**< bitmask of META_* flags */
78 /** META_ALPHA_TEST */
79 GLboolean AlphaEnabled
;
82 GLboolean BlendEnabled
;
83 GLboolean ColorLogicOpEnabled
;
85 /** META_COLOR_MASK */
88 /** META_DEPTH_TEST */
89 struct gl_depthbuffer_attrib Depth
;
94 /** META_PIXEL_STORE */
95 struct gl_pixelstore_attrib Pack
, Unpack
;
97 /** META_RASTERIZATION */
98 GLenum FrontPolygonMode
, BackPolygonMode
;
99 GLboolean PolygonOffset
;
100 GLboolean PolygonSmooth
;
101 GLboolean PolygonStipple
;
102 GLboolean PolygonCull
;
105 struct gl_scissor_attrib Scissor
;
108 GLboolean VertexProgramEnabled
;
109 struct gl_vertex_program
*VertexProgram
;
110 GLboolean FragmentProgramEnabled
;
111 struct gl_fragment_program
*FragmentProgram
;
114 /** META_STENCIL_TEST */
115 struct gl_stencil_attrib Stencil
;
117 /** META_TRANSFORM */
119 GLfloat ModelviewMatrix
[16];
120 GLfloat ProjectionMatrix
[16];
121 GLfloat TextureMatrix
[16];
122 GLbitfield ClipPlanesEnabled
;
126 GLuint ClientActiveUnit
;
127 /** for unit[0] only */
128 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
129 /** mask of TEXTURE_2D_BIT, etc */
130 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
131 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
132 GLuint EnvMode
; /* unit[0] only */
135 struct gl_array_object
*ArrayObj
;
136 struct gl_buffer_object
*ArrayBufferObj
;
139 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
140 GLclampd DepthNear
, DepthFar
;
142 /** Miscellaneous (always disabled) */
148 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
149 * This is currently shared by all the meta ops. But we could create a
150 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
155 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
156 GLsizei MinSize
; /**< Min texture size to allocate */
157 GLsizei MaxSize
; /**< Max possible texture size */
158 GLboolean NPOT
; /**< Non-power of two size OK? */
159 GLsizei Width
, Height
; /**< Current texture size */
161 GLfloat Sright
, Ttop
; /**< right, top texcoords */
166 * State for glBlitFramebufer()
177 * State for glClear()
187 * State for glCopyPixels()
197 * State for glDrawPixels()
204 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
205 GLuint DepthFP
; /**< Fragment program for drawing depth images */
210 * State for glBitmap()
216 struct temp_texture Tex
; /**< separate texture from other meta ops */
221 * State for _mesa_meta_generate_mipmap()
223 struct gen_mipmap_state
232 * All per-context meta state.
236 struct save_state Save
; /**< state saved during meta-ops */
238 struct temp_texture TempTex
;
240 struct blit_state Blit
; /**< For _mesa_meta_blit_framebuffer() */
241 struct clear_state Clear
; /**< For _mesa_meta_clear() */
242 struct copypix_state CopyPix
; /**< For _mesa_meta_copy_pixels() */
243 struct drawpix_state DrawPix
; /**< For _mesa_meta_draw_pixels() */
244 struct bitmap_state Bitmap
; /**< For _mesa_meta_bitmap() */
245 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_generate_mipmap() */
250 * Initialize meta-ops for a context.
251 * To be called once during context creation.
254 _mesa_meta_init(GLcontext
*ctx
)
258 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
263 * Free context meta-op state.
264 * To be called once during context destruction.
267 _mesa_meta_free(GLcontext
*ctx
)
269 struct gl_meta_state
*meta
= ctx
->Meta
;
271 if (_mesa_get_current_context()) {
272 /* if there's no current context, these textures, buffers, etc should
273 * still get freed by _mesa_free_context_data().
276 /* the temporary texture */
277 _mesa_DeleteTextures(1, &meta
->TempTex
.TexObj
);
279 /* glBlitFramebuffer */
280 _mesa_DeleteBuffersARB(1, & meta
->Blit
.VBO
);
281 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Blit
.ArrayObj
);
282 _mesa_DeletePrograms(1, &meta
->Blit
.DepthFP
);
285 _mesa_DeleteBuffersARB(1, & meta
->Clear
.VBO
);
286 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Clear
.ArrayObj
);
289 _mesa_DeleteBuffersARB(1, & meta
->CopyPix
.VBO
);
290 _mesa_DeleteVertexArraysAPPLE(1, &meta
->CopyPix
.ArrayObj
);
293 _mesa_DeleteBuffersARB(1, & meta
->DrawPix
.VBO
);
294 _mesa_DeleteVertexArraysAPPLE(1, &meta
->DrawPix
.ArrayObj
);
295 _mesa_DeletePrograms(1, &meta
->DrawPix
.DepthFP
);
296 _mesa_DeletePrograms(1, &meta
->DrawPix
.StencilFP
);
299 _mesa_DeleteBuffersARB(1, & meta
->Bitmap
.VBO
);
300 _mesa_DeleteVertexArraysAPPLE(1, &meta
->Bitmap
.ArrayObj
);
301 _mesa_DeleteTextures(1, &meta
->Bitmap
.Tex
.TexObj
);
304 _mesa_free(ctx
->Meta
);
310 * Enter meta state. This is like a light-weight version of glPushAttrib
311 * but it also resets most GL state back to default values.
313 * \param state bitmask of META_* flags indicating which attribute groups
314 * to save and reset to their defaults
317 _mesa_meta_begin(GLcontext
*ctx
, GLbitfield state
)
319 struct save_state
*save
= &ctx
->Meta
->Save
;
321 save
->SavedState
= state
;
323 if (state
& META_ALPHA_TEST
) {
324 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
325 if (ctx
->Color
.AlphaEnabled
)
326 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
329 if (state
& META_BLEND
) {
330 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
331 if (ctx
->Color
.BlendEnabled
)
332 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
333 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
334 if (ctx
->Color
.ColorLogicOpEnabled
)
335 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
338 if (state
& META_COLOR_MASK
) {
339 COPY_4V(save
->ColorMask
, ctx
->Color
.ColorMask
);
340 if (!ctx
->Color
.ColorMask
[0] ||
341 !ctx
->Color
.ColorMask
[1] ||
342 !ctx
->Color
.ColorMask
[2] ||
343 !ctx
->Color
.ColorMask
[3])
344 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
347 if (state
& META_DEPTH_TEST
) {
348 save
->Depth
= ctx
->Depth
; /* struct copy */
350 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
353 if (state
& META_FOG
) {
354 save
->Fog
= ctx
->Fog
.Enabled
;
355 if (ctx
->Fog
.Enabled
)
356 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
359 if (state
& META_PIXEL_STORE
) {
360 save
->Pack
= ctx
->Pack
;
361 save
->Unpack
= ctx
->Unpack
;
362 ctx
->Pack
= ctx
->DefaultPacking
;
363 ctx
->Unpack
= ctx
->DefaultPacking
;
366 if (state
& META_RASTERIZATION
) {
367 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
368 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
369 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
370 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
371 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
372 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
373 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
374 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
375 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
376 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
377 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
380 if (state
& META_SCISSOR
) {
381 save
->Scissor
= ctx
->Scissor
; /* struct copy */
384 if (state
& META_SHADER
) {
385 if (ctx
->Extensions
.ARB_vertex_program
) {
386 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
387 save
->VertexProgram
= ctx
->VertexProgram
.Current
;
388 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
391 if (ctx
->Extensions
.ARB_fragment_program
) {
392 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
393 save
->FragmentProgram
= ctx
->FragmentProgram
.Current
;
394 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
397 if (ctx
->Extensions
.ARB_shader_objects
) {
398 save
->Shader
= ctx
->Shader
.CurrentProgram
?
399 ctx
->Shader
.CurrentProgram
->Name
: 0;
400 _mesa_UseProgramObjectARB(0);
404 if (state
& META_STENCIL_TEST
) {
405 save
->Stencil
= ctx
->Stencil
; /* struct copy */
406 if (ctx
->Stencil
.Enabled
)
407 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
408 /* NOTE: other stencil state not reset */
411 if (state
& META_TEXTURE
) {
414 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
415 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
416 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
418 /* Disable all texture units */
419 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
420 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
421 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
422 if (ctx
->Texture
.Unit
[u
].Enabled
||
423 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
424 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
425 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
426 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
427 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
428 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
429 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
430 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
431 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
432 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
433 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
437 /* save current texture objects for unit[0] only */
438 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
439 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
440 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
443 /* set defaults for unit[0] */
444 _mesa_ActiveTextureARB(GL_TEXTURE0
);
445 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
446 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
449 if (state
& META_TRANSFORM
) {
450 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
451 _mesa_memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
452 16 * sizeof(GLfloat
));
453 _mesa_memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
454 16 * sizeof(GLfloat
));
455 _mesa_memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
456 16 * sizeof(GLfloat
));
457 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
458 /* set 1:1 vertex:pixel coordinate transform */
459 _mesa_ActiveTextureARB(GL_TEXTURE0
);
460 _mesa_MatrixMode(GL_TEXTURE
);
461 _mesa_LoadIdentity();
462 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
463 _mesa_MatrixMode(GL_MODELVIEW
);
464 _mesa_LoadIdentity();
465 _mesa_MatrixMode(GL_PROJECTION
);
466 _mesa_LoadIdentity();
467 _mesa_Ortho(0.0F
, ctx
->DrawBuffer
->Width
,
468 0.0F
, ctx
->DrawBuffer
->Height
,
470 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
471 if (ctx
->Transform
.ClipPlanesEnabled
) {
473 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
474 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
479 if (state
& META_VERTEX
) {
480 /* save vertex array object state */
481 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
482 ctx
->Array
.ArrayObj
);
483 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
484 ctx
->Array
.ArrayBufferObj
);
485 /* set some default state? */
488 if (state
& META_VIEWPORT
) {
489 /* save viewport state */
490 save
->ViewportX
= ctx
->Viewport
.X
;
491 save
->ViewportY
= ctx
->Viewport
.Y
;
492 save
->ViewportW
= ctx
->Viewport
.Width
;
493 save
->ViewportH
= ctx
->Viewport
.Height
;
494 /* set viewport to match window size */
495 if (ctx
->Viewport
.X
!= 0 ||
496 ctx
->Viewport
.Y
!= 0 ||
497 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
498 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
499 _mesa_set_viewport(ctx
, 0, 0,
500 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
502 /* save depth range state */
503 save
->DepthNear
= ctx
->Viewport
.Near
;
504 save
->DepthFar
= ctx
->Viewport
.Far
;
505 /* set depth range to default */
506 _mesa_DepthRange(0.0, 1.0);
511 save
->Lighting
= ctx
->Light
.Enabled
;
512 if (ctx
->Light
.Enabled
)
513 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
519 * Leave meta state. This is like a light-weight version of glPopAttrib().
522 _mesa_meta_end(GLcontext
*ctx
)
524 struct save_state
*save
= &ctx
->Meta
->Save
;
525 const GLbitfield state
= save
->SavedState
;
527 if (state
& META_ALPHA_TEST
) {
528 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
529 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
532 if (state
& META_BLEND
) {
533 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
)
534 _mesa_set_enable(ctx
, GL_BLEND
, save
->BlendEnabled
);
535 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
536 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
539 if (state
& META_COLOR_MASK
) {
540 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
, save
->ColorMask
))
541 _mesa_ColorMask(save
->ColorMask
[0], save
->ColorMask
[1],
542 save
->ColorMask
[2], save
->ColorMask
[3]);
545 if (state
& META_DEPTH_TEST
) {
546 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
547 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
548 _mesa_DepthFunc(save
->Depth
.Func
);
549 _mesa_DepthMask(save
->Depth
.Mask
);
552 if (state
& META_FOG
) {
553 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
556 if (state
& META_PIXEL_STORE
) {
557 ctx
->Pack
= save
->Pack
;
558 ctx
->Unpack
= save
->Unpack
;
561 if (state
& META_RASTERIZATION
) {
562 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
563 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
564 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
565 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
566 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
567 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
570 if (state
& META_SCISSOR
) {
571 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
572 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
573 save
->Scissor
.Width
, save
->Scissor
.Height
);
576 if (state
& META_SHADER
) {
577 if (ctx
->Extensions
.ARB_vertex_program
) {
578 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
579 save
->VertexProgramEnabled
);
580 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
581 save
->VertexProgram
);
584 if (ctx
->Extensions
.ARB_fragment_program
) {
585 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
586 save
->FragmentProgramEnabled
);
587 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
588 save
->FragmentProgram
);
591 if (ctx
->Extensions
.ARB_shader_objects
) {
592 _mesa_UseProgramObjectARB(save
->Shader
);
596 if (state
& META_STENCIL_TEST
) {
597 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
599 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
600 _mesa_ClearStencil(stencil
->Clear
);
601 if (ctx
->Extensions
.EXT_stencil_two_side
) {
602 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
603 stencil
->TestTwoSide
);
604 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
605 ? GL_BACK
: GL_FRONT
);
608 _mesa_StencilFuncSeparate(GL_FRONT
,
609 stencil
->Function
[0],
611 stencil
->ValueMask
[0]);
612 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
613 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
614 stencil
->ZFailFunc
[0],
615 stencil
->ZPassFunc
[0]);
617 _mesa_StencilFuncSeparate(GL_BACK
,
618 stencil
->Function
[1],
620 stencil
->ValueMask
[1]);
621 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
622 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
623 stencil
->ZFailFunc
[1],
624 stencil
->ZPassFunc
[1]);
627 if (state
& META_TEXTURE
) {
630 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
632 /* restore texenv for unit[0] */
633 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
635 /* restore texture objects for unit[0] only */
636 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
637 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
638 save
->CurrentTexture
[tgt
]);
641 /* Re-enable textures, texgen */
642 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
643 if (save
->TexEnabled
[u
]) {
644 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
646 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
647 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
648 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
649 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
650 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
651 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
652 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
653 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
654 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
655 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
658 if (save
->TexGenEnabled
[u
]) {
659 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
661 if (save
->TexGenEnabled
[u
] & S_BIT
)
662 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
663 if (save
->TexGenEnabled
[u
] & T_BIT
)
664 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
665 if (save
->TexGenEnabled
[u
] & R_BIT
)
666 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
667 if (save
->TexGenEnabled
[u
] & Q_BIT
)
668 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
672 /* restore current unit state */
673 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
674 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
677 if (state
& META_TRANSFORM
) {
678 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
679 _mesa_ActiveTextureARB(GL_TEXTURE0
);
680 _mesa_MatrixMode(GL_TEXTURE
);
681 _mesa_LoadMatrixf(save
->TextureMatrix
);
682 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
684 _mesa_MatrixMode(GL_MODELVIEW
);
685 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
687 _mesa_MatrixMode(GL_PROJECTION
);
688 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
690 _mesa_MatrixMode(save
->MatrixMode
);
692 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
693 if (save
->ClipPlanesEnabled
) {
695 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
696 if (save
->ClipPlanesEnabled
& (1 << i
)) {
697 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
703 if (state
& META_VERTEX
) {
704 /* restore vertex buffer object */
705 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
706 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
708 /* restore vertex array object */
709 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
710 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
713 if (state
& META_VIEWPORT
) {
714 if (save
->ViewportX
!= ctx
->Viewport
.X
||
715 save
->ViewportY
!= ctx
->Viewport
.Y
||
716 save
->ViewportW
!= ctx
->Viewport
.Width
||
717 save
->ViewportH
!= ctx
->Viewport
.Height
) {
718 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
719 save
->ViewportW
, save
->ViewportH
);
721 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
725 if (save
->Lighting
) {
726 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
729 _mesa_set_enable(ctx
, GL_FOG
, GL_TRUE
);
735 * One-time init for a temp_texture object.
736 * Choose tex target, compute max tex size, etc.
739 init_temp_texture(GLcontext
*ctx
, struct temp_texture
*tex
)
741 /* prefer texture rectangle */
742 if (ctx
->Extensions
.NV_texture_rectangle
) {
743 tex
->Target
= GL_TEXTURE_RECTANGLE
;
744 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
748 /* use 2D texture, NPOT if possible */
749 tex
->Target
= GL_TEXTURE_2D
;
750 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
751 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
753 tex
->MinSize
= 16; /* 16 x 16 at least */
754 assert(tex
->MaxSize
> 0);
756 _mesa_GenTextures(1, &tex
->TexObj
);
757 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
762 * Return pointer to temp_texture info for non-bitmap ops.
763 * This does some one-time init if needed.
765 static struct temp_texture
*
766 get_temp_texture(GLcontext
*ctx
)
768 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
771 init_temp_texture(ctx
, tex
);
779 * Return pointer to temp_texture info for _mesa_meta_bitmap().
780 * We use a separate texture for bitmaps to reduce texture
781 * allocation/deallocation.
783 static struct temp_texture
*
784 get_bitmap_temp_texture(GLcontext
*ctx
)
786 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
789 init_temp_texture(ctx
, tex
);
797 * Compute the width/height of texture needed to draw an image of the
798 * given size. Return a flag indicating whether the current texture
799 * can be re-used (glTexSubImage2D) or if a new texture needs to be
800 * allocated (glTexImage2D).
801 * Also, compute s/t texcoords for drawing.
803 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
806 alloc_texture(struct temp_texture
*tex
,
807 GLsizei width
, GLsizei height
, GLenum intFormat
)
809 GLboolean newTex
= GL_FALSE
;
811 ASSERT(width
<= tex
->MaxSize
);
812 ASSERT(height
<= tex
->MaxSize
);
814 if (width
> tex
->Width
||
815 height
> tex
->Height
||
816 intFormat
!= tex
->IntFormat
) {
817 /* alloc new texture (larger or different format) */
820 /* use non-power of two size */
821 tex
->Width
= MAX2(tex
->MinSize
, width
);
822 tex
->Height
= MAX2(tex
->MinSize
, height
);
825 /* find power of two size */
827 w
= h
= tex
->MinSize
;
836 tex
->IntFormat
= intFormat
;
841 /* compute texcoords */
842 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
843 tex
->Sright
= (GLfloat
) width
;
844 tex
->Ttop
= (GLfloat
) height
;
847 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
848 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
856 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
859 setup_copypix_texture(struct temp_texture
*tex
,
861 GLint srcX
, GLint srcY
,
862 GLsizei width
, GLsizei height
, GLenum intFormat
,
865 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
866 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
867 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
868 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
870 /* copy framebuffer image to texture */
872 /* create new tex image */
873 if (tex
->Width
== width
&& tex
->Height
== height
) {
874 /* create new tex with framebuffer data */
875 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
876 srcX
, srcY
, width
, height
, 0);
879 /* create empty texture */
880 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
881 tex
->Width
, tex
->Height
, 0,
882 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
884 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
885 0, 0, srcX
, srcY
, width
, height
);
889 /* replace existing tex image */
890 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
891 0, 0, srcX
, srcY
, width
, height
);
897 * Setup/load texture for glDrawPixels.
900 setup_drawpix_texture(struct temp_texture
*tex
,
903 GLsizei width
, GLsizei height
,
904 GLenum format
, GLenum type
,
905 const GLvoid
*pixels
)
907 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
908 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
909 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
910 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
912 /* copy pixel data to texture */
914 /* create new tex image */
915 if (tex
->Width
== width
&& tex
->Height
== height
) {
916 /* create new tex and load image data */
917 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
918 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
921 /* create empty texture */
922 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
923 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
925 _mesa_TexSubImage2D(tex
->Target
, 0,
926 0, 0, width
, height
, format
, type
, pixels
);
930 /* replace existing tex image */
931 _mesa_TexSubImage2D(tex
->Target
, 0,
932 0, 0, width
, height
, format
, type
, pixels
);
939 * One-time init for drawing depth pixels.
942 init_blit_depth_pixels(GLcontext
*ctx
)
944 static const char *program
=
946 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
949 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
950 struct temp_texture
*tex
= get_temp_texture(ctx
);
951 const char *texTarget
;
953 assert(blit
->DepthFP
== 0);
955 /* replace %s with "RECT" or "2D" */
956 assert(strlen(program
) + 4 < sizeof(program2
));
957 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
961 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
963 _mesa_GenPrograms(1, &blit
->DepthFP
);
964 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
965 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
966 strlen(program2
), (const GLubyte
*) program2
);
971 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
972 * of texture mapping and polygon rendering.
975 _mesa_meta_blit_framebuffer(GLcontext
*ctx
,
976 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
977 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
978 GLbitfield mask
, GLenum filter
)
980 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
981 struct temp_texture
*tex
= get_temp_texture(ctx
);
982 const GLsizei maxTexSize
= tex
->MaxSize
;
983 const GLint srcX
= MIN2(srcX0
, srcX1
);
984 const GLint srcY
= MIN2(srcY0
, srcY1
);
985 const GLint srcW
= abs(srcX1
- srcX0
);
986 const GLint srcH
= abs(srcY1
- srcY0
);
987 const GLboolean srcFlipX
= srcX1
< srcX0
;
988 const GLboolean srcFlipY
= srcY1
< srcY0
;
992 struct vertex verts
[4];
995 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
996 /* XXX avoid this fallback */
997 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
998 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1014 /* only scissor effects blit so save/clear all other relevant state */
1015 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1017 if (blit
->ArrayObj
== 0) {
1018 /* one-time setup */
1020 /* create vertex array object */
1021 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1022 _mesa_BindVertexArray(blit
->ArrayObj
);
1024 /* create vertex array buffer */
1025 _mesa_GenBuffersARB(1, &blit
->VBO
);
1026 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1027 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1028 NULL
, GL_DYNAMIC_DRAW_ARB
);
1030 /* setup vertex arrays */
1031 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1032 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1033 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1034 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1037 _mesa_BindVertexArray(blit
->ArrayObj
);
1038 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1041 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1043 /* vertex positions/texcoords (after texture allocation!) */
1045 verts
[0].x
= (GLfloat
) dstX0
;
1046 verts
[0].y
= (GLfloat
) dstY0
;
1047 verts
[1].x
= (GLfloat
) dstX1
;
1048 verts
[1].y
= (GLfloat
) dstY0
;
1049 verts
[2].x
= (GLfloat
) dstX1
;
1050 verts
[2].y
= (GLfloat
) dstY1
;
1051 verts
[3].x
= (GLfloat
) dstX0
;
1052 verts
[3].y
= (GLfloat
) dstY1
;
1056 verts
[1].s
= tex
->Sright
;
1058 verts
[2].s
= tex
->Sright
;
1059 verts
[2].t
= tex
->Ttop
;
1061 verts
[3].t
= tex
->Ttop
;
1063 /* upload new vertex data */
1064 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1067 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1069 if (mask
& GL_COLOR_BUFFER_BIT
) {
1070 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1072 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1073 mask
&= ~GL_COLOR_BUFFER_BIT
;
1076 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1077 GLuint
*tmp
= (GLuint
*) _mesa_malloc(srcW
* srcH
* sizeof(GLuint
));
1080 init_blit_depth_pixels(ctx
);
1082 /* maybe change tex format here */
1083 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1085 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1086 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1088 setup_drawpix_texture(tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1089 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1091 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1092 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1093 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1094 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1095 _mesa_DepthFunc(GL_ALWAYS
);
1096 _mesa_DepthMask(GL_TRUE
);
1098 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1099 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1105 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1106 /* XXX can't easily do stencil */
1109 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1111 _mesa_meta_end(ctx
);
1114 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1115 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1121 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1124 _mesa_meta_clear(GLcontext
*ctx
, GLbitfield buffers
)
1126 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1128 GLfloat x
, y
, z
, r
, g
, b
, a
;
1130 struct vertex verts
[4];
1131 /* save all state but scissor, pixel pack/unpack */
1132 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1134 if (buffers
& BUFFER_BITS_COLOR
) {
1135 /* if clearing color buffers, don't save/restore colormask */
1136 metaSave
-= META_COLOR_MASK
;
1139 _mesa_meta_begin(ctx
, metaSave
);
1141 if (clear
->ArrayObj
== 0) {
1142 /* one-time setup */
1144 /* create vertex array object */
1145 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1146 _mesa_BindVertexArray(clear
->ArrayObj
);
1148 /* create vertex array buffer */
1149 _mesa_GenBuffersARB(1, &clear
->VBO
);
1150 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1151 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1152 NULL
, GL_DYNAMIC_DRAW_ARB
);
1154 /* setup vertex arrays */
1155 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1156 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1157 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1158 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1161 _mesa_BindVertexArray(clear
->ArrayObj
);
1162 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1165 /* GL_COLOR_BUFFER_BIT */
1166 if (buffers
& BUFFER_BITS_COLOR
) {
1167 /* leave colormask, glDrawBuffer state as-is */
1170 ASSERT(metaSave
& META_COLOR_MASK
);
1171 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1174 /* GL_DEPTH_BUFFER_BIT */
1175 if (buffers
& BUFFER_BIT_DEPTH
) {
1176 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1177 _mesa_DepthFunc(GL_ALWAYS
);
1178 _mesa_DepthMask(GL_TRUE
);
1181 assert(!ctx
->Depth
.Test
);
1184 /* GL_STENCIL_BUFFER_BIT */
1185 if (buffers
& BUFFER_BIT_STENCIL
) {
1186 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1187 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1188 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1189 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1190 ctx
->Stencil
.Clear
& 0x7fffffff,
1191 ctx
->Stencil
.WriteMask
[0]);
1194 assert(!ctx
->Stencil
.Enabled
);
1197 /* vertex positions/colors */
1199 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1200 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1201 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1202 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1203 const GLfloat z
= 1.0 - 2.0 * ctx
->Depth
.Clear
;
1220 for (i
= 0; i
< 4; i
++) {
1221 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1222 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1223 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1224 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1227 /* upload new vertex data */
1228 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1232 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1234 _mesa_meta_end(ctx
);
1239 * Meta implementation of ctx->Driver.CopyPixels() in terms
1240 * of texture mapping and polygon rendering.
1243 _mesa_meta_copy_pixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1244 GLsizei width
, GLsizei height
,
1245 GLint dstX
, GLint dstY
, GLenum type
)
1247 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1248 struct temp_texture
*tex
= get_temp_texture(ctx
);
1250 GLfloat x
, y
, z
, s
, t
;
1252 struct vertex verts
[4];
1254 GLenum intFormat
= GL_RGBA
;
1256 if (type
!= GL_COLOR
||
1257 ctx
->_ImageTransferState
||
1259 width
> tex
->MaxSize
||
1260 height
> tex
->MaxSize
) {
1261 /* XXX avoid this fallback */
1262 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1266 /* Most GL state applies to glCopyPixels, but a there's a few things
1267 * we need to override:
1269 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1276 if (copypix
->ArrayObj
== 0) {
1277 /* one-time setup */
1279 /* create vertex array object */
1280 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1281 _mesa_BindVertexArray(copypix
->ArrayObj
);
1283 /* create vertex array buffer */
1284 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1285 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1286 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1287 NULL
, GL_DYNAMIC_DRAW_ARB
);
1289 /* setup vertex arrays */
1290 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1291 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1292 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1293 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1296 _mesa_BindVertexArray(copypix
->ArrayObj
);
1297 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1300 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1302 /* vertex positions, texcoords (after texture allocation!) */
1304 const GLfloat dstX0
= (GLfloat
) dstX
;
1305 const GLfloat dstY0
= (GLfloat
) dstY
;
1306 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1307 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1308 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1318 verts
[1].s
= tex
->Sright
;
1323 verts
[2].s
= tex
->Sright
;
1324 verts
[2].t
= tex
->Ttop
;
1329 verts
[3].t
= tex
->Ttop
;
1331 /* upload new vertex data */
1332 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1335 /* Alloc/setup texture */
1336 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1337 GL_RGBA
, GL_NEAREST
);
1339 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1341 /* draw textured quad */
1342 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1344 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1346 _mesa_meta_end(ctx
);
1352 * When the glDrawPixels() image size is greater than the max rectangle
1353 * texture size we use this function to break the glDrawPixels() image
1354 * into tiles which fit into the max texture size.
1357 tiled_draw_pixels(GLcontext
*ctx
,
1359 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1360 GLenum format
, GLenum type
,
1361 const struct gl_pixelstore_attrib
*unpack
,
1362 const GLvoid
*pixels
)
1364 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1367 if (tileUnpack
.RowLength
== 0)
1368 tileUnpack
.RowLength
= width
;
1370 for (i
= 0; i
< width
; i
+= tileSize
) {
1371 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1372 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1374 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1376 for (j
= 0; j
< height
; j
+= tileSize
) {
1377 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1378 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1380 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1382 _mesa_meta_draw_pixels(ctx
, tileX
, tileY
,
1383 tileWidth
, tileHeight
,
1384 format
, type
, &tileUnpack
, pixels
);
1391 * One-time init for drawing stencil pixels.
1394 init_draw_stencil_pixels(GLcontext
*ctx
)
1396 /* This program is run eight times, once for each stencil bit.
1397 * The stencil values to draw are found in an 8-bit alpha texture.
1398 * We read the texture/stencil value and test if bit 'b' is set.
1399 * If the bit is not set, use KIL to kill the fragment.
1400 * Finally, we use the stencil test to update the stencil buffer.
1402 * The basic algorithm for checking if a bit is set is:
1403 * if (is_odd(value / (1 << bit)))
1404 * result is one (or non-zero).
1407 * The program parameter contains three values:
1408 * parm.x = 255 / (1 << bit)
1412 static const char *program
=
1414 "PARAM parm = program.local[0]; \n"
1416 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1417 "# t = t * 255 / bit \n"
1418 "MUL t.x, t.a, parm.x; \n"
1421 "SUB t.x, t.x, t.y; \n"
1423 "MUL t.x, t.x, parm.y; \n"
1424 "# t = fract(t.x) \n"
1425 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1426 "# t.x = (t.x == 0 ? 1 : 0) \n"
1427 "SGE t.x, -t.x, parm.z; \n"
1429 "# for debug only \n"
1430 "#MOV result.color, t.x; \n"
1432 char program2
[1000];
1433 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1434 struct temp_texture
*tex
= get_temp_texture(ctx
);
1435 const char *texTarget
;
1437 assert(drawpix
->StencilFP
== 0);
1439 /* replace %s with "RECT" or "2D" */
1440 assert(strlen(program
) + 4 < sizeof(program2
));
1441 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1445 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1447 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1448 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1449 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1450 strlen(program2
), (const GLubyte
*) program2
);
1455 * One-time init for drawing depth pixels.
1458 init_draw_depth_pixels(GLcontext
*ctx
)
1460 static const char *program
=
1462 "PARAM color = program.local[0]; \n"
1463 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1464 "MOV result.color, color; \n"
1467 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1468 struct temp_texture
*tex
= get_temp_texture(ctx
);
1469 const char *texTarget
;
1471 assert(drawpix
->DepthFP
== 0);
1473 /* replace %s with "RECT" or "2D" */
1474 assert(strlen(program
) + 4 < sizeof(program2
));
1475 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1479 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1481 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1482 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1483 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1484 strlen(program2
), (const GLubyte
*) program2
);
1489 * Meta implementation of ctx->Driver.DrawPixels() in terms
1490 * of texture mapping and polygon rendering.
1493 _mesa_meta_draw_pixels(GLcontext
*ctx
,
1494 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1495 GLenum format
, GLenum type
,
1496 const struct gl_pixelstore_attrib
*unpack
,
1497 const GLvoid
*pixels
)
1499 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1500 struct temp_texture
*tex
= get_temp_texture(ctx
);
1501 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1502 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1504 GLfloat x
, y
, z
, s
, t
;
1506 struct vertex verts
[4];
1507 GLenum texIntFormat
;
1508 GLboolean fallback
, newTex
;
1509 GLbitfield metaExtraSave
= 0x0;
1512 * Determine if we can do the glDrawPixels with texture mapping.
1514 fallback
= GL_FALSE
;
1515 if (ctx
->_ImageTransferState
||
1520 if (_mesa_is_color_format(format
)) {
1521 /* use more compact format when possible */
1522 /* XXX disable special case for GL_LUMINANCE for now to work around
1523 * apparent i965 driver bug (see bug #23670).
1525 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1526 texIntFormat
= format
;
1528 texIntFormat
= GL_RGBA
;
1530 else if (_mesa_is_stencil_format(format
)) {
1531 if (ctx
->Extensions
.ARB_fragment_program
&&
1532 ctx
->Pixel
.IndexShift
== 0 &&
1533 ctx
->Pixel
.IndexOffset
== 0 &&
1534 type
== GL_UNSIGNED_BYTE
) {
1535 /* We'll store stencil as alpha. This only works for GLubyte
1536 * image data because of how incoming values are mapped to alpha
1539 texIntFormat
= GL_ALPHA
;
1540 metaExtraSave
= (META_COLOR_MASK
|
1549 else if (_mesa_is_depth_format(format
)) {
1550 if (ctx
->Extensions
.ARB_depth_texture
&&
1551 ctx
->Extensions
.ARB_fragment_program
) {
1552 texIntFormat
= GL_DEPTH_COMPONENT
;
1553 metaExtraSave
= (META_SHADER
);
1564 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1565 format
, type
, unpack
, pixels
);
1570 * Check image size against max texture size, draw as tiles if needed.
1572 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1573 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1574 format
, type
, unpack
, pixels
);
1578 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1579 * but a there's a few things we need to override:
1581 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1589 if (drawpix
->ArrayObj
== 0) {
1590 /* one-time setup */
1592 /* create vertex array object */
1593 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1594 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1596 /* create vertex array buffer */
1597 _mesa_GenBuffersARB(1, &drawpix
->VBO
);
1598 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawpix
->VBO
);
1599 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1600 NULL
, GL_DYNAMIC_DRAW_ARB
);
1602 /* setup vertex arrays */
1603 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1604 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1605 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1606 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1609 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1610 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, drawpix
->VBO
);
1613 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1615 /* vertex positions, texcoords (after texture allocation!) */
1617 const GLfloat x0
= (GLfloat
) x
;
1618 const GLfloat y0
= (GLfloat
) y
;
1619 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1620 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1621 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1631 verts
[1].s
= tex
->Sright
;
1636 verts
[2].s
= tex
->Sright
;
1637 verts
[2].t
= tex
->Ttop
;
1642 verts
[3].t
= tex
->Ttop
;
1644 /* upload new vertex data */
1645 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1648 /* set given unpack params */
1649 ctx
->Unpack
= *unpack
;
1651 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1653 if (_mesa_is_stencil_format(format
)) {
1654 /* Drawing stencil */
1657 if (!drawpix
->StencilFP
)
1658 init_draw_stencil_pixels(ctx
);
1660 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1661 GL_ALPHA
, type
, pixels
);
1663 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1665 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1667 /* set all stencil bits to 0 */
1668 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1669 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1670 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1672 /* set stencil bits to 1 where needed */
1673 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1675 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1676 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1678 for (bit
= 0; bit
< ctx
->Visual
.stencilBits
; bit
++) {
1679 const GLuint mask
= 1 << bit
;
1680 if (mask
& origStencilMask
) {
1681 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1682 _mesa_StencilMask(mask
);
1684 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1685 255.0 / mask
, 0.5, 0.0, 0.0);
1687 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1691 else if (_mesa_is_depth_format(format
)) {
1693 if (!drawpix
->DepthFP
)
1694 init_draw_depth_pixels(ctx
);
1696 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1697 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1699 /* polygon color = current raster color */
1700 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1701 ctx
->Current
.RasterColor
);
1703 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1704 format
, type
, pixels
);
1706 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1710 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1711 format
, type
, pixels
);
1712 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1715 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1717 /* restore unpack params */
1718 ctx
->Unpack
= unpackSave
;
1720 _mesa_meta_end(ctx
);
1725 * Do glBitmap with a alpha texture quad. Use the alpha test to
1726 * cull the 'off' bits. If alpha test is already enabled, fall back
1727 * to swrast (should be a rare case).
1728 * A bitmap cache as in the gallium/mesa state tracker would
1729 * improve performance a lot.
1732 _mesa_meta_bitmap(GLcontext
*ctx
,
1733 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1734 const struct gl_pixelstore_attrib
*unpack
,
1735 const GLubyte
*bitmap1
)
1737 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1738 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1739 const GLenum texIntFormat
= GL_ALPHA
;
1740 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
1742 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
1744 struct vertex verts
[4];
1749 * Check if swrast fallback is needed.
1751 if (ctx
->_ImageTransferState
||
1752 ctx
->Color
.AlphaEnabled
||
1754 ctx
->Texture
._EnabledUnits
||
1755 width
> tex
->MaxSize
||
1756 height
> tex
->MaxSize
) {
1757 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
1761 /* Most GL state applies to glBitmap (like blending, stencil, etc),
1762 * but a there's a few things we need to override:
1764 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
1766 META_RASTERIZATION
|
1773 if (bitmap
->ArrayObj
== 0) {
1774 /* one-time setup */
1776 /* create vertex array object */
1777 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
1778 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
1780 /* create vertex array buffer */
1781 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
1782 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
1783 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1784 NULL
, GL_DYNAMIC_DRAW_ARB
);
1786 /* setup vertex arrays */
1787 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1788 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1789 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1790 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1791 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1792 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1795 _mesa_BindVertexArray(bitmap
->ArrayObj
);
1796 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
1799 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1801 /* vertex positions, texcoords, colors (after texture allocation!) */
1803 const GLfloat x0
= (GLfloat
) x
;
1804 const GLfloat y0
= (GLfloat
) y
;
1805 const GLfloat x1
= (GLfloat
) (x
+ width
);
1806 const GLfloat y1
= (GLfloat
) (y
+ height
);
1807 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1818 verts
[1].s
= tex
->Sright
;
1823 verts
[2].s
= tex
->Sright
;
1824 verts
[2].t
= tex
->Ttop
;
1829 verts
[3].t
= tex
->Ttop
;
1831 for (i
= 0; i
< 4; i
++) {
1832 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
1833 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
1834 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
1835 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
1838 /* upload new vertex data */
1839 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1842 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
1846 bitmap8
= (GLubyte
*) _mesa_calloc(width
* height
);
1848 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
1849 bitmap8
, width
, 0xff);
1851 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1853 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
1854 _mesa_AlphaFunc(GL_GREATER
, 0.0);
1856 setup_drawpix_texture(tex
, newTex
, texIntFormat
, width
, height
,
1857 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
1859 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1861 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1863 _mesa_free(bitmap8
);
1866 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
1868 _mesa_meta_end(ctx
);
1873 _mesa_meta_generate_mipmap(GLcontext
*ctx
, GLenum target
,
1874 struct gl_texture_object
*texObj
)
1876 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
1878 GLfloat x
, y
, s
, t
, r
;
1880 struct vertex verts
[4];
1881 const GLuint baseLevel
= texObj
->BaseLevel
;
1882 const GLuint maxLevel
= texObj
->MaxLevel
;
1883 const GLenum minFilterSave
= texObj
->MinFilter
;
1884 const GLenum magFilterSave
= texObj
->MagFilter
;
1885 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
1890 /* check for fallbacks */
1891 if (!ctx
->Extensions
.EXT_framebuffer_object
) {
1892 _mesa_generate_mipmap(ctx
, target
, texObj
);
1896 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1897 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
1898 faceTarget
= target
;
1899 target
= GL_TEXTURE_CUBE_MAP
;
1902 faceTarget
= target
;
1905 _mesa_meta_begin(ctx
, META_ALL
);
1907 if (mipmap
->ArrayObj
== 0) {
1908 /* one-time setup */
1910 /* create vertex array object */
1911 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
1912 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
1914 /* create vertex array buffer */
1915 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
1916 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
1917 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1918 NULL
, GL_DYNAMIC_DRAW_ARB
);
1920 /* setup vertex arrays */
1921 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1922 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1923 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1924 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1927 _mesa_BindVertexArray(mipmap
->ArrayObj
);
1928 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
1932 /* Bind the new renderbuffer to the color attachment point. */
1933 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
1936 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
1938 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1939 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1940 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1942 /* setup texcoords once (XXX what about border?) */
1943 switch (faceTarget
) {
1944 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1963 for (level
= baseLevel
+ 1; level
<= maxLevel
; level
++) {
1964 const struct gl_texture_image
*srcImage
;
1965 const GLuint srcLevel
= level
- 1;
1966 GLsizei srcWidth
, srcHeight
;
1967 GLsizei newWidth
, newHeight
;
1970 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1971 assert(srcImage
->Border
== 0); /* XXX we can fix this */
1973 srcWidth
= srcImage
->Width
- 2 * border
;
1974 srcHeight
= srcImage
->Height
- 2 * border
;
1976 newWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
1977 newHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
1979 if (newWidth
== srcImage
->Width
&& newHeight
== srcImage
->Height
) {
1983 /* Create empty image */
1984 _mesa_TexImage2D(GL_TEXTURE_2D
, level
, srcImage
->InternalFormat
,
1985 newWidth
, newHeight
, border
,
1986 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
1988 /* vertex positions */
1992 verts
[1].x
= (GLfloat
) newWidth
;
1994 verts
[2].x
= (GLfloat
) newWidth
;
1995 verts
[2].y
= (GLfloat
) newHeight
;
1997 verts
[3].y
= (GLfloat
) newHeight
;
1999 /* upload new vertex data */
2000 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2003 /* limit sampling to src level */
2004 _mesa_TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2005 _mesa_TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2007 /* Set to draw into the current level */
2008 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2009 GL_COLOR_ATTACHMENT0_EXT
,
2014 /* Choose to render to the color attachment. */
2015 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2017 status
= _mesa_CheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT
);
2018 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2023 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2026 _mesa_meta_end(ctx
);
2028 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2029 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2031 /* restore (XXX add to meta_begin/end()? */
2032 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);