mesa: Add a function to query whether a meta-op is in progress.
[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 * Determine whether Mesa is currently in a meta state.
989 */
990 GLboolean
991 _mesa_meta_in_progress(struct gl_context *ctx)
992 {
993 return ctx->Meta->SaveStackDepth != 0;
994 }
995
996
997 /**
998 * Convert Z from a normalized value in the range [0, 1] to an object-space
999 * Z coordinate in [-1, +1] so that drawing at the new Z position with the
1000 * default/identity ortho projection results in the original Z value.
1001 * Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
1002 * value comes from the clear value or raster position.
1003 */
1004 static INLINE GLfloat
1005 invert_z(GLfloat normZ)
1006 {
1007 GLfloat objZ = 1.0 - 2.0 * normZ;
1008 return objZ;
1009 }
1010
1011
1012 /**
1013 * One-time init for a temp_texture object.
1014 * Choose tex target, compute max tex size, etc.
1015 */
1016 static void
1017 init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
1018 {
1019 /* prefer texture rectangle */
1020 if (ctx->Extensions.NV_texture_rectangle) {
1021 tex->Target = GL_TEXTURE_RECTANGLE;
1022 tex->MaxSize = ctx->Const.MaxTextureRectSize;
1023 tex->NPOT = GL_TRUE;
1024 }
1025 else {
1026 /* use 2D texture, NPOT if possible */
1027 tex->Target = GL_TEXTURE_2D;
1028 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1029 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
1030 }
1031 tex->MinSize = 16; /* 16 x 16 at least */
1032 assert(tex->MaxSize > 0);
1033
1034 _mesa_GenTextures(1, &tex->TexObj);
1035 }
1036
1037
1038 /**
1039 * Return pointer to temp_texture info for non-bitmap ops.
1040 * This does some one-time init if needed.
1041 */
1042 static struct temp_texture *
1043 get_temp_texture(struct gl_context *ctx)
1044 {
1045 struct temp_texture *tex = &ctx->Meta->TempTex;
1046
1047 if (!tex->TexObj) {
1048 init_temp_texture(ctx, tex);
1049 }
1050
1051 return tex;
1052 }
1053
1054
1055 /**
1056 * Return pointer to temp_texture info for _mesa_meta_bitmap().
1057 * We use a separate texture for bitmaps to reduce texture
1058 * allocation/deallocation.
1059 */
1060 static struct temp_texture *
1061 get_bitmap_temp_texture(struct gl_context *ctx)
1062 {
1063 struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
1064
1065 if (!tex->TexObj) {
1066 init_temp_texture(ctx, tex);
1067 }
1068
1069 return tex;
1070 }
1071
1072
1073 /**
1074 * Compute the width/height of texture needed to draw an image of the
1075 * given size. Return a flag indicating whether the current texture
1076 * can be re-used (glTexSubImage2D) or if a new texture needs to be
1077 * allocated (glTexImage2D).
1078 * Also, compute s/t texcoords for drawing.
1079 *
1080 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
1081 */
1082 static GLboolean
1083 alloc_texture(struct temp_texture *tex,
1084 GLsizei width, GLsizei height, GLenum intFormat)
1085 {
1086 GLboolean newTex = GL_FALSE;
1087
1088 ASSERT(width <= tex->MaxSize);
1089 ASSERT(height <= tex->MaxSize);
1090
1091 if (width > tex->Width ||
1092 height > tex->Height ||
1093 intFormat != tex->IntFormat) {
1094 /* alloc new texture (larger or different format) */
1095
1096 if (tex->NPOT) {
1097 /* use non-power of two size */
1098 tex->Width = MAX2(tex->MinSize, width);
1099 tex->Height = MAX2(tex->MinSize, height);
1100 }
1101 else {
1102 /* find power of two size */
1103 GLsizei w, h;
1104 w = h = tex->MinSize;
1105 while (w < width)
1106 w *= 2;
1107 while (h < height)
1108 h *= 2;
1109 tex->Width = w;
1110 tex->Height = h;
1111 }
1112
1113 tex->IntFormat = intFormat;
1114
1115 newTex = GL_TRUE;
1116 }
1117
1118 /* compute texcoords */
1119 if (tex->Target == GL_TEXTURE_RECTANGLE) {
1120 tex->Sright = (GLfloat) width;
1121 tex->Ttop = (GLfloat) height;
1122 }
1123 else {
1124 tex->Sright = (GLfloat) width / tex->Width;
1125 tex->Ttop = (GLfloat) height / tex->Height;
1126 }
1127
1128 return newTex;
1129 }
1130
1131
1132 /**
1133 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
1134 */
1135 static void
1136 setup_copypix_texture(struct temp_texture *tex,
1137 GLboolean newTex,
1138 GLint srcX, GLint srcY,
1139 GLsizei width, GLsizei height, GLenum intFormat,
1140 GLenum filter)
1141 {
1142 _mesa_BindTexture(tex->Target, tex->TexObj);
1143 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
1144 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
1145 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1146
1147 /* copy framebuffer image to texture */
1148 if (newTex) {
1149 /* create new tex image */
1150 if (tex->Width == width && tex->Height == height) {
1151 /* create new tex with framebuffer data */
1152 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
1153 srcX, srcY, width, height, 0);
1154 }
1155 else {
1156 /* create empty texture */
1157 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1158 tex->Width, tex->Height, 0,
1159 intFormat, GL_UNSIGNED_BYTE, NULL);
1160 /* load image */
1161 _mesa_CopyTexSubImage2D(tex->Target, 0,
1162 0, 0, srcX, srcY, width, height);
1163 }
1164 }
1165 else {
1166 /* replace existing tex image */
1167 _mesa_CopyTexSubImage2D(tex->Target, 0,
1168 0, 0, srcX, srcY, width, height);
1169 }
1170 }
1171
1172
1173 /**
1174 * Setup/load texture for glDrawPixels.
1175 */
1176 static void
1177 setup_drawpix_texture(struct gl_context *ctx,
1178 struct temp_texture *tex,
1179 GLboolean newTex,
1180 GLenum texIntFormat,
1181 GLsizei width, GLsizei height,
1182 GLenum format, GLenum type,
1183 const GLvoid *pixels)
1184 {
1185 _mesa_BindTexture(tex->Target, tex->TexObj);
1186 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1187 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1188 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1189
1190 /* copy pixel data to texture */
1191 if (newTex) {
1192 /* create new tex image */
1193 if (tex->Width == width && tex->Height == height) {
1194 /* create new tex and load image data */
1195 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1196 tex->Width, tex->Height, 0, format, type, pixels);
1197 }
1198 else {
1199 struct gl_buffer_object *save_unpack_obj = NULL;
1200
1201 _mesa_reference_buffer_object(ctx, &save_unpack_obj,
1202 ctx->Unpack.BufferObj);
1203 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1204 /* create empty texture */
1205 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
1206 tex->Width, tex->Height, 0, format, type, NULL);
1207 if (save_unpack_obj != NULL)
1208 _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB,
1209 save_unpack_obj->Name);
1210 /* load image */
1211 _mesa_TexSubImage2D(tex->Target, 0,
1212 0, 0, width, height, format, type, pixels);
1213 }
1214 }
1215 else {
1216 /* replace existing tex image */
1217 _mesa_TexSubImage2D(tex->Target, 0,
1218 0, 0, width, height, format, type, pixels);
1219 }
1220 }
1221
1222
1223
1224 /**
1225 * One-time init for drawing depth pixels.
1226 */
1227 static void
1228 init_blit_depth_pixels(struct gl_context *ctx)
1229 {
1230 static const char *program =
1231 "!!ARBfp1.0\n"
1232 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1233 "END \n";
1234 char program2[200];
1235 struct blit_state *blit = &ctx->Meta->Blit;
1236 struct temp_texture *tex = get_temp_texture(ctx);
1237 const char *texTarget;
1238
1239 assert(blit->DepthFP == 0);
1240
1241 /* replace %s with "RECT" or "2D" */
1242 assert(strlen(program) + 4 < sizeof(program2));
1243 if (tex->Target == GL_TEXTURE_RECTANGLE)
1244 texTarget = "RECT";
1245 else
1246 texTarget = "2D";
1247 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1248
1249 _mesa_GenPrograms(1, &blit->DepthFP);
1250 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1251 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1252 strlen(program2), (const GLubyte *) program2);
1253 }
1254
1255
1256 /**
1257 * Try to do a glBlitFramebuffer using no-copy texturing.
1258 * We can do this when the src renderbuffer is actually a texture.
1259 * But if the src buffer == dst buffer we cannot do this.
1260 *
1261 * \return new buffer mask indicating the buffers left to blit using the
1262 * normal path.
1263 */
1264 static GLbitfield
1265 blitframebuffer_texture(struct gl_context *ctx,
1266 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1267 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1268 GLbitfield mask, GLenum filter)
1269 {
1270 if (mask & GL_COLOR_BUFFER_BIT) {
1271 const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
1272 const struct gl_framebuffer *readFb = ctx->ReadBuffer;
1273 const struct gl_renderbuffer_attachment *drawAtt =
1274 &drawFb->Attachment[drawFb->_ColorDrawBufferIndexes[0]];
1275 const struct gl_renderbuffer_attachment *readAtt =
1276 &readFb->Attachment[readFb->_ColorReadBufferIndex];
1277
1278 if (readAtt && readAtt->Texture) {
1279 const struct gl_texture_object *texObj = readAtt->Texture;
1280 const GLuint srcLevel = readAtt->TextureLevel;
1281 const GLenum minFilterSave = texObj->Sampler.MinFilter;
1282 const GLenum magFilterSave = texObj->Sampler.MagFilter;
1283 const GLint baseLevelSave = texObj->BaseLevel;
1284 const GLint maxLevelSave = texObj->MaxLevel;
1285 const GLenum wrapSSave = texObj->Sampler.WrapS;
1286 const GLenum wrapTSave = texObj->Sampler.WrapT;
1287 const GLenum srgbSave = texObj->Sampler.sRGBDecode;
1288 const GLenum fbo_srgb_save = ctx->Color.sRGBEnabled;
1289 const GLenum target = texObj->Target;
1290
1291 if (drawAtt->Texture == readAtt->Texture) {
1292 /* Can't use same texture as both the source and dest. We need
1293 * to handle overlapping blits and besides, some hw may not
1294 * support this.
1295 */
1296 return mask;
1297 }
1298
1299 if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_ARB) {
1300 /* Can't handle other texture types at this time */
1301 return mask;
1302 }
1303
1304 /*
1305 printf("Blit from texture!\n");
1306 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
1307 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
1308 */
1309
1310 /* Prepare src texture state */
1311 _mesa_BindTexture(target, texObj->Name);
1312 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
1313 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
1314 if (target != GL_TEXTURE_RECTANGLE_ARB) {
1315 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
1316 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
1317 }
1318 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1319 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1320
1321 /* Always do our blits with no sRGB decode or encode.*/
1322 if (ctx->Extensions.EXT_texture_sRGB_decode) {
1323 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1324 GL_SKIP_DECODE_EXT);
1325 }
1326 if (ctx->Extensions.EXT_framebuffer_sRGB) {
1327 _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_FALSE);
1328 }
1329
1330 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1331 _mesa_set_enable(ctx, target, GL_TRUE);
1332
1333 /* Prepare vertex data (the VBO was previously created and bound) */
1334 {
1335 struct vertex {
1336 GLfloat x, y, s, t;
1337 };
1338 struct vertex verts[4];
1339 GLfloat s0, t0, s1, t1;
1340
1341 if (target == GL_TEXTURE_2D) {
1342 const struct gl_texture_image *texImage
1343 = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
1344 s0 = srcX0 / (float) texImage->Width;
1345 s1 = srcX1 / (float) texImage->Width;
1346 t0 = srcY0 / (float) texImage->Height;
1347 t1 = srcY1 / (float) texImage->Height;
1348 }
1349 else {
1350 assert(target == GL_TEXTURE_RECTANGLE_ARB);
1351 s0 = srcX0;
1352 s1 = srcX1;
1353 t0 = srcY0;
1354 t1 = srcY1;
1355 }
1356
1357 verts[0].x = (GLfloat) dstX0;
1358 verts[0].y = (GLfloat) dstY0;
1359 verts[1].x = (GLfloat) dstX1;
1360 verts[1].y = (GLfloat) dstY0;
1361 verts[2].x = (GLfloat) dstX1;
1362 verts[2].y = (GLfloat) dstY1;
1363 verts[3].x = (GLfloat) dstX0;
1364 verts[3].y = (GLfloat) dstY1;
1365
1366 verts[0].s = s0;
1367 verts[0].t = t0;
1368 verts[1].s = s1;
1369 verts[1].t = t0;
1370 verts[2].s = s1;
1371 verts[2].t = t1;
1372 verts[3].s = s0;
1373 verts[3].t = t1;
1374
1375 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1376 }
1377
1378 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1379
1380 /* Restore texture object state, the texture binding will
1381 * be restored by _mesa_meta_end().
1382 */
1383 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
1384 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
1385 if (target != GL_TEXTURE_RECTANGLE_ARB) {
1386 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
1387 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
1388 }
1389 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
1390 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
1391 if (ctx->Extensions.EXT_texture_sRGB_decode) {
1392 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, srgbSave);
1393 }
1394 if (ctx->Extensions.EXT_framebuffer_sRGB && fbo_srgb_save) {
1395 _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_TRUE);
1396 }
1397
1398 /* Done with color buffer */
1399 mask &= ~GL_COLOR_BUFFER_BIT;
1400 }
1401 }
1402
1403 return mask;
1404 }
1405
1406
1407 /**
1408 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
1409 * of texture mapping and polygon rendering.
1410 */
1411 void
1412 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
1413 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1414 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1415 GLbitfield mask, GLenum filter)
1416 {
1417 struct blit_state *blit = &ctx->Meta->Blit;
1418 struct temp_texture *tex = get_temp_texture(ctx);
1419 const GLsizei maxTexSize = tex->MaxSize;
1420 const GLint srcX = MIN2(srcX0, srcX1);
1421 const GLint srcY = MIN2(srcY0, srcY1);
1422 const GLint srcW = abs(srcX1 - srcX0);
1423 const GLint srcH = abs(srcY1 - srcY0);
1424 const GLboolean srcFlipX = srcX1 < srcX0;
1425 const GLboolean srcFlipY = srcY1 < srcY0;
1426 struct vertex {
1427 GLfloat x, y, s, t;
1428 };
1429 struct vertex verts[4];
1430 GLboolean newTex;
1431
1432 if (srcW > maxTexSize || srcH > maxTexSize) {
1433 /* XXX avoid this fallback */
1434 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1435 dstX0, dstY0, dstX1, dstY1, mask, filter);
1436 return;
1437 }
1438
1439 if (srcFlipX) {
1440 GLint tmp = dstX0;
1441 dstX0 = dstX1;
1442 dstX1 = tmp;
1443 }
1444
1445 if (srcFlipY) {
1446 GLint tmp = dstY0;
1447 dstY0 = dstY1;
1448 dstY1 = tmp;
1449 }
1450
1451 /* only scissor effects blit so save/clear all other relevant state */
1452 _mesa_meta_begin(ctx, ~MESA_META_SCISSOR);
1453
1454 if (blit->ArrayObj == 0) {
1455 /* one-time setup */
1456
1457 /* create vertex array object */
1458 _mesa_GenVertexArrays(1, &blit->ArrayObj);
1459 _mesa_BindVertexArray(blit->ArrayObj);
1460
1461 /* create vertex array buffer */
1462 _mesa_GenBuffersARB(1, &blit->VBO);
1463 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1464 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1465 NULL, GL_DYNAMIC_DRAW_ARB);
1466
1467 /* setup vertex arrays */
1468 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1469 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1470 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1471 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1472 }
1473 else {
1474 _mesa_BindVertexArray(blit->ArrayObj);
1475 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
1476 }
1477
1478 /* Try faster, direct texture approach first */
1479 mask = blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
1480 dstX0, dstY0, dstX1, dstY1, mask, filter);
1481 if (mask == 0x0) {
1482 _mesa_meta_end(ctx);
1483 return;
1484 }
1485
1486 /* Continue with "normal" approach which involves copying the src rect
1487 * into a temporary texture and is "blitted" by drawing a textured quad.
1488 */
1489
1490 newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
1491
1492 /* vertex positions/texcoords (after texture allocation!) */
1493 {
1494 verts[0].x = (GLfloat) dstX0;
1495 verts[0].y = (GLfloat) dstY0;
1496 verts[1].x = (GLfloat) dstX1;
1497 verts[1].y = (GLfloat) dstY0;
1498 verts[2].x = (GLfloat) dstX1;
1499 verts[2].y = (GLfloat) dstY1;
1500 verts[3].x = (GLfloat) dstX0;
1501 verts[3].y = (GLfloat) dstY1;
1502
1503 verts[0].s = 0.0F;
1504 verts[0].t = 0.0F;
1505 verts[1].s = tex->Sright;
1506 verts[1].t = 0.0F;
1507 verts[2].s = tex->Sright;
1508 verts[2].t = tex->Ttop;
1509 verts[3].s = 0.0F;
1510 verts[3].t = tex->Ttop;
1511
1512 /* upload new vertex data */
1513 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1514 }
1515
1516 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1517
1518 if (mask & GL_COLOR_BUFFER_BIT) {
1519 setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
1520 GL_RGBA, filter);
1521 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1522 mask &= ~GL_COLOR_BUFFER_BIT;
1523 }
1524
1525 if (mask & GL_DEPTH_BUFFER_BIT) {
1526 GLuint *tmp = (GLuint *) malloc(srcW * srcH * sizeof(GLuint));
1527 if (tmp) {
1528 if (!blit->DepthFP)
1529 init_blit_depth_pixels(ctx);
1530
1531 /* maybe change tex format here */
1532 newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
1533
1534 _mesa_ReadPixels(srcX, srcY, srcW, srcH,
1535 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1536
1537 setup_drawpix_texture(ctx, tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
1538 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1539
1540 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1541 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1542 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1543 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1544 _mesa_DepthFunc(GL_ALWAYS);
1545 _mesa_DepthMask(GL_TRUE);
1546
1547 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1548 mask &= ~GL_DEPTH_BUFFER_BIT;
1549
1550 free(tmp);
1551 }
1552 }
1553
1554 if (mask & GL_STENCIL_BUFFER_BIT) {
1555 /* XXX can't easily do stencil */
1556 }
1557
1558 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1559
1560 _mesa_meta_end(ctx);
1561
1562 if (mask) {
1563 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1564 dstX0, dstY0, dstX1, dstY1, mask, filter);
1565 }
1566 }
1567
1568
1569 /**
1570 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1571 */
1572 void
1573 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
1574 {
1575 struct clear_state *clear = &ctx->Meta->Clear;
1576 struct vertex {
1577 GLfloat x, y, z, r, g, b, a;
1578 };
1579 struct vertex verts[4];
1580 /* save all state but scissor, pixel pack/unpack */
1581 GLbitfield metaSave = (MESA_META_ALL -
1582 MESA_META_SCISSOR -
1583 MESA_META_PIXEL_STORE -
1584 MESA_META_CONDITIONAL_RENDER);
1585 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1586
1587 if (buffers & BUFFER_BITS_COLOR) {
1588 /* if clearing color buffers, don't save/restore colormask */
1589 metaSave -= MESA_META_COLOR_MASK;
1590 }
1591
1592 _mesa_meta_begin(ctx, metaSave);
1593
1594 if (clear->ArrayObj == 0) {
1595 /* one-time setup */
1596
1597 /* create vertex array object */
1598 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1599 _mesa_BindVertexArray(clear->ArrayObj);
1600
1601 /* create vertex array buffer */
1602 _mesa_GenBuffersARB(1, &clear->VBO);
1603 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1604
1605 /* setup vertex arrays */
1606 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1607 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
1608 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1609 _mesa_EnableClientState(GL_COLOR_ARRAY);
1610 }
1611 else {
1612 _mesa_BindVertexArray(clear->ArrayObj);
1613 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1614 }
1615
1616 /* GL_COLOR_BUFFER_BIT */
1617 if (buffers & BUFFER_BITS_COLOR) {
1618 /* leave colormask, glDrawBuffer state as-is */
1619
1620 /* Clears never have the color clamped. */
1621 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1622 }
1623 else {
1624 ASSERT(metaSave & MESA_META_COLOR_MASK);
1625 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1626 }
1627
1628 /* GL_DEPTH_BUFFER_BIT */
1629 if (buffers & BUFFER_BIT_DEPTH) {
1630 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1631 _mesa_DepthFunc(GL_ALWAYS);
1632 _mesa_DepthMask(GL_TRUE);
1633 }
1634 else {
1635 assert(!ctx->Depth.Test);
1636 }
1637
1638 /* GL_STENCIL_BUFFER_BIT */
1639 if (buffers & BUFFER_BIT_STENCIL) {
1640 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1641 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1642 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1643 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1644 ctx->Stencil.Clear & stencilMax,
1645 ctx->Stencil.WriteMask[0]);
1646 }
1647 else {
1648 assert(!ctx->Stencil.Enabled);
1649 }
1650
1651 /* vertex positions/colors */
1652 {
1653 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1654 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1655 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1656 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1657 const GLfloat z = invert_z(ctx->Depth.Clear);
1658 GLuint i;
1659
1660 verts[0].x = x0;
1661 verts[0].y = y0;
1662 verts[0].z = z;
1663 verts[1].x = x1;
1664 verts[1].y = y0;
1665 verts[1].z = z;
1666 verts[2].x = x1;
1667 verts[2].y = y1;
1668 verts[2].z = z;
1669 verts[3].x = x0;
1670 verts[3].y = y1;
1671 verts[3].z = z;
1672
1673 /* vertex colors */
1674 for (i = 0; i < 4; i++) {
1675 verts[i].r = ctx->Color.ClearColor.f[0];
1676 verts[i].g = ctx->Color.ClearColor.f[1];
1677 verts[i].b = ctx->Color.ClearColor.f[2];
1678 verts[i].a = ctx->Color.ClearColor.f[3];
1679 }
1680
1681 /* upload new vertex data */
1682 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1683 GL_DYNAMIC_DRAW_ARB);
1684 }
1685
1686 /* draw quad */
1687 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1688
1689 _mesa_meta_end(ctx);
1690 }
1691
1692 static void
1693 meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
1694 {
1695 const char *vs_source =
1696 "attribute vec4 position;\n"
1697 "void main()\n"
1698 "{\n"
1699 " gl_Position = position;\n"
1700 "}\n";
1701 const char *fs_source =
1702 "uniform vec4 color;\n"
1703 "void main()\n"
1704 "{\n"
1705 " gl_FragColor = color;\n"
1706 "}\n";
1707 const char *vs_int_source =
1708 "#version 130\n"
1709 "attribute vec4 position;\n"
1710 "void main()\n"
1711 "{\n"
1712 " gl_Position = position;\n"
1713 "}\n";
1714 const char *fs_int_source =
1715 "#version 130\n"
1716 "uniform ivec4 color;\n"
1717 "out ivec4 out_color;\n"
1718 "\n"
1719 "void main()\n"
1720 "{\n"
1721 " out_color = color;\n"
1722 "}\n";
1723 GLuint vs, fs;
1724
1725 if (clear->ArrayObj != 0)
1726 return;
1727
1728 /* create vertex array object */
1729 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1730 _mesa_BindVertexArray(clear->ArrayObj);
1731
1732 /* create vertex array buffer */
1733 _mesa_GenBuffersARB(1, &clear->VBO);
1734 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1735
1736 /* setup vertex arrays */
1737 _mesa_VertexAttribPointerARB(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
1738 _mesa_EnableVertexAttribArrayARB(0);
1739
1740 vs = _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER);
1741 _mesa_ShaderSourceARB(vs, 1, &vs_source, NULL);
1742 _mesa_CompileShaderARB(vs);
1743
1744 fs = _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER);
1745 _mesa_ShaderSourceARB(fs, 1, &fs_source, NULL);
1746 _mesa_CompileShaderARB(fs);
1747
1748 clear->ShaderProg = _mesa_CreateProgramObjectARB();
1749 _mesa_AttachShader(clear->ShaderProg, fs);
1750 _mesa_AttachShader(clear->ShaderProg, vs);
1751 _mesa_BindAttribLocationARB(clear->ShaderProg, 0, "position");
1752 _mesa_LinkProgramARB(clear->ShaderProg);
1753
1754 clear->ColorLocation = _mesa_GetUniformLocationARB(clear->ShaderProg,
1755 "color");
1756
1757 if (ctx->Const.GLSLVersion >= 130) {
1758 vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_int_source);
1759 fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_int_source);
1760
1761 clear->IntegerShaderProg = _mesa_CreateProgramObjectARB();
1762 _mesa_AttachShader(clear->IntegerShaderProg, fs);
1763 _mesa_AttachShader(clear->IntegerShaderProg, vs);
1764 _mesa_BindAttribLocationARB(clear->IntegerShaderProg, 0, "position");
1765
1766 /* Note that user-defined out attributes get automatically assigned
1767 * locations starting from 0, so we don't need to explicitly
1768 * BindFragDataLocation to 0.
1769 */
1770
1771 link_program_with_debug(ctx, clear->IntegerShaderProg);
1772
1773 clear->IntegerColorLocation =
1774 _mesa_GetUniformLocationARB(clear->IntegerShaderProg, "color");
1775 }
1776 }
1777
1778 /**
1779 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1780 */
1781 void
1782 _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
1783 {
1784 struct clear_state *clear = &ctx->Meta->Clear;
1785 GLbitfield metaSave;
1786 const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
1787 struct gl_framebuffer *fb = ctx->DrawBuffer;
1788 const float x0 = ((float)fb->_Xmin / fb->Width) * 2.0f - 1.0f;
1789 const float y0 = ((float)fb->_Ymin / fb->Height) * 2.0f - 1.0f;
1790 const float x1 = ((float)fb->_Xmax / fb->Width) * 2.0f - 1.0f;
1791 const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
1792 const float z = -invert_z(ctx->Depth.Clear);
1793 struct vertex {
1794 GLfloat x, y, z;
1795 } verts[4];
1796
1797 metaSave = (MESA_META_ALPHA_TEST |
1798 MESA_META_BLEND |
1799 MESA_META_DEPTH_TEST |
1800 MESA_META_RASTERIZATION |
1801 MESA_META_SHADER |
1802 MESA_META_STENCIL_TEST |
1803 MESA_META_VERTEX |
1804 MESA_META_VIEWPORT |
1805 MESA_META_CLIP |
1806 MESA_META_CLAMP_FRAGMENT_COLOR);
1807
1808 if (!(buffers & BUFFER_BITS_COLOR)) {
1809 /* We'll use colormask to disable color writes. Otherwise,
1810 * respect color mask
1811 */
1812 metaSave |= MESA_META_COLOR_MASK;
1813 }
1814
1815 _mesa_meta_begin(ctx, metaSave);
1816
1817 meta_glsl_clear_init(ctx, clear);
1818
1819 if (fb->_IntegerColor) {
1820 _mesa_UseProgramObjectARB(clear->IntegerShaderProg);
1821 _mesa_Uniform4ivARB(clear->IntegerColorLocation, 1,
1822 ctx->Color.ClearColor.i);
1823 } else {
1824 _mesa_UseProgramObjectARB(clear->ShaderProg);
1825 _mesa_Uniform4fvARB(clear->ColorLocation, 1,
1826 ctx->Color.ClearColor.f);
1827 }
1828
1829 _mesa_BindVertexArray(clear->ArrayObj);
1830 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1831
1832 /* GL_COLOR_BUFFER_BIT */
1833 if (buffers & BUFFER_BITS_COLOR) {
1834 /* leave colormask, glDrawBuffer state as-is */
1835
1836 /* Clears never have the color clamped. */
1837 _mesa_ClampColorARB(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
1838 }
1839 else {
1840 ASSERT(metaSave & MESA_META_COLOR_MASK);
1841 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1842 }
1843
1844 /* GL_DEPTH_BUFFER_BIT */
1845 if (buffers & BUFFER_BIT_DEPTH) {
1846 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1847 _mesa_DepthFunc(GL_ALWAYS);
1848 _mesa_DepthMask(GL_TRUE);
1849 }
1850 else {
1851 assert(!ctx->Depth.Test);
1852 }
1853
1854 /* GL_STENCIL_BUFFER_BIT */
1855 if (buffers & BUFFER_BIT_STENCIL) {
1856 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1857 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1858 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1859 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1860 ctx->Stencil.Clear & stencilMax,
1861 ctx->Stencil.WriteMask[0]);
1862 }
1863 else {
1864 assert(!ctx->Stencil.Enabled);
1865 }
1866
1867 /* vertex positions */
1868 verts[0].x = x0;
1869 verts[0].y = y0;
1870 verts[0].z = z;
1871 verts[1].x = x1;
1872 verts[1].y = y0;
1873 verts[1].z = z;
1874 verts[2].x = x1;
1875 verts[2].y = y1;
1876 verts[2].z = z;
1877 verts[3].x = x0;
1878 verts[3].y = y1;
1879 verts[3].z = z;
1880
1881 /* upload new vertex data */
1882 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
1883 GL_DYNAMIC_DRAW_ARB);
1884
1885 /* draw quad */
1886 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1887
1888 _mesa_meta_end(ctx);
1889 }
1890
1891 /**
1892 * Meta implementation of ctx->Driver.CopyPixels() in terms
1893 * of texture mapping and polygon rendering and GLSL shaders.
1894 */
1895 void
1896 _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
1897 GLsizei width, GLsizei height,
1898 GLint dstX, GLint dstY, GLenum type)
1899 {
1900 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1901 struct temp_texture *tex = get_temp_texture(ctx);
1902 struct vertex {
1903 GLfloat x, y, z, s, t;
1904 };
1905 struct vertex verts[4];
1906 GLboolean newTex;
1907 GLenum intFormat = GL_RGBA;
1908
1909 if (type != GL_COLOR ||
1910 ctx->_ImageTransferState ||
1911 ctx->Fog.Enabled ||
1912 width > tex->MaxSize ||
1913 height > tex->MaxSize) {
1914 /* XXX avoid this fallback */
1915 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1916 return;
1917 }
1918
1919 /* Most GL state applies to glCopyPixels, but a there's a few things
1920 * we need to override:
1921 */
1922 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
1923 MESA_META_SHADER |
1924 MESA_META_TEXTURE |
1925 MESA_META_TRANSFORM |
1926 MESA_META_CLIP |
1927 MESA_META_VERTEX |
1928 MESA_META_VIEWPORT));
1929
1930 if (copypix->ArrayObj == 0) {
1931 /* one-time setup */
1932
1933 /* create vertex array object */
1934 _mesa_GenVertexArrays(1, &copypix->ArrayObj);
1935 _mesa_BindVertexArray(copypix->ArrayObj);
1936
1937 /* create vertex array buffer */
1938 _mesa_GenBuffersARB(1, &copypix->VBO);
1939 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1940 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1941 NULL, GL_DYNAMIC_DRAW_ARB);
1942
1943 /* setup vertex arrays */
1944 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
1945 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
1946 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1947 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1948 }
1949 else {
1950 _mesa_BindVertexArray(copypix->ArrayObj);
1951 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1952 }
1953
1954 newTex = alloc_texture(tex, width, height, intFormat);
1955
1956 /* vertex positions, texcoords (after texture allocation!) */
1957 {
1958 const GLfloat dstX0 = (GLfloat) dstX;
1959 const GLfloat dstY0 = (GLfloat) dstY;
1960 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1961 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1962 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
1963
1964 verts[0].x = dstX0;
1965 verts[0].y = dstY0;
1966 verts[0].z = z;
1967 verts[0].s = 0.0F;
1968 verts[0].t = 0.0F;
1969 verts[1].x = dstX1;
1970 verts[1].y = dstY0;
1971 verts[1].z = z;
1972 verts[1].s = tex->Sright;
1973 verts[1].t = 0.0F;
1974 verts[2].x = dstX1;
1975 verts[2].y = dstY1;
1976 verts[2].z = z;
1977 verts[2].s = tex->Sright;
1978 verts[2].t = tex->Ttop;
1979 verts[3].x = dstX0;
1980 verts[3].y = dstY1;
1981 verts[3].z = z;
1982 verts[3].s = 0.0F;
1983 verts[3].t = tex->Ttop;
1984
1985 /* upload new vertex data */
1986 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1987 }
1988
1989 /* Alloc/setup texture */
1990 setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
1991 GL_RGBA, GL_NEAREST);
1992
1993 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
1994
1995 /* draw textured quad */
1996 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1997
1998 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
1999
2000 _mesa_meta_end(ctx);
2001 }
2002
2003
2004
2005 /**
2006 * When the glDrawPixels() image size is greater than the max rectangle
2007 * texture size we use this function to break the glDrawPixels() image
2008 * into tiles which fit into the max texture size.
2009 */
2010 static void
2011 tiled_draw_pixels(struct gl_context *ctx,
2012 GLint tileSize,
2013 GLint x, GLint y, GLsizei width, GLsizei height,
2014 GLenum format, GLenum type,
2015 const struct gl_pixelstore_attrib *unpack,
2016 const GLvoid *pixels)
2017 {
2018 struct gl_pixelstore_attrib tileUnpack = *unpack;
2019 GLint i, j;
2020
2021 if (tileUnpack.RowLength == 0)
2022 tileUnpack.RowLength = width;
2023
2024 for (i = 0; i < width; i += tileSize) {
2025 const GLint tileWidth = MIN2(tileSize, width - i);
2026 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
2027
2028 tileUnpack.SkipPixels = unpack->SkipPixels + i;
2029
2030 for (j = 0; j < height; j += tileSize) {
2031 const GLint tileHeight = MIN2(tileSize, height - j);
2032 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
2033
2034 tileUnpack.SkipRows = unpack->SkipRows + j;
2035
2036 _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
2037 format, type, &tileUnpack, pixels);
2038 }
2039 }
2040 }
2041
2042
2043 /**
2044 * One-time init for drawing stencil pixels.
2045 */
2046 static void
2047 init_draw_stencil_pixels(struct gl_context *ctx)
2048 {
2049 /* This program is run eight times, once for each stencil bit.
2050 * The stencil values to draw are found in an 8-bit alpha texture.
2051 * We read the texture/stencil value and test if bit 'b' is set.
2052 * If the bit is not set, use KIL to kill the fragment.
2053 * Finally, we use the stencil test to update the stencil buffer.
2054 *
2055 * The basic algorithm for checking if a bit is set is:
2056 * if (is_odd(value / (1 << bit)))
2057 * result is one (or non-zero).
2058 * else
2059 * result is zero.
2060 * The program parameter contains three values:
2061 * parm.x = 255 / (1 << bit)
2062 * parm.y = 0.5
2063 * parm.z = 0.0
2064 */
2065 static const char *program =
2066 "!!ARBfp1.0\n"
2067 "PARAM parm = program.local[0]; \n"
2068 "TEMP t; \n"
2069 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
2070 "# t = t * 255 / bit \n"
2071 "MUL t.x, t.a, parm.x; \n"
2072 "# t = (int) t \n"
2073 "FRC t.y, t.x; \n"
2074 "SUB t.x, t.x, t.y; \n"
2075 "# t = t * 0.5 \n"
2076 "MUL t.x, t.x, parm.y; \n"
2077 "# t = fract(t.x) \n"
2078 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
2079 "# t.x = (t.x == 0 ? 1 : 0) \n"
2080 "SGE t.x, -t.x, parm.z; \n"
2081 "KIL -t.x; \n"
2082 "# for debug only \n"
2083 "#MOV result.color, t.x; \n"
2084 "END \n";
2085 char program2[1000];
2086 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2087 struct temp_texture *tex = get_temp_texture(ctx);
2088 const char *texTarget;
2089
2090 assert(drawpix->StencilFP == 0);
2091
2092 /* replace %s with "RECT" or "2D" */
2093 assert(strlen(program) + 4 < sizeof(program2));
2094 if (tex->Target == GL_TEXTURE_RECTANGLE)
2095 texTarget = "RECT";
2096 else
2097 texTarget = "2D";
2098 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2099
2100 _mesa_GenPrograms(1, &drawpix->StencilFP);
2101 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2102 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2103 strlen(program2), (const GLubyte *) program2);
2104 }
2105
2106
2107 /**
2108 * One-time init for drawing depth pixels.
2109 */
2110 static void
2111 init_draw_depth_pixels(struct gl_context *ctx)
2112 {
2113 static const char *program =
2114 "!!ARBfp1.0\n"
2115 "PARAM color = program.local[0]; \n"
2116 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
2117 "MOV result.color, color; \n"
2118 "END \n";
2119 char program2[200];
2120 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2121 struct temp_texture *tex = get_temp_texture(ctx);
2122 const char *texTarget;
2123
2124 assert(drawpix->DepthFP == 0);
2125
2126 /* replace %s with "RECT" or "2D" */
2127 assert(strlen(program) + 4 < sizeof(program2));
2128 if (tex->Target == GL_TEXTURE_RECTANGLE)
2129 texTarget = "RECT";
2130 else
2131 texTarget = "2D";
2132 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
2133
2134 _mesa_GenPrograms(1, &drawpix->DepthFP);
2135 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2136 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
2137 strlen(program2), (const GLubyte *) program2);
2138 }
2139
2140
2141 /**
2142 * Meta implementation of ctx->Driver.DrawPixels() in terms
2143 * of texture mapping and polygon rendering.
2144 */
2145 void
2146 _mesa_meta_DrawPixels(struct gl_context *ctx,
2147 GLint x, GLint y, GLsizei width, GLsizei height,
2148 GLenum format, GLenum type,
2149 const struct gl_pixelstore_attrib *unpack,
2150 const GLvoid *pixels)
2151 {
2152 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
2153 struct temp_texture *tex = get_temp_texture(ctx);
2154 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
2155 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
2156 struct vertex {
2157 GLfloat x, y, z, s, t;
2158 };
2159 struct vertex verts[4];
2160 GLenum texIntFormat;
2161 GLboolean fallback, newTex;
2162 GLbitfield metaExtraSave = 0x0;
2163 GLuint vbo;
2164
2165 /*
2166 * Determine if we can do the glDrawPixels with texture mapping.
2167 */
2168 fallback = GL_FALSE;
2169 if (ctx->_ImageTransferState ||
2170 ctx->Fog.Enabled) {
2171 fallback = GL_TRUE;
2172 }
2173
2174 if (_mesa_is_color_format(format)) {
2175 /* use more compact format when possible */
2176 /* XXX disable special case for GL_LUMINANCE for now to work around
2177 * apparent i965 driver bug (see bug #23670).
2178 */
2179 if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
2180 texIntFormat = format;
2181 else
2182 texIntFormat = GL_RGBA;
2183
2184 /* If we're not supposed to clamp the resulting color, then just
2185 * promote our texture to fully float. We could do better by
2186 * just going for the matching set of channels, in floating
2187 * point.
2188 */
2189 if (ctx->Color.ClampFragmentColor != GL_TRUE &&
2190 ctx->Extensions.ARB_texture_float)
2191 texIntFormat = GL_RGBA32F;
2192 }
2193 else if (_mesa_is_stencil_format(format)) {
2194 if (ctx->Extensions.ARB_fragment_program &&
2195 ctx->Pixel.IndexShift == 0 &&
2196 ctx->Pixel.IndexOffset == 0 &&
2197 type == GL_UNSIGNED_BYTE) {
2198 /* We'll store stencil as alpha. This only works for GLubyte
2199 * image data because of how incoming values are mapped to alpha
2200 * in [0,1].
2201 */
2202 texIntFormat = GL_ALPHA;
2203 metaExtraSave = (MESA_META_COLOR_MASK |
2204 MESA_META_DEPTH_TEST |
2205 MESA_META_SHADER |
2206 MESA_META_STENCIL_TEST);
2207 }
2208 else {
2209 fallback = GL_TRUE;
2210 }
2211 }
2212 else if (_mesa_is_depth_format(format)) {
2213 if (ctx->Extensions.ARB_depth_texture &&
2214 ctx->Extensions.ARB_fragment_program) {
2215 texIntFormat = GL_DEPTH_COMPONENT;
2216 metaExtraSave = (MESA_META_SHADER);
2217 }
2218 else {
2219 fallback = GL_TRUE;
2220 }
2221 }
2222 else {
2223 fallback = GL_TRUE;
2224 }
2225
2226 if (fallback) {
2227 _swrast_DrawPixels(ctx, x, y, width, height,
2228 format, type, unpack, pixels);
2229 return;
2230 }
2231
2232 /*
2233 * Check image size against max texture size, draw as tiles if needed.
2234 */
2235 if (width > tex->MaxSize || height > tex->MaxSize) {
2236 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
2237 format, type, unpack, pixels);
2238 return;
2239 }
2240
2241 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
2242 * but a there's a few things we need to override:
2243 */
2244 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
2245 MESA_META_SHADER |
2246 MESA_META_TEXTURE |
2247 MESA_META_TRANSFORM |
2248 MESA_META_CLIP |
2249 MESA_META_VERTEX |
2250 MESA_META_VIEWPORT |
2251 MESA_META_CLAMP_FRAGMENT_COLOR |
2252 metaExtraSave));
2253
2254 newTex = alloc_texture(tex, width, height, texIntFormat);
2255
2256 /* vertex positions, texcoords (after texture allocation!) */
2257 {
2258 const GLfloat x0 = (GLfloat) x;
2259 const GLfloat y0 = (GLfloat) y;
2260 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
2261 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
2262 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2263
2264 verts[0].x = x0;
2265 verts[0].y = y0;
2266 verts[0].z = z;
2267 verts[0].s = 0.0F;
2268 verts[0].t = 0.0F;
2269 verts[1].x = x1;
2270 verts[1].y = y0;
2271 verts[1].z = z;
2272 verts[1].s = tex->Sright;
2273 verts[1].t = 0.0F;
2274 verts[2].x = x1;
2275 verts[2].y = y1;
2276 verts[2].z = z;
2277 verts[2].s = tex->Sright;
2278 verts[2].t = tex->Ttop;
2279 verts[3].x = x0;
2280 verts[3].y = y1;
2281 verts[3].z = z;
2282 verts[3].s = 0.0F;
2283 verts[3].t = tex->Ttop;
2284 }
2285
2286 if (drawpix->ArrayObj == 0) {
2287 /* one-time setup: create vertex array object */
2288 _mesa_GenVertexArrays(1, &drawpix->ArrayObj);
2289 }
2290 _mesa_BindVertexArray(drawpix->ArrayObj);
2291
2292 /* create vertex array buffer */
2293 _mesa_GenBuffersARB(1, &vbo);
2294 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
2295 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2296 verts, GL_DYNAMIC_DRAW_ARB);
2297
2298 /* setup vertex arrays */
2299 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2300 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2301 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2302 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2303
2304 /* set given unpack params */
2305 ctx->Unpack = *unpack;
2306
2307 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2308
2309 if (_mesa_is_stencil_format(format)) {
2310 /* Drawing stencil */
2311 GLint bit;
2312
2313 if (!drawpix->StencilFP)
2314 init_draw_stencil_pixels(ctx);
2315
2316 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2317 GL_ALPHA, type, pixels);
2318
2319 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2320
2321 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
2322
2323 /* set all stencil bits to 0 */
2324 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2325 _mesa_StencilFunc(GL_ALWAYS, 0, 255);
2326 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2327
2328 /* set stencil bits to 1 where needed */
2329 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2330
2331 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
2332 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2333
2334 for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
2335 const GLuint mask = 1 << bit;
2336 if (mask & origStencilMask) {
2337 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
2338 _mesa_StencilMask(mask);
2339
2340 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2341 255.0 / mask, 0.5, 0.0, 0.0);
2342
2343 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2344 }
2345 }
2346 }
2347 else if (_mesa_is_depth_format(format)) {
2348 /* Drawing depth */
2349 if (!drawpix->DepthFP)
2350 init_draw_depth_pixels(ctx);
2351
2352 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
2353 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
2354
2355 /* polygon color = current raster color */
2356 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
2357 ctx->Current.RasterColor);
2358
2359 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2360 format, type, pixels);
2361
2362 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2363 }
2364 else {
2365 /* Drawing RGBA */
2366 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2367 format, type, pixels);
2368 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2369 }
2370
2371 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2372
2373 _mesa_DeleteBuffersARB(1, &vbo);
2374
2375 /* restore unpack params */
2376 ctx->Unpack = unpackSave;
2377
2378 _mesa_meta_end(ctx);
2379 }
2380
2381 static GLboolean
2382 alpha_test_raster_color(struct gl_context *ctx)
2383 {
2384 GLfloat alpha = ctx->Current.RasterColor[ACOMP];
2385 GLfloat ref = ctx->Color.AlphaRef;
2386
2387 switch (ctx->Color.AlphaFunc) {
2388 case GL_NEVER:
2389 return GL_FALSE;
2390 case GL_LESS:
2391 return alpha < ref;
2392 case GL_EQUAL:
2393 return alpha == ref;
2394 case GL_LEQUAL:
2395 return alpha <= ref;
2396 case GL_GREATER:
2397 return alpha > ref;
2398 case GL_NOTEQUAL:
2399 return alpha != ref;
2400 case GL_GEQUAL:
2401 return alpha >= ref;
2402 case GL_ALWAYS:
2403 return GL_TRUE;
2404 default:
2405 assert(0);
2406 return GL_FALSE;
2407 }
2408 }
2409
2410 /**
2411 * Do glBitmap with a alpha texture quad. Use the alpha test to cull
2412 * the 'off' bits. A bitmap cache as in the gallium/mesa state
2413 * tracker would improve performance a lot.
2414 */
2415 void
2416 _mesa_meta_Bitmap(struct gl_context *ctx,
2417 GLint x, GLint y, GLsizei width, GLsizei height,
2418 const struct gl_pixelstore_attrib *unpack,
2419 const GLubyte *bitmap1)
2420 {
2421 struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
2422 struct temp_texture *tex = get_bitmap_temp_texture(ctx);
2423 const GLenum texIntFormat = GL_ALPHA;
2424 const struct gl_pixelstore_attrib unpackSave = *unpack;
2425 GLubyte fg, bg;
2426 struct vertex {
2427 GLfloat x, y, z, s, t, r, g, b, a;
2428 };
2429 struct vertex verts[4];
2430 GLboolean newTex;
2431 GLubyte *bitmap8;
2432
2433 /*
2434 * Check if swrast fallback is needed.
2435 */
2436 if (ctx->_ImageTransferState ||
2437 ctx->FragmentProgram._Enabled ||
2438 ctx->Fog.Enabled ||
2439 ctx->Texture._EnabledUnits ||
2440 width > tex->MaxSize ||
2441 height > tex->MaxSize) {
2442 _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
2443 return;
2444 }
2445
2446 if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
2447 return;
2448
2449 /* Most GL state applies to glBitmap (like blending, stencil, etc),
2450 * but a there's a few things we need to override:
2451 */
2452 _mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
2453 MESA_META_PIXEL_STORE |
2454 MESA_META_RASTERIZATION |
2455 MESA_META_SHADER |
2456 MESA_META_TEXTURE |
2457 MESA_META_TRANSFORM |
2458 MESA_META_CLIP |
2459 MESA_META_VERTEX |
2460 MESA_META_VIEWPORT));
2461
2462 if (bitmap->ArrayObj == 0) {
2463 /* one-time setup */
2464
2465 /* create vertex array object */
2466 _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
2467 _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
2468
2469 /* create vertex array buffer */
2470 _mesa_GenBuffersARB(1, &bitmap->VBO);
2471 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2472 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2473 NULL, GL_DYNAMIC_DRAW_ARB);
2474
2475 /* setup vertex arrays */
2476 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2477 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
2478 _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
2479 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2480 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2481 _mesa_EnableClientState(GL_COLOR_ARRAY);
2482 }
2483 else {
2484 _mesa_BindVertexArray(bitmap->ArrayObj);
2485 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
2486 }
2487
2488 newTex = alloc_texture(tex, width, height, texIntFormat);
2489
2490 /* vertex positions, texcoords, colors (after texture allocation!) */
2491 {
2492 const GLfloat x0 = (GLfloat) x;
2493 const GLfloat y0 = (GLfloat) y;
2494 const GLfloat x1 = (GLfloat) (x + width);
2495 const GLfloat y1 = (GLfloat) (y + height);
2496 const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
2497 GLuint i;
2498
2499 verts[0].x = x0;
2500 verts[0].y = y0;
2501 verts[0].z = z;
2502 verts[0].s = 0.0F;
2503 verts[0].t = 0.0F;
2504 verts[1].x = x1;
2505 verts[1].y = y0;
2506 verts[1].z = z;
2507 verts[1].s = tex->Sright;
2508 verts[1].t = 0.0F;
2509 verts[2].x = x1;
2510 verts[2].y = y1;
2511 verts[2].z = z;
2512 verts[2].s = tex->Sright;
2513 verts[2].t = tex->Ttop;
2514 verts[3].x = x0;
2515 verts[3].y = y1;
2516 verts[3].z = z;
2517 verts[3].s = 0.0F;
2518 verts[3].t = tex->Ttop;
2519
2520 for (i = 0; i < 4; i++) {
2521 verts[i].r = ctx->Current.RasterColor[0];
2522 verts[i].g = ctx->Current.RasterColor[1];
2523 verts[i].b = ctx->Current.RasterColor[2];
2524 verts[i].a = ctx->Current.RasterColor[3];
2525 }
2526
2527 /* upload new vertex data */
2528 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2529 }
2530
2531 /* choose different foreground/background alpha values */
2532 CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
2533 bg = (fg > 127 ? 0 : 255);
2534
2535 bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
2536 if (!bitmap1) {
2537 _mesa_meta_end(ctx);
2538 return;
2539 }
2540
2541 bitmap8 = (GLubyte *) malloc(width * height);
2542 if (bitmap8) {
2543 memset(bitmap8, bg, width * height);
2544 _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
2545 bitmap8, width, fg);
2546
2547 _mesa_set_enable(ctx, tex->Target, GL_TRUE);
2548
2549 _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
2550 _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
2551
2552 setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
2553 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
2554
2555 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
2556
2557 _mesa_set_enable(ctx, tex->Target, GL_FALSE);
2558
2559 free(bitmap8);
2560 }
2561
2562 _mesa_unmap_pbo_source(ctx, &unpackSave);
2563
2564 _mesa_meta_end(ctx);
2565 }
2566
2567
2568 /**
2569 * Check if the call to _mesa_meta_GenerateMipmap() will require a
2570 * software fallback. The fallback path will require that the texture
2571 * images are mapped.
2572 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
2573 */
2574 GLboolean
2575 _mesa_meta_check_generate_mipmap_fallback(struct gl_context *ctx, GLenum target,
2576 struct gl_texture_object *texObj)
2577 {
2578 const GLuint fboSave = ctx->DrawBuffer->Name;
2579 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2580 struct gl_texture_image *baseImage;
2581 GLuint srcLevel;
2582 GLenum status;
2583
2584 /* check for fallbacks */
2585 if (!ctx->Extensions.EXT_framebuffer_object ||
2586 target == GL_TEXTURE_3D ||
2587 target == GL_TEXTURE_1D_ARRAY ||
2588 target == GL_TEXTURE_2D_ARRAY) {
2589 return GL_TRUE;
2590 }
2591
2592 srcLevel = texObj->BaseLevel;
2593 baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
2594 if (!baseImage || _mesa_is_format_compressed(baseImage->TexFormat)) {
2595 return GL_TRUE;
2596 }
2597
2598 if (_mesa_get_format_color_encoding(baseImage->TexFormat) == GL_SRGB &&
2599 !ctx->Extensions.EXT_texture_sRGB_decode) {
2600 /* The texture format is sRGB but we can't turn off sRGB->linear
2601 * texture sample conversion. So we won't be able to generate the
2602 * right colors when rendering. Need to use a fallback.
2603 */
2604 return GL_TRUE;
2605 }
2606
2607 /*
2608 * Test that we can actually render in the texture's format.
2609 */
2610 if (!mipmap->FBO)
2611 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2612 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2613
2614 if (target == GL_TEXTURE_1D) {
2615 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2616 GL_COLOR_ATTACHMENT0_EXT,
2617 target, texObj->Name, srcLevel);
2618 }
2619 #if 0
2620 /* other work is needed to enable 3D mipmap generation */
2621 else if (target == GL_TEXTURE_3D) {
2622 GLint zoffset = 0;
2623 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2624 GL_COLOR_ATTACHMENT0_EXT,
2625 target, texObj->Name, srcLevel, zoffset);
2626 }
2627 #endif
2628 else {
2629 /* 2D / cube */
2630 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2631 GL_COLOR_ATTACHMENT0_EXT,
2632 target, texObj->Name, srcLevel);
2633 }
2634
2635 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
2636
2637 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
2638
2639 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2640 return GL_TRUE;
2641 }
2642
2643 return GL_FALSE;
2644 }
2645
2646
2647 /**
2648 * Compute the texture coordinates for the four vertices of a quad for
2649 * drawing a 2D texture image or slice of a cube/3D texture.
2650 * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
2651 * \param slice slice of a 1D/2D array texture or 3D texture
2652 * \param width width of the texture image
2653 * \param height height of the texture image
2654 * \param coords0/1/2/3 returns the computed texcoords
2655 */
2656 static void
2657 setup_texture_coords(GLenum faceTarget,
2658 GLint slice,
2659 GLint width,
2660 GLint height,
2661 GLfloat coords0[3],
2662 GLfloat coords1[3],
2663 GLfloat coords2[3],
2664 GLfloat coords3[3])
2665 {
2666 static const GLfloat st[4][2] = {
2667 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
2668 };
2669 GLuint i;
2670 GLfloat r;
2671
2672 switch (faceTarget) {
2673 case GL_TEXTURE_1D:
2674 case GL_TEXTURE_2D:
2675 case GL_TEXTURE_3D:
2676 case GL_TEXTURE_2D_ARRAY:
2677 if (faceTarget == GL_TEXTURE_3D)
2678 r = 1.0F / slice;
2679 else if (faceTarget == GL_TEXTURE_2D_ARRAY)
2680 r = slice;
2681 else
2682 r = 0.0F;
2683 coords0[0] = 0.0F; /* s */
2684 coords0[1] = 0.0F; /* t */
2685 coords0[2] = r; /* r */
2686 coords1[0] = 1.0F;
2687 coords1[1] = 0.0F;
2688 coords1[2] = r;
2689 coords2[0] = 1.0F;
2690 coords2[1] = 1.0F;
2691 coords2[2] = r;
2692 coords3[0] = 0.0F;
2693 coords3[1] = 1.0F;
2694 coords3[2] = r;
2695 break;
2696 case GL_TEXTURE_RECTANGLE_ARB:
2697 coords0[0] = 0.0F; /* s */
2698 coords0[1] = 0.0F; /* t */
2699 coords0[2] = 0.0F; /* r */
2700 coords1[0] = width;
2701 coords1[1] = 0.0F;
2702 coords1[2] = 0.0F;
2703 coords2[0] = width;
2704 coords2[1] = height;
2705 coords2[2] = 0.0F;
2706 coords3[0] = 0.0F;
2707 coords3[1] = height;
2708 coords3[2] = 0.0F;
2709 break;
2710 case GL_TEXTURE_1D_ARRAY:
2711 coords0[0] = 0.0F; /* s */
2712 coords0[1] = slice; /* t */
2713 coords0[2] = 0.0F; /* r */
2714 coords1[0] = 1.0f;
2715 coords1[1] = slice;
2716 coords1[2] = 0.0F;
2717 coords2[0] = 1.0F;
2718 coords2[1] = slice;
2719 coords2[2] = 0.0F;
2720 coords3[0] = 0.0F;
2721 coords3[1] = slice;
2722 coords3[2] = 0.0F;
2723 break;
2724
2725 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2726 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2727 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2728 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2729 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2730 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2731 /* loop over quad verts */
2732 for (i = 0; i < 4; i++) {
2733 /* Compute sc = +/-scale and tc = +/-scale.
2734 * Not +/-1 to avoid cube face selection ambiguity near the edges,
2735 * though that can still sometimes happen with this scale factor...
2736 */
2737 const GLfloat scale = 0.9999f;
2738 const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
2739 const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
2740 GLfloat *coord;
2741
2742 switch (i) {
2743 case 0:
2744 coord = coords0;
2745 break;
2746 case 1:
2747 coord = coords1;
2748 break;
2749 case 2:
2750 coord = coords2;
2751 break;
2752 case 3:
2753 coord = coords3;
2754 break;
2755 default:
2756 assert(0);
2757 }
2758
2759 switch (faceTarget) {
2760 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2761 coord[0] = 1.0f;
2762 coord[1] = -tc;
2763 coord[2] = -sc;
2764 break;
2765 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2766 coord[0] = -1.0f;
2767 coord[1] = -tc;
2768 coord[2] = sc;
2769 break;
2770 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2771 coord[0] = sc;
2772 coord[1] = 1.0f;
2773 coord[2] = tc;
2774 break;
2775 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2776 coord[0] = sc;
2777 coord[1] = -1.0f;
2778 coord[2] = -tc;
2779 break;
2780 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2781 coord[0] = sc;
2782 coord[1] = -tc;
2783 coord[2] = 1.0f;
2784 break;
2785 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2786 coord[0] = -sc;
2787 coord[1] = -tc;
2788 coord[2] = -1.0f;
2789 break;
2790 default:
2791 assert(0);
2792 }
2793 }
2794 break;
2795 default:
2796 assert(0 && "unexpected target in meta setup_texture_coords()");
2797 }
2798 }
2799
2800
2801 /**
2802 * Called via ctx->Driver.GenerateMipmap()
2803 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
2804 * borders.
2805 */
2806 void
2807 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
2808 struct gl_texture_object *texObj)
2809 {
2810 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
2811 struct vertex {
2812 GLfloat x, y, tex[3];
2813 };
2814 struct vertex verts[4];
2815 const GLuint baseLevel = texObj->BaseLevel;
2816 const GLuint maxLevel = texObj->MaxLevel;
2817 const GLenum minFilterSave = texObj->Sampler.MinFilter;
2818 const GLenum magFilterSave = texObj->Sampler.MagFilter;
2819 const GLint maxLevelSave = texObj->MaxLevel;
2820 const GLboolean genMipmapSave = texObj->GenerateMipmap;
2821 const GLenum wrapSSave = texObj->Sampler.WrapS;
2822 const GLenum wrapTSave = texObj->Sampler.WrapT;
2823 const GLenum wrapRSave = texObj->Sampler.WrapR;
2824 const GLenum srgbDecodeSave = texObj->Sampler.sRGBDecode;
2825 const GLenum srgbBufferSave = ctx->Color.sRGBEnabled;
2826 const GLuint fboSave = ctx->DrawBuffer->Name;
2827 const GLuint original_active_unit = ctx->Texture.CurrentUnit;
2828 GLenum faceTarget;
2829 GLuint dstLevel;
2830 const GLuint border = 0;
2831 const GLint slice = 0;
2832
2833 if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
2834 _mesa_generate_mipmap(ctx, target, texObj);
2835 return;
2836 }
2837
2838 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
2839 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
2840 faceTarget = target;
2841 target = GL_TEXTURE_CUBE_MAP;
2842 }
2843 else {
2844 faceTarget = target;
2845 }
2846
2847 _mesa_meta_begin(ctx, MESA_META_ALL);
2848
2849 if (original_active_unit != 0)
2850 _mesa_BindTexture(target, texObj->Name);
2851
2852 if (mipmap->ArrayObj == 0) {
2853 /* one-time setup */
2854
2855 /* create vertex array object */
2856 _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
2857 _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
2858
2859 /* create vertex array buffer */
2860 _mesa_GenBuffersARB(1, &mipmap->VBO);
2861 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2862 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
2863 NULL, GL_DYNAMIC_DRAW_ARB);
2864
2865 /* setup vertex arrays */
2866 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
2867 _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
2868 _mesa_EnableClientState(GL_VERTEX_ARRAY);
2869 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
2870 }
2871 else {
2872 _mesa_BindVertexArray(mipmap->ArrayObj);
2873 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
2874 }
2875
2876 if (!mipmap->FBO) {
2877 _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
2878 }
2879 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
2880
2881 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2882 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2883 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
2884 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2885 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2886 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2887
2888 /* We don't want to encode or decode sRGB values; treat them as linear */
2889 if (ctx->Extensions.EXT_texture_sRGB_decode) {
2890 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
2891 GL_SKIP_DECODE_EXT);
2892 }
2893 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2894 _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_FALSE);
2895 }
2896
2897 _mesa_set_enable(ctx, target, GL_TRUE);
2898
2899 /* setup texcoords (XXX what about border?) */
2900 setup_texture_coords(faceTarget,
2901 0.0, 0.0, /* width, height never used here */
2902 slice,
2903 verts[0].tex,
2904 verts[1].tex,
2905 verts[2].tex,
2906 verts[3].tex);
2907
2908 /* setup vertex positions */
2909 verts[0].x = 0.0F;
2910 verts[0].y = 0.0F;
2911 verts[1].x = 1.0F;
2912 verts[1].y = 0.0F;
2913 verts[2].x = 1.0F;
2914 verts[2].y = 1.0F;
2915 verts[3].x = 0.0F;
2916 verts[3].y = 1.0F;
2917
2918 /* upload new vertex data */
2919 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
2920
2921 /* setup projection matrix */
2922 _mesa_MatrixMode(GL_PROJECTION);
2923 _mesa_LoadIdentity();
2924 _mesa_Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
2925
2926 /* texture is already locked, unlock now */
2927 _mesa_unlock_texture(ctx, texObj);
2928
2929 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
2930 const struct gl_texture_image *srcImage;
2931 const GLuint srcLevel = dstLevel - 1;
2932 GLsizei srcWidth, srcHeight, srcDepth;
2933 GLsizei dstWidth, dstHeight, dstDepth;
2934 GLenum status;
2935
2936 srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
2937 assert(srcImage->Border == 0); /* XXX we can fix this */
2938
2939 /* src size w/out border */
2940 srcWidth = srcImage->Width - 2 * border;
2941 srcHeight = srcImage->Height - 2 * border;
2942 srcDepth = srcImage->Depth - 2 * border;
2943
2944 /* new dst size w/ border */
2945 dstWidth = MAX2(1, srcWidth / 2) + 2 * border;
2946 dstHeight = MAX2(1, srcHeight / 2) + 2 * border;
2947 dstDepth = MAX2(1, srcDepth / 2) + 2 * border;
2948
2949 if (dstWidth == srcImage->Width &&
2950 dstHeight == srcImage->Height &&
2951 dstDepth == srcImage->Depth) {
2952 /* all done */
2953 break;
2954 }
2955
2956 /* Allocate storage for the destination mipmap image(s) */
2957
2958 /* Set MaxLevel large enough to hold the new level when we allocate it */
2959 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
2960
2961 if (!_mesa_prepare_mipmap_level(ctx, texObj, dstLevel,
2962 dstWidth, dstHeight, dstDepth,
2963 srcImage->Border,
2964 srcImage->InternalFormat,
2965 srcImage->TexFormat)) {
2966 /* All done. We either ran out of memory or we would go beyond the
2967 * last valid level of an immutable texture if we continued.
2968 */
2969 break;
2970 }
2971
2972 /* limit minification to src level */
2973 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
2974
2975 /* Set to draw into the current dstLevel */
2976 if (target == GL_TEXTURE_1D) {
2977 _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT,
2978 GL_COLOR_ATTACHMENT0_EXT,
2979 target,
2980 texObj->Name,
2981 dstLevel);
2982 }
2983 else if (target == GL_TEXTURE_3D) {
2984 GLint zoffset = 0; /* XXX unfinished */
2985 _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
2986 GL_COLOR_ATTACHMENT0_EXT,
2987 target,
2988 texObj->Name,
2989 dstLevel, zoffset);
2990 }
2991 else {
2992 /* 2D / cube */
2993 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
2994 GL_COLOR_ATTACHMENT0_EXT,
2995 faceTarget,
2996 texObj->Name,
2997 dstLevel);
2998 }
2999
3000 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
3001
3002 /* sanity check */
3003 status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
3004 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
3005 abort();
3006 break;
3007 }
3008
3009 assert(dstWidth == ctx->DrawBuffer->Width);
3010 assert(dstHeight == ctx->DrawBuffer->Height);
3011
3012 /* setup viewport */
3013 _mesa_set_viewport(ctx, 0, 0, dstWidth, dstHeight);
3014
3015 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3016 }
3017
3018 if (ctx->Extensions.EXT_texture_sRGB_decode) {
3019 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
3020 srgbDecodeSave);
3021 }
3022 if (ctx->Extensions.EXT_framebuffer_sRGB && srgbBufferSave) {
3023 _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_TRUE);
3024 }
3025
3026 _mesa_lock_texture(ctx, texObj); /* relock */
3027
3028 _mesa_meta_end(ctx);
3029
3030 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
3031 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
3032 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
3033 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
3034 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
3035 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
3036 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, wrapRSave);
3037
3038 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
3039 }
3040
3041
3042 /**
3043 * Determine the GL data type to use for the temporary image read with
3044 * ReadPixels() and passed to Tex[Sub]Image().
3045 */
3046 static GLenum
3047 get_temp_image_type(struct gl_context *ctx, GLenum baseFormat)
3048 {
3049 switch (baseFormat) {
3050 case GL_RGBA:
3051 case GL_RGB:
3052 case GL_ALPHA:
3053 case GL_LUMINANCE:
3054 case GL_LUMINANCE_ALPHA:
3055 case GL_INTENSITY:
3056 if (ctx->DrawBuffer->Visual.redBits <= 8)
3057 return GL_UNSIGNED_BYTE;
3058 else if (ctx->DrawBuffer->Visual.redBits <= 16)
3059 return GL_UNSIGNED_SHORT;
3060 else
3061 return GL_FLOAT;
3062 case GL_DEPTH_COMPONENT:
3063 return GL_UNSIGNED_INT;
3064 case GL_DEPTH_STENCIL:
3065 return GL_UNSIGNED_INT_24_8;
3066 default:
3067 _mesa_problem(ctx, "Unexpected format in get_temp_image_type()");
3068 return 0;
3069 }
3070 }
3071
3072
3073 /**
3074 * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
3075 * Have to be careful with locking and meta state for pixel transfer.
3076 */
3077 static void
3078 copy_tex_sub_image(struct gl_context *ctx,
3079 GLuint dims, GLenum target, GLint level,
3080 GLint xoffset, GLint yoffset, GLint zoffset,
3081 GLint x, GLint y,
3082 GLsizei width, GLsizei height)
3083 {
3084 struct gl_texture_object *texObj;
3085 struct gl_texture_image *texImage;
3086 GLenum format, type;
3087 GLint bpp;
3088 void *buf;
3089
3090 texObj = _mesa_get_current_tex_object(ctx, target);
3091 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3092
3093 /* Choose format/type for temporary image buffer */
3094 format = _mesa_get_format_base_format(texImage->TexFormat);
3095 if (format == GL_LUMINANCE ||
3096 format == GL_LUMINANCE_ALPHA ||
3097 format == GL_INTENSITY) {
3098 /* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
3099 * temp image buffer because glReadPixels will do L=R+G+B which is
3100 * not what we want (should be L=R).
3101 */
3102 format = GL_RGBA;
3103 }
3104
3105 type = get_temp_image_type(ctx, format);
3106 bpp = _mesa_bytes_per_pixel(format, type);
3107 if (bpp <= 0) {
3108 _mesa_problem(ctx, "Bad bpp in meta copy_tex_sub_image()");
3109 return;
3110 }
3111
3112 /*
3113 * Alloc image buffer (XXX could use a PBO)
3114 */
3115 buf = malloc(width * height * bpp);
3116 if (!buf) {
3117 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
3118 return;
3119 }
3120
3121 _mesa_unlock_texture(ctx, texObj); /* need to unlock first */
3122
3123 /*
3124 * Read image from framebuffer (disable pixel transfer ops)
3125 */
3126 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
3127 ctx->Driver.ReadPixels(ctx, x, y, width, height,
3128 format, type, &ctx->Pack, buf);
3129 _mesa_meta_end(ctx);
3130
3131 _mesa_update_state(ctx); /* to update pixel transfer state */
3132
3133 /*
3134 * Store texture data (with pixel transfer ops)
3135 */
3136 _mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
3137 if (target == GL_TEXTURE_1D) {
3138 ctx->Driver.TexSubImage1D(ctx, target, level, xoffset,
3139 width, format, type, buf,
3140 &ctx->Unpack, texObj, texImage);
3141 }
3142 else if (target == GL_TEXTURE_3D) {
3143 ctx->Driver.TexSubImage3D(ctx, target, level, xoffset, yoffset, zoffset,
3144 width, height, 1, format, type, buf,
3145 &ctx->Unpack, texObj, texImage);
3146 }
3147 else {
3148 ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset,
3149 width, height, format, type, buf,
3150 &ctx->Unpack, texObj, texImage);
3151 }
3152 _mesa_meta_end(ctx);
3153
3154 _mesa_lock_texture(ctx, texObj); /* re-lock */
3155
3156 free(buf);
3157 }
3158
3159
3160 void
3161 _mesa_meta_CopyTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
3162 GLint xoffset,
3163 GLint x, GLint y, GLsizei width)
3164 {
3165 copy_tex_sub_image(ctx, 1, target, level, xoffset, 0, 0,
3166 x, y, width, 1);
3167 }
3168
3169
3170 void
3171 _mesa_meta_CopyTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
3172 GLint xoffset, GLint yoffset,
3173 GLint x, GLint y,
3174 GLsizei width, GLsizei height)
3175 {
3176 copy_tex_sub_image(ctx, 2, target, level, xoffset, yoffset, 0,
3177 x, y, width, height);
3178 }
3179
3180
3181 void
3182 _mesa_meta_CopyTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
3183 GLint xoffset, GLint yoffset, GLint zoffset,
3184 GLint x, GLint y,
3185 GLsizei width, GLsizei height)
3186 {
3187 copy_tex_sub_image(ctx, 3, target, level, xoffset, yoffset, zoffset,
3188 x, y, width, height);
3189 }
3190
3191
3192 /**
3193 * Decompress a texture image by drawing a quad with the compressed
3194 * texture and reading the pixels out of the color buffer.
3195 * \param slice which slice of a 3D texture or layer of a 1D/2D texture
3196 * \param destFormat format, ala glReadPixels
3197 * \param destType type, ala glReadPixels
3198 * \param dest destination buffer
3199 * \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
3200 */
3201 static void
3202 decompress_texture_image(struct gl_context *ctx,
3203 struct gl_texture_image *texImage,
3204 GLuint slice,
3205 GLenum destFormat, GLenum destType,
3206 GLvoid *dest, GLint destRowLength)
3207 {
3208 struct decompress_state *decompress = &ctx->Meta->Decompress;
3209 struct gl_texture_object *texObj = texImage->TexObject;
3210 const GLint width = texImage->Width;
3211 const GLint height = texImage->Height;
3212 const GLenum target = texObj->Target;
3213 GLenum faceTarget;
3214 struct vertex {
3215 GLfloat x, y, tex[3];
3216 };
3217 struct vertex verts[4];
3218 GLuint fboDrawSave, fboReadSave;
3219
3220 if (slice > 0) {
3221 assert(target == GL_TEXTURE_3D ||
3222 target == GL_TEXTURE_2D_ARRAY);
3223 }
3224
3225 if (target == GL_TEXTURE_CUBE_MAP) {
3226 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
3227 }
3228 else {
3229 faceTarget = target;
3230 }
3231
3232 /* save fbo bindings (not saved by _mesa_meta_begin()) */
3233 fboDrawSave = ctx->DrawBuffer->Name;
3234 fboReadSave = ctx->ReadBuffer->Name;
3235
3236 _mesa_meta_begin(ctx, MESA_META_ALL);
3237
3238 /* Create/bind FBO/renderbuffer */
3239 if (decompress->FBO == 0) {
3240 _mesa_GenFramebuffersEXT(1, &decompress->FBO);
3241 _mesa_GenRenderbuffersEXT(1, &decompress->RBO);
3242 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, decompress->FBO);
3243 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, decompress->RBO);
3244 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
3245 GL_COLOR_ATTACHMENT0_EXT,
3246 GL_RENDERBUFFER_EXT,
3247 decompress->RBO);
3248 }
3249 else {
3250 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, decompress->FBO);
3251 }
3252
3253 /* alloc dest surface */
3254 if (width != decompress->Width || height != decompress->Height) {
3255 _mesa_RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA,
3256 width, height);
3257 decompress->Width = width;
3258 decompress->Height = height;
3259 }
3260
3261 /* setup VBO data */
3262 if (decompress->ArrayObj == 0) {
3263 /* create vertex array object */
3264 _mesa_GenVertexArrays(1, &decompress->ArrayObj);
3265 _mesa_BindVertexArray(decompress->ArrayObj);
3266
3267 /* create vertex array buffer */
3268 _mesa_GenBuffersARB(1, &decompress->VBO);
3269 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, decompress->VBO);
3270 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
3271 NULL, GL_DYNAMIC_DRAW_ARB);
3272
3273 /* setup vertex arrays */
3274 _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
3275 _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
3276 _mesa_EnableClientState(GL_VERTEX_ARRAY);
3277 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
3278 }
3279 else {
3280 _mesa_BindVertexArray(decompress->ArrayObj);
3281 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, decompress->VBO);
3282 }
3283
3284 setup_texture_coords(faceTarget, slice, width, height,
3285 verts[0].tex,
3286 verts[1].tex,
3287 verts[2].tex,
3288 verts[3].tex);
3289
3290 /* setup vertex positions */
3291 verts[0].x = 0.0F;
3292 verts[0].y = 0.0F;
3293 verts[1].x = width;
3294 verts[1].y = 0.0F;
3295 verts[2].x = width;
3296 verts[2].y = height;
3297 verts[3].x = 0.0F;
3298 verts[3].y = height;
3299
3300 /* upload new vertex data */
3301 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
3302
3303 /* setup texture state */
3304 _mesa_BindTexture(target, texObj->Name);
3305 _mesa_set_enable(ctx, target, GL_TRUE);
3306
3307 {
3308 /* save texture object state */
3309 const GLenum minFilterSave = texObj->Sampler.MinFilter;
3310 const GLenum magFilterSave = texObj->Sampler.MagFilter;
3311 const GLint baseLevelSave = texObj->BaseLevel;
3312 const GLint maxLevelSave = texObj->MaxLevel;
3313 const GLenum wrapSSave = texObj->Sampler.WrapS;
3314 const GLenum wrapTSave = texObj->Sampler.WrapT;
3315 const GLenum srgbSave = texObj->Sampler.sRGBDecode;
3316
3317 /* restrict sampling to the texture level of interest */
3318 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, texImage->Level);
3319 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, texImage->Level);
3320 /* nearest filtering */
3321 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3322 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3323
3324 /* No sRGB decode or encode.*/
3325 if (ctx->Extensions.EXT_texture_sRGB_decode) {
3326 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
3327 GL_SKIP_DECODE_EXT);
3328 }
3329 if (ctx->Extensions.EXT_framebuffer_sRGB) {
3330 _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB_EXT, GL_FALSE);
3331 }
3332
3333 /* render quad w/ texture into renderbuffer */
3334 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3335
3336 /* Restore texture object state, the texture binding will
3337 * be restored by _mesa_meta_end().
3338 */
3339 _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
3340 _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
3341 if (target != GL_TEXTURE_RECTANGLE_ARB) {
3342 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
3343 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
3344 }
3345 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave);
3346 _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave);
3347 if (ctx->Extensions.EXT_texture_sRGB_decode) {
3348 _mesa_TexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, srgbSave);
3349 }
3350 }
3351
3352 /* read pixels from renderbuffer */
3353 {
3354 GLenum baseTexFormat = texImage->_BaseFormat;
3355
3356 /* The pixel transfer state will be set to default values at this point
3357 * (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
3358 * turned off (as required by glGetTexImage) but we need to handle some
3359 * special cases. In particular, single-channel texture values are
3360 * returned as red and two-channel texture values are returned as
3361 * red/alpha.
3362 */
3363 if (baseTexFormat == GL_LUMINANCE ||
3364 baseTexFormat == GL_LUMINANCE_ALPHA ||
3365 baseTexFormat == GL_INTENSITY) {
3366 /* Green and blue must be zero */
3367 _mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
3368 _mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
3369 }
3370
3371 ctx->Pack.RowLength = destRowLength;
3372 _mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
3373 }
3374
3375 /* disable texture unit */
3376 _mesa_set_enable(ctx, target, GL_FALSE);
3377
3378 _mesa_meta_end(ctx);
3379
3380 /* restore fbo bindings */
3381 if (fboDrawSave == fboReadSave) {
3382 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboDrawSave);
3383 }
3384 else {
3385 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fboDrawSave);
3386 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fboReadSave);
3387 }
3388 }
3389
3390
3391 /**
3392 * This is just a wrapper around _mesa_get_tex_image() and
3393 * decompress_texture_image(). Meta functions should not be directly called
3394 * from core Mesa.
3395 */
3396 void
3397 _mesa_meta_GetTexImage(struct gl_context *ctx,
3398 GLenum format, GLenum type, GLvoid *pixels,
3399 struct gl_texture_image *texImage)
3400 {
3401 /* We can only use the decompress-with-blit method here if the texels are
3402 * unsigned, normalized values. We could handle signed and unnormalized
3403 * with floating point renderbuffers...
3404 */
3405 if (_mesa_is_format_compressed(texImage->TexFormat) &&
3406 _mesa_get_format_datatype(texImage->TexFormat)
3407 == GL_UNSIGNED_NORMALIZED) {
3408 struct gl_texture_object *texObj = texImage->TexObject;
3409 const GLuint slice = 0; /* only 2D compressed textures for now */
3410 /* Need to unlock the texture here to prevent deadlock... */
3411 _mesa_unlock_texture(ctx, texObj);
3412 decompress_texture_image(ctx, texImage, slice, format, type, pixels,
3413 ctx->Pack.RowLength);
3414 /* ... and relock it */
3415 _mesa_lock_texture(ctx, texObj);
3416 }
3417 else {
3418 _mesa_get_teximage(ctx, format, type, pixels, texImage);
3419 }
3420 }
3421
3422
3423 /**
3424 * Meta implementation of ctx->Driver.DrawTex() in terms
3425 * of polygon rendering.
3426 */
3427 void
3428 _mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
3429 GLfloat width, GLfloat height)
3430 {
3431 #if FEATURE_OES_draw_texture
3432 struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
3433 struct vertex {
3434 GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
3435 };
3436 struct vertex verts[4];
3437 GLuint i;
3438
3439 _mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
3440 MESA_META_SHADER |
3441 MESA_META_TRANSFORM |
3442 MESA_META_VERTEX |
3443 MESA_META_VIEWPORT));
3444
3445 if (drawtex->ArrayObj == 0) {
3446 /* one-time setup */
3447 GLint active_texture;
3448
3449 /* create vertex array object */
3450 _mesa_GenVertexArrays(1, &drawtex->ArrayObj);
3451 _mesa_BindVertexArray(drawtex->ArrayObj);
3452
3453 /* create vertex array buffer */
3454 _mesa_GenBuffersARB(1, &drawtex->VBO);
3455 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
3456 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
3457 NULL, GL_DYNAMIC_DRAW_ARB);
3458
3459 /* client active texture is not part of the array object */
3460 active_texture = ctx->Array.ActiveTexture;
3461
3462 /* setup vertex arrays */
3463 _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
3464 _mesa_EnableClientState(GL_VERTEX_ARRAY);
3465 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3466 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + i);
3467 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(st[i]));
3468 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
3469 }
3470
3471 /* restore client active texture */
3472 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + active_texture);
3473 }
3474 else {
3475 _mesa_BindVertexArray(drawtex->ArrayObj);
3476 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
3477 }
3478
3479 /* vertex positions, texcoords */
3480 {
3481 const GLfloat x1 = x + width;
3482 const GLfloat y1 = y + height;
3483
3484 z = CLAMP(z, 0.0, 1.0);
3485 z = invert_z(z);
3486
3487 verts[0].x = x;
3488 verts[0].y = y;
3489 verts[0].z = z;
3490
3491 verts[1].x = x1;
3492 verts[1].y = y;
3493 verts[1].z = z;
3494
3495 verts[2].x = x1;
3496 verts[2].y = y1;
3497 verts[2].z = z;
3498
3499 verts[3].x = x;
3500 verts[3].y = y1;
3501 verts[3].z = z;
3502
3503 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
3504 const struct gl_texture_object *texObj;
3505 const struct gl_texture_image *texImage;
3506 GLfloat s, t, s1, t1;
3507 GLuint tw, th;
3508
3509 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
3510 GLuint j;
3511 for (j = 0; j < 4; j++) {
3512 verts[j].st[i][0] = 0.0f;
3513 verts[j].st[i][1] = 0.0f;
3514 }
3515 continue;
3516 }
3517
3518 texObj = ctx->Texture.Unit[i]._Current;
3519 texImage = texObj->Image[0][texObj->BaseLevel];
3520 tw = texImage->Width2;
3521 th = texImage->Height2;
3522
3523 s = (GLfloat) texObj->CropRect[0] / tw;
3524 t = (GLfloat) texObj->CropRect[1] / th;
3525 s1 = (GLfloat) (texObj->CropRect[0] + texObj->CropRect[2]) / tw;
3526 t1 = (GLfloat) (texObj->CropRect[1] + texObj->CropRect[3]) / th;
3527
3528 verts[0].st[i][0] = s;
3529 verts[0].st[i][1] = t;
3530
3531 verts[1].st[i][0] = s1;
3532 verts[1].st[i][1] = t;
3533
3534 verts[2].st[i][0] = s1;
3535 verts[2].st[i][1] = t1;
3536
3537 verts[3].st[i][0] = s;
3538 verts[3].st[i][1] = t1;
3539 }
3540
3541 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
3542 }
3543
3544 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
3545
3546 _mesa_meta_end(ctx);
3547 #endif /* FEATURE_OES_draw_texture */
3548 }