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_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
= ctx
->Color
._ClampFragmentColor
;
689 rasterizer
.gl_rasterization_rules
= 1;
690 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
691 cso_set_rasterizer(cso
, &rasterizer
);
695 /* Stencil writing bypasses the normal fragment pipeline to
696 * disable color writing and set stencil test to always pass.
698 struct pipe_depth_stencil_alpha_state dsa
;
699 struct pipe_blend_state blend
;
702 memset(&dsa
, 0, sizeof(dsa
));
703 dsa
.stencil
[0].enabled
= 1;
704 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
705 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
706 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
708 /* writing depth+stencil: depth test always passes */
709 dsa
.depth
.enabled
= 1;
710 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
711 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
713 cso_set_depth_stencil_alpha(cso
, &dsa
);
715 /* blend (colormask) */
716 memset(&blend
, 0, sizeof(blend
));
717 cso_set_blend(cso
, &blend
);
720 /* fragment shader state: TEX lookup program */
721 cso_set_fragment_shader_handle(cso
, driver_fp
);
723 /* vertex shader state: position + texcoord pass-through */
724 cso_set_vertex_shader_handle(cso
, driver_vp
);
726 /* geometry shader state: disabled */
727 cso_set_geometry_shader_handle(cso
, NULL
);
729 /* texture sampling state: */
731 struct pipe_sampler_state sampler
;
732 memset(&sampler
, 0, sizeof(sampler
));
733 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
734 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
735 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
736 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
737 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
738 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
739 sampler
.normalized_coords
= normalized
;
741 cso_single_sampler(cso
, 0, &sampler
);
742 if (num_sampler_view
> 1) {
743 cso_single_sampler(cso
, 1, &sampler
);
745 cso_single_sampler_done(cso
);
748 /* viewport state: viewport matching window dims */
750 const float w
= (float) ctx
->DrawBuffer
->Width
;
751 const float h
= (float) ctx
->DrawBuffer
->Height
;
752 struct pipe_viewport_state vp
;
753 vp
.scale
[0] = 0.5f
* w
;
754 vp
.scale
[1] = -0.5f
* h
;
757 vp
.translate
[0] = 0.5f
* w
;
758 vp
.translate
[1] = 0.5f
* h
;
759 vp
.translate
[2] = 0.5f
;
760 vp
.translate
[3] = 0.0f
;
761 cso_set_viewport(cso
, &vp
);
764 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
765 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
768 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
770 /* Compute Gallium window coords (y=0=top) with pixel zoom.
771 * Recall that these coords are transformed by the current
772 * vertex shader and viewport transformation.
774 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
775 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
776 invertTex
= !invertTex
;
780 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
782 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
784 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
787 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
788 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
789 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
792 cso_restore_rasterizer(cso
);
793 cso_restore_viewport(cso
);
794 cso_restore_samplers(cso
);
795 cso_restore_fragment_sampler_views(cso
);
796 cso_restore_fragment_shader(cso
);
797 cso_restore_vertex_shader(cso
);
798 cso_restore_geometry_shader(cso
);
799 cso_restore_vertex_elements(cso
);
800 cso_restore_vertex_buffers(cso
);
801 cso_restore_stream_outputs(cso
);
803 cso_restore_depth_stencil_alpha(cso
);
804 cso_restore_blend(cso
);
810 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
811 * can't use a fragment shader to write stencil values.
814 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
815 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
816 const struct gl_pixelstore_attrib
*unpack
,
817 const GLvoid
*pixels
)
819 struct st_context
*st
= st_context(ctx
);
820 struct pipe_context
*pipe
= st
->pipe
;
821 struct st_renderbuffer
*strb
;
822 enum pipe_transfer_usage usage
;
823 struct pipe_transfer
*pt
;
824 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
827 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
830 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
832 /* totally clipped */
837 strb
= st_renderbuffer(ctx
->DrawBuffer
->
838 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
840 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
841 y
= ctx
->DrawBuffer
->Height
- y
- height
;
844 if(format
!= GL_DEPTH_STENCIL
&&
845 util_format_get_component_bits(strb
->format
,
846 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
847 usage
= PIPE_TRANSFER_READ_WRITE
;
849 usage
= PIPE_TRANSFER_WRITE
;
851 pt
= pipe_get_transfer(pipe
, strb
->texture
,
852 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
856 stmap
= pipe_transfer_map(pipe
, pt
);
858 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
861 /* if width > MAX_WIDTH, have to process image in chunks */
863 while (skipPixels
< width
) {
864 const GLint spanX
= skipPixels
;
865 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
867 for (row
= 0; row
< height
; row
++) {
868 GLubyte sValues
[MAX_WIDTH
];
869 GLuint zValues
[MAX_WIDTH
];
870 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
871 GLenum destType
= GL_UNSIGNED_BYTE
;
872 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
876 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
877 type
, source
, &clippedUnpack
,
878 ctx
->_ImageTransferState
);
880 if (format
== GL_DEPTH_STENCIL
) {
882 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
883 GL_FLOAT
: GL_UNSIGNED_INT
;
885 _mesa_unpack_depth_span(ctx
, spanWidth
, ztype
, zValues
,
886 (1 << 24) - 1, type
, source
,
891 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
892 "zoom not complete");
898 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
899 spanY
= height
- row
- 1;
905 /* now pack the stencil (and Z) values in the dest format */
906 switch (pt
->resource
->format
) {
907 case PIPE_FORMAT_S8_UINT
:
909 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
910 assert(usage
== PIPE_TRANSFER_WRITE
);
911 memcpy(dest
, sValues
, spanWidth
);
914 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
915 if (format
== GL_DEPTH_STENCIL
) {
916 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
918 assert(usage
== PIPE_TRANSFER_WRITE
);
919 for (k
= 0; k
< spanWidth
; k
++) {
920 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
924 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
926 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
927 for (k
= 0; k
< spanWidth
; k
++) {
928 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
932 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
933 if (format
== GL_DEPTH_STENCIL
) {
934 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
936 assert(usage
== PIPE_TRANSFER_WRITE
);
937 for (k
= 0; k
< spanWidth
; k
++) {
938 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
942 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
944 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
945 for (k
= 0; k
< spanWidth
; k
++) {
946 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
950 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
951 if (format
== GL_DEPTH_STENCIL
) {
952 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
953 GLfloat
*destf
= (GLfloat
*)dest
;
955 assert(usage
== PIPE_TRANSFER_WRITE
);
956 for (k
= 0; k
< spanWidth
; k
++) {
957 destf
[k
*2] = zValuesFloat
[k
];
958 dest
[k
*2+1] = sValues
[k
] & 0xff;
962 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
964 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
965 for (k
= 0; k
< spanWidth
; k
++) {
966 dest
[k
*2+1] = sValues
[k
] & 0xff;
975 skipPixels
+= spanWidth
;
978 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
980 /* unmap the stencil buffer */
981 pipe_transfer_unmap(pipe
, pt
);
982 pipe
->transfer_destroy(pipe
, pt
);
987 * Get fragment program variant for a glDrawPixels or glCopyPixels
988 * command for RGBA data.
990 static struct st_fp_variant
*
991 get_color_fp_variant(struct st_context
*st
)
993 struct gl_context
*ctx
= st
->ctx
;
994 struct st_fp_variant_key key
;
995 struct st_fp_variant
*fpv
;
997 memset(&key
, 0, sizeof(key
));
1001 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1002 ctx
->Pixel
.RedScale
!= 1.0 ||
1003 ctx
->Pixel
.GreenBias
!= 0.0 ||
1004 ctx
->Pixel
.GreenScale
!= 1.0 ||
1005 ctx
->Pixel
.BlueBias
!= 0.0 ||
1006 ctx
->Pixel
.BlueScale
!= 1.0 ||
1007 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1008 ctx
->Pixel
.AlphaScale
!= 1.0);
1009 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1011 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1018 * Get fragment program variant for a glDrawPixels or glCopyPixels
1019 * command for depth/stencil data.
1021 static struct st_fp_variant
*
1022 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1023 GLboolean write_stencil
)
1025 struct st_fp_variant_key key
;
1026 struct st_fp_variant
*fpv
;
1028 memset(&key
, 0, sizeof(key
));
1032 key
.drawpixels_z
= write_depth
;
1033 key
.drawpixels_stencil
= write_stencil
;
1035 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1042 * Called via ctx->Driver.DrawPixels()
1045 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1046 GLsizei width
, GLsizei height
,
1047 GLenum format
, GLenum type
,
1048 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1050 void *driver_vp
, *driver_fp
;
1051 struct st_context
*st
= st_context(ctx
);
1052 const GLfloat
*color
;
1053 struct pipe_context
*pipe
= st
->pipe
;
1054 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1055 struct pipe_sampler_view
*sv
[2];
1056 int num_sampler_view
= 1;
1057 struct st_fp_variant
*fpv
;
1059 if (format
== GL_DEPTH_STENCIL
)
1060 write_stencil
= write_depth
= GL_TRUE
;
1061 else if (format
== GL_STENCIL_INDEX
)
1062 write_stencil
= GL_TRUE
;
1063 else if (format
== GL_DEPTH_COMPONENT
)
1064 write_depth
= GL_TRUE
;
1066 if (write_stencil
&&
1067 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1068 /* software fallback */
1069 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1074 /* Mesa state should be up to date by now */
1075 assert(ctx
->NewState
== 0x0);
1077 st_validate_state(st
);
1080 * Get vertex/fragment shaders
1082 if (write_depth
|| write_stencil
) {
1083 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1085 driver_fp
= fpv
->driver_shader
;
1087 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1089 color
= ctx
->Current
.RasterColor
;
1092 fpv
= get_color_fp_variant(st
);
1094 driver_fp
= fpv
->driver_shader
;
1096 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1099 if (st
->pixel_xfer
.pixelmap_enabled
) {
1100 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1105 /* update fragment program constants */
1106 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1108 /* draw with textured quad */
1110 struct pipe_resource
*pt
1111 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1113 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1116 /* Create a second sampler view to read stencil.
1117 * The stencil is written using the shader stencil export
1119 if (write_stencil
) {
1120 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1122 switch (pt
->format
) {
1123 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1124 case PIPE_FORMAT_X24S8_UINT
:
1125 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1127 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1128 case PIPE_FORMAT_S8X24_UINT
:
1129 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1131 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1132 case PIPE_FORMAT_X32_S8X24_UINT
:
1133 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1135 case PIPE_FORMAT_S8_UINT
:
1136 stencil_format
= PIPE_FORMAT_S8_UINT
;
1142 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1147 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1149 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1154 color
, GL_FALSE
, write_depth
, write_stencil
);
1155 pipe_sampler_view_reference(&sv
[0], NULL
);
1156 if (num_sampler_view
> 1)
1157 pipe_sampler_view_reference(&sv
[1], NULL
);
1159 pipe_resource_reference(&pt
, NULL
);
1167 * Software fallback for glCopyPixels(GL_STENCIL).
1170 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1171 GLsizei width
, GLsizei height
,
1172 GLint dstx
, GLint dsty
)
1174 struct st_renderbuffer
*rbDraw
;
1175 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1176 enum pipe_transfer_usage usage
;
1177 struct pipe_transfer
*ptDraw
;
1182 buffer
= malloc(width
* height
* sizeof(ubyte
));
1184 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1188 /* Get the dest renderbuffer */
1189 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1190 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
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_DEPTH_STENCIL
) {
1445 /* XXX make this more efficient */
1446 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1447 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1451 if (type
== GL_STENCIL
) {
1452 /* can't use texturing to do stencil */
1453 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1457 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1461 * The subsequent code implements glCopyPixels by copying the source
1462 * pixels into a temporary texture that's then applied to a textured quad.
1463 * When we draw the textured quad, all the usual per-fragment operations
1469 * Get vertex/fragment shaders
1471 if (type
== GL_COLOR
) {
1472 rbRead
= st_get_color_read_renderbuffer(ctx
);
1475 fpv
= get_color_fp_variant(st
);
1476 driver_fp
= fpv
->driver_shader
;
1478 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1480 if (st
->pixel_xfer
.pixelmap_enabled
) {
1481 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1486 assert(type
== GL_DEPTH
);
1487 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1488 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1489 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1491 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1492 driver_fp
= fpv
->driver_shader
;
1494 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1497 /* update fragment program constants */
1498 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1500 sample_count
= rbRead
->texture
->nr_samples
;
1501 /* I believe this would be legal, presumably would need to do a resolve
1502 for color, and for depth/stencil spec says to just use one of the
1503 depth/stencil samples per pixel? Need some transfer clarifications. */
1504 assert(sample_count
< 2);
1506 srcFormat
= rbRead
->texture
->format
;
1508 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1510 PIPE_BIND_SAMPLER_VIEW
)) {
1511 texFormat
= srcFormat
;
1514 /* srcFormat can't be used as a texture format */
1515 if (type
== GL_DEPTH
) {
1516 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1517 GL_NONE
, GL_NONE
, st
->internal_target
,
1518 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1519 assert(texFormat
!= PIPE_FORMAT_NONE
);
1522 /* default color format */
1523 texFormat
= st_choose_format(screen
, GL_RGBA
,
1524 GL_NONE
, GL_NONE
, st
->internal_target
,
1525 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1526 assert(texFormat
!= PIPE_FORMAT_NONE
);
1530 /* Invert src region if needed */
1531 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1532 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1533 invertTex
= !invertTex
;
1536 /* Clip the read region against the src buffer bounds.
1537 * We'll still allocate a temporary buffer/texture for the original
1538 * src region size but we'll only read the region which is on-screen.
1539 * This may mean that we draw garbage pixels into the dest region, but
1546 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1547 readW
= MAX2(0, readW
);
1548 readH
= MAX2(0, readH
);
1550 /* alloc temporary texture */
1551 pt
= alloc_texture(st
, width
, height
, texFormat
);
1555 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1557 pipe_resource_reference(&pt
, NULL
);
1561 /* Make temporary texture which is a copy of the src region.
1563 if (srcFormat
== texFormat
) {
1564 struct pipe_box src_box
;
1565 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1566 /* copy source framebuffer surface into mipmap/texture */
1567 pipe
->resource_copy_region(pipe
,
1570 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1571 rbRead
->texture
, /* src tex */
1572 rbRead
->rtt_level
, /* src lvl */
1577 /* CPU-based fallback/conversion */
1578 struct pipe_transfer
*ptRead
=
1579 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1581 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1583 readX
, readY
, readW
, readH
);
1584 struct pipe_transfer
*ptTex
;
1585 enum pipe_transfer_usage transfer_usage
;
1587 if (ST_DEBUG
& DEBUG_FALLBACK
)
1588 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1590 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1591 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1593 transfer_usage
= PIPE_TRANSFER_WRITE
;
1595 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1596 0, 0, width
, height
);
1598 /* copy image from ptRead surface to ptTex surface */
1599 if (type
== GL_COLOR
) {
1600 /* alternate path using get/put_tile() */
1601 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1602 enum pipe_format readFormat
, drawFormat
;
1603 readFormat
= util_format_linear(rbRead
->texture
->format
);
1604 drawFormat
= util_format_linear(pt
->format
);
1605 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1607 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1608 readW
, readH
, drawFormat
, buf
);
1613 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1614 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1615 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1620 pipe
->transfer_destroy(pipe
, ptRead
);
1621 pipe
->transfer_destroy(pipe
, ptTex
);
1624 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1625 * textured quad with that texture.
1627 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1628 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1633 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1635 pipe_resource_reference(&pt
, NULL
);
1636 pipe_sampler_view_reference(&sv
[0], NULL
);
1641 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1643 functions
->DrawPixels
= st_DrawPixels
;
1644 functions
->CopyPixels
= st_CopyPixels
;
1649 st_destroy_drawpix(struct st_context
*st
)
1653 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1654 if (st
->drawpix
.shaders
[i
])
1655 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1658 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1659 if (st
->drawpix
.vert_shaders
[0])
1660 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1661 if (st
->drawpix
.vert_shaders
[1])
1662 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1665 #endif /* FEATURE_drawpix */