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/mtypes.h"
39 #include "main/pack.h"
41 #include "main/readpix.h"
42 #include "main/texformat.h"
43 #include "main/teximage.h"
44 #include "main/texstore.h"
45 #include "main/glformats.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"
74 * Check if the given program is:
75 * 0: MOVE result.color, fragment.color;
79 is_passthrough_program(const struct gl_fragment_program
*prog
)
81 if (prog
->Base
.NumInstructions
== 2) {
82 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
83 if (inst
[0].Opcode
== OPCODE_MOV
&&
84 inst
[1].Opcode
== OPCODE_END
&&
85 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
86 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
87 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
88 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
89 inst
[0].SrcReg
[0].Index
== VARYING_SLOT_COL0
&&
90 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
99 * Returns a fragment program which implements the current pixel transfer ops.
101 static struct gl_fragment_program
*
102 get_glsl_pixel_transfer_program(struct st_context
*st
,
103 struct st_fragment_program
*orig
)
105 int pixelMaps
= 0, scaleAndBias
= 0;
106 struct gl_context
*ctx
= st
->ctx
;
107 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
108 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
113 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
114 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
115 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
116 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
120 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
123 /* create the colormap/texture now if not already done */
124 if (!st
->pixel_xfer
.pixelmap_texture
) {
125 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
126 st
->pixel_xfer
.pixelmap_sampler_view
=
127 st_create_texture_sampler_view(st
->pipe
,
128 st
->pixel_xfer
.pixelmap_texture
);
132 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
133 scaleAndBias
, pixelMaps
);
140 * Make fragment shader for glDraw/CopyPixels. This shader is made
141 * by combining the pixel transfer shader with the user-defined shader.
142 * \param fpIn the current/incoming fragment program
143 * \param fpOut returns the combined fragment program
146 st_make_drawpix_fragment_program(struct st_context
*st
,
147 struct gl_fragment_program
*fpIn
,
148 struct gl_fragment_program
**fpOut
)
150 struct gl_program
*newProg
;
151 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
153 if (is_passthrough_program(fpIn
)) {
154 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
155 &st
->pixel_xfer
.program
->Base
);
157 else if (stfp
->glsl_to_tgsi
!= NULL
) {
158 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
163 printf("Base program:\n");
164 _mesa_print_program(&fpIn
->Base
);
165 printf("DrawPix program:\n");
166 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
168 newProg
= _mesa_combine_programs(st
->ctx
,
169 &st
->pixel_xfer
.program
->Base
.Base
,
175 printf("Combined DrawPixels program:\n");
176 _mesa_print_program(newProg
);
177 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
178 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
179 _mesa_print_parameter_list(newProg
->Parameters
);
182 *fpOut
= (struct gl_fragment_program
*) newProg
;
187 * Create fragment program that does a TEX() instruction to get a Z and/or
188 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
189 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
190 * Pass fragment color through as-is.
191 * \return pointer to the gl_fragment program
193 struct gl_fragment_program
*
194 st_make_drawpix_z_stencil_program(struct st_context
*st
,
195 GLboolean write_depth
,
196 GLboolean write_stencil
)
198 struct gl_context
*ctx
= st
->ctx
;
199 struct gl_program
*p
;
200 struct gl_fragment_program
*fp
;
202 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
204 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
206 if (st
->drawpix
.shaders
[shaderIndex
]) {
207 /* already have the proper shader */
208 return st
->drawpix
.shaders
[shaderIndex
];
214 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
218 p
->NumInstructions
= write_depth
? 3 : 1;
219 p
->NumInstructions
+= write_stencil
? 1 : 0;
221 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
222 if (!p
->Instructions
) {
223 ctx
->Driver
.DeleteProgram(ctx
, p
);
226 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
229 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
230 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
231 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
232 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
233 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
234 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
235 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_TEX0
;
236 p
->Instructions
[ic
].TexSrcUnit
= 0;
237 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
239 /* MOV result.color, fragment.color; */
240 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
241 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
242 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
243 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
244 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_COL0
;
249 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
250 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
251 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
252 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
253 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
254 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
255 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_TEX0
;
256 p
->Instructions
[ic
].TexSrcUnit
= 1;
257 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
262 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
264 assert(ic
== p
->NumInstructions
);
266 p
->InputsRead
= VARYING_BIT_TEX0
| VARYING_BIT_COL0
;
267 p
->OutputsWritten
= 0;
269 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
270 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_COLOR
);
273 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
275 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
277 p
->SamplersUsed
|= 1 << 1;
279 fp
= (struct gl_fragment_program
*) p
;
281 /* save the new shader */
282 st
->drawpix
.shaders
[shaderIndex
] = fp
;
289 * Create a simple vertex shader that just passes through the
290 * vertex position and texcoord (and optionally, color).
293 make_passthrough_vertex_shader(struct st_context
*st
,
296 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
297 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
299 if (!st
->drawpix
.vert_shaders
[passColor
]) {
300 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
305 /* MOV result.pos, vertex.pos; */
307 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
308 ureg_DECL_vs_input( ureg
, 0 ));
310 /* MOV result.texcoord0, vertex.attr[1]; */
312 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
313 ureg_DECL_vs_input( ureg
, 1 ));
316 /* MOV result.color0, vertex.attr[2]; */
318 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
319 ureg_DECL_vs_input( ureg
, 2 ));
324 st
->drawpix
.vert_shaders
[passColor
] =
325 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
328 return st
->drawpix
.vert_shaders
[passColor
];
333 * Return a texture internalFormat for drawing/copying an image
334 * of the given format and type.
337 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
340 case GL_DEPTH_COMPONENT
:
342 case GL_UNSIGNED_SHORT
:
343 return GL_DEPTH_COMPONENT16
;
345 case GL_UNSIGNED_INT
:
346 return GL_DEPTH_COMPONENT32
;
349 if (ctx
->Extensions
.ARB_depth_buffer_float
)
350 return GL_DEPTH_COMPONENT32F
;
352 return GL_DEPTH_COMPONENT
;
355 return GL_DEPTH_COMPONENT
;
358 case GL_DEPTH_STENCIL
:
360 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
361 return GL_DEPTH32F_STENCIL8
;
363 case GL_UNSIGNED_INT_24_8
:
365 return GL_DEPTH24_STENCIL8
;
368 case GL_STENCIL_INDEX
:
369 return GL_STENCIL_INDEX
;
372 if (_mesa_is_enum_format_integer(format
)) {
376 case GL_UNSIGNED_BYTE
:
380 case GL_UNSIGNED_SHORT
:
384 case GL_UNSIGNED_INT
:
387 assert(0 && "Unexpected type in internal_format()");
388 return GL_RGBA_INTEGER
;
393 case GL_UNSIGNED_BYTE
:
394 case GL_UNSIGNED_INT_8_8_8_8
:
395 case GL_UNSIGNED_INT_8_8_8_8_REV
:
399 case GL_UNSIGNED_BYTE_3_3_2
:
400 case GL_UNSIGNED_BYTE_2_3_3_REV
:
403 case GL_UNSIGNED_SHORT_4_4_4_4
:
404 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
407 case GL_UNSIGNED_SHORT_5_6_5
:
408 case GL_UNSIGNED_SHORT_5_6_5_REV
:
411 case GL_UNSIGNED_SHORT_5_5_5_1
:
412 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
415 case GL_UNSIGNED_INT_10_10_10_2
:
416 case GL_UNSIGNED_INT_2_10_10_10_REV
:
419 case GL_UNSIGNED_SHORT
:
420 case GL_UNSIGNED_INT
:
425 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
430 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
432 case GL_HALF_FLOAT_ARB
:
434 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
435 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
440 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
441 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
443 case GL_UNSIGNED_INT_5_9_9_9_REV
:
444 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
447 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
448 assert(ctx
->Extensions
.EXT_packed_float
);
449 return GL_R11F_G11F_B10F
;
457 * Create a temporary texture to hold an image of the given size.
458 * If width, height are not POT and the driver only handles POT textures,
459 * allocate the next larger size of texture that is POT.
461 static struct pipe_resource
*
462 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
463 enum pipe_format texFormat
)
465 struct pipe_resource
*pt
;
467 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
468 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
475 * Make texture containing an image for glDrawPixels image.
476 * If 'pixels' is NULL, leave the texture image data undefined.
478 static struct pipe_resource
*
479 make_texture(struct st_context
*st
,
480 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
481 const struct gl_pixelstore_attrib
*unpack
,
482 const GLvoid
*pixels
)
484 struct gl_context
*ctx
= st
->ctx
;
485 struct pipe_context
*pipe
= st
->pipe
;
487 struct pipe_resource
*pt
;
488 enum pipe_format pipeFormat
;
489 GLenum baseInternalFormat
;
491 /* Choose a pixel format for the temp texture which will hold the
494 pipeFormat
= st_choose_matching_format(pipe
->screen
, PIPE_BIND_SAMPLER_VIEW
,
495 format
, type
, unpack
->SwapBytes
);
497 if (pipeFormat
!= PIPE_FORMAT_NONE
) {
498 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
499 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
502 /* Use the generic approach. */
503 GLenum intFormat
= internal_format(ctx
, format
, type
);
505 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
506 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
507 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
509 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
510 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
513 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
517 /* alloc temporary texture */
518 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
520 _mesa_unmap_pbo_source(ctx
, unpack
);
525 struct pipe_transfer
*transfer
;
528 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
530 /* we'll do pixel transfer in a fragment shader */
531 ctx
->_ImageTransferState
= 0x0;
533 /* map texture transfer */
534 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
535 PIPE_TRANSFER_WRITE
, 0, 0,
536 width
, height
, &transfer
);
539 /* Put image into texture transfer.
540 * Note that the image is actually going to be upside down in
541 * the texture. We deal with that with texcoords.
543 success
= _mesa_texstore(ctx
, 2, /* dims */
544 baseInternalFormat
, /* baseInternalFormat */
545 mformat
, /* gl_format */
546 transfer
->stride
, /* dstRowStride, bytes */
547 &dest
, /* destSlices */
548 width
, height
, 1, /* size */
549 format
, type
, /* src format/type */
550 pixels
, /* data source */
554 pipe_transfer_unmap(pipe
, transfer
);
559 ctx
->_ImageTransferState
= imageTransferStateSave
;
562 _mesa_unmap_pbo_source(ctx
, unpack
);
569 * Draw quad with texcoords and optional color.
570 * Coords are gallium window coords with y=0=top.
571 * \param color may be null
572 * \param invertTex if true, flip texcoords vertically
575 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
576 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
577 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
579 struct st_context
*st
= st_context(ctx
);
580 struct pipe_context
*pipe
= st
->pipe
;
581 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
582 struct pipe_resource
*buf
= NULL
;
585 if (u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
586 &buf
, (void **) &verts
) != PIPE_OK
) {
590 /* setup vertex data */
592 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
593 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
594 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
595 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
596 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
597 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
598 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
599 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
600 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
601 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
605 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
606 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
609 verts
[1][0][0] = clip_x1
;
610 verts
[1][0][1] = clip_y0
;
613 verts
[2][0][0] = clip_x1
;
614 verts
[2][0][1] = clip_y1
;
617 verts
[3][0][0] = clip_x0
;
618 verts
[3][0][1] = clip_y1
;
620 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
621 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
622 verts
[1][1][0] = sRight
;
623 verts
[1][1][1] = tTop
;
624 verts
[2][1][0] = sRight
;
625 verts
[2][1][1] = tBot
;
626 verts
[3][1][0] = sLeft
;
627 verts
[3][1][1] = tBot
;
629 /* same for all verts: */
631 for (i
= 0; i
< 4; i
++) {
632 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
633 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
634 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
635 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
636 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
637 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
638 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
639 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
643 for (i
= 0; i
< 4; i
++) {
644 verts
[i
][0][2] = z
; /*Z*/
645 verts
[i
][0][3] = 1.0f
; /*W*/
646 verts
[i
][1][2] = 0.0f
; /*R*/
647 verts
[i
][1][3] = 1.0f
; /*Q*/
652 u_upload_unmap(st
->uploader
);
653 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
654 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
658 3); /* attribs/vert */
659 pipe_resource_reference(&buf
, NULL
);
665 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
666 GLsizei width
, GLsizei height
,
667 GLfloat zoomX
, GLfloat zoomY
,
668 struct pipe_sampler_view
**sv
,
669 int num_sampler_view
,
672 const GLfloat
*color
,
674 GLboolean write_depth
, GLboolean write_stencil
)
676 struct st_context
*st
= st_context(ctx
);
677 struct pipe_context
*pipe
= st
->pipe
;
678 struct cso_context
*cso
= st
->cso_context
;
679 GLfloat x0
, y0
, x1
, y1
;
681 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
684 /* XXX if DrawPixels image is larger than max texture size, break
687 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
688 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
689 assert(width
<= maxSize
);
690 assert(height
<= maxSize
);
692 cso_save_rasterizer(cso
);
693 cso_save_viewport(cso
);
694 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
695 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
696 cso_save_fragment_shader(cso
);
697 cso_save_stream_outputs(cso
);
698 cso_save_vertex_shader(cso
);
699 cso_save_geometry_shader(cso
);
700 cso_save_vertex_elements(cso
);
701 cso_save_aux_vertex_buffer_slot(cso
);
703 cso_save_depth_stencil_alpha(cso
);
707 /* rasterizer state: just scissor */
709 struct pipe_rasterizer_state rasterizer
;
710 memset(&rasterizer
, 0, sizeof(rasterizer
));
711 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
712 ctx
->Color
._ClampFragmentColor
;
713 rasterizer
.gl_rasterization_rules
= 1;
714 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
715 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
716 cso_set_rasterizer(cso
, &rasterizer
);
720 /* Stencil writing bypasses the normal fragment pipeline to
721 * disable color writing and set stencil test to always pass.
723 struct pipe_depth_stencil_alpha_state dsa
;
724 struct pipe_blend_state blend
;
727 memset(&dsa
, 0, sizeof(dsa
));
728 dsa
.stencil
[0].enabled
= 1;
729 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
730 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
731 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
733 /* writing depth+stencil: depth test always passes */
734 dsa
.depth
.enabled
= 1;
735 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
736 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
738 cso_set_depth_stencil_alpha(cso
, &dsa
);
740 /* blend (colormask) */
741 memset(&blend
, 0, sizeof(blend
));
742 cso_set_blend(cso
, &blend
);
745 /* fragment shader state: TEX lookup program */
746 cso_set_fragment_shader_handle(cso
, driver_fp
);
748 /* vertex shader state: position + texcoord pass-through */
749 cso_set_vertex_shader_handle(cso
, driver_vp
);
751 /* geometry shader state: disabled */
752 cso_set_geometry_shader_handle(cso
, NULL
);
754 /* texture sampling state: */
756 struct pipe_sampler_state sampler
;
757 memset(&sampler
, 0, sizeof(sampler
));
758 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
759 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
760 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
761 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
762 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
763 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
764 sampler
.normalized_coords
= normalized
;
766 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 0, &sampler
);
767 if (num_sampler_view
> 1) {
768 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler
);
770 cso_single_sampler_done(cso
, PIPE_SHADER_FRAGMENT
);
773 /* viewport state: viewport matching window dims */
775 const float w
= (float) ctx
->DrawBuffer
->Width
;
776 const float h
= (float) ctx
->DrawBuffer
->Height
;
777 struct pipe_viewport_state vp
;
778 vp
.scale
[0] = 0.5f
* w
;
779 vp
.scale
[1] = -0.5f
* h
;
782 vp
.translate
[0] = 0.5f
* w
;
783 vp
.translate
[1] = 0.5f
* h
;
784 vp
.translate
[2] = 0.5f
;
785 vp
.translate
[3] = 0.0f
;
786 cso_set_viewport(cso
, &vp
);
789 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
790 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
793 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
795 /* Compute Gallium window coords (y=0=top) with pixel zoom.
796 * Recall that these coords are transformed by the current
797 * vertex shader and viewport transformation.
799 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
800 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
801 invertTex
= !invertTex
;
805 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
807 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
809 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
812 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
813 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
814 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
817 cso_restore_rasterizer(cso
);
818 cso_restore_viewport(cso
);
819 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
820 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
821 cso_restore_fragment_shader(cso
);
822 cso_restore_vertex_shader(cso
);
823 cso_restore_geometry_shader(cso
);
824 cso_restore_vertex_elements(cso
);
825 cso_restore_aux_vertex_buffer_slot(cso
);
826 cso_restore_stream_outputs(cso
);
828 cso_restore_depth_stencil_alpha(cso
);
829 cso_restore_blend(cso
);
835 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
836 * can't use a fragment shader to write stencil values.
839 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
840 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
841 const struct gl_pixelstore_attrib
*unpack
,
842 const GLvoid
*pixels
)
844 struct st_context
*st
= st_context(ctx
);
845 struct pipe_context
*pipe
= st
->pipe
;
846 struct st_renderbuffer
*strb
;
847 enum pipe_transfer_usage usage
;
848 struct pipe_transfer
*pt
;
849 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
851 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
856 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
858 /* totally clipped */
863 strb
= st_renderbuffer(ctx
->DrawBuffer
->
864 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
866 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
867 y
= ctx
->DrawBuffer
->Height
- y
- height
;
870 if (format
== GL_STENCIL_INDEX
&&
871 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
872 /* writing stencil to a combined depth+stencil buffer */
873 usage
= PIPE_TRANSFER_READ_WRITE
;
876 usage
= PIPE_TRANSFER_WRITE
;
879 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
880 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
884 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
887 sValues
= malloc(width
* sizeof(GLubyte
));
888 zValues
= malloc(width
* sizeof(GLuint
));
890 if (sValues
&& zValues
) {
892 for (row
= 0; row
< height
; row
++) {
893 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
894 GLenum destType
= GL_UNSIGNED_BYTE
;
895 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
899 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
900 type
, source
, &clippedUnpack
,
901 ctx
->_ImageTransferState
);
903 if (format
== GL_DEPTH_STENCIL
) {
905 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
906 GL_FLOAT
: GL_UNSIGNED_INT
;
908 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
909 (1 << 24) - 1, type
, source
,
914 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
915 "zoom not complete");
921 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
922 spanY
= height
- row
- 1;
928 /* now pack the stencil (and Z) values in the dest format */
929 switch (pt
->resource
->format
) {
930 case PIPE_FORMAT_S8_UINT
:
932 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
933 assert(usage
== PIPE_TRANSFER_WRITE
);
934 memcpy(dest
, sValues
, width
);
937 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
938 if (format
== GL_DEPTH_STENCIL
) {
939 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
941 assert(usage
== PIPE_TRANSFER_WRITE
);
942 for (k
= 0; k
< width
; k
++) {
943 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
947 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
949 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
950 for (k
= 0; k
< width
; k
++) {
951 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
955 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
956 if (format
== GL_DEPTH_STENCIL
) {
957 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
959 assert(usage
== PIPE_TRANSFER_WRITE
);
960 for (k
= 0; k
< width
; k
++) {
961 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
965 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
967 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
968 for (k
= 0; k
< width
; k
++) {
969 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
973 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
974 if (format
== GL_DEPTH_STENCIL
) {
975 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
976 GLfloat
*destf
= (GLfloat
*)dest
;
978 assert(usage
== PIPE_TRANSFER_WRITE
);
979 for (k
= 0; k
< width
; k
++) {
980 destf
[k
*2] = zValuesFloat
[k
];
981 dest
[k
*2+1] = sValues
[k
] & 0xff;
985 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
987 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
988 for (k
= 0; k
< width
; k
++) {
989 dest
[k
*2+1] = sValues
[k
] & 0xff;
1000 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
1006 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
1008 /* unmap the stencil buffer */
1009 pipe_transfer_unmap(pipe
, pt
);
1014 * Get fragment program variant for a glDrawPixels or glCopyPixels
1015 * command for RGBA data.
1017 static struct st_fp_variant
*
1018 get_color_fp_variant(struct st_context
*st
)
1020 struct gl_context
*ctx
= st
->ctx
;
1021 struct st_fp_variant_key key
;
1022 struct st_fp_variant
*fpv
;
1024 memset(&key
, 0, sizeof(key
));
1028 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1029 ctx
->Pixel
.RedScale
!= 1.0 ||
1030 ctx
->Pixel
.GreenBias
!= 0.0 ||
1031 ctx
->Pixel
.GreenScale
!= 1.0 ||
1032 ctx
->Pixel
.BlueBias
!= 0.0 ||
1033 ctx
->Pixel
.BlueScale
!= 1.0 ||
1034 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1035 ctx
->Pixel
.AlphaScale
!= 1.0);
1036 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1037 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1038 st
->ctx
->Color
._ClampFragmentColor
;
1040 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1047 * Get fragment program variant for a glDrawPixels or glCopyPixels
1048 * command for depth/stencil data.
1050 static struct st_fp_variant
*
1051 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1052 GLboolean write_stencil
)
1054 struct st_fp_variant_key key
;
1055 struct st_fp_variant
*fpv
;
1057 memset(&key
, 0, sizeof(key
));
1061 key
.drawpixels_z
= write_depth
;
1062 key
.drawpixels_stencil
= write_stencil
;
1064 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1071 * Clamp glDrawPixels width and height to the maximum texture size.
1074 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1075 struct gl_pixelstore_attrib
*unpack
)
1078 1 << (pipe
->screen
->get_param(pipe
->screen
,
1079 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1081 if (*width
> maxSize
) {
1082 if (unpack
->RowLength
== 0)
1083 unpack
->RowLength
= *width
;
1086 if (*height
> maxSize
) {
1093 * Called via ctx->Driver.DrawPixels()
1096 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1097 GLsizei width
, GLsizei height
,
1098 GLenum format
, GLenum type
,
1099 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1101 void *driver_vp
, *driver_fp
;
1102 struct st_context
*st
= st_context(ctx
);
1103 const GLfloat
*color
;
1104 struct pipe_context
*pipe
= st
->pipe
;
1105 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1106 struct pipe_sampler_view
*sv
[2];
1107 int num_sampler_view
= 1;
1108 struct st_fp_variant
*fpv
;
1109 struct gl_pixelstore_attrib clippedUnpack
;
1111 /* Mesa state should be up to date by now */
1112 assert(ctx
->NewState
== 0x0);
1114 st_validate_state(st
);
1116 /* Limit the size of the glDrawPixels to the max texture size.
1117 * Strictly speaking, that's not correct but since we don't handle
1118 * larger images yet, this is better than crashing.
1120 clippedUnpack
= *unpack
;
1121 unpack
= &clippedUnpack
;
1122 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1124 if (format
== GL_DEPTH_STENCIL
)
1125 write_stencil
= write_depth
= GL_TRUE
;
1126 else if (format
== GL_STENCIL_INDEX
)
1127 write_stencil
= GL_TRUE
;
1128 else if (format
== GL_DEPTH_COMPONENT
)
1129 write_depth
= GL_TRUE
;
1131 if (write_stencil
&&
1132 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1133 /* software fallback */
1134 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1140 * Get vertex/fragment shaders
1142 if (write_depth
|| write_stencil
) {
1143 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1145 driver_fp
= fpv
->driver_shader
;
1147 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1149 color
= ctx
->Current
.RasterColor
;
1152 fpv
= get_color_fp_variant(st
);
1154 driver_fp
= fpv
->driver_shader
;
1156 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1159 if (st
->pixel_xfer
.pixelmap_enabled
) {
1160 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1165 /* update fragment program constants */
1166 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1168 /* draw with textured quad */
1170 struct pipe_resource
*pt
1171 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1173 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1176 /* Create a second sampler view to read stencil.
1177 * The stencil is written using the shader stencil export
1179 if (write_stencil
) {
1180 enum pipe_format stencil_format
=
1181 util_format_stencil_only(pt
->format
);
1183 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1188 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1190 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1195 color
, GL_FALSE
, write_depth
, write_stencil
);
1196 pipe_sampler_view_reference(&sv
[0], NULL
);
1197 if (num_sampler_view
> 1)
1198 pipe_sampler_view_reference(&sv
[1], NULL
);
1200 pipe_resource_reference(&pt
, NULL
);
1208 * Software fallback for glCopyPixels(GL_STENCIL).
1211 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1212 GLsizei width
, GLsizei height
,
1213 GLint dstx
, GLint dsty
)
1215 struct st_renderbuffer
*rbDraw
;
1216 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1217 enum pipe_transfer_usage usage
;
1218 struct pipe_transfer
*ptDraw
;
1223 buffer
= malloc(width
* height
* sizeof(ubyte
));
1225 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1229 /* Get the dest renderbuffer */
1230 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1231 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1233 /* this will do stencil pixel transfer ops */
1234 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1235 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1236 &ctx
->DefaultPacking
, buffer
);
1239 /* debug code: dump stencil values */
1241 for (row
= 0; row
< height
; row
++) {
1242 printf("%3d: ", row
);
1243 for (col
= 0; col
< width
; col
++) {
1244 printf("%02x ", buffer
[col
+ row
* width
]);
1250 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1251 usage
= PIPE_TRANSFER_READ_WRITE
;
1253 usage
= PIPE_TRANSFER_WRITE
;
1255 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1256 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1259 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1260 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1262 /* map the stencil buffer */
1263 drawMap
= pipe_transfer_map(pipe
,
1266 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1268 width
, height
, &ptDraw
);
1271 /* XXX PixelZoom not handled yet */
1272 for (i
= 0; i
< height
; i
++) {
1279 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1283 dst
= drawMap
+ y
* ptDraw
->stride
;
1284 src
= buffer
+ i
* width
;
1286 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1291 /* unmap the stencil buffer */
1292 pipe_transfer_unmap(pipe
, ptDraw
);
1297 * Return renderbuffer to use for reading color pixels for glCopyPixels
1299 static struct st_renderbuffer
*
1300 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1302 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1303 struct st_renderbuffer
*strb
=
1304 st_renderbuffer(fb
->_ColorReadBuffer
);
1310 /** Do the src/dest regions overlap? */
1312 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1313 GLsizei width
, GLsizei height
)
1315 if (srcX
+ width
<= dstX
||
1316 dstX
+ width
<= srcX
||
1317 srcY
+ height
<= dstY
||
1318 dstY
+ height
<= srcY
)
1326 * Try to do a glCopyPixels for simple cases with a blit by calling
1327 * pipe->resource_copy_region().
1329 * We can do this when we're copying color pixels (depth/stencil
1330 * eventually) with no pixel zoom, no pixel transfer ops, no
1331 * per-fragment ops, the src/dest regions don't overlap and the
1332 * src/dest pixel formats are the same.
1335 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1336 GLsizei width
, GLsizei height
,
1337 GLint dstx
, GLint dsty
, GLenum type
)
1339 struct st_context
*st
= st_context(ctx
);
1340 struct pipe_context
*pipe
= st
->pipe
;
1341 struct gl_pixelstore_attrib pack
, unpack
;
1342 GLint readX
, readY
, readW
, readH
;
1344 if (type
== GL_COLOR
&&
1345 ctx
->Pixel
.ZoomX
== 1.0 &&
1346 ctx
->Pixel
.ZoomY
== 1.0 &&
1347 ctx
->_ImageTransferState
== 0x0 &&
1348 !ctx
->Color
.BlendEnabled
&&
1349 !ctx
->Color
.AlphaEnabled
&&
1351 !ctx
->Fog
.Enabled
&&
1352 !ctx
->Stencil
.Enabled
&&
1353 !ctx
->FragmentProgram
.Enabled
&&
1354 !ctx
->VertexProgram
.Enabled
&&
1355 !ctx
->Shader
.CurrentFragmentProgram
&&
1356 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1357 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1358 !ctx
->Query
.CondRenderQuery
) {
1359 struct st_renderbuffer
*rbRead
, *rbDraw
;
1363 * Clip the read region against the src buffer bounds.
1364 * We'll still allocate a temporary buffer/texture for the original
1365 * src region size but we'll only read the region which is on-screen.
1366 * This may mean that we draw garbage pixels into the dest region, but
1373 pack
= ctx
->DefaultPacking
;
1374 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1375 return GL_TRUE
; /* all done */
1377 /* clip against dest buffer bounds and scissor box */
1378 drawX
= dstx
+ pack
.SkipPixels
;
1379 drawY
= dsty
+ pack
.SkipRows
;
1381 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1382 return GL_TRUE
; /* all done */
1384 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1385 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1387 rbRead
= st_get_color_read_renderbuffer(ctx
);
1388 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1390 if ((rbRead
!= rbDraw
||
1391 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1392 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1393 struct pipe_box srcBox
;
1395 /* flip src/dst position if needed */
1396 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1397 /* both buffers will have the same orientation */
1398 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1399 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1402 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1404 pipe
->resource_copy_region(pipe
,
1406 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1408 rbRead
->rtt_level
, &srcBox
);
1418 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1419 GLsizei width
, GLsizei height
,
1420 GLint dstx
, GLint dsty
, GLenum type
)
1422 struct st_context
*st
= st_context(ctx
);
1423 struct pipe_context
*pipe
= st
->pipe
;
1424 struct pipe_screen
*screen
= pipe
->screen
;
1425 struct st_renderbuffer
*rbRead
;
1426 void *driver_vp
, *driver_fp
;
1427 struct pipe_resource
*pt
;
1428 struct pipe_sampler_view
*sv
[2];
1429 int num_sampler_view
= 1;
1431 enum pipe_format srcFormat
, texFormat
;
1432 GLboolean invertTex
= GL_FALSE
;
1433 GLint readX
, readY
, readW
, readH
;
1434 GLuint sample_count
;
1435 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1436 struct st_fp_variant
*fpv
;
1438 st_validate_state(st
);
1440 if (type
== GL_DEPTH_STENCIL
) {
1441 /* XXX make this more efficient */
1442 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1443 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1447 if (type
== GL_STENCIL
) {
1448 /* can't use texturing to do stencil */
1449 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1453 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1457 * The subsequent code implements glCopyPixels by copying the source
1458 * pixels into a temporary texture that's then applied to a textured quad.
1459 * When we draw the textured quad, all the usual per-fragment operations
1465 * Get vertex/fragment shaders
1467 if (type
== GL_COLOR
) {
1468 rbRead
= st_get_color_read_renderbuffer(ctx
);
1471 fpv
= get_color_fp_variant(st
);
1472 driver_fp
= fpv
->driver_shader
;
1474 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1476 if (st
->pixel_xfer
.pixelmap_enabled
) {
1477 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1482 assert(type
== GL_DEPTH
);
1483 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1484 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1485 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1487 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1488 driver_fp
= fpv
->driver_shader
;
1490 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1493 /* update fragment program constants */
1494 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1496 sample_count
= rbRead
->texture
->nr_samples
;
1497 /* I believe this would be legal, presumably would need to do a resolve
1498 for color, and for depth/stencil spec says to just use one of the
1499 depth/stencil samples per pixel? Need some transfer clarifications. */
1500 assert(sample_count
< 2);
1502 srcFormat
= rbRead
->texture
->format
;
1504 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1506 PIPE_BIND_SAMPLER_VIEW
)) {
1507 texFormat
= srcFormat
;
1510 /* srcFormat can't be used as a texture format */
1511 if (type
== GL_DEPTH
) {
1512 texFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
,
1513 GL_NONE
, GL_NONE
, st
->internal_target
,
1514 sample_count
, PIPE_BIND_DEPTH_STENCIL
,
1516 assert(texFormat
!= PIPE_FORMAT_NONE
);
1519 /* default color format */
1520 texFormat
= st_choose_format(st
, GL_RGBA
,
1521 GL_NONE
, GL_NONE
, st
->internal_target
,
1522 sample_count
, PIPE_BIND_SAMPLER_VIEW
,
1524 assert(texFormat
!= PIPE_FORMAT_NONE
);
1528 /* Invert src region if needed */
1529 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1530 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1531 invertTex
= !invertTex
;
1534 /* Clip the read region against the src buffer bounds.
1535 * We'll still allocate a temporary buffer/texture for the original
1536 * src region size but we'll only read the region which is on-screen.
1537 * This may mean that we draw garbage pixels into the dest region, but
1544 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1545 /* The source region is completely out of bounds. Do nothing.
1546 * The GL spec says "Results of copies from outside the window,
1547 * or from regions of the window that are not exposed, are
1548 * hardware dependent and undefined."
1553 readW
= MAX2(0, readW
);
1554 readH
= MAX2(0, readH
);
1556 /* alloc temporary texture */
1557 pt
= alloc_texture(st
, width
, height
, texFormat
);
1561 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1563 pipe_resource_reference(&pt
, NULL
);
1567 /* Make temporary texture which is a copy of the src region.
1569 if (srcFormat
== texFormat
) {
1570 struct pipe_box src_box
;
1571 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1572 /* copy source framebuffer surface into mipmap/texture */
1573 pipe
->resource_copy_region(pipe
,
1576 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1577 rbRead
->texture
, /* src tex */
1578 rbRead
->rtt_level
, /* src lvl */
1583 /* CPU-based fallback/conversion */
1584 struct pipe_transfer
*ptRead
;
1586 pipe_transfer_map(st
->pipe
, rbRead
->texture
,
1588 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1590 readX
, readY
, readW
, readH
, &ptRead
);
1591 struct pipe_transfer
*ptTex
;
1593 enum pipe_transfer_usage transfer_usage
;
1595 if (ST_DEBUG
& DEBUG_FALLBACK
)
1596 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1598 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1599 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1601 transfer_usage
= PIPE_TRANSFER_WRITE
;
1603 mapTex
= pipe_transfer_map(st
->pipe
, pt
, 0, 0, transfer_usage
,
1604 0, 0, width
, height
, &ptTex
);
1606 /* copy image from ptRead surface to ptTex surface */
1607 if (type
== GL_COLOR
) {
1608 /* alternate path using get/put_tile() */
1609 GLfloat
*buf
= malloc(width
* height
* 4 * sizeof(GLfloat
));
1610 enum pipe_format readFormat
, drawFormat
;
1611 readFormat
= util_format_linear(rbRead
->texture
->format
);
1612 drawFormat
= util_format_linear(pt
->format
);
1613 pipe_get_tile_rgba_format(ptRead
, mapRead
, 0, 0, readW
, readH
,
1615 pipe_put_tile_rgba_format(ptTex
, mapTex
, pack
.SkipPixels
,
1617 readW
, readH
, drawFormat
, buf
);
1622 GLuint
*buf
= malloc(width
* height
* sizeof(GLuint
));
1623 pipe_get_tile_z(ptRead
, mapRead
, 0, 0, readW
, readH
, buf
);
1624 pipe_put_tile_z(ptTex
, mapTex
, pack
.SkipPixels
, pack
.SkipRows
,
1629 pipe
->transfer_unmap(pipe
, ptRead
);
1630 pipe
->transfer_unmap(pipe
, ptTex
);
1633 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1634 * textured quad with that texture.
1636 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1637 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1642 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1644 pipe_resource_reference(&pt
, NULL
);
1645 pipe_sampler_view_reference(&sv
[0], NULL
);
1650 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1652 functions
->DrawPixels
= st_DrawPixels
;
1653 functions
->CopyPixels
= st_CopyPixels
;
1658 st_destroy_drawpix(struct st_context
*st
)
1662 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1663 if (st
->drawpix
.shaders
[i
])
1664 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1667 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1668 if (st
->drawpix
.vert_shaders
[0])
1669 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1670 if (st
->drawpix
.vert_shaders
[1])
1671 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);