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