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