0a4430501f9dc84b5244f1bfaab841646990dac8
[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/texformat.h"
38 #include "main/state.h"
39 #include "shader/program.h"
40 #include "shader/prog_parameter.h"
41 #include "shader/prog_print.h"
42
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_atom_constbuf.h"
46 #include "st_draw.h"
47 #include "st_program.h"
48 #include "st_cb_drawpixels.h"
49 #include "st_cb_readpixels.h"
50 #include "st_cb_fbo.h"
51 #include "st_cb_texture.h"
52 #include "st_draw.h"
53 #include "st_format.h"
54 #include "st_mesa_to_tgsi.h"
55 #include "st_texture.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "pipe/p_inlines.h"
59 #include "util/u_tile.h"
60 #include "util/u_draw_quad.h"
61 #include "shader/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
63
64
65 /**
66 * Check if the given program is:
67 * 0: MOVE result.color, fragment.color;
68 * 1: END;
69 */
70 static GLboolean
71 is_passthrough_program(const struct gl_fragment_program *prog)
72 {
73 if (prog->Base.NumInstructions == 2) {
74 const struct prog_instruction *inst = prog->Base.Instructions;
75 if (inst[0].Opcode == OPCODE_MOV &&
76 inst[1].Opcode == OPCODE_END &&
77 inst[0].DstReg.File == PROGRAM_OUTPUT &&
78 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
79 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
80 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
81 inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
82 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
83 return GL_TRUE;
84 }
85 }
86 return GL_FALSE;
87 }
88
89
90
91 /**
92 * Make fragment shader for glDraw/CopyPixels. This shader is made
93 * by combining the pixel transfer shader with the user-defined shader.
94 */
95 static struct st_fragment_program *
96 combined_drawpix_fragment_program(GLcontext *ctx)
97 {
98 struct st_context *st = ctx->st;
99 struct st_fragment_program *stfp;
100
101 if (st->pixel_xfer.program->serialNo == st->pixel_xfer.xfer_prog_sn
102 && st->fp->serialNo == st->pixel_xfer.user_prog_sn) {
103 /* the pixel tranfer program has not changed and the user-defined
104 * program has not changed, so re-use the combined program.
105 */
106 stfp = st->pixel_xfer.combined_prog;
107 }
108 else {
109 /* Concatenate the pixel transfer program with the current user-
110 * defined program.
111 */
112 if (is_passthrough_program(&st->fp->Base)) {
113 stfp = (struct st_fragment_program *)
114 _mesa_clone_program(ctx, &st->pixel_xfer.program->Base.Base);
115 }
116 else {
117 #if 0
118 printf("Base program:\n");
119 _mesa_print_program(&st->fp->Base.Base);
120 printf("DrawPix program:\n");
121 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
122 #endif
123 stfp = (struct st_fragment_program *)
124 _mesa_combine_programs(ctx,
125 &st->pixel_xfer.program->Base.Base,
126 &st->fp->Base.Base);
127 }
128
129 #if 0
130 {
131 struct gl_program *p = &stfp->Base.Base;
132 printf("Combined DrawPixels program:\n");
133 _mesa_print_program(p);
134 printf("InputsRead: 0x%x\n", p->InputsRead);
135 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
136 _mesa_print_parameter_list(p->Parameters);
137 }
138 #endif
139
140 /* translate to TGSI tokens */
141 st_translate_fragment_program(st, stfp, NULL);
142
143 /* save new program, update serial numbers */
144 st->pixel_xfer.xfer_prog_sn = st->pixel_xfer.program->serialNo;
145 st->pixel_xfer.user_prog_sn = st->fp->serialNo;
146 st->pixel_xfer.combined_prog_sn = stfp->serialNo;
147 st->pixel_xfer.combined_prog = stfp;
148 }
149
150 /* Ideally we'd have updated the pipe constants during the normal
151 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
152 */
153 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
154
155 return stfp;
156 }
157
158
159 /**
160 * Create fragment shader that does a TEX() instruction to get a Z
161 * value, then writes to FRAG_RESULT_DEPTH.
162 * Pass fragment color through as-is.
163 */
164 static struct st_fragment_program *
165 make_fragment_shader_z(struct st_context *st)
166 {
167 GLcontext *ctx = st->ctx;
168 struct gl_program *p;
169 GLuint ic = 0;
170
171 if (st->drawpix.z_shader) {
172 return st->drawpix.z_shader;
173 }
174
175 /*
176 * Create shader now
177 */
178 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
179 if (!p)
180 return NULL;
181
182 p->NumInstructions = 3;
183
184 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
185 if (!p->Instructions) {
186 ctx->Driver.DeleteProgram(ctx, p);
187 return NULL;
188 }
189 _mesa_init_instructions(p->Instructions, p->NumInstructions);
190
191 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
192 p->Instructions[ic].Opcode = OPCODE_TEX;
193 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
194 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
195 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
196 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
197 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
198 p->Instructions[ic].TexSrcUnit = 0;
199 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
200 ic++;
201
202 /* MOV result.color, fragment.color */
203 p->Instructions[ic].Opcode = OPCODE_MOV;
204 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
205 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR;
206 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
207 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0;
208 ic++;
209
210 /* END; */
211 p->Instructions[ic++].Opcode = OPCODE_END;
212
213 assert(ic == p->NumInstructions);
214
215 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
216 p->OutputsWritten = (1 << FRAG_RESULT_COLOR) | (1 << FRAG_RESULT_DEPTH);
217 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
218
219 st->drawpix.z_shader = (struct st_fragment_program *) p;
220 st_translate_fragment_program(st, st->drawpix.z_shader, NULL);
221
222 return st->drawpix.z_shader;
223 }
224
225
226
227 /**
228 * Create a simple vertex shader that just passes through the
229 * vertex position and texcoord (and optionally, color).
230 */
231 static struct st_vertex_program *
232 st_make_passthrough_vertex_shader(struct st_context *st, GLboolean passColor)
233 {
234 GLcontext *ctx = st->ctx;
235 struct st_vertex_program *stvp;
236 struct gl_program *p;
237 GLuint ic = 0;
238
239 if (st->drawpix.vert_shaders[passColor])
240 return st->drawpix.vert_shaders[passColor];
241
242 /*
243 * Create shader now
244 */
245 p = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
246 if (!p)
247 return NULL;
248
249 if (passColor)
250 p->NumInstructions = 4;
251 else
252 p->NumInstructions = 3;
253
254 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
255 if (!p->Instructions) {
256 ctx->Driver.DeleteProgram(ctx, p);
257 return NULL;
258 }
259 _mesa_init_instructions(p->Instructions, p->NumInstructions);
260 /* MOV result.pos, vertex.pos; */
261 p->Instructions[0].Opcode = OPCODE_MOV;
262 p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
263 p->Instructions[0].DstReg.Index = VERT_RESULT_HPOS;
264 p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
265 p->Instructions[0].SrcReg[0].Index = VERT_ATTRIB_POS;
266 /* MOV result.texcoord0, vertex.texcoord0; */
267 p->Instructions[1].Opcode = OPCODE_MOV;
268 p->Instructions[1].DstReg.File = PROGRAM_OUTPUT;
269 p->Instructions[1].DstReg.Index = VERT_RESULT_TEX0;
270 p->Instructions[1].SrcReg[0].File = PROGRAM_INPUT;
271 p->Instructions[1].SrcReg[0].Index = VERT_ATTRIB_TEX0;
272 ic = 2;
273 if (passColor) {
274 /* MOV result.color0, vertex.color0; */
275 p->Instructions[ic].Opcode = OPCODE_MOV;
276 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
277 p->Instructions[ic].DstReg.Index = VERT_RESULT_COL0;
278 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
279 p->Instructions[ic].SrcReg[0].Index = VERT_ATTRIB_COLOR0;
280 ic++;
281 }
282
283 /* END; */
284 p->Instructions[ic].Opcode = OPCODE_END;
285 ic++;
286
287 assert(ic == p->NumInstructions);
288
289 p->InputsRead = VERT_BIT_POS | VERT_BIT_TEX0;
290 p->OutputsWritten = ((1 << VERT_RESULT_TEX0) |
291 (1 << VERT_RESULT_HPOS));
292 if (passColor) {
293 p->InputsRead |= VERT_BIT_COLOR0;
294 p->OutputsWritten |= (1 << VERT_RESULT_COL0);
295 }
296
297 stvp = (struct st_vertex_program *) p;
298 st_translate_vertex_program(st, stvp, NULL, NULL, NULL);
299
300 st->drawpix.vert_shaders[passColor] = stvp;
301
302 return stvp;
303 }
304
305
306 static GLenum
307 _mesa_base_format(GLenum format)
308 {
309 switch (format) {
310 case GL_DEPTH_COMPONENT:
311 return GL_DEPTH_COMPONENT;
312 case GL_DEPTH_STENCIL:
313 return GL_DEPTH_STENCIL;
314 case GL_STENCIL_INDEX:
315 return GL_STENCIL_INDEX;
316 default:
317 return GL_RGBA;
318 }
319 }
320
321
322 /**
323 * Make texture containing an image for glDrawPixels image.
324 * If 'pixels' is NULL, leave the texture image data undefined.
325 */
326 static struct pipe_texture *
327 make_texture(struct st_context *st,
328 GLsizei width, GLsizei height, GLenum format, GLenum type,
329 const struct gl_pixelstore_attrib *unpack,
330 const GLvoid *pixels)
331 {
332 GLcontext *ctx = st->ctx;
333 struct pipe_context *pipe = st->pipe;
334 struct pipe_screen *screen = pipe->screen;
335 const struct gl_texture_format *mformat;
336 struct pipe_texture *pt;
337 enum pipe_format pipeFormat;
338 GLuint cpp;
339 GLenum baseFormat;
340
341 baseFormat = _mesa_base_format(format);
342
343 mformat = st_ChooseTextureFormat(ctx, baseFormat, format, type);
344 assert(mformat);
345
346 pipeFormat = st_mesa_format_to_pipe_format(mformat->MesaFormat);
347 assert(pipeFormat);
348 cpp = st_sizeof_format(pipeFormat);
349
350 pixels = _mesa_map_drawpix_pbo(ctx, unpack, pixels);
351 if (!pixels)
352 return NULL;
353
354 pt = st_texture_create(st, PIPE_TEXTURE_2D, pipeFormat, 0, width, height, 1,
355 PIPE_TEXTURE_USAGE_SAMPLER);
356 if (!pt) {
357 _mesa_unmap_drawpix_pbo(ctx, unpack);
358 return NULL;
359 }
360
361 {
362 struct pipe_transfer *transfer;
363 static const GLuint dstImageOffsets = 0;
364 GLboolean success;
365 GLubyte *dest;
366 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
367
368 /* we'll do pixel transfer in a fragment shader */
369 ctx->_ImageTransferState = 0x0;
370
371 transfer = screen->get_tex_transfer(screen, pt, 0, 0, 0,
372 PIPE_TRANSFER_WRITE, 0, 0,
373 width, height);
374
375 /* map texture transfer */
376 dest = screen->transfer_map(screen, transfer);
377
378 /* Put image into texture transfer.
379 * Note that the image is actually going to be upside down in
380 * the texture. We deal with that with texcoords.
381 */
382 success = mformat->StoreImage(ctx, 2, /* dims */
383 baseFormat, /* baseInternalFormat */
384 mformat, /* gl_texture_format */
385 dest, /* dest */
386 0, 0, 0, /* dstX/Y/Zoffset */
387 transfer->stride, /* dstRowStride, bytes */
388 &dstImageOffsets, /* dstImageOffsets */
389 width, height, 1, /* size */
390 format, type, /* src format/type */
391 pixels, /* data source */
392 unpack);
393
394 /* unmap */
395 screen->transfer_unmap(screen, transfer);
396 screen->tex_transfer_destroy(transfer);
397
398 assert(success);
399
400 /* restore */
401 ctx->_ImageTransferState = imageTransferStateSave;
402 }
403
404 _mesa_unmap_drawpix_pbo(ctx, unpack);
405
406 return pt;
407 }
408
409
410 /**
411 * Draw quad with texcoords and optional color.
412 * Coords are window coords with y=0=bottom.
413 * \param color may be null
414 * \param invertTex if true, flip texcoords vertically
415 */
416 static void
417 draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
418 GLfloat x1, GLfloat y1, const GLfloat *color,
419 GLboolean invertTex)
420 {
421 struct st_context *st = ctx->st;
422 struct pipe_context *pipe = ctx->st->pipe;
423 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
424
425 /* setup vertex data */
426 {
427 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
428 const GLfloat fb_width = (GLfloat) fb->Width;
429 const GLfloat fb_height = (GLfloat) fb->Height;
430 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
431 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
432 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
433 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
434 const GLfloat sLeft = 0.0f, sRight = 1.0f;
435 const GLfloat tTop = invertTex, tBot = 1.0f - tTop;
436 GLuint tex, i;
437
438 /* upper-left */
439 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
440 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
441
442 /* upper-right */
443 verts[1][0][0] = clip_x1;
444 verts[1][0][1] = clip_y0;
445
446 /* lower-right */
447 verts[2][0][0] = clip_x1;
448 verts[2][0][1] = clip_y1;
449
450 /* lower-left */
451 verts[3][0][0] = clip_x0;
452 verts[3][0][1] = clip_y1;
453
454 tex = color ? 2 : 1;
455 verts[0][tex][0] = sLeft; /* v[0].attr[tex].s */
456 verts[0][tex][1] = tTop; /* v[0].attr[tex].t */
457 verts[1][tex][0] = sRight;
458 verts[1][tex][1] = tTop;
459 verts[2][tex][0] = sRight;
460 verts[2][tex][1] = tBot;
461 verts[3][tex][0] = sLeft;
462 verts[3][tex][1] = tBot;
463
464 /* same for all verts: */
465 if (color) {
466 for (i = 0; i < 4; i++) {
467 verts[i][0][2] = z; /*Z*/
468 verts[i][0][3] = 1.0f; /*W*/
469 verts[i][1][0] = color[0];
470 verts[i][1][1] = color[1];
471 verts[i][1][2] = color[2];
472 verts[i][1][3] = color[3];
473 verts[i][2][2] = 0.0f; /*R*/
474 verts[i][2][3] = 1.0f; /*Q*/
475 }
476 }
477 else {
478 for (i = 0; i < 4; i++) {
479 verts[i][0][2] = z; /*Z*/
480 verts[i][0][3] = 1.0f; /*W*/
481 verts[i][1][2] = 0.0f; /*R*/
482 verts[i][1][3] = 1.0f; /*Q*/
483 }
484 }
485 }
486
487 {
488 struct pipe_buffer *buf;
489
490 /* allocate/load buffer object with vertex data */
491 buf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
492 sizeof(verts));
493 pipe_buffer_write(pipe->screen, buf, 0, sizeof(verts), verts);
494
495 util_draw_vertex_buffer(pipe, buf, 0,
496 PIPE_PRIM_QUADS,
497 4, /* verts */
498 3); /* attribs/vert */
499 pipe_buffer_reference(&buf, NULL);
500 }
501 }
502
503
504
505 static void
506 draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
507 GLsizei width, GLsizei height,
508 GLfloat zoomX, GLfloat zoomY,
509 struct pipe_texture *pt,
510 struct st_vertex_program *stvp,
511 struct st_fragment_program *stfp,
512 const GLfloat *color,
513 GLboolean invertTex)
514 {
515 struct st_context *st = ctx->st;
516 struct pipe_context *pipe = ctx->st->pipe;
517 struct cso_context *cso = ctx->st->cso_context;
518 GLfloat x0, y0, x1, y1;
519 GLsizei maxSize;
520
521 /* limit checks */
522 /* XXX if DrawPixels image is larger than max texture size, break
523 * it up into chunks.
524 */
525 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
526 assert(width <= maxSize);
527 assert(height <= maxSize);
528
529 cso_save_rasterizer(cso);
530 cso_save_viewport(cso);
531 cso_save_samplers(cso);
532 cso_save_sampler_textures(cso);
533 cso_save_fragment_shader(cso);
534 cso_save_vertex_shader(cso);
535
536 /* rasterizer state: just scissor */
537 {
538 struct pipe_rasterizer_state rasterizer;
539 memset(&rasterizer, 0, sizeof(rasterizer));
540 rasterizer.gl_rasterization_rules = 1;
541 rasterizer.scissor = ctx->Scissor.Enabled;
542 cso_set_rasterizer(cso, &rasterizer);
543 }
544
545 /* fragment shader state: TEX lookup program */
546 cso_set_fragment_shader_handle(cso, stfp->driver_shader);
547
548 /* vertex shader state: position + texcoord pass-through */
549 cso_set_vertex_shader_handle(cso, stvp->driver_shader);
550
551
552 /* texture sampling state: */
553 {
554 struct pipe_sampler_state sampler;
555 memset(&sampler, 0, sizeof(sampler));
556 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
557 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
558 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
559 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
560 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
561 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
562 sampler.normalized_coords = 1;
563
564 cso_single_sampler(cso, 0, &sampler);
565 if (st->pixel_xfer.pixelmap_enabled) {
566 cso_single_sampler(cso, 1, &sampler);
567 }
568 cso_single_sampler_done(cso);
569 }
570
571 /* viewport state: viewport matching window dims */
572 {
573 const float width = (float) ctx->DrawBuffer->Width;
574 const float height = (float) ctx->DrawBuffer->Height;
575 struct pipe_viewport_state vp;
576 vp.scale[0] = 0.5f * width;
577 vp.scale[1] = -0.5f * height;
578 vp.scale[2] = 1.0f;
579 vp.scale[3] = 1.0f;
580 vp.translate[0] = 0.5f * width;
581 vp.translate[1] = 0.5f * height;
582 vp.translate[2] = 0.0f;
583 vp.translate[3] = 0.0f;
584 cso_set_viewport(cso, &vp);
585 }
586
587 /* texture state: */
588 if (st->pixel_xfer.pixelmap_enabled) {
589 struct pipe_texture *textures[2];
590 textures[0] = pt;
591 textures[1] = st->pixel_xfer.pixelmap_texture;
592 pipe->set_sampler_textures(pipe, 2, textures);
593 }
594 else {
595 pipe->set_sampler_textures(pipe, 1, &pt);
596 }
597
598 /* Compute window coords (y=0=bottom) with pixel zoom.
599 * Recall that these coords are transformed by the current
600 * vertex shader and viewport transformation.
601 */
602 x0 = (GLfloat) x;
603 x1 = x + width * ctx->Pixel.ZoomX;
604 y0 = (GLfloat) y;
605 y1 = y + height * ctx->Pixel.ZoomY;
606
607 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex);
608
609 /* restore state */
610 cso_restore_rasterizer(cso);
611 cso_restore_viewport(cso);
612 cso_restore_samplers(cso);
613 cso_restore_sampler_textures(cso);
614 cso_restore_fragment_shader(cso);
615 cso_restore_vertex_shader(cso);
616 }
617
618
619 static void
620 draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
621 GLsizei width, GLsizei height, GLenum format, GLenum type,
622 const struct gl_pixelstore_attrib *unpack,
623 const GLvoid *pixels)
624 {
625 struct st_context *st = ctx->st;
626 struct pipe_context *pipe = st->pipe;
627 struct pipe_screen *screen = pipe->screen;
628 struct st_renderbuffer *strb;
629 struct pipe_transfer *pt;
630 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
631 GLint skipPixels;
632 ubyte *stmap;
633
634 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
635
636 strb = st_renderbuffer(ctx->DrawBuffer->
637 Attachment[BUFFER_STENCIL].Renderbuffer);
638
639 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
640 y = ctx->DrawBuffer->Height - y - height;
641 }
642
643 pt = screen->get_tex_transfer(screen, strb->texture, 0, 0, 0,
644 PIPE_TRANSFER_WRITE, x, y,
645 width, height);
646
647 stmap = screen->transfer_map(screen, pt);
648
649 pixels = _mesa_map_drawpix_pbo(ctx, unpack, pixels);
650 assert(pixels);
651
652 /* if width > MAX_WIDTH, have to process image in chunks */
653 skipPixels = 0;
654 while (skipPixels < width) {
655 const GLint spanX = skipPixels;
656 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
657 GLint row;
658 for (row = 0; row < height; row++) {
659 GLubyte sValues[MAX_WIDTH];
660 GLuint zValues[MAX_WIDTH];
661 GLenum destType = GL_UNSIGNED_BYTE;
662 const GLvoid *source = _mesa_image_address2d(unpack, pixels,
663 width, height,
664 format, type,
665 row, skipPixels);
666 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
667 type, source, unpack,
668 ctx->_ImageTransferState);
669
670 if (format == GL_DEPTH_STENCIL) {
671 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
672 (1 << 24) - 1, type, source, unpack);
673 }
674
675 if (zoom) {
676 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
677 "zoom not complete");
678 }
679
680 {
681 GLint spanY;
682
683 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
684 spanY = height - row - 1;
685 }
686 else {
687 spanY = row;
688 }
689
690 /* now pack the stencil (and Z) values in the dest format */
691 switch (pt->format) {
692 case PIPE_FORMAT_S8_UNORM:
693 {
694 ubyte *dest = stmap + spanY * pt->stride + spanX;
695 memcpy(dest, sValues, spanWidth);
696 }
697 break;
698 case PIPE_FORMAT_S8Z24_UNORM:
699 if (format == GL_DEPTH_STENCIL) {
700 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
701 GLint k;
702 for (k = 0; k < spanWidth; k++) {
703 dest[k] = zValues[k] | (sValues[k] << 24);
704 }
705 }
706 else {
707 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
708 GLint k;
709 for (k = 0; k < spanWidth; k++) {
710 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
711 }
712 }
713 break;
714 case PIPE_FORMAT_Z24S8_UNORM:
715 if (format == GL_DEPTH_STENCIL) {
716 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
717 GLint k;
718 for (k = 0; k < spanWidth; k++) {
719 dest[k] = zValues[k] | (sValues[k] << 24);
720 }
721 }
722 else {
723 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
724 GLint k;
725 for (k = 0; k < spanWidth; k++) {
726 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
727 }
728 }
729 break;
730 default:
731 assert(0);
732 }
733 }
734 }
735 skipPixels += spanWidth;
736 }
737
738 _mesa_unmap_drawpix_pbo(ctx, unpack);
739
740 /* unmap the stencil buffer */
741 screen->transfer_unmap(screen, pt);
742 screen->tex_transfer_destroy(pt);
743 }
744
745
746 /**
747 * Called via ctx->Driver.DrawPixels()
748 */
749 static void
750 st_DrawPixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
751 GLenum format, GLenum type,
752 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
753 {
754 struct st_fragment_program *stfp;
755 struct st_vertex_program *stvp;
756 struct st_context *st = ctx->st;
757 struct pipe_surface *ps;
758 GLuint bufferFormat;
759 const GLfloat *color;
760
761 if (format == GL_STENCIL_INDEX ||
762 format == GL_DEPTH_STENCIL) {
763 draw_stencil_pixels(ctx, x, y, width, height, format, type,
764 unpack, pixels);
765 return;
766 }
767
768 _mesa_set_vp_override( ctx, TRUE );
769 _mesa_update_state( ctx );
770
771 st_validate_state(st);
772
773 if (format == GL_DEPTH_COMPONENT) {
774 ps = st->state.framebuffer.zsbuf;
775 stfp = make_fragment_shader_z(ctx->st);
776 stvp = st_make_passthrough_vertex_shader(ctx->st, GL_TRUE);
777 color = ctx->Current.RasterColor;
778 }
779 else {
780 ps = st->state.framebuffer.cbufs[0];
781 stfp = combined_drawpix_fragment_program(ctx);
782 stvp = st_make_passthrough_vertex_shader(ctx->st, GL_FALSE);
783 color = NULL;
784 }
785
786 bufferFormat = ps->format;
787
788 /* draw with textured quad */
789 {
790 struct pipe_texture *pt
791 = make_texture(ctx->st, width, height, format, type, unpack, pixels);
792 if (pt) {
793 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
794 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
795 pt, stvp, stfp, color, GL_FALSE);
796 pipe_texture_reference(&pt, NULL);
797 }
798 }
799
800 _mesa_set_vp_override( ctx, FALSE );
801 }
802
803
804
805 static void
806 copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
807 GLsizei width, GLsizei height,
808 GLint dstx, GLint dsty)
809 {
810 struct st_renderbuffer *rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
811 struct pipe_screen *screen = ctx->st->pipe->screen;
812 struct pipe_transfer *ptDraw;
813 ubyte *drawMap;
814 ubyte *buffer;
815 int i;
816
817 buffer = _mesa_malloc(width * height * sizeof(ubyte));
818 if (!buffer) {
819 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
820 return;
821 }
822
823 /* this will do stencil pixel transfer ops */
824 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
825 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
826 &ctx->DefaultPacking, buffer);
827
828 ptDraw = screen->get_tex_transfer(screen, rbDraw->texture, 0, 0, 0,
829 PIPE_TRANSFER_WRITE, dstx, dsty,
830 width, height);
831
832 assert(ptDraw->block.width == 1);
833 assert(ptDraw->block.height == 1);
834
835 /* map the stencil buffer */
836 drawMap = screen->transfer_map(screen, ptDraw);
837
838 /* draw */
839 /* XXX PixelZoom not handled yet */
840 for (i = 0; i < height; i++) {
841 ubyte *dst;
842 const ubyte *src;
843 int y;
844
845 y = i;
846
847 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
848 y = height - y - 1;
849 }
850
851 dst = drawMap + y * ptDraw->stride;
852 src = buffer + i * width;
853
854 switch (ptDraw->format) {
855 case PIPE_FORMAT_S8Z24_UNORM:
856 {
857 uint *dst4 = (uint *) dst;
858 int j;
859 for (j = 0; j < width; j++) {
860 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
861 dst4++;
862 }
863 }
864 break;
865 case PIPE_FORMAT_Z24S8_UNORM:
866 {
867 uint *dst4 = (uint *) dst;
868 int j;
869 for (j = 0; j < width; j++) {
870 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
871 dst4++;
872 }
873 }
874 break;
875 case PIPE_FORMAT_S8_UNORM:
876 memcpy(dst, src, width);
877 break;
878 default:
879 assert(0);
880 }
881 }
882
883 _mesa_free(buffer);
884
885 /* unmap the stencil buffer */
886 screen->transfer_unmap(screen, ptDraw);
887 screen->tex_transfer_destroy(ptDraw);
888 }
889
890
891 static void
892 st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
893 GLsizei width, GLsizei height,
894 GLint dstx, GLint dsty, GLenum type)
895 {
896 struct st_context *st = ctx->st;
897 struct pipe_context *pipe = st->pipe;
898 struct pipe_screen *screen = pipe->screen;
899 struct st_renderbuffer *rbRead;
900 struct st_vertex_program *stvp;
901 struct st_fragment_program *stfp;
902 struct pipe_texture *pt;
903 GLfloat *color;
904 enum pipe_format srcFormat, texFormat;
905
906 /* make sure rendering has completed */
907 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
908
909 st_validate_state(st);
910
911 if (type == GL_STENCIL) {
912 /* can't use texturing to do stencil */
913 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
914 return;
915 }
916
917 if (type == GL_COLOR) {
918 rbRead = st_get_color_read_renderbuffer(ctx);
919 color = NULL;
920 stfp = combined_drawpix_fragment_program(ctx);
921 stvp = st_make_passthrough_vertex_shader(ctx->st, GL_FALSE);
922 }
923 else {
924 assert(type == GL_DEPTH);
925 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
926 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
927 stfp = make_fragment_shader_z(ctx->st);
928 stvp = st_make_passthrough_vertex_shader(ctx->st, GL_TRUE);
929 }
930
931 srcFormat = rbRead->texture->format;
932
933 if (screen->is_format_supported(screen, srcFormat, PIPE_TEXTURE_2D,
934 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
935 texFormat = srcFormat;
936 }
937 else {
938 /* srcFormat can't be used as a texture format */
939 if (type == GL_DEPTH) {
940 texFormat = st_choose_format(pipe, GL_DEPTH_COMPONENT, PIPE_TEXTURE_2D,
941 PIPE_TEXTURE_USAGE_DEPTH_STENCIL);
942 assert(texFormat != PIPE_FORMAT_NONE); /* XXX no depth texture formats??? */
943 }
944 else {
945 /* default color format */
946 texFormat = st_choose_format(pipe, GL_RGBA, PIPE_TEXTURE_2D,
947 PIPE_TEXTURE_USAGE_SAMPLER);
948 assert(texFormat != PIPE_FORMAT_NONE);
949 }
950 }
951
952 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, texFormat, 0,
953 width, height, 1,
954 PIPE_TEXTURE_USAGE_SAMPLER);
955 if (!pt)
956 return;
957
958 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
959 srcy = ctx->DrawBuffer->Height - srcy - height;
960 }
961
962 if (srcFormat == texFormat) {
963 /* copy source framebuffer surface into mipmap/texture */
964 struct pipe_surface *psRead = screen->get_tex_surface(screen,
965 rbRead->texture, 0, 0, 0,
966 PIPE_BUFFER_USAGE_GPU_READ);
967 struct pipe_surface *psTex = screen->get_tex_surface(screen, pt, 0, 0, 0,
968 PIPE_BUFFER_USAGE_GPU_WRITE );
969 pipe->surface_copy(pipe,
970 psTex, /* dest */
971 0, 0, /* destx/y */
972 psRead,
973 srcx, srcy, width, height);
974 pipe_surface_reference(&psRead, NULL);
975 pipe_surface_reference(&psTex, NULL);
976 }
977 else {
978 /* CPU-based fallback/conversion */
979 struct pipe_transfer *ptRead =
980 screen->get_tex_transfer(screen, rbRead->texture, 0, 0, 0,
981 PIPE_TRANSFER_READ, srcx, srcy, width,
982 height);
983
984 struct pipe_transfer *ptTex =
985 screen->get_tex_transfer(screen, pt, 0, 0, 0, PIPE_TRANSFER_WRITE,
986 0, 0, width, height);
987
988 if (type == GL_COLOR) {
989 /* alternate path using get/put_tile() */
990 GLfloat *buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
991
992 pipe_get_tile_rgba(ptRead, 0, 0, width, height, buf);
993 pipe_put_tile_rgba(ptTex, 0, 0, width, height, buf);
994
995 _mesa_free(buf);
996 }
997 else {
998 /* GL_DEPTH */
999 GLuint *buf = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
1000 pipe_get_tile_z(ptRead, 0, 0, width, height, buf);
1001 pipe_put_tile_z(ptTex, 0, 0, width, height, buf);
1002 _mesa_free(buf);
1003 }
1004
1005 screen->tex_transfer_destroy(ptRead);
1006 screen->tex_transfer_destroy(ptTex);
1007 }
1008
1009 /* draw textured quad */
1010 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1011 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1012 pt, stvp, stfp, color, GL_TRUE);
1013
1014 pipe_texture_reference(&pt, NULL);
1015 }
1016
1017
1018
1019 void st_init_drawpixels_functions(struct dd_function_table *functions)
1020 {
1021 functions->DrawPixels = st_DrawPixels;
1022 functions->CopyPixels = st_CopyPixels;
1023 }
1024
1025
1026 void
1027 st_destroy_drawpix(struct st_context *st)
1028 {
1029 st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
1030 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1031 st_reference_vertprog(st, &st->drawpix.vert_shaders[0], NULL);
1032 st_reference_vertprog(st, &st->drawpix.vert_shaders[1], NULL);
1033 }