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 /* Choose a pixel format for the temp texture which will hold the
495 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
496 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
498 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
499 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
501 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
505 /* alloc temporary texture */
506 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
508 _mesa_unmap_pbo_source(ctx
, unpack
);
513 struct pipe_transfer
*transfer
;
516 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
518 /* we'll do pixel transfer in a fragment shader */
519 ctx
->_ImageTransferState
= 0x0;
521 /* map texture transfer */
522 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
523 PIPE_TRANSFER_WRITE
, 0, 0,
524 width
, height
, &transfer
);
527 /* Put image into texture transfer.
528 * Note that the image is actually going to be upside down in
529 * the texture. We deal with that with texcoords.
531 success
= _mesa_texstore(ctx
, 2, /* dims */
532 baseInternalFormat
, /* baseInternalFormat */
533 mformat
, /* gl_format */
534 transfer
->stride
, /* dstRowStride, bytes */
535 &dest
, /* destSlices */
536 width
, height
, 1, /* size */
537 format
, type
, /* src format/type */
538 pixels
, /* data source */
542 pipe_transfer_unmap(pipe
, transfer
);
547 ctx
->_ImageTransferState
= imageTransferStateSave
;
550 _mesa_unmap_pbo_source(ctx
, unpack
);
557 * Draw quad with texcoords and optional color.
558 * Coords are gallium window coords with y=0=top.
559 * \param color may be null
560 * \param invertTex if true, flip texcoords vertically
563 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
564 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
565 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
567 struct st_context
*st
= st_context(ctx
);
568 struct pipe_context
*pipe
= st
->pipe
;
569 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
570 struct pipe_resource
*buf
= NULL
;
573 if (u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
574 &buf
, (void **) &verts
) != PIPE_OK
) {
578 /* setup vertex data */
580 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
581 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
582 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
583 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
584 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
585 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
586 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
587 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
588 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
589 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
593 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
594 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
597 verts
[1][0][0] = clip_x1
;
598 verts
[1][0][1] = clip_y0
;
601 verts
[2][0][0] = clip_x1
;
602 verts
[2][0][1] = clip_y1
;
605 verts
[3][0][0] = clip_x0
;
606 verts
[3][0][1] = clip_y1
;
608 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
609 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
610 verts
[1][1][0] = sRight
;
611 verts
[1][1][1] = tTop
;
612 verts
[2][1][0] = sRight
;
613 verts
[2][1][1] = tBot
;
614 verts
[3][1][0] = sLeft
;
615 verts
[3][1][1] = tBot
;
617 /* same for all verts: */
619 for (i
= 0; i
< 4; i
++) {
620 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
621 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
622 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
623 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
624 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
625 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
626 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
627 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
631 for (i
= 0; i
< 4; i
++) {
632 verts
[i
][0][2] = z
; /*Z*/
633 verts
[i
][0][3] = 1.0f
; /*W*/
634 verts
[i
][1][2] = 0.0f
; /*R*/
635 verts
[i
][1][3] = 1.0f
; /*Q*/
640 u_upload_unmap(st
->uploader
);
641 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
642 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
646 3); /* attribs/vert */
647 pipe_resource_reference(&buf
, NULL
);
653 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
654 GLsizei width
, GLsizei height
,
655 GLfloat zoomX
, GLfloat zoomY
,
656 struct pipe_sampler_view
**sv
,
657 int num_sampler_view
,
660 const GLfloat
*color
,
662 GLboolean write_depth
, GLboolean write_stencil
)
664 struct st_context
*st
= st_context(ctx
);
665 struct pipe_context
*pipe
= st
->pipe
;
666 struct cso_context
*cso
= st
->cso_context
;
667 GLfloat x0
, y0
, x1
, y1
;
669 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
672 /* XXX if DrawPixels image is larger than max texture size, break
675 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
676 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
677 assert(width
<= maxSize
);
678 assert(height
<= maxSize
);
680 cso_save_rasterizer(cso
);
681 cso_save_viewport(cso
);
682 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
683 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
684 cso_save_fragment_shader(cso
);
685 cso_save_stream_outputs(cso
);
686 cso_save_vertex_shader(cso
);
687 cso_save_geometry_shader(cso
);
688 cso_save_vertex_elements(cso
);
689 cso_save_aux_vertex_buffer_slot(cso
);
691 cso_save_depth_stencil_alpha(cso
);
695 /* rasterizer state: just scissor */
697 struct pipe_rasterizer_state rasterizer
;
698 memset(&rasterizer
, 0, sizeof(rasterizer
));
699 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
700 ctx
->Color
._ClampFragmentColor
&&
701 !ctx
->DrawBuffer
->_IntegerColor
;
702 rasterizer
.gl_rasterization_rules
= 1;
703 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
704 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
705 cso_set_rasterizer(cso
, &rasterizer
);
709 /* Stencil writing bypasses the normal fragment pipeline to
710 * disable color writing and set stencil test to always pass.
712 struct pipe_depth_stencil_alpha_state dsa
;
713 struct pipe_blend_state blend
;
716 memset(&dsa
, 0, sizeof(dsa
));
717 dsa
.stencil
[0].enabled
= 1;
718 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
719 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
720 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
722 /* writing depth+stencil: depth test always passes */
723 dsa
.depth
.enabled
= 1;
724 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
725 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
727 cso_set_depth_stencil_alpha(cso
, &dsa
);
729 /* blend (colormask) */
730 memset(&blend
, 0, sizeof(blend
));
731 cso_set_blend(cso
, &blend
);
734 /* fragment shader state: TEX lookup program */
735 cso_set_fragment_shader_handle(cso
, driver_fp
);
737 /* vertex shader state: position + texcoord pass-through */
738 cso_set_vertex_shader_handle(cso
, driver_vp
);
740 /* geometry shader state: disabled */
741 cso_set_geometry_shader_handle(cso
, NULL
);
743 /* texture sampling state: */
745 struct pipe_sampler_state sampler
;
746 memset(&sampler
, 0, sizeof(sampler
));
747 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
748 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
749 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
750 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
751 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
752 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
753 sampler
.normalized_coords
= normalized
;
755 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 0, &sampler
);
756 if (num_sampler_view
> 1) {
757 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler
);
759 cso_single_sampler_done(cso
, PIPE_SHADER_FRAGMENT
);
762 /* viewport state: viewport matching window dims */
764 const float w
= (float) ctx
->DrawBuffer
->Width
;
765 const float h
= (float) ctx
->DrawBuffer
->Height
;
766 struct pipe_viewport_state vp
;
767 vp
.scale
[0] = 0.5f
* w
;
768 vp
.scale
[1] = -0.5f
* h
;
771 vp
.translate
[0] = 0.5f
* w
;
772 vp
.translate
[1] = 0.5f
* h
;
773 vp
.translate
[2] = 0.5f
;
774 vp
.translate
[3] = 0.0f
;
775 cso_set_viewport(cso
, &vp
);
778 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
779 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
782 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
784 /* Compute Gallium window coords (y=0=top) with pixel zoom.
785 * Recall that these coords are transformed by the current
786 * vertex shader and viewport transformation.
788 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
789 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
790 invertTex
= !invertTex
;
794 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
796 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
798 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
801 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
802 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
803 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
806 cso_restore_rasterizer(cso
);
807 cso_restore_viewport(cso
);
808 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
809 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
810 cso_restore_fragment_shader(cso
);
811 cso_restore_vertex_shader(cso
);
812 cso_restore_geometry_shader(cso
);
813 cso_restore_vertex_elements(cso
);
814 cso_restore_aux_vertex_buffer_slot(cso
);
815 cso_restore_stream_outputs(cso
);
817 cso_restore_depth_stencil_alpha(cso
);
818 cso_restore_blend(cso
);
824 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
825 * can't use a fragment shader to write stencil values.
828 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
829 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
830 const struct gl_pixelstore_attrib
*unpack
,
831 const GLvoid
*pixels
)
833 struct st_context
*st
= st_context(ctx
);
834 struct pipe_context
*pipe
= st
->pipe
;
835 struct st_renderbuffer
*strb
;
836 enum pipe_transfer_usage usage
;
837 struct pipe_transfer
*pt
;
838 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
840 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
845 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
847 /* totally clipped */
852 strb
= st_renderbuffer(ctx
->DrawBuffer
->
853 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
855 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
856 y
= ctx
->DrawBuffer
->Height
- y
- height
;
859 if (format
== GL_STENCIL_INDEX
&&
860 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
861 /* writing stencil to a combined depth+stencil buffer */
862 usage
= PIPE_TRANSFER_READ_WRITE
;
865 usage
= PIPE_TRANSFER_WRITE
;
868 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
869 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
873 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
876 sValues
= malloc(width
* sizeof(GLubyte
));
877 zValues
= malloc(width
* sizeof(GLuint
));
879 if (sValues
&& zValues
) {
881 for (row
= 0; row
< height
; row
++) {
882 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
883 GLenum destType
= GL_UNSIGNED_BYTE
;
884 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
888 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
889 type
, source
, &clippedUnpack
,
890 ctx
->_ImageTransferState
);
892 if (format
== GL_DEPTH_STENCIL
) {
894 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
895 GL_FLOAT
: GL_UNSIGNED_INT
;
897 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
898 (1 << 24) - 1, type
, source
,
903 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
904 "zoom not complete");
910 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
911 spanY
= height
- row
- 1;
917 /* now pack the stencil (and Z) values in the dest format */
918 switch (pt
->resource
->format
) {
919 case PIPE_FORMAT_S8_UINT
:
921 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
922 assert(usage
== PIPE_TRANSFER_WRITE
);
923 memcpy(dest
, sValues
, width
);
926 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
927 if (format
== GL_DEPTH_STENCIL
) {
928 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
930 assert(usage
== PIPE_TRANSFER_WRITE
);
931 for (k
= 0; k
< width
; k
++) {
932 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
936 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
938 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
939 for (k
= 0; k
< width
; k
++) {
940 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
944 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
945 if (format
== GL_DEPTH_STENCIL
) {
946 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
948 assert(usage
== PIPE_TRANSFER_WRITE
);
949 for (k
= 0; k
< width
; k
++) {
950 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
954 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
956 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
957 for (k
= 0; k
< width
; k
++) {
958 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
962 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
963 if (format
== GL_DEPTH_STENCIL
) {
964 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
965 GLfloat
*destf
= (GLfloat
*)dest
;
967 assert(usage
== PIPE_TRANSFER_WRITE
);
968 for (k
= 0; k
< width
; k
++) {
969 destf
[k
*2] = zValuesFloat
[k
];
970 dest
[k
*2+1] = sValues
[k
] & 0xff;
974 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
976 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
977 for (k
= 0; k
< width
; k
++) {
978 dest
[k
*2+1] = sValues
[k
] & 0xff;
989 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
995 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
997 /* unmap the stencil buffer */
998 pipe_transfer_unmap(pipe
, pt
);
1003 * Get fragment program variant for a glDrawPixels or glCopyPixels
1004 * command for RGBA data.
1006 static struct st_fp_variant
*
1007 get_color_fp_variant(struct st_context
*st
)
1009 struct gl_context
*ctx
= st
->ctx
;
1010 struct st_fp_variant_key key
;
1011 struct st_fp_variant
*fpv
;
1013 memset(&key
, 0, sizeof(key
));
1017 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1018 ctx
->Pixel
.RedScale
!= 1.0 ||
1019 ctx
->Pixel
.GreenBias
!= 0.0 ||
1020 ctx
->Pixel
.GreenScale
!= 1.0 ||
1021 ctx
->Pixel
.BlueBias
!= 0.0 ||
1022 ctx
->Pixel
.BlueScale
!= 1.0 ||
1023 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1024 ctx
->Pixel
.AlphaScale
!= 1.0);
1025 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1026 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1027 st
->ctx
->Color
._ClampFragmentColor
&&
1028 !st
->ctx
->DrawBuffer
->_IntegerColor
;
1030 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1037 * Get fragment program variant for a glDrawPixels or glCopyPixels
1038 * command for depth/stencil data.
1040 static struct st_fp_variant
*
1041 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1042 GLboolean write_stencil
)
1044 struct st_fp_variant_key key
;
1045 struct st_fp_variant
*fpv
;
1047 memset(&key
, 0, sizeof(key
));
1051 key
.drawpixels_z
= write_depth
;
1052 key
.drawpixels_stencil
= write_stencil
;
1054 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1061 * Clamp glDrawPixels width and height to the maximum texture size.
1064 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1065 struct gl_pixelstore_attrib
*unpack
)
1068 1 << (pipe
->screen
->get_param(pipe
->screen
,
1069 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1071 if (*width
> maxSize
) {
1072 if (unpack
->RowLength
== 0)
1073 unpack
->RowLength
= *width
;
1076 if (*height
> maxSize
) {
1083 * Called via ctx->Driver.DrawPixels()
1086 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1087 GLsizei width
, GLsizei height
,
1088 GLenum format
, GLenum type
,
1089 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1091 void *driver_vp
, *driver_fp
;
1092 struct st_context
*st
= st_context(ctx
);
1093 const GLfloat
*color
;
1094 struct pipe_context
*pipe
= st
->pipe
;
1095 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1096 struct pipe_sampler_view
*sv
[2];
1097 int num_sampler_view
= 1;
1098 struct st_fp_variant
*fpv
;
1099 struct gl_pixelstore_attrib clippedUnpack
;
1101 /* Mesa state should be up to date by now */
1102 assert(ctx
->NewState
== 0x0);
1104 st_validate_state(st
);
1106 /* Limit the size of the glDrawPixels to the max texture size.
1107 * Strictly speaking, that's not correct but since we don't handle
1108 * larger images yet, this is better than crashing.
1110 clippedUnpack
= *unpack
;
1111 unpack
= &clippedUnpack
;
1112 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1114 if (format
== GL_DEPTH_STENCIL
)
1115 write_stencil
= write_depth
= GL_TRUE
;
1116 else if (format
== GL_STENCIL_INDEX
)
1117 write_stencil
= GL_TRUE
;
1118 else if (format
== GL_DEPTH_COMPONENT
)
1119 write_depth
= GL_TRUE
;
1121 if (write_stencil
&&
1122 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1123 /* software fallback */
1124 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1130 * Get vertex/fragment shaders
1132 if (write_depth
|| write_stencil
) {
1133 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1135 driver_fp
= fpv
->driver_shader
;
1137 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1139 color
= ctx
->Current
.RasterColor
;
1142 fpv
= get_color_fp_variant(st
);
1144 driver_fp
= fpv
->driver_shader
;
1146 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1149 if (st
->pixel_xfer
.pixelmap_enabled
) {
1150 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1155 /* update fragment program constants */
1156 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1158 /* draw with textured quad */
1160 struct pipe_resource
*pt
1161 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1163 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1166 /* Create a second sampler view to read stencil.
1167 * The stencil is written using the shader stencil export
1169 if (write_stencil
) {
1170 enum pipe_format stencil_format
=
1171 util_format_stencil_only(pt
->format
);
1173 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1178 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1180 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1185 color
, GL_FALSE
, write_depth
, write_stencil
);
1186 pipe_sampler_view_reference(&sv
[0], NULL
);
1187 if (num_sampler_view
> 1)
1188 pipe_sampler_view_reference(&sv
[1], NULL
);
1190 pipe_resource_reference(&pt
, NULL
);
1198 * Software fallback for glCopyPixels(GL_STENCIL).
1201 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1202 GLsizei width
, GLsizei height
,
1203 GLint dstx
, GLint dsty
)
1205 struct st_renderbuffer
*rbDraw
;
1206 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1207 enum pipe_transfer_usage usage
;
1208 struct pipe_transfer
*ptDraw
;
1213 buffer
= malloc(width
* height
* sizeof(ubyte
));
1215 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1219 /* Get the dest renderbuffer */
1220 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1221 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1223 /* this will do stencil pixel transfer ops */
1224 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1225 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1226 &ctx
->DefaultPacking
, buffer
);
1229 /* debug code: dump stencil values */
1231 for (row
= 0; row
< height
; row
++) {
1232 printf("%3d: ", row
);
1233 for (col
= 0; col
< width
; col
++) {
1234 printf("%02x ", buffer
[col
+ row
* width
]);
1240 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1241 usage
= PIPE_TRANSFER_READ_WRITE
;
1243 usage
= PIPE_TRANSFER_WRITE
;
1245 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1246 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1249 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1250 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1252 /* map the stencil buffer */
1253 drawMap
= pipe_transfer_map(pipe
,
1256 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1258 width
, height
, &ptDraw
);
1261 /* XXX PixelZoom not handled yet */
1262 for (i
= 0; i
< height
; i
++) {
1269 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1273 dst
= drawMap
+ y
* ptDraw
->stride
;
1274 src
= buffer
+ i
* width
;
1276 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1281 /* unmap the stencil buffer */
1282 pipe_transfer_unmap(pipe
, ptDraw
);
1287 * Return renderbuffer to use for reading color pixels for glCopyPixels
1289 static struct st_renderbuffer
*
1290 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1292 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1293 struct st_renderbuffer
*strb
=
1294 st_renderbuffer(fb
->_ColorReadBuffer
);
1300 /** Do the src/dest regions overlap? */
1302 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1303 GLsizei width
, GLsizei height
)
1305 if (srcX
+ width
<= dstX
||
1306 dstX
+ width
<= srcX
||
1307 srcY
+ height
<= dstY
||
1308 dstY
+ height
<= srcY
)
1316 * Try to do a glCopyPixels for simple cases with a blit by calling
1317 * pipe->resource_copy_region().
1319 * We can do this when we're copying color pixels (depth/stencil
1320 * eventually) with no pixel zoom, no pixel transfer ops, no
1321 * per-fragment ops, the src/dest regions don't overlap and the
1322 * src/dest pixel formats are the same.
1325 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1326 GLsizei width
, GLsizei height
,
1327 GLint dstx
, GLint dsty
, GLenum type
)
1329 struct st_context
*st
= st_context(ctx
);
1330 struct pipe_context
*pipe
= st
->pipe
;
1331 struct gl_pixelstore_attrib pack
, unpack
;
1332 GLint readX
, readY
, readW
, readH
;
1334 if (type
== GL_COLOR
&&
1335 ctx
->Pixel
.ZoomX
== 1.0 &&
1336 ctx
->Pixel
.ZoomY
== 1.0 &&
1337 ctx
->_ImageTransferState
== 0x0 &&
1338 !ctx
->Color
.BlendEnabled
&&
1339 !ctx
->Color
.AlphaEnabled
&&
1341 !ctx
->Fog
.Enabled
&&
1342 !ctx
->Stencil
.Enabled
&&
1343 !ctx
->FragmentProgram
.Enabled
&&
1344 !ctx
->VertexProgram
.Enabled
&&
1345 !ctx
->Shader
.CurrentFragmentProgram
&&
1346 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1347 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1348 !ctx
->Query
.CondRenderQuery
) {
1349 struct st_renderbuffer
*rbRead
, *rbDraw
;
1353 * Clip the read region against the src buffer bounds.
1354 * We'll still allocate a temporary buffer/texture for the original
1355 * src region size but we'll only read the region which is on-screen.
1356 * This may mean that we draw garbage pixels into the dest region, but
1363 pack
= ctx
->DefaultPacking
;
1364 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1365 return GL_TRUE
; /* all done */
1367 /* clip against dest buffer bounds and scissor box */
1368 drawX
= dstx
+ pack
.SkipPixels
;
1369 drawY
= dsty
+ pack
.SkipRows
;
1371 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1372 return GL_TRUE
; /* all done */
1374 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1375 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1377 rbRead
= st_get_color_read_renderbuffer(ctx
);
1378 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1380 if ((rbRead
!= rbDraw
||
1381 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1382 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1383 struct pipe_box srcBox
;
1385 /* flip src/dst position if needed */
1386 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1387 /* both buffers will have the same orientation */
1388 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1389 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1392 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1394 pipe
->resource_copy_region(pipe
,
1396 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1398 rbRead
->rtt_level
, &srcBox
);
1408 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1409 GLsizei width
, GLsizei height
,
1410 GLint dstx
, GLint dsty
, GLenum type
)
1412 struct st_context
*st
= st_context(ctx
);
1413 struct pipe_context
*pipe
= st
->pipe
;
1414 struct pipe_screen
*screen
= pipe
->screen
;
1415 struct st_renderbuffer
*rbRead
;
1416 void *driver_vp
, *driver_fp
;
1417 struct pipe_resource
*pt
;
1418 struct pipe_sampler_view
*sv
[2];
1419 int num_sampler_view
= 1;
1421 enum pipe_format srcFormat
, texFormat
;
1422 GLboolean invertTex
= GL_FALSE
;
1423 GLint readX
, readY
, readW
, readH
;
1424 GLuint sample_count
;
1425 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1426 struct st_fp_variant
*fpv
;
1428 st_validate_state(st
);
1430 if (type
== GL_DEPTH_STENCIL
) {
1431 /* XXX make this more efficient */
1432 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1433 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1437 if (type
== GL_STENCIL
) {
1438 /* can't use texturing to do stencil */
1439 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1443 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1447 * The subsequent code implements glCopyPixels by copying the source
1448 * pixels into a temporary texture that's then applied to a textured quad.
1449 * When we draw the textured quad, all the usual per-fragment operations
1455 * Get vertex/fragment shaders
1457 if (type
== GL_COLOR
) {
1458 rbRead
= st_get_color_read_renderbuffer(ctx
);
1461 fpv
= get_color_fp_variant(st
);
1462 driver_fp
= fpv
->driver_shader
;
1464 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1466 if (st
->pixel_xfer
.pixelmap_enabled
) {
1467 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1472 assert(type
== GL_DEPTH
);
1473 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1474 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1475 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1477 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1478 driver_fp
= fpv
->driver_shader
;
1480 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1483 /* update fragment program constants */
1484 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1486 sample_count
= rbRead
->texture
->nr_samples
;
1487 /* I believe this would be legal, presumably would need to do a resolve
1488 for color, and for depth/stencil spec says to just use one of the
1489 depth/stencil samples per pixel? Need some transfer clarifications. */
1490 assert(sample_count
< 2);
1492 srcFormat
= rbRead
->texture
->format
;
1494 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1496 PIPE_BIND_SAMPLER_VIEW
)) {
1497 texFormat
= srcFormat
;
1500 /* srcFormat can't be used as a texture format */
1501 if (type
== GL_DEPTH
) {
1502 texFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
,
1503 GL_NONE
, GL_NONE
, st
->internal_target
,
1504 sample_count
, PIPE_BIND_DEPTH_STENCIL
,
1506 assert(texFormat
!= PIPE_FORMAT_NONE
);
1509 /* default color format */
1510 texFormat
= st_choose_format(st
, GL_RGBA
,
1511 GL_NONE
, GL_NONE
, st
->internal_target
,
1512 sample_count
, PIPE_BIND_SAMPLER_VIEW
,
1514 assert(texFormat
!= PIPE_FORMAT_NONE
);
1518 /* Invert src region if needed */
1519 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1520 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1521 invertTex
= !invertTex
;
1524 /* Clip the read region against the src buffer bounds.
1525 * We'll still allocate a temporary buffer/texture for the original
1526 * src region size but we'll only read the region which is on-screen.
1527 * This may mean that we draw garbage pixels into the dest region, but
1534 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1535 /* The source region is completely out of bounds. Do nothing.
1536 * The GL spec says "Results of copies from outside the window,
1537 * or from regions of the window that are not exposed, are
1538 * hardware dependent and undefined."
1543 readW
= MAX2(0, readW
);
1544 readH
= MAX2(0, readH
);
1546 /* alloc temporary texture */
1547 pt
= alloc_texture(st
, width
, height
, texFormat
);
1551 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1553 pipe_resource_reference(&pt
, NULL
);
1557 /* Make temporary texture which is a copy of the src region.
1559 if (srcFormat
== texFormat
) {
1560 struct pipe_box src_box
;
1561 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1562 /* copy source framebuffer surface into mipmap/texture */
1563 pipe
->resource_copy_region(pipe
,
1566 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1567 rbRead
->texture
, /* src tex */
1568 rbRead
->rtt_level
, /* src lvl */
1573 /* CPU-based fallback/conversion */
1574 struct pipe_transfer
*ptRead
;
1576 pipe_transfer_map(st
->pipe
, rbRead
->texture
,
1578 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1580 readX
, readY
, readW
, readH
, &ptRead
);
1581 struct pipe_transfer
*ptTex
;
1583 enum pipe_transfer_usage transfer_usage
;
1585 if (ST_DEBUG
& DEBUG_FALLBACK
)
1586 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1588 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1589 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1591 transfer_usage
= PIPE_TRANSFER_WRITE
;
1593 mapTex
= pipe_transfer_map(st
->pipe
, pt
, 0, 0, transfer_usage
,
1594 0, 0, width
, height
, &ptTex
);
1596 /* copy image from ptRead surface to ptTex surface */
1597 if (type
== GL_COLOR
) {
1598 /* alternate path using get/put_tile() */
1599 GLfloat
*buf
= malloc(width
* height
* 4 * sizeof(GLfloat
));
1600 enum pipe_format readFormat
, drawFormat
;
1601 readFormat
= util_format_linear(rbRead
->texture
->format
);
1602 drawFormat
= util_format_linear(pt
->format
);
1603 pipe_get_tile_rgba_format(ptRead
, mapRead
, 0, 0, readW
, readH
,
1605 pipe_put_tile_rgba_format(ptTex
, mapTex
, pack
.SkipPixels
,
1607 readW
, readH
, drawFormat
, buf
);
1612 GLuint
*buf
= malloc(width
* height
* sizeof(GLuint
));
1613 pipe_get_tile_z(ptRead
, mapRead
, 0, 0, readW
, readH
, buf
);
1614 pipe_put_tile_z(ptTex
, mapTex
, pack
.SkipPixels
, pack
.SkipRows
,
1619 pipe
->transfer_unmap(pipe
, ptRead
);
1620 pipe
->transfer_unmap(pipe
, ptTex
);
1623 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1624 * textured quad with that texture.
1626 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1627 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1632 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1634 pipe_resource_reference(&pt
, NULL
);
1635 pipe_sampler_view_reference(&sv
[0], NULL
);
1640 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1642 functions
->DrawPixels
= st_DrawPixels
;
1643 functions
->CopyPixels
= st_CopyPixels
;
1648 st_destroy_drawpix(struct st_context
*st
)
1652 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1653 if (st
->drawpix
.shaders
[i
])
1654 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1657 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1658 if (st
->drawpix
.vert_shaders
[0])
1659 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1660 if (st
->drawpix
.vert_shaders
[1])
1661 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);