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