1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
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"
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"
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"
69 #include "pipe/p_context.h"
70 #include "pipe/p_defines.h"
71 #include "tgsi/tgsi_ureg.h"
72 #include "util/u_format.h"
73 #include "util/u_inlines.h"
74 #include "util/u_math.h"
75 #include "util/u_tile.h"
76 #include "cso_cache/cso_context.h"
80 * We have a simple glDrawPixels cache to try to optimize the case where the
81 * same image is drawn over and over again. It basically works as follows:
83 * 1. After we construct a texture map with the image and draw it, we do
84 * not discard the texture. We keep it around, plus we note the
85 * glDrawPixels width, height, format, etc. parameters and keep a copy
86 * of the image in a malloc'd buffer.
88 * 2. On the next glDrawPixels we check if the parameters match the previous
89 * call. If those match, we check if the image matches the previous image
90 * via a memcmp() call. If everything matches, we re-use the previous
91 * texture, thereby avoiding the cost creating a new texture and copying
94 * The effectiveness of this cache depends upon:
95 * 1. If the memcmp() finds a difference, it happens relatively quickly.
96 Hopefully, not just the last pixels differ!
97 * 2. If the memcmp() finds no difference, doing that check is faster than
98 * creating and loading a texture.
101 * 1. We don't support any pixel unpacking parameters.
102 * 2. We don't try to cache images in Pixel Buffer Objects.
103 * 3. Instead of saving the whole image, perhaps some sort of reliable
104 * checksum function could be used instead.
106 #define USE_DRAWPIXELS_CACHE 1
111 * Create fragment program that does a TEX() instruction to get a Z and/or
112 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
113 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
114 * Pass fragment color through as-is.
116 * \return CSO of the fragment shader.
119 get_drawpix_z_stencil_program(struct st_context
*st
,
120 GLboolean write_depth
,
121 GLboolean write_stencil
)
123 struct ureg_program
*ureg
;
124 struct ureg_src depth_sampler
, stencil_sampler
;
125 struct ureg_src texcoord
, color
;
126 struct ureg_dst out_color
, out_depth
, out_stencil
;
127 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
130 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
132 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
133 /* already have the proper shader */
134 return st
->drawpix
.zs_shaders
[shaderIndex
];
137 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
141 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
144 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
145 TGSI_INTERPOLATE_COLOR
);
146 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
148 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
149 ureg_DECL_sampler_view(ureg
, 0, TGSI_TEXTURE_2D
,
150 TGSI_RETURN_TYPE_FLOAT
,
151 TGSI_RETURN_TYPE_FLOAT
,
152 TGSI_RETURN_TYPE_FLOAT
,
153 TGSI_RETURN_TYPE_FLOAT
);
154 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
158 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
159 ureg_DECL_sampler_view(ureg
, 1, TGSI_TEXTURE_2D
,
160 TGSI_RETURN_TYPE_UINT
,
161 TGSI_RETURN_TYPE_UINT
,
162 TGSI_RETURN_TYPE_UINT
,
163 TGSI_RETURN_TYPE_UINT
);
164 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
167 texcoord
= ureg_DECL_fs_input(ureg
,
168 st
->needs_texcoord_semantic
?
169 TGSI_SEMANTIC_TEXCOORD
:
170 TGSI_SEMANTIC_GENERIC
,
171 0, TGSI_INTERPOLATE_LINEAR
);
174 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
175 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
176 ureg_MOV(ureg
, out_color
, color
);
180 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
181 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
184 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
186 /* save the new shader */
187 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
193 * Create a simple vertex shader that just passes through the
194 * vertex position and texcoord (and optionally, color).
197 make_passthrough_vertex_shader(struct st_context
*st
,
200 const enum tgsi_semantic texcoord_semantic
= st
->needs_texcoord_semantic
?
201 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
203 if (!st
->drawpix
.vert_shaders
[passColor
]) {
204 struct ureg_program
*ureg
= ureg_create( PIPE_SHADER_VERTEX
);
209 /* MOV result.pos, vertex.pos; */
211 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
212 ureg_DECL_vs_input( ureg
, 0 ));
215 /* MOV result.color0, vertex.attr[1]; */
217 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
218 ureg_DECL_vs_input( ureg
, 1 ));
221 /* MOV result.texcoord0, vertex.attr[2]; */
223 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
224 ureg_DECL_vs_input( ureg
, 2 ));
228 st
->drawpix
.vert_shaders
[passColor
] =
229 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
232 return st
->drawpix
.vert_shaders
[passColor
];
237 * Return a texture internalFormat for drawing/copying an image
238 * of the given format and type.
241 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
244 case GL_DEPTH_COMPONENT
:
246 case GL_UNSIGNED_SHORT
:
247 return GL_DEPTH_COMPONENT16
;
249 case GL_UNSIGNED_INT
:
250 return GL_DEPTH_COMPONENT32
;
253 if (ctx
->Extensions
.ARB_depth_buffer_float
)
254 return GL_DEPTH_COMPONENT32F
;
256 return GL_DEPTH_COMPONENT
;
259 return GL_DEPTH_COMPONENT
;
262 case GL_DEPTH_STENCIL
:
264 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
265 return GL_DEPTH32F_STENCIL8
;
267 case GL_UNSIGNED_INT_24_8
:
269 return GL_DEPTH24_STENCIL8
;
272 case GL_STENCIL_INDEX
:
273 return GL_STENCIL_INDEX
;
276 if (_mesa_is_enum_format_integer(format
)) {
280 case GL_UNSIGNED_BYTE
:
284 case GL_UNSIGNED_SHORT
:
288 case GL_UNSIGNED_INT
:
291 assert(0 && "Unexpected type in internal_format()");
292 return GL_RGBA_INTEGER
;
297 case GL_UNSIGNED_BYTE
:
298 case GL_UNSIGNED_INT_8_8_8_8
:
299 case GL_UNSIGNED_INT_8_8_8_8_REV
:
303 case GL_UNSIGNED_BYTE_3_3_2
:
304 case GL_UNSIGNED_BYTE_2_3_3_REV
:
307 case GL_UNSIGNED_SHORT_4_4_4_4
:
308 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
311 case GL_UNSIGNED_SHORT_5_6_5
:
312 case GL_UNSIGNED_SHORT_5_6_5_REV
:
315 case GL_UNSIGNED_SHORT_5_5_5_1
:
316 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
319 case GL_UNSIGNED_INT_10_10_10_2
:
320 case GL_UNSIGNED_INT_2_10_10_10_REV
:
323 case GL_UNSIGNED_SHORT
:
324 case GL_UNSIGNED_INT
:
329 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
334 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
336 case GL_HALF_FLOAT_ARB
:
338 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
339 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
344 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
345 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
347 case GL_UNSIGNED_INT_5_9_9_9_REV
:
348 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
351 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
352 assert(ctx
->Extensions
.EXT_packed_float
);
353 return GL_R11F_G11F_B10F
;
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.
365 static struct pipe_resource
*
366 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
367 enum pipe_format texFormat
, unsigned bind
)
369 struct pipe_resource
*pt
;
371 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
372 width
, height
, 1, 1, 0, bind
);
379 * Search the cache for an image which matches the given parameters.
380 * \return pipe_resource pointer if found, NULL if not found.
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
,
389 struct pipe_resource
*pt
= NULL
;
390 const GLint bpp
= _mesa_bytes_per_pixel(format
, type
);
393 if ((unpack
->RowLength
!= 0 && unpack
->RowLength
!= width
) ||
394 unpack
->SkipPixels
!= 0 ||
395 unpack
->SkipRows
!= 0 ||
397 _mesa_is_bufferobj(unpack
->BufferObj
)) {
398 /* we don't allow non-default pixel unpacking values */
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
];
406 if (width
== entry
->width
&&
407 height
== entry
->height
&&
408 format
== entry
->format
&&
409 type
== entry
->type
&&
410 pixels
== entry
->user_pointer
&&
412 assert(entry
->texture
);
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.
422 assert(pt
->reference
.count
>= 2);
424 /* update the age of this entry */
425 entry
->age
= ++st
->drawpix_cache
.age
;
432 /* no cache match found */
438 * Find the oldest entry in the glDrawPixels cache. We'll replace this
439 * one when we need to store a new image.
441 static struct drawpix_cache_entry
*
442 find_oldest_drawpixels_cache_entry(struct st_context
*st
)
444 unsigned oldest_age
= ~0u, oldest_index
= ~0u;
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
;
456 assert(oldest_index
!= ~0u);
458 return &st
->drawpix_cache
.entries
[oldest_index
];
463 * Try to save the given glDrawPixels image in the cache.
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
,
471 struct pipe_resource
*pt
)
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
);
480 entry
->width
= width
;
481 entry
->height
= height
;
482 entry
->format
= format
;
484 entry
->user_pointer
= pixels
;
486 entry
->image
= malloc(width
* height
* bpp
);
488 memcpy(entry
->image
, pixels
, width
* height
* bpp
);
489 pipe_resource_reference(&entry
->texture
, pt
);
490 entry
->age
= ++st
->drawpix_cache
.age
;
493 /* out of memory, free/disable cached texture */
496 pipe_resource_reference(&entry
->texture
, NULL
);
503 * Make texture containing an image for glDrawPixels image.
504 * If 'pixels' is NULL, leave the texture image data undefined.
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
,
512 struct gl_context
*ctx
= st
->ctx
;
513 struct pipe_context
*pipe
= st
->pipe
;
515 struct pipe_resource
*pt
= NULL
;
516 enum pipe_format pipeFormat
;
517 GLenum baseInternalFormat
;
519 #if USE_DRAWPIXELS_CACHE
520 pt
= search_drawpixels_cache(st
, width
, height
, format
, type
,
527 /* Choose a pixel format for the temp texture which will hold the
530 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
531 format
, type
, unpack
->SwapBytes
);
533 if (pipeFormat
== PIPE_FORMAT_NONE
) {
534 /* Use the generic approach. */
535 GLenum intFormat
= internal_format(ctx
, format
, type
);
537 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
538 st
->internal_target
, 0,
539 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
540 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
543 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
544 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
546 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
550 /* alloc temporary texture */
551 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
553 _mesa_unmap_pbo_source(ctx
, unpack
);
558 struct pipe_transfer
*transfer
;
560 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
562 /* we'll do pixel transfer in a fragment shader */
563 ctx
->_ImageTransferState
= 0x0;
565 /* map texture transfer */
566 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
567 PIPE_TRANSFER_WRITE
, 0, 0,
568 width
, height
, &transfer
);
571 /* Put image into texture transfer.
572 * Note that the image is actually going to be upside down in
573 * the texture. We deal with that with texcoords.
575 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
576 && type
== GL_UNSIGNED_BYTE
) {
577 /* Use a memcpy-based texstore to avoid software pixel swizzling.
578 * We'll do the necessary swizzling with the pipe_sampler_view to
579 * give much better performance.
580 * XXX in the future, expand this to accomodate more format and
583 _mesa_memcpy_texture(ctx
, 2,
584 mformat
, /* mesa_format */
585 transfer
->stride
, /* dstRowStride, bytes */
586 &dest
, /* destSlices */
587 width
, height
, 1, /* size */
588 format
, type
, /* src format/type */
589 pixels
, /* data source */
593 bool MAYBE_UNUSED success
;
594 success
= _mesa_texstore(ctx
, 2, /* dims */
595 baseInternalFormat
, /* baseInternalFormat */
596 mformat
, /* mesa_format */
597 transfer
->stride
, /* dstRowStride, bytes */
598 &dest
, /* destSlices */
599 width
, height
, 1, /* size */
600 format
, type
, /* src format/type */
601 pixels
, /* data source */
608 pipe_transfer_unmap(pipe
, transfer
);
611 ctx
->_ImageTransferState
= imageTransferStateSave
;
614 _mesa_unmap_pbo_source(ctx
, unpack
);
616 #if USE_DRAWPIXELS_CACHE
617 cache_drawpixels_image(st
, width
, height
, format
, type
, unpack
, pixels
, pt
);
625 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
626 GLsizei width
, GLsizei height
,
627 GLfloat zoomX
, GLfloat zoomY
,
628 struct pipe_sampler_view
**sv
,
629 int num_sampler_view
,
632 struct st_fp_variant
*fpv
,
633 const GLfloat
*color
,
635 GLboolean write_depth
, GLboolean write_stencil
)
637 struct st_context
*st
= st_context(ctx
);
638 struct pipe_context
*pipe
= st
->pipe
;
639 struct cso_context
*cso
= st
->cso_context
;
640 const unsigned fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
641 const unsigned fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
642 GLfloat x0
, y0
, x1
, y1
;
643 GLsizei MAYBE_UNUSED maxSize
;
644 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
645 unsigned cso_state_mask
;
647 assert(sv
[0]->texture
->target
== st
->internal_target
);
650 /* XXX if DrawPixels image is larger than max texture size, break
653 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
654 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
655 assert(width
<= maxSize
);
656 assert(height
<= maxSize
);
658 cso_state_mask
= (CSO_BIT_RASTERIZER
|
660 CSO_BIT_FRAGMENT_SAMPLERS
|
661 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
662 CSO_BIT_STREAM_OUTPUTS
|
663 CSO_BIT_VERTEX_ELEMENTS
|
664 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
665 CSO_BITS_ALL_SHADERS
);
667 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
670 cso_save_state(cso
, cso_state_mask
);
672 /* rasterizer state: just scissor */
674 struct pipe_rasterizer_state rasterizer
;
675 memset(&rasterizer
, 0, sizeof(rasterizer
));
676 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
677 ctx
->Color
._ClampFragmentColor
;
678 rasterizer
.half_pixel_center
= 1;
679 rasterizer
.bottom_edge_rule
= 1;
680 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
681 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
682 cso_set_rasterizer(cso
, &rasterizer
);
686 /* Stencil writing bypasses the normal fragment pipeline to
687 * disable color writing and set stencil test to always pass.
689 struct pipe_depth_stencil_alpha_state dsa
;
690 struct pipe_blend_state blend
;
693 memset(&dsa
, 0, sizeof(dsa
));
694 dsa
.stencil
[0].enabled
= 1;
695 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
696 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
697 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
699 /* writing depth+stencil: depth test always passes */
700 dsa
.depth
.enabled
= 1;
701 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
702 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
704 cso_set_depth_stencil_alpha(cso
, &dsa
);
706 /* blend (colormask) */
707 memset(&blend
, 0, sizeof(blend
));
708 cso_set_blend(cso
, &blend
);
711 /* fragment shader state: TEX lookup program */
712 cso_set_fragment_shader_handle(cso
, driver_fp
);
714 /* vertex shader state: position + texcoord pass-through */
715 cso_set_vertex_shader_handle(cso
, driver_vp
);
717 /* disable other shaders */
718 cso_set_tessctrl_shader_handle(cso
, NULL
);
719 cso_set_tesseval_shader_handle(cso
, NULL
);
720 cso_set_geometry_shader_handle(cso
, NULL
);
722 /* user samplers, plus the drawpix samplers */
724 struct pipe_sampler_state sampler
;
726 memset(&sampler
, 0, sizeof(sampler
));
727 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
728 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
729 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
730 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
731 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
732 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
733 sampler
.normalized_coords
= normalized
;
736 /* drawing a color image */
737 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
738 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
739 fpv
->pixelmap_sampler
+ 1,
740 st
->state
.num_frag_samplers
);
743 for (i
= 0; i
< st
->state
.num_frag_samplers
; i
++)
744 samplers
[i
] = &st
->state
.frag_samplers
[i
];
746 samplers
[fpv
->drawpix_sampler
] = &sampler
;
748 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
750 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
752 /* drawing a depth/stencil image */
753 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
755 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
759 /* user textures, plus the drawpix textures */
761 /* drawing a color image */
762 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
763 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
764 fpv
->pixelmap_sampler
+ 1,
765 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
767 memcpy(sampler_views
, st
->state
.frag_sampler_views
,
768 sizeof(sampler_views
));
770 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
772 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
773 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
775 /* drawing a depth/stencil image */
776 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
779 /* viewport state: viewport matching window dims */
780 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
782 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
783 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
785 /* Compute Gallium window coords (y=0=top) with pixel zoom.
786 * Recall that these coords are transformed by the current
787 * vertex shader and viewport transformation.
789 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
790 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
791 invertTex
= !invertTex
;
795 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
797 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
799 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
803 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
804 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
805 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
806 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
807 const float maxXcoord
= normalized
?
808 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
809 const float maxYcoord
= normalized
810 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
811 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
812 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
813 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
815 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
816 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
817 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
822 cso_restore_state(cso
);
827 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
828 * can't use a fragment shader to write stencil values.
831 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
832 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
833 const struct gl_pixelstore_attrib
*unpack
,
836 struct st_context
*st
= st_context(ctx
);
837 struct pipe_context
*pipe
= st
->pipe
;
838 struct st_renderbuffer
*strb
;
839 enum pipe_transfer_usage usage
;
840 struct pipe_transfer
*pt
;
841 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
843 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
848 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
850 /* totally clipped */
855 strb
= st_renderbuffer(ctx
->DrawBuffer
->
856 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
858 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
859 y
= ctx
->DrawBuffer
->Height
- y
- height
;
862 if (format
== GL_STENCIL_INDEX
&&
863 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
864 /* writing stencil to a combined depth+stencil buffer */
865 usage
= PIPE_TRANSFER_READ_WRITE
;
868 usage
= PIPE_TRANSFER_WRITE
;
871 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
872 strb
->surface
->u
.tex
.level
,
873 strb
->surface
->u
.tex
.first_layer
,
877 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
880 sValues
= malloc(width
* sizeof(GLubyte
));
881 zValues
= malloc(width
* sizeof(GLuint
));
883 if (sValues
&& zValues
) {
885 for (row
= 0; row
< height
; row
++) {
886 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
887 GLenum destType
= GL_UNSIGNED_BYTE
;
888 const void *source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
892 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
893 type
, source
, &clippedUnpack
,
894 ctx
->_ImageTransferState
);
896 if (format
== GL_DEPTH_STENCIL
) {
898 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
899 GL_FLOAT
: GL_UNSIGNED_INT
;
901 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
902 (1 << 24) - 1, type
, source
,
907 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
908 "zoom not complete");
914 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
915 spanY
= height
- row
- 1;
921 /* now pack the stencil (and Z) values in the dest format */
922 switch (pt
->resource
->format
) {
923 case PIPE_FORMAT_S8_UINT
:
925 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
926 assert(usage
== PIPE_TRANSFER_WRITE
);
927 memcpy(dest
, sValues
, width
);
930 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
931 if (format
== GL_DEPTH_STENCIL
) {
932 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
934 assert(usage
== PIPE_TRANSFER_WRITE
);
935 for (k
= 0; k
< width
; k
++) {
936 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
940 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
942 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
943 for (k
= 0; k
< width
; k
++) {
944 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
948 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
949 if (format
== GL_DEPTH_STENCIL
) {
950 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
952 assert(usage
== PIPE_TRANSFER_WRITE
);
953 for (k
= 0; k
< width
; k
++) {
954 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
958 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
960 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
961 for (k
= 0; k
< width
; k
++) {
962 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
966 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
967 if (format
== GL_DEPTH_STENCIL
) {
968 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
969 GLfloat
*destf
= (GLfloat
*)dest
;
971 assert(usage
== PIPE_TRANSFER_WRITE
);
972 for (k
= 0; k
< width
; k
++) {
973 destf
[k
*2] = zValuesFloat
[k
];
974 dest
[k
*2+1] = sValues
[k
] & 0xff;
978 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
980 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
981 for (k
= 0; k
< width
; k
++) {
982 dest
[k
*2+1] = sValues
[k
] & 0xff;
993 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
999 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
1001 /* unmap the stencil buffer */
1002 pipe_transfer_unmap(pipe
, pt
);
1007 * Get fragment program variant for a glDrawPixels or glCopyPixels
1008 * command for RGBA data.
1010 static struct st_fp_variant
*
1011 get_color_fp_variant(struct st_context
*st
)
1013 struct gl_context
*ctx
= st
->ctx
;
1014 struct st_fp_variant_key key
;
1015 struct st_fp_variant
*fpv
;
1017 memset(&key
, 0, sizeof(key
));
1019 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1021 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1022 ctx
->Pixel
.RedScale
!= 1.0 ||
1023 ctx
->Pixel
.GreenBias
!= 0.0 ||
1024 ctx
->Pixel
.GreenScale
!= 1.0 ||
1025 ctx
->Pixel
.BlueBias
!= 0.0 ||
1026 ctx
->Pixel
.BlueScale
!= 1.0 ||
1027 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1028 ctx
->Pixel
.AlphaScale
!= 1.0);
1029 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1030 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1031 ctx
->Color
._ClampFragmentColor
;
1033 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1040 * Clamp glDrawPixels width and height to the maximum texture size.
1043 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1044 struct gl_pixelstore_attrib
*unpack
)
1047 1 << (pipe
->screen
->get_param(pipe
->screen
,
1048 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1050 if (*width
> maxSize
) {
1051 if (unpack
->RowLength
== 0)
1052 unpack
->RowLength
= *width
;
1055 if (*height
> maxSize
) {
1062 * Search the array of 4 swizzle components for the named component and return
1066 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
1069 for (i
= 0; i
< 4; i
++) {
1070 if (swizzle
[i
] == component
)
1073 assert(!"search_swizzle() failed");
1079 * Set the sampler view's swizzle terms. This is used to handle RGBA
1080 * swizzling when the incoming image format isn't an exact match for
1081 * the actual texture format. For example, if we have glDrawPixels(
1082 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1083 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1084 * avoid swizzling all the pixels in software in the texstore code.
1087 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1089 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1090 const struct util_format_description
*desc
=
1091 util_format_description(sv
->texture
->format
);
1092 unsigned c0
, c1
, c2
, c3
;
1094 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1095 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1097 assert(desc
->block
.bits
== 32);
1099 /* invert the format's swizzle to setup the sampler's swizzle */
1100 if (format
== GL_RGBA
) {
1101 c0
= PIPE_SWIZZLE_X
;
1102 c1
= PIPE_SWIZZLE_Y
;
1103 c2
= PIPE_SWIZZLE_Z
;
1104 c3
= PIPE_SWIZZLE_W
;
1107 assert(format
== GL_BGRA
);
1108 c0
= PIPE_SWIZZLE_Z
;
1109 c1
= PIPE_SWIZZLE_Y
;
1110 c2
= PIPE_SWIZZLE_X
;
1111 c3
= PIPE_SWIZZLE_W
;
1113 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1114 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1115 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1116 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1119 /* use the default sampler swizzle */
1125 * Called via ctx->Driver.DrawPixels()
1128 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1129 GLsizei width
, GLsizei height
,
1130 GLenum format
, GLenum type
,
1131 const struct gl_pixelstore_attrib
*unpack
, const void *pixels
)
1133 void *driver_vp
, *driver_fp
;
1134 struct st_context
*st
= st_context(ctx
);
1135 struct pipe_context
*pipe
= st
->pipe
;
1136 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1137 struct pipe_sampler_view
*sv
[2] = { NULL
};
1138 int num_sampler_view
= 1;
1139 struct gl_pixelstore_attrib clippedUnpack
;
1140 struct st_fp_variant
*fpv
= NULL
;
1141 struct pipe_resource
*pt
;
1143 /* Mesa state should be up to date by now */
1144 assert(ctx
->NewState
== 0x0);
1146 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1148 st_flush_bitmap_cache(st
);
1149 st_invalidate_readpix_cache(st
);
1151 st_validate_state(st
, ST_PIPELINE_META
);
1153 /* Limit the size of the glDrawPixels to the max texture size.
1154 * Strictly speaking, that's not correct but since we don't handle
1155 * larger images yet, this is better than crashing.
1157 clippedUnpack
= *unpack
;
1158 unpack
= &clippedUnpack
;
1159 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1161 if (format
== GL_DEPTH_STENCIL
)
1162 write_stencil
= write_depth
= GL_TRUE
;
1163 else if (format
== GL_STENCIL_INDEX
)
1164 write_stencil
= GL_TRUE
;
1165 else if (format
== GL_DEPTH_COMPONENT
)
1166 write_depth
= GL_TRUE
;
1168 if (write_stencil
&&
1169 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1170 /* software fallback */
1171 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1177 * Get vertex/fragment shaders
1179 if (write_depth
|| write_stencil
) {
1180 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1182 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1185 fpv
= get_color_fp_variant(st
);
1187 driver_fp
= fpv
->driver_shader
;
1188 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1190 if (ctx
->Pixel
.MapColorFlag
) {
1191 pipe_sampler_view_reference(&sv
[1],
1192 st
->pixel_xfer
.pixelmap_sampler_view
);
1196 /* compiling a new fragment shader variant added new state constants
1197 * into the constant buffer, we need to update them
1199 st_upload_constants(st
, &st
->fp
->Base
);
1202 /* Put glDrawPixels image into a texture */
1203 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1205 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1209 /* create sampler view for the image */
1210 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1213 pipe_resource_reference(&pt
, NULL
);
1217 /* Set up the sampler view's swizzle */
1218 setup_sampler_swizzle(sv
[0], format
, type
);
1220 /* Create a second sampler view to read stencil. The stencil is
1221 * written using the shader stencil export functionality.
1223 if (write_stencil
) {
1224 enum pipe_format stencil_format
=
1225 util_format_stencil_only(pt
->format
);
1226 /* we should not be doing pixel map/transfer (see above) */
1227 assert(num_sampler_view
== 1);
1228 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1231 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1232 pipe_resource_reference(&pt
, NULL
);
1233 pipe_sampler_view_reference(&sv
[0], NULL
);
1239 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1241 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1246 ctx
->Current
.RasterColor
,
1247 GL_FALSE
, write_depth
, write_stencil
);
1248 pipe_sampler_view_reference(&sv
[0], NULL
);
1249 if (num_sampler_view
> 1)
1250 pipe_sampler_view_reference(&sv
[1], NULL
);
1252 /* free the texture (but may persist in the cache) */
1253 pipe_resource_reference(&pt
, NULL
);
1259 * Software fallback for glCopyPixels(GL_STENCIL).
1262 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1263 GLsizei width
, GLsizei height
,
1264 GLint dstx
, GLint dsty
)
1266 struct st_renderbuffer
*rbDraw
;
1267 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1268 enum pipe_transfer_usage usage
;
1269 struct pipe_transfer
*ptDraw
;
1274 buffer
= malloc(width
* height
* sizeof(ubyte
));
1276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1280 /* Get the dest renderbuffer */
1281 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1282 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1284 /* this will do stencil pixel transfer ops */
1285 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1286 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1287 &ctx
->DefaultPacking
, buffer
);
1290 /* debug code: dump stencil values */
1292 for (row
= 0; row
< height
; row
++) {
1293 printf("%3d: ", row
);
1294 for (col
= 0; col
< width
; col
++) {
1295 printf("%02x ", buffer
[col
+ row
* width
]);
1301 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1302 usage
= PIPE_TRANSFER_READ_WRITE
;
1304 usage
= PIPE_TRANSFER_WRITE
;
1306 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1307 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1310 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1311 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1313 /* map the stencil buffer */
1314 drawMap
= pipe_transfer_map(pipe
,
1316 rbDraw
->surface
->u
.tex
.level
,
1317 rbDraw
->surface
->u
.tex
.first_layer
,
1319 width
, height
, &ptDraw
);
1322 /* XXX PixelZoom not handled yet */
1323 for (i
= 0; i
< height
; i
++) {
1330 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1334 dst
= drawMap
+ y
* ptDraw
->stride
;
1335 src
= buffer
+ i
* width
;
1337 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1342 /* unmap the stencil buffer */
1343 pipe_transfer_unmap(pipe
, ptDraw
);
1348 * Return renderbuffer to use for reading color pixels for glCopyPixels
1350 static struct st_renderbuffer
*
1351 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1353 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1354 struct st_renderbuffer
*strb
=
1355 st_renderbuffer(fb
->_ColorReadBuffer
);
1362 * Try to do a glCopyPixels for simple cases with a blit by calling
1365 * We can do this when we're copying color pixels (depth/stencil
1366 * eventually) with no pixel zoom, no pixel transfer ops, no
1367 * per-fragment ops, and the src/dest regions don't overlap.
1370 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1371 GLsizei width
, GLsizei height
,
1372 GLint dstx
, GLint dsty
, GLenum type
)
1374 struct st_context
*st
= st_context(ctx
);
1375 struct pipe_context
*pipe
= st
->pipe
;
1376 struct pipe_screen
*screen
= pipe
->screen
;
1377 struct gl_pixelstore_attrib pack
, unpack
;
1378 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1380 if (type
== GL_COLOR
&&
1381 ctx
->Pixel
.ZoomX
== 1.0 &&
1382 ctx
->Pixel
.ZoomY
== 1.0 &&
1383 ctx
->_ImageTransferState
== 0x0 &&
1384 !ctx
->Color
.BlendEnabled
&&
1385 !ctx
->Color
.AlphaEnabled
&&
1386 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1388 !ctx
->Fog
.Enabled
&&
1389 !ctx
->Stencil
.Enabled
&&
1390 !ctx
->FragmentProgram
.Enabled
&&
1391 !ctx
->VertexProgram
.Enabled
&&
1392 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1393 !_mesa_ati_fragment_shader_enabled(ctx
) &&
1394 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1395 !ctx
->Query
.CondRenderQuery
&&
1396 !ctx
->Query
.CurrentOcclusionObject
) {
1397 struct st_renderbuffer
*rbRead
, *rbDraw
;
1400 * Clip the read region against the src buffer bounds.
1401 * We'll still allocate a temporary buffer/texture for the original
1402 * src region size but we'll only read the region which is on-screen.
1403 * This may mean that we draw garbage pixels into the dest region, but
1410 pack
= ctx
->DefaultPacking
;
1411 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1412 return GL_TRUE
; /* all done */
1414 /* clip against dest buffer bounds and scissor box */
1415 drawX
= dstx
+ pack
.SkipPixels
;
1416 drawY
= dsty
+ pack
.SkipRows
;
1418 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1419 return GL_TRUE
; /* all done */
1421 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1422 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1427 rbRead
= st_get_color_read_renderbuffer(ctx
);
1428 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1430 /* Flip src/dst position depending on the orientation of buffers. */
1431 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1432 readY
= rbRead
->Base
.Height
- readY
;
1436 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1437 /* We can't flip the destination for pipe->blit, so we only adjust
1438 * its position and flip the source.
1440 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1445 if (rbRead
!= rbDraw
||
1446 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1447 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1448 struct pipe_blit_info blit
;
1450 memset(&blit
, 0, sizeof(blit
));
1451 blit
.src
.resource
= rbRead
->texture
;
1452 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1453 blit
.src
.format
= rbRead
->texture
->format
;
1454 blit
.src
.box
.x
= readX
;
1455 blit
.src
.box
.y
= readY
;
1456 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1457 blit
.src
.box
.width
= readW
;
1458 blit
.src
.box
.height
= readH
;
1459 blit
.src
.box
.depth
= 1;
1460 blit
.dst
.resource
= rbDraw
->texture
;
1461 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1462 blit
.dst
.format
= rbDraw
->texture
->format
;
1463 blit
.dst
.box
.x
= drawX
;
1464 blit
.dst
.box
.y
= drawY
;
1465 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1466 blit
.dst
.box
.width
= drawW
;
1467 blit
.dst
.box
.height
= drawH
;
1468 blit
.dst
.box
.depth
= 1;
1469 blit
.mask
= PIPE_MASK_RGBA
;
1470 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1472 if (ctx
->DrawBuffer
!= ctx
->WinSysDrawBuffer
)
1473 st_window_rectangles_to_blit(ctx
, &blit
);
1475 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1476 blit
.src
.resource
->target
,
1477 blit
.src
.resource
->nr_samples
,
1478 PIPE_BIND_SAMPLER_VIEW
) &&
1479 screen
->is_format_supported(screen
, blit
.dst
.format
,
1480 blit
.dst
.resource
->target
,
1481 blit
.dst
.resource
->nr_samples
,
1482 PIPE_BIND_RENDER_TARGET
)) {
1483 pipe
->blit(pipe
, &blit
);
1494 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1495 GLsizei width
, GLsizei height
,
1496 GLint dstx
, GLint dsty
, GLenum type
)
1498 struct st_context
*st
= st_context(ctx
);
1499 struct pipe_context
*pipe
= st
->pipe
;
1500 struct pipe_screen
*screen
= pipe
->screen
;
1501 struct st_renderbuffer
*rbRead
;
1502 void *driver_vp
, *driver_fp
;
1503 struct pipe_resource
*pt
;
1504 struct pipe_sampler_view
*sv
[2] = { NULL
};
1505 struct st_fp_variant
*fpv
= NULL
;
1506 int num_sampler_view
= 1;
1507 enum pipe_format srcFormat
;
1509 GLboolean invertTex
= GL_FALSE
;
1510 GLint readX
, readY
, readW
, readH
;
1511 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1513 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1515 st_flush_bitmap_cache(st
);
1516 st_invalidate_readpix_cache(st
);
1518 st_validate_state(st
, ST_PIPELINE_META
);
1520 if (type
== GL_DEPTH_STENCIL
) {
1521 /* XXX make this more efficient */
1522 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1523 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1527 if (type
== GL_STENCIL
) {
1528 /* can't use texturing to do stencil */
1529 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1533 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1537 * The subsequent code implements glCopyPixels by copying the source
1538 * pixels into a temporary texture that's then applied to a textured quad.
1539 * When we draw the textured quad, all the usual per-fragment operations
1545 * Get vertex/fragment shaders
1547 if (type
== GL_COLOR
) {
1548 fpv
= get_color_fp_variant(st
);
1550 rbRead
= st_get_color_read_renderbuffer(ctx
);
1552 driver_fp
= fpv
->driver_shader
;
1553 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1555 if (ctx
->Pixel
.MapColorFlag
) {
1556 pipe_sampler_view_reference(&sv
[1],
1557 st
->pixel_xfer
.pixelmap_sampler_view
);
1561 /* compiling a new fragment shader variant added new state constants
1562 * into the constant buffer, we need to update them
1564 st_upload_constants(st
, &st
->fp
->Base
);
1567 assert(type
== GL_DEPTH
);
1568 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1569 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1571 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1572 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1575 /* Choose the format for the temporary texture. */
1576 srcFormat
= rbRead
->texture
->format
;
1577 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1578 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1580 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1582 /* srcFormat is non-renderable. Find a compatible renderable format. */
1583 if (type
== GL_DEPTH
) {
1584 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1585 GL_NONE
, st
->internal_target
, 0,
1589 assert(type
== GL_COLOR
);
1591 if (util_format_is_float(srcFormat
)) {
1592 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1593 GL_NONE
, st
->internal_target
, 0,
1596 else if (util_format_is_pure_sint(srcFormat
)) {
1597 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1598 GL_NONE
, st
->internal_target
, 0,
1601 else if (util_format_is_pure_uint(srcFormat
)) {
1602 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1603 GL_NONE
, st
->internal_target
, 0,
1606 else if (util_format_is_snorm(srcFormat
)) {
1607 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1608 GL_NONE
, st
->internal_target
, 0,
1612 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1613 GL_NONE
, st
->internal_target
, 0,
1618 if (srcFormat
== PIPE_FORMAT_NONE
) {
1619 assert(0 && "cannot choose a format for src of CopyPixels");
1624 /* Invert src region if needed */
1625 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1626 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1627 invertTex
= !invertTex
;
1630 /* Clip the read region against the src buffer bounds.
1631 * We'll still allocate a temporary buffer/texture for the original
1632 * src region size but we'll only read the region which is on-screen.
1633 * This may mean that we draw garbage pixels into the dest region, but
1640 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1641 /* The source region is completely out of bounds. Do nothing.
1642 * The GL spec says "Results of copies from outside the window,
1643 * or from regions of the window that are not exposed, are
1644 * hardware dependent and undefined."
1649 readW
= MAX2(0, readW
);
1650 readH
= MAX2(0, readH
);
1652 /* Allocate the temporary texture. */
1653 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1657 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1659 pipe_resource_reference(&pt
, NULL
);
1663 /* Copy the src region to the temporary texture. */
1665 struct pipe_blit_info blit
;
1667 memset(&blit
, 0, sizeof(blit
));
1668 blit
.src
.resource
= rbRead
->texture
;
1669 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1670 blit
.src
.format
= rbRead
->texture
->format
;
1671 blit
.src
.box
.x
= readX
;
1672 blit
.src
.box
.y
= readY
;
1673 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1674 blit
.src
.box
.width
= readW
;
1675 blit
.src
.box
.height
= readH
;
1676 blit
.src
.box
.depth
= 1;
1677 blit
.dst
.resource
= pt
;
1679 blit
.dst
.format
= pt
->format
;
1680 blit
.dst
.box
.x
= pack
.SkipPixels
;
1681 blit
.dst
.box
.y
= pack
.SkipRows
;
1683 blit
.dst
.box
.width
= readW
;
1684 blit
.dst
.box
.height
= readH
;
1685 blit
.dst
.box
.depth
= 1;
1686 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1687 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1689 pipe
->blit(pipe
, &blit
);
1692 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1693 * textured quad with that texture.
1695 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1696 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1701 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1702 invertTex
, GL_FALSE
, GL_FALSE
);
1704 pipe_resource_reference(&pt
, NULL
);
1705 pipe_sampler_view_reference(&sv
[0], NULL
);
1710 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1712 functions
->DrawPixels
= st_DrawPixels
;
1713 functions
->CopyPixels
= st_CopyPixels
;
1718 st_destroy_drawpix(struct st_context
*st
)
1722 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1723 if (st
->drawpix
.zs_shaders
[i
])
1724 cso_delete_fragment_shader(st
->cso_context
,
1725 st
->drawpix
.zs_shaders
[i
]);
1728 if (st
->drawpix
.vert_shaders
[0])
1729 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1730 if (st
->drawpix
.vert_shaders
[1])
1731 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1733 /* Free cache data */
1734 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
1735 struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
1737 pipe_resource_reference(&entry
->texture
, NULL
);