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"
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 "program/prog_instruction.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(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 * Create a temporary texture to hold an image of the given size.
297 * If width, height are not POT and the driver only handles POT textures,
298 * allocate the next larger size of texture that is POT.
300 static struct pipe_resource
*
301 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
302 enum pipe_format texFormat
)
304 struct pipe_context
*pipe
= st
->pipe
;
305 struct pipe_resource
*pt
;
307 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
308 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
315 * Make texture containing an image for glDrawPixels image.
316 * If 'pixels' is NULL, leave the texture image data undefined.
318 static struct pipe_resource
*
319 make_texture(struct st_context
*st
,
320 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
321 const struct gl_pixelstore_attrib
*unpack
,
322 const GLvoid
*pixels
)
324 GLcontext
*ctx
= st
->ctx
;
325 struct pipe_context
*pipe
= st
->pipe
;
327 struct pipe_resource
*pt
;
328 enum pipe_format pipeFormat
;
332 baseFormat
= base_format(format
);
334 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
337 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
339 cpp
= util_format_get_blocksize(pipeFormat
);
341 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
345 /* alloc temporary texture */
346 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
348 _mesa_unmap_pbo_source(ctx
, unpack
);
353 struct pipe_transfer
*transfer
;
354 static const GLuint dstImageOffsets
= 0;
357 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
359 /* we'll do pixel transfer in a fragment shader */
360 ctx
->_ImageTransferState
= 0x0;
362 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0,
363 PIPE_TRANSFER_WRITE
, 0, 0,
366 /* map texture transfer */
367 dest
= pipe_transfer_map(pipe
, transfer
);
370 /* Put image into texture transfer.
371 * Note that the image is actually going to be upside down in
372 * the texture. We deal with that with texcoords.
374 success
= _mesa_texstore(ctx
, 2, /* dims */
375 baseFormat
, /* baseInternalFormat */
376 mformat
, /* gl_format */
378 0, 0, 0, /* dstX/Y/Zoffset */
379 transfer
->stride
, /* dstRowStride, bytes */
380 &dstImageOffsets
, /* dstImageOffsets */
381 width
, height
, 1, /* size */
382 format
, type
, /* src format/type */
383 pixels
, /* data source */
387 pipe_transfer_unmap(pipe
, transfer
);
388 pipe
->transfer_destroy(pipe
, transfer
);
393 ctx
->_ImageTransferState
= imageTransferStateSave
;
396 _mesa_unmap_pbo_source(ctx
, unpack
);
403 * Draw quad with texcoords and optional color.
404 * Coords are gallium window coords with y=0=top.
405 * \param color may be null
406 * \param invertTex if true, flip texcoords vertically
409 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
410 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
411 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
413 struct st_context
*st
= st_context(ctx
);
414 struct pipe_context
*pipe
= st
->pipe
;
415 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
417 /* setup vertex data */
419 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
420 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
421 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
422 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
423 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
424 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
425 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
426 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
427 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
428 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
432 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
433 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
436 verts
[1][0][0] = clip_x1
;
437 verts
[1][0][1] = clip_y0
;
440 verts
[2][0][0] = clip_x1
;
441 verts
[2][0][1] = clip_y1
;
444 verts
[3][0][0] = clip_x0
;
445 verts
[3][0][1] = clip_y1
;
447 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
448 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
449 verts
[1][1][0] = sRight
;
450 verts
[1][1][1] = tTop
;
451 verts
[2][1][0] = sRight
;
452 verts
[2][1][1] = tBot
;
453 verts
[3][1][0] = sLeft
;
454 verts
[3][1][1] = tBot
;
456 /* same for all verts: */
458 for (i
= 0; i
< 4; i
++) {
459 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
460 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
461 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
462 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
463 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
464 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
465 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
466 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
470 for (i
= 0; i
< 4; i
++) {
471 verts
[i
][0][2] = z
; /*Z*/
472 verts
[i
][0][3] = 1.0f
; /*W*/
473 verts
[i
][1][2] = 0.0f
; /*R*/
474 verts
[i
][1][3] = 1.0f
; /*Q*/
480 struct pipe_resource
*buf
;
482 /* allocate/load buffer object with vertex data */
483 buf
= pipe_buffer_create(pipe
->screen
,
484 PIPE_BIND_VERTEX_BUFFER
,
486 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
488 util_draw_vertex_buffer(pipe
, buf
, 0,
491 3); /* attribs/vert */
492 pipe_resource_reference(&buf
, NULL
);
499 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
500 GLsizei width
, GLsizei height
,
501 GLfloat zoomX
, GLfloat zoomY
,
502 struct pipe_sampler_view
*sv
,
505 const GLfloat
*color
,
508 struct st_context
*st
= st_context(ctx
);
509 struct pipe_context
*pipe
= st
->pipe
;
510 struct cso_context
*cso
= st
->cso_context
;
511 GLfloat x0
, y0
, x1
, y1
;
513 boolean normalized
= sv
->texture
->target
!= PIPE_TEXTURE_RECT
;
516 /* XXX if DrawPixels image is larger than max texture size, break
519 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
520 assert(width
<= maxSize
);
521 assert(height
<= maxSize
);
523 cso_save_rasterizer(cso
);
524 cso_save_viewport(cso
);
525 cso_save_samplers(cso
);
526 cso_save_fragment_sampler_views(cso
);
527 cso_save_fragment_shader(cso
);
528 cso_save_vertex_shader(cso
);
529 cso_save_vertex_elements(cso
);
531 /* rasterizer state: just scissor */
533 struct pipe_rasterizer_state rasterizer
;
534 memset(&rasterizer
, 0, sizeof(rasterizer
));
535 rasterizer
.gl_rasterization_rules
= 1;
536 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
537 cso_set_rasterizer(cso
, &rasterizer
);
540 /* fragment shader state: TEX lookup program */
541 cso_set_fragment_shader_handle(cso
, driver_fp
);
543 /* vertex shader state: position + texcoord pass-through */
544 cso_set_vertex_shader_handle(cso
, driver_vp
);
547 /* texture sampling state: */
549 struct pipe_sampler_state sampler
;
550 memset(&sampler
, 0, sizeof(sampler
));
551 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
552 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
553 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
554 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
555 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
556 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
557 sampler
.normalized_coords
= normalized
;
559 cso_single_sampler(cso
, 0, &sampler
);
560 if (st
->pixel_xfer
.pixelmap_enabled
) {
561 cso_single_sampler(cso
, 1, &sampler
);
563 cso_single_sampler_done(cso
);
566 /* viewport state: viewport matching window dims */
568 const float w
= (float) ctx
->DrawBuffer
->Width
;
569 const float h
= (float) ctx
->DrawBuffer
->Height
;
570 struct pipe_viewport_state vp
;
571 vp
.scale
[0] = 0.5f
* w
;
572 vp
.scale
[1] = -0.5f
* h
;
575 vp
.translate
[0] = 0.5f
* w
;
576 vp
.translate
[1] = 0.5f
* h
;
577 vp
.translate
[2] = 0.5f
;
578 vp
.translate
[3] = 0.0f
;
579 cso_set_viewport(cso
, &vp
);
582 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
585 if (st
->pixel_xfer
.pixelmap_enabled
) {
586 struct pipe_sampler_view
*sampler_views
[2];
587 sampler_views
[0] = sv
;
588 sampler_views
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
589 cso_set_fragment_sampler_views(cso
, 2, sampler_views
);
592 cso_set_fragment_sampler_views(cso
, 1, &sv
);
595 /* Compute Gallium window coords (y=0=top) with pixel zoom.
596 * Recall that these coords are transformed by the current
597 * vertex shader and viewport transformation.
599 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
600 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
601 invertTex
= !invertTex
;
605 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
607 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
609 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
612 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
613 normalized
? ((GLfloat
) width
/ sv
->texture
->width0
) : (GLfloat
)width
,
614 normalized
? ((GLfloat
) height
/ sv
->texture
->height0
) : (GLfloat
)height
);
617 cso_restore_rasterizer(cso
);
618 cso_restore_viewport(cso
);
619 cso_restore_samplers(cso
);
620 cso_restore_fragment_sampler_views(cso
);
621 cso_restore_fragment_shader(cso
);
622 cso_restore_vertex_shader(cso
);
623 cso_restore_vertex_elements(cso
);
628 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
629 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
630 const struct gl_pixelstore_attrib
*unpack
,
631 const GLvoid
*pixels
)
633 struct st_context
*st
= st_context(ctx
);
634 struct pipe_context
*pipe
= st
->pipe
;
635 struct st_renderbuffer
*strb
;
636 enum pipe_transfer_usage usage
;
637 struct pipe_transfer
*pt
;
638 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
641 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
644 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
646 /* totally clipped */
651 strb
= st_renderbuffer(ctx
->DrawBuffer
->
652 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
654 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
655 y
= ctx
->DrawBuffer
->Height
- y
- height
;
658 if(format
!= GL_DEPTH_STENCIL
&&
659 util_format_get_component_bits(strb
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
660 usage
= PIPE_TRANSFER_READ_WRITE
;
662 usage
= PIPE_TRANSFER_WRITE
;
664 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0, 0,
668 stmap
= pipe_transfer_map(pipe
, pt
);
670 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
673 /* if width > MAX_WIDTH, have to process image in chunks */
675 while (skipPixels
< width
) {
676 const GLint spanX
= skipPixels
;
677 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
679 for (row
= 0; row
< height
; row
++) {
680 GLubyte sValues
[MAX_WIDTH
];
681 GLuint zValues
[MAX_WIDTH
];
682 GLenum destType
= GL_UNSIGNED_BYTE
;
683 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
687 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
688 type
, source
, &clippedUnpack
,
689 ctx
->_ImageTransferState
);
691 if (format
== GL_DEPTH_STENCIL
) {
692 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
693 (1 << 24) - 1, type
, source
,
698 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
699 "zoom not complete");
705 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
706 spanY
= height
- row
- 1;
712 /* now pack the stencil (and Z) values in the dest format */
713 switch (pt
->resource
->format
) {
714 case PIPE_FORMAT_S8_USCALED
:
716 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
717 assert(usage
== PIPE_TRANSFER_WRITE
);
718 memcpy(dest
, sValues
, spanWidth
);
721 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
722 if (format
== GL_DEPTH_STENCIL
) {
723 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
725 assert(usage
== PIPE_TRANSFER_WRITE
);
726 for (k
= 0; k
< spanWidth
; k
++) {
727 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
731 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
733 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
734 for (k
= 0; k
< spanWidth
; k
++) {
735 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
739 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
740 if (format
== GL_DEPTH_STENCIL
) {
741 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
743 assert(usage
== PIPE_TRANSFER_WRITE
);
744 for (k
= 0; k
< spanWidth
; k
++) {
745 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
749 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
751 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
752 for (k
= 0; k
< spanWidth
; k
++) {
753 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
762 skipPixels
+= spanWidth
;
765 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
767 /* unmap the stencil buffer */
768 pipe_transfer_unmap(pipe
, pt
);
769 pipe
->transfer_destroy(pipe
, pt
);
774 * Called via ctx->Driver.DrawPixels()
777 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
778 GLenum format
, GLenum type
,
779 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
781 void *driver_vp
, *driver_fp
;
782 struct st_context
*st
= st_context(ctx
);
783 const GLfloat
*color
;
785 if (format
== GL_STENCIL_INDEX
||
786 format
== GL_DEPTH_STENCIL
) {
787 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
792 /* Mesa state should be up to date by now */
793 assert(ctx
->NewState
== 0x0);
795 st_validate_state(st
);
797 if (format
== GL_DEPTH_COMPONENT
) {
798 driver_fp
= make_fragment_shader_z(st
);
799 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
800 color
= ctx
->Current
.RasterColor
;
803 driver_fp
= combined_drawpix_fragment_program(ctx
);
804 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
808 /* draw with textured quad */
810 struct pipe_resource
*pt
811 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
813 struct pipe_sampler_view
*sv
= st_create_texture_sampler_view(st
->pipe
, pt
);
816 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
817 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
822 pipe_sampler_view_reference(&sv
, NULL
);
824 pipe_resource_reference(&pt
, NULL
);
832 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
833 GLsizei width
, GLsizei height
,
834 GLint dstx
, GLint dsty
)
836 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
837 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
838 enum pipe_transfer_usage usage
;
839 struct pipe_transfer
*ptDraw
;
844 buffer
= malloc(width
* height
* sizeof(ubyte
));
846 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
850 /* this will do stencil pixel transfer ops */
851 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
852 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
853 &ctx
->DefaultPacking
, buffer
);
855 if(util_format_get_component_bits(rbDraw
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
856 usage
= PIPE_TRANSFER_READ_WRITE
;
858 usage
= PIPE_TRANSFER_WRITE
;
860 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
861 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
864 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
865 rbDraw
->texture
, 0, 0, 0,
869 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
870 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
872 /* map the stencil buffer */
873 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
876 /* XXX PixelZoom not handled yet */
877 for (i
= 0; i
< height
; i
++) {
884 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
888 dst
= drawMap
+ y
* ptDraw
->stride
;
889 src
= buffer
+ i
* width
;
891 switch (ptDraw
->resource
->format
) {
892 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
894 uint
*dst4
= (uint
*) dst
;
896 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
897 for (j
= 0; j
< width
; j
++) {
898 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
903 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
905 uint
*dst4
= (uint
*) dst
;
907 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
908 for (j
= 0; j
< width
; j
++) {
909 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
914 case PIPE_FORMAT_S8_USCALED
:
915 assert(usage
== PIPE_TRANSFER_WRITE
);
916 memcpy(dst
, src
, width
);
925 /* unmap the stencil buffer */
926 pipe_transfer_unmap(pipe
, ptDraw
);
927 pipe
->transfer_destroy(pipe
, ptDraw
);
932 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
933 GLsizei width
, GLsizei height
,
934 GLint dstx
, GLint dsty
, GLenum type
)
936 struct st_context
*st
= st_context(ctx
);
937 struct pipe_context
*pipe
= st
->pipe
;
938 struct pipe_screen
*screen
= pipe
->screen
;
939 struct st_renderbuffer
*rbRead
;
940 void *driver_vp
, *driver_fp
;
941 struct pipe_resource
*pt
;
942 struct pipe_sampler_view
*sv
;
944 enum pipe_format srcFormat
, texFormat
;
945 GLboolean invertTex
= GL_FALSE
;
946 GLint readX
, readY
, readW
, readH
;
948 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
950 st_validate_state(st
);
952 if (type
== GL_STENCIL
) {
953 /* can't use texturing to do stencil */
954 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
958 if (type
== GL_COLOR
) {
959 rbRead
= st_get_color_read_renderbuffer(ctx
);
961 driver_fp
= combined_drawpix_fragment_program(ctx
);
962 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
965 assert(type
== GL_DEPTH
);
966 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
967 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
968 driver_fp
= make_fragment_shader_z(st
);
969 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
972 sample_count
= rbRead
->texture
->nr_samples
;
973 /* I believe this would be legal, presumably would need to do a resolve
974 for color, and for depth/stencil spec says to just use one of the
975 depth/stencil samples per pixel? Need some transfer clarifications. */
976 assert(sample_count
< 2);
978 srcFormat
= rbRead
->texture
->format
;
980 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
, sample_count
,
981 PIPE_BIND_SAMPLER_VIEW
, 0)) {
982 texFormat
= srcFormat
;
985 /* srcFormat can't be used as a texture format */
986 if (type
== GL_DEPTH
) {
987 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
988 st
->internal_target
, sample_count
,
989 PIPE_BIND_DEPTH_STENCIL
);
990 assert(texFormat
!= PIPE_FORMAT_NONE
);
993 /* default color format */
994 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
995 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
996 assert(texFormat
!= PIPE_FORMAT_NONE
);
1000 /* Invert src region if needed */
1001 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1002 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1003 invertTex
= !invertTex
;
1006 /* Clip the read region against the src buffer bounds.
1007 * We'll still allocate a temporary buffer/texture for the original
1008 * src region size but we'll only read the region which is on-screen.
1009 * This may mean that we draw garbage pixels into the dest region, but
1016 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1017 readW
= MAX2(0, readW
);
1018 readH
= MAX2(0, readH
);
1020 /* alloc temporary texture */
1021 pt
= alloc_texture(st
, width
, height
, texFormat
);
1025 sv
= st_create_texture_sampler_view(st
->pipe
, pt
);
1027 pipe_resource_reference(&pt
, NULL
);
1031 /* Make temporary texture which is a copy of the src region.
1033 if (srcFormat
== texFormat
) {
1034 struct pipe_subresource srcsub
, dstsub
;
1039 /* copy source framebuffer surface into mipmap/texture */
1040 pipe
->resource_copy_region(pipe
,
1043 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1044 rbRead
->texture
, /* src tex */
1046 readX
, readY
, 0, readW
, readH
); /* src region */
1050 /* CPU-based fallback/conversion */
1051 struct pipe_transfer
*ptRead
=
1052 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0, 0,
1054 readX
, readY
, readW
, readH
);
1055 struct pipe_transfer
*ptTex
;
1056 enum pipe_transfer_usage transfer_usage
;
1058 if (ST_DEBUG
& DEBUG_FALLBACK
)
1059 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1061 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1062 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1064 transfer_usage
= PIPE_TRANSFER_WRITE
;
1066 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0, transfer_usage
,
1067 0, 0, width
, height
);
1069 /* copy image from ptRead surface to ptTex surface */
1070 if (type
== GL_COLOR
) {
1071 /* alternate path using get/put_tile() */
1072 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1073 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1074 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1080 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1081 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1082 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1087 pipe
->transfer_destroy(pipe
, ptRead
);
1088 pipe
->transfer_destroy(pipe
, ptTex
);
1091 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1092 * textured quad with that texture.
1094 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1095 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1101 pipe_resource_reference(&pt
, NULL
);
1102 pipe_sampler_view_reference(&sv
, NULL
);
1107 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1109 functions
->DrawPixels
= st_DrawPixels
;
1110 functions
->CopyPixels
= st_CopyPixels
;
1115 st_destroy_drawpix(struct st_context
*st
)
1117 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1118 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1119 if (st
->drawpix
.vert_shaders
[0])
1120 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1121 if (st
->drawpix
.vert_shaders
[1])
1122 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1125 #endif /* FEATURE_drawpix */