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