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