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