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"
52 #include "st_inlines.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "util/u_inlines.h"
57 #include "tgsi/tgsi_ureg.h"
58 #include "util/u_tile.h"
59 #include "util/u_draw_quad.h"
60 #include "util/u_format.h"
61 #include "util/u_math.h"
62 #include "util/u_rect.h"
63 #include "shader/prog_instruction.h"
64 #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(GLcontext
*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
)
173 GLcontext
*ctx
= st
->ctx
;
174 struct gl_program
*p
;
177 if (st
->drawpix
.z_shader
) {
178 return st
->drawpix
.z_shader
->driver_shader
;
184 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
188 p
->NumInstructions
= 3;
190 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
191 if (!p
->Instructions
) {
192 ctx
->Driver
.DeleteProgram(ctx
, p
);
195 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
197 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
198 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
199 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
200 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
201 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
202 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
203 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
204 p
->Instructions
[ic
].TexSrcUnit
= 0;
205 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
208 /* MOV result.color, fragment.color */
209 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
210 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
211 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
212 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
213 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
217 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
219 assert(ic
== p
->NumInstructions
);
221 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
222 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
223 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
225 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
226 st_translate_fragment_program(st
, st
->drawpix
.z_shader
);
228 return st
->drawpix
.z_shader
->driver_shader
;
234 * Create a simple vertex shader that just passes through the
235 * vertex position and texcoord (and optionally, color).
238 make_passthrough_vertex_shader(struct st_context
*st
,
241 if (!st
->drawpix
.vert_shaders
[passColor
]) {
242 struct ureg_program
*ureg
=
243 ureg_create( TGSI_PROCESSOR_VERTEX
);
248 /* MOV result.pos, vertex.pos; */
250 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
251 ureg_DECL_vs_input( ureg
, 0 ));
253 /* MOV result.texcoord0, vertex.attr[1]; */
255 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
256 ureg_DECL_vs_input( ureg
, 1 ));
259 /* MOV result.color0, vertex.attr[2]; */
261 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
262 ureg_DECL_vs_input( ureg
, 2 ));
267 st
->drawpix
.vert_shaders
[passColor
] =
268 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
271 return st
->drawpix
.vert_shaders
[passColor
];
276 * Return a texture internalFormat for drawing/copying an image
280 base_format(GLenum format
)
283 case GL_DEPTH_COMPONENT
:
284 return GL_DEPTH_COMPONENT
;
285 case GL_DEPTH_STENCIL
:
286 return GL_DEPTH_STENCIL
;
287 case GL_STENCIL_INDEX
:
288 return GL_STENCIL_INDEX
;
296 * Make texture containing an image for glDrawPixels image.
297 * If 'pixels' is NULL, leave the texture image data undefined.
299 static struct pipe_texture
*
300 make_texture(struct st_context
*st
,
301 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
302 const struct gl_pixelstore_attrib
*unpack
,
303 const GLvoid
*pixels
)
305 GLcontext
*ctx
= st
->ctx
;
306 struct pipe_context
*pipe
= st
->pipe
;
307 struct pipe_screen
*screen
= pipe
->screen
;
309 struct pipe_texture
*pt
;
310 enum pipe_format pipeFormat
;
315 baseFormat
= base_format(format
);
317 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
320 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
322 cpp
= util_format_get_blocksize(pipeFormat
);
324 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
328 /* Need to use POT texture? */
331 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
334 l2pt
= util_logbase2(width
);
335 if (1<<l2pt
!= width
) {
338 l2pt
= util_logbase2(height
);
339 if (1<<l2pt
!= height
) {
343 /* Check against maximum texture size */
344 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
345 assert(ptw
<= maxSize
);
346 assert(pth
<= maxSize
);
349 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, ptw
, pth
, 1,
350 PIPE_TEXTURE_USAGE_SAMPLER
);
352 _mesa_unmap_pbo_source(ctx
, unpack
);
357 struct pipe_transfer
*transfer
;
358 static const GLuint dstImageOffsets
= 0;
361 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
363 /* we'll do pixel transfer in a fragment shader */
364 ctx
->_ImageTransferState
= 0x0;
366 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
367 PIPE_TRANSFER_WRITE
, 0, 0,
370 /* map texture transfer */
371 dest
= screen
->transfer_map(screen
, transfer
);
374 /* Put image into texture transfer.
375 * Note that the image is actually going to be upside down in
376 * the texture. We deal with that with texcoords.
378 success
= _mesa_texstore(ctx
, 2, /* dims */
379 baseFormat
, /* baseInternalFormat */
380 mformat
, /* gl_format */
382 0, 0, 0, /* dstX/Y/Zoffset */
383 transfer
->stride
, /* dstRowStride, bytes */
384 &dstImageOffsets
, /* dstImageOffsets */
385 width
, height
, 1, /* size */
386 format
, type
, /* src format/type */
387 pixels
, /* data source */
391 screen
->transfer_unmap(screen
, transfer
);
392 screen
->tex_transfer_destroy(transfer
);
397 ctx
->_ImageTransferState
= imageTransferStateSave
;
400 _mesa_unmap_pbo_source(ctx
, unpack
);
407 * Draw quad with texcoords and optional color.
408 * Coords are gallium window coords with y=0=top.
409 * \param color may be null
410 * \param invertTex if true, flip texcoords vertically
413 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
414 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
415 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
417 struct st_context
*st
= st_context(ctx
);
418 struct pipe_context
*pipe
= st
->pipe
;
419 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
421 /* setup vertex data */
423 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
424 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
425 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
426 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
427 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
428 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
429 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
430 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
431 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
432 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
436 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
437 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
440 verts
[1][0][0] = clip_x1
;
441 verts
[1][0][1] = clip_y0
;
444 verts
[2][0][0] = clip_x1
;
445 verts
[2][0][1] = clip_y1
;
448 verts
[3][0][0] = clip_x0
;
449 verts
[3][0][1] = clip_y1
;
451 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
452 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
453 verts
[1][1][0] = sRight
;
454 verts
[1][1][1] = tTop
;
455 verts
[2][1][0] = sRight
;
456 verts
[2][1][1] = tBot
;
457 verts
[3][1][0] = sLeft
;
458 verts
[3][1][1] = tBot
;
460 /* same for all verts: */
462 for (i
= 0; i
< 4; i
++) {
463 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
464 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
465 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
466 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
467 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
468 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
469 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
470 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
474 for (i
= 0; i
< 4; i
++) {
475 verts
[i
][0][2] = z
; /*Z*/
476 verts
[i
][0][3] = 1.0f
; /*W*/
477 verts
[i
][1][2] = 0.0f
; /*R*/
478 verts
[i
][1][3] = 1.0f
; /*Q*/
484 struct pipe_buffer
*buf
;
486 /* allocate/load buffer object with vertex data */
487 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
489 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
491 util_draw_vertex_buffer(pipe
, buf
, 0,
494 3); /* attribs/vert */
495 pipe_buffer_reference(&buf
, NULL
);
502 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
503 GLsizei width
, GLsizei height
,
504 GLfloat zoomX
, GLfloat zoomY
,
505 struct pipe_texture
*pt
,
508 const GLfloat
*color
,
511 struct st_context
*st
= st_context(ctx
);
512 struct pipe_context
*pipe
= st
->pipe
;
513 struct cso_context
*cso
= st
->cso_context
;
514 GLfloat x0
, y0
, x1
, y1
;
518 /* XXX if DrawPixels image is larger than max texture size, break
521 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
522 assert(width
<= maxSize
);
523 assert(height
<= maxSize
);
525 cso_save_rasterizer(cso
);
526 cso_save_viewport(cso
);
527 cso_save_samplers(cso
);
528 cso_save_sampler_textures(cso
);
529 cso_save_fragment_shader(cso
);
530 cso_save_vertex_shader(cso
);
532 /* rasterizer state: just scissor */
534 struct pipe_rasterizer_state rasterizer
;
535 memset(&rasterizer
, 0, sizeof(rasterizer
));
536 rasterizer
.gl_rasterization_rules
= 1;
537 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
538 cso_set_rasterizer(cso
, &rasterizer
);
541 /* fragment shader state: TEX lookup program */
542 cso_set_fragment_shader_handle(cso
, driver_fp
);
544 /* vertex shader state: position + texcoord pass-through */
545 cso_set_vertex_shader_handle(cso
, driver_vp
);
548 /* texture sampling state: */
550 struct pipe_sampler_state sampler
;
551 memset(&sampler
, 0, sizeof(sampler
));
552 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
553 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
554 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
555 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
556 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
557 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
558 sampler
.normalized_coords
= 1;
560 cso_single_sampler(cso
, 0, &sampler
);
561 if (st
->pixel_xfer
.pixelmap_enabled
) {
562 cso_single_sampler(cso
, 1, &sampler
);
564 cso_single_sampler_done(cso
);
567 /* viewport state: viewport matching window dims */
569 const float w
= (float) ctx
->DrawBuffer
->Width
;
570 const float h
= (float) ctx
->DrawBuffer
->Height
;
571 struct pipe_viewport_state vp
;
572 vp
.scale
[0] = 0.5f
* w
;
573 vp
.scale
[1] = -0.5f
* h
;
576 vp
.translate
[0] = 0.5f
* w
;
577 vp
.translate
[1] = 0.5f
* h
;
578 vp
.translate
[2] = 0.5f
;
579 vp
.translate
[3] = 0.0f
;
580 cso_set_viewport(cso
, &vp
);
584 if (st
->pixel_xfer
.pixelmap_enabled
) {
585 struct pipe_texture
*textures
[2];
587 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
588 pipe
->set_fragment_sampler_textures(pipe
, 2, textures
);
591 pipe
->set_fragment_sampler_textures(pipe
, 1, &pt
);
594 /* Compute Gallium window coords (y=0=top) with pixel zoom.
595 * Recall that these coords are transformed by the current
596 * vertex shader and viewport transformation.
598 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
599 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
600 invertTex
= !invertTex
;
604 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
606 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
608 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
611 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
612 (GLfloat
) width
/ pt
->width0
,
613 (GLfloat
) height
/ pt
->height0
);
616 cso_restore_rasterizer(cso
);
617 cso_restore_viewport(cso
);
618 cso_restore_samplers(cso
);
619 cso_restore_sampler_textures(cso
);
620 cso_restore_fragment_shader(cso
);
621 cso_restore_vertex_shader(cso
);
626 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
627 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
628 const struct gl_pixelstore_attrib
*unpack
,
629 const GLvoid
*pixels
)
631 struct st_context
*st
= st_context(ctx
);
632 struct pipe_context
*pipe
= st
->pipe
;
633 struct pipe_screen
*screen
= pipe
->screen
;
634 struct st_renderbuffer
*strb
;
635 enum pipe_transfer_usage usage
;
636 struct pipe_transfer
*pt
;
637 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
640 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
643 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
645 /* totally clipped */
650 strb
= st_renderbuffer(ctx
->DrawBuffer
->
651 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
653 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
654 y
= ctx
->DrawBuffer
->Height
- y
- height
;
657 if(format
!= GL_DEPTH_STENCIL
&&
658 util_format_get_component_bits(strb
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
659 usage
= PIPE_TRANSFER_READ_WRITE
;
661 usage
= PIPE_TRANSFER_WRITE
;
663 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
667 stmap
= screen
->transfer_map(screen
, pt
);
669 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
672 /* if width > MAX_WIDTH, have to process image in chunks */
674 while (skipPixels
< width
) {
675 const GLint spanX
= skipPixels
;
676 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
678 for (row
= 0; row
< height
; row
++) {
679 GLubyte sValues
[MAX_WIDTH
];
680 GLuint zValues
[MAX_WIDTH
];
681 GLenum destType
= GL_UNSIGNED_BYTE
;
682 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
686 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
687 type
, source
, &clippedUnpack
,
688 ctx
->_ImageTransferState
);
690 if (format
== GL_DEPTH_STENCIL
) {
691 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
692 (1 << 24) - 1, type
, source
,
697 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
698 "zoom not complete");
704 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
705 spanY
= height
- row
- 1;
711 /* now pack the stencil (and Z) values in the dest format */
712 switch (pt
->texture
->format
) {
713 case PIPE_FORMAT_S8_UNORM
:
715 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
716 assert(usage
== PIPE_TRANSFER_WRITE
);
717 memcpy(dest
, sValues
, spanWidth
);
720 case PIPE_FORMAT_Z24S8_UNORM
:
721 if (format
== GL_DEPTH_STENCIL
) {
722 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
724 assert(usage
== PIPE_TRANSFER_WRITE
);
725 for (k
= 0; k
< spanWidth
; k
++) {
726 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
730 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
732 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
733 for (k
= 0; k
< spanWidth
; k
++) {
734 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
738 case PIPE_FORMAT_S8Z24_UNORM
:
739 if (format
== GL_DEPTH_STENCIL
) {
740 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
742 assert(usage
== PIPE_TRANSFER_WRITE
);
743 for (k
= 0; k
< spanWidth
; k
++) {
744 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
748 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
750 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
751 for (k
= 0; k
< spanWidth
; k
++) {
752 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
761 skipPixels
+= spanWidth
;
764 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
766 /* unmap the stencil buffer */
767 screen
->transfer_unmap(screen
, pt
);
768 screen
->tex_transfer_destroy(pt
);
773 * Called via ctx->Driver.DrawPixels()
776 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
777 GLenum format
, GLenum type
,
778 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
780 void *driver_vp
, *driver_fp
;
781 struct st_context
*st
= st_context(ctx
);
782 const GLfloat
*color
;
784 if (format
== GL_STENCIL_INDEX
||
785 format
== GL_DEPTH_STENCIL
) {
786 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
791 /* Mesa state should be up to date by now */
792 assert(ctx
->NewState
== 0x0);
794 st_validate_state(st
);
796 if (format
== GL_DEPTH_COMPONENT
) {
797 driver_fp
= make_fragment_shader_z(st
);
798 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
799 color
= ctx
->Current
.RasterColor
;
802 driver_fp
= combined_drawpix_fragment_program(ctx
);
803 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
807 /* draw with textured quad */
809 struct pipe_texture
*pt
810 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
812 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
813 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
818 pipe_texture_reference(&pt
, NULL
);
826 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
827 GLsizei width
, GLsizei height
,
828 GLint dstx
, GLint dsty
)
830 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
831 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
832 enum pipe_transfer_usage usage
;
833 struct pipe_transfer
*ptDraw
;
838 buffer
= malloc(width
* height
* sizeof(ubyte
));
840 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
844 /* this will do stencil pixel transfer ops */
845 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
846 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
847 &ctx
->DefaultPacking
, buffer
);
849 if(util_format_get_component_bits(rbDraw
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
850 usage
= PIPE_TRANSFER_READ_WRITE
;
852 usage
= PIPE_TRANSFER_WRITE
;
854 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
855 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
858 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
859 rbDraw
->texture
, 0, 0, 0,
863 assert(util_format_get_blockwidth(ptDraw
->texture
->format
) == 1);
864 assert(util_format_get_blockheight(ptDraw
->texture
->format
) == 1);
866 /* map the stencil buffer */
867 drawMap
= screen
->transfer_map(screen
, ptDraw
);
870 /* XXX PixelZoom not handled yet */
871 for (i
= 0; i
< height
; i
++) {
878 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
882 dst
= drawMap
+ y
* ptDraw
->stride
;
883 src
= buffer
+ i
* width
;
885 switch (ptDraw
->texture
->format
) {
886 case PIPE_FORMAT_Z24S8_UNORM
:
888 uint
*dst4
= (uint
*) dst
;
890 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
891 for (j
= 0; j
< width
; j
++) {
892 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
897 case PIPE_FORMAT_S8Z24_UNORM
:
899 uint
*dst4
= (uint
*) dst
;
901 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
902 for (j
= 0; j
< width
; j
++) {
903 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
908 case PIPE_FORMAT_S8_UNORM
:
909 assert(usage
== PIPE_TRANSFER_WRITE
);
910 memcpy(dst
, src
, width
);
919 /* unmap the stencil buffer */
920 screen
->transfer_unmap(screen
, ptDraw
);
921 screen
->tex_transfer_destroy(ptDraw
);
926 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
927 GLsizei width
, GLsizei height
,
928 GLint dstx
, GLint dsty
, GLenum type
)
930 struct st_context
*st
= st_context(ctx
);
931 struct pipe_context
*pipe
= st
->pipe
;
932 struct pipe_screen
*screen
= pipe
->screen
;
933 struct st_renderbuffer
*rbRead
;
934 void *driver_vp
, *driver_fp
;
935 struct pipe_texture
*pt
;
937 enum pipe_format srcFormat
, texFormat
;
939 GLboolean invertTex
= GL_FALSE
;
940 GLint readX
, readY
, readW
, readH
;
941 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
943 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
945 st_validate_state(st
);
947 if (type
== GL_STENCIL
) {
948 /* can't use texturing to do stencil */
949 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
953 if (type
== GL_COLOR
) {
954 rbRead
= st_get_color_read_renderbuffer(ctx
);
956 driver_fp
= combined_drawpix_fragment_program(ctx
);
957 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
960 assert(type
== GL_DEPTH
);
961 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
962 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
963 driver_fp
= make_fragment_shader_z(st
);
964 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
967 srcFormat
= rbRead
->texture
->format
;
969 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
970 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
971 texFormat
= srcFormat
;
974 /* srcFormat can't be used as a texture format */
975 if (type
== GL_DEPTH
) {
976 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
978 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
979 assert(texFormat
!= PIPE_FORMAT_NONE
);
982 /* default color format */
983 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
984 PIPE_TEXTURE_USAGE_SAMPLER
);
985 assert(texFormat
!= PIPE_FORMAT_NONE
);
989 /* Invert src region if needed */
990 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
991 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
992 invertTex
= !invertTex
;
995 /* Clip the read region against the src buffer bounds.
996 * We'll still allocate a temporary buffer/texture for the original
997 * src region size but we'll only read the region which is on-screen.
998 * This may mean that we draw garbage pixels into the dest region, but
1005 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1006 readW
= MAX2(0, readW
);
1007 readH
= MAX2(0, readH
);
1009 /* Need to use POT texture? */
1012 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
1015 l2pt
= util_logbase2(width
);
1016 if (1<<l2pt
!= width
) {
1019 l2pt
= util_logbase2(height
);
1020 if (1<<l2pt
!= height
) {
1024 /* Check against maximum texture size */
1025 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1026 assert(ptw
<= maxSize
);
1027 assert(pth
<= maxSize
);
1030 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1032 PIPE_TEXTURE_USAGE_SAMPLER
);
1036 /* Make temporary texture which is a copy of the src region.
1038 if (srcFormat
== texFormat
) {
1039 /* copy source framebuffer surface into mipmap/texture */
1040 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1041 rbRead
->texture
, 0, 0, 0,
1042 PIPE_BUFFER_USAGE_GPU_READ
);
1043 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1044 PIPE_BUFFER_USAGE_GPU_WRITE
);
1045 if (pipe
->surface_copy
) {
1046 pipe
->surface_copy(pipe
,
1047 psTex
, /* dest surf */
1048 pack
.SkipPixels
, pack
.SkipRows
, /* dest pos */
1049 psRead
, /* src surf */
1050 readX
, readY
, readW
, readH
); /* src region */
1052 util_surface_copy(pipe
, FALSE
,
1054 pack
.SkipPixels
, pack
.SkipRows
,
1056 readX
, readY
, readW
, readH
);
1061 debug_dump_surface("copypixsrcsurf", psRead
);
1062 debug_dump_surface("copypixtemptex", psTex
);
1065 pipe_surface_reference(&psRead
, NULL
);
1066 pipe_surface_reference(&psTex
, NULL
);
1069 /* CPU-based fallback/conversion */
1070 struct pipe_transfer
*ptRead
=
1071 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1073 readX
, readY
, readW
, readH
);
1074 struct pipe_transfer
*ptTex
;
1075 enum pipe_transfer_usage transfer_usage
;
1077 if (ST_DEBUG
& DEBUG_FALLBACK
)
1078 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1080 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1081 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1083 transfer_usage
= PIPE_TRANSFER_WRITE
;
1085 ptTex
= st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, transfer_usage
,
1086 0, 0, width
, height
);
1088 /* copy image from ptRead surface to ptTex surface */
1089 if (type
== GL_COLOR
) {
1090 /* alternate path using get/put_tile() */
1091 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1092 pipe_get_tile_rgba(ptRead
, readX
, readY
, readW
, readH
, buf
);
1093 pipe_put_tile_rgba(ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1099 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1100 pipe_get_tile_z(ptRead
, readX
, readY
, readW
, readH
, buf
);
1101 pipe_put_tile_z(ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1106 screen
->tex_transfer_destroy(ptRead
);
1107 screen
->tex_transfer_destroy(ptTex
);
1110 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1111 * textured quad with that texture.
1113 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1114 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1120 pipe_texture_reference(&pt
, NULL
);
1125 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1127 functions
->DrawPixels
= st_DrawPixels
;
1128 functions
->CopyPixels
= st_CopyPixels
;
1133 st_destroy_drawpix(struct st_context
*st
)
1135 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1136 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1137 if (st
->drawpix
.vert_shaders
[0])
1138 free(st
->drawpix
.vert_shaders
[0]);
1139 if (st
->drawpix
.vert_shaders
[1])
1140 free(st
->drawpix
.vert_shaders
[1]);