1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "main/texstore.h"
39 #include "main/state.h"
40 #include "shader/program.h"
41 #include "shader/prog_parameter.h"
42 #include "shader/prog_print.h"
45 #include "st_context.h"
47 #include "st_atom_constbuf.h"
49 #include "st_program.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_readpixels.h"
52 #include "st_cb_fbo.h"
53 #include "st_cb_texture.h"
55 #include "st_format.h"
56 #include "st_mesa_to_tgsi.h"
57 #include "st_texture.h"
58 #include "st_inlines.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "pipe/p_inlines.h"
63 #include "util/u_tile.h"
64 #include "util/u_draw_quad.h"
65 #include "util/u_math.h"
66 #include "util/u_rect.h"
67 #include "shader/prog_instruction.h"
68 #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.
101 static struct st_fragment_program
*
102 combined_drawpix_fragment_program(GLcontext
*ctx
)
104 struct st_context
*st
= st_context(ctx
);
105 struct st_fragment_program
*stfp
;
107 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
108 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
109 /* the pixel tranfer program has not changed and the user-defined
110 * program has not changed, so re-use the combined program.
112 stfp
= st
->pixel_xfer
.combined_prog
;
115 /* Concatenate the pixel transfer program with the current user-
118 if (is_passthrough_program(&st
->fp
->Base
)) {
119 stfp
= (struct st_fragment_program
*)
120 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
124 printf("Base program:\n");
125 _mesa_print_program(&st
->fp
->Base
.Base
);
126 printf("DrawPix program:\n");
127 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
129 stfp
= (struct st_fragment_program
*)
130 _mesa_combine_programs(ctx
,
131 &st
->pixel_xfer
.program
->Base
.Base
,
137 struct gl_program
*p
= &stfp
->Base
.Base
;
138 printf("Combined DrawPixels program:\n");
139 _mesa_print_program(p
);
140 printf("InputsRead: 0x%x\n", p
->InputsRead
);
141 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
142 _mesa_print_parameter_list(p
->Parameters
);
146 /* translate to TGSI tokens */
147 st_translate_fragment_program(st
, stfp
, NULL
);
149 /* save new program, update serial numbers */
150 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
151 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
152 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
153 /* can't reference new program directly, already have a reference on it */
154 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
155 st
->pixel_xfer
.combined_prog
= stfp
;
158 /* Ideally we'd have updated the pipe constants during the normal
159 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
161 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
168 * Create fragment shader that does a TEX() instruction to get a Z
169 * value, then writes to FRAG_RESULT_DEPTH.
170 * Pass fragment color through as-is.
172 static struct st_fragment_program
*
173 make_fragment_shader_z(struct st_context
*st
)
175 GLcontext
*ctx
= st
->ctx
;
176 struct gl_program
*p
;
179 if (st
->drawpix
.z_shader
) {
180 return st
->drawpix
.z_shader
;
186 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
190 p
->NumInstructions
= 3;
192 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
193 if (!p
->Instructions
) {
194 ctx
->Driver
.DeleteProgram(ctx
, p
);
197 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
199 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
200 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
201 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
202 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPTH
;
203 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
204 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
205 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
206 p
->Instructions
[ic
].TexSrcUnit
= 0;
207 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
210 /* MOV result.color, fragment.color */
211 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
212 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
213 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLOR
;
214 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
215 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
219 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
221 assert(ic
== p
->NumInstructions
);
223 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
224 p
->OutputsWritten
= (1 << FRAG_RESULT_COLOR
) | (1 << FRAG_RESULT_DEPTH
);
225 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
227 st
->drawpix
.z_shader
= (struct st_fragment_program
*) p
;
228 st_translate_fragment_program(st
, st
->drawpix
.z_shader
, NULL
);
230 return st
->drawpix
.z_shader
;
236 * Create a simple vertex shader that just passes through the
237 * vertex position and texcoord (and optionally, color).
239 static struct st_vertex_program
*
240 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
242 GLcontext
*ctx
= st
->ctx
;
243 struct st_vertex_program
*stvp
;
244 struct gl_program
*p
;
247 if (st
->drawpix
.vert_shaders
[passColor
])
248 return st
->drawpix
.vert_shaders
[passColor
];
253 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
258 p
->NumInstructions
= 4;
260 p
->NumInstructions
= 3;
262 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
263 if (!p
->Instructions
) {
264 ctx
->Driver
.DeleteProgram(ctx
, p
);
267 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
268 /* MOV result.pos, vertex.pos; */
269 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
270 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
271 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
272 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
273 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
274 /* MOV result.texcoord0, vertex.texcoord0; */
275 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
276 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
277 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
278 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
279 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
282 /* MOV result.color0, vertex.color0; */
283 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
284 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
285 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
286 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
287 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
292 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
295 assert(ic
== p
->NumInstructions
);
297 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
298 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
299 (1 << VERT_RESULT_HPOS
));
301 p
->InputsRead
|= VERT_BIT_COLOR0
;
302 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
305 stvp
= (struct st_vertex_program
*) p
;
306 st_translate_vertex_program(st
, stvp
, NULL
, NULL
, NULL
);
308 st
->drawpix
.vert_shaders
[passColor
] = stvp
;
315 _mesa_base_format(GLenum format
)
318 case GL_DEPTH_COMPONENT
:
319 return GL_DEPTH_COMPONENT
;
320 case GL_DEPTH_STENCIL
:
321 return GL_DEPTH_STENCIL
;
322 case GL_STENCIL_INDEX
:
323 return GL_STENCIL_INDEX
;
331 * Make texture containing an image for glDrawPixels image.
332 * If 'pixels' is NULL, leave the texture image data undefined.
334 static struct pipe_texture
*
335 make_texture(struct st_context
*st
,
336 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
337 const struct gl_pixelstore_attrib
*unpack
,
338 const GLvoid
*pixels
)
340 GLcontext
*ctx
= st
->ctx
;
341 struct pipe_context
*pipe
= st
->pipe
;
342 struct pipe_screen
*screen
= pipe
->screen
;
344 struct pipe_texture
*pt
;
345 enum pipe_format pipeFormat
;
350 baseFormat
= _mesa_base_format(format
);
352 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
355 pipeFormat
= st_mesa_format_to_pipe_format(mformat
);
357 cpp
= st_sizeof_format(pipeFormat
);
359 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
363 /* Need to use POT texture? */
366 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
369 l2pt
= util_logbase2(width
);
370 if (1<<l2pt
!= width
) {
373 l2pt
= util_logbase2(height
);
374 if (1<<l2pt
!= height
) {
378 /* Check against maximum texture size */
379 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
380 assert(ptw
<= maxSize
);
381 assert(pth
<= maxSize
);
384 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, ptw
, pth
, 1,
385 PIPE_TEXTURE_USAGE_SAMPLER
);
387 _mesa_unmap_pbo_source(ctx
, unpack
);
392 struct pipe_transfer
*transfer
;
393 static const GLuint dstImageOffsets
= 0;
396 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
398 /* we'll do pixel transfer in a fragment shader */
399 ctx
->_ImageTransferState
= 0x0;
401 transfer
= st_no_flush_get_tex_transfer(st
, pt
, 0, 0, 0,
402 PIPE_TRANSFER_WRITE
, 0, 0,
405 /* map texture transfer */
406 dest
= screen
->transfer_map(screen
, transfer
);
409 /* Put image into texture transfer.
410 * Note that the image is actually going to be upside down in
411 * the texture. We deal with that with texcoords.
413 success
= _mesa_texstore(ctx
, 2, /* dims */
414 baseFormat
, /* baseInternalFormat */
415 mformat
, /* gl_format */
417 0, 0, 0, /* dstX/Y/Zoffset */
418 transfer
->stride
, /* dstRowStride, bytes */
419 &dstImageOffsets
, /* dstImageOffsets */
420 width
, height
, 1, /* size */
421 format
, type
, /* src format/type */
422 pixels
, /* data source */
426 screen
->transfer_unmap(screen
, transfer
);
427 screen
->tex_transfer_destroy(transfer
);
432 ctx
->_ImageTransferState
= imageTransferStateSave
;
435 _mesa_unmap_pbo_source(ctx
, unpack
);
442 * Draw quad with texcoords and optional color.
443 * Coords are window coords with y=0=bottom.
444 * \param color may be null
445 * \param invertTex if true, flip texcoords vertically
448 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
449 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
450 GLboolean invertTex
, GLfloat maxXcoord
, GLfloat maxYcoord
)
452 struct st_context
*st
= st_context(ctx
);
453 struct pipe_context
*pipe
= st
->pipe
;
454 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
456 /* setup vertex data */
458 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
459 const GLfloat fb_width
= (GLfloat
) fb
->Width
;
460 const GLfloat fb_height
= (GLfloat
) fb
->Height
;
461 const GLfloat clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
462 const GLfloat clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
463 const GLfloat clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
464 const GLfloat clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
465 const GLfloat sLeft
= 0.0f
, sRight
= maxXcoord
;
466 const GLfloat tTop
= invertTex
? maxYcoord
: 0.0f
;
467 const GLfloat tBot
= invertTex
? 0.0f
: maxYcoord
;
471 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
472 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
475 verts
[1][0][0] = clip_x1
;
476 verts
[1][0][1] = clip_y0
;
479 verts
[2][0][0] = clip_x1
;
480 verts
[2][0][1] = clip_y1
;
483 verts
[3][0][0] = clip_x0
;
484 verts
[3][0][1] = clip_y1
;
487 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
488 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
489 verts
[1][tex
][0] = sRight
;
490 verts
[1][tex
][1] = tTop
;
491 verts
[2][tex
][0] = sRight
;
492 verts
[2][tex
][1] = tBot
;
493 verts
[3][tex
][0] = sLeft
;
494 verts
[3][tex
][1] = tBot
;
496 /* same for all verts: */
498 for (i
= 0; i
< 4; i
++) {
499 verts
[i
][0][2] = z
; /*Z*/
500 verts
[i
][0][3] = 1.0f
; /*W*/
501 verts
[i
][1][0] = color
[0];
502 verts
[i
][1][1] = color
[1];
503 verts
[i
][1][2] = color
[2];
504 verts
[i
][1][3] = color
[3];
505 verts
[i
][2][2] = 0.0f
; /*R*/
506 verts
[i
][2][3] = 1.0f
; /*Q*/
510 for (i
= 0; i
< 4; i
++) {
511 verts
[i
][0][2] = z
; /*Z*/
512 verts
[i
][0][3] = 1.0f
; /*W*/
513 verts
[i
][1][2] = 0.0f
; /*R*/
514 verts
[i
][1][3] = 1.0f
; /*Q*/
520 struct pipe_buffer
*buf
;
522 /* allocate/load buffer object with vertex data */
523 buf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
525 st_no_flush_pipe_buffer_write(st
, buf
, 0, sizeof(verts
), verts
);
527 util_draw_vertex_buffer(pipe
, buf
, 0,
530 3); /* attribs/vert */
531 pipe_buffer_reference(&buf
, NULL
);
538 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
539 GLsizei width
, GLsizei height
,
540 GLfloat zoomX
, GLfloat zoomY
,
541 struct pipe_texture
*pt
,
542 struct st_vertex_program
*stvp
,
543 struct st_fragment_program
*stfp
,
544 const GLfloat
*color
,
547 struct st_context
*st
= st_context(ctx
);
548 struct pipe_context
*pipe
= st
->pipe
;
549 struct cso_context
*cso
= st
->cso_context
;
550 GLfloat x0
, y0
, x1
, y1
;
554 /* XXX if DrawPixels image is larger than max texture size, break
557 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
558 assert(width
<= maxSize
);
559 assert(height
<= maxSize
);
561 cso_save_rasterizer(cso
);
562 cso_save_viewport(cso
);
563 cso_save_samplers(cso
);
564 cso_save_sampler_textures(cso
);
565 cso_save_fragment_shader(cso
);
566 cso_save_vertex_shader(cso
);
568 /* rasterizer state: just scissor */
570 struct pipe_rasterizer_state rasterizer
;
571 memset(&rasterizer
, 0, sizeof(rasterizer
));
572 rasterizer
.gl_rasterization_rules
= 1;
573 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
574 cso_set_rasterizer(cso
, &rasterizer
);
577 /* fragment shader state: TEX lookup program */
578 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
580 /* vertex shader state: position + texcoord pass-through */
581 cso_set_vertex_shader_handle(cso
, stvp
->driver_shader
);
584 /* texture sampling state: */
586 struct pipe_sampler_state sampler
;
587 memset(&sampler
, 0, sizeof(sampler
));
588 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
589 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
590 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
591 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
592 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
593 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
594 sampler
.normalized_coords
= 1;
596 cso_single_sampler(cso
, 0, &sampler
);
597 if (st
->pixel_xfer
.pixelmap_enabled
) {
598 cso_single_sampler(cso
, 1, &sampler
);
600 cso_single_sampler_done(cso
);
603 /* viewport state: viewport matching window dims */
605 const float w
= (float) ctx
->DrawBuffer
->Width
;
606 const float h
= (float) ctx
->DrawBuffer
->Height
;
607 struct pipe_viewport_state vp
;
608 vp
.scale
[0] = 0.5f
* w
;
609 vp
.scale
[1] = -0.5f
* h
;
612 vp
.translate
[0] = 0.5f
* w
;
613 vp
.translate
[1] = 0.5f
* h
;
614 vp
.translate
[2] = 0.0f
;
615 vp
.translate
[3] = 0.0f
;
616 cso_set_viewport(cso
, &vp
);
620 if (st
->pixel_xfer
.pixelmap_enabled
) {
621 struct pipe_texture
*textures
[2];
623 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
624 pipe
->set_sampler_textures(pipe
, 2, textures
);
627 pipe
->set_sampler_textures(pipe
, 1, &pt
);
630 /* Compute window coords (y=0=bottom) with pixel zoom.
631 * Recall that these coords are transformed by the current
632 * vertex shader and viewport transformation.
635 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
637 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
639 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
,
640 (GLfloat
) width
/ pt
->width
[0],
641 (GLfloat
) height
/ pt
->height
[0]);
644 cso_restore_rasterizer(cso
);
645 cso_restore_viewport(cso
);
646 cso_restore_samplers(cso
);
647 cso_restore_sampler_textures(cso
);
648 cso_restore_fragment_shader(cso
);
649 cso_restore_vertex_shader(cso
);
654 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
655 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
656 const struct gl_pixelstore_attrib
*unpack
,
657 const GLvoid
*pixels
)
659 struct st_context
*st
= st_context(ctx
);
660 struct pipe_context
*pipe
= st
->pipe
;
661 struct pipe_screen
*screen
= pipe
->screen
;
662 struct st_renderbuffer
*strb
;
663 enum pipe_transfer_usage usage
;
664 struct pipe_transfer
*pt
;
665 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
668 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
671 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
673 /* totally clipped */
678 strb
= st_renderbuffer(ctx
->DrawBuffer
->
679 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
681 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
682 y
= ctx
->DrawBuffer
->Height
- y
- height
;
685 if(format
!= GL_DEPTH_STENCIL
&&
686 pf_get_component_bits( strb
->format
, PIPE_FORMAT_COMP_Z
) != 0)
687 usage
= PIPE_TRANSFER_READ_WRITE
;
689 usage
= PIPE_TRANSFER_WRITE
;
691 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
, 0, 0, 0,
695 stmap
= screen
->transfer_map(screen
, pt
);
697 pixels
= _mesa_map_pbo_source(ctx
, &clippedUnpack
, pixels
);
700 /* if width > MAX_WIDTH, have to process image in chunks */
702 while (skipPixels
< width
) {
703 const GLint spanX
= skipPixels
;
704 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
706 for (row
= 0; row
< height
; row
++) {
707 GLubyte sValues
[MAX_WIDTH
];
708 GLuint zValues
[MAX_WIDTH
];
709 GLenum destType
= GL_UNSIGNED_BYTE
;
710 const GLvoid
*source
= _mesa_image_address2d(&clippedUnpack
, pixels
,
714 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, sValues
,
715 type
, source
, &clippedUnpack
,
716 ctx
->_ImageTransferState
);
718 if (format
== GL_DEPTH_STENCIL
) {
719 _mesa_unpack_depth_span(ctx
, spanWidth
, GL_UNSIGNED_INT
, zValues
,
720 (1 << 24) - 1, type
, source
,
725 _mesa_problem(ctx
, "Gallium glDrawPixels(GL_STENCIL) with "
726 "zoom not complete");
732 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
733 spanY
= height
- row
- 1;
739 /* now pack the stencil (and Z) values in the dest format */
740 switch (pt
->format
) {
741 case PIPE_FORMAT_S8_UNORM
:
743 ubyte
*dest
= stmap
+ spanY
* pt
->stride
+ spanX
;
744 assert(usage
== PIPE_TRANSFER_WRITE
);
745 memcpy(dest
, sValues
, spanWidth
);
748 case PIPE_FORMAT_S8Z24_UNORM
:
749 if (format
== GL_DEPTH_STENCIL
) {
750 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
752 assert(usage
== PIPE_TRANSFER_WRITE
);
753 for (k
= 0; k
< spanWidth
; k
++) {
754 dest
[k
] = zValues
[k
] | (sValues
[k
] << 24);
758 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
760 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
761 for (k
= 0; k
< spanWidth
; k
++) {
762 dest
[k
] = (dest
[k
] & 0xffffff) | (sValues
[k
] << 24);
766 case PIPE_FORMAT_Z24S8_UNORM
:
767 if (format
== GL_DEPTH_STENCIL
) {
768 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
770 assert(usage
== PIPE_TRANSFER_WRITE
);
771 for (k
= 0; k
< spanWidth
; k
++) {
772 dest
[k
] = (zValues
[k
] << 8) | (sValues
[k
] & 0xff);
776 uint
*dest
= (uint
*) (stmap
+ spanY
* pt
->stride
+ spanX
*4);
778 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
779 for (k
= 0; k
< spanWidth
; k
++) {
780 dest
[k
] = (dest
[k
] & 0xffffff00) | (sValues
[k
] & 0xff);
789 skipPixels
+= spanWidth
;
792 _mesa_unmap_pbo_source(ctx
, &clippedUnpack
);
794 /* unmap the stencil buffer */
795 screen
->transfer_unmap(screen
, pt
);
796 screen
->tex_transfer_destroy(pt
);
801 * Called via ctx->Driver.DrawPixels()
804 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
805 GLenum format
, GLenum type
,
806 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
808 struct st_fragment_program
*stfp
;
809 struct st_vertex_program
*stvp
;
810 struct st_context
*st
= st_context(ctx
);
811 struct pipe_surface
*ps
;
812 const GLfloat
*color
;
814 if (format
== GL_STENCIL_INDEX
||
815 format
== GL_DEPTH_STENCIL
) {
816 draw_stencil_pixels(ctx
, x
, y
, width
, height
, format
, type
,
821 /* Mesa state should be up to date by now */
822 assert(ctx
->NewState
== 0x0);
824 st_validate_state(st
);
826 if (format
== GL_DEPTH_COMPONENT
) {
827 ps
= st
->state
.framebuffer
.zsbuf
;
828 stfp
= make_fragment_shader_z(st
);
829 stvp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
830 color
= ctx
->Current
.RasterColor
;
833 ps
= st
->state
.framebuffer
.cbufs
[0];
834 stfp
= combined_drawpix_fragment_program(ctx
);
835 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
839 /* draw with textured quad */
841 struct pipe_texture
*pt
842 = make_texture(st
, width
, height
, format
, type
, unpack
, pixels
);
844 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
845 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
846 pt
, stvp
, stfp
, color
, GL_FALSE
);
847 pipe_texture_reference(&pt
, NULL
);
855 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
856 GLsizei width
, GLsizei height
,
857 GLint dstx
, GLint dsty
)
859 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
860 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
861 enum pipe_transfer_usage usage
;
862 struct pipe_transfer
*ptDraw
;
867 buffer
= _mesa_malloc(width
* height
* sizeof(ubyte
));
869 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
873 /* this will do stencil pixel transfer ops */
874 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
,
875 GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
876 &ctx
->DefaultPacking
, buffer
);
878 if(pf_get_component_bits( rbDraw
->format
, PIPE_FORMAT_COMP_Z
) != 0)
879 usage
= PIPE_TRANSFER_READ_WRITE
;
881 usage
= PIPE_TRANSFER_WRITE
;
883 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
884 dsty
= rbDraw
->Base
.Height
- dsty
- height
;
887 ptDraw
= st_cond_flush_get_tex_transfer(st_context(ctx
),
888 rbDraw
->texture
, 0, 0, 0,
892 assert(ptDraw
->block
.width
== 1);
893 assert(ptDraw
->block
.height
== 1);
895 /* map the stencil buffer */
896 drawMap
= screen
->transfer_map(screen
, ptDraw
);
899 /* XXX PixelZoom not handled yet */
900 for (i
= 0; i
< height
; i
++) {
907 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
911 dst
= drawMap
+ y
* ptDraw
->stride
;
912 src
= buffer
+ i
* width
;
914 switch (ptDraw
->format
) {
915 case PIPE_FORMAT_S8Z24_UNORM
:
917 uint
*dst4
= (uint
*) dst
;
919 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
920 for (j
= 0; j
< width
; j
++) {
921 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
926 case PIPE_FORMAT_Z24S8_UNORM
:
928 uint
*dst4
= (uint
*) dst
;
930 assert(usage
== PIPE_TRANSFER_READ_WRITE
);
931 for (j
= 0; j
< width
; j
++) {
932 *dst4
= (*dst4
& 0xffffff00) | (src
[j
] & 0xff);
937 case PIPE_FORMAT_S8_UNORM
:
938 assert(usage
== PIPE_TRANSFER_WRITE
);
939 memcpy(dst
, src
, width
);
948 /* unmap the stencil buffer */
949 screen
->transfer_unmap(screen
, ptDraw
);
950 screen
->tex_transfer_destroy(ptDraw
);
955 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
956 GLsizei width
, GLsizei height
,
957 GLint dstx
, GLint dsty
, GLenum type
)
959 struct st_context
*st
= st_context(ctx
);
960 struct pipe_context
*pipe
= st
->pipe
;
961 struct pipe_screen
*screen
= pipe
->screen
;
962 struct st_renderbuffer
*rbRead
;
963 struct st_vertex_program
*stvp
;
964 struct st_fragment_program
*stfp
;
965 struct pipe_texture
*pt
;
967 enum pipe_format srcFormat
, texFormat
;
970 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
972 st_validate_state(st
);
998 if (width
< 0 || height
< 0)
1002 if (type
== GL_STENCIL
) {
1003 /* can't use texturing to do stencil */
1004 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1008 if (type
== GL_COLOR
) {
1009 rbRead
= st_get_color_read_renderbuffer(ctx
);
1011 stfp
= combined_drawpix_fragment_program(ctx
);
1012 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
1015 assert(type
== GL_DEPTH
);
1016 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1017 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1018 stfp
= make_fragment_shader_z(st
);
1019 stvp
= st_make_passthrough_vertex_shader(st
, GL_TRUE
);
1022 srcFormat
= rbRead
->texture
->format
;
1024 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE_2D
,
1025 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
1026 texFormat
= srcFormat
;
1029 /* srcFormat can't be used as a texture format */
1030 if (type
== GL_DEPTH
) {
1031 texFormat
= st_choose_format(screen
, GL_DEPTH_COMPONENT
,
1033 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
);
1034 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
1037 /* default color format */
1038 texFormat
= st_choose_format(screen
, GL_RGBA
, PIPE_TEXTURE_2D
,
1039 PIPE_TEXTURE_USAGE_SAMPLER
);
1040 assert(texFormat
!= PIPE_FORMAT_NONE
);
1044 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1045 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1056 /* Need to use POT texture? */
1059 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
)) {
1062 l2pt
= util_logbase2(width
);
1063 if (1<<l2pt
!= width
) {
1066 l2pt
= util_logbase2(height
);
1067 if (1<<l2pt
!= height
) {
1071 /* Check against maximum texture size */
1072 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
1073 assert(ptw
<= maxSize
);
1074 assert(pth
<= maxSize
);
1077 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1079 PIPE_TEXTURE_USAGE_SAMPLER
);
1084 if (srcFormat
== texFormat
) {
1085 /* copy source framebuffer surface into mipmap/texture */
1086 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1087 rbRead
->texture
, 0, 0, 0,
1088 PIPE_BUFFER_USAGE_GPU_READ
);
1089 struct pipe_surface
*psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1090 PIPE_BUFFER_USAGE_GPU_WRITE
);
1091 if (pipe
->surface_copy
) {
1092 pipe
->surface_copy(pipe
,
1096 srcx
, srcy
, width
, height
);
1098 util_surface_copy(pipe
, FALSE
,
1102 srcx
, srcy
, width
, height
);
1104 pipe_surface_reference(&psRead
, NULL
);
1105 pipe_surface_reference(&psTex
, NULL
);
1108 /* CPU-based fallback/conversion */
1109 struct pipe_transfer
*ptRead
=
1110 st_cond_flush_get_tex_transfer(st
, rbRead
->texture
, 0, 0, 0,
1111 PIPE_TRANSFER_READ
, srcx
, srcy
, width
,
1113 struct pipe_transfer
*ptTex
;
1114 enum pipe_transfer_usage transfer_usage
;
1116 if (ST_DEBUG
& DEBUG_FALLBACK
)
1117 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1119 if (type
== GL_DEPTH
&& pf_is_depth_and_stencil(pt
->format
))
1120 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1122 transfer_usage
= PIPE_TRANSFER_WRITE
;
1124 ptTex
= st_cond_flush_get_tex_transfer(st
, pt
, 0, 0, 0, transfer_usage
,
1125 0, 0, width
, height
);
1127 if (type
== GL_COLOR
) {
1128 /* alternate path using get/put_tile() */
1129 GLfloat
*buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1131 pipe_get_tile_rgba(ptRead
, 0, 0, width
, height
, buf
);
1132 pipe_put_tile_rgba(ptTex
, 0, 0, width
, height
, buf
);
1138 GLuint
*buf
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
1139 pipe_get_tile_z(ptRead
, 0, 0, width
, height
, buf
);
1140 pipe_put_tile_z(ptTex
, 0, 0, width
, height
, buf
);
1144 screen
->tex_transfer_destroy(ptRead
);
1145 screen
->tex_transfer_destroy(ptTex
);
1148 /* draw textured quad */
1149 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1150 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1151 pt
, stvp
, stfp
, color
, GL_TRUE
);
1153 pipe_texture_reference(&pt
, NULL
);
1158 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1160 functions
->DrawPixels
= st_DrawPixels
;
1161 functions
->CopyPixels
= st_CopyPixels
;
1166 st_destroy_drawpix(struct st_context
*st
)
1168 st_reference_fragprog(st
, &st
->drawpix
.z_shader
, NULL
);
1169 st_reference_fragprog(st
, &st
->pixel_xfer
.combined_prog
, NULL
);
1170 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[0], NULL
);
1171 st_reference_vertprog(st
, &st
->drawpix
.vert_shaders
[1], NULL
);