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/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/blit.h"
37 #include "main/format_pack.h"
38 #include "main/framebuffer.h"
39 #include "main/macros.h"
40 #include "main/mtypes.h"
41 #include "main/pack.h"
43 #include "main/readpix.h"
44 #include "main/texformat.h"
45 #include "main/teximage.h"
46 #include "main/texstore.h"
47 #include "main/glformats.h"
48 #include "program/program.h"
49 #include "program/prog_print.h"
50 #include "program/prog_instruction.h"
53 #include "st_atom_constbuf.h"
54 #include "st_cb_bitmap.h"
55 #include "st_cb_drawpixels.h"
56 #include "st_cb_readpixels.h"
57 #include "st_cb_fbo.h"
58 #include "st_context.h"
61 #include "st_format.h"
62 #include "st_program.h"
63 #include "st_texture.h"
65 #include "pipe/p_context.h"
66 #include "pipe/p_defines.h"
67 #include "tgsi/tgsi_ureg.h"
68 #include "util/u_format.h"
69 #include "util/u_inlines.h"
70 #include "util/u_math.h"
71 #include "util/u_tile.h"
72 #include "cso_cache/cso_context.h"
76 * We have a simple glDrawPixels cache to try to optimize the case where the
77 * same image is drawn over and over again. It basically works as follows:
79 * 1. After we construct a texture map with the image and draw it, we do
80 * not discard the texture. We keep it around, plus we note the
81 * glDrawPixels width, height, format, etc. parameters and keep a copy
82 * of the image in a malloc'd buffer.
84 * 2. On the next glDrawPixels we check if the parameters match the previous
85 * call. If those match, we check if the image matches the previous image
86 * via a memcmp() call. If everything matches, we re-use the previous
87 * texture, thereby avoiding the cost creating a new texture and copying
90 * The effectiveness of this cache depends upon:
91 * 1. If the memcmp() finds a difference, it happens relatively quickly.
92 Hopefully, not just the last pixels differ!
93 * 2. If the memcmp() finds no difference, doing that check is faster than
94 * creating and loading a texture.
97 * 1. We don't support any pixel unpacking parameters.
98 * 2. We don't try to cache images in Pixel Buffer Objects.
99 * 3. Instead of saving the whole image, perhaps some sort of reliable
100 * checksum function could be used instead.
102 #define USE_DRAWPIXELS_CACHE 1
107 * Create fragment program that does a TEX() instruction to get a Z and/or
108 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
109 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
110 * Pass fragment color through as-is.
112 * \return CSO of the fragment shader.
115 get_drawpix_z_stencil_program(struct st_context
*st
,
116 GLboolean write_depth
,
117 GLboolean write_stencil
)
119 struct ureg_program
*ureg
;
120 struct ureg_src depth_sampler
, stencil_sampler
;
121 struct ureg_src texcoord
, color
;
122 struct ureg_dst out_color
, out_depth
, out_stencil
;
123 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
126 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
128 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
129 /* already have the proper shader */
130 return st
->drawpix
.zs_shaders
[shaderIndex
];
133 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
137 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
140 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
141 TGSI_INTERPOLATE_COLOR
);
142 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
144 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
145 ureg_DECL_sampler_view(ureg
, 0, TGSI_TEXTURE_2D
,
146 TGSI_RETURN_TYPE_FLOAT
,
147 TGSI_RETURN_TYPE_FLOAT
,
148 TGSI_RETURN_TYPE_FLOAT
,
149 TGSI_RETURN_TYPE_FLOAT
);
150 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
154 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
155 ureg_DECL_sampler_view(ureg
, 1, TGSI_TEXTURE_2D
,
156 TGSI_RETURN_TYPE_UINT
,
157 TGSI_RETURN_TYPE_UINT
,
158 TGSI_RETURN_TYPE_UINT
,
159 TGSI_RETURN_TYPE_UINT
);
160 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
163 texcoord
= ureg_DECL_fs_input(ureg
,
164 st
->needs_texcoord_semantic
?
165 TGSI_SEMANTIC_TEXCOORD
:
166 TGSI_SEMANTIC_GENERIC
,
167 0, TGSI_INTERPOLATE_LINEAR
);
170 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
171 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
172 ureg_MOV(ureg
, out_color
, color
);
176 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
177 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
180 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
182 /* save the new shader */
183 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
189 * Create a simple vertex shader that just passes through the
190 * vertex position and texcoord (and optionally, color).
193 make_passthrough_vertex_shader(struct st_context
*st
,
196 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
197 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
199 if (!st
->drawpix
.vert_shaders
[passColor
]) {
200 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
205 /* MOV result.pos, vertex.pos; */
207 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
208 ureg_DECL_vs_input( ureg
, 0 ));
211 /* MOV result.color0, vertex.attr[1]; */
213 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
214 ureg_DECL_vs_input( ureg
, 1 ));
217 /* MOV result.texcoord0, vertex.attr[2]; */
219 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
220 ureg_DECL_vs_input( ureg
, 2 ));
224 st
->drawpix
.vert_shaders
[passColor
] =
225 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
228 return st
->drawpix
.vert_shaders
[passColor
];
233 * Return a texture internalFormat for drawing/copying an image
234 * of the given format and type.
237 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
240 case GL_DEPTH_COMPONENT
:
242 case GL_UNSIGNED_SHORT
:
243 return GL_DEPTH_COMPONENT16
;
245 case GL_UNSIGNED_INT
:
246 return GL_DEPTH_COMPONENT32
;
249 if (ctx
->Extensions
.ARB_depth_buffer_float
)
250 return GL_DEPTH_COMPONENT32F
;
252 return GL_DEPTH_COMPONENT
;
255 return GL_DEPTH_COMPONENT
;
258 case GL_DEPTH_STENCIL
:
260 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
261 return GL_DEPTH32F_STENCIL8
;
263 case GL_UNSIGNED_INT_24_8
:
265 return GL_DEPTH24_STENCIL8
;
268 case GL_STENCIL_INDEX
:
269 return GL_STENCIL_INDEX
;
272 if (_mesa_is_enum_format_integer(format
)) {
276 case GL_UNSIGNED_BYTE
:
280 case GL_UNSIGNED_SHORT
:
284 case GL_UNSIGNED_INT
:
287 assert(0 && "Unexpected type in internal_format()");
288 return GL_RGBA_INTEGER
;
293 case GL_UNSIGNED_BYTE
:
294 case GL_UNSIGNED_INT_8_8_8_8
:
295 case GL_UNSIGNED_INT_8_8_8_8_REV
:
299 case GL_UNSIGNED_BYTE_3_3_2
:
300 case GL_UNSIGNED_BYTE_2_3_3_REV
:
303 case GL_UNSIGNED_SHORT_4_4_4_4
:
304 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
307 case GL_UNSIGNED_SHORT_5_6_5
:
308 case GL_UNSIGNED_SHORT_5_6_5_REV
:
311 case GL_UNSIGNED_SHORT_5_5_5_1
:
312 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
315 case GL_UNSIGNED_INT_10_10_10_2
:
316 case GL_UNSIGNED_INT_2_10_10_10_REV
:
319 case GL_UNSIGNED_SHORT
:
320 case GL_UNSIGNED_INT
:
325 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
330 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
332 case GL_HALF_FLOAT_ARB
:
334 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
335 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
340 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
341 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
343 case GL_UNSIGNED_INT_5_9_9_9_REV
:
344 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
347 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
348 assert(ctx
->Extensions
.EXT_packed_float
);
349 return GL_R11F_G11F_B10F
;
357 * Create a temporary texture to hold an image of the given size.
358 * If width, height are not POT and the driver only handles POT textures,
359 * allocate the next larger size of texture that is POT.
361 static struct pipe_resource
*
362 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
363 enum pipe_format texFormat
, unsigned bind
)
365 struct pipe_resource
*pt
;
367 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
368 width
, height
, 1, 1, 0, bind
);
375 * Make texture containing an image for glDrawPixels image.
376 * If 'pixels' is NULL, leave the texture image data undefined.
378 static struct pipe_resource
*
379 make_texture(struct st_context
*st
,
380 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
381 const struct gl_pixelstore_attrib
*unpack
,
382 const GLvoid
*pixels
)
384 struct gl_context
*ctx
= st
->ctx
;
385 struct pipe_context
*pipe
= st
->pipe
;
387 struct pipe_resource
*pt
;
388 enum pipe_format pipeFormat
;
389 GLenum baseInternalFormat
;
391 #if USE_DRAWPIXELS_CACHE
392 const GLint bpp
= _mesa_bytes_per_pixel(format
, type
);
394 /* Check if the glDrawPixels() parameters and state matches the cache */
395 if (width
== st
->drawpix_cache
.width
&&
396 height
== st
->drawpix_cache
.height
&&
397 format
== st
->drawpix_cache
.format
&&
398 type
== st
->drawpix_cache
.type
&&
399 pixels
== st
->drawpix_cache
.user_pointer
&&
400 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
401 (unpack
->RowLength
== 0 || unpack
->RowLength
== width
) &&
402 unpack
->SkipPixels
== 0 &&
403 unpack
->SkipRows
== 0 &&
404 unpack
->SwapBytes
== GL_FALSE
&&
405 st
->drawpix_cache
.image
) {
406 /* check if the pixel data is the same */
407 if (memcmp(pixels
, st
->drawpix_cache
.image
, width
* height
* bpp
) == 0) {
408 /* OK, re-use the cached texture */
409 return st
->drawpix_cache
.texture
;
413 /* discard the cached image and texture (if there is one) */
414 st
->drawpix_cache
.width
= 0;
415 st
->drawpix_cache
.height
= 0;
416 st
->drawpix_cache
.user_pointer
= NULL
;
417 if (st
->drawpix_cache
.image
) {
418 free(st
->drawpix_cache
.image
);
419 st
->drawpix_cache
.image
= NULL
;
421 pipe_resource_reference(&st
->drawpix_cache
.texture
, NULL
);
424 /* Choose a pixel format for the temp texture which will hold the
427 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
428 format
, type
, unpack
->SwapBytes
);
430 if (pipeFormat
== PIPE_FORMAT_NONE
) {
431 /* Use the generic approach. */
432 GLenum intFormat
= internal_format(ctx
, format
, type
);
434 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
435 st
->internal_target
, 0,
436 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
437 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
440 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
441 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
443 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
447 /* alloc temporary texture */
448 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
450 _mesa_unmap_pbo_source(ctx
, unpack
);
455 struct pipe_transfer
*transfer
;
458 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
460 /* we'll do pixel transfer in a fragment shader */
461 ctx
->_ImageTransferState
= 0x0;
463 /* map texture transfer */
464 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
465 PIPE_TRANSFER_WRITE
, 0, 0,
466 width
, height
, &transfer
);
469 /* Put image into texture transfer.
470 * Note that the image is actually going to be upside down in
471 * the texture. We deal with that with texcoords.
473 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
474 && type
== GL_UNSIGNED_BYTE
) {
475 /* Use a memcpy-based texstore to avoid software pixel swizzling.
476 * We'll do the necessary swizzling with the pipe_sampler_view to
477 * give much better performance.
478 * XXX in the future, expand this to accomodate more format and
481 _mesa_memcpy_texture(ctx
, 2,
482 mformat
, /* mesa_format */
483 transfer
->stride
, /* dstRowStride, bytes */
484 &dest
, /* destSlices */
485 width
, height
, 1, /* size */
486 format
, type
, /* src format/type */
487 pixels
, /* data source */
492 success
= _mesa_texstore(ctx
, 2, /* dims */
493 baseInternalFormat
, /* baseInternalFormat */
494 mformat
, /* mesa_format */
495 transfer
->stride
, /* dstRowStride, bytes */
496 &dest
, /* destSlices */
497 width
, height
, 1, /* size */
498 format
, type
, /* src format/type */
499 pixels
, /* data source */
504 pipe_transfer_unmap(pipe
, transfer
);
509 ctx
->_ImageTransferState
= imageTransferStateSave
;
512 _mesa_unmap_pbo_source(ctx
, unpack
);
514 #if USE_DRAWPIXELS_CACHE
515 /* Save the glDrawPixels parameter and image in the cache */
516 if ((unpack
->RowLength
== 0 || unpack
->RowLength
== width
) &&
517 unpack
->SkipPixels
== 0 &&
518 unpack
->SkipRows
== 0) {
519 st
->drawpix_cache
.width
= width
;
520 st
->drawpix_cache
.height
= height
;
521 st
->drawpix_cache
.format
= format
;
522 st
->drawpix_cache
.type
= type
;
523 st
->drawpix_cache
.user_pointer
= pixels
;
524 assert(!st
->drawpix_cache
.image
);
525 st
->drawpix_cache
.image
= malloc(width
* height
* bpp
);
526 if (st
->drawpix_cache
.image
) {
527 memcpy(st
->drawpix_cache
.image
, pixels
, width
* height
* bpp
);
529 st
->drawpix_cache
.texture
= pt
;
538 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
539 GLsizei width
, GLsizei height
,
540 GLfloat zoomX
, GLfloat zoomY
,
541 struct pipe_sampler_view
**sv
,
542 int num_sampler_view
,
545 struct st_fp_variant
*fpv
,
546 const GLfloat
*color
,
548 GLboolean write_depth
, GLboolean write_stencil
)
550 struct st_context
*st
= st_context(ctx
);
551 struct pipe_context
*pipe
= st
->pipe
;
552 struct cso_context
*cso
= st
->cso_context
;
553 const unsigned fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
554 const unsigned fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
555 GLfloat x0
, y0
, x1
, y1
;
557 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
558 unsigned cso_state_mask
;
560 assert(sv
[0]->texture
->target
== st
->internal_target
);
563 /* XXX if DrawPixels image is larger than max texture size, break
566 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
567 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
568 assert(width
<= maxSize
);
569 assert(height
<= maxSize
);
571 cso_state_mask
= (CSO_BIT_RASTERIZER
|
573 CSO_BIT_FRAGMENT_SAMPLERS
|
574 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
575 CSO_BIT_STREAM_OUTPUTS
|
576 CSO_BIT_VERTEX_ELEMENTS
|
577 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
578 CSO_BITS_ALL_SHADERS
);
580 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
583 cso_save_state(cso
, cso_state_mask
);
585 /* rasterizer state: just scissor */
587 struct pipe_rasterizer_state rasterizer
;
588 memset(&rasterizer
, 0, sizeof(rasterizer
));
589 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
590 ctx
->Color
._ClampFragmentColor
;
591 rasterizer
.half_pixel_center
= 1;
592 rasterizer
.bottom_edge_rule
= 1;
593 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
594 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
595 cso_set_rasterizer(cso
, &rasterizer
);
599 /* Stencil writing bypasses the normal fragment pipeline to
600 * disable color writing and set stencil test to always pass.
602 struct pipe_depth_stencil_alpha_state dsa
;
603 struct pipe_blend_state blend
;
606 memset(&dsa
, 0, sizeof(dsa
));
607 dsa
.stencil
[0].enabled
= 1;
608 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
609 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
610 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
612 /* writing depth+stencil: depth test always passes */
613 dsa
.depth
.enabled
= 1;
614 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
615 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
617 cso_set_depth_stencil_alpha(cso
, &dsa
);
619 /* blend (colormask) */
620 memset(&blend
, 0, sizeof(blend
));
621 cso_set_blend(cso
, &blend
);
624 /* fragment shader state: TEX lookup program */
625 cso_set_fragment_shader_handle(cso
, driver_fp
);
627 /* vertex shader state: position + texcoord pass-through */
628 cso_set_vertex_shader_handle(cso
, driver_vp
);
630 /* disable other shaders */
631 cso_set_tessctrl_shader_handle(cso
, NULL
);
632 cso_set_tesseval_shader_handle(cso
, NULL
);
633 cso_set_geometry_shader_handle(cso
, NULL
);
635 /* user samplers, plus the drawpix samplers */
637 struct pipe_sampler_state sampler
;
639 memset(&sampler
, 0, sizeof(sampler
));
640 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
641 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
642 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
643 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
644 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
645 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
646 sampler
.normalized_coords
= normalized
;
649 /* drawing a color image */
650 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
651 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
652 fpv
->pixelmap_sampler
+ 1,
653 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
656 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
657 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
659 samplers
[fpv
->drawpix_sampler
] = &sampler
;
661 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
663 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
665 /* drawing a depth/stencil image */
666 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
668 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
672 /* user textures, plus the drawpix textures */
674 /* drawing a color image */
675 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
676 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
677 fpv
->pixelmap_sampler
+ 1,
678 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
680 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
681 sizeof(sampler_views
));
683 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
685 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
686 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
688 /* drawing a depth/stencil image */
689 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
692 /* viewport state: viewport matching window dims */
693 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
695 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
696 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
698 /* Compute Gallium window coords (y=0=top) with pixel zoom.
699 * Recall that these coords are transformed by the current
700 * vertex shader and viewport transformation.
702 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
703 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
704 invertTex
= !invertTex
;
708 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
710 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
712 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
716 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
717 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
718 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
719 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
720 const float maxXcoord
= normalized
?
721 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
722 const float maxYcoord
= normalized
723 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
724 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
725 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
726 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
728 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
729 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
730 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
735 cso_restore_state(cso
);
740 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
741 * can't use a fragment shader to write stencil values.
744 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
745 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
746 const struct gl_pixelstore_attrib
*unpack
,
747 const GLvoid
*pixels
)
749 struct st_context
*st
= st_context(ctx
);
750 struct pipe_context
*pipe
= st
->pipe
;
751 struct st_renderbuffer
*strb
;
752 enum pipe_transfer_usage usage
;
753 struct pipe_transfer
*pt
;
754 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
756 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
761 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
763 /* totally clipped */
768 strb
= st_renderbuffer(ctx
->DrawBuffer
->
769 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
771 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
772 y
= ctx
->DrawBuffer
->Height
- y
- height
;
775 if (format
== GL_STENCIL_INDEX
&&
776 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
777 /* writing stencil to a combined depth+stencil buffer */
778 usage
= PIPE_TRANSFER_READ_WRITE
;
781 usage
= PIPE_TRANSFER_WRITE
;
784 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
785 strb
->surface
->u
.tex
.level
,
786 strb
->surface
->u
.tex
.first_layer
,
790 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
793 sValues
= malloc(width
* sizeof(GLubyte
));
794 zValues
= malloc(width
* sizeof(GLuint
));
796 if (sValues
&& zValues
) {
798 for (row
= 0; row
< height
; row
++) {
799 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
800 GLenum destType
= GL_UNSIGNED_BYTE
;
801 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
805 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
806 type
, source
, &clippedUnpack
,
807 ctx
->_ImageTransferState
);
809 if (format
== GL_DEPTH_STENCIL
) {
811 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
812 GL_FLOAT
: GL_UNSIGNED_INT
;
814 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
815 (1 << 24) - 1, type
, source
,
820 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
821 "zoom not complete");
827 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
828 spanY
= height
- row
- 1;
834 /* now pack the stencil (and Z) values in the dest format */
835 switch (pt
->resource
->format
) {
836 case PIPE_FORMAT_S8_UINT
:
838 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
839 assert(usage
== PIPE_TRANSFER_WRITE
);
840 memcpy(dest
, sValues
, width
);
843 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
844 if (format
== GL_DEPTH_STENCIL
) {
845 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
847 assert(usage
== PIPE_TRANSFER_WRITE
);
848 for (k
= 0; k
< width
; k
++) {
849 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
853 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
855 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
856 for (k
= 0; k
< width
; k
++) {
857 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
861 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
862 if (format
== GL_DEPTH_STENCIL
) {
863 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
865 assert(usage
== PIPE_TRANSFER_WRITE
);
866 for (k
= 0; k
< width
; k
++) {
867 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
871 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
873 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
874 for (k
= 0; k
< width
; k
++) {
875 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
879 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
880 if (format
== GL_DEPTH_STENCIL
) {
881 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
882 GLfloat
*destf
= (GLfloat
*)dest
;
884 assert(usage
== PIPE_TRANSFER_WRITE
);
885 for (k
= 0; k
< width
; k
++) {
886 destf
[k
*2] = zValuesFloat
[k
];
887 dest
[k
*2+1] = sValues
[k
] & 0xff;
891 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
893 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
894 for (k
= 0; k
< width
; k
++) {
895 dest
[k
*2+1] = sValues
[k
] & 0xff;
906 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
912 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
914 /* unmap the stencil buffer */
915 pipe_transfer_unmap(pipe
, pt
);
920 * Get fragment program variant for a glDrawPixels or glCopyPixels
921 * command for RGBA data.
923 static struct st_fp_variant
*
924 get_color_fp_variant(struct st_context
*st
)
926 struct gl_context
*ctx
= st
->ctx
;
927 struct st_fp_variant_key key
;
928 struct st_fp_variant
*fpv
;
930 memset(&key
, 0, sizeof(key
));
932 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
934 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
935 ctx
->Pixel
.RedScale
!= 1.0 ||
936 ctx
->Pixel
.GreenBias
!= 0.0 ||
937 ctx
->Pixel
.GreenScale
!= 1.0 ||
938 ctx
->Pixel
.BlueBias
!= 0.0 ||
939 ctx
->Pixel
.BlueScale
!= 1.0 ||
940 ctx
->Pixel
.AlphaBias
!= 0.0 ||
941 ctx
->Pixel
.AlphaScale
!= 1.0);
942 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
943 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
944 ctx
->Color
._ClampFragmentColor
;
946 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
953 * Clamp glDrawPixels width and height to the maximum texture size.
956 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
957 struct gl_pixelstore_attrib
*unpack
)
960 1 << (pipe
->screen
->get_param(pipe
->screen
,
961 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
963 if (*width
> maxSize
) {
964 if (unpack
->RowLength
== 0)
965 unpack
->RowLength
= *width
;
968 if (*height
> maxSize
) {
975 * Search the array of 4 swizzle components for the named component and return
979 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
982 for (i
= 0; i
< 4; i
++) {
983 if (swizzle
[i
] == component
)
986 assert(!"search_swizzle() failed");
992 * Set the sampler view's swizzle terms. This is used to handle RGBA
993 * swizzling when the incoming image format isn't an exact match for
994 * the actual texture format. For example, if we have glDrawPixels(
995 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
996 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
997 * avoid swizzling all the pixels in software in the texstore code.
1000 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1002 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1003 const struct util_format_description
*desc
=
1004 util_format_description(sv
->texture
->format
);
1005 unsigned c0
, c1
, c2
, c3
;
1007 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1008 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1010 assert(desc
->block
.bits
== 32);
1012 /* invert the format's swizzle to setup the sampler's swizzle */
1013 if (format
== GL_RGBA
) {
1014 c0
= UTIL_FORMAT_SWIZZLE_X
;
1015 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1016 c2
= UTIL_FORMAT_SWIZZLE_Z
;
1017 c3
= UTIL_FORMAT_SWIZZLE_W
;
1020 assert(format
== GL_BGRA
);
1021 c0
= UTIL_FORMAT_SWIZZLE_Z
;
1022 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1023 c2
= UTIL_FORMAT_SWIZZLE_X
;
1024 c3
= UTIL_FORMAT_SWIZZLE_W
;
1026 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1027 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1028 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1029 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1032 /* use the default sampler swizzle */
1038 * Called via ctx->Driver.DrawPixels()
1041 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1042 GLsizei width
, GLsizei height
,
1043 GLenum format
, GLenum type
,
1044 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1046 void *driver_vp
, *driver_fp
;
1047 struct st_context
*st
= st_context(ctx
);
1048 struct pipe_context
*pipe
= st
->pipe
;
1049 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1050 struct pipe_sampler_view
*sv
[2] = { NULL
};
1051 int num_sampler_view
= 1;
1052 struct gl_pixelstore_attrib clippedUnpack
;
1053 struct st_fp_variant
*fpv
= NULL
;
1054 struct pipe_resource
*pt
;
1056 /* Mesa state should be up to date by now */
1057 assert(ctx
->NewState
== 0x0);
1059 st_flush_bitmap_cache(st
);
1061 st_validate_state(st
, ST_PIPELINE_RENDER
);
1063 /* Limit the size of the glDrawPixels to the max texture size.
1064 * Strictly speaking, that's not correct but since we don't handle
1065 * larger images yet, this is better than crashing.
1067 clippedUnpack
= *unpack
;
1068 unpack
= &clippedUnpack
;
1069 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1071 if (format
== GL_DEPTH_STENCIL
)
1072 write_stencil
= write_depth
= GL_TRUE
;
1073 else if (format
== GL_STENCIL_INDEX
)
1074 write_stencil
= GL_TRUE
;
1075 else if (format
== GL_DEPTH_COMPONENT
)
1076 write_depth
= GL_TRUE
;
1078 if (write_stencil
&&
1079 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1080 /* software fallback */
1081 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1087 * Get vertex/fragment shaders
1089 if (write_depth
|| write_stencil
) {
1090 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1092 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1095 fpv
= get_color_fp_variant(st
);
1097 driver_fp
= fpv
->driver_shader
;
1098 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1100 if (ctx
->Pixel
.MapColorFlag
) {
1101 pipe_sampler_view_reference(&sv
[1],
1102 st
->pixel_xfer
.pixelmap_sampler_view
);
1106 /* compiling a new fragment shader variant added new state constants
1107 * into the constant buffer, we need to update them
1109 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1110 PIPE_SHADER_FRAGMENT
);
1113 /* Put glDrawPixels image into a texture */
1114 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1116 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1120 /* create sampler view for the image */
1121 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1123 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1124 pipe_resource_reference(&pt
, NULL
);
1128 /* Set up the sampler view's swizzle */
1129 setup_sampler_swizzle(sv
[0], format
, type
);
1131 /* Create a second sampler view to read stencil. The stencil is
1132 * written using the shader stencil export functionality.
1134 if (write_stencil
) {
1135 enum pipe_format stencil_format
=
1136 util_format_stencil_only(pt
->format
);
1137 /* we should not be doing pixel map/transfer (see above) */
1138 assert(num_sampler_view
== 1);
1139 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1142 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1143 pipe_resource_reference(&pt
, NULL
);
1144 pipe_sampler_view_reference(&sv
[0], NULL
);
1150 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1152 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1157 ctx
->Current
.RasterColor
,
1158 GL_FALSE
, write_depth
, write_stencil
);
1159 pipe_sampler_view_reference(&sv
[0], NULL
);
1160 if (num_sampler_view
> 1)
1161 pipe_sampler_view_reference(&sv
[1], NULL
);
1163 #if !USE_DRAWPIXELS_CACHE
1164 pipe_resource_reference(&pt
, NULL
);
1171 * Software fallback for glCopyPixels(GL_STENCIL).
1174 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1175 GLsizei width
, GLsizei height
,
1176 GLint dstx
, GLint dsty
)
1178 struct st_renderbuffer
*rbDraw
;
1179 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1180 enum pipe_transfer_usage usage
;
1181 struct pipe_transfer
*ptDraw
;
1186 buffer
= malloc(width
* height
* sizeof(ubyte
));
1188 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1192 /* Get the dest renderbuffer */
1193 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1194 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1196 /* this will do stencil pixel transfer ops */
1197 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1198 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1199 &ctx
->DefaultPacking
, buffer
);
1202 /* debug code: dump stencil values */
1204 for (row
= 0; row
< height
; row
++) {
1205 printf("%3d: ", row
);
1206 for (col
= 0; col
< width
; col
++) {
1207 printf("%02x ", buffer
[col
+ row
* width
]);
1213 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1214 usage
= PIPE_TRANSFER_READ_WRITE
;
1216 usage
= PIPE_TRANSFER_WRITE
;
1218 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1219 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1222 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1223 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1225 /* map the stencil buffer */
1226 drawMap
= pipe_transfer_map(pipe
,
1228 rbDraw
->surface
->u
.tex
.level
,
1229 rbDraw
->surface
->u
.tex
.first_layer
,
1231 width
, height
, &ptDraw
);
1234 /* XXX PixelZoom not handled yet */
1235 for (i
= 0; i
< height
; i
++) {
1242 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1246 dst
= drawMap
+ y
* ptDraw
->stride
;
1247 src
= buffer
+ i
* width
;
1249 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1254 /* unmap the stencil buffer */
1255 pipe_transfer_unmap(pipe
, ptDraw
);
1260 * Return renderbuffer to use for reading color pixels for glCopyPixels
1262 static struct st_renderbuffer
*
1263 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1265 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1266 struct st_renderbuffer
*strb
=
1267 st_renderbuffer(fb
->_ColorReadBuffer
);
1274 * Try to do a glCopyPixels for simple cases with a blit by calling
1277 * We can do this when we're copying color pixels (depth/stencil
1278 * eventually) with no pixel zoom, no pixel transfer ops, no
1279 * per-fragment ops, and the src/dest regions don't overlap.
1282 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1283 GLsizei width
, GLsizei height
,
1284 GLint dstx
, GLint dsty
, GLenum type
)
1286 struct st_context
*st
= st_context(ctx
);
1287 struct pipe_context
*pipe
= st
->pipe
;
1288 struct pipe_screen
*screen
= pipe
->screen
;
1289 struct gl_pixelstore_attrib pack
, unpack
;
1290 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1292 if (type
== GL_COLOR
&&
1293 ctx
->Pixel
.ZoomX
== 1.0 &&
1294 ctx
->Pixel
.ZoomY
== 1.0 &&
1295 ctx
->_ImageTransferState
== 0x0 &&
1296 !ctx
->Color
.BlendEnabled
&&
1297 !ctx
->Color
.AlphaEnabled
&&
1298 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1300 !ctx
->Fog
.Enabled
&&
1301 !ctx
->Stencil
.Enabled
&&
1302 !ctx
->FragmentProgram
.Enabled
&&
1303 !ctx
->VertexProgram
.Enabled
&&
1304 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1305 !ctx
->ATIFragmentShader
._Enabled
&&
1306 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1307 !ctx
->Query
.CondRenderQuery
&&
1308 !ctx
->Query
.CurrentOcclusionObject
) {
1309 struct st_renderbuffer
*rbRead
, *rbDraw
;
1312 * Clip the read region against the src buffer bounds.
1313 * We'll still allocate a temporary buffer/texture for the original
1314 * src region size but we'll only read the region which is on-screen.
1315 * This may mean that we draw garbage pixels into the dest region, but
1322 pack
= ctx
->DefaultPacking
;
1323 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1324 return GL_TRUE
; /* all done */
1326 /* clip against dest buffer bounds and scissor box */
1327 drawX
= dstx
+ pack
.SkipPixels
;
1328 drawY
= dsty
+ pack
.SkipRows
;
1330 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1331 return GL_TRUE
; /* all done */
1333 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1334 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1339 rbRead
= st_get_color_read_renderbuffer(ctx
);
1340 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1342 /* Flip src/dst position depending on the orientation of buffers. */
1343 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1344 readY
= rbRead
->Base
.Height
- readY
;
1348 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1349 /* We can't flip the destination for pipe->blit, so we only adjust
1350 * its position and flip the source.
1352 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1357 if (rbRead
!= rbDraw
||
1358 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1359 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1360 struct pipe_blit_info blit
;
1362 memset(&blit
, 0, sizeof(blit
));
1363 blit
.src
.resource
= rbRead
->texture
;
1364 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1365 blit
.src
.format
= rbRead
->texture
->format
;
1366 blit
.src
.box
.x
= readX
;
1367 blit
.src
.box
.y
= readY
;
1368 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1369 blit
.src
.box
.width
= readW
;
1370 blit
.src
.box
.height
= readH
;
1371 blit
.src
.box
.depth
= 1;
1372 blit
.dst
.resource
= rbDraw
->texture
;
1373 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1374 blit
.dst
.format
= rbDraw
->texture
->format
;
1375 blit
.dst
.box
.x
= drawX
;
1376 blit
.dst
.box
.y
= drawY
;
1377 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1378 blit
.dst
.box
.width
= drawW
;
1379 blit
.dst
.box
.height
= drawH
;
1380 blit
.dst
.box
.depth
= 1;
1381 blit
.mask
= PIPE_MASK_RGBA
;
1382 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1384 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1385 blit
.src
.resource
->target
,
1386 blit
.src
.resource
->nr_samples
,
1387 PIPE_BIND_SAMPLER_VIEW
) &&
1388 screen
->is_format_supported(screen
, blit
.dst
.format
,
1389 blit
.dst
.resource
->target
,
1390 blit
.dst
.resource
->nr_samples
,
1391 PIPE_BIND_RENDER_TARGET
)) {
1392 pipe
->blit(pipe
, &blit
);
1403 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1404 GLsizei width
, GLsizei height
,
1405 GLint dstx
, GLint dsty
, GLenum type
)
1407 struct st_context
*st
= st_context(ctx
);
1408 struct pipe_context
*pipe
= st
->pipe
;
1409 struct pipe_screen
*screen
= pipe
->screen
;
1410 struct st_renderbuffer
*rbRead
;
1411 void *driver_vp
, *driver_fp
;
1412 struct pipe_resource
*pt
;
1413 struct pipe_sampler_view
*sv
[2] = { NULL
};
1414 struct st_fp_variant
*fpv
= NULL
;
1415 int num_sampler_view
= 1;
1416 enum pipe_format srcFormat
;
1418 GLboolean invertTex
= GL_FALSE
;
1419 GLint readX
, readY
, readW
, readH
;
1420 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1422 st_flush_bitmap_cache(st
);
1424 st_validate_state(st
, ST_PIPELINE_RENDER
);
1426 if (type
== GL_DEPTH_STENCIL
) {
1427 /* XXX make this more efficient */
1428 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1429 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1433 if (type
== GL_STENCIL
) {
1434 /* can't use texturing to do stencil */
1435 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1439 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1443 * The subsequent code implements glCopyPixels by copying the source
1444 * pixels into a temporary texture that's then applied to a textured quad.
1445 * When we draw the textured quad, all the usual per-fragment operations
1451 * Get vertex/fragment shaders
1453 if (type
== GL_COLOR
) {
1454 fpv
= get_color_fp_variant(st
);
1456 rbRead
= st_get_color_read_renderbuffer(ctx
);
1458 driver_fp
= fpv
->driver_shader
;
1459 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1461 if (ctx
->Pixel
.MapColorFlag
) {
1462 pipe_sampler_view_reference(&sv
[1],
1463 st
->pixel_xfer
.pixelmap_sampler_view
);
1467 /* compiling a new fragment shader variant added new state constants
1468 * into the constant buffer, we need to update them
1470 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1471 PIPE_SHADER_FRAGMENT
);
1474 assert(type
== GL_DEPTH
);
1475 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1476 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1478 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1479 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1482 /* Choose the format for the temporary texture. */
1483 srcFormat
= rbRead
->texture
->format
;
1484 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1485 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1487 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1489 /* srcFormat is non-renderable. Find a compatible renderable format. */
1490 if (type
== GL_DEPTH
) {
1491 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1492 GL_NONE
, st
->internal_target
, 0,
1496 assert(type
== GL_COLOR
);
1498 if (util_format_is_float(srcFormat
)) {
1499 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1500 GL_NONE
, st
->internal_target
, 0,
1503 else if (util_format_is_pure_sint(srcFormat
)) {
1504 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1505 GL_NONE
, st
->internal_target
, 0,
1508 else if (util_format_is_pure_uint(srcFormat
)) {
1509 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1510 GL_NONE
, st
->internal_target
, 0,
1513 else if (util_format_is_snorm(srcFormat
)) {
1514 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1515 GL_NONE
, st
->internal_target
, 0,
1519 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1520 GL_NONE
, st
->internal_target
, 0,
1525 if (srcFormat
== PIPE_FORMAT_NONE
) {
1526 assert(0 && "cannot choose a format for src of CopyPixels");
1531 /* Invert src region if needed */
1532 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1533 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1534 invertTex
= !invertTex
;
1537 /* Clip the read region against the src buffer bounds.
1538 * We'll still allocate a temporary buffer/texture for the original
1539 * src region size but we'll only read the region which is on-screen.
1540 * This may mean that we draw garbage pixels into the dest region, but
1547 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1548 /* The source region is completely out of bounds. Do nothing.
1549 * The GL spec says "Results of copies from outside the window,
1550 * or from regions of the window that are not exposed, are
1551 * hardware dependent and undefined."
1556 readW
= MAX2(0, readW
);
1557 readH
= MAX2(0, readH
);
1559 /* Allocate the temporary texture. */
1560 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1564 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1566 pipe_resource_reference(&pt
, NULL
);
1570 /* Copy the src region to the temporary texture. */
1572 struct pipe_blit_info blit
;
1574 memset(&blit
, 0, sizeof(blit
));
1575 blit
.src
.resource
= rbRead
->texture
;
1576 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1577 blit
.src
.format
= rbRead
->texture
->format
;
1578 blit
.src
.box
.x
= readX
;
1579 blit
.src
.box
.y
= readY
;
1580 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1581 blit
.src
.box
.width
= readW
;
1582 blit
.src
.box
.height
= readH
;
1583 blit
.src
.box
.depth
= 1;
1584 blit
.dst
.resource
= pt
;
1586 blit
.dst
.format
= pt
->format
;
1587 blit
.dst
.box
.x
= pack
.SkipPixels
;
1588 blit
.dst
.box
.y
= pack
.SkipRows
;
1590 blit
.dst
.box
.width
= readW
;
1591 blit
.dst
.box
.height
= readH
;
1592 blit
.dst
.box
.depth
= 1;
1593 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1594 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1596 pipe
->blit(pipe
, &blit
);
1599 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1600 * textured quad with that texture.
1602 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1603 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1608 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1609 invertTex
, GL_FALSE
, GL_FALSE
);
1611 pipe_resource_reference(&pt
, NULL
);
1612 pipe_sampler_view_reference(&sv
[0], NULL
);
1617 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1619 functions
->DrawPixels
= st_DrawPixels
;
1620 functions
->CopyPixels
= st_CopyPixels
;
1625 st_destroy_drawpix(struct st_context
*st
)
1629 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1630 if (st
->drawpix
.zs_shaders
[i
])
1631 cso_delete_fragment_shader(st
->cso_context
,
1632 st
->drawpix
.zs_shaders
[i
]);
1635 if (st
->drawpix
.vert_shaders
[0])
1636 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1637 if (st
->drawpix
.vert_shaders
[1])
1638 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);