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