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
, 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
);
574 cso_save_depth_stencil_alpha(cso
);
578 /* rasterizer state: just scissor */
580 struct pipe_rasterizer_state rasterizer
;
581 memset(&rasterizer
, 0, sizeof(rasterizer
));
582 rasterizer
.gl_rasterization_rules
= 1;
583 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
584 cso_set_rasterizer(cso
, &rasterizer
);
588 /* Stencil writing bypasses the normal fragment pipeline to
589 * disable color writing and set stencil test to always pass.
591 struct pipe_depth_stencil_alpha_state dsa
;
592 struct pipe_blend_state blend
;
595 memset(&dsa
, 0, sizeof(dsa
));
596 dsa
.stencil
[0].enabled
= 1;
597 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
598 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
599 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
601 /* writing depth+stencil: depth test always passes */
602 dsa
.depth
.enabled
= 1;
603 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
604 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
606 cso_set_depth_stencil_alpha(cso
, &dsa
);
608 /* blend (colormask) */
609 memset(&blend
, 0, sizeof(blend
));
610 cso_set_blend(cso
, &blend
);
613 /* fragment shader state: TEX lookup program */
614 cso_set_fragment_shader_handle(cso
, driver_fp
);
616 /* vertex shader state: position + texcoord pass-through */
617 cso_set_vertex_shader_handle(cso
, driver_vp
);
620 /* texture sampling state: */
622 struct pipe_sampler_state sampler
;
623 memset(&sampler
, 0, sizeof(sampler
));
624 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
625 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
626 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
627 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
628 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
629 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
630 sampler
.normalized_coords
= normalized
;
632 cso_single_sampler(cso
, 0, &sampler
);
633 if (num_sampler_view
> 1) {
634 cso_single_sampler(cso
, 1, &sampler
);
636 cso_single_sampler_done(cso
);
639 /* viewport state: viewport matching window dims */
641 const float w
= (float) ctx
->DrawBuffer
->Width
;
642 const float h
= (float) ctx
->DrawBuffer
->Height
;
643 struct pipe_viewport_state vp
;
644 vp
.scale
[0] = 0.5f
* w
;
645 vp
.scale
[1] = -0.5f
* h
;
648 vp
.translate
[0] = 0.5f
* w
;
649 vp
.translate
[1] = 0.5f
* h
;
650 vp
.translate
[2] = 0.5f
;
651 vp
.translate
[3] = 0.0f
;
652 cso_set_viewport(cso
, &vp
);
655 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
658 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
660 /* Compute Gallium window coords (y=0=top) with pixel zoom.
661 * Recall that these coords are transformed by the current
662 * vertex shader and viewport transformation.
664 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
665 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
666 invertTex
= !invertTex
;
670 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
672 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
674 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
677 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
678 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
679 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
682 cso_restore_rasterizer(cso
);
683 cso_restore_viewport(cso
);
684 cso_restore_samplers(cso
);
685 cso_restore_fragment_sampler_views(cso
);
686 cso_restore_fragment_shader(cso
);
687 cso_restore_vertex_shader(cso
);
688 cso_restore_vertex_elements(cso
);
690 cso_restore_depth_stencil_alpha(cso
);
691 cso_restore_blend(cso
);
697 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
698 * can't use a fragment shader to write stencil values.
701 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
702 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
703 const struct gl_pixelstore_attrib
*unpack
,
704 const GLvoid
*pixels
)
706 struct st_context
*st
= st_context(ctx
);
707 struct pipe_context
*pipe
= st
->pipe
;
708 struct st_renderbuffer
*strb
;
709 enum pipe_transfer_usage usage
;
710 struct pipe_transfer
*pt
;
711 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
714 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
717 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
719 /* totally clipped */
724 strb
= st_renderbuffer(ctx
->DrawBuffer
->
725 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
727 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
728 y
= ctx
->DrawBuffer
->Height
- y
- height
;
731 if(format
!= GL_DEPTH_STENCIL
&&
732 util_format_get_component_bits(strb
->format
,
733 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
734 usage
= PIPE_TRANSFER_READ_WRITE
;
736 usage
= PIPE_TRANSFER_WRITE
;
738 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0,
742 stmap
= pipe_transfer_map(pipe
, pt
);
744 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
747 /* if width > MAX_WIDTH, have to process image in chunks */
749 while (skipPixels
< width
) {
750 const GLint spanX
= skipPixels
;
751 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
753 for (row
= 0; row
< height
; row
++) {
754 GLubyte sValues
[MAX_WIDTH
];
755 GLuint zValues
[MAX_WIDTH
];
756 GLenum destType
= GL_UNSIGNED_BYTE
;
757 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
761 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
762 type
, source
, &clippedUnpack
,
763 ctx
->_ImageTransferState
);
765 if (format
== GL_DEPTH_STENCIL
) {
766 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
767 (1 << 24) - 1, type
, source
,
772 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
773 "zoom not complete");
779 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
780 spanY
= height
- row
- 1;
786 /* now pack the stencil (and Z) values in the dest format */
787 switch (pt
->resource
->format
) {
788 case PIPE_FORMAT_S8_USCALED
:
790 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
791 assert(usage
== PIPE_TRANSFER_WRITE
);
792 memcpy(dest
, sValues
, spanWidth
);
795 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
796 if (format
== GL_DEPTH_STENCIL
) {
797 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
799 assert(usage
== PIPE_TRANSFER_WRITE
);
800 for (k
= 0; k
< spanWidth
; k
++) {
801 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
805 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
807 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
808 for (k
= 0; k
< spanWidth
; k
++) {
809 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
813 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
814 if (format
== GL_DEPTH_STENCIL
) {
815 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
817 assert(usage
== PIPE_TRANSFER_WRITE
);
818 for (k
= 0; k
< spanWidth
; k
++) {
819 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
823 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
825 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
826 for (k
= 0; k
< spanWidth
; k
++) {
827 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
836 skipPixels
+= spanWidth
;
839 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
841 /* unmap the stencil buffer */
842 pipe_transfer_unmap(pipe
, pt
);
843 pipe
->transfer_destroy(pipe
, pt
);
848 * Get fragment program variant for a glDrawPixels or glCopyPixels
849 * command for RGBA data.
851 static struct st_fp_variant
*
852 get_color_fp_variant(struct st_context
*st
)
854 struct gl_context
*ctx
= st
->ctx
;
855 struct st_fp_variant_key key
;
856 struct st_fp_variant
*fpv
;
858 memset(&key
, 0, sizeof(key
));
862 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
863 ctx
->Pixel
.RedScale
!= 1.0 ||
864 ctx
->Pixel
.GreenBias
!= 0.0 ||
865 ctx
->Pixel
.GreenScale
!= 1.0 ||
866 ctx
->Pixel
.BlueBias
!= 0.0 ||
867 ctx
->Pixel
.BlueScale
!= 1.0 ||
868 ctx
->Pixel
.AlphaBias
!= 0.0 ||
869 ctx
->Pixel
.AlphaScale
!= 1.0);
870 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
872 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
879 * Get fragment program variant for a glDrawPixels or glCopyPixels
880 * command for depth/stencil data.
882 static struct st_fp_variant
*
883 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
884 GLboolean write_stencil
)
886 struct st_fp_variant_key key
;
887 struct st_fp_variant
*fpv
;
889 memset(&key
, 0, sizeof(key
));
893 key
.drawpixels_z
= write_depth
;
894 key
.drawpixels_stencil
= write_stencil
;
896 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
903 * Called via ctx->Driver.DrawPixels()
906 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
907 GLsizei width
, GLsizei height
,
908 GLenum format
, GLenum type
,
909 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
911 void *driver_vp
, *driver_fp
;
912 struct st_context
*st
= st_context(ctx
);
913 const GLfloat
*color
;
914 struct pipe_context
*pipe
= st
->pipe
;
915 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
916 struct pipe_sampler_view
*sv
[2];
917 int num_sampler_view
= 1;
918 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
919 struct st_fp_variant
*fpv
;
921 if (format
== GL_DEPTH_STENCIL
)
922 write_stencil
= write_depth
= GL_TRUE
;
923 else if (format
== GL_STENCIL_INDEX
)
924 write_stencil
= GL_TRUE
;
925 else if (format
== GL_DEPTH_COMPONENT
)
926 write_depth
= GL_TRUE
;
929 enum pipe_format tex_format
;
930 /* can we write to stencil if not fallback */
931 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
932 goto stencil_fallback
;
934 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
936 0, PIPE_BIND_SAMPLER_VIEW
);
937 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
938 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
939 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
940 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
942 stencil_format
= PIPE_FORMAT_S8_USCALED
;
943 if (stencil_format
== PIPE_FORMAT_NONE
)
944 goto stencil_fallback
;
947 /* Mesa state should be up to date by now */
948 assert(ctx
->NewState
== 0x0);
950 st_validate_state(st
);
953 * Get vertex/fragment shaders
955 if (write_depth
|| write_stencil
) {
956 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
958 driver_fp
= fpv
->driver_shader
;
960 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
962 color
= ctx
->Current
.RasterColor
;
965 fpv
= get_color_fp_variant(st
);
967 driver_fp
= fpv
->driver_shader
;
969 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
972 if (st
->pixel_xfer
.pixelmap_enabled
) {
973 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
978 /* update fragment program constants */
979 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
981 /* draw with textured quad */
983 struct pipe_resource
*pt
984 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
986 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
990 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
995 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
997 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1002 color
, GL_FALSE
, write_depth
, write_stencil
);
1003 pipe_sampler_view_reference(&sv
[0], NULL
);
1004 if (num_sampler_view
> 1)
1005 pipe_sampler_view_reference(&sv
[1], NULL
);
1007 pipe_resource_reference(&pt
, NULL
);
1013 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1020 * Software fallback for glCopyPixels(GL_STENCIL).
1023 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1024 GLsizei width
, GLsizei height
,
1025 GLint dstx
, GLint dsty
)
1027 struct st_renderbuffer
*rbDraw
;
1028 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1029 enum pipe_transfer_usage usage
;
1030 struct pipe_transfer
*ptDraw
;
1035 buffer
= malloc(width
* height
* sizeof(ubyte
));
1037 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1041 /* Get the dest renderbuffer. If there's a wrapper, use the
1042 * underlying renderbuffer.
1044 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1045 if (rbDraw
->Base
.Wrapped
)
1046 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1048 /* this will do stencil pixel transfer ops */
1049 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1050 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1051 &ctx
->DefaultPacking
, buffer
);
1054 /* debug code: dump stencil values */
1056 for (row
= 0; row
< height
; row
++) {
1057 printf("%3d: ", row
);
1058 for (col
= 0; col
< width
; col
++) {
1059 printf("%02x ", buffer
[col
+ row
* width
]);
1065 if (util_format_get_component_bits(rbDraw
->format
,
1066 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1067 usage
= PIPE_TRANSFER_READ_WRITE
;
1069 usage
= PIPE_TRANSFER_WRITE
;
1071 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1072 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1075 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
1076 rbDraw
->texture
, 0, 0,
1080 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1081 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1083 /* map the stencil buffer */
1084 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1087 /* XXX PixelZoom not handled yet */
1088 for (i
= 0; i
< height
; i
++) {
1095 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1099 dst
= drawMap
+ y
* ptDraw
->stride
;
1100 src
= buffer
+ i
* width
;
1102 switch (ptDraw
->resource
->format
) {
1103 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1105 uint
*dst4
= (uint
*) dst
;
1107 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1108 for (j
= 0; j
< width
; j
++) {
1109 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1114 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1116 uint
*dst4
= (uint
*) dst
;
1118 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1119 for (j
= 0; j
< width
; j
++) {
1120 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1125 case PIPE_FORMAT_S8_USCALED
:
1126 assert(usage
== PIPE_TRANSFER_WRITE
);
1127 memcpy(dst
, src
, width
);
1136 /* unmap the stencil buffer */
1137 pipe_transfer_unmap(pipe
, ptDraw
);
1138 pipe
->transfer_destroy(pipe
, ptDraw
);
1143 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1144 GLsizei width
, GLsizei height
,
1145 GLint dstx
, GLint dsty
, GLenum type
)
1147 struct st_context
*st
= st_context(ctx
);
1148 struct pipe_context
*pipe
= st
->pipe
;
1149 struct pipe_screen
*screen
= pipe
->screen
;
1150 struct st_renderbuffer
*rbRead
;
1151 void *driver_vp
, *driver_fp
;
1152 struct pipe_resource
*pt
;
1153 struct pipe_sampler_view
*sv
[2];
1154 int num_sampler_view
= 1;
1156 enum pipe_format srcFormat
, texFormat
;
1157 GLboolean invertTex
= GL_FALSE
;
1158 GLint readX
, readY
, readW
, readH
;
1159 GLuint sample_count
;
1160 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1161 struct st_fp_variant
*fpv
;
1163 st_validate_state(st
);
1165 if (type
== GL_STENCIL
) {
1166 /* can't use texturing to do stencil */
1167 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1172 * Get vertex/fragment shaders
1174 if (type
== GL_COLOR
) {
1175 rbRead
= st_get_color_read_renderbuffer(ctx
);
1178 fpv
= get_color_fp_variant(st
);
1179 driver_fp
= fpv
->driver_shader
;
1181 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1183 if (st
->pixel_xfer
.pixelmap_enabled
) {
1184 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1189 assert(type
== GL_DEPTH
);
1190 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1191 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1193 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1194 driver_fp
= fpv
->driver_shader
;
1196 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1199 /* update fragment program constants */
1200 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1203 if (rbRead
->Base
.Wrapped
)
1204 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1206 sample_count
= rbRead
->texture
->nr_samples
;
1207 /* I believe this would be legal, presumably would need to do a resolve
1208 for color, and for depth/stencil spec says to just use one of the
1209 depth/stencil samples per pixel? Need some transfer clarifications. */
1210 assert(sample_count
< 2);
1212 srcFormat
= rbRead
->texture
->format
;
1214 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1216 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1217 texFormat
= srcFormat
;
1220 /* srcFormat can't be used as a texture format */
1221 if (type
== GL_DEPTH
) {
1222 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1223 st
->internal_target
, sample_count
,
1224 PIPE_BIND_DEPTH_STENCIL
);
1225 assert(texFormat
!= PIPE_FORMAT_NONE
);
1228 /* default color format */
1229 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1230 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1231 assert(texFormat
!= PIPE_FORMAT_NONE
);
1235 /* Invert src region if needed */
1236 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1237 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1238 invertTex
= !invertTex
;
1241 /* Clip the read region against the src buffer bounds.
1242 * We'll still allocate a temporary buffer/texture for the original
1243 * src region size but we'll only read the region which is on-screen.
1244 * This may mean that we draw garbage pixels into the dest region, but
1251 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1252 readW
= MAX2(0, readW
);
1253 readH
= MAX2(0, readH
);
1255 /* alloc temporary texture */
1256 pt
= alloc_texture(st
, width
, height
, texFormat
);
1260 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1262 pipe_resource_reference(&pt
, NULL
);
1266 /* Make temporary texture which is a copy of the src region.
1268 if (srcFormat
== texFormat
) {
1269 struct pipe_box src_box
;
1270 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1271 /* copy source framebuffer surface into mipmap/texture */
1272 pipe
->resource_copy_region(pipe
,
1275 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1276 rbRead
->texture
, /* src tex */
1282 /* CPU-based fallback/conversion */
1283 struct pipe_transfer
*ptRead
=
1284 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0,
1286 readX
, readY
, readW
, readH
);
1287 struct pipe_transfer
*ptTex
;
1288 enum pipe_transfer_usage transfer_usage
;
1290 if (ST_DEBUG
& DEBUG_FALLBACK
)
1291 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1293 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1294 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1296 transfer_usage
= PIPE_TRANSFER_WRITE
;
1298 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1299 0, 0, width
, height
);
1301 /* copy image from ptRead surface to ptTex surface */
1302 if (type
== GL_COLOR
) {
1303 /* alternate path using get/put_tile() */
1304 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1305 enum pipe_format readFormat
, drawFormat
;
1306 readFormat
= util_format_linear(rbRead
->texture
->format
);
1307 drawFormat
= util_format_linear(pt
->format
);
1308 pipe_get_tile_rgba_format(pipe
, ptRead
, readX
, readY
, readW
, readH
,
1310 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1311 readW
, readH
, drawFormat
, buf
);
1316 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1317 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1318 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1323 pipe
->transfer_destroy(pipe
, ptRead
);
1324 pipe
->transfer_destroy(pipe
, ptTex
);
1327 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1328 * textured quad with that texture.
1330 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1331 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1336 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1338 pipe_resource_reference(&pt
, NULL
);
1339 pipe_sampler_view_reference(&sv
[0], NULL
);
1344 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1346 functions
->DrawPixels
= st_DrawPixels
;
1347 functions
->CopyPixels
= st_CopyPixels
;
1352 st_destroy_drawpix(struct st_context
*st
)
1356 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1357 if (st
->drawpix
.shaders
[i
])
1358 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1361 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1362 if (st
->drawpix
.vert_shaders
[0])
1363 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1364 if (st
->drawpix
.vert_shaders
[1])
1365 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1368 #endif /* FEATURE_drawpix */