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 "util/u_string.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_strings.h"
47 #include "tgsi/tgsi_ureg.h"
48 #include "tgsi/tgsi_text.h"
49 #include <stdio.h> /* include last */
54 * Make simple vertex pass-through shader.
55 * \param num_attribs number of attributes to pass through
56 * \param semantic_names array of semantic names for each attribute
57 * \param semantic_indexes array of semantic indexes for each attribute
60 util_make_vertex_passthrough_shader(struct pipe_context
*pipe
,
62 const uint
*semantic_names
,
63 const uint
*semantic_indexes
,
66 return util_make_vertex_passthrough_shader_with_so(pipe
, num_attribs
,
73 util_make_vertex_passthrough_shader_with_so(struct pipe_context
*pipe
,
75 const uint
*semantic_names
,
76 const uint
*semantic_indexes
,
78 const struct pipe_stream_output_info
*so
)
80 struct ureg_program
*ureg
;
83 ureg
= ureg_create( PIPE_SHADER_VERTEX
);
88 ureg_property(ureg
, TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
, TRUE
);
90 for (i
= 0; i
< num_attribs
; i
++) {
94 src
= ureg_DECL_vs_input( ureg
, i
);
96 dst
= ureg_DECL_output( ureg
,
100 ureg_MOV( ureg
, dst
, src
);
105 return ureg_create_shader_with_so_and_destroy( ureg
, pipe
, so
);
109 void *util_make_layered_clear_vertex_shader(struct pipe_context
*pipe
)
111 static const char text
[] =
115 "DCL SV[0], INSTANCEID\n"
116 "DCL OUT[0], POSITION\n"
117 "DCL OUT[1], GENERIC[0]\n"
118 "DCL OUT[2], LAYER\n"
120 "MOV OUT[0], IN[0]\n"
121 "MOV OUT[1], IN[1]\n"
122 "MOV OUT[2], SV[0]\n"
124 struct tgsi_token tokens
[1000];
125 struct pipe_shader_state state
;
127 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
131 pipe_shader_state_from_tgsi(&state
, tokens
);
132 return pipe
->create_vs_state(pipe
, &state
);
136 * Takes position and color, and outputs position, color, and instance id.
138 void *util_make_layered_clear_helper_vertex_shader(struct pipe_context
*pipe
)
140 static const char text
[] =
144 "DCL SV[0], INSTANCEID\n"
145 "DCL OUT[0], POSITION\n"
146 "DCL OUT[1], GENERIC[0]\n"
147 "DCL OUT[2], GENERIC[1]\n"
149 "MOV OUT[0], IN[0]\n"
150 "MOV OUT[1], IN[1]\n"
151 "MOV OUT[2].x, SV[0].xxxx\n"
153 struct tgsi_token tokens
[1000];
154 struct pipe_shader_state state
;
156 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
160 pipe_shader_state_from_tgsi(&state
, tokens
);
161 return pipe
->create_vs_state(pipe
, &state
);
165 * Takes position, color, and target layer, and emits vertices on that target
166 * layer, with the specified color.
168 void *util_make_layered_clear_geometry_shader(struct pipe_context
*pipe
)
170 static const char text
[] =
172 "PROPERTY GS_INPUT_PRIMITIVE TRIANGLES\n"
173 "PROPERTY GS_OUTPUT_PRIMITIVE TRIANGLE_STRIP\n"
174 "PROPERTY GS_MAX_OUTPUT_VERTICES 3\n"
175 "PROPERTY GS_INVOCATIONS 1\n"
176 "DCL IN[][0], POSITION\n" /* position */
177 "DCL IN[][1], GENERIC[0]\n" /* color */
178 "DCL IN[][2], GENERIC[1]\n" /* vs invocation */
179 "DCL OUT[0], POSITION\n"
180 "DCL OUT[1], GENERIC[0]\n"
181 "DCL OUT[2], LAYER\n"
182 "IMM[0] INT32 {0, 0, 0, 0}\n"
184 "MOV OUT[0], IN[0][0]\n"
185 "MOV OUT[1], IN[0][1]\n"
186 "MOV OUT[2].x, IN[0][2].xxxx\n"
188 "MOV OUT[0], IN[1][0]\n"
189 "MOV OUT[1], IN[1][1]\n"
190 "MOV OUT[2].x, IN[1][2].xxxx\n"
192 "MOV OUT[0], IN[2][0]\n"
193 "MOV OUT[1], IN[2][1]\n"
194 "MOV OUT[2].x, IN[2][2].xxxx\n"
197 struct tgsi_token tokens
[1000];
198 struct pipe_shader_state state
;
200 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
204 pipe_shader_state_from_tgsi(&state
, tokens
);
205 return pipe
->create_gs_state(pipe
, &state
);
209 * Make simple fragment texture shader:
210 * IMM {0,0,0,1} // (if writemask != 0xf)
211 * MOV TEMP[0], IMM[0] // (if writemask != 0xf)
212 * TEX TEMP[0].writemask, IN[0], SAMP[0], 2D;
213 * .. optional SINT <-> UINT clamping ..
214 * MOV OUT[0], TEMP[0]
217 * \param tex_target one of PIPE_TEXTURE_x
218 * \parma interp_mode either TGSI_INTERPOLATE_LINEAR or PERSPECTIVE
219 * \param writemask mask of TGSI_WRITEMASK_x
222 util_make_fragment_tex_shader_writemask(struct pipe_context
*pipe
,
224 unsigned interp_mode
,
226 enum tgsi_return_type stype
,
227 enum tgsi_return_type dtype
)
229 struct ureg_program
*ureg
;
230 struct ureg_src sampler
;
232 struct ureg_dst temp
;
235 assert((stype
== TGSI_RETURN_TYPE_FLOAT
) == (dtype
== TGSI_RETURN_TYPE_FLOAT
));
236 assert(interp_mode
== TGSI_INTERPOLATE_LINEAR
||
237 interp_mode
== TGSI_INTERPOLATE_PERSPECTIVE
);
239 ureg
= ureg_create( PIPE_SHADER_FRAGMENT
);
243 sampler
= ureg_DECL_sampler( ureg
, 0 );
245 ureg_DECL_sampler_view(ureg
, 0, tex_target
, stype
, stype
, stype
, stype
);
247 tex
= ureg_DECL_fs_input( ureg
,
248 TGSI_SEMANTIC_GENERIC
, 0,
251 out
= ureg_DECL_output( ureg
,
255 temp
= ureg_DECL_temporary(ureg
);
257 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
258 struct ureg_src imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
260 ureg_MOV( ureg
, out
, imm
);
263 if (tex_target
== TGSI_TEXTURE_BUFFER
)
265 ureg_writemask(temp
, writemask
),
266 tex_target
, tex
, sampler
);
269 ureg_writemask(temp
, writemask
),
270 tex_target
, tex
, sampler
);
272 if (stype
!= dtype
) {
273 if (stype
== TGSI_RETURN_TYPE_SINT
) {
274 assert(dtype
== TGSI_RETURN_TYPE_UINT
);
276 ureg_IMAX(ureg
, temp
, ureg_src(temp
), ureg_imm1i(ureg
, 0));
278 assert(stype
== TGSI_RETURN_TYPE_UINT
);
279 assert(dtype
== TGSI_RETURN_TYPE_SINT
);
281 ureg_UMIN(ureg
, temp
, ureg_src(temp
), ureg_imm1u(ureg
, (1u << 31) - 1));
285 ureg_MOV(ureg
, out
, ureg_src(temp
));
289 return ureg_create_shader_and_destroy( ureg
, pipe
);
294 * Make a simple fragment shader that sets the output color to a color
295 * taken from a texture.
296 * \param tex_target one of PIPE_TEXTURE_x
299 util_make_fragment_tex_shader(struct pipe_context
*pipe
, unsigned tex_target
,
300 unsigned interp_mode
,
301 enum tgsi_return_type stype
,
302 enum tgsi_return_type dtype
)
304 return util_make_fragment_tex_shader_writemask( pipe
,
313 * Make a simple fragment texture shader which reads an X component from
314 * a texture and writes it as depth.
317 util_make_fragment_tex_shader_writedepth(struct pipe_context
*pipe
,
319 unsigned interp_mode
)
321 struct ureg_program
*ureg
;
322 struct ureg_src sampler
;
324 struct ureg_dst out
, depth
;
327 ureg
= ureg_create( PIPE_SHADER_FRAGMENT
);
331 sampler
= ureg_DECL_sampler( ureg
, 0 );
333 ureg_DECL_sampler_view(ureg
, 0, tex_target
,
334 TGSI_RETURN_TYPE_FLOAT
,
335 TGSI_RETURN_TYPE_FLOAT
,
336 TGSI_RETURN_TYPE_FLOAT
,
337 TGSI_RETURN_TYPE_FLOAT
);
339 tex
= ureg_DECL_fs_input( ureg
,
340 TGSI_SEMANTIC_GENERIC
, 0,
343 out
= ureg_DECL_output( ureg
,
347 depth
= ureg_DECL_output( ureg
,
348 TGSI_SEMANTIC_POSITION
,
351 imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
353 ureg_MOV( ureg
, out
, imm
);
356 ureg_writemask(depth
, TGSI_WRITEMASK_Z
),
357 tex_target
, tex
, sampler
);
360 return ureg_create_shader_and_destroy( ureg
, pipe
);
365 * Make a simple fragment texture shader which reads the texture unit 0 and 1
366 * and writes it as depth and stencil, respectively.
369 util_make_fragment_tex_shader_writedepthstencil(struct pipe_context
*pipe
,
371 unsigned interp_mode
)
373 struct ureg_program
*ureg
;
374 struct ureg_src depth_sampler
, stencil_sampler
;
376 struct ureg_dst out
, depth
, stencil
;
379 ureg
= ureg_create( PIPE_SHADER_FRAGMENT
);
383 depth_sampler
= ureg_DECL_sampler( ureg
, 0 );
384 ureg_DECL_sampler_view(ureg
, 0, tex_target
,
385 TGSI_RETURN_TYPE_FLOAT
,
386 TGSI_RETURN_TYPE_FLOAT
,
387 TGSI_RETURN_TYPE_FLOAT
,
388 TGSI_RETURN_TYPE_FLOAT
);
389 stencil_sampler
= ureg_DECL_sampler( ureg
, 1 );
390 ureg_DECL_sampler_view(ureg
, 0, tex_target
,
391 TGSI_RETURN_TYPE_UINT
,
392 TGSI_RETURN_TYPE_UINT
,
393 TGSI_RETURN_TYPE_UINT
,
394 TGSI_RETURN_TYPE_UINT
);
396 tex
= ureg_DECL_fs_input( ureg
,
397 TGSI_SEMANTIC_GENERIC
, 0,
400 out
= ureg_DECL_output( ureg
,
404 depth
= ureg_DECL_output( ureg
,
405 TGSI_SEMANTIC_POSITION
,
408 stencil
= ureg_DECL_output( ureg
,
409 TGSI_SEMANTIC_STENCIL
,
412 imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
414 ureg_MOV( ureg
, out
, imm
);
417 ureg_writemask(depth
, TGSI_WRITEMASK_Z
),
418 tex_target
, tex
, depth_sampler
);
420 ureg_writemask(stencil
, TGSI_WRITEMASK_Y
),
421 tex_target
, tex
, stencil_sampler
);
424 return ureg_create_shader_and_destroy( ureg
, pipe
);
429 * Make a simple fragment texture shader which reads a texture and writes it
433 util_make_fragment_tex_shader_writestencil(struct pipe_context
*pipe
,
435 unsigned interp_mode
)
437 struct ureg_program
*ureg
;
438 struct ureg_src stencil_sampler
;
440 struct ureg_dst out
, stencil
;
443 ureg
= ureg_create( PIPE_SHADER_FRAGMENT
);
447 stencil_sampler
= ureg_DECL_sampler( ureg
, 0 );
449 ureg_DECL_sampler_view(ureg
, 0, tex_target
,
450 TGSI_RETURN_TYPE_UINT
,
451 TGSI_RETURN_TYPE_UINT
,
452 TGSI_RETURN_TYPE_UINT
,
453 TGSI_RETURN_TYPE_UINT
);
455 tex
= ureg_DECL_fs_input( ureg
,
456 TGSI_SEMANTIC_GENERIC
, 0,
459 out
= ureg_DECL_output( ureg
,
463 stencil
= ureg_DECL_output( ureg
,
464 TGSI_SEMANTIC_STENCIL
,
467 imm
= ureg_imm4f( ureg
, 0, 0, 0, 1 );
469 ureg_MOV( ureg
, out
, imm
);
472 ureg_writemask(stencil
, TGSI_WRITEMASK_Y
),
473 tex_target
, tex
, stencil_sampler
);
476 return ureg_create_shader_and_destroy( ureg
, pipe
);
481 * Make simple fragment color pass-through shader that replicates OUT[0]
482 * to all bound colorbuffers.
485 util_make_fragment_passthrough_shader(struct pipe_context
*pipe
,
487 int input_interpolate
,
488 boolean write_all_cbufs
)
490 static const char shader_templ
[] =
493 "DCL IN[0], %s[0], %s\n"
494 "DCL OUT[0], COLOR[0]\n"
496 "MOV OUT[0], IN[0]\n"
499 char text
[sizeof(shader_templ
)+100];
500 struct tgsi_token tokens
[1000];
501 struct pipe_shader_state state
;
503 sprintf(text
, shader_templ
,
504 write_all_cbufs
? "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n" : "",
505 tgsi_semantic_names
[input_semantic
],
506 tgsi_interpolate_names
[input_interpolate
]);
508 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
512 pipe_shader_state_from_tgsi(&state
, tokens
);
514 tgsi_dump(state
.tokens
, 0);
517 return pipe
->create_fs_state(pipe
, &state
);
522 util_make_empty_fragment_shader(struct pipe_context
*pipe
)
524 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
529 return ureg_create_shader_and_destroy(ureg
, pipe
);
534 * Make a fragment shader that copies the input color to N output colors.
537 util_make_fragment_cloneinput_shader(struct pipe_context
*pipe
, int num_cbufs
,
539 int input_interpolate
)
541 struct ureg_program
*ureg
;
543 struct ureg_dst dst
[PIPE_MAX_COLOR_BUFS
];
546 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
548 ureg
= ureg_create( PIPE_SHADER_FRAGMENT
);
552 src
= ureg_DECL_fs_input( ureg
, input_semantic
, 0,
555 for (i
= 0; i
< num_cbufs
; i
++)
556 dst
[i
] = ureg_DECL_output( ureg
, TGSI_SEMANTIC_COLOR
, i
);
558 for (i
= 0; i
< num_cbufs
; i
++)
559 ureg_MOV( ureg
, dst
[i
], src
);
563 return ureg_create_shader_and_destroy( ureg
, pipe
);
568 util_make_fs_blit_msaa_gen(struct pipe_context
*pipe
,
570 const char *samp_type
,
571 const char *output_semantic
,
572 const char *output_mask
,
573 const char *conversion_decl
,
574 const char *conversion
)
576 static const char shader_templ
[] =
578 "DCL IN[0], GENERIC[0], LINEAR\n"
580 "DCL SVIEW[0], %s, %s\n"
585 "F2U TEMP[0], IN[0]\n"
586 "TXF TEMP[0], TEMP[0], SAMP[0], %s\n"
588 "MOV OUT[0]%s, TEMP[0]\n"
591 const char *type
= tgsi_texture_names
[tgsi_tex
];
592 char text
[sizeof(shader_templ
)+100];
593 struct tgsi_token tokens
[1000];
594 struct pipe_shader_state state
;
596 assert(tgsi_tex
== TGSI_TEXTURE_2D_MSAA
||
597 tgsi_tex
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
599 util_snprintf(text
, sizeof(text
), shader_templ
, type
, samp_type
,
600 output_semantic
, conversion_decl
, type
, conversion
, output_mask
);
602 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
607 pipe_shader_state_from_tgsi(&state
, tokens
);
609 tgsi_dump(state
.tokens
, 0);
612 return pipe
->create_fs_state(pipe
, &state
);
617 * Make a fragment shader that sets the output color to a color
618 * fetched from a multisample texture.
619 * \param tex_target one of PIPE_TEXTURE_x
622 util_make_fs_blit_msaa_color(struct pipe_context
*pipe
,
624 enum tgsi_return_type stype
,
625 enum tgsi_return_type dtype
)
627 const char *samp_type
;
628 const char *conversion_decl
= "";
629 const char *conversion
= "";
631 if (stype
== TGSI_RETURN_TYPE_UINT
) {
634 if (dtype
== TGSI_RETURN_TYPE_SINT
) {
635 conversion_decl
= "IMM[0] UINT32 {2147483647, 0, 0, 0}\n";
636 conversion
= "UMIN TEMP[0], TEMP[0], IMM[0].x\n";
638 } else if (stype
== TGSI_RETURN_TYPE_SINT
) {
641 if (dtype
== TGSI_RETURN_TYPE_UINT
) {
642 conversion_decl
= "IMM[0] INT32 {0, 0, 0, 0}\n";
643 conversion
= "IMAX TEMP[0], TEMP[0], IMM[0].x\n";
646 assert(dtype
== TGSI_RETURN_TYPE_FLOAT
);
650 return util_make_fs_blit_msaa_gen(pipe
, tgsi_tex
, samp_type
,
651 "COLOR[0]", "", conversion_decl
,
657 * Make a fragment shader that sets the output depth to a depth value
658 * fetched from a multisample texture.
659 * \param tex_target one of PIPE_TEXTURE_x
662 util_make_fs_blit_msaa_depth(struct pipe_context
*pipe
,
665 return util_make_fs_blit_msaa_gen(pipe
, tgsi_tex
, "FLOAT",
666 "POSITION", ".z", "", "");
671 * Make a fragment shader that sets the output stencil to a stencil value
672 * fetched from a multisample texture.
673 * \param tex_target one of PIPE_TEXTURE_x
676 util_make_fs_blit_msaa_stencil(struct pipe_context
*pipe
,
679 return util_make_fs_blit_msaa_gen(pipe
, tgsi_tex
, "UINT",
680 "STENCIL", ".y", "", "");
685 * Make a fragment shader that sets the output depth and stencil to depth
686 * and stencil values fetched from two multisample textures / samplers.
687 * The sizes of both textures should match (it should be one depth-stencil
689 * \param tex_target one of PIPE_TEXTURE_x
692 util_make_fs_blit_msaa_depthstencil(struct pipe_context
*pipe
,
695 static const char shader_templ
[] =
697 "DCL IN[0], GENERIC[0], LINEAR\n"
699 "DCL SVIEW[0..1], %s, FLOAT\n"
700 "DCL OUT[0], POSITION\n"
701 "DCL OUT[1], STENCIL\n"
704 "F2U TEMP[0], IN[0]\n"
705 "TXF OUT[0].z, TEMP[0], SAMP[0], %s\n"
706 "TXF OUT[1].y, TEMP[0], SAMP[1], %s\n"
709 const char *type
= tgsi_texture_names
[tgsi_tex
];
710 char text
[sizeof(shader_templ
)+100];
711 struct tgsi_token tokens
[1000];
712 struct pipe_shader_state state
;
714 assert(tgsi_tex
== TGSI_TEXTURE_2D_MSAA
||
715 tgsi_tex
== TGSI_TEXTURE_2D_ARRAY_MSAA
);
717 sprintf(text
, shader_templ
, type
, type
, type
);
719 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
723 pipe_shader_state_from_tgsi(&state
, tokens
);
725 tgsi_dump(state
.tokens
, 0);
728 return pipe
->create_fs_state(pipe
, &state
);
733 util_make_fs_msaa_resolve(struct pipe_context
*pipe
,
734 unsigned tgsi_tex
, unsigned nr_samples
,
735 enum tgsi_return_type stype
)
737 struct ureg_program
*ureg
;
738 struct ureg_src sampler
, coord
;
739 struct ureg_dst out
, tmp_sum
, tmp_coord
, tmp
;
742 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
747 sampler
= ureg_DECL_sampler(ureg
, 0);
748 ureg_DECL_sampler_view(ureg
, 0, tgsi_tex
, stype
, stype
, stype
, stype
);
749 coord
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_GENERIC
, 0,
750 TGSI_INTERPOLATE_LINEAR
);
751 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
752 tmp_sum
= ureg_DECL_temporary(ureg
);
753 tmp_coord
= ureg_DECL_temporary(ureg
);
754 tmp
= ureg_DECL_temporary(ureg
);
757 ureg_MOV(ureg
, tmp_sum
, ureg_imm1f(ureg
, 0));
758 ureg_F2U(ureg
, tmp_coord
, coord
);
760 for (i
= 0; i
< nr_samples
; i
++) {
761 /* Read one sample. */
762 ureg_MOV(ureg
, ureg_writemask(tmp_coord
, TGSI_WRITEMASK_W
),
763 ureg_imm1u(ureg
, i
));
764 ureg_TXF(ureg
, tmp
, tgsi_tex
, ureg_src(tmp_coord
), sampler
);
766 if (stype
== TGSI_RETURN_TYPE_UINT
)
767 ureg_U2F(ureg
, tmp
, ureg_src(tmp
));
768 else if (stype
== TGSI_RETURN_TYPE_SINT
)
769 ureg_I2F(ureg
, tmp
, ureg_src(tmp
));
771 /* Add it to the sum.*/
772 ureg_ADD(ureg
, tmp_sum
, ureg_src(tmp_sum
), ureg_src(tmp
));
775 /* Calculate the average and return. */
776 ureg_MUL(ureg
, tmp_sum
, ureg_src(tmp_sum
),
777 ureg_imm1f(ureg
, 1.0 / nr_samples
));
779 if (stype
== TGSI_RETURN_TYPE_UINT
)
780 ureg_F2U(ureg
, out
, ureg_src(tmp_sum
));
781 else if (stype
== TGSI_RETURN_TYPE_SINT
)
782 ureg_F2I(ureg
, out
, ureg_src(tmp_sum
));
784 ureg_MOV(ureg
, out
, ureg_src(tmp_sum
));
788 return ureg_create_shader_and_destroy(ureg
, pipe
);
793 util_make_fs_msaa_resolve_bilinear(struct pipe_context
*pipe
,
794 unsigned tgsi_tex
, unsigned nr_samples
,
795 enum tgsi_return_type stype
)
797 struct ureg_program
*ureg
;
798 struct ureg_src sampler
, coord
;
799 struct ureg_dst out
, tmp
, top
, bottom
;
800 struct ureg_dst tmp_coord
[4], tmp_sum
[4];
803 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
808 sampler
= ureg_DECL_sampler(ureg
, 0);
809 ureg_DECL_sampler_view(ureg
, 0, tgsi_tex
, stype
, stype
, stype
, stype
);
810 coord
= ureg_DECL_fs_input(ureg
, TGSI_SEMANTIC_GENERIC
, 0,
811 TGSI_INTERPOLATE_LINEAR
);
812 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
813 for (c
= 0; c
< 4; c
++)
814 tmp_sum
[c
] = ureg_DECL_temporary(ureg
);
815 for (c
= 0; c
< 4; c
++)
816 tmp_coord
[c
] = ureg_DECL_temporary(ureg
);
817 tmp
= ureg_DECL_temporary(ureg
);
818 top
= ureg_DECL_temporary(ureg
);
819 bottom
= ureg_DECL_temporary(ureg
);
822 for (c
= 0; c
< 4; c
++)
823 ureg_MOV(ureg
, tmp_sum
[c
], ureg_imm1f(ureg
, 0));
825 /* Get 4 texture coordinates for the bilinear filter. */
826 ureg_F2U(ureg
, tmp_coord
[0], coord
); /* top-left */
827 ureg_UADD(ureg
, tmp_coord
[1], ureg_src(tmp_coord
[0]),
828 ureg_imm4u(ureg
, 1, 0, 0, 0)); /* top-right */
829 ureg_UADD(ureg
, tmp_coord
[2], ureg_src(tmp_coord
[0]),
830 ureg_imm4u(ureg
, 0, 1, 0, 0)); /* bottom-left */
831 ureg_UADD(ureg
, tmp_coord
[3], ureg_src(tmp_coord
[0]),
832 ureg_imm4u(ureg
, 1, 1, 0, 0)); /* bottom-right */
834 for (i
= 0; i
< nr_samples
; i
++) {
835 for (c
= 0; c
< 4; c
++) {
836 /* Read one sample. */
837 ureg_MOV(ureg
, ureg_writemask(tmp_coord
[c
], TGSI_WRITEMASK_W
),
838 ureg_imm1u(ureg
, i
));
839 ureg_TXF(ureg
, tmp
, tgsi_tex
, ureg_src(tmp_coord
[c
]), sampler
);
841 if (stype
== TGSI_RETURN_TYPE_UINT
)
842 ureg_U2F(ureg
, tmp
, ureg_src(tmp
));
843 else if (stype
== TGSI_RETURN_TYPE_SINT
)
844 ureg_I2F(ureg
, tmp
, ureg_src(tmp
));
846 /* Add it to the sum.*/
847 ureg_ADD(ureg
, tmp_sum
[c
], ureg_src(tmp_sum
[c
]), ureg_src(tmp
));
851 /* Calculate the average. */
852 for (c
= 0; c
< 4; c
++)
853 ureg_MUL(ureg
, tmp_sum
[c
], ureg_src(tmp_sum
[c
]),
854 ureg_imm1f(ureg
, 1.0 / nr_samples
));
856 /* Take the 4 average values and apply a standard bilinear filter. */
857 ureg_FRC(ureg
, tmp
, coord
);
860 ureg_scalar(ureg_src(tmp
), 0),
861 ureg_src(tmp_sum
[1]),
862 ureg_src(tmp_sum
[0]));
864 ureg_LRP(ureg
, bottom
,
865 ureg_scalar(ureg_src(tmp
), 0),
866 ureg_src(tmp_sum
[3]),
867 ureg_src(tmp_sum
[2]));
870 ureg_scalar(ureg_src(tmp
), 1),
874 /* Convert to the texture format and return. */
875 if (stype
== TGSI_RETURN_TYPE_UINT
)
876 ureg_F2U(ureg
, out
, ureg_src(tmp
));
877 else if (stype
== TGSI_RETURN_TYPE_SINT
)
878 ureg_F2I(ureg
, out
, ureg_src(tmp
));
880 ureg_MOV(ureg
, out
, ureg_src(tmp
));
884 return ureg_create_shader_and_destroy(ureg
, pipe
);
888 util_make_geometry_passthrough_shader(struct pipe_context
*pipe
,
890 const ubyte
*semantic_names
,
891 const ubyte
*semantic_indexes
)
893 static const unsigned zero
[4] = {0, 0, 0, 0};
895 struct ureg_program
*ureg
;
896 struct ureg_dst dst
[PIPE_MAX_SHADER_OUTPUTS
];
897 struct ureg_src src
[PIPE_MAX_SHADER_INPUTS
];
902 ureg
= ureg_create(PIPE_SHADER_GEOMETRY
);
906 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
, PIPE_PRIM_POINTS
);
907 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
, PIPE_PRIM_POINTS
);
908 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
, 1);
909 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
, 1);
910 imm
= ureg_DECL_immediate_uint(ureg
, zero
, 4);
913 * Loop over all the attribs and declare the corresponding
914 * declarations in the geometry shader
916 for (i
= 0; i
< num_attribs
; i
++) {
917 src
[i
] = ureg_DECL_input(ureg
, semantic_names
[i
],
918 semantic_indexes
[i
], 0, 1);
919 src
[i
] = ureg_src_dimension(src
[i
], 0);
920 dst
[i
] = ureg_DECL_output(ureg
, semantic_names
[i
], semantic_indexes
[i
]);
923 /* MOV dst[i] src[i] */
924 for (i
= 0; i
< num_attribs
; i
++) {
925 ureg_MOV(ureg
, dst
[i
], src
[i
]);
929 ureg_insn(ureg
, TGSI_OPCODE_EMIT
, NULL
, 0, &imm
, 1);
934 return ureg_create_shader_and_destroy(ureg
, pipe
);