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