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
,
384 struct gl_context
*ctx
= st
->ctx
;
385 struct pipe_context
*pipe
= st
->pipe
;
387 struct pipe_resource
*pt
= NULL
;
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 assert(st
->drawpix_cache
.texture
);
408 /* check if the pixel data is the same */
409 if (memcmp(pixels
, st
->drawpix_cache
.image
, width
* height
* bpp
) == 0) {
410 /* OK, re-use the cached texture */
411 pipe_resource_reference(&pt
, st
->drawpix_cache
.texture
);
412 /* refcount of returned texture should be at least two here. One
413 * reference for the cache to hold on to, one for the caller (which
414 * it will release), and possibly more held by the driver.
416 assert(pt
->reference
.count
>= 2);
421 /* discard the cached image and texture (if there is one) */
422 st
->drawpix_cache
.width
= 0;
423 st
->drawpix_cache
.height
= 0;
424 st
->drawpix_cache
.user_pointer
= NULL
;
425 if (st
->drawpix_cache
.image
) {
426 free(st
->drawpix_cache
.image
);
427 st
->drawpix_cache
.image
= NULL
;
429 pipe_resource_reference(&st
->drawpix_cache
.texture
, NULL
);
432 /* Choose a pixel format for the temp texture which will hold the
435 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
436 format
, type
, unpack
->SwapBytes
);
438 if (pipeFormat
== PIPE_FORMAT_NONE
) {
439 /* Use the generic approach. */
440 GLenum intFormat
= internal_format(ctx
, format
, type
);
442 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
443 st
->internal_target
, 0,
444 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
445 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
448 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
449 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
451 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
455 /* alloc temporary texture */
456 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
458 _mesa_unmap_pbo_source(ctx
, unpack
);
463 struct pipe_transfer
*transfer
;
466 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
468 /* we'll do pixel transfer in a fragment shader */
469 ctx
->_ImageTransferState
= 0x0;
471 /* map texture transfer */
472 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
473 PIPE_TRANSFER_WRITE
, 0, 0,
474 width
, height
, &transfer
);
477 /* Put image into texture transfer.
478 * Note that the image is actually going to be upside down in
479 * the texture. We deal with that with texcoords.
481 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
482 && type
== GL_UNSIGNED_BYTE
) {
483 /* Use a memcpy-based texstore to avoid software pixel swizzling.
484 * We'll do the necessary swizzling with the pipe_sampler_view to
485 * give much better performance.
486 * XXX in the future, expand this to accomodate more format and
489 _mesa_memcpy_texture(ctx
, 2,
490 mformat
, /* mesa_format */
491 transfer
->stride
, /* dstRowStride, bytes */
492 &dest
, /* destSlices */
493 width
, height
, 1, /* size */
494 format
, type
, /* src format/type */
495 pixels
, /* data source */
500 success
= _mesa_texstore(ctx
, 2, /* dims */
501 baseInternalFormat
, /* baseInternalFormat */
502 mformat
, /* mesa_format */
503 transfer
->stride
, /* dstRowStride, bytes */
504 &dest
, /* destSlices */
505 width
, height
, 1, /* size */
506 format
, type
, /* src format/type */
507 pixels
, /* data source */
512 pipe_transfer_unmap(pipe
, transfer
);
517 ctx
->_ImageTransferState
= imageTransferStateSave
;
520 _mesa_unmap_pbo_source(ctx
, unpack
);
522 #if USE_DRAWPIXELS_CACHE
523 /* Save the glDrawPixels parameter and image in the cache */
524 if ((unpack
->RowLength
== 0 || unpack
->RowLength
== width
) &&
525 unpack
->SkipPixels
== 0 &&
526 unpack
->SkipRows
== 0) {
527 st
->drawpix_cache
.width
= width
;
528 st
->drawpix_cache
.height
= height
;
529 st
->drawpix_cache
.format
= format
;
530 st
->drawpix_cache
.type
= type
;
531 st
->drawpix_cache
.user_pointer
= pixels
;
532 assert(!st
->drawpix_cache
.image
);
533 st
->drawpix_cache
.image
= malloc(width
* height
* bpp
);
534 if (st
->drawpix_cache
.image
) {
535 memcpy(st
->drawpix_cache
.image
, pixels
, width
* height
* bpp
);
536 pipe_resource_reference(&st
->drawpix_cache
.texture
, pt
);
539 /* out of memory, free/disable cached texture */
540 st
->drawpix_cache
.width
= 0;
541 st
->drawpix_cache
.height
= 0;
542 pipe_resource_reference(&st
->drawpix_cache
.texture
, NULL
);
552 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
553 GLsizei width
, GLsizei height
,
554 GLfloat zoomX
, GLfloat zoomY
,
555 struct pipe_sampler_view
**sv
,
556 int num_sampler_view
,
559 struct st_fp_variant
*fpv
,
560 const GLfloat
*color
,
562 GLboolean write_depth
, GLboolean write_stencil
)
564 struct st_context
*st
= st_context(ctx
);
565 struct pipe_context
*pipe
= st
->pipe
;
566 struct cso_context
*cso
= st
->cso_context
;
567 const unsigned fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
568 const unsigned fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
569 GLfloat x0
, y0
, x1
, y1
;
571 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
572 unsigned cso_state_mask
;
574 assert(sv
[0]->texture
->target
== st
->internal_target
);
577 /* XXX if DrawPixels image is larger than max texture size, break
580 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
581 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
582 assert(width
<= maxSize
);
583 assert(height
<= maxSize
);
585 cso_state_mask
= (CSO_BIT_RASTERIZER
|
587 CSO_BIT_FRAGMENT_SAMPLERS
|
588 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
589 CSO_BIT_STREAM_OUTPUTS
|
590 CSO_BIT_VERTEX_ELEMENTS
|
591 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
592 CSO_BITS_ALL_SHADERS
);
594 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
597 cso_save_state(cso
, cso_state_mask
);
599 /* rasterizer state: just scissor */
601 struct pipe_rasterizer_state rasterizer
;
602 memset(&rasterizer
, 0, sizeof(rasterizer
));
603 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
604 ctx
->Color
._ClampFragmentColor
;
605 rasterizer
.half_pixel_center
= 1;
606 rasterizer
.bottom_edge_rule
= 1;
607 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
608 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
609 cso_set_rasterizer(cso
, &rasterizer
);
613 /* Stencil writing bypasses the normal fragment pipeline to
614 * disable color writing and set stencil test to always pass.
616 struct pipe_depth_stencil_alpha_state dsa
;
617 struct pipe_blend_state blend
;
620 memset(&dsa
, 0, sizeof(dsa
));
621 dsa
.stencil
[0].enabled
= 1;
622 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
623 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
624 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
626 /* writing depth+stencil: depth test always passes */
627 dsa
.depth
.enabled
= 1;
628 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
629 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
631 cso_set_depth_stencil_alpha(cso
, &dsa
);
633 /* blend (colormask) */
634 memset(&blend
, 0, sizeof(blend
));
635 cso_set_blend(cso
, &blend
);
638 /* fragment shader state: TEX lookup program */
639 cso_set_fragment_shader_handle(cso
, driver_fp
);
641 /* vertex shader state: position + texcoord pass-through */
642 cso_set_vertex_shader_handle(cso
, driver_vp
);
644 /* disable other shaders */
645 cso_set_tessctrl_shader_handle(cso
, NULL
);
646 cso_set_tesseval_shader_handle(cso
, NULL
);
647 cso_set_geometry_shader_handle(cso
, NULL
);
649 /* user samplers, plus the drawpix samplers */
651 struct pipe_sampler_state sampler
;
653 memset(&sampler
, 0, sizeof(sampler
));
654 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
655 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
656 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
657 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
658 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
659 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
660 sampler
.normalized_coords
= normalized
;
663 /* drawing a color image */
664 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
665 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
666 fpv
->pixelmap_sampler
+ 1,
667 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
670 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
671 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
673 samplers
[fpv
->drawpix_sampler
] = &sampler
;
675 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
677 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
679 /* drawing a depth/stencil image */
680 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
682 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
686 /* user textures, plus the drawpix textures */
688 /* drawing a color image */
689 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
690 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
691 fpv
->pixelmap_sampler
+ 1,
692 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
694 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
695 sizeof(sampler_views
));
697 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
699 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
700 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
702 /* drawing a depth/stencil image */
703 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
706 /* viewport state: viewport matching window dims */
707 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
709 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
710 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
712 /* Compute Gallium window coords (y=0=top) with pixel zoom.
713 * Recall that these coords are transformed by the current
714 * vertex shader and viewport transformation.
716 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
717 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
718 invertTex
= !invertTex
;
722 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
724 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
726 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
730 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
731 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
732 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
733 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
734 const float maxXcoord
= normalized
?
735 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
736 const float maxYcoord
= normalized
737 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
738 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
739 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
740 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
742 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
743 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
744 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
749 cso_restore_state(cso
);
754 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
755 * can't use a fragment shader to write stencil values.
758 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
759 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
760 const struct gl_pixelstore_attrib
*unpack
,
763 struct st_context
*st
= st_context(ctx
);
764 struct pipe_context
*pipe
= st
->pipe
;
765 struct st_renderbuffer
*strb
;
766 enum pipe_transfer_usage usage
;
767 struct pipe_transfer
*pt
;
768 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
770 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
775 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
777 /* totally clipped */
782 strb
= st_renderbuffer(ctx
->DrawBuffer
->
783 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
785 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
786 y
= ctx
->DrawBuffer
->Height
- y
- height
;
789 if (format
== GL_STENCIL_INDEX
&&
790 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
791 /* writing stencil to a combined depth+stencil buffer */
792 usage
= PIPE_TRANSFER_READ_WRITE
;
795 usage
= PIPE_TRANSFER_WRITE
;
798 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
799 strb
->surface
->u
.tex
.level
,
800 strb
->surface
->u
.tex
.first_layer
,
804 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
807 sValues
= malloc(width
* sizeof(GLubyte
));
808 zValues
= malloc(width
* sizeof(GLuint
));
810 if (sValues
&& zValues
) {
812 for (row
= 0; row
< height
; row
++) {
813 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
814 GLenum destType
= GL_UNSIGNED_BYTE
;
815 const void *source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
819 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
820 type
, source
, &clippedUnpack
,
821 ctx
->_ImageTransferState
);
823 if (format
== GL_DEPTH_STENCIL
) {
825 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
826 GL_FLOAT
: GL_UNSIGNED_INT
;
828 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
829 (1 << 24) - 1, type
, source
,
834 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
835 "zoom not complete");
841 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
842 spanY
= height
- row
- 1;
848 /* now pack the stencil (and Z) values in the dest format */
849 switch (pt
->resource
->format
) {
850 case PIPE_FORMAT_S8_UINT
:
852 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
853 assert(usage
== PIPE_TRANSFER_WRITE
);
854 memcpy(dest
, sValues
, width
);
857 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
858 if (format
== GL_DEPTH_STENCIL
) {
859 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
861 assert(usage
== PIPE_TRANSFER_WRITE
);
862 for (k
= 0; k
< width
; k
++) {
863 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
867 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
869 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
870 for (k
= 0; k
< width
; k
++) {
871 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
875 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
876 if (format
== GL_DEPTH_STENCIL
) {
877 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
879 assert(usage
== PIPE_TRANSFER_WRITE
);
880 for (k
= 0; k
< width
; k
++) {
881 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
885 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
887 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
888 for (k
= 0; k
< width
; k
++) {
889 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
893 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
894 if (format
== GL_DEPTH_STENCIL
) {
895 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
896 GLfloat
*destf
= (GLfloat
*)dest
;
898 assert(usage
== PIPE_TRANSFER_WRITE
);
899 for (k
= 0; k
< width
; k
++) {
900 destf
[k
*2] = zValuesFloat
[k
];
901 dest
[k
*2+1] = sValues
[k
] & 0xff;
905 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
907 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
908 for (k
= 0; k
< width
; k
++) {
909 dest
[k
*2+1] = sValues
[k
] & 0xff;
920 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
926 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
928 /* unmap the stencil buffer */
929 pipe_transfer_unmap(pipe
, pt
);
934 * Get fragment program variant for a glDrawPixels or glCopyPixels
935 * command for RGBA data.
937 static struct st_fp_variant
*
938 get_color_fp_variant(struct st_context
*st
)
940 struct gl_context
*ctx
= st
->ctx
;
941 struct st_fp_variant_key key
;
942 struct st_fp_variant
*fpv
;
944 memset(&key
, 0, sizeof(key
));
946 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
948 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
949 ctx
->Pixel
.RedScale
!= 1.0 ||
950 ctx
->Pixel
.GreenBias
!= 0.0 ||
951 ctx
->Pixel
.GreenScale
!= 1.0 ||
952 ctx
->Pixel
.BlueBias
!= 0.0 ||
953 ctx
->Pixel
.BlueScale
!= 1.0 ||
954 ctx
->Pixel
.AlphaBias
!= 0.0 ||
955 ctx
->Pixel
.AlphaScale
!= 1.0);
956 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
957 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
958 ctx
->Color
._ClampFragmentColor
;
960 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
967 * Clamp glDrawPixels width and height to the maximum texture size.
970 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
971 struct gl_pixelstore_attrib
*unpack
)
974 1 << (pipe
->screen
->get_param(pipe
->screen
,
975 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
977 if (*width
> maxSize
) {
978 if (unpack
->RowLength
== 0)
979 unpack
->RowLength
= *width
;
982 if (*height
> maxSize
) {
989 * Search the array of 4 swizzle components for the named component and return
993 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
996 for (i
= 0; i
< 4; i
++) {
997 if (swizzle
[i
] == component
)
1000 assert(!"search_swizzle() failed");
1006 * Set the sampler view's swizzle terms. This is used to handle RGBA
1007 * swizzling when the incoming image format isn't an exact match for
1008 * the actual texture format. For example, if we have glDrawPixels(
1009 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1010 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1011 * avoid swizzling all the pixels in software in the texstore code.
1014 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1016 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1017 const struct util_format_description
*desc
=
1018 util_format_description(sv
->texture
->format
);
1019 unsigned c0
, c1
, c2
, c3
;
1021 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1022 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1024 assert(desc
->block
.bits
== 32);
1026 /* invert the format's swizzle to setup the sampler's swizzle */
1027 if (format
== GL_RGBA
) {
1028 c0
= UTIL_FORMAT_SWIZZLE_X
;
1029 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1030 c2
= UTIL_FORMAT_SWIZZLE_Z
;
1031 c3
= UTIL_FORMAT_SWIZZLE_W
;
1034 assert(format
== GL_BGRA
);
1035 c0
= UTIL_FORMAT_SWIZZLE_Z
;
1036 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1037 c2
= UTIL_FORMAT_SWIZZLE_X
;
1038 c3
= UTIL_FORMAT_SWIZZLE_W
;
1040 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1041 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1042 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1043 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1046 /* use the default sampler swizzle */
1052 * Called via ctx->Driver.DrawPixels()
1055 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1056 GLsizei width
, GLsizei height
,
1057 GLenum format
, GLenum type
,
1058 const struct gl_pixelstore_attrib
*unpack
, const void *pixels
)
1060 void *driver_vp
, *driver_fp
;
1061 struct st_context
*st
= st_context(ctx
);
1062 struct pipe_context
*pipe
= st
->pipe
;
1063 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1064 struct pipe_sampler_view
*sv
[2] = { NULL
};
1065 int num_sampler_view
= 1;
1066 struct gl_pixelstore_attrib clippedUnpack
;
1067 struct st_fp_variant
*fpv
= NULL
;
1068 struct pipe_resource
*pt
;
1070 /* Mesa state should be up to date by now */
1071 assert(ctx
->NewState
== 0x0);
1073 st_flush_bitmap_cache(st
);
1075 st_validate_state(st
, ST_PIPELINE_RENDER
);
1077 /* Limit the size of the glDrawPixels to the max texture size.
1078 * Strictly speaking, that's not correct but since we don't handle
1079 * larger images yet, this is better than crashing.
1081 clippedUnpack
= *unpack
;
1082 unpack
= &clippedUnpack
;
1083 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1085 if (format
== GL_DEPTH_STENCIL
)
1086 write_stencil
= write_depth
= GL_TRUE
;
1087 else if (format
== GL_STENCIL_INDEX
)
1088 write_stencil
= GL_TRUE
;
1089 else if (format
== GL_DEPTH_COMPONENT
)
1090 write_depth
= GL_TRUE
;
1092 if (write_stencil
&&
1093 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1094 /* software fallback */
1095 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1101 * Get vertex/fragment shaders
1103 if (write_depth
|| write_stencil
) {
1104 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1106 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1109 fpv
= get_color_fp_variant(st
);
1111 driver_fp
= fpv
->driver_shader
;
1112 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1114 if (ctx
->Pixel
.MapColorFlag
) {
1115 pipe_sampler_view_reference(&sv
[1],
1116 st
->pixel_xfer
.pixelmap_sampler_view
);
1120 /* compiling a new fragment shader variant added new state constants
1121 * into the constant buffer, we need to update them
1123 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1124 PIPE_SHADER_FRAGMENT
);
1127 /* Put glDrawPixels image into a texture */
1128 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1130 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1134 /* create sampler view for the image */
1135 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1137 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1138 pipe_resource_reference(&pt
, NULL
);
1142 /* Set up the sampler view's swizzle */
1143 setup_sampler_swizzle(sv
[0], format
, type
);
1145 /* Create a second sampler view to read stencil. The stencil is
1146 * written using the shader stencil export functionality.
1148 if (write_stencil
) {
1149 enum pipe_format stencil_format
=
1150 util_format_stencil_only(pt
->format
);
1151 /* we should not be doing pixel map/transfer (see above) */
1152 assert(num_sampler_view
== 1);
1153 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1156 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1157 pipe_resource_reference(&pt
, NULL
);
1158 pipe_sampler_view_reference(&sv
[0], NULL
);
1164 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1166 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1171 ctx
->Current
.RasterColor
,
1172 GL_FALSE
, write_depth
, write_stencil
);
1173 pipe_sampler_view_reference(&sv
[0], NULL
);
1174 if (num_sampler_view
> 1)
1175 pipe_sampler_view_reference(&sv
[1], NULL
);
1177 /* free the texture (but may persist in the cache) */
1178 pipe_resource_reference(&pt
, NULL
);
1184 * Software fallback for glCopyPixels(GL_STENCIL).
1187 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1188 GLsizei width
, GLsizei height
,
1189 GLint dstx
, GLint dsty
)
1191 struct st_renderbuffer
*rbDraw
;
1192 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1193 enum pipe_transfer_usage usage
;
1194 struct pipe_transfer
*ptDraw
;
1199 buffer
= malloc(width
* height
* sizeof(ubyte
));
1201 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1205 /* Get the dest renderbuffer */
1206 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1207 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1209 /* this will do stencil pixel transfer ops */
1210 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1211 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1212 &ctx
->DefaultPacking
, buffer
);
1215 /* debug code: dump stencil values */
1217 for (row
= 0; row
< height
; row
++) {
1218 printf("%3d: ", row
);
1219 for (col
= 0; col
< width
; col
++) {
1220 printf("%02x ", buffer
[col
+ row
* width
]);
1226 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1227 usage
= PIPE_TRANSFER_READ_WRITE
;
1229 usage
= PIPE_TRANSFER_WRITE
;
1231 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1232 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1235 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1236 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1238 /* map the stencil buffer */
1239 drawMap
= pipe_transfer_map(pipe
,
1241 rbDraw
->surface
->u
.tex
.level
,
1242 rbDraw
->surface
->u
.tex
.first_layer
,
1244 width
, height
, &ptDraw
);
1247 /* XXX PixelZoom not handled yet */
1248 for (i
= 0; i
< height
; i
++) {
1255 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1259 dst
= drawMap
+ y
* ptDraw
->stride
;
1260 src
= buffer
+ i
* width
;
1262 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1267 /* unmap the stencil buffer */
1268 pipe_transfer_unmap(pipe
, ptDraw
);
1273 * Return renderbuffer to use for reading color pixels for glCopyPixels
1275 static struct st_renderbuffer
*
1276 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1278 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1279 struct st_renderbuffer
*strb
=
1280 st_renderbuffer(fb
->_ColorReadBuffer
);
1287 * Try to do a glCopyPixels for simple cases with a blit by calling
1290 * We can do this when we're copying color pixels (depth/stencil
1291 * eventually) with no pixel zoom, no pixel transfer ops, no
1292 * per-fragment ops, and the src/dest regions don't overlap.
1295 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1296 GLsizei width
, GLsizei height
,
1297 GLint dstx
, GLint dsty
, GLenum type
)
1299 struct st_context
*st
= st_context(ctx
);
1300 struct pipe_context
*pipe
= st
->pipe
;
1301 struct pipe_screen
*screen
= pipe
->screen
;
1302 struct gl_pixelstore_attrib pack
, unpack
;
1303 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1305 if (type
== GL_COLOR
&&
1306 ctx
->Pixel
.ZoomX
== 1.0 &&
1307 ctx
->Pixel
.ZoomY
== 1.0 &&
1308 ctx
->_ImageTransferState
== 0x0 &&
1309 !ctx
->Color
.BlendEnabled
&&
1310 !ctx
->Color
.AlphaEnabled
&&
1311 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1313 !ctx
->Fog
.Enabled
&&
1314 !ctx
->Stencil
.Enabled
&&
1315 !ctx
->FragmentProgram
.Enabled
&&
1316 !ctx
->VertexProgram
.Enabled
&&
1317 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1318 !ctx
->ATIFragmentShader
._Enabled
&&
1319 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1320 !ctx
->Query
.CondRenderQuery
&&
1321 !ctx
->Query
.CurrentOcclusionObject
) {
1322 struct st_renderbuffer
*rbRead
, *rbDraw
;
1325 * Clip the read region against the src buffer bounds.
1326 * We'll still allocate a temporary buffer/texture for the original
1327 * src region size but we'll only read the region which is on-screen.
1328 * This may mean that we draw garbage pixels into the dest region, but
1335 pack
= ctx
->DefaultPacking
;
1336 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1337 return GL_TRUE
; /* all done */
1339 /* clip against dest buffer bounds and scissor box */
1340 drawX
= dstx
+ pack
.SkipPixels
;
1341 drawY
= dsty
+ pack
.SkipRows
;
1343 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1344 return GL_TRUE
; /* all done */
1346 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1347 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1352 rbRead
= st_get_color_read_renderbuffer(ctx
);
1353 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1355 /* Flip src/dst position depending on the orientation of buffers. */
1356 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1357 readY
= rbRead
->Base
.Height
- readY
;
1361 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1362 /* We can't flip the destination for pipe->blit, so we only adjust
1363 * its position and flip the source.
1365 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1370 if (rbRead
!= rbDraw
||
1371 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1372 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1373 struct pipe_blit_info blit
;
1375 memset(&blit
, 0, sizeof(blit
));
1376 blit
.src
.resource
= rbRead
->texture
;
1377 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1378 blit
.src
.format
= rbRead
->texture
->format
;
1379 blit
.src
.box
.x
= readX
;
1380 blit
.src
.box
.y
= readY
;
1381 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1382 blit
.src
.box
.width
= readW
;
1383 blit
.src
.box
.height
= readH
;
1384 blit
.src
.box
.depth
= 1;
1385 blit
.dst
.resource
= rbDraw
->texture
;
1386 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1387 blit
.dst
.format
= rbDraw
->texture
->format
;
1388 blit
.dst
.box
.x
= drawX
;
1389 blit
.dst
.box
.y
= drawY
;
1390 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1391 blit
.dst
.box
.width
= drawW
;
1392 blit
.dst
.box
.height
= drawH
;
1393 blit
.dst
.box
.depth
= 1;
1394 blit
.mask
= PIPE_MASK_RGBA
;
1395 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1397 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1398 blit
.src
.resource
->target
,
1399 blit
.src
.resource
->nr_samples
,
1400 PIPE_BIND_SAMPLER_VIEW
) &&
1401 screen
->is_format_supported(screen
, blit
.dst
.format
,
1402 blit
.dst
.resource
->target
,
1403 blit
.dst
.resource
->nr_samples
,
1404 PIPE_BIND_RENDER_TARGET
)) {
1405 pipe
->blit(pipe
, &blit
);
1416 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1417 GLsizei width
, GLsizei height
,
1418 GLint dstx
, GLint dsty
, GLenum type
)
1420 struct st_context
*st
= st_context(ctx
);
1421 struct pipe_context
*pipe
= st
->pipe
;
1422 struct pipe_screen
*screen
= pipe
->screen
;
1423 struct st_renderbuffer
*rbRead
;
1424 void *driver_vp
, *driver_fp
;
1425 struct pipe_resource
*pt
;
1426 struct pipe_sampler_view
*sv
[2] = { NULL
};
1427 struct st_fp_variant
*fpv
= NULL
;
1428 int num_sampler_view
= 1;
1429 enum pipe_format srcFormat
;
1431 GLboolean invertTex
= GL_FALSE
;
1432 GLint readX
, readY
, readW
, readH
;
1433 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1435 st_flush_bitmap_cache(st
);
1437 st_validate_state(st
, ST_PIPELINE_RENDER
);
1439 if (type
== GL_DEPTH_STENCIL
) {
1440 /* XXX make this more efficient */
1441 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1442 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1446 if (type
== GL_STENCIL
) {
1447 /* can't use texturing to do stencil */
1448 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1452 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1456 * The subsequent code implements glCopyPixels by copying the source
1457 * pixels into a temporary texture that's then applied to a textured quad.
1458 * When we draw the textured quad, all the usual per-fragment operations
1464 * Get vertex/fragment shaders
1466 if (type
== GL_COLOR
) {
1467 fpv
= get_color_fp_variant(st
);
1469 rbRead
= st_get_color_read_renderbuffer(ctx
);
1471 driver_fp
= fpv
->driver_shader
;
1472 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1474 if (ctx
->Pixel
.MapColorFlag
) {
1475 pipe_sampler_view_reference(&sv
[1],
1476 st
->pixel_xfer
.pixelmap_sampler_view
);
1480 /* compiling a new fragment shader variant added new state constants
1481 * into the constant buffer, we need to update them
1483 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1484 PIPE_SHADER_FRAGMENT
);
1487 assert(type
== GL_DEPTH
);
1488 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1489 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1491 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1492 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1495 /* Choose the format for the temporary texture. */
1496 srcFormat
= rbRead
->texture
->format
;
1497 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1498 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1500 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1502 /* srcFormat is non-renderable. Find a compatible renderable format. */
1503 if (type
== GL_DEPTH
) {
1504 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1505 GL_NONE
, st
->internal_target
, 0,
1509 assert(type
== GL_COLOR
);
1511 if (util_format_is_float(srcFormat
)) {
1512 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1513 GL_NONE
, st
->internal_target
, 0,
1516 else if (util_format_is_pure_sint(srcFormat
)) {
1517 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1518 GL_NONE
, st
->internal_target
, 0,
1521 else if (util_format_is_pure_uint(srcFormat
)) {
1522 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1523 GL_NONE
, st
->internal_target
, 0,
1526 else if (util_format_is_snorm(srcFormat
)) {
1527 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1528 GL_NONE
, st
->internal_target
, 0,
1532 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1533 GL_NONE
, st
->internal_target
, 0,
1538 if (srcFormat
== PIPE_FORMAT_NONE
) {
1539 assert(0 && "cannot choose a format for src of CopyPixels");
1544 /* Invert src region if needed */
1545 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1546 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1547 invertTex
= !invertTex
;
1550 /* Clip the read region against the src buffer bounds.
1551 * We'll still allocate a temporary buffer/texture for the original
1552 * src region size but we'll only read the region which is on-screen.
1553 * This may mean that we draw garbage pixels into the dest region, but
1560 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1561 /* The source region is completely out of bounds. Do nothing.
1562 * The GL spec says "Results of copies from outside the window,
1563 * or from regions of the window that are not exposed, are
1564 * hardware dependent and undefined."
1569 readW
= MAX2(0, readW
);
1570 readH
= MAX2(0, readH
);
1572 /* Allocate the temporary texture. */
1573 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1577 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1579 pipe_resource_reference(&pt
, NULL
);
1583 /* Copy the src region to the temporary texture. */
1585 struct pipe_blit_info blit
;
1587 memset(&blit
, 0, sizeof(blit
));
1588 blit
.src
.resource
= rbRead
->texture
;
1589 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1590 blit
.src
.format
= rbRead
->texture
->format
;
1591 blit
.src
.box
.x
= readX
;
1592 blit
.src
.box
.y
= readY
;
1593 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1594 blit
.src
.box
.width
= readW
;
1595 blit
.src
.box
.height
= readH
;
1596 blit
.src
.box
.depth
= 1;
1597 blit
.dst
.resource
= pt
;
1599 blit
.dst
.format
= pt
->format
;
1600 blit
.dst
.box
.x
= pack
.SkipPixels
;
1601 blit
.dst
.box
.y
= pack
.SkipRows
;
1603 blit
.dst
.box
.width
= readW
;
1604 blit
.dst
.box
.height
= readH
;
1605 blit
.dst
.box
.depth
= 1;
1606 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1607 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1609 pipe
->blit(pipe
, &blit
);
1612 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1613 * textured quad with that texture.
1615 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1616 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1621 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1622 invertTex
, GL_FALSE
, GL_FALSE
);
1624 pipe_resource_reference(&pt
, NULL
);
1625 pipe_sampler_view_reference(&sv
[0], NULL
);
1630 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1632 functions
->DrawPixels
= st_DrawPixels
;
1633 functions
->CopyPixels
= st_CopyPixels
;
1638 st_destroy_drawpix(struct st_context
*st
)
1642 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1643 if (st
->drawpix
.zs_shaders
[i
])
1644 cso_delete_fragment_shader(st
->cso_context
,
1645 st
->drawpix
.zs_shaders
[i
]);
1648 if (st
->drawpix
.vert_shaders
[0])
1649 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1650 if (st
->drawpix
.vert_shaders
[1])
1651 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);