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 "pipe/p_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.
97 static struct st_fragment_program
*
98 combined_drawpix_fragment_program(GLcontext
*ctx
)
100 struct st_context
*st
= st_context(ctx
);
101 struct st_fragment_program
*stfp
;
103 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
104 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
105 /* the pixel tranfer program has not changed and the user-defined
106 * program has not changed, so re-use the combined program.
108 stfp
= st
->pixel_xfer
.combined_prog
;
111 /* Concatenate the pixel transfer program with the current user-
114 if (is_passthrough_program(&st
->fp
->Base
)) {
115 stfp
= (struct st_fragment_program
*)
116 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
120 printf("Base program:\n");
121 _mesa_print_program(&st
->fp
->Base
.Base
);
122 printf("DrawPix program:\n");
123 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
125 stfp
= (struct st_fragment_program
*)
126 _mesa_combine_programs(ctx
,
127 &st
->pixel_xfer
.program
->Base
.Base
,
133 struct gl_program
*p
= &stfp
->Base
.Base
;
134 printf("Combined DrawPixels program:\n");
135 _mesa_print_program(p
);
136 printf("InputsRead: 0x%x\n", p
->InputsRead
);
137 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
138 _mesa_print_parameter_list(p
->Parameters
);
142 /* translate to TGSI tokens */
143 st_translate_fragment_program(st
, stfp
);
145 /* save new program, update serial numbers */
146 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
147 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
148 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
149 /* can't reference new program directly, already have a reference on it */
150 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
151 st
->pixel_xfer
.combined_prog
= stfp
;
154 /* Ideally we'd have updated the pipe constants during the normal
155 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
157 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
164 * Create fragment shader that does a TEX() instruction to get a Z
165 * value, then writes to FRAG_RESULT_DEPTH.
166 * Pass fragment color through as-is.
168 static struct st_fragment_program
*
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
;
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
;
232 * Create a simple vertex shader that just passes through the
233 * vertex position and texcoord (and optionally, color).
236 st_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.texcoord0; */
253 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
254 ureg_DECL_vs_input( ureg
, 1 ));
257 /* MOV result.color0, vertex.color0; */
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 _mesa_base_format(GLenum format
)
277 case GL_DEPTH_COMPONENT
:
278 return GL_DEPTH_COMPONENT
;
279 case GL_DEPTH_STENCIL
:
280 return GL_DEPTH_STENCIL
;
281 case GL_STENCIL_INDEX
:
282 return GL_STENCIL_INDEX
;
290 * Make texture containing an image for glDrawPixels image.
291 * If 'pixels' is NULL, leave the texture image data undefined.
293 static struct pipe_texture
*
294 make_texture(struct st_context
*st
,
295 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
296 const struct gl_pixelstore_attrib
*unpack
,
297 const GLvoid
*pixels
)
299 GLcontext
*ctx
= st
->ctx
;
300 struct pipe_context
*pipe
= st
->pipe
;
301 struct pipe_screen
*screen
= pipe
->screen
;
303 struct pipe_texture
*pt
;
304 enum pipe_format pipeFormat
;
309 baseFormat
= _mesa_base_format(format
);
311 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
314 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
316 cpp
= st_sizeof_format(pipeFormat
);
318 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
322 /* Need to use POT texture? */
325 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
328 l2pt
= util_logbase2(width
);
329 if (1<<l2pt
!= width
) {
332 l2pt
= util_logbase2(height
);
333 if (1<<l2pt
!= height
) {
337 /* Check against maximum texture size */
338 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
339 assert(ptw
<= maxSize
);
340 assert(pth
<= maxSize
);
343 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, ptw
, pth
, 1,
344 PIPE_TEXTURE_USAGE_SAMPLER
);
346 _mesa_unmap_pbo_source(ctx
, unpack
);
351 struct pipe_transfer
*transfer
;
352 static const GLuint dstImageOffsets
= 0;
355 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
357 /* we'll do pixel transfer in a fragment shader */
358 ctx
->_ImageTransferState
= 0x0;
360 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
361 PIPE_TRANSFER_WRITE
, 0, 0,
364 /* map texture transfer */
365 dest
= screen
->transfer_map(screen
, transfer
);
368 /* Put image into texture transfer.
369 * Note that the image is actually going to be upside down in
370 * the texture. We deal with that with texcoords.
372 success
= _mesa_texstore(ctx
, 2, /* dims */
373 baseFormat
, /* baseInternalFormat */
374 mformat
, /* gl_format */
376 0, 0, 0, /* dstX/Y/Zoffset */
377 transfer
->stride
, /* dstRowStride, bytes */
378 &dstImageOffsets
, /* dstImageOffsets */
379 width
, height
, 1, /* size */
380 format
, type
, /* src format/type */
381 pixels
, /* data source */
385 screen
->transfer_unmap(screen
, transfer
);
386 screen
->tex_transfer_destroy(transfer
);
391 ctx
->_ImageTransferState
= imageTransferStateSave
;
394 _mesa_unmap_pbo_source(ctx
, unpack
);
401 * Draw quad with texcoords and optional color.
402 * Coords are window coords with y=0=bottom.
403 * \param color may be null
404 * \param invertTex if true, flip texcoords vertically
407 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
408 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
409 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
411 struct st_context
*st
= st_context(ctx
);
412 struct pipe_context
*pipe
= st
->pipe
;
413 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
415 /* setup vertex data */
417 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
418 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
419 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
420 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
421 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
422 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
423 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
424 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
425 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
426 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
430 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
431 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
434 verts
[1][0][0] = clip_x1
;
435 verts
[1][0][1] = clip_y0
;
438 verts
[2][0][0] = clip_x1
;
439 verts
[2][0][1] = clip_y1
;
442 verts
[3][0][0] = clip_x0
;
443 verts
[3][0][1] = clip_y1
;
446 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
447 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
448 verts
[1][tex
][0] = sRight
;
449 verts
[1][tex
][1] = tTop
;
450 verts
[2][tex
][0] = sRight
;
451 verts
[2][tex
][1] = tBot
;
452 verts
[3][tex
][0] = sLeft
;
453 verts
[3][tex
][1] = tBot
;
455 /* same for all verts: */
457 for (i
= 0; i
< 4; i
++) {
458 verts
[i
][0][2] = z
; /*Z*/
459 verts
[i
][0][3] = 1.0f
; /*W*/
460 verts
[i
][1][0] = color
[0];
461 verts
[i
][1][1] = color
[1];
462 verts
[i
][1][2] = color
[2];
463 verts
[i
][1][3] = color
[3];
464 verts
[i
][2][2] = 0.0f
; /*R*/
465 verts
[i
][2][3] = 1.0f
; /*Q*/
469 for (i
= 0; i
< 4; i
++) {
470 verts
[i
][0][2] = z
; /*Z*/
471 verts
[i
][0][3] = 1.0f
; /*W*/
472 verts
[i
][1][2] = 0.0f
; /*R*/
473 verts
[i
][1][3] = 1.0f
; /*Q*/
479 struct pipe_buffer
*buf
;
481 /* allocate/load buffer object with vertex data */
482 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
484 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
486 util_draw_vertex_buffer(pipe
, buf
, 0,
489 3); /* attribs/vert */
490 pipe_buffer_reference(&buf
, NULL
);
497 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
498 GLsizei width
, GLsizei height
,
499 GLfloat zoomX
, GLfloat zoomY
,
500 struct pipe_texture
*pt
,
503 const GLfloat
*color
,
506 struct st_context
*st
= st_context(ctx
);
507 struct pipe_context
*pipe
= st
->pipe
;
508 struct cso_context
*cso
= st
->cso_context
;
509 GLfloat x0
, y0
, x1
, y1
;
513 /* XXX if DrawPixels image is larger than max texture size, break
516 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
517 assert(width
<= maxSize
);
518 assert(height
<= maxSize
);
520 cso_save_rasterizer(cso
);
521 cso_save_viewport(cso
);
522 cso_save_samplers(cso
);
523 cso_save_sampler_textures(cso
);
524 cso_save_fragment_shader(cso
);
525 cso_save_vertex_shader(cso
);
527 /* rasterizer state: just scissor */
529 struct pipe_rasterizer_state rasterizer
;
530 memset(&rasterizer
, 0, sizeof(rasterizer
));
531 rasterizer
.gl_rasterization_rules
= 1;
532 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
533 cso_set_rasterizer(cso
, &rasterizer
);
536 /* fragment shader state: TEX lookup program */
537 cso_set_fragment_shader_handle(cso
, driver_fp
);
539 /* vertex shader state: position + texcoord pass-through */
540 cso_set_vertex_shader_handle(cso
, driver_vp
);
543 /* texture sampling state: */
545 struct pipe_sampler_state sampler
;
546 memset(&sampler
, 0, sizeof(sampler
));
547 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
548 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
549 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
550 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
551 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
552 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
553 sampler
.normalized_coords
= 1;
555 cso_single_sampler(cso
, 0, &sampler
);
556 if (st
->pixel_xfer
.pixelmap_enabled
) {
557 cso_single_sampler(cso
, 1, &sampler
);
559 cso_single_sampler_done(cso
);
562 /* viewport state: viewport matching window dims */
564 const float w
= (float) ctx
->DrawBuffer
->Width
;
565 const float h
= (float) ctx
->DrawBuffer
->Height
;
566 struct pipe_viewport_state vp
;
567 vp
.scale
[0] = 0.5f
* w
;
568 vp
.scale
[1] = -0.5f
* h
;
571 vp
.translate
[0] = 0.5f
* w
;
572 vp
.translate
[1] = 0.5f
* h
;
573 vp
.translate
[2] = 0.0f
;
574 vp
.translate
[3] = 0.0f
;
575 cso_set_viewport(cso
, &vp
);
579 if (st
->pixel_xfer
.pixelmap_enabled
) {
580 struct pipe_texture
*textures
[2];
582 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
583 pipe
->set_fragment_sampler_textures(pipe
, 2, textures
);
586 pipe
->set_fragment_sampler_textures(pipe
, 1, &pt
);
589 /* Compute window coords (y=0=bottom) with pixel zoom.
590 * Recall that these coords are transformed by the current
591 * vertex shader and viewport transformation.
594 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
596 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
598 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
599 (GLfloat
) width
/ pt
->width0
,
600 (GLfloat
) height
/ pt
->height0
);
603 cso_restore_rasterizer(cso
);
604 cso_restore_viewport(cso
);
605 cso_restore_samplers(cso
);
606 cso_restore_sampler_textures(cso
);
607 cso_restore_fragment_shader(cso
);
608 cso_restore_vertex_shader(cso
);
613 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
614 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
615 const struct gl_pixelstore_attrib
*unpack
,
616 const GLvoid
*pixels
)
618 struct st_context
*st
= st_context(ctx
);
619 struct pipe_context
*pipe
= st
->pipe
;
620 struct pipe_screen
*screen
= pipe
->screen
;
621 struct st_renderbuffer
*strb
;
622 enum pipe_transfer_usage usage
;
623 struct pipe_transfer
*pt
;
624 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
627 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
630 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
632 /* totally clipped */
637 strb
= st_renderbuffer(ctx
->DrawBuffer
->
638 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
640 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
641 y
= ctx
->DrawBuffer
->Height
- y
- height
;
644 if(format
!= GL_DEPTH_STENCIL
&&
645 util_format_get_component_bits(strb
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
646 usage
= PIPE_TRANSFER_READ_WRITE
;
648 usage
= PIPE_TRANSFER_WRITE
;
650 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
654 stmap
= screen
->transfer_map(screen
, pt
);
656 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
659 /* if width > MAX_WIDTH, have to process image in chunks */
661 while (skipPixels
< width
) {
662 const GLint spanX
= skipPixels
;
663 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
665 for (row
= 0; row
< height
; row
++) {
666 GLubyte sValues
[MAX_WIDTH
];
667 GLuint zValues
[MAX_WIDTH
];
668 GLenum destType
= GL_UNSIGNED_BYTE
;
669 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
673 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
674 type
, source
, &clippedUnpack
,
675 ctx
->_ImageTransferState
);
677 if (format
== GL_DEPTH_STENCIL
) {
678 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
679 (1 << 24) - 1, type
, source
,
684 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
685 "zoom not complete");
691 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
692 spanY
= height
- row
- 1;
698 /* now pack the stencil (and Z) values in the dest format */
699 switch (pt
->texture
->format
) {
700 case PIPE_FORMAT_S8_UNORM
:
702 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
703 assert(usage
== PIPE_TRANSFER_WRITE
);
704 memcpy(dest
, sValues
, spanWidth
);
707 case PIPE_FORMAT_S8Z24_UNORM
:
708 if (format
== GL_DEPTH_STENCIL
) {
709 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
711 assert(usage
== PIPE_TRANSFER_WRITE
);
712 for (k
= 0; k
< spanWidth
; k
++) {
713 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
717 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
719 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
720 for (k
= 0; k
< spanWidth
; k
++) {
721 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
725 case PIPE_FORMAT_Z24S8_UNORM
:
726 if (format
== GL_DEPTH_STENCIL
) {
727 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
729 assert(usage
== PIPE_TRANSFER_WRITE
);
730 for (k
= 0; k
< spanWidth
; k
++) {
731 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
735 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
737 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
738 for (k
= 0; k
< spanWidth
; k
++) {
739 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
748 skipPixels
+= spanWidth
;
751 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
753 /* unmap the stencil buffer */
754 screen
->transfer_unmap(screen
, pt
);
755 screen
->tex_transfer_destroy(pt
);
760 * Called via ctx->Driver.DrawPixels()
763 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
764 GLenum format
, GLenum type
,
765 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
767 struct st_fragment_program
*stfp
;
769 struct st_context
*st
= st_context(ctx
);
770 struct pipe_surface
*ps
;
771 const GLfloat
*color
;
773 if (format
== GL_STENCIL_INDEX
||
774 format
== GL_DEPTH_STENCIL
) {
775 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
780 /* Mesa state should be up to date by now */
781 assert(ctx
->NewState
== 0x0);
783 st_validate_state(st
);
785 if (format
== GL_DEPTH_COMPONENT
) {
786 ps
= st
->state
.framebuffer
.zsbuf
;
787 stfp
= make_fragment_shader_z(st
);
788 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
789 color
= ctx
->Current
.RasterColor
;
792 ps
= st
->state
.framebuffer
.cbufs
[0];
793 stfp
= combined_drawpix_fragment_program(ctx
);
794 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
798 /* draw with textured quad */
800 struct pipe_texture
*pt
801 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
803 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
804 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
809 pipe_texture_reference(&pt
, NULL
);
817 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
818 GLsizei width
, GLsizei height
,
819 GLint dstx
, GLint dsty
)
821 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
822 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
823 enum pipe_transfer_usage usage
;
824 struct pipe_transfer
*ptDraw
;
829 buffer
= _mesa_malloc(width
* height
* sizeof(ubyte
));
831 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
835 /* this will do stencil pixel transfer ops */
836 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
837 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
838 &ctx
->DefaultPacking
, buffer
);
840 if(util_format_get_component_bits(rbDraw
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
841 usage
= PIPE_TRANSFER_READ_WRITE
;
843 usage
= PIPE_TRANSFER_WRITE
;
845 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
846 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
849 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
850 rbDraw
->texture
, 0, 0, 0,
854 assert(util_format_get_blockwidth(ptDraw
->texture
->format
) == 1);
855 assert(util_format_get_blockheight(ptDraw
->texture
->format
) == 1);
857 /* map the stencil buffer */
858 drawMap
= screen
->transfer_map(screen
, ptDraw
);
861 /* XXX PixelZoom not handled yet */
862 for (i
= 0; i
< height
; i
++) {
869 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
873 dst
= drawMap
+ y
* ptDraw
->stride
;
874 src
= buffer
+ i
* width
;
876 switch (ptDraw
->texture
->format
) {
877 case PIPE_FORMAT_S8Z24_UNORM
:
879 uint
*dst4
= (uint
*) dst
;
881 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
882 for (j
= 0; j
< width
; j
++) {
883 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
888 case PIPE_FORMAT_Z24S8_UNORM
:
890 uint
*dst4
= (uint
*) dst
;
892 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
893 for (j
= 0; j
< width
; j
++) {
894 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
899 case PIPE_FORMAT_S8_UNORM
:
900 assert(usage
== PIPE_TRANSFER_WRITE
);
901 memcpy(dst
, src
, width
);
910 /* unmap the stencil buffer */
911 screen
->transfer_unmap(screen
, ptDraw
);
912 screen
->tex_transfer_destroy(ptDraw
);
917 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
918 GLsizei width
, GLsizei height
,
919 GLint dstx
, GLint dsty
, GLenum type
)
921 struct st_context
*st
= st_context(ctx
);
922 struct pipe_context
*pipe
= st
->pipe
;
923 struct pipe_screen
*screen
= pipe
->screen
;
924 struct st_renderbuffer
*rbRead
;
926 struct st_fragment_program
*stfp
;
927 struct pipe_texture
*pt
;
929 enum pipe_format srcFormat
, texFormat
;
932 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
934 st_validate_state(st
);
960 if (width
< 0 || height
< 0)
964 if (type
== GL_STENCIL
) {
965 /* can't use texturing to do stencil */
966 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
970 if (type
== GL_COLOR
) {
971 rbRead
= st_get_color_read_renderbuffer(ctx
);
973 stfp
= combined_drawpix_fragment_program(ctx
);
974 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
977 assert(type
== GL_DEPTH
);
978 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
979 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
980 stfp
= make_fragment_shader_z(st
);
981 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
984 srcFormat
= rbRead
->texture
->format
;
986 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
987 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
988 texFormat
= srcFormat
;
991 /* srcFormat can't be used as a texture format */
992 if (type
== GL_DEPTH
) {
993 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
995 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
996 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
999 /* default color format */
1000 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
1001 PIPE_TEXTURE_USAGE_SAMPLER
);
1002 assert(texFormat
!= PIPE_FORMAT_NONE
);
1006 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1007 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1018 /* Need to use POT texture? */
1021 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
1024 l2pt
= util_logbase2(width
);
1025 if (1<<l2pt
!= width
) {
1028 l2pt
= util_logbase2(height
);
1029 if (1<<l2pt
!= height
) {
1033 /* Check against maximum texture size */
1034 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1035 assert(ptw
<= maxSize
);
1036 assert(pth
<= maxSize
);
1039 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1041 PIPE_TEXTURE_USAGE_SAMPLER
);
1046 if (srcFormat
== texFormat
) {
1047 /* copy source framebuffer surface into mipmap/texture */
1048 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1049 rbRead
->texture
, 0, 0, 0,
1050 PIPE_BUFFER_USAGE_GPU_READ
);
1051 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1052 PIPE_BUFFER_USAGE_GPU_WRITE
);
1053 if (pipe
->surface_copy
) {
1054 pipe
->surface_copy(pipe
,
1058 srcx
, srcy
, width
, height
);
1060 util_surface_copy(pipe
, FALSE
,
1064 srcx
, srcy
, width
, height
);
1066 pipe_surface_reference(&psRead
, NULL
);
1067 pipe_surface_reference(&psTex
, NULL
);
1070 /* CPU-based fallback/conversion */
1071 struct pipe_transfer
*ptRead
=
1072 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1073 PIPE_TRANSFER_READ
, srcx
, srcy
, width
,
1075 struct pipe_transfer
*ptTex
;
1076 enum pipe_transfer_usage transfer_usage
;
1078 if (ST_DEBUG
& DEBUG_FALLBACK
)
1079 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1081 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1082 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1084 transfer_usage
= PIPE_TRANSFER_WRITE
;
1086 ptTex
= st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, transfer_usage
,
1087 0, 0, width
, height
);
1089 if (type
== GL_COLOR
) {
1090 /* alternate path using get/put_tile() */
1091 GLfloat
*buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1093 pipe_get_tile_rgba(ptRead
, 0, 0, width
, height
, buf
);
1094 pipe_put_tile_rgba(ptTex
, 0, 0, width
, height
, buf
);
1100 GLuint
*buf
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
1101 pipe_get_tile_z(ptRead
, 0, 0, width
, height
, buf
);
1102 pipe_put_tile_z(ptTex
, 0, 0, width
, height
, buf
);
1106 screen
->tex_transfer_destroy(ptRead
);
1107 screen
->tex_transfer_destroy(ptTex
);
1110 /* draw textured quad */
1111 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1112 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1115 stfp
->driver_shader
,
1118 pipe_texture_reference(&pt
, NULL
);
1123 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1125 functions
->DrawPixels
= st_DrawPixels
;
1126 functions
->CopyPixels
= st_CopyPixels
;
1131 st_destroy_drawpix(struct st_context
*st
)
1133 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1134 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1135 if (st
->drawpix
.vert_shaders
[0])
1136 free(st
->drawpix
.vert_shaders
[0]);
1137 if (st
->drawpix
.vert_shaders
[1])
1138 free(st
->drawpix
.vert_shaders
[1]);