Squash-merge branch 'gallium-clip-state'
[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 = ctx->Color._ClampFragmentColor;
688 rasterizer.gl_rasterization_rules = 1;
689 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
690 rasterizer.scissor = ctx->Scissor.Enabled;
691 cso_set_rasterizer(cso, &rasterizer);
692 }
693
694 if (write_stencil) {
695 /* Stencil writing bypasses the normal fragment pipeline to
696 * disable color writing and set stencil test to always pass.
697 */
698 struct pipe_depth_stencil_alpha_state dsa;
699 struct pipe_blend_state blend;
700
701 /* depth/stencil */
702 memset(&dsa, 0, sizeof(dsa));
703 dsa.stencil[0].enabled = 1;
704 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
705 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
706 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
707 if (write_depth) {
708 /* writing depth+stencil: depth test always passes */
709 dsa.depth.enabled = 1;
710 dsa.depth.writemask = ctx->Depth.Mask;
711 dsa.depth.func = PIPE_FUNC_ALWAYS;
712 }
713 cso_set_depth_stencil_alpha(cso, &dsa);
714
715 /* blend (colormask) */
716 memset(&blend, 0, sizeof(blend));
717 cso_set_blend(cso, &blend);
718 }
719
720 /* fragment shader state: TEX lookup program */
721 cso_set_fragment_shader_handle(cso, driver_fp);
722
723 /* vertex shader state: position + texcoord pass-through */
724 cso_set_vertex_shader_handle(cso, driver_vp);
725
726 /* geometry shader state: disabled */
727 cso_set_geometry_shader_handle(cso, NULL);
728
729 /* texture sampling state: */
730 {
731 struct pipe_sampler_state sampler;
732 memset(&sampler, 0, sizeof(sampler));
733 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
734 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
735 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
736 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
737 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
738 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
739 sampler.normalized_coords = normalized;
740
741 cso_single_sampler(cso, 0, &sampler);
742 if (num_sampler_view > 1) {
743 cso_single_sampler(cso, 1, &sampler);
744 }
745 cso_single_sampler_done(cso);
746 }
747
748 /* viewport state: viewport matching window dims */
749 {
750 const float w = (float) ctx->DrawBuffer->Width;
751 const float h = (float) ctx->DrawBuffer->Height;
752 struct pipe_viewport_state vp;
753 vp.scale[0] = 0.5f * w;
754 vp.scale[1] = -0.5f * h;
755 vp.scale[2] = 0.5f;
756 vp.scale[3] = 1.0f;
757 vp.translate[0] = 0.5f * w;
758 vp.translate[1] = 0.5f * h;
759 vp.translate[2] = 0.5f;
760 vp.translate[3] = 0.0f;
761 cso_set_viewport(cso, &vp);
762 }
763
764 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
765 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
766
767 /* texture state: */
768 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
769
770 /* Compute Gallium window coords (y=0=top) with pixel zoom.
771 * Recall that these coords are transformed by the current
772 * vertex shader and viewport transformation.
773 */
774 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
775 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
776 invertTex = !invertTex;
777 }
778
779 x0 = (GLfloat) x;
780 x1 = x + width * ctx->Pixel.ZoomX;
781 y0 = (GLfloat) y;
782 y1 = y + height * ctx->Pixel.ZoomY;
783
784 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
785 z = z * 2.0 - 1.0;
786
787 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
788 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
789 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
790
791 /* restore state */
792 cso_restore_rasterizer(cso);
793 cso_restore_viewport(cso);
794 cso_restore_samplers(cso);
795 cso_restore_fragment_sampler_views(cso);
796 cso_restore_fragment_shader(cso);
797 cso_restore_vertex_shader(cso);
798 cso_restore_geometry_shader(cso);
799 cso_restore_vertex_elements(cso);
800 cso_restore_vertex_buffers(cso);
801 cso_restore_stream_outputs(cso);
802 if (write_stencil) {
803 cso_restore_depth_stencil_alpha(cso);
804 cso_restore_blend(cso);
805 }
806 }
807
808
809 /**
810 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
811 * can't use a fragment shader to write stencil values.
812 */
813 static void
814 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
815 GLsizei width, GLsizei height, GLenum format, GLenum type,
816 const struct gl_pixelstore_attrib *unpack,
817 const GLvoid *pixels)
818 {
819 struct st_context *st = st_context(ctx);
820 struct pipe_context *pipe = st->pipe;
821 struct st_renderbuffer *strb;
822 enum pipe_transfer_usage usage;
823 struct pipe_transfer *pt;
824 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
825 GLint skipPixels;
826 ubyte *stmap;
827 struct gl_pixelstore_attrib clippedUnpack = *unpack;
828
829 if (!zoom) {
830 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
831 &clippedUnpack)) {
832 /* totally clipped */
833 return;
834 }
835 }
836
837 strb = st_renderbuffer(ctx->DrawBuffer->
838 Attachment[BUFFER_STENCIL].Renderbuffer);
839
840 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
841 y = ctx->DrawBuffer->Height - y - height;
842 }
843
844 if(format != GL_DEPTH_STENCIL &&
845 util_format_get_component_bits(strb->format,
846 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
847 usage = PIPE_TRANSFER_READ_WRITE;
848 else
849 usage = PIPE_TRANSFER_WRITE;
850
851 pt = pipe_get_transfer(pipe, strb->texture,
852 strb->rtt_level, strb->rtt_face + strb->rtt_slice,
853 usage, x, y,
854 width, height);
855
856 stmap = pipe_transfer_map(pipe, pt);
857
858 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
859 assert(pixels);
860
861 /* if width > MAX_WIDTH, have to process image in chunks */
862 skipPixels = 0;
863 while (skipPixels < width) {
864 const GLint spanX = skipPixels;
865 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
866 GLint row;
867 for (row = 0; row < height; row++) {
868 GLubyte sValues[MAX_WIDTH];
869 GLuint zValues[MAX_WIDTH];
870 GLfloat *zValuesFloat = (GLfloat*)zValues;
871 GLenum destType = GL_UNSIGNED_BYTE;
872 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
873 width, height,
874 format, type,
875 row, skipPixels);
876 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
877 type, source, &clippedUnpack,
878 ctx->_ImageTransferState);
879
880 if (format == GL_DEPTH_STENCIL) {
881 GLenum ztype =
882 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
883 GL_FLOAT : GL_UNSIGNED_INT;
884
885 _mesa_unpack_depth_span(ctx, spanWidth, ztype, zValues,
886 (1 << 24) - 1, type, source,
887 &clippedUnpack);
888 }
889
890 if (zoom) {
891 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
892 "zoom not complete");
893 }
894
895 {
896 GLint spanY;
897
898 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
899 spanY = height - row - 1;
900 }
901 else {
902 spanY = row;
903 }
904
905 /* now pack the stencil (and Z) values in the dest format */
906 switch (pt->resource->format) {
907 case PIPE_FORMAT_S8_UINT:
908 {
909 ubyte *dest = stmap + spanY * pt->stride + spanX;
910 assert(usage == PIPE_TRANSFER_WRITE);
911 memcpy(dest, sValues, spanWidth);
912 }
913 break;
914 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
915 if (format == GL_DEPTH_STENCIL) {
916 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
917 GLint k;
918 assert(usage == PIPE_TRANSFER_WRITE);
919 for (k = 0; k < spanWidth; k++) {
920 dest[k] = zValues[k] | (sValues[k] << 24);
921 }
922 }
923 else {
924 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
925 GLint k;
926 assert(usage == PIPE_TRANSFER_READ_WRITE);
927 for (k = 0; k < spanWidth; k++) {
928 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
929 }
930 }
931 break;
932 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
933 if (format == GL_DEPTH_STENCIL) {
934 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
935 GLint k;
936 assert(usage == PIPE_TRANSFER_WRITE);
937 for (k = 0; k < spanWidth; k++) {
938 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
939 }
940 }
941 else {
942 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
943 GLint k;
944 assert(usage == PIPE_TRANSFER_READ_WRITE);
945 for (k = 0; k < spanWidth; k++) {
946 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
947 }
948 }
949 break;
950 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
951 if (format == GL_DEPTH_STENCIL) {
952 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
953 GLfloat *destf = (GLfloat*)dest;
954 GLint k;
955 assert(usage == PIPE_TRANSFER_WRITE);
956 for (k = 0; k < spanWidth; k++) {
957 destf[k*2] = zValuesFloat[k];
958 dest[k*2+1] = sValues[k] & 0xff;
959 }
960 }
961 else {
962 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
963 GLint k;
964 assert(usage == PIPE_TRANSFER_READ_WRITE);
965 for (k = 0; k < spanWidth; k++) {
966 dest[k*2+1] = sValues[k] & 0xff;
967 }
968 }
969 break;
970 default:
971 assert(0);
972 }
973 }
974 }
975 skipPixels += spanWidth;
976 }
977
978 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
979
980 /* unmap the stencil buffer */
981 pipe_transfer_unmap(pipe, pt);
982 pipe->transfer_destroy(pipe, pt);
983 }
984
985
986 /**
987 * Get fragment program variant for a glDrawPixels or glCopyPixels
988 * command for RGBA data.
989 */
990 static struct st_fp_variant *
991 get_color_fp_variant(struct st_context *st)
992 {
993 struct gl_context *ctx = st->ctx;
994 struct st_fp_variant_key key;
995 struct st_fp_variant *fpv;
996
997 memset(&key, 0, sizeof(key));
998
999 key.st = st;
1000 key.drawpixels = 1;
1001 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
1002 ctx->Pixel.RedScale != 1.0 ||
1003 ctx->Pixel.GreenBias != 0.0 ||
1004 ctx->Pixel.GreenScale != 1.0 ||
1005 ctx->Pixel.BlueBias != 0.0 ||
1006 ctx->Pixel.BlueScale != 1.0 ||
1007 ctx->Pixel.AlphaBias != 0.0 ||
1008 ctx->Pixel.AlphaScale != 1.0);
1009 key.pixelMaps = ctx->Pixel.MapColorFlag;
1010
1011 fpv = st_get_fp_variant(st, st->fp, &key);
1012
1013 return fpv;
1014 }
1015
1016
1017 /**
1018 * Get fragment program variant for a glDrawPixels or glCopyPixels
1019 * command for depth/stencil data.
1020 */
1021 static struct st_fp_variant *
1022 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
1023 GLboolean write_stencil)
1024 {
1025 struct st_fp_variant_key key;
1026 struct st_fp_variant *fpv;
1027
1028 memset(&key, 0, sizeof(key));
1029
1030 key.st = st;
1031 key.drawpixels = 1;
1032 key.drawpixels_z = write_depth;
1033 key.drawpixels_stencil = write_stencil;
1034
1035 fpv = st_get_fp_variant(st, st->fp, &key);
1036
1037 return fpv;
1038 }
1039
1040
1041 /**
1042 * Called via ctx->Driver.DrawPixels()
1043 */
1044 static void
1045 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1046 GLsizei width, GLsizei height,
1047 GLenum format, GLenum type,
1048 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
1049 {
1050 void *driver_vp, *driver_fp;
1051 struct st_context *st = st_context(ctx);
1052 const GLfloat *color;
1053 struct pipe_context *pipe = st->pipe;
1054 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1055 struct pipe_sampler_view *sv[2];
1056 int num_sampler_view = 1;
1057 struct st_fp_variant *fpv;
1058
1059 if (format == GL_DEPTH_STENCIL)
1060 write_stencil = write_depth = GL_TRUE;
1061 else if (format == GL_STENCIL_INDEX)
1062 write_stencil = GL_TRUE;
1063 else if (format == GL_DEPTH_COMPONENT)
1064 write_depth = GL_TRUE;
1065
1066 if (write_stencil &&
1067 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1068 /* software fallback */
1069 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1070 unpack, pixels);
1071 return;
1072 }
1073
1074 /* Mesa state should be up to date by now */
1075 assert(ctx->NewState == 0x0);
1076
1077 st_validate_state(st);
1078
1079 /*
1080 * Get vertex/fragment shaders
1081 */
1082 if (write_depth || write_stencil) {
1083 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
1084
1085 driver_fp = fpv->driver_shader;
1086
1087 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1088
1089 color = ctx->Current.RasterColor;
1090 }
1091 else {
1092 fpv = get_color_fp_variant(st);
1093
1094 driver_fp = fpv->driver_shader;
1095
1096 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1097
1098 color = NULL;
1099 if (st->pixel_xfer.pixelmap_enabled) {
1100 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1101 num_sampler_view++;
1102 }
1103 }
1104
1105 /* update fragment program constants */
1106 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1107
1108 /* draw with textured quad */
1109 {
1110 struct pipe_resource *pt
1111 = make_texture(st, width, height, format, type, unpack, pixels);
1112 if (pt) {
1113 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1114
1115 if (sv[0]) {
1116 /* Create a second sampler view to read stencil.
1117 * The stencil is written using the shader stencil export
1118 * functionality. */
1119 if (write_stencil) {
1120 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
1121
1122 switch (pt->format) {
1123 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1124 case PIPE_FORMAT_X24S8_UINT:
1125 stencil_format = PIPE_FORMAT_X24S8_UINT;
1126 break;
1127 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1128 case PIPE_FORMAT_S8X24_UINT:
1129 stencil_format = PIPE_FORMAT_S8X24_UINT;
1130 break;
1131 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1132 case PIPE_FORMAT_X32_S8X24_UINT:
1133 stencil_format = PIPE_FORMAT_X32_S8X24_UINT;
1134 break;
1135 case PIPE_FORMAT_S8_UINT:
1136 stencil_format = PIPE_FORMAT_S8_UINT;
1137 break;
1138 default:
1139 assert(0);
1140 }
1141
1142 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1143 stencil_format);
1144 num_sampler_view++;
1145 }
1146
1147 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1148 width, height,
1149 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1150 sv,
1151 num_sampler_view,
1152 driver_vp,
1153 driver_fp,
1154 color, GL_FALSE, write_depth, write_stencil);
1155 pipe_sampler_view_reference(&sv[0], NULL);
1156 if (num_sampler_view > 1)
1157 pipe_sampler_view_reference(&sv[1], NULL);
1158 }
1159 pipe_resource_reference(&pt, NULL);
1160 }
1161 }
1162 }
1163
1164
1165
1166 /**
1167 * Software fallback for glCopyPixels(GL_STENCIL).
1168 */
1169 static void
1170 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1171 GLsizei width, GLsizei height,
1172 GLint dstx, GLint dsty)
1173 {
1174 struct st_renderbuffer *rbDraw;
1175 struct pipe_context *pipe = st_context(ctx)->pipe;
1176 enum pipe_transfer_usage usage;
1177 struct pipe_transfer *ptDraw;
1178 ubyte *drawMap;
1179 ubyte *buffer;
1180 int i;
1181
1182 buffer = malloc(width * height * sizeof(ubyte));
1183 if (!buffer) {
1184 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1185 return;
1186 }
1187
1188 /* Get the dest renderbuffer */
1189 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1190 Attachment[BUFFER_STENCIL].Renderbuffer);
1191
1192 /* this will do stencil pixel transfer ops */
1193 _mesa_readpixels(ctx, srcx, srcy, width, height,
1194 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1195 &ctx->DefaultPacking, buffer);
1196
1197 if (0) {
1198 /* debug code: dump stencil values */
1199 GLint row, col;
1200 for (row = 0; row < height; row++) {
1201 printf("%3d: ", row);
1202 for (col = 0; col < width; col++) {
1203 printf("%02x ", buffer[col + row * width]);
1204 }
1205 printf("\n");
1206 }
1207 }
1208
1209 if (util_format_get_component_bits(rbDraw->format,
1210 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
1211 usage = PIPE_TRANSFER_READ_WRITE;
1212 else
1213 usage = PIPE_TRANSFER_WRITE;
1214
1215 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1216 dsty = rbDraw->Base.Height - dsty - height;
1217 }
1218
1219 ptDraw = pipe_get_transfer(pipe,
1220 rbDraw->texture,
1221 rbDraw->rtt_level,
1222 rbDraw->rtt_face + rbDraw->rtt_slice,
1223 usage, dstx, dsty,
1224 width, height);
1225
1226 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1227 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1228
1229 /* map the stencil buffer */
1230 drawMap = pipe_transfer_map(pipe, ptDraw);
1231
1232 /* draw */
1233 /* XXX PixelZoom not handled yet */
1234 for (i = 0; i < height; i++) {
1235 ubyte *dst;
1236 const ubyte *src;
1237 int y;
1238
1239 y = i;
1240
1241 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1242 y = height - y - 1;
1243 }
1244
1245 dst = drawMap + y * ptDraw->stride;
1246 src = buffer + i * width;
1247
1248 switch (ptDraw->resource->format) {
1249 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1250 {
1251 uint *dst4 = (uint *) dst;
1252 int j;
1253 assert(usage == PIPE_TRANSFER_READ_WRITE);
1254 for (j = 0; j < width; j++) {
1255 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
1256 dst4++;
1257 }
1258 }
1259 break;
1260 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1261 {
1262 uint *dst4 = (uint *) dst;
1263 int j;
1264 assert(usage == PIPE_TRANSFER_READ_WRITE);
1265 for (j = 0; j < width; j++) {
1266 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
1267 dst4++;
1268 }
1269 }
1270 break;
1271 case PIPE_FORMAT_S8_UINT:
1272 assert(usage == PIPE_TRANSFER_WRITE);
1273 memcpy(dst, src, width);
1274 break;
1275 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1276 {
1277 uint *dst4 = (uint *) dst;
1278 int j;
1279 dst4++;
1280 assert(usage == PIPE_TRANSFER_READ_WRITE);
1281 for (j = 0; j < width; j++) {
1282 *dst4 = src[j] & 0xff;
1283 dst4 += 2;
1284 }
1285 }
1286 break;
1287 default:
1288 assert(0);
1289 }
1290 }
1291
1292 free(buffer);
1293
1294 /* unmap the stencil buffer */
1295 pipe_transfer_unmap(pipe, ptDraw);
1296 pipe->transfer_destroy(pipe, ptDraw);
1297 }
1298
1299
1300 /**
1301 * Return renderbuffer to use for reading color pixels for glCopyPixels
1302 */
1303 static struct st_renderbuffer *
1304 st_get_color_read_renderbuffer(struct gl_context *ctx)
1305 {
1306 struct gl_framebuffer *fb = ctx->ReadBuffer;
1307 struct st_renderbuffer *strb =
1308 st_renderbuffer(fb->_ColorReadBuffer);
1309
1310 return strb;
1311 }
1312
1313
1314 /** Do the src/dest regions overlap? */
1315 static GLboolean
1316 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1317 GLsizei width, GLsizei height)
1318 {
1319 if (srcX + width <= dstX ||
1320 dstX + width <= srcX ||
1321 srcY + height <= dstY ||
1322 dstY + height <= srcY)
1323 return GL_FALSE;
1324 else
1325 return GL_TRUE;
1326 }
1327
1328
1329 /**
1330 * Try to do a glCopyPixels for simple cases with a blit by calling
1331 * pipe->resource_copy_region().
1332 *
1333 * We can do this when we're copying color pixels (depth/stencil
1334 * eventually) with no pixel zoom, no pixel transfer ops, no
1335 * per-fragment ops, the src/dest regions don't overlap and the
1336 * src/dest pixel formats are the same.
1337 */
1338 static GLboolean
1339 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1340 GLsizei width, GLsizei height,
1341 GLint dstx, GLint dsty, GLenum type)
1342 {
1343 struct st_context *st = st_context(ctx);
1344 struct pipe_context *pipe = st->pipe;
1345 struct gl_pixelstore_attrib pack, unpack;
1346 GLint readX, readY, readW, readH;
1347
1348 if (type == GL_COLOR &&
1349 ctx->Pixel.ZoomX == 1.0 &&
1350 ctx->Pixel.ZoomY == 1.0 &&
1351 ctx->_ImageTransferState == 0x0 &&
1352 !ctx->Color.BlendEnabled &&
1353 !ctx->Color.AlphaEnabled &&
1354 !ctx->Depth.Test &&
1355 !ctx->Fog.Enabled &&
1356 !ctx->Stencil.Enabled &&
1357 !ctx->FragmentProgram.Enabled &&
1358 !ctx->VertexProgram.Enabled &&
1359 !ctx->Shader.CurrentFragmentProgram &&
1360 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1361 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1362 !ctx->Query.CondRenderQuery) {
1363 struct st_renderbuffer *rbRead, *rbDraw;
1364 GLint drawX, drawY;
1365
1366 /*
1367 * Clip the read region against the src buffer bounds.
1368 * We'll still allocate a temporary buffer/texture for the original
1369 * src region size but we'll only read the region which is on-screen.
1370 * This may mean that we draw garbage pixels into the dest region, but
1371 * that's expected.
1372 */
1373 readX = srcx;
1374 readY = srcy;
1375 readW = width;
1376 readH = height;
1377 pack = ctx->DefaultPacking;
1378 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1379 return GL_TRUE; /* all done */
1380
1381 /* clip against dest buffer bounds and scissor box */
1382 drawX = dstx + pack.SkipPixels;
1383 drawY = dsty + pack.SkipRows;
1384 unpack = pack;
1385 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1386 return GL_TRUE; /* all done */
1387
1388 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1389 readY = readY - pack.SkipRows + unpack.SkipRows;
1390
1391 rbRead = st_get_color_read_renderbuffer(ctx);
1392 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1393
1394 if ((rbRead != rbDraw ||
1395 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1396 rbRead->Base.Format == rbDraw->Base.Format) {
1397 struct pipe_box srcBox;
1398
1399 /* flip src/dst position if needed */
1400 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1401 /* both buffers will have the same orientation */
1402 readY = ctx->ReadBuffer->Height - readY - readH;
1403 drawY = ctx->DrawBuffer->Height - drawY - readH;
1404 }
1405
1406 u_box_2d(readX, readY, readW, readH, &srcBox);
1407
1408 pipe->resource_copy_region(pipe,
1409 rbDraw->texture,
1410 rbDraw->rtt_level, drawX, drawY, 0,
1411 rbRead->texture,
1412 rbRead->rtt_level, &srcBox);
1413 return GL_TRUE;
1414 }
1415 }
1416
1417 return GL_FALSE;
1418 }
1419
1420
1421 static void
1422 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1423 GLsizei width, GLsizei height,
1424 GLint dstx, GLint dsty, GLenum type)
1425 {
1426 struct st_context *st = st_context(ctx);
1427 struct pipe_context *pipe = st->pipe;
1428 struct pipe_screen *screen = pipe->screen;
1429 struct st_renderbuffer *rbRead;
1430 void *driver_vp, *driver_fp;
1431 struct pipe_resource *pt;
1432 struct pipe_sampler_view *sv[2];
1433 int num_sampler_view = 1;
1434 GLfloat *color;
1435 enum pipe_format srcFormat, texFormat;
1436 GLboolean invertTex = GL_FALSE;
1437 GLint readX, readY, readW, readH;
1438 GLuint sample_count;
1439 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1440 struct st_fp_variant *fpv;
1441
1442 st_validate_state(st);
1443
1444 if (type == GL_DEPTH_STENCIL) {
1445 /* XXX make this more efficient */
1446 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1447 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1448 return;
1449 }
1450
1451 if (type == GL_STENCIL) {
1452 /* can't use texturing to do stencil */
1453 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1454 return;
1455 }
1456
1457 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1458 return;
1459
1460 /*
1461 * The subsequent code implements glCopyPixels by copying the source
1462 * pixels into a temporary texture that's then applied to a textured quad.
1463 * When we draw the textured quad, all the usual per-fragment operations
1464 * are handled.
1465 */
1466
1467
1468 /*
1469 * Get vertex/fragment shaders
1470 */
1471 if (type == GL_COLOR) {
1472 rbRead = st_get_color_read_renderbuffer(ctx);
1473 color = NULL;
1474
1475 fpv = get_color_fp_variant(st);
1476 driver_fp = fpv->driver_shader;
1477
1478 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1479
1480 if (st->pixel_xfer.pixelmap_enabled) {
1481 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1482 num_sampler_view++;
1483 }
1484 }
1485 else {
1486 assert(type == GL_DEPTH);
1487 rbRead = st_renderbuffer(ctx->ReadBuffer->
1488 Attachment[BUFFER_DEPTH].Renderbuffer);
1489 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1490
1491 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1492 driver_fp = fpv->driver_shader;
1493
1494 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1495 }
1496
1497 /* update fragment program constants */
1498 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1499
1500 sample_count = rbRead->texture->nr_samples;
1501 /* I believe this would be legal, presumably would need to do a resolve
1502 for color, and for depth/stencil spec says to just use one of the
1503 depth/stencil samples per pixel? Need some transfer clarifications. */
1504 assert(sample_count < 2);
1505
1506 srcFormat = rbRead->texture->format;
1507
1508 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1509 sample_count,
1510 PIPE_BIND_SAMPLER_VIEW)) {
1511 texFormat = srcFormat;
1512 }
1513 else {
1514 /* srcFormat can't be used as a texture format */
1515 if (type == GL_DEPTH) {
1516 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1517 GL_NONE, GL_NONE, st->internal_target,
1518 sample_count, PIPE_BIND_DEPTH_STENCIL);
1519 assert(texFormat != PIPE_FORMAT_NONE);
1520 }
1521 else {
1522 /* default color format */
1523 texFormat = st_choose_format(screen, GL_RGBA,
1524 GL_NONE, GL_NONE, st->internal_target,
1525 sample_count, PIPE_BIND_SAMPLER_VIEW);
1526 assert(texFormat != PIPE_FORMAT_NONE);
1527 }
1528 }
1529
1530 /* Invert src region if needed */
1531 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1532 srcy = ctx->ReadBuffer->Height - srcy - height;
1533 invertTex = !invertTex;
1534 }
1535
1536 /* Clip the read region against the src buffer bounds.
1537 * We'll still allocate a temporary buffer/texture for the original
1538 * src region size but we'll only read the region which is on-screen.
1539 * This may mean that we draw garbage pixels into the dest region, but
1540 * that's expected.
1541 */
1542 readX = srcx;
1543 readY = srcy;
1544 readW = width;
1545 readH = height;
1546 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1547 readW = MAX2(0, readW);
1548 readH = MAX2(0, readH);
1549
1550 /* alloc temporary texture */
1551 pt = alloc_texture(st, width, height, texFormat);
1552 if (!pt)
1553 return;
1554
1555 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1556 if (!sv[0]) {
1557 pipe_resource_reference(&pt, NULL);
1558 return;
1559 }
1560
1561 /* Make temporary texture which is a copy of the src region.
1562 */
1563 if (srcFormat == texFormat) {
1564 struct pipe_box src_box;
1565 u_box_2d(readX, readY, readW, readH, &src_box);
1566 /* copy source framebuffer surface into mipmap/texture */
1567 pipe->resource_copy_region(pipe,
1568 pt, /* dest tex */
1569 0, /* dest lvl */
1570 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1571 rbRead->texture, /* src tex */
1572 rbRead->rtt_level, /* src lvl */
1573 &src_box);
1574
1575 }
1576 else {
1577 /* CPU-based fallback/conversion */
1578 struct pipe_transfer *ptRead =
1579 pipe_get_transfer(st->pipe, rbRead->texture,
1580 rbRead->rtt_level,
1581 rbRead->rtt_face + rbRead->rtt_slice,
1582 PIPE_TRANSFER_READ,
1583 readX, readY, readW, readH);
1584 struct pipe_transfer *ptTex;
1585 enum pipe_transfer_usage transfer_usage;
1586
1587 if (ST_DEBUG & DEBUG_FALLBACK)
1588 debug_printf("%s: fallback processing\n", __FUNCTION__);
1589
1590 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1591 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1592 else
1593 transfer_usage = PIPE_TRANSFER_WRITE;
1594
1595 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1596 0, 0, width, height);
1597
1598 /* copy image from ptRead surface to ptTex surface */
1599 if (type == GL_COLOR) {
1600 /* alternate path using get/put_tile() */
1601 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1602 enum pipe_format readFormat, drawFormat;
1603 readFormat = util_format_linear(rbRead->texture->format);
1604 drawFormat = util_format_linear(pt->format);
1605 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1606 readFormat, buf);
1607 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1608 readW, readH, drawFormat, buf);
1609 free(buf);
1610 }
1611 else {
1612 /* GL_DEPTH */
1613 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1614 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1615 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1616 readW, readH, buf);
1617 free(buf);
1618 }
1619
1620 pipe->transfer_destroy(pipe, ptRead);
1621 pipe->transfer_destroy(pipe, ptTex);
1622 }
1623
1624 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1625 * textured quad with that texture.
1626 */
1627 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1628 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1629 sv,
1630 num_sampler_view,
1631 driver_vp,
1632 driver_fp,
1633 color, invertTex, GL_FALSE, GL_FALSE);
1634
1635 pipe_resource_reference(&pt, NULL);
1636 pipe_sampler_view_reference(&sv[0], NULL);
1637 }
1638
1639
1640
1641 void st_init_drawpixels_functions(struct dd_function_table *functions)
1642 {
1643 functions->DrawPixels = st_DrawPixels;
1644 functions->CopyPixels = st_CopyPixels;
1645 }
1646
1647
1648 void
1649 st_destroy_drawpix(struct st_context *st)
1650 {
1651 GLuint i;
1652
1653 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1654 if (st->drawpix.shaders[i])
1655 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1656 }
1657
1658 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1659 if (st->drawpix.vert_shaders[0])
1660 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1661 if (st->drawpix.vert_shaders[1])
1662 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1663 }
1664
1665 #endif /* FEATURE_drawpix */