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 "pipe/p_debug.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "pipe/p_util.h"
41 #include "pipe/p_winsys.h"
42 #include "pipe/p_shader_tokens.h"
44 #include "util/u_simple_shaders.h"
46 #include "tgsi/util/tgsi_build.h"
47 #include "tgsi/util/tgsi_dump.h"
48 #include "tgsi/util/tgsi_parse.h"
53 * Make simple vertex pass-through shader.
56 util_make_vertex_passthrough_shader(struct pipe_context
*pipe
,
58 const uint
*semantic_names
,
59 const uint
*semantic_indexes
)
62 struct tgsi_token
*tokens
;
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
;
69 struct pipe_shader_state shader
;
71 tokens
= (struct tgsi_token
*) MALLOC(maxTokens
* sizeof(tokens
[0]));
75 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
77 header
= (struct tgsi_header
*) &tokens
[1];
78 *header
= tgsi_build_header();
80 processor
= (struct tgsi_processor
*) &tokens
[2];
81 *processor
= tgsi_build_processor( procType
, header
);
86 for (i
= 0; i
< num_attribs
; i
++) {
88 decl
= tgsi_default_full_declaration();
89 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
91 decl.Declaration.Semantic = 1;
92 decl.Semantic.SemanticName = TGSI_SEMANTIC_POSITION;
93 decl.Semantic.SemanticIndex = 0;
95 decl
.u
.DeclarationRange
.First
=
96 decl
.u
.DeclarationRange
.Last
= 0;
97 ti
+= tgsi_build_full_declaration(&decl
,
103 /* declare outputs */
104 for (i
= 0; i
< num_attribs
; i
++) {
106 decl
= tgsi_default_full_declaration();
107 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
108 decl
.Declaration
.Semantic
= 1;
109 decl
.Semantic
.SemanticName
= semantic_names
[i
];
110 decl
.Semantic
.SemanticIndex
= semantic_indexes
[i
];
111 decl
.u
.DeclarationRange
.First
=
112 decl
.u
.DeclarationRange
.Last
= 0;
113 ti
+= tgsi_build_full_declaration(&decl
,
120 /* emit MOV instructions */
121 for (i
= 0; i
< num_attribs
; i
++) {
122 /* MOVE out[i], in[i]; */
123 inst
= tgsi_default_full_instruction();
124 inst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
125 inst
.Instruction
.NumDstRegs
= 1;
126 inst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
127 inst
.FullDstRegisters
[0].DstRegister
.Index
= i
;
128 inst
.Instruction
.NumSrcRegs
= 1;
129 inst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
130 inst
.FullSrcRegisters
[0].SrcRegister
.Index
= i
;
131 ti
+= tgsi_build_full_instruction(&inst
,
137 /* END instruction */
138 inst
= tgsi_default_full_instruction();
139 inst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
140 inst
.Instruction
.NumDstRegs
= 0;
141 inst
.Instruction
.NumSrcRegs
= 0;
142 ti
+= tgsi_build_full_instruction(&inst
,
148 tgsi_dump(tokens
, 0);
151 shader
.tokens
= tokens
;
152 return pipe
->create_vs_state(pipe
, &shader
);
159 * Make simple fragment texture shader:
160 * TEX OUT[0], IN[0], SAMP[0], 2D;
164 util_make_fragment_tex_shader(struct pipe_context
*pipe
)
166 uint maxTokens
= 100;
167 struct tgsi_token
*tokens
;
168 struct tgsi_header
*header
;
169 struct tgsi_processor
*processor
;
170 struct tgsi_full_declaration decl
;
171 struct tgsi_full_instruction inst
;
172 const uint procType
= TGSI_PROCESSOR_FRAGMENT
;
174 struct pipe_shader_state shader
;
176 tokens
= (struct tgsi_token
*) MALLOC(maxTokens
* sizeof(tokens
[0]));
180 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
182 header
= (struct tgsi_header
*) &tokens
[1];
183 *header
= tgsi_build_header();
185 processor
= (struct tgsi_processor
*) &tokens
[2];
186 *processor
= tgsi_build_processor( procType
, header
);
190 /* declare TEX[0] input */
191 decl
= tgsi_default_full_declaration();
192 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
193 decl
.Declaration
.Semantic
= 1;
194 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
195 decl
.Semantic
.SemanticIndex
= 0;
196 /* XXX this could be linear... */
197 decl
.Declaration
.Interpolate
= 1;
198 decl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
199 decl
.u
.DeclarationRange
.First
=
200 decl
.u
.DeclarationRange
.Last
= 0;
201 ti
+= tgsi_build_full_declaration(&decl
,
206 /* declare color[0] output */
207 decl
= tgsi_default_full_declaration();
208 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
209 decl
.Declaration
.Semantic
= 1;
210 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_COLOR
;
211 decl
.Semantic
.SemanticIndex
= 0;
212 decl
.u
.DeclarationRange
.First
=
213 decl
.u
.DeclarationRange
.Last
= 0;
214 ti
+= tgsi_build_full_declaration(&decl
,
219 /* declare sampler */
220 decl
= tgsi_default_full_declaration();
221 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
222 decl
.u
.DeclarationRange
.First
=
223 decl
.u
.DeclarationRange
.Last
= 0;
224 ti
+= tgsi_build_full_declaration(&decl
,
229 /* TEX instruction */
230 inst
= tgsi_default_full_instruction();
231 inst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
232 inst
.Instruction
.NumDstRegs
= 1;
233 inst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
234 inst
.FullDstRegisters
[0].DstRegister
.Index
= 0;
235 inst
.Instruction
.NumSrcRegs
= 2;
236 inst
.InstructionExtTexture
.Texture
= TGSI_TEXTURE_2D
;
237 inst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
238 inst
.FullSrcRegisters
[0].SrcRegister
.Index
= 0;
239 inst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
240 inst
.FullSrcRegisters
[1].SrcRegister
.Index
= 0;
241 ti
+= tgsi_build_full_instruction(&inst
,
246 /* END instruction */
247 inst
= tgsi_default_full_instruction();
248 inst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
249 inst
.Instruction
.NumDstRegs
= 0;
250 inst
.Instruction
.NumSrcRegs
= 0;
251 ti
+= tgsi_build_full_instruction(&inst
,
257 tgsi_dump(tokens
, 0);
260 shader
.tokens
= tokens
;
261 return pipe
->create_fs_state(pipe
, &shader
);
269 * Make simple fragment color pass-through shader.
272 util_make_fragment_passthrough_shader(struct pipe_context
*pipe
)
275 struct tgsi_token
*tokens
;
276 struct tgsi_header
*header
;
277 struct tgsi_processor
*processor
;
278 struct tgsi_full_declaration decl
;
279 struct tgsi_full_instruction inst
;
280 const uint procType
= TGSI_PROCESSOR_FRAGMENT
;
282 struct pipe_shader_state shader
;
284 tokens
= (struct tgsi_token
*) MALLOC(maxTokens
* sizeof(tokens
[0]));
288 *(struct tgsi_version
*) &tokens
[0] = tgsi_build_version();
290 header
= (struct tgsi_header
*) &tokens
[1];
291 *header
= tgsi_build_header();
293 processor
= (struct tgsi_processor
*) &tokens
[2];
294 *processor
= tgsi_build_processor( procType
, header
);
299 decl
= tgsi_default_full_declaration();
300 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
301 decl
.Declaration
.Semantic
= 1;
302 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_COLOR
;
303 decl
.Semantic
.SemanticIndex
= 0;
304 decl
.u
.DeclarationRange
.First
=
305 decl
.u
.DeclarationRange
.Last
= 0;
306 ti
+= tgsi_build_full_declaration(&decl
,
312 decl
= tgsi_default_full_declaration();
313 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
314 decl
.Declaration
.Semantic
= 1;
315 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_COLOR
;
316 decl
.Semantic
.SemanticIndex
= 0;
317 decl
.u
.DeclarationRange
.First
=
318 decl
.u
.DeclarationRange
.Last
= 0;
319 ti
+= tgsi_build_full_declaration(&decl
,
325 /* MOVE out[0], in[0]; */
326 inst
= tgsi_default_full_instruction();
327 inst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
328 inst
.Instruction
.NumDstRegs
= 1;
329 inst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
330 inst
.FullDstRegisters
[0].DstRegister
.Index
= 0;
331 inst
.Instruction
.NumSrcRegs
= 1;
332 inst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
333 inst
.FullSrcRegisters
[0].SrcRegister
.Index
= 0;
334 ti
+= tgsi_build_full_instruction(&inst
,
339 /* END instruction */
340 inst
= tgsi_default_full_instruction();
341 inst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
342 inst
.Instruction
.NumDstRegs
= 0;
343 inst
.Instruction
.NumSrcRegs
= 0;
344 ti
+= tgsi_build_full_instruction(&inst
,
349 assert(ti
< maxTokens
);
352 tgsi_dump(tokens
, 0);
355 shader
.tokens
= tokens
;
356 return pipe
->create_fs_state(pipe
, &shader
);