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