Merge branch 'glsl2-head' into glsl2
[mesa.git] / src / gallium / auxiliary / util / u_simple_shaders.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * Simple vertex/fragment shader generators.
32 *
33 * @author Brian Paul
34 Marek Olšák
35 */
36
37
38 #include "pipe/p_context.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "util/u_simple_shaders.h"
41 #include "util/u_debug.h"
42 #include "tgsi/tgsi_ureg.h"
43
44
45
46 /**
47 * Make simple vertex pass-through shader.
48 * \param num_attribs number of attributes to pass through
49 * \param semantic_names array of semantic names for each attribute
50 * \param semantic_indexes array of semantic indexes for each attribute
51 */
52 void *
53 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
54 uint num_attribs,
55 const uint *semantic_names,
56 const uint *semantic_indexes)
57 {
58 struct ureg_program *ureg;
59 uint i;
60
61 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
62 if (ureg == NULL)
63 return NULL;
64
65 for (i = 0; i < num_attribs; i++) {
66 struct ureg_src src;
67 struct ureg_dst dst;
68
69 src = ureg_DECL_vs_input( ureg, i );
70
71 dst = ureg_DECL_output( ureg,
72 semantic_names[i],
73 semantic_indexes[i]);
74
75 ureg_MOV( ureg, dst, src );
76 }
77
78 ureg_END( ureg );
79
80 return ureg_create_shader_and_destroy( ureg, pipe );
81 }
82
83
84 /**
85 * Make simple fragment texture shader:
86 * IMM {0,0,0,1} // (if writemask != 0xf)
87 * MOV OUT[0], IMM[0] // (if writemask != 0xf)
88 * TEX OUT[0].writemask, IN[0], SAMP[0], 2D;
89 * END;
90 *
91 * \param tex_target one of PIPE_TEXTURE_x
92 * \parma interp_mode either TGSI_INTERPOLATE_LINEAR or PERSPECTIVE
93 * \param writemask mask of TGSI_WRITEMASK_x
94 */
95 void *
96 util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
97 unsigned tex_target,
98 unsigned interp_mode,
99 unsigned writemask )
100 {
101 struct ureg_program *ureg;
102 struct ureg_src sampler;
103 struct ureg_src tex;
104 struct ureg_dst out;
105
106 assert(interp_mode == TGSI_INTERPOLATE_LINEAR ||
107 interp_mode == TGSI_INTERPOLATE_PERSPECTIVE);
108
109 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
110 if (ureg == NULL)
111 return NULL;
112
113 sampler = ureg_DECL_sampler( ureg, 0 );
114
115 tex = ureg_DECL_fs_input( ureg,
116 TGSI_SEMANTIC_GENERIC, 0,
117 interp_mode );
118
119 out = ureg_DECL_output( ureg,
120 TGSI_SEMANTIC_COLOR,
121 0 );
122
123 if (writemask != TGSI_WRITEMASK_XYZW) {
124 struct ureg_src imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
125
126 ureg_MOV( ureg, out, imm );
127 }
128
129 ureg_TEX( ureg,
130 ureg_writemask(out, writemask),
131 tex_target, tex, sampler );
132 ureg_END( ureg );
133
134 return ureg_create_shader_and_destroy( ureg, pipe );
135 }
136
137
138 /**
139 * Make a simple fragment shader that sets the output color to a color
140 * taken from a texture.
141 * \param tex_target one of PIPE_TEXTURE_x
142 */
143 void *
144 util_make_fragment_tex_shader(struct pipe_context *pipe, unsigned tex_target,
145 unsigned interp_mode)
146 {
147 return util_make_fragment_tex_shader_writemask( pipe,
148 tex_target,
149 interp_mode,
150 TGSI_WRITEMASK_XYZW );
151 }
152
153
154 /**
155 * Make a simple fragment texture shader which reads an X component from
156 * a texture and writes it as depth.
157 */
158 void *
159 util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
160 unsigned tex_target,
161 unsigned interp_mode)
162 {
163 struct ureg_program *ureg;
164 struct ureg_src sampler;
165 struct ureg_src tex;
166 struct ureg_dst out, depth;
167 struct ureg_src imm;
168
169 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
170 if (ureg == NULL)
171 return NULL;
172
173 sampler = ureg_DECL_sampler( ureg, 0 );
174
175 tex = ureg_DECL_fs_input( ureg,
176 TGSI_SEMANTIC_GENERIC, 0,
177 interp_mode );
178
179 out = ureg_DECL_output( ureg,
180 TGSI_SEMANTIC_COLOR,
181 0 );
182
183 depth = ureg_DECL_output( ureg,
184 TGSI_SEMANTIC_POSITION,
185 0 );
186
187 imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
188
189 ureg_MOV( ureg, out, imm );
190
191 ureg_TEX( ureg,
192 ureg_writemask(depth, TGSI_WRITEMASK_Z),
193 tex_target, tex, sampler );
194 ureg_END( ureg );
195
196 return ureg_create_shader_and_destroy( ureg, pipe );
197 }
198
199
200 /**
201 * Make simple fragment color pass-through shader.
202 */
203 void *
204 util_make_fragment_passthrough_shader(struct pipe_context *pipe)
205 {
206 return util_make_fragment_clonecolor_shader(pipe, 1);
207 }
208
209
210 /**
211 * Make a fragment shader that copies the input color to N output colors.
212 */
213 void *
214 util_make_fragment_clonecolor_shader(struct pipe_context *pipe, int num_cbufs)
215 {
216 struct ureg_program *ureg;
217 struct ureg_src src;
218 struct ureg_dst dst[PIPE_MAX_COLOR_BUFS];
219 int i;
220
221 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
222
223 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
224 if (ureg == NULL)
225 return NULL;
226
227 src = ureg_DECL_fs_input( ureg, TGSI_SEMANTIC_COLOR, 0,
228 TGSI_INTERPOLATE_PERSPECTIVE );
229
230 for (i = 0; i < num_cbufs; i++)
231 dst[i] = ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, i );
232
233 for (i = 0; i < num_cbufs; i++)
234 ureg_MOV( ureg, dst[i], src );
235
236 ureg_END( ureg );
237
238 return ureg_create_shader_and_destroy( ureg, pipe );
239 }