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