mesa: Remove unsused local variable.
[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 "program/program.h"
40 #include "program/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
53 #include "pipe/p_context.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_inlines.h"
56 #include "tgsi/tgsi_ureg.h"
57 #include "util/u_tile.h"
58 #include "util/u_draw_quad.h"
59 #include "util/u_format.h"
60 #include "util/u_math.h"
61 #include "program/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
63
64
65 #if FEATURE_drawpix
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.attr[1]; */
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.attr[2]; */
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_resource *
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_resource *pt;
306
307 pt = st_texture_create(st, st->internal_target, texFormat, 0,
308 width, height, 1, PIPE_BIND_SAMPLER_VIEW);
309
310 return pt;
311 }
312
313
314 /**
315 * Make texture containing an image for glDrawPixels image.
316 * If 'pixels' is NULL, leave the texture image data undefined.
317 */
318 static struct pipe_resource *
319 make_texture(struct st_context *st,
320 GLsizei width, GLsizei height, GLenum format, GLenum type,
321 const struct gl_pixelstore_attrib *unpack,
322 const GLvoid *pixels)
323 {
324 GLcontext *ctx = st->ctx;
325 struct pipe_context *pipe = st->pipe;
326 gl_format mformat;
327 struct pipe_resource *pt;
328 enum pipe_format pipeFormat;
329 GLuint cpp;
330 GLenum baseFormat;
331
332 baseFormat = base_format(format);
333
334 mformat = st_ChooseTextureFormat(ctx, baseFormat, format, type);
335 assert(mformat);
336
337 pipeFormat = st_mesa_format_to_pipe_format(mformat);
338 assert(pipeFormat);
339 cpp = util_format_get_blocksize(pipeFormat);
340
341 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
342 if (!pixels)
343 return NULL;
344
345 /* alloc temporary texture */
346 pt = alloc_texture(st, width, height, pipeFormat);
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 = pipe_get_transfer(st->pipe, pt, 0, 0, 0,
363 PIPE_TRANSFER_WRITE, 0, 0,
364 width, height);
365
366 /* map texture transfer */
367 dest = pipe_transfer_map(pipe, 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 pipe_transfer_unmap(pipe, transfer);
388 pipe->transfer_destroy(pipe, 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 gallium window coords with y=0=top.
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 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 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
448 verts[0][1][1] = tTop; /* v[0].attr[1].T */
449 verts[1][1][0] = sRight;
450 verts[1][1][1] = tTop;
451 verts[2][1][0] = sRight;
452 verts[2][1][1] = tBot;
453 verts[3][1][0] = sLeft;
454 verts[3][1][1] = tBot;
455
456 /* same for all verts: */
457 if (color) {
458 for (i = 0; i < 4; i++) {
459 verts[i][0][2] = z; /* v[i].attr[0].z */
460 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
461 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
462 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
463 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
464 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
465 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
466 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
467 }
468 }
469 else {
470 for (i = 0; i < 4; i++) {
471 verts[i][0][2] = z; /*Z*/
472 verts[i][0][3] = 1.0f; /*W*/
473 verts[i][1][2] = 0.0f; /*R*/
474 verts[i][1][3] = 1.0f; /*Q*/
475 }
476 }
477 }
478
479 {
480 struct pipe_resource *buf;
481
482 /* allocate/load buffer object with vertex data */
483 buf = pipe_buffer_create(pipe->screen,
484 PIPE_BIND_VERTEX_BUFFER,
485 sizeof(verts));
486 pipe_buffer_write(st->pipe, 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_resource_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_sampler_view *sv,
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 boolean normalized = sv->texture->target != PIPE_TEXTURE_RECT;
514
515 /* limit checks */
516 /* XXX if DrawPixels image is larger than max texture size, break
517 * it up into chunks.
518 */
519 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
520 assert(width <= maxSize);
521 assert(height <= maxSize);
522
523 cso_save_rasterizer(cso);
524 cso_save_viewport(cso);
525 cso_save_samplers(cso);
526 cso_save_fragment_sampler_views(cso);
527 cso_save_fragment_shader(cso);
528 cso_save_vertex_shader(cso);
529 cso_save_vertex_elements(cso);
530
531 /* rasterizer state: just scissor */
532 {
533 struct pipe_rasterizer_state rasterizer;
534 memset(&rasterizer, 0, sizeof(rasterizer));
535 rasterizer.gl_rasterization_rules = 1;
536 rasterizer.scissor = ctx->Scissor.Enabled;
537 cso_set_rasterizer(cso, &rasterizer);
538 }
539
540 /* fragment shader state: TEX lookup program */
541 cso_set_fragment_shader_handle(cso, driver_fp);
542
543 /* vertex shader state: position + texcoord pass-through */
544 cso_set_vertex_shader_handle(cso, driver_vp);
545
546
547 /* texture sampling state: */
548 {
549 struct pipe_sampler_state sampler;
550 memset(&sampler, 0, sizeof(sampler));
551 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
552 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
553 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
554 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
555 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
556 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
557 sampler.normalized_coords = normalized;
558
559 cso_single_sampler(cso, 0, &sampler);
560 if (st->pixel_xfer.pixelmap_enabled) {
561 cso_single_sampler(cso, 1, &sampler);
562 }
563 cso_single_sampler_done(cso);
564 }
565
566 /* viewport state: viewport matching window dims */
567 {
568 const float w = (float) ctx->DrawBuffer->Width;
569 const float h = (float) ctx->DrawBuffer->Height;
570 struct pipe_viewport_state vp;
571 vp.scale[0] = 0.5f * w;
572 vp.scale[1] = -0.5f * h;
573 vp.scale[2] = 0.5f;
574 vp.scale[3] = 1.0f;
575 vp.translate[0] = 0.5f * w;
576 vp.translate[1] = 0.5f * h;
577 vp.translate[2] = 0.5f;
578 vp.translate[3] = 0.0f;
579 cso_set_viewport(cso, &vp);
580 }
581
582 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
583
584 /* texture state: */
585 if (st->pixel_xfer.pixelmap_enabled) {
586 struct pipe_sampler_view *sampler_views[2];
587 sampler_views[0] = sv;
588 sampler_views[1] = st->pixel_xfer.pixelmap_sampler_view;
589 cso_set_fragment_sampler_views(cso, 2, sampler_views);
590 }
591 else {
592 cso_set_fragment_sampler_views(cso, 1, &sv);
593 }
594
595 /* Compute Gallium window coords (y=0=top) with pixel zoom.
596 * Recall that these coords are transformed by the current
597 * vertex shader and viewport transformation.
598 */
599 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
600 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
601 invertTex = !invertTex;
602 }
603
604 x0 = (GLfloat) x;
605 x1 = x + width * ctx->Pixel.ZoomX;
606 y0 = (GLfloat) y;
607 y1 = y + height * ctx->Pixel.ZoomY;
608
609 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
610 z = z * 2.0 - 1.0;
611
612 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
613 normalized ? ((GLfloat) width / sv->texture->width0) : (GLfloat)width,
614 normalized ? ((GLfloat) height / sv->texture->height0) : (GLfloat)height);
615
616 /* restore state */
617 cso_restore_rasterizer(cso);
618 cso_restore_viewport(cso);
619 cso_restore_samplers(cso);
620 cso_restore_fragment_sampler_views(cso);
621 cso_restore_fragment_shader(cso);
622 cso_restore_vertex_shader(cso);
623 cso_restore_vertex_elements(cso);
624 }
625
626
627 static void
628 draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
629 GLsizei width, GLsizei height, GLenum format, GLenum type,
630 const struct gl_pixelstore_attrib *unpack,
631 const GLvoid *pixels)
632 {
633 struct st_context *st = st_context(ctx);
634 struct pipe_context *pipe = st->pipe;
635 struct st_renderbuffer *strb;
636 enum pipe_transfer_usage usage;
637 struct pipe_transfer *pt;
638 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
639 GLint skipPixels;
640 ubyte *stmap;
641 struct gl_pixelstore_attrib clippedUnpack = *unpack;
642
643 if (!zoom) {
644 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
645 &clippedUnpack)) {
646 /* totally clipped */
647 return;
648 }
649 }
650
651 strb = st_renderbuffer(ctx->DrawBuffer->
652 Attachment[BUFFER_STENCIL].Renderbuffer);
653
654 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
655 y = ctx->DrawBuffer->Height - y - height;
656 }
657
658 if(format != GL_DEPTH_STENCIL &&
659 util_format_get_component_bits(strb->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
660 usage = PIPE_TRANSFER_READ_WRITE;
661 else
662 usage = PIPE_TRANSFER_WRITE;
663
664 pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0, 0,
665 usage, x, y,
666 width, height);
667
668 stmap = pipe_transfer_map(pipe, pt);
669
670 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
671 assert(pixels);
672
673 /* if width > MAX_WIDTH, have to process image in chunks */
674 skipPixels = 0;
675 while (skipPixels < width) {
676 const GLint spanX = skipPixels;
677 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
678 GLint row;
679 for (row = 0; row < height; row++) {
680 GLubyte sValues[MAX_WIDTH];
681 GLuint zValues[MAX_WIDTH];
682 GLenum destType = GL_UNSIGNED_BYTE;
683 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
684 width, height,
685 format, type,
686 row, skipPixels);
687 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
688 type, source, &clippedUnpack,
689 ctx->_ImageTransferState);
690
691 if (format == GL_DEPTH_STENCIL) {
692 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
693 (1 << 24) - 1, type, source,
694 &clippedUnpack);
695 }
696
697 if (zoom) {
698 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
699 "zoom not complete");
700 }
701
702 {
703 GLint spanY;
704
705 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
706 spanY = height - row - 1;
707 }
708 else {
709 spanY = row;
710 }
711
712 /* now pack the stencil (and Z) values in the dest format */
713 switch (pt->resource->format) {
714 case PIPE_FORMAT_S8_USCALED:
715 {
716 ubyte *dest = stmap + spanY * pt->stride + spanX;
717 assert(usage == PIPE_TRANSFER_WRITE);
718 memcpy(dest, sValues, spanWidth);
719 }
720 break;
721 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
722 if (format == GL_DEPTH_STENCIL) {
723 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
724 GLint k;
725 assert(usage == PIPE_TRANSFER_WRITE);
726 for (k = 0; k < spanWidth; k++) {
727 dest[k] = zValues[k] | (sValues[k] << 24);
728 }
729 }
730 else {
731 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
732 GLint k;
733 assert(usage == PIPE_TRANSFER_READ_WRITE);
734 for (k = 0; k < spanWidth; k++) {
735 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
736 }
737 }
738 break;
739 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
740 if (format == GL_DEPTH_STENCIL) {
741 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
742 GLint k;
743 assert(usage == PIPE_TRANSFER_WRITE);
744 for (k = 0; k < spanWidth; k++) {
745 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
746 }
747 }
748 else {
749 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
750 GLint k;
751 assert(usage == PIPE_TRANSFER_READ_WRITE);
752 for (k = 0; k < spanWidth; k++) {
753 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
754 }
755 }
756 break;
757 default:
758 assert(0);
759 }
760 }
761 }
762 skipPixels += spanWidth;
763 }
764
765 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
766
767 /* unmap the stencil buffer */
768 pipe_transfer_unmap(pipe, pt);
769 pipe->transfer_destroy(pipe, pt);
770 }
771
772
773 /**
774 * Called via ctx->Driver.DrawPixels()
775 */
776 static void
777 st_DrawPixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
778 GLenum format, GLenum type,
779 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
780 {
781 void *driver_vp, *driver_fp;
782 struct st_context *st = st_context(ctx);
783 const GLfloat *color;
784
785 if (format == GL_STENCIL_INDEX ||
786 format == GL_DEPTH_STENCIL) {
787 draw_stencil_pixels(ctx, x, y, width, height, format, type,
788 unpack, pixels);
789 return;
790 }
791
792 /* Mesa state should be up to date by now */
793 assert(ctx->NewState == 0x0);
794
795 st_validate_state(st);
796
797 if (format == GL_DEPTH_COMPONENT) {
798 driver_fp = make_fragment_shader_z(st);
799 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
800 color = ctx->Current.RasterColor;
801 }
802 else {
803 driver_fp = combined_drawpix_fragment_program(ctx);
804 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
805 color = NULL;
806 }
807
808 /* draw with textured quad */
809 {
810 struct pipe_resource *pt
811 = make_texture(st, width, height, format, type, unpack, pixels);
812 if (pt) {
813 struct pipe_sampler_view *sv = st_create_texture_sampler_view(st->pipe, pt);
814
815 if (sv) {
816 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
817 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
818 sv,
819 driver_vp,
820 driver_fp,
821 color, GL_FALSE);
822 pipe_sampler_view_reference(&sv, NULL);
823 }
824 pipe_resource_reference(&pt, NULL);
825 }
826 }
827 }
828
829
830
831 static void
832 copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
833 GLsizei width, GLsizei height,
834 GLint dstx, GLint dsty)
835 {
836 struct st_renderbuffer *rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
837 struct pipe_context *pipe = st_context(ctx)->pipe;
838 enum pipe_transfer_usage usage;
839 struct pipe_transfer *ptDraw;
840 ubyte *drawMap;
841 ubyte *buffer;
842 int i;
843
844 buffer = malloc(width * height * sizeof(ubyte));
845 if (!buffer) {
846 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
847 return;
848 }
849
850 /* this will do stencil pixel transfer ops */
851 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
852 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
853 &ctx->DefaultPacking, buffer);
854
855 if(util_format_get_component_bits(rbDraw->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
856 usage = PIPE_TRANSFER_READ_WRITE;
857 else
858 usage = PIPE_TRANSFER_WRITE;
859
860 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
861 dsty = rbDraw->Base.Height - dsty - height;
862 }
863
864 ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
865 rbDraw->texture, 0, 0, 0,
866 usage, dstx, dsty,
867 width, height);
868
869 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
870 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
871
872 /* map the stencil buffer */
873 drawMap = pipe_transfer_map(pipe, ptDraw);
874
875 /* draw */
876 /* XXX PixelZoom not handled yet */
877 for (i = 0; i < height; i++) {
878 ubyte *dst;
879 const ubyte *src;
880 int y;
881
882 y = i;
883
884 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
885 y = height - y - 1;
886 }
887
888 dst = drawMap + y * ptDraw->stride;
889 src = buffer + i * width;
890
891 switch (ptDraw->resource->format) {
892 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
893 {
894 uint *dst4 = (uint *) dst;
895 int j;
896 assert(usage == PIPE_TRANSFER_READ_WRITE);
897 for (j = 0; j < width; j++) {
898 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
899 dst4++;
900 }
901 }
902 break;
903 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
904 {
905 uint *dst4 = (uint *) dst;
906 int j;
907 assert(usage == PIPE_TRANSFER_READ_WRITE);
908 for (j = 0; j < width; j++) {
909 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
910 dst4++;
911 }
912 }
913 break;
914 case PIPE_FORMAT_S8_USCALED:
915 assert(usage == PIPE_TRANSFER_WRITE);
916 memcpy(dst, src, width);
917 break;
918 default:
919 assert(0);
920 }
921 }
922
923 free(buffer);
924
925 /* unmap the stencil buffer */
926 pipe_transfer_unmap(pipe, ptDraw);
927 pipe->transfer_destroy(pipe, ptDraw);
928 }
929
930
931 static void
932 st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
933 GLsizei width, GLsizei height,
934 GLint dstx, GLint dsty, GLenum type)
935 {
936 struct st_context *st = st_context(ctx);
937 struct pipe_context *pipe = st->pipe;
938 struct pipe_screen *screen = pipe->screen;
939 struct st_renderbuffer *rbRead;
940 void *driver_vp, *driver_fp;
941 struct pipe_resource *pt;
942 struct pipe_sampler_view *sv;
943 GLfloat *color;
944 enum pipe_format srcFormat, texFormat;
945 GLboolean invertTex = GL_FALSE;
946 GLint readX, readY, readW, readH;
947 GLuint sample_count;
948 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
949
950 st_validate_state(st);
951
952 if (type == GL_STENCIL) {
953 /* can't use texturing to do stencil */
954 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
955 return;
956 }
957
958 if (type == GL_COLOR) {
959 rbRead = st_get_color_read_renderbuffer(ctx);
960 color = NULL;
961 driver_fp = combined_drawpix_fragment_program(ctx);
962 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
963 }
964 else {
965 assert(type == GL_DEPTH);
966 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
967 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
968 driver_fp = make_fragment_shader_z(st);
969 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
970 }
971
972 sample_count = rbRead->texture->nr_samples;
973 /* I believe this would be legal, presumably would need to do a resolve
974 for color, and for depth/stencil spec says to just use one of the
975 depth/stencil samples per pixel? Need some transfer clarifications. */
976 assert(sample_count < 2);
977
978 srcFormat = rbRead->texture->format;
979
980 if (screen->is_format_supported(screen, srcFormat, st->internal_target, sample_count,
981 PIPE_BIND_SAMPLER_VIEW, 0)) {
982 texFormat = srcFormat;
983 }
984 else {
985 /* srcFormat can't be used as a texture format */
986 if (type == GL_DEPTH) {
987 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
988 st->internal_target, sample_count,
989 PIPE_BIND_DEPTH_STENCIL);
990 assert(texFormat != PIPE_FORMAT_NONE);
991 }
992 else {
993 /* default color format */
994 texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
995 sample_count, PIPE_BIND_SAMPLER_VIEW);
996 assert(texFormat != PIPE_FORMAT_NONE);
997 }
998 }
999
1000 /* Invert src region if needed */
1001 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1002 srcy = ctx->ReadBuffer->Height - srcy - height;
1003 invertTex = !invertTex;
1004 }
1005
1006 /* Clip the read region against the src buffer bounds.
1007 * We'll still allocate a temporary buffer/texture for the original
1008 * src region size but we'll only read the region which is on-screen.
1009 * This may mean that we draw garbage pixels into the dest region, but
1010 * that's expected.
1011 */
1012 readX = srcx;
1013 readY = srcy;
1014 readW = width;
1015 readH = height;
1016 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1017 readW = MAX2(0, readW);
1018 readH = MAX2(0, readH);
1019
1020 /* alloc temporary texture */
1021 pt = alloc_texture(st, width, height, texFormat);
1022 if (!pt)
1023 return;
1024
1025 sv = st_create_texture_sampler_view(st->pipe, pt);
1026 if (!sv) {
1027 pipe_resource_reference(&pt, NULL);
1028 return;
1029 }
1030
1031 /* Make temporary texture which is a copy of the src region.
1032 */
1033 if (srcFormat == texFormat) {
1034 struct pipe_subresource srcsub, dstsub;
1035 srcsub.face = 0;
1036 srcsub.level = 0;
1037 dstsub.face = 0;
1038 dstsub.level = 0;
1039 /* copy source framebuffer surface into mipmap/texture */
1040 pipe->resource_copy_region(pipe,
1041 pt, /* dest tex */
1042 dstsub,
1043 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1044 rbRead->texture, /* src tex */
1045 srcsub,
1046 readX, readY, 0, readW, readH); /* src region */
1047
1048 }
1049 else {
1050 /* CPU-based fallback/conversion */
1051 struct pipe_transfer *ptRead =
1052 pipe_get_transfer(st->pipe, rbRead->texture, 0, 0, 0,
1053 PIPE_TRANSFER_READ,
1054 readX, readY, readW, readH);
1055 struct pipe_transfer *ptTex;
1056 enum pipe_transfer_usage transfer_usage;
1057
1058 if (ST_DEBUG & DEBUG_FALLBACK)
1059 debug_printf("%s: fallback processing\n", __FUNCTION__);
1060
1061 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1062 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1063 else
1064 transfer_usage = PIPE_TRANSFER_WRITE;
1065
1066 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, 0, transfer_usage,
1067 0, 0, width, height);
1068
1069 /* copy image from ptRead surface to ptTex surface */
1070 if (type == GL_COLOR) {
1071 /* alternate path using get/put_tile() */
1072 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1073 pipe_get_tile_rgba(pipe, ptRead, readX, readY, readW, readH, buf);
1074 pipe_put_tile_rgba(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1075 readW, readH, buf);
1076 free(buf);
1077 }
1078 else {
1079 /* GL_DEPTH */
1080 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1081 pipe_get_tile_z(pipe, ptRead, readX, readY, readW, readH, buf);
1082 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1083 readW, readH, buf);
1084 free(buf);
1085 }
1086
1087 pipe->transfer_destroy(pipe, ptRead);
1088 pipe->transfer_destroy(pipe, ptTex);
1089 }
1090
1091 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1092 * textured quad with that texture.
1093 */
1094 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1095 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1096 sv,
1097 driver_vp,
1098 driver_fp,
1099 color, invertTex);
1100
1101 pipe_resource_reference(&pt, NULL);
1102 pipe_sampler_view_reference(&sv, NULL);
1103 }
1104
1105
1106
1107 void st_init_drawpixels_functions(struct dd_function_table *functions)
1108 {
1109 functions->DrawPixels = st_DrawPixels;
1110 functions->CopyPixels = st_CopyPixels;
1111 }
1112
1113
1114 void
1115 st_destroy_drawpix(struct st_context *st)
1116 {
1117 st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
1118 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1119 if (st->drawpix.vert_shaders[0])
1120 ureg_free_tokens(st->drawpix.vert_shaders[0]);
1121 if (st->drawpix.vert_shaders[1])
1122 ureg_free_tokens(st->drawpix.vert_shaders[1]);
1123 }
1124
1125 #endif /* FEATURE_drawpix */