1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Simple vertex/fragment shader generators.
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"
42 #include "util/u_memory.h"
43 #include "util/u_simple_shaders.h"
45 #include "tgsi/tgsi_build.h"
46 #include "tgsi/tgsi_dump.h"
47 #include "tgsi/tgsi_parse.h"
52 * Make simple vertex pass-through shader.
55 util_make_vertex_passthrough_shader(struct pipe_context
*pipe
,
57 const uint
*semantic_names
,
58 const uint
*semantic_indexes
)
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
;
72 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
74 header
= (struct tgsi_header
*) &tokens
[1];
75 *header
= tgsi_build_header();
77 processor
= (struct tgsi_processor
*) &tokens
[2];
78 *processor
= tgsi_build_processor( procType
, header
);
83 for (i
= 0; i
< num_attribs
; i
++) {
84 decl
= tgsi_default_full_declaration();
85 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
87 decl
.Declaration
.Semantic
= 1;
88 decl
.Semantic
.SemanticName
= semantic_names
[i
];
89 decl
.Semantic
.SemanticIndex
= semantic_indexes
[i
];
91 decl
.DeclarationRange
.First
=
92 decl
.DeclarationRange
.Last
= i
;
93 ti
+= tgsi_build_full_declaration(&decl
,
96 Elements(tokens
) - ti
);
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
,
111 Elements(tokens
) - ti
);
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
,
128 Elements(tokens
) - ti
);
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
,
139 Elements(tokens
) - ti
);
142 tgsi_dump(tokens
, 0);
145 shader
.tokens
= tokens
;
147 return pipe
->create_vs_state(pipe
, &shader
);
154 * Make simple fragment texture shader:
155 * TEX OUT[0], IN[0], SAMP[0], 2D;
159 util_make_fragment_tex_shader(struct pipe_context
*pipe
)
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
;
172 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
174 header
= (struct tgsi_header
*) &tokens
[1];
175 *header
= tgsi_build_header();
177 processor
= (struct tgsi_processor
*) &tokens
[2];
178 *processor
= tgsi_build_processor( procType
, header
);
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
,
195 Elements(tokens
) - ti
);
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
,
208 Elements(tokens
) - ti
);
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
,
218 Elements(tokens
) - ti
);
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
,
235 Elements(tokens
) - ti
);
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
,
245 Elements(tokens
) - ti
);
248 tgsi_dump(tokens
, 0);
251 shader
.tokens
= tokens
;
253 return pipe
->create_fs_state(pipe
, &shader
);
261 * Make simple fragment color pass-through shader.
264 util_make_fragment_passthrough_shader(struct pipe_context
*pipe
)
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
;
277 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
279 header
= (struct tgsi_header
*) &tokens
[1];
280 *header
= tgsi_build_header();
282 processor
= (struct tgsi_processor
*) &tokens
[2];
283 *processor
= tgsi_build_processor( procType
, header
);
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
,
298 Elements(tokens
) - ti
);
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
,
311 Elements(tokens
) - ti
);
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
,
326 Elements(tokens
) - ti
);
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
,
336 Elements(tokens
) - ti
);
338 assert(ti
< Elements(tokens
));
341 tgsi_dump(tokens
, 0);
344 shader
.tokens
= tokens
;
346 return pipe
->create_fs_state(pipe
, &shader
);