st/mesa: make Z/S drawpix shaders independent of variants, don't use Mesa IR v2
[mesa.git] / src / mesa / state_tracker / st_cb_drawpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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/blit.h"
37 #include "main/format_pack.h"
38 #include "main/macros.h"
39 #include "main/mtypes.h"
40 #include "main/pack.h"
41 #include "main/pbo.h"
42 #include "main/readpix.h"
43 #include "main/texformat.h"
44 #include "main/teximage.h"
45 #include "main/texstore.h"
46 #include "main/glformats.h"
47 #include "program/program.h"
48 #include "program/prog_print.h"
49 #include "program/prog_instruction.h"
50
51 #include "st_atom.h"
52 #include "st_atom_constbuf.h"
53 #include "st_cb_drawpixels.h"
54 #include "st_cb_readpixels.h"
55 #include "st_cb_fbo.h"
56 #include "st_context.h"
57 #include "st_debug.h"
58 #include "st_format.h"
59 #include "st_program.h"
60 #include "st_texture.h"
61
62 #include "pipe/p_context.h"
63 #include "pipe/p_defines.h"
64 #include "tgsi/tgsi_ureg.h"
65 #include "util/u_draw_quad.h"
66 #include "util/u_format.h"
67 #include "util/u_inlines.h"
68 #include "util/u_math.h"
69 #include "util/u_tile.h"
70 #include "util/u_upload_mgr.h"
71 #include "cso_cache/cso_context.h"
72
73
74 /**
75 * Check if the given program is:
76 * 0: MOVE result.color, fragment.color;
77 * 1: END;
78 */
79 static GLboolean
80 is_passthrough_program(const struct gl_fragment_program *prog)
81 {
82 if (prog->Base.NumInstructions == 2) {
83 const struct prog_instruction *inst = prog->Base.Instructions;
84 if (inst[0].Opcode == OPCODE_MOV &&
85 inst[1].Opcode == OPCODE_END &&
86 inst[0].DstReg.File == PROGRAM_OUTPUT &&
87 inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
88 inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
89 inst[0].SrcReg[0].File == PROGRAM_INPUT &&
90 inst[0].SrcReg[0].Index == VARYING_SLOT_COL0 &&
91 inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
92 return GL_TRUE;
93 }
94 }
95 return GL_FALSE;
96 }
97
98
99 /**
100 * Returns a fragment program which implements the current pixel transfer ops.
101 */
102 static struct gl_fragment_program *
103 get_glsl_pixel_transfer_program(struct st_context *st,
104 struct st_fragment_program *orig)
105 {
106 int pixelMaps = 0, scaleAndBias = 0;
107 struct gl_context *ctx = st->ctx;
108 struct st_fragment_program *fp = (struct st_fragment_program *)
109 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
110
111 if (!fp)
112 return NULL;
113
114 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
115 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
116 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
117 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
118 scaleAndBias = 1;
119 }
120
121 pixelMaps = ctx->Pixel.MapColorFlag;
122
123 if (pixelMaps) {
124 /* create the colormap/texture now if not already done */
125 if (!st->pixel_xfer.pixelmap_texture) {
126 st->pixel_xfer.pixelmap_texture = st_create_color_map_texture(ctx);
127 st->pixel_xfer.pixelmap_sampler_view =
128 st_create_texture_sampler_view(st->pipe,
129 st->pixel_xfer.pixelmap_texture);
130 }
131 }
132
133 get_pixel_transfer_visitor(fp, orig->glsl_to_tgsi,
134 scaleAndBias, pixelMaps);
135
136 return &fp->Base;
137 }
138
139
140 /**
141 * Make fragment shader for glDraw/CopyPixels. This shader is made
142 * by combining the pixel transfer shader with the user-defined shader.
143 * \param fpIn the current/incoming fragment program
144 * \param fpOut returns the combined fragment program
145 */
146 void
147 st_make_drawpix_fragment_program(struct st_context *st,
148 struct gl_fragment_program *fpIn,
149 struct gl_fragment_program **fpOut)
150 {
151 struct gl_program *newProg;
152 struct st_fragment_program *stfp = (struct st_fragment_program *) fpIn;
153
154 if (is_passthrough_program(fpIn)) {
155 newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
156 &st->pixel_xfer.program->Base);
157 }
158 else if (stfp->glsl_to_tgsi != NULL) {
159 newProg = (struct gl_program *) get_glsl_pixel_transfer_program(st, stfp);
160 }
161 else {
162 #if 0
163 /* debug */
164 printf("Base program:\n");
165 _mesa_print_program(&fpIn->Base);
166 printf("DrawPix program:\n");
167 _mesa_print_program(&st->pixel_xfer.program->Base.Base);
168 #endif
169 newProg = _mesa_combine_programs(st->ctx,
170 &st->pixel_xfer.program->Base.Base,
171 &fpIn->Base);
172 }
173
174 #if 0
175 /* debug */
176 printf("Combined DrawPixels program:\n");
177 _mesa_print_program(newProg);
178 printf("InputsRead: 0x%x\n", newProg->InputsRead);
179 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
180 _mesa_print_parameter_list(newProg->Parameters);
181 #endif
182
183 *fpOut = (struct gl_fragment_program *) newProg;
184 }
185
186
187 /**
188 * Create fragment program that does a TEX() instruction to get a Z and/or
189 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
190 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
191 * Pass fragment color through as-is.
192 *
193 * \return CSO of the fragment shader.
194 */
195 static void *
196 get_drawpix_z_stencil_program(struct st_context *st,
197 GLboolean write_depth,
198 GLboolean write_stencil)
199 {
200 struct ureg_program *ureg;
201 struct ureg_src depth_sampler, stencil_sampler;
202 struct ureg_src texcoord, color;
203 struct ureg_dst out_color, out_depth, out_stencil;
204 const GLuint shaderIndex = write_depth * 2 + write_stencil;
205 void *cso;
206
207 assert(shaderIndex < ARRAY_SIZE(st->drawpix.zs_shaders));
208
209 if (st->drawpix.zs_shaders[shaderIndex]) {
210 /* already have the proper shader */
211 return st->drawpix.zs_shaders[shaderIndex];
212 }
213
214 ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
215 if (ureg == NULL)
216 return NULL;
217
218 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, TRUE);
219
220 if (write_depth) {
221 color = ureg_DECL_fs_input(ureg, TGSI_SEMANTIC_COLOR, 0,
222 TGSI_INTERPOLATE_COLOR);
223 out_color = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
224
225 depth_sampler = ureg_DECL_sampler(ureg, 0);
226 out_depth = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
227 }
228
229 if (write_stencil) {
230 stencil_sampler = ureg_DECL_sampler(ureg, 1);
231 out_stencil = ureg_DECL_output(ureg, TGSI_SEMANTIC_STENCIL, 0);
232 }
233
234 texcoord = ureg_DECL_fs_input(ureg,
235 st->needs_texcoord_semantic ?
236 TGSI_SEMANTIC_TEXCOORD :
237 TGSI_SEMANTIC_GENERIC,
238 0, TGSI_INTERPOLATE_LINEAR);
239
240 if (write_depth) {
241 ureg_TEX(ureg, ureg_writemask(out_depth, TGSI_WRITEMASK_Z),
242 TGSI_TEXTURE_2D, texcoord, depth_sampler);
243 ureg_MOV(ureg, out_color, color);
244 }
245
246 if (write_stencil)
247 ureg_TEX(ureg, ureg_writemask(out_stencil, TGSI_WRITEMASK_Y),
248 TGSI_TEXTURE_2D, texcoord, stencil_sampler);
249
250 ureg_END(ureg);
251 cso = ureg_create_shader_and_destroy(ureg, st->pipe);
252
253 /* save the new shader */
254 st->drawpix.zs_shaders[shaderIndex] = cso;
255 return cso;
256 }
257
258
259 /**
260 * Create a simple vertex shader that just passes through the
261 * vertex position and texcoord (and optionally, color).
262 */
263 static void *
264 make_passthrough_vertex_shader(struct st_context *st,
265 GLboolean passColor)
266 {
267 const unsigned texcoord_semantic = st->needs_texcoord_semantic ?
268 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC;
269
270 if (!st->drawpix.vert_shaders[passColor]) {
271 struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
272
273 if (ureg == NULL)
274 return NULL;
275
276 /* MOV result.pos, vertex.pos; */
277 ureg_MOV(ureg,
278 ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
279 ureg_DECL_vs_input( ureg, 0 ));
280
281 /* MOV result.texcoord0, vertex.attr[1]; */
282 ureg_MOV(ureg,
283 ureg_DECL_output( ureg, texcoord_semantic, 0 ),
284 ureg_DECL_vs_input( ureg, 1 ));
285
286 if (passColor) {
287 /* MOV result.color0, vertex.attr[2]; */
288 ureg_MOV(ureg,
289 ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
290 ureg_DECL_vs_input( ureg, 2 ));
291 }
292
293 ureg_END( ureg );
294
295 st->drawpix.vert_shaders[passColor] =
296 ureg_create_shader_and_destroy( ureg, st->pipe );
297 }
298
299 return st->drawpix.vert_shaders[passColor];
300 }
301
302
303 /**
304 * Return a texture internalFormat for drawing/copying an image
305 * of the given format and type.
306 */
307 static GLenum
308 internal_format(struct gl_context *ctx, GLenum format, GLenum type)
309 {
310 switch (format) {
311 case GL_DEPTH_COMPONENT:
312 switch (type) {
313 case GL_UNSIGNED_SHORT:
314 return GL_DEPTH_COMPONENT16;
315
316 case GL_UNSIGNED_INT:
317 return GL_DEPTH_COMPONENT32;
318
319 case GL_FLOAT:
320 if (ctx->Extensions.ARB_depth_buffer_float)
321 return GL_DEPTH_COMPONENT32F;
322 else
323 return GL_DEPTH_COMPONENT;
324
325 default:
326 return GL_DEPTH_COMPONENT;
327 }
328
329 case GL_DEPTH_STENCIL:
330 switch (type) {
331 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
332 return GL_DEPTH32F_STENCIL8;
333
334 case GL_UNSIGNED_INT_24_8:
335 default:
336 return GL_DEPTH24_STENCIL8;
337 }
338
339 case GL_STENCIL_INDEX:
340 return GL_STENCIL_INDEX;
341
342 default:
343 if (_mesa_is_enum_format_integer(format)) {
344 switch (type) {
345 case GL_BYTE:
346 return GL_RGBA8I;
347 case GL_UNSIGNED_BYTE:
348 return GL_RGBA8UI;
349 case GL_SHORT:
350 return GL_RGBA16I;
351 case GL_UNSIGNED_SHORT:
352 return GL_RGBA16UI;
353 case GL_INT:
354 return GL_RGBA32I;
355 case GL_UNSIGNED_INT:
356 return GL_RGBA32UI;
357 default:
358 assert(0 && "Unexpected type in internal_format()");
359 return GL_RGBA_INTEGER;
360 }
361 }
362 else {
363 switch (type) {
364 case GL_UNSIGNED_BYTE:
365 case GL_UNSIGNED_INT_8_8_8_8:
366 case GL_UNSIGNED_INT_8_8_8_8_REV:
367 default:
368 return GL_RGBA8;
369
370 case GL_UNSIGNED_BYTE_3_3_2:
371 case GL_UNSIGNED_BYTE_2_3_3_REV:
372 return GL_R3_G3_B2;
373
374 case GL_UNSIGNED_SHORT_4_4_4_4:
375 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
376 return GL_RGBA4;
377
378 case GL_UNSIGNED_SHORT_5_6_5:
379 case GL_UNSIGNED_SHORT_5_6_5_REV:
380 return GL_RGB565;
381
382 case GL_UNSIGNED_SHORT_5_5_5_1:
383 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
384 return GL_RGB5_A1;
385
386 case GL_UNSIGNED_INT_10_10_10_2:
387 case GL_UNSIGNED_INT_2_10_10_10_REV:
388 return GL_RGB10_A2;
389
390 case GL_UNSIGNED_SHORT:
391 case GL_UNSIGNED_INT:
392 return GL_RGBA16;
393
394 case GL_BYTE:
395 return
396 ctx->Extensions.EXT_texture_snorm ? GL_RGBA8_SNORM : GL_RGBA8;
397
398 case GL_SHORT:
399 case GL_INT:
400 return
401 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
402
403 case GL_HALF_FLOAT_ARB:
404 return
405 ctx->Extensions.ARB_texture_float ? GL_RGBA16F :
406 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
407
408 case GL_FLOAT:
409 case GL_DOUBLE:
410 return
411 ctx->Extensions.ARB_texture_float ? GL_RGBA32F :
412 ctx->Extensions.EXT_texture_snorm ? GL_RGBA16_SNORM : GL_RGBA16;
413
414 case GL_UNSIGNED_INT_5_9_9_9_REV:
415 assert(ctx->Extensions.EXT_texture_shared_exponent);
416 return GL_RGB9_E5;
417
418 case GL_UNSIGNED_INT_10F_11F_11F_REV:
419 assert(ctx->Extensions.EXT_packed_float);
420 return GL_R11F_G11F_B10F;
421 }
422 }
423 }
424 }
425
426
427 /**
428 * Create a temporary texture to hold an image of the given size.
429 * If width, height are not POT and the driver only handles POT textures,
430 * allocate the next larger size of texture that is POT.
431 */
432 static struct pipe_resource *
433 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
434 enum pipe_format texFormat, unsigned bind)
435 {
436 struct pipe_resource *pt;
437
438 pt = st_texture_create(st, st->internal_target, texFormat, 0,
439 width, height, 1, 1, 0, bind);
440
441 return pt;
442 }
443
444
445 /**
446 * Make texture containing an image for glDrawPixels image.
447 * If 'pixels' is NULL, leave the texture image data undefined.
448 */
449 static struct pipe_resource *
450 make_texture(struct st_context *st,
451 GLsizei width, GLsizei height, GLenum format, GLenum type,
452 const struct gl_pixelstore_attrib *unpack,
453 const GLvoid *pixels)
454 {
455 struct gl_context *ctx = st->ctx;
456 struct pipe_context *pipe = st->pipe;
457 mesa_format mformat;
458 struct pipe_resource *pt;
459 enum pipe_format pipeFormat;
460 GLenum baseInternalFormat;
461
462 /* Choose a pixel format for the temp texture which will hold the
463 * image to draw.
464 */
465 pipeFormat = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
466 format, type, unpack->SwapBytes);
467
468 if (pipeFormat == PIPE_FORMAT_NONE) {
469 /* Use the generic approach. */
470 GLenum intFormat = internal_format(ctx, format, type);
471
472 pipeFormat = st_choose_format(st, intFormat, format, type,
473 PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW,
474 FALSE);
475 assert(pipeFormat != PIPE_FORMAT_NONE);
476 }
477
478 mformat = st_pipe_format_to_mesa_format(pipeFormat);
479 baseInternalFormat = _mesa_get_format_base_format(mformat);
480
481 pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
482 if (!pixels)
483 return NULL;
484
485 /* alloc temporary texture */
486 pt = alloc_texture(st, width, height, pipeFormat, PIPE_BIND_SAMPLER_VIEW);
487 if (!pt) {
488 _mesa_unmap_pbo_source(ctx, unpack);
489 return NULL;
490 }
491
492 {
493 struct pipe_transfer *transfer;
494 GLboolean success;
495 GLubyte *dest;
496 const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
497
498 /* we'll do pixel transfer in a fragment shader */
499 ctx->_ImageTransferState = 0x0;
500
501 /* map texture transfer */
502 dest = pipe_transfer_map(pipe, pt, 0, 0,
503 PIPE_TRANSFER_WRITE, 0, 0,
504 width, height, &transfer);
505
506
507 /* Put image into texture transfer.
508 * Note that the image is actually going to be upside down in
509 * the texture. We deal with that with texcoords.
510 */
511 success = _mesa_texstore(ctx, 2, /* dims */
512 baseInternalFormat, /* baseInternalFormat */
513 mformat, /* mesa_format */
514 transfer->stride, /* dstRowStride, bytes */
515 &dest, /* destSlices */
516 width, height, 1, /* size */
517 format, type, /* src format/type */
518 pixels, /* data source */
519 unpack);
520
521 /* unmap */
522 pipe_transfer_unmap(pipe, transfer);
523
524 assert(success);
525
526 /* restore */
527 ctx->_ImageTransferState = imageTransferStateSave;
528 }
529
530 _mesa_unmap_pbo_source(ctx, unpack);
531
532 return pt;
533 }
534
535
536 /**
537 * Draw quad with texcoords and optional color.
538 * Coords are gallium window coords with y=0=top.
539 * \param color may be null
540 * \param invertTex if true, flip texcoords vertically
541 */
542 static void
543 draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
544 GLfloat x1, GLfloat y1, const GLfloat *color,
545 GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
546 {
547 struct st_context *st = st_context(ctx);
548 struct pipe_context *pipe = st->pipe;
549 GLfloat (*verts)[3][4]; /* four verts, three attribs, XYZW */
550 struct pipe_resource *buf = NULL;
551 unsigned offset;
552
553 u_upload_alloc(st->uploader, 0, 4 * sizeof(verts[0]), &offset,
554 &buf, (void **) &verts);
555 if (!buf) {
556 return;
557 }
558
559 /* setup vertex data */
560 {
561 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
562 const GLfloat fb_width = (GLfloat) fb->Width;
563 const GLfloat fb_height = (GLfloat) fb->Height;
564 const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
565 const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
566 const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
567 const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
568 const GLfloat sLeft = 0.0f, sRight = maxXcoord;
569 const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
570 const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
571 GLuint i;
572
573 /* upper-left */
574 verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
575 verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
576
577 /* upper-right */
578 verts[1][0][0] = clip_x1;
579 verts[1][0][1] = clip_y0;
580
581 /* lower-right */
582 verts[2][0][0] = clip_x1;
583 verts[2][0][1] = clip_y1;
584
585 /* lower-left */
586 verts[3][0][0] = clip_x0;
587 verts[3][0][1] = clip_y1;
588
589 verts[0][1][0] = sLeft; /* v[0].attr[1].S */
590 verts[0][1][1] = tTop; /* v[0].attr[1].T */
591 verts[1][1][0] = sRight;
592 verts[1][1][1] = tTop;
593 verts[2][1][0] = sRight;
594 verts[2][1][1] = tBot;
595 verts[3][1][0] = sLeft;
596 verts[3][1][1] = tBot;
597
598 /* same for all verts: */
599 if (color) {
600 for (i = 0; i < 4; i++) {
601 verts[i][0][2] = z; /* v[i].attr[0].z */
602 verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
603 verts[i][2][0] = color[0]; /* v[i].attr[2].r */
604 verts[i][2][1] = color[1]; /* v[i].attr[2].g */
605 verts[i][2][2] = color[2]; /* v[i].attr[2].b */
606 verts[i][2][3] = color[3]; /* v[i].attr[2].a */
607 verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
608 verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
609 }
610 }
611 else {
612 for (i = 0; i < 4; i++) {
613 verts[i][0][2] = z; /*Z*/
614 verts[i][0][3] = 1.0f; /*W*/
615 verts[i][1][2] = 0.0f; /*R*/
616 verts[i][1][3] = 1.0f; /*Q*/
617 }
618 }
619 }
620
621 u_upload_unmap(st->uploader);
622 util_draw_vertex_buffer(pipe, st->cso_context, buf,
623 cso_get_aux_vertex_buffer_slot(st->cso_context),
624 offset,
625 PIPE_PRIM_QUADS,
626 4, /* verts */
627 3); /* attribs/vert */
628 pipe_resource_reference(&buf, NULL);
629 }
630
631
632
633 static void
634 draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
635 GLsizei width, GLsizei height,
636 GLfloat zoomX, GLfloat zoomY,
637 struct pipe_sampler_view **sv,
638 int num_sampler_view,
639 void *driver_vp,
640 void *driver_fp,
641 const GLfloat *color,
642 GLboolean invertTex,
643 GLboolean write_depth, GLboolean write_stencil)
644 {
645 struct st_context *st = st_context(ctx);
646 struct pipe_context *pipe = st->pipe;
647 struct cso_context *cso = st->cso_context;
648 GLfloat x0, y0, x1, y1;
649 GLsizei maxSize;
650 boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
651
652 /* limit checks */
653 /* XXX if DrawPixels image is larger than max texture size, break
654 * it up into chunks.
655 */
656 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
657 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
658 assert(width <= maxSize);
659 assert(height <= maxSize);
660
661 cso_save_rasterizer(cso);
662 cso_save_viewport(cso);
663 cso_save_fragment_samplers(cso);
664 cso_save_fragment_sampler_views(cso);
665 cso_save_fragment_shader(cso);
666 cso_save_stream_outputs(cso);
667 cso_save_vertex_shader(cso);
668 cso_save_tessctrl_shader(cso);
669 cso_save_tesseval_shader(cso);
670 cso_save_geometry_shader(cso);
671 cso_save_vertex_elements(cso);
672 cso_save_aux_vertex_buffer_slot(cso);
673 if (write_stencil) {
674 cso_save_depth_stencil_alpha(cso);
675 cso_save_blend(cso);
676 }
677
678 /* rasterizer state: just scissor */
679 {
680 struct pipe_rasterizer_state rasterizer;
681 memset(&rasterizer, 0, sizeof(rasterizer));
682 rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
683 ctx->Color._ClampFragmentColor;
684 rasterizer.half_pixel_center = 1;
685 rasterizer.bottom_edge_rule = 1;
686 rasterizer.depth_clip = !ctx->Transform.DepthClamp;
687 rasterizer.scissor = ctx->Scissor.EnableFlags;
688 cso_set_rasterizer(cso, &rasterizer);
689 }
690
691 if (write_stencil) {
692 /* Stencil writing bypasses the normal fragment pipeline to
693 * disable color writing and set stencil test to always pass.
694 */
695 struct pipe_depth_stencil_alpha_state dsa;
696 struct pipe_blend_state blend;
697
698 /* depth/stencil */
699 memset(&dsa, 0, sizeof(dsa));
700 dsa.stencil[0].enabled = 1;
701 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
702 dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
703 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
704 if (write_depth) {
705 /* writing depth+stencil: depth test always passes */
706 dsa.depth.enabled = 1;
707 dsa.depth.writemask = ctx->Depth.Mask;
708 dsa.depth.func = PIPE_FUNC_ALWAYS;
709 }
710 cso_set_depth_stencil_alpha(cso, &dsa);
711
712 /* blend (colormask) */
713 memset(&blend, 0, sizeof(blend));
714 cso_set_blend(cso, &blend);
715 }
716
717 /* fragment shader state: TEX lookup program */
718 cso_set_fragment_shader_handle(cso, driver_fp);
719
720 /* vertex shader state: position + texcoord pass-through */
721 cso_set_vertex_shader_handle(cso, driver_vp);
722
723 /* disable other shaders */
724 cso_set_tessctrl_shader_handle(cso, NULL);
725 cso_set_tesseval_shader_handle(cso, NULL);
726 cso_set_geometry_shader_handle(cso, NULL);
727
728 /* texture sampling state: */
729 {
730 struct pipe_sampler_state sampler;
731 const struct pipe_sampler_state *states[2] = {&sampler, &sampler};
732
733 memset(&sampler, 0, sizeof(sampler));
734 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
735 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
736 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
737 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
738 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
739 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
740 sampler.normalized_coords = normalized;
741
742 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT,
743 num_sampler_view > 1 ? 2 : 1, states);
744 }
745
746 /* viewport state: viewport matching window dims */
747 {
748 const float w = (float) ctx->DrawBuffer->Width;
749 const float h = (float) ctx->DrawBuffer->Height;
750 struct pipe_viewport_state vp;
751 vp.scale[0] = 0.5f * w;
752 vp.scale[1] = -0.5f * h;
753 vp.scale[2] = 0.5f;
754 vp.translate[0] = 0.5f * w;
755 vp.translate[1] = 0.5f * h;
756 vp.translate[2] = 0.5f;
757 cso_set_viewport(cso, &vp);
758 }
759
760 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
761 cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
762
763 /* texture state: */
764 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num_sampler_view, sv);
765
766 /* Compute Gallium window coords (y=0=top) with pixel zoom.
767 * Recall that these coords are transformed by the current
768 * vertex shader and viewport transformation.
769 */
770 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
771 y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
772 invertTex = !invertTex;
773 }
774
775 x0 = (GLfloat) x;
776 x1 = x + width * ctx->Pixel.ZoomX;
777 y0 = (GLfloat) y;
778 y1 = y + height * ctx->Pixel.ZoomY;
779
780 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
781 z = z * 2.0f - 1.0f;
782
783 draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
784 normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
785 normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
786
787 /* restore state */
788 cso_restore_rasterizer(cso);
789 cso_restore_viewport(cso);
790 cso_restore_fragment_samplers(cso);
791 cso_restore_fragment_sampler_views(cso);
792 cso_restore_fragment_shader(cso);
793 cso_restore_vertex_shader(cso);
794 cso_restore_tessctrl_shader(cso);
795 cso_restore_tesseval_shader(cso);
796 cso_restore_geometry_shader(cso);
797 cso_restore_vertex_elements(cso);
798 cso_restore_aux_vertex_buffer_slot(cso);
799 cso_restore_stream_outputs(cso);
800 if (write_stencil) {
801 cso_restore_depth_stencil_alpha(cso);
802 cso_restore_blend(cso);
803 }
804 }
805
806
807 /**
808 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
809 * can't use a fragment shader to write stencil values.
810 */
811 static void
812 draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
813 GLsizei width, GLsizei height, GLenum format, GLenum type,
814 const struct gl_pixelstore_attrib *unpack,
815 const GLvoid *pixels)
816 {
817 struct st_context *st = st_context(ctx);
818 struct pipe_context *pipe = st->pipe;
819 struct st_renderbuffer *strb;
820 enum pipe_transfer_usage usage;
821 struct pipe_transfer *pt;
822 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
823 ubyte *stmap;
824 struct gl_pixelstore_attrib clippedUnpack = *unpack;
825 GLubyte *sValues;
826 GLuint *zValues;
827
828 if (!zoom) {
829 if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
830 &clippedUnpack)) {
831 /* totally clipped */
832 return;
833 }
834 }
835
836 strb = st_renderbuffer(ctx->DrawBuffer->
837 Attachment[BUFFER_STENCIL].Renderbuffer);
838
839 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
840 y = ctx->DrawBuffer->Height - y - height;
841 }
842
843 if (format == GL_STENCIL_INDEX &&
844 _mesa_is_format_packed_depth_stencil(strb->Base.Format)) {
845 /* writing stencil to a combined depth+stencil buffer */
846 usage = PIPE_TRANSFER_READ_WRITE;
847 }
848 else {
849 usage = PIPE_TRANSFER_WRITE;
850 }
851
852 stmap = pipe_transfer_map(pipe, strb->texture,
853 strb->surface->u.tex.level,
854 strb->surface->u.tex.first_layer,
855 usage, x, y,
856 width, height, &pt);
857
858 pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
859 assert(pixels);
860
861 sValues = malloc(width * sizeof(GLubyte));
862 zValues = malloc(width * sizeof(GLuint));
863
864 if (sValues && zValues) {
865 GLint row;
866 for (row = 0; row < height; row++) {
867 GLfloat *zValuesFloat = (GLfloat*)zValues;
868 GLenum destType = GL_UNSIGNED_BYTE;
869 const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
870 width, height,
871 format, type,
872 row, 0);
873 _mesa_unpack_stencil_span(ctx, width, destType, sValues,
874 type, source, &clippedUnpack,
875 ctx->_ImageTransferState);
876
877 if (format == GL_DEPTH_STENCIL) {
878 GLenum ztype =
879 pt->resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
880 GL_FLOAT : GL_UNSIGNED_INT;
881
882 _mesa_unpack_depth_span(ctx, width, ztype, zValues,
883 (1 << 24) - 1, type, source,
884 &clippedUnpack);
885 }
886
887 if (zoom) {
888 _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
889 "zoom not complete");
890 }
891
892 {
893 GLint spanY;
894
895 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
896 spanY = height - row - 1;
897 }
898 else {
899 spanY = row;
900 }
901
902 /* now pack the stencil (and Z) values in the dest format */
903 switch (pt->resource->format) {
904 case PIPE_FORMAT_S8_UINT:
905 {
906 ubyte *dest = stmap + spanY * pt->stride;
907 assert(usage == PIPE_TRANSFER_WRITE);
908 memcpy(dest, sValues, width);
909 }
910 break;
911 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
912 if (format == GL_DEPTH_STENCIL) {
913 uint *dest = (uint *) (stmap + spanY * pt->stride);
914 GLint k;
915 assert(usage == PIPE_TRANSFER_WRITE);
916 for (k = 0; k < width; k++) {
917 dest[k] = zValues[k] | (sValues[k] << 24);
918 }
919 }
920 else {
921 uint *dest = (uint *) (stmap + spanY * pt->stride);
922 GLint k;
923 assert(usage == PIPE_TRANSFER_READ_WRITE);
924 for (k = 0; k < width; k++) {
925 dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
926 }
927 }
928 break;
929 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
930 if (format == GL_DEPTH_STENCIL) {
931 uint *dest = (uint *) (stmap + spanY * pt->stride);
932 GLint k;
933 assert(usage == PIPE_TRANSFER_WRITE);
934 for (k = 0; k < width; k++) {
935 dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
936 }
937 }
938 else {
939 uint *dest = (uint *) (stmap + spanY * pt->stride);
940 GLint k;
941 assert(usage == PIPE_TRANSFER_READ_WRITE);
942 for (k = 0; k < width; k++) {
943 dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
944 }
945 }
946 break;
947 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
948 if (format == GL_DEPTH_STENCIL) {
949 uint *dest = (uint *) (stmap + spanY * pt->stride);
950 GLfloat *destf = (GLfloat*)dest;
951 GLint k;
952 assert(usage == PIPE_TRANSFER_WRITE);
953 for (k = 0; k < width; k++) {
954 destf[k*2] = zValuesFloat[k];
955 dest[k*2+1] = sValues[k] & 0xff;
956 }
957 }
958 else {
959 uint *dest = (uint *) (stmap + spanY * pt->stride);
960 GLint k;
961 assert(usage == PIPE_TRANSFER_READ_WRITE);
962 for (k = 0; k < width; k++) {
963 dest[k*2+1] = sValues[k] & 0xff;
964 }
965 }
966 break;
967 default:
968 assert(0);
969 }
970 }
971 }
972 }
973 else {
974 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDrawPixels()");
975 }
976
977 free(sValues);
978 free(zValues);
979
980 _mesa_unmap_pbo_source(ctx, &clippedUnpack);
981
982 /* unmap the stencil buffer */
983 pipe_transfer_unmap(pipe, pt);
984 }
985
986
987 /**
988 * Get fragment program variant for a glDrawPixels or glCopyPixels
989 * command for RGBA data.
990 */
991 static struct st_fp_variant *
992 get_color_fp_variant(struct st_context *st)
993 {
994 struct gl_context *ctx = st->ctx;
995 struct st_fp_variant_key key;
996 struct st_fp_variant *fpv;
997
998 memset(&key, 0, sizeof(key));
999
1000 key.st = st;
1001 key.drawpixels = 1;
1002 key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
1003 ctx->Pixel.RedScale != 1.0 ||
1004 ctx->Pixel.GreenBias != 0.0 ||
1005 ctx->Pixel.GreenScale != 1.0 ||
1006 ctx->Pixel.BlueBias != 0.0 ||
1007 ctx->Pixel.BlueScale != 1.0 ||
1008 ctx->Pixel.AlphaBias != 0.0 ||
1009 ctx->Pixel.AlphaScale != 1.0);
1010 key.pixelMaps = ctx->Pixel.MapColorFlag;
1011 key.clamp_color = st->clamp_frag_color_in_shader &&
1012 st->ctx->Color._ClampFragmentColor;
1013
1014 fpv = st_get_fp_variant(st, st->fp, &key);
1015
1016 return fpv;
1017 }
1018
1019
1020 /**
1021 * Clamp glDrawPixels width and height to the maximum texture size.
1022 */
1023 static void
1024 clamp_size(struct pipe_context *pipe, GLsizei *width, GLsizei *height,
1025 struct gl_pixelstore_attrib *unpack)
1026 {
1027 const int maxSize =
1028 1 << (pipe->screen->get_param(pipe->screen,
1029 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
1030
1031 if (*width > maxSize) {
1032 if (unpack->RowLength == 0)
1033 unpack->RowLength = *width;
1034 *width = maxSize;
1035 }
1036 if (*height > maxSize) {
1037 *height = maxSize;
1038 }
1039 }
1040
1041
1042 /**
1043 * Called via ctx->Driver.DrawPixels()
1044 */
1045 static void
1046 st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
1047 GLsizei width, GLsizei height,
1048 GLenum format, GLenum type,
1049 const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
1050 {
1051 void *driver_vp, *driver_fp;
1052 struct st_context *st = st_context(ctx);
1053 const GLfloat *color;
1054 struct pipe_context *pipe = st->pipe;
1055 GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
1056 struct pipe_sampler_view *sv[2] = { NULL };
1057 int num_sampler_view = 1;
1058 struct gl_pixelstore_attrib clippedUnpack;
1059
1060 /* Mesa state should be up to date by now */
1061 assert(ctx->NewState == 0x0);
1062
1063 st_validate_state(st);
1064
1065 /* Limit the size of the glDrawPixels to the max texture size.
1066 * Strictly speaking, that's not correct but since we don't handle
1067 * larger images yet, this is better than crashing.
1068 */
1069 clippedUnpack = *unpack;
1070 unpack = &clippedUnpack;
1071 clamp_size(st->pipe, &width, &height, &clippedUnpack);
1072
1073 if (format == GL_DEPTH_STENCIL)
1074 write_stencil = write_depth = GL_TRUE;
1075 else if (format == GL_STENCIL_INDEX)
1076 write_stencil = GL_TRUE;
1077 else if (format == GL_DEPTH_COMPONENT)
1078 write_depth = GL_TRUE;
1079
1080 if (write_stencil &&
1081 !pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) {
1082 /* software fallback */
1083 draw_stencil_pixels(ctx, x, y, width, height, format, type,
1084 unpack, pixels);
1085 return;
1086 }
1087
1088 /*
1089 * Get vertex/fragment shaders
1090 */
1091 if (write_depth || write_stencil) {
1092 driver_fp = get_drawpix_z_stencil_program(st, write_depth,
1093 write_stencil);
1094 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1095 color = ctx->Current.RasterColor;
1096 }
1097 else {
1098 struct st_fp_variant *fpv = get_color_fp_variant(st);
1099
1100 driver_fp = fpv->driver_shader;
1101 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1102
1103 color = NULL;
1104 if (st->pixel_xfer.pixelmap_enabled) {
1105 pipe_sampler_view_reference(&sv[1],
1106 st->pixel_xfer.pixelmap_sampler_view);
1107 num_sampler_view++;
1108 }
1109
1110 /* update fragment program constants */
1111 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1112 }
1113
1114 /* draw with textured quad */
1115 {
1116 struct pipe_resource *pt
1117 = make_texture(st, width, height, format, type, unpack, pixels);
1118 if (pt) {
1119 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1120
1121 if (sv[0]) {
1122 /* Create a second sampler view to read stencil.
1123 * The stencil is written using the shader stencil export
1124 * functionality. */
1125 if (write_stencil) {
1126 enum pipe_format stencil_format =
1127 util_format_stencil_only(pt->format);
1128 /* we should not be doing pixel map/transfer (see above) */
1129 assert(num_sampler_view == 1);
1130 sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
1131 stencil_format);
1132 num_sampler_view++;
1133 }
1134
1135 draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
1136 width, height,
1137 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1138 sv,
1139 num_sampler_view,
1140 driver_vp,
1141 driver_fp,
1142 color, GL_FALSE, write_depth, write_stencil);
1143 pipe_sampler_view_reference(&sv[0], NULL);
1144 if (num_sampler_view > 1)
1145 pipe_sampler_view_reference(&sv[1], NULL);
1146 }
1147 pipe_resource_reference(&pt, NULL);
1148 }
1149 }
1150 }
1151
1152
1153
1154 /**
1155 * Software fallback for glCopyPixels(GL_STENCIL).
1156 */
1157 static void
1158 copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1159 GLsizei width, GLsizei height,
1160 GLint dstx, GLint dsty)
1161 {
1162 struct st_renderbuffer *rbDraw;
1163 struct pipe_context *pipe = st_context(ctx)->pipe;
1164 enum pipe_transfer_usage usage;
1165 struct pipe_transfer *ptDraw;
1166 ubyte *drawMap;
1167 ubyte *buffer;
1168 int i;
1169
1170 buffer = malloc(width * height * sizeof(ubyte));
1171 if (!buffer) {
1172 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
1173 return;
1174 }
1175
1176 /* Get the dest renderbuffer */
1177 rbDraw = st_renderbuffer(ctx->DrawBuffer->
1178 Attachment[BUFFER_STENCIL].Renderbuffer);
1179
1180 /* this will do stencil pixel transfer ops */
1181 _mesa_readpixels(ctx, srcx, srcy, width, height,
1182 GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
1183 &ctx->DefaultPacking, buffer);
1184
1185 if (0) {
1186 /* debug code: dump stencil values */
1187 GLint row, col;
1188 for (row = 0; row < height; row++) {
1189 printf("%3d: ", row);
1190 for (col = 0; col < width; col++) {
1191 printf("%02x ", buffer[col + row * width]);
1192 }
1193 printf("\n");
1194 }
1195 }
1196
1197 if (_mesa_is_format_packed_depth_stencil(rbDraw->Base.Format))
1198 usage = PIPE_TRANSFER_READ_WRITE;
1199 else
1200 usage = PIPE_TRANSFER_WRITE;
1201
1202 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1203 dsty = rbDraw->Base.Height - dsty - height;
1204 }
1205
1206 assert(util_format_get_blockwidth(rbDraw->texture->format) == 1);
1207 assert(util_format_get_blockheight(rbDraw->texture->format) == 1);
1208
1209 /* map the stencil buffer */
1210 drawMap = pipe_transfer_map(pipe,
1211 rbDraw->texture,
1212 rbDraw->surface->u.tex.level,
1213 rbDraw->surface->u.tex.first_layer,
1214 usage, dstx, dsty,
1215 width, height, &ptDraw);
1216
1217 /* draw */
1218 /* XXX PixelZoom not handled yet */
1219 for (i = 0; i < height; i++) {
1220 ubyte *dst;
1221 const ubyte *src;
1222 int y;
1223
1224 y = i;
1225
1226 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1227 y = height - y - 1;
1228 }
1229
1230 dst = drawMap + y * ptDraw->stride;
1231 src = buffer + i * width;
1232
1233 _mesa_pack_ubyte_stencil_row(rbDraw->Base.Format, width, src, dst);
1234 }
1235
1236 free(buffer);
1237
1238 /* unmap the stencil buffer */
1239 pipe_transfer_unmap(pipe, ptDraw);
1240 }
1241
1242
1243 /**
1244 * Return renderbuffer to use for reading color pixels for glCopyPixels
1245 */
1246 static struct st_renderbuffer *
1247 st_get_color_read_renderbuffer(struct gl_context *ctx)
1248 {
1249 struct gl_framebuffer *fb = ctx->ReadBuffer;
1250 struct st_renderbuffer *strb =
1251 st_renderbuffer(fb->_ColorReadBuffer);
1252
1253 return strb;
1254 }
1255
1256
1257 /**
1258 * Try to do a glCopyPixels for simple cases with a blit by calling
1259 * pipe->blit().
1260 *
1261 * We can do this when we're copying color pixels (depth/stencil
1262 * eventually) with no pixel zoom, no pixel transfer ops, no
1263 * per-fragment ops, and the src/dest regions don't overlap.
1264 */
1265 static GLboolean
1266 blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1267 GLsizei width, GLsizei height,
1268 GLint dstx, GLint dsty, GLenum type)
1269 {
1270 struct st_context *st = st_context(ctx);
1271 struct pipe_context *pipe = st->pipe;
1272 struct pipe_screen *screen = pipe->screen;
1273 struct gl_pixelstore_attrib pack, unpack;
1274 GLint readX, readY, readW, readH, drawX, drawY, drawW, drawH;
1275
1276 if (type == GL_COLOR &&
1277 ctx->Pixel.ZoomX == 1.0 &&
1278 ctx->Pixel.ZoomY == 1.0 &&
1279 ctx->_ImageTransferState == 0x0 &&
1280 !ctx->Color.BlendEnabled &&
1281 !ctx->Color.AlphaEnabled &&
1282 !ctx->Depth.Test &&
1283 !ctx->Fog.Enabled &&
1284 !ctx->Stencil.Enabled &&
1285 !ctx->FragmentProgram.Enabled &&
1286 !ctx->VertexProgram.Enabled &&
1287 !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] &&
1288 ctx->DrawBuffer->_NumColorDrawBuffers == 1 &&
1289 !ctx->Query.CondRenderQuery &&
1290 !ctx->Query.CurrentOcclusionObject) {
1291 struct st_renderbuffer *rbRead, *rbDraw;
1292
1293 /*
1294 * Clip the read region against the src buffer bounds.
1295 * We'll still allocate a temporary buffer/texture for the original
1296 * src region size but we'll only read the region which is on-screen.
1297 * This may mean that we draw garbage pixels into the dest region, but
1298 * that's expected.
1299 */
1300 readX = srcx;
1301 readY = srcy;
1302 readW = width;
1303 readH = height;
1304 pack = ctx->DefaultPacking;
1305 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
1306 return GL_TRUE; /* all done */
1307
1308 /* clip against dest buffer bounds and scissor box */
1309 drawX = dstx + pack.SkipPixels;
1310 drawY = dsty + pack.SkipRows;
1311 unpack = pack;
1312 if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
1313 return GL_TRUE; /* all done */
1314
1315 readX = readX - pack.SkipPixels + unpack.SkipPixels;
1316 readY = readY - pack.SkipRows + unpack.SkipRows;
1317
1318 drawW = readW;
1319 drawH = readH;
1320
1321 rbRead = st_get_color_read_renderbuffer(ctx);
1322 rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
1323
1324 /* Flip src/dst position depending on the orientation of buffers. */
1325 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1326 readY = rbRead->Base.Height - readY;
1327 readH = -readH;
1328 }
1329
1330 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
1331 /* We can't flip the destination for pipe->blit, so we only adjust
1332 * its position and flip the source.
1333 */
1334 drawY = rbDraw->Base.Height - drawY - drawH;
1335 readY += readH;
1336 readH = -readH;
1337 }
1338
1339 if (rbRead != rbDraw ||
1340 !_mesa_regions_overlap(readX, readY, readX + readW, readY + readH,
1341 drawX, drawY, drawX + drawW, drawY + drawH)) {
1342 struct pipe_blit_info blit;
1343
1344 memset(&blit, 0, sizeof(blit));
1345 blit.src.resource = rbRead->texture;
1346 blit.src.level = rbRead->surface->u.tex.level;
1347 blit.src.format = rbRead->texture->format;
1348 blit.src.box.x = readX;
1349 blit.src.box.y = readY;
1350 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1351 blit.src.box.width = readW;
1352 blit.src.box.height = readH;
1353 blit.src.box.depth = 1;
1354 blit.dst.resource = rbDraw->texture;
1355 blit.dst.level = rbDraw->surface->u.tex.level;
1356 blit.dst.format = rbDraw->texture->format;
1357 blit.dst.box.x = drawX;
1358 blit.dst.box.y = drawY;
1359 blit.dst.box.z = rbDraw->surface->u.tex.first_layer;
1360 blit.dst.box.width = drawW;
1361 blit.dst.box.height = drawH;
1362 blit.dst.box.depth = 1;
1363 blit.mask = PIPE_MASK_RGBA;
1364 blit.filter = PIPE_TEX_FILTER_NEAREST;
1365
1366 if (screen->is_format_supported(screen, blit.src.format,
1367 blit.src.resource->target,
1368 blit.src.resource->nr_samples,
1369 PIPE_BIND_SAMPLER_VIEW) &&
1370 screen->is_format_supported(screen, blit.dst.format,
1371 blit.dst.resource->target,
1372 blit.dst.resource->nr_samples,
1373 PIPE_BIND_RENDER_TARGET)) {
1374 pipe->blit(pipe, &blit);
1375 return GL_TRUE;
1376 }
1377 }
1378 }
1379
1380 return GL_FALSE;
1381 }
1382
1383
1384 static void
1385 st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
1386 GLsizei width, GLsizei height,
1387 GLint dstx, GLint dsty, GLenum type)
1388 {
1389 struct st_context *st = st_context(ctx);
1390 struct pipe_context *pipe = st->pipe;
1391 struct pipe_screen *screen = pipe->screen;
1392 struct st_renderbuffer *rbRead;
1393 void *driver_vp, *driver_fp;
1394 struct pipe_resource *pt;
1395 struct pipe_sampler_view *sv[2] = { NULL };
1396 int num_sampler_view = 1;
1397 GLfloat *color;
1398 enum pipe_format srcFormat;
1399 unsigned srcBind;
1400 GLboolean invertTex = GL_FALSE;
1401 GLint readX, readY, readW, readH;
1402 struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
1403
1404 st_validate_state(st);
1405
1406 if (type == GL_DEPTH_STENCIL) {
1407 /* XXX make this more efficient */
1408 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_STENCIL);
1409 st_CopyPixels(ctx, srcx, srcy, width, height, dstx, dsty, GL_DEPTH);
1410 return;
1411 }
1412
1413 if (type == GL_STENCIL) {
1414 /* can't use texturing to do stencil */
1415 copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
1416 return;
1417 }
1418
1419 if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
1420 return;
1421
1422 /*
1423 * The subsequent code implements glCopyPixels by copying the source
1424 * pixels into a temporary texture that's then applied to a textured quad.
1425 * When we draw the textured quad, all the usual per-fragment operations
1426 * are handled.
1427 */
1428
1429
1430 /*
1431 * Get vertex/fragment shaders
1432 */
1433 if (type == GL_COLOR) {
1434 struct st_fp_variant *fpv = get_color_fp_variant(st);
1435
1436 rbRead = st_get_color_read_renderbuffer(ctx);
1437 color = NULL;
1438
1439 driver_fp = fpv->driver_shader;
1440 driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
1441
1442 if (st->pixel_xfer.pixelmap_enabled) {
1443 pipe_sampler_view_reference(&sv[1],
1444 st->pixel_xfer.pixelmap_sampler_view);
1445 num_sampler_view++;
1446 }
1447
1448 /* update fragment program constants */
1449 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
1450 }
1451 else {
1452 assert(type == GL_DEPTH);
1453 rbRead = st_renderbuffer(ctx->ReadBuffer->
1454 Attachment[BUFFER_DEPTH].Renderbuffer);
1455 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
1456
1457 driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
1458 driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
1459 }
1460
1461 /* Choose the format for the temporary texture. */
1462 srcFormat = rbRead->texture->format;
1463 srcBind = PIPE_BIND_SAMPLER_VIEW |
1464 (type == GL_COLOR ? PIPE_BIND_RENDER_TARGET : PIPE_BIND_DEPTH_STENCIL);
1465
1466 if (!screen->is_format_supported(screen, srcFormat, st->internal_target, 0,
1467 srcBind)) {
1468 /* srcFormat is non-renderable. Find a compatible renderable format. */
1469 if (type == GL_DEPTH) {
1470 srcFormat = st_choose_format(st, GL_DEPTH_COMPONENT, GL_NONE,
1471 GL_NONE, st->internal_target, 0,
1472 srcBind, FALSE);
1473 }
1474 else {
1475 assert(type == GL_COLOR);
1476
1477 if (util_format_is_float(srcFormat)) {
1478 srcFormat = st_choose_format(st, GL_RGBA32F, GL_NONE,
1479 GL_NONE, st->internal_target, 0,
1480 srcBind, FALSE);
1481 }
1482 else if (util_format_is_pure_sint(srcFormat)) {
1483 srcFormat = st_choose_format(st, GL_RGBA32I, GL_NONE,
1484 GL_NONE, st->internal_target, 0,
1485 srcBind, FALSE);
1486 }
1487 else if (util_format_is_pure_uint(srcFormat)) {
1488 srcFormat = st_choose_format(st, GL_RGBA32UI, GL_NONE,
1489 GL_NONE, st->internal_target, 0,
1490 srcBind, FALSE);
1491 }
1492 else if (util_format_is_snorm(srcFormat)) {
1493 srcFormat = st_choose_format(st, GL_RGBA16_SNORM, GL_NONE,
1494 GL_NONE, st->internal_target, 0,
1495 srcBind, FALSE);
1496 }
1497 else {
1498 srcFormat = st_choose_format(st, GL_RGBA, GL_NONE,
1499 GL_NONE, st->internal_target, 0,
1500 srcBind, FALSE);
1501 }
1502 }
1503
1504 if (srcFormat == PIPE_FORMAT_NONE) {
1505 assert(0 && "cannot choose a format for src of CopyPixels");
1506 return;
1507 }
1508 }
1509
1510 /* Invert src region if needed */
1511 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1512 srcy = ctx->ReadBuffer->Height - srcy - height;
1513 invertTex = !invertTex;
1514 }
1515
1516 /* Clip the read region against the src buffer bounds.
1517 * We'll still allocate a temporary buffer/texture for the original
1518 * src region size but we'll only read the region which is on-screen.
1519 * This may mean that we draw garbage pixels into the dest region, but
1520 * that's expected.
1521 */
1522 readX = srcx;
1523 readY = srcy;
1524 readW = width;
1525 readH = height;
1526 if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) {
1527 /* The source region is completely out of bounds. Do nothing.
1528 * The GL spec says "Results of copies from outside the window,
1529 * or from regions of the window that are not exposed, are
1530 * hardware dependent and undefined."
1531 */
1532 return;
1533 }
1534
1535 readW = MAX2(0, readW);
1536 readH = MAX2(0, readH);
1537
1538 /* Allocate the temporary texture. */
1539 pt = alloc_texture(st, width, height, srcFormat, srcBind);
1540 if (!pt)
1541 return;
1542
1543 sv[0] = st_create_texture_sampler_view(st->pipe, pt);
1544 if (!sv[0]) {
1545 pipe_resource_reference(&pt, NULL);
1546 return;
1547 }
1548
1549 /* Copy the src region to the temporary texture. */
1550 {
1551 struct pipe_blit_info blit;
1552
1553 memset(&blit, 0, sizeof(blit));
1554 blit.src.resource = rbRead->texture;
1555 blit.src.level = rbRead->surface->u.tex.level;
1556 blit.src.format = rbRead->texture->format;
1557 blit.src.box.x = readX;
1558 blit.src.box.y = readY;
1559 blit.src.box.z = rbRead->surface->u.tex.first_layer;
1560 blit.src.box.width = readW;
1561 blit.src.box.height = readH;
1562 blit.src.box.depth = 1;
1563 blit.dst.resource = pt;
1564 blit.dst.level = 0;
1565 blit.dst.format = pt->format;
1566 blit.dst.box.x = pack.SkipPixels;
1567 blit.dst.box.y = pack.SkipRows;
1568 blit.dst.box.z = 0;
1569 blit.dst.box.width = readW;
1570 blit.dst.box.height = readH;
1571 blit.dst.box.depth = 1;
1572 blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
1573 blit.filter = PIPE_TEX_FILTER_NEAREST;
1574
1575 pipe->blit(pipe, &blit);
1576 }
1577
1578 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1579 * textured quad with that texture.
1580 */
1581 draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
1582 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
1583 sv,
1584 num_sampler_view,
1585 driver_vp,
1586 driver_fp,
1587 color, invertTex, GL_FALSE, GL_FALSE);
1588
1589 pipe_resource_reference(&pt, NULL);
1590 pipe_sampler_view_reference(&sv[0], NULL);
1591 }
1592
1593
1594
1595 void st_init_drawpixels_functions(struct dd_function_table *functions)
1596 {
1597 functions->DrawPixels = st_DrawPixels;
1598 functions->CopyPixels = st_CopyPixels;
1599 }
1600
1601
1602 void
1603 st_destroy_drawpix(struct st_context *st)
1604 {
1605 GLuint i;
1606
1607 for (i = 0; i < ARRAY_SIZE(st->drawpix.zs_shaders); i++) {
1608 if (st->drawpix.zs_shaders[i])
1609 cso_delete_fragment_shader(st->cso_context,
1610 st->drawpix.zs_shaders[i]);
1611 }
1612
1613 st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
1614 if (st->drawpix.vert_shaders[0])
1615 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[0]);
1616 if (st->drawpix.vert_shaders[1])
1617 cso_delete_vertex_shader(st->cso_context, st->drawpix.vert_shaders[1]);
1618 }