-
Notifications
You must be signed in to change notification settings - Fork 3
/
SWculling.c
206 lines (172 loc) · 9.31 KB
/
SWculling.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "SWculling.h"
#include "SWalloc.h"
#include "SWintrin.inl"
SWint _swProcessTrianglesIndexed_Ref(SWcull_ctx *ctx, const void *attribs, const SWuint *indices, SWuint stride,
SWuint index_count, const SWfloat *xform, SWint is_occluder);
SWint _swCullCtxTestRect_Ref(const SWcull_ctx *ctx, const SWfloat p_min[2], const SWfloat p_max[3],
const SWfloat w_min);
void _swCullCtxClearBuf_Ref(SWcull_ctx *ctx);
void _swCullCtxDebugDepth_Ref(const SWcull_ctx *ctx, SWfloat *out_depth);
#if defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
SWint _swProcessTrianglesIndexed_NEON(SWcull_ctx *ctx, const void *attribs, const SWuint *indices, SWuint stride,
SWuint index_count, const SWfloat *xform, SWint is_occluder);
SWint _swCullCtxTestRect_NEON(const SWcull_ctx *ctx, const SWfloat p_min[2], const SWfloat p_max[3],
const SWfloat w_min);
void _swCullCtxClearBuf_NEON(SWcull_ctx *ctx);
void _swCullCtxDebugDepth_NEON(const SWcull_ctx *ctx, SWfloat *out_depth);
#else // defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
SWint _swProcessTrianglesIndexed_SSE2(SWcull_ctx *ctx, const void *attribs, const SWuint *indices, SWuint stride,
SWuint index_count, const SWfloat *xform, SWint is_occluder);
SWint _swProcessTrianglesIndexed_AVX2(SWcull_ctx *ctx, const void *attribs, const SWuint *indices, SWuint stride,
SWuint index_count, const SWfloat *xform, SWint is_occluder);
SWint _swCullCtxTestRect_SSE2(const SWcull_ctx *ctx, const SWfloat p_min[2], const SWfloat p_max[3],
const SWfloat w_min);
SWint _swCullCtxTestRect_AVX2(const SWcull_ctx *ctx, const SWfloat p_min[2], const SWfloat p_max[3],
const SWfloat w_min);
void _swCullCtxClearBuf_SSE2(SWcull_ctx *ctx);
void _swCullCtxClearBuf_AVX2(SWcull_ctx *ctx);
void _swCullCtxDebugDepth_SSE2(const SWcull_ctx *ctx, SWfloat *out_depth);
void _swCullCtxDebugDepth_AVX2(const SWcull_ctx *ctx, SWfloat *out_depth);
#if !defined(_MSC_VER) || _MSC_VER > 1916
SWint _swProcessTrianglesIndexed_AVX512(SWcull_ctx *ctx, const void *attribs, const SWuint *indices, SWuint stride,
SWuint index_count, const SWfloat *xform, SWint is_occluder);
SWint _swCullCtxTestRect_AVX512(const SWcull_ctx *ctx, const SWfloat p_min[2], const SWfloat p_max[3],
const SWfloat w_min);
void _swCullCtxClearBuf_AVX512(SWcull_ctx *ctx);
void _swCullCtxDebugDepth_AVX512(const SWcull_ctx *ctx, SWfloat *out_depth);
#endif
#endif // defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
void swCullCtxInit(SWcull_ctx *ctx, const SWint w, const SWint h, SWfloat near_clip) {
swCPUInfoInit(&ctx->cpu_info);
ctx->ztiles = NULL;
swCullCtxResize(ctx, w, h, near_clip);
swCullCtxClear(ctx);
}
void swCullCtxDestroy(SWcull_ctx *ctx) {
swCPUInfoDestroy(&ctx->cpu_info);
sw_aligned_free(ctx->ztiles);
memset(ctx, 0, sizeof(SWcull_ctx));
}
void swCullCtxResize(SWcull_ctx *ctx, const SWint w, const SWint h, SWfloat near_clip) {
if (ctx->w == w && ctx->h == h && ctx->near_clip == near_clip) {
return;
}
ctx->w = w;
ctx->h = h;
ctx->half_w = (SWfloat)w / 2;
ctx->half_h = (SWfloat)h / 2;
#if defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
ctx->tile_size_y = 4;
ctx->subtile_size_y = 4;
ctx->tri_indexed_proc = (SWCullTrianglesIndexedProcType)&_swProcessTrianglesIndexed_NEON;
ctx->test_rect_proc = &_swCullCtxTestRect_NEON;
ctx->clear_buf_proc = &_swCullCtxClearBuf_NEON;
ctx->debug_depth_proc = (SWCullDebugDepthProcType)&_swCullCtxDebugDepth_NEON;
#else
#if !defined(_MSC_VER) || _MSC_VER > 1916
if (ctx->cpu_info.avx512_supported) {
ctx->tile_size_y = 16;
ctx->subtile_size_y = 4;
ctx->tri_indexed_proc = (SWCullTrianglesIndexedProcType)&_swProcessTrianglesIndexed_AVX512;
ctx->test_rect_proc = &_swCullCtxTestRect_AVX512;
ctx->clear_buf_proc = &_swCullCtxClearBuf_AVX512;
ctx->debug_depth_proc = (SWCullDebugDepthProcType)&_swCullCtxDebugDepth_AVX512;
} else
#endif
if (ctx->cpu_info.avx2_supported) {
ctx->tile_size_y = 8;
ctx->subtile_size_y = 4;
ctx->tri_indexed_proc = (SWCullTrianglesIndexedProcType)&_swProcessTrianglesIndexed_AVX2;
ctx->test_rect_proc = &_swCullCtxTestRect_AVX2;
ctx->clear_buf_proc = &_swCullCtxClearBuf_AVX2;
ctx->debug_depth_proc = (SWCullDebugDepthProcType)&_swCullCtxDebugDepth_AVX2;
} else if (ctx->cpu_info.sse2_supported) {
ctx->tile_size_y = 4;
ctx->subtile_size_y = 4;
ctx->tri_indexed_proc = (SWCullTrianglesIndexedProcType)&_swProcessTrianglesIndexed_SSE2;
ctx->test_rect_proc = &_swCullCtxTestRect_SSE2;
ctx->clear_buf_proc = &_swCullCtxClearBuf_SSE2;
ctx->debug_depth_proc = (SWCullDebugDepthProcType)&_swCullCtxDebugDepth_SSE2;
} else
#endif
{
ctx->tile_size_y = 1;
ctx->subtile_size_y = 1;
ctx->tri_indexed_proc = (SWCullTrianglesIndexedProcType)&_swProcessTrianglesIndexed_Ref;
ctx->test_rect_proc = &_swCullCtxTestRect_Ref;
ctx->clear_buf_proc = &_swCullCtxClearBuf_Ref;
ctx->debug_depth_proc = (SWCullDebugDepthProcType)&_swCullCtxDebugDepth_Ref;
}
assert((w % SW_CULL_SUBTILE_X == 0) && (h % ctx->subtile_size_y == 0));
ctx->tile_w = (w + (SW_CULL_TILE_SIZE_X - 1)) / SW_CULL_TILE_SIZE_X;
ctx->tile_h = (h + (ctx->tile_size_y - 1)) / ctx->tile_size_y;
const int tile_size = SW_CULL_TILE_SIZE_X * ctx->tile_size_y / 8 + 2 * sizeof(float) *
(SW_CULL_TILE_SIZE_X / SW_CULL_SUBTILE_X) *
(ctx->tile_size_y / ctx->subtile_size_y);
ctx->ztiles_mem_size = ctx->tile_w * ctx->tile_h * tile_size;
sw_aligned_free(ctx->ztiles);
ctx->ztiles = sw_aligned_malloc(ctx->ztiles_mem_size, 64);
assert((uintptr_t)ctx->size_ivec4 % 16 == 0);
__m128i *size_ivec4 = (__m128i *)ctx->size_ivec4;
(*size_ivec4) = _mm128_setr_epi32(ctx->w, ctx->w, ctx->h, ctx->h);
assert((uintptr_t)ctx->half_size_vec4 % 16 == 0);
__m128 *half_size = (__m128 *)ctx->half_size_vec4;
(*half_size) = _mm128_setr_ps(ctx->half_w, ctx->half_w, ctx->half_h, ctx->half_h);
const SWfloat pad_w = ((SWfloat)2) / ctx->w;
const SWfloat pad_h = ((SWfloat)2) / ctx->h;
assert((uintptr_t)ctx->clip_planes % 16 == 0);
__m128 *clip_planes = (__m128 *)ctx->clip_planes;
clip_planes[0] = _mm128_setr_ps(1.0f - pad_w, 0.0f, 1.0f, 0.0f);
clip_planes[1] = _mm128_setr_ps(-1.0f + pad_w, 0.0f, 1.0f, 0.0f);
clip_planes[2] = _mm128_setr_ps(0.0f, -1.0f + pad_h, 1.0f, 0.0f);
clip_planes[3] = _mm128_setr_ps(0.0f, 1.0f - pad_h, 1.0f, 0.0f);
clip_planes[4] = _mm128_setr_ps(0.0f, 0.0f, 1.0f, -near_clip);
ctx->near_clip = near_clip;
}
void swCullCtxClear(SWcull_ctx *ctx) { (*ctx->clear_buf_proc)(ctx); }
void swCullCtxSubmitCullSurfs(SWcull_ctx *ctx, SWcull_surf *surfs, const SWuint count) {
for (SWuint i = 0; i < count; i++) {
SWcull_surf *s = &surfs[i];
if (s->indices) {
if (s->prim_type == SW_TRIANGLES) {
if (s->index_type == SW_UNSIGNED_INT) {
s->visible = (*ctx->tri_indexed_proc)(ctx, s->attribs, (const SWuint *)s->indices, s->stride,
s->count, s->xform, (s->type == SW_OCCLUDER));
} else {
assert(0);
}
}
} else {
}
}
}
SWint swCullCtxTestRect(SWcull_ctx *ctx, const SWfloat p_min[2], const SWfloat p_max[3], const SWfloat w_min) {
return (*ctx->test_rect_proc)(ctx, p_min, p_max, w_min);
}
SWint _swClipPolygon(const __m128 in_vtx[], const SWint in_vtx_count, const __m128 plane, __m128 out_vtx[]) {
__m128 p0 = in_vtx[in_vtx_count - 1];
__m128 dist0 = _mm128_dp4_ps(plane, p0);
SWint out_vtx_count = 0;
for (SWint i = 0; i < in_vtx_count; i++) {
const __m128 p1 = in_vtx[i];
const __m128 dist1 = _mm128_dp4_ps(plane, p1);
const int dist0_neg = _mm128_movemask_ps(dist0);
if (!dist0_neg) { // dist0 >= 0.0f
out_vtx[out_vtx_count++] = p0;
}
// if dist0 and dist1 have different signs (segment intersects plane)
if (_mm128_movemask_ps(_mm128_xor_ps(dist0, dist1))) {
if (!dist0_neg) {
const __m128 t = _mm128_div_ps(dist0, _mm128_sub_ps(dist0, dist1));
out_vtx[out_vtx_count++] = _mm128_fmadd_ps(_mm128_sub_ps(p1, p0), t, p0);
} else {
const __m128 t = _mm128_div_ps(dist1, _mm128_sub_ps(dist1, dist0));
out_vtx[out_vtx_count++] = _mm128_fmadd_ps(_mm128_sub_ps(p0, p1), t, p1);
}
}
dist0 = dist1;
p0 = p1;
}
return out_vtx_count;
}
void swCullCtxDebugDepth(SWcull_ctx *ctx, SWfloat *out_depth) { (*ctx->debug_depth_proc)(ctx, out_depth); }