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