tgsi/scan: get information about shader buffer usage
[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 = TGSI_WRITEMASK_XYZW;
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_op3_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 src1_file,
313 unsigned src1_index,
314 unsigned src2_file,
315 unsigned src2_index)
316 {
317 struct tgsi_full_instruction inst;
318
319 inst = tgsi_default_full_instruction();
320 inst.Instruction.Opcode = opcode;
321 inst.Instruction.NumDstRegs = 1;
322 inst.Dst[0].Register.File = dst_file,
323 inst.Dst[0].Register.Index = dst_index;
324 inst.Dst[0].Register.WriteMask = dst_writemask;
325 inst.Instruction.NumSrcRegs = 3;
326 inst.Src[0].Register.File = src0_file;
327 inst.Src[0].Register.Index = src0_index;
328 inst.Src[1].Register.File = src1_file;
329 inst.Src[1].Register.Index = src1_index;
330 inst.Src[2].Register.File = src2_file;
331 inst.Src[2].Register.Index = src2_index;
332
333 ctx->emit_instruction(ctx, &inst);
334 }
335
336
337
338 static inline void
339 tgsi_transform_op1_swz_inst(struct tgsi_transform_context *ctx,
340 unsigned opcode,
341 unsigned dst_file,
342 unsigned dst_index,
343 unsigned dst_writemask,
344 unsigned src0_file,
345 unsigned src0_index,
346 unsigned src0_swizzle)
347 {
348 struct tgsi_full_instruction inst;
349
350 inst = tgsi_default_full_instruction();
351 inst.Instruction.Opcode = opcode;
352 inst.Instruction.NumDstRegs = 1;
353 inst.Dst[0].Register.File = dst_file,
354 inst.Dst[0].Register.Index = dst_index;
355 inst.Dst[0].Register.WriteMask = dst_writemask;
356 inst.Instruction.NumSrcRegs = 1;
357 inst.Src[0].Register.File = src0_file;
358 inst.Src[0].Register.Index = src0_index;
359 switch (dst_writemask) {
360 case TGSI_WRITEMASK_X:
361 inst.Src[0].Register.SwizzleX = src0_swizzle;
362 break;
363 case TGSI_WRITEMASK_Y:
364 inst.Src[0].Register.SwizzleY = src0_swizzle;
365 break;
366 case TGSI_WRITEMASK_Z:
367 inst.Src[0].Register.SwizzleZ = src0_swizzle;
368 break;
369 case TGSI_WRITEMASK_W:
370 inst.Src[0].Register.SwizzleW = src0_swizzle;
371 break;
372 default:
373 ; /* nothing */
374 }
375
376 ctx->emit_instruction(ctx, &inst);
377 }
378
379
380 static inline void
381 tgsi_transform_op2_swz_inst(struct tgsi_transform_context *ctx,
382 unsigned opcode,
383 unsigned dst_file,
384 unsigned dst_index,
385 unsigned dst_writemask,
386 unsigned src0_file,
387 unsigned src0_index,
388 unsigned src0_swizzle,
389 unsigned src1_file,
390 unsigned src1_index,
391 unsigned src1_swizzle)
392 {
393 struct tgsi_full_instruction inst;
394
395 inst = tgsi_default_full_instruction();
396 inst.Instruction.Opcode = opcode;
397 inst.Instruction.NumDstRegs = 1;
398 inst.Dst[0].Register.File = dst_file,
399 inst.Dst[0].Register.Index = dst_index;
400 inst.Dst[0].Register.WriteMask = dst_writemask;
401 inst.Instruction.NumSrcRegs = 2;
402 inst.Src[0].Register.File = src0_file;
403 inst.Src[0].Register.Index = src0_index;
404 inst.Src[1].Register.File = src1_file;
405 inst.Src[1].Register.Index = src1_index;
406 switch (dst_writemask) {
407 case TGSI_WRITEMASK_X:
408 inst.Src[0].Register.SwizzleX = src0_swizzle;
409 inst.Src[1].Register.SwizzleX = src1_swizzle;
410 break;
411 case TGSI_WRITEMASK_Y:
412 inst.Src[0].Register.SwizzleY = src0_swizzle;
413 inst.Src[1].Register.SwizzleY = src1_swizzle;
414 break;
415 case TGSI_WRITEMASK_Z:
416 inst.Src[0].Register.SwizzleZ = src0_swizzle;
417 inst.Src[1].Register.SwizzleZ = src1_swizzle;
418 break;
419 case TGSI_WRITEMASK_W:
420 inst.Src[0].Register.SwizzleW = src0_swizzle;
421 inst.Src[1].Register.SwizzleW = src1_swizzle;
422 break;
423 default:
424 ; /* nothing */
425 }
426
427 ctx->emit_instruction(ctx, &inst);
428 }
429
430
431 static inline void
432 tgsi_transform_op3_swz_inst(struct tgsi_transform_context *ctx,
433 unsigned opcode,
434 unsigned dst_file,
435 unsigned dst_index,
436 unsigned dst_writemask,
437 unsigned src0_file,
438 unsigned src0_index,
439 unsigned src0_swizzle,
440 unsigned src0_negate,
441 unsigned src1_file,
442 unsigned src1_index,
443 unsigned src1_swizzle,
444 unsigned src2_file,
445 unsigned src2_index,
446 unsigned src2_swizzle)
447 {
448 struct tgsi_full_instruction inst;
449
450 inst = tgsi_default_full_instruction();
451 inst.Instruction.Opcode = opcode;
452 inst.Instruction.NumDstRegs = 1;
453 inst.Dst[0].Register.File = dst_file,
454 inst.Dst[0].Register.Index = dst_index;
455 inst.Dst[0].Register.WriteMask = dst_writemask;
456 inst.Instruction.NumSrcRegs = 3;
457 inst.Src[0].Register.File = src0_file;
458 inst.Src[0].Register.Index = src0_index;
459 inst.Src[0].Register.Negate = src0_negate;
460 inst.Src[1].Register.File = src1_file;
461 inst.Src[1].Register.Index = src1_index;
462 inst.Src[2].Register.File = src2_file;
463 inst.Src[2].Register.Index = src2_index;
464 switch (dst_writemask) {
465 case TGSI_WRITEMASK_X:
466 inst.Src[0].Register.SwizzleX = src0_swizzle;
467 inst.Src[1].Register.SwizzleX = src1_swizzle;
468 inst.Src[2].Register.SwizzleX = src2_swizzle;
469 break;
470 case TGSI_WRITEMASK_Y:
471 inst.Src[0].Register.SwizzleY = src0_swizzle;
472 inst.Src[1].Register.SwizzleY = src1_swizzle;
473 inst.Src[2].Register.SwizzleY = src2_swizzle;
474 break;
475 case TGSI_WRITEMASK_Z:
476 inst.Src[0].Register.SwizzleZ = src0_swizzle;
477 inst.Src[1].Register.SwizzleZ = src1_swizzle;
478 inst.Src[2].Register.SwizzleZ = src2_swizzle;
479 break;
480 case TGSI_WRITEMASK_W:
481 inst.Src[0].Register.SwizzleW = src0_swizzle;
482 inst.Src[1].Register.SwizzleW = src1_swizzle;
483 inst.Src[2].Register.SwizzleW = src2_swizzle;
484 break;
485 default:
486 ; /* nothing */
487 }
488
489 ctx->emit_instruction(ctx, &inst);
490 }
491
492
493 static inline void
494 tgsi_transform_kill_inst(struct tgsi_transform_context *ctx,
495 unsigned src_file,
496 unsigned src_index,
497 unsigned src_swizzle,
498 boolean negate)
499 {
500 struct tgsi_full_instruction inst;
501
502 inst = tgsi_default_full_instruction();
503 inst.Instruction.Opcode = TGSI_OPCODE_KILL_IF;
504 inst.Instruction.NumDstRegs = 0;
505 inst.Instruction.NumSrcRegs = 1;
506 inst.Src[0].Register.File = src_file;
507 inst.Src[0].Register.Index = src_index;
508 inst.Src[0].Register.SwizzleX =
509 inst.Src[0].Register.SwizzleY =
510 inst.Src[0].Register.SwizzleZ =
511 inst.Src[0].Register.SwizzleW = src_swizzle;
512 inst.Src[0].Register.Negate = negate;
513
514 ctx->emit_instruction(ctx, &inst);
515 }
516
517
518 static inline void
519 tgsi_transform_tex_inst(struct tgsi_transform_context *ctx,
520 unsigned dst_file,
521 unsigned dst_index,
522 unsigned src_file,
523 unsigned src_index,
524 unsigned tex_target,
525 unsigned sampler_index)
526 {
527 struct tgsi_full_instruction inst;
528
529 assert(tex_target < TGSI_TEXTURE_COUNT);
530
531 inst = tgsi_default_full_instruction();
532 inst.Instruction.Opcode = TGSI_OPCODE_TEX;
533 inst.Instruction.NumDstRegs = 1;
534 inst.Dst[0].Register.File = dst_file;
535 inst.Dst[0].Register.Index = dst_index;
536 inst.Instruction.NumSrcRegs = 2;
537 inst.Instruction.Texture = TRUE;
538 inst.Texture.Texture = tex_target;
539 inst.Src[0].Register.File = src_file;
540 inst.Src[0].Register.Index = src_index;
541 inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
542 inst.Src[1].Register.Index = sampler_index;
543
544 ctx->emit_instruction(ctx, &inst);
545 }
546
547
548 extern int
549 tgsi_transform_shader(const struct tgsi_token *tokens_in,
550 struct tgsi_token *tokens_out,
551 uint max_tokens_out,
552 struct tgsi_transform_context *ctx);
553
554
555 #endif /* TGSI_TRANSFORM_H */