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