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"
40 #include "main/texformat.h"
41 #include "main/texstore.h"
42 #include "program/program.h"
43 #include "program/prog_print.h"
44 #include "program/prog_instruction.h"
47 #include "st_atom_constbuf.h"
48 #include "st_cb_drawpixels.h"
49 #include "st_cb_readpixels.h"
50 #include "st_cb_fbo.h"
51 #include "st_context.h"
53 #include "st_format.h"
54 #include "st_program.h"
55 #include "st_texture.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "tgsi/tgsi_ureg.h"
60 #include "util/u_draw_quad.h"
61 #include "util/u_format.h"
62 #include "util/u_inlines.h"
63 #include "util/u_math.h"
64 #include "util/u_tile.h"
65 #include "cso_cache/cso_context.h"
71 * Check if the given program is:
72 * 0: MOVE result.color, fragment.color;
76 is_passthrough_program(const struct gl_fragment_program
*prog
)
78 if (prog
->Base
.NumInstructions
== 2) {
79 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
80 if (inst
[0].Opcode
== OPCODE_MOV
&&
81 inst
[1].Opcode
== OPCODE_END
&&
82 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
83 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
84 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
85 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
86 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
87 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
97 * Make fragment shader for glDraw/CopyPixels. This shader is made
98 * by combining the pixel transfer shader with the user-defined shader.
99 * \param fpIn the current/incoming fragment program
100 * \param fpOut returns the combined fragment program
103 st_make_drawpix_fragment_program(struct st_context
*st
,
104 struct gl_fragment_program
*fpIn
,
105 struct gl_fragment_program
**fpOut
)
107 struct gl_program
*newProg
;
109 if (is_passthrough_program(fpIn
)) {
110 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
111 &st
->pixel_xfer
.program
->Base
);
116 printf("Base program:\n");
117 _mesa_print_program(&fpIn
->Base
);
118 printf("DrawPix program:\n");
119 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
121 newProg
= _mesa_combine_programs(st
->ctx
,
122 &st
->pixel_xfer
.program
->Base
.Base
,
128 printf("Combined DrawPixels program:\n");
129 _mesa_print_program(newProg
);
130 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
131 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
132 _mesa_print_parameter_list(newProg
->Parameters
);
135 *fpOut
= (struct gl_fragment_program
*) newProg
;
140 * Create fragment program that does a TEX() instruction to get a Z and/or
141 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
142 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
143 * Pass fragment color through as-is.
144 * \return pointer to the gl_fragment program
146 struct gl_fragment_program
*
147 st_make_drawpix_z_stencil_program(struct st_context
*st
,
148 GLboolean write_depth
,
149 GLboolean write_stencil
)
151 struct gl_context
*ctx
= st
->ctx
;
152 struct gl_program
*p
;
153 struct gl_fragment_program
*fp
;
155 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
157 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
159 if (st
->drawpix
.shaders
[shaderIndex
]) {
160 /* already have the proper shader */
161 return st
->drawpix
.shaders
[shaderIndex
];
167 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
171 p
->NumInstructions
= write_depth
? 2 : 1;
172 p
->NumInstructions
+= write_stencil
? 1 : 0;
174 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
175 if (!p
->Instructions
) {
176 ctx
->Driver
.DeleteProgram(ctx
, p
);
179 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
182 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
183 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
184 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
185 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
186 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
187 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
188 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
189 p
->Instructions
[ic
].TexSrcUnit
= 0;
190 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
195 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
196 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
197 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
198 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
199 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
200 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
201 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
202 p
->Instructions
[ic
].TexSrcUnit
= 1;
203 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
208 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
210 assert(ic
== p
->NumInstructions
);
212 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
213 p
->OutputsWritten
= 0;
215 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
217 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
219 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
221 p
->SamplersUsed
|= 1 << 1;
223 fp
= (struct gl_fragment_program
*) p
;
225 /* save the new shader */
226 st
->drawpix
.shaders
[shaderIndex
] = fp
;
233 * Create a simple vertex shader that just passes through the
234 * vertex position and texcoord (and optionally, color).
237 make_passthrough_vertex_shader(struct st_context
*st
,
240 if (!st
->drawpix
.vert_shaders
[passColor
]) {
241 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
246 /* MOV result.pos, vertex.pos; */
248 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
249 ureg_DECL_vs_input( ureg
, 0 ));
251 /* MOV result.texcoord0, vertex.attr[1]; */
253 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
254 ureg_DECL_vs_input( ureg
, 1 ));
257 /* MOV result.color0, vertex.attr[2]; */
259 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
260 ureg_DECL_vs_input( ureg
, 2 ));
265 st
->drawpix
.vert_shaders
[passColor
] =
266 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
269 return st
->drawpix
.vert_shaders
[passColor
];
274 * Return a texture base format for drawing/copying an image
275 * of the given format.
278 base_format(GLenum format
)
281 case GL_DEPTH_COMPONENT
:
282 return GL_DEPTH_COMPONENT
;
283 case GL_DEPTH_STENCIL
:
284 return GL_DEPTH_STENCIL
;
285 case GL_STENCIL_INDEX
:
286 return GL_STENCIL_INDEX
;
294 * Return a texture internalFormat for drawing/copying an image
295 * of the given format and type.
298 internal_format(GLenum format
, GLenum type
)
301 case GL_DEPTH_COMPONENT
:
302 return GL_DEPTH_COMPONENT
;
303 case GL_DEPTH_STENCIL
:
304 return GL_DEPTH_STENCIL
;
305 case GL_STENCIL_INDEX
:
306 return GL_STENCIL_INDEX
;
308 if (_mesa_is_integer_format(format
)) {
312 case GL_UNSIGNED_BYTE
:
316 case GL_UNSIGNED_SHORT
:
320 case GL_UNSIGNED_INT
:
323 assert(0 && "Unexpected type in internal_format()");
324 return GL_RGBA_INTEGER
;
335 * Create a temporary texture to hold an image of the given size.
336 * If width, height are not POT and the driver only handles POT textures,
337 * allocate the next larger size of texture that is POT.
339 static struct pipe_resource
*
340 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
341 enum pipe_format texFormat
)
343 struct pipe_resource
*pt
;
345 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
346 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
353 * Make texture containing an image for glDrawPixels image.
354 * If 'pixels' is NULL, leave the texture image data undefined.
356 static struct pipe_resource
*
357 make_texture(struct st_context
*st
,
358 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
359 const struct gl_pixelstore_attrib
*unpack
,
360 const GLvoid
*pixels
)
362 struct gl_context
*ctx
= st
->ctx
;
363 struct pipe_context
*pipe
= st
->pipe
;
365 struct pipe_resource
*pt
;
366 enum pipe_format pipeFormat
;
368 GLenum baseFormat
, intFormat
;
370 baseFormat
= base_format(format
);
371 intFormat
= internal_format(format
, type
);
373 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
374 format
, type
, GL_FALSE
);
377 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
379 cpp
= util_format_get_blocksize(pipeFormat
);
381 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
385 /* alloc temporary texture */
386 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
388 _mesa_unmap_pbo_source(ctx
, unpack
);
393 struct pipe_transfer
*transfer
;
394 static const GLuint dstImageOffsets
= 0;
397 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
399 /* we'll do pixel transfer in a fragment shader */
400 ctx
->_ImageTransferState
= 0x0;
402 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
403 PIPE_TRANSFER_WRITE
, 0, 0,
406 /* map texture transfer */
407 dest
= pipe_transfer_map(pipe
, transfer
);
410 /* Put image into texture transfer.
411 * Note that the image is actually going to be upside down in
412 * the texture. We deal with that with texcoords.
414 success
= _mesa_texstore(ctx
, 2, /* dims */
415 baseFormat
, /* baseInternalFormat */
416 mformat
, /* gl_format */
418 0, 0, 0, /* dstX/Y/Zoffset */
419 transfer
->stride
, /* dstRowStride, bytes */
420 &dstImageOffsets
, /* dstImageOffsets */
421 width
, height
, 1, /* size */
422 format
, type
, /* src format/type */
423 pixels
, /* data source */
427 pipe_transfer_unmap(pipe
, transfer
);
428 pipe
->transfer_destroy(pipe
, transfer
);
433 ctx
->_ImageTransferState
= imageTransferStateSave
;
436 _mesa_unmap_pbo_source(ctx
, unpack
);
443 * Draw quad with texcoords and optional color.
444 * Coords are gallium window coords with y=0=top.
445 * \param color may be null
446 * \param invertTex if true, flip texcoords vertically
449 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
450 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
451 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
453 struct st_context
*st
= st_context(ctx
);
454 struct pipe_context
*pipe
= st
->pipe
;
455 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
457 /* setup vertex data */
459 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
460 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
461 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
462 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
463 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
464 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
465 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
466 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
467 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
468 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
472 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
473 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
476 verts
[1][0][0] = clip_x1
;
477 verts
[1][0][1] = clip_y0
;
480 verts
[2][0][0] = clip_x1
;
481 verts
[2][0][1] = clip_y1
;
484 verts
[3][0][0] = clip_x0
;
485 verts
[3][0][1] = clip_y1
;
487 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
488 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
489 verts
[1][1][0] = sRight
;
490 verts
[1][1][1] = tTop
;
491 verts
[2][1][0] = sRight
;
492 verts
[2][1][1] = tBot
;
493 verts
[3][1][0] = sLeft
;
494 verts
[3][1][1] = tBot
;
496 /* same for all verts: */
498 for (i
= 0; i
< 4; i
++) {
499 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
500 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
501 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
502 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
503 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
504 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
505 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
506 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
510 for (i
= 0; i
< 4; i
++) {
511 verts
[i
][0][2] = z
; /*Z*/
512 verts
[i
][0][3] = 1.0f
; /*W*/
513 verts
[i
][1][2] = 0.0f
; /*R*/
514 verts
[i
][1][3] = 1.0f
; /*Q*/
520 struct pipe_resource
*buf
;
522 /* allocate/load buffer object with vertex data */
523 buf
= pipe_buffer_create(pipe
->screen
,
524 PIPE_BIND_VERTEX_BUFFER
,
526 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
528 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
531 3); /* attribs/vert */
532 pipe_resource_reference(&buf
, NULL
);
539 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
540 GLsizei width
, GLsizei height
,
541 GLfloat zoomX
, GLfloat zoomY
,
542 struct pipe_sampler_view
**sv
,
543 int num_sampler_view
,
546 const GLfloat
*color
,
548 GLboolean write_depth
, GLboolean write_stencil
)
550 struct st_context
*st
= st_context(ctx
);
551 struct pipe_context
*pipe
= st
->pipe
;
552 struct cso_context
*cso
= st
->cso_context
;
553 GLfloat x0
, y0
, x1
, y1
;
555 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
558 /* XXX if DrawPixels image is larger than max texture size, break
561 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
562 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
563 assert(width
<= maxSize
);
564 assert(height
<= maxSize
);
566 cso_save_rasterizer(cso
);
567 cso_save_viewport(cso
);
568 cso_save_samplers(cso
);
569 cso_save_fragment_sampler_views(cso
);
570 cso_save_fragment_shader(cso
);
571 cso_save_vertex_shader(cso
);
572 cso_save_vertex_elements(cso
);
573 cso_save_vertex_buffers(cso
);
575 cso_save_depth_stencil_alpha(cso
);
579 /* rasterizer state: just scissor */
581 struct pipe_rasterizer_state rasterizer
;
582 memset(&rasterizer
, 0, sizeof(rasterizer
));
583 rasterizer
.gl_rasterization_rules
= 1;
584 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
585 cso_set_rasterizer(cso
, &rasterizer
);
589 /* Stencil writing bypasses the normal fragment pipeline to
590 * disable color writing and set stencil test to always pass.
592 struct pipe_depth_stencil_alpha_state dsa
;
593 struct pipe_blend_state blend
;
596 memset(&dsa
, 0, sizeof(dsa
));
597 dsa
.stencil
[0].enabled
= 1;
598 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
599 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
600 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
602 /* writing depth+stencil: depth test always passes */
603 dsa
.depth
.enabled
= 1;
604 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
605 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
607 cso_set_depth_stencil_alpha(cso
, &dsa
);
609 /* blend (colormask) */
610 memset(&blend
, 0, sizeof(blend
));
611 cso_set_blend(cso
, &blend
);
614 /* fragment shader state: TEX lookup program */
615 cso_set_fragment_shader_handle(cso
, driver_fp
);
617 /* vertex shader state: position + texcoord pass-through */
618 cso_set_vertex_shader_handle(cso
, driver_vp
);
621 /* texture sampling state: */
623 struct pipe_sampler_state sampler
;
624 memset(&sampler
, 0, sizeof(sampler
));
625 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
626 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
627 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
628 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
629 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
630 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
631 sampler
.normalized_coords
= normalized
;
633 cso_single_sampler(cso
, 0, &sampler
);
634 if (num_sampler_view
> 1) {
635 cso_single_sampler(cso
, 1, &sampler
);
637 cso_single_sampler_done(cso
);
640 /* viewport state: viewport matching window dims */
642 const float w
= (float) ctx
->DrawBuffer
->Width
;
643 const float h
= (float) ctx
->DrawBuffer
->Height
;
644 struct pipe_viewport_state vp
;
645 vp
.scale
[0] = 0.5f
* w
;
646 vp
.scale
[1] = -0.5f
* h
;
649 vp
.translate
[0] = 0.5f
* w
;
650 vp
.translate
[1] = 0.5f
* h
;
651 vp
.translate
[2] = 0.5f
;
652 vp
.translate
[3] = 0.0f
;
653 cso_set_viewport(cso
, &vp
);
656 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
659 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
661 /* Compute Gallium window coords (y=0=top) with pixel zoom.
662 * Recall that these coords are transformed by the current
663 * vertex shader and viewport transformation.
665 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
666 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
667 invertTex
= !invertTex
;
671 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
673 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
675 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
678 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
679 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
680 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
683 cso_restore_rasterizer(cso
);
684 cso_restore_viewport(cso
);
685 cso_restore_samplers(cso
);
686 cso_restore_fragment_sampler_views(cso
);
687 cso_restore_fragment_shader(cso
);
688 cso_restore_vertex_shader(cso
);
689 cso_restore_vertex_elements(cso
);
690 cso_restore_vertex_buffers(cso
);
692 cso_restore_depth_stencil_alpha(cso
);
693 cso_restore_blend(cso
);
699 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
700 * can't use a fragment shader to write stencil values.
703 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
704 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
705 const struct gl_pixelstore_attrib
*unpack
,
706 const GLvoid
*pixels
)
708 struct st_context
*st
= st_context(ctx
);
709 struct pipe_context
*pipe
= st
->pipe
;
710 struct st_renderbuffer
*strb
;
711 enum pipe_transfer_usage usage
;
712 struct pipe_transfer
*pt
;
713 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
716 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
719 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
721 /* totally clipped */
726 strb
= st_renderbuffer(ctx
->DrawBuffer
->
727 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
729 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
730 y
= ctx
->DrawBuffer
->Height
- y
- height
;
733 if(format
!= GL_DEPTH_STENCIL
&&
734 util_format_get_component_bits(strb
->format
,
735 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
736 usage
= PIPE_TRANSFER_READ_WRITE
;
738 usage
= PIPE_TRANSFER_WRITE
;
740 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0,
744 stmap
= pipe_transfer_map(pipe
, pt
);
746 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
749 /* if width > MAX_WIDTH, have to process image in chunks */
751 while (skipPixels
< width
) {
752 const GLint spanX
= skipPixels
;
753 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
755 for (row
= 0; row
< height
; row
++) {
756 GLubyte sValues
[MAX_WIDTH
];
757 GLuint zValues
[MAX_WIDTH
];
758 GLenum destType
= GL_UNSIGNED_BYTE
;
759 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
763 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
764 type
, source
, &clippedUnpack
,
765 ctx
->_ImageTransferState
);
767 if (format
== GL_DEPTH_STENCIL
) {
768 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
769 (1 << 24) - 1, type
, source
,
774 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
775 "zoom not complete");
781 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
782 spanY
= height
- row
- 1;
788 /* now pack the stencil (and Z) values in the dest format */
789 switch (pt
->resource
->format
) {
790 case PIPE_FORMAT_S8_USCALED
:
792 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
793 assert(usage
== PIPE_TRANSFER_WRITE
);
794 memcpy(dest
, sValues
, spanWidth
);
797 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
798 if (format
== GL_DEPTH_STENCIL
) {
799 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
801 assert(usage
== PIPE_TRANSFER_WRITE
);
802 for (k
= 0; k
< spanWidth
; k
++) {
803 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
807 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
809 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
810 for (k
= 0; k
< spanWidth
; k
++) {
811 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
815 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
816 if (format
== GL_DEPTH_STENCIL
) {
817 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
819 assert(usage
== PIPE_TRANSFER_WRITE
);
820 for (k
= 0; k
< spanWidth
; k
++) {
821 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
825 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
827 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
828 for (k
= 0; k
< spanWidth
; k
++) {
829 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
838 skipPixels
+= spanWidth
;
841 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
843 /* unmap the stencil buffer */
844 pipe_transfer_unmap(pipe
, pt
);
845 pipe
->transfer_destroy(pipe
, pt
);
850 * Get fragment program variant for a glDrawPixels or glCopyPixels
851 * command for RGBA data.
853 static struct st_fp_variant
*
854 get_color_fp_variant(struct st_context
*st
)
856 struct gl_context
*ctx
= st
->ctx
;
857 struct st_fp_variant_key key
;
858 struct st_fp_variant
*fpv
;
860 memset(&key
, 0, sizeof(key
));
864 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
865 ctx
->Pixel
.RedScale
!= 1.0 ||
866 ctx
->Pixel
.GreenBias
!= 0.0 ||
867 ctx
->Pixel
.GreenScale
!= 1.0 ||
868 ctx
->Pixel
.BlueBias
!= 0.0 ||
869 ctx
->Pixel
.BlueScale
!= 1.0 ||
870 ctx
->Pixel
.AlphaBias
!= 0.0 ||
871 ctx
->Pixel
.AlphaScale
!= 1.0);
872 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
874 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
881 * Get fragment program variant for a glDrawPixels or glCopyPixels
882 * command for depth/stencil data.
884 static struct st_fp_variant
*
885 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
886 GLboolean write_stencil
)
888 struct st_fp_variant_key key
;
889 struct st_fp_variant
*fpv
;
891 memset(&key
, 0, sizeof(key
));
895 key
.drawpixels_z
= write_depth
;
896 key
.drawpixels_stencil
= write_stencil
;
898 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
905 * Called via ctx->Driver.DrawPixels()
908 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
909 GLsizei width
, GLsizei height
,
910 GLenum format
, GLenum type
,
911 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
913 void *driver_vp
, *driver_fp
;
914 struct st_context
*st
= st_context(ctx
);
915 const GLfloat
*color
;
916 struct pipe_context
*pipe
= st
->pipe
;
917 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
918 struct pipe_sampler_view
*sv
[2];
919 int num_sampler_view
= 1;
920 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
921 struct st_fp_variant
*fpv
;
923 if (format
== GL_DEPTH_STENCIL
)
924 write_stencil
= write_depth
= GL_TRUE
;
925 else if (format
== GL_STENCIL_INDEX
)
926 write_stencil
= GL_TRUE
;
927 else if (format
== GL_DEPTH_COMPONENT
)
928 write_depth
= GL_TRUE
;
931 enum pipe_format tex_format
;
932 /* can we write to stencil if not fallback */
933 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
934 goto stencil_fallback
;
936 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
938 0, PIPE_BIND_SAMPLER_VIEW
);
939 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
940 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
941 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
942 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
944 stencil_format
= PIPE_FORMAT_S8_USCALED
;
945 if (stencil_format
== PIPE_FORMAT_NONE
)
946 goto stencil_fallback
;
949 /* Mesa state should be up to date by now */
950 assert(ctx
->NewState
== 0x0);
952 st_validate_state(st
);
955 * Get vertex/fragment shaders
957 if (write_depth
|| write_stencil
) {
958 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
960 driver_fp
= fpv
->driver_shader
;
962 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
964 color
= ctx
->Current
.RasterColor
;
967 fpv
= get_color_fp_variant(st
);
969 driver_fp
= fpv
->driver_shader
;
971 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
974 if (st
->pixel_xfer
.pixelmap_enabled
) {
975 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
980 /* update fragment program constants */
981 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
983 /* draw with textured quad */
985 struct pipe_resource
*pt
986 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
988 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
992 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
997 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
999 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1004 color
, GL_FALSE
, write_depth
, write_stencil
);
1005 pipe_sampler_view_reference(&sv
[0], NULL
);
1006 if (num_sampler_view
> 1)
1007 pipe_sampler_view_reference(&sv
[1], NULL
);
1009 pipe_resource_reference(&pt
, NULL
);
1015 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1022 * Software fallback for glCopyPixels(GL_STENCIL).
1025 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1026 GLsizei width
, GLsizei height
,
1027 GLint dstx
, GLint dsty
)
1029 struct st_renderbuffer
*rbDraw
;
1030 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1031 enum pipe_transfer_usage usage
;
1032 struct pipe_transfer
*ptDraw
;
1037 buffer
= malloc(width
* height
* sizeof(ubyte
));
1039 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1043 /* Get the dest renderbuffer. If there's a wrapper, use the
1044 * underlying renderbuffer.
1046 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1047 if (rbDraw
->Base
.Wrapped
)
1048 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1050 /* this will do stencil pixel transfer ops */
1051 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1052 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1053 &ctx
->DefaultPacking
, buffer
);
1056 /* debug code: dump stencil values */
1058 for (row
= 0; row
< height
; row
++) {
1059 printf("%3d: ", row
);
1060 for (col
= 0; col
< width
; col
++) {
1061 printf("%02x ", buffer
[col
+ row
* width
]);
1067 if (util_format_get_component_bits(rbDraw
->format
,
1068 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1069 usage
= PIPE_TRANSFER_READ_WRITE
;
1071 usage
= PIPE_TRANSFER_WRITE
;
1073 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1074 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1077 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
1078 rbDraw
->texture
, 0, 0,
1082 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1083 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1085 /* map the stencil buffer */
1086 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1089 /* XXX PixelZoom not handled yet */
1090 for (i
= 0; i
< height
; i
++) {
1097 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1101 dst
= drawMap
+ y
* ptDraw
->stride
;
1102 src
= buffer
+ i
* width
;
1104 switch (ptDraw
->resource
->format
) {
1105 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1107 uint
*dst4
= (uint
*) dst
;
1109 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1110 for (j
= 0; j
< width
; j
++) {
1111 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1116 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1118 uint
*dst4
= (uint
*) dst
;
1120 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1121 for (j
= 0; j
< width
; j
++) {
1122 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1127 case PIPE_FORMAT_S8_USCALED
:
1128 assert(usage
== PIPE_TRANSFER_WRITE
);
1129 memcpy(dst
, src
, width
);
1138 /* unmap the stencil buffer */
1139 pipe_transfer_unmap(pipe
, ptDraw
);
1140 pipe
->transfer_destroy(pipe
, ptDraw
);
1145 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1146 GLsizei width
, GLsizei height
,
1147 GLint dstx
, GLint dsty
, GLenum type
)
1149 struct st_context
*st
= st_context(ctx
);
1150 struct pipe_context
*pipe
= st
->pipe
;
1151 struct pipe_screen
*screen
= pipe
->screen
;
1152 struct st_renderbuffer
*rbRead
;
1153 void *driver_vp
, *driver_fp
;
1154 struct pipe_resource
*pt
;
1155 struct pipe_sampler_view
*sv
[2];
1156 int num_sampler_view
= 1;
1158 enum pipe_format srcFormat
, texFormat
;
1159 GLboolean invertTex
= GL_FALSE
;
1160 GLint readX
, readY
, readW
, readH
;
1161 GLuint sample_count
;
1162 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1163 struct st_fp_variant
*fpv
;
1165 st_validate_state(st
);
1167 if (type
== GL_STENCIL
) {
1168 /* can't use texturing to do stencil */
1169 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1174 * Get vertex/fragment shaders
1176 if (type
== GL_COLOR
) {
1177 rbRead
= st_get_color_read_renderbuffer(ctx
);
1180 fpv
= get_color_fp_variant(st
);
1181 driver_fp
= fpv
->driver_shader
;
1183 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1185 if (st
->pixel_xfer
.pixelmap_enabled
) {
1186 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1191 assert(type
== GL_DEPTH
);
1192 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1193 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1195 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1196 driver_fp
= fpv
->driver_shader
;
1198 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1201 /* update fragment program constants */
1202 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1205 if (rbRead
->Base
.Wrapped
)
1206 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1208 sample_count
= rbRead
->texture
->nr_samples
;
1209 /* I believe this would be legal, presumably would need to do a resolve
1210 for color, and for depth/stencil spec says to just use one of the
1211 depth/stencil samples per pixel? Need some transfer clarifications. */
1212 assert(sample_count
< 2);
1214 srcFormat
= rbRead
->texture
->format
;
1216 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1218 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1219 texFormat
= srcFormat
;
1222 /* srcFormat can't be used as a texture format */
1223 if (type
== GL_DEPTH
) {
1224 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1225 st
->internal_target
, sample_count
,
1226 PIPE_BIND_DEPTH_STENCIL
);
1227 assert(texFormat
!= PIPE_FORMAT_NONE
);
1230 /* default color format */
1231 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1232 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1233 assert(texFormat
!= PIPE_FORMAT_NONE
);
1237 /* Invert src region if needed */
1238 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1239 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1240 invertTex
= !invertTex
;
1243 /* Clip the read region against the src buffer bounds.
1244 * We'll still allocate a temporary buffer/texture for the original
1245 * src region size but we'll only read the region which is on-screen.
1246 * This may mean that we draw garbage pixels into the dest region, but
1253 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1254 readW
= MAX2(0, readW
);
1255 readH
= MAX2(0, readH
);
1257 /* alloc temporary texture */
1258 pt
= alloc_texture(st
, width
, height
, texFormat
);
1262 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1264 pipe_resource_reference(&pt
, NULL
);
1268 /* Make temporary texture which is a copy of the src region.
1270 if (srcFormat
== texFormat
) {
1271 struct pipe_box src_box
;
1272 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1273 /* copy source framebuffer surface into mipmap/texture */
1274 pipe
->resource_copy_region(pipe
,
1277 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1278 rbRead
->texture
, /* src tex */
1284 /* CPU-based fallback/conversion */
1285 struct pipe_transfer
*ptRead
=
1286 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0,
1288 readX
, readY
, readW
, readH
);
1289 struct pipe_transfer
*ptTex
;
1290 enum pipe_transfer_usage transfer_usage
;
1292 if (ST_DEBUG
& DEBUG_FALLBACK
)
1293 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1295 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1296 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1298 transfer_usage
= PIPE_TRANSFER_WRITE
;
1300 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1301 0, 0, width
, height
);
1303 /* copy image from ptRead surface to ptTex surface */
1304 if (type
== GL_COLOR
) {
1305 /* alternate path using get/put_tile() */
1306 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1307 enum pipe_format readFormat
, drawFormat
;
1308 readFormat
= util_format_linear(rbRead
->texture
->format
);
1309 drawFormat
= util_format_linear(pt
->format
);
1310 pipe_get_tile_rgba_format(pipe
, ptRead
, readX
, readY
, readW
, readH
,
1312 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1313 readW
, readH
, drawFormat
, buf
);
1318 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1319 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1320 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1325 pipe
->transfer_destroy(pipe
, ptRead
);
1326 pipe
->transfer_destroy(pipe
, ptTex
);
1329 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1330 * textured quad with that texture.
1332 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1333 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1338 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1340 pipe_resource_reference(&pt
, NULL
);
1341 pipe_sampler_view_reference(&sv
[0], NULL
);
1346 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1348 functions
->DrawPixels
= st_DrawPixels
;
1349 functions
->CopyPixels
= st_CopyPixels
;
1354 st_destroy_drawpix(struct st_context
*st
)
1358 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1359 if (st
->drawpix
.shaders
[i
])
1360 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1363 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1364 if (st
->drawpix
.vert_shaders
[0])
1365 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1366 if (st
->drawpix
.vert_shaders
[1])
1367 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1370 #endif /* FEATURE_drawpix */