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