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"
71 #include "pipe/p_context.h"
72 #include "pipe/p_defines.h"
73 #include "tgsi/tgsi_ureg.h"
74 #include "util/u_format.h"
75 #include "util/u_inlines.h"
76 #include "util/u_math.h"
77 #include "util/u_simple_shaders.h"
78 #include "util/u_tile.h"
79 #include "cso_cache/cso_context.h"
81 #include "compiler/nir/nir_builder.h"
84 * We have a simple glDrawPixels cache to try to optimize the case where the
85 * same image is drawn over and over again. It basically works as follows:
87 * 1. After we construct a texture map with the image and draw it, we do
88 * not discard the texture. We keep it around, plus we note the
89 * glDrawPixels width, height, format, etc. parameters and keep a copy
90 * of the image in a malloc'd buffer.
92 * 2. On the next glDrawPixels we check if the parameters match the previous
93 * call. If those match, we check if the image matches the previous image
94 * via a memcmp() call. If everything matches, we re-use the previous
95 * texture, thereby avoiding the cost creating a new texture and copying
98 * The effectiveness of this cache depends upon:
99 * 1. If the memcmp() finds a difference, it happens relatively quickly.
100 Hopefully, not just the last pixels differ!
101 * 2. If the memcmp() finds no difference, doing that check is faster than
102 * creating and loading a texture.
105 * 1. We don't support any pixel unpacking parameters.
106 * 2. We don't try to cache images in Pixel Buffer Objects.
107 * 3. Instead of saving the whole image, perhaps some sort of reliable
108 * checksum function could be used instead.
110 #define USE_DRAWPIXELS_CACHE 1
113 sample_via_nir(nir_builder
*b
, nir_variable
*texcoord
,
114 const char *name
, int sampler
)
116 const struct glsl_type
*sampler2D
=
117 glsl_sampler_type(GLSL_SAMPLER_DIM_2D
, false, false, GLSL_TYPE_FLOAT
);
120 nir_variable_create(b
->shader
, nir_var_uniform
, sampler2D
, name
);
121 var
->data
.binding
= sampler
;
122 var
->data
.explicit_binding
= true;
124 nir_deref_instr
*deref
= nir_build_deref_var(b
, var
);
126 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, 3);
127 tex
->op
= nir_texop_tex
;
128 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
129 tex
->coord_components
= 2;
130 tex
->dest_type
= nir_type_float
;
131 tex
->src
[0].src_type
= nir_tex_src_texture_deref
;
132 tex
->src
[0].src
= nir_src_for_ssa(&deref
->dest
.ssa
);
133 tex
->src
[1].src_type
= nir_tex_src_sampler_deref
;
134 tex
->src
[1].src
= nir_src_for_ssa(&deref
->dest
.ssa
);
135 tex
->src
[2].src_type
= nir_tex_src_coord
;
137 nir_src_for_ssa(nir_channels(b
, nir_load_var(b
, texcoord
),
138 (1 << tex
->coord_components
) - 1));
140 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
141 nir_builder_instr_insert(b
, &tex
->instr
);
142 return nir_channel(b
, &tex
->dest
.ssa
, 0);
146 make_drawpix_z_stencil_program_nir(struct st_context
*st
,
150 struct nir_builder b
;
151 const nir_shader_compiler_options
*options
=
152 st
->ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
].NirOptions
;
154 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, options
);
156 nir_variable
*texcoord
=
157 nir_variable_create(b
.shader
, nir_var_shader_in
, glsl_vec_type(2),
159 texcoord
->data
.location
= VARYING_SLOT_TEX0
;
163 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_float_type(),
165 out
->data
.location
= FRAG_RESULT_DEPTH
;
166 nir_ssa_def
*depth
= sample_via_nir(&b
, texcoord
, "depth", 0);
167 nir_store_var(&b
, out
, depth
, 0x1);
169 /* Also copy color */
170 nir_variable
*color_in
=
171 nir_variable_create(b
.shader
, nir_var_shader_in
, glsl_vec_type(4),
173 color_in
->data
.location
= VARYING_SLOT_COL0
;
175 nir_variable
*color_out
=
176 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_vec_type(4),
178 color_out
->data
.location
= FRAG_RESULT_COLOR
;
179 nir_copy_var(&b
, color_out
, color_in
);
184 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_uint_type(),
185 "gl_FragStencilRefARB");
186 out
->data
.location
= FRAG_RESULT_STENCIL
;
187 nir_ssa_def
*stencil
= sample_via_nir(&b
, texcoord
, "stencil", 1);
188 nir_store_var(&b
, out
, stencil
, 0x1);
192 snprintf(name
, 14, "drawpixels %s%s",
193 write_depth
? "Z" : "", write_stencil
? "S" : "");
195 return st_nir_finish_builtin_shader(st
, b
.shader
, name
);
200 make_drawpix_z_stencil_program_tgsi(struct st_context
*st
,
204 struct ureg_program
*ureg
;
205 struct ureg_src depth_sampler
, stencil_sampler
;
206 struct ureg_src texcoord
, color
;
207 struct ureg_dst out_color
, out_depth
, out_stencil
;
209 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
213 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
216 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
217 TGSI_INTERPOLATE_COLOR
);
218 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
220 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
221 ureg_DECL_sampler_view(ureg
, 0, TGSI_TEXTURE_2D
,
222 TGSI_RETURN_TYPE_FLOAT
,
223 TGSI_RETURN_TYPE_FLOAT
,
224 TGSI_RETURN_TYPE_FLOAT
,
225 TGSI_RETURN_TYPE_FLOAT
);
226 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
230 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
231 ureg_DECL_sampler_view(ureg
, 1, TGSI_TEXTURE_2D
,
232 TGSI_RETURN_TYPE_UINT
,
233 TGSI_RETURN_TYPE_UINT
,
234 TGSI_RETURN_TYPE_UINT
,
235 TGSI_RETURN_TYPE_UINT
);
236 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
239 texcoord
= ureg_DECL_fs_input(ureg
,
240 st
->needs_texcoord_semantic
?
241 TGSI_SEMANTIC_TEXCOORD
:
242 TGSI_SEMANTIC_GENERIC
,
243 0, TGSI_INTERPOLATE_LINEAR
);
246 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
247 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
248 ureg_MOV(ureg
, out_color
, color
);
252 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
253 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
256 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
261 * Create fragment program that does a TEX() instruction to get a Z and/or
262 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
263 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
264 * Pass fragment color through as-is.
266 * \return CSO of the fragment shader.
269 get_drawpix_z_stencil_program(struct st_context
*st
,
273 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
274 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
277 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
279 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
280 /* already have the proper shader */
281 return st
->drawpix
.zs_shaders
[shaderIndex
];
284 enum pipe_shader_ir preferred_ir
=
285 pscreen
->get_shader_param(pscreen
, PIPE_SHADER_FRAGMENT
,
286 PIPE_SHADER_CAP_PREFERRED_IR
);
288 if (preferred_ir
== PIPE_SHADER_IR_NIR
)
289 cso
= make_drawpix_z_stencil_program_nir(st
, write_depth
, write_stencil
);
291 cso
= make_drawpix_z_stencil_program_tgsi(st
, write_depth
, write_stencil
);
293 /* save the new shader */
294 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
300 * Create a simple vertex shader that just passes through the
301 * vertex position, texcoord, and color.
304 st_make_passthrough_vertex_shader(struct st_context
*st
)
306 struct pipe_context
*pipe
= st
->pipe
;
307 struct pipe_screen
*screen
= pipe
->screen
;
309 if (st
->passthrough_vs
)
312 enum pipe_shader_ir preferred_ir
=
313 screen
->get_shader_param(screen
, PIPE_SHADER_VERTEX
,
314 PIPE_SHADER_CAP_PREFERRED_IR
);
316 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
318 { VERT_ATTRIB_POS
, VERT_ATTRIB_COLOR0
, VERT_ATTRIB_GENERIC0
};
320 { VARYING_SLOT_POS
, VARYING_SLOT_COL0
, VARYING_SLOT_TEX0
};
323 st_nir_make_passthrough_shader(st
, "drawpixels VS",
324 MESA_SHADER_VERTEX
, 3,
325 inputs
, outputs
, NULL
, 0);
327 const enum tgsi_semantic semantic_names
[] = {
328 TGSI_SEMANTIC_POSITION
,
330 st
->needs_texcoord_semantic
? TGSI_SEMANTIC_TEXCOORD
:
331 TGSI_SEMANTIC_GENERIC
333 const uint semantic_indexes
[] = { 0, 0, 0 };
336 util_make_vertex_passthrough_shader(st
->pipe
, 3, semantic_names
,
337 semantic_indexes
, false);
343 * Return a texture internalFormat for drawing/copying an image
344 * of the given format and type.
347 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
350 case GL_DEPTH_COMPONENT
:
352 case GL_UNSIGNED_SHORT
:
353 return GL_DEPTH_COMPONENT16
;
355 case GL_UNSIGNED_INT
:
356 return GL_DEPTH_COMPONENT32
;
359 if (ctx
->Extensions
.ARB_depth_buffer_float
)
360 return GL_DEPTH_COMPONENT32F
;
362 return GL_DEPTH_COMPONENT
;
365 return GL_DEPTH_COMPONENT
;
368 case GL_DEPTH_STENCIL
:
370 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
371 return GL_DEPTH32F_STENCIL8
;
373 case GL_UNSIGNED_INT_24_8
:
375 return GL_DEPTH24_STENCIL8
;
378 case GL_STENCIL_INDEX
:
379 return GL_STENCIL_INDEX
;
382 if (_mesa_is_enum_format_integer(format
)) {
386 case GL_UNSIGNED_BYTE
:
390 case GL_UNSIGNED_SHORT
:
394 case GL_UNSIGNED_INT
:
397 assert(0 && "Unexpected type in internal_format()");
398 return GL_RGBA_INTEGER
;
403 case GL_UNSIGNED_BYTE
:
404 case GL_UNSIGNED_INT_8_8_8_8
:
405 case GL_UNSIGNED_INT_8_8_8_8_REV
:
409 case GL_UNSIGNED_BYTE_3_3_2
:
410 case GL_UNSIGNED_BYTE_2_3_3_REV
:
413 case GL_UNSIGNED_SHORT_4_4_4_4
:
414 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
417 case GL_UNSIGNED_SHORT_5_6_5
:
418 case GL_UNSIGNED_SHORT_5_6_5_REV
:
421 case GL_UNSIGNED_SHORT_5_5_5_1
:
422 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
425 case GL_UNSIGNED_INT_10_10_10_2
:
426 case GL_UNSIGNED_INT_2_10_10_10_REV
:
429 case GL_UNSIGNED_SHORT
:
430 case GL_UNSIGNED_INT
:
435 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
440 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
442 case GL_HALF_FLOAT_ARB
:
444 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
445 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
450 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
451 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
453 case GL_UNSIGNED_INT_5_9_9_9_REV
:
454 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
457 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
458 assert(ctx
->Extensions
.EXT_packed_float
);
459 return GL_R11F_G11F_B10F
;
467 * Create a temporary texture to hold an image of the given size.
468 * If width, height are not POT and the driver only handles POT textures,
469 * allocate the next larger size of texture that is POT.
471 static struct pipe_resource
*
472 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
473 enum pipe_format texFormat
, unsigned bind
)
475 struct pipe_resource
*pt
;
477 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
478 width
, height
, 1, 1, 0, bind
);
485 * Search the cache for an image which matches the given parameters.
486 * \return pipe_resource pointer if found, NULL if not found.
488 static struct pipe_resource
*
489 search_drawpixels_cache(struct st_context
*st
,
490 GLsizei width
, GLsizei height
,
491 GLenum format
, GLenum type
,
492 const struct gl_pixelstore_attrib
*unpack
,
495 struct pipe_resource
*pt
= NULL
;
496 const GLint bpp
= _mesa_bytes_per_pixel(format
, type
);
499 if ((unpack
->RowLength
!= 0 && unpack
->RowLength
!= width
) ||
500 unpack
->SkipPixels
!= 0 ||
501 unpack
->SkipRows
!= 0 ||
503 _mesa_is_bufferobj(unpack
->BufferObj
)) {
504 /* we don't allow non-default pixel unpacking values */
508 /* Search cache entries for a match */
509 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
510 struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
512 if (width
== entry
->width
&&
513 height
== entry
->height
&&
514 format
== entry
->format
&&
515 type
== entry
->type
&&
516 pixels
== entry
->user_pointer
&&
518 assert(entry
->texture
);
520 /* check if the pixel data is the same */
521 if (memcmp(pixels
, entry
->image
, width
* height
* bpp
) == 0) {
522 /* Success - found a cache match */
523 pipe_resource_reference(&pt
, entry
->texture
);
524 /* refcount of returned texture should be at least two here. One
525 * reference for the cache to hold on to, one for the caller (which
526 * it will release), and possibly more held by the driver.
528 assert(pt
->reference
.count
>= 2);
530 /* update the age of this entry */
531 entry
->age
= ++st
->drawpix_cache
.age
;
538 /* no cache match found */
544 * Find the oldest entry in the glDrawPixels cache. We'll replace this
545 * one when we need to store a new image.
547 static struct drawpix_cache_entry
*
548 find_oldest_drawpixels_cache_entry(struct st_context
*st
)
550 unsigned oldest_age
= ~0u, oldest_index
= ~0u;
553 /* Find entry with oldest (lowest) age */
554 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
555 const struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
556 if (entry
->age
< oldest_age
) {
557 oldest_age
= entry
->age
;
562 assert(oldest_index
!= ~0u);
564 return &st
->drawpix_cache
.entries
[oldest_index
];
569 * Try to save the given glDrawPixels image in the cache.
572 cache_drawpixels_image(struct st_context
*st
,
573 GLsizei width
, GLsizei height
,
574 GLenum format
, GLenum type
,
575 const struct gl_pixelstore_attrib
*unpack
,
577 struct pipe_resource
*pt
)
579 if ((unpack
->RowLength
== 0 || unpack
->RowLength
== width
) &&
580 unpack
->SkipPixels
== 0 &&
581 unpack
->SkipRows
== 0) {
582 const GLint bpp
= _mesa_bytes_per_pixel(format
, type
);
583 struct drawpix_cache_entry
*entry
=
584 find_oldest_drawpixels_cache_entry(st
);
586 entry
->width
= width
;
587 entry
->height
= height
;
588 entry
->format
= format
;
590 entry
->user_pointer
= pixels
;
592 entry
->image
= malloc(width
* height
* bpp
);
594 memcpy(entry
->image
, pixels
, width
* height
* bpp
);
595 pipe_resource_reference(&entry
->texture
, pt
);
596 entry
->age
= ++st
->drawpix_cache
.age
;
599 /* out of memory, free/disable cached texture */
602 pipe_resource_reference(&entry
->texture
, NULL
);
609 * Make texture containing an image for glDrawPixels image.
610 * If 'pixels' is NULL, leave the texture image data undefined.
612 static struct pipe_resource
*
613 make_texture(struct st_context
*st
,
614 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
615 const struct gl_pixelstore_attrib
*unpack
,
618 struct gl_context
*ctx
= st
->ctx
;
619 struct pipe_context
*pipe
= st
->pipe
;
621 struct pipe_resource
*pt
= NULL
;
622 enum pipe_format pipeFormat
;
623 GLenum baseInternalFormat
;
625 #if USE_DRAWPIXELS_CACHE
626 pt
= search_drawpixels_cache(st
, width
, height
, format
, type
,
633 /* Choose a pixel format for the temp texture which will hold the
636 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
637 format
, type
, unpack
->SwapBytes
);
639 if (pipeFormat
== PIPE_FORMAT_NONE
) {
640 /* Use the generic approach. */
641 GLenum intFormat
= internal_format(ctx
, format
, type
);
643 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
644 st
->internal_target
, 0, 0,
645 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
646 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
649 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
650 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
652 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
656 /* alloc temporary texture */
657 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
659 _mesa_unmap_pbo_source(ctx
, unpack
);
664 struct pipe_transfer
*transfer
;
666 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
668 /* we'll do pixel transfer in a fragment shader */
669 ctx
->_ImageTransferState
= 0x0;
671 /* map texture transfer */
672 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
673 PIPE_TRANSFER_WRITE
, 0, 0,
674 width
, height
, &transfer
);
676 pipe_resource_reference(&pt
, NULL
);
677 _mesa_unmap_pbo_source(ctx
, unpack
);
681 /* Put image into texture transfer.
682 * Note that the image is actually going to be upside down in
683 * the texture. We deal with that with texcoords.
685 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
686 && type
== GL_UNSIGNED_BYTE
) {
687 /* Use a memcpy-based texstore to avoid software pixel swizzling.
688 * We'll do the necessary swizzling with the pipe_sampler_view to
689 * give much better performance.
690 * XXX in the future, expand this to accomodate more format and
693 _mesa_memcpy_texture(ctx
, 2,
694 mformat
, /* mesa_format */
695 transfer
->stride
, /* dstRowStride, bytes */
696 &dest
, /* destSlices */
697 width
, height
, 1, /* size */
698 format
, type
, /* src format/type */
699 pixels
, /* data source */
703 ASSERTED
bool success
;
704 success
= _mesa_texstore(ctx
, 2, /* dims */
705 baseInternalFormat
, /* baseInternalFormat */
706 mformat
, /* mesa_format */
707 transfer
->stride
, /* dstRowStride, bytes */
708 &dest
, /* destSlices */
709 width
, height
, 1, /* size */
710 format
, type
, /* src format/type */
711 pixels
, /* data source */
718 pipe_transfer_unmap(pipe
, transfer
);
721 ctx
->_ImageTransferState
= imageTransferStateSave
;
724 _mesa_unmap_pbo_source(ctx
, unpack
);
726 #if USE_DRAWPIXELS_CACHE
727 cache_drawpixels_image(st
, width
, height
, format
, type
, unpack
, pixels
, pt
);
735 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
736 GLsizei width
, GLsizei height
,
737 GLfloat zoomX
, GLfloat zoomY
,
738 struct pipe_sampler_view
**sv
,
739 int num_sampler_view
,
742 struct st_fp_variant
*fpv
,
743 const GLfloat
*color
,
745 GLboolean write_depth
, GLboolean write_stencil
)
747 struct st_context
*st
= st_context(ctx
);
748 struct pipe_context
*pipe
= st
->pipe
;
749 struct cso_context
*cso
= st
->cso_context
;
750 const unsigned fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
751 const unsigned fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
752 GLfloat x0
, y0
, x1
, y1
;
753 ASSERTED GLsizei maxSize
;
754 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
755 unsigned cso_state_mask
;
757 assert(sv
[0]->texture
->target
== st
->internal_target
);
760 /* XXX if DrawPixels image is larger than max texture size, break
763 maxSize
= pipe
->screen
->get_param(pipe
->screen
,
764 PIPE_CAP_MAX_TEXTURE_2D_SIZE
);
765 assert(width
<= maxSize
);
766 assert(height
<= maxSize
);
768 cso_state_mask
= (CSO_BIT_RASTERIZER
|
770 CSO_BIT_FRAGMENT_SAMPLERS
|
771 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
772 CSO_BIT_STREAM_OUTPUTS
|
773 CSO_BIT_VERTEX_ELEMENTS
|
774 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
775 CSO_BITS_ALL_SHADERS
);
777 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
780 cso_save_state(cso
, cso_state_mask
);
782 /* rasterizer state: just scissor */
784 struct pipe_rasterizer_state rasterizer
;
785 memset(&rasterizer
, 0, sizeof(rasterizer
));
786 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
787 ctx
->Color
._ClampFragmentColor
;
788 rasterizer
.half_pixel_center
= 1;
789 rasterizer
.bottom_edge_rule
= 1;
790 rasterizer
.depth_clip_near
= st
->clamp_frag_depth_in_shader
||
791 !ctx
->Transform
.DepthClampNear
;
792 rasterizer
.depth_clip_far
= st
->clamp_frag_depth_in_shader
||
793 !ctx
->Transform
.DepthClampFar
;
794 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
795 cso_set_rasterizer(cso
, &rasterizer
);
799 /* Stencil writing bypasses the normal fragment pipeline to
800 * disable color writing and set stencil test to always pass.
802 struct pipe_depth_stencil_alpha_state dsa
;
803 struct pipe_blend_state blend
;
806 memset(&dsa
, 0, sizeof(dsa
));
807 dsa
.stencil
[0].enabled
= 1;
808 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
809 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
810 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
812 /* writing depth+stencil: depth test always passes */
813 dsa
.depth
.enabled
= 1;
814 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
815 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
817 cso_set_depth_stencil_alpha(cso
, &dsa
);
819 /* blend (colormask) */
820 memset(&blend
, 0, sizeof(blend
));
821 cso_set_blend(cso
, &blend
);
824 /* fragment shader state: TEX lookup program */
825 cso_set_fragment_shader_handle(cso
, driver_fp
);
827 /* vertex shader state: position + texcoord pass-through */
828 cso_set_vertex_shader_handle(cso
, driver_vp
);
830 /* disable other shaders */
831 cso_set_tessctrl_shader_handle(cso
, NULL
);
832 cso_set_tesseval_shader_handle(cso
, NULL
);
833 cso_set_geometry_shader_handle(cso
, NULL
);
835 /* user samplers, plus the drawpix samplers */
837 struct pipe_sampler_state sampler
;
839 memset(&sampler
, 0, sizeof(sampler
));
840 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
841 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
842 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
843 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
844 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
845 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
846 sampler
.normalized_coords
= normalized
;
849 /* drawing a color image */
850 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
851 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
852 fpv
->pixelmap_sampler
+ 1,
853 st
->state
.num_frag_samplers
);
856 for (i
= 0; i
< st
->state
.num_frag_samplers
; i
++)
857 samplers
[i
] = &st
->state
.frag_samplers
[i
];
859 samplers
[fpv
->drawpix_sampler
] = &sampler
;
861 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
863 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
865 /* drawing a depth/stencil image */
866 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
868 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
872 /* user textures, plus the drawpix textures */
874 /* drawing a color image */
875 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
876 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
877 fpv
->pixelmap_sampler
+ 1,
878 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
880 memcpy(sampler_views
, st
->state
.frag_sampler_views
,
881 sizeof(sampler_views
));
883 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
885 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
886 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
888 /* drawing a depth/stencil image */
889 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
892 /* viewport state: viewport matching window dims */
893 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
895 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
896 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
898 /* Compute Gallium window coords (y=0=top) with pixel zoom.
899 * Recall that these coords are transformed by the current
900 * vertex shader and viewport transformation.
902 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
903 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
904 invertTex
= !invertTex
;
908 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
910 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
912 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
916 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
917 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
918 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
919 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
920 const float maxXcoord
= normalized
?
921 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
922 const float maxYcoord
= normalized
923 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
924 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
925 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
926 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
928 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
929 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
930 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
935 cso_restore_state(cso
);
940 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
941 * can't use a fragment shader to write stencil values.
944 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
945 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
946 const struct gl_pixelstore_attrib
*unpack
,
949 struct st_context
*st
= st_context(ctx
);
950 struct pipe_context
*pipe
= st
->pipe
;
951 struct st_renderbuffer
*strb
;
952 enum pipe_transfer_usage usage
;
953 struct pipe_transfer
*pt
;
954 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
956 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
961 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
963 /* totally clipped */
968 strb
= st_renderbuffer(ctx
->DrawBuffer
->
969 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
971 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
972 y
= ctx
->DrawBuffer
->Height
- y
- height
;
975 if (format
== GL_STENCIL_INDEX
&&
976 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
977 /* writing stencil to a combined depth+stencil buffer */
978 usage
= PIPE_TRANSFER_READ_WRITE
;
981 usage
= PIPE_TRANSFER_WRITE
;
984 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
985 strb
->surface
->u
.tex
.level
,
986 strb
->surface
->u
.tex
.first_layer
,
990 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
993 sValues
= malloc(width
* sizeof(GLubyte
));
994 zValues
= malloc(width
* sizeof(GLuint
));
996 if (sValues
&& zValues
) {
998 for (row
= 0; row
< height
; row
++) {
999 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
1000 GLenum destType
= GL_UNSIGNED_BYTE
;
1001 const void *source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
1005 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
1006 type
, source
, &clippedUnpack
,
1007 ctx
->_ImageTransferState
);
1009 if (format
== GL_DEPTH_STENCIL
) {
1011 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
1012 GL_FLOAT
: GL_UNSIGNED_INT
;
1014 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
1015 (1 << 24) - 1, type
, source
,
1020 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
1021 "zoom not complete");
1027 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1028 spanY
= height
- row
- 1;
1034 /* now pack the stencil (and Z) values in the dest format */
1035 switch (pt
->resource
->format
) {
1036 case PIPE_FORMAT_S8_UINT
:
1038 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
1039 assert(usage
== PIPE_TRANSFER_WRITE
);
1040 memcpy(dest
, sValues
, width
);
1043 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1044 if (format
== GL_DEPTH_STENCIL
) {
1045 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1047 assert(usage
== PIPE_TRANSFER_WRITE
);
1048 for (k
= 0; k
< width
; k
++) {
1049 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
1053 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1055 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1056 for (k
= 0; k
< width
; k
++) {
1057 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
1061 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1062 if (format
== GL_DEPTH_STENCIL
) {
1063 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1065 assert(usage
== PIPE_TRANSFER_WRITE
);
1066 for (k
= 0; k
< width
; k
++) {
1067 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
1071 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1073 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1074 for (k
= 0; k
< width
; k
++) {
1075 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
1079 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1080 if (format
== GL_DEPTH_STENCIL
) {
1081 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1082 GLfloat
*destf
= (GLfloat
*)dest
;
1084 assert(usage
== PIPE_TRANSFER_WRITE
);
1085 for (k
= 0; k
< width
; k
++) {
1086 destf
[k
*2] = zValuesFloat
[k
];
1087 dest
[k
*2+1] = sValues
[k
] & 0xff;
1091 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1093 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1094 for (k
= 0; k
< width
; k
++) {
1095 dest
[k
*2+1] = sValues
[k
] & 0xff;
1106 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
1112 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
1114 /* unmap the stencil buffer */
1115 pipe_transfer_unmap(pipe
, pt
);
1120 * Get fragment program variant for a glDrawPixels or glCopyPixels
1121 * command for RGBA data.
1123 static struct st_fp_variant
*
1124 get_color_fp_variant(struct st_context
*st
)
1126 struct gl_context
*ctx
= st
->ctx
;
1127 struct st_fp_variant_key key
;
1128 struct st_fp_variant
*fpv
;
1130 memset(&key
, 0, sizeof(key
));
1132 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1134 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1135 ctx
->Pixel
.RedScale
!= 1.0 ||
1136 ctx
->Pixel
.GreenBias
!= 0.0 ||
1137 ctx
->Pixel
.GreenScale
!= 1.0 ||
1138 ctx
->Pixel
.BlueBias
!= 0.0 ||
1139 ctx
->Pixel
.BlueScale
!= 1.0 ||
1140 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1141 ctx
->Pixel
.AlphaScale
!= 1.0);
1142 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1143 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1144 ctx
->Color
._ClampFragmentColor
;
1146 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1152 * Get fragment program variant for a glDrawPixels command
1153 * for COLOR_INDEX data
1155 static struct st_fp_variant
*
1156 get_color_index_fp_variant(struct st_context
*st
)
1158 struct gl_context
*ctx
= st
->ctx
;
1159 struct st_fp_variant_key key
;
1160 struct st_fp_variant
*fpv
;
1162 memset(&key
, 0, sizeof(key
));
1164 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1166 /* Since GL is always in RGBA mode MapColorFlag does not
1167 * affect GL_COLOR_INDEX format.
1168 * Scale and bias also never affect GL_COLOR_INDEX format.
1170 key
.scaleAndBias
= 0;
1172 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1173 ctx
->Color
._ClampFragmentColor
;
1175 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1182 * Clamp glDrawPixels width and height to the maximum texture size.
1185 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1186 struct gl_pixelstore_attrib
*unpack
)
1188 const int maxSize
= pipe
->screen
->get_param(pipe
->screen
,
1189 PIPE_CAP_MAX_TEXTURE_2D_SIZE
);
1191 if (*width
> maxSize
) {
1192 if (unpack
->RowLength
== 0)
1193 unpack
->RowLength
= *width
;
1196 if (*height
> maxSize
) {
1203 * Search the array of 4 swizzle components for the named component and return
1207 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
1210 for (i
= 0; i
< 4; i
++) {
1211 if (swizzle
[i
] == component
)
1214 assert(!"search_swizzle() failed");
1220 * Set the sampler view's swizzle terms. This is used to handle RGBA
1221 * swizzling when the incoming image format isn't an exact match for
1222 * the actual texture format. For example, if we have glDrawPixels(
1223 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1224 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1225 * avoid swizzling all the pixels in software in the texstore code.
1228 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1230 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1231 const struct util_format_description
*desc
=
1232 util_format_description(sv
->texture
->format
);
1233 unsigned c0
, c1
, c2
, c3
;
1235 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1236 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1238 assert(desc
->block
.bits
== 32);
1240 /* invert the format's swizzle to setup the sampler's swizzle */
1241 if (format
== GL_RGBA
) {
1242 c0
= PIPE_SWIZZLE_X
;
1243 c1
= PIPE_SWIZZLE_Y
;
1244 c2
= PIPE_SWIZZLE_Z
;
1245 c3
= PIPE_SWIZZLE_W
;
1248 assert(format
== GL_BGRA
);
1249 c0
= PIPE_SWIZZLE_Z
;
1250 c1
= PIPE_SWIZZLE_Y
;
1251 c2
= PIPE_SWIZZLE_X
;
1252 c3
= PIPE_SWIZZLE_W
;
1254 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1255 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1256 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1257 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1260 /* use the default sampler swizzle */
1266 * Compute the effective raster z position. This performs depth-clamping
1270 get_effective_raster_z(struct gl_context
*ctx
)
1272 float z
= ctx
->Current
.RasterPos
[2];
1273 if (st_context(ctx
)->clamp_frag_depth_in_shader
) {
1276 if (ctx
->ViewportArray
[0].Near
< ctx
->ViewportArray
[0].Far
) {
1277 depth_near
= ctx
->ViewportArray
[0].Near
;
1278 depth_far
= ctx
->ViewportArray
[0].Far
;
1280 depth_near
= ctx
->ViewportArray
[0].Far
;
1281 depth_far
= ctx
->ViewportArray
[0].Near
;
1284 if (ctx
->Transform
.DepthClampNear
)
1285 z
= MAX2(z
, depth_near
);
1286 if (ctx
->Transform
.DepthClampFar
)
1287 z
= MIN2(z
, depth_far
);
1294 * Called via ctx->Driver.DrawPixels()
1297 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1298 GLsizei width
, GLsizei height
,
1299 GLenum format
, GLenum type
,
1300 const struct gl_pixelstore_attrib
*unpack
, const void *pixels
)
1303 struct st_context
*st
= st_context(ctx
);
1304 struct pipe_context
*pipe
= st
->pipe
;
1305 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1306 struct pipe_sampler_view
*sv
[2] = { NULL
};
1307 int num_sampler_view
= 1;
1308 struct gl_pixelstore_attrib clippedUnpack
;
1309 struct st_fp_variant
*fpv
= NULL
;
1310 struct pipe_resource
*pt
;
1312 /* Mesa state should be up to date by now */
1313 assert(ctx
->NewState
== 0x0);
1315 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1317 st_flush_bitmap_cache(st
);
1318 st_invalidate_readpix_cache(st
);
1320 st_validate_state(st
, ST_PIPELINE_META
);
1322 /* Limit the size of the glDrawPixels to the max texture size.
1323 * Strictly speaking, that's not correct but since we don't handle
1324 * larger images yet, this is better than crashing.
1326 clippedUnpack
= *unpack
;
1327 unpack
= &clippedUnpack
;
1328 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1330 if (format
== GL_DEPTH_STENCIL
)
1331 write_stencil
= write_depth
= GL_TRUE
;
1332 else if (format
== GL_STENCIL_INDEX
)
1333 write_stencil
= GL_TRUE
;
1334 else if (format
== GL_DEPTH_COMPONENT
)
1335 write_depth
= GL_TRUE
;
1337 if (write_stencil
&&
1338 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1339 /* software fallback */
1340 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1345 /* Put glDrawPixels image into a texture */
1346 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1348 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1352 st_make_passthrough_vertex_shader(st
);
1355 * Get vertex/fragment shaders
1357 if (write_depth
|| write_stencil
) {
1358 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1362 fpv
= (format
!= GL_COLOR_INDEX
) ? get_color_fp_variant(st
) :
1363 get_color_index_fp_variant(st
);
1365 driver_fp
= fpv
->driver_shader
;
1367 if (ctx
->Pixel
.MapColorFlag
&& format
!= GL_COLOR_INDEX
) {
1368 pipe_sampler_view_reference(&sv
[1],
1369 st
->pixel_xfer
.pixelmap_sampler_view
);
1373 /* compiling a new fragment shader variant added new state constants
1374 * into the constant buffer, we need to update them
1376 st_upload_constants(st
, &st
->fp
->Base
);
1379 /* create sampler view for the image */
1380 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1382 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1383 pipe_resource_reference(&pt
, NULL
);
1387 /* Set up the sampler view's swizzle */
1388 setup_sampler_swizzle(sv
[0], format
, type
);
1390 /* Create a second sampler view to read stencil. The stencil is
1391 * written using the shader stencil export functionality.
1393 if (write_stencil
) {
1394 enum pipe_format stencil_format
=
1395 util_format_stencil_only(pt
->format
);
1396 /* we should not be doing pixel map/transfer (see above) */
1397 assert(num_sampler_view
== 1);
1398 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1401 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1402 pipe_resource_reference(&pt
, NULL
);
1403 pipe_sampler_view_reference(&sv
[0], NULL
);
1409 draw_textured_quad(ctx
, x
, y
, get_effective_raster_z(ctx
),
1411 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1416 ctx
->Current
.RasterColor
,
1417 GL_FALSE
, write_depth
, write_stencil
);
1418 pipe_sampler_view_reference(&sv
[0], NULL
);
1419 if (num_sampler_view
> 1)
1420 pipe_sampler_view_reference(&sv
[1], NULL
);
1422 /* free the texture (but may persist in the cache) */
1423 pipe_resource_reference(&pt
, NULL
);
1429 * Software fallback for glCopyPixels(GL_STENCIL).
1432 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1433 GLsizei width
, GLsizei height
,
1434 GLint dstx
, GLint dsty
)
1436 struct st_renderbuffer
*rbDraw
;
1437 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1438 enum pipe_transfer_usage usage
;
1439 struct pipe_transfer
*ptDraw
;
1444 buffer
= malloc(width
* height
* sizeof(ubyte
));
1446 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1450 /* Get the dest renderbuffer */
1451 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1452 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1454 /* this will do stencil pixel transfer ops */
1455 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1456 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1457 &ctx
->DefaultPacking
, buffer
);
1460 /* debug code: dump stencil values */
1462 for (row
= 0; row
< height
; row
++) {
1463 printf("%3d: ", row
);
1464 for (col
= 0; col
< width
; col
++) {
1465 printf("%02x ", buffer
[col
+ row
* width
]);
1471 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1472 usage
= PIPE_TRANSFER_READ_WRITE
;
1474 usage
= PIPE_TRANSFER_WRITE
;
1476 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1477 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1480 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1481 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1483 /* map the stencil buffer */
1484 drawMap
= pipe_transfer_map(pipe
,
1486 rbDraw
->surface
->u
.tex
.level
,
1487 rbDraw
->surface
->u
.tex
.first_layer
,
1489 width
, height
, &ptDraw
);
1492 /* XXX PixelZoom not handled yet */
1493 for (i
= 0; i
< height
; i
++) {
1500 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1504 dst
= drawMap
+ y
* ptDraw
->stride
;
1505 src
= buffer
+ i
* width
;
1507 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1512 /* unmap the stencil buffer */
1513 pipe_transfer_unmap(pipe
, ptDraw
);
1518 * Return renderbuffer to use for reading color pixels for glCopyPixels
1520 static struct st_renderbuffer
*
1521 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1523 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1524 struct st_renderbuffer
*strb
=
1525 st_renderbuffer(fb
->_ColorReadBuffer
);
1532 * Try to do a glCopyPixels for simple cases with a blit by calling
1535 * We can do this when we're copying color pixels (depth/stencil
1536 * eventually) with no pixel zoom, no pixel transfer ops, no
1537 * per-fragment ops, and the src/dest regions don't overlap.
1540 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1541 GLsizei width
, GLsizei height
,
1542 GLint dstx
, GLint dsty
, GLenum type
)
1544 struct st_context
*st
= st_context(ctx
);
1545 struct pipe_context
*pipe
= st
->pipe
;
1546 struct pipe_screen
*screen
= pipe
->screen
;
1547 struct gl_pixelstore_attrib pack
, unpack
;
1548 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1550 if (ctx
->Pixel
.ZoomX
== 1.0 &&
1551 ctx
->Pixel
.ZoomY
== 1.0 &&
1552 (type
!= GL_COLOR
||
1553 (ctx
->_ImageTransferState
== 0x0 &&
1554 !ctx
->Color
.BlendEnabled
&&
1555 !ctx
->Color
.AlphaEnabled
&&
1556 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1558 !ctx
->Fog
.Enabled
&&
1559 !ctx
->Stencil
.Enabled
&&
1560 !ctx
->FragmentProgram
.Enabled
&&
1561 !ctx
->VertexProgram
.Enabled
&&
1562 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1563 !_mesa_ati_fragment_shader_enabled(ctx
) &&
1564 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1)) &&
1565 !ctx
->Query
.CondRenderQuery
&&
1566 !ctx
->Query
.CurrentOcclusionObject
) {
1567 struct st_renderbuffer
*rbRead
, *rbDraw
;
1570 * Clip the read region against the src buffer bounds.
1571 * We'll still allocate a temporary buffer/texture for the original
1572 * src region size but we'll only read the region which is on-screen.
1573 * This may mean that we draw garbage pixels into the dest region, but
1580 pack
= ctx
->DefaultPacking
;
1581 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1582 return GL_TRUE
; /* all done */
1584 /* clip against dest buffer bounds and scissor box */
1585 drawX
= dstx
+ pack
.SkipPixels
;
1586 drawY
= dsty
+ pack
.SkipRows
;
1588 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1589 return GL_TRUE
; /* all done */
1591 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1592 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1597 if (type
== GL_COLOR
) {
1598 rbRead
= st_get_color_read_renderbuffer(ctx
);
1599 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1600 } else if (type
== GL_DEPTH
|| type
== GL_DEPTH_STENCIL
) {
1601 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1602 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1603 } else if (type
== GL_STENCIL
) {
1604 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1605 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1610 /* Flip src/dst position depending on the orientation of buffers. */
1611 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1612 readY
= rbRead
->Base
.Height
- readY
;
1616 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1617 /* We can't flip the destination for pipe->blit, so we only adjust
1618 * its position and flip the source.
1620 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1625 if (rbRead
!= rbDraw
||
1626 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1627 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1628 struct pipe_blit_info blit
;
1630 memset(&blit
, 0, sizeof(blit
));
1631 blit
.src
.resource
= rbRead
->texture
;
1632 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1633 blit
.src
.format
= rbRead
->texture
->format
;
1634 blit
.src
.box
.x
= readX
;
1635 blit
.src
.box
.y
= readY
;
1636 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1637 blit
.src
.box
.width
= readW
;
1638 blit
.src
.box
.height
= readH
;
1639 blit
.src
.box
.depth
= 1;
1640 blit
.dst
.resource
= rbDraw
->texture
;
1641 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1642 blit
.dst
.format
= rbDraw
->texture
->format
;
1643 blit
.dst
.box
.x
= drawX
;
1644 blit
.dst
.box
.y
= drawY
;
1645 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1646 blit
.dst
.box
.width
= drawW
;
1647 blit
.dst
.box
.height
= drawH
;
1648 blit
.dst
.box
.depth
= 1;
1649 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1651 if (type
== GL_COLOR
)
1652 blit
.mask
|= PIPE_MASK_RGBA
;
1653 if (type
== GL_DEPTH
)
1654 blit
.mask
|= PIPE_MASK_Z
;
1655 if (type
== GL_STENCIL
)
1656 blit
.mask
|= PIPE_MASK_S
;
1657 if (type
== GL_DEPTH_STENCIL
)
1658 blit
.mask
|= PIPE_MASK_ZS
;
1660 if (ctx
->DrawBuffer
!= ctx
->WinSysDrawBuffer
)
1661 st_window_rectangles_to_blit(ctx
, &blit
);
1663 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1664 blit
.src
.resource
->target
,
1665 blit
.src
.resource
->nr_samples
,
1666 blit
.src
.resource
->nr_storage_samples
,
1667 PIPE_BIND_SAMPLER_VIEW
) &&
1668 screen
->is_format_supported(screen
, blit
.dst
.format
,
1669 blit
.dst
.resource
->target
,
1670 blit
.dst
.resource
->nr_samples
,
1671 blit
.dst
.resource
->nr_storage_samples
,
1672 PIPE_BIND_RENDER_TARGET
)) {
1673 pipe
->blit(pipe
, &blit
);
1684 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1685 GLsizei width
, GLsizei height
,
1686 GLint dstx
, GLint dsty
, GLenum type
)
1688 struct st_context
*st
= st_context(ctx
);
1689 struct pipe_context
*pipe
= st
->pipe
;
1690 struct pipe_screen
*screen
= pipe
->screen
;
1691 struct st_renderbuffer
*rbRead
;
1693 struct pipe_resource
*pt
;
1694 struct pipe_sampler_view
*sv
[2] = { NULL
};
1695 struct st_fp_variant
*fpv
= NULL
;
1696 int num_sampler_view
= 1;
1697 enum pipe_format srcFormat
;
1699 GLboolean invertTex
= GL_FALSE
;
1700 GLint readX
, readY
, readW
, readH
;
1701 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1703 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1705 st_flush_bitmap_cache(st
);
1706 st_invalidate_readpix_cache(st
);
1708 st_validate_state(st
, ST_PIPELINE_META
);
1710 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1713 if (type
== GL_DEPTH_STENCIL
) {
1714 /* XXX make this more efficient */
1715 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1716 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1720 if (type
== GL_STENCIL
) {
1721 /* can't use texturing to do stencil */
1722 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1727 * The subsequent code implements glCopyPixels by copying the source
1728 * pixels into a temporary texture that's then applied to a textured quad.
1729 * When we draw the textured quad, all the usual per-fragment operations
1733 st_make_passthrough_vertex_shader(st
);
1736 * Get vertex/fragment shaders
1738 if (type
== GL_COLOR
) {
1739 fpv
= get_color_fp_variant(st
);
1741 rbRead
= st_get_color_read_renderbuffer(ctx
);
1743 driver_fp
= fpv
->driver_shader
;
1745 if (ctx
->Pixel
.MapColorFlag
) {
1746 pipe_sampler_view_reference(&sv
[1],
1747 st
->pixel_xfer
.pixelmap_sampler_view
);
1751 /* compiling a new fragment shader variant added new state constants
1752 * into the constant buffer, we need to update them
1754 st_upload_constants(st
, &st
->fp
->Base
);
1757 assert(type
== GL_DEPTH
);
1758 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1759 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1761 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1764 /* Choose the format for the temporary texture. */
1765 srcFormat
= rbRead
->texture
->format
;
1766 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1767 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1769 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1771 /* srcFormat is non-renderable. Find a compatible renderable format. */
1772 if (type
== GL_DEPTH
) {
1773 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1774 GL_NONE
, st
->internal_target
, 0, 0,
1778 assert(type
== GL_COLOR
);
1780 if (util_format_is_float(srcFormat
)) {
1781 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1782 GL_NONE
, st
->internal_target
, 0, 0,
1785 else if (util_format_is_pure_sint(srcFormat
)) {
1786 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1787 GL_NONE
, st
->internal_target
, 0, 0,
1790 else if (util_format_is_pure_uint(srcFormat
)) {
1791 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1792 GL_NONE
, st
->internal_target
, 0, 0,
1795 else if (util_format_is_snorm(srcFormat
)) {
1796 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1797 GL_NONE
, st
->internal_target
, 0, 0,
1801 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1802 GL_NONE
, st
->internal_target
, 0, 0,
1807 if (srcFormat
== PIPE_FORMAT_NONE
) {
1808 assert(0 && "cannot choose a format for src of CopyPixels");
1813 /* Invert src region if needed */
1814 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1815 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1816 invertTex
= !invertTex
;
1819 /* Clip the read region against the src buffer bounds.
1820 * We'll still allocate a temporary buffer/texture for the original
1821 * src region size but we'll only read the region which is on-screen.
1822 * This may mean that we draw garbage pixels into the dest region, but
1829 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1830 /* The source region is completely out of bounds. Do nothing.
1831 * The GL spec says "Results of copies from outside the window,
1832 * or from regions of the window that are not exposed, are
1833 * hardware dependent and undefined."
1838 readW
= MAX2(0, readW
);
1839 readH
= MAX2(0, readH
);
1841 /* Allocate the temporary texture. */
1842 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1846 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1848 pipe_resource_reference(&pt
, NULL
);
1852 /* Copy the src region to the temporary texture. */
1854 struct pipe_blit_info blit
;
1856 memset(&blit
, 0, sizeof(blit
));
1857 blit
.src
.resource
= rbRead
->texture
;
1858 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1859 blit
.src
.format
= rbRead
->texture
->format
;
1860 blit
.src
.box
.x
= readX
;
1861 blit
.src
.box
.y
= readY
;
1862 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1863 blit
.src
.box
.width
= readW
;
1864 blit
.src
.box
.height
= readH
;
1865 blit
.src
.box
.depth
= 1;
1866 blit
.dst
.resource
= pt
;
1868 blit
.dst
.format
= pt
->format
;
1869 blit
.dst
.box
.x
= pack
.SkipPixels
;
1870 blit
.dst
.box
.y
= pack
.SkipRows
;
1872 blit
.dst
.box
.width
= readW
;
1873 blit
.dst
.box
.height
= readH
;
1874 blit
.dst
.box
.depth
= 1;
1875 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1876 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1878 pipe
->blit(pipe
, &blit
);
1881 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1882 * textured quad with that texture.
1885 draw_textured_quad(ctx
, dstx
, dsty
, get_effective_raster_z(ctx
),
1886 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1891 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1892 invertTex
, GL_FALSE
, GL_FALSE
);
1894 pipe_resource_reference(&pt
, NULL
);
1895 pipe_sampler_view_reference(&sv
[0], NULL
);
1900 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1902 functions
->DrawPixels
= st_DrawPixels
;
1903 functions
->CopyPixels
= st_CopyPixels
;
1908 st_destroy_drawpix(struct st_context
*st
)
1912 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1913 if (st
->drawpix
.zs_shaders
[i
])
1914 cso_delete_fragment_shader(st
->cso_context
,
1915 st
->drawpix
.zs_shaders
[i
]);
1918 if (st
->passthrough_vs
)
1919 cso_delete_vertex_shader(st
->cso_context
, st
->passthrough_vs
);
1921 /* Free cache data */
1922 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
1923 struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
1925 pipe_resource_reference(&entry
->texture
, NULL
);