1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "main/texstore.h"
39 #include "shader/program.h"
40 #include "shader/prog_print.h"
43 #include "st_context.h"
45 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_format.h"
51 #include "st_texture.h"
52 #include "st_inlines.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "pipe/p_inlines.h"
57 #include "util/u_tile.h"
58 #include "util/u_draw_quad.h"
59 #include "util/u_math.h"
60 #include "util/u_rect.h"
61 #include "shader/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
66 * Check if the given program is:
67 * 0: MOVE result.color, fragment.color;
71 is_passthrough_program(const struct gl_fragment_program
*prog
)
73 if (prog
->Base
.NumInstructions
== 2) {
74 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
75 if (inst
[0].Opcode
== OPCODE_MOV
&&
76 inst
[1].Opcode
== OPCODE_END
&&
77 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
78 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
79 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
80 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
81 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
82 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
92 * Make fragment shader for glDraw/CopyPixels. This shader is made
93 * by combining the pixel transfer shader with the user-defined shader.
95 static struct st_fragment_program
*
96 combined_drawpix_fragment_program(GLcontext
*ctx
)
98 struct st_context
*st
= st_context(ctx
);
99 struct st_fragment_program
*stfp
;
101 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
102 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
103 /* the pixel tranfer program has not changed and the user-defined
104 * program has not changed, so re-use the combined program.
106 stfp
= st
->pixel_xfer
.combined_prog
;
109 /* Concatenate the pixel transfer program with the current user-
112 if (is_passthrough_program(&st
->fp
->Base
)) {
113 stfp
= (struct st_fragment_program
*)
114 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
118 printf("Base program:\n");
119 _mesa_print_program(&st
->fp
->Base
.Base
);
120 printf("DrawPix program:\n");
121 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
123 stfp
= (struct st_fragment_program
*)
124 _mesa_combine_programs(ctx
,
125 &st
->pixel_xfer
.program
->Base
.Base
,
131 struct gl_program
*p
= &stfp
->Base
.Base
;
132 printf("Combined DrawPixels program:\n");
133 _mesa_print_program(p
);
134 printf("InputsRead: 0x%x\n", p
->InputsRead
);
135 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
136 _mesa_print_parameter_list(p
->Parameters
);
140 /* translate to TGSI tokens */
141 st_translate_fragment_program(st
, stfp
, NULL
);
143 /* save new program, update serial numbers */
144 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
145 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
146 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
147 /* can't reference new program directly, already have a reference on it */
148 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
149 st
->pixel_xfer
.combined_prog
= stfp
;
152 /* Ideally we'd have updated the pipe constants during the normal
153 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
155 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
162 * Create fragment shader that does a TEX() instruction to get a Z
163 * value, then writes to FRAG_RESULT_DEPTH.
164 * Pass fragment color through as-is.
166 static struct st_fragment_program
*
167 make_fragment_shader_z(struct st_context
*st
)
169 GLcontext
*ctx
= st
->ctx
;
170 struct gl_program
*p
;
173 if (st
->drawpix
.z_shader
) {
174 return st
->drawpix
.z_shader
;
180 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
184 p
->NumInstructions
= 3;
186 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
187 if (!p
->Instructions
) {
188 ctx
->Driver
.DeleteProgram(ctx
, p
);
191 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
193 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
194 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
195 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
196 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
197 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
198 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
199 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
200 p
->Instructions
[ic
].TexSrcUnit
= 0;
201 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
204 /* MOV result.color, fragment.color */
205 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
206 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
207 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
208 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
209 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
213 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
215 assert(ic
== p
->NumInstructions
);
217 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
218 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
219 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
221 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
222 st_translate_fragment_program(st
, st
->drawpix
.z_shader
, NULL
);
224 return st
->drawpix
.z_shader
;
230 * Create a simple vertex shader that just passes through the
231 * vertex position and texcoord (and optionally, color).
233 static struct st_vertex_program
*
234 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
236 GLcontext
*ctx
= st
->ctx
;
237 struct st_vertex_program
*stvp
;
238 struct gl_program
*p
;
241 if (st
->drawpix
.vert_shaders
[passColor
])
242 return st
->drawpix
.vert_shaders
[passColor
];
247 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
252 p
->NumInstructions
= 4;
254 p
->NumInstructions
= 3;
256 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
257 if (!p
->Instructions
) {
258 ctx
->Driver
.DeleteProgram(ctx
, p
);
261 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
262 /* MOV result.pos, vertex.pos; */
263 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
264 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
265 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
266 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
267 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
268 /* MOV result.texcoord0, vertex.texcoord0; */
269 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
270 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
271 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
272 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
273 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
276 /* MOV result.color0, vertex.color0; */
277 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
278 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
279 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
280 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
281 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
286 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
289 assert(ic
== p
->NumInstructions
);
291 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
292 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
293 (1 << VERT_RESULT_HPOS
));
295 p
->InputsRead
|= VERT_BIT_COLOR0
;
296 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
299 stvp
= (struct st_vertex_program
*) p
;
300 st_translate_vertex_program(st
, stvp
, NULL
, NULL
, NULL
);
302 st
->drawpix
.vert_shaders
[passColor
] = stvp
;
309 _mesa_base_format(GLenum format
)
312 case GL_DEPTH_COMPONENT
:
313 return GL_DEPTH_COMPONENT
;
314 case GL_DEPTH_STENCIL
:
315 return GL_DEPTH_STENCIL
;
316 case GL_STENCIL_INDEX
:
317 return GL_STENCIL_INDEX
;
325 * Make texture containing an image for glDrawPixels image.
326 * If 'pixels' is NULL, leave the texture image data undefined.
328 static struct pipe_texture
*
329 make_texture(struct st_context
*st
,
330 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
331 const struct gl_pixelstore_attrib
*unpack
,
332 const GLvoid
*pixels
)
334 GLcontext
*ctx
= st
->ctx
;
335 struct pipe_context
*pipe
= st
->pipe
;
336 struct pipe_screen
*screen
= pipe
->screen
;
338 struct pipe_texture
*pt
;
339 enum pipe_format pipeFormat
;
344 baseFormat
= _mesa_base_format(format
);
346 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
349 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
351 cpp
= st_sizeof_format(pipeFormat
);
353 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
357 /* Need to use POT texture? */
360 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
363 l2pt
= util_logbase2(width
);
364 if (1<<l2pt
!= width
) {
367 l2pt
= util_logbase2(height
);
368 if (1<<l2pt
!= height
) {
372 /* Check against maximum texture size */
373 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
374 assert(ptw
<= maxSize
);
375 assert(pth
<= maxSize
);
378 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, ptw
, pth
, 1,
379 PIPE_TEXTURE_USAGE_SAMPLER
);
381 _mesa_unmap_pbo_source(ctx
, unpack
);
386 struct pipe_transfer
*transfer
;
387 static const GLuint dstImageOffsets
= 0;
390 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
392 /* we'll do pixel transfer in a fragment shader */
393 ctx
->_ImageTransferState
= 0x0;
395 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
396 PIPE_TRANSFER_WRITE
, 0, 0,
399 /* map texture transfer */
400 dest
= screen
->transfer_map(screen
, transfer
);
403 /* Put image into texture transfer.
404 * Note that the image is actually going to be upside down in
405 * the texture. We deal with that with texcoords.
407 success
= _mesa_texstore(ctx
, 2, /* dims */
408 baseFormat
, /* baseInternalFormat */
409 mformat
, /* gl_format */
411 0, 0, 0, /* dstX/Y/Zoffset */
412 transfer
->stride
, /* dstRowStride, bytes */
413 &dstImageOffsets
, /* dstImageOffsets */
414 width
, height
, 1, /* size */
415 format
, type
, /* src format/type */
416 pixels
, /* data source */
420 screen
->transfer_unmap(screen
, transfer
);
421 screen
->tex_transfer_destroy(transfer
);
426 ctx
->_ImageTransferState
= imageTransferStateSave
;
429 _mesa_unmap_pbo_source(ctx
, unpack
);
436 * Draw quad with texcoords and optional color.
437 * Coords are window coords with y=0=bottom.
438 * \param color may be null
439 * \param invertTex if true, flip texcoords vertically
442 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
443 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
444 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
446 struct st_context
*st
= st_context(ctx
);
447 struct pipe_context
*pipe
= st
->pipe
;
448 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
450 /* setup vertex data */
452 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
453 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
454 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
455 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
456 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
457 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
458 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
459 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
460 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
461 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
465 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
466 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
469 verts
[1][0][0] = clip_x1
;
470 verts
[1][0][1] = clip_y0
;
473 verts
[2][0][0] = clip_x1
;
474 verts
[2][0][1] = clip_y1
;
477 verts
[3][0][0] = clip_x0
;
478 verts
[3][0][1] = clip_y1
;
481 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
482 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
483 verts
[1][tex
][0] = sRight
;
484 verts
[1][tex
][1] = tTop
;
485 verts
[2][tex
][0] = sRight
;
486 verts
[2][tex
][1] = tBot
;
487 verts
[3][tex
][0] = sLeft
;
488 verts
[3][tex
][1] = tBot
;
490 /* same for all verts: */
492 for (i
= 0; i
< 4; i
++) {
493 verts
[i
][0][2] = z
; /*Z*/
494 verts
[i
][0][3] = 1.0f
; /*W*/
495 verts
[i
][1][0] = color
[0];
496 verts
[i
][1][1] = color
[1];
497 verts
[i
][1][2] = color
[2];
498 verts
[i
][1][3] = color
[3];
499 verts
[i
][2][2] = 0.0f
; /*R*/
500 verts
[i
][2][3] = 1.0f
; /*Q*/
504 for (i
= 0; i
< 4; i
++) {
505 verts
[i
][0][2] = z
; /*Z*/
506 verts
[i
][0][3] = 1.0f
; /*W*/
507 verts
[i
][1][2] = 0.0f
; /*R*/
508 verts
[i
][1][3] = 1.0f
; /*Q*/
514 struct pipe_buffer
*buf
;
516 /* allocate/load buffer object with vertex data */
517 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
519 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
521 util_draw_vertex_buffer(pipe
, buf
, 0,
524 3); /* attribs/vert */
525 pipe_buffer_reference(&buf
, NULL
);
532 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
533 GLsizei width
, GLsizei height
,
534 GLfloat zoomX
, GLfloat zoomY
,
535 struct pipe_texture
*pt
,
536 struct st_vertex_program
*stvp
,
537 struct st_fragment_program
*stfp
,
538 const GLfloat
*color
,
541 struct st_context
*st
= st_context(ctx
);
542 struct pipe_context
*pipe
= st
->pipe
;
543 struct cso_context
*cso
= st
->cso_context
;
544 GLfloat x0
, y0
, x1
, y1
;
548 /* XXX if DrawPixels image is larger than max texture size, break
551 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
552 assert(width
<= maxSize
);
553 assert(height
<= maxSize
);
555 cso_save_rasterizer(cso
);
556 cso_save_viewport(cso
);
557 cso_save_samplers(cso
);
558 cso_save_sampler_textures(cso
);
559 cso_save_fragment_shader(cso
);
560 cso_save_vertex_shader(cso
);
562 /* rasterizer state: just scissor */
564 struct pipe_rasterizer_state rasterizer
;
565 memset(&rasterizer
, 0, sizeof(rasterizer
));
566 rasterizer
.gl_rasterization_rules
= 1;
567 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
568 cso_set_rasterizer(cso
, &rasterizer
);
571 /* fragment shader state: TEX lookup program */
572 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
574 /* vertex shader state: position + texcoord pass-through */
575 cso_set_vertex_shader_handle(cso
, stvp
->driver_shader
);
578 /* texture sampling state: */
580 struct pipe_sampler_state sampler
;
581 memset(&sampler
, 0, sizeof(sampler
));
582 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
583 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
584 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
585 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
586 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
587 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
588 sampler
.normalized_coords
= 1;
590 cso_single_sampler(cso
, 0, &sampler
);
591 if (st
->pixel_xfer
.pixelmap_enabled
) {
592 cso_single_sampler(cso
, 1, &sampler
);
594 cso_single_sampler_done(cso
);
597 /* viewport state: viewport matching window dims */
599 const float w
= (float) ctx
->DrawBuffer
->Width
;
600 const float h
= (float) ctx
->DrawBuffer
->Height
;
601 struct pipe_viewport_state vp
;
602 vp
.scale
[0] = 0.5f
* w
;
603 vp
.scale
[1] = -0.5f
* h
;
606 vp
.translate
[0] = 0.5f
* w
;
607 vp
.translate
[1] = 0.5f
* h
;
608 vp
.translate
[2] = 0.0f
;
609 vp
.translate
[3] = 0.0f
;
610 cso_set_viewport(cso
, &vp
);
614 if (st
->pixel_xfer
.pixelmap_enabled
) {
615 struct pipe_texture
*textures
[2];
617 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
618 pipe
->set_sampler_textures(pipe
, 2, textures
);
621 pipe
->set_sampler_textures(pipe
, 1, &pt
);
624 /* Compute window coords (y=0=bottom) with pixel zoom.
625 * Recall that these coords are transformed by the current
626 * vertex shader and viewport transformation.
629 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
631 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
633 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
634 (GLfloat
) width
/ pt
->width
[0],
635 (GLfloat
) height
/ pt
->height
[0]);
638 cso_restore_rasterizer(cso
);
639 cso_restore_viewport(cso
);
640 cso_restore_samplers(cso
);
641 cso_restore_sampler_textures(cso
);
642 cso_restore_fragment_shader(cso
);
643 cso_restore_vertex_shader(cso
);
648 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
649 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
650 const struct gl_pixelstore_attrib
*unpack
,
651 const GLvoid
*pixels
)
653 struct st_context
*st
= st_context(ctx
);
654 struct pipe_context
*pipe
= st
->pipe
;
655 struct pipe_screen
*screen
= pipe
->screen
;
656 struct st_renderbuffer
*strb
;
657 enum pipe_transfer_usage usage
;
658 struct pipe_transfer
*pt
;
659 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
662 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
665 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
667 /* totally clipped */
672 strb
= st_renderbuffer(ctx
->DrawBuffer
->
673 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
675 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
676 y
= ctx
->DrawBuffer
->Height
- y
- height
;
679 if(format
!= GL_DEPTH_STENCIL
&&
680 pf_get_component_bits( strb
->format
, PIPE_FORMAT_COMP_Z
) != 0)
681 usage
= PIPE_TRANSFER_READ_WRITE
;
683 usage
= PIPE_TRANSFER_WRITE
;
685 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
689 stmap
= screen
->transfer_map(screen
, pt
);
691 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
694 /* if width > MAX_WIDTH, have to process image in chunks */
696 while (skipPixels
< width
) {
697 const GLint spanX
= skipPixels
;
698 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
700 for (row
= 0; row
< height
; row
++) {
701 GLubyte sValues
[MAX_WIDTH
];
702 GLuint zValues
[MAX_WIDTH
];
703 GLenum destType
= GL_UNSIGNED_BYTE
;
704 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
708 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
709 type
, source
, &clippedUnpack
,
710 ctx
->_ImageTransferState
);
712 if (format
== GL_DEPTH_STENCIL
) {
713 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
714 (1 << 24) - 1, type
, source
,
719 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
720 "zoom not complete");
726 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
727 spanY
= height
- row
- 1;
733 /* now pack the stencil (and Z) values in the dest format */
734 switch (pt
->format
) {
735 case PIPE_FORMAT_S8_UNORM
:
737 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
738 assert(usage
== PIPE_TRANSFER_WRITE
);
739 memcpy(dest
, sValues
, spanWidth
);
742 case PIPE_FORMAT_S8Z24_UNORM
:
743 if (format
== GL_DEPTH_STENCIL
) {
744 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
746 assert(usage
== PIPE_TRANSFER_WRITE
);
747 for (k
= 0; k
< spanWidth
; k
++) {
748 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
752 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
754 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
755 for (k
= 0; k
< spanWidth
; k
++) {
756 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
760 case PIPE_FORMAT_Z24S8_UNORM
:
761 if (format
== GL_DEPTH_STENCIL
) {
762 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
764 assert(usage
== PIPE_TRANSFER_WRITE
);
765 for (k
= 0; k
< spanWidth
; k
++) {
766 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
770 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
772 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
773 for (k
= 0; k
< spanWidth
; k
++) {
774 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
783 skipPixels
+= spanWidth
;
786 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
788 /* unmap the stencil buffer */
789 screen
->transfer_unmap(screen
, pt
);
790 screen
->tex_transfer_destroy(pt
);
795 * Called via ctx->Driver.DrawPixels()
798 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
799 GLenum format
, GLenum type
,
800 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
802 struct st_fragment_program
*stfp
;
803 struct st_vertex_program
*stvp
;
804 struct st_context
*st
= st_context(ctx
);
805 struct pipe_surface
*ps
;
806 const GLfloat
*color
;
808 if (format
== GL_STENCIL_INDEX
||
809 format
== GL_DEPTH_STENCIL
) {
810 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
815 /* Mesa state should be up to date by now */
816 assert(ctx
->NewState
== 0x0);
818 st_validate_state(st
);
820 if (format
== GL_DEPTH_COMPONENT
) {
821 ps
= st
->state
.framebuffer
.zsbuf
;
822 stfp
= make_fragment_shader_z(st
);
823 stvp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
824 color
= ctx
->Current
.RasterColor
;
827 ps
= st
->state
.framebuffer
.cbufs
[0];
828 stfp
= combined_drawpix_fragment_program(ctx
);
829 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
833 /* draw with textured quad */
835 struct pipe_texture
*pt
836 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
838 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
839 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
840 pt
, stvp
, stfp
, color
, GL_FALSE
);
841 pipe_texture_reference(&pt
, NULL
);
849 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
850 GLsizei width
, GLsizei height
,
851 GLint dstx
, GLint dsty
)
853 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
854 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
855 enum pipe_transfer_usage usage
;
856 struct pipe_transfer
*ptDraw
;
861 buffer
= _mesa_malloc(width
* height
* sizeof(ubyte
));
863 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
867 /* this will do stencil pixel transfer ops */
868 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
869 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
870 &ctx
->DefaultPacking
, buffer
);
872 if(pf_get_component_bits( rbDraw
->format
, PIPE_FORMAT_COMP_Z
) != 0)
873 usage
= PIPE_TRANSFER_READ_WRITE
;
875 usage
= PIPE_TRANSFER_WRITE
;
877 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
878 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
881 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
882 rbDraw
->texture
, 0, 0, 0,
886 assert(ptDraw
->block
.width
== 1);
887 assert(ptDraw
->block
.height
== 1);
889 /* map the stencil buffer */
890 drawMap
= screen
->transfer_map(screen
, ptDraw
);
893 /* XXX PixelZoom not handled yet */
894 for (i
= 0; i
< height
; i
++) {
901 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
905 dst
= drawMap
+ y
* ptDraw
->stride
;
906 src
= buffer
+ i
* width
;
908 switch (ptDraw
->format
) {
909 case PIPE_FORMAT_S8Z24_UNORM
:
911 uint
*dst4
= (uint
*) dst
;
913 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
914 for (j
= 0; j
< width
; j
++) {
915 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
920 case PIPE_FORMAT_Z24S8_UNORM
:
922 uint
*dst4
= (uint
*) dst
;
924 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
925 for (j
= 0; j
< width
; j
++) {
926 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
931 case PIPE_FORMAT_S8_UNORM
:
932 assert(usage
== PIPE_TRANSFER_WRITE
);
933 memcpy(dst
, src
, width
);
942 /* unmap the stencil buffer */
943 screen
->transfer_unmap(screen
, ptDraw
);
944 screen
->tex_transfer_destroy(ptDraw
);
949 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
950 GLsizei width
, GLsizei height
,
951 GLint dstx
, GLint dsty
, GLenum type
)
953 struct st_context
*st
= st_context(ctx
);
954 struct pipe_context
*pipe
= st
->pipe
;
955 struct pipe_screen
*screen
= pipe
->screen
;
956 struct st_renderbuffer
*rbRead
;
957 struct st_vertex_program
*stvp
;
958 struct st_fragment_program
*stfp
;
959 struct pipe_texture
*pt
;
961 enum pipe_format srcFormat
, texFormat
;
964 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
966 st_validate_state(st
);
992 if (width
< 0 || height
< 0)
996 if (type
== GL_STENCIL
) {
997 /* can't use texturing to do stencil */
998 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1002 if (type
== GL_COLOR
) {
1003 rbRead
= st_get_color_read_renderbuffer(ctx
);
1005 stfp
= combined_drawpix_fragment_program(ctx
);
1006 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
1009 assert(type
== GL_DEPTH
);
1010 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1011 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1012 stfp
= make_fragment_shader_z(st
);
1013 stvp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
1016 srcFormat
= rbRead
->texture
->format
;
1018 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
1019 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
1020 texFormat
= srcFormat
;
1023 /* srcFormat can't be used as a texture format */
1024 if (type
== GL_DEPTH
) {
1025 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1027 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
1028 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
1031 /* default color format */
1032 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
1033 PIPE_TEXTURE_USAGE_SAMPLER
);
1034 assert(texFormat
!= PIPE_FORMAT_NONE
);
1038 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1039 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1050 /* Need to use POT texture? */
1053 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
1056 l2pt
= util_logbase2(width
);
1057 if (1<<l2pt
!= width
) {
1060 l2pt
= util_logbase2(height
);
1061 if (1<<l2pt
!= height
) {
1065 /* Check against maximum texture size */
1066 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1067 assert(ptw
<= maxSize
);
1068 assert(pth
<= maxSize
);
1071 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1073 PIPE_TEXTURE_USAGE_SAMPLER
);
1078 if (srcFormat
== texFormat
) {
1079 /* copy source framebuffer surface into mipmap/texture */
1080 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1081 rbRead
->texture
, 0, 0, 0,
1082 PIPE_BUFFER_USAGE_GPU_READ
);
1083 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1084 PIPE_BUFFER_USAGE_GPU_WRITE
);
1085 if (pipe
->surface_copy
) {
1086 pipe
->surface_copy(pipe
,
1090 srcx
, srcy
, width
, height
);
1092 util_surface_copy(pipe
, FALSE
,
1096 srcx
, srcy
, width
, height
);
1098 pipe_surface_reference(&psRead
, NULL
);
1099 pipe_surface_reference(&psTex
, NULL
);
1102 /* CPU-based fallback/conversion */
1103 struct pipe_transfer
*ptRead
=
1104 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1105 PIPE_TRANSFER_READ
, srcx
, srcy
, width
,
1107 struct pipe_transfer
*ptTex
;
1108 enum pipe_transfer_usage transfer_usage
;
1110 if (ST_DEBUG
& DEBUG_FALLBACK
)
1111 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1113 if (type
== GL_DEPTH
&& pf_is_depth_and_stencil(pt
->format
))
1114 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1116 transfer_usage
= PIPE_TRANSFER_WRITE
;
1118 ptTex
= st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, transfer_usage
,
1119 0, 0, width
, height
);
1121 if (type
== GL_COLOR
) {
1122 /* alternate path using get/put_tile() */
1123 GLfloat
*buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1125 pipe_get_tile_rgba(ptRead
, 0, 0, width
, height
, buf
);
1126 pipe_put_tile_rgba(ptTex
, 0, 0, width
, height
, buf
);
1132 GLuint
*buf
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
1133 pipe_get_tile_z(ptRead
, 0, 0, width
, height
, buf
);
1134 pipe_put_tile_z(ptTex
, 0, 0, width
, height
, buf
);
1138 screen
->tex_transfer_destroy(ptRead
);
1139 screen
->tex_transfer_destroy(ptTex
);
1142 /* draw textured quad */
1143 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1144 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1145 pt
, stvp
, stfp
, color
, GL_TRUE
);
1147 pipe_texture_reference(&pt
, NULL
);
1152 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1154 functions
->DrawPixels
= st_DrawPixels
;
1155 functions
->CopyPixels
= st_CopyPixels
;
1160 st_destroy_drawpix(struct st_context
*st
)
1162 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1163 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1164 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[0], NULL
);
1165 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[1], NULL
);