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