• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 Andreas Baierl <ichgeh@imkreisrum.de>
3  *
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, sub license,
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:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
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 NON-INFRINGEMENT. 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
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include "util/u_math.h"
26 
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30 
31 #include "lima_context.h"
32 #include "lima_parser.h"
33 #include "lima_texture.h"
34 
35 #include "lima/ir/gp/codegen.h"
36 #include "lima/ir/pp/codegen.h"
37 
38 typedef struct {
39    char *info;
40 } render_state_info;
41 
42 static render_state_info render_state_infos[] = {
43    { .info = "BLEND_COLOR_BG", },
44    { .info = "BLEND_COLOR_RA", },
45    { .info = "ALPHA_BLEND", },
46    { .info = "DEPTH_TEST", },
47    { .info = "DEPTH_RANGE", },
48    { .info = "STENCIL_FRONT", },
49    { .info = "STENCIL_BACK", },
50    { .info = "STENCIL_TEST", },
51    { .info = "MULTI_SAMPLE", },
52    { .info = "SHADER_ADDRESS (FS)", },
53    { .info = "VARYING_TYPES", },
54    { .info = "UNIFORMS_ADDRESS (PP)", },
55    { .info = "TEXTURES_ADDRESS", },
56    { .info = "AUX0", },
57    { .info = "AUX1", },
58    { .info = "VARYINGS_ADDRESS", },
59 };
60 
61 /* VS CMD stream parser functions */
62 
63 static void
parse_vs_draw(FILE * fp,uint32_t * value1,uint32_t * value2)64 parse_vs_draw(FILE *fp, uint32_t *value1, uint32_t *value2)
65 {
66    if ((*value1 == 0x00000000) && (*value2 == 0x00000000))
67       fprintf(fp, "\t/* ---EMPTY CMD */\n");
68    else
69       fprintf(fp, "\t/* DRAW: num: %d, index_draw: %s */\n",
70               (*value1 & 0xff000000) >> 24 | (*value2 & 0x000000ff) << 8,
71               (*value1 & 0x00000001) ? "true" : "false");
72 }
73 
74 static void
parse_vs_shader_info(FILE * fp,uint32_t * value1,uint32_t * value2)75 parse_vs_shader_info(FILE *fp, uint32_t *value1, uint32_t *value2)
76 {
77    fprintf(fp, "\t/* SHADER_INFO: prefetch: %d, size: %d */\n",
78            (*value1 & 0xfff00000) >> 20,
79            (((*value1 & 0x000fffff) >> 10) + 1) << 4);
80 }
81 
82 static void
parse_vs_unknown1(FILE * fp,uint32_t * value1,uint32_t * value2)83 parse_vs_unknown1(FILE *fp, uint32_t *value1, uint32_t *value2)
84 {
85    fprintf(fp, "\t/* UNKNOWN_1 */\n");
86 }
87 
88 static void
parse_vs_varying_attribute_count(FILE * fp,uint32_t * value1,uint32_t * value2)89 parse_vs_varying_attribute_count(FILE *fp, uint32_t *value1, uint32_t *value2)
90 {
91    fprintf(fp, "\t/* VARYING_ATTRIBUTE_COUNT: nr_vary: %d, nr_attr: %d */\n",
92            ((*value1 & 0x00ffffff) >> 8) + 1, (*value1 >> 24) + 1);
93 }
94 
95 static void
parse_vs_attributes_address(FILE * fp,uint32_t * value1,uint32_t * value2)96 parse_vs_attributes_address(FILE *fp, uint32_t *value1, uint32_t *value2)
97 {
98    fprintf(fp, "\t/* ATTRIBUTES_ADDRESS: address: 0x%08x, size: %d */\n",
99            *value1, (*value2 & 0x0fffffff) >> 17);
100 }
101 
102 static void
parse_vs_varyings_address(FILE * fp,uint32_t * value1,uint32_t * value2)103 parse_vs_varyings_address(FILE *fp, uint32_t *value1, uint32_t *value2)
104 {
105    fprintf(fp, "\t/* VARYINGS_ADDRESS: varying info @ 0x%08x, size: %d */\n",
106            *value1, (*value2 & 0x0fffffff) >> 17);
107 }
108 
109 static void
parse_vs_uniforms_address(FILE * fp,uint32_t * value1,uint32_t * value2)110 parse_vs_uniforms_address(FILE *fp, uint32_t *value1, uint32_t *value2)
111 {
112    fprintf(fp, "\t/* UNIFORMS_ADDRESS (GP): address: 0x%08x, size: %d */\n",
113            *value1, (*value2 & 0x0fffffff) >> 12);
114 }
115 
116 static void
parse_vs_shader_address(FILE * fp,uint32_t * value1,uint32_t * value2)117 parse_vs_shader_address(FILE *fp, uint32_t *value1, uint32_t *value2)
118 {
119    fprintf(fp, "\t/* SHADER_ADDRESS (VS): address: 0x%08x, size: %d */\n",
120            *value1, (*value2 & 0x0fffffff) >> 12);
121 }
122 
123 static void
parse_vs_semaphore(FILE * fp,uint32_t * value1,uint32_t * value2)124 parse_vs_semaphore(FILE *fp, uint32_t *value1, uint32_t *value2)
125 {
126    if (*value1 == 0x00028000)
127       fprintf(fp, "\t/* SEMAPHORE_BEGIN_1 */\n");
128    else if (*value1 == 0x00000001)
129       fprintf(fp, "\t/* SEMAPHORE_BEGIN_2 */\n");
130    else if (*value1 == 0x00000000)
131       fprintf(fp, "\t/* SEMAPHORE_END: index_draw disabled */\n");
132    else if (*value1 == 0x00018000)
133       fprintf(fp, "\t/* SEMAPHORE_END: index_draw enabled */\n");
134    else
135       fprintf(fp, "\t/* SEMAPHORE - cmd unknown! */\n");
136 }
137 
138 static void
parse_vs_unknown2(FILE * fp,uint32_t * value1,uint32_t * value2)139 parse_vs_unknown2(FILE *fp, uint32_t *value1, uint32_t *value2)
140 {
141    fprintf(fp, "\t/* UNKNOWN_2 */\n");
142 }
143 
144 static void
parse_vs_continue(FILE * fp,uint32_t * value1,uint32_t * value2)145 parse_vs_continue(FILE *fp, uint32_t *value1, uint32_t *value2)
146 {
147    fprintf(fp, "\t/* CONTINUE: at 0x%08x */\n", *value1);
148 }
149 
150 void
lima_parse_vs(FILE * fp,uint32_t * data,int size,uint32_t start)151 lima_parse_vs(FILE *fp, uint32_t *data, int size, uint32_t start)
152 {
153    uint32_t *value1;
154    uint32_t *value2;
155 
156    fprintf(fp, "\n");
157    fprintf(fp, "/* ============ VS CMD STREAM BEGIN ============= */\n");
158    for (int i = 0; i * 4 < size; i += 2) {
159       value1 = &data[i];
160       value2 = &data[i + 1];
161       fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x 0x%08x",
162               start + i * 4, i * 4, *value1, *value2);
163 
164       if ((*value2 & 0xffff0000) == 0x00000000)
165          parse_vs_draw(fp, value1, value2);
166       else if ((*value2 & 0xff0000ff) == 0x10000040)
167          parse_vs_shader_info(fp, value1, value2);
168       else if ((*value2 & 0xff0000ff) == 0x10000041)
169          parse_vs_unknown1(fp, value1, value2);
170       else if ((*value2 & 0xff0000ff) == 0x10000042)
171          parse_vs_varying_attribute_count(fp, value1, value2);
172       else if ((*value2 & 0xff0000ff) == 0x20000000)
173          parse_vs_attributes_address(fp, value1, value2);
174       else if ((*value2 & 0xff0000ff) == 0x20000008)
175          parse_vs_varyings_address(fp, value1, value2);
176       else if ((*value2 & 0xff000000) == 0x30000000)
177          parse_vs_uniforms_address(fp, value1, value2);
178       else if ((*value2 & 0xff000000) == 0x40000000)
179          parse_vs_shader_address(fp, value1, value2);
180       else if ((*value2  & 0xff000000)== 0x50000000)
181          parse_vs_semaphore(fp, value1, value2);
182       else if ((*value2 & 0xff000000) == 0x60000000)
183          parse_vs_unknown2(fp, value1, value2);
184       else if ((*value2 & 0xff000000) == 0xf0000000)
185          parse_vs_continue(fp, value1, value2);
186       else
187          fprintf(fp, "\t/* --- unknown cmd --- */\n");
188    }
189    fprintf(fp, "/* ============ VS CMD STREAM END =============== */\n");
190    fprintf(fp, "\n");
191 }
192 
193 /* PLBU CMD stream parser functions */
194 
195 static void
parse_plbu_block_step(FILE * fp,uint32_t * value1,uint32_t * value2)196 parse_plbu_block_step(FILE *fp, uint32_t *value1, uint32_t *value2)
197 {
198    fprintf(fp, "\t/* BLOCK_STEP: shift_min: %d, shift_h: %d, shift_w: %d */\n",
199            (*value1 & 0xf0000000) >> 28,
200            (*value1 & 0x0fff0000) >> 16,
201            *value1 & 0x0000ffff);
202 }
203 
204 static void
parse_plbu_tiled_dimensions(FILE * fp,uint32_t * value1,uint32_t * value2)205 parse_plbu_tiled_dimensions(FILE *fp, uint32_t *value1, uint32_t *value2)
206 {
207    fprintf(fp, "\t/* TILED_DIMENSIONS: tiled_w: %d, tiled_h: %d */\n",
208            ((*value1 & 0xff000000) >> 24) + 1,
209            ((*value1 & 0x00ffff00) >> 8) + 1);
210 }
211 
212 static void
parse_plbu_block_stride(FILE * fp,uint32_t * value1,uint32_t * value2)213 parse_plbu_block_stride(FILE *fp, uint32_t *value1, uint32_t *value2)
214 {
215    fprintf(fp, "\t/* BLOCK_STRIDE: block_w: %d */\n", *value1 & 0x000000ff);
216 }
217 
218 static void
parse_plbu_array_address(FILE * fp,uint32_t * value1,uint32_t * value2)219 parse_plbu_array_address(FILE *fp, uint32_t *value1, uint32_t *value2)
220 {
221    fprintf(fp, "\t/* ARRAY_ADDRESS: gp_stream: 0x%08x, block_num (block_w * block_h): %d */\n",
222            *value1, (*value2 & 0x00ffffff) + 1);
223 }
224 
225 static void
parse_plbu_viewport_left(FILE * fp,float * value1,uint32_t * value2)226 parse_plbu_viewport_left(FILE *fp, float *value1, uint32_t *value2)
227 {
228    fprintf(fp, "\t/* VIEWPORT_LEFT: viewport_left: %f */\n", *value1);
229 }
230 
231 static void
parse_plbu_viewport_right(FILE * fp,float * value1,uint32_t * value2)232 parse_plbu_viewport_right(FILE *fp, float *value1, uint32_t *value2)
233 {
234    fprintf(fp, "\t/* VIEWPORT_RIGHT: viewport_right: %f */\n", *value1);
235 }
236 
237 static void
parse_plbu_viewport_bottom(FILE * fp,float * value1,uint32_t * value2)238 parse_plbu_viewport_bottom(FILE *fp, float *value1, uint32_t *value2)
239 {
240    fprintf(fp, "\t/* VIEWPORT_BOTTOM: viewport_bottom: %f */\n", *value1);
241 }
242 
243 static void
parse_plbu_viewport_top(FILE * fp,float * value1,uint32_t * value2)244 parse_plbu_viewport_top(FILE *fp, float *value1, uint32_t *value2)
245 {
246    fprintf(fp, "\t/* VIEWPORT_TOP: viewport_top: %f */\n", *value1);
247 }
248 
249 static void
parse_plbu_semaphore(FILE * fp,uint32_t * value1,uint32_t * value2)250 parse_plbu_semaphore(FILE *fp, uint32_t *value1, uint32_t *value2)
251 {
252    if (*value1 == 0x00010002)
253       fprintf(fp, "\t/* ARRAYS_SEMAPHORE_BEGIN */\n");
254    else if (*value1 == 0x00010001)
255       fprintf(fp, "\t/* ARRAYS_SEMAPHORE_END */\n");
256    else
257       fprintf(fp, "\t/* SEMAPHORE - cmd unknown! */\n");
258 }
259 
260 static void
parse_plbu_primitive_setup(FILE * fp,uint32_t * value1,uint32_t * value2)261 parse_plbu_primitive_setup(FILE *fp, uint32_t *value1, uint32_t *value2)
262 {
263    if (*value1 == 0x00000200)
264       fprintf(fp, "\t/* UNKNOWN_2 (PRIMITIVE_SETUP INIT?) */\n");
265    else
266       fprintf(fp, "\t/* PRIMITIVE_SETUP: %scull: %d (0x%x), index_size: %d */\n",
267               (*value1 & 0x1000) ? "force point size, " : "",
268               (*value1 & 0x000f0000) >> 16, (*value1 & 0x000f0000) >> 16,
269               (*value1 & 0x00000e00) >> 9);
270 }
271 
272 static void
parse_plbu_rsw_vertex_array(FILE * fp,uint32_t * value1,uint32_t * value2)273 parse_plbu_rsw_vertex_array(FILE *fp, uint32_t *value1, uint32_t *value2)
274 {
275    fprintf(fp, "\t/* RSW_VERTEX_ARRAY: rsw: 0x%08x, gl_pos: 0x%08x */\n",
276            *value1,
277            (*value2 & 0x0fffffff) << 4);
278 }
279 
280 static void
parse_plbu_scissors(FILE * fp,uint32_t * value1,uint32_t * value2)281 parse_plbu_scissors(FILE *fp, uint32_t *value1, uint32_t *value2)
282 {
283    float minx = (*value1 & 0xc0000000) >> 30 | (*value2 & 0x00001fff) << 2;
284    float maxx = ((*value2 & 0x0fffe000) >> 13) + 1;
285    float miny = *value1 & 0x00003fff;
286    float maxy = ((*value1 & 0x3fff8000) >> 15) + 1;
287 
288    fprintf(fp, "\t/* SCISSORS: minx: %f, maxx: %f, miny: %f, maxy: %f */\n",
289            minx, maxx, miny, maxy);
290 }
291 
292 static void
parse_plbu_unknown_1(FILE * fp,uint32_t * value1,uint32_t * value2)293 parse_plbu_unknown_1(FILE *fp, uint32_t *value1, uint32_t *value2)
294 {
295    fprintf(fp, "\t/* UNKNOWN_1 */\n");
296 }
297 
298 static void
parse_plbu_low_prim_size(FILE * fp,float * value1,uint32_t * value2)299 parse_plbu_low_prim_size(FILE *fp, float *value1, uint32_t *value2)
300 {
301    fprintf(fp, "\t/* LOW_PRIM_SIZE: size: %f */\n", *value1);
302 }
303 
304 static void
parse_plbu_depth_range_near(FILE * fp,float * value1,uint32_t * value2)305 parse_plbu_depth_range_near(FILE *fp, float *value1, uint32_t *value2)
306 {
307    fprintf(fp, "\t/* DEPTH_RANG_NEAR: depth_range: %f */\n", *value1);
308 }
309 
310 static void
parse_plbu_depth_range_far(FILE * fp,float * value1,uint32_t * value2)311 parse_plbu_depth_range_far(FILE *fp, float *value1, uint32_t *value2)
312 {
313    fprintf(fp, "\t/* DEPTH_RANGE_FAR: depth_range: %f */\n", *value1);
314 }
315 
316 static void
parse_plbu_indexed_dest(FILE * fp,uint32_t * value1,uint32_t * value2)317 parse_plbu_indexed_dest(FILE *fp, uint32_t *value1, uint32_t *value2)
318 {
319    fprintf(fp, "\t/* INDEXED_DEST: gl_pos: 0x%08x */\n", *value1);
320 }
321 
322 static void
parse_plbu_indexed_pt_size(FILE * fp,uint32_t * value1,uint32_t * value2)323 parse_plbu_indexed_pt_size(FILE *fp, uint32_t *value1, uint32_t *value2)
324 {
325    fprintf(fp, "\t/* INDEXED_PT_SIZE: pt_size: 0x%08x */\n", *value1);
326 }
327 
328 static void
parse_plbu_indices(FILE * fp,uint32_t * value1,uint32_t * value2)329 parse_plbu_indices(FILE *fp, uint32_t *value1, uint32_t *value2)
330 {
331    fprintf(fp, "\t/* INDICES: indices: 0x%08x */\n", *value1);
332 }
333 
334 static void
parse_plbu_draw_arrays(FILE * fp,uint32_t * value1,uint32_t * value2)335 parse_plbu_draw_arrays(FILE *fp, uint32_t *value1, uint32_t *value2)
336 {
337    if ((*value1 == 0x00000000) && (*value2 == 0x00000000)) {
338       fprintf(fp, "\t/* ---EMPTY CMD */\n");
339       return;
340    }
341 
342    uint32_t count = (*value1 & 0xff000000) >> 24 | (*value2 & 0x000000ff) << 8;
343    uint32_t start = *value1 & 0x00ffffff;
344    uint32_t mode = (*value2 & 0x001f0000) >> 16;
345 
346    fprintf(fp, "\t/* DRAW_ARRAYS: count: %d, start: %d, mode: %d (0x%x) */\n",
347            count, start, mode, mode);
348 }
349 
350 static void
parse_plbu_draw_elements(FILE * fp,uint32_t * value1,uint32_t * value2)351 parse_plbu_draw_elements(FILE *fp, uint32_t *value1, uint32_t *value2)
352 {
353    uint32_t count = (*value1 & 0xff000000) >> 24 | (*value2 & 0x000000ff) << 8;
354    uint32_t start = *value1 & 0x00ffffff;
355    uint32_t mode = (*value2 & 0x001f0000) >> 16;
356 
357    fprintf(fp, "\t/* DRAW_ELEMENTS: count: %d, start: %d, mode: %d (0x%x) */\n",
358            count, start, mode, mode);
359 }
360 
361 static void
parse_plbu_continue(FILE * fp,uint32_t * value1,uint32_t * value2)362 parse_plbu_continue(FILE *fp, uint32_t *value1, uint32_t *value2)
363 {
364    fprintf(fp, "\t/* CONTINUE: continue at 0x%08x */\n", *value1);
365 }
366 
367 static void
parse_plbu_end(FILE * fp,uint32_t * value1,uint32_t * value2)368 parse_plbu_end(FILE *fp, uint32_t *value1, uint32_t *value2)
369 {
370    fprintf(fp, "\t/* END (FINISH/FLUSH) */\n");
371 }
372 
373 void
lima_parse_plbu(FILE * fp,uint32_t * data,int size,uint32_t start)374 lima_parse_plbu(FILE *fp, uint32_t *data, int size, uint32_t start)
375 {
376    uint32_t *value1;
377    uint32_t *value2;
378 
379    fprintf(fp, "/* ============ PLBU CMD STREAM BEGIN ============= */\n");
380    for (int i = 0; i * 4 < size; i += 2) {
381       value1 = &data[i];
382       value2 = &data[i + 1];
383       fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x 0x%08x",
384               start + i * 4, i * 4, *value1, *value2);
385 
386       if ((*value2 & 0xffe00000) == 0x00000000)
387          parse_plbu_draw_arrays(fp, value1, value2);
388       else if ((*value2 & 0xffe00000) == 0x00200000)
389          parse_plbu_draw_elements(fp, value1, value2);
390       else if ((*value2 & 0xff000fff) == 0x10000100)
391          parse_plbu_indexed_dest(fp, value1, value2);
392       else if ((*value2 & 0xff000fff) == 0x10000101)
393          parse_plbu_indices(fp, value1, value2);
394       else if ((*value2 & 0xff000fff) == 0x10000102)
395          parse_plbu_indexed_pt_size(fp, value1, value2);
396       else if ((*value2 & 0xff000fff) == 0x10000105)
397          parse_plbu_viewport_bottom(fp, (float *)value1, value2);
398       else if ((*value2 & 0xff000fff) == 0x10000106)
399          parse_plbu_viewport_top(fp, (float *)value1, value2);
400       else if ((*value2 & 0xff000fff) == 0x10000107)
401          parse_plbu_viewport_left(fp, (float *)value1, value2);
402       else if ((*value2 & 0xff000fff) == 0x10000108)
403          parse_plbu_viewport_right(fp, (float *)value1, value2);
404       else if ((*value2 & 0xff000fff) == 0x10000109)
405          parse_plbu_tiled_dimensions(fp, value1, value2);
406       else if ((*value2 & 0xff000fff) == 0x1000010a)
407          parse_plbu_unknown_1(fp, value1, value2);
408       else if ((*value2 & 0xff000fff) == 0x1000010b) /* also unknown_2 */
409          parse_plbu_primitive_setup(fp, value1, value2);
410       else if ((*value2 & 0xff000fff) == 0x1000010c)
411          parse_plbu_block_step(fp, value1, value2);
412       else if ((*value2 & 0xff000fff) == 0x1000010d)
413          parse_plbu_low_prim_size(fp, (float *)value1, value2);
414       else if ((*value2 & 0xff000fff) == 0x1000010e)
415          parse_plbu_depth_range_near(fp, (float *)value1, value2);
416       else if ((*value2 & 0xff000fff) == 0x1000010f)
417          parse_plbu_depth_range_far(fp, (float *)value1, value2);
418       else if ((*value2 & 0xff000000) == 0x28000000)
419          parse_plbu_array_address(fp, value1, value2);
420       else if ((*value2 & 0xf0000000) == 0x30000000)
421          parse_plbu_block_stride(fp, value1, value2);
422       else if (*value2 == 0x50000000)
423          parse_plbu_end(fp, value1, value2);
424       else if ((*value2  & 0xf0000000)== 0x60000000)
425          parse_plbu_semaphore(fp, value1, value2);
426       else if ((*value2  & 0xf0000000)== 0x70000000)
427          parse_plbu_scissors(fp, value1, value2);
428       else if ((*value2  & 0xf0000000)== 0x80000000)
429          parse_plbu_rsw_vertex_array(fp, value1, value2);
430       else if ((*value2  & 0xf0000000)== 0xf0000000)
431          parse_plbu_continue(fp, value1, value2);
432       else
433          fprintf(fp, "\t/* --- unknown cmd --- */\n");
434    }
435    fprintf(fp, "/* ============ PLBU CMD STREAM END =============== */\n");
436    fprintf(fp, "\n");
437 }
438 
439 void
lima_parse_shader(FILE * fp,uint32_t * data,int size,bool is_frag)440 lima_parse_shader(FILE *fp, uint32_t *data, int size, bool is_frag)
441 {
442    uint32_t *value = &data[0];
443 
444    if (is_frag) {
445       uint32_t *bin = value;
446       uint32_t offt = 0;
447       uint32_t next_instr_length = 0;
448 
449       fprintf(fp, "/* ============ FS DISASSEMBLY BEGIN ============== */\n");
450 
451       do {
452          ppir_codegen_ctrl *ctrl = (ppir_codegen_ctrl *)bin;
453          fprintf(fp, "@%6d: ", offt);
454          ppir_disassemble_instr(bin, offt, fp);
455          bin += ctrl->count;
456          offt += ctrl->count;
457          next_instr_length = ctrl->next_count;
458       } while (next_instr_length);
459 
460       fprintf(fp, "/* ============ FS DISASSEMBLY END ================= */\n");
461    } else {
462       fprintf(fp, "/* ============ VS DISASSEMBLY BEGIN ============== */\n");
463       gpir_disassemble_program((gpir_codegen_instr *)value, size / sizeof(gpir_codegen_instr), fp);
464       fprintf(fp, "/* ============ VS DISASSEMBLY END ================= */\n");
465    }
466 }
467 
468 static void
parse_rsw(FILE * fp,uint32_t * value,int i,uint32_t * helper)469 parse_rsw(FILE *fp, uint32_t *value, int i, uint32_t *helper)
470 {
471    fprintf(fp, "\t/* %s", render_state_infos[i].info);
472 
473    switch (i) {
474    case 0: /* BLEND COLOR BG */
475       fprintf(fp, ": blend_color.color[1] = %f, blend_color.color[2] = %f */\n",
476               (float)(ubyte_to_float((*value & 0xffff0000) >> 16)),
477               (float)(ubyte_to_float(*value & 0x0000ffff)));
478       break;
479    case 1: /* BLEND COLOR RA */
480       fprintf(fp, ": blend_color.color[3] = %f, blend_color.color[0] = %f */\n",
481               (float)(ubyte_to_float((*value & 0xffff0000) >> 16)),
482               (float)(ubyte_to_float(*value & 0x0000ffff)));
483       break;
484    case 2: /* ALPHA BLEND */
485       fprintf(fp, "(1): colormask 0x%02x, rgb_func %d (%s), alpha_func %d (%s) */\n",
486               (*value & 0xf0000000) >> 28, /* colormask */
487               (*value & 0x00000007),
488               lima_get_blend_func_string((*value & 0x00000007)), /* rgb_func */
489               (*value & 0x00000038) >> 3,
490               lima_get_blend_func_string((*value & 0x00000038) >> 3)); /* alpha_func */
491       /* add a few tabs for alignment */
492       fprintf(fp, "\t\t\t\t\t\t/* %s(2)", render_state_infos[i].info);
493       fprintf(fp, ": rgb_src_factor %d (%s), rbg_dst_factor %d (%s) */\n",
494               (*value & 0x000007c0) >> 6,
495               lima_get_blendfactor_string((*value & 0x000007c0) >> 6), /* rgb_src_factor */
496               (*value & 0x0000f800) >> 11,
497               lima_get_blendfactor_string((*value & 0x0000f800) >> 11)); /* rgb_dst_factor */
498       fprintf(fp, "\t\t\t\t\t\t/* %s(3)", render_state_infos[i].info);
499       fprintf(fp, ": alpha_src_factor %d (%s), alpha_dst_factor %d (%s), bits 24-27 0x%02x */\n",
500               (*value & 0x000f0000) >> 16,
501               lima_get_blendfactor_string((*value & 0x000f0000) >> 16), /* alpha_src_factor */
502               (*value & 0x00f00000) >> 20,
503               lima_get_blendfactor_string((*value & 0x00f00000) >> 20), /* alpha_dst_factor */
504               (*value & 0x0f000000) >> 24); /* bits 24-27 */
505       break;
506    case 3: /* DEPTH TEST */
507       if ((*value & 0x00000001) == 0x00000001)
508          fprintf(fp, "(1): depth test enabled && writes allowed");
509       else
510          fprintf(fp, "(1): depth test disabled || writes not allowed");
511 
512       fprintf(fp, "\n\t\t\t\t\t\t/* %s(2)", render_state_infos[i].info);
513       fprintf(fp, ": depth_func %d (%s)", ((*value & 0x0000000e) >> 1),
514               lima_get_compare_func_string((*value & 0x0000000e) >> 1));
515       fprintf(fp, ", offset_scale: %d", (*value & 0x00ff0000) >> 16);
516       fprintf(fp, ", offset_units: %d", (*value & 0xff000000) >> 24);
517       if (*value & 0x400)
518          fprintf(fp, ", shader writes depth or stencil");
519       if (*value & 0x800)
520          fprintf(fp, ", shader writes depth");
521       if (*value & 0x1000)
522          fprintf(fp, ", shader writes stencil");
523       fprintf(fp, " */\n\t\t\t\t\t\t/* %s(3)", render_state_infos[i].info);
524       if ((*value & 0x00000010) == 0x00000010)
525          fprintf(fp, ": ignore depth clip near");
526       if ((*value & 0x00000020) == 0x00000020)
527          fprintf(fp, ", ignore depth clip far");
528       fprintf(fp, ", unknown bits 6-9: 0x%08x", *value & 0x000003c0);
529       fprintf(fp, ", unknown bits 13-15: 0x%08x */\n", *value & 0x00000e000);
530       break;
531    case 4: /* DEPTH RANGE */
532       fprintf(fp, ": viewport.far = %f, viewport.near = %f */\n",
533               (float)(ushort_to_float((*value & 0xffff0000) >> 16)),
534               (float)(ushort_to_float(*value & 0x0000ffff)));
535       break;
536    case 5: /* STENCIL FRONT */
537       fprintf(fp, "(1): valuemask 0x%02x, ref value %d (0x%02x), stencil_func %d (%s)*/\n",
538               (*value & 0xff000000) >> 24, /* valuemask */
539               (*value & 0x00ff0000) >> 16, (*value & 0x00ff0000) >> 16, /* ref value */
540               (*value & 0x00000007),
541               lima_get_compare_func_string((*value & 0x00000007))); /* stencil_func */
542       /* add a few tabs for alignment */
543       fprintf(fp, "\t\t\t\t\t\t/* %s(2)", render_state_infos[i].info);
544       fprintf(fp, ": fail_op %d (%s), zfail_op %d (%s), zpass_op %d (%s), unknown (12-15) 0x%02x */\n",
545               (*value & 0x00000038) >> 3,
546               lima_get_stencil_op_string((*value & 0x00000038) >> 3), /* fail_op */
547               (*value & 0x000001c0) >> 6,
548               lima_get_stencil_op_string((*value & 0x000001c0) >> 6), /* zfail_op */
549               (*value & 0x00000e00) >> 9,
550               lima_get_stencil_op_string((*value & 0x00000e00) >> 9), /* zpass_op */
551               (*value & 0x0000f000) >> 12); /* unknown */
552       break;
553    case 6: /* STENCIL BACK */
554       fprintf(fp, "(1): valuemask 0x%02x, ref value %d (0x%02x), stencil_func %d (%s)*/\n",
555               (*value & 0xff000000) >> 24, /* valuemask */
556               (*value & 0x00ff0000) >> 16, (*value & 0x00ff0000) >> 16, /* ref value */
557               (*value & 0x00000007),
558               lima_get_compare_func_string((*value & 0x00000007))); /* stencil_func */
559       /* add a few tabs for alignment */
560       fprintf(fp, "\t\t\t\t\t\t/* %s(2)", render_state_infos[i].info);
561       fprintf(fp, ": fail_op %d (%s), zfail_op %d (%s), zpass_op %d (%s), unknown (12-15) 0x%02x */\n",
562               (*value & 0x00000038) >> 3,
563               lima_get_stencil_op_string((*value & 0x00000038) >> 3), /* fail_op */
564               (*value & 0x000001c0) >> 6,
565               lima_get_stencil_op_string((*value & 0x000001c0) >> 6), /* zfail_op */
566               (*value & 0x00000e00) >> 9,
567               lima_get_stencil_op_string((*value & 0x00000e00) >> 9), /* zpass_op */
568               (*value & 0x0000f000) >> 12); /* unknown */
569       break;
570    case 7: /* STENCIL TEST */
571       fprintf(fp, "(1): stencil_front writemask 0x%02x, stencil_back writemask 0x%02x */\n",
572               (*value & 0x000000ff), /* front writemask */
573               (*value & 0x0000ff00) >> 8); /* back writemask */
574       /* add a few tabs for alignment */
575       fprintf(fp, "\t\t\t\t\t\t/* %s(2)", render_state_infos[i].info);
576       fprintf(fp, ": alpha_ref_value: 0x%02x */\n", (*value & 0x00ff0000) >> 16);
577       fprintf(fp, "\t\t\t\t\t\t/* %s(3)", render_state_infos[i].info);
578       fprintf(fp, ": unknown (bits 24-31) 0x%02x */\n",
579               (*value & 0xff000000) >> 24); /* unknown */
580       break;
581    case 8: /* MULTI SAMPLE */
582       if ((*value & 0x00000f00) == 0x00000000)
583          fprintf(fp, ": points");
584       else if ((*value & 0x00000f00) == 0x00000400)
585          fprintf(fp, ": lines");
586       else if ((*value & 0x00000f00) == 0x00000800)
587          fprintf(fp, ": triangles");
588       else
589          fprintf(fp, ": unknown");
590 
591       if ((*value & 0x00000078) == 0x00000068)
592          fprintf(fp, ", fb_samples */\n");
593       else if ((*value & 0x00000078) == 0x00000000)
594          fprintf(fp, " */\n");
595       else
596          fprintf(fp, ", UNKNOWN\n");
597       fprintf(fp, "\t\t\t\t\t\t/* %s(2)", render_state_infos[i].info);
598       fprintf(fp, ": alpha_test_func: %d (%s) */\n",
599               (*value & 0x00000007),
600               lima_get_compare_func_string((*value & 0x00000007))); /* alpha_test_func */
601       break;
602    case 9: /* SHADER ADDRESS */
603       fprintf(fp, ": fs shader @ 0x%08x, first instr length %d */\n",
604               *value & 0xffffffe0, *value & 0x0000001f);
605       break;
606    case 10: /* VARYING TYPES */
607       fprintf(fp, "(1): ");
608       int val, j;
609       /* 0 - 5 */
610       for (j = 0; j < 6; j++) {
611          val = (*value >> (j * 3)) & 0x07;
612          fprintf(fp, "val %d-%d, ", j, val);
613       }
614       /* 6 - 9 */
615       /* add a few tabs for alignment */
616       fprintf(fp, "\n\t\t\t\t\t\t/* %s(2): ", render_state_infos[i].info);
617       for (j = 6; j < 10; j++) {
618          val = (*value >> (j * 3)) & 0x07;
619          fprintf(fp, "val %d-%d, ", j, val);
620       }
621       /* 10 */
622       val = ((*value & 0xc0000000) >> 30) | ((*helper & 0x00000001) << 2);
623       fprintf(fp, "val %d-%d, ", j, val);
624       j++;
625       /* 11 */
626       val = (*helper & 0x0000000e) >> 1;
627       fprintf(fp, "val %d-%d */\n", j, val);
628       break;
629    case 11: /* UNIFORMS ADDRESS */
630       fprintf(fp, ": pp uniform info @ 0x%08x, bits: 0x%01x */\n",
631               *value & 0xfffffff0, *value & 0x0000000f);
632       break;
633    case 12: /* TEXTURES ADDRESS */
634       fprintf(fp, ": address: 0x%08x */\n", *value);
635       break;
636    case 13: /* AUX0 */
637       fprintf(fp, "(1): varying_stride: %d", /* bits 0 - 4 varying stride, 8 aligned */
638               (*value & 0x0000001f) << 3);
639       if ((*value & 0x00000020) == 0x00000020) /* bit 5 has num_samplers */
640          fprintf(fp, ", num_samplers %d",
641                  (*value & 0xffffc000) >> 14); /* bits 14 - 31 num_samplers */
642 
643       if ((*value & 0x00000080) == 0x00000080) /* bit 7 has_fs_uniforms */
644          fprintf(fp, ", has_fs_uniforms */");
645       else
646          fprintf(fp, " */");
647 
648       fprintf(fp, "\n\t\t\t\t\t\t/* %s(2):", render_state_infos[i].info);
649       if ((*value & 0x00000200) == 0x00000200) /* bit 9 early-z */
650          fprintf(fp, " early-z enabled");
651       else
652          fprintf(fp, " early-z disabled");
653 
654       if ((*value & 0x00001000) == 0x00001000) /* bit 12 pixel-kill */
655          fprintf(fp, ", pixel kill enabled");
656       else
657          fprintf(fp, ", pixel kill disabled");
658 
659       if ((*value & 0x00000040) == 0x00000040) /* bit 6 unknown */
660          fprintf(fp, ", bit 6 set");
661 
662       if ((*value & 0x00000100) == 0x00000100) /* bit 8 unknown */
663          fprintf(fp, ", bit 8 set");
664 
665       if (((*value & 0x00000c00) >> 10) > 0) /* bit 10 - 11 unknown */
666          fprintf(fp, ", bit 10 - 11: %d", ((*value & 0x00000c00) >> 10));
667 
668       if ((*value & 0x00002000) == 0x00002000) /* bit 13 unknown */
669          fprintf(fp, ", bit 13 set");
670       fprintf(fp, " */\n");
671       break;
672    case 14: /* AUX1 */
673       fprintf(fp, ": ");
674       if ((*value & 0x00002000) == 0x00002000)
675          fprintf(fp, "blend->base.dither true, ");
676 
677       if ((*value & 0x00001000) == 0x00001000)
678          fprintf(fp, "glFrontFace(GL_CCW), ");
679       else
680          fprintf(fp, "glFrontFace(GL_CW), ");
681 
682       if ((*value & 0x00010000) == 0x00010000)
683          fprintf(fp, "ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer true ");
684       fprintf(fp, "*/\n");
685       break;
686    case 15: /* VARYINGS ADDRESS */
687       fprintf(fp, ": varyings @ 0x%08x */\n", *value & 0xfffffff0);
688       break;
689    default: /* should never be executed! */
690       fprintf(fp, ": something went wrong!!! */\n");
691       break;
692    }
693 }
694 
695 void
lima_parse_render_state(FILE * fp,uint32_t * data,int size,uint32_t start)696 lima_parse_render_state(FILE *fp, uint32_t *data, int size, uint32_t start)
697 {
698    uint32_t *value;
699 
700    fprintf(fp, "/* ============ RSW BEGIN ========================= */\n");
701    for (int i = 0; i * 4 < size; i++) {
702       value = &data[i];
703       fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x",
704               start + i * 4, i * 4, *value);
705       if (i == 10)
706          parse_rsw(fp, value, i, &data[15]);
707       else
708          parse_rsw(fp, value, i, NULL);
709    }
710    fprintf(fp, "/* ============ RSW END =========================== */\n");
711 }
712 
713 static void
parse_texture(FILE * fp,uint32_t * data,uint32_t start,uint32_t offset)714 parse_texture(FILE *fp, uint32_t *data, uint32_t start, uint32_t offset)
715 {
716    uint32_t i = 0;
717    offset /= 4;
718    lima_tex_desc *desc = (lima_tex_desc *)&data[offset];
719 
720    /* Word 0 */
721    fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x\n",
722            start + i * 4, i * 4, *(&data[i + offset]));
723    i++;
724    fprintf(fp, "\t format: 0x%x (%d)\n", desc->format, desc->format);
725    fprintf(fp, "\t flag1: 0x%x (%d)\n", desc->flag1, desc->flag1);
726    fprintf(fp, "\t swap_r_b: 0x%x (%d)\n", desc->swap_r_b, desc->swap_r_b);
727    fprintf(fp, "\t unknown_0_1: 0x%x (%d)\n", desc->unknown_0_1, desc->unknown_0_1);
728    fprintf(fp, "\t stride: 0x%x (%d)\n", desc->stride, desc->stride);
729    fprintf(fp, "\t unknown_0_2: 0x%x (%d)\n", desc->unknown_0_2, desc->unknown_0_2);
730 
731    /* Word 1 - 3 */
732    fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x 0x%08x 0x%08x\n",
733            start + i * 4, i * 4, *(&data[i + offset]), *(&data[i + 1 + offset]), *(&data[i + 2 + offset]));
734    i += 3;
735    fprintf(fp, "\t unknown_1_1: 0x%x (%d)\n", desc->unknown_1_1, desc->unknown_1_1);
736    fprintf(fp, "\t unnorm_coords: 0x%x (%d)\n", desc->unnorm_coords, desc->unnorm_coords);
737    fprintf(fp, "\t unknown_1_2: 0x%x (%d)\n", desc->unknown_1_2, desc->unknown_1_2);
738    fprintf(fp, "\t texture_type: 0x%x (%d)\n", desc->texture_type, desc->texture_type);
739    fprintf(fp, "\t min_lod: 0x%x (%d) (%f)\n", desc->min_lod, desc->min_lod, lima_fixed8_to_float(desc->min_lod));
740    fprintf(fp, "\t max_lod: 0x%x (%d) (%f)\n", desc->max_lod, desc->max_lod, lima_fixed8_to_float(desc->max_lod));
741    fprintf(fp, "\t lod_bias: 0x%x (%d) (%f)\n", desc->lod_bias, desc->lod_bias, lima_fixed8_to_float(desc->lod_bias));
742    fprintf(fp, "\t unknown_2_1: 0x%x (%d)\n", desc->unknown_2_1, desc->unknown_2_1);
743    fprintf(fp, "\t has_stride: 0x%x (%d)\n", desc->has_stride, desc->has_stride);
744    fprintf(fp, "\t min_mipfilter_2: 0x%x (%d)\n", desc->min_mipfilter_2, desc->min_mipfilter_2);
745    fprintf(fp, "\t min_img_filter_nearest: 0x%x (%d)\n", desc->min_img_filter_nearest, desc->min_img_filter_nearest);
746    fprintf(fp, "\t mag_img_filter_nearest: 0x%x (%d)\n", desc->mag_img_filter_nearest, desc->mag_img_filter_nearest);
747    fprintf(fp, "\t wrap_s_clamp_to_edge: 0x%x (%d)\n", desc->wrap_s_clamp_to_edge, desc->wrap_s_clamp_to_edge);
748    fprintf(fp, "\t wrap_s_clamp: 0x%x (%d)\n", desc->wrap_s_clamp, desc->wrap_s_clamp);
749    fprintf(fp, "\t wrap_s_mirror_repeat: 0x%x (%d)\n", desc->wrap_s_mirror_repeat, desc->wrap_s_mirror_repeat);
750    fprintf(fp, "\t wrap_t_clamp_to_edge: 0x%x (%d)\n", desc->wrap_t_clamp_to_edge, desc->wrap_t_clamp_to_edge);
751    fprintf(fp, "\t wrap_t_clamp: 0x%x (%d)\n", desc->wrap_t_clamp, desc->wrap_t_clamp);
752    fprintf(fp, "\t wrap_t_mirror_repeat: 0x%x (%d)\n", desc->wrap_t_mirror_repeat, desc->wrap_t_mirror_repeat);
753    fprintf(fp, "\t unknown_2_2: 0x%x (%d)\n", desc->unknown_2_2, desc->unknown_2_2);
754    fprintf(fp, "\t width: 0x%x (%d)\n", desc->width, desc->width);
755    fprintf(fp, "\t height: 0x%x (%d)\n", desc->height, desc->height);
756    fprintf(fp, "\t unknown_3_1: 0x%x (%d)\n", desc->unknown_3_1, desc->unknown_3_1);
757    fprintf(fp, "\t unknown_3_2: 0x%x (%d)\n", desc->unknown_3_2, desc->unknown_3_2);
758 
759    /* Word 4 */
760    fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x\n",
761            start + i * 4, i * 4, *(&data[i + offset]));
762    i++;
763    fprintf(fp, "\t unknown_4: 0x%x (%d)\n", desc->unknown_4, desc->unknown_4);
764 
765    /* Word 5 */
766    fprintf(fp, "/* 0x%08x (0x%08x) */\t0x%08x\n",
767            start + i * 4, i * 4, *(&data[i + offset]));
768    i++;
769    fprintf(fp, "\t unknown_5: 0x%x (%d)\n", desc->unknown_5, desc->unknown_5);
770 
771    /* Word 6 - */
772    fprintf(fp, "/* 0x%08x (0x%08x) */",
773            start + i * 4, i * 4);
774    fprintf(fp, "\t");
775 
776    int miplevels = (int)lima_fixed8_to_float(desc->max_lod);
777    for (int k = 0; k < ((((miplevels + 1) * 26) + 64) / 32); k++)
778       fprintf(fp, "0x%08x ", *(&data[i + offset + k]));
779    fprintf(fp, "\n");
780 
781    i++;
782    fprintf(fp, "\t unknown_6_1: 0x%x (%d)\n", desc->va_s.unknown_6_1, desc->va_s.unknown_6_1);
783    fprintf(fp, "\t layout: 0x%x (%d)\n", desc->va_s.layout, desc->va_s.layout);
784    fprintf(fp, "\t unknown_6_2: 0x%x (%d)\n", desc->va_s.unknown_6_2, desc->va_s.unknown_6_2);
785    fprintf(fp, "\t unknown_6_3: 0x%x (%d)\n", desc->va_s.unknown_6_3, desc->va_s.unknown_6_3);
786 
787    /* first level */
788    fprintf(fp, "\t va_0: 0x%x \n", desc->va_s.va_0 << 6);
789 
790    /* second level up to desc->miplevels */
791    int j;
792    unsigned va_bit_idx;
793    unsigned va_idx;
794    uint32_t va;
795    uint32_t va_1;
796    uint32_t va_2;
797    for (j = 1; j <= miplevels; j++) {
798       va = 0;
799       va_1 = 0;
800       va_2 = 0;
801 
802       va_bit_idx = VA_BIT_OFFSET + (VA_BIT_SIZE * j);
803       va_idx = va_bit_idx / 32;
804       va_bit_idx %= 32;
805 
806       /* the first (32 - va_bit_idx) bits */
807       va_1 |= (*(&data[i + offset + va_idx - 1]) >> va_bit_idx);
808 
809       /* do we need some bits from the following word? */
810       if (va_bit_idx > 6) {
811          /* shift left and right again to erase the unneeded bits, keep space for va1 */
812          va_2 |= (*(&data[i + offset + va_idx]) << (2 * 32 - VA_BIT_SIZE - va_bit_idx));
813          va_2 >>= ((2 * 32 - VA_BIT_SIZE - va_bit_idx) - (32 - va_bit_idx));
814          va |= va_2;
815       }
816       va |= va_1;
817       va <<= 6;
818       fprintf(fp, "\t va_%d: 0x%x \n", j, va);
819    }
820 }
821 
822 void
lima_parse_texture_descriptor(FILE * fp,uint32_t * data,int size,uint32_t start,uint32_t offset)823 lima_parse_texture_descriptor(FILE *fp, uint32_t *data, int size, uint32_t start, uint32_t offset)
824 {
825    fprintf(fp, "/* ============ TEXTURE BEGIN ===================== */\n");
826    parse_texture(fp, data, start, offset);
827    fprintf(fp, "/* ============ TEXTURE END ======================= */\n");
828 }
829