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