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 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_context
*pipe
= st
->pipe
;
456 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
457 struct pipe_resource
*buf
= NULL
;
460 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
461 &buf
, (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
;
481 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
482 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
485 verts
[1][0][0] = clip_x1
;
486 verts
[1][0][1] = clip_y0
;
489 verts
[2][0][0] = clip_x1
;
490 verts
[2][0][1] = clip_y1
;
493 verts
[3][0][0] = clip_x0
;
494 verts
[3][0][1] = clip_y1
;
496 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
497 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
498 verts
[1][1][0] = sRight
;
499 verts
[1][1][1] = tTop
;
500 verts
[2][1][0] = sRight
;
501 verts
[2][1][1] = tBot
;
502 verts
[3][1][0] = sLeft
;
503 verts
[3][1][1] = tBot
;
505 /* same for all verts: */
507 for (i
= 0; i
< 4; i
++) {
508 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
509 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
510 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
511 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
512 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
513 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
514 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
515 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
519 for (i
= 0; i
< 4; i
++) {
520 verts
[i
][0][2] = z
; /*Z*/
521 verts
[i
][0][3] = 1.0f
; /*W*/
522 verts
[i
][1][2] = 0.0f
; /*R*/
523 verts
[i
][1][3] = 1.0f
; /*Q*/
528 u_upload_unmap(st
->uploader
);
529 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
530 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
534 3); /* attribs/vert */
535 pipe_resource_reference(&buf
, NULL
);
541 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
542 GLsizei width
, GLsizei height
,
543 GLfloat zoomX
, GLfloat zoomY
,
544 struct pipe_sampler_view
**sv
,
545 int num_sampler_view
,
548 struct st_fp_variant
*fpv
,
549 const GLfloat
*color
,
551 GLboolean write_depth
, GLboolean write_stencil
)
553 struct st_context
*st
= st_context(ctx
);
554 struct pipe_context
*pipe
= st
->pipe
;
555 struct cso_context
*cso
= st
->cso_context
;
556 GLfloat x0
, y0
, x1
, y1
;
558 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
561 /* XXX if DrawPixels image is larger than max texture size, break
564 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
565 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
566 assert(width
<= maxSize
);
567 assert(height
<= maxSize
);
569 cso_save_rasterizer(cso
);
570 cso_save_viewport(cso
);
571 cso_save_fragment_samplers(cso
);
572 cso_save_fragment_sampler_views(cso
);
573 cso_save_fragment_shader(cso
);
574 cso_save_stream_outputs(cso
);
575 cso_save_vertex_shader(cso
);
576 cso_save_tessctrl_shader(cso
);
577 cso_save_tesseval_shader(cso
);
578 cso_save_geometry_shader(cso
);
579 cso_save_vertex_elements(cso
);
580 cso_save_aux_vertex_buffer_slot(cso
);
582 cso_save_depth_stencil_alpha(cso
);
586 /* rasterizer state: just scissor */
588 struct pipe_rasterizer_state rasterizer
;
589 memset(&rasterizer
, 0, sizeof(rasterizer
));
590 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
591 ctx
->Color
._ClampFragmentColor
;
592 rasterizer
.half_pixel_center
= 1;
593 rasterizer
.bottom_edge_rule
= 1;
594 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
595 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
596 cso_set_rasterizer(cso
, &rasterizer
);
600 /* Stencil writing bypasses the normal fragment pipeline to
601 * disable color writing and set stencil test to always pass.
603 struct pipe_depth_stencil_alpha_state dsa
;
604 struct pipe_blend_state blend
;
607 memset(&dsa
, 0, sizeof(dsa
));
608 dsa
.stencil
[0].enabled
= 1;
609 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
610 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
611 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
613 /* writing depth+stencil: depth test always passes */
614 dsa
.depth
.enabled
= 1;
615 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
616 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
618 cso_set_depth_stencil_alpha(cso
, &dsa
);
620 /* blend (colormask) */
621 memset(&blend
, 0, sizeof(blend
));
622 cso_set_blend(cso
, &blend
);
625 /* fragment shader state: TEX lookup program */
626 cso_set_fragment_shader_handle(cso
, driver_fp
);
628 /* vertex shader state: position + texcoord pass-through */
629 cso_set_vertex_shader_handle(cso
, driver_vp
);
631 /* disable other shaders */
632 cso_set_tessctrl_shader_handle(cso
, NULL
);
633 cso_set_tesseval_shader_handle(cso
, NULL
);
634 cso_set_geometry_shader_handle(cso
, NULL
);
636 /* user samplers, plus the drawpix samplers */
638 struct pipe_sampler_state sampler
;
640 memset(&sampler
, 0, sizeof(sampler
));
641 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
642 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
643 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
644 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
645 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
646 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
647 sampler
.normalized_coords
= normalized
;
650 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
651 uint num
= MAX2(MAX2(fpv
->drawpix_sampler
, fpv
->pixelmap_sampler
) + 1,
652 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
655 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
656 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
658 samplers
[fpv
->drawpix_sampler
] = &sampler
;
660 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
662 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
664 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
666 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
670 /* viewport state: viewport matching window dims */
672 const float w
= (float) ctx
->DrawBuffer
->Width
;
673 const float h
= (float) ctx
->DrawBuffer
->Height
;
674 struct pipe_viewport_state vp
;
675 vp
.scale
[0] = 0.5f
* w
;
676 vp
.scale
[1] = -0.5f
* h
;
678 vp
.translate
[0] = 0.5f
* w
;
679 vp
.translate
[1] = 0.5f
* h
;
680 vp
.translate
[2] = 0.5f
;
681 cso_set_viewport(cso
, &vp
);
684 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
685 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
687 /* user textures, plus the drawpix textures */
689 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
690 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
691 fpv
->pixelmap_sampler
+ 1,
692 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
694 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
695 sizeof(sampler_views
));
697 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
699 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
700 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
702 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
704 /* Compute Gallium window coords (y=0=top) with pixel zoom.
705 * Recall that these coords are transformed by the current
706 * vertex shader and viewport transformation.
708 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
709 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
710 invertTex
= !invertTex
;
714 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
716 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
718 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
721 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
722 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
723 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
726 cso_restore_rasterizer(cso
);
727 cso_restore_viewport(cso
);
728 cso_restore_fragment_samplers(cso
);
729 cso_restore_fragment_sampler_views(cso
);
730 cso_restore_fragment_shader(cso
);
731 cso_restore_vertex_shader(cso
);
732 cso_restore_tessctrl_shader(cso
);
733 cso_restore_tesseval_shader(cso
);
734 cso_restore_geometry_shader(cso
);
735 cso_restore_vertex_elements(cso
);
736 cso_restore_aux_vertex_buffer_slot(cso
);
737 cso_restore_stream_outputs(cso
);
739 cso_restore_depth_stencil_alpha(cso
);
740 cso_restore_blend(cso
);
746 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
747 * can't use a fragment shader to write stencil values.
750 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
751 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
752 const struct gl_pixelstore_attrib
*unpack
,
753 const GLvoid
*pixels
)
755 struct st_context
*st
= st_context(ctx
);
756 struct pipe_context
*pipe
= st
->pipe
;
757 struct st_renderbuffer
*strb
;
758 enum pipe_transfer_usage usage
;
759 struct pipe_transfer
*pt
;
760 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
762 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
767 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
769 /* totally clipped */
774 strb
= st_renderbuffer(ctx
->DrawBuffer
->
775 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
777 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
778 y
= ctx
->DrawBuffer
->Height
- y
- height
;
781 if (format
== GL_STENCIL_INDEX
&&
782 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
783 /* writing stencil to a combined depth+stencil buffer */
784 usage
= PIPE_TRANSFER_READ_WRITE
;
787 usage
= PIPE_TRANSFER_WRITE
;
790 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
791 strb
->surface
->u
.tex
.level
,
792 strb
->surface
->u
.tex
.first_layer
,
796 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
799 sValues
= malloc(width
* sizeof(GLubyte
));
800 zValues
= malloc(width
* sizeof(GLuint
));
802 if (sValues
&& zValues
) {
804 for (row
= 0; row
< height
; row
++) {
805 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
806 GLenum destType
= GL_UNSIGNED_BYTE
;
807 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
811 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
812 type
, source
, &clippedUnpack
,
813 ctx
->_ImageTransferState
);
815 if (format
== GL_DEPTH_STENCIL
) {
817 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
818 GL_FLOAT
: GL_UNSIGNED_INT
;
820 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
821 (1 << 24) - 1, type
, source
,
826 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
827 "zoom not complete");
833 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
834 spanY
= height
- row
- 1;
840 /* now pack the stencil (and Z) values in the dest format */
841 switch (pt
->resource
->format
) {
842 case PIPE_FORMAT_S8_UINT
:
844 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
845 assert(usage
== PIPE_TRANSFER_WRITE
);
846 memcpy(dest
, sValues
, width
);
849 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
850 if (format
== GL_DEPTH_STENCIL
) {
851 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
853 assert(usage
== PIPE_TRANSFER_WRITE
);
854 for (k
= 0; k
< width
; k
++) {
855 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
859 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
861 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
862 for (k
= 0; k
< width
; k
++) {
863 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
867 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
868 if (format
== GL_DEPTH_STENCIL
) {
869 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
871 assert(usage
== PIPE_TRANSFER_WRITE
);
872 for (k
= 0; k
< width
; k
++) {
873 dest
[k
] = (zValues
[k
] << 8) | (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
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
885 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
886 if (format
== GL_DEPTH_STENCIL
) {
887 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
888 GLfloat
*destf
= (GLfloat
*)dest
;
890 assert(usage
== PIPE_TRANSFER_WRITE
);
891 for (k
= 0; k
< width
; k
++) {
892 destf
[k
*2] = zValuesFloat
[k
];
893 dest
[k
*2+1] = sValues
[k
] & 0xff;
897 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
899 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
900 for (k
= 0; k
< width
; k
++) {
901 dest
[k
*2+1] = sValues
[k
] & 0xff;
912 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
918 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
920 /* unmap the stencil buffer */
921 pipe_transfer_unmap(pipe
, pt
);
926 * Get fragment program variant for a glDrawPixels or glCopyPixels
927 * command for RGBA data.
929 static struct st_fp_variant
*
930 get_color_fp_variant(struct st_context
*st
)
932 struct gl_context
*ctx
= st
->ctx
;
933 struct st_fp_variant_key key
;
934 struct st_fp_variant
*fpv
;
936 memset(&key
, 0, sizeof(key
));
938 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
940 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
941 ctx
->Pixel
.RedScale
!= 1.0 ||
942 ctx
->Pixel
.GreenBias
!= 0.0 ||
943 ctx
->Pixel
.GreenScale
!= 1.0 ||
944 ctx
->Pixel
.BlueBias
!= 0.0 ||
945 ctx
->Pixel
.BlueScale
!= 1.0 ||
946 ctx
->Pixel
.AlphaBias
!= 0.0 ||
947 ctx
->Pixel
.AlphaScale
!= 1.0);
948 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
949 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
950 st
->ctx
->Color
._ClampFragmentColor
;
952 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
959 * Clamp glDrawPixels width and height to the maximum texture size.
962 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
963 struct gl_pixelstore_attrib
*unpack
)
966 1 << (pipe
->screen
->get_param(pipe
->screen
,
967 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
969 if (*width
> maxSize
) {
970 if (unpack
->RowLength
== 0)
971 unpack
->RowLength
= *width
;
974 if (*height
> maxSize
) {
981 * Search the array of 4 swizzle components for the named component and return
985 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
988 for (i
= 0; i
< 4; i
++) {
989 if (swizzle
[i
] == component
)
992 assert(!"search_swizzle() failed");
998 * Set the sampler view's swizzle terms. This is used to handle RGBA
999 * swizzling when the incoming image format isn't an exact match for
1000 * the actual texture format. For example, if we have glDrawPixels(
1001 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1002 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1003 * avoid swizzling all the pixels in software in the texstore code.
1006 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1008 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1009 const struct util_format_description
*desc
=
1010 util_format_description(sv
->texture
->format
);
1011 unsigned c0
, c1
, c2
, c3
;
1013 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1014 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1016 assert(desc
->block
.bits
== 32);
1018 /* invert the format's swizzle to setup the sampler's swizzle */
1019 if (format
== GL_RGBA
) {
1020 c0
= UTIL_FORMAT_SWIZZLE_X
;
1021 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1022 c2
= UTIL_FORMAT_SWIZZLE_Z
;
1023 c3
= UTIL_FORMAT_SWIZZLE_W
;
1026 assert(format
== GL_BGRA
);
1027 c0
= UTIL_FORMAT_SWIZZLE_Z
;
1028 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1029 c2
= UTIL_FORMAT_SWIZZLE_X
;
1030 c3
= UTIL_FORMAT_SWIZZLE_W
;
1032 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1033 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1034 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1035 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1038 /* use the default sampler swizzle */
1044 * Called via ctx->Driver.DrawPixels()
1047 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1048 GLsizei width
, GLsizei height
,
1049 GLenum format
, GLenum type
,
1050 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1052 void *driver_vp
, *driver_fp
;
1053 struct st_context
*st
= st_context(ctx
);
1054 const GLfloat
*color
;
1055 struct pipe_context
*pipe
= st
->pipe
;
1056 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1057 struct pipe_sampler_view
*sv
[2] = { NULL
};
1058 int num_sampler_view
= 1;
1059 struct gl_pixelstore_attrib clippedUnpack
;
1060 struct st_fp_variant
*fpv
= NULL
;
1061 struct pipe_resource
*pt
;
1063 /* Mesa state should be up to date by now */
1064 assert(ctx
->NewState
== 0x0);
1066 st_validate_state(st
);
1068 /* Limit the size of the glDrawPixels to the max texture size.
1069 * Strictly speaking, that's not correct but since we don't handle
1070 * larger images yet, this is better than crashing.
1072 clippedUnpack
= *unpack
;
1073 unpack
= &clippedUnpack
;
1074 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1076 if (format
== GL_DEPTH_STENCIL
)
1077 write_stencil
= write_depth
= GL_TRUE
;
1078 else if (format
== GL_STENCIL_INDEX
)
1079 write_stencil
= GL_TRUE
;
1080 else if (format
== GL_DEPTH_COMPONENT
)
1081 write_depth
= GL_TRUE
;
1083 if (write_stencil
&&
1084 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1085 /* software fallback */
1086 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1092 * Get vertex/fragment shaders
1094 if (write_depth
|| write_stencil
) {
1095 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1097 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1098 color
= ctx
->Current
.RasterColor
;
1101 fpv
= get_color_fp_variant(st
);
1103 driver_fp
= fpv
->driver_shader
;
1104 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1107 if (ctx
->Pixel
.MapColorFlag
) {
1108 pipe_sampler_view_reference(&sv
[1],
1109 st
->pixel_xfer
.pixelmap_sampler_view
);
1113 /* update fragment program constants */
1114 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1117 /* Put glDrawPixels image into a texture */
1118 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1120 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1124 /* create sampler view for the image */
1125 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1127 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1128 pipe_resource_reference(&pt
, NULL
);
1132 /* Set up the sampler view's swizzle */
1133 setup_sampler_swizzle(sv
[0], format
, type
);
1135 /* Create a second sampler view to read stencil. The stencil is
1136 * written using the shader stencil export functionality.
1138 if (write_stencil
) {
1139 enum pipe_format stencil_format
=
1140 util_format_stencil_only(pt
->format
);
1141 /* we should not be doing pixel map/transfer (see above) */
1142 assert(num_sampler_view
== 1);
1143 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1146 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1147 pipe_resource_reference(&pt
, NULL
);
1148 pipe_sampler_view_reference(&sv
[0], NULL
);
1154 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1156 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1161 color
, GL_FALSE
, write_depth
, write_stencil
);
1162 pipe_sampler_view_reference(&sv
[0], NULL
);
1163 if (num_sampler_view
> 1)
1164 pipe_sampler_view_reference(&sv
[1], NULL
);
1166 pipe_resource_reference(&pt
, NULL
);
1172 * Software fallback for glCopyPixels(GL_STENCIL).
1175 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1176 GLsizei width
, GLsizei height
,
1177 GLint dstx
, GLint dsty
)
1179 struct st_renderbuffer
*rbDraw
;
1180 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1181 enum pipe_transfer_usage usage
;
1182 struct pipe_transfer
*ptDraw
;
1187 buffer
= malloc(width
* height
* sizeof(ubyte
));
1189 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1193 /* Get the dest renderbuffer */
1194 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1195 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1197 /* this will do stencil pixel transfer ops */
1198 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1199 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1200 &ctx
->DefaultPacking
, buffer
);
1203 /* debug code: dump stencil values */
1205 for (row
= 0; row
< height
; row
++) {
1206 printf("%3d: ", row
);
1207 for (col
= 0; col
< width
; col
++) {
1208 printf("%02x ", buffer
[col
+ row
* width
]);
1214 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1215 usage
= PIPE_TRANSFER_READ_WRITE
;
1217 usage
= PIPE_TRANSFER_WRITE
;
1219 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1220 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1223 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1224 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1226 /* map the stencil buffer */
1227 drawMap
= pipe_transfer_map(pipe
,
1229 rbDraw
->surface
->u
.tex
.level
,
1230 rbDraw
->surface
->u
.tex
.first_layer
,
1232 width
, height
, &ptDraw
);
1235 /* XXX PixelZoom not handled yet */
1236 for (i
= 0; i
< height
; i
++) {
1243 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1247 dst
= drawMap
+ y
* ptDraw
->stride
;
1248 src
= buffer
+ i
* width
;
1250 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1255 /* unmap the stencil buffer */
1256 pipe_transfer_unmap(pipe
, ptDraw
);
1261 * Return renderbuffer to use for reading color pixels for glCopyPixels
1263 static struct st_renderbuffer
*
1264 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1266 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1267 struct st_renderbuffer
*strb
=
1268 st_renderbuffer(fb
->_ColorReadBuffer
);
1275 * Try to do a glCopyPixels for simple cases with a blit by calling
1278 * We can do this when we're copying color pixels (depth/stencil
1279 * eventually) with no pixel zoom, no pixel transfer ops, no
1280 * per-fragment ops, and the src/dest regions don't overlap.
1283 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1284 GLsizei width
, GLsizei height
,
1285 GLint dstx
, GLint dsty
, GLenum type
)
1287 struct st_context
*st
= st_context(ctx
);
1288 struct pipe_context
*pipe
= st
->pipe
;
1289 struct pipe_screen
*screen
= pipe
->screen
;
1290 struct gl_pixelstore_attrib pack
, unpack
;
1291 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1293 if (type
== GL_COLOR
&&
1294 ctx
->Pixel
.ZoomX
== 1.0 &&
1295 ctx
->Pixel
.ZoomY
== 1.0 &&
1296 ctx
->_ImageTransferState
== 0x0 &&
1297 !ctx
->Color
.BlendEnabled
&&
1298 !ctx
->Color
.AlphaEnabled
&&
1300 !ctx
->Fog
.Enabled
&&
1301 !ctx
->Stencil
.Enabled
&&
1302 !ctx
->FragmentProgram
.Enabled
&&
1303 !ctx
->VertexProgram
.Enabled
&&
1304 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1305 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1306 !ctx
->Query
.CondRenderQuery
&&
1307 !ctx
->Query
.CurrentOcclusionObject
) {
1308 struct st_renderbuffer
*rbRead
, *rbDraw
;
1311 * Clip the read region against the src buffer bounds.
1312 * We'll still allocate a temporary buffer/texture for the original
1313 * src region size but we'll only read the region which is on-screen.
1314 * This may mean that we draw garbage pixels into the dest region, but
1321 pack
= ctx
->DefaultPacking
;
1322 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1323 return GL_TRUE
; /* all done */
1325 /* clip against dest buffer bounds and scissor box */
1326 drawX
= dstx
+ pack
.SkipPixels
;
1327 drawY
= dsty
+ pack
.SkipRows
;
1329 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1330 return GL_TRUE
; /* all done */
1332 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1333 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1338 rbRead
= st_get_color_read_renderbuffer(ctx
);
1339 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1341 /* Flip src/dst position depending on the orientation of buffers. */
1342 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1343 readY
= rbRead
->Base
.Height
- readY
;
1347 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1348 /* We can't flip the destination for pipe->blit, so we only adjust
1349 * its position and flip the source.
1351 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1356 if (rbRead
!= rbDraw
||
1357 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1358 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1359 struct pipe_blit_info blit
;
1361 memset(&blit
, 0, sizeof(blit
));
1362 blit
.src
.resource
= rbRead
->texture
;
1363 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1364 blit
.src
.format
= rbRead
->texture
->format
;
1365 blit
.src
.box
.x
= readX
;
1366 blit
.src
.box
.y
= readY
;
1367 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1368 blit
.src
.box
.width
= readW
;
1369 blit
.src
.box
.height
= readH
;
1370 blit
.src
.box
.depth
= 1;
1371 blit
.dst
.resource
= rbDraw
->texture
;
1372 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1373 blit
.dst
.format
= rbDraw
->texture
->format
;
1374 blit
.dst
.box
.x
= drawX
;
1375 blit
.dst
.box
.y
= drawY
;
1376 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1377 blit
.dst
.box
.width
= drawW
;
1378 blit
.dst
.box
.height
= drawH
;
1379 blit
.dst
.box
.depth
= 1;
1380 blit
.mask
= PIPE_MASK_RGBA
;
1381 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1383 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1384 blit
.src
.resource
->target
,
1385 blit
.src
.resource
->nr_samples
,
1386 PIPE_BIND_SAMPLER_VIEW
) &&
1387 screen
->is_format_supported(screen
, blit
.dst
.format
,
1388 blit
.dst
.resource
->target
,
1389 blit
.dst
.resource
->nr_samples
,
1390 PIPE_BIND_RENDER_TARGET
)) {
1391 pipe
->blit(pipe
, &blit
);
1402 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1403 GLsizei width
, GLsizei height
,
1404 GLint dstx
, GLint dsty
, GLenum type
)
1406 struct st_context
*st
= st_context(ctx
);
1407 struct pipe_context
*pipe
= st
->pipe
;
1408 struct pipe_screen
*screen
= pipe
->screen
;
1409 struct st_renderbuffer
*rbRead
;
1410 void *driver_vp
, *driver_fp
;
1411 struct pipe_resource
*pt
;
1412 struct pipe_sampler_view
*sv
[2] = { NULL
};
1413 struct st_fp_variant
*fpv
= NULL
;
1414 int num_sampler_view
= 1;
1416 enum pipe_format srcFormat
;
1418 GLboolean invertTex
= GL_FALSE
;
1419 GLint readX
, readY
, readW
, readH
;
1420 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1422 st_validate_state(st
);
1424 if (type
== GL_DEPTH_STENCIL
) {
1425 /* XXX make this more efficient */
1426 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1427 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1431 if (type
== GL_STENCIL
) {
1432 /* can't use texturing to do stencil */
1433 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1437 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1441 * The subsequent code implements glCopyPixels by copying the source
1442 * pixels into a temporary texture that's then applied to a textured quad.
1443 * When we draw the textured quad, all the usual per-fragment operations
1449 * Get vertex/fragment shaders
1451 if (type
== GL_COLOR
) {
1452 fpv
= get_color_fp_variant(st
);
1454 rbRead
= st_get_color_read_renderbuffer(ctx
);
1457 driver_fp
= fpv
->driver_shader
;
1458 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1460 if (ctx
->Pixel
.MapColorFlag
) {
1461 pipe_sampler_view_reference(&sv
[1],
1462 st
->pixel_xfer
.pixelmap_sampler_view
);
1466 /* update fragment program constants */
1467 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1470 assert(type
== GL_DEPTH
);
1471 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1472 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1473 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1475 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1476 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1479 /* Choose the format for the temporary texture. */
1480 srcFormat
= rbRead
->texture
->format
;
1481 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1482 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1484 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1486 /* srcFormat is non-renderable. Find a compatible renderable format. */
1487 if (type
== GL_DEPTH
) {
1488 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1489 GL_NONE
, st
->internal_target
, 0,
1493 assert(type
== GL_COLOR
);
1495 if (util_format_is_float(srcFormat
)) {
1496 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1497 GL_NONE
, st
->internal_target
, 0,
1500 else if (util_format_is_pure_sint(srcFormat
)) {
1501 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1502 GL_NONE
, st
->internal_target
, 0,
1505 else if (util_format_is_pure_uint(srcFormat
)) {
1506 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1507 GL_NONE
, st
->internal_target
, 0,
1510 else if (util_format_is_snorm(srcFormat
)) {
1511 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1512 GL_NONE
, st
->internal_target
, 0,
1516 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1517 GL_NONE
, st
->internal_target
, 0,
1522 if (srcFormat
== PIPE_FORMAT_NONE
) {
1523 assert(0 && "cannot choose a format for src of CopyPixels");
1528 /* Invert src region if needed */
1529 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1530 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1531 invertTex
= !invertTex
;
1534 /* Clip the read region against the src buffer bounds.
1535 * We'll still allocate a temporary buffer/texture for the original
1536 * src region size but we'll only read the region which is on-screen.
1537 * This may mean that we draw garbage pixels into the dest region, but
1544 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1545 /* The source region is completely out of bounds. Do nothing.
1546 * The GL spec says "Results of copies from outside the window,
1547 * or from regions of the window that are not exposed, are
1548 * hardware dependent and undefined."
1553 readW
= MAX2(0, readW
);
1554 readH
= MAX2(0, readH
);
1556 /* Allocate the temporary texture. */
1557 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1561 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1563 pipe_resource_reference(&pt
, NULL
);
1567 /* Copy the src region to the temporary texture. */
1569 struct pipe_blit_info blit
;
1571 memset(&blit
, 0, sizeof(blit
));
1572 blit
.src
.resource
= rbRead
->texture
;
1573 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1574 blit
.src
.format
= rbRead
->texture
->format
;
1575 blit
.src
.box
.x
= readX
;
1576 blit
.src
.box
.y
= readY
;
1577 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1578 blit
.src
.box
.width
= readW
;
1579 blit
.src
.box
.height
= readH
;
1580 blit
.src
.box
.depth
= 1;
1581 blit
.dst
.resource
= pt
;
1583 blit
.dst
.format
= pt
->format
;
1584 blit
.dst
.box
.x
= pack
.SkipPixels
;
1585 blit
.dst
.box
.y
= pack
.SkipRows
;
1587 blit
.dst
.box
.width
= readW
;
1588 blit
.dst
.box
.height
= readH
;
1589 blit
.dst
.box
.depth
= 1;
1590 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1591 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1593 pipe
->blit(pipe
, &blit
);
1596 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1597 * textured quad with that texture.
1599 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1600 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1605 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1607 pipe_resource_reference(&pt
, NULL
);
1608 pipe_sampler_view_reference(&sv
[0], NULL
);
1613 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1615 functions
->DrawPixels
= st_DrawPixels
;
1616 functions
->CopyPixels
= st_CopyPixels
;
1621 st_destroy_drawpix(struct st_context
*st
)
1625 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1626 if (st
->drawpix
.zs_shaders
[i
])
1627 cso_delete_fragment_shader(st
->cso_context
,
1628 st
->drawpix
.zs_shaders
[i
]);
1631 if (st
->drawpix
.vert_shaders
[0])
1632 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1633 if (st
->drawpix
.vert_shaders
[1])
1634 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);