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 if (u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
572 &buf
, (void **) &verts
) != PIPE_OK
) {
576 /* setup vertex data */
578 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
579 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
580 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
581 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
582 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
583 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
584 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
585 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
586 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
587 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
591 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
592 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
595 verts
[1][0][0] = clip_x1
;
596 verts
[1][0][1] = clip_y0
;
599 verts
[2][0][0] = clip_x1
;
600 verts
[2][0][1] = clip_y1
;
603 verts
[3][0][0] = clip_x0
;
604 verts
[3][0][1] = clip_y1
;
606 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
607 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
608 verts
[1][1][0] = sRight
;
609 verts
[1][1][1] = tTop
;
610 verts
[2][1][0] = sRight
;
611 verts
[2][1][1] = tBot
;
612 verts
[3][1][0] = sLeft
;
613 verts
[3][1][1] = tBot
;
615 /* same for all verts: */
617 for (i
= 0; i
< 4; i
++) {
618 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
619 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
620 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
621 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
622 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
623 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
624 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
625 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
629 for (i
= 0; i
< 4; i
++) {
630 verts
[i
][0][2] = z
; /*Z*/
631 verts
[i
][0][3] = 1.0f
; /*W*/
632 verts
[i
][1][2] = 0.0f
; /*R*/
633 verts
[i
][1][3] = 1.0f
; /*Q*/
638 u_upload_unmap(st
->uploader
);
639 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
640 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
644 3); /* attribs/vert */
645 pipe_resource_reference(&buf
, NULL
);
651 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
652 GLsizei width
, GLsizei height
,
653 GLfloat zoomX
, GLfloat zoomY
,
654 struct pipe_sampler_view
**sv
,
655 int num_sampler_view
,
658 const GLfloat
*color
,
660 GLboolean write_depth
, GLboolean write_stencil
)
662 struct st_context
*st
= st_context(ctx
);
663 struct pipe_context
*pipe
= st
->pipe
;
664 struct cso_context
*cso
= st
->cso_context
;
665 GLfloat x0
, y0
, x1
, y1
;
667 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
670 /* XXX if DrawPixels image is larger than max texture size, break
673 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
674 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
675 assert(width
<= maxSize
);
676 assert(height
<= maxSize
);
678 cso_save_rasterizer(cso
);
679 cso_save_viewport(cso
);
680 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
681 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
682 cso_save_fragment_shader(cso
);
683 cso_save_stream_outputs(cso
);
684 cso_save_vertex_shader(cso
);
685 cso_save_geometry_shader(cso
);
686 cso_save_vertex_elements(cso
);
687 cso_save_aux_vertex_buffer_slot(cso
);
689 cso_save_depth_stencil_alpha(cso
);
693 /* rasterizer state: just scissor */
695 struct pipe_rasterizer_state rasterizer
;
696 memset(&rasterizer
, 0, sizeof(rasterizer
));
697 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
698 ctx
->Color
._ClampFragmentColor
&&
699 !ctx
->DrawBuffer
->_IntegerColor
;
700 rasterizer
.gl_rasterization_rules
= 1;
701 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
702 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
703 cso_set_rasterizer(cso
, &rasterizer
);
707 /* Stencil writing bypasses the normal fragment pipeline to
708 * disable color writing and set stencil test to always pass.
710 struct pipe_depth_stencil_alpha_state dsa
;
711 struct pipe_blend_state blend
;
714 memset(&dsa
, 0, sizeof(dsa
));
715 dsa
.stencil
[0].enabled
= 1;
716 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
717 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
718 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
720 /* writing depth+stencil: depth test always passes */
721 dsa
.depth
.enabled
= 1;
722 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
723 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
725 cso_set_depth_stencil_alpha(cso
, &dsa
);
727 /* blend (colormask) */
728 memset(&blend
, 0, sizeof(blend
));
729 cso_set_blend(cso
, &blend
);
732 /* fragment shader state: TEX lookup program */
733 cso_set_fragment_shader_handle(cso
, driver_fp
);
735 /* vertex shader state: position + texcoord pass-through */
736 cso_set_vertex_shader_handle(cso
, driver_vp
);
738 /* geometry shader state: disabled */
739 cso_set_geometry_shader_handle(cso
, NULL
);
741 /* texture sampling state: */
743 struct pipe_sampler_state sampler
;
744 memset(&sampler
, 0, sizeof(sampler
));
745 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
746 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
747 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
748 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
749 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
750 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
751 sampler
.normalized_coords
= normalized
;
753 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 0, &sampler
);
754 if (num_sampler_view
> 1) {
755 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler
);
757 cso_single_sampler_done(cso
, PIPE_SHADER_FRAGMENT
);
760 /* viewport state: viewport matching window dims */
762 const float w
= (float) ctx
->DrawBuffer
->Width
;
763 const float h
= (float) ctx
->DrawBuffer
->Height
;
764 struct pipe_viewport_state vp
;
765 vp
.scale
[0] = 0.5f
* w
;
766 vp
.scale
[1] = -0.5f
* h
;
769 vp
.translate
[0] = 0.5f
* w
;
770 vp
.translate
[1] = 0.5f
* h
;
771 vp
.translate
[2] = 0.5f
;
772 vp
.translate
[3] = 0.0f
;
773 cso_set_viewport(cso
, &vp
);
776 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
777 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
780 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
782 /* Compute Gallium window coords (y=0=top) with pixel zoom.
783 * Recall that these coords are transformed by the current
784 * vertex shader and viewport transformation.
786 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
787 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
788 invertTex
= !invertTex
;
792 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
794 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
796 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
799 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
800 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
801 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
804 cso_restore_rasterizer(cso
);
805 cso_restore_viewport(cso
);
806 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
807 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
808 cso_restore_fragment_shader(cso
);
809 cso_restore_vertex_shader(cso
);
810 cso_restore_geometry_shader(cso
);
811 cso_restore_vertex_elements(cso
);
812 cso_restore_aux_vertex_buffer_slot(cso
);
813 cso_restore_stream_outputs(cso
);
815 cso_restore_depth_stencil_alpha(cso
);
816 cso_restore_blend(cso
);
822 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
823 * can't use a fragment shader to write stencil values.
826 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
827 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
828 const struct gl_pixelstore_attrib
*unpack
,
829 const GLvoid
*pixels
)
831 struct st_context
*st
= st_context(ctx
);
832 struct pipe_context
*pipe
= st
->pipe
;
833 struct st_renderbuffer
*strb
;
834 enum pipe_transfer_usage usage
;
835 struct pipe_transfer
*pt
;
836 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
838 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
843 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
845 /* totally clipped */
850 strb
= st_renderbuffer(ctx
->DrawBuffer
->
851 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
853 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
854 y
= ctx
->DrawBuffer
->Height
- y
- height
;
857 if (format
== GL_STENCIL_INDEX
&&
858 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
859 /* writing stencil to a combined depth+stencil buffer */
860 usage
= PIPE_TRANSFER_READ_WRITE
;
863 usage
= PIPE_TRANSFER_WRITE
;
866 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
867 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
871 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
874 sValues
= malloc(width
* sizeof(GLubyte
));
875 zValues
= malloc(width
* sizeof(GLuint
));
877 if (sValues
&& zValues
) {
879 for (row
= 0; row
< height
; row
++) {
880 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
881 GLenum destType
= GL_UNSIGNED_BYTE
;
882 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
886 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
887 type
, source
, &clippedUnpack
,
888 ctx
->_ImageTransferState
);
890 if (format
== GL_DEPTH_STENCIL
) {
892 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
893 GL_FLOAT
: GL_UNSIGNED_INT
;
895 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
896 (1 << 24) - 1, type
, source
,
901 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
902 "zoom not complete");
908 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
909 spanY
= height
- row
- 1;
915 /* now pack the stencil (and Z) values in the dest format */
916 switch (pt
->resource
->format
) {
917 case PIPE_FORMAT_S8_UINT
:
919 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
920 assert(usage
== PIPE_TRANSFER_WRITE
);
921 memcpy(dest
, sValues
, width
);
924 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
925 if (format
== GL_DEPTH_STENCIL
) {
926 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
928 assert(usage
== PIPE_TRANSFER_WRITE
);
929 for (k
= 0; k
< width
; k
++) {
930 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
934 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
936 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
937 for (k
= 0; k
< width
; k
++) {
938 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
942 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
943 if (format
== GL_DEPTH_STENCIL
) {
944 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
946 assert(usage
== PIPE_TRANSFER_WRITE
);
947 for (k
= 0; k
< width
; k
++) {
948 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
952 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
954 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
955 for (k
= 0; k
< width
; k
++) {
956 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
960 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
961 if (format
== GL_DEPTH_STENCIL
) {
962 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
963 GLfloat
*destf
= (GLfloat
*)dest
;
965 assert(usage
== PIPE_TRANSFER_WRITE
);
966 for (k
= 0; k
< width
; k
++) {
967 destf
[k
*2] = zValuesFloat
[k
];
968 dest
[k
*2+1] = sValues
[k
] & 0xff;
972 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
974 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
975 for (k
= 0; k
< width
; k
++) {
976 dest
[k
*2+1] = sValues
[k
] & 0xff;
987 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
993 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
995 /* unmap the stencil buffer */
996 pipe_transfer_unmap(pipe
, pt
);
1001 * Get fragment program variant for a glDrawPixels or glCopyPixels
1002 * command for RGBA data.
1004 static struct st_fp_variant
*
1005 get_color_fp_variant(struct st_context
*st
)
1007 struct gl_context
*ctx
= st
->ctx
;
1008 struct st_fp_variant_key key
;
1009 struct st_fp_variant
*fpv
;
1011 memset(&key
, 0, sizeof(key
));
1015 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1016 ctx
->Pixel
.RedScale
!= 1.0 ||
1017 ctx
->Pixel
.GreenBias
!= 0.0 ||
1018 ctx
->Pixel
.GreenScale
!= 1.0 ||
1019 ctx
->Pixel
.BlueBias
!= 0.0 ||
1020 ctx
->Pixel
.BlueScale
!= 1.0 ||
1021 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1022 ctx
->Pixel
.AlphaScale
!= 1.0);
1023 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1024 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1025 st
->ctx
->Color
._ClampFragmentColor
&&
1026 !st
->ctx
->DrawBuffer
->_IntegerColor
;
1028 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1035 * Get fragment program variant for a glDrawPixels or glCopyPixels
1036 * command for depth/stencil data.
1038 static struct st_fp_variant
*
1039 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1040 GLboolean write_stencil
)
1042 struct st_fp_variant_key key
;
1043 struct st_fp_variant
*fpv
;
1045 memset(&key
, 0, sizeof(key
));
1049 key
.drawpixels_z
= write_depth
;
1050 key
.drawpixels_stencil
= write_stencil
;
1052 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1059 * Clamp glDrawPixels width and height to the maximum texture size.
1062 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1063 struct gl_pixelstore_attrib
*unpack
)
1066 1 << (pipe
->screen
->get_param(pipe
->screen
,
1067 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1069 if (*width
> maxSize
) {
1070 if (unpack
->RowLength
== 0)
1071 unpack
->RowLength
= *width
;
1074 if (*height
> maxSize
) {
1081 * Called via ctx->Driver.DrawPixels()
1084 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1085 GLsizei width
, GLsizei height
,
1086 GLenum format
, GLenum type
,
1087 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1089 void *driver_vp
, *driver_fp
;
1090 struct st_context
*st
= st_context(ctx
);
1091 const GLfloat
*color
;
1092 struct pipe_context
*pipe
= st
->pipe
;
1093 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1094 struct pipe_sampler_view
*sv
[2];
1095 int num_sampler_view
= 1;
1096 struct st_fp_variant
*fpv
;
1097 struct gl_pixelstore_attrib clippedUnpack
;
1099 /* Mesa state should be up to date by now */
1100 assert(ctx
->NewState
== 0x0);
1102 st_validate_state(st
);
1104 /* Limit the size of the glDrawPixels to the max texture size.
1105 * Strictly speaking, that's not correct but since we don't handle
1106 * larger images yet, this is better than crashing.
1108 clippedUnpack
= *unpack
;
1109 unpack
= &clippedUnpack
;
1110 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1112 if (format
== GL_DEPTH_STENCIL
)
1113 write_stencil
= write_depth
= GL_TRUE
;
1114 else if (format
== GL_STENCIL_INDEX
)
1115 write_stencil
= GL_TRUE
;
1116 else if (format
== GL_DEPTH_COMPONENT
)
1117 write_depth
= GL_TRUE
;
1119 if (write_stencil
&&
1120 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1121 /* software fallback */
1122 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1128 * Get vertex/fragment shaders
1130 if (write_depth
|| write_stencil
) {
1131 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1133 driver_fp
= fpv
->driver_shader
;
1135 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1137 color
= ctx
->Current
.RasterColor
;
1140 fpv
= get_color_fp_variant(st
);
1142 driver_fp
= fpv
->driver_shader
;
1144 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1147 if (st
->pixel_xfer
.pixelmap_enabled
) {
1148 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1153 /* update fragment program constants */
1154 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1156 /* draw with textured quad */
1158 struct pipe_resource
*pt
1159 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1161 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1164 /* Create a second sampler view to read stencil.
1165 * The stencil is written using the shader stencil export
1167 if (write_stencil
) {
1168 enum pipe_format stencil_format
=
1169 util_format_stencil_only(pt
->format
);
1171 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1176 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1178 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1183 color
, GL_FALSE
, write_depth
, write_stencil
);
1184 pipe_sampler_view_reference(&sv
[0], NULL
);
1185 if (num_sampler_view
> 1)
1186 pipe_sampler_view_reference(&sv
[1], NULL
);
1188 pipe_resource_reference(&pt
, NULL
);
1196 * Software fallback for glCopyPixels(GL_STENCIL).
1199 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1200 GLsizei width
, GLsizei height
,
1201 GLint dstx
, GLint dsty
)
1203 struct st_renderbuffer
*rbDraw
;
1204 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1205 enum pipe_transfer_usage usage
;
1206 struct pipe_transfer
*ptDraw
;
1211 buffer
= malloc(width
* height
* sizeof(ubyte
));
1213 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1217 /* Get the dest renderbuffer */
1218 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1219 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1221 /* this will do stencil pixel transfer ops */
1222 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1223 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1224 &ctx
->DefaultPacking
, buffer
);
1227 /* debug code: dump stencil values */
1229 for (row
= 0; row
< height
; row
++) {
1230 printf("%3d: ", row
);
1231 for (col
= 0; col
< width
; col
++) {
1232 printf("%02x ", buffer
[col
+ row
* width
]);
1238 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1239 usage
= PIPE_TRANSFER_READ_WRITE
;
1241 usage
= PIPE_TRANSFER_WRITE
;
1243 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1244 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1247 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1248 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1250 /* map the stencil buffer */
1251 drawMap
= pipe_transfer_map(pipe
,
1254 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1256 width
, height
, &ptDraw
);
1259 /* XXX PixelZoom not handled yet */
1260 for (i
= 0; i
< height
; i
++) {
1267 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1271 dst
= drawMap
+ y
* ptDraw
->stride
;
1272 src
= buffer
+ i
* width
;
1274 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1279 /* unmap the stencil buffer */
1280 pipe_transfer_unmap(pipe
, ptDraw
);
1285 * Return renderbuffer to use for reading color pixels for glCopyPixels
1287 static struct st_renderbuffer
*
1288 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1290 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1291 struct st_renderbuffer
*strb
=
1292 st_renderbuffer(fb
->_ColorReadBuffer
);
1298 /** Do the src/dest regions overlap? */
1300 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1301 GLsizei width
, GLsizei height
)
1303 if (srcX
+ width
<= dstX
||
1304 dstX
+ width
<= srcX
||
1305 srcY
+ height
<= dstY
||
1306 dstY
+ height
<= srcY
)
1314 * Try to do a glCopyPixels for simple cases with a blit by calling
1315 * pipe->resource_copy_region().
1317 * We can do this when we're copying color pixels (depth/stencil
1318 * eventually) with no pixel zoom, no pixel transfer ops, no
1319 * per-fragment ops, the src/dest regions don't overlap and the
1320 * src/dest pixel formats are the same.
1323 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1324 GLsizei width
, GLsizei height
,
1325 GLint dstx
, GLint dsty
, GLenum type
)
1327 struct st_context
*st
= st_context(ctx
);
1328 struct pipe_context
*pipe
= st
->pipe
;
1329 struct gl_pixelstore_attrib pack
, unpack
;
1330 GLint readX
, readY
, readW
, readH
;
1332 if (type
== GL_COLOR
&&
1333 ctx
->Pixel
.ZoomX
== 1.0 &&
1334 ctx
->Pixel
.ZoomY
== 1.0 &&
1335 ctx
->_ImageTransferState
== 0x0 &&
1336 !ctx
->Color
.BlendEnabled
&&
1337 !ctx
->Color
.AlphaEnabled
&&
1339 !ctx
->Fog
.Enabled
&&
1340 !ctx
->Stencil
.Enabled
&&
1341 !ctx
->FragmentProgram
.Enabled
&&
1342 !ctx
->VertexProgram
.Enabled
&&
1343 !ctx
->Shader
.CurrentFragmentProgram
&&
1344 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1345 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1346 !ctx
->Query
.CondRenderQuery
) {
1347 struct st_renderbuffer
*rbRead
, *rbDraw
;
1351 * Clip the read region against the src buffer bounds.
1352 * We'll still allocate a temporary buffer/texture for the original
1353 * src region size but we'll only read the region which is on-screen.
1354 * This may mean that we draw garbage pixels into the dest region, but
1361 pack
= ctx
->DefaultPacking
;
1362 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1363 return GL_TRUE
; /* all done */
1365 /* clip against dest buffer bounds and scissor box */
1366 drawX
= dstx
+ pack
.SkipPixels
;
1367 drawY
= dsty
+ pack
.SkipRows
;
1369 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1370 return GL_TRUE
; /* all done */
1372 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1373 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1375 rbRead
= st_get_color_read_renderbuffer(ctx
);
1376 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1378 if ((rbRead
!= rbDraw
||
1379 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1380 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1381 struct pipe_box srcBox
;
1383 /* flip src/dst position if needed */
1384 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1385 /* both buffers will have the same orientation */
1386 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1387 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1390 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1392 pipe
->resource_copy_region(pipe
,
1394 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1396 rbRead
->rtt_level
, &srcBox
);
1406 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1407 GLsizei width
, GLsizei height
,
1408 GLint dstx
, GLint dsty
, GLenum type
)
1410 struct st_context
*st
= st_context(ctx
);
1411 struct pipe_context
*pipe
= st
->pipe
;
1412 struct pipe_screen
*screen
= pipe
->screen
;
1413 struct st_renderbuffer
*rbRead
;
1414 void *driver_vp
, *driver_fp
;
1415 struct pipe_resource
*pt
;
1416 struct pipe_sampler_view
*sv
[2];
1417 int num_sampler_view
= 1;
1419 enum pipe_format srcFormat
, texFormat
;
1420 GLboolean invertTex
= GL_FALSE
;
1421 GLint readX
, readY
, readW
, readH
;
1422 GLuint sample_count
;
1423 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1424 struct st_fp_variant
*fpv
;
1426 st_validate_state(st
);
1428 if (type
== GL_DEPTH_STENCIL
) {
1429 /* XXX make this more efficient */
1430 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1431 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1435 if (type
== GL_STENCIL
) {
1436 /* can't use texturing to do stencil */
1437 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1441 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1445 * The subsequent code implements glCopyPixels by copying the source
1446 * pixels into a temporary texture that's then applied to a textured quad.
1447 * When we draw the textured quad, all the usual per-fragment operations
1453 * Get vertex/fragment shaders
1455 if (type
== GL_COLOR
) {
1456 rbRead
= st_get_color_read_renderbuffer(ctx
);
1459 fpv
= get_color_fp_variant(st
);
1460 driver_fp
= fpv
->driver_shader
;
1462 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1464 if (st
->pixel_xfer
.pixelmap_enabled
) {
1465 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1470 assert(type
== GL_DEPTH
);
1471 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1472 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1473 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1475 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1476 driver_fp
= fpv
->driver_shader
;
1478 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1481 /* update fragment program constants */
1482 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1484 sample_count
= rbRead
->texture
->nr_samples
;
1485 /* I believe this would be legal, presumably would need to do a resolve
1486 for color, and for depth/stencil spec says to just use one of the
1487 depth/stencil samples per pixel? Need some transfer clarifications. */
1488 assert(sample_count
< 2);
1490 srcFormat
= rbRead
->texture
->format
;
1492 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1494 PIPE_BIND_SAMPLER_VIEW
)) {
1495 texFormat
= srcFormat
;
1498 /* srcFormat can't be used as a texture format */
1499 if (type
== GL_DEPTH
) {
1500 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1501 GL_NONE
, GL_NONE
, st
->internal_target
,
1502 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1503 assert(texFormat
!= PIPE_FORMAT_NONE
);
1506 /* default color format */
1507 texFormat
= st_choose_format(screen
, GL_RGBA
,
1508 GL_NONE
, GL_NONE
, st
->internal_target
,
1509 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1510 assert(texFormat
!= PIPE_FORMAT_NONE
);
1514 /* Invert src region if needed */
1515 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1516 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1517 invertTex
= !invertTex
;
1520 /* Clip the read region against the src buffer bounds.
1521 * We'll still allocate a temporary buffer/texture for the original
1522 * src region size but we'll only read the region which is on-screen.
1523 * This may mean that we draw garbage pixels into the dest region, but
1530 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1531 /* The source region is completely out of bounds. Do nothing.
1532 * The GL spec says "Results of copies from outside the window,
1533 * or from regions of the window that are not exposed, are
1534 * hardware dependent and undefined."
1539 readW
= MAX2(0, readW
);
1540 readH
= MAX2(0, readH
);
1542 /* alloc temporary texture */
1543 pt
= alloc_texture(st
, width
, height
, texFormat
);
1547 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1549 pipe_resource_reference(&pt
, NULL
);
1553 /* Make temporary texture which is a copy of the src region.
1555 if (srcFormat
== texFormat
) {
1556 struct pipe_box src_box
;
1557 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1558 /* copy source framebuffer surface into mipmap/texture */
1559 pipe
->resource_copy_region(pipe
,
1562 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1563 rbRead
->texture
, /* src tex */
1564 rbRead
->rtt_level
, /* src lvl */
1569 /* CPU-based fallback/conversion */
1570 struct pipe_transfer
*ptRead
;
1572 pipe_transfer_map(st
->pipe
, rbRead
->texture
,
1574 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1576 readX
, readY
, readW
, readH
, &ptRead
);
1577 struct pipe_transfer
*ptTex
;
1579 enum pipe_transfer_usage transfer_usage
;
1581 if (ST_DEBUG
& DEBUG_FALLBACK
)
1582 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1584 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1585 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1587 transfer_usage
= PIPE_TRANSFER_WRITE
;
1589 mapTex
= pipe_transfer_map(st
->pipe
, pt
, 0, 0, transfer_usage
,
1590 0, 0, width
, height
, &ptTex
);
1592 /* copy image from ptRead surface to ptTex surface */
1593 if (type
== GL_COLOR
) {
1594 /* alternate path using get/put_tile() */
1595 GLfloat
*buf
= malloc(width
* height
* 4 * sizeof(GLfloat
));
1596 enum pipe_format readFormat
, drawFormat
;
1597 readFormat
= util_format_linear(rbRead
->texture
->format
);
1598 drawFormat
= util_format_linear(pt
->format
);
1599 pipe_get_tile_rgba_format(ptRead
, mapRead
, 0, 0, readW
, readH
,
1601 pipe_put_tile_rgba_format(ptTex
, mapTex
, pack
.SkipPixels
,
1603 readW
, readH
, drawFormat
, buf
);
1608 GLuint
*buf
= malloc(width
* height
* sizeof(GLuint
));
1609 pipe_get_tile_z(ptRead
, mapRead
, 0, 0, readW
, readH
, buf
);
1610 pipe_put_tile_z(ptTex
, mapTex
, pack
.SkipPixels
, pack
.SkipRows
,
1615 pipe
->transfer_unmap(pipe
, ptRead
);
1616 pipe
->transfer_unmap(pipe
, ptTex
);
1619 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1620 * textured quad with that texture.
1622 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1623 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1628 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1630 pipe_resource_reference(&pt
, NULL
);
1631 pipe_sampler_view_reference(&sv
[0], NULL
);
1636 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1638 functions
->DrawPixels
= st_DrawPixels
;
1639 functions
->CopyPixels
= st_CopyPixels
;
1644 st_destroy_drawpix(struct st_context
*st
)
1648 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1649 if (st
->drawpix
.shaders
[i
])
1650 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1653 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1654 if (st
->drawpix
.vert_shaders
[0])
1655 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1656 if (st
->drawpix
.vert_shaders
[1])
1657 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);