mesa: move PBO-related functions into a new file
[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 |= (1 << FRAG_RESULT_DEPTH);
217 if (write_stencil)
218 p->OutputsWritten |= (1 << 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.gl_rasterization_rules = 1;
586 rasterizer.scissor = ctx->Scissor.Enabled;
587 cso_set_rasterizer(cso, &rasterizer);
588 }
589
590 if (write_stencil) {
591 /* Stencil writing bypasses the normal fragment pipeline to
592 * disable color writing and set stencil test to always pass.
593 */
594 struct pipe_depth_stencil_alpha_state dsa;
595 struct pipe_blend_state blend;
596
597 /* depth/stencil */
598 memset(&dsa, 0, sizeof(dsa));
599 dsa.stencil[0].enabled = 1;
600 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
601 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
602 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
603 if (write_depth) {
604 /* writing depth+stencil: depth test always passes */
605 dsa.depth.enabled = 1;
606 dsa.depth.writemask = ctx->Depth.Mask;
607 dsa.depth.func = PIPE_FUNC_ALWAYS;
608 }
609 cso_set_depth_stencil_alpha(cso, &dsa);
610
611 /* blend (colormask) */
612 memset(&blend, 0, sizeof(blend));
613 cso_set_blend(cso, &blend);
614 }
615
616 /* fragment shader state: TEX lookup program */
617 cso_set_fragment_shader_handle(cso, driver_fp);
618
619 /* vertex shader state: position + texcoord pass-through */
620 cso_set_vertex_shader_handle(cso, driver_vp);
621
622
623 /* texture sampling state: */
624 {
625 struct pipe_sampler_state sampler;
626 memset(&sampler, 0, sizeof(sampler));
627 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
628 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
629 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
630 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
631 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
632 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
633 sampler.normalized_coords = normalized;
634
635 cso_single_sampler(cso, 0, &sampler);
636 if (num_sampler_view > 1) {
637 cso_single_sampler(cso, 1, &sampler);
638 }
639 cso_single_sampler_done(cso);
640 }
641
642 /* viewport state: viewport matching window dims */
643 {
644 const float w = (float) ctx->DrawBuffer->Width;
645 const float h = (float) ctx->DrawBuffer->Height;
646 struct pipe_viewport_state vp;
647 vp.scale[0] = 0.5f * w;
648 vp.scale[1] = -0.5f * h;
649 vp.scale[2] = 0.5f;
650 vp.scale[3] = 1.0f;
651 vp.translate[0] = 0.5f * w;
652 vp.translate[1] = 0.5f * h;
653 vp.translate[2] = 0.5f;
654 vp.translate[3] = 0.0f;
655 cso_set_viewport(cso, &vp);
656 }
657
658 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
659
660 /* texture state: */
661 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
662
663 /* Compute Gallium window coords (y=0=top) with pixel zoom.
664 * Recall that these coords are transformed by the current
665 * vertex shader and viewport transformation.
666 */
667 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
668 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
669 invertTex = !invertTex;
670 }
671
672 x0 = (GLfloat) x;
673 x1 = x + width * ctx->Pixel.ZoomX;
674 y0 = (GLfloat) y;
675 y1 = y + height * ctx->Pixel.ZoomY;
676
677 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
678 z = z * 2.0 - 1.0;
679
680 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
681 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
682 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
683
684 /* restore state */
685 cso_restore_rasterizer(cso);
686 cso_restore_viewport(cso);
687 cso_restore_samplers(cso);
688 cso_restore_fragment_sampler_views(cso);
689 cso_restore_fragment_shader(cso);
690 cso_restore_vertex_shader(cso);
691 cso_restore_vertex_elements(cso);
692 cso_restore_vertex_buffers(cso);
693 if (write_stencil) {
694 cso_restore_depth_stencil_alpha(cso);
695 cso_restore_blend(cso);
696 }
697 }
698
699
700 /**
701 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
702 * can't use a fragment shader to write stencil values.
703 */
704 static void
705 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
706 GLsizei width, GLsizei height, GLenum format, GLenum type,
707 const struct gl_pixelstore_attrib *unpack,
708 const GLvoid *pixels)
709 {
710 struct st_context *st = st_context(ctx);
711 struct pipe_context *pipe = st->pipe;
712 struct st_renderbuffer *strb;
713 enum pipe_transfer_usage usage;
714 struct pipe_transfer *pt;
715 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
716 GLint skipPixels;
717 ubyte *stmap;
718 struct gl_pixelstore_attrib clippedUnpack = *unpack;
719
720 if (!zoom) {
721 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
722 &clippedUnpack)) {
723 /* totally clipped */
724 return;
725 }
726 }
727
728 strb = st_renderbuffer(ctx->DrawBuffer->
729 Attachment[BUFFER_STENCIL].Renderbuffer);
730
731 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
732 y = ctx->DrawBuffer->Height - y - height;
733 }
734
735 if(format != GL_DEPTH_STENCIL &&
736 util_format_get_component_bits(strb->format,
737 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
738 usage = PIPE_TRANSFER_READ_WRITE;
739 else
740 usage = PIPE_TRANSFER_WRITE;
741
742 pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0,
743 usage, x, y,
744 width, height);
745
746 stmap = pipe_transfer_map(pipe, pt);
747
748 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
749 assert(pixels);
750
751 /* if width > MAX_WIDTH, have to process image in chunks */
752 skipPixels = 0;
753 while (skipPixels < width) {
754 const GLint spanX = skipPixels;
755 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
756 GLint row;
757 for (row = 0; row < height; row++) {
758 GLubyte sValues[MAX_WIDTH];
759 GLuint zValues[MAX_WIDTH];
760 GLenum destType = GL_UNSIGNED_BYTE;
761 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
762 width, height,
763 format, type,
764 row, skipPixels);
765 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
766 type, source, &clippedUnpack,
767 ctx->_ImageTransferState);
768
769 if (format == GL_DEPTH_STENCIL) {
770 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
771 (1 << 24) - 1, type, source,
772 &clippedUnpack);
773 }
774
775 if (zoom) {
776 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
777 "zoom not complete");
778 }
779
780 {
781 GLint spanY;
782
783 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
784 spanY = height - row - 1;
785 }
786 else {
787 spanY = row;
788 }
789
790 /* now pack the stencil (and Z) values in the dest format */
791 switch (pt->resource->format) {
792 case PIPE_FORMAT_S8_USCALED:
793 {
794 ubyte *dest = stmap + spanY * pt->stride + spanX;
795 assert(usage == PIPE_TRANSFER_WRITE);
796 memcpy(dest, sValues, spanWidth);
797 }
798 break;
799 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
800 if (format == GL_DEPTH_STENCIL) {
801 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
802 GLint k;
803 assert(usage == PIPE_TRANSFER_WRITE);
804 for (k = 0; k < spanWidth; k++) {
805 dest[k] = zValues[k] | (sValues[k] << 24);
806 }
807 }
808 else {
809 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
810 GLint k;
811 assert(usage == PIPE_TRANSFER_READ_WRITE);
812 for (k = 0; k < spanWidth; k++) {
813 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
814 }
815 }
816 break;
817 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
818 if (format == GL_DEPTH_STENCIL) {
819 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
820 GLint k;
821 assert(usage == PIPE_TRANSFER_WRITE);
822 for (k = 0; k < spanWidth; k++) {
823 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
824 }
825 }
826 else {
827 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
828 GLint k;
829 assert(usage == PIPE_TRANSFER_READ_WRITE);
830 for (k = 0; k < spanWidth; k++) {
831 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
832 }
833 }
834 break;
835 default:
836 assert(0);
837 }
838 }
839 }
840 skipPixels += spanWidth;
841 }
842
843 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
844
845 /* unmap the stencil buffer */
846 pipe_transfer_unmap(pipe, pt);
847 pipe->transfer_destroy(pipe, pt);
848 }
849
850
851 /**
852 * Get fragment program variant for a glDrawPixels or glCopyPixels
853 * command for RGBA data.
854 */
855 static struct st_fp_variant *
856 get_color_fp_variant(struct st_context *st)
857 {
858 struct gl_context *ctx = st->ctx;
859 struct st_fp_variant_key key;
860 struct st_fp_variant *fpv;
861
862 memset(&key, 0, sizeof(key));
863
864 key.st = st;
865 key.drawpixels = 1;
866 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
867 ctx->Pixel.RedScale != 1.0 ||
868 ctx->Pixel.GreenBias != 0.0 ||
869 ctx->Pixel.GreenScale != 1.0 ||
870 ctx->Pixel.BlueBias != 0.0 ||
871 ctx->Pixel.BlueScale != 1.0 ||
872 ctx->Pixel.AlphaBias != 0.0 ||
873 ctx->Pixel.AlphaScale != 1.0);
874 key.pixelMaps = ctx->Pixel.MapColorFlag;
875
876 fpv = st_get_fp_variant(st, st->fp, &key);
877
878 return fpv;
879 }
880
881
882 /**
883 * Get fragment program variant for a glDrawPixels or glCopyPixels
884 * command for depth/stencil data.
885 */
886 static struct st_fp_variant *
887 get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
888 GLboolean write_stencil)
889 {
890 struct st_fp_variant_key key;
891 struct st_fp_variant *fpv;
892
893 memset(&key, 0, sizeof(key));
894
895 key.st = st;
896 key.drawpixels = 1;
897 key.drawpixels_z = write_depth;
898 key.drawpixels_stencil = write_stencil;
899
900 fpv = st_get_fp_variant(st, st->fp, &key);
901
902 return fpv;
903 }
904
905
906 /**
907 * Called via ctx->Driver.DrawPixels()
908 */
909 static void
910 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
911 GLsizei width, GLsizei height,
912 GLenum format, GLenum type,
913 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
914 {
915 void *driver_vp, *driver_fp;
916 struct st_context *st = st_context(ctx);
917 const GLfloat *color;
918 struct pipe_context *pipe = st->pipe;
919 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
920 struct pipe_sampler_view *sv[2];
921 int num_sampler_view = 1;
922 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
923 struct st_fp_variant *fpv;
924
925 if (format == GL_DEPTH_STENCIL)
926 write_stencil = write_depth = GL_TRUE;
927 else if (format == GL_STENCIL_INDEX)
928 write_stencil = GL_TRUE;
929 else if (format == GL_DEPTH_COMPONENT)
930 write_depth = GL_TRUE;
931
932 if (write_stencil) {
933 enum pipe_format tex_format;
934 /* can we write to stencil if not fallback */
935 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT))
936 goto stencil_fallback;
937
938 tex_format = st_choose_format(st->pipe->screen, base_format(format),
939 PIPE_TEXTURE_2D,
940 0, PIPE_BIND_SAMPLER_VIEW);
941 if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
942 stencil_format = PIPE_FORMAT_X24S8_USCALED;
943 else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM)
944 stencil_format = PIPE_FORMAT_S8X24_USCALED;
945 else
946 stencil_format = PIPE_FORMAT_S8_USCALED;
947 if (stencil_format == PIPE_FORMAT_NONE)
948 goto stencil_fallback;
949 }
950
951 /* Mesa state should be up to date by now */
952 assert(ctx->NewState == 0x0);
953
954 st_validate_state(st);
955
956 /*
957 * Get vertex/fragment shaders
958 */
959 if (write_depth || write_stencil) {
960 fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
961
962 driver_fp = fpv->driver_shader;
963
964 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
965
966 color = ctx->Current.RasterColor;
967 }
968 else {
969 fpv = get_color_fp_variant(st);
970
971 driver_fp = fpv->driver_shader;
972
973 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
974
975 color = NULL;
976 if (st->pixel_xfer.pixelmap_enabled) {
977 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
978 num_sampler_view++;
979 }
980 }
981
982 /* update fragment program constants */
983 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
984
985 /* draw with textured quad */
986 {
987 struct pipe_resource *pt
988 = make_texture(st, width, height, format, type, unpack, pixels);
989 if (pt) {
990 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
991
992 if (sv[0]) {
993 if (write_stencil) {
994 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
995 stencil_format);
996 num_sampler_view++;
997 }
998
999 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1000 width, height,
1001 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1002 sv,
1003 num_sampler_view,
1004 driver_vp,
1005 driver_fp,
1006 color, GL_FALSE, write_depth, write_stencil);
1007 pipe_sampler_view_reference(&sv[0], NULL);
1008 if (num_sampler_view > 1)
1009 pipe_sampler_view_reference(&sv[1], NULL);
1010 }
1011 pipe_resource_reference(&pt, NULL);
1012 }
1013 }
1014 return;
1015
1016 stencil_fallback:
1017 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1018 unpack, pixels);
1019 }
1020
1021
1022
1023 /**
1024 * Software fallback for glCopyPixels(GL_STENCIL).
1025 */
1026 static void
1027 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1028 GLsizei width, GLsizei height,
1029 GLint dstx, GLint dsty)
1030 {
1031 struct st_renderbuffer *rbDraw;
1032 struct pipe_context *pipe = st_context(ctx)->pipe;
1033 enum pipe_transfer_usage usage;
1034 struct pipe_transfer *ptDraw;
1035 ubyte *drawMap;
1036 ubyte *buffer;
1037 int i;
1038
1039 buffer = malloc(width * height * sizeof(ubyte));
1040 if (!buffer) {
1041 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1042 return;
1043 }
1044
1045 /* Get the dest renderbuffer. If there's a wrapper, use the
1046 * underlying renderbuffer.
1047 */
1048 rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
1049 if (rbDraw->Base.Wrapped)
1050 rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
1051
1052 /* this will do stencil pixel transfer ops */
1053 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
1054 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1055 &ctx->DefaultPacking, buffer);
1056
1057 if (0) {
1058 /* debug code: dump stencil values */
1059 GLint row, col;
1060 for (row = 0; row < height; row++) {
1061 printf("%3d: ", row);
1062 for (col = 0; col < width; col++) {
1063 printf("%02x ", buffer[col + row * width]);
1064 }
1065 printf("\n");
1066 }
1067 }
1068
1069 if (util_format_get_component_bits(rbDraw->format,
1070 UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
1071 usage = PIPE_TRANSFER_READ_WRITE;
1072 else
1073 usage = PIPE_TRANSFER_WRITE;
1074
1075 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1076 dsty = rbDraw->Base.Height - dsty - height;
1077 }
1078
1079 ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
1080 rbDraw->texture, 0, 0,
1081 usage, dstx, dsty,
1082 width, height);
1083
1084 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
1085 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
1086
1087 /* map the stencil buffer */
1088 drawMap = pipe_transfer_map(pipe, ptDraw);
1089
1090 /* draw */
1091 /* XXX PixelZoom not handled yet */
1092 for (i = 0; i < height; i++) {
1093 ubyte *dst;
1094 const ubyte *src;
1095 int y;
1096
1097 y = i;
1098
1099 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1100 y = height - y - 1;
1101 }
1102
1103 dst = drawMap + y * ptDraw->stride;
1104 src = buffer + i * width;
1105
1106 switch (ptDraw->resource->format) {
1107 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
1108 {
1109 uint *dst4 = (uint *) dst;
1110 int j;
1111 assert(usage == PIPE_TRANSFER_READ_WRITE);
1112 for (j = 0; j < width; j++) {
1113 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
1114 dst4++;
1115 }
1116 }
1117 break;
1118 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
1119 {
1120 uint *dst4 = (uint *) dst;
1121 int j;
1122 assert(usage == PIPE_TRANSFER_READ_WRITE);
1123 for (j = 0; j < width; j++) {
1124 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
1125 dst4++;
1126 }
1127 }
1128 break;
1129 case PIPE_FORMAT_S8_USCALED:
1130 assert(usage == PIPE_TRANSFER_WRITE);
1131 memcpy(dst, src, width);
1132 break;
1133 default:
1134 assert(0);
1135 }
1136 }
1137
1138 free(buffer);
1139
1140 /* unmap the stencil buffer */
1141 pipe_transfer_unmap(pipe, ptDraw);
1142 pipe->transfer_destroy(pipe, ptDraw);
1143 }
1144
1145
1146 /** Do the src/dest regions overlap? */
1147 static GLboolean
1148 regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
1149 GLsizei width, GLsizei height)
1150 {
1151 if (srcX + width <= dstX ||
1152 dstX + width <= srcX ||
1153 srcY + height <= dstY ||
1154 dstY + height <= srcY)
1155 return GL_FALSE;
1156 else
1157 return GL_TRUE;
1158 }
1159
1160
1161 /**
1162 * Try to do a glCopyPixels for simple cases with a blit by calling
1163 * pipe->resource_copy_region().
1164 *
1165 * We can do this when we're copying color pixels (depth/stencil
1166 * eventually) with no pixel zoom, no pixel transfer ops, no
1167 * per-fragment ops, the src/dest regions don't overlap and the
1168 * src/dest pixel formats are the same.
1169 */
1170 static GLboolean
1171 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1172 GLsizei width, GLsizei height,
1173 GLint dstx, GLint dsty, GLenum type)
1174 {
1175 struct st_context *st = st_context(ctx);
1176 struct pipe_context *pipe = st->pipe;
1177 struct gl_pixelstore_attrib pack, unpack;
1178 GLint readX, readY, readW, readH;
1179
1180 if (type == GL_COLOR &&
1181 ctx->Pixel.ZoomX == 1.0 &&
1182 ctx->Pixel.ZoomY == 1.0 &&
1183 ctx->_ImageTransferState == 0x0 &&
1184 !ctx->Color.BlendEnabled &&
1185 !ctx->Color.AlphaEnabled &&
1186 !ctx->Depth.Test &&
1187 !ctx->Fog.Enabled &&
1188 !ctx->Stencil.Enabled &&
1189 !ctx->FragmentProgram.Enabled &&
1190 !ctx->VertexProgram.Enabled &&
1191 !ctx->Shader.CurrentFragmentProgram &&
1192 st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
1193 ctx->DrawBuffer->_NumColorDrawBuffers == 1) {
1194 struct st_renderbuffer *rbRead, *rbDraw;
1195 GLint drawX, drawY;
1196
1197 /*
1198 * Clip the read region against the src buffer bounds.
1199 * We'll still allocate a temporary buffer/texture for the original
1200 * src region size but we'll only read the region which is on-screen.
1201 * This may mean that we draw garbage pixels into the dest region, but
1202 * that's expected.
1203 */
1204 readX = srcx;
1205 readY = srcy;
1206 readW = width;
1207 readH = height;
1208 pack = ctx->DefaultPacking;
1209 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1210 return GL_TRUE; /* all done */
1211
1212 /* clip against dest buffer bounds and scissor box */
1213 drawX = dstx + pack.SkipPixels;
1214 drawY = dsty + pack.SkipRows;
1215 unpack = pack;
1216 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1217 return GL_TRUE; /* all done */
1218
1219 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1220 readY = readY - pack.SkipRows + unpack.SkipRows;
1221
1222 rbRead = st_get_color_read_renderbuffer(ctx);
1223 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1224
1225 if ((rbRead != rbDraw ||
1226 !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
1227 rbRead->Base.Format == rbDraw->Base.Format) {
1228 struct pipe_box srcBox;
1229
1230 /* flip src/dst position if needed */
1231 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1232 /* both buffers will have the same orientation */
1233 readY = ctx->ReadBuffer->Height - readY - readH;
1234 drawY = ctx->DrawBuffer->Height - drawY - readH;
1235 }
1236
1237 u_box_2d(readX, readY, readW, readH, &srcBox);
1238
1239 pipe->resource_copy_region(pipe,
1240 rbDraw->texture, 0, drawX, drawY, 0,
1241 rbRead->texture, 0, &srcBox);
1242 return GL_TRUE;
1243 }
1244 }
1245
1246 return GL_FALSE;
1247 }
1248
1249
1250 static void
1251 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1252 GLsizei width, GLsizei height,
1253 GLint dstx, GLint dsty, GLenum type)
1254 {
1255 struct st_context *st = st_context(ctx);
1256 struct pipe_context *pipe = st->pipe;
1257 struct pipe_screen *screen = pipe->screen;
1258 struct st_renderbuffer *rbRead;
1259 void *driver_vp, *driver_fp;
1260 struct pipe_resource *pt;
1261 struct pipe_sampler_view *sv[2];
1262 int num_sampler_view = 1;
1263 GLfloat *color;
1264 enum pipe_format srcFormat, texFormat;
1265 GLboolean invertTex = GL_FALSE;
1266 GLint readX, readY, readW, readH;
1267 GLuint sample_count;
1268 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1269 struct st_fp_variant *fpv;
1270
1271 st_validate_state(st);
1272
1273 if (type == GL_STENCIL) {
1274 /* can't use texturing to do stencil */
1275 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1276 return;
1277 }
1278
1279 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1280 return;
1281
1282 /*
1283 * The subsequent code implements glCopyPixels by copying the source
1284 * pixels into a temporary texture that's then applied to a textured quad.
1285 * When we draw the textured quad, all the usual per-fragment operations
1286 * are handled.
1287 */
1288
1289
1290 /*
1291 * Get vertex/fragment shaders
1292 */
1293 if (type == GL_COLOR) {
1294 rbRead = st_get_color_read_renderbuffer(ctx);
1295 color = NULL;
1296
1297 fpv = get_color_fp_variant(st);
1298 driver_fp = fpv->driver_shader;
1299
1300 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1301
1302 if (st->pixel_xfer.pixelmap_enabled) {
1303 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1304 num_sampler_view++;
1305 }
1306 }
1307 else {
1308 assert(type == GL_DEPTH);
1309 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
1310 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1311
1312 fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
1313 driver_fp = fpv->driver_shader;
1314
1315 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1316 }
1317
1318 /* update fragment program constants */
1319 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1320
1321
1322 if (rbRead->Base.Wrapped)
1323 rbRead = st_renderbuffer(rbRead->Base.Wrapped);
1324
1325 sample_count = rbRead->texture->nr_samples;
1326 /* I believe this would be legal, presumably would need to do a resolve
1327 for color, and for depth/stencil spec says to just use one of the
1328 depth/stencil samples per pixel? Need some transfer clarifications. */
1329 assert(sample_count < 2);
1330
1331 srcFormat = rbRead->texture->format;
1332
1333 if (screen->is_format_supported(screen, srcFormat, st->internal_target,
1334 sample_count,
1335 PIPE_BIND_SAMPLER_VIEW, 0)) {
1336 texFormat = srcFormat;
1337 }
1338 else {
1339 /* srcFormat can't be used as a texture format */
1340 if (type == GL_DEPTH) {
1341 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1342 st->internal_target, sample_count,
1343 PIPE_BIND_DEPTH_STENCIL);
1344 assert(texFormat != PIPE_FORMAT_NONE);
1345 }
1346 else {
1347 /* default color format */
1348 texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
1349 sample_count, PIPE_BIND_SAMPLER_VIEW);
1350 assert(texFormat != PIPE_FORMAT_NONE);
1351 }
1352 }
1353
1354 /* Invert src region if needed */
1355 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1356 srcy = ctx->ReadBuffer->Height - srcy - height;
1357 invertTex = !invertTex;
1358 }
1359
1360 /* Clip the read region against the src buffer bounds.
1361 * We'll still allocate a temporary buffer/texture for the original
1362 * src region size but we'll only read the region which is on-screen.
1363 * This may mean that we draw garbage pixels into the dest region, but
1364 * that's expected.
1365 */
1366 readX = srcx;
1367 readY = srcy;
1368 readW = width;
1369 readH = height;
1370 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1371 readW = MAX2(0, readW);
1372 readH = MAX2(0, readH);
1373
1374 /* alloc temporary texture */
1375 pt = alloc_texture(st, width, height, texFormat);
1376 if (!pt)
1377 return;
1378
1379 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1380 if (!sv[0]) {
1381 pipe_resource_reference(&pt, NULL);
1382 return;
1383 }
1384
1385 /* Make temporary texture which is a copy of the src region.
1386 */
1387 if (srcFormat == texFormat) {
1388 struct pipe_box src_box;
1389 u_box_2d(readX, readY, readW, readH, &src_box);
1390 /* copy source framebuffer surface into mipmap/texture */
1391 pipe->resource_copy_region(pipe,
1392 pt, /* dest tex */
1393 0,
1394 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1395 rbRead->texture, /* src tex */
1396 0,
1397 &src_box);
1398
1399 }
1400 else {
1401 /* CPU-based fallback/conversion */
1402 struct pipe_transfer *ptRead =
1403 pipe_get_transfer(st->pipe, rbRead->texture,
1404 0, 0, /* level, layer */
1405 PIPE_TRANSFER_READ,
1406 readX, readY, readW, readH);
1407 struct pipe_transfer *ptTex;
1408 enum pipe_transfer_usage transfer_usage;
1409
1410 if (ST_DEBUG & DEBUG_FALLBACK)
1411 debug_printf("%s: fallback processing\n", __FUNCTION__);
1412
1413 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1414 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1415 else
1416 transfer_usage = PIPE_TRANSFER_WRITE;
1417
1418 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
1419 0, 0, width, height);
1420
1421 /* copy image from ptRead surface to ptTex surface */
1422 if (type == GL_COLOR) {
1423 /* alternate path using get/put_tile() */
1424 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1425 enum pipe_format readFormat, drawFormat;
1426 readFormat = util_format_linear(rbRead->texture->format);
1427 drawFormat = util_format_linear(pt->format);
1428 pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
1429 readFormat, buf);
1430 pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1431 readW, readH, drawFormat, buf);
1432 free(buf);
1433 }
1434 else {
1435 /* GL_DEPTH */
1436 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1437 pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
1438 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1439 readW, readH, buf);
1440 free(buf);
1441 }
1442
1443 pipe->transfer_destroy(pipe, ptRead);
1444 pipe->transfer_destroy(pipe, ptTex);
1445 }
1446
1447 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1448 * textured quad with that texture.
1449 */
1450 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1451 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1452 sv,
1453 num_sampler_view,
1454 driver_vp,
1455 driver_fp,
1456 color, invertTex, GL_FALSE, GL_FALSE);
1457
1458 pipe_resource_reference(&pt, NULL);
1459 pipe_sampler_view_reference(&sv[0], NULL);
1460 }
1461
1462
1463
1464 void st_init_drawpixels_functions(struct dd_function_table *functions)
1465 {
1466 functions->DrawPixels = st_DrawPixels;
1467 functions->CopyPixels = st_CopyPixels;
1468 }
1469
1470
1471 void
1472 st_destroy_drawpix(struct st_context *st)
1473 {
1474 GLuint i;
1475
1476 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1477 if (st->drawpix.shaders[i])
1478 _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
1479 }
1480
1481 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1482 if (st->drawpix.vert_shaders[0])
1483 ureg_free_tokens(st->drawpix.vert_shaders[0]);
1484 if (st->drawpix.vert_shaders[1])
1485 ureg_free_tokens(st->drawpix.vert_shaders[1]);
1486 }
1487
1488 #endif /* FEATURE_drawpix */