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 0, 0, 0, /* dstX/Y/Zoffset */
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_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
= ctx
->Color
._ClampFragmentColor
;
688 rasterizer
.gl_rasterization_rules
= 1;
689 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
690 cso_set_rasterizer(cso
, &rasterizer
);
694 /* Stencil writing bypasses the normal fragment pipeline to
695 * disable color writing and set stencil test to always pass.
697 struct pipe_depth_stencil_alpha_state dsa
;
698 struct pipe_blend_state blend
;
701 memset(&dsa
, 0, sizeof(dsa
));
702 dsa
.stencil
[0].enabled
= 1;
703 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
704 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
705 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
707 /* writing depth+stencil: depth test always passes */
708 dsa
.depth
.enabled
= 1;
709 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
710 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
712 cso_set_depth_stencil_alpha(cso
, &dsa
);
714 /* blend (colormask) */
715 memset(&blend
, 0, sizeof(blend
));
716 cso_set_blend(cso
, &blend
);
719 /* fragment shader state: TEX lookup program */
720 cso_set_fragment_shader_handle(cso
, driver_fp
);
722 /* vertex shader state: position + texcoord pass-through */
723 cso_set_vertex_shader_handle(cso
, driver_vp
);
725 /* geometry shader state: disabled */
726 cso_set_geometry_shader_handle(cso
, NULL
);
728 /* texture sampling state: */
730 struct pipe_sampler_state sampler
;
731 memset(&sampler
, 0, sizeof(sampler
));
732 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
733 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
734 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
735 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
736 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
737 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
738 sampler
.normalized_coords
= normalized
;
740 cso_single_sampler(cso
, 0, &sampler
);
741 if (num_sampler_view
> 1) {
742 cso_single_sampler(cso
, 1, &sampler
);
744 cso_single_sampler_done(cso
);
747 /* viewport state: viewport matching window dims */
749 const float w
= (float) ctx
->DrawBuffer
->Width
;
750 const float h
= (float) ctx
->DrawBuffer
->Height
;
751 struct pipe_viewport_state vp
;
752 vp
.scale
[0] = 0.5f
* w
;
753 vp
.scale
[1] = -0.5f
* h
;
756 vp
.translate
[0] = 0.5f
* w
;
757 vp
.translate
[1] = 0.5f
* h
;
758 vp
.translate
[2] = 0.5f
;
759 vp
.translate
[3] = 0.0f
;
760 cso_set_viewport(cso
, &vp
);
763 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
766 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
768 /* Compute Gallium window coords (y=0=top) with pixel zoom.
769 * Recall that these coords are transformed by the current
770 * vertex shader and viewport transformation.
772 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
773 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
774 invertTex
= !invertTex
;
778 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
780 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
782 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
785 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
786 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
787 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
790 cso_restore_rasterizer(cso
);
791 cso_restore_viewport(cso
);
792 cso_restore_samplers(cso
);
793 cso_restore_fragment_sampler_views(cso
);
794 cso_restore_fragment_shader(cso
);
795 cso_restore_vertex_shader(cso
);
796 cso_restore_geometry_shader(cso
);
797 cso_restore_vertex_elements(cso
);
798 cso_restore_vertex_buffers(cso
);
800 cso_restore_depth_stencil_alpha(cso
);
801 cso_restore_blend(cso
);
807 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
808 * can't use a fragment shader to write stencil values.
811 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
812 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
813 const struct gl_pixelstore_attrib
*unpack
,
814 const GLvoid
*pixels
)
816 struct st_context
*st
= st_context(ctx
);
817 struct pipe_context
*pipe
= st
->pipe
;
818 struct st_renderbuffer
*strb
;
819 enum pipe_transfer_usage usage
;
820 struct pipe_transfer
*pt
;
821 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
824 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
827 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
829 /* totally clipped */
834 strb
= st_renderbuffer(ctx
->DrawBuffer
->
835 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
837 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
838 y
= ctx
->DrawBuffer
->Height
- y
- height
;
841 if(format
!= GL_DEPTH_STENCIL
&&
842 util_format_get_component_bits(strb
->format
,
843 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
844 usage
= PIPE_TRANSFER_READ_WRITE
;
846 usage
= PIPE_TRANSFER_WRITE
;
848 pt
= pipe_get_transfer(pipe
, strb
->texture
,
849 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
853 stmap
= pipe_transfer_map(pipe
, pt
);
855 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
858 /* if width > MAX_WIDTH, have to process image in chunks */
860 while (skipPixels
< width
) {
861 const GLint spanX
= skipPixels
;
862 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
864 for (row
= 0; row
< height
; row
++) {
865 GLubyte sValues
[MAX_WIDTH
];
866 GLuint zValues
[MAX_WIDTH
];
867 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
868 GLenum destType
= GL_UNSIGNED_BYTE
;
869 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
873 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
874 type
, source
, &clippedUnpack
,
875 ctx
->_ImageTransferState
);
877 if (format
== GL_DEPTH_STENCIL
) {
879 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
880 GL_FLOAT
: GL_UNSIGNED_INT
;
882 _mesa_unpack_depth_span(ctx
, spanWidth
, ztype
, zValues
,
883 (1 << 24) - 1, type
, source
,
888 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
889 "zoom not complete");
895 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
896 spanY
= height
- row
- 1;
902 /* now pack the stencil (and Z) values in the dest format */
903 switch (pt
->resource
->format
) {
904 case PIPE_FORMAT_S8_UINT
:
906 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
907 assert(usage
== PIPE_TRANSFER_WRITE
);
908 memcpy(dest
, sValues
, spanWidth
);
911 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
912 if (format
== GL_DEPTH_STENCIL
) {
913 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
915 assert(usage
== PIPE_TRANSFER_WRITE
);
916 for (k
= 0; k
< spanWidth
; k
++) {
917 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
921 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
923 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
924 for (k
= 0; k
< spanWidth
; k
++) {
925 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
929 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
930 if (format
== GL_DEPTH_STENCIL
) {
931 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
933 assert(usage
== PIPE_TRANSFER_WRITE
);
934 for (k
= 0; k
< spanWidth
; k
++) {
935 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
939 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
941 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
942 for (k
= 0; k
< spanWidth
; k
++) {
943 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
947 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
948 if (format
== GL_DEPTH_STENCIL
) {
949 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
950 GLfloat
*destf
= (GLfloat
*)dest
;
952 assert(usage
== PIPE_TRANSFER_WRITE
);
953 for (k
= 0; k
< spanWidth
; k
++) {
954 destf
[k
*2] = zValuesFloat
[k
];
955 dest
[k
*2+1] = sValues
[k
] & 0xff;
959 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
961 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
962 for (k
= 0; k
< spanWidth
; k
++) {
963 dest
[k
*2+1] = sValues
[k
] & 0xff;
972 skipPixels
+= spanWidth
;
975 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
977 /* unmap the stencil buffer */
978 pipe_transfer_unmap(pipe
, pt
);
979 pipe
->transfer_destroy(pipe
, pt
);
984 * Get fragment program variant for a glDrawPixels or glCopyPixels
985 * command for RGBA data.
987 static struct st_fp_variant
*
988 get_color_fp_variant(struct st_context
*st
)
990 struct gl_context
*ctx
= st
->ctx
;
991 struct st_fp_variant_key key
;
992 struct st_fp_variant
*fpv
;
994 memset(&key
, 0, sizeof(key
));
998 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
999 ctx
->Pixel
.RedScale
!= 1.0 ||
1000 ctx
->Pixel
.GreenBias
!= 0.0 ||
1001 ctx
->Pixel
.GreenScale
!= 1.0 ||
1002 ctx
->Pixel
.BlueBias
!= 0.0 ||
1003 ctx
->Pixel
.BlueScale
!= 1.0 ||
1004 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1005 ctx
->Pixel
.AlphaScale
!= 1.0);
1006 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1008 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1015 * Get fragment program variant for a glDrawPixels or glCopyPixels
1016 * command for depth/stencil data.
1018 static struct st_fp_variant
*
1019 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1020 GLboolean write_stencil
)
1022 struct st_fp_variant_key key
;
1023 struct st_fp_variant
*fpv
;
1025 memset(&key
, 0, sizeof(key
));
1029 key
.drawpixels_z
= write_depth
;
1030 key
.drawpixels_stencil
= write_stencil
;
1032 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1039 * Called via ctx->Driver.DrawPixels()
1042 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1043 GLsizei width
, GLsizei height
,
1044 GLenum format
, GLenum type
,
1045 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1047 void *driver_vp
, *driver_fp
;
1048 struct st_context
*st
= st_context(ctx
);
1049 const GLfloat
*color
;
1050 struct pipe_context
*pipe
= st
->pipe
;
1051 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1052 struct pipe_sampler_view
*sv
[2];
1053 int num_sampler_view
= 1;
1054 struct st_fp_variant
*fpv
;
1056 if (format
== GL_DEPTH_STENCIL
)
1057 write_stencil
= write_depth
= GL_TRUE
;
1058 else if (format
== GL_STENCIL_INDEX
)
1059 write_stencil
= GL_TRUE
;
1060 else if (format
== GL_DEPTH_COMPONENT
)
1061 write_depth
= GL_TRUE
;
1063 if (write_stencil
&&
1064 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1065 /* software fallback */
1066 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1071 /* Mesa state should be up to date by now */
1072 assert(ctx
->NewState
== 0x0);
1074 st_validate_state(st
);
1077 * Get vertex/fragment shaders
1079 if (write_depth
|| write_stencil
) {
1080 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1082 driver_fp
= fpv
->driver_shader
;
1084 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1086 color
= ctx
->Current
.RasterColor
;
1089 fpv
= get_color_fp_variant(st
);
1091 driver_fp
= fpv
->driver_shader
;
1093 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1096 if (st
->pixel_xfer
.pixelmap_enabled
) {
1097 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1102 /* update fragment program constants */
1103 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1105 /* draw with textured quad */
1107 struct pipe_resource
*pt
1108 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1110 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1113 /* Create a second sampler view to read stencil.
1114 * The stencil is written using the shader stencil export
1116 if (write_stencil
) {
1117 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1119 switch (pt
->format
) {
1120 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1121 case PIPE_FORMAT_X24S8_UINT
:
1122 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1124 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1125 case PIPE_FORMAT_S8X24_UINT
:
1126 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1128 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1129 case PIPE_FORMAT_X32_S8X24_UINT
:
1130 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1132 case PIPE_FORMAT_S8_UINT
:
1133 stencil_format
= PIPE_FORMAT_S8_UINT
;
1139 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1144 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1146 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1151 color
, GL_FALSE
, write_depth
, write_stencil
);
1152 pipe_sampler_view_reference(&sv
[0], NULL
);
1153 if (num_sampler_view
> 1)
1154 pipe_sampler_view_reference(&sv
[1], NULL
);
1156 pipe_resource_reference(&pt
, NULL
);
1164 * Software fallback for glCopyPixels(GL_STENCIL).
1167 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1168 GLsizei width
, GLsizei height
,
1169 GLint dstx
, GLint dsty
)
1171 struct st_renderbuffer
*rbDraw
;
1172 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1173 enum pipe_transfer_usage usage
;
1174 struct pipe_transfer
*ptDraw
;
1179 buffer
= malloc(width
* height
* sizeof(ubyte
));
1181 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1185 /* Get the dest renderbuffer. If there's a wrapper, use the
1186 * underlying renderbuffer.
1188 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1189 if (rbDraw
->Base
.Wrapped
)
1190 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1192 /* this will do stencil pixel transfer ops */
1193 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1194 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1195 &ctx
->DefaultPacking
, buffer
);
1198 /* debug code: dump stencil values */
1200 for (row
= 0; row
< height
; row
++) {
1201 printf("%3d: ", row
);
1202 for (col
= 0; col
< width
; col
++) {
1203 printf("%02x ", buffer
[col
+ row
* width
]);
1209 if (util_format_get_component_bits(rbDraw
->format
,
1210 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1211 usage
= PIPE_TRANSFER_READ_WRITE
;
1213 usage
= PIPE_TRANSFER_WRITE
;
1215 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1216 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1219 ptDraw
= pipe_get_transfer(pipe
,
1222 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1226 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1227 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1229 /* map the stencil buffer */
1230 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1233 /* XXX PixelZoom not handled yet */
1234 for (i
= 0; i
< height
; i
++) {
1241 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1245 dst
= drawMap
+ y
* ptDraw
->stride
;
1246 src
= buffer
+ i
* width
;
1248 switch (ptDraw
->resource
->format
) {
1249 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1251 uint
*dst4
= (uint
*) dst
;
1253 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1254 for (j
= 0; j
< width
; j
++) {
1255 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1260 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1262 uint
*dst4
= (uint
*) dst
;
1264 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1265 for (j
= 0; j
< width
; j
++) {
1266 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1271 case PIPE_FORMAT_S8_UINT
:
1272 assert(usage
== PIPE_TRANSFER_WRITE
);
1273 memcpy(dst
, src
, width
);
1275 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1277 uint
*dst4
= (uint
*) dst
;
1280 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1281 for (j
= 0; j
< width
; j
++) {
1282 *dst4
= src
[j
] & 0xff;
1294 /* unmap the stencil buffer */
1295 pipe_transfer_unmap(pipe
, ptDraw
);
1296 pipe
->transfer_destroy(pipe
, ptDraw
);
1301 * Return renderbuffer to use for reading color pixels for glCopyPixels
1303 static struct st_renderbuffer
*
1304 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1306 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1307 struct st_renderbuffer
*strb
=
1308 st_renderbuffer(fb
->_ColorReadBuffer
);
1314 /** Do the src/dest regions overlap? */
1316 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1317 GLsizei width
, GLsizei height
)
1319 if (srcX
+ width
<= dstX
||
1320 dstX
+ width
<= srcX
||
1321 srcY
+ height
<= dstY
||
1322 dstY
+ height
<= srcY
)
1330 * Try to do a glCopyPixels for simple cases with a blit by calling
1331 * pipe->resource_copy_region().
1333 * We can do this when we're copying color pixels (depth/stencil
1334 * eventually) with no pixel zoom, no pixel transfer ops, no
1335 * per-fragment ops, the src/dest regions don't overlap and the
1336 * src/dest pixel formats are the same.
1339 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1340 GLsizei width
, GLsizei height
,
1341 GLint dstx
, GLint dsty
, GLenum type
)
1343 struct st_context
*st
= st_context(ctx
);
1344 struct pipe_context
*pipe
= st
->pipe
;
1345 struct gl_pixelstore_attrib pack
, unpack
;
1346 GLint readX
, readY
, readW
, readH
;
1348 if (type
== GL_COLOR
&&
1349 ctx
->Pixel
.ZoomX
== 1.0 &&
1350 ctx
->Pixel
.ZoomY
== 1.0 &&
1351 ctx
->_ImageTransferState
== 0x0 &&
1352 !ctx
->Color
.BlendEnabled
&&
1353 !ctx
->Color
.AlphaEnabled
&&
1355 !ctx
->Fog
.Enabled
&&
1356 !ctx
->Stencil
.Enabled
&&
1357 !ctx
->FragmentProgram
.Enabled
&&
1358 !ctx
->VertexProgram
.Enabled
&&
1359 !ctx
->Shader
.CurrentFragmentProgram
&&
1360 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1361 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1362 !ctx
->Query
.CondRenderQuery
) {
1363 struct st_renderbuffer
*rbRead
, *rbDraw
;
1367 * Clip the read region against the src buffer bounds.
1368 * We'll still allocate a temporary buffer/texture for the original
1369 * src region size but we'll only read the region which is on-screen.
1370 * This may mean that we draw garbage pixels into the dest region, but
1377 pack
= ctx
->DefaultPacking
;
1378 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1379 return GL_TRUE
; /* all done */
1381 /* clip against dest buffer bounds and scissor box */
1382 drawX
= dstx
+ pack
.SkipPixels
;
1383 drawY
= dsty
+ pack
.SkipRows
;
1385 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1386 return GL_TRUE
; /* all done */
1388 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1389 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1391 rbRead
= st_get_color_read_renderbuffer(ctx
);
1392 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1394 if ((rbRead
!= rbDraw
||
1395 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1396 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1397 struct pipe_box srcBox
;
1399 /* flip src/dst position if needed */
1400 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1401 /* both buffers will have the same orientation */
1402 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1403 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1406 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1408 pipe
->resource_copy_region(pipe
,
1410 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1412 rbRead
->rtt_level
, &srcBox
);
1422 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1423 GLsizei width
, GLsizei height
,
1424 GLint dstx
, GLint dsty
, GLenum type
)
1426 struct st_context
*st
= st_context(ctx
);
1427 struct pipe_context
*pipe
= st
->pipe
;
1428 struct pipe_screen
*screen
= pipe
->screen
;
1429 struct st_renderbuffer
*rbRead
;
1430 void *driver_vp
, *driver_fp
;
1431 struct pipe_resource
*pt
;
1432 struct pipe_sampler_view
*sv
[2];
1433 int num_sampler_view
= 1;
1435 enum pipe_format srcFormat
, texFormat
;
1436 GLboolean invertTex
= GL_FALSE
;
1437 GLint readX
, readY
, readW
, readH
;
1438 GLuint sample_count
;
1439 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1440 struct st_fp_variant
*fpv
;
1442 st_validate_state(st
);
1444 if (type
== GL_STENCIL
) {
1445 /* can't use texturing to do stencil */
1446 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1450 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1454 * The subsequent code implements glCopyPixels by copying the source
1455 * pixels into a temporary texture that's then applied to a textured quad.
1456 * When we draw the textured quad, all the usual per-fragment operations
1462 * Get vertex/fragment shaders
1464 if (type
== GL_COLOR
) {
1465 rbRead
= st_get_color_read_renderbuffer(ctx
);
1468 fpv
= get_color_fp_variant(st
);
1469 driver_fp
= fpv
->driver_shader
;
1471 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1473 if (st
->pixel_xfer
.pixelmap_enabled
) {
1474 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1479 assert(type
== GL_DEPTH
);
1480 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1481 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1483 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1484 driver_fp
= fpv
->driver_shader
;
1486 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1489 /* update fragment program constants */
1490 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1493 if (rbRead
->Base
.Wrapped
)
1494 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1496 sample_count
= rbRead
->texture
->nr_samples
;
1497 /* I believe this would be legal, presumably would need to do a resolve
1498 for color, and for depth/stencil spec says to just use one of the
1499 depth/stencil samples per pixel? Need some transfer clarifications. */
1500 assert(sample_count
< 2);
1502 srcFormat
= rbRead
->texture
->format
;
1504 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1506 PIPE_BIND_SAMPLER_VIEW
)) {
1507 texFormat
= srcFormat
;
1510 /* srcFormat can't be used as a texture format */
1511 if (type
== GL_DEPTH
) {
1512 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1513 GL_NONE
, GL_NONE
, st
->internal_target
,
1514 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1515 assert(texFormat
!= PIPE_FORMAT_NONE
);
1518 /* default color format */
1519 texFormat
= st_choose_format(screen
, GL_RGBA
,
1520 GL_NONE
, GL_NONE
, st
->internal_target
,
1521 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1522 assert(texFormat
!= PIPE_FORMAT_NONE
);
1526 /* Invert src region if needed */
1527 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1528 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1529 invertTex
= !invertTex
;
1532 /* Clip the read region against the src buffer bounds.
1533 * We'll still allocate a temporary buffer/texture for the original
1534 * src region size but we'll only read the region which is on-screen.
1535 * This may mean that we draw garbage pixels into the dest region, but
1542 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1543 readW
= MAX2(0, readW
);
1544 readH
= MAX2(0, readH
);
1546 /* alloc temporary texture */
1547 pt
= alloc_texture(st
, width
, height
, texFormat
);
1551 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1553 pipe_resource_reference(&pt
, NULL
);
1557 /* Make temporary texture which is a copy of the src region.
1559 if (srcFormat
== texFormat
) {
1560 struct pipe_box src_box
;
1561 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1562 /* copy source framebuffer surface into mipmap/texture */
1563 pipe
->resource_copy_region(pipe
,
1566 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1567 rbRead
->texture
, /* src tex */
1568 rbRead
->rtt_level
, /* src lvl */
1573 /* CPU-based fallback/conversion */
1574 struct pipe_transfer
*ptRead
=
1575 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1577 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1579 readX
, readY
, readW
, readH
);
1580 struct pipe_transfer
*ptTex
;
1581 enum pipe_transfer_usage transfer_usage
;
1583 if (ST_DEBUG
& DEBUG_FALLBACK
)
1584 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1586 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1587 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1589 transfer_usage
= PIPE_TRANSFER_WRITE
;
1591 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1592 0, 0, width
, height
);
1594 /* copy image from ptRead surface to ptTex surface */
1595 if (type
== GL_COLOR
) {
1596 /* alternate path using get/put_tile() */
1597 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1598 enum pipe_format readFormat
, drawFormat
;
1599 readFormat
= util_format_linear(rbRead
->texture
->format
);
1600 drawFormat
= util_format_linear(pt
->format
);
1601 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1603 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1604 readW
, readH
, drawFormat
, buf
);
1609 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1610 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1611 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1616 pipe
->transfer_destroy(pipe
, ptRead
);
1617 pipe
->transfer_destroy(pipe
, ptTex
);
1620 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1621 * textured quad with that texture.
1623 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1624 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1629 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1631 pipe_resource_reference(&pt
, NULL
);
1632 pipe_sampler_view_reference(&sv
[0], NULL
);
1637 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1639 functions
->DrawPixels
= st_DrawPixels
;
1640 functions
->CopyPixels
= st_CopyPixels
;
1645 st_destroy_drawpix(struct st_context
*st
)
1649 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1650 if (st
->drawpix
.shaders
[i
])
1651 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1654 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1655 if (st
->drawpix
.vert_shaders
[0])
1656 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1657 if (st
->drawpix
.vert_shaders
[1])
1658 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1661 #endif /* FEATURE_drawpix */