2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/convolve.h"
44 #include "main/depth.h"
45 #include "main/enable.h"
46 #include "main/fbobject.h"
47 #include "main/formats.h"
48 #include "main/image.h"
49 #include "main/macros.h"
50 #include "main/matrix.h"
51 #include "main/mipmap.h"
52 #include "main/polygon.h"
53 #include "main/readpix.h"
54 #include "main/scissor.h"
55 #include "main/shaderapi.h"
56 #include "main/state.h"
57 #include "main/stencil.h"
58 #include "main/texobj.h"
59 #include "main/texenv.h"
60 #include "main/teximage.h"
61 #include "main/texparam.h"
62 #include "main/texstate.h"
63 #include "main/varray.h"
64 #include "main/viewport.h"
65 #include "program/program.h"
66 #include "swrast/swrast.h"
67 #include "drivers/common/meta.h"
70 /** Return offset in bytes of the field within a vertex struct */
71 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
75 * Flags passed to _mesa_meta_begin().
79 #define META_ALPHA_TEST 0x1
80 #define META_BLEND 0x2 /**< includes logicop */
81 #define META_COLOR_MASK 0x4
82 #define META_DEPTH_TEST 0x8
84 #define META_PIXEL_STORE 0x20
85 #define META_PIXEL_TRANSFER 0x40
86 #define META_RASTERIZATION 0x80
87 #define META_SCISSOR 0x100
88 #define META_SHADER 0x200
89 #define META_STENCIL_TEST 0x400
90 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
91 #define META_TEXTURE 0x1000
92 #define META_VERTEX 0x2000
93 #define META_VIEWPORT 0x4000
98 * State which we may save/restore across meta ops.
99 * XXX this may be incomplete...
103 GLbitfield SavedState
; /**< bitmask of META_* flags */
105 /** META_ALPHA_TEST */
106 GLboolean AlphaEnabled
;
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 */
427 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
430 if (state
& META_SHADER
) {
431 if (ctx
->Extensions
.ARB_vertex_program
) {
432 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
433 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
434 ctx
->VertexProgram
.Current
);
435 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
438 if (ctx
->Extensions
.ARB_fragment_program
) {
439 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
440 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
441 ctx
->FragmentProgram
.Current
);
442 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
445 if (ctx
->Extensions
.ARB_shader_objects
) {
446 save
->Shader
= ctx
->Shader
.CurrentProgram
?
447 ctx
->Shader
.CurrentProgram
->Name
: 0;
448 _mesa_UseProgramObjectARB(0);
452 if (state
& META_STENCIL_TEST
) {
453 save
->Stencil
= ctx
->Stencil
; /* struct copy */
454 if (ctx
->Stencil
.Enabled
)
455 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
456 /* NOTE: other stencil state not reset */
459 if (state
& META_TEXTURE
) {
462 save
->ActiveUnit
= ctx
->Texture
.CurrentUnit
;
463 save
->ClientActiveUnit
= ctx
->Array
.ActiveTexture
;
464 save
->EnvMode
= ctx
->Texture
.Unit
[0].EnvMode
;
466 /* Disable all texture units */
467 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
468 save
->TexEnabled
[u
] = ctx
->Texture
.Unit
[u
].Enabled
;
469 save
->TexGenEnabled
[u
] = ctx
->Texture
.Unit
[u
].TexGenEnabled
;
470 if (ctx
->Texture
.Unit
[u
].Enabled
||
471 ctx
->Texture
.Unit
[u
].TexGenEnabled
) {
472 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
473 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_FALSE
);
474 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_FALSE
);
475 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_FALSE
);
476 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_FALSE
);
477 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_FALSE
);
478 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_FALSE
);
479 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_FALSE
);
480 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_FALSE
);
481 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_FALSE
);
485 /* save current texture objects for unit[0] only */
486 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
487 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
],
488 ctx
->Texture
.Unit
[0].CurrentTex
[tgt
]);
491 /* set defaults for unit[0] */
492 _mesa_ActiveTextureARB(GL_TEXTURE0
);
493 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
494 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
497 if (state
& META_TRANSFORM
) {
498 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
499 memcpy(save
->ModelviewMatrix
, ctx
->ModelviewMatrixStack
.Top
->m
,
500 16 * sizeof(GLfloat
));
501 memcpy(save
->ProjectionMatrix
, ctx
->ProjectionMatrixStack
.Top
->m
,
502 16 * sizeof(GLfloat
));
503 memcpy(save
->TextureMatrix
, ctx
->TextureMatrixStack
[0].Top
->m
,
504 16 * sizeof(GLfloat
));
505 save
->MatrixMode
= ctx
->Transform
.MatrixMode
;
506 /* set 1:1 vertex:pixel coordinate transform */
507 _mesa_ActiveTextureARB(GL_TEXTURE0
);
508 _mesa_MatrixMode(GL_TEXTURE
);
509 _mesa_LoadIdentity();
510 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
511 _mesa_MatrixMode(GL_MODELVIEW
);
512 _mesa_LoadIdentity();
513 _mesa_MatrixMode(GL_PROJECTION
);
514 _mesa_LoadIdentity();
515 _mesa_Ortho(0.0, ctx
->DrawBuffer
->Width
,
516 0.0, ctx
->DrawBuffer
->Height
,
518 save
->ClipPlanesEnabled
= ctx
->Transform
.ClipPlanesEnabled
;
519 if (ctx
->Transform
.ClipPlanesEnabled
) {
521 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
522 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_FALSE
);
527 if (state
& META_VERTEX
) {
528 /* save vertex array object state */
529 _mesa_reference_array_object(ctx
, &save
->ArrayObj
,
530 ctx
->Array
.ArrayObj
);
531 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
,
532 ctx
->Array
.ArrayBufferObj
);
533 /* set some default state? */
536 if (state
& META_VIEWPORT
) {
537 /* save viewport state */
538 save
->ViewportX
= ctx
->Viewport
.X
;
539 save
->ViewportY
= ctx
->Viewport
.Y
;
540 save
->ViewportW
= ctx
->Viewport
.Width
;
541 save
->ViewportH
= ctx
->Viewport
.Height
;
542 /* set viewport to match window size */
543 if (ctx
->Viewport
.X
!= 0 ||
544 ctx
->Viewport
.Y
!= 0 ||
545 ctx
->Viewport
.Width
!= ctx
->DrawBuffer
->Width
||
546 ctx
->Viewport
.Height
!= ctx
->DrawBuffer
->Height
) {
547 _mesa_set_viewport(ctx
, 0, 0,
548 ctx
->DrawBuffer
->Width
, ctx
->DrawBuffer
->Height
);
550 /* save depth range state */
551 save
->DepthNear
= ctx
->Viewport
.Near
;
552 save
->DepthFar
= ctx
->Viewport
.Far
;
553 /* set depth range to default */
554 _mesa_DepthRange(0.0, 1.0);
559 save
->Lighting
= ctx
->Light
.Enabled
;
560 if (ctx
->Light
.Enabled
)
561 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_FALSE
);
567 * Leave meta state. This is like a light-weight version of glPopAttrib().
570 _mesa_meta_end(GLcontext
*ctx
)
572 struct save_state
*save
= &ctx
->Meta
->Save
;
573 const GLbitfield state
= save
->SavedState
;
575 if (state
& META_ALPHA_TEST
) {
576 if (ctx
->Color
.AlphaEnabled
!= save
->AlphaEnabled
)
577 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, save
->AlphaEnabled
);
580 if (state
& META_BLEND
) {
581 if (ctx
->Color
.BlendEnabled
!= save
->BlendEnabled
) {
582 if (ctx
->Extensions
.EXT_draw_buffers2
) {
584 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
585 _mesa_set_enablei(ctx
, GL_BLEND
, i
, (save
->BlendEnabled
>> i
) & 1);
589 _mesa_set_enable(ctx
, GL_BLEND
, (save
->BlendEnabled
& 1));
592 if (ctx
->Color
.ColorLogicOpEnabled
!= save
->ColorLogicOpEnabled
)
593 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, save
->ColorLogicOpEnabled
);
596 if (state
& META_COLOR_MASK
) {
598 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
599 if (!TEST_EQ_4V(ctx
->Color
.ColorMask
[i
], save
->ColorMask
[i
])) {
601 _mesa_ColorMask(save
->ColorMask
[i
][0], save
->ColorMask
[i
][1],
602 save
->ColorMask
[i
][2], save
->ColorMask
[i
][3]);
605 _mesa_ColorMaskIndexed(i
,
606 save
->ColorMask
[i
][0],
607 save
->ColorMask
[i
][1],
608 save
->ColorMask
[i
][2],
609 save
->ColorMask
[i
][3]);
615 if (state
& META_DEPTH_TEST
) {
616 if (ctx
->Depth
.Test
!= save
->Depth
.Test
)
617 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, save
->Depth
.Test
);
618 _mesa_DepthFunc(save
->Depth
.Func
);
619 _mesa_DepthMask(save
->Depth
.Mask
);
622 if (state
& META_FOG
) {
623 _mesa_set_enable(ctx
, GL_FOG
, save
->Fog
);
626 if (state
& META_PIXEL_STORE
) {
627 ctx
->Pack
= save
->Pack
;
628 ctx
->Unpack
= save
->Unpack
;
631 if (state
& META_PIXEL_TRANSFER
) {
632 ctx
->Pixel
.RedScale
= save
->RedScale
;
633 ctx
->Pixel
.RedBias
= save
->RedBias
;
634 ctx
->Pixel
.GreenScale
= save
->GreenScale
;
635 ctx
->Pixel
.GreenBias
= save
->GreenBias
;
636 ctx
->Pixel
.BlueScale
= save
->BlueScale
;
637 ctx
->Pixel
.BlueBias
= save
->BlueBias
;
638 ctx
->Pixel
.AlphaScale
= save
->AlphaScale
;
639 ctx
->Pixel
.AlphaBias
= save
->AlphaBias
;
640 ctx
->Pixel
.MapColorFlag
= save
->MapColorFlag
;
641 ctx
->Pixel
.Convolution1DEnabled
= save
->Convolution1DEnabled
;
642 ctx
->Pixel
.Convolution2DEnabled
= save
->Convolution2DEnabled
;
643 ctx
->Pixel
.Separable2DEnabled
= save
->Separable2DEnabled
;
645 ctx
->NewState
|=_NEW_PIXEL
;
648 if (state
& META_RASTERIZATION
) {
649 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
650 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
651 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
652 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
653 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
654 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
657 if (state
& META_SCISSOR
) {
658 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
659 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
660 save
->Scissor
.Width
, save
->Scissor
.Height
);
663 if (state
& META_SHADER
) {
664 if (ctx
->Extensions
.ARB_vertex_program
) {
665 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
666 save
->VertexProgramEnabled
);
667 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
668 save
->VertexProgram
);
669 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
672 if (ctx
->Extensions
.ARB_fragment_program
) {
673 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
674 save
->FragmentProgramEnabled
);
675 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
676 save
->FragmentProgram
);
677 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
680 if (ctx
->Extensions
.ARB_shader_objects
) {
681 _mesa_UseProgramObjectARB(save
->Shader
);
685 if (state
& META_STENCIL_TEST
) {
686 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
688 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
689 _mesa_ClearStencil(stencil
->Clear
);
690 if (ctx
->Extensions
.EXT_stencil_two_side
) {
691 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
692 stencil
->TestTwoSide
);
693 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
694 ? GL_BACK
: GL_FRONT
);
697 _mesa_StencilFuncSeparate(GL_FRONT
,
698 stencil
->Function
[0],
700 stencil
->ValueMask
[0]);
701 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
702 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
703 stencil
->ZFailFunc
[0],
704 stencil
->ZPassFunc
[0]);
706 _mesa_StencilFuncSeparate(GL_BACK
,
707 stencil
->Function
[1],
709 stencil
->ValueMask
[1]);
710 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
711 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
712 stencil
->ZFailFunc
[1],
713 stencil
->ZPassFunc
[1]);
716 if (state
& META_TEXTURE
) {
719 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
721 /* restore texenv for unit[0] */
722 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
724 /* restore texture objects for unit[0] only */
725 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
726 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
727 save
->CurrentTexture
[tgt
]);
728 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
731 /* Re-enable textures, texgen */
732 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
733 if (save
->TexEnabled
[u
]) {
734 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
736 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
737 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
738 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
739 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
740 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
741 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
742 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
743 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
744 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
745 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
748 if (save
->TexGenEnabled
[u
]) {
749 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
751 if (save
->TexGenEnabled
[u
] & S_BIT
)
752 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
753 if (save
->TexGenEnabled
[u
] & T_BIT
)
754 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
755 if (save
->TexGenEnabled
[u
] & R_BIT
)
756 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
757 if (save
->TexGenEnabled
[u
] & Q_BIT
)
758 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
762 /* restore current unit state */
763 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
764 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
767 if (state
& META_TRANSFORM
) {
768 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
769 _mesa_ActiveTextureARB(GL_TEXTURE0
);
770 _mesa_MatrixMode(GL_TEXTURE
);
771 _mesa_LoadMatrixf(save
->TextureMatrix
);
772 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
774 _mesa_MatrixMode(GL_MODELVIEW
);
775 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
777 _mesa_MatrixMode(GL_PROJECTION
);
778 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
780 _mesa_MatrixMode(save
->MatrixMode
);
782 if (save
->ClipPlanesEnabled
) {
784 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
785 if (save
->ClipPlanesEnabled
& (1 << i
)) {
786 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
792 if (state
& META_VERTEX
) {
793 /* restore vertex buffer object */
794 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
795 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
797 /* restore vertex array object */
798 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
799 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
802 if (state
& META_VIEWPORT
) {
803 if (save
->ViewportX
!= ctx
->Viewport
.X
||
804 save
->ViewportY
!= ctx
->Viewport
.Y
||
805 save
->ViewportW
!= ctx
->Viewport
.Width
||
806 save
->ViewportH
!= ctx
->Viewport
.Height
) {
807 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
808 save
->ViewportW
, save
->ViewportH
);
810 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
814 if (save
->Lighting
) {
815 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
821 * Convert Z from a normalized value in the range [0, 1] to an object-space
822 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
823 * default/identity ortho projection results in the original Z value.
824 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
825 * value comes from the clear value or raster position.
827 static INLINE GLfloat
828 invert_z(GLfloat normZ
)
830 GLfloat objZ
= 1.0 - 2.0 * normZ
;
836 * One-time init for a temp_texture object.
837 * Choose tex target, compute max tex size, etc.
840 init_temp_texture(GLcontext
*ctx
, struct temp_texture
*tex
)
842 /* prefer texture rectangle */
843 if (ctx
->Extensions
.NV_texture_rectangle
) {
844 tex
->Target
= GL_TEXTURE_RECTANGLE
;
845 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
849 /* use 2D texture, NPOT if possible */
850 tex
->Target
= GL_TEXTURE_2D
;
851 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
852 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
854 tex
->MinSize
= 16; /* 16 x 16 at least */
855 assert(tex
->MaxSize
> 0);
857 _mesa_GenTextures(1, &tex
->TexObj
);
858 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
863 * Return pointer to temp_texture info for non-bitmap ops.
864 * This does some one-time init if needed.
866 static struct temp_texture
*
867 get_temp_texture(GLcontext
*ctx
)
869 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
872 init_temp_texture(ctx
, tex
);
880 * Return pointer to temp_texture info for _mesa_meta_bitmap().
881 * We use a separate texture for bitmaps to reduce texture
882 * allocation/deallocation.
884 static struct temp_texture
*
885 get_bitmap_temp_texture(GLcontext
*ctx
)
887 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
890 init_temp_texture(ctx
, tex
);
898 * Compute the width/height of texture needed to draw an image of the
899 * given size. Return a flag indicating whether the current texture
900 * can be re-used (glTexSubImage2D) or if a new texture needs to be
901 * allocated (glTexImage2D).
902 * Also, compute s/t texcoords for drawing.
904 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
907 alloc_texture(struct temp_texture
*tex
,
908 GLsizei width
, GLsizei height
, GLenum intFormat
)
910 GLboolean newTex
= GL_FALSE
;
912 ASSERT(width
<= tex
->MaxSize
);
913 ASSERT(height
<= tex
->MaxSize
);
915 if (width
> tex
->Width
||
916 height
> tex
->Height
||
917 intFormat
!= tex
->IntFormat
) {
918 /* alloc new texture (larger or different format) */
921 /* use non-power of two size */
922 tex
->Width
= MAX2(tex
->MinSize
, width
);
923 tex
->Height
= MAX2(tex
->MinSize
, height
);
926 /* find power of two size */
928 w
= h
= tex
->MinSize
;
937 tex
->IntFormat
= intFormat
;
942 /* compute texcoords */
943 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
944 tex
->Sright
= (GLfloat
) width
;
945 tex
->Ttop
= (GLfloat
) height
;
948 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
949 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
957 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
960 setup_copypix_texture(struct temp_texture
*tex
,
962 GLint srcX
, GLint srcY
,
963 GLsizei width
, GLsizei height
, GLenum intFormat
,
966 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
967 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
968 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
969 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
971 /* copy framebuffer image to texture */
973 /* create new tex image */
974 if (tex
->Width
== width
&& tex
->Height
== height
) {
975 /* create new tex with framebuffer data */
976 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
977 srcX
, srcY
, width
, height
, 0);
980 /* create empty texture */
981 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
982 tex
->Width
, tex
->Height
, 0,
983 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
985 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
986 0, 0, srcX
, srcY
, width
, height
);
990 /* replace existing tex image */
991 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
992 0, 0, srcX
, srcY
, width
, height
);
998 * Setup/load texture for glDrawPixels.
1001 setup_drawpix_texture(GLcontext
*ctx
,
1002 struct temp_texture
*tex
,
1004 GLenum texIntFormat
,
1005 GLsizei width
, GLsizei height
,
1006 GLenum format
, GLenum type
,
1007 const GLvoid
*pixels
)
1009 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1010 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1011 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1012 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1014 /* copy pixel data to texture */
1016 /* create new tex image */
1017 if (tex
->Width
== width
&& tex
->Height
== height
) {
1018 /* create new tex and load image data */
1019 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1020 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1023 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1025 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1026 ctx
->Unpack
.BufferObj
);
1027 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1028 /* create empty texture */
1029 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1030 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1031 if (save_unpack_obj
!= NULL
)
1032 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1033 save_unpack_obj
->Name
);
1035 _mesa_TexSubImage2D(tex
->Target
, 0,
1036 0, 0, width
, height
, format
, type
, pixels
);
1040 /* replace existing tex image */
1041 _mesa_TexSubImage2D(tex
->Target
, 0,
1042 0, 0, width
, height
, format
, type
, pixels
);
1049 * One-time init for drawing depth pixels.
1052 init_blit_depth_pixels(GLcontext
*ctx
)
1054 static const char *program
=
1056 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1059 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1060 struct temp_texture
*tex
= get_temp_texture(ctx
);
1061 const char *texTarget
;
1063 assert(blit
->DepthFP
== 0);
1065 /* replace %s with "RECT" or "2D" */
1066 assert(strlen(program
) + 4 < sizeof(program2
));
1067 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1071 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1073 _mesa_GenPrograms(1, &blit
->DepthFP
);
1074 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1075 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1076 strlen(program2
), (const GLubyte
*) program2
);
1081 * Try to do a glBlitFramebuffer using no-copy texturing.
1082 * We can do this when the src renderbuffer is actually a texture.
1083 * But if the src buffer == dst buffer we cannot do this.
1085 * \return new buffer mask indicating the buffers left to blit using the
1089 blitframebuffer_texture(GLcontext
*ctx
,
1090 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1091 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1092 GLbitfield mask
, GLenum filter
)
1094 if (mask
& GL_COLOR_BUFFER_BIT
) {
1095 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1096 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1097 const struct gl_renderbuffer_attachment
*drawAtt
=
1098 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1099 const struct gl_renderbuffer_attachment
*readAtt
=
1100 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1102 if (readAtt
&& readAtt
->Texture
) {
1103 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1104 const GLuint srcLevel
= readAtt
->TextureLevel
;
1105 const GLenum minFilterSave
= texObj
->MinFilter
;
1106 const GLenum magFilterSave
= texObj
->MagFilter
;
1107 const GLint baseLevelSave
= texObj
->BaseLevel
;
1108 const GLint maxLevelSave
= texObj
->MaxLevel
;
1109 const GLenum wrapSSave
= texObj
->WrapS
;
1110 const GLenum wrapTSave
= texObj
->WrapT
;
1111 const GLenum target
= texObj
->Target
;
1113 if (drawAtt
->Texture
== readAtt
->Texture
) {
1114 /* Can't use same texture as both the source and dest. We need
1115 * to handle overlapping blits and besides, some hw may not
1121 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1122 /* Can't handle other texture types at this time */
1127 printf("Blit from texture!\n");
1128 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1129 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1132 /* Prepare src texture state */
1133 _mesa_BindTexture(target
, texObj
->Name
);
1134 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1135 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1136 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1137 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1138 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1140 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1141 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1142 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1143 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1145 /* Prepare vertex data (the VBO was previously created and bound) */
1150 struct vertex verts
[4];
1151 GLfloat s0
, t0
, s1
, t1
;
1153 if (target
== GL_TEXTURE_2D
) {
1154 const struct gl_texture_image
*texImage
1155 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1156 s0
= srcX0
/ (float) texImage
->Width
;
1157 s1
= srcX1
/ (float) texImage
->Width
;
1158 t0
= srcY0
/ (float) texImage
->Height
;
1159 t1
= srcY1
/ (float) texImage
->Height
;
1162 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1169 verts
[0].x
= (GLfloat
) dstX0
;
1170 verts
[0].y
= (GLfloat
) dstY0
;
1171 verts
[1].x
= (GLfloat
) dstX1
;
1172 verts
[1].y
= (GLfloat
) dstY0
;
1173 verts
[2].x
= (GLfloat
) dstX1
;
1174 verts
[2].y
= (GLfloat
) dstY1
;
1175 verts
[3].x
= (GLfloat
) dstX0
;
1176 verts
[3].y
= (GLfloat
) dstY1
;
1187 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1190 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1192 /* Restore texture object state, the texture binding will
1193 * be restored by _mesa_meta_end().
1195 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1196 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1197 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1198 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1199 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1201 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1202 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1204 /* Done with color buffer */
1205 mask
&= ~GL_COLOR_BUFFER_BIT
;
1214 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1215 * of texture mapping and polygon rendering.
1218 _mesa_meta_BlitFramebuffer(GLcontext
*ctx
,
1219 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1220 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1221 GLbitfield mask
, GLenum filter
)
1223 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1224 struct temp_texture
*tex
= get_temp_texture(ctx
);
1225 const GLsizei maxTexSize
= tex
->MaxSize
;
1226 const GLint srcX
= MIN2(srcX0
, srcX1
);
1227 const GLint srcY
= MIN2(srcY0
, srcY1
);
1228 const GLint srcW
= abs(srcX1
- srcX0
);
1229 const GLint srcH
= abs(srcY1
- srcY0
);
1230 const GLboolean srcFlipX
= srcX1
< srcX0
;
1231 const GLboolean srcFlipY
= srcY1
< srcY0
;
1235 struct vertex verts
[4];
1238 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1239 /* XXX avoid this fallback */
1240 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1241 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1257 /* only scissor effects blit so save/clear all other relevant state */
1258 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1260 if (blit
->ArrayObj
== 0) {
1261 /* one-time setup */
1263 /* create vertex array object */
1264 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1265 _mesa_BindVertexArray(blit
->ArrayObj
);
1267 /* create vertex array buffer */
1268 _mesa_GenBuffersARB(1, &blit
->VBO
);
1269 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1270 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1271 NULL
, GL_DYNAMIC_DRAW_ARB
);
1273 /* setup vertex arrays */
1274 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1275 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1276 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1277 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1280 _mesa_BindVertexArray(blit
->ArrayObj
);
1281 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1284 /* Try faster, direct texture approach first */
1285 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1286 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1288 _mesa_meta_end(ctx
);
1292 /* Continue with "normal" approach which involves copying the src rect
1293 * into a temporary texture and is "blitted" by drawing a textured quad.
1296 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1298 /* vertex positions/texcoords (after texture allocation!) */
1300 verts
[0].x
= (GLfloat
) dstX0
;
1301 verts
[0].y
= (GLfloat
) dstY0
;
1302 verts
[1].x
= (GLfloat
) dstX1
;
1303 verts
[1].y
= (GLfloat
) dstY0
;
1304 verts
[2].x
= (GLfloat
) dstX1
;
1305 verts
[2].y
= (GLfloat
) dstY1
;
1306 verts
[3].x
= (GLfloat
) dstX0
;
1307 verts
[3].y
= (GLfloat
) dstY1
;
1311 verts
[1].s
= tex
->Sright
;
1313 verts
[2].s
= tex
->Sright
;
1314 verts
[2].t
= tex
->Ttop
;
1316 verts
[3].t
= tex
->Ttop
;
1318 /* upload new vertex data */
1319 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1322 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1324 if (mask
& GL_COLOR_BUFFER_BIT
) {
1325 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1327 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1328 mask
&= ~GL_COLOR_BUFFER_BIT
;
1331 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1332 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1335 init_blit_depth_pixels(ctx
);
1337 /* maybe change tex format here */
1338 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1340 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1341 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1343 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1344 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1346 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1347 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1348 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1349 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1350 _mesa_DepthFunc(GL_ALWAYS
);
1351 _mesa_DepthMask(GL_TRUE
);
1353 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1354 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1360 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1361 /* XXX can't easily do stencil */
1364 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1366 _mesa_meta_end(ctx
);
1369 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1370 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1376 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1379 _mesa_meta_Clear(GLcontext
*ctx
, GLbitfield buffers
)
1381 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1383 GLfloat x
, y
, z
, r
, g
, b
, a
;
1385 struct vertex verts
[4];
1386 /* save all state but scissor, pixel pack/unpack */
1387 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1389 if (buffers
& BUFFER_BITS_COLOR
) {
1390 /* if clearing color buffers, don't save/restore colormask */
1391 metaSave
-= META_COLOR_MASK
;
1394 _mesa_meta_begin(ctx
, metaSave
);
1396 if (clear
->ArrayObj
== 0) {
1397 /* one-time setup */
1399 /* create vertex array object */
1400 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1401 _mesa_BindVertexArray(clear
->ArrayObj
);
1403 /* create vertex array buffer */
1404 _mesa_GenBuffersARB(1, &clear
->VBO
);
1405 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1407 /* setup vertex arrays */
1408 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1409 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1410 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1411 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1414 _mesa_BindVertexArray(clear
->ArrayObj
);
1415 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1418 /* GL_COLOR_BUFFER_BIT */
1419 if (buffers
& BUFFER_BITS_COLOR
) {
1420 /* leave colormask, glDrawBuffer state as-is */
1423 ASSERT(metaSave
& META_COLOR_MASK
);
1424 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1427 /* GL_DEPTH_BUFFER_BIT */
1428 if (buffers
& BUFFER_BIT_DEPTH
) {
1429 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1430 _mesa_DepthFunc(GL_ALWAYS
);
1431 _mesa_DepthMask(GL_TRUE
);
1434 assert(!ctx
->Depth
.Test
);
1437 /* GL_STENCIL_BUFFER_BIT */
1438 if (buffers
& BUFFER_BIT_STENCIL
) {
1439 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1440 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1441 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1442 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1443 ctx
->Stencil
.Clear
& 0x7fffffff,
1444 ctx
->Stencil
.WriteMask
[0]);
1447 assert(!ctx
->Stencil
.Enabled
);
1450 /* vertex positions/colors */
1452 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1453 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1454 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1455 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1456 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1473 for (i
= 0; i
< 4; i
++) {
1474 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1475 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1476 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1477 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1480 /* upload new vertex data */
1481 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1482 GL_DYNAMIC_DRAW_ARB
);
1486 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1488 _mesa_meta_end(ctx
);
1493 * Meta implementation of ctx->Driver.CopyPixels() in terms
1494 * of texture mapping and polygon rendering.
1497 _mesa_meta_CopyPixels(GLcontext
*ctx
, GLint srcX
, GLint srcY
,
1498 GLsizei width
, GLsizei height
,
1499 GLint dstX
, GLint dstY
, GLenum type
)
1501 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1502 struct temp_texture
*tex
= get_temp_texture(ctx
);
1504 GLfloat x
, y
, z
, s
, t
;
1506 struct vertex verts
[4];
1508 GLenum intFormat
= GL_RGBA
;
1510 if (type
!= GL_COLOR
||
1511 ctx
->_ImageTransferState
||
1513 width
> tex
->MaxSize
||
1514 height
> tex
->MaxSize
) {
1515 /* XXX avoid this fallback */
1516 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1520 /* Most GL state applies to glCopyPixels, but a there's a few things
1521 * we need to override:
1523 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1530 if (copypix
->ArrayObj
== 0) {
1531 /* one-time setup */
1533 /* create vertex array object */
1534 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1535 _mesa_BindVertexArray(copypix
->ArrayObj
);
1537 /* create vertex array buffer */
1538 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1539 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1540 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1541 NULL
, GL_DYNAMIC_DRAW_ARB
);
1543 /* setup vertex arrays */
1544 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1545 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1546 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1547 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1550 _mesa_BindVertexArray(copypix
->ArrayObj
);
1551 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1554 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1556 /* vertex positions, texcoords (after texture allocation!) */
1558 const GLfloat dstX0
= (GLfloat
) dstX
;
1559 const GLfloat dstY0
= (GLfloat
) dstY
;
1560 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1561 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1562 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1572 verts
[1].s
= tex
->Sright
;
1577 verts
[2].s
= tex
->Sright
;
1578 verts
[2].t
= tex
->Ttop
;
1583 verts
[3].t
= tex
->Ttop
;
1585 /* upload new vertex data */
1586 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1589 /* Alloc/setup texture */
1590 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1591 GL_RGBA
, GL_NEAREST
);
1593 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1595 /* draw textured quad */
1596 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1598 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1600 _mesa_meta_end(ctx
);
1606 * When the glDrawPixels() image size is greater than the max rectangle
1607 * texture size we use this function to break the glDrawPixels() image
1608 * into tiles which fit into the max texture size.
1611 tiled_draw_pixels(GLcontext
*ctx
,
1613 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1614 GLenum format
, GLenum type
,
1615 const struct gl_pixelstore_attrib
*unpack
,
1616 const GLvoid
*pixels
)
1618 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1621 if (tileUnpack
.RowLength
== 0)
1622 tileUnpack
.RowLength
= width
;
1624 for (i
= 0; i
< width
; i
+= tileSize
) {
1625 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1626 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1628 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1630 for (j
= 0; j
< height
; j
+= tileSize
) {
1631 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1632 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1634 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1636 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1637 format
, type
, &tileUnpack
, pixels
);
1644 * One-time init for drawing stencil pixels.
1647 init_draw_stencil_pixels(GLcontext
*ctx
)
1649 /* This program is run eight times, once for each stencil bit.
1650 * The stencil values to draw are found in an 8-bit alpha texture.
1651 * We read the texture/stencil value and test if bit 'b' is set.
1652 * If the bit is not set, use KIL to kill the fragment.
1653 * Finally, we use the stencil test to update the stencil buffer.
1655 * The basic algorithm for checking if a bit is set is:
1656 * if (is_odd(value / (1 << bit)))
1657 * result is one (or non-zero).
1660 * The program parameter contains three values:
1661 * parm.x = 255 / (1 << bit)
1665 static const char *program
=
1667 "PARAM parm = program.local[0]; \n"
1669 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1670 "# t = t * 255 / bit \n"
1671 "MUL t.x, t.a, parm.x; \n"
1674 "SUB t.x, t.x, t.y; \n"
1676 "MUL t.x, t.x, parm.y; \n"
1677 "# t = fract(t.x) \n"
1678 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1679 "# t.x = (t.x == 0 ? 1 : 0) \n"
1680 "SGE t.x, -t.x, parm.z; \n"
1682 "# for debug only \n"
1683 "#MOV result.color, t.x; \n"
1685 char program2
[1000];
1686 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1687 struct temp_texture
*tex
= get_temp_texture(ctx
);
1688 const char *texTarget
;
1690 assert(drawpix
->StencilFP
== 0);
1692 /* replace %s with "RECT" or "2D" */
1693 assert(strlen(program
) + 4 < sizeof(program2
));
1694 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1698 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1700 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1701 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1702 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1703 strlen(program2
), (const GLubyte
*) program2
);
1708 * One-time init for drawing depth pixels.
1711 init_draw_depth_pixels(GLcontext
*ctx
)
1713 static const char *program
=
1715 "PARAM color = program.local[0]; \n"
1716 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1717 "MOV result.color, color; \n"
1720 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1721 struct temp_texture
*tex
= get_temp_texture(ctx
);
1722 const char *texTarget
;
1724 assert(drawpix
->DepthFP
== 0);
1726 /* replace %s with "RECT" or "2D" */
1727 assert(strlen(program
) + 4 < sizeof(program2
));
1728 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1732 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1734 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1735 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1736 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1737 strlen(program2
), (const GLubyte
*) program2
);
1742 * Meta implementation of ctx->Driver.DrawPixels() in terms
1743 * of texture mapping and polygon rendering.
1746 _mesa_meta_DrawPixels(GLcontext
*ctx
,
1747 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1748 GLenum format
, GLenum type
,
1749 const struct gl_pixelstore_attrib
*unpack
,
1750 const GLvoid
*pixels
)
1752 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1753 struct temp_texture
*tex
= get_temp_texture(ctx
);
1754 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1755 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1757 GLfloat x
, y
, z
, s
, t
;
1759 struct vertex verts
[4];
1760 GLenum texIntFormat
;
1761 GLboolean fallback
, newTex
;
1762 GLbitfield metaExtraSave
= 0x0;
1766 * Determine if we can do the glDrawPixels with texture mapping.
1768 fallback
= GL_FALSE
;
1769 if (ctx
->_ImageTransferState
||
1774 if (_mesa_is_color_format(format
)) {
1775 /* use more compact format when possible */
1776 /* XXX disable special case for GL_LUMINANCE for now to work around
1777 * apparent i965 driver bug (see bug #23670).
1779 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1780 texIntFormat
= format
;
1782 texIntFormat
= GL_RGBA
;
1784 else if (_mesa_is_stencil_format(format
)) {
1785 if (ctx
->Extensions
.ARB_fragment_program
&&
1786 ctx
->Pixel
.IndexShift
== 0 &&
1787 ctx
->Pixel
.IndexOffset
== 0 &&
1788 type
== GL_UNSIGNED_BYTE
) {
1789 /* We'll store stencil as alpha. This only works for GLubyte
1790 * image data because of how incoming values are mapped to alpha
1793 texIntFormat
= GL_ALPHA
;
1794 metaExtraSave
= (META_COLOR_MASK
|
1803 else if (_mesa_is_depth_format(format
)) {
1804 if (ctx
->Extensions
.ARB_depth_texture
&&
1805 ctx
->Extensions
.ARB_fragment_program
) {
1806 texIntFormat
= GL_DEPTH_COMPONENT
;
1807 metaExtraSave
= (META_SHADER
);
1818 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1819 format
, type
, unpack
, pixels
);
1824 * Check image size against max texture size, draw as tiles if needed.
1826 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1827 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1828 format
, type
, unpack
, pixels
);
1832 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1833 * but a there's a few things we need to override:
1835 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1843 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1845 /* vertex positions, texcoords (after texture allocation!) */
1847 const GLfloat x0
= (GLfloat
) x
;
1848 const GLfloat y0
= (GLfloat
) y
;
1849 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1850 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1851 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1861 verts
[1].s
= tex
->Sright
;
1866 verts
[2].s
= tex
->Sright
;
1867 verts
[2].t
= tex
->Ttop
;
1872 verts
[3].t
= tex
->Ttop
;
1875 if (drawpix
->ArrayObj
== 0) {
1876 /* one-time setup: create vertex array object */
1877 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1879 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1881 /* create vertex array buffer */
1882 _mesa_GenBuffersARB(1, &vbo
);
1883 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1884 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1885 verts
, GL_DYNAMIC_DRAW_ARB
);
1887 /* setup vertex arrays */
1888 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1889 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1890 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1891 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1893 /* set given unpack params */
1894 ctx
->Unpack
= *unpack
;
1896 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1898 if (_mesa_is_stencil_format(format
)) {
1899 /* Drawing stencil */
1902 if (!drawpix
->StencilFP
)
1903 init_draw_stencil_pixels(ctx
);
1905 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1906 GL_ALPHA
, type
, pixels
);
1908 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1910 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1912 /* set all stencil bits to 0 */
1913 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1914 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1915 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1917 /* set stencil bits to 1 where needed */
1918 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1920 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1921 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1923 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1924 const GLuint mask
= 1 << bit
;
1925 if (mask
& origStencilMask
) {
1926 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1927 _mesa_StencilMask(mask
);
1929 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1930 255.0 / mask
, 0.5, 0.0, 0.0);
1932 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1936 else if (_mesa_is_depth_format(format
)) {
1938 if (!drawpix
->DepthFP
)
1939 init_draw_depth_pixels(ctx
);
1941 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1942 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1944 /* polygon color = current raster color */
1945 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1946 ctx
->Current
.RasterColor
);
1948 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1949 format
, type
, pixels
);
1951 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1955 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1956 format
, type
, pixels
);
1957 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1960 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1962 _mesa_DeleteBuffersARB(1, &vbo
);
1964 /* restore unpack params */
1965 ctx
->Unpack
= unpackSave
;
1967 _mesa_meta_end(ctx
);
1972 * Do glBitmap with a alpha texture quad. Use the alpha test to
1973 * cull the 'off' bits. If alpha test is already enabled, fall back
1974 * to swrast (should be a rare case).
1975 * A bitmap cache as in the gallium/mesa state tracker would
1976 * improve performance a lot.
1979 _mesa_meta_Bitmap(GLcontext
*ctx
,
1980 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1981 const struct gl_pixelstore_attrib
*unpack
,
1982 const GLubyte
*bitmap1
)
1984 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1985 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1986 const GLenum texIntFormat
= GL_ALPHA
;
1987 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
1989 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
1991 struct vertex verts
[4];
1996 * Check if swrast fallback is needed.
1998 if (ctx
->_ImageTransferState
||
1999 ctx
->Color
.AlphaEnabled
||
2001 ctx
->Texture
._EnabledUnits
||
2002 width
> tex
->MaxSize
||
2003 height
> tex
->MaxSize
) {
2004 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2008 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2009 * but a there's a few things we need to override:
2011 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2013 META_RASTERIZATION
|
2020 if (bitmap
->ArrayObj
== 0) {
2021 /* one-time setup */
2023 /* create vertex array object */
2024 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2025 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2027 /* create vertex array buffer */
2028 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2029 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2030 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2031 NULL
, GL_DYNAMIC_DRAW_ARB
);
2033 /* setup vertex arrays */
2034 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2035 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2036 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2037 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2038 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2039 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2042 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2043 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2046 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2048 /* vertex positions, texcoords, colors (after texture allocation!) */
2050 const GLfloat x0
= (GLfloat
) x
;
2051 const GLfloat y0
= (GLfloat
) y
;
2052 const GLfloat x1
= (GLfloat
) (x
+ width
);
2053 const GLfloat y1
= (GLfloat
) (y
+ height
);
2054 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2065 verts
[1].s
= tex
->Sright
;
2070 verts
[2].s
= tex
->Sright
;
2071 verts
[2].t
= tex
->Ttop
;
2076 verts
[3].t
= tex
->Ttop
;
2078 for (i
= 0; i
< 4; i
++) {
2079 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2080 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2081 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2082 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2085 /* upload new vertex data */
2086 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2089 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2091 _mesa_meta_end(ctx
);
2095 bitmap8
= (GLubyte
*) calloc(1, width
* height
);
2097 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2098 bitmap8
, width
, 0xff);
2100 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2102 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2103 _mesa_AlphaFunc(GL_GREATER
, 0.0);
2105 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2106 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2108 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2110 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2115 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2117 _mesa_meta_end(ctx
);
2122 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2123 * software fallback. The fallback path will require that the texture
2124 * images are mapped.
2125 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2128 _mesa_meta_check_generate_mipmap_fallback(GLcontext
*ctx
, GLenum target
,
2129 struct gl_texture_object
*texObj
)
2131 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2132 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2133 struct gl_texture_image
*baseImage
;
2137 /* check for fallbacks */
2138 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2139 target
== GL_TEXTURE_3D
) {
2143 srcLevel
= texObj
->BaseLevel
;
2144 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2145 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2150 * Test that we can actually render in the texture's format.
2153 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2154 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2156 if (target
== GL_TEXTURE_1D
) {
2157 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2158 GL_COLOR_ATTACHMENT0_EXT
,
2159 target
, texObj
->Name
, srcLevel
);
2162 /* other work is needed to enable 3D mipmap generation */
2163 else if (target
== GL_TEXTURE_3D
) {
2165 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2166 GL_COLOR_ATTACHMENT0_EXT
,
2167 target
, texObj
->Name
, srcLevel
, zoffset
);
2172 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2173 GL_COLOR_ATTACHMENT0_EXT
,
2174 target
, texObj
->Name
, srcLevel
);
2177 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2179 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2181 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2190 * Called via ctx->Driver.GenerateMipmap()
2191 * Note: texture borders and 3D texture support not yet complete.
2194 _mesa_meta_GenerateMipmap(GLcontext
*ctx
, GLenum target
,
2195 struct gl_texture_object
*texObj
)
2197 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2199 GLfloat x
, y
, s
, t
, r
;
2201 struct vertex verts
[4];
2202 const GLuint baseLevel
= texObj
->BaseLevel
;
2203 const GLuint maxLevel
= texObj
->MaxLevel
;
2204 const GLenum minFilterSave
= texObj
->MinFilter
;
2205 const GLenum magFilterSave
= texObj
->MagFilter
;
2206 const GLint baseLevelSave
= texObj
->BaseLevel
;
2207 const GLint maxLevelSave
= texObj
->MaxLevel
;
2208 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2209 const GLenum wrapSSave
= texObj
->WrapS
;
2210 const GLenum wrapTSave
= texObj
->WrapT
;
2211 const GLenum wrapRSave
= texObj
->WrapR
;
2212 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2213 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2218 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2219 _mesa_generate_mipmap(ctx
, target
, texObj
);
2223 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2224 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2225 faceTarget
= target
;
2226 target
= GL_TEXTURE_CUBE_MAP
;
2229 faceTarget
= target
;
2232 _mesa_meta_begin(ctx
, META_ALL
);
2234 if (original_active_unit
!= 0)
2235 _mesa_BindTexture(target
, texObj
->Name
);
2237 if (mipmap
->ArrayObj
== 0) {
2238 /* one-time setup */
2240 /* create vertex array object */
2241 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2242 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2244 /* create vertex array buffer */
2245 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2246 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2247 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2248 NULL
, GL_DYNAMIC_DRAW_ARB
);
2250 /* setup vertex arrays */
2251 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2252 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2253 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2254 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2257 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2258 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2262 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2264 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2266 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
2267 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2268 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2269 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2270 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2271 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2273 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2275 /* setup texcoords once (XXX what about border?) */
2276 switch (faceTarget
) {
2298 static const GLfloat st
[4][2] = {
2299 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2303 /* loop over quad verts */
2304 for (i
= 0; i
< 4; i
++) {
2305 /* Compute sc = +/-scale and tc = +/-scale.
2306 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2307 * though that can still sometimes happen with this scale factor...
2309 const GLfloat scale
= 0.9999f
;
2310 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2311 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2313 switch (faceTarget
) {
2314 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2319 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2324 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2329 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2334 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2339 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2351 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2353 /* setup vertex positions */
2364 /* upload new vertex data */
2365 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2368 /* setup projection matrix */
2369 _mesa_MatrixMode(GL_PROJECTION
);
2370 _mesa_LoadIdentity();
2371 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2373 /* texture is already locked, unlock now */
2374 _mesa_unlock_texture(ctx
, texObj
);
2376 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2377 const struct gl_texture_image
*srcImage
;
2378 const GLuint srcLevel
= dstLevel
- 1;
2379 GLsizei srcWidth
, srcHeight
, srcDepth
;
2380 GLsizei dstWidth
, dstHeight
, dstDepth
;
2383 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2384 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2386 /* src size w/out border */
2387 srcWidth
= srcImage
->Width
- 2 * border
;
2388 srcHeight
= srcImage
->Height
- 2 * border
;
2389 srcDepth
= srcImage
->Depth
- 2 * border
;
2391 /* new dst size w/ border */
2392 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2393 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2394 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2396 if (dstWidth
== srcImage
->Width
&&
2397 dstHeight
== srcImage
->Height
&&
2398 dstDepth
== srcImage
->Depth
) {
2403 /* Set MaxLevel large enough to hold the new level when we allocate it */
2404 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2406 /* Create empty dest image */
2407 if (target
== GL_TEXTURE_1D
) {
2408 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2410 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2412 else if (target
== GL_TEXTURE_3D
) {
2413 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2414 dstWidth
, dstHeight
, dstDepth
, border
,
2415 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2419 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2420 dstWidth
, dstHeight
, border
,
2421 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2423 if (target
== GL_TEXTURE_CUBE_MAP
) {
2424 /* If texturing from a cube, we need to make sure all src faces
2425 * have been defined (even if we're not sampling from them.)
2426 * Otherwise the texture object will be 'incomplete' and
2427 * texturing from it will not be allowed.
2430 for (face
= 0; face
< 6; face
++) {
2431 if (!texObj
->Image
[face
][srcLevel
] ||
2432 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2433 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2434 srcLevel
, srcImage
->InternalFormat
,
2435 srcWidth
, srcHeight
, border
,
2436 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2442 /* limit sampling to src level */
2443 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2444 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2446 /* Set to draw into the current dstLevel */
2447 if (target
== GL_TEXTURE_1D
) {
2448 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2449 GL_COLOR_ATTACHMENT0_EXT
,
2454 else if (target
== GL_TEXTURE_3D
) {
2455 GLint zoffset
= 0; /* XXX unfinished */
2456 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2457 GL_COLOR_ATTACHMENT0_EXT
,
2464 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2465 GL_COLOR_ATTACHMENT0_EXT
,
2471 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2474 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2475 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2480 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2481 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2483 /* setup viewport */
2484 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2486 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2489 _mesa_lock_texture(ctx
, texObj
); /* relock */
2491 _mesa_meta_end(ctx
);
2493 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2494 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2495 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
2496 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2497 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2498 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2499 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2500 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2502 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2507 * Determine the GL data type to use for the temporary image read with
2508 * ReadPixels() and passed to Tex[Sub]Image().
2511 get_temp_image_type(GLcontext
*ctx
, GLenum baseFormat
)
2513 switch (baseFormat
) {
2518 case GL_LUMINANCE_ALPHA
:
2520 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2521 return GL_UNSIGNED_BYTE
;
2522 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2523 return GL_UNSIGNED_SHORT
;
2526 case GL_DEPTH_COMPONENT
:
2527 return GL_UNSIGNED_INT
;
2528 case GL_DEPTH_STENCIL
:
2529 return GL_UNSIGNED_INT_24_8
;
2531 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2538 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2539 * Have to be careful with locking and meta state for pixel transfer.
2542 copy_tex_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2543 GLenum internalFormat
, GLint x
, GLint y
,
2544 GLsizei width
, GLsizei height
, GLint border
)
2546 struct gl_texture_object
*texObj
;
2547 struct gl_texture_image
*texImage
;
2548 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2549 GLenum format
, type
;
2553 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2554 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2556 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2557 type
= get_temp_image_type(ctx
, format
);
2558 bpp
= _mesa_bytes_per_pixel(format
, type
);
2560 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2565 * Alloc image buffer (XXX could use a PBO)
2567 buf
= malloc(width
* height
* bpp
);
2569 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2573 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2576 * Read image from framebuffer (disable pixel transfer ops)
2578 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2579 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2580 format
, type
, &ctx
->Pack
, buf
);
2581 _mesa_meta_end(ctx
);
2584 * Prepare for new texture image size/data
2586 #if FEATURE_convolve
2587 if (_mesa_is_color_format(internalFormat
)) {
2588 _mesa_adjust_image_for_convolution(ctx
, 2,
2589 &postConvWidth
, &postConvHeight
);
2593 if (texImage
->Data
) {
2594 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2597 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2598 postConvWidth
, postConvHeight
, 1,
2599 border
, internalFormat
);
2601 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2602 internalFormat
, GL_NONE
, GL_NONE
);
2605 * Store texture data (with pixel transfer ops)
2607 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2609 _mesa_update_state(ctx
); /* to update pixel transfer state */
2611 if (target
== GL_TEXTURE_1D
) {
2612 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2613 width
, border
, format
, type
,
2614 buf
, &ctx
->Unpack
, texObj
, texImage
);
2617 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2618 width
, height
, border
, format
, type
,
2619 buf
, &ctx
->Unpack
, texObj
, texImage
);
2621 _mesa_meta_end(ctx
);
2623 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2630 _mesa_meta_CopyTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2631 GLenum internalFormat
, GLint x
, GLint y
,
2632 GLsizei width
, GLint border
)
2634 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2640 _mesa_meta_CopyTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2641 GLenum internalFormat
, GLint x
, GLint y
,
2642 GLsizei width
, GLsizei height
, GLint border
)
2644 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2645 width
, height
, border
);
2651 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2652 * Have to be careful with locking and meta state for pixel transfer.
2655 copy_tex_sub_image(GLcontext
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2656 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2658 GLsizei width
, GLsizei height
)
2660 struct gl_texture_object
*texObj
;
2661 struct gl_texture_image
*texImage
;
2662 GLenum format
, type
;
2666 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2667 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2669 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2670 type
= get_temp_image_type(ctx
, format
);
2671 bpp
= _mesa_bytes_per_pixel(format
, type
);
2673 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2678 * Alloc image buffer (XXX could use a PBO)
2680 buf
= malloc(width
* height
* bpp
);
2682 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2686 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2689 * Read image from framebuffer (disable pixel transfer ops)
2691 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2692 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2693 format
, type
, &ctx
->Pack
, buf
);
2694 _mesa_meta_end(ctx
);
2696 _mesa_update_state(ctx
); /* to update pixel transfer state */
2699 * Store texture data (with pixel transfer ops)
2701 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2702 if (target
== GL_TEXTURE_1D
) {
2703 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2704 width
, format
, type
, buf
,
2705 &ctx
->Unpack
, texObj
, texImage
);
2707 else if (target
== GL_TEXTURE_3D
) {
2708 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2709 width
, height
, 1, format
, type
, buf
,
2710 &ctx
->Unpack
, texObj
, texImage
);
2713 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2714 width
, height
, format
, type
, buf
,
2715 &ctx
->Unpack
, texObj
, texImage
);
2717 _mesa_meta_end(ctx
);
2719 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2726 _mesa_meta_CopyTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
2728 GLint x
, GLint y
, GLsizei width
)
2730 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2736 _mesa_meta_CopyTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
2737 GLint xoffset
, GLint yoffset
,
2739 GLsizei width
, GLsizei height
)
2741 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2742 x
, y
, width
, height
);
2747 _mesa_meta_CopyTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
2748 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2750 GLsizei width
, GLsizei height
)
2752 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2753 x
, y
, width
, height
);
2758 _mesa_meta_CopyColorTable(GLcontext
*ctx
,
2759 GLenum target
, GLenum internalformat
,
2760 GLint x
, GLint y
, GLsizei width
)
2764 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2766 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
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_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2779 _mesa_meta_end(ctx
);
2786 _mesa_meta_CopyColorSubTable(GLcontext
*ctx
,GLenum target
, GLsizei start
,
2787 GLint x
, GLint y
, GLsizei width
)
2791 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2793 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2798 * Read image from framebuffer (disable pixel transfer ops)
2800 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2801 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2802 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2804 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2806 _mesa_meta_end(ctx
);
2813 _mesa_meta_CopyConvolutionFilter1D(GLcontext
*ctx
, GLenum target
,
2814 GLenum internalFormat
,
2815 GLint x
, GLint y
, GLsizei width
)
2819 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2821 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyConvolutionFilter2D");
2826 * Read image from framebuffer (disable pixel transfer ops)
2828 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2829 _mesa_update_state(ctx
);
2830 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2831 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2833 _mesa_ConvolutionFilter1D(target
, internalFormat
, width
,
2834 GL_RGBA
, GL_FLOAT
, buf
);
2836 _mesa_meta_end(ctx
);
2843 _mesa_meta_CopyConvolutionFilter2D(GLcontext
*ctx
, GLenum target
,
2844 GLenum internalFormat
, GLint x
, GLint y
,
2845 GLsizei width
, GLsizei height
)
2849 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
2851 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyConvolutionFilter2D");
2856 * Read image from framebuffer (disable pixel transfer ops)
2858 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2859 _mesa_update_state(ctx
);
2861 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2862 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2864 _mesa_ConvolutionFilter2D(target
, internalFormat
, width
, height
,
2865 GL_RGBA
, GL_FLOAT
, buf
);
2867 _mesa_meta_end(ctx
);