gallium: make p_winsys internal
[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_debug.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "pipe/p_screen.h"
41 #include "pipe/p_shader_tokens.h"
42
43 #include "util/u_memory.h"
44 #include "util/u_simple_shaders.h"
45
46 #include "tgsi/tgsi_build.h"
47 #include "tgsi/tgsi_dump.h"
48 #include "tgsi/tgsi_parse.h"
49
50
51
52 /**
53 * Make simple vertex pass-through shader.
54 */
55 void *
56 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
57 uint num_attribs,
58 const uint *semantic_names,
59 const uint *semantic_indexes,
60 struct pipe_shader_state *shader)
61
62 {
63 uint maxTokens = 100;
64 struct tgsi_token *tokens;
65 struct tgsi_header *header;
66 struct tgsi_processor *processor;
67 struct tgsi_full_declaration decl;
68 struct tgsi_full_instruction inst;
69 const uint procType = TGSI_PROCESSOR_VERTEX;
70 uint ti, i;
71
72 tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
73
74 /* shader header
75 */
76 *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
77
78 header = (struct tgsi_header *) &tokens[1];
79 *header = tgsi_build_header();
80
81 processor = (struct tgsi_processor *) &tokens[2];
82 *processor = tgsi_build_processor( procType, header );
83
84 ti = 3;
85
86 /* declare inputs */
87 for (i = 0; i < num_attribs; i++) {
88 decl = tgsi_default_full_declaration();
89 decl.Declaration.File = TGSI_FILE_INPUT;
90
91 decl.Declaration.Semantic = 1;
92 decl.Semantic.SemanticName = semantic_names[i];
93 decl.Semantic.SemanticIndex = semantic_indexes[i];
94
95 decl.DeclarationRange.First =
96 decl.DeclarationRange.Last = i;
97 ti += tgsi_build_full_declaration(&decl,
98 &tokens[ti],
99 header,
100 maxTokens - ti);
101 }
102
103 /* declare outputs */
104 for (i = 0; i < num_attribs; i++) {
105 decl = tgsi_default_full_declaration();
106 decl.Declaration.File = TGSI_FILE_OUTPUT;
107 decl.Declaration.Semantic = 1;
108 decl.Semantic.SemanticName = semantic_names[i];
109 decl.Semantic.SemanticIndex = semantic_indexes[i];
110 decl.DeclarationRange.First =
111 decl.DeclarationRange.Last = i;
112 ti += tgsi_build_full_declaration(&decl,
113 &tokens[ti],
114 header,
115 maxTokens - ti);
116 }
117
118 /* emit MOV instructions */
119 for (i = 0; i < num_attribs; i++) {
120 /* MOVE out[i], in[i]; */
121 inst = tgsi_default_full_instruction();
122 inst.Instruction.Opcode = TGSI_OPCODE_MOV;
123 inst.Instruction.NumDstRegs = 1;
124 inst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
125 inst.FullDstRegisters[0].DstRegister.Index = i;
126 inst.Instruction.NumSrcRegs = 1;
127 inst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
128 inst.FullSrcRegisters[0].SrcRegister.Index = i;
129 ti += tgsi_build_full_instruction(&inst,
130 &tokens[ti],
131 header,
132 maxTokens - ti );
133 }
134
135 /* END instruction */
136 inst = tgsi_default_full_instruction();
137 inst.Instruction.Opcode = TGSI_OPCODE_END;
138 inst.Instruction.NumDstRegs = 0;
139 inst.Instruction.NumSrcRegs = 0;
140 ti += tgsi_build_full_instruction(&inst,
141 &tokens[ti],
142 header,
143 maxTokens - ti );
144
145 #if 0 /*debug*/
146 tgsi_dump(tokens, 0);
147 #endif
148
149 shader->tokens = tokens;
150 /*shader->num_tokens = ti;*/
151
152 return pipe->create_vs_state(pipe, shader);
153 }
154
155
156
157
158 /**
159 * Make simple fragment texture shader:
160 * TEX OUT[0], IN[0], SAMP[0], 2D;
161 * END;
162 */
163 void *
164 util_make_fragment_tex_shader(struct pipe_context *pipe,
165 struct pipe_shader_state *shader)
166 {
167 uint maxTokens = 100;
168 struct tgsi_token *tokens;
169 struct tgsi_header *header;
170 struct tgsi_processor *processor;
171 struct tgsi_full_declaration decl;
172 struct tgsi_full_instruction inst;
173 const uint procType = TGSI_PROCESSOR_FRAGMENT;
174 uint ti;
175
176 tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
177
178 /* shader header
179 */
180 *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
181
182 header = (struct tgsi_header *) &tokens[1];
183 *header = tgsi_build_header();
184
185 processor = (struct tgsi_processor *) &tokens[2];
186 *processor = tgsi_build_processor( procType, header );
187
188 ti = 3;
189
190 /* declare TEX[0] input */
191 decl = tgsi_default_full_declaration();
192 decl.Declaration.File = TGSI_FILE_INPUT;
193 /* XXX this could be linear... */
194 decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
195 decl.Declaration.Semantic = 1;
196 decl.Semantic.SemanticName = TGSI_SEMANTIC_GENERIC;
197 decl.Semantic.SemanticIndex = 0;
198 decl.DeclarationRange.First =
199 decl.DeclarationRange.Last = 0;
200 ti += tgsi_build_full_declaration(&decl,
201 &tokens[ti],
202 header,
203 maxTokens - ti);
204
205 /* declare color[0] output */
206 decl = tgsi_default_full_declaration();
207 decl.Declaration.File = TGSI_FILE_OUTPUT;
208 decl.Declaration.Semantic = 1;
209 decl.Semantic.SemanticName = TGSI_SEMANTIC_COLOR;
210 decl.Semantic.SemanticIndex = 0;
211 decl.DeclarationRange.First =
212 decl.DeclarationRange.Last = 0;
213 ti += tgsi_build_full_declaration(&decl,
214 &tokens[ti],
215 header,
216 maxTokens - ti);
217
218 /* declare sampler */
219 decl = tgsi_default_full_declaration();
220 decl.Declaration.File = TGSI_FILE_SAMPLER;
221 decl.DeclarationRange.First =
222 decl.DeclarationRange.Last = 0;
223 ti += tgsi_build_full_declaration(&decl,
224 &tokens[ti],
225 header,
226 maxTokens - ti);
227
228 /* TEX instruction */
229 inst = tgsi_default_full_instruction();
230 inst.Instruction.Opcode = TGSI_OPCODE_TEX;
231 inst.Instruction.NumDstRegs = 1;
232 inst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
233 inst.FullDstRegisters[0].DstRegister.Index = 0;
234 inst.Instruction.NumSrcRegs = 2;
235 inst.InstructionExtTexture.Texture = TGSI_TEXTURE_2D;
236 inst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
237 inst.FullSrcRegisters[0].SrcRegister.Index = 0;
238 inst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
239 inst.FullSrcRegisters[1].SrcRegister.Index = 0;
240 ti += tgsi_build_full_instruction(&inst,
241 &tokens[ti],
242 header,
243 maxTokens - ti );
244
245 /* END instruction */
246 inst = tgsi_default_full_instruction();
247 inst.Instruction.Opcode = TGSI_OPCODE_END;
248 inst.Instruction.NumDstRegs = 0;
249 inst.Instruction.NumSrcRegs = 0;
250 ti += tgsi_build_full_instruction(&inst,
251 &tokens[ti],
252 header,
253 maxTokens - ti );
254
255 #if 0 /*debug*/
256 tgsi_dump(tokens, 0);
257 #endif
258
259 shader->tokens = tokens;
260 /*shader->num_tokens = ti;*/
261
262 return pipe->create_fs_state(pipe, shader);
263 }
264
265
266
267
268
269 /**
270 * Make simple fragment color pass-through shader.
271 */
272 void *
273 util_make_fragment_passthrough_shader(struct pipe_context *pipe,
274 struct pipe_shader_state *shader)
275 {
276 uint maxTokens = 40;
277 struct tgsi_token *tokens;
278 struct tgsi_header *header;
279 struct tgsi_processor *processor;
280 struct tgsi_full_declaration decl;
281 struct tgsi_full_instruction inst;
282 const uint procType = TGSI_PROCESSOR_FRAGMENT;
283 uint ti;
284
285 tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
286
287 /* shader header
288 */
289 *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
290
291 header = (struct tgsi_header *) &tokens[1];
292 *header = tgsi_build_header();
293
294 processor = (struct tgsi_processor *) &tokens[2];
295 *processor = tgsi_build_processor( procType, header );
296
297 ti = 3;
298
299 /* declare input */
300 decl = tgsi_default_full_declaration();
301 decl.Declaration.File = TGSI_FILE_INPUT;
302 decl.Declaration.Semantic = 1;
303 decl.Semantic.SemanticName = TGSI_SEMANTIC_COLOR;
304 decl.Semantic.SemanticIndex = 0;
305 decl.DeclarationRange.First =
306 decl.DeclarationRange.Last = 0;
307 ti += tgsi_build_full_declaration(&decl,
308 &tokens[ti],
309 header,
310 maxTokens - ti);
311
312 /* declare output */
313 decl = tgsi_default_full_declaration();
314 decl.Declaration.File = TGSI_FILE_OUTPUT;
315 decl.Declaration.Semantic = 1;
316 decl.Semantic.SemanticName = TGSI_SEMANTIC_COLOR;
317 decl.Semantic.SemanticIndex = 0;
318 decl.DeclarationRange.First =
319 decl.DeclarationRange.Last = 0;
320 ti += tgsi_build_full_declaration(&decl,
321 &tokens[ti],
322 header,
323 maxTokens - ti);
324
325
326 /* MOVE out[0], in[0]; */
327 inst = tgsi_default_full_instruction();
328 inst.Instruction.Opcode = TGSI_OPCODE_MOV;
329 inst.Instruction.NumDstRegs = 1;
330 inst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
331 inst.FullDstRegisters[0].DstRegister.Index = 0;
332 inst.Instruction.NumSrcRegs = 1;
333 inst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
334 inst.FullSrcRegisters[0].SrcRegister.Index = 0;
335 ti += tgsi_build_full_instruction(&inst,
336 &tokens[ti],
337 header,
338 maxTokens - ti );
339
340 /* END instruction */
341 inst = tgsi_default_full_instruction();
342 inst.Instruction.Opcode = TGSI_OPCODE_END;
343 inst.Instruction.NumDstRegs = 0;
344 inst.Instruction.NumSrcRegs = 0;
345 ti += tgsi_build_full_instruction(&inst,
346 &tokens[ti],
347 header,
348 maxTokens - ti );
349
350 assert(ti < maxTokens);
351
352 #if 0 /*debug*/
353 tgsi_dump(tokens, 0);
354 #endif
355
356 shader->tokens = tokens;
357 /*shader->num_tokens = ti;*/
358
359 return pipe->create_fs_state(pipe, shader);
360 }
361