1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/mfeatures.h"
38 #include "main/mtypes.h"
39 #include "main/pack.h"
41 #include "main/texformat.h"
42 #include "main/teximage.h"
43 #include "main/texstore.h"
44 #include "program/program.h"
45 #include "program/prog_print.h"
46 #include "program/prog_instruction.h"
49 #include "st_atom_constbuf.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_readpixels.h"
52 #include "st_cb_fbo.h"
53 #include "st_context.h"
55 #include "st_format.h"
56 #include "st_program.h"
57 #include "st_texture.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "tgsi/tgsi_ureg.h"
62 #include "util/u_draw_quad.h"
63 #include "util/u_format.h"
64 #include "util/u_inlines.h"
65 #include "util/u_math.h"
66 #include "util/u_tile.h"
67 #include "cso_cache/cso_context.h"
73 * Check if the given program is:
74 * 0: MOVE result.color, fragment.color;
78 is_passthrough_program(const struct gl_fragment_program
*prog
)
80 if (prog
->Base
.NumInstructions
== 2) {
81 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
82 if (inst
[0].Opcode
== OPCODE_MOV
&&
83 inst
[1].Opcode
== OPCODE_END
&&
84 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
85 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
86 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
87 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
88 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
89 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
99 * Make fragment shader for glDraw/CopyPixels. This shader is made
100 * by combining the pixel transfer shader with the user-defined shader.
101 * \param fpIn the current/incoming fragment program
102 * \param fpOut returns the combined fragment program
105 st_make_drawpix_fragment_program(struct st_context
*st
,
106 struct gl_fragment_program
*fpIn
,
107 struct gl_fragment_program
**fpOut
)
109 struct gl_program
*newProg
;
111 if (is_passthrough_program(fpIn
)) {
112 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
113 &st
->pixel_xfer
.program
->Base
);
118 printf("Base program:\n");
119 _mesa_print_program(&fpIn
->Base
);
120 printf("DrawPix program:\n");
121 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
123 newProg
= _mesa_combine_programs(st
->ctx
,
124 &st
->pixel_xfer
.program
->Base
.Base
,
130 printf("Combined DrawPixels program:\n");
131 _mesa_print_program(newProg
);
132 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
133 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
134 _mesa_print_parameter_list(newProg
->Parameters
);
137 *fpOut
= (struct gl_fragment_program
*) newProg
;
142 * Create fragment program that does a TEX() instruction to get a Z and/or
143 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
144 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
145 * Pass fragment color through as-is.
146 * \return pointer to the gl_fragment program
148 struct gl_fragment_program
*
149 st_make_drawpix_z_stencil_program(struct st_context
*st
,
150 GLboolean write_depth
,
151 GLboolean write_stencil
)
153 struct gl_context
*ctx
= st
->ctx
;
154 struct gl_program
*p
;
155 struct gl_fragment_program
*fp
;
157 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
159 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
161 if (st
->drawpix
.shaders
[shaderIndex
]) {
162 /* already have the proper shader */
163 return st
->drawpix
.shaders
[shaderIndex
];
169 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
173 p
->NumInstructions
= write_depth
? 2 : 1;
174 p
->NumInstructions
+= write_stencil
? 1 : 0;
176 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
177 if (!p
->Instructions
) {
178 ctx
->Driver
.DeleteProgram(ctx
, p
);
181 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
184 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
185 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
186 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
187 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
188 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
189 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
190 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
191 p
->Instructions
[ic
].TexSrcUnit
= 0;
192 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
197 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
198 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
199 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
200 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
201 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
202 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
203 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
204 p
->Instructions
[ic
].TexSrcUnit
= 1;
205 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
210 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
212 assert(ic
== p
->NumInstructions
);
214 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
215 p
->OutputsWritten
= 0;
217 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_DEPTH
);
219 p
->OutputsWritten
|= BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
221 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
223 p
->SamplersUsed
|= 1 << 1;
225 fp
= (struct gl_fragment_program
*) p
;
227 /* save the new shader */
228 st
->drawpix
.shaders
[shaderIndex
] = fp
;
235 * Create a simple vertex shader that just passes through the
236 * vertex position and texcoord (and optionally, color).
239 make_passthrough_vertex_shader(struct st_context
*st
,
242 if (!st
->drawpix
.vert_shaders
[passColor
]) {
243 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
248 /* MOV result.pos, vertex.pos; */
250 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
251 ureg_DECL_vs_input( ureg
, 0 ));
253 /* MOV result.texcoord0, vertex.attr[1]; */
255 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
256 ureg_DECL_vs_input( ureg
, 1 ));
259 /* MOV result.color0, vertex.attr[2]; */
261 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
262 ureg_DECL_vs_input( ureg
, 2 ));
267 st
->drawpix
.vert_shaders
[passColor
] =
268 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
271 return st
->drawpix
.vert_shaders
[passColor
];
276 * Return a texture base format for drawing/copying an image
277 * of the given format.
280 base_format(GLenum format
)
283 case GL_DEPTH_COMPONENT
:
284 return GL_DEPTH_COMPONENT
;
285 case GL_DEPTH_STENCIL
:
286 return GL_DEPTH_STENCIL
;
287 case GL_STENCIL_INDEX
:
288 return GL_STENCIL_INDEX
;
296 * Return a texture internalFormat for drawing/copying an image
297 * of the given format and type.
300 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
303 case GL_DEPTH_COMPONENT
:
304 return GL_DEPTH_COMPONENT
;
305 case GL_DEPTH_STENCIL
:
306 return GL_DEPTH_STENCIL
;
307 case GL_STENCIL_INDEX
:
308 return GL_STENCIL_INDEX
;
310 if (_mesa_is_integer_format(format
)) {
314 case GL_UNSIGNED_BYTE
:
318 case GL_UNSIGNED_SHORT
:
322 case GL_UNSIGNED_INT
:
325 assert(0 && "Unexpected type in internal_format()");
326 return GL_RGBA_INTEGER
;
331 case GL_UNSIGNED_BYTE
:
332 case GL_UNSIGNED_INT_8_8_8_8
:
333 case GL_UNSIGNED_INT_8_8_8_8_REV
:
337 case GL_UNSIGNED_BYTE_3_3_2
:
338 case GL_UNSIGNED_BYTE_2_3_3_REV
:
339 case GL_UNSIGNED_SHORT_4_4_4_4
:
340 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
343 case GL_UNSIGNED_SHORT_5_6_5
:
344 case GL_UNSIGNED_SHORT_5_6_5_REV
:
345 case GL_UNSIGNED_SHORT_5_5_5_1
:
346 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
349 case GL_UNSIGNED_INT_10_10_10_2
:
350 case GL_UNSIGNED_INT_2_10_10_10_REV
:
353 case GL_UNSIGNED_SHORT
:
354 case GL_UNSIGNED_INT
:
359 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
364 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
366 case GL_HALF_FLOAT_ARB
:
368 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
369 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
374 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
375 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
377 case GL_UNSIGNED_INT_5_9_9_9_REV
:
378 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
381 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
382 assert(ctx
->Extensions
.EXT_packed_float
);
383 return GL_R11F_G11F_B10F
;
391 * Create a temporary texture to hold an image of the given size.
392 * If width, height are not POT and the driver only handles POT textures,
393 * allocate the next larger size of texture that is POT.
395 static struct pipe_resource
*
396 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
397 enum pipe_format texFormat
)
399 struct pipe_resource
*pt
;
401 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
402 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
409 * Make texture containing an image for glDrawPixels image.
410 * If 'pixels' is NULL, leave the texture image data undefined.
412 static struct pipe_resource
*
413 make_texture(struct st_context
*st
,
414 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
415 const struct gl_pixelstore_attrib
*unpack
,
416 const GLvoid
*pixels
)
418 struct gl_context
*ctx
= st
->ctx
;
419 struct pipe_context
*pipe
= st
->pipe
;
421 struct pipe_resource
*pt
;
422 enum pipe_format pipeFormat
;
423 GLenum baseInternalFormat
, intFormat
;
425 intFormat
= internal_format(ctx
, format
, type
);
426 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
428 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
429 format
, type
, GL_FALSE
);
432 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
435 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
439 /* alloc temporary texture */
440 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
442 _mesa_unmap_pbo_source(ctx
, unpack
);
447 struct pipe_transfer
*transfer
;
448 static const GLuint dstImageOffsets
= 0;
451 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
453 /* we'll do pixel transfer in a fragment shader */
454 ctx
->_ImageTransferState
= 0x0;
456 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
457 PIPE_TRANSFER_WRITE
, 0, 0,
460 /* map texture transfer */
461 dest
= pipe_transfer_map(pipe
, transfer
);
464 /* Put image into texture transfer.
465 * Note that the image is actually going to be upside down in
466 * the texture. We deal with that with texcoords.
468 success
= _mesa_texstore(ctx
, 2, /* dims */
469 baseInternalFormat
, /* baseInternalFormat */
470 mformat
, /* gl_format */
472 0, 0, 0, /* dstX/Y/Zoffset */
473 transfer
->stride
, /* dstRowStride, bytes */
474 &dstImageOffsets
, /* dstImageOffsets */
475 width
, height
, 1, /* size */
476 format
, type
, /* src format/type */
477 pixels
, /* data source */
481 pipe_transfer_unmap(pipe
, transfer
);
482 pipe
->transfer_destroy(pipe
, transfer
);
487 ctx
->_ImageTransferState
= imageTransferStateSave
;
490 _mesa_unmap_pbo_source(ctx
, unpack
);
497 * Draw quad with texcoords and optional color.
498 * Coords are gallium window coords with y=0=top.
499 * \param color may be null
500 * \param invertTex if true, flip texcoords vertically
503 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
504 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
505 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
507 struct st_context
*st
= st_context(ctx
);
508 struct pipe_context
*pipe
= st
->pipe
;
509 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
511 /* setup vertex data */
513 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
514 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
515 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
516 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
517 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
518 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
519 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
520 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
521 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
522 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
526 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
527 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
530 verts
[1][0][0] = clip_x1
;
531 verts
[1][0][1] = clip_y0
;
534 verts
[2][0][0] = clip_x1
;
535 verts
[2][0][1] = clip_y1
;
538 verts
[3][0][0] = clip_x0
;
539 verts
[3][0][1] = clip_y1
;
541 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
542 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
543 verts
[1][1][0] = sRight
;
544 verts
[1][1][1] = tTop
;
545 verts
[2][1][0] = sRight
;
546 verts
[2][1][1] = tBot
;
547 verts
[3][1][0] = sLeft
;
548 verts
[3][1][1] = tBot
;
550 /* same for all verts: */
552 for (i
= 0; i
< 4; i
++) {
553 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
554 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
555 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
556 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
557 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
558 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
559 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
560 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
564 for (i
= 0; i
< 4; i
++) {
565 verts
[i
][0][2] = z
; /*Z*/
566 verts
[i
][0][3] = 1.0f
; /*W*/
567 verts
[i
][1][2] = 0.0f
; /*R*/
568 verts
[i
][1][3] = 1.0f
; /*Q*/
574 struct pipe_resource
*buf
;
576 /* allocate/load buffer object with vertex data */
577 buf
= pipe_buffer_create(pipe
->screen
,
578 PIPE_BIND_VERTEX_BUFFER
,
581 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
583 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
586 3); /* attribs/vert */
587 pipe_resource_reference(&buf
, NULL
);
594 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
595 GLsizei width
, GLsizei height
,
596 GLfloat zoomX
, GLfloat zoomY
,
597 struct pipe_sampler_view
**sv
,
598 int num_sampler_view
,
601 const GLfloat
*color
,
603 GLboolean write_depth
, GLboolean write_stencil
)
605 struct st_context
*st
= st_context(ctx
);
606 struct pipe_context
*pipe
= st
->pipe
;
607 struct cso_context
*cso
= st
->cso_context
;
608 GLfloat x0
, y0
, x1
, y1
;
610 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
613 /* XXX if DrawPixels image is larger than max texture size, break
616 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
617 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
618 assert(width
<= maxSize
);
619 assert(height
<= maxSize
);
621 cso_save_rasterizer(cso
);
622 cso_save_viewport(cso
);
623 cso_save_samplers(cso
);
624 cso_save_fragment_sampler_views(cso
);
625 cso_save_fragment_shader(cso
);
626 cso_save_vertex_shader(cso
);
627 cso_save_vertex_elements(cso
);
628 cso_save_vertex_buffers(cso
);
630 cso_save_depth_stencil_alpha(cso
);
634 /* rasterizer state: just scissor */
636 struct pipe_rasterizer_state rasterizer
;
637 memset(&rasterizer
, 0, sizeof(rasterizer
));
638 rasterizer
.clamp_fragment_color
= ctx
->Color
._ClampFragmentColor
;
639 rasterizer
.gl_rasterization_rules
= 1;
640 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
641 cso_set_rasterizer(cso
, &rasterizer
);
645 /* Stencil writing bypasses the normal fragment pipeline to
646 * disable color writing and set stencil test to always pass.
648 struct pipe_depth_stencil_alpha_state dsa
;
649 struct pipe_blend_state blend
;
652 memset(&dsa
, 0, sizeof(dsa
));
653 dsa
.stencil
[0].enabled
= 1;
654 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
655 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
656 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
658 /* writing depth+stencil: depth test always passes */
659 dsa
.depth
.enabled
= 1;
660 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
661 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
663 cso_set_depth_stencil_alpha(cso
, &dsa
);
665 /* blend (colormask) */
666 memset(&blend
, 0, sizeof(blend
));
667 cso_set_blend(cso
, &blend
);
670 /* fragment shader state: TEX lookup program */
671 cso_set_fragment_shader_handle(cso
, driver_fp
);
673 /* vertex shader state: position + texcoord pass-through */
674 cso_set_vertex_shader_handle(cso
, driver_vp
);
677 /* texture sampling state: */
679 struct pipe_sampler_state sampler
;
680 memset(&sampler
, 0, sizeof(sampler
));
681 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
682 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
683 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
684 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
685 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
686 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
687 sampler
.normalized_coords
= normalized
;
689 cso_single_sampler(cso
, 0, &sampler
);
690 if (num_sampler_view
> 1) {
691 cso_single_sampler(cso
, 1, &sampler
);
693 cso_single_sampler_done(cso
);
696 /* viewport state: viewport matching window dims */
698 const float w
= (float) ctx
->DrawBuffer
->Width
;
699 const float h
= (float) ctx
->DrawBuffer
->Height
;
700 struct pipe_viewport_state vp
;
701 vp
.scale
[0] = 0.5f
* w
;
702 vp
.scale
[1] = -0.5f
* h
;
705 vp
.translate
[0] = 0.5f
* w
;
706 vp
.translate
[1] = 0.5f
* h
;
707 vp
.translate
[2] = 0.5f
;
708 vp
.translate
[3] = 0.0f
;
709 cso_set_viewport(cso
, &vp
);
712 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
715 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
717 /* Compute Gallium window coords (y=0=top) with pixel zoom.
718 * Recall that these coords are transformed by the current
719 * vertex shader and viewport transformation.
721 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
722 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
723 invertTex
= !invertTex
;
727 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
729 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
731 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
734 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
735 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
736 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
739 cso_restore_rasterizer(cso
);
740 cso_restore_viewport(cso
);
741 cso_restore_samplers(cso
);
742 cso_restore_fragment_sampler_views(cso
);
743 cso_restore_fragment_shader(cso
);
744 cso_restore_vertex_shader(cso
);
745 cso_restore_vertex_elements(cso
);
746 cso_restore_vertex_buffers(cso
);
748 cso_restore_depth_stencil_alpha(cso
);
749 cso_restore_blend(cso
);
755 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
756 * can't use a fragment shader to write stencil values.
759 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
760 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
761 const struct gl_pixelstore_attrib
*unpack
,
762 const GLvoid
*pixels
)
764 struct st_context
*st
= st_context(ctx
);
765 struct pipe_context
*pipe
= st
->pipe
;
766 struct st_renderbuffer
*strb
;
767 enum pipe_transfer_usage usage
;
768 struct pipe_transfer
*pt
;
769 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
772 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
775 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
777 /* totally clipped */
782 strb
= st_renderbuffer(ctx
->DrawBuffer
->
783 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
785 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
786 y
= ctx
->DrawBuffer
->Height
- y
- height
;
789 if(format
!= GL_DEPTH_STENCIL
&&
790 util_format_get_component_bits(strb
->format
,
791 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
792 usage
= PIPE_TRANSFER_READ_WRITE
;
794 usage
= PIPE_TRANSFER_WRITE
;
796 pt
= pipe_get_transfer(pipe
, strb
->texture
,
797 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
801 stmap
= pipe_transfer_map(pipe
, pt
);
803 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
806 /* if width > MAX_WIDTH, have to process image in chunks */
808 while (skipPixels
< width
) {
809 const GLint spanX
= skipPixels
;
810 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
812 for (row
= 0; row
< height
; row
++) {
813 GLubyte sValues
[MAX_WIDTH
];
814 GLuint zValues
[MAX_WIDTH
];
815 GLenum destType
= GL_UNSIGNED_BYTE
;
816 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
820 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
821 type
, source
, &clippedUnpack
,
822 ctx
->_ImageTransferState
);
824 if (format
== GL_DEPTH_STENCIL
) {
825 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
826 (1 << 24) - 1, type
, source
,
831 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
832 "zoom not complete");
838 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
839 spanY
= height
- row
- 1;
845 /* now pack the stencil (and Z) values in the dest format */
846 switch (pt
->resource
->format
) {
847 case PIPE_FORMAT_S8_USCALED
:
849 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
850 assert(usage
== PIPE_TRANSFER_WRITE
);
851 memcpy(dest
, sValues
, spanWidth
);
854 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
855 if (format
== GL_DEPTH_STENCIL
) {
856 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
858 assert(usage
== PIPE_TRANSFER_WRITE
);
859 for (k
= 0; k
< spanWidth
; k
++) {
860 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
864 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
866 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
867 for (k
= 0; k
< spanWidth
; k
++) {
868 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
872 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
873 if (format
== GL_DEPTH_STENCIL
) {
874 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
876 assert(usage
== PIPE_TRANSFER_WRITE
);
877 for (k
= 0; k
< spanWidth
; k
++) {
878 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
882 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
884 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
885 for (k
= 0; k
< spanWidth
; k
++) {
886 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
895 skipPixels
+= spanWidth
;
898 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
900 /* unmap the stencil buffer */
901 pipe_transfer_unmap(pipe
, pt
);
902 pipe
->transfer_destroy(pipe
, pt
);
907 * Get fragment program variant for a glDrawPixels or glCopyPixels
908 * command for RGBA data.
910 static struct st_fp_variant
*
911 get_color_fp_variant(struct st_context
*st
)
913 struct gl_context
*ctx
= st
->ctx
;
914 struct st_fp_variant_key key
;
915 struct st_fp_variant
*fpv
;
917 memset(&key
, 0, sizeof(key
));
921 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
922 ctx
->Pixel
.RedScale
!= 1.0 ||
923 ctx
->Pixel
.GreenBias
!= 0.0 ||
924 ctx
->Pixel
.GreenScale
!= 1.0 ||
925 ctx
->Pixel
.BlueBias
!= 0.0 ||
926 ctx
->Pixel
.BlueScale
!= 1.0 ||
927 ctx
->Pixel
.AlphaBias
!= 0.0 ||
928 ctx
->Pixel
.AlphaScale
!= 1.0);
929 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
931 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
938 * Get fragment program variant for a glDrawPixels or glCopyPixels
939 * command for depth/stencil data.
941 static struct st_fp_variant
*
942 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
943 GLboolean write_stencil
)
945 struct st_fp_variant_key key
;
946 struct st_fp_variant
*fpv
;
948 memset(&key
, 0, sizeof(key
));
952 key
.drawpixels_z
= write_depth
;
953 key
.drawpixels_stencil
= write_stencil
;
955 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
962 * Called via ctx->Driver.DrawPixels()
965 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
966 GLsizei width
, GLsizei height
,
967 GLenum format
, GLenum type
,
968 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
970 void *driver_vp
, *driver_fp
;
971 struct st_context
*st
= st_context(ctx
);
972 const GLfloat
*color
;
973 struct pipe_context
*pipe
= st
->pipe
;
974 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
975 struct pipe_sampler_view
*sv
[2];
976 int num_sampler_view
= 1;
977 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
978 struct st_fp_variant
*fpv
;
980 if (format
== GL_DEPTH_STENCIL
)
981 write_stencil
= write_depth
= GL_TRUE
;
982 else if (format
== GL_STENCIL_INDEX
)
983 write_stencil
= GL_TRUE
;
984 else if (format
== GL_DEPTH_COMPONENT
)
985 write_depth
= GL_TRUE
;
988 enum pipe_format tex_format
;
989 /* can we write to stencil if not fallback */
990 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
991 goto stencil_fallback
;
993 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
996 0, PIPE_BIND_SAMPLER_VIEW
);
997 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
998 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
999 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
1000 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
1002 stencil_format
= PIPE_FORMAT_S8_USCALED
;
1003 if (stencil_format
== PIPE_FORMAT_NONE
)
1004 goto stencil_fallback
;
1007 /* Mesa state should be up to date by now */
1008 assert(ctx
->NewState
== 0x0);
1010 st_validate_state(st
);
1013 * Get vertex/fragment shaders
1015 if (write_depth
|| write_stencil
) {
1016 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1018 driver_fp
= fpv
->driver_shader
;
1020 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1022 color
= ctx
->Current
.RasterColor
;
1025 fpv
= get_color_fp_variant(st
);
1027 driver_fp
= fpv
->driver_shader
;
1029 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1032 if (st
->pixel_xfer
.pixelmap_enabled
) {
1033 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1038 /* update fragment program constants */
1039 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1041 /* draw with textured quad */
1043 struct pipe_resource
*pt
1044 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1046 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1049 if (write_stencil
) {
1050 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1055 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1057 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1062 color
, GL_FALSE
, write_depth
, write_stencil
);
1063 pipe_sampler_view_reference(&sv
[0], NULL
);
1064 if (num_sampler_view
> 1)
1065 pipe_sampler_view_reference(&sv
[1], NULL
);
1067 pipe_resource_reference(&pt
, NULL
);
1073 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1080 * Software fallback for glCopyPixels(GL_STENCIL).
1083 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1084 GLsizei width
, GLsizei height
,
1085 GLint dstx
, GLint dsty
)
1087 struct st_renderbuffer
*rbDraw
;
1088 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1089 enum pipe_transfer_usage usage
;
1090 struct pipe_transfer
*ptDraw
;
1095 buffer
= malloc(width
* height
* sizeof(ubyte
));
1097 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1101 /* Get the dest renderbuffer. If there's a wrapper, use the
1102 * underlying renderbuffer.
1104 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1105 if (rbDraw
->Base
.Wrapped
)
1106 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1108 /* this will do stencil pixel transfer ops */
1109 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1110 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1111 &ctx
->DefaultPacking
, buffer
);
1114 /* debug code: dump stencil values */
1116 for (row
= 0; row
< height
; row
++) {
1117 printf("%3d: ", row
);
1118 for (col
= 0; col
< width
; col
++) {
1119 printf("%02x ", buffer
[col
+ row
* width
]);
1125 if (util_format_get_component_bits(rbDraw
->format
,
1126 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1127 usage
= PIPE_TRANSFER_READ_WRITE
;
1129 usage
= PIPE_TRANSFER_WRITE
;
1131 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1132 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1135 ptDraw
= pipe_get_transfer(pipe
,
1138 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1142 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1143 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1145 /* map the stencil buffer */
1146 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1149 /* XXX PixelZoom not handled yet */
1150 for (i
= 0; i
< height
; i
++) {
1157 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1161 dst
= drawMap
+ y
* ptDraw
->stride
;
1162 src
= buffer
+ i
* width
;
1164 switch (ptDraw
->resource
->format
) {
1165 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1167 uint
*dst4
= (uint
*) dst
;
1169 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1170 for (j
= 0; j
< width
; j
++) {
1171 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1176 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1178 uint
*dst4
= (uint
*) dst
;
1180 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1181 for (j
= 0; j
< width
; j
++) {
1182 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1187 case PIPE_FORMAT_S8_USCALED
:
1188 assert(usage
== PIPE_TRANSFER_WRITE
);
1189 memcpy(dst
, src
, width
);
1198 /* unmap the stencil buffer */
1199 pipe_transfer_unmap(pipe
, ptDraw
);
1200 pipe
->transfer_destroy(pipe
, ptDraw
);
1204 /** Do the src/dest regions overlap? */
1206 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1207 GLsizei width
, GLsizei height
)
1209 if (srcX
+ width
<= dstX
||
1210 dstX
+ width
<= srcX
||
1211 srcY
+ height
<= dstY
||
1212 dstY
+ height
<= srcY
)
1220 * Try to do a glCopyPixels for simple cases with a blit by calling
1221 * pipe->resource_copy_region().
1223 * We can do this when we're copying color pixels (depth/stencil
1224 * eventually) with no pixel zoom, no pixel transfer ops, no
1225 * per-fragment ops, the src/dest regions don't overlap and the
1226 * src/dest pixel formats are the same.
1229 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1230 GLsizei width
, GLsizei height
,
1231 GLint dstx
, GLint dsty
, GLenum type
)
1233 struct st_context
*st
= st_context(ctx
);
1234 struct pipe_context
*pipe
= st
->pipe
;
1235 struct gl_pixelstore_attrib pack
, unpack
;
1236 GLint readX
, readY
, readW
, readH
;
1238 if (type
== GL_COLOR
&&
1239 ctx
->Pixel
.ZoomX
== 1.0 &&
1240 ctx
->Pixel
.ZoomY
== 1.0 &&
1241 ctx
->_ImageTransferState
== 0x0 &&
1242 !ctx
->Color
.BlendEnabled
&&
1243 !ctx
->Color
.AlphaEnabled
&&
1245 !ctx
->Fog
.Enabled
&&
1246 !ctx
->Stencil
.Enabled
&&
1247 !ctx
->FragmentProgram
.Enabled
&&
1248 !ctx
->VertexProgram
.Enabled
&&
1249 !ctx
->Shader
.CurrentFragmentProgram
&&
1250 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1251 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1252 !ctx
->Query
.CondRenderQuery
) {
1253 struct st_renderbuffer
*rbRead
, *rbDraw
;
1257 * Clip the read region against the src buffer bounds.
1258 * We'll still allocate a temporary buffer/texture for the original
1259 * src region size but we'll only read the region which is on-screen.
1260 * This may mean that we draw garbage pixels into the dest region, but
1267 pack
= ctx
->DefaultPacking
;
1268 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1269 return GL_TRUE
; /* all done */
1271 /* clip against dest buffer bounds and scissor box */
1272 drawX
= dstx
+ pack
.SkipPixels
;
1273 drawY
= dsty
+ pack
.SkipRows
;
1275 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1276 return GL_TRUE
; /* all done */
1278 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1279 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1281 rbRead
= st_get_color_read_renderbuffer(ctx
);
1282 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1284 if ((rbRead
!= rbDraw
||
1285 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1286 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1287 struct pipe_box srcBox
;
1289 /* flip src/dst position if needed */
1290 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1291 /* both buffers will have the same orientation */
1292 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1293 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1296 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1298 pipe
->resource_copy_region(pipe
,
1300 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1302 rbRead
->rtt_level
, &srcBox
);
1312 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1313 GLsizei width
, GLsizei height
,
1314 GLint dstx
, GLint dsty
, GLenum type
)
1316 struct st_context
*st
= st_context(ctx
);
1317 struct pipe_context
*pipe
= st
->pipe
;
1318 struct pipe_screen
*screen
= pipe
->screen
;
1319 struct st_renderbuffer
*rbRead
;
1320 void *driver_vp
, *driver_fp
;
1321 struct pipe_resource
*pt
;
1322 struct pipe_sampler_view
*sv
[2];
1323 int num_sampler_view
= 1;
1325 enum pipe_format srcFormat
, texFormat
;
1326 GLboolean invertTex
= GL_FALSE
;
1327 GLint readX
, readY
, readW
, readH
;
1328 GLuint sample_count
;
1329 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1330 struct st_fp_variant
*fpv
;
1332 st_validate_state(st
);
1334 if (type
== GL_STENCIL
) {
1335 /* can't use texturing to do stencil */
1336 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1340 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1344 * The subsequent code implements glCopyPixels by copying the source
1345 * pixels into a temporary texture that's then applied to a textured quad.
1346 * When we draw the textured quad, all the usual per-fragment operations
1352 * Get vertex/fragment shaders
1354 if (type
== GL_COLOR
) {
1355 rbRead
= st_get_color_read_renderbuffer(ctx
);
1358 fpv
= get_color_fp_variant(st
);
1359 driver_fp
= fpv
->driver_shader
;
1361 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1363 if (st
->pixel_xfer
.pixelmap_enabled
) {
1364 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1369 assert(type
== GL_DEPTH
);
1370 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1371 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1373 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1374 driver_fp
= fpv
->driver_shader
;
1376 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1379 /* update fragment program constants */
1380 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1383 if (rbRead
->Base
.Wrapped
)
1384 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1386 sample_count
= rbRead
->texture
->nr_samples
;
1387 /* I believe this would be legal, presumably would need to do a resolve
1388 for color, and for depth/stencil spec says to just use one of the
1389 depth/stencil samples per pixel? Need some transfer clarifications. */
1390 assert(sample_count
< 2);
1392 srcFormat
= rbRead
->texture
->format
;
1394 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1396 PIPE_BIND_SAMPLER_VIEW
)) {
1397 texFormat
= srcFormat
;
1400 /* srcFormat can't be used as a texture format */
1401 if (type
== GL_DEPTH
) {
1402 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1403 GL_NONE
, GL_NONE
, st
->internal_target
,
1404 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1405 assert(texFormat
!= PIPE_FORMAT_NONE
);
1408 /* default color format */
1409 texFormat
= st_choose_format(screen
, GL_RGBA
,
1410 GL_NONE
, GL_NONE
, st
->internal_target
,
1411 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1412 assert(texFormat
!= PIPE_FORMAT_NONE
);
1416 /* Invert src region if needed */
1417 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1418 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1419 invertTex
= !invertTex
;
1422 /* Clip the read region against the src buffer bounds.
1423 * We'll still allocate a temporary buffer/texture for the original
1424 * src region size but we'll only read the region which is on-screen.
1425 * This may mean that we draw garbage pixels into the dest region, but
1432 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1433 readW
= MAX2(0, readW
);
1434 readH
= MAX2(0, readH
);
1436 /* alloc temporary texture */
1437 pt
= alloc_texture(st
, width
, height
, texFormat
);
1441 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1443 pipe_resource_reference(&pt
, NULL
);
1447 /* Make temporary texture which is a copy of the src region.
1449 if (srcFormat
== texFormat
) {
1450 struct pipe_box src_box
;
1451 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1452 /* copy source framebuffer surface into mipmap/texture */
1453 pipe
->resource_copy_region(pipe
,
1456 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1457 rbRead
->texture
, /* src tex */
1458 rbRead
->rtt_level
, /* src lvl */
1463 /* CPU-based fallback/conversion */
1464 struct pipe_transfer
*ptRead
=
1465 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1467 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1469 readX
, readY
, readW
, readH
);
1470 struct pipe_transfer
*ptTex
;
1471 enum pipe_transfer_usage transfer_usage
;
1473 if (ST_DEBUG
& DEBUG_FALLBACK
)
1474 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1476 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1477 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1479 transfer_usage
= PIPE_TRANSFER_WRITE
;
1481 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1482 0, 0, width
, height
);
1484 /* copy image from ptRead surface to ptTex surface */
1485 if (type
== GL_COLOR
) {
1486 /* alternate path using get/put_tile() */
1487 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1488 enum pipe_format readFormat
, drawFormat
;
1489 readFormat
= util_format_linear(rbRead
->texture
->format
);
1490 drawFormat
= util_format_linear(pt
->format
);
1491 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1493 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1494 readW
, readH
, drawFormat
, buf
);
1499 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1500 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1501 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1506 pipe
->transfer_destroy(pipe
, ptRead
);
1507 pipe
->transfer_destroy(pipe
, ptTex
);
1510 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1511 * textured quad with that texture.
1513 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1514 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1519 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1521 pipe_resource_reference(&pt
, NULL
);
1522 pipe_sampler_view_reference(&sv
[0], NULL
);
1527 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1529 functions
->DrawPixels
= st_DrawPixels
;
1530 functions
->CopyPixels
= st_CopyPixels
;
1535 st_destroy_drawpix(struct st_context
*st
)
1539 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1540 if (st
->drawpix
.shaders
[i
])
1541 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1544 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1545 if (st
->drawpix
.vert_shaders
[0])
1546 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1547 if (st
->drawpix
.vert_shaders
[1])
1548 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1551 #endif /* FEATURE_drawpix */