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