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
= MAX2(MAX2(fpv
->drawpix_sampler
, fpv
->pixelmap_sampler
) + 1,
671 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
673 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
674 sizeof(sampler_views
));
676 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
678 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
679 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
681 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
683 /* Compute Gallium window coords (y=0=top) with pixel zoom.
684 * Recall that these coords are transformed by the current
685 * vertex shader and viewport transformation.
687 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
688 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
689 invertTex
= !invertTex
;
693 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
695 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
697 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
700 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
701 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
702 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
705 cso_restore_rasterizer(cso
);
706 cso_restore_viewport(cso
);
707 cso_restore_fragment_samplers(cso
);
708 cso_restore_fragment_sampler_views(cso
);
709 cso_restore_fragment_shader(cso
);
710 cso_restore_vertex_shader(cso
);
711 cso_restore_tessctrl_shader(cso
);
712 cso_restore_tesseval_shader(cso
);
713 cso_restore_geometry_shader(cso
);
714 cso_restore_vertex_elements(cso
);
715 cso_restore_aux_vertex_buffer_slot(cso
);
716 cso_restore_stream_outputs(cso
);
718 cso_restore_depth_stencil_alpha(cso
);
719 cso_restore_blend(cso
);
725 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
726 * can't use a fragment shader to write stencil values.
729 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
730 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
731 const struct gl_pixelstore_attrib
*unpack
,
732 const GLvoid
*pixels
)
734 struct st_context
*st
= st_context(ctx
);
735 struct pipe_context
*pipe
= st
->pipe
;
736 struct st_renderbuffer
*strb
;
737 enum pipe_transfer_usage usage
;
738 struct pipe_transfer
*pt
;
739 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
741 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
746 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
748 /* totally clipped */
753 strb
= st_renderbuffer(ctx
->DrawBuffer
->
754 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
756 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
757 y
= ctx
->DrawBuffer
->Height
- y
- height
;
760 if (format
== GL_STENCIL_INDEX
&&
761 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
762 /* writing stencil to a combined depth+stencil buffer */
763 usage
= PIPE_TRANSFER_READ_WRITE
;
766 usage
= PIPE_TRANSFER_WRITE
;
769 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
770 strb
->surface
->u
.tex
.level
,
771 strb
->surface
->u
.tex
.first_layer
,
775 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
778 sValues
= malloc(width
* sizeof(GLubyte
));
779 zValues
= malloc(width
* sizeof(GLuint
));
781 if (sValues
&& zValues
) {
783 for (row
= 0; row
< height
; row
++) {
784 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
785 GLenum destType
= GL_UNSIGNED_BYTE
;
786 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
790 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
791 type
, source
, &clippedUnpack
,
792 ctx
->_ImageTransferState
);
794 if (format
== GL_DEPTH_STENCIL
) {
796 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
797 GL_FLOAT
: GL_UNSIGNED_INT
;
799 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
800 (1 << 24) - 1, type
, source
,
805 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
806 "zoom not complete");
812 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
813 spanY
= height
- row
- 1;
819 /* now pack the stencil (and Z) values in the dest format */
820 switch (pt
->resource
->format
) {
821 case PIPE_FORMAT_S8_UINT
:
823 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
824 assert(usage
== PIPE_TRANSFER_WRITE
);
825 memcpy(dest
, sValues
, width
);
828 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
829 if (format
== GL_DEPTH_STENCIL
) {
830 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
832 assert(usage
== PIPE_TRANSFER_WRITE
);
833 for (k
= 0; k
< width
; k
++) {
834 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
838 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
840 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
841 for (k
= 0; k
< width
; k
++) {
842 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
846 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
847 if (format
== GL_DEPTH_STENCIL
) {
848 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
850 assert(usage
== PIPE_TRANSFER_WRITE
);
851 for (k
= 0; k
< width
; k
++) {
852 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
856 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
858 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
859 for (k
= 0; k
< width
; k
++) {
860 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
864 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
865 if (format
== GL_DEPTH_STENCIL
) {
866 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
867 GLfloat
*destf
= (GLfloat
*)dest
;
869 assert(usage
== PIPE_TRANSFER_WRITE
);
870 for (k
= 0; k
< width
; k
++) {
871 destf
[k
*2] = zValuesFloat
[k
];
872 dest
[k
*2+1] = sValues
[k
] & 0xff;
876 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
878 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
879 for (k
= 0; k
< width
; k
++) {
880 dest
[k
*2+1] = sValues
[k
] & 0xff;
891 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
897 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
899 /* unmap the stencil buffer */
900 pipe_transfer_unmap(pipe
, pt
);
905 * Get fragment program variant for a glDrawPixels or glCopyPixels
906 * command for RGBA data.
908 static struct st_fp_variant
*
909 get_color_fp_variant(struct st_context
*st
)
911 struct gl_context
*ctx
= st
->ctx
;
912 struct st_fp_variant_key key
;
913 struct st_fp_variant
*fpv
;
915 memset(&key
, 0, sizeof(key
));
919 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
920 ctx
->Pixel
.RedScale
!= 1.0 ||
921 ctx
->Pixel
.GreenBias
!= 0.0 ||
922 ctx
->Pixel
.GreenScale
!= 1.0 ||
923 ctx
->Pixel
.BlueBias
!= 0.0 ||
924 ctx
->Pixel
.BlueScale
!= 1.0 ||
925 ctx
->Pixel
.AlphaBias
!= 0.0 ||
926 ctx
->Pixel
.AlphaScale
!= 1.0);
927 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
928 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
929 st
->ctx
->Color
._ClampFragmentColor
;
931 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
938 * Clamp glDrawPixels width and height to the maximum texture size.
941 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
942 struct gl_pixelstore_attrib
*unpack
)
945 1 << (pipe
->screen
->get_param(pipe
->screen
,
946 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
948 if (*width
> maxSize
) {
949 if (unpack
->RowLength
== 0)
950 unpack
->RowLength
= *width
;
953 if (*height
> maxSize
) {
960 * Called via ctx->Driver.DrawPixels()
963 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
964 GLsizei width
, GLsizei height
,
965 GLenum format
, GLenum type
,
966 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
968 void *driver_vp
, *driver_fp
;
969 struct st_context
*st
= st_context(ctx
);
970 const GLfloat
*color
;
971 struct pipe_context
*pipe
= st
->pipe
;
972 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
973 struct pipe_sampler_view
*sv
[2] = { NULL
};
974 int num_sampler_view
= 1;
975 struct gl_pixelstore_attrib clippedUnpack
;
976 struct st_fp_variant
*fpv
= NULL
;
978 /* Mesa state should be up to date by now */
979 assert(ctx
->NewState
== 0x0);
981 st_validate_state(st
);
983 /* Limit the size of the glDrawPixels to the max texture size.
984 * Strictly speaking, that's not correct but since we don't handle
985 * larger images yet, this is better than crashing.
987 clippedUnpack
= *unpack
;
988 unpack
= &clippedUnpack
;
989 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
991 if (format
== GL_DEPTH_STENCIL
)
992 write_stencil
= write_depth
= GL_TRUE
;
993 else if (format
== GL_STENCIL_INDEX
)
994 write_stencil
= GL_TRUE
;
995 else if (format
== GL_DEPTH_COMPONENT
)
996 write_depth
= GL_TRUE
;
999 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1000 /* software fallback */
1001 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1007 * Get vertex/fragment shaders
1009 if (write_depth
|| write_stencil
) {
1010 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1012 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1013 color
= ctx
->Current
.RasterColor
;
1016 fpv
= get_color_fp_variant(st
);
1018 driver_fp
= fpv
->driver_shader
;
1019 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1022 if (ctx
->Pixel
.MapColorFlag
) {
1023 pipe_sampler_view_reference(&sv
[1],
1024 st
->pixel_xfer
.pixelmap_sampler_view
);
1028 /* update fragment program constants */
1029 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1032 /* draw with textured quad */
1034 struct pipe_resource
*pt
1035 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1037 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1040 /* Create a second sampler view to read stencil.
1041 * The stencil is written using the shader stencil export
1043 if (write_stencil
) {
1044 enum pipe_format stencil_format
=
1045 util_format_stencil_only(pt
->format
);
1046 /* we should not be doing pixel map/transfer (see above) */
1047 assert(num_sampler_view
== 1);
1048 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1053 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1055 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1060 color
, GL_FALSE
, write_depth
, write_stencil
);
1061 pipe_sampler_view_reference(&sv
[0], NULL
);
1062 if (num_sampler_view
> 1)
1063 pipe_sampler_view_reference(&sv
[1], NULL
);
1065 pipe_resource_reference(&pt
, NULL
);
1073 * Software fallback for glCopyPixels(GL_STENCIL).
1076 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1077 GLsizei width
, GLsizei height
,
1078 GLint dstx
, GLint dsty
)
1080 struct st_renderbuffer
*rbDraw
;
1081 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1082 enum pipe_transfer_usage usage
;
1083 struct pipe_transfer
*ptDraw
;
1088 buffer
= malloc(width
* height
* sizeof(ubyte
));
1090 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1094 /* Get the dest renderbuffer */
1095 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1096 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1098 /* this will do stencil pixel transfer ops */
1099 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1100 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1101 &ctx
->DefaultPacking
, buffer
);
1104 /* debug code: dump stencil values */
1106 for (row
= 0; row
< height
; row
++) {
1107 printf("%3d: ", row
);
1108 for (col
= 0; col
< width
; col
++) {
1109 printf("%02x ", buffer
[col
+ row
* width
]);
1115 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1116 usage
= PIPE_TRANSFER_READ_WRITE
;
1118 usage
= PIPE_TRANSFER_WRITE
;
1120 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1121 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1124 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1125 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1127 /* map the stencil buffer */
1128 drawMap
= pipe_transfer_map(pipe
,
1130 rbDraw
->surface
->u
.tex
.level
,
1131 rbDraw
->surface
->u
.tex
.first_layer
,
1133 width
, height
, &ptDraw
);
1136 /* XXX PixelZoom not handled yet */
1137 for (i
= 0; i
< height
; i
++) {
1144 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1148 dst
= drawMap
+ y
* ptDraw
->stride
;
1149 src
= buffer
+ i
* width
;
1151 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1156 /* unmap the stencil buffer */
1157 pipe_transfer_unmap(pipe
, ptDraw
);
1162 * Return renderbuffer to use for reading color pixels for glCopyPixels
1164 static struct st_renderbuffer
*
1165 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1167 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1168 struct st_renderbuffer
*strb
=
1169 st_renderbuffer(fb
->_ColorReadBuffer
);
1176 * Try to do a glCopyPixels for simple cases with a blit by calling
1179 * We can do this when we're copying color pixels (depth/stencil
1180 * eventually) with no pixel zoom, no pixel transfer ops, no
1181 * per-fragment ops, and the src/dest regions don't overlap.
1184 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1185 GLsizei width
, GLsizei height
,
1186 GLint dstx
, GLint dsty
, GLenum type
)
1188 struct st_context
*st
= st_context(ctx
);
1189 struct pipe_context
*pipe
= st
->pipe
;
1190 struct pipe_screen
*screen
= pipe
->screen
;
1191 struct gl_pixelstore_attrib pack
, unpack
;
1192 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1194 if (type
== GL_COLOR
&&
1195 ctx
->Pixel
.ZoomX
== 1.0 &&
1196 ctx
->Pixel
.ZoomY
== 1.0 &&
1197 ctx
->_ImageTransferState
== 0x0 &&
1198 !ctx
->Color
.BlendEnabled
&&
1199 !ctx
->Color
.AlphaEnabled
&&
1201 !ctx
->Fog
.Enabled
&&
1202 !ctx
->Stencil
.Enabled
&&
1203 !ctx
->FragmentProgram
.Enabled
&&
1204 !ctx
->VertexProgram
.Enabled
&&
1205 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1206 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1207 !ctx
->Query
.CondRenderQuery
&&
1208 !ctx
->Query
.CurrentOcclusionObject
) {
1209 struct st_renderbuffer
*rbRead
, *rbDraw
;
1212 * Clip the read region against the src buffer bounds.
1213 * We'll still allocate a temporary buffer/texture for the original
1214 * src region size but we'll only read the region which is on-screen.
1215 * This may mean that we draw garbage pixels into the dest region, but
1222 pack
= ctx
->DefaultPacking
;
1223 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1224 return GL_TRUE
; /* all done */
1226 /* clip against dest buffer bounds and scissor box */
1227 drawX
= dstx
+ pack
.SkipPixels
;
1228 drawY
= dsty
+ pack
.SkipRows
;
1230 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1231 return GL_TRUE
; /* all done */
1233 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1234 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1239 rbRead
= st_get_color_read_renderbuffer(ctx
);
1240 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1242 /* Flip src/dst position depending on the orientation of buffers. */
1243 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1244 readY
= rbRead
->Base
.Height
- readY
;
1248 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1249 /* We can't flip the destination for pipe->blit, so we only adjust
1250 * its position and flip the source.
1252 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1257 if (rbRead
!= rbDraw
||
1258 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1259 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1260 struct pipe_blit_info blit
;
1262 memset(&blit
, 0, sizeof(blit
));
1263 blit
.src
.resource
= rbRead
->texture
;
1264 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1265 blit
.src
.format
= rbRead
->texture
->format
;
1266 blit
.src
.box
.x
= readX
;
1267 blit
.src
.box
.y
= readY
;
1268 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1269 blit
.src
.box
.width
= readW
;
1270 blit
.src
.box
.height
= readH
;
1271 blit
.src
.box
.depth
= 1;
1272 blit
.dst
.resource
= rbDraw
->texture
;
1273 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1274 blit
.dst
.format
= rbDraw
->texture
->format
;
1275 blit
.dst
.box
.x
= drawX
;
1276 blit
.dst
.box
.y
= drawY
;
1277 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1278 blit
.dst
.box
.width
= drawW
;
1279 blit
.dst
.box
.height
= drawH
;
1280 blit
.dst
.box
.depth
= 1;
1281 blit
.mask
= PIPE_MASK_RGBA
;
1282 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1284 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1285 blit
.src
.resource
->target
,
1286 blit
.src
.resource
->nr_samples
,
1287 PIPE_BIND_SAMPLER_VIEW
) &&
1288 screen
->is_format_supported(screen
, blit
.dst
.format
,
1289 blit
.dst
.resource
->target
,
1290 blit
.dst
.resource
->nr_samples
,
1291 PIPE_BIND_RENDER_TARGET
)) {
1292 pipe
->blit(pipe
, &blit
);
1303 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1304 GLsizei width
, GLsizei height
,
1305 GLint dstx
, GLint dsty
, GLenum type
)
1307 struct st_context
*st
= st_context(ctx
);
1308 struct pipe_context
*pipe
= st
->pipe
;
1309 struct pipe_screen
*screen
= pipe
->screen
;
1310 struct st_renderbuffer
*rbRead
;
1311 void *driver_vp
, *driver_fp
;
1312 struct pipe_resource
*pt
;
1313 struct pipe_sampler_view
*sv
[2] = { NULL
};
1314 struct st_fp_variant
*fpv
= NULL
;
1315 int num_sampler_view
= 1;
1317 enum pipe_format srcFormat
;
1319 GLboolean invertTex
= GL_FALSE
;
1320 GLint readX
, readY
, readW
, readH
;
1321 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1323 st_validate_state(st
);
1325 if (type
== GL_DEPTH_STENCIL
) {
1326 /* XXX make this more efficient */
1327 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1328 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1332 if (type
== GL_STENCIL
) {
1333 /* can't use texturing to do stencil */
1334 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1338 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1342 * The subsequent code implements glCopyPixels by copying the source
1343 * pixels into a temporary texture that's then applied to a textured quad.
1344 * When we draw the textured quad, all the usual per-fragment operations
1350 * Get vertex/fragment shaders
1352 if (type
== GL_COLOR
) {
1353 fpv
= get_color_fp_variant(st
);
1355 rbRead
= st_get_color_read_renderbuffer(ctx
);
1358 driver_fp
= fpv
->driver_shader
;
1359 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1361 if (ctx
->Pixel
.MapColorFlag
) {
1362 pipe_sampler_view_reference(&sv
[1],
1363 st
->pixel_xfer
.pixelmap_sampler_view
);
1367 /* update fragment program constants */
1368 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1371 assert(type
== GL_DEPTH
);
1372 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1373 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1374 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1376 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1377 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1380 /* Choose the format for the temporary texture. */
1381 srcFormat
= rbRead
->texture
->format
;
1382 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1383 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1385 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1387 /* srcFormat is non-renderable. Find a compatible renderable format. */
1388 if (type
== GL_DEPTH
) {
1389 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1390 GL_NONE
, st
->internal_target
, 0,
1394 assert(type
== GL_COLOR
);
1396 if (util_format_is_float(srcFormat
)) {
1397 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1398 GL_NONE
, st
->internal_target
, 0,
1401 else if (util_format_is_pure_sint(srcFormat
)) {
1402 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1403 GL_NONE
, st
->internal_target
, 0,
1406 else if (util_format_is_pure_uint(srcFormat
)) {
1407 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1408 GL_NONE
, st
->internal_target
, 0,
1411 else if (util_format_is_snorm(srcFormat
)) {
1412 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1413 GL_NONE
, st
->internal_target
, 0,
1417 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1418 GL_NONE
, st
->internal_target
, 0,
1423 if (srcFormat
== PIPE_FORMAT_NONE
) {
1424 assert(0 && "cannot choose a format for src of CopyPixels");
1429 /* Invert src region if needed */
1430 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1431 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1432 invertTex
= !invertTex
;
1435 /* Clip the read region against the src buffer bounds.
1436 * We'll still allocate a temporary buffer/texture for the original
1437 * src region size but we'll only read the region which is on-screen.
1438 * This may mean that we draw garbage pixels into the dest region, but
1445 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1446 /* The source region is completely out of bounds. Do nothing.
1447 * The GL spec says "Results of copies from outside the window,
1448 * or from regions of the window that are not exposed, are
1449 * hardware dependent and undefined."
1454 readW
= MAX2(0, readW
);
1455 readH
= MAX2(0, readH
);
1457 /* Allocate the temporary texture. */
1458 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1462 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1464 pipe_resource_reference(&pt
, NULL
);
1468 /* Copy the src region to the temporary texture. */
1470 struct pipe_blit_info blit
;
1472 memset(&blit
, 0, sizeof(blit
));
1473 blit
.src
.resource
= rbRead
->texture
;
1474 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1475 blit
.src
.format
= rbRead
->texture
->format
;
1476 blit
.src
.box
.x
= readX
;
1477 blit
.src
.box
.y
= readY
;
1478 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1479 blit
.src
.box
.width
= readW
;
1480 blit
.src
.box
.height
= readH
;
1481 blit
.src
.box
.depth
= 1;
1482 blit
.dst
.resource
= pt
;
1484 blit
.dst
.format
= pt
->format
;
1485 blit
.dst
.box
.x
= pack
.SkipPixels
;
1486 blit
.dst
.box
.y
= pack
.SkipRows
;
1488 blit
.dst
.box
.width
= readW
;
1489 blit
.dst
.box
.height
= readH
;
1490 blit
.dst
.box
.depth
= 1;
1491 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1492 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1494 pipe
->blit(pipe
, &blit
);
1497 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1498 * textured quad with that texture.
1500 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1501 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1506 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1508 pipe_resource_reference(&pt
, NULL
);
1509 pipe_sampler_view_reference(&sv
[0], NULL
);
1514 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1516 functions
->DrawPixels
= st_DrawPixels
;
1517 functions
->CopyPixels
= st_CopyPixels
;
1522 st_destroy_drawpix(struct st_context
*st
)
1526 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1527 if (st
->drawpix
.zs_shaders
[i
])
1528 cso_delete_fragment_shader(st
->cso_context
,
1529 st
->drawpix
.zs_shaders
[i
]);
1532 if (st
->drawpix
.vert_shaders
[0])
1533 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1534 if (st
->drawpix
.vert_shaders
[1])
1535 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);