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