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