1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "main/texstore.h"
39 #include "shader/program.h"
40 #include "shader/prog_print.h"
43 #include "st_context.h"
45 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
51 #include "st_format.h"
52 #include "st_texture.h"
53 #include "st_inlines.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_defines.h"
57 #include "pipe/p_inlines.h"
58 #include "util/u_tile.h"
59 #include "util/u_draw_quad.h"
60 #include "util/u_math.h"
61 #include "util/u_rect.h"
62 #include "shader/prog_instruction.h"
63 #include "cso_cache/cso_context.h"
67 * Check if the given program is:
68 * 0: MOVE result.color, fragment.color;
72 is_passthrough_program(const struct gl_fragment_program
*prog
)
74 if (prog
->Base
.NumInstructions
== 2) {
75 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
76 if (inst
[0].Opcode
== OPCODE_MOV
&&
77 inst
[1].Opcode
== OPCODE_END
&&
78 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
79 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
80 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
81 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
82 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
83 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
93 * Make fragment shader for glDraw/CopyPixels. This shader is made
94 * by combining the pixel transfer shader with the user-defined shader.
96 static struct st_fragment_program
*
97 combined_drawpix_fragment_program(GLcontext
*ctx
)
99 struct st_context
*st
= st_context(ctx
);
100 struct st_fragment_program
*stfp
;
102 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
103 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
104 /* the pixel tranfer program has not changed and the user-defined
105 * program has not changed, so re-use the combined program.
107 stfp
= st
->pixel_xfer
.combined_prog
;
110 /* Concatenate the pixel transfer program with the current user-
113 if (is_passthrough_program(&st
->fp
->Base
)) {
114 stfp
= (struct st_fragment_program
*)
115 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
119 printf("Base program:\n");
120 _mesa_print_program(&st
->fp
->Base
.Base
);
121 printf("DrawPix program:\n");
122 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
124 stfp
= (struct st_fragment_program
*)
125 _mesa_combine_programs(ctx
,
126 &st
->pixel_xfer
.program
->Base
.Base
,
132 struct gl_program
*p
= &stfp
->Base
.Base
;
133 printf("Combined DrawPixels program:\n");
134 _mesa_print_program(p
);
135 printf("InputsRead: 0x%x\n", p
->InputsRead
);
136 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
137 _mesa_print_parameter_list(p
->Parameters
);
141 /* translate to TGSI tokens */
142 st_translate_fragment_program(st
, stfp
, NULL
);
144 /* save new program, update serial numbers */
145 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
146 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
147 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
148 /* can't reference new program directly, already have a reference on it */
149 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
150 st
->pixel_xfer
.combined_prog
= stfp
;
153 /* Ideally we'd have updated the pipe constants during the normal
154 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
156 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
163 * Create fragment shader that does a TEX() instruction to get a Z
164 * value, then writes to FRAG_RESULT_DEPTH.
165 * Pass fragment color through as-is.
167 static struct st_fragment_program
*
168 make_fragment_shader_z(struct st_context
*st
)
170 GLcontext
*ctx
= st
->ctx
;
171 struct gl_program
*p
;
174 if (st
->drawpix
.z_shader
) {
175 return st
->drawpix
.z_shader
;
181 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
185 p
->NumInstructions
= 3;
187 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
188 if (!p
->Instructions
) {
189 ctx
->Driver
.DeleteProgram(ctx
, p
);
192 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
194 /* TEX result.depth, 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_DEPTH
;
198 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
199 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
200 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
201 p
->Instructions
[ic
].TexSrcUnit
= 0;
202 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
205 /* MOV result.color, fragment.color */
206 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
207 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
208 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
209 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
210 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
214 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
216 assert(ic
== p
->NumInstructions
);
218 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
219 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
220 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
222 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
223 st_translate_fragment_program(st
, st
->drawpix
.z_shader
, NULL
);
225 return st
->drawpix
.z_shader
;
231 * Create a simple vertex shader that just passes through the
232 * vertex position and texcoord (and optionally, color).
234 static struct st_vertex_program
*
235 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
237 GLcontext
*ctx
= st
->ctx
;
238 struct st_vertex_program
*stvp
;
239 struct gl_program
*p
;
242 if (st
->drawpix
.vert_shaders
[passColor
])
243 return st
->drawpix
.vert_shaders
[passColor
];
248 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
253 p
->NumInstructions
= 4;
255 p
->NumInstructions
= 3;
257 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
258 if (!p
->Instructions
) {
259 ctx
->Driver
.DeleteProgram(ctx
, p
);
262 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
263 /* MOV result.pos, vertex.pos; */
264 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
265 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
266 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
267 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
268 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
269 /* MOV result.texcoord0, vertex.texcoord0; */
270 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
271 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
272 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
273 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
274 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
277 /* MOV result.color0, vertex.color0; */
278 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
279 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
280 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
281 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
282 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
287 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
290 assert(ic
== p
->NumInstructions
);
292 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
293 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
294 (1 << VERT_RESULT_HPOS
));
296 p
->InputsRead
|= VERT_BIT_COLOR0
;
297 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
300 stvp
= (struct st_vertex_program
*) p
;
301 st_translate_vertex_program(st
, stvp
, NULL
, NULL
, NULL
);
303 st
->drawpix
.vert_shaders
[passColor
] = stvp
;
310 _mesa_base_format(GLenum format
)
313 case GL_DEPTH_COMPONENT
:
314 return GL_DEPTH_COMPONENT
;
315 case GL_DEPTH_STENCIL
:
316 return GL_DEPTH_STENCIL
;
317 case GL_STENCIL_INDEX
:
318 return GL_STENCIL_INDEX
;
326 * Make texture containing an image for glDrawPixels image.
327 * If 'pixels' is NULL, leave the texture image data undefined.
329 static struct pipe_texture
*
330 make_texture(struct st_context
*st
,
331 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
332 const struct gl_pixelstore_attrib
*unpack
,
333 const GLvoid
*pixels
)
335 GLcontext
*ctx
= st
->ctx
;
336 struct pipe_context
*pipe
= st
->pipe
;
337 struct pipe_screen
*screen
= pipe
->screen
;
339 struct pipe_texture
*pt
;
340 enum pipe_format pipeFormat
;
345 baseFormat
= _mesa_base_format(format
);
347 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
350 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
352 cpp
= st_sizeof_format(pipeFormat
);
354 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
358 /* Need to use POT texture? */
361 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
364 l2pt
= util_logbase2(width
);
365 if (1<<l2pt
!= width
) {
368 l2pt
= util_logbase2(height
);
369 if (1<<l2pt
!= height
) {
373 /* Check against maximum texture size */
374 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
375 assert(ptw
<= maxSize
);
376 assert(pth
<= maxSize
);
379 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, ptw
, pth
, 1,
380 PIPE_TEXTURE_USAGE_SAMPLER
);
382 _mesa_unmap_pbo_source(ctx
, unpack
);
387 struct pipe_transfer
*transfer
;
388 static const GLuint dstImageOffsets
= 0;
391 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
393 /* we'll do pixel transfer in a fragment shader */
394 ctx
->_ImageTransferState
= 0x0;
396 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
397 PIPE_TRANSFER_WRITE
, 0, 0,
400 /* map texture transfer */
401 dest
= screen
->transfer_map(screen
, transfer
);
404 /* Put image into texture transfer.
405 * Note that the image is actually going to be upside down in
406 * the texture. We deal with that with texcoords.
408 success
= _mesa_texstore(ctx
, 2, /* dims */
409 baseFormat
, /* baseInternalFormat */
410 mformat
, /* gl_format */
412 0, 0, 0, /* dstX/Y/Zoffset */
413 transfer
->stride
, /* dstRowStride, bytes */
414 &dstImageOffsets
, /* dstImageOffsets */
415 width
, height
, 1, /* size */
416 format
, type
, /* src format/type */
417 pixels
, /* data source */
421 screen
->transfer_unmap(screen
, transfer
);
422 screen
->tex_transfer_destroy(transfer
);
427 ctx
->_ImageTransferState
= imageTransferStateSave
;
430 _mesa_unmap_pbo_source(ctx
, unpack
);
437 * Draw quad with texcoords and optional color.
438 * Coords are window coords with y=0=bottom.
439 * \param color may be null
440 * \param invertTex if true, flip texcoords vertically
443 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
444 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
445 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
447 struct st_context
*st
= st_context(ctx
);
448 struct pipe_context
*pipe
= st
->pipe
;
449 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
451 /* setup vertex data */
453 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
454 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
455 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
456 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
457 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
458 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
459 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
460 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
461 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
462 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
466 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
467 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
470 verts
[1][0][0] = clip_x1
;
471 verts
[1][0][1] = clip_y0
;
474 verts
[2][0][0] = clip_x1
;
475 verts
[2][0][1] = clip_y1
;
478 verts
[3][0][0] = clip_x0
;
479 verts
[3][0][1] = clip_y1
;
482 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
483 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
484 verts
[1][tex
][0] = sRight
;
485 verts
[1][tex
][1] = tTop
;
486 verts
[2][tex
][0] = sRight
;
487 verts
[2][tex
][1] = tBot
;
488 verts
[3][tex
][0] = sLeft
;
489 verts
[3][tex
][1] = tBot
;
491 /* same for all verts: */
493 for (i
= 0; i
< 4; i
++) {
494 verts
[i
][0][2] = z
; /*Z*/
495 verts
[i
][0][3] = 1.0f
; /*W*/
496 verts
[i
][1][0] = color
[0];
497 verts
[i
][1][1] = color
[1];
498 verts
[i
][1][2] = color
[2];
499 verts
[i
][1][3] = color
[3];
500 verts
[i
][2][2] = 0.0f
; /*R*/
501 verts
[i
][2][3] = 1.0f
; /*Q*/
505 for (i
= 0; i
< 4; i
++) {
506 verts
[i
][0][2] = z
; /*Z*/
507 verts
[i
][0][3] = 1.0f
; /*W*/
508 verts
[i
][1][2] = 0.0f
; /*R*/
509 verts
[i
][1][3] = 1.0f
; /*Q*/
515 struct pipe_buffer
*buf
;
517 /* allocate/load buffer object with vertex data */
518 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
520 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
522 util_draw_vertex_buffer(pipe
, buf
, 0,
525 3); /* attribs/vert */
526 pipe_buffer_reference(&buf
, NULL
);
533 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
534 GLsizei width
, GLsizei height
,
535 GLfloat zoomX
, GLfloat zoomY
,
536 struct pipe_texture
*pt
,
537 struct st_vertex_program
*stvp
,
538 struct st_fragment_program
*stfp
,
539 const GLfloat
*color
,
542 struct st_context
*st
= st_context(ctx
);
543 struct pipe_context
*pipe
= st
->pipe
;
544 struct cso_context
*cso
= st
->cso_context
;
545 GLfloat x0
, y0
, x1
, y1
;
549 /* XXX if DrawPixels image is larger than max texture size, break
552 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
553 assert(width
<= maxSize
);
554 assert(height
<= maxSize
);
556 cso_save_rasterizer(cso
);
557 cso_save_viewport(cso
);
558 cso_save_samplers(cso
);
559 cso_save_sampler_textures(cso
);
560 cso_save_fragment_shader(cso
);
561 cso_save_vertex_shader(cso
);
563 /* rasterizer state: just scissor */
565 struct pipe_rasterizer_state rasterizer
;
566 memset(&rasterizer
, 0, sizeof(rasterizer
));
567 rasterizer
.gl_rasterization_rules
= 1;
568 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
569 cso_set_rasterizer(cso
, &rasterizer
);
572 /* fragment shader state: TEX lookup program */
573 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
575 /* vertex shader state: position + texcoord pass-through */
576 cso_set_vertex_shader_handle(cso
, stvp
->driver_shader
);
579 /* texture sampling state: */
581 struct pipe_sampler_state sampler
;
582 memset(&sampler
, 0, sizeof(sampler
));
583 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
584 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
585 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
586 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
587 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
588 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
589 sampler
.normalized_coords
= 1;
591 cso_single_sampler(cso
, 0, &sampler
);
592 if (st
->pixel_xfer
.pixelmap_enabled
) {
593 cso_single_sampler(cso
, 1, &sampler
);
595 cso_single_sampler_done(cso
);
598 /* viewport state: viewport matching window dims */
600 const float w
= (float) ctx
->DrawBuffer
->Width
;
601 const float h
= (float) ctx
->DrawBuffer
->Height
;
602 struct pipe_viewport_state vp
;
603 vp
.scale
[0] = 0.5f
* w
;
604 vp
.scale
[1] = -0.5f
* h
;
607 vp
.translate
[0] = 0.5f
* w
;
608 vp
.translate
[1] = 0.5f
* h
;
609 vp
.translate
[2] = 0.0f
;
610 vp
.translate
[3] = 0.0f
;
611 cso_set_viewport(cso
, &vp
);
615 if (st
->pixel_xfer
.pixelmap_enabled
) {
616 struct pipe_texture
*textures
[2];
618 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
619 pipe
->set_sampler_textures(pipe
, 2, textures
);
622 pipe
->set_sampler_textures(pipe
, 1, &pt
);
625 /* Compute window coords (y=0=bottom) with pixel zoom.
626 * Recall that these coords are transformed by the current
627 * vertex shader and viewport transformation.
630 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
632 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
634 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
635 (GLfloat
) width
/ pt
->width
[0],
636 (GLfloat
) height
/ pt
->height
[0]);
639 cso_restore_rasterizer(cso
);
640 cso_restore_viewport(cso
);
641 cso_restore_samplers(cso
);
642 cso_restore_sampler_textures(cso
);
643 cso_restore_fragment_shader(cso
);
644 cso_restore_vertex_shader(cso
);
649 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
650 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
651 const struct gl_pixelstore_attrib
*unpack
,
652 const GLvoid
*pixels
)
654 struct st_context
*st
= st_context(ctx
);
655 struct pipe_context
*pipe
= st
->pipe
;
656 struct pipe_screen
*screen
= pipe
->screen
;
657 struct st_renderbuffer
*strb
;
658 enum pipe_transfer_usage usage
;
659 struct pipe_transfer
*pt
;
660 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
663 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
666 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
668 /* totally clipped */
673 strb
= st_renderbuffer(ctx
->DrawBuffer
->
674 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
676 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
677 y
= ctx
->DrawBuffer
->Height
- y
- height
;
680 if(format
!= GL_DEPTH_STENCIL
&&
681 pf_get_component_bits( strb
->format
, PIPE_FORMAT_COMP_Z
) != 0)
682 usage
= PIPE_TRANSFER_READ_WRITE
;
684 usage
= PIPE_TRANSFER_WRITE
;
686 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
690 stmap
= screen
->transfer_map(screen
, pt
);
692 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
695 /* if width > MAX_WIDTH, have to process image in chunks */
697 while (skipPixels
< width
) {
698 const GLint spanX
= skipPixels
;
699 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
701 for (row
= 0; row
< height
; row
++) {
702 GLubyte sValues
[MAX_WIDTH
];
703 GLuint zValues
[MAX_WIDTH
];
704 GLenum destType
= GL_UNSIGNED_BYTE
;
705 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
709 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
710 type
, source
, &clippedUnpack
,
711 ctx
->_ImageTransferState
);
713 if (format
== GL_DEPTH_STENCIL
) {
714 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
715 (1 << 24) - 1, type
, source
,
720 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
721 "zoom not complete");
727 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
728 spanY
= height
- row
- 1;
734 /* now pack the stencil (and Z) values in the dest format */
735 switch (pt
->format
) {
736 case PIPE_FORMAT_S8_UNORM
:
738 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
739 assert(usage
== PIPE_TRANSFER_WRITE
);
740 memcpy(dest
, sValues
, spanWidth
);
743 case PIPE_FORMAT_S8Z24_UNORM
:
744 if (format
== GL_DEPTH_STENCIL
) {
745 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
747 assert(usage
== PIPE_TRANSFER_WRITE
);
748 for (k
= 0; k
< spanWidth
; k
++) {
749 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
753 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
755 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
756 for (k
= 0; k
< spanWidth
; k
++) {
757 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
761 case PIPE_FORMAT_Z24S8_UNORM
:
762 if (format
== GL_DEPTH_STENCIL
) {
763 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
765 assert(usage
== PIPE_TRANSFER_WRITE
);
766 for (k
= 0; k
< spanWidth
; k
++) {
767 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
771 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
773 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
774 for (k
= 0; k
< spanWidth
; k
++) {
775 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
784 skipPixels
+= spanWidth
;
787 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
789 /* unmap the stencil buffer */
790 screen
->transfer_unmap(screen
, pt
);
791 screen
->tex_transfer_destroy(pt
);
796 * Called via ctx->Driver.DrawPixels()
799 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
800 GLenum format
, GLenum type
,
801 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
803 struct st_fragment_program
*stfp
;
804 struct st_vertex_program
*stvp
;
805 struct st_context
*st
= st_context(ctx
);
806 struct pipe_surface
*ps
;
807 const GLfloat
*color
;
809 if (format
== GL_STENCIL_INDEX
||
810 format
== GL_DEPTH_STENCIL
) {
811 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
816 /* Mesa state should be up to date by now */
817 assert(ctx
->NewState
== 0x0);
819 st_validate_state(st
);
821 if (format
== GL_DEPTH_COMPONENT
) {
822 ps
= st
->state
.framebuffer
.zsbuf
;
823 stfp
= make_fragment_shader_z(st
);
824 stvp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
825 color
= ctx
->Current
.RasterColor
;
828 ps
= st
->state
.framebuffer
.cbufs
[0];
829 stfp
= combined_drawpix_fragment_program(ctx
);
830 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
834 /* draw with textured quad */
836 struct pipe_texture
*pt
837 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
839 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
840 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
841 pt
, stvp
, stfp
, color
, GL_FALSE
);
842 pipe_texture_reference(&pt
, NULL
);
850 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
851 GLsizei width
, GLsizei height
,
852 GLint dstx
, GLint dsty
)
854 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
855 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
856 enum pipe_transfer_usage usage
;
857 struct pipe_transfer
*ptDraw
;
862 buffer
= _mesa_malloc(width
* height
* sizeof(ubyte
));
864 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
868 /* this will do stencil pixel transfer ops */
869 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
870 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
871 &ctx
->DefaultPacking
, buffer
);
873 if(pf_get_component_bits( rbDraw
->format
, PIPE_FORMAT_COMP_Z
) != 0)
874 usage
= PIPE_TRANSFER_READ_WRITE
;
876 usage
= PIPE_TRANSFER_WRITE
;
878 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
879 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
882 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
883 rbDraw
->texture
, 0, 0, 0,
887 assert(ptDraw
->block
.width
== 1);
888 assert(ptDraw
->block
.height
== 1);
890 /* map the stencil buffer */
891 drawMap
= screen
->transfer_map(screen
, ptDraw
);
894 /* XXX PixelZoom not handled yet */
895 for (i
= 0; i
< height
; i
++) {
902 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
906 dst
= drawMap
+ y
* ptDraw
->stride
;
907 src
= buffer
+ i
* width
;
909 switch (ptDraw
->format
) {
910 case PIPE_FORMAT_S8Z24_UNORM
:
912 uint
*dst4
= (uint
*) dst
;
914 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
915 for (j
= 0; j
< width
; j
++) {
916 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
921 case PIPE_FORMAT_Z24S8_UNORM
:
923 uint
*dst4
= (uint
*) dst
;
925 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
926 for (j
= 0; j
< width
; j
++) {
927 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
932 case PIPE_FORMAT_S8_UNORM
:
933 assert(usage
== PIPE_TRANSFER_WRITE
);
934 memcpy(dst
, src
, width
);
943 /* unmap the stencil buffer */
944 screen
->transfer_unmap(screen
, ptDraw
);
945 screen
->tex_transfer_destroy(ptDraw
);
950 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
951 GLsizei width
, GLsizei height
,
952 GLint dstx
, GLint dsty
, GLenum type
)
954 struct st_context
*st
= st_context(ctx
);
955 struct pipe_context
*pipe
= st
->pipe
;
956 struct pipe_screen
*screen
= pipe
->screen
;
957 struct st_renderbuffer
*rbRead
;
958 struct st_vertex_program
*stvp
;
959 struct st_fragment_program
*stfp
;
960 struct pipe_texture
*pt
;
962 enum pipe_format srcFormat
, texFormat
;
965 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
967 st_validate_state(st
);
993 if (width
< 0 || height
< 0)
997 if (type
== GL_STENCIL
) {
998 /* can't use texturing to do stencil */
999 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1003 if (type
== GL_COLOR
) {
1004 rbRead
= st_get_color_read_renderbuffer(ctx
);
1006 stfp
= combined_drawpix_fragment_program(ctx
);
1007 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
1010 assert(type
== GL_DEPTH
);
1011 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1012 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1013 stfp
= make_fragment_shader_z(st
);
1014 stvp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
1017 srcFormat
= rbRead
->texture
->format
;
1019 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
1020 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
1021 texFormat
= srcFormat
;
1024 /* srcFormat can't be used as a texture format */
1025 if (type
== GL_DEPTH
) {
1026 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1028 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
1029 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
1032 /* default color format */
1033 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
1034 PIPE_TEXTURE_USAGE_SAMPLER
);
1035 assert(texFormat
!= PIPE_FORMAT_NONE
);
1039 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1040 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1051 /* Need to use POT texture? */
1054 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
1057 l2pt
= util_logbase2(width
);
1058 if (1<<l2pt
!= width
) {
1061 l2pt
= util_logbase2(height
);
1062 if (1<<l2pt
!= height
) {
1066 /* Check against maximum texture size */
1067 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1068 assert(ptw
<= maxSize
);
1069 assert(pth
<= maxSize
);
1072 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1074 PIPE_TEXTURE_USAGE_SAMPLER
);
1079 if (srcFormat
== texFormat
) {
1080 /* copy source framebuffer surface into mipmap/texture */
1081 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1082 rbRead
->texture
, 0, 0, 0,
1083 PIPE_BUFFER_USAGE_GPU_READ
);
1084 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1085 PIPE_BUFFER_USAGE_GPU_WRITE
);
1086 if (pipe
->surface_copy
) {
1087 pipe
->surface_copy(pipe
,
1091 srcx
, srcy
, width
, height
);
1093 util_surface_copy(pipe
, FALSE
,
1097 srcx
, srcy
, width
, height
);
1099 pipe_surface_reference(&psRead
, NULL
);
1100 pipe_surface_reference(&psTex
, NULL
);
1103 /* CPU-based fallback/conversion */
1104 struct pipe_transfer
*ptRead
=
1105 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1106 PIPE_TRANSFER_READ
, srcx
, srcy
, width
,
1108 struct pipe_transfer
*ptTex
;
1109 enum pipe_transfer_usage transfer_usage
;
1111 if (ST_DEBUG
& DEBUG_FALLBACK
)
1112 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1114 if (type
== GL_DEPTH
&& pf_is_depth_and_stencil(pt
->format
))
1115 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1117 transfer_usage
= PIPE_TRANSFER_WRITE
;
1119 ptTex
= st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, transfer_usage
,
1120 0, 0, width
, height
);
1122 if (type
== GL_COLOR
) {
1123 /* alternate path using get/put_tile() */
1124 GLfloat
*buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1126 pipe_get_tile_rgba(ptRead
, 0, 0, width
, height
, buf
);
1127 pipe_put_tile_rgba(ptTex
, 0, 0, width
, height
, buf
);
1133 GLuint
*buf
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
1134 pipe_get_tile_z(ptRead
, 0, 0, width
, height
, buf
);
1135 pipe_put_tile_z(ptTex
, 0, 0, width
, height
, buf
);
1139 screen
->tex_transfer_destroy(ptRead
);
1140 screen
->tex_transfer_destroy(ptTex
);
1143 /* draw textured quad */
1144 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1145 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1146 pt
, stvp
, stfp
, color
, GL_TRUE
);
1148 pipe_texture_reference(&pt
, NULL
);
1153 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1155 functions
->DrawPixels
= st_DrawPixels
;
1156 functions
->CopyPixels
= st_CopyPixels
;
1161 st_destroy_drawpix(struct st_context
*st
)
1163 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1164 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1165 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[0], NULL
);
1166 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[1], NULL
);