1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/format_pack.h"
37 #include "main/macros.h"
38 #include "main/mfeatures.h"
39 #include "main/mtypes.h"
40 #include "main/pack.h"
42 #include "main/readpix.h"
43 #include "main/texformat.h"
44 #include "main/teximage.h"
45 #include "main/texstore.h"
46 #include "main/glformats.h"
47 #include "program/program.h"
48 #include "program/prog_print.h"
49 #include "program/prog_instruction.h"
52 #include "st_atom_constbuf.h"
53 #include "st_cb_drawpixels.h"
54 #include "st_cb_readpixels.h"
55 #include "st_cb_fbo.h"
56 #include "st_context.h"
58 #include "st_format.h"
59 #include "st_program.h"
60 #include "st_texture.h"
62 #include "pipe/p_context.h"
63 #include "pipe/p_defines.h"
64 #include "tgsi/tgsi_ureg.h"
65 #include "util/u_draw_quad.h"
66 #include "util/u_format.h"
67 #include "util/u_inlines.h"
68 #include "util/u_math.h"
69 #include "util/u_tile.h"
70 #include "util/u_upload_mgr.h"
71 #include "cso_cache/cso_context.h"
75 * Check if the given program is:
76 * 0: MOVE result.color, fragment.color;
80 is_passthrough_program(const struct gl_fragment_program
*prog
)
82 if (prog
->Base
.NumInstructions
== 2) {
83 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
84 if (inst
[0].Opcode
== OPCODE_MOV
&&
85 inst
[1].Opcode
== OPCODE_END
&&
86 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
87 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
88 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
89 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
90 inst
[0].SrcReg
[0].Index
== VARYING_SLOT_COL0
&&
91 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
100 * Returns a fragment program which implements the current pixel transfer ops.
102 static struct gl_fragment_program
*
103 get_glsl_pixel_transfer_program(struct st_context
*st
,
104 struct st_fragment_program
*orig
)
106 int pixelMaps
= 0, scaleAndBias
= 0;
107 struct gl_context
*ctx
= st
->ctx
;
108 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
109 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
114 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
115 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
116 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
117 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
121 pixelMaps
= ctx
->Pixel
.MapColorFlag
;
124 /* create the colormap/texture now if not already done */
125 if (!st
->pixel_xfer
.pixelmap_texture
) {
126 st
->pixel_xfer
.pixelmap_texture
= st_create_color_map_texture(ctx
);
127 st
->pixel_xfer
.pixelmap_sampler_view
=
128 st_create_texture_sampler_view(st
->pipe
,
129 st
->pixel_xfer
.pixelmap_texture
);
133 get_pixel_transfer_visitor(fp
, orig
->glsl_to_tgsi
,
134 scaleAndBias
, pixelMaps
);
141 * Make fragment shader for glDraw/CopyPixels. This shader is made
142 * by combining the pixel transfer shader with the user-defined shader.
143 * \param fpIn the current/incoming fragment program
144 * \param fpOut returns the combined fragment program
147 st_make_drawpix_fragment_program(struct st_context
*st
,
148 struct gl_fragment_program
*fpIn
,
149 struct gl_fragment_program
**fpOut
)
151 struct gl_program
*newProg
;
152 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) fpIn
;
154 if (is_passthrough_program(fpIn
)) {
155 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
156 &st
->pixel_xfer
.program
->Base
);
158 else if (stfp
->glsl_to_tgsi
!= NULL
) {
159 newProg
= (struct gl_program
*) get_glsl_pixel_transfer_program(st
, stfp
);
164 printf("Base program:\n");
165 _mesa_print_program(&fpIn
->Base
);
166 printf("DrawPix program:\n");
167 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
169 newProg
= _mesa_combine_programs(st
->ctx
,
170 &st
->pixel_xfer
.program
->Base
.Base
,
176 printf("Combined DrawPixels program:\n");
177 _mesa_print_program(newProg
);
178 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
179 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
180 _mesa_print_parameter_list(newProg
->Parameters
);
183 *fpOut
= (struct gl_fragment_program
*) newProg
;
188 * Create fragment program that does a TEX() instruction to get a Z and/or
189 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
190 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
191 * Pass fragment color through as-is.
192 * \return pointer to the gl_fragment program
194 struct gl_fragment_program
*
195 st_make_drawpix_z_stencil_program(struct st_context
*st
,
196 GLboolean write_depth
,
197 GLboolean write_stencil
)
199 struct gl_context
*ctx
= st
->ctx
;
200 struct gl_program
*p
;
201 struct gl_fragment_program
*fp
;
203 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
205 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
207 if (st
->drawpix
.shaders
[shaderIndex
]) {
208 /* already have the proper shader */
209 return st
->drawpix
.shaders
[shaderIndex
];
215 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
219 p
->NumInstructions
= write_depth
? 3 : 1;
220 p
->NumInstructions
+= write_stencil
? 1 : 0;
222 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
223 if (!p
->Instructions
) {
224 ctx
->Driver
.DeleteProgram(ctx
, p
);
227 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
230 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
231 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
232 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
233 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
234 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
235 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
236 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_TEX0
;
237 p
->Instructions
[ic
].TexSrcUnit
= 0;
238 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
240 /* MOV result.color, fragment.color; */
241 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
242 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
243 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
244 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
245 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_COL0
;
250 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
251 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
252 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
253 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
254 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
255 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
256 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_TEX0
;
257 p
->Instructions
[ic
].TexSrcUnit
= 1;
258 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
263 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
265 assert(ic
== p
->NumInstructions
);
267 p
->InputsRead
= VARYING_BIT_TEX0
| VARYING_BIT_COL0
;
268 p
->OutputsWritten
= 0;
270 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
271 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_COLOR
);
274 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
276 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
278 p
->SamplersUsed
|= 1 << 1;
280 fp
= (struct gl_fragment_program
*) p
;
282 /* save the new shader */
283 st
->drawpix
.shaders
[shaderIndex
] = fp
;
290 * Create a simple vertex shader that just passes through the
291 * vertex position and texcoord (and optionally, color).
294 make_passthrough_vertex_shader(struct st_context
*st
,
297 const unsigned texcoord_semantic
= st
->needs_texcoord_semantic
?
298 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
300 if (!st
->drawpix
.vert_shaders
[passColor
]) {
301 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
306 /* MOV result.pos, vertex.pos; */
308 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
309 ureg_DECL_vs_input( ureg
, 0 ));
311 /* MOV result.texcoord0, vertex.attr[1]; */
313 ureg_DECL_output( ureg
, texcoord_semantic
, 0 ),
314 ureg_DECL_vs_input( ureg
, 1 ));
317 /* MOV result.color0, vertex.attr[2]; */
319 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
320 ureg_DECL_vs_input( ureg
, 2 ));
325 st
->drawpix
.vert_shaders
[passColor
] =
326 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
329 return st
->drawpix
.vert_shaders
[passColor
];
334 * Return a texture internalFormat for drawing/copying an image
335 * of the given format and type.
338 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
341 case GL_DEPTH_COMPONENT
:
343 case GL_UNSIGNED_SHORT
:
344 return GL_DEPTH_COMPONENT16
;
346 case GL_UNSIGNED_INT
:
347 return GL_DEPTH_COMPONENT32
;
350 if (ctx
->Extensions
.ARB_depth_buffer_float
)
351 return GL_DEPTH_COMPONENT32F
;
353 return GL_DEPTH_COMPONENT
;
356 return GL_DEPTH_COMPONENT
;
359 case GL_DEPTH_STENCIL
:
361 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
362 return GL_DEPTH32F_STENCIL8
;
364 case GL_UNSIGNED_INT_24_8
:
366 return GL_DEPTH24_STENCIL8
;
369 case GL_STENCIL_INDEX
:
370 return GL_STENCIL_INDEX
;
373 if (_mesa_is_enum_format_integer(format
)) {
377 case GL_UNSIGNED_BYTE
:
381 case GL_UNSIGNED_SHORT
:
385 case GL_UNSIGNED_INT
:
388 assert(0 && "Unexpected type in internal_format()");
389 return GL_RGBA_INTEGER
;
394 case GL_UNSIGNED_BYTE
:
395 case GL_UNSIGNED_INT_8_8_8_8
:
396 case GL_UNSIGNED_INT_8_8_8_8_REV
:
400 case GL_UNSIGNED_BYTE_3_3_2
:
401 case GL_UNSIGNED_BYTE_2_3_3_REV
:
404 case GL_UNSIGNED_SHORT_4_4_4_4
:
405 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
408 case GL_UNSIGNED_SHORT_5_6_5
:
409 case GL_UNSIGNED_SHORT_5_6_5_REV
:
412 case GL_UNSIGNED_SHORT_5_5_5_1
:
413 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
416 case GL_UNSIGNED_INT_10_10_10_2
:
417 case GL_UNSIGNED_INT_2_10_10_10_REV
:
420 case GL_UNSIGNED_SHORT
:
421 case GL_UNSIGNED_INT
:
426 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
431 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
433 case GL_HALF_FLOAT_ARB
:
435 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
436 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
441 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
442 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
444 case GL_UNSIGNED_INT_5_9_9_9_REV
:
445 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
448 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
449 assert(ctx
->Extensions
.EXT_packed_float
);
450 return GL_R11F_G11F_B10F
;
458 * Create a temporary texture to hold an image of the given size.
459 * If width, height are not POT and the driver only handles POT textures,
460 * allocate the next larger size of texture that is POT.
462 static struct pipe_resource
*
463 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
464 enum pipe_format texFormat
)
466 struct pipe_resource
*pt
;
468 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
469 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
476 * Make texture containing an image for glDrawPixels image.
477 * If 'pixels' is NULL, leave the texture image data undefined.
479 static struct pipe_resource
*
480 make_texture(struct st_context
*st
,
481 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
482 const struct gl_pixelstore_attrib
*unpack
,
483 const GLvoid
*pixels
)
485 struct gl_context
*ctx
= st
->ctx
;
486 struct pipe_context
*pipe
= st
->pipe
;
488 struct pipe_resource
*pt
;
489 enum pipe_format pipeFormat
;
490 GLenum baseInternalFormat
;
492 /* Choose a pixel format for the temp texture which will hold the
495 pipeFormat
= st_choose_matching_format(pipe
->screen
, PIPE_BIND_SAMPLER_VIEW
,
496 format
, type
, unpack
->SwapBytes
);
498 if (pipeFormat
!= PIPE_FORMAT_NONE
) {
499 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
500 baseInternalFormat
= _mesa_get_format_base_format(mformat
);
503 /* Use the generic approach. */
504 GLenum intFormat
= internal_format(ctx
, format
, type
);
506 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
507 pipeFormat
= st_choose_format(st
, intFormat
, format
, type
,
508 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
510 assert(pipeFormat
!= PIPE_FORMAT_NONE
);
511 mformat
= st_pipe_format_to_mesa_format(pipeFormat
);
514 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
518 /* alloc temporary texture */
519 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
521 _mesa_unmap_pbo_source(ctx
, unpack
);
526 struct pipe_transfer
*transfer
;
529 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
531 /* we'll do pixel transfer in a fragment shader */
532 ctx
->_ImageTransferState
= 0x0;
534 /* map texture transfer */
535 dest
= pipe_transfer_map(pipe
, pt
, 0, 0,
536 PIPE_TRANSFER_WRITE
, 0, 0,
537 width
, height
, &transfer
);
540 /* Put image into texture transfer.
541 * Note that the image is actually going to be upside down in
542 * the texture. We deal with that with texcoords.
544 success
= _mesa_texstore(ctx
, 2, /* dims */
545 baseInternalFormat
, /* baseInternalFormat */
546 mformat
, /* gl_format */
547 transfer
->stride
, /* dstRowStride, bytes */
548 &dest
, /* destSlices */
549 width
, height
, 1, /* size */
550 format
, type
, /* src format/type */
551 pixels
, /* data source */
555 pipe_transfer_unmap(pipe
, transfer
);
560 ctx
->_ImageTransferState
= imageTransferStateSave
;
563 _mesa_unmap_pbo_source(ctx
, unpack
);
570 * Draw quad with texcoords and optional color.
571 * Coords are gallium window coords with y=0=top.
572 * \param color may be null
573 * \param invertTex if true, flip texcoords vertically
576 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
577 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
578 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
580 struct st_context
*st
= st_context(ctx
);
581 struct pipe_context
*pipe
= st
->pipe
;
582 GLfloat (*verts
)[3][4]; /* four verts, three attribs, XYZW */
583 struct pipe_resource
*buf
= NULL
;
586 if (u_upload_alloc(st
->uploader
, 0, 4 * sizeof(verts
[0]), &offset
,
587 &buf
, (void **) &verts
) != PIPE_OK
) {
591 /* setup vertex data */
593 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
594 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
595 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
596 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
597 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
598 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
599 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
600 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
601 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
602 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
606 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
607 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
610 verts
[1][0][0] = clip_x1
;
611 verts
[1][0][1] = clip_y0
;
614 verts
[2][0][0] = clip_x1
;
615 verts
[2][0][1] = clip_y1
;
618 verts
[3][0][0] = clip_x0
;
619 verts
[3][0][1] = clip_y1
;
621 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
622 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
623 verts
[1][1][0] = sRight
;
624 verts
[1][1][1] = tTop
;
625 verts
[2][1][0] = sRight
;
626 verts
[2][1][1] = tBot
;
627 verts
[3][1][0] = sLeft
;
628 verts
[3][1][1] = tBot
;
630 /* same for all verts: */
632 for (i
= 0; i
< 4; i
++) {
633 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
634 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
635 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
636 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
637 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
638 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
639 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
640 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
644 for (i
= 0; i
< 4; i
++) {
645 verts
[i
][0][2] = z
; /*Z*/
646 verts
[i
][0][3] = 1.0f
; /*W*/
647 verts
[i
][1][2] = 0.0f
; /*R*/
648 verts
[i
][1][3] = 1.0f
; /*Q*/
653 u_upload_unmap(st
->uploader
);
654 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
,
655 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
659 3); /* attribs/vert */
660 pipe_resource_reference(&buf
, NULL
);
666 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
667 GLsizei width
, GLsizei height
,
668 GLfloat zoomX
, GLfloat zoomY
,
669 struct pipe_sampler_view
**sv
,
670 int num_sampler_view
,
673 const GLfloat
*color
,
675 GLboolean write_depth
, GLboolean write_stencil
)
677 struct st_context
*st
= st_context(ctx
);
678 struct pipe_context
*pipe
= st
->pipe
;
679 struct cso_context
*cso
= st
->cso_context
;
680 GLfloat x0
, y0
, x1
, y1
;
682 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
685 /* XXX if DrawPixels image is larger than max texture size, break
688 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
689 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
690 assert(width
<= maxSize
);
691 assert(height
<= maxSize
);
693 cso_save_rasterizer(cso
);
694 cso_save_viewport(cso
);
695 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
696 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
697 cso_save_fragment_shader(cso
);
698 cso_save_stream_outputs(cso
);
699 cso_save_vertex_shader(cso
);
700 cso_save_geometry_shader(cso
);
701 cso_save_vertex_elements(cso
);
702 cso_save_aux_vertex_buffer_slot(cso
);
704 cso_save_depth_stencil_alpha(cso
);
708 /* rasterizer state: just scissor */
710 struct pipe_rasterizer_state rasterizer
;
711 memset(&rasterizer
, 0, sizeof(rasterizer
));
712 rasterizer
.clamp_fragment_color
= !st
->clamp_frag_color_in_shader
&&
713 ctx
->Color
._ClampFragmentColor
&&
714 !ctx
->DrawBuffer
->_IntegerColor
;
715 rasterizer
.gl_rasterization_rules
= 1;
716 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
717 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
718 cso_set_rasterizer(cso
, &rasterizer
);
722 /* Stencil writing bypasses the normal fragment pipeline to
723 * disable color writing and set stencil test to always pass.
725 struct pipe_depth_stencil_alpha_state dsa
;
726 struct pipe_blend_state blend
;
729 memset(&dsa
, 0, sizeof(dsa
));
730 dsa
.stencil
[0].enabled
= 1;
731 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
732 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
733 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
735 /* writing depth+stencil: depth test always passes */
736 dsa
.depth
.enabled
= 1;
737 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
738 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
740 cso_set_depth_stencil_alpha(cso
, &dsa
);
742 /* blend (colormask) */
743 memset(&blend
, 0, sizeof(blend
));
744 cso_set_blend(cso
, &blend
);
747 /* fragment shader state: TEX lookup program */
748 cso_set_fragment_shader_handle(cso
, driver_fp
);
750 /* vertex shader state: position + texcoord pass-through */
751 cso_set_vertex_shader_handle(cso
, driver_vp
);
753 /* geometry shader state: disabled */
754 cso_set_geometry_shader_handle(cso
, NULL
);
756 /* texture sampling state: */
758 struct pipe_sampler_state sampler
;
759 memset(&sampler
, 0, sizeof(sampler
));
760 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
761 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
762 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
763 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
764 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
765 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
766 sampler
.normalized_coords
= normalized
;
768 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 0, &sampler
);
769 if (num_sampler_view
> 1) {
770 cso_single_sampler(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler
);
772 cso_single_sampler_done(cso
, PIPE_SHADER_FRAGMENT
);
775 /* viewport state: viewport matching window dims */
777 const float w
= (float) ctx
->DrawBuffer
->Width
;
778 const float h
= (float) ctx
->DrawBuffer
->Height
;
779 struct pipe_viewport_state vp
;
780 vp
.scale
[0] = 0.5f
* w
;
781 vp
.scale
[1] = -0.5f
* h
;
784 vp
.translate
[0] = 0.5f
* w
;
785 vp
.translate
[1] = 0.5f
* h
;
786 vp
.translate
[2] = 0.5f
;
787 vp
.translate
[3] = 0.0f
;
788 cso_set_viewport(cso
, &vp
);
791 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
792 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
795 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num_sampler_view
, sv
);
797 /* Compute Gallium window coords (y=0=top) with pixel zoom.
798 * Recall that these coords are transformed by the current
799 * vertex shader and viewport transformation.
801 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
802 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
803 invertTex
= !invertTex
;
807 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
809 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
811 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
814 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
815 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
816 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
819 cso_restore_rasterizer(cso
);
820 cso_restore_viewport(cso
);
821 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
822 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
823 cso_restore_fragment_shader(cso
);
824 cso_restore_vertex_shader(cso
);
825 cso_restore_geometry_shader(cso
);
826 cso_restore_vertex_elements(cso
);
827 cso_restore_aux_vertex_buffer_slot(cso
);
828 cso_restore_stream_outputs(cso
);
830 cso_restore_depth_stencil_alpha(cso
);
831 cso_restore_blend(cso
);
837 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
838 * can't use a fragment shader to write stencil values.
841 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
842 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
843 const struct gl_pixelstore_attrib
*unpack
,
844 const GLvoid
*pixels
)
846 struct st_context
*st
= st_context(ctx
);
847 struct pipe_context
*pipe
= st
->pipe
;
848 struct st_renderbuffer
*strb
;
849 enum pipe_transfer_usage usage
;
850 struct pipe_transfer
*pt
;
851 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
853 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
858 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
860 /* totally clipped */
865 strb
= st_renderbuffer(ctx
->DrawBuffer
->
866 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
868 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
869 y
= ctx
->DrawBuffer
->Height
- y
- height
;
872 if (format
== GL_STENCIL_INDEX
&&
873 _mesa_is_format_packed_depth_stencil(strb
->Base
.Format
)) {
874 /* writing stencil to a combined depth+stencil buffer */
875 usage
= PIPE_TRANSFER_READ_WRITE
;
878 usage
= PIPE_TRANSFER_WRITE
;
881 stmap
= pipe_transfer_map(pipe
, strb
->texture
,
882 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
886 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
889 sValues
= malloc(width
* sizeof(GLubyte
));
890 zValues
= malloc(width
* sizeof(GLuint
));
892 if (sValues
&& zValues
) {
894 for (row
= 0; row
< height
; row
++) {
895 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
896 GLenum destType
= GL_UNSIGNED_BYTE
;
897 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
901 _mesa_unpack_stencil_span(ctx
, width
, destType
, sValues
,
902 type
, source
, &clippedUnpack
,
903 ctx
->_ImageTransferState
);
905 if (format
== GL_DEPTH_STENCIL
) {
907 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
908 GL_FLOAT
: GL_UNSIGNED_INT
;
910 _mesa_unpack_depth_span(ctx
, width
, ztype
, zValues
,
911 (1 << 24) - 1, type
, source
,
916 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
917 "zoom not complete");
923 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
924 spanY
= height
- row
- 1;
930 /* now pack the stencil (and Z) values in the dest format */
931 switch (pt
->resource
->format
) {
932 case PIPE_FORMAT_S8_UINT
:
934 ubyte
*dest
= stmap
+ spanY
* pt
->stride
;
935 assert(usage
== PIPE_TRANSFER_WRITE
);
936 memcpy(dest
, sValues
, width
);
939 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
940 if (format
== GL_DEPTH_STENCIL
) {
941 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
943 assert(usage
== PIPE_TRANSFER_WRITE
);
944 for (k
= 0; k
< width
; k
++) {
945 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
949 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
951 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
952 for (k
= 0; k
< width
; k
++) {
953 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
957 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
958 if (format
== GL_DEPTH_STENCIL
) {
959 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
961 assert(usage
== PIPE_TRANSFER_WRITE
);
962 for (k
= 0; k
< width
; k
++) {
963 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
967 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
969 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
970 for (k
= 0; k
< width
; k
++) {
971 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
975 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
976 if (format
== GL_DEPTH_STENCIL
) {
977 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
978 GLfloat
*destf
= (GLfloat
*)dest
;
980 assert(usage
== PIPE_TRANSFER_WRITE
);
981 for (k
= 0; k
< width
; k
++) {
982 destf
[k
*2] = zValuesFloat
[k
];
983 dest
[k
*2+1] = sValues
[k
] & 0xff;
987 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
);
989 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
990 for (k
= 0; k
< width
; k
++) {
991 dest
[k
*2+1] = sValues
[k
] & 0xff;
1002 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels()");
1008 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
1010 /* unmap the stencil buffer */
1011 pipe_transfer_unmap(pipe
, pt
);
1016 * Get fragment program variant for a glDrawPixels or glCopyPixels
1017 * command for RGBA data.
1019 static struct st_fp_variant
*
1020 get_color_fp_variant(struct st_context
*st
)
1022 struct gl_context
*ctx
= st
->ctx
;
1023 struct st_fp_variant_key key
;
1024 struct st_fp_variant
*fpv
;
1026 memset(&key
, 0, sizeof(key
));
1030 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1031 ctx
->Pixel
.RedScale
!= 1.0 ||
1032 ctx
->Pixel
.GreenBias
!= 0.0 ||
1033 ctx
->Pixel
.GreenScale
!= 1.0 ||
1034 ctx
->Pixel
.BlueBias
!= 0.0 ||
1035 ctx
->Pixel
.BlueScale
!= 1.0 ||
1036 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1037 ctx
->Pixel
.AlphaScale
!= 1.0);
1038 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1039 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
1040 st
->ctx
->Color
._ClampFragmentColor
&&
1041 !st
->ctx
->DrawBuffer
->_IntegerColor
;
1043 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1050 * Get fragment program variant for a glDrawPixels or glCopyPixels
1051 * command for depth/stencil data.
1053 static struct st_fp_variant
*
1054 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1055 GLboolean write_stencil
)
1057 struct st_fp_variant_key key
;
1058 struct st_fp_variant
*fpv
;
1060 memset(&key
, 0, sizeof(key
));
1064 key
.drawpixels_z
= write_depth
;
1065 key
.drawpixels_stencil
= write_stencil
;
1067 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1074 * Clamp glDrawPixels width and height to the maximum texture size.
1077 clamp_size(struct pipe_context
*pipe
, GLsizei
*width
, GLsizei
*height
,
1078 struct gl_pixelstore_attrib
*unpack
)
1081 1 << (pipe
->screen
->get_param(pipe
->screen
,
1082 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1084 if (*width
> maxSize
) {
1085 if (unpack
->RowLength
== 0)
1086 unpack
->RowLength
= *width
;
1089 if (*height
> maxSize
) {
1096 * Called via ctx->Driver.DrawPixels()
1099 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1100 GLsizei width
, GLsizei height
,
1101 GLenum format
, GLenum type
,
1102 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1104 void *driver_vp
, *driver_fp
;
1105 struct st_context
*st
= st_context(ctx
);
1106 const GLfloat
*color
;
1107 struct pipe_context
*pipe
= st
->pipe
;
1108 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1109 struct pipe_sampler_view
*sv
[2];
1110 int num_sampler_view
= 1;
1111 struct st_fp_variant
*fpv
;
1112 struct gl_pixelstore_attrib clippedUnpack
;
1114 /* Mesa state should be up to date by now */
1115 assert(ctx
->NewState
== 0x0);
1117 st_validate_state(st
);
1119 /* Limit the size of the glDrawPixels to the max texture size.
1120 * Strictly speaking, that's not correct but since we don't handle
1121 * larger images yet, this is better than crashing.
1123 clippedUnpack
= *unpack
;
1124 unpack
= &clippedUnpack
;
1125 clamp_size(st
->pipe
, &width
, &height
, &clippedUnpack
);
1127 if (format
== GL_DEPTH_STENCIL
)
1128 write_stencil
= write_depth
= GL_TRUE
;
1129 else if (format
== GL_STENCIL_INDEX
)
1130 write_stencil
= GL_TRUE
;
1131 else if (format
== GL_DEPTH_COMPONENT
)
1132 write_depth
= GL_TRUE
;
1134 if (write_stencil
&&
1135 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1136 /* software fallback */
1137 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1143 * Get vertex/fragment shaders
1145 if (write_depth
|| write_stencil
) {
1146 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1148 driver_fp
= fpv
->driver_shader
;
1150 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1152 color
= ctx
->Current
.RasterColor
;
1155 fpv
= get_color_fp_variant(st
);
1157 driver_fp
= fpv
->driver_shader
;
1159 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1162 if (st
->pixel_xfer
.pixelmap_enabled
) {
1163 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1168 /* update fragment program constants */
1169 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1171 /* draw with textured quad */
1173 struct pipe_resource
*pt
1174 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1176 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1179 /* Create a second sampler view to read stencil.
1180 * The stencil is written using the shader stencil export
1182 if (write_stencil
) {
1183 enum pipe_format stencil_format
=
1184 util_format_stencil_only(pt
->format
);
1186 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1191 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1193 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1198 color
, GL_FALSE
, write_depth
, write_stencil
);
1199 pipe_sampler_view_reference(&sv
[0], NULL
);
1200 if (num_sampler_view
> 1)
1201 pipe_sampler_view_reference(&sv
[1], NULL
);
1203 pipe_resource_reference(&pt
, NULL
);
1211 * Software fallback for glCopyPixels(GL_STENCIL).
1214 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1215 GLsizei width
, GLsizei height
,
1216 GLint dstx
, GLint dsty
)
1218 struct st_renderbuffer
*rbDraw
;
1219 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1220 enum pipe_transfer_usage usage
;
1221 struct pipe_transfer
*ptDraw
;
1226 buffer
= malloc(width
* height
* sizeof(ubyte
));
1228 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1232 /* Get the dest renderbuffer */
1233 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1234 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1236 /* this will do stencil pixel transfer ops */
1237 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1238 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1239 &ctx
->DefaultPacking
, buffer
);
1242 /* debug code: dump stencil values */
1244 for (row
= 0; row
< height
; row
++) {
1245 printf("%3d: ", row
);
1246 for (col
= 0; col
< width
; col
++) {
1247 printf("%02x ", buffer
[col
+ row
* width
]);
1253 if (_mesa_is_format_packed_depth_stencil(rbDraw
->Base
.Format
))
1254 usage
= PIPE_TRANSFER_READ_WRITE
;
1256 usage
= PIPE_TRANSFER_WRITE
;
1258 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1259 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1262 assert(util_format_get_blockwidth(rbDraw
->texture
->format
) == 1);
1263 assert(util_format_get_blockheight(rbDraw
->texture
->format
) == 1);
1265 /* map the stencil buffer */
1266 drawMap
= pipe_transfer_map(pipe
,
1269 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1271 width
, height
, &ptDraw
);
1274 /* XXX PixelZoom not handled yet */
1275 for (i
= 0; i
< height
; i
++) {
1282 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1286 dst
= drawMap
+ y
* ptDraw
->stride
;
1287 src
= buffer
+ i
* width
;
1289 _mesa_pack_ubyte_stencil_row(rbDraw
->Base
.Format
, width
, src
, dst
);
1294 /* unmap the stencil buffer */
1295 pipe_transfer_unmap(pipe
, ptDraw
);
1300 * Return renderbuffer to use for reading color pixels for glCopyPixels
1302 static struct st_renderbuffer
*
1303 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1305 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1306 struct st_renderbuffer
*strb
=
1307 st_renderbuffer(fb
->_ColorReadBuffer
);
1313 /** Do the src/dest regions overlap? */
1315 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1316 GLsizei width
, GLsizei height
)
1318 if (srcX
+ width
<= dstX
||
1319 dstX
+ width
<= srcX
||
1320 srcY
+ height
<= dstY
||
1321 dstY
+ height
<= srcY
)
1329 * Try to do a glCopyPixels for simple cases with a blit by calling
1330 * pipe->resource_copy_region().
1332 * We can do this when we're copying color pixels (depth/stencil
1333 * eventually) with no pixel zoom, no pixel transfer ops, no
1334 * per-fragment ops, the src/dest regions don't overlap and the
1335 * src/dest pixel formats are the same.
1338 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1339 GLsizei width
, GLsizei height
,
1340 GLint dstx
, GLint dsty
, GLenum type
)
1342 struct st_context
*st
= st_context(ctx
);
1343 struct pipe_context
*pipe
= st
->pipe
;
1344 struct gl_pixelstore_attrib pack
, unpack
;
1345 GLint readX
, readY
, readW
, readH
;
1347 if (type
== GL_COLOR
&&
1348 ctx
->Pixel
.ZoomX
== 1.0 &&
1349 ctx
->Pixel
.ZoomY
== 1.0 &&
1350 ctx
->_ImageTransferState
== 0x0 &&
1351 !ctx
->Color
.BlendEnabled
&&
1352 !ctx
->Color
.AlphaEnabled
&&
1354 !ctx
->Fog
.Enabled
&&
1355 !ctx
->Stencil
.Enabled
&&
1356 !ctx
->FragmentProgram
.Enabled
&&
1357 !ctx
->VertexProgram
.Enabled
&&
1358 !ctx
->Shader
.CurrentFragmentProgram
&&
1359 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1360 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1361 !ctx
->Query
.CondRenderQuery
) {
1362 struct st_renderbuffer
*rbRead
, *rbDraw
;
1366 * Clip the read region against the src buffer bounds.
1367 * We'll still allocate a temporary buffer/texture for the original
1368 * src region size but we'll only read the region which is on-screen.
1369 * This may mean that we draw garbage pixels into the dest region, but
1376 pack
= ctx
->DefaultPacking
;
1377 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1378 return GL_TRUE
; /* all done */
1380 /* clip against dest buffer bounds and scissor box */
1381 drawX
= dstx
+ pack
.SkipPixels
;
1382 drawY
= dsty
+ pack
.SkipRows
;
1384 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1385 return GL_TRUE
; /* all done */
1387 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1388 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1390 rbRead
= st_get_color_read_renderbuffer(ctx
);
1391 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1393 if ((rbRead
!= rbDraw
||
1394 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1395 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1396 struct pipe_box srcBox
;
1398 /* flip src/dst position if needed */
1399 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1400 /* both buffers will have the same orientation */
1401 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1402 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1405 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1407 pipe
->resource_copy_region(pipe
,
1409 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1411 rbRead
->rtt_level
, &srcBox
);
1421 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1422 GLsizei width
, GLsizei height
,
1423 GLint dstx
, GLint dsty
, GLenum type
)
1425 struct st_context
*st
= st_context(ctx
);
1426 struct pipe_context
*pipe
= st
->pipe
;
1427 struct pipe_screen
*screen
= pipe
->screen
;
1428 struct st_renderbuffer
*rbRead
;
1429 void *driver_vp
, *driver_fp
;
1430 struct pipe_resource
*pt
;
1431 struct pipe_sampler_view
*sv
[2];
1432 int num_sampler_view
= 1;
1434 enum pipe_format srcFormat
, texFormat
;
1435 GLboolean invertTex
= GL_FALSE
;
1436 GLint readX
, readY
, readW
, readH
;
1437 GLuint sample_count
;
1438 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1439 struct st_fp_variant
*fpv
;
1441 st_validate_state(st
);
1443 if (type
== GL_DEPTH_STENCIL
) {
1444 /* XXX make this more efficient */
1445 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1446 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1450 if (type
== GL_STENCIL
) {
1451 /* can't use texturing to do stencil */
1452 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1456 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1460 * The subsequent code implements glCopyPixels by copying the source
1461 * pixels into a temporary texture that's then applied to a textured quad.
1462 * When we draw the textured quad, all the usual per-fragment operations
1468 * Get vertex/fragment shaders
1470 if (type
== GL_COLOR
) {
1471 rbRead
= st_get_color_read_renderbuffer(ctx
);
1474 fpv
= get_color_fp_variant(st
);
1475 driver_fp
= fpv
->driver_shader
;
1477 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1479 if (st
->pixel_xfer
.pixelmap_enabled
) {
1480 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1485 assert(type
== GL_DEPTH
);
1486 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1487 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1488 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1490 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1491 driver_fp
= fpv
->driver_shader
;
1493 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1496 /* update fragment program constants */
1497 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1499 sample_count
= rbRead
->texture
->nr_samples
;
1500 /* I believe this would be legal, presumably would need to do a resolve
1501 for color, and for depth/stencil spec says to just use one of the
1502 depth/stencil samples per pixel? Need some transfer clarifications. */
1503 assert(sample_count
< 2);
1505 srcFormat
= rbRead
->texture
->format
;
1507 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1509 PIPE_BIND_SAMPLER_VIEW
)) {
1510 texFormat
= srcFormat
;
1513 /* srcFormat can't be used as a texture format */
1514 if (type
== GL_DEPTH
) {
1515 texFormat
= st_choose_format(st
, GL_DEPTH_COMPONENT
,
1516 GL_NONE
, GL_NONE
, st
->internal_target
,
1517 sample_count
, PIPE_BIND_DEPTH_STENCIL
,
1519 assert(texFormat
!= PIPE_FORMAT_NONE
);
1522 /* default color format */
1523 texFormat
= st_choose_format(st
, GL_RGBA
,
1524 GL_NONE
, GL_NONE
, st
->internal_target
,
1525 sample_count
, PIPE_BIND_SAMPLER_VIEW
,
1527 assert(texFormat
!= PIPE_FORMAT_NONE
);
1531 /* Invert src region if needed */
1532 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1533 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1534 invertTex
= !invertTex
;
1537 /* Clip the read region against the src buffer bounds.
1538 * We'll still allocate a temporary buffer/texture for the original
1539 * src region size but we'll only read the region which is on-screen.
1540 * This may mean that we draw garbage pixels into the dest region, but
1547 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1548 /* The source region is completely out of bounds. Do nothing.
1549 * The GL spec says "Results of copies from outside the window,
1550 * or from regions of the window that are not exposed, are
1551 * hardware dependent and undefined."
1556 readW
= MAX2(0, readW
);
1557 readH
= MAX2(0, readH
);
1559 /* alloc temporary texture */
1560 pt
= alloc_texture(st
, width
, height
, texFormat
);
1564 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1566 pipe_resource_reference(&pt
, NULL
);
1570 /* Make temporary texture which is a copy of the src region.
1572 if (srcFormat
== texFormat
) {
1573 struct pipe_box src_box
;
1574 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1575 /* copy source framebuffer surface into mipmap/texture */
1576 pipe
->resource_copy_region(pipe
,
1579 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1580 rbRead
->texture
, /* src tex */
1581 rbRead
->rtt_level
, /* src lvl */
1586 /* CPU-based fallback/conversion */
1587 struct pipe_transfer
*ptRead
;
1589 pipe_transfer_map(st
->pipe
, rbRead
->texture
,
1591 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1593 readX
, readY
, readW
, readH
, &ptRead
);
1594 struct pipe_transfer
*ptTex
;
1596 enum pipe_transfer_usage transfer_usage
;
1598 if (ST_DEBUG
& DEBUG_FALLBACK
)
1599 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1601 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1602 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1604 transfer_usage
= PIPE_TRANSFER_WRITE
;
1606 mapTex
= pipe_transfer_map(st
->pipe
, pt
, 0, 0, transfer_usage
,
1607 0, 0, width
, height
, &ptTex
);
1609 /* copy image from ptRead surface to ptTex surface */
1610 if (type
== GL_COLOR
) {
1611 /* alternate path using get/put_tile() */
1612 GLfloat
*buf
= malloc(width
* height
* 4 * sizeof(GLfloat
));
1613 enum pipe_format readFormat
, drawFormat
;
1614 readFormat
= util_format_linear(rbRead
->texture
->format
);
1615 drawFormat
= util_format_linear(pt
->format
);
1616 pipe_get_tile_rgba_format(ptRead
, mapRead
, 0, 0, readW
, readH
,
1618 pipe_put_tile_rgba_format(ptTex
, mapTex
, pack
.SkipPixels
,
1620 readW
, readH
, drawFormat
, buf
);
1625 GLuint
*buf
= malloc(width
* height
* sizeof(GLuint
));
1626 pipe_get_tile_z(ptRead
, mapRead
, 0, 0, readW
, readH
, buf
);
1627 pipe_put_tile_z(ptTex
, mapTex
, pack
.SkipPixels
, pack
.SkipRows
,
1632 pipe
->transfer_unmap(pipe
, ptRead
);
1633 pipe
->transfer_unmap(pipe
, ptTex
);
1636 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1637 * textured quad with that texture.
1639 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1640 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1645 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1647 pipe_resource_reference(&pt
, NULL
);
1648 pipe_sampler_view_reference(&sv
[0], NULL
);
1653 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1655 functions
->DrawPixels
= st_DrawPixels
;
1656 functions
->CopyPixels
= st_CopyPixels
;
1661 st_destroy_drawpix(struct st_context
*st
)
1665 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1666 if (st
->drawpix
.shaders
[i
])
1667 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1670 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1671 if (st
->drawpix
.vert_shaders
[0])
1672 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1673 if (st
->drawpix
.vert_shaders
[1])
1674 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);