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