1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 "tgsi/tgsi_ureg.h"
48 * Make simple vertex pass-through shader.
49 * \param num_attribs number of attributes to pass through
50 * \param semantic_names array of semantic names for each attribute
51 * \param semantic_indexes array of semantic indexes for each attribute
54 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
56 const uint *semantic_names,
57 const uint *semantic_indexes)
59 return util_make_vertex_passthrough_shader_with_so(pipe, num_attribs,
61 semantic_indexes, NULL);
65 util_make_vertex_passthrough_shader_with_so(struct pipe_context *pipe,
67 const uint *semantic_names,
68 const uint *semantic_indexes,
69 const struct pipe_stream_output_info *so)
71 struct ureg_program *ureg;
74 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
78 for (i = 0; i < num_attribs; i++) {
82 src = ureg_DECL_vs_input( ureg, i );
84 dst = ureg_DECL_output( ureg,
88 ureg_MOV( ureg, dst, src );
93 return ureg_create_shader_with_so_and_destroy( ureg, pipe, so );
98 * Make simple fragment texture shader:
99 * IMM {0,0,0,1} // (if writemask != 0xf)
100 * MOV OUT[0], IMM[0] // (if writemask != 0xf)
101 * TEX OUT[0].writemask, IN[0], SAMP[0], 2D;
104 * \param tex_target one of PIPE_TEXTURE_x
105 * \parma interp_mode either TGSI_INTERPOLATE_LINEAR or PERSPECTIVE
106 * \param writemask mask of TGSI_WRITEMASK_x
109 util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
111 unsigned interp_mode,
114 struct ureg_program *ureg;
115 struct ureg_src sampler;
119 assert(interp_mode == TGSI_INTERPOLATE_LINEAR ||
120 interp_mode == TGSI_INTERPOLATE_PERSPECTIVE);
122 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
126 sampler = ureg_DECL_sampler( ureg, 0 );
128 tex = ureg_DECL_fs_input( ureg,
129 TGSI_SEMANTIC_GENERIC, 0,
132 out = ureg_DECL_output( ureg,
136 if (writemask != TGSI_WRITEMASK_XYZW) {
137 struct ureg_src imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
139 ureg_MOV( ureg, out, imm );
143 ureg_writemask(out, writemask),
144 tex_target, tex, sampler );
147 return ureg_create_shader_and_destroy( ureg, pipe );
152 * Make a simple fragment shader that sets the output color to a color
153 * taken from a texture.
154 * \param tex_target one of PIPE_TEXTURE_x
157 util_make_fragment_tex_shader(struct pipe_context *pipe, unsigned tex_target,
158 unsigned interp_mode)
160 return util_make_fragment_tex_shader_writemask( pipe,
163 TGSI_WRITEMASK_XYZW );
168 * Make a simple fragment texture shader which reads an X component from
169 * a texture and writes it as depth.
172 util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
174 unsigned interp_mode)
176 struct ureg_program *ureg;
177 struct ureg_src sampler;
179 struct ureg_dst out, depth;
182 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
186 sampler = ureg_DECL_sampler( ureg, 0 );
188 tex = ureg_DECL_fs_input( ureg,
189 TGSI_SEMANTIC_GENERIC, 0,
192 out = ureg_DECL_output( ureg,
196 depth = ureg_DECL_output( ureg,
197 TGSI_SEMANTIC_POSITION,
200 imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
202 ureg_MOV( ureg, out, imm );
205 ureg_writemask(depth, TGSI_WRITEMASK_Z),
206 tex_target, tex, sampler );
209 return ureg_create_shader_and_destroy( ureg, pipe );
214 * Make simple fragment color pass-through shader.
217 util_make_fragment_passthrough_shader(struct pipe_context *pipe)
219 return util_make_fragment_cloneinput_shader(pipe, 1, TGSI_SEMANTIC_COLOR,
220 TGSI_INTERPOLATE_PERSPECTIVE);
225 * Make a fragment shader that copies the input color to N output colors.
228 util_make_fragment_cloneinput_shader(struct pipe_context *pipe, int num_cbufs,
230 int input_interpolate)
232 struct ureg_program *ureg;
234 struct ureg_dst dst[PIPE_MAX_COLOR_BUFS];
237 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
239 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
243 src = ureg_DECL_fs_input( ureg, input_semantic, 0,
246 for (i = 0; i < num_cbufs; i++)
247 dst[i] = ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, i );
249 for (i = 0; i < num_cbufs; i++)
250 ureg_MOV( ureg, dst[i], src );
254 return ureg_create_shader_and_destroy( ureg, pipe );