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/readpix.h"
42 #include "main/texformat.h"
43 #include "main/teximage.h"
44 #include "main/texstore.h"
45 #include "program/program.h"
46 #include "program/prog_print.h"
47 #include "program/prog_instruction.h"
50 #include "st_atom_constbuf.h"
51 #include "st_cb_drawpixels.h"
52 #include "st_cb_readpixels.h"
53 #include "st_cb_fbo.h"
54 #include "st_context.h"
56 #include "st_format.h"
57 #include "st_program.h"
58 #include "st_texture.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "tgsi/tgsi_ureg.h"
63 #include "util/u_draw_quad.h"
64 #include "util/u_format.h"
65 #include "util/u_inlines.h"
66 #include "util/u_math.h"
67 #include "util/u_tile.h"
68 #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
== FRAG_ATTRIB_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
= FRAG_ATTRIB_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
= FRAG_ATTRIB_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
= FRAG_ATTRIB_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
= FRAG_BIT_TEX0
| FRAG_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 if (!st
->drawpix
.vert_shaders
[passColor
]) {
297 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
302 /* MOV result.pos, vertex.pos; */
304 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
305 ureg_DECL_vs_input( ureg
, 0 ));
307 /* MOV result.texcoord0, vertex.attr[1]; */
309 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
310 ureg_DECL_vs_input( ureg
, 1 ));
313 /* MOV result.color0, vertex.attr[2]; */
315 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
316 ureg_DECL_vs_input( ureg
, 2 ));
321 st
->drawpix
.vert_shaders
[passColor
] =
322 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
325 return st
->drawpix
.vert_shaders
[passColor
];
330 * Return a texture internalFormat for drawing/copying an image
331 * of the given format and type.
334 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
337 case GL_DEPTH_COMPONENT
:
339 case GL_UNSIGNED_SHORT
:
340 return GL_DEPTH_COMPONENT16
;
342 case GL_UNSIGNED_INT
:
343 return GL_DEPTH_COMPONENT32
;
346 if (ctx
->Extensions
.ARB_depth_buffer_float
)
347 return GL_DEPTH_COMPONENT32F
;
349 return GL_DEPTH_COMPONENT
;
352 return GL_DEPTH_COMPONENT
;
355 case GL_DEPTH_STENCIL
:
357 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
358 return GL_DEPTH32F_STENCIL8
;
360 case GL_UNSIGNED_INT_24_8
:
362 return GL_DEPTH24_STENCIL8
;
365 case GL_STENCIL_INDEX
:
366 return GL_STENCIL_INDEX
;
369 if (_mesa_is_integer_format(format
)) {
373 case GL_UNSIGNED_BYTE
:
377 case GL_UNSIGNED_SHORT
:
381 case GL_UNSIGNED_INT
:
384 assert(0 && "Unexpected type in internal_format()");
385 return GL_RGBA_INTEGER
;
390 case GL_UNSIGNED_BYTE
:
391 case GL_UNSIGNED_INT_8_8_8_8
:
392 case GL_UNSIGNED_INT_8_8_8_8_REV
:
396 case GL_UNSIGNED_BYTE_3_3_2
:
397 case GL_UNSIGNED_BYTE_2_3_3_REV
:
398 case GL_UNSIGNED_SHORT_4_4_4_4
:
399 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
402 case GL_UNSIGNED_SHORT_5_6_5
:
403 case GL_UNSIGNED_SHORT_5_6_5_REV
:
404 case GL_UNSIGNED_SHORT_5_5_5_1
:
405 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
408 case GL_UNSIGNED_INT_10_10_10_2
:
409 case GL_UNSIGNED_INT_2_10_10_10_REV
:
412 case GL_UNSIGNED_SHORT
:
413 case GL_UNSIGNED_INT
:
418 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
423 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
425 case GL_HALF_FLOAT_ARB
:
427 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
428 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
433 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
434 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
436 case GL_UNSIGNED_INT_5_9_9_9_REV
:
437 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
440 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
441 assert(ctx
->Extensions
.EXT_packed_float
);
442 return GL_R11F_G11F_B10F
;
450 * Create a temporary texture to hold an image of the given size.
451 * If width, height are not POT and the driver only handles POT textures,
452 * allocate the next larger size of texture that is POT.
454 static struct pipe_resource
*
455 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
456 enum pipe_format texFormat
)
458 struct pipe_resource
*pt
;
460 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
461 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
468 * Make texture containing an image for glDrawPixels image.
469 * If 'pixels' is NULL, leave the texture image data undefined.
471 static struct pipe_resource
*
472 make_texture(struct st_context
*st
,
473 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
474 const struct gl_pixelstore_attrib
*unpack
,
475 const GLvoid
*pixels
)
477 struct gl_context
*ctx
= st
->ctx
;
478 struct pipe_context
*pipe
= st
->pipe
;
480 struct pipe_resource
*pt
;
481 enum pipe_format pipeFormat
;
482 GLenum baseInternalFormat
, intFormat
;
484 intFormat
= internal_format(ctx
, format
, type
);
485 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
487 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
488 format
, type
, GL_FALSE
);
491 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
494 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
498 /* alloc temporary texture */
499 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
501 _mesa_unmap_pbo_source(ctx
, unpack
);
506 struct pipe_transfer
*transfer
;
509 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
511 /* we'll do pixel transfer in a fragment shader */
512 ctx
->_ImageTransferState
= 0x0;
514 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
515 PIPE_TRANSFER_WRITE
, 0, 0,
518 /* map texture transfer */
519 dest
= pipe_transfer_map(pipe
, transfer
);
522 /* Put image into texture transfer.
523 * Note that the image is actually going to be upside down in
524 * the texture. We deal with that with texcoords.
526 success
= _mesa_texstore(ctx
, 2, /* dims */
527 baseInternalFormat
, /* baseInternalFormat */
528 mformat
, /* gl_format */
529 transfer
->stride
, /* dstRowStride, bytes */
530 &dest
, /* destSlices */
531 width
, height
, 1, /* size */
532 format
, type
, /* src format/type */
533 pixels
, /* data source */
537 pipe_transfer_unmap(pipe
, transfer
);
538 pipe
->transfer_destroy(pipe
, transfer
);
543 ctx
->_ImageTransferState
= imageTransferStateSave
;
546 _mesa_unmap_pbo_source(ctx
, unpack
);
553 * Draw quad with texcoords and optional color.
554 * Coords are gallium window coords with y=0=top.
555 * \param color may be null
556 * \param invertTex if true, flip texcoords vertically
559 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
560 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
561 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
563 struct st_context
*st
= st_context(ctx
);
564 struct pipe_context
*pipe
= st
->pipe
;
565 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
567 /* setup vertex data */
569 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
570 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
571 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
572 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
573 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
574 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
575 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
576 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
577 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
578 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
582 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
583 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
586 verts
[1][0][0] = clip_x1
;
587 verts
[1][0][1] = clip_y0
;
590 verts
[2][0][0] = clip_x1
;
591 verts
[2][0][1] = clip_y1
;
594 verts
[3][0][0] = clip_x0
;
595 verts
[3][0][1] = clip_y1
;
597 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
598 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
599 verts
[1][1][0] = sRight
;
600 verts
[1][1][1] = tTop
;
601 verts
[2][1][0] = sRight
;
602 verts
[2][1][1] = tBot
;
603 verts
[3][1][0] = sLeft
;
604 verts
[3][1][1] = tBot
;
606 /* same for all verts: */
608 for (i
= 0; i
< 4; i
++) {
609 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
610 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
611 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
612 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
613 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
614 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
615 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
616 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
620 for (i
= 0; i
< 4; i
++) {
621 verts
[i
][0][2] = z
; /*Z*/
622 verts
[i
][0][3] = 1.0f
; /*W*/
623 verts
[i
][1][2] = 0.0f
; /*R*/
624 verts
[i
][1][3] = 1.0f
; /*Q*/
630 struct pipe_resource
*buf
;
632 /* allocate/load buffer object with vertex data */
633 buf
= pipe_buffer_create(pipe
->screen
,
634 PIPE_BIND_VERTEX_BUFFER
,
637 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
639 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
642 3); /* attribs/vert */
643 pipe_resource_reference(&buf
, NULL
);
650 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
651 GLsizei width
, GLsizei height
,
652 GLfloat zoomX
, GLfloat zoomY
,
653 struct pipe_sampler_view
**sv
,
654 int num_sampler_view
,
657 const GLfloat
*color
,
659 GLboolean write_depth
, GLboolean write_stencil
)
661 struct st_context
*st
= st_context(ctx
);
662 struct pipe_context
*pipe
= st
->pipe
;
663 struct cso_context
*cso
= st
->cso_context
;
664 GLfloat x0
, y0
, x1
, y1
;
666 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
669 /* XXX if DrawPixels image is larger than max texture size, break
672 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
673 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
674 assert(width
<= maxSize
);
675 assert(height
<= maxSize
);
677 cso_save_rasterizer(cso
);
678 cso_save_viewport(cso
);
679 cso_save_samplers(cso
);
680 cso_save_fragment_sampler_views(cso
);
681 cso_save_fragment_shader(cso
);
682 cso_save_stream_outputs(cso
);
683 cso_save_vertex_shader(cso
);
684 cso_save_geometry_shader(cso
);
685 cso_save_vertex_elements(cso
);
686 cso_save_vertex_buffers(cso
);
688 cso_save_depth_stencil_alpha(cso
);
692 /* rasterizer state: just scissor */
694 struct pipe_rasterizer_state rasterizer
;
695 memset(&rasterizer
, 0, sizeof(rasterizer
));
696 rasterizer
.clamp_fragment_color
= ctx
->Color
._ClampFragmentColor
;
697 rasterizer
.gl_rasterization_rules
= 1;
698 rasterizer
.depth_clip
= !ctx
->Transform
.DepthClamp
;
699 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
700 cso_set_rasterizer(cso
, &rasterizer
);
704 /* Stencil writing bypasses the normal fragment pipeline to
705 * disable color writing and set stencil test to always pass.
707 struct pipe_depth_stencil_alpha_state dsa
;
708 struct pipe_blend_state blend
;
711 memset(&dsa
, 0, sizeof(dsa
));
712 dsa
.stencil
[0].enabled
= 1;
713 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
714 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
715 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
717 /* writing depth+stencil: depth test always passes */
718 dsa
.depth
.enabled
= 1;
719 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
720 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
722 cso_set_depth_stencil_alpha(cso
, &dsa
);
724 /* blend (colormask) */
725 memset(&blend
, 0, sizeof(blend
));
726 cso_set_blend(cso
, &blend
);
729 /* fragment shader state: TEX lookup program */
730 cso_set_fragment_shader_handle(cso
, driver_fp
);
732 /* vertex shader state: position + texcoord pass-through */
733 cso_set_vertex_shader_handle(cso
, driver_vp
);
735 /* geometry shader state: disabled */
736 cso_set_geometry_shader_handle(cso
, NULL
);
738 /* texture sampling state: */
740 struct pipe_sampler_state sampler
;
741 memset(&sampler
, 0, sizeof(sampler
));
742 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
743 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
744 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
745 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
746 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
747 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
748 sampler
.normalized_coords
= normalized
;
750 cso_single_sampler(cso
, 0, &sampler
);
751 if (num_sampler_view
> 1) {
752 cso_single_sampler(cso
, 1, &sampler
);
754 cso_single_sampler_done(cso
);
757 /* viewport state: viewport matching window dims */
759 const float w
= (float) ctx
->DrawBuffer
->Width
;
760 const float h
= (float) ctx
->DrawBuffer
->Height
;
761 struct pipe_viewport_state vp
;
762 vp
.scale
[0] = 0.5f
* w
;
763 vp
.scale
[1] = -0.5f
* h
;
766 vp
.translate
[0] = 0.5f
* w
;
767 vp
.translate
[1] = 0.5f
* h
;
768 vp
.translate
[2] = 0.5f
;
769 vp
.translate
[3] = 0.0f
;
770 cso_set_viewport(cso
, &vp
);
773 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
774 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
777 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
779 /* Compute Gallium window coords (y=0=top) with pixel zoom.
780 * Recall that these coords are transformed by the current
781 * vertex shader and viewport transformation.
783 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
784 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
785 invertTex
= !invertTex
;
789 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
791 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
793 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
796 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
797 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
798 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
801 cso_restore_rasterizer(cso
);
802 cso_restore_viewport(cso
);
803 cso_restore_samplers(cso
);
804 cso_restore_fragment_sampler_views(cso
);
805 cso_restore_fragment_shader(cso
);
806 cso_restore_vertex_shader(cso
);
807 cso_restore_geometry_shader(cso
);
808 cso_restore_vertex_elements(cso
);
809 cso_restore_vertex_buffers(cso
);
810 cso_restore_stream_outputs(cso
);
812 cso_restore_depth_stencil_alpha(cso
);
813 cso_restore_blend(cso
);
819 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
820 * can't use a fragment shader to write stencil values.
823 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
824 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
825 const struct gl_pixelstore_attrib
*unpack
,
826 const GLvoid
*pixels
)
828 struct st_context
*st
= st_context(ctx
);
829 struct pipe_context
*pipe
= st
->pipe
;
830 struct st_renderbuffer
*strb
;
831 enum pipe_transfer_usage usage
;
832 struct pipe_transfer
*pt
;
833 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
836 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
839 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
841 /* totally clipped */
846 strb
= st_renderbuffer(ctx
->DrawBuffer
->
847 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
849 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
850 y
= ctx
->DrawBuffer
->Height
- y
- height
;
853 if(format
!= GL_DEPTH_STENCIL
&&
854 util_format_get_component_bits(strb
->format
,
855 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
856 usage
= PIPE_TRANSFER_READ_WRITE
;
858 usage
= PIPE_TRANSFER_WRITE
;
860 pt
= pipe_get_transfer(pipe
, strb
->texture
,
861 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
865 stmap
= pipe_transfer_map(pipe
, pt
);
867 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
870 /* if width > MAX_WIDTH, have to process image in chunks */
872 while (skipPixels
< width
) {
873 const GLint spanX
= skipPixels
;
874 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
876 for (row
= 0; row
< height
; row
++) {
877 GLubyte sValues
[MAX_WIDTH
];
878 GLuint zValues
[MAX_WIDTH
];
879 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
880 GLenum destType
= GL_UNSIGNED_BYTE
;
881 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
885 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
886 type
, source
, &clippedUnpack
,
887 ctx
->_ImageTransferState
);
889 if (format
== GL_DEPTH_STENCIL
) {
891 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
?
892 GL_FLOAT
: GL_UNSIGNED_INT
;
894 _mesa_unpack_depth_span(ctx
, spanWidth
, ztype
, zValues
,
895 (1 << 24) - 1, type
, source
,
900 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
901 "zoom not complete");
907 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
908 spanY
= height
- row
- 1;
914 /* now pack the stencil (and Z) values in the dest format */
915 switch (pt
->resource
->format
) {
916 case PIPE_FORMAT_S8_UINT
:
918 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
919 assert(usage
== PIPE_TRANSFER_WRITE
);
920 memcpy(dest
, sValues
, spanWidth
);
923 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
924 if (format
== GL_DEPTH_STENCIL
) {
925 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
927 assert(usage
== PIPE_TRANSFER_WRITE
);
928 for (k
= 0; k
< spanWidth
; k
++) {
929 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
933 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
935 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
936 for (k
= 0; k
< spanWidth
; k
++) {
937 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
941 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
942 if (format
== GL_DEPTH_STENCIL
) {
943 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
945 assert(usage
== PIPE_TRANSFER_WRITE
);
946 for (k
= 0; k
< spanWidth
; k
++) {
947 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
951 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
953 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
954 for (k
= 0; k
< spanWidth
; k
++) {
955 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
959 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
960 if (format
== GL_DEPTH_STENCIL
) {
961 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
962 GLfloat
*destf
= (GLfloat
*)dest
;
964 assert(usage
== PIPE_TRANSFER_WRITE
);
965 for (k
= 0; k
< spanWidth
; k
++) {
966 destf
[k
*2] = zValuesFloat
[k
];
967 dest
[k
*2+1] = sValues
[k
] & 0xff;
971 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
973 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
974 for (k
= 0; k
< spanWidth
; k
++) {
975 dest
[k
*2+1] = sValues
[k
] & 0xff;
984 skipPixels
+= spanWidth
;
987 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
989 /* unmap the stencil buffer */
990 pipe_transfer_unmap(pipe
, pt
);
991 pipe
->transfer_destroy(pipe
, pt
);
996 * Get fragment program variant for a glDrawPixels or glCopyPixels
997 * command for RGBA data.
999 static struct st_fp_variant
*
1000 get_color_fp_variant(struct st_context
*st
)
1002 struct gl_context
*ctx
= st
->ctx
;
1003 struct st_fp_variant_key key
;
1004 struct st_fp_variant
*fpv
;
1006 memset(&key
, 0, sizeof(key
));
1010 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
1011 ctx
->Pixel
.RedScale
!= 1.0 ||
1012 ctx
->Pixel
.GreenBias
!= 0.0 ||
1013 ctx
->Pixel
.GreenScale
!= 1.0 ||
1014 ctx
->Pixel
.BlueBias
!= 0.0 ||
1015 ctx
->Pixel
.BlueScale
!= 1.0 ||
1016 ctx
->Pixel
.AlphaBias
!= 0.0 ||
1017 ctx
->Pixel
.AlphaScale
!= 1.0);
1018 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
1020 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1027 * Get fragment program variant for a glDrawPixels or glCopyPixels
1028 * command for depth/stencil data.
1030 static struct st_fp_variant
*
1031 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
1032 GLboolean write_stencil
)
1034 struct st_fp_variant_key key
;
1035 struct st_fp_variant
*fpv
;
1037 memset(&key
, 0, sizeof(key
));
1041 key
.drawpixels_z
= write_depth
;
1042 key
.drawpixels_stencil
= write_stencil
;
1044 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
1051 * Called via ctx->Driver.DrawPixels()
1054 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
1055 GLsizei width
, GLsizei height
,
1056 GLenum format
, GLenum type
,
1057 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
1059 void *driver_vp
, *driver_fp
;
1060 struct st_context
*st
= st_context(ctx
);
1061 const GLfloat
*color
;
1062 struct pipe_context
*pipe
= st
->pipe
;
1063 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
1064 struct pipe_sampler_view
*sv
[2];
1065 int num_sampler_view
= 1;
1066 struct st_fp_variant
*fpv
;
1068 if (format
== GL_DEPTH_STENCIL
)
1069 write_stencil
= write_depth
= GL_TRUE
;
1070 else if (format
== GL_STENCIL_INDEX
)
1071 write_stencil
= GL_TRUE
;
1072 else if (format
== GL_DEPTH_COMPONENT
)
1073 write_depth
= GL_TRUE
;
1075 if (write_stencil
&&
1076 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
1077 /* software fallback */
1078 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1083 /* Mesa state should be up to date by now */
1084 assert(ctx
->NewState
== 0x0);
1086 st_validate_state(st
);
1089 * Get vertex/fragment shaders
1091 if (write_depth
|| write_stencil
) {
1092 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1094 driver_fp
= fpv
->driver_shader
;
1096 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1098 color
= ctx
->Current
.RasterColor
;
1101 fpv
= get_color_fp_variant(st
);
1103 driver_fp
= fpv
->driver_shader
;
1105 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1108 if (st
->pixel_xfer
.pixelmap_enabled
) {
1109 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1114 /* update fragment program constants */
1115 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1117 /* draw with textured quad */
1119 struct pipe_resource
*pt
1120 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1122 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1125 /* Create a second sampler view to read stencil.
1126 * The stencil is written using the shader stencil export
1128 if (write_stencil
) {
1129 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1131 switch (pt
->format
) {
1132 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1133 case PIPE_FORMAT_X24S8_UINT
:
1134 stencil_format
= PIPE_FORMAT_X24S8_UINT
;
1136 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1137 case PIPE_FORMAT_S8X24_UINT
:
1138 stencil_format
= PIPE_FORMAT_S8X24_UINT
;
1140 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1141 case PIPE_FORMAT_X32_S8X24_UINT
:
1142 stencil_format
= PIPE_FORMAT_X32_S8X24_UINT
;
1144 case PIPE_FORMAT_S8_UINT
:
1145 stencil_format
= PIPE_FORMAT_S8_UINT
;
1151 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1156 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1158 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1163 color
, GL_FALSE
, write_depth
, write_stencil
);
1164 pipe_sampler_view_reference(&sv
[0], NULL
);
1165 if (num_sampler_view
> 1)
1166 pipe_sampler_view_reference(&sv
[1], NULL
);
1168 pipe_resource_reference(&pt
, NULL
);
1176 * Software fallback for glCopyPixels(GL_STENCIL).
1179 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1180 GLsizei width
, GLsizei height
,
1181 GLint dstx
, GLint dsty
)
1183 struct st_renderbuffer
*rbDraw
;
1184 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1185 enum pipe_transfer_usage usage
;
1186 struct pipe_transfer
*ptDraw
;
1191 buffer
= malloc(width
* height
* sizeof(ubyte
));
1193 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1197 /* Get the dest renderbuffer */
1198 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->
1199 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
1201 /* this will do stencil pixel transfer ops */
1202 _mesa_readpixels(ctx
, srcx
, srcy
, width
, height
,
1203 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1204 &ctx
->DefaultPacking
, buffer
);
1207 /* debug code: dump stencil values */
1209 for (row
= 0; row
< height
; row
++) {
1210 printf("%3d: ", row
);
1211 for (col
= 0; col
< width
; col
++) {
1212 printf("%02x ", buffer
[col
+ row
* width
]);
1218 if (util_format_get_component_bits(rbDraw
->format
,
1219 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1220 usage
= PIPE_TRANSFER_READ_WRITE
;
1222 usage
= PIPE_TRANSFER_WRITE
;
1224 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1225 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1228 ptDraw
= pipe_get_transfer(pipe
,
1231 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1235 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1236 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1238 /* map the stencil buffer */
1239 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1242 /* XXX PixelZoom not handled yet */
1243 for (i
= 0; i
< height
; i
++) {
1250 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1254 dst
= drawMap
+ y
* ptDraw
->stride
;
1255 src
= buffer
+ i
* width
;
1257 switch (ptDraw
->resource
->format
) {
1258 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1260 uint
*dst4
= (uint
*) dst
;
1262 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1263 for (j
= 0; j
< width
; j
++) {
1264 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1269 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1271 uint
*dst4
= (uint
*) dst
;
1273 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1274 for (j
= 0; j
< width
; j
++) {
1275 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1280 case PIPE_FORMAT_S8_UINT
:
1281 assert(usage
== PIPE_TRANSFER_WRITE
);
1282 memcpy(dst
, src
, width
);
1284 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1286 uint
*dst4
= (uint
*) dst
;
1289 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1290 for (j
= 0; j
< width
; j
++) {
1291 *dst4
= src
[j
] & 0xff;
1303 /* unmap the stencil buffer */
1304 pipe_transfer_unmap(pipe
, ptDraw
);
1305 pipe
->transfer_destroy(pipe
, ptDraw
);
1310 * Return renderbuffer to use for reading color pixels for glCopyPixels
1312 static struct st_renderbuffer
*
1313 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
1315 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1316 struct st_renderbuffer
*strb
=
1317 st_renderbuffer(fb
->_ColorReadBuffer
);
1323 /** Do the src/dest regions overlap? */
1325 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1326 GLsizei width
, GLsizei height
)
1328 if (srcX
+ width
<= dstX
||
1329 dstX
+ width
<= srcX
||
1330 srcY
+ height
<= dstY
||
1331 dstY
+ height
<= srcY
)
1339 * Try to do a glCopyPixels for simple cases with a blit by calling
1340 * pipe->resource_copy_region().
1342 * We can do this when we're copying color pixels (depth/stencil
1343 * eventually) with no pixel zoom, no pixel transfer ops, no
1344 * per-fragment ops, the src/dest regions don't overlap and the
1345 * src/dest pixel formats are the same.
1348 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1349 GLsizei width
, GLsizei height
,
1350 GLint dstx
, GLint dsty
, GLenum type
)
1352 struct st_context
*st
= st_context(ctx
);
1353 struct pipe_context
*pipe
= st
->pipe
;
1354 struct gl_pixelstore_attrib pack
, unpack
;
1355 GLint readX
, readY
, readW
, readH
;
1357 if (type
== GL_COLOR
&&
1358 ctx
->Pixel
.ZoomX
== 1.0 &&
1359 ctx
->Pixel
.ZoomY
== 1.0 &&
1360 ctx
->_ImageTransferState
== 0x0 &&
1361 !ctx
->Color
.BlendEnabled
&&
1362 !ctx
->Color
.AlphaEnabled
&&
1364 !ctx
->Fog
.Enabled
&&
1365 !ctx
->Stencil
.Enabled
&&
1366 !ctx
->FragmentProgram
.Enabled
&&
1367 !ctx
->VertexProgram
.Enabled
&&
1368 !ctx
->Shader
.CurrentFragmentProgram
&&
1369 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1370 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1371 !ctx
->Query
.CondRenderQuery
) {
1372 struct st_renderbuffer
*rbRead
, *rbDraw
;
1376 * Clip the read region against the src buffer bounds.
1377 * We'll still allocate a temporary buffer/texture for the original
1378 * src region size but we'll only read the region which is on-screen.
1379 * This may mean that we draw garbage pixels into the dest region, but
1386 pack
= ctx
->DefaultPacking
;
1387 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1388 return GL_TRUE
; /* all done */
1390 /* clip against dest buffer bounds and scissor box */
1391 drawX
= dstx
+ pack
.SkipPixels
;
1392 drawY
= dsty
+ pack
.SkipRows
;
1394 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1395 return GL_TRUE
; /* all done */
1397 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1398 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1400 rbRead
= st_get_color_read_renderbuffer(ctx
);
1401 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1403 if ((rbRead
!= rbDraw
||
1404 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1405 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1406 struct pipe_box srcBox
;
1408 /* flip src/dst position if needed */
1409 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1410 /* both buffers will have the same orientation */
1411 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1412 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1415 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1417 pipe
->resource_copy_region(pipe
,
1419 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1421 rbRead
->rtt_level
, &srcBox
);
1431 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1432 GLsizei width
, GLsizei height
,
1433 GLint dstx
, GLint dsty
, GLenum type
)
1435 struct st_context
*st
= st_context(ctx
);
1436 struct pipe_context
*pipe
= st
->pipe
;
1437 struct pipe_screen
*screen
= pipe
->screen
;
1438 struct st_renderbuffer
*rbRead
;
1439 void *driver_vp
, *driver_fp
;
1440 struct pipe_resource
*pt
;
1441 struct pipe_sampler_view
*sv
[2];
1442 int num_sampler_view
= 1;
1444 enum pipe_format srcFormat
, texFormat
;
1445 GLboolean invertTex
= GL_FALSE
;
1446 GLint readX
, readY
, readW
, readH
;
1447 GLuint sample_count
;
1448 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1449 struct st_fp_variant
*fpv
;
1451 st_validate_state(st
);
1453 if (type
== GL_DEPTH_STENCIL
) {
1454 /* XXX make this more efficient */
1455 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_STENCIL
);
1456 st_CopyPixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, GL_DEPTH
);
1460 if (type
== GL_STENCIL
) {
1461 /* can't use texturing to do stencil */
1462 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1466 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1470 * The subsequent code implements glCopyPixels by copying the source
1471 * pixels into a temporary texture that's then applied to a textured quad.
1472 * When we draw the textured quad, all the usual per-fragment operations
1478 * Get vertex/fragment shaders
1480 if (type
== GL_COLOR
) {
1481 rbRead
= st_get_color_read_renderbuffer(ctx
);
1484 fpv
= get_color_fp_variant(st
);
1485 driver_fp
= fpv
->driver_shader
;
1487 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1489 if (st
->pixel_xfer
.pixelmap_enabled
) {
1490 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1495 assert(type
== GL_DEPTH
);
1496 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->
1497 Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1498 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1500 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1501 driver_fp
= fpv
->driver_shader
;
1503 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1506 /* update fragment program constants */
1507 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1509 sample_count
= rbRead
->texture
->nr_samples
;
1510 /* I believe this would be legal, presumably would need to do a resolve
1511 for color, and for depth/stencil spec says to just use one of the
1512 depth/stencil samples per pixel? Need some transfer clarifications. */
1513 assert(sample_count
< 2);
1515 srcFormat
= rbRead
->texture
->format
;
1517 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1519 PIPE_BIND_SAMPLER_VIEW
)) {
1520 texFormat
= srcFormat
;
1523 /* srcFormat can't be used as a texture format */
1524 if (type
== GL_DEPTH
) {
1525 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1526 GL_NONE
, GL_NONE
, st
->internal_target
,
1527 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1528 assert(texFormat
!= PIPE_FORMAT_NONE
);
1531 /* default color format */
1532 texFormat
= st_choose_format(screen
, GL_RGBA
,
1533 GL_NONE
, GL_NONE
, st
->internal_target
,
1534 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1535 assert(texFormat
!= PIPE_FORMAT_NONE
);
1539 /* Invert src region if needed */
1540 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1541 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1542 invertTex
= !invertTex
;
1545 /* Clip the read region against the src buffer bounds.
1546 * We'll still allocate a temporary buffer/texture for the original
1547 * src region size but we'll only read the region which is on-screen.
1548 * This may mean that we draw garbage pixels into the dest region, but
1555 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
)) {
1556 /* The source region is completely out of bounds. Do nothing.
1557 * The GL spec says "Results of copies from outside the window,
1558 * or from regions of the window that are not exposed, are
1559 * hardware dependent and undefined."
1564 readW
= MAX2(0, readW
);
1565 readH
= MAX2(0, readH
);
1567 /* alloc temporary texture */
1568 pt
= alloc_texture(st
, width
, height
, texFormat
);
1572 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1574 pipe_resource_reference(&pt
, NULL
);
1578 /* Make temporary texture which is a copy of the src region.
1580 if (srcFormat
== texFormat
) {
1581 struct pipe_box src_box
;
1582 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1583 /* copy source framebuffer surface into mipmap/texture */
1584 pipe
->resource_copy_region(pipe
,
1587 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1588 rbRead
->texture
, /* src tex */
1589 rbRead
->rtt_level
, /* src lvl */
1594 /* CPU-based fallback/conversion */
1595 struct pipe_transfer
*ptRead
=
1596 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1598 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1600 readX
, readY
, readW
, readH
);
1601 struct pipe_transfer
*ptTex
;
1602 enum pipe_transfer_usage transfer_usage
;
1604 if (ST_DEBUG
& DEBUG_FALLBACK
)
1605 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1607 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1608 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1610 transfer_usage
= PIPE_TRANSFER_WRITE
;
1612 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1613 0, 0, width
, height
);
1615 /* copy image from ptRead surface to ptTex surface */
1616 if (type
== GL_COLOR
) {
1617 /* alternate path using get/put_tile() */
1618 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1619 enum pipe_format readFormat
, drawFormat
;
1620 readFormat
= util_format_linear(rbRead
->texture
->format
);
1621 drawFormat
= util_format_linear(pt
->format
);
1622 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1624 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1625 readW
, readH
, drawFormat
, buf
);
1630 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1631 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1632 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1637 pipe
->transfer_destroy(pipe
, ptRead
);
1638 pipe
->transfer_destroy(pipe
, ptTex
);
1641 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1642 * textured quad with that texture.
1644 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1645 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1650 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1652 pipe_resource_reference(&pt
, NULL
);
1653 pipe_sampler_view_reference(&sv
[0], NULL
);
1658 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1660 functions
->DrawPixels
= st_DrawPixels
;
1661 functions
->CopyPixels
= st_CopyPixels
;
1666 st_destroy_drawpix(struct st_context
*st
)
1670 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1671 if (st
->drawpix
.shaders
[i
])
1672 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1675 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1676 if (st
->drawpix
.vert_shaders
[0])
1677 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[0]);
1678 if (st
->drawpix
.vert_shaders
[1])
1679 cso_delete_vertex_shader(st
->cso_context
, st
->drawpix
.vert_shaders
[1]);
1682 #endif /* FEATURE_drawpix */