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/depth.h"
44 #include "main/enable.h"
45 #include "main/fbobject.h"
46 #include "main/formats.h"
47 #include "main/image.h"
48 #include "main/macros.h"
49 #include "main/matrix.h"
50 #include "main/mipmap.h"
51 #include "main/polygon.h"
52 #include "main/readpix.h"
53 #include "main/scissor.h"
54 #include "main/shaderapi.h"
55 #include "main/shaderobj.h"
56 #include "main/state.h"
57 #include "main/stencil.h"
58 #include "main/texobj.h"
59 #include "main/texenv.h"
60 #include "main/teximage.h"
61 #include "main/texparam.h"
62 #include "main/texstate.h"
63 #include "main/varray.h"
64 #include "main/viewport.h"
65 #include "program/program.h"
66 #include "swrast/swrast.h"
67 #include "drivers/common/meta.h"
70 /** Return offset in bytes of the field within a vertex struct */
71 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
75 * Flags passed to _mesa_meta_begin().
79 #define META_ALPHA_TEST 0x1
80 #define META_BLEND 0x2 /**< includes logicop */
81 #define META_COLOR_MASK 0x4
82 #define META_DEPTH_TEST 0x8
84 #define META_PIXEL_STORE 0x20
85 #define META_PIXEL_TRANSFER 0x40
86 #define META_RASTERIZATION 0x80
87 #define META_SCISSOR 0x100
88 #define META_SHADER 0x200
89 #define META_STENCIL_TEST 0x400
90 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
91 #define META_TEXTURE 0x1000
92 #define META_VERTEX 0x2000
93 #define META_VIEWPORT 0x4000
98 * State which we may save/restore across meta ops.
99 * XXX this may be incomplete...
103 GLbitfield SavedState
; /**< bitmask of META_* flags */
105 /** META_ALPHA_TEST */
106 GLboolean AlphaEnabled
;
111 GLbitfield BlendEnabled
;
112 GLboolean ColorLogicOpEnabled
;
114 /** META_COLOR_MASK */
115 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
117 /** META_DEPTH_TEST */
118 struct gl_depthbuffer_attrib Depth
;
123 /** META_PIXEL_STORE */
124 struct gl_pixelstore_attrib Pack
, Unpack
;
126 /** META_PIXEL_TRANSFER */
127 GLfloat RedBias
, RedScale
;
128 GLfloat GreenBias
, GreenScale
;
129 GLfloat BlueBias
, BlueScale
;
130 GLfloat AlphaBias
, AlphaScale
;
131 GLfloat DepthBias
, DepthScale
;
132 GLboolean MapColorFlag
;
134 /** META_RASTERIZATION */
135 GLenum FrontPolygonMode
, BackPolygonMode
;
136 GLboolean PolygonOffset
;
137 GLboolean PolygonSmooth
;
138 GLboolean PolygonStipple
;
139 GLboolean PolygonCull
;
142 struct gl_scissor_attrib Scissor
;
145 GLboolean VertexProgramEnabled
;
146 struct gl_vertex_program
*VertexProgram
;
147 GLboolean FragmentProgramEnabled
;
148 struct gl_fragment_program
*FragmentProgram
;
149 struct gl_shader_program
*VertexShader
;
150 struct gl_shader_program
*GeometryShader
;
151 struct gl_shader_program
*FragmentShader
;
152 struct gl_shader_program
*ActiveShader
;
154 /** META_STENCIL_TEST */
155 struct gl_stencil_attrib Stencil
;
157 /** META_TRANSFORM */
159 GLfloat ModelviewMatrix
[16];
160 GLfloat ProjectionMatrix
[16];
161 GLfloat TextureMatrix
[16];
162 GLbitfield ClipPlanesEnabled
;
166 GLuint ClientActiveUnit
;
167 /** for unit[0] only */
168 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
169 /** mask of TEXTURE_2D_BIT, etc */
170 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
171 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
172 GLuint EnvMode
; /* unit[0] only */
175 struct gl_array_object
*ArrayObj
;
176 struct gl_buffer_object
*ArrayBufferObj
;
179 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
180 GLclampd DepthNear
, DepthFar
;
182 /** Miscellaneous (always disabled) */
188 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
189 * This is currently shared by all the meta ops. But we could create a
190 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
195 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
196 GLsizei MinSize
; /**< Min texture size to allocate */
197 GLsizei MaxSize
; /**< Max possible texture size */
198 GLboolean NPOT
; /**< Non-power of two size OK? */
199 GLsizei Width
, Height
; /**< Current texture size */
201 GLfloat Sright
, Ttop
; /**< right, top texcoords */
206 * State for glBlitFramebufer()
217 * State for glClear()
227 * State for glCopyPixels()
237 * State for glDrawPixels()
243 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
244 GLuint DepthFP
; /**< Fragment program for drawing depth images */
249 * State for glBitmap()
255 struct temp_texture Tex
; /**< separate texture from other meta ops */
260 * State for _mesa_meta_generate_mipmap()
262 struct gen_mipmap_state
271 * All per-context meta state.
275 struct save_state Save
; /**< state saved during meta-ops */
277 struct temp_texture TempTex
;
279 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
280 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
281 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
282 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
283 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
284 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
289 * Initialize meta-ops for a context.
290 * To be called once during context creation.
293 _mesa_meta_init(struct gl_context
*ctx
)
297 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
302 * Free context meta-op state.
303 * To be called once during context destruction.
306 _mesa_meta_free(struct gl_context
*ctx
)
308 /* Note: Any textures, VBOs, etc, that we allocate should get
309 * freed by the normal context destruction code. But this would be
310 * the place to free other meta data someday.
318 * Enter meta state. This is like a light-weight version of glPushAttrib
319 * but it also resets most GL state back to default values.
321 * \param state bitmask of META_* flags indicating which attribute groups
322 * to save and reset to their defaults
325 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
327 struct save_state
*save
= &ctx
->Meta
->Save
;
329 save
->SavedState
= state
;
331 if (state
& META_ALPHA_TEST
) {
332 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
333 save
->AlphaFunc
= ctx
->Color
.AlphaFunc
;
334 save
->AlphaRef
= ctx
->Color
.AlphaRef
;
335 if (ctx
->Color
.AlphaEnabled
)
336 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
339 if (state
& META_BLEND
) {
340 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
341 if (ctx
->Color
.BlendEnabled
) {
342 if (ctx
->Extensions
.EXT_draw_buffers2
) {
344 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
345 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
349 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
352 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
353 if (ctx
->Color
.ColorLogicOpEnabled
)
354 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
357 if (state
& META_COLOR_MASK
) {
358 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
359 sizeof(ctx
->Color
.ColorMask
));
360 if (!ctx
->Color
.ColorMask
[0][0] ||
361 !ctx
->Color
.ColorMask
[0][1] ||
362 !ctx
->Color
.ColorMask
[0][2] ||
363 !ctx
->Color
.ColorMask
[0][3])
364 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
367 if (state
& META_DEPTH_TEST
) {
368 save
->Depth
= ctx
->Depth
; /* struct copy */
370 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
373 if (state
& META_FOG
) {
374 save
->Fog
= ctx
->Fog
.Enabled
;
375 if (ctx
->Fog
.Enabled
)
376 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
379 if (state
& META_PIXEL_STORE
) {
380 save
->Pack
= ctx
->Pack
;
381 save
->Unpack
= ctx
->Unpack
;
382 ctx
->Pack
= ctx
->DefaultPacking
;
383 ctx
->Unpack
= ctx
->DefaultPacking
;
386 if (state
& META_PIXEL_TRANSFER
) {
387 save
->RedScale
= ctx
->Pixel
.RedScale
;
388 save
->RedBias
= ctx
->Pixel
.RedBias
;
389 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
390 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
391 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
392 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
393 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
394 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
395 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
396 ctx
->Pixel
.RedScale
= 1.0F
;
397 ctx
->Pixel
.RedBias
= 0.0F
;
398 ctx
->Pixel
.GreenScale
= 1.0F
;
399 ctx
->Pixel
.GreenBias
= 0.0F
;
400 ctx
->Pixel
.BlueScale
= 1.0F
;
401 ctx
->Pixel
.BlueBias
= 0.0F
;
402 ctx
->Pixel
.AlphaScale
= 1.0F
;
403 ctx
->Pixel
.AlphaBias
= 0.0F
;
404 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
406 ctx
->NewState
|=_NEW_PIXEL
;
409 if (state
& META_RASTERIZATION
) {
410 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
411 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
412 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
413 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
414 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
415 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
416 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
417 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
418 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
419 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
420 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
423 if (state
& META_SCISSOR
) {
424 save
->Scissor
= ctx
->Scissor
; /* struct copy */
425 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
428 if (state
& META_SHADER
) {
429 if (ctx
->Extensions
.ARB_vertex_program
) {
430 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
431 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
432 ctx
->VertexProgram
.Current
);
433 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
436 if (ctx
->Extensions
.ARB_fragment_program
) {
437 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
438 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
439 ctx
->FragmentProgram
.Current
);
440 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
443 if (ctx
->Extensions
.ARB_shader_objects
) {
444 _mesa_reference_shader_program(ctx
, &save
->VertexShader
,
445 ctx
->Shader
.CurrentVertexProgram
);
446 _mesa_reference_shader_program(ctx
, &save
->GeometryShader
,
447 ctx
->Shader
.CurrentGeometryProgram
);
448 _mesa_reference_shader_program(ctx
, &save
->FragmentShader
,
449 ctx
->Shader
.CurrentFragmentProgram
);
450 _mesa_reference_shader_program(ctx
, &save
->ActiveShader
,
451 ctx
->Shader
.CurrentFragmentProgram
);
453 _mesa_UseProgramObjectARB(0);
457 if (state
& META_STENCIL_TEST
) {
458 save
->Stencil
= ctx
->Stencil
; /* struct copy */
459 if (ctx
->Stencil
.Enabled
)
460 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
461 /* NOTE: other stencil state not reset */
464 if (state
& META_TEXTURE
) {
467 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
468 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
469 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
471 /* Disable all texture units */
472 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
473 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
474 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
475 if (ctx
->Texture
.Unit
[u
].Enabled
||
476 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
477 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
478 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
479 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
480 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
481 if (ctx
->Extensions
.ARB_texture_cube_map
)
482 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
483 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
484 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
485 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
486 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
487 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
491 /* save current texture objects for unit[0] only */
492 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
493 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
494 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
497 /* set defaults for unit[0] */
498 _mesa_ActiveTextureARB(GL_TEXTURE0
);
499 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
500 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
503 if (state
& META_TRANSFORM
) {
504 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
505 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
506 16 * sizeof(GLfloat
));
507 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
508 16 * sizeof(GLfloat
));
509 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
510 16 * sizeof(GLfloat
));
511 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
512 /* set 1:1 vertex:pixel coordinate transform */
513 _mesa_ActiveTextureARB(GL_TEXTURE0
);
514 _mesa_MatrixMode(GL_TEXTURE
);
515 _mesa_LoadIdentity();
516 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
517 _mesa_MatrixMode(GL_MODELVIEW
);
518 _mesa_LoadIdentity();
519 _mesa_MatrixMode(GL_PROJECTION
);
520 _mesa_LoadIdentity();
521 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
522 0.0, ctx
->DrawBuffer
->Height
,
524 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
525 if (ctx
->Transform
.ClipPlanesEnabled
) {
527 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
528 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
533 if (state
& META_VERTEX
) {
534 /* save vertex array object state */
535 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
536 ctx
->Array
.ArrayObj
);
537 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
538 ctx
->Array
.ArrayBufferObj
);
539 /* set some default state? */
542 if (state
& META_VIEWPORT
) {
543 /* save viewport state */
544 save
->ViewportX
= ctx
->Viewport
.X
;
545 save
->ViewportY
= ctx
->Viewport
.Y
;
546 save
->ViewportW
= ctx
->Viewport
.Width
;
547 save
->ViewportH
= ctx
->Viewport
.Height
;
548 /* set viewport to match window size */
549 if (ctx
->Viewport
.X
!= 0 ||
550 ctx
->Viewport
.Y
!= 0 ||
551 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
552 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
553 _mesa_set_viewport(ctx
, 0, 0,
554 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
556 /* save depth range state */
557 save
->DepthNear
= ctx
->Viewport
.Near
;
558 save
->DepthFar
= ctx
->Viewport
.Far
;
559 /* set depth range to default */
560 _mesa_DepthRange(0.0, 1.0);
565 save
->Lighting
= ctx
->Light
.Enabled
;
566 if (ctx
->Light
.Enabled
)
567 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
573 * Leave meta state. This is like a light-weight version of glPopAttrib().
576 _mesa_meta_end(struct gl_context
*ctx
)
578 struct save_state
*save
= &ctx
->Meta
->Save
;
579 const GLbitfield state
= save
->SavedState
;
581 if (state
& META_ALPHA_TEST
) {
582 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
583 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
584 _mesa_AlphaFunc(save
->AlphaFunc
, save
->AlphaRef
);
587 if (state
& META_BLEND
) {
588 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
589 if (ctx
->Extensions
.EXT_draw_buffers2
) {
591 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
592 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
596 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
599 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
600 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
603 if (state
& META_COLOR_MASK
) {
605 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
606 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
608 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
609 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
612 _mesa_ColorMaskIndexed(i
,
613 save
->ColorMask
[i
][0],
614 save
->ColorMask
[i
][1],
615 save
->ColorMask
[i
][2],
616 save
->ColorMask
[i
][3]);
622 if (state
& META_DEPTH_TEST
) {
623 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
624 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
625 _mesa_DepthFunc(save
->Depth
.Func
);
626 _mesa_DepthMask(save
->Depth
.Mask
);
629 if (state
& META_FOG
) {
630 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
633 if (state
& META_PIXEL_STORE
) {
634 ctx
->Pack
= save
->Pack
;
635 ctx
->Unpack
= save
->Unpack
;
638 if (state
& META_PIXEL_TRANSFER
) {
639 ctx
->Pixel
.RedScale
= save
->RedScale
;
640 ctx
->Pixel
.RedBias
= save
->RedBias
;
641 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
642 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
643 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
644 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
645 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
646 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
647 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
649 ctx
->NewState
|=_NEW_PIXEL
;
652 if (state
& META_RASTERIZATION
) {
653 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
654 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
655 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
656 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
657 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
658 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
661 if (state
& META_SCISSOR
) {
662 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
663 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
664 save
->Scissor
.Width
, save
->Scissor
.Height
);
667 if (state
& META_SHADER
) {
668 if (ctx
->Extensions
.ARB_vertex_program
) {
669 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
670 save
->VertexProgramEnabled
);
671 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
672 save
->VertexProgram
);
673 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
676 if (ctx
->Extensions
.ARB_fragment_program
) {
677 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
678 save
->FragmentProgramEnabled
);
679 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
680 save
->FragmentProgram
);
681 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
684 if (ctx
->Extensions
.ARB_vertex_shader
)
685 _mesa_use_shader_program(ctx
, GL_VERTEX_SHADER
, save
->VertexShader
);
687 if (ctx
->Extensions
.ARB_geometry_shader4
)
688 _mesa_use_shader_program(ctx
, GL_GEOMETRY_SHADER_ARB
,
689 save
->GeometryShader
);
691 if (ctx
->Extensions
.ARB_fragment_shader
)
692 _mesa_use_shader_program(ctx
, GL_FRAGMENT_SHADER
,
693 save
->FragmentShader
);
695 _mesa_reference_shader_program(ctx
, &ctx
->Shader
.ActiveProgram
,
699 if (state
& META_STENCIL_TEST
) {
700 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
702 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
703 _mesa_ClearStencil(stencil
->Clear
);
704 if (ctx
->Extensions
.EXT_stencil_two_side
) {
705 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
706 stencil
->TestTwoSide
);
707 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
708 ? GL_BACK
: GL_FRONT
);
711 _mesa_StencilFuncSeparate(GL_FRONT
,
712 stencil
->Function
[0],
714 stencil
->ValueMask
[0]);
715 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
716 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
717 stencil
->ZFailFunc
[0],
718 stencil
->ZPassFunc
[0]);
720 _mesa_StencilFuncSeparate(GL_BACK
,
721 stencil
->Function
[1],
723 stencil
->ValueMask
[1]);
724 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
725 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
726 stencil
->ZFailFunc
[1],
727 stencil
->ZPassFunc
[1]);
730 if (state
& META_TEXTURE
) {
733 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
735 /* restore texenv for unit[0] */
736 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
738 /* restore texture objects for unit[0] only */
739 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
740 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
741 save
->CurrentTexture
[tgt
]);
742 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
745 /* Re-enable textures, texgen */
746 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
747 if (save
->TexEnabled
[u
]) {
748 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
750 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
751 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
752 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
753 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
754 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
755 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
756 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
757 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
758 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
759 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
762 if (save
->TexGenEnabled
[u
]) {
763 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
765 if (save
->TexGenEnabled
[u
] & S_BIT
)
766 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
767 if (save
->TexGenEnabled
[u
] & T_BIT
)
768 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
769 if (save
->TexGenEnabled
[u
] & R_BIT
)
770 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
771 if (save
->TexGenEnabled
[u
] & Q_BIT
)
772 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
776 /* restore current unit state */
777 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
778 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
781 if (state
& META_TRANSFORM
) {
782 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
783 _mesa_ActiveTextureARB(GL_TEXTURE0
);
784 _mesa_MatrixMode(GL_TEXTURE
);
785 _mesa_LoadMatrixf(save
->TextureMatrix
);
786 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
788 _mesa_MatrixMode(GL_MODELVIEW
);
789 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
791 _mesa_MatrixMode(GL_PROJECTION
);
792 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
794 _mesa_MatrixMode(save
->MatrixMode
);
796 if (save
->ClipPlanesEnabled
) {
798 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
799 if (save
->ClipPlanesEnabled
& (1 << i
)) {
800 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
806 if (state
& META_VERTEX
) {
807 /* restore vertex buffer object */
808 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
809 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
811 /* restore vertex array object */
812 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
813 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
816 if (state
& META_VIEWPORT
) {
817 if (save
->ViewportX
!= ctx
->Viewport
.X
||
818 save
->ViewportY
!= ctx
->Viewport
.Y
||
819 save
->ViewportW
!= ctx
->Viewport
.Width
||
820 save
->ViewportH
!= ctx
->Viewport
.Height
) {
821 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
822 save
->ViewportW
, save
->ViewportH
);
824 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
828 if (save
->Lighting
) {
829 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
835 * Convert Z from a normalized value in the range [0, 1] to an object-space
836 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
837 * default/identity ortho projection results in the original Z value.
838 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
839 * value comes from the clear value or raster position.
841 static INLINE GLfloat
842 invert_z(GLfloat normZ
)
844 GLfloat objZ
= 1.0 - 2.0 * normZ
;
850 * One-time init for a temp_texture object.
851 * Choose tex target, compute max tex size, etc.
854 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
856 /* prefer texture rectangle */
857 if (ctx
->Extensions
.NV_texture_rectangle
) {
858 tex
->Target
= GL_TEXTURE_RECTANGLE
;
859 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
863 /* use 2D texture, NPOT if possible */
864 tex
->Target
= GL_TEXTURE_2D
;
865 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
866 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
868 tex
->MinSize
= 16; /* 16 x 16 at least */
869 assert(tex
->MaxSize
> 0);
871 _mesa_GenTextures(1, &tex
->TexObj
);
876 * Return pointer to temp_texture info for non-bitmap ops.
877 * This does some one-time init if needed.
879 static struct temp_texture
*
880 get_temp_texture(struct gl_context
*ctx
)
882 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
885 init_temp_texture(ctx
, tex
);
893 * Return pointer to temp_texture info for _mesa_meta_bitmap().
894 * We use a separate texture for bitmaps to reduce texture
895 * allocation/deallocation.
897 static struct temp_texture
*
898 get_bitmap_temp_texture(struct gl_context
*ctx
)
900 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
903 init_temp_texture(ctx
, tex
);
911 * Compute the width/height of texture needed to draw an image of the
912 * given size. Return a flag indicating whether the current texture
913 * can be re-used (glTexSubImage2D) or if a new texture needs to be
914 * allocated (glTexImage2D).
915 * Also, compute s/t texcoords for drawing.
917 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
920 alloc_texture(struct temp_texture
*tex
,
921 GLsizei width
, GLsizei height
, GLenum intFormat
)
923 GLboolean newTex
= GL_FALSE
;
925 ASSERT(width
<= tex
->MaxSize
);
926 ASSERT(height
<= tex
->MaxSize
);
928 if (width
> tex
->Width
||
929 height
> tex
->Height
||
930 intFormat
!= tex
->IntFormat
) {
931 /* alloc new texture (larger or different format) */
934 /* use non-power of two size */
935 tex
->Width
= MAX2(tex
->MinSize
, width
);
936 tex
->Height
= MAX2(tex
->MinSize
, height
);
939 /* find power of two size */
941 w
= h
= tex
->MinSize
;
950 tex
->IntFormat
= intFormat
;
955 /* compute texcoords */
956 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
957 tex
->Sright
= (GLfloat
) width
;
958 tex
->Ttop
= (GLfloat
) height
;
961 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
962 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
970 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
973 setup_copypix_texture(struct temp_texture
*tex
,
975 GLint srcX
, GLint srcY
,
976 GLsizei width
, GLsizei height
, GLenum intFormat
,
979 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
980 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
981 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
982 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
984 /* copy framebuffer image to texture */
986 /* create new tex image */
987 if (tex
->Width
== width
&& tex
->Height
== height
) {
988 /* create new tex with framebuffer data */
989 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
990 srcX
, srcY
, width
, height
, 0);
993 /* create empty texture */
994 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
995 tex
->Width
, tex
->Height
, 0,
996 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
998 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
999 0, 0, srcX
, srcY
, width
, height
);
1003 /* replace existing tex image */
1004 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
1005 0, 0, srcX
, srcY
, width
, height
);
1011 * Setup/load texture for glDrawPixels.
1014 setup_drawpix_texture(struct gl_context
*ctx
,
1015 struct temp_texture
*tex
,
1017 GLenum texIntFormat
,
1018 GLsizei width
, GLsizei height
,
1019 GLenum format
, GLenum type
,
1020 const GLvoid
*pixels
)
1022 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1023 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1024 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1025 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1027 /* copy pixel data to texture */
1029 /* create new tex image */
1030 if (tex
->Width
== width
&& tex
->Height
== height
) {
1031 /* create new tex and load image data */
1032 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1033 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1036 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1038 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1039 ctx
->Unpack
.BufferObj
);
1040 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1041 /* create empty texture */
1042 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1043 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1044 if (save_unpack_obj
!= NULL
)
1045 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1046 save_unpack_obj
->Name
);
1048 _mesa_TexSubImage2D(tex
->Target
, 0,
1049 0, 0, width
, height
, format
, type
, pixels
);
1053 /* replace existing tex image */
1054 _mesa_TexSubImage2D(tex
->Target
, 0,
1055 0, 0, width
, height
, format
, type
, pixels
);
1062 * One-time init for drawing depth pixels.
1065 init_blit_depth_pixels(struct gl_context
*ctx
)
1067 static const char *program
=
1069 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1072 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1073 struct temp_texture
*tex
= get_temp_texture(ctx
);
1074 const char *texTarget
;
1076 assert(blit
->DepthFP
== 0);
1078 /* replace %s with "RECT" or "2D" */
1079 assert(strlen(program
) + 4 < sizeof(program2
));
1080 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1084 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1086 _mesa_GenPrograms(1, &blit
->DepthFP
);
1087 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1088 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1089 strlen(program2
), (const GLubyte
*) program2
);
1094 * Try to do a glBlitFramebuffer using no-copy texturing.
1095 * We can do this when the src renderbuffer is actually a texture.
1096 * But if the src buffer == dst buffer we cannot do this.
1098 * \return new buffer mask indicating the buffers left to blit using the
1102 blitframebuffer_texture(struct gl_context
*ctx
,
1103 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1104 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1105 GLbitfield mask
, GLenum filter
)
1107 if (mask
& GL_COLOR_BUFFER_BIT
) {
1108 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1109 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1110 const struct gl_renderbuffer_attachment
*drawAtt
=
1111 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1112 const struct gl_renderbuffer_attachment
*readAtt
=
1113 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1115 if (readAtt
&& readAtt
->Texture
) {
1116 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1117 const GLuint srcLevel
= readAtt
->TextureLevel
;
1118 const GLenum minFilterSave
= texObj
->MinFilter
;
1119 const GLenum magFilterSave
= texObj
->MagFilter
;
1120 const GLint baseLevelSave
= texObj
->BaseLevel
;
1121 const GLint maxLevelSave
= texObj
->MaxLevel
;
1122 const GLenum wrapSSave
= texObj
->WrapS
;
1123 const GLenum wrapTSave
= texObj
->WrapT
;
1124 const GLenum target
= texObj
->Target
;
1126 if (drawAtt
->Texture
== readAtt
->Texture
) {
1127 /* Can't use same texture as both the source and dest. We need
1128 * to handle overlapping blits and besides, some hw may not
1134 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1135 /* Can't handle other texture types at this time */
1140 printf("Blit from texture!\n");
1141 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1142 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1145 /* Prepare src texture state */
1146 _mesa_BindTexture(target
, texObj
->Name
);
1147 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1148 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1149 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1150 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1151 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1153 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1154 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1155 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1156 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1158 /* Prepare vertex data (the VBO was previously created and bound) */
1163 struct vertex verts
[4];
1164 GLfloat s0
, t0
, s1
, t1
;
1166 if (target
== GL_TEXTURE_2D
) {
1167 const struct gl_texture_image
*texImage
1168 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1169 s0
= srcX0
/ (float) texImage
->Width
;
1170 s1
= srcX1
/ (float) texImage
->Width
;
1171 t0
= srcY0
/ (float) texImage
->Height
;
1172 t1
= srcY1
/ (float) texImage
->Height
;
1175 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1182 verts
[0].x
= (GLfloat
) dstX0
;
1183 verts
[0].y
= (GLfloat
) dstY0
;
1184 verts
[1].x
= (GLfloat
) dstX1
;
1185 verts
[1].y
= (GLfloat
) dstY0
;
1186 verts
[2].x
= (GLfloat
) dstX1
;
1187 verts
[2].y
= (GLfloat
) dstY1
;
1188 verts
[3].x
= (GLfloat
) dstX0
;
1189 verts
[3].y
= (GLfloat
) dstY1
;
1200 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1203 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1205 /* Restore texture object state, the texture binding will
1206 * be restored by _mesa_meta_end().
1208 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1209 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1210 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1211 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1212 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1214 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1215 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1217 /* Done with color buffer */
1218 mask
&= ~GL_COLOR_BUFFER_BIT
;
1227 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1228 * of texture mapping and polygon rendering.
1231 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1232 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1233 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1234 GLbitfield mask
, GLenum filter
)
1236 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1237 struct temp_texture
*tex
= get_temp_texture(ctx
);
1238 const GLsizei maxTexSize
= tex
->MaxSize
;
1239 const GLint srcX
= MIN2(srcX0
, srcX1
);
1240 const GLint srcY
= MIN2(srcY0
, srcY1
);
1241 const GLint srcW
= abs(srcX1
- srcX0
);
1242 const GLint srcH
= abs(srcY1
- srcY0
);
1243 const GLboolean srcFlipX
= srcX1
< srcX0
;
1244 const GLboolean srcFlipY
= srcY1
< srcY0
;
1248 struct vertex verts
[4];
1251 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1252 /* XXX avoid this fallback */
1253 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1254 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1270 /* only scissor effects blit so save/clear all other relevant state */
1271 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1273 if (blit
->ArrayObj
== 0) {
1274 /* one-time setup */
1276 /* create vertex array object */
1277 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1278 _mesa_BindVertexArray(blit
->ArrayObj
);
1280 /* create vertex array buffer */
1281 _mesa_GenBuffersARB(1, &blit
->VBO
);
1282 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1283 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1284 NULL
, GL_DYNAMIC_DRAW_ARB
);
1286 /* setup vertex arrays */
1287 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1288 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1289 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1290 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1293 _mesa_BindVertexArray(blit
->ArrayObj
);
1294 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1297 /* Try faster, direct texture approach first */
1298 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1299 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1301 _mesa_meta_end(ctx
);
1305 /* Continue with "normal" approach which involves copying the src rect
1306 * into a temporary texture and is "blitted" by drawing a textured quad.
1309 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1311 /* vertex positions/texcoords (after texture allocation!) */
1313 verts
[0].x
= (GLfloat
) dstX0
;
1314 verts
[0].y
= (GLfloat
) dstY0
;
1315 verts
[1].x
= (GLfloat
) dstX1
;
1316 verts
[1].y
= (GLfloat
) dstY0
;
1317 verts
[2].x
= (GLfloat
) dstX1
;
1318 verts
[2].y
= (GLfloat
) dstY1
;
1319 verts
[3].x
= (GLfloat
) dstX0
;
1320 verts
[3].y
= (GLfloat
) dstY1
;
1324 verts
[1].s
= tex
->Sright
;
1326 verts
[2].s
= tex
->Sright
;
1327 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 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1337 if (mask
& GL_COLOR_BUFFER_BIT
) {
1338 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1340 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1341 mask
&= ~GL_COLOR_BUFFER_BIT
;
1344 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1345 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1348 init_blit_depth_pixels(ctx
);
1350 /* maybe change tex format here */
1351 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1353 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1354 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1356 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1357 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1359 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1360 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1361 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1362 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1363 _mesa_DepthFunc(GL_ALWAYS
);
1364 _mesa_DepthMask(GL_TRUE
);
1366 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1367 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1373 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1374 /* XXX can't easily do stencil */
1377 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1379 _mesa_meta_end(ctx
);
1382 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1383 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1389 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1392 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1394 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1396 GLfloat x
, y
, z
, r
, g
, b
, a
;
1398 struct vertex verts
[4];
1399 /* save all state but scissor, pixel pack/unpack */
1400 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1402 if (buffers
& BUFFER_BITS_COLOR
) {
1403 /* if clearing color buffers, don't save/restore colormask */
1404 metaSave
-= META_COLOR_MASK
;
1407 _mesa_meta_begin(ctx
, metaSave
);
1409 if (clear
->ArrayObj
== 0) {
1410 /* one-time setup */
1412 /* create vertex array object */
1413 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1414 _mesa_BindVertexArray(clear
->ArrayObj
);
1416 /* create vertex array buffer */
1417 _mesa_GenBuffersARB(1, &clear
->VBO
);
1418 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1420 /* setup vertex arrays */
1421 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1422 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1423 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1424 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1427 _mesa_BindVertexArray(clear
->ArrayObj
);
1428 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1431 /* GL_COLOR_BUFFER_BIT */
1432 if (buffers
& BUFFER_BITS_COLOR
) {
1433 /* leave colormask, glDrawBuffer state as-is */
1436 ASSERT(metaSave
& META_COLOR_MASK
);
1437 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1440 /* GL_DEPTH_BUFFER_BIT */
1441 if (buffers
& BUFFER_BIT_DEPTH
) {
1442 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1443 _mesa_DepthFunc(GL_ALWAYS
);
1444 _mesa_DepthMask(GL_TRUE
);
1447 assert(!ctx
->Depth
.Test
);
1450 /* GL_STENCIL_BUFFER_BIT */
1451 if (buffers
& BUFFER_BIT_STENCIL
) {
1452 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1453 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1454 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1455 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1456 ctx
->Stencil
.Clear
& 0x7fffffff,
1457 ctx
->Stencil
.WriteMask
[0]);
1460 assert(!ctx
->Stencil
.Enabled
);
1463 /* vertex positions/colors */
1465 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1466 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1467 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1468 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1469 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1486 for (i
= 0; i
< 4; i
++) {
1487 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1488 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1489 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1490 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1493 /* upload new vertex data */
1494 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1495 GL_DYNAMIC_DRAW_ARB
);
1499 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1501 _mesa_meta_end(ctx
);
1506 * Meta implementation of ctx->Driver.CopyPixels() in terms
1507 * of texture mapping and polygon rendering.
1510 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1511 GLsizei width
, GLsizei height
,
1512 GLint dstX
, GLint dstY
, GLenum type
)
1514 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1515 struct temp_texture
*tex
= get_temp_texture(ctx
);
1517 GLfloat x
, y
, z
, s
, t
;
1519 struct vertex verts
[4];
1521 GLenum intFormat
= GL_RGBA
;
1523 if (type
!= GL_COLOR
||
1524 ctx
->_ImageTransferState
||
1526 width
> tex
->MaxSize
||
1527 height
> tex
->MaxSize
) {
1528 /* XXX avoid this fallback */
1529 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1533 /* Most GL state applies to glCopyPixels, but a there's a few things
1534 * we need to override:
1536 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1543 if (copypix
->ArrayObj
== 0) {
1544 /* one-time setup */
1546 /* create vertex array object */
1547 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1548 _mesa_BindVertexArray(copypix
->ArrayObj
);
1550 /* create vertex array buffer */
1551 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1552 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1553 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1554 NULL
, GL_DYNAMIC_DRAW_ARB
);
1556 /* setup vertex arrays */
1557 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1558 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1559 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1560 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1563 _mesa_BindVertexArray(copypix
->ArrayObj
);
1564 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1567 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1569 /* vertex positions, texcoords (after texture allocation!) */
1571 const GLfloat dstX0
= (GLfloat
) dstX
;
1572 const GLfloat dstY0
= (GLfloat
) dstY
;
1573 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1574 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1575 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1585 verts
[1].s
= tex
->Sright
;
1590 verts
[2].s
= tex
->Sright
;
1591 verts
[2].t
= tex
->Ttop
;
1596 verts
[3].t
= tex
->Ttop
;
1598 /* upload new vertex data */
1599 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1602 /* Alloc/setup texture */
1603 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1604 GL_RGBA
, GL_NEAREST
);
1606 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1608 /* draw textured quad */
1609 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1611 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1613 _mesa_meta_end(ctx
);
1619 * When the glDrawPixels() image size is greater than the max rectangle
1620 * texture size we use this function to break the glDrawPixels() image
1621 * into tiles which fit into the max texture size.
1624 tiled_draw_pixels(struct gl_context
*ctx
,
1626 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1627 GLenum format
, GLenum type
,
1628 const struct gl_pixelstore_attrib
*unpack
,
1629 const GLvoid
*pixels
)
1631 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1634 if (tileUnpack
.RowLength
== 0)
1635 tileUnpack
.RowLength
= width
;
1637 for (i
= 0; i
< width
; i
+= tileSize
) {
1638 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1639 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1641 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1643 for (j
= 0; j
< height
; j
+= tileSize
) {
1644 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1645 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1647 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1649 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1650 format
, type
, &tileUnpack
, pixels
);
1657 * One-time init for drawing stencil pixels.
1660 init_draw_stencil_pixels(struct gl_context
*ctx
)
1662 /* This program is run eight times, once for each stencil bit.
1663 * The stencil values to draw are found in an 8-bit alpha texture.
1664 * We read the texture/stencil value and test if bit 'b' is set.
1665 * If the bit is not set, use KIL to kill the fragment.
1666 * Finally, we use the stencil test to update the stencil buffer.
1668 * The basic algorithm for checking if a bit is set is:
1669 * if (is_odd(value / (1 << bit)))
1670 * result is one (or non-zero).
1673 * The program parameter contains three values:
1674 * parm.x = 255 / (1 << bit)
1678 static const char *program
=
1680 "PARAM parm = program.local[0]; \n"
1682 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1683 "# t = t * 255 / bit \n"
1684 "MUL t.x, t.a, parm.x; \n"
1687 "SUB t.x, t.x, t.y; \n"
1689 "MUL t.x, t.x, parm.y; \n"
1690 "# t = fract(t.x) \n"
1691 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1692 "# t.x = (t.x == 0 ? 1 : 0) \n"
1693 "SGE t.x, -t.x, parm.z; \n"
1695 "# for debug only \n"
1696 "#MOV result.color, t.x; \n"
1698 char program2
[1000];
1699 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1700 struct temp_texture
*tex
= get_temp_texture(ctx
);
1701 const char *texTarget
;
1703 assert(drawpix
->StencilFP
== 0);
1705 /* replace %s with "RECT" or "2D" */
1706 assert(strlen(program
) + 4 < sizeof(program2
));
1707 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1711 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1713 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1714 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1715 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1716 strlen(program2
), (const GLubyte
*) program2
);
1721 * One-time init for drawing depth pixels.
1724 init_draw_depth_pixels(struct gl_context
*ctx
)
1726 static const char *program
=
1728 "PARAM color = program.local[0]; \n"
1729 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1730 "MOV result.color, color; \n"
1733 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1734 struct temp_texture
*tex
= get_temp_texture(ctx
);
1735 const char *texTarget
;
1737 assert(drawpix
->DepthFP
== 0);
1739 /* replace %s with "RECT" or "2D" */
1740 assert(strlen(program
) + 4 < sizeof(program2
));
1741 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1745 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1747 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1748 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1749 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1750 strlen(program2
), (const GLubyte
*) program2
);
1755 * Meta implementation of ctx->Driver.DrawPixels() in terms
1756 * of texture mapping and polygon rendering.
1759 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
1760 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1761 GLenum format
, GLenum type
,
1762 const struct gl_pixelstore_attrib
*unpack
,
1763 const GLvoid
*pixels
)
1765 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1766 struct temp_texture
*tex
= get_temp_texture(ctx
);
1767 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1768 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1770 GLfloat x
, y
, z
, s
, t
;
1772 struct vertex verts
[4];
1773 GLenum texIntFormat
;
1774 GLboolean fallback
, newTex
;
1775 GLbitfield metaExtraSave
= 0x0;
1779 * Determine if we can do the glDrawPixels with texture mapping.
1781 fallback
= GL_FALSE
;
1782 if (ctx
->_ImageTransferState
||
1787 if (_mesa_is_color_format(format
)) {
1788 /* use more compact format when possible */
1789 /* XXX disable special case for GL_LUMINANCE for now to work around
1790 * apparent i965 driver bug (see bug #23670).
1792 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1793 texIntFormat
= format
;
1795 texIntFormat
= GL_RGBA
;
1797 else if (_mesa_is_stencil_format(format
)) {
1798 if (ctx
->Extensions
.ARB_fragment_program
&&
1799 ctx
->Pixel
.IndexShift
== 0 &&
1800 ctx
->Pixel
.IndexOffset
== 0 &&
1801 type
== GL_UNSIGNED_BYTE
) {
1802 /* We'll store stencil as alpha. This only works for GLubyte
1803 * image data because of how incoming values are mapped to alpha
1806 texIntFormat
= GL_ALPHA
;
1807 metaExtraSave
= (META_COLOR_MASK
|
1816 else if (_mesa_is_depth_format(format
)) {
1817 if (ctx
->Extensions
.ARB_depth_texture
&&
1818 ctx
->Extensions
.ARB_fragment_program
) {
1819 texIntFormat
= GL_DEPTH_COMPONENT
;
1820 metaExtraSave
= (META_SHADER
);
1831 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1832 format
, type
, unpack
, pixels
);
1837 * Check image size against max texture size, draw as tiles if needed.
1839 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1840 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1841 format
, type
, unpack
, pixels
);
1845 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1846 * but a there's a few things we need to override:
1848 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1856 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1858 /* vertex positions, texcoords (after texture allocation!) */
1860 const GLfloat x0
= (GLfloat
) x
;
1861 const GLfloat y0
= (GLfloat
) y
;
1862 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1863 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1864 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1874 verts
[1].s
= tex
->Sright
;
1879 verts
[2].s
= tex
->Sright
;
1880 verts
[2].t
= tex
->Ttop
;
1885 verts
[3].t
= tex
->Ttop
;
1888 if (drawpix
->ArrayObj
== 0) {
1889 /* one-time setup: create vertex array object */
1890 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1892 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1894 /* create vertex array buffer */
1895 _mesa_GenBuffersARB(1, &vbo
);
1896 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1897 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1898 verts
, GL_DYNAMIC_DRAW_ARB
);
1900 /* setup vertex arrays */
1901 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1902 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1903 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1904 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1906 /* set given unpack params */
1907 ctx
->Unpack
= *unpack
;
1909 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1911 if (_mesa_is_stencil_format(format
)) {
1912 /* Drawing stencil */
1915 if (!drawpix
->StencilFP
)
1916 init_draw_stencil_pixels(ctx
);
1918 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1919 GL_ALPHA
, type
, pixels
);
1921 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1923 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1925 /* set all stencil bits to 0 */
1926 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1927 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1928 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1930 /* set stencil bits to 1 where needed */
1931 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1933 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1934 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1936 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1937 const GLuint mask
= 1 << bit
;
1938 if (mask
& origStencilMask
) {
1939 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1940 _mesa_StencilMask(mask
);
1942 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1943 255.0 / mask
, 0.5, 0.0, 0.0);
1945 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1949 else if (_mesa_is_depth_format(format
)) {
1951 if (!drawpix
->DepthFP
)
1952 init_draw_depth_pixels(ctx
);
1954 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1955 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1957 /* polygon color = current raster color */
1958 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1959 ctx
->Current
.RasterColor
);
1961 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1962 format
, type
, pixels
);
1964 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1968 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1969 format
, type
, pixels
);
1970 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1973 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1975 _mesa_DeleteBuffersARB(1, &vbo
);
1977 /* restore unpack params */
1978 ctx
->Unpack
= unpackSave
;
1980 _mesa_meta_end(ctx
);
1985 * Do glBitmap with a alpha texture quad. Use the alpha test to
1986 * cull the 'off' bits. If alpha test is already enabled, fall back
1987 * to swrast (should be a rare case).
1988 * A bitmap cache as in the gallium/mesa state tracker would
1989 * improve performance a lot.
1992 _mesa_meta_Bitmap(struct gl_context
*ctx
,
1993 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1994 const struct gl_pixelstore_attrib
*unpack
,
1995 const GLubyte
*bitmap1
)
1997 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1998 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1999 const GLenum texIntFormat
= GL_ALPHA
;
2000 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
2003 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
2005 struct vertex verts
[4];
2010 * Check if swrast fallback is needed.
2012 if (ctx
->_ImageTransferState
||
2013 ctx
->Color
.AlphaEnabled
||
2015 ctx
->Texture
._EnabledUnits
||
2016 width
> tex
->MaxSize
||
2017 height
> tex
->MaxSize
) {
2018 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2022 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2023 * but a there's a few things we need to override:
2025 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2027 META_RASTERIZATION
|
2034 if (bitmap
->ArrayObj
== 0) {
2035 /* one-time setup */
2037 /* create vertex array object */
2038 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2039 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2041 /* create vertex array buffer */
2042 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2043 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2044 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2045 NULL
, GL_DYNAMIC_DRAW_ARB
);
2047 /* setup vertex arrays */
2048 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2049 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2050 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2051 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2052 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2053 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2056 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2057 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2060 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2062 /* vertex positions, texcoords, colors (after texture allocation!) */
2064 const GLfloat x0
= (GLfloat
) x
;
2065 const GLfloat y0
= (GLfloat
) y
;
2066 const GLfloat x1
= (GLfloat
) (x
+ width
);
2067 const GLfloat y1
= (GLfloat
) (y
+ height
);
2068 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2079 verts
[1].s
= tex
->Sright
;
2084 verts
[2].s
= tex
->Sright
;
2085 verts
[2].t
= tex
->Ttop
;
2090 verts
[3].t
= tex
->Ttop
;
2092 for (i
= 0; i
< 4; i
++) {
2093 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2094 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2095 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2096 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2099 /* upload new vertex data */
2100 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2103 /* choose different foreground/background alpha values */
2104 CLAMPED_FLOAT_TO_UBYTE(fg
, ctx
->Current
.RasterColor
[ACOMP
]);
2105 bg
= (fg
> 127 ? 0 : 255);
2107 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2109 _mesa_meta_end(ctx
);
2113 bitmap8
= (GLubyte
*) malloc(width
* height
);
2115 memset(bitmap8
, bg
, width
* height
);
2116 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2117 bitmap8
, width
, fg
);
2119 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2121 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2122 _mesa_AlphaFunc(GL_NOTEQUAL
, UBYTE_TO_FLOAT(bg
));
2124 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2125 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2127 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2129 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2134 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2136 _mesa_meta_end(ctx
);
2141 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2142 * software fallback. The fallback path will require that the texture
2143 * images are mapped.
2144 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2147 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2148 struct gl_texture_object
*texObj
)
2150 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2151 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2152 struct gl_texture_image
*baseImage
;
2156 /* check for fallbacks */
2157 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2158 target
== GL_TEXTURE_3D
) {
2162 srcLevel
= texObj
->BaseLevel
;
2163 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2164 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2169 * Test that we can actually render in the texture's format.
2172 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2173 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2175 if (target
== GL_TEXTURE_1D
) {
2176 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2177 GL_COLOR_ATTACHMENT0_EXT
,
2178 target
, texObj
->Name
, srcLevel
);
2181 /* other work is needed to enable 3D mipmap generation */
2182 else if (target
== GL_TEXTURE_3D
) {
2184 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2185 GL_COLOR_ATTACHMENT0_EXT
,
2186 target
, texObj
->Name
, srcLevel
, zoffset
);
2191 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2192 GL_COLOR_ATTACHMENT0_EXT
,
2193 target
, texObj
->Name
, srcLevel
);
2196 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2198 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2200 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2209 * Called via ctx->Driver.GenerateMipmap()
2210 * Note: texture borders and 3D texture support not yet complete.
2213 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2214 struct gl_texture_object
*texObj
)
2216 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2218 GLfloat x
, y
, s
, t
, r
;
2220 struct vertex verts
[4];
2221 const GLuint baseLevel
= texObj
->BaseLevel
;
2222 const GLuint maxLevel
= texObj
->MaxLevel
;
2223 const GLenum minFilterSave
= texObj
->MinFilter
;
2224 const GLenum magFilterSave
= texObj
->MagFilter
;
2225 const GLint baseLevelSave
= texObj
->BaseLevel
;
2226 const GLint maxLevelSave
= texObj
->MaxLevel
;
2227 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2228 const GLenum wrapSSave
= texObj
->WrapS
;
2229 const GLenum wrapTSave
= texObj
->WrapT
;
2230 const GLenum wrapRSave
= texObj
->WrapR
;
2231 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2232 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2237 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2238 _mesa_generate_mipmap(ctx
, target
, texObj
);
2242 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2243 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2244 faceTarget
= target
;
2245 target
= GL_TEXTURE_CUBE_MAP
;
2248 faceTarget
= target
;
2251 _mesa_meta_begin(ctx
, META_ALL
);
2253 if (original_active_unit
!= 0)
2254 _mesa_BindTexture(target
, texObj
->Name
);
2256 if (mipmap
->ArrayObj
== 0) {
2257 /* one-time setup */
2259 /* create vertex array object */
2260 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2261 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2263 /* create vertex array buffer */
2264 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2265 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2266 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2267 NULL
, GL_DYNAMIC_DRAW_ARB
);
2269 /* setup vertex arrays */
2270 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2271 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2272 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2273 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2276 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2277 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2281 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2283 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2285 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
2286 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2287 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2288 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2289 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2290 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2292 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2294 /* setup texcoords once (XXX what about border?) */
2295 switch (faceTarget
) {
2317 static const GLfloat st
[4][2] = {
2318 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2322 /* loop over quad verts */
2323 for (i
= 0; i
< 4; i
++) {
2324 /* Compute sc = +/-scale and tc = +/-scale.
2325 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2326 * though that can still sometimes happen with this scale factor...
2328 const GLfloat scale
= 0.9999f
;
2329 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2330 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2332 switch (faceTarget
) {
2333 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2338 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2343 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2348 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2353 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2358 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2370 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2372 /* setup vertex positions */
2383 /* upload new vertex data */
2384 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2387 /* setup projection matrix */
2388 _mesa_MatrixMode(GL_PROJECTION
);
2389 _mesa_LoadIdentity();
2390 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2392 /* texture is already locked, unlock now */
2393 _mesa_unlock_texture(ctx
, texObj
);
2395 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2396 const struct gl_texture_image
*srcImage
;
2397 const GLuint srcLevel
= dstLevel
- 1;
2398 GLsizei srcWidth
, srcHeight
, srcDepth
;
2399 GLsizei dstWidth
, dstHeight
, dstDepth
;
2402 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2403 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2405 /* src size w/out border */
2406 srcWidth
= srcImage
->Width
- 2 * border
;
2407 srcHeight
= srcImage
->Height
- 2 * border
;
2408 srcDepth
= srcImage
->Depth
- 2 * border
;
2410 /* new dst size w/ border */
2411 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2412 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2413 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2415 if (dstWidth
== srcImage
->Width
&&
2416 dstHeight
== srcImage
->Height
&&
2417 dstDepth
== srcImage
->Depth
) {
2422 /* Set MaxLevel large enough to hold the new level when we allocate it */
2423 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2425 /* Create empty dest image */
2426 if (target
== GL_TEXTURE_1D
) {
2427 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2429 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2431 else if (target
== GL_TEXTURE_3D
) {
2432 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2433 dstWidth
, dstHeight
, dstDepth
, border
,
2434 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2438 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2439 dstWidth
, dstHeight
, border
,
2440 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2442 if (target
== GL_TEXTURE_CUBE_MAP
) {
2443 /* If texturing from a cube, we need to make sure all src faces
2444 * have been defined (even if we're not sampling from them.)
2445 * Otherwise the texture object will be 'incomplete' and
2446 * texturing from it will not be allowed.
2449 for (face
= 0; face
< 6; face
++) {
2450 if (!texObj
->Image
[face
][srcLevel
] ||
2451 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2452 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2453 srcLevel
, srcImage
->InternalFormat
,
2454 srcWidth
, srcHeight
, border
,
2455 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2461 /* limit sampling to src level */
2462 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2463 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2465 /* Set to draw into the current dstLevel */
2466 if (target
== GL_TEXTURE_1D
) {
2467 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2468 GL_COLOR_ATTACHMENT0_EXT
,
2473 else if (target
== GL_TEXTURE_3D
) {
2474 GLint zoffset
= 0; /* XXX unfinished */
2475 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2476 GL_COLOR_ATTACHMENT0_EXT
,
2483 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2484 GL_COLOR_ATTACHMENT0_EXT
,
2490 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2493 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2494 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2499 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2500 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2502 /* setup viewport */
2503 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2505 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2508 _mesa_lock_texture(ctx
, texObj
); /* relock */
2510 _mesa_meta_end(ctx
);
2512 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2513 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2514 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
2515 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2516 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2517 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2518 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2519 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2521 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2526 * Determine the GL data type to use for the temporary image read with
2527 * ReadPixels() and passed to Tex[Sub]Image().
2530 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2532 switch (baseFormat
) {
2537 case GL_LUMINANCE_ALPHA
:
2539 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2540 return GL_UNSIGNED_BYTE
;
2541 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2542 return GL_UNSIGNED_SHORT
;
2545 case GL_DEPTH_COMPONENT
:
2546 return GL_UNSIGNED_INT
;
2547 case GL_DEPTH_STENCIL
:
2548 return GL_UNSIGNED_INT_24_8
;
2550 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2557 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2558 * Have to be careful with locking and meta state for pixel transfer.
2561 copy_tex_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2562 GLenum internalFormat
, GLint x
, GLint y
,
2563 GLsizei width
, GLsizei height
, GLint border
)
2565 struct gl_texture_object
*texObj
;
2566 struct gl_texture_image
*texImage
;
2567 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2568 GLenum format
, type
;
2572 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2573 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2575 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2576 type
= get_temp_image_type(ctx
, format
);
2577 bpp
= _mesa_bytes_per_pixel(format
, type
);
2579 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2584 * Alloc image buffer (XXX could use a PBO)
2586 buf
= malloc(width
* height
* bpp
);
2588 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2592 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2595 * Read image from framebuffer (disable pixel transfer ops)
2597 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2598 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2599 format
, type
, &ctx
->Pack
, buf
);
2600 _mesa_meta_end(ctx
);
2602 if (texImage
->Data
) {
2603 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2606 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2607 postConvWidth
, postConvHeight
, 1,
2608 border
, internalFormat
);
2610 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2611 internalFormat
, GL_NONE
, GL_NONE
);
2614 * Store texture data (with pixel transfer ops)
2616 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2618 _mesa_update_state(ctx
); /* to update pixel transfer state */
2620 if (target
== GL_TEXTURE_1D
) {
2621 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2622 width
, border
, format
, type
,
2623 buf
, &ctx
->Unpack
, texObj
, texImage
);
2626 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2627 width
, height
, border
, format
, type
,
2628 buf
, &ctx
->Unpack
, texObj
, texImage
);
2630 _mesa_meta_end(ctx
);
2632 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2639 _mesa_meta_CopyTexImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2640 GLenum internalFormat
, GLint x
, GLint y
,
2641 GLsizei width
, GLint border
)
2643 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2649 _mesa_meta_CopyTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2650 GLenum internalFormat
, GLint x
, GLint y
,
2651 GLsizei width
, GLsizei height
, GLint border
)
2653 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2654 width
, height
, border
);
2660 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2661 * Have to be careful with locking and meta state for pixel transfer.
2664 copy_tex_sub_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2665 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2667 GLsizei width
, GLsizei height
)
2669 struct gl_texture_object
*texObj
;
2670 struct gl_texture_image
*texImage
;
2671 GLenum format
, type
;
2675 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2676 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2678 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2679 type
= get_temp_image_type(ctx
, format
);
2680 bpp
= _mesa_bytes_per_pixel(format
, type
);
2682 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2687 * Alloc image buffer (XXX could use a PBO)
2689 buf
= malloc(width
* height
* bpp
);
2691 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2695 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2698 * Read image from framebuffer (disable pixel transfer ops)
2700 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2701 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2702 format
, type
, &ctx
->Pack
, buf
);
2703 _mesa_meta_end(ctx
);
2705 _mesa_update_state(ctx
); /* to update pixel transfer state */
2708 * Store texture data (with pixel transfer ops)
2710 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2711 if (target
== GL_TEXTURE_1D
) {
2712 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2713 width
, format
, type
, buf
,
2714 &ctx
->Unpack
, texObj
, texImage
);
2716 else if (target
== GL_TEXTURE_3D
) {
2717 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2718 width
, height
, 1, format
, type
, buf
,
2719 &ctx
->Unpack
, texObj
, texImage
);
2722 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2723 width
, height
, format
, type
, buf
,
2724 &ctx
->Unpack
, texObj
, texImage
);
2726 _mesa_meta_end(ctx
);
2728 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2735 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2737 GLint x
, GLint y
, GLsizei width
)
2739 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2745 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2746 GLint xoffset
, GLint yoffset
,
2748 GLsizei width
, GLsizei height
)
2750 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2751 x
, y
, width
, height
);
2756 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2757 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2759 GLsizei width
, GLsizei height
)
2761 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2762 x
, y
, width
, height
);
2767 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2768 GLenum target
, GLenum internalformat
,
2769 GLint x
, GLint y
, GLsizei width
)
2773 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2775 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2780 * Read image from framebuffer (disable pixel transfer ops)
2782 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2783 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2784 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2786 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2788 _mesa_meta_end(ctx
);
2795 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2796 GLint x
, GLint y
, GLsizei width
)
2800 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2802 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2807 * Read image from framebuffer (disable pixel transfer ops)
2809 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2810 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2811 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2813 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2815 _mesa_meta_end(ctx
);