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/macros.h"
39 #include "main/mtypes.h"
40 #include "main/pack.h"
42 #include "main/readpix.h"
43 #include "main/texformat.h"
44 #include "main/teximage.h"
45 #include "main/texstore.h"
46 #include "main/glformats.h"
47 #include "program/program.h"
48 #include "program/prog_print.h"
49 #include "program/prog_instruction.h"
52 #include "st_atom_constbuf.h"
53 #include "st_cb_bitmap.h"
54 #include "st_cb_drawpixels.h"
55 #include "st_cb_readpixels.h"
56 #include "st_cb_fbo.h"
57 #include "st_context.h"
60 #include "st_format.h"
61 #include "st_program.h"
62 #include "st_texture.h"
64 #include "pipe/p_context.h"
65 #include "pipe/p_defines.h"
66 #include "tgsi/tgsi_ureg.h"
67 #include "util/u_format.h"
68 #include "util/u_inlines.h"
69 #include "util/u_math.h"
70 #include "util/u_tile.h"
71 #include "cso_cache/cso_context.h"
75 * Create fragment program that does a TEX() instruction to get a Z and/or
76 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
77 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
78 * Pass fragment color through as-is.
80 * \return CSO of the fragment shader.
83 get_drawpix_z_stencil_program(struct st_context
*st
,
84 GLboolean write_depth
,
85 GLboolean write_stencil
)
87 struct ureg_program
*ureg
;
88 struct ureg_src depth_sampler
, stencil_sampler
;
89 struct ureg_src texcoord
, color
;
90 struct ureg_dst out_color
, out_depth
, out_stencil
;
91 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
94 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
96 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
97 /* already have the proper shader */
98 return st
->drawpix
.zs_shaders
[shaderIndex
];
101 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
105 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
108 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
109 TGSI_INTERPOLATE_COLOR
);
110 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
112 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
113 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
117 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
118 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
121 texcoord
= ureg_DECL_fs_input(ureg
,
122 st
->needs_texcoord_semantic
?
123 TGSI_SEMANTIC_TEXCOORD
:
124 TGSI_SEMANTIC_GENERIC
,
125 0, TGSI_INTERPOLATE_LINEAR
);
128 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
129 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
130 ureg_MOV(ureg
, out_color
, color
);
134 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
135 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
138 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
140 /* save the new shader */
141 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
147 * Create a simple vertex shader that just passes through the
148 * vertex position and texcoord (and optionally, color).
151 make_passthrough_vertex_shader(struct st_context
*st
,
154 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
155 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
157 if (!st
->drawpix
.vert_shaders
[passColor
]) {
158 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
163 /* MOV result.pos, vertex.pos; */
165 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
166 ureg_DECL_vs_input( ureg
, 0 ));
169 /* MOV result.color0, vertex.attr[1]; */
171 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
172 ureg_DECL_vs_input( ureg
, 1 ));
175 /* MOV result.texcoord0, vertex.attr[2]; */
177 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
178 ureg_DECL_vs_input( ureg
, 2 ));
182 st
->drawpix
.vert_shaders
[passColor
] =
183 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
186 return st
->drawpix
.vert_shaders
[passColor
];
191 * Return a texture internalFormat for drawing/copying an image
192 * of the given format and type.
195 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
198 case GL_DEPTH_COMPONENT
:
200 case GL_UNSIGNED_SHORT
:
201 return GL_DEPTH_COMPONENT16
;
203 case GL_UNSIGNED_INT
:
204 return GL_DEPTH_COMPONENT32
;
207 if (ctx
->Extensions
.ARB_depth_buffer_float
)
208 return GL_DEPTH_COMPONENT32F
;
210 return GL_DEPTH_COMPONENT
;
213 return GL_DEPTH_COMPONENT
;
216 case GL_DEPTH_STENCIL
:
218 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
219 return GL_DEPTH32F_STENCIL8
;
221 case GL_UNSIGNED_INT_24_8
:
223 return GL_DEPTH24_STENCIL8
;
226 case GL_STENCIL_INDEX
:
227 return GL_STENCIL_INDEX
;
230 if (_mesa_is_enum_format_integer(format
)) {
234 case GL_UNSIGNED_BYTE
:
238 case GL_UNSIGNED_SHORT
:
242 case GL_UNSIGNED_INT
:
245 assert(0 && "Unexpected type in internal_format()");
246 return GL_RGBA_INTEGER
;
251 case GL_UNSIGNED_BYTE
:
252 case GL_UNSIGNED_INT_8_8_8_8
:
253 case GL_UNSIGNED_INT_8_8_8_8_REV
:
257 case GL_UNSIGNED_BYTE_3_3_2
:
258 case GL_UNSIGNED_BYTE_2_3_3_REV
:
261 case GL_UNSIGNED_SHORT_4_4_4_4
:
262 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
265 case GL_UNSIGNED_SHORT_5_6_5
:
266 case GL_UNSIGNED_SHORT_5_6_5_REV
:
269 case GL_UNSIGNED_SHORT_5_5_5_1
:
270 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
273 case GL_UNSIGNED_INT_10_10_10_2
:
274 case GL_UNSIGNED_INT_2_10_10_10_REV
:
277 case GL_UNSIGNED_SHORT
:
278 case GL_UNSIGNED_INT
:
283 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
288 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
290 case GL_HALF_FLOAT_ARB
:
292 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
293 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
298 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
299 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
301 case GL_UNSIGNED_INT_5_9_9_9_REV
:
302 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
305 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
306 assert(ctx
->Extensions
.EXT_packed_float
);
307 return GL_R11F_G11F_B10F
;
315 * Create a temporary texture to hold an image of the given size.
316 * If width, height are not POT and the driver only handles POT textures,
317 * allocate the next larger size of texture that is POT.
319 static struct pipe_resource
*
320 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
321 enum pipe_format texFormat
, unsigned bind
)
323 struct pipe_resource
*pt
;
325 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
326 width
, height
, 1, 1, 0, bind
);
333 * Make texture containing an image for glDrawPixels image.
334 * If 'pixels' is NULL, leave the texture image data undefined.
336 static struct pipe_resource
*
337 make_texture(struct st_context
*st
,
338 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
339 const struct gl_pixelstore_attrib
*unpack
,
340 const GLvoid
*pixels
)
342 struct gl_context
*ctx
= st
->ctx
;
343 struct pipe_context
*pipe
= st
->pipe
;
345 struct pipe_resource
*pt
;
346 enum pipe_format pipeFormat
;
347 GLenum baseInternalFormat
;
349 /* Choose a pixel format for the temp texture which will hold the
352 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
353 format
, type
, unpack
->SwapBytes
);
355 if (pipeFormat
== PIPE_FORMAT_NONE
) {
356 /* Use the generic approach. */
357 GLenum intFormat
= internal_format(ctx
, format
, type
);
359 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
360 st
->internal_target
, 0,
361 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
362 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
365 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
366 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
368 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
372 /* alloc temporary texture */
373 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
375 _mesa_unmap_pbo_source(ctx
, unpack
);
380 struct pipe_transfer
*transfer
;
383 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
385 /* we'll do pixel transfer in a fragment shader */
386 ctx
->_ImageTransferState
= 0x0;
388 /* map texture transfer */
389 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
390 PIPE_TRANSFER_WRITE
, 0, 0,
391 width
, height
, &transfer
);
394 /* Put image into texture transfer.
395 * Note that the image is actually going to be upside down in
396 * the texture. We deal with that with texcoords.
398 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
399 && type
== GL_UNSIGNED_BYTE
) {
400 /* Use a memcpy-based texstore to avoid software pixel swizzling.
401 * We'll do the necessary swizzling with the pipe_sampler_view to
402 * give much better performance.
403 * XXX in the future, expand this to accomodate more format and
406 _mesa_memcpy_texture(ctx
, 2,
407 mformat
, /* mesa_format */
408 transfer
->stride
, /* dstRowStride, bytes */
409 &dest
, /* destSlices */
410 width
, height
, 1, /* size */
411 format
, type
, /* src format/type */
412 pixels
, /* data source */
417 success
= _mesa_texstore(ctx
, 2, /* dims */
418 baseInternalFormat
, /* baseInternalFormat */
419 mformat
, /* mesa_format */
420 transfer
->stride
, /* dstRowStride, bytes */
421 &dest
, /* destSlices */
422 width
, height
, 1, /* size */
423 format
, type
, /* src format/type */
424 pixels
, /* data source */
429 pipe_transfer_unmap(pipe
, transfer
);
434 ctx
->_ImageTransferState
= imageTransferStateSave
;
437 _mesa_unmap_pbo_source(ctx
, unpack
);
444 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
445 GLsizei width
, GLsizei height
,
446 GLfloat zoomX
, GLfloat zoomY
,
447 struct pipe_sampler_view
**sv
,
448 int num_sampler_view
,
451 struct st_fp_variant
*fpv
,
452 const GLfloat
*color
,
454 GLboolean write_depth
, GLboolean write_stencil
)
456 struct st_context
*st
= st_context(ctx
);
457 struct pipe_context
*pipe
= st
->pipe
;
458 struct cso_context
*cso
= st
->cso_context
;
459 GLfloat x0
, y0
, x1
, y1
;
461 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
462 unsigned cso_state_mask
;
464 assert(sv
[0]->texture
->target
== st
->internal_target
);
467 /* XXX if DrawPixels image is larger than max texture size, break
470 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
471 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
472 assert(width
<= maxSize
);
473 assert(height
<= maxSize
);
475 cso_state_mask
= (CSO_BIT_RASTERIZER
|
477 CSO_BIT_FRAGMENT_SAMPLERS
|
478 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
479 CSO_BIT_FRAGMENT_SHADER
|
480 CSO_BIT_STREAM_OUTPUTS
|
481 CSO_BIT_VERTEX_SHADER
|
482 CSO_BIT_TESSCTRL_SHADER
|
483 CSO_BIT_TESSEVAL_SHADER
|
484 CSO_BIT_GEOMETRY_SHADER
|
485 CSO_BIT_VERTEX_ELEMENTS
|
486 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
);
488 cso_state_mask
|= (CSO_BIT_DEPTH_STENCIL_ALPHA
|
491 cso_save_state(cso
, cso_state_mask
);
493 /* rasterizer state: just scissor */
495 struct pipe_rasterizer_state rasterizer
;
496 memset(&rasterizer
, 0, sizeof(rasterizer
));
497 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
498 ctx
->Color
._ClampFragmentColor
;
499 rasterizer
.half_pixel_center
= 1;
500 rasterizer
.bottom_edge_rule
= 1;
501 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
502 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
503 cso_set_rasterizer(cso
, &rasterizer
);
507 /* Stencil writing bypasses the normal fragment pipeline to
508 * disable color writing and set stencil test to always pass.
510 struct pipe_depth_stencil_alpha_state dsa
;
511 struct pipe_blend_state blend
;
514 memset(&dsa
, 0, sizeof(dsa
));
515 dsa
.stencil
[0].enabled
= 1;
516 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
517 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
518 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
520 /* writing depth+stencil: depth test always passes */
521 dsa
.depth
.enabled
= 1;
522 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
523 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
525 cso_set_depth_stencil_alpha(cso
, &dsa
);
527 /* blend (colormask) */
528 memset(&blend
, 0, sizeof(blend
));
529 cso_set_blend(cso
, &blend
);
532 /* fragment shader state: TEX lookup program */
533 cso_set_fragment_shader_handle(cso
, driver_fp
);
535 /* vertex shader state: position + texcoord pass-through */
536 cso_set_vertex_shader_handle(cso
, driver_vp
);
538 /* disable other shaders */
539 cso_set_tessctrl_shader_handle(cso
, NULL
);
540 cso_set_tesseval_shader_handle(cso
, NULL
);
541 cso_set_geometry_shader_handle(cso
, NULL
);
543 /* user samplers, plus the drawpix samplers */
545 struct pipe_sampler_state sampler
;
547 memset(&sampler
, 0, sizeof(sampler
));
548 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
549 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
550 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
551 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
552 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
553 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
554 sampler
.normalized_coords
= normalized
;
557 /* drawing a color image */
558 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
559 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
560 fpv
->pixelmap_sampler
+ 1,
561 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
564 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
565 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
567 samplers
[fpv
->drawpix_sampler
] = &sampler
;
569 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
571 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
573 /* drawing a depth/stencil image */
574 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
576 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
580 /* user textures, plus the drawpix textures */
582 /* drawing a color image */
583 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
584 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
585 fpv
->pixelmap_sampler
+ 1,
586 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
588 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
589 sizeof(sampler_views
));
591 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
593 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
594 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
596 /* drawing a depth/stencil image */
597 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
600 /* viewport state: viewport matching window dims */
601 cso_set_viewport_dims(cso
, ctx
->DrawBuffer
->Width
,
602 ctx
->DrawBuffer
->Height
, TRUE
);
604 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
605 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
607 /* Compute Gallium window coords (y=0=top) with pixel zoom.
608 * Recall that these coords are transformed by the current
609 * vertex shader and viewport transformation.
611 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
612 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
613 invertTex
= !invertTex
;
617 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
619 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
621 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
625 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
626 const float fb_width
= (float) fb
->Width
;
627 const float fb_height
= (float) fb
->Height
;
628 const float clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
629 const float clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
630 const float clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
631 const float clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
632 const float maxXcoord
= normalized
?
633 ((float) width
/ sv
[0]->texture
->width0
) : (float) width
;
634 const float maxYcoord
= normalized
635 ? ((float) height
/ sv
[0]->texture
->height0
) : (float) height
;
636 const float sLeft
= 0.0f
, sRight
= maxXcoord
;
637 const float tTop
= invertTex
? maxYcoord
: 0.0f
;
638 const float tBot
= invertTex
? 0.0f
: maxYcoord
;
640 if (!st_draw_quad(ctx
->st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
641 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
642 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
647 cso_restore_state(cso
);
652 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
653 * can't use a fragment shader to write stencil values.
656 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
657 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
658 const struct gl_pixelstore_attrib
*unpack
,
659 const GLvoid
*pixels
)
661 struct st_context
*st
= st_context(ctx
);
662 struct pipe_context
*pipe
= st
->pipe
;
663 struct st_renderbuffer
*strb
;
664 enum pipe_transfer_usage usage
;
665 struct pipe_transfer
*pt
;
666 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
668 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
673 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
675 /* totally clipped */
680 strb
= st_renderbuffer(ctx
->DrawBuffer
->
681 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
683 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
684 y
= ctx
->DrawBuffer
->Height
- y
- height
;
687 if (format
== GL_STENCIL_INDEX
&&
688 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
689 /* writing stencil to a combined depth+stencil buffer */
690 usage
= PIPE_TRANSFER_READ_WRITE
;
693 usage
= PIPE_TRANSFER_WRITE
;
696 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
697 strb
->surface
->u
.tex
.level
,
698 strb
->surface
->u
.tex
.first_layer
,
702 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
705 sValues
= malloc(width
* sizeof(GLubyte
));
706 zValues
= malloc(width
* sizeof(GLuint
));
708 if (sValues
&& zValues
) {
710 for (row
= 0; row
< height
; row
++) {
711 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
712 GLenum destType
= GL_UNSIGNED_BYTE
;
713 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
717 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
718 type
, source
, &clippedUnpack
,
719 ctx
->_ImageTransferState
);
721 if (format
== GL_DEPTH_STENCIL
) {
723 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
724 GL_FLOAT
: GL_UNSIGNED_INT
;
726 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
727 (1 << 24) - 1, type
, source
,
732 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
733 "zoom not complete");
739 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
740 spanY
= height
- row
- 1;
746 /* now pack the stencil (and Z) values in the dest format */
747 switch (pt
->resource
->format
) {
748 case PIPE_FORMAT_S8_UINT
:
750 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
751 assert(usage
== PIPE_TRANSFER_WRITE
);
752 memcpy(dest
, sValues
, width
);
755 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
756 if (format
== GL_DEPTH_STENCIL
) {
757 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
759 assert(usage
== PIPE_TRANSFER_WRITE
);
760 for (k
= 0; k
< width
; k
++) {
761 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
765 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
767 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
768 for (k
= 0; k
< width
; k
++) {
769 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
773 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
774 if (format
== GL_DEPTH_STENCIL
) {
775 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
777 assert(usage
== PIPE_TRANSFER_WRITE
);
778 for (k
= 0; k
< width
; k
++) {
779 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
783 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
785 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
786 for (k
= 0; k
< width
; k
++) {
787 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
791 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
792 if (format
== GL_DEPTH_STENCIL
) {
793 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
794 GLfloat
*destf
= (GLfloat
*)dest
;
796 assert(usage
== PIPE_TRANSFER_WRITE
);
797 for (k
= 0; k
< width
; k
++) {
798 destf
[k
*2] = zValuesFloat
[k
];
799 dest
[k
*2+1] = sValues
[k
] & 0xff;
803 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
805 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
806 for (k
= 0; k
< width
; k
++) {
807 dest
[k
*2+1] = sValues
[k
] & 0xff;
818 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
824 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
826 /* unmap the stencil buffer */
827 pipe_transfer_unmap(pipe
, pt
);
832 * Get fragment program variant for a glDrawPixels or glCopyPixels
833 * command for RGBA data.
835 static struct st_fp_variant
*
836 get_color_fp_variant(struct st_context
*st
)
838 struct gl_context
*ctx
= st
->ctx
;
839 struct st_fp_variant_key key
;
840 struct st_fp_variant
*fpv
;
842 memset(&key
, 0, sizeof(key
));
844 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
846 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
847 ctx
->Pixel
.RedScale
!= 1.0 ||
848 ctx
->Pixel
.GreenBias
!= 0.0 ||
849 ctx
->Pixel
.GreenScale
!= 1.0 ||
850 ctx
->Pixel
.BlueBias
!= 0.0 ||
851 ctx
->Pixel
.BlueScale
!= 1.0 ||
852 ctx
->Pixel
.AlphaBias
!= 0.0 ||
853 ctx
->Pixel
.AlphaScale
!= 1.0);
854 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
855 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
856 st
->ctx
->Color
._ClampFragmentColor
;
858 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
865 * Clamp glDrawPixels width and height to the maximum texture size.
868 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
869 struct gl_pixelstore_attrib
*unpack
)
872 1 << (pipe
->screen
->get_param(pipe
->screen
,
873 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
875 if (*width
> maxSize
) {
876 if (unpack
->RowLength
== 0)
877 unpack
->RowLength
= *width
;
880 if (*height
> maxSize
) {
887 * Search the array of 4 swizzle components for the named component and return
891 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
894 for (i
= 0; i
< 4; i
++) {
895 if (swizzle
[i
] == component
)
898 assert(!"search_swizzle() failed");
904 * Set the sampler view's swizzle terms. This is used to handle RGBA
905 * swizzling when the incoming image format isn't an exact match for
906 * the actual texture format. For example, if we have glDrawPixels(
907 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
908 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
909 * avoid swizzling all the pixels in software in the texstore code.
912 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
914 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
915 const struct util_format_description
*desc
=
916 util_format_description(sv
->texture
->format
);
917 unsigned c0
, c1
, c2
, c3
;
919 /* Every gallium driver supports at least one 32-bit packed RGBA format.
920 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
922 assert(desc
->block
.bits
== 32);
924 /* invert the format's swizzle to setup the sampler's swizzle */
925 if (format
== GL_RGBA
) {
926 c0
= UTIL_FORMAT_SWIZZLE_X
;
927 c1
= UTIL_FORMAT_SWIZZLE_Y
;
928 c2
= UTIL_FORMAT_SWIZZLE_Z
;
929 c3
= UTIL_FORMAT_SWIZZLE_W
;
932 assert(format
== GL_BGRA
);
933 c0
= UTIL_FORMAT_SWIZZLE_Z
;
934 c1
= UTIL_FORMAT_SWIZZLE_Y
;
935 c2
= UTIL_FORMAT_SWIZZLE_X
;
936 c3
= UTIL_FORMAT_SWIZZLE_W
;
938 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
939 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
940 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
941 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
944 /* use the default sampler swizzle */
950 * Called via ctx->Driver.DrawPixels()
953 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
954 GLsizei width
, GLsizei height
,
955 GLenum format
, GLenum type
,
956 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
958 void *driver_vp
, *driver_fp
;
959 struct st_context
*st
= st_context(ctx
);
960 struct pipe_context
*pipe
= st
->pipe
;
961 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
962 struct pipe_sampler_view
*sv
[2] = { NULL
};
963 int num_sampler_view
= 1;
964 struct gl_pixelstore_attrib clippedUnpack
;
965 struct st_fp_variant
*fpv
= NULL
;
966 struct pipe_resource
*pt
;
968 /* Mesa state should be up to date by now */
969 assert(ctx
->NewState
== 0x0);
971 st_flush_bitmap_cache(st
);
973 st_validate_state(st
, ST_PIPELINE_RENDER
);
975 /* Limit the size of the glDrawPixels to the max texture size.
976 * Strictly speaking, that's not correct but since we don't handle
977 * larger images yet, this is better than crashing.
979 clippedUnpack
= *unpack
;
980 unpack
= &clippedUnpack
;
981 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
983 if (format
== GL_DEPTH_STENCIL
)
984 write_stencil
= write_depth
= GL_TRUE
;
985 else if (format
== GL_STENCIL_INDEX
)
986 write_stencil
= GL_TRUE
;
987 else if (format
== GL_DEPTH_COMPONENT
)
988 write_depth
= GL_TRUE
;
991 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
992 /* software fallback */
993 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
999 * Get vertex/fragment shaders
1001 if (write_depth
|| write_stencil
) {
1002 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1004 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1007 fpv
= get_color_fp_variant(st
);
1009 driver_fp
= fpv
->driver_shader
;
1010 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1012 if (ctx
->Pixel
.MapColorFlag
) {
1013 pipe_sampler_view_reference(&sv
[1],
1014 st
->pixel_xfer
.pixelmap_sampler_view
);
1018 /* compiling a new fragment shader variant added new state constants
1019 * into the constant buffer, we need to update them
1021 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1022 PIPE_SHADER_FRAGMENT
);
1025 /* Put glDrawPixels image into a texture */
1026 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1028 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1032 /* create sampler view for the image */
1033 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1035 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1036 pipe_resource_reference(&pt
, NULL
);
1040 /* Set up the sampler view's swizzle */
1041 setup_sampler_swizzle(sv
[0], format
, type
);
1043 /* Create a second sampler view to read stencil. The stencil is
1044 * written using the shader stencil export functionality.
1046 if (write_stencil
) {
1047 enum pipe_format stencil_format
=
1048 util_format_stencil_only(pt
->format
);
1049 /* we should not be doing pixel map/transfer (see above) */
1050 assert(num_sampler_view
== 1);
1051 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1054 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1055 pipe_resource_reference(&pt
, NULL
);
1056 pipe_sampler_view_reference(&sv
[0], NULL
);
1062 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1064 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1069 ctx
->Current
.RasterColor
,
1070 GL_FALSE
, write_depth
, write_stencil
);
1071 pipe_sampler_view_reference(&sv
[0], NULL
);
1072 if (num_sampler_view
> 1)
1073 pipe_sampler_view_reference(&sv
[1], NULL
);
1075 pipe_resource_reference(&pt
, NULL
);
1081 * Software fallback for glCopyPixels(GL_STENCIL).
1084 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1085 GLsizei width
, GLsizei height
,
1086 GLint dstx
, GLint dsty
)
1088 struct st_renderbuffer
*rbDraw
;
1089 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1090 enum pipe_transfer_usage usage
;
1091 struct pipe_transfer
*ptDraw
;
1096 buffer
= malloc(width
* height
* sizeof(ubyte
));
1098 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1102 /* Get the dest renderbuffer */
1103 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1104 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1106 /* this will do stencil pixel transfer ops */
1107 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1108 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1109 &ctx
->DefaultPacking
, buffer
);
1112 /* debug code: dump stencil values */
1114 for (row
= 0; row
< height
; row
++) {
1115 printf("%3d: ", row
);
1116 for (col
= 0; col
< width
; col
++) {
1117 printf("%02x ", buffer
[col
+ row
* width
]);
1123 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1124 usage
= PIPE_TRANSFER_READ_WRITE
;
1126 usage
= PIPE_TRANSFER_WRITE
;
1128 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1129 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1132 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1133 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1135 /* map the stencil buffer */
1136 drawMap
= pipe_transfer_map(pipe
,
1138 rbDraw
->surface
->u
.tex
.level
,
1139 rbDraw
->surface
->u
.tex
.first_layer
,
1141 width
, height
, &ptDraw
);
1144 /* XXX PixelZoom not handled yet */
1145 for (i
= 0; i
< height
; i
++) {
1152 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1156 dst
= drawMap
+ y
* ptDraw
->stride
;
1157 src
= buffer
+ i
* width
;
1159 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1164 /* unmap the stencil buffer */
1165 pipe_transfer_unmap(pipe
, ptDraw
);
1170 * Return renderbuffer to use for reading color pixels for glCopyPixels
1172 static struct st_renderbuffer
*
1173 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1175 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1176 struct st_renderbuffer
*strb
=
1177 st_renderbuffer(fb
->_ColorReadBuffer
);
1184 * Try to do a glCopyPixels for simple cases with a blit by calling
1187 * We can do this when we're copying color pixels (depth/stencil
1188 * eventually) with no pixel zoom, no pixel transfer ops, no
1189 * per-fragment ops, and the src/dest regions don't overlap.
1192 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1193 GLsizei width
, GLsizei height
,
1194 GLint dstx
, GLint dsty
, GLenum type
)
1196 struct st_context
*st
= st_context(ctx
);
1197 struct pipe_context
*pipe
= st
->pipe
;
1198 struct pipe_screen
*screen
= pipe
->screen
;
1199 struct gl_pixelstore_attrib pack
, unpack
;
1200 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1202 if (type
== GL_COLOR
&&
1203 ctx
->Pixel
.ZoomX
== 1.0 &&
1204 ctx
->Pixel
.ZoomY
== 1.0 &&
1205 ctx
->_ImageTransferState
== 0x0 &&
1206 !ctx
->Color
.BlendEnabled
&&
1207 !ctx
->Color
.AlphaEnabled
&&
1208 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1210 !ctx
->Fog
.Enabled
&&
1211 !ctx
->Stencil
.Enabled
&&
1212 !ctx
->FragmentProgram
.Enabled
&&
1213 !ctx
->VertexProgram
.Enabled
&&
1214 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1215 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1216 !ctx
->Query
.CondRenderQuery
&&
1217 !ctx
->Query
.CurrentOcclusionObject
) {
1218 struct st_renderbuffer
*rbRead
, *rbDraw
;
1221 * Clip the read region against the src buffer bounds.
1222 * We'll still allocate a temporary buffer/texture for the original
1223 * src region size but we'll only read the region which is on-screen.
1224 * This may mean that we draw garbage pixels into the dest region, but
1231 pack
= ctx
->DefaultPacking
;
1232 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1233 return GL_TRUE
; /* all done */
1235 /* clip against dest buffer bounds and scissor box */
1236 drawX
= dstx
+ pack
.SkipPixels
;
1237 drawY
= dsty
+ pack
.SkipRows
;
1239 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1240 return GL_TRUE
; /* all done */
1242 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1243 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1248 rbRead
= st_get_color_read_renderbuffer(ctx
);
1249 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1251 /* Flip src/dst position depending on the orientation of buffers. */
1252 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1253 readY
= rbRead
->Base
.Height
- readY
;
1257 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1258 /* We can't flip the destination for pipe->blit, so we only adjust
1259 * its position and flip the source.
1261 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1266 if (rbRead
!= rbDraw
||
1267 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1268 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1269 struct pipe_blit_info blit
;
1271 memset(&blit
, 0, sizeof(blit
));
1272 blit
.src
.resource
= rbRead
->texture
;
1273 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1274 blit
.src
.format
= rbRead
->texture
->format
;
1275 blit
.src
.box
.x
= readX
;
1276 blit
.src
.box
.y
= readY
;
1277 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1278 blit
.src
.box
.width
= readW
;
1279 blit
.src
.box
.height
= readH
;
1280 blit
.src
.box
.depth
= 1;
1281 blit
.dst
.resource
= rbDraw
->texture
;
1282 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1283 blit
.dst
.format
= rbDraw
->texture
->format
;
1284 blit
.dst
.box
.x
= drawX
;
1285 blit
.dst
.box
.y
= drawY
;
1286 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1287 blit
.dst
.box
.width
= drawW
;
1288 blit
.dst
.box
.height
= drawH
;
1289 blit
.dst
.box
.depth
= 1;
1290 blit
.mask
= PIPE_MASK_RGBA
;
1291 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1293 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1294 blit
.src
.resource
->target
,
1295 blit
.src
.resource
->nr_samples
,
1296 PIPE_BIND_SAMPLER_VIEW
) &&
1297 screen
->is_format_supported(screen
, blit
.dst
.format
,
1298 blit
.dst
.resource
->target
,
1299 blit
.dst
.resource
->nr_samples
,
1300 PIPE_BIND_RENDER_TARGET
)) {
1301 pipe
->blit(pipe
, &blit
);
1312 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1313 GLsizei width
, GLsizei height
,
1314 GLint dstx
, GLint dsty
, GLenum type
)
1316 struct st_context
*st
= st_context(ctx
);
1317 struct pipe_context
*pipe
= st
->pipe
;
1318 struct pipe_screen
*screen
= pipe
->screen
;
1319 struct st_renderbuffer
*rbRead
;
1320 void *driver_vp
, *driver_fp
;
1321 struct pipe_resource
*pt
;
1322 struct pipe_sampler_view
*sv
[2] = { NULL
};
1323 struct st_fp_variant
*fpv
= NULL
;
1324 int num_sampler_view
= 1;
1325 enum pipe_format srcFormat
;
1327 GLboolean invertTex
= GL_FALSE
;
1328 GLint readX
, readY
, readW
, readH
;
1329 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1331 st_flush_bitmap_cache(st
);
1333 st_validate_state(st
, ST_PIPELINE_RENDER
);
1335 if (type
== GL_DEPTH_STENCIL
) {
1336 /* XXX make this more efficient */
1337 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1338 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1342 if (type
== GL_STENCIL
) {
1343 /* can't use texturing to do stencil */
1344 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1348 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1352 * The subsequent code implements glCopyPixels by copying the source
1353 * pixels into a temporary texture that's then applied to a textured quad.
1354 * When we draw the textured quad, all the usual per-fragment operations
1360 * Get vertex/fragment shaders
1362 if (type
== GL_COLOR
) {
1363 fpv
= get_color_fp_variant(st
);
1365 rbRead
= st_get_color_read_renderbuffer(ctx
);
1367 driver_fp
= fpv
->driver_shader
;
1368 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1370 if (ctx
->Pixel
.MapColorFlag
) {
1371 pipe_sampler_view_reference(&sv
[1],
1372 st
->pixel_xfer
.pixelmap_sampler_view
);
1376 /* compiling a new fragment shader variant added new state constants
1377 * into the constant buffer, we need to update them
1379 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1380 PIPE_SHADER_FRAGMENT
);
1383 assert(type
== GL_DEPTH
);
1384 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1385 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1387 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1388 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1391 /* Choose the format for the temporary texture. */
1392 srcFormat
= rbRead
->texture
->format
;
1393 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1394 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1396 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1398 /* srcFormat is non-renderable. Find a compatible renderable format. */
1399 if (type
== GL_DEPTH
) {
1400 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1401 GL_NONE
, st
->internal_target
, 0,
1405 assert(type
== GL_COLOR
);
1407 if (util_format_is_float(srcFormat
)) {
1408 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1409 GL_NONE
, st
->internal_target
, 0,
1412 else if (util_format_is_pure_sint(srcFormat
)) {
1413 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1414 GL_NONE
, st
->internal_target
, 0,
1417 else if (util_format_is_pure_uint(srcFormat
)) {
1418 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1419 GL_NONE
, st
->internal_target
, 0,
1422 else if (util_format_is_snorm(srcFormat
)) {
1423 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1424 GL_NONE
, st
->internal_target
, 0,
1428 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1429 GL_NONE
, st
->internal_target
, 0,
1434 if (srcFormat
== PIPE_FORMAT_NONE
) {
1435 assert(0 && "cannot choose a format for src of CopyPixels");
1440 /* Invert src region if needed */
1441 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1442 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1443 invertTex
= !invertTex
;
1446 /* Clip the read region against the src buffer bounds.
1447 * We'll still allocate a temporary buffer/texture for the original
1448 * src region size but we'll only read the region which is on-screen.
1449 * This may mean that we draw garbage pixels into the dest region, but
1456 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1457 /* The source region is completely out of bounds. Do nothing.
1458 * The GL spec says "Results of copies from outside the window,
1459 * or from regions of the window that are not exposed, are
1460 * hardware dependent and undefined."
1465 readW
= MAX2(0, readW
);
1466 readH
= MAX2(0, readH
);
1468 /* Allocate the temporary texture. */
1469 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1473 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1475 pipe_resource_reference(&pt
, NULL
);
1479 /* Copy the src region to the temporary texture. */
1481 struct pipe_blit_info blit
;
1483 memset(&blit
, 0, sizeof(blit
));
1484 blit
.src
.resource
= rbRead
->texture
;
1485 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1486 blit
.src
.format
= rbRead
->texture
->format
;
1487 blit
.src
.box
.x
= readX
;
1488 blit
.src
.box
.y
= readY
;
1489 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1490 blit
.src
.box
.width
= readW
;
1491 blit
.src
.box
.height
= readH
;
1492 blit
.src
.box
.depth
= 1;
1493 blit
.dst
.resource
= pt
;
1495 blit
.dst
.format
= pt
->format
;
1496 blit
.dst
.box
.x
= pack
.SkipPixels
;
1497 blit
.dst
.box
.y
= pack
.SkipRows
;
1499 blit
.dst
.box
.width
= readW
;
1500 blit
.dst
.box
.height
= readH
;
1501 blit
.dst
.box
.depth
= 1;
1502 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1503 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1505 pipe
->blit(pipe
, &blit
);
1508 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1509 * textured quad with that texture.
1511 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1512 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1517 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1518 invertTex
, GL_FALSE
, GL_FALSE
);
1520 pipe_resource_reference(&pt
, NULL
);
1521 pipe_sampler_view_reference(&sv
[0], NULL
);
1526 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1528 functions
->DrawPixels
= st_DrawPixels
;
1529 functions
->CopyPixels
= st_CopyPixels
;
1534 st_destroy_drawpix(struct st_context
*st
)
1538 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1539 if (st
->drawpix
.zs_shaders
[i
])
1540 cso_delete_fragment_shader(st
->cso_context
,
1541 st
->drawpix
.zs_shaders
[i
]);
1544 if (st
->drawpix
.vert_shaders
[0])
1545 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1546 if (st
->drawpix
.vert_shaders
[1])
1547 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);