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"
41 #include "program/prog_instruction.h"
44 #include "st_atom_constbuf.h"
45 #include "st_cb_drawpixels.h"
46 #include "st_cb_readpixels.h"
47 #include "st_cb_fbo.h"
48 #include "st_context.h"
50 #include "st_format.h"
51 #include "st_program.h"
52 #include "st_texture.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "tgsi/tgsi_ureg.h"
57 #include "util/u_draw_quad.h"
58 #include "util/u_format.h"
59 #include "util/u_inlines.h"
60 #include "util/u_math.h"
61 #include "util/u_tile.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(struct gl_context
*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 and/or
166 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
167 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
168 * Pass fragment color through as-is.
169 * \return pointer to the Gallium driver fragment shader
172 make_fragment_shader_z_stencil(struct st_context
*st
, GLboolean write_depth
,
173 GLboolean write_stencil
)
175 struct gl_context
*ctx
= st
->ctx
;
176 struct gl_program
*p
;
177 struct st_fragment_program
*stp
;
179 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
181 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
183 if (st
->drawpix
.shaders
[shaderIndex
]) {
184 /* already have the proper shader */
185 return st
->drawpix
.shaders
[shaderIndex
]->driver_shader
;
191 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
195 p
->NumInstructions
= write_depth
? 2 : 1;
196 p
->NumInstructions
+= write_stencil
? 1 : 0;
198 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
199 if (!p
->Instructions
) {
200 ctx
->Driver
.DeleteProgram(ctx
, p
);
203 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
206 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
207 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
208 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
209 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
210 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
211 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
212 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
213 p
->Instructions
[ic
].TexSrcUnit
= 0;
214 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
219 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
220 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
221 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
222 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
223 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
224 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
225 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
226 p
->Instructions
[ic
].TexSrcUnit
= 1;
227 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
232 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
234 assert(ic
== p
->NumInstructions
);
236 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
237 p
->OutputsWritten
= 0;
239 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
241 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
243 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
245 p
->SamplersUsed
|= 1 << 1;
247 stp
= st_fragment_program((struct gl_fragment_program
*) p
);
249 /* save the new shader */
250 st
->drawpix
.shaders
[shaderIndex
] = stp
;
252 st_translate_fragment_program(st
, stp
);
254 return stp
->driver_shader
;
260 * Create a simple vertex shader that just passes through the
261 * vertex position and texcoord (and optionally, color).
264 make_passthrough_vertex_shader(struct st_context
*st
,
267 if (!st
->drawpix
.vert_shaders
[passColor
]) {
268 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
273 /* MOV result.pos, vertex.pos; */
275 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
276 ureg_DECL_vs_input( ureg
, 0 ));
278 /* MOV result.texcoord0, vertex.attr[1]; */
280 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
281 ureg_DECL_vs_input( ureg
, 1 ));
284 /* MOV result.color0, vertex.attr[2]; */
286 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
287 ureg_DECL_vs_input( ureg
, 2 ));
292 st
->drawpix
.vert_shaders
[passColor
] =
293 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
296 return st
->drawpix
.vert_shaders
[passColor
];
301 * Return a texture internalFormat for drawing/copying an image
305 base_format(GLenum format
)
308 case GL_DEPTH_COMPONENT
:
309 return GL_DEPTH_COMPONENT
;
310 case GL_DEPTH_STENCIL
:
311 return GL_DEPTH_STENCIL
;
312 case GL_STENCIL_INDEX
:
313 return GL_STENCIL_INDEX
;
321 * Create a temporary texture to hold an image of the given size.
322 * If width, height are not POT and the driver only handles POT textures,
323 * allocate the next larger size of texture that is POT.
325 static struct pipe_resource
*
326 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
327 enum pipe_format texFormat
)
329 struct pipe_resource
*pt
;
331 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
332 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
339 * Make texture containing an image for glDrawPixels image.
340 * If 'pixels' is NULL, leave the texture image data undefined.
342 static struct pipe_resource
*
343 make_texture(struct st_context
*st
,
344 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
345 const struct gl_pixelstore_attrib
*unpack
,
346 const GLvoid
*pixels
)
348 struct gl_context
*ctx
= st
->ctx
;
349 struct pipe_context
*pipe
= st
->pipe
;
351 struct pipe_resource
*pt
;
352 enum pipe_format pipeFormat
;
356 baseFormat
= base_format(format
);
358 mformat
= st_ChooseTextureFormat_renderable(ctx
, baseFormat
,
359 format
, type
, GL_FALSE
);
362 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
364 cpp
= util_format_get_blocksize(pipeFormat
);
366 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
370 /* alloc temporary texture */
371 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
373 _mesa_unmap_pbo_source(ctx
, unpack
);
378 struct pipe_transfer
*transfer
;
379 static const GLuint dstImageOffsets
= 0;
382 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
384 /* we'll do pixel transfer in a fragment shader */
385 ctx
->_ImageTransferState
= 0x0;
387 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0,
388 PIPE_TRANSFER_WRITE
, 0, 0,
391 /* map texture transfer */
392 dest
= pipe_transfer_map(pipe
, transfer
);
395 /* Put image into texture transfer.
396 * Note that the image is actually going to be upside down in
397 * the texture. We deal with that with texcoords.
399 success
= _mesa_texstore(ctx
, 2, /* dims */
400 baseFormat
, /* baseInternalFormat */
401 mformat
, /* gl_format */
403 0, 0, 0, /* dstX/Y/Zoffset */
404 transfer
->stride
, /* dstRowStride, bytes */
405 &dstImageOffsets
, /* dstImageOffsets */
406 width
, height
, 1, /* size */
407 format
, type
, /* src format/type */
408 pixels
, /* data source */
412 pipe_transfer_unmap(pipe
, transfer
);
413 pipe
->transfer_destroy(pipe
, transfer
);
418 ctx
->_ImageTransferState
= imageTransferStateSave
;
421 _mesa_unmap_pbo_source(ctx
, unpack
);
428 * Draw quad with texcoords and optional color.
429 * Coords are gallium window coords with y=0=top.
430 * \param color may be null
431 * \param invertTex if true, flip texcoords vertically
434 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
435 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
436 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
438 struct st_context
*st
= st_context(ctx
);
439 struct pipe_context
*pipe
= st
->pipe
;
440 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
442 /* setup vertex data */
444 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
445 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
446 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
447 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
448 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
449 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
450 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
451 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
452 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
453 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
457 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
458 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
461 verts
[1][0][0] = clip_x1
;
462 verts
[1][0][1] = clip_y0
;
465 verts
[2][0][0] = clip_x1
;
466 verts
[2][0][1] = clip_y1
;
469 verts
[3][0][0] = clip_x0
;
470 verts
[3][0][1] = clip_y1
;
472 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
473 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
474 verts
[1][1][0] = sRight
;
475 verts
[1][1][1] = tTop
;
476 verts
[2][1][0] = sRight
;
477 verts
[2][1][1] = tBot
;
478 verts
[3][1][0] = sLeft
;
479 verts
[3][1][1] = tBot
;
481 /* same for all verts: */
483 for (i
= 0; i
< 4; i
++) {
484 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
485 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
486 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
487 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
488 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
489 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
490 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
491 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
495 for (i
= 0; i
< 4; i
++) {
496 verts
[i
][0][2] = z
; /*Z*/
497 verts
[i
][0][3] = 1.0f
; /*W*/
498 verts
[i
][1][2] = 0.0f
; /*R*/
499 verts
[i
][1][3] = 1.0f
; /*Q*/
505 struct pipe_resource
*buf
;
507 /* allocate/load buffer object with vertex data */
508 buf
= pipe_buffer_create(pipe
->screen
,
509 PIPE_BIND_VERTEX_BUFFER
,
511 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
513 util_draw_vertex_buffer(pipe
, buf
, 0,
516 3); /* attribs/vert */
517 pipe_resource_reference(&buf
, NULL
);
524 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
525 GLsizei width
, GLsizei height
,
526 GLfloat zoomX
, GLfloat zoomY
,
527 struct pipe_sampler_view
**sv
,
528 int num_sampler_view
,
531 const GLfloat
*color
,
533 GLboolean write_depth
, GLboolean write_stencil
)
535 struct st_context
*st
= st_context(ctx
);
536 struct pipe_context
*pipe
= st
->pipe
;
537 struct cso_context
*cso
= st
->cso_context
;
538 GLfloat x0
, y0
, x1
, y1
;
540 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
543 /* XXX if DrawPixels image is larger than max texture size, break
546 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
547 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
548 assert(width
<= maxSize
);
549 assert(height
<= maxSize
);
551 cso_save_depth_stencil_alpha(cso
);
552 cso_save_rasterizer(cso
);
553 cso_save_viewport(cso
);
554 cso_save_samplers(cso
);
555 cso_save_fragment_sampler_views(cso
);
556 cso_save_fragment_shader(cso
);
557 cso_save_vertex_shader(cso
);
558 cso_save_vertex_elements(cso
);
560 /* rasterizer state: just scissor */
562 struct pipe_rasterizer_state rasterizer
;
563 memset(&rasterizer
, 0, sizeof(rasterizer
));
564 rasterizer
.gl_rasterization_rules
= 1;
565 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
566 cso_set_rasterizer(cso
, &rasterizer
);
569 if (write_depth
|| write_stencil
)
571 struct pipe_depth_stencil_alpha_state dsa
;
572 memset(&dsa
, 0, sizeof(dsa
));
574 dsa
.depth
.enabled
= 1;
575 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
576 dsa
.depth
.writemask
= 1;
579 dsa
.stencil
[0].enabled
= 1;
580 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
581 dsa
.stencil
[0].writemask
= 0xff;
582 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
584 cso_set_depth_stencil_alpha(cso
, &dsa
);
587 /* fragment shader state: TEX lookup program */
588 cso_set_fragment_shader_handle(cso
, driver_fp
);
590 /* vertex shader state: position + texcoord pass-through */
591 cso_set_vertex_shader_handle(cso
, driver_vp
);
594 /* texture sampling state: */
596 struct pipe_sampler_state sampler
;
597 memset(&sampler
, 0, sizeof(sampler
));
598 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
599 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
600 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
601 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
602 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
603 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
604 sampler
.normalized_coords
= normalized
;
606 cso_single_sampler(cso
, 0, &sampler
);
607 if (num_sampler_view
> 1) {
608 cso_single_sampler(cso
, 1, &sampler
);
610 cso_single_sampler_done(cso
);
613 /* viewport state: viewport matching window dims */
615 const float w
= (float) ctx
->DrawBuffer
->Width
;
616 const float h
= (float) ctx
->DrawBuffer
->Height
;
617 struct pipe_viewport_state vp
;
618 vp
.scale
[0] = 0.5f
* w
;
619 vp
.scale
[1] = -0.5f
* h
;
622 vp
.translate
[0] = 0.5f
* w
;
623 vp
.translate
[1] = 0.5f
* h
;
624 vp
.translate
[2] = 0.5f
;
625 vp
.translate
[3] = 0.0f
;
626 cso_set_viewport(cso
, &vp
);
629 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
632 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
634 /* Compute Gallium window coords (y=0=top) with pixel zoom.
635 * Recall that these coords are transformed by the current
636 * vertex shader and viewport transformation.
638 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
639 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
640 invertTex
= !invertTex
;
644 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
646 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
648 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
651 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
652 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
653 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
656 cso_restore_depth_stencil_alpha(cso
);
657 cso_restore_rasterizer(cso
);
658 cso_restore_viewport(cso
);
659 cso_restore_samplers(cso
);
660 cso_restore_fragment_sampler_views(cso
);
661 cso_restore_fragment_shader(cso
);
662 cso_restore_vertex_shader(cso
);
663 cso_restore_vertex_elements(cso
);
668 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
669 * can't use a fragment shader to write stencil values.
672 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
673 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
674 const struct gl_pixelstore_attrib
*unpack
,
675 const GLvoid
*pixels
)
677 struct st_context
*st
= st_context(ctx
);
678 struct pipe_context
*pipe
= st
->pipe
;
679 struct st_renderbuffer
*strb
;
680 enum pipe_transfer_usage usage
;
681 struct pipe_transfer
*pt
;
682 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
685 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
688 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
690 /* totally clipped */
695 strb
= st_renderbuffer(ctx
->DrawBuffer
->
696 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
698 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
699 y
= ctx
->DrawBuffer
->Height
- y
- height
;
702 if(format
!= GL_DEPTH_STENCIL
&&
703 util_format_get_component_bits(strb
->format
,
704 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
705 usage
= PIPE_TRANSFER_READ_WRITE
;
707 usage
= PIPE_TRANSFER_WRITE
;
709 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0, 0,
713 stmap
= pipe_transfer_map(pipe
, pt
);
715 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
718 /* if width > MAX_WIDTH, have to process image in chunks */
720 while (skipPixels
< width
) {
721 const GLint spanX
= skipPixels
;
722 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
724 for (row
= 0; row
< height
; row
++) {
725 GLubyte sValues
[MAX_WIDTH
];
726 GLuint zValues
[MAX_WIDTH
];
727 GLenum destType
= GL_UNSIGNED_BYTE
;
728 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
732 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
733 type
, source
, &clippedUnpack
,
734 ctx
->_ImageTransferState
);
736 if (format
== GL_DEPTH_STENCIL
) {
737 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
738 (1 << 24) - 1, type
, source
,
743 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
744 "zoom not complete");
750 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
751 spanY
= height
- row
- 1;
757 /* now pack the stencil (and Z) values in the dest format */
758 switch (pt
->resource
->format
) {
759 case PIPE_FORMAT_S8_USCALED
:
761 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
762 assert(usage
== PIPE_TRANSFER_WRITE
);
763 memcpy(dest
, sValues
, spanWidth
);
766 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
767 if (format
== GL_DEPTH_STENCIL
) {
768 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
770 assert(usage
== PIPE_TRANSFER_WRITE
);
771 for (k
= 0; k
< spanWidth
; k
++) {
772 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
776 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
778 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
779 for (k
= 0; k
< spanWidth
; k
++) {
780 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
784 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
785 if (format
== GL_DEPTH_STENCIL
) {
786 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
788 assert(usage
== PIPE_TRANSFER_WRITE
);
789 for (k
= 0; k
< spanWidth
; k
++) {
790 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
794 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
796 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
797 for (k
= 0; k
< spanWidth
; k
++) {
798 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
807 skipPixels
+= spanWidth
;
810 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
812 /* unmap the stencil buffer */
813 pipe_transfer_unmap(pipe
, pt
);
814 pipe
->transfer_destroy(pipe
, pt
);
819 * Called via ctx->Driver.DrawPixels()
822 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
823 GLsizei width
, GLsizei height
,
824 GLenum format
, GLenum type
,
825 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
827 void *driver_vp
, *driver_fp
;
828 struct st_context
*st
= st_context(ctx
);
829 const GLfloat
*color
;
830 struct pipe_context
*pipe
= st
->pipe
;
831 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
832 struct pipe_sampler_view
*sv
[2];
833 int num_sampler_view
= 1;
834 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
836 if (format
== GL_DEPTH_STENCIL
)
837 write_stencil
= write_depth
= GL_TRUE
;
838 else if (format
== GL_STENCIL_INDEX
)
839 write_stencil
= GL_TRUE
;
840 else if (format
== GL_DEPTH_COMPONENT
)
841 write_depth
= GL_TRUE
;
844 enum pipe_format tex_format
;
845 /* can we write to stencil if not fallback */
846 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
847 goto stencil_fallback
;
849 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
851 0, PIPE_BIND_SAMPLER_VIEW
);
852 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
853 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
854 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
855 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
857 stencil_format
= PIPE_FORMAT_S8_USCALED
;
858 if (stencil_format
== PIPE_FORMAT_NONE
)
859 goto stencil_fallback
;
862 /* Mesa state should be up to date by now */
863 assert(ctx
->NewState
== 0x0);
865 st_validate_state(st
);
867 if (write_depth
|| write_stencil
) {
868 driver_fp
= make_fragment_shader_z_stencil(st
, write_depth
, write_stencil
);
869 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
870 color
= ctx
->Current
.RasterColor
;
873 driver_fp
= combined_drawpix_fragment_program(ctx
);
874 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
876 if (st
->pixel_xfer
.pixelmap_enabled
) {
877 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
882 /* draw with textured quad */
884 struct pipe_resource
*pt
885 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
887 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
891 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
896 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
898 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
903 color
, GL_FALSE
, write_depth
, write_stencil
);
904 pipe_sampler_view_reference(&sv
[0], NULL
);
905 if (num_sampler_view
> 1)
906 pipe_sampler_view_reference(&sv
[1], NULL
);
908 pipe_resource_reference(&pt
, NULL
);
914 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
921 * Software fallback for glCopyPixels(GL_STENCIL).
924 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
925 GLsizei width
, GLsizei height
,
926 GLint dstx
, GLint dsty
)
928 struct st_renderbuffer
*rbDraw
;
929 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
930 enum pipe_transfer_usage usage
;
931 struct pipe_transfer
*ptDraw
;
936 buffer
= malloc(width
* height
* sizeof(ubyte
));
938 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
942 /* Get the dest renderbuffer. If there's a wrapper, use the
943 * underlying renderbuffer.
945 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
946 if (rbDraw
->Base
.Wrapped
)
947 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
949 /* this will do stencil pixel transfer ops */
950 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
951 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
952 &ctx
->DefaultPacking
, buffer
);
954 if (util_format_get_component_bits(rbDraw
->format
,
955 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
956 usage
= PIPE_TRANSFER_READ_WRITE
;
958 usage
= PIPE_TRANSFER_WRITE
;
960 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
961 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
964 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
965 rbDraw
->texture
, 0, 0, 0,
969 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
970 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
972 /* map the stencil buffer */
973 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
976 /* XXX PixelZoom not handled yet */
977 for (i
= 0; i
< height
; i
++) {
984 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
988 dst
= drawMap
+ y
* ptDraw
->stride
;
989 src
= buffer
+ i
* width
;
991 switch (ptDraw
->resource
->format
) {
992 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
994 uint
*dst4
= (uint
*) dst
;
996 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
997 for (j
= 0; j
< width
; j
++) {
998 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1003 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1005 uint
*dst4
= (uint
*) dst
;
1007 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1008 for (j
= 0; j
< width
; j
++) {
1009 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1014 case PIPE_FORMAT_S8_USCALED
:
1015 assert(usage
== PIPE_TRANSFER_WRITE
);
1016 memcpy(dst
, src
, width
);
1025 /* unmap the stencil buffer */
1026 pipe_transfer_unmap(pipe
, ptDraw
);
1027 pipe
->transfer_destroy(pipe
, ptDraw
);
1032 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1033 GLsizei width
, GLsizei height
,
1034 GLint dstx
, GLint dsty
, GLenum type
)
1036 struct st_context
*st
= st_context(ctx
);
1037 struct pipe_context
*pipe
= st
->pipe
;
1038 struct pipe_screen
*screen
= pipe
->screen
;
1039 struct st_renderbuffer
*rbRead
;
1040 void *driver_vp
, *driver_fp
;
1041 struct pipe_resource
*pt
;
1042 struct pipe_sampler_view
*sv
[2];
1043 int num_sampler_view
= 1;
1045 enum pipe_format srcFormat
, texFormat
;
1046 GLboolean invertTex
= GL_FALSE
;
1047 GLint readX
, readY
, readW
, readH
;
1048 GLuint sample_count
;
1049 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1051 st_validate_state(st
);
1053 if (type
== GL_STENCIL
) {
1054 /* can't use texturing to do stencil */
1055 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1059 if (type
== GL_COLOR
) {
1060 rbRead
= st_get_color_read_renderbuffer(ctx
);
1062 driver_fp
= combined_drawpix_fragment_program(ctx
);
1063 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1064 if (st
->pixel_xfer
.pixelmap_enabled
) {
1065 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1070 assert(type
== GL_DEPTH
);
1071 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1072 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1073 driver_fp
= make_fragment_shader_z_stencil(st
, GL_TRUE
, GL_FALSE
);
1074 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1077 if (rbRead
->Base
.Wrapped
)
1078 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1080 sample_count
= rbRead
->texture
->nr_samples
;
1081 /* I believe this would be legal, presumably would need to do a resolve
1082 for color, and for depth/stencil spec says to just use one of the
1083 depth/stencil samples per pixel? Need some transfer clarifications. */
1084 assert(sample_count
< 2);
1086 srcFormat
= rbRead
->texture
->format
;
1088 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1090 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1091 texFormat
= srcFormat
;
1094 /* srcFormat can't be used as a texture format */
1095 if (type
== GL_DEPTH
) {
1096 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1097 st
->internal_target
, sample_count
,
1098 PIPE_BIND_DEPTH_STENCIL
);
1099 assert(texFormat
!= PIPE_FORMAT_NONE
);
1102 /* default color format */
1103 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1104 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1105 assert(texFormat
!= PIPE_FORMAT_NONE
);
1109 /* Invert src region if needed */
1110 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1111 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1112 invertTex
= !invertTex
;
1115 /* Clip the read region against the src buffer bounds.
1116 * We'll still allocate a temporary buffer/texture for the original
1117 * src region size but we'll only read the region which is on-screen.
1118 * This may mean that we draw garbage pixels into the dest region, but
1125 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1126 readW
= MAX2(0, readW
);
1127 readH
= MAX2(0, readH
);
1129 /* alloc temporary texture */
1130 pt
= alloc_texture(st
, width
, height
, texFormat
);
1134 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1136 pipe_resource_reference(&pt
, NULL
);
1140 /* Make temporary texture which is a copy of the src region.
1142 if (srcFormat
== texFormat
) {
1143 struct pipe_subresource srcsub
, dstsub
;
1148 /* copy source framebuffer surface into mipmap/texture */
1149 pipe
->resource_copy_region(pipe
,
1152 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1153 rbRead
->texture
, /* src tex */
1155 readX
, readY
, 0, readW
, readH
); /* src region */
1159 /* CPU-based fallback/conversion */
1160 struct pipe_transfer
*ptRead
=
1161 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0, 0,
1163 readX
, readY
, readW
, readH
);
1164 struct pipe_transfer
*ptTex
;
1165 enum pipe_transfer_usage transfer_usage
;
1167 if (ST_DEBUG
& DEBUG_FALLBACK
)
1168 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1170 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1171 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1173 transfer_usage
= PIPE_TRANSFER_WRITE
;
1175 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0, transfer_usage
,
1176 0, 0, width
, height
);
1178 /* copy image from ptRead surface to ptTex surface */
1179 if (type
== GL_COLOR
) {
1180 /* alternate path using get/put_tile() */
1181 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1182 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1183 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1189 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1190 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1191 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1196 pipe
->transfer_destroy(pipe
, ptRead
);
1197 pipe
->transfer_destroy(pipe
, ptTex
);
1200 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1201 * textured quad with that texture.
1203 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1204 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1209 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1211 pipe_resource_reference(&pt
, NULL
);
1212 pipe_sampler_view_reference(&sv
[0], NULL
);
1217 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1219 functions
->DrawPixels
= st_DrawPixels
;
1220 functions
->CopyPixels
= st_CopyPixels
;
1225 st_destroy_drawpix(struct st_context
*st
)
1229 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1230 if (st
->drawpix
.shaders
[i
])
1231 st_reference_fragprog(st
, &st
->drawpix
.shaders
[i
], NULL
);
1234 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1235 if (st
->drawpix
.vert_shaders
[0])
1236 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1237 if (st
->drawpix
.vert_shaders
[1])
1238 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1241 #endif /* FEATURE_drawpix */