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/colortab.h"
42 #include "main/convolve.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/shaders.h"
55 #include "main/state.h"
56 #include "main/stencil.h"
57 #include "main/texobj.h"
58 #include "main/texenv.h"
59 #include "main/teximage.h"
60 #include "main/texparam.h"
61 #include "main/texstate.h"
62 #include "main/varray.h"
63 #include "main/viewport.h"
64 #include "shader/program.h"
65 #include "shader/arbprogram.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
;
109 GLbitfield BlendEnabled
;
110 GLboolean ColorLogicOpEnabled
;
112 /** META_COLOR_MASK */
113 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
115 /** META_DEPTH_TEST */
116 struct gl_depthbuffer_attrib Depth
;
121 /** META_PIXEL_STORE */
122 struct gl_pixelstore_attrib Pack
, Unpack
;
124 /** META_PIXEL_TRANSFER */
125 GLfloat RedBias
, RedScale
;
126 GLfloat GreenBias
, GreenScale
;
127 GLfloat BlueBias
, BlueScale
;
128 GLfloat AlphaBias
, AlphaScale
;
129 GLfloat DepthBias
, DepthScale
;
130 GLboolean MapColorFlag
;
131 GLboolean Convolution1DEnabled
;
132 GLboolean Convolution2DEnabled
;
133 GLboolean Separable2DEnabled
;
135 /** META_RASTERIZATION */
136 GLenum FrontPolygonMode
, BackPolygonMode
;
137 GLboolean PolygonOffset
;
138 GLboolean PolygonSmooth
;
139 GLboolean PolygonStipple
;
140 GLboolean PolygonCull
;
143 struct gl_scissor_attrib Scissor
;
146 GLboolean VertexProgramEnabled
;
147 struct gl_vertex_program
*VertexProgram
;
148 GLboolean FragmentProgramEnabled
;
149 struct gl_fragment_program
*FragmentProgram
;
152 /** META_STENCIL_TEST */
153 struct gl_stencil_attrib Stencil
;
155 /** META_TRANSFORM */
157 GLfloat ModelviewMatrix
[16];
158 GLfloat ProjectionMatrix
[16];
159 GLfloat TextureMatrix
[16];
160 GLbitfield ClipPlanesEnabled
;
164 GLuint ClientActiveUnit
;
165 /** for unit[0] only */
166 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
167 /** mask of TEXTURE_2D_BIT, etc */
168 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
169 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
170 GLuint EnvMode
; /* unit[0] only */
173 struct gl_array_object
*ArrayObj
;
174 struct gl_buffer_object
*ArrayBufferObj
;
177 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
178 GLclampd DepthNear
, DepthFar
;
180 /** Miscellaneous (always disabled) */
186 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
187 * This is currently shared by all the meta ops. But we could create a
188 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
193 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
194 GLsizei MinSize
; /**< Min texture size to allocate */
195 GLsizei MaxSize
; /**< Max possible texture size */
196 GLboolean NPOT
; /**< Non-power of two size OK? */
197 GLsizei Width
, Height
; /**< Current texture size */
199 GLfloat Sright
, Ttop
; /**< right, top texcoords */
204 * State for glBlitFramebufer()
215 * State for glClear()
225 * State for glCopyPixels()
235 * State for glDrawPixels()
241 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
242 GLuint DepthFP
; /**< Fragment program for drawing depth images */
247 * State for glBitmap()
253 struct temp_texture Tex
; /**< separate texture from other meta ops */
258 * State for _mesa_meta_generate_mipmap()
260 struct gen_mipmap_state
269 * All per-context meta state.
273 struct save_state Save
; /**< state saved during meta-ops */
275 struct temp_texture TempTex
;
277 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
278 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
279 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
280 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
281 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
282 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
287 * Initialize meta-ops for a context.
288 * To be called once during context creation.
291 _mesa_meta_init(GLcontext
*ctx
)
295 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
300 * Free context meta-op state.
301 * To be called once during context destruction.
304 _mesa_meta_free(GLcontext
*ctx
)
306 /* Note: Any textures, VBOs, etc, that we allocate should get
307 * freed by the normal context destruction code. But this would be
308 * the place to free other meta data someday.
316 * Enter meta state. This is like a light-weight version of glPushAttrib
317 * but it also resets most GL state back to default values.
319 * \param state bitmask of META_* flags indicating which attribute groups
320 * to save and reset to their defaults
323 _mesa_meta_begin(GLcontext
*ctx
, GLbitfield state
)
325 struct save_state
*save
= &ctx
->Meta
->Save
;
327 save
->SavedState
= state
;
329 if (state
& META_ALPHA_TEST
) {
330 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
331 if (ctx
->Color
.AlphaEnabled
)
332 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
335 if (state
& META_BLEND
) {
336 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
337 if (ctx
->Color
.BlendEnabled
) {
338 if (ctx
->Extensions
.EXT_draw_buffers2
) {
340 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
341 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
345 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
348 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
349 if (ctx
->Color
.ColorLogicOpEnabled
)
350 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
353 if (state
& META_COLOR_MASK
) {
354 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
355 sizeof(ctx
->Color
.ColorMask
));
356 if (!ctx
->Color
.ColorMask
[0][0] ||
357 !ctx
->Color
.ColorMask
[0][1] ||
358 !ctx
->Color
.ColorMask
[0][2] ||
359 !ctx
->Color
.ColorMask
[0][3])
360 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
363 if (state
& META_DEPTH_TEST
) {
364 save
->Depth
= ctx
->Depth
; /* struct copy */
366 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
369 if (state
& META_FOG
) {
370 save
->Fog
= ctx
->Fog
.Enabled
;
371 if (ctx
->Fog
.Enabled
)
372 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
375 if (state
& META_PIXEL_STORE
) {
376 save
->Pack
= ctx
->Pack
;
377 save
->Unpack
= ctx
->Unpack
;
378 ctx
->Pack
= ctx
->DefaultPacking
;
379 ctx
->Unpack
= ctx
->DefaultPacking
;
382 if (state
& META_PIXEL_TRANSFER
) {
383 save
->RedScale
= ctx
->Pixel
.RedScale
;
384 save
->RedBias
= ctx
->Pixel
.RedBias
;
385 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
386 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
387 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
388 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
389 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
390 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
391 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
392 save
->Convolution1DEnabled
= ctx
->Pixel
.Convolution1DEnabled
;
393 save
->Convolution2DEnabled
= ctx
->Pixel
.Convolution2DEnabled
;
394 save
->Separable2DEnabled
= ctx
->Pixel
.Separable2DEnabled
;
395 ctx
->Pixel
.RedScale
= 1.0F
;
396 ctx
->Pixel
.RedBias
= 0.0F
;
397 ctx
->Pixel
.GreenScale
= 1.0F
;
398 ctx
->Pixel
.GreenBias
= 0.0F
;
399 ctx
->Pixel
.BlueScale
= 1.0F
;
400 ctx
->Pixel
.BlueBias
= 0.0F
;
401 ctx
->Pixel
.AlphaScale
= 1.0F
;
402 ctx
->Pixel
.AlphaBias
= 0.0F
;
403 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
404 ctx
->Pixel
.Convolution1DEnabled
= GL_FALSE
;
405 ctx
->Pixel
.Convolution2DEnabled
= GL_FALSE
;
406 ctx
->Pixel
.Separable2DEnabled
= GL_FALSE
;
408 ctx
->NewState
|=_NEW_PIXEL
;
411 if (state
& META_RASTERIZATION
) {
412 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
413 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
414 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
415 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
416 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
417 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
418 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
419 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
420 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
421 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
422 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
425 if (state
& META_SCISSOR
) {
426 save
->Scissor
= ctx
->Scissor
; /* struct copy */
429 if (state
& META_SHADER
) {
430 if (ctx
->Extensions
.ARB_vertex_program
) {
431 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
432 save
->VertexProgram
= 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 save
->FragmentProgram
= ctx
->FragmentProgram
.Current
;
439 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
442 if (ctx
->Extensions
.ARB_shader_objects
) {
443 save
->Shader
= ctx
->Shader
.CurrentProgram
?
444 ctx
->Shader
.CurrentProgram
->Name
: 0;
445 _mesa_UseProgramObjectARB(0);
449 if (state
& META_STENCIL_TEST
) {
450 save
->Stencil
= ctx
->Stencil
; /* struct copy */
451 if (ctx
->Stencil
.Enabled
)
452 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
453 /* NOTE: other stencil state not reset */
456 if (state
& META_TEXTURE
) {
459 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
460 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
461 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
463 /* Disable all texture units */
464 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
465 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
466 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
467 if (ctx
->Texture
.Unit
[u
].Enabled
||
468 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
469 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
470 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
471 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
472 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
473 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
474 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
475 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
476 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
477 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
478 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
482 /* save current texture objects for unit[0] only */
483 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
484 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
485 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
488 /* set defaults for unit[0] */
489 _mesa_ActiveTextureARB(GL_TEXTURE0
);
490 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
491 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
494 if (state
& META_TRANSFORM
) {
495 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
496 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
497 16 * sizeof(GLfloat
));
498 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
499 16 * sizeof(GLfloat
));
500 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
501 16 * sizeof(GLfloat
));
502 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
503 /* set 1:1 vertex:pixel coordinate transform */
504 _mesa_ActiveTextureARB(GL_TEXTURE0
);
505 _mesa_MatrixMode(GL_TEXTURE
);
506 _mesa_LoadIdentity();
507 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
508 _mesa_MatrixMode(GL_MODELVIEW
);
509 _mesa_LoadIdentity();
510 _mesa_MatrixMode(GL_PROJECTION
);
511 _mesa_LoadIdentity();
512 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
513 0.0, ctx
->DrawBuffer
->Height
,
515 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
516 if (ctx
->Transform
.ClipPlanesEnabled
) {
518 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
519 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
524 if (state
& META_VERTEX
) {
525 /* save vertex array object state */
526 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
527 ctx
->Array
.ArrayObj
);
528 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
529 ctx
->Array
.ArrayBufferObj
);
530 /* set some default state? */
533 if (state
& META_VIEWPORT
) {
534 /* save viewport state */
535 save
->ViewportX
= ctx
->Viewport
.X
;
536 save
->ViewportY
= ctx
->Viewport
.Y
;
537 save
->ViewportW
= ctx
->Viewport
.Width
;
538 save
->ViewportH
= ctx
->Viewport
.Height
;
539 /* set viewport to match window size */
540 if (ctx
->Viewport
.X
!= 0 ||
541 ctx
->Viewport
.Y
!= 0 ||
542 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
543 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
544 _mesa_set_viewport(ctx
, 0, 0,
545 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
547 /* save depth range state */
548 save
->DepthNear
= ctx
->Viewport
.Near
;
549 save
->DepthFar
= ctx
->Viewport
.Far
;
550 /* set depth range to default */
551 _mesa_DepthRange(0.0, 1.0);
556 save
->Lighting
= ctx
->Light
.Enabled
;
557 if (ctx
->Light
.Enabled
)
558 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
564 * Leave meta state. This is like a light-weight version of glPopAttrib().
567 _mesa_meta_end(GLcontext
*ctx
)
569 struct save_state
*save
= &ctx
->Meta
->Save
;
570 const GLbitfield state
= save
->SavedState
;
572 if (state
& META_ALPHA_TEST
) {
573 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
574 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
577 if (state
& META_BLEND
) {
578 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
579 if (ctx
->Extensions
.EXT_draw_buffers2
) {
581 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
582 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
586 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
589 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
590 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
593 if (state
& META_COLOR_MASK
) {
595 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
596 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
598 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
599 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
602 _mesa_ColorMaskIndexed(i
,
603 save
->ColorMask
[i
][0],
604 save
->ColorMask
[i
][1],
605 save
->ColorMask
[i
][2],
606 save
->ColorMask
[i
][3]);
612 if (state
& META_DEPTH_TEST
) {
613 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
614 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
615 _mesa_DepthFunc(save
->Depth
.Func
);
616 _mesa_DepthMask(save
->Depth
.Mask
);
619 if (state
& META_FOG
) {
620 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
623 if (state
& META_PIXEL_STORE
) {
624 ctx
->Pack
= save
->Pack
;
625 ctx
->Unpack
= save
->Unpack
;
628 if (state
& META_PIXEL_TRANSFER
) {
629 ctx
->Pixel
.RedScale
= save
->RedScale
;
630 ctx
->Pixel
.RedBias
= save
->RedBias
;
631 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
632 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
633 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
634 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
635 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
636 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
637 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
638 ctx
->Pixel
.Convolution1DEnabled
= save
->Convolution1DEnabled
;
639 ctx
->Pixel
.Convolution2DEnabled
= save
->Convolution2DEnabled
;
640 ctx
->Pixel
.Separable2DEnabled
= save
->Separable2DEnabled
;
642 ctx
->NewState
|=_NEW_PIXEL
;
645 if (state
& META_RASTERIZATION
) {
646 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
647 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
648 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
649 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
650 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
651 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
654 if (state
& META_SCISSOR
) {
655 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
656 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
657 save
->Scissor
.Width
, save
->Scissor
.Height
);
660 if (state
& META_SHADER
) {
661 if (ctx
->Extensions
.ARB_vertex_program
) {
662 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
663 save
->VertexProgramEnabled
);
664 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
665 save
->VertexProgram
);
668 if (ctx
->Extensions
.ARB_fragment_program
) {
669 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
670 save
->FragmentProgramEnabled
);
671 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
672 save
->FragmentProgram
);
675 if (ctx
->Extensions
.ARB_shader_objects
) {
676 _mesa_UseProgramObjectARB(save
->Shader
);
680 if (state
& META_STENCIL_TEST
) {
681 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
683 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
684 _mesa_ClearStencil(stencil
->Clear
);
685 if (ctx
->Extensions
.EXT_stencil_two_side
) {
686 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
687 stencil
->TestTwoSide
);
688 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
689 ? GL_BACK
: GL_FRONT
);
692 _mesa_StencilFuncSeparate(GL_FRONT
,
693 stencil
->Function
[0],
695 stencil
->ValueMask
[0]);
696 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
697 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
698 stencil
->ZFailFunc
[0],
699 stencil
->ZPassFunc
[0]);
701 _mesa_StencilFuncSeparate(GL_BACK
,
702 stencil
->Function
[1],
704 stencil
->ValueMask
[1]);
705 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
706 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
707 stencil
->ZFailFunc
[1],
708 stencil
->ZPassFunc
[1]);
711 if (state
& META_TEXTURE
) {
714 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
716 /* restore texenv for unit[0] */
717 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
719 /* restore texture objects for unit[0] only */
720 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
721 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
722 save
->CurrentTexture
[tgt
]);
725 /* Re-enable textures, texgen */
726 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
727 if (save
->TexEnabled
[u
]) {
728 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
730 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
731 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
732 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
733 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
734 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
735 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
736 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
737 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
738 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
739 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
742 if (save
->TexGenEnabled
[u
]) {
743 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
745 if (save
->TexGenEnabled
[u
] & S_BIT
)
746 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
747 if (save
->TexGenEnabled
[u
] & T_BIT
)
748 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
749 if (save
->TexGenEnabled
[u
] & R_BIT
)
750 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
751 if (save
->TexGenEnabled
[u
] & Q_BIT
)
752 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
756 /* restore current unit state */
757 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
758 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
761 if (state
& META_TRANSFORM
) {
762 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
763 _mesa_ActiveTextureARB(GL_TEXTURE0
);
764 _mesa_MatrixMode(GL_TEXTURE
);
765 _mesa_LoadMatrixf(save
->TextureMatrix
);
766 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
768 _mesa_MatrixMode(GL_MODELVIEW
);
769 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
771 _mesa_MatrixMode(GL_PROJECTION
);
772 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
774 _mesa_MatrixMode(save
->MatrixMode
);
776 if (save
->ClipPlanesEnabled
) {
778 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
779 if (save
->ClipPlanesEnabled
& (1 << i
)) {
780 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
786 if (state
& META_VERTEX
) {
787 /* restore vertex buffer object */
788 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
789 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
791 /* restore vertex array object */
792 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
793 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
796 if (state
& META_VIEWPORT
) {
797 if (save
->ViewportX
!= ctx
->Viewport
.X
||
798 save
->ViewportY
!= ctx
->Viewport
.Y
||
799 save
->ViewportW
!= ctx
->Viewport
.Width
||
800 save
->ViewportH
!= ctx
->Viewport
.Height
) {
801 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
802 save
->ViewportW
, save
->ViewportH
);
804 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
808 if (save
->Lighting
) {
809 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
815 * One-time init for a temp_texture object.
816 * Choose tex target, compute max tex size, etc.
819 init_temp_texture(GLcontext
*ctx
, struct temp_texture
*tex
)
821 /* prefer texture rectangle */
822 if (ctx
->Extensions
.NV_texture_rectangle
) {
823 tex
->Target
= GL_TEXTURE_RECTANGLE
;
824 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
828 /* use 2D texture, NPOT if possible */
829 tex
->Target
= GL_TEXTURE_2D
;
830 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
831 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
833 tex
->MinSize
= 16; /* 16 x 16 at least */
834 assert(tex
->MaxSize
> 0);
836 _mesa_GenTextures(1, &tex
->TexObj
);
837 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
842 * Return pointer to temp_texture info for non-bitmap ops.
843 * This does some one-time init if needed.
845 static struct temp_texture
*
846 get_temp_texture(GLcontext
*ctx
)
848 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
851 init_temp_texture(ctx
, tex
);
859 * Return pointer to temp_texture info for _mesa_meta_bitmap().
860 * We use a separate texture for bitmaps to reduce texture
861 * allocation/deallocation.
863 static struct temp_texture
*
864 get_bitmap_temp_texture(GLcontext
*ctx
)
866 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
869 init_temp_texture(ctx
, tex
);
877 * Compute the width/height of texture needed to draw an image of the
878 * given size. Return a flag indicating whether the current texture
879 * can be re-used (glTexSubImage2D) or if a new texture needs to be
880 * allocated (glTexImage2D).
881 * Also, compute s/t texcoords for drawing.
883 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
886 alloc_texture(struct temp_texture
*tex
,
887 GLsizei width
, GLsizei height
, GLenum intFormat
)
889 GLboolean newTex
= GL_FALSE
;
891 ASSERT(width
<= tex
->MaxSize
);
892 ASSERT(height
<= tex
->MaxSize
);
894 if (width
> tex
->Width
||
895 height
> tex
->Height
||
896 intFormat
!= tex
->IntFormat
) {
897 /* alloc new texture (larger or different format) */
900 /* use non-power of two size */
901 tex
->Width
= MAX2(tex
->MinSize
, width
);
902 tex
->Height
= MAX2(tex
->MinSize
, height
);
905 /* find power of two size */
907 w
= h
= tex
->MinSize
;
916 tex
->IntFormat
= intFormat
;
921 /* compute texcoords */
922 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
923 tex
->Sright
= (GLfloat
) width
;
924 tex
->Ttop
= (GLfloat
) height
;
927 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
928 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
936 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
939 setup_copypix_texture(struct temp_texture
*tex
,
941 GLint srcX
, GLint srcY
,
942 GLsizei width
, GLsizei height
, GLenum intFormat
,
945 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
946 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
947 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
948 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
950 /* copy framebuffer image to texture */
952 /* create new tex image */
953 if (tex
->Width
== width
&& tex
->Height
== height
) {
954 /* create new tex with framebuffer data */
955 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
956 srcX
, srcY
, width
, height
, 0);
959 /* create empty texture */
960 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
961 tex
->Width
, tex
->Height
, 0,
962 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
964 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
965 0, 0, srcX
, srcY
, width
, height
);
969 /* replace existing tex image */
970 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
971 0, 0, srcX
, srcY
, width
, height
);
977 * Setup/load texture for glDrawPixels.
980 setup_drawpix_texture(GLcontext
*ctx
,
981 struct temp_texture
*tex
,
984 GLsizei width
, GLsizei height
,
985 GLenum format
, GLenum type
,
986 const GLvoid
*pixels
)
988 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
989 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
990 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
991 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
993 /* copy pixel data to texture */
995 /* create new tex image */
996 if (tex
->Width
== width
&& tex
->Height
== height
) {
997 /* create new tex and load image data */
998 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
999 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1002 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1004 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1005 ctx
->Unpack
.BufferObj
);
1006 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1007 /* create empty texture */
1008 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1009 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1010 if (save_unpack_obj
!= NULL
)
1011 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1012 save_unpack_obj
->Name
);
1014 _mesa_TexSubImage2D(tex
->Target
, 0,
1015 0, 0, width
, height
, format
, type
, pixels
);
1019 /* replace existing tex image */
1020 _mesa_TexSubImage2D(tex
->Target
, 0,
1021 0, 0, width
, height
, format
, type
, pixels
);
1028 * One-time init for drawing depth pixels.
1031 init_blit_depth_pixels(GLcontext
*ctx
)
1033 static const char *program
=
1035 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1038 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1039 struct temp_texture
*tex
= get_temp_texture(ctx
);
1040 const char *texTarget
;
1042 assert(blit
->DepthFP
== 0);
1044 /* replace %s with "RECT" or "2D" */
1045 assert(strlen(program
) + 4 < sizeof(program2
));
1046 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1050 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1052 _mesa_GenPrograms(1, &blit
->DepthFP
);
1053 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1054 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1055 strlen(program2
), (const GLubyte
*) program2
);
1060 * Try to do a glBlitFramebuffer using no-copy texturing.
1061 * We can do this when the src renderbuffer is actually a texture.
1062 * But if the src buffer == dst buffer we cannot do this.
1064 * \return new buffer mask indicating the buffers left to blit using the
1068 blitframebuffer_texture(GLcontext
*ctx
,
1069 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1070 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1071 GLbitfield mask
, GLenum filter
)
1073 if (mask
& GL_COLOR_BUFFER_BIT
) {
1074 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1075 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1076 const struct gl_renderbuffer_attachment
*drawAtt
=
1077 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1078 const struct gl_renderbuffer_attachment
*readAtt
=
1079 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1081 if (readAtt
&& readAtt
->Texture
) {
1082 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1083 const GLuint srcLevel
= readAtt
->TextureLevel
;
1084 const GLenum minFilterSave
= texObj
->MinFilter
;
1085 const GLenum magFilterSave
= texObj
->MagFilter
;
1086 const GLint baseLevelSave
= texObj
->BaseLevel
;
1087 const GLint maxLevelSave
= texObj
->MaxLevel
;
1088 const GLenum wrapSSave
= texObj
->WrapS
;
1089 const GLenum wrapTSave
= texObj
->WrapT
;
1090 const GLenum target
= texObj
->Target
;
1092 if (drawAtt
->Texture
== readAtt
->Texture
) {
1093 /* Can't use same texture as both the source and dest. We need
1094 * to handle overlapping blits and besides, some hw may not
1100 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1101 /* Can't handle other texture types at this time */
1106 printf("Blit from texture!\n");
1107 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1108 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1111 /* Prepare src texture state */
1112 _mesa_BindTexture(target
, texObj
->Name
);
1113 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1114 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1115 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1116 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1117 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1118 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1119 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1120 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1122 /* Prepare vertex data (the VBO was previously created and bound) */
1127 struct vertex verts
[4];
1128 GLfloat s0
, t0
, s1
, t1
;
1130 if (target
== GL_TEXTURE_2D
) {
1131 const struct gl_texture_image
*texImage
1132 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1133 s0
= srcX0
/ (float) texImage
->Width
;
1134 s1
= srcX1
/ (float) texImage
->Width
;
1135 t0
= srcY0
/ (float) texImage
->Height
;
1136 t1
= srcY1
/ (float) texImage
->Height
;
1139 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1146 verts
[0].x
= (GLfloat
) dstX0
;
1147 verts
[0].y
= (GLfloat
) dstY0
;
1148 verts
[1].x
= (GLfloat
) dstX1
;
1149 verts
[1].y
= (GLfloat
) dstY0
;
1150 verts
[2].x
= (GLfloat
) dstX1
;
1151 verts
[2].y
= (GLfloat
) dstY1
;
1152 verts
[3].x
= (GLfloat
) dstX0
;
1153 verts
[3].y
= (GLfloat
) dstY1
;
1164 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1167 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1169 /* Restore texture object state, the texture binding will
1170 * be restored by _mesa_meta_end().
1172 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1173 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1174 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1175 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1176 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1177 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1179 /* Done with color buffer */
1180 mask
&= ~GL_COLOR_BUFFER_BIT
;
1189 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1190 * of texture mapping and polygon rendering.
1193 _mesa_meta_BlitFramebuffer(GLcontext
*ctx
,
1194 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1195 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1196 GLbitfield mask
, GLenum filter
)
1198 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1199 struct temp_texture
*tex
= get_temp_texture(ctx
);
1200 const GLsizei maxTexSize
= tex
->MaxSize
;
1201 const GLint srcX
= MIN2(srcX0
, srcX1
);
1202 const GLint srcY
= MIN2(srcY0
, srcY1
);
1203 const GLint srcW
= abs(srcX1
- srcX0
);
1204 const GLint srcH
= abs(srcY1
- srcY0
);
1205 const GLboolean srcFlipX
= srcX1
< srcX0
;
1206 const GLboolean srcFlipY
= srcY1
< srcY0
;
1210 struct vertex verts
[4];
1213 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1214 /* XXX avoid this fallback */
1215 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1216 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1232 /* only scissor effects blit so save/clear all other relevant state */
1233 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1235 if (blit
->ArrayObj
== 0) {
1236 /* one-time setup */
1238 /* create vertex array object */
1239 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1240 _mesa_BindVertexArray(blit
->ArrayObj
);
1242 /* create vertex array buffer */
1243 _mesa_GenBuffersARB(1, &blit
->VBO
);
1244 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1245 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1246 NULL
, GL_DYNAMIC_DRAW_ARB
);
1248 /* setup vertex arrays */
1249 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1250 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1251 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1252 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1255 _mesa_BindVertexArray(blit
->ArrayObj
);
1256 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1259 /* Try faster, direct texture approach first */
1260 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1261 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1263 _mesa_meta_end(ctx
);
1267 /* Continue with "normal" approach which involves copying the src rect
1268 * into a temporary texture and is "blitted" by drawing a textured quad.
1271 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1273 /* vertex positions/texcoords (after texture allocation!) */
1275 verts
[0].x
= (GLfloat
) dstX0
;
1276 verts
[0].y
= (GLfloat
) dstY0
;
1277 verts
[1].x
= (GLfloat
) dstX1
;
1278 verts
[1].y
= (GLfloat
) dstY0
;
1279 verts
[2].x
= (GLfloat
) dstX1
;
1280 verts
[2].y
= (GLfloat
) dstY1
;
1281 verts
[3].x
= (GLfloat
) dstX0
;
1282 verts
[3].y
= (GLfloat
) dstY1
;
1286 verts
[1].s
= tex
->Sright
;
1288 verts
[2].s
= tex
->Sright
;
1289 verts
[2].t
= tex
->Ttop
;
1291 verts
[3].t
= tex
->Ttop
;
1293 /* upload new vertex data */
1294 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1297 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1299 if (mask
& GL_COLOR_BUFFER_BIT
) {
1300 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1302 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1303 mask
&= ~GL_COLOR_BUFFER_BIT
;
1306 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1307 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1310 init_blit_depth_pixels(ctx
);
1312 /* maybe change tex format here */
1313 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1315 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1316 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1318 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1319 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1321 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1322 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1323 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1324 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1325 _mesa_DepthFunc(GL_ALWAYS
);
1326 _mesa_DepthMask(GL_TRUE
);
1328 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1329 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1335 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1336 /* XXX can't easily do stencil */
1339 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1341 _mesa_meta_end(ctx
);
1344 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1345 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1351 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1354 _mesa_meta_Clear(GLcontext
*ctx
, GLbitfield buffers
)
1356 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1358 GLfloat x
, y
, z
, r
, g
, b
, a
;
1360 struct vertex verts
[4];
1361 /* save all state but scissor, pixel pack/unpack */
1362 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1364 if (buffers
& BUFFER_BITS_COLOR
) {
1365 /* if clearing color buffers, don't save/restore colormask */
1366 metaSave
-= META_COLOR_MASK
;
1369 _mesa_meta_begin(ctx
, metaSave
);
1371 if (clear
->ArrayObj
== 0) {
1372 /* one-time setup */
1374 /* create vertex array object */
1375 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1376 _mesa_BindVertexArray(clear
->ArrayObj
);
1378 /* create vertex array buffer */
1379 _mesa_GenBuffersARB(1, &clear
->VBO
);
1380 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1382 /* setup vertex arrays */
1383 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1384 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1385 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1386 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1389 _mesa_BindVertexArray(clear
->ArrayObj
);
1390 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1393 /* GL_COLOR_BUFFER_BIT */
1394 if (buffers
& BUFFER_BITS_COLOR
) {
1395 /* leave colormask, glDrawBuffer state as-is */
1398 ASSERT(metaSave
& META_COLOR_MASK
);
1399 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1402 /* GL_DEPTH_BUFFER_BIT */
1403 if (buffers
& BUFFER_BIT_DEPTH
) {
1404 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1405 _mesa_DepthFunc(GL_ALWAYS
);
1406 _mesa_DepthMask(GL_TRUE
);
1409 assert(!ctx
->Depth
.Test
);
1412 /* GL_STENCIL_BUFFER_BIT */
1413 if (buffers
& BUFFER_BIT_STENCIL
) {
1414 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1415 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1416 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1417 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1418 ctx
->Stencil
.Clear
& 0x7fffffff,
1419 ctx
->Stencil
.WriteMask
[0]);
1422 assert(!ctx
->Stencil
.Enabled
);
1425 /* vertex positions/colors */
1427 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1428 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1429 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1430 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1431 const GLfloat z
= 1.0 - 2.0 * ctx
->Depth
.Clear
;
1448 for (i
= 0; i
< 4; i
++) {
1449 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1450 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1451 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1452 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1455 /* upload new vertex data */
1456 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1457 GL_DYNAMIC_DRAW_ARB
);
1461 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1463 _mesa_meta_end(ctx
);
1468 * Meta implementation of ctx->Driver.CopyPixels() in terms
1469 * of texture mapping and polygon rendering.
1472 _mesa_meta_CopyPixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1473 GLsizei width
, GLsizei height
,
1474 GLint dstX
, GLint dstY
, GLenum type
)
1476 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1477 struct temp_texture
*tex
= get_temp_texture(ctx
);
1479 GLfloat x
, y
, z
, s
, t
;
1481 struct vertex verts
[4];
1483 GLenum intFormat
= GL_RGBA
;
1485 if (type
!= GL_COLOR
||
1486 ctx
->_ImageTransferState
||
1488 width
> tex
->MaxSize
||
1489 height
> tex
->MaxSize
) {
1490 /* XXX avoid this fallback */
1491 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1495 /* Most GL state applies to glCopyPixels, but a there's a few things
1496 * we need to override:
1498 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1505 if (copypix
->ArrayObj
== 0) {
1506 /* one-time setup */
1508 /* create vertex array object */
1509 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1510 _mesa_BindVertexArray(copypix
->ArrayObj
);
1512 /* create vertex array buffer */
1513 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1514 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1515 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1516 NULL
, GL_DYNAMIC_DRAW_ARB
);
1518 /* setup vertex arrays */
1519 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1520 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1521 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1522 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1525 _mesa_BindVertexArray(copypix
->ArrayObj
);
1526 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1529 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1531 /* vertex positions, texcoords (after texture allocation!) */
1533 const GLfloat dstX0
= (GLfloat
) dstX
;
1534 const GLfloat dstY0
= (GLfloat
) dstY
;
1535 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1536 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1537 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1547 verts
[1].s
= tex
->Sright
;
1552 verts
[2].s
= tex
->Sright
;
1553 verts
[2].t
= tex
->Ttop
;
1558 verts
[3].t
= tex
->Ttop
;
1560 /* upload new vertex data */
1561 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1564 /* Alloc/setup texture */
1565 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1566 GL_RGBA
, GL_NEAREST
);
1568 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1570 /* draw textured quad */
1571 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1573 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1575 _mesa_meta_end(ctx
);
1581 * When the glDrawPixels() image size is greater than the max rectangle
1582 * texture size we use this function to break the glDrawPixels() image
1583 * into tiles which fit into the max texture size.
1586 tiled_draw_pixels(GLcontext
*ctx
,
1588 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1589 GLenum format
, GLenum type
,
1590 const struct gl_pixelstore_attrib
*unpack
,
1591 const GLvoid
*pixels
)
1593 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1596 if (tileUnpack
.RowLength
== 0)
1597 tileUnpack
.RowLength
= width
;
1599 for (i
= 0; i
< width
; i
+= tileSize
) {
1600 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1601 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1603 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1605 for (j
= 0; j
< height
; j
+= tileSize
) {
1606 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1607 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1609 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1611 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1612 format
, type
, &tileUnpack
, pixels
);
1619 * One-time init for drawing stencil pixels.
1622 init_draw_stencil_pixels(GLcontext
*ctx
)
1624 /* This program is run eight times, once for each stencil bit.
1625 * The stencil values to draw are found in an 8-bit alpha texture.
1626 * We read the texture/stencil value and test if bit 'b' is set.
1627 * If the bit is not set, use KIL to kill the fragment.
1628 * Finally, we use the stencil test to update the stencil buffer.
1630 * The basic algorithm for checking if a bit is set is:
1631 * if (is_odd(value / (1 << bit)))
1632 * result is one (or non-zero).
1635 * The program parameter contains three values:
1636 * parm.x = 255 / (1 << bit)
1640 static const char *program
=
1642 "PARAM parm = program.local[0]; \n"
1644 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1645 "# t = t * 255 / bit \n"
1646 "MUL t.x, t.a, parm.x; \n"
1649 "SUB t.x, t.x, t.y; \n"
1651 "MUL t.x, t.x, parm.y; \n"
1652 "# t = fract(t.x) \n"
1653 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1654 "# t.x = (t.x == 0 ? 1 : 0) \n"
1655 "SGE t.x, -t.x, parm.z; \n"
1657 "# for debug only \n"
1658 "#MOV result.color, t.x; \n"
1660 char program2
[1000];
1661 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1662 struct temp_texture
*tex
= get_temp_texture(ctx
);
1663 const char *texTarget
;
1665 assert(drawpix
->StencilFP
== 0);
1667 /* replace %s with "RECT" or "2D" */
1668 assert(strlen(program
) + 4 < sizeof(program2
));
1669 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1673 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1675 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1676 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1677 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1678 strlen(program2
), (const GLubyte
*) program2
);
1683 * One-time init for drawing depth pixels.
1686 init_draw_depth_pixels(GLcontext
*ctx
)
1688 static const char *program
=
1690 "PARAM color = program.local[0]; \n"
1691 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1692 "MOV result.color, color; \n"
1695 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1696 struct temp_texture
*tex
= get_temp_texture(ctx
);
1697 const char *texTarget
;
1699 assert(drawpix
->DepthFP
== 0);
1701 /* replace %s with "RECT" or "2D" */
1702 assert(strlen(program
) + 4 < sizeof(program2
));
1703 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1707 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1709 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1710 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1711 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1712 strlen(program2
), (const GLubyte
*) program2
);
1717 * Meta implementation of ctx->Driver.DrawPixels() in terms
1718 * of texture mapping and polygon rendering.
1721 _mesa_meta_DrawPixels(GLcontext
*ctx
,
1722 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1723 GLenum format
, GLenum type
,
1724 const struct gl_pixelstore_attrib
*unpack
,
1725 const GLvoid
*pixels
)
1727 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1728 struct temp_texture
*tex
= get_temp_texture(ctx
);
1729 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1730 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1732 GLfloat x
, y
, z
, s
, t
;
1734 struct vertex verts
[4];
1735 GLenum texIntFormat
;
1736 GLboolean fallback
, newTex
;
1737 GLbitfield metaExtraSave
= 0x0;
1741 * Determine if we can do the glDrawPixels with texture mapping.
1743 fallback
= GL_FALSE
;
1744 if (ctx
->_ImageTransferState
||
1749 if (_mesa_is_color_format(format
)) {
1750 /* use more compact format when possible */
1751 /* XXX disable special case for GL_LUMINANCE for now to work around
1752 * apparent i965 driver bug (see bug #23670).
1754 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1755 texIntFormat
= format
;
1757 texIntFormat
= GL_RGBA
;
1759 else if (_mesa_is_stencil_format(format
)) {
1760 if (ctx
->Extensions
.ARB_fragment_program
&&
1761 ctx
->Pixel
.IndexShift
== 0 &&
1762 ctx
->Pixel
.IndexOffset
== 0 &&
1763 type
== GL_UNSIGNED_BYTE
) {
1764 /* We'll store stencil as alpha. This only works for GLubyte
1765 * image data because of how incoming values are mapped to alpha
1768 texIntFormat
= GL_ALPHA
;
1769 metaExtraSave
= (META_COLOR_MASK
|
1778 else if (_mesa_is_depth_format(format
)) {
1779 if (ctx
->Extensions
.ARB_depth_texture
&&
1780 ctx
->Extensions
.ARB_fragment_program
) {
1781 texIntFormat
= GL_DEPTH_COMPONENT
;
1782 metaExtraSave
= (META_SHADER
);
1793 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1794 format
, type
, unpack
, pixels
);
1799 * Check image size against max texture size, draw as tiles if needed.
1801 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1802 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1803 format
, type
, unpack
, pixels
);
1807 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1808 * but a there's a few things we need to override:
1810 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1818 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1820 /* vertex positions, texcoords (after texture allocation!) */
1822 const GLfloat x0
= (GLfloat
) x
;
1823 const GLfloat y0
= (GLfloat
) y
;
1824 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1825 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1826 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1836 verts
[1].s
= tex
->Sright
;
1841 verts
[2].s
= tex
->Sright
;
1842 verts
[2].t
= tex
->Ttop
;
1847 verts
[3].t
= tex
->Ttop
;
1850 if (drawpix
->ArrayObj
== 0) {
1851 /* one-time setup: create vertex array object */
1852 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1854 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1856 /* create vertex array buffer */
1857 _mesa_GenBuffersARB(1, &vbo
);
1858 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1859 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1860 verts
, GL_DYNAMIC_DRAW_ARB
);
1862 /* setup vertex arrays */
1863 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1864 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1865 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1866 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1868 /* set given unpack params */
1869 ctx
->Unpack
= *unpack
;
1871 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1873 if (_mesa_is_stencil_format(format
)) {
1874 /* Drawing stencil */
1877 if (!drawpix
->StencilFP
)
1878 init_draw_stencil_pixels(ctx
);
1880 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1881 GL_ALPHA
, type
, pixels
);
1883 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1885 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1887 /* set all stencil bits to 0 */
1888 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1889 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1890 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1892 /* set stencil bits to 1 where needed */
1893 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1895 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1896 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1898 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1899 const GLuint mask
= 1 << bit
;
1900 if (mask
& origStencilMask
) {
1901 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1902 _mesa_StencilMask(mask
);
1904 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1905 255.0 / mask
, 0.5, 0.0, 0.0);
1907 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1911 else if (_mesa_is_depth_format(format
)) {
1913 if (!drawpix
->DepthFP
)
1914 init_draw_depth_pixels(ctx
);
1916 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1917 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1919 /* polygon color = current raster color */
1920 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1921 ctx
->Current
.RasterColor
);
1923 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1924 format
, type
, pixels
);
1926 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1930 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1931 format
, type
, pixels
);
1932 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1935 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1937 _mesa_DeleteBuffersARB(1, &vbo
);
1939 /* restore unpack params */
1940 ctx
->Unpack
= unpackSave
;
1942 _mesa_meta_end(ctx
);
1947 * Do glBitmap with a alpha texture quad. Use the alpha test to
1948 * cull the 'off' bits. If alpha test is already enabled, fall back
1949 * to swrast (should be a rare case).
1950 * A bitmap cache as in the gallium/mesa state tracker would
1951 * improve performance a lot.
1954 _mesa_meta_Bitmap(GLcontext
*ctx
,
1955 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1956 const struct gl_pixelstore_attrib
*unpack
,
1957 const GLubyte
*bitmap1
)
1959 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1960 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1961 const GLenum texIntFormat
= GL_ALPHA
;
1962 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
1964 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
1966 struct vertex verts
[4];
1971 * Check if swrast fallback is needed.
1973 if (ctx
->_ImageTransferState
||
1974 ctx
->Color
.AlphaEnabled
||
1976 ctx
->Texture
._EnabledUnits
||
1977 width
> tex
->MaxSize
||
1978 height
> tex
->MaxSize
) {
1979 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
1983 /* Most GL state applies to glBitmap (like blending, stencil, etc),
1984 * but a there's a few things we need to override:
1986 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
1988 META_RASTERIZATION
|
1995 if (bitmap
->ArrayObj
== 0) {
1996 /* one-time setup */
1998 /* create vertex array object */
1999 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2000 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2002 /* create vertex array buffer */
2003 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2004 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2005 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2006 NULL
, GL_DYNAMIC_DRAW_ARB
);
2008 /* setup vertex arrays */
2009 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2010 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2011 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2012 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2013 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2014 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2017 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2018 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2021 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2023 /* vertex positions, texcoords, colors (after texture allocation!) */
2025 const GLfloat x0
= (GLfloat
) x
;
2026 const GLfloat y0
= (GLfloat
) y
;
2027 const GLfloat x1
= (GLfloat
) (x
+ width
);
2028 const GLfloat y1
= (GLfloat
) (y
+ height
);
2029 const GLfloat z
= ctx
->Current
.RasterPos
[2];
2040 verts
[1].s
= tex
->Sright
;
2045 verts
[2].s
= tex
->Sright
;
2046 verts
[2].t
= tex
->Ttop
;
2051 verts
[3].t
= tex
->Ttop
;
2053 for (i
= 0; i
< 4; i
++) {
2054 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2055 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2056 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2057 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2060 /* upload new vertex data */
2061 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2064 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2068 bitmap8
= (GLubyte
*) calloc(1, width
* height
);
2070 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2071 bitmap8
, width
, 0xff);
2073 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2075 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2076 _mesa_AlphaFunc(GL_GREATER
, 0.0);
2078 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2079 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2081 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2083 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2088 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2090 _mesa_meta_end(ctx
);
2095 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2096 * software fallback. The fallback path will require that the texture
2097 * images are mapped.
2098 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2101 _mesa_meta_check_generate_mipmap_fallback(GLcontext
*ctx
, GLenum target
,
2102 struct gl_texture_object
*texObj
)
2104 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2105 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2106 struct gl_texture_image
*baseImage
;
2110 /* check for fallbacks */
2111 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2112 target
== GL_TEXTURE_3D
) {
2116 srcLevel
= texObj
->BaseLevel
;
2117 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2118 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2123 * Test that we can actually render in the texture's format.
2126 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2127 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2129 if (target
== GL_TEXTURE_1D
) {
2130 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2131 GL_COLOR_ATTACHMENT0_EXT
,
2132 target
, texObj
->Name
, srcLevel
);
2135 /* other work is needed to enable 3D mipmap generation */
2136 else if (target
== GL_TEXTURE_3D
) {
2138 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2139 GL_COLOR_ATTACHMENT0_EXT
,
2140 target
, texObj
->Name
, srcLevel
, zoffset
);
2145 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2146 GL_COLOR_ATTACHMENT0_EXT
,
2147 target
, texObj
->Name
, srcLevel
);
2150 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2152 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2154 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2163 * Called via ctx->Driver.GenerateMipmap()
2164 * Note: texture borders and 3D texture support not yet complete.
2167 _mesa_meta_GenerateMipmap(GLcontext
*ctx
, GLenum target
,
2168 struct gl_texture_object
*texObj
)
2170 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2172 GLfloat x
, y
, s
, t
, r
;
2174 struct vertex verts
[4];
2175 const GLuint baseLevel
= texObj
->BaseLevel
;
2176 const GLuint maxLevel
= texObj
->MaxLevel
;
2177 const GLenum minFilterSave
= texObj
->MinFilter
;
2178 const GLenum magFilterSave
= texObj
->MagFilter
;
2179 const GLint baseLevelSave
= texObj
->BaseLevel
;
2180 const GLint maxLevelSave
= texObj
->MaxLevel
;
2181 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2182 const GLenum wrapSSave
= texObj
->WrapS
;
2183 const GLenum wrapTSave
= texObj
->WrapT
;
2184 const GLenum wrapRSave
= texObj
->WrapR
;
2185 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2186 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2191 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2192 _mesa_generate_mipmap(ctx
, target
, texObj
);
2196 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2197 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2198 faceTarget
= target
;
2199 target
= GL_TEXTURE_CUBE_MAP
;
2202 faceTarget
= target
;
2205 _mesa_meta_begin(ctx
, META_ALL
);
2207 if (original_active_unit
!= 0)
2208 _mesa_BindTexture(target
, texObj
->Name
);
2210 if (mipmap
->ArrayObj
== 0) {
2211 /* one-time setup */
2213 /* create vertex array object */
2214 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2215 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2217 /* create vertex array buffer */
2218 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2219 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2220 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2221 NULL
, GL_DYNAMIC_DRAW_ARB
);
2223 /* setup vertex arrays */
2224 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2225 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2226 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2227 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2230 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2231 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2235 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2237 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2239 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
2240 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2241 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2242 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2243 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2244 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2246 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2248 /* setup texcoords once (XXX what about border?) */
2249 switch (faceTarget
) {
2271 static const GLfloat st
[4][2] = {
2272 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2276 /* loop over quad verts */
2277 for (i
= 0; i
< 4; i
++) {
2278 /* Compute sc = +/-scale and tc = +/-scale.
2279 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2280 * though that can still sometimes happen with this scale factor...
2282 const GLfloat scale
= 0.9999f
;
2283 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2284 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2286 switch (faceTarget
) {
2287 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2292 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2297 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2302 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2307 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2312 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2324 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2326 /* setup vertex positions */
2337 /* upload new vertex data */
2338 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2341 /* setup projection matrix */
2342 _mesa_MatrixMode(GL_PROJECTION
);
2343 _mesa_LoadIdentity();
2344 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2346 /* texture is already locked, unlock now */
2347 _mesa_unlock_texture(ctx
, texObj
);
2349 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2350 const struct gl_texture_image
*srcImage
;
2351 const GLuint srcLevel
= dstLevel
- 1;
2352 GLsizei srcWidth
, srcHeight
, srcDepth
;
2353 GLsizei dstWidth
, dstHeight
, dstDepth
;
2356 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2357 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2359 /* src size w/out border */
2360 srcWidth
= srcImage
->Width
- 2 * border
;
2361 srcHeight
= srcImage
->Height
- 2 * border
;
2362 srcDepth
= srcImage
->Depth
- 2 * border
;
2364 /* new dst size w/ border */
2365 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2366 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2367 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2369 if (dstWidth
== srcImage
->Width
&&
2370 dstHeight
== srcImage
->Height
&&
2371 dstDepth
== srcImage
->Depth
) {
2376 /* Create empty dest image */
2377 if (target
== GL_TEXTURE_1D
) {
2378 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2380 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2382 else if (target
== GL_TEXTURE_3D
) {
2383 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2384 dstWidth
, dstHeight
, dstDepth
, border
,
2385 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2389 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2390 dstWidth
, dstHeight
, border
,
2391 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2393 if (target
== GL_TEXTURE_CUBE_MAP
) {
2394 /* If texturing from a cube, we need to make sure all src faces
2395 * have been defined (even if we're not sampling from them.)
2396 * Otherwise the texture object will be 'incomplete' and
2397 * texturing from it will not be allowed.
2400 for (face
= 0; face
< 6; face
++) {
2401 if (!texObj
->Image
[face
][srcLevel
] ||
2402 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2403 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2404 srcLevel
, srcImage
->InternalFormat
,
2405 srcWidth
, srcHeight
, border
,
2406 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2412 /* limit sampling to src level */
2413 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2414 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2416 /* Set to draw into the current dstLevel */
2417 if (target
== GL_TEXTURE_1D
) {
2418 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2419 GL_COLOR_ATTACHMENT0_EXT
,
2424 else if (target
== GL_TEXTURE_3D
) {
2425 GLint zoffset
= 0; /* XXX unfinished */
2426 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2427 GL_COLOR_ATTACHMENT0_EXT
,
2434 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2435 GL_COLOR_ATTACHMENT0_EXT
,
2441 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2444 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2445 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2450 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2451 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2453 /* setup viewport */
2454 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2456 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2459 _mesa_lock_texture(ctx
, texObj
); /* relock */
2461 _mesa_meta_end(ctx
);
2463 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2464 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2465 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
2466 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2467 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2468 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2469 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2470 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2472 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2477 * Determine the GL data type to use for the temporary image read with
2478 * ReadPixels() and passed to Tex[Sub]Image().
2481 get_temp_image_type(GLcontext
*ctx
, GLenum baseFormat
)
2483 switch (baseFormat
) {
2488 case GL_LUMINANCE_ALPHA
:
2490 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2491 return GL_UNSIGNED_BYTE
;
2492 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2493 return GL_UNSIGNED_SHORT
;
2496 case GL_DEPTH_COMPONENT
:
2497 return GL_UNSIGNED_INT
;
2498 case GL_DEPTH_STENCIL
:
2499 return GL_UNSIGNED_INT_24_8
;
2501 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2508 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2509 * Have to be careful with locking and meta state for pixel transfer.
2512 copy_tex_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2513 GLenum internalFormat
, GLint x
, GLint y
,
2514 GLsizei width
, GLsizei height
, GLint border
)
2516 struct gl_texture_object
*texObj
;
2517 struct gl_texture_image
*texImage
;
2518 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2519 GLenum format
, type
;
2523 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2524 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2526 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2527 type
= get_temp_image_type(ctx
, format
);
2528 bpp
= _mesa_bytes_per_pixel(format
, type
);
2530 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2535 * Alloc image buffer (XXX could use a PBO)
2537 buf
= malloc(width
* height
* bpp
);
2539 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2543 if (texImage
->TexFormat
== MESA_FORMAT_NONE
)
2544 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
,
2549 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2552 * Read image from framebuffer (disable pixel transfer ops)
2554 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2555 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2556 format
, type
, &ctx
->Pack
, buf
);
2557 _mesa_meta_end(ctx
);
2560 * Prepare for new texture image size/data
2562 #if FEATURE_convolve
2563 if (_mesa_is_color_format(internalFormat
)) {
2564 _mesa_adjust_image_for_convolution(ctx
, 2,
2565 &postConvWidth
, &postConvHeight
);
2569 if (texImage
->Data
) {
2570 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2573 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2574 postConvWidth
, postConvHeight
, 1,
2575 border
, internalFormat
);
2578 * Store texture data (with pixel transfer ops)
2580 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2582 _mesa_update_state(ctx
); /* to update pixel transfer state */
2584 if (target
== GL_TEXTURE_1D
) {
2585 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2586 width
, border
, format
, type
,
2587 buf
, &ctx
->Unpack
, texObj
, texImage
);
2590 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2591 width
, height
, border
, format
, type
,
2592 buf
, &ctx
->Unpack
, texObj
, texImage
);
2594 _mesa_meta_end(ctx
);
2596 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2603 _mesa_meta_CopyTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2604 GLenum internalFormat
, GLint x
, GLint y
,
2605 GLsizei width
, GLint border
)
2607 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2613 _mesa_meta_CopyTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2614 GLenum internalFormat
, GLint x
, GLint y
,
2615 GLsizei width
, GLsizei height
, GLint border
)
2617 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2618 width
, height
, border
);
2624 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2625 * Have to be careful with locking and meta state for pixel transfer.
2628 copy_tex_sub_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2629 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2631 GLsizei width
, GLsizei height
)
2633 struct gl_texture_object
*texObj
;
2634 struct gl_texture_image
*texImage
;
2635 GLenum format
, type
;
2639 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2640 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2642 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2643 type
= get_temp_image_type(ctx
, format
);
2644 bpp
= _mesa_bytes_per_pixel(format
, type
);
2646 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2651 * Alloc image buffer (XXX could use a PBO)
2653 buf
= malloc(width
* height
* bpp
);
2655 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2659 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2662 * Read image from framebuffer (disable pixel transfer ops)
2664 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2665 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2666 format
, type
, &ctx
->Pack
, buf
);
2667 _mesa_meta_end(ctx
);
2669 _mesa_update_state(ctx
); /* to update pixel transfer state */
2672 * Store texture data (with pixel transfer ops)
2674 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2675 if (target
== GL_TEXTURE_1D
) {
2676 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2677 width
, format
, type
, buf
,
2678 &ctx
->Unpack
, texObj
, texImage
);
2680 else if (target
== GL_TEXTURE_3D
) {
2681 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2682 width
, height
, 1, format
, type
, buf
,
2683 &ctx
->Unpack
, texObj
, texImage
);
2686 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2687 width
, height
, format
, type
, buf
,
2688 &ctx
->Unpack
, texObj
, texImage
);
2690 _mesa_meta_end(ctx
);
2692 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2699 _mesa_meta_CopyTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2701 GLint x
, GLint y
, GLsizei width
)
2703 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2709 _mesa_meta_CopyTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2710 GLint xoffset
, GLint yoffset
,
2712 GLsizei width
, GLsizei height
)
2714 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2715 x
, y
, width
, height
);
2720 _mesa_meta_CopyTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
2721 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2723 GLsizei width
, GLsizei height
)
2725 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2726 x
, y
, width
, height
);
2731 _mesa_meta_CopyColorTable(GLcontext
*ctx
,
2732 GLenum target
, GLenum internalformat
,
2733 GLint x
, GLint y
, GLsizei width
)
2737 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2739 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2744 * Read image from framebuffer (disable pixel transfer ops)
2746 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2747 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2748 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2750 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2752 _mesa_meta_end(ctx
);
2759 _mesa_meta_CopyColorSubTable(GLcontext
*ctx
,GLenum target
, GLsizei start
,
2760 GLint x
, GLint y
, GLsizei width
)
2764 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2766 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2771 * Read image from framebuffer (disable pixel transfer ops)
2773 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2774 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2775 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2777 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2779 _mesa_meta_end(ctx
);
2786 _mesa_meta_CopyConvolutionFilter1D(GLcontext
*ctx
, GLenum target
,
2787 GLenum internalFormat
,
2788 GLint x
, GLint y
, GLsizei width
)
2792 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2794 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyConvolutionFilter2D");
2799 * Read image from framebuffer (disable pixel transfer ops)
2801 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2802 _mesa_update_state(ctx
);
2803 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2804 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2806 _mesa_ConvolutionFilter1D(target
, internalFormat
, width
,
2807 GL_RGBA
, GL_FLOAT
, buf
);
2809 _mesa_meta_end(ctx
);
2816 _mesa_meta_CopyConvolutionFilter2D(GLcontext
*ctx
, GLenum target
,
2817 GLenum internalFormat
, GLint x
, GLint y
,
2818 GLsizei width
, GLsizei height
)
2822 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
2824 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyConvolutionFilter2D");
2829 * Read image from framebuffer (disable pixel transfer ops)
2831 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2832 _mesa_update_state(ctx
);
2834 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2835 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2837 _mesa_ConvolutionFilter2D(target
, internalFormat
, width
, height
,
2838 GL_RGBA
, GL_FLOAT
, buf
);
2840 _mesa_meta_end(ctx
);