Merge branch 'master' of ../mesa into vulkan
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_transform.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 #ifndef TGSI_TRANSFORM_H
29 #define TGSI_TRANSFORM_H
30
31
32 #include "pipe/p_shader_tokens.h"
33 #include "tgsi/tgsi_parse.h"
34 #include "tgsi/tgsi_build.h"
35
36
37
38 /**
39 * Subclass this to add caller-specific data
40 */
41 struct tgsi_transform_context
42 {
43 /**** PUBLIC ***/
44
45 /**
46 * User-defined callbacks invoked per instruction.
47 */
48 void (*transform_instruction)(struct tgsi_transform_context *ctx,
49 struct tgsi_full_instruction *inst);
50
51 void (*transform_declaration)(struct tgsi_transform_context *ctx,
52 struct tgsi_full_declaration *decl);
53
54 void (*transform_immediate)(struct tgsi_transform_context *ctx,
55 struct tgsi_full_immediate *imm);
56 void (*transform_property)(struct tgsi_transform_context *ctx,
57 struct tgsi_full_property *prop);
58
59 /**
60 * Called after last declaration, before first instruction. This is
61 * where the user might insert new declarations and/or instructions.
62 */
63 void (*prolog)(struct tgsi_transform_context *ctx);
64
65 /**
66 * Called at end of input program to allow caller to append extra
67 * instructions. Return number of tokens emitted.
68 */
69 void (*epilog)(struct tgsi_transform_context *ctx);
70
71
72 /*** PRIVATE ***/
73
74 /**
75 * These are setup by tgsi_transform_shader() and cannot be overridden.
76 * Meant to be called from in the above user callback functions.
77 */
78 void (*emit_instruction)(struct tgsi_transform_context *ctx,
79 const struct tgsi_full_instruction *inst);
80 void (*emit_declaration)(struct tgsi_transform_context *ctx,
81 const struct tgsi_full_declaration *decl);
82 void (*emit_immediate)(struct tgsi_transform_context *ctx,
83 const struct tgsi_full_immediate *imm);
84 void (*emit_property)(struct tgsi_transform_context *ctx,
85 const struct tgsi_full_property *prop);
86
87 struct tgsi_header *header;
88 uint max_tokens_out;
89 struct tgsi_token *tokens_out;
90 uint ti;
91 };
92
93
94 /**
95 * Helper for emitting temporary register declarations.
96 */
97 static inline void
98 tgsi_transform_temps_decl(struct tgsi_transform_context *ctx,
99 unsigned firstIdx, unsigned lastIdx)
100 {
101 struct tgsi_full_declaration decl;
102
103 decl = tgsi_default_full_declaration();
104 decl.Declaration.File = TGSI_FILE_TEMPORARY;
105 decl.Range.First = firstIdx;
106 decl.Range.Last = lastIdx;
107 ctx->emit_declaration(ctx, &decl);
108 }
109
110 static inline void
111 tgsi_transform_temp_decl(struct tgsi_transform_context *ctx,
112 unsigned index)
113 {
114 tgsi_transform_temps_decl(ctx, index, index);
115 }
116
117 static inline void
118 tgsi_transform_const_decl(struct tgsi_transform_context *ctx,
119 unsigned firstIdx, unsigned lastIdx)
120 {
121 struct tgsi_full_declaration decl;
122
123 decl = tgsi_default_full_declaration();
124 decl.Declaration.File = TGSI_FILE_CONSTANT;
125 decl.Range.First = firstIdx;
126 decl.Range.Last = lastIdx;
127 ctx->emit_declaration(ctx, &decl);
128 }
129
130 static inline void
131 tgsi_transform_input_decl(struct tgsi_transform_context *ctx,
132 unsigned index,
133 unsigned sem_name, unsigned sem_index,
134 unsigned interp)
135 {
136 struct tgsi_full_declaration decl;
137
138 decl = tgsi_default_full_declaration();
139 decl.Declaration.File = TGSI_FILE_INPUT;
140 decl.Declaration.Interpolate = 1;
141 decl.Declaration.Semantic = 1;
142 decl.Semantic.Name = sem_name;
143 decl.Semantic.Index = sem_index;
144 decl.Range.First =
145 decl.Range.Last = index;
146 decl.Interp.Interpolate = interp;
147
148 ctx->emit_declaration(ctx, &decl);
149 }
150
151 static inline void
152 tgsi_transform_output_decl(struct tgsi_transform_context *ctx,
153 unsigned index,
154 unsigned sem_name, unsigned sem_index,
155 unsigned interp)
156 {
157 struct tgsi_full_declaration decl;
158
159 decl = tgsi_default_full_declaration();
160 decl.Declaration.File = TGSI_FILE_OUTPUT;
161 decl.Declaration.Interpolate = 1;
162 decl.Declaration.Semantic = 1;
163 decl.Semantic.Name = sem_name;
164 decl.Semantic.Index = sem_index;
165 decl.Range.First =
166 decl.Range.Last = index;
167 decl.Interp.Interpolate = interp;
168
169 ctx->emit_declaration(ctx, &decl);
170 }
171
172 static inline void
173 tgsi_transform_sampler_decl(struct tgsi_transform_context *ctx,
174 unsigned index)
175 {
176 struct tgsi_full_declaration decl;
177
178 decl = tgsi_default_full_declaration();
179 decl.Declaration.File = TGSI_FILE_SAMPLER;
180 decl.Range.First =
181 decl.Range.Last = index;
182 ctx->emit_declaration(ctx, &decl);
183 }
184
185 static inline void
186 tgsi_transform_sampler_view_decl(struct tgsi_transform_context *ctx,
187 unsigned index,
188 unsigned target,
189 enum tgsi_return_type type)
190 {
191 struct tgsi_full_declaration decl;
192
193 decl = tgsi_default_full_declaration();
194 decl.Declaration.File = TGSI_FILE_SAMPLER_VIEW;
195 decl.Declaration.UsageMask = 0xf;
196 decl.Range.First =
197 decl.Range.Last = index;
198 decl.SamplerView.Resource = target;
199 decl.SamplerView.ReturnTypeX = type;
200 decl.SamplerView.ReturnTypeY = type;
201 decl.SamplerView.ReturnTypeZ = type;
202 decl.SamplerView.ReturnTypeW = type;
203
204 ctx->emit_declaration(ctx, &decl);
205 }
206
207 static inline void
208 tgsi_transform_immediate_decl(struct tgsi_transform_context *ctx,
209 float x, float y, float z, float w)
210 {
211 struct tgsi_full_immediate immed;
212 unsigned size = 4;
213
214 immed = tgsi_default_full_immediate();
215 immed.Immediate.NrTokens = 1 + size; /* one for the token itself */
216 immed.u[0].Float = x;
217 immed.u[1].Float = y;
218 immed.u[2].Float = z;
219 immed.u[3].Float = w;
220
221 ctx->emit_immediate(ctx, &immed);
222 }
223
224 static inline void
225 tgsi_transform_dst_reg(struct tgsi_full_dst_register *reg,
226 unsigned file, unsigned index, unsigned writemask)
227 {
228 reg->Register.File = file;
229 reg->Register.Index = index;
230 reg->Register.WriteMask = writemask;
231 }
232
233 static inline void
234 tgsi_transform_src_reg(struct tgsi_full_src_register *reg,
235 unsigned file, unsigned index,
236 unsigned swizzleX, unsigned swizzleY,
237 unsigned swizzleZ, unsigned swizzleW)
238 {
239 reg->Register.File = file;
240 reg->Register.Index = index;
241 reg->Register.SwizzleX = swizzleX;
242 reg->Register.SwizzleY = swizzleY;
243 reg->Register.SwizzleZ = swizzleZ;
244 reg->Register.SwizzleW = swizzleW;
245 }
246
247 /**
248 * Helper for emitting 1-operand instructions.
249 */
250 static inline void
251 tgsi_transform_op1_inst(struct tgsi_transform_context *ctx,
252 unsigned opcode,
253 unsigned dst_file,
254 unsigned dst_index,
255 unsigned dst_writemask,
256 unsigned src0_file,
257 unsigned src0_index)
258 {
259 struct tgsi_full_instruction inst;
260
261 inst = tgsi_default_full_instruction();
262 inst.Instruction.Opcode = opcode;
263 inst.Instruction.NumDstRegs = 1;
264 inst.Dst[0].Register.File = dst_file,
265 inst.Dst[0].Register.Index = dst_index;
266 inst.Dst[0].Register.WriteMask = dst_writemask;
267 inst.Instruction.NumSrcRegs = 1;
268 inst.Src[0].Register.File = src0_file;
269 inst.Src[0].Register.Index = src0_index;
270
271 ctx->emit_instruction(ctx, &inst);
272 }
273
274
275 static inline void
276 tgsi_transform_op2_inst(struct tgsi_transform_context *ctx,
277 unsigned opcode,
278 unsigned dst_file,
279 unsigned dst_index,
280 unsigned dst_writemask,
281 unsigned src0_file,
282 unsigned src0_index,
283 unsigned src1_file,
284 unsigned src1_index)
285 {
286 struct tgsi_full_instruction inst;
287
288 inst = tgsi_default_full_instruction();
289 inst.Instruction.Opcode = opcode;
290 inst.Instruction.NumDstRegs = 1;
291 inst.Dst[0].Register.File = dst_file,
292 inst.Dst[0].Register.Index = dst_index;
293 inst.Dst[0].Register.WriteMask = dst_writemask;
294 inst.Instruction.NumSrcRegs = 2;
295 inst.Src[0].Register.File = src0_file;
296 inst.Src[0].Register.Index = src0_index;
297 inst.Src[1].Register.File = src1_file;
298 inst.Src[1].Register.Index = src1_index;
299
300 ctx->emit_instruction(ctx, &inst);
301 }
302
303
304 static inline void
305 tgsi_transform_op1_swz_inst(struct tgsi_transform_context *ctx,
306 unsigned opcode,
307 unsigned dst_file,
308 unsigned dst_index,
309 unsigned dst_writemask,
310 unsigned src0_file,
311 unsigned src0_index,
312 unsigned src0_swizzle)
313 {
314 struct tgsi_full_instruction inst;
315
316 inst = tgsi_default_full_instruction();
317 inst.Instruction.Opcode = opcode;
318 inst.Instruction.NumDstRegs = 1;
319 inst.Dst[0].Register.File = dst_file,
320 inst.Dst[0].Register.Index = dst_index;
321 inst.Dst[0].Register.WriteMask = dst_writemask;
322 inst.Instruction.NumSrcRegs = 1;
323 inst.Src[0].Register.File = src0_file;
324 inst.Src[0].Register.Index = src0_index;
325 switch (dst_writemask) {
326 case TGSI_WRITEMASK_X:
327 inst.Src[0].Register.SwizzleX = src0_swizzle;
328 break;
329 case TGSI_WRITEMASK_Y:
330 inst.Src[0].Register.SwizzleY = src0_swizzle;
331 break;
332 case TGSI_WRITEMASK_Z:
333 inst.Src[0].Register.SwizzleZ = src0_swizzle;
334 break;
335 case TGSI_WRITEMASK_W:
336 inst.Src[0].Register.SwizzleW = src0_swizzle;
337 break;
338 default:
339 ; /* nothing */
340 }
341
342 ctx->emit_instruction(ctx, &inst);
343 }
344
345
346 static inline void
347 tgsi_transform_op2_swz_inst(struct tgsi_transform_context *ctx,
348 unsigned opcode,
349 unsigned dst_file,
350 unsigned dst_index,
351 unsigned dst_writemask,
352 unsigned src0_file,
353 unsigned src0_index,
354 unsigned src0_swizzle,
355 unsigned src1_file,
356 unsigned src1_index,
357 unsigned src1_swizzle)
358 {
359 struct tgsi_full_instruction inst;
360
361 inst = tgsi_default_full_instruction();
362 inst.Instruction.Opcode = opcode;
363 inst.Instruction.NumDstRegs = 1;
364 inst.Dst[0].Register.File = dst_file,
365 inst.Dst[0].Register.Index = dst_index;
366 inst.Dst[0].Register.WriteMask = dst_writemask;
367 inst.Instruction.NumSrcRegs = 2;
368 inst.Src[0].Register.File = src0_file;
369 inst.Src[0].Register.Index = src0_index;
370 inst.Src[1].Register.File = src1_file;
371 inst.Src[1].Register.Index = src1_index;
372 switch (dst_writemask) {
373 case TGSI_WRITEMASK_X:
374 inst.Src[0].Register.SwizzleX = src0_swizzle;
375 inst.Src[1].Register.SwizzleX = src1_swizzle;
376 break;
377 case TGSI_WRITEMASK_Y:
378 inst.Src[0].Register.SwizzleY = src0_swizzle;
379 inst.Src[1].Register.SwizzleY = src1_swizzle;
380 break;
381 case TGSI_WRITEMASK_Z:
382 inst.Src[0].Register.SwizzleZ = src0_swizzle;
383 inst.Src[1].Register.SwizzleZ = src1_swizzle;
384 break;
385 case TGSI_WRITEMASK_W:
386 inst.Src[0].Register.SwizzleW = src0_swizzle;
387 inst.Src[1].Register.SwizzleW = src1_swizzle;
388 break;
389 default:
390 ; /* nothing */
391 }
392
393 ctx->emit_instruction(ctx, &inst);
394 }
395
396
397 static inline void
398 tgsi_transform_op3_swz_inst(struct tgsi_transform_context *ctx,
399 unsigned opcode,
400 unsigned dst_file,
401 unsigned dst_index,
402 unsigned dst_writemask,
403 unsigned src0_file,
404 unsigned src0_index,
405 unsigned src0_swizzle,
406 unsigned src0_negate,
407 unsigned src1_file,
408 unsigned src1_index,
409 unsigned src1_swizzle,
410 unsigned src2_file,
411 unsigned src2_index,
412 unsigned src2_swizzle)
413 {
414 struct tgsi_full_instruction inst;
415
416 inst = tgsi_default_full_instruction();
417 inst.Instruction.Opcode = opcode;
418 inst.Instruction.NumDstRegs = 1;
419 inst.Dst[0].Register.File = dst_file,
420 inst.Dst[0].Register.Index = dst_index;
421 inst.Dst[0].Register.WriteMask = dst_writemask;
422 inst.Instruction.NumSrcRegs = 3;
423 inst.Src[0].Register.File = src0_file;
424 inst.Src[0].Register.Index = src0_index;
425 inst.Src[0].Register.Negate = src0_negate;
426 inst.Src[1].Register.File = src1_file;
427 inst.Src[1].Register.Index = src1_index;
428 inst.Src[2].Register.File = src2_file;
429 inst.Src[2].Register.Index = src2_index;
430 switch (dst_writemask) {
431 case TGSI_WRITEMASK_X:
432 inst.Src[0].Register.SwizzleX = src0_swizzle;
433 inst.Src[1].Register.SwizzleX = src1_swizzle;
434 inst.Src[2].Register.SwizzleX = src2_swizzle;
435 break;
436 case TGSI_WRITEMASK_Y:
437 inst.Src[0].Register.SwizzleY = src0_swizzle;
438 inst.Src[1].Register.SwizzleY = src1_swizzle;
439 inst.Src[2].Register.SwizzleY = src2_swizzle;
440 break;
441 case TGSI_WRITEMASK_Z:
442 inst.Src[0].Register.SwizzleZ = src0_swizzle;
443 inst.Src[1].Register.SwizzleZ = src1_swizzle;
444 inst.Src[2].Register.SwizzleZ = src2_swizzle;
445 break;
446 case TGSI_WRITEMASK_W:
447 inst.Src[0].Register.SwizzleW = src0_swizzle;
448 inst.Src[1].Register.SwizzleW = src1_swizzle;
449 inst.Src[2].Register.SwizzleW = src2_swizzle;
450 break;
451 default:
452 ; /* nothing */
453 }
454
455 ctx->emit_instruction(ctx, &inst);
456 }
457
458
459 static inline void
460 tgsi_transform_kill_inst(struct tgsi_transform_context *ctx,
461 unsigned src_file,
462 unsigned src_index,
463 unsigned src_swizzle,
464 boolean negate)
465 {
466 struct tgsi_full_instruction inst;
467
468 inst = tgsi_default_full_instruction();
469 inst.Instruction.Opcode = TGSI_OPCODE_KILL_IF;
470 inst.Instruction.NumDstRegs = 0;
471 inst.Instruction.NumSrcRegs = 1;
472 inst.Src[0].Register.File = src_file;
473 inst.Src[0].Register.Index = src_index;
474 inst.Src[0].Register.SwizzleX =
475 inst.Src[0].Register.SwizzleY =
476 inst.Src[0].Register.SwizzleZ =
477 inst.Src[0].Register.SwizzleW = src_swizzle;
478 inst.Src[0].Register.Negate = negate;
479
480 ctx->emit_instruction(ctx, &inst);
481 }
482
483
484 static inline void
485 tgsi_transform_tex_2d_inst(struct tgsi_transform_context *ctx,
486 unsigned dst_file,
487 unsigned dst_index,
488 unsigned src_file,
489 unsigned src_index,
490 unsigned sampler_index)
491 {
492 struct tgsi_full_instruction inst;
493
494 inst = tgsi_default_full_instruction();
495 inst.Instruction.Opcode = TGSI_OPCODE_TEX;
496 inst.Instruction.NumDstRegs = 1;
497 inst.Dst[0].Register.File = dst_file;
498 inst.Dst[0].Register.Index = dst_index;
499 inst.Instruction.NumSrcRegs = 2;
500 inst.Instruction.Texture = TRUE;
501 inst.Texture.Texture = TGSI_TEXTURE_2D;
502 inst.Src[0].Register.File = src_file;
503 inst.Src[0].Register.Index = src_index;
504 inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
505 inst.Src[1].Register.Index = sampler_index;
506
507 ctx->emit_instruction(ctx, &inst);
508 }
509
510
511 extern int
512 tgsi_transform_shader(const struct tgsi_token *tokens_in,
513 struct tgsi_token *tokens_out,
514 uint max_tokens_out,
515 struct tgsi_transform_context *ctx);
516
517
518 #endif /* TGSI_TRANSFORM_H */