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