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