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"
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 "shader/prog_instruction.h"
63 * Check if the given program is:
64 * 0: MOVE result.color, fragment.color;
68 is_passthrough_program(const struct gl_fragment_program
*prog
)
70 if (prog
->Base
.NumInstructions
== 2) {
71 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
72 if (inst
[0].Opcode
== OPCODE_MOV
&&
73 inst
[1].Opcode
== OPCODE_END
&&
74 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
75 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
76 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
77 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
78 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
79 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
88 * Make fragment program for glBitmap:
89 * Sample the texture and kill the fragment if the bit is 0.
90 * This program will be combined with the user's fragment program.
92 static struct st_fragment_program
*
93 make_bitmap_fragment_program(GLcontext
*ctx
)
95 struct st_fragment_program
*stfp
;
99 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
103 p
->NumInstructions
= 5;
105 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
106 if (!p
->Instructions
) {
107 ctx
->Driver
.DeleteProgram(ctx
, p
);
110 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
112 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
113 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
114 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
115 p
->Instructions
[ic
].DstReg
.Index
= 0;
116 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
117 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
118 p
->Instructions
[ic
].TexSrcUnit
= 0;
119 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
122 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
123 p
->Instructions
[ic
].Opcode
= OPCODE_SWZ
;
124 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
125 p
->Instructions
[ic
].DstReg
.Index
= 0;
126 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
127 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
128 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
129 p
->Instructions
[ic
].SrcReg
[0].Swizzle
130 = MAKE_SWIZZLE4(SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
);
133 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */
134 p
->Instructions
[ic
].Opcode
= OPCODE_SUB
;
135 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
136 p
->Instructions
[ic
].DstReg
.Index
= 0;
137 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
138 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
139 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
140 p
->Instructions
[ic
].SrcReg
[1].File
= PROGRAM_TEMPORARY
;
141 p
->Instructions
[ic
].SrcReg
[1].Index
= 0;
142 p
->Instructions
[ic
].SrcReg
[1].Swizzle
= SWIZZLE_XXXX
; /* 1.0 */
145 /* KIL if tmp0 < 0 */
146 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
147 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
148 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
152 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
154 assert(ic
== p
->NumInstructions
);
156 p
->InputsRead
= FRAG_BIT_TEX0
;
157 p
->OutputsWritten
= 0x0;
159 stfp
= (struct st_fragment_program
*) p
;
160 st_translate_fragment_program(ctx
->st
, stfp
, NULL
,
161 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
168 * Combine basic bitmap fragment program with the user-defined program.
170 static struct st_fragment_program
*
171 combined_bitmap_fragment_program(GLcontext
*ctx
)
173 struct st_context
*st
= ctx
->st
;
174 struct st_fragment_program
*stfp
;
176 if (!st
->bitmap
.program
) {
177 /* create the basic bitmap fragment program */
178 st
->bitmap
.program
= make_bitmap_fragment_program(ctx
);
181 if (st
->bitmap
.user_prog_sn
== st
->fp
->serialNo
) {
183 stfp
= st
->bitmap
.combined_prog
;
186 /* Concatenate the bitmap program with the current user-defined program.
188 stfp
= (struct st_fragment_program
*)
189 _mesa_combine_programs(ctx
,
190 &st
->bitmap
.program
->Base
.Base
,
195 struct gl_program
*p
= &stfp
->Base
.Base
;
196 printf("Combined bitmap program:\n");
197 _mesa_print_program(p
);
198 printf("InputsRead: 0x%x\n", p
->InputsRead
);
199 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
200 _mesa_print_parameter_list(p
->Parameters
);
204 /* translate to TGSI tokens */
205 st_translate_fragment_program(st
, stfp
, NULL
,
206 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
208 /* save new program, update serial numbers */
209 st
->bitmap
.user_prog_sn
= st
->fp
->serialNo
;
210 st
->bitmap
.combined_prog
= stfp
;
213 /* Ideally we'd have updated the pipe constants during the normal
214 * st/atom mechanism. But we can't since this is specific to glBitmap.
216 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
224 * Make fragment shader for glDraw/CopyPixels. This shader is made
225 * by combining the pixel transfer shader with the user-defined shader.
227 static struct st_fragment_program
*
228 combined_drawpix_fragment_program(GLcontext
*ctx
)
230 struct st_context
*st
= ctx
->st
;
231 struct st_fragment_program
*stfp
;
233 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
234 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
235 /* the pixel tranfer program has not changed and the user-defined
236 * program has not changed, so re-use the combined program.
238 stfp
= st
->pixel_xfer
.combined_prog
;
241 /* Concatenate the pixel transfer program with the current user-
244 if (is_passthrough_program(&st
->fp
->Base
)) {
245 stfp
= (struct st_fragment_program
*)
246 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
249 stfp
= (struct st_fragment_program
*)
250 _mesa_combine_programs(ctx
,
251 &st
->pixel_xfer
.program
->Base
.Base
,
257 struct gl_program
*p
= &stfp
->Base
.Base
;
258 printf("Combined DrawPixels program:\n");
259 _mesa_print_program(p
);
260 printf("InputsRead: 0x%x\n", p
->InputsRead
);
261 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
262 _mesa_print_parameter_list(p
->Parameters
);
266 /* translate to TGSI tokens */
267 st_translate_fragment_program(st
, stfp
, NULL
,
268 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
270 /* save new program, update serial numbers */
271 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
272 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
273 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
274 st
->pixel_xfer
.combined_prog
= stfp
;
277 /* Ideally we'd have updated the pipe constants during the normal
278 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
280 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
287 * Create fragment shader that does a TEX() instruction to get a Z
288 * value, then writes to FRAG_RESULT_DEPR.
289 * Pass fragment color through as-is.
291 static struct st_fragment_program
*
292 make_fragment_shader_z(struct st_context
*st
)
294 GLcontext
*ctx
= st
->ctx
;
295 /* only make programs once and re-use */
296 static struct st_fragment_program
*stfp
= NULL
;
297 struct gl_program
*p
;
303 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
307 p
->NumInstructions
= 3;
309 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
310 if (!p
->Instructions
) {
311 ctx
->Driver
.DeleteProgram(ctx
, p
);
314 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
316 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
317 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
318 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
319 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPR
;
320 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
321 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
322 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
323 p
->Instructions
[ic
].TexSrcUnit
= 0;
324 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
327 /* MOV result.color, fragment.color */
328 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
329 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
330 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
331 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
332 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
336 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
338 assert(ic
== p
->NumInstructions
);
340 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
341 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
343 stfp
= (struct st_fragment_program
*) p
;
344 st_translate_fragment_program(st
, stfp
, NULL
,
345 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
353 * Create a simple vertex shader that just passes through the
354 * vertex position and texcoord (and optionally, color).
356 static struct st_vertex_program
*
357 make_vertex_shader(struct st_context
*st
, GLboolean passColor
)
359 /* only make programs once and re-use */
360 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
361 GLcontext
*ctx
= st
->ctx
;
362 struct st_vertex_program
*stvp
;
363 struct gl_program
*p
;
366 if (progs
[passColor
])
367 return progs
[passColor
];
369 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
374 p
->NumInstructions
= 4;
376 p
->NumInstructions
= 3;
378 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
379 if (!p
->Instructions
) {
380 ctx
->Driver
.DeleteProgram(ctx
, p
);
383 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
384 /* MOV result.pos, vertex.pos; */
385 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
386 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
387 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
388 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
389 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
390 /* MOV result.texcoord0, vertex.texcoord0; */
391 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
392 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
393 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
394 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
395 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
398 /* MOV result.color0, vertex.color0; */
399 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
400 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
401 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
402 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
403 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
408 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
411 assert(ic
== p
->NumInstructions
);
413 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
414 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
415 (1 << VERT_RESULT_HPOS
));
417 p
->InputsRead
|= VERT_BIT_COLOR0
;
418 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
421 stvp
= (struct st_vertex_program
*) p
;
422 st_translate_vertex_program(st
, stvp
, NULL
,
423 stvp
->tokens
, ST_MAX_SHADER_TOKENS
);
425 progs
[passColor
] = stvp
;
432 _mesa_base_format(GLenum format
)
435 case GL_DEPTH_COMPONENT
:
436 return GL_DEPTH_COMPONENT
;
437 case GL_STENCIL_INDEX
:
438 return GL_STENCIL_INDEX
;
446 * Make texture containing an image for glDrawPixels image.
447 * If 'pixels' is NULL, leave the texture image data undefined.
449 static struct pipe_texture
*
450 make_texture(struct st_context
*st
,
451 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
452 const struct gl_pixelstore_attrib
*unpack
,
453 const GLvoid
*pixels
)
455 GLcontext
*ctx
= st
->ctx
;
456 struct pipe_context
*pipe
= st
->pipe
;
457 const struct gl_texture_format
*mformat
;
458 struct pipe_texture
*pt
;
459 enum pipe_format pipeFormat
;
463 baseFormat
= _mesa_base_format(format
);
465 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
468 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
470 cpp
= st_sizeof_format(pipeFormat
);
472 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, 0, width
, height
,
477 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
479 pt->region = buffer_object_region(unpack->BufferObj);
481 printf("st_DrawPixels (sourcing from PBO not implemented yet)\n");
485 struct pipe_surface
*surface
;
486 static const GLuint dstImageOffsets
= 0;
489 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
491 /* we'll do pixel transfer in a fragment shader */
492 ctx
->_ImageTransferState
= 0x0;
494 surface
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
496 /* map texture surface */
497 dest
= pipe_surface_map(surface
);
499 /* Put image into texture surface.
500 * Note that the image is actually going to be upside down in
501 * the texture. We deal with that with texcoords.
503 success
= mformat
->StoreImage(ctx
, 2, /* dims */
504 baseFormat
, /* baseInternalFormat */
505 mformat
, /* gl_texture_format */
507 0, 0, 0, /* dstX/Y/Zoffset */
508 surface
->pitch
* cpp
, /* dstRowStride, bytes */
509 &dstImageOffsets
, /* dstImageOffsets */
510 width
, height
, 1, /* size */
511 format
, type
, /* src format/type */
512 pixels
, /* data source */
516 pipe_surface_unmap(surface
);
517 pipe_surface_reference(&surface
, NULL
);
521 ctx
->_ImageTransferState
= imageTransferStateSave
;
529 * Draw textured quad.
530 * Coords are window coords with y=0=bottom.
533 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
534 GLfloat x1
, GLfloat y1
, GLboolean invertTex
)
536 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
538 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
541 verts
[0][0][0] = x0
; /* attr[0].x */
542 verts
[0][0][1] = y0
; /* attr[0].x */
543 verts
[0][1][0] = 0.0; /* attr[1].s */
544 verts
[0][1][1] = tTop
; /* attr[1].t */
549 verts
[1][1][0] = 1.0;
550 verts
[1][1][1] = tTop
;
555 verts
[2][1][0] = 1.0;
556 verts
[2][1][1] = tBot
;
561 verts
[3][1][0] = 0.0;
562 verts
[3][1][1] = tBot
;
564 /* same for all verts: */
565 for (i
= 0; i
< 4; i
++) {
566 verts
[i
][0][2] = z
; /*Z*/
567 verts
[i
][0][3] = 1.0; /*W*/
568 verts
[i
][1][2] = 0.0; /*R*/
569 verts
[i
][1][3] = 1.0; /*Q*/
572 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2);
577 draw_quad_colored(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
578 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
581 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
583 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
586 verts
[0][0][0] = x0
; /* attr[0].x */
587 verts
[0][0][1] = y0
; /* attr[0].y */
588 verts
[0][2][0] = 0.0; /* attr[2].s */
589 verts
[0][2][1] = tTop
; /* attr[2].t */
594 verts
[1][2][0] = 1.0;
595 verts
[1][2][1] = tTop
;
600 verts
[2][2][0] = 1.0;
601 verts
[2][2][1] = tBot
;
606 verts
[3][2][0] = 0.0;
607 verts
[3][2][1] = tBot
;
609 /* same for all verts: */
610 for (i
= 0; i
< 4; i
++) {
611 verts
[i
][0][2] = z
; /*Z*/
612 verts
[i
][0][3] = 1.0; /*W*/
613 verts
[i
][1][0] = color
[0];
614 verts
[i
][1][1] = color
[1];
615 verts
[i
][1][2] = color
[2];
616 verts
[i
][1][3] = color
[3];
617 verts
[i
][2][2] = 0.0; /*R*/
618 verts
[i
][2][3] = 1.0; /*Q*/
621 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 3);
627 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
628 GLsizei width
, GLsizei height
,
629 GLfloat zoomX
, GLfloat zoomY
,
630 struct pipe_texture
*pt
,
631 struct st_vertex_program
*stvp
,
632 struct st_fragment_program
*stfp
,
633 const GLfloat
*color
,
636 const GLuint unit
= 0;
637 struct pipe_context
*pipe
= ctx
->st
->pipe
;
638 GLfloat x0
, y0
, x1
, y1
;
642 /* XXX if DrawPixels image is larger than max texture size, break
645 maxSize
= 1 << (pipe
->get_param(pipe
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
646 assert(width
<= maxSize
);
647 assert(height
<= maxSize
);
649 /* setup state: just scissor */
651 struct pipe_rasterizer_state setup
;
652 const struct cso_rasterizer
*cso
;
653 memset(&setup
, 0, sizeof(setup
));
654 if (ctx
->Scissor
.Enabled
)
656 cso
= st_cached_rasterizer_state(ctx
->st
, &setup
);
657 pipe
->bind_rasterizer_state(pipe
, cso
->data
);
660 /* fragment shader state: TEX lookup program */
661 pipe
->bind_fs_state(pipe
, stfp
->fs
->data
);
663 /* vertex shader state: position + texcoord pass-through */
664 pipe
->bind_vs_state(pipe
, stvp
->vs
->data
);
666 /* texture sampling state: */
668 struct pipe_sampler_state sampler
;
669 const struct cso_sampler
*cso
;
670 memset(&sampler
, 0, sizeof(sampler
));
671 sampler
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
672 sampler
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
673 sampler
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
674 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
675 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
676 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
677 sampler
.normalized_coords
= 1;
678 cso
= st_cached_sampler_state(ctx
->st
, &sampler
);
679 pipe
->bind_sampler_state(pipe
, unit
, cso
->data
);
682 /* viewport state: viewport matching window dims */
684 const float width
= ctx
->DrawBuffer
->Width
;
685 const float height
= ctx
->DrawBuffer
->Height
;
686 struct pipe_viewport_state vp
;
687 vp
.scale
[0] = 0.5 * width
;
688 vp
.scale
[1] = -0.5 * height
;
691 vp
.translate
[0] = 0.5 * width
;
692 vp
.translate
[1] = 0.5 * height
;
693 vp
.translate
[2] = 0.0;
694 vp
.translate
[3] = 0.0;
695 pipe
->set_viewport_state(pipe
, &vp
);
700 pipe
->set_sampler_texture(pipe
, unit
, pt
);
703 /* Compute window coords (y=0=bottom) with pixel zoom.
704 * Recall that these coords are transformed by the current
705 * vertex shader and viewport transformation.
708 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
710 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
712 /* draw textured quad */
714 draw_quad_colored(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
716 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, invertTex
);
718 /* restore GL state */
719 pipe
->bind_rasterizer_state(pipe
, ctx
->st
->state
.rasterizer
->data
);
720 pipe
->bind_fs_state(pipe
, ctx
->st
->state
.fs
->data
);
721 pipe
->bind_vs_state(pipe
, ctx
->st
->state
.vs
->data
);
722 pipe
->set_sampler_texture(pipe
, unit
, ctx
->st
->state
.sampler_texture
[unit
]);
723 pipe
->bind_sampler_state(pipe
, unit
, ctx
->st
->state
.sampler
[unit
]->data
);
724 pipe
->set_viewport_state(pipe
, &ctx
->st
->state
.viewport
);
729 * Check if a GL format/type combination is a match to the given pipe format.
730 * XXX probably move this to a re-usable place.
733 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
735 static const GLuint one
= 1;
736 GLubyte littleEndian
= *((GLubyte
*) &one
);
738 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
740 type
== GL_UNSIGNED_BYTE
&&
744 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
745 format
== GL_ABGR_EXT
&&
746 type
== GL_UNSIGNED_BYTE
&&
750 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
752 type
== GL_UNSIGNED_BYTE
&&
756 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
758 type
== GL_UNSIGNED_SHORT_5_6_5
) {
759 /* endian don't care */
762 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
764 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
765 /* endian don't care */
768 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
769 format
== GL_STENCIL_INDEX
&&
770 type
== GL_UNSIGNED_BYTE
) {
773 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
774 format
== GL_DEPTH_COMPONENT
&&
775 type
== GL_UNSIGNED_INT
) {
778 /* XXX add more cases */
786 * Check if any per-fragment ops are enabled.
787 * XXX probably move this to a re-usable place.
790 any_fragment_ops(const struct st_context
*st
)
792 if (st
->state
.depth_stencil
->state
.alpha
.enabled
||
793 st
->state
.blend
->state
.blend_enable
||
794 st
->state
.blend
->state
.logicop_enable
||
795 st
->state
.depth_stencil
->state
.depth
.enabled
)
796 /* XXX more checks */
804 * Check if any pixel transfer ops are enabled.
805 * XXX probably move this to a re-usable place.
808 any_pixel_transfer_ops(const struct st_context
*st
)
810 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
811 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
812 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
813 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
814 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
815 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
816 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
817 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
818 st
->ctx
->Pixel
.MapColorFlag
)
819 /* XXX more checks */
827 * Draw image with a blit, or other non-textured quad method.
830 draw_blit(struct st_context
*st
,
831 GLsizei width
, GLsizei height
,
832 GLenum format
, GLenum type
, const GLvoid
*pixels
)
840 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
841 GLsizei width
, GLsizei height
, GLenum type
,
842 const struct gl_pixelstore_attrib
*unpack
,
843 const GLvoid
*pixels
)
845 struct st_context
*st
= ctx
->st
;
846 struct pipe_context
*pipe
= st
->pipe
;
847 struct pipe_surface
*ps
= st
->state
.framebuffer
.sbuf
;
848 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
852 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
854 /* map the stencil buffer */
855 stmap
= pipe_surface_map(ps
);
857 /* if width > MAX_WIDTH, have to process image in chunks */
859 while (skipPixels
< width
) {
860 const GLint spanX
= x
+ skipPixels
;
861 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
863 for (row
= 0; row
< height
; row
++) {
864 GLint spanY
= y
+ row
;
865 GLubyte values
[MAX_WIDTH
];
866 GLenum destType
= GL_UNSIGNED_BYTE
;
867 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
869 GL_COLOR_INDEX
, type
,
871 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
872 type
, source
, unpack
,
873 ctx
->_ImageTransferState
);
876 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
877 spanX, spanY, values);
881 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
882 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
885 switch (ps
->format
) {
886 case PIPE_FORMAT_U_S8
:
888 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
889 memcpy(dest
, values
, spanWidth
);
892 case PIPE_FORMAT_S8Z24_UNORM
:
894 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
896 for (k
= 0; k
< spanWidth
; k
++) {
898 p
= (p
& 0xffffff) | (values
[k
] << 24);
908 skipPixels
+= spanWidth
;
911 /* unmap the stencil buffer */
912 pipe_surface_unmap(ps
);
917 * Called via ctx->Driver.DrawPixels()
920 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
921 GLenum format
, GLenum type
,
922 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
924 struct st_fragment_program
*stfp
;
925 struct st_vertex_program
*stvp
;
926 struct st_context
*st
= ctx
->st
;
927 struct pipe_surface
*ps
;
929 const GLfloat
*color
;
931 if (format
== GL_STENCIL_INDEX
) {
932 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
936 st_validate_state(st
);
938 if (format
== GL_DEPTH_COMPONENT
) {
939 ps
= st
->state
.framebuffer
.zbuf
;
940 stfp
= make_fragment_shader_z(ctx
->st
);
941 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
942 color
= ctx
->Current
.RasterColor
;
944 else if (format
== GL_STENCIL_INDEX
) {
945 ps
= st
->state
.framebuffer
.sbuf
;
946 /* XXX special case - can't use texture map */
950 ps
= st
->state
.framebuffer
.cbufs
[0];
951 stfp
= combined_drawpix_fragment_program(ctx
);
952 stvp
= make_vertex_shader(ctx
->st
, GL_FALSE
);
956 bufferFormat
= ps
->format
;
958 if (any_fragment_ops(st
) ||
959 any_pixel_transfer_ops(st
) ||
960 !compatible_formats(format
, type
, ps
->format
)) {
962 struct pipe_texture
*pt
963 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
965 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
966 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
967 pt
, stvp
, stfp
, color
, GL_FALSE
);
968 st
->pipe
->texture_release(st
->pipe
, &pt
);
973 draw_blit(st
, width
, height
, format
, type
, pixels
);
980 * Create a texture which represents a bitmap image.
982 static struct pipe_texture
*
983 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
984 const struct gl_pixelstore_attrib
*unpack
,
985 const GLubyte
*bitmap
)
987 struct pipe_context
*pipe
= ctx
->st
->pipe
;
988 struct pipe_surface
*surface
;
989 uint format
= 0, cpp
, comp
;
991 struct pipe_texture
*pt
;
994 /* find a texture format we know */
995 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
, PIPE_TEXTURE
)) {
996 format
= PIPE_FORMAT_U_I8
;
1000 else if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, PIPE_TEXTURE
)) {
1001 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
1003 comp
= 3; /* alpha channel */ /*XXX little-endian dependency */
1006 /* XXX support more formats */
1013 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, 0, width
, height
,
1018 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
1020 pt->region = buffer_object_region(unpack->BufferObj);
1022 printf("st_Bitmap (sourcing from PBO not implemented yet)\n");
1025 surface
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
1027 /* map texture surface */
1028 dest
= pipe_surface_map(surface
);
1030 /* Put image into texture surface.
1031 * Note that the image is actually going to be upside down in
1032 * the texture. We deal with that with texcoords.
1035 for (row
= 0; row
< height
; row
++) {
1036 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
1037 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
1038 ubyte
*destRow
= dest
+ row
* surface
->pitch
* cpp
;
1040 if (unpack
->LsbFirst
) {
1042 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1043 for (col
= 0; col
< width
; col
++) {
1045 /* set texel to 255 if bit is set */
1046 destRow
[comp
] = (*src
& mask
) ? 255 : 0;
1058 /* get ready for next row */
1064 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1065 for (col
= 0; col
< width
; col
++) {
1067 /* set texel to 255 if bit is set */
1068 destRow
[comp
] =(*src
& mask
) ? 255 : 0;
1080 /* get ready for next row */
1087 /* Release surface */
1088 pipe_surface_unmap(surface
);
1089 pipe_surface_reference(&surface
, NULL
);
1091 pt
->format
= format
;
1099 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1100 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
1102 struct st_fragment_program
*stfp
;
1103 struct st_vertex_program
*stvp
;
1104 struct st_context
*st
= ctx
->st
;
1105 struct pipe_texture
*pt
;
1107 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1108 stfp
= combined_bitmap_fragment_program(ctx
);
1110 st_validate_state(st
);
1112 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
1114 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1115 width
, height
, 1.0, 1.0,
1117 ctx
->Current
.RasterColor
, GL_FALSE
);
1119 st
->pipe
->texture_release(st
->pipe
, &pt
);
1125 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1126 GLsizei width
, GLsizei height
,
1127 GLint dstx
, GLint dsty
)
1129 struct st_renderbuffer
*rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_StencilBuffer
);
1130 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1131 struct pipe_surface
*psRead
= rbRead
->surface
;
1132 struct pipe_surface
*psDraw
= rbDraw
->surface
;
1133 ubyte
*readMap
, *drawMap
;
1137 buffer
= malloc(width
* height
* sizeof(ubyte
));
1139 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1143 /* map the stencil buffers */
1144 readMap
= pipe_surface_map(psRead
);
1145 drawMap
= pipe_surface_map(psDraw
);
1147 /* this will do stencil pixel transfer ops */
1148 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
1149 &ctx
->DefaultPacking
, buffer
);
1152 /* XXX PixelZoom not handled yet */
1153 for (i
= 0; i
< height
; i
++) {
1160 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1161 y
= ctx
->DrawBuffer
->Height
- y
- 1;
1164 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
1165 src
= buffer
+ i
* width
;
1167 switch (psDraw
->format
) {
1168 case PIPE_FORMAT_S8Z24_UNORM
:
1170 uint
*dst4
= (uint
*) dst
;
1172 for (j
= 0; j
< width
; j
++) {
1173 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1178 case PIPE_FORMAT_U_S8
:
1179 memcpy(dst
, src
, width
);
1188 /* unmap the stencil buffers */
1189 pipe_surface_unmap(psRead
);
1190 pipe_surface_unmap(psDraw
);
1195 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1196 GLsizei width
, GLsizei height
,
1197 GLint dstx
, GLint dsty
, GLenum type
)
1199 struct st_context
*st
= ctx
->st
;
1200 struct pipe_context
*pipe
= st
->pipe
;
1201 struct st_renderbuffer
*rbRead
;
1202 struct st_vertex_program
*stvp
;
1203 struct st_fragment_program
*stfp
;
1204 struct pipe_surface
*psRead
;
1205 struct pipe_surface
*psTex
;
1206 struct pipe_texture
*pt
;
1210 /* make sure rendering has completed */
1211 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
1213 st_validate_state(st
);
1215 if (type
== GL_STENCIL
) {
1216 /* can't use texturing to do stencil */
1217 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1221 if (type
== GL_COLOR
) {
1222 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
1224 stfp
= combined_drawpix_fragment_program(ctx
);
1225 stvp
= make_vertex_shader(ctx
->st
, GL_FALSE
);
1228 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1229 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1230 stfp
= make_fragment_shader_z(ctx
->st
);
1231 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1234 psRead
= rbRead
->surface
;
1235 format
= psRead
->format
;
1237 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0, 0, width
, height
,
1242 psTex
= pipe
->get_tex_surface(pipe
, pt
, 0, 0, 0);
1244 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1245 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1248 /* For some drivers (like Xlib) it's not possible to treat the
1249 * front/back color buffers as surfaces (they're XImages and Pixmaps).
1250 * So, this var tells us if we can use surface_copy here...
1252 if (st
->haveFramebufferSurfaces
) {
1253 /* copy source framebuffer surface into mipmap/texture */
1254 pipe
->surface_copy(pipe
,
1258 srcx
, srcy
, width
, height
);
1261 /* alternate path using get/put_tile() */
1262 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1264 (void) pipe_surface_map(psRead
);
1265 (void) pipe_surface_map(psTex
);
1267 pipe
->get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1268 pipe
->put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1270 pipe_surface_unmap(psRead
);
1271 pipe_surface_unmap(psTex
);
1276 /* draw textured quad */
1277 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1278 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1279 pt
, stvp
, stfp
, color
, GL_TRUE
);
1281 pipe_surface_reference(&psTex
, NULL
);
1282 st
->pipe
->texture_release(st
->pipe
, &pt
);
1287 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1289 functions
->DrawPixels
= st_DrawPixels
;
1290 functions
->CopyPixels
= st_CopyPixels
;
1291 functions
->Bitmap
= st_Bitmap
;