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 "shader/program.h"
40 #include "shader/prog_print.h"
43 #include "st_context.h"
45 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_format.h"
51 #include "st_texture.h"
53 #include "pipe/p_context.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_inlines.h"
56 #include "tgsi/tgsi_ureg.h"
57 #include "util/u_tile.h"
58 #include "util/u_draw_quad.h"
59 #include "util/u_format.h"
60 #include "util/u_math.h"
61 #include "shader/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
66 * Check if the given program is:
67 * 0: MOVE result.color, fragment.color;
71 is_passthrough_program(const struct gl_fragment_program
*prog
)
73 if (prog
->Base
.NumInstructions
== 2) {
74 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
75 if (inst
[0].Opcode
== OPCODE_MOV
&&
76 inst
[1].Opcode
== OPCODE_END
&&
77 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
78 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
79 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
80 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
81 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
82 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
92 * Make fragment shader for glDraw/CopyPixels. This shader is made
93 * by combining the pixel transfer shader with the user-defined shader.
94 * \return pointer to Gallium driver fragment shader
97 combined_drawpix_fragment_program(GLcontext
*ctx
)
99 struct st_context
*st
= st_context(ctx
);
100 struct st_fragment_program
*stfp
;
102 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
103 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
104 /* the pixel tranfer program has not changed and the user-defined
105 * program has not changed, so re-use the combined program.
107 stfp
= st
->pixel_xfer
.combined_prog
;
110 /* Concatenate the pixel transfer program with the current user-
113 if (is_passthrough_program(&st
->fp
->Base
)) {
114 stfp
= (struct st_fragment_program
*)
115 _mesa_clone_fragment_program(ctx
, &st
->pixel_xfer
.program
->Base
);
119 printf("Base program:\n");
120 _mesa_print_program(&st
->fp
->Base
.Base
);
121 printf("DrawPix program:\n");
122 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
124 stfp
= (struct st_fragment_program
*)
125 _mesa_combine_programs(ctx
,
126 &st
->pixel_xfer
.program
->Base
.Base
,
132 struct gl_program
*p
= &stfp
->Base
.Base
;
133 printf("Combined DrawPixels program:\n");
134 _mesa_print_program(p
);
135 printf("InputsRead: 0x%x\n", p
->InputsRead
);
136 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
137 _mesa_print_parameter_list(p
->Parameters
);
141 /* translate to TGSI tokens */
142 st_translate_fragment_program(st
, stfp
);
144 /* save new program, update serial numbers */
145 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
146 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
147 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
148 /* can't reference new program directly, already have a reference on it */
149 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
150 st
->pixel_xfer
.combined_prog
= stfp
;
153 /* Ideally we'd have updated the pipe constants during the normal
154 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
156 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
158 return stfp
->driver_shader
;
163 * Create fragment shader that does a TEX() instruction to get a Z
164 * value, then writes to FRAG_RESULT_DEPTH.
165 * Pass fragment color through as-is.
166 * \return pointer to the Gallium driver fragment shader
169 make_fragment_shader_z(struct st_context
*st
)
171 GLcontext
*ctx
= st
->ctx
;
172 struct gl_program
*p
;
175 if (st
->drawpix
.z_shader
) {
176 return st
->drawpix
.z_shader
->driver_shader
;
182 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
186 p
->NumInstructions
= 3;
188 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
189 if (!p
->Instructions
) {
190 ctx
->Driver
.DeleteProgram(ctx
, p
);
193 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
195 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
196 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
197 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
198 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
199 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
200 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
201 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
202 p
->Instructions
[ic
].TexSrcUnit
= 0;
203 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
206 /* MOV result.color, fragment.color */
207 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
208 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
209 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
210 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
211 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
215 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
217 assert(ic
== p
->NumInstructions
);
219 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
220 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
221 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
223 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
224 st_translate_fragment_program(st
, st
->drawpix
.z_shader
);
226 return st
->drawpix
.z_shader
->driver_shader
;
232 * Create a simple vertex shader that just passes through the
233 * vertex position and texcoord (and optionally, color).
236 make_passthrough_vertex_shader(struct st_context
*st
,
239 if (!st
->drawpix
.vert_shaders
[passColor
]) {
240 struct ureg_program
*ureg
=
241 ureg_create( TGSI_PROCESSOR_VERTEX
);
246 /* MOV result.pos, vertex.pos; */
248 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
249 ureg_DECL_vs_input( ureg
, 0 ));
251 /* MOV result.texcoord0, vertex.attr[1]; */
253 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
254 ureg_DECL_vs_input( ureg
, 1 ));
257 /* MOV result.color0, vertex.attr[2]; */
259 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
260 ureg_DECL_vs_input( ureg
, 2 ));
265 st
->drawpix
.vert_shaders
[passColor
] =
266 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
269 return st
->drawpix
.vert_shaders
[passColor
];
274 * Return a texture internalFormat for drawing/copying an image
278 base_format(GLenum format
)
281 case GL_DEPTH_COMPONENT
:
282 return GL_DEPTH_COMPONENT
;
283 case GL_DEPTH_STENCIL
:
284 return GL_DEPTH_STENCIL
;
285 case GL_STENCIL_INDEX
:
286 return GL_STENCIL_INDEX
;
294 * Create a temporary texture to hold an image of the given size.
295 * If width, height are not POT and the driver only handles POT textures,
296 * allocate the next larger size of texture that is POT.
298 static struct pipe_resource
*
299 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
300 enum pipe_format texFormat
)
302 struct pipe_context
*pipe
= st
->pipe
;
303 struct pipe_screen
*screen
= pipe
->screen
;
304 struct pipe_resource
*pt
;
310 /* Need to use POT texture? */
311 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
314 l2pt
= util_logbase2(width
);
315 if (1 << l2pt
!= width
) {
316 ptw
= 1 << (l2pt
+ 1);
319 l2pt
= util_logbase2(height
);
320 if (1 << l2pt
!= height
) {
321 pth
= 1 << (l2pt
+ 1);
324 /* Check against maximum texture size */
325 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
326 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
327 assert(ptw
<= maxSize
);
328 assert(pth
<= maxSize
);
331 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
332 ptw
, pth
, 1, PIPE_BIND_SAMPLER_VIEW
);
339 * Make texture containing an image for glDrawPixels image.
340 * If 'pixels' is NULL, leave the texture image data undefined.
342 static struct pipe_resource
*
343 make_texture(struct st_context
*st
,
344 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
345 const struct gl_pixelstore_attrib
*unpack
,
346 const GLvoid
*pixels
)
348 GLcontext
*ctx
= st
->ctx
;
349 struct pipe_context
*pipe
= st
->pipe
;
351 struct pipe_resource
*pt
;
352 enum pipe_format pipeFormat
;
356 baseFormat
= base_format(format
);
358 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
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(GLcontext
*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(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
524 GLsizei width
, GLsizei height
,
525 GLfloat zoomX
, GLfloat zoomY
,
526 struct pipe_sampler_view
*sv
,
529 const GLfloat
*color
,
532 struct st_context
*st
= st_context(ctx
);
533 struct pipe_context
*pipe
= st
->pipe
;
534 struct cso_context
*cso
= st
->cso_context
;
535 GLfloat x0
, y0
, x1
, y1
;
539 /* XXX if DrawPixels image is larger than max texture size, break
542 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
543 assert(width
<= maxSize
);
544 assert(height
<= maxSize
);
546 cso_save_rasterizer(cso
);
547 cso_save_viewport(cso
);
548 cso_save_samplers(cso
);
549 cso_save_fragment_sampler_views(cso
);
550 cso_save_fragment_shader(cso
);
551 cso_save_vertex_shader(cso
);
552 cso_save_vertex_elements(cso
);
554 /* rasterizer state: just scissor */
556 struct pipe_rasterizer_state rasterizer
;
557 memset(&rasterizer
, 0, sizeof(rasterizer
));
558 rasterizer
.gl_rasterization_rules
= 1;
559 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
560 cso_set_rasterizer(cso
, &rasterizer
);
563 /* fragment shader state: TEX lookup program */
564 cso_set_fragment_shader_handle(cso
, driver_fp
);
566 /* vertex shader state: position + texcoord pass-through */
567 cso_set_vertex_shader_handle(cso
, driver_vp
);
570 /* texture sampling state: */
572 struct pipe_sampler_state sampler
;
573 memset(&sampler
, 0, sizeof(sampler
));
574 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
575 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
576 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
577 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
578 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
579 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
580 sampler
.normalized_coords
= 1;
582 cso_single_sampler(cso
, 0, &sampler
);
583 if (st
->pixel_xfer
.pixelmap_enabled
) {
584 cso_single_sampler(cso
, 1, &sampler
);
586 cso_single_sampler_done(cso
);
589 /* viewport state: viewport matching window dims */
591 const float w
= (float) ctx
->DrawBuffer
->Width
;
592 const float h
= (float) ctx
->DrawBuffer
->Height
;
593 struct pipe_viewport_state vp
;
594 vp
.scale
[0] = 0.5f
* w
;
595 vp
.scale
[1] = -0.5f
* h
;
598 vp
.translate
[0] = 0.5f
* w
;
599 vp
.translate
[1] = 0.5f
* h
;
600 vp
.translate
[2] = 0.5f
;
601 vp
.translate
[3] = 0.0f
;
602 cso_set_viewport(cso
, &vp
);
605 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
608 if (st
->pixel_xfer
.pixelmap_enabled
) {
609 struct pipe_sampler_view
*sampler_views
[2];
610 sampler_views
[0] = sv
;
611 sampler_views
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
612 cso_set_fragment_sampler_views(cso
, 2, sampler_views
);
615 cso_set_fragment_sampler_views(cso
, 1, &sv
);
618 /* Compute Gallium window coords (y=0=top) with pixel zoom.
619 * Recall that these coords are transformed by the current
620 * vertex shader and viewport transformation.
622 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
623 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
624 invertTex
= !invertTex
;
628 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
630 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
632 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
635 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
636 (GLfloat
) width
/ sv
->texture
->width0
,
637 (GLfloat
) height
/ sv
->texture
->height0
);
640 cso_restore_rasterizer(cso
);
641 cso_restore_viewport(cso
);
642 cso_restore_samplers(cso
);
643 cso_restore_fragment_sampler_views(cso
);
644 cso_restore_fragment_shader(cso
);
645 cso_restore_vertex_shader(cso
);
646 cso_restore_vertex_elements(cso
);
651 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
652 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
653 const struct gl_pixelstore_attrib
*unpack
,
654 const GLvoid
*pixels
)
656 struct st_context
*st
= st_context(ctx
);
657 struct pipe_context
*pipe
= st
->pipe
;
658 struct st_renderbuffer
*strb
;
659 enum pipe_transfer_usage usage
;
660 struct pipe_transfer
*pt
;
661 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
664 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
667 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
669 /* totally clipped */
674 strb
= st_renderbuffer(ctx
->DrawBuffer
->
675 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
677 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
678 y
= ctx
->DrawBuffer
->Height
- y
- height
;
681 if(format
!= GL_DEPTH_STENCIL
&&
682 util_format_get_component_bits(strb
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
683 usage
= PIPE_TRANSFER_READ_WRITE
;
685 usage
= PIPE_TRANSFER_WRITE
;
687 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0, 0,
691 stmap
= pipe_transfer_map(pipe
, pt
);
693 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
696 /* if width > MAX_WIDTH, have to process image in chunks */
698 while (skipPixels
< width
) {
699 const GLint spanX
= skipPixels
;
700 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
702 for (row
= 0; row
< height
; row
++) {
703 GLubyte sValues
[MAX_WIDTH
];
704 GLuint zValues
[MAX_WIDTH
];
705 GLenum destType
= GL_UNSIGNED_BYTE
;
706 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
710 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
711 type
, source
, &clippedUnpack
,
712 ctx
->_ImageTransferState
);
714 if (format
== GL_DEPTH_STENCIL
) {
715 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
716 (1 << 24) - 1, type
, source
,
721 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
722 "zoom not complete");
728 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
729 spanY
= height
- row
- 1;
735 /* now pack the stencil (and Z) values in the dest format */
736 switch (pt
->resource
->format
) {
737 case PIPE_FORMAT_S8_USCALED
:
739 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
740 assert(usage
== PIPE_TRANSFER_WRITE
);
741 memcpy(dest
, sValues
, spanWidth
);
744 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
745 if (format
== GL_DEPTH_STENCIL
) {
746 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
748 assert(usage
== PIPE_TRANSFER_WRITE
);
749 for (k
= 0; k
< spanWidth
; k
++) {
750 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
754 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
756 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
757 for (k
= 0; k
< spanWidth
; k
++) {
758 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
762 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
763 if (format
== GL_DEPTH_STENCIL
) {
764 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
766 assert(usage
== PIPE_TRANSFER_WRITE
);
767 for (k
= 0; k
< spanWidth
; k
++) {
768 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
772 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
774 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
775 for (k
= 0; k
< spanWidth
; k
++) {
776 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
785 skipPixels
+= spanWidth
;
788 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
790 /* unmap the stencil buffer */
791 pipe_transfer_unmap(pipe
, pt
);
792 pipe
->transfer_destroy(pipe
, pt
);
797 * Called via ctx->Driver.DrawPixels()
800 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
801 GLenum format
, GLenum type
,
802 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
804 void *driver_vp
, *driver_fp
;
805 struct st_context
*st
= st_context(ctx
);
806 const GLfloat
*color
;
808 if (format
== GL_STENCIL_INDEX
||
809 format
== GL_DEPTH_STENCIL
) {
810 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
815 /* Mesa state should be up to date by now */
816 assert(ctx
->NewState
== 0x0);
818 st_validate_state(st
);
820 if (format
== GL_DEPTH_COMPONENT
) {
821 driver_fp
= make_fragment_shader_z(st
);
822 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
823 color
= ctx
->Current
.RasterColor
;
826 driver_fp
= combined_drawpix_fragment_program(ctx
);
827 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
831 /* draw with textured quad */
833 struct pipe_resource
*pt
834 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
836 struct pipe_sampler_view
*sv
= st_create_texture_sampler_view(st
->pipe
, pt
);
839 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
840 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
845 pipe_sampler_view_reference(&sv
, NULL
);
847 pipe_resource_reference(&pt
, NULL
);
855 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
856 GLsizei width
, GLsizei height
,
857 GLint dstx
, GLint dsty
)
859 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
860 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
861 enum pipe_transfer_usage usage
;
862 struct pipe_transfer
*ptDraw
;
867 buffer
= malloc(width
* height
* sizeof(ubyte
));
869 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
873 /* this will do stencil pixel transfer ops */
874 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
875 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
876 &ctx
->DefaultPacking
, buffer
);
878 if(util_format_get_component_bits(rbDraw
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
879 usage
= PIPE_TRANSFER_READ_WRITE
;
881 usage
= PIPE_TRANSFER_WRITE
;
883 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
884 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
887 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
888 rbDraw
->texture
, 0, 0, 0,
892 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
893 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
895 /* map the stencil buffer */
896 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
899 /* XXX PixelZoom not handled yet */
900 for (i
= 0; i
< height
; i
++) {
907 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
911 dst
= drawMap
+ y
* ptDraw
->stride
;
912 src
= buffer
+ i
* width
;
914 switch (ptDraw
->resource
->format
) {
915 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
917 uint
*dst4
= (uint
*) dst
;
919 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
920 for (j
= 0; j
< width
; j
++) {
921 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
926 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
928 uint
*dst4
= (uint
*) dst
;
930 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
931 for (j
= 0; j
< width
; j
++) {
932 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
937 case PIPE_FORMAT_S8_USCALED
:
938 assert(usage
== PIPE_TRANSFER_WRITE
);
939 memcpy(dst
, src
, width
);
948 /* unmap the stencil buffer */
949 pipe_transfer_unmap(pipe
, ptDraw
);
950 pipe
->transfer_destroy(pipe
, ptDraw
);
955 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
956 GLsizei width
, GLsizei height
,
957 GLint dstx
, GLint dsty
, GLenum type
)
959 struct st_context
*st
= st_context(ctx
);
960 struct pipe_context
*pipe
= st
->pipe
;
961 struct pipe_screen
*screen
= pipe
->screen
;
962 struct st_renderbuffer
*rbRead
;
963 void *driver_vp
, *driver_fp
;
964 struct pipe_resource
*pt
;
965 struct pipe_sampler_view
*sv
;
967 enum pipe_format srcFormat
, texFormat
;
968 GLboolean invertTex
= GL_FALSE
;
969 GLint readX
, readY
, readW
, readH
;
970 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
972 st_validate_state(st
);
974 if (type
== GL_STENCIL
) {
975 /* can't use texturing to do stencil */
976 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
980 if (type
== GL_COLOR
) {
981 rbRead
= st_get_color_read_renderbuffer(ctx
);
983 driver_fp
= combined_drawpix_fragment_program(ctx
);
984 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
987 assert(type
== GL_DEPTH
);
988 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
989 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
990 driver_fp
= make_fragment_shader_z(st
);
991 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
994 srcFormat
= rbRead
->texture
->format
;
996 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
997 PIPE_BIND_SAMPLER_VIEW
, 0)) {
998 texFormat
= srcFormat
;
1001 /* srcFormat can't be used as a texture format */
1002 if (type
== GL_DEPTH
) {
1003 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1005 PIPE_BIND_DEPTH_STENCIL
);
1006 assert(texFormat
!= PIPE_FORMAT_NONE
);
1009 /* default color format */
1010 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
1011 PIPE_BIND_SAMPLER_VIEW
);
1012 assert(texFormat
!= PIPE_FORMAT_NONE
);
1016 /* Invert src region if needed */
1017 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1018 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1019 invertTex
= !invertTex
;
1022 /* Clip the read region against the src buffer bounds.
1023 * We'll still allocate a temporary buffer/texture for the original
1024 * src region size but we'll only read the region which is on-screen.
1025 * This may mean that we draw garbage pixels into the dest region, but
1032 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1033 readW
= MAX2(0, readW
);
1034 readH
= MAX2(0, readH
);
1036 /* alloc temporary texture */
1037 pt
= alloc_texture(st
, width
, height
, texFormat
);
1041 sv
= st_create_texture_sampler_view(st
->pipe
, pt
);
1043 pipe_resource_reference(&pt
, NULL
);
1047 /* Make temporary texture which is a copy of the src region.
1049 if (srcFormat
== texFormat
) {
1050 /* copy source framebuffer surface into mipmap/texture */
1051 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1052 rbRead
->texture
, 0, 0, 0,
1053 PIPE_BIND_BLIT_SOURCE
);
1054 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1055 PIPE_BIND_RENDER_TARGET
|
1056 PIPE_BIND_BLIT_DESTINATION
);
1057 pipe
->surface_copy(pipe
,
1058 psTex
, /* dest surf */
1059 pack
.SkipPixels
, pack
.SkipRows
, /* dest pos */
1060 psRead
, /* src surf */
1061 readX
, readY
, readW
, readH
); /* src region */
1065 debug_dump_surface(pipe
, "copypixsrcsurf", psRead
);
1066 debug_dump_surface(pipe
, "copypixtemptex", psTex
);
1069 pipe_surface_reference(&psRead
, NULL
);
1070 pipe_surface_reference(&psTex
, NULL
);
1073 /* CPU-based fallback/conversion */
1074 struct pipe_transfer
*ptRead
=
1075 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0, 0,
1077 readX
, readY
, readW
, readH
);
1078 struct pipe_transfer
*ptTex
;
1079 enum pipe_transfer_usage transfer_usage
;
1081 if (ST_DEBUG
& DEBUG_FALLBACK
)
1082 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1084 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1085 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1087 transfer_usage
= PIPE_TRANSFER_WRITE
;
1089 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0, transfer_usage
,
1090 0, 0, width
, height
);
1092 /* copy image from ptRead surface to ptTex surface */
1093 if (type
== GL_COLOR
) {
1094 /* alternate path using get/put_tile() */
1095 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1096 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1097 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1103 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1104 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1105 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1110 pipe
->transfer_destroy(pipe
, ptRead
);
1111 pipe
->transfer_destroy(pipe
, ptTex
);
1114 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1115 * textured quad with that texture.
1117 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1118 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1124 pipe_resource_reference(&pt
, NULL
);
1125 pipe_sampler_view_reference(&sv
, NULL
);
1130 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1132 functions
->DrawPixels
= st_DrawPixels
;
1133 functions
->CopyPixels
= st_CopyPixels
;
1138 st_destroy_drawpix(struct st_context
*st
)
1140 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1141 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1142 if (st
->drawpix
.vert_shaders
[0])
1143 free(st
->drawpix
.vert_shaders
[0]);
1144 if (st
->drawpix
.vert_shaders
[1])
1145 free(st
->drawpix
.vert_shaders
[1]);