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_FRAGMENT_SHADER
|
483 CSO_BIT_STREAM_OUTPUTS
|
484 CSO_BIT_VERTEX_SHADER
|
485 CSO_BIT_TESSCTRL_SHADER
|
486 CSO_BIT_TESSEVAL_SHADER
|
487 CSO_BIT_GEOMETRY_SHADER
|
488 CSO_BIT_VERTEX_ELEMENTS
|
489 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
);
491 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
494 cso_save_state(cso
, cso_state_mask
);
496 /* rasterizer state: just scissor */
498 struct pipe_rasterizer_state rasterizer
;
499 memset(&rasterizer
, 0, sizeof(rasterizer
));
500 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
501 ctx
->Color
._ClampFragmentColor
;
502 rasterizer
.half_pixel_center
= 1;
503 rasterizer
.bottom_edge_rule
= 1;
504 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
505 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
506 cso_set_rasterizer(cso
, &rasterizer
);
510 /* Stencil writing bypasses the normal fragment pipeline to
511 * disable color writing and set stencil test to always pass.
513 struct pipe_depth_stencil_alpha_state dsa
;
514 struct pipe_blend_state blend
;
517 memset(&dsa
, 0, sizeof(dsa
));
518 dsa
.stencil
[0].enabled
= 1;
519 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
520 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
521 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
523 /* writing depth+stencil: depth test always passes */
524 dsa
.depth
.enabled
= 1;
525 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
526 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
528 cso_set_depth_stencil_alpha(cso
, &dsa
);
530 /* blend (colormask) */
531 memset(&blend
, 0, sizeof(blend
));
532 cso_set_blend(cso
, &blend
);
535 /* fragment shader state: TEX lookup program */
536 cso_set_fragment_shader_handle(cso
, driver_fp
);
538 /* vertex shader state: position + texcoord pass-through */
539 cso_set_vertex_shader_handle(cso
, driver_vp
);
541 /* disable other shaders */
542 cso_set_tessctrl_shader_handle(cso
, NULL
);
543 cso_set_tesseval_shader_handle(cso
, NULL
);
544 cso_set_geometry_shader_handle(cso
, NULL
);
546 /* user samplers, plus the drawpix samplers */
548 struct pipe_sampler_state sampler
;
550 memset(&sampler
, 0, sizeof(sampler
));
551 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
552 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
553 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
554 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
555 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
556 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
557 sampler
.normalized_coords
= normalized
;
560 /* drawing a color image */
561 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
562 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
563 fpv
->pixelmap_sampler
+ 1,
564 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
567 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
568 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
570 samplers
[fpv
->drawpix_sampler
] = &sampler
;
572 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
574 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
576 /* drawing a depth/stencil image */
577 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
579 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
583 /* user textures, plus the drawpix textures */
585 /* drawing a color image */
586 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
587 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
588 fpv
->pixelmap_sampler
+ 1,
589 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
591 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
592 sizeof(sampler_views
));
594 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
596 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
597 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
599 /* drawing a depth/stencil image */
600 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
603 /* viewport state: viewport matching window dims */
604 cso_set_viewport_dims(cso
, fb_width
, fb_height
, TRUE
);
606 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
607 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
609 /* Compute Gallium window coords (y=0=top) with pixel zoom.
610 * Recall that these coords are transformed by the current
611 * vertex shader and viewport transformation.
613 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
614 y
= fb_height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
615 invertTex
= !invertTex
;
619 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
621 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
623 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
627 const float clip_x0
= x0
/ (float) fb_width
* 2.0f
- 1.0f
;
628 const float clip_y0
= y0
/ (float) fb_height
* 2.0f
- 1.0f
;
629 const float clip_x1
= x1
/ (float) fb_width
* 2.0f
- 1.0f
;
630 const float clip_y1
= y1
/ (float) fb_height
* 2.0f
- 1.0f
;
631 const float maxXcoord
= normalized
?
632 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
633 const float maxYcoord
= normalized
634 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
635 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
636 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
637 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
639 if (!st_draw_quad(ctx
->st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
640 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
641 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
646 cso_restore_state(cso
);
651 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
652 * can't use a fragment shader to write stencil values.
655 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
656 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
657 const struct gl_pixelstore_attrib
*unpack
,
658 const GLvoid
*pixels
)
660 struct st_context
*st
= st_context(ctx
);
661 struct pipe_context
*pipe
= st
->pipe
;
662 struct st_renderbuffer
*strb
;
663 enum pipe_transfer_usage usage
;
664 struct pipe_transfer
*pt
;
665 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
667 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
672 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
674 /* totally clipped */
679 strb
= st_renderbuffer(ctx
->DrawBuffer
->
680 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
682 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
683 y
= ctx
->DrawBuffer
->Height
- y
- height
;
686 if (format
== GL_STENCIL_INDEX
&&
687 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
688 /* writing stencil to a combined depth+stencil buffer */
689 usage
= PIPE_TRANSFER_READ_WRITE
;
692 usage
= PIPE_TRANSFER_WRITE
;
695 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
696 strb
->surface
->u
.tex
.level
,
697 strb
->surface
->u
.tex
.first_layer
,
701 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
704 sValues
= malloc(width
* sizeof(GLubyte
));
705 zValues
= malloc(width
* sizeof(GLuint
));
707 if (sValues
&& zValues
) {
709 for (row
= 0; row
< height
; row
++) {
710 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
711 GLenum destType
= GL_UNSIGNED_BYTE
;
712 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
716 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
717 type
, source
, &clippedUnpack
,
718 ctx
->_ImageTransferState
);
720 if (format
== GL_DEPTH_STENCIL
) {
722 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
723 GL_FLOAT
: GL_UNSIGNED_INT
;
725 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
726 (1 << 24) - 1, type
, source
,
731 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
732 "zoom not complete");
738 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
739 spanY
= height
- row
- 1;
745 /* now pack the stencil (and Z) values in the dest format */
746 switch (pt
->resource
->format
) {
747 case PIPE_FORMAT_S8_UINT
:
749 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
750 assert(usage
== PIPE_TRANSFER_WRITE
);
751 memcpy(dest
, sValues
, width
);
754 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
755 if (format
== GL_DEPTH_STENCIL
) {
756 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
758 assert(usage
== PIPE_TRANSFER_WRITE
);
759 for (k
= 0; k
< width
; k
++) {
760 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
764 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
766 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
767 for (k
= 0; k
< width
; k
++) {
768 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
772 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
773 if (format
== GL_DEPTH_STENCIL
) {
774 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
776 assert(usage
== PIPE_TRANSFER_WRITE
);
777 for (k
= 0; k
< width
; k
++) {
778 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
782 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
784 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
785 for (k
= 0; k
< width
; k
++) {
786 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
790 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
791 if (format
== GL_DEPTH_STENCIL
) {
792 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
793 GLfloat
*destf
= (GLfloat
*)dest
;
795 assert(usage
== PIPE_TRANSFER_WRITE
);
796 for (k
= 0; k
< width
; k
++) {
797 destf
[k
*2] = zValuesFloat
[k
];
798 dest
[k
*2+1] = sValues
[k
] & 0xff;
802 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
804 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
805 for (k
= 0; k
< width
; k
++) {
806 dest
[k
*2+1] = sValues
[k
] & 0xff;
817 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
823 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
825 /* unmap the stencil buffer */
826 pipe_transfer_unmap(pipe
, pt
);
831 * Get fragment program variant for a glDrawPixels or glCopyPixels
832 * command for RGBA data.
834 static struct st_fp_variant
*
835 get_color_fp_variant(struct st_context
*st
)
837 struct gl_context
*ctx
= st
->ctx
;
838 struct st_fp_variant_key key
;
839 struct st_fp_variant
*fpv
;
841 memset(&key
, 0, sizeof(key
));
843 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
845 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
846 ctx
->Pixel
.RedScale
!= 1.0 ||
847 ctx
->Pixel
.GreenBias
!= 0.0 ||
848 ctx
->Pixel
.GreenScale
!= 1.0 ||
849 ctx
->Pixel
.BlueBias
!= 0.0 ||
850 ctx
->Pixel
.BlueScale
!= 1.0 ||
851 ctx
->Pixel
.AlphaBias
!= 0.0 ||
852 ctx
->Pixel
.AlphaScale
!= 1.0);
853 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
854 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
855 st
->ctx
->Color
._ClampFragmentColor
;
857 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
864 * Clamp glDrawPixels width and height to the maximum texture size.
867 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
868 struct gl_pixelstore_attrib
*unpack
)
871 1 << (pipe
->screen
->get_param(pipe
->screen
,
872 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
874 if (*width
> maxSize
) {
875 if (unpack
->RowLength
== 0)
876 unpack
->RowLength
= *width
;
879 if (*height
> maxSize
) {
886 * Search the array of 4 swizzle components for the named component and return
890 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
893 for (i
= 0; i
< 4; i
++) {
894 if (swizzle
[i
] == component
)
897 assert(!"search_swizzle() failed");
903 * Set the sampler view's swizzle terms. This is used to handle RGBA
904 * swizzling when the incoming image format isn't an exact match for
905 * the actual texture format. For example, if we have glDrawPixels(
906 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
907 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
908 * avoid swizzling all the pixels in software in the texstore code.
911 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
913 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
914 const struct util_format_description
*desc
=
915 util_format_description(sv
->texture
->format
);
916 unsigned c0
, c1
, c2
, c3
;
918 /* Every gallium driver supports at least one 32-bit packed RGBA format.
919 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
921 assert(desc
->block
.bits
== 32);
923 /* invert the format's swizzle to setup the sampler's swizzle */
924 if (format
== GL_RGBA
) {
925 c0
= UTIL_FORMAT_SWIZZLE_X
;
926 c1
= UTIL_FORMAT_SWIZZLE_Y
;
927 c2
= UTIL_FORMAT_SWIZZLE_Z
;
928 c3
= UTIL_FORMAT_SWIZZLE_W
;
931 assert(format
== GL_BGRA
);
932 c0
= UTIL_FORMAT_SWIZZLE_Z
;
933 c1
= UTIL_FORMAT_SWIZZLE_Y
;
934 c2
= UTIL_FORMAT_SWIZZLE_X
;
935 c3
= UTIL_FORMAT_SWIZZLE_W
;
937 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
938 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
939 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
940 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
943 /* use the default sampler swizzle */
949 * Called via ctx->Driver.DrawPixels()
952 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
953 GLsizei width
, GLsizei height
,
954 GLenum format
, GLenum type
,
955 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
957 void *driver_vp
, *driver_fp
;
958 struct st_context
*st
= st_context(ctx
);
959 struct pipe_context
*pipe
= st
->pipe
;
960 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
961 struct pipe_sampler_view
*sv
[2] = { NULL
};
962 int num_sampler_view
= 1;
963 struct gl_pixelstore_attrib clippedUnpack
;
964 struct st_fp_variant
*fpv
= NULL
;
965 struct pipe_resource
*pt
;
967 /* Mesa state should be up to date by now */
968 assert(ctx
->NewState
== 0x0);
970 st_flush_bitmap_cache(st
);
972 st_validate_state(st
, ST_PIPELINE_RENDER
);
974 /* Limit the size of the glDrawPixels to the max texture size.
975 * Strictly speaking, that's not correct but since we don't handle
976 * larger images yet, this is better than crashing.
978 clippedUnpack
= *unpack
;
979 unpack
= &clippedUnpack
;
980 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
982 if (format
== GL_DEPTH_STENCIL
)
983 write_stencil
= write_depth
= GL_TRUE
;
984 else if (format
== GL_STENCIL_INDEX
)
985 write_stencil
= GL_TRUE
;
986 else if (format
== GL_DEPTH_COMPONENT
)
987 write_depth
= GL_TRUE
;
990 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
991 /* software fallback */
992 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
998 * Get vertex/fragment shaders
1000 if (write_depth
|| write_stencil
) {
1001 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1003 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1006 fpv
= get_color_fp_variant(st
);
1008 driver_fp
= fpv
->driver_shader
;
1009 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1011 if (ctx
->Pixel
.MapColorFlag
) {
1012 pipe_sampler_view_reference(&sv
[1],
1013 st
->pixel_xfer
.pixelmap_sampler_view
);
1017 /* compiling a new fragment shader variant added new state constants
1018 * into the constant buffer, we need to update them
1020 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1021 PIPE_SHADER_FRAGMENT
);
1024 /* Put glDrawPixels image into a texture */
1025 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1027 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1031 /* create sampler view for the image */
1032 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1034 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1035 pipe_resource_reference(&pt
, NULL
);
1039 /* Set up the sampler view's swizzle */
1040 setup_sampler_swizzle(sv
[0], format
, type
);
1042 /* Create a second sampler view to read stencil. The stencil is
1043 * written using the shader stencil export functionality.
1045 if (write_stencil
) {
1046 enum pipe_format stencil_format
=
1047 util_format_stencil_only(pt
->format
);
1048 /* we should not be doing pixel map/transfer (see above) */
1049 assert(num_sampler_view
== 1);
1050 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1053 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1054 pipe_resource_reference(&pt
, NULL
);
1055 pipe_sampler_view_reference(&sv
[0], NULL
);
1061 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1063 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1068 ctx
->Current
.RasterColor
,
1069 GL_FALSE
, write_depth
, write_stencil
);
1070 pipe_sampler_view_reference(&sv
[0], NULL
);
1071 if (num_sampler_view
> 1)
1072 pipe_sampler_view_reference(&sv
[1], NULL
);
1074 pipe_resource_reference(&pt
, NULL
);
1080 * Software fallback for glCopyPixels(GL_STENCIL).
1083 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1084 GLsizei width
, GLsizei height
,
1085 GLint dstx
, GLint dsty
)
1087 struct st_renderbuffer
*rbDraw
;
1088 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1089 enum pipe_transfer_usage usage
;
1090 struct pipe_transfer
*ptDraw
;
1095 buffer
= malloc(width
* height
* sizeof(ubyte
));
1097 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1101 /* Get the dest renderbuffer */
1102 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1103 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1105 /* this will do stencil pixel transfer ops */
1106 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1107 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1108 &ctx
->DefaultPacking
, buffer
);
1111 /* debug code: dump stencil values */
1113 for (row
= 0; row
< height
; row
++) {
1114 printf("%3d: ", row
);
1115 for (col
= 0; col
< width
; col
++) {
1116 printf("%02x ", buffer
[col
+ row
* width
]);
1122 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1123 usage
= PIPE_TRANSFER_READ_WRITE
;
1125 usage
= PIPE_TRANSFER_WRITE
;
1127 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1128 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1131 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1132 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1134 /* map the stencil buffer */
1135 drawMap
= pipe_transfer_map(pipe
,
1137 rbDraw
->surface
->u
.tex
.level
,
1138 rbDraw
->surface
->u
.tex
.first_layer
,
1140 width
, height
, &ptDraw
);
1143 /* XXX PixelZoom not handled yet */
1144 for (i
= 0; i
< height
; i
++) {
1151 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1155 dst
= drawMap
+ y
* ptDraw
->stride
;
1156 src
= buffer
+ i
* width
;
1158 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1163 /* unmap the stencil buffer */
1164 pipe_transfer_unmap(pipe
, ptDraw
);
1169 * Return renderbuffer to use for reading color pixels for glCopyPixels
1171 static struct st_renderbuffer
*
1172 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1174 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1175 struct st_renderbuffer
*strb
=
1176 st_renderbuffer(fb
->_ColorReadBuffer
);
1183 * Try to do a glCopyPixels for simple cases with a blit by calling
1186 * We can do this when we're copying color pixels (depth/stencil
1187 * eventually) with no pixel zoom, no pixel transfer ops, no
1188 * per-fragment ops, and the src/dest regions don't overlap.
1191 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1192 GLsizei width
, GLsizei height
,
1193 GLint dstx
, GLint dsty
, GLenum type
)
1195 struct st_context
*st
= st_context(ctx
);
1196 struct pipe_context
*pipe
= st
->pipe
;
1197 struct pipe_screen
*screen
= pipe
->screen
;
1198 struct gl_pixelstore_attrib pack
, unpack
;
1199 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1201 if (type
== GL_COLOR
&&
1202 ctx
->Pixel
.ZoomX
== 1.0 &&
1203 ctx
->Pixel
.ZoomY
== 1.0 &&
1204 ctx
->_ImageTransferState
== 0x0 &&
1205 !ctx
->Color
.BlendEnabled
&&
1206 !ctx
->Color
.AlphaEnabled
&&
1207 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1209 !ctx
->Fog
.Enabled
&&
1210 !ctx
->Stencil
.Enabled
&&
1211 !ctx
->FragmentProgram
.Enabled
&&
1212 !ctx
->VertexProgram
.Enabled
&&
1213 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1214 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1215 !ctx
->Query
.CondRenderQuery
&&
1216 !ctx
->Query
.CurrentOcclusionObject
) {
1217 struct st_renderbuffer
*rbRead
, *rbDraw
;
1220 * Clip the read region against the src buffer bounds.
1221 * We'll still allocate a temporary buffer/texture for the original
1222 * src region size but we'll only read the region which is on-screen.
1223 * This may mean that we draw garbage pixels into the dest region, but
1230 pack
= ctx
->DefaultPacking
;
1231 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1232 return GL_TRUE
; /* all done */
1234 /* clip against dest buffer bounds and scissor box */
1235 drawX
= dstx
+ pack
.SkipPixels
;
1236 drawY
= dsty
+ pack
.SkipRows
;
1238 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1239 return GL_TRUE
; /* all done */
1241 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1242 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1247 rbRead
= st_get_color_read_renderbuffer(ctx
);
1248 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1250 /* Flip src/dst position depending on the orientation of buffers. */
1251 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1252 readY
= rbRead
->Base
.Height
- readY
;
1256 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1257 /* We can't flip the destination for pipe->blit, so we only adjust
1258 * its position and flip the source.
1260 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1265 if (rbRead
!= rbDraw
||
1266 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1267 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1268 struct pipe_blit_info blit
;
1270 memset(&blit
, 0, sizeof(blit
));
1271 blit
.src
.resource
= rbRead
->texture
;
1272 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1273 blit
.src
.format
= rbRead
->texture
->format
;
1274 blit
.src
.box
.x
= readX
;
1275 blit
.src
.box
.y
= readY
;
1276 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1277 blit
.src
.box
.width
= readW
;
1278 blit
.src
.box
.height
= readH
;
1279 blit
.src
.box
.depth
= 1;
1280 blit
.dst
.resource
= rbDraw
->texture
;
1281 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1282 blit
.dst
.format
= rbDraw
->texture
->format
;
1283 blit
.dst
.box
.x
= drawX
;
1284 blit
.dst
.box
.y
= drawY
;
1285 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1286 blit
.dst
.box
.width
= drawW
;
1287 blit
.dst
.box
.height
= drawH
;
1288 blit
.dst
.box
.depth
= 1;
1289 blit
.mask
= PIPE_MASK_RGBA
;
1290 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1292 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1293 blit
.src
.resource
->target
,
1294 blit
.src
.resource
->nr_samples
,
1295 PIPE_BIND_SAMPLER_VIEW
) &&
1296 screen
->is_format_supported(screen
, blit
.dst
.format
,
1297 blit
.dst
.resource
->target
,
1298 blit
.dst
.resource
->nr_samples
,
1299 PIPE_BIND_RENDER_TARGET
)) {
1300 pipe
->blit(pipe
, &blit
);
1311 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1312 GLsizei width
, GLsizei height
,
1313 GLint dstx
, GLint dsty
, GLenum type
)
1315 struct st_context
*st
= st_context(ctx
);
1316 struct pipe_context
*pipe
= st
->pipe
;
1317 struct pipe_screen
*screen
= pipe
->screen
;
1318 struct st_renderbuffer
*rbRead
;
1319 void *driver_vp
, *driver_fp
;
1320 struct pipe_resource
*pt
;
1321 struct pipe_sampler_view
*sv
[2] = { NULL
};
1322 struct st_fp_variant
*fpv
= NULL
;
1323 int num_sampler_view
= 1;
1324 enum pipe_format srcFormat
;
1326 GLboolean invertTex
= GL_FALSE
;
1327 GLint readX
, readY
, readW
, readH
;
1328 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1330 st_flush_bitmap_cache(st
);
1332 st_validate_state(st
, ST_PIPELINE_RENDER
);
1334 if (type
== GL_DEPTH_STENCIL
) {
1335 /* XXX make this more efficient */
1336 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1337 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1341 if (type
== GL_STENCIL
) {
1342 /* can't use texturing to do stencil */
1343 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1347 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1351 * The subsequent code implements glCopyPixels by copying the source
1352 * pixels into a temporary texture that's then applied to a textured quad.
1353 * When we draw the textured quad, all the usual per-fragment operations
1359 * Get vertex/fragment shaders
1361 if (type
== GL_COLOR
) {
1362 fpv
= get_color_fp_variant(st
);
1364 rbRead
= st_get_color_read_renderbuffer(ctx
);
1366 driver_fp
= fpv
->driver_shader
;
1367 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1369 if (ctx
->Pixel
.MapColorFlag
) {
1370 pipe_sampler_view_reference(&sv
[1],
1371 st
->pixel_xfer
.pixelmap_sampler_view
);
1375 /* compiling a new fragment shader variant added new state constants
1376 * into the constant buffer, we need to update them
1378 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1379 PIPE_SHADER_FRAGMENT
);
1382 assert(type
== GL_DEPTH
);
1383 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1384 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1386 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1387 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1390 /* Choose the format for the temporary texture. */
1391 srcFormat
= rbRead
->texture
->format
;
1392 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1393 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1395 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1397 /* srcFormat is non-renderable. Find a compatible renderable format. */
1398 if (type
== GL_DEPTH
) {
1399 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1400 GL_NONE
, st
->internal_target
, 0,
1404 assert(type
== GL_COLOR
);
1406 if (util_format_is_float(srcFormat
)) {
1407 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1408 GL_NONE
, st
->internal_target
, 0,
1411 else if (util_format_is_pure_sint(srcFormat
)) {
1412 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1413 GL_NONE
, st
->internal_target
, 0,
1416 else if (util_format_is_pure_uint(srcFormat
)) {
1417 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1418 GL_NONE
, st
->internal_target
, 0,
1421 else if (util_format_is_snorm(srcFormat
)) {
1422 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1423 GL_NONE
, st
->internal_target
, 0,
1427 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1428 GL_NONE
, st
->internal_target
, 0,
1433 if (srcFormat
== PIPE_FORMAT_NONE
) {
1434 assert(0 && "cannot choose a format for src of CopyPixels");
1439 /* Invert src region if needed */
1440 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1441 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1442 invertTex
= !invertTex
;
1445 /* Clip the read region against the src buffer bounds.
1446 * We'll still allocate a temporary buffer/texture for the original
1447 * src region size but we'll only read the region which is on-screen.
1448 * This may mean that we draw garbage pixels into the dest region, but
1455 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1456 /* The source region is completely out of bounds. Do nothing.
1457 * The GL spec says "Results of copies from outside the window,
1458 * or from regions of the window that are not exposed, are
1459 * hardware dependent and undefined."
1464 readW
= MAX2(0, readW
);
1465 readH
= MAX2(0, readH
);
1467 /* Allocate the temporary texture. */
1468 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1472 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1474 pipe_resource_reference(&pt
, NULL
);
1478 /* Copy the src region to the temporary texture. */
1480 struct pipe_blit_info blit
;
1482 memset(&blit
, 0, sizeof(blit
));
1483 blit
.src
.resource
= rbRead
->texture
;
1484 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1485 blit
.src
.format
= rbRead
->texture
->format
;
1486 blit
.src
.box
.x
= readX
;
1487 blit
.src
.box
.y
= readY
;
1488 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1489 blit
.src
.box
.width
= readW
;
1490 blit
.src
.box
.height
= readH
;
1491 blit
.src
.box
.depth
= 1;
1492 blit
.dst
.resource
= pt
;
1494 blit
.dst
.format
= pt
->format
;
1495 blit
.dst
.box
.x
= pack
.SkipPixels
;
1496 blit
.dst
.box
.y
= pack
.SkipRows
;
1498 blit
.dst
.box
.width
= readW
;
1499 blit
.dst
.box
.height
= readH
;
1500 blit
.dst
.box
.depth
= 1;
1501 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1502 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1504 pipe
->blit(pipe
, &blit
);
1507 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1508 * textured quad with that texture.
1510 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1511 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1516 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1517 invertTex
, GL_FALSE
, GL_FALSE
);
1519 pipe_resource_reference(&pt
, NULL
);
1520 pipe_sampler_view_reference(&sv
[0], NULL
);
1525 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1527 functions
->DrawPixels
= st_DrawPixels
;
1528 functions
->CopyPixels
= st_CopyPixels
;
1533 st_destroy_drawpix(struct st_context
*st
)
1537 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1538 if (st
->drawpix
.zs_shaders
[i
])
1539 cso_delete_fragment_shader(st
->cso_context
,
1540 st
->drawpix
.zs_shaders
[i
]);
1543 if (st
->drawpix
.vert_shaders
[0])
1544 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1545 if (st
->drawpix
.vert_shaders
[1])
1546 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);