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