Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Simple vertex/fragment shader generators.
31 *
32 * @author Brian Paul
33 */
34
35
36 #include "pipe/p_context.h"
37 #include "pipe/p_shader_tokens.h"
38 #include "util/u_simple_shaders.h"
39 #include "tgsi/tgsi_ureg.h"
40
41
42
43 /**
44 * Make simple vertex pass-through shader.
45 */
46 void *
47 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
48 uint num_attribs,
49 const uint *semantic_names,
50 const uint *semantic_indexes)
51
52 {
53 struct ureg_program *ureg;
54 uint i;
55
56 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
57 if (ureg == NULL)
58 return NULL;
59
60 for (i = 0; i < num_attribs; i++) {
61 struct ureg_src src;
62 struct ureg_dst dst;
63
64 src = ureg_DECL_vs_input( ureg, i );
65
66 dst = ureg_DECL_output( ureg,
67 semantic_names[i],
68 semantic_indexes[i]);
69
70 ureg_MOV( ureg, dst, src );
71 }
72
73 ureg_END( ureg );
74
75 return ureg_create_shader_and_destroy( ureg, pipe );
76 }
77
78
79
80
81 /**
82 * Make simple fragment texture shader:
83 * IMM {0,0,0,1} // (if writemask != 0xf)
84 * MOV OUT[0], IMM[0] // (if writemask != 0xf)
85 * TEX OUT[0].writemask, IN[0], SAMP[0], 2D;
86 * END;
87 */
88 void *
89 util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
90 unsigned writemask )
91 {
92 struct ureg_program *ureg;
93 struct ureg_src sampler;
94 struct ureg_src tex;
95 struct ureg_dst out;
96
97 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
98 if (ureg == NULL)
99 return NULL;
100
101 sampler = ureg_DECL_sampler( ureg, 0 );
102
103 tex = ureg_DECL_fs_input( ureg,
104 TGSI_SEMANTIC_GENERIC, 0,
105 TGSI_INTERPOLATE_PERSPECTIVE );
106
107 out = ureg_DECL_output( ureg,
108 TGSI_SEMANTIC_COLOR,
109 0 );
110
111 if (writemask != TGSI_WRITEMASK_XYZW) {
112 struct ureg_src imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
113
114 ureg_MOV( ureg, out, imm );
115 }
116
117 ureg_TEX( ureg,
118 ureg_writemask(out, writemask),
119 TGSI_TEXTURE_2D, tex, sampler );
120 ureg_END( ureg );
121
122 return ureg_create_shader_and_destroy( ureg, pipe );
123 }
124
125 void *
126 util_make_fragment_tex_shader(struct pipe_context *pipe )
127 {
128 return util_make_fragment_tex_shader_writemask( pipe,
129 TGSI_WRITEMASK_XYZW );
130 }
131
132
133
134 /**
135 * Make simple fragment color pass-through shader.
136 */
137 void *
138 util_make_fragment_passthrough_shader(struct pipe_context *pipe)
139 {
140 struct ureg_program *ureg;
141 struct ureg_src src;
142 struct ureg_dst dst;
143
144 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
145 if (ureg == NULL)
146 return NULL;
147
148 src = ureg_DECL_fs_input( ureg, TGSI_SEMANTIC_COLOR, 0,
149 TGSI_INTERPOLATE_PERSPECTIVE );
150
151 dst = ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 );
152
153 ureg_MOV( ureg, dst, src );
154 ureg_END( ureg );
155
156 return ureg_create_shader_and_destroy( ureg, pipe );
157 }
158
159