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