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 "shader/program.h"
39 #include "shader/prog_parameter.h"
40 #include "shader/prog_print.h"
42 #include "st_context.h"
44 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_cb_texture.h"
52 #include "st_format.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "st_texture.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_defines.h"
57 #include "pipe/p_inlines.h"
58 #include "pipe/p_winsys.h"
59 #include "util/p_tile.h"
60 #include "util/u_draw_quad.h"
61 #include "shader/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
66 * Check if the given program is:
67 * 0: MOVE result.color, fragment.color;
71 is_passthrough_program(const struct gl_fragment_program
*prog
)
73 if (prog
->Base
.NumInstructions
== 2) {
74 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
75 if (inst
[0].Opcode
== OPCODE_MOV
&&
76 inst
[1].Opcode
== OPCODE_END
&&
77 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
78 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
79 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
80 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
81 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
82 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
92 * Make fragment shader for glDraw/CopyPixels. This shader is made
93 * by combining the pixel transfer shader with the user-defined shader.
95 static struct st_fragment_program
*
96 combined_drawpix_fragment_program(GLcontext
*ctx
)
98 struct st_context
*st
= ctx
->st
;
99 struct st_fragment_program
*stfp
;
101 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
102 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
103 /* the pixel tranfer program has not changed and the user-defined
104 * program has not changed, so re-use the combined program.
106 stfp
= st
->pixel_xfer
.combined_prog
;
109 /* Concatenate the pixel transfer program with the current user-
112 if (is_passthrough_program(&st
->fp
->Base
)) {
113 stfp
= (struct st_fragment_program
*)
114 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
117 stfp
= (struct st_fragment_program
*)
118 _mesa_combine_programs(ctx
,
119 &st
->pixel_xfer
.program
->Base
.Base
,
125 struct gl_program
*p
= &stfp
->Base
.Base
;
126 printf("Combined DrawPixels program:\n");
127 _mesa_print_program(p
);
128 printf("InputsRead: 0x%x\n", p
->InputsRead
);
129 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
130 _mesa_print_parameter_list(p
->Parameters
);
134 /* translate to TGSI tokens */
135 st_translate_fragment_program(st
, stfp
, NULL
);
137 /* save new program, update serial numbers */
138 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
139 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
140 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
141 st
->pixel_xfer
.combined_prog
= stfp
;
144 /* Ideally we'd have updated the pipe constants during the normal
145 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
147 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
154 * Create fragment shader that does a TEX() instruction to get a Z
155 * value, then writes to FRAG_RESULT_DEPR.
156 * Pass fragment color through as-is.
158 static struct st_fragment_program
*
159 make_fragment_shader_z(struct st_context
*st
)
161 GLcontext
*ctx
= st
->ctx
;
162 /* only make programs once and re-use */
163 static struct st_fragment_program
*stfp
= NULL
;
164 struct gl_program
*p
;
170 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
174 p
->NumInstructions
= 3;
176 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
177 if (!p
->Instructions
) {
178 ctx
->Driver
.DeleteProgram(ctx
, p
);
181 _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_DEPR
;
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
;
194 /* MOV result.color, fragment.color */
195 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
196 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
197 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
198 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
199 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
203 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
205 assert(ic
== p
->NumInstructions
);
207 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
208 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
210 stfp
= (struct st_fragment_program
*) p
;
211 st_translate_fragment_program(st
, stfp
, NULL
);
219 * Create a simple vertex shader that just passes through the
220 * vertex position and texcoord (and optionally, color).
222 static struct st_vertex_program
*
223 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
225 /* only make programs once and re-use */
226 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
227 GLcontext
*ctx
= st
->ctx
;
228 struct st_vertex_program
*stvp
;
229 struct gl_program
*p
;
232 if (progs
[passColor
])
233 return progs
[passColor
];
235 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
240 p
->NumInstructions
= 4;
242 p
->NumInstructions
= 3;
244 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
245 if (!p
->Instructions
) {
246 ctx
->Driver
.DeleteProgram(ctx
, p
);
249 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
250 /* MOV result.pos, vertex.pos; */
251 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
252 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
253 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
254 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
255 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
256 /* MOV result.texcoord0, vertex.texcoord0; */
257 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
258 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
259 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
260 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
261 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
264 /* MOV result.color0, vertex.color0; */
265 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
266 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
267 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
268 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
269 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
274 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
277 assert(ic
== p
->NumInstructions
);
279 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
280 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
281 (1 << VERT_RESULT_HPOS
));
283 p
->InputsRead
|= VERT_BIT_COLOR0
;
284 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
287 stvp
= (struct st_vertex_program
*) p
;
288 st_translate_vertex_program(st
, stvp
, NULL
);
290 progs
[passColor
] = stvp
;
297 _mesa_base_format(GLenum format
)
300 case GL_DEPTH_COMPONENT
:
301 return GL_DEPTH_COMPONENT
;
302 case GL_STENCIL_INDEX
:
303 return GL_STENCIL_INDEX
;
311 * Make texture containing an image for glDrawPixels image.
312 * If 'pixels' is NULL, leave the texture image data undefined.
314 static struct pipe_texture
*
315 make_texture(struct st_context
*st
,
316 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
317 const struct gl_pixelstore_attrib
*unpack
,
318 const GLvoid
*pixels
)
320 GLcontext
*ctx
= st
->ctx
;
321 struct pipe_context
*pipe
= st
->pipe
;
322 struct pipe_screen
*screen
= pipe
->screen
;
323 const struct gl_texture_format
*mformat
;
324 struct pipe_texture
*pt
;
325 enum pipe_format pipeFormat
;
329 baseFormat
= _mesa_base_format(format
);
331 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
334 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
336 cpp
= st_sizeof_format(pipeFormat
);
338 pixels
= _mesa_map_drawpix_pbo(ctx
, unpack
, pixels
);
342 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, width
, height
,
345 _mesa_unmap_drapix_pbo(ctx
, unpack
);
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
;
391 _mesa_unmap_drapix_pbo(ctx
, unpack
);
398 * Draw textured quad.
399 * Coords are window coords with y=0=bottom.
402 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
403 GLfloat x1
, GLfloat y1
, GLboolean invertTex
)
405 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
407 GLfloat sLeft
= 0.0, sRight
= 1.0;
408 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
411 verts
[0][0][0] = x0
; /* attr[0].x */
412 verts
[0][0][1] = y0
; /* attr[0].y */
413 verts
[0][1][0] = sLeft
; /* attr[1].s */
414 verts
[0][1][1] = tTop
; /* attr[1].t */
419 verts
[1][1][0] = sRight
;
420 verts
[1][1][1] = tTop
;
425 verts
[2][1][0] = sRight
;
426 verts
[2][1][1] = tBot
;
431 verts
[3][1][0] = sLeft
;
432 verts
[3][1][1] = tBot
;
434 /* same for all verts: */
435 for (i
= 0; i
< 4; i
++) {
436 verts
[i
][0][2] = z
; /*Z*/
437 verts
[i
][0][3] = 1.0; /*W*/
438 verts
[i
][1][2] = 0.0; /*R*/
439 verts
[i
][1][3] = 1.0; /*Q*/
442 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2, GL_FALSE
);
447 draw_quad_colored(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
448 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
451 GLfloat bias
= ctx
->st
->bitmap_texcoord_bias
;
452 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
454 GLfloat xBias
= bias
/ (x1
-x0
);
455 GLfloat yBias
= bias
/ (y1
-y0
);
456 GLfloat sLeft
= 0.0 + xBias
, sRight
= 1.0 + xBias
;
457 GLfloat tTop
= invertTex
- yBias
, tBot
= 1.0 - tTop
- yBias
;
460 verts
[0][0][0] = x0
; /* attr[0].x */
461 verts
[0][0][1] = y0
; /* attr[0].y */
462 verts
[0][2][0] = sLeft
; /* attr[2].s */
463 verts
[0][2][1] = tTop
; /* attr[2].t */
468 verts
[1][2][0] = sRight
;
469 verts
[1][2][1] = tTop
;
474 verts
[2][2][0] = sRight
;
475 verts
[2][2][1] = tBot
;
480 verts
[3][2][0] = sLeft
;
481 verts
[3][2][1] = tBot
;
483 /* same for all verts: */
484 for (i
= 0; i
< 4; i
++) {
485 verts
[i
][0][2] = z
; /*Z*/
486 verts
[i
][0][3] = 1.0; /*W*/
487 verts
[i
][1][0] = color
[0];
488 verts
[i
][1][1] = color
[1];
489 verts
[i
][1][2] = color
[2];
490 verts
[i
][1][3] = color
[3];
491 verts
[i
][2][2] = 0.0; /*R*/
492 verts
[i
][2][3] = 1.0; /*Q*/
495 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 3, GL_FALSE
);
501 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
502 GLsizei width
, GLsizei height
,
503 GLfloat zoomX
, GLfloat zoomY
,
504 struct pipe_texture
*pt
,
505 struct st_vertex_program
*stvp
,
506 struct st_fragment_program
*stfp
,
507 const GLfloat
*color
,
510 struct st_context
*st
= ctx
->st
;
511 struct pipe_context
*pipe
= ctx
->st
->pipe
;
512 struct cso_context
*cso
= ctx
->st
->cso_context
;
513 GLfloat x0
, y0
, x1
, y1
;
517 /* XXX if DrawPixels image is larger than max texture size, break
520 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
521 assert(width
<= maxSize
);
522 assert(height
<= maxSize
);
524 cso_save_rasterizer(cso
);
525 cso_save_viewport(cso
);
527 /* rasterizer state: just scissor */
529 struct pipe_rasterizer_state rasterizer
;
530 memset(&rasterizer
, 0, sizeof(rasterizer
));
531 if (ctx
->Scissor
.Enabled
)
532 rasterizer
.scissor
= 1;
534 cso_set_rasterizer(cso
, &rasterizer
);
537 /* fragment shader state: TEX lookup program */
538 pipe
->bind_fs_state(pipe
, stfp
->driver_shader
);
540 /* vertex shader state: position + texcoord pass-through */
541 pipe
->bind_vs_state(pipe
, stvp
->driver_shader
);
544 /* texture sampling state: */
546 struct pipe_sampler_state sampler
;
547 memset(&sampler
, 0, sizeof(sampler
));
548 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
549 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
550 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
551 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
552 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
553 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
554 sampler
.normalized_coords
= 1;
556 cso_single_sampler(cso
, 0, &sampler
);
557 cso_single_sampler_done(cso
);
560 /* viewport state: viewport matching window dims */
562 const float width
= ctx
->DrawBuffer
->Width
;
563 const float height
= ctx
->DrawBuffer
->Height
;
564 struct pipe_viewport_state vp
;
565 vp
.scale
[0] = 0.5 * width
;
566 vp
.scale
[1] = -0.5 * height
;
569 vp
.translate
[0] = 0.5 * width
;
570 vp
.translate
[1] = 0.5 * height
;
571 vp
.translate
[2] = 0.0;
572 vp
.translate
[3] = 0.0;
573 cso_set_viewport(cso
, &vp
);
578 pipe
->set_sampler_textures(pipe
, 1, &pt
);
581 /* Compute window coords (y=0=bottom) with pixel zoom.
582 * Recall that these coords are transformed by the current
583 * vertex shader and viewport transformation.
586 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
588 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
590 /* draw textured quad */
592 draw_quad_colored(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
594 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, invertTex
);
597 cso_restore_rasterizer(cso
);
598 cso_restore_viewport(cso
);
599 /* shaders don't go through cso yet */
600 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
601 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
603 cso_set_rasterizer(cso
, &st
->state
.rasterizer
);
604 cso_set_samplers(cso
, PIPE_MAX_SAMPLERS
,
605 (const struct pipe_sampler_state
**) st
->state
.sampler_list
);
606 pipe
->set_sampler_textures(pipe
, ctx
->st
->state
.num_textures
,
607 ctx
->st
->state
.sampler_texture
);
612 * Check if a GL format/type combination is a match to the given pipe format.
613 * XXX probably move this to a re-usable place.
616 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
618 static const GLuint one
= 1;
619 GLubyte littleEndian
= *((GLubyte
*) &one
);
621 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
623 type
== GL_UNSIGNED_BYTE
&&
627 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
628 format
== GL_ABGR_EXT
&&
629 type
== GL_UNSIGNED_BYTE
&&
633 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
635 type
== GL_UNSIGNED_BYTE
&&
639 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
641 type
== GL_UNSIGNED_SHORT_5_6_5
) {
642 /* endian don't care */
645 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
647 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
648 /* endian don't care */
651 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
652 format
== GL_STENCIL_INDEX
&&
653 type
== GL_UNSIGNED_BYTE
) {
656 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
657 format
== GL_DEPTH_COMPONENT
&&
658 type
== GL_UNSIGNED_INT
) {
661 /* XXX add more cases */
669 * Check if any per-fragment ops are enabled.
670 * XXX probably move this to a re-usable place.
673 any_fragment_ops(const struct st_context
*st
)
675 if (st
->state
.depth_stencil
.alpha
.enabled
||
676 st
->state
.depth_stencil
.depth
.enabled
||
677 st
->state
.blend
.blend_enable
||
678 st
->state
.blend
.logicop_enable
)
679 /* XXX more checks */
687 * Check if any pixel transfer ops are enabled.
688 * XXX probably move this to a re-usable place.
691 any_pixel_transfer_ops(const struct st_context
*st
)
693 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
694 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
695 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
696 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
697 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
698 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
699 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
700 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
701 st
->ctx
->Pixel
.MapColorFlag
)
702 /* XXX more checks */
710 * Draw image with a blit, or other non-textured quad method.
713 draw_blit(struct st_context
*st
,
714 GLsizei width
, GLsizei height
,
715 GLenum format
, GLenum type
, const GLvoid
*pixels
)
723 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
724 GLsizei width
, GLsizei height
, GLenum type
,
725 const struct gl_pixelstore_attrib
*unpack
,
726 const GLvoid
*pixels
)
728 struct st_context
*st
= ctx
->st
;
729 struct pipe_context
*pipe
= st
->pipe
;
730 struct pipe_surface
*ps
= st
->state
.framebuffer
.zsbuf
;
731 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
735 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
737 /* map the stencil buffer */
738 stmap
= pipe_surface_map(ps
);
740 /* if width > MAX_WIDTH, have to process image in chunks */
742 while (skipPixels
< width
) {
743 const GLint spanX
= x
+ skipPixels
;
744 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
746 for (row
= 0; row
< height
; row
++) {
747 GLint spanY
= y
+ row
;
748 GLubyte values
[MAX_WIDTH
];
749 GLenum destType
= GL_UNSIGNED_BYTE
;
750 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
752 GL_COLOR_INDEX
, type
,
754 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
755 type
, source
, unpack
,
756 ctx
->_ImageTransferState
);
759 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
760 spanX, spanY, values);
764 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
765 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
768 switch (ps
->format
) {
769 case PIPE_FORMAT_U_S8
:
771 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
772 memcpy(dest
, values
, spanWidth
);
775 case PIPE_FORMAT_S8Z24_UNORM
:
777 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
779 for (k
= 0; k
< spanWidth
; k
++) {
781 p
= (p
& 0xffffff) | (values
[k
] << 24);
791 skipPixels
+= spanWidth
;
794 /* unmap the stencil buffer */
795 pipe_surface_unmap(ps
);
800 * Called via ctx->Driver.DrawPixels()
803 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
804 GLenum format
, GLenum type
,
805 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
807 struct st_fragment_program
*stfp
;
808 struct st_vertex_program
*stvp
;
809 struct st_context
*st
= ctx
->st
;
810 struct pipe_surface
*ps
;
812 const GLfloat
*color
;
814 if (format
== GL_STENCIL_INDEX
) {
815 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
819 st_validate_state(st
);
821 if (format
== GL_DEPTH_COMPONENT
) {
822 ps
= st
->state
.framebuffer
.zsbuf
;
823 stfp
= make_fragment_shader_z(ctx
->st
);
824 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
825 color
= ctx
->Current
.RasterColor
;
827 else if (format
== GL_STENCIL_INDEX
) {
828 ps
= st
->state
.framebuffer
.zsbuf
;
829 /* XXX special case - can't use texture map */
833 ps
= st
->state
.framebuffer
.cbufs
[0];
834 stfp
= combined_drawpix_fragment_program(ctx
);
835 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
839 bufferFormat
= ps
->format
;
841 if (1/*any_fragment_ops(st) ||
842 any_pixel_transfer_ops(st) ||
843 !compatible_formats(format, type, ps->format)*/) {
845 struct pipe_texture
*pt
846 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
848 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
849 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
850 pt
, stvp
, stfp
, color
, GL_FALSE
);
851 pipe_texture_reference(&pt
, NULL
);
856 draw_blit(st
, width
, height
, format
, type
, pixels
);
863 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
864 GLsizei width
, GLsizei height
,
865 GLint dstx
, GLint dsty
)
867 struct st_renderbuffer
*rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_StencilBuffer
);
868 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
869 struct pipe_surface
*psRead
= rbRead
->surface
;
870 struct pipe_surface
*psDraw
= rbDraw
->surface
;
871 ubyte
*readMap
, *drawMap
;
875 buffer
= malloc(width
* height
* sizeof(ubyte
));
877 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
881 /* map the stencil buffers */
882 readMap
= pipe_surface_map(psRead
);
883 drawMap
= pipe_surface_map(psDraw
);
885 /* this will do stencil pixel transfer ops */
886 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
887 &ctx
->DefaultPacking
, buffer
);
890 /* XXX PixelZoom not handled yet */
891 for (i
= 0; i
< height
; i
++) {
898 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
899 y
= ctx
->DrawBuffer
->Height
- y
- 1;
902 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
903 src
= buffer
+ i
* width
;
905 switch (psDraw
->format
) {
906 case PIPE_FORMAT_S8Z24_UNORM
:
908 uint
*dst4
= (uint
*) dst
;
910 for (j
= 0; j
< width
; j
++) {
911 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
916 case PIPE_FORMAT_U_S8
:
917 memcpy(dst
, src
, width
);
926 /* unmap the stencil buffers */
927 pipe_surface_unmap(psRead
);
928 pipe_surface_unmap(psDraw
);
933 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
934 GLsizei width
, GLsizei height
,
935 GLint dstx
, GLint dsty
, GLenum type
)
937 struct st_context
*st
= ctx
->st
;
938 struct pipe_context
*pipe
= st
->pipe
;
939 struct pipe_screen
*screen
= pipe
->screen
;
940 struct st_renderbuffer
*rbRead
;
941 struct st_vertex_program
*stvp
;
942 struct st_fragment_program
*stfp
;
943 struct pipe_surface
*psRead
;
944 struct pipe_surface
*psTex
;
945 struct pipe_texture
*pt
;
947 enum pipe_format srcFormat
, texFormat
;
949 /* make sure rendering has completed */
950 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
952 st_validate_state(st
);
954 if (type
== GL_STENCIL
) {
955 /* can't use texturing to do stencil */
956 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
960 if (type
== GL_COLOR
) {
961 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
963 stfp
= combined_drawpix_fragment_program(ctx
);
964 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
967 assert(type
== GL_DEPTH
);
968 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
969 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
970 stfp
= make_fragment_shader_z(ctx
->st
);
971 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
974 psRead
= rbRead
->surface
;
975 srcFormat
= psRead
->format
;
977 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE
)) {
978 texFormat
= srcFormat
;
981 /* srcFormat can't be used as a texture format */
982 if (type
== GL_DEPTH
) {
983 static const enum pipe_format zFormats
[] = {
984 PIPE_FORMAT_Z16_UNORM
,
985 PIPE_FORMAT_Z32_UNORM
,
986 PIPE_FORMAT_S8Z24_UNORM
,
987 PIPE_FORMAT_Z24S8_UNORM
991 for (i
= 0; i
< Elements(zFormats
); i
++) {
992 if (screen
->is_format_supported(screen
, zFormats
[i
],
994 texFormat
= zFormats
[i
];
998 assert(texFormat
); /* XXX no depth texture formats??? */
1006 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1007 width
, height
, 1, 0);
1011 psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0);
1013 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1014 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1017 /* For some drivers (like Xlib) it's not possible to treat the
1018 * front/back color buffers as surfaces (they're XImages and Pixmaps).
1019 * So, this var tells us if we can use surface_copy here...
1021 if (st
->haveFramebufferSurfaces
&& srcFormat
== texFormat
) {
1022 /* copy source framebuffer surface into mipmap/texture */
1023 pipe
->surface_copy(pipe
,
1028 srcx
, srcy
, width
, height
);
1030 else if (type
== GL_COLOR
) {
1031 /* alternate path using get/put_tile() */
1032 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1034 pipe_get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1035 pipe_put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1041 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1042 pipe_get_tile_z(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1043 pipe_put_tile_z(pipe
, psTex
, 0, 0, width
, height
, buf
);
1047 /* draw textured quad */
1048 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1049 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1050 pt
, stvp
, stfp
, color
, GL_TRUE
);
1052 pipe_surface_reference(&psTex
, NULL
);
1053 pipe_texture_reference(&pt
, NULL
);
1058 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1060 functions
->DrawPixels
= st_DrawPixels
;
1061 functions
->CopyPixels
= st_CopyPixels
;