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