radeonsi: Fix sampler views for depth textures.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast_debug.c
1 #include <inttypes.h> /* for PRIu64 macro */
2 #include "util/u_math.h"
3 #include "lp_rast_priv.h"
4 #include "lp_state_fs.h"
5
6 struct tile {
7 int coverage;
8 int overdraw;
9 const struct lp_rast_state *state;
10 char data[TILE_SIZE][TILE_SIZE];
11 };
12
13 static char get_label( int i )
14 {
15 static const char *cmd_labels = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
16 unsigned max_label = (2*26+10);
17
18 if (i < max_label)
19 return cmd_labels[i];
20 else
21 return '?';
22 }
23
24
25
26 static const char *cmd_names[LP_RAST_OP_MAX] =
27 {
28 "clear_color",
29 "clear_zstencil",
30 "triangle_1",
31 "triangle_2",
32 "triangle_3",
33 "triangle_4",
34 "triangle_5",
35 "triangle_6",
36 "triangle_7",
37 "triangle_8",
38 "triangle_3_4",
39 "triangle_3_16",
40 "triangle_4_16",
41 "shade_tile",
42 "shade_tile_opaque",
43 "begin_query",
44 "end_query",
45 "set_state",
46 };
47
48 static const char *cmd_name(unsigned cmd)
49 {
50 assert(Elements(cmd_names) > cmd);
51 return cmd_names[cmd];
52 }
53
54 static const struct lp_fragment_shader_variant *
55 get_variant( const struct lp_rast_state *state,
56 const struct cmd_block *block,
57 int k )
58 {
59 if (!state)
60 return NULL;
61
62 if (block->cmd[k] == LP_RAST_OP_SHADE_TILE ||
63 block->cmd[k] == LP_RAST_OP_SHADE_TILE_OPAQUE ||
64 block->cmd[k] == LP_RAST_OP_TRIANGLE_1 ||
65 block->cmd[k] == LP_RAST_OP_TRIANGLE_2 ||
66 block->cmd[k] == LP_RAST_OP_TRIANGLE_3 ||
67 block->cmd[k] == LP_RAST_OP_TRIANGLE_4 ||
68 block->cmd[k] == LP_RAST_OP_TRIANGLE_5 ||
69 block->cmd[k] == LP_RAST_OP_TRIANGLE_6 ||
70 block->cmd[k] == LP_RAST_OP_TRIANGLE_7)
71 return state->variant;
72
73 return NULL;
74 }
75
76
77 static boolean
78 is_blend( const struct lp_rast_state *state,
79 const struct cmd_block *block,
80 int k )
81 {
82 const struct lp_fragment_shader_variant *variant = get_variant(state, block, k);
83
84 if (variant)
85 return variant->key.blend.rt[0].blend_enable;
86
87 return FALSE;
88 }
89
90
91
92 static void
93 debug_bin( const struct cmd_bin *bin )
94 {
95 const struct lp_rast_state *state = NULL;
96 const struct cmd_block *head = bin->head;
97 int i, j = 0;
98
99 debug_printf("bin %d,%d:\n", bin->x, bin->y);
100
101 while (head) {
102 for (i = 0; i < head->count; i++, j++) {
103 if (head->cmd[i] == LP_RAST_OP_SET_STATE)
104 state = head->arg[i].state;
105
106 debug_printf("%d: %s %s\n", j,
107 cmd_name(head->cmd[i]),
108 is_blend(state, head, i) ? "blended" : "");
109 }
110 head = head->next;
111 }
112 }
113
114
115 static void plot(struct tile *tile,
116 int x, int y,
117 char val,
118 boolean blend)
119 {
120 if (tile->data[x][y] == ' ')
121 tile->coverage++;
122 else
123 tile->overdraw++;
124
125 tile->data[x][y] = val;
126 }
127
128
129
130
131
132
133 static int
134 debug_shade_tile(int x, int y,
135 const union lp_rast_cmd_arg arg,
136 struct tile *tile,
137 char val)
138 {
139 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
140 boolean blend;
141 unsigned i,j;
142
143 if (!tile->state)
144 return 0;
145
146 blend = tile->state->variant->key.blend.rt[0].blend_enable;
147
148 if (inputs->disable)
149 return 0;
150
151 for (i = 0; i < TILE_SIZE; i++)
152 for (j = 0; j < TILE_SIZE; j++)
153 plot(tile, i, j, val, blend);
154
155 return TILE_SIZE * TILE_SIZE;
156 }
157
158 static int
159 debug_clear_tile(int x, int y,
160 const union lp_rast_cmd_arg arg,
161 struct tile *tile,
162 char val)
163 {
164 unsigned i,j;
165
166 for (i = 0; i < TILE_SIZE; i++)
167 for (j = 0; j < TILE_SIZE; j++)
168 plot(tile, i, j, val, FALSE);
169
170 return TILE_SIZE * TILE_SIZE;
171
172 }
173
174
175 static int
176 debug_triangle(int tilex, int tiley,
177 const union lp_rast_cmd_arg arg,
178 struct tile *tile,
179 char val)
180 {
181 const struct lp_rast_triangle *tri = arg.triangle.tri;
182 unsigned plane_mask = arg.triangle.plane_mask;
183 const struct lp_rast_plane *tri_plane = GET_PLANES(tri);
184 struct lp_rast_plane plane[8];
185 int x, y;
186 int count = 0;
187 unsigned i, nr_planes = 0;
188 boolean blend = tile->state->variant->key.blend.rt[0].blend_enable;
189
190 if (tri->inputs.disable) {
191 /* This triangle was partially binned and has been disabled */
192 return 0;
193 }
194
195 while (plane_mask) {
196 plane[nr_planes] = tri_plane[u_bit_scan(&plane_mask)];
197 plane[nr_planes].c = (plane[nr_planes].c +
198 plane[nr_planes].dcdy * tiley -
199 plane[nr_planes].dcdx * tilex);
200 nr_planes++;
201 }
202
203 for(y = 0; y < TILE_SIZE; y++)
204 {
205 for(x = 0; x < TILE_SIZE; x++)
206 {
207 for (i = 0; i < nr_planes; i++)
208 if (plane[i].c <= 0)
209 goto out;
210
211 plot(tile, x, y, val, blend);
212 count++;
213
214 out:
215 for (i = 0; i < nr_planes; i++)
216 plane[i].c -= plane[i].dcdx;
217 }
218
219 for (i = 0; i < nr_planes; i++) {
220 plane[i].c += plane[i].dcdx * TILE_SIZE;
221 plane[i].c += plane[i].dcdy;
222 }
223 }
224 return count;
225 }
226
227
228
229
230
231 static void
232 do_debug_bin( struct tile *tile,
233 const struct cmd_bin *bin,
234 boolean print_cmds)
235 {
236 unsigned k, j = 0;
237 const struct cmd_block *block;
238
239 int tx = bin->x * TILE_SIZE;
240 int ty = bin->y * TILE_SIZE;
241
242 memset(tile->data, ' ', sizeof tile->data);
243 tile->coverage = 0;
244 tile->overdraw = 0;
245 tile->state = NULL;
246
247 for (block = bin->head; block; block = block->next) {
248 for (k = 0; k < block->count; k++, j++) {
249 boolean blend = is_blend(tile->state, block, k);
250 char val = get_label(j);
251 int count = 0;
252
253 if (print_cmds)
254 debug_printf("%c: %15s", val, cmd_name(block->cmd[k]));
255
256 if (block->cmd[k] == LP_RAST_OP_SET_STATE)
257 tile->state = block->arg[k].state;
258
259 if (block->cmd[k] == LP_RAST_OP_CLEAR_COLOR ||
260 block->cmd[k] == LP_RAST_OP_CLEAR_ZSTENCIL)
261 count = debug_clear_tile(tx, ty, block->arg[k], tile, val);
262
263 if (block->cmd[k] == LP_RAST_OP_SHADE_TILE ||
264 block->cmd[k] == LP_RAST_OP_SHADE_TILE_OPAQUE)
265 count = debug_shade_tile(tx, ty, block->arg[k], tile, val);
266
267 if (block->cmd[k] == LP_RAST_OP_TRIANGLE_1 ||
268 block->cmd[k] == LP_RAST_OP_TRIANGLE_2 ||
269 block->cmd[k] == LP_RAST_OP_TRIANGLE_3 ||
270 block->cmd[k] == LP_RAST_OP_TRIANGLE_4 ||
271 block->cmd[k] == LP_RAST_OP_TRIANGLE_5 ||
272 block->cmd[k] == LP_RAST_OP_TRIANGLE_6 ||
273 block->cmd[k] == LP_RAST_OP_TRIANGLE_7)
274 count = debug_triangle(tx, ty, block->arg[k], tile, val);
275
276 if (print_cmds) {
277 debug_printf(" % 5d", count);
278
279 if (blend)
280 debug_printf(" blended");
281
282 debug_printf("\n");
283 }
284 }
285 }
286 }
287
288 void
289 lp_debug_bin( const struct cmd_bin *bin)
290 {
291 struct tile tile;
292 int x,y;
293
294 if (bin->head) {
295 do_debug_bin(&tile, bin, TRUE);
296
297 debug_printf("------------------------------------------------------------------\n");
298 for (y = 0; y < TILE_SIZE; y++) {
299 for (x = 0; x < TILE_SIZE; x++) {
300 debug_printf("%c", tile.data[y][x]);
301 }
302 debug_printf("|\n");
303 }
304 debug_printf("------------------------------------------------------------------\n");
305
306 debug_printf("each pixel drawn avg %f times\n",
307 ((float)tile.overdraw + tile.coverage)/(float)tile.coverage);
308 }
309 }
310
311
312
313
314
315
316 /** Return number of bytes used for a single bin */
317 static unsigned
318 lp_scene_bin_size( const struct lp_scene *scene, unsigned x, unsigned y )
319 {
320 struct cmd_bin *bin = lp_scene_get_bin((struct lp_scene *) scene, x, y);
321 const struct cmd_block *cmd;
322 unsigned size = 0;
323 for (cmd = bin->head; cmd; cmd = cmd->next) {
324 size += (cmd->count *
325 (sizeof(uint8_t) + sizeof(union lp_rast_cmd_arg)));
326 }
327 return size;
328 }
329
330
331
332 void
333 lp_debug_draw_bins_by_coverage( struct lp_scene *scene )
334 {
335 unsigned x, y;
336 unsigned total = 0;
337 unsigned possible = 0;
338 static uint64_t _total = 0;
339 static uint64_t _possible = 0;
340
341 for (x = 0; x < scene->tiles_x; x++)
342 debug_printf("-");
343 debug_printf("\n");
344
345 for (y = 0; y < scene->tiles_y; y++) {
346 for (x = 0; x < scene->tiles_x; x++) {
347 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
348 const char *bits = "0123456789";
349 struct tile tile;
350
351 if (bin->head) {
352 //lp_debug_bin(bin);
353
354 do_debug_bin(&tile, bin, FALSE);
355
356 total += tile.coverage;
357 possible += 64*64;
358
359 if (tile.coverage == 64*64)
360 debug_printf("*");
361 else if (tile.coverage) {
362 int bit = tile.coverage/(64.0*64.0)*10;
363 debug_printf("%c", bits[MIN2(bit,10)]);
364 }
365 else
366 debug_printf("?");
367 }
368 else {
369 debug_printf(" ");
370 }
371 }
372 debug_printf("|\n");
373 }
374
375 for (x = 0; x < scene->tiles_x; x++)
376 debug_printf("-");
377 debug_printf("\n");
378
379 debug_printf("this tile total: %u possible %u: percentage: %f\n",
380 total,
381 possible,
382 total * 100.0 / (float)possible);
383
384 _total += total;
385 _possible += possible;
386
387
388 debug_printf("overall total: %" PRIu64
389 " possible %" PRIu64 ": percentage: %f\n",
390 _total,
391 _possible,
392 (double) _total * 100.0 / (double)_possible);
393 }
394
395
396 void
397 lp_debug_draw_bins_by_cmd_length( struct lp_scene *scene )
398 {
399 unsigned x, y;
400
401 for (y = 0; y < scene->tiles_y; y++) {
402 for (x = 0; x < scene->tiles_x; x++) {
403 const char *bits = " ...,-~:;=o+xaw*#XAWWWWWWWWWWWWWWWW";
404 unsigned sz = lp_scene_bin_size(scene, x, y);
405 unsigned sz2 = util_logbase2(sz);
406 debug_printf("%c", bits[MIN2(sz2,32)]);
407 }
408 debug_printf("\n");
409 }
410 }
411
412
413 void
414 lp_debug_bins( struct lp_scene *scene )
415 {
416 unsigned x, y;
417
418 for (y = 0; y < scene->tiles_y; y++) {
419 for (x = 0; x < scene->tiles_x; x++) {
420 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
421 if (bin->head) {
422 debug_bin(bin);
423 }
424 }
425 }
426 }