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/macros.h"
36 #include "main/texformat.h"
37 #include "shader/program.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_print.h"
41 #include "st_context.h"
43 #include "st_atom_constbuf.h"
45 #include "st_program.h"
46 #include "st_cb_drawpixels.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_texture.h"
51 #include "st_format.h"
52 #include "st_mesa_to_tgsi.h"
53 #include "st_texture.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "pipe/p_inlines.h"
57 #include "pipe/p_winsys.h"
58 #include "util/p_tile.h"
59 #include "util/u_draw_quad.h"
60 #include "shader/prog_instruction.h"
61 #include "cso_cache/cso_context.h"
65 * Check if the given program is:
66 * 0: MOVE result.color, fragment.color;
70 is_passthrough_program(const struct gl_fragment_program
*prog
)
72 if (prog
->Base
.NumInstructions
== 2) {
73 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
74 if (inst
[0].Opcode
== OPCODE_MOV
&&
75 inst
[1].Opcode
== OPCODE_END
&&
76 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
77 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
78 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
79 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
80 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
81 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
91 * Make fragment shader for glDraw/CopyPixels. This shader is made
92 * by combining the pixel transfer shader with the user-defined shader.
94 static struct st_fragment_program
*
95 combined_drawpix_fragment_program(GLcontext
*ctx
)
97 struct st_context
*st
= ctx
->st
;
98 struct st_fragment_program
*stfp
;
100 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
101 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
102 /* the pixel tranfer program has not changed and the user-defined
103 * program has not changed, so re-use the combined program.
105 stfp
= st
->pixel_xfer
.combined_prog
;
108 /* Concatenate the pixel transfer program with the current user-
111 if (is_passthrough_program(&st
->fp
->Base
)) {
112 stfp
= (struct st_fragment_program
*)
113 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
116 stfp
= (struct st_fragment_program
*)
117 _mesa_combine_programs(ctx
,
118 &st
->pixel_xfer
.program
->Base
.Base
,
124 struct gl_program
*p
= &stfp
->Base
.Base
;
125 printf("Combined DrawPixels program:\n");
126 _mesa_print_program(p
);
127 printf("InputsRead: 0x%x\n", p
->InputsRead
);
128 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
129 _mesa_print_parameter_list(p
->Parameters
);
133 /* translate to TGSI tokens */
134 st_translate_fragment_program(st
, stfp
, NULL
);
136 /* save new program, update serial numbers */
137 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
138 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
139 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
140 st
->pixel_xfer
.combined_prog
= stfp
;
143 /* Ideally we'd have updated the pipe constants during the normal
144 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
146 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
153 * Create fragment shader that does a TEX() instruction to get a Z
154 * value, then writes to FRAG_RESULT_DEPR.
155 * Pass fragment color through as-is.
157 static struct st_fragment_program
*
158 make_fragment_shader_z(struct st_context
*st
)
160 GLcontext
*ctx
= st
->ctx
;
161 /* only make programs once and re-use */
162 static struct st_fragment_program
*stfp
= NULL
;
163 struct gl_program
*p
;
169 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
173 p
->NumInstructions
= 3;
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
);
182 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
183 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
184 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
185 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPR
;
186 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
187 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
188 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
189 p
->Instructions
[ic
].TexSrcUnit
= 0;
190 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
193 /* MOV result.color, fragment.color */
194 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
195 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
196 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
197 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
198 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
202 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
204 assert(ic
== p
->NumInstructions
);
206 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
207 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
209 stfp
= (struct st_fragment_program
*) p
;
210 st_translate_fragment_program(st
, stfp
, NULL
);
218 * Create a simple vertex shader that just passes through the
219 * vertex position and texcoord (and optionally, color).
221 static struct st_vertex_program
*
222 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
224 /* only make programs once and re-use */
225 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
226 GLcontext
*ctx
= st
->ctx
;
227 struct st_vertex_program
*stvp
;
228 struct gl_program
*p
;
231 if (progs
[passColor
])
232 return progs
[passColor
];
234 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
239 p
->NumInstructions
= 4;
241 p
->NumInstructions
= 3;
243 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
244 if (!p
->Instructions
) {
245 ctx
->Driver
.DeleteProgram(ctx
, p
);
248 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
249 /* MOV result.pos, vertex.pos; */
250 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
251 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
252 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
253 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
254 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
255 /* MOV result.texcoord0, vertex.texcoord0; */
256 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
257 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
258 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
259 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
260 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
263 /* MOV result.color0, vertex.color0; */
264 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
265 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
266 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
267 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
268 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
273 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
276 assert(ic
== p
->NumInstructions
);
278 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
279 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
280 (1 << VERT_RESULT_HPOS
));
282 p
->InputsRead
|= VERT_BIT_COLOR0
;
283 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
286 stvp
= (struct st_vertex_program
*) p
;
287 st_translate_vertex_program(st
, stvp
, NULL
);
289 progs
[passColor
] = stvp
;
296 _mesa_base_format(GLenum format
)
299 case GL_DEPTH_COMPONENT
:
300 return GL_DEPTH_COMPONENT
;
301 case GL_STENCIL_INDEX
:
302 return GL_STENCIL_INDEX
;
310 * Make texture containing an image for glDrawPixels image.
311 * If 'pixels' is NULL, leave the texture image data undefined.
313 static struct pipe_texture
*
314 make_texture(struct st_context
*st
,
315 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
316 const struct gl_pixelstore_attrib
*unpack
,
317 const GLvoid
*pixels
)
319 GLcontext
*ctx
= st
->ctx
;
320 struct pipe_context
*pipe
= st
->pipe
;
321 struct pipe_screen
*screen
= pipe
->screen
;
322 const struct gl_texture_format
*mformat
;
323 struct pipe_texture
*pt
;
324 enum pipe_format pipeFormat
;
328 baseFormat
= _mesa_base_format(format
);
330 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
333 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
335 cpp
= st_sizeof_format(pipeFormat
);
337 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, width
, height
,
342 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
344 pt->region = buffer_object_region(unpack->BufferObj);
346 printf("st_DrawPixels (sourcing from PBO not implemented yet)\n");
350 struct pipe_surface
*surface
;
351 static const GLuint dstImageOffsets
= 0;
354 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
356 /* we'll do pixel transfer in a fragment shader */
357 ctx
->_ImageTransferState
= 0x0;
359 surface
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0);
361 /* map texture surface */
362 dest
= pipe_surface_map(surface
);
364 /* Put image into texture surface.
365 * Note that the image is actually going to be upside down in
366 * the texture. We deal with that with texcoords.
368 success
= mformat
->StoreImage(ctx
, 2, /* dims */
369 baseFormat
, /* baseInternalFormat */
370 mformat
, /* gl_texture_format */
372 0, 0, 0, /* dstX/Y/Zoffset */
373 surface
->pitch
* cpp
, /* dstRowStride, bytes */
374 &dstImageOffsets
, /* dstImageOffsets */
375 width
, height
, 1, /* size */
376 format
, type
, /* src format/type */
377 pixels
, /* data source */
381 pipe_surface_unmap(surface
);
382 pipe_surface_reference(&surface
, NULL
);
383 pipe
->texture_update(pipe
, pt
, 0, 0x1);
388 ctx
->_ImageTransferState
= imageTransferStateSave
;
396 * Draw textured quad.
397 * Coords are window coords with y=0=bottom.
400 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
401 GLfloat x1
, GLfloat y1
, GLboolean invertTex
)
403 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
405 GLfloat sLeft
= 0.0, sRight
= 1.0;
406 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
409 verts
[0][0][0] = x0
; /* attr[0].x */
410 verts
[0][0][1] = y0
; /* attr[0].y */
411 verts
[0][1][0] = sLeft
; /* attr[1].s */
412 verts
[0][1][1] = tTop
; /* attr[1].t */
417 verts
[1][1][0] = sRight
;
418 verts
[1][1][1] = tTop
;
423 verts
[2][1][0] = sRight
;
424 verts
[2][1][1] = tBot
;
429 verts
[3][1][0] = sLeft
;
430 verts
[3][1][1] = tBot
;
432 /* same for all verts: */
433 for (i
= 0; i
< 4; i
++) {
434 verts
[i
][0][2] = z
; /*Z*/
435 verts
[i
][0][3] = 1.0; /*W*/
436 verts
[i
][1][2] = 0.0; /*R*/
437 verts
[i
][1][3] = 1.0; /*Q*/
440 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2, GL_FALSE
);
445 draw_quad_colored(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
446 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
449 GLfloat bias
= ctx
->st
->bitmap_texcoord_bias
;
450 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
452 GLfloat xBias
= bias
/ (x1
-x0
);
453 GLfloat yBias
= bias
/ (y1
-y0
);
454 GLfloat sLeft
= 0.0 + xBias
, sRight
= 1.0 + xBias
;
455 GLfloat tTop
= invertTex
- yBias
, tBot
= 1.0 - tTop
- yBias
;
458 verts
[0][0][0] = x0
; /* attr[0].x */
459 verts
[0][0][1] = y0
; /* attr[0].y */
460 verts
[0][2][0] = sLeft
; /* attr[2].s */
461 verts
[0][2][1] = tTop
; /* attr[2].t */
466 verts
[1][2][0] = sRight
;
467 verts
[1][2][1] = tTop
;
472 verts
[2][2][0] = sRight
;
473 verts
[2][2][1] = tBot
;
478 verts
[3][2][0] = sLeft
;
479 verts
[3][2][1] = tBot
;
481 /* same for all verts: */
482 for (i
= 0; i
< 4; i
++) {
483 verts
[i
][0][2] = z
; /*Z*/
484 verts
[i
][0][3] = 1.0; /*W*/
485 verts
[i
][1][0] = color
[0];
486 verts
[i
][1][1] = color
[1];
487 verts
[i
][1][2] = color
[2];
488 verts
[i
][1][3] = color
[3];
489 verts
[i
][2][2] = 0.0; /*R*/
490 verts
[i
][2][3] = 1.0; /*Q*/
493 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 3, GL_FALSE
);
499 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
500 GLsizei width
, GLsizei height
,
501 GLfloat zoomX
, GLfloat zoomY
,
502 struct pipe_texture
*pt
,
503 struct st_vertex_program
*stvp
,
504 struct st_fragment_program
*stfp
,
505 const GLfloat
*color
,
508 struct st_context
*st
= ctx
->st
;
509 struct pipe_context
*pipe
= ctx
->st
->pipe
;
510 struct cso_context
*cso
= ctx
->st
->cso_context
;
511 GLfloat x0
, y0
, x1
, y1
;
515 /* XXX if DrawPixels image is larger than max texture size, break
518 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
519 assert(width
<= maxSize
);
520 assert(height
<= maxSize
);
522 cso_save_rasterizer(cso
);
523 cso_save_viewport(cso
);
525 /* rasterizer state: just scissor */
527 struct pipe_rasterizer_state rasterizer
;
528 memset(&rasterizer
, 0, sizeof(rasterizer
));
529 if (ctx
->Scissor
.Enabled
)
530 rasterizer
.scissor
= 1;
532 cso_set_rasterizer(cso
, &rasterizer
);
535 /* fragment shader state: TEX lookup program */
536 pipe
->bind_fs_state(pipe
, stfp
->driver_shader
);
538 /* vertex shader state: position + texcoord pass-through */
539 pipe
->bind_vs_state(pipe
, stvp
->driver_shader
);
542 /* texture sampling state: */
544 struct pipe_sampler_state sampler
;
545 memset(&sampler
, 0, sizeof(sampler
));
546 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
547 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
548 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
549 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
550 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
551 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
552 sampler
.normalized_coords
= 1;
554 cso_single_sampler(cso
, 0, &sampler
);
555 cso_single_sampler_done(cso
);
558 /* viewport state: viewport matching window dims */
560 const float width
= ctx
->DrawBuffer
->Width
;
561 const float height
= ctx
->DrawBuffer
->Height
;
562 struct pipe_viewport_state vp
;
563 vp
.scale
[0] = 0.5 * width
;
564 vp
.scale
[1] = -0.5 * height
;
567 vp
.translate
[0] = 0.5 * width
;
568 vp
.translate
[1] = 0.5 * height
;
569 vp
.translate
[2] = 0.0;
570 vp
.translate
[3] = 0.0;
571 cso_set_viewport(cso
, &vp
);
576 pipe
->set_sampler_textures(pipe
, 1, &pt
);
579 /* Compute window coords (y=0=bottom) with pixel zoom.
580 * Recall that these coords are transformed by the current
581 * vertex shader and viewport transformation.
584 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
586 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
588 /* draw textured quad */
590 draw_quad_colored(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
592 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, invertTex
);
595 cso_restore_rasterizer(cso
);
596 cso_restore_viewport(cso
);
597 /* shaders don't go through cso yet */
598 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
599 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
601 cso_set_rasterizer(cso
, &st
->state
.rasterizer
);
602 cso_set_samplers(cso
, PIPE_MAX_SAMPLERS
,
603 (const struct pipe_sampler_state
**) st
->state
.sampler_list
);
604 pipe
->set_sampler_textures(pipe
, ctx
->st
->state
.num_textures
,
605 ctx
->st
->state
.sampler_texture
);
610 * Check if a GL format/type combination is a match to the given pipe format.
611 * XXX probably move this to a re-usable place.
614 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
616 static const GLuint one
= 1;
617 GLubyte littleEndian
= *((GLubyte
*) &one
);
619 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
621 type
== GL_UNSIGNED_BYTE
&&
625 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
626 format
== GL_ABGR_EXT
&&
627 type
== GL_UNSIGNED_BYTE
&&
631 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
633 type
== GL_UNSIGNED_BYTE
&&
637 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
639 type
== GL_UNSIGNED_SHORT_5_6_5
) {
640 /* endian don't care */
643 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
645 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
646 /* endian don't care */
649 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
650 format
== GL_STENCIL_INDEX
&&
651 type
== GL_UNSIGNED_BYTE
) {
654 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
655 format
== GL_DEPTH_COMPONENT
&&
656 type
== GL_UNSIGNED_INT
) {
659 /* XXX add more cases */
667 * Check if any per-fragment ops are enabled.
668 * XXX probably move this to a re-usable place.
671 any_fragment_ops(const struct st_context
*st
)
673 if (st
->state
.depth_stencil
.alpha
.enabled
||
674 st
->state
.depth_stencil
.depth
.enabled
||
675 st
->state
.blend
.blend_enable
||
676 st
->state
.blend
.logicop_enable
)
677 /* XXX more checks */
685 * Check if any pixel transfer ops are enabled.
686 * XXX probably move this to a re-usable place.
689 any_pixel_transfer_ops(const struct st_context
*st
)
691 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
692 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
693 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
694 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
695 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
696 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
697 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
698 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
699 st
->ctx
->Pixel
.MapColorFlag
)
700 /* XXX more checks */
708 * Draw image with a blit, or other non-textured quad method.
711 draw_blit(struct st_context
*st
,
712 GLsizei width
, GLsizei height
,
713 GLenum format
, GLenum type
, const GLvoid
*pixels
)
721 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
722 GLsizei width
, GLsizei height
, GLenum type
,
723 const struct gl_pixelstore_attrib
*unpack
,
724 const GLvoid
*pixels
)
726 struct st_context
*st
= ctx
->st
;
727 struct pipe_context
*pipe
= st
->pipe
;
728 struct pipe_surface
*ps
= st
->state
.framebuffer
.zsbuf
;
729 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
733 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
735 /* map the stencil buffer */
736 stmap
= pipe_surface_map(ps
);
738 /* if width > MAX_WIDTH, have to process image in chunks */
740 while (skipPixels
< width
) {
741 const GLint spanX
= x
+ skipPixels
;
742 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
744 for (row
= 0; row
< height
; row
++) {
745 GLint spanY
= y
+ row
;
746 GLubyte values
[MAX_WIDTH
];
747 GLenum destType
= GL_UNSIGNED_BYTE
;
748 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
750 GL_COLOR_INDEX
, type
,
752 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
753 type
, source
, unpack
,
754 ctx
->_ImageTransferState
);
757 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
758 spanX, spanY, values);
762 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
763 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
766 switch (ps
->format
) {
767 case PIPE_FORMAT_U_S8
:
769 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
770 memcpy(dest
, values
, spanWidth
);
773 case PIPE_FORMAT_S8Z24_UNORM
:
775 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
777 for (k
= 0; k
< spanWidth
; k
++) {
779 p
= (p
& 0xffffff) | (values
[k
] << 24);
789 skipPixels
+= spanWidth
;
792 /* unmap the stencil buffer */
793 pipe_surface_unmap(ps
);
798 * Called via ctx->Driver.DrawPixels()
801 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
802 GLenum format
, GLenum type
,
803 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
805 struct st_fragment_program
*stfp
;
806 struct st_vertex_program
*stvp
;
807 struct st_context
*st
= ctx
->st
;
808 struct pipe_surface
*ps
;
810 const GLfloat
*color
;
812 if (format
== GL_STENCIL_INDEX
) {
813 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
817 st_validate_state(st
);
819 if (format
== GL_DEPTH_COMPONENT
) {
820 ps
= st
->state
.framebuffer
.zsbuf
;
821 stfp
= make_fragment_shader_z(ctx
->st
);
822 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
823 color
= ctx
->Current
.RasterColor
;
825 else if (format
== GL_STENCIL_INDEX
) {
826 ps
= st
->state
.framebuffer
.zsbuf
;
827 /* XXX special case - can't use texture map */
831 ps
= st
->state
.framebuffer
.cbufs
[0];
832 stfp
= combined_drawpix_fragment_program(ctx
);
833 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
837 bufferFormat
= ps
->format
;
839 if (any_fragment_ops(st
) ||
840 any_pixel_transfer_ops(st
) ||
841 !compatible_formats(format
, type
, ps
->format
)) {
843 struct pipe_texture
*pt
844 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
846 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
847 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
848 pt
, stvp
, stfp
, color
, GL_FALSE
);
849 pipe_texture_reference(&pt
, NULL
);
854 draw_blit(st
, width
, height
, format
, type
, pixels
);
861 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
862 GLsizei width
, GLsizei height
,
863 GLint dstx
, GLint dsty
)
865 struct st_renderbuffer
*rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_StencilBuffer
);
866 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
867 struct pipe_surface
*psRead
= rbRead
->surface
;
868 struct pipe_surface
*psDraw
= rbDraw
->surface
;
869 ubyte
*readMap
, *drawMap
;
873 buffer
= malloc(width
* height
* sizeof(ubyte
));
875 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
879 /* map the stencil buffers */
880 readMap
= pipe_surface_map(psRead
);
881 drawMap
= pipe_surface_map(psDraw
);
883 /* this will do stencil pixel transfer ops */
884 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
885 &ctx
->DefaultPacking
, buffer
);
888 /* XXX PixelZoom not handled yet */
889 for (i
= 0; i
< height
; i
++) {
896 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
897 y
= ctx
->DrawBuffer
->Height
- y
- 1;
900 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
901 src
= buffer
+ i
* width
;
903 switch (psDraw
->format
) {
904 case PIPE_FORMAT_S8Z24_UNORM
:
906 uint
*dst4
= (uint
*) dst
;
908 for (j
= 0; j
< width
; j
++) {
909 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
914 case PIPE_FORMAT_U_S8
:
915 memcpy(dst
, src
, width
);
924 /* unmap the stencil buffers */
925 pipe_surface_unmap(psRead
);
926 pipe_surface_unmap(psDraw
);
931 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
932 GLsizei width
, GLsizei height
,
933 GLint dstx
, GLint dsty
, GLenum type
)
935 struct st_context
*st
= ctx
->st
;
936 struct pipe_context
*pipe
= st
->pipe
;
937 struct pipe_screen
*screen
= pipe
->screen
;
938 struct st_renderbuffer
*rbRead
;
939 struct st_vertex_program
*stvp
;
940 struct st_fragment_program
*stfp
;
941 struct pipe_surface
*psRead
;
942 struct pipe_surface
*psTex
;
943 struct pipe_texture
*pt
;
945 enum pipe_format srcFormat
, texFormat
;
947 /* make sure rendering has completed */
948 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
950 st_validate_state(st
);
952 if (type
== GL_STENCIL
) {
953 /* can't use texturing to do stencil */
954 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
958 if (type
== GL_COLOR
) {
959 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
961 stfp
= combined_drawpix_fragment_program(ctx
);
962 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
965 assert(type
== GL_DEPTH
);
966 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
967 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
968 stfp
= make_fragment_shader_z(ctx
->st
);
969 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
972 psRead
= rbRead
->surface
;
973 srcFormat
= psRead
->format
;
975 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE
)) {
976 texFormat
= srcFormat
;
979 /* srcFormat can't be used as a texture format */
980 if (type
== GL_DEPTH
) {
981 static const enum pipe_format zFormats
[] = {
982 PIPE_FORMAT_Z16_UNORM
,
983 PIPE_FORMAT_Z32_UNORM
,
984 PIPE_FORMAT_S8Z24_UNORM
,
985 PIPE_FORMAT_Z24S8_UNORM
989 for (i
= 0; i
< Elements(zFormats
); i
++) {
990 if (screen
->is_format_supported(screen
, zFormats
[i
],
992 texFormat
= zFormats
[i
];
996 assert(texFormat
); /* XXX no depth texture formats??? */
1004 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1005 width
, height
, 1, 0);
1009 psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0);
1011 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1012 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1015 /* For some drivers (like Xlib) it's not possible to treat the
1016 * front/back color buffers as surfaces (they're XImages and Pixmaps).
1017 * So, this var tells us if we can use surface_copy here...
1019 if (st
->haveFramebufferSurfaces
&& srcFormat
== texFormat
) {
1020 /* copy source framebuffer surface into mipmap/texture */
1021 pipe
->surface_copy(pipe
,
1026 srcx
, srcy
, width
, height
);
1028 else if (type
== GL_COLOR
) {
1029 /* alternate path using get/put_tile() */
1030 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1032 pipe_get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1033 pipe_put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1039 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1040 pipe_get_tile_z(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1041 pipe_put_tile_z(pipe
, psTex
, 0, 0, width
, height
, buf
);
1045 /* draw textured quad */
1046 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1047 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1048 pt
, stvp
, stfp
, color
, GL_TRUE
);
1050 pipe_surface_reference(&psTex
, NULL
);
1051 pipe_texture_reference(&pt
, NULL
);
1056 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1058 functions
->DrawPixels
= st_DrawPixels
;
1059 functions
->CopyPixels
= st_CopyPixels
;