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