Merge branch 'remove-copyteximage-hook'
[mesa.git] / src / mesa / drivers / common / meta.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
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.
29 *
30 * \author Brian Paul
31 */
32
33
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/condrender.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/pbo.h"
53 #include "main/polygon.h"
54 #include "main/readpix.h"
55 #include "main/scissor.h"
56 #include "main/shaderapi.h"
57 #include "main/shaderobj.h"
58 #include "main/state.h"
59 #include "main/stencil.h"
60 #include "main/texobj.h"
61 #include "main/texenv.h"
62 #include "main/teximage.h"
63 #include "main/texparam.h"
64 #include "main/texstate.h"
65 #include "main/uniforms.h"
66 #include "main/varray.h"
67 #include "main/viewport.h"
68 #include "program/program.h"
69 #include "swrast/swrast.h"
70 #include "drivers/common/meta.h"
71
72
73 /** Return offset in bytes of the field within a vertex struct */
74 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
75
76
77 /**
78 * Flags passed to _mesa_meta_begin().
79 */
80 /*@{*/
81 #define META_ALL ~0x0
82 #define META_ALPHA_TEST 0x1
83 #define META_BLEND 0x2 /**< includes logicop */
84 #define META_COLOR_MASK 0x4
85 #define META_DEPTH_TEST 0x8
86 #define META_FOG 0x10
87 #define META_PIXEL_STORE 0x20
88 #define META_PIXEL_TRANSFER 0x40
89 #define META_RASTERIZATION 0x80
90 #define META_SCISSOR 0x100
91 #define META_SHADER 0x200
92 #define META_STENCIL_TEST 0x400
93 #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */
94 #define META_TEXTURE 0x1000
95 #define META_VERTEX 0x2000
96 #define META_VIEWPORT 0x4000
97 #define META_CLAMP_FRAGMENT_COLOR 0x8000
98 #define META_CLAMP_VERTEX_COLOR 0x10000
99 #define META_CONDITIONAL_RENDER 0x20000
100 /*@}*/
101
102
103 /**
104 * State which we may save/restore across meta ops.
105 * XXX this may be incomplete...
106 */
107 struct save_state
108 {
109 GLbitfield SavedState; /**< bitmask of META_* flags */
110
111 /** META_ALPHA_TEST */
112 GLboolean AlphaEnabled;
113 GLenum AlphaFunc;
114 GLclampf AlphaRef;
115
116 /** META_BLEND */
117 GLbitfield BlendEnabled;
118 GLboolean ColorLogicOpEnabled;
119
120 /** META_COLOR_MASK */
121 GLubyte ColorMask[MAX_DRAW_BUFFERS][4];
122
123 /** META_DEPTH_TEST */
124 struct gl_depthbuffer_attrib Depth;
125
126 /** META_FOG */
127 GLboolean Fog;
128
129 /** META_PIXEL_STORE */
130 struct gl_pixelstore_attrib Pack, Unpack;
131
132 /** META_PIXEL_TRANSFER */
133 GLfloat RedBias, RedScale;
134 GLfloat GreenBias, GreenScale;
135 GLfloat BlueBias, BlueScale;
136 GLfloat AlphaBias, AlphaScale;
137 GLfloat DepthBias, DepthScale;
138 GLboolean MapColorFlag;
139
140 /** META_RASTERIZATION */
141 GLenum FrontPolygonMode, BackPolygonMode;
142 GLboolean PolygonOffset;
143 GLboolean PolygonSmooth;
144 GLboolean PolygonStipple;
145 GLboolean PolygonCull;
146
147 /** META_SCISSOR */
148 struct gl_scissor_attrib Scissor;
149
150 /** META_SHADER */
151 GLboolean VertexProgramEnabled;
152 struct gl_vertex_program *VertexProgram;
153 GLboolean FragmentProgramEnabled;
154 struct gl_fragment_program *FragmentProgram;
155 struct gl_shader_program *VertexShader;
156 struct gl_shader_program *GeometryShader;
157 struct gl_shader_program *FragmentShader;
158 struct gl_shader_program *ActiveShader;
159
160 /** META_STENCIL_TEST */
161 struct gl_stencil_attrib Stencil;
162
163 /** META_TRANSFORM */
164 GLenum MatrixMode;
165 GLfloat ModelviewMatrix[16];
166 GLfloat ProjectionMatrix[16];
167 GLfloat TextureMatrix[16];
168 GLbitfield ClipPlanesEnabled;
169
170 /** META_TEXTURE */
171 GLuint ActiveUnit;
172 GLuint ClientActiveUnit;
173 /** for unit[0] only */
174 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
175 /** mask of TEXTURE_2D_BIT, etc */
176 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
177 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
178 GLuint EnvMode; /* unit[0] only */
179
180 /** META_VERTEX */
181 struct gl_array_object *ArrayObj;
182 struct gl_buffer_object *ArrayBufferObj;
183
184 /** META_VIEWPORT */
185 GLint ViewportX, ViewportY, ViewportW, ViewportH;
186 GLclampd DepthNear, DepthFar;
187
188 /** META_CLAMP_FRAGMENT_COLOR */
189 GLenum ClampFragmentColor;
190
191 /** META_CLAMP_VERTEX_COLOR */
192 GLenum ClampVertexColor;
193
194 /** META_CONDITIONAL_RENDER */
195 struct gl_query_object *CondRenderQuery;
196 GLenum CondRenderMode;
197
198 /** Miscellaneous (always disabled) */
199 GLboolean Lighting;
200 };
201
202
203 /**
204 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
205 * This is currently shared by all the meta ops. But we could create a
206 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
207 */
208 struct temp_texture
209 {
210 GLuint TexObj;
211 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
212 GLsizei MinSize; /**< Min texture size to allocate */
213 GLsizei MaxSize; /**< Max possible texture size */
214 GLboolean NPOT; /**< Non-power of two size OK? */
215 GLsizei Width, Height; /**< Current texture size */
216 GLenum IntFormat;
217 GLfloat Sright, Ttop; /**< right, top texcoords */
218 };
219
220
221 /**
222 * State for glBlitFramebufer()
223 */
224 struct blit_state
225 {
226 GLuint ArrayObj;
227 GLuint VBO;
228 GLuint DepthFP;
229 };
230
231
232 /**
233 * State for glClear()
234 */
235 struct clear_state
236 {
237 GLuint ArrayObj;
238 GLuint VBO;
239 GLuint ShaderProg;
240 GLint ColorLocation;
241 };
242
243
244 /**
245 * State for glCopyPixels()
246 */
247 struct copypix_state
248 {
249 GLuint ArrayObj;
250 GLuint VBO;
251 };
252
253
254 /**
255 * State for glDrawPixels()
256 */
257 struct drawpix_state
258 {
259 GLuint ArrayObj;
260
261 GLuint StencilFP; /**< Fragment program for drawing stencil images */
262 GLuint DepthFP; /**< Fragment program for drawing depth images */
263 };
264
265
266 /**
267 * State for glBitmap()
268 */
269 struct bitmap_state
270 {
271 GLuint ArrayObj;
272 GLuint VBO;
273 struct temp_texture Tex; /**< separate texture from other meta ops */
274 };
275
276
277 /**
278 * State for _mesa_meta_generate_mipmap()
279 */
280 struct gen_mipmap_state
281 {
282 GLuint ArrayObj;
283 GLuint VBO;
284 GLuint FBO;
285 };
286
287 #define MAX_META_OPS_DEPTH 2
288 /**
289 * All per-context meta state.
290 */
291 struct gl_meta_state
292 {
293 /** Stack of state saved during meta-ops */
294 struct save_state Save[MAX_META_OPS_DEPTH];
295 /** Save stack depth */
296 GLuint SaveStackDepth;
297
298 struct temp_texture TempTex;
299
300 struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
301 struct clear_state Clear; /**< For _mesa_meta_Clear() */
302 struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
303 struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
304 struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
305 struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
306 };
307
308
309 /**
310 * Initialize meta-ops for a context.
311 * To be called once during context creation.
312 */
313 void
314 _mesa_meta_init(struct gl_context *ctx)
315 {
316 ASSERT(!ctx->Meta);
317
318 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
319 }
320
321
322 /**
323 * Free context meta-op state.
324 * To be called once during context destruction.
325 */
326 void
327 _mesa_meta_free(struct gl_context *ctx)
328 {
329 /* Note: Any textures, VBOs, etc, that we allocate should get
330 * freed by the normal context destruction code. But this would be
331 * the place to free other meta data someday.
332 */
333 free(ctx->Meta);
334 ctx->Meta = NULL;
335 }
336
337
338 /**
339 * Enter meta state. This is like a light-weight version of glPushAttrib
340 * but it also resets most GL state back to default values.
341 *
342 * \param state bitmask of META_* flags indicating which attribute groups
343 * to save and reset to their defaults
344 */
345 static void
346 _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
347 {
348 struct save_state *save;
349
350 /* hope MAX_META_OPS_DEPTH is large enough */
351 assert(ctx->Meta->SaveStackDepth < MAX_META_OPS_DEPTH);
352
353 save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth++];
354 memset(save, 0, sizeof(*save));
355 save->SavedState = state;
356
357 if (state & META_ALPHA_TEST) {
358 save->AlphaEnabled = ctx->Color.AlphaEnabled;
359 save->AlphaFunc = ctx->Color.AlphaFunc;
360 save->AlphaRef = ctx->Color.AlphaRef;
361 if (ctx->Color.AlphaEnabled)
362 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
363 }
364
365 if (state & META_BLEND) {
366 save->BlendEnabled = ctx->Color.BlendEnabled;
367 if (ctx->Color.BlendEnabled) {
368 if (ctx->Extensions.EXT_draw_buffers2) {
369 GLuint i;
370 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
371 _mesa_set_enablei(ctx, GL_BLEND, i, GL_FALSE);
372 }
373 }
374 else {
375 _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
376 }
377 }
378 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
379 if (ctx->Color.ColorLogicOpEnabled)
380 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
381 }
382
383 if (state & META_COLOR_MASK) {
384 memcpy(save->ColorMask, ctx->Color.ColorMask,
385 sizeof(ctx->Color.ColorMask));
386 if (!ctx->Color.ColorMask[0][0] ||
387 !ctx->Color.ColorMask[0][1] ||
388 !ctx->Color.ColorMask[0][2] ||
389 !ctx->Color.ColorMask[0][3])
390 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
391 }
392
393 if (state & META_DEPTH_TEST) {
394 save->Depth = ctx->Depth; /* struct copy */
395 if (ctx->Depth.Test)
396 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
397 }
398
399 if (state & META_FOG) {
400 save->Fog = ctx->Fog.Enabled;
401 if (ctx->Fog.Enabled)
402 _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
403 }
404
405 if (state & META_PIXEL_STORE) {
406 save->Pack = ctx->Pack;
407 save->Unpack = ctx->Unpack;
408 ctx->Pack = ctx->DefaultPacking;
409 ctx->Unpack = ctx->DefaultPacking;
410 }
411
412 if (state & META_PIXEL_TRANSFER) {
413 save->RedScale = ctx->Pixel.RedScale;
414 save->RedBias = ctx->Pixel.RedBias;
415 save->GreenScale = ctx->Pixel.GreenScale;
416 save->GreenBias = ctx->Pixel.GreenBias;
417 save->BlueScale = ctx->Pixel.BlueScale;
418 save->BlueBias = ctx->Pixel.BlueBias;
419 save->AlphaScale = ctx->Pixel.AlphaScale;
420 save->AlphaBias = ctx->Pixel.AlphaBias;
421 save->MapColorFlag = ctx->Pixel.MapColorFlag;
422 ctx->Pixel.RedScale = 1.0F;
423 ctx->Pixel.RedBias = 0.0F;
424 ctx->Pixel.GreenScale = 1.0F;
425 ctx->Pixel.GreenBias = 0.0F;
426 ctx->Pixel.BlueScale = 1.0F;
427 ctx->Pixel.BlueBias = 0.0F;
428 ctx->Pixel.AlphaScale = 1.0F;
429 ctx->Pixel.AlphaBias = 0.0F;
430 ctx->Pixel.MapColorFlag = GL_FALSE;
431 /* XXX more state */
432 ctx->NewState |=_NEW_PIXEL;
433 }
434
435 if (state & META_RASTERIZATION) {
436 save->FrontPolygonMode = ctx->Polygon.FrontMode;
437 save->BackPolygonMode = ctx->Polygon.BackMode;
438 save->PolygonOffset = ctx->Polygon.OffsetFill;
439 save->PolygonSmooth = ctx->Polygon.SmoothFlag;
440 save->PolygonStipple = ctx->Polygon.StippleFlag;
441 save->PolygonCull = ctx->Polygon.CullFlag;
442 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
443 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
444 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
445 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
446 _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
447 }
448
449 if (state & META_SCISSOR) {
450 save->Scissor = ctx->Scissor; /* struct copy */
451 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_FALSE);
452 }
453
454 if (state & META_SHADER) {
455 if (ctx->Extensions.ARB_vertex_program) {
456 save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
457 _mesa_reference_vertprog(ctx, &save->VertexProgram,
458 ctx->VertexProgram.Current);
459 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
460 }
461
462 if (ctx->Extensions.ARB_fragment_program) {
463 save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
464 _mesa_reference_fragprog(ctx, &save->FragmentProgram,
465 ctx->FragmentProgram.Current);
466 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
467 }
468
469 if (ctx->Extensions.ARB_shader_objects) {
470 _mesa_reference_shader_program(ctx, &save->VertexShader,
471 ctx->Shader.CurrentVertexProgram);
472 _mesa_reference_shader_program(ctx, &save->GeometryShader,
473 ctx->Shader.CurrentGeometryProgram);
474 _mesa_reference_shader_program(ctx, &save->FragmentShader,
475 ctx->Shader.CurrentFragmentProgram);
476 _mesa_reference_shader_program(ctx, &save->ActiveShader,
477 ctx->Shader.CurrentFragmentProgram);
478
479 _mesa_UseProgramObjectARB(0);
480 }
481 }
482
483 if (state & META_STENCIL_TEST) {
484 save->Stencil = ctx->Stencil; /* struct copy */
485 if (ctx->Stencil.Enabled)
486 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
487 /* NOTE: other stencil state not reset */
488 }
489
490 if (state & META_TEXTURE) {
491 GLuint u, tgt;
492
493 save->ActiveUnit = ctx->Texture.CurrentUnit;
494 save->ClientActiveUnit = ctx->Array.ActiveTexture;
495 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
496
497 /* Disable all texture units */
498 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
499 save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
500 save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled;
501 if (ctx->Texture.Unit[u].Enabled ||
502 ctx->Texture.Unit[u].TexGenEnabled) {
503 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
504 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
505 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
506 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
507 if (ctx->Extensions.ARB_texture_cube_map)
508 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
509 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
510 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
511 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
512 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
513 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
514 }
515 }
516
517 /* save current texture objects for unit[0] only */
518 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
519 _mesa_reference_texobj(&save->CurrentTexture[tgt],
520 ctx->Texture.Unit[0].CurrentTex[tgt]);
521 }
522
523 /* set defaults for unit[0] */
524 _mesa_ActiveTextureARB(GL_TEXTURE0);
525 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
526 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
527 }
528
529 if (state & META_TRANSFORM) {
530 GLuint activeTexture = ctx->Texture.CurrentUnit;
531 memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
532 16 * sizeof(GLfloat));
533 memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
534 16 * sizeof(GLfloat));
535 memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
536 16 * sizeof(GLfloat));
537 save->MatrixMode = ctx->Transform.MatrixMode;
538 /* set 1:1 vertex:pixel coordinate transform */
539 _mesa_ActiveTextureARB(GL_TEXTURE0);
540 _mesa_MatrixMode(GL_TEXTURE);
541 _mesa_LoadIdentity();
542 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
543 _mesa_MatrixMode(GL_MODELVIEW);
544 _mesa_LoadIdentity();
545 _mesa_MatrixMode(GL_PROJECTION);
546 _mesa_LoadIdentity();
547 _mesa_Ortho(0.0, ctx->DrawBuffer->Width,
548 0.0, ctx->DrawBuffer->Height,
549 -1.0, 1.0);
550 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
551 if (ctx->Transform.ClipPlanesEnabled) {
552 GLuint i;
553 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
554 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
555 }
556 }
557 }
558
559 if (state & META_VERTEX) {
560 /* save vertex array object state */
561 _mesa_reference_array_object(ctx, &save->ArrayObj,
562 ctx->Array.ArrayObj);
563 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
564 ctx->Array.ArrayBufferObj);
565 /* set some default state? */
566 }
567
568 if (state & META_VIEWPORT) {
569 /* save viewport state */
570 save->ViewportX = ctx->Viewport.X;
571 save->ViewportY = ctx->Viewport.Y;
572 save->ViewportW = ctx->Viewport.Width;
573 save->ViewportH = ctx->Viewport.Height;
574 /* set viewport to match window size */
575 if (ctx->Viewport.X != 0 ||
576 ctx->Viewport.Y != 0 ||
577 ctx->Viewport.Width != ctx->DrawBuffer->Width ||
578 ctx->Viewport.Height != ctx->DrawBuffer->Height) {
579 _mesa_set_viewport(ctx, 0, 0,
580 ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
581 }
582 /* save depth range state */
583 save->DepthNear = ctx->Viewport.Near;
584 save->DepthFar = ctx->Viewport.Far;
585 /* set depth range to default */
586 _mesa_DepthRange(0.0, 1.0);
587 }
588
589 if (state & META_CLAMP_FRAGMENT_COLOR) {
590 save->ClampFragmentColor = ctx->Color.ClampFragmentColor;
591
592 /* Generally in here we want to do clamping according to whether
593 * it's for the pixel path (ClampFragmentColor is GL_TRUE),
594 * regardless of the internal implementation of the metaops.
595 */
596 if (ctx->Color.ClampFragmentColor != GL_TRUE)
597 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
598 }
599
600 if (state & META_CLAMP_VERTEX_COLOR) {
601 save->ClampVertexColor = ctx->Light.ClampVertexColor;
602
603 /* Generally in here we never want vertex color clamping --
604 * result clamping is only dependent on fragment clamping.
605 */
606 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
607 }
608
609 if (state & META_CONDITIONAL_RENDER) {
610 save->CondRenderQuery = ctx->Query.CondRenderQuery;
611 save->CondRenderMode = ctx->Query.CondRenderMode;
612
613 if (ctx->Query.CondRenderQuery)
614 _mesa_EndConditionalRender();
615 }
616
617 /* misc */
618 {
619 save->Lighting = ctx->Light.Enabled;
620 if (ctx->Light.Enabled)
621 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
622 }
623 }
624
625
626 /**
627 * Leave meta state. This is like a light-weight version of glPopAttrib().
628 */
629 static void
630 _mesa_meta_end(struct gl_context *ctx)
631 {
632 struct save_state *save = &ctx->Meta->Save[--ctx->Meta->SaveStackDepth];
633 const GLbitfield state = save->SavedState;
634
635 if (state & META_ALPHA_TEST) {
636 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
637 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
638 _mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
639 }
640
641 if (state & META_BLEND) {
642 if (ctx->Color.BlendEnabled != save->BlendEnabled) {
643 if (ctx->Extensions.EXT_draw_buffers2) {
644 GLuint i;
645 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
646 _mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
647 }
648 }
649 else {
650 _mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
651 }
652 }
653 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
654 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
655 }
656
657 if (state & META_COLOR_MASK) {
658 GLuint i;
659 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
660 if (!TEST_EQ_4V(ctx->Color.ColorMask[i], save->ColorMask[i])) {
661 if (i == 0) {
662 _mesa_ColorMask(save->ColorMask[i][0], save->ColorMask[i][1],
663 save->ColorMask[i][2], save->ColorMask[i][3]);
664 }
665 else {
666 _mesa_ColorMaskIndexed(i,
667 save->ColorMask[i][0],
668 save->ColorMask[i][1],
669 save->ColorMask[i][2],
670 save->ColorMask[i][3]);
671 }
672 }
673 }
674 }
675
676 if (state & META_DEPTH_TEST) {
677 if (ctx->Depth.Test != save->Depth.Test)
678 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
679 _mesa_DepthFunc(save->Depth.Func);
680 _mesa_DepthMask(save->Depth.Mask);
681 }
682
683 if (state & META_FOG) {
684 _mesa_set_enable(ctx, GL_FOG, save->Fog);
685 }
686
687 if (state & META_PIXEL_STORE) {
688 ctx->Pack = save->Pack;
689 ctx->Unpack = save->Unpack;
690 }
691
692 if (state & META_PIXEL_TRANSFER) {
693 ctx->Pixel.RedScale = save->RedScale;
694 ctx->Pixel.RedBias = save->RedBias;
695 ctx->Pixel.GreenScale = save->GreenScale;
696 ctx->Pixel.GreenBias = save->GreenBias;
697 ctx->Pixel.BlueScale = save->BlueScale;
698 ctx->Pixel.BlueBias = save->BlueBias;
699 ctx->Pixel.AlphaScale = save->AlphaScale;
700 ctx->Pixel.AlphaBias = save->AlphaBias;
701 ctx->Pixel.MapColorFlag = save->MapColorFlag;
702 /* XXX more state */
703 ctx->NewState |=_NEW_PIXEL;
704 }
705
706 if (state & META_RASTERIZATION) {
707 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
708 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
709 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
710 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
711 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
712 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
713 }
714
715 if (state & META_SCISSOR) {
716 _mesa_set_enable(ctx, GL_SCISSOR_TEST, save->Scissor.Enabled);
717 _mesa_Scissor(save->Scissor.X, save->Scissor.Y,
718 save->Scissor.Width, save->Scissor.Height);
719 }
720
721 if (state & META_SHADER) {
722 if (ctx->Extensions.ARB_vertex_program) {
723 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
724 save->VertexProgramEnabled);
725 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
726 save->VertexProgram);
727 _mesa_reference_vertprog(ctx, &save->VertexProgram, NULL);
728 }
729
730 if (ctx->Extensions.ARB_fragment_program) {
731 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
732 save->FragmentProgramEnabled);
733 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
734 save->FragmentProgram);
735 _mesa_reference_fragprog(ctx, &save->FragmentProgram, NULL);
736 }
737
738 if (ctx->Extensions.ARB_vertex_shader)
739 _mesa_use_shader_program(ctx, GL_VERTEX_SHADER, save->VertexShader);
740
741 if (ctx->Extensions.ARB_geometry_shader4)
742 _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB,
743 save->GeometryShader);
744
745 if (ctx->Extensions.ARB_fragment_shader)
746 _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER,
747 save->FragmentShader);
748
749 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram,
750 save->ActiveShader);
751 }
752
753 if (state & META_STENCIL_TEST) {
754 const struct gl_stencil_attrib *stencil = &save->Stencil;
755
756 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
757 _mesa_ClearStencil(stencil->Clear);
758 if (ctx->Extensions.EXT_stencil_two_side) {
759 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
760 stencil->TestTwoSide);
761 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
762 ? GL_BACK : GL_FRONT);
763 }
764 /* front state */
765 _mesa_StencilFuncSeparate(GL_FRONT,
766 stencil->Function[0],
767 stencil->Ref[0],
768 stencil->ValueMask[0]);
769 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
770 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
771 stencil->ZFailFunc[0],
772 stencil->ZPassFunc[0]);
773 /* back state */
774 _mesa_StencilFuncSeparate(GL_BACK,
775 stencil->Function[1],
776 stencil->Ref[1],
777 stencil->ValueMask[1]);
778 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
779 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
780 stencil->ZFailFunc[1],
781 stencil->ZPassFunc[1]);
782 }
783
784 if (state & META_TEXTURE) {
785 GLuint u, tgt;
786
787 ASSERT(ctx->Texture.CurrentUnit == 0);
788
789 /* restore texenv for unit[0] */
790 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
791
792 /* restore texture objects for unit[0] only */
793 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
794 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
795 save->CurrentTexture[tgt]);
796 _mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
797 }
798
799 /* Re-enable textures, texgen */
800 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
801 if (save->TexEnabled[u]) {
802 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
803
804 if (save->TexEnabled[u] & TEXTURE_1D_BIT)
805 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_TRUE);
806 if (save->TexEnabled[u] & TEXTURE_2D_BIT)
807 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_TRUE);
808 if (save->TexEnabled[u] & TEXTURE_3D_BIT)
809 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_TRUE);
810 if (save->TexEnabled[u] & TEXTURE_CUBE_BIT)
811 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_TRUE);
812 if (save->TexEnabled[u] & TEXTURE_RECT_BIT)
813 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_TRUE);
814 }
815
816 if (save->TexGenEnabled[u]) {
817 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
818
819 if (save->TexGenEnabled[u] & S_BIT)
820 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_TRUE);
821 if (save->TexGenEnabled[u] & T_BIT)
822 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_TRUE);
823 if (save->TexGenEnabled[u] & R_BIT)
824 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_TRUE);
825 if (save->TexGenEnabled[u] & Q_BIT)
826 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
827 }
828 }
829
830 /* restore current unit state */
831 _mesa_ActiveTextureARB(GL_TEXTURE0 + save->ActiveUnit);
832 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + save->ClientActiveUnit);
833 }
834
835 if (state & META_TRANSFORM) {
836 GLuint activeTexture = ctx->Texture.CurrentUnit;
837 _mesa_ActiveTextureARB(GL_TEXTURE0);
838 _mesa_MatrixMode(GL_TEXTURE);
839 _mesa_LoadMatrixf(save->TextureMatrix);
840 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
841
842 _mesa_MatrixMode(GL_MODELVIEW);
843 _mesa_LoadMatrixf(save->ModelviewMatrix);
844
845 _mesa_MatrixMode(GL_PROJECTION);
846 _mesa_LoadMatrixf(save->ProjectionMatrix);
847
848 _mesa_MatrixMode(save->MatrixMode);
849
850 if (save->ClipPlanesEnabled) {
851 GLuint i;
852 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
853 if (save->ClipPlanesEnabled & (1 << i)) {
854 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
855 }
856 }
857 }
858 }
859
860 if (state & META_VERTEX) {
861 /* restore vertex buffer object */
862 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
863 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
864
865 /* restore vertex array object */
866 _mesa_BindVertexArray(save->ArrayObj->Name);
867 _mesa_reference_array_object(ctx, &save->ArrayObj, NULL);
868 }
869
870 if (state & META_VIEWPORT) {
871 if (save->ViewportX != ctx->Viewport.X ||
872 save->ViewportY != ctx->Viewport.Y ||
873 save->ViewportW != ctx->Viewport.Width ||
874 save->ViewportH != ctx->Viewport.Height) {
875 _mesa_set_viewport(ctx, save->ViewportX, save->ViewportY,
876 save->ViewportW, save->ViewportH);
877 }
878 _mesa_DepthRange(save->DepthNear, save->DepthFar);
879 }
880
881 if (state & META_CLAMP_FRAGMENT_COLOR) {
882 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
883 }
884
885 if (state & META_CLAMP_VERTEX_COLOR) {
886 _mesa_ClampColorARB(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
887 }
888
889 if (state & META_CONDITIONAL_RENDER) {
890 if (save->CondRenderQuery)
891 _mesa_BeginConditionalRender(save->CondRenderQuery->Id,
892 save->CondRenderMode);
893 }
894
895 /* misc */
896 if (save->Lighting) {
897 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
898 }
899 }
900
901
902 /**
903 * Convert Z from a normalized value in the range [0, 1] to an object-space
904 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
905 * default/identity ortho projection results in the original Z value.
906 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
907 * value comes from the clear value or raster position.
908 */
909 static INLINE GLfloat
910 invert_z(GLfloat normZ)
911 {
912 GLfloat objZ = 1.0 - 2.0 * normZ;
913 return objZ;
914 }
915
916
917 /**
918 * One-time init for a temp_texture object.
919 * Choose tex target, compute max tex size, etc.
920 */
921 static void
922 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
923 {
924 /* prefer texture rectangle */
925 if (ctx->Extensions.NV_texture_rectangle) {
926 tex->Target = GL_TEXTURE_RECTANGLE;
927 tex->MaxSize = ctx->Const.MaxTextureRectSize;
928 tex->NPOT = GL_TRUE;
929 }
930 else {
931 /* use 2D texture, NPOT if possible */
932 tex->Target = GL_TEXTURE_2D;
933 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
934 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
935 }
936 tex->MinSize = 16; /* 16 x 16 at least */
937 assert(tex->MaxSize > 0);
938
939 _mesa_GenTextures(1, &tex->TexObj);
940 }
941
942
943 /**
944 * Return pointer to temp_texture info for non-bitmap ops.
945 * This does some one-time init if needed.
946 */
947 static struct temp_texture *
948 get_temp_texture(struct gl_context *ctx)
949 {
950 struct temp_texture *tex = &ctx->Meta->TempTex;
951
952 if (!tex->TexObj) {
953 init_temp_texture(ctx, tex);
954 }
955
956 return tex;
957 }
958
959
960 /**
961 * Return pointer to temp_texture info for _mesa_meta_bitmap().
962 * We use a separate texture for bitmaps to reduce texture
963 * allocation/deallocation.
964 */
965 static struct temp_texture *
966 get_bitmap_temp_texture(struct gl_context *ctx)
967 {
968 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
969
970 if (!tex->TexObj) {
971 init_temp_texture(ctx, tex);
972 }
973
974 return tex;
975 }
976
977
978 /**
979 * Compute the width/height of texture needed to draw an image of the
980 * given size. Return a flag indicating whether the current texture
981 * can be re-used (glTexSubImage2D) or if a new texture needs to be
982 * allocated (glTexImage2D).
983 * Also, compute s/t texcoords for drawing.
984 *
985 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
986 */
987 static GLboolean
988 alloc_texture(struct temp_texture *tex,
989 GLsizei width, GLsizei height, GLenum intFormat)
990 {
991 GLboolean newTex = GL_FALSE;
992
993 ASSERT(width <= tex->MaxSize);
994 ASSERT(height <= tex->MaxSize);
995
996 if (width > tex->Width ||
997 height > tex->Height ||
998 intFormat != tex->IntFormat) {
999 /* alloc new texture (larger or different format) */
1000
1001 if (tex->NPOT) {
1002 /* use non-power of two size */
1003 tex->Width = MAX2(tex->MinSize, width);
1004 tex->Height = MAX2(tex->MinSize, height);
1005 }
1006 else {
1007 /* find power of two size */
1008 GLsizei w, h;
1009 w = h = tex->MinSize;
1010 while (w < width)
1011 w *= 2;
1012 while (h < height)
1013 h *= 2;
1014 tex->Width = w;
1015 tex->Height = h;
1016 }
1017
1018 tex->IntFormat = intFormat;
1019
1020 newTex = GL_TRUE;
1021 }
1022
1023 /* compute texcoords */
1024 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1025 tex->Sright = (GLfloat) width;
1026 tex->Ttop = (GLfloat) height;
1027 }
1028 else {
1029 tex->Sright = (GLfloat) width / tex->Width;
1030 tex->Ttop = (GLfloat) height / tex->Height;
1031 }
1032
1033 return newTex;
1034 }
1035
1036
1037 /**
1038 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1039 */
1040 static void
1041 setup_copypix_texture(struct temp_texture *tex,
1042 GLboolean newTex,
1043 GLint srcX, GLint srcY,
1044 GLsizei width, GLsizei height, GLenum intFormat,
1045 GLenum filter)
1046 {
1047 _mesa_BindTexture(tex->Target, tex->TexObj);
1048 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
1049 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
1050 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1051
1052 /* copy framebuffer image to texture */
1053 if (newTex) {
1054 /* create new tex image */
1055 if (tex->Width == width && tex->Height == height) {
1056 /* create new tex with framebuffer data */
1057 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
1058 srcX, srcY, width, height, 0);
1059 }
1060 else {
1061 /* create empty texture */
1062 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1063 tex->Width, tex->Height, 0,
1064 intFormat, GL_UNSIGNED_BYTE, NULL);
1065 /* load image */
1066 _mesa_CopyTexSubImage2D(tex->Target, 0,
1067 0, 0, srcX, srcY, width, height);
1068 }
1069 }
1070 else {
1071 /* replace existing tex image */
1072 _mesa_CopyTexSubImage2D(tex->Target, 0,
1073 0, 0, srcX, srcY, width, height);
1074 }
1075 }
1076
1077
1078 /**
1079 * Setup/load texture for glDrawPixels.
1080 */
1081 static void
1082 setup_drawpix_texture(struct gl_context *ctx,
1083 struct temp_texture *tex,
1084 GLboolean newTex,
1085 GLenum texIntFormat,
1086 GLsizei width, GLsizei height,
1087 GLenum format, GLenum type,
1088 const GLvoid *pixels)
1089 {
1090 _mesa_BindTexture(tex->Target, tex->TexObj);
1091 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1092 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1093 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1094
1095 /* copy pixel data to texture */
1096 if (newTex) {
1097 /* create new tex image */
1098 if (tex->Width == width && tex->Height == height) {
1099 /* create new tex and load image data */
1100 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1101 tex->Width, tex->Height, 0, format, type, pixels);
1102 }
1103 else {
1104 struct gl_buffer_object *save_unpack_obj = NULL;
1105
1106 _mesa_reference_buffer_object(ctx, &save_unpack_obj,
1107 ctx->Unpack.BufferObj);
1108 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1109 /* create empty texture */
1110 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1111 tex->Width, tex->Height, 0, format, type, NULL);
1112 if (save_unpack_obj != NULL)
1113 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB,
1114 save_unpack_obj->Name);
1115 /* load image */
1116 _mesa_TexSubImage2D(tex->Target, 0,
1117 0, 0, width, height, format, type, pixels);
1118 }
1119 }
1120 else {
1121 /* replace existing tex image */
1122 _mesa_TexSubImage2D(tex->Target, 0,
1123 0, 0, width, height, format, type, pixels);
1124 }
1125 }
1126
1127
1128
1129 /**
1130 * One-time init for drawing depth pixels.
1131 */
1132 static void
1133 init_blit_depth_pixels(struct gl_context *ctx)
1134 {
1135 static const char *program =
1136 "!!ARBfp1.0\n"
1137 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1138 "END \n";
1139 char program2[200];
1140 struct blit_state *blit = &ctx->Meta->Blit;
1141 struct temp_texture *tex = get_temp_texture(ctx);
1142 const char *texTarget;
1143
1144 assert(blit->DepthFP == 0);
1145
1146 /* replace %s with "RECT" or "2D" */
1147 assert(strlen(program) + 4 < sizeof(program2));
1148 if (tex->Target == GL_TEXTURE_RECTANGLE)
1149 texTarget = "RECT";
1150 else
1151 texTarget = "2D";
1152 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1153
1154 _mesa_GenPrograms(1, &blit->DepthFP);
1155 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1156 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1157 strlen(program2), (const GLubyte *) program2);
1158 }
1159
1160
1161 /**
1162 * Try to do a glBlitFramebuffer using no-copy texturing.
1163 * We can do this when the src renderbuffer is actually a texture.
1164 * But if the src buffer == dst buffer we cannot do this.
1165 *
1166 * \return new buffer mask indicating the buffers left to blit using the
1167 * normal path.
1168 */
1169 static GLbitfield
1170 blitframebuffer_texture(struct gl_context *ctx,
1171 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1172 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1173 GLbitfield mask, GLenum filter)
1174 {
1175 if (mask & GL_COLOR_BUFFER_BIT) {
1176 const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
1177 const struct gl_framebuffer *readFb = ctx->ReadBuffer;
1178 const struct gl_renderbuffer_attachment *drawAtt =
1179 &drawFb->Attachment[drawFb->_ColorDrawBufferIndexes[0]];
1180 const struct gl_renderbuffer_attachment *readAtt =
1181 &readFb->Attachment[readFb->_ColorReadBufferIndex];
1182
1183 if (readAtt && readAtt->Texture) {
1184 const struct gl_texture_object *texObj = readAtt->Texture;
1185 const GLuint srcLevel = readAtt->TextureLevel;
1186 const GLenum minFilterSave = texObj->Sampler.MinFilter;
1187 const GLenum magFilterSave = texObj->Sampler.MagFilter;
1188 const GLint baseLevelSave = texObj->BaseLevel;
1189 const GLint maxLevelSave = texObj->MaxLevel;
1190 const GLenum wrapSSave = texObj->Sampler.WrapS;
1191 const GLenum wrapTSave = texObj->Sampler.WrapT;
1192 const GLenum srgbSave = texObj->Sampler.sRGBDecode;
1193 const GLenum fbo_srgb_save = ctx->Color.sRGBEnabled;
1194 const GLenum target = texObj->Target;
1195
1196 if (drawAtt->Texture == readAtt->Texture) {
1197 /* Can't use same texture as both the source and dest. We need
1198 * to handle overlapping blits and besides, some hw may not
1199 * support this.
1200 */
1201 return mask;
1202 }
1203
1204 if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_ARB) {
1205 /* Can't handle other texture types at this time */
1206 return mask;
1207 }
1208
1209 /*
1210 printf("Blit from texture!\n");
1211 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1212 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1213 */
1214
1215 /* Prepare src texture state */
1216 _mesa_BindTexture(target, texObj->Name);
1217 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
1218 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
1219 if (target != GL_TEXTURE_RECTANGLE_ARB) {
1220 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
1221 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
1222 }
1223 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1224 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1225
1226 /* Always do our blits with no sRGB decode or encode.*/
1227 if (ctx->Extensions.EXT_texture_sRGB_decode) {
1228 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1229 GL_SKIP_DECODE_EXT);
1230 }
1231 _mesa_Disable(GL_FRAMEBUFFER_SRGB_EXT);
1232
1233 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1234 _mesa_set_enable(ctx, target, GL_TRUE);
1235
1236 /* Prepare vertex data (the VBO was previously created and bound) */
1237 {
1238 struct vertex {
1239 GLfloat x, y, s, t;
1240 };
1241 struct vertex verts[4];
1242 GLfloat s0, t0, s1, t1;
1243
1244 if (target == GL_TEXTURE_2D) {
1245 const struct gl_texture_image *texImage
1246 = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
1247 s0 = srcX0 / (float) texImage->Width;
1248 s1 = srcX1 / (float) texImage->Width;
1249 t0 = srcY0 / (float) texImage->Height;
1250 t1 = srcY1 / (float) texImage->Height;
1251 }
1252 else {
1253 assert(target == GL_TEXTURE_RECTANGLE_ARB);
1254 s0 = srcX0;
1255 s1 = srcX1;
1256 t0 = srcY0;
1257 t1 = srcY1;
1258 }
1259
1260 verts[0].x = (GLfloat) dstX0;
1261 verts[0].y = (GLfloat) dstY0;
1262 verts[1].x = (GLfloat) dstX1;
1263 verts[1].y = (GLfloat) dstY0;
1264 verts[2].x = (GLfloat) dstX1;
1265 verts[2].y = (GLfloat) dstY1;
1266 verts[3].x = (GLfloat) dstX0;
1267 verts[3].y = (GLfloat) dstY1;
1268
1269 verts[0].s = s0;
1270 verts[0].t = t0;
1271 verts[1].s = s1;
1272 verts[1].t = t0;
1273 verts[2].s = s1;
1274 verts[2].t = t1;
1275 verts[3].s = s0;
1276 verts[3].t = t1;
1277
1278 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1279 }
1280
1281 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1282
1283 /* Restore texture object state, the texture binding will
1284 * be restored by _mesa_meta_end().
1285 */
1286 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
1287 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
1288 if (target != GL_TEXTURE_RECTANGLE_ARB) {
1289 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
1290 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
1291 }
1292 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
1293 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
1294 if (ctx->Extensions.EXT_texture_sRGB_decode) {
1295 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, srgbSave);
1296 }
1297 if (ctx->Extensions.EXT_texture_sRGB_decode && fbo_srgb_save) {
1298 _mesa_Enable(GL_FRAMEBUFFER_SRGB_EXT);
1299 }
1300
1301 /* Done with color buffer */
1302 mask &= ~GL_COLOR_BUFFER_BIT;
1303 }
1304 }
1305
1306 return mask;
1307 }
1308
1309
1310 /**
1311 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1312 * of texture mapping and polygon rendering.
1313 */
1314 void
1315 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
1316 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1317 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1318 GLbitfield mask, GLenum filter)
1319 {
1320 struct blit_state *blit = &ctx->Meta->Blit;
1321 struct temp_texture *tex = get_temp_texture(ctx);
1322 const GLsizei maxTexSize = tex->MaxSize;
1323 const GLint srcX = MIN2(srcX0, srcX1);
1324 const GLint srcY = MIN2(srcY0, srcY1);
1325 const GLint srcW = abs(srcX1 - srcX0);
1326 const GLint srcH = abs(srcY1 - srcY0);
1327 const GLboolean srcFlipX = srcX1 < srcX0;
1328 const GLboolean srcFlipY = srcY1 < srcY0;
1329 struct vertex {
1330 GLfloat x, y, s, t;
1331 };
1332 struct vertex verts[4];
1333 GLboolean newTex;
1334
1335 if (srcW > maxTexSize || srcH > maxTexSize) {
1336 /* XXX avoid this fallback */
1337 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1338 dstX0, dstY0, dstX1, dstY1, mask, filter);
1339 return;
1340 }
1341
1342 if (srcFlipX) {
1343 GLint tmp = dstX0;
1344 dstX0 = dstX1;
1345 dstX1 = tmp;
1346 }
1347
1348 if (srcFlipY) {
1349 GLint tmp = dstY0;
1350 dstY0 = dstY1;
1351 dstY1 = tmp;
1352 }
1353
1354 /* only scissor effects blit so save/clear all other relevant state */
1355 _mesa_meta_begin(ctx, ~META_SCISSOR);
1356
1357 if (blit->ArrayObj == 0) {
1358 /* one-time setup */
1359
1360 /* create vertex array object */
1361 _mesa_GenVertexArrays(1, &blit->ArrayObj);
1362 _mesa_BindVertexArray(blit->ArrayObj);
1363
1364 /* create vertex array buffer */
1365 _mesa_GenBuffersARB(1, &blit->VBO);
1366 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1367 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1368 NULL, GL_DYNAMIC_DRAW_ARB);
1369
1370 /* setup vertex arrays */
1371 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1372 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1373 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1374 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1375 }
1376 else {
1377 _mesa_BindVertexArray(blit->ArrayObj);
1378 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1379 }
1380
1381 /* Try faster, direct texture approach first */
1382 mask = blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
1383 dstX0, dstY0, dstX1, dstY1, mask, filter);
1384 if (mask == 0x0) {
1385 _mesa_meta_end(ctx);
1386 return;
1387 }
1388
1389 /* Continue with "normal" approach which involves copying the src rect
1390 * into a temporary texture and is "blitted" by drawing a textured quad.
1391 */
1392
1393 newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
1394
1395 /* vertex positions/texcoords (after texture allocation!) */
1396 {
1397 verts[0].x = (GLfloat) dstX0;
1398 verts[0].y = (GLfloat) dstY0;
1399 verts[1].x = (GLfloat) dstX1;
1400 verts[1].y = (GLfloat) dstY0;
1401 verts[2].x = (GLfloat) dstX1;
1402 verts[2].y = (GLfloat) dstY1;
1403 verts[3].x = (GLfloat) dstX0;
1404 verts[3].y = (GLfloat) dstY1;
1405
1406 verts[0].s = 0.0F;
1407 verts[0].t = 0.0F;
1408 verts[1].s = tex->Sright;
1409 verts[1].t = 0.0F;
1410 verts[2].s = tex->Sright;
1411 verts[2].t = tex->Ttop;
1412 verts[3].s = 0.0F;
1413 verts[3].t = tex->Ttop;
1414
1415 /* upload new vertex data */
1416 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1417 }
1418
1419 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1420
1421 if (mask & GL_COLOR_BUFFER_BIT) {
1422 setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
1423 GL_RGBA, filter);
1424 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1425 mask &= ~GL_COLOR_BUFFER_BIT;
1426 }
1427
1428 if (mask & GL_DEPTH_BUFFER_BIT) {
1429 GLuint *tmp = (GLuint *) malloc(srcW * srcH * sizeof(GLuint));
1430 if (tmp) {
1431 if (!blit->DepthFP)
1432 init_blit_depth_pixels(ctx);
1433
1434 /* maybe change tex format here */
1435 newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
1436
1437 _mesa_ReadPixels(srcX, srcY, srcW, srcH,
1438 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1439
1440 setup_drawpix_texture(ctx, tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
1441 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1442
1443 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1444 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1445 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1446 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1447 _mesa_DepthFunc(GL_ALWAYS);
1448 _mesa_DepthMask(GL_TRUE);
1449
1450 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1451 mask &= ~GL_DEPTH_BUFFER_BIT;
1452
1453 free(tmp);
1454 }
1455 }
1456
1457 if (mask & GL_STENCIL_BUFFER_BIT) {
1458 /* XXX can't easily do stencil */
1459 }
1460
1461 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1462
1463 _mesa_meta_end(ctx);
1464
1465 if (mask) {
1466 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1467 dstX0, dstY0, dstX1, dstY1, mask, filter);
1468 }
1469 }
1470
1471
1472 /**
1473 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1474 */
1475 void
1476 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1477 {
1478 struct clear_state *clear = &ctx->Meta->Clear;
1479 struct vertex {
1480 GLfloat x, y, z, r, g, b, a;
1481 };
1482 struct vertex verts[4];
1483 /* save all state but scissor, pixel pack/unpack */
1484 GLbitfield metaSave = (META_ALL -
1485 META_SCISSOR -
1486 META_PIXEL_STORE -
1487 META_CONDITIONAL_RENDER);
1488 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1489
1490 if (buffers & BUFFER_BITS_COLOR) {
1491 /* if clearing color buffers, don't save/restore colormask */
1492 metaSave -= META_COLOR_MASK;
1493 }
1494
1495 _mesa_meta_begin(ctx, metaSave);
1496
1497 if (clear->ArrayObj == 0) {
1498 /* one-time setup */
1499
1500 /* create vertex array object */
1501 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1502 _mesa_BindVertexArray(clear->ArrayObj);
1503
1504 /* create vertex array buffer */
1505 _mesa_GenBuffersARB(1, &clear->VBO);
1506 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1507
1508 /* setup vertex arrays */
1509 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1510 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
1511 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1512 _mesa_EnableClientState(GL_COLOR_ARRAY);
1513 }
1514 else {
1515 _mesa_BindVertexArray(clear->ArrayObj);
1516 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1517 }
1518
1519 /* GL_COLOR_BUFFER_BIT */
1520 if (buffers & BUFFER_BITS_COLOR) {
1521 /* leave colormask, glDrawBuffer state as-is */
1522
1523 /* Clears never have the color clamped. */
1524 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1525 }
1526 else {
1527 ASSERT(metaSave & META_COLOR_MASK);
1528 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1529 }
1530
1531 /* GL_DEPTH_BUFFER_BIT */
1532 if (buffers & BUFFER_BIT_DEPTH) {
1533 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1534 _mesa_DepthFunc(GL_ALWAYS);
1535 _mesa_DepthMask(GL_TRUE);
1536 }
1537 else {
1538 assert(!ctx->Depth.Test);
1539 }
1540
1541 /* GL_STENCIL_BUFFER_BIT */
1542 if (buffers & BUFFER_BIT_STENCIL) {
1543 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1544 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1545 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1546 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1547 ctx->Stencil.Clear & stencilMax,
1548 ctx->Stencil.WriteMask[0]);
1549 }
1550 else {
1551 assert(!ctx->Stencil.Enabled);
1552 }
1553
1554 /* vertex positions/colors */
1555 {
1556 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1557 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1558 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1559 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1560 const GLfloat z = invert_z(ctx->Depth.Clear);
1561 GLuint i;
1562
1563 verts[0].x = x0;
1564 verts[0].y = y0;
1565 verts[0].z = z;
1566 verts[1].x = x1;
1567 verts[1].y = y0;
1568 verts[1].z = z;
1569 verts[2].x = x1;
1570 verts[2].y = y1;
1571 verts[2].z = z;
1572 verts[3].x = x0;
1573 verts[3].y = y1;
1574 verts[3].z = z;
1575
1576 /* vertex colors */
1577 for (i = 0; i < 4; i++) {
1578 verts[i].r = ctx->Color.ClearColorUnclamped[0];
1579 verts[i].g = ctx->Color.ClearColorUnclamped[1];
1580 verts[i].b = ctx->Color.ClearColorUnclamped[2];
1581 verts[i].a = ctx->Color.ClearColorUnclamped[3];
1582 }
1583
1584 /* upload new vertex data */
1585 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1586 GL_DYNAMIC_DRAW_ARB);
1587 }
1588
1589 /* draw quad */
1590 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1591
1592 _mesa_meta_end(ctx);
1593 }
1594
1595 static void
1596 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1597 {
1598 const char *vs_source =
1599 "attribute vec4 position;\n"
1600 "void main()\n"
1601 "{\n"
1602 " gl_Position = position;\n"
1603 "}\n";
1604 const char *fs_source =
1605 "uniform vec4 color;\n"
1606 "void main()\n"
1607 "{\n"
1608 " gl_FragColor = color;\n"
1609 "}\n";
1610 GLuint vs, fs;
1611
1612 if (clear->ArrayObj != 0)
1613 return;
1614
1615 /* create vertex array object */
1616 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1617 _mesa_BindVertexArray(clear->ArrayObj);
1618
1619 /* create vertex array buffer */
1620 _mesa_GenBuffersARB(1, &clear->VBO);
1621 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1622
1623 /* setup vertex arrays */
1624 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
1625 _mesa_EnableVertexAttribArrayARB(0);
1626
1627 vs = _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER);
1628 _mesa_ShaderSourceARB(vs, 1, &vs_source, NULL);
1629 _mesa_CompileShaderARB(vs);
1630
1631 fs = _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER);
1632 _mesa_ShaderSourceARB(fs, 1, &fs_source, NULL);
1633 _mesa_CompileShaderARB(fs);
1634
1635 clear->ShaderProg = _mesa_CreateProgramObjectARB();
1636 _mesa_AttachShader(clear->ShaderProg, fs);
1637 _mesa_AttachShader(clear->ShaderProg, vs);
1638 _mesa_BindAttribLocationARB(clear->ShaderProg, 0, "position");
1639 _mesa_LinkProgramARB(clear->ShaderProg);
1640
1641 clear->ColorLocation = _mesa_GetUniformLocationARB(clear->ShaderProg,
1642 "color");
1643 }
1644
1645 /**
1646 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1647 */
1648 void
1649 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1650 {
1651 struct clear_state *clear = &ctx->Meta->Clear;
1652 GLbitfield metaSave;
1653 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1654 struct gl_framebuffer *fb = ctx->DrawBuffer;
1655 const float x0 = ((float)fb->_Xmin / fb->Width) * 2.0f - 1.0f;
1656 const float y0 = ((float)fb->_Ymin / fb->Height) * 2.0f - 1.0f;
1657 const float x1 = ((float)fb->_Xmax / fb->Width) * 2.0f - 1.0f;
1658 const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
1659 const float z = -invert_z(ctx->Depth.Clear);
1660 struct vertex {
1661 GLfloat x, y, z;
1662 } verts[4];
1663
1664 metaSave = (META_ALPHA_TEST |
1665 META_BLEND |
1666 META_DEPTH_TEST |
1667 META_RASTERIZATION |
1668 META_SHADER |
1669 META_STENCIL_TEST |
1670 META_VERTEX |
1671 META_VIEWPORT |
1672 META_CLAMP_FRAGMENT_COLOR);
1673
1674 if (!(buffers & BUFFER_BITS_COLOR)) {
1675 /* We'll use colormask to disable color writes. Otherwise,
1676 * respect color mask
1677 */
1678 metaSave |= META_COLOR_MASK;
1679 }
1680
1681 _mesa_meta_begin(ctx, metaSave);
1682
1683 meta_glsl_clear_init(ctx, clear);
1684
1685 _mesa_UseProgramObjectARB(clear->ShaderProg);
1686 _mesa_Uniform4fvARB(clear->ColorLocation, 1,
1687 ctx->Color.ClearColorUnclamped);
1688
1689 _mesa_BindVertexArray(clear->ArrayObj);
1690 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1691
1692 /* GL_COLOR_BUFFER_BIT */
1693 if (buffers & BUFFER_BITS_COLOR) {
1694 /* leave colormask, glDrawBuffer state as-is */
1695
1696 /* Clears never have the color clamped. */
1697 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1698 }
1699 else {
1700 ASSERT(metaSave & META_COLOR_MASK);
1701 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1702 }
1703
1704 /* GL_DEPTH_BUFFER_BIT */
1705 if (buffers & BUFFER_BIT_DEPTH) {
1706 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1707 _mesa_DepthFunc(GL_ALWAYS);
1708 _mesa_DepthMask(GL_TRUE);
1709 }
1710 else {
1711 assert(!ctx->Depth.Test);
1712 }
1713
1714 /* GL_STENCIL_BUFFER_BIT */
1715 if (buffers & BUFFER_BIT_STENCIL) {
1716 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1717 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1718 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1719 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1720 ctx->Stencil.Clear & stencilMax,
1721 ctx->Stencil.WriteMask[0]);
1722 }
1723 else {
1724 assert(!ctx->Stencil.Enabled);
1725 }
1726
1727 /* vertex positions */
1728 verts[0].x = x0;
1729 verts[0].y = y0;
1730 verts[0].z = z;
1731 verts[1].x = x1;
1732 verts[1].y = y0;
1733 verts[1].z = z;
1734 verts[2].x = x1;
1735 verts[2].y = y1;
1736 verts[2].z = z;
1737 verts[3].x = x0;
1738 verts[3].y = y1;
1739 verts[3].z = z;
1740
1741 /* upload new vertex data */
1742 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1743 GL_DYNAMIC_DRAW_ARB);
1744
1745 /* draw quad */
1746 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1747
1748 _mesa_meta_end(ctx);
1749 }
1750
1751 /**
1752 * Meta implementation of ctx->Driver.CopyPixels() in terms
1753 * of texture mapping and polygon rendering and GLSL shaders.
1754 */
1755 void
1756 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1757 GLsizei width, GLsizei height,
1758 GLint dstX, GLint dstY, GLenum type)
1759 {
1760 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1761 struct temp_texture *tex = get_temp_texture(ctx);
1762 struct vertex {
1763 GLfloat x, y, z, s, t;
1764 };
1765 struct vertex verts[4];
1766 GLboolean newTex;
1767 GLenum intFormat = GL_RGBA;
1768
1769 if (type != GL_COLOR ||
1770 ctx->_ImageTransferState ||
1771 ctx->Fog.Enabled ||
1772 width > tex->MaxSize ||
1773 height > tex->MaxSize) {
1774 /* XXX avoid this fallback */
1775 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1776 return;
1777 }
1778
1779 /* Most GL state applies to glCopyPixels, but a there's a few things
1780 * we need to override:
1781 */
1782 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1783 META_SHADER |
1784 META_TEXTURE |
1785 META_TRANSFORM |
1786 META_VERTEX |
1787 META_VIEWPORT));
1788
1789 if (copypix->ArrayObj == 0) {
1790 /* one-time setup */
1791
1792 /* create vertex array object */
1793 _mesa_GenVertexArrays(1, &copypix->ArrayObj);
1794 _mesa_BindVertexArray(copypix->ArrayObj);
1795
1796 /* create vertex array buffer */
1797 _mesa_GenBuffersARB(1, &copypix->VBO);
1798 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1799 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1800 NULL, GL_DYNAMIC_DRAW_ARB);
1801
1802 /* setup vertex arrays */
1803 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1804 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1805 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1806 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1807 }
1808 else {
1809 _mesa_BindVertexArray(copypix->ArrayObj);
1810 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1811 }
1812
1813 newTex = alloc_texture(tex, width, height, intFormat);
1814
1815 /* vertex positions, texcoords (after texture allocation!) */
1816 {
1817 const GLfloat dstX0 = (GLfloat) dstX;
1818 const GLfloat dstY0 = (GLfloat) dstY;
1819 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1820 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1821 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
1822
1823 verts[0].x = dstX0;
1824 verts[0].y = dstY0;
1825 verts[0].z = z;
1826 verts[0].s = 0.0F;
1827 verts[0].t = 0.0F;
1828 verts[1].x = dstX1;
1829 verts[1].y = dstY0;
1830 verts[1].z = z;
1831 verts[1].s = tex->Sright;
1832 verts[1].t = 0.0F;
1833 verts[2].x = dstX1;
1834 verts[2].y = dstY1;
1835 verts[2].z = z;
1836 verts[2].s = tex->Sright;
1837 verts[2].t = tex->Ttop;
1838 verts[3].x = dstX0;
1839 verts[3].y = dstY1;
1840 verts[3].z = z;
1841 verts[3].s = 0.0F;
1842 verts[3].t = tex->Ttop;
1843
1844 /* upload new vertex data */
1845 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1846 }
1847
1848 /* Alloc/setup texture */
1849 setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
1850 GL_RGBA, GL_NEAREST);
1851
1852 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1853
1854 /* draw textured quad */
1855 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1856
1857 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1858
1859 _mesa_meta_end(ctx);
1860 }
1861
1862
1863
1864 /**
1865 * When the glDrawPixels() image size is greater than the max rectangle
1866 * texture size we use this function to break the glDrawPixels() image
1867 * into tiles which fit into the max texture size.
1868 */
1869 static void
1870 tiled_draw_pixels(struct gl_context *ctx,
1871 GLint tileSize,
1872 GLint x, GLint y, GLsizei width, GLsizei height,
1873 GLenum format, GLenum type,
1874 const struct gl_pixelstore_attrib *unpack,
1875 const GLvoid *pixels)
1876 {
1877 struct gl_pixelstore_attrib tileUnpack = *unpack;
1878 GLint i, j;
1879
1880 if (tileUnpack.RowLength == 0)
1881 tileUnpack.RowLength = width;
1882
1883 for (i = 0; i < width; i += tileSize) {
1884 const GLint tileWidth = MIN2(tileSize, width - i);
1885 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1886
1887 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1888
1889 for (j = 0; j < height; j += tileSize) {
1890 const GLint tileHeight = MIN2(tileSize, height - j);
1891 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1892
1893 tileUnpack.SkipRows = unpack->SkipRows + j;
1894
1895 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
1896 format, type, &tileUnpack, pixels);
1897 }
1898 }
1899 }
1900
1901
1902 /**
1903 * One-time init for drawing stencil pixels.
1904 */
1905 static void
1906 init_draw_stencil_pixels(struct gl_context *ctx)
1907 {
1908 /* This program is run eight times, once for each stencil bit.
1909 * The stencil values to draw are found in an 8-bit alpha texture.
1910 * We read the texture/stencil value and test if bit 'b' is set.
1911 * If the bit is not set, use KIL to kill the fragment.
1912 * Finally, we use the stencil test to update the stencil buffer.
1913 *
1914 * The basic algorithm for checking if a bit is set is:
1915 * if (is_odd(value / (1 << bit)))
1916 * result is one (or non-zero).
1917 * else
1918 * result is zero.
1919 * The program parameter contains three values:
1920 * parm.x = 255 / (1 << bit)
1921 * parm.y = 0.5
1922 * parm.z = 0.0
1923 */
1924 static const char *program =
1925 "!!ARBfp1.0\n"
1926 "PARAM parm = program.local[0]; \n"
1927 "TEMP t; \n"
1928 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1929 "# t = t * 255 / bit \n"
1930 "MUL t.x, t.a, parm.x; \n"
1931 "# t = (int) t \n"
1932 "FRC t.y, t.x; \n"
1933 "SUB t.x, t.x, t.y; \n"
1934 "# t = t * 0.5 \n"
1935 "MUL t.x, t.x, parm.y; \n"
1936 "# t = fract(t.x) \n"
1937 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1938 "# t.x = (t.x == 0 ? 1 : 0) \n"
1939 "SGE t.x, -t.x, parm.z; \n"
1940 "KIL -t.x; \n"
1941 "# for debug only \n"
1942 "#MOV result.color, t.x; \n"
1943 "END \n";
1944 char program2[1000];
1945 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1946 struct temp_texture *tex = get_temp_texture(ctx);
1947 const char *texTarget;
1948
1949 assert(drawpix->StencilFP == 0);
1950
1951 /* replace %s with "RECT" or "2D" */
1952 assert(strlen(program) + 4 < sizeof(program2));
1953 if (tex->Target == GL_TEXTURE_RECTANGLE)
1954 texTarget = "RECT";
1955 else
1956 texTarget = "2D";
1957 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1958
1959 _mesa_GenPrograms(1, &drawpix->StencilFP);
1960 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1961 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1962 strlen(program2), (const GLubyte *) program2);
1963 }
1964
1965
1966 /**
1967 * One-time init for drawing depth pixels.
1968 */
1969 static void
1970 init_draw_depth_pixels(struct gl_context *ctx)
1971 {
1972 static const char *program =
1973 "!!ARBfp1.0\n"
1974 "PARAM color = program.local[0]; \n"
1975 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1976 "MOV result.color, color; \n"
1977 "END \n";
1978 char program2[200];
1979 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1980 struct temp_texture *tex = get_temp_texture(ctx);
1981 const char *texTarget;
1982
1983 assert(drawpix->DepthFP == 0);
1984
1985 /* replace %s with "RECT" or "2D" */
1986 assert(strlen(program) + 4 < sizeof(program2));
1987 if (tex->Target == GL_TEXTURE_RECTANGLE)
1988 texTarget = "RECT";
1989 else
1990 texTarget = "2D";
1991 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1992
1993 _mesa_GenPrograms(1, &drawpix->DepthFP);
1994 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1995 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1996 strlen(program2), (const GLubyte *) program2);
1997 }
1998
1999
2000 /**
2001 * Meta implementation of ctx->Driver.DrawPixels() in terms
2002 * of texture mapping and polygon rendering.
2003 */
2004 void
2005 _mesa_meta_DrawPixels(struct gl_context *ctx,
2006 GLint x, GLint y, GLsizei width, GLsizei height,
2007 GLenum format, GLenum type,
2008 const struct gl_pixelstore_attrib *unpack,
2009 const GLvoid *pixels)
2010 {
2011 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2012 struct temp_texture *tex = get_temp_texture(ctx);
2013 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
2014 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
2015 struct vertex {
2016 GLfloat x, y, z, s, t;
2017 };
2018 struct vertex verts[4];
2019 GLenum texIntFormat;
2020 GLboolean fallback, newTex;
2021 GLbitfield metaExtraSave = 0x0;
2022 GLuint vbo;
2023
2024 /*
2025 * Determine if we can do the glDrawPixels with texture mapping.
2026 */
2027 fallback = GL_FALSE;
2028 if (ctx->_ImageTransferState ||
2029 ctx->Fog.Enabled) {
2030 fallback = GL_TRUE;
2031 }
2032
2033 if (_mesa_is_color_format(format)) {
2034 /* use more compact format when possible */
2035 /* XXX disable special case for GL_LUMINANCE for now to work around
2036 * apparent i965 driver bug (see bug #23670).
2037 */
2038 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2039 texIntFormat = format;
2040 else
2041 texIntFormat = GL_RGBA;
2042
2043 /* If we're not supposed to clamp the resulting color, then just
2044 * promote our texture to fully float. We could do better by
2045 * just going for the matching set of channels, in floating
2046 * point.
2047 */
2048 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2049 ctx->Extensions.ARB_texture_float)
2050 texIntFormat = GL_RGBA32F;
2051 }
2052 else if (_mesa_is_stencil_format(format)) {
2053 if (ctx->Extensions.ARB_fragment_program &&
2054 ctx->Pixel.IndexShift == 0 &&
2055 ctx->Pixel.IndexOffset == 0 &&
2056 type == GL_UNSIGNED_BYTE) {
2057 /* We'll store stencil as alpha. This only works for GLubyte
2058 * image data because of how incoming values are mapped to alpha
2059 * in [0,1].
2060 */
2061 texIntFormat = GL_ALPHA;
2062 metaExtraSave = (META_COLOR_MASK |
2063 META_DEPTH_TEST |
2064 META_SHADER |
2065 META_STENCIL_TEST);
2066 }
2067 else {
2068 fallback = GL_TRUE;
2069 }
2070 }
2071 else if (_mesa_is_depth_format(format)) {
2072 if (ctx->Extensions.ARB_depth_texture &&
2073 ctx->Extensions.ARB_fragment_program) {
2074 texIntFormat = GL_DEPTH_COMPONENT;
2075 metaExtraSave = (META_SHADER);
2076 }
2077 else {
2078 fallback = GL_TRUE;
2079 }
2080 }
2081 else {
2082 fallback = GL_TRUE;
2083 }
2084
2085 if (fallback) {
2086 _swrast_DrawPixels(ctx, x, y, width, height,
2087 format, type, unpack, pixels);
2088 return;
2089 }
2090
2091 /*
2092 * Check image size against max texture size, draw as tiles if needed.
2093 */
2094 if (width > tex->MaxSize || height > tex->MaxSize) {
2095 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2096 format, type, unpack, pixels);
2097 return;
2098 }
2099
2100 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2101 * but a there's a few things we need to override:
2102 */
2103 _mesa_meta_begin(ctx, (META_RASTERIZATION |
2104 META_SHADER |
2105 META_TEXTURE |
2106 META_TRANSFORM |
2107 META_VERTEX |
2108 META_VIEWPORT |
2109 META_CLAMP_FRAGMENT_COLOR |
2110 metaExtraSave));
2111
2112 newTex = alloc_texture(tex, width, height, texIntFormat);
2113
2114 /* vertex positions, texcoords (after texture allocation!) */
2115 {
2116 const GLfloat x0 = (GLfloat) x;
2117 const GLfloat y0 = (GLfloat) y;
2118 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2119 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2120 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2121
2122 verts[0].x = x0;
2123 verts[0].y = y0;
2124 verts[0].z = z;
2125 verts[0].s = 0.0F;
2126 verts[0].t = 0.0F;
2127 verts[1].x = x1;
2128 verts[1].y = y0;
2129 verts[1].z = z;
2130 verts[1].s = tex->Sright;
2131 verts[1].t = 0.0F;
2132 verts[2].x = x1;
2133 verts[2].y = y1;
2134 verts[2].z = z;
2135 verts[2].s = tex->Sright;
2136 verts[2].t = tex->Ttop;
2137 verts[3].x = x0;
2138 verts[3].y = y1;
2139 verts[3].z = z;
2140 verts[3].s = 0.0F;
2141 verts[3].t = tex->Ttop;
2142 }
2143
2144 if (drawpix->ArrayObj == 0) {
2145 /* one-time setup: create vertex array object */
2146 _mesa_GenVertexArrays(1, &drawpix->ArrayObj);
2147 }
2148 _mesa_BindVertexArray(drawpix->ArrayObj);
2149
2150 /* create vertex array buffer */
2151 _mesa_GenBuffersARB(1, &vbo);
2152 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
2153 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2154 verts, GL_DYNAMIC_DRAW_ARB);
2155
2156 /* setup vertex arrays */
2157 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2158 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2159 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2160 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2161
2162 /* set given unpack params */
2163 ctx->Unpack = *unpack;
2164
2165 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2166
2167 if (_mesa_is_stencil_format(format)) {
2168 /* Drawing stencil */
2169 GLint bit;
2170
2171 if (!drawpix->StencilFP)
2172 init_draw_stencil_pixels(ctx);
2173
2174 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2175 GL_ALPHA, type, pixels);
2176
2177 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2178
2179 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2180
2181 /* set all stencil bits to 0 */
2182 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2183 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2184 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2185
2186 /* set stencil bits to 1 where needed */
2187 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2188
2189 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2190 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2191
2192 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2193 const GLuint mask = 1 << bit;
2194 if (mask & origStencilMask) {
2195 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2196 _mesa_StencilMask(mask);
2197
2198 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2199 255.0 / mask, 0.5, 0.0, 0.0);
2200
2201 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2202 }
2203 }
2204 }
2205 else if (_mesa_is_depth_format(format)) {
2206 /* Drawing depth */
2207 if (!drawpix->DepthFP)
2208 init_draw_depth_pixels(ctx);
2209
2210 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2211 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2212
2213 /* polygon color = current raster color */
2214 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2215 ctx->Current.RasterColor);
2216
2217 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2218 format, type, pixels);
2219
2220 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2221 }
2222 else {
2223 /* Drawing RGBA */
2224 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2225 format, type, pixels);
2226 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2227 }
2228
2229 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2230
2231 _mesa_DeleteBuffersARB(1, &vbo);
2232
2233 /* restore unpack params */
2234 ctx->Unpack = unpackSave;
2235
2236 _mesa_meta_end(ctx);
2237 }
2238
2239 static GLboolean
2240 alpha_test_raster_color(struct gl_context *ctx)
2241 {
2242 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2243 GLfloat ref = ctx->Color.AlphaRef;
2244
2245 switch (ctx->Color.AlphaFunc) {
2246 case GL_NEVER:
2247 return GL_FALSE;
2248 case GL_LESS:
2249 return alpha < ref;
2250 case GL_EQUAL:
2251 return alpha == ref;
2252 case GL_LEQUAL:
2253 return alpha <= ref;
2254 case GL_GREATER:
2255 return alpha > ref;
2256 case GL_NOTEQUAL:
2257 return alpha != ref;
2258 case GL_GEQUAL:
2259 return alpha >= ref;
2260 case GL_ALWAYS:
2261 return GL_TRUE;
2262 default:
2263 assert(0);
2264 return GL_FALSE;
2265 }
2266 }
2267
2268 /**
2269 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2270 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2271 * tracker would improve performance a lot.
2272 */
2273 void
2274 _mesa_meta_Bitmap(struct gl_context *ctx,
2275 GLint x, GLint y, GLsizei width, GLsizei height,
2276 const struct gl_pixelstore_attrib *unpack,
2277 const GLubyte *bitmap1)
2278 {
2279 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2280 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2281 const GLenum texIntFormat = GL_ALPHA;
2282 const struct gl_pixelstore_attrib unpackSave = *unpack;
2283 GLubyte fg, bg;
2284 struct vertex {
2285 GLfloat x, y, z, s, t, r, g, b, a;
2286 };
2287 struct vertex verts[4];
2288 GLboolean newTex;
2289 GLubyte *bitmap8;
2290
2291 /*
2292 * Check if swrast fallback is needed.
2293 */
2294 if (ctx->_ImageTransferState ||
2295 ctx->FragmentProgram._Enabled ||
2296 ctx->Fog.Enabled ||
2297 ctx->Texture._EnabledUnits ||
2298 width > tex->MaxSize ||
2299 height > tex->MaxSize) {
2300 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2301 return;
2302 }
2303
2304 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2305 return;
2306
2307 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2308 * but a there's a few things we need to override:
2309 */
2310 _mesa_meta_begin(ctx, (META_ALPHA_TEST |
2311 META_PIXEL_STORE |
2312 META_RASTERIZATION |
2313 META_SHADER |
2314 META_TEXTURE |
2315 META_TRANSFORM |
2316 META_VERTEX |
2317 META_VIEWPORT));
2318
2319 if (bitmap->ArrayObj == 0) {
2320 /* one-time setup */
2321
2322 /* create vertex array object */
2323 _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
2324 _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
2325
2326 /* create vertex array buffer */
2327 _mesa_GenBuffersARB(1, &bitmap->VBO);
2328 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2329 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2330 NULL, GL_DYNAMIC_DRAW_ARB);
2331
2332 /* setup vertex arrays */
2333 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2334 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2335 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
2336 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2337 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2338 _mesa_EnableClientState(GL_COLOR_ARRAY);
2339 }
2340 else {
2341 _mesa_BindVertexArray(bitmap->ArrayObj);
2342 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2343 }
2344
2345 newTex = alloc_texture(tex, width, height, texIntFormat);
2346
2347 /* vertex positions, texcoords, colors (after texture allocation!) */
2348 {
2349 const GLfloat x0 = (GLfloat) x;
2350 const GLfloat y0 = (GLfloat) y;
2351 const GLfloat x1 = (GLfloat) (x + width);
2352 const GLfloat y1 = (GLfloat) (y + height);
2353 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2354 GLuint i;
2355
2356 verts[0].x = x0;
2357 verts[0].y = y0;
2358 verts[0].z = z;
2359 verts[0].s = 0.0F;
2360 verts[0].t = 0.0F;
2361 verts[1].x = x1;
2362 verts[1].y = y0;
2363 verts[1].z = z;
2364 verts[1].s = tex->Sright;
2365 verts[1].t = 0.0F;
2366 verts[2].x = x1;
2367 verts[2].y = y1;
2368 verts[2].z = z;
2369 verts[2].s = tex->Sright;
2370 verts[2].t = tex->Ttop;
2371 verts[3].x = x0;
2372 verts[3].y = y1;
2373 verts[3].z = z;
2374 verts[3].s = 0.0F;
2375 verts[3].t = tex->Ttop;
2376
2377 for (i = 0; i < 4; i++) {
2378 verts[i].r = ctx->Current.RasterColor[0];
2379 verts[i].g = ctx->Current.RasterColor[1];
2380 verts[i].b = ctx->Current.RasterColor[2];
2381 verts[i].a = ctx->Current.RasterColor[3];
2382 }
2383
2384 /* upload new vertex data */
2385 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2386 }
2387
2388 /* choose different foreground/background alpha values */
2389 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2390 bg = (fg > 127 ? 0 : 255);
2391
2392 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2393 if (!bitmap1) {
2394 _mesa_meta_end(ctx);
2395 return;
2396 }
2397
2398 bitmap8 = (GLubyte *) malloc(width * height);
2399 if (bitmap8) {
2400 memset(bitmap8, bg, width * height);
2401 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2402 bitmap8, width, fg);
2403
2404 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2405
2406 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2407 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2408
2409 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2410 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2411
2412 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2413
2414 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2415
2416 free(bitmap8);
2417 }
2418
2419 _mesa_unmap_pbo_source(ctx, &unpackSave);
2420
2421 _mesa_meta_end(ctx);
2422 }
2423
2424
2425 /**
2426 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2427 * software fallback. The fallback path will require that the texture
2428 * images are mapped.
2429 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2430 */
2431 GLboolean
2432 _mesa_meta_check_generate_mipmap_fallback(struct gl_context *ctx, GLenum target,
2433 struct gl_texture_object *texObj)
2434 {
2435 const GLuint fboSave = ctx->DrawBuffer->Name;
2436 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2437 struct gl_texture_image *baseImage;
2438 GLuint srcLevel;
2439 GLenum status;
2440
2441 /* check for fallbacks */
2442 if (!ctx->Extensions.EXT_framebuffer_object ||
2443 target == GL_TEXTURE_3D) {
2444 return GL_TRUE;
2445 }
2446
2447 srcLevel = texObj->BaseLevel;
2448 baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
2449 if (!baseImage || _mesa_is_format_compressed(baseImage->TexFormat)) {
2450 return GL_TRUE;
2451 }
2452
2453 /*
2454 * Test that we can actually render in the texture's format.
2455 */
2456 if (!mipmap->FBO)
2457 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2458 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2459
2460 if (target == GL_TEXTURE_1D) {
2461 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2462 GL_COLOR_ATTACHMENT0_EXT,
2463 target, texObj->Name, srcLevel);
2464 }
2465 #if 0
2466 /* other work is needed to enable 3D mipmap generation */
2467 else if (target == GL_TEXTURE_3D) {
2468 GLint zoffset = 0;
2469 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2470 GL_COLOR_ATTACHMENT0_EXT,
2471 target, texObj->Name, srcLevel, zoffset);
2472 }
2473 #endif
2474 else {
2475 /* 2D / cube */
2476 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2477 GL_COLOR_ATTACHMENT0_EXT,
2478 target, texObj->Name, srcLevel);
2479 }
2480
2481 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2482
2483 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2484
2485 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2486 return GL_TRUE;
2487 }
2488
2489 return GL_FALSE;
2490 }
2491
2492
2493 /**
2494 * Called via ctx->Driver.GenerateMipmap()
2495 * Note: texture borders and 3D texture support not yet complete.
2496 */
2497 void
2498 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
2499 struct gl_texture_object *texObj)
2500 {
2501 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2502 struct vertex {
2503 GLfloat x, y, s, t, r;
2504 };
2505 struct vertex verts[4];
2506 const GLuint baseLevel = texObj->BaseLevel;
2507 const GLuint maxLevel = texObj->MaxLevel;
2508 const GLenum minFilterSave = texObj->Sampler.MinFilter;
2509 const GLenum magFilterSave = texObj->Sampler.MagFilter;
2510 const GLint maxLevelSave = texObj->MaxLevel;
2511 const GLboolean genMipmapSave = texObj->GenerateMipmap;
2512 const GLenum wrapSSave = texObj->Sampler.WrapS;
2513 const GLenum wrapTSave = texObj->Sampler.WrapT;
2514 const GLenum wrapRSave = texObj->Sampler.WrapR;
2515 const GLuint fboSave = ctx->DrawBuffer->Name;
2516 const GLuint original_active_unit = ctx->Texture.CurrentUnit;
2517 GLenum faceTarget;
2518 GLuint dstLevel;
2519 GLuint border = 0;
2520
2521 if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
2522 _mesa_generate_mipmap(ctx, target, texObj);
2523 return;
2524 }
2525
2526 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
2527 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
2528 faceTarget = target;
2529 target = GL_TEXTURE_CUBE_MAP;
2530 }
2531 else {
2532 faceTarget = target;
2533 }
2534
2535 _mesa_meta_begin(ctx, META_ALL);
2536
2537 if (original_active_unit != 0)
2538 _mesa_BindTexture(target, texObj->Name);
2539
2540 if (mipmap->ArrayObj == 0) {
2541 /* one-time setup */
2542
2543 /* create vertex array object */
2544 _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
2545 _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
2546
2547 /* create vertex array buffer */
2548 _mesa_GenBuffersARB(1, &mipmap->VBO);
2549 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2550 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2551 NULL, GL_DYNAMIC_DRAW_ARB);
2552
2553 /* setup vertex arrays */
2554 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2555 _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2556 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2557 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2558 }
2559 else {
2560 _mesa_BindVertexArray(mipmap->ArrayObj);
2561 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2562 }
2563
2564 if (!mipmap->FBO) {
2565 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2566 }
2567 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2568
2569 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2570 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2571 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
2572 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2573 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2574 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2575
2576 _mesa_set_enable(ctx, target, GL_TRUE);
2577
2578 /* setup texcoords once (XXX what about border?) */
2579 switch (faceTarget) {
2580 case GL_TEXTURE_1D:
2581 case GL_TEXTURE_2D:
2582 verts[0].s = 0.0F;
2583 verts[0].t = 0.0F;
2584 verts[0].r = 0.0F;
2585 verts[1].s = 1.0F;
2586 verts[1].t = 0.0F;
2587 verts[1].r = 0.0F;
2588 verts[2].s = 1.0F;
2589 verts[2].t = 1.0F;
2590 verts[2].r = 0.0F;
2591 verts[3].s = 0.0F;
2592 verts[3].t = 1.0F;
2593 verts[3].r = 0.0F;
2594 break;
2595 case GL_TEXTURE_3D:
2596 abort();
2597 break;
2598 default:
2599 /* cube face */
2600 {
2601 static const GLfloat st[4][2] = {
2602 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
2603 };
2604 GLuint i;
2605
2606 /* loop over quad verts */
2607 for (i = 0; i < 4; i++) {
2608 /* Compute sc = +/-scale and tc = +/-scale.
2609 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2610 * though that can still sometimes happen with this scale factor...
2611 */
2612 const GLfloat scale = 0.9999f;
2613 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2614 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2615
2616 switch (faceTarget) {
2617 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2618 verts[i].s = 1.0f;
2619 verts[i].t = -tc;
2620 verts[i].r = -sc;
2621 break;
2622 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2623 verts[i].s = -1.0f;
2624 verts[i].t = -tc;
2625 verts[i].r = sc;
2626 break;
2627 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2628 verts[i].s = sc;
2629 verts[i].t = 1.0f;
2630 verts[i].r = tc;
2631 break;
2632 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2633 verts[i].s = sc;
2634 verts[i].t = -1.0f;
2635 verts[i].r = -tc;
2636 break;
2637 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2638 verts[i].s = sc;
2639 verts[i].t = -tc;
2640 verts[i].r = 1.0f;
2641 break;
2642 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2643 verts[i].s = -sc;
2644 verts[i].t = -tc;
2645 verts[i].r = -1.0f;
2646 break;
2647 default:
2648 assert(0);
2649 }
2650 }
2651 }
2652 }
2653
2654 _mesa_set_enable(ctx, target, GL_TRUE);
2655
2656 /* setup vertex positions */
2657 {
2658 verts[0].x = 0.0F;
2659 verts[0].y = 0.0F;
2660 verts[1].x = 1.0F;
2661 verts[1].y = 0.0F;
2662 verts[2].x = 1.0F;
2663 verts[2].y = 1.0F;
2664 verts[3].x = 0.0F;
2665 verts[3].y = 1.0F;
2666
2667 /* upload new vertex data */
2668 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2669 }
2670
2671 /* setup projection matrix */
2672 _mesa_MatrixMode(GL_PROJECTION);
2673 _mesa_LoadIdentity();
2674 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2675
2676 /* texture is already locked, unlock now */
2677 _mesa_unlock_texture(ctx, texObj);
2678
2679 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
2680 const struct gl_texture_image *srcImage;
2681 const GLuint srcLevel = dstLevel - 1;
2682 GLsizei srcWidth, srcHeight, srcDepth;
2683 GLsizei dstWidth, dstHeight, dstDepth;
2684 GLenum status;
2685
2686 srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
2687 assert(srcImage->Border == 0); /* XXX we can fix this */
2688
2689 /* src size w/out border */
2690 srcWidth = srcImage->Width - 2 * border;
2691 srcHeight = srcImage->Height - 2 * border;
2692 srcDepth = srcImage->Depth - 2 * border;
2693
2694 /* new dst size w/ border */
2695 dstWidth = MAX2(1, srcWidth / 2) + 2 * border;
2696 dstHeight = MAX2(1, srcHeight / 2) + 2 * border;
2697 dstDepth = MAX2(1, srcDepth / 2) + 2 * border;
2698
2699 if (dstWidth == srcImage->Width &&
2700 dstHeight == srcImage->Height &&
2701 dstDepth == srcImage->Depth) {
2702 /* all done */
2703 break;
2704 }
2705
2706 /* Set MaxLevel large enough to hold the new level when we allocate it */
2707 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
2708
2709 /* Create empty dest image */
2710 if (target == GL_TEXTURE_1D) {
2711 _mesa_TexImage1D(target, dstLevel, srcImage->InternalFormat,
2712 dstWidth, border,
2713 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2714 }
2715 else if (target == GL_TEXTURE_3D) {
2716 _mesa_TexImage3D(target, dstLevel, srcImage->InternalFormat,
2717 dstWidth, dstHeight, dstDepth, border,
2718 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2719 }
2720 else {
2721 /* 2D or cube */
2722 _mesa_TexImage2D(faceTarget, dstLevel, srcImage->InternalFormat,
2723 dstWidth, dstHeight, border,
2724 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2725
2726 if (target == GL_TEXTURE_CUBE_MAP) {
2727 /* If texturing from a cube, we need to make sure all src faces
2728 * have been defined (even if we're not sampling from them.)
2729 * Otherwise the texture object will be 'incomplete' and
2730 * texturing from it will not be allowed.
2731 */
2732 GLuint face;
2733 for (face = 0; face < 6; face++) {
2734 if (!texObj->Image[face][srcLevel] ||
2735 texObj->Image[face][srcLevel]->Width != srcWidth) {
2736 _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
2737 srcLevel, srcImage->InternalFormat,
2738 srcWidth, srcHeight, border,
2739 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2740 }
2741 }
2742 }
2743 }
2744
2745 /* limit minification to src level */
2746 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
2747
2748 /* Set to draw into the current dstLevel */
2749 if (target == GL_TEXTURE_1D) {
2750 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2751 GL_COLOR_ATTACHMENT0_EXT,
2752 target,
2753 texObj->Name,
2754 dstLevel);
2755 }
2756 else if (target == GL_TEXTURE_3D) {
2757 GLint zoffset = 0; /* XXX unfinished */
2758 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2759 GL_COLOR_ATTACHMENT0_EXT,
2760 target,
2761 texObj->Name,
2762 dstLevel, zoffset);
2763 }
2764 else {
2765 /* 2D / cube */
2766 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2767 GL_COLOR_ATTACHMENT0_EXT,
2768 faceTarget,
2769 texObj->Name,
2770 dstLevel);
2771 }
2772
2773 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
2774
2775 /* sanity check */
2776 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2777 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2778 abort();
2779 break;
2780 }
2781
2782 assert(dstWidth == ctx->DrawBuffer->Width);
2783 assert(dstHeight == ctx->DrawBuffer->Height);
2784
2785 /* setup viewport */
2786 _mesa_set_viewport(ctx, 0, 0, dstWidth, dstHeight);
2787
2788 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2789 }
2790
2791 _mesa_lock_texture(ctx, texObj); /* relock */
2792
2793 _mesa_meta_end(ctx);
2794
2795 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
2796 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
2797 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
2798 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
2799 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
2800 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
2801 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, wrapRSave);
2802
2803 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2804 }
2805
2806
2807 /**
2808 * Determine the GL data type to use for the temporary image read with
2809 * ReadPixels() and passed to Tex[Sub]Image().
2810 */
2811 static GLenum
2812 get_temp_image_type(struct gl_context *ctx, GLenum baseFormat)
2813 {
2814 switch (baseFormat) {
2815 case GL_RGBA:
2816 case GL_RGB:
2817 case GL_ALPHA:
2818 case GL_LUMINANCE:
2819 case GL_LUMINANCE_ALPHA:
2820 case GL_INTENSITY:
2821 if (ctx->DrawBuffer->Visual.redBits <= 8)
2822 return GL_UNSIGNED_BYTE;
2823 else if (ctx->DrawBuffer->Visual.redBits <= 8)
2824 return GL_UNSIGNED_SHORT;
2825 else
2826 return GL_FLOAT;
2827 case GL_DEPTH_COMPONENT:
2828 return GL_UNSIGNED_INT;
2829 case GL_DEPTH_STENCIL:
2830 return GL_UNSIGNED_INT_24_8;
2831 default:
2832 _mesa_problem(ctx, "Unexpected format in get_temp_image_type()");
2833 return 0;
2834 }
2835 }
2836
2837
2838 /**
2839 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
2840 * Have to be careful with locking and meta state for pixel transfer.
2841 */
2842 static void
2843 copy_tex_sub_image(struct gl_context *ctx,
2844 GLuint dims, GLenum target, GLint level,
2845 GLint xoffset, GLint yoffset, GLint zoffset,
2846 GLint x, GLint y,
2847 GLsizei width, GLsizei height)
2848 {
2849 struct gl_texture_object *texObj;
2850 struct gl_texture_image *texImage;
2851 GLenum format, type;
2852 GLint bpp;
2853 void *buf;
2854
2855 texObj = _mesa_get_current_tex_object(ctx, target);
2856 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2857
2858 /* Choose format/type for temporary image buffer */
2859 format = _mesa_get_format_base_format(texImage->TexFormat);
2860 type = get_temp_image_type(ctx, format);
2861 bpp = _mesa_bytes_per_pixel(format, type);
2862 if (bpp <= 0) {
2863 _mesa_problem(ctx, "Bad bpp in meta copy_tex_sub_image()");
2864 return;
2865 }
2866
2867 /*
2868 * Alloc image buffer (XXX could use a PBO)
2869 */
2870 buf = malloc(width * height * bpp);
2871 if (!buf) {
2872 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
2873 return;
2874 }
2875
2876 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
2877
2878 /*
2879 * Read image from framebuffer (disable pixel transfer ops)
2880 */
2881 _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER);
2882 ctx->Driver.ReadPixels(ctx, x, y, width, height,
2883 format, type, &ctx->Pack, buf);
2884 _mesa_meta_end(ctx);
2885
2886 _mesa_update_state(ctx); /* to update pixel transfer state */
2887
2888 /*
2889 * Store texture data (with pixel transfer ops)
2890 */
2891 _mesa_meta_begin(ctx, META_PIXEL_STORE);
2892 if (target == GL_TEXTURE_1D) {
2893 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset,
2894 width, format, type, buf,
2895 &ctx->Unpack, texObj, texImage);
2896 }
2897 else if (target == GL_TEXTURE_3D) {
2898 ctx->Driver.TexSubImage3D(ctx, target, level, xoffset, yoffset, zoffset,
2899 width, height, 1, format, type, buf,
2900 &ctx->Unpack, texObj, texImage);
2901 }
2902 else {
2903 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
2904 width, height, format, type, buf,
2905 &ctx->Unpack, texObj, texImage);
2906 }
2907 _mesa_meta_end(ctx);
2908
2909 _mesa_lock_texture(ctx, texObj); /* re-lock */
2910
2911 free(buf);
2912 }
2913
2914
2915 void
2916 _mesa_meta_CopyTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
2917 GLint xoffset,
2918 GLint x, GLint y, GLsizei width)
2919 {
2920 copy_tex_sub_image(ctx, 1, target, level, xoffset, 0, 0,
2921 x, y, width, 1);
2922 }
2923
2924
2925 void
2926 _mesa_meta_CopyTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
2927 GLint xoffset, GLint yoffset,
2928 GLint x, GLint y,
2929 GLsizei width, GLsizei height)
2930 {
2931 copy_tex_sub_image(ctx, 2, target, level, xoffset, yoffset, 0,
2932 x, y, width, height);
2933 }
2934
2935
2936 void
2937 _mesa_meta_CopyTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
2938 GLint xoffset, GLint yoffset, GLint zoffset,
2939 GLint x, GLint y,
2940 GLsizei width, GLsizei height)
2941 {
2942 copy_tex_sub_image(ctx, 3, target, level, xoffset, yoffset, zoffset,
2943 x, y, width, height);
2944 }
2945
2946
2947 void
2948 _mesa_meta_CopyColorTable(struct gl_context *ctx,
2949 GLenum target, GLenum internalformat,
2950 GLint x, GLint y, GLsizei width)
2951 {
2952 GLfloat *buf;
2953
2954 buf = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
2955 if (!buf) {
2956 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorTable");
2957 return;
2958 }
2959
2960 /*
2961 * Read image from framebuffer (disable pixel transfer ops)
2962 */
2963 _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER);
2964 ctx->Driver.ReadPixels(ctx, x, y, width, 1,
2965 GL_RGBA, GL_FLOAT, &ctx->Pack, buf);
2966
2967 _mesa_ColorTable(target, internalformat, width, GL_RGBA, GL_FLOAT, buf);
2968
2969 _mesa_meta_end(ctx);
2970
2971 free(buf);
2972 }
2973
2974
2975 void
2976 _mesa_meta_CopyColorSubTable(struct gl_context *ctx,GLenum target, GLsizei start,
2977 GLint x, GLint y, GLsizei width)
2978 {
2979 GLfloat *buf;
2980
2981 buf = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
2982 if (!buf) {
2983 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorSubTable");
2984 return;
2985 }
2986
2987 /*
2988 * Read image from framebuffer (disable pixel transfer ops)
2989 */
2990 _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER);
2991 ctx->Driver.ReadPixels(ctx, x, y, width, 1,
2992 GL_RGBA, GL_FLOAT, &ctx->Pack, buf);
2993
2994 _mesa_ColorSubTable(target, start, width, GL_RGBA, GL_FLOAT, buf);
2995
2996 _mesa_meta_end(ctx);
2997
2998 free(buf);
2999 }