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_draw_quad.h"
67 #include "util/u_format.h"
68 #include "util/u_inlines.h"
69 #include "util/u_math.h"
70 #include "util/u_tile.h"
71 #include "util/u_upload_mgr.h"
72 #include "cso_cache/cso_context.h"
76 * Create fragment program that does a TEX() instruction to get a Z and/or
77 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
78 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
79 * Pass fragment color through as-is.
81 * \return CSO of the fragment shader.
84 get_drawpix_z_stencil_program(struct st_context
*st
,
85 GLboolean write_depth
,
86 GLboolean write_stencil
)
88 struct ureg_program
*ureg
;
89 struct ureg_src depth_sampler
, stencil_sampler
;
90 struct ureg_src texcoord
, color
;
91 struct ureg_dst out_color
, out_depth
, out_stencil
;
92 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
95 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
97 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
98 /* already have the proper shader */
99 return st
->drawpix
.zs_shaders
[shaderIndex
];
102 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
106 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
109 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
110 TGSI_INTERPOLATE_COLOR
);
111 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
113 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
114 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
118 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
119 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
122 texcoord
= ureg_DECL_fs_input(ureg
,
123 st
->needs_texcoord_semantic
?
124 TGSI_SEMANTIC_TEXCOORD
:
125 TGSI_SEMANTIC_GENERIC
,
126 0, TGSI_INTERPOLATE_LINEAR
);
129 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
130 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
131 ureg_MOV(ureg
, out_color
, color
);
135 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
136 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
139 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
141 /* save the new shader */
142 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
148 * Create a simple vertex shader that just passes through the
149 * vertex position and texcoord (and optionally, color).
152 make_passthrough_vertex_shader(struct st_context
*st
,
155 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
156 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
158 if (!st
->drawpix
.vert_shaders
[passColor
]) {
159 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
164 /* MOV result.pos, vertex.pos; */
166 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
167 ureg_DECL_vs_input( ureg
, 0 ));
169 /* MOV result.texcoord0, vertex.attr[1]; */
171 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
172 ureg_DECL_vs_input( ureg
, 1 ));
175 /* MOV result.color0, vertex.attr[2]; */
177 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
178 ureg_DECL_vs_input( ureg
, 2 ));
183 st
->drawpix
.vert_shaders
[passColor
] =
184 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
187 return st
->drawpix
.vert_shaders
[passColor
];
192 * Return a texture internalFormat for drawing/copying an image
193 * of the given format and type.
196 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
199 case GL_DEPTH_COMPONENT
:
201 case GL_UNSIGNED_SHORT
:
202 return GL_DEPTH_COMPONENT16
;
204 case GL_UNSIGNED_INT
:
205 return GL_DEPTH_COMPONENT32
;
208 if (ctx
->Extensions
.ARB_depth_buffer_float
)
209 return GL_DEPTH_COMPONENT32F
;
211 return GL_DEPTH_COMPONENT
;
214 return GL_DEPTH_COMPONENT
;
217 case GL_DEPTH_STENCIL
:
219 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
220 return GL_DEPTH32F_STENCIL8
;
222 case GL_UNSIGNED_INT_24_8
:
224 return GL_DEPTH24_STENCIL8
;
227 case GL_STENCIL_INDEX
:
228 return GL_STENCIL_INDEX
;
231 if (_mesa_is_enum_format_integer(format
)) {
235 case GL_UNSIGNED_BYTE
:
239 case GL_UNSIGNED_SHORT
:
243 case GL_UNSIGNED_INT
:
246 assert(0 && "Unexpected type in internal_format()");
247 return GL_RGBA_INTEGER
;
252 case GL_UNSIGNED_BYTE
:
253 case GL_UNSIGNED_INT_8_8_8_8
:
254 case GL_UNSIGNED_INT_8_8_8_8_REV
:
258 case GL_UNSIGNED_BYTE_3_3_2
:
259 case GL_UNSIGNED_BYTE_2_3_3_REV
:
262 case GL_UNSIGNED_SHORT_4_4_4_4
:
263 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
266 case GL_UNSIGNED_SHORT_5_6_5
:
267 case GL_UNSIGNED_SHORT_5_6_5_REV
:
270 case GL_UNSIGNED_SHORT_5_5_5_1
:
271 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
274 case GL_UNSIGNED_INT_10_10_10_2
:
275 case GL_UNSIGNED_INT_2_10_10_10_REV
:
278 case GL_UNSIGNED_SHORT
:
279 case GL_UNSIGNED_INT
:
284 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
289 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
291 case GL_HALF_FLOAT_ARB
:
293 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
294 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
299 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
300 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
302 case GL_UNSIGNED_INT_5_9_9_9_REV
:
303 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
306 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
307 assert(ctx
->Extensions
.EXT_packed_float
);
308 return GL_R11F_G11F_B10F
;
316 * Create a temporary texture to hold an image of the given size.
317 * If width, height are not POT and the driver only handles POT textures,
318 * allocate the next larger size of texture that is POT.
320 static struct pipe_resource
*
321 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
322 enum pipe_format texFormat
, unsigned bind
)
324 struct pipe_resource
*pt
;
326 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
327 width
, height
, 1, 1, 0, bind
);
334 * Make texture containing an image for glDrawPixels image.
335 * If 'pixels' is NULL, leave the texture image data undefined.
337 static struct pipe_resource
*
338 make_texture(struct st_context
*st
,
339 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
340 const struct gl_pixelstore_attrib
*unpack
,
341 const GLvoid
*pixels
)
343 struct gl_context
*ctx
= st
->ctx
;
344 struct pipe_context
*pipe
= st
->pipe
;
346 struct pipe_resource
*pt
;
347 enum pipe_format pipeFormat
;
348 GLenum baseInternalFormat
;
350 /* Choose a pixel format for the temp texture which will hold the
353 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
354 format
, type
, unpack
->SwapBytes
);
356 if (pipeFormat
== PIPE_FORMAT_NONE
) {
357 /* Use the generic approach. */
358 GLenum intFormat
= internal_format(ctx
, format
, type
);
360 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
361 st
->internal_target
, 0,
362 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
363 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
366 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
367 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
369 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
373 /* alloc temporary texture */
374 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
376 _mesa_unmap_pbo_source(ctx
, unpack
);
381 struct pipe_transfer
*transfer
;
384 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
386 /* we'll do pixel transfer in a fragment shader */
387 ctx
->_ImageTransferState
= 0x0;
389 /* map texture transfer */
390 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
391 PIPE_TRANSFER_WRITE
, 0, 0,
392 width
, height
, &transfer
);
395 /* Put image into texture transfer.
396 * Note that the image is actually going to be upside down in
397 * the texture. We deal with that with texcoords.
399 if ((format
== GL_RGBA
|| format
== GL_BGRA
)
400 && type
== GL_UNSIGNED_BYTE
) {
401 /* Use a memcpy-based texstore to avoid software pixel swizzling.
402 * We'll do the necessary swizzling with the pipe_sampler_view to
403 * give much better performance.
404 * XXX in the future, expand this to accomodate more format and
407 _mesa_memcpy_texture(ctx
, 2,
408 mformat
, /* mesa_format */
409 transfer
->stride
, /* dstRowStride, bytes */
410 &dest
, /* destSlices */
411 width
, height
, 1, /* size */
412 format
, type
, /* src format/type */
413 pixels
, /* data source */
418 success
= _mesa_texstore(ctx
, 2, /* dims */
419 baseInternalFormat
, /* baseInternalFormat */
420 mformat
, /* mesa_format */
421 transfer
->stride
, /* dstRowStride, bytes */
422 &dest
, /* destSlices */
423 width
, height
, 1, /* size */
424 format
, type
, /* src format/type */
425 pixels
, /* data source */
430 pipe_transfer_unmap(pipe
, transfer
);
435 ctx
->_ImageTransferState
= imageTransferStateSave
;
438 _mesa_unmap_pbo_source(ctx
, unpack
);
445 * Draw quad with texcoords and optional color.
446 * Coords are gallium window coords with y=0=top.
447 * \param color may be null
448 * \param invertTex if true, flip texcoords vertically
451 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
452 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
453 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
455 struct st_context
*st
= st_context(ctx
);
456 struct pipe_context
*pipe
= st
->pipe
;
457 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
458 struct pipe_resource
*buf
= NULL
;
461 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), 4, &offset
,
462 &buf
, (void **) &verts
);
467 /* setup vertex data */
469 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
470 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
471 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
472 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
473 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
474 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
475 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
476 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
477 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
478 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
482 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
483 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
486 verts
[1][0][0] = clip_x1
;
487 verts
[1][0][1] = clip_y0
;
490 verts
[2][0][0] = clip_x1
;
491 verts
[2][0][1] = clip_y1
;
494 verts
[3][0][0] = clip_x0
;
495 verts
[3][0][1] = clip_y1
;
497 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
498 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
499 verts
[1][1][0] = sRight
;
500 verts
[1][1][1] = tTop
;
501 verts
[2][1][0] = sRight
;
502 verts
[2][1][1] = tBot
;
503 verts
[3][1][0] = sLeft
;
504 verts
[3][1][1] = tBot
;
506 /* same for all verts: */
508 for (i
= 0; i
< 4; i
++) {
509 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
510 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
511 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
512 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
513 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
514 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
515 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
516 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
520 for (i
= 0; i
< 4; i
++) {
521 verts
[i
][0][2] = z
; /*Z*/
522 verts
[i
][0][3] = 1.0f
; /*W*/
523 verts
[i
][1][2] = 0.0f
; /*R*/
524 verts
[i
][1][3] = 1.0f
; /*Q*/
529 u_upload_unmap(st
->uploader
);
530 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
531 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
535 3); /* attribs/vert */
536 pipe_resource_reference(&buf
, NULL
);
542 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
543 GLsizei width
, GLsizei height
,
544 GLfloat zoomX
, GLfloat zoomY
,
545 struct pipe_sampler_view
**sv
,
546 int num_sampler_view
,
549 struct st_fp_variant
*fpv
,
550 const GLfloat
*color
,
552 GLboolean write_depth
, GLboolean write_stencil
)
554 struct st_context
*st
= st_context(ctx
);
555 struct pipe_context
*pipe
= st
->pipe
;
556 struct cso_context
*cso
= st
->cso_context
;
557 GLfloat x0
, y0
, x1
, y1
;
559 boolean normalized
= sv
[0]->texture
->target
== PIPE_TEXTURE_2D
;
561 assert(sv
[0]->texture
->target
== st
->internal_target
);
564 /* XXX if DrawPixels image is larger than max texture size, break
567 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
568 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
569 assert(width
<= maxSize
);
570 assert(height
<= maxSize
);
572 cso_save_rasterizer(cso
);
573 cso_save_viewport(cso
);
574 cso_save_fragment_samplers(cso
);
575 cso_save_fragment_sampler_views(cso
);
576 cso_save_fragment_shader(cso
);
577 cso_save_stream_outputs(cso
);
578 cso_save_vertex_shader(cso
);
579 cso_save_tessctrl_shader(cso
);
580 cso_save_tesseval_shader(cso
);
581 cso_save_geometry_shader(cso
);
582 cso_save_vertex_elements(cso
);
583 cso_save_aux_vertex_buffer_slot(cso
);
585 cso_save_depth_stencil_alpha(cso
);
589 /* rasterizer state: just scissor */
591 struct pipe_rasterizer_state rasterizer
;
592 memset(&rasterizer
, 0, sizeof(rasterizer
));
593 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
594 ctx
->Color
._ClampFragmentColor
;
595 rasterizer
.half_pixel_center
= 1;
596 rasterizer
.bottom_edge_rule
= 1;
597 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
598 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
599 cso_set_rasterizer(cso
, &rasterizer
);
603 /* Stencil writing bypasses the normal fragment pipeline to
604 * disable color writing and set stencil test to always pass.
606 struct pipe_depth_stencil_alpha_state dsa
;
607 struct pipe_blend_state blend
;
610 memset(&dsa
, 0, sizeof(dsa
));
611 dsa
.stencil
[0].enabled
= 1;
612 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
613 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
614 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
616 /* writing depth+stencil: depth test always passes */
617 dsa
.depth
.enabled
= 1;
618 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
619 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
621 cso_set_depth_stencil_alpha(cso
, &dsa
);
623 /* blend (colormask) */
624 memset(&blend
, 0, sizeof(blend
));
625 cso_set_blend(cso
, &blend
);
628 /* fragment shader state: TEX lookup program */
629 cso_set_fragment_shader_handle(cso
, driver_fp
);
631 /* vertex shader state: position + texcoord pass-through */
632 cso_set_vertex_shader_handle(cso
, driver_vp
);
634 /* disable other shaders */
635 cso_set_tessctrl_shader_handle(cso
, NULL
);
636 cso_set_tesseval_shader_handle(cso
, NULL
);
637 cso_set_geometry_shader_handle(cso
, NULL
);
639 /* user samplers, plus the drawpix samplers */
641 struct pipe_sampler_state sampler
;
643 memset(&sampler
, 0, sizeof(sampler
));
644 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
645 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
646 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
647 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
648 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
649 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
650 sampler
.normalized_coords
= normalized
;
653 /* drawing a color image */
654 const struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
655 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
656 fpv
->pixelmap_sampler
+ 1,
657 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
660 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++)
661 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
663 samplers
[fpv
->drawpix_sampler
] = &sampler
;
665 samplers
[fpv
->pixelmap_sampler
] = &sampler
;
667 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
, samplers
);
669 /* drawing a depth/stencil image */
670 const struct pipe_sampler_state
*samplers
[2] = {&sampler
, &sampler
};
672 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, samplers
);
676 /* user textures, plus the drawpix textures */
678 /* drawing a color image */
679 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
680 uint num
= MAX3(fpv
->drawpix_sampler
+ 1,
681 fpv
->pixelmap_sampler
+ 1,
682 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
684 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
685 sizeof(sampler_views
));
687 sampler_views
[fpv
->drawpix_sampler
] = sv
[0];
689 sampler_views
[fpv
->pixelmap_sampler
] = sv
[1];
690 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
692 /* drawing a depth/stencil image */
693 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
696 /* viewport state: viewport matching window dims */
698 const float w
= (float) ctx
->DrawBuffer
->Width
;
699 const float h
= (float) ctx
->DrawBuffer
->Height
;
700 struct pipe_viewport_state vp
;
701 vp
.scale
[0] = 0.5f
* w
;
702 vp
.scale
[1] = -0.5f
* h
;
704 vp
.translate
[0] = 0.5f
* w
;
705 vp
.translate
[1] = 0.5f
* h
;
706 vp
.translate
[2] = 0.5f
;
707 cso_set_viewport(cso
, &vp
);
710 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
711 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
713 /* Compute Gallium window coords (y=0=top) with pixel zoom.
714 * Recall that these coords are transformed by the current
715 * vertex shader and viewport transformation.
717 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
718 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
719 invertTex
= !invertTex
;
723 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
725 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
727 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
730 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
731 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
732 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
735 cso_restore_rasterizer(cso
);
736 cso_restore_viewport(cso
);
737 cso_restore_fragment_samplers(cso
);
738 cso_restore_fragment_sampler_views(cso
);
739 cso_restore_fragment_shader(cso
);
740 cso_restore_vertex_shader(cso
);
741 cso_restore_tessctrl_shader(cso
);
742 cso_restore_tesseval_shader(cso
);
743 cso_restore_geometry_shader(cso
);
744 cso_restore_vertex_elements(cso
);
745 cso_restore_aux_vertex_buffer_slot(cso
);
746 cso_restore_stream_outputs(cso
);
748 cso_restore_depth_stencil_alpha(cso
);
749 cso_restore_blend(cso
);
755 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
756 * can't use a fragment shader to write stencil values.
759 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
760 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
761 const struct gl_pixelstore_attrib
*unpack
,
762 const GLvoid
*pixels
)
764 struct st_context
*st
= st_context(ctx
);
765 struct pipe_context
*pipe
= st
->pipe
;
766 struct st_renderbuffer
*strb
;
767 enum pipe_transfer_usage usage
;
768 struct pipe_transfer
*pt
;
769 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
771 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
776 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
778 /* totally clipped */
783 strb
= st_renderbuffer(ctx
->DrawBuffer
->
784 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
786 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
787 y
= ctx
->DrawBuffer
->Height
- y
- height
;
790 if (format
== GL_STENCIL_INDEX
&&
791 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
792 /* writing stencil to a combined depth+stencil buffer */
793 usage
= PIPE_TRANSFER_READ_WRITE
;
796 usage
= PIPE_TRANSFER_WRITE
;
799 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
800 strb
->surface
->u
.tex
.level
,
801 strb
->surface
->u
.tex
.first_layer
,
805 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
808 sValues
= malloc(width
* sizeof(GLubyte
));
809 zValues
= malloc(width
* sizeof(GLuint
));
811 if (sValues
&& zValues
) {
813 for (row
= 0; row
< height
; row
++) {
814 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
815 GLenum destType
= GL_UNSIGNED_BYTE
;
816 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
820 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
821 type
, source
, &clippedUnpack
,
822 ctx
->_ImageTransferState
);
824 if (format
== GL_DEPTH_STENCIL
) {
826 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
827 GL_FLOAT
: GL_UNSIGNED_INT
;
829 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
830 (1 << 24) - 1, type
, source
,
835 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
836 "zoom not complete");
842 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
843 spanY
= height
- row
- 1;
849 /* now pack the stencil (and Z) values in the dest format */
850 switch (pt
->resource
->format
) {
851 case PIPE_FORMAT_S8_UINT
:
853 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
854 assert(usage
== PIPE_TRANSFER_WRITE
);
855 memcpy(dest
, sValues
, width
);
858 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
859 if (format
== GL_DEPTH_STENCIL
) {
860 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
862 assert(usage
== PIPE_TRANSFER_WRITE
);
863 for (k
= 0; k
< width
; k
++) {
864 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
868 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
870 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
871 for (k
= 0; k
< width
; k
++) {
872 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
876 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
877 if (format
== GL_DEPTH_STENCIL
) {
878 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
880 assert(usage
== PIPE_TRANSFER_WRITE
);
881 for (k
= 0; k
< width
; k
++) {
882 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
886 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
888 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
889 for (k
= 0; k
< width
; k
++) {
890 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
894 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
895 if (format
== GL_DEPTH_STENCIL
) {
896 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
897 GLfloat
*destf
= (GLfloat
*)dest
;
899 assert(usage
== PIPE_TRANSFER_WRITE
);
900 for (k
= 0; k
< width
; k
++) {
901 destf
[k
*2] = zValuesFloat
[k
];
902 dest
[k
*2+1] = sValues
[k
] & 0xff;
906 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
908 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
909 for (k
= 0; k
< width
; k
++) {
910 dest
[k
*2+1] = sValues
[k
] & 0xff;
921 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
927 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
929 /* unmap the stencil buffer */
930 pipe_transfer_unmap(pipe
, pt
);
935 * Get fragment program variant for a glDrawPixels or glCopyPixels
936 * command for RGBA data.
938 static struct st_fp_variant
*
939 get_color_fp_variant(struct st_context
*st
)
941 struct gl_context
*ctx
= st
->ctx
;
942 struct st_fp_variant_key key
;
943 struct st_fp_variant
*fpv
;
945 memset(&key
, 0, sizeof(key
));
947 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
949 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
950 ctx
->Pixel
.RedScale
!= 1.0 ||
951 ctx
->Pixel
.GreenBias
!= 0.0 ||
952 ctx
->Pixel
.GreenScale
!= 1.0 ||
953 ctx
->Pixel
.BlueBias
!= 0.0 ||
954 ctx
->Pixel
.BlueScale
!= 1.0 ||
955 ctx
->Pixel
.AlphaBias
!= 0.0 ||
956 ctx
->Pixel
.AlphaScale
!= 1.0);
957 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
958 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
959 st
->ctx
->Color
._ClampFragmentColor
;
961 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
968 * Clamp glDrawPixels width and height to the maximum texture size.
971 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
972 struct gl_pixelstore_attrib
*unpack
)
975 1 << (pipe
->screen
->get_param(pipe
->screen
,
976 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
978 if (*width
> maxSize
) {
979 if (unpack
->RowLength
== 0)
980 unpack
->RowLength
= *width
;
983 if (*height
> maxSize
) {
990 * Search the array of 4 swizzle components for the named component and return
994 search_swizzle(const unsigned char swizzle
[4], unsigned component
)
997 for (i
= 0; i
< 4; i
++) {
998 if (swizzle
[i
] == component
)
1001 assert(!"search_swizzle() failed");
1007 * Set the sampler view's swizzle terms. This is used to handle RGBA
1008 * swizzling when the incoming image format isn't an exact match for
1009 * the actual texture format. For example, if we have glDrawPixels(
1010 * GL_RGBA, GL_UNSIGNED_BYTE) and we chose the texture format
1011 * PIPE_FORMAT_B8G8R8A8 then we can do use the sampler view swizzle to
1012 * avoid swizzling all the pixels in software in the texstore code.
1015 setup_sampler_swizzle(struct pipe_sampler_view
*sv
, GLenum format
, GLenum type
)
1017 if ((format
== GL_RGBA
|| format
== GL_BGRA
) && type
== GL_UNSIGNED_BYTE
) {
1018 const struct util_format_description
*desc
=
1019 util_format_description(sv
->texture
->format
);
1020 unsigned c0
, c1
, c2
, c3
;
1022 /* Every gallium driver supports at least one 32-bit packed RGBA format.
1023 * We must have chosen one for (GL_RGBA, GL_UNSIGNED_BYTE).
1025 assert(desc
->block
.bits
== 32);
1027 /* invert the format's swizzle to setup the sampler's swizzle */
1028 if (format
== GL_RGBA
) {
1029 c0
= UTIL_FORMAT_SWIZZLE_X
;
1030 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1031 c2
= UTIL_FORMAT_SWIZZLE_Z
;
1032 c3
= UTIL_FORMAT_SWIZZLE_W
;
1035 assert(format
== GL_BGRA
);
1036 c0
= UTIL_FORMAT_SWIZZLE_Z
;
1037 c1
= UTIL_FORMAT_SWIZZLE_Y
;
1038 c2
= UTIL_FORMAT_SWIZZLE_X
;
1039 c3
= UTIL_FORMAT_SWIZZLE_W
;
1041 sv
->swizzle_r
= search_swizzle(desc
->swizzle
, c0
);
1042 sv
->swizzle_g
= search_swizzle(desc
->swizzle
, c1
);
1043 sv
->swizzle_b
= search_swizzle(desc
->swizzle
, c2
);
1044 sv
->swizzle_a
= search_swizzle(desc
->swizzle
, c3
);
1047 /* use the default sampler swizzle */
1053 * Called via ctx->Driver.DrawPixels()
1056 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1057 GLsizei width
, GLsizei height
,
1058 GLenum format
, GLenum type
,
1059 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1061 void *driver_vp
, *driver_fp
;
1062 struct st_context
*st
= st_context(ctx
);
1063 const GLfloat
*color
;
1064 struct pipe_context
*pipe
= st
->pipe
;
1065 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1066 struct pipe_sampler_view
*sv
[2] = { NULL
};
1067 int num_sampler_view
= 1;
1068 struct gl_pixelstore_attrib clippedUnpack
;
1069 struct st_fp_variant
*fpv
= NULL
;
1070 struct pipe_resource
*pt
;
1072 /* Mesa state should be up to date by now */
1073 assert(ctx
->NewState
== 0x0);
1075 st_flush_bitmap_cache(st
);
1077 st_validate_state(st
);
1079 /* Limit the size of the glDrawPixels to the max texture size.
1080 * Strictly speaking, that's not correct but since we don't handle
1081 * larger images yet, this is better than crashing.
1083 clippedUnpack
= *unpack
;
1084 unpack
= &clippedUnpack
;
1085 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1087 if (format
== GL_DEPTH_STENCIL
)
1088 write_stencil
= write_depth
= GL_TRUE
;
1089 else if (format
== GL_STENCIL_INDEX
)
1090 write_stencil
= GL_TRUE
;
1091 else if (format
== GL_DEPTH_COMPONENT
)
1092 write_depth
= GL_TRUE
;
1094 if (write_stencil
&&
1095 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1096 /* software fallback */
1097 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1103 * Get vertex/fragment shaders
1105 if (write_depth
|| write_stencil
) {
1106 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1108 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1109 color
= ctx
->Current
.RasterColor
;
1112 fpv
= get_color_fp_variant(st
);
1114 driver_fp
= fpv
->driver_shader
;
1115 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1118 if (ctx
->Pixel
.MapColorFlag
) {
1119 pipe_sampler_view_reference(&sv
[1],
1120 st
->pixel_xfer
.pixelmap_sampler_view
);
1124 /* compiling a new fragment shader variant added new state constants
1125 * into the constant buffer, we need to update them
1127 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1128 PIPE_SHADER_FRAGMENT
);
1131 /* Put glDrawPixels image into a texture */
1132 pt
= make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1134 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1138 /* create sampler view for the image */
1139 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1141 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1142 pipe_resource_reference(&pt
, NULL
);
1146 /* Set up the sampler view's swizzle */
1147 setup_sampler_swizzle(sv
[0], format
, type
);
1149 /* Create a second sampler view to read stencil. The stencil is
1150 * written using the shader stencil export functionality.
1152 if (write_stencil
) {
1153 enum pipe_format stencil_format
=
1154 util_format_stencil_only(pt
->format
);
1155 /* we should not be doing pixel map/transfer (see above) */
1156 assert(num_sampler_view
== 1);
1157 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1160 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
1161 pipe_resource_reference(&pt
, NULL
);
1162 pipe_sampler_view_reference(&sv
[0], NULL
);
1168 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1170 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1175 color
, GL_FALSE
, write_depth
, write_stencil
);
1176 pipe_sampler_view_reference(&sv
[0], NULL
);
1177 if (num_sampler_view
> 1)
1178 pipe_sampler_view_reference(&sv
[1], NULL
);
1180 pipe_resource_reference(&pt
, NULL
);
1186 * Software fallback for glCopyPixels(GL_STENCIL).
1189 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1190 GLsizei width
, GLsizei height
,
1191 GLint dstx
, GLint dsty
)
1193 struct st_renderbuffer
*rbDraw
;
1194 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1195 enum pipe_transfer_usage usage
;
1196 struct pipe_transfer
*ptDraw
;
1201 buffer
= malloc(width
* height
* sizeof(ubyte
));
1203 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1207 /* Get the dest renderbuffer */
1208 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1209 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1211 /* this will do stencil pixel transfer ops */
1212 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1213 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1214 &ctx
->DefaultPacking
, buffer
);
1217 /* debug code: dump stencil values */
1219 for (row
= 0; row
< height
; row
++) {
1220 printf("%3d: ", row
);
1221 for (col
= 0; col
< width
; col
++) {
1222 printf("%02x ", buffer
[col
+ row
* width
]);
1228 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1229 usage
= PIPE_TRANSFER_READ_WRITE
;
1231 usage
= PIPE_TRANSFER_WRITE
;
1233 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1234 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1237 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1238 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1240 /* map the stencil buffer */
1241 drawMap
= pipe_transfer_map(pipe
,
1243 rbDraw
->surface
->u
.tex
.level
,
1244 rbDraw
->surface
->u
.tex
.first_layer
,
1246 width
, height
, &ptDraw
);
1249 /* XXX PixelZoom not handled yet */
1250 for (i
= 0; i
< height
; i
++) {
1257 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1261 dst
= drawMap
+ y
* ptDraw
->stride
;
1262 src
= buffer
+ i
* width
;
1264 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1269 /* unmap the stencil buffer */
1270 pipe_transfer_unmap(pipe
, ptDraw
);
1275 * Return renderbuffer to use for reading color pixels for glCopyPixels
1277 static struct st_renderbuffer
*
1278 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1280 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1281 struct st_renderbuffer
*strb
=
1282 st_renderbuffer(fb
->_ColorReadBuffer
);
1289 * Try to do a glCopyPixels for simple cases with a blit by calling
1292 * We can do this when we're copying color pixels (depth/stencil
1293 * eventually) with no pixel zoom, no pixel transfer ops, no
1294 * per-fragment ops, and the src/dest regions don't overlap.
1297 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1298 GLsizei width
, GLsizei height
,
1299 GLint dstx
, GLint dsty
, GLenum type
)
1301 struct st_context
*st
= st_context(ctx
);
1302 struct pipe_context
*pipe
= st
->pipe
;
1303 struct pipe_screen
*screen
= pipe
->screen
;
1304 struct gl_pixelstore_attrib pack
, unpack
;
1305 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1307 if (type
== GL_COLOR
&&
1308 ctx
->Pixel
.ZoomX
== 1.0 &&
1309 ctx
->Pixel
.ZoomY
== 1.0 &&
1310 ctx
->_ImageTransferState
== 0x0 &&
1311 !ctx
->Color
.BlendEnabled
&&
1312 !ctx
->Color
.AlphaEnabled
&&
1313 (!ctx
->Color
.ColorLogicOpEnabled
|| ctx
->Color
.LogicOp
== GL_COPY
) &&
1315 !ctx
->Fog
.Enabled
&&
1316 !ctx
->Stencil
.Enabled
&&
1317 !ctx
->FragmentProgram
.Enabled
&&
1318 !ctx
->VertexProgram
.Enabled
&&
1319 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1320 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1321 !ctx
->Query
.CondRenderQuery
&&
1322 !ctx
->Query
.CurrentOcclusionObject
) {
1323 struct st_renderbuffer
*rbRead
, *rbDraw
;
1326 * Clip the read region against the src buffer bounds.
1327 * We'll still allocate a temporary buffer/texture for the original
1328 * src region size but we'll only read the region which is on-screen.
1329 * This may mean that we draw garbage pixels into the dest region, but
1336 pack
= ctx
->DefaultPacking
;
1337 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1338 return GL_TRUE
; /* all done */
1340 /* clip against dest buffer bounds and scissor box */
1341 drawX
= dstx
+ pack
.SkipPixels
;
1342 drawY
= dsty
+ pack
.SkipRows
;
1344 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1345 return GL_TRUE
; /* all done */
1347 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1348 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1353 rbRead
= st_get_color_read_renderbuffer(ctx
);
1354 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1356 /* Flip src/dst position depending on the orientation of buffers. */
1357 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1358 readY
= rbRead
->Base
.Height
- readY
;
1362 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1363 /* We can't flip the destination for pipe->blit, so we only adjust
1364 * its position and flip the source.
1366 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1371 if (rbRead
!= rbDraw
||
1372 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1373 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1374 struct pipe_blit_info blit
;
1376 memset(&blit
, 0, sizeof(blit
));
1377 blit
.src
.resource
= rbRead
->texture
;
1378 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1379 blit
.src
.format
= rbRead
->texture
->format
;
1380 blit
.src
.box
.x
= readX
;
1381 blit
.src
.box
.y
= readY
;
1382 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1383 blit
.src
.box
.width
= readW
;
1384 blit
.src
.box
.height
= readH
;
1385 blit
.src
.box
.depth
= 1;
1386 blit
.dst
.resource
= rbDraw
->texture
;
1387 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1388 blit
.dst
.format
= rbDraw
->texture
->format
;
1389 blit
.dst
.box
.x
= drawX
;
1390 blit
.dst
.box
.y
= drawY
;
1391 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1392 blit
.dst
.box
.width
= drawW
;
1393 blit
.dst
.box
.height
= drawH
;
1394 blit
.dst
.box
.depth
= 1;
1395 blit
.mask
= PIPE_MASK_RGBA
;
1396 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1398 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1399 blit
.src
.resource
->target
,
1400 blit
.src
.resource
->nr_samples
,
1401 PIPE_BIND_SAMPLER_VIEW
) &&
1402 screen
->is_format_supported(screen
, blit
.dst
.format
,
1403 blit
.dst
.resource
->target
,
1404 blit
.dst
.resource
->nr_samples
,
1405 PIPE_BIND_RENDER_TARGET
)) {
1406 pipe
->blit(pipe
, &blit
);
1417 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1418 GLsizei width
, GLsizei height
,
1419 GLint dstx
, GLint dsty
, GLenum type
)
1421 struct st_context
*st
= st_context(ctx
);
1422 struct pipe_context
*pipe
= st
->pipe
;
1423 struct pipe_screen
*screen
= pipe
->screen
;
1424 struct st_renderbuffer
*rbRead
;
1425 void *driver_vp
, *driver_fp
;
1426 struct pipe_resource
*pt
;
1427 struct pipe_sampler_view
*sv
[2] = { NULL
};
1428 struct st_fp_variant
*fpv
= NULL
;
1429 int num_sampler_view
= 1;
1431 enum pipe_format srcFormat
;
1433 GLboolean invertTex
= GL_FALSE
;
1434 GLint readX
, readY
, readW
, readH
;
1435 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1437 st_flush_bitmap_cache(st
);
1439 st_validate_state(st
);
1441 if (type
== GL_DEPTH_STENCIL
) {
1442 /* XXX make this more efficient */
1443 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1444 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1448 if (type
== GL_STENCIL
) {
1449 /* can't use texturing to do stencil */
1450 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1454 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1458 * The subsequent code implements glCopyPixels by copying the source
1459 * pixels into a temporary texture that's then applied to a textured quad.
1460 * When we draw the textured quad, all the usual per-fragment operations
1466 * Get vertex/fragment shaders
1468 if (type
== GL_COLOR
) {
1469 fpv
= get_color_fp_variant(st
);
1471 rbRead
= st_get_color_read_renderbuffer(ctx
);
1474 driver_fp
= fpv
->driver_shader
;
1475 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1477 if (ctx
->Pixel
.MapColorFlag
) {
1478 pipe_sampler_view_reference(&sv
[1],
1479 st
->pixel_xfer
.pixelmap_sampler_view
);
1483 /* compiling a new fragment shader variant added new state constants
1484 * into the constant buffer, we need to update them
1486 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
1487 PIPE_SHADER_FRAGMENT
);
1490 assert(type
== GL_DEPTH
);
1491 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1492 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1493 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1495 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1496 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1499 /* Choose the format for the temporary texture. */
1500 srcFormat
= rbRead
->texture
->format
;
1501 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1502 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1504 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1506 /* srcFormat is non-renderable. Find a compatible renderable format. */
1507 if (type
== GL_DEPTH
) {
1508 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1509 GL_NONE
, st
->internal_target
, 0,
1513 assert(type
== GL_COLOR
);
1515 if (util_format_is_float(srcFormat
)) {
1516 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1517 GL_NONE
, st
->internal_target
, 0,
1520 else if (util_format_is_pure_sint(srcFormat
)) {
1521 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1522 GL_NONE
, st
->internal_target
, 0,
1525 else if (util_format_is_pure_uint(srcFormat
)) {
1526 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1527 GL_NONE
, st
->internal_target
, 0,
1530 else if (util_format_is_snorm(srcFormat
)) {
1531 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1532 GL_NONE
, st
->internal_target
, 0,
1536 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1537 GL_NONE
, st
->internal_target
, 0,
1542 if (srcFormat
== PIPE_FORMAT_NONE
) {
1543 assert(0 && "cannot choose a format for src of CopyPixels");
1548 /* Invert src region if needed */
1549 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1550 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1551 invertTex
= !invertTex
;
1554 /* Clip the read region against the src buffer bounds.
1555 * We'll still allocate a temporary buffer/texture for the original
1556 * src region size but we'll only read the region which is on-screen.
1557 * This may mean that we draw garbage pixels into the dest region, but
1564 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1565 /* The source region is completely out of bounds. Do nothing.
1566 * The GL spec says "Results of copies from outside the window,
1567 * or from regions of the window that are not exposed, are
1568 * hardware dependent and undefined."
1573 readW
= MAX2(0, readW
);
1574 readH
= MAX2(0, readH
);
1576 /* Allocate the temporary texture. */
1577 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1581 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1583 pipe_resource_reference(&pt
, NULL
);
1587 /* Copy the src region to the temporary texture. */
1589 struct pipe_blit_info blit
;
1591 memset(&blit
, 0, sizeof(blit
));
1592 blit
.src
.resource
= rbRead
->texture
;
1593 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1594 blit
.src
.format
= rbRead
->texture
->format
;
1595 blit
.src
.box
.x
= readX
;
1596 blit
.src
.box
.y
= readY
;
1597 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1598 blit
.src
.box
.width
= readW
;
1599 blit
.src
.box
.height
= readH
;
1600 blit
.src
.box
.depth
= 1;
1601 blit
.dst
.resource
= pt
;
1603 blit
.dst
.format
= pt
->format
;
1604 blit
.dst
.box
.x
= pack
.SkipPixels
;
1605 blit
.dst
.box
.y
= pack
.SkipRows
;
1607 blit
.dst
.box
.width
= readW
;
1608 blit
.dst
.box
.height
= readH
;
1609 blit
.dst
.box
.depth
= 1;
1610 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1611 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1613 pipe
->blit(pipe
, &blit
);
1616 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1617 * textured quad with that texture.
1619 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1620 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1625 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1627 pipe_resource_reference(&pt
, NULL
);
1628 pipe_sampler_view_reference(&sv
[0], NULL
);
1633 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1635 functions
->DrawPixels
= st_DrawPixels
;
1636 functions
->CopyPixels
= st_CopyPixels
;
1641 st_destroy_drawpix(struct st_context
*st
)
1645 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1646 if (st
->drawpix
.zs_shaders
[i
])
1647 cso_delete_fragment_shader(st
->cso_context
,
1648 st
->drawpix
.zs_shaders
[i
]);
1651 if (st
->drawpix
.vert_shaders
[0])
1652 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1653 if (st
->drawpix
.vert_shaders
[1])
1654 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);