2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arbprogram.h"
38 #include "main/arrayobj.h"
39 #include "main/blend.h"
40 #include "main/bufferobj.h"
41 #include "main/buffers.h"
42 #include "main/colortab.h"
43 #include "main/depth.h"
44 #include "main/enable.h"
45 #include "main/fbobject.h"
46 #include "main/formats.h"
47 #include "main/image.h"
48 #include "main/macros.h"
49 #include "main/matrix.h"
50 #include "main/mipmap.h"
51 #include "main/polygon.h"
52 #include "main/readpix.h"
53 #include "main/scissor.h"
54 #include "main/shaderapi.h"
55 #include "main/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 "program/program.h"
65 #include "swrast/swrast.h"
66 #include "drivers/common/meta.h"
69 /** Return offset in bytes of the field within a vertex struct */
70 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
74 * Flags passed to _mesa_meta_begin().
78 #define META_ALPHA_TEST 0x1
79 #define META_BLEND 0x2 /**< includes logicop */
80 #define META_COLOR_MASK 0x4
81 #define META_DEPTH_TEST 0x8
83 #define META_PIXEL_STORE 0x20
84 #define META_PIXEL_TRANSFER 0x40
85 #define META_RASTERIZATION 0x80
86 #define META_SCISSOR 0x100
87 #define META_SHADER 0x200
88 #define META_STENCIL_TEST 0x400
89 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
90 #define META_TEXTURE 0x1000
91 #define META_VERTEX 0x2000
92 #define META_VIEWPORT 0x4000
97 * State which we may save/restore across meta ops.
98 * XXX this may be incomplete...
102 GLbitfield SavedState
; /**< bitmask of META_* flags */
104 /** META_ALPHA_TEST */
105 GLboolean AlphaEnabled
;
108 GLbitfield BlendEnabled
;
109 GLboolean ColorLogicOpEnabled
;
111 /** META_COLOR_MASK */
112 GLubyte ColorMask
[MAX_DRAW_BUFFERS
][4];
114 /** META_DEPTH_TEST */
115 struct gl_depthbuffer_attrib Depth
;
120 /** META_PIXEL_STORE */
121 struct gl_pixelstore_attrib Pack
, Unpack
;
123 /** META_PIXEL_TRANSFER */
124 GLfloat RedBias
, RedScale
;
125 GLfloat GreenBias
, GreenScale
;
126 GLfloat BlueBias
, BlueScale
;
127 GLfloat AlphaBias
, AlphaScale
;
128 GLfloat DepthBias
, DepthScale
;
129 GLboolean MapColorFlag
;
131 /** META_RASTERIZATION */
132 GLenum FrontPolygonMode
, BackPolygonMode
;
133 GLboolean PolygonOffset
;
134 GLboolean PolygonSmooth
;
135 GLboolean PolygonStipple
;
136 GLboolean PolygonCull
;
139 struct gl_scissor_attrib Scissor
;
142 GLboolean VertexProgramEnabled
;
143 struct gl_vertex_program
*VertexProgram
;
144 GLboolean FragmentProgramEnabled
;
145 struct gl_fragment_program
*FragmentProgram
;
147 GLuint GeometryShader
;
148 GLuint FragmentShader
;
151 /** META_STENCIL_TEST */
152 struct gl_stencil_attrib Stencil
;
154 /** META_TRANSFORM */
156 GLfloat ModelviewMatrix
[16];
157 GLfloat ProjectionMatrix
[16];
158 GLfloat TextureMatrix
[16];
159 GLbitfield ClipPlanesEnabled
;
163 GLuint ClientActiveUnit
;
164 /** for unit[0] only */
165 struct gl_texture_object
*CurrentTexture
[NUM_TEXTURE_TARGETS
];
166 /** mask of TEXTURE_2D_BIT, etc */
167 GLbitfield TexEnabled
[MAX_TEXTURE_UNITS
];
168 GLbitfield TexGenEnabled
[MAX_TEXTURE_UNITS
];
169 GLuint EnvMode
; /* unit[0] only */
172 struct gl_array_object
*ArrayObj
;
173 struct gl_buffer_object
*ArrayBufferObj
;
176 GLint ViewportX
, ViewportY
, ViewportW
, ViewportH
;
177 GLclampd DepthNear
, DepthFar
;
179 /** Miscellaneous (always disabled) */
185 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
186 * This is currently shared by all the meta ops. But we could create a
187 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
192 GLenum Target
; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
193 GLsizei MinSize
; /**< Min texture size to allocate */
194 GLsizei MaxSize
; /**< Max possible texture size */
195 GLboolean NPOT
; /**< Non-power of two size OK? */
196 GLsizei Width
, Height
; /**< Current texture size */
198 GLfloat Sright
, Ttop
; /**< right, top texcoords */
203 * State for glBlitFramebufer()
214 * State for glClear()
224 * State for glCopyPixels()
234 * State for glDrawPixels()
240 GLuint StencilFP
; /**< Fragment program for drawing stencil images */
241 GLuint DepthFP
; /**< Fragment program for drawing depth images */
246 * State for glBitmap()
252 struct temp_texture Tex
; /**< separate texture from other meta ops */
257 * State for _mesa_meta_generate_mipmap()
259 struct gen_mipmap_state
268 * All per-context meta state.
272 struct save_state Save
; /**< state saved during meta-ops */
274 struct temp_texture TempTex
;
276 struct blit_state Blit
; /**< For _mesa_meta_BlitFramebuffer() */
277 struct clear_state Clear
; /**< For _mesa_meta_Clear() */
278 struct copypix_state CopyPix
; /**< For _mesa_meta_CopyPixels() */
279 struct drawpix_state DrawPix
; /**< For _mesa_meta_DrawPixels() */
280 struct bitmap_state Bitmap
; /**< For _mesa_meta_Bitmap() */
281 struct gen_mipmap_state Mipmap
; /**< For _mesa_meta_GenerateMipmap() */
286 * Initialize meta-ops for a context.
287 * To be called once during context creation.
290 _mesa_meta_init(struct gl_context
*ctx
)
294 ctx
->Meta
= CALLOC_STRUCT(gl_meta_state
);
299 * Free context meta-op state.
300 * To be called once during context destruction.
303 _mesa_meta_free(struct gl_context
*ctx
)
305 /* Note: Any textures, VBOs, etc, that we allocate should get
306 * freed by the normal context destruction code. But this would be
307 * the place to free other meta data someday.
315 * Enter meta state. This is like a light-weight version of glPushAttrib
316 * but it also resets most GL state back to default values.
318 * \param state bitmask of META_* flags indicating which attribute groups
319 * to save and reset to their defaults
322 _mesa_meta_begin(struct gl_context
*ctx
, GLbitfield state
)
324 struct save_state
*save
= &ctx
->Meta
->Save
;
326 save
->SavedState
= state
;
328 if (state
& META_ALPHA_TEST
) {
329 save
->AlphaEnabled
= ctx
->Color
.AlphaEnabled
;
330 if (ctx
->Color
.AlphaEnabled
)
331 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_FALSE
);
334 if (state
& META_BLEND
) {
335 save
->BlendEnabled
= ctx
->Color
.BlendEnabled
;
336 if (ctx
->Color
.BlendEnabled
) {
337 if (ctx
->Extensions
.EXT_draw_buffers2
) {
339 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
340 _mesa_set_enablei(ctx
, GL_BLEND
, i
, GL_FALSE
);
344 _mesa_set_enable(ctx
, GL_BLEND
, GL_FALSE
);
347 save
->ColorLogicOpEnabled
= ctx
->Color
.ColorLogicOpEnabled
;
348 if (ctx
->Color
.ColorLogicOpEnabled
)
349 _mesa_set_enable(ctx
, GL_COLOR_LOGIC_OP
, GL_FALSE
);
352 if (state
& META_COLOR_MASK
) {
353 memcpy(save
->ColorMask
, ctx
->Color
.ColorMask
,
354 sizeof(ctx
->Color
.ColorMask
));
355 if (!ctx
->Color
.ColorMask
[0][0] ||
356 !ctx
->Color
.ColorMask
[0][1] ||
357 !ctx
->Color
.ColorMask
[0][2] ||
358 !ctx
->Color
.ColorMask
[0][3])
359 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
362 if (state
& META_DEPTH_TEST
) {
363 save
->Depth
= ctx
->Depth
; /* struct copy */
365 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
368 if (state
& META_FOG
) {
369 save
->Fog
= ctx
->Fog
.Enabled
;
370 if (ctx
->Fog
.Enabled
)
371 _mesa_set_enable(ctx
, GL_FOG
, GL_FALSE
);
374 if (state
& META_PIXEL_STORE
) {
375 save
->Pack
= ctx
->Pack
;
376 save
->Unpack
= ctx
->Unpack
;
377 ctx
->Pack
= ctx
->DefaultPacking
;
378 ctx
->Unpack
= ctx
->DefaultPacking
;
381 if (state
& META_PIXEL_TRANSFER
) {
382 save
->RedScale
= ctx
->Pixel
.RedScale
;
383 save
->RedBias
= ctx
->Pixel
.RedBias
;
384 save
->GreenScale
= ctx
->Pixel
.GreenScale
;
385 save
->GreenBias
= ctx
->Pixel
.GreenBias
;
386 save
->BlueScale
= ctx
->Pixel
.BlueScale
;
387 save
->BlueBias
= ctx
->Pixel
.BlueBias
;
388 save
->AlphaScale
= ctx
->Pixel
.AlphaScale
;
389 save
->AlphaBias
= ctx
->Pixel
.AlphaBias
;
390 save
->MapColorFlag
= ctx
->Pixel
.MapColorFlag
;
391 ctx
->Pixel
.RedScale
= 1.0F
;
392 ctx
->Pixel
.RedBias
= 0.0F
;
393 ctx
->Pixel
.GreenScale
= 1.0F
;
394 ctx
->Pixel
.GreenBias
= 0.0F
;
395 ctx
->Pixel
.BlueScale
= 1.0F
;
396 ctx
->Pixel
.BlueBias
= 0.0F
;
397 ctx
->Pixel
.AlphaScale
= 1.0F
;
398 ctx
->Pixel
.AlphaBias
= 0.0F
;
399 ctx
->Pixel
.MapColorFlag
= GL_FALSE
;
401 ctx
->NewState
|=_NEW_PIXEL
;
404 if (state
& META_RASTERIZATION
) {
405 save
->FrontPolygonMode
= ctx
->Polygon
.FrontMode
;
406 save
->BackPolygonMode
= ctx
->Polygon
.BackMode
;
407 save
->PolygonOffset
= ctx
->Polygon
.OffsetFill
;
408 save
->PolygonSmooth
= ctx
->Polygon
.SmoothFlag
;
409 save
->PolygonStipple
= ctx
->Polygon
.StippleFlag
;
410 save
->PolygonCull
= ctx
->Polygon
.CullFlag
;
411 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
412 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, GL_FALSE
);
413 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, GL_FALSE
);
414 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, GL_FALSE
);
415 _mesa_set_enable(ctx
, GL_CULL_FACE
, GL_FALSE
);
418 if (state
& META_SCISSOR
) {
419 save
->Scissor
= ctx
->Scissor
; /* struct copy */
420 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_FALSE
);
423 if (state
& META_SHADER
) {
424 if (ctx
->Extensions
.ARB_vertex_program
) {
425 save
->VertexProgramEnabled
= ctx
->VertexProgram
.Enabled
;
426 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
,
427 ctx
->VertexProgram
.Current
);
428 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
, GL_FALSE
);
431 if (ctx
->Extensions
.ARB_fragment_program
) {
432 save
->FragmentProgramEnabled
= ctx
->FragmentProgram
.Enabled
;
433 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
,
434 ctx
->FragmentProgram
.Current
);
435 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_FALSE
);
438 if (ctx
->Extensions
.ARB_shader_objects
) {
439 save
->VertexShader
= ctx
->Shader
.CurrentVertexProgram
?
440 ctx
->Shader
.CurrentVertexProgram
->Name
: 0;
441 save
->GeometryShader
= ctx
->Shader
.CurrentGeometryProgram
?
442 ctx
->Shader
.CurrentGeometryProgram
->Name
: 0;
443 save
->FragmentShader
= ctx
->Shader
.CurrentFragmentProgram
?
444 ctx
->Shader
.CurrentFragmentProgram
->Name
: 0;
445 save
->ActiveShader
= ctx
->Shader
.ActiveProgram
?
446 ctx
->Shader
.ActiveProgram
->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(struct gl_context
*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
;
642 ctx
->NewState
|=_NEW_PIXEL
;
645 if (state
& META_RASTERIZATION
) {
646 _mesa_PolygonMode(GL_FRONT
, save
->FrontPolygonMode
);
647 _mesa_PolygonMode(GL_BACK
, save
->BackPolygonMode
);
648 _mesa_set_enable(ctx
, GL_POLYGON_STIPPLE
, save
->PolygonStipple
);
649 _mesa_set_enable(ctx
, GL_POLYGON_OFFSET_FILL
, save
->PolygonOffset
);
650 _mesa_set_enable(ctx
, GL_POLYGON_SMOOTH
, save
->PolygonSmooth
);
651 _mesa_set_enable(ctx
, GL_CULL_FACE
, save
->PolygonCull
);
654 if (state
& META_SCISSOR
) {
655 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, save
->Scissor
.Enabled
);
656 _mesa_Scissor(save
->Scissor
.X
, save
->Scissor
.Y
,
657 save
->Scissor
.Width
, save
->Scissor
.Height
);
660 if (state
& META_SHADER
) {
661 if (ctx
->Extensions
.ARB_vertex_program
) {
662 _mesa_set_enable(ctx
, GL_VERTEX_PROGRAM_ARB
,
663 save
->VertexProgramEnabled
);
664 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
665 save
->VertexProgram
);
666 _mesa_reference_vertprog(ctx
, &save
->VertexProgram
, NULL
);
669 if (ctx
->Extensions
.ARB_fragment_program
) {
670 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
671 save
->FragmentProgramEnabled
);
672 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
673 save
->FragmentProgram
);
674 _mesa_reference_fragprog(ctx
, &save
->FragmentProgram
, NULL
);
677 if (ctx
->Extensions
.ARB_vertex_shader
)
678 _mesa_UseShaderProgramEXT(GL_VERTEX_SHADER
, save
->VertexShader
);
680 if (ctx
->Extensions
.ARB_geometry_shader4
)
681 _mesa_UseShaderProgramEXT(GL_GEOMETRY_SHADER_ARB
,
682 save
->GeometryShader
);
684 if (ctx
->Extensions
.ARB_fragment_shader
)
685 _mesa_UseShaderProgramEXT(GL_FRAGMENT_SHADER
, save
->FragmentShader
);
687 _mesa_ActiveProgramEXT(save
->ActiveShader
);
690 if (state
& META_STENCIL_TEST
) {
691 const struct gl_stencil_attrib
*stencil
= &save
->Stencil
;
693 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, stencil
->Enabled
);
694 _mesa_ClearStencil(stencil
->Clear
);
695 if (ctx
->Extensions
.EXT_stencil_two_side
) {
696 _mesa_set_enable(ctx
, GL_STENCIL_TEST_TWO_SIDE_EXT
,
697 stencil
->TestTwoSide
);
698 _mesa_ActiveStencilFaceEXT(stencil
->ActiveFace
699 ? GL_BACK
: GL_FRONT
);
702 _mesa_StencilFuncSeparate(GL_FRONT
,
703 stencil
->Function
[0],
705 stencil
->ValueMask
[0]);
706 _mesa_StencilMaskSeparate(GL_FRONT
, stencil
->WriteMask
[0]);
707 _mesa_StencilOpSeparate(GL_FRONT
, stencil
->FailFunc
[0],
708 stencil
->ZFailFunc
[0],
709 stencil
->ZPassFunc
[0]);
711 _mesa_StencilFuncSeparate(GL_BACK
,
712 stencil
->Function
[1],
714 stencil
->ValueMask
[1]);
715 _mesa_StencilMaskSeparate(GL_BACK
, stencil
->WriteMask
[1]);
716 _mesa_StencilOpSeparate(GL_BACK
, stencil
->FailFunc
[1],
717 stencil
->ZFailFunc
[1],
718 stencil
->ZPassFunc
[1]);
721 if (state
& META_TEXTURE
) {
724 ASSERT(ctx
->Texture
.CurrentUnit
== 0);
726 /* restore texenv for unit[0] */
727 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, save
->EnvMode
);
729 /* restore texture objects for unit[0] only */
730 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
731 _mesa_reference_texobj(&ctx
->Texture
.Unit
[0].CurrentTex
[tgt
],
732 save
->CurrentTexture
[tgt
]);
733 _mesa_reference_texobj(&save
->CurrentTexture
[tgt
], NULL
);
736 /* Re-enable textures, texgen */
737 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
738 if (save
->TexEnabled
[u
]) {
739 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
741 if (save
->TexEnabled
[u
] & TEXTURE_1D_BIT
)
742 _mesa_set_enable(ctx
, GL_TEXTURE_1D
, GL_TRUE
);
743 if (save
->TexEnabled
[u
] & TEXTURE_2D_BIT
)
744 _mesa_set_enable(ctx
, GL_TEXTURE_2D
, GL_TRUE
);
745 if (save
->TexEnabled
[u
] & TEXTURE_3D_BIT
)
746 _mesa_set_enable(ctx
, GL_TEXTURE_3D
, GL_TRUE
);
747 if (save
->TexEnabled
[u
] & TEXTURE_CUBE_BIT
)
748 _mesa_set_enable(ctx
, GL_TEXTURE_CUBE_MAP
, GL_TRUE
);
749 if (save
->TexEnabled
[u
] & TEXTURE_RECT_BIT
)
750 _mesa_set_enable(ctx
, GL_TEXTURE_RECTANGLE
, GL_TRUE
);
753 if (save
->TexGenEnabled
[u
]) {
754 _mesa_ActiveTextureARB(GL_TEXTURE0
+ u
);
756 if (save
->TexGenEnabled
[u
] & S_BIT
)
757 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_S
, GL_TRUE
);
758 if (save
->TexGenEnabled
[u
] & T_BIT
)
759 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_T
, GL_TRUE
);
760 if (save
->TexGenEnabled
[u
] & R_BIT
)
761 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_R
, GL_TRUE
);
762 if (save
->TexGenEnabled
[u
] & Q_BIT
)
763 _mesa_set_enable(ctx
, GL_TEXTURE_GEN_Q
, GL_TRUE
);
767 /* restore current unit state */
768 _mesa_ActiveTextureARB(GL_TEXTURE0
+ save
->ActiveUnit
);
769 _mesa_ClientActiveTextureARB(GL_TEXTURE0
+ save
->ClientActiveUnit
);
772 if (state
& META_TRANSFORM
) {
773 GLuint activeTexture
= ctx
->Texture
.CurrentUnit
;
774 _mesa_ActiveTextureARB(GL_TEXTURE0
);
775 _mesa_MatrixMode(GL_TEXTURE
);
776 _mesa_LoadMatrixf(save
->TextureMatrix
);
777 _mesa_ActiveTextureARB(GL_TEXTURE0
+ activeTexture
);
779 _mesa_MatrixMode(GL_MODELVIEW
);
780 _mesa_LoadMatrixf(save
->ModelviewMatrix
);
782 _mesa_MatrixMode(GL_PROJECTION
);
783 _mesa_LoadMatrixf(save
->ProjectionMatrix
);
785 _mesa_MatrixMode(save
->MatrixMode
);
787 if (save
->ClipPlanesEnabled
) {
789 for (i
= 0; i
< ctx
->Const
.MaxClipPlanes
; i
++) {
790 if (save
->ClipPlanesEnabled
& (1 << i
)) {
791 _mesa_set_enable(ctx
, GL_CLIP_PLANE0
+ i
, GL_TRUE
);
797 if (state
& META_VERTEX
) {
798 /* restore vertex buffer object */
799 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, save
->ArrayBufferObj
->Name
);
800 _mesa_reference_buffer_object(ctx
, &save
->ArrayBufferObj
, NULL
);
802 /* restore vertex array object */
803 _mesa_BindVertexArray(save
->ArrayObj
->Name
);
804 _mesa_reference_array_object(ctx
, &save
->ArrayObj
, NULL
);
807 if (state
& META_VIEWPORT
) {
808 if (save
->ViewportX
!= ctx
->Viewport
.X
||
809 save
->ViewportY
!= ctx
->Viewport
.Y
||
810 save
->ViewportW
!= ctx
->Viewport
.Width
||
811 save
->ViewportH
!= ctx
->Viewport
.Height
) {
812 _mesa_set_viewport(ctx
, save
->ViewportX
, save
->ViewportY
,
813 save
->ViewportW
, save
->ViewportH
);
815 _mesa_DepthRange(save
->DepthNear
, save
->DepthFar
);
819 if (save
->Lighting
) {
820 _mesa_set_enable(ctx
, GL_LIGHTING
, GL_TRUE
);
826 * Convert Z from a normalized value in the range [0, 1] to an object-space
827 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
828 * default/identity ortho projection results in the original Z value.
829 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
830 * value comes from the clear value or raster position.
832 static INLINE GLfloat
833 invert_z(GLfloat normZ
)
835 GLfloat objZ
= 1.0 - 2.0 * normZ
;
841 * One-time init for a temp_texture object.
842 * Choose tex target, compute max tex size, etc.
845 init_temp_texture(struct gl_context
*ctx
, struct temp_texture
*tex
)
847 /* prefer texture rectangle */
848 if (ctx
->Extensions
.NV_texture_rectangle
) {
849 tex
->Target
= GL_TEXTURE_RECTANGLE
;
850 tex
->MaxSize
= ctx
->Const
.MaxTextureRectSize
;
854 /* use 2D texture, NPOT if possible */
855 tex
->Target
= GL_TEXTURE_2D
;
856 tex
->MaxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
857 tex
->NPOT
= ctx
->Extensions
.ARB_texture_non_power_of_two
;
859 tex
->MinSize
= 16; /* 16 x 16 at least */
860 assert(tex
->MaxSize
> 0);
862 _mesa_GenTextures(1, &tex
->TexObj
);
867 * Return pointer to temp_texture info for non-bitmap ops.
868 * This does some one-time init if needed.
870 static struct temp_texture
*
871 get_temp_texture(struct gl_context
*ctx
)
873 struct temp_texture
*tex
= &ctx
->Meta
->TempTex
;
876 init_temp_texture(ctx
, tex
);
884 * Return pointer to temp_texture info for _mesa_meta_bitmap().
885 * We use a separate texture for bitmaps to reduce texture
886 * allocation/deallocation.
888 static struct temp_texture
*
889 get_bitmap_temp_texture(struct gl_context
*ctx
)
891 struct temp_texture
*tex
= &ctx
->Meta
->Bitmap
.Tex
;
894 init_temp_texture(ctx
, tex
);
902 * Compute the width/height of texture needed to draw an image of the
903 * given size. Return a flag indicating whether the current texture
904 * can be re-used (glTexSubImage2D) or if a new texture needs to be
905 * allocated (glTexImage2D).
906 * Also, compute s/t texcoords for drawing.
908 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
911 alloc_texture(struct temp_texture
*tex
,
912 GLsizei width
, GLsizei height
, GLenum intFormat
)
914 GLboolean newTex
= GL_FALSE
;
916 ASSERT(width
<= tex
->MaxSize
);
917 ASSERT(height
<= tex
->MaxSize
);
919 if (width
> tex
->Width
||
920 height
> tex
->Height
||
921 intFormat
!= tex
->IntFormat
) {
922 /* alloc new texture (larger or different format) */
925 /* use non-power of two size */
926 tex
->Width
= MAX2(tex
->MinSize
, width
);
927 tex
->Height
= MAX2(tex
->MinSize
, height
);
930 /* find power of two size */
932 w
= h
= tex
->MinSize
;
941 tex
->IntFormat
= intFormat
;
946 /* compute texcoords */
947 if (tex
->Target
== GL_TEXTURE_RECTANGLE
) {
948 tex
->Sright
= (GLfloat
) width
;
949 tex
->Ttop
= (GLfloat
) height
;
952 tex
->Sright
= (GLfloat
) width
/ tex
->Width
;
953 tex
->Ttop
= (GLfloat
) height
/ tex
->Height
;
961 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
964 setup_copypix_texture(struct temp_texture
*tex
,
966 GLint srcX
, GLint srcY
,
967 GLsizei width
, GLsizei height
, GLenum intFormat
,
970 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
971 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, filter
);
972 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, filter
);
973 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
975 /* copy framebuffer image to texture */
977 /* create new tex image */
978 if (tex
->Width
== width
&& tex
->Height
== height
) {
979 /* create new tex with framebuffer data */
980 _mesa_CopyTexImage2D(tex
->Target
, 0, tex
->IntFormat
,
981 srcX
, srcY
, width
, height
, 0);
984 /* create empty texture */
985 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
986 tex
->Width
, tex
->Height
, 0,
987 intFormat
, GL_UNSIGNED_BYTE
, NULL
);
989 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
990 0, 0, srcX
, srcY
, width
, height
);
994 /* replace existing tex image */
995 _mesa_CopyTexSubImage2D(tex
->Target
, 0,
996 0, 0, srcX
, srcY
, width
, height
);
1002 * Setup/load texture for glDrawPixels.
1005 setup_drawpix_texture(struct gl_context
*ctx
,
1006 struct temp_texture
*tex
,
1008 GLenum texIntFormat
,
1009 GLsizei width
, GLsizei height
,
1010 GLenum format
, GLenum type
,
1011 const GLvoid
*pixels
)
1013 _mesa_BindTexture(tex
->Target
, tex
->TexObj
);
1014 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1015 _mesa_TexParameteri(tex
->Target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1016 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1018 /* copy pixel data to texture */
1020 /* create new tex image */
1021 if (tex
->Width
== width
&& tex
->Height
== height
) {
1022 /* create new tex and load image data */
1023 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1024 tex
->Width
, tex
->Height
, 0, format
, type
, pixels
);
1027 struct gl_buffer_object
*save_unpack_obj
= NULL
;
1029 _mesa_reference_buffer_object(ctx
, &save_unpack_obj
,
1030 ctx
->Unpack
.BufferObj
);
1031 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1032 /* create empty texture */
1033 _mesa_TexImage2D(tex
->Target
, 0, tex
->IntFormat
,
1034 tex
->Width
, tex
->Height
, 0, format
, type
, NULL
);
1035 if (save_unpack_obj
!= NULL
)
1036 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
,
1037 save_unpack_obj
->Name
);
1039 _mesa_TexSubImage2D(tex
->Target
, 0,
1040 0, 0, width
, height
, format
, type
, pixels
);
1044 /* replace existing tex image */
1045 _mesa_TexSubImage2D(tex
->Target
, 0,
1046 0, 0, width
, height
, format
, type
, pixels
);
1053 * One-time init for drawing depth pixels.
1056 init_blit_depth_pixels(struct gl_context
*ctx
)
1058 static const char *program
=
1060 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1063 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1064 struct temp_texture
*tex
= get_temp_texture(ctx
);
1065 const char *texTarget
;
1067 assert(blit
->DepthFP
== 0);
1069 /* replace %s with "RECT" or "2D" */
1070 assert(strlen(program
) + 4 < sizeof(program2
));
1071 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1075 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1077 _mesa_GenPrograms(1, &blit
->DepthFP
);
1078 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1079 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1080 strlen(program2
), (const GLubyte
*) program2
);
1085 * Try to do a glBlitFramebuffer using no-copy texturing.
1086 * We can do this when the src renderbuffer is actually a texture.
1087 * But if the src buffer == dst buffer we cannot do this.
1089 * \return new buffer mask indicating the buffers left to blit using the
1093 blitframebuffer_texture(struct gl_context
*ctx
,
1094 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1095 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1096 GLbitfield mask
, GLenum filter
)
1098 if (mask
& GL_COLOR_BUFFER_BIT
) {
1099 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
1100 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
1101 const struct gl_renderbuffer_attachment
*drawAtt
=
1102 &drawFb
->Attachment
[drawFb
->_ColorDrawBufferIndexes
[0]];
1103 const struct gl_renderbuffer_attachment
*readAtt
=
1104 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
1106 if (readAtt
&& readAtt
->Texture
) {
1107 const struct gl_texture_object
*texObj
= readAtt
->Texture
;
1108 const GLuint srcLevel
= readAtt
->TextureLevel
;
1109 const GLenum minFilterSave
= texObj
->MinFilter
;
1110 const GLenum magFilterSave
= texObj
->MagFilter
;
1111 const GLint baseLevelSave
= texObj
->BaseLevel
;
1112 const GLint maxLevelSave
= texObj
->MaxLevel
;
1113 const GLenum wrapSSave
= texObj
->WrapS
;
1114 const GLenum wrapTSave
= texObj
->WrapT
;
1115 const GLenum target
= texObj
->Target
;
1117 if (drawAtt
->Texture
== readAtt
->Texture
) {
1118 /* Can't use same texture as both the source and dest. We need
1119 * to handle overlapping blits and besides, some hw may not
1125 if (target
!= GL_TEXTURE_2D
&& target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1126 /* Can't handle other texture types at this time */
1131 printf("Blit from texture!\n");
1132 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1133 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1136 /* Prepare src texture state */
1137 _mesa_BindTexture(target
, texObj
->Name
);
1138 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
1139 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
1140 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1141 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
1142 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
1144 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1145 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1146 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1147 _mesa_set_enable(ctx
, target
, GL_TRUE
);
1149 /* Prepare vertex data (the VBO was previously created and bound) */
1154 struct vertex verts
[4];
1155 GLfloat s0
, t0
, s1
, t1
;
1157 if (target
== GL_TEXTURE_2D
) {
1158 const struct gl_texture_image
*texImage
1159 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
1160 s0
= srcX0
/ (float) texImage
->Width
;
1161 s1
= srcX1
/ (float) texImage
->Width
;
1162 t0
= srcY0
/ (float) texImage
->Height
;
1163 t1
= srcY1
/ (float) texImage
->Height
;
1166 assert(target
== GL_TEXTURE_RECTANGLE_ARB
);
1173 verts
[0].x
= (GLfloat
) dstX0
;
1174 verts
[0].y
= (GLfloat
) dstY0
;
1175 verts
[1].x
= (GLfloat
) dstX1
;
1176 verts
[1].y
= (GLfloat
) dstY0
;
1177 verts
[2].x
= (GLfloat
) dstX1
;
1178 verts
[2].y
= (GLfloat
) dstY1
;
1179 verts
[3].x
= (GLfloat
) dstX0
;
1180 verts
[3].y
= (GLfloat
) dstY1
;
1191 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1194 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1196 /* Restore texture object state, the texture binding will
1197 * be restored by _mesa_meta_end().
1199 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
1200 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
1201 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
1202 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
1203 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
1205 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
1206 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
1208 /* Done with color buffer */
1209 mask
&= ~GL_COLOR_BUFFER_BIT
;
1218 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1219 * of texture mapping and polygon rendering.
1222 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
1223 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
1224 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
1225 GLbitfield mask
, GLenum filter
)
1227 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
1228 struct temp_texture
*tex
= get_temp_texture(ctx
);
1229 const GLsizei maxTexSize
= tex
->MaxSize
;
1230 const GLint srcX
= MIN2(srcX0
, srcX1
);
1231 const GLint srcY
= MIN2(srcY0
, srcY1
);
1232 const GLint srcW
= abs(srcX1
- srcX0
);
1233 const GLint srcH
= abs(srcY1
- srcY0
);
1234 const GLboolean srcFlipX
= srcX1
< srcX0
;
1235 const GLboolean srcFlipY
= srcY1
< srcY0
;
1239 struct vertex verts
[4];
1242 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
1243 /* XXX avoid this fallback */
1244 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1245 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1261 /* only scissor effects blit so save/clear all other relevant state */
1262 _mesa_meta_begin(ctx
, ~META_SCISSOR
);
1264 if (blit
->ArrayObj
== 0) {
1265 /* one-time setup */
1267 /* create vertex array object */
1268 _mesa_GenVertexArrays(1, &blit
->ArrayObj
);
1269 _mesa_BindVertexArray(blit
->ArrayObj
);
1271 /* create vertex array buffer */
1272 _mesa_GenBuffersARB(1, &blit
->VBO
);
1273 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1274 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1275 NULL
, GL_DYNAMIC_DRAW_ARB
);
1277 /* setup vertex arrays */
1278 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1279 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1280 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1281 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1284 _mesa_BindVertexArray(blit
->ArrayObj
);
1285 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, blit
->VBO
);
1288 /* Try faster, direct texture approach first */
1289 mask
= blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1290 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1292 _mesa_meta_end(ctx
);
1296 /* Continue with "normal" approach which involves copying the src rect
1297 * into a temporary texture and is "blitted" by drawing a textured quad.
1300 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_RGBA
);
1302 /* vertex positions/texcoords (after texture allocation!) */
1304 verts
[0].x
= (GLfloat
) dstX0
;
1305 verts
[0].y
= (GLfloat
) dstY0
;
1306 verts
[1].x
= (GLfloat
) dstX1
;
1307 verts
[1].y
= (GLfloat
) dstY0
;
1308 verts
[2].x
= (GLfloat
) dstX1
;
1309 verts
[2].y
= (GLfloat
) dstY1
;
1310 verts
[3].x
= (GLfloat
) dstX0
;
1311 verts
[3].y
= (GLfloat
) dstY1
;
1315 verts
[1].s
= tex
->Sright
;
1317 verts
[2].s
= tex
->Sright
;
1318 verts
[2].t
= tex
->Ttop
;
1320 verts
[3].t
= tex
->Ttop
;
1322 /* upload new vertex data */
1323 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1326 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1328 if (mask
& GL_COLOR_BUFFER_BIT
) {
1329 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, srcW
, srcH
,
1331 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1332 mask
&= ~GL_COLOR_BUFFER_BIT
;
1335 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1336 GLuint
*tmp
= (GLuint
*) malloc(srcW
* srcH
* sizeof(GLuint
));
1339 init_blit_depth_pixels(ctx
);
1341 /* maybe change tex format here */
1342 newTex
= alloc_texture(tex
, srcW
, srcH
, GL_DEPTH_COMPONENT
);
1344 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
,
1345 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1347 setup_drawpix_texture(ctx
, tex
, newTex
, GL_DEPTH_COMPONENT
, srcW
, srcH
,
1348 GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, tmp
);
1350 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
1351 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1352 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1353 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1354 _mesa_DepthFunc(GL_ALWAYS
);
1355 _mesa_DepthMask(GL_TRUE
);
1357 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1358 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1364 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1365 /* XXX can't easily do stencil */
1368 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1370 _mesa_meta_end(ctx
);
1373 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
1374 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
1380 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1383 _mesa_meta_Clear(struct gl_context
*ctx
, GLbitfield buffers
)
1385 struct clear_state
*clear
= &ctx
->Meta
->Clear
;
1387 GLfloat x
, y
, z
, r
, g
, b
, a
;
1389 struct vertex verts
[4];
1390 /* save all state but scissor, pixel pack/unpack */
1391 GLbitfield metaSave
= META_ALL
- META_SCISSOR
- META_PIXEL_STORE
;
1393 if (buffers
& BUFFER_BITS_COLOR
) {
1394 /* if clearing color buffers, don't save/restore colormask */
1395 metaSave
-= META_COLOR_MASK
;
1398 _mesa_meta_begin(ctx
, metaSave
);
1400 if (clear
->ArrayObj
== 0) {
1401 /* one-time setup */
1403 /* create vertex array object */
1404 _mesa_GenVertexArrays(1, &clear
->ArrayObj
);
1405 _mesa_BindVertexArray(clear
->ArrayObj
);
1407 /* create vertex array buffer */
1408 _mesa_GenBuffersARB(1, &clear
->VBO
);
1409 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1411 /* setup vertex arrays */
1412 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1413 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
1414 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1415 _mesa_EnableClientState(GL_COLOR_ARRAY
);
1418 _mesa_BindVertexArray(clear
->ArrayObj
);
1419 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, clear
->VBO
);
1422 /* GL_COLOR_BUFFER_BIT */
1423 if (buffers
& BUFFER_BITS_COLOR
) {
1424 /* leave colormask, glDrawBuffer state as-is */
1427 ASSERT(metaSave
& META_COLOR_MASK
);
1428 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1431 /* GL_DEPTH_BUFFER_BIT */
1432 if (buffers
& BUFFER_BIT_DEPTH
) {
1433 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1434 _mesa_DepthFunc(GL_ALWAYS
);
1435 _mesa_DepthMask(GL_TRUE
);
1438 assert(!ctx
->Depth
.Test
);
1441 /* GL_STENCIL_BUFFER_BIT */
1442 if (buffers
& BUFFER_BIT_STENCIL
) {
1443 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1444 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK
,
1445 GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1446 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK
, GL_ALWAYS
,
1447 ctx
->Stencil
.Clear
& 0x7fffffff,
1448 ctx
->Stencil
.WriteMask
[0]);
1451 assert(!ctx
->Stencil
.Enabled
);
1454 /* vertex positions/colors */
1456 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
1457 const GLfloat y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
1458 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
1459 const GLfloat y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
1460 const GLfloat z
= invert_z(ctx
->Depth
.Clear
);
1477 for (i
= 0; i
< 4; i
++) {
1478 verts
[i
].r
= ctx
->Color
.ClearColor
[0];
1479 verts
[i
].g
= ctx
->Color
.ClearColor
[1];
1480 verts
[i
].b
= ctx
->Color
.ClearColor
[2];
1481 verts
[i
].a
= ctx
->Color
.ClearColor
[3];
1484 /* upload new vertex data */
1485 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
,
1486 GL_DYNAMIC_DRAW_ARB
);
1490 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1492 _mesa_meta_end(ctx
);
1497 * Meta implementation of ctx->Driver.CopyPixels() in terms
1498 * of texture mapping and polygon rendering.
1501 _mesa_meta_CopyPixels(struct gl_context
*ctx
, GLint srcX
, GLint srcY
,
1502 GLsizei width
, GLsizei height
,
1503 GLint dstX
, GLint dstY
, GLenum type
)
1505 struct copypix_state
*copypix
= &ctx
->Meta
->CopyPix
;
1506 struct temp_texture
*tex
= get_temp_texture(ctx
);
1508 GLfloat x
, y
, z
, s
, t
;
1510 struct vertex verts
[4];
1512 GLenum intFormat
= GL_RGBA
;
1514 if (type
!= GL_COLOR
||
1515 ctx
->_ImageTransferState
||
1517 width
> tex
->MaxSize
||
1518 height
> tex
->MaxSize
) {
1519 /* XXX avoid this fallback */
1520 _swrast_CopyPixels(ctx
, srcX
, srcY
, width
, height
, dstX
, dstY
, type
);
1524 /* Most GL state applies to glCopyPixels, but a there's a few things
1525 * we need to override:
1527 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1534 if (copypix
->ArrayObj
== 0) {
1535 /* one-time setup */
1537 /* create vertex array object */
1538 _mesa_GenVertexArrays(1, ©pix
->ArrayObj
);
1539 _mesa_BindVertexArray(copypix
->ArrayObj
);
1541 /* create vertex array buffer */
1542 _mesa_GenBuffersARB(1, ©pix
->VBO
);
1543 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1544 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1545 NULL
, GL_DYNAMIC_DRAW_ARB
);
1547 /* setup vertex arrays */
1548 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1549 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1550 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1551 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1554 _mesa_BindVertexArray(copypix
->ArrayObj
);
1555 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, copypix
->VBO
);
1558 newTex
= alloc_texture(tex
, width
, height
, intFormat
);
1560 /* vertex positions, texcoords (after texture allocation!) */
1562 const GLfloat dstX0
= (GLfloat
) dstX
;
1563 const GLfloat dstY0
= (GLfloat
) dstY
;
1564 const GLfloat dstX1
= dstX
+ width
* ctx
->Pixel
.ZoomX
;
1565 const GLfloat dstY1
= dstY
+ height
* ctx
->Pixel
.ZoomY
;
1566 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1576 verts
[1].s
= tex
->Sright
;
1581 verts
[2].s
= tex
->Sright
;
1582 verts
[2].t
= tex
->Ttop
;
1587 verts
[3].t
= tex
->Ttop
;
1589 /* upload new vertex data */
1590 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
1593 /* Alloc/setup texture */
1594 setup_copypix_texture(tex
, newTex
, srcX
, srcY
, width
, height
,
1595 GL_RGBA
, GL_NEAREST
);
1597 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1599 /* draw textured quad */
1600 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1602 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1604 _mesa_meta_end(ctx
);
1610 * When the glDrawPixels() image size is greater than the max rectangle
1611 * texture size we use this function to break the glDrawPixels() image
1612 * into tiles which fit into the max texture size.
1615 tiled_draw_pixels(struct gl_context
*ctx
,
1617 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1618 GLenum format
, GLenum type
,
1619 const struct gl_pixelstore_attrib
*unpack
,
1620 const GLvoid
*pixels
)
1622 struct gl_pixelstore_attrib tileUnpack
= *unpack
;
1625 if (tileUnpack
.RowLength
== 0)
1626 tileUnpack
.RowLength
= width
;
1628 for (i
= 0; i
< width
; i
+= tileSize
) {
1629 const GLint tileWidth
= MIN2(tileSize
, width
- i
);
1630 const GLint tileX
= (GLint
) (x
+ i
* ctx
->Pixel
.ZoomX
);
1632 tileUnpack
.SkipPixels
= unpack
->SkipPixels
+ i
;
1634 for (j
= 0; j
< height
; j
+= tileSize
) {
1635 const GLint tileHeight
= MIN2(tileSize
, height
- j
);
1636 const GLint tileY
= (GLint
) (y
+ j
* ctx
->Pixel
.ZoomY
);
1638 tileUnpack
.SkipRows
= unpack
->SkipRows
+ j
;
1640 _mesa_meta_DrawPixels(ctx
, tileX
, tileY
, tileWidth
, tileHeight
,
1641 format
, type
, &tileUnpack
, pixels
);
1648 * One-time init for drawing stencil pixels.
1651 init_draw_stencil_pixels(struct gl_context
*ctx
)
1653 /* This program is run eight times, once for each stencil bit.
1654 * The stencil values to draw are found in an 8-bit alpha texture.
1655 * We read the texture/stencil value and test if bit 'b' is set.
1656 * If the bit is not set, use KIL to kill the fragment.
1657 * Finally, we use the stencil test to update the stencil buffer.
1659 * The basic algorithm for checking if a bit is set is:
1660 * if (is_odd(value / (1 << bit)))
1661 * result is one (or non-zero).
1664 * The program parameter contains three values:
1665 * parm.x = 255 / (1 << bit)
1669 static const char *program
=
1671 "PARAM parm = program.local[0]; \n"
1673 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1674 "# t = t * 255 / bit \n"
1675 "MUL t.x, t.a, parm.x; \n"
1678 "SUB t.x, t.x, t.y; \n"
1680 "MUL t.x, t.x, parm.y; \n"
1681 "# t = fract(t.x) \n"
1682 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1683 "# t.x = (t.x == 0 ? 1 : 0) \n"
1684 "SGE t.x, -t.x, parm.z; \n"
1686 "# for debug only \n"
1687 "#MOV result.color, t.x; \n"
1689 char program2
[1000];
1690 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1691 struct temp_texture
*tex
= get_temp_texture(ctx
);
1692 const char *texTarget
;
1694 assert(drawpix
->StencilFP
== 0);
1696 /* replace %s with "RECT" or "2D" */
1697 assert(strlen(program
) + 4 < sizeof(program2
));
1698 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1702 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1704 _mesa_GenPrograms(1, &drawpix
->StencilFP
);
1705 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1706 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1707 strlen(program2
), (const GLubyte
*) program2
);
1712 * One-time init for drawing depth pixels.
1715 init_draw_depth_pixels(struct gl_context
*ctx
)
1717 static const char *program
=
1719 "PARAM color = program.local[0]; \n"
1720 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1721 "MOV result.color, color; \n"
1724 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1725 struct temp_texture
*tex
= get_temp_texture(ctx
);
1726 const char *texTarget
;
1728 assert(drawpix
->DepthFP
== 0);
1730 /* replace %s with "RECT" or "2D" */
1731 assert(strlen(program
) + 4 < sizeof(program2
));
1732 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
1736 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
1738 _mesa_GenPrograms(1, &drawpix
->DepthFP
);
1739 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1740 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
1741 strlen(program2
), (const GLubyte
*) program2
);
1746 * Meta implementation of ctx->Driver.DrawPixels() in terms
1747 * of texture mapping and polygon rendering.
1750 _mesa_meta_DrawPixels(struct gl_context
*ctx
,
1751 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1752 GLenum format
, GLenum type
,
1753 const struct gl_pixelstore_attrib
*unpack
,
1754 const GLvoid
*pixels
)
1756 struct drawpix_state
*drawpix
= &ctx
->Meta
->DrawPix
;
1757 struct temp_texture
*tex
= get_temp_texture(ctx
);
1758 const struct gl_pixelstore_attrib unpackSave
= ctx
->Unpack
;
1759 const GLuint origStencilMask
= ctx
->Stencil
.WriteMask
[0];
1761 GLfloat x
, y
, z
, s
, t
;
1763 struct vertex verts
[4];
1764 GLenum texIntFormat
;
1765 GLboolean fallback
, newTex
;
1766 GLbitfield metaExtraSave
= 0x0;
1770 * Determine if we can do the glDrawPixels with texture mapping.
1772 fallback
= GL_FALSE
;
1773 if (ctx
->_ImageTransferState
||
1778 if (_mesa_is_color_format(format
)) {
1779 /* use more compact format when possible */
1780 /* XXX disable special case for GL_LUMINANCE for now to work around
1781 * apparent i965 driver bug (see bug #23670).
1783 if (/*format == GL_LUMINANCE ||*/ format
== GL_LUMINANCE_ALPHA
)
1784 texIntFormat
= format
;
1786 texIntFormat
= GL_RGBA
;
1788 else if (_mesa_is_stencil_format(format
)) {
1789 if (ctx
->Extensions
.ARB_fragment_program
&&
1790 ctx
->Pixel
.IndexShift
== 0 &&
1791 ctx
->Pixel
.IndexOffset
== 0 &&
1792 type
== GL_UNSIGNED_BYTE
) {
1793 /* We'll store stencil as alpha. This only works for GLubyte
1794 * image data because of how incoming values are mapped to alpha
1797 texIntFormat
= GL_ALPHA
;
1798 metaExtraSave
= (META_COLOR_MASK
|
1807 else if (_mesa_is_depth_format(format
)) {
1808 if (ctx
->Extensions
.ARB_depth_texture
&&
1809 ctx
->Extensions
.ARB_fragment_program
) {
1810 texIntFormat
= GL_DEPTH_COMPONENT
;
1811 metaExtraSave
= (META_SHADER
);
1822 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
1823 format
, type
, unpack
, pixels
);
1828 * Check image size against max texture size, draw as tiles if needed.
1830 if (width
> tex
->MaxSize
|| height
> tex
->MaxSize
) {
1831 tiled_draw_pixels(ctx
, tex
->MaxSize
, x
, y
, width
, height
,
1832 format
, type
, unpack
, pixels
);
1836 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1837 * but a there's a few things we need to override:
1839 _mesa_meta_begin(ctx
, (META_RASTERIZATION
|
1847 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
1849 /* vertex positions, texcoords (after texture allocation!) */
1851 const GLfloat x0
= (GLfloat
) x
;
1852 const GLfloat y0
= (GLfloat
) y
;
1853 const GLfloat x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1854 const GLfloat y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1855 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
1865 verts
[1].s
= tex
->Sright
;
1870 verts
[2].s
= tex
->Sright
;
1871 verts
[2].t
= tex
->Ttop
;
1876 verts
[3].t
= tex
->Ttop
;
1879 if (drawpix
->ArrayObj
== 0) {
1880 /* one-time setup: create vertex array object */
1881 _mesa_GenVertexArrays(1, &drawpix
->ArrayObj
);
1883 _mesa_BindVertexArray(drawpix
->ArrayObj
);
1885 /* create vertex array buffer */
1886 _mesa_GenBuffersARB(1, &vbo
);
1887 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, vbo
);
1888 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
1889 verts
, GL_DYNAMIC_DRAW_ARB
);
1891 /* setup vertex arrays */
1892 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
1893 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
1894 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
1895 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
1897 /* set given unpack params */
1898 ctx
->Unpack
= *unpack
;
1900 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
1902 if (_mesa_is_stencil_format(format
)) {
1903 /* Drawing stencil */
1906 if (!drawpix
->StencilFP
)
1907 init_draw_stencil_pixels(ctx
);
1909 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1910 GL_ALPHA
, type
, pixels
);
1912 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1914 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_TRUE
);
1916 /* set all stencil bits to 0 */
1917 _mesa_StencilOp(GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
1918 _mesa_StencilFunc(GL_ALWAYS
, 0, 255);
1919 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1921 /* set stencil bits to 1 where needed */
1922 _mesa_StencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
1924 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->StencilFP
);
1925 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1927 for (bit
= 0; bit
< ctx
->DrawBuffer
->Visual
.stencilBits
; bit
++) {
1928 const GLuint mask
= 1 << bit
;
1929 if (mask
& origStencilMask
) {
1930 _mesa_StencilFunc(GL_ALWAYS
, mask
, mask
);
1931 _mesa_StencilMask(mask
);
1933 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1934 255.0 / mask
, 0.5, 0.0, 0.0);
1936 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1940 else if (_mesa_is_depth_format(format
)) {
1942 if (!drawpix
->DepthFP
)
1943 init_draw_depth_pixels(ctx
);
1945 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB
, drawpix
->DepthFP
);
1946 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
1948 /* polygon color = current raster color */
1949 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
1950 ctx
->Current
.RasterColor
);
1952 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1953 format
, type
, pixels
);
1955 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1959 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
1960 format
, type
, pixels
);
1961 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
1964 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
1966 _mesa_DeleteBuffersARB(1, &vbo
);
1968 /* restore unpack params */
1969 ctx
->Unpack
= unpackSave
;
1971 _mesa_meta_end(ctx
);
1976 * Do glBitmap with a alpha texture quad. Use the alpha test to
1977 * cull the 'off' bits. If alpha test is already enabled, fall back
1978 * to swrast (should be a rare case).
1979 * A bitmap cache as in the gallium/mesa state tracker would
1980 * improve performance a lot.
1983 _mesa_meta_Bitmap(struct gl_context
*ctx
,
1984 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1985 const struct gl_pixelstore_attrib
*unpack
,
1986 const GLubyte
*bitmap1
)
1988 struct bitmap_state
*bitmap
= &ctx
->Meta
->Bitmap
;
1989 struct temp_texture
*tex
= get_bitmap_temp_texture(ctx
);
1990 const GLenum texIntFormat
= GL_ALPHA
;
1991 const struct gl_pixelstore_attrib unpackSave
= *unpack
;
1993 GLfloat x
, y
, z
, s
, t
, r
, g
, b
, a
;
1995 struct vertex verts
[4];
2000 * Check if swrast fallback is needed.
2002 if (ctx
->_ImageTransferState
||
2003 ctx
->Color
.AlphaEnabled
||
2005 ctx
->Texture
._EnabledUnits
||
2006 width
> tex
->MaxSize
||
2007 height
> tex
->MaxSize
) {
2008 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap1
);
2012 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2013 * but a there's a few things we need to override:
2015 _mesa_meta_begin(ctx
, (META_ALPHA_TEST
|
2017 META_RASTERIZATION
|
2024 if (bitmap
->ArrayObj
== 0) {
2025 /* one-time setup */
2027 /* create vertex array object */
2028 _mesa_GenVertexArraysAPPLE(1, &bitmap
->ArrayObj
);
2029 _mesa_BindVertexArrayAPPLE(bitmap
->ArrayObj
);
2031 /* create vertex array buffer */
2032 _mesa_GenBuffersARB(1, &bitmap
->VBO
);
2033 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2034 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2035 NULL
, GL_DYNAMIC_DRAW_ARB
);
2037 /* setup vertex arrays */
2038 _mesa_VertexPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2039 _mesa_TexCoordPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2040 _mesa_ColorPointer(4, GL_FLOAT
, sizeof(struct vertex
), OFFSET(r
));
2041 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2042 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2043 _mesa_EnableClientState(GL_COLOR_ARRAY
);
2046 _mesa_BindVertexArray(bitmap
->ArrayObj
);
2047 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, bitmap
->VBO
);
2050 newTex
= alloc_texture(tex
, width
, height
, texIntFormat
);
2052 /* vertex positions, texcoords, colors (after texture allocation!) */
2054 const GLfloat x0
= (GLfloat
) x
;
2055 const GLfloat y0
= (GLfloat
) y
;
2056 const GLfloat x1
= (GLfloat
) (x
+ width
);
2057 const GLfloat y1
= (GLfloat
) (y
+ height
);
2058 const GLfloat z
= invert_z(ctx
->Current
.RasterPos
[2]);
2069 verts
[1].s
= tex
->Sright
;
2074 verts
[2].s
= tex
->Sright
;
2075 verts
[2].t
= tex
->Ttop
;
2080 verts
[3].t
= tex
->Ttop
;
2082 for (i
= 0; i
< 4; i
++) {
2083 verts
[i
].r
= ctx
->Current
.RasterColor
[0];
2084 verts
[i
].g
= ctx
->Current
.RasterColor
[1];
2085 verts
[i
].b
= ctx
->Current
.RasterColor
[2];
2086 verts
[i
].a
= ctx
->Current
.RasterColor
[3];
2089 /* upload new vertex data */
2090 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2093 bitmap1
= _mesa_map_pbo_source(ctx
, &unpackSave
, bitmap1
);
2095 _mesa_meta_end(ctx
);
2099 bitmap8
= (GLubyte
*) calloc(1, width
* height
);
2101 _mesa_expand_bitmap(width
, height
, &unpackSave
, bitmap1
,
2102 bitmap8
, width
, 0xff);
2104 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
2106 _mesa_set_enable(ctx
, GL_ALPHA_TEST
, GL_TRUE
);
2107 _mesa_AlphaFunc(GL_GREATER
, 0.0);
2109 setup_drawpix_texture(ctx
, tex
, newTex
, texIntFormat
, width
, height
,
2110 GL_ALPHA
, GL_UNSIGNED_BYTE
, bitmap8
);
2112 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2114 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
2119 _mesa_unmap_pbo_source(ctx
, &unpackSave
);
2121 _mesa_meta_end(ctx
);
2126 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2127 * software fallback. The fallback path will require that the texture
2128 * images are mapped.
2129 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2132 _mesa_meta_check_generate_mipmap_fallback(struct gl_context
*ctx
, GLenum target
,
2133 struct gl_texture_object
*texObj
)
2135 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2136 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2137 struct gl_texture_image
*baseImage
;
2141 /* check for fallbacks */
2142 if (!ctx
->Extensions
.EXT_framebuffer_object
||
2143 target
== GL_TEXTURE_3D
) {
2147 srcLevel
= texObj
->BaseLevel
;
2148 baseImage
= _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
2149 if (!baseImage
|| _mesa_is_format_compressed(baseImage
->TexFormat
)) {
2154 * Test that we can actually render in the texture's format.
2157 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2158 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2160 if (target
== GL_TEXTURE_1D
) {
2161 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2162 GL_COLOR_ATTACHMENT0_EXT
,
2163 target
, texObj
->Name
, srcLevel
);
2166 /* other work is needed to enable 3D mipmap generation */
2167 else if (target
== GL_TEXTURE_3D
) {
2169 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2170 GL_COLOR_ATTACHMENT0_EXT
,
2171 target
, texObj
->Name
, srcLevel
, zoffset
);
2176 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2177 GL_COLOR_ATTACHMENT0_EXT
,
2178 target
, texObj
->Name
, srcLevel
);
2181 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2183 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2185 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2194 * Called via ctx->Driver.GenerateMipmap()
2195 * Note: texture borders and 3D texture support not yet complete.
2198 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
2199 struct gl_texture_object
*texObj
)
2201 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
2203 GLfloat x
, y
, s
, t
, r
;
2205 struct vertex verts
[4];
2206 const GLuint baseLevel
= texObj
->BaseLevel
;
2207 const GLuint maxLevel
= texObj
->MaxLevel
;
2208 const GLenum minFilterSave
= texObj
->MinFilter
;
2209 const GLenum magFilterSave
= texObj
->MagFilter
;
2210 const GLint baseLevelSave
= texObj
->BaseLevel
;
2211 const GLint maxLevelSave
= texObj
->MaxLevel
;
2212 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
2213 const GLenum wrapSSave
= texObj
->WrapS
;
2214 const GLenum wrapTSave
= texObj
->WrapT
;
2215 const GLenum wrapRSave
= texObj
->WrapR
;
2216 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
2217 const GLuint original_active_unit
= ctx
->Texture
.CurrentUnit
;
2222 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
2223 _mesa_generate_mipmap(ctx
, target
, texObj
);
2227 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
2228 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
2229 faceTarget
= target
;
2230 target
= GL_TEXTURE_CUBE_MAP
;
2233 faceTarget
= target
;
2236 _mesa_meta_begin(ctx
, META_ALL
);
2238 if (original_active_unit
!= 0)
2239 _mesa_BindTexture(target
, texObj
->Name
);
2241 if (mipmap
->ArrayObj
== 0) {
2242 /* one-time setup */
2244 /* create vertex array object */
2245 _mesa_GenVertexArraysAPPLE(1, &mipmap
->ArrayObj
);
2246 _mesa_BindVertexArrayAPPLE(mipmap
->ArrayObj
);
2248 /* create vertex array buffer */
2249 _mesa_GenBuffersARB(1, &mipmap
->VBO
);
2250 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2251 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
2252 NULL
, GL_DYNAMIC_DRAW_ARB
);
2254 /* setup vertex arrays */
2255 _mesa_VertexPointer(2, GL_FLOAT
, sizeof(struct vertex
), OFFSET(x
));
2256 _mesa_TexCoordPointer(3, GL_FLOAT
, sizeof(struct vertex
), OFFSET(s
));
2257 _mesa_EnableClientState(GL_VERTEX_ARRAY
);
2258 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY
);
2261 _mesa_BindVertexArray(mipmap
->ArrayObj
);
2262 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB
, mipmap
->VBO
);
2266 _mesa_GenFramebuffersEXT(1, &mipmap
->FBO
);
2268 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
2270 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
2271 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
2272 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
2273 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2274 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2275 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
2277 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2279 /* setup texcoords once (XXX what about border?) */
2280 switch (faceTarget
) {
2302 static const GLfloat st
[4][2] = {
2303 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
2307 /* loop over quad verts */
2308 for (i
= 0; i
< 4; i
++) {
2309 /* Compute sc = +/-scale and tc = +/-scale.
2310 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2311 * though that can still sometimes happen with this scale factor...
2313 const GLfloat scale
= 0.9999f
;
2314 const GLfloat sc
= (2.0f
* st
[i
][0] - 1.0f
) * scale
;
2315 const GLfloat tc
= (2.0f
* st
[i
][1] - 1.0f
) * scale
;
2317 switch (faceTarget
) {
2318 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
2323 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
2328 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
2333 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
2338 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
2343 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
2355 _mesa_set_enable(ctx
, target
, GL_TRUE
);
2357 /* setup vertex positions */
2368 /* upload new vertex data */
2369 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
2372 /* setup projection matrix */
2373 _mesa_MatrixMode(GL_PROJECTION
);
2374 _mesa_LoadIdentity();
2375 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2377 /* texture is already locked, unlock now */
2378 _mesa_unlock_texture(ctx
, texObj
);
2380 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
2381 const struct gl_texture_image
*srcImage
;
2382 const GLuint srcLevel
= dstLevel
- 1;
2383 GLsizei srcWidth
, srcHeight
, srcDepth
;
2384 GLsizei dstWidth
, dstHeight
, dstDepth
;
2387 srcImage
= _mesa_select_tex_image(ctx
, texObj
, faceTarget
, srcLevel
);
2388 assert(srcImage
->Border
== 0); /* XXX we can fix this */
2390 /* src size w/out border */
2391 srcWidth
= srcImage
->Width
- 2 * border
;
2392 srcHeight
= srcImage
->Height
- 2 * border
;
2393 srcDepth
= srcImage
->Depth
- 2 * border
;
2395 /* new dst size w/ border */
2396 dstWidth
= MAX2(1, srcWidth
/ 2) + 2 * border
;
2397 dstHeight
= MAX2(1, srcHeight
/ 2) + 2 * border
;
2398 dstDepth
= MAX2(1, srcDepth
/ 2) + 2 * border
;
2400 if (dstWidth
== srcImage
->Width
&&
2401 dstHeight
== srcImage
->Height
&&
2402 dstDepth
== srcImage
->Depth
) {
2407 /* Set MaxLevel large enough to hold the new level when we allocate it */
2408 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
2410 /* Create empty dest image */
2411 if (target
== GL_TEXTURE_1D
) {
2412 _mesa_TexImage1D(target
, dstLevel
, srcImage
->InternalFormat
,
2414 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2416 else if (target
== GL_TEXTURE_3D
) {
2417 _mesa_TexImage3D(target
, dstLevel
, srcImage
->InternalFormat
,
2418 dstWidth
, dstHeight
, dstDepth
, border
,
2419 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2423 _mesa_TexImage2D(faceTarget
, dstLevel
, srcImage
->InternalFormat
,
2424 dstWidth
, dstHeight
, border
,
2425 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2427 if (target
== GL_TEXTURE_CUBE_MAP
) {
2428 /* If texturing from a cube, we need to make sure all src faces
2429 * have been defined (even if we're not sampling from them.)
2430 * Otherwise the texture object will be 'incomplete' and
2431 * texturing from it will not be allowed.
2434 for (face
= 0; face
< 6; face
++) {
2435 if (!texObj
->Image
[face
][srcLevel
] ||
2436 texObj
->Image
[face
][srcLevel
]->Width
!= srcWidth
) {
2437 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
,
2438 srcLevel
, srcImage
->InternalFormat
,
2439 srcWidth
, srcHeight
, border
,
2440 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
2446 /* limit sampling to src level */
2447 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
2448 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
2450 /* Set to draw into the current dstLevel */
2451 if (target
== GL_TEXTURE_1D
) {
2452 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT
,
2453 GL_COLOR_ATTACHMENT0_EXT
,
2458 else if (target
== GL_TEXTURE_3D
) {
2459 GLint zoffset
= 0; /* XXX unfinished */
2460 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT
,
2461 GL_COLOR_ATTACHMENT0_EXT
,
2468 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
2469 GL_COLOR_ATTACHMENT0_EXT
,
2475 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
2478 status
= _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
2479 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2484 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
2485 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
2487 /* setup viewport */
2488 _mesa_set_viewport(ctx
, 0, 0, dstWidth
, dstHeight
);
2490 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
2493 _mesa_lock_texture(ctx
, texObj
); /* relock */
2495 _mesa_meta_end(ctx
);
2497 _mesa_TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, minFilterSave
);
2498 _mesa_TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, magFilterSave
);
2499 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
2500 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
2501 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
2502 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_S
, wrapSSave
);
2503 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_T
, wrapTSave
);
2504 _mesa_TexParameteri(target
, GL_TEXTURE_WRAP_R
, wrapRSave
);
2506 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fboSave
);
2511 * Determine the GL data type to use for the temporary image read with
2512 * ReadPixels() and passed to Tex[Sub]Image().
2515 get_temp_image_type(struct gl_context
*ctx
, GLenum baseFormat
)
2517 switch (baseFormat
) {
2522 case GL_LUMINANCE_ALPHA
:
2524 if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2525 return GL_UNSIGNED_BYTE
;
2526 else if (ctx
->DrawBuffer
->Visual
.redBits
<= 8)
2527 return GL_UNSIGNED_SHORT
;
2530 case GL_DEPTH_COMPONENT
:
2531 return GL_UNSIGNED_INT
;
2532 case GL_DEPTH_STENCIL
:
2533 return GL_UNSIGNED_INT_24_8
;
2535 _mesa_problem(ctx
, "Unexpected format in get_temp_image_type()");
2542 * Helper for _mesa_meta_CopyTexImage1/2D() functions.
2543 * Have to be careful with locking and meta state for pixel transfer.
2546 copy_tex_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2547 GLenum internalFormat
, GLint x
, GLint y
,
2548 GLsizei width
, GLsizei height
, GLint border
)
2550 struct gl_texture_object
*texObj
;
2551 struct gl_texture_image
*texImage
;
2552 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2553 GLenum format
, type
;
2557 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2558 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2560 format
= _mesa_base_tex_format(ctx
, internalFormat
);
2561 type
= get_temp_image_type(ctx
, format
);
2562 bpp
= _mesa_bytes_per_pixel(format
, type
);
2564 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_image()");
2569 * Alloc image buffer (XXX could use a PBO)
2571 buf
= malloc(width
* height
* bpp
);
2573 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2577 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2580 * Read image from framebuffer (disable pixel transfer ops)
2582 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2583 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2584 format
, type
, &ctx
->Pack
, buf
);
2585 _mesa_meta_end(ctx
);
2587 if (texImage
->Data
) {
2588 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
2591 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2592 postConvWidth
, postConvHeight
, 1,
2593 border
, internalFormat
);
2595 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2596 internalFormat
, GL_NONE
, GL_NONE
);
2599 * Store texture data (with pixel transfer ops)
2601 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2603 _mesa_update_state(ctx
); /* to update pixel transfer state */
2605 if (target
== GL_TEXTURE_1D
) {
2606 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2607 width
, border
, format
, type
,
2608 buf
, &ctx
->Unpack
, texObj
, texImage
);
2611 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2612 width
, height
, border
, format
, type
,
2613 buf
, &ctx
->Unpack
, texObj
, texImage
);
2615 _mesa_meta_end(ctx
);
2617 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2624 _mesa_meta_CopyTexImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2625 GLenum internalFormat
, GLint x
, GLint y
,
2626 GLsizei width
, GLint border
)
2628 copy_tex_image(ctx
, 1, target
, level
, internalFormat
, x
, y
,
2634 _mesa_meta_CopyTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2635 GLenum internalFormat
, GLint x
, GLint y
,
2636 GLsizei width
, GLsizei height
, GLint border
)
2638 copy_tex_image(ctx
, 2, target
, level
, internalFormat
, x
, y
,
2639 width
, height
, border
);
2645 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2646 * Have to be careful with locking and meta state for pixel transfer.
2649 copy_tex_sub_image(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2650 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2652 GLsizei width
, GLsizei height
)
2654 struct gl_texture_object
*texObj
;
2655 struct gl_texture_image
*texImage
;
2656 GLenum format
, type
;
2660 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2661 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2663 format
= _mesa_get_format_base_format(texImage
->TexFormat
);
2664 type
= get_temp_image_type(ctx
, format
);
2665 bpp
= _mesa_bytes_per_pixel(format
, type
);
2667 _mesa_problem(ctx
, "Bad bpp in meta copy_tex_sub_image()");
2672 * Alloc image buffer (XXX could use a PBO)
2674 buf
= malloc(width
* height
* bpp
);
2676 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%uD", dims
);
2680 _mesa_unlock_texture(ctx
, texObj
); /* need to unlock first */
2683 * Read image from framebuffer (disable pixel transfer ops)
2685 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2686 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
2687 format
, type
, &ctx
->Pack
, buf
);
2688 _mesa_meta_end(ctx
);
2690 _mesa_update_state(ctx
); /* to update pixel transfer state */
2693 * Store texture data (with pixel transfer ops)
2695 _mesa_meta_begin(ctx
, META_PIXEL_STORE
);
2696 if (target
== GL_TEXTURE_1D
) {
2697 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
,
2698 width
, format
, type
, buf
,
2699 &ctx
->Unpack
, texObj
, texImage
);
2701 else if (target
== GL_TEXTURE_3D
) {
2702 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
2703 width
, height
, 1, format
, type
, buf
,
2704 &ctx
->Unpack
, texObj
, texImage
);
2707 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2708 width
, height
, format
, type
, buf
,
2709 &ctx
->Unpack
, texObj
, texImage
);
2711 _mesa_meta_end(ctx
);
2713 _mesa_lock_texture(ctx
, texObj
); /* re-lock */
2720 _mesa_meta_CopyTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2722 GLint x
, GLint y
, GLsizei width
)
2724 copy_tex_sub_image(ctx
, 1, target
, level
, xoffset
, 0, 0,
2730 _mesa_meta_CopyTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2731 GLint xoffset
, GLint yoffset
,
2733 GLsizei width
, GLsizei height
)
2735 copy_tex_sub_image(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2736 x
, y
, width
, height
);
2741 _mesa_meta_CopyTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
2742 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2744 GLsizei width
, GLsizei height
)
2746 copy_tex_sub_image(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2747 x
, y
, width
, height
);
2752 _mesa_meta_CopyColorTable(struct gl_context
*ctx
,
2753 GLenum target
, GLenum internalformat
,
2754 GLint x
, GLint y
, GLsizei width
)
2758 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2760 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorTable");
2765 * Read image from framebuffer (disable pixel transfer ops)
2767 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2768 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2769 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2771 _mesa_ColorTable(target
, internalformat
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2773 _mesa_meta_end(ctx
);
2780 _mesa_meta_CopyColorSubTable(struct gl_context
*ctx
,GLenum target
, GLsizei start
,
2781 GLint x
, GLint y
, GLsizei width
)
2785 buf
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
2787 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyColorSubTable");
2792 * Read image from framebuffer (disable pixel transfer ops)
2794 _mesa_meta_begin(ctx
, META_PIXEL_STORE
| META_PIXEL_TRANSFER
);
2795 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, 1,
2796 GL_RGBA
, GL_FLOAT
, &ctx
->Pack
, buf
);
2798 _mesa_ColorSubTable(target
, start
, width
, GL_RGBA
, GL_FLOAT
, buf
);
2800 _mesa_meta_end(ctx
);