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 "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
== FRAG_ATTRIB_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.
192 * \return pointer to the gl_fragment program
194 struct gl_fragment_program
*
195 st_make_drawpix_z_stencil_program(struct st_context
*st
,
196 GLboolean write_depth
,
197 GLboolean write_stencil
)
199 struct gl_context
*ctx
= st
->ctx
;
200 struct gl_program
*p
;
201 struct gl_fragment_program
*fp
;
203 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
205 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
207 if (st
->drawpix
.shaders
[shaderIndex
]) {
208 /* already have the proper shader */
209 return st
->drawpix
.shaders
[shaderIndex
];
215 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
219 p
->NumInstructions
= write_depth
? 2 : 1;
220 p
->NumInstructions
+= write_stencil
? 1 : 0;
222 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
223 if (!p
->Instructions
) {
224 ctx
->Driver
.DeleteProgram(ctx
, p
);
227 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
230 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
231 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
232 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
233 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
234 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
235 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
236 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
237 p
->Instructions
[ic
].TexSrcUnit
= 0;
238 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
243 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
244 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
245 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
246 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
247 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
248 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
249 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
250 p
->Instructions
[ic
].TexSrcUnit
= 1;
251 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
256 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
258 assert(ic
== p
->NumInstructions
);
260 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
261 p
->OutputsWritten
= 0;
263 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
265 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
267 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
269 p
->SamplersUsed
|= 1 << 1;
271 fp
= (struct gl_fragment_program
*) p
;
273 /* save the new shader */
274 st
->drawpix
.shaders
[shaderIndex
] = fp
;
281 * Create a simple vertex shader that just passes through the
282 * vertex position and texcoord (and optionally, color).
285 make_passthrough_vertex_shader(struct st_context
*st
,
288 if (!st
->drawpix
.vert_shaders
[passColor
]) {
289 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
294 /* MOV result.pos, vertex.pos; */
296 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
297 ureg_DECL_vs_input( ureg
, 0 ));
299 /* MOV result.texcoord0, vertex.attr[1]; */
301 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
302 ureg_DECL_vs_input( ureg
, 1 ));
305 /* MOV result.color0, vertex.attr[2]; */
307 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
308 ureg_DECL_vs_input( ureg
, 2 ));
313 st
->drawpix
.vert_shaders
[passColor
] =
314 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
317 return st
->drawpix
.vert_shaders
[passColor
];
322 * Return a texture internalFormat for drawing/copying an image
323 * of the given format and type.
326 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
329 case GL_DEPTH_COMPONENT
:
331 case GL_UNSIGNED_SHORT
:
332 return GL_DEPTH_COMPONENT16
;
334 case GL_UNSIGNED_INT
:
335 return GL_DEPTH_COMPONENT32
;
338 if (ctx
->Extensions
.ARB_depth_buffer_float
)
339 return GL_DEPTH_COMPONENT32F
;
341 return GL_DEPTH_COMPONENT
;
344 return GL_DEPTH_COMPONENT
;
347 case GL_DEPTH_STENCIL
:
349 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
350 return GL_DEPTH32F_STENCIL8
;
352 case GL_UNSIGNED_INT_24_8
:
354 return GL_DEPTH24_STENCIL8
;
357 case GL_STENCIL_INDEX
:
358 return GL_STENCIL_INDEX
;
361 if (_mesa_is_integer_format(format
)) {
365 case GL_UNSIGNED_BYTE
:
369 case GL_UNSIGNED_SHORT
:
373 case GL_UNSIGNED_INT
:
376 assert(0 && "Unexpected type in internal_format()");
377 return GL_RGBA_INTEGER
;
382 case GL_UNSIGNED_BYTE
:
383 case GL_UNSIGNED_INT_8_8_8_8
:
384 case GL_UNSIGNED_INT_8_8_8_8_REV
:
388 case GL_UNSIGNED_BYTE_3_3_2
:
389 case GL_UNSIGNED_BYTE_2_3_3_REV
:
390 case GL_UNSIGNED_SHORT_4_4_4_4
:
391 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
394 case GL_UNSIGNED_SHORT_5_6_5
:
395 case GL_UNSIGNED_SHORT_5_6_5_REV
:
396 case GL_UNSIGNED_SHORT_5_5_5_1
:
397 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
400 case GL_UNSIGNED_INT_10_10_10_2
:
401 case GL_UNSIGNED_INT_2_10_10_10_REV
:
404 case GL_UNSIGNED_SHORT
:
405 case GL_UNSIGNED_INT
:
410 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
415 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
417 case GL_HALF_FLOAT_ARB
:
419 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
420 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
425 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
426 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
428 case GL_UNSIGNED_INT_5_9_9_9_REV
:
429 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
432 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
433 assert(ctx
->Extensions
.EXT_packed_float
);
434 return GL_R11F_G11F_B10F
;
442 * Create a temporary texture to hold an image of the given size.
443 * If width, height are not POT and the driver only handles POT textures,
444 * allocate the next larger size of texture that is POT.
446 static struct pipe_resource
*
447 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
448 enum pipe_format texFormat
)
450 struct pipe_resource
*pt
;
452 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
453 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
460 * Make texture containing an image for glDrawPixels image.
461 * If 'pixels' is NULL, leave the texture image data undefined.
463 static struct pipe_resource
*
464 make_texture(struct st_context
*st
,
465 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
466 const struct gl_pixelstore_attrib
*unpack
,
467 const GLvoid
*pixels
)
469 struct gl_context
*ctx
= st
->ctx
;
470 struct pipe_context
*pipe
= st
->pipe
;
472 struct pipe_resource
*pt
;
473 enum pipe_format pipeFormat
;
474 GLenum baseInternalFormat
, intFormat
;
476 intFormat
= internal_format(ctx
, format
, type
);
477 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
479 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
480 format
, type
, GL_FALSE
);
483 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
486 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
490 /* alloc temporary texture */
491 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
493 _mesa_unmap_pbo_source(ctx
, unpack
);
498 struct pipe_transfer
*transfer
;
501 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
503 /* we'll do pixel transfer in a fragment shader */
504 ctx
->_ImageTransferState
= 0x0;
506 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
507 PIPE_TRANSFER_WRITE
, 0, 0,
510 /* map texture transfer */
511 dest
= pipe_transfer_map(pipe
, transfer
);
514 /* Put image into texture transfer.
515 * Note that the image is actually going to be upside down in
516 * the texture. We deal with that with texcoords.
518 success
= _mesa_texstore(ctx
, 2, /* dims */
519 baseInternalFormat
, /* baseInternalFormat */
520 mformat
, /* gl_format */
521 transfer
->stride
, /* dstRowStride, bytes */
522 &dest
, /* destSlices */
523 width
, height
, 1, /* size */
524 format
, type
, /* src format/type */
525 pixels
, /* data source */
529 pipe_transfer_unmap(pipe
, transfer
);
530 pipe
->transfer_destroy(pipe
, transfer
);
535 ctx
->_ImageTransferState
= imageTransferStateSave
;
538 _mesa_unmap_pbo_source(ctx
, unpack
);
545 * Draw quad with texcoords and optional color.
546 * Coords are gallium window coords with y=0=top.
547 * \param color may be null
548 * \param invertTex if true, flip texcoords vertically
551 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
552 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
553 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
555 struct st_context
*st
= st_context(ctx
);
556 struct pipe_context
*pipe
= st
->pipe
;
557 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
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*/
622 struct pipe_resource
*buf
;
624 /* allocate/load buffer object with vertex data */
625 buf
= pipe_buffer_create(pipe
->screen
,
626 PIPE_BIND_VERTEX_BUFFER
,
629 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
631 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
634 3); /* attribs/vert */
635 pipe_resource_reference(&buf
, NULL
);
642 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
643 GLsizei width
, GLsizei height
,
644 GLfloat zoomX
, GLfloat zoomY
,
645 struct pipe_sampler_view
**sv
,
646 int num_sampler_view
,
649 const GLfloat
*color
,
651 GLboolean write_depth
, GLboolean write_stencil
)
653 struct st_context
*st
= st_context(ctx
);
654 struct pipe_context
*pipe
= st
->pipe
;
655 struct cso_context
*cso
= st
->cso_context
;
656 GLfloat x0
, y0
, x1
, y1
;
658 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
661 /* XXX if DrawPixels image is larger than max texture size, break
664 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
665 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
666 assert(width
<= maxSize
);
667 assert(height
<= maxSize
);
669 cso_save_rasterizer(cso
);
670 cso_save_viewport(cso
);
671 cso_save_samplers(cso
);
672 cso_save_fragment_sampler_views(cso
);
673 cso_save_fragment_shader(cso
);
674 cso_save_stream_outputs(cso
);
675 cso_save_vertex_shader(cso
);
676 cso_save_geometry_shader(cso
);
677 cso_save_vertex_elements(cso
);
678 cso_save_vertex_buffers(cso
);
680 cso_save_depth_stencil_alpha(cso
);
684 /* rasterizer state: just scissor */
686 struct pipe_rasterizer_state rasterizer
;
687 memset(&rasterizer
, 0, sizeof(rasterizer
));
688 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
689 ctx
->Color
._ClampFragmentColor
;
690 rasterizer
.gl_rasterization_rules
= 1;
691 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
692 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
693 cso_set_rasterizer(cso
, &rasterizer
);
697 /* Stencil writing bypasses the normal fragment pipeline to
698 * disable color writing and set stencil test to always pass.
700 struct pipe_depth_stencil_alpha_state dsa
;
701 struct pipe_blend_state blend
;
704 memset(&dsa
, 0, sizeof(dsa
));
705 dsa
.stencil
[0].enabled
= 1;
706 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
707 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
708 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
710 /* writing depth+stencil: depth test always passes */
711 dsa
.depth
.enabled
= 1;
712 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
713 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
715 cso_set_depth_stencil_alpha(cso
, &dsa
);
717 /* blend (colormask) */
718 memset(&blend
, 0, sizeof(blend
));
719 cso_set_blend(cso
, &blend
);
722 /* fragment shader state: TEX lookup program */
723 cso_set_fragment_shader_handle(cso
, driver_fp
);
725 /* vertex shader state: position + texcoord pass-through */
726 cso_set_vertex_shader_handle(cso
, driver_vp
);
728 /* geometry shader state: disabled */
729 cso_set_geometry_shader_handle(cso
, NULL
);
731 /* texture sampling state: */
733 struct pipe_sampler_state sampler
;
734 memset(&sampler
, 0, sizeof(sampler
));
735 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
736 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
737 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
738 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
739 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
740 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
741 sampler
.normalized_coords
= normalized
;
743 cso_single_sampler(cso
, 0, &sampler
);
744 if (num_sampler_view
> 1) {
745 cso_single_sampler(cso
, 1, &sampler
);
747 cso_single_sampler_done(cso
);
750 /* viewport state: viewport matching window dims */
752 const float w
= (float) ctx
->DrawBuffer
->Width
;
753 const float h
= (float) ctx
->DrawBuffer
->Height
;
754 struct pipe_viewport_state vp
;
755 vp
.scale
[0] = 0.5f
* w
;
756 vp
.scale
[1] = -0.5f
* h
;
759 vp
.translate
[0] = 0.5f
* w
;
760 vp
.translate
[1] = 0.5f
* h
;
761 vp
.translate
[2] = 0.5f
;
762 vp
.translate
[3] = 0.0f
;
763 cso_set_viewport(cso
, &vp
);
766 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
767 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
770 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
772 /* Compute Gallium window coords (y=0=top) with pixel zoom.
773 * Recall that these coords are transformed by the current
774 * vertex shader and viewport transformation.
776 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
777 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
778 invertTex
= !invertTex
;
782 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
784 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
786 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
789 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
790 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
791 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
794 cso_restore_rasterizer(cso
);
795 cso_restore_viewport(cso
);
796 cso_restore_samplers(cso
);
797 cso_restore_fragment_sampler_views(cso
);
798 cso_restore_fragment_shader(cso
);
799 cso_restore_vertex_shader(cso
);
800 cso_restore_geometry_shader(cso
);
801 cso_restore_vertex_elements(cso
);
802 cso_restore_vertex_buffers(cso
);
803 cso_restore_stream_outputs(cso
);
805 cso_restore_depth_stencil_alpha(cso
);
806 cso_restore_blend(cso
);
812 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
813 * can't use a fragment shader to write stencil values.
816 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
817 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
818 const struct gl_pixelstore_attrib
*unpack
,
819 const GLvoid
*pixels
)
821 struct st_context
*st
= st_context(ctx
);
822 struct pipe_context
*pipe
= st
->pipe
;
823 struct st_renderbuffer
*strb
;
824 enum pipe_transfer_usage usage
;
825 struct pipe_transfer
*pt
;
826 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
828 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
833 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
835 /* totally clipped */
840 strb
= st_renderbuffer(ctx
->DrawBuffer
->
841 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
843 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
844 y
= ctx
->DrawBuffer
->Height
- y
- height
;
847 if (format
== GL_STENCIL_INDEX
&&
848 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
849 /* writing stencil to a combined depth+stencil buffer */
850 usage
= PIPE_TRANSFER_READ_WRITE
;
853 usage
= PIPE_TRANSFER_WRITE
;
856 pt
= pipe_get_transfer(pipe
, strb
->texture
,
857 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
861 stmap
= pipe_transfer_map(pipe
, pt
);
863 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
866 sValues
= (GLubyte
*) malloc(width
* sizeof(GLubyte
));
867 zValues
= (GLuint
*) malloc(width
* sizeof(GLuint
));
869 if (sValues
&& zValues
) {
871 for (row
= 0; row
< height
; row
++) {
872 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
873 GLenum destType
= GL_UNSIGNED_BYTE
;
874 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
878 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
879 type
, source
, &clippedUnpack
,
880 ctx
->_ImageTransferState
);
882 if (format
== GL_DEPTH_STENCIL
) {
884 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
885 GL_FLOAT
: GL_UNSIGNED_INT
;
887 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
888 (1 << 24) - 1, type
, source
,
893 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
894 "zoom not complete");
900 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
901 spanY
= height
- row
- 1;
907 /* now pack the stencil (and Z) values in the dest format */
908 switch (pt
->resource
->format
) {
909 case PIPE_FORMAT_S8_UINT
:
911 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
912 assert(usage
== PIPE_TRANSFER_WRITE
);
913 memcpy(dest
, sValues
, width
);
916 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
917 if (format
== GL_DEPTH_STENCIL
) {
918 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
920 assert(usage
== PIPE_TRANSFER_WRITE
);
921 for (k
= 0; k
< width
; k
++) {
922 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
926 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
928 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
929 for (k
= 0; k
< width
; k
++) {
930 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
934 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
935 if (format
== GL_DEPTH_STENCIL
) {
936 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
938 assert(usage
== PIPE_TRANSFER_WRITE
);
939 for (k
= 0; k
< width
; k
++) {
940 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
944 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
946 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
947 for (k
= 0; k
< width
; k
++) {
948 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
952 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
953 if (format
== GL_DEPTH_STENCIL
) {
954 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
955 GLfloat
*destf
= (GLfloat
*)dest
;
957 assert(usage
== PIPE_TRANSFER_WRITE
);
958 for (k
= 0; k
< width
; k
++) {
959 destf
[k
*2] = zValuesFloat
[k
];
960 dest
[k
*2+1] = sValues
[k
] & 0xff;
964 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
966 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
967 for (k
= 0; k
< width
; k
++) {
968 dest
[k
*2+1] = sValues
[k
] & 0xff;
979 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
985 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
987 /* unmap the stencil buffer */
988 pipe_transfer_unmap(pipe
, pt
);
989 pipe
->transfer_destroy(pipe
, pt
);
994 * Get fragment program variant for a glDrawPixels or glCopyPixels
995 * command for RGBA data.
997 static struct st_fp_variant
*
998 get_color_fp_variant(struct st_context
*st
)
1000 struct gl_context
*ctx
= st
->ctx
;
1001 struct st_fp_variant_key key
;
1002 struct st_fp_variant
*fpv
;
1004 memset(&key
, 0, sizeof(key
));
1008 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1009 ctx
->Pixel
.RedScale
!= 1.0 ||
1010 ctx
->Pixel
.GreenBias
!= 0.0 ||
1011 ctx
->Pixel
.GreenScale
!= 1.0 ||
1012 ctx
->Pixel
.BlueBias
!= 0.0 ||
1013 ctx
->Pixel
.BlueScale
!= 1.0 ||
1014 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1015 ctx
->Pixel
.AlphaScale
!= 1.0);
1016 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1017 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1018 st
->ctx
->Color
._ClampFragmentColor
;
1020 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1027 * Get fragment program variant for a glDrawPixels or glCopyPixels
1028 * command for depth/stencil data.
1030 static struct st_fp_variant
*
1031 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1032 GLboolean write_stencil
)
1034 struct st_fp_variant_key key
;
1035 struct st_fp_variant
*fpv
;
1037 memset(&key
, 0, sizeof(key
));
1041 key
.drawpixels_z
= write_depth
;
1042 key
.drawpixels_stencil
= write_stencil
;
1044 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1051 * Called via ctx->Driver.DrawPixels()
1054 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1055 GLsizei width
, GLsizei height
,
1056 GLenum format
, GLenum type
,
1057 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1059 void *driver_vp
, *driver_fp
;
1060 struct st_context
*st
= st_context(ctx
);
1061 const GLfloat
*color
;
1062 struct pipe_context
*pipe
= st
->pipe
;
1063 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1064 struct pipe_sampler_view
*sv
[2];
1065 int num_sampler_view
= 1;
1066 struct st_fp_variant
*fpv
;
1068 if (format
== GL_DEPTH_STENCIL
)
1069 write_stencil
= write_depth
= GL_TRUE
;
1070 else if (format
== GL_STENCIL_INDEX
)
1071 write_stencil
= GL_TRUE
;
1072 else if (format
== GL_DEPTH_COMPONENT
)
1073 write_depth
= GL_TRUE
;
1075 if (write_stencil
&&
1076 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1077 /* software fallback */
1078 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1083 /* Mesa state should be up to date by now */
1084 assert(ctx
->NewState
== 0x0);
1086 st_validate_state(st
);
1089 * Get vertex/fragment shaders
1091 if (write_depth
|| write_stencil
) {
1092 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1094 driver_fp
= fpv
->driver_shader
;
1096 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1098 color
= ctx
->Current
.RasterColor
;
1101 fpv
= get_color_fp_variant(st
);
1103 driver_fp
= fpv
->driver_shader
;
1105 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1108 if (st
->pixel_xfer
.pixelmap_enabled
) {
1109 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1114 /* update fragment program constants */
1115 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1117 /* draw with textured quad */
1119 struct pipe_resource
*pt
1120 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1122 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1125 /* Create a second sampler view to read stencil.
1126 * The stencil is written using the shader stencil export
1128 if (write_stencil
) {
1129 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1131 switch (pt
->format
) {
1132 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1133 case PIPE_FORMAT_X24S8_UINT
:
1134 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1136 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1137 case PIPE_FORMAT_S8X24_UINT
:
1138 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1140 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1141 case PIPE_FORMAT_X32_S8X24_UINT
:
1142 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1144 case PIPE_FORMAT_S8_UINT
:
1145 stencil_format
= PIPE_FORMAT_S8_UINT
;
1151 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1156 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1158 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1163 color
, GL_FALSE
, write_depth
, write_stencil
);
1164 pipe_sampler_view_reference(&sv
[0], NULL
);
1165 if (num_sampler_view
> 1)
1166 pipe_sampler_view_reference(&sv
[1], NULL
);
1168 pipe_resource_reference(&pt
, NULL
);
1176 * Software fallback for glCopyPixels(GL_STENCIL).
1179 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1180 GLsizei width
, GLsizei height
,
1181 GLint dstx
, GLint dsty
)
1183 struct st_renderbuffer
*rbDraw
;
1184 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1185 enum pipe_transfer_usage usage
;
1186 struct pipe_transfer
*ptDraw
;
1191 buffer
= malloc(width
* height
* sizeof(ubyte
));
1193 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1197 /* Get the dest renderbuffer */
1198 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1199 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1201 /* this will do stencil pixel transfer ops */
1202 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1203 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1204 &ctx
->DefaultPacking
, buffer
);
1207 /* debug code: dump stencil values */
1209 for (row
= 0; row
< height
; row
++) {
1210 printf("%3d: ", row
);
1211 for (col
= 0; col
< width
; col
++) {
1212 printf("%02x ", buffer
[col
+ row
* width
]);
1218 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1219 usage
= PIPE_TRANSFER_READ_WRITE
;
1221 usage
= PIPE_TRANSFER_WRITE
;
1223 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1224 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1227 ptDraw
= pipe_get_transfer(pipe
,
1230 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1234 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1235 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1237 /* map the stencil buffer */
1238 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1241 /* XXX PixelZoom not handled yet */
1242 for (i
= 0; i
< height
; i
++) {
1249 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1253 dst
= drawMap
+ y
* ptDraw
->stride
;
1254 src
= buffer
+ i
* width
;
1256 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1261 /* unmap the stencil buffer */
1262 pipe_transfer_unmap(pipe
, ptDraw
);
1263 pipe
->transfer_destroy(pipe
, ptDraw
);
1268 * Return renderbuffer to use for reading color pixels for glCopyPixels
1270 static struct st_renderbuffer
*
1271 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1273 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1274 struct st_renderbuffer
*strb
=
1275 st_renderbuffer(fb
->_ColorReadBuffer
);
1281 /** Do the src/dest regions overlap? */
1283 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1284 GLsizei width
, GLsizei height
)
1286 if (srcX
+ width
<= dstX
||
1287 dstX
+ width
<= srcX
||
1288 srcY
+ height
<= dstY
||
1289 dstY
+ height
<= srcY
)
1297 * Try to do a glCopyPixels for simple cases with a blit by calling
1298 * pipe->resource_copy_region().
1300 * We can do this when we're copying color pixels (depth/stencil
1301 * eventually) with no pixel zoom, no pixel transfer ops, no
1302 * per-fragment ops, the src/dest regions don't overlap and the
1303 * src/dest pixel formats are the same.
1306 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1307 GLsizei width
, GLsizei height
,
1308 GLint dstx
, GLint dsty
, GLenum type
)
1310 struct st_context
*st
= st_context(ctx
);
1311 struct pipe_context
*pipe
= st
->pipe
;
1312 struct gl_pixelstore_attrib pack
, unpack
;
1313 GLint readX
, readY
, readW
, readH
;
1315 if (type
== GL_COLOR
&&
1316 ctx
->Pixel
.ZoomX
== 1.0 &&
1317 ctx
->Pixel
.ZoomY
== 1.0 &&
1318 ctx
->_ImageTransferState
== 0x0 &&
1319 !ctx
->Color
.BlendEnabled
&&
1320 !ctx
->Color
.AlphaEnabled
&&
1322 !ctx
->Fog
.Enabled
&&
1323 !ctx
->Stencil
.Enabled
&&
1324 !ctx
->FragmentProgram
.Enabled
&&
1325 !ctx
->VertexProgram
.Enabled
&&
1326 !ctx
->Shader
.CurrentFragmentProgram
&&
1327 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1328 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1329 !ctx
->Query
.CondRenderQuery
) {
1330 struct st_renderbuffer
*rbRead
, *rbDraw
;
1334 * Clip the read region against the src buffer bounds.
1335 * We'll still allocate a temporary buffer/texture for the original
1336 * src region size but we'll only read the region which is on-screen.
1337 * This may mean that we draw garbage pixels into the dest region, but
1344 pack
= ctx
->DefaultPacking
;
1345 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1346 return GL_TRUE
; /* all done */
1348 /* clip against dest buffer bounds and scissor box */
1349 drawX
= dstx
+ pack
.SkipPixels
;
1350 drawY
= dsty
+ pack
.SkipRows
;
1352 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1353 return GL_TRUE
; /* all done */
1355 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1356 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1358 rbRead
= st_get_color_read_renderbuffer(ctx
);
1359 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1361 if ((rbRead
!= rbDraw
||
1362 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1363 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1364 struct pipe_box srcBox
;
1366 /* flip src/dst position if needed */
1367 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1368 /* both buffers will have the same orientation */
1369 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1370 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1373 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1375 pipe
->resource_copy_region(pipe
,
1377 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1379 rbRead
->rtt_level
, &srcBox
);
1389 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1390 GLsizei width
, GLsizei height
,
1391 GLint dstx
, GLint dsty
, GLenum type
)
1393 struct st_context
*st
= st_context(ctx
);
1394 struct pipe_context
*pipe
= st
->pipe
;
1395 struct pipe_screen
*screen
= pipe
->screen
;
1396 struct st_renderbuffer
*rbRead
;
1397 void *driver_vp
, *driver_fp
;
1398 struct pipe_resource
*pt
;
1399 struct pipe_sampler_view
*sv
[2];
1400 int num_sampler_view
= 1;
1402 enum pipe_format srcFormat
, texFormat
;
1403 GLboolean invertTex
= GL_FALSE
;
1404 GLint readX
, readY
, readW
, readH
;
1405 GLuint sample_count
;
1406 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1407 struct st_fp_variant
*fpv
;
1409 st_validate_state(st
);
1411 if (type
== GL_DEPTH_STENCIL
) {
1412 /* XXX make this more efficient */
1413 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1414 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1418 if (type
== GL_STENCIL
) {
1419 /* can't use texturing to do stencil */
1420 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1424 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1428 * The subsequent code implements glCopyPixels by copying the source
1429 * pixels into a temporary texture that's then applied to a textured quad.
1430 * When we draw the textured quad, all the usual per-fragment operations
1436 * Get vertex/fragment shaders
1438 if (type
== GL_COLOR
) {
1439 rbRead
= st_get_color_read_renderbuffer(ctx
);
1442 fpv
= get_color_fp_variant(st
);
1443 driver_fp
= fpv
->driver_shader
;
1445 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1447 if (st
->pixel_xfer
.pixelmap_enabled
) {
1448 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1453 assert(type
== GL_DEPTH
);
1454 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1455 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1456 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1458 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1459 driver_fp
= fpv
->driver_shader
;
1461 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1464 /* update fragment program constants */
1465 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1467 sample_count
= rbRead
->texture
->nr_samples
;
1468 /* I believe this would be legal, presumably would need to do a resolve
1469 for color, and for depth/stencil spec says to just use one of the
1470 depth/stencil samples per pixel? Need some transfer clarifications. */
1471 assert(sample_count
< 2);
1473 srcFormat
= rbRead
->texture
->format
;
1475 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1477 PIPE_BIND_SAMPLER_VIEW
)) {
1478 texFormat
= srcFormat
;
1481 /* srcFormat can't be used as a texture format */
1482 if (type
== GL_DEPTH
) {
1483 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1484 GL_NONE
, GL_NONE
, st
->internal_target
,
1485 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1486 assert(texFormat
!= PIPE_FORMAT_NONE
);
1489 /* default color format */
1490 texFormat
= st_choose_format(screen
, GL_RGBA
,
1491 GL_NONE
, GL_NONE
, st
->internal_target
,
1492 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1493 assert(texFormat
!= PIPE_FORMAT_NONE
);
1497 /* Invert src region if needed */
1498 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1499 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1500 invertTex
= !invertTex
;
1503 /* Clip the read region against the src buffer bounds.
1504 * We'll still allocate a temporary buffer/texture for the original
1505 * src region size but we'll only read the region which is on-screen.
1506 * This may mean that we draw garbage pixels into the dest region, but
1513 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1514 readW
= MAX2(0, readW
);
1515 readH
= MAX2(0, readH
);
1517 /* alloc temporary texture */
1518 pt
= alloc_texture(st
, width
, height
, texFormat
);
1522 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1524 pipe_resource_reference(&pt
, NULL
);
1528 /* Make temporary texture which is a copy of the src region.
1530 if (srcFormat
== texFormat
) {
1531 struct pipe_box src_box
;
1532 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1533 /* copy source framebuffer surface into mipmap/texture */
1534 pipe
->resource_copy_region(pipe
,
1537 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1538 rbRead
->texture
, /* src tex */
1539 rbRead
->rtt_level
, /* src lvl */
1544 /* CPU-based fallback/conversion */
1545 struct pipe_transfer
*ptRead
=
1546 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1548 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1550 readX
, readY
, readW
, readH
);
1551 struct pipe_transfer
*ptTex
;
1552 enum pipe_transfer_usage transfer_usage
;
1554 if (ST_DEBUG
& DEBUG_FALLBACK
)
1555 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1557 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1558 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1560 transfer_usage
= PIPE_TRANSFER_WRITE
;
1562 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1563 0, 0, width
, height
);
1565 /* copy image from ptRead surface to ptTex surface */
1566 if (type
== GL_COLOR
) {
1567 /* alternate path using get/put_tile() */
1568 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1569 enum pipe_format readFormat
, drawFormat
;
1570 readFormat
= util_format_linear(rbRead
->texture
->format
);
1571 drawFormat
= util_format_linear(pt
->format
);
1572 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1574 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1575 readW
, readH
, drawFormat
, buf
);
1580 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1581 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1582 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1587 pipe
->transfer_destroy(pipe
, ptRead
);
1588 pipe
->transfer_destroy(pipe
, ptTex
);
1591 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1592 * textured quad with that texture.
1594 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1595 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1600 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1602 pipe_resource_reference(&pt
, NULL
);
1603 pipe_sampler_view_reference(&sv
[0], NULL
);
1608 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1610 functions
->DrawPixels
= st_DrawPixels
;
1611 functions
->CopyPixels
= st_CopyPixels
;
1616 st_destroy_drawpix(struct st_context
*st
)
1620 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1621 if (st
->drawpix
.shaders
[i
])
1622 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1625 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1626 if (st
->drawpix
.vert_shaders
[0])
1627 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1628 if (st
->drawpix
.vert_shaders
[1])
1629 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1632 #endif /* FEATURE_drawpix */