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 * Create fragment program that does a TEX() instruction to get a Z and/or
77 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
78 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
79 * Pass fragment color through as-is.
81 * \return CSO of the fragment shader.
84 get_drawpix_z_stencil_program(struct st_context
*st
,
85 GLboolean write_depth
,
86 GLboolean write_stencil
)
88 struct ureg_program
*ureg
;
89 struct ureg_src depth_sampler
, stencil_sampler
;
90 struct ureg_src texcoord
, color
;
91 struct ureg_dst out_color
, out_depth
, out_stencil
;
92 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
95 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
97 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
98 /* already have the proper shader */
99 return st
->drawpix
.zs_shaders
[shaderIndex
];
102 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
106 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
109 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
110 TGSI_INTERPOLATE_COLOR
);
111 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
113 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
114 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
118 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
119 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
122 texcoord
= ureg_DECL_fs_input(ureg
,
123 st
->needs_texcoord_semantic
?
124 TGSI_SEMANTIC_TEXCOORD
:
125 TGSI_SEMANTIC_GENERIC
,
126 0, TGSI_INTERPOLATE_LINEAR
);
129 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
130 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
131 ureg_MOV(ureg
, out_color
, color
);
135 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
136 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
139 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
141 /* save the new shader */
142 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
148 * Create a simple vertex shader that just passes through the
149 * vertex position and texcoord (and optionally, color).
152 make_passthrough_vertex_shader(struct st_context
*st
,
155 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
156 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
158 if (!st
->drawpix
.vert_shaders
[passColor
]) {
159 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
164 /* MOV result.pos, vertex.pos; */
166 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
167 ureg_DECL_vs_input( ureg
, 0 ));
170 /* MOV result.color0, vertex.attr[1]; */
172 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
173 ureg_DECL_vs_input( ureg
, 1 ));
176 /* MOV result.texcoord0, vertex.attr[2]; */
178 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
179 ureg_DECL_vs_input( ureg
, 2 ));
183 st
->drawpix
.vert_shaders
[passColor
] =
184 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
187 return st
->drawpix
.vert_shaders
[passColor
];
192 * Return a texture internalFormat for drawing/copying an image
193 * of the given format and type.
196 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
199 case GL_DEPTH_COMPONENT
:
201 case GL_UNSIGNED_SHORT
:
202 return GL_DEPTH_COMPONENT16
;
204 case GL_UNSIGNED_INT
:
205 return GL_DEPTH_COMPONENT32
;
208 if (ctx
->Extensions
.ARB_depth_buffer_float
)
209 return GL_DEPTH_COMPONENT32F
;
211 return GL_DEPTH_COMPONENT
;
214 return GL_DEPTH_COMPONENT
;
217 case GL_DEPTH_STENCIL
:
219 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
220 return GL_DEPTH32F_STENCIL8
;
222 case GL_UNSIGNED_INT_24_8
:
224 return GL_DEPTH24_STENCIL8
;
227 case GL_STENCIL_INDEX
:
228 return GL_STENCIL_INDEX
;
231 if (_mesa_is_enum_format_integer(format
)) {
235 case GL_UNSIGNED_BYTE
:
239 case GL_UNSIGNED_SHORT
:
243 case GL_UNSIGNED_INT
:
246 assert(0 && "Unexpected type in internal_format()");
247 return GL_RGBA_INTEGER
;
252 case GL_UNSIGNED_BYTE
:
253 case GL_UNSIGNED_INT_8_8_8_8
:
254 case GL_UNSIGNED_INT_8_8_8_8_REV
:
258 case GL_UNSIGNED_BYTE_3_3_2
:
259 case GL_UNSIGNED_BYTE_2_3_3_REV
:
262 case GL_UNSIGNED_SHORT_4_4_4_4
:
263 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
266 case GL_UNSIGNED_SHORT_5_6_5
:
267 case GL_UNSIGNED_SHORT_5_6_5_REV
:
270 case GL_UNSIGNED_SHORT_5_5_5_1
:
271 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
274 case GL_UNSIGNED_INT_10_10_10_2
:
275 case GL_UNSIGNED_INT_2_10_10_10_REV
:
278 case GL_UNSIGNED_SHORT
:
279 case GL_UNSIGNED_INT
:
284 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
289 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
291 case GL_HALF_FLOAT_ARB
:
293 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
294 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
299 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
300 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
302 case GL_UNSIGNED_INT_5_9_9_9_REV
:
303 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
306 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
307 assert(ctx
->Extensions
.EXT_packed_float
);
308 return GL_R11F_G11F_B10F
;
316 * Create a temporary texture to hold an image of the given size.
317 * If width, height are not POT and the driver only handles POT textures,
318 * allocate the next larger size of texture that is POT.
320 static struct pipe_resource
*
321 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
322 enum pipe_format texFormat
, unsigned bind
)
324 struct pipe_resource
*pt
;
326 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
327 width
, height
, 1, 1, 0, bind
);
334 * Make texture containing an image for glDrawPixels image.
335 * If 'pixels' is NULL, leave the texture image data undefined.
337 static struct pipe_resource
*
338 make_texture(struct st_context
*st
,
339 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
340 const struct gl_pixelstore_attrib
*unpack
,
341 const GLvoid
*pixels
)
343 struct gl_context
*ctx
= st
->ctx
;
344 struct pipe_context
*pipe
= st
->pipe
;
346 struct pipe_resource
*pt
;
347 enum pipe_format pipeFormat
;
348 GLenum baseInternalFormat
;
350 /* Choose a pixel format for the temp texture which will hold the
353 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
354 format
, type
, unpack
->SwapBytes
);
356 if (pipeFormat
== PIPE_FORMAT_NONE
) {
357 /* Use the generic approach. */
358 GLenum intFormat
= internal_format(ctx
, format
, type
);
360 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
361 st
->internal_target
, 0,
362 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
363 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
366 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
367 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
369 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
373 /* alloc temporary texture */
374 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
376 _mesa_unmap_pbo_source(ctx
, unpack
);
381 struct pipe_transfer
*transfer
;
384 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
386 /* we'll do pixel transfer in a fragment shader */
387 ctx
->_ImageTransferState
= 0x0;
389 /* map texture transfer */
390 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
391 PIPE_TRANSFER_WRITE
, 0, 0,
392 width
, height
, &transfer
);
395 /* Put image into texture transfer.
396 * Note that the image is actually going to be upside down in
397 * the texture. We deal with that with texcoords.
399 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
400 && type
== GL_UNSIGNED_BYTE
) {
401 /* Use a memcpy-based texstore to avoid software pixel swizzling.
402 * We'll do the necessary swizzling with the pipe_sampler_view to
403 * give much better performance.
404 * XXX in the future, expand this to accomodate more format and
407 _mesa_memcpy_texture(ctx
, 2,
408 mformat
, /* mesa_format */
409 transfer
->stride
, /* dstRowStride, bytes */
410 &dest
, /* destSlices */
411 width
, height
, 1, /* size */
412 format
, type
, /* src format/type */
413 pixels
, /* data source */
418 success
= _mesa_texstore(ctx
, 2, /* dims */
419 baseInternalFormat
, /* baseInternalFormat */
420 mformat
, /* mesa_format */
421 transfer
->stride
, /* dstRowStride, bytes */
422 &dest
, /* destSlices */
423 width
, height
, 1, /* size */
424 format
, type
, /* src format/type */
425 pixels
, /* data source */
430 pipe_transfer_unmap(pipe
, transfer
);
435 ctx
->_ImageTransferState
= imageTransferStateSave
;
438 _mesa_unmap_pbo_source(ctx
, unpack
);
445 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
446 GLsizei width
, GLsizei height
,
447 GLfloat zoomX
, GLfloat zoomY
,
448 struct pipe_sampler_view
**sv
,
449 int num_sampler_view
,
452 struct st_fp_variant
*fpv
,
453 const GLfloat
*color
,
455 GLboolean write_depth
, GLboolean write_stencil
)
457 struct st_context
*st
= st_context(ctx
);
458 struct pipe_context
*pipe
= st
->pipe
;
459 struct cso_context
*cso
= st
->cso_context
;
460 const unsigned fb_width
= _mesa_geometric_width(ctx
->DrawBuffer
);
461 const unsigned fb_height
= _mesa_geometric_height(ctx
->DrawBuffer
);
462 GLfloat x0
, y0
, x1
, y1
;
464 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
465 unsigned cso_state_mask
;
467 assert(sv
[0]->texture
->target
== st
->internal_target
);
470 /* XXX if DrawPixels image is larger than max texture size, break
473 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
474 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
475 assert(width
<= maxSize
);
476 assert(height
<= maxSize
);
478 cso_state_mask
= (CSO_BIT_RASTERIZER
|
480 CSO_BIT_FRAGMENT_SAMPLERS
|
481 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
482 CSO_BIT_STREAM_OUTPUTS
|
483 CSO_BIT_VERTEX_ELEMENTS
|
484 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
485 CSO_BITS_ALL_SHADERS
);
487 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
490 cso_save_state(cso
, cso_state_mask
);
492 /* rasterizer state: just scissor */
494 struct pipe_rasterizer_state rasterizer
;
495 memset(&rasterizer
, 0, sizeof(rasterizer
));
496 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
497 ctx
->Color
._ClampFragmentColor
;
498 rasterizer
.half_pixel_center
= 1;
499 rasterizer
.bottom_edge_rule
= 1;
500 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
501 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
502 cso_set_rasterizer(cso
, &rasterizer
);
506 /* Stencil writing bypasses the normal fragment pipeline to
507 * disable color writing and set stencil test to always pass.
509 struct pipe_depth_stencil_alpha_state dsa
;
510 struct pipe_blend_state blend
;
513 memset(&dsa
, 0, sizeof(dsa
));
514 dsa
.stencil
[0].enabled
= 1;
515 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
516 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
517 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
519 /* writing depth+stencil: depth test always passes */
520 dsa
.depth
.enabled
= 1;
521 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
522 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
524 cso_set_depth_stencil_alpha(cso
, &dsa
);
526 /* blend (colormask) */
527 memset(&blend
, 0, sizeof(blend
));
528 cso_set_blend(cso
, &blend
);
531 /* fragment shader state: TEX lookup program */
532 cso_set_fragment_shader_handle(cso
, driver_fp
);
534 /* vertex shader state: position + texcoord pass-through */
535 cso_set_vertex_shader_handle(cso
, driver_vp
);
537 /* disable other shaders */
538 cso_set_tessctrl_shader_handle(cso
, NULL
);
539 cso_set_tesseval_shader_handle(cso
, NULL
);
540 cso_set_geometry_shader_handle(cso
, NULL
);
542 /* user samplers, plus the drawpix samplers */
544 struct pipe_sampler_state sampler
;
546 memset(&sampler
, 0, sizeof(sampler
));
547 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
548 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
549 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
550 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
551 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
552 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
553 sampler
.normalized_coords
= normalized
;
556 /* drawing a color image */
557 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
558 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
559 fpv
->pixelmap_sampler
+ 1,
560 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
563 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
564 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
566 samplers
[fpv
->drawpix_sampler
] = &sampler
;
568 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
570 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
572 /* drawing a depth/stencil image */
573 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
575 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
579 /* user textures, plus the drawpix textures */
581 /* drawing a color image */
582 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
583 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
584 fpv
->pixelmap_sampler
+ 1,
585 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
587 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
588 sizeof(sampler_views
));
590 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
592 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
593 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
595 /* drawing a depth/stencil image */
596 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
599 /* viewport state: viewport matching window dims */
600 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
602 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
603 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
605 /* Compute Gallium window coords (y=0=top) with pixel zoom.
606 * Recall that these coords are transformed by the current
607 * vertex shader and viewport transformation.
609 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
610 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
611 invertTex
= !invertTex
;
615 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
617 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
619 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
623 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
624 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
625 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
626 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
627 const float maxXcoord
= normalized
?
628 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
629 const float maxYcoord
= normalized
630 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
631 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
632 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
633 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
635 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
636 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
637 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
642 cso_restore_state(cso
);
647 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
648 * can't use a fragment shader to write stencil values.
651 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
652 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
653 const struct gl_pixelstore_attrib
*unpack
,
654 const GLvoid
*pixels
)
656 struct st_context
*st
= st_context(ctx
);
657 struct pipe_context
*pipe
= st
->pipe
;
658 struct st_renderbuffer
*strb
;
659 enum pipe_transfer_usage usage
;
660 struct pipe_transfer
*pt
;
661 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
663 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
668 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
670 /* totally clipped */
675 strb
= st_renderbuffer(ctx
->DrawBuffer
->
676 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
678 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
679 y
= ctx
->DrawBuffer
->Height
- y
- height
;
682 if (format
== GL_STENCIL_INDEX
&&
683 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
684 /* writing stencil to a combined depth+stencil buffer */
685 usage
= PIPE_TRANSFER_READ_WRITE
;
688 usage
= PIPE_TRANSFER_WRITE
;
691 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
692 strb
->surface
->u
.tex
.level
,
693 strb
->surface
->u
.tex
.first_layer
,
697 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
700 sValues
= malloc(width
* sizeof(GLubyte
));
701 zValues
= malloc(width
* sizeof(GLuint
));
703 if (sValues
&& zValues
) {
705 for (row
= 0; row
< height
; row
++) {
706 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
707 GLenum destType
= GL_UNSIGNED_BYTE
;
708 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
712 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
713 type
, source
, &clippedUnpack
,
714 ctx
->_ImageTransferState
);
716 if (format
== GL_DEPTH_STENCIL
) {
718 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
719 GL_FLOAT
: GL_UNSIGNED_INT
;
721 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
722 (1 << 24) - 1, type
, source
,
727 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
728 "zoom not complete");
734 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
735 spanY
= height
- row
- 1;
741 /* now pack the stencil (and Z) values in the dest format */
742 switch (pt
->resource
->format
) {
743 case PIPE_FORMAT_S8_UINT
:
745 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
746 assert(usage
== PIPE_TRANSFER_WRITE
);
747 memcpy(dest
, sValues
, width
);
750 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
751 if (format
== GL_DEPTH_STENCIL
) {
752 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
754 assert(usage
== PIPE_TRANSFER_WRITE
);
755 for (k
= 0; k
< width
; k
++) {
756 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
760 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
762 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
763 for (k
= 0; k
< width
; k
++) {
764 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
768 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
769 if (format
== GL_DEPTH_STENCIL
) {
770 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
772 assert(usage
== PIPE_TRANSFER_WRITE
);
773 for (k
= 0; k
< width
; k
++) {
774 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
778 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
780 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
781 for (k
= 0; k
< width
; k
++) {
782 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
786 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
787 if (format
== GL_DEPTH_STENCIL
) {
788 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
789 GLfloat
*destf
= (GLfloat
*)dest
;
791 assert(usage
== PIPE_TRANSFER_WRITE
);
792 for (k
= 0; k
< width
; k
++) {
793 destf
[k
*2] = zValuesFloat
[k
];
794 dest
[k
*2+1] = sValues
[k
] & 0xff;
798 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
800 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
801 for (k
= 0; k
< width
; k
++) {
802 dest
[k
*2+1] = sValues
[k
] & 0xff;
813 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
819 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
821 /* unmap the stencil buffer */
822 pipe_transfer_unmap(pipe
, pt
);
827 * Get fragment program variant for a glDrawPixels or glCopyPixels
828 * command for RGBA data.
830 static struct st_fp_variant
*
831 get_color_fp_variant(struct st_context
*st
)
833 struct gl_context
*ctx
= st
->ctx
;
834 struct st_fp_variant_key key
;
835 struct st_fp_variant
*fpv
;
837 memset(&key
, 0, sizeof(key
));
839 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
841 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
842 ctx
->Pixel
.RedScale
!= 1.0 ||
843 ctx
->Pixel
.GreenBias
!= 0.0 ||
844 ctx
->Pixel
.GreenScale
!= 1.0 ||
845 ctx
->Pixel
.BlueBias
!= 0.0 ||
846 ctx
->Pixel
.BlueScale
!= 1.0 ||
847 ctx
->Pixel
.AlphaBias
!= 0.0 ||
848 ctx
->Pixel
.AlphaScale
!= 1.0);
849 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
850 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
851 ctx
->Color
._ClampFragmentColor
;
853 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
860 * Clamp glDrawPixels width and height to the maximum texture size.
863 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
864 struct gl_pixelstore_attrib
*unpack
)
867 1 << (pipe
->screen
->get_param(pipe
->screen
,
868 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
870 if (*width
> maxSize
) {
871 if (unpack
->RowLength
== 0)
872 unpack
->RowLength
= *width
;
875 if (*height
> maxSize
) {
882 * Search the array of 4 swizzle components for the named component and return
886 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
889 for (i
= 0; i
< 4; i
++) {
890 if (swizzle
[i
] == component
)
893 assert(!"search_swizzle() failed");
899 * Set the sampler view's swizzle terms. This is used to handle RGBA
900 * swizzling when the incoming image format isn't an exact match for
901 * the actual texture format. For example, if we have glDrawPixels(
902 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
903 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
904 * avoid swizzling all the pixels in software in the texstore code.
907 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
909 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
910 const struct util_format_description
*desc
=
911 util_format_description(sv
->texture
->format
);
912 unsigned c0
, c1
, c2
, c3
;
914 /* Every gallium driver supports at least one 32-bit packed RGBA format.
915 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
917 assert(desc
->block
.bits
== 32);
919 /* invert the format's swizzle to setup the sampler's swizzle */
920 if (format
== GL_RGBA
) {
921 c0
= UTIL_FORMAT_SWIZZLE_X
;
922 c1
= UTIL_FORMAT_SWIZZLE_Y
;
923 c2
= UTIL_FORMAT_SWIZZLE_Z
;
924 c3
= UTIL_FORMAT_SWIZZLE_W
;
927 assert(format
== GL_BGRA
);
928 c0
= UTIL_FORMAT_SWIZZLE_Z
;
929 c1
= UTIL_FORMAT_SWIZZLE_Y
;
930 c2
= UTIL_FORMAT_SWIZZLE_X
;
931 c3
= UTIL_FORMAT_SWIZZLE_W
;
933 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
934 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
935 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
936 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
939 /* use the default sampler swizzle */
945 * Called via ctx->Driver.DrawPixels()
948 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
949 GLsizei width
, GLsizei height
,
950 GLenum format
, GLenum type
,
951 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
953 void *driver_vp
, *driver_fp
;
954 struct st_context
*st
= st_context(ctx
);
955 struct pipe_context
*pipe
= st
->pipe
;
956 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
957 struct pipe_sampler_view
*sv
[2] = { NULL
};
958 int num_sampler_view
= 1;
959 struct gl_pixelstore_attrib clippedUnpack
;
960 struct st_fp_variant
*fpv
= NULL
;
961 struct pipe_resource
*pt
;
963 /* Mesa state should be up to date by now */
964 assert(ctx
->NewState
== 0x0);
966 st_flush_bitmap_cache(st
);
968 st_validate_state(st
, ST_PIPELINE_RENDER
);
970 /* Limit the size of the glDrawPixels to the max texture size.
971 * Strictly speaking, that's not correct but since we don't handle
972 * larger images yet, this is better than crashing.
974 clippedUnpack
= *unpack
;
975 unpack
= &clippedUnpack
;
976 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
978 if (format
== GL_DEPTH_STENCIL
)
979 write_stencil
= write_depth
= GL_TRUE
;
980 else if (format
== GL_STENCIL_INDEX
)
981 write_stencil
= GL_TRUE
;
982 else if (format
== GL_DEPTH_COMPONENT
)
983 write_depth
= GL_TRUE
;
986 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
987 /* software fallback */
988 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
994 * Get vertex/fragment shaders
996 if (write_depth
|| write_stencil
) {
997 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
999 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1002 fpv
= get_color_fp_variant(st
);
1004 driver_fp
= fpv
->driver_shader
;
1005 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1007 if (ctx
->Pixel
.MapColorFlag
) {
1008 pipe_sampler_view_reference(&sv
[1],
1009 st
->pixel_xfer
.pixelmap_sampler_view
);
1013 /* compiling a new fragment shader variant added new state constants
1014 * into the constant buffer, we need to update them
1016 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1017 PIPE_SHADER_FRAGMENT
);
1020 /* Put glDrawPixels image into a texture */
1021 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1023 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1027 /* create sampler view for the image */
1028 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1030 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1031 pipe_resource_reference(&pt
, NULL
);
1035 /* Set up the sampler view's swizzle */
1036 setup_sampler_swizzle(sv
[0], format
, type
);
1038 /* Create a second sampler view to read stencil. The stencil is
1039 * written using the shader stencil export functionality.
1041 if (write_stencil
) {
1042 enum pipe_format stencil_format
=
1043 util_format_stencil_only(pt
->format
);
1044 /* we should not be doing pixel map/transfer (see above) */
1045 assert(num_sampler_view
== 1);
1046 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1049 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1050 pipe_resource_reference(&pt
, NULL
);
1051 pipe_sampler_view_reference(&sv
[0], NULL
);
1057 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1059 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1064 ctx
->Current
.RasterColor
,
1065 GL_FALSE
, write_depth
, write_stencil
);
1066 pipe_sampler_view_reference(&sv
[0], NULL
);
1067 if (num_sampler_view
> 1)
1068 pipe_sampler_view_reference(&sv
[1], NULL
);
1070 pipe_resource_reference(&pt
, NULL
);
1076 * Software fallback for glCopyPixels(GL_STENCIL).
1079 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1080 GLsizei width
, GLsizei height
,
1081 GLint dstx
, GLint dsty
)
1083 struct st_renderbuffer
*rbDraw
;
1084 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1085 enum pipe_transfer_usage usage
;
1086 struct pipe_transfer
*ptDraw
;
1091 buffer
= malloc(width
* height
* sizeof(ubyte
));
1093 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1097 /* Get the dest renderbuffer */
1098 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1099 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1101 /* this will do stencil pixel transfer ops */
1102 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1103 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1104 &ctx
->DefaultPacking
, buffer
);
1107 /* debug code: dump stencil values */
1109 for (row
= 0; row
< height
; row
++) {
1110 printf("%3d: ", row
);
1111 for (col
= 0; col
< width
; col
++) {
1112 printf("%02x ", buffer
[col
+ row
* width
]);
1118 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1119 usage
= PIPE_TRANSFER_READ_WRITE
;
1121 usage
= PIPE_TRANSFER_WRITE
;
1123 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1124 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1127 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1128 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1130 /* map the stencil buffer */
1131 drawMap
= pipe_transfer_map(pipe
,
1133 rbDraw
->surface
->u
.tex
.level
,
1134 rbDraw
->surface
->u
.tex
.first_layer
,
1136 width
, height
, &ptDraw
);
1139 /* XXX PixelZoom not handled yet */
1140 for (i
= 0; i
< height
; i
++) {
1147 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1151 dst
= drawMap
+ y
* ptDraw
->stride
;
1152 src
= buffer
+ i
* width
;
1154 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1159 /* unmap the stencil buffer */
1160 pipe_transfer_unmap(pipe
, ptDraw
);
1165 * Return renderbuffer to use for reading color pixels for glCopyPixels
1167 static struct st_renderbuffer
*
1168 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1170 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1171 struct st_renderbuffer
*strb
=
1172 st_renderbuffer(fb
->_ColorReadBuffer
);
1179 * Try to do a glCopyPixels for simple cases with a blit by calling
1182 * We can do this when we're copying color pixels (depth/stencil
1183 * eventually) with no pixel zoom, no pixel transfer ops, no
1184 * per-fragment ops, and the src/dest regions don't overlap.
1187 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1188 GLsizei width
, GLsizei height
,
1189 GLint dstx
, GLint dsty
, GLenum type
)
1191 struct st_context
*st
= st_context(ctx
);
1192 struct pipe_context
*pipe
= st
->pipe
;
1193 struct pipe_screen
*screen
= pipe
->screen
;
1194 struct gl_pixelstore_attrib pack
, unpack
;
1195 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1197 if (type
== GL_COLOR
&&
1198 ctx
->Pixel
.ZoomX
== 1.0 &&
1199 ctx
->Pixel
.ZoomY
== 1.0 &&
1200 ctx
->_ImageTransferState
== 0x0 &&
1201 !ctx
->Color
.BlendEnabled
&&
1202 !ctx
->Color
.AlphaEnabled
&&
1203 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1205 !ctx
->Fog
.Enabled
&&
1206 !ctx
->Stencil
.Enabled
&&
1207 !ctx
->FragmentProgram
.Enabled
&&
1208 !ctx
->VertexProgram
.Enabled
&&
1209 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1210 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1211 !ctx
->Query
.CondRenderQuery
&&
1212 !ctx
->Query
.CurrentOcclusionObject
) {
1213 struct st_renderbuffer
*rbRead
, *rbDraw
;
1216 * Clip the read region against the src buffer bounds.
1217 * We'll still allocate a temporary buffer/texture for the original
1218 * src region size but we'll only read the region which is on-screen.
1219 * This may mean that we draw garbage pixels into the dest region, but
1226 pack
= ctx
->DefaultPacking
;
1227 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1228 return GL_TRUE
; /* all done */
1230 /* clip against dest buffer bounds and scissor box */
1231 drawX
= dstx
+ pack
.SkipPixels
;
1232 drawY
= dsty
+ pack
.SkipRows
;
1234 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1235 return GL_TRUE
; /* all done */
1237 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1238 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1243 rbRead
= st_get_color_read_renderbuffer(ctx
);
1244 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1246 /* Flip src/dst position depending on the orientation of buffers. */
1247 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1248 readY
= rbRead
->Base
.Height
- readY
;
1252 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1253 /* We can't flip the destination for pipe->blit, so we only adjust
1254 * its position and flip the source.
1256 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1261 if (rbRead
!= rbDraw
||
1262 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1263 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1264 struct pipe_blit_info blit
;
1266 memset(&blit
, 0, sizeof(blit
));
1267 blit
.src
.resource
= rbRead
->texture
;
1268 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1269 blit
.src
.format
= rbRead
->texture
->format
;
1270 blit
.src
.box
.x
= readX
;
1271 blit
.src
.box
.y
= readY
;
1272 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1273 blit
.src
.box
.width
= readW
;
1274 blit
.src
.box
.height
= readH
;
1275 blit
.src
.box
.depth
= 1;
1276 blit
.dst
.resource
= rbDraw
->texture
;
1277 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1278 blit
.dst
.format
= rbDraw
->texture
->format
;
1279 blit
.dst
.box
.x
= drawX
;
1280 blit
.dst
.box
.y
= drawY
;
1281 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1282 blit
.dst
.box
.width
= drawW
;
1283 blit
.dst
.box
.height
= drawH
;
1284 blit
.dst
.box
.depth
= 1;
1285 blit
.mask
= PIPE_MASK_RGBA
;
1286 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1288 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1289 blit
.src
.resource
->target
,
1290 blit
.src
.resource
->nr_samples
,
1291 PIPE_BIND_SAMPLER_VIEW
) &&
1292 screen
->is_format_supported(screen
, blit
.dst
.format
,
1293 blit
.dst
.resource
->target
,
1294 blit
.dst
.resource
->nr_samples
,
1295 PIPE_BIND_RENDER_TARGET
)) {
1296 pipe
->blit(pipe
, &blit
);
1307 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1308 GLsizei width
, GLsizei height
,
1309 GLint dstx
, GLint dsty
, GLenum type
)
1311 struct st_context
*st
= st_context(ctx
);
1312 struct pipe_context
*pipe
= st
->pipe
;
1313 struct pipe_screen
*screen
= pipe
->screen
;
1314 struct st_renderbuffer
*rbRead
;
1315 void *driver_vp
, *driver_fp
;
1316 struct pipe_resource
*pt
;
1317 struct pipe_sampler_view
*sv
[2] = { NULL
};
1318 struct st_fp_variant
*fpv
= NULL
;
1319 int num_sampler_view
= 1;
1320 enum pipe_format srcFormat
;
1322 GLboolean invertTex
= GL_FALSE
;
1323 GLint readX
, readY
, readW
, readH
;
1324 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1326 st_flush_bitmap_cache(st
);
1328 st_validate_state(st
, ST_PIPELINE_RENDER
);
1330 if (type
== GL_DEPTH_STENCIL
) {
1331 /* XXX make this more efficient */
1332 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1333 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1337 if (type
== GL_STENCIL
) {
1338 /* can't use texturing to do stencil */
1339 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1343 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1347 * The subsequent code implements glCopyPixels by copying the source
1348 * pixels into a temporary texture that's then applied to a textured quad.
1349 * When we draw the textured quad, all the usual per-fragment operations
1355 * Get vertex/fragment shaders
1357 if (type
== GL_COLOR
) {
1358 fpv
= get_color_fp_variant(st
);
1360 rbRead
= st_get_color_read_renderbuffer(ctx
);
1362 driver_fp
= fpv
->driver_shader
;
1363 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1365 if (ctx
->Pixel
.MapColorFlag
) {
1366 pipe_sampler_view_reference(&sv
[1],
1367 st
->pixel_xfer
.pixelmap_sampler_view
);
1371 /* compiling a new fragment shader variant added new state constants
1372 * into the constant buffer, we need to update them
1374 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1375 PIPE_SHADER_FRAGMENT
);
1378 assert(type
== GL_DEPTH
);
1379 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1380 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1382 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1383 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1386 /* Choose the format for the temporary texture. */
1387 srcFormat
= rbRead
->texture
->format
;
1388 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1389 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1391 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1393 /* srcFormat is non-renderable. Find a compatible renderable format. */
1394 if (type
== GL_DEPTH
) {
1395 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1396 GL_NONE
, st
->internal_target
, 0,
1400 assert(type
== GL_COLOR
);
1402 if (util_format_is_float(srcFormat
)) {
1403 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1404 GL_NONE
, st
->internal_target
, 0,
1407 else if (util_format_is_pure_sint(srcFormat
)) {
1408 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1409 GL_NONE
, st
->internal_target
, 0,
1412 else if (util_format_is_pure_uint(srcFormat
)) {
1413 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1414 GL_NONE
, st
->internal_target
, 0,
1417 else if (util_format_is_snorm(srcFormat
)) {
1418 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1419 GL_NONE
, st
->internal_target
, 0,
1423 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1424 GL_NONE
, st
->internal_target
, 0,
1429 if (srcFormat
== PIPE_FORMAT_NONE
) {
1430 assert(0 && "cannot choose a format for src of CopyPixels");
1435 /* Invert src region if needed */
1436 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1437 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1438 invertTex
= !invertTex
;
1441 /* Clip the read region against the src buffer bounds.
1442 * We'll still allocate a temporary buffer/texture for the original
1443 * src region size but we'll only read the region which is on-screen.
1444 * This may mean that we draw garbage pixels into the dest region, but
1451 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1452 /* The source region is completely out of bounds. Do nothing.
1453 * The GL spec says "Results of copies from outside the window,
1454 * or from regions of the window that are not exposed, are
1455 * hardware dependent and undefined."
1460 readW
= MAX2(0, readW
);
1461 readH
= MAX2(0, readH
);
1463 /* Allocate the temporary texture. */
1464 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1468 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1470 pipe_resource_reference(&pt
, NULL
);
1474 /* Copy the src region to the temporary texture. */
1476 struct pipe_blit_info blit
;
1478 memset(&blit
, 0, sizeof(blit
));
1479 blit
.src
.resource
= rbRead
->texture
;
1480 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1481 blit
.src
.format
= rbRead
->texture
->format
;
1482 blit
.src
.box
.x
= readX
;
1483 blit
.src
.box
.y
= readY
;
1484 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1485 blit
.src
.box
.width
= readW
;
1486 blit
.src
.box
.height
= readH
;
1487 blit
.src
.box
.depth
= 1;
1488 blit
.dst
.resource
= pt
;
1490 blit
.dst
.format
= pt
->format
;
1491 blit
.dst
.box
.x
= pack
.SkipPixels
;
1492 blit
.dst
.box
.y
= pack
.SkipRows
;
1494 blit
.dst
.box
.width
= readW
;
1495 blit
.dst
.box
.height
= readH
;
1496 blit
.dst
.box
.depth
= 1;
1497 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1498 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1500 pipe
->blit(pipe
, &blit
);
1503 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1504 * textured quad with that texture.
1506 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1507 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1512 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1513 invertTex
, GL_FALSE
, GL_FALSE
);
1515 pipe_resource_reference(&pt
, NULL
);
1516 pipe_sampler_view_reference(&sv
[0], NULL
);
1521 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1523 functions
->DrawPixels
= st_DrawPixels
;
1524 functions
->CopyPixels
= st_CopyPixels
;
1529 st_destroy_drawpix(struct st_context
*st
)
1533 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1534 if (st
->drawpix
.zs_shaders
[i
])
1535 cso_delete_fragment_shader(st
->cso_context
,
1536 st
->drawpix
.zs_shaders
[i
]);
1539 if (st
->drawpix
.vert_shaders
[0])
1540 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1541 if (st
->drawpix
.vert_shaders
[1])
1542 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);