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 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
433 ctx
->VertexProgram
.Current
);
434 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
437 if (ctx
->Extensions
.ARB_fragment_program
) {
438 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
439 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
440 ctx
->FragmentProgram
.Current
);
441 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
444 if (ctx
->Extensions
.ARB_shader_objects
) {
445 save
->Shader
= ctx
->Shader
.CurrentProgram
?
446 ctx
->Shader
.CurrentProgram
->Name
: 0;
447 _mesa_UseProgramObjectARB(0);
451 if (state
& META_STENCIL_TEST
) {
452 save
->Stencil
= ctx
->Stencil
; /* struct copy */
453 if (ctx
->Stencil
.Enabled
)
454 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
455 /* NOTE: other stencil state not reset */
458 if (state
& META_TEXTURE
) {
461 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
462 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
463 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
465 /* Disable all texture units */
466 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
467 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
468 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
469 if (ctx
->Texture
.Unit
[u
].Enabled
||
470 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
471 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
472 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
473 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
474 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
475 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
476 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
477 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
478 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
479 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
480 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
484 /* save current texture objects for unit[0] only */
485 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
486 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
487 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
490 /* set defaults for unit[0] */
491 _mesa_ActiveTextureARB(GL_TEXTURE0
);
492 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
493 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
496 if (state
& META_TRANSFORM
) {
497 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
498 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
499 16 * sizeof(GLfloat
));
500 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
501 16 * sizeof(GLfloat
));
502 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
503 16 * sizeof(GLfloat
));
504 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
505 /* set 1:1 vertex:pixel coordinate transform */
506 _mesa_ActiveTextureARB(GL_TEXTURE0
);
507 _mesa_MatrixMode(GL_TEXTURE
);
508 _mesa_LoadIdentity();
509 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
510 _mesa_MatrixMode(GL_MODELVIEW
);
511 _mesa_LoadIdentity();
512 _mesa_MatrixMode(GL_PROJECTION
);
513 _mesa_LoadIdentity();
514 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
515 0.0, ctx
->DrawBuffer
->Height
,
517 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
518 if (ctx
->Transform
.ClipPlanesEnabled
) {
520 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
521 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
526 if (state
& META_VERTEX
) {
527 /* save vertex array object state */
528 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
529 ctx
->Array
.ArrayObj
);
530 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
531 ctx
->Array
.ArrayBufferObj
);
532 /* set some default state? */
535 if (state
& META_VIEWPORT
) {
536 /* save viewport state */
537 save
->ViewportX
= ctx
->Viewport
.X
;
538 save
->ViewportY
= ctx
->Viewport
.Y
;
539 save
->ViewportW
= ctx
->Viewport
.Width
;
540 save
->ViewportH
= ctx
->Viewport
.Height
;
541 /* set viewport to match window size */
542 if (ctx
->Viewport
.X
!= 0 ||
543 ctx
->Viewport
.Y
!= 0 ||
544 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
545 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
546 _mesa_set_viewport(ctx
, 0, 0,
547 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
549 /* save depth range state */
550 save
->DepthNear
= ctx
->Viewport
.Near
;
551 save
->DepthFar
= ctx
->Viewport
.Far
;
552 /* set depth range to default */
553 _mesa_DepthRange(0.0, 1.0);
558 save
->Lighting
= ctx
->Light
.Enabled
;
559 if (ctx
->Light
.Enabled
)
560 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
566 * Leave meta state. This is like a light-weight version of glPopAttrib().
569 _mesa_meta_end(GLcontext
*ctx
)
571 struct save_state
*save
= &ctx
->Meta
->Save
;
572 const GLbitfield state
= save
->SavedState
;
574 if (state
& META_ALPHA_TEST
) {
575 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
576 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
579 if (state
& META_BLEND
) {
580 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
581 if (ctx
->Extensions
.EXT_draw_buffers2
) {
583 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
584 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
588 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
591 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
592 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
595 if (state
& META_COLOR_MASK
) {
597 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
598 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
600 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
601 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
604 _mesa_ColorMaskIndexed(i
,
605 save
->ColorMask
[i
][0],
606 save
->ColorMask
[i
][1],
607 save
->ColorMask
[i
][2],
608 save
->ColorMask
[i
][3]);
614 if (state
& META_DEPTH_TEST
) {
615 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
616 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
617 _mesa_DepthFunc(save
->Depth
.Func
);
618 _mesa_DepthMask(save
->Depth
.Mask
);
621 if (state
& META_FOG
) {
622 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
625 if (state
& META_PIXEL_STORE
) {
626 ctx
->Pack
= save
->Pack
;
627 ctx
->Unpack
= save
->Unpack
;
630 if (state
& META_PIXEL_TRANSFER
) {
631 ctx
->Pixel
.RedScale
= save
->RedScale
;
632 ctx
->Pixel
.RedBias
= save
->RedBias
;
633 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
634 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
635 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
636 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
637 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
638 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
639 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
640 ctx
->Pixel
.Convolution1DEnabled
= save
->Convolution1DEnabled
;
641 ctx
->Pixel
.Convolution2DEnabled
= save
->Convolution2DEnabled
;
642 ctx
->Pixel
.Separable2DEnabled
= save
->Separable2DEnabled
;
644 ctx
->NewState
|=_NEW_PIXEL
;
647 if (state
& META_RASTERIZATION
) {
648 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
649 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
650 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
651 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
652 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
653 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
656 if (state
& META_SCISSOR
) {
657 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
658 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
659 save
->Scissor
.Width
, save
->Scissor
.Height
);
662 if (state
& META_SHADER
) {
663 if (ctx
->Extensions
.ARB_vertex_program
) {
664 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
665 save
->VertexProgramEnabled
);
666 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
667 save
->VertexProgram
);
668 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
671 if (ctx
->Extensions
.ARB_fragment_program
) {
672 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
673 save
->FragmentProgramEnabled
);
674 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
675 save
->FragmentProgram
);
676 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
679 if (ctx
->Extensions
.ARB_shader_objects
) {
680 _mesa_UseProgramObjectARB(save
->Shader
);
684 if (state
& META_STENCIL_TEST
) {
685 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
687 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
688 _mesa_ClearStencil(stencil
->Clear
);
689 if (ctx
->Extensions
.EXT_stencil_two_side
) {
690 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
691 stencil
->TestTwoSide
);
692 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
693 ? GL_BACK
: GL_FRONT
);
696 _mesa_StencilFuncSeparate(GL_FRONT
,
697 stencil
->Function
[0],
699 stencil
->ValueMask
[0]);
700 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
701 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
702 stencil
->ZFailFunc
[0],
703 stencil
->ZPassFunc
[0]);
705 _mesa_StencilFuncSeparate(GL_BACK
,
706 stencil
->Function
[1],
708 stencil
->ValueMask
[1]);
709 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
710 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
711 stencil
->ZFailFunc
[1],
712 stencil
->ZPassFunc
[1]);
715 if (state
& META_TEXTURE
) {
718 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
720 /* restore texenv for unit[0] */
721 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
723 /* restore texture objects for unit[0] only */
724 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
725 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
726 save
->CurrentTexture
[tgt
]);
727 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
730 /* Re-enable textures, texgen */
731 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
732 if (save
->TexEnabled
[u
]) {
733 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
735 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
736 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
737 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
738 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
739 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
740 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
741 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
742 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
743 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
744 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
747 if (save
->TexGenEnabled
[u
]) {
748 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
750 if (save
->TexGenEnabled
[u
] & S_BIT
)
751 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
752 if (save
->TexGenEnabled
[u
] & T_BIT
)
753 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
754 if (save
->TexGenEnabled
[u
] & R_BIT
)
755 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
756 if (save
->TexGenEnabled
[u
] & Q_BIT
)
757 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
761 /* restore current unit state */
762 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
763 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
766 if (state
& META_TRANSFORM
) {
767 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
768 _mesa_ActiveTextureARB(GL_TEXTURE0
);
769 _mesa_MatrixMode(GL_TEXTURE
);
770 _mesa_LoadMatrixf(save
->TextureMatrix
);
771 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
773 _mesa_MatrixMode(GL_MODELVIEW
);
774 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
776 _mesa_MatrixMode(GL_PROJECTION
);
777 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
779 _mesa_MatrixMode(save
->MatrixMode
);
781 if (save
->ClipPlanesEnabled
) {
783 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
784 if (save
->ClipPlanesEnabled
& (1 << i
)) {
785 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
791 if (state
& META_VERTEX
) {
792 /* restore vertex buffer object */
793 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
794 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
796 /* restore vertex array object */
797 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
798 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
801 if (state
& META_VIEWPORT
) {
802 if (save
->ViewportX
!= ctx
->Viewport
.X
||
803 save
->ViewportY
!= ctx
->Viewport
.Y
||
804 save
->ViewportW
!= ctx
->Viewport
.Width
||
805 save
->ViewportH
!= ctx
->Viewport
.Height
) {
806 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
807 save
->ViewportW
, save
->ViewportH
);
809 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
813 if (save
->Lighting
) {
814 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
820 * One-time init for a temp_texture object.
821 * Choose tex target, compute max tex size, etc.
824 init_temp_texture(GLcontext
*ctx
, struct temp_texture
*tex
)
826 /* prefer texture rectangle */
827 if (ctx
->Extensions
.NV_texture_rectangle
) {
828 tex
->Target
= GL_TEXTURE_RECTANGLE
;
829 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
833 /* use 2D texture, NPOT if possible */
834 tex
->Target
= GL_TEXTURE_2D
;
835 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
836 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
838 tex
->MinSize
= 16; /* 16 x 16 at least */
839 assert(tex
->MaxSize
> 0);
841 _mesa_GenTextures(1, &tex
->TexObj
);
842 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
847 * Return pointer to temp_texture info for non-bitmap ops.
848 * This does some one-time init if needed.
850 static struct temp_texture
*
851 get_temp_texture(GLcontext
*ctx
)
853 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
856 init_temp_texture(ctx
, tex
);
864 * Return pointer to temp_texture info for _mesa_meta_bitmap().
865 * We use a separate texture for bitmaps to reduce texture
866 * allocation/deallocation.
868 static struct temp_texture
*
869 get_bitmap_temp_texture(GLcontext
*ctx
)
871 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
874 init_temp_texture(ctx
, tex
);
882 * Compute the width/height of texture needed to draw an image of the
883 * given size. Return a flag indicating whether the current texture
884 * can be re-used (glTexSubImage2D) or if a new texture needs to be
885 * allocated (glTexImage2D).
886 * Also, compute s/t texcoords for drawing.
888 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
891 alloc_texture(struct temp_texture
*tex
,
892 GLsizei width
, GLsizei height
, GLenum intFormat
)
894 GLboolean newTex
= GL_FALSE
;
896 ASSERT(width
<= tex
->MaxSize
);
897 ASSERT(height
<= tex
->MaxSize
);
899 if (width
> tex
->Width
||
900 height
> tex
->Height
||
901 intFormat
!= tex
->IntFormat
) {
902 /* alloc new texture (larger or different format) */
905 /* use non-power of two size */
906 tex
->Width
= MAX2(tex
->MinSize
, width
);
907 tex
->Height
= MAX2(tex
->MinSize
, height
);
910 /* find power of two size */
912 w
= h
= tex
->MinSize
;
921 tex
->IntFormat
= intFormat
;
926 /* compute texcoords */
927 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
928 tex
->Sright
= (GLfloat
) width
;
929 tex
->Ttop
= (GLfloat
) height
;
932 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
933 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
941 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
944 setup_copypix_texture(struct temp_texture
*tex
,
946 GLint srcX
, GLint srcY
,
947 GLsizei width
, GLsizei height
, GLenum intFormat
,
950 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
951 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
952 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
953 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
955 /* copy framebuffer image to texture */
957 /* create new tex image */
958 if (tex
->Width
== width
&& tex
->Height
== height
) {
959 /* create new tex with framebuffer data */
960 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
961 srcX
, srcY
, width
, height
, 0);
964 /* create empty texture */
965 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
966 tex
->Width
, tex
->Height
, 0,
967 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
969 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
970 0, 0, srcX
, srcY
, width
, height
);
974 /* replace existing tex image */
975 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
976 0, 0, srcX
, srcY
, width
, height
);
982 * Setup/load texture for glDrawPixels.
985 setup_drawpix_texture(GLcontext
*ctx
,
986 struct temp_texture
*tex
,
989 GLsizei width
, GLsizei height
,
990 GLenum format
, GLenum type
,
991 const GLvoid
*pixels
)
993 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
994 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
995 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
996 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
998 /* copy pixel data to texture */
1000 /* create new tex image */
1001 if (tex
->Width
== width
&& tex
->Height
== height
) {
1002 /* create new tex and load image data */
1003 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1004 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1007 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1009 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1010 ctx
->Unpack
.BufferObj
);
1011 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1012 /* create empty texture */
1013 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1014 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1015 if (save_unpack_obj
!= NULL
)
1016 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1017 save_unpack_obj
->Name
);
1019 _mesa_TexSubImage2D(tex
->Target
, 0,
1020 0, 0, width
, height
, format
, type
, pixels
);
1024 /* replace existing tex image */
1025 _mesa_TexSubImage2D(tex
->Target
, 0,
1026 0, 0, width
, height
, format
, type
, pixels
);
1033 * One-time init for drawing depth pixels.
1036 init_blit_depth_pixels(GLcontext
*ctx
)
1038 static const char *program
=
1040 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1043 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1044 struct temp_texture
*tex
= get_temp_texture(ctx
);
1045 const char *texTarget
;
1047 assert(blit
->DepthFP
== 0);
1049 /* replace %s with "RECT" or "2D" */
1050 assert(strlen(program
) + 4 < sizeof(program2
));
1051 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1055 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1057 _mesa_GenPrograms(1, &blit
->DepthFP
);
1058 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1059 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1060 strlen(program2
), (const GLubyte
*) program2
);
1065 * Try to do a glBlitFramebuffer using no-copy texturing.
1066 * We can do this when the src renderbuffer is actually a texture.
1067 * But if the src buffer == dst buffer we cannot do this.
1069 * \return new buffer mask indicating the buffers left to blit using the
1073 blitframebuffer_texture(GLcontext
*ctx
,
1074 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1075 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1076 GLbitfield mask
, GLenum filter
)
1078 if (mask
& GL_COLOR_BUFFER_BIT
) {
1079 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1080 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1081 const struct gl_renderbuffer_attachment
*drawAtt
=
1082 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1083 const struct gl_renderbuffer_attachment
*readAtt
=
1084 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1086 if (readAtt
&& readAtt
->Texture
) {
1087 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1088 const GLuint srcLevel
= readAtt
->TextureLevel
;
1089 const GLenum minFilterSave
= texObj
->MinFilter
;
1090 const GLenum magFilterSave
= texObj
->MagFilter
;
1091 const GLint baseLevelSave
= texObj
->BaseLevel
;
1092 const GLint maxLevelSave
= texObj
->MaxLevel
;
1093 const GLenum wrapSSave
= texObj
->WrapS
;
1094 const GLenum wrapTSave
= texObj
->WrapT
;
1095 const GLenum target
= texObj
->Target
;
1097 if (drawAtt
->Texture
== readAtt
->Texture
) {
1098 /* Can't use same texture as both the source and dest. We need
1099 * to handle overlapping blits and besides, some hw may not
1105 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1106 /* Can't handle other texture types at this time */
1111 printf("Blit from texture!\n");
1112 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1113 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1116 /* Prepare src texture state */
1117 _mesa_BindTexture(target
, texObj
->Name
);
1118 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1119 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1120 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1121 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1122 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1123 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1124 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1125 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1127 /* Prepare vertex data (the VBO was previously created and bound) */
1132 struct vertex verts
[4];
1133 GLfloat s0
, t0
, s1
, t1
;
1135 if (target
== GL_TEXTURE_2D
) {
1136 const struct gl_texture_image
*texImage
1137 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1138 s0
= srcX0
/ (float) texImage
->Width
;
1139 s1
= srcX1
/ (float) texImage
->Width
;
1140 t0
= srcY0
/ (float) texImage
->Height
;
1141 t1
= srcY1
/ (float) texImage
->Height
;
1144 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1151 verts
[0].x
= (GLfloat
) dstX0
;
1152 verts
[0].y
= (GLfloat
) dstY0
;
1153 verts
[1].x
= (GLfloat
) dstX1
;
1154 verts
[1].y
= (GLfloat
) dstY0
;
1155 verts
[2].x
= (GLfloat
) dstX1
;
1156 verts
[2].y
= (GLfloat
) dstY1
;
1157 verts
[3].x
= (GLfloat
) dstX0
;
1158 verts
[3].y
= (GLfloat
) dstY1
;
1169 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1172 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1174 /* Restore texture object state, the texture binding will
1175 * be restored by _mesa_meta_end().
1177 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1178 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1179 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1180 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1181 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1182 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1184 /* Done with color buffer */
1185 mask
&= ~GL_COLOR_BUFFER_BIT
;
1194 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1195 * of texture mapping and polygon rendering.
1198 _mesa_meta_BlitFramebuffer(GLcontext
*ctx
,
1199 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1200 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1201 GLbitfield mask
, GLenum filter
)
1203 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1204 struct temp_texture
*tex
= get_temp_texture(ctx
);
1205 const GLsizei maxTexSize
= tex
->MaxSize
;
1206 const GLint srcX
= MIN2(srcX0
, srcX1
);
1207 const GLint srcY
= MIN2(srcY0
, srcY1
);
1208 const GLint srcW
= abs(srcX1
- srcX0
);
1209 const GLint srcH
= abs(srcY1
- srcY0
);
1210 const GLboolean srcFlipX
= srcX1
< srcX0
;
1211 const GLboolean srcFlipY
= srcY1
< srcY0
;
1215 struct vertex verts
[4];
1218 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1219 /* XXX avoid this fallback */
1220 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1221 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1237 /* only scissor effects blit so save/clear all other relevant state */
1238 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1240 if (blit
->ArrayObj
== 0) {
1241 /* one-time setup */
1243 /* create vertex array object */
1244 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1245 _mesa_BindVertexArray(blit
->ArrayObj
);
1247 /* create vertex array buffer */
1248 _mesa_GenBuffersARB(1, &blit
->VBO
);
1249 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1250 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1251 NULL
, GL_DYNAMIC_DRAW_ARB
);
1253 /* setup vertex arrays */
1254 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1255 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1256 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1257 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1260 _mesa_BindVertexArray(blit
->ArrayObj
);
1261 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1264 /* Try faster, direct texture approach first */
1265 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1266 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1268 _mesa_meta_end(ctx
);
1272 /* Continue with "normal" approach which involves copying the src rect
1273 * into a temporary texture and is "blitted" by drawing a textured quad.
1276 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1278 /* vertex positions/texcoords (after texture allocation!) */
1280 verts
[0].x
= (GLfloat
) dstX0
;
1281 verts
[0].y
= (GLfloat
) dstY0
;
1282 verts
[1].x
= (GLfloat
) dstX1
;
1283 verts
[1].y
= (GLfloat
) dstY0
;
1284 verts
[2].x
= (GLfloat
) dstX1
;
1285 verts
[2].y
= (GLfloat
) dstY1
;
1286 verts
[3].x
= (GLfloat
) dstX0
;
1287 verts
[3].y
= (GLfloat
) dstY1
;
1291 verts
[1].s
= tex
->Sright
;
1293 verts
[2].s
= tex
->Sright
;
1294 verts
[2].t
= tex
->Ttop
;
1296 verts
[3].t
= tex
->Ttop
;
1298 /* upload new vertex data */
1299 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1302 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1304 if (mask
& GL_COLOR_BUFFER_BIT
) {
1305 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1307 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1308 mask
&= ~GL_COLOR_BUFFER_BIT
;
1311 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1312 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1315 init_blit_depth_pixels(ctx
);
1317 /* maybe change tex format here */
1318 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1320 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1321 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1323 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1324 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1326 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1327 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1328 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1329 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1330 _mesa_DepthFunc(GL_ALWAYS
);
1331 _mesa_DepthMask(GL_TRUE
);
1333 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1334 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1340 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1341 /* XXX can't easily do stencil */
1344 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1346 _mesa_meta_end(ctx
);
1349 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1350 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1356 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1359 _mesa_meta_Clear(GLcontext
*ctx
, GLbitfield buffers
)
1361 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1363 GLfloat x
, y
, z
, r
, g
, b
, a
;
1365 struct vertex verts
[4];
1366 /* save all state but scissor, pixel pack/unpack */
1367 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1369 if (buffers
& BUFFER_BITS_COLOR
) {
1370 /* if clearing color buffers, don't save/restore colormask */
1371 metaSave
-= META_COLOR_MASK
;
1374 _mesa_meta_begin(ctx
, metaSave
);
1376 if (clear
->ArrayObj
== 0) {
1377 /* one-time setup */
1379 /* create vertex array object */
1380 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1381 _mesa_BindVertexArray(clear
->ArrayObj
);
1383 /* create vertex array buffer */
1384 _mesa_GenBuffersARB(1, &clear
->VBO
);
1385 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1387 /* setup vertex arrays */
1388 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1389 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1390 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1391 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1394 _mesa_BindVertexArray(clear
->ArrayObj
);
1395 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1398 /* GL_COLOR_BUFFER_BIT */
1399 if (buffers
& BUFFER_BITS_COLOR
) {
1400 /* leave colormask, glDrawBuffer state as-is */
1403 ASSERT(metaSave
& META_COLOR_MASK
);
1404 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1407 /* GL_DEPTH_BUFFER_BIT */
1408 if (buffers
& BUFFER_BIT_DEPTH
) {
1409 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1410 _mesa_DepthFunc(GL_ALWAYS
);
1411 _mesa_DepthMask(GL_TRUE
);
1414 assert(!ctx
->Depth
.Test
);
1417 /* GL_STENCIL_BUFFER_BIT */
1418 if (buffers
& BUFFER_BIT_STENCIL
) {
1419 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1420 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1421 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1422 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1423 ctx
->Stencil
.Clear
& 0x7fffffff,
1424 ctx
->Stencil
.WriteMask
[0]);
1427 assert(!ctx
->Stencil
.Enabled
);
1430 /* vertex positions/colors */
1432 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1433 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1434 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1435 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1436 const GLfloat z
= 1.0 - 2.0 * ctx
->Depth
.Clear
;
1453 for (i
= 0; i
< 4; i
++) {
1454 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1455 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1456 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1457 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1460 /* upload new vertex data */
1461 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1462 GL_DYNAMIC_DRAW_ARB
);
1466 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1468 _mesa_meta_end(ctx
);
1473 * Meta implementation of ctx->Driver.CopyPixels() in terms
1474 * of texture mapping and polygon rendering.
1477 _mesa_meta_CopyPixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1478 GLsizei width
, GLsizei height
,
1479 GLint dstX
, GLint dstY
, GLenum type
)
1481 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1482 struct temp_texture
*tex
= get_temp_texture(ctx
);
1484 GLfloat x
, y
, z
, s
, t
;
1486 struct vertex verts
[4];
1488 GLenum intFormat
= GL_RGBA
;
1490 if (type
!= GL_COLOR
||
1491 ctx
->_ImageTransferState
||
1493 width
> tex
->MaxSize
||
1494 height
> tex
->MaxSize
) {
1495 /* XXX avoid this fallback */
1496 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1500 /* Most GL state applies to glCopyPixels, but a there's a few things
1501 * we need to override:
1503 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1510 if (copypix
->ArrayObj
== 0) {
1511 /* one-time setup */
1513 /* create vertex array object */
1514 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1515 _mesa_BindVertexArray(copypix
->ArrayObj
);
1517 /* create vertex array buffer */
1518 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1519 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1520 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1521 NULL
, GL_DYNAMIC_DRAW_ARB
);
1523 /* setup vertex arrays */
1524 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1525 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1526 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1527 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1530 _mesa_BindVertexArray(copypix
->ArrayObj
);
1531 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1534 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1536 /* vertex positions, texcoords (after texture allocation!) */
1538 const GLfloat dstX0
= (GLfloat
) dstX
;
1539 const GLfloat dstY0
= (GLfloat
) dstY
;
1540 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1541 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1542 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1552 verts
[1].s
= tex
->Sright
;
1557 verts
[2].s
= tex
->Sright
;
1558 verts
[2].t
= tex
->Ttop
;
1563 verts
[3].t
= tex
->Ttop
;
1565 /* upload new vertex data */
1566 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1569 /* Alloc/setup texture */
1570 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1571 GL_RGBA
, GL_NEAREST
);
1573 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1575 /* draw textured quad */
1576 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1578 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1580 _mesa_meta_end(ctx
);
1586 * When the glDrawPixels() image size is greater than the max rectangle
1587 * texture size we use this function to break the glDrawPixels() image
1588 * into tiles which fit into the max texture size.
1591 tiled_draw_pixels(GLcontext
*ctx
,
1593 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1594 GLenum format
, GLenum type
,
1595 const struct gl_pixelstore_attrib
*unpack
,
1596 const GLvoid
*pixels
)
1598 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1601 if (tileUnpack
.RowLength
== 0)
1602 tileUnpack
.RowLength
= width
;
1604 for (i
= 0; i
< width
; i
+= tileSize
) {
1605 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1606 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1608 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1610 for (j
= 0; j
< height
; j
+= tileSize
) {
1611 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1612 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1614 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1616 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1617 format
, type
, &tileUnpack
, pixels
);
1624 * One-time init for drawing stencil pixels.
1627 init_draw_stencil_pixels(GLcontext
*ctx
)
1629 /* This program is run eight times, once for each stencil bit.
1630 * The stencil values to draw are found in an 8-bit alpha texture.
1631 * We read the texture/stencil value and test if bit 'b' is set.
1632 * If the bit is not set, use KIL to kill the fragment.
1633 * Finally, we use the stencil test to update the stencil buffer.
1635 * The basic algorithm for checking if a bit is set is:
1636 * if (is_odd(value / (1 << bit)))
1637 * result is one (or non-zero).
1640 * The program parameter contains three values:
1641 * parm.x = 255 / (1 << bit)
1645 static const char *program
=
1647 "PARAM parm = program.local[0]; \n"
1649 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1650 "# t = t * 255 / bit \n"
1651 "MUL t.x, t.a, parm.x; \n"
1654 "SUB t.x, t.x, t.y; \n"
1656 "MUL t.x, t.x, parm.y; \n"
1657 "# t = fract(t.x) \n"
1658 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1659 "# t.x = (t.x == 0 ? 1 : 0) \n"
1660 "SGE t.x, -t.x, parm.z; \n"
1662 "# for debug only \n"
1663 "#MOV result.color, t.x; \n"
1665 char program2
[1000];
1666 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1667 struct temp_texture
*tex
= get_temp_texture(ctx
);
1668 const char *texTarget
;
1670 assert(drawpix
->StencilFP
== 0);
1672 /* replace %s with "RECT" or "2D" */
1673 assert(strlen(program
) + 4 < sizeof(program2
));
1674 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1678 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1680 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1681 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1682 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1683 strlen(program2
), (const GLubyte
*) program2
);
1688 * One-time init for drawing depth pixels.
1691 init_draw_depth_pixels(GLcontext
*ctx
)
1693 static const char *program
=
1695 "PARAM color = program.local[0]; \n"
1696 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1697 "MOV result.color, color; \n"
1700 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1701 struct temp_texture
*tex
= get_temp_texture(ctx
);
1702 const char *texTarget
;
1704 assert(drawpix
->DepthFP
== 0);
1706 /* replace %s with "RECT" or "2D" */
1707 assert(strlen(program
) + 4 < sizeof(program2
));
1708 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1712 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1714 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1715 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1716 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1717 strlen(program2
), (const GLubyte
*) program2
);
1722 * Meta implementation of ctx->Driver.DrawPixels() in terms
1723 * of texture mapping and polygon rendering.
1726 _mesa_meta_DrawPixels(GLcontext
*ctx
,
1727 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1728 GLenum format
, GLenum type
,
1729 const struct gl_pixelstore_attrib
*unpack
,
1730 const GLvoid
*pixels
)
1732 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1733 struct temp_texture
*tex
= get_temp_texture(ctx
);
1734 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1735 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1737 GLfloat x
, y
, z
, s
, t
;
1739 struct vertex verts
[4];
1740 GLenum texIntFormat
;
1741 GLboolean fallback
, newTex
;
1742 GLbitfield metaExtraSave
= 0x0;
1746 * Determine if we can do the glDrawPixels with texture mapping.
1748 fallback
= GL_FALSE
;
1749 if (ctx
->_ImageTransferState
||
1754 if (_mesa_is_color_format(format
)) {
1755 /* use more compact format when possible */
1756 /* XXX disable special case for GL_LUMINANCE for now to work around
1757 * apparent i965 driver bug (see bug #23670).
1759 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1760 texIntFormat
= format
;
1762 texIntFormat
= GL_RGBA
;
1764 else if (_mesa_is_stencil_format(format
)) {
1765 if (ctx
->Extensions
.ARB_fragment_program
&&
1766 ctx
->Pixel
.IndexShift
== 0 &&
1767 ctx
->Pixel
.IndexOffset
== 0 &&
1768 type
== GL_UNSIGNED_BYTE
) {
1769 /* We'll store stencil as alpha. This only works for GLubyte
1770 * image data because of how incoming values are mapped to alpha
1773 texIntFormat
= GL_ALPHA
;
1774 metaExtraSave
= (META_COLOR_MASK
|
1783 else if (_mesa_is_depth_format(format
)) {
1784 if (ctx
->Extensions
.ARB_depth_texture
&&
1785 ctx
->Extensions
.ARB_fragment_program
) {
1786 texIntFormat
= GL_DEPTH_COMPONENT
;
1787 metaExtraSave
= (META_SHADER
);
1798 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1799 format
, type
, unpack
, pixels
);
1804 * Check image size against max texture size, draw as tiles if needed.
1806 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1807 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1808 format
, type
, unpack
, pixels
);
1812 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1813 * but a there's a few things we need to override:
1815 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1823 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1825 /* vertex positions, texcoords (after texture allocation!) */
1827 const GLfloat x0
= (GLfloat
) x
;
1828 const GLfloat y0
= (GLfloat
) y
;
1829 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1830 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1831 const GLfloat z
= ctx
->Current
.RasterPos
[2];
1841 verts
[1].s
= tex
->Sright
;
1846 verts
[2].s
= tex
->Sright
;
1847 verts
[2].t
= tex
->Ttop
;
1852 verts
[3].t
= tex
->Ttop
;
1855 if (drawpix
->ArrayObj
== 0) {
1856 /* one-time setup: create vertex array object */
1857 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1859 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1861 /* create vertex array buffer */
1862 _mesa_GenBuffersARB(1, &vbo
);
1863 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1864 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1865 verts
, GL_DYNAMIC_DRAW_ARB
);
1867 /* setup vertex arrays */
1868 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1869 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1870 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1871 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1873 /* set given unpack params */
1874 ctx
->Unpack
= *unpack
;
1876 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1878 if (_mesa_is_stencil_format(format
)) {
1879 /* Drawing stencil */
1882 if (!drawpix
->StencilFP
)
1883 init_draw_stencil_pixels(ctx
);
1885 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1886 GL_ALPHA
, type
, pixels
);
1888 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1890 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1892 /* set all stencil bits to 0 */
1893 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1894 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1895 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1897 /* set stencil bits to 1 where needed */
1898 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1900 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1901 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1903 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1904 const GLuint mask
= 1 << bit
;
1905 if (mask
& origStencilMask
) {
1906 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1907 _mesa_StencilMask(mask
);
1909 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1910 255.0 / mask
, 0.5, 0.0, 0.0);
1912 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1916 else if (_mesa_is_depth_format(format
)) {
1918 if (!drawpix
->DepthFP
)
1919 init_draw_depth_pixels(ctx
);
1921 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1922 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1924 /* polygon color = current raster color */
1925 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1926 ctx
->Current
.RasterColor
);
1928 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1929 format
, type
, pixels
);
1931 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1935 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1936 format
, type
, pixels
);
1937 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1940 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1942 _mesa_DeleteBuffersARB(1, &vbo
);
1944 /* restore unpack params */
1945 ctx
->Unpack
= unpackSave
;
1947 _mesa_meta_end(ctx
);
1952 * Do glBitmap with a alpha texture quad. Use the alpha test to
1953 * cull the 'off' bits. If alpha test is already enabled, fall back
1954 * to swrast (should be a rare case).
1955 * A bitmap cache as in the gallium/mesa state tracker would
1956 * improve performance a lot.
1959 _mesa_meta_Bitmap(GLcontext
*ctx
,
1960 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1961 const struct gl_pixelstore_attrib
*unpack
,
1962 const GLubyte
*bitmap1
)
1964 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1965 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1966 const GLenum texIntFormat
= GL_ALPHA
;
1967 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
1969 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
1971 struct vertex verts
[4];
1976 * Check if swrast fallback is needed.
1978 if (ctx
->_ImageTransferState
||
1979 ctx
->Color
.AlphaEnabled
||
1981 ctx
->Texture
._EnabledUnits
||
1982 width
> tex
->MaxSize
||
1983 height
> tex
->MaxSize
) {
1984 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
1988 /* Most GL state applies to glBitmap (like blending, stencil, etc),
1989 * but a there's a few things we need to override:
1991 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
1993 META_RASTERIZATION
|
2000 if (bitmap
->ArrayObj
== 0) {
2001 /* one-time setup */
2003 /* create vertex array object */
2004 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2005 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2007 /* create vertex array buffer */
2008 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2009 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2010 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2011 NULL
, GL_DYNAMIC_DRAW_ARB
);
2013 /* setup vertex arrays */
2014 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2015 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2016 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2017 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2018 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2019 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2022 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2023 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2026 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2028 /* vertex positions, texcoords, colors (after texture allocation!) */
2030 const GLfloat x0
= (GLfloat
) x
;
2031 const GLfloat y0
= (GLfloat
) y
;
2032 const GLfloat x1
= (GLfloat
) (x
+ width
);
2033 const GLfloat y1
= (GLfloat
) (y
+ height
);
2034 const GLfloat z
= ctx
->Current
.RasterPos
[2];
2045 verts
[1].s
= tex
->Sright
;
2050 verts
[2].s
= tex
->Sright
;
2051 verts
[2].t
= tex
->Ttop
;
2056 verts
[3].t
= tex
->Ttop
;
2058 for (i
= 0; i
< 4; i
++) {
2059 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2060 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2061 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2062 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2065 /* upload new vertex data */
2066 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2069 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2071 _mesa_meta_end(ctx
);
2075 bitmap8
= (GLubyte
*) calloc(1, width
* height
);
2077 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2078 bitmap8
, width
, 0xff);
2080 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2082 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2083 _mesa_AlphaFunc(GL_GREATER
, 0.0);
2085 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2086 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2088 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2090 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2095 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2097 _mesa_meta_end(ctx
);
2102 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2103 * software fallback. The fallback path will require that the texture
2104 * images are mapped.
2105 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2108 _mesa_meta_check_generate_mipmap_fallback(GLcontext
*ctx
, GLenum target
,
2109 struct gl_texture_object
*texObj
)
2111 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2112 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2113 struct gl_texture_image
*baseImage
;
2117 /* check for fallbacks */
2118 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2119 target
== GL_TEXTURE_3D
) {
2123 srcLevel
= texObj
->BaseLevel
;
2124 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2125 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2130 * Test that we can actually render in the texture's format.
2133 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2134 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2136 if (target
== GL_TEXTURE_1D
) {
2137 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2138 GL_COLOR_ATTACHMENT0_EXT
,
2139 target
, texObj
->Name
, srcLevel
);
2142 /* other work is needed to enable 3D mipmap generation */
2143 else if (target
== GL_TEXTURE_3D
) {
2145 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2146 GL_COLOR_ATTACHMENT0_EXT
,
2147 target
, texObj
->Name
, srcLevel
, zoffset
);
2152 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2153 GL_COLOR_ATTACHMENT0_EXT
,
2154 target
, texObj
->Name
, srcLevel
);
2157 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2159 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2161 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2170 * Called via ctx->Driver.GenerateMipmap()
2171 * Note: texture borders and 3D texture support not yet complete.
2174 _mesa_meta_GenerateMipmap(GLcontext
*ctx
, GLenum target
,
2175 struct gl_texture_object
*texObj
)
2177 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2179 GLfloat x
, y
, s
, t
, r
;
2181 struct vertex verts
[4];
2182 const GLuint baseLevel
= texObj
->BaseLevel
;
2183 const GLuint maxLevel
= texObj
->MaxLevel
;
2184 const GLenum minFilterSave
= texObj
->MinFilter
;
2185 const GLenum magFilterSave
= texObj
->MagFilter
;
2186 const GLint baseLevelSave
= texObj
->BaseLevel
;
2187 const GLint maxLevelSave
= texObj
->MaxLevel
;
2188 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2189 const GLenum wrapSSave
= texObj
->WrapS
;
2190 const GLenum wrapTSave
= texObj
->WrapT
;
2191 const GLenum wrapRSave
= texObj
->WrapR
;
2192 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2193 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2198 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2199 _mesa_generate_mipmap(ctx
, target
, texObj
);
2203 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2204 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2205 faceTarget
= target
;
2206 target
= GL_TEXTURE_CUBE_MAP
;
2209 faceTarget
= target
;
2212 _mesa_meta_begin(ctx
, META_ALL
);
2214 if (original_active_unit
!= 0)
2215 _mesa_BindTexture(target
, texObj
->Name
);
2217 if (mipmap
->ArrayObj
== 0) {
2218 /* one-time setup */
2220 /* create vertex array object */
2221 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2222 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2224 /* create vertex array buffer */
2225 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2226 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2227 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2228 NULL
, GL_DYNAMIC_DRAW_ARB
);
2230 /* setup vertex arrays */
2231 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2232 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2233 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2234 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2237 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2238 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2242 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2244 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2246 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
2247 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2248 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2249 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2250 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2251 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2253 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2255 /* setup texcoords once (XXX what about border?) */
2256 switch (faceTarget
) {
2278 static const GLfloat st
[4][2] = {
2279 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2283 /* loop over quad verts */
2284 for (i
= 0; i
< 4; i
++) {
2285 /* Compute sc = +/-scale and tc = +/-scale.
2286 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2287 * though that can still sometimes happen with this scale factor...
2289 const GLfloat scale
= 0.9999f
;
2290 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2291 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2293 switch (faceTarget
) {
2294 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2299 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2304 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2314 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2319 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2331 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2333 /* setup vertex positions */
2344 /* upload new vertex data */
2345 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2348 /* setup projection matrix */
2349 _mesa_MatrixMode(GL_PROJECTION
);
2350 _mesa_LoadIdentity();
2351 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2353 /* texture is already locked, unlock now */
2354 _mesa_unlock_texture(ctx
, texObj
);
2356 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2357 const struct gl_texture_image
*srcImage
;
2358 const GLuint srcLevel
= dstLevel
- 1;
2359 GLsizei srcWidth
, srcHeight
, srcDepth
;
2360 GLsizei dstWidth
, dstHeight
, dstDepth
;
2363 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2364 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2366 /* src size w/out border */
2367 srcWidth
= srcImage
->Width
- 2 * border
;
2368 srcHeight
= srcImage
->Height
- 2 * border
;
2369 srcDepth
= srcImage
->Depth
- 2 * border
;
2371 /* new dst size w/ border */
2372 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2373 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2374 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2376 if (dstWidth
== srcImage
->Width
&&
2377 dstHeight
== srcImage
->Height
&&
2378 dstDepth
== srcImage
->Depth
) {
2383 /* Create empty dest image */
2384 if (target
== GL_TEXTURE_1D
) {
2385 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2387 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2389 else if (target
== GL_TEXTURE_3D
) {
2390 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2391 dstWidth
, dstHeight
, dstDepth
, border
,
2392 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2396 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2397 dstWidth
, dstHeight
, border
,
2398 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2400 if (target
== GL_TEXTURE_CUBE_MAP
) {
2401 /* If texturing from a cube, we need to make sure all src faces
2402 * have been defined (even if we're not sampling from them.)
2403 * Otherwise the texture object will be 'incomplete' and
2404 * texturing from it will not be allowed.
2407 for (face
= 0; face
< 6; face
++) {
2408 if (!texObj
->Image
[face
][srcLevel
] ||
2409 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2410 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2411 srcLevel
, srcImage
->InternalFormat
,
2412 srcWidth
, srcHeight
, border
,
2413 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2419 /* limit sampling to src level */
2420 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2421 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2423 /* Set to draw into the current dstLevel */
2424 if (target
== GL_TEXTURE_1D
) {
2425 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2426 GL_COLOR_ATTACHMENT0_EXT
,
2431 else if (target
== GL_TEXTURE_3D
) {
2432 GLint zoffset
= 0; /* XXX unfinished */
2433 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2434 GL_COLOR_ATTACHMENT0_EXT
,
2441 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2442 GL_COLOR_ATTACHMENT0_EXT
,
2448 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2451 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2452 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2457 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2458 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2460 /* setup viewport */
2461 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2463 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2466 _mesa_lock_texture(ctx
, texObj
); /* relock */
2468 _mesa_meta_end(ctx
);
2470 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2471 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2472 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
2473 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2474 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2475 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2476 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2477 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2479 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2484 * Determine the GL data type to use for the temporary image read with
2485 * ReadPixels() and passed to Tex[Sub]Image().
2488 get_temp_image_type(GLcontext
*ctx
, GLenum baseFormat
)
2490 switch (baseFormat
) {
2495 case GL_LUMINANCE_ALPHA
:
2497 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2498 return GL_UNSIGNED_BYTE
;
2499 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2500 return GL_UNSIGNED_SHORT
;
2503 case GL_DEPTH_COMPONENT
:
2504 return GL_UNSIGNED_INT
;
2505 case GL_DEPTH_STENCIL
:
2506 return GL_UNSIGNED_INT_24_8
;
2508 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2515 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2516 * Have to be careful with locking and meta state for pixel transfer.
2519 copy_tex_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2520 GLenum internalFormat
, GLint x
, GLint y
,
2521 GLsizei width
, GLsizei height
, GLint border
)
2523 struct gl_texture_object
*texObj
;
2524 struct gl_texture_image
*texImage
;
2525 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2526 GLenum format
, type
;
2530 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2531 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2533 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2534 type
= get_temp_image_type(ctx
, format
);
2535 bpp
= _mesa_bytes_per_pixel(format
, type
);
2537 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2542 * Alloc image buffer (XXX could use a PBO)
2544 buf
= malloc(width
* height
* bpp
);
2546 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2550 if (texImage
->TexFormat
== MESA_FORMAT_NONE
)
2551 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
,
2556 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2559 * Read image from framebuffer (disable pixel transfer ops)
2561 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2562 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2563 format
, type
, &ctx
->Pack
, buf
);
2564 _mesa_meta_end(ctx
);
2567 * Prepare for new texture image size/data
2569 #if FEATURE_convolve
2570 if (_mesa_is_color_format(internalFormat
)) {
2571 _mesa_adjust_image_for_convolution(ctx
, 2,
2572 &postConvWidth
, &postConvHeight
);
2576 if (texImage
->Data
) {
2577 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2580 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2581 postConvWidth
, postConvHeight
, 1,
2582 border
, internalFormat
);
2585 * Store texture data (with pixel transfer ops)
2587 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2589 _mesa_update_state(ctx
); /* to update pixel transfer state */
2591 if (target
== GL_TEXTURE_1D
) {
2592 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2593 width
, border
, format
, type
,
2594 buf
, &ctx
->Unpack
, texObj
, texImage
);
2597 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2598 width
, height
, border
, format
, type
,
2599 buf
, &ctx
->Unpack
, texObj
, texImage
);
2601 _mesa_meta_end(ctx
);
2603 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2610 _mesa_meta_CopyTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2611 GLenum internalFormat
, GLint x
, GLint y
,
2612 GLsizei width
, GLint border
)
2614 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2620 _mesa_meta_CopyTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2621 GLenum internalFormat
, GLint x
, GLint y
,
2622 GLsizei width
, GLsizei height
, GLint border
)
2624 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2625 width
, height
, border
);
2631 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2632 * Have to be careful with locking and meta state for pixel transfer.
2635 copy_tex_sub_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2636 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2638 GLsizei width
, GLsizei height
)
2640 struct gl_texture_object
*texObj
;
2641 struct gl_texture_image
*texImage
;
2642 GLenum format
, type
;
2646 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2647 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2649 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2650 type
= get_temp_image_type(ctx
, format
);
2651 bpp
= _mesa_bytes_per_pixel(format
, type
);
2653 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2658 * Alloc image buffer (XXX could use a PBO)
2660 buf
= malloc(width
* height
* bpp
);
2662 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2666 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2669 * Read image from framebuffer (disable pixel transfer ops)
2671 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2672 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2673 format
, type
, &ctx
->Pack
, buf
);
2674 _mesa_meta_end(ctx
);
2676 _mesa_update_state(ctx
); /* to update pixel transfer state */
2679 * Store texture data (with pixel transfer ops)
2681 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2682 if (target
== GL_TEXTURE_1D
) {
2683 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2684 width
, format
, type
, buf
,
2685 &ctx
->Unpack
, texObj
, texImage
);
2687 else if (target
== GL_TEXTURE_3D
) {
2688 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2689 width
, height
, 1, format
, type
, buf
,
2690 &ctx
->Unpack
, texObj
, texImage
);
2693 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2694 width
, height
, format
, type
, buf
,
2695 &ctx
->Unpack
, texObj
, texImage
);
2697 _mesa_meta_end(ctx
);
2699 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2706 _mesa_meta_CopyTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2708 GLint x
, GLint y
, GLsizei width
)
2710 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2716 _mesa_meta_CopyTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2717 GLint xoffset
, GLint yoffset
,
2719 GLsizei width
, GLsizei height
)
2721 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2722 x
, y
, width
, height
);
2727 _mesa_meta_CopyTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
2728 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2730 GLsizei width
, GLsizei height
)
2732 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2733 x
, y
, width
, height
);
2738 _mesa_meta_CopyColorTable(GLcontext
*ctx
,
2739 GLenum target
, GLenum internalformat
,
2740 GLint x
, GLint y
, GLsizei width
)
2744 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2746 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2751 * Read image from framebuffer (disable pixel transfer ops)
2753 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2754 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2755 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2757 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2759 _mesa_meta_end(ctx
);
2766 _mesa_meta_CopyColorSubTable(GLcontext
*ctx
,GLenum target
, GLsizei start
,
2767 GLint x
, GLint y
, GLsizei width
)
2771 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2773 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2778 * Read image from framebuffer (disable pixel transfer ops)
2780 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2781 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2782 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2784 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2786 _mesa_meta_end(ctx
);
2793 _mesa_meta_CopyConvolutionFilter1D(GLcontext
*ctx
, GLenum target
,
2794 GLenum internalFormat
,
2795 GLint x
, GLint y
, GLsizei width
)
2799 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2801 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyConvolutionFilter2D");
2806 * Read image from framebuffer (disable pixel transfer ops)
2808 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2809 _mesa_update_state(ctx
);
2810 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2811 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2813 _mesa_ConvolutionFilter1D(target
, internalFormat
, width
,
2814 GL_RGBA
, GL_FLOAT
, buf
);
2816 _mesa_meta_end(ctx
);
2823 _mesa_meta_CopyConvolutionFilter2D(GLcontext
*ctx
, GLenum target
,
2824 GLenum internalFormat
, GLint x
, GLint y
,
2825 GLsizei width
, GLsizei height
)
2829 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
2831 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyConvolutionFilter2D");
2836 * Read image from framebuffer (disable pixel transfer ops)
2838 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2839 _mesa_update_state(ctx
);
2841 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2842 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2844 _mesa_ConvolutionFilter2D(target
, internalFormat
, width
, height
,
2845 GL_RGBA
, GL_FLOAT
, buf
);
2847 _mesa_meta_end(ctx
);