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