1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
5 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * Simple vertex/fragment shader generators.
38 #include "pipe/p_context.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "pipe/p_state.h"
41 #include "util/u_simple_shaders.h"
42 #include "util/u_debug.h"
43 #include "util/u_memory.h"
44 #include "tgsi/tgsi_dump.h"
45 #include "tgsi/tgsi_strings.h"
46 #include "tgsi/tgsi_ureg.h"
47 #include "tgsi/tgsi_text.h"
48 #include <stdio.h> /* include last */
53 * Make simple vertex pass-through shader.
54 * \param num_attribs number of attributes to pass through
55 * \param semantic_names array of semantic names for each attribute
56 * \param semantic_indexes array of semantic indexes for each attribute
59 util_make_vertex_passthrough_shader(struct pipe_context
*pipe
,
61 const uint
*semantic_names
,
62 const uint
*semantic_indexes
,
65 return util_make_vertex_passthrough_shader_with_so(pipe
, num_attribs
,
72 util_make_vertex_passthrough_shader_with_so(struct pipe_context
*pipe
,
74 const uint
*semantic_names
,
75 const uint
*semantic_indexes
,
77 const struct pipe_stream_output_info
*so
)
79 struct ureg_program
*ureg
;
82 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
87 ureg_property(ureg
, TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
, TRUE
);
89 for (i
= 0; i
< num_attribs
; i
++) {
93 src
= ureg_DECL_vs_input( ureg
, i
);
95 dst
= ureg_DECL_output( ureg
,
99 ureg_MOV( ureg
, dst
, src
);
104 return ureg_create_shader_with_so_and_destroy( ureg
, pipe
, so
);
108 void *util_make_layered_clear_vertex_shader(struct pipe_context
*pipe
)
110 static const char text
[] =
114 "DCL SV[0], INSTANCEID\n"
115 "DCL OUT[0], POSITION\n"
116 "DCL OUT[1], GENERIC[0]\n"
117 "DCL OUT[2], LAYER\n"
119 "MOV OUT[0], IN[0]\n"
120 "MOV OUT[1], IN[1]\n"
121 "MOV OUT[2], SV[0]\n"
123 struct tgsi_token tokens
[1000];
124 struct pipe_shader_state state
= {tokens
};
126 if (!tgsi_text_translate(text
, tokens
, Elements(tokens
))) {
130 return pipe
->create_vs_state(pipe
, &state
);
135 * Make simple fragment texture shader:
136 * IMM {0,0,0,1} // (if writemask != 0xf)
137 * MOV OUT[0], IMM[0] // (if writemask != 0xf)
138 * TEX OUT[0].writemask, IN[0], SAMP[0], 2D;
141 * \param tex_target one of PIPE_TEXTURE_x
142 * \parma interp_mode either TGSI_INTERPOLATE_LINEAR or PERSPECTIVE
143 * \param writemask mask of TGSI_WRITEMASK_x
146 util_make_fragment_tex_shader_writemask(struct pipe_context
*pipe
,
148 unsigned interp_mode
,
151 struct ureg_program
*ureg
;
152 struct ureg_src sampler
;
156 assert(interp_mode
== TGSI_INTERPOLATE_LINEAR
||
157 interp_mode
== TGSI_INTERPOLATE_PERSPECTIVE
);
159 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
163 sampler
= ureg_DECL_sampler( ureg
, 0 );
165 tex
= ureg_DECL_fs_input( ureg
,
166 TGSI_SEMANTIC_GENERIC
, 0,
169 out
= ureg_DECL_output( ureg
,
173 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
174 struct ureg_src imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
176 ureg_MOV( ureg
, out
, imm
);
180 ureg_writemask(out
, writemask
),
181 tex_target
, tex
, sampler
);
184 return ureg_create_shader_and_destroy( ureg
, pipe
);
189 * Make a simple fragment shader that sets the output color to a color
190 * taken from a texture.
191 * \param tex_target one of PIPE_TEXTURE_x
194 util_make_fragment_tex_shader(struct pipe_context
*pipe
, unsigned tex_target
,
195 unsigned interp_mode
)
197 return util_make_fragment_tex_shader_writemask( pipe
,
200 TGSI_WRITEMASK_XYZW
);
205 * Make a simple fragment texture shader which reads an X component from
206 * a texture and writes it as depth.
209 util_make_fragment_tex_shader_writedepth(struct pipe_context
*pipe
,
211 unsigned interp_mode
)
213 struct ureg_program
*ureg
;
214 struct ureg_src sampler
;
216 struct ureg_dst out
, depth
;
219 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
223 sampler
= ureg_DECL_sampler( ureg
, 0 );
225 tex
= ureg_DECL_fs_input( ureg
,
226 TGSI_SEMANTIC_GENERIC
, 0,
229 out
= ureg_DECL_output( ureg
,
233 depth
= ureg_DECL_output( ureg
,
234 TGSI_SEMANTIC_POSITION
,
237 imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
239 ureg_MOV( ureg
, out
, imm
);
242 ureg_writemask(depth
, TGSI_WRITEMASK_Z
),
243 tex_target
, tex
, sampler
);
246 return ureg_create_shader_and_destroy( ureg
, pipe
);
251 * Make a simple fragment texture shader which reads the texture unit 0 and 1
252 * and writes it as depth and stencil, respectively.
255 util_make_fragment_tex_shader_writedepthstencil(struct pipe_context
*pipe
,
257 unsigned interp_mode
)
259 struct ureg_program
*ureg
;
260 struct ureg_src depth_sampler
, stencil_sampler
;
262 struct ureg_dst out
, depth
, stencil
;
265 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
269 depth_sampler
= ureg_DECL_sampler( ureg
, 0 );
270 stencil_sampler
= ureg_DECL_sampler( ureg
, 1 );
272 tex
= ureg_DECL_fs_input( ureg
,
273 TGSI_SEMANTIC_GENERIC
, 0,
276 out
= ureg_DECL_output( ureg
,
280 depth
= ureg_DECL_output( ureg
,
281 TGSI_SEMANTIC_POSITION
,
284 stencil
= ureg_DECL_output( ureg
,
285 TGSI_SEMANTIC_STENCIL
,
288 imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
290 ureg_MOV( ureg
, out
, imm
);
293 ureg_writemask(depth
, TGSI_WRITEMASK_Z
),
294 tex_target
, tex
, depth_sampler
);
296 ureg_writemask(stencil
, TGSI_WRITEMASK_Y
),
297 tex_target
, tex
, stencil_sampler
);
300 return ureg_create_shader_and_destroy( ureg
, pipe
);
305 * Make a simple fragment texture shader which reads a texture and writes it
309 util_make_fragment_tex_shader_writestencil(struct pipe_context
*pipe
,
311 unsigned interp_mode
)
313 struct ureg_program
*ureg
;
314 struct ureg_src stencil_sampler
;
316 struct ureg_dst out
, stencil
;
319 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
323 stencil_sampler
= ureg_DECL_sampler( ureg
, 0 );
325 tex
= ureg_DECL_fs_input( ureg
,
326 TGSI_SEMANTIC_GENERIC
, 0,
329 out
= ureg_DECL_output( ureg
,
333 stencil
= ureg_DECL_output( ureg
,
334 TGSI_SEMANTIC_STENCIL
,
337 imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
339 ureg_MOV( ureg
, out
, imm
);
342 ureg_writemask(stencil
, TGSI_WRITEMASK_Y
),
343 tex_target
, tex
, stencil_sampler
);
346 return ureg_create_shader_and_destroy( ureg
, pipe
);
351 * Make simple fragment color pass-through shader that replicates OUT[0]
352 * to all bound colorbuffers.
355 util_make_fragment_passthrough_shader(struct pipe_context
*pipe
,
357 int input_interpolate
,
358 boolean write_all_cbufs
)
360 static const char shader_templ
[] =
363 "DCL IN[0], %s[0], %s\n"
364 "DCL OUT[0], COLOR[0]\n"
366 "MOV OUT[0], IN[0]\n"
369 char text
[sizeof(shader_templ
)+100];
370 struct tgsi_token tokens
[1000];
371 struct pipe_shader_state state
= {tokens
};
373 sprintf(text
, shader_templ
,
374 write_all_cbufs
? "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n" : "",
375 tgsi_semantic_names
[input_semantic
],
376 tgsi_interpolate_names
[input_interpolate
]);
378 if (!tgsi_text_translate(text
, tokens
, Elements(tokens
))) {
383 tgsi_dump(state
.tokens
, 0);
386 return pipe
->create_fs_state(pipe
, &state
);
391 util_make_empty_fragment_shader(struct pipe_context
*pipe
)
393 struct ureg_program
*ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
398 return ureg_create_shader_and_destroy(ureg
, pipe
);
403 * Make a fragment shader that copies the input color to N output colors.
406 util_make_fragment_cloneinput_shader(struct pipe_context
*pipe
, int num_cbufs
,
408 int input_interpolate
)
410 struct ureg_program
*ureg
;
412 struct ureg_dst dst
[PIPE_MAX_COLOR_BUFS
];
415 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
417 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
421 src
= ureg_DECL_fs_input( ureg
, input_semantic
, 0,
424 for (i
= 0; i
< num_cbufs
; i
++)
425 dst
[i
] = ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, i
);
427 for (i
= 0; i
< num_cbufs
; i
++)
428 ureg_MOV( ureg
, dst
[i
], src
);
432 return ureg_create_shader_and_destroy( ureg
, pipe
);
437 util_make_fs_blit_msaa_gen(struct pipe_context
*pipe
,
439 const char *output_semantic
,
440 const char *output_mask
)
442 static const char shader_templ
[] =
444 "DCL IN[0], GENERIC[0], LINEAR\n"
449 "F2U TEMP[0], IN[0]\n"
450 "TXF OUT[0]%s, TEMP[0], SAMP[0], %s\n"
453 const char *type
= tgsi_texture_names
[tgsi_tex
];
454 char text
[sizeof(shader_templ
)+100];
455 struct tgsi_token tokens
[1000];
456 struct pipe_shader_state state
= {tokens
};
458 assert(tgsi_tex
== TGSI_TEXTURE_2D_MSAA
||
459 tgsi_tex
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
461 sprintf(text
, shader_templ
, output_semantic
, output_mask
, type
);
463 if (!tgsi_text_translate(text
, tokens
, Elements(tokens
))) {
469 tgsi_dump(state
.tokens
, 0);
472 return pipe
->create_fs_state(pipe
, &state
);
477 * Make a fragment shader that sets the output color to a color
478 * fetched from a multisample texture.
479 * \param tex_target one of PIPE_TEXTURE_x
482 util_make_fs_blit_msaa_color(struct pipe_context
*pipe
,
485 return util_make_fs_blit_msaa_gen(pipe
, tgsi_tex
,
491 * Make a fragment shader that sets the output depth to a depth value
492 * fetched from a multisample texture.
493 * \param tex_target one of PIPE_TEXTURE_x
496 util_make_fs_blit_msaa_depth(struct pipe_context
*pipe
,
499 return util_make_fs_blit_msaa_gen(pipe
, tgsi_tex
,
505 * Make a fragment shader that sets the output stencil to a stencil value
506 * fetched from a multisample texture.
507 * \param tex_target one of PIPE_TEXTURE_x
510 util_make_fs_blit_msaa_stencil(struct pipe_context
*pipe
,
513 return util_make_fs_blit_msaa_gen(pipe
, tgsi_tex
,
519 * Make a fragment shader that sets the output depth and stencil to depth
520 * and stencil values fetched from two multisample textures / samplers.
521 * The sizes of both textures should match (it should be one depth-stencil
523 * \param tex_target one of PIPE_TEXTURE_x
526 util_make_fs_blit_msaa_depthstencil(struct pipe_context
*pipe
,
529 static const char shader_templ
[] =
531 "DCL IN[0], GENERIC[0], LINEAR\n"
533 "DCL OUT[0], POSITION\n"
534 "DCL OUT[1], STENCIL\n"
537 "F2U TEMP[0], IN[0]\n"
538 "TXF OUT[0].z, TEMP[0], SAMP[0], %s\n"
539 "TXF OUT[1].y, TEMP[0], SAMP[1], %s\n"
542 const char *type
= tgsi_texture_names
[tgsi_tex
];
543 char text
[sizeof(shader_templ
)+100];
544 struct tgsi_token tokens
[1000];
545 struct pipe_shader_state state
= {tokens
};
547 assert(tgsi_tex
== TGSI_TEXTURE_2D_MSAA
||
548 tgsi_tex
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
550 sprintf(text
, shader_templ
, type
, type
);
552 if (!tgsi_text_translate(text
, tokens
, Elements(tokens
))) {
557 tgsi_dump(state
.tokens
, 0);
560 return pipe
->create_fs_state(pipe
, &state
);
565 util_make_fs_msaa_resolve(struct pipe_context
*pipe
,
566 unsigned tgsi_tex
, unsigned nr_samples
,
567 boolean is_uint
, boolean is_sint
)
569 struct ureg_program
*ureg
;
570 struct ureg_src sampler
, coord
;
571 struct ureg_dst out
, tmp_sum
, tmp_coord
, tmp
;
574 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
579 sampler
= ureg_DECL_sampler(ureg
, 0);
580 coord
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_GENERIC
, 0,
581 TGSI_INTERPOLATE_LINEAR
);
582 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
583 tmp_sum
= ureg_DECL_temporary(ureg
);
584 tmp_coord
= ureg_DECL_temporary(ureg
);
585 tmp
= ureg_DECL_temporary(ureg
);
588 ureg_MOV(ureg
, tmp_sum
, ureg_imm1f(ureg
, 0));
589 ureg_F2U(ureg
, tmp_coord
, coord
);
591 for (i
= 0; i
< nr_samples
; i
++) {
592 /* Read one sample. */
593 ureg_MOV(ureg
, ureg_writemask(tmp_coord
, TGSI_WRITEMASK_W
),
594 ureg_imm1u(ureg
, i
));
595 ureg_TXF(ureg
, tmp
, tgsi_tex
, ureg_src(tmp_coord
), sampler
);
598 ureg_U2F(ureg
, tmp
, ureg_src(tmp
));
600 ureg_I2F(ureg
, tmp
, ureg_src(tmp
));
602 /* Add it to the sum.*/
603 ureg_ADD(ureg
, tmp_sum
, ureg_src(tmp_sum
), ureg_src(tmp
));
606 /* Calculate the average and return. */
607 ureg_MUL(ureg
, tmp_sum
, ureg_src(tmp_sum
),
608 ureg_imm1f(ureg
, 1.0 / nr_samples
));
611 ureg_F2U(ureg
, out
, ureg_src(tmp_sum
));
613 ureg_F2I(ureg
, out
, ureg_src(tmp_sum
));
615 ureg_MOV(ureg
, out
, ureg_src(tmp_sum
));
619 return ureg_create_shader_and_destroy(ureg
, pipe
);
624 util_make_fs_msaa_resolve_bilinear(struct pipe_context
*pipe
,
625 unsigned tgsi_tex
, unsigned nr_samples
,
626 boolean is_uint
, boolean is_sint
)
628 struct ureg_program
*ureg
;
629 struct ureg_src sampler
, coord
;
630 struct ureg_dst out
, tmp
, top
, bottom
;
631 struct ureg_dst tmp_coord
[4], tmp_sum
[4];
634 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
639 sampler
= ureg_DECL_sampler(ureg
, 0);
640 coord
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_GENERIC
, 0,
641 TGSI_INTERPOLATE_LINEAR
);
642 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
643 for (c
= 0; c
< 4; c
++)
644 tmp_sum
[c
] = ureg_DECL_temporary(ureg
);
645 for (c
= 0; c
< 4; c
++)
646 tmp_coord
[c
] = ureg_DECL_temporary(ureg
);
647 tmp
= ureg_DECL_temporary(ureg
);
648 top
= ureg_DECL_temporary(ureg
);
649 bottom
= ureg_DECL_temporary(ureg
);
652 for (c
= 0; c
< 4; c
++)
653 ureg_MOV(ureg
, tmp_sum
[c
], ureg_imm1f(ureg
, 0));
655 /* Get 4 texture coordinates for the bilinear filter. */
656 ureg_F2U(ureg
, tmp_coord
[0], coord
); /* top-left */
657 ureg_UADD(ureg
, tmp_coord
[1], ureg_src(tmp_coord
[0]),
658 ureg_imm4u(ureg
, 1, 0, 0, 0)); /* top-right */
659 ureg_UADD(ureg
, tmp_coord
[2], ureg_src(tmp_coord
[0]),
660 ureg_imm4u(ureg
, 0, 1, 0, 0)); /* bottom-left */
661 ureg_UADD(ureg
, tmp_coord
[3], ureg_src(tmp_coord
[0]),
662 ureg_imm4u(ureg
, 1, 1, 0, 0)); /* bottom-right */
664 for (i
= 0; i
< nr_samples
; i
++) {
665 for (c
= 0; c
< 4; c
++) {
666 /* Read one sample. */
667 ureg_MOV(ureg
, ureg_writemask(tmp_coord
[c
], TGSI_WRITEMASK_W
),
668 ureg_imm1u(ureg
, i
));
669 ureg_TXF(ureg
, tmp
, tgsi_tex
, ureg_src(tmp_coord
[c
]), sampler
);
672 ureg_U2F(ureg
, tmp
, ureg_src(tmp
));
674 ureg_I2F(ureg
, tmp
, ureg_src(tmp
));
676 /* Add it to the sum.*/
677 ureg_ADD(ureg
, tmp_sum
[c
], ureg_src(tmp_sum
[c
]), ureg_src(tmp
));
681 /* Calculate the average. */
682 for (c
= 0; c
< 4; c
++)
683 ureg_MUL(ureg
, tmp_sum
[c
], ureg_src(tmp_sum
[c
]),
684 ureg_imm1f(ureg
, 1.0 / nr_samples
));
686 /* Take the 4 average values and apply a standard bilinear filter. */
687 ureg_FRC(ureg
, tmp
, coord
);
690 ureg_scalar(ureg_src(tmp
), 0),
691 ureg_src(tmp_sum
[1]),
692 ureg_src(tmp_sum
[0]));
694 ureg_LRP(ureg
, bottom
,
695 ureg_scalar(ureg_src(tmp
), 0),
696 ureg_src(tmp_sum
[3]),
697 ureg_src(tmp_sum
[2]));
700 ureg_scalar(ureg_src(tmp
), 1),
704 /* Convert to the texture format and return. */
706 ureg_F2U(ureg
, out
, ureg_src(tmp
));
708 ureg_F2I(ureg
, out
, ureg_src(tmp
));
710 ureg_MOV(ureg
, out
, ureg_src(tmp
));
714 return ureg_create_shader_and_destroy(ureg
, pipe
);