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"
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/format/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
, enum glsl_base_type base_type
,
115 nir_alu_type alu_type
)
117 const struct glsl_type
*sampler2D
=
118 glsl_sampler_type(GLSL_SAMPLER_DIM_2D
, false, false, base_type
);
121 nir_variable_create(b
->shader
, nir_var_uniform
, sampler2D
, name
);
122 var
->data
.binding
= sampler
;
123 var
->data
.explicit_binding
= true;
125 nir_deref_instr
*deref
= nir_build_deref_var(b
, var
);
127 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, 3);
128 tex
->op
= nir_texop_tex
;
129 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
130 tex
->coord_components
= 2;
131 tex
->dest_type
= alu_type
;
132 tex
->src
[0].src_type
= nir_tex_src_texture_deref
;
133 tex
->src
[0].src
= nir_src_for_ssa(&deref
->dest
.ssa
);
134 tex
->src
[1].src_type
= nir_tex_src_sampler_deref
;
135 tex
->src
[1].src
= nir_src_for_ssa(&deref
->dest
.ssa
);
136 tex
->src
[2].src_type
= nir_tex_src_coord
;
138 nir_src_for_ssa(nir_channels(b
, nir_load_var(b
, texcoord
),
139 (1 << tex
->coord_components
) - 1));
141 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
142 nir_builder_instr_insert(b
, &tex
->instr
);
143 return nir_channel(b
, &tex
->dest
.ssa
, 0);
147 make_drawpix_z_stencil_program_nir(struct st_context
*st
,
151 struct nir_builder b
;
152 const nir_shader_compiler_options
*options
=
153 st
->ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
].NirOptions
;
155 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, options
);
157 nir_variable
*texcoord
=
158 nir_variable_create(b
.shader
, nir_var_shader_in
, glsl_vec_type(2),
160 texcoord
->data
.location
= VARYING_SLOT_TEX0
;
164 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_float_type(),
166 out
->data
.location
= FRAG_RESULT_DEPTH
;
167 nir_ssa_def
*depth
= sample_via_nir(&b
, texcoord
, "depth", 0,
168 GLSL_TYPE_FLOAT
, nir_type_float
);
169 nir_store_var(&b
, out
, depth
, 0x1);
171 /* Also copy color */
172 nir_variable
*color_in
=
173 nir_variable_create(b
.shader
, nir_var_shader_in
, glsl_vec_type(4),
175 color_in
->data
.location
= VARYING_SLOT_COL0
;
177 nir_variable
*color_out
=
178 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_vec_type(4),
180 color_out
->data
.location
= FRAG_RESULT_COLOR
;
181 nir_copy_var(&b
, color_out
, color_in
);
186 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_uint_type(),
187 "gl_FragStencilRefARB");
188 out
->data
.location
= FRAG_RESULT_STENCIL
;
189 nir_ssa_def
*stencil
= sample_via_nir(&b
, texcoord
, "stencil", 1,
190 GLSL_TYPE_UINT
, nir_type_uint
);
191 nir_store_var(&b
, out
, stencil
, 0x1);
195 snprintf(name
, 14, "drawpixels %s%s",
196 write_depth
? "Z" : "", write_stencil
? "S" : "");
198 return st_nir_finish_builtin_shader(st
, b
.shader
, name
);
202 make_drawpix_zs_to_color_program_nir(struct st_context
*st
,
205 struct nir_builder b
;
206 const nir_shader_compiler_options
*options
=
207 st
->ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
].NirOptions
;
209 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, options
);
211 nir_variable
*texcoord
=
212 nir_variable_create(b
.shader
, nir_var_shader_in
, glsl_vec_type(2),
214 texcoord
->data
.location
= VARYING_SLOT_TEX0
;
216 /* Sample depth and stencil */
217 nir_ssa_def
*depth
= sample_via_nir(&b
, texcoord
, "depth", 0,
218 GLSL_TYPE_FLOAT
, nir_type_float
);
219 nir_ssa_def
*stencil
= sample_via_nir(&b
, texcoord
, "stencil", 1,
220 GLSL_TYPE_UINT
, nir_type_uint
);
222 /* Create the variable to store the output color */
223 nir_variable
*color_out
=
224 nir_variable_create(b
.shader
, nir_var_shader_out
, glsl_vec_type(4),
225 "make_drawpix_zs_to_color_program_nirgl_FragColor");
226 color_out
->data
.location
= FRAG_RESULT_COLOR
;
228 nir_ssa_def
*shifted_depth
= nir_fmul(&b
,nir_f2f64(&b
, depth
), nir_imm_double(&b
,0xffffff));
229 nir_ssa_def
*int_depth
= nir_f2u32(&b
,shifted_depth
);
232 ds
[0] = nir_ubitfield_extract(&b
, stencil
, nir_imm_int(&b
, 0), nir_imm_int(&b
,8));
233 ds
[1] = nir_ubitfield_extract(&b
, int_depth
, nir_imm_int(&b
, 0), nir_imm_int(&b
,8));
234 ds
[2] = nir_ubitfield_extract(&b
, int_depth
, nir_imm_int(&b
, 8), nir_imm_int(&b
,8));
235 ds
[3] = nir_ubitfield_extract(&b
, int_depth
, nir_imm_int(&b
, 16), nir_imm_int(&b
,8));
237 nir_ssa_def
*ds_comp
[4];
238 ds_comp
[0] = nir_fsat(&b
, nir_fmul_imm(&b
, nir_u2f32(&b
, ds
[3]), 1.0/255.0));
239 ds_comp
[1] = nir_fsat(&b
, nir_fmul_imm(&b
, nir_u2f32(&b
, ds
[2]), 1.0/255.0));
240 ds_comp
[2] = nir_fsat(&b
, nir_fmul_imm(&b
, nir_u2f32(&b
, ds
[1]), 1.0/255.0));
241 ds_comp
[3] = nir_fsat(&b
, nir_fmul_imm(&b
, nir_u2f32(&b
, ds
[0]), 1.0/255.0));
243 nir_ssa_def
*unpacked_ds
= nir_vec4(&b
, ds_comp
[0], ds_comp
[1], ds_comp
[2], ds_comp
[3]);
246 nir_store_var(&b
, color_out
, unpacked_ds
, 0xf);
249 unsigned zyxw
[4] = { 2, 1, 0, 3 };
250 nir_ssa_def
*swizzled_ds
= nir_swizzle(&b
, unpacked_ds
, zyxw
, 4);
251 nir_store_var(&b
, color_out
, swizzled_ds
, 0xf);
255 snprintf(name
, 17, "copypixels ZStoC");
257 return st_nir_finish_builtin_shader(st
, b
.shader
, name
);
261 make_drawpix_z_stencil_program_tgsi(struct st_context
*st
,
265 struct ureg_program
*ureg
;
266 struct ureg_src depth_sampler
, stencil_sampler
;
267 struct ureg_src texcoord
, color
;
268 struct ureg_dst out_color
, out_depth
, out_stencil
;
270 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
274 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
277 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
278 TGSI_INTERPOLATE_COLOR
);
279 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
281 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
282 ureg_DECL_sampler_view(ureg
, 0, TGSI_TEXTURE_2D
,
283 TGSI_RETURN_TYPE_FLOAT
,
284 TGSI_RETURN_TYPE_FLOAT
,
285 TGSI_RETURN_TYPE_FLOAT
,
286 TGSI_RETURN_TYPE_FLOAT
);
287 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
291 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
292 ureg_DECL_sampler_view(ureg
, 1, TGSI_TEXTURE_2D
,
293 TGSI_RETURN_TYPE_UINT
,
294 TGSI_RETURN_TYPE_UINT
,
295 TGSI_RETURN_TYPE_UINT
,
296 TGSI_RETURN_TYPE_UINT
);
297 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
300 texcoord
= ureg_DECL_fs_input(ureg
,
301 st
->needs_texcoord_semantic
?
302 TGSI_SEMANTIC_TEXCOORD
:
303 TGSI_SEMANTIC_GENERIC
,
304 0, TGSI_INTERPOLATE_LINEAR
);
307 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
308 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
309 ureg_MOV(ureg
, out_color
, color
);
313 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
314 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
317 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
322 * Create fragment program that does a TEX() instruction to get a Z and/or
323 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
324 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
325 * Pass fragment color through as-is.
327 * \return CSO of the fragment shader.
330 get_drawpix_z_stencil_program(struct st_context
*st
,
334 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
335 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
338 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
340 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
341 /* already have the proper shader */
342 return st
->drawpix
.zs_shaders
[shaderIndex
];
345 enum pipe_shader_ir preferred_ir
=
346 pscreen
->get_shader_param(pscreen
, PIPE_SHADER_FRAGMENT
,
347 PIPE_SHADER_CAP_PREFERRED_IR
);
349 if (preferred_ir
== PIPE_SHADER_IR_NIR
)
350 cso
= make_drawpix_z_stencil_program_nir(st
, write_depth
, write_stencil
);
352 cso
= make_drawpix_z_stencil_program_tgsi(st
, write_depth
, write_stencil
);
354 /* save the new shader */
355 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
360 * Create fragment program that does a TEX() instruction to get a Z and
361 * stencil value value, then writes to FRAG_RESULT_COLOR.
362 * Used for glCopyPixels(GL_DEPTH_STENCIL_TO_RGBA_NV / GL_DEPTH_STENCIL_TO_BGRA_NV).
364 * \return CSO of the fragment shader.
367 get_drawpix_zs_to_color_program(struct st_context
*st
,
370 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
379 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
381 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
382 /* already have the proper shader */
383 return st
->drawpix
.zs_shaders
[shaderIndex
];
386 enum pipe_shader_ir preferred_ir
=
387 pscreen
->get_shader_param(pscreen
, PIPE_SHADER_FRAGMENT
,
388 PIPE_SHADER_CAP_PREFERRED_IR
);
390 if (preferred_ir
== PIPE_SHADER_IR_NIR
)
391 cso
= make_drawpix_zs_to_color_program_nir(st
, rgba
);
395 /* save the new shader */
396 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
401 * Create a simple vertex shader that just passes through the
402 * vertex position, texcoord, and color.
405 st_make_passthrough_vertex_shader(struct st_context
*st
)
407 struct pipe_context
*pipe
= st
->pipe
;
408 struct pipe_screen
*screen
= pipe
->screen
;
410 if (st
->passthrough_vs
)
413 enum pipe_shader_ir preferred_ir
=
414 screen
->get_shader_param(screen
, PIPE_SHADER_VERTEX
,
415 PIPE_SHADER_CAP_PREFERRED_IR
);
417 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
419 { VERT_ATTRIB_POS
, VERT_ATTRIB_COLOR0
, VERT_ATTRIB_GENERIC0
};
421 { VARYING_SLOT_POS
, VARYING_SLOT_COL0
, VARYING_SLOT_TEX0
};
424 st_nir_make_passthrough_shader(st
, "drawpixels VS",
425 MESA_SHADER_VERTEX
, 3,
426 inputs
, outputs
, NULL
, 0);
428 const enum tgsi_semantic semantic_names
[] = {
429 TGSI_SEMANTIC_POSITION
,
431 st
->needs_texcoord_semantic
? TGSI_SEMANTIC_TEXCOORD
:
432 TGSI_SEMANTIC_GENERIC
434 const uint semantic_indexes
[] = { 0, 0, 0 };
437 util_make_vertex_passthrough_shader(st
->pipe
, 3, semantic_names
,
438 semantic_indexes
, false);
444 * Return a texture internalFormat for drawing/copying an image
445 * of the given format and type.
448 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
451 case GL_DEPTH_COMPONENT
:
453 case GL_UNSIGNED_SHORT
:
454 return GL_DEPTH_COMPONENT16
;
456 case GL_UNSIGNED_INT
:
457 return GL_DEPTH_COMPONENT32
;
460 if (ctx
->Extensions
.ARB_depth_buffer_float
)
461 return GL_DEPTH_COMPONENT32F
;
463 return GL_DEPTH_COMPONENT
;
466 return GL_DEPTH_COMPONENT
;
469 case GL_DEPTH_STENCIL
:
471 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
472 return GL_DEPTH32F_STENCIL8
;
474 case GL_UNSIGNED_INT_24_8
:
476 return GL_DEPTH24_STENCIL8
;
479 case GL_STENCIL_INDEX
:
480 return GL_STENCIL_INDEX
;
483 if (_mesa_is_enum_format_integer(format
)) {
487 case GL_UNSIGNED_BYTE
:
491 case GL_UNSIGNED_SHORT
:
495 case GL_UNSIGNED_INT
:
498 assert(0 && "Unexpected type in internal_format()");
499 return GL_RGBA_INTEGER
;
504 case GL_UNSIGNED_BYTE
:
505 case GL_UNSIGNED_INT_8_8_8_8
:
506 case GL_UNSIGNED_INT_8_8_8_8_REV
:
510 case GL_UNSIGNED_BYTE_3_3_2
:
511 case GL_UNSIGNED_BYTE_2_3_3_REV
:
514 case GL_UNSIGNED_SHORT_4_4_4_4
:
515 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
518 case GL_UNSIGNED_SHORT_5_6_5
:
519 case GL_UNSIGNED_SHORT_5_6_5_REV
:
522 case GL_UNSIGNED_SHORT_5_5_5_1
:
523 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
526 case GL_UNSIGNED_INT_10_10_10_2
:
527 case GL_UNSIGNED_INT_2_10_10_10_REV
:
530 case GL_UNSIGNED_SHORT
:
531 case GL_UNSIGNED_INT
:
536 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
541 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
543 case GL_HALF_FLOAT_ARB
:
545 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
546 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
551 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
552 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
554 case GL_UNSIGNED_INT_5_9_9_9_REV
:
555 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
558 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
559 assert(ctx
->Extensions
.EXT_packed_float
);
560 return GL_R11F_G11F_B10F
;
568 * Create a temporary texture to hold an image of the given size.
569 * If width, height are not POT and the driver only handles POT textures,
570 * allocate the next larger size of texture that is POT.
572 static struct pipe_resource
*
573 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
574 enum pipe_format texFormat
, unsigned bind
)
576 struct pipe_resource
*pt
;
578 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
579 width
, height
, 1, 1, 0, bind
);
586 * Search the cache for an image which matches the given parameters.
587 * \return pipe_resource pointer if found, NULL if not found.
589 static struct pipe_resource
*
590 search_drawpixels_cache(struct st_context
*st
,
591 GLsizei width
, GLsizei height
,
592 GLenum format
, GLenum type
,
593 const struct gl_pixelstore_attrib
*unpack
,
596 struct pipe_resource
*pt
= NULL
;
597 const GLint bpp
= _mesa_bytes_per_pixel(format
, type
);
600 if ((unpack
->RowLength
!= 0 && unpack
->RowLength
!= width
) ||
601 unpack
->SkipPixels
!= 0 ||
602 unpack
->SkipRows
!= 0 ||
605 /* we don't allow non-default pixel unpacking values */
609 /* Search cache entries for a match */
610 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
611 struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
613 if (width
== entry
->width
&&
614 height
== entry
->height
&&
615 format
== entry
->format
&&
616 type
== entry
->type
&&
617 pixels
== entry
->user_pointer
&&
619 assert(entry
->texture
);
621 /* check if the pixel data is the same */
622 if (memcmp(pixels
, entry
->image
, width
* height
* bpp
) == 0) {
623 /* Success - found a cache match */
624 pipe_resource_reference(&pt
, entry
->texture
);
625 /* refcount of returned texture should be at least two here. One
626 * reference for the cache to hold on to, one for the caller (which
627 * it will release), and possibly more held by the driver.
629 assert(pt
->reference
.count
>= 2);
631 /* update the age of this entry */
632 entry
->age
= ++st
->drawpix_cache
.age
;
639 /* no cache match found */
645 * Find the oldest entry in the glDrawPixels cache. We'll replace this
646 * one when we need to store a new image.
648 static struct drawpix_cache_entry
*
649 find_oldest_drawpixels_cache_entry(struct st_context
*st
)
651 unsigned oldest_age
= ~0u, oldest_index
= ~0u;
654 /* Find entry with oldest (lowest) age */
655 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
656 const struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
657 if (entry
->age
< oldest_age
) {
658 oldest_age
= entry
->age
;
663 assert(oldest_index
!= ~0u);
665 return &st
->drawpix_cache
.entries
[oldest_index
];
670 * Try to save the given glDrawPixels image in the cache.
673 cache_drawpixels_image(struct st_context
*st
,
674 GLsizei width
, GLsizei height
,
675 GLenum format
, GLenum type
,
676 const struct gl_pixelstore_attrib
*unpack
,
678 struct pipe_resource
*pt
)
680 if ((unpack
->RowLength
== 0 || unpack
->RowLength
== width
) &&
681 unpack
->SkipPixels
== 0 &&
682 unpack
->SkipRows
== 0) {
683 const GLint bpp
= _mesa_bytes_per_pixel(format
, type
);
684 struct drawpix_cache_entry
*entry
=
685 find_oldest_drawpixels_cache_entry(st
);
687 entry
->width
= width
;
688 entry
->height
= height
;
689 entry
->format
= format
;
691 entry
->user_pointer
= pixels
;
693 entry
->image
= malloc(width
* height
* bpp
);
695 memcpy(entry
->image
, pixels
, width
* height
* bpp
);
696 pipe_resource_reference(&entry
->texture
, pt
);
697 entry
->age
= ++st
->drawpix_cache
.age
;
700 /* out of memory, free/disable cached texture */
703 pipe_resource_reference(&entry
->texture
, NULL
);
710 * Make texture containing an image for glDrawPixels image.
711 * If 'pixels' is NULL, leave the texture image data undefined.
713 static struct pipe_resource
*
714 make_texture(struct st_context
*st
,
715 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
716 const struct gl_pixelstore_attrib
*unpack
,
719 struct gl_context
*ctx
= st
->ctx
;
720 struct pipe_context
*pipe
= st
->pipe
;
722 struct pipe_resource
*pt
= NULL
;
723 enum pipe_format pipeFormat
;
724 GLenum baseInternalFormat
;
726 #if USE_DRAWPIXELS_CACHE
727 pt
= search_drawpixels_cache(st
, width
, height
, format
, type
,
734 /* Choose a pixel format for the temp texture which will hold the
737 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
738 format
, type
, unpack
->SwapBytes
);
740 if (pipeFormat
== PIPE_FORMAT_NONE
) {
741 /* Use the generic approach. */
742 GLenum intFormat
= internal_format(ctx
, format
, type
);
744 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
745 st
->internal_target
, 0, 0,
746 PIPE_BIND_SAMPLER_VIEW
,
748 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
751 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
752 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
754 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
758 /* alloc temporary texture */
759 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
761 _mesa_unmap_pbo_source(ctx
, unpack
);
766 struct pipe_transfer
*transfer
;
768 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
770 /* we'll do pixel transfer in a fragment shader */
771 ctx
->_ImageTransferState
= 0x0;
773 /* map texture transfer */
774 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
775 PIPE_TRANSFER_WRITE
, 0, 0,
776 width
, height
, &transfer
);
778 pipe_resource_reference(&pt
, NULL
);
779 _mesa_unmap_pbo_source(ctx
, unpack
);
783 /* Put image into texture transfer.
784 * Note that the image is actually going to be upside down in
785 * the texture. We deal with that with texcoords.
787 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
788 && type
== GL_UNSIGNED_BYTE
) {
789 /* Use a memcpy-based texstore to avoid software pixel swizzling.
790 * We'll do the necessary swizzling with the pipe_sampler_view to
791 * give much better performance.
792 * XXX in the future, expand this to accomodate more format and
795 _mesa_memcpy_texture(ctx
, 2,
796 mformat
, /* mesa_format */
797 transfer
->stride
, /* dstRowStride, bytes */
798 &dest
, /* destSlices */
799 width
, height
, 1, /* size */
800 format
, type
, /* src format/type */
801 pixels
, /* data source */
805 ASSERTED
bool success
;
806 success
= _mesa_texstore(ctx
, 2, /* dims */
807 baseInternalFormat
, /* baseInternalFormat */
808 mformat
, /* mesa_format */
809 transfer
->stride
, /* dstRowStride, bytes */
810 &dest
, /* destSlices */
811 width
, height
, 1, /* size */
812 format
, type
, /* src format/type */
813 pixels
, /* data source */
820 pipe_transfer_unmap(pipe
, transfer
);
823 ctx
->_ImageTransferState
= imageTransferStateSave
;
826 #if USE_DRAWPIXELS_CACHE
827 cache_drawpixels_image(st
, width
, height
, format
, type
, unpack
, pixels
, pt
);
830 _mesa_unmap_pbo_source(ctx
, unpack
);
837 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
838 GLsizei width
, GLsizei height
,
839 GLfloat zoomX
, GLfloat zoomY
,
840 struct pipe_sampler_view
**sv
,
841 int num_sampler_view
,
844 struct st_fp_variant
*fpv
,
845 const GLfloat
*color
,
847 GLboolean write_depth
, GLboolean write_stencil
)
849 struct st_context
*st
= st_context(ctx
);
850 struct pipe_context
*pipe
= st
->pipe
;
851 struct cso_context
*cso
= st
->cso_context
;
852 const unsigned fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
853 const unsigned fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
854 GLfloat x0
, y0
, x1
, y1
;
855 ASSERTED GLsizei maxSize
;
856 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
857 unsigned cso_state_mask
;
859 assert(sv
[0]->texture
->target
== st
->internal_target
);
862 /* XXX if DrawPixels image is larger than max texture size, break
865 maxSize
= pipe
->screen
->get_param(pipe
->screen
,
866 PIPE_CAP_MAX_TEXTURE_2D_SIZE
);
867 assert(width
<= maxSize
);
868 assert(height
<= maxSize
);
870 cso_state_mask
= (CSO_BIT_RASTERIZER
|
872 CSO_BIT_FRAGMENT_SAMPLERS
|
873 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
874 CSO_BIT_STREAM_OUTPUTS
|
875 CSO_BIT_VERTEX_ELEMENTS
|
876 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
877 CSO_BITS_ALL_SHADERS
);
879 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
882 cso_save_state(cso
, cso_state_mask
);
884 /* rasterizer state: just scissor */
886 struct pipe_rasterizer_state rasterizer
;
887 memset(&rasterizer
, 0, sizeof(rasterizer
));
888 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
889 ctx
->Color
._ClampFragmentColor
;
890 rasterizer
.half_pixel_center
= 1;
891 rasterizer
.bottom_edge_rule
= 1;
892 rasterizer
.depth_clip_near
= st
->clamp_frag_depth_in_shader
||
893 !ctx
->Transform
.DepthClampNear
;
894 rasterizer
.depth_clip_far
= st
->clamp_frag_depth_in_shader
||
895 !ctx
->Transform
.DepthClampFar
;
896 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
897 cso_set_rasterizer(cso
, &rasterizer
);
901 /* Stencil writing bypasses the normal fragment pipeline to
902 * disable color writing and set stencil test to always pass.
904 struct pipe_depth_stencil_alpha_state dsa
;
905 struct pipe_blend_state blend
;
908 memset(&dsa
, 0, sizeof(dsa
));
909 dsa
.stencil
[0].enabled
= 1;
910 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
911 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
912 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
914 /* writing depth+stencil: depth test always passes */
915 dsa
.depth
.enabled
= 1;
916 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
917 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
919 cso_set_depth_stencil_alpha(cso
, &dsa
);
921 /* blend (colormask) */
922 memset(&blend
, 0, sizeof(blend
));
923 cso_set_blend(cso
, &blend
);
926 /* fragment shader state: TEX lookup program */
927 cso_set_fragment_shader_handle(cso
, driver_fp
);
929 /* vertex shader state: position + texcoord pass-through */
930 cso_set_vertex_shader_handle(cso
, driver_vp
);
932 /* disable other shaders */
933 cso_set_tessctrl_shader_handle(cso
, NULL
);
934 cso_set_tesseval_shader_handle(cso
, NULL
);
935 cso_set_geometry_shader_handle(cso
, NULL
);
937 /* user samplers, plus the drawpix samplers */
939 struct pipe_sampler_state sampler
;
941 memset(&sampler
, 0, sizeof(sampler
));
942 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
943 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
944 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
945 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
946 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
947 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
948 sampler
.normalized_coords
= normalized
;
951 /* drawing a color image */
952 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
953 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
954 fpv
->pixelmap_sampler
+ 1,
955 st
->state
.num_frag_samplers
);
958 for (i
= 0; i
< st
->state
.num_frag_samplers
; i
++)
959 samplers
[i
] = &st
->state
.frag_samplers
[i
];
961 samplers
[fpv
->drawpix_sampler
] = &sampler
;
963 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
965 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
967 /* drawing a depth/stencil image */
968 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
970 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
974 /* user textures, plus the drawpix textures */
976 /* drawing a color image */
977 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
978 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
979 fpv
->pixelmap_sampler
+ 1,
980 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
982 memcpy(sampler_views
, st
->state
.frag_sampler_views
,
983 sizeof(sampler_views
));
985 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
987 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
988 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
990 /* drawing a depth/stencil image */
991 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
994 /* viewport state: viewport matching window dims */
995 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
997 st
->util_velems
.count
= 3;
998 cso_set_vertex_elements(cso
, &st
->util_velems
);
999 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
1001 /* Compute Gallium window coords (y=0=top) with pixel zoom.
1002 * Recall that these coords are transformed by the current
1003 * vertex shader and viewport transformation.
1005 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
1006 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
1007 invertTex
= !invertTex
;
1011 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
1013 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
1015 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
1016 z
= z
* 2.0f
- 1.0f
;
1019 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
1020 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
1021 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
1022 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
1023 const float maxXcoord
= normalized
?
1024 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
1025 const float maxYcoord
= normalized
1026 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
1027 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
1028 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
1029 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
1031 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
1032 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
1033 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1038 cso_restore_state(cso
);
1043 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
1044 * can't use a fragment shader to write stencil values.
1047 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1048 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
1049 const struct gl_pixelstore_attrib
*unpack
,
1052 struct st_context
*st
= st_context(ctx
);
1053 struct pipe_context
*pipe
= st
->pipe
;
1054 struct st_renderbuffer
*strb
;
1055 enum pipe_transfer_usage usage
;
1056 struct pipe_transfer
*pt
;
1057 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
1059 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
1064 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
1066 /* totally clipped */
1071 strb
= st_renderbuffer(ctx
->DrawBuffer
->
1072 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1074 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1075 y
= ctx
->DrawBuffer
->Height
- y
- height
;
1078 if (format
== GL_STENCIL_INDEX
&&
1079 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
1080 /* writing stencil to a combined depth+stencil buffer */
1081 usage
= PIPE_TRANSFER_READ_WRITE
;
1084 usage
= PIPE_TRANSFER_WRITE
;
1087 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
1088 strb
->surface
->u
.tex
.level
,
1089 strb
->surface
->u
.tex
.first_layer
,
1091 width
, height
, &pt
);
1093 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
1096 sValues
= malloc(width
* sizeof(GLubyte
));
1097 zValues
= malloc(width
* sizeof(GLuint
));
1099 if (sValues
&& zValues
) {
1101 for (row
= 0; row
< height
; row
++) {
1102 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
1103 GLenum destType
= GL_UNSIGNED_BYTE
;
1104 const void *source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
1108 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
1109 type
, source
, &clippedUnpack
,
1110 ctx
->_ImageTransferState
);
1112 if (format
== GL_DEPTH_STENCIL
) {
1114 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
1115 GL_FLOAT
: GL_UNSIGNED_INT
;
1117 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
1118 (1 << 24) - 1, type
, source
,
1123 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
1124 "zoom not complete");
1130 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1131 spanY
= height
- row
- 1;
1137 /* now pack the stencil (and Z) values in the dest format */
1138 switch (pt
->resource
->format
) {
1139 case PIPE_FORMAT_S8_UINT
:
1141 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
1142 assert(usage
== PIPE_TRANSFER_WRITE
);
1143 memcpy(dest
, sValues
, width
);
1146 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1147 if (format
== GL_DEPTH_STENCIL
) {
1148 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1150 assert(usage
== PIPE_TRANSFER_WRITE
);
1151 for (k
= 0; k
< width
; k
++) {
1152 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
1156 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1158 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1159 for (k
= 0; k
< width
; k
++) {
1160 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
1164 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1165 if (format
== GL_DEPTH_STENCIL
) {
1166 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1168 assert(usage
== PIPE_TRANSFER_WRITE
);
1169 for (k
= 0; k
< width
; k
++) {
1170 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
1174 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1176 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1177 for (k
= 0; k
< width
; k
++) {
1178 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
1182 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1183 if (format
== GL_DEPTH_STENCIL
) {
1184 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1185 GLfloat
*destf
= (GLfloat
*)dest
;
1187 assert(usage
== PIPE_TRANSFER_WRITE
);
1188 for (k
= 0; k
< width
; k
++) {
1189 destf
[k
*2] = zValuesFloat
[k
];
1190 dest
[k
*2+1] = sValues
[k
] & 0xff;
1194 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
1196 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1197 for (k
= 0; k
< width
; k
++) {
1198 dest
[k
*2+1] = sValues
[k
] & 0xff;
1209 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
1215 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
1217 /* unmap the stencil buffer */
1218 pipe_transfer_unmap(pipe
, pt
);
1223 * Get fragment program variant for a glDrawPixels or glCopyPixels
1224 * command for RGBA data.
1226 static struct st_fp_variant
*
1227 get_color_fp_variant(struct st_context
*st
)
1229 struct gl_context
*ctx
= st
->ctx
;
1230 struct st_fp_variant_key key
;
1231 struct st_fp_variant
*fpv
;
1233 memset(&key
, 0, sizeof(key
));
1235 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1237 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1238 ctx
->Pixel
.RedScale
!= 1.0 ||
1239 ctx
->Pixel
.GreenBias
!= 0.0 ||
1240 ctx
->Pixel
.GreenScale
!= 1.0 ||
1241 ctx
->Pixel
.BlueBias
!= 0.0 ||
1242 ctx
->Pixel
.BlueScale
!= 1.0 ||
1243 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1244 ctx
->Pixel
.AlphaScale
!= 1.0);
1245 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1246 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1247 ctx
->Color
._ClampFragmentColor
;
1249 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1255 * Get fragment program variant for a glDrawPixels command
1256 * for COLOR_INDEX data
1258 static struct st_fp_variant
*
1259 get_color_index_fp_variant(struct st_context
*st
)
1261 struct gl_context
*ctx
= st
->ctx
;
1262 struct st_fp_variant_key key
;
1263 struct st_fp_variant
*fpv
;
1265 memset(&key
, 0, sizeof(key
));
1267 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1269 /* Since GL is always in RGBA mode MapColorFlag does not
1270 * affect GL_COLOR_INDEX format.
1271 * Scale and bias also never affect GL_COLOR_INDEX format.
1273 key
.scaleAndBias
= 0;
1275 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1276 ctx
->Color
._ClampFragmentColor
;
1278 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1285 * Clamp glDrawPixels width and height to the maximum texture size.
1288 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1289 struct gl_pixelstore_attrib
*unpack
)
1291 const int maxSize
= pipe
->screen
->get_param(pipe
->screen
,
1292 PIPE_CAP_MAX_TEXTURE_2D_SIZE
);
1294 if (*width
> maxSize
) {
1295 if (unpack
->RowLength
== 0)
1296 unpack
->RowLength
= *width
;
1299 if (*height
> maxSize
) {
1306 * Search the array of 4 swizzle components for the named component and return
1310 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
1313 for (i
= 0; i
< 4; i
++) {
1314 if (swizzle
[i
] == component
)
1317 assert(!"search_swizzle() failed");
1323 * Set the sampler view's swizzle terms. This is used to handle RGBA
1324 * swizzling when the incoming image format isn't an exact match for
1325 * the actual texture format. For example, if we have glDrawPixels(
1326 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1327 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1328 * avoid swizzling all the pixels in software in the texstore code.
1331 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1333 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1334 const struct util_format_description
*desc
=
1335 util_format_description(sv
->texture
->format
);
1336 unsigned c0
, c1
, c2
, c3
;
1338 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1339 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1341 assert(desc
->block
.bits
== 32);
1343 /* invert the format's swizzle to setup the sampler's swizzle */
1344 if (format
== GL_RGBA
) {
1345 c0
= PIPE_SWIZZLE_X
;
1346 c1
= PIPE_SWIZZLE_Y
;
1347 c2
= PIPE_SWIZZLE_Z
;
1348 c3
= PIPE_SWIZZLE_W
;
1351 assert(format
== GL_BGRA
);
1352 c0
= PIPE_SWIZZLE_Z
;
1353 c1
= PIPE_SWIZZLE_Y
;
1354 c2
= PIPE_SWIZZLE_X
;
1355 c3
= PIPE_SWIZZLE_W
;
1357 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1358 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1359 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1360 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1363 /* use the default sampler swizzle */
1369 * Compute the effective raster z position. This performs depth-clamping
1373 get_effective_raster_z(struct gl_context
*ctx
)
1375 float z
= ctx
->Current
.RasterPos
[2];
1376 if (st_context(ctx
)->clamp_frag_depth_in_shader
) {
1379 if (ctx
->ViewportArray
[0].Near
< ctx
->ViewportArray
[0].Far
) {
1380 depth_near
= ctx
->ViewportArray
[0].Near
;
1381 depth_far
= ctx
->ViewportArray
[0].Far
;
1383 depth_near
= ctx
->ViewportArray
[0].Far
;
1384 depth_far
= ctx
->ViewportArray
[0].Near
;
1387 if (ctx
->Transform
.DepthClampNear
)
1388 z
= MAX2(z
, depth_near
);
1389 if (ctx
->Transform
.DepthClampFar
)
1390 z
= MIN2(z
, depth_far
);
1397 * Called via ctx->Driver.DrawPixels()
1400 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1401 GLsizei width
, GLsizei height
,
1402 GLenum format
, GLenum type
,
1403 const struct gl_pixelstore_attrib
*unpack
, const void *pixels
)
1406 struct st_context
*st
= st_context(ctx
);
1407 struct pipe_context
*pipe
= st
->pipe
;
1408 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1409 struct pipe_sampler_view
*sv
[2] = { NULL
};
1410 int num_sampler_view
= 1;
1411 struct gl_pixelstore_attrib clippedUnpack
;
1412 struct st_fp_variant
*fpv
= NULL
;
1413 struct pipe_resource
*pt
;
1415 /* Mesa state should be up to date by now */
1416 assert(ctx
->NewState
== 0x0);
1418 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1420 st_flush_bitmap_cache(st
);
1421 st_invalidate_readpix_cache(st
);
1423 st_validate_state(st
, ST_PIPELINE_META
);
1425 /* Limit the size of the glDrawPixels to the max texture size.
1426 * Strictly speaking, that's not correct but since we don't handle
1427 * larger images yet, this is better than crashing.
1429 clippedUnpack
= *unpack
;
1430 unpack
= &clippedUnpack
;
1431 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1433 if (format
== GL_DEPTH_STENCIL
)
1434 write_stencil
= write_depth
= GL_TRUE
;
1435 else if (format
== GL_STENCIL_INDEX
)
1436 write_stencil
= GL_TRUE
;
1437 else if (format
== GL_DEPTH_COMPONENT
)
1438 write_depth
= GL_TRUE
;
1440 if (write_stencil
&&
1441 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1442 /* software fallback */
1443 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1448 /* Put glDrawPixels image into a texture */
1449 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1451 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1455 st_make_passthrough_vertex_shader(st
);
1458 * Get vertex/fragment shaders
1460 if (write_depth
|| write_stencil
) {
1461 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1465 fpv
= (format
!= GL_COLOR_INDEX
) ? get_color_fp_variant(st
) :
1466 get_color_index_fp_variant(st
);
1468 driver_fp
= fpv
->base
.driver_shader
;
1470 if (ctx
->Pixel
.MapColorFlag
&& format
!= GL_COLOR_INDEX
) {
1471 pipe_sampler_view_reference(&sv
[1],
1472 st
->pixel_xfer
.pixelmap_sampler_view
);
1476 /* compiling a new fragment shader variant added new state constants
1477 * into the constant buffer, we need to update them
1479 st_upload_constants(st
, &st
->fp
->Base
);
1482 /* create sampler view for the image */
1483 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1485 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1486 pipe_resource_reference(&pt
, NULL
);
1490 /* Set up the sampler view's swizzle */
1491 setup_sampler_swizzle(sv
[0], format
, type
);
1493 /* Create a second sampler view to read stencil. The stencil is
1494 * written using the shader stencil export functionality.
1496 if (write_stencil
) {
1497 enum pipe_format stencil_format
=
1498 util_format_stencil_only(pt
->format
);
1499 /* we should not be doing pixel map/transfer (see above) */
1500 assert(num_sampler_view
== 1);
1501 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1504 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1505 pipe_resource_reference(&pt
, NULL
);
1506 pipe_sampler_view_reference(&sv
[0], NULL
);
1512 draw_textured_quad(ctx
, x
, y
, get_effective_raster_z(ctx
),
1514 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1519 ctx
->Current
.RasterColor
,
1520 GL_FALSE
, write_depth
, write_stencil
);
1521 pipe_sampler_view_reference(&sv
[0], NULL
);
1522 if (num_sampler_view
> 1)
1523 pipe_sampler_view_reference(&sv
[1], NULL
);
1525 /* free the texture (but may persist in the cache) */
1526 pipe_resource_reference(&pt
, NULL
);
1532 * Software fallback for glCopyPixels(GL_STENCIL).
1535 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1536 GLsizei width
, GLsizei height
,
1537 GLint dstx
, GLint dsty
)
1539 struct st_renderbuffer
*rbDraw
;
1540 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1541 enum pipe_transfer_usage usage
;
1542 struct pipe_transfer
*ptDraw
;
1547 buffer
= malloc(width
* height
* sizeof(ubyte
));
1549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1553 /* Get the dest renderbuffer */
1554 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1555 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1557 /* this will do stencil pixel transfer ops */
1558 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1559 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1560 &ctx
->DefaultPacking
, buffer
);
1563 /* debug code: dump stencil values */
1565 for (row
= 0; row
< height
; row
++) {
1566 printf("%3d: ", row
);
1567 for (col
= 0; col
< width
; col
++) {
1568 printf("%02x ", buffer
[col
+ row
* width
]);
1574 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1575 usage
= PIPE_TRANSFER_READ_WRITE
;
1577 usage
= PIPE_TRANSFER_WRITE
;
1579 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1580 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1583 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1584 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1586 /* map the stencil buffer */
1587 drawMap
= pipe_transfer_map(pipe
,
1589 rbDraw
->surface
->u
.tex
.level
,
1590 rbDraw
->surface
->u
.tex
.first_layer
,
1592 width
, height
, &ptDraw
);
1595 /* XXX PixelZoom not handled yet */
1596 for (i
= 0; i
< height
; i
++) {
1603 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1607 dst
= drawMap
+ y
* ptDraw
->stride
;
1608 src
= buffer
+ i
* width
;
1610 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1615 /* unmap the stencil buffer */
1616 pipe_transfer_unmap(pipe
, ptDraw
);
1621 * Return renderbuffer to use for reading color pixels for glCopyPixels
1623 static struct st_renderbuffer
*
1624 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1626 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1627 struct st_renderbuffer
*strb
=
1628 st_renderbuffer(fb
->_ColorReadBuffer
);
1635 * Try to do a glCopyPixels for simple cases with a blit by calling
1638 * We can do this when we're copying color pixels (depth/stencil
1639 * eventually) with no pixel zoom, no pixel transfer ops, no
1640 * per-fragment ops, and the src/dest regions don't overlap.
1643 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1644 GLsizei width
, GLsizei height
,
1645 GLint dstx
, GLint dsty
, GLenum type
)
1647 struct st_context
*st
= st_context(ctx
);
1648 struct pipe_context
*pipe
= st
->pipe
;
1649 struct pipe_screen
*screen
= pipe
->screen
;
1650 struct gl_pixelstore_attrib pack
, unpack
;
1651 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1653 if (type
== GL_DEPTH_STENCIL_TO_RGBA_NV
|| type
== GL_DEPTH_STENCIL_TO_BGRA_NV
)
1656 if (ctx
->Pixel
.ZoomX
== 1.0 &&
1657 ctx
->Pixel
.ZoomY
== 1.0 &&
1658 (type
!= GL_COLOR
||
1659 (ctx
->_ImageTransferState
== 0x0 &&
1660 !ctx
->Color
.BlendEnabled
&&
1661 !ctx
->Color
.AlphaEnabled
&&
1662 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1664 !ctx
->Fog
.Enabled
&&
1665 !ctx
->Stencil
.Enabled
&&
1666 !ctx
->FragmentProgram
.Enabled
&&
1667 !ctx
->VertexProgram
.Enabled
&&
1668 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1669 !_mesa_ati_fragment_shader_enabled(ctx
) &&
1670 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1)) &&
1671 !ctx
->Query
.CondRenderQuery
&&
1672 !ctx
->Query
.CurrentOcclusionObject
) {
1673 struct st_renderbuffer
*rbRead
, *rbDraw
;
1676 * Clip the read region against the src buffer bounds.
1677 * We'll still allocate a temporary buffer/texture for the original
1678 * src region size but we'll only read the region which is on-screen.
1679 * This may mean that we draw garbage pixels into the dest region, but
1686 pack
= ctx
->DefaultPacking
;
1687 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1688 return GL_TRUE
; /* all done */
1690 /* clip against dest buffer bounds and scissor box */
1691 drawX
= dstx
+ pack
.SkipPixels
;
1692 drawY
= dsty
+ pack
.SkipRows
;
1694 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1695 return GL_TRUE
; /* all done */
1697 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1698 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1703 if (type
== GL_COLOR
) {
1704 rbRead
= st_get_color_read_renderbuffer(ctx
);
1705 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1706 } else if (type
== GL_DEPTH
|| type
== GL_DEPTH_STENCIL
) {
1707 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1708 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1709 } else if (type
== GL_STENCIL
) {
1710 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1711 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1716 /* Flip src/dst position depending on the orientation of buffers. */
1717 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1718 readY
= rbRead
->Base
.Height
- readY
;
1722 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1723 /* We can't flip the destination for pipe->blit, so we only adjust
1724 * its position and flip the source.
1726 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1731 if (rbRead
!= rbDraw
||
1732 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1733 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1734 struct pipe_blit_info blit
;
1736 memset(&blit
, 0, sizeof(blit
));
1737 blit
.src
.resource
= rbRead
->texture
;
1738 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1739 blit
.src
.format
= rbRead
->texture
->format
;
1740 blit
.src
.box
.x
= readX
;
1741 blit
.src
.box
.y
= readY
;
1742 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1743 blit
.src
.box
.width
= readW
;
1744 blit
.src
.box
.height
= readH
;
1745 blit
.src
.box
.depth
= 1;
1746 blit
.dst
.resource
= rbDraw
->texture
;
1747 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1748 blit
.dst
.format
= rbDraw
->texture
->format
;
1749 blit
.dst
.box
.x
= drawX
;
1750 blit
.dst
.box
.y
= drawY
;
1751 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1752 blit
.dst
.box
.width
= drawW
;
1753 blit
.dst
.box
.height
= drawH
;
1754 blit
.dst
.box
.depth
= 1;
1755 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1757 if (type
== GL_COLOR
)
1758 blit
.mask
|= PIPE_MASK_RGBA
;
1759 if (type
== GL_DEPTH
)
1760 blit
.mask
|= PIPE_MASK_Z
;
1761 if (type
== GL_STENCIL
)
1762 blit
.mask
|= PIPE_MASK_S
;
1763 if (type
== GL_DEPTH_STENCIL
)
1764 blit
.mask
|= PIPE_MASK_ZS
;
1766 if (ctx
->DrawBuffer
!= ctx
->WinSysDrawBuffer
)
1767 st_window_rectangles_to_blit(ctx
, &blit
);
1769 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1770 blit
.src
.resource
->target
,
1771 blit
.src
.resource
->nr_samples
,
1772 blit
.src
.resource
->nr_storage_samples
,
1773 PIPE_BIND_SAMPLER_VIEW
) &&
1774 screen
->is_format_supported(screen
, blit
.dst
.format
,
1775 blit
.dst
.resource
->target
,
1776 blit
.dst
.resource
->nr_samples
,
1777 blit
.dst
.resource
->nr_storage_samples
,
1778 PIPE_BIND_RENDER_TARGET
)) {
1779 pipe
->blit(pipe
, &blit
);
1790 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1791 GLsizei width
, GLsizei height
,
1792 GLint dstx
, GLint dsty
, GLenum type
)
1794 struct st_context
*st
= st_context(ctx
);
1795 struct pipe_context
*pipe
= st
->pipe
;
1796 struct pipe_screen
*screen
= pipe
->screen
;
1797 struct st_renderbuffer
*rbRead
;
1799 struct pipe_resource
*pt
;
1800 struct pipe_sampler_view
*sv
[2] = { NULL
};
1801 struct st_fp_variant
*fpv
= NULL
;
1802 int num_sampler_view
= 1;
1803 enum pipe_format srcFormat
;
1805 GLboolean invertTex
= GL_FALSE
;
1806 GLint readX
, readY
, readW
, readH
;
1807 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1808 GLboolean write_stencil
= GL_FALSE
;
1809 GLboolean write_depth
= GL_FALSE
;
1811 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
1813 st_flush_bitmap_cache(st
);
1814 st_invalidate_readpix_cache(st
);
1816 st_validate_state(st
, ST_PIPELINE_META
);
1818 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1821 /* fallback if the driver can't do stencil exports */
1822 if (type
== GL_DEPTH_STENCIL
&&
1823 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1824 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1825 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1829 /* fallback if the driver can't do stencil exports */
1830 if (type
== GL_STENCIL
&&
1831 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1832 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1837 * The subsequent code implements glCopyPixels by copying the source
1838 * pixels into a temporary texture that's then applied to a textured quad.
1839 * When we draw the textured quad, all the usual per-fragment operations
1843 st_make_passthrough_vertex_shader(st
);
1846 * Get vertex/fragment shaders
1848 if (type
== GL_COLOR
) {
1849 fpv
= get_color_fp_variant(st
);
1851 rbRead
= st_get_color_read_renderbuffer(ctx
);
1853 driver_fp
= fpv
->base
.driver_shader
;
1855 if (ctx
->Pixel
.MapColorFlag
) {
1856 pipe_sampler_view_reference(&sv
[1],
1857 st
->pixel_xfer
.pixelmap_sampler_view
);
1861 /* compiling a new fragment shader variant added new state constants
1862 * into the constant buffer, we need to update them
1864 st_upload_constants(st
, &st
->fp
->Base
);
1865 } else if (type
== GL_DEPTH
) {
1866 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1867 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1868 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1869 } else if (type
== GL_STENCIL
) {
1870 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1871 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1872 driver_fp
= get_drawpix_z_stencil_program(st
, GL_FALSE
, GL_TRUE
);
1873 } else if (type
== GL_DEPTH_STENCIL
) {
1874 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1875 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1876 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_TRUE
);
1878 assert(type
== GL_DEPTH_STENCIL_TO_RGBA_NV
|| type
== GL_DEPTH_STENCIL_TO_BGRA_NV
);
1879 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1880 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1881 if (type
== GL_DEPTH_STENCIL_TO_RGBA_NV
)
1882 driver_fp
= get_drawpix_zs_to_color_program(st
, GL_TRUE
);
1884 driver_fp
= get_drawpix_zs_to_color_program(st
, GL_FALSE
);
1886 assert(0 && "operation not supported by CopyPixels implemetation");
1892 /* Choose the format for the temporary texture. */
1893 srcFormat
= rbRead
->texture
->format
;
1894 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1895 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1897 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1899 /* srcFormat is non-renderable. Find a compatible renderable format. */
1900 if (type
== GL_DEPTH
) {
1901 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1902 GL_NONE
, st
->internal_target
, 0, 0,
1903 srcBind
, false, false);
1905 else if (type
== GL_STENCIL
) {
1906 /* can't use texturing, fallback to copy */
1907 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1911 assert(type
== GL_COLOR
);
1913 if (util_format_is_float(srcFormat
)) {
1914 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1915 GL_NONE
, st
->internal_target
, 0, 0,
1916 srcBind
, false, false);
1918 else if (util_format_is_pure_sint(srcFormat
)) {
1919 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1920 GL_NONE
, st
->internal_target
, 0, 0,
1921 srcBind
, false, false);
1923 else if (util_format_is_pure_uint(srcFormat
)) {
1924 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1925 GL_NONE
, st
->internal_target
, 0, 0,
1926 srcBind
, false, false);
1928 else if (util_format_is_snorm(srcFormat
)) {
1929 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1930 GL_NONE
, st
->internal_target
, 0, 0,
1931 srcBind
, false, false);
1934 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1935 GL_NONE
, st
->internal_target
, 0, 0,
1936 srcBind
, false, false);
1940 if (srcFormat
== PIPE_FORMAT_NONE
) {
1941 assert(0 && "cannot choose a format for src of CopyPixels");
1946 /* Invert src region if needed */
1947 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1948 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1949 invertTex
= !invertTex
;
1952 /* Clip the read region against the src buffer bounds.
1953 * We'll still allocate a temporary buffer/texture for the original
1954 * src region size but we'll only read the region which is on-screen.
1955 * This may mean that we draw garbage pixels into the dest region, but
1962 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1963 /* The source region is completely out of bounds. Do nothing.
1964 * The GL spec says "Results of copies from outside the window,
1965 * or from regions of the window that are not exposed, are
1966 * hardware dependent and undefined."
1971 readW
= MAX2(0, readW
);
1972 readH
= MAX2(0, readH
);
1974 /* Allocate the temporary texture. */
1975 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1979 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1981 pipe_resource_reference(&pt
, NULL
);
1985 /* Create a second sampler view to read stencil */
1986 if (type
== GL_STENCIL
|| type
== GL_DEPTH_STENCIL
||
1987 type
== GL_DEPTH_STENCIL_TO_RGBA_NV
|| type
== GL_DEPTH_STENCIL_TO_BGRA_NV
) {
1988 write_stencil
= GL_TRUE
;
1989 if (type
== GL_DEPTH_STENCIL
)
1990 write_depth
= GL_TRUE
;
1991 if (type
== GL_DEPTH_STENCIL_TO_RGBA_NV
|| type
== GL_DEPTH_STENCIL_TO_BGRA_NV
) {
1992 write_depth
= FALSE
;
1993 write_stencil
= FALSE
;
1996 enum pipe_format stencil_format
=
1997 util_format_stencil_only(pt
->format
);
1998 /* we should not be doing pixel map/transfer (see above) */
1999 assert(num_sampler_view
== 1);
2000 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
2003 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
2004 pipe_resource_reference(&pt
, NULL
);
2005 pipe_sampler_view_reference(&sv
[0], NULL
);
2010 /* Copy the src region to the temporary texture. */
2012 struct pipe_blit_info blit
;
2014 memset(&blit
, 0, sizeof(blit
));
2015 blit
.src
.resource
= rbRead
->texture
;
2016 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
2017 blit
.src
.format
= rbRead
->texture
->format
;
2018 blit
.src
.box
.x
= readX
;
2019 blit
.src
.box
.y
= readY
;
2020 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
2021 blit
.src
.box
.width
= readW
;
2022 blit
.src
.box
.height
= readH
;
2023 blit
.src
.box
.depth
= 1;
2024 blit
.dst
.resource
= pt
;
2026 blit
.dst
.format
= pt
->format
;
2027 blit
.dst
.box
.x
= pack
.SkipPixels
;
2028 blit
.dst
.box
.y
= pack
.SkipRows
;
2030 blit
.dst
.box
.width
= readW
;
2031 blit
.dst
.box
.height
= readH
;
2032 blit
.dst
.box
.depth
= 1;
2033 if (type
== GL_DEPTH
)
2034 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
2035 else if (type
== GL_STENCIL
)
2036 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_Z
;
2038 blit
.mask
= util_format_get_mask(pt
->format
);
2039 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2041 pipe
->blit(pipe
, &blit
);
2044 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
2045 * textured quad with that texture.
2048 draw_textured_quad(ctx
, dstx
, dsty
, get_effective_raster_z(ctx
),
2049 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
2054 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
2055 invertTex
, write_depth
, write_stencil
);
2057 pipe_resource_reference(&pt
, NULL
);
2058 pipe_sampler_view_reference(&sv
[0], NULL
);
2063 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
2065 functions
->DrawPixels
= st_DrawPixels
;
2066 functions
->CopyPixels
= st_CopyPixels
;
2071 st_destroy_drawpix(struct st_context
*st
)
2075 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
2076 if (st
->drawpix
.zs_shaders
[i
])
2077 st
->pipe
->delete_fs_state(st
->pipe
, st
->drawpix
.zs_shaders
[i
]);
2080 if (st
->passthrough_vs
)
2081 st
->pipe
->delete_vs_state(st
->pipe
, st
->passthrough_vs
);
2083 /* Free cache data */
2084 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix_cache
.entries
); i
++) {
2085 struct drawpix_cache_entry
*entry
= &st
->drawpix_cache
.entries
[i
];
2087 pipe_resource_reference(&entry
->texture
, NULL
);