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