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