d3f8d1368955e4b71bb73ce3f8375cd46dec3d26
[mesa.git] / src / mesa / state_tracker / st_cb_drawpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/format_pack.h"
37 #include "main/macros.h"
38 #include "main/mfeatures.h"
39 #include "main/mtypes.h"
40 #include "main/pack.h"
41 #include "main/pbo.h"
42 #include "main/readpix.h"
43 #include "main/texformat.h"
44 #include "main/teximage.h"
45 #include "main/texstore.h"
46 #include "program/program.h"
47 #include "program/prog_print.h"
48 #include "program/prog_instruction.h"
49
50 #include "st_atom.h"
51 #include "st_atom_constbuf.h"
52 #include "st_cb_drawpixels.h"
53 #include "st_cb_readpixels.h"
54 #include "st_cb_fbo.h"
55 #include "st_context.h"
56 #include "st_debug.h"
57 #include "st_format.h"
58 #include "st_program.h"
59 #include "st_texture.h"
60
61 #include "pipe/p_context.h"
62 #include "pipe/p_defines.h"
63 #include "tgsi/tgsi_ureg.h"
64 #include "util/u_draw_quad.h"
65 #include "util/u_format.h"
66 #include "util/u_inlines.h"
67 #include "util/u_math.h"
68 #include "util/u_tile.h"
69 #include "util/u_upload_mgr.h"
70 #include "cso_cache/cso_context.h"
71
72
73 #if FEATURE_drawpix
74
75 /**
76 * Check if the given program is:
77 * 0: MOVE result.color, fragment.color;
78 * 1: END;
79 */
80 static GLboolean
81 is_passthrough_program(const struct gl_fragment_program *prog)
82 {
83 if (prog->Base.NumInstructions == 2) {
84 const struct prog_instruction *inst = prog->Base.Instructions;
85 if (inst[0].Opcode == OPCODE_MOV &&
86 inst[1].Opcode == OPCODE_END &&
87 inst[0].DstReg.File == PROGRAM_OUTPUT &&
88 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
89 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
90 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
91 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
92 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
93 return GL_TRUE;
94 }
95 }
96 return GL_FALSE;
97 }
98
99
100 /**
101 * Returns a fragment program which implements the current pixel transfer ops.
102 */
103 static struct gl_fragment_program *
104 get_glsl_pixel_transfer_program(struct st_context *st,
105 struct st_fragment_program *orig)
106 {
107 int pixelMaps = 0, scaleAndBias = 0;
108 struct gl_context *ctx = st->ctx;
109 struct st_fragment_program *fp = (struct st_fragment_program *)
110 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
111
112 if (!fp)
113 return NULL;
114
115 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
116 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
117 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
118 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
119 scaleAndBias = 1;
120 }
121
122 pixelMaps = ctx->Pixel.MapColorFlag;
123
124 if (pixelMaps) {
125 /* create the colormap/texture now if not already done */
126 if (!st->pixel_xfer.pixelmap_texture) {
127 st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
128 st->pixel_xfer.pixelmap_sampler_view =
129 st_create_texture_sampler_view(st->pipe,
130 st->pixel_xfer.pixelmap_texture);
131 }
132 }
133
134 get_pixel_transfer_visitor(fp, orig->glsl_to_tgsi,
135 scaleAndBias, pixelMaps);
136
137 return &fp->Base;
138 }
139
140
141 /**
142 * Make fragment shader for glDraw/CopyPixels. This shader is made
143 * by combining the pixel transfer shader with the user-defined shader.
144 * \param fpIn the current/incoming fragment program
145 * \param fpOut returns the combined fragment program
146 */
147 void
148 st_make_drawpix_fragment_program(struct st_context *st,
149 struct gl_fragment_program *fpIn,
150 struct gl_fragment_program **fpOut)
151 {
152 struct gl_program *newProg;
153 struct st_fragment_program *stfp = (struct st_fragment_program *) fpIn;
154
155 if (is_passthrough_program(fpIn)) {
156 newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
157 &st->pixel_xfer.program->Base);
158 }
159 else if (stfp->glsl_to_tgsi != NULL) {
160 newProg = (struct gl_program *) get_glsl_pixel_transfer_program(st, stfp);
161 }
162 else {
163 #if 0
164 /* debug */
165 printf("Base program:\n");
166 _mesa_print_program(&fpIn->Base);
167 printf("DrawPix program:\n");
168 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
169 #endif
170 newProg = _mesa_combine_programs(st->ctx,
171 &st->pixel_xfer.program->Base.Base,
172 &fpIn->Base);
173 }
174
175 #if 0
176 /* debug */
177 printf("Combined DrawPixels program:\n");
178 _mesa_print_program(newProg);
179 printf("InputsRead: 0x%x\n", newProg->InputsRead);
180 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
181 _mesa_print_parameter_list(newProg->Parameters);
182 #endif
183
184 *fpOut = (struct gl_fragment_program *) newProg;
185 }
186
187
188 /**
189 * Create fragment program that does a TEX() instruction to get a Z and/or
190 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
191 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
192 * Pass fragment color through as-is.
193 * \return pointer to the gl_fragment program
194 */
195 struct gl_fragment_program *
196 st_make_drawpix_z_stencil_program(struct st_context *st,
197 GLboolean write_depth,
198 GLboolean write_stencil)
199 {
200 struct gl_context *ctx = st->ctx;
201 struct gl_program *p;
202 struct gl_fragment_program *fp;
203 GLuint ic = 0;
204 const GLuint shaderIndex = write_depth * 2 + write_stencil;
205
206 assert(shaderIndex < Elements(st->drawpix.shaders));
207
208 if (st->drawpix.shaders[shaderIndex]) {
209 /* already have the proper shader */
210 return st->drawpix.shaders[shaderIndex];
211 }
212
213 /*
214 * Create shader now
215 */
216 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
217 if (!p)
218 return NULL;
219
220 p->NumInstructions = write_depth ? 3 : 1;
221 p->NumInstructions += write_stencil ? 1 : 0;
222
223 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
224 if (!p->Instructions) {
225 ctx->Driver.DeleteProgram(ctx, p);
226 return NULL;
227 }
228 _mesa_init_instructions(p->Instructions, p->NumInstructions);
229
230 if (write_depth) {
231 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
232 p->Instructions[ic].Opcode = OPCODE_TEX;
233 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
234 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
235 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
236 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
237 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
238 p->Instructions[ic].TexSrcUnit = 0;
239 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
240 ic++;
241 /* MOV result.color, fragment.color; */
242 p->Instructions[ic].Opcode = OPCODE_MOV;
243 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
244 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR;
245 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
246 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0;
247 ic++;
248 }
249
250 if (write_stencil) {
251 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
252 p->Instructions[ic].Opcode = OPCODE_TEX;
253 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
254 p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
255 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
256 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
257 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
258 p->Instructions[ic].TexSrcUnit = 1;
259 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
260 ic++;
261 }
262
263 /* END; */
264 p->Instructions[ic++].Opcode = OPCODE_END;
265
266 assert(ic == p->NumInstructions);
267
268 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
269 p->OutputsWritten = 0;
270 if (write_depth) {
271 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_DEPTH);
272 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_COLOR);
273 }
274 if (write_stencil)
275 p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_STENCIL);
276
277 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
278 if (write_stencil)
279 p->SamplersUsed |= 1 << 1;
280
281 fp = (struct gl_fragment_program *) p;
282
283 /* save the new shader */
284 st->drawpix.shaders[shaderIndex] = fp;
285
286 return fp;
287 }
288
289
290 /**
291 * Create a simple vertex shader that just passes through the
292 * vertex position and texcoord (and optionally, color).
293 */
294 static void *
295 make_passthrough_vertex_shader(struct st_context *st,
296 GLboolean passColor)
297 {
298 if (!st->drawpix.vert_shaders[passColor]) {
299 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
300
301 if (ureg == NULL)
302 return NULL;
303
304 /* MOV result.pos, vertex.pos; */
305 ureg_MOV(ureg,
306 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
307 ureg_DECL_vs_input( ureg, 0 ));
308
309 /* MOV result.texcoord0, vertex.attr[1]; */
310 ureg_MOV(ureg,
311 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
312 ureg_DECL_vs_input( ureg, 1 ));
313
314 if (passColor) {
315 /* MOV result.color0, vertex.attr[2]; */
316 ureg_MOV(ureg,
317 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
318 ureg_DECL_vs_input( ureg, 2 ));
319 }
320
321 ureg_END( ureg );
322
323 st->drawpix.vert_shaders[passColor] =
324 ureg_create_shader_and_destroy( ureg, st->pipe );
325 }
326
327 return st->drawpix.vert_shaders[passColor];
328 }
329
330
331 /**
332 * Return a texture internalFormat for drawing/copying an image
333 * of the given format and type.
334 */
335 static GLenum
336 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
337 {
338 switch (format) {
339 case GL_DEPTH_COMPONENT:
340 switch (type) {
341 case GL_UNSIGNED_SHORT:
342 return GL_DEPTH_COMPONENT16;
343
344 case GL_UNSIGNED_INT:
345 return GL_DEPTH_COMPONENT32;
346
347 case GL_FLOAT:
348 if (ctx->Extensions.ARB_depth_buffer_float)
349 return GL_DEPTH_COMPONENT32F;
350 else
351 return GL_DEPTH_COMPONENT;
352
353 default:
354 return GL_DEPTH_COMPONENT;
355 }
356
357 case GL_DEPTH_STENCIL:
358 switch (type) {
359 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
360 return GL_DEPTH32F_STENCIL8;
361
362 case GL_UNSIGNED_INT_24_8:
363 default:
364 return GL_DEPTH24_STENCIL8;
365 }
366
367 case GL_STENCIL_INDEX:
368 return GL_STENCIL_INDEX;
369
370 default:
371 if (_mesa_is_integer_format(format)) {
372 switch (type) {
373 case GL_BYTE:
374 return GL_RGBA8I;
375 case GL_UNSIGNED_BYTE:
376 return GL_RGBA8UI;
377 case GL_SHORT:
378 return GL_RGBA16I;
379 case GL_UNSIGNED_SHORT:
380 return GL_RGBA16UI;
381 case GL_INT:
382 return GL_RGBA32I;
383 case GL_UNSIGNED_INT:
384 return GL_RGBA32UI;
385 default:
386 assert(0 && "Unexpected type in internal_format()");
387 return GL_RGBA_INTEGER;
388 }
389 }
390 else {
391 switch (type) {
392 case GL_UNSIGNED_BYTE:
393 case GL_UNSIGNED_INT_8_8_8_8:
394 case GL_UNSIGNED_INT_8_8_8_8_REV:
395 default:
396 return GL_RGBA8;
397
398 case GL_UNSIGNED_BYTE_3_3_2:
399 case GL_UNSIGNED_BYTE_2_3_3_REV:
400 case GL_UNSIGNED_SHORT_4_4_4_4:
401 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
402 return GL_RGBA4;
403
404 case GL_UNSIGNED_SHORT_5_6_5:
405 case GL_UNSIGNED_SHORT_5_6_5_REV:
406 case GL_UNSIGNED_SHORT_5_5_5_1:
407 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
408 return GL_RGB5_A1;
409
410 case GL_UNSIGNED_INT_10_10_10_2:
411 case GL_UNSIGNED_INT_2_10_10_10_REV:
412 return GL_RGB10_A2;
413
414 case GL_UNSIGNED_SHORT:
415 case GL_UNSIGNED_INT:
416 return GL_RGBA16;
417
418 case GL_BYTE:
419 return
420 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
421
422 case GL_SHORT:
423 case GL_INT:
424 return
425 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
426
427 case GL_HALF_FLOAT_ARB:
428 return
429 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
430 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
431
432 case GL_FLOAT:
433 case GL_DOUBLE:
434 return
435 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
436 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
437
438 case GL_UNSIGNED_INT_5_9_9_9_REV:
439 assert(ctx->Extensions.EXT_texture_shared_exponent);
440 return GL_RGB9_E5;
441
442 case GL_UNSIGNED_INT_10F_11F_11F_REV:
443 assert(ctx->Extensions.EXT_packed_float);
444 return GL_R11F_G11F_B10F;
445 }
446 }
447 }
448 }
449
450
451 /**
452 * Create a temporary texture to hold an image of the given size.
453 * If width, height are not POT and the driver only handles POT textures,
454 * allocate the next larger size of texture that is POT.
455 */
456 static struct pipe_resource *
457 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
458 enum pipe_format texFormat)
459 {
460 struct pipe_resource *pt;
461
462 pt = st_texture_create(st, st->internal_target, texFormat, 0,
463 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
464
465 return pt;
466 }
467
468
469 /**
470 * Make texture containing an image for glDrawPixels image.
471 * If 'pixels' is NULL, leave the texture image data undefined.
472 */
473 static struct pipe_resource *
474 make_texture(struct st_context *st,
475 GLsizei width, GLsizei height, GLenum format, GLenum type,
476 const struct gl_pixelstore_attrib *unpack,
477 const GLvoid *pixels)
478 {
479 struct gl_context *ctx = st->ctx;
480 struct pipe_context *pipe = st->pipe;
481 gl_format mformat;
482 struct pipe_resource *pt;
483 enum pipe_format pipeFormat;
484 GLenum baseInternalFormat, intFormat;
485
486 intFormat = internal_format(ctx, format, type);
487 baseInternalFormat = _mesa_base_tex_format(ctx, intFormat);
488
489 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
490 format, type, GL_FALSE);
491 assert(mformat);
492
493 pipeFormat = st_mesa_format_to_pipe_format(mformat);
494 assert(pipeFormat);
495
496 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
497 if (!pixels)
498 return NULL;
499
500 /* alloc temporary texture */
501 pt = alloc_texture(st, width, height, pipeFormat);
502 if (!pt) {
503 _mesa_unmap_pbo_source(ctx, unpack);
504 return NULL;
505 }
506
507 {
508 struct pipe_transfer *transfer;
509 GLboolean success;
510 GLubyte *dest;
511 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
512
513 /* we'll do pixel transfer in a fragment shader */
514 ctx->_ImageTransferState = 0x0;
515
516 transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
517 PIPE_TRANSFER_WRITE, 0, 0,
518 width, height);
519
520 /* map texture transfer */
521 dest = pipe_transfer_map(pipe, transfer);
522
523
524 /* Put image into texture transfer.
525 * Note that the image is actually going to be upside down in
526 * the texture. We deal with that with texcoords.
527 */
528 success = _mesa_texstore(ctx, 2, /* dims */
529 baseInternalFormat, /* baseInternalFormat */
530 mformat, /* gl_format */
531 transfer->stride, /* dstRowStride, bytes */
532 &dest, /* destSlices */
533 width, height, 1, /* size */
534 format, type, /* src format/type */
535 pixels, /* data source */
536 unpack);
537
538 /* unmap */
539 pipe_transfer_unmap(pipe, transfer);
540 pipe->transfer_destroy(pipe, transfer);
541
542 assert(success);
543
544 /* restore */
545 ctx->_ImageTransferState = imageTransferStateSave;
546 }
547
548 _mesa_unmap_pbo_source(ctx, unpack);
549
550 return pt;
551 }
552
553
554 /**
555 * Draw quad with texcoords and optional color.
556 * Coords are gallium window coords with y=0=top.
557 * \param color may be null
558 * \param invertTex if true, flip texcoords vertically
559 */
560 static void
561 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
562 GLfloat x1, GLfloat y1, const GLfloat *color,
563 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
564 {
565 struct st_context *st = st_context(ctx);
566 struct pipe_context *pipe = st->pipe;
567 GLfloat (*verts)[3][4]; /* four verts, three attribs, XYZW */
568 struct pipe_resource *buf = NULL;
569 unsigned offset;
570
571 u_upload_alloc(st->uploader, 0, 4 * sizeof(verts[0]), &offset, &buf,
572 (void**)&verts);
573 if (!buf) {
574 return;
575 }
576
577 /* setup vertex data */
578 {
579 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
580 const GLfloat fb_width = (GLfloat) fb->Width;
581 const GLfloat fb_height = (GLfloat) fb->Height;
582 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
583 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
584 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
585 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
586 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
587 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
588 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
589 GLuint i;
590
591 /* upper-left */
592 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
593 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
594
595 /* upper-right */
596 verts[1][0][0] = clip_x1;
597 verts[1][0][1] = clip_y0;
598
599 /* lower-right */
600 verts[2][0][0] = clip_x1;
601 verts[2][0][1] = clip_y1;
602
603 /* lower-left */
604 verts[3][0][0] = clip_x0;
605 verts[3][0][1] = clip_y1;
606
607 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
608 verts[0][1][1] = tTop; /* v[0].attr[1].T */
609 verts[1][1][0] = sRight;
610 verts[1][1][1] = tTop;
611 verts[2][1][0] = sRight;
612 verts[2][1][1] = tBot;
613 verts[3][1][0] = sLeft;
614 verts[3][1][1] = tBot;
615
616 /* same for all verts: */
617 if (color) {
618 for (i = 0; i < 4; i++) {
619 verts[i][0][2] = z; /* v[i].attr[0].z */
620 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
621 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
622 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
623 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
624 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
625 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
626 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
627 }
628 }
629 else {
630 for (i = 0; i < 4; i++) {
631 verts[i][0][2] = z; /*Z*/
632 verts[i][0][3] = 1.0f; /*W*/
633 verts[i][1][2] = 0.0f; /*R*/
634 verts[i][1][3] = 1.0f; /*Q*/
635 }
636 }
637 }
638
639 u_upload_unmap(st->uploader);
640 util_draw_vertex_buffer(pipe, st->cso_context, buf, offset,
641 PIPE_PRIM_QUADS,
642 4, /* verts */
643 3); /* attribs/vert */
644 pipe_resource_reference(&buf, NULL);
645 }
646
647
648
649 static void
650 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
651 GLsizei width, GLsizei height,
652 GLfloat zoomX, GLfloat zoomY,
653 struct pipe_sampler_view **sv,
654 int num_sampler_view,
655 void *driver_vp,
656 void *driver_fp,
657 const GLfloat *color,
658 GLboolean invertTex,
659 GLboolean write_depth, GLboolean write_stencil)
660 {
661 struct st_context *st = st_context(ctx);
662 struct pipe_context *pipe = st->pipe;
663 struct cso_context *cso = st->cso_context;
664 GLfloat x0, y0, x1, y1;
665 GLsizei maxSize;
666 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
667
668 /* limit checks */
669 /* XXX if DrawPixels image is larger than max texture size, break
670 * it up into chunks.
671 */
672 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
673 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
674 assert(width <= maxSize);
675 assert(height <= maxSize);
676
677 cso_save_rasterizer(cso);
678 cso_save_viewport(cso);
679 cso_save_samplers(cso);
680 cso_save_fragment_sampler_views(cso);
681 cso_save_fragment_shader(cso);
682 cso_save_stream_outputs(cso);
683 cso_save_vertex_shader(cso);
684 cso_save_geometry_shader(cso);
685 cso_save_vertex_elements(cso);
686 cso_save_vertex_buffers(cso);
687 if (write_stencil) {
688 cso_save_depth_stencil_alpha(cso);
689 cso_save_blend(cso);
690 }
691
692 /* rasterizer state: just scissor */
693 {
694 struct pipe_rasterizer_state rasterizer;
695 memset(&rasterizer, 0, sizeof(rasterizer));
696 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
697 ctx->Color._ClampFragmentColor;
698 rasterizer.gl_rasterization_rules = 1;
699 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
700 rasterizer.scissor = ctx->Scissor.Enabled;
701 cso_set_rasterizer(cso, &rasterizer);
702 }
703
704 if (write_stencil) {
705 /* Stencil writing bypasses the normal fragment pipeline to
706 * disable color writing and set stencil test to always pass.
707 */
708 struct pipe_depth_stencil_alpha_state dsa;
709 struct pipe_blend_state blend;
710
711 /* depth/stencil */
712 memset(&dsa, 0, sizeof(dsa));
713 dsa.stencil[0].enabled = 1;
714 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
715 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
716 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
717 if (write_depth) {
718 /* writing depth+stencil: depth test always passes */
719 dsa.depth.enabled = 1;
720 dsa.depth.writemask = ctx->Depth.Mask;
721 dsa.depth.func = PIPE_FUNC_ALWAYS;
722 }
723 cso_set_depth_stencil_alpha(cso, &dsa);
724
725 /* blend (colormask) */
726 memset(&blend, 0, sizeof(blend));
727 cso_set_blend(cso, &blend);
728 }
729
730 /* fragment shader state: TEX lookup program */
731 cso_set_fragment_shader_handle(cso, driver_fp);
732
733 /* vertex shader state: position + texcoord pass-through */
734 cso_set_vertex_shader_handle(cso, driver_vp);
735
736 /* geometry shader state: disabled */
737 cso_set_geometry_shader_handle(cso, NULL);
738
739 /* texture sampling state: */
740 {
741 struct pipe_sampler_state sampler;
742 memset(&sampler, 0, sizeof(sampler));
743 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
744 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
745 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
746 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
747 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
748 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
749 sampler.normalized_coords = normalized;
750
751 cso_single_sampler(cso, 0, &sampler);
752 if (num_sampler_view > 1) {
753 cso_single_sampler(cso, 1, &sampler);
754 }
755 cso_single_sampler_done(cso);
756 }
757
758 /* viewport state: viewport matching window dims */
759 {
760 const float w = (float) ctx->DrawBuffer->Width;
761 const float h = (float) ctx->DrawBuffer->Height;
762 struct pipe_viewport_state vp;
763 vp.scale[0] = 0.5f * w;
764 vp.scale[1] = -0.5f * h;
765 vp.scale[2] = 0.5f;
766 vp.scale[3] = 1.0f;
767 vp.translate[0] = 0.5f * w;
768 vp.translate[1] = 0.5f * h;
769 vp.translate[2] = 0.5f;
770 vp.translate[3] = 0.0f;
771 cso_set_viewport(cso, &vp);
772 }
773
774 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
775 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
776
777 /* texture state: */
778 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
779
780 /* Compute Gallium window coords (y=0=top) with pixel zoom.
781 * Recall that these coords are transformed by the current
782 * vertex shader and viewport transformation.
783 */
784 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
785 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
786 invertTex = !invertTex;
787 }
788
789 x0 = (GLfloat) x;
790 x1 = x + width * ctx->Pixel.ZoomX;
791 y0 = (GLfloat) y;
792 y1 = y + height * ctx->Pixel.ZoomY;
793
794 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
795 z = z * 2.0 - 1.0;
796
797 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
798 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
799 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
800
801 /* restore state */
802 cso_restore_rasterizer(cso);
803 cso_restore_viewport(cso);
804 cso_restore_samplers(cso);
805 cso_restore_fragment_sampler_views(cso);
806 cso_restore_fragment_shader(cso);
807 cso_restore_vertex_shader(cso);
808 cso_restore_geometry_shader(cso);
809 cso_restore_vertex_elements(cso);
810 cso_restore_vertex_buffers(cso);
811 cso_restore_stream_outputs(cso);
812 if (write_stencil) {
813 cso_restore_depth_stencil_alpha(cso);
814 cso_restore_blend(cso);
815 }
816 }
817
818
819 /**
820 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
821 * can't use a fragment shader to write stencil values.
822 */
823 static void
824 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
825 GLsizei width, GLsizei height, GLenum format, GLenum type,
826 const struct gl_pixelstore_attrib *unpack,
827 const GLvoid *pixels)
828 {
829 struct st_context *st = st_context(ctx);
830 struct pipe_context *pipe = st->pipe;
831 struct st_renderbuffer *strb;
832 enum pipe_transfer_usage usage;
833 struct pipe_transfer *pt;
834 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
835 ubyte *stmap;
836 struct gl_pixelstore_attrib clippedUnpack = *unpack;
837 GLubyte *sValues;
838 GLuint *zValues;
839
840 if (!zoom) {
841 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
842 &clippedUnpack)) {
843 /* totally clipped */
844 return;
845 }
846 }
847
848 strb = st_renderbuffer(ctx->DrawBuffer->
849 Attachment[BUFFER_STENCIL].Renderbuffer);
850
851 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
852 y = ctx->DrawBuffer->Height - y - height;
853 }
854
855 if (format == GL_STENCIL_INDEX &&
856 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
857 /* writing stencil to a combined depth+stencil buffer */
858 usage = PIPE_TRANSFER_READ_WRITE;
859 }
860 else {
861 usage = PIPE_TRANSFER_WRITE;
862 }
863
864 pt = pipe_get_transfer(pipe, strb->texture,
865 strb->rtt_level, strb->rtt_face + strb->rtt_slice,
866 usage, x, y,
867 width, height);
868
869 stmap = pipe_transfer_map(pipe, pt);
870
871 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
872 assert(pixels);
873
874 sValues = (GLubyte *) malloc(width * sizeof(GLubyte));
875 zValues = (GLuint *) malloc(width * sizeof(GLuint));
876
877 if (sValues && zValues) {
878 GLint row;
879 for (row = 0; row < height; row++) {
880 GLfloat *zValuesFloat = (GLfloat*)zValues;
881 GLenum destType = GL_UNSIGNED_BYTE;
882 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
883 width, height,
884 format, type,
885 row, 0);
886 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
887 type, source, &clippedUnpack,
888 ctx->_ImageTransferState);
889
890 if (format == GL_DEPTH_STENCIL) {
891 GLenum ztype =
892 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
893 GL_FLOAT : GL_UNSIGNED_INT;
894
895 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
896 (1 << 24) - 1, type, source,
897 &clippedUnpack);
898 }
899
900 if (zoom) {
901 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
902 "zoom not complete");
903 }
904
905 {
906 GLint spanY;
907
908 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
909 spanY = height - row - 1;
910 }
911 else {
912 spanY = row;
913 }
914
915 /* now pack the stencil (and Z) values in the dest format */
916 switch (pt->resource->format) {
917 case PIPE_FORMAT_S8_UINT:
918 {
919 ubyte *dest = stmap + spanY * pt->stride;
920 assert(usage == PIPE_TRANSFER_WRITE);
921 memcpy(dest, sValues, width);
922 }
923 break;
924 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
925 if (format == GL_DEPTH_STENCIL) {
926 uint *dest = (uint *) (stmap + spanY * pt->stride);
927 GLint k;
928 assert(usage == PIPE_TRANSFER_WRITE);
929 for (k = 0; k < width; k++) {
930 dest[k] = zValues[k] | (sValues[k] << 24);
931 }
932 }
933 else {
934 uint *dest = (uint *) (stmap + spanY * pt->stride);
935 GLint k;
936 assert(usage == PIPE_TRANSFER_READ_WRITE);
937 for (k = 0; k < width; k++) {
938 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
939 }
940 }
941 break;
942 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
943 if (format == GL_DEPTH_STENCIL) {
944 uint *dest = (uint *) (stmap + spanY * pt->stride);
945 GLint k;
946 assert(usage == PIPE_TRANSFER_WRITE);
947 for (k = 0; k < width; k++) {
948 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
949 }
950 }
951 else {
952 uint *dest = (uint *) (stmap + spanY * pt->stride);
953 GLint k;
954 assert(usage == PIPE_TRANSFER_READ_WRITE);
955 for (k = 0; k < width; k++) {
956 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
957 }
958 }
959 break;
960 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
961 if (format == GL_DEPTH_STENCIL) {
962 uint *dest = (uint *) (stmap + spanY * pt->stride);
963 GLfloat *destf = (GLfloat*)dest;
964 GLint k;
965 assert(usage == PIPE_TRANSFER_WRITE);
966 for (k = 0; k < width; k++) {
967 destf[k*2] = zValuesFloat[k];
968 dest[k*2+1] = sValues[k] & 0xff;
969 }
970 }
971 else {
972 uint *dest = (uint *) (stmap + spanY * pt->stride);
973 GLint k;
974 assert(usage == PIPE_TRANSFER_READ_WRITE);
975 for (k = 0; k < width; k++) {
976 dest[k*2+1] = sValues[k] & 0xff;
977 }
978 }
979 break;
980 default:
981 assert(0);
982 }
983 }
984 }
985 }
986 else {
987 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
988 }
989
990 free(sValues);
991 free(zValues);
992
993 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
994
995 /* unmap the stencil buffer */
996 pipe_transfer_unmap(pipe, pt);
997 pipe->transfer_destroy(pipe, pt);
998 }
999
1000
1001 /**
1002 * Get fragment program variant for a glDrawPixels or glCopyPixels
1003 * command for RGBA data.
1004 */
1005 static struct st_fp_variant *
1006 get_color_fp_variant(struct st_context *st)
1007 {
1008 struct gl_context *ctx = st->ctx;
1009 struct st_fp_variant_key key;
1010 struct st_fp_variant *fpv;
1011
1012 memset(&key, 0, sizeof(key));
1013
1014 key.st = st;
1015 key.drawpixels = 1;
1016 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
1017 ctx->Pixel.RedScale != 1.0 ||
1018 ctx->Pixel.GreenBias != 0.0 ||
1019 ctx->Pixel.GreenScale != 1.0 ||
1020 ctx->Pixel.BlueBias != 0.0 ||
1021 ctx->Pixel.BlueScale != 1.0 ||
1022 ctx->Pixel.AlphaBias != 0.0 ||
1023 ctx->Pixel.AlphaScale != 1.0);
1024 key.pixelMaps = ctx->Pixel.MapColorFlag;
1025 key.clamp_color = st->clamp_frag_color_in_shader &&
1026 st->ctx->Color._ClampFragmentColor;
1027
1028 fpv = st_get_fp_variant(st, st->fp, &key);
1029
1030 return fpv;
1031 }
1032
1033
1034 /**
1035 * Get fragment program variant for a glDrawPixels or glCopyPixels
1036 * command for depth/stencil data.
1037 */
1038 static struct st_fp_variant *
1039 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
1040 GLboolean write_stencil)
1041 {
1042 struct st_fp_variant_key key;
1043 struct st_fp_variant *fpv;
1044
1045 memset(&key, 0, sizeof(key));
1046
1047 key.st = st;
1048 key.drawpixels = 1;
1049 key.drawpixels_z = write_depth;
1050 key.drawpixels_stencil = write_stencil;
1051
1052 fpv = st_get_fp_variant(st, st->fp, &key);
1053
1054 return fpv;
1055 }
1056
1057
1058 /**
1059 * Called via ctx->Driver.DrawPixels()
1060 */
1061 static void
1062 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1063 GLsizei width, GLsizei height,
1064 GLenum format, GLenum type,
1065 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
1066 {
1067 void *driver_vp, *driver_fp;
1068 struct st_context *st = st_context(ctx);
1069 const GLfloat *color;
1070 struct pipe_context *pipe = st->pipe;
1071 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1072 struct pipe_sampler_view *sv[2];
1073 int num_sampler_view = 1;
1074 struct st_fp_variant *fpv;
1075
1076 if (format == GL_DEPTH_STENCIL)
1077 write_stencil = write_depth = GL_TRUE;
1078 else if (format == GL_STENCIL_INDEX)
1079 write_stencil = GL_TRUE;
1080 else if (format == GL_DEPTH_COMPONENT)
1081 write_depth = GL_TRUE;
1082
1083 if (write_stencil &&
1084 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1085 /* software fallback */
1086 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1087 unpack, pixels);
1088 return;
1089 }
1090
1091 /* Mesa state should be up to date by now */
1092 assert(ctx->NewState == 0x0);
1093
1094 st_validate_state(st);
1095
1096 /*
1097 * Get vertex/fragment shaders
1098 */
1099 if (write_depth || write_stencil) {
1100 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
1101
1102 driver_fp = fpv->driver_shader;
1103
1104 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1105
1106 color = ctx->Current.RasterColor;
1107 }
1108 else {
1109 fpv = get_color_fp_variant(st);
1110
1111 driver_fp = fpv->driver_shader;
1112
1113 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1114
1115 color = NULL;
1116 if (st->pixel_xfer.pixelmap_enabled) {
1117 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1118 num_sampler_view++;
1119 }
1120 }
1121
1122 /* update fragment program constants */
1123 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1124
1125 /* draw with textured quad */
1126 {
1127 struct pipe_resource *pt
1128 = make_texture(st, width, height, format, type, unpack, pixels);
1129 if (pt) {
1130 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1131
1132 if (sv[0]) {
1133 /* Create a second sampler view to read stencil.
1134 * The stencil is written using the shader stencil export
1135 * functionality. */
1136 if (write_stencil) {
1137 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
1138
1139 switch (pt->format) {
1140 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1141 case PIPE_FORMAT_X24S8_UINT:
1142 stencil_format = PIPE_FORMAT_X24S8_UINT;
1143 break;
1144 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1145 case PIPE_FORMAT_S8X24_UINT:
1146 stencil_format = PIPE_FORMAT_S8X24_UINT;
1147 break;
1148 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1149 case PIPE_FORMAT_X32_S8X24_UINT:
1150 stencil_format = PIPE_FORMAT_X32_S8X24_UINT;
1151 break;
1152 case PIPE_FORMAT_S8_UINT:
1153 stencil_format = PIPE_FORMAT_S8_UINT;
1154 break;
1155 default:
1156 assert(0);
1157 }
1158
1159 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1160 stencil_format);
1161 num_sampler_view++;
1162 }
1163
1164 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1165 width, height,
1166 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1167 sv,
1168 num_sampler_view,
1169 driver_vp,
1170 driver_fp,
1171 color, GL_FALSE, write_depth, write_stencil);
1172 pipe_sampler_view_reference(&sv[0], NULL);
1173 if (num_sampler_view > 1)
1174 pipe_sampler_view_reference(&sv[1], NULL);
1175 }
1176 pipe_resource_reference(&pt, NULL);
1177 }
1178 }
1179 }
1180
1181
1182
1183 /**
1184 * Software fallback for glCopyPixels(GL_STENCIL).
1185 */
1186 static void
1187 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1188 GLsizei width, GLsizei height,
1189 GLint dstx, GLint dsty)
1190 {
1191 struct st_renderbuffer *rbDraw;
1192 struct pipe_context *pipe = st_context(ctx)->pipe;
1193 enum pipe_transfer_usage usage;
1194 struct pipe_transfer *ptDraw;
1195 ubyte *drawMap;
1196 ubyte *buffer;
1197 int i;
1198
1199 buffer = malloc(width * height * sizeof(ubyte));
1200 if (!buffer) {
1201 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1202 return;
1203 }
1204
1205 /* Get the dest renderbuffer */
1206 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1207 Attachment[BUFFER_STENCIL].Renderbuffer);
1208
1209 /* this will do stencil pixel transfer ops */
1210 _mesa_readpixels(ctx, srcx, srcy, width, height,
1211 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1212 &ctx->DefaultPacking, buffer);
1213
1214 if (0) {
1215 /* debug code: dump stencil values */
1216 GLint row, col;
1217 for (row = 0; row < height; row++) {
1218 printf("%3d: ", row);
1219 for (col = 0; col < width; col++) {
1220 printf("%02x ", buffer[col + row * width]);
1221 }
1222 printf("\n");
1223 }
1224 }
1225
1226 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1227 usage = PIPE_TRANSFER_READ_WRITE;
1228 else
1229 usage = PIPE_TRANSFER_WRITE;
1230
1231 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1232 dsty = rbDraw->Base.Height - dsty - height;
1233 }
1234
1235 ptDraw = pipe_get_transfer(pipe,
1236 rbDraw->texture,
1237 rbDraw->rtt_level,
1238 rbDraw->rtt_face + rbDraw->rtt_slice,
1239 usage, dstx, dsty,
1240 width, height);
1241
1242 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1243 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1244
1245 /* map the stencil buffer */
1246 drawMap = pipe_transfer_map(pipe, ptDraw);
1247
1248 /* draw */
1249 /* XXX PixelZoom not handled yet */
1250 for (i = 0; i < height; i++) {
1251 ubyte *dst;
1252 const ubyte *src;
1253 int y;
1254
1255 y = i;
1256
1257 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1258 y = height - y - 1;
1259 }
1260
1261 dst = drawMap + y * ptDraw->stride;
1262 src = buffer + i * width;
1263
1264 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1265 }
1266
1267 free(buffer);
1268
1269 /* unmap the stencil buffer */
1270 pipe_transfer_unmap(pipe, ptDraw);
1271 pipe->transfer_destroy(pipe, ptDraw);
1272 }
1273
1274
1275 /**
1276 * Return renderbuffer to use for reading color pixels for glCopyPixels
1277 */
1278 static struct st_renderbuffer *
1279 st_get_color_read_renderbuffer(struct gl_context *ctx)
1280 {
1281 struct gl_framebuffer *fb = ctx->ReadBuffer;
1282 struct st_renderbuffer *strb =
1283 st_renderbuffer(fb->_ColorReadBuffer);
1284
1285 return strb;
1286 }
1287
1288
1289 /** Do the src/dest regions overlap? */
1290 static GLboolean
1291 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1292 GLsizei width, GLsizei height)
1293 {
1294 if (srcX + width <= dstX ||
1295 dstX + width <= srcX ||
1296 srcY + height <= dstY ||
1297 dstY + height <= srcY)
1298 return GL_FALSE;
1299 else
1300 return GL_TRUE;
1301 }
1302
1303
1304 /**
1305 * Try to do a glCopyPixels for simple cases with a blit by calling
1306 * pipe->resource_copy_region().
1307 *
1308 * We can do this when we're copying color pixels (depth/stencil
1309 * eventually) with no pixel zoom, no pixel transfer ops, no
1310 * per-fragment ops, the src/dest regions don't overlap and the
1311 * src/dest pixel formats are the same.
1312 */
1313 static GLboolean
1314 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1315 GLsizei width, GLsizei height,
1316 GLint dstx, GLint dsty, GLenum type)
1317 {
1318 struct st_context *st = st_context(ctx);
1319 struct pipe_context *pipe = st->pipe;
1320 struct gl_pixelstore_attrib pack, unpack;
1321 GLint readX, readY, readW, readH;
1322
1323 if (type == GL_COLOR &&
1324 ctx->Pixel.ZoomX == 1.0 &&
1325 ctx->Pixel.ZoomY == 1.0 &&
1326 ctx->_ImageTransferState == 0x0 &&
1327 !ctx->Color.BlendEnabled &&
1328 !ctx->Color.AlphaEnabled &&
1329 !ctx->Depth.Test &&
1330 !ctx->Fog.Enabled &&
1331 !ctx->Stencil.Enabled &&
1332 !ctx->FragmentProgram.Enabled &&
1333 !ctx->VertexProgram.Enabled &&
1334 !ctx->Shader.CurrentFragmentProgram &&
1335 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1336 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1337 !ctx->Query.CondRenderQuery) {
1338 struct st_renderbuffer *rbRead, *rbDraw;
1339 GLint drawX, drawY;
1340
1341 /*
1342 * Clip the read region against the src buffer bounds.
1343 * We'll still allocate a temporary buffer/texture for the original
1344 * src region size but we'll only read the region which is on-screen.
1345 * This may mean that we draw garbage pixels into the dest region, but
1346 * that's expected.
1347 */
1348 readX = srcx;
1349 readY = srcy;
1350 readW = width;
1351 readH = height;
1352 pack = ctx->DefaultPacking;
1353 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1354 return GL_TRUE; /* all done */
1355
1356 /* clip against dest buffer bounds and scissor box */
1357 drawX = dstx + pack.SkipPixels;
1358 drawY = dsty + pack.SkipRows;
1359 unpack = pack;
1360 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1361 return GL_TRUE; /* all done */
1362
1363 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1364 readY = readY - pack.SkipRows + unpack.SkipRows;
1365
1366 rbRead = st_get_color_read_renderbuffer(ctx);
1367 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1368
1369 if ((rbRead != rbDraw ||
1370 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1371 rbRead->Base.Format == rbDraw->Base.Format) {
1372 struct pipe_box srcBox;
1373
1374 /* flip src/dst position if needed */
1375 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1376 /* both buffers will have the same orientation */
1377 readY = ctx->ReadBuffer->Height - readY - readH;
1378 drawY = ctx->DrawBuffer->Height - drawY - readH;
1379 }
1380
1381 u_box_2d(readX, readY, readW, readH, &srcBox);
1382
1383 pipe->resource_copy_region(pipe,
1384 rbDraw->texture,
1385 rbDraw->rtt_level, drawX, drawY, 0,
1386 rbRead->texture,
1387 rbRead->rtt_level, &srcBox);
1388 return GL_TRUE;
1389 }
1390 }
1391
1392 return GL_FALSE;
1393 }
1394
1395
1396 static void
1397 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1398 GLsizei width, GLsizei height,
1399 GLint dstx, GLint dsty, GLenum type)
1400 {
1401 struct st_context *st = st_context(ctx);
1402 struct pipe_context *pipe = st->pipe;
1403 struct pipe_screen *screen = pipe->screen;
1404 struct st_renderbuffer *rbRead;
1405 void *driver_vp, *driver_fp;
1406 struct pipe_resource *pt;
1407 struct pipe_sampler_view *sv[2];
1408 int num_sampler_view = 1;
1409 GLfloat *color;
1410 enum pipe_format srcFormat, texFormat;
1411 GLboolean invertTex = GL_FALSE;
1412 GLint readX, readY, readW, readH;
1413 GLuint sample_count;
1414 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1415 struct st_fp_variant *fpv;
1416
1417 st_validate_state(st);
1418
1419 if (type == GL_DEPTH_STENCIL) {
1420 /* XXX make this more efficient */
1421 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1422 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1423 return;
1424 }
1425
1426 if (type == GL_STENCIL) {
1427 /* can't use texturing to do stencil */
1428 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1429 return;
1430 }
1431
1432 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1433 return;
1434
1435 /*
1436 * The subsequent code implements glCopyPixels by copying the source
1437 * pixels into a temporary texture that's then applied to a textured quad.
1438 * When we draw the textured quad, all the usual per-fragment operations
1439 * are handled.
1440 */
1441
1442
1443 /*
1444 * Get vertex/fragment shaders
1445 */
1446 if (type == GL_COLOR) {
1447 rbRead = st_get_color_read_renderbuffer(ctx);
1448 color = NULL;
1449
1450 fpv = get_color_fp_variant(st);
1451 driver_fp = fpv->driver_shader;
1452
1453 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1454
1455 if (st->pixel_xfer.pixelmap_enabled) {
1456 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1457 num_sampler_view++;
1458 }
1459 }
1460 else {
1461 assert(type == GL_DEPTH);
1462 rbRead = st_renderbuffer(ctx->ReadBuffer->
1463 Attachment[BUFFER_DEPTH].Renderbuffer);
1464 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1465
1466 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1467 driver_fp = fpv->driver_shader;
1468
1469 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1470 }
1471
1472 /* update fragment program constants */
1473 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1474
1475 sample_count = rbRead->texture->nr_samples;
1476 /* I believe this would be legal, presumably would need to do a resolve
1477 for color, and for depth/stencil spec says to just use one of the
1478 depth/stencil samples per pixel? Need some transfer clarifications. */
1479 assert(sample_count < 2);
1480
1481 srcFormat = rbRead->texture->format;
1482
1483 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1484 sample_count,
1485 PIPE_BIND_SAMPLER_VIEW)) {
1486 texFormat = srcFormat;
1487 }
1488 else {
1489 /* srcFormat can't be used as a texture format */
1490 if (type == GL_DEPTH) {
1491 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1492 GL_NONE, GL_NONE, st->internal_target,
1493 sample_count, PIPE_BIND_DEPTH_STENCIL);
1494 assert(texFormat != PIPE_FORMAT_NONE);
1495 }
1496 else {
1497 /* default color format */
1498 texFormat = st_choose_format(screen, GL_RGBA,
1499 GL_NONE, GL_NONE, st->internal_target,
1500 sample_count, PIPE_BIND_SAMPLER_VIEW);
1501 assert(texFormat != PIPE_FORMAT_NONE);
1502 }
1503 }
1504
1505 /* Invert src region if needed */
1506 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1507 srcy = ctx->ReadBuffer->Height - srcy - height;
1508 invertTex = !invertTex;
1509 }
1510
1511 /* Clip the read region against the src buffer bounds.
1512 * We'll still allocate a temporary buffer/texture for the original
1513 * src region size but we'll only read the region which is on-screen.
1514 * This may mean that we draw garbage pixels into the dest region, but
1515 * that's expected.
1516 */
1517 readX = srcx;
1518 readY = srcy;
1519 readW = width;
1520 readH = height;
1521 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1522 /* The source region is completely out of bounds. Do nothing.
1523 * The GL spec says "Results of copies from outside the window,
1524 * or from regions of the window that are not exposed, are
1525 * hardware dependent and undefined."
1526 */
1527 return;
1528 }
1529
1530 readW = MAX2(0, readW);
1531 readH = MAX2(0, readH);
1532
1533 /* alloc temporary texture */
1534 pt = alloc_texture(st, width, height, texFormat);
1535 if (!pt)
1536 return;
1537
1538 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1539 if (!sv[0]) {
1540 pipe_resource_reference(&pt, NULL);
1541 return;
1542 }
1543
1544 /* Make temporary texture which is a copy of the src region.
1545 */
1546 if (srcFormat == texFormat) {
1547 struct pipe_box src_box;
1548 u_box_2d(readX, readY, readW, readH, &src_box);
1549 /* copy source framebuffer surface into mipmap/texture */
1550 pipe->resource_copy_region(pipe,
1551 pt, /* dest tex */
1552 0, /* dest lvl */
1553 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1554 rbRead->texture, /* src tex */
1555 rbRead->rtt_level, /* src lvl */
1556 &src_box);
1557
1558 }
1559 else {
1560 /* CPU-based fallback/conversion */
1561 struct pipe_transfer *ptRead =
1562 pipe_get_transfer(st->pipe, rbRead->texture,
1563 rbRead->rtt_level,
1564 rbRead->rtt_face + rbRead->rtt_slice,
1565 PIPE_TRANSFER_READ,
1566 readX, readY, readW, readH);
1567 struct pipe_transfer *ptTex;
1568 enum pipe_transfer_usage transfer_usage;
1569
1570 if (ST_DEBUG & DEBUG_FALLBACK)
1571 debug_printf("%s: fallback processing\n", __FUNCTION__);
1572
1573 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1574 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1575 else
1576 transfer_usage = PIPE_TRANSFER_WRITE;
1577
1578 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1579 0, 0, width, height);
1580
1581 /* copy image from ptRead surface to ptTex surface */
1582 if (type == GL_COLOR) {
1583 /* alternate path using get/put_tile() */
1584 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1585 enum pipe_format readFormat, drawFormat;
1586 readFormat = util_format_linear(rbRead->texture->format);
1587 drawFormat = util_format_linear(pt->format);
1588 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1589 readFormat, buf);
1590 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1591 readW, readH, drawFormat, buf);
1592 free(buf);
1593 }
1594 else {
1595 /* GL_DEPTH */
1596 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1597 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1598 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1599 readW, readH, buf);
1600 free(buf);
1601 }
1602
1603 pipe->transfer_destroy(pipe, ptRead);
1604 pipe->transfer_destroy(pipe, ptTex);
1605 }
1606
1607 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1608 * textured quad with that texture.
1609 */
1610 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1611 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1612 sv,
1613 num_sampler_view,
1614 driver_vp,
1615 driver_fp,
1616 color, invertTex, GL_FALSE, GL_FALSE);
1617
1618 pipe_resource_reference(&pt, NULL);
1619 pipe_sampler_view_reference(&sv[0], NULL);
1620 }
1621
1622
1623
1624 void st_init_drawpixels_functions(struct dd_function_table *functions)
1625 {
1626 functions->DrawPixels = st_DrawPixels;
1627 functions->CopyPixels = st_CopyPixels;
1628 }
1629
1630
1631 void
1632 st_destroy_drawpix(struct st_context *st)
1633 {
1634 GLuint i;
1635
1636 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1637 if (st->drawpix.shaders[i])
1638 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1639 }
1640
1641 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1642 if (st->drawpix.vert_shaders[0])
1643 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1644 if (st->drawpix.vert_shaders[1])
1645 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1646 }
1647
1648 #endif /* FEATURE_drawpix */