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 * \return pointer to Gallium driver fragment shader
100 combined_drawpix_fragment_program(struct gl_context
*ctx
)
102 struct st_context
*st
= st_context(ctx
);
103 struct st_fragment_program
*stfp
;
105 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
106 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
107 /* the pixel tranfer program has not changed and the user-defined
108 * program has not changed, so re-use the combined program.
110 stfp
= st
->pixel_xfer
.combined_prog
;
113 /* Concatenate the pixel transfer program with the current user-
116 if (is_passthrough_program(&st
->fp
->Base
)) {
117 stfp
= (struct st_fragment_program
*)
118 _mesa_clone_fragment_program(ctx
, &st
->pixel_xfer
.program
->Base
);
122 printf("Base program:\n");
123 _mesa_print_program(&st
->fp
->Base
.Base
);
124 printf("DrawPix program:\n");
125 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
127 stfp
= (struct st_fragment_program
*)
128 _mesa_combine_programs(ctx
,
129 &st
->pixel_xfer
.program
->Base
.Base
,
135 struct gl_program
*p
= &stfp
->Base
.Base
;
136 printf("Combined DrawPixels program:\n");
137 _mesa_print_program(p
);
138 printf("InputsRead: 0x%x\n", p
->InputsRead
);
139 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
140 _mesa_print_parameter_list(p
->Parameters
);
144 /* translate to TGSI tokens */
145 st_translate_fragment_program(st
, stfp
);
147 /* save new program, update serial numbers */
148 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
149 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
150 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
151 /* can't reference new program directly, already have a reference on it */
152 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
153 st
->pixel_xfer
.combined_prog
= stfp
;
156 /* Ideally we'd have updated the pipe constants during the normal
157 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
159 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
161 return stfp
->driver_shader
;
166 * Create fragment shader that does a TEX() instruction to get a Z and/or
167 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
168 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
169 * Pass fragment color through as-is.
170 * \return pointer to the Gallium driver fragment shader
173 make_fragment_shader_z_stencil(struct st_context
*st
, GLboolean write_depth
,
174 GLboolean write_stencil
)
176 struct gl_context
*ctx
= st
->ctx
;
177 struct gl_program
*p
;
178 struct st_fragment_program
*stp
;
180 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
182 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
184 if (st
->drawpix
.shaders
[shaderIndex
]) {
185 /* already have the proper shader */
186 return st
->drawpix
.shaders
[shaderIndex
]->driver_shader
;
192 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
196 p
->NumInstructions
= write_depth
? 2 : 1;
197 p
->NumInstructions
+= write_stencil
? 1 : 0;
199 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
200 if (!p
->Instructions
) {
201 ctx
->Driver
.DeleteProgram(ctx
, p
);
204 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
207 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
208 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
209 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
210 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
211 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
212 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
213 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
214 p
->Instructions
[ic
].TexSrcUnit
= 0;
215 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
220 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
221 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
222 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
223 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
224 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
225 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
226 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
227 p
->Instructions
[ic
].TexSrcUnit
= 1;
228 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
233 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
235 assert(ic
== p
->NumInstructions
);
237 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
238 p
->OutputsWritten
= 0;
240 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
242 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
244 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
246 p
->SamplersUsed
|= 1 << 1;
248 stp
= st_fragment_program((struct gl_fragment_program
*) p
);
250 /* save the new shader */
251 st
->drawpix
.shaders
[shaderIndex
] = stp
;
253 st_translate_fragment_program(st
, stp
);
255 return stp
->driver_shader
;
261 * Create a simple vertex shader that just passes through the
262 * vertex position and texcoord (and optionally, color).
265 make_passthrough_vertex_shader(struct st_context
*st
,
268 if (!st
->drawpix
.vert_shaders
[passColor
]) {
269 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
274 /* MOV result.pos, vertex.pos; */
276 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
277 ureg_DECL_vs_input( ureg
, 0 ));
279 /* MOV result.texcoord0, vertex.attr[1]; */
281 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
282 ureg_DECL_vs_input( ureg
, 1 ));
285 /* MOV result.color0, vertex.attr[2]; */
287 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
288 ureg_DECL_vs_input( ureg
, 2 ));
293 st
->drawpix
.vert_shaders
[passColor
] =
294 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
297 return st
->drawpix
.vert_shaders
[passColor
];
302 * Return a texture base format for drawing/copying an image
303 * of the given format.
306 base_format(GLenum format
)
309 case GL_DEPTH_COMPONENT
:
310 return GL_DEPTH_COMPONENT
;
311 case GL_DEPTH_STENCIL
:
312 return GL_DEPTH_STENCIL
;
313 case GL_STENCIL_INDEX
:
314 return GL_STENCIL_INDEX
;
322 * Return a texture internalFormat for drawing/copying an image
323 * of the given format and type.
326 internal_format(GLenum format
, GLenum type
)
329 case GL_DEPTH_COMPONENT
:
330 return GL_DEPTH_COMPONENT
;
331 case GL_DEPTH_STENCIL
:
332 return GL_DEPTH_STENCIL
;
333 case GL_STENCIL_INDEX
:
334 return GL_STENCIL_INDEX
;
336 if (_mesa_is_integer_format(format
)) {
340 case GL_UNSIGNED_BYTE
:
344 case GL_UNSIGNED_SHORT
:
348 case GL_UNSIGNED_INT
:
351 assert(0 && "Unexpected type in internal_format()");
352 return GL_RGBA_INTEGER
;
363 * Create a temporary texture to hold an image of the given size.
364 * If width, height are not POT and the driver only handles POT textures,
365 * allocate the next larger size of texture that is POT.
367 static struct pipe_resource
*
368 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
369 enum pipe_format texFormat
)
371 struct pipe_resource
*pt
;
373 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
374 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
381 * Make texture containing an image for glDrawPixels image.
382 * If 'pixels' is NULL, leave the texture image data undefined.
384 static struct pipe_resource
*
385 make_texture(struct st_context
*st
,
386 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
387 const struct gl_pixelstore_attrib
*unpack
,
388 const GLvoid
*pixels
)
390 struct gl_context
*ctx
= st
->ctx
;
391 struct pipe_context
*pipe
= st
->pipe
;
393 struct pipe_resource
*pt
;
394 enum pipe_format pipeFormat
;
396 GLenum baseFormat
, intFormat
;
398 baseFormat
= base_format(format
);
399 intFormat
= internal_format(format
, type
);
401 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
402 format
, type
, GL_FALSE
);
405 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
407 cpp
= util_format_get_blocksize(pipeFormat
);
409 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
413 /* alloc temporary texture */
414 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
416 _mesa_unmap_pbo_source(ctx
, unpack
);
421 struct pipe_transfer
*transfer
;
422 static const GLuint dstImageOffsets
= 0;
425 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
427 /* we'll do pixel transfer in a fragment shader */
428 ctx
->_ImageTransferState
= 0x0;
430 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0,
431 PIPE_TRANSFER_WRITE
, 0, 0,
434 /* map texture transfer */
435 dest
= pipe_transfer_map(pipe
, transfer
);
438 /* Put image into texture transfer.
439 * Note that the image is actually going to be upside down in
440 * the texture. We deal with that with texcoords.
442 success
= _mesa_texstore(ctx
, 2, /* dims */
443 baseFormat
, /* baseInternalFormat */
444 mformat
, /* gl_format */
446 0, 0, 0, /* dstX/Y/Zoffset */
447 transfer
->stride
, /* dstRowStride, bytes */
448 &dstImageOffsets
, /* dstImageOffsets */
449 width
, height
, 1, /* size */
450 format
, type
, /* src format/type */
451 pixels
, /* data source */
455 pipe_transfer_unmap(pipe
, transfer
);
456 pipe
->transfer_destroy(pipe
, transfer
);
461 ctx
->_ImageTransferState
= imageTransferStateSave
;
464 _mesa_unmap_pbo_source(ctx
, unpack
);
471 * Draw quad with texcoords and optional color.
472 * Coords are gallium window coords with y=0=top.
473 * \param color may be null
474 * \param invertTex if true, flip texcoords vertically
477 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
478 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
479 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
481 struct st_context
*st
= st_context(ctx
);
482 struct pipe_context
*pipe
= st
->pipe
;
483 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
485 /* setup vertex data */
487 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
488 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
489 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
490 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
491 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
492 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
493 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
494 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
495 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
496 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
500 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
501 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
504 verts
[1][0][0] = clip_x1
;
505 verts
[1][0][1] = clip_y0
;
508 verts
[2][0][0] = clip_x1
;
509 verts
[2][0][1] = clip_y1
;
512 verts
[3][0][0] = clip_x0
;
513 verts
[3][0][1] = clip_y1
;
515 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
516 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
517 verts
[1][1][0] = sRight
;
518 verts
[1][1][1] = tTop
;
519 verts
[2][1][0] = sRight
;
520 verts
[2][1][1] = tBot
;
521 verts
[3][1][0] = sLeft
;
522 verts
[3][1][1] = tBot
;
524 /* same for all verts: */
526 for (i
= 0; i
< 4; i
++) {
527 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
528 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
529 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
530 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
531 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
532 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
533 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
534 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
538 for (i
= 0; i
< 4; i
++) {
539 verts
[i
][0][2] = z
; /*Z*/
540 verts
[i
][0][3] = 1.0f
; /*W*/
541 verts
[i
][1][2] = 0.0f
; /*R*/
542 verts
[i
][1][3] = 1.0f
; /*Q*/
548 struct pipe_resource
*buf
;
550 /* allocate/load buffer object with vertex data */
551 buf
= pipe_buffer_create(pipe
->screen
,
552 PIPE_BIND_VERTEX_BUFFER
,
554 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
556 util_draw_vertex_buffer(pipe
, buf
, 0,
559 3); /* attribs/vert */
560 pipe_resource_reference(&buf
, NULL
);
567 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
568 GLsizei width
, GLsizei height
,
569 GLfloat zoomX
, GLfloat zoomY
,
570 struct pipe_sampler_view
**sv
,
571 int num_sampler_view
,
574 const GLfloat
*color
,
576 GLboolean write_depth
, GLboolean write_stencil
)
578 struct st_context
*st
= st_context(ctx
);
579 struct pipe_context
*pipe
= st
->pipe
;
580 struct cso_context
*cso
= st
->cso_context
;
581 GLfloat x0
, y0
, x1
, y1
;
583 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
586 /* XXX if DrawPixels image is larger than max texture size, break
589 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
590 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
591 assert(width
<= maxSize
);
592 assert(height
<= maxSize
);
594 cso_save_depth_stencil_alpha(cso
);
595 cso_save_rasterizer(cso
);
596 cso_save_viewport(cso
);
597 cso_save_samplers(cso
);
598 cso_save_fragment_sampler_views(cso
);
599 cso_save_fragment_shader(cso
);
600 cso_save_vertex_shader(cso
);
601 cso_save_vertex_elements(cso
);
603 /* rasterizer state: just scissor */
605 struct pipe_rasterizer_state rasterizer
;
606 memset(&rasterizer
, 0, sizeof(rasterizer
));
607 rasterizer
.gl_rasterization_rules
= 1;
608 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
609 cso_set_rasterizer(cso
, &rasterizer
);
612 if (write_depth
|| write_stencil
)
614 struct pipe_depth_stencil_alpha_state dsa
;
615 memset(&dsa
, 0, sizeof(dsa
));
617 dsa
.depth
.enabled
= 1;
618 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
619 dsa
.depth
.writemask
= 1;
622 dsa
.stencil
[0].enabled
= 1;
623 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
624 dsa
.stencil
[0].writemask
= 0xff;
625 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
627 cso_set_depth_stencil_alpha(cso
, &dsa
);
630 /* fragment shader state: TEX lookup program */
631 cso_set_fragment_shader_handle(cso
, driver_fp
);
633 /* vertex shader state: position + texcoord pass-through */
634 cso_set_vertex_shader_handle(cso
, driver_vp
);
637 /* texture sampling state: */
639 struct pipe_sampler_state sampler
;
640 memset(&sampler
, 0, sizeof(sampler
));
641 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
642 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
643 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
644 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
645 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
646 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
647 sampler
.normalized_coords
= normalized
;
649 cso_single_sampler(cso
, 0, &sampler
);
650 if (num_sampler_view
> 1) {
651 cso_single_sampler(cso
, 1, &sampler
);
653 cso_single_sampler_done(cso
);
656 /* viewport state: viewport matching window dims */
658 const float w
= (float) ctx
->DrawBuffer
->Width
;
659 const float h
= (float) ctx
->DrawBuffer
->Height
;
660 struct pipe_viewport_state vp
;
661 vp
.scale
[0] = 0.5f
* w
;
662 vp
.scale
[1] = -0.5f
* h
;
665 vp
.translate
[0] = 0.5f
* w
;
666 vp
.translate
[1] = 0.5f
* h
;
667 vp
.translate
[2] = 0.5f
;
668 vp
.translate
[3] = 0.0f
;
669 cso_set_viewport(cso
, &vp
);
672 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
675 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
677 /* Compute Gallium window coords (y=0=top) with pixel zoom.
678 * Recall that these coords are transformed by the current
679 * vertex shader and viewport transformation.
681 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
682 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
683 invertTex
= !invertTex
;
687 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
689 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
691 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
694 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
695 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
696 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
699 cso_restore_depth_stencil_alpha(cso
);
700 cso_restore_rasterizer(cso
);
701 cso_restore_viewport(cso
);
702 cso_restore_samplers(cso
);
703 cso_restore_fragment_sampler_views(cso
);
704 cso_restore_fragment_shader(cso
);
705 cso_restore_vertex_shader(cso
);
706 cso_restore_vertex_elements(cso
);
711 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
712 * can't use a fragment shader to write stencil values.
715 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
716 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
717 const struct gl_pixelstore_attrib
*unpack
,
718 const GLvoid
*pixels
)
720 struct st_context
*st
= st_context(ctx
);
721 struct pipe_context
*pipe
= st
->pipe
;
722 struct st_renderbuffer
*strb
;
723 enum pipe_transfer_usage usage
;
724 struct pipe_transfer
*pt
;
725 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
728 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
731 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
733 /* totally clipped */
738 strb
= st_renderbuffer(ctx
->DrawBuffer
->
739 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
741 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
742 y
= ctx
->DrawBuffer
->Height
- y
- height
;
745 if(format
!= GL_DEPTH_STENCIL
&&
746 util_format_get_component_bits(strb
->format
,
747 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
748 usage
= PIPE_TRANSFER_READ_WRITE
;
750 usage
= PIPE_TRANSFER_WRITE
;
752 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0, 0,
756 stmap
= pipe_transfer_map(pipe
, pt
);
758 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
761 /* if width > MAX_WIDTH, have to process image in chunks */
763 while (skipPixels
< width
) {
764 const GLint spanX
= skipPixels
;
765 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
767 for (row
= 0; row
< height
; row
++) {
768 GLubyte sValues
[MAX_WIDTH
];
769 GLuint zValues
[MAX_WIDTH
];
770 GLenum destType
= GL_UNSIGNED_BYTE
;
771 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
775 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
776 type
, source
, &clippedUnpack
,
777 ctx
->_ImageTransferState
);
779 if (format
== GL_DEPTH_STENCIL
) {
780 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
781 (1 << 24) - 1, type
, source
,
786 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
787 "zoom not complete");
793 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
794 spanY
= height
- row
- 1;
800 /* now pack the stencil (and Z) values in the dest format */
801 switch (pt
->resource
->format
) {
802 case PIPE_FORMAT_S8_USCALED
:
804 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
805 assert(usage
== PIPE_TRANSFER_WRITE
);
806 memcpy(dest
, sValues
, spanWidth
);
809 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
810 if (format
== GL_DEPTH_STENCIL
) {
811 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
813 assert(usage
== PIPE_TRANSFER_WRITE
);
814 for (k
= 0; k
< spanWidth
; k
++) {
815 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
819 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
821 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
822 for (k
= 0; k
< spanWidth
; k
++) {
823 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
827 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
828 if (format
== GL_DEPTH_STENCIL
) {
829 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
831 assert(usage
== PIPE_TRANSFER_WRITE
);
832 for (k
= 0; k
< spanWidth
; k
++) {
833 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
837 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
839 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
840 for (k
= 0; k
< spanWidth
; k
++) {
841 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
850 skipPixels
+= spanWidth
;
853 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
855 /* unmap the stencil buffer */
856 pipe_transfer_unmap(pipe
, pt
);
857 pipe
->transfer_destroy(pipe
, pt
);
862 * Called via ctx->Driver.DrawPixels()
865 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
866 GLsizei width
, GLsizei height
,
867 GLenum format
, GLenum type
,
868 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
870 void *driver_vp
, *driver_fp
;
871 struct st_context
*st
= st_context(ctx
);
872 const GLfloat
*color
;
873 struct pipe_context
*pipe
= st
->pipe
;
874 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
875 struct pipe_sampler_view
*sv
[2];
876 int num_sampler_view
= 1;
877 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
879 if (format
== GL_DEPTH_STENCIL
)
880 write_stencil
= write_depth
= GL_TRUE
;
881 else if (format
== GL_STENCIL_INDEX
)
882 write_stencil
= GL_TRUE
;
883 else if (format
== GL_DEPTH_COMPONENT
)
884 write_depth
= GL_TRUE
;
887 enum pipe_format tex_format
;
888 /* can we write to stencil if not fallback */
889 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
890 goto stencil_fallback
;
892 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
894 0, PIPE_BIND_SAMPLER_VIEW
);
895 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
896 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
897 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
898 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
900 stencil_format
= PIPE_FORMAT_S8_USCALED
;
901 if (stencil_format
== PIPE_FORMAT_NONE
)
902 goto stencil_fallback
;
905 /* Mesa state should be up to date by now */
906 assert(ctx
->NewState
== 0x0);
908 st_validate_state(st
);
910 if (write_depth
|| write_stencil
) {
911 driver_fp
= make_fragment_shader_z_stencil(st
, write_depth
, write_stencil
);
912 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
913 color
= ctx
->Current
.RasterColor
;
916 driver_fp
= combined_drawpix_fragment_program(ctx
);
917 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
919 if (st
->pixel_xfer
.pixelmap_enabled
) {
920 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
925 /* draw with textured quad */
927 struct pipe_resource
*pt
928 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
930 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
934 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
939 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
941 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
946 color
, GL_FALSE
, write_depth
, write_stencil
);
947 pipe_sampler_view_reference(&sv
[0], NULL
);
948 if (num_sampler_view
> 1)
949 pipe_sampler_view_reference(&sv
[1], NULL
);
951 pipe_resource_reference(&pt
, NULL
);
957 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
964 * Software fallback for glCopyPixels(GL_STENCIL).
967 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
968 GLsizei width
, GLsizei height
,
969 GLint dstx
, GLint dsty
)
971 struct st_renderbuffer
*rbDraw
;
972 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
973 enum pipe_transfer_usage usage
;
974 struct pipe_transfer
*ptDraw
;
979 buffer
= malloc(width
* height
* sizeof(ubyte
));
981 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
985 /* Get the dest renderbuffer. If there's a wrapper, use the
986 * underlying renderbuffer.
988 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
989 if (rbDraw
->Base
.Wrapped
)
990 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
992 /* this will do stencil pixel transfer ops */
993 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
994 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
995 &ctx
->DefaultPacking
, buffer
);
997 if (util_format_get_component_bits(rbDraw
->format
,
998 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
999 usage
= PIPE_TRANSFER_READ_WRITE
;
1001 usage
= PIPE_TRANSFER_WRITE
;
1003 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1004 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1007 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
1008 rbDraw
->texture
, 0, 0, 0,
1012 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1013 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1015 /* map the stencil buffer */
1016 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1019 /* XXX PixelZoom not handled yet */
1020 for (i
= 0; i
< height
; i
++) {
1027 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1031 dst
= drawMap
+ y
* ptDraw
->stride
;
1032 src
= buffer
+ i
* width
;
1034 switch (ptDraw
->resource
->format
) {
1035 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1037 uint
*dst4
= (uint
*) dst
;
1039 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1040 for (j
= 0; j
< width
; j
++) {
1041 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1046 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1048 uint
*dst4
= (uint
*) dst
;
1050 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1051 for (j
= 0; j
< width
; j
++) {
1052 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1057 case PIPE_FORMAT_S8_USCALED
:
1058 assert(usage
== PIPE_TRANSFER_WRITE
);
1059 memcpy(dst
, src
, width
);
1068 /* unmap the stencil buffer */
1069 pipe_transfer_unmap(pipe
, ptDraw
);
1070 pipe
->transfer_destroy(pipe
, ptDraw
);
1075 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1076 GLsizei width
, GLsizei height
,
1077 GLint dstx
, GLint dsty
, GLenum type
)
1079 struct st_context
*st
= st_context(ctx
);
1080 struct pipe_context
*pipe
= st
->pipe
;
1081 struct pipe_screen
*screen
= pipe
->screen
;
1082 struct st_renderbuffer
*rbRead
;
1083 void *driver_vp
, *driver_fp
;
1084 struct pipe_resource
*pt
;
1085 struct pipe_sampler_view
*sv
[2];
1086 int num_sampler_view
= 1;
1088 enum pipe_format srcFormat
, texFormat
;
1089 GLboolean invertTex
= GL_FALSE
;
1090 GLint readX
, readY
, readW
, readH
;
1091 GLuint sample_count
;
1092 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1094 st_validate_state(st
);
1096 if (type
== GL_STENCIL
) {
1097 /* can't use texturing to do stencil */
1098 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1102 if (type
== GL_COLOR
) {
1103 rbRead
= st_get_color_read_renderbuffer(ctx
);
1105 driver_fp
= combined_drawpix_fragment_program(ctx
);
1106 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1107 if (st
->pixel_xfer
.pixelmap_enabled
) {
1108 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1113 assert(type
== GL_DEPTH
);
1114 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1115 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1116 driver_fp
= make_fragment_shader_z_stencil(st
, GL_TRUE
, GL_FALSE
);
1117 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1120 if (rbRead
->Base
.Wrapped
)
1121 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1123 sample_count
= rbRead
->texture
->nr_samples
;
1124 /* I believe this would be legal, presumably would need to do a resolve
1125 for color, and for depth/stencil spec says to just use one of the
1126 depth/stencil samples per pixel? Need some transfer clarifications. */
1127 assert(sample_count
< 2);
1129 srcFormat
= rbRead
->texture
->format
;
1131 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1133 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1134 texFormat
= srcFormat
;
1137 /* srcFormat can't be used as a texture format */
1138 if (type
== GL_DEPTH
) {
1139 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1140 st
->internal_target
, sample_count
,
1141 PIPE_BIND_DEPTH_STENCIL
);
1142 assert(texFormat
!= PIPE_FORMAT_NONE
);
1145 /* default color format */
1146 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1147 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1148 assert(texFormat
!= PIPE_FORMAT_NONE
);
1152 /* Invert src region if needed */
1153 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1154 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1155 invertTex
= !invertTex
;
1158 /* Clip the read region against the src buffer bounds.
1159 * We'll still allocate a temporary buffer/texture for the original
1160 * src region size but we'll only read the region which is on-screen.
1161 * This may mean that we draw garbage pixels into the dest region, but
1168 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1169 readW
= MAX2(0, readW
);
1170 readH
= MAX2(0, readH
);
1172 /* alloc temporary texture */
1173 pt
= alloc_texture(st
, width
, height
, texFormat
);
1177 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1179 pipe_resource_reference(&pt
, NULL
);
1183 /* Make temporary texture which is a copy of the src region.
1185 if (srcFormat
== texFormat
) {
1186 struct pipe_subresource srcsub
, dstsub
;
1191 /* copy source framebuffer surface into mipmap/texture */
1192 pipe
->resource_copy_region(pipe
,
1195 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1196 rbRead
->texture
, /* src tex */
1198 readX
, readY
, 0, readW
, readH
); /* src region */
1202 /* CPU-based fallback/conversion */
1203 struct pipe_transfer
*ptRead
=
1204 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0, 0,
1206 readX
, readY
, readW
, readH
);
1207 struct pipe_transfer
*ptTex
;
1208 enum pipe_transfer_usage transfer_usage
;
1210 if (ST_DEBUG
& DEBUG_FALLBACK
)
1211 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1213 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1214 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1216 transfer_usage
= PIPE_TRANSFER_WRITE
;
1218 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0, transfer_usage
,
1219 0, 0, width
, height
);
1221 /* copy image from ptRead surface to ptTex surface */
1222 if (type
== GL_COLOR
) {
1223 /* alternate path using get/put_tile() */
1224 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1225 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1226 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1232 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1233 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1234 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1239 pipe
->transfer_destroy(pipe
, ptRead
);
1240 pipe
->transfer_destroy(pipe
, ptTex
);
1243 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1244 * textured quad with that texture.
1246 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1247 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1252 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1254 pipe_resource_reference(&pt
, NULL
);
1255 pipe_sampler_view_reference(&sv
[0], NULL
);
1260 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1262 functions
->DrawPixels
= st_DrawPixels
;
1263 functions
->CopyPixels
= st_CopyPixels
;
1268 st_destroy_drawpix(struct st_context
*st
)
1272 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1273 if (st
->drawpix
.shaders
[i
])
1274 st_reference_fragprog(st
, &st
->drawpix
.shaders
[i
], NULL
);
1277 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1278 if (st
->drawpix
.vert_shaders
[0])
1279 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1280 if (st
->drawpix
.vert_shaders
[1])
1281 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1284 #endif /* FEATURE_drawpix */