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