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