Merge branch 'master' of ssh://people.freedesktop.org/~jbarnes/mesa
[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 "tgsi/tgsi_ureg.h"
42
43
44
45 /**
46 * Make simple vertex pass-through shader.
47 * \param num_attribs number of attributes to pass through
48 * \param semantic_names array of semantic names for each attribute
49 * \param semantic_indexes array of semantic indexes for each attribute
50 */
51 void *
52 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
53 uint num_attribs,
54 const uint *semantic_names,
55 const uint *semantic_indexes)
56 {
57 struct ureg_program *ureg;
58 uint i;
59
60 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
61 if (ureg == NULL)
62 return NULL;
63
64 for (i = 0; i < num_attribs; i++) {
65 struct ureg_src src;
66 struct ureg_dst dst;
67
68 src = ureg_DECL_vs_input( ureg, i );
69
70 dst = ureg_DECL_output( ureg,
71 semantic_names[i],
72 semantic_indexes[i]);
73
74 ureg_MOV( ureg, dst, src );
75 }
76
77 ureg_END( ureg );
78
79 return ureg_create_shader_and_destroy( ureg, pipe );
80 }
81
82
83 /**
84 * Make simple fragment texture shader:
85 * IMM {0,0,0,1} // (if writemask != 0xf)
86 * MOV OUT[0], IMM[0] // (if writemask != 0xf)
87 * TEX OUT[0].writemask, IN[0], SAMP[0], 2D;
88 * END;
89 */
90 void *
91 util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
92 unsigned tex_target,
93 unsigned writemask )
94 {
95 struct ureg_program *ureg;
96 struct ureg_src sampler;
97 struct ureg_src tex;
98 struct ureg_dst out;
99
100 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
101 if (ureg == NULL)
102 return NULL;
103
104 sampler = ureg_DECL_sampler( ureg, 0 );
105
106 tex = ureg_DECL_fs_input( ureg,
107 TGSI_SEMANTIC_GENERIC, 0,
108 TGSI_INTERPOLATE_PERSPECTIVE );
109
110 out = ureg_DECL_output( ureg,
111 TGSI_SEMANTIC_COLOR,
112 0 );
113
114 if (writemask != TGSI_WRITEMASK_XYZW) {
115 struct ureg_src imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
116
117 ureg_MOV( ureg, out, imm );
118 }
119
120 ureg_TEX( ureg,
121 ureg_writemask(out, writemask),
122 tex_target, tex, sampler );
123 ureg_END( ureg );
124
125 return ureg_create_shader_and_destroy( ureg, pipe );
126 }
127
128
129 /**
130 * Make a simple fragment shader that sets the output color to a color
131 * taken from a texture.
132 * \param tex_target one of PIPE_TEXTURE_x
133 */
134 void *
135 util_make_fragment_tex_shader(struct pipe_context *pipe, unsigned tex_target )
136 {
137 return util_make_fragment_tex_shader_writemask( pipe,
138 tex_target,
139 TGSI_WRITEMASK_XYZW );
140 }
141
142
143 /**
144 * Make a simple fragment texture shader which reads an X component from
145 * a texture and writes it as depth.
146 */
147 void *
148 util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
149 unsigned tex_target)
150 {
151 struct ureg_program *ureg;
152 struct ureg_src sampler;
153 struct ureg_src tex;
154 struct ureg_dst out, depth;
155 struct ureg_src imm;
156
157 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
158 if (ureg == NULL)
159 return NULL;
160
161 sampler = ureg_DECL_sampler( ureg, 0 );
162
163 tex = ureg_DECL_fs_input( ureg,
164 TGSI_SEMANTIC_GENERIC, 0,
165 TGSI_INTERPOLATE_PERSPECTIVE );
166
167 out = ureg_DECL_output( ureg,
168 TGSI_SEMANTIC_COLOR,
169 0 );
170
171 depth = ureg_DECL_output( ureg,
172 TGSI_SEMANTIC_POSITION,
173 0 );
174
175 imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
176
177 ureg_MOV( ureg, out, imm );
178
179 ureg_TEX( ureg,
180 ureg_writemask(depth, TGSI_WRITEMASK_Z),
181 tex_target, tex, sampler );
182 ureg_END( ureg );
183
184 return ureg_create_shader_and_destroy( ureg, pipe );
185 }
186
187
188 /**
189 * Make simple fragment color pass-through shader.
190 */
191 void *
192 util_make_fragment_passthrough_shader(struct pipe_context *pipe)
193 {
194 return util_make_fragment_clonecolor_shader(pipe, 1);
195 }
196
197
198 /**
199 * Make a fragment shader that copies the input color to N output colors.
200 */
201 void *
202 util_make_fragment_clonecolor_shader(struct pipe_context *pipe, int num_cbufs)
203 {
204 struct ureg_program *ureg;
205 struct ureg_src src;
206 struct ureg_dst dst[PIPE_MAX_COLOR_BUFS];
207 int i;
208
209 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
210
211 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
212 if (ureg == NULL)
213 return NULL;
214
215 src = ureg_DECL_fs_input( ureg, TGSI_SEMANTIC_COLOR, 0,
216 TGSI_INTERPOLATE_PERSPECTIVE );
217
218 for (i = 0; i < num_cbufs; i++)
219 dst[i] = ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, i );
220
221 for (i = 0; i < num_cbufs; i++)
222 ureg_MOV( ureg, dst[i], src );
223
224 ureg_END( ureg );
225
226 return ureg_create_shader_and_destroy( ureg, pipe );
227 }