st/mesa: fix regressions in glDrawPixels(GL_STENCIL_INDEX)
[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(struct gl_context *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, GLboolean write_depth, GLboolean write_stencil)
172 {
173 struct gl_context *ctx = st->ctx;
174 struct gl_program *p;
175 struct st_fragment_program *stp;
176 GLuint ic = 0;
177 const GLuint shaderIndex = write_depth * 2 + write_stencil;
178
179 assert(shaderIndex < Elements(st->drawpix.shaders));
180
181 if (st->drawpix.shaders[shaderIndex]) {
182 /* already have the proper shader */
183 return st->drawpix.shaders[shaderIndex]->driver_shader;
184 }
185
186 /*
187 * Create shader now
188 */
189 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
190 if (!p)
191 return NULL;
192
193 p->NumInstructions = write_depth ? 2 : 1;
194 p->NumInstructions += write_stencil ? 1 : 0;
195
196 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
197 if (!p->Instructions) {
198 ctx->Driver.DeleteProgram(ctx, p);
199 return NULL;
200 }
201 _mesa_init_instructions(p->Instructions, p->NumInstructions);
202
203 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
204 if (write_depth) {
205 p->Instructions[ic].Opcode = OPCODE_TEX;
206 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
207 p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
208 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
209 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
210 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
211 p->Instructions[ic].TexSrcUnit = 0;
212 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
213 ic++;
214 }
215
216 if (write_stencil) {
217 p->Instructions[ic].Opcode = OPCODE_TEX;
218 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
219 p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
220 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
221 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
222 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
223 p->Instructions[ic].TexSrcUnit = 1;
224 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
225 ic++;
226 }
227
228 /* END; */
229 p->Instructions[ic++].Opcode = OPCODE_END;
230
231 assert(ic == p->NumInstructions);
232
233 p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
234 p->OutputsWritten = 0;
235 if (write_depth)
236 p->OutputsWritten |= (1 << FRAG_RESULT_DEPTH);
237 if (write_stencil)
238 p->OutputsWritten |= (1 << FRAG_RESULT_STENCIL);
239
240 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
241 if (write_stencil)
242 p->SamplersUsed |= 1 << 1;
243
244 stp = st_fragment_program((struct gl_fragment_program *) p);
245
246 /* save the new shader */
247 st->drawpix.shaders[shaderIndex] = stp;
248
249 st_translate_fragment_program(st, stp);
250
251 return stp->driver_shader;
252 }
253
254
255
256 /**
257 * Create a simple vertex shader that just passes through the
258 * vertex position and texcoord (and optionally, color).
259 */
260 static void *
261 make_passthrough_vertex_shader(struct st_context *st,
262 GLboolean passColor)
263 {
264 if (!st->drawpix.vert_shaders[passColor]) {
265 struct ureg_program *ureg =
266 ureg_create( TGSI_PROCESSOR_VERTEX );
267
268 if (ureg == NULL)
269 return NULL;
270
271 /* MOV result.pos, vertex.pos; */
272 ureg_MOV(ureg,
273 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
274 ureg_DECL_vs_input( ureg, 0 ));
275
276 /* MOV result.texcoord0, vertex.attr[1]; */
277 ureg_MOV(ureg,
278 ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
279 ureg_DECL_vs_input( ureg, 1 ));
280
281 if (passColor) {
282 /* MOV result.color0, vertex.attr[2]; */
283 ureg_MOV(ureg,
284 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
285 ureg_DECL_vs_input( ureg, 2 ));
286 }
287
288 ureg_END( ureg );
289
290 st->drawpix.vert_shaders[passColor] =
291 ureg_create_shader_and_destroy( ureg, st->pipe );
292 }
293
294 return st->drawpix.vert_shaders[passColor];
295 }
296
297
298 /**
299 * Return a texture internalFormat for drawing/copying an image
300 * of the given type.
301 */
302 static GLenum
303 base_format(GLenum format)
304 {
305 switch (format) {
306 case GL_DEPTH_COMPONENT:
307 return GL_DEPTH_COMPONENT;
308 case GL_DEPTH_STENCIL:
309 return GL_DEPTH_STENCIL;
310 case GL_STENCIL_INDEX:
311 return GL_STENCIL_INDEX;
312 default:
313 return GL_RGBA;
314 }
315 }
316
317
318 /**
319 * Create a temporary texture to hold an image of the given size.
320 * If width, height are not POT and the driver only handles POT textures,
321 * allocate the next larger size of texture that is POT.
322 */
323 static struct pipe_resource *
324 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
325 enum pipe_format texFormat)
326 {
327 struct pipe_resource *pt;
328
329 pt = st_texture_create(st, st->internal_target, texFormat, 0,
330 width, height, 1, PIPE_BIND_SAMPLER_VIEW);
331
332 return pt;
333 }
334
335
336 /**
337 * Make texture containing an image for glDrawPixels image.
338 * If 'pixels' is NULL, leave the texture image data undefined.
339 */
340 static struct pipe_resource *
341 make_texture(struct st_context *st,
342 GLsizei width, GLsizei height, GLenum format, GLenum type,
343 const struct gl_pixelstore_attrib *unpack,
344 const GLvoid *pixels)
345 {
346 struct gl_context *ctx = st->ctx;
347 struct pipe_context *pipe = st->pipe;
348 gl_format mformat;
349 struct pipe_resource *pt;
350 enum pipe_format pipeFormat;
351 GLuint cpp;
352 GLenum baseFormat;
353
354 baseFormat = base_format(format);
355
356 mformat = st_ChooseTextureFormat_renderable(ctx, baseFormat, format, type, GL_FALSE);
357 assert(mformat);
358
359 pipeFormat = st_mesa_format_to_pipe_format(mformat);
360 assert(pipeFormat);
361 cpp = util_format_get_blocksize(pipeFormat);
362
363 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
364 if (!pixels)
365 return NULL;
366
367 /* alloc temporary texture */
368 pt = alloc_texture(st, width, height, pipeFormat);
369 if (!pt) {
370 _mesa_unmap_pbo_source(ctx, unpack);
371 return NULL;
372 }
373
374 {
375 struct pipe_transfer *transfer;
376 static const GLuint dstImageOffsets = 0;
377 GLboolean success;
378 GLubyte *dest;
379 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
380
381 /* we'll do pixel transfer in a fragment shader */
382 ctx->_ImageTransferState = 0x0;
383
384 transfer = pipe_get_transfer(st->pipe, pt, 0, 0, 0,
385 PIPE_TRANSFER_WRITE, 0, 0,
386 width, height);
387
388 /* map texture transfer */
389 dest = pipe_transfer_map(pipe, transfer);
390
391
392 /* Put image into texture transfer.
393 * Note that the image is actually going to be upside down in
394 * the texture. We deal with that with texcoords.
395 */
396 success = _mesa_texstore(ctx, 2, /* dims */
397 baseFormat, /* baseInternalFormat */
398 mformat, /* gl_format */
399 dest, /* dest */
400 0, 0, 0, /* dstX/Y/Zoffset */
401 transfer->stride, /* dstRowStride, bytes */
402 &dstImageOffsets, /* dstImageOffsets */
403 width, height, 1, /* size */
404 format, type, /* src format/type */
405 pixels, /* data source */
406 unpack);
407
408 /* unmap */
409 pipe_transfer_unmap(pipe, transfer);
410 pipe->transfer_destroy(pipe, transfer);
411
412 assert(success);
413
414 /* restore */
415 ctx->_ImageTransferState = imageTransferStateSave;
416 }
417
418 _mesa_unmap_pbo_source(ctx, unpack);
419
420 return pt;
421 }
422
423
424 /**
425 * Draw quad with texcoords and optional color.
426 * Coords are gallium window coords with y=0=top.
427 * \param color may be null
428 * \param invertTex if true, flip texcoords vertically
429 */
430 static void
431 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
432 GLfloat x1, GLfloat y1, const GLfloat *color,
433 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
434 {
435 struct st_context *st = st_context(ctx);
436 struct pipe_context *pipe = st->pipe;
437 GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
438
439 /* setup vertex data */
440 {
441 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
442 const GLfloat fb_width = (GLfloat) fb->Width;
443 const GLfloat fb_height = (GLfloat) fb->Height;
444 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
445 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
446 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
447 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
448 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
449 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
450 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
451 GLuint i;
452
453 /* upper-left */
454 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
455 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
456
457 /* upper-right */
458 verts[1][0][0] = clip_x1;
459 verts[1][0][1] = clip_y0;
460
461 /* lower-right */
462 verts[2][0][0] = clip_x1;
463 verts[2][0][1] = clip_y1;
464
465 /* lower-left */
466 verts[3][0][0] = clip_x0;
467 verts[3][0][1] = clip_y1;
468
469 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
470 verts[0][1][1] = tTop; /* v[0].attr[1].T */
471 verts[1][1][0] = sRight;
472 verts[1][1][1] = tTop;
473 verts[2][1][0] = sRight;
474 verts[2][1][1] = tBot;
475 verts[3][1][0] = sLeft;
476 verts[3][1][1] = tBot;
477
478 /* same for all verts: */
479 if (color) {
480 for (i = 0; i < 4; i++) {
481 verts[i][0][2] = z; /* v[i].attr[0].z */
482 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
483 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
484 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
485 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
486 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
487 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
488 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
489 }
490 }
491 else {
492 for (i = 0; i < 4; i++) {
493 verts[i][0][2] = z; /*Z*/
494 verts[i][0][3] = 1.0f; /*W*/
495 verts[i][1][2] = 0.0f; /*R*/
496 verts[i][1][3] = 1.0f; /*Q*/
497 }
498 }
499 }
500
501 {
502 struct pipe_resource *buf;
503
504 /* allocate/load buffer object with vertex data */
505 buf = pipe_buffer_create(pipe->screen,
506 PIPE_BIND_VERTEX_BUFFER,
507 sizeof(verts));
508 pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
509
510 util_draw_vertex_buffer(pipe, buf, 0,
511 PIPE_PRIM_QUADS,
512 4, /* verts */
513 3); /* attribs/vert */
514 pipe_resource_reference(&buf, NULL);
515 }
516 }
517
518
519
520 static void
521 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
522 GLsizei width, GLsizei height,
523 GLfloat zoomX, GLfloat zoomY,
524 struct pipe_sampler_view **sv,
525 int num_sampler_view,
526 void *driver_vp,
527 void *driver_fp,
528 const GLfloat *color,
529 GLboolean invertTex, GLboolean write_depth, GLboolean write_stencil)
530 {
531 struct st_context *st = st_context(ctx);
532 struct pipe_context *pipe = st->pipe;
533 struct cso_context *cso = st->cso_context;
534 GLfloat x0, y0, x1, y1;
535 GLsizei maxSize;
536 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
537
538 /* limit checks */
539 /* XXX if DrawPixels image is larger than max texture size, break
540 * it up into chunks.
541 */
542 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
543 assert(width <= maxSize);
544 assert(height <= maxSize);
545
546 cso_save_depth_stencil_alpha(cso);
547 cso_save_rasterizer(cso);
548 cso_save_viewport(cso);
549 cso_save_samplers(cso);
550 cso_save_fragment_sampler_views(cso);
551 cso_save_fragment_shader(cso);
552 cso_save_vertex_shader(cso);
553 cso_save_vertex_elements(cso);
554
555 /* rasterizer state: just scissor */
556 {
557 struct pipe_rasterizer_state rasterizer;
558 memset(&rasterizer, 0, sizeof(rasterizer));
559 rasterizer.gl_rasterization_rules = 1;
560 rasterizer.scissor = ctx->Scissor.Enabled;
561 cso_set_rasterizer(cso, &rasterizer);
562 }
563
564 if (write_depth || write_stencil)
565 {
566 struct pipe_depth_stencil_alpha_state dsa;
567 memset(&dsa, 0, sizeof(dsa));
568 if (write_depth) {
569 dsa.depth.enabled = 1;
570 dsa.depth.func = PIPE_FUNC_ALWAYS;
571 dsa.depth.writemask = 1;
572 }
573 if (write_stencil) {
574 dsa.stencil[0].enabled = 1;
575 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
576 dsa.stencil[0].writemask = 0xff;
577 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
578 }
579 cso_set_depth_stencil_alpha(cso, &dsa);
580 }
581
582 /* fragment shader state: TEX lookup program */
583 cso_set_fragment_shader_handle(cso, driver_fp);
584
585 /* vertex shader state: position + texcoord pass-through */
586 cso_set_vertex_shader_handle(cso, driver_vp);
587
588
589 /* texture sampling state: */
590 {
591 struct pipe_sampler_state sampler;
592 memset(&sampler, 0, sizeof(sampler));
593 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
594 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
595 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
596 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
597 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
598 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
599 sampler.normalized_coords = normalized;
600
601 cso_single_sampler(cso, 0, &sampler);
602 if (num_sampler_view > 1) {
603 cso_single_sampler(cso, 1, &sampler);
604 }
605 cso_single_sampler_done(cso);
606 }
607
608 /* viewport state: viewport matching window dims */
609 {
610 const float w = (float) ctx->DrawBuffer->Width;
611 const float h = (float) ctx->DrawBuffer->Height;
612 struct pipe_viewport_state vp;
613 vp.scale[0] = 0.5f * w;
614 vp.scale[1] = -0.5f * h;
615 vp.scale[2] = 0.5f;
616 vp.scale[3] = 1.0f;
617 vp.translate[0] = 0.5f * w;
618 vp.translate[1] = 0.5f * h;
619 vp.translate[2] = 0.5f;
620 vp.translate[3] = 0.0f;
621 cso_set_viewport(cso, &vp);
622 }
623
624 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
625
626 /* texture state: */
627 cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
628
629 /* Compute Gallium window coords (y=0=top) with pixel zoom.
630 * Recall that these coords are transformed by the current
631 * vertex shader and viewport transformation.
632 */
633 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
634 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
635 invertTex = !invertTex;
636 }
637
638 x0 = (GLfloat) x;
639 x1 = x + width * ctx->Pixel.ZoomX;
640 y0 = (GLfloat) y;
641 y1 = y + height * ctx->Pixel.ZoomY;
642
643 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
644 z = z * 2.0 - 1.0;
645
646 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
647 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
648 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
649
650 /* restore state */
651 cso_restore_depth_stencil_alpha(cso);
652 cso_restore_rasterizer(cso);
653 cso_restore_viewport(cso);
654 cso_restore_samplers(cso);
655 cso_restore_fragment_sampler_views(cso);
656 cso_restore_fragment_shader(cso);
657 cso_restore_vertex_shader(cso);
658 cso_restore_vertex_elements(cso);
659 }
660
661
662 static void
663 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
664 GLsizei width, GLsizei height, GLenum format, GLenum type,
665 const struct gl_pixelstore_attrib *unpack,
666 const GLvoid *pixels)
667 {
668 struct st_context *st = st_context(ctx);
669 struct pipe_context *pipe = st->pipe;
670 struct st_renderbuffer *strb;
671 enum pipe_transfer_usage usage;
672 struct pipe_transfer *pt;
673 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
674 GLint skipPixels;
675 ubyte *stmap;
676 struct gl_pixelstore_attrib clippedUnpack = *unpack;
677
678 if (!zoom) {
679 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
680 &clippedUnpack)) {
681 /* totally clipped */
682 return;
683 }
684 }
685
686 strb = st_renderbuffer(ctx->DrawBuffer->
687 Attachment[BUFFER_STENCIL].Renderbuffer);
688
689 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
690 y = ctx->DrawBuffer->Height - y - height;
691 }
692
693 if(format != GL_DEPTH_STENCIL &&
694 util_format_get_component_bits(strb->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
695 usage = PIPE_TRANSFER_READ_WRITE;
696 else
697 usage = PIPE_TRANSFER_WRITE;
698
699 pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0, 0,
700 usage, x, y,
701 width, height);
702
703 stmap = pipe_transfer_map(pipe, pt);
704
705 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
706 assert(pixels);
707
708 /* if width > MAX_WIDTH, have to process image in chunks */
709 skipPixels = 0;
710 while (skipPixels < width) {
711 const GLint spanX = skipPixels;
712 const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
713 GLint row;
714 for (row = 0; row < height; row++) {
715 GLubyte sValues[MAX_WIDTH];
716 GLuint zValues[MAX_WIDTH];
717 GLenum destType = GL_UNSIGNED_BYTE;
718 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
719 width, height,
720 format, type,
721 row, skipPixels);
722 _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
723 type, source, &clippedUnpack,
724 ctx->_ImageTransferState);
725
726 if (format == GL_DEPTH_STENCIL) {
727 _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
728 (1 << 24) - 1, type, source,
729 &clippedUnpack);
730 }
731
732 if (zoom) {
733 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
734 "zoom not complete");
735 }
736
737 {
738 GLint spanY;
739
740 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
741 spanY = height - row - 1;
742 }
743 else {
744 spanY = row;
745 }
746
747 /* now pack the stencil (and Z) values in the dest format */
748 switch (pt->resource->format) {
749 case PIPE_FORMAT_S8_USCALED:
750 {
751 ubyte *dest = stmap + spanY * pt->stride + spanX;
752 assert(usage == PIPE_TRANSFER_WRITE);
753 memcpy(dest, sValues, spanWidth);
754 }
755 break;
756 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
757 if (format == GL_DEPTH_STENCIL) {
758 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
759 GLint k;
760 assert(usage == PIPE_TRANSFER_WRITE);
761 for (k = 0; k < spanWidth; k++) {
762 dest[k] = zValues[k] | (sValues[k] << 24);
763 }
764 }
765 else {
766 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
767 GLint k;
768 assert(usage == PIPE_TRANSFER_READ_WRITE);
769 for (k = 0; k < spanWidth; k++) {
770 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
771 }
772 }
773 break;
774 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
775 if (format == GL_DEPTH_STENCIL) {
776 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
777 GLint k;
778 assert(usage == PIPE_TRANSFER_WRITE);
779 for (k = 0; k < spanWidth; k++) {
780 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
781 }
782 }
783 else {
784 uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
785 GLint k;
786 assert(usage == PIPE_TRANSFER_READ_WRITE);
787 for (k = 0; k < spanWidth; k++) {
788 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
789 }
790 }
791 break;
792 default:
793 assert(0);
794 }
795 }
796 }
797 skipPixels += spanWidth;
798 }
799
800 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
801
802 /* unmap the stencil buffer */
803 pipe_transfer_unmap(pipe, pt);
804 pipe->transfer_destroy(pipe, pt);
805 }
806
807
808 /**
809 * Called via ctx->Driver.DrawPixels()
810 */
811 static void
812 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
813 GLenum format, GLenum type,
814 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
815 {
816 void *driver_vp, *driver_fp;
817 struct st_context *st = st_context(ctx);
818 const GLfloat *color;
819 struct pipe_context *pipe = st->pipe;
820 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
821 struct pipe_sampler_view *sv[2];
822 int num_sampler_view = 1;
823 enum pipe_format stencil_format = PIPE_FORMAT_NONE;
824
825 if (format == GL_DEPTH_STENCIL)
826 write_stencil = write_depth = GL_TRUE;
827 else if (format == GL_STENCIL_INDEX)
828 write_stencil = GL_TRUE;
829 else if (format == GL_DEPTH_COMPONENT)
830 write_depth = GL_TRUE;
831
832 if (write_stencil) {
833 enum pipe_format tex_format;
834 /* can we write to stencil if not fallback */
835 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT))
836 goto stencil_fallback;
837
838 tex_format = st_choose_format(st->pipe->screen, base_format(format), PIPE_TEXTURE_2D,
839 0, PIPE_BIND_SAMPLER_VIEW);
840 if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
841 stencil_format = PIPE_FORMAT_X24S8_USCALED;
842 else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM)
843 stencil_format = PIPE_FORMAT_S8X24_USCALED;
844 else
845 stencil_format = PIPE_FORMAT_S8_USCALED;
846 if (stencil_format == PIPE_FORMAT_NONE)
847 goto stencil_fallback;
848 }
849
850 /* Mesa state should be up to date by now */
851 assert(ctx->NewState == 0x0);
852
853 st_validate_state(st);
854
855 if (write_depth || write_stencil) {
856 driver_fp = make_fragment_shader_z(st, write_depth, write_stencil);
857 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
858 color = ctx->Current.RasterColor;
859 }
860 else {
861 driver_fp = combined_drawpix_fragment_program(ctx);
862 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
863 color = NULL;
864 if (st->pixel_xfer.pixelmap_enabled) {
865 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
866 num_sampler_view++;
867 }
868 }
869
870 /* draw with textured quad */
871 {
872 struct pipe_resource *pt
873 = make_texture(st, width, height, format, type, unpack, pixels);
874 if (pt) {
875 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
876
877 if (sv[0]) {
878 if (write_stencil) {
879 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt, stencil_format);
880 num_sampler_view++;
881 }
882
883 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
884 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
885 sv,
886 num_sampler_view,
887 driver_vp,
888 driver_fp,
889 color, GL_FALSE, write_depth, write_stencil);
890 pipe_sampler_view_reference(&sv[0], NULL);
891 if (num_sampler_view > 1)
892 pipe_sampler_view_reference(&sv[1], NULL);
893 }
894 pipe_resource_reference(&pt, NULL);
895 }
896 }
897 return;
898 stencil_fallback:
899 draw_stencil_pixels(ctx, x, y, width, height, format, type,
900 unpack, pixels);
901 }
902
903
904
905 static void
906 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
907 GLsizei width, GLsizei height,
908 GLint dstx, GLint dsty)
909 {
910 struct st_renderbuffer *rbDraw;
911 struct pipe_context *pipe = st_context(ctx)->pipe;
912 enum pipe_transfer_usage usage;
913 struct pipe_transfer *ptDraw;
914 ubyte *drawMap;
915 ubyte *buffer;
916 int i;
917
918 buffer = malloc(width * height * sizeof(ubyte));
919 if (!buffer) {
920 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
921 return;
922 }
923
924 /* Get the dest renderbuffer. If there's a wrapper, use the
925 * underlying renderbuffer.
926 */
927 rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
928 if (rbDraw->Base.Wrapped)
929 rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
930
931 /* this will do stencil pixel transfer ops */
932 st_read_stencil_pixels(ctx, srcx, srcy, width, height,
933 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
934 &ctx->DefaultPacking, buffer);
935
936 if(util_format_get_component_bits(rbDraw->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
937 usage = PIPE_TRANSFER_READ_WRITE;
938 else
939 usage = PIPE_TRANSFER_WRITE;
940
941 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
942 dsty = rbDraw->Base.Height - dsty - height;
943 }
944
945 ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
946 rbDraw->texture, 0, 0, 0,
947 usage, dstx, dsty,
948 width, height);
949
950 assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
951 assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
952
953 /* map the stencil buffer */
954 drawMap = pipe_transfer_map(pipe, ptDraw);
955
956 /* draw */
957 /* XXX PixelZoom not handled yet */
958 for (i = 0; i < height; i++) {
959 ubyte *dst;
960 const ubyte *src;
961 int y;
962
963 y = i;
964
965 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
966 y = height - y - 1;
967 }
968
969 dst = drawMap + y * ptDraw->stride;
970 src = buffer + i * width;
971
972 switch (ptDraw->resource->format) {
973 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
974 {
975 uint *dst4 = (uint *) dst;
976 int j;
977 assert(usage == PIPE_TRANSFER_READ_WRITE);
978 for (j = 0; j < width; j++) {
979 *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
980 dst4++;
981 }
982 }
983 break;
984 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
985 {
986 uint *dst4 = (uint *) dst;
987 int j;
988 assert(usage == PIPE_TRANSFER_READ_WRITE);
989 for (j = 0; j < width; j++) {
990 *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
991 dst4++;
992 }
993 }
994 break;
995 case PIPE_FORMAT_S8_USCALED:
996 assert(usage == PIPE_TRANSFER_WRITE);
997 memcpy(dst, src, width);
998 break;
999 default:
1000 assert(0);
1001 }
1002 }
1003
1004 free(buffer);
1005
1006 /* unmap the stencil buffer */
1007 pipe_transfer_unmap(pipe, ptDraw);
1008 pipe->transfer_destroy(pipe, ptDraw);
1009 }
1010
1011
1012 static void
1013 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1014 GLsizei width, GLsizei height,
1015 GLint dstx, GLint dsty, GLenum type)
1016 {
1017 struct st_context *st = st_context(ctx);
1018 struct pipe_context *pipe = st->pipe;
1019 struct pipe_screen *screen = pipe->screen;
1020 struct st_renderbuffer *rbRead;
1021 void *driver_vp, *driver_fp;
1022 struct pipe_resource *pt;
1023 struct pipe_sampler_view *sv[2];
1024 int num_sampler_view = 1;
1025 GLfloat *color;
1026 enum pipe_format srcFormat, texFormat;
1027 GLboolean invertTex = GL_FALSE;
1028 GLint readX, readY, readW, readH;
1029 GLuint sample_count;
1030 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1031
1032 st_validate_state(st);
1033
1034 if (type == GL_STENCIL) {
1035 /* can't use texturing to do stencil */
1036 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1037 return;
1038 }
1039
1040 if (type == GL_COLOR) {
1041 rbRead = st_get_color_read_renderbuffer(ctx);
1042 color = NULL;
1043 driver_fp = combined_drawpix_fragment_program(ctx);
1044 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1045 if (st->pixel_xfer.pixelmap_enabled) {
1046 sv[1] = st->pixel_xfer.pixelmap_sampler_view;
1047 num_sampler_view++;
1048 }
1049 }
1050 else {
1051 assert(type == GL_DEPTH);
1052 rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
1053 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1054 driver_fp = make_fragment_shader_z(st, GL_TRUE, GL_FALSE);
1055 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1056 }
1057
1058 if (rbRead->Base.Wrapped)
1059 rbRead = st_renderbuffer(rbRead->Base.Wrapped);
1060
1061 sample_count = rbRead->texture->nr_samples;
1062 /* I believe this would be legal, presumably would need to do a resolve
1063 for color, and for depth/stencil spec says to just use one of the
1064 depth/stencil samples per pixel? Need some transfer clarifications. */
1065 assert(sample_count < 2);
1066
1067 srcFormat = rbRead->texture->format;
1068
1069 if (screen->is_format_supported(screen, srcFormat, st->internal_target, sample_count,
1070 PIPE_BIND_SAMPLER_VIEW, 0)) {
1071 texFormat = srcFormat;
1072 }
1073 else {
1074 /* srcFormat can't be used as a texture format */
1075 if (type == GL_DEPTH) {
1076 texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
1077 st->internal_target, sample_count,
1078 PIPE_BIND_DEPTH_STENCIL);
1079 assert(texFormat != PIPE_FORMAT_NONE);
1080 }
1081 else {
1082 /* default color format */
1083 texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
1084 sample_count, PIPE_BIND_SAMPLER_VIEW);
1085 assert(texFormat != PIPE_FORMAT_NONE);
1086 }
1087 }
1088
1089 /* Invert src region if needed */
1090 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1091 srcy = ctx->ReadBuffer->Height - srcy - height;
1092 invertTex = !invertTex;
1093 }
1094
1095 /* Clip the read region against the src buffer bounds.
1096 * We'll still allocate a temporary buffer/texture for the original
1097 * src region size but we'll only read the region which is on-screen.
1098 * This may mean that we draw garbage pixels into the dest region, but
1099 * that's expected.
1100 */
1101 readX = srcx;
1102 readY = srcy;
1103 readW = width;
1104 readH = height;
1105 _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
1106 readW = MAX2(0, readW);
1107 readH = MAX2(0, readH);
1108
1109 /* alloc temporary texture */
1110 pt = alloc_texture(st, width, height, texFormat);
1111 if (!pt)
1112 return;
1113
1114 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1115 if (!sv[0]) {
1116 pipe_resource_reference(&pt, NULL);
1117 return;
1118 }
1119
1120 /* Make temporary texture which is a copy of the src region.
1121 */
1122 if (srcFormat == texFormat) {
1123 struct pipe_subresource srcsub, dstsub;
1124 srcsub.face = 0;
1125 srcsub.level = 0;
1126 dstsub.face = 0;
1127 dstsub.level = 0;
1128 /* copy source framebuffer surface into mipmap/texture */
1129 pipe->resource_copy_region(pipe,
1130 pt, /* dest tex */
1131 dstsub,
1132 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
1133 rbRead->texture, /* src tex */
1134 srcsub,
1135 readX, readY, 0, readW, readH); /* src region */
1136
1137 }
1138 else {
1139 /* CPU-based fallback/conversion */
1140 struct pipe_transfer *ptRead =
1141 pipe_get_transfer(st->pipe, rbRead->texture, 0, 0, 0,
1142 PIPE_TRANSFER_READ,
1143 readX, readY, readW, readH);
1144 struct pipe_transfer *ptTex;
1145 enum pipe_transfer_usage transfer_usage;
1146
1147 if (ST_DEBUG & DEBUG_FALLBACK)
1148 debug_printf("%s: fallback processing\n", __FUNCTION__);
1149
1150 if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
1151 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1152 else
1153 transfer_usage = PIPE_TRANSFER_WRITE;
1154
1155 ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, 0, transfer_usage,
1156 0, 0, width, height);
1157
1158 /* copy image from ptRead surface to ptTex surface */
1159 if (type == GL_COLOR) {
1160 /* alternate path using get/put_tile() */
1161 GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1162 pipe_get_tile_rgba(pipe, ptRead, readX, readY, readW, readH, buf);
1163 pipe_put_tile_rgba(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1164 readW, readH, buf);
1165 free(buf);
1166 }
1167 else {
1168 /* GL_DEPTH */
1169 GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
1170 pipe_get_tile_z(pipe, ptRead, readX, readY, readW, readH, buf);
1171 pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
1172 readW, readH, buf);
1173 free(buf);
1174 }
1175
1176 pipe->transfer_destroy(pipe, ptRead);
1177 pipe->transfer_destroy(pipe, ptTex);
1178 }
1179
1180 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1181 * textured quad with that texture.
1182 */
1183 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1184 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1185 sv,
1186 num_sampler_view,
1187 driver_vp,
1188 driver_fp,
1189 color, invertTex, GL_FALSE, GL_FALSE);
1190
1191 pipe_resource_reference(&pt, NULL);
1192 pipe_sampler_view_reference(&sv[0], NULL);
1193 }
1194
1195
1196
1197 void st_init_drawpixels_functions(struct dd_function_table *functions)
1198 {
1199 functions->DrawPixels = st_DrawPixels;
1200 functions->CopyPixels = st_CopyPixels;
1201 }
1202
1203
1204 void
1205 st_destroy_drawpix(struct st_context *st)
1206 {
1207 GLuint i;
1208
1209 for (i = 0; i < Elements(st->drawpix.shaders); i++) {
1210 if (st->drawpix.shaders[i])
1211 st_reference_fragprog(st, &st->drawpix.shaders[i], NULL);
1212 }
1213
1214 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1215 if (st->drawpix.vert_shaders[0])
1216 ureg_free_tokens(st->drawpix.vert_shaders[0]);
1217 if (st->drawpix.vert_shaders[1])
1218 ureg_free_tokens(st->drawpix.vert_shaders[1]);
1219 }
1220
1221 #endif /* FEATURE_drawpix */