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"
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"
80 #include "compiler/nir/nir_builder.h"
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:
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.
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
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.
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.
109 #define USE_DRAWPIXELS_CACHE 1
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.
119 * \return CSO of the fragment shader.
122 get_drawpix_z_stencil_program(struct st_context
*st
,
123 GLboolean write_depth
,
124 GLboolean write_stencil
)
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
;
133 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
135 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
136 /* already have the proper shader */
137 return st
->drawpix
.zs_shaders
[shaderIndex
];
140 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
144 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
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);
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);
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);
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
);
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
);
183 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
184 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
187 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
189 /* save the new shader */
190 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
196 * Create a simple vertex shader that just passes through the
197 * vertex position, texcoord, and color.
200 st_make_passthrough_vertex_shader(struct st_context
*st
)
202 struct pipe_context
*pipe
= st
->pipe
;
203 struct pipe_screen
*screen
= pipe
->screen
;
205 if (st
->passthrough_vs
)
208 enum pipe_shader_ir preferred_ir
=
209 screen
->get_shader_param(screen
, MESA_SHADER_VERTEX
,
210 PIPE_SHADER_CAP_PREFERRED_IR
);
212 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
214 { VERT_ATTRIB_POS
, VERT_ATTRIB_COLOR0
, VERT_ATTRIB_GENERIC0
};
216 { VARYING_SLOT_POS
, VARYING_SLOT_COL0
, VARYING_SLOT_TEX0
};
219 st_nir_make_passthrough_shader(st
, "drawpixels VS",
220 MESA_SHADER_VERTEX
, 3,
221 inputs
, outputs
, NULL
, 0);
223 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
225 st
->needs_texcoord_semantic
? TGSI_SEMANTIC_TEXCOORD
:
226 TGSI_SEMANTIC_GENERIC
};
227 const uint semantic_indexes
[] = { 0, 0, 0 };
230 util_make_vertex_passthrough_shader(st
->pipe
, 3, semantic_names
,
231 semantic_indexes
, false);
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, 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
);
570 pipe_resource_reference(&pt
, NULL
);
571 _mesa_unmap_pbo_source(ctx
, unpack
);
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.
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
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 */
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 */
612 pipe_transfer_unmap(pipe
, transfer
);
615 ctx
->_ImageTransferState
= imageTransferStateSave
;
618 _mesa_unmap_pbo_source(ctx
, unpack
);
620 #if USE_DRAWPIXELS_CACHE
621 cache_drawpixels_image(st
, width
, height
, format
, type
, unpack
, pixels
, pt
);
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
,
636 struct st_fp_variant
*fpv
,
637 const GLfloat
*color
,
639 GLboolean write_depth
, GLboolean write_stencil
)
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
;
651 assert(sv
[0]->texture
->target
== st
->internal_target
);
654 /* XXX if DrawPixels image is larger than max texture size, break
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
);
662 cso_state_mask
= (CSO_BIT_RASTERIZER
|
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
);
671 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
674 cso_save_state(cso
, cso_state_mask
);
676 /* rasterizer state: just scissor */
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
);
691 /* Stencil writing bypasses the normal fragment pipeline to
692 * disable color writing and set stencil test to always pass.
694 struct pipe_depth_stencil_alpha_state dsa
;
695 struct pipe_blend_state blend
;
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
;
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
;
709 cso_set_depth_stencil_alpha(cso
, &dsa
);
711 /* blend (colormask) */
712 memset(&blend
, 0, sizeof(blend
));
713 cso_set_blend(cso
, &blend
);
716 /* fragment shader state: TEX lookup program */
717 cso_set_fragment_shader_handle(cso
, driver_fp
);
719 /* vertex shader state: position + texcoord pass-through */
720 cso_set_vertex_shader_handle(cso
, driver_vp
);
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
);
727 /* user samplers, plus the drawpix samplers */
729 struct pipe_sampler_state sampler
;
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
;
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
);
748 for (i
= 0; i
< st
->state
.num_frag_samplers
; i
++)
749 samplers
[i
] = &st
->state
.frag_samplers
[i
];
751 samplers
[fpv
->drawpix_sampler
] = &sampler
;
753 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
755 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
757 /* drawing a depth/stencil image */
758 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
760 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
764 /* user textures, plus the drawpix textures */
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
]);
772 memcpy(sampler_views
, st
->state
.frag_sampler_views
,
773 sizeof(sampler_views
));
775 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
777 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
778 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
780 /* drawing a depth/stencil image */
781 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
784 /* viewport state: viewport matching window dims */
785 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
787 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
788 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
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.
794 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
795 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
796 invertTex
= !invertTex
;
800 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
802 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
804 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
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
;
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");
827 cso_restore_state(cso
);
832 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
833 * can't use a fragment shader to write stencil values.
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
,
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;
848 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
853 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
855 /* totally clipped */
860 strb
= st_renderbuffer(ctx
->DrawBuffer
->
861 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
863 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
864 y
= ctx
->DrawBuffer
->Height
- y
- height
;
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
;
873 usage
= PIPE_TRANSFER_WRITE
;
876 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
877 strb
->surface
->u
.tex
.level
,
878 strb
->surface
->u
.tex
.first_layer
,
882 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
885 sValues
= malloc(width
* sizeof(GLubyte
));
886 zValues
= malloc(width
* sizeof(GLuint
));
888 if (sValues
&& zValues
) {
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
,
897 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
898 type
, source
, &clippedUnpack
,
899 ctx
->_ImageTransferState
);
901 if (format
== GL_DEPTH_STENCIL
) {
903 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
904 GL_FLOAT
: GL_UNSIGNED_INT
;
906 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
907 (1 << 24) - 1, type
, source
,
912 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
913 "zoom not complete");
919 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
920 spanY
= height
- row
- 1;
926 /* now pack the stencil (and Z) values in the dest format */
927 switch (pt
->resource
->format
) {
928 case PIPE_FORMAT_S8_UINT
:
930 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
931 assert(usage
== PIPE_TRANSFER_WRITE
);
932 memcpy(dest
, sValues
, width
);
935 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
936 if (format
== GL_DEPTH_STENCIL
) {
937 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
939 assert(usage
== PIPE_TRANSFER_WRITE
);
940 for (k
= 0; k
< width
; k
++) {
941 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
945 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
947 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
948 for (k
= 0; k
< width
; k
++) {
949 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
953 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
954 if (format
== GL_DEPTH_STENCIL
) {
955 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
957 assert(usage
== PIPE_TRANSFER_WRITE
);
958 for (k
= 0; k
< width
; k
++) {
959 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
963 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
965 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
966 for (k
= 0; k
< width
; k
++) {
967 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
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
;
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;
983 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
985 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
986 for (k
= 0; k
< width
; k
++) {
987 dest
[k
*2+1] = sValues
[k
] & 0xff;
998 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
1004 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
1006 /* unmap the stencil buffer */
1007 pipe_transfer_unmap(pipe
, pt
);
1012 * Get fragment program variant for a glDrawPixels or glCopyPixels
1013 * command for RGBA data.
1015 static struct st_fp_variant
*
1016 get_color_fp_variant(struct st_context
*st
)
1018 struct gl_context
*ctx
= st
->ctx
;
1019 struct st_fp_variant_key key
;
1020 struct st_fp_variant
*fpv
;
1022 memset(&key
, 0, sizeof(key
));
1024 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
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
;
1038 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1045 * Clamp glDrawPixels width and height to the maximum texture size.
1048 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1049 struct gl_pixelstore_attrib
*unpack
)
1052 1 << (pipe
->screen
->get_param(pipe
->screen
,
1053 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1055 if (*width
> maxSize
) {
1056 if (unpack
->RowLength
== 0)
1057 unpack
->RowLength
= *width
;
1060 if (*height
> maxSize
) {
1067 * Search the array of 4 swizzle components for the named component and return
1071 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
1074 for (i
= 0; i
< 4; i
++) {
1075 if (swizzle
[i
] == component
)
1078 assert(!"search_swizzle() failed");
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.
1092 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
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
;
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).
1102 assert(desc
->block
.bits
== 32);
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
;
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
;
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
);
1124 /* use the default sampler swizzle */
1130 * Called via ctx->Driver.DrawPixels()
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
)
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
;
1148 /* Mesa state should be up to date by now */
1149 assert(ctx
->NewState
== 0x0);
1151 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1153 st_flush_bitmap_cache(st
);
1154 st_invalidate_readpix_cache(st
);
1156 st_validate_state(st
, ST_PIPELINE_META
);
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.
1162 clippedUnpack
= *unpack
;
1163 unpack
= &clippedUnpack
;
1164 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
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
;
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
,
1181 /* Put glDrawPixels image into a texture */
1182 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1184 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1188 st_make_passthrough_vertex_shader(st
);
1191 * Get vertex/fragment shaders
1193 if (write_depth
|| write_stencil
) {
1194 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1198 fpv
= get_color_fp_variant(st
);
1200 driver_fp
= fpv
->driver_shader
;
1202 if (ctx
->Pixel
.MapColorFlag
) {
1203 pipe_sampler_view_reference(&sv
[1],
1204 st
->pixel_xfer
.pixelmap_sampler_view
);
1208 /* compiling a new fragment shader variant added new state constants
1209 * into the constant buffer, we need to update them
1211 st_upload_constants(st
, &st
->fp
->Base
);
1214 /* create sampler view for the image */
1215 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1217 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1218 pipe_resource_reference(&pt
, NULL
);
1222 /* Set up the sampler view's swizzle */
1223 setup_sampler_swizzle(sv
[0], format
, type
);
1225 /* Create a second sampler view to read stencil. The stencil is
1226 * written using the shader stencil export functionality.
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
,
1236 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1237 pipe_resource_reference(&pt
, NULL
);
1238 pipe_sampler_view_reference(&sv
[0], NULL
);
1244 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1246 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
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
);
1257 /* free the texture (but may persist in the cache) */
1258 pipe_resource_reference(&pt
, NULL
);
1264 * Software fallback for glCopyPixels(GL_STENCIL).
1267 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1268 GLsizei width
, GLsizei height
,
1269 GLint dstx
, GLint dsty
)
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
;
1279 buffer
= malloc(width
* height
* sizeof(ubyte
));
1281 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1285 /* Get the dest renderbuffer */
1286 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1287 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
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
);
1295 /* debug code: dump stencil values */
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
]);
1306 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1307 usage
= PIPE_TRANSFER_READ_WRITE
;
1309 usage
= PIPE_TRANSFER_WRITE
;
1311 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1312 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1315 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1316 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1318 /* map the stencil buffer */
1319 drawMap
= pipe_transfer_map(pipe
,
1321 rbDraw
->surface
->u
.tex
.level
,
1322 rbDraw
->surface
->u
.tex
.first_layer
,
1324 width
, height
, &ptDraw
);
1327 /* XXX PixelZoom not handled yet */
1328 for (i
= 0; i
< height
; i
++) {
1335 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1339 dst
= drawMap
+ y
* ptDraw
->stride
;
1340 src
= buffer
+ i
* width
;
1342 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1347 /* unmap the stencil buffer */
1348 pipe_transfer_unmap(pipe
, ptDraw
);
1353 * Return renderbuffer to use for reading color pixels for glCopyPixels
1355 static struct st_renderbuffer
*
1356 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1358 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1359 struct st_renderbuffer
*strb
=
1360 st_renderbuffer(fb
->_ColorReadBuffer
);
1367 * Try to do a glCopyPixels for simple cases with a blit by calling
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.
1375 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1376 GLsizei width
, GLsizei height
,
1377 GLint dstx
, GLint dsty
, GLenum type
)
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
;
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
) &&
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
;
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
1415 pack
= ctx
->DefaultPacking
;
1416 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1417 return GL_TRUE
; /* all done */
1419 /* clip against dest buffer bounds and scissor box */
1420 drawX
= dstx
+ pack
.SkipPixels
;
1421 drawY
= dsty
+ pack
.SkipRows
;
1423 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1424 return GL_TRUE
; /* all done */
1426 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1427 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1432 rbRead
= st_get_color_read_renderbuffer(ctx
);
1433 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
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
;
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.
1445 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
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
;
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
;
1477 if (ctx
->DrawBuffer
!= ctx
->WinSysDrawBuffer
)
1478 st_window_rectangles_to_blit(ctx
, &blit
);
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
);
1501 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1502 GLsizei width
, GLsizei height
,
1503 GLint dstx
, GLint dsty
, GLenum type
)
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
;
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
;
1516 GLboolean invertTex
= GL_FALSE
;
1517 GLint readX
, readY
, readW
, readH
;
1518 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1520 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1522 st_flush_bitmap_cache(st
);
1523 st_invalidate_readpix_cache(st
);
1525 st_validate_state(st
, ST_PIPELINE_META
);
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
);
1534 if (type
== GL_STENCIL
) {
1535 /* can't use texturing to do stencil */
1536 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1540 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
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
1550 st_make_passthrough_vertex_shader(st
);
1553 * Get vertex/fragment shaders
1555 if (type
== GL_COLOR
) {
1556 fpv
= get_color_fp_variant(st
);
1558 rbRead
= st_get_color_read_renderbuffer(ctx
);
1560 driver_fp
= fpv
->driver_shader
;
1562 if (ctx
->Pixel
.MapColorFlag
) {
1563 pipe_sampler_view_reference(&sv
[1],
1564 st
->pixel_xfer
.pixelmap_sampler_view
);
1568 /* compiling a new fragment shader variant added new state constants
1569 * into the constant buffer, we need to update them
1571 st_upload_constants(st
, &st
->fp
->Base
);
1574 assert(type
== GL_DEPTH
);
1575 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1576 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1578 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
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
);
1586 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
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,
1595 assert(type
== GL_COLOR
);
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,
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,
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,
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,
1618 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1619 GL_NONE
, st
->internal_target
, 0, 0,
1624 if (srcFormat
== PIPE_FORMAT_NONE
) {
1625 assert(0 && "cannot choose a format for src of CopyPixels");
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
;
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
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."
1655 readW
= MAX2(0, readW
);
1656 readH
= MAX2(0, readH
);
1658 /* Allocate the temporary texture. */
1659 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1663 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1665 pipe_resource_reference(&pt
, NULL
);
1669 /* Copy the src region to the temporary texture. */
1671 struct pipe_blit_info blit
;
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
;
1685 blit
.dst
.format
= pt
->format
;
1686 blit
.dst
.box
.x
= pack
.SkipPixels
;
1687 blit
.dst
.box
.y
= pack
.SkipRows
;
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
;
1695 pipe
->blit(pipe
, &blit
);
1698 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1699 * textured quad with that texture.
1701 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1702 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1707 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1708 invertTex
, GL_FALSE
, GL_FALSE
);
1710 pipe_resource_reference(&pt
, NULL
);
1711 pipe_sampler_view_reference(&sv
[0], NULL
);
1716 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1718 functions
->DrawPixels
= st_DrawPixels
;
1719 functions
->CopyPixels
= st_CopyPixels
;
1724 st_destroy_drawpix(struct st_context
*st
)
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
]);
1734 if (st
->passthrough_vs
)
1735 cso_delete_vertex_shader(st
->cso_context
, st
->passthrough_vs
);
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
];
1741 pipe_resource_reference(&entry
->texture
, NULL
);