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