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/macros.h"
37 #include "main/mfeatures.h"
38 #include "main/mtypes.h"
39 #include "main/pack.h"
41 #include "main/texformat.h"
42 #include "main/teximage.h"
43 #include "main/texstore.h"
44 #include "program/program.h"
45 #include "program/prog_print.h"
46 #include "program/prog_instruction.h"
49 #include "st_atom_constbuf.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_readpixels.h"
52 #include "st_cb_fbo.h"
53 #include "st_context.h"
55 #include "st_format.h"
56 #include "st_program.h"
57 #include "st_texture.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "tgsi/tgsi_ureg.h"
62 #include "util/u_draw_quad.h"
63 #include "util/u_format.h"
64 #include "util/u_inlines.h"
65 #include "util/u_math.h"
66 #include "util/u_tile.h"
67 #include "cso_cache/cso_context.h"
73 * Check if the given program is:
74 * 0: MOVE result.color, fragment.color;
78 is_passthrough_program(const struct gl_fragment_program
*prog
)
80 if (prog
->Base
.NumInstructions
== 2) {
81 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
82 if (inst
[0].Opcode
== OPCODE_MOV
&&
83 inst
[1].Opcode
== OPCODE_END
&&
84 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
85 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
86 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
87 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
88 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
89 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
98 * Returns a fragment program which implements the current pixel transfer ops.
100 static struct gl_fragment_program
*
101 get_glsl_pixel_transfer_program(struct st_context
*st
,
102 struct st_fragment_program
*orig
)
104 int pixelMaps
= 0, scaleAndBias
= 0;
105 struct gl_context
*ctx
= st
->ctx
;
106 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
107 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
112 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
113 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
114 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
115 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
119 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
122 /* create the colormap/texture now if not already done */
123 if (!st
->pixel_xfer
.pixelmap_texture
) {
124 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
125 st
->pixel_xfer
.pixelmap_sampler_view
=
126 st_create_texture_sampler_view(st
->pipe
,
127 st
->pixel_xfer
.pixelmap_texture
);
131 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
132 scaleAndBias
, pixelMaps
);
139 * Make fragment shader for glDraw/CopyPixels. This shader is made
140 * by combining the pixel transfer shader with the user-defined shader.
141 * \param fpIn the current/incoming fragment program
142 * \param fpOut returns the combined fragment program
145 st_make_drawpix_fragment_program(struct st_context
*st
,
146 struct gl_fragment_program
*fpIn
,
147 struct gl_fragment_program
**fpOut
)
149 struct gl_program
*newProg
;
150 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
152 if (is_passthrough_program(fpIn
)) {
153 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
154 &st
->pixel_xfer
.program
->Base
);
156 else if (stfp
->glsl_to_tgsi
!= NULL
) {
157 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
162 printf("Base program:\n");
163 _mesa_print_program(&fpIn
->Base
);
164 printf("DrawPix program:\n");
165 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
167 newProg
= _mesa_combine_programs(st
->ctx
,
168 &st
->pixel_xfer
.program
->Base
.Base
,
174 printf("Combined DrawPixels program:\n");
175 _mesa_print_program(newProg
);
176 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
177 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
178 _mesa_print_parameter_list(newProg
->Parameters
);
181 *fpOut
= (struct gl_fragment_program
*) newProg
;
186 * Create fragment program that does a TEX() instruction to get a Z and/or
187 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
188 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
189 * Pass fragment color through as-is.
190 * \return pointer to the gl_fragment program
192 struct gl_fragment_program
*
193 st_make_drawpix_z_stencil_program(struct st_context
*st
,
194 GLboolean write_depth
,
195 GLboolean write_stencil
)
197 struct gl_context
*ctx
= st
->ctx
;
198 struct gl_program
*p
;
199 struct gl_fragment_program
*fp
;
201 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
203 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
205 if (st
->drawpix
.shaders
[shaderIndex
]) {
206 /* already have the proper shader */
207 return st
->drawpix
.shaders
[shaderIndex
];
213 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
217 p
->NumInstructions
= write_depth
? 2 : 1;
218 p
->NumInstructions
+= write_stencil
? 1 : 0;
220 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
221 if (!p
->Instructions
) {
222 ctx
->Driver
.DeleteProgram(ctx
, p
);
225 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
228 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
229 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
230 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
231 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
232 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
233 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
234 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
235 p
->Instructions
[ic
].TexSrcUnit
= 0;
236 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
241 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
242 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
243 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
244 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
245 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
246 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
247 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
248 p
->Instructions
[ic
].TexSrcUnit
= 1;
249 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
254 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
256 assert(ic
== p
->NumInstructions
);
258 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
259 p
->OutputsWritten
= 0;
261 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
263 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
265 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
267 p
->SamplersUsed
|= 1 << 1;
269 fp
= (struct gl_fragment_program
*) p
;
271 /* save the new shader */
272 st
->drawpix
.shaders
[shaderIndex
] = fp
;
279 * Create a simple vertex shader that just passes through the
280 * vertex position and texcoord (and optionally, color).
283 make_passthrough_vertex_shader(struct st_context
*st
,
286 if (!st
->drawpix
.vert_shaders
[passColor
]) {
287 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
292 /* MOV result.pos, vertex.pos; */
294 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
295 ureg_DECL_vs_input( ureg
, 0 ));
297 /* MOV result.texcoord0, vertex.attr[1]; */
299 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
300 ureg_DECL_vs_input( ureg
, 1 ));
303 /* MOV result.color0, vertex.attr[2]; */
305 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
306 ureg_DECL_vs_input( ureg
, 2 ));
311 st
->drawpix
.vert_shaders
[passColor
] =
312 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
315 return st
->drawpix
.vert_shaders
[passColor
];
320 * Return a texture internalFormat for drawing/copying an image
321 * of the given format and type.
324 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
327 case GL_DEPTH_COMPONENT
:
329 case GL_UNSIGNED_SHORT
:
330 return GL_DEPTH_COMPONENT16
;
332 case GL_UNSIGNED_INT
:
333 return GL_DEPTH_COMPONENT32
;
336 if (ctx
->Extensions
.ARB_depth_buffer_float
)
337 return GL_DEPTH_COMPONENT32F
;
339 return GL_DEPTH_COMPONENT
;
342 return GL_DEPTH_COMPONENT
;
345 case GL_DEPTH_STENCIL
:
347 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
348 return GL_DEPTH32F_STENCIL8
;
350 case GL_UNSIGNED_INT_24_8
:
352 return GL_DEPTH24_STENCIL8
;
355 case GL_STENCIL_INDEX
:
356 return GL_STENCIL_INDEX
;
359 if (_mesa_is_integer_format(format
)) {
363 case GL_UNSIGNED_BYTE
:
367 case GL_UNSIGNED_SHORT
:
371 case GL_UNSIGNED_INT
:
374 assert(0 && "Unexpected type in internal_format()");
375 return GL_RGBA_INTEGER
;
380 case GL_UNSIGNED_BYTE
:
381 case GL_UNSIGNED_INT_8_8_8_8
:
382 case GL_UNSIGNED_INT_8_8_8_8_REV
:
386 case GL_UNSIGNED_BYTE_3_3_2
:
387 case GL_UNSIGNED_BYTE_2_3_3_REV
:
388 case GL_UNSIGNED_SHORT_4_4_4_4
:
389 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
392 case GL_UNSIGNED_SHORT_5_6_5
:
393 case GL_UNSIGNED_SHORT_5_6_5_REV
:
394 case GL_UNSIGNED_SHORT_5_5_5_1
:
395 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
398 case GL_UNSIGNED_INT_10_10_10_2
:
399 case GL_UNSIGNED_INT_2_10_10_10_REV
:
402 case GL_UNSIGNED_SHORT
:
403 case GL_UNSIGNED_INT
:
408 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
413 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
415 case GL_HALF_FLOAT_ARB
:
417 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
418 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
423 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
424 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
426 case GL_UNSIGNED_INT_5_9_9_9_REV
:
427 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
430 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
431 assert(ctx
->Extensions
.EXT_packed_float
);
432 return GL_R11F_G11F_B10F
;
440 * Create a temporary texture to hold an image of the given size.
441 * If width, height are not POT and the driver only handles POT textures,
442 * allocate the next larger size of texture that is POT.
444 static struct pipe_resource
*
445 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
446 enum pipe_format texFormat
)
448 struct pipe_resource
*pt
;
450 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
451 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
458 * Make texture containing an image for glDrawPixels image.
459 * If 'pixels' is NULL, leave the texture image data undefined.
461 static struct pipe_resource
*
462 make_texture(struct st_context
*st
,
463 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
464 const struct gl_pixelstore_attrib
*unpack
,
465 const GLvoid
*pixels
)
467 struct gl_context
*ctx
= st
->ctx
;
468 struct pipe_context
*pipe
= st
->pipe
;
470 struct pipe_resource
*pt
;
471 enum pipe_format pipeFormat
;
472 GLenum baseInternalFormat
, intFormat
;
474 intFormat
= internal_format(ctx
, format
, type
);
475 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
477 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
478 format
, type
, GL_FALSE
);
481 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
484 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
488 /* alloc temporary texture */
489 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
491 _mesa_unmap_pbo_source(ctx
, unpack
);
496 struct pipe_transfer
*transfer
;
499 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
501 /* we'll do pixel transfer in a fragment shader */
502 ctx
->_ImageTransferState
= 0x0;
504 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
505 PIPE_TRANSFER_WRITE
, 0, 0,
508 /* map texture transfer */
509 dest
= pipe_transfer_map(pipe
, transfer
);
512 /* Put image into texture transfer.
513 * Note that the image is actually going to be upside down in
514 * the texture. We deal with that with texcoords.
516 success
= _mesa_texstore(ctx
, 2, /* dims */
517 baseInternalFormat
, /* baseInternalFormat */
518 mformat
, /* gl_format */
519 0, 0, 0, /* dstX/Y/Zoffset */
520 transfer
->stride
, /* dstRowStride, bytes */
521 &dest
, /* destSlices */
522 width
, height
, 1, /* size */
523 format
, type
, /* src format/type */
524 pixels
, /* data source */
528 pipe_transfer_unmap(pipe
, transfer
);
529 pipe
->transfer_destroy(pipe
, transfer
);
534 ctx
->_ImageTransferState
= imageTransferStateSave
;
537 _mesa_unmap_pbo_source(ctx
, unpack
);
544 * Draw quad with texcoords and optional color.
545 * Coords are gallium window coords with y=0=top.
546 * \param color may be null
547 * \param invertTex if true, flip texcoords vertically
550 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
551 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
552 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
554 struct st_context
*st
= st_context(ctx
);
555 struct pipe_context
*pipe
= st
->pipe
;
556 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
558 /* setup vertex data */
560 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
561 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
562 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
563 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
564 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
565 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
566 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
567 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
568 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
569 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
573 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
574 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
577 verts
[1][0][0] = clip_x1
;
578 verts
[1][0][1] = clip_y0
;
581 verts
[2][0][0] = clip_x1
;
582 verts
[2][0][1] = clip_y1
;
585 verts
[3][0][0] = clip_x0
;
586 verts
[3][0][1] = clip_y1
;
588 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
589 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
590 verts
[1][1][0] = sRight
;
591 verts
[1][1][1] = tTop
;
592 verts
[2][1][0] = sRight
;
593 verts
[2][1][1] = tBot
;
594 verts
[3][1][0] = sLeft
;
595 verts
[3][1][1] = tBot
;
597 /* same for all verts: */
599 for (i
= 0; i
< 4; i
++) {
600 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
601 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
602 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
603 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
604 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
605 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
606 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
607 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
611 for (i
= 0; i
< 4; i
++) {
612 verts
[i
][0][2] = z
; /*Z*/
613 verts
[i
][0][3] = 1.0f
; /*W*/
614 verts
[i
][1][2] = 0.0f
; /*R*/
615 verts
[i
][1][3] = 1.0f
; /*Q*/
621 struct pipe_resource
*buf
;
623 /* allocate/load buffer object with vertex data */
624 buf
= pipe_buffer_create(pipe
->screen
,
625 PIPE_BIND_VERTEX_BUFFER
,
628 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
630 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
633 3); /* attribs/vert */
634 pipe_resource_reference(&buf
, NULL
);
641 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
642 GLsizei width
, GLsizei height
,
643 GLfloat zoomX
, GLfloat zoomY
,
644 struct pipe_sampler_view
**sv
,
645 int num_sampler_view
,
648 const GLfloat
*color
,
650 GLboolean write_depth
, GLboolean write_stencil
)
652 struct st_context
*st
= st_context(ctx
);
653 struct pipe_context
*pipe
= st
->pipe
;
654 struct cso_context
*cso
= st
->cso_context
;
655 GLfloat x0
, y0
, x1
, y1
;
657 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
660 /* XXX if DrawPixels image is larger than max texture size, break
663 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
664 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
665 assert(width
<= maxSize
);
666 assert(height
<= maxSize
);
668 cso_save_rasterizer(cso
);
669 cso_save_viewport(cso
);
670 cso_save_samplers(cso
);
671 cso_save_fragment_sampler_views(cso
);
672 cso_save_fragment_shader(cso
);
673 cso_save_vertex_shader(cso
);
674 cso_save_geometry_shader(cso
);
675 cso_save_vertex_elements(cso
);
676 cso_save_vertex_buffers(cso
);
678 cso_save_depth_stencil_alpha(cso
);
682 /* rasterizer state: just scissor */
684 struct pipe_rasterizer_state rasterizer
;
685 memset(&rasterizer
, 0, sizeof(rasterizer
));
686 rasterizer
.clamp_fragment_color
= ctx
->Color
._ClampFragmentColor
;
687 rasterizer
.gl_rasterization_rules
= 1;
688 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
689 cso_set_rasterizer(cso
, &rasterizer
);
693 /* Stencil writing bypasses the normal fragment pipeline to
694 * disable color writing and set stencil test to always pass.
696 struct pipe_depth_stencil_alpha_state dsa
;
697 struct pipe_blend_state blend
;
700 memset(&dsa
, 0, sizeof(dsa
));
701 dsa
.stencil
[0].enabled
= 1;
702 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
703 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
704 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
706 /* writing depth+stencil: depth test always passes */
707 dsa
.depth
.enabled
= 1;
708 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
709 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
711 cso_set_depth_stencil_alpha(cso
, &dsa
);
713 /* blend (colormask) */
714 memset(&blend
, 0, sizeof(blend
));
715 cso_set_blend(cso
, &blend
);
718 /* fragment shader state: TEX lookup program */
719 cso_set_fragment_shader_handle(cso
, driver_fp
);
721 /* vertex shader state: position + texcoord pass-through */
722 cso_set_vertex_shader_handle(cso
, driver_vp
);
724 /* geometry shader state: disabled */
725 cso_set_geometry_shader_handle(cso
, NULL
);
727 /* texture sampling state: */
729 struct pipe_sampler_state sampler
;
730 memset(&sampler
, 0, sizeof(sampler
));
731 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
732 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
733 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
734 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
735 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
736 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
737 sampler
.normalized_coords
= normalized
;
739 cso_single_sampler(cso
, 0, &sampler
);
740 if (num_sampler_view
> 1) {
741 cso_single_sampler(cso
, 1, &sampler
);
743 cso_single_sampler_done(cso
);
746 /* viewport state: viewport matching window dims */
748 const float w
= (float) ctx
->DrawBuffer
->Width
;
749 const float h
= (float) ctx
->DrawBuffer
->Height
;
750 struct pipe_viewport_state vp
;
751 vp
.scale
[0] = 0.5f
* w
;
752 vp
.scale
[1] = -0.5f
* h
;
755 vp
.translate
[0] = 0.5f
* w
;
756 vp
.translate
[1] = 0.5f
* h
;
757 vp
.translate
[2] = 0.5f
;
758 vp
.translate
[3] = 0.0f
;
759 cso_set_viewport(cso
, &vp
);
762 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
765 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
767 /* Compute Gallium window coords (y=0=top) with pixel zoom.
768 * Recall that these coords are transformed by the current
769 * vertex shader and viewport transformation.
771 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
772 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
773 invertTex
= !invertTex
;
777 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
779 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
781 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
784 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
785 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
786 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
789 cso_restore_rasterizer(cso
);
790 cso_restore_viewport(cso
);
791 cso_restore_samplers(cso
);
792 cso_restore_fragment_sampler_views(cso
);
793 cso_restore_fragment_shader(cso
);
794 cso_restore_vertex_shader(cso
);
795 cso_restore_geometry_shader(cso
);
796 cso_restore_vertex_elements(cso
);
797 cso_restore_vertex_buffers(cso
);
799 cso_restore_depth_stencil_alpha(cso
);
800 cso_restore_blend(cso
);
806 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
807 * can't use a fragment shader to write stencil values.
810 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
811 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
812 const struct gl_pixelstore_attrib
*unpack
,
813 const GLvoid
*pixels
)
815 struct st_context
*st
= st_context(ctx
);
816 struct pipe_context
*pipe
= st
->pipe
;
817 struct st_renderbuffer
*strb
;
818 enum pipe_transfer_usage usage
;
819 struct pipe_transfer
*pt
;
820 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
823 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
826 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
828 /* totally clipped */
833 strb
= st_renderbuffer(ctx
->DrawBuffer
->
834 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
836 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
837 y
= ctx
->DrawBuffer
->Height
- y
- height
;
840 if(format
!= GL_DEPTH_STENCIL
&&
841 util_format_get_component_bits(strb
->format
,
842 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
843 usage
= PIPE_TRANSFER_READ_WRITE
;
845 usage
= PIPE_TRANSFER_WRITE
;
847 pt
= pipe_get_transfer(pipe
, strb
->texture
,
848 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
852 stmap
= pipe_transfer_map(pipe
, pt
);
854 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
857 /* if width > MAX_WIDTH, have to process image in chunks */
859 while (skipPixels
< width
) {
860 const GLint spanX
= skipPixels
;
861 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
863 for (row
= 0; row
< height
; row
++) {
864 GLubyte sValues
[MAX_WIDTH
];
865 GLuint zValues
[MAX_WIDTH
];
866 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
867 GLenum destType
= GL_UNSIGNED_BYTE
;
868 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
872 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
873 type
, source
, &clippedUnpack
,
874 ctx
->_ImageTransferState
);
876 if (format
== GL_DEPTH_STENCIL
) {
878 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
879 GL_FLOAT
: GL_UNSIGNED_INT
;
881 _mesa_unpack_depth_span(ctx
, spanWidth
, ztype
, zValues
,
882 (1 << 24) - 1, type
, source
,
887 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
888 "zoom not complete");
894 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
895 spanY
= height
- row
- 1;
901 /* now pack the stencil (and Z) values in the dest format */
902 switch (pt
->resource
->format
) {
903 case PIPE_FORMAT_S8_UINT
:
905 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
906 assert(usage
== PIPE_TRANSFER_WRITE
);
907 memcpy(dest
, sValues
, spanWidth
);
910 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
911 if (format
== GL_DEPTH_STENCIL
) {
912 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
914 assert(usage
== PIPE_TRANSFER_WRITE
);
915 for (k
= 0; k
< spanWidth
; k
++) {
916 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
920 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
922 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
923 for (k
= 0; k
< spanWidth
; k
++) {
924 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
928 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
929 if (format
== GL_DEPTH_STENCIL
) {
930 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
932 assert(usage
== PIPE_TRANSFER_WRITE
);
933 for (k
= 0; k
< spanWidth
; k
++) {
934 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
938 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
940 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
941 for (k
= 0; k
< spanWidth
; k
++) {
942 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
946 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
947 if (format
== GL_DEPTH_STENCIL
) {
948 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
949 GLfloat
*destf
= (GLfloat
*)dest
;
951 assert(usage
== PIPE_TRANSFER_WRITE
);
952 for (k
= 0; k
< spanWidth
; k
++) {
953 destf
[k
*2] = zValuesFloat
[k
];
954 dest
[k
*2+1] = sValues
[k
] & 0xff;
958 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
960 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
961 for (k
= 0; k
< spanWidth
; k
++) {
962 dest
[k
*2+1] = sValues
[k
] & 0xff;
971 skipPixels
+= spanWidth
;
974 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
976 /* unmap the stencil buffer */
977 pipe_transfer_unmap(pipe
, pt
);
978 pipe
->transfer_destroy(pipe
, pt
);
983 * Get fragment program variant for a glDrawPixels or glCopyPixels
984 * command for RGBA data.
986 static struct st_fp_variant
*
987 get_color_fp_variant(struct st_context
*st
)
989 struct gl_context
*ctx
= st
->ctx
;
990 struct st_fp_variant_key key
;
991 struct st_fp_variant
*fpv
;
993 memset(&key
, 0, sizeof(key
));
997 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
998 ctx
->Pixel
.RedScale
!= 1.0 ||
999 ctx
->Pixel
.GreenBias
!= 0.0 ||
1000 ctx
->Pixel
.GreenScale
!= 1.0 ||
1001 ctx
->Pixel
.BlueBias
!= 0.0 ||
1002 ctx
->Pixel
.BlueScale
!= 1.0 ||
1003 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1004 ctx
->Pixel
.AlphaScale
!= 1.0);
1005 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1007 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1014 * Get fragment program variant for a glDrawPixels or glCopyPixels
1015 * command for depth/stencil data.
1017 static struct st_fp_variant
*
1018 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1019 GLboolean write_stencil
)
1021 struct st_fp_variant_key key
;
1022 struct st_fp_variant
*fpv
;
1024 memset(&key
, 0, sizeof(key
));
1028 key
.drawpixels_z
= write_depth
;
1029 key
.drawpixels_stencil
= write_stencil
;
1031 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1038 * Called via ctx->Driver.DrawPixels()
1041 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1042 GLsizei width
, GLsizei height
,
1043 GLenum format
, GLenum type
,
1044 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1046 void *driver_vp
, *driver_fp
;
1047 struct st_context
*st
= st_context(ctx
);
1048 const GLfloat
*color
;
1049 struct pipe_context
*pipe
= st
->pipe
;
1050 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1051 struct pipe_sampler_view
*sv
[2];
1052 int num_sampler_view
= 1;
1053 struct st_fp_variant
*fpv
;
1055 if (format
== GL_DEPTH_STENCIL
)
1056 write_stencil
= write_depth
= GL_TRUE
;
1057 else if (format
== GL_STENCIL_INDEX
)
1058 write_stencil
= GL_TRUE
;
1059 else if (format
== GL_DEPTH_COMPONENT
)
1060 write_depth
= GL_TRUE
;
1062 if (write_stencil
&&
1063 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1064 /* software fallback */
1065 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1070 /* Mesa state should be up to date by now */
1071 assert(ctx
->NewState
== 0x0);
1073 st_validate_state(st
);
1076 * Get vertex/fragment shaders
1078 if (write_depth
|| write_stencil
) {
1079 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1081 driver_fp
= fpv
->driver_shader
;
1083 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1085 color
= ctx
->Current
.RasterColor
;
1088 fpv
= get_color_fp_variant(st
);
1090 driver_fp
= fpv
->driver_shader
;
1092 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1095 if (st
->pixel_xfer
.pixelmap_enabled
) {
1096 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1101 /* update fragment program constants */
1102 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1104 /* draw with textured quad */
1106 struct pipe_resource
*pt
1107 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1109 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1112 /* Create a second sampler view to read stencil.
1113 * The stencil is written using the shader stencil export
1115 if (write_stencil
) {
1116 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1118 switch (pt
->format
) {
1119 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1120 case PIPE_FORMAT_X24S8_UINT
:
1121 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1123 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1124 case PIPE_FORMAT_S8X24_UINT
:
1125 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1127 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1128 case PIPE_FORMAT_X32_S8X24_UINT
:
1129 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1131 case PIPE_FORMAT_S8_UINT
:
1132 stencil_format
= PIPE_FORMAT_S8_UINT
;
1138 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1143 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1145 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1150 color
, GL_FALSE
, write_depth
, write_stencil
);
1151 pipe_sampler_view_reference(&sv
[0], NULL
);
1152 if (num_sampler_view
> 1)
1153 pipe_sampler_view_reference(&sv
[1], NULL
);
1155 pipe_resource_reference(&pt
, NULL
);
1163 * Software fallback for glCopyPixels(GL_STENCIL).
1166 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1167 GLsizei width
, GLsizei height
,
1168 GLint dstx
, GLint dsty
)
1170 struct st_renderbuffer
*rbDraw
;
1171 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1172 enum pipe_transfer_usage usage
;
1173 struct pipe_transfer
*ptDraw
;
1178 buffer
= malloc(width
* height
* sizeof(ubyte
));
1180 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1184 /* Get the dest renderbuffer. If there's a wrapper, use the
1185 * underlying renderbuffer.
1187 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1188 if (rbDraw
->Base
.Wrapped
)
1189 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1191 /* this will do stencil pixel transfer ops */
1192 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1193 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1194 &ctx
->DefaultPacking
, buffer
);
1197 /* debug code: dump stencil values */
1199 for (row
= 0; row
< height
; row
++) {
1200 printf("%3d: ", row
);
1201 for (col
= 0; col
< width
; col
++) {
1202 printf("%02x ", buffer
[col
+ row
* width
]);
1208 if (util_format_get_component_bits(rbDraw
->format
,
1209 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1210 usage
= PIPE_TRANSFER_READ_WRITE
;
1212 usage
= PIPE_TRANSFER_WRITE
;
1214 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1215 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1218 ptDraw
= pipe_get_transfer(pipe
,
1221 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1225 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1226 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1228 /* map the stencil buffer */
1229 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1232 /* XXX PixelZoom not handled yet */
1233 for (i
= 0; i
< height
; i
++) {
1240 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1244 dst
= drawMap
+ y
* ptDraw
->stride
;
1245 src
= buffer
+ i
* width
;
1247 switch (ptDraw
->resource
->format
) {
1248 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1250 uint
*dst4
= (uint
*) dst
;
1252 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1253 for (j
= 0; j
< width
; j
++) {
1254 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1259 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1261 uint
*dst4
= (uint
*) dst
;
1263 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1264 for (j
= 0; j
< width
; j
++) {
1265 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1270 case PIPE_FORMAT_S8_UINT
:
1271 assert(usage
== PIPE_TRANSFER_WRITE
);
1272 memcpy(dst
, src
, width
);
1274 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1276 uint
*dst4
= (uint
*) dst
;
1279 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1280 for (j
= 0; j
< width
; j
++) {
1281 *dst4
= src
[j
] & 0xff;
1293 /* unmap the stencil buffer */
1294 pipe_transfer_unmap(pipe
, ptDraw
);
1295 pipe
->transfer_destroy(pipe
, ptDraw
);
1299 /** Do the src/dest regions overlap? */
1301 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1302 GLsizei width
, GLsizei height
)
1304 if (srcX
+ width
<= dstX
||
1305 dstX
+ width
<= srcX
||
1306 srcY
+ height
<= dstY
||
1307 dstY
+ height
<= srcY
)
1315 * Try to do a glCopyPixels for simple cases with a blit by calling
1316 * pipe->resource_copy_region().
1318 * We can do this when we're copying color pixels (depth/stencil
1319 * eventually) with no pixel zoom, no pixel transfer ops, no
1320 * per-fragment ops, the src/dest regions don't overlap and the
1321 * src/dest pixel formats are the same.
1324 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1325 GLsizei width
, GLsizei height
,
1326 GLint dstx
, GLint dsty
, GLenum type
)
1328 struct st_context
*st
= st_context(ctx
);
1329 struct pipe_context
*pipe
= st
->pipe
;
1330 struct gl_pixelstore_attrib pack
, unpack
;
1331 GLint readX
, readY
, readW
, readH
;
1333 if (type
== GL_COLOR
&&
1334 ctx
->Pixel
.ZoomX
== 1.0 &&
1335 ctx
->Pixel
.ZoomY
== 1.0 &&
1336 ctx
->_ImageTransferState
== 0x0 &&
1337 !ctx
->Color
.BlendEnabled
&&
1338 !ctx
->Color
.AlphaEnabled
&&
1340 !ctx
->Fog
.Enabled
&&
1341 !ctx
->Stencil
.Enabled
&&
1342 !ctx
->FragmentProgram
.Enabled
&&
1343 !ctx
->VertexProgram
.Enabled
&&
1344 !ctx
->Shader
.CurrentFragmentProgram
&&
1345 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1346 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1347 !ctx
->Query
.CondRenderQuery
) {
1348 struct st_renderbuffer
*rbRead
, *rbDraw
;
1352 * Clip the read region against the src buffer bounds.
1353 * We'll still allocate a temporary buffer/texture for the original
1354 * src region size but we'll only read the region which is on-screen.
1355 * This may mean that we draw garbage pixels into the dest region, but
1362 pack
= ctx
->DefaultPacking
;
1363 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1364 return GL_TRUE
; /* all done */
1366 /* clip against dest buffer bounds and scissor box */
1367 drawX
= dstx
+ pack
.SkipPixels
;
1368 drawY
= dsty
+ pack
.SkipRows
;
1370 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1371 return GL_TRUE
; /* all done */
1373 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1374 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1376 rbRead
= st_get_color_read_renderbuffer(ctx
);
1377 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1379 if ((rbRead
!= rbDraw
||
1380 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1381 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1382 struct pipe_box srcBox
;
1384 /* flip src/dst position if needed */
1385 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1386 /* both buffers will have the same orientation */
1387 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1388 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1391 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1393 pipe
->resource_copy_region(pipe
,
1395 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1397 rbRead
->rtt_level
, &srcBox
);
1407 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1408 GLsizei width
, GLsizei height
,
1409 GLint dstx
, GLint dsty
, GLenum type
)
1411 struct st_context
*st
= st_context(ctx
);
1412 struct pipe_context
*pipe
= st
->pipe
;
1413 struct pipe_screen
*screen
= pipe
->screen
;
1414 struct st_renderbuffer
*rbRead
;
1415 void *driver_vp
, *driver_fp
;
1416 struct pipe_resource
*pt
;
1417 struct pipe_sampler_view
*sv
[2];
1418 int num_sampler_view
= 1;
1420 enum pipe_format srcFormat
, texFormat
;
1421 GLboolean invertTex
= GL_FALSE
;
1422 GLint readX
, readY
, readW
, readH
;
1423 GLuint sample_count
;
1424 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1425 struct st_fp_variant
*fpv
;
1427 st_validate_state(st
);
1429 if (type
== GL_STENCIL
) {
1430 /* can't use texturing to do stencil */
1431 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1435 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1439 * The subsequent code implements glCopyPixels by copying the source
1440 * pixels into a temporary texture that's then applied to a textured quad.
1441 * When we draw the textured quad, all the usual per-fragment operations
1447 * Get vertex/fragment shaders
1449 if (type
== GL_COLOR
) {
1450 rbRead
= st_get_color_read_renderbuffer(ctx
);
1453 fpv
= get_color_fp_variant(st
);
1454 driver_fp
= fpv
->driver_shader
;
1456 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1458 if (st
->pixel_xfer
.pixelmap_enabled
) {
1459 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1464 assert(type
== GL_DEPTH
);
1465 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1466 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1468 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1469 driver_fp
= fpv
->driver_shader
;
1471 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1474 /* update fragment program constants */
1475 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1478 if (rbRead
->Base
.Wrapped
)
1479 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
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 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1528 readW
= MAX2(0, readW
);
1529 readH
= MAX2(0, readH
);
1531 /* alloc temporary texture */
1532 pt
= alloc_texture(st
, width
, height
, texFormat
);
1536 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1538 pipe_resource_reference(&pt
, NULL
);
1542 /* Make temporary texture which is a copy of the src region.
1544 if (srcFormat
== texFormat
) {
1545 struct pipe_box src_box
;
1546 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1547 /* copy source framebuffer surface into mipmap/texture */
1548 pipe
->resource_copy_region(pipe
,
1551 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1552 rbRead
->texture
, /* src tex */
1553 rbRead
->rtt_level
, /* src lvl */
1558 /* CPU-based fallback/conversion */
1559 struct pipe_transfer
*ptRead
=
1560 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1562 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1564 readX
, readY
, readW
, readH
);
1565 struct pipe_transfer
*ptTex
;
1566 enum pipe_transfer_usage transfer_usage
;
1568 if (ST_DEBUG
& DEBUG_FALLBACK
)
1569 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1571 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1572 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1574 transfer_usage
= PIPE_TRANSFER_WRITE
;
1576 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1577 0, 0, width
, height
);
1579 /* copy image from ptRead surface to ptTex surface */
1580 if (type
== GL_COLOR
) {
1581 /* alternate path using get/put_tile() */
1582 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1583 enum pipe_format readFormat
, drawFormat
;
1584 readFormat
= util_format_linear(rbRead
->texture
->format
);
1585 drawFormat
= util_format_linear(pt
->format
);
1586 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1588 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1589 readW
, readH
, drawFormat
, buf
);
1594 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1595 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1596 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1601 pipe
->transfer_destroy(pipe
, ptRead
);
1602 pipe
->transfer_destroy(pipe
, ptTex
);
1605 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1606 * textured quad with that texture.
1608 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1609 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1614 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1616 pipe_resource_reference(&pt
, NULL
);
1617 pipe_sampler_view_reference(&sv
[0], NULL
);
1622 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1624 functions
->DrawPixels
= st_DrawPixels
;
1625 functions
->CopyPixels
= st_CopyPixels
;
1630 st_destroy_drawpix(struct st_context
*st
)
1634 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1635 if (st
->drawpix
.shaders
[i
])
1636 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1639 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1640 if (st
->drawpix
.vert_shaders
[0])
1641 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1642 if (st
->drawpix
.vert_shaders
[1])
1643 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1646 #endif /* FEATURE_drawpix */