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