e377861b8d2c06d9819662729d5d832ef7f255da
[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 case GL_UNSIGNED_INT_5_9_9_9_REV:
377 assert(ctx->Extensions.EXT_texture_shared_exponent);
378 return GL_RGB9_E5;
379
380 case GL_UNSIGNED_INT_10F_11F_11F_REV:
381 assert(ctx->Extensions.EXT_packed_float);
382 return GL_R11F_G11F_B10F;
383 }
384 }
385 }
386 }
387
388
389 /**
390 * Create a temporary texture to hold an image of the given size.
391 * If width, height are not POT and the driver only handles POT textures,
392 * allocate the next larger size of texture that is POT.
393 */
394 static struct pipe_resource *
395 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
396 enum pipe_format texFormat)
397 {
398 struct pipe_resource *pt;
399
400 pt = st_texture_create(st, st->internal_target, texFormat, 0,
401 width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
402
403 return pt;
404 }
405
406
407 /**
408 * Make texture containing an image for glDrawPixels image.
409 * If 'pixels' is NULL, leave the texture image data undefined.
410 */
411 static struct pipe_resource *
412 make_texture(struct st_context *st,
413 GLsizei width, GLsizei height, GLenum format, GLenum type,
414 const struct gl_pixelstore_attrib *unpack,
415 const GLvoid *pixels)
416 {
417 struct gl_context *ctx = st->ctx;
418 struct pipe_context *pipe = st->pipe;
419 gl_format mformat;
420 struct pipe_resource *pt;
421 enum pipe_format pipeFormat;
422 GLenum baseFormat, intFormat;
423
424 baseFormat = base_format(format);
425 intFormat = internal_format(ctx, format, type);
426
427 mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
428 format, type, GL_FALSE);
429 assert(mformat);
430
431 pipeFormat = st_mesa_format_to_pipe_format(mformat);
432 assert(pipeFormat);
433
434 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
435 if (!pixels)
436 return NULL;
437
438 /* alloc temporary texture */
439 pt = alloc_texture(st, width, height, pipeFormat);
440 if (!pt) {
441 _mesa_unmap_pbo_source(ctx, unpack);
442 return NULL;
443 }
444
445 {
446 struct pipe_transfer *transfer;
447 static const GLuint dstImageOffsets = 0;
448 GLboolean success;
449 GLubyte *dest;
450 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
451
452 /* we'll do pixel transfer in a fragment shader */
453 ctx->_ImageTransferState = 0x0;
454
455 transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
456 PIPE_TRANSFER_WRITE, 0, 0,
457 width, height);
458
459 /* map texture transfer */
460 dest = pipe_transfer_map(pipe, transfer);
461
462
463 /* Put image into texture transfer.
464 * Note that the image is actually going to be upside down in
465 * the texture. We deal with that with texcoords.
466 */
467 success = _mesa_texstore(ctx, 2, /* dims */
468 baseFormat, /* baseInternalFormat */
469 mformat, /* gl_format */
470 dest, /* dest */
471 0, 0, 0, /* dstX/Y/Zoffset */
472 transfer->stride, /* dstRowStride, bytes */
473 &dstImageOffsets, /* dstImageOffsets */
474 width, height, 1, /* size */
475 format, type, /* src format/type */
476 pixels, /* data source */
477 unpack);
478
479 /* unmap */
480 pipe_transfer_unmap(pipe, transfer);
481 pipe->transfer_destroy(pipe, transfer);
482
483 assert(success);
484
485 /* restore */
486 ctx->_ImageTransferState = imageTransferStateSave;
487 }
488
489 _mesa_unmap_pbo_source(ctx, unpack);
490
491 return pt;
492 }
493
494
495 /**
496 * Draw quad with texcoords and optional color.
497 * Coords are gallium window coords with y=0=top.
498 * \param color may be null
499 * \param invertTex if true, flip texcoords vertically
500 */
501 static void
502 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
503 GLfloat x1, GLfloat y1, const GLfloat *color,
504 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
505 {
506 struct st_context *st = st_context(ctx);
507 struct pipe_context *pipe = st->pipe;
508 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
509
510 /* setup vertex data */
511 {
512 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
513 const GLfloat fb_width = (GLfloat) fb->Width;
514 const GLfloat fb_height = (GLfloat) fb->Height;
515 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
516 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
517 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
518 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
519 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
520 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
521 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
522 GLuint i;
523
524 /* upper-left */
525 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
526 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
527
528 /* upper-right */
529 verts[1][0][0] = clip_x1;
530 verts[1][0][1] = clip_y0;
531
532 /* lower-right */
533 verts[2][0][0] = clip_x1;
534 verts[2][0][1] = clip_y1;
535
536 /* lower-left */
537 verts[3][0][0] = clip_x0;
538 verts[3][0][1] = clip_y1;
539
540 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
541 verts[0][1][1] = tTop; /* v[0].attr[1].T */
542 verts[1][1][0] = sRight;
543 verts[1][1][1] = tTop;
544 verts[2][1][0] = sRight;
545 verts[2][1][1] = tBot;
546 verts[3][1][0] = sLeft;
547 verts[3][1][1] = tBot;
548
549 /* same for all verts: */
550 if (color) {
551 for (i = 0; i < 4; i++) {
552 verts[i][0][2] = z; /* v[i].attr[0].z */
553 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
554 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
555 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
556 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
557 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
558 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
559 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
560 }
561 }
562 else {
563 for (i = 0; i < 4; i++) {
564 verts[i][0][2] = z; /*Z*/
565 verts[i][0][3] = 1.0f; /*W*/
566 verts[i][1][2] = 0.0f; /*R*/
567 verts[i][1][3] = 1.0f; /*Q*/
568 }
569 }
570 }
571
572 {
573 struct pipe_resource *buf;
574
575 /* allocate/load buffer object with vertex data */
576 buf = pipe_buffer_create(pipe->screen,
577 PIPE_BIND_VERTEX_BUFFER,
578 PIPE_USAGE_STATIC,
579 sizeof(verts));
580 pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
581
582 util_draw_vertex_buffer(pipe, st->cso_context, buf, 0,
583 PIPE_PRIM_QUADS,
584 4, /* verts */
585 3); /* attribs/vert */
586 pipe_resource_reference(&buf, NULL);
587 }
588 }
589
590
591
592 static void
593 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
594 GLsizei width, GLsizei height,
595 GLfloat zoomX, GLfloat zoomY,
596 struct pipe_sampler_view **sv,
597 int num_sampler_view,
598 void *driver_vp,
599 void *driver_fp,
600 const GLfloat *color,
601 GLboolean invertTex,
602 GLboolean write_depth, GLboolean write_stencil)
603 {
604 struct st_context *st = st_context(ctx);
605 struct pipe_context *pipe = st->pipe;
606 struct cso_context *cso = st->cso_context;
607 GLfloat x0, y0, x1, y1;
608 GLsizei maxSize;
609 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
610
611 /* limit checks */
612 /* XXX if DrawPixels image is larger than max texture size, break
613 * it up into chunks.
614 */
615 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
616 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
617 assert(width <= maxSize);
618 assert(height <= maxSize);
619
620 cso_save_rasterizer(cso);
621 cso_save_viewport(cso);
622 cso_save_samplers(cso);
623 cso_save_fragment_sampler_views(cso);
624 cso_save_fragment_shader(cso);
625 cso_save_vertex_shader(cso);
626 cso_save_vertex_elements(cso);
627 cso_save_vertex_buffers(cso);
628 if (write_stencil) {
629 cso_save_depth_stencil_alpha(cso);
630 cso_save_blend(cso);
631 }
632
633 /* rasterizer state: just scissor */
634 {
635 struct pipe_rasterizer_state rasterizer;
636 memset(&rasterizer, 0, sizeof(rasterizer));
637 rasterizer.clamp_fragment_color = ctx->Color._ClampFragmentColor;
638 rasterizer.gl_rasterization_rules = 1;
639 rasterizer.scissor = ctx->Scissor.Enabled;
640 cso_set_rasterizer(cso, &rasterizer);
641 }
642
643 if (write_stencil) {
644 /* Stencil writing bypasses the normal fragment pipeline to
645 * disable color writing and set stencil test to always pass.
646 */
647 struct pipe_depth_stencil_alpha_state dsa;
648 struct pipe_blend_state blend;
649
650 /* depth/stencil */
651 memset(&dsa, 0, sizeof(dsa));
652 dsa.stencil[0].enabled = 1;
653 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
654 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
655 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
656 if (write_depth) {
657 /* writing depth+stencil: depth test always passes */
658 dsa.depth.enabled = 1;
659 dsa.depth.writemask = ctx->Depth.Mask;
660 dsa.depth.func = PIPE_FUNC_ALWAYS;
661 }
662 cso_set_depth_stencil_alpha(cso, &dsa);
663
664 /* blend (colormask) */
665 memset(&blend, 0, sizeof(blend));
666 cso_set_blend(cso, &blend);
667 }
668
669 /* fragment shader state: TEX lookup program */
670 cso_set_fragment_shader_handle(cso, driver_fp);
671
672 /* vertex shader state: position + texcoord pass-through */
673 cso_set_vertex_shader_handle(cso, driver_vp);
674
675
676 /* texture sampling state: */
677 {
678 struct pipe_sampler_state sampler;
679 memset(&sampler, 0, sizeof(sampler));
680 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
681 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
682 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
683 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
684 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
685 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
686 sampler.normalized_coords = normalized;
687
688 cso_single_sampler(cso, 0, &sampler);
689 if (num_sampler_view > 1) {
690 cso_single_sampler(cso, 1, &sampler);
691 }
692 cso_single_sampler_done(cso);
693 }
694
695 /* viewport state: viewport matching window dims */
696 {
697 const float w = (float) ctx->DrawBuffer->Width;
698 const float h = (float) ctx->DrawBuffer->Height;
699 struct pipe_viewport_state vp;
700 vp.scale[0] = 0.5f * w;
701 vp.scale[1] = -0.5f * h;
702 vp.scale[2] = 0.5f;
703 vp.scale[3] = 1.0f;
704 vp.translate[0] = 0.5f * w;
705 vp.translate[1] = 0.5f * h;
706 vp.translate[2] = 0.5f;
707 vp.translate[3] = 0.0f;
708 cso_set_viewport(cso, &vp);
709 }
710
711 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
712
713 /* texture state: */
714 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
715
716 /* Compute Gallium window coords (y=0=top) with pixel zoom.
717 * Recall that these coords are transformed by the current
718 * vertex shader and viewport transformation.
719 */
720 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
721 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
722 invertTex = !invertTex;
723 }
724
725 x0 = (GLfloat) x;
726 x1 = x + width * ctx->Pixel.ZoomX;
727 y0 = (GLfloat) y;
728 y1 = y + height * ctx->Pixel.ZoomY;
729
730 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
731 z = z * 2.0 - 1.0;
732
733 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
734 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
735 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
736
737 /* restore state */
738 cso_restore_rasterizer(cso);
739 cso_restore_viewport(cso);
740 cso_restore_samplers(cso);
741 cso_restore_fragment_sampler_views(cso);
742 cso_restore_fragment_shader(cso);
743 cso_restore_vertex_shader(cso);
744 cso_restore_vertex_elements(cso);
745 cso_restore_vertex_buffers(cso);
746 if (write_stencil) {
747 cso_restore_depth_stencil_alpha(cso);
748 cso_restore_blend(cso);
749 }
750 }
751
752
753 /**
754 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
755 * can't use a fragment shader to write stencil values.
756 */
757 static void
758 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
759 GLsizei width, GLsizei height, GLenum format, GLenum type,
760 const struct gl_pixelstore_attrib *unpack,
761 const GLvoid *pixels)
762 {
763 struct st_context *st = st_context(ctx);
764 struct pipe_context *pipe = st->pipe;
765 struct st_renderbuffer *strb;
766 enum pipe_transfer_usage usage;
767 struct pipe_transfer *pt;
768 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
769 GLint skipPixels;
770 ubyte *stmap;
771 struct gl_pixelstore_attrib clippedUnpack = *unpack;
772
773 if (!zoom) {
774 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
775 &clippedUnpack)) {
776 /* totally clipped */
777 return;
778 }
779 }
780
781 strb = st_renderbuffer(ctx->DrawBuffer->
782 Attachment[BUFFER_STENCIL].Renderbuffer);
783
784 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
785 y = ctx->DrawBuffer->Height - y - height;
786 }
787
788 if(format != GL_DEPTH_STENCIL &&
789 util_format_get_component_bits(strb->format,
790 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
791 usage = PIPE_TRANSFER_READ_WRITE;
792 else
793 usage = PIPE_TRANSFER_WRITE;
794
795 pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0,
796 usage, x, y,
797 width, height);
798
799 stmap = pipe_transfer_map(pipe, pt);
800
801 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
802 assert(pixels);
803
804 /* if width > MAX_WIDTH, have to process image in chunks */
805 skipPixels = 0;
806 while (skipPixels < width) {
807 const GLint spanX = skipPixels;
808 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
809 GLint row;
810 for (row = 0; row < height; row++) {
811 GLubyte sValues[MAX_WIDTH];
812 GLuint zValues[MAX_WIDTH];
813 GLenum destType = GL_UNSIGNED_BYTE;
814 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
815 width, height,
816 format, type,
817 row, skipPixels);
818 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
819 type, source, &clippedUnpack,
820 ctx->_ImageTransferState);
821
822 if (format == GL_DEPTH_STENCIL) {
823 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
824 (1 << 24) - 1, type, source,
825 &clippedUnpack);
826 }
827
828 if (zoom) {
829 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
830 "zoom not complete");
831 }
832
833 {
834 GLint spanY;
835
836 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
837 spanY = height - row - 1;
838 }
839 else {
840 spanY = row;
841 }
842
843 /* now pack the stencil (and Z) values in the dest format */
844 switch (pt->resource->format) {
845 case PIPE_FORMAT_S8_USCALED:
846 {
847 ubyte *dest = stmap + spanY * pt->stride + spanX;
848 assert(usage == PIPE_TRANSFER_WRITE);
849 memcpy(dest, sValues, spanWidth);
850 }
851 break;
852 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
853 if (format == GL_DEPTH_STENCIL) {
854 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
855 GLint k;
856 assert(usage == PIPE_TRANSFER_WRITE);
857 for (k = 0; k < spanWidth; k++) {
858 dest[k] = zValues[k] | (sValues[k] << 24);
859 }
860 }
861 else {
862 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
863 GLint k;
864 assert(usage == PIPE_TRANSFER_READ_WRITE);
865 for (k = 0; k < spanWidth; k++) {
866 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
867 }
868 }
869 break;
870 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
871 if (format == GL_DEPTH_STENCIL) {
872 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
873 GLint k;
874 assert(usage == PIPE_TRANSFER_WRITE);
875 for (k = 0; k < spanWidth; k++) {
876 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
877 }
878 }
879 else {
880 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
881 GLint k;
882 assert(usage == PIPE_TRANSFER_READ_WRITE);
883 for (k = 0; k < spanWidth; k++) {
884 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
885 }
886 }
887 break;
888 default:
889 assert(0);
890 }
891 }
892 }
893 skipPixels += spanWidth;
894 }
895
896 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
897
898 /* unmap the stencil buffer */
899 pipe_transfer_unmap(pipe, pt);
900 pipe->transfer_destroy(pipe, pt);
901 }
902
903
904 /**
905 * Get fragment program variant for a glDrawPixels or glCopyPixels
906 * command for RGBA data.
907 */
908 static struct st_fp_variant *
909 get_color_fp_variant(struct st_context *st)
910 {
911 struct gl_context *ctx = st->ctx;
912 struct st_fp_variant_key key;
913 struct st_fp_variant *fpv;
914
915 memset(&key, 0, sizeof(key));
916
917 key.st = st;
918 key.drawpixels = 1;
919 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
920 ctx->Pixel.RedScale != 1.0 ||
921 ctx->Pixel.GreenBias != 0.0 ||
922 ctx->Pixel.GreenScale != 1.0 ||
923 ctx->Pixel.BlueBias != 0.0 ||
924 ctx->Pixel.BlueScale != 1.0 ||
925 ctx->Pixel.AlphaBias != 0.0 ||
926 ctx->Pixel.AlphaScale != 1.0);
927 key.pixelMaps = ctx->Pixel.MapColorFlag;
928
929 fpv = st_get_fp_variant(st, st->fp, &key);
930
931 return fpv;
932 }
933
934
935 /**
936 * Get fragment program variant for a glDrawPixels or glCopyPixels
937 * command for depth/stencil data.
938 */
939 static struct st_fp_variant *
940 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
941 GLboolean write_stencil)
942 {
943 struct st_fp_variant_key key;
944 struct st_fp_variant *fpv;
945
946 memset(&key, 0, sizeof(key));
947
948 key.st = st;
949 key.drawpixels = 1;
950 key.drawpixels_z = write_depth;
951 key.drawpixels_stencil = write_stencil;
952
953 fpv = st_get_fp_variant(st, st->fp, &key);
954
955 return fpv;
956 }
957
958
959 /**
960 * Called via ctx->Driver.DrawPixels()
961 */
962 static void
963 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
964 GLsizei width, GLsizei height,
965 GLenum format, GLenum type,
966 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
967 {
968 void *driver_vp, *driver_fp;
969 struct st_context *st = st_context(ctx);
970 const GLfloat *color;
971 struct pipe_context *pipe = st->pipe;
972 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
973 struct pipe_sampler_view *sv[2];
974 int num_sampler_view = 1;
975 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
976 struct st_fp_variant *fpv;
977
978 if (format == GL_DEPTH_STENCIL)
979 write_stencil = write_depth = GL_TRUE;
980 else if (format == GL_STENCIL_INDEX)
981 write_stencil = GL_TRUE;
982 else if (format == GL_DEPTH_COMPONENT)
983 write_depth = GL_TRUE;
984
985 if (write_stencil) {
986 enum pipe_format tex_format;
987 /* can we write to stencil if not fallback */
988 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT))
989 goto stencil_fallback;
990
991 tex_format = st_choose_format(st->pipe->screen, base_format(format),
992 PIPE_TEXTURE_2D,
993 0, PIPE_BIND_SAMPLER_VIEW);
994 if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
995 stencil_format = PIPE_FORMAT_X24S8_USCALED;
996 else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM)
997 stencil_format = PIPE_FORMAT_S8X24_USCALED;
998 else
999 stencil_format = PIPE_FORMAT_S8_USCALED;
1000 if (stencil_format == PIPE_FORMAT_NONE)
1001 goto stencil_fallback;
1002 }
1003
1004 /* Mesa state should be up to date by now */
1005 assert(ctx->NewState == 0x0);
1006
1007 st_validate_state(st);
1008
1009 /*
1010 * Get vertex/fragment shaders
1011 */
1012 if (write_depth || write_stencil) {
1013 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
1014
1015 driver_fp = fpv->driver_shader;
1016
1017 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1018
1019 color = ctx->Current.RasterColor;
1020 }
1021 else {
1022 fpv = get_color_fp_variant(st);
1023
1024 driver_fp = fpv->driver_shader;
1025
1026 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1027
1028 color = NULL;
1029 if (st->pixel_xfer.pixelmap_enabled) {
1030 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1031 num_sampler_view++;
1032 }
1033 }
1034
1035 /* update fragment program constants */
1036 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1037
1038 /* draw with textured quad */
1039 {
1040 struct pipe_resource *pt
1041 = make_texture(st, width, height, format, type, unpack, pixels);
1042 if (pt) {
1043 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1044
1045 if (sv[0]) {
1046 if (write_stencil) {
1047 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1048 stencil_format);
1049 num_sampler_view++;
1050 }
1051
1052 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1053 width, height,
1054 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1055 sv,
1056 num_sampler_view,
1057 driver_vp,
1058 driver_fp,
1059 color, GL_FALSE, write_depth, write_stencil);
1060 pipe_sampler_view_reference(&sv[0], NULL);
1061 if (num_sampler_view > 1)
1062 pipe_sampler_view_reference(&sv[1], NULL);
1063 }
1064 pipe_resource_reference(&pt, NULL);
1065 }
1066 }
1067 return;
1068
1069 stencil_fallback:
1070 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1071 unpack, pixels);
1072 }
1073
1074
1075
1076 /**
1077 * Software fallback for glCopyPixels(GL_STENCIL).
1078 */
1079 static void
1080 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1081 GLsizei width, GLsizei height,
1082 GLint dstx, GLint dsty)
1083 {
1084 struct st_renderbuffer *rbDraw;
1085 struct pipe_context *pipe = st_context(ctx)->pipe;
1086 enum pipe_transfer_usage usage;
1087 struct pipe_transfer *ptDraw;
1088 ubyte *drawMap;
1089 ubyte *buffer;
1090 int i;
1091
1092 buffer = malloc(width * height * sizeof(ubyte));
1093 if (!buffer) {
1094 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1095 return;
1096 }
1097
1098 /* Get the dest renderbuffer. If there's a wrapper, use the
1099 * underlying renderbuffer.
1100 */
1101 rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
1102 if (rbDraw->Base.Wrapped)
1103 rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
1104
1105 /* this will do stencil pixel transfer ops */
1106 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
1107 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1108 &ctx->DefaultPacking, buffer);
1109
1110 if (0) {
1111 /* debug code: dump stencil values */
1112 GLint row, col;
1113 for (row = 0; row < height; row++) {
1114 printf("%3d: ", row);
1115 for (col = 0; col < width; col++) {
1116 printf("%02x ", buffer[col + row * width]);
1117 }
1118 printf("\n");
1119 }
1120 }
1121
1122 if (util_format_get_component_bits(rbDraw->format,
1123 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
1124 usage = PIPE_TRANSFER_READ_WRITE;
1125 else
1126 usage = PIPE_TRANSFER_WRITE;
1127
1128 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1129 dsty = rbDraw->Base.Height - dsty - height;
1130 }
1131
1132 ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
1133 rbDraw->texture, 0, 0,
1134 usage, dstx, dsty,
1135 width, height);
1136
1137 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1138 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1139
1140 /* map the stencil buffer */
1141 drawMap = pipe_transfer_map(pipe, ptDraw);
1142
1143 /* draw */
1144 /* XXX PixelZoom not handled yet */
1145 for (i = 0; i < height; i++) {
1146 ubyte *dst;
1147 const ubyte *src;
1148 int y;
1149
1150 y = i;
1151
1152 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1153 y = height - y - 1;
1154 }
1155
1156 dst = drawMap + y * ptDraw->stride;
1157 src = buffer + i * width;
1158
1159 switch (ptDraw->resource->format) {
1160 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
1161 {
1162 uint *dst4 = (uint *) dst;
1163 int j;
1164 assert(usage == PIPE_TRANSFER_READ_WRITE);
1165 for (j = 0; j < width; j++) {
1166 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
1167 dst4++;
1168 }
1169 }
1170 break;
1171 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
1172 {
1173 uint *dst4 = (uint *) dst;
1174 int j;
1175 assert(usage == PIPE_TRANSFER_READ_WRITE);
1176 for (j = 0; j < width; j++) {
1177 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
1178 dst4++;
1179 }
1180 }
1181 break;
1182 case PIPE_FORMAT_S8_USCALED:
1183 assert(usage == PIPE_TRANSFER_WRITE);
1184 memcpy(dst, src, width);
1185 break;
1186 default:
1187 assert(0);
1188 }
1189 }
1190
1191 free(buffer);
1192
1193 /* unmap the stencil buffer */
1194 pipe_transfer_unmap(pipe, ptDraw);
1195 pipe->transfer_destroy(pipe, ptDraw);
1196 }
1197
1198
1199 /** Do the src/dest regions overlap? */
1200 static GLboolean
1201 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1202 GLsizei width, GLsizei height)
1203 {
1204 if (srcX + width <= dstX ||
1205 dstX + width <= srcX ||
1206 srcY + height <= dstY ||
1207 dstY + height <= srcY)
1208 return GL_FALSE;
1209 else
1210 return GL_TRUE;
1211 }
1212
1213
1214 /**
1215 * Try to do a glCopyPixels for simple cases with a blit by calling
1216 * pipe->resource_copy_region().
1217 *
1218 * We can do this when we're copying color pixels (depth/stencil
1219 * eventually) with no pixel zoom, no pixel transfer ops, no
1220 * per-fragment ops, the src/dest regions don't overlap and the
1221 * src/dest pixel formats are the same.
1222 */
1223 static GLboolean
1224 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1225 GLsizei width, GLsizei height,
1226 GLint dstx, GLint dsty, GLenum type)
1227 {
1228 struct st_context *st = st_context(ctx);
1229 struct pipe_context *pipe = st->pipe;
1230 struct gl_pixelstore_attrib pack, unpack;
1231 GLint readX, readY, readW, readH;
1232
1233 if (type == GL_COLOR &&
1234 ctx->Pixel.ZoomX == 1.0 &&
1235 ctx->Pixel.ZoomY == 1.0 &&
1236 ctx->_ImageTransferState == 0x0 &&
1237 !ctx->Color.BlendEnabled &&
1238 !ctx->Color.AlphaEnabled &&
1239 !ctx->Depth.Test &&
1240 !ctx->Fog.Enabled &&
1241 !ctx->Stencil.Enabled &&
1242 !ctx->FragmentProgram.Enabled &&
1243 !ctx->VertexProgram.Enabled &&
1244 !ctx->Shader.CurrentFragmentProgram &&
1245 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1246 ctx->DrawBuffer->_NumColorDrawBuffers == 1) {
1247 struct st_renderbuffer *rbRead, *rbDraw;
1248 GLint drawX, drawY;
1249
1250 /*
1251 * Clip the read region against the src buffer bounds.
1252 * We'll still allocate a temporary buffer/texture for the original
1253 * src region size but we'll only read the region which is on-screen.
1254 * This may mean that we draw garbage pixels into the dest region, but
1255 * that's expected.
1256 */
1257 readX = srcx;
1258 readY = srcy;
1259 readW = width;
1260 readH = height;
1261 pack = ctx->DefaultPacking;
1262 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1263 return GL_TRUE; /* all done */
1264
1265 /* clip against dest buffer bounds and scissor box */
1266 drawX = dstx + pack.SkipPixels;
1267 drawY = dsty + pack.SkipRows;
1268 unpack = pack;
1269 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1270 return GL_TRUE; /* all done */
1271
1272 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1273 readY = readY - pack.SkipRows + unpack.SkipRows;
1274
1275 rbRead = st_get_color_read_renderbuffer(ctx);
1276 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1277
1278 if ((rbRead != rbDraw ||
1279 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1280 rbRead->Base.Format == rbDraw->Base.Format) {
1281 struct pipe_box srcBox;
1282
1283 /* flip src/dst position if needed */
1284 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1285 /* both buffers will have the same orientation */
1286 readY = ctx->ReadBuffer->Height - readY - readH;
1287 drawY = ctx->DrawBuffer->Height - drawY - readH;
1288 }
1289
1290 u_box_2d(readX, readY, readW, readH, &srcBox);
1291
1292 pipe->resource_copy_region(pipe,
1293 rbDraw->texture, 0, drawX, drawY, 0,
1294 rbRead->texture, 0, &srcBox);
1295 return GL_TRUE;
1296 }
1297 }
1298
1299 return GL_FALSE;
1300 }
1301
1302
1303 static void
1304 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1305 GLsizei width, GLsizei height,
1306 GLint dstx, GLint dsty, GLenum type)
1307 {
1308 struct st_context *st = st_context(ctx);
1309 struct pipe_context *pipe = st->pipe;
1310 struct pipe_screen *screen = pipe->screen;
1311 struct st_renderbuffer *rbRead;
1312 void *driver_vp, *driver_fp;
1313 struct pipe_resource *pt;
1314 struct pipe_sampler_view *sv[2];
1315 int num_sampler_view = 1;
1316 GLfloat *color;
1317 enum pipe_format srcFormat, texFormat;
1318 GLboolean invertTex = GL_FALSE;
1319 GLint readX, readY, readW, readH;
1320 GLuint sample_count;
1321 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1322 struct st_fp_variant *fpv;
1323
1324 st_validate_state(st);
1325
1326 if (type == GL_STENCIL) {
1327 /* can't use texturing to do stencil */
1328 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1329 return;
1330 }
1331
1332 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1333 return;
1334
1335 /*
1336 * The subsequent code implements glCopyPixels by copying the source
1337 * pixels into a temporary texture that's then applied to a textured quad.
1338 * When we draw the textured quad, all the usual per-fragment operations
1339 * are handled.
1340 */
1341
1342
1343 /*
1344 * Get vertex/fragment shaders
1345 */
1346 if (type == GL_COLOR) {
1347 rbRead = st_get_color_read_renderbuffer(ctx);
1348 color = NULL;
1349
1350 fpv = get_color_fp_variant(st);
1351 driver_fp = fpv->driver_shader;
1352
1353 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1354
1355 if (st->pixel_xfer.pixelmap_enabled) {
1356 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1357 num_sampler_view++;
1358 }
1359 }
1360 else {
1361 assert(type == GL_DEPTH);
1362 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
1363 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1364
1365 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1366 driver_fp = fpv->driver_shader;
1367
1368 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1369 }
1370
1371 /* update fragment program constants */
1372 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1373
1374
1375 if (rbRead->Base.Wrapped)
1376 rbRead = st_renderbuffer(rbRead->Base.Wrapped);
1377
1378 sample_count = rbRead->texture->nr_samples;
1379 /* I believe this would be legal, presumably would need to do a resolve
1380 for color, and for depth/stencil spec says to just use one of the
1381 depth/stencil samples per pixel? Need some transfer clarifications. */
1382 assert(sample_count < 2);
1383
1384 srcFormat = rbRead->texture->format;
1385
1386 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1387 sample_count,
1388 PIPE_BIND_SAMPLER_VIEW)) {
1389 texFormat = srcFormat;
1390 }
1391 else {
1392 /* srcFormat can't be used as a texture format */
1393 if (type == GL_DEPTH) {
1394 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1395 st->internal_target, sample_count,
1396 PIPE_BIND_DEPTH_STENCIL);
1397 assert(texFormat != PIPE_FORMAT_NONE);
1398 }
1399 else {
1400 /* default color format */
1401 texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
1402 sample_count, PIPE_BIND_SAMPLER_VIEW);
1403 assert(texFormat != PIPE_FORMAT_NONE);
1404 }
1405 }
1406
1407 /* Invert src region if needed */
1408 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1409 srcy = ctx->ReadBuffer->Height - srcy - height;
1410 invertTex = !invertTex;
1411 }
1412
1413 /* Clip the read region against the src buffer bounds.
1414 * We'll still allocate a temporary buffer/texture for the original
1415 * src region size but we'll only read the region which is on-screen.
1416 * This may mean that we draw garbage pixels into the dest region, but
1417 * that's expected.
1418 */
1419 readX = srcx;
1420 readY = srcy;
1421 readW = width;
1422 readH = height;
1423 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1424 readW = MAX2(0, readW);
1425 readH = MAX2(0, readH);
1426
1427 /* alloc temporary texture */
1428 pt = alloc_texture(st, width, height, texFormat);
1429 if (!pt)
1430 return;
1431
1432 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1433 if (!sv[0]) {
1434 pipe_resource_reference(&pt, NULL);
1435 return;
1436 }
1437
1438 /* Make temporary texture which is a copy of the src region.
1439 */
1440 if (srcFormat == texFormat) {
1441 struct pipe_box src_box;
1442 u_box_2d(readX, readY, readW, readH, &src_box);
1443 /* copy source framebuffer surface into mipmap/texture */
1444 pipe->resource_copy_region(pipe,
1445 pt, /* dest tex */
1446 0,
1447 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1448 rbRead->texture, /* src tex */
1449 0,
1450 &src_box);
1451
1452 }
1453 else {
1454 /* CPU-based fallback/conversion */
1455 struct pipe_transfer *ptRead =
1456 pipe_get_transfer(st->pipe, rbRead->texture,
1457 0, 0, /* level, layer */
1458 PIPE_TRANSFER_READ,
1459 readX, readY, readW, readH);
1460 struct pipe_transfer *ptTex;
1461 enum pipe_transfer_usage transfer_usage;
1462
1463 if (ST_DEBUG & DEBUG_FALLBACK)
1464 debug_printf("%s: fallback processing\n", __FUNCTION__);
1465
1466 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1467 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1468 else
1469 transfer_usage = PIPE_TRANSFER_WRITE;
1470
1471 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1472 0, 0, width, height);
1473
1474 /* copy image from ptRead surface to ptTex surface */
1475 if (type == GL_COLOR) {
1476 /* alternate path using get/put_tile() */
1477 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1478 enum pipe_format readFormat, drawFormat;
1479 readFormat = util_format_linear(rbRead->texture->format);
1480 drawFormat = util_format_linear(pt->format);
1481 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1482 readFormat, buf);
1483 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1484 readW, readH, drawFormat, buf);
1485 free(buf);
1486 }
1487 else {
1488 /* GL_DEPTH */
1489 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1490 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1491 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1492 readW, readH, buf);
1493 free(buf);
1494 }
1495
1496 pipe->transfer_destroy(pipe, ptRead);
1497 pipe->transfer_destroy(pipe, ptTex);
1498 }
1499
1500 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1501 * textured quad with that texture.
1502 */
1503 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1504 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1505 sv,
1506 num_sampler_view,
1507 driver_vp,
1508 driver_fp,
1509 color, invertTex, GL_FALSE, GL_FALSE);
1510
1511 pipe_resource_reference(&pt, NULL);
1512 pipe_sampler_view_reference(&sv[0], NULL);
1513 }
1514
1515
1516
1517 void st_init_drawpixels_functions(struct dd_function_table *functions)
1518 {
1519 functions->DrawPixels = st_DrawPixels;
1520 functions->CopyPixels = st_CopyPixels;
1521 }
1522
1523
1524 void
1525 st_destroy_drawpix(struct st_context *st)
1526 {
1527 GLuint i;
1528
1529 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1530 if (st->drawpix.shaders[i])
1531 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1532 }
1533
1534 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1535 if (st->drawpix.vert_shaders[0])
1536 ureg_free_tokens(st->drawpix.vert_shaders[0]);
1537 if (st->drawpix.vert_shaders[1])
1538 ureg_free_tokens(st->drawpix.vert_shaders[1]);
1539 }
1540
1541 #endif /* FEATURE_drawpix */