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"
59 #include "st_format.h"
60 #include "st_program.h"
61 #include "st_texture.h"
63 #include "pipe/p_context.h"
64 #include "pipe/p_defines.h"
65 #include "tgsi/tgsi_ureg.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 ));
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 quad with texcoords and optional color.
445 * Coords are gallium window coords with y=0=top.
446 * \param color may be null
447 * \param invertTex if true, flip texcoords vertically
450 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
451 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
452 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
454 struct st_context
*st
= st_context(ctx
);
455 struct pipe_vertex_buffer vb
= {0};
456 struct st_util_vertex
*verts
;
458 vb
.stride
= sizeof(struct st_util_vertex
);
460 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(struct st_util_vertex
), 4,
461 &vb
.buffer_offset
, &vb
.buffer
, (void **) &verts
);
466 /* setup vertex data */
468 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
469 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
470 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
471 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
472 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
473 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
474 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
475 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
476 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
477 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
480 verts
[0].x
= clip_x0
;
481 verts
[0].y
= clip_y0
;
483 verts
[0].r
= color
[0];
484 verts
[0].g
= color
[1];
485 verts
[0].b
= color
[2];
486 verts
[0].a
= color
[3];
491 verts
[1].x
= clip_x1
;
492 verts
[1].y
= clip_y0
;
494 verts
[1].r
= color
[0];
495 verts
[1].g
= color
[1];
496 verts
[1].b
= color
[2];
497 verts
[1].a
= color
[3];
502 verts
[2].x
= clip_x1
;
503 verts
[2].y
= clip_y1
;
505 verts
[2].r
= color
[0];
506 verts
[2].g
= color
[1];
507 verts
[2].b
= color
[2];
508 verts
[2].a
= color
[3];
513 verts
[3].x
= clip_x0
;
514 verts
[3].y
= clip_y1
;
516 verts
[3].r
= color
[0];
517 verts
[3].g
= color
[1];
518 verts
[3].b
= color
[2];
519 verts
[3].a
= color
[3];
524 u_upload_unmap(st
->uploader
);
526 cso_set_vertex_buffers(st
->cso_context
,
527 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
530 cso_draw_arrays(st
->cso_context
, PIPE_PRIM_QUADS
, 0, 4);
532 pipe_resource_reference(&vb
.buffer
, NULL
);
538 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
539 GLsizei width
, GLsizei height
,
540 GLfloat zoomX
, GLfloat zoomY
,
541 struct pipe_sampler_view
**sv
,
542 int num_sampler_view
,
545 struct st_fp_variant
*fpv
,
546 const GLfloat
*color
,
548 GLboolean write_depth
, GLboolean write_stencil
)
550 struct st_context
*st
= st_context(ctx
);
551 struct pipe_context
*pipe
= st
->pipe
;
552 struct cso_context
*cso
= st
->cso_context
;
553 GLfloat x0
, y0
, x1
, y1
;
555 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
557 assert(sv
[0]->texture
->target
== st
->internal_target
);
560 /* XXX if DrawPixels image is larger than max texture size, break
563 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
564 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
565 assert(width
<= maxSize
);
566 assert(height
<= maxSize
);
568 cso_save_rasterizer(cso
);
569 cso_save_viewport(cso
);
570 cso_save_fragment_samplers(cso
);
571 cso_save_fragment_sampler_views(cso
);
572 cso_save_fragment_shader(cso
);
573 cso_save_stream_outputs(cso
);
574 cso_save_vertex_shader(cso
);
575 cso_save_tessctrl_shader(cso
);
576 cso_save_tesseval_shader(cso
);
577 cso_save_geometry_shader(cso
);
578 cso_save_vertex_elements(cso
);
579 cso_save_aux_vertex_buffer_slot(cso
);
581 cso_save_depth_stencil_alpha(cso
);
585 /* rasterizer state: just scissor */
587 struct pipe_rasterizer_state rasterizer
;
588 memset(&rasterizer
, 0, sizeof(rasterizer
));
589 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
590 ctx
->Color
._ClampFragmentColor
;
591 rasterizer
.half_pixel_center
= 1;
592 rasterizer
.bottom_edge_rule
= 1;
593 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
594 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
595 cso_set_rasterizer(cso
, &rasterizer
);
599 /* Stencil writing bypasses the normal fragment pipeline to
600 * disable color writing and set stencil test to always pass.
602 struct pipe_depth_stencil_alpha_state dsa
;
603 struct pipe_blend_state blend
;
606 memset(&dsa
, 0, sizeof(dsa
));
607 dsa
.stencil
[0].enabled
= 1;
608 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
609 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
610 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
612 /* writing depth+stencil: depth test always passes */
613 dsa
.depth
.enabled
= 1;
614 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
615 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
617 cso_set_depth_stencil_alpha(cso
, &dsa
);
619 /* blend (colormask) */
620 memset(&blend
, 0, sizeof(blend
));
621 cso_set_blend(cso
, &blend
);
624 /* fragment shader state: TEX lookup program */
625 cso_set_fragment_shader_handle(cso
, driver_fp
);
627 /* vertex shader state: position + texcoord pass-through */
628 cso_set_vertex_shader_handle(cso
, driver_vp
);
630 /* disable other shaders */
631 cso_set_tessctrl_shader_handle(cso
, NULL
);
632 cso_set_tesseval_shader_handle(cso
, NULL
);
633 cso_set_geometry_shader_handle(cso
, NULL
);
635 /* user samplers, plus the drawpix samplers */
637 struct pipe_sampler_state sampler
;
639 memset(&sampler
, 0, sizeof(sampler
));
640 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
641 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
642 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
643 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
644 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
645 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
646 sampler
.normalized_coords
= normalized
;
649 /* drawing a color image */
650 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
651 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
652 fpv
->pixelmap_sampler
+ 1,
653 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
656 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
657 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
659 samplers
[fpv
->drawpix_sampler
] = &sampler
;
661 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
663 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
665 /* drawing a depth/stencil image */
666 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
668 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
672 /* user textures, plus the drawpix textures */
674 /* drawing a color image */
675 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
676 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
677 fpv
->pixelmap_sampler
+ 1,
678 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
680 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
681 sizeof(sampler_views
));
683 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
685 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
686 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
688 /* drawing a depth/stencil image */
689 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
692 /* viewport state: viewport matching window dims */
694 const float w
= (float) ctx
->DrawBuffer
->Width
;
695 const float h
= (float) ctx
->DrawBuffer
->Height
;
696 struct pipe_viewport_state vp
;
697 vp
.scale
[0] = 0.5f
* w
;
698 vp
.scale
[1] = -0.5f
* h
;
700 vp
.translate
[0] = 0.5f
* w
;
701 vp
.translate
[1] = 0.5f
* h
;
702 vp
.translate
[2] = 0.5f
;
703 cso_set_viewport(cso
, &vp
);
706 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
707 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
709 /* Compute Gallium window coords (y=0=top) with pixel zoom.
710 * Recall that these coords are transformed by the current
711 * vertex shader and viewport transformation.
713 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
714 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
715 invertTex
= !invertTex
;
719 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
721 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
723 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
726 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
727 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
728 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
731 cso_restore_rasterizer(cso
);
732 cso_restore_viewport(cso
);
733 cso_restore_fragment_samplers(cso
);
734 cso_restore_fragment_sampler_views(cso
);
735 cso_restore_fragment_shader(cso
);
736 cso_restore_vertex_shader(cso
);
737 cso_restore_tessctrl_shader(cso
);
738 cso_restore_tesseval_shader(cso
);
739 cso_restore_geometry_shader(cso
);
740 cso_restore_vertex_elements(cso
);
741 cso_restore_aux_vertex_buffer_slot(cso
);
742 cso_restore_stream_outputs(cso
);
744 cso_restore_depth_stencil_alpha(cso
);
745 cso_restore_blend(cso
);
751 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
752 * can't use a fragment shader to write stencil values.
755 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
756 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
757 const struct gl_pixelstore_attrib
*unpack
,
758 const GLvoid
*pixels
)
760 struct st_context
*st
= st_context(ctx
);
761 struct pipe_context
*pipe
= st
->pipe
;
762 struct st_renderbuffer
*strb
;
763 enum pipe_transfer_usage usage
;
764 struct pipe_transfer
*pt
;
765 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
767 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
772 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
774 /* totally clipped */
779 strb
= st_renderbuffer(ctx
->DrawBuffer
->
780 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
782 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
783 y
= ctx
->DrawBuffer
->Height
- y
- height
;
786 if (format
== GL_STENCIL_INDEX
&&
787 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
788 /* writing stencil to a combined depth+stencil buffer */
789 usage
= PIPE_TRANSFER_READ_WRITE
;
792 usage
= PIPE_TRANSFER_WRITE
;
795 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
796 strb
->surface
->u
.tex
.level
,
797 strb
->surface
->u
.tex
.first_layer
,
801 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
804 sValues
= malloc(width
* sizeof(GLubyte
));
805 zValues
= malloc(width
* sizeof(GLuint
));
807 if (sValues
&& zValues
) {
809 for (row
= 0; row
< height
; row
++) {
810 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
811 GLenum destType
= GL_UNSIGNED_BYTE
;
812 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
816 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
817 type
, source
, &clippedUnpack
,
818 ctx
->_ImageTransferState
);
820 if (format
== GL_DEPTH_STENCIL
) {
822 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
823 GL_FLOAT
: GL_UNSIGNED_INT
;
825 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
826 (1 << 24) - 1, type
, source
,
831 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
832 "zoom not complete");
838 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
839 spanY
= height
- row
- 1;
845 /* now pack the stencil (and Z) values in the dest format */
846 switch (pt
->resource
->format
) {
847 case PIPE_FORMAT_S8_UINT
:
849 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
850 assert(usage
== PIPE_TRANSFER_WRITE
);
851 memcpy(dest
, sValues
, width
);
854 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
855 if (format
== GL_DEPTH_STENCIL
) {
856 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
858 assert(usage
== PIPE_TRANSFER_WRITE
);
859 for (k
= 0; k
< width
; k
++) {
860 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
864 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
866 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
867 for (k
= 0; k
< width
; k
++) {
868 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
872 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
873 if (format
== GL_DEPTH_STENCIL
) {
874 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
876 assert(usage
== PIPE_TRANSFER_WRITE
);
877 for (k
= 0; k
< width
; k
++) {
878 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
882 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
884 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
885 for (k
= 0; k
< width
; k
++) {
886 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
890 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
891 if (format
== GL_DEPTH_STENCIL
) {
892 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
893 GLfloat
*destf
= (GLfloat
*)dest
;
895 assert(usage
== PIPE_TRANSFER_WRITE
);
896 for (k
= 0; k
< width
; k
++) {
897 destf
[k
*2] = zValuesFloat
[k
];
898 dest
[k
*2+1] = sValues
[k
] & 0xff;
902 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
904 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
905 for (k
= 0; k
< width
; k
++) {
906 dest
[k
*2+1] = sValues
[k
] & 0xff;
917 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
923 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
925 /* unmap the stencil buffer */
926 pipe_transfer_unmap(pipe
, pt
);
931 * Get fragment program variant for a glDrawPixels or glCopyPixels
932 * command for RGBA data.
934 static struct st_fp_variant
*
935 get_color_fp_variant(struct st_context
*st
)
937 struct gl_context
*ctx
= st
->ctx
;
938 struct st_fp_variant_key key
;
939 struct st_fp_variant
*fpv
;
941 memset(&key
, 0, sizeof(key
));
943 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
945 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
946 ctx
->Pixel
.RedScale
!= 1.0 ||
947 ctx
->Pixel
.GreenBias
!= 0.0 ||
948 ctx
->Pixel
.GreenScale
!= 1.0 ||
949 ctx
->Pixel
.BlueBias
!= 0.0 ||
950 ctx
->Pixel
.BlueScale
!= 1.0 ||
951 ctx
->Pixel
.AlphaBias
!= 0.0 ||
952 ctx
->Pixel
.AlphaScale
!= 1.0);
953 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
954 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
955 st
->ctx
->Color
._ClampFragmentColor
;
957 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
964 * Clamp glDrawPixels width and height to the maximum texture size.
967 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
968 struct gl_pixelstore_attrib
*unpack
)
971 1 << (pipe
->screen
->get_param(pipe
->screen
,
972 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
974 if (*width
> maxSize
) {
975 if (unpack
->RowLength
== 0)
976 unpack
->RowLength
= *width
;
979 if (*height
> maxSize
) {
986 * Search the array of 4 swizzle components for the named component and return
990 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
993 for (i
= 0; i
< 4; i
++) {
994 if (swizzle
[i
] == component
)
997 assert(!"search_swizzle() failed");
1003 * Set the sampler view's swizzle terms. This is used to handle RGBA
1004 * swizzling when the incoming image format isn't an exact match for
1005 * the actual texture format. For example, if we have glDrawPixels(
1006 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1007 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1008 * avoid swizzling all the pixels in software in the texstore code.
1011 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1013 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1014 const struct util_format_description
*desc
=
1015 util_format_description(sv
->texture
->format
);
1016 unsigned c0
, c1
, c2
, c3
;
1018 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1019 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1021 assert(desc
->block
.bits
== 32);
1023 /* invert the format's swizzle to setup the sampler's swizzle */
1024 if (format
== GL_RGBA
) {
1025 c0
= UTIL_FORMAT_SWIZZLE_X
;
1026 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1027 c2
= UTIL_FORMAT_SWIZZLE_Z
;
1028 c3
= UTIL_FORMAT_SWIZZLE_W
;
1031 assert(format
== GL_BGRA
);
1032 c0
= UTIL_FORMAT_SWIZZLE_Z
;
1033 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1034 c2
= UTIL_FORMAT_SWIZZLE_X
;
1035 c3
= UTIL_FORMAT_SWIZZLE_W
;
1037 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1038 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1039 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1040 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1043 /* use the default sampler swizzle */
1049 * Called via ctx->Driver.DrawPixels()
1052 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1053 GLsizei width
, GLsizei height
,
1054 GLenum format
, GLenum type
,
1055 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1057 void *driver_vp
, *driver_fp
;
1058 struct st_context
*st
= st_context(ctx
);
1059 struct pipe_context
*pipe
= st
->pipe
;
1060 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1061 struct pipe_sampler_view
*sv
[2] = { NULL
};
1062 int num_sampler_view
= 1;
1063 struct gl_pixelstore_attrib clippedUnpack
;
1064 struct st_fp_variant
*fpv
= NULL
;
1065 struct pipe_resource
*pt
;
1067 /* Mesa state should be up to date by now */
1068 assert(ctx
->NewState
== 0x0);
1070 st_flush_bitmap_cache(st
);
1072 st_validate_state(st
, ST_PIPELINE_RENDER
);
1074 /* Limit the size of the glDrawPixels to the max texture size.
1075 * Strictly speaking, that's not correct but since we don't handle
1076 * larger images yet, this is better than crashing.
1078 clippedUnpack
= *unpack
;
1079 unpack
= &clippedUnpack
;
1080 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1082 if (format
== GL_DEPTH_STENCIL
)
1083 write_stencil
= write_depth
= GL_TRUE
;
1084 else if (format
== GL_STENCIL_INDEX
)
1085 write_stencil
= GL_TRUE
;
1086 else if (format
== GL_DEPTH_COMPONENT
)
1087 write_depth
= GL_TRUE
;
1089 if (write_stencil
&&
1090 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1091 /* software fallback */
1092 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1098 * Get vertex/fragment shaders
1100 if (write_depth
|| write_stencil
) {
1101 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1103 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1106 fpv
= get_color_fp_variant(st
);
1108 driver_fp
= fpv
->driver_shader
;
1109 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1111 if (ctx
->Pixel
.MapColorFlag
) {
1112 pipe_sampler_view_reference(&sv
[1],
1113 st
->pixel_xfer
.pixelmap_sampler_view
);
1117 /* compiling a new fragment shader variant added new state constants
1118 * into the constant buffer, we need to update them
1120 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1121 PIPE_SHADER_FRAGMENT
);
1124 /* Put glDrawPixels image into a texture */
1125 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1127 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1131 /* create sampler view for the image */
1132 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1134 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1135 pipe_resource_reference(&pt
, NULL
);
1139 /* Set up the sampler view's swizzle */
1140 setup_sampler_swizzle(sv
[0], format
, type
);
1142 /* Create a second sampler view to read stencil. The stencil is
1143 * written using the shader stencil export functionality.
1145 if (write_stencil
) {
1146 enum pipe_format stencil_format
=
1147 util_format_stencil_only(pt
->format
);
1148 /* we should not be doing pixel map/transfer (see above) */
1149 assert(num_sampler_view
== 1);
1150 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1153 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1154 pipe_resource_reference(&pt
, NULL
);
1155 pipe_sampler_view_reference(&sv
[0], NULL
);
1161 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1163 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1168 ctx
->Current
.RasterColor
,
1169 GL_FALSE
, write_depth
, write_stencil
);
1170 pipe_sampler_view_reference(&sv
[0], NULL
);
1171 if (num_sampler_view
> 1)
1172 pipe_sampler_view_reference(&sv
[1], NULL
);
1174 pipe_resource_reference(&pt
, NULL
);
1180 * Software fallback for glCopyPixels(GL_STENCIL).
1183 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1184 GLsizei width
, GLsizei height
,
1185 GLint dstx
, GLint dsty
)
1187 struct st_renderbuffer
*rbDraw
;
1188 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1189 enum pipe_transfer_usage usage
;
1190 struct pipe_transfer
*ptDraw
;
1195 buffer
= malloc(width
* height
* sizeof(ubyte
));
1197 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1201 /* Get the dest renderbuffer */
1202 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1203 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1205 /* this will do stencil pixel transfer ops */
1206 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1207 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1208 &ctx
->DefaultPacking
, buffer
);
1211 /* debug code: dump stencil values */
1213 for (row
= 0; row
< height
; row
++) {
1214 printf("%3d: ", row
);
1215 for (col
= 0; col
< width
; col
++) {
1216 printf("%02x ", buffer
[col
+ row
* width
]);
1222 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1223 usage
= PIPE_TRANSFER_READ_WRITE
;
1225 usage
= PIPE_TRANSFER_WRITE
;
1227 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1228 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1231 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1232 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1234 /* map the stencil buffer */
1235 drawMap
= pipe_transfer_map(pipe
,
1237 rbDraw
->surface
->u
.tex
.level
,
1238 rbDraw
->surface
->u
.tex
.first_layer
,
1240 width
, height
, &ptDraw
);
1243 /* XXX PixelZoom not handled yet */
1244 for (i
= 0; i
< height
; i
++) {
1251 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1255 dst
= drawMap
+ y
* ptDraw
->stride
;
1256 src
= buffer
+ i
* width
;
1258 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1263 /* unmap the stencil buffer */
1264 pipe_transfer_unmap(pipe
, ptDraw
);
1269 * Return renderbuffer to use for reading color pixels for glCopyPixels
1271 static struct st_renderbuffer
*
1272 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1274 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1275 struct st_renderbuffer
*strb
=
1276 st_renderbuffer(fb
->_ColorReadBuffer
);
1283 * Try to do a glCopyPixels for simple cases with a blit by calling
1286 * We can do this when we're copying color pixels (depth/stencil
1287 * eventually) with no pixel zoom, no pixel transfer ops, no
1288 * per-fragment ops, and the src/dest regions don't overlap.
1291 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1292 GLsizei width
, GLsizei height
,
1293 GLint dstx
, GLint dsty
, GLenum type
)
1295 struct st_context
*st
= st_context(ctx
);
1296 struct pipe_context
*pipe
= st
->pipe
;
1297 struct pipe_screen
*screen
= pipe
->screen
;
1298 struct gl_pixelstore_attrib pack
, unpack
;
1299 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1301 if (type
== GL_COLOR
&&
1302 ctx
->Pixel
.ZoomX
== 1.0 &&
1303 ctx
->Pixel
.ZoomY
== 1.0 &&
1304 ctx
->_ImageTransferState
== 0x0 &&
1305 !ctx
->Color
.BlendEnabled
&&
1306 !ctx
->Color
.AlphaEnabled
&&
1307 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1309 !ctx
->Fog
.Enabled
&&
1310 !ctx
->Stencil
.Enabled
&&
1311 !ctx
->FragmentProgram
.Enabled
&&
1312 !ctx
->VertexProgram
.Enabled
&&
1313 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1314 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1315 !ctx
->Query
.CondRenderQuery
&&
1316 !ctx
->Query
.CurrentOcclusionObject
) {
1317 struct st_renderbuffer
*rbRead
, *rbDraw
;
1320 * Clip the read region against the src buffer bounds.
1321 * We'll still allocate a temporary buffer/texture for the original
1322 * src region size but we'll only read the region which is on-screen.
1323 * This may mean that we draw garbage pixels into the dest region, but
1330 pack
= ctx
->DefaultPacking
;
1331 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1332 return GL_TRUE
; /* all done */
1334 /* clip against dest buffer bounds and scissor box */
1335 drawX
= dstx
+ pack
.SkipPixels
;
1336 drawY
= dsty
+ pack
.SkipRows
;
1338 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1339 return GL_TRUE
; /* all done */
1341 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1342 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1347 rbRead
= st_get_color_read_renderbuffer(ctx
);
1348 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1350 /* Flip src/dst position depending on the orientation of buffers. */
1351 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1352 readY
= rbRead
->Base
.Height
- readY
;
1356 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1357 /* We can't flip the destination for pipe->blit, so we only adjust
1358 * its position and flip the source.
1360 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1365 if (rbRead
!= rbDraw
||
1366 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1367 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1368 struct pipe_blit_info blit
;
1370 memset(&blit
, 0, sizeof(blit
));
1371 blit
.src
.resource
= rbRead
->texture
;
1372 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1373 blit
.src
.format
= rbRead
->texture
->format
;
1374 blit
.src
.box
.x
= readX
;
1375 blit
.src
.box
.y
= readY
;
1376 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1377 blit
.src
.box
.width
= readW
;
1378 blit
.src
.box
.height
= readH
;
1379 blit
.src
.box
.depth
= 1;
1380 blit
.dst
.resource
= rbDraw
->texture
;
1381 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1382 blit
.dst
.format
= rbDraw
->texture
->format
;
1383 blit
.dst
.box
.x
= drawX
;
1384 blit
.dst
.box
.y
= drawY
;
1385 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1386 blit
.dst
.box
.width
= drawW
;
1387 blit
.dst
.box
.height
= drawH
;
1388 blit
.dst
.box
.depth
= 1;
1389 blit
.mask
= PIPE_MASK_RGBA
;
1390 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1392 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1393 blit
.src
.resource
->target
,
1394 blit
.src
.resource
->nr_samples
,
1395 PIPE_BIND_SAMPLER_VIEW
) &&
1396 screen
->is_format_supported(screen
, blit
.dst
.format
,
1397 blit
.dst
.resource
->target
,
1398 blit
.dst
.resource
->nr_samples
,
1399 PIPE_BIND_RENDER_TARGET
)) {
1400 pipe
->blit(pipe
, &blit
);
1411 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1412 GLsizei width
, GLsizei height
,
1413 GLint dstx
, GLint dsty
, GLenum type
)
1415 struct st_context
*st
= st_context(ctx
);
1416 struct pipe_context
*pipe
= st
->pipe
;
1417 struct pipe_screen
*screen
= pipe
->screen
;
1418 struct st_renderbuffer
*rbRead
;
1419 void *driver_vp
, *driver_fp
;
1420 struct pipe_resource
*pt
;
1421 struct pipe_sampler_view
*sv
[2] = { NULL
};
1422 struct st_fp_variant
*fpv
= NULL
;
1423 int num_sampler_view
= 1;
1424 enum pipe_format srcFormat
;
1426 GLboolean invertTex
= GL_FALSE
;
1427 GLint readX
, readY
, readW
, readH
;
1428 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1430 st_flush_bitmap_cache(st
);
1432 st_validate_state(st
, ST_PIPELINE_RENDER
);
1434 if (type
== GL_DEPTH_STENCIL
) {
1435 /* XXX make this more efficient */
1436 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1437 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1441 if (type
== GL_STENCIL
) {
1442 /* can't use texturing to do stencil */
1443 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1447 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1451 * The subsequent code implements glCopyPixels by copying the source
1452 * pixels into a temporary texture that's then applied to a textured quad.
1453 * When we draw the textured quad, all the usual per-fragment operations
1459 * Get vertex/fragment shaders
1461 if (type
== GL_COLOR
) {
1462 fpv
= get_color_fp_variant(st
);
1464 rbRead
= st_get_color_read_renderbuffer(ctx
);
1466 driver_fp
= fpv
->driver_shader
;
1467 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1469 if (ctx
->Pixel
.MapColorFlag
) {
1470 pipe_sampler_view_reference(&sv
[1],
1471 st
->pixel_xfer
.pixelmap_sampler_view
);
1475 /* compiling a new fragment shader variant added new state constants
1476 * into the constant buffer, we need to update them
1478 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1479 PIPE_SHADER_FRAGMENT
);
1482 assert(type
== GL_DEPTH
);
1483 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1484 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1486 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1487 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1490 /* Choose the format for the temporary texture. */
1491 srcFormat
= rbRead
->texture
->format
;
1492 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1493 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1495 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1497 /* srcFormat is non-renderable. Find a compatible renderable format. */
1498 if (type
== GL_DEPTH
) {
1499 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1500 GL_NONE
, st
->internal_target
, 0,
1504 assert(type
== GL_COLOR
);
1506 if (util_format_is_float(srcFormat
)) {
1507 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1508 GL_NONE
, st
->internal_target
, 0,
1511 else if (util_format_is_pure_sint(srcFormat
)) {
1512 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1513 GL_NONE
, st
->internal_target
, 0,
1516 else if (util_format_is_pure_uint(srcFormat
)) {
1517 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1518 GL_NONE
, st
->internal_target
, 0,
1521 else if (util_format_is_snorm(srcFormat
)) {
1522 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1523 GL_NONE
, st
->internal_target
, 0,
1527 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1528 GL_NONE
, st
->internal_target
, 0,
1533 if (srcFormat
== PIPE_FORMAT_NONE
) {
1534 assert(0 && "cannot choose a format for src of CopyPixels");
1539 /* Invert src region if needed */
1540 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1541 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1542 invertTex
= !invertTex
;
1545 /* Clip the read region against the src buffer bounds.
1546 * We'll still allocate a temporary buffer/texture for the original
1547 * src region size but we'll only read the region which is on-screen.
1548 * This may mean that we draw garbage pixels into the dest region, but
1555 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1556 /* The source region is completely out of bounds. Do nothing.
1557 * The GL spec says "Results of copies from outside the window,
1558 * or from regions of the window that are not exposed, are
1559 * hardware dependent and undefined."
1564 readW
= MAX2(0, readW
);
1565 readH
= MAX2(0, readH
);
1567 /* Allocate the temporary texture. */
1568 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1572 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1574 pipe_resource_reference(&pt
, NULL
);
1578 /* Copy the src region to the temporary texture. */
1580 struct pipe_blit_info blit
;
1582 memset(&blit
, 0, sizeof(blit
));
1583 blit
.src
.resource
= rbRead
->texture
;
1584 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1585 blit
.src
.format
= rbRead
->texture
->format
;
1586 blit
.src
.box
.x
= readX
;
1587 blit
.src
.box
.y
= readY
;
1588 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1589 blit
.src
.box
.width
= readW
;
1590 blit
.src
.box
.height
= readH
;
1591 blit
.src
.box
.depth
= 1;
1592 blit
.dst
.resource
= pt
;
1594 blit
.dst
.format
= pt
->format
;
1595 blit
.dst
.box
.x
= pack
.SkipPixels
;
1596 blit
.dst
.box
.y
= pack
.SkipRows
;
1598 blit
.dst
.box
.width
= readW
;
1599 blit
.dst
.box
.height
= readH
;
1600 blit
.dst
.box
.depth
= 1;
1601 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1602 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1604 pipe
->blit(pipe
, &blit
);
1607 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1608 * textured quad with that texture.
1610 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1611 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1616 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
1617 invertTex
, GL_FALSE
, GL_FALSE
);
1619 pipe_resource_reference(&pt
, NULL
);
1620 pipe_sampler_view_reference(&sv
[0], NULL
);
1625 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1627 functions
->DrawPixels
= st_DrawPixels
;
1628 functions
->CopyPixels
= st_CopyPixels
;
1633 st_destroy_drawpix(struct st_context
*st
)
1637 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1638 if (st
->drawpix
.zs_shaders
[i
])
1639 cso_delete_fragment_shader(st
->cso_context
,
1640 st
->drawpix
.zs_shaders
[i
]);
1643 if (st
->drawpix
.vert_shaders
[0])
1644 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1645 if (st
->drawpix
.vert_shaders
[1])
1646 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);