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_drawpixels.h"
54 #include "st_cb_readpixels.h"
55 #include "st_cb_fbo.h"
56 #include "st_context.h"
58 #include "st_format.h"
59 #include "st_program.h"
60 #include "st_texture.h"
62 #include "pipe/p_context.h"
63 #include "pipe/p_defines.h"
64 #include "tgsi/tgsi_ureg.h"
65 #include "util/u_draw_quad.h"
66 #include "util/u_format.h"
67 #include "util/u_inlines.h"
68 #include "util/u_math.h"
69 #include "util/u_tile.h"
70 #include "util/u_upload_mgr.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 ));
168 /* MOV result.texcoord0, vertex.attr[1]; */
170 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
171 ureg_DECL_vs_input( ureg
, 1 ));
174 /* MOV result.color0, vertex.attr[2]; */
176 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
177 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 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
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 success
= _mesa_texstore(ctx
, 2, /* dims */
399 baseInternalFormat
, /* baseInternalFormat */
400 mformat
, /* mesa_format */
401 transfer
->stride
, /* dstRowStride, bytes */
402 &dest
, /* destSlices */
403 width
, height
, 1, /* size */
404 format
, type
, /* src format/type */
405 pixels
, /* data source */
409 pipe_transfer_unmap(pipe
, transfer
);
414 ctx
->_ImageTransferState
= imageTransferStateSave
;
417 _mesa_unmap_pbo_source(ctx
, unpack
);
424 * Draw quad with texcoords and optional color.
425 * Coords are gallium window coords with y=0=top.
426 * \param color may be null
427 * \param invertTex if true, flip texcoords vertically
430 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
431 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
432 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
434 struct st_context
*st
= st_context(ctx
);
435 struct pipe_context
*pipe
= st
->pipe
;
436 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
437 struct pipe_resource
*buf
= NULL
;
440 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
441 &buf
, (void **) &verts
);
446 /* setup vertex data */
448 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
449 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
450 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
451 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
452 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
453 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
454 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
455 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
456 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
457 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
461 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
462 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
465 verts
[1][0][0] = clip_x1
;
466 verts
[1][0][1] = clip_y0
;
469 verts
[2][0][0] = clip_x1
;
470 verts
[2][0][1] = clip_y1
;
473 verts
[3][0][0] = clip_x0
;
474 verts
[3][0][1] = clip_y1
;
476 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
477 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
478 verts
[1][1][0] = sRight
;
479 verts
[1][1][1] = tTop
;
480 verts
[2][1][0] = sRight
;
481 verts
[2][1][1] = tBot
;
482 verts
[3][1][0] = sLeft
;
483 verts
[3][1][1] = tBot
;
485 /* same for all verts: */
487 for (i
= 0; i
< 4; i
++) {
488 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
489 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
490 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
491 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
492 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
493 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
494 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
495 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
499 for (i
= 0; i
< 4; i
++) {
500 verts
[i
][0][2] = z
; /*Z*/
501 verts
[i
][0][3] = 1.0f
; /*W*/
502 verts
[i
][1][2] = 0.0f
; /*R*/
503 verts
[i
][1][3] = 1.0f
; /*Q*/
508 u_upload_unmap(st
->uploader
);
509 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
510 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
514 3); /* attribs/vert */
515 pipe_resource_reference(&buf
, NULL
);
521 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
522 GLsizei width
, GLsizei height
,
523 GLfloat zoomX
, GLfloat zoomY
,
524 struct pipe_sampler_view
**sv
,
525 int num_sampler_view
,
528 struct st_fp_variant
*fpv
,
529 const GLfloat
*color
,
531 GLboolean write_depth
, GLboolean write_stencil
)
533 struct st_context
*st
= st_context(ctx
);
534 struct pipe_context
*pipe
= st
->pipe
;
535 struct cso_context
*cso
= st
->cso_context
;
536 GLfloat x0
, y0
, x1
, y1
;
538 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
541 /* XXX if DrawPixels image is larger than max texture size, break
544 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
545 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
546 assert(width
<= maxSize
);
547 assert(height
<= maxSize
);
549 cso_save_rasterizer(cso
);
550 cso_save_viewport(cso
);
551 cso_save_fragment_samplers(cso
);
552 cso_save_fragment_sampler_views(cso
);
553 cso_save_fragment_shader(cso
);
554 cso_save_stream_outputs(cso
);
555 cso_save_vertex_shader(cso
);
556 cso_save_tessctrl_shader(cso
);
557 cso_save_tesseval_shader(cso
);
558 cso_save_geometry_shader(cso
);
559 cso_save_vertex_elements(cso
);
560 cso_save_aux_vertex_buffer_slot(cso
);
562 cso_save_depth_stencil_alpha(cso
);
566 /* rasterizer state: just scissor */
568 struct pipe_rasterizer_state rasterizer
;
569 memset(&rasterizer
, 0, sizeof(rasterizer
));
570 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
571 ctx
->Color
._ClampFragmentColor
;
572 rasterizer
.half_pixel_center
= 1;
573 rasterizer
.bottom_edge_rule
= 1;
574 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
575 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
576 cso_set_rasterizer(cso
, &rasterizer
);
580 /* Stencil writing bypasses the normal fragment pipeline to
581 * disable color writing and set stencil test to always pass.
583 struct pipe_depth_stencil_alpha_state dsa
;
584 struct pipe_blend_state blend
;
587 memset(&dsa
, 0, sizeof(dsa
));
588 dsa
.stencil
[0].enabled
= 1;
589 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
590 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
591 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
593 /* writing depth+stencil: depth test always passes */
594 dsa
.depth
.enabled
= 1;
595 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
596 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
598 cso_set_depth_stencil_alpha(cso
, &dsa
);
600 /* blend (colormask) */
601 memset(&blend
, 0, sizeof(blend
));
602 cso_set_blend(cso
, &blend
);
605 /* fragment shader state: TEX lookup program */
606 cso_set_fragment_shader_handle(cso
, driver_fp
);
608 /* vertex shader state: position + texcoord pass-through */
609 cso_set_vertex_shader_handle(cso
, driver_vp
);
611 /* disable other shaders */
612 cso_set_tessctrl_shader_handle(cso
, NULL
);
613 cso_set_tesseval_shader_handle(cso
, NULL
);
614 cso_set_geometry_shader_handle(cso
, NULL
);
616 /* user samplers, plus the drawpix samplers */
618 struct pipe_sampler_state sampler
;
620 memset(&sampler
, 0, sizeof(sampler
));
621 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
622 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
623 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
624 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
625 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
626 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
627 sampler
.normalized_coords
= normalized
;
630 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
631 uint num
= MAX2(MAX2(fpv
->drawpix_sampler
, fpv
->pixelmap_sampler
) + 1,
632 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
635 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
636 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
638 samplers
[fpv
->drawpix_sampler
] = &sampler
;
640 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
642 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
644 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
646 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
650 /* viewport state: viewport matching window dims */
652 const float w
= (float) ctx
->DrawBuffer
->Width
;
653 const float h
= (float) ctx
->DrawBuffer
->Height
;
654 struct pipe_viewport_state vp
;
655 vp
.scale
[0] = 0.5f
* w
;
656 vp
.scale
[1] = -0.5f
* h
;
658 vp
.translate
[0] = 0.5f
* w
;
659 vp
.translate
[1] = 0.5f
* h
;
660 vp
.translate
[2] = 0.5f
;
661 cso_set_viewport(cso
, &vp
);
664 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
665 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
667 /* user textures, plus the drawpix textures */
669 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
670 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
671 fpv
->pixelmap_sampler
+ 1,
672 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
674 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
675 sizeof(sampler_views
));
677 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
679 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
680 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
682 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
684 /* Compute Gallium window coords (y=0=top) with pixel zoom.
685 * Recall that these coords are transformed by the current
686 * vertex shader and viewport transformation.
688 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
689 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
690 invertTex
= !invertTex
;
694 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
696 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
698 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
701 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
702 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
703 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
706 cso_restore_rasterizer(cso
);
707 cso_restore_viewport(cso
);
708 cso_restore_fragment_samplers(cso
);
709 cso_restore_fragment_sampler_views(cso
);
710 cso_restore_fragment_shader(cso
);
711 cso_restore_vertex_shader(cso
);
712 cso_restore_tessctrl_shader(cso
);
713 cso_restore_tesseval_shader(cso
);
714 cso_restore_geometry_shader(cso
);
715 cso_restore_vertex_elements(cso
);
716 cso_restore_aux_vertex_buffer_slot(cso
);
717 cso_restore_stream_outputs(cso
);
719 cso_restore_depth_stencil_alpha(cso
);
720 cso_restore_blend(cso
);
726 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
727 * can't use a fragment shader to write stencil values.
730 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
731 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
732 const struct gl_pixelstore_attrib
*unpack
,
733 const GLvoid
*pixels
)
735 struct st_context
*st
= st_context(ctx
);
736 struct pipe_context
*pipe
= st
->pipe
;
737 struct st_renderbuffer
*strb
;
738 enum pipe_transfer_usage usage
;
739 struct pipe_transfer
*pt
;
740 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
742 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
747 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
749 /* totally clipped */
754 strb
= st_renderbuffer(ctx
->DrawBuffer
->
755 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
757 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
758 y
= ctx
->DrawBuffer
->Height
- y
- height
;
761 if (format
== GL_STENCIL_INDEX
&&
762 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
763 /* writing stencil to a combined depth+stencil buffer */
764 usage
= PIPE_TRANSFER_READ_WRITE
;
767 usage
= PIPE_TRANSFER_WRITE
;
770 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
771 strb
->surface
->u
.tex
.level
,
772 strb
->surface
->u
.tex
.first_layer
,
776 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
779 sValues
= malloc(width
* sizeof(GLubyte
));
780 zValues
= malloc(width
* sizeof(GLuint
));
782 if (sValues
&& zValues
) {
784 for (row
= 0; row
< height
; row
++) {
785 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
786 GLenum destType
= GL_UNSIGNED_BYTE
;
787 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
791 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
792 type
, source
, &clippedUnpack
,
793 ctx
->_ImageTransferState
);
795 if (format
== GL_DEPTH_STENCIL
) {
797 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
798 GL_FLOAT
: GL_UNSIGNED_INT
;
800 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
801 (1 << 24) - 1, type
, source
,
806 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
807 "zoom not complete");
813 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
814 spanY
= height
- row
- 1;
820 /* now pack the stencil (and Z) values in the dest format */
821 switch (pt
->resource
->format
) {
822 case PIPE_FORMAT_S8_UINT
:
824 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
825 assert(usage
== PIPE_TRANSFER_WRITE
);
826 memcpy(dest
, sValues
, width
);
829 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
830 if (format
== GL_DEPTH_STENCIL
) {
831 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
833 assert(usage
== PIPE_TRANSFER_WRITE
);
834 for (k
= 0; k
< width
; k
++) {
835 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
839 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
841 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
842 for (k
= 0; k
< width
; k
++) {
843 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
847 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
848 if (format
== GL_DEPTH_STENCIL
) {
849 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
851 assert(usage
== PIPE_TRANSFER_WRITE
);
852 for (k
= 0; k
< width
; k
++) {
853 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
857 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
859 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
860 for (k
= 0; k
< width
; k
++) {
861 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
865 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
866 if (format
== GL_DEPTH_STENCIL
) {
867 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
868 GLfloat
*destf
= (GLfloat
*)dest
;
870 assert(usage
== PIPE_TRANSFER_WRITE
);
871 for (k
= 0; k
< width
; k
++) {
872 destf
[k
*2] = zValuesFloat
[k
];
873 dest
[k
*2+1] = sValues
[k
] & 0xff;
877 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
879 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
880 for (k
= 0; k
< width
; k
++) {
881 dest
[k
*2+1] = sValues
[k
] & 0xff;
892 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
898 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
900 /* unmap the stencil buffer */
901 pipe_transfer_unmap(pipe
, pt
);
906 * Get fragment program variant for a glDrawPixels or glCopyPixels
907 * command for RGBA data.
909 static struct st_fp_variant
*
910 get_color_fp_variant(struct st_context
*st
)
912 struct gl_context
*ctx
= st
->ctx
;
913 struct st_fp_variant_key key
;
914 struct st_fp_variant
*fpv
;
916 memset(&key
, 0, sizeof(key
));
918 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
920 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
921 ctx
->Pixel
.RedScale
!= 1.0 ||
922 ctx
->Pixel
.GreenBias
!= 0.0 ||
923 ctx
->Pixel
.GreenScale
!= 1.0 ||
924 ctx
->Pixel
.BlueBias
!= 0.0 ||
925 ctx
->Pixel
.BlueScale
!= 1.0 ||
926 ctx
->Pixel
.AlphaBias
!= 0.0 ||
927 ctx
->Pixel
.AlphaScale
!= 1.0);
928 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
929 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
930 st
->ctx
->Color
._ClampFragmentColor
;
932 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
939 * Clamp glDrawPixels width and height to the maximum texture size.
942 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
943 struct gl_pixelstore_attrib
*unpack
)
946 1 << (pipe
->screen
->get_param(pipe
->screen
,
947 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
949 if (*width
> maxSize
) {
950 if (unpack
->RowLength
== 0)
951 unpack
->RowLength
= *width
;
954 if (*height
> maxSize
) {
961 * Called via ctx->Driver.DrawPixels()
964 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
965 GLsizei width
, GLsizei height
,
966 GLenum format
, GLenum type
,
967 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
969 void *driver_vp
, *driver_fp
;
970 struct st_context
*st
= st_context(ctx
);
971 const GLfloat
*color
;
972 struct pipe_context
*pipe
= st
->pipe
;
973 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
974 struct pipe_sampler_view
*sv
[2] = { NULL
};
975 int num_sampler_view
= 1;
976 struct gl_pixelstore_attrib clippedUnpack
;
977 struct st_fp_variant
*fpv
= NULL
;
978 struct pipe_resource
*pt
;
980 /* Mesa state should be up to date by now */
981 assert(ctx
->NewState
== 0x0);
983 st_validate_state(st
);
985 /* Limit the size of the glDrawPixels to the max texture size.
986 * Strictly speaking, that's not correct but since we don't handle
987 * larger images yet, this is better than crashing.
989 clippedUnpack
= *unpack
;
990 unpack
= &clippedUnpack
;
991 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
993 if (format
== GL_DEPTH_STENCIL
)
994 write_stencil
= write_depth
= GL_TRUE
;
995 else if (format
== GL_STENCIL_INDEX
)
996 write_stencil
= GL_TRUE
;
997 else if (format
== GL_DEPTH_COMPONENT
)
998 write_depth
= GL_TRUE
;
1000 if (write_stencil
&&
1001 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1002 /* software fallback */
1003 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1009 * Get vertex/fragment shaders
1011 if (write_depth
|| write_stencil
) {
1012 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1014 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1015 color
= ctx
->Current
.RasterColor
;
1018 fpv
= get_color_fp_variant(st
);
1020 driver_fp
= fpv
->driver_shader
;
1021 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1024 if (ctx
->Pixel
.MapColorFlag
) {
1025 pipe_sampler_view_reference(&sv
[1],
1026 st
->pixel_xfer
.pixelmap_sampler_view
);
1030 /* update fragment program constants */
1031 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1034 /* Put glDrawPixels image into a texture */
1035 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1037 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1041 /* create sampler view for the image */
1042 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1044 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1045 pipe_resource_reference(&pt
, NULL
);
1049 /* Create a second sampler view to read stencil. The stencil is
1050 * written using the shader stencil export functionality.
1052 if (write_stencil
) {
1053 enum pipe_format stencil_format
=
1054 util_format_stencil_only(pt
->format
);
1055 /* we should not be doing pixel map/transfer (see above) */
1056 assert(num_sampler_view
== 1);
1057 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1061 pipe_resource_reference(&pt
, NULL
);
1062 pipe_sampler_view_reference(&sv
[0], NULL
);
1068 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1070 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1075 color
, GL_FALSE
, write_depth
, write_stencil
);
1076 pipe_sampler_view_reference(&sv
[0], NULL
);
1077 if (num_sampler_view
> 1)
1078 pipe_sampler_view_reference(&sv
[1], NULL
);
1080 pipe_resource_reference(&pt
, NULL
);
1086 * Software fallback for glCopyPixels(GL_STENCIL).
1089 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1090 GLsizei width
, GLsizei height
,
1091 GLint dstx
, GLint dsty
)
1093 struct st_renderbuffer
*rbDraw
;
1094 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1095 enum pipe_transfer_usage usage
;
1096 struct pipe_transfer
*ptDraw
;
1101 buffer
= malloc(width
* height
* sizeof(ubyte
));
1103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1107 /* Get the dest renderbuffer */
1108 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1109 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1111 /* this will do stencil pixel transfer ops */
1112 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1113 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1114 &ctx
->DefaultPacking
, buffer
);
1117 /* debug code: dump stencil values */
1119 for (row
= 0; row
< height
; row
++) {
1120 printf("%3d: ", row
);
1121 for (col
= 0; col
< width
; col
++) {
1122 printf("%02x ", buffer
[col
+ row
* width
]);
1128 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1129 usage
= PIPE_TRANSFER_READ_WRITE
;
1131 usage
= PIPE_TRANSFER_WRITE
;
1133 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1134 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1137 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1138 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1140 /* map the stencil buffer */
1141 drawMap
= pipe_transfer_map(pipe
,
1143 rbDraw
->surface
->u
.tex
.level
,
1144 rbDraw
->surface
->u
.tex
.first_layer
,
1146 width
, height
, &ptDraw
);
1149 /* XXX PixelZoom not handled yet */
1150 for (i
= 0; i
< height
; i
++) {
1157 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1161 dst
= drawMap
+ y
* ptDraw
->stride
;
1162 src
= buffer
+ i
* width
;
1164 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1169 /* unmap the stencil buffer */
1170 pipe_transfer_unmap(pipe
, ptDraw
);
1175 * Return renderbuffer to use for reading color pixels for glCopyPixels
1177 static struct st_renderbuffer
*
1178 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1180 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1181 struct st_renderbuffer
*strb
=
1182 st_renderbuffer(fb
->_ColorReadBuffer
);
1189 * Try to do a glCopyPixels for simple cases with a blit by calling
1192 * We can do this when we're copying color pixels (depth/stencil
1193 * eventually) with no pixel zoom, no pixel transfer ops, no
1194 * per-fragment ops, and the src/dest regions don't overlap.
1197 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1198 GLsizei width
, GLsizei height
,
1199 GLint dstx
, GLint dsty
, GLenum type
)
1201 struct st_context
*st
= st_context(ctx
);
1202 struct pipe_context
*pipe
= st
->pipe
;
1203 struct pipe_screen
*screen
= pipe
->screen
;
1204 struct gl_pixelstore_attrib pack
, unpack
;
1205 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1207 if (type
== GL_COLOR
&&
1208 ctx
->Pixel
.ZoomX
== 1.0 &&
1209 ctx
->Pixel
.ZoomY
== 1.0 &&
1210 ctx
->_ImageTransferState
== 0x0 &&
1211 !ctx
->Color
.BlendEnabled
&&
1212 !ctx
->Color
.AlphaEnabled
&&
1214 !ctx
->Fog
.Enabled
&&
1215 !ctx
->Stencil
.Enabled
&&
1216 !ctx
->FragmentProgram
.Enabled
&&
1217 !ctx
->VertexProgram
.Enabled
&&
1218 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1219 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1220 !ctx
->Query
.CondRenderQuery
&&
1221 !ctx
->Query
.CurrentOcclusionObject
) {
1222 struct st_renderbuffer
*rbRead
, *rbDraw
;
1225 * Clip the read region against the src buffer bounds.
1226 * We'll still allocate a temporary buffer/texture for the original
1227 * src region size but we'll only read the region which is on-screen.
1228 * This may mean that we draw garbage pixels into the dest region, but
1235 pack
= ctx
->DefaultPacking
;
1236 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1237 return GL_TRUE
; /* all done */
1239 /* clip against dest buffer bounds and scissor box */
1240 drawX
= dstx
+ pack
.SkipPixels
;
1241 drawY
= dsty
+ pack
.SkipRows
;
1243 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1244 return GL_TRUE
; /* all done */
1246 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1247 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1252 rbRead
= st_get_color_read_renderbuffer(ctx
);
1253 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1255 /* Flip src/dst position depending on the orientation of buffers. */
1256 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1257 readY
= rbRead
->Base
.Height
- readY
;
1261 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1262 /* We can't flip the destination for pipe->blit, so we only adjust
1263 * its position and flip the source.
1265 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1270 if (rbRead
!= rbDraw
||
1271 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1272 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1273 struct pipe_blit_info blit
;
1275 memset(&blit
, 0, sizeof(blit
));
1276 blit
.src
.resource
= rbRead
->texture
;
1277 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1278 blit
.src
.format
= rbRead
->texture
->format
;
1279 blit
.src
.box
.x
= readX
;
1280 blit
.src
.box
.y
= readY
;
1281 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1282 blit
.src
.box
.width
= readW
;
1283 blit
.src
.box
.height
= readH
;
1284 blit
.src
.box
.depth
= 1;
1285 blit
.dst
.resource
= rbDraw
->texture
;
1286 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1287 blit
.dst
.format
= rbDraw
->texture
->format
;
1288 blit
.dst
.box
.x
= drawX
;
1289 blit
.dst
.box
.y
= drawY
;
1290 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1291 blit
.dst
.box
.width
= drawW
;
1292 blit
.dst
.box
.height
= drawH
;
1293 blit
.dst
.box
.depth
= 1;
1294 blit
.mask
= PIPE_MASK_RGBA
;
1295 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1297 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1298 blit
.src
.resource
->target
,
1299 blit
.src
.resource
->nr_samples
,
1300 PIPE_BIND_SAMPLER_VIEW
) &&
1301 screen
->is_format_supported(screen
, blit
.dst
.format
,
1302 blit
.dst
.resource
->target
,
1303 blit
.dst
.resource
->nr_samples
,
1304 PIPE_BIND_RENDER_TARGET
)) {
1305 pipe
->blit(pipe
, &blit
);
1316 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1317 GLsizei width
, GLsizei height
,
1318 GLint dstx
, GLint dsty
, GLenum type
)
1320 struct st_context
*st
= st_context(ctx
);
1321 struct pipe_context
*pipe
= st
->pipe
;
1322 struct pipe_screen
*screen
= pipe
->screen
;
1323 struct st_renderbuffer
*rbRead
;
1324 void *driver_vp
, *driver_fp
;
1325 struct pipe_resource
*pt
;
1326 struct pipe_sampler_view
*sv
[2] = { NULL
};
1327 struct st_fp_variant
*fpv
= NULL
;
1328 int num_sampler_view
= 1;
1330 enum pipe_format srcFormat
;
1332 GLboolean invertTex
= GL_FALSE
;
1333 GLint readX
, readY
, readW
, readH
;
1334 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1336 st_validate_state(st
);
1338 if (type
== GL_DEPTH_STENCIL
) {
1339 /* XXX make this more efficient */
1340 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1341 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1345 if (type
== GL_STENCIL
) {
1346 /* can't use texturing to do stencil */
1347 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1351 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1355 * The subsequent code implements glCopyPixels by copying the source
1356 * pixels into a temporary texture that's then applied to a textured quad.
1357 * When we draw the textured quad, all the usual per-fragment operations
1363 * Get vertex/fragment shaders
1365 if (type
== GL_COLOR
) {
1366 fpv
= get_color_fp_variant(st
);
1368 rbRead
= st_get_color_read_renderbuffer(ctx
);
1371 driver_fp
= fpv
->driver_shader
;
1372 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1374 if (ctx
->Pixel
.MapColorFlag
) {
1375 pipe_sampler_view_reference(&sv
[1],
1376 st
->pixel_xfer
.pixelmap_sampler_view
);
1380 /* update fragment program constants */
1381 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1384 assert(type
== GL_DEPTH
);
1385 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1386 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1387 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1389 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1390 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1393 /* Choose the format for the temporary texture. */
1394 srcFormat
= rbRead
->texture
->format
;
1395 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1396 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1398 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1400 /* srcFormat is non-renderable. Find a compatible renderable format. */
1401 if (type
== GL_DEPTH
) {
1402 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1403 GL_NONE
, st
->internal_target
, 0,
1407 assert(type
== GL_COLOR
);
1409 if (util_format_is_float(srcFormat
)) {
1410 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1411 GL_NONE
, st
->internal_target
, 0,
1414 else if (util_format_is_pure_sint(srcFormat
)) {
1415 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1416 GL_NONE
, st
->internal_target
, 0,
1419 else if (util_format_is_pure_uint(srcFormat
)) {
1420 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1421 GL_NONE
, st
->internal_target
, 0,
1424 else if (util_format_is_snorm(srcFormat
)) {
1425 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1426 GL_NONE
, st
->internal_target
, 0,
1430 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1431 GL_NONE
, st
->internal_target
, 0,
1436 if (srcFormat
== PIPE_FORMAT_NONE
) {
1437 assert(0 && "cannot choose a format for src of CopyPixels");
1442 /* Invert src region if needed */
1443 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1444 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1445 invertTex
= !invertTex
;
1448 /* Clip the read region against the src buffer bounds.
1449 * We'll still allocate a temporary buffer/texture for the original
1450 * src region size but we'll only read the region which is on-screen.
1451 * This may mean that we draw garbage pixels into the dest region, but
1458 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1459 /* The source region is completely out of bounds. Do nothing.
1460 * The GL spec says "Results of copies from outside the window,
1461 * or from regions of the window that are not exposed, are
1462 * hardware dependent and undefined."
1467 readW
= MAX2(0, readW
);
1468 readH
= MAX2(0, readH
);
1470 /* Allocate the temporary texture. */
1471 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1475 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1477 pipe_resource_reference(&pt
, NULL
);
1481 /* Copy the src region to the temporary texture. */
1483 struct pipe_blit_info blit
;
1485 memset(&blit
, 0, sizeof(blit
));
1486 blit
.src
.resource
= rbRead
->texture
;
1487 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1488 blit
.src
.format
= rbRead
->texture
->format
;
1489 blit
.src
.box
.x
= readX
;
1490 blit
.src
.box
.y
= readY
;
1491 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1492 blit
.src
.box
.width
= readW
;
1493 blit
.src
.box
.height
= readH
;
1494 blit
.src
.box
.depth
= 1;
1495 blit
.dst
.resource
= pt
;
1497 blit
.dst
.format
= pt
->format
;
1498 blit
.dst
.box
.x
= pack
.SkipPixels
;
1499 blit
.dst
.box
.y
= pack
.SkipRows
;
1501 blit
.dst
.box
.width
= readW
;
1502 blit
.dst
.box
.height
= readH
;
1503 blit
.dst
.box
.depth
= 1;
1504 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1505 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1507 pipe
->blit(pipe
, &blit
);
1510 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1511 * textured quad with that texture.
1513 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1514 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1519 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1521 pipe_resource_reference(&pt
, NULL
);
1522 pipe_sampler_view_reference(&sv
[0], NULL
);
1527 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1529 functions
->DrawPixels
= st_DrawPixels
;
1530 functions
->CopyPixels
= st_CopyPixels
;
1535 st_destroy_drawpix(struct st_context
*st
)
1539 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1540 if (st
->drawpix
.zs_shaders
[i
])
1541 cso_delete_fragment_shader(st
->cso_context
,
1542 st
->drawpix
.zs_shaders
[i
]);
1545 if (st
->drawpix
.vert_shaders
[0])
1546 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1547 if (st
->drawpix
.vert_shaders
[1])
1548 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);