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