st/mesa: remove unused var
[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 "util/u_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 const GLfloat *color;
771
772 if (format == GL_STENCIL_INDEX ||
773 format == GL_DEPTH_STENCIL) {
774 draw_stencil_pixels(ctx, x, y, width, height, format, type,
775 unpack, pixels);
776 return;
777 }
778
779 /* Mesa state should be up to date by now */
780 assert(ctx->NewState == 0x0);
781
782 st_validate_state(st);
783
784 if (format == GL_DEPTH_COMPONENT) {
785 stfp = make_fragment_shader_z(st);
786 driver_vp = st_make_passthrough_vertex_shader(st, GL_TRUE);
787 color = ctx->Current.RasterColor;
788 }
789 else {
790 stfp = combined_drawpix_fragment_program(ctx);
791 driver_vp = st_make_passthrough_vertex_shader(st, GL_FALSE);
792 color = NULL;
793 }
794
795 /* draw with textured quad */
796 {
797 struct pipe_texture *pt
798 = make_texture(st, width, height, format, type, unpack, pixels);
799 if (pt) {
800 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
801 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
802 pt,
803 driver_vp,
804 stfp->driver_shader,
805 color, GL_FALSE);
806 pipe_texture_reference(&pt, NULL);
807 }
808 }
809 }
810
811
812
813 static void
814 copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
815 GLsizei width, GLsizei height,
816 GLint dstx, GLint dsty)
817 {
818 struct st_renderbuffer *rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
819 struct pipe_screen *screen = ctx->st->pipe->screen;
820 enum pipe_transfer_usage usage;
821 struct pipe_transfer *ptDraw;
822 ubyte *drawMap;
823 ubyte *buffer;
824 int i;
825
826 buffer = _mesa_malloc(width * height * sizeof(ubyte));
827 if (!buffer) {
828 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
829 return;
830 }
831
832 /* this will do stencil pixel transfer ops */
833 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
834 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
835 &ctx->DefaultPacking, buffer);
836
837 if(util_format_get_component_bits(rbDraw->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
838 usage = PIPE_TRANSFER_READ_WRITE;
839 else
840 usage = PIPE_TRANSFER_WRITE;
841
842 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
843 dsty = rbDraw->Base.Height - dsty - height;
844 }
845
846 ptDraw = st_cond_flush_get_tex_transfer(st_context(ctx),
847 rbDraw->texture, 0, 0, 0,
848 usage, dstx, dsty,
849 width, height);
850
851 assert(util_format_get_blockwidth(ptDraw->texture->format) == 1);
852 assert(util_format_get_blockheight(ptDraw->texture->format) == 1);
853
854 /* map the stencil buffer */
855 drawMap = screen->transfer_map(screen, ptDraw);
856
857 /* draw */
858 /* XXX PixelZoom not handled yet */
859 for (i = 0; i < height; i++) {
860 ubyte *dst;
861 const ubyte *src;
862 int y;
863
864 y = i;
865
866 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
867 y = height - y - 1;
868 }
869
870 dst = drawMap + y * ptDraw->stride;
871 src = buffer + i * width;
872
873 switch (ptDraw->texture->format) {
874 case PIPE_FORMAT_S8Z24_UNORM:
875 {
876 uint *dst4 = (uint *) dst;
877 int j;
878 assert(usage == PIPE_TRANSFER_READ_WRITE);
879 for (j = 0; j < width; j++) {
880 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
881 dst4++;
882 }
883 }
884 break;
885 case PIPE_FORMAT_Z24S8_UNORM:
886 {
887 uint *dst4 = (uint *) dst;
888 int j;
889 assert(usage == PIPE_TRANSFER_READ_WRITE);
890 for (j = 0; j < width; j++) {
891 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
892 dst4++;
893 }
894 }
895 break;
896 case PIPE_FORMAT_S8_UNORM:
897 assert(usage == PIPE_TRANSFER_WRITE);
898 memcpy(dst, src, width);
899 break;
900 default:
901 assert(0);
902 }
903 }
904
905 _mesa_free(buffer);
906
907 /* unmap the stencil buffer */
908 screen->transfer_unmap(screen, ptDraw);
909 screen->tex_transfer_destroy(ptDraw);
910 }
911
912
913 static void
914 st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
915 GLsizei width, GLsizei height,
916 GLint dstx, GLint dsty, GLenum type)
917 {
918 struct st_context *st = st_context(ctx);
919 struct pipe_context *pipe = st->pipe;
920 struct pipe_screen *screen = pipe->screen;
921 struct st_renderbuffer *rbRead;
922 void *driver_vp;
923 struct st_fragment_program *stfp;
924 struct pipe_texture *pt;
925 GLfloat *color;
926 enum pipe_format srcFormat, texFormat;
927 int ptw, pth;
928
929 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
930
931 st_validate_state(st);
932
933 if (srcx < 0) {
934 width -= -srcx;
935 dstx += -srcx;
936 srcx = 0;
937 }
938
939 if (srcy < 0) {
940 height -= -srcy;
941 dsty += -srcy;
942 srcy = 0;
943 }
944
945 if (dstx < 0) {
946 width -= -dstx;
947 srcx += -dstx;
948 dstx = 0;
949 }
950
951 if (dsty < 0) {
952 height -= -dsty;
953 srcy += -dsty;
954 dsty = 0;
955 }
956
957 if (width < 0 || height < 0)
958 return;
959
960
961 if (type == GL_STENCIL) {
962 /* can't use texturing to do stencil */
963 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
964 return;
965 }
966
967 if (type == GL_COLOR) {
968 rbRead = st_get_color_read_renderbuffer(ctx);
969 color = NULL;
970 stfp = combined_drawpix_fragment_program(ctx);
971 driver_vp = st_make_passthrough_vertex_shader(st, GL_FALSE);
972 }
973 else {
974 assert(type == GL_DEPTH);
975 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
976 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
977 stfp = make_fragment_shader_z(st);
978 driver_vp = st_make_passthrough_vertex_shader(st, GL_TRUE);
979 }
980
981 srcFormat = rbRead->texture->format;
982
983 if (screen->is_format_supported(screen, srcFormat, PIPE_TEXTURE_2D,
984 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
985 texFormat = srcFormat;
986 }
987 else {
988 /* srcFormat can't be used as a texture format */
989 if (type == GL_DEPTH) {
990 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
991 PIPE_TEXTURE_2D,
992 PIPE_TEXTURE_USAGE_DEPTH_STENCIL);
993 assert(texFormat != PIPE_FORMAT_NONE); /* XXX no depth texture formats??? */
994 }
995 else {
996 /* default color format */
997 texFormat = st_choose_format(screen, GL_RGBA, PIPE_TEXTURE_2D,
998 PIPE_TEXTURE_USAGE_SAMPLER);
999 assert(texFormat != PIPE_FORMAT_NONE);
1000 }
1001 }
1002
1003 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1004 srcy = ctx->DrawBuffer->Height - srcy - height;
1005
1006 if (srcy < 0) {
1007 height -= -srcy;
1008 srcy = 0;
1009 }
1010
1011 if (height < 0)
1012 return;
1013 }
1014
1015 /* Need to use POT texture? */
1016 ptw = width;
1017 pth = height;
1018 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
1019 int l2pt, maxSize;
1020
1021 l2pt = util_logbase2(width);
1022 if (1<<l2pt != width) {
1023 ptw = 1<<(l2pt+1);
1024 }
1025 l2pt = util_logbase2(height);
1026 if (1<<l2pt != height) {
1027 pth = 1<<(l2pt+1);
1028 }
1029
1030 /* Check against maximum texture size */
1031 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1032 assert(ptw <= maxSize);
1033 assert(pth <= maxSize);
1034 }
1035
1036 pt = st_texture_create(st, PIPE_TEXTURE_2D, texFormat, 0,
1037 ptw, pth, 1,
1038 PIPE_TEXTURE_USAGE_SAMPLER);
1039 if (!pt)
1040 return;
1041
1042
1043 if (srcFormat == texFormat) {
1044 /* copy source framebuffer surface into mipmap/texture */
1045 struct pipe_surface *psRead = screen->get_tex_surface(screen,
1046 rbRead->texture, 0, 0, 0,
1047 PIPE_BUFFER_USAGE_GPU_READ);
1048 struct pipe_surface *psTex = screen->get_tex_surface(screen, pt, 0, 0, 0,
1049 PIPE_BUFFER_USAGE_GPU_WRITE );
1050 if (pipe->surface_copy) {
1051 pipe->surface_copy(pipe,
1052 psTex, /* dest */
1053 0, 0, /* destx/y */
1054 psRead,
1055 srcx, srcy, width, height);
1056 } else {
1057 util_surface_copy(pipe, FALSE,
1058 psTex,
1059 0, 0,
1060 psRead,
1061 srcx, srcy, width, height);
1062 }
1063 pipe_surface_reference(&psRead, NULL);
1064 pipe_surface_reference(&psTex, NULL);
1065 }
1066 else {
1067 /* CPU-based fallback/conversion */
1068 struct pipe_transfer *ptRead =
1069 st_cond_flush_get_tex_transfer(st, rbRead->texture, 0, 0, 0,
1070 PIPE_TRANSFER_READ, srcx, srcy, width,
1071 height);
1072 struct pipe_transfer *ptTex;
1073 enum pipe_transfer_usage transfer_usage;
1074
1075 if (ST_DEBUG & DEBUG_FALLBACK)
1076 debug_printf("%s: fallback processing\n", __FUNCTION__);
1077
1078 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1079 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1080 else
1081 transfer_usage = PIPE_TRANSFER_WRITE;
1082
1083 ptTex = st_cond_flush_get_tex_transfer(st, pt, 0, 0, 0, transfer_usage,
1084 0, 0, width, height);
1085
1086 if (type == GL_COLOR) {
1087 /* alternate path using get/put_tile() */
1088 GLfloat *buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
1089
1090 pipe_get_tile_rgba(ptRead, 0, 0, width, height, buf);
1091 pipe_put_tile_rgba(ptTex, 0, 0, width, height, buf);
1092
1093 _mesa_free(buf);
1094 }
1095 else {
1096 /* GL_DEPTH */
1097 GLuint *buf = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
1098 pipe_get_tile_z(ptRead, 0, 0, width, height, buf);
1099 pipe_put_tile_z(ptTex, 0, 0, width, height, buf);
1100 _mesa_free(buf);
1101 }
1102
1103 screen->tex_transfer_destroy(ptRead);
1104 screen->tex_transfer_destroy(ptTex);
1105 }
1106
1107 /* draw textured quad */
1108 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1109 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1110 pt,
1111 driver_vp,
1112 stfp->driver_shader,
1113 color, GL_TRUE);
1114
1115 pipe_texture_reference(&pt, NULL);
1116 }
1117
1118
1119
1120 void st_init_drawpixels_functions(struct dd_function_table *functions)
1121 {
1122 functions->DrawPixels = st_DrawPixels;
1123 functions->CopyPixels = st_CopyPixels;
1124 }
1125
1126
1127 void
1128 st_destroy_drawpix(struct st_context *st)
1129 {
1130 st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
1131 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1132 if (st->drawpix.vert_shaders[0])
1133 free(st->drawpix.vert_shaders[0]);
1134 if (st->drawpix.vert_shaders[1])
1135 free(st->drawpix.vert_shaders[1]);
1136 }