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