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/macros.h"
37 #include "main/mfeatures.h"
38 #include "main/mtypes.h"
39 #include "main/pack.h"
41 #include "main/readpix.h"
42 #include "main/texformat.h"
43 #include "main/teximage.h"
44 #include "main/texstore.h"
45 #include "program/program.h"
46 #include "program/prog_print.h"
47 #include "program/prog_instruction.h"
50 #include "st_atom_constbuf.h"
51 #include "st_cb_drawpixels.h"
52 #include "st_cb_readpixels.h"
53 #include "st_cb_fbo.h"
54 #include "st_context.h"
56 #include "st_format.h"
57 #include "st_program.h"
58 #include "st_texture.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "tgsi/tgsi_ureg.h"
63 #include "util/u_draw_quad.h"
64 #include "util/u_format.h"
65 #include "util/u_inlines.h"
66 #include "util/u_math.h"
67 #include "util/u_tile.h"
68 #include "cso_cache/cso_context.h"
74 * Check if the given program is:
75 * 0: MOVE result.color, fragment.color;
79 is_passthrough_program(const struct gl_fragment_program
*prog
)
81 if (prog
->Base
.NumInstructions
== 2) {
82 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
83 if (inst
[0].Opcode
== OPCODE_MOV
&&
84 inst
[1].Opcode
== OPCODE_END
&&
85 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
86 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
87 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
88 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
89 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
90 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
99 * Returns a fragment program which implements the current pixel transfer ops.
101 static struct gl_fragment_program
*
102 get_glsl_pixel_transfer_program(struct st_context
*st
,
103 struct st_fragment_program
*orig
)
105 int pixelMaps
= 0, scaleAndBias
= 0;
106 struct gl_context
*ctx
= st
->ctx
;
107 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
108 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
113 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
114 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
115 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
116 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
120 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
123 /* create the colormap/texture now if not already done */
124 if (!st
->pixel_xfer
.pixelmap_texture
) {
125 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
126 st
->pixel_xfer
.pixelmap_sampler_view
=
127 st_create_texture_sampler_view(st
->pipe
,
128 st
->pixel_xfer
.pixelmap_texture
);
132 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
133 scaleAndBias
, pixelMaps
);
140 * Make fragment shader for glDraw/CopyPixels. This shader is made
141 * by combining the pixel transfer shader with the user-defined shader.
142 * \param fpIn the current/incoming fragment program
143 * \param fpOut returns the combined fragment program
146 st_make_drawpix_fragment_program(struct st_context
*st
,
147 struct gl_fragment_program
*fpIn
,
148 struct gl_fragment_program
**fpOut
)
150 struct gl_program
*newProg
;
151 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
153 if (is_passthrough_program(fpIn
)) {
154 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
155 &st
->pixel_xfer
.program
->Base
);
157 else if (stfp
->glsl_to_tgsi
!= NULL
) {
158 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
163 printf("Base program:\n");
164 _mesa_print_program(&fpIn
->Base
);
165 printf("DrawPix program:\n");
166 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
168 newProg
= _mesa_combine_programs(st
->ctx
,
169 &st
->pixel_xfer
.program
->Base
.Base
,
175 printf("Combined DrawPixels program:\n");
176 _mesa_print_program(newProg
);
177 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
178 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
179 _mesa_print_parameter_list(newProg
->Parameters
);
182 *fpOut
= (struct gl_fragment_program
*) newProg
;
187 * Create fragment program that does a TEX() instruction to get a Z and/or
188 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
189 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
190 * Pass fragment color through as-is.
191 * \return pointer to the gl_fragment program
193 struct gl_fragment_program
*
194 st_make_drawpix_z_stencil_program(struct st_context
*st
,
195 GLboolean write_depth
,
196 GLboolean write_stencil
)
198 struct gl_context
*ctx
= st
->ctx
;
199 struct gl_program
*p
;
200 struct gl_fragment_program
*fp
;
202 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
204 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
206 if (st
->drawpix
.shaders
[shaderIndex
]) {
207 /* already have the proper shader */
208 return st
->drawpix
.shaders
[shaderIndex
];
214 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
218 p
->NumInstructions
= write_depth
? 2 : 1;
219 p
->NumInstructions
+= write_stencil
? 1 : 0;
221 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
222 if (!p
->Instructions
) {
223 ctx
->Driver
.DeleteProgram(ctx
, p
);
226 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
229 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
230 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
231 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
232 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
233 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
234 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
235 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
236 p
->Instructions
[ic
].TexSrcUnit
= 0;
237 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
242 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
243 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
244 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
245 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
246 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
247 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
248 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
249 p
->Instructions
[ic
].TexSrcUnit
= 1;
250 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
255 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
257 assert(ic
== p
->NumInstructions
);
259 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
260 p
->OutputsWritten
= 0;
262 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
264 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
266 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
268 p
->SamplersUsed
|= 1 << 1;
270 fp
= (struct gl_fragment_program
*) p
;
272 /* save the new shader */
273 st
->drawpix
.shaders
[shaderIndex
] = fp
;
280 * Create a simple vertex shader that just passes through the
281 * vertex position and texcoord (and optionally, color).
284 make_passthrough_vertex_shader(struct st_context
*st
,
287 if (!st
->drawpix
.vert_shaders
[passColor
]) {
288 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
293 /* MOV result.pos, vertex.pos; */
295 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
296 ureg_DECL_vs_input( ureg
, 0 ));
298 /* MOV result.texcoord0, vertex.attr[1]; */
300 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
301 ureg_DECL_vs_input( ureg
, 1 ));
304 /* MOV result.color0, vertex.attr[2]; */
306 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
307 ureg_DECL_vs_input( ureg
, 2 ));
312 st
->drawpix
.vert_shaders
[passColor
] =
313 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
316 return st
->drawpix
.vert_shaders
[passColor
];
321 * Return a texture internalFormat for drawing/copying an image
322 * of the given format and type.
325 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
328 case GL_DEPTH_COMPONENT
:
330 case GL_UNSIGNED_SHORT
:
331 return GL_DEPTH_COMPONENT16
;
333 case GL_UNSIGNED_INT
:
334 return GL_DEPTH_COMPONENT32
;
337 if (ctx
->Extensions
.ARB_depth_buffer_float
)
338 return GL_DEPTH_COMPONENT32F
;
340 return GL_DEPTH_COMPONENT
;
343 return GL_DEPTH_COMPONENT
;
346 case GL_DEPTH_STENCIL
:
348 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
349 return GL_DEPTH32F_STENCIL8
;
351 case GL_UNSIGNED_INT_24_8
:
353 return GL_DEPTH24_STENCIL8
;
356 case GL_STENCIL_INDEX
:
357 return GL_STENCIL_INDEX
;
360 if (_mesa_is_integer_format(format
)) {
364 case GL_UNSIGNED_BYTE
:
368 case GL_UNSIGNED_SHORT
:
372 case GL_UNSIGNED_INT
:
375 assert(0 && "Unexpected type in internal_format()");
376 return GL_RGBA_INTEGER
;
381 case GL_UNSIGNED_BYTE
:
382 case GL_UNSIGNED_INT_8_8_8_8
:
383 case GL_UNSIGNED_INT_8_8_8_8_REV
:
387 case GL_UNSIGNED_BYTE_3_3_2
:
388 case GL_UNSIGNED_BYTE_2_3_3_REV
:
389 case GL_UNSIGNED_SHORT_4_4_4_4
:
390 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
393 case GL_UNSIGNED_SHORT_5_6_5
:
394 case GL_UNSIGNED_SHORT_5_6_5_REV
:
395 case GL_UNSIGNED_SHORT_5_5_5_1
:
396 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
399 case GL_UNSIGNED_INT_10_10_10_2
:
400 case GL_UNSIGNED_INT_2_10_10_10_REV
:
403 case GL_UNSIGNED_SHORT
:
404 case GL_UNSIGNED_INT
:
409 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
414 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
416 case GL_HALF_FLOAT_ARB
:
418 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
419 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
424 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
425 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
427 case GL_UNSIGNED_INT_5_9_9_9_REV
:
428 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
431 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
432 assert(ctx
->Extensions
.EXT_packed_float
);
433 return GL_R11F_G11F_B10F
;
441 * Create a temporary texture to hold an image of the given size.
442 * If width, height are not POT and the driver only handles POT textures,
443 * allocate the next larger size of texture that is POT.
445 static struct pipe_resource
*
446 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
447 enum pipe_format texFormat
)
449 struct pipe_resource
*pt
;
451 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
452 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
459 * Make texture containing an image for glDrawPixels image.
460 * If 'pixels' is NULL, leave the texture image data undefined.
462 static struct pipe_resource
*
463 make_texture(struct st_context
*st
,
464 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
465 const struct gl_pixelstore_attrib
*unpack
,
466 const GLvoid
*pixels
)
468 struct gl_context
*ctx
= st
->ctx
;
469 struct pipe_context
*pipe
= st
->pipe
;
471 struct pipe_resource
*pt
;
472 enum pipe_format pipeFormat
;
473 GLenum baseInternalFormat
, intFormat
;
475 intFormat
= internal_format(ctx
, format
, type
);
476 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
478 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
479 format
, type
, GL_FALSE
);
482 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
485 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
489 /* alloc temporary texture */
490 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
492 _mesa_unmap_pbo_source(ctx
, unpack
);
497 struct pipe_transfer
*transfer
;
500 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
502 /* we'll do pixel transfer in a fragment shader */
503 ctx
->_ImageTransferState
= 0x0;
505 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
506 PIPE_TRANSFER_WRITE
, 0, 0,
509 /* map texture transfer */
510 dest
= pipe_transfer_map(pipe
, transfer
);
513 /* Put image into texture transfer.
514 * Note that the image is actually going to be upside down in
515 * the texture. We deal with that with texcoords.
517 success
= _mesa_texstore(ctx
, 2, /* dims */
518 baseInternalFormat
, /* baseInternalFormat */
519 mformat
, /* gl_format */
520 transfer
->stride
, /* dstRowStride, bytes */
521 &dest
, /* destSlices */
522 width
, height
, 1, /* size */
523 format
, type
, /* src format/type */
524 pixels
, /* data source */
528 pipe_transfer_unmap(pipe
, transfer
);
529 pipe
->transfer_destroy(pipe
, transfer
);
534 ctx
->_ImageTransferState
= imageTransferStateSave
;
537 _mesa_unmap_pbo_source(ctx
, unpack
);
544 * Draw quad with texcoords and optional color.
545 * Coords are gallium window coords with y=0=top.
546 * \param color may be null
547 * \param invertTex if true, flip texcoords vertically
550 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
551 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
552 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
554 struct st_context
*st
= st_context(ctx
);
555 struct pipe_context
*pipe
= st
->pipe
;
556 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
558 /* setup vertex data */
560 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
561 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
562 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
563 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
564 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
565 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
566 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
567 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
568 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
569 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
573 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
574 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
577 verts
[1][0][0] = clip_x1
;
578 verts
[1][0][1] = clip_y0
;
581 verts
[2][0][0] = clip_x1
;
582 verts
[2][0][1] = clip_y1
;
585 verts
[3][0][0] = clip_x0
;
586 verts
[3][0][1] = clip_y1
;
588 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
589 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
590 verts
[1][1][0] = sRight
;
591 verts
[1][1][1] = tTop
;
592 verts
[2][1][0] = sRight
;
593 verts
[2][1][1] = tBot
;
594 verts
[3][1][0] = sLeft
;
595 verts
[3][1][1] = tBot
;
597 /* same for all verts: */
599 for (i
= 0; i
< 4; i
++) {
600 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
601 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
602 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
603 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
604 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
605 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
606 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
607 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
611 for (i
= 0; i
< 4; i
++) {
612 verts
[i
][0][2] = z
; /*Z*/
613 verts
[i
][0][3] = 1.0f
; /*W*/
614 verts
[i
][1][2] = 0.0f
; /*R*/
615 verts
[i
][1][3] = 1.0f
; /*Q*/
621 struct pipe_resource
*buf
;
623 /* allocate/load buffer object with vertex data */
624 buf
= pipe_buffer_create(pipe
->screen
,
625 PIPE_BIND_VERTEX_BUFFER
,
628 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
630 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
633 3); /* attribs/vert */
634 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;
828 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
831 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
833 /* totally clipped */
838 strb
= st_renderbuffer(ctx
->DrawBuffer
->
839 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
841 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
842 y
= ctx
->DrawBuffer
->Height
- y
- height
;
845 if(format
!= GL_DEPTH_STENCIL
&&
846 util_format_get_component_bits(strb
->format
,
847 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
848 usage
= PIPE_TRANSFER_READ_WRITE
;
850 usage
= PIPE_TRANSFER_WRITE
;
852 pt
= pipe_get_transfer(pipe
, strb
->texture
,
853 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
857 stmap
= pipe_transfer_map(pipe
, pt
);
859 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
862 /* if width > MAX_WIDTH, have to process image in chunks */
864 while (skipPixels
< width
) {
865 const GLint spanX
= skipPixels
;
866 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
868 for (row
= 0; row
< height
; row
++) {
869 GLubyte sValues
[MAX_WIDTH
];
870 GLuint zValues
[MAX_WIDTH
];
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
, spanWidth
, 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
, spanWidth
, 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
+ spanX
;
911 assert(usage
== PIPE_TRANSFER_WRITE
);
912 memcpy(dest
, sValues
, spanWidth
);
915 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
916 if (format
== GL_DEPTH_STENCIL
) {
917 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
919 assert(usage
== PIPE_TRANSFER_WRITE
);
920 for (k
= 0; k
< spanWidth
; k
++) {
921 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
925 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
927 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
928 for (k
= 0; k
< spanWidth
; 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
+ spanX
*4);
937 assert(usage
== PIPE_TRANSFER_WRITE
);
938 for (k
= 0; k
< spanWidth
; k
++) {
939 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
943 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
945 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
946 for (k
= 0; k
< spanWidth
; 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
+ spanX
*4);
954 GLfloat
*destf
= (GLfloat
*)dest
;
956 assert(usage
== PIPE_TRANSFER_WRITE
);
957 for (k
= 0; k
< spanWidth
; k
++) {
958 destf
[k
*2] = zValuesFloat
[k
];
959 dest
[k
*2+1] = sValues
[k
] & 0xff;
963 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
965 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
966 for (k
= 0; k
< spanWidth
; k
++) {
967 dest
[k
*2+1] = sValues
[k
] & 0xff;
976 skipPixels
+= spanWidth
;
979 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
981 /* unmap the stencil buffer */
982 pipe_transfer_unmap(pipe
, pt
);
983 pipe
->transfer_destroy(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 * Get fragment program variant for a glDrawPixels or glCopyPixels
1022 * command for depth/stencil data.
1024 static struct st_fp_variant
*
1025 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1026 GLboolean write_stencil
)
1028 struct st_fp_variant_key key
;
1029 struct st_fp_variant
*fpv
;
1031 memset(&key
, 0, sizeof(key
));
1035 key
.drawpixels_z
= write_depth
;
1036 key
.drawpixels_stencil
= write_stencil
;
1038 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1045 * Called via ctx->Driver.DrawPixels()
1048 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1049 GLsizei width
, GLsizei height
,
1050 GLenum format
, GLenum type
,
1051 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1053 void *driver_vp
, *driver_fp
;
1054 struct st_context
*st
= st_context(ctx
);
1055 const GLfloat
*color
;
1056 struct pipe_context
*pipe
= st
->pipe
;
1057 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1058 struct pipe_sampler_view
*sv
[2];
1059 int num_sampler_view
= 1;
1060 struct st_fp_variant
*fpv
;
1062 if (format
== GL_DEPTH_STENCIL
)
1063 write_stencil
= write_depth
= GL_TRUE
;
1064 else if (format
== GL_STENCIL_INDEX
)
1065 write_stencil
= GL_TRUE
;
1066 else if (format
== GL_DEPTH_COMPONENT
)
1067 write_depth
= GL_TRUE
;
1069 if (write_stencil
&&
1070 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1071 /* software fallback */
1072 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1077 /* Mesa state should be up to date by now */
1078 assert(ctx
->NewState
== 0x0);
1080 st_validate_state(st
);
1083 * Get vertex/fragment shaders
1085 if (write_depth
|| write_stencil
) {
1086 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1088 driver_fp
= fpv
->driver_shader
;
1090 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1092 color
= ctx
->Current
.RasterColor
;
1095 fpv
= get_color_fp_variant(st
);
1097 driver_fp
= fpv
->driver_shader
;
1099 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1102 if (st
->pixel_xfer
.pixelmap_enabled
) {
1103 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1108 /* update fragment program constants */
1109 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1111 /* draw with textured quad */
1113 struct pipe_resource
*pt
1114 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1116 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1119 /* Create a second sampler view to read stencil.
1120 * The stencil is written using the shader stencil export
1122 if (write_stencil
) {
1123 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1125 switch (pt
->format
) {
1126 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1127 case PIPE_FORMAT_X24S8_UINT
:
1128 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1130 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1131 case PIPE_FORMAT_S8X24_UINT
:
1132 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1134 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1135 case PIPE_FORMAT_X32_S8X24_UINT
:
1136 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1138 case PIPE_FORMAT_S8_UINT
:
1139 stencil_format
= PIPE_FORMAT_S8_UINT
;
1145 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1150 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1152 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1157 color
, GL_FALSE
, write_depth
, write_stencil
);
1158 pipe_sampler_view_reference(&sv
[0], NULL
);
1159 if (num_sampler_view
> 1)
1160 pipe_sampler_view_reference(&sv
[1], NULL
);
1162 pipe_resource_reference(&pt
, NULL
);
1170 * Software fallback for glCopyPixels(GL_STENCIL).
1173 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1174 GLsizei width
, GLsizei height
,
1175 GLint dstx
, GLint dsty
)
1177 struct st_renderbuffer
*rbDraw
;
1178 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1179 enum pipe_transfer_usage usage
;
1180 struct pipe_transfer
*ptDraw
;
1185 buffer
= malloc(width
* height
* sizeof(ubyte
));
1187 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1191 /* Get the dest renderbuffer */
1192 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1193 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1195 /* this will do stencil pixel transfer ops */
1196 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1197 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1198 &ctx
->DefaultPacking
, buffer
);
1201 /* debug code: dump stencil values */
1203 for (row
= 0; row
< height
; row
++) {
1204 printf("%3d: ", row
);
1205 for (col
= 0; col
< width
; col
++) {
1206 printf("%02x ", buffer
[col
+ row
* width
]);
1212 if (util_format_get_component_bits(rbDraw
->format
,
1213 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1214 usage
= PIPE_TRANSFER_READ_WRITE
;
1216 usage
= PIPE_TRANSFER_WRITE
;
1218 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1219 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1222 ptDraw
= pipe_get_transfer(pipe
,
1225 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1229 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1230 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1232 /* map the stencil buffer */
1233 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1236 /* XXX PixelZoom not handled yet */
1237 for (i
= 0; i
< height
; i
++) {
1244 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1248 dst
= drawMap
+ y
* ptDraw
->stride
;
1249 src
= buffer
+ i
* width
;
1251 switch (ptDraw
->resource
->format
) {
1252 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1254 uint
*dst4
= (uint
*) dst
;
1256 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1257 for (j
= 0; j
< width
; j
++) {
1258 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1263 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1265 uint
*dst4
= (uint
*) dst
;
1267 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1268 for (j
= 0; j
< width
; j
++) {
1269 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1274 case PIPE_FORMAT_S8_UINT
:
1275 assert(usage
== PIPE_TRANSFER_WRITE
);
1276 memcpy(dst
, src
, width
);
1278 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1280 uint
*dst4
= (uint
*) dst
;
1283 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1284 for (j
= 0; j
< width
; j
++) {
1285 *dst4
= src
[j
] & 0xff;
1297 /* unmap the stencil buffer */
1298 pipe_transfer_unmap(pipe
, ptDraw
);
1299 pipe
->transfer_destroy(pipe
, ptDraw
);
1304 * Return renderbuffer to use for reading color pixels for glCopyPixels
1306 static struct st_renderbuffer
*
1307 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1309 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1310 struct st_renderbuffer
*strb
=
1311 st_renderbuffer(fb
->_ColorReadBuffer
);
1317 /** Do the src/dest regions overlap? */
1319 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1320 GLsizei width
, GLsizei height
)
1322 if (srcX
+ width
<= dstX
||
1323 dstX
+ width
<= srcX
||
1324 srcY
+ height
<= dstY
||
1325 dstY
+ height
<= srcY
)
1333 * Try to do a glCopyPixels for simple cases with a blit by calling
1334 * pipe->resource_copy_region().
1336 * We can do this when we're copying color pixels (depth/stencil
1337 * eventually) with no pixel zoom, no pixel transfer ops, no
1338 * per-fragment ops, the src/dest regions don't overlap and the
1339 * src/dest pixel formats are the same.
1342 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1343 GLsizei width
, GLsizei height
,
1344 GLint dstx
, GLint dsty
, GLenum type
)
1346 struct st_context
*st
= st_context(ctx
);
1347 struct pipe_context
*pipe
= st
->pipe
;
1348 struct gl_pixelstore_attrib pack
, unpack
;
1349 GLint readX
, readY
, readW
, readH
;
1351 if (type
== GL_COLOR
&&
1352 ctx
->Pixel
.ZoomX
== 1.0 &&
1353 ctx
->Pixel
.ZoomY
== 1.0 &&
1354 ctx
->_ImageTransferState
== 0x0 &&
1355 !ctx
->Color
.BlendEnabled
&&
1356 !ctx
->Color
.AlphaEnabled
&&
1358 !ctx
->Fog
.Enabled
&&
1359 !ctx
->Stencil
.Enabled
&&
1360 !ctx
->FragmentProgram
.Enabled
&&
1361 !ctx
->VertexProgram
.Enabled
&&
1362 !ctx
->Shader
.CurrentFragmentProgram
&&
1363 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1364 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1365 !ctx
->Query
.CondRenderQuery
) {
1366 struct st_renderbuffer
*rbRead
, *rbDraw
;
1370 * Clip the read region against the src buffer bounds.
1371 * We'll still allocate a temporary buffer/texture for the original
1372 * src region size but we'll only read the region which is on-screen.
1373 * This may mean that we draw garbage pixels into the dest region, but
1380 pack
= ctx
->DefaultPacking
;
1381 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1382 return GL_TRUE
; /* all done */
1384 /* clip against dest buffer bounds and scissor box */
1385 drawX
= dstx
+ pack
.SkipPixels
;
1386 drawY
= dsty
+ pack
.SkipRows
;
1388 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1389 return GL_TRUE
; /* all done */
1391 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1392 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1394 rbRead
= st_get_color_read_renderbuffer(ctx
);
1395 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1397 if ((rbRead
!= rbDraw
||
1398 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1399 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1400 struct pipe_box srcBox
;
1402 /* flip src/dst position if needed */
1403 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1404 /* both buffers will have the same orientation */
1405 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1406 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1409 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1411 pipe
->resource_copy_region(pipe
,
1413 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1415 rbRead
->rtt_level
, &srcBox
);
1425 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1426 GLsizei width
, GLsizei height
,
1427 GLint dstx
, GLint dsty
, GLenum type
)
1429 struct st_context
*st
= st_context(ctx
);
1430 struct pipe_context
*pipe
= st
->pipe
;
1431 struct pipe_screen
*screen
= pipe
->screen
;
1432 struct st_renderbuffer
*rbRead
;
1433 void *driver_vp
, *driver_fp
;
1434 struct pipe_resource
*pt
;
1435 struct pipe_sampler_view
*sv
[2];
1436 int num_sampler_view
= 1;
1438 enum pipe_format srcFormat
, texFormat
;
1439 GLboolean invertTex
= GL_FALSE
;
1440 GLint readX
, readY
, readW
, readH
;
1441 GLuint sample_count
;
1442 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1443 struct st_fp_variant
*fpv
;
1445 st_validate_state(st
);
1447 if (type
== GL_DEPTH_STENCIL
) {
1448 /* XXX make this more efficient */
1449 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1450 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1454 if (type
== GL_STENCIL
) {
1455 /* can't use texturing to do stencil */
1456 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1460 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1464 * The subsequent code implements glCopyPixels by copying the source
1465 * pixels into a temporary texture that's then applied to a textured quad.
1466 * When we draw the textured quad, all the usual per-fragment operations
1472 * Get vertex/fragment shaders
1474 if (type
== GL_COLOR
) {
1475 rbRead
= st_get_color_read_renderbuffer(ctx
);
1478 fpv
= get_color_fp_variant(st
);
1479 driver_fp
= fpv
->driver_shader
;
1481 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1483 if (st
->pixel_xfer
.pixelmap_enabled
) {
1484 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1489 assert(type
== GL_DEPTH
);
1490 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1491 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1492 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1494 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1495 driver_fp
= fpv
->driver_shader
;
1497 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1500 /* update fragment program constants */
1501 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1503 sample_count
= rbRead
->texture
->nr_samples
;
1504 /* I believe this would be legal, presumably would need to do a resolve
1505 for color, and for depth/stencil spec says to just use one of the
1506 depth/stencil samples per pixel? Need some transfer clarifications. */
1507 assert(sample_count
< 2);
1509 srcFormat
= rbRead
->texture
->format
;
1511 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1513 PIPE_BIND_SAMPLER_VIEW
)) {
1514 texFormat
= srcFormat
;
1517 /* srcFormat can't be used as a texture format */
1518 if (type
== GL_DEPTH
) {
1519 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1520 GL_NONE
, GL_NONE
, st
->internal_target
,
1521 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1522 assert(texFormat
!= PIPE_FORMAT_NONE
);
1525 /* default color format */
1526 texFormat
= st_choose_format(screen
, GL_RGBA
,
1527 GL_NONE
, GL_NONE
, st
->internal_target
,
1528 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1529 assert(texFormat
!= PIPE_FORMAT_NONE
);
1533 /* Invert src region if needed */
1534 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1535 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1536 invertTex
= !invertTex
;
1539 /* Clip the read region against the src buffer bounds.
1540 * We'll still allocate a temporary buffer/texture for the original
1541 * src region size but we'll only read the region which is on-screen.
1542 * This may mean that we draw garbage pixels into the dest region, but
1549 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1550 readW
= MAX2(0, readW
);
1551 readH
= MAX2(0, readH
);
1553 /* alloc temporary texture */
1554 pt
= alloc_texture(st
, width
, height
, texFormat
);
1558 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1560 pipe_resource_reference(&pt
, NULL
);
1564 /* Make temporary texture which is a copy of the src region.
1566 if (srcFormat
== texFormat
) {
1567 struct pipe_box src_box
;
1568 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1569 /* copy source framebuffer surface into mipmap/texture */
1570 pipe
->resource_copy_region(pipe
,
1573 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1574 rbRead
->texture
, /* src tex */
1575 rbRead
->rtt_level
, /* src lvl */
1580 /* CPU-based fallback/conversion */
1581 struct pipe_transfer
*ptRead
=
1582 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1584 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1586 readX
, readY
, readW
, readH
);
1587 struct pipe_transfer
*ptTex
;
1588 enum pipe_transfer_usage transfer_usage
;
1590 if (ST_DEBUG
& DEBUG_FALLBACK
)
1591 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1593 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1594 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1596 transfer_usage
= PIPE_TRANSFER_WRITE
;
1598 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1599 0, 0, width
, height
);
1601 /* copy image from ptRead surface to ptTex surface */
1602 if (type
== GL_COLOR
) {
1603 /* alternate path using get/put_tile() */
1604 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1605 enum pipe_format readFormat
, drawFormat
;
1606 readFormat
= util_format_linear(rbRead
->texture
->format
);
1607 drawFormat
= util_format_linear(pt
->format
);
1608 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1610 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1611 readW
, readH
, drawFormat
, buf
);
1616 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1617 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1618 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1623 pipe
->transfer_destroy(pipe
, ptRead
);
1624 pipe
->transfer_destroy(pipe
, ptTex
);
1627 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1628 * textured quad with that texture.
1630 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1631 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1636 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1638 pipe_resource_reference(&pt
, NULL
);
1639 pipe_sampler_view_reference(&sv
[0], NULL
);
1644 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1646 functions
->DrawPixels
= st_DrawPixels
;
1647 functions
->CopyPixels
= st_CopyPixels
;
1652 st_destroy_drawpix(struct st_context
*st
)
1656 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1657 if (st
->drawpix
.shaders
[i
])
1658 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1661 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1662 if (st
->drawpix
.vert_shaders
[0])
1663 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1664 if (st
->drawpix
.vert_shaders
[1])
1665 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1668 #endif /* FEATURE_drawpix */