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/mtypes.h"
38 #include "main/pack.h"
39 #include "main/texformat.h"
40 #include "main/texstore.h"
41 #include "program/program.h"
42 #include "program/prog_print.h"
43 #include "program/prog_instruction.h"
46 #include "st_atom_constbuf.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_context.h"
52 #include "st_format.h"
53 #include "st_program.h"
54 #include "st_texture.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "tgsi/tgsi_ureg.h"
59 #include "util/u_draw_quad.h"
60 #include "util/u_format.h"
61 #include "util/u_inlines.h"
62 #include "util/u_math.h"
63 #include "util/u_tile.h"
64 #include "cso_cache/cso_context.h"
70 * Check if the given program is:
71 * 0: MOVE result.color, fragment.color;
75 is_passthrough_program(const struct gl_fragment_program
*prog
)
77 if (prog
->Base
.NumInstructions
== 2) {
78 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
79 if (inst
[0].Opcode
== OPCODE_MOV
&&
80 inst
[1].Opcode
== OPCODE_END
&&
81 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
82 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
83 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
84 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
85 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
86 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
96 * Make fragment shader for glDraw/CopyPixels. This shader is made
97 * by combining the pixel transfer shader with the user-defined shader.
98 * \param fpIn the current/incoming fragment program
99 * \param fpOut returns the combined fragment program
102 st_make_drawpix_fragment_program(struct st_context
*st
,
103 struct gl_fragment_program
*fpIn
,
104 struct gl_fragment_program
**fpOut
)
106 struct gl_program
*newProg
;
108 if (is_passthrough_program(fpIn
)) {
109 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
110 &st
->pixel_xfer
.program
->Base
);
115 printf("Base program:\n");
116 _mesa_print_program(&fpIn
->Base
);
117 printf("DrawPix program:\n");
118 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
120 newProg
= _mesa_combine_programs(st
->ctx
,
121 &st
->pixel_xfer
.program
->Base
.Base
,
127 printf("Combined DrawPixels program:\n");
128 _mesa_print_program(newProg
);
129 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
130 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
131 _mesa_print_parameter_list(newProg
->Parameters
);
134 *fpOut
= (struct gl_fragment_program
*) newProg
;
139 * Create fragment program that does a TEX() instruction to get a Z and/or
140 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
141 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
142 * Pass fragment color through as-is.
143 * \return pointer to the gl_fragment program
145 struct gl_fragment_program
*
146 st_make_drawpix_z_stencil_program(struct st_context
*st
,
147 GLboolean write_depth
,
148 GLboolean write_stencil
)
150 struct gl_context
*ctx
= st
->ctx
;
151 struct gl_program
*p
;
152 struct gl_fragment_program
*fp
;
154 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
156 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
158 if (st
->drawpix
.shaders
[shaderIndex
]) {
159 /* already have the proper shader */
160 return st
->drawpix
.shaders
[shaderIndex
];
166 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
170 p
->NumInstructions
= write_depth
? 2 : 1;
171 p
->NumInstructions
+= write_stencil
? 1 : 0;
173 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
174 if (!p
->Instructions
) {
175 ctx
->Driver
.DeleteProgram(ctx
, p
);
178 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
181 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
182 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
183 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
184 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
185 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
186 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
187 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
188 p
->Instructions
[ic
].TexSrcUnit
= 0;
189 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
194 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
195 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
196 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
197 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
198 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
199 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
200 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
201 p
->Instructions
[ic
].TexSrcUnit
= 1;
202 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
207 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
209 assert(ic
== p
->NumInstructions
);
211 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
212 p
->OutputsWritten
= 0;
214 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
216 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
218 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
220 p
->SamplersUsed
|= 1 << 1;
222 fp
= (struct gl_fragment_program
*) p
;
224 /* save the new shader */
225 st
->drawpix
.shaders
[shaderIndex
] = fp
;
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
= ureg_create( TGSI_PROCESSOR_VERTEX
);
245 /* MOV result.pos, vertex.pos; */
247 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
248 ureg_DECL_vs_input( ureg
, 0 ));
250 /* MOV result.texcoord0, vertex.attr[1]; */
252 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
253 ureg_DECL_vs_input( ureg
, 1 ));
256 /* MOV result.color0, vertex.attr[2]; */
258 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
259 ureg_DECL_vs_input( ureg
, 2 ));
264 st
->drawpix
.vert_shaders
[passColor
] =
265 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
268 return st
->drawpix
.vert_shaders
[passColor
];
273 * Return a texture base format for drawing/copying an image
274 * of the given format.
277 base_format(GLenum format
)
280 case GL_DEPTH_COMPONENT
:
281 return GL_DEPTH_COMPONENT
;
282 case GL_DEPTH_STENCIL
:
283 return GL_DEPTH_STENCIL
;
284 case GL_STENCIL_INDEX
:
285 return GL_STENCIL_INDEX
;
293 * Return a texture internalFormat for drawing/copying an image
294 * of the given format and type.
297 internal_format(GLenum format
, GLenum type
)
300 case GL_DEPTH_COMPONENT
:
301 return GL_DEPTH_COMPONENT
;
302 case GL_DEPTH_STENCIL
:
303 return GL_DEPTH_STENCIL
;
304 case GL_STENCIL_INDEX
:
305 return GL_STENCIL_INDEX
;
307 if (_mesa_is_integer_format(format
)) {
311 case GL_UNSIGNED_BYTE
:
315 case GL_UNSIGNED_SHORT
:
319 case GL_UNSIGNED_INT
:
322 assert(0 && "Unexpected type in internal_format()");
323 return GL_RGBA_INTEGER
;
334 * Create a temporary texture to hold an image of the given size.
335 * If width, height are not POT and the driver only handles POT textures,
336 * allocate the next larger size of texture that is POT.
338 static struct pipe_resource
*
339 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
340 enum pipe_format texFormat
)
342 struct pipe_resource
*pt
;
344 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
345 width
, height
, 1, PIPE_BIND_SAMPLER_VIEW
);
352 * Make texture containing an image for glDrawPixels image.
353 * If 'pixels' is NULL, leave the texture image data undefined.
355 static struct pipe_resource
*
356 make_texture(struct st_context
*st
,
357 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
358 const struct gl_pixelstore_attrib
*unpack
,
359 const GLvoid
*pixels
)
361 struct gl_context
*ctx
= st
->ctx
;
362 struct pipe_context
*pipe
= st
->pipe
;
364 struct pipe_resource
*pt
;
365 enum pipe_format pipeFormat
;
367 GLenum baseFormat
, intFormat
;
369 baseFormat
= base_format(format
);
370 intFormat
= internal_format(format
, type
);
372 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
373 format
, type
, GL_FALSE
);
376 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
378 cpp
= util_format_get_blocksize(pipeFormat
);
380 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
384 /* alloc temporary texture */
385 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
387 _mesa_unmap_pbo_source(ctx
, unpack
);
392 struct pipe_transfer
*transfer
;
393 static const GLuint dstImageOffsets
= 0;
396 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
398 /* we'll do pixel transfer in a fragment shader */
399 ctx
->_ImageTransferState
= 0x0;
401 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
402 PIPE_TRANSFER_WRITE
, 0, 0,
405 /* map texture transfer */
406 dest
= pipe_transfer_map(pipe
, transfer
);
409 /* Put image into texture transfer.
410 * Note that the image is actually going to be upside down in
411 * the texture. We deal with that with texcoords.
413 success
= _mesa_texstore(ctx
, 2, /* dims */
414 baseFormat
, /* baseInternalFormat */
415 mformat
, /* gl_format */
417 0, 0, 0, /* dstX/Y/Zoffset */
418 transfer
->stride
, /* dstRowStride, bytes */
419 &dstImageOffsets
, /* dstImageOffsets */
420 width
, height
, 1, /* size */
421 format
, type
, /* src format/type */
422 pixels
, /* data source */
426 pipe_transfer_unmap(pipe
, transfer
);
427 pipe
->transfer_destroy(pipe
, transfer
);
432 ctx
->_ImageTransferState
= imageTransferStateSave
;
435 _mesa_unmap_pbo_source(ctx
, unpack
);
442 * Draw quad with texcoords and optional color.
443 * Coords are gallium window coords with y=0=top.
444 * \param color may be null
445 * \param invertTex if true, flip texcoords vertically
448 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
449 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
450 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
452 struct st_context
*st
= st_context(ctx
);
453 struct pipe_context
*pipe
= st
->pipe
;
454 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
456 /* setup vertex data */
458 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
459 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
460 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
461 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
462 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
463 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
464 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
465 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
466 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
467 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
471 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
472 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
475 verts
[1][0][0] = clip_x1
;
476 verts
[1][0][1] = clip_y0
;
479 verts
[2][0][0] = clip_x1
;
480 verts
[2][0][1] = clip_y1
;
483 verts
[3][0][0] = clip_x0
;
484 verts
[3][0][1] = clip_y1
;
486 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
487 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
488 verts
[1][1][0] = sRight
;
489 verts
[1][1][1] = tTop
;
490 verts
[2][1][0] = sRight
;
491 verts
[2][1][1] = tBot
;
492 verts
[3][1][0] = sLeft
;
493 verts
[3][1][1] = tBot
;
495 /* same for all verts: */
497 for (i
= 0; i
< 4; i
++) {
498 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
499 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
500 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
501 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
502 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
503 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
504 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
505 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
509 for (i
= 0; i
< 4; i
++) {
510 verts
[i
][0][2] = z
; /*Z*/
511 verts
[i
][0][3] = 1.0f
; /*W*/
512 verts
[i
][1][2] = 0.0f
; /*R*/
513 verts
[i
][1][3] = 1.0f
; /*Q*/
519 struct pipe_resource
*buf
;
521 /* allocate/load buffer object with vertex data */
522 buf
= pipe_buffer_create(pipe
->screen
,
523 PIPE_BIND_VERTEX_BUFFER
,
525 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
527 util_draw_vertex_buffer(pipe
, buf
, 0,
530 3); /* attribs/vert */
531 pipe_resource_reference(&buf
, NULL
);
538 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
539 GLsizei width
, GLsizei height
,
540 GLfloat zoomX
, GLfloat zoomY
,
541 struct pipe_sampler_view
**sv
,
542 int num_sampler_view
,
545 const GLfloat
*color
,
547 GLboolean write_depth
, GLboolean write_stencil
)
549 struct st_context
*st
= st_context(ctx
);
550 struct pipe_context
*pipe
= st
->pipe
;
551 struct cso_context
*cso
= st
->cso_context
;
552 GLfloat x0
, y0
, x1
, y1
;
554 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
557 /* XXX if DrawPixels image is larger than max texture size, break
560 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
561 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
562 assert(width
<= maxSize
);
563 assert(height
<= maxSize
);
565 cso_save_rasterizer(cso
);
566 cso_save_viewport(cso
);
567 cso_save_samplers(cso
);
568 cso_save_fragment_sampler_views(cso
);
569 cso_save_fragment_shader(cso
);
570 cso_save_vertex_shader(cso
);
571 cso_save_vertex_elements(cso
);
573 cso_save_depth_stencil_alpha(cso
);
577 /* rasterizer state: just scissor */
579 struct pipe_rasterizer_state rasterizer
;
580 memset(&rasterizer
, 0, sizeof(rasterizer
));
581 rasterizer
.gl_rasterization_rules
= 1;
582 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
583 cso_set_rasterizer(cso
, &rasterizer
);
587 /* Stencil writing bypasses the normal fragment pipeline to
588 * disable color writing and set stencil test to always pass.
590 struct pipe_depth_stencil_alpha_state dsa
;
591 struct pipe_blend_state blend
;
594 memset(&dsa
, 0, sizeof(dsa
));
595 dsa
.stencil
[0].enabled
= 1;
596 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
597 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
598 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
600 /* writing depth+stencil: depth test always passes */
601 dsa
.depth
.enabled
= 1;
602 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
603 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
605 cso_set_depth_stencil_alpha(cso
, &dsa
);
607 /* blend (colormask) */
608 memset(&blend
, 0, sizeof(blend
));
609 cso_set_blend(cso
, &blend
);
612 /* fragment shader state: TEX lookup program */
613 cso_set_fragment_shader_handle(cso
, driver_fp
);
615 /* vertex shader state: position + texcoord pass-through */
616 cso_set_vertex_shader_handle(cso
, driver_vp
);
619 /* texture sampling state: */
621 struct pipe_sampler_state sampler
;
622 memset(&sampler
, 0, sizeof(sampler
));
623 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
624 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
625 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
626 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
627 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
628 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
629 sampler
.normalized_coords
= normalized
;
631 cso_single_sampler(cso
, 0, &sampler
);
632 if (num_sampler_view
> 1) {
633 cso_single_sampler(cso
, 1, &sampler
);
635 cso_single_sampler_done(cso
);
638 /* viewport state: viewport matching window dims */
640 const float w
= (float) ctx
->DrawBuffer
->Width
;
641 const float h
= (float) ctx
->DrawBuffer
->Height
;
642 struct pipe_viewport_state vp
;
643 vp
.scale
[0] = 0.5f
* w
;
644 vp
.scale
[1] = -0.5f
* h
;
647 vp
.translate
[0] = 0.5f
* w
;
648 vp
.translate
[1] = 0.5f
* h
;
649 vp
.translate
[2] = 0.5f
;
650 vp
.translate
[3] = 0.0f
;
651 cso_set_viewport(cso
, &vp
);
654 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
657 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
659 /* Compute Gallium window coords (y=0=top) with pixel zoom.
660 * Recall that these coords are transformed by the current
661 * vertex shader and viewport transformation.
663 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
664 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
665 invertTex
= !invertTex
;
669 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
671 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
673 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
676 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
677 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
678 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
681 cso_restore_rasterizer(cso
);
682 cso_restore_viewport(cso
);
683 cso_restore_samplers(cso
);
684 cso_restore_fragment_sampler_views(cso
);
685 cso_restore_fragment_shader(cso
);
686 cso_restore_vertex_shader(cso
);
687 cso_restore_vertex_elements(cso
);
689 cso_restore_depth_stencil_alpha(cso
);
690 cso_restore_blend(cso
);
696 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
697 * can't use a fragment shader to write stencil values.
700 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
701 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
702 const struct gl_pixelstore_attrib
*unpack
,
703 const GLvoid
*pixels
)
705 struct st_context
*st
= st_context(ctx
);
706 struct pipe_context
*pipe
= st
->pipe
;
707 struct st_renderbuffer
*strb
;
708 enum pipe_transfer_usage usage
;
709 struct pipe_transfer
*pt
;
710 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
713 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
716 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
718 /* totally clipped */
723 strb
= st_renderbuffer(ctx
->DrawBuffer
->
724 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
726 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
727 y
= ctx
->DrawBuffer
->Height
- y
- height
;
730 if(format
!= GL_DEPTH_STENCIL
&&
731 util_format_get_component_bits(strb
->format
,
732 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
733 usage
= PIPE_TRANSFER_READ_WRITE
;
735 usage
= PIPE_TRANSFER_WRITE
;
737 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0,
741 stmap
= pipe_transfer_map(pipe
, pt
);
743 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
746 /* if width > MAX_WIDTH, have to process image in chunks */
748 while (skipPixels
< width
) {
749 const GLint spanX
= skipPixels
;
750 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
752 for (row
= 0; row
< height
; row
++) {
753 GLubyte sValues
[MAX_WIDTH
];
754 GLuint zValues
[MAX_WIDTH
];
755 GLenum destType
= GL_UNSIGNED_BYTE
;
756 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
760 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
761 type
, source
, &clippedUnpack
,
762 ctx
->_ImageTransferState
);
764 if (format
== GL_DEPTH_STENCIL
) {
765 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
766 (1 << 24) - 1, type
, source
,
771 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
772 "zoom not complete");
778 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
779 spanY
= height
- row
- 1;
785 /* now pack the stencil (and Z) values in the dest format */
786 switch (pt
->resource
->format
) {
787 case PIPE_FORMAT_S8_USCALED
:
789 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
790 assert(usage
== PIPE_TRANSFER_WRITE
);
791 memcpy(dest
, sValues
, spanWidth
);
794 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
795 if (format
== GL_DEPTH_STENCIL
) {
796 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
798 assert(usage
== PIPE_TRANSFER_WRITE
);
799 for (k
= 0; k
< spanWidth
; k
++) {
800 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
804 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
806 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
807 for (k
= 0; k
< spanWidth
; k
++) {
808 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
812 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
813 if (format
== GL_DEPTH_STENCIL
) {
814 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
816 assert(usage
== PIPE_TRANSFER_WRITE
);
817 for (k
= 0; k
< spanWidth
; k
++) {
818 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
822 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
824 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
825 for (k
= 0; k
< spanWidth
; k
++) {
826 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
835 skipPixels
+= spanWidth
;
838 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
840 /* unmap the stencil buffer */
841 pipe_transfer_unmap(pipe
, pt
);
842 pipe
->transfer_destroy(pipe
, pt
);
847 * Get fragment program variant for a glDrawPixels or glCopyPixels
848 * command for RGBA data.
850 static struct st_fp_variant
*
851 get_color_fp_variant(struct st_context
*st
)
853 struct gl_context
*ctx
= st
->ctx
;
854 struct st_fp_variant_key key
;
855 struct st_fp_variant
*fpv
;
857 memset(&key
, 0, sizeof(key
));
861 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
862 ctx
->Pixel
.RedScale
!= 1.0 ||
863 ctx
->Pixel
.GreenBias
!= 0.0 ||
864 ctx
->Pixel
.GreenScale
!= 1.0 ||
865 ctx
->Pixel
.BlueBias
!= 0.0 ||
866 ctx
->Pixel
.BlueScale
!= 1.0 ||
867 ctx
->Pixel
.AlphaBias
!= 0.0 ||
868 ctx
->Pixel
.AlphaScale
!= 1.0);
869 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
871 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
878 * Get fragment program variant for a glDrawPixels or glCopyPixels
879 * command for depth/stencil data.
881 static struct st_fp_variant
*
882 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
883 GLboolean write_stencil
)
885 struct st_fp_variant_key key
;
886 struct st_fp_variant
*fpv
;
888 memset(&key
, 0, sizeof(key
));
892 key
.drawpixels_z
= write_depth
;
893 key
.drawpixels_stencil
= write_stencil
;
895 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
902 * Called via ctx->Driver.DrawPixels()
905 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
906 GLsizei width
, GLsizei height
,
907 GLenum format
, GLenum type
,
908 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
910 void *driver_vp
, *driver_fp
;
911 struct st_context
*st
= st_context(ctx
);
912 const GLfloat
*color
;
913 struct pipe_context
*pipe
= st
->pipe
;
914 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
915 struct pipe_sampler_view
*sv
[2];
916 int num_sampler_view
= 1;
917 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
918 struct st_fp_variant
*fpv
;
920 if (format
== GL_DEPTH_STENCIL
)
921 write_stencil
= write_depth
= GL_TRUE
;
922 else if (format
== GL_STENCIL_INDEX
)
923 write_stencil
= GL_TRUE
;
924 else if (format
== GL_DEPTH_COMPONENT
)
925 write_depth
= GL_TRUE
;
928 enum pipe_format tex_format
;
929 /* can we write to stencil if not fallback */
930 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
931 goto stencil_fallback
;
933 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
935 0, PIPE_BIND_SAMPLER_VIEW
);
936 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
937 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
938 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
939 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
941 stencil_format
= PIPE_FORMAT_S8_USCALED
;
942 if (stencil_format
== PIPE_FORMAT_NONE
)
943 goto stencil_fallback
;
946 /* Mesa state should be up to date by now */
947 assert(ctx
->NewState
== 0x0);
949 st_validate_state(st
);
952 * Get vertex/fragment shaders
954 if (write_depth
|| write_stencil
) {
955 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
957 driver_fp
= fpv
->driver_shader
;
959 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
961 color
= ctx
->Current
.RasterColor
;
964 fpv
= get_color_fp_variant(st
);
966 driver_fp
= fpv
->driver_shader
;
968 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
971 if (st
->pixel_xfer
.pixelmap_enabled
) {
972 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
977 /* update fragment program constants */
978 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
980 /* draw with textured quad */
982 struct pipe_resource
*pt
983 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
985 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
989 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
994 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
996 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1001 color
, GL_FALSE
, write_depth
, write_stencil
);
1002 pipe_sampler_view_reference(&sv
[0], NULL
);
1003 if (num_sampler_view
> 1)
1004 pipe_sampler_view_reference(&sv
[1], NULL
);
1006 pipe_resource_reference(&pt
, NULL
);
1012 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1019 * Software fallback for glCopyPixels(GL_STENCIL).
1022 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1023 GLsizei width
, GLsizei height
,
1024 GLint dstx
, GLint dsty
)
1026 struct st_renderbuffer
*rbDraw
;
1027 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1028 enum pipe_transfer_usage usage
;
1029 struct pipe_transfer
*ptDraw
;
1034 buffer
= malloc(width
* height
* sizeof(ubyte
));
1036 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1040 /* Get the dest renderbuffer. If there's a wrapper, use the
1041 * underlying renderbuffer.
1043 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1044 if (rbDraw
->Base
.Wrapped
)
1045 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1047 /* this will do stencil pixel transfer ops */
1048 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1049 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1050 &ctx
->DefaultPacking
, buffer
);
1053 /* debug code: dump stencil values */
1055 for (row
= 0; row
< height
; row
++) {
1056 printf("%3d: ", row
);
1057 for (col
= 0; col
< width
; col
++) {
1058 printf("%02x ", buffer
[col
+ row
* width
]);
1064 if (util_format_get_component_bits(rbDraw
->format
,
1065 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1066 usage
= PIPE_TRANSFER_READ_WRITE
;
1068 usage
= PIPE_TRANSFER_WRITE
;
1070 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1071 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1074 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
1075 rbDraw
->texture
, 0, 0,
1079 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1080 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1082 /* map the stencil buffer */
1083 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1086 /* XXX PixelZoom not handled yet */
1087 for (i
= 0; i
< height
; i
++) {
1094 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1098 dst
= drawMap
+ y
* ptDraw
->stride
;
1099 src
= buffer
+ i
* width
;
1101 switch (ptDraw
->resource
->format
) {
1102 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1104 uint
*dst4
= (uint
*) dst
;
1106 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1107 for (j
= 0; j
< width
; j
++) {
1108 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1113 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1115 uint
*dst4
= (uint
*) dst
;
1117 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1118 for (j
= 0; j
< width
; j
++) {
1119 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1124 case PIPE_FORMAT_S8_USCALED
:
1125 assert(usage
== PIPE_TRANSFER_WRITE
);
1126 memcpy(dst
, src
, width
);
1135 /* unmap the stencil buffer */
1136 pipe_transfer_unmap(pipe
, ptDraw
);
1137 pipe
->transfer_destroy(pipe
, ptDraw
);
1142 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1143 GLsizei width
, GLsizei height
,
1144 GLint dstx
, GLint dsty
, GLenum type
)
1146 struct st_context
*st
= st_context(ctx
);
1147 struct pipe_context
*pipe
= st
->pipe
;
1148 struct pipe_screen
*screen
= pipe
->screen
;
1149 struct st_renderbuffer
*rbRead
;
1150 void *driver_vp
, *driver_fp
;
1151 struct pipe_resource
*pt
;
1152 struct pipe_sampler_view
*sv
[2];
1153 int num_sampler_view
= 1;
1155 enum pipe_format srcFormat
, texFormat
;
1156 GLboolean invertTex
= GL_FALSE
;
1157 GLint readX
, readY
, readW
, readH
;
1158 GLuint sample_count
;
1159 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1160 struct st_fp_variant
*fpv
;
1162 st_validate_state(st
);
1164 if (type
== GL_STENCIL
) {
1165 /* can't use texturing to do stencil */
1166 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1171 * Get vertex/fragment shaders
1173 if (type
== GL_COLOR
) {
1174 rbRead
= st_get_color_read_renderbuffer(ctx
);
1177 fpv
= get_color_fp_variant(st
);
1178 driver_fp
= fpv
->driver_shader
;
1180 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1182 if (st
->pixel_xfer
.pixelmap_enabled
) {
1183 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1188 assert(type
== GL_DEPTH
);
1189 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1190 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1192 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1193 driver_fp
= fpv
->driver_shader
;
1195 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1198 /* update fragment program constants */
1199 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1202 if (rbRead
->Base
.Wrapped
)
1203 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1205 sample_count
= rbRead
->texture
->nr_samples
;
1206 /* I believe this would be legal, presumably would need to do a resolve
1207 for color, and for depth/stencil spec says to just use one of the
1208 depth/stencil samples per pixel? Need some transfer clarifications. */
1209 assert(sample_count
< 2);
1211 srcFormat
= rbRead
->texture
->format
;
1213 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1215 PIPE_BIND_SAMPLER_VIEW
, 0)) {
1216 texFormat
= srcFormat
;
1219 /* srcFormat can't be used as a texture format */
1220 if (type
== GL_DEPTH
) {
1221 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1222 st
->internal_target
, sample_count
,
1223 PIPE_BIND_DEPTH_STENCIL
);
1224 assert(texFormat
!= PIPE_FORMAT_NONE
);
1227 /* default color format */
1228 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1229 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1230 assert(texFormat
!= PIPE_FORMAT_NONE
);
1234 /* Invert src region if needed */
1235 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1236 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1237 invertTex
= !invertTex
;
1240 /* Clip the read region against the src buffer bounds.
1241 * We'll still allocate a temporary buffer/texture for the original
1242 * src region size but we'll only read the region which is on-screen.
1243 * This may mean that we draw garbage pixels into the dest region, but
1250 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1251 readW
= MAX2(0, readW
);
1252 readH
= MAX2(0, readH
);
1254 /* alloc temporary texture */
1255 pt
= alloc_texture(st
, width
, height
, texFormat
);
1259 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1261 pipe_resource_reference(&pt
, NULL
);
1265 /* Make temporary texture which is a copy of the src region.
1267 if (srcFormat
== texFormat
) {
1268 struct pipe_box src_box
;
1269 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1270 /* copy source framebuffer surface into mipmap/texture */
1271 pipe
->resource_copy_region(pipe
,
1274 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1275 rbRead
->texture
, /* src tex */
1281 /* CPU-based fallback/conversion */
1282 struct pipe_transfer
*ptRead
=
1283 pipe_get_transfer(st
->pipe
, rbRead
->texture
, 0, 0,
1285 readX
, readY
, readW
, readH
);
1286 struct pipe_transfer
*ptTex
;
1287 enum pipe_transfer_usage transfer_usage
;
1289 if (ST_DEBUG
& DEBUG_FALLBACK
)
1290 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1292 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1293 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1295 transfer_usage
= PIPE_TRANSFER_WRITE
;
1297 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1298 0, 0, width
, height
);
1300 /* copy image from ptRead surface to ptTex surface */
1301 if (type
== GL_COLOR
) {
1302 /* alternate path using get/put_tile() */
1303 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1304 pipe_get_tile_rgba(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1305 pipe_put_tile_rgba(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1311 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1312 pipe_get_tile_z(pipe
, ptRead
, readX
, readY
, readW
, readH
, buf
);
1313 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1318 pipe
->transfer_destroy(pipe
, ptRead
);
1319 pipe
->transfer_destroy(pipe
, ptTex
);
1322 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1323 * textured quad with that texture.
1325 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1326 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1331 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1333 pipe_resource_reference(&pt
, NULL
);
1334 pipe_sampler_view_reference(&sv
[0], NULL
);
1339 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1341 functions
->DrawPixels
= st_DrawPixels
;
1342 functions
->CopyPixels
= st_CopyPixels
;
1347 st_destroy_drawpix(struct st_context
*st
)
1351 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1352 if (st
->drawpix
.shaders
[i
])
1353 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1356 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1357 if (st
->drawpix
.vert_shaders
[0])
1358 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1359 if (st
->drawpix
.vert_shaders
[1])
1360 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1363 #endif /* FEATURE_drawpix */