1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "main/texstore.h"
39 #include "program/program.h"
40 #include "program/prog_print.h"
41 #include "program/prog_instruction.h"
44 #include "st_atom_constbuf.h"
45 #include "st_cb_drawpixels.h"
46 #include "st_cb_readpixels.h"
47 #include "st_cb_fbo.h"
48 #include "st_context.h"
50 #include "st_format.h"
51 #include "st_program.h"
52 #include "st_texture.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "tgsi/tgsi_ureg.h"
57 #include "util/u_draw_quad.h"
58 #include "util/u_format.h"
59 #include "util/u_inlines.h"
60 #include "util/u_math.h"
61 #include "util/u_tile.h"
62 #include "cso_cache/cso_context.h"
68 * Check if the given program is:
69 * 0: MOVE result.color, fragment.color;
73 is_passthrough_program(const struct gl_fragment_program
*prog
)
75 if (prog
->Base
.NumInstructions
== 2) {
76 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
77 if (inst
[0].Opcode
== OPCODE_MOV
&&
78 inst
[1].Opcode
== OPCODE_END
&&
79 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
80 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
81 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
82 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
83 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
84 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
94 * Make fragment shader for glDraw/CopyPixels. This shader is made
95 * by combining the pixel transfer shader with the user-defined shader.
96 * \return pointer to Gallium driver fragment shader
99 combined_drawpix_fragment_program(struct gl_context
*ctx
)
101 struct st_context
*st
= st_context(ctx
);
102 struct st_fragment_program
*stfp
;
104 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
105 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
106 /* the pixel tranfer program has not changed and the user-defined
107 * program has not changed, so re-use the combined program.
109 stfp
= st
->pixel_xfer
.combined_prog
;
112 /* Concatenate the pixel transfer program with the current user-
115 if (is_passthrough_program(&st
->fp
->Base
)) {
116 stfp
= (struct st_fragment_program
*)
117 _mesa_clone_fragment_program(ctx
, &st
->pixel_xfer
.program
->Base
);
121 printf("Base program:\n");
122 _mesa_print_program(&st
->fp
->Base
.Base
);
123 printf("DrawPix program:\n");
124 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
126 stfp
= (struct st_fragment_program
*)
127 _mesa_combine_programs(ctx
,
128 &st
->pixel_xfer
.program
->Base
.Base
,
134 struct gl_program
*p
= &stfp
->Base
.Base
;
135 printf("Combined DrawPixels program:\n");
136 _mesa_print_program(p
);
137 printf("InputsRead: 0x%x\n", p
->InputsRead
);
138 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
139 _mesa_print_parameter_list(p
->Parameters
);
143 /* translate to TGSI tokens */
144 st_translate_fragment_program(st
, stfp
);
146 /* save new program, update serial numbers */
147 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
148 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
149 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
150 /* can't reference new program directly, already have a reference on it */
151 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
152 st
->pixel_xfer
.combined_prog
= stfp
;
155 /* Ideally we'd have updated the pipe constants during the normal
156 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
158 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
160 return stfp
->driver_shader
;
165 * Create fragment shader that does a TEX() instruction to get a Z
166 * value, then writes to FRAG_RESULT_DEPTH.
167 * Pass fragment color through as-is.
168 * \return pointer to the Gallium driver fragment shader
171 make_fragment_shader_z(struct st_context
*st
, GLboolean write_depth
,
172 GLboolean write_stencil
)
174 struct gl_context
*ctx
= st
->ctx
;
175 struct gl_program
*p
;
176 struct st_fragment_program
*stp
;
178 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
180 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
182 if (st
->drawpix
.shaders
[shaderIndex
]) {
183 /* already have the proper shader */
184 return st
->drawpix
.shaders
[shaderIndex
]->driver_shader
;
190 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
194 p
->NumInstructions
= write_depth
? 2 : 1;
195 p
->NumInstructions
+= write_stencil
? 1 : 0;
197 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
198 if (!p
->Instructions
) {
199 ctx
->Driver
.DeleteProgram(ctx
, p
);
202 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
205 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
206 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
207 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
208 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
209 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
210 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
211 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
212 p
->Instructions
[ic
].TexSrcUnit
= 0;
213 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
218 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
219 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
220 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
221 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
222 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
223 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
224 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
225 p
->Instructions
[ic
].TexSrcUnit
= 1;
226 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
231 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
233 assert(ic
== p
->NumInstructions
);
235 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
236 p
->OutputsWritten
= 0;
238 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
240 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
242 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
244 p
->SamplersUsed
|= 1 << 1;
246 stp
= st_fragment_program((struct gl_fragment_program
*) p
);
248 /* save the new shader */
249 st
->drawpix
.shaders
[shaderIndex
] = stp
;
251 st_translate_fragment_program(st
, stp
);
253 return stp
->driver_shader
;
259 * Create a simple vertex shader that just passes through the
260 * vertex position and texcoord (and optionally, color).
263 make_passthrough_vertex_shader(struct st_context
*st
,
266 if (!st
->drawpix
.vert_shaders
[passColor
]) {
267 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
272 /* MOV result.pos, vertex.pos; */
274 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
275 ureg_DECL_vs_input( ureg
, 0 ));
277 /* MOV result.texcoord0, vertex.attr[1]; */
279 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
280 ureg_DECL_vs_input( ureg
, 1 ));
283 /* MOV result.color0, vertex.attr[2]; */
285 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
286 ureg_DECL_vs_input( ureg
, 2 ));
291 st
->drawpix
.vert_shaders
[passColor
] =
292 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
295 return st
->drawpix
.vert_shaders
[passColor
];
300 * Return a texture internalFormat for drawing/copying an image
304 base_format(GLenum format
)
307 case GL_DEPTH_COMPONENT
:
308 return GL_DEPTH_COMPONENT
;
309 case GL_DEPTH_STENCIL
:
310 return GL_DEPTH_STENCIL
;
311 case GL_STENCIL_INDEX
:
312 return GL_STENCIL_INDEX
;
320 * Create a temporary texture to hold an image of the given size.
321 * If width, height are not POT and the driver only handles POT textures,
322 * allocate the next larger size of texture that is POT.
324 static struct pipe_resource
*
325 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
326 enum pipe_format texFormat
)
328 struct pipe_resource
*pt
;
330 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
331 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
338 * Make texture containing an image for glDrawPixels image.
339 * If 'pixels' is NULL, leave the texture image data undefined.
341 static struct pipe_resource
*
342 make_texture(struct st_context
*st
,
343 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
344 const struct gl_pixelstore_attrib
*unpack
,
345 const GLvoid
*pixels
)
347 struct gl_context
*ctx
= st
->ctx
;
348 struct pipe_context
*pipe
= st
->pipe
;
350 struct pipe_resource
*pt
;
351 enum pipe_format pipeFormat
;
355 baseFormat
= base_format(format
);
357 mformat
= st_ChooseTextureFormat_renderable(ctx
, baseFormat
,
358 format
, type
, GL_FALSE
);
361 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
363 cpp
= util_format_get_blocksize(pipeFormat
);
365 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
369 /* alloc temporary texture */
370 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
372 _mesa_unmap_pbo_source(ctx
, unpack
);
377 struct pipe_transfer
*transfer
;
378 static const GLuint dstImageOffsets
= 0;
381 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
383 /* we'll do pixel transfer in a fragment shader */
384 ctx
->_ImageTransferState
= 0x0;
386 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0,
387 PIPE_TRANSFER_WRITE
, 0, 0,
390 /* map texture transfer */
391 dest
= pipe_transfer_map(pipe
, transfer
);
394 /* Put image into texture transfer.
395 * Note that the image is actually going to be upside down in
396 * the texture. We deal with that with texcoords.
398 success
= _mesa_texstore(ctx
, 2, /* dims */
399 baseFormat
, /* baseInternalFormat */
400 mformat
, /* gl_format */
402 0, 0, 0, /* dstX/Y/Zoffset */
403 transfer
->stride
, /* dstRowStride, bytes */
404 &dstImageOffsets
, /* dstImageOffsets */
405 width
, height
, 1, /* size */
406 format
, type
, /* src format/type */
407 pixels
, /* data source */
411 pipe_transfer_unmap(pipe
, transfer
);
412 pipe
->transfer_destroy(pipe
, transfer
);
417 ctx
->_ImageTransferState
= imageTransferStateSave
;
420 _mesa_unmap_pbo_source(ctx
, unpack
);
427 * Draw quad with texcoords and optional color.
428 * Coords are gallium window coords with y=0=top.
429 * \param color may be null
430 * \param invertTex if true, flip texcoords vertically
433 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
434 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
435 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
437 struct st_context
*st
= st_context(ctx
);
438 struct pipe_context
*pipe
= st
->pipe
;
439 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
441 /* setup vertex data */
443 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
444 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
445 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
446 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
447 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
448 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
449 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
450 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
451 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
452 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
456 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
457 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
460 verts
[1][0][0] = clip_x1
;
461 verts
[1][0][1] = clip_y0
;
464 verts
[2][0][0] = clip_x1
;
465 verts
[2][0][1] = clip_y1
;
468 verts
[3][0][0] = clip_x0
;
469 verts
[3][0][1] = clip_y1
;
471 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
472 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
473 verts
[1][1][0] = sRight
;
474 verts
[1][1][1] = tTop
;
475 verts
[2][1][0] = sRight
;
476 verts
[2][1][1] = tBot
;
477 verts
[3][1][0] = sLeft
;
478 verts
[3][1][1] = tBot
;
480 /* same for all verts: */
482 for (i
= 0; i
< 4; i
++) {
483 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
484 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
485 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
486 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
487 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
488 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
489 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
490 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
494 for (i
= 0; i
< 4; i
++) {
495 verts
[i
][0][2] = z
; /*Z*/
496 verts
[i
][0][3] = 1.0f
; /*W*/
497 verts
[i
][1][2] = 0.0f
; /*R*/
498 verts
[i
][1][3] = 1.0f
; /*Q*/
504 struct pipe_resource
*buf
;
506 /* allocate/load buffer object with vertex data */
507 buf
= pipe_buffer_create(pipe
->screen
,
508 PIPE_BIND_VERTEX_BUFFER
,
510 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
512 util_draw_vertex_buffer(pipe
, buf
, 0,
515 3); /* attribs/vert */
516 pipe_resource_reference(&buf
, NULL
);
523 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
524 GLsizei width
, GLsizei height
,
525 GLfloat zoomX
, GLfloat zoomY
,
526 struct pipe_sampler_view
**sv
,
527 int num_sampler_view
,
530 const GLfloat
*color
,
532 GLboolean write_depth
, GLboolean write_stencil
)
534 struct st_context
*st
= st_context(ctx
);
535 struct pipe_context
*pipe
= st
->pipe
;
536 struct cso_context
*cso
= st
->cso_context
;
537 GLfloat x0
, y0
, x1
, y1
;
539 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
542 /* XXX if DrawPixels image is larger than max texture size, break
545 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
546 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
547 assert(width
<= maxSize
);
548 assert(height
<= maxSize
);
550 cso_save_depth_stencil_alpha(cso
);
551 cso_save_rasterizer(cso
);
552 cso_save_viewport(cso
);
553 cso_save_samplers(cso
);
554 cso_save_fragment_sampler_views(cso
);
555 cso_save_fragment_shader(cso
);
556 cso_save_vertex_shader(cso
);
557 cso_save_vertex_elements(cso
);
559 /* rasterizer state: just scissor */
561 struct pipe_rasterizer_state rasterizer
;
562 memset(&rasterizer
, 0, sizeof(rasterizer
));
563 rasterizer
.gl_rasterization_rules
= 1;
564 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
565 cso_set_rasterizer(cso
, &rasterizer
);
568 if (write_depth
|| write_stencil
)
570 struct pipe_depth_stencil_alpha_state dsa
;
571 memset(&dsa
, 0, sizeof(dsa
));
573 dsa
.depth
.enabled
= 1;
574 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
575 dsa
.depth
.writemask
= 1;
578 dsa
.stencil
[0].enabled
= 1;
579 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
580 dsa
.stencil
[0].writemask
= 0xff;
581 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
583 cso_set_depth_stencil_alpha(cso
, &dsa
);
586 /* fragment shader state: TEX lookup program */
587 cso_set_fragment_shader_handle(cso
, driver_fp
);
589 /* vertex shader state: position + texcoord pass-through */
590 cso_set_vertex_shader_handle(cso
, driver_vp
);
593 /* texture sampling state: */
595 struct pipe_sampler_state sampler
;
596 memset(&sampler
, 0, sizeof(sampler
));
597 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
598 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
599 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
600 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
601 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
602 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
603 sampler
.normalized_coords
= normalized
;
605 cso_single_sampler(cso
, 0, &sampler
);
606 if (num_sampler_view
> 1) {
607 cso_single_sampler(cso
, 1, &sampler
);
609 cso_single_sampler_done(cso
);
612 /* viewport state: viewport matching window dims */
614 const float w
= (float) ctx
->DrawBuffer
->Width
;
615 const float h
= (float) ctx
->DrawBuffer
->Height
;
616 struct pipe_viewport_state vp
;
617 vp
.scale
[0] = 0.5f
* w
;
618 vp
.scale
[1] = -0.5f
* h
;
621 vp
.translate
[0] = 0.5f
* w
;
622 vp
.translate
[1] = 0.5f
* h
;
623 vp
.translate
[2] = 0.5f
;
624 vp
.translate
[3] = 0.0f
;
625 cso_set_viewport(cso
, &vp
);
628 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
631 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
633 /* Compute Gallium window coords (y=0=top) with pixel zoom.
634 * Recall that these coords are transformed by the current
635 * vertex shader and viewport transformation.
637 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
638 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
639 invertTex
= !invertTex
;
643 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
645 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
647 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
650 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
651 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
652 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
655 cso_restore_depth_stencil_alpha(cso
);
656 cso_restore_rasterizer(cso
);
657 cso_restore_viewport(cso
);
658 cso_restore_samplers(cso
);
659 cso_restore_fragment_sampler_views(cso
);
660 cso_restore_fragment_shader(cso
);
661 cso_restore_vertex_shader(cso
);
662 cso_restore_vertex_elements(cso
);
667 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
668 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
669 const struct gl_pixelstore_attrib
*unpack
,
670 const GLvoid
*pixels
)
672 struct st_context
*st
= st_context(ctx
);
673 struct pipe_context
*pipe
= st
->pipe
;
674 struct st_renderbuffer
*strb
;
675 enum pipe_transfer_usage usage
;
676 struct pipe_transfer
*pt
;
677 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
680 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
683 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
685 /* totally clipped */
690 strb
= st_renderbuffer(ctx
->DrawBuffer
->
691 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
693 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
694 y
= ctx
->DrawBuffer
->Height
- y
- height
;
697 if(format
!= GL_DEPTH_STENCIL
&&
698 util_format_get_component_bits(strb
->format
,
699 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
700 usage
= PIPE_TRANSFER_READ_WRITE
;
702 usage
= PIPE_TRANSFER_WRITE
;
704 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0, 0,
708 stmap
= pipe_transfer_map(pipe
, pt
);
710 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
713 /* if width > MAX_WIDTH, have to process image in chunks */
715 while (skipPixels
< width
) {
716 const GLint spanX
= skipPixels
;
717 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
719 for (row
= 0; row
< height
; row
++) {
720 GLubyte sValues
[MAX_WIDTH
];
721 GLuint zValues
[MAX_WIDTH
];
722 GLenum destType
= GL_UNSIGNED_BYTE
;
723 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
727 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
728 type
, source
, &clippedUnpack
,
729 ctx
->_ImageTransferState
);
731 if (format
== GL_DEPTH_STENCIL
) {
732 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
733 (1 << 24) - 1, type
, source
,
738 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
739 "zoom not complete");
745 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
746 spanY
= height
- row
- 1;
752 /* now pack the stencil (and Z) values in the dest format */
753 switch (pt
->resource
->format
) {
754 case PIPE_FORMAT_S8_USCALED
:
756 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
757 assert(usage
== PIPE_TRANSFER_WRITE
);
758 memcpy(dest
, sValues
, spanWidth
);
761 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
762 if (format
== GL_DEPTH_STENCIL
) {
763 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
765 assert(usage
== PIPE_TRANSFER_WRITE
);
766 for (k
= 0; k
< spanWidth
; k
++) {
767 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
771 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
773 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
774 for (k
= 0; k
< spanWidth
; k
++) {
775 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
779 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
780 if (format
== GL_DEPTH_STENCIL
) {
781 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
783 assert(usage
== PIPE_TRANSFER_WRITE
);
784 for (k
= 0; k
< spanWidth
; k
++) {
785 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
789 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
791 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
792 for (k
= 0; k
< spanWidth
; k
++) {
793 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
802 skipPixels
+= spanWidth
;
805 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
807 /* unmap the stencil buffer */
808 pipe_transfer_unmap(pipe
, pt
);
809 pipe
->transfer_destroy(pipe
, pt
);
814 * Called via ctx->Driver.DrawPixels()
817 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
818 GLsizei width
, GLsizei height
,
819 GLenum format
, GLenum type
,
820 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
822 void *driver_vp
, *driver_fp
;
823 struct st_context
*st
= st_context(ctx
);
824 const GLfloat
*color
;
825 struct pipe_context
*pipe
= st
->pipe
;
826 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
827 struct pipe_sampler_view
*sv
[2];
828 int num_sampler_view
= 1;
829 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
831 if (format
== GL_DEPTH_STENCIL
)
832 write_stencil
= write_depth
= GL_TRUE
;
833 else if (format
== GL_STENCIL_INDEX
)
834 write_stencil
= GL_TRUE
;
835 else if (format
== GL_DEPTH_COMPONENT
)
836 write_depth
= GL_TRUE
;
839 enum pipe_format tex_format
;
840 /* can we write to stencil if not fallback */
841 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
842 goto stencil_fallback
;
844 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
846 0, PIPE_BIND_SAMPLER_VIEW
);
847 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
848 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
849 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
850 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
852 stencil_format
= PIPE_FORMAT_S8_USCALED
;
853 if (stencil_format
== PIPE_FORMAT_NONE
)
854 goto stencil_fallback
;
857 /* Mesa state should be up to date by now */
858 assert(ctx
->NewState
== 0x0);
860 st_validate_state(st
);
862 if (write_depth
|| write_stencil
) {
863 driver_fp
= make_fragment_shader_z(st
, write_depth
, write_stencil
);
864 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
865 color
= ctx
->Current
.RasterColor
;
868 driver_fp
= combined_drawpix_fragment_program(ctx
);
869 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
871 if (st
->pixel_xfer
.pixelmap_enabled
) {
872 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
877 /* draw with textured quad */
879 struct pipe_resource
*pt
880 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
882 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
886 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
891 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
893 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
898 color
, GL_FALSE
, write_depth
, write_stencil
);
899 pipe_sampler_view_reference(&sv
[0], NULL
);
900 if (num_sampler_view
> 1)
901 pipe_sampler_view_reference(&sv
[1], NULL
);
903 pipe_resource_reference(&pt
, NULL
);
909 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
916 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
917 GLsizei width
, GLsizei height
,
918 GLint dstx
, GLint dsty
)
920 struct st_renderbuffer
*rbDraw
;
921 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
922 enum pipe_transfer_usage usage
;
923 struct pipe_transfer
*ptDraw
;
928 buffer
= malloc(width
* height
* sizeof(ubyte
));
930 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
934 /* Get the dest renderbuffer. If there's a wrapper, use the
935 * underlying renderbuffer.
937 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
938 if (rbDraw
->Base
.Wrapped
)
939 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
941 /* this will do stencil pixel transfer ops */
942 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
943 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
944 &ctx
->DefaultPacking
, buffer
);
946 if (util_format_get_component_bits(rbDraw
->format
,
947 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
948 usage
= PIPE_TRANSFER_READ_WRITE
;
950 usage
= PIPE_TRANSFER_WRITE
;
952 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
953 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
956 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
957 rbDraw
->texture
, 0, 0, 0,
961 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
962 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
964 /* map the stencil buffer */
965 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
968 /* XXX PixelZoom not handled yet */
969 for (i
= 0; i
< height
; i
++) {
976 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
980 dst
= drawMap
+ y
* ptDraw
->stride
;
981 src
= buffer
+ i
* width
;
983 switch (ptDraw
->resource
->format
) {
984 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
986 uint
*dst4
= (uint
*) dst
;
988 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
989 for (j
= 0; j
< width
; j
++) {
990 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
995 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
997 uint
*dst4
= (uint
*) dst
;
999 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1000 for (j
= 0; j
< width
; j
++) {
1001 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1006 case PIPE_FORMAT_S8_USCALED
:
1007 assert(usage
== PIPE_TRANSFER_WRITE
);
1008 memcpy(dst
, src
, width
);
1017 /* unmap the stencil buffer */
1018 pipe_transfer_unmap(pipe
, ptDraw
);
1019 pipe
->transfer_destroy(pipe
, ptDraw
);
1024 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1025 GLsizei width
, GLsizei height
,
1026 GLint dstx
, GLint dsty
, GLenum type
)
1028 struct st_context
*st
= st_context(ctx
);
1029 struct pipe_context
*pipe
= st
->pipe
;
1030 struct pipe_screen
*screen
= pipe
->screen
;
1031 struct st_renderbuffer
*rbRead
;
1032 void *driver_vp
, *driver_fp
;
1033 struct pipe_resource
*pt
;
1034 struct pipe_sampler_view
*sv
[2];
1035 int num_sampler_view
= 1;
1037 enum pipe_format srcFormat
, texFormat
;
1038 GLboolean invertTex
= GL_FALSE
;
1039 GLint readX
, readY
, readW
, readH
;
1040 GLuint sample_count
;
1041 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1043 st_validate_state(st
);
1045 if (type
== GL_STENCIL
) {
1046 /* can't use texturing to do stencil */
1047 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1051 if (type
== GL_COLOR
) {
1052 rbRead
= st_get_color_read_renderbuffer(ctx
);
1054 driver_fp
= combined_drawpix_fragment_program(ctx
);
1055 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1056 if (st
->pixel_xfer
.pixelmap_enabled
) {
1057 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1062 assert(type
== GL_DEPTH
);
1063 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1064 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1065 driver_fp
= make_fragment_shader_z(st
, GL_TRUE
, GL_FALSE
);
1066 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1069 if (rbRead
->Base
.Wrapped
)
1070 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1072 sample_count
= rbRead
->texture
->nr_samples
;
1073 /* I believe this would be legal, presumably would need to do a resolve
1074 for color, and for depth/stencil spec says to just use one of the
1075 depth/stencil samples per pixel? Need some transfer clarifications. */
1076 assert(sample_count
< 2);
1078 srcFormat
= rbRead
->texture
->format
;
1080 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1082 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1083 texFormat
= srcFormat
;
1086 /* srcFormat can't be used as a texture format */
1087 if (type
== GL_DEPTH
) {
1088 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1089 st
->internal_target
, sample_count
,
1090 PIPE_BIND_DEPTH_STENCIL
);
1091 assert(texFormat
!= PIPE_FORMAT_NONE
);
1094 /* default color format */
1095 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1096 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1097 assert(texFormat
!= PIPE_FORMAT_NONE
);
1101 /* Invert src region if needed */
1102 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1103 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1104 invertTex
= !invertTex
;
1107 /* Clip the read region against the src buffer bounds.
1108 * We'll still allocate a temporary buffer/texture for the original
1109 * src region size but we'll only read the region which is on-screen.
1110 * This may mean that we draw garbage pixels into the dest region, but
1117 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1118 readW
= MAX2(0, readW
);
1119 readH
= MAX2(0, readH
);
1121 /* alloc temporary texture */
1122 pt
= alloc_texture(st
, width
, height
, texFormat
);
1126 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1128 pipe_resource_reference(&pt
, NULL
);
1132 /* Make temporary texture which is a copy of the src region.
1134 if (srcFormat
== texFormat
) {
1135 struct pipe_subresource srcsub
, dstsub
;
1140 /* copy source framebuffer surface into mipmap/texture */
1141 pipe
->resource_copy_region(pipe
,
1144 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1145 rbRead
->texture
, /* src tex */
1147 readX
, readY
, 0, readW
, readH
); /* src region */
1151 /* CPU-based fallback/conversion */
1152 struct pipe_transfer
*ptRead
=
1153 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0, 0,
1155 readX
, readY
, readW
, readH
);
1156 struct pipe_transfer
*ptTex
;
1157 enum pipe_transfer_usage transfer_usage
;
1159 if (ST_DEBUG
& DEBUG_FALLBACK
)
1160 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1162 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1163 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1165 transfer_usage
= PIPE_TRANSFER_WRITE
;
1167 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0, transfer_usage
,
1168 0, 0, width
, height
);
1170 /* copy image from ptRead surface to ptTex surface */
1171 if (type
== GL_COLOR
) {
1172 /* alternate path using get/put_tile() */
1173 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1174 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1175 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1181 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1182 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1183 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1188 pipe
->transfer_destroy(pipe
, ptRead
);
1189 pipe
->transfer_destroy(pipe
, ptTex
);
1192 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1193 * textured quad with that texture.
1195 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1196 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1201 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1203 pipe_resource_reference(&pt
, NULL
);
1204 pipe_sampler_view_reference(&sv
[0], NULL
);
1209 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1211 functions
->DrawPixels
= st_DrawPixels
;
1212 functions
->CopyPixels
= st_CopyPixels
;
1217 st_destroy_drawpix(struct st_context
*st
)
1221 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1222 if (st
->drawpix
.shaders
[i
])
1223 st_reference_fragprog(st
, &st
->drawpix
.shaders
[i
], NULL
);
1226 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1227 if (st
->drawpix
.vert_shaders
[0])
1228 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1229 if (st
->drawpix
.vert_shaders
[1])
1230 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1233 #endif /* FEATURE_drawpix */