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 "program/program.h"
47 #include "program/prog_print.h"
48 #include "program/prog_instruction.h"
51 #include "st_atom_constbuf.h"
52 #include "st_cb_drawpixels.h"
53 #include "st_cb_readpixels.h"
54 #include "st_cb_fbo.h"
55 #include "st_context.h"
57 #include "st_format.h"
58 #include "st_program.h"
59 #include "st_texture.h"
61 #include "pipe/p_context.h"
62 #include "pipe/p_defines.h"
63 #include "tgsi/tgsi_ureg.h"
64 #include "util/u_draw_quad.h"
65 #include "util/u_format.h"
66 #include "util/u_inlines.h"
67 #include "util/u_math.h"
68 #include "util/u_tile.h"
69 #include "util/u_upload_mgr.h"
70 #include "cso_cache/cso_context.h"
76 * Check if the given program is:
77 * 0: MOVE result.color, fragment.color;
81 is_passthrough_program(const struct gl_fragment_program
*prog
)
83 if (prog
->Base
.NumInstructions
== 2) {
84 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
85 if (inst
[0].Opcode
== OPCODE_MOV
&&
86 inst
[1].Opcode
== OPCODE_END
&&
87 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
88 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
89 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
90 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
91 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
92 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
101 * Returns a fragment program which implements the current pixel transfer ops.
103 static struct gl_fragment_program
*
104 get_glsl_pixel_transfer_program(struct st_context
*st
,
105 struct st_fragment_program
*orig
)
107 int pixelMaps
= 0, scaleAndBias
= 0;
108 struct gl_context
*ctx
= st
->ctx
;
109 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
110 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
115 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
116 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
117 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
118 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
122 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
125 /* create the colormap/texture now if not already done */
126 if (!st
->pixel_xfer
.pixelmap_texture
) {
127 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
128 st
->pixel_xfer
.pixelmap_sampler_view
=
129 st_create_texture_sampler_view(st
->pipe
,
130 st
->pixel_xfer
.pixelmap_texture
);
134 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
135 scaleAndBias
, pixelMaps
);
142 * Make fragment shader for glDraw/CopyPixels. This shader is made
143 * by combining the pixel transfer shader with the user-defined shader.
144 * \param fpIn the current/incoming fragment program
145 * \param fpOut returns the combined fragment program
148 st_make_drawpix_fragment_program(struct st_context
*st
,
149 struct gl_fragment_program
*fpIn
,
150 struct gl_fragment_program
**fpOut
)
152 struct gl_program
*newProg
;
153 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
155 if (is_passthrough_program(fpIn
)) {
156 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
157 &st
->pixel_xfer
.program
->Base
);
159 else if (stfp
->glsl_to_tgsi
!= NULL
) {
160 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
165 printf("Base program:\n");
166 _mesa_print_program(&fpIn
->Base
);
167 printf("DrawPix program:\n");
168 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
170 newProg
= _mesa_combine_programs(st
->ctx
,
171 &st
->pixel_xfer
.program
->Base
.Base
,
177 printf("Combined DrawPixels program:\n");
178 _mesa_print_program(newProg
);
179 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
180 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
181 _mesa_print_parameter_list(newProg
->Parameters
);
184 *fpOut
= (struct gl_fragment_program
*) newProg
;
189 * Create fragment program that does a TEX() instruction to get a Z and/or
190 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
191 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
192 * Pass fragment color through as-is.
193 * \return pointer to the gl_fragment program
195 struct gl_fragment_program
*
196 st_make_drawpix_z_stencil_program(struct st_context
*st
,
197 GLboolean write_depth
,
198 GLboolean write_stencil
)
200 struct gl_context
*ctx
= st
->ctx
;
201 struct gl_program
*p
;
202 struct gl_fragment_program
*fp
;
204 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
206 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
208 if (st
->drawpix
.shaders
[shaderIndex
]) {
209 /* already have the proper shader */
210 return st
->drawpix
.shaders
[shaderIndex
];
216 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
220 p
->NumInstructions
= write_depth
? 3 : 1;
221 p
->NumInstructions
+= write_stencil
? 1 : 0;
223 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
224 if (!p
->Instructions
) {
225 ctx
->Driver
.DeleteProgram(ctx
, p
);
228 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
231 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
232 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
233 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
234 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
235 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
236 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
237 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
238 p
->Instructions
[ic
].TexSrcUnit
= 0;
239 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
241 /* MOV result.color, fragment.color; */
242 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
243 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
244 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
245 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
246 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
251 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
252 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
253 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
254 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
255 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
256 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
257 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
258 p
->Instructions
[ic
].TexSrcUnit
= 1;
259 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
264 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
266 assert(ic
== p
->NumInstructions
);
268 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
269 p
->OutputsWritten
= 0;
271 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
272 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_COLOR
);
275 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
277 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
279 p
->SamplersUsed
|= 1 << 1;
281 fp
= (struct gl_fragment_program
*) p
;
283 /* save the new shader */
284 st
->drawpix
.shaders
[shaderIndex
] = fp
;
291 * Create a simple vertex shader that just passes through the
292 * vertex position and texcoord (and optionally, color).
295 make_passthrough_vertex_shader(struct st_context
*st
,
298 if (!st
->drawpix
.vert_shaders
[passColor
]) {
299 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
304 /* MOV result.pos, vertex.pos; */
306 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
307 ureg_DECL_vs_input( ureg
, 0 ));
309 /* MOV result.texcoord0, vertex.attr[1]; */
311 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
312 ureg_DECL_vs_input( ureg
, 1 ));
315 /* MOV result.color0, vertex.attr[2]; */
317 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
318 ureg_DECL_vs_input( ureg
, 2 ));
323 st
->drawpix
.vert_shaders
[passColor
] =
324 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
327 return st
->drawpix
.vert_shaders
[passColor
];
332 * Return a texture internalFormat for drawing/copying an image
333 * of the given format and type.
336 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
339 case GL_DEPTH_COMPONENT
:
341 case GL_UNSIGNED_SHORT
:
342 return GL_DEPTH_COMPONENT16
;
344 case GL_UNSIGNED_INT
:
345 return GL_DEPTH_COMPONENT32
;
348 if (ctx
->Extensions
.ARB_depth_buffer_float
)
349 return GL_DEPTH_COMPONENT32F
;
351 return GL_DEPTH_COMPONENT
;
354 return GL_DEPTH_COMPONENT
;
357 case GL_DEPTH_STENCIL
:
359 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
360 return GL_DEPTH32F_STENCIL8
;
362 case GL_UNSIGNED_INT_24_8
:
364 return GL_DEPTH24_STENCIL8
;
367 case GL_STENCIL_INDEX
:
368 return GL_STENCIL_INDEX
;
371 if (_mesa_is_integer_format(format
)) {
375 case GL_UNSIGNED_BYTE
:
379 case GL_UNSIGNED_SHORT
:
383 case GL_UNSIGNED_INT
:
386 assert(0 && "Unexpected type in internal_format()");
387 return GL_RGBA_INTEGER
;
392 case GL_UNSIGNED_BYTE
:
393 case GL_UNSIGNED_INT_8_8_8_8
:
394 case GL_UNSIGNED_INT_8_8_8_8_REV
:
398 case GL_UNSIGNED_BYTE_3_3_2
:
399 case GL_UNSIGNED_BYTE_2_3_3_REV
:
400 case GL_UNSIGNED_SHORT_4_4_4_4
:
401 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
404 case GL_UNSIGNED_SHORT_5_6_5
:
405 case GL_UNSIGNED_SHORT_5_6_5_REV
:
406 case GL_UNSIGNED_SHORT_5_5_5_1
:
407 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
410 case GL_UNSIGNED_INT_10_10_10_2
:
411 case GL_UNSIGNED_INT_2_10_10_10_REV
:
414 case GL_UNSIGNED_SHORT
:
415 case GL_UNSIGNED_INT
:
420 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
425 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
427 case GL_HALF_FLOAT_ARB
:
429 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
430 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
435 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
436 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
438 case GL_UNSIGNED_INT_5_9_9_9_REV
:
439 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
442 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
443 assert(ctx
->Extensions
.EXT_packed_float
);
444 return GL_R11F_G11F_B10F
;
452 * Create a temporary texture to hold an image of the given size.
453 * If width, height are not POT and the driver only handles POT textures,
454 * allocate the next larger size of texture that is POT.
456 static struct pipe_resource
*
457 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
458 enum pipe_format texFormat
)
460 struct pipe_resource
*pt
;
462 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
463 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
470 * Make texture containing an image for glDrawPixels image.
471 * If 'pixels' is NULL, leave the texture image data undefined.
473 static struct pipe_resource
*
474 make_texture(struct st_context
*st
,
475 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
476 const struct gl_pixelstore_attrib
*unpack
,
477 const GLvoid
*pixels
)
479 struct gl_context
*ctx
= st
->ctx
;
480 struct pipe_context
*pipe
= st
->pipe
;
482 struct pipe_resource
*pt
;
483 enum pipe_format pipeFormat
;
484 GLenum baseInternalFormat
, intFormat
;
486 intFormat
= internal_format(ctx
, format
, type
);
487 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
489 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
490 format
, type
, GL_FALSE
);
493 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
496 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
500 /* alloc temporary texture */
501 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
503 _mesa_unmap_pbo_source(ctx
, unpack
);
508 struct pipe_transfer
*transfer
;
511 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
513 /* we'll do pixel transfer in a fragment shader */
514 ctx
->_ImageTransferState
= 0x0;
516 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
517 PIPE_TRANSFER_WRITE
, 0, 0,
520 /* map texture transfer */
521 dest
= pipe_transfer_map(pipe
, transfer
);
524 /* Put image into texture transfer.
525 * Note that the image is actually going to be upside down in
526 * the texture. We deal with that with texcoords.
528 success
= _mesa_texstore(ctx
, 2, /* dims */
529 baseInternalFormat
, /* baseInternalFormat */
530 mformat
, /* gl_format */
531 transfer
->stride
, /* dstRowStride, bytes */
532 &dest
, /* destSlices */
533 width
, height
, 1, /* size */
534 format
, type
, /* src format/type */
535 pixels
, /* data source */
539 pipe_transfer_unmap(pipe
, transfer
);
540 pipe
->transfer_destroy(pipe
, transfer
);
545 ctx
->_ImageTransferState
= imageTransferStateSave
;
548 _mesa_unmap_pbo_source(ctx
, unpack
);
555 * Draw quad with texcoords and optional color.
556 * Coords are gallium window coords with y=0=top.
557 * \param color may be null
558 * \param invertTex if true, flip texcoords vertically
561 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
562 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
563 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
565 struct st_context
*st
= st_context(ctx
);
566 struct pipe_context
*pipe
= st
->pipe
;
567 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
568 struct pipe_resource
*buf
= NULL
;
571 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
, &buf
,
577 /* setup vertex data */
579 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
580 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
581 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
582 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
583 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
584 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
585 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
586 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
587 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
588 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
592 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
593 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
596 verts
[1][0][0] = clip_x1
;
597 verts
[1][0][1] = clip_y0
;
600 verts
[2][0][0] = clip_x1
;
601 verts
[2][0][1] = clip_y1
;
604 verts
[3][0][0] = clip_x0
;
605 verts
[3][0][1] = clip_y1
;
607 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
608 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
609 verts
[1][1][0] = sRight
;
610 verts
[1][1][1] = tTop
;
611 verts
[2][1][0] = sRight
;
612 verts
[2][1][1] = tBot
;
613 verts
[3][1][0] = sLeft
;
614 verts
[3][1][1] = tBot
;
616 /* same for all verts: */
618 for (i
= 0; i
< 4; i
++) {
619 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
620 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
621 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
622 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
623 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
624 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
625 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
626 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
630 for (i
= 0; i
< 4; i
++) {
631 verts
[i
][0][2] = z
; /*Z*/
632 verts
[i
][0][3] = 1.0f
; /*W*/
633 verts
[i
][1][2] = 0.0f
; /*R*/
634 verts
[i
][1][3] = 1.0f
; /*Q*/
639 u_upload_unmap(st
->uploader
);
640 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, offset
,
643 3); /* attribs/vert */
644 pipe_resource_reference(&buf
, NULL
);
650 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
651 GLsizei width
, GLsizei height
,
652 GLfloat zoomX
, GLfloat zoomY
,
653 struct pipe_sampler_view
**sv
,
654 int num_sampler_view
,
657 const GLfloat
*color
,
659 GLboolean write_depth
, GLboolean write_stencil
)
661 struct st_context
*st
= st_context(ctx
);
662 struct pipe_context
*pipe
= st
->pipe
;
663 struct cso_context
*cso
= st
->cso_context
;
664 GLfloat x0
, y0
, x1
, y1
;
666 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
669 /* XXX if DrawPixels image is larger than max texture size, break
672 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
673 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
674 assert(width
<= maxSize
);
675 assert(height
<= maxSize
);
677 cso_save_rasterizer(cso
);
678 cso_save_viewport(cso
);
679 cso_save_samplers(cso
);
680 cso_save_fragment_sampler_views(cso
);
681 cso_save_fragment_shader(cso
);
682 cso_save_stream_outputs(cso
);
683 cso_save_vertex_shader(cso
);
684 cso_save_geometry_shader(cso
);
685 cso_save_vertex_elements(cso
);
686 cso_save_vertex_buffers(cso
);
688 cso_save_depth_stencil_alpha(cso
);
692 /* rasterizer state: just scissor */
694 struct pipe_rasterizer_state rasterizer
;
695 memset(&rasterizer
, 0, sizeof(rasterizer
));
696 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
697 ctx
->Color
._ClampFragmentColor
;
698 rasterizer
.gl_rasterization_rules
= 1;
699 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
700 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
701 cso_set_rasterizer(cso
, &rasterizer
);
705 /* Stencil writing bypasses the normal fragment pipeline to
706 * disable color writing and set stencil test to always pass.
708 struct pipe_depth_stencil_alpha_state dsa
;
709 struct pipe_blend_state blend
;
712 memset(&dsa
, 0, sizeof(dsa
));
713 dsa
.stencil
[0].enabled
= 1;
714 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
715 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
716 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
718 /* writing depth+stencil: depth test always passes */
719 dsa
.depth
.enabled
= 1;
720 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
721 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
723 cso_set_depth_stencil_alpha(cso
, &dsa
);
725 /* blend (colormask) */
726 memset(&blend
, 0, sizeof(blend
));
727 cso_set_blend(cso
, &blend
);
730 /* fragment shader state: TEX lookup program */
731 cso_set_fragment_shader_handle(cso
, driver_fp
);
733 /* vertex shader state: position + texcoord pass-through */
734 cso_set_vertex_shader_handle(cso
, driver_vp
);
736 /* geometry shader state: disabled */
737 cso_set_geometry_shader_handle(cso
, NULL
);
739 /* texture sampling state: */
741 struct pipe_sampler_state sampler
;
742 memset(&sampler
, 0, sizeof(sampler
));
743 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
744 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
745 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
746 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
747 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
748 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
749 sampler
.normalized_coords
= normalized
;
751 cso_single_sampler(cso
, 0, &sampler
);
752 if (num_sampler_view
> 1) {
753 cso_single_sampler(cso
, 1, &sampler
);
755 cso_single_sampler_done(cso
);
758 /* viewport state: viewport matching window dims */
760 const float w
= (float) ctx
->DrawBuffer
->Width
;
761 const float h
= (float) ctx
->DrawBuffer
->Height
;
762 struct pipe_viewport_state vp
;
763 vp
.scale
[0] = 0.5f
* w
;
764 vp
.scale
[1] = -0.5f
* h
;
767 vp
.translate
[0] = 0.5f
* w
;
768 vp
.translate
[1] = 0.5f
* h
;
769 vp
.translate
[2] = 0.5f
;
770 vp
.translate
[3] = 0.0f
;
771 cso_set_viewport(cso
, &vp
);
774 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
775 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
778 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
780 /* Compute Gallium window coords (y=0=top) with pixel zoom.
781 * Recall that these coords are transformed by the current
782 * vertex shader and viewport transformation.
784 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
785 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
786 invertTex
= !invertTex
;
790 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
792 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
794 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
797 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
798 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
799 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
802 cso_restore_rasterizer(cso
);
803 cso_restore_viewport(cso
);
804 cso_restore_samplers(cso
);
805 cso_restore_fragment_sampler_views(cso
);
806 cso_restore_fragment_shader(cso
);
807 cso_restore_vertex_shader(cso
);
808 cso_restore_geometry_shader(cso
);
809 cso_restore_vertex_elements(cso
);
810 cso_restore_vertex_buffers(cso
);
811 cso_restore_stream_outputs(cso
);
813 cso_restore_depth_stencil_alpha(cso
);
814 cso_restore_blend(cso
);
820 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
821 * can't use a fragment shader to write stencil values.
824 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
825 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
826 const struct gl_pixelstore_attrib
*unpack
,
827 const GLvoid
*pixels
)
829 struct st_context
*st
= st_context(ctx
);
830 struct pipe_context
*pipe
= st
->pipe
;
831 struct st_renderbuffer
*strb
;
832 enum pipe_transfer_usage usage
;
833 struct pipe_transfer
*pt
;
834 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
836 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
841 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
843 /* totally clipped */
848 strb
= st_renderbuffer(ctx
->DrawBuffer
->
849 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
851 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
852 y
= ctx
->DrawBuffer
->Height
- y
- height
;
855 if (format
== GL_STENCIL_INDEX
&&
856 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
857 /* writing stencil to a combined depth+stencil buffer */
858 usage
= PIPE_TRANSFER_READ_WRITE
;
861 usage
= PIPE_TRANSFER_WRITE
;
864 pt
= pipe_get_transfer(pipe
, strb
->texture
,
865 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
869 stmap
= pipe_transfer_map(pipe
, pt
);
871 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
874 sValues
= (GLubyte
*) malloc(width
* sizeof(GLubyte
));
875 zValues
= (GLuint
*) 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
);
997 pipe
->transfer_destroy(pipe
, pt
);
1002 * Get fragment program variant for a glDrawPixels or glCopyPixels
1003 * command for RGBA data.
1005 static struct st_fp_variant
*
1006 get_color_fp_variant(struct st_context
*st
)
1008 struct gl_context
*ctx
= st
->ctx
;
1009 struct st_fp_variant_key key
;
1010 struct st_fp_variant
*fpv
;
1012 memset(&key
, 0, sizeof(key
));
1016 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1017 ctx
->Pixel
.RedScale
!= 1.0 ||
1018 ctx
->Pixel
.GreenBias
!= 0.0 ||
1019 ctx
->Pixel
.GreenScale
!= 1.0 ||
1020 ctx
->Pixel
.BlueBias
!= 0.0 ||
1021 ctx
->Pixel
.BlueScale
!= 1.0 ||
1022 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1023 ctx
->Pixel
.AlphaScale
!= 1.0);
1024 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1025 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1026 st
->ctx
->Color
._ClampFragmentColor
;
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 * Called via ctx->Driver.DrawPixels()
1062 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1063 GLsizei width
, GLsizei height
,
1064 GLenum format
, GLenum type
,
1065 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1067 void *driver_vp
, *driver_fp
;
1068 struct st_context
*st
= st_context(ctx
);
1069 const GLfloat
*color
;
1070 struct pipe_context
*pipe
= st
->pipe
;
1071 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1072 struct pipe_sampler_view
*sv
[2];
1073 int num_sampler_view
= 1;
1074 struct st_fp_variant
*fpv
;
1076 if (format
== GL_DEPTH_STENCIL
)
1077 write_stencil
= write_depth
= GL_TRUE
;
1078 else if (format
== GL_STENCIL_INDEX
)
1079 write_stencil
= GL_TRUE
;
1080 else if (format
== GL_DEPTH_COMPONENT
)
1081 write_depth
= GL_TRUE
;
1083 if (write_stencil
&&
1084 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1085 /* software fallback */
1086 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1091 /* Mesa state should be up to date by now */
1092 assert(ctx
->NewState
== 0x0);
1094 st_validate_state(st
);
1097 * Get vertex/fragment shaders
1099 if (write_depth
|| write_stencil
) {
1100 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1102 driver_fp
= fpv
->driver_shader
;
1104 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1106 color
= ctx
->Current
.RasterColor
;
1109 fpv
= get_color_fp_variant(st
);
1111 driver_fp
= fpv
->driver_shader
;
1113 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1116 if (st
->pixel_xfer
.pixelmap_enabled
) {
1117 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1122 /* update fragment program constants */
1123 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1125 /* draw with textured quad */
1127 struct pipe_resource
*pt
1128 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1130 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1133 /* Create a second sampler view to read stencil.
1134 * The stencil is written using the shader stencil export
1136 if (write_stencil
) {
1137 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1139 switch (pt
->format
) {
1140 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1141 case PIPE_FORMAT_X24S8_UINT
:
1142 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1144 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1145 case PIPE_FORMAT_S8X24_UINT
:
1146 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1148 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1149 case PIPE_FORMAT_X32_S8X24_UINT
:
1150 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1152 case PIPE_FORMAT_S8_UINT
:
1153 stencil_format
= PIPE_FORMAT_S8_UINT
;
1159 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1164 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1166 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1171 color
, GL_FALSE
, write_depth
, write_stencil
);
1172 pipe_sampler_view_reference(&sv
[0], NULL
);
1173 if (num_sampler_view
> 1)
1174 pipe_sampler_view_reference(&sv
[1], NULL
);
1176 pipe_resource_reference(&pt
, NULL
);
1184 * Software fallback for glCopyPixels(GL_STENCIL).
1187 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1188 GLsizei width
, GLsizei height
,
1189 GLint dstx
, GLint dsty
)
1191 struct st_renderbuffer
*rbDraw
;
1192 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1193 enum pipe_transfer_usage usage
;
1194 struct pipe_transfer
*ptDraw
;
1199 buffer
= malloc(width
* height
* sizeof(ubyte
));
1201 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1205 /* Get the dest renderbuffer */
1206 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1207 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1209 /* this will do stencil pixel transfer ops */
1210 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1211 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1212 &ctx
->DefaultPacking
, buffer
);
1215 /* debug code: dump stencil values */
1217 for (row
= 0; row
< height
; row
++) {
1218 printf("%3d: ", row
);
1219 for (col
= 0; col
< width
; col
++) {
1220 printf("%02x ", buffer
[col
+ row
* width
]);
1226 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1227 usage
= PIPE_TRANSFER_READ_WRITE
;
1229 usage
= PIPE_TRANSFER_WRITE
;
1231 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1232 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1235 ptDraw
= pipe_get_transfer(pipe
,
1238 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1242 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1243 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1245 /* map the stencil buffer */
1246 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1249 /* XXX PixelZoom not handled yet */
1250 for (i
= 0; i
< height
; i
++) {
1257 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1261 dst
= drawMap
+ y
* ptDraw
->stride
;
1262 src
= buffer
+ i
* width
;
1264 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1269 /* unmap the stencil buffer */
1270 pipe_transfer_unmap(pipe
, ptDraw
);
1271 pipe
->transfer_destroy(pipe
, ptDraw
);
1276 * Return renderbuffer to use for reading color pixels for glCopyPixels
1278 static struct st_renderbuffer
*
1279 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1281 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1282 struct st_renderbuffer
*strb
=
1283 st_renderbuffer(fb
->_ColorReadBuffer
);
1289 /** Do the src/dest regions overlap? */
1291 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1292 GLsizei width
, GLsizei height
)
1294 if (srcX
+ width
<= dstX
||
1295 dstX
+ width
<= srcX
||
1296 srcY
+ height
<= dstY
||
1297 dstY
+ height
<= srcY
)
1305 * Try to do a glCopyPixels for simple cases with a blit by calling
1306 * pipe->resource_copy_region().
1308 * We can do this when we're copying color pixels (depth/stencil
1309 * eventually) with no pixel zoom, no pixel transfer ops, no
1310 * per-fragment ops, the src/dest regions don't overlap and the
1311 * src/dest pixel formats are the same.
1314 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1315 GLsizei width
, GLsizei height
,
1316 GLint dstx
, GLint dsty
, GLenum type
)
1318 struct st_context
*st
= st_context(ctx
);
1319 struct pipe_context
*pipe
= st
->pipe
;
1320 struct gl_pixelstore_attrib pack
, unpack
;
1321 GLint readX
, readY
, readW
, readH
;
1323 if (type
== GL_COLOR
&&
1324 ctx
->Pixel
.ZoomX
== 1.0 &&
1325 ctx
->Pixel
.ZoomY
== 1.0 &&
1326 ctx
->_ImageTransferState
== 0x0 &&
1327 !ctx
->Color
.BlendEnabled
&&
1328 !ctx
->Color
.AlphaEnabled
&&
1330 !ctx
->Fog
.Enabled
&&
1331 !ctx
->Stencil
.Enabled
&&
1332 !ctx
->FragmentProgram
.Enabled
&&
1333 !ctx
->VertexProgram
.Enabled
&&
1334 !ctx
->Shader
.CurrentFragmentProgram
&&
1335 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1336 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1337 !ctx
->Query
.CondRenderQuery
) {
1338 struct st_renderbuffer
*rbRead
, *rbDraw
;
1342 * Clip the read region against the src buffer bounds.
1343 * We'll still allocate a temporary buffer/texture for the original
1344 * src region size but we'll only read the region which is on-screen.
1345 * This may mean that we draw garbage pixels into the dest region, but
1352 pack
= ctx
->DefaultPacking
;
1353 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1354 return GL_TRUE
; /* all done */
1356 /* clip against dest buffer bounds and scissor box */
1357 drawX
= dstx
+ pack
.SkipPixels
;
1358 drawY
= dsty
+ pack
.SkipRows
;
1360 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1361 return GL_TRUE
; /* all done */
1363 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1364 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1366 rbRead
= st_get_color_read_renderbuffer(ctx
);
1367 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1369 if ((rbRead
!= rbDraw
||
1370 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1371 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1372 struct pipe_box srcBox
;
1374 /* flip src/dst position if needed */
1375 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1376 /* both buffers will have the same orientation */
1377 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1378 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1381 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1383 pipe
->resource_copy_region(pipe
,
1385 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1387 rbRead
->rtt_level
, &srcBox
);
1397 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1398 GLsizei width
, GLsizei height
,
1399 GLint dstx
, GLint dsty
, GLenum type
)
1401 struct st_context
*st
= st_context(ctx
);
1402 struct pipe_context
*pipe
= st
->pipe
;
1403 struct pipe_screen
*screen
= pipe
->screen
;
1404 struct st_renderbuffer
*rbRead
;
1405 void *driver_vp
, *driver_fp
;
1406 struct pipe_resource
*pt
;
1407 struct pipe_sampler_view
*sv
[2];
1408 int num_sampler_view
= 1;
1410 enum pipe_format srcFormat
, texFormat
;
1411 GLboolean invertTex
= GL_FALSE
;
1412 GLint readX
, readY
, readW
, readH
;
1413 GLuint sample_count
;
1414 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1415 struct st_fp_variant
*fpv
;
1417 st_validate_state(st
);
1419 if (type
== GL_DEPTH_STENCIL
) {
1420 /* XXX make this more efficient */
1421 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1422 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1426 if (type
== GL_STENCIL
) {
1427 /* can't use texturing to do stencil */
1428 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1432 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1436 * The subsequent code implements glCopyPixels by copying the source
1437 * pixels into a temporary texture that's then applied to a textured quad.
1438 * When we draw the textured quad, all the usual per-fragment operations
1444 * Get vertex/fragment shaders
1446 if (type
== GL_COLOR
) {
1447 rbRead
= st_get_color_read_renderbuffer(ctx
);
1450 fpv
= get_color_fp_variant(st
);
1451 driver_fp
= fpv
->driver_shader
;
1453 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1455 if (st
->pixel_xfer
.pixelmap_enabled
) {
1456 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1461 assert(type
== GL_DEPTH
);
1462 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1463 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1464 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1466 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1467 driver_fp
= fpv
->driver_shader
;
1469 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1472 /* update fragment program constants */
1473 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1475 sample_count
= rbRead
->texture
->nr_samples
;
1476 /* I believe this would be legal, presumably would need to do a resolve
1477 for color, and for depth/stencil spec says to just use one of the
1478 depth/stencil samples per pixel? Need some transfer clarifications. */
1479 assert(sample_count
< 2);
1481 srcFormat
= rbRead
->texture
->format
;
1483 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1485 PIPE_BIND_SAMPLER_VIEW
)) {
1486 texFormat
= srcFormat
;
1489 /* srcFormat can't be used as a texture format */
1490 if (type
== GL_DEPTH
) {
1491 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1492 GL_NONE
, GL_NONE
, st
->internal_target
,
1493 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1494 assert(texFormat
!= PIPE_FORMAT_NONE
);
1497 /* default color format */
1498 texFormat
= st_choose_format(screen
, GL_RGBA
,
1499 GL_NONE
, GL_NONE
, st
->internal_target
,
1500 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1501 assert(texFormat
!= PIPE_FORMAT_NONE
);
1505 /* Invert src region if needed */
1506 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1507 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1508 invertTex
= !invertTex
;
1511 /* Clip the read region against the src buffer bounds.
1512 * We'll still allocate a temporary buffer/texture for the original
1513 * src region size but we'll only read the region which is on-screen.
1514 * This may mean that we draw garbage pixels into the dest region, but
1521 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1522 /* The source region is completely out of bounds. Do nothing.
1523 * The GL spec says "Results of copies from outside the window,
1524 * or from regions of the window that are not exposed, are
1525 * hardware dependent and undefined."
1530 readW
= MAX2(0, readW
);
1531 readH
= MAX2(0, readH
);
1533 /* alloc temporary texture */
1534 pt
= alloc_texture(st
, width
, height
, texFormat
);
1538 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1540 pipe_resource_reference(&pt
, NULL
);
1544 /* Make temporary texture which is a copy of the src region.
1546 if (srcFormat
== texFormat
) {
1547 struct pipe_box src_box
;
1548 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1549 /* copy source framebuffer surface into mipmap/texture */
1550 pipe
->resource_copy_region(pipe
,
1553 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1554 rbRead
->texture
, /* src tex */
1555 rbRead
->rtt_level
, /* src lvl */
1560 /* CPU-based fallback/conversion */
1561 struct pipe_transfer
*ptRead
=
1562 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1564 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1566 readX
, readY
, readW
, readH
);
1567 struct pipe_transfer
*ptTex
;
1568 enum pipe_transfer_usage transfer_usage
;
1570 if (ST_DEBUG
& DEBUG_FALLBACK
)
1571 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1573 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1574 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1576 transfer_usage
= PIPE_TRANSFER_WRITE
;
1578 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1579 0, 0, width
, height
);
1581 /* copy image from ptRead surface to ptTex surface */
1582 if (type
== GL_COLOR
) {
1583 /* alternate path using get/put_tile() */
1584 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1585 enum pipe_format readFormat
, drawFormat
;
1586 readFormat
= util_format_linear(rbRead
->texture
->format
);
1587 drawFormat
= util_format_linear(pt
->format
);
1588 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1590 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1591 readW
, readH
, drawFormat
, buf
);
1596 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1597 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1598 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1603 pipe
->transfer_destroy(pipe
, ptRead
);
1604 pipe
->transfer_destroy(pipe
, ptTex
);
1607 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1608 * textured quad with that texture.
1610 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1611 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1616 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1618 pipe_resource_reference(&pt
, NULL
);
1619 pipe_sampler_view_reference(&sv
[0], NULL
);
1624 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1626 functions
->DrawPixels
= st_DrawPixels
;
1627 functions
->CopyPixels
= st_CopyPixels
;
1632 st_destroy_drawpix(struct st_context
*st
)
1636 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1637 if (st
->drawpix
.shaders
[i
])
1638 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1641 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1642 if (st
->drawpix
.vert_shaders
[0])
1643 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1644 if (st
->drawpix
.vert_shaders
[1])
1645 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1648 #endif /* FEATURE_drawpix */