Merge branch '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 * Make texture containing an image for glDrawPixels image.
297 * If 'pixels' is NULL, leave the texture image data undefined.
298 */
299 static struct pipe_texture *
300 make_texture(struct st_context *st,
301 GLsizei width, GLsizei height, GLenum format, GLenum type,
302 const struct gl_pixelstore_attrib *unpack,
303 const GLvoid *pixels)
304 {
305 GLcontext *ctx = st->ctx;
306 struct pipe_context *pipe = st->pipe;
307 struct pipe_screen *screen = pipe->screen;
308 gl_format mformat;
309 struct pipe_texture *pt;
310 enum pipe_format pipeFormat;
311 GLuint cpp;
312 GLenum baseFormat;
313 int ptw, pth;
314
315 baseFormat = base_format(format);
316
317 mformat = st_ChooseTextureFormat(ctx, baseFormat, format, type);
318 assert(mformat);
319
320 pipeFormat = st_mesa_format_to_pipe_format(mformat);
321 assert(pipeFormat);
322 cpp = util_format_get_blocksize(pipeFormat);
323
324 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
325 if (!pixels)
326 return NULL;
327
328 /* Need to use POT texture? */
329 ptw = width;
330 pth = height;
331 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
332 int l2pt, maxSize;
333
334 l2pt = util_logbase2(width);
335 if (1<<l2pt != width) {
336 ptw = 1<<(l2pt+1);
337 }
338 l2pt = util_logbase2(height);
339 if (1<<l2pt != height) {
340 pth = 1<<(l2pt+1);
341 }
342
343 /* Check against maximum texture size */
344 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
345 assert(ptw <= maxSize);
346 assert(pth <= maxSize);
347 }
348
349 pt = st_texture_create(st, PIPE_TEXTURE_2D, pipeFormat, 0, ptw, pth, 1,
350 PIPE_TEXTURE_USAGE_SAMPLER);
351 if (!pt) {
352 _mesa_unmap_pbo_source(ctx, unpack);
353 return NULL;
354 }
355
356 {
357 struct pipe_transfer *transfer;
358 static const GLuint dstImageOffsets = 0;
359 GLboolean success;
360 GLubyte *dest;
361 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
362
363 /* we'll do pixel transfer in a fragment shader */
364 ctx->_ImageTransferState = 0x0;
365
366 transfer = st_no_flush_get_tex_transfer(st, pt, 0, 0, 0,
367 PIPE_TRANSFER_WRITE, 0, 0,
368 width, height);
369
370 /* map texture transfer */
371 dest = screen->transfer_map(screen, transfer);
372
373
374 /* Put image into texture transfer.
375 * Note that the image is actually going to be upside down in
376 * the texture. We deal with that with texcoords.
377 */
378 success = _mesa_texstore(ctx, 2, /* dims */
379 baseFormat, /* baseInternalFormat */
380 mformat, /* gl_format */
381 dest, /* dest */
382 0, 0, 0, /* dstX/Y/Zoffset */
383 transfer->stride, /* dstRowStride, bytes */
384 &dstImageOffsets, /* dstImageOffsets */
385 width, height, 1, /* size */
386 format, type, /* src format/type */
387 pixels, /* data source */
388 unpack);
389
390 /* unmap */
391 screen->transfer_unmap(screen, transfer);
392 screen->tex_transfer_destroy(transfer);
393
394 assert(success);
395
396 /* restore */
397 ctx->_ImageTransferState = imageTransferStateSave;
398 }
399
400 _mesa_unmap_pbo_source(ctx, unpack);
401
402 return pt;
403 }
404
405
406 /**
407 * Draw quad with texcoords and optional color.
408 * Coords are gallium window coords with y=0=top.
409 * \param color may be null
410 * \param invertTex if true, flip texcoords vertically
411 */
412 static void
413 draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
414 GLfloat x1, GLfloat y1, const GLfloat *color,
415 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
416 {
417 struct st_context *st = st_context(ctx);
418 struct pipe_context *pipe = st->pipe;
419 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
420
421 /* setup vertex data */
422 {
423 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
424 const GLfloat fb_width = (GLfloat) fb->Width;
425 const GLfloat fb_height = (GLfloat) fb->Height;
426 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
427 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
428 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
429 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
430 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
431 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
432 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
433 GLuint tex, i;
434
435 /* upper-left */
436 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
437 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
438
439 /* upper-right */
440 verts[1][0][0] = clip_x1;
441 verts[1][0][1] = clip_y0;
442
443 /* lower-right */
444 verts[2][0][0] = clip_x1;
445 verts[2][0][1] = clip_y1;
446
447 /* lower-left */
448 verts[3][0][0] = clip_x0;
449 verts[3][0][1] = clip_y1;
450
451 tex = color ? 2 : 1;
452 verts[0][tex][0] = sLeft; /* v[0].attr[tex].s */
453 verts[0][tex][1] = tTop; /* v[0].attr[tex].t */
454 verts[1][tex][0] = sRight;
455 verts[1][tex][1] = tTop;
456 verts[2][tex][0] = sRight;
457 verts[2][tex][1] = tBot;
458 verts[3][tex][0] = sLeft;
459 verts[3][tex][1] = tBot;
460
461 /* same for all verts: */
462 if (color) {
463 for (i = 0; i < 4; i++) {
464 verts[i][0][2] = z; /*Z*/
465 verts[i][0][3] = 1.0f; /*W*/
466 verts[i][1][0] = color[0];
467 verts[i][1][1] = color[1];
468 verts[i][1][2] = color[2];
469 verts[i][1][3] = color[3];
470 verts[i][2][2] = 0.0f; /*R*/
471 verts[i][2][3] = 1.0f; /*Q*/
472 }
473 }
474 else {
475 for (i = 0; i < 4; i++) {
476 verts[i][0][2] = z; /*Z*/
477 verts[i][0][3] = 1.0f; /*W*/
478 verts[i][1][2] = 0.0f; /*R*/
479 verts[i][1][3] = 1.0f; /*Q*/
480 }
481 }
482 }
483
484 {
485 struct pipe_buffer *buf;
486
487 /* allocate/load buffer object with vertex data */
488 buf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
489 sizeof(verts));
490 st_no_flush_pipe_buffer_write(st, buf, 0, sizeof(verts), verts);
491
492 util_draw_vertex_buffer(pipe, buf, 0,
493 PIPE_PRIM_QUADS,
494 4, /* verts */
495 3); /* attribs/vert */
496 pipe_buffer_reference(&buf, NULL);
497 }
498 }
499
500
501
502 static void
503 draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
504 GLsizei width, GLsizei height,
505 GLfloat zoomX, GLfloat zoomY,
506 struct pipe_texture *pt,
507 void *driver_vp,
508 void *driver_fp,
509 const GLfloat *color,
510 GLboolean invertTex)
511 {
512 struct st_context *st = st_context(ctx);
513 struct pipe_context *pipe = st->pipe;
514 struct cso_context *cso = st->cso_context;
515 GLfloat x0, y0, x1, y1;
516 GLsizei maxSize;
517
518 /* limit checks */
519 /* XXX if DrawPixels image is larger than max texture size, break
520 * it up into chunks.
521 */
522 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
523 assert(width <= maxSize);
524 assert(height <= maxSize);
525
526 cso_save_rasterizer(cso);
527 cso_save_viewport(cso);
528 cso_save_samplers(cso);
529 cso_save_sampler_textures(cso);
530 cso_save_fragment_shader(cso);
531 cso_save_vertex_shader(cso);
532 cso_save_vertex_elements(cso);
533
534 /* rasterizer state: just scissor */
535 {
536 struct pipe_rasterizer_state rasterizer;
537 memset(&rasterizer, 0, sizeof(rasterizer));
538 rasterizer.gl_rasterization_rules = 1;
539 rasterizer.scissor = ctx->Scissor.Enabled;
540 cso_set_rasterizer(cso, &rasterizer);
541 }
542
543 /* fragment shader state: TEX lookup program */
544 cso_set_fragment_shader_handle(cso, driver_fp);
545
546 /* vertex shader state: position + texcoord pass-through */
547 cso_set_vertex_shader_handle(cso, driver_vp);
548
549
550 /* texture sampling state: */
551 {
552 struct pipe_sampler_state sampler;
553 memset(&sampler, 0, sizeof(sampler));
554 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
555 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
556 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
557 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
558 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
559 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
560 sampler.normalized_coords = 1;
561
562 cso_single_sampler(cso, 0, &sampler);
563 if (st->pixel_xfer.pixelmap_enabled) {
564 cso_single_sampler(cso, 1, &sampler);
565 }
566 cso_single_sampler_done(cso);
567 }
568
569 /* viewport state: viewport matching window dims */
570 {
571 const float w = (float) ctx->DrawBuffer->Width;
572 const float h = (float) ctx->DrawBuffer->Height;
573 struct pipe_viewport_state vp;
574 vp.scale[0] = 0.5f * w;
575 vp.scale[1] = -0.5f * h;
576 vp.scale[2] = 0.5f;
577 vp.scale[3] = 1.0f;
578 vp.translate[0] = 0.5f * w;
579 vp.translate[1] = 0.5f * h;
580 vp.translate[2] = 0.5f;
581 vp.translate[3] = 0.0f;
582 cso_set_viewport(cso, &vp);
583 }
584
585 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
586
587 /* texture state: */
588 if (st->pixel_xfer.pixelmap_enabled) {
589 struct pipe_texture *textures[2];
590 textures[0] = pt;
591 textures[1] = st->pixel_xfer.pixelmap_texture;
592 pipe->set_fragment_sampler_textures(pipe, 2, textures);
593 }
594 else {
595 pipe->set_fragment_sampler_textures(pipe, 1, &pt);
596 }
597
598 /* Compute Gallium window coords (y=0=top) with pixel zoom.
599 * Recall that these coords are transformed by the current
600 * vertex shader and viewport transformation.
601 */
602 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
603 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
604 invertTex = !invertTex;
605 }
606
607 x0 = (GLfloat) x;
608 x1 = x + width * ctx->Pixel.ZoomX;
609 y0 = (GLfloat) y;
610 y1 = y + height * ctx->Pixel.ZoomY;
611
612 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
613 z = z * 2.0 - 1.0;
614
615 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
616 (GLfloat) width / pt->width0,
617 (GLfloat) height / pt->height0);
618
619 /* restore state */
620 cso_restore_rasterizer(cso);
621 cso_restore_viewport(cso);
622 cso_restore_samplers(cso);
623 cso_restore_sampler_textures(cso);
624 cso_restore_fragment_shader(cso);
625 cso_restore_vertex_shader(cso);
626 cso_restore_vertex_elements(cso);
627 }
628
629
630 static void
631 draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
632 GLsizei width, GLsizei height, GLenum format, GLenum type,
633 const struct gl_pixelstore_attrib *unpack,
634 const GLvoid *pixels)
635 {
636 struct st_context *st = st_context(ctx);
637 struct pipe_context *pipe = st->pipe;
638 struct pipe_screen *screen = pipe->screen;
639 struct st_renderbuffer *strb;
640 enum pipe_transfer_usage usage;
641 struct pipe_transfer *pt;
642 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
643 GLint skipPixels;
644 ubyte *stmap;
645 struct gl_pixelstore_attrib clippedUnpack = *unpack;
646
647 if (!zoom) {
648 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
649 &clippedUnpack)) {
650 /* totally clipped */
651 return;
652 }
653 }
654
655 strb = st_renderbuffer(ctx->DrawBuffer->
656 Attachment[BUFFER_STENCIL].Renderbuffer);
657
658 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
659 y = ctx->DrawBuffer->Height - y - height;
660 }
661
662 if(format != GL_DEPTH_STENCIL &&
663 util_format_get_component_bits(strb->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
664 usage = PIPE_TRANSFER_READ_WRITE;
665 else
666 usage = PIPE_TRANSFER_WRITE;
667
668 pt = st_cond_flush_get_tex_transfer(st_context(ctx), strb->texture, 0, 0, 0,
669 usage, x, y,
670 width, height);
671
672 stmap = screen->transfer_map(screen, pt);
673
674 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
675 assert(pixels);
676
677 /* if width > MAX_WIDTH, have to process image in chunks */
678 skipPixels = 0;
679 while (skipPixels < width) {
680 const GLint spanX = skipPixels;
681 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
682 GLint row;
683 for (row = 0; row < height; row++) {
684 GLubyte sValues[MAX_WIDTH];
685 GLuint zValues[MAX_WIDTH];
686 GLenum destType = GL_UNSIGNED_BYTE;
687 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
688 width, height,
689 format, type,
690 row, skipPixels);
691 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
692 type, source, &clippedUnpack,
693 ctx->_ImageTransferState);
694
695 if (format == GL_DEPTH_STENCIL) {
696 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
697 (1 << 24) - 1, type, source,
698 &clippedUnpack);
699 }
700
701 if (zoom) {
702 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
703 "zoom not complete");
704 }
705
706 {
707 GLint spanY;
708
709 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
710 spanY = height - row - 1;
711 }
712 else {
713 spanY = row;
714 }
715
716 /* now pack the stencil (and Z) values in the dest format */
717 switch (pt->texture->format) {
718 case PIPE_FORMAT_S8_UNORM:
719 {
720 ubyte *dest = stmap + spanY * pt->stride + spanX;
721 assert(usage == PIPE_TRANSFER_WRITE);
722 memcpy(dest, sValues, spanWidth);
723 }
724 break;
725 case PIPE_FORMAT_Z24S8_UNORM:
726 if (format == GL_DEPTH_STENCIL) {
727 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
728 GLint k;
729 assert(usage == PIPE_TRANSFER_WRITE);
730 for (k = 0; k < spanWidth; k++) {
731 dest[k] = zValues[k] | (sValues[k] << 24);
732 }
733 }
734 else {
735 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
736 GLint k;
737 assert(usage == PIPE_TRANSFER_READ_WRITE);
738 for (k = 0; k < spanWidth; k++) {
739 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
740 }
741 }
742 break;
743 case PIPE_FORMAT_S8Z24_UNORM:
744 if (format == GL_DEPTH_STENCIL) {
745 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
746 GLint k;
747 assert(usage == PIPE_TRANSFER_WRITE);
748 for (k = 0; k < spanWidth; k++) {
749 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
750 }
751 }
752 else {
753 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
754 GLint k;
755 assert(usage == PIPE_TRANSFER_READ_WRITE);
756 for (k = 0; k < spanWidth; k++) {
757 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
758 }
759 }
760 break;
761 default:
762 assert(0);
763 }
764 }
765 }
766 skipPixels += spanWidth;
767 }
768
769 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
770
771 /* unmap the stencil buffer */
772 screen->transfer_unmap(screen, pt);
773 screen->tex_transfer_destroy(pt);
774 }
775
776
777 /**
778 * Called via ctx->Driver.DrawPixels()
779 */
780 static void
781 st_DrawPixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
782 GLenum format, GLenum type,
783 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
784 {
785 void *driver_vp, *driver_fp;
786 struct st_context *st = st_context(ctx);
787 const GLfloat *color;
788
789 if (format == GL_STENCIL_INDEX ||
790 format == GL_DEPTH_STENCIL) {
791 draw_stencil_pixels(ctx, x, y, width, height, format, type,
792 unpack, pixels);
793 return;
794 }
795
796 /* Mesa state should be up to date by now */
797 assert(ctx->NewState == 0x0);
798
799 st_validate_state(st);
800
801 if (format == GL_DEPTH_COMPONENT) {
802 driver_fp = make_fragment_shader_z(st);
803 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
804 color = ctx->Current.RasterColor;
805 }
806 else {
807 driver_fp = combined_drawpix_fragment_program(ctx);
808 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
809 color = NULL;
810 }
811
812 /* draw with textured quad */
813 {
814 struct pipe_texture *pt
815 = make_texture(st, width, height, format, type, unpack, pixels);
816 if (pt) {
817 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
818 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
819 pt,
820 driver_vp,
821 driver_fp,
822 color, GL_FALSE);
823 pipe_texture_reference(&pt, NULL);
824 }
825 }
826 }
827
828
829
830 static void
831 copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
832 GLsizei width, GLsizei height,
833 GLint dstx, GLint dsty)
834 {
835 struct st_renderbuffer *rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
836 struct pipe_screen *screen = ctx->st->pipe->screen;
837 enum pipe_transfer_usage usage;
838 struct pipe_transfer *ptDraw;
839 ubyte *drawMap;
840 ubyte *buffer;
841 int i;
842
843 buffer = malloc(width * height * sizeof(ubyte));
844 if (!buffer) {
845 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
846 return;
847 }
848
849 /* this will do stencil pixel transfer ops */
850 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
851 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
852 &ctx->DefaultPacking, buffer);
853
854 if(util_format_get_component_bits(rbDraw->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
855 usage = PIPE_TRANSFER_READ_WRITE;
856 else
857 usage = PIPE_TRANSFER_WRITE;
858
859 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
860 dsty = rbDraw->Base.Height - dsty - height;
861 }
862
863 ptDraw = st_cond_flush_get_tex_transfer(st_context(ctx),
864 rbDraw->texture, 0, 0, 0,
865 usage, dstx, dsty,
866 width, height);
867
868 assert(util_format_get_blockwidth(ptDraw->texture->format) == 1);
869 assert(util_format_get_blockheight(ptDraw->texture->format) == 1);
870
871 /* map the stencil buffer */
872 drawMap = screen->transfer_map(screen, ptDraw);
873
874 /* draw */
875 /* XXX PixelZoom not handled yet */
876 for (i = 0; i < height; i++) {
877 ubyte *dst;
878 const ubyte *src;
879 int y;
880
881 y = i;
882
883 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
884 y = height - y - 1;
885 }
886
887 dst = drawMap + y * ptDraw->stride;
888 src = buffer + i * width;
889
890 switch (ptDraw->texture->format) {
891 case PIPE_FORMAT_Z24S8_UNORM:
892 {
893 uint *dst4 = (uint *) dst;
894 int j;
895 assert(usage == PIPE_TRANSFER_READ_WRITE);
896 for (j = 0; j < width; j++) {
897 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
898 dst4++;
899 }
900 }
901 break;
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 & 0xffffff00) | (src[j] & 0xff);
909 dst4++;
910 }
911 }
912 break;
913 case PIPE_FORMAT_S8_UNORM:
914 assert(usage == PIPE_TRANSFER_WRITE);
915 memcpy(dst, src, width);
916 break;
917 default:
918 assert(0);
919 }
920 }
921
922 free(buffer);
923
924 /* unmap the stencil buffer */
925 screen->transfer_unmap(screen, ptDraw);
926 screen->tex_transfer_destroy(ptDraw);
927 }
928
929
930 static void
931 st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
932 GLsizei width, GLsizei height,
933 GLint dstx, GLint dsty, GLenum type)
934 {
935 struct st_context *st = st_context(ctx);
936 struct pipe_context *pipe = st->pipe;
937 struct pipe_screen *screen = pipe->screen;
938 struct st_renderbuffer *rbRead;
939 void *driver_vp, *driver_fp;
940 struct pipe_texture *pt;
941 GLfloat *color;
942 enum pipe_format srcFormat, texFormat;
943 int ptw, pth;
944 GLboolean invertTex = GL_FALSE;
945
946 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
947
948 st_validate_state(st);
949
950 if (srcx < 0) {
951 width -= -srcx;
952 dstx += -srcx;
953 srcx = 0;
954 }
955
956 if (srcy < 0) {
957 height -= -srcy;
958 dsty += -srcy;
959 srcy = 0;
960 }
961
962 if (dstx < 0) {
963 width -= -dstx;
964 srcx += -dstx;
965 dstx = 0;
966 }
967
968 if (dsty < 0) {
969 height -= -dsty;
970 srcy += -dsty;
971 dsty = 0;
972 }
973
974 if (width < 0 || height < 0)
975 return;
976
977
978 if (type == GL_STENCIL) {
979 /* can't use texturing to do stencil */
980 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
981 return;
982 }
983
984 if (type == GL_COLOR) {
985 rbRead = st_get_color_read_renderbuffer(ctx);
986 color = NULL;
987 driver_fp = combined_drawpix_fragment_program(ctx);
988 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
989 }
990 else {
991 assert(type == GL_DEPTH);
992 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
993 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
994 driver_fp = make_fragment_shader_z(st);
995 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
996 }
997
998 srcFormat = rbRead->texture->format;
999
1000 if (screen->is_format_supported(screen, srcFormat, PIPE_TEXTURE_2D,
1001 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
1002 texFormat = srcFormat;
1003 }
1004 else {
1005 /* srcFormat can't be used as a texture format */
1006 if (type == GL_DEPTH) {
1007 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1008 PIPE_TEXTURE_2D,
1009 PIPE_TEXTURE_USAGE_DEPTH_STENCIL);
1010 assert(texFormat != PIPE_FORMAT_NONE); /* XXX no depth texture formats??? */
1011 }
1012 else {
1013 /* default color format */
1014 texFormat = st_choose_format(screen, GL_RGBA, PIPE_TEXTURE_2D,
1015 PIPE_TEXTURE_USAGE_SAMPLER);
1016 assert(texFormat != PIPE_FORMAT_NONE);
1017 }
1018 }
1019
1020 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1021 srcy = ctx->ReadBuffer->Height - srcy - height;
1022
1023 if (srcy < 0) {
1024 height -= -srcy;
1025 srcy = 0;
1026 }
1027
1028 if (height < 0)
1029 return;
1030
1031 invertTex = !invertTex;
1032 }
1033
1034 /* Need to use POT texture? */
1035 ptw = width;
1036 pth = height;
1037 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
1038 int l2pt, maxSize;
1039
1040 l2pt = util_logbase2(width);
1041 if (1<<l2pt != width) {
1042 ptw = 1<<(l2pt+1);
1043 }
1044 l2pt = util_logbase2(height);
1045 if (1<<l2pt != height) {
1046 pth = 1<<(l2pt+1);
1047 }
1048
1049 /* Check against maximum texture size */
1050 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1051 assert(ptw <= maxSize);
1052 assert(pth <= maxSize);
1053 }
1054
1055 pt = st_texture_create(st, PIPE_TEXTURE_2D, texFormat, 0,
1056 ptw, pth, 1,
1057 PIPE_TEXTURE_USAGE_SAMPLER);
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 }