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 * Check if the given program is:
76 * 0: MOVE result.color, fragment.color;
80 is_passthrough_program(const struct gl_fragment_program
*prog
)
82 if (prog
->Base
.NumInstructions
== 2) {
83 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
84 if (inst
[0].Opcode
== OPCODE_MOV
&&
85 inst
[1].Opcode
== OPCODE_END
&&
86 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
87 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
88 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
89 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
90 inst
[0].SrcReg
[0].Index
== VARYING_SLOT_COL0
&&
91 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
100 * Returns a fragment program which implements the current pixel transfer ops.
102 static struct gl_fragment_program
*
103 get_glsl_pixel_transfer_program(struct st_context
*st
,
104 struct st_fragment_program
*orig
)
106 int pixelMaps
= 0, scaleAndBias
= 0;
107 struct gl_context
*ctx
= st
->ctx
;
108 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
109 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
114 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
115 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
116 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
117 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
121 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
124 /* create the colormap/texture now if not already done */
125 if (!st
->pixel_xfer
.pixelmap_texture
) {
126 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
127 st
->pixel_xfer
.pixelmap_sampler_view
=
128 st_create_texture_sampler_view(st
->pipe
,
129 st
->pixel_xfer
.pixelmap_texture
);
133 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
134 scaleAndBias
, pixelMaps
);
141 * Make fragment shader for glDraw/CopyPixels. This shader is made
142 * by combining the pixel transfer shader with the user-defined shader.
143 * \param fpIn the current/incoming fragment program
144 * \param fpOut returns the combined fragment program
147 st_make_drawpix_fragment_program(struct st_context
*st
,
148 struct gl_fragment_program
*fpIn
,
149 struct gl_fragment_program
**fpOut
)
151 struct gl_program
*newProg
;
152 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
154 if (is_passthrough_program(fpIn
)) {
155 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
156 &st
->pixel_xfer
.program
->Base
);
158 else if (stfp
->glsl_to_tgsi
!= NULL
) {
159 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
164 printf("Base program:\n");
165 _mesa_print_program(&fpIn
->Base
);
166 printf("DrawPix program:\n");
167 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
169 newProg
= _mesa_combine_programs(st
->ctx
,
170 &st
->pixel_xfer
.program
->Base
.Base
,
176 printf("Combined DrawPixels program:\n");
177 _mesa_print_program(newProg
);
178 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
179 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
180 _mesa_print_parameter_list(newProg
->Parameters
);
183 *fpOut
= (struct gl_fragment_program
*) newProg
;
188 * Create fragment program that does a TEX() instruction to get a Z and/or
189 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
190 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
191 * Pass fragment color through as-is.
193 * \return CSO of the fragment shader.
196 get_drawpix_z_stencil_program(struct st_context
*st
,
197 GLboolean write_depth
,
198 GLboolean write_stencil
)
200 struct ureg_program
*ureg
;
201 struct ureg_src depth_sampler
, stencil_sampler
;
202 struct ureg_src texcoord
, color
;
203 struct ureg_dst out_color
, out_depth
, out_stencil
;
204 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
207 assert(shaderIndex
< ARRAY_SIZE(st
->drawpix
.zs_shaders
));
209 if (st
->drawpix
.zs_shaders
[shaderIndex
]) {
210 /* already have the proper shader */
211 return st
->drawpix
.zs_shaders
[shaderIndex
];
214 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
218 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, TRUE
);
221 color
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_COLOR
, 0,
222 TGSI_INTERPOLATE_COLOR
);
223 out_color
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
225 depth_sampler
= ureg_DECL_sampler(ureg
, 0);
226 out_depth
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
230 stencil_sampler
= ureg_DECL_sampler(ureg
, 1);
231 out_stencil
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_STENCIL
, 0);
234 texcoord
= ureg_DECL_fs_input(ureg
,
235 st
->needs_texcoord_semantic
?
236 TGSI_SEMANTIC_TEXCOORD
:
237 TGSI_SEMANTIC_GENERIC
,
238 0, TGSI_INTERPOLATE_LINEAR
);
241 ureg_TEX(ureg
, ureg_writemask(out_depth
, TGSI_WRITEMASK_Z
),
242 TGSI_TEXTURE_2D
, texcoord
, depth_sampler
);
243 ureg_MOV(ureg
, out_color
, color
);
247 ureg_TEX(ureg
, ureg_writemask(out_stencil
, TGSI_WRITEMASK_Y
),
248 TGSI_TEXTURE_2D
, texcoord
, stencil_sampler
);
251 cso
= ureg_create_shader_and_destroy(ureg
, st
->pipe
);
253 /* save the new shader */
254 st
->drawpix
.zs_shaders
[shaderIndex
] = cso
;
260 * Create a simple vertex shader that just passes through the
261 * vertex position and texcoord (and optionally, color).
264 make_passthrough_vertex_shader(struct st_context
*st
,
267 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
268 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
270 if (!st
->drawpix
.vert_shaders
[passColor
]) {
271 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
276 /* MOV result.pos, vertex.pos; */
278 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
279 ureg_DECL_vs_input( ureg
, 0 ));
281 /* MOV result.texcoord0, vertex.attr[1]; */
283 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
284 ureg_DECL_vs_input( ureg
, 1 ));
287 /* MOV result.color0, vertex.attr[2]; */
289 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
290 ureg_DECL_vs_input( ureg
, 2 ));
295 st
->drawpix
.vert_shaders
[passColor
] =
296 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
299 return st
->drawpix
.vert_shaders
[passColor
];
304 * Return a texture internalFormat for drawing/copying an image
305 * of the given format and type.
308 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
311 case GL_DEPTH_COMPONENT
:
313 case GL_UNSIGNED_SHORT
:
314 return GL_DEPTH_COMPONENT16
;
316 case GL_UNSIGNED_INT
:
317 return GL_DEPTH_COMPONENT32
;
320 if (ctx
->Extensions
.ARB_depth_buffer_float
)
321 return GL_DEPTH_COMPONENT32F
;
323 return GL_DEPTH_COMPONENT
;
326 return GL_DEPTH_COMPONENT
;
329 case GL_DEPTH_STENCIL
:
331 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
332 return GL_DEPTH32F_STENCIL8
;
334 case GL_UNSIGNED_INT_24_8
:
336 return GL_DEPTH24_STENCIL8
;
339 case GL_STENCIL_INDEX
:
340 return GL_STENCIL_INDEX
;
343 if (_mesa_is_enum_format_integer(format
)) {
347 case GL_UNSIGNED_BYTE
:
351 case GL_UNSIGNED_SHORT
:
355 case GL_UNSIGNED_INT
:
358 assert(0 && "Unexpected type in internal_format()");
359 return GL_RGBA_INTEGER
;
364 case GL_UNSIGNED_BYTE
:
365 case GL_UNSIGNED_INT_8_8_8_8
:
366 case GL_UNSIGNED_INT_8_8_8_8_REV
:
370 case GL_UNSIGNED_BYTE_3_3_2
:
371 case GL_UNSIGNED_BYTE_2_3_3_REV
:
374 case GL_UNSIGNED_SHORT_4_4_4_4
:
375 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
378 case GL_UNSIGNED_SHORT_5_6_5
:
379 case GL_UNSIGNED_SHORT_5_6_5_REV
:
382 case GL_UNSIGNED_SHORT_5_5_5_1
:
383 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
386 case GL_UNSIGNED_INT_10_10_10_2
:
387 case GL_UNSIGNED_INT_2_10_10_10_REV
:
390 case GL_UNSIGNED_SHORT
:
391 case GL_UNSIGNED_INT
:
396 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
401 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
403 case GL_HALF_FLOAT_ARB
:
405 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
406 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
411 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
412 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
414 case GL_UNSIGNED_INT_5_9_9_9_REV
:
415 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
418 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
419 assert(ctx
->Extensions
.EXT_packed_float
);
420 return GL_R11F_G11F_B10F
;
428 * Create a temporary texture to hold an image of the given size.
429 * If width, height are not POT and the driver only handles POT textures,
430 * allocate the next larger size of texture that is POT.
432 static struct pipe_resource
*
433 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
434 enum pipe_format texFormat
, unsigned bind
)
436 struct pipe_resource
*pt
;
438 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
439 width
, height
, 1, 1, 0, bind
);
446 * Make texture containing an image for glDrawPixels image.
447 * If 'pixels' is NULL, leave the texture image data undefined.
449 static struct pipe_resource
*
450 make_texture(struct st_context
*st
,
451 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
452 const struct gl_pixelstore_attrib
*unpack
,
453 const GLvoid
*pixels
)
455 struct gl_context
*ctx
= st
->ctx
;
456 struct pipe_context
*pipe
= st
->pipe
;
458 struct pipe_resource
*pt
;
459 enum pipe_format pipeFormat
;
460 GLenum baseInternalFormat
;
462 /* Choose a pixel format for the temp texture which will hold the
465 pipeFormat
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
466 format
, type
, unpack
->SwapBytes
);
468 if (pipeFormat
== PIPE_FORMAT_NONE
) {
469 /* Use the generic approach. */
470 GLenum intFormat
= internal_format(ctx
, format
, type
);
472 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
473 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
475 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
478 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
479 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
481 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
485 /* alloc temporary texture */
486 pt
= alloc_texture(st
, width
, height
, pipeFormat
, PIPE_BIND_SAMPLER_VIEW
);
488 _mesa_unmap_pbo_source(ctx
, unpack
);
493 struct pipe_transfer
*transfer
;
496 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
498 /* we'll do pixel transfer in a fragment shader */
499 ctx
->_ImageTransferState
= 0x0;
501 /* map texture transfer */
502 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
503 PIPE_TRANSFER_WRITE
, 0, 0,
504 width
, height
, &transfer
);
507 /* Put image into texture transfer.
508 * Note that the image is actually going to be upside down in
509 * the texture. We deal with that with texcoords.
511 success
= _mesa_texstore(ctx
, 2, /* dims */
512 baseInternalFormat
, /* baseInternalFormat */
513 mformat
, /* mesa_format */
514 transfer
->stride
, /* dstRowStride, bytes */
515 &dest
, /* destSlices */
516 width
, height
, 1, /* size */
517 format
, type
, /* src format/type */
518 pixels
, /* data source */
522 pipe_transfer_unmap(pipe
, transfer
);
527 ctx
->_ImageTransferState
= imageTransferStateSave
;
530 _mesa_unmap_pbo_source(ctx
, unpack
);
537 * Draw quad with texcoords and optional color.
538 * Coords are gallium window coords with y=0=top.
539 * \param color may be null
540 * \param invertTex if true, flip texcoords vertically
543 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
544 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
545 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
547 struct st_context
*st
= st_context(ctx
);
548 struct pipe_context
*pipe
= st
->pipe
;
549 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
550 struct pipe_resource
*buf
= NULL
;
553 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
554 &buf
, (void **) &verts
);
559 /* setup vertex data */
561 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
562 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
563 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
564 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
565 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
566 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
567 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
568 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
569 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
570 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
574 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
575 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
578 verts
[1][0][0] = clip_x1
;
579 verts
[1][0][1] = clip_y0
;
582 verts
[2][0][0] = clip_x1
;
583 verts
[2][0][1] = clip_y1
;
586 verts
[3][0][0] = clip_x0
;
587 verts
[3][0][1] = clip_y1
;
589 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
590 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
591 verts
[1][1][0] = sRight
;
592 verts
[1][1][1] = tTop
;
593 verts
[2][1][0] = sRight
;
594 verts
[2][1][1] = tBot
;
595 verts
[3][1][0] = sLeft
;
596 verts
[3][1][1] = tBot
;
598 /* same for all verts: */
600 for (i
= 0; i
< 4; i
++) {
601 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
602 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
603 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
604 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
605 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
606 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
607 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
608 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
612 for (i
= 0; i
< 4; i
++) {
613 verts
[i
][0][2] = z
; /*Z*/
614 verts
[i
][0][3] = 1.0f
; /*W*/
615 verts
[i
][1][2] = 0.0f
; /*R*/
616 verts
[i
][1][3] = 1.0f
; /*Q*/
621 u_upload_unmap(st
->uploader
);
622 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
623 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
627 3); /* attribs/vert */
628 pipe_resource_reference(&buf
, NULL
);
634 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
635 GLsizei width
, GLsizei height
,
636 GLfloat zoomX
, GLfloat zoomY
,
637 struct pipe_sampler_view
**sv
,
638 int num_sampler_view
,
641 const GLfloat
*color
,
643 GLboolean write_depth
, GLboolean write_stencil
)
645 struct st_context
*st
= st_context(ctx
);
646 struct pipe_context
*pipe
= st
->pipe
;
647 struct cso_context
*cso
= st
->cso_context
;
648 GLfloat x0
, y0
, x1
, y1
;
650 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
653 /* XXX if DrawPixels image is larger than max texture size, break
656 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
657 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
658 assert(width
<= maxSize
);
659 assert(height
<= maxSize
);
661 cso_save_rasterizer(cso
);
662 cso_save_viewport(cso
);
663 cso_save_fragment_samplers(cso
);
664 cso_save_fragment_sampler_views(cso
);
665 cso_save_fragment_shader(cso
);
666 cso_save_stream_outputs(cso
);
667 cso_save_vertex_shader(cso
);
668 cso_save_tessctrl_shader(cso
);
669 cso_save_tesseval_shader(cso
);
670 cso_save_geometry_shader(cso
);
671 cso_save_vertex_elements(cso
);
672 cso_save_aux_vertex_buffer_slot(cso
);
674 cso_save_depth_stencil_alpha(cso
);
678 /* rasterizer state: just scissor */
680 struct pipe_rasterizer_state rasterizer
;
681 memset(&rasterizer
, 0, sizeof(rasterizer
));
682 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
683 ctx
->Color
._ClampFragmentColor
;
684 rasterizer
.half_pixel_center
= 1;
685 rasterizer
.bottom_edge_rule
= 1;
686 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
687 rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
;
688 cso_set_rasterizer(cso
, &rasterizer
);
692 /* Stencil writing bypasses the normal fragment pipeline to
693 * disable color writing and set stencil test to always pass.
695 struct pipe_depth_stencil_alpha_state dsa
;
696 struct pipe_blend_state blend
;
699 memset(&dsa
, 0, sizeof(dsa
));
700 dsa
.stencil
[0].enabled
= 1;
701 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
702 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
703 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
705 /* writing depth+stencil: depth test always passes */
706 dsa
.depth
.enabled
= 1;
707 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
708 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
710 cso_set_depth_stencil_alpha(cso
, &dsa
);
712 /* blend (colormask) */
713 memset(&blend
, 0, sizeof(blend
));
714 cso_set_blend(cso
, &blend
);
717 /* fragment shader state: TEX lookup program */
718 cso_set_fragment_shader_handle(cso
, driver_fp
);
720 /* vertex shader state: position + texcoord pass-through */
721 cso_set_vertex_shader_handle(cso
, driver_vp
);
723 /* disable other shaders */
724 cso_set_tessctrl_shader_handle(cso
, NULL
);
725 cso_set_tesseval_shader_handle(cso
, NULL
);
726 cso_set_geometry_shader_handle(cso
, NULL
);
728 /* texture sampling state: */
730 struct pipe_sampler_state sampler
;
731 const struct pipe_sampler_state
*states
[2] = {&sampler
, &sampler
};
733 memset(&sampler
, 0, sizeof(sampler
));
734 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
735 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
736 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
737 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
738 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
739 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
740 sampler
.normalized_coords
= normalized
;
742 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
,
743 num_sampler_view
> 1 ? 2 : 1, states
);
746 /* viewport state: viewport matching window dims */
748 const float w
= (float) ctx
->DrawBuffer
->Width
;
749 const float h
= (float) ctx
->DrawBuffer
->Height
;
750 struct pipe_viewport_state vp
;
751 vp
.scale
[0] = 0.5f
* w
;
752 vp
.scale
[1] = -0.5f
* h
;
754 vp
.translate
[0] = 0.5f
* w
;
755 vp
.translate
[1] = 0.5f
* h
;
756 vp
.translate
[2] = 0.5f
;
757 cso_set_viewport(cso
, &vp
);
760 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
761 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
764 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
766 /* Compute Gallium window coords (y=0=top) with pixel zoom.
767 * Recall that these coords are transformed by the current
768 * vertex shader and viewport transformation.
770 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
771 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
772 invertTex
= !invertTex
;
776 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
778 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
780 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
783 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
784 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
785 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
788 cso_restore_rasterizer(cso
);
789 cso_restore_viewport(cso
);
790 cso_restore_fragment_samplers(cso
);
791 cso_restore_fragment_sampler_views(cso
);
792 cso_restore_fragment_shader(cso
);
793 cso_restore_vertex_shader(cso
);
794 cso_restore_tessctrl_shader(cso
);
795 cso_restore_tesseval_shader(cso
);
796 cso_restore_geometry_shader(cso
);
797 cso_restore_vertex_elements(cso
);
798 cso_restore_aux_vertex_buffer_slot(cso
);
799 cso_restore_stream_outputs(cso
);
801 cso_restore_depth_stencil_alpha(cso
);
802 cso_restore_blend(cso
);
808 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
809 * can't use a fragment shader to write stencil values.
812 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
813 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
814 const struct gl_pixelstore_attrib
*unpack
,
815 const GLvoid
*pixels
)
817 struct st_context
*st
= st_context(ctx
);
818 struct pipe_context
*pipe
= st
->pipe
;
819 struct st_renderbuffer
*strb
;
820 enum pipe_transfer_usage usage
;
821 struct pipe_transfer
*pt
;
822 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
824 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
829 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
831 /* totally clipped */
836 strb
= st_renderbuffer(ctx
->DrawBuffer
->
837 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
839 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
840 y
= ctx
->DrawBuffer
->Height
- y
- height
;
843 if (format
== GL_STENCIL_INDEX
&&
844 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
845 /* writing stencil to a combined depth+stencil buffer */
846 usage
= PIPE_TRANSFER_READ_WRITE
;
849 usage
= PIPE_TRANSFER_WRITE
;
852 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
853 strb
->surface
->u
.tex
.level
,
854 strb
->surface
->u
.tex
.first_layer
,
858 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
861 sValues
= malloc(width
* sizeof(GLubyte
));
862 zValues
= malloc(width
* sizeof(GLuint
));
864 if (sValues
&& zValues
) {
866 for (row
= 0; row
< height
; row
++) {
867 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
868 GLenum destType
= GL_UNSIGNED_BYTE
;
869 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
873 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
874 type
, source
, &clippedUnpack
,
875 ctx
->_ImageTransferState
);
877 if (format
== GL_DEPTH_STENCIL
) {
879 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
880 GL_FLOAT
: GL_UNSIGNED_INT
;
882 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
883 (1 << 24) - 1, type
, source
,
888 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
889 "zoom not complete");
895 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
896 spanY
= height
- row
- 1;
902 /* now pack the stencil (and Z) values in the dest format */
903 switch (pt
->resource
->format
) {
904 case PIPE_FORMAT_S8_UINT
:
906 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
907 assert(usage
== PIPE_TRANSFER_WRITE
);
908 memcpy(dest
, sValues
, width
);
911 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
912 if (format
== GL_DEPTH_STENCIL
) {
913 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
915 assert(usage
== PIPE_TRANSFER_WRITE
);
916 for (k
= 0; k
< width
; k
++) {
917 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
921 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
923 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
924 for (k
= 0; k
< width
; k
++) {
925 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
929 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
930 if (format
== GL_DEPTH_STENCIL
) {
931 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
933 assert(usage
== PIPE_TRANSFER_WRITE
);
934 for (k
= 0; k
< width
; k
++) {
935 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
939 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
941 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
942 for (k
= 0; k
< width
; k
++) {
943 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
947 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
948 if (format
== GL_DEPTH_STENCIL
) {
949 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
950 GLfloat
*destf
= (GLfloat
*)dest
;
952 assert(usage
== PIPE_TRANSFER_WRITE
);
953 for (k
= 0; k
< width
; k
++) {
954 destf
[k
*2] = zValuesFloat
[k
];
955 dest
[k
*2+1] = sValues
[k
] & 0xff;
959 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
961 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
962 for (k
= 0; k
< width
; k
++) {
963 dest
[k
*2+1] = sValues
[k
] & 0xff;
974 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
980 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
982 /* unmap the stencil buffer */
983 pipe_transfer_unmap(pipe
, pt
);
988 * Get fragment program variant for a glDrawPixels or glCopyPixels
989 * command for RGBA data.
991 static struct st_fp_variant
*
992 get_color_fp_variant(struct st_context
*st
)
994 struct gl_context
*ctx
= st
->ctx
;
995 struct st_fp_variant_key key
;
996 struct st_fp_variant
*fpv
;
998 memset(&key
, 0, sizeof(key
));
1002 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1003 ctx
->Pixel
.RedScale
!= 1.0 ||
1004 ctx
->Pixel
.GreenBias
!= 0.0 ||
1005 ctx
->Pixel
.GreenScale
!= 1.0 ||
1006 ctx
->Pixel
.BlueBias
!= 0.0 ||
1007 ctx
->Pixel
.BlueScale
!= 1.0 ||
1008 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1009 ctx
->Pixel
.AlphaScale
!= 1.0);
1010 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1011 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1012 st
->ctx
->Color
._ClampFragmentColor
;
1014 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1021 * Clamp glDrawPixels width and height to the maximum texture size.
1024 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1025 struct gl_pixelstore_attrib
*unpack
)
1028 1 << (pipe
->screen
->get_param(pipe
->screen
,
1029 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1031 if (*width
> maxSize
) {
1032 if (unpack
->RowLength
== 0)
1033 unpack
->RowLength
= *width
;
1036 if (*height
> maxSize
) {
1043 * Called via ctx->Driver.DrawPixels()
1046 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1047 GLsizei width
, GLsizei height
,
1048 GLenum format
, GLenum type
,
1049 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1051 void *driver_vp
, *driver_fp
;
1052 struct st_context
*st
= st_context(ctx
);
1053 const GLfloat
*color
;
1054 struct pipe_context
*pipe
= st
->pipe
;
1055 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1056 struct pipe_sampler_view
*sv
[2] = { NULL
};
1057 int num_sampler_view
= 1;
1058 struct gl_pixelstore_attrib clippedUnpack
;
1060 /* Mesa state should be up to date by now */
1061 assert(ctx
->NewState
== 0x0);
1063 st_validate_state(st
);
1065 /* Limit the size of the glDrawPixels to the max texture size.
1066 * Strictly speaking, that's not correct but since we don't handle
1067 * larger images yet, this is better than crashing.
1069 clippedUnpack
= *unpack
;
1070 unpack
= &clippedUnpack
;
1071 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1073 if (format
== GL_DEPTH_STENCIL
)
1074 write_stencil
= write_depth
= GL_TRUE
;
1075 else if (format
== GL_STENCIL_INDEX
)
1076 write_stencil
= GL_TRUE
;
1077 else if (format
== GL_DEPTH_COMPONENT
)
1078 write_depth
= GL_TRUE
;
1080 if (write_stencil
&&
1081 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1082 /* software fallback */
1083 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1089 * Get vertex/fragment shaders
1091 if (write_depth
|| write_stencil
) {
1092 driver_fp
= get_drawpix_z_stencil_program(st
, write_depth
,
1094 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1095 color
= ctx
->Current
.RasterColor
;
1098 struct st_fp_variant
*fpv
= get_color_fp_variant(st
);
1100 driver_fp
= fpv
->driver_shader
;
1101 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1104 if (st
->pixel_xfer
.pixelmap_enabled
) {
1105 pipe_sampler_view_reference(&sv
[1],
1106 st
->pixel_xfer
.pixelmap_sampler_view
);
1110 /* update fragment program constants */
1111 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1114 /* draw with textured quad */
1116 struct pipe_resource
*pt
1117 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1119 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1122 /* Create a second sampler view to read stencil.
1123 * The stencil is written using the shader stencil export
1125 if (write_stencil
) {
1126 enum pipe_format stencil_format
=
1127 util_format_stencil_only(pt
->format
);
1128 /* we should not be doing pixel map/transfer (see above) */
1129 assert(num_sampler_view
== 1);
1130 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1135 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1137 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1142 color
, GL_FALSE
, write_depth
, write_stencil
);
1143 pipe_sampler_view_reference(&sv
[0], NULL
);
1144 if (num_sampler_view
> 1)
1145 pipe_sampler_view_reference(&sv
[1], NULL
);
1147 pipe_resource_reference(&pt
, NULL
);
1155 * Software fallback for glCopyPixels(GL_STENCIL).
1158 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1159 GLsizei width
, GLsizei height
,
1160 GLint dstx
, GLint dsty
)
1162 struct st_renderbuffer
*rbDraw
;
1163 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1164 enum pipe_transfer_usage usage
;
1165 struct pipe_transfer
*ptDraw
;
1170 buffer
= malloc(width
* height
* sizeof(ubyte
));
1172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1176 /* Get the dest renderbuffer */
1177 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1178 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1180 /* this will do stencil pixel transfer ops */
1181 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1182 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1183 &ctx
->DefaultPacking
, buffer
);
1186 /* debug code: dump stencil values */
1188 for (row
= 0; row
< height
; row
++) {
1189 printf("%3d: ", row
);
1190 for (col
= 0; col
< width
; col
++) {
1191 printf("%02x ", buffer
[col
+ row
* width
]);
1197 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1198 usage
= PIPE_TRANSFER_READ_WRITE
;
1200 usage
= PIPE_TRANSFER_WRITE
;
1202 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1203 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1206 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1207 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1209 /* map the stencil buffer */
1210 drawMap
= pipe_transfer_map(pipe
,
1212 rbDraw
->surface
->u
.tex
.level
,
1213 rbDraw
->surface
->u
.tex
.first_layer
,
1215 width
, height
, &ptDraw
);
1218 /* XXX PixelZoom not handled yet */
1219 for (i
= 0; i
< height
; i
++) {
1226 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1230 dst
= drawMap
+ y
* ptDraw
->stride
;
1231 src
= buffer
+ i
* width
;
1233 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1238 /* unmap the stencil buffer */
1239 pipe_transfer_unmap(pipe
, ptDraw
);
1244 * Return renderbuffer to use for reading color pixels for glCopyPixels
1246 static struct st_renderbuffer
*
1247 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1249 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1250 struct st_renderbuffer
*strb
=
1251 st_renderbuffer(fb
->_ColorReadBuffer
);
1258 * Try to do a glCopyPixels for simple cases with a blit by calling
1261 * We can do this when we're copying color pixels (depth/stencil
1262 * eventually) with no pixel zoom, no pixel transfer ops, no
1263 * per-fragment ops, and the src/dest regions don't overlap.
1266 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1267 GLsizei width
, GLsizei height
,
1268 GLint dstx
, GLint dsty
, GLenum type
)
1270 struct st_context
*st
= st_context(ctx
);
1271 struct pipe_context
*pipe
= st
->pipe
;
1272 struct pipe_screen
*screen
= pipe
->screen
;
1273 struct gl_pixelstore_attrib pack
, unpack
;
1274 GLint readX
, readY
, readW
, readH
, drawX
, drawY
, drawW
, drawH
;
1276 if (type
== GL_COLOR
&&
1277 ctx
->Pixel
.ZoomX
== 1.0 &&
1278 ctx
->Pixel
.ZoomY
== 1.0 &&
1279 ctx
->_ImageTransferState
== 0x0 &&
1280 !ctx
->Color
.BlendEnabled
&&
1281 !ctx
->Color
.AlphaEnabled
&&
1283 !ctx
->Fog
.Enabled
&&
1284 !ctx
->Stencil
.Enabled
&&
1285 !ctx
->FragmentProgram
.Enabled
&&
1286 !ctx
->VertexProgram
.Enabled
&&
1287 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] &&
1288 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1289 !ctx
->Query
.CondRenderQuery
&&
1290 !ctx
->Query
.CurrentOcclusionObject
) {
1291 struct st_renderbuffer
*rbRead
, *rbDraw
;
1294 * Clip the read region against the src buffer bounds.
1295 * We'll still allocate a temporary buffer/texture for the original
1296 * src region size but we'll only read the region which is on-screen.
1297 * This may mean that we draw garbage pixels into the dest region, but
1304 pack
= ctx
->DefaultPacking
;
1305 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1306 return GL_TRUE
; /* all done */
1308 /* clip against dest buffer bounds and scissor box */
1309 drawX
= dstx
+ pack
.SkipPixels
;
1310 drawY
= dsty
+ pack
.SkipRows
;
1312 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1313 return GL_TRUE
; /* all done */
1315 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1316 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1321 rbRead
= st_get_color_read_renderbuffer(ctx
);
1322 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1324 /* Flip src/dst position depending on the orientation of buffers. */
1325 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1326 readY
= rbRead
->Base
.Height
- readY
;
1330 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1331 /* We can't flip the destination for pipe->blit, so we only adjust
1332 * its position and flip the source.
1334 drawY
= rbDraw
->Base
.Height
- drawY
- drawH
;
1339 if (rbRead
!= rbDraw
||
1340 !_mesa_regions_overlap(readX
, readY
, readX
+ readW
, readY
+ readH
,
1341 drawX
, drawY
, drawX
+ drawW
, drawY
+ drawH
)) {
1342 struct pipe_blit_info blit
;
1344 memset(&blit
, 0, sizeof(blit
));
1345 blit
.src
.resource
= rbRead
->texture
;
1346 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1347 blit
.src
.format
= rbRead
->texture
->format
;
1348 blit
.src
.box
.x
= readX
;
1349 blit
.src
.box
.y
= readY
;
1350 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1351 blit
.src
.box
.width
= readW
;
1352 blit
.src
.box
.height
= readH
;
1353 blit
.src
.box
.depth
= 1;
1354 blit
.dst
.resource
= rbDraw
->texture
;
1355 blit
.dst
.level
= rbDraw
->surface
->u
.tex
.level
;
1356 blit
.dst
.format
= rbDraw
->texture
->format
;
1357 blit
.dst
.box
.x
= drawX
;
1358 blit
.dst
.box
.y
= drawY
;
1359 blit
.dst
.box
.z
= rbDraw
->surface
->u
.tex
.first_layer
;
1360 blit
.dst
.box
.width
= drawW
;
1361 blit
.dst
.box
.height
= drawH
;
1362 blit
.dst
.box
.depth
= 1;
1363 blit
.mask
= PIPE_MASK_RGBA
;
1364 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1366 if (screen
->is_format_supported(screen
, blit
.src
.format
,
1367 blit
.src
.resource
->target
,
1368 blit
.src
.resource
->nr_samples
,
1369 PIPE_BIND_SAMPLER_VIEW
) &&
1370 screen
->is_format_supported(screen
, blit
.dst
.format
,
1371 blit
.dst
.resource
->target
,
1372 blit
.dst
.resource
->nr_samples
,
1373 PIPE_BIND_RENDER_TARGET
)) {
1374 pipe
->blit(pipe
, &blit
);
1385 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1386 GLsizei width
, GLsizei height
,
1387 GLint dstx
, GLint dsty
, GLenum type
)
1389 struct st_context
*st
= st_context(ctx
);
1390 struct pipe_context
*pipe
= st
->pipe
;
1391 struct pipe_screen
*screen
= pipe
->screen
;
1392 struct st_renderbuffer
*rbRead
;
1393 void *driver_vp
, *driver_fp
;
1394 struct pipe_resource
*pt
;
1395 struct pipe_sampler_view
*sv
[2] = { NULL
};
1396 int num_sampler_view
= 1;
1398 enum pipe_format srcFormat
;
1400 GLboolean invertTex
= GL_FALSE
;
1401 GLint readX
, readY
, readW
, readH
;
1402 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1404 st_validate_state(st
);
1406 if (type
== GL_DEPTH_STENCIL
) {
1407 /* XXX make this more efficient */
1408 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1409 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1413 if (type
== GL_STENCIL
) {
1414 /* can't use texturing to do stencil */
1415 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1419 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1423 * The subsequent code implements glCopyPixels by copying the source
1424 * pixels into a temporary texture that's then applied to a textured quad.
1425 * When we draw the textured quad, all the usual per-fragment operations
1431 * Get vertex/fragment shaders
1433 if (type
== GL_COLOR
) {
1434 struct st_fp_variant
*fpv
= get_color_fp_variant(st
);
1436 rbRead
= st_get_color_read_renderbuffer(ctx
);
1439 driver_fp
= fpv
->driver_shader
;
1440 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1442 if (st
->pixel_xfer
.pixelmap_enabled
) {
1443 pipe_sampler_view_reference(&sv
[1],
1444 st
->pixel_xfer
.pixelmap_sampler_view
);
1448 /* update fragment program constants */
1449 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1452 assert(type
== GL_DEPTH
);
1453 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1454 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1455 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1457 driver_fp
= get_drawpix_z_stencil_program(st
, GL_TRUE
, GL_FALSE
);
1458 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1461 /* Choose the format for the temporary texture. */
1462 srcFormat
= rbRead
->texture
->format
;
1463 srcBind
= PIPE_BIND_SAMPLER_VIEW
|
1464 (type
== GL_COLOR
? PIPE_BIND_RENDER_TARGET
: PIPE_BIND_DEPTH_STENCIL
);
1466 if (!screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, 0,
1468 /* srcFormat is non-renderable. Find a compatible renderable format. */
1469 if (type
== GL_DEPTH
) {
1470 srcFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
, GL_NONE
,
1471 GL_NONE
, st
->internal_target
, 0,
1475 assert(type
== GL_COLOR
);
1477 if (util_format_is_float(srcFormat
)) {
1478 srcFormat
= st_choose_format(st
, GL_RGBA32F
, GL_NONE
,
1479 GL_NONE
, st
->internal_target
, 0,
1482 else if (util_format_is_pure_sint(srcFormat
)) {
1483 srcFormat
= st_choose_format(st
, GL_RGBA32I
, GL_NONE
,
1484 GL_NONE
, st
->internal_target
, 0,
1487 else if (util_format_is_pure_uint(srcFormat
)) {
1488 srcFormat
= st_choose_format(st
, GL_RGBA32UI
, GL_NONE
,
1489 GL_NONE
, st
->internal_target
, 0,
1492 else if (util_format_is_snorm(srcFormat
)) {
1493 srcFormat
= st_choose_format(st
, GL_RGBA16_SNORM
, GL_NONE
,
1494 GL_NONE
, st
->internal_target
, 0,
1498 srcFormat
= st_choose_format(st
, GL_RGBA
, GL_NONE
,
1499 GL_NONE
, st
->internal_target
, 0,
1504 if (srcFormat
== PIPE_FORMAT_NONE
) {
1505 assert(0 && "cannot choose a format for src of CopyPixels");
1510 /* Invert src region if needed */
1511 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1512 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1513 invertTex
= !invertTex
;
1516 /* Clip the read region against the src buffer bounds.
1517 * We'll still allocate a temporary buffer/texture for the original
1518 * src region size but we'll only read the region which is on-screen.
1519 * This may mean that we draw garbage pixels into the dest region, but
1526 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1527 /* The source region is completely out of bounds. Do nothing.
1528 * The GL spec says "Results of copies from outside the window,
1529 * or from regions of the window that are not exposed, are
1530 * hardware dependent and undefined."
1535 readW
= MAX2(0, readW
);
1536 readH
= MAX2(0, readH
);
1538 /* Allocate the temporary texture. */
1539 pt
= alloc_texture(st
, width
, height
, srcFormat
, srcBind
);
1543 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1545 pipe_resource_reference(&pt
, NULL
);
1549 /* Copy the src region to the temporary texture. */
1551 struct pipe_blit_info blit
;
1553 memset(&blit
, 0, sizeof(blit
));
1554 blit
.src
.resource
= rbRead
->texture
;
1555 blit
.src
.level
= rbRead
->surface
->u
.tex
.level
;
1556 blit
.src
.format
= rbRead
->texture
->format
;
1557 blit
.src
.box
.x
= readX
;
1558 blit
.src
.box
.y
= readY
;
1559 blit
.src
.box
.z
= rbRead
->surface
->u
.tex
.first_layer
;
1560 blit
.src
.box
.width
= readW
;
1561 blit
.src
.box
.height
= readH
;
1562 blit
.src
.box
.depth
= 1;
1563 blit
.dst
.resource
= pt
;
1565 blit
.dst
.format
= pt
->format
;
1566 blit
.dst
.box
.x
= pack
.SkipPixels
;
1567 blit
.dst
.box
.y
= pack
.SkipRows
;
1569 blit
.dst
.box
.width
= readW
;
1570 blit
.dst
.box
.height
= readH
;
1571 blit
.dst
.box
.depth
= 1;
1572 blit
.mask
= util_format_get_mask(pt
->format
) & ~PIPE_MASK_S
;
1573 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1575 pipe
->blit(pipe
, &blit
);
1578 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1579 * textured quad with that texture.
1581 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1582 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1587 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1589 pipe_resource_reference(&pt
, NULL
);
1590 pipe_sampler_view_reference(&sv
[0], NULL
);
1595 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1597 functions
->DrawPixels
= st_DrawPixels
;
1598 functions
->CopyPixels
= st_CopyPixels
;
1603 st_destroy_drawpix(struct st_context
*st
)
1607 for (i
= 0; i
< ARRAY_SIZE(st
->drawpix
.zs_shaders
); i
++) {
1608 if (st
->drawpix
.zs_shaders
[i
])
1609 cso_delete_fragment_shader(st
->cso_context
,
1610 st
->drawpix
.zs_shaders
[i
]);
1613 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1614 if (st
->drawpix
.vert_shaders
[0])
1615 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1616 if (st
->drawpix
.vert_shaders
[1])
1617 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);