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