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