1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/format_pack.h"
37 #include "main/macros.h"
38 #include "main/mfeatures.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 "program/program.h"
47 #include "program/prog_print.h"
48 #include "program/prog_instruction.h"
51 #include "st_atom_constbuf.h"
52 #include "st_cb_drawpixels.h"
53 #include "st_cb_readpixels.h"
54 #include "st_cb_fbo.h"
55 #include "st_context.h"
57 #include "st_format.h"
58 #include "st_program.h"
59 #include "st_texture.h"
61 #include "pipe/p_context.h"
62 #include "pipe/p_defines.h"
63 #include "tgsi/tgsi_ureg.h"
64 #include "util/u_draw_quad.h"
65 #include "util/u_format.h"
66 #include "util/u_inlines.h"
67 #include "util/u_math.h"
68 #include "util/u_tile.h"
69 #include "util/u_upload_mgr.h"
70 #include "cso_cache/cso_context.h"
76 * Check if the given program is:
77 * 0: MOVE result.color, fragment.color;
81 is_passthrough_program(const struct gl_fragment_program
*prog
)
83 if (prog
->Base
.NumInstructions
== 2) {
84 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
85 if (inst
[0].Opcode
== OPCODE_MOV
&&
86 inst
[1].Opcode
== OPCODE_END
&&
87 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
88 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
89 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
90 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
91 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
92 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
101 * Returns a fragment program which implements the current pixel transfer ops.
103 static struct gl_fragment_program
*
104 get_glsl_pixel_transfer_program(struct st_context
*st
,
105 struct st_fragment_program
*orig
)
107 int pixelMaps
= 0, scaleAndBias
= 0;
108 struct gl_context
*ctx
= st
->ctx
;
109 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
110 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
115 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
116 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
117 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
118 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
122 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
125 /* create the colormap/texture now if not already done */
126 if (!st
->pixel_xfer
.pixelmap_texture
) {
127 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
128 st
->pixel_xfer
.pixelmap_sampler_view
=
129 st_create_texture_sampler_view(st
->pipe
,
130 st
->pixel_xfer
.pixelmap_texture
);
134 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
135 scaleAndBias
, pixelMaps
);
142 * Make fragment shader for glDraw/CopyPixels. This shader is made
143 * by combining the pixel transfer shader with the user-defined shader.
144 * \param fpIn the current/incoming fragment program
145 * \param fpOut returns the combined fragment program
148 st_make_drawpix_fragment_program(struct st_context
*st
,
149 struct gl_fragment_program
*fpIn
,
150 struct gl_fragment_program
**fpOut
)
152 struct gl_program
*newProg
;
153 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
155 if (is_passthrough_program(fpIn
)) {
156 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
157 &st
->pixel_xfer
.program
->Base
);
159 else if (stfp
->glsl_to_tgsi
!= NULL
) {
160 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
165 printf("Base program:\n");
166 _mesa_print_program(&fpIn
->Base
);
167 printf("DrawPix program:\n");
168 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
170 newProg
= _mesa_combine_programs(st
->ctx
,
171 &st
->pixel_xfer
.program
->Base
.Base
,
177 printf("Combined DrawPixels program:\n");
178 _mesa_print_program(newProg
);
179 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
180 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
181 _mesa_print_parameter_list(newProg
->Parameters
);
184 *fpOut
= (struct gl_fragment_program
*) newProg
;
189 * Create fragment program that does a TEX() instruction to get a Z and/or
190 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
191 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
192 * Pass fragment color through as-is.
193 * \return pointer to the gl_fragment program
195 struct gl_fragment_program
*
196 st_make_drawpix_z_stencil_program(struct st_context
*st
,
197 GLboolean write_depth
,
198 GLboolean write_stencil
)
200 struct gl_context
*ctx
= st
->ctx
;
201 struct gl_program
*p
;
202 struct gl_fragment_program
*fp
;
204 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
206 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
208 if (st
->drawpix
.shaders
[shaderIndex
]) {
209 /* already have the proper shader */
210 return st
->drawpix
.shaders
[shaderIndex
];
216 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
220 p
->NumInstructions
= write_depth
? 2 : 1;
221 p
->NumInstructions
+= write_stencil
? 1 : 0;
223 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
224 if (!p
->Instructions
) {
225 ctx
->Driver
.DeleteProgram(ctx
, p
);
228 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
231 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
232 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
233 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
234 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
235 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
236 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
237 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
238 p
->Instructions
[ic
].TexSrcUnit
= 0;
239 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
244 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
245 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
246 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
247 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
248 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
249 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
250 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
251 p
->Instructions
[ic
].TexSrcUnit
= 1;
252 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
257 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
259 assert(ic
== p
->NumInstructions
);
261 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
262 p
->OutputsWritten
= 0;
264 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
266 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
268 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
270 p
->SamplersUsed
|= 1 << 1;
272 fp
= (struct gl_fragment_program
*) p
;
274 /* save the new shader */
275 st
->drawpix
.shaders
[shaderIndex
] = fp
;
282 * Create a simple vertex shader that just passes through the
283 * vertex position and texcoord (and optionally, color).
286 make_passthrough_vertex_shader(struct st_context
*st
,
289 if (!st
->drawpix
.vert_shaders
[passColor
]) {
290 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
295 /* MOV result.pos, vertex.pos; */
297 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
298 ureg_DECL_vs_input( ureg
, 0 ));
300 /* MOV result.texcoord0, vertex.attr[1]; */
302 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
303 ureg_DECL_vs_input( ureg
, 1 ));
306 /* MOV result.color0, vertex.attr[2]; */
308 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
309 ureg_DECL_vs_input( ureg
, 2 ));
314 st
->drawpix
.vert_shaders
[passColor
] =
315 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
318 return st
->drawpix
.vert_shaders
[passColor
];
323 * Return a texture internalFormat for drawing/copying an image
324 * of the given format and type.
327 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
330 case GL_DEPTH_COMPONENT
:
332 case GL_UNSIGNED_SHORT
:
333 return GL_DEPTH_COMPONENT16
;
335 case GL_UNSIGNED_INT
:
336 return GL_DEPTH_COMPONENT32
;
339 if (ctx
->Extensions
.ARB_depth_buffer_float
)
340 return GL_DEPTH_COMPONENT32F
;
342 return GL_DEPTH_COMPONENT
;
345 return GL_DEPTH_COMPONENT
;
348 case GL_DEPTH_STENCIL
:
350 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
351 return GL_DEPTH32F_STENCIL8
;
353 case GL_UNSIGNED_INT_24_8
:
355 return GL_DEPTH24_STENCIL8
;
358 case GL_STENCIL_INDEX
:
359 return GL_STENCIL_INDEX
;
362 if (_mesa_is_integer_format(format
)) {
366 case GL_UNSIGNED_BYTE
:
370 case GL_UNSIGNED_SHORT
:
374 case GL_UNSIGNED_INT
:
377 assert(0 && "Unexpected type in internal_format()");
378 return GL_RGBA_INTEGER
;
383 case GL_UNSIGNED_BYTE
:
384 case GL_UNSIGNED_INT_8_8_8_8
:
385 case GL_UNSIGNED_INT_8_8_8_8_REV
:
389 case GL_UNSIGNED_BYTE_3_3_2
:
390 case GL_UNSIGNED_BYTE_2_3_3_REV
:
391 case GL_UNSIGNED_SHORT_4_4_4_4
:
392 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
395 case GL_UNSIGNED_SHORT_5_6_5
:
396 case GL_UNSIGNED_SHORT_5_6_5_REV
:
397 case GL_UNSIGNED_SHORT_5_5_5_1
:
398 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
401 case GL_UNSIGNED_INT_10_10_10_2
:
402 case GL_UNSIGNED_INT_2_10_10_10_REV
:
405 case GL_UNSIGNED_SHORT
:
406 case GL_UNSIGNED_INT
:
411 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
416 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
418 case GL_HALF_FLOAT_ARB
:
420 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
421 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
426 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
427 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
429 case GL_UNSIGNED_INT_5_9_9_9_REV
:
430 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
433 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
434 assert(ctx
->Extensions
.EXT_packed_float
);
435 return GL_R11F_G11F_B10F
;
443 * Create a temporary texture to hold an image of the given size.
444 * If width, height are not POT and the driver only handles POT textures,
445 * allocate the next larger size of texture that is POT.
447 static struct pipe_resource
*
448 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
449 enum pipe_format texFormat
)
451 struct pipe_resource
*pt
;
453 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
454 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
461 * Make texture containing an image for glDrawPixels image.
462 * If 'pixels' is NULL, leave the texture image data undefined.
464 static struct pipe_resource
*
465 make_texture(struct st_context
*st
,
466 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
467 const struct gl_pixelstore_attrib
*unpack
,
468 const GLvoid
*pixels
)
470 struct gl_context
*ctx
= st
->ctx
;
471 struct pipe_context
*pipe
= st
->pipe
;
473 struct pipe_resource
*pt
;
474 enum pipe_format pipeFormat
;
475 GLenum baseInternalFormat
, intFormat
;
477 intFormat
= internal_format(ctx
, format
, type
);
478 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
480 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
481 format
, type
, GL_FALSE
);
484 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
487 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
491 /* alloc temporary texture */
492 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
494 _mesa_unmap_pbo_source(ctx
, unpack
);
499 struct pipe_transfer
*transfer
;
502 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
504 /* we'll do pixel transfer in a fragment shader */
505 ctx
->_ImageTransferState
= 0x0;
507 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
508 PIPE_TRANSFER_WRITE
, 0, 0,
511 /* map texture transfer */
512 dest
= pipe_transfer_map(pipe
, transfer
);
515 /* Put image into texture transfer.
516 * Note that the image is actually going to be upside down in
517 * the texture. We deal with that with texcoords.
519 success
= _mesa_texstore(ctx
, 2, /* dims */
520 baseInternalFormat
, /* baseInternalFormat */
521 mformat
, /* gl_format */
522 transfer
->stride
, /* dstRowStride, bytes */
523 &dest
, /* destSlices */
524 width
, height
, 1, /* size */
525 format
, type
, /* src format/type */
526 pixels
, /* data source */
530 pipe_transfer_unmap(pipe
, transfer
);
531 pipe
->transfer_destroy(pipe
, transfer
);
536 ctx
->_ImageTransferState
= imageTransferStateSave
;
539 _mesa_unmap_pbo_source(ctx
, unpack
);
546 * Draw quad with texcoords and optional color.
547 * Coords are gallium window coords with y=0=top.
548 * \param color may be null
549 * \param invertTex if true, flip texcoords vertically
552 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
553 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
554 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
556 struct st_context
*st
= st_context(ctx
);
557 struct pipe_context
*pipe
= st
->pipe
;
558 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
559 struct pipe_resource
*buf
= NULL
;
562 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
, &buf
,
568 /* setup vertex data */
570 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
571 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
572 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
573 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
574 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
575 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
576 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
577 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
578 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
579 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
583 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
584 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
587 verts
[1][0][0] = clip_x1
;
588 verts
[1][0][1] = clip_y0
;
591 verts
[2][0][0] = clip_x1
;
592 verts
[2][0][1] = clip_y1
;
595 verts
[3][0][0] = clip_x0
;
596 verts
[3][0][1] = clip_y1
;
598 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
599 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
600 verts
[1][1][0] = sRight
;
601 verts
[1][1][1] = tTop
;
602 verts
[2][1][0] = sRight
;
603 verts
[2][1][1] = tBot
;
604 verts
[3][1][0] = sLeft
;
605 verts
[3][1][1] = tBot
;
607 /* same for all verts: */
609 for (i
= 0; i
< 4; i
++) {
610 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
611 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
612 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
613 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
614 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
615 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
616 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
617 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
621 for (i
= 0; i
< 4; i
++) {
622 verts
[i
][0][2] = z
; /*Z*/
623 verts
[i
][0][3] = 1.0f
; /*W*/
624 verts
[i
][1][2] = 0.0f
; /*R*/
625 verts
[i
][1][3] = 1.0f
; /*Q*/
630 u_upload_unmap(st
->uploader
);
631 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, offset
,
634 3); /* attribs/vert */
635 pipe_resource_reference(&buf
, NULL
);
641 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
642 GLsizei width
, GLsizei height
,
643 GLfloat zoomX
, GLfloat zoomY
,
644 struct pipe_sampler_view
**sv
,
645 int num_sampler_view
,
648 const GLfloat
*color
,
650 GLboolean write_depth
, GLboolean write_stencil
)
652 struct st_context
*st
= st_context(ctx
);
653 struct pipe_context
*pipe
= st
->pipe
;
654 struct cso_context
*cso
= st
->cso_context
;
655 GLfloat x0
, y0
, x1
, y1
;
657 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
660 /* XXX if DrawPixels image is larger than max texture size, break
663 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
664 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
665 assert(width
<= maxSize
);
666 assert(height
<= maxSize
);
668 cso_save_rasterizer(cso
);
669 cso_save_viewport(cso
);
670 cso_save_samplers(cso
);
671 cso_save_fragment_sampler_views(cso
);
672 cso_save_fragment_shader(cso
);
673 cso_save_stream_outputs(cso
);
674 cso_save_vertex_shader(cso
);
675 cso_save_geometry_shader(cso
);
676 cso_save_vertex_elements(cso
);
677 cso_save_vertex_buffers(cso
);
679 cso_save_depth_stencil_alpha(cso
);
683 /* rasterizer state: just scissor */
685 struct pipe_rasterizer_state rasterizer
;
686 memset(&rasterizer
, 0, sizeof(rasterizer
));
687 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
688 ctx
->Color
._ClampFragmentColor
;
689 rasterizer
.gl_rasterization_rules
= 1;
690 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
691 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
692 cso_set_rasterizer(cso
, &rasterizer
);
696 /* Stencil writing bypasses the normal fragment pipeline to
697 * disable color writing and set stencil test to always pass.
699 struct pipe_depth_stencil_alpha_state dsa
;
700 struct pipe_blend_state blend
;
703 memset(&dsa
, 0, sizeof(dsa
));
704 dsa
.stencil
[0].enabled
= 1;
705 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
706 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
707 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
709 /* writing depth+stencil: depth test always passes */
710 dsa
.depth
.enabled
= 1;
711 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
712 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
714 cso_set_depth_stencil_alpha(cso
, &dsa
);
716 /* blend (colormask) */
717 memset(&blend
, 0, sizeof(blend
));
718 cso_set_blend(cso
, &blend
);
721 /* fragment shader state: TEX lookup program */
722 cso_set_fragment_shader_handle(cso
, driver_fp
);
724 /* vertex shader state: position + texcoord pass-through */
725 cso_set_vertex_shader_handle(cso
, driver_vp
);
727 /* geometry shader state: disabled */
728 cso_set_geometry_shader_handle(cso
, NULL
);
730 /* texture sampling state: */
732 struct pipe_sampler_state 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_single_sampler(cso
, 0, &sampler
);
743 if (num_sampler_view
> 1) {
744 cso_single_sampler(cso
, 1, &sampler
);
746 cso_single_sampler_done(cso
);
749 /* viewport state: viewport matching window dims */
751 const float w
= (float) ctx
->DrawBuffer
->Width
;
752 const float h
= (float) ctx
->DrawBuffer
->Height
;
753 struct pipe_viewport_state vp
;
754 vp
.scale
[0] = 0.5f
* w
;
755 vp
.scale
[1] = -0.5f
* h
;
758 vp
.translate
[0] = 0.5f
* w
;
759 vp
.translate
[1] = 0.5f
* h
;
760 vp
.translate
[2] = 0.5f
;
761 vp
.translate
[3] = 0.0f
;
762 cso_set_viewport(cso
, &vp
);
765 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
766 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
769 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
771 /* Compute Gallium window coords (y=0=top) with pixel zoom.
772 * Recall that these coords are transformed by the current
773 * vertex shader and viewport transformation.
775 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
776 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
777 invertTex
= !invertTex
;
781 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
783 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
785 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
788 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
789 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
790 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
793 cso_restore_rasterizer(cso
);
794 cso_restore_viewport(cso
);
795 cso_restore_samplers(cso
);
796 cso_restore_fragment_sampler_views(cso
);
797 cso_restore_fragment_shader(cso
);
798 cso_restore_vertex_shader(cso
);
799 cso_restore_geometry_shader(cso
);
800 cso_restore_vertex_elements(cso
);
801 cso_restore_vertex_buffers(cso
);
802 cso_restore_stream_outputs(cso
);
804 cso_restore_depth_stencil_alpha(cso
);
805 cso_restore_blend(cso
);
811 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
812 * can't use a fragment shader to write stencil values.
815 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
816 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
817 const struct gl_pixelstore_attrib
*unpack
,
818 const GLvoid
*pixels
)
820 struct st_context
*st
= st_context(ctx
);
821 struct pipe_context
*pipe
= st
->pipe
;
822 struct st_renderbuffer
*strb
;
823 enum pipe_transfer_usage usage
;
824 struct pipe_transfer
*pt
;
825 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
827 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
832 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
834 /* totally clipped */
839 strb
= st_renderbuffer(ctx
->DrawBuffer
->
840 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
842 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
843 y
= ctx
->DrawBuffer
->Height
- y
- height
;
846 if (format
== GL_STENCIL_INDEX
&&
847 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
848 /* writing stencil to a combined depth+stencil buffer */
849 usage
= PIPE_TRANSFER_READ_WRITE
;
852 usage
= PIPE_TRANSFER_WRITE
;
855 pt
= pipe_get_transfer(pipe
, strb
->texture
,
856 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
860 stmap
= pipe_transfer_map(pipe
, pt
);
862 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
865 sValues
= (GLubyte
*) malloc(width
* sizeof(GLubyte
));
866 zValues
= (GLuint
*) malloc(width
* sizeof(GLuint
));
868 if (sValues
&& zValues
) {
870 for (row
= 0; row
< height
; row
++) {
871 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
872 GLenum destType
= GL_UNSIGNED_BYTE
;
873 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
877 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
878 type
, source
, &clippedUnpack
,
879 ctx
->_ImageTransferState
);
881 if (format
== GL_DEPTH_STENCIL
) {
883 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
884 GL_FLOAT
: GL_UNSIGNED_INT
;
886 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
887 (1 << 24) - 1, type
, source
,
892 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
893 "zoom not complete");
899 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
900 spanY
= height
- row
- 1;
906 /* now pack the stencil (and Z) values in the dest format */
907 switch (pt
->resource
->format
) {
908 case PIPE_FORMAT_S8_UINT
:
910 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
911 assert(usage
== PIPE_TRANSFER_WRITE
);
912 memcpy(dest
, sValues
, width
);
915 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
916 if (format
== GL_DEPTH_STENCIL
) {
917 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
919 assert(usage
== PIPE_TRANSFER_WRITE
);
920 for (k
= 0; k
< width
; k
++) {
921 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
925 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
927 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
928 for (k
= 0; k
< width
; k
++) {
929 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
933 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
934 if (format
== GL_DEPTH_STENCIL
) {
935 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
937 assert(usage
== PIPE_TRANSFER_WRITE
);
938 for (k
= 0; k
< width
; k
++) {
939 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
943 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
945 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
946 for (k
= 0; k
< width
; k
++) {
947 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
951 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
952 if (format
== GL_DEPTH_STENCIL
) {
953 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
954 GLfloat
*destf
= (GLfloat
*)dest
;
956 assert(usage
== PIPE_TRANSFER_WRITE
);
957 for (k
= 0; k
< width
; k
++) {
958 destf
[k
*2] = zValuesFloat
[k
];
959 dest
[k
*2+1] = sValues
[k
] & 0xff;
963 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
965 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
966 for (k
= 0; k
< width
; k
++) {
967 dest
[k
*2+1] = sValues
[k
] & 0xff;
978 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
984 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
986 /* unmap the stencil buffer */
987 pipe_transfer_unmap(pipe
, pt
);
988 pipe
->transfer_destroy(pipe
, pt
);
993 * Get fragment program variant for a glDrawPixels or glCopyPixels
994 * command for RGBA data.
996 static struct st_fp_variant
*
997 get_color_fp_variant(struct st_context
*st
)
999 struct gl_context
*ctx
= st
->ctx
;
1000 struct st_fp_variant_key key
;
1001 struct st_fp_variant
*fpv
;
1003 memset(&key
, 0, sizeof(key
));
1007 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1008 ctx
->Pixel
.RedScale
!= 1.0 ||
1009 ctx
->Pixel
.GreenBias
!= 0.0 ||
1010 ctx
->Pixel
.GreenScale
!= 1.0 ||
1011 ctx
->Pixel
.BlueBias
!= 0.0 ||
1012 ctx
->Pixel
.BlueScale
!= 1.0 ||
1013 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1014 ctx
->Pixel
.AlphaScale
!= 1.0);
1015 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1016 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1017 st
->ctx
->Color
._ClampFragmentColor
;
1019 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1026 * Get fragment program variant for a glDrawPixels or glCopyPixels
1027 * command for depth/stencil data.
1029 static struct st_fp_variant
*
1030 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1031 GLboolean write_stencil
)
1033 struct st_fp_variant_key key
;
1034 struct st_fp_variant
*fpv
;
1036 memset(&key
, 0, sizeof(key
));
1040 key
.drawpixels_z
= write_depth
;
1041 key
.drawpixels_stencil
= write_stencil
;
1043 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1050 * Called via ctx->Driver.DrawPixels()
1053 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1054 GLsizei width
, GLsizei height
,
1055 GLenum format
, GLenum type
,
1056 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1058 void *driver_vp
, *driver_fp
;
1059 struct st_context
*st
= st_context(ctx
);
1060 const GLfloat
*color
;
1061 struct pipe_context
*pipe
= st
->pipe
;
1062 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1063 struct pipe_sampler_view
*sv
[2];
1064 int num_sampler_view
= 1;
1065 struct st_fp_variant
*fpv
;
1067 if (format
== GL_DEPTH_STENCIL
)
1068 write_stencil
= write_depth
= GL_TRUE
;
1069 else if (format
== GL_STENCIL_INDEX
)
1070 write_stencil
= GL_TRUE
;
1071 else if (format
== GL_DEPTH_COMPONENT
)
1072 write_depth
= GL_TRUE
;
1074 if (write_stencil
&&
1075 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1076 /* software fallback */
1077 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1082 /* Mesa state should be up to date by now */
1083 assert(ctx
->NewState
== 0x0);
1085 st_validate_state(st
);
1088 * Get vertex/fragment shaders
1090 if (write_depth
|| write_stencil
) {
1091 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1093 driver_fp
= fpv
->driver_shader
;
1095 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1097 color
= ctx
->Current
.RasterColor
;
1100 fpv
= get_color_fp_variant(st
);
1102 driver_fp
= fpv
->driver_shader
;
1104 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1107 if (st
->pixel_xfer
.pixelmap_enabled
) {
1108 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1113 /* update fragment program constants */
1114 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1116 /* draw with textured quad */
1118 struct pipe_resource
*pt
1119 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1121 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1124 /* Create a second sampler view to read stencil.
1125 * The stencil is written using the shader stencil export
1127 if (write_stencil
) {
1128 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1130 switch (pt
->format
) {
1131 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1132 case PIPE_FORMAT_X24S8_UINT
:
1133 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1135 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1136 case PIPE_FORMAT_S8X24_UINT
:
1137 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1139 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1140 case PIPE_FORMAT_X32_S8X24_UINT
:
1141 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1143 case PIPE_FORMAT_S8_UINT
:
1144 stencil_format
= PIPE_FORMAT_S8_UINT
;
1150 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1155 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1157 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1162 color
, GL_FALSE
, write_depth
, write_stencil
);
1163 pipe_sampler_view_reference(&sv
[0], NULL
);
1164 if (num_sampler_view
> 1)
1165 pipe_sampler_view_reference(&sv
[1], NULL
);
1167 pipe_resource_reference(&pt
, NULL
);
1175 * Software fallback for glCopyPixels(GL_STENCIL).
1178 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1179 GLsizei width
, GLsizei height
,
1180 GLint dstx
, GLint dsty
)
1182 struct st_renderbuffer
*rbDraw
;
1183 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1184 enum pipe_transfer_usage usage
;
1185 struct pipe_transfer
*ptDraw
;
1190 buffer
= malloc(width
* height
* sizeof(ubyte
));
1192 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1196 /* Get the dest renderbuffer */
1197 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1198 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1200 /* this will do stencil pixel transfer ops */
1201 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1202 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1203 &ctx
->DefaultPacking
, buffer
);
1206 /* debug code: dump stencil values */
1208 for (row
= 0; row
< height
; row
++) {
1209 printf("%3d: ", row
);
1210 for (col
= 0; col
< width
; col
++) {
1211 printf("%02x ", buffer
[col
+ row
* width
]);
1217 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1218 usage
= PIPE_TRANSFER_READ_WRITE
;
1220 usage
= PIPE_TRANSFER_WRITE
;
1222 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1223 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1226 ptDraw
= pipe_get_transfer(pipe
,
1229 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1233 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1234 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1236 /* map the stencil buffer */
1237 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1240 /* XXX PixelZoom not handled yet */
1241 for (i
= 0; i
< height
; i
++) {
1248 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1252 dst
= drawMap
+ y
* ptDraw
->stride
;
1253 src
= buffer
+ i
* width
;
1255 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1260 /* unmap the stencil buffer */
1261 pipe_transfer_unmap(pipe
, ptDraw
);
1262 pipe
->transfer_destroy(pipe
, ptDraw
);
1267 * Return renderbuffer to use for reading color pixels for glCopyPixels
1269 static struct st_renderbuffer
*
1270 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1272 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1273 struct st_renderbuffer
*strb
=
1274 st_renderbuffer(fb
->_ColorReadBuffer
);
1280 /** Do the src/dest regions overlap? */
1282 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1283 GLsizei width
, GLsizei height
)
1285 if (srcX
+ width
<= dstX
||
1286 dstX
+ width
<= srcX
||
1287 srcY
+ height
<= dstY
||
1288 dstY
+ height
<= srcY
)
1296 * Try to do a glCopyPixels for simple cases with a blit by calling
1297 * pipe->resource_copy_region().
1299 * We can do this when we're copying color pixels (depth/stencil
1300 * eventually) with no pixel zoom, no pixel transfer ops, no
1301 * per-fragment ops, the src/dest regions don't overlap and the
1302 * src/dest pixel formats are the same.
1305 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1306 GLsizei width
, GLsizei height
,
1307 GLint dstx
, GLint dsty
, GLenum type
)
1309 struct st_context
*st
= st_context(ctx
);
1310 struct pipe_context
*pipe
= st
->pipe
;
1311 struct gl_pixelstore_attrib pack
, unpack
;
1312 GLint readX
, readY
, readW
, readH
;
1314 if (type
== GL_COLOR
&&
1315 ctx
->Pixel
.ZoomX
== 1.0 &&
1316 ctx
->Pixel
.ZoomY
== 1.0 &&
1317 ctx
->_ImageTransferState
== 0x0 &&
1318 !ctx
->Color
.BlendEnabled
&&
1319 !ctx
->Color
.AlphaEnabled
&&
1321 !ctx
->Fog
.Enabled
&&
1322 !ctx
->Stencil
.Enabled
&&
1323 !ctx
->FragmentProgram
.Enabled
&&
1324 !ctx
->VertexProgram
.Enabled
&&
1325 !ctx
->Shader
.CurrentFragmentProgram
&&
1326 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1327 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1328 !ctx
->Query
.CondRenderQuery
) {
1329 struct st_renderbuffer
*rbRead
, *rbDraw
;
1333 * Clip the read region against the src buffer bounds.
1334 * We'll still allocate a temporary buffer/texture for the original
1335 * src region size but we'll only read the region which is on-screen.
1336 * This may mean that we draw garbage pixels into the dest region, but
1343 pack
= ctx
->DefaultPacking
;
1344 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1345 return GL_TRUE
; /* all done */
1347 /* clip against dest buffer bounds and scissor box */
1348 drawX
= dstx
+ pack
.SkipPixels
;
1349 drawY
= dsty
+ pack
.SkipRows
;
1351 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1352 return GL_TRUE
; /* all done */
1354 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1355 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1357 rbRead
= st_get_color_read_renderbuffer(ctx
);
1358 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1360 if ((rbRead
!= rbDraw
||
1361 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1362 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1363 struct pipe_box srcBox
;
1365 /* flip src/dst position if needed */
1366 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1367 /* both buffers will have the same orientation */
1368 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1369 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1372 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1374 pipe
->resource_copy_region(pipe
,
1376 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1378 rbRead
->rtt_level
, &srcBox
);
1388 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1389 GLsizei width
, GLsizei height
,
1390 GLint dstx
, GLint dsty
, GLenum type
)
1392 struct st_context
*st
= st_context(ctx
);
1393 struct pipe_context
*pipe
= st
->pipe
;
1394 struct pipe_screen
*screen
= pipe
->screen
;
1395 struct st_renderbuffer
*rbRead
;
1396 void *driver_vp
, *driver_fp
;
1397 struct pipe_resource
*pt
;
1398 struct pipe_sampler_view
*sv
[2];
1399 int num_sampler_view
= 1;
1401 enum pipe_format srcFormat
, texFormat
;
1402 GLboolean invertTex
= GL_FALSE
;
1403 GLint readX
, readY
, readW
, readH
;
1404 GLuint sample_count
;
1405 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1406 struct st_fp_variant
*fpv
;
1408 st_validate_state(st
);
1410 if (type
== GL_DEPTH_STENCIL
) {
1411 /* XXX make this more efficient */
1412 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1413 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1417 if (type
== GL_STENCIL
) {
1418 /* can't use texturing to do stencil */
1419 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1423 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1427 * The subsequent code implements glCopyPixels by copying the source
1428 * pixels into a temporary texture that's then applied to a textured quad.
1429 * When we draw the textured quad, all the usual per-fragment operations
1435 * Get vertex/fragment shaders
1437 if (type
== GL_COLOR
) {
1438 rbRead
= st_get_color_read_renderbuffer(ctx
);
1441 fpv
= get_color_fp_variant(st
);
1442 driver_fp
= fpv
->driver_shader
;
1444 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1446 if (st
->pixel_xfer
.pixelmap_enabled
) {
1447 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
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 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1458 driver_fp
= fpv
->driver_shader
;
1460 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1463 /* update fragment program constants */
1464 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1466 sample_count
= rbRead
->texture
->nr_samples
;
1467 /* I believe this would be legal, presumably would need to do a resolve
1468 for color, and for depth/stencil spec says to just use one of the
1469 depth/stencil samples per pixel? Need some transfer clarifications. */
1470 assert(sample_count
< 2);
1472 srcFormat
= rbRead
->texture
->format
;
1474 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1476 PIPE_BIND_SAMPLER_VIEW
)) {
1477 texFormat
= srcFormat
;
1480 /* srcFormat can't be used as a texture format */
1481 if (type
== GL_DEPTH
) {
1482 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1483 GL_NONE
, GL_NONE
, st
->internal_target
,
1484 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1485 assert(texFormat
!= PIPE_FORMAT_NONE
);
1488 /* default color format */
1489 texFormat
= st_choose_format(screen
, GL_RGBA
,
1490 GL_NONE
, GL_NONE
, st
->internal_target
,
1491 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1492 assert(texFormat
!= PIPE_FORMAT_NONE
);
1496 /* Invert src region if needed */
1497 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1498 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1499 invertTex
= !invertTex
;
1502 /* Clip the read region against the src buffer bounds.
1503 * We'll still allocate a temporary buffer/texture for the original
1504 * src region size but we'll only read the region which is on-screen.
1505 * This may mean that we draw garbage pixels into the dest region, but
1512 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1513 /* The source region is completely out of bounds. Do nothing.
1514 * The GL spec says "Results of copies from outside the window,
1515 * or from regions of the window that are not exposed, are
1516 * hardware dependent and undefined."
1521 readW
= MAX2(0, readW
);
1522 readH
= MAX2(0, readH
);
1524 /* alloc temporary texture */
1525 pt
= alloc_texture(st
, width
, height
, texFormat
);
1529 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1531 pipe_resource_reference(&pt
, NULL
);
1535 /* Make temporary texture which is a copy of the src region.
1537 if (srcFormat
== texFormat
) {
1538 struct pipe_box src_box
;
1539 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1540 /* copy source framebuffer surface into mipmap/texture */
1541 pipe
->resource_copy_region(pipe
,
1544 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1545 rbRead
->texture
, /* src tex */
1546 rbRead
->rtt_level
, /* src lvl */
1551 /* CPU-based fallback/conversion */
1552 struct pipe_transfer
*ptRead
=
1553 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1555 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1557 readX
, readY
, readW
, readH
);
1558 struct pipe_transfer
*ptTex
;
1559 enum pipe_transfer_usage transfer_usage
;
1561 if (ST_DEBUG
& DEBUG_FALLBACK
)
1562 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1564 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1565 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1567 transfer_usage
= PIPE_TRANSFER_WRITE
;
1569 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1570 0, 0, width
, height
);
1572 /* copy image from ptRead surface to ptTex surface */
1573 if (type
== GL_COLOR
) {
1574 /* alternate path using get/put_tile() */
1575 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1576 enum pipe_format readFormat
, drawFormat
;
1577 readFormat
= util_format_linear(rbRead
->texture
->format
);
1578 drawFormat
= util_format_linear(pt
->format
);
1579 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1581 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1582 readW
, readH
, drawFormat
, buf
);
1587 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1588 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1589 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1594 pipe
->transfer_destroy(pipe
, ptRead
);
1595 pipe
->transfer_destroy(pipe
, ptTex
);
1598 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1599 * textured quad with that texture.
1601 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1602 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1607 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1609 pipe_resource_reference(&pt
, NULL
);
1610 pipe_sampler_view_reference(&sv
[0], NULL
);
1615 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1617 functions
->DrawPixels
= st_DrawPixels
;
1618 functions
->CopyPixels
= st_CopyPixels
;
1623 st_destroy_drawpix(struct st_context
*st
)
1627 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1628 if (st
->drawpix
.shaders
[i
])
1629 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1632 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1633 if (st
->drawpix
.vert_shaders
[0])
1634 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1635 if (st
->drawpix
.vert_shaders
[1])
1636 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1639 #endif /* FEATURE_drawpix */