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 "main/state.h"
40 #include "shader/program.h"
41 #include "shader/prog_parameter.h"
42 #include "shader/prog_print.h"
45 #include "st_context.h"
47 #include "st_atom_constbuf.h"
49 #include "st_program.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_readpixels.h"
52 #include "st_cb_fbo.h"
53 #include "st_cb_texture.h"
55 #include "st_format.h"
56 #include "st_mesa_to_tgsi.h"
57 #include "st_texture.h"
58 #include "st_inlines.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "pipe/p_inlines.h"
63 #include "tgsi/tgsi_ureg.h"
64 #include "util/u_tile.h"
65 #include "util/u_draw_quad.h"
66 #include "util/u_math.h"
67 #include "util/u_rect.h"
68 #include "shader/prog_instruction.h"
69 #include "cso_cache/cso_context.h"
73 * Check if the given program is:
74 * 0: MOVE result.color, fragment.color;
78 is_passthrough_program(const struct gl_fragment_program
*prog
)
80 if (prog
->Base
.NumInstructions
== 2) {
81 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
82 if (inst
[0].Opcode
== OPCODE_MOV
&&
83 inst
[1].Opcode
== OPCODE_END
&&
84 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
85 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
86 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
87 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
88 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
89 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
99 * Make fragment shader for glDraw/CopyPixels. This shader is made
100 * by combining the pixel transfer shader with the user-defined shader.
102 static struct st_fragment_program
*
103 combined_drawpix_fragment_program(GLcontext
*ctx
)
105 struct st_context
*st
= st_context(ctx
);
106 struct st_fragment_program
*stfp
;
108 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
109 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
110 /* the pixel tranfer program has not changed and the user-defined
111 * program has not changed, so re-use the combined program.
113 stfp
= st
->pixel_xfer
.combined_prog
;
116 /* Concatenate the pixel transfer program with the current user-
119 if (is_passthrough_program(&st
->fp
->Base
)) {
120 stfp
= (struct st_fragment_program
*)
121 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
125 printf("Base program:\n");
126 _mesa_print_program(&st
->fp
->Base
.Base
);
127 printf("DrawPix program:\n");
128 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
130 stfp
= (struct st_fragment_program
*)
131 _mesa_combine_programs(ctx
,
132 &st
->pixel_xfer
.program
->Base
.Base
,
138 struct gl_program
*p
= &stfp
->Base
.Base
;
139 printf("Combined DrawPixels program:\n");
140 _mesa_print_program(p
);
141 printf("InputsRead: 0x%x\n", p
->InputsRead
);
142 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
143 _mesa_print_parameter_list(p
->Parameters
);
147 /* translate to TGSI tokens */
148 st_translate_fragment_program(st
, stfp
, NULL
);
150 /* save new program, update serial numbers */
151 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
152 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
153 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
154 /* can't reference new program directly, already have a reference on it */
155 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
156 st
->pixel_xfer
.combined_prog
= stfp
;
159 /* Ideally we'd have updated the pipe constants during the normal
160 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
162 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
169 * Create fragment shader that does a TEX() instruction to get a Z
170 * value, then writes to FRAG_RESULT_DEPTH.
171 * Pass fragment color through as-is.
173 static struct st_fragment_program
*
174 make_fragment_shader_z(struct st_context
*st
)
176 GLcontext
*ctx
= st
->ctx
;
177 struct gl_program
*p
;
180 if (st
->drawpix
.z_shader
) {
181 return st
->drawpix
.z_shader
;
187 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
191 p
->NumInstructions
= 3;
193 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
194 if (!p
->Instructions
) {
195 ctx
->Driver
.DeleteProgram(ctx
, p
);
198 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
200 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
201 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
202 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
203 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
204 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
205 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
206 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
207 p
->Instructions
[ic
].TexSrcUnit
= 0;
208 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
211 /* MOV result.color, fragment.color */
212 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
213 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
214 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
215 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
216 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
220 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
222 assert(ic
== p
->NumInstructions
);
224 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
225 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
226 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
228 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
229 st_translate_fragment_program(st
, st
->drawpix
.z_shader
, NULL
);
231 return st
->drawpix
.z_shader
;
237 * Create a simple vertex shader that just passes through the
238 * vertex position and texcoord (and optionally, color).
241 st_make_passthrough_vertex_shader(struct st_context
*st
,
244 if (!st
->drawpix
.vert_shaders
[passColor
]) {
245 struct ureg_program
*ureg
=
246 ureg_create( TGSI_PROCESSOR_VERTEX
);
251 /* MOV result.pos, vertex.pos; */
253 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
254 ureg_DECL_vs_input( ureg
, 0 ));
256 /* MOV result.texcoord0, vertex.texcoord0; */
258 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
259 ureg_DECL_vs_input( ureg
, 1 ));
262 /* MOV result.color0, vertex.color0; */
264 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
265 ureg_DECL_vs_input( ureg
, 2 ));
270 st
->drawpix
.vert_shaders
[passColor
] =
271 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
274 return st
->drawpix
.vert_shaders
[passColor
];
279 _mesa_base_format(GLenum format
)
282 case GL_DEPTH_COMPONENT
:
283 return GL_DEPTH_COMPONENT
;
284 case GL_DEPTH_STENCIL
:
285 return GL_DEPTH_STENCIL
;
286 case GL_STENCIL_INDEX
:
287 return GL_STENCIL_INDEX
;
295 * Make texture containing an image for glDrawPixels image.
296 * If 'pixels' is NULL, leave the texture image data undefined.
298 static struct pipe_texture
*
299 make_texture(struct st_context
*st
,
300 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
301 const struct gl_pixelstore_attrib
*unpack
,
302 const GLvoid
*pixels
)
304 GLcontext
*ctx
= st
->ctx
;
305 struct pipe_context
*pipe
= st
->pipe
;
306 struct pipe_screen
*screen
= pipe
->screen
;
308 struct pipe_texture
*pt
;
309 enum pipe_format pipeFormat
;
314 baseFormat
= _mesa_base_format(format
);
316 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
319 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
321 cpp
= st_sizeof_format(pipeFormat
);
323 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
327 /* Need to use POT texture? */
330 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
333 l2pt
= util_logbase2(width
);
334 if (1<<l2pt
!= width
) {
337 l2pt
= util_logbase2(height
);
338 if (1<<l2pt
!= height
) {
342 /* Check against maximum texture size */
343 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
344 assert(ptw
<= maxSize
);
345 assert(pth
<= maxSize
);
348 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, ptw
, pth
, 1,
349 PIPE_TEXTURE_USAGE_SAMPLER
);
351 _mesa_unmap_pbo_source(ctx
, unpack
);
356 struct pipe_transfer
*transfer
;
357 static const GLuint dstImageOffsets
= 0;
360 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
362 /* we'll do pixel transfer in a fragment shader */
363 ctx
->_ImageTransferState
= 0x0;
365 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
366 PIPE_TRANSFER_WRITE
, 0, 0,
369 /* map texture transfer */
370 dest
= screen
->transfer_map(screen
, transfer
);
373 /* Put image into texture transfer.
374 * Note that the image is actually going to be upside down in
375 * the texture. We deal with that with texcoords.
377 success
= _mesa_texstore(ctx
, 2, /* dims */
378 baseFormat
, /* baseInternalFormat */
379 mformat
, /* gl_format */
381 0, 0, 0, /* dstX/Y/Zoffset */
382 transfer
->stride
, /* dstRowStride, bytes */
383 &dstImageOffsets
, /* dstImageOffsets */
384 width
, height
, 1, /* size */
385 format
, type
, /* src format/type */
386 pixels
, /* data source */
390 screen
->transfer_unmap(screen
, transfer
);
391 screen
->tex_transfer_destroy(transfer
);
396 ctx
->_ImageTransferState
= imageTransferStateSave
;
399 _mesa_unmap_pbo_source(ctx
, unpack
);
406 * Draw quad with texcoords and optional color.
407 * Coords are window coords with y=0=bottom.
408 * \param color may be null
409 * \param invertTex if true, flip texcoords vertically
412 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
413 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
414 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
416 struct st_context
*st
= st_context(ctx
);
417 struct pipe_context
*pipe
= st
->pipe
;
418 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
420 /* setup vertex data */
422 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
423 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
424 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
425 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
426 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
427 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
428 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
429 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
430 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
431 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
435 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
436 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
439 verts
[1][0][0] = clip_x1
;
440 verts
[1][0][1] = clip_y0
;
443 verts
[2][0][0] = clip_x1
;
444 verts
[2][0][1] = clip_y1
;
447 verts
[3][0][0] = clip_x0
;
448 verts
[3][0][1] = clip_y1
;
451 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
452 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
453 verts
[1][tex
][0] = sRight
;
454 verts
[1][tex
][1] = tTop
;
455 verts
[2][tex
][0] = sRight
;
456 verts
[2][tex
][1] = tBot
;
457 verts
[3][tex
][0] = sLeft
;
458 verts
[3][tex
][1] = tBot
;
460 /* same for all verts: */
462 for (i
= 0; i
< 4; i
++) {
463 verts
[i
][0][2] = z
; /*Z*/
464 verts
[i
][0][3] = 1.0f
; /*W*/
465 verts
[i
][1][0] = color
[0];
466 verts
[i
][1][1] = color
[1];
467 verts
[i
][1][2] = color
[2];
468 verts
[i
][1][3] = color
[3];
469 verts
[i
][2][2] = 0.0f
; /*R*/
470 verts
[i
][2][3] = 1.0f
; /*Q*/
474 for (i
= 0; i
< 4; i
++) {
475 verts
[i
][0][2] = z
; /*Z*/
476 verts
[i
][0][3] = 1.0f
; /*W*/
477 verts
[i
][1][2] = 0.0f
; /*R*/
478 verts
[i
][1][3] = 1.0f
; /*Q*/
484 struct pipe_buffer
*buf
;
486 /* allocate/load buffer object with vertex data */
487 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
489 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
491 util_draw_vertex_buffer(pipe
, buf
, 0,
494 3); /* attribs/vert */
495 pipe_buffer_reference(&buf
, NULL
);
502 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
503 GLsizei width
, GLsizei height
,
504 GLfloat zoomX
, GLfloat zoomY
,
505 struct pipe_texture
*pt
,
508 const GLfloat
*color
,
511 struct st_context
*st
= st_context(ctx
);
512 struct pipe_context
*pipe
= st
->pipe
;
513 struct cso_context
*cso
= st
->cso_context
;
514 GLfloat x0
, y0
, x1
, y1
;
518 /* XXX if DrawPixels image is larger than max texture size, break
521 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
522 assert(width
<= maxSize
);
523 assert(height
<= maxSize
);
525 cso_save_rasterizer(cso
);
526 cso_save_viewport(cso
);
527 cso_save_samplers(cso
);
528 cso_save_sampler_textures(cso
);
529 cso_save_fragment_shader(cso
);
530 cso_save_vertex_shader(cso
);
532 /* rasterizer state: just scissor */
534 struct pipe_rasterizer_state rasterizer
;
535 memset(&rasterizer
, 0, sizeof(rasterizer
));
536 rasterizer
.gl_rasterization_rules
= 1;
537 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
538 cso_set_rasterizer(cso
, &rasterizer
);
541 /* fragment shader state: TEX lookup program */
542 cso_set_fragment_shader_handle(cso
, driver_fp
);
544 /* vertex shader state: position + texcoord pass-through */
545 cso_set_vertex_shader_handle(cso
, driver_vp
);
548 /* texture sampling state: */
550 struct pipe_sampler_state sampler
;
551 memset(&sampler
, 0, sizeof(sampler
));
552 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
553 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
554 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
555 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
556 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
557 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
558 sampler
.normalized_coords
= 1;
560 cso_single_sampler(cso
, 0, &sampler
);
561 if (st
->pixel_xfer
.pixelmap_enabled
) {
562 cso_single_sampler(cso
, 1, &sampler
);
564 cso_single_sampler_done(cso
);
567 /* viewport state: viewport matching window dims */
569 const float w
= (float) ctx
->DrawBuffer
->Width
;
570 const float h
= (float) ctx
->DrawBuffer
->Height
;
571 struct pipe_viewport_state vp
;
572 vp
.scale
[0] = 0.5f
* w
;
573 vp
.scale
[1] = -0.5f
* h
;
576 vp
.translate
[0] = 0.5f
* w
;
577 vp
.translate
[1] = 0.5f
* h
;
578 vp
.translate
[2] = 0.0f
;
579 vp
.translate
[3] = 0.0f
;
580 cso_set_viewport(cso
, &vp
);
584 if (st
->pixel_xfer
.pixelmap_enabled
) {
585 struct pipe_texture
*textures
[2];
587 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
588 pipe
->set_sampler_textures(pipe
, 2, textures
);
591 pipe
->set_sampler_textures(pipe
, 1, &pt
);
594 /* Compute window coords (y=0=bottom) with pixel zoom.
595 * Recall that these coords are transformed by the current
596 * vertex shader and viewport transformation.
599 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
601 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
603 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
604 (GLfloat
) width
/ pt
->width
[0],
605 (GLfloat
) height
/ pt
->height
[0]);
608 cso_restore_rasterizer(cso
);
609 cso_restore_viewport(cso
);
610 cso_restore_samplers(cso
);
611 cso_restore_sampler_textures(cso
);
612 cso_restore_fragment_shader(cso
);
613 cso_restore_vertex_shader(cso
);
618 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
619 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
620 const struct gl_pixelstore_attrib
*unpack
,
621 const GLvoid
*pixels
)
623 struct st_context
*st
= st_context(ctx
);
624 struct pipe_context
*pipe
= st
->pipe
;
625 struct pipe_screen
*screen
= pipe
->screen
;
626 struct st_renderbuffer
*strb
;
627 enum pipe_transfer_usage usage
;
628 struct pipe_transfer
*pt
;
629 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
632 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
635 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
637 /* totally clipped */
642 strb
= st_renderbuffer(ctx
->DrawBuffer
->
643 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
645 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
646 y
= ctx
->DrawBuffer
->Height
- y
- height
;
649 if(format
!= GL_DEPTH_STENCIL
&&
650 pf_get_component_bits( strb
->format
, PIPE_FORMAT_COMP_Z
) != 0)
651 usage
= PIPE_TRANSFER_READ_WRITE
;
653 usage
= PIPE_TRANSFER_WRITE
;
655 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
659 stmap
= screen
->transfer_map(screen
, pt
);
661 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
664 /* if width > MAX_WIDTH, have to process image in chunks */
666 while (skipPixels
< width
) {
667 const GLint spanX
= skipPixels
;
668 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
670 for (row
= 0; row
< height
; row
++) {
671 GLubyte sValues
[MAX_WIDTH
];
672 GLuint zValues
[MAX_WIDTH
];
673 GLenum destType
= GL_UNSIGNED_BYTE
;
674 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
678 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
679 type
, source
, &clippedUnpack
,
680 ctx
->_ImageTransferState
);
682 if (format
== GL_DEPTH_STENCIL
) {
683 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
684 (1 << 24) - 1, type
, source
,
689 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
690 "zoom not complete");
696 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
697 spanY
= height
- row
- 1;
703 /* now pack the stencil (and Z) values in the dest format */
704 switch (pt
->format
) {
705 case PIPE_FORMAT_S8_UNORM
:
707 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
708 assert(usage
== PIPE_TRANSFER_WRITE
);
709 memcpy(dest
, sValues
, spanWidth
);
712 case PIPE_FORMAT_S8Z24_UNORM
:
713 if (format
== GL_DEPTH_STENCIL
) {
714 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
716 assert(usage
== PIPE_TRANSFER_WRITE
);
717 for (k
= 0; k
< spanWidth
; k
++) {
718 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
722 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
724 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
725 for (k
= 0; k
< spanWidth
; k
++) {
726 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
730 case PIPE_FORMAT_Z24S8_UNORM
:
731 if (format
== GL_DEPTH_STENCIL
) {
732 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
734 assert(usage
== PIPE_TRANSFER_WRITE
);
735 for (k
= 0; k
< spanWidth
; k
++) {
736 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
740 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
742 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
743 for (k
= 0; k
< spanWidth
; k
++) {
744 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
753 skipPixels
+= spanWidth
;
756 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
758 /* unmap the stencil buffer */
759 screen
->transfer_unmap(screen
, pt
);
760 screen
->tex_transfer_destroy(pt
);
765 * Called via ctx->Driver.DrawPixels()
768 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
769 GLenum format
, GLenum type
,
770 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
772 struct st_fragment_program
*stfp
;
774 struct st_context
*st
= st_context(ctx
);
775 struct pipe_surface
*ps
;
776 const GLfloat
*color
;
778 if (format
== GL_STENCIL_INDEX
||
779 format
== GL_DEPTH_STENCIL
) {
780 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
785 /* Mesa state should be up to date by now */
786 assert(ctx
->NewState
== 0x0);
788 st_validate_state(st
);
790 if (format
== GL_DEPTH_COMPONENT
) {
791 ps
= st
->state
.framebuffer
.zsbuf
;
792 stfp
= make_fragment_shader_z(st
);
793 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
794 color
= ctx
->Current
.RasterColor
;
797 ps
= st
->state
.framebuffer
.cbufs
[0];
798 stfp
= combined_drawpix_fragment_program(ctx
);
799 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
803 /* draw with textured quad */
805 struct pipe_texture
*pt
806 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
808 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
809 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
814 pipe_texture_reference(&pt
, NULL
);
822 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
823 GLsizei width
, GLsizei height
,
824 GLint dstx
, GLint dsty
)
826 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
827 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
828 enum pipe_transfer_usage usage
;
829 struct pipe_transfer
*ptDraw
;
834 buffer
= _mesa_malloc(width
* height
* sizeof(ubyte
));
836 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
840 /* this will do stencil pixel transfer ops */
841 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
842 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
843 &ctx
->DefaultPacking
, buffer
);
845 if(pf_get_component_bits( rbDraw
->format
, PIPE_FORMAT_COMP_Z
) != 0)
846 usage
= PIPE_TRANSFER_READ_WRITE
;
848 usage
= PIPE_TRANSFER_WRITE
;
850 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
851 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
854 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
855 rbDraw
->texture
, 0, 0, 0,
859 assert(ptDraw
->block
.width
== 1);
860 assert(ptDraw
->block
.height
== 1);
862 /* map the stencil buffer */
863 drawMap
= screen
->transfer_map(screen
, ptDraw
);
866 /* XXX PixelZoom not handled yet */
867 for (i
= 0; i
< height
; i
++) {
874 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
878 dst
= drawMap
+ y
* ptDraw
->stride
;
879 src
= buffer
+ i
* width
;
881 switch (ptDraw
->format
) {
882 case PIPE_FORMAT_S8Z24_UNORM
:
884 uint
*dst4
= (uint
*) dst
;
886 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
887 for (j
= 0; j
< width
; j
++) {
888 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
893 case PIPE_FORMAT_Z24S8_UNORM
:
895 uint
*dst4
= (uint
*) dst
;
897 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
898 for (j
= 0; j
< width
; j
++) {
899 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
904 case PIPE_FORMAT_S8_UNORM
:
905 assert(usage
== PIPE_TRANSFER_WRITE
);
906 memcpy(dst
, src
, width
);
915 /* unmap the stencil buffer */
916 screen
->transfer_unmap(screen
, ptDraw
);
917 screen
->tex_transfer_destroy(ptDraw
);
922 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
923 GLsizei width
, GLsizei height
,
924 GLint dstx
, GLint dsty
, GLenum type
)
926 struct st_context
*st
= st_context(ctx
);
927 struct pipe_context
*pipe
= st
->pipe
;
928 struct pipe_screen
*screen
= pipe
->screen
;
929 struct st_renderbuffer
*rbRead
;
931 struct st_fragment_program
*stfp
;
932 struct pipe_texture
*pt
;
934 enum pipe_format srcFormat
, texFormat
;
937 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
939 st_validate_state(st
);
965 if (width
< 0 || height
< 0)
969 if (type
== GL_STENCIL
) {
970 /* can't use texturing to do stencil */
971 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
975 if (type
== GL_COLOR
) {
976 rbRead
= st_get_color_read_renderbuffer(ctx
);
978 stfp
= combined_drawpix_fragment_program(ctx
);
979 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
982 assert(type
== GL_DEPTH
);
983 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
984 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
985 stfp
= make_fragment_shader_z(st
);
986 driver_vp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
989 srcFormat
= rbRead
->texture
->format
;
991 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
992 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
993 texFormat
= srcFormat
;
996 /* srcFormat can't be used as a texture format */
997 if (type
== GL_DEPTH
) {
998 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1000 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
1001 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
1004 /* default color format */
1005 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
1006 PIPE_TEXTURE_USAGE_SAMPLER
);
1007 assert(texFormat
!= PIPE_FORMAT_NONE
);
1011 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1012 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1023 /* Need to use POT texture? */
1026 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
1029 l2pt
= util_logbase2(width
);
1030 if (1<<l2pt
!= width
) {
1033 l2pt
= util_logbase2(height
);
1034 if (1<<l2pt
!= height
) {
1038 /* Check against maximum texture size */
1039 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1040 assert(ptw
<= maxSize
);
1041 assert(pth
<= maxSize
);
1044 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1046 PIPE_TEXTURE_USAGE_SAMPLER
);
1051 if (srcFormat
== texFormat
) {
1052 /* copy source framebuffer surface into mipmap/texture */
1053 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1054 rbRead
->texture
, 0, 0, 0,
1055 PIPE_BUFFER_USAGE_GPU_READ
);
1056 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1057 PIPE_BUFFER_USAGE_GPU_WRITE
);
1058 if (pipe
->surface_copy
) {
1059 pipe
->surface_copy(pipe
,
1063 srcx
, srcy
, width
, height
);
1065 util_surface_copy(pipe
, FALSE
,
1069 srcx
, srcy
, width
, height
);
1071 pipe_surface_reference(&psRead
, NULL
);
1072 pipe_surface_reference(&psTex
, NULL
);
1075 /* CPU-based fallback/conversion */
1076 struct pipe_transfer
*ptRead
=
1077 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1078 PIPE_TRANSFER_READ
, srcx
, srcy
, width
,
1080 struct pipe_transfer
*ptTex
;
1081 enum pipe_transfer_usage transfer_usage
;
1083 if (ST_DEBUG
& DEBUG_FALLBACK
)
1084 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1086 if (type
== GL_DEPTH
&& pf_is_depth_and_stencil(pt
->format
))
1087 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1089 transfer_usage
= PIPE_TRANSFER_WRITE
;
1091 ptTex
= st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, transfer_usage
,
1092 0, 0, width
, height
);
1094 if (type
== GL_COLOR
) {
1095 /* alternate path using get/put_tile() */
1096 GLfloat
*buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1098 pipe_get_tile_rgba(ptRead
, 0, 0, width
, height
, buf
);
1099 pipe_put_tile_rgba(ptTex
, 0, 0, width
, height
, buf
);
1105 GLuint
*buf
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
1106 pipe_get_tile_z(ptRead
, 0, 0, width
, height
, buf
);
1107 pipe_put_tile_z(ptTex
, 0, 0, width
, height
, buf
);
1111 screen
->tex_transfer_destroy(ptRead
);
1112 screen
->tex_transfer_destroy(ptTex
);
1115 /* draw textured quad */
1116 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1117 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1120 stfp
->driver_shader
,
1123 pipe_texture_reference(&pt
, NULL
);
1128 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1130 functions
->DrawPixels
= st_DrawPixels
;
1131 functions
->CopyPixels
= st_CopyPixels
;
1136 st_destroy_drawpix(struct st_context
*st
)
1138 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1139 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1140 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[0], NULL
);
1141 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[1], NULL
);