1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/format_pack.h"
37 #include "main/macros.h"
38 #include "main/mfeatures.h"
39 #include "main/mtypes.h"
40 #include "main/pack.h"
42 #include "main/readpix.h"
43 #include "main/texformat.h"
44 #include "main/teximage.h"
45 #include "main/texstore.h"
46 #include "main/glformats.h"
47 #include "program/program.h"
48 #include "program/prog_print.h"
49 #include "program/prog_instruction.h"
52 #include "st_atom_constbuf.h"
53 #include "st_cb_drawpixels.h"
54 #include "st_cb_readpixels.h"
55 #include "st_cb_fbo.h"
56 #include "st_context.h"
58 #include "st_format.h"
59 #include "st_program.h"
60 #include "st_texture.h"
62 #include "pipe/p_context.h"
63 #include "pipe/p_defines.h"
64 #include "tgsi/tgsi_ureg.h"
65 #include "util/u_draw_quad.h"
66 #include "util/u_format.h"
67 #include "util/u_inlines.h"
68 #include "util/u_math.h"
69 #include "util/u_tile.h"
70 #include "util/u_upload_mgr.h"
71 #include "cso_cache/cso_context.h"
75 * Check if the given program is:
76 * 0: MOVE result.color, fragment.color;
80 is_passthrough_program(const struct gl_fragment_program
*prog
)
82 if (prog
->Base
.NumInstructions
== 2) {
83 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
84 if (inst
[0].Opcode
== OPCODE_MOV
&&
85 inst
[1].Opcode
== OPCODE_END
&&
86 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
87 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
88 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
89 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
90 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
91 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
100 * Returns a fragment program which implements the current pixel transfer ops.
102 static struct gl_fragment_program
*
103 get_glsl_pixel_transfer_program(struct st_context
*st
,
104 struct st_fragment_program
*orig
)
106 int pixelMaps
= 0, scaleAndBias
= 0;
107 struct gl_context
*ctx
= st
->ctx
;
108 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
109 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
114 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
115 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
116 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
117 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
121 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
124 /* create the colormap/texture now if not already done */
125 if (!st
->pixel_xfer
.pixelmap_texture
) {
126 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
127 st
->pixel_xfer
.pixelmap_sampler_view
=
128 st_create_texture_sampler_view(st
->pipe
,
129 st
->pixel_xfer
.pixelmap_texture
);
133 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
134 scaleAndBias
, pixelMaps
);
141 * Make fragment shader for glDraw/CopyPixels. This shader is made
142 * by combining the pixel transfer shader with the user-defined shader.
143 * \param fpIn the current/incoming fragment program
144 * \param fpOut returns the combined fragment program
147 st_make_drawpix_fragment_program(struct st_context
*st
,
148 struct gl_fragment_program
*fpIn
,
149 struct gl_fragment_program
**fpOut
)
151 struct gl_program
*newProg
;
152 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
154 if (is_passthrough_program(fpIn
)) {
155 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
156 &st
->pixel_xfer
.program
->Base
);
158 else if (stfp
->glsl_to_tgsi
!= NULL
) {
159 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
164 printf("Base program:\n");
165 _mesa_print_program(&fpIn
->Base
);
166 printf("DrawPix program:\n");
167 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
169 newProg
= _mesa_combine_programs(st
->ctx
,
170 &st
->pixel_xfer
.program
->Base
.Base
,
176 printf("Combined DrawPixels program:\n");
177 _mesa_print_program(newProg
);
178 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
179 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
180 _mesa_print_parameter_list(newProg
->Parameters
);
183 *fpOut
= (struct gl_fragment_program
*) newProg
;
188 * Create fragment program that does a TEX() instruction to get a Z and/or
189 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
190 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
191 * Pass fragment color through as-is.
192 * \return pointer to the gl_fragment program
194 struct gl_fragment_program
*
195 st_make_drawpix_z_stencil_program(struct st_context
*st
,
196 GLboolean write_depth
,
197 GLboolean write_stencil
)
199 struct gl_context
*ctx
= st
->ctx
;
200 struct gl_program
*p
;
201 struct gl_fragment_program
*fp
;
203 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
205 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
207 if (st
->drawpix
.shaders
[shaderIndex
]) {
208 /* already have the proper shader */
209 return st
->drawpix
.shaders
[shaderIndex
];
215 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
219 p
->NumInstructions
= write_depth
? 3 : 1;
220 p
->NumInstructions
+= write_stencil
? 1 : 0;
222 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
223 if (!p
->Instructions
) {
224 ctx
->Driver
.DeleteProgram(ctx
, p
);
227 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
230 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
231 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
232 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
233 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
234 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
235 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
236 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
237 p
->Instructions
[ic
].TexSrcUnit
= 0;
238 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
240 /* MOV result.color, fragment.color; */
241 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
242 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
243 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
244 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
245 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
250 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
251 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
252 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
253 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
254 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
255 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
256 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
257 p
->Instructions
[ic
].TexSrcUnit
= 1;
258 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
263 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
265 assert(ic
== p
->NumInstructions
);
267 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
268 p
->OutputsWritten
= 0;
270 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
271 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_COLOR
);
274 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
276 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
278 p
->SamplersUsed
|= 1 << 1;
280 fp
= (struct gl_fragment_program
*) p
;
282 /* save the new shader */
283 st
->drawpix
.shaders
[shaderIndex
] = fp
;
290 * Create a simple vertex shader that just passes through the
291 * vertex position and texcoord (and optionally, color).
294 make_passthrough_vertex_shader(struct st_context
*st
,
297 if (!st
->drawpix
.vert_shaders
[passColor
]) {
298 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
303 /* MOV result.pos, vertex.pos; */
305 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
306 ureg_DECL_vs_input( ureg
, 0 ));
308 /* MOV result.texcoord0, vertex.attr[1]; */
310 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
311 ureg_DECL_vs_input( ureg
, 1 ));
314 /* MOV result.color0, vertex.attr[2]; */
316 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
317 ureg_DECL_vs_input( ureg
, 2 ));
322 st
->drawpix
.vert_shaders
[passColor
] =
323 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
326 return st
->drawpix
.vert_shaders
[passColor
];
331 * Return a texture internalFormat for drawing/copying an image
332 * of the given format and type.
335 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
338 case GL_DEPTH_COMPONENT
:
340 case GL_UNSIGNED_SHORT
:
341 return GL_DEPTH_COMPONENT16
;
343 case GL_UNSIGNED_INT
:
344 return GL_DEPTH_COMPONENT32
;
347 if (ctx
->Extensions
.ARB_depth_buffer_float
)
348 return GL_DEPTH_COMPONENT32F
;
350 return GL_DEPTH_COMPONENT
;
353 return GL_DEPTH_COMPONENT
;
356 case GL_DEPTH_STENCIL
:
358 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
359 return GL_DEPTH32F_STENCIL8
;
361 case GL_UNSIGNED_INT_24_8
:
363 return GL_DEPTH24_STENCIL8
;
366 case GL_STENCIL_INDEX
:
367 return GL_STENCIL_INDEX
;
370 if (_mesa_is_enum_format_integer(format
)) {
374 case GL_UNSIGNED_BYTE
:
378 case GL_UNSIGNED_SHORT
:
382 case GL_UNSIGNED_INT
:
385 assert(0 && "Unexpected type in internal_format()");
386 return GL_RGBA_INTEGER
;
391 case GL_UNSIGNED_BYTE
:
392 case GL_UNSIGNED_INT_8_8_8_8
:
393 case GL_UNSIGNED_INT_8_8_8_8_REV
:
397 case GL_UNSIGNED_BYTE_3_3_2
:
398 case GL_UNSIGNED_BYTE_2_3_3_REV
:
401 case GL_UNSIGNED_SHORT_4_4_4_4
:
402 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
405 case GL_UNSIGNED_SHORT_5_6_5
:
406 case GL_UNSIGNED_SHORT_5_6_5_REV
:
409 case GL_UNSIGNED_SHORT_5_5_5_1
:
410 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
413 case GL_UNSIGNED_INT_10_10_10_2
:
414 case GL_UNSIGNED_INT_2_10_10_10_REV
:
417 case GL_UNSIGNED_SHORT
:
418 case GL_UNSIGNED_INT
:
423 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
428 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
430 case GL_HALF_FLOAT_ARB
:
432 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
433 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
438 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
439 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
441 case GL_UNSIGNED_INT_5_9_9_9_REV
:
442 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
445 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
446 assert(ctx
->Extensions
.EXT_packed_float
);
447 return GL_R11F_G11F_B10F
;
455 * Create a temporary texture to hold an image of the given size.
456 * If width, height are not POT and the driver only handles POT textures,
457 * allocate the next larger size of texture that is POT.
459 static struct pipe_resource
*
460 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
461 enum pipe_format texFormat
)
463 struct pipe_resource
*pt
;
465 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
466 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
473 * Make texture containing an image for glDrawPixels image.
474 * If 'pixels' is NULL, leave the texture image data undefined.
476 static struct pipe_resource
*
477 make_texture(struct st_context
*st
,
478 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
479 const struct gl_pixelstore_attrib
*unpack
,
480 const GLvoid
*pixels
)
482 struct gl_context
*ctx
= st
->ctx
;
483 struct pipe_context
*pipe
= st
->pipe
;
485 struct pipe_resource
*pt
;
486 enum pipe_format pipeFormat
;
487 GLenum baseInternalFormat
, intFormat
;
489 intFormat
= internal_format(ctx
, format
, type
);
490 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
492 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
493 format
, type
, GL_FALSE
);
496 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
499 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
503 /* alloc temporary texture */
504 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
506 _mesa_unmap_pbo_source(ctx
, unpack
);
511 struct pipe_transfer
*transfer
;
514 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
516 /* we'll do pixel transfer in a fragment shader */
517 ctx
->_ImageTransferState
= 0x0;
519 /* map texture transfer */
520 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
521 PIPE_TRANSFER_WRITE
, 0, 0,
522 width
, height
, &transfer
);
525 /* Put image into texture transfer.
526 * Note that the image is actually going to be upside down in
527 * the texture. We deal with that with texcoords.
529 success
= _mesa_texstore(ctx
, 2, /* dims */
530 baseInternalFormat
, /* baseInternalFormat */
531 mformat
, /* gl_format */
532 transfer
->stride
, /* dstRowStride, bytes */
533 &dest
, /* destSlices */
534 width
, height
, 1, /* size */
535 format
, type
, /* src format/type */
536 pixels
, /* data source */
540 pipe_transfer_unmap(pipe
, transfer
);
545 ctx
->_ImageTransferState
= imageTransferStateSave
;
548 _mesa_unmap_pbo_source(ctx
, unpack
);
555 * Draw quad with texcoords and optional color.
556 * Coords are gallium window coords with y=0=top.
557 * \param color may be null
558 * \param invertTex if true, flip texcoords vertically
561 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
562 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
563 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
565 struct st_context
*st
= st_context(ctx
);
566 struct pipe_context
*pipe
= st
->pipe
;
567 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
568 struct pipe_resource
*buf
= NULL
;
571 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
, &buf
,
577 /* setup vertex data */
579 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
580 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
581 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
582 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
583 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
584 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
585 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
586 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
587 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
588 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
592 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
593 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
596 verts
[1][0][0] = clip_x1
;
597 verts
[1][0][1] = clip_y0
;
600 verts
[2][0][0] = clip_x1
;
601 verts
[2][0][1] = clip_y1
;
604 verts
[3][0][0] = clip_x0
;
605 verts
[3][0][1] = clip_y1
;
607 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
608 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
609 verts
[1][1][0] = sRight
;
610 verts
[1][1][1] = tTop
;
611 verts
[2][1][0] = sRight
;
612 verts
[2][1][1] = tBot
;
613 verts
[3][1][0] = sLeft
;
614 verts
[3][1][1] = tBot
;
616 /* same for all verts: */
618 for (i
= 0; i
< 4; i
++) {
619 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
620 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
621 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
622 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
623 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
624 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
625 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
626 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
630 for (i
= 0; i
< 4; i
++) {
631 verts
[i
][0][2] = z
; /*Z*/
632 verts
[i
][0][3] = 1.0f
; /*W*/
633 verts
[i
][1][2] = 0.0f
; /*R*/
634 verts
[i
][1][3] = 1.0f
; /*Q*/
639 u_upload_unmap(st
->uploader
);
640 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, offset
,
643 3); /* attribs/vert */
644 pipe_resource_reference(&buf
, NULL
);
650 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
651 GLsizei width
, GLsizei height
,
652 GLfloat zoomX
, GLfloat zoomY
,
653 struct pipe_sampler_view
**sv
,
654 int num_sampler_view
,
657 const GLfloat
*color
,
659 GLboolean write_depth
, GLboolean write_stencil
)
661 struct st_context
*st
= st_context(ctx
);
662 struct pipe_context
*pipe
= st
->pipe
;
663 struct cso_context
*cso
= st
->cso_context
;
664 GLfloat x0
, y0
, x1
, y1
;
666 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
669 /* XXX if DrawPixels image is larger than max texture size, break
672 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
673 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
674 assert(width
<= maxSize
);
675 assert(height
<= maxSize
);
677 cso_save_rasterizer(cso
);
678 cso_save_viewport(cso
);
679 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
680 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
681 cso_save_fragment_shader(cso
);
682 cso_save_stream_outputs(cso
);
683 cso_save_vertex_shader(cso
);
684 cso_save_geometry_shader(cso
);
685 cso_save_vertex_elements(cso
);
686 cso_save_vertex_buffers(cso
);
688 cso_save_depth_stencil_alpha(cso
);
692 /* rasterizer state: just scissor */
694 struct pipe_rasterizer_state rasterizer
;
695 memset(&rasterizer
, 0, sizeof(rasterizer
));
696 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
697 ctx
->Color
._ClampFragmentColor
&&
698 !ctx
->DrawBuffer
->_IntegerColor
;
699 rasterizer
.gl_rasterization_rules
= 1;
700 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
701 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
702 cso_set_rasterizer(cso
, &rasterizer
);
706 /* Stencil writing bypasses the normal fragment pipeline to
707 * disable color writing and set stencil test to always pass.
709 struct pipe_depth_stencil_alpha_state dsa
;
710 struct pipe_blend_state blend
;
713 memset(&dsa
, 0, sizeof(dsa
));
714 dsa
.stencil
[0].enabled
= 1;
715 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
716 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
717 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
719 /* writing depth+stencil: depth test always passes */
720 dsa
.depth
.enabled
= 1;
721 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
722 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
724 cso_set_depth_stencil_alpha(cso
, &dsa
);
726 /* blend (colormask) */
727 memset(&blend
, 0, sizeof(blend
));
728 cso_set_blend(cso
, &blend
);
731 /* fragment shader state: TEX lookup program */
732 cso_set_fragment_shader_handle(cso
, driver_fp
);
734 /* vertex shader state: position + texcoord pass-through */
735 cso_set_vertex_shader_handle(cso
, driver_vp
);
737 /* geometry shader state: disabled */
738 cso_set_geometry_shader_handle(cso
, NULL
);
740 /* texture sampling state: */
742 struct pipe_sampler_state sampler
;
743 memset(&sampler
, 0, sizeof(sampler
));
744 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
745 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
746 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
747 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
748 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
749 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
750 sampler
.normalized_coords
= normalized
;
752 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 0, &sampler
);
753 if (num_sampler_view
> 1) {
754 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler
);
756 cso_single_sampler_done(cso
, PIPE_SHADER_FRAGMENT
);
759 /* viewport state: viewport matching window dims */
761 const float w
= (float) ctx
->DrawBuffer
->Width
;
762 const float h
= (float) ctx
->DrawBuffer
->Height
;
763 struct pipe_viewport_state vp
;
764 vp
.scale
[0] = 0.5f
* w
;
765 vp
.scale
[1] = -0.5f
* h
;
768 vp
.translate
[0] = 0.5f
* w
;
769 vp
.translate
[1] = 0.5f
* h
;
770 vp
.translate
[2] = 0.5f
;
771 vp
.translate
[3] = 0.0f
;
772 cso_set_viewport(cso
, &vp
);
775 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
776 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
779 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
781 /* Compute Gallium window coords (y=0=top) with pixel zoom.
782 * Recall that these coords are transformed by the current
783 * vertex shader and viewport transformation.
785 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
786 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
787 invertTex
= !invertTex
;
791 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
793 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
795 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
798 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
799 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
800 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
803 cso_restore_rasterizer(cso
);
804 cso_restore_viewport(cso
);
805 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
806 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
807 cso_restore_fragment_shader(cso
);
808 cso_restore_vertex_shader(cso
);
809 cso_restore_geometry_shader(cso
);
810 cso_restore_vertex_elements(cso
);
811 cso_restore_vertex_buffers(cso
);
812 cso_restore_stream_outputs(cso
);
814 cso_restore_depth_stencil_alpha(cso
);
815 cso_restore_blend(cso
);
821 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
822 * can't use a fragment shader to write stencil values.
825 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
826 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
827 const struct gl_pixelstore_attrib
*unpack
,
828 const GLvoid
*pixels
)
830 struct st_context
*st
= st_context(ctx
);
831 struct pipe_context
*pipe
= st
->pipe
;
832 struct st_renderbuffer
*strb
;
833 enum pipe_transfer_usage usage
;
834 struct pipe_transfer
*pt
;
835 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
837 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
842 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
844 /* totally clipped */
849 strb
= st_renderbuffer(ctx
->DrawBuffer
->
850 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
852 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
853 y
= ctx
->DrawBuffer
->Height
- y
- height
;
856 if (format
== GL_STENCIL_INDEX
&&
857 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
858 /* writing stencil to a combined depth+stencil buffer */
859 usage
= PIPE_TRANSFER_READ_WRITE
;
862 usage
= PIPE_TRANSFER_WRITE
;
865 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
866 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
870 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
873 sValues
= malloc(width
* sizeof(GLubyte
));
874 zValues
= malloc(width
* sizeof(GLuint
));
876 if (sValues
&& zValues
) {
878 for (row
= 0; row
< height
; row
++) {
879 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
880 GLenum destType
= GL_UNSIGNED_BYTE
;
881 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
885 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
886 type
, source
, &clippedUnpack
,
887 ctx
->_ImageTransferState
);
889 if (format
== GL_DEPTH_STENCIL
) {
891 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
892 GL_FLOAT
: GL_UNSIGNED_INT
;
894 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
895 (1 << 24) - 1, type
, source
,
900 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
901 "zoom not complete");
907 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
908 spanY
= height
- row
- 1;
914 /* now pack the stencil (and Z) values in the dest format */
915 switch (pt
->resource
->format
) {
916 case PIPE_FORMAT_S8_UINT
:
918 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
919 assert(usage
== PIPE_TRANSFER_WRITE
);
920 memcpy(dest
, sValues
, width
);
923 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
924 if (format
== GL_DEPTH_STENCIL
) {
925 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
927 assert(usage
== PIPE_TRANSFER_WRITE
);
928 for (k
= 0; k
< width
; k
++) {
929 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
933 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
935 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
936 for (k
= 0; k
< width
; k
++) {
937 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
941 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
942 if (format
== GL_DEPTH_STENCIL
) {
943 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
945 assert(usage
== PIPE_TRANSFER_WRITE
);
946 for (k
= 0; k
< width
; k
++) {
947 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
951 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
953 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
954 for (k
= 0; k
< width
; k
++) {
955 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
959 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
960 if (format
== GL_DEPTH_STENCIL
) {
961 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
962 GLfloat
*destf
= (GLfloat
*)dest
;
964 assert(usage
== PIPE_TRANSFER_WRITE
);
965 for (k
= 0; k
< width
; k
++) {
966 destf
[k
*2] = zValuesFloat
[k
];
967 dest
[k
*2+1] = sValues
[k
] & 0xff;
971 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
973 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
974 for (k
= 0; k
< width
; k
++) {
975 dest
[k
*2+1] = sValues
[k
] & 0xff;
986 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
992 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
994 /* unmap the stencil buffer */
995 pipe_transfer_unmap(pipe
, pt
);
1000 * Get fragment program variant for a glDrawPixels or glCopyPixels
1001 * command for RGBA data.
1003 static struct st_fp_variant
*
1004 get_color_fp_variant(struct st_context
*st
)
1006 struct gl_context
*ctx
= st
->ctx
;
1007 struct st_fp_variant_key key
;
1008 struct st_fp_variant
*fpv
;
1010 memset(&key
, 0, sizeof(key
));
1014 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1015 ctx
->Pixel
.RedScale
!= 1.0 ||
1016 ctx
->Pixel
.GreenBias
!= 0.0 ||
1017 ctx
->Pixel
.GreenScale
!= 1.0 ||
1018 ctx
->Pixel
.BlueBias
!= 0.0 ||
1019 ctx
->Pixel
.BlueScale
!= 1.0 ||
1020 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1021 ctx
->Pixel
.AlphaScale
!= 1.0);
1022 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1023 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1024 st
->ctx
->Color
._ClampFragmentColor
&&
1025 !st
->ctx
->DrawBuffer
->_IntegerColor
;
1027 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1034 * Get fragment program variant for a glDrawPixels or glCopyPixels
1035 * command for depth/stencil data.
1037 static struct st_fp_variant
*
1038 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1039 GLboolean write_stencil
)
1041 struct st_fp_variant_key key
;
1042 struct st_fp_variant
*fpv
;
1044 memset(&key
, 0, sizeof(key
));
1048 key
.drawpixels_z
= write_depth
;
1049 key
.drawpixels_stencil
= write_stencil
;
1051 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1058 * Clamp glDrawPixels width and height to the maximum texture size.
1061 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1062 struct gl_pixelstore_attrib
*unpack
)
1064 const unsigned maxSize
=
1065 1 << (pipe
->screen
->get_param(pipe
->screen
,
1066 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1068 if (*width
> maxSize
) {
1069 if (unpack
->RowLength
== 0)
1070 unpack
->RowLength
= *width
;
1073 if (*height
> maxSize
) {
1080 * Called via ctx->Driver.DrawPixels()
1083 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1084 GLsizei width
, GLsizei height
,
1085 GLenum format
, GLenum type
,
1086 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1088 void *driver_vp
, *driver_fp
;
1089 struct st_context
*st
= st_context(ctx
);
1090 const GLfloat
*color
;
1091 struct pipe_context
*pipe
= st
->pipe
;
1092 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1093 struct pipe_sampler_view
*sv
[2];
1094 int num_sampler_view
= 1;
1095 struct st_fp_variant
*fpv
;
1096 struct gl_pixelstore_attrib clippedUnpack
;
1098 /* Mesa state should be up to date by now */
1099 assert(ctx
->NewState
== 0x0);
1101 st_validate_state(st
);
1103 /* Limit the size of the glDrawPixels to the max texture size.
1104 * Strictly speaking, that's not correct but since we don't handle
1105 * larger images yet, this is better than crashing.
1107 clippedUnpack
= *unpack
;
1108 unpack
= &clippedUnpack
;
1109 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1111 if (format
== GL_DEPTH_STENCIL
)
1112 write_stencil
= write_depth
= GL_TRUE
;
1113 else if (format
== GL_STENCIL_INDEX
)
1114 write_stencil
= GL_TRUE
;
1115 else if (format
== GL_DEPTH_COMPONENT
)
1116 write_depth
= GL_TRUE
;
1118 if (write_stencil
&&
1119 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1120 /* software fallback */
1121 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1127 * Get vertex/fragment shaders
1129 if (write_depth
|| write_stencil
) {
1130 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1132 driver_fp
= fpv
->driver_shader
;
1134 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1136 color
= ctx
->Current
.RasterColor
;
1139 fpv
= get_color_fp_variant(st
);
1141 driver_fp
= fpv
->driver_shader
;
1143 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1146 if (st
->pixel_xfer
.pixelmap_enabled
) {
1147 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1152 /* update fragment program constants */
1153 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1155 /* draw with textured quad */
1157 struct pipe_resource
*pt
1158 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1160 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1163 /* Create a second sampler view to read stencil.
1164 * The stencil is written using the shader stencil export
1166 if (write_stencil
) {
1167 enum pipe_format stencil_format
=
1168 util_format_stencil_only(pt
->format
);
1170 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1175 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1177 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1182 color
, GL_FALSE
, write_depth
, write_stencil
);
1183 pipe_sampler_view_reference(&sv
[0], NULL
);
1184 if (num_sampler_view
> 1)
1185 pipe_sampler_view_reference(&sv
[1], NULL
);
1187 pipe_resource_reference(&pt
, NULL
);
1195 * Software fallback for glCopyPixels(GL_STENCIL).
1198 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1199 GLsizei width
, GLsizei height
,
1200 GLint dstx
, GLint dsty
)
1202 struct st_renderbuffer
*rbDraw
;
1203 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1204 enum pipe_transfer_usage usage
;
1205 struct pipe_transfer
*ptDraw
;
1210 buffer
= malloc(width
* height
* sizeof(ubyte
));
1212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1216 /* Get the dest renderbuffer */
1217 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1218 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1220 /* this will do stencil pixel transfer ops */
1221 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1222 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1223 &ctx
->DefaultPacking
, buffer
);
1226 /* debug code: dump stencil values */
1228 for (row
= 0; row
< height
; row
++) {
1229 printf("%3d: ", row
);
1230 for (col
= 0; col
< width
; col
++) {
1231 printf("%02x ", buffer
[col
+ row
* width
]);
1237 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1238 usage
= PIPE_TRANSFER_READ_WRITE
;
1240 usage
= PIPE_TRANSFER_WRITE
;
1242 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1243 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1246 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1247 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1249 /* map the stencil buffer */
1250 drawMap
= pipe_transfer_map(pipe
,
1253 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1255 width
, height
, &ptDraw
);
1258 /* XXX PixelZoom not handled yet */
1259 for (i
= 0; i
< height
; i
++) {
1266 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1270 dst
= drawMap
+ y
* ptDraw
->stride
;
1271 src
= buffer
+ i
* width
;
1273 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1278 /* unmap the stencil buffer */
1279 pipe_transfer_unmap(pipe
, ptDraw
);
1284 * Return renderbuffer to use for reading color pixels for glCopyPixels
1286 static struct st_renderbuffer
*
1287 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1289 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1290 struct st_renderbuffer
*strb
=
1291 st_renderbuffer(fb
->_ColorReadBuffer
);
1297 /** Do the src/dest regions overlap? */
1299 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1300 GLsizei width
, GLsizei height
)
1302 if (srcX
+ width
<= dstX
||
1303 dstX
+ width
<= srcX
||
1304 srcY
+ height
<= dstY
||
1305 dstY
+ height
<= srcY
)
1313 * Try to do a glCopyPixels for simple cases with a blit by calling
1314 * pipe->resource_copy_region().
1316 * We can do this when we're copying color pixels (depth/stencil
1317 * eventually) with no pixel zoom, no pixel transfer ops, no
1318 * per-fragment ops, the src/dest regions don't overlap and the
1319 * src/dest pixel formats are the same.
1322 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1323 GLsizei width
, GLsizei height
,
1324 GLint dstx
, GLint dsty
, GLenum type
)
1326 struct st_context
*st
= st_context(ctx
);
1327 struct pipe_context
*pipe
= st
->pipe
;
1328 struct gl_pixelstore_attrib pack
, unpack
;
1329 GLint readX
, readY
, readW
, readH
;
1331 if (type
== GL_COLOR
&&
1332 ctx
->Pixel
.ZoomX
== 1.0 &&
1333 ctx
->Pixel
.ZoomY
== 1.0 &&
1334 ctx
->_ImageTransferState
== 0x0 &&
1335 !ctx
->Color
.BlendEnabled
&&
1336 !ctx
->Color
.AlphaEnabled
&&
1338 !ctx
->Fog
.Enabled
&&
1339 !ctx
->Stencil
.Enabled
&&
1340 !ctx
->FragmentProgram
.Enabled
&&
1341 !ctx
->VertexProgram
.Enabled
&&
1342 !ctx
->Shader
.CurrentFragmentProgram
&&
1343 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1344 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1345 !ctx
->Query
.CondRenderQuery
) {
1346 struct st_renderbuffer
*rbRead
, *rbDraw
;
1350 * Clip the read region against the src buffer bounds.
1351 * We'll still allocate a temporary buffer/texture for the original
1352 * src region size but we'll only read the region which is on-screen.
1353 * This may mean that we draw garbage pixels into the dest region, but
1360 pack
= ctx
->DefaultPacking
;
1361 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1362 return GL_TRUE
; /* all done */
1364 /* clip against dest buffer bounds and scissor box */
1365 drawX
= dstx
+ pack
.SkipPixels
;
1366 drawY
= dsty
+ pack
.SkipRows
;
1368 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1369 return GL_TRUE
; /* all done */
1371 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1372 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1374 rbRead
= st_get_color_read_renderbuffer(ctx
);
1375 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1377 if ((rbRead
!= rbDraw
||
1378 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1379 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1380 struct pipe_box srcBox
;
1382 /* flip src/dst position if needed */
1383 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1384 /* both buffers will have the same orientation */
1385 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1386 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1389 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1391 pipe
->resource_copy_region(pipe
,
1393 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1395 rbRead
->rtt_level
, &srcBox
);
1405 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1406 GLsizei width
, GLsizei height
,
1407 GLint dstx
, GLint dsty
, GLenum type
)
1409 struct st_context
*st
= st_context(ctx
);
1410 struct pipe_context
*pipe
= st
->pipe
;
1411 struct pipe_screen
*screen
= pipe
->screen
;
1412 struct st_renderbuffer
*rbRead
;
1413 void *driver_vp
, *driver_fp
;
1414 struct pipe_resource
*pt
;
1415 struct pipe_sampler_view
*sv
[2];
1416 int num_sampler_view
= 1;
1418 enum pipe_format srcFormat
, texFormat
;
1419 GLboolean invertTex
= GL_FALSE
;
1420 GLint readX
, readY
, readW
, readH
;
1421 GLuint sample_count
;
1422 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1423 struct st_fp_variant
*fpv
;
1425 st_validate_state(st
);
1427 if (type
== GL_DEPTH_STENCIL
) {
1428 /* XXX make this more efficient */
1429 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1430 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1434 if (type
== GL_STENCIL
) {
1435 /* can't use texturing to do stencil */
1436 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1440 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1444 * The subsequent code implements glCopyPixels by copying the source
1445 * pixels into a temporary texture that's then applied to a textured quad.
1446 * When we draw the textured quad, all the usual per-fragment operations
1452 * Get vertex/fragment shaders
1454 if (type
== GL_COLOR
) {
1455 rbRead
= st_get_color_read_renderbuffer(ctx
);
1458 fpv
= get_color_fp_variant(st
);
1459 driver_fp
= fpv
->driver_shader
;
1461 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1463 if (st
->pixel_xfer
.pixelmap_enabled
) {
1464 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1469 assert(type
== GL_DEPTH
);
1470 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1471 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1472 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1474 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1475 driver_fp
= fpv
->driver_shader
;
1477 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1480 /* update fragment program constants */
1481 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1483 sample_count
= rbRead
->texture
->nr_samples
;
1484 /* I believe this would be legal, presumably would need to do a resolve
1485 for color, and for depth/stencil spec says to just use one of the
1486 depth/stencil samples per pixel? Need some transfer clarifications. */
1487 assert(sample_count
< 2);
1489 srcFormat
= rbRead
->texture
->format
;
1491 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1493 PIPE_BIND_SAMPLER_VIEW
)) {
1494 texFormat
= srcFormat
;
1497 /* srcFormat can't be used as a texture format */
1498 if (type
== GL_DEPTH
) {
1499 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1500 GL_NONE
, GL_NONE
, st
->internal_target
,
1501 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1502 assert(texFormat
!= PIPE_FORMAT_NONE
);
1505 /* default color format */
1506 texFormat
= st_choose_format(screen
, GL_RGBA
,
1507 GL_NONE
, GL_NONE
, st
->internal_target
,
1508 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1509 assert(texFormat
!= PIPE_FORMAT_NONE
);
1513 /* Invert src region if needed */
1514 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1515 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1516 invertTex
= !invertTex
;
1519 /* Clip the read region against the src buffer bounds.
1520 * We'll still allocate a temporary buffer/texture for the original
1521 * src region size but we'll only read the region which is on-screen.
1522 * This may mean that we draw garbage pixels into the dest region, but
1529 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1530 /* The source region is completely out of bounds. Do nothing.
1531 * The GL spec says "Results of copies from outside the window,
1532 * or from regions of the window that are not exposed, are
1533 * hardware dependent and undefined."
1538 readW
= MAX2(0, readW
);
1539 readH
= MAX2(0, readH
);
1541 /* alloc temporary texture */
1542 pt
= alloc_texture(st
, width
, height
, texFormat
);
1546 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1548 pipe_resource_reference(&pt
, NULL
);
1552 /* Make temporary texture which is a copy of the src region.
1554 if (srcFormat
== texFormat
) {
1555 struct pipe_box src_box
;
1556 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1557 /* copy source framebuffer surface into mipmap/texture */
1558 pipe
->resource_copy_region(pipe
,
1561 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1562 rbRead
->texture
, /* src tex */
1563 rbRead
->rtt_level
, /* src lvl */
1568 /* CPU-based fallback/conversion */
1569 struct pipe_transfer
*ptRead
;
1571 pipe_transfer_map(st
->pipe
, rbRead
->texture
,
1573 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1575 readX
, readY
, readW
, readH
, &ptRead
);
1576 struct pipe_transfer
*ptTex
;
1578 enum pipe_transfer_usage transfer_usage
;
1580 if (ST_DEBUG
& DEBUG_FALLBACK
)
1581 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1583 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1584 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1586 transfer_usage
= PIPE_TRANSFER_WRITE
;
1588 mapTex
= pipe_transfer_map(st
->pipe
, pt
, 0, 0, transfer_usage
,
1589 0, 0, width
, height
, &ptTex
);
1591 /* copy image from ptRead surface to ptTex surface */
1592 if (type
== GL_COLOR
) {
1593 /* alternate path using get/put_tile() */
1594 GLfloat
*buf
= malloc(width
* height
* 4 * sizeof(GLfloat
));
1595 enum pipe_format readFormat
, drawFormat
;
1596 readFormat
= util_format_linear(rbRead
->texture
->format
);
1597 drawFormat
= util_format_linear(pt
->format
);
1598 pipe_get_tile_rgba_format(ptRead
, mapRead
, 0, 0, readW
, readH
,
1600 pipe_put_tile_rgba_format(ptTex
, mapTex
, pack
.SkipPixels
,
1602 readW
, readH
, drawFormat
, buf
);
1607 GLuint
*buf
= malloc(width
* height
* sizeof(GLuint
));
1608 pipe_get_tile_z(ptRead
, mapRead
, 0, 0, readW
, readH
, buf
);
1609 pipe_put_tile_z(ptTex
, mapTex
, pack
.SkipPixels
, pack
.SkipRows
,
1614 pipe
->transfer_unmap(pipe
, ptRead
);
1615 pipe
->transfer_unmap(pipe
, ptTex
);
1618 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1619 * textured quad with that texture.
1621 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1622 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1627 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1629 pipe_resource_reference(&pt
, NULL
);
1630 pipe_sampler_view_reference(&sv
[0], NULL
);
1635 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1637 functions
->DrawPixels
= st_DrawPixels
;
1638 functions
->CopyPixels
= st_CopyPixels
;
1643 st_destroy_drawpix(struct st_context
*st
)
1647 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1648 if (st
->drawpix
.shaders
[i
])
1649 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1652 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1653 if (st
->drawpix
.vert_shaders
[0])
1654 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1655 if (st
->drawpix
.vert_shaders
[1])
1656 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);