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