2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include <sys/types.h>
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_text.h"
37 #include "tgsi/tgsi_dump.h"
39 #include "freedreno_util.h"
41 #include "ir3_compiler.h"
42 #include "instr-a3xx.h"
45 static void dump_reg(const char *name
, uint32_t r
)
48 debug_printf("; %s: r%d.%c\n", name
, r
>> 2, "xyzw"[r
& 0x3]);
51 static void dump_semantic(struct ir3_shader_variant
*so
,
52 unsigned sem
, const char *name
)
55 regid
= ir3_find_output_regid(so
, ir3_semantic_name(sem
, 0));
56 dump_reg(name
, regid
);
59 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
62 const char *type
= (so
->type
== SHADER_VERTEX
) ? "VERT" : "FRAG";
64 // for debug, dump some before/after info:
65 // TODO make gpu_id configurable on cmdline
66 bin
= ir3_shader_assemble(so
, 320);
67 if (fd_mesa_debug
& FD_DBG_DISASM
) {
68 struct ir3_block
*block
= so
->ir
->block
;
69 struct ir3_register
*reg
;
73 debug_printf("; %s: %s\n", type
, str
);
76 for (i
= 0; i
< block
->ninputs
; i
++) {
77 if (!block
->inputs
[i
]) {
78 debug_printf("; in%d unused\n", i
);
81 reg
= block
->inputs
[i
]->regs
[0];
83 debug_printf("@in(%sr%d.%c)\tin%d\n",
84 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
85 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
88 for (i
= 0; i
< block
->noutputs
; i
++) {
89 if (!block
->outputs
[i
]) {
90 debug_printf("; out%d unused\n", i
);
93 /* kill shows up as a virtual output.. skip it! */
94 if (is_kill(block
->outputs
[i
]))
96 reg
= block
->outputs
[i
]->regs
[0];
98 debug_printf("@out(%sr%d.%c)\tout%d\n",
99 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
100 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
103 /* hack to deal w/ old compiler:
104 * TODO maybe we can just keep this path? I guess should
105 * be good enough (other than not able to deal w/ half)
107 for (i
= 0; i
< so
->inputs_count
; i
++) {
108 unsigned j
, regid
= so
->inputs
[i
].regid
;
109 for (j
= 0; j
< so
->inputs
[i
].ncomp
; j
++) {
110 debug_printf("@in(r%d.%c)\tin%d\n",
111 (regid
>> 2), "xyzw"[regid
& 0x3], (i
* 4) + j
);
115 for (i
= 0; i
< so
->outputs_count
; i
++) {
116 unsigned j
, regid
= so
->outputs
[i
].regid
;
117 for (j
= 0; j
< 4; j
++) {
118 debug_printf("@out(r%d.%c)\tout%d\n",
119 (regid
>> 2), "xyzw"[regid
& 0x3], (i
* 4) + j
);
125 disasm_a3xx(bin
, so
->info
.sizedwords
, 0, so
->type
);
127 debug_printf("; %s: outputs:", type
);
128 for (i
= 0; i
< so
->outputs_count
; i
++) {
129 uint8_t regid
= so
->outputs
[i
].regid
;
130 ir3_semantic sem
= so
->outputs
[i
].semantic
;
131 debug_printf(" r%d.%c (%u:%u)",
132 (regid
>> 2), "xyzw"[regid
& 0x3],
133 sem2name(sem
), sem2idx(sem
));
136 debug_printf("; %s: inputs:", type
);
137 for (i
= 0; i
< so
->inputs_count
; i
++) {
138 uint8_t regid
= so
->inputs
[i
].regid
;
139 ir3_semantic sem
= so
->inputs
[i
].semantic
;
140 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
141 (regid
>> 2), "xyzw"[regid
& 0x3],
142 sem2name(sem
), sem2idx(sem
),
143 so
->inputs
[i
].compmask
,
150 /* print generic shader info: */
151 debug_printf("; %s: %u instructions, %d half, %d full\n", type
,
152 so
->info
.instrs_count
,
153 so
->info
.max_half_reg
+ 1,
154 so
->info
.max_reg
+ 1);
156 /* print shader type specific info: */
159 dump_semantic(so
, TGSI_SEMANTIC_POSITION
, "pos");
160 dump_semantic(so
, TGSI_SEMANTIC_PSIZE
, "psize");
162 case SHADER_FRAGMENT
:
163 dump_reg("pos (bary)", so
->pos_regid
);
164 dump_semantic(so
, TGSI_SEMANTIC_POSITION
, "posz");
165 dump_semantic(so
, TGSI_SEMANTIC_COLOR
, "color");
166 /* these two are hard-coded since we don't know how to
167 * program them to anything but all 0's...
170 debug_printf("; fragcoord: r0.x\n");
172 debug_printf("; fragface: hr0.x\n");
184 read_file(const char *filename
, void **ptr
, size_t *size
)
191 fd
= open(filename
, O_RDONLY
);
193 warnx("couldn't open `%s'", filename
);
197 ret
= fstat(fd
, &st
);
199 errx(1, "couldn't stat `%s'", filename
);
202 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
203 if (*ptr
== MAP_FAILED
)
204 errx(1, "couldn't map `%s'", filename
);
211 static void reset_variant(struct ir3_shader_variant
*v
, const char *msg
)
215 v
->outputs_count
= 0;
218 v
->immediates_count
= 0;
221 static void print_usage(void)
223 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
224 printf(" --verbose - verbose compiler/debug messages\n");
225 printf(" --binning-pass - generate binning pass shader (VERT)\n");
226 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
227 printf(" --half-precision - use half-precision\n");
228 printf(" --alpha - generate render-to-alpha shader (FRAG)\n");
229 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
230 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
231 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
232 printf(" --nocp - disable copy propagation\n");
233 printf(" --help - show this message\n");
236 int main(int argc
, char **argv
)
239 const char *filename
;
240 struct tgsi_token toks
[65536];
241 struct tgsi_parse_context parse
;
242 struct ir3_shader_variant v
;
243 struct ir3_shader_key key
= {};
248 fd_mesa_debug
|= FD_DBG_DISASM
;
250 /* cmdline args which impact shader variant get spit out in a
251 * comment on the first line.. a quick/dirty way to preserve
252 * that info so when ir3test recompiles the shader with a new
253 * compiler version, we use the same shader-key settings:
255 debug_printf("; options:");
258 if (!strcmp(argv
[n
], "--verbose")) {
259 fd_mesa_debug
|= FD_DBG_OPTDUMP
| FD_DBG_MSGS
| FD_DBG_OPTMSGS
;
264 if (!strcmp(argv
[n
], "--binning-pass")) {
265 debug_printf(" %s", argv
[n
]);
266 key
.binning_pass
= true;
271 if (!strcmp(argv
[n
], "--color-two-side")) {
272 debug_printf(" %s", argv
[n
]);
273 key
.color_two_side
= true;
278 if (!strcmp(argv
[n
], "--half-precision")) {
279 debug_printf(" %s", argv
[n
]);
280 key
.half_precision
= true;
285 if (!strcmp(argv
[n
], "--alpha")) {
286 debug_printf(" %s", argv
[n
]);
292 if (!strcmp(argv
[n
], "--saturate-s")) {
293 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
294 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
299 if (!strcmp(argv
[n
], "--saturate-t")) {
300 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
301 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
306 if (!strcmp(argv
[n
], "--saturate-r")) {
307 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
308 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
313 if (!strcmp(argv
[n
], "--nocp")) {
314 fd_mesa_debug
|= FD_DBG_NOCP
;
319 if (!strcmp(argv
[n
], "--help")) {
330 memset(&v
, 0, sizeof(v
));
333 ret
= read_file(filename
, &ptr
, &size
);
339 if (!tgsi_text_translate(ptr
, toks
, Elements(toks
)))
340 errx(1, "could not parse `%s'", filename
);
342 tgsi_parse_init(&parse
, toks
);
343 switch (parse
.FullHeader
.Processor
.Processor
) {
344 case TGSI_PROCESSOR_FRAGMENT
:
345 v
.type
= SHADER_FRAGMENT
;
347 case TGSI_PROCESSOR_VERTEX
:
348 v
.type
= SHADER_VERTEX
;
350 case TGSI_PROCESSOR_COMPUTE
:
351 v
.type
= SHADER_COMPUTE
;
355 if (!(fd_mesa_debug
& FD_DBG_NOOPT
)) {
356 /* with new compiler: */
357 info
= "new compiler";
358 ret
= ir3_compile_shader(&v
, toks
, key
, true);
361 reset_variant(&v
, "new compiler failed, trying without copy propagation!");
362 info
= "new compiler (no copy propagation)";
363 ret
= ir3_compile_shader(&v
, toks
, key
, false);
365 reset_variant(&v
, "new compiler failed, trying fallback!\n");
370 info
= "old compiler";
371 ret
= ir3_compile_shader_old(&v
, toks
, key
);
375 fprintf(stderr
, "old compiler failed!\n");