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