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