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
:
399 case GL_UNSIGNED_SHORT_4_4_4_4
:
400 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
403 case GL_UNSIGNED_SHORT_5_6_5
:
404 case GL_UNSIGNED_SHORT_5_6_5_REV
:
407 case GL_UNSIGNED_SHORT_5_5_5_1
:
408 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
411 case GL_UNSIGNED_INT_10_10_10_2
:
412 case GL_UNSIGNED_INT_2_10_10_10_REV
:
415 case GL_UNSIGNED_SHORT
:
416 case GL_UNSIGNED_INT
:
421 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
426 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
428 case GL_HALF_FLOAT_ARB
:
430 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
431 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
436 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
437 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
439 case GL_UNSIGNED_INT_5_9_9_9_REV
:
440 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
443 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
444 assert(ctx
->Extensions
.EXT_packed_float
);
445 return GL_R11F_G11F_B10F
;
453 * Create a temporary texture to hold an image of the given size.
454 * If width, height are not POT and the driver only handles POT textures,
455 * allocate the next larger size of texture that is POT.
457 static struct pipe_resource
*
458 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
459 enum pipe_format texFormat
)
461 struct pipe_resource
*pt
;
463 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
464 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
471 * Make texture containing an image for glDrawPixels image.
472 * If 'pixels' is NULL, leave the texture image data undefined.
474 static struct pipe_resource
*
475 make_texture(struct st_context
*st
,
476 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
477 const struct gl_pixelstore_attrib
*unpack
,
478 const GLvoid
*pixels
)
480 struct gl_context
*ctx
= st
->ctx
;
481 struct pipe_context
*pipe
= st
->pipe
;
483 struct pipe_resource
*pt
;
484 enum pipe_format pipeFormat
;
485 GLenum baseInternalFormat
, intFormat
;
487 intFormat
= internal_format(ctx
, format
, type
);
488 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
490 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
491 format
, type
, GL_FALSE
);
494 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
497 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
501 /* alloc temporary texture */
502 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
504 _mesa_unmap_pbo_source(ctx
, unpack
);
509 struct pipe_transfer
*transfer
;
512 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
514 /* we'll do pixel transfer in a fragment shader */
515 ctx
->_ImageTransferState
= 0x0;
517 /* map texture transfer */
518 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
519 PIPE_TRANSFER_WRITE
, 0, 0,
520 width
, height
, &transfer
);
523 /* Put image into texture transfer.
524 * Note that the image is actually going to be upside down in
525 * the texture. We deal with that with texcoords.
527 success
= _mesa_texstore(ctx
, 2, /* dims */
528 baseInternalFormat
, /* baseInternalFormat */
529 mformat
, /* gl_format */
530 transfer
->stride
, /* dstRowStride, bytes */
531 &dest
, /* destSlices */
532 width
, height
, 1, /* size */
533 format
, type
, /* src format/type */
534 pixels
, /* data source */
538 pipe_transfer_unmap(pipe
, transfer
);
543 ctx
->_ImageTransferState
= imageTransferStateSave
;
546 _mesa_unmap_pbo_source(ctx
, unpack
);
553 * Draw quad with texcoords and optional color.
554 * Coords are gallium window coords with y=0=top.
555 * \param color may be null
556 * \param invertTex if true, flip texcoords vertically
559 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
560 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
561 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
563 struct st_context
*st
= st_context(ctx
);
564 struct pipe_context
*pipe
= st
->pipe
;
565 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
566 struct pipe_resource
*buf
= NULL
;
569 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
, &buf
,
575 /* setup vertex data */
577 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
578 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
579 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
580 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
581 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
582 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
583 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
584 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
585 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
586 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
590 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
591 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
594 verts
[1][0][0] = clip_x1
;
595 verts
[1][0][1] = clip_y0
;
598 verts
[2][0][0] = clip_x1
;
599 verts
[2][0][1] = clip_y1
;
602 verts
[3][0][0] = clip_x0
;
603 verts
[3][0][1] = clip_y1
;
605 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
606 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
607 verts
[1][1][0] = sRight
;
608 verts
[1][1][1] = tTop
;
609 verts
[2][1][0] = sRight
;
610 verts
[2][1][1] = tBot
;
611 verts
[3][1][0] = sLeft
;
612 verts
[3][1][1] = tBot
;
614 /* same for all verts: */
616 for (i
= 0; i
< 4; i
++) {
617 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
618 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
619 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
620 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
621 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
622 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
623 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
624 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
628 for (i
= 0; i
< 4; i
++) {
629 verts
[i
][0][2] = z
; /*Z*/
630 verts
[i
][0][3] = 1.0f
; /*W*/
631 verts
[i
][1][2] = 0.0f
; /*R*/
632 verts
[i
][1][3] = 1.0f
; /*Q*/
637 u_upload_unmap(st
->uploader
);
638 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, offset
,
641 3); /* attribs/vert */
642 pipe_resource_reference(&buf
, NULL
);
648 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
649 GLsizei width
, GLsizei height
,
650 GLfloat zoomX
, GLfloat zoomY
,
651 struct pipe_sampler_view
**sv
,
652 int num_sampler_view
,
655 const GLfloat
*color
,
657 GLboolean write_depth
, GLboolean write_stencil
)
659 struct st_context
*st
= st_context(ctx
);
660 struct pipe_context
*pipe
= st
->pipe
;
661 struct cso_context
*cso
= st
->cso_context
;
662 GLfloat x0
, y0
, x1
, y1
;
664 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
667 /* XXX if DrawPixels image is larger than max texture size, break
670 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
671 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
672 assert(width
<= maxSize
);
673 assert(height
<= maxSize
);
675 cso_save_rasterizer(cso
);
676 cso_save_viewport(cso
);
677 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
678 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
679 cso_save_fragment_shader(cso
);
680 cso_save_stream_outputs(cso
);
681 cso_save_vertex_shader(cso
);
682 cso_save_geometry_shader(cso
);
683 cso_save_vertex_elements(cso
);
684 cso_save_vertex_buffers(cso
);
686 cso_save_depth_stencil_alpha(cso
);
690 /* rasterizer state: just scissor */
692 struct pipe_rasterizer_state rasterizer
;
693 memset(&rasterizer
, 0, sizeof(rasterizer
));
694 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
695 ctx
->Color
._ClampFragmentColor
&&
696 !ctx
->DrawBuffer
->_IntegerColor
;
697 rasterizer
.gl_rasterization_rules
= 1;
698 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
699 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
700 cso_set_rasterizer(cso
, &rasterizer
);
704 /* Stencil writing bypasses the normal fragment pipeline to
705 * disable color writing and set stencil test to always pass.
707 struct pipe_depth_stencil_alpha_state dsa
;
708 struct pipe_blend_state blend
;
711 memset(&dsa
, 0, sizeof(dsa
));
712 dsa
.stencil
[0].enabled
= 1;
713 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
714 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
715 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
717 /* writing depth+stencil: depth test always passes */
718 dsa
.depth
.enabled
= 1;
719 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
720 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
722 cso_set_depth_stencil_alpha(cso
, &dsa
);
724 /* blend (colormask) */
725 memset(&blend
, 0, sizeof(blend
));
726 cso_set_blend(cso
, &blend
);
729 /* fragment shader state: TEX lookup program */
730 cso_set_fragment_shader_handle(cso
, driver_fp
);
732 /* vertex shader state: position + texcoord pass-through */
733 cso_set_vertex_shader_handle(cso
, driver_vp
);
735 /* geometry shader state: disabled */
736 cso_set_geometry_shader_handle(cso
, NULL
);
738 /* texture sampling state: */
740 struct pipe_sampler_state sampler
;
741 memset(&sampler
, 0, sizeof(sampler
));
742 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
743 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
744 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
745 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
746 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
747 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
748 sampler
.normalized_coords
= normalized
;
750 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 0, &sampler
);
751 if (num_sampler_view
> 1) {
752 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler
);
754 cso_single_sampler_done(cso
, PIPE_SHADER_FRAGMENT
);
757 /* viewport state: viewport matching window dims */
759 const float w
= (float) ctx
->DrawBuffer
->Width
;
760 const float h
= (float) ctx
->DrawBuffer
->Height
;
761 struct pipe_viewport_state vp
;
762 vp
.scale
[0] = 0.5f
* w
;
763 vp
.scale
[1] = -0.5f
* h
;
766 vp
.translate
[0] = 0.5f
* w
;
767 vp
.translate
[1] = 0.5f
* h
;
768 vp
.translate
[2] = 0.5f
;
769 vp
.translate
[3] = 0.0f
;
770 cso_set_viewport(cso
, &vp
);
773 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
774 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
777 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
779 /* Compute Gallium window coords (y=0=top) with pixel zoom.
780 * Recall that these coords are transformed by the current
781 * vertex shader and viewport transformation.
783 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
784 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
785 invertTex
= !invertTex
;
789 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
791 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
793 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
796 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
797 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
798 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
801 cso_restore_rasterizer(cso
);
802 cso_restore_viewport(cso
);
803 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
804 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
805 cso_restore_fragment_shader(cso
);
806 cso_restore_vertex_shader(cso
);
807 cso_restore_geometry_shader(cso
);
808 cso_restore_vertex_elements(cso
);
809 cso_restore_vertex_buffers(cso
);
810 cso_restore_stream_outputs(cso
);
812 cso_restore_depth_stencil_alpha(cso
);
813 cso_restore_blend(cso
);
819 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
820 * can't use a fragment shader to write stencil values.
823 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
824 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
825 const struct gl_pixelstore_attrib
*unpack
,
826 const GLvoid
*pixels
)
828 struct st_context
*st
= st_context(ctx
);
829 struct pipe_context
*pipe
= st
->pipe
;
830 struct st_renderbuffer
*strb
;
831 enum pipe_transfer_usage usage
;
832 struct pipe_transfer
*pt
;
833 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
835 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
840 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
842 /* totally clipped */
847 strb
= st_renderbuffer(ctx
->DrawBuffer
->
848 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
850 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
851 y
= ctx
->DrawBuffer
->Height
- y
- height
;
854 if (format
== GL_STENCIL_INDEX
&&
855 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
856 /* writing stencil to a combined depth+stencil buffer */
857 usage
= PIPE_TRANSFER_READ_WRITE
;
860 usage
= PIPE_TRANSFER_WRITE
;
863 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
864 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
868 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
871 sValues
= malloc(width
* sizeof(GLubyte
));
872 zValues
= malloc(width
* sizeof(GLuint
));
874 if (sValues
&& zValues
) {
876 for (row
= 0; row
< height
; row
++) {
877 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
878 GLenum destType
= GL_UNSIGNED_BYTE
;
879 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
883 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
884 type
, source
, &clippedUnpack
,
885 ctx
->_ImageTransferState
);
887 if (format
== GL_DEPTH_STENCIL
) {
889 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
890 GL_FLOAT
: GL_UNSIGNED_INT
;
892 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
893 (1 << 24) - 1, type
, source
,
898 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
899 "zoom not complete");
905 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
906 spanY
= height
- row
- 1;
912 /* now pack the stencil (and Z) values in the dest format */
913 switch (pt
->resource
->format
) {
914 case PIPE_FORMAT_S8_UINT
:
916 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
917 assert(usage
== PIPE_TRANSFER_WRITE
);
918 memcpy(dest
, sValues
, width
);
921 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
922 if (format
== GL_DEPTH_STENCIL
) {
923 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
925 assert(usage
== PIPE_TRANSFER_WRITE
);
926 for (k
= 0; k
< width
; k
++) {
927 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
931 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
933 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
934 for (k
= 0; k
< width
; k
++) {
935 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
939 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
940 if (format
== GL_DEPTH_STENCIL
) {
941 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
943 assert(usage
== PIPE_TRANSFER_WRITE
);
944 for (k
= 0; k
< width
; k
++) {
945 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
949 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
951 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
952 for (k
= 0; k
< width
; k
++) {
953 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
957 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
958 if (format
== GL_DEPTH_STENCIL
) {
959 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
960 GLfloat
*destf
= (GLfloat
*)dest
;
962 assert(usage
== PIPE_TRANSFER_WRITE
);
963 for (k
= 0; k
< width
; k
++) {
964 destf
[k
*2] = zValuesFloat
[k
];
965 dest
[k
*2+1] = sValues
[k
] & 0xff;
969 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
971 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
972 for (k
= 0; k
< width
; k
++) {
973 dest
[k
*2+1] = sValues
[k
] & 0xff;
984 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
990 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
992 /* unmap the stencil buffer */
993 pipe_transfer_unmap(pipe
, pt
);
998 * Get fragment program variant for a glDrawPixels or glCopyPixels
999 * command for RGBA data.
1001 static struct st_fp_variant
*
1002 get_color_fp_variant(struct st_context
*st
)
1004 struct gl_context
*ctx
= st
->ctx
;
1005 struct st_fp_variant_key key
;
1006 struct st_fp_variant
*fpv
;
1008 memset(&key
, 0, sizeof(key
));
1012 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1013 ctx
->Pixel
.RedScale
!= 1.0 ||
1014 ctx
->Pixel
.GreenBias
!= 0.0 ||
1015 ctx
->Pixel
.GreenScale
!= 1.0 ||
1016 ctx
->Pixel
.BlueBias
!= 0.0 ||
1017 ctx
->Pixel
.BlueScale
!= 1.0 ||
1018 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1019 ctx
->Pixel
.AlphaScale
!= 1.0);
1020 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1021 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1022 st
->ctx
->Color
._ClampFragmentColor
&&
1023 !st
->ctx
->DrawBuffer
->_IntegerColor
;
1025 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1032 * Get fragment program variant for a glDrawPixels or glCopyPixels
1033 * command for depth/stencil data.
1035 static struct st_fp_variant
*
1036 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1037 GLboolean write_stencil
)
1039 struct st_fp_variant_key key
;
1040 struct st_fp_variant
*fpv
;
1042 memset(&key
, 0, sizeof(key
));
1046 key
.drawpixels_z
= write_depth
;
1047 key
.drawpixels_stencil
= write_stencil
;
1049 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1056 * Clamp glDrawPixels width and height to the maximum texture size.
1059 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1060 struct gl_pixelstore_attrib
*unpack
)
1062 const unsigned maxSize
=
1063 1 << (pipe
->screen
->get_param(pipe
->screen
,
1064 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1066 if (*width
> maxSize
) {
1067 if (unpack
->RowLength
== 0)
1068 unpack
->RowLength
= *width
;
1071 if (*height
> maxSize
) {
1078 * Called via ctx->Driver.DrawPixels()
1081 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1082 GLsizei width
, GLsizei height
,
1083 GLenum format
, GLenum type
,
1084 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1086 void *driver_vp
, *driver_fp
;
1087 struct st_context
*st
= st_context(ctx
);
1088 const GLfloat
*color
;
1089 struct pipe_context
*pipe
= st
->pipe
;
1090 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1091 struct pipe_sampler_view
*sv
[2];
1092 int num_sampler_view
= 1;
1093 struct st_fp_variant
*fpv
;
1094 struct gl_pixelstore_attrib clippedUnpack
;
1096 /* Mesa state should be up to date by now */
1097 assert(ctx
->NewState
== 0x0);
1099 st_validate_state(st
);
1101 /* Limit the size of the glDrawPixels to the max texture size.
1102 * Strictly speaking, that's not correct but since we don't handle
1103 * larger images yet, this is better than crashing.
1105 clippedUnpack
= *unpack
;
1106 unpack
= &clippedUnpack
;
1107 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1109 if (format
== GL_DEPTH_STENCIL
)
1110 write_stencil
= write_depth
= GL_TRUE
;
1111 else if (format
== GL_STENCIL_INDEX
)
1112 write_stencil
= GL_TRUE
;
1113 else if (format
== GL_DEPTH_COMPONENT
)
1114 write_depth
= GL_TRUE
;
1116 if (write_stencil
&&
1117 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1118 /* software fallback */
1119 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1125 * Get vertex/fragment shaders
1127 if (write_depth
|| write_stencil
) {
1128 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1130 driver_fp
= fpv
->driver_shader
;
1132 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1134 color
= ctx
->Current
.RasterColor
;
1137 fpv
= get_color_fp_variant(st
);
1139 driver_fp
= fpv
->driver_shader
;
1141 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1144 if (st
->pixel_xfer
.pixelmap_enabled
) {
1145 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1150 /* update fragment program constants */
1151 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1153 /* draw with textured quad */
1155 struct pipe_resource
*pt
1156 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1158 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1161 /* Create a second sampler view to read stencil.
1162 * The stencil is written using the shader stencil export
1164 if (write_stencil
) {
1165 enum pipe_format stencil_format
=
1166 util_format_stencil_only(pt
->format
);
1168 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1173 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1175 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1180 color
, GL_FALSE
, write_depth
, write_stencil
);
1181 pipe_sampler_view_reference(&sv
[0], NULL
);
1182 if (num_sampler_view
> 1)
1183 pipe_sampler_view_reference(&sv
[1], NULL
);
1185 pipe_resource_reference(&pt
, NULL
);
1193 * Software fallback for glCopyPixels(GL_STENCIL).
1196 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1197 GLsizei width
, GLsizei height
,
1198 GLint dstx
, GLint dsty
)
1200 struct st_renderbuffer
*rbDraw
;
1201 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1202 enum pipe_transfer_usage usage
;
1203 struct pipe_transfer
*ptDraw
;
1208 buffer
= malloc(width
* height
* sizeof(ubyte
));
1210 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1214 /* Get the dest renderbuffer */
1215 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1216 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1218 /* this will do stencil pixel transfer ops */
1219 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1220 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1221 &ctx
->DefaultPacking
, buffer
);
1224 /* debug code: dump stencil values */
1226 for (row
= 0; row
< height
; row
++) {
1227 printf("%3d: ", row
);
1228 for (col
= 0; col
< width
; col
++) {
1229 printf("%02x ", buffer
[col
+ row
* width
]);
1235 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1236 usage
= PIPE_TRANSFER_READ_WRITE
;
1238 usage
= PIPE_TRANSFER_WRITE
;
1240 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1241 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1244 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1245 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1247 /* map the stencil buffer */
1248 drawMap
= pipe_transfer_map(pipe
,
1251 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1253 width
, height
, &ptDraw
);
1256 /* XXX PixelZoom not handled yet */
1257 for (i
= 0; i
< height
; i
++) {
1264 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1268 dst
= drawMap
+ y
* ptDraw
->stride
;
1269 src
= buffer
+ i
* width
;
1271 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1276 /* unmap the stencil buffer */
1277 pipe_transfer_unmap(pipe
, ptDraw
);
1282 * Return renderbuffer to use for reading color pixels for glCopyPixels
1284 static struct st_renderbuffer
*
1285 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1287 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1288 struct st_renderbuffer
*strb
=
1289 st_renderbuffer(fb
->_ColorReadBuffer
);
1295 /** Do the src/dest regions overlap? */
1297 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1298 GLsizei width
, GLsizei height
)
1300 if (srcX
+ width
<= dstX
||
1301 dstX
+ width
<= srcX
||
1302 srcY
+ height
<= dstY
||
1303 dstY
+ height
<= srcY
)
1311 * Try to do a glCopyPixels for simple cases with a blit by calling
1312 * pipe->resource_copy_region().
1314 * We can do this when we're copying color pixels (depth/stencil
1315 * eventually) with no pixel zoom, no pixel transfer ops, no
1316 * per-fragment ops, the src/dest regions don't overlap and the
1317 * src/dest pixel formats are the same.
1320 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1321 GLsizei width
, GLsizei height
,
1322 GLint dstx
, GLint dsty
, GLenum type
)
1324 struct st_context
*st
= st_context(ctx
);
1325 struct pipe_context
*pipe
= st
->pipe
;
1326 struct gl_pixelstore_attrib pack
, unpack
;
1327 GLint readX
, readY
, readW
, readH
;
1329 if (type
== GL_COLOR
&&
1330 ctx
->Pixel
.ZoomX
== 1.0 &&
1331 ctx
->Pixel
.ZoomY
== 1.0 &&
1332 ctx
->_ImageTransferState
== 0x0 &&
1333 !ctx
->Color
.BlendEnabled
&&
1334 !ctx
->Color
.AlphaEnabled
&&
1336 !ctx
->Fog
.Enabled
&&
1337 !ctx
->Stencil
.Enabled
&&
1338 !ctx
->FragmentProgram
.Enabled
&&
1339 !ctx
->VertexProgram
.Enabled
&&
1340 !ctx
->Shader
.CurrentFragmentProgram
&&
1341 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1342 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1343 !ctx
->Query
.CondRenderQuery
) {
1344 struct st_renderbuffer
*rbRead
, *rbDraw
;
1348 * Clip the read region against the src buffer bounds.
1349 * We'll still allocate a temporary buffer/texture for the original
1350 * src region size but we'll only read the region which is on-screen.
1351 * This may mean that we draw garbage pixels into the dest region, but
1358 pack
= ctx
->DefaultPacking
;
1359 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1360 return GL_TRUE
; /* all done */
1362 /* clip against dest buffer bounds and scissor box */
1363 drawX
= dstx
+ pack
.SkipPixels
;
1364 drawY
= dsty
+ pack
.SkipRows
;
1366 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1367 return GL_TRUE
; /* all done */
1369 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1370 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1372 rbRead
= st_get_color_read_renderbuffer(ctx
);
1373 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1375 if ((rbRead
!= rbDraw
||
1376 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1377 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1378 struct pipe_box srcBox
;
1380 /* flip src/dst position if needed */
1381 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1382 /* both buffers will have the same orientation */
1383 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1384 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1387 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1389 pipe
->resource_copy_region(pipe
,
1391 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1393 rbRead
->rtt_level
, &srcBox
);
1403 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1404 GLsizei width
, GLsizei height
,
1405 GLint dstx
, GLint dsty
, GLenum type
)
1407 struct st_context
*st
= st_context(ctx
);
1408 struct pipe_context
*pipe
= st
->pipe
;
1409 struct pipe_screen
*screen
= pipe
->screen
;
1410 struct st_renderbuffer
*rbRead
;
1411 void *driver_vp
, *driver_fp
;
1412 struct pipe_resource
*pt
;
1413 struct pipe_sampler_view
*sv
[2];
1414 int num_sampler_view
= 1;
1416 enum pipe_format srcFormat
, texFormat
;
1417 GLboolean invertTex
= GL_FALSE
;
1418 GLint readX
, readY
, readW
, readH
;
1419 GLuint sample_count
;
1420 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1421 struct st_fp_variant
*fpv
;
1423 st_validate_state(st
);
1425 if (type
== GL_DEPTH_STENCIL
) {
1426 /* XXX make this more efficient */
1427 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1428 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1432 if (type
== GL_STENCIL
) {
1433 /* can't use texturing to do stencil */
1434 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1438 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1442 * The subsequent code implements glCopyPixels by copying the source
1443 * pixels into a temporary texture that's then applied to a textured quad.
1444 * When we draw the textured quad, all the usual per-fragment operations
1450 * Get vertex/fragment shaders
1452 if (type
== GL_COLOR
) {
1453 rbRead
= st_get_color_read_renderbuffer(ctx
);
1456 fpv
= get_color_fp_variant(st
);
1457 driver_fp
= fpv
->driver_shader
;
1459 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1461 if (st
->pixel_xfer
.pixelmap_enabled
) {
1462 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1467 assert(type
== GL_DEPTH
);
1468 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1469 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1470 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1472 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1473 driver_fp
= fpv
->driver_shader
;
1475 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1478 /* update fragment program constants */
1479 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1481 sample_count
= rbRead
->texture
->nr_samples
;
1482 /* I believe this would be legal, presumably would need to do a resolve
1483 for color, and for depth/stencil spec says to just use one of the
1484 depth/stencil samples per pixel? Need some transfer clarifications. */
1485 assert(sample_count
< 2);
1487 srcFormat
= rbRead
->texture
->format
;
1489 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1491 PIPE_BIND_SAMPLER_VIEW
)) {
1492 texFormat
= srcFormat
;
1495 /* srcFormat can't be used as a texture format */
1496 if (type
== GL_DEPTH
) {
1497 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1498 GL_NONE
, GL_NONE
, st
->internal_target
,
1499 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1500 assert(texFormat
!= PIPE_FORMAT_NONE
);
1503 /* default color format */
1504 texFormat
= st_choose_format(screen
, GL_RGBA
,
1505 GL_NONE
, GL_NONE
, st
->internal_target
,
1506 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1507 assert(texFormat
!= PIPE_FORMAT_NONE
);
1511 /* Invert src region if needed */
1512 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1513 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1514 invertTex
= !invertTex
;
1517 /* Clip the read region against the src buffer bounds.
1518 * We'll still allocate a temporary buffer/texture for the original
1519 * src region size but we'll only read the region which is on-screen.
1520 * This may mean that we draw garbage pixels into the dest region, but
1527 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1528 /* The source region is completely out of bounds. Do nothing.
1529 * The GL spec says "Results of copies from outside the window,
1530 * or from regions of the window that are not exposed, are
1531 * hardware dependent and undefined."
1536 readW
= MAX2(0, readW
);
1537 readH
= MAX2(0, readH
);
1539 /* alloc temporary texture */
1540 pt
= alloc_texture(st
, width
, height
, texFormat
);
1544 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1546 pipe_resource_reference(&pt
, NULL
);
1550 /* Make temporary texture which is a copy of the src region.
1552 if (srcFormat
== texFormat
) {
1553 struct pipe_box src_box
;
1554 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1555 /* copy source framebuffer surface into mipmap/texture */
1556 pipe
->resource_copy_region(pipe
,
1559 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1560 rbRead
->texture
, /* src tex */
1561 rbRead
->rtt_level
, /* src lvl */
1566 /* CPU-based fallback/conversion */
1567 struct pipe_transfer
*ptRead
;
1569 pipe_transfer_map(st
->pipe
, rbRead
->texture
,
1571 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1573 readX
, readY
, readW
, readH
, &ptRead
);
1574 struct pipe_transfer
*ptTex
;
1576 enum pipe_transfer_usage transfer_usage
;
1578 if (ST_DEBUG
& DEBUG_FALLBACK
)
1579 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1581 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1582 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1584 transfer_usage
= PIPE_TRANSFER_WRITE
;
1586 mapTex
= pipe_transfer_map(st
->pipe
, pt
, 0, 0, transfer_usage
,
1587 0, 0, width
, height
, &ptTex
);
1589 /* copy image from ptRead surface to ptTex surface */
1590 if (type
== GL_COLOR
) {
1591 /* alternate path using get/put_tile() */
1592 GLfloat
*buf
= malloc(width
* height
* 4 * sizeof(GLfloat
));
1593 enum pipe_format readFormat
, drawFormat
;
1594 readFormat
= util_format_linear(rbRead
->texture
->format
);
1595 drawFormat
= util_format_linear(pt
->format
);
1596 pipe_get_tile_rgba_format(ptRead
, mapRead
, 0, 0, readW
, readH
,
1598 pipe_put_tile_rgba_format(ptTex
, mapTex
, pack
.SkipPixels
,
1600 readW
, readH
, drawFormat
, buf
);
1605 GLuint
*buf
= malloc(width
* height
* sizeof(GLuint
));
1606 pipe_get_tile_z(ptRead
, mapRead
, 0, 0, readW
, readH
, buf
);
1607 pipe_put_tile_z(ptTex
, mapTex
, pack
.SkipPixels
, pack
.SkipRows
,
1612 pipe
->transfer_unmap(pipe
, ptRead
);
1613 pipe
->transfer_unmap(pipe
, ptTex
);
1616 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1617 * textured quad with that texture.
1619 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1620 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1625 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1627 pipe_resource_reference(&pt
, NULL
);
1628 pipe_sampler_view_reference(&sv
[0], NULL
);
1633 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1635 functions
->DrawPixels
= st_DrawPixels
;
1636 functions
->CopyPixels
= st_CopyPixels
;
1641 st_destroy_drawpix(struct st_context
*st
)
1645 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1646 if (st
->drawpix
.shaders
[i
])
1647 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1650 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1651 if (st
->drawpix
.vert_shaders
[0])
1652 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1653 if (st
->drawpix
.vert_shaders
[1])
1654 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);