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/state.h"
39 #include "shader/program.h"
40 #include "shader/prog_parameter.h"
41 #include "shader/prog_print.h"
43 #include "st_context.h"
45 #include "st_atom_constbuf.h"
47 #include "st_program.h"
48 #include "st_cb_drawpixels.h"
49 #include "st_cb_readpixels.h"
50 #include "st_cb_fbo.h"
51 #include "st_cb_texture.h"
53 #include "st_format.h"
54 #include "st_mesa_to_tgsi.h"
55 #include "st_texture.h"
56 #include "st_inlines.h"
58 #include "pipe/p_context.h"
59 #include "pipe/p_defines.h"
60 #include "pipe/p_inlines.h"
61 #include "util/u_tile.h"
62 #include "util/u_draw_quad.h"
63 #include "shader/prog_instruction.h"
64 #include "cso_cache/cso_context.h"
68 * Check if the given program is:
69 * 0: MOVE result.color, fragment.color;
73 is_passthrough_program(const struct gl_fragment_program
*prog
)
75 if (prog
->Base
.NumInstructions
== 2) {
76 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
77 if (inst
[0].Opcode
== OPCODE_MOV
&&
78 inst
[1].Opcode
== OPCODE_END
&&
79 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
80 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
81 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
82 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
83 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
84 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
94 * Make fragment shader for glDraw/CopyPixels. This shader is made
95 * by combining the pixel transfer shader with the user-defined shader.
97 static struct st_fragment_program
*
98 combined_drawpix_fragment_program(GLcontext
*ctx
)
100 struct st_context
*st
= ctx
->st
;
101 struct st_fragment_program
*stfp
;
103 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
104 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
105 /* the pixel tranfer program has not changed and the user-defined
106 * program has not changed, so re-use the combined program.
108 stfp
= st
->pixel_xfer
.combined_prog
;
111 /* Concatenate the pixel transfer program with the current user-
114 if (is_passthrough_program(&st
->fp
->Base
)) {
115 stfp
= (struct st_fragment_program
*)
116 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
120 printf("Base program:\n");
121 _mesa_print_program(&st
->fp
->Base
.Base
);
122 printf("DrawPix program:\n");
123 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
125 stfp
= (struct st_fragment_program
*)
126 _mesa_combine_programs(ctx
,
127 &st
->pixel_xfer
.program
->Base
.Base
,
133 struct gl_program
*p
= &stfp
->Base
.Base
;
134 printf("Combined DrawPixels program:\n");
135 _mesa_print_program(p
);
136 printf("InputsRead: 0x%x\n", p
->InputsRead
);
137 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
138 _mesa_print_parameter_list(p
->Parameters
);
142 /* translate to TGSI tokens */
143 st_translate_fragment_program(st
, stfp
, NULL
);
145 /* save new program, update serial numbers */
146 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
147 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
148 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
149 /* can't reference new program directly, already have a reference on it */
150 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
151 st
->pixel_xfer
.combined_prog
= stfp
;
154 /* Ideally we'd have updated the pipe constants during the normal
155 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
157 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
164 * Create fragment shader that does a TEX() instruction to get a Z
165 * value, then writes to FRAG_RESULT_DEPTH.
166 * Pass fragment color through as-is.
168 static struct st_fragment_program
*
169 make_fragment_shader_z(struct st_context
*st
)
171 GLcontext
*ctx
= st
->ctx
;
172 struct gl_program
*p
;
175 if (st
->drawpix
.z_shader
) {
176 return st
->drawpix
.z_shader
;
182 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
186 p
->NumInstructions
= 3;
188 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
189 if (!p
->Instructions
) {
190 ctx
->Driver
.DeleteProgram(ctx
, p
);
193 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
195 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
196 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
197 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
198 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
199 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
200 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
201 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
202 p
->Instructions
[ic
].TexSrcUnit
= 0;
203 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
206 /* MOV result.color, fragment.color */
207 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
208 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
209 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
210 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
211 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
215 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
217 assert(ic
== p
->NumInstructions
);
219 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
220 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
221 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
223 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
224 st_translate_fragment_program(st
, st
->drawpix
.z_shader
, NULL
);
226 return st
->drawpix
.z_shader
;
232 * Create a simple vertex shader that just passes through the
233 * vertex position and texcoord (and optionally, color).
235 static struct st_vertex_program
*
236 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
238 GLcontext
*ctx
= st
->ctx
;
239 struct st_vertex_program
*stvp
;
240 struct gl_program
*p
;
243 if (st
->drawpix
.vert_shaders
[passColor
])
244 return st
->drawpix
.vert_shaders
[passColor
];
249 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
254 p
->NumInstructions
= 4;
256 p
->NumInstructions
= 3;
258 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
259 if (!p
->Instructions
) {
260 ctx
->Driver
.DeleteProgram(ctx
, p
);
263 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
264 /* MOV result.pos, vertex.pos; */
265 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
266 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
267 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
268 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
269 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
270 /* MOV result.texcoord0, vertex.texcoord0; */
271 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
272 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
273 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
274 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
275 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
278 /* MOV result.color0, vertex.color0; */
279 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
280 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
281 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
282 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
283 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
288 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
291 assert(ic
== p
->NumInstructions
);
293 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
294 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
295 (1 << VERT_RESULT_HPOS
));
297 p
->InputsRead
|= VERT_BIT_COLOR0
;
298 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
301 stvp
= (struct st_vertex_program
*) p
;
302 st_translate_vertex_program(st
, stvp
, NULL
, NULL
, NULL
);
304 st
->drawpix
.vert_shaders
[passColor
] = stvp
;
311 _mesa_base_format(GLenum format
)
314 case GL_DEPTH_COMPONENT
:
315 return GL_DEPTH_COMPONENT
;
316 case GL_DEPTH_STENCIL
:
317 return GL_DEPTH_STENCIL
;
318 case GL_STENCIL_INDEX
:
319 return GL_STENCIL_INDEX
;
327 * Make texture containing an image for glDrawPixels image.
328 * If 'pixels' is NULL, leave the texture image data undefined.
330 static struct pipe_texture
*
331 make_texture(struct st_context
*st
,
332 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
333 const struct gl_pixelstore_attrib
*unpack
,
334 const GLvoid
*pixels
)
336 GLcontext
*ctx
= st
->ctx
;
337 struct pipe_context
*pipe
= st
->pipe
;
338 struct pipe_screen
*screen
= pipe
->screen
;
339 const struct gl_texture_format
*mformat
;
340 struct pipe_texture
*pt
;
341 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
->MesaFormat
);
352 cpp
= st_sizeof_format(pipeFormat
);
354 pixels
= _mesa_map_drawpix_pbo(ctx
, unpack
, pixels
);
358 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, width
, height
, 1,
359 PIPE_TEXTURE_USAGE_SAMPLER
);
361 _mesa_unmap_drawpix_pbo(ctx
, unpack
);
366 struct pipe_transfer
*transfer
;
367 static const GLuint dstImageOffsets
= 0;
370 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
372 /* we'll do pixel transfer in a fragment shader */
373 ctx
->_ImageTransferState
= 0x0;
375 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
376 PIPE_TRANSFER_WRITE
, 0, 0,
379 /* map texture transfer */
380 dest
= screen
->transfer_map(screen
, transfer
);
382 /* Put image into texture transfer.
383 * Note that the image is actually going to be upside down in
384 * the texture. We deal with that with texcoords.
386 success
= mformat
->StoreImage(ctx
, 2, /* dims */
387 baseFormat
, /* baseInternalFormat */
388 mformat
, /* gl_texture_format */
390 0, 0, 0, /* dstX/Y/Zoffset */
391 transfer
->stride
, /* dstRowStride, bytes */
392 &dstImageOffsets
, /* dstImageOffsets */
393 width
, height
, 1, /* size */
394 format
, type
, /* src format/type */
395 pixels
, /* data source */
399 screen
->transfer_unmap(screen
, transfer
);
400 screen
->tex_transfer_destroy(transfer
);
405 ctx
->_ImageTransferState
= imageTransferStateSave
;
408 _mesa_unmap_drawpix_pbo(ctx
, unpack
);
415 * Draw quad with texcoords and optional color.
416 * Coords are window coords with y=0=bottom.
417 * \param color may be null
418 * \param invertTex if true, flip texcoords vertically
421 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
422 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
425 struct st_context
*st
= ctx
->st
;
426 struct pipe_context
*pipe
= ctx
->st
->pipe
;
427 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
429 /* setup vertex data */
431 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
432 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
433 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
434 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
435 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
436 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
437 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
438 const GLfloat sLeft
= 0.0f
, sRight
= 1.0f
;
439 const GLfloat tTop
= invertTex
, tBot
= 1.0f
- tTop
;
443 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
444 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
447 verts
[1][0][0] = clip_x1
;
448 verts
[1][0][1] = clip_y0
;
451 verts
[2][0][0] = clip_x1
;
452 verts
[2][0][1] = clip_y1
;
455 verts
[3][0][0] = clip_x0
;
456 verts
[3][0][1] = clip_y1
;
459 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
460 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
461 verts
[1][tex
][0] = sRight
;
462 verts
[1][tex
][1] = tTop
;
463 verts
[2][tex
][0] = sRight
;
464 verts
[2][tex
][1] = tBot
;
465 verts
[3][tex
][0] = sLeft
;
466 verts
[3][tex
][1] = tBot
;
468 /* same for all verts: */
470 for (i
= 0; i
< 4; i
++) {
471 verts
[i
][0][2] = z
; /*Z*/
472 verts
[i
][0][3] = 1.0f
; /*W*/
473 verts
[i
][1][0] = color
[0];
474 verts
[i
][1][1] = color
[1];
475 verts
[i
][1][2] = color
[2];
476 verts
[i
][1][3] = color
[3];
477 verts
[i
][2][2] = 0.0f
; /*R*/
478 verts
[i
][2][3] = 1.0f
; /*Q*/
482 for (i
= 0; i
< 4; i
++) {
483 verts
[i
][0][2] = z
; /*Z*/
484 verts
[i
][0][3] = 1.0f
; /*W*/
485 verts
[i
][1][2] = 0.0f
; /*R*/
486 verts
[i
][1][3] = 1.0f
; /*Q*/
492 struct pipe_buffer
*buf
;
494 /* allocate/load buffer object with vertex data */
495 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
497 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
499 util_draw_vertex_buffer(pipe
, buf
, 0,
502 3); /* attribs/vert */
503 pipe_buffer_reference(&buf
, NULL
);
510 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
511 GLsizei width
, GLsizei height
,
512 GLfloat zoomX
, GLfloat zoomY
,
513 struct pipe_texture
*pt
,
514 struct st_vertex_program
*stvp
,
515 struct st_fragment_program
*stfp
,
516 const GLfloat
*color
,
519 struct st_context
*st
= ctx
->st
;
520 struct pipe_context
*pipe
= ctx
->st
->pipe
;
521 struct cso_context
*cso
= ctx
->st
->cso_context
;
522 GLfloat x0
, y0
, x1
, y1
;
526 /* XXX if DrawPixels image is larger than max texture size, break
529 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
530 assert(width
<= maxSize
);
531 assert(height
<= maxSize
);
533 cso_save_rasterizer(cso
);
534 cso_save_viewport(cso
);
535 cso_save_samplers(cso
);
536 cso_save_sampler_textures(cso
);
537 cso_save_fragment_shader(cso
);
538 cso_save_vertex_shader(cso
);
540 /* rasterizer state: just scissor */
542 struct pipe_rasterizer_state rasterizer
;
543 memset(&rasterizer
, 0, sizeof(rasterizer
));
544 rasterizer
.gl_rasterization_rules
= 1;
545 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
546 cso_set_rasterizer(cso
, &rasterizer
);
549 /* fragment shader state: TEX lookup program */
550 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
552 /* vertex shader state: position + texcoord pass-through */
553 cso_set_vertex_shader_handle(cso
, stvp
->driver_shader
);
556 /* texture sampling state: */
558 struct pipe_sampler_state sampler
;
559 memset(&sampler
, 0, sizeof(sampler
));
560 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
561 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
562 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
563 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
564 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
565 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
566 sampler
.normalized_coords
= 1;
568 cso_single_sampler(cso
, 0, &sampler
);
569 if (st
->pixel_xfer
.pixelmap_enabled
) {
570 cso_single_sampler(cso
, 1, &sampler
);
572 cso_single_sampler_done(cso
);
575 /* viewport state: viewport matching window dims */
577 const float width
= (float) ctx
->DrawBuffer
->Width
;
578 const float height
= (float) ctx
->DrawBuffer
->Height
;
579 struct pipe_viewport_state vp
;
580 vp
.scale
[0] = 0.5f
* width
;
581 vp
.scale
[1] = -0.5f
* height
;
584 vp
.translate
[0] = 0.5f
* width
;
585 vp
.translate
[1] = 0.5f
* height
;
586 vp
.translate
[2] = 0.0f
;
587 vp
.translate
[3] = 0.0f
;
588 cso_set_viewport(cso
, &vp
);
592 if (st
->pixel_xfer
.pixelmap_enabled
) {
593 struct pipe_texture
*textures
[2];
595 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
596 pipe
->set_sampler_textures(pipe
, 2, textures
);
599 pipe
->set_sampler_textures(pipe
, 1, &pt
);
602 /* Compute window coords (y=0=bottom) with pixel zoom.
603 * Recall that these coords are transformed by the current
604 * vertex shader and viewport transformation.
607 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
609 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
611 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
614 cso_restore_rasterizer(cso
);
615 cso_restore_viewport(cso
);
616 cso_restore_samplers(cso
);
617 cso_restore_sampler_textures(cso
);
618 cso_restore_fragment_shader(cso
);
619 cso_restore_vertex_shader(cso
);
624 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
625 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
626 const struct gl_pixelstore_attrib
*unpack
,
627 const GLvoid
*pixels
)
629 struct st_context
*st
= ctx
->st
;
630 struct pipe_context
*pipe
= st
->pipe
;
631 struct pipe_screen
*screen
= pipe
->screen
;
632 struct st_renderbuffer
*strb
;
633 struct pipe_transfer
*pt
;
634 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
638 strb
= st_renderbuffer(ctx
->DrawBuffer
->
639 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
641 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
642 y
= ctx
->DrawBuffer
->Height
- y
- height
;
645 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
646 PIPE_TRANSFER_WRITE
, x
, y
,
649 stmap
= screen
->transfer_map(screen
, pt
);
651 pixels
= _mesa_map_drawpix_pbo(ctx
, unpack
, pixels
);
654 /* if width > MAX_WIDTH, have to process image in chunks */
656 while (skipPixels
< width
) {
657 const GLint spanX
= skipPixels
;
658 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
660 for (row
= 0; row
< height
; row
++) {
661 GLubyte sValues
[MAX_WIDTH
];
662 GLuint zValues
[MAX_WIDTH
];
663 GLenum destType
= GL_UNSIGNED_BYTE
;
664 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
668 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
669 type
, source
, unpack
,
670 ctx
->_ImageTransferState
);
672 if (format
== GL_DEPTH_STENCIL
) {
673 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
674 (1 << 24) - 1, type
, source
, unpack
);
678 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
679 "zoom not complete");
685 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
686 spanY
= height
- row
- 1;
692 /* now pack the stencil (and Z) values in the dest format */
693 switch (pt
->format
) {
694 case PIPE_FORMAT_S8_UNORM
:
696 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
697 memcpy(dest
, sValues
, spanWidth
);
700 case PIPE_FORMAT_S8Z24_UNORM
:
701 if (format
== GL_DEPTH_STENCIL
) {
702 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
704 for (k
= 0; k
< spanWidth
; k
++) {
705 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
709 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
711 for (k
= 0; k
< spanWidth
; k
++) {
712 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
716 case PIPE_FORMAT_Z24S8_UNORM
:
717 if (format
== GL_DEPTH_STENCIL
) {
718 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
720 for (k
= 0; k
< spanWidth
; k
++) {
721 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
725 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
727 for (k
= 0; k
< spanWidth
; k
++) {
728 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
737 skipPixels
+= spanWidth
;
740 _mesa_unmap_drawpix_pbo(ctx
, unpack
);
742 /* unmap the stencil buffer */
743 screen
->transfer_unmap(screen
, pt
);
744 screen
->tex_transfer_destroy(pt
);
749 * Called via ctx->Driver.DrawPixels()
752 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
753 GLenum format
, GLenum type
,
754 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
756 struct st_fragment_program
*stfp
;
757 struct st_vertex_program
*stvp
;
758 struct st_context
*st
= ctx
->st
;
759 struct pipe_surface
*ps
;
761 const GLfloat
*color
;
763 if (format
== GL_STENCIL_INDEX
||
764 format
== GL_DEPTH_STENCIL
) {
765 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
770 _mesa_set_vp_override( ctx
, TRUE
);
771 _mesa_update_state( ctx
);
773 st_validate_state(st
);
775 if (format
== GL_DEPTH_COMPONENT
) {
776 ps
= st
->state
.framebuffer
.zsbuf
;
777 stfp
= make_fragment_shader_z(ctx
->st
);
778 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
779 color
= ctx
->Current
.RasterColor
;
782 ps
= st
->state
.framebuffer
.cbufs
[0];
783 stfp
= combined_drawpix_fragment_program(ctx
);
784 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
788 bufferFormat
= ps
->format
;
790 /* draw with textured quad */
792 struct pipe_texture
*pt
793 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
795 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
796 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
797 pt
, stvp
, stfp
, color
, GL_FALSE
);
798 pipe_texture_reference(&pt
, NULL
);
802 _mesa_set_vp_override( ctx
, FALSE
);
808 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
809 GLsizei width
, GLsizei height
,
810 GLint dstx
, GLint dsty
)
812 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
813 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
814 struct pipe_transfer
*ptDraw
;
819 buffer
= _mesa_malloc(width
* height
* sizeof(ubyte
));
821 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
825 /* this will do stencil pixel transfer ops */
826 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
827 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
828 &ctx
->DefaultPacking
, buffer
);
830 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
831 rbDraw
->texture
, 0, 0, 0,
832 PIPE_TRANSFER_WRITE
, dstx
, dsty
,
835 assert(ptDraw
->block
.width
== 1);
836 assert(ptDraw
->block
.height
== 1);
838 /* map the stencil buffer */
839 drawMap
= screen
->transfer_map(screen
, ptDraw
);
842 /* XXX PixelZoom not handled yet */
843 for (i
= 0; i
< height
; i
++) {
850 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
854 dst
= drawMap
+ y
* ptDraw
->stride
;
855 src
= buffer
+ i
* width
;
857 switch (ptDraw
->format
) {
858 case PIPE_FORMAT_S8Z24_UNORM
:
860 uint
*dst4
= (uint
*) dst
;
862 for (j
= 0; j
< width
; j
++) {
863 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
868 case PIPE_FORMAT_Z24S8_UNORM
:
870 uint
*dst4
= (uint
*) dst
;
872 for (j
= 0; j
< width
; j
++) {
873 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
878 case PIPE_FORMAT_S8_UNORM
:
879 memcpy(dst
, src
, width
);
888 /* unmap the stencil buffer */
889 screen
->transfer_unmap(screen
, ptDraw
);
890 screen
->tex_transfer_destroy(ptDraw
);
895 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
896 GLsizei width
, GLsizei height
,
897 GLint dstx
, GLint dsty
, GLenum type
)
899 struct st_context
*st
= ctx
->st
;
900 struct pipe_context
*pipe
= st
->pipe
;
901 struct pipe_screen
*screen
= pipe
->screen
;
902 struct st_renderbuffer
*rbRead
;
903 struct st_vertex_program
*stvp
;
904 struct st_fragment_program
*stfp
;
905 struct pipe_texture
*pt
;
907 enum pipe_format srcFormat
, texFormat
;
909 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
911 st_validate_state(st
);
937 if (width
< 0 || height
< 0)
941 if (type
== GL_STENCIL
) {
942 /* can't use texturing to do stencil */
943 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
947 if (type
== GL_COLOR
) {
948 rbRead
= st_get_color_read_renderbuffer(ctx
);
950 stfp
= combined_drawpix_fragment_program(ctx
);
951 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
954 assert(type
== GL_DEPTH
);
955 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
956 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
957 stfp
= make_fragment_shader_z(ctx
->st
);
958 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
961 srcFormat
= rbRead
->texture
->format
;
963 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
964 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
965 texFormat
= srcFormat
;
968 /* srcFormat can't be used as a texture format */
969 if (type
== GL_DEPTH
) {
970 texFormat
= st_choose_format(pipe
, GL_DEPTH_COMPONENT
, PIPE_TEXTURE_2D
,
971 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
972 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
975 /* default color format */
976 texFormat
= st_choose_format(pipe
, GL_RGBA
, PIPE_TEXTURE_2D
,
977 PIPE_TEXTURE_USAGE_SAMPLER
);
978 assert(texFormat
!= PIPE_FORMAT_NONE
);
982 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
983 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
994 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, texFormat
, 0,
996 PIPE_TEXTURE_USAGE_SAMPLER
);
1001 if (srcFormat
== texFormat
) {
1002 /* copy source framebuffer surface into mipmap/texture */
1003 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1004 rbRead
->texture
, 0, 0, 0,
1005 PIPE_BUFFER_USAGE_GPU_READ
);
1006 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1007 PIPE_BUFFER_USAGE_GPU_WRITE
);
1008 pipe
->surface_copy(pipe
,
1012 srcx
, srcy
, width
, height
);
1013 pipe_surface_reference(&psRead
, NULL
);
1014 pipe_surface_reference(&psTex
, NULL
);
1017 /* CPU-based fallback/conversion */
1018 struct pipe_transfer
*ptRead
=
1019 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1020 PIPE_TRANSFER_READ
, srcx
, srcy
, width
,
1023 struct pipe_transfer
*ptTex
=
1024 st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, PIPE_TRANSFER_WRITE
,
1025 0, 0, width
, height
);
1027 if (type
== GL_COLOR
) {
1028 /* alternate path using get/put_tile() */
1029 GLfloat
*buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1031 pipe_get_tile_rgba(ptRead
, 0, 0, width
, height
, buf
);
1032 pipe_put_tile_rgba(ptTex
, 0, 0, width
, height
, buf
);
1038 GLuint
*buf
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
1039 pipe_get_tile_z(ptRead
, 0, 0, width
, height
, buf
);
1040 pipe_put_tile_z(ptTex
, 0, 0, width
, height
, buf
);
1044 screen
->tex_transfer_destroy(ptRead
);
1045 screen
->tex_transfer_destroy(ptTex
);
1048 /* draw textured quad */
1049 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1050 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1051 pt
, stvp
, stfp
, color
, GL_TRUE
);
1053 pipe_texture_reference(&pt
, NULL
);
1058 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1060 functions
->DrawPixels
= st_DrawPixels
;
1061 functions
->CopyPixels
= st_CopyPixels
;
1066 st_destroy_drawpix(struct st_context
*st
)
1068 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1069 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1070 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[0], NULL
);
1071 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[1], NULL
);