-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdepth.c
More file actions
181 lines (146 loc) · 4.89 KB
/
Copy pathdepth.c
File metadata and controls
181 lines (146 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <math.h>
#include <stdlib.h>
#include "core/assert.h"
#include "core/sort.h"
#include "file/trace.h"
#include "guest/pvr/tr.h"
struct depth_entry {
/* vertex index */
int n;
/* depth buffer value */
union {
float f;
uint32_t i;
} d;
};
typedef void (*depth_cb)(float, float, float, struct depth_entry *);
struct test {
const char *name;
depth_cb depth;
sort_cmp cmp;
int match;
int total;
};
static struct tr_texture *find_texture(void *userdata, union tsp tsp,
union tcw tcw) {
/* return a non-zero handle so it doesn't try to create a texture with
the render backend (which is NULL) */
static struct tr_texture tex;
tex.handle = 1;
return &tex;
}
static int depth_cmp(const void *a, const void *b) {
const struct depth_entry *ea = (const struct depth_entry *)a;
const struct depth_entry *eb = (const struct depth_entry *)b;
return ea->d.i <= eb->d.i;
}
static int depth_cmpf(const void *a, const void *b) {
const struct depth_entry *ea = (const struct depth_entry *)a;
const struct depth_entry *eb = (const struct depth_entry *)b;
return ea->d.f <= eb->d.f;
}
static void test_context(struct trace_cmd *cmd, struct test *tests,
int num_tests) {
CHECK_EQ(cmd->type, TRACE_CMD_CONTEXT);
struct ta_context *ctx = calloc(1, sizeof(struct ta_context));
struct tr_context *rc = calloc(1, sizeof(struct tr_context));
/* parse the context */
trace_copy_context(cmd, ctx);
tr_convert_context(NULL, NULL, &find_texture, ctx, rc);
/* sort each vertex by the original w */
struct depth_entry *original =
calloc(rc->num_verts, sizeof(struct depth_entry));
float minw = FLT_MAX;
float maxw = -FLT_MAX;
for (int i = 0; i < rc->num_verts; i++) {
struct ta_vertex *vert = &rc->verts[i];
struct depth_entry *entry = &original[i];
entry->n = i;
entry->d.f = 1.0f / vert->xyz[2];
minw = MIN(minw, entry->d.f);
maxw = MAX(maxw, entry->d.f);
}
msort(original, rc->num_verts, sizeof(struct depth_entry), &depth_cmpf);
for (int i = 0; i < num_tests; i++) {
struct test *test = &tests[i];
/* calculate the depth for each vertex using the test's depth function */
struct depth_entry *tmp = calloc(rc->num_verts, sizeof(struct depth_entry));
for (int j = 0; j < rc->num_verts; j++) {
struct ta_vertex *vert = &rc->verts[j];
struct depth_entry *entry = &tmp[j];
entry->n = j;
test->depth(1.0f / vert->xyz[2], minw, maxw, entry);
}
/* sort the vertices based on the depth value */
msort(tmp, rc->num_verts, sizeof(struct depth_entry), test->cmp);
/* compare sorted results with original results */
for (int j = 0; j < rc->num_verts; j++) {
if (original[j].n == tmp[j].n) {
test->match++;
}
}
test->total += rc->num_verts;
free(tmp);
}
free(original);
free(rc);
free(ctx);
}
static void test_flt(float w, float minw, float maxw,
struct depth_entry *entry) {
entry->d.f = (w - minw) / (maxw - minw);
}
static void test_int(float w, float minw, float maxw,
struct depth_entry *entry) {
entry->d.i = ((w - minw) / (maxw - minw)) * ((1 << 24) - 1);
}
static void test_log2(float w, float minw, float maxw,
struct depth_entry *entry) {
entry->d.i = (log2(1.0f + w - minw) / log2(maxw - minw)) * ((1 << 24) - 1);
}
static void test_log2_fixed(float w, float minw, float maxw,
struct depth_entry *entry) {
entry->d.i = (log2(1.0f + w) / 17.0f) * ((1 << 24) - 1);
}
int cmd_depth(int argc, const char **argv) {
if (argc < 1) {
return 0;
}
const char *filename = argv[0];
struct trace *trace = trace_parse(filename);
struct test tests[] = {
{"32-bit float", &test_flt, &depth_cmpf, 0, 0},
{"24-bit int", &test_int, &depth_cmp, 0, 0},
{"24-bit int using log2", &test_log2, &depth_cmp, 0, 0},
{"24-bit int using log2 w/ fixed max", &test_log2_fixed, &depth_cmp, 0,
0},
};
int num_tests = ARRAY_SIZE(tests);
/* check each context in the trace */
struct trace_cmd *next = trace->cmds;
while (next) {
if (next->type == TRACE_CMD_CONTEXT) {
test_context(next, tests, num_tests);
break;
}
next = next->next;
}
trace_destroy(trace);
/* print results */
LOG_INFO("===-----------------------------------------------------===");
LOG_INFO("depth test results");
LOG_INFO("===-----------------------------------------------------===");
LOG_INFO("");
int max_name_length = 0;
for (int i = 0; i < num_tests; i++) {
struct test *test = &tests[i];
int l = (int)strlen(test->name);
max_name_length = MAX(max_name_length, l);
}
for (int i = 0; i < num_tests; i++) {
struct test *test = &tests[i];
LOG_INFO("%-*s %f%%", max_name_length, test->name,
((float)test->match / test->total) * 100.0f);
}
return 1;
}