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