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 internalFormat for drawing/copying an image
277 * of the given format and type.
280 internal_format(struct gl_context
*ctx
, GLenum format
, GLenum type
)
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
;
290 if (_mesa_is_integer_format(format
)) {
294 case GL_UNSIGNED_BYTE
:
298 case GL_UNSIGNED_SHORT
:
302 case GL_UNSIGNED_INT
:
305 assert(0 && "Unexpected type in internal_format()");
306 return GL_RGBA_INTEGER
;
311 case GL_UNSIGNED_BYTE
:
312 case GL_UNSIGNED_INT_8_8_8_8
:
313 case GL_UNSIGNED_INT_8_8_8_8_REV
:
317 case GL_UNSIGNED_BYTE_3_3_2
:
318 case GL_UNSIGNED_BYTE_2_3_3_REV
:
319 case GL_UNSIGNED_SHORT_4_4_4_4
:
320 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
323 case GL_UNSIGNED_SHORT_5_6_5
:
324 case GL_UNSIGNED_SHORT_5_6_5_REV
:
325 case GL_UNSIGNED_SHORT_5_5_5_1
:
326 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
329 case GL_UNSIGNED_INT_10_10_10_2
:
330 case GL_UNSIGNED_INT_2_10_10_10_REV
:
333 case GL_UNSIGNED_SHORT
:
334 case GL_UNSIGNED_INT
:
339 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA8_SNORM
: GL_RGBA8
;
344 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
346 case GL_HALF_FLOAT_ARB
:
348 ctx
->Extensions
.ARB_texture_float
? GL_RGBA16F
:
349 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
354 ctx
->Extensions
.ARB_texture_float
? GL_RGBA32F
:
355 ctx
->Extensions
.EXT_texture_snorm
? GL_RGBA16_SNORM
: GL_RGBA16
;
357 case GL_UNSIGNED_INT_5_9_9_9_REV
:
358 assert(ctx
->Extensions
.EXT_texture_shared_exponent
);
361 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
362 assert(ctx
->Extensions
.EXT_packed_float
);
363 return GL_R11F_G11F_B10F
;
371 * Create a temporary texture to hold an image of the given size.
372 * If width, height are not POT and the driver only handles POT textures,
373 * allocate the next larger size of texture that is POT.
375 static struct pipe_resource
*
376 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
377 enum pipe_format texFormat
)
379 struct pipe_resource
*pt
;
381 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
382 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
389 * Make texture containing an image for glDrawPixels image.
390 * If 'pixels' is NULL, leave the texture image data undefined.
392 static struct pipe_resource
*
393 make_texture(struct st_context
*st
,
394 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
395 const struct gl_pixelstore_attrib
*unpack
,
396 const GLvoid
*pixels
)
398 struct gl_context
*ctx
= st
->ctx
;
399 struct pipe_context
*pipe
= st
->pipe
;
401 struct pipe_resource
*pt
;
402 enum pipe_format pipeFormat
;
403 GLenum baseInternalFormat
, intFormat
;
405 intFormat
= internal_format(ctx
, format
, type
);
406 baseInternalFormat
= _mesa_base_tex_format(ctx
, intFormat
);
408 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
409 format
, type
, GL_FALSE
);
412 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
415 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
419 /* alloc temporary texture */
420 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
422 _mesa_unmap_pbo_source(ctx
, unpack
);
427 struct pipe_transfer
*transfer
;
428 static const GLuint dstImageOffsets
= 0;
431 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
433 /* we'll do pixel transfer in a fragment shader */
434 ctx
->_ImageTransferState
= 0x0;
436 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
437 PIPE_TRANSFER_WRITE
, 0, 0,
440 /* map texture transfer */
441 dest
= pipe_transfer_map(pipe
, transfer
);
444 /* Put image into texture transfer.
445 * Note that the image is actually going to be upside down in
446 * the texture. We deal with that with texcoords.
448 success
= _mesa_texstore(ctx
, 2, /* dims */
449 baseInternalFormat
, /* baseInternalFormat */
450 mformat
, /* gl_format */
452 0, 0, 0, /* dstX/Y/Zoffset */
453 transfer
->stride
, /* dstRowStride, bytes */
454 &dstImageOffsets
, /* dstImageOffsets */
455 width
, height
, 1, /* size */
456 format
, type
, /* src format/type */
457 pixels
, /* data source */
461 pipe_transfer_unmap(pipe
, transfer
);
462 pipe
->transfer_destroy(pipe
, transfer
);
467 ctx
->_ImageTransferState
= imageTransferStateSave
;
470 _mesa_unmap_pbo_source(ctx
, unpack
);
477 * Draw quad with texcoords and optional color.
478 * Coords are gallium window coords with y=0=top.
479 * \param color may be null
480 * \param invertTex if true, flip texcoords vertically
483 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
484 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
485 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
487 struct st_context
*st
= st_context(ctx
);
488 struct pipe_context
*pipe
= st
->pipe
;
489 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
491 /* setup vertex data */
493 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
494 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
495 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
496 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
497 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
498 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
499 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
500 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
501 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
502 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
506 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
507 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
510 verts
[1][0][0] = clip_x1
;
511 verts
[1][0][1] = clip_y0
;
514 verts
[2][0][0] = clip_x1
;
515 verts
[2][0][1] = clip_y1
;
518 verts
[3][0][0] = clip_x0
;
519 verts
[3][0][1] = clip_y1
;
521 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
522 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
523 verts
[1][1][0] = sRight
;
524 verts
[1][1][1] = tTop
;
525 verts
[2][1][0] = sRight
;
526 verts
[2][1][1] = tBot
;
527 verts
[3][1][0] = sLeft
;
528 verts
[3][1][1] = tBot
;
530 /* same for all verts: */
532 for (i
= 0; i
< 4; i
++) {
533 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
534 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
535 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
536 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
537 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
538 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
539 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
540 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
544 for (i
= 0; i
< 4; i
++) {
545 verts
[i
][0][2] = z
; /*Z*/
546 verts
[i
][0][3] = 1.0f
; /*W*/
547 verts
[i
][1][2] = 0.0f
; /*R*/
548 verts
[i
][1][3] = 1.0f
; /*Q*/
554 struct pipe_resource
*buf
;
556 /* allocate/load buffer object with vertex data */
557 buf
= pipe_buffer_create(pipe
->screen
,
558 PIPE_BIND_VERTEX_BUFFER
,
561 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
563 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
566 3); /* attribs/vert */
567 pipe_resource_reference(&buf
, NULL
);
574 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
575 GLsizei width
, GLsizei height
,
576 GLfloat zoomX
, GLfloat zoomY
,
577 struct pipe_sampler_view
**sv
,
578 int num_sampler_view
,
581 const GLfloat
*color
,
583 GLboolean write_depth
, GLboolean write_stencil
)
585 struct st_context
*st
= st_context(ctx
);
586 struct pipe_context
*pipe
= st
->pipe
;
587 struct cso_context
*cso
= st
->cso_context
;
588 GLfloat x0
, y0
, x1
, y1
;
590 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
593 /* XXX if DrawPixels image is larger than max texture size, break
596 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
597 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
598 assert(width
<= maxSize
);
599 assert(height
<= maxSize
);
601 cso_save_rasterizer(cso
);
602 cso_save_viewport(cso
);
603 cso_save_samplers(cso
);
604 cso_save_fragment_sampler_views(cso
);
605 cso_save_fragment_shader(cso
);
606 cso_save_vertex_shader(cso
);
607 cso_save_vertex_elements(cso
);
608 cso_save_vertex_buffers(cso
);
610 cso_save_depth_stencil_alpha(cso
);
614 /* rasterizer state: just scissor */
616 struct pipe_rasterizer_state rasterizer
;
617 memset(&rasterizer
, 0, sizeof(rasterizer
));
618 rasterizer
.clamp_fragment_color
= ctx
->Color
._ClampFragmentColor
;
619 rasterizer
.gl_rasterization_rules
= 1;
620 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
621 cso_set_rasterizer(cso
, &rasterizer
);
625 /* Stencil writing bypasses the normal fragment pipeline to
626 * disable color writing and set stencil test to always pass.
628 struct pipe_depth_stencil_alpha_state dsa
;
629 struct pipe_blend_state blend
;
632 memset(&dsa
, 0, sizeof(dsa
));
633 dsa
.stencil
[0].enabled
= 1;
634 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
635 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
636 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
638 /* writing depth+stencil: depth test always passes */
639 dsa
.depth
.enabled
= 1;
640 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
641 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
643 cso_set_depth_stencil_alpha(cso
, &dsa
);
645 /* blend (colormask) */
646 memset(&blend
, 0, sizeof(blend
));
647 cso_set_blend(cso
, &blend
);
650 /* fragment shader state: TEX lookup program */
651 cso_set_fragment_shader_handle(cso
, driver_fp
);
653 /* vertex shader state: position + texcoord pass-through */
654 cso_set_vertex_shader_handle(cso
, driver_vp
);
657 /* texture sampling state: */
659 struct pipe_sampler_state sampler
;
660 memset(&sampler
, 0, sizeof(sampler
));
661 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
662 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
663 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
664 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
665 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
666 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
667 sampler
.normalized_coords
= normalized
;
669 cso_single_sampler(cso
, 0, &sampler
);
670 if (num_sampler_view
> 1) {
671 cso_single_sampler(cso
, 1, &sampler
);
673 cso_single_sampler_done(cso
);
676 /* viewport state: viewport matching window dims */
678 const float w
= (float) ctx
->DrawBuffer
->Width
;
679 const float h
= (float) ctx
->DrawBuffer
->Height
;
680 struct pipe_viewport_state vp
;
681 vp
.scale
[0] = 0.5f
* w
;
682 vp
.scale
[1] = -0.5f
* h
;
685 vp
.translate
[0] = 0.5f
* w
;
686 vp
.translate
[1] = 0.5f
* h
;
687 vp
.translate
[2] = 0.5f
;
688 vp
.translate
[3] = 0.0f
;
689 cso_set_viewport(cso
, &vp
);
692 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
695 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
697 /* Compute Gallium window coords (y=0=top) with pixel zoom.
698 * Recall that these coords are transformed by the current
699 * vertex shader and viewport transformation.
701 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
702 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
703 invertTex
= !invertTex
;
707 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
709 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
711 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
714 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
715 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
716 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
719 cso_restore_rasterizer(cso
);
720 cso_restore_viewport(cso
);
721 cso_restore_samplers(cso
);
722 cso_restore_fragment_sampler_views(cso
);
723 cso_restore_fragment_shader(cso
);
724 cso_restore_vertex_shader(cso
);
725 cso_restore_vertex_elements(cso
);
726 cso_restore_vertex_buffers(cso
);
728 cso_restore_depth_stencil_alpha(cso
);
729 cso_restore_blend(cso
);
735 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
736 * can't use a fragment shader to write stencil values.
739 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
740 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
741 const struct gl_pixelstore_attrib
*unpack
,
742 const GLvoid
*pixels
)
744 struct st_context
*st
= st_context(ctx
);
745 struct pipe_context
*pipe
= st
->pipe
;
746 struct st_renderbuffer
*strb
;
747 enum pipe_transfer_usage usage
;
748 struct pipe_transfer
*pt
;
749 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
752 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
755 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
757 /* totally clipped */
762 strb
= st_renderbuffer(ctx
->DrawBuffer
->
763 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
765 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
766 y
= ctx
->DrawBuffer
->Height
- y
- height
;
769 if(format
!= GL_DEPTH_STENCIL
&&
770 util_format_get_component_bits(strb
->format
,
771 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
772 usage
= PIPE_TRANSFER_READ_WRITE
;
774 usage
= PIPE_TRANSFER_WRITE
;
776 pt
= pipe_get_transfer(pipe
, strb
->texture
,
777 strb
->rtt_level
, strb
->rtt_face
+ strb
->rtt_slice
,
781 stmap
= pipe_transfer_map(pipe
, pt
);
783 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
786 /* if width > MAX_WIDTH, have to process image in chunks */
788 while (skipPixels
< width
) {
789 const GLint spanX
= skipPixels
;
790 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
792 for (row
= 0; row
< height
; row
++) {
793 GLubyte sValues
[MAX_WIDTH
];
794 GLuint zValues
[MAX_WIDTH
];
795 GLfloat
*zValuesFloat
= (GLfloat
*)zValues
;
796 GLenum destType
= GL_UNSIGNED_BYTE
;
797 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
801 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
802 type
, source
, &clippedUnpack
,
803 ctx
->_ImageTransferState
);
805 if (format
== GL_DEPTH_STENCIL
) {
807 pt
->resource
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
?
808 GL_FLOAT
: GL_UNSIGNED_INT
;
810 _mesa_unpack_depth_span(ctx
, spanWidth
, ztype
, zValues
,
811 (1 << 24) - 1, type
, source
,
816 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
817 "zoom not complete");
823 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
824 spanY
= height
- row
- 1;
830 /* now pack the stencil (and Z) values in the dest format */
831 switch (pt
->resource
->format
) {
832 case PIPE_FORMAT_S8_USCALED
:
834 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
835 assert(usage
== PIPE_TRANSFER_WRITE
);
836 memcpy(dest
, sValues
, spanWidth
);
839 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
840 if (format
== GL_DEPTH_STENCIL
) {
841 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
843 assert(usage
== PIPE_TRANSFER_WRITE
);
844 for (k
= 0; k
< spanWidth
; k
++) {
845 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
849 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
851 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
852 for (k
= 0; k
< spanWidth
; k
++) {
853 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
857 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
858 if (format
== GL_DEPTH_STENCIL
) {
859 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
861 assert(usage
== PIPE_TRANSFER_WRITE
);
862 for (k
= 0; k
< spanWidth
; k
++) {
863 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
867 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
869 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
870 for (k
= 0; k
< spanWidth
; k
++) {
871 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
875 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
876 if (format
== GL_DEPTH_STENCIL
) {
877 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
878 GLfloat
*destf
= (GLfloat
*)dest
;
880 assert(usage
== PIPE_TRANSFER_WRITE
);
881 for (k
= 0; k
< spanWidth
; k
++) {
882 destf
[k
*2] = zValuesFloat
[k
];
883 dest
[k
*2+1] = sValues
[k
] & 0xff;
887 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
889 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
890 for (k
= 0; k
< spanWidth
; k
++) {
891 dest
[k
*2+1] = sValues
[k
] & 0xff;
900 skipPixels
+= spanWidth
;
903 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
905 /* unmap the stencil buffer */
906 pipe_transfer_unmap(pipe
, pt
);
907 pipe
->transfer_destroy(pipe
, pt
);
912 * Get fragment program variant for a glDrawPixels or glCopyPixels
913 * command for RGBA data.
915 static struct st_fp_variant
*
916 get_color_fp_variant(struct st_context
*st
)
918 struct gl_context
*ctx
= st
->ctx
;
919 struct st_fp_variant_key key
;
920 struct st_fp_variant
*fpv
;
922 memset(&key
, 0, sizeof(key
));
926 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
927 ctx
->Pixel
.RedScale
!= 1.0 ||
928 ctx
->Pixel
.GreenBias
!= 0.0 ||
929 ctx
->Pixel
.GreenScale
!= 1.0 ||
930 ctx
->Pixel
.BlueBias
!= 0.0 ||
931 ctx
->Pixel
.BlueScale
!= 1.0 ||
932 ctx
->Pixel
.AlphaBias
!= 0.0 ||
933 ctx
->Pixel
.AlphaScale
!= 1.0);
934 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
936 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
943 * Get fragment program variant for a glDrawPixels or glCopyPixels
944 * command for depth/stencil data.
946 static struct st_fp_variant
*
947 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
948 GLboolean write_stencil
)
950 struct st_fp_variant_key key
;
951 struct st_fp_variant
*fpv
;
953 memset(&key
, 0, sizeof(key
));
957 key
.drawpixels_z
= write_depth
;
958 key
.drawpixels_stencil
= write_stencil
;
960 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
967 * Called via ctx->Driver.DrawPixels()
970 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
971 GLsizei width
, GLsizei height
,
972 GLenum format
, GLenum type
,
973 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
975 void *driver_vp
, *driver_fp
;
976 struct st_context
*st
= st_context(ctx
);
977 const GLfloat
*color
;
978 struct pipe_context
*pipe
= st
->pipe
;
979 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
980 struct pipe_sampler_view
*sv
[2];
981 int num_sampler_view
= 1;
982 struct st_fp_variant
*fpv
;
984 if (format
== GL_DEPTH_STENCIL
)
985 write_stencil
= write_depth
= GL_TRUE
;
986 else if (format
== GL_STENCIL_INDEX
)
987 write_stencil
= GL_TRUE
;
988 else if (format
== GL_DEPTH_COMPONENT
)
989 write_depth
= GL_TRUE
;
992 !pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
)) {
993 /* software fallback */
994 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
999 /* Mesa state should be up to date by now */
1000 assert(ctx
->NewState
== 0x0);
1002 st_validate_state(st
);
1005 * Get vertex/fragment shaders
1007 if (write_depth
|| write_stencil
) {
1008 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
1010 driver_fp
= fpv
->driver_shader
;
1012 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1014 color
= ctx
->Current
.RasterColor
;
1017 fpv
= get_color_fp_variant(st
);
1019 driver_fp
= fpv
->driver_shader
;
1021 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1024 if (st
->pixel_xfer
.pixelmap_enabled
) {
1025 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1030 /* update fragment program constants */
1031 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1033 /* draw with textured quad */
1035 struct pipe_resource
*pt
1036 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
1038 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1041 /* Create a second sampler view to read stencil.
1042 * The stencil is written using the shader stencil export
1044 if (write_stencil
) {
1045 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
1047 switch (pt
->format
) {
1048 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1049 case PIPE_FORMAT_X24S8_USCALED
:
1050 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
1052 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1053 case PIPE_FORMAT_S8X24_USCALED
:
1054 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
1056 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
1057 case PIPE_FORMAT_X32_S8X24_USCALED
:
1058 stencil_format
= PIPE_FORMAT_X32_S8X24_USCALED
;
1060 case PIPE_FORMAT_S8_USCALED
:
1061 stencil_format
= PIPE_FORMAT_S8_USCALED
;
1067 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
1072 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1074 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1079 color
, GL_FALSE
, write_depth
, write_stencil
);
1080 pipe_sampler_view_reference(&sv
[0], NULL
);
1081 if (num_sampler_view
> 1)
1082 pipe_sampler_view_reference(&sv
[1], NULL
);
1084 pipe_resource_reference(&pt
, NULL
);
1092 * Software fallback for glCopyPixels(GL_STENCIL).
1095 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1096 GLsizei width
, GLsizei height
,
1097 GLint dstx
, GLint dsty
)
1099 struct st_renderbuffer
*rbDraw
;
1100 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1101 enum pipe_transfer_usage usage
;
1102 struct pipe_transfer
*ptDraw
;
1107 buffer
= malloc(width
* height
* sizeof(ubyte
));
1109 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1113 /* Get the dest renderbuffer. If there's a wrapper, use the
1114 * underlying renderbuffer.
1116 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1117 if (rbDraw
->Base
.Wrapped
)
1118 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1120 /* this will do stencil pixel transfer ops */
1121 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1122 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1123 &ctx
->DefaultPacking
, buffer
);
1126 /* debug code: dump stencil values */
1128 for (row
= 0; row
< height
; row
++) {
1129 printf("%3d: ", row
);
1130 for (col
= 0; col
< width
; col
++) {
1131 printf("%02x ", buffer
[col
+ row
* width
]);
1137 if (util_format_get_component_bits(rbDraw
->format
,
1138 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1139 usage
= PIPE_TRANSFER_READ_WRITE
;
1141 usage
= PIPE_TRANSFER_WRITE
;
1143 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1144 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1147 ptDraw
= pipe_get_transfer(pipe
,
1150 rbDraw
->rtt_face
+ rbDraw
->rtt_slice
,
1154 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1155 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1157 /* map the stencil buffer */
1158 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1161 /* XXX PixelZoom not handled yet */
1162 for (i
= 0; i
< height
; i
++) {
1169 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1173 dst
= drawMap
+ y
* ptDraw
->stride
;
1174 src
= buffer
+ i
* width
;
1176 switch (ptDraw
->resource
->format
) {
1177 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1179 uint
*dst4
= (uint
*) dst
;
1181 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1182 for (j
= 0; j
< width
; j
++) {
1183 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1188 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1190 uint
*dst4
= (uint
*) dst
;
1192 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1193 for (j
= 0; j
< width
; j
++) {
1194 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1199 case PIPE_FORMAT_S8_USCALED
:
1200 assert(usage
== PIPE_TRANSFER_WRITE
);
1201 memcpy(dst
, src
, width
);
1203 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
1205 uint
*dst4
= (uint
*) dst
;
1208 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1209 for (j
= 0; j
< width
; j
++) {
1210 *dst4
= src
[j
] & 0xff;
1222 /* unmap the stencil buffer */
1223 pipe_transfer_unmap(pipe
, ptDraw
);
1224 pipe
->transfer_destroy(pipe
, ptDraw
);
1228 /** Do the src/dest regions overlap? */
1230 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1231 GLsizei width
, GLsizei height
)
1233 if (srcX
+ width
<= dstX
||
1234 dstX
+ width
<= srcX
||
1235 srcY
+ height
<= dstY
||
1236 dstY
+ height
<= srcY
)
1244 * Try to do a glCopyPixels for simple cases with a blit by calling
1245 * pipe->resource_copy_region().
1247 * We can do this when we're copying color pixels (depth/stencil
1248 * eventually) with no pixel zoom, no pixel transfer ops, no
1249 * per-fragment ops, the src/dest regions don't overlap and the
1250 * src/dest pixel formats are the same.
1253 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1254 GLsizei width
, GLsizei height
,
1255 GLint dstx
, GLint dsty
, GLenum type
)
1257 struct st_context
*st
= st_context(ctx
);
1258 struct pipe_context
*pipe
= st
->pipe
;
1259 struct gl_pixelstore_attrib pack
, unpack
;
1260 GLint readX
, readY
, readW
, readH
;
1262 if (type
== GL_COLOR
&&
1263 ctx
->Pixel
.ZoomX
== 1.0 &&
1264 ctx
->Pixel
.ZoomY
== 1.0 &&
1265 ctx
->_ImageTransferState
== 0x0 &&
1266 !ctx
->Color
.BlendEnabled
&&
1267 !ctx
->Color
.AlphaEnabled
&&
1269 !ctx
->Fog
.Enabled
&&
1270 !ctx
->Stencil
.Enabled
&&
1271 !ctx
->FragmentProgram
.Enabled
&&
1272 !ctx
->VertexProgram
.Enabled
&&
1273 !ctx
->Shader
.CurrentFragmentProgram
&&
1274 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1275 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1 &&
1276 !ctx
->Query
.CondRenderQuery
) {
1277 struct st_renderbuffer
*rbRead
, *rbDraw
;
1281 * Clip the read region against the src buffer bounds.
1282 * We'll still allocate a temporary buffer/texture for the original
1283 * src region size but we'll only read the region which is on-screen.
1284 * This may mean that we draw garbage pixels into the dest region, but
1291 pack
= ctx
->DefaultPacking
;
1292 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1293 return GL_TRUE
; /* all done */
1295 /* clip against dest buffer bounds and scissor box */
1296 drawX
= dstx
+ pack
.SkipPixels
;
1297 drawY
= dsty
+ pack
.SkipRows
;
1299 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1300 return GL_TRUE
; /* all done */
1302 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1303 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1305 rbRead
= st_get_color_read_renderbuffer(ctx
);
1306 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1308 if ((rbRead
!= rbDraw
||
1309 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1310 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1311 struct pipe_box srcBox
;
1313 /* flip src/dst position if needed */
1314 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1315 /* both buffers will have the same orientation */
1316 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1317 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1320 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1322 pipe
->resource_copy_region(pipe
,
1324 rbDraw
->rtt_level
, drawX
, drawY
, 0,
1326 rbRead
->rtt_level
, &srcBox
);
1336 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1337 GLsizei width
, GLsizei height
,
1338 GLint dstx
, GLint dsty
, GLenum type
)
1340 struct st_context
*st
= st_context(ctx
);
1341 struct pipe_context
*pipe
= st
->pipe
;
1342 struct pipe_screen
*screen
= pipe
->screen
;
1343 struct st_renderbuffer
*rbRead
;
1344 void *driver_vp
, *driver_fp
;
1345 struct pipe_resource
*pt
;
1346 struct pipe_sampler_view
*sv
[2];
1347 int num_sampler_view
= 1;
1349 enum pipe_format srcFormat
, texFormat
;
1350 GLboolean invertTex
= GL_FALSE
;
1351 GLint readX
, readY
, readW
, readH
;
1352 GLuint sample_count
;
1353 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1354 struct st_fp_variant
*fpv
;
1356 st_validate_state(st
);
1358 if (type
== GL_STENCIL
) {
1359 /* can't use texturing to do stencil */
1360 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1364 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1368 * The subsequent code implements glCopyPixels by copying the source
1369 * pixels into a temporary texture that's then applied to a textured quad.
1370 * When we draw the textured quad, all the usual per-fragment operations
1376 * Get vertex/fragment shaders
1378 if (type
== GL_COLOR
) {
1379 rbRead
= st_get_color_read_renderbuffer(ctx
);
1382 fpv
= get_color_fp_variant(st
);
1383 driver_fp
= fpv
->driver_shader
;
1385 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1387 if (st
->pixel_xfer
.pixelmap_enabled
) {
1388 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1393 assert(type
== GL_DEPTH
);
1394 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1395 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1397 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1398 driver_fp
= fpv
->driver_shader
;
1400 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1403 /* update fragment program constants */
1404 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1407 if (rbRead
->Base
.Wrapped
)
1408 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1410 sample_count
= rbRead
->texture
->nr_samples
;
1411 /* I believe this would be legal, presumably would need to do a resolve
1412 for color, and for depth/stencil spec says to just use one of the
1413 depth/stencil samples per pixel? Need some transfer clarifications. */
1414 assert(sample_count
< 2);
1416 srcFormat
= rbRead
->texture
->format
;
1418 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1420 PIPE_BIND_SAMPLER_VIEW
)) {
1421 texFormat
= srcFormat
;
1424 /* srcFormat can't be used as a texture format */
1425 if (type
== GL_DEPTH
) {
1426 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1427 GL_NONE
, GL_NONE
, st
->internal_target
,
1428 sample_count
, PIPE_BIND_DEPTH_STENCIL
);
1429 assert(texFormat
!= PIPE_FORMAT_NONE
);
1432 /* default color format */
1433 texFormat
= st_choose_format(screen
, GL_RGBA
,
1434 GL_NONE
, GL_NONE
, st
->internal_target
,
1435 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1436 assert(texFormat
!= PIPE_FORMAT_NONE
);
1440 /* Invert src region if needed */
1441 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1442 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1443 invertTex
= !invertTex
;
1446 /* Clip the read region against the src buffer bounds.
1447 * We'll still allocate a temporary buffer/texture for the original
1448 * src region size but we'll only read the region which is on-screen.
1449 * This may mean that we draw garbage pixels into the dest region, but
1456 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1457 readW
= MAX2(0, readW
);
1458 readH
= MAX2(0, readH
);
1460 /* alloc temporary texture */
1461 pt
= alloc_texture(st
, width
, height
, texFormat
);
1465 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1467 pipe_resource_reference(&pt
, NULL
);
1471 /* Make temporary texture which is a copy of the src region.
1473 if (srcFormat
== texFormat
) {
1474 struct pipe_box src_box
;
1475 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1476 /* copy source framebuffer surface into mipmap/texture */
1477 pipe
->resource_copy_region(pipe
,
1480 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1481 rbRead
->texture
, /* src tex */
1482 rbRead
->rtt_level
, /* src lvl */
1487 /* CPU-based fallback/conversion */
1488 struct pipe_transfer
*ptRead
=
1489 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1491 rbRead
->rtt_face
+ rbRead
->rtt_slice
,
1493 readX
, readY
, readW
, readH
);
1494 struct pipe_transfer
*ptTex
;
1495 enum pipe_transfer_usage transfer_usage
;
1497 if (ST_DEBUG
& DEBUG_FALLBACK
)
1498 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1500 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1501 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1503 transfer_usage
= PIPE_TRANSFER_WRITE
;
1505 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1506 0, 0, width
, height
);
1508 /* copy image from ptRead surface to ptTex surface */
1509 if (type
== GL_COLOR
) {
1510 /* alternate path using get/put_tile() */
1511 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1512 enum pipe_format readFormat
, drawFormat
;
1513 readFormat
= util_format_linear(rbRead
->texture
->format
);
1514 drawFormat
= util_format_linear(pt
->format
);
1515 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1517 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1518 readW
, readH
, drawFormat
, buf
);
1523 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1524 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1525 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1530 pipe
->transfer_destroy(pipe
, ptRead
);
1531 pipe
->transfer_destroy(pipe
, ptTex
);
1534 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1535 * textured quad with that texture.
1537 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1538 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1543 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1545 pipe_resource_reference(&pt
, NULL
);
1546 pipe_sampler_view_reference(&sv
[0], NULL
);
1551 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1553 functions
->DrawPixels
= st_DrawPixels
;
1554 functions
->CopyPixels
= st_CopyPixels
;
1559 st_destroy_drawpix(struct st_context
*st
)
1563 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1564 if (st
->drawpix
.shaders
[i
])
1565 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1568 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1569 if (st
->drawpix
.vert_shaders
[0])
1570 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1571 if (st
->drawpix
.vert_shaders
[1])
1572 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1575 #endif /* FEATURE_drawpix */