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/mfeatures.h"
38 #include "main/mtypes.h"
39 #include "main/pack.h"
41 #include "main/texformat.h"
42 #include "main/texstore.h"
43 #include "program/program.h"
44 #include "program/prog_print.h"
45 #include "program/prog_instruction.h"
48 #include "st_atom_constbuf.h"
49 #include "st_cb_drawpixels.h"
50 #include "st_cb_readpixels.h"
51 #include "st_cb_fbo.h"
52 #include "st_context.h"
54 #include "st_format.h"
55 #include "st_program.h"
56 #include "st_texture.h"
58 #include "pipe/p_context.h"
59 #include "pipe/p_defines.h"
60 #include "tgsi/tgsi_ureg.h"
61 #include "util/u_draw_quad.h"
62 #include "util/u_format.h"
63 #include "util/u_inlines.h"
64 #include "util/u_math.h"
65 #include "util/u_tile.h"
66 #include "cso_cache/cso_context.h"
72 * Check if the given program is:
73 * 0: MOVE result.color, fragment.color;
77 is_passthrough_program(const struct gl_fragment_program
*prog
)
79 if (prog
->Base
.NumInstructions
== 2) {
80 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
81 if (inst
[0].Opcode
== OPCODE_MOV
&&
82 inst
[1].Opcode
== OPCODE_END
&&
83 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
84 inst
[0].DstReg
.Index
== FRAG_RESULT_COLOR
&&
85 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
86 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
87 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
88 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
98 * Make fragment shader for glDraw/CopyPixels. This shader is made
99 * by combining the pixel transfer shader with the user-defined shader.
100 * \param fpIn the current/incoming fragment program
101 * \param fpOut returns the combined fragment program
104 st_make_drawpix_fragment_program(struct st_context
*st
,
105 struct gl_fragment_program
*fpIn
,
106 struct gl_fragment_program
**fpOut
)
108 struct gl_program
*newProg
;
110 if (is_passthrough_program(fpIn
)) {
111 newProg
= (struct gl_program
*) _mesa_clone_fragment_program(st
->ctx
,
112 &st
->pixel_xfer
.program
->Base
);
117 printf("Base program:\n");
118 _mesa_print_program(&fpIn
->Base
);
119 printf("DrawPix program:\n");
120 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
122 newProg
= _mesa_combine_programs(st
->ctx
,
123 &st
->pixel_xfer
.program
->Base
.Base
,
129 printf("Combined DrawPixels program:\n");
130 _mesa_print_program(newProg
);
131 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
132 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
133 _mesa_print_parameter_list(newProg
->Parameters
);
136 *fpOut
= (struct gl_fragment_program
*) newProg
;
141 * Create fragment program that does a TEX() instruction to get a Z and/or
142 * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
143 * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
144 * Pass fragment color through as-is.
145 * \return pointer to the gl_fragment program
147 struct gl_fragment_program
*
148 st_make_drawpix_z_stencil_program(struct st_context
*st
,
149 GLboolean write_depth
,
150 GLboolean write_stencil
)
152 struct gl_context
*ctx
= st
->ctx
;
153 struct gl_program
*p
;
154 struct gl_fragment_program
*fp
;
156 const GLuint shaderIndex
= write_depth
* 2 + write_stencil
;
158 assert(shaderIndex
< Elements(st
->drawpix
.shaders
));
160 if (st
->drawpix
.shaders
[shaderIndex
]) {
161 /* already have the proper shader */
162 return st
->drawpix
.shaders
[shaderIndex
];
168 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
172 p
->NumInstructions
= write_depth
? 2 : 1;
173 p
->NumInstructions
+= write_stencil
? 1 : 0;
175 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
176 if (!p
->Instructions
) {
177 ctx
->Driver
.DeleteProgram(ctx
, p
);
180 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
183 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
184 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
185 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
186 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
187 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
188 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
189 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
190 p
->Instructions
[ic
].TexSrcUnit
= 0;
191 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
196 /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
197 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
198 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
199 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_STENCIL
;
200 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
201 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
202 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
203 p
->Instructions
[ic
].TexSrcUnit
= 1;
204 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
209 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
211 assert(ic
== p
->NumInstructions
);
213 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
214 p
->OutputsWritten
= 0;
216 p
->OutputsWritten
|= (1 << FRAG_RESULT_DEPTH
);
218 p
->OutputsWritten
|= (1 << FRAG_RESULT_STENCIL
);
220 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
222 p
->SamplersUsed
|= 1 << 1;
224 fp
= (struct gl_fragment_program
*) p
;
226 /* save the new shader */
227 st
->drawpix
.shaders
[shaderIndex
] = fp
;
234 * Create a simple vertex shader that just passes through the
235 * vertex position and texcoord (and optionally, color).
238 make_passthrough_vertex_shader(struct st_context
*st
,
241 if (!st
->drawpix
.vert_shaders
[passColor
]) {
242 struct ureg_program
*ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
247 /* MOV result.pos, vertex.pos; */
249 ureg_DECL_output( ureg
, TGSI_SEMANTIC_POSITION
, 0 ),
250 ureg_DECL_vs_input( ureg
, 0 ));
252 /* MOV result.texcoord0, vertex.attr[1]; */
254 ureg_DECL_output( ureg
, TGSI_SEMANTIC_GENERIC
, 0 ),
255 ureg_DECL_vs_input( ureg
, 1 ));
258 /* MOV result.color0, vertex.attr[2]; */
260 ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, 0 ),
261 ureg_DECL_vs_input( ureg
, 2 ));
266 st
->drawpix
.vert_shaders
[passColor
] =
267 ureg_create_shader_and_destroy( ureg
, st
->pipe
);
270 return st
->drawpix
.vert_shaders
[passColor
];
275 * Return a texture base format for drawing/copying an image
276 * of the given format.
279 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 * Return a texture internalFormat for drawing/copying an image
296 * of the given format and type.
299 internal_format(GLenum format
, GLenum type
)
302 case GL_DEPTH_COMPONENT
:
303 return GL_DEPTH_COMPONENT
;
304 case GL_DEPTH_STENCIL
:
305 return GL_DEPTH_STENCIL
;
306 case GL_STENCIL_INDEX
:
307 return GL_STENCIL_INDEX
;
309 if (_mesa_is_integer_format(format
)) {
313 case GL_UNSIGNED_BYTE
:
317 case GL_UNSIGNED_SHORT
:
321 case GL_UNSIGNED_INT
:
324 assert(0 && "Unexpected type in internal_format()");
325 return GL_RGBA_INTEGER
;
336 * Create a temporary texture to hold an image of the given size.
337 * If width, height are not POT and the driver only handles POT textures,
338 * allocate the next larger size of texture that is POT.
340 static struct pipe_resource
*
341 alloc_texture(struct st_context
*st
, GLsizei width
, GLsizei height
,
342 enum pipe_format texFormat
)
344 struct pipe_resource
*pt
;
346 pt
= st_texture_create(st
, st
->internal_target
, texFormat
, 0,
347 width
, height
, 1, 1, PIPE_BIND_SAMPLER_VIEW
);
354 * Make texture containing an image for glDrawPixels image.
355 * If 'pixels' is NULL, leave the texture image data undefined.
357 static struct pipe_resource
*
358 make_texture(struct st_context
*st
,
359 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
360 const struct gl_pixelstore_attrib
*unpack
,
361 const GLvoid
*pixels
)
363 struct gl_context
*ctx
= st
->ctx
;
364 struct pipe_context
*pipe
= st
->pipe
;
366 struct pipe_resource
*pt
;
367 enum pipe_format pipeFormat
;
369 GLenum baseFormat
, intFormat
;
371 baseFormat
= base_format(format
);
372 intFormat
= internal_format(format
, type
);
374 mformat
= st_ChooseTextureFormat_renderable(ctx
, intFormat
,
375 format
, type
, GL_FALSE
);
378 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
380 cpp
= util_format_get_blocksize(pipeFormat
);
382 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
386 /* alloc temporary texture */
387 pt
= alloc_texture(st
, width
, height
, pipeFormat
);
389 _mesa_unmap_pbo_source(ctx
, unpack
);
394 struct pipe_transfer
*transfer
;
395 static const GLuint dstImageOffsets
= 0;
398 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
400 /* we'll do pixel transfer in a fragment shader */
401 ctx
->_ImageTransferState
= 0x0;
403 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
404 PIPE_TRANSFER_WRITE
, 0, 0,
407 /* map texture transfer */
408 dest
= pipe_transfer_map(pipe
, transfer
);
411 /* Put image into texture transfer.
412 * Note that the image is actually going to be upside down in
413 * the texture. We deal with that with texcoords.
415 success
= _mesa_texstore(ctx
, 2, /* dims */
416 baseFormat
, /* baseInternalFormat */
417 mformat
, /* gl_format */
419 0, 0, 0, /* dstX/Y/Zoffset */
420 transfer
->stride
, /* dstRowStride, bytes */
421 &dstImageOffsets
, /* dstImageOffsets */
422 width
, height
, 1, /* size */
423 format
, type
, /* src format/type */
424 pixels
, /* data source */
428 pipe_transfer_unmap(pipe
, transfer
);
429 pipe
->transfer_destroy(pipe
, transfer
);
434 ctx
->_ImageTransferState
= imageTransferStateSave
;
437 _mesa_unmap_pbo_source(ctx
, unpack
);
444 * Draw quad with texcoords and optional color.
445 * Coords are gallium window coords with y=0=top.
446 * \param color may be null
447 * \param invertTex if true, flip texcoords vertically
450 draw_quad(struct gl_context
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
451 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
452 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
454 struct st_context
*st
= st_context(ctx
);
455 struct pipe_context
*pipe
= st
->pipe
;
456 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
458 /* setup vertex data */
460 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
461 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
462 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
463 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
464 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
465 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
466 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
467 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
468 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
469 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
473 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
474 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
477 verts
[1][0][0] = clip_x1
;
478 verts
[1][0][1] = clip_y0
;
481 verts
[2][0][0] = clip_x1
;
482 verts
[2][0][1] = clip_y1
;
485 verts
[3][0][0] = clip_x0
;
486 verts
[3][0][1] = clip_y1
;
488 verts
[0][1][0] = sLeft
; /* v[0].attr[1].S */
489 verts
[0][1][1] = tTop
; /* v[0].attr[1].T */
490 verts
[1][1][0] = sRight
;
491 verts
[1][1][1] = tTop
;
492 verts
[2][1][0] = sRight
;
493 verts
[2][1][1] = tBot
;
494 verts
[3][1][0] = sLeft
;
495 verts
[3][1][1] = tBot
;
497 /* same for all verts: */
499 for (i
= 0; i
< 4; i
++) {
500 verts
[i
][0][2] = z
; /* v[i].attr[0].z */
501 verts
[i
][0][3] = 1.0f
; /* v[i].attr[0].w */
502 verts
[i
][2][0] = color
[0]; /* v[i].attr[2].r */
503 verts
[i
][2][1] = color
[1]; /* v[i].attr[2].g */
504 verts
[i
][2][2] = color
[2]; /* v[i].attr[2].b */
505 verts
[i
][2][3] = color
[3]; /* v[i].attr[2].a */
506 verts
[i
][1][2] = 0.0f
; /* v[i].attr[1].R */
507 verts
[i
][1][3] = 1.0f
; /* v[i].attr[1].Q */
511 for (i
= 0; i
< 4; i
++) {
512 verts
[i
][0][2] = z
; /*Z*/
513 verts
[i
][0][3] = 1.0f
; /*W*/
514 verts
[i
][1][2] = 0.0f
; /*R*/
515 verts
[i
][1][3] = 1.0f
; /*Q*/
521 struct pipe_resource
*buf
;
523 /* allocate/load buffer object with vertex data */
524 buf
= pipe_buffer_create(pipe
->screen
,
525 PIPE_BIND_VERTEX_BUFFER
,
528 pipe_buffer_write(st
->pipe
, buf
, 0, sizeof(verts
), verts
);
530 util_draw_vertex_buffer(pipe
, st
->cso_context
, buf
, 0,
533 3); /* attribs/vert */
534 pipe_resource_reference(&buf
, NULL
);
541 draw_textured_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
542 GLsizei width
, GLsizei height
,
543 GLfloat zoomX
, GLfloat zoomY
,
544 struct pipe_sampler_view
**sv
,
545 int num_sampler_view
,
548 const GLfloat
*color
,
550 GLboolean write_depth
, GLboolean write_stencil
)
552 struct st_context
*st
= st_context(ctx
);
553 struct pipe_context
*pipe
= st
->pipe
;
554 struct cso_context
*cso
= st
->cso_context
;
555 GLfloat x0
, y0
, x1
, y1
;
557 boolean normalized
= sv
[0]->texture
->target
!= PIPE_TEXTURE_RECT
;
560 /* XXX if DrawPixels image is larger than max texture size, break
563 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
564 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
565 assert(width
<= maxSize
);
566 assert(height
<= maxSize
);
568 cso_save_rasterizer(cso
);
569 cso_save_viewport(cso
);
570 cso_save_samplers(cso
);
571 cso_save_fragment_sampler_views(cso
);
572 cso_save_fragment_shader(cso
);
573 cso_save_vertex_shader(cso
);
574 cso_save_vertex_elements(cso
);
575 cso_save_vertex_buffers(cso
);
577 cso_save_depth_stencil_alpha(cso
);
581 /* rasterizer state: just scissor */
583 struct pipe_rasterizer_state rasterizer
;
584 memset(&rasterizer
, 0, sizeof(rasterizer
));
585 rasterizer
.gl_rasterization_rules
= 1;
586 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
587 cso_set_rasterizer(cso
, &rasterizer
);
591 /* Stencil writing bypasses the normal fragment pipeline to
592 * disable color writing and set stencil test to always pass.
594 struct pipe_depth_stencil_alpha_state dsa
;
595 struct pipe_blend_state blend
;
598 memset(&dsa
, 0, sizeof(dsa
));
599 dsa
.stencil
[0].enabled
= 1;
600 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
601 dsa
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
602 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
604 /* writing depth+stencil: depth test always passes */
605 dsa
.depth
.enabled
= 1;
606 dsa
.depth
.writemask
= ctx
->Depth
.Mask
;
607 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
609 cso_set_depth_stencil_alpha(cso
, &dsa
);
611 /* blend (colormask) */
612 memset(&blend
, 0, sizeof(blend
));
613 cso_set_blend(cso
, &blend
);
616 /* fragment shader state: TEX lookup program */
617 cso_set_fragment_shader_handle(cso
, driver_fp
);
619 /* vertex shader state: position + texcoord pass-through */
620 cso_set_vertex_shader_handle(cso
, driver_vp
);
623 /* texture sampling state: */
625 struct pipe_sampler_state sampler
;
626 memset(&sampler
, 0, sizeof(sampler
));
627 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
628 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
629 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
630 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
631 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
632 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
633 sampler
.normalized_coords
= normalized
;
635 cso_single_sampler(cso
, 0, &sampler
);
636 if (num_sampler_view
> 1) {
637 cso_single_sampler(cso
, 1, &sampler
);
639 cso_single_sampler_done(cso
);
642 /* viewport state: viewport matching window dims */
644 const float w
= (float) ctx
->DrawBuffer
->Width
;
645 const float h
= (float) ctx
->DrawBuffer
->Height
;
646 struct pipe_viewport_state vp
;
647 vp
.scale
[0] = 0.5f
* w
;
648 vp
.scale
[1] = -0.5f
* h
;
651 vp
.translate
[0] = 0.5f
* w
;
652 vp
.translate
[1] = 0.5f
* h
;
653 vp
.translate
[2] = 0.5f
;
654 vp
.translate
[3] = 0.0f
;
655 cso_set_viewport(cso
, &vp
);
658 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
661 cso_set_fragment_sampler_views(cso
, num_sampler_view
, sv
);
663 /* Compute Gallium window coords (y=0=top) with pixel zoom.
664 * Recall that these coords are transformed by the current
665 * vertex shader and viewport transformation.
667 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_BOTTOM
) {
668 y
= ctx
->DrawBuffer
->Height
- (int) (y
+ height
* ctx
->Pixel
.ZoomY
);
669 invertTex
= !invertTex
;
673 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
675 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
677 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
680 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
681 normalized
? ((GLfloat
) width
/ sv
[0]->texture
->width0
) : (GLfloat
)width
,
682 normalized
? ((GLfloat
) height
/ sv
[0]->texture
->height0
) : (GLfloat
)height
);
685 cso_restore_rasterizer(cso
);
686 cso_restore_viewport(cso
);
687 cso_restore_samplers(cso
);
688 cso_restore_fragment_sampler_views(cso
);
689 cso_restore_fragment_shader(cso
);
690 cso_restore_vertex_shader(cso
);
691 cso_restore_vertex_elements(cso
);
692 cso_restore_vertex_buffers(cso
);
694 cso_restore_depth_stencil_alpha(cso
);
695 cso_restore_blend(cso
);
701 * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
702 * can't use a fragment shader to write stencil values.
705 draw_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
706 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
707 const struct gl_pixelstore_attrib
*unpack
,
708 const GLvoid
*pixels
)
710 struct st_context
*st
= st_context(ctx
);
711 struct pipe_context
*pipe
= st
->pipe
;
712 struct st_renderbuffer
*strb
;
713 enum pipe_transfer_usage usage
;
714 struct pipe_transfer
*pt
;
715 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
718 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
721 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
723 /* totally clipped */
728 strb
= st_renderbuffer(ctx
->DrawBuffer
->
729 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
731 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
732 y
= ctx
->DrawBuffer
->Height
- y
- height
;
735 if(format
!= GL_DEPTH_STENCIL
&&
736 util_format_get_component_bits(strb
->format
,
737 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
738 usage
= PIPE_TRANSFER_READ_WRITE
;
740 usage
= PIPE_TRANSFER_WRITE
;
742 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
, 0, 0,
746 stmap
= pipe_transfer_map(pipe
, pt
);
748 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
751 /* if width > MAX_WIDTH, have to process image in chunks */
753 while (skipPixels
< width
) {
754 const GLint spanX
= skipPixels
;
755 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
757 for (row
= 0; row
< height
; row
++) {
758 GLubyte sValues
[MAX_WIDTH
];
759 GLuint zValues
[MAX_WIDTH
];
760 GLenum destType
= GL_UNSIGNED_BYTE
;
761 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
765 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
766 type
, source
, &clippedUnpack
,
767 ctx
->_ImageTransferState
);
769 if (format
== GL_DEPTH_STENCIL
) {
770 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
771 (1 << 24) - 1, type
, source
,
776 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
777 "zoom not complete");
783 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
784 spanY
= height
- row
- 1;
790 /* now pack the stencil (and Z) values in the dest format */
791 switch (pt
->resource
->format
) {
792 case PIPE_FORMAT_S8_USCALED
:
794 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
795 assert(usage
== PIPE_TRANSFER_WRITE
);
796 memcpy(dest
, sValues
, spanWidth
);
799 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
800 if (format
== GL_DEPTH_STENCIL
) {
801 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
803 assert(usage
== PIPE_TRANSFER_WRITE
);
804 for (k
= 0; k
< spanWidth
; k
++) {
805 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
809 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
811 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
812 for (k
= 0; k
< spanWidth
; k
++) {
813 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
817 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
818 if (format
== GL_DEPTH_STENCIL
) {
819 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
821 assert(usage
== PIPE_TRANSFER_WRITE
);
822 for (k
= 0; k
< spanWidth
; k
++) {
823 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
827 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
829 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
830 for (k
= 0; k
< spanWidth
; k
++) {
831 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
840 skipPixels
+= spanWidth
;
843 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
845 /* unmap the stencil buffer */
846 pipe_transfer_unmap(pipe
, pt
);
847 pipe
->transfer_destroy(pipe
, pt
);
852 * Get fragment program variant for a glDrawPixels or glCopyPixels
853 * command for RGBA data.
855 static struct st_fp_variant
*
856 get_color_fp_variant(struct st_context
*st
)
858 struct gl_context
*ctx
= st
->ctx
;
859 struct st_fp_variant_key key
;
860 struct st_fp_variant
*fpv
;
862 memset(&key
, 0, sizeof(key
));
866 key
.scaleAndBias
= (ctx
->Pixel
.RedBias
!= 0.0 ||
867 ctx
->Pixel
.RedScale
!= 1.0 ||
868 ctx
->Pixel
.GreenBias
!= 0.0 ||
869 ctx
->Pixel
.GreenScale
!= 1.0 ||
870 ctx
->Pixel
.BlueBias
!= 0.0 ||
871 ctx
->Pixel
.BlueScale
!= 1.0 ||
872 ctx
->Pixel
.AlphaBias
!= 0.0 ||
873 ctx
->Pixel
.AlphaScale
!= 1.0);
874 key
.pixelMaps
= ctx
->Pixel
.MapColorFlag
;
876 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
883 * Get fragment program variant for a glDrawPixels or glCopyPixels
884 * command for depth/stencil data.
886 static struct st_fp_variant
*
887 get_depth_stencil_fp_variant(struct st_context
*st
, GLboolean write_depth
,
888 GLboolean write_stencil
)
890 struct st_fp_variant_key key
;
891 struct st_fp_variant
*fpv
;
893 memset(&key
, 0, sizeof(key
));
897 key
.drawpixels_z
= write_depth
;
898 key
.drawpixels_stencil
= write_stencil
;
900 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
907 * Called via ctx->Driver.DrawPixels()
910 st_DrawPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
911 GLsizei width
, GLsizei height
,
912 GLenum format
, GLenum type
,
913 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
915 void *driver_vp
, *driver_fp
;
916 struct st_context
*st
= st_context(ctx
);
917 const GLfloat
*color
;
918 struct pipe_context
*pipe
= st
->pipe
;
919 GLboolean write_stencil
= GL_FALSE
, write_depth
= GL_FALSE
;
920 struct pipe_sampler_view
*sv
[2];
921 int num_sampler_view
= 1;
922 enum pipe_format stencil_format
= PIPE_FORMAT_NONE
;
923 struct st_fp_variant
*fpv
;
925 if (format
== GL_DEPTH_STENCIL
)
926 write_stencil
= write_depth
= GL_TRUE
;
927 else if (format
== GL_STENCIL_INDEX
)
928 write_stencil
= GL_TRUE
;
929 else if (format
== GL_DEPTH_COMPONENT
)
930 write_depth
= GL_TRUE
;
933 enum pipe_format tex_format
;
934 /* can we write to stencil if not fallback */
935 if (!pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
))
936 goto stencil_fallback
;
938 tex_format
= st_choose_format(st
->pipe
->screen
, base_format(format
),
940 0, PIPE_BIND_SAMPLER_VIEW
);
941 if (tex_format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
)
942 stencil_format
= PIPE_FORMAT_X24S8_USCALED
;
943 else if (tex_format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
)
944 stencil_format
= PIPE_FORMAT_S8X24_USCALED
;
946 stencil_format
= PIPE_FORMAT_S8_USCALED
;
947 if (stencil_format
== PIPE_FORMAT_NONE
)
948 goto stencil_fallback
;
951 /* Mesa state should be up to date by now */
952 assert(ctx
->NewState
== 0x0);
954 st_validate_state(st
);
957 * Get vertex/fragment shaders
959 if (write_depth
|| write_stencil
) {
960 fpv
= get_depth_stencil_fp_variant(st
, write_depth
, write_stencil
);
962 driver_fp
= fpv
->driver_shader
;
964 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
966 color
= ctx
->Current
.RasterColor
;
969 fpv
= get_color_fp_variant(st
);
971 driver_fp
= fpv
->driver_shader
;
973 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
976 if (st
->pixel_xfer
.pixelmap_enabled
) {
977 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
982 /* update fragment program constants */
983 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
985 /* draw with textured quad */
987 struct pipe_resource
*pt
988 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
990 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
994 sv
[1] = st_create_texture_sampler_view_format(st
->pipe
, pt
,
999 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1001 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1006 color
, GL_FALSE
, write_depth
, write_stencil
);
1007 pipe_sampler_view_reference(&sv
[0], NULL
);
1008 if (num_sampler_view
> 1)
1009 pipe_sampler_view_reference(&sv
[1], NULL
);
1011 pipe_resource_reference(&pt
, NULL
);
1017 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
1024 * Software fallback for glCopyPixels(GL_STENCIL).
1027 copy_stencil_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1028 GLsizei width
, GLsizei height
,
1029 GLint dstx
, GLint dsty
)
1031 struct st_renderbuffer
*rbDraw
;
1032 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
1033 enum pipe_transfer_usage usage
;
1034 struct pipe_transfer
*ptDraw
;
1039 buffer
= malloc(width
* height
* sizeof(ubyte
));
1041 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1045 /* Get the dest renderbuffer. If there's a wrapper, use the
1046 * underlying renderbuffer.
1048 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1049 if (rbDraw
->Base
.Wrapped
)
1050 rbDraw
= st_renderbuffer(rbDraw
->Base
.Wrapped
);
1052 /* this will do stencil pixel transfer ops */
1053 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
1054 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
1055 &ctx
->DefaultPacking
, buffer
);
1058 /* debug code: dump stencil values */
1060 for (row
= 0; row
< height
; row
++) {
1061 printf("%3d: ", row
);
1062 for (col
= 0; col
< width
; col
++) {
1063 printf("%02x ", buffer
[col
+ row
* width
]);
1069 if (util_format_get_component_bits(rbDraw
->format
,
1070 UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0)
1071 usage
= PIPE_TRANSFER_READ_WRITE
;
1073 usage
= PIPE_TRANSFER_WRITE
;
1075 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1076 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
1079 ptDraw
= pipe_get_transfer(st_context(ctx
)->pipe
,
1080 rbDraw
->texture
, 0, 0,
1084 assert(util_format_get_blockwidth(ptDraw
->resource
->format
) == 1);
1085 assert(util_format_get_blockheight(ptDraw
->resource
->format
) == 1);
1087 /* map the stencil buffer */
1088 drawMap
= pipe_transfer_map(pipe
, ptDraw
);
1091 /* XXX PixelZoom not handled yet */
1092 for (i
= 0; i
< height
; i
++) {
1099 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1103 dst
= drawMap
+ y
* ptDraw
->stride
;
1104 src
= buffer
+ i
* width
;
1106 switch (ptDraw
->resource
->format
) {
1107 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
1109 uint
*dst4
= (uint
*) dst
;
1111 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1112 for (j
= 0; j
< width
; j
++) {
1113 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1118 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
1120 uint
*dst4
= (uint
*) dst
;
1122 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
1123 for (j
= 0; j
< width
; j
++) {
1124 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
1129 case PIPE_FORMAT_S8_USCALED
:
1130 assert(usage
== PIPE_TRANSFER_WRITE
);
1131 memcpy(dst
, src
, width
);
1140 /* unmap the stencil buffer */
1141 pipe_transfer_unmap(pipe
, ptDraw
);
1142 pipe
->transfer_destroy(pipe
, ptDraw
);
1146 /** Do the src/dest regions overlap? */
1148 regions_overlap(GLint srcX
, GLint srcY
, GLint dstX
, GLint dstY
,
1149 GLsizei width
, GLsizei height
)
1151 if (srcX
+ width
<= dstX
||
1152 dstX
+ width
<= srcX
||
1153 srcY
+ height
<= dstY
||
1154 dstY
+ height
<= srcY
)
1162 * Try to do a glCopyPixels for simple cases with a blit by calling
1163 * pipe->resource_copy_region().
1165 * We can do this when we're copying color pixels (depth/stencil
1166 * eventually) with no pixel zoom, no pixel transfer ops, no
1167 * per-fragment ops, the src/dest regions don't overlap and the
1168 * src/dest pixel formats are the same.
1171 blit_copy_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1172 GLsizei width
, GLsizei height
,
1173 GLint dstx
, GLint dsty
, GLenum type
)
1175 struct st_context
*st
= st_context(ctx
);
1176 struct pipe_context
*pipe
= st
->pipe
;
1177 struct gl_pixelstore_attrib pack
, unpack
;
1178 GLint readX
, readY
, readW
, readH
;
1180 if (type
== GL_COLOR
&&
1181 ctx
->Pixel
.ZoomX
== 1.0 &&
1182 ctx
->Pixel
.ZoomY
== 1.0 &&
1183 ctx
->_ImageTransferState
== 0x0 &&
1184 !ctx
->Color
.BlendEnabled
&&
1185 !ctx
->Color
.AlphaEnabled
&&
1187 !ctx
->Fog
.Enabled
&&
1188 !ctx
->Stencil
.Enabled
&&
1189 !ctx
->FragmentProgram
.Enabled
&&
1190 !ctx
->VertexProgram
.Enabled
&&
1191 !ctx
->Shader
.CurrentFragmentProgram
&&
1192 st_fb_orientation(ctx
->ReadBuffer
) == st_fb_orientation(ctx
->DrawBuffer
) &&
1193 ctx
->DrawBuffer
->_NumColorDrawBuffers
== 1) {
1194 struct st_renderbuffer
*rbRead
, *rbDraw
;
1198 * Clip the read region against the src buffer bounds.
1199 * We'll still allocate a temporary buffer/texture for the original
1200 * src region size but we'll only read the region which is on-screen.
1201 * This may mean that we draw garbage pixels into the dest region, but
1208 pack
= ctx
->DefaultPacking
;
1209 if (!_mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
))
1210 return GL_TRUE
; /* all done */
1212 /* clip against dest buffer bounds and scissor box */
1213 drawX
= dstx
+ pack
.SkipPixels
;
1214 drawY
= dsty
+ pack
.SkipRows
;
1216 if (!_mesa_clip_drawpixels(ctx
, &drawX
, &drawY
, &readW
, &readH
, &unpack
))
1217 return GL_TRUE
; /* all done */
1219 readX
= readX
- pack
.SkipPixels
+ unpack
.SkipPixels
;
1220 readY
= readY
- pack
.SkipRows
+ unpack
.SkipRows
;
1222 rbRead
= st_get_color_read_renderbuffer(ctx
);
1223 rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
1225 if ((rbRead
!= rbDraw
||
1226 !regions_overlap(readX
, readY
, drawX
, drawY
, readW
, readH
)) &&
1227 rbRead
->Base
.Format
== rbDraw
->Base
.Format
) {
1228 struct pipe_box srcBox
;
1230 /* flip src/dst position if needed */
1231 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1232 /* both buffers will have the same orientation */
1233 readY
= ctx
->ReadBuffer
->Height
- readY
- readH
;
1234 drawY
= ctx
->DrawBuffer
->Height
- drawY
- readH
;
1237 u_box_2d(readX
, readY
, readW
, readH
, &srcBox
);
1239 pipe
->resource_copy_region(pipe
,
1240 rbDraw
->texture
, 0, drawX
, drawY
, 0,
1241 rbRead
->texture
, 0, &srcBox
);
1251 st_CopyPixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
1252 GLsizei width
, GLsizei height
,
1253 GLint dstx
, GLint dsty
, GLenum type
)
1255 struct st_context
*st
= st_context(ctx
);
1256 struct pipe_context
*pipe
= st
->pipe
;
1257 struct pipe_screen
*screen
= pipe
->screen
;
1258 struct st_renderbuffer
*rbRead
;
1259 void *driver_vp
, *driver_fp
;
1260 struct pipe_resource
*pt
;
1261 struct pipe_sampler_view
*sv
[2];
1262 int num_sampler_view
= 1;
1264 enum pipe_format srcFormat
, texFormat
;
1265 GLboolean invertTex
= GL_FALSE
;
1266 GLint readX
, readY
, readW
, readH
;
1267 GLuint sample_count
;
1268 struct gl_pixelstore_attrib pack
= ctx
->DefaultPacking
;
1269 struct st_fp_variant
*fpv
;
1271 st_validate_state(st
);
1273 if (type
== GL_STENCIL
) {
1274 /* can't use texturing to do stencil */
1275 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1279 if (blit_copy_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
, type
))
1283 * The subsequent code implements glCopyPixels by copying the source
1284 * pixels into a temporary texture that's then applied to a textured quad.
1285 * When we draw the textured quad, all the usual per-fragment operations
1291 * Get vertex/fragment shaders
1293 if (type
== GL_COLOR
) {
1294 rbRead
= st_get_color_read_renderbuffer(ctx
);
1297 fpv
= get_color_fp_variant(st
);
1298 driver_fp
= fpv
->driver_shader
;
1300 driver_vp
= make_passthrough_vertex_shader(st
, GL_FALSE
);
1302 if (st
->pixel_xfer
.pixelmap_enabled
) {
1303 sv
[1] = st
->pixel_xfer
.pixelmap_sampler_view
;
1308 assert(type
== GL_DEPTH
);
1309 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1310 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1312 fpv
= get_depth_stencil_fp_variant(st
, GL_TRUE
, GL_FALSE
);
1313 driver_fp
= fpv
->driver_shader
;
1315 driver_vp
= make_passthrough_vertex_shader(st
, GL_TRUE
);
1318 /* update fragment program constants */
1319 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
1322 if (rbRead
->Base
.Wrapped
)
1323 rbRead
= st_renderbuffer(rbRead
->Base
.Wrapped
);
1325 sample_count
= rbRead
->texture
->nr_samples
;
1326 /* I believe this would be legal, presumably would need to do a resolve
1327 for color, and for depth/stencil spec says to just use one of the
1328 depth/stencil samples per pixel? Need some transfer clarifications. */
1329 assert(sample_count
< 2);
1331 srcFormat
= rbRead
->texture
->format
;
1333 if (screen
->is_format_supported(screen
, srcFormat
, st
->internal_target
,
1335 PIPE_BIND_SAMPLER_VIEW
)) {
1336 texFormat
= srcFormat
;
1339 /* srcFormat can't be used as a texture format */
1340 if (type
== GL_DEPTH
) {
1341 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1342 st
->internal_target
, sample_count
,
1343 PIPE_BIND_DEPTH_STENCIL
);
1344 assert(texFormat
!= PIPE_FORMAT_NONE
);
1347 /* default color format */
1348 texFormat
= st_choose_format(screen
, GL_RGBA
, st
->internal_target
,
1349 sample_count
, PIPE_BIND_SAMPLER_VIEW
);
1350 assert(texFormat
!= PIPE_FORMAT_NONE
);
1354 /* Invert src region if needed */
1355 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1356 srcy
= ctx
->ReadBuffer
->Height
- srcy
- height
;
1357 invertTex
= !invertTex
;
1360 /* Clip the read region against the src buffer bounds.
1361 * We'll still allocate a temporary buffer/texture for the original
1362 * src region size but we'll only read the region which is on-screen.
1363 * This may mean that we draw garbage pixels into the dest region, but
1370 _mesa_clip_readpixels(ctx
, &readX
, &readY
, &readW
, &readH
, &pack
);
1371 readW
= MAX2(0, readW
);
1372 readH
= MAX2(0, readH
);
1374 /* alloc temporary texture */
1375 pt
= alloc_texture(st
, width
, height
, texFormat
);
1379 sv
[0] = st_create_texture_sampler_view(st
->pipe
, pt
);
1381 pipe_resource_reference(&pt
, NULL
);
1385 /* Make temporary texture which is a copy of the src region.
1387 if (srcFormat
== texFormat
) {
1388 struct pipe_box src_box
;
1389 u_box_2d(readX
, readY
, readW
, readH
, &src_box
);
1390 /* copy source framebuffer surface into mipmap/texture */
1391 pipe
->resource_copy_region(pipe
,
1394 pack
.SkipPixels
, pack
.SkipRows
, 0, /* dest pos */
1395 rbRead
->texture
, /* src tex */
1401 /* CPU-based fallback/conversion */
1402 struct pipe_transfer
*ptRead
=
1403 pipe_get_transfer(st
->pipe
, rbRead
->texture
,
1404 0, 0, /* level, layer */
1406 readX
, readY
, readW
, readH
);
1407 struct pipe_transfer
*ptTex
;
1408 enum pipe_transfer_usage transfer_usage
;
1410 if (ST_DEBUG
& DEBUG_FALLBACK
)
1411 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1413 if (type
== GL_DEPTH
&& util_format_is_depth_and_stencil(pt
->format
))
1414 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1416 transfer_usage
= PIPE_TRANSFER_WRITE
;
1418 ptTex
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, transfer_usage
,
1419 0, 0, width
, height
);
1421 /* copy image from ptRead surface to ptTex surface */
1422 if (type
== GL_COLOR
) {
1423 /* alternate path using get/put_tile() */
1424 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1425 enum pipe_format readFormat
, drawFormat
;
1426 readFormat
= util_format_linear(rbRead
->texture
->format
);
1427 drawFormat
= util_format_linear(pt
->format
);
1428 pipe_get_tile_rgba_format(pipe
, ptRead
, 0, 0, readW
, readH
,
1430 pipe_put_tile_rgba_format(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1431 readW
, readH
, drawFormat
, buf
);
1436 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1437 pipe_get_tile_z(pipe
, ptRead
, 0, 0, readW
, readH
, buf
);
1438 pipe_put_tile_z(pipe
, ptTex
, pack
.SkipPixels
, pack
.SkipRows
,
1443 pipe
->transfer_destroy(pipe
, ptRead
);
1444 pipe
->transfer_destroy(pipe
, ptTex
);
1447 /* OK, the texture 'pt' contains the src image/pixels. Now draw a
1448 * textured quad with that texture.
1450 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1451 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1456 color
, invertTex
, GL_FALSE
, GL_FALSE
);
1458 pipe_resource_reference(&pt
, NULL
);
1459 pipe_sampler_view_reference(&sv
[0], NULL
);
1464 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1466 functions
->DrawPixels
= st_DrawPixels
;
1467 functions
->CopyPixels
= st_CopyPixels
;
1472 st_destroy_drawpix(struct st_context
*st
)
1476 for (i
= 0; i
< Elements(st
->drawpix
.shaders
); i
++) {
1477 if (st
->drawpix
.shaders
[i
])
1478 _mesa_reference_fragprog(st
->ctx
, &st
->drawpix
.shaders
[i
], NULL
);
1481 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1482 if (st
->drawpix
.vert_shaders
[0])
1483 ureg_free_tokens(st
->drawpix
.vert_shaders
[0]);
1484 if (st
->drawpix
.vert_shaders
[1])
1485 ureg_free_tokens(st
->drawpix
.vert_shaders
[1]);
1488 #endif /* FEATURE_drawpix */