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/pack.h"
38 #include "main/texformat.h"
39 #include "main/texstore.h"
40 #include "program/program.h"
41 #include "program/prog_print.h"
42 #include "program/prog_instruction.h"
45 #include "st_atom_constbuf.h"
46 #include "st_cb_drawpixels.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_fbo.h"
49 #include "st_context.h"
51 #include "st_format.h"
52 #include "st_program.h"
53 #include "st_texture.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_defines.h"
57 #include "tgsi/tgsi_ureg.h"
58 #include "util/u_draw_quad.h"
59 #include "util/u_format.h"
60 #include "util/u_inlines.h"
61 #include "util/u_math.h"
62 #include "util/u_tile.h"
63 #include "cso_cache/cso_context.h"
69 * Check if the given program is:
70 * 0: MOVE result.color, fragment.color;
74 is_passthrough_program(const struct gl_fragment_program
*prog
)
76 if (prog
->Base
.NumInstructions
== 2) {
77 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
78 if (inst
[0].Opcode
== OPCODE_MOV
&&
79 inst
[1].Opcode
== OPCODE_END
&&
80 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
81 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
82 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
83 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
84 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
85 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
95 * Make fragment shader for glDraw/CopyPixels. This shader is made
96 * by combining the pixel transfer shader with the user-defined shader.
97 * \param fpIn the current/incoming fragment program
98 * \param fpOut returns the combined fragment program
101 st_make_drawpix_fragment_program(struct st_context
*st
,
102 struct gl_fragment_program
*fpIn
,
103 struct gl_fragment_program
**fpOut
)
105 struct gl_program
*newProg
;
107 if (is_passthrough_program(fpIn
)) {
108 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
109 &st
->pixel_xfer
.program
->Base
);
114 printf("Base program:\n");
115 _mesa_print_program(&fpIn
->Base
);
116 printf("DrawPix program:\n");
117 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
119 newProg
= _mesa_combine_programs(st
->ctx
,
120 &st
->pixel_xfer
.program
->Base
.Base
,
126 printf("Combined DrawPixels program:\n");
127 _mesa_print_program(newProg
);
128 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
129 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
130 _mesa_print_parameter_list(newProg
->Parameters
);
133 *fpOut
= (struct gl_fragment_program
*) newProg
;
138 * Create fragment program that does a TEX() instruction to get a Z and/or
139 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
140 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
141 * Pass fragment color through as-is.
142 * \return pointer to the gl_fragment program
144 struct gl_fragment_program
*
145 st_make_drawpix_z_stencil_program(struct st_context
*st
,
146 GLboolean write_depth
,
147 GLboolean write_stencil
)
149 struct gl_context
*ctx
= st
->ctx
;
150 struct gl_program
*p
;
151 struct gl_fragment_program
*fp
;
153 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
155 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
157 if (st
->drawpix
.shaders
[shaderIndex
]) {
158 /* already have the proper shader */
159 return st
->drawpix
.shaders
[shaderIndex
];
165 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
169 p
->NumInstructions
= write_depth
? 2 : 1;
170 p
->NumInstructions
+= write_stencil
? 1 : 0;
172 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
173 if (!p
->Instructions
) {
174 ctx
->Driver
.DeleteProgram(ctx
, p
);
177 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
180 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
181 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
182 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
183 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
184 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
185 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
186 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
187 p
->Instructions
[ic
].TexSrcUnit
= 0;
188 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
193 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
194 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
195 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
196 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
197 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
198 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
199 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
200 p
->Instructions
[ic
].TexSrcUnit
= 1;
201 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
206 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
208 assert(ic
== p
->NumInstructions
);
210 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
211 p
->OutputsWritten
= 0;
213 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
215 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
217 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
219 p
->SamplersUsed
|= 1 << 1;
221 fp
= (struct gl_fragment_program
*) p
;
223 /* save the new shader */
224 st
->drawpix
.shaders
[shaderIndex
] = fp
;
231 * Create a simple vertex shader that just passes through the
232 * vertex position and texcoord (and optionally, color).
235 make_passthrough_vertex_shader(struct st_context
*st
,
238 if (!st
->drawpix
.vert_shaders
[passColor
]) {
239 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
244 /* MOV result.pos, vertex.pos; */
246 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
247 ureg_DECL_vs_input( ureg
, 0 ));
249 /* MOV result.texcoord0, vertex.attr[1]; */
251 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
252 ureg_DECL_vs_input( ureg
, 1 ));
255 /* MOV result.color0, vertex.attr[2]; */
257 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
258 ureg_DECL_vs_input( ureg
, 2 ));
263 st
->drawpix
.vert_shaders
[passColor
] =
264 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
267 return st
->drawpix
.vert_shaders
[passColor
];
272 * Return a texture base format for drawing/copying an image
273 * of the given format.
276 base_format(GLenum format
)
279 case GL_DEPTH_COMPONENT
:
280 return GL_DEPTH_COMPONENT
;
281 case GL_DEPTH_STENCIL
:
282 return GL_DEPTH_STENCIL
;
283 case GL_STENCIL_INDEX
:
284 return GL_STENCIL_INDEX
;
292 * Return a texture internalFormat for drawing/copying an image
293 * of the given format and type.
296 internal_format(GLenum format
, GLenum type
)
299 case GL_DEPTH_COMPONENT
:
300 return GL_DEPTH_COMPONENT
;
301 case GL_DEPTH_STENCIL
:
302 return GL_DEPTH_STENCIL
;
303 case GL_STENCIL_INDEX
:
304 return GL_STENCIL_INDEX
;
306 if (_mesa_is_integer_format(format
)) {
310 case GL_UNSIGNED_BYTE
:
314 case GL_UNSIGNED_SHORT
:
318 case GL_UNSIGNED_INT
:
321 assert(0 && "Unexpected type in internal_format()");
322 return GL_RGBA_INTEGER
;
333 * Create a temporary texture to hold an image of the given size.
334 * If width, height are not POT and the driver only handles POT textures,
335 * allocate the next larger size of texture that is POT.
337 static struct pipe_resource
*
338 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
339 enum pipe_format texFormat
)
341 struct pipe_resource
*pt
;
343 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
344 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
351 * Make texture containing an image for glDrawPixels image.
352 * If 'pixels' is NULL, leave the texture image data undefined.
354 static struct pipe_resource
*
355 make_texture(struct st_context
*st
,
356 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
357 const struct gl_pixelstore_attrib
*unpack
,
358 const GLvoid
*pixels
)
360 struct gl_context
*ctx
= st
->ctx
;
361 struct pipe_context
*pipe
= st
->pipe
;
363 struct pipe_resource
*pt
;
364 enum pipe_format pipeFormat
;
366 GLenum baseFormat
, intFormat
;
368 baseFormat
= base_format(format
);
369 intFormat
= internal_format(format
, type
);
371 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
372 format
, type
, GL_FALSE
);
375 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
377 cpp
= util_format_get_blocksize(pipeFormat
);
379 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
383 /* alloc temporary texture */
384 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
386 _mesa_unmap_pbo_source(ctx
, unpack
);
391 struct pipe_transfer
*transfer
;
392 static const GLuint dstImageOffsets
= 0;
395 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
397 /* we'll do pixel transfer in a fragment shader */
398 ctx
->_ImageTransferState
= 0x0;
400 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
401 PIPE_TRANSFER_WRITE
, 0, 0,
404 /* map texture transfer */
405 dest
= pipe_transfer_map(pipe
, transfer
);
408 /* Put image into texture transfer.
409 * Note that the image is actually going to be upside down in
410 * the texture. We deal with that with texcoords.
412 success
= _mesa_texstore(ctx
, 2, /* dims */
413 baseFormat
, /* baseInternalFormat */
414 mformat
, /* gl_format */
416 0, 0, 0, /* dstX/Y/Zoffset */
417 transfer
->stride
, /* dstRowStride, bytes */
418 &dstImageOffsets
, /* dstImageOffsets */
419 width
, height
, 1, /* size */
420 format
, type
, /* src format/type */
421 pixels
, /* data source */
425 pipe_transfer_unmap(pipe
, transfer
);
426 pipe
->transfer_destroy(pipe
, transfer
);
431 ctx
->_ImageTransferState
= imageTransferStateSave
;
434 _mesa_unmap_pbo_source(ctx
, unpack
);
441 * Draw quad with texcoords and optional color.
442 * Coords are gallium window coords with y=0=top.
443 * \param color may be null
444 * \param invertTex if true, flip texcoords vertically
447 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
448 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
449 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
451 struct st_context
*st
= st_context(ctx
);
452 struct pipe_context
*pipe
= st
->pipe
;
453 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
455 /* setup vertex data */
457 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
458 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
459 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
460 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
461 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
462 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
463 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
464 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
465 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
466 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
470 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
471 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
474 verts
[1][0][0] = clip_x1
;
475 verts
[1][0][1] = clip_y0
;
478 verts
[2][0][0] = clip_x1
;
479 verts
[2][0][1] = clip_y1
;
482 verts
[3][0][0] = clip_x0
;
483 verts
[3][0][1] = clip_y1
;
485 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
486 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
487 verts
[1][1][0] = sRight
;
488 verts
[1][1][1] = tTop
;
489 verts
[2][1][0] = sRight
;
490 verts
[2][1][1] = tBot
;
491 verts
[3][1][0] = sLeft
;
492 verts
[3][1][1] = tBot
;
494 /* same for all verts: */
496 for (i
= 0; i
< 4; i
++) {
497 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
498 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
499 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
500 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
501 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
502 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
503 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
504 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
508 for (i
= 0; i
< 4; i
++) {
509 verts
[i
][0][2] = z
; /*Z*/
510 verts
[i
][0][3] = 1.0f
; /*W*/
511 verts
[i
][1][2] = 0.0f
; /*R*/
512 verts
[i
][1][3] = 1.0f
; /*Q*/
518 struct pipe_resource
*buf
;
520 /* allocate/load buffer object with vertex data */
521 buf
= pipe_buffer_create(pipe
->screen
,
522 PIPE_BIND_VERTEX_BUFFER
,
524 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
526 util_draw_vertex_buffer(pipe
, buf
, 0,
529 3); /* attribs/vert */
530 pipe_resource_reference(&buf
, NULL
);
537 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
538 GLsizei width
, GLsizei height
,
539 GLfloat zoomX
, GLfloat zoomY
,
540 struct pipe_sampler_view
**sv
,
541 int num_sampler_view
,
544 const GLfloat
*color
,
546 GLboolean write_depth
, GLboolean write_stencil
)
548 struct st_context
*st
= st_context(ctx
);
549 struct pipe_context
*pipe
= st
->pipe
;
550 struct cso_context
*cso
= st
->cso_context
;
551 GLfloat x0
, y0
, x1
, y1
;
553 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
556 /* XXX if DrawPixels image is larger than max texture size, break
559 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
560 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
561 assert(width
<= maxSize
);
562 assert(height
<= maxSize
);
564 cso_save_rasterizer(cso
);
565 cso_save_viewport(cso
);
566 cso_save_samplers(cso
);
567 cso_save_fragment_sampler_views(cso
);
568 cso_save_fragment_shader(cso
);
569 cso_save_vertex_shader(cso
);
570 cso_save_vertex_elements(cso
);
572 cso_save_depth_stencil_alpha(cso
);
576 /* rasterizer state: just scissor */
578 struct pipe_rasterizer_state rasterizer
;
579 memset(&rasterizer
, 0, sizeof(rasterizer
));
580 rasterizer
.gl_rasterization_rules
= 1;
581 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
582 cso_set_rasterizer(cso
, &rasterizer
);
586 /* Stencil writing bypasses the normal fragment pipeline to
587 * disable color writing and set stencil test to always pass.
589 struct pipe_depth_stencil_alpha_state dsa
;
590 struct pipe_blend_state blend
;
593 memset(&dsa
, 0, sizeof(dsa
));
594 dsa
.stencil
[0].enabled
= 1;
595 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
596 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
597 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
599 /* writing depth+stencil: depth test always passes */
600 dsa
.depth
.enabled
= 1;
601 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
602 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
604 cso_set_depth_stencil_alpha(cso
, &dsa
);
606 /* blend (colormask) */
607 memset(&blend
, 0, sizeof(blend
));
608 cso_set_blend(cso
, &blend
);
611 /* fragment shader state: TEX lookup program */
612 cso_set_fragment_shader_handle(cso
, driver_fp
);
614 /* vertex shader state: position + texcoord pass-through */
615 cso_set_vertex_shader_handle(cso
, driver_vp
);
618 /* texture sampling state: */
620 struct pipe_sampler_state sampler
;
621 memset(&sampler
, 0, sizeof(sampler
));
622 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
623 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
624 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
625 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
626 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
627 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
628 sampler
.normalized_coords
= normalized
;
630 cso_single_sampler(cso
, 0, &sampler
);
631 if (num_sampler_view
> 1) {
632 cso_single_sampler(cso
, 1, &sampler
);
634 cso_single_sampler_done(cso
);
637 /* viewport state: viewport matching window dims */
639 const float w
= (float) ctx
->DrawBuffer
->Width
;
640 const float h
= (float) ctx
->DrawBuffer
->Height
;
641 struct pipe_viewport_state vp
;
642 vp
.scale
[0] = 0.5f
* w
;
643 vp
.scale
[1] = -0.5f
* h
;
646 vp
.translate
[0] = 0.5f
* w
;
647 vp
.translate
[1] = 0.5f
* h
;
648 vp
.translate
[2] = 0.5f
;
649 vp
.translate
[3] = 0.0f
;
650 cso_set_viewport(cso
, &vp
);
653 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
656 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
658 /* Compute Gallium window coords (y=0=top) with pixel zoom.
659 * Recall that these coords are transformed by the current
660 * vertex shader and viewport transformation.
662 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
663 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
664 invertTex
= !invertTex
;
668 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
670 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
672 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
675 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
676 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
677 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
680 cso_restore_rasterizer(cso
);
681 cso_restore_viewport(cso
);
682 cso_restore_samplers(cso
);
683 cso_restore_fragment_sampler_views(cso
);
684 cso_restore_fragment_shader(cso
);
685 cso_restore_vertex_shader(cso
);
686 cso_restore_vertex_elements(cso
);
688 cso_restore_depth_stencil_alpha(cso
);
689 cso_restore_blend(cso
);
695 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
696 * can't use a fragment shader to write stencil values.
699 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
700 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
701 const struct gl_pixelstore_attrib
*unpack
,
702 const GLvoid
*pixels
)
704 struct st_context
*st
= st_context(ctx
);
705 struct pipe_context
*pipe
= st
->pipe
;
706 struct st_renderbuffer
*strb
;
707 enum pipe_transfer_usage usage
;
708 struct pipe_transfer
*pt
;
709 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
712 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
715 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
717 /* totally clipped */
722 strb
= st_renderbuffer(ctx
->DrawBuffer
->
723 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
725 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
726 y
= ctx
->DrawBuffer
->Height
- y
- height
;
729 if(format
!= GL_DEPTH_STENCIL
&&
730 util_format_get_component_bits(strb
->format
,
731 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
732 usage
= PIPE_TRANSFER_READ_WRITE
;
734 usage
= PIPE_TRANSFER_WRITE
;
736 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0,
740 stmap
= pipe_transfer_map(pipe
, pt
);
742 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
745 /* if width > MAX_WIDTH, have to process image in chunks */
747 while (skipPixels
< width
) {
748 const GLint spanX
= skipPixels
;
749 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
751 for (row
= 0; row
< height
; row
++) {
752 GLubyte sValues
[MAX_WIDTH
];
753 GLuint zValues
[MAX_WIDTH
];
754 GLenum destType
= GL_UNSIGNED_BYTE
;
755 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
759 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
760 type
, source
, &clippedUnpack
,
761 ctx
->_ImageTransferState
);
763 if (format
== GL_DEPTH_STENCIL
) {
764 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
765 (1 << 24) - 1, type
, source
,
770 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
771 "zoom not complete");
777 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
778 spanY
= height
- row
- 1;
784 /* now pack the stencil (and Z) values in the dest format */
785 switch (pt
->resource
->format
) {
786 case PIPE_FORMAT_S8_USCALED
:
788 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
789 assert(usage
== PIPE_TRANSFER_WRITE
);
790 memcpy(dest
, sValues
, spanWidth
);
793 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
794 if (format
== GL_DEPTH_STENCIL
) {
795 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
797 assert(usage
== PIPE_TRANSFER_WRITE
);
798 for (k
= 0; k
< spanWidth
; k
++) {
799 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
803 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
805 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
806 for (k
= 0; k
< spanWidth
; k
++) {
807 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
811 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
812 if (format
== GL_DEPTH_STENCIL
) {
813 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
815 assert(usage
== PIPE_TRANSFER_WRITE
);
816 for (k
= 0; k
< spanWidth
; k
++) {
817 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
821 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
823 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
824 for (k
= 0; k
< spanWidth
; k
++) {
825 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
834 skipPixels
+= spanWidth
;
837 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
839 /* unmap the stencil buffer */
840 pipe_transfer_unmap(pipe
, pt
);
841 pipe
->transfer_destroy(pipe
, pt
);
846 * Get fragment program variant for a glDrawPixels or glCopyPixels
847 * command for RGBA data.
849 static struct st_fp_variant
*
850 get_color_fp_variant(struct st_context
*st
)
852 struct gl_context
*ctx
= st
->ctx
;
853 struct st_fp_variant_key key
;
854 struct st_fp_variant
*fpv
;
856 memset(&key
, 0, sizeof(key
));
860 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
861 ctx
->Pixel
.RedScale
!= 1.0 ||
862 ctx
->Pixel
.GreenBias
!= 0.0 ||
863 ctx
->Pixel
.GreenScale
!= 1.0 ||
864 ctx
->Pixel
.BlueBias
!= 0.0 ||
865 ctx
->Pixel
.BlueScale
!= 1.0 ||
866 ctx
->Pixel
.AlphaBias
!= 0.0 ||
867 ctx
->Pixel
.AlphaScale
!= 1.0);
868 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
870 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
877 * Get fragment program variant for a glDrawPixels or glCopyPixels
878 * command for depth/stencil data.
880 static struct st_fp_variant
*
881 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
882 GLboolean write_stencil
)
884 struct st_fp_variant_key key
;
885 struct st_fp_variant
*fpv
;
887 memset(&key
, 0, sizeof(key
));
891 key
.drawpixels_z
= write_depth
;
892 key
.drawpixels_stencil
= write_stencil
;
894 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
901 * Called via ctx->Driver.DrawPixels()
904 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
905 GLsizei width
, GLsizei height
,
906 GLenum format
, GLenum type
,
907 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
909 void *driver_vp
, *driver_fp
;
910 struct st_context
*st
= st_context(ctx
);
911 const GLfloat
*color
;
912 struct pipe_context
*pipe
= st
->pipe
;
913 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
914 struct pipe_sampler_view
*sv
[2];
915 int num_sampler_view
= 1;
916 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
917 struct st_fp_variant
*fpv
;
919 if (format
== GL_DEPTH_STENCIL
)
920 write_stencil
= write_depth
= GL_TRUE
;
921 else if (format
== GL_STENCIL_INDEX
)
922 write_stencil
= GL_TRUE
;
923 else if (format
== GL_DEPTH_COMPONENT
)
924 write_depth
= GL_TRUE
;
927 enum pipe_format tex_format
;
928 /* can we write to stencil if not fallback */
929 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
930 goto stencil_fallback
;
932 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
934 0, PIPE_BIND_SAMPLER_VIEW
);
935 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
936 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
937 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
938 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
940 stencil_format
= PIPE_FORMAT_S8_USCALED
;
941 if (stencil_format
== PIPE_FORMAT_NONE
)
942 goto stencil_fallback
;
945 /* Mesa state should be up to date by now */
946 assert(ctx
->NewState
== 0x0);
948 st_validate_state(st
);
951 * Get vertex/fragment shaders
953 if (write_depth
|| write_stencil
) {
954 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
956 driver_fp
= fpv
->driver_shader
;
958 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
960 color
= ctx
->Current
.RasterColor
;
963 fpv
= get_color_fp_variant(st
);
965 driver_fp
= fpv
->driver_shader
;
967 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
970 if (st
->pixel_xfer
.pixelmap_enabled
) {
971 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
976 /* update fragment program constants */
977 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
979 /* draw with textured quad */
981 struct pipe_resource
*pt
982 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
984 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
988 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
993 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
995 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1000 color
, GL_FALSE
, write_depth
, write_stencil
);
1001 pipe_sampler_view_reference(&sv
[0], NULL
);
1002 if (num_sampler_view
> 1)
1003 pipe_sampler_view_reference(&sv
[1], NULL
);
1005 pipe_resource_reference(&pt
, NULL
);
1011 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1018 * Software fallback for glCopyPixels(GL_STENCIL).
1021 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1022 GLsizei width
, GLsizei height
,
1023 GLint dstx
, GLint dsty
)
1025 struct st_renderbuffer
*rbDraw
;
1026 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1027 enum pipe_transfer_usage usage
;
1028 struct pipe_transfer
*ptDraw
;
1033 buffer
= malloc(width
* height
* sizeof(ubyte
));
1035 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1039 /* Get the dest renderbuffer. If there's a wrapper, use the
1040 * underlying renderbuffer.
1042 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1043 if (rbDraw
->Base
.Wrapped
)
1044 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1046 /* this will do stencil pixel transfer ops */
1047 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1048 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1049 &ctx
->DefaultPacking
, buffer
);
1052 /* debug code: dump stencil values */
1054 for (row
= 0; row
< height
; row
++) {
1055 printf("%3d: ", row
);
1056 for (col
= 0; col
< width
; col
++) {
1057 printf("%02x ", buffer
[col
+ row
* width
]);
1063 if (util_format_get_component_bits(rbDraw
->format
,
1064 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1065 usage
= PIPE_TRANSFER_READ_WRITE
;
1067 usage
= PIPE_TRANSFER_WRITE
;
1069 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1070 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1073 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
1074 rbDraw
->texture
, 0, 0,
1078 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1079 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1081 /* map the stencil buffer */
1082 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1085 /* XXX PixelZoom not handled yet */
1086 for (i
= 0; i
< height
; i
++) {
1093 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1097 dst
= drawMap
+ y
* ptDraw
->stride
;
1098 src
= buffer
+ i
* width
;
1100 switch (ptDraw
->resource
->format
) {
1101 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1103 uint
*dst4
= (uint
*) dst
;
1105 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1106 for (j
= 0; j
< width
; j
++) {
1107 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1112 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1114 uint
*dst4
= (uint
*) dst
;
1116 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1117 for (j
= 0; j
< width
; j
++) {
1118 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1123 case PIPE_FORMAT_S8_USCALED
:
1124 assert(usage
== PIPE_TRANSFER_WRITE
);
1125 memcpy(dst
, src
, width
);
1134 /* unmap the stencil buffer */
1135 pipe_transfer_unmap(pipe
, ptDraw
);
1136 pipe
->transfer_destroy(pipe
, ptDraw
);
1141 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1142 GLsizei width
, GLsizei height
,
1143 GLint dstx
, GLint dsty
, GLenum type
)
1145 struct st_context
*st
= st_context(ctx
);
1146 struct pipe_context
*pipe
= st
->pipe
;
1147 struct pipe_screen
*screen
= pipe
->screen
;
1148 struct st_renderbuffer
*rbRead
;
1149 void *driver_vp
, *driver_fp
;
1150 struct pipe_resource
*pt
;
1151 struct pipe_sampler_view
*sv
[2];
1152 int num_sampler_view
= 1;
1154 enum pipe_format srcFormat
, texFormat
;
1155 GLboolean invertTex
= GL_FALSE
;
1156 GLint readX
, readY
, readW
, readH
;
1157 GLuint sample_count
;
1158 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1159 struct st_fp_variant
*fpv
;
1161 st_validate_state(st
);
1163 if (type
== GL_STENCIL
) {
1164 /* can't use texturing to do stencil */
1165 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1170 * Get vertex/fragment shaders
1172 if (type
== GL_COLOR
) {
1173 rbRead
= st_get_color_read_renderbuffer(ctx
);
1176 fpv
= get_color_fp_variant(st
);
1177 driver_fp
= fpv
->driver_shader
;
1179 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1181 if (st
->pixel_xfer
.pixelmap_enabled
) {
1182 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1187 assert(type
== GL_DEPTH
);
1188 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1189 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1191 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1192 driver_fp
= fpv
->driver_shader
;
1194 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1197 /* update fragment program constants */
1198 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1201 if (rbRead
->Base
.Wrapped
)
1202 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1204 sample_count
= rbRead
->texture
->nr_samples
;
1205 /* I believe this would be legal, presumably would need to do a resolve
1206 for color, and for depth/stencil spec says to just use one of the
1207 depth/stencil samples per pixel? Need some transfer clarifications. */
1208 assert(sample_count
< 2);
1210 srcFormat
= rbRead
->texture
->format
;
1212 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1214 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1215 texFormat
= srcFormat
;
1218 /* srcFormat can't be used as a texture format */
1219 if (type
== GL_DEPTH
) {
1220 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1221 st
->internal_target
, sample_count
,
1222 PIPE_BIND_DEPTH_STENCIL
);
1223 assert(texFormat
!= PIPE_FORMAT_NONE
);
1226 /* default color format */
1227 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1228 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1229 assert(texFormat
!= PIPE_FORMAT_NONE
);
1233 /* Invert src region if needed */
1234 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1235 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1236 invertTex
= !invertTex
;
1239 /* Clip the read region against the src buffer bounds.
1240 * We'll still allocate a temporary buffer/texture for the original
1241 * src region size but we'll only read the region which is on-screen.
1242 * This may mean that we draw garbage pixels into the dest region, but
1249 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1250 readW
= MAX2(0, readW
);
1251 readH
= MAX2(0, readH
);
1253 /* alloc temporary texture */
1254 pt
= alloc_texture(st
, width
, height
, texFormat
);
1258 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1260 pipe_resource_reference(&pt
, NULL
);
1264 /* Make temporary texture which is a copy of the src region.
1266 if (srcFormat
== texFormat
) {
1267 struct pipe_box src_box
;
1268 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1269 /* copy source framebuffer surface into mipmap/texture */
1270 pipe
->resource_copy_region(pipe
,
1273 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1274 rbRead
->texture
, /* src tex */
1280 /* CPU-based fallback/conversion */
1281 struct pipe_transfer
*ptRead
=
1282 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0,
1284 readX
, readY
, readW
, readH
);
1285 struct pipe_transfer
*ptTex
;
1286 enum pipe_transfer_usage transfer_usage
;
1288 if (ST_DEBUG
& DEBUG_FALLBACK
)
1289 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1291 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1292 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1294 transfer_usage
= PIPE_TRANSFER_WRITE
;
1296 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1297 0, 0, width
, height
);
1299 /* copy image from ptRead surface to ptTex surface */
1300 if (type
== GL_COLOR
) {
1301 /* alternate path using get/put_tile() */
1302 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1303 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1304 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1310 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1311 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1312 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1317 pipe
->transfer_destroy(pipe
, ptRead
);
1318 pipe
->transfer_destroy(pipe
, ptTex
);
1321 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1322 * textured quad with that texture.
1324 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1325 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1330 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1332 pipe_resource_reference(&pt
, NULL
);
1333 pipe_sampler_view_reference(&sv
[0], NULL
);
1338 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1340 functions
->DrawPixels
= st_DrawPixels
;
1341 functions
->CopyPixels
= st_CopyPixels
;
1346 st_destroy_drawpix(struct st_context
*st
)
1350 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1351 if (st
->drawpix
.shaders
[i
])
1352 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1355 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1356 if (st
->drawpix
.vert_shaders
[0])
1357 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1358 if (st
->drawpix
.vert_shaders
[1])
1359 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1362 #endif /* FEATURE_drawpix */