Merge branch 'master' into r300-compiler
[mesa.git] / src / mesa / drivers / common / meta.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
29 *
30 * \author Brian Paul
31 */
32
33
34 #include "main/glheader.h"
35 #include "main/mtypes.h"
36 #include "main/imports.h"
37 #include "main/arrayobj.h"
38 #include "main/blend.h"
39 #include "main/bufferobj.h"
40 #include "main/depth.h"
41 #include "main/enable.h"
42 #include "main/image.h"
43 #include "main/macros.h"
44 #include "main/matrix.h"
45 #include "main/polygon.h"
46 #include "main/readpix.h"
47 #include "main/scissor.h"
48 #include "main/shaders.h"
49 #include "main/stencil.h"
50 #include "main/texobj.h"
51 #include "main/texenv.h"
52 #include "main/teximage.h"
53 #include "main/texparam.h"
54 #include "main/texstate.h"
55 #include "main/varray.h"
56 #include "main/viewport.h"
57 #include "shader/program.h"
58 #include "shader/arbprogram.h"
59 #include "swrast/swrast.h"
60 #include "drivers/common/meta.h"
61
62
63 /**
64 * State which we may save/restore across meta ops.
65 * XXX this may be incomplete...
66 */
67 struct save_state
68 {
69 GLbitfield SavedState; /**< bitmask of META_* flags */
70
71 /** META_ALPHA_TEST */
72 GLboolean AlphaEnabled;
73
74 /** META_BLEND */
75 GLboolean BlendEnabled;
76 GLboolean ColorLogicOpEnabled;
77
78 /** META_COLOR_MASK */
79 GLubyte ColorMask[4];
80
81 /** META_DEPTH_TEST */
82 struct gl_depthbuffer_attrib Depth;
83
84 /** META_FOG */
85 GLboolean Fog;
86
87 /** META_PIXEL_STORE */
88 struct gl_pixelstore_attrib Pack, Unpack;
89
90 /** META_RASTERIZATION */
91 GLenum FrontPolygonMode, BackPolygonMode;
92 GLboolean PolygonOffset;
93 GLboolean PolygonSmooth;
94 GLboolean PolygonStipple;
95 GLboolean PolygonCull;
96
97 /** META_SCISSOR */
98 struct gl_scissor_attrib Scissor;
99
100 /** META_SHADER */
101 GLboolean VertexProgramEnabled;
102 struct gl_vertex_program *VertexProgram;
103 GLboolean FragmentProgramEnabled;
104 struct gl_fragment_program *FragmentProgram;
105 GLuint Shader;
106
107 /** META_STENCIL_TEST */
108 struct gl_stencil_attrib Stencil;
109
110 /** META_TRANSFORM */
111 GLenum MatrixMode;
112 GLfloat ModelviewMatrix[16];
113 GLfloat ProjectionMatrix[16];
114 GLfloat TextureMatrix[16];
115 GLbitfield ClipPlanesEnabled;
116
117 /** META_TEXTURE */
118 GLuint ActiveUnit;
119 GLuint ClientActiveUnit;
120 /** for unit[0] only */
121 struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
122 /** mask of TEXTURE_2D_BIT, etc */
123 GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
124 GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
125 GLuint EnvMode; /* unit[0] only */
126
127 /** META_VERTEX */
128 struct gl_array_object *ArrayObj;
129 struct gl_buffer_object *ArrayBufferObj;
130
131 /** META_VIEWPORT */
132 GLint ViewportX, ViewportY, ViewportW, ViewportH;
133 GLclampd DepthNear, DepthFar;
134
135 /** Miscellaneous (always disabled) */
136 GLboolean Lighting;
137 };
138
139
140 /**
141 * State for glBlitFramebufer()
142 */
143 struct blit_state
144 {
145 GLuint ArrayObj;
146 GLuint VBO;
147 GLuint DepthFP;
148 };
149
150
151 /**
152 * State for glClear()
153 */
154 struct clear_state
155 {
156 GLuint ArrayObj;
157 GLuint VBO;
158 };
159
160
161 /**
162 * State for glCopyPixels()
163 */
164 struct copypix_state
165 {
166 GLuint ArrayObj;
167 GLuint VBO;
168 };
169
170
171 /**
172 * State for glDrawPixels()
173 */
174 struct drawpix_state
175 {
176 GLuint ArrayObj;
177 GLuint VBO;
178
179 GLuint StencilFP; /**< Fragment program for drawing stencil images */
180 GLuint DepthFP; /**< Fragment program for drawing depth images */
181 };
182
183
184 /**
185 * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
186 * This is currently shared by all the meta ops. But we could create a
187 * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
188 */
189 struct temp_texture
190 {
191 GLuint TexObj;
192 GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
193 GLsizei MaxSize; /**< Max possible texture size */
194 GLboolean NPOT; /**< Non-power of two size OK? */
195 GLsizei Width, Height; /**< Current texture size */
196 GLenum IntFormat;
197 GLfloat Sright, Ttop; /**< right, top texcoords */
198 };
199
200
201 /**
202 * All per-context meta state.
203 */
204 struct gl_meta_state
205 {
206 struct save_state Save; /**< state saved during meta-ops */
207
208 struct temp_texture TempTex;
209
210 struct blit_state Blit; /**< For _mesa_meta_blit_framebuffer() */
211 struct clear_state Clear; /**< For _mesa_meta_clear() */
212 struct copypix_state CopyPix; /**< For _mesa_meta_copy_pixels() */
213 struct drawpix_state DrawPix; /**< For _mesa_meta_draw_pixels() */
214
215 /* other possible meta-ops:
216 * glBitmap()
217 * glGenerateMipmap()
218 */
219 };
220
221
222 /**
223 * Initialize meta-ops for a context.
224 * To be called once during context creation.
225 */
226 void
227 _mesa_meta_init(GLcontext *ctx)
228 {
229 ASSERT(!ctx->Meta);
230
231 ctx->Meta = CALLOC_STRUCT(gl_meta_state);
232 }
233
234
235 /**
236 * Free context meta-op state.
237 * To be called once during context destruction.
238 */
239 void
240 _mesa_meta_free(GLcontext *ctx)
241 {
242 struct gl_meta_state *meta = ctx->Meta;
243
244 if (_mesa_get_current_context()) {
245 /* if there's no current context, these textures, buffers, etc should
246 * still get freed by _mesa_free_context_data().
247 */
248
249 _mesa_DeleteTextures(1, &meta->TempTex.TexObj);
250
251 /* glBlitFramebuffer */
252 _mesa_DeleteBuffersARB(1, & meta->Blit.VBO);
253 _mesa_DeleteVertexArraysAPPLE(1, &meta->Blit.ArrayObj);
254 _mesa_DeletePrograms(1, &meta->Blit.DepthFP);
255
256 /* glClear */
257 _mesa_DeleteBuffersARB(1, & meta->Clear.VBO);
258 _mesa_DeleteVertexArraysAPPLE(1, &meta->Clear.ArrayObj);
259
260 /* glCopyPixels */
261 _mesa_DeleteBuffersARB(1, & meta->CopyPix.VBO);
262 _mesa_DeleteVertexArraysAPPLE(1, &meta->CopyPix.ArrayObj);
263
264 /* glDrawPixels */
265 _mesa_DeleteBuffersARB(1, & meta->DrawPix.VBO);
266 _mesa_DeleteVertexArraysAPPLE(1, &meta->DrawPix.ArrayObj);
267 _mesa_DeletePrograms(1, &meta->DrawPix.DepthFP);
268 _mesa_DeletePrograms(1, &meta->DrawPix.StencilFP);
269 }
270
271 _mesa_free(ctx->Meta);
272 ctx->Meta = NULL;
273 }
274
275
276 /**
277 * Enter meta state. This is like a light-weight version of glPushAttrib
278 * but it also resets most GL state back to default values.
279 *
280 * \param state bitmask of META_* flags indicating which attribute groups
281 * to save and reset to their defaults
282 */
283 static void
284 _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
285 {
286 struct save_state *save = &ctx->Meta->Save;
287
288 save->SavedState = state;
289
290 if (state & META_ALPHA_TEST) {
291 save->AlphaEnabled = ctx->Color.AlphaEnabled;
292 if (ctx->Color.AlphaEnabled)
293 _mesa_Disable(GL_ALPHA_TEST);
294 }
295
296 if (state & META_BLEND) {
297 save->BlendEnabled = ctx->Color.BlendEnabled;
298 if (ctx->Color.BlendEnabled)
299 _mesa_Disable(GL_BLEND);
300 save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
301 if (ctx->Color.ColorLogicOpEnabled)
302 _mesa_Disable(GL_COLOR_LOGIC_OP);
303 }
304
305 if (state & META_COLOR_MASK) {
306 COPY_4V(save->ColorMask, ctx->Color.ColorMask);
307 if (!ctx->Color.ColorMask[0] ||
308 !ctx->Color.ColorMask[1] ||
309 !ctx->Color.ColorMask[2] ||
310 !ctx->Color.ColorMask[3])
311 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
312 }
313
314 if (state & META_DEPTH_TEST) {
315 save->Depth = ctx->Depth; /* struct copy */
316 if (ctx->Depth.Test)
317 _mesa_Disable(GL_DEPTH_TEST);
318 }
319
320 if (state & META_FOG) {
321 save->Fog = ctx->Fog.Enabled;
322 if (ctx->Fog.Enabled)
323 _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
324 }
325
326 if (state & META_PIXEL_STORE) {
327 save->Pack = ctx->Pack;
328 save->Unpack = ctx->Unpack;
329 ctx->Pack = ctx->DefaultPacking;
330 ctx->Unpack = ctx->DefaultPacking;
331 }
332
333 if (state & META_RASTERIZATION) {
334 save->FrontPolygonMode = ctx->Polygon.FrontMode;
335 save->BackPolygonMode = ctx->Polygon.BackMode;
336 save->PolygonOffset = ctx->Polygon.OffsetFill;
337 save->PolygonSmooth = ctx->Polygon.SmoothFlag;
338 save->PolygonStipple = ctx->Polygon.StippleFlag;
339 save->PolygonCull = ctx->Polygon.CullFlag;
340 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
341 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
342 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
343 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
344 _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
345 }
346
347 if (state & META_SCISSOR) {
348 save->Scissor = ctx->Scissor; /* struct copy */
349 }
350
351 if (state & META_SHADER) {
352 if (ctx->Extensions.ARB_vertex_program) {
353 save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
354 save->VertexProgram = ctx->VertexProgram.Current;
355 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
356 }
357
358 if (ctx->Extensions.ARB_fragment_program) {
359 save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
360 save->FragmentProgram = ctx->FragmentProgram.Current;
361 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
362 }
363
364 if (ctx->Extensions.ARB_shader_objects) {
365 save->Shader = ctx->Shader.CurrentProgram ?
366 ctx->Shader.CurrentProgram->Name : 0;
367 _mesa_UseProgramObjectARB(0);
368 }
369 }
370
371 if (state & META_STENCIL_TEST) {
372 save->Stencil = ctx->Stencil; /* struct copy */
373 if (ctx->Stencil.Enabled)
374 _mesa_Disable(GL_STENCIL_TEST);
375 /* NOTE: other stencil state not reset */
376 }
377
378 if (state & META_TEXTURE) {
379 GLuint u, tgt;
380
381 save->ActiveUnit = ctx->Texture.CurrentUnit;
382 save->ClientActiveUnit = ctx->Array.ActiveTexture;
383 save->EnvMode = ctx->Texture.Unit[0].EnvMode;
384
385 /* Disable all texture units */
386 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
387 save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
388 save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled;
389 if (ctx->Texture.Unit[u].Enabled ||
390 ctx->Texture.Unit[u].TexGenEnabled) {
391 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
392 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
393 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
394 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
395 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
396 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
397 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
398 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
399 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
400 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
401 }
402 }
403
404 /* save current texture objects for unit[0] only */
405 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
406 _mesa_reference_texobj(&save->CurrentTexture[tgt],
407 ctx->Texture.Unit[0].CurrentTex[tgt]);
408 }
409
410 /* set defaults for unit[0] */
411 _mesa_ActiveTextureARB(GL_TEXTURE0);
412 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
413 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
414 }
415
416 if (state & META_TRANSFORM) {
417 GLuint activeTexture = ctx->Texture.CurrentUnit;
418 _mesa_memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
419 16 * sizeof(GLfloat));
420 _mesa_memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
421 16 * sizeof(GLfloat));
422 _mesa_memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
423 16 * sizeof(GLfloat));
424 save->MatrixMode = ctx->Transform.MatrixMode;
425 /* set 1:1 vertex:pixel coordinate transform */
426 _mesa_ActiveTextureARB(GL_TEXTURE0);
427 _mesa_MatrixMode(GL_TEXTURE);
428 _mesa_LoadIdentity();
429 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
430 _mesa_MatrixMode(GL_MODELVIEW);
431 _mesa_LoadIdentity();
432 _mesa_MatrixMode(GL_PROJECTION);
433 _mesa_LoadIdentity();
434 _mesa_Ortho(0.0F, ctx->DrawBuffer->Width,
435 0.0F, ctx->DrawBuffer->Height,
436 -1.0F, 1.0F);
437 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
438 if (ctx->Transform.ClipPlanesEnabled) {
439 GLuint i;
440 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
441 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
442 }
443 }
444 }
445
446 if (state & META_VERTEX) {
447 /* save vertex array object state */
448 _mesa_reference_array_object(ctx, &save->ArrayObj,
449 ctx->Array.ArrayObj);
450 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
451 ctx->Array.ArrayBufferObj);
452 /* set some default state? */
453 }
454
455 if (state & META_VIEWPORT) {
456 /* save viewport state */
457 save->ViewportX = ctx->Viewport.X;
458 save->ViewportY = ctx->Viewport.Y;
459 save->ViewportW = ctx->Viewport.Width;
460 save->ViewportH = ctx->Viewport.Height;
461 /* set viewport to match window size */
462 if (ctx->Viewport.X != 0 ||
463 ctx->Viewport.Y != 0 ||
464 ctx->Viewport.Width != ctx->DrawBuffer->Width ||
465 ctx->Viewport.Height != ctx->DrawBuffer->Height) {
466 _mesa_set_viewport(ctx, 0, 0,
467 ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
468 }
469 /* save depth range state */
470 save->DepthNear = ctx->Viewport.Near;
471 save->DepthFar = ctx->Viewport.Far;
472 /* set depth range to default */
473 _mesa_DepthRange(0.0, 1.0);
474 }
475
476 /* misc */
477 {
478 save->Lighting = ctx->Light.Enabled;
479 if (ctx->Light.Enabled)
480 _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
481 }
482 }
483
484
485 /**
486 * Leave meta state. This is like a light-weight version of glPopAttrib().
487 */
488 static void
489 _mesa_meta_end(GLcontext *ctx)
490 {
491 struct save_state *save = &ctx->Meta->Save;
492 const GLbitfield state = save->SavedState;
493
494 if (state & META_ALPHA_TEST) {
495 if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
496 _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
497 }
498
499 if (state & META_BLEND) {
500 if (ctx->Color.BlendEnabled != save->BlendEnabled)
501 _mesa_set_enable(ctx, GL_BLEND, save->BlendEnabled);
502 if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
503 _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
504 }
505
506 if (state & META_COLOR_MASK) {
507 if (!TEST_EQ_4V(ctx->Color.ColorMask, save->ColorMask))
508 _mesa_ColorMask(save->ColorMask[0], save->ColorMask[1],
509 save->ColorMask[2], save->ColorMask[3]);
510 }
511
512 if (state & META_DEPTH_TEST) {
513 if (ctx->Depth.Test != save->Depth.Test)
514 _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
515 _mesa_DepthFunc(save->Depth.Func);
516 _mesa_DepthMask(save->Depth.Mask);
517 }
518
519 if (state & META_FOG) {
520 _mesa_set_enable(ctx, GL_FOG, save->Fog);
521 }
522
523 if (state & META_PIXEL_STORE) {
524 ctx->Pack = save->Pack;
525 ctx->Unpack = save->Unpack;
526 }
527
528 if (state & META_RASTERIZATION) {
529 _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
530 _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
531 _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
532 _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
533 _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
534 _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
535 }
536
537 if (state & META_SCISSOR) {
538 _mesa_set_enable(ctx, GL_SCISSOR_TEST, save->Scissor.Enabled);
539 _mesa_Scissor(save->Scissor.X, save->Scissor.Y,
540 save->Scissor.Width, save->Scissor.Height);
541 }
542
543 if (state & META_SHADER) {
544 if (ctx->Extensions.ARB_vertex_program) {
545 _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
546 save->VertexProgramEnabled);
547 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
548 save->VertexProgram);
549 }
550
551 if (ctx->Extensions.ARB_fragment_program) {
552 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
553 save->FragmentProgramEnabled);
554 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
555 save->FragmentProgram);
556 }
557
558 if (ctx->Extensions.ARB_shader_objects) {
559 _mesa_UseProgramObjectARB(save->Shader);
560 }
561 }
562
563 if (state & META_STENCIL_TEST) {
564 const struct gl_stencil_attrib *stencil = &save->Stencil;
565
566 _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
567 _mesa_ClearStencil(stencil->Clear);
568 if (ctx->Extensions.EXT_stencil_two_side) {
569 _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
570 stencil->TestTwoSide);
571 _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
572 ? GL_BACK : GL_FRONT);
573 }
574 /* front state */
575 _mesa_StencilFuncSeparate(GL_FRONT,
576 stencil->Function[0],
577 stencil->Ref[0],
578 stencil->ValueMask[0]);
579 _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
580 _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
581 stencil->ZFailFunc[0],
582 stencil->ZPassFunc[0]);
583 /* back state */
584 _mesa_StencilFuncSeparate(GL_BACK,
585 stencil->Function[1],
586 stencil->Ref[1],
587 stencil->ValueMask[1]);
588 _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
589 _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
590 stencil->ZFailFunc[1],
591 stencil->ZPassFunc[1]);
592 }
593
594 if (state & META_TEXTURE) {
595 GLuint u, tgt;
596
597 ASSERT(ctx->Texture.CurrentUnit == 0);
598
599 /* restore texenv for unit[0] */
600 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
601
602 /* restore texture objects for unit[0] only */
603 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
604 _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
605 save->CurrentTexture[tgt]);
606 }
607
608 /* Re-enable textures, texgen */
609 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
610 if (save->TexEnabled[u]) {
611 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
612
613 if (save->TexEnabled[u] & TEXTURE_1D_BIT)
614 _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_TRUE);
615 if (save->TexEnabled[u] & TEXTURE_2D_BIT)
616 _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_TRUE);
617 if (save->TexEnabled[u] & TEXTURE_3D_BIT)
618 _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_TRUE);
619 if (save->TexEnabled[u] & TEXTURE_CUBE_BIT)
620 _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_TRUE);
621 if (save->TexEnabled[u] & TEXTURE_RECT_BIT)
622 _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_TRUE);
623 }
624
625 if (save->TexGenEnabled[u]) {
626 _mesa_ActiveTextureARB(GL_TEXTURE0 + u);
627
628 if (save->TexGenEnabled[u] & S_BIT)
629 _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_TRUE);
630 if (save->TexGenEnabled[u] & T_BIT)
631 _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_TRUE);
632 if (save->TexGenEnabled[u] & R_BIT)
633 _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_TRUE);
634 if (save->TexGenEnabled[u] & Q_BIT)
635 _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
636 }
637 }
638
639 /* restore current unit state */
640 _mesa_ActiveTextureARB(GL_TEXTURE0 + save->ActiveUnit);
641 _mesa_ClientActiveTextureARB(GL_TEXTURE0 + save->ClientActiveUnit);
642 }
643
644 if (state & META_TRANSFORM) {
645 GLuint activeTexture = ctx->Texture.CurrentUnit;
646 _mesa_ActiveTextureARB(GL_TEXTURE0);
647 _mesa_MatrixMode(GL_TEXTURE);
648 _mesa_LoadMatrixf(save->TextureMatrix);
649 _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture);
650
651 _mesa_MatrixMode(GL_MODELVIEW);
652 _mesa_LoadMatrixf(save->ModelviewMatrix);
653
654 _mesa_MatrixMode(GL_PROJECTION);
655 _mesa_LoadMatrixf(save->ProjectionMatrix);
656
657 _mesa_MatrixMode(save->MatrixMode);
658
659 save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
660 if (save->ClipPlanesEnabled) {
661 GLuint i;
662 for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
663 if (save->ClipPlanesEnabled & (1 << i)) {
664 _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
665 }
666 }
667 }
668 }
669
670 if (state & META_VERTEX) {
671 /* restore vertex buffer object */
672 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
673 _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
674
675 /* restore vertex array object */
676 _mesa_BindVertexArray(save->ArrayObj->Name);
677 _mesa_reference_array_object(ctx, &save->ArrayObj, NULL);
678 }
679
680 if (state & META_VIEWPORT) {
681 if (save->ViewportX != ctx->Viewport.X ||
682 save->ViewportY != ctx->Viewport.Y ||
683 save->ViewportW != ctx->Viewport.Width ||
684 save->ViewportH != ctx->Viewport.Height) {
685 _mesa_set_viewport(ctx, save->ViewportX, save->ViewportY,
686 save->ViewportW, save->ViewportH);
687 }
688 _mesa_DepthRange(save->DepthNear, save->DepthFar);
689 }
690
691 /* misc */
692 if (save->Lighting) {
693 _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
694 }
695 if (save->Fog) {
696 _mesa_set_enable(ctx, GL_FOG, GL_TRUE);
697 }
698 }
699
700
701 /**
702 * Return pointer to temp_texture info. This does some one-time init
703 * if needed.
704 */
705 static struct temp_texture *
706 get_temp_texture(GLcontext *ctx)
707 {
708 struct temp_texture *tex = &ctx->Meta->TempTex;
709
710 if (!tex->TexObj) {
711 /* do one-time init */
712
713 /* prefer texture rectangle */
714 if (ctx->Extensions.NV_texture_rectangle) {
715 tex->Target = GL_TEXTURE_RECTANGLE;
716 tex->MaxSize = ctx->Const.MaxTextureRectSize;
717 tex->NPOT = GL_TRUE;
718 }
719 else {
720 /* use 2D texture, NPOT if possible */
721 tex->Target = GL_TEXTURE_2D;
722 tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
723 tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
724 }
725 assert(tex->MaxSize > 0);
726
727 _mesa_GenTextures(1, &tex->TexObj);
728 _mesa_BindTexture(tex->Target, tex->TexObj);
729 }
730
731 return tex;
732 }
733
734
735 /**
736 * Compute the width/height of texture needed to draw an image of the
737 * given size. Return a flag indicating whether the current texture
738 * can be re-used (glTexSubImage2D) or if a new texture needs to be
739 * allocated (glTexImage2D).
740 * Also, compute s/t texcoords for drawing.
741 *
742 * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
743 */
744 static GLboolean
745 alloc_texture(struct temp_texture *tex,
746 GLsizei width, GLsizei height, GLenum intFormat)
747 {
748 GLboolean newTex = GL_FALSE;
749
750 if (width > tex->Width ||
751 height > tex->Height ||
752 intFormat != tex->IntFormat) {
753 /* alloc new texture (larger or different format) */
754
755 if (tex->NPOT) {
756 /* use non-power of two size */
757 tex->Width = width;
758 tex->Height = height;
759 }
760 else {
761 /* find power of two size */
762 GLsizei w, h;
763 w = h = 16;
764 while (w < width)
765 w *= 2;
766 while (h < height)
767 h *= 2;
768 tex->Width = w;
769 tex->Height = h;
770 }
771
772 tex->IntFormat = intFormat;
773
774 newTex = GL_TRUE;
775 }
776
777 /* compute texcoords */
778 if (tex->Target == GL_TEXTURE_RECTANGLE) {
779 tex->Sright = (GLfloat) width;
780 tex->Ttop = (GLfloat) height;
781 }
782 else {
783 tex->Sright = (GLfloat) width / tex->Width;
784 tex->Ttop = (GLfloat) height / tex->Height;
785 }
786
787 return newTex;
788 }
789
790
791 /**
792 * Setup/load texture for glCopyPixels or glBlitFramebuffer.
793 */
794 static void
795 setup_copypix_texture(struct temp_texture *tex,
796 GLboolean newTex,
797 GLint srcX, GLint srcY,
798 GLsizei width, GLsizei height, GLenum intFormat,
799 GLenum filter)
800 {
801 _mesa_BindTexture(tex->Target, tex->TexObj);
802 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
803 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
804 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
805
806 /* copy framebuffer image to texture */
807 if (newTex) {
808 /* create new tex image */
809 if (tex->Width == width && tex->Height == height) {
810 /* create new tex with framebuffer data */
811 _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
812 srcX, srcY, width, height, 0);
813 }
814 else {
815 /* create empty texture */
816 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
817 tex->Width, tex->Height, 0,
818 intFormat, GL_UNSIGNED_BYTE, NULL);
819 /* load image */
820 _mesa_CopyTexSubImage2D(tex->Target, 0,
821 0, 0, srcX, srcY, width, height);
822 }
823 }
824 else {
825 /* replace existing tex image */
826 _mesa_CopyTexSubImage2D(tex->Target, 0,
827 0, 0, srcX, srcY, width, height);
828 }
829 }
830
831
832 /**
833 * Setup/load texture for glDrawPixels.
834 */
835 static void
836 setup_drawpix_texture(struct temp_texture *tex,
837 GLboolean newTex,
838 GLenum texIntFormat,
839 GLsizei width, GLsizei height,
840 GLenum format, GLenum type,
841 const GLvoid *pixels)
842 {
843 _mesa_BindTexture(tex->Target, tex->TexObj);
844 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
845 _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
846 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
847
848 /* copy pixel data to texture */
849 if (newTex) {
850 /* create new tex image */
851 if (tex->Width == width && tex->Height == height) {
852 /* create new tex and load image data */
853 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
854 tex->Width, tex->Height, 0, format, type, pixels);
855 }
856 else {
857 /* create empty texture */
858 _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
859 tex->Width, tex->Height, 0, format, type, NULL);
860 /* load image */
861 _mesa_TexSubImage2D(tex->Target, 0,
862 0, 0, width, height, format, type, pixels);
863 }
864 }
865 else {
866 /* replace existing tex image */
867 _mesa_TexSubImage2D(tex->Target, 0,
868 0, 0, width, height, format, type, pixels);
869 }
870 }
871
872
873
874 /**
875 * One-time init for drawing depth pixels.
876 */
877 static void
878 init_blit_depth_pixels(GLcontext *ctx)
879 {
880 static const char *program =
881 "!!ARBfp1.0\n"
882 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
883 "END \n";
884 char program2[200];
885 struct blit_state *blit = &ctx->Meta->Blit;
886 struct temp_texture *tex = get_temp_texture(ctx);
887 const char *texTarget;
888
889 assert(blit->DepthFP == 0);
890
891 /* replace %s with "RECT" or "2D" */
892 assert(strlen(program) + 4 < sizeof(program2));
893 if (tex->Target == GL_TEXTURE_RECTANGLE)
894 texTarget = "RECT";
895 else
896 texTarget = "2D";
897 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
898
899 _mesa_GenPrograms(1, &blit->DepthFP);
900 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
901 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
902 strlen(program2), (const GLubyte *) program2);
903 }
904
905
906 /**
907 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
908 * of texture mapping and polygon rendering.
909 */
910 void
911 _mesa_meta_blit_framebuffer(GLcontext *ctx,
912 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
913 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
914 GLbitfield mask, GLenum filter)
915 {
916 struct blit_state *blit = &ctx->Meta->Blit;
917 struct temp_texture *tex = get_temp_texture(ctx);
918 const GLsizei maxTexSize = tex->MaxSize;
919 const GLint srcX = MIN2(srcX0, srcX1);
920 const GLint srcY = MIN2(srcY0, srcY1);
921 const GLint srcW = abs(srcX1 - srcX0);
922 const GLint srcH = abs(srcY1 - srcY0);
923 const GLboolean srcFlipX = srcX1 < srcX0;
924 const GLboolean srcFlipY = srcY1 < srcY0;
925 GLfloat verts[4][4]; /* four verts of X,Y,S,T */
926 GLboolean newTex;
927
928 if (srcW > maxTexSize || srcH > maxTexSize) {
929 /* XXX avoid this fallback */
930 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
931 dstX0, dstY0, dstX1, dstY1, mask, filter);
932 return;
933 }
934
935 if (srcFlipX) {
936 GLint tmp = dstX0;
937 dstX0 = dstX1;
938 dstX1 = tmp;
939 }
940
941 if (srcFlipY) {
942 GLint tmp = dstY0;
943 dstY0 = dstY1;
944 dstY1 = tmp;
945 }
946
947 /* only scissor effects blit so save/clear all other relevant state */
948 _mesa_meta_begin(ctx, ~META_SCISSOR);
949
950 if (blit->ArrayObj == 0) {
951 /* one-time setup */
952
953 /* create vertex array object */
954 _mesa_GenVertexArrays(1, &blit->ArrayObj);
955 _mesa_BindVertexArray(blit->ArrayObj);
956
957 /* create vertex array buffer */
958 _mesa_GenBuffersARB(1, &blit->VBO);
959 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
960 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
961 NULL, GL_DYNAMIC_DRAW_ARB);
962
963 /* setup vertex arrays */
964 _mesa_VertexPointer(2, GL_FLOAT, sizeof(verts[0]),
965 (void *) (0 * sizeof(GLfloat)));
966 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
967 (void *) (2 * sizeof(GLfloat)));
968 _mesa_EnableClientState(GL_VERTEX_ARRAY);
969 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
970 }
971 else {
972 _mesa_BindVertexArray(blit->ArrayObj);
973 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
974 }
975
976 newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
977
978 /* vertex positions/texcoords (after texture allocation!) */
979 {
980 verts[0][0] = (GLfloat) dstX0;
981 verts[0][1] = (GLfloat) dstY0;
982 verts[1][0] = (GLfloat) dstX1;
983 verts[1][1] = (GLfloat) dstY0;
984 verts[2][0] = (GLfloat) dstX1;
985 verts[2][1] = (GLfloat) dstY1;
986 verts[3][0] = (GLfloat) dstX0;
987 verts[3][1] = (GLfloat) dstY1;
988
989 verts[0][2] = 0.0F;
990 verts[0][3] = 0.0F;
991 verts[1][2] = tex->Sright;
992 verts[1][3] = 0.0F;
993 verts[2][2] = tex->Sright;
994 verts[2][3] = tex->Ttop;
995 verts[3][2] = 0.0F;
996 verts[3][3] = tex->Ttop;
997
998 /* upload new vertex data */
999 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1000 }
1001
1002 _mesa_Enable(tex->Target);
1003
1004 if (mask & GL_COLOR_BUFFER_BIT) {
1005 setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
1006 GL_RGBA, filter);
1007 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1008 mask &= ~GL_COLOR_BUFFER_BIT;
1009 }
1010
1011 if (mask & GL_DEPTH_BUFFER_BIT) {
1012 GLuint *tmp = (GLuint *) _mesa_malloc(srcW * srcH * sizeof(GLuint));
1013 if (tmp) {
1014 if (!blit->DepthFP)
1015 init_blit_depth_pixels(ctx);
1016
1017 /* maybe change tex format here */
1018 newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
1019
1020 _mesa_ReadPixels(srcX, srcY, srcW, srcH,
1021 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1022
1023 setup_drawpix_texture(tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
1024 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
1025
1026 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
1027 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1028 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1029 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1030 _mesa_DepthFunc(GL_ALWAYS);
1031 _mesa_DepthMask(GL_TRUE);
1032
1033 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1034 mask &= ~GL_DEPTH_BUFFER_BIT;
1035
1036 _mesa_free(tmp);
1037 }
1038 }
1039
1040 if (mask & GL_STENCIL_BUFFER_BIT) {
1041 /* XXX can't easily do stencil */
1042 }
1043
1044 _mesa_Disable(tex->Target);
1045
1046 _mesa_meta_end(ctx);
1047
1048 if (mask) {
1049 _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
1050 dstX0, dstY0, dstX1, dstY1, mask, filter);
1051 }
1052 }
1053
1054
1055 /**
1056 * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
1057 */
1058 void
1059 _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
1060 {
1061 struct clear_state *clear = &ctx->Meta->Clear;
1062 GLfloat verts[4][7]; /* four verts of X,Y,Z,R,G,B,A */
1063 /* save all state but scissor, pixel pack/unpack */
1064 GLbitfield metaSave = META_ALL - META_SCISSOR - META_PIXEL_STORE;
1065
1066 if (buffers & BUFFER_BITS_COLOR) {
1067 /* if clearing color buffers, don't save/restore colormask */
1068 metaSave -= META_COLOR_MASK;
1069 }
1070
1071 _mesa_meta_begin(ctx, metaSave);
1072
1073 if (clear->ArrayObj == 0) {
1074 /* one-time setup */
1075
1076 /* create vertex array object */
1077 _mesa_GenVertexArrays(1, &clear->ArrayObj);
1078 _mesa_BindVertexArray(clear->ArrayObj);
1079
1080 /* create vertex array buffer */
1081 _mesa_GenBuffersARB(1, &clear->VBO);
1082 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1083 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1084 NULL, GL_DYNAMIC_DRAW_ARB);
1085
1086 /* setup vertex arrays */
1087 _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
1088 (void *) (0 * sizeof(GLfloat)));
1089 _mesa_ColorPointer(4, GL_FLOAT, sizeof(verts[0]),
1090 (void *) (3 * sizeof(GLfloat)));
1091 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1092 _mesa_EnableClientState(GL_COLOR_ARRAY);
1093 }
1094 else {
1095 _mesa_BindVertexArray(clear->ArrayObj);
1096 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
1097 }
1098
1099 /* GL_COLOR_BUFFER_BIT */
1100 if (buffers & BUFFER_BITS_COLOR) {
1101 /* leave colormask, glDrawBuffer state as-is */
1102 }
1103 else {
1104 ASSERT(metaSave & META_COLOR_MASK);
1105 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1106 }
1107
1108 /* GL_DEPTH_BUFFER_BIT */
1109 if (buffers & BUFFER_BIT_DEPTH) {
1110 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1111 _mesa_DepthFunc(GL_ALWAYS);
1112 _mesa_DepthMask(GL_TRUE);
1113 }
1114 else {
1115 assert(!ctx->Depth.Test);
1116 }
1117
1118 /* GL_STENCIL_BUFFER_BIT */
1119 if (buffers & BUFFER_BIT_STENCIL) {
1120 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1121 _mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
1122 GL_REPLACE, GL_REPLACE, GL_REPLACE);
1123 _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
1124 ctx->Stencil.Clear & 0x7fffffff,
1125 ctx->Stencil.WriteMask[0]);
1126 }
1127 else {
1128 assert(!ctx->Stencil.Enabled);
1129 }
1130
1131 /* vertex positions/colors */
1132 {
1133 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
1134 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
1135 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
1136 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
1137 const GLfloat z = 1.0 - 2.0 * ctx->Depth.Clear;
1138 GLuint i;
1139
1140 verts[0][0] = x0;
1141 verts[0][1] = y0;
1142 verts[0][2] = z;
1143 verts[1][0] = x1;
1144 verts[1][1] = y0;
1145 verts[1][2] = z;
1146 verts[2][0] = x1;
1147 verts[2][1] = y1;
1148 verts[2][2] = z;
1149 verts[3][0] = x0;
1150 verts[3][1] = y1;
1151 verts[3][2] = z;
1152
1153 /* vertex colors */
1154 for (i = 0; i < 4; i++) {
1155 COPY_4FV(&verts[i][3], ctx->Color.ClearColor);
1156 }
1157
1158 /* upload new vertex data */
1159 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1160 }
1161
1162 /* draw quad */
1163 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1164
1165 _mesa_meta_end(ctx);
1166 }
1167
1168
1169 /**
1170 * Meta implementation of ctx->Driver.CopyPixels() in terms
1171 * of texture mapping and polygon rendering.
1172 */
1173 void
1174 _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
1175 GLsizei width, GLsizei height,
1176 GLint dstX, GLint dstY, GLenum type)
1177 {
1178 struct copypix_state *copypix = &ctx->Meta->CopyPix;
1179 struct temp_texture *tex = get_temp_texture(ctx);
1180 GLfloat verts[4][5]; /* four verts of X,Y,Z,S,T */
1181 GLboolean newTex;
1182 GLenum intFormat = GL_RGBA;
1183
1184 if (type != GL_COLOR ||
1185 ctx->_ImageTransferState ||
1186 ctx->Fog.Enabled ||
1187 width > tex->MaxSize ||
1188 height > tex->MaxSize) {
1189 /* XXX avoid this fallback */
1190 _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
1191 return;
1192 }
1193
1194 /* Most GL state applies to glCopyPixels, but a there's a few things
1195 * we need to override:
1196 */
1197 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1198 META_SHADER |
1199 META_TEXTURE |
1200 META_TRANSFORM |
1201 META_VERTEX |
1202 META_VIEWPORT));
1203
1204 if (copypix->ArrayObj == 0) {
1205 /* one-time setup */
1206
1207 /* create vertex array object */
1208 _mesa_GenVertexArrays(1, &copypix->ArrayObj);
1209 _mesa_BindVertexArray(copypix->ArrayObj);
1210
1211 /* create vertex array buffer */
1212 _mesa_GenBuffersARB(1, &copypix->VBO);
1213 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1214 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1215 NULL, GL_DYNAMIC_DRAW_ARB);
1216
1217 /* setup vertex arrays */
1218 _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
1219 (void *) (0 * sizeof(GLfloat)));
1220 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
1221 (void *) (3 * sizeof(GLfloat)));
1222 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1223 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1224 }
1225 else {
1226 _mesa_BindVertexArray(copypix->ArrayObj);
1227 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
1228 }
1229
1230 newTex = alloc_texture(tex, width, height, intFormat);
1231
1232 /* vertex positions, texcoords (after texture allocation!) */
1233 {
1234 const GLfloat dstX0 = (GLfloat) dstX;
1235 const GLfloat dstY0 = (GLfloat) dstY;
1236 const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
1237 const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
1238 const GLfloat z = ctx->Current.RasterPos[2];
1239
1240 verts[0][0] = dstX0;
1241 verts[0][1] = dstY0;
1242 verts[0][2] = z;
1243 verts[0][3] = 0.0F;
1244 verts[0][4] = 0.0F;
1245 verts[1][0] = dstX1;
1246 verts[1][1] = dstY0;
1247 verts[1][2] = z;
1248 verts[1][3] = tex->Sright;
1249 verts[1][4] = 0.0F;
1250 verts[2][0] = dstX1;
1251 verts[2][1] = dstY1;
1252 verts[2][2] = z;
1253 verts[2][3] = tex->Sright;
1254 verts[2][4] = tex->Ttop;
1255 verts[3][0] = dstX0;
1256 verts[3][1] = dstY1;
1257 verts[3][2] = z;
1258 verts[3][3] = 0.0F;
1259 verts[3][4] = tex->Ttop;
1260
1261 /* upload new vertex data */
1262 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1263 }
1264
1265 /* Alloc/setup texture */
1266 setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
1267 GL_RGBA, GL_NEAREST);
1268
1269 _mesa_Enable(tex->Target);
1270
1271 /* draw textured quad */
1272 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1273
1274 _mesa_Disable(tex->Target);
1275
1276 _mesa_meta_end(ctx);
1277 }
1278
1279
1280
1281 /**
1282 * When the glDrawPixels() image size is greater than the max rectangle
1283 * texture size we use this function to break the glDrawPixels() image
1284 * into tiles which fit into the max texture size.
1285 */
1286 static void
1287 tiled_draw_pixels(GLcontext *ctx,
1288 GLint tileSize,
1289 GLint x, GLint y, GLsizei width, GLsizei height,
1290 GLenum format, GLenum type,
1291 const struct gl_pixelstore_attrib *unpack,
1292 const GLvoid *pixels)
1293 {
1294 struct gl_pixelstore_attrib tileUnpack = *unpack;
1295 GLint i, j;
1296
1297 if (tileUnpack.RowLength == 0)
1298 tileUnpack.RowLength = width;
1299
1300 for (i = 0; i < width; i += tileSize) {
1301 const GLint tileWidth = MIN2(tileSize, width - i);
1302 const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
1303
1304 tileUnpack.SkipPixels = unpack->SkipPixels + i;
1305
1306 for (j = 0; j < height; j += tileSize) {
1307 const GLint tileHeight = MIN2(tileSize, height - j);
1308 const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
1309
1310 tileUnpack.SkipRows = unpack->SkipRows + j;
1311
1312 _mesa_meta_draw_pixels(ctx, tileX, tileY,
1313 tileWidth, tileHeight,
1314 format, type, &tileUnpack, pixels);
1315 }
1316 }
1317 }
1318
1319
1320 /**
1321 * One-time init for drawing stencil pixels.
1322 */
1323 static void
1324 init_draw_stencil_pixels(GLcontext *ctx)
1325 {
1326 /* This program is run eight times, once for each stencil bit.
1327 * The stencil values to draw are found in an 8-bit alpha texture.
1328 * We read the texture/stencil value and test if bit 'b' is set.
1329 * If the bit is not set, use KIL to kill the fragment.
1330 * Finally, we use the stencil test to update the stencil buffer.
1331 *
1332 * The basic algorithm for checking if a bit is set is:
1333 * if (is_odd(value / (1 << bit)))
1334 * result is one (or non-zero).
1335 * else
1336 * result is zero.
1337 * The program parameter contains three values:
1338 * parm.x = 255 / (1 << bit)
1339 * parm.y = 0.5
1340 * parm.z = 0.0
1341 */
1342 static const char *program =
1343 "!!ARBfp1.0\n"
1344 "PARAM parm = program.local[0]; \n"
1345 "TEMP t; \n"
1346 "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
1347 "# t = t * 255 / bit \n"
1348 "MUL t.x, t.a, parm.x; \n"
1349 "# t = (int) t \n"
1350 "FRC t.y, t.x; \n"
1351 "SUB t.x, t.x, t.y; \n"
1352 "# t = t * 0.5 \n"
1353 "MUL t.x, t.x, parm.y; \n"
1354 "# t = fract(t.x) \n"
1355 "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
1356 "# t.x = (t.x == 0 ? 1 : 0) \n"
1357 "SGE t.x, -t.x, parm.z; \n"
1358 "KIL -t.x; \n"
1359 "# for debug only \n"
1360 "#MOV result.color, t.x; \n"
1361 "END \n";
1362 char program2[1000];
1363 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1364 struct temp_texture *tex = get_temp_texture(ctx);
1365 const char *texTarget;
1366
1367 assert(drawpix->StencilFP == 0);
1368
1369 /* replace %s with "RECT" or "2D" */
1370 assert(strlen(program) + 4 < sizeof(program2));
1371 if (tex->Target == GL_TEXTURE_RECTANGLE)
1372 texTarget = "RECT";
1373 else
1374 texTarget = "2D";
1375 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1376
1377 _mesa_GenPrograms(1, &drawpix->StencilFP);
1378 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1379 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1380 strlen(program2), (const GLubyte *) program2);
1381 }
1382
1383
1384 /**
1385 * One-time init for drawing depth pixels.
1386 */
1387 static void
1388 init_draw_depth_pixels(GLcontext *ctx)
1389 {
1390 static const char *program =
1391 "!!ARBfp1.0\n"
1392 "PARAM color = program.local[0]; \n"
1393 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
1394 "MOV result.color, color; \n"
1395 "END \n";
1396 char program2[200];
1397 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1398 struct temp_texture *tex = get_temp_texture(ctx);
1399 const char *texTarget;
1400
1401 assert(drawpix->DepthFP == 0);
1402
1403 /* replace %s with "RECT" or "2D" */
1404 assert(strlen(program) + 4 < sizeof(program2));
1405 if (tex->Target == GL_TEXTURE_RECTANGLE)
1406 texTarget = "RECT";
1407 else
1408 texTarget = "2D";
1409 _mesa_snprintf(program2, sizeof(program2), program, texTarget);
1410
1411 _mesa_GenPrograms(1, &drawpix->DepthFP);
1412 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1413 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
1414 strlen(program2), (const GLubyte *) program2);
1415 }
1416
1417
1418 /**
1419 * Meta implementation of ctx->Driver.DrawPixels() in terms
1420 * of texture mapping and polygon rendering.
1421 */
1422 void
1423 _mesa_meta_draw_pixels(GLcontext *ctx,
1424 GLint x, GLint y, GLsizei width, GLsizei height,
1425 GLenum format, GLenum type,
1426 const struct gl_pixelstore_attrib *unpack,
1427 const GLvoid *pixels)
1428 {
1429 struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
1430 struct temp_texture *tex = get_temp_texture(ctx);
1431 const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
1432 const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
1433 GLfloat verts[4][5]; /* four verts of X,Y,Z,S,T */
1434 GLenum texIntFormat;
1435 GLboolean fallback, newTex;
1436 GLbitfield metaExtraSave = 0x0;
1437
1438 /*
1439 * Determine if we can do the glDrawPixels with texture mapping.
1440 */
1441 fallback = GL_FALSE;
1442 if (ctx->_ImageTransferState ||
1443 ctx->Fog.Enabled) {
1444 fallback = GL_TRUE;
1445 }
1446
1447 if (_mesa_is_color_format(format)) {
1448 /* use more compact format when possible */
1449 if (format == GL_LUMINANCE || format == GL_LUMINANCE_ALPHA)
1450 texIntFormat = format;
1451 else
1452 texIntFormat = GL_RGBA;
1453 }
1454 else if (_mesa_is_stencil_format(format)) {
1455 if (ctx->Extensions.ARB_fragment_program &&
1456 type == GL_UNSIGNED_BYTE) {
1457 /* We'll store stencil as alpha. This only works for GLubyte
1458 * image data because of how incoming values are mapped to alpha
1459 * in [0,1].
1460 */
1461 texIntFormat = GL_ALPHA;
1462 metaExtraSave = (META_COLOR_MASK |
1463 META_DEPTH_TEST |
1464 META_SHADER |
1465 META_STENCIL_TEST);
1466 }
1467 else {
1468 fallback = GL_TRUE;
1469 }
1470 }
1471 else if (_mesa_is_depth_format(format)) {
1472 if (ctx->Extensions.ARB_depth_texture &&
1473 ctx->Extensions.ARB_fragment_program) {
1474 texIntFormat = GL_DEPTH_COMPONENT;
1475 metaExtraSave = (META_SHADER);
1476 }
1477 else {
1478 fallback = GL_TRUE;
1479 }
1480 }
1481 else {
1482 fallback = GL_TRUE;
1483 }
1484
1485 if (fallback) {
1486 _swrast_DrawPixels(ctx, x, y, width, height,
1487 format, type, unpack, pixels);
1488 return;
1489 }
1490
1491 /*
1492 * Check image size against max texture size, draw as tiles if needed.
1493 */
1494 if (width > tex->MaxSize || height > tex->MaxSize) {
1495 tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
1496 format, type, unpack, pixels);
1497 return;
1498 }
1499
1500 /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
1501 * but a there's a few things we need to override:
1502 */
1503 _mesa_meta_begin(ctx, (META_RASTERIZATION |
1504 META_SHADER |
1505 META_TEXTURE |
1506 META_TRANSFORM |
1507 META_VERTEX |
1508 META_VIEWPORT |
1509 metaExtraSave));
1510
1511 if (drawpix->ArrayObj == 0) {
1512 /* one-time setup */
1513
1514 /* create vertex array object */
1515 _mesa_GenVertexArrays(1, &drawpix->ArrayObj);
1516 _mesa_BindVertexArray(drawpix->ArrayObj);
1517
1518 /* create vertex array buffer */
1519 _mesa_GenBuffersARB(1, &drawpix->VBO);
1520 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
1521 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
1522 NULL, GL_DYNAMIC_DRAW_ARB);
1523
1524 /* setup vertex arrays */
1525 _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
1526 (void *) (0 * sizeof(GLfloat)));
1527 _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
1528 (void *) (3 * sizeof(GLfloat)));
1529 _mesa_EnableClientState(GL_VERTEX_ARRAY);
1530 _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
1531 }
1532 else {
1533 _mesa_BindVertexArray(drawpix->ArrayObj);
1534 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
1535 }
1536
1537 newTex = alloc_texture(tex, width, height, texIntFormat);
1538
1539 /* vertex positions, texcoords (after texture allocation!) */
1540 {
1541 const GLfloat x0 = (GLfloat) x;
1542 const GLfloat y0 = (GLfloat) y;
1543 const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
1544 const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
1545 const GLfloat z = ctx->Current.RasterPos[2];
1546
1547 verts[0][0] = x0;
1548 verts[0][1] = y0;
1549 verts[0][2] = z;
1550 verts[0][3] = 0.0F;
1551 verts[0][4] = 0.0F;
1552 verts[1][0] = x1;
1553 verts[1][1] = y0;
1554 verts[1][2] = z;
1555 verts[1][3] = tex->Sright;
1556 verts[1][4] = 0.0F;
1557 verts[2][0] = x1;
1558 verts[2][1] = y1;
1559 verts[2][2] = z;
1560 verts[2][3] = tex->Sright;
1561 verts[2][4] = tex->Ttop;
1562 verts[3][0] = x0;
1563 verts[3][1] = y1;
1564 verts[3][2] = z;
1565 verts[3][3] = 0.0F;
1566 verts[3][4] = tex->Ttop;
1567
1568 /* upload new vertex data */
1569 _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
1570 }
1571
1572 /* set given unpack params */
1573 ctx->Unpack = *unpack;
1574
1575 _mesa_Enable(tex->Target);
1576
1577 if (_mesa_is_stencil_format(format)) {
1578 /* Drawing stencil */
1579 GLint bit;
1580
1581 if (!drawpix->StencilFP)
1582 init_draw_stencil_pixels(ctx);
1583
1584 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1585 GL_ALPHA, type, pixels);
1586
1587 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1588
1589 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
1590 _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
1591
1592 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
1593 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1594
1595 for (bit = 0; bit < ctx->Visual.stencilBits; bit++) {
1596 const GLuint mask = 1 << bit;
1597 if (mask & origStencilMask) {
1598 _mesa_StencilFunc(GL_ALWAYS, mask, mask);
1599 _mesa_StencilMask(mask);
1600
1601 _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
1602 255.0 / mask, 0.5, 0.0, 0.0);
1603
1604 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1605 }
1606 }
1607 }
1608 else if (_mesa_is_depth_format(format)) {
1609 /* Drawing depth */
1610 if (!drawpix->DepthFP)
1611 init_draw_depth_pixels(ctx);
1612
1613 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
1614 _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
1615
1616 /* polygon color = current raster color */
1617 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
1618 ctx->Current.RasterColor);
1619
1620 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1621 format, type, pixels);
1622
1623 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1624 }
1625 else {
1626 /* Drawing RGBA */
1627 setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
1628 format, type, pixels);
1629 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
1630 }
1631
1632 _mesa_Disable(tex->Target);
1633
1634 /* restore unpack params */
1635 ctx->Unpack = unpackSave;
1636
1637 _mesa_meta_end(ctx);
1638 }