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/texformat.h"
38 #include "main/texstore.h"
39 #include "program/program.h"
40 #include "program/prog_print.h"
43 #include "st_context.h"
45 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_format.h"
51 #include "st_texture.h"
53 #include "pipe/p_context.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_inlines.h"
56 #include "tgsi/tgsi_ureg.h"
57 #include "util/u_tile.h"
58 #include "util/u_draw_quad.h"
59 #include "util/u_format.h"
60 #include "util/u_math.h"
61 #include "program/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
68 * Check if the given program is:
69 * 0: MOVE result.color, fragment.color;
73 is_passthrough_program(const struct gl_fragment_program
*prog
)
75 if (prog
->Base
.NumInstructions
== 2) {
76 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
77 if (inst
[0].Opcode
== OPCODE_MOV
&&
78 inst
[1].Opcode
== OPCODE_END
&&
79 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
80 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
81 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
82 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
83 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
84 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
94 * Make fragment shader for glDraw/CopyPixels. This shader is made
95 * by combining the pixel transfer shader with the user-defined shader.
96 * \return pointer to Gallium driver fragment shader
99 combined_drawpix_fragment_program(GLcontext
*ctx
)
101 struct st_context
*st
= st_context(ctx
);
102 struct st_fragment_program
*stfp
;
104 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
105 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
106 /* the pixel tranfer program has not changed and the user-defined
107 * program has not changed, so re-use the combined program.
109 stfp
= st
->pixel_xfer
.combined_prog
;
112 /* Concatenate the pixel transfer program with the current user-
115 if (is_passthrough_program(&st
->fp
->Base
)) {
116 stfp
= (struct st_fragment_program
*)
117 _mesa_clone_fragment_program(ctx
, &st
->pixel_xfer
.program
->Base
);
121 printf("Base program:\n");
122 _mesa_print_program(&st
->fp
->Base
.Base
);
123 printf("DrawPix program:\n");
124 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
126 stfp
= (struct st_fragment_program
*)
127 _mesa_combine_programs(ctx
,
128 &st
->pixel_xfer
.program
->Base
.Base
,
134 struct gl_program
*p
= &stfp
->Base
.Base
;
135 printf("Combined DrawPixels program:\n");
136 _mesa_print_program(p
);
137 printf("InputsRead: 0x%x\n", p
->InputsRead
);
138 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
139 _mesa_print_parameter_list(p
->Parameters
);
143 /* translate to TGSI tokens */
144 st_translate_fragment_program(st
, stfp
);
146 /* save new program, update serial numbers */
147 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
148 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
149 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
150 /* can't reference new program directly, already have a reference on it */
151 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
152 st
->pixel_xfer
.combined_prog
= stfp
;
155 /* Ideally we'd have updated the pipe constants during the normal
156 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
158 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
160 return stfp
->driver_shader
;
165 * Create fragment shader that does a TEX() instruction to get a Z
166 * value, then writes to FRAG_RESULT_DEPTH.
167 * Pass fragment color through as-is.
168 * \return pointer to the Gallium driver fragment shader
171 make_fragment_shader_z(struct st_context
*st
, GLboolean write_depth
, GLboolean write_stencil
)
173 GLcontext
*ctx
= st
->ctx
;
174 struct gl_program
*p
;
177 if (st
->drawpix
.z_shader
) {
178 return st
->drawpix
.z_shader
->driver_shader
;
184 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
188 p
->NumInstructions
= write_depth
? 2 : 1;
189 p
->NumInstructions
+= write_stencil
? 1 : 0;
191 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
192 if (!p
->Instructions
) {
193 ctx
->Driver
.DeleteProgram(ctx
, p
);
196 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
198 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
200 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
201 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
202 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
203 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
204 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
205 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
206 p
->Instructions
[ic
].TexSrcUnit
= 0;
207 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
212 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
213 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
214 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
215 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
216 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
217 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
218 p
->Instructions
[ic
].TexSrcUnit
= 1;
219 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
224 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
226 assert(ic
== p
->NumInstructions
);
228 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
229 p
->OutputsWritten
= 0;
231 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
233 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
235 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
237 p
->SamplersUsed
|= 1 << 1;
238 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
239 st_translate_fragment_program(st
, st
->drawpix
.z_shader
);
241 return st
->drawpix
.z_shader
->driver_shader
;
247 * Create a simple vertex shader that just passes through the
248 * vertex position and texcoord (and optionally, color).
251 make_passthrough_vertex_shader(struct st_context
*st
,
254 if (!st
->drawpix
.vert_shaders
[passColor
]) {
255 struct ureg_program
*ureg
=
256 ureg_create( TGSI_PROCESSOR_VERTEX
);
261 /* MOV result.pos, vertex.pos; */
263 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
264 ureg_DECL_vs_input( ureg
, 0 ));
266 /* MOV result.texcoord0, vertex.attr[1]; */
268 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
269 ureg_DECL_vs_input( ureg
, 1 ));
272 /* MOV result.color0, vertex.attr[2]; */
274 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
275 ureg_DECL_vs_input( ureg
, 2 ));
280 st
->drawpix
.vert_shaders
[passColor
] =
281 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
284 return st
->drawpix
.vert_shaders
[passColor
];
289 * Return a texture internalFormat for drawing/copying an image
293 base_format(GLenum format
)
296 case GL_DEPTH_COMPONENT
:
297 return GL_DEPTH_COMPONENT
;
298 case GL_DEPTH_STENCIL
:
299 return GL_DEPTH_STENCIL
;
300 case GL_STENCIL_INDEX
:
301 return GL_STENCIL_INDEX
;
309 * Create a temporary texture to hold an image of the given size.
310 * If width, height are not POT and the driver only handles POT textures,
311 * allocate the next larger size of texture that is POT.
313 static struct pipe_resource
*
314 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
315 enum pipe_format texFormat
)
317 struct pipe_resource
*pt
;
319 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
320 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
327 * Make texture containing an image for glDrawPixels image.
328 * If 'pixels' is NULL, leave the texture image data undefined.
330 static struct pipe_resource
*
331 make_texture(struct st_context
*st
,
332 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
333 const struct gl_pixelstore_attrib
*unpack
,
334 const GLvoid
*pixels
)
336 GLcontext
*ctx
= st
->ctx
;
337 struct pipe_context
*pipe
= st
->pipe
;
339 struct pipe_resource
*pt
;
340 enum pipe_format pipeFormat
;
344 baseFormat
= base_format(format
);
346 mformat
= st_ChooseTextureFormat_renderable(ctx
, baseFormat
, format
, type
, GL_FALSE
);
349 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
351 cpp
= util_format_get_blocksize(pipeFormat
);
353 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
357 /* alloc temporary texture */
358 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
360 _mesa_unmap_pbo_source(ctx
, unpack
);
365 struct pipe_transfer
*transfer
;
366 static const GLuint dstImageOffsets
= 0;
369 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
371 /* we'll do pixel transfer in a fragment shader */
372 ctx
->_ImageTransferState
= 0x0;
374 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0,
375 PIPE_TRANSFER_WRITE
, 0, 0,
378 /* map texture transfer */
379 dest
= pipe_transfer_map(pipe
, transfer
);
382 /* Put image into texture transfer.
383 * Note that the image is actually going to be upside down in
384 * the texture. We deal with that with texcoords.
386 success
= _mesa_texstore(ctx
, 2, /* dims */
387 baseFormat
, /* baseInternalFormat */
388 mformat
, /* gl_format */
390 0, 0, 0, /* dstX/Y/Zoffset */
391 transfer
->stride
, /* dstRowStride, bytes */
392 &dstImageOffsets
, /* dstImageOffsets */
393 width
, height
, 1, /* size */
394 format
, type
, /* src format/type */
395 pixels
, /* data source */
399 pipe_transfer_unmap(pipe
, transfer
);
400 pipe
->transfer_destroy(pipe
, transfer
);
405 ctx
->_ImageTransferState
= imageTransferStateSave
;
408 _mesa_unmap_pbo_source(ctx
, unpack
);
415 * Draw quad with texcoords and optional color.
416 * Coords are gallium window coords with y=0=top.
417 * \param color may be null
418 * \param invertTex if true, flip texcoords vertically
421 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
422 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
423 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
425 struct st_context
*st
= st_context(ctx
);
426 struct pipe_context
*pipe
= st
->pipe
;
427 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
429 /* setup vertex data */
431 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
432 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
433 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
434 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
435 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
436 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
437 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
438 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
439 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
440 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
444 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
445 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
448 verts
[1][0][0] = clip_x1
;
449 verts
[1][0][1] = clip_y0
;
452 verts
[2][0][0] = clip_x1
;
453 verts
[2][0][1] = clip_y1
;
456 verts
[3][0][0] = clip_x0
;
457 verts
[3][0][1] = clip_y1
;
459 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
460 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
461 verts
[1][1][0] = sRight
;
462 verts
[1][1][1] = tTop
;
463 verts
[2][1][0] = sRight
;
464 verts
[2][1][1] = tBot
;
465 verts
[3][1][0] = sLeft
;
466 verts
[3][1][1] = tBot
;
468 /* same for all verts: */
470 for (i
= 0; i
< 4; i
++) {
471 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
472 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
473 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
474 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
475 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
476 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
477 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
478 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
482 for (i
= 0; i
< 4; i
++) {
483 verts
[i
][0][2] = z
; /*Z*/
484 verts
[i
][0][3] = 1.0f
; /*W*/
485 verts
[i
][1][2] = 0.0f
; /*R*/
486 verts
[i
][1][3] = 1.0f
; /*Q*/
492 struct pipe_resource
*buf
;
494 /* allocate/load buffer object with vertex data */
495 buf
= pipe_buffer_create(pipe
->screen
,
496 PIPE_BIND_VERTEX_BUFFER
,
498 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
500 util_draw_vertex_buffer(pipe
, buf
, 0,
503 3); /* attribs/vert */
504 pipe_resource_reference(&buf
, NULL
);
511 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
512 GLsizei width
, GLsizei height
,
513 GLfloat zoomX
, GLfloat zoomY
,
514 struct pipe_sampler_view
**sv
,
515 int num_sampler_view
,
518 const GLfloat
*color
,
519 GLboolean invertTex
, GLboolean write_depth
, GLboolean write_stencil
)
521 struct st_context
*st
= st_context(ctx
);
522 struct pipe_context
*pipe
= st
->pipe
;
523 struct cso_context
*cso
= st
->cso_context
;
524 GLfloat x0
, y0
, x1
, y1
;
526 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
529 /* XXX if DrawPixels image is larger than max texture size, break
532 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
533 assert(width
<= maxSize
);
534 assert(height
<= maxSize
);
536 cso_save_depth_stencil_alpha(cso
);
537 cso_save_rasterizer(cso
);
538 cso_save_viewport(cso
);
539 cso_save_samplers(cso
);
540 cso_save_fragment_sampler_views(cso
);
541 cso_save_fragment_shader(cso
);
542 cso_save_vertex_shader(cso
);
543 cso_save_vertex_elements(cso
);
545 /* rasterizer state: just scissor */
547 struct pipe_rasterizer_state rasterizer
;
548 memset(&rasterizer
, 0, sizeof(rasterizer
));
549 rasterizer
.gl_rasterization_rules
= 1;
550 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
551 cso_set_rasterizer(cso
, &rasterizer
);
554 if (write_depth
|| write_stencil
)
556 struct pipe_depth_stencil_alpha_state dsa
;
557 memset(&dsa
, 0, sizeof(dsa
));
559 dsa
.depth
.enabled
= 1;
560 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
561 dsa
.depth
.writemask
= 1;
564 dsa
.stencil
[0].enabled
= 1;
565 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
566 dsa
.stencil
[0].writemask
= 0xff;
567 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
569 cso_set_depth_stencil_alpha(cso
, &dsa
);
572 /* fragment shader state: TEX lookup program */
573 cso_set_fragment_shader_handle(cso
, driver_fp
);
575 /* vertex shader state: position + texcoord pass-through */
576 cso_set_vertex_shader_handle(cso
, driver_vp
);
579 /* texture sampling state: */
581 struct pipe_sampler_state sampler
;
582 memset(&sampler
, 0, sizeof(sampler
));
583 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
584 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
585 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
586 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
587 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
588 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
589 sampler
.normalized_coords
= normalized
;
591 cso_single_sampler(cso
, 0, &sampler
);
592 if (num_sampler_view
> 1) {
593 cso_single_sampler(cso
, 1, &sampler
);
595 cso_single_sampler_done(cso
);
598 /* viewport state: viewport matching window dims */
600 const float w
= (float) ctx
->DrawBuffer
->Width
;
601 const float h
= (float) ctx
->DrawBuffer
->Height
;
602 struct pipe_viewport_state vp
;
603 vp
.scale
[0] = 0.5f
* w
;
604 vp
.scale
[1] = -0.5f
* h
;
607 vp
.translate
[0] = 0.5f
* w
;
608 vp
.translate
[1] = 0.5f
* h
;
609 vp
.translate
[2] = 0.5f
;
610 vp
.translate
[3] = 0.0f
;
611 cso_set_viewport(cso
, &vp
);
614 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
617 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
619 /* Compute Gallium window coords (y=0=top) with pixel zoom.
620 * Recall that these coords are transformed by the current
621 * vertex shader and viewport transformation.
623 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
624 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
625 invertTex
= !invertTex
;
629 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
631 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
633 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
636 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
637 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
638 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
641 cso_restore_depth_stencil_alpha(cso
);
642 cso_restore_rasterizer(cso
);
643 cso_restore_viewport(cso
);
644 cso_restore_samplers(cso
);
645 cso_restore_fragment_sampler_views(cso
);
646 cso_restore_fragment_shader(cso
);
647 cso_restore_vertex_shader(cso
);
648 cso_restore_vertex_elements(cso
);
653 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
654 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
655 const struct gl_pixelstore_attrib
*unpack
,
656 const GLvoid
*pixels
)
658 struct st_context
*st
= st_context(ctx
);
659 struct pipe_context
*pipe
= st
->pipe
;
660 struct st_renderbuffer
*strb
;
661 enum pipe_transfer_usage usage
;
662 struct pipe_transfer
*pt
;
663 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
666 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
669 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
671 /* totally clipped */
676 strb
= st_renderbuffer(ctx
->DrawBuffer
->
677 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
679 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
680 y
= ctx
->DrawBuffer
->Height
- y
- height
;
683 if(format
!= GL_DEPTH_STENCIL
&&
684 util_format_get_component_bits(strb
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
685 usage
= PIPE_TRANSFER_READ_WRITE
;
687 usage
= PIPE_TRANSFER_WRITE
;
689 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0, 0,
693 stmap
= pipe_transfer_map(pipe
, pt
);
695 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
698 /* if width > MAX_WIDTH, have to process image in chunks */
700 while (skipPixels
< width
) {
701 const GLint spanX
= skipPixels
;
702 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
704 for (row
= 0; row
< height
; row
++) {
705 GLubyte sValues
[MAX_WIDTH
];
706 GLuint zValues
[MAX_WIDTH
];
707 GLenum destType
= GL_UNSIGNED_BYTE
;
708 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
712 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
713 type
, source
, &clippedUnpack
,
714 ctx
->_ImageTransferState
);
716 if (format
== GL_DEPTH_STENCIL
) {
717 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
718 (1 << 24) - 1, type
, source
,
723 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
724 "zoom not complete");
730 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
731 spanY
= height
- row
- 1;
737 /* now pack the stencil (and Z) values in the dest format */
738 switch (pt
->resource
->format
) {
739 case PIPE_FORMAT_S8_USCALED
:
741 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
742 assert(usage
== PIPE_TRANSFER_WRITE
);
743 memcpy(dest
, sValues
, spanWidth
);
746 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
747 if (format
== GL_DEPTH_STENCIL
) {
748 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
750 assert(usage
== PIPE_TRANSFER_WRITE
);
751 for (k
= 0; k
< spanWidth
; k
++) {
752 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
756 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
758 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
759 for (k
= 0; k
< spanWidth
; k
++) {
760 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
764 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
765 if (format
== GL_DEPTH_STENCIL
) {
766 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
768 assert(usage
== PIPE_TRANSFER_WRITE
);
769 for (k
= 0; k
< spanWidth
; k
++) {
770 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
774 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
776 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
777 for (k
= 0; k
< spanWidth
; k
++) {
778 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
787 skipPixels
+= spanWidth
;
790 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
792 /* unmap the stencil buffer */
793 pipe_transfer_unmap(pipe
, pt
);
794 pipe
->transfer_destroy(pipe
, pt
);
799 * Called via ctx->Driver.DrawPixels()
802 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
803 GLenum format
, GLenum type
,
804 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
806 void *driver_vp
, *driver_fp
;
807 struct st_context
*st
= st_context(ctx
);
808 const GLfloat
*color
;
809 struct pipe_context
*pipe
= st
->pipe
;
810 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
811 struct pipe_sampler_view
*sv
[2];
812 int num_sampler_view
= 1;
813 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
815 if (format
== GL_DEPTH_STENCIL
)
816 write_stencil
= write_depth
= GL_TRUE
;
817 else if (format
== GL_STENCIL_INDEX
)
818 write_stencil
= GL_TRUE
;
819 else if (format
== GL_DEPTH_COMPONENT
)
820 write_depth
= GL_TRUE
;
823 enum pipe_format tex_format
;
824 /* can we write to stencil if not fallback */
825 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
826 goto stencil_fallback
;
828 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
), PIPE_TEXTURE_2D
,
829 0, PIPE_BIND_SAMPLER_VIEW
);
830 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
831 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
832 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
833 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
835 stencil_format
= PIPE_FORMAT_S8_USCALED
;
836 if (stencil_format
== PIPE_FORMAT_NONE
)
837 goto stencil_fallback
;
840 /* Mesa state should be up to date by now */
841 assert(ctx
->NewState
== 0x0);
843 st_validate_state(st
);
845 if (write_depth
|| write_stencil
) {
846 driver_fp
= make_fragment_shader_z(st
, write_depth
, write_stencil
);
847 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
848 color
= ctx
->Current
.RasterColor
;
851 driver_fp
= combined_drawpix_fragment_program(ctx
);
852 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
854 if (st
->pixel_xfer
.pixelmap_enabled
) {
855 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
860 /* draw with textured quad */
862 struct pipe_resource
*pt
863 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
865 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
869 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
, stencil_format
);
873 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
874 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
879 color
, GL_FALSE
, write_depth
, write_stencil
);
880 pipe_sampler_view_reference(&sv
[0], NULL
);
881 if (num_sampler_view
> 1)
882 pipe_sampler_view_reference(&sv
[1], NULL
);
884 pipe_resource_reference(&pt
, NULL
);
889 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
896 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
897 GLsizei width
, GLsizei height
,
898 GLint dstx
, GLint dsty
)
900 struct st_renderbuffer
*rbDraw
;
901 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
902 enum pipe_transfer_usage usage
;
903 struct pipe_transfer
*ptDraw
;
908 buffer
= malloc(width
* height
* sizeof(ubyte
));
910 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
914 /* Get the dest renderbuffer. If there's a wrapper, use the
915 * underlying renderbuffer.
917 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
918 if (rbDraw
->Base
.Wrapped
)
919 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
921 /* this will do stencil pixel transfer ops */
922 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
923 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
924 &ctx
->DefaultPacking
, buffer
);
926 if(util_format_get_component_bits(rbDraw
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
927 usage
= PIPE_TRANSFER_READ_WRITE
;
929 usage
= PIPE_TRANSFER_WRITE
;
931 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
932 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
935 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
936 rbDraw
->texture
, 0, 0, 0,
940 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
941 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
943 /* map the stencil buffer */
944 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
947 /* XXX PixelZoom not handled yet */
948 for (i
= 0; i
< height
; i
++) {
955 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
959 dst
= drawMap
+ y
* ptDraw
->stride
;
960 src
= buffer
+ i
* width
;
962 switch (ptDraw
->resource
->format
) {
963 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
965 uint
*dst4
= (uint
*) dst
;
967 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
968 for (j
= 0; j
< width
; j
++) {
969 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
974 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
976 uint
*dst4
= (uint
*) dst
;
978 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
979 for (j
= 0; j
< width
; j
++) {
980 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
985 case PIPE_FORMAT_S8_USCALED
:
986 assert(usage
== PIPE_TRANSFER_WRITE
);
987 memcpy(dst
, src
, width
);
996 /* unmap the stencil buffer */
997 pipe_transfer_unmap(pipe
, ptDraw
);
998 pipe
->transfer_destroy(pipe
, ptDraw
);
1003 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1004 GLsizei width
, GLsizei height
,
1005 GLint dstx
, GLint dsty
, GLenum type
)
1007 struct st_context
*st
= st_context(ctx
);
1008 struct pipe_context
*pipe
= st
->pipe
;
1009 struct pipe_screen
*screen
= pipe
->screen
;
1010 struct st_renderbuffer
*rbRead
;
1011 void *driver_vp
, *driver_fp
;
1012 struct pipe_resource
*pt
;
1013 struct pipe_sampler_view
*sv
[2];
1014 int num_sampler_view
= 1;
1016 enum pipe_format srcFormat
, texFormat
;
1017 GLboolean invertTex
= GL_FALSE
;
1018 GLint readX
, readY
, readW
, readH
;
1019 GLuint sample_count
;
1020 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1022 st_validate_state(st
);
1024 if (type
== GL_STENCIL
) {
1025 /* can't use texturing to do stencil */
1026 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1030 if (type
== GL_COLOR
) {
1031 rbRead
= st_get_color_read_renderbuffer(ctx
);
1033 driver_fp
= combined_drawpix_fragment_program(ctx
);
1034 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1035 if (st
->pixel_xfer
.pixelmap_enabled
) {
1036 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1041 assert(type
== GL_DEPTH
);
1042 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1043 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1044 driver_fp
= make_fragment_shader_z(st
, GL_TRUE
, GL_FALSE
);
1045 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1048 if (rbRead
->Base
.Wrapped
)
1049 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1051 sample_count
= rbRead
->texture
->nr_samples
;
1052 /* I believe this would be legal, presumably would need to do a resolve
1053 for color, and for depth/stencil spec says to just use one of the
1054 depth/stencil samples per pixel? Need some transfer clarifications. */
1055 assert(sample_count
< 2);
1057 srcFormat
= rbRead
->texture
->format
;
1059 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, sample_count
,
1060 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1061 texFormat
= srcFormat
;
1064 /* srcFormat can't be used as a texture format */
1065 if (type
== GL_DEPTH
) {
1066 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1067 st
->internal_target
, sample_count
,
1068 PIPE_BIND_DEPTH_STENCIL
);
1069 assert(texFormat
!= PIPE_FORMAT_NONE
);
1072 /* default color format */
1073 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1074 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1075 assert(texFormat
!= PIPE_FORMAT_NONE
);
1079 /* Invert src region if needed */
1080 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1081 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1082 invertTex
= !invertTex
;
1085 /* Clip the read region against the src buffer bounds.
1086 * We'll still allocate a temporary buffer/texture for the original
1087 * src region size but we'll only read the region which is on-screen.
1088 * This may mean that we draw garbage pixels into the dest region, but
1095 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1096 readW
= MAX2(0, readW
);
1097 readH
= MAX2(0, readH
);
1099 /* alloc temporary texture */
1100 pt
= alloc_texture(st
, width
, height
, texFormat
);
1104 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1106 pipe_resource_reference(&pt
, NULL
);
1110 /* Make temporary texture which is a copy of the src region.
1112 if (srcFormat
== texFormat
) {
1113 struct pipe_subresource srcsub
, dstsub
;
1118 /* copy source framebuffer surface into mipmap/texture */
1119 pipe
->resource_copy_region(pipe
,
1122 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1123 rbRead
->texture
, /* src tex */
1125 readX
, readY
, 0, readW
, readH
); /* src region */
1129 /* CPU-based fallback/conversion */
1130 struct pipe_transfer
*ptRead
=
1131 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0, 0,
1133 readX
, readY
, readW
, readH
);
1134 struct pipe_transfer
*ptTex
;
1135 enum pipe_transfer_usage transfer_usage
;
1137 if (ST_DEBUG
& DEBUG_FALLBACK
)
1138 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1140 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1141 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1143 transfer_usage
= PIPE_TRANSFER_WRITE
;
1145 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0, transfer_usage
,
1146 0, 0, width
, height
);
1148 /* copy image from ptRead surface to ptTex surface */
1149 if (type
== GL_COLOR
) {
1150 /* alternate path using get/put_tile() */
1151 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1152 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1153 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1159 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1160 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1161 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1166 pipe
->transfer_destroy(pipe
, ptRead
);
1167 pipe
->transfer_destroy(pipe
, ptTex
);
1170 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1171 * textured quad with that texture.
1173 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1174 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1179 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1181 pipe_resource_reference(&pt
, NULL
);
1182 pipe_sampler_view_reference(&sv
[0], NULL
);
1187 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1189 functions
->DrawPixels
= st_DrawPixels
;
1190 functions
->CopyPixels
= st_CopyPixels
;
1195 st_destroy_drawpix(struct st_context
*st
)
1197 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1198 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1199 if (st
->drawpix
.vert_shaders
[0])
1200 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1201 if (st
->drawpix
.vert_shaders
[1])
1202 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1205 #endif /* FEATURE_drawpix */