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