Inlining the abstracted simd

This commit is contained in:
Krzosa Karol
2022-07-05 20:11:11 +02:00
parent 5aff90642f
commit 5b4412f80a

144
main.cpp
View File

@@ -292,7 +292,9 @@ U64 filled_pixel_count;
U64 filled_pixel_total_time; U64 filled_pixel_total_time;
// #include "optimization_log.cpp" // #include "optimization_log.cpp"
#define I(x,i) (((F32 *)&x)[i])
typedef __m256 F32x8;
typedef __m256i S32x8;
function function
void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 light_direction, void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 light_direction,
Vec4 p0, Vec4 p1, Vec4 p2, Vec4 p0, Vec4 p1, Vec4 p2,
@@ -331,65 +333,102 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
F32 Cy1 = dy21 * min_x - dx21 * min_y - C1; F32 Cy1 = dy21 * min_x - dx21 * min_y - C1;
F32 Cy2 = dy02 * min_x - dx02 * min_y - C2; F32 Cy2 = dy02 * min_x - dx02 * min_y - C2;
Vec8 var255 = vec8(255); F32x8 var255 = _mm256_set1_ps(255);
Vec8 zero8 = vec8(0); F32x8 var0 = _mm256_set1_ps(0);
F32x8 var_max_x = _mm256_set1_ps(max_x);
F32x8 var07 = _mm256_set_ps(7,6,5,4,3,2,1,0);
// F32x8 var1 = _mm256_set1_ps(1);
Vec8 var1 = vec8(1); Vec8 var1 = vec8(1);
Vec8I var0i = vec8i(0); Vec8I var0i = vec8i(0);
Vec8I var1i = vec8i(1); Vec8I var1i = vec8i(1);
Vec8I var07i = vec8i(0,1,2,3,4,5,6,7);
Vec8 var07 = vec8(0,1,2,3,4,5,6,7);
Vec8 var1_8 = vec8(1,2,3,4,5,6,7,8); Vec8 var1_8 = vec8(1,2,3,4,5,6,7,8);
Vec8 Dy10 = vec8(dy10) * var1_8; Vec8 Dy10 = vec8(dy10) * var1_8;
Vec8 Dy21 = vec8(dy21) * var1_8; Vec8 Dy21 = vec8(dy21) * var1_8;
Vec8 Dy02 = vec8(dy02) * var1_8; Vec8 Dy02 = vec8(dy02) * var1_8;
Vec8 iw_term0 = vec8(1.f / p0.w); F32x8 var_tex0x = _mm256_set1_ps(tex0.x);
Vec8 iw_term1 = vec8(1.f / p1.w); F32x8 var_tex1x = _mm256_set1_ps(tex1.x);
Vec8 iw_term2 = vec8(1.f / p2.w); F32x8 var_tex2x = _mm256_set1_ps(tex2.x);
F32x8 var_tex0y = _mm256_set1_ps(tex0.y);
F32x8 var_tex1y = _mm256_set1_ps(tex1.y);
F32x8 var_tex2y = _mm256_set1_ps(tex2.y);
F32x8 var_p0w = _mm256_set1_ps(p0.w);
F32x8 var_p1w = _mm256_set1_ps(p1.w);
F32x8 var_p2w = _mm256_set1_ps(p2.w);
F32x8 one_over_p0w = _mm256_set1_ps(1.f / p0.w);
F32x8 one_over_p1w = _mm256_set1_ps(1.f / p1.w);
F32x8 one_over_p2w = _mm256_set1_ps(1.f / p2.w);
U32 *destination = dst->pixels + dst->x*min_y; U32 *destination = dst->pixels + dst->x*min_y;
F32 area = (p1.y - p0.y) * (p2.x - p0.x) - (p1.x - p0.x) * (p2.y - p0.y); F32 area = (p1.y - p0.y) * (p2.x - p0.x) - (p1.x - p0.x) * (p2.y - p0.y);
Vec8 area8 = vec8(area); F32x8 area8 = _mm256_set1_ps(area);
U64 fill_pixels_begin = __rdtsc(); U64 fill_pixels_begin = __rdtsc();
for (S64 y = min_y; y < max_y; y++) { for (S64 y = min_y; y < max_y; y++) {
Vec8 Cx0 = vec8(Cy0); F32x8 Cx0 = _mm256_set1_ps(Cy0);
Vec8 Cx1 = vec8(Cy1); F32x8 Cx1 = _mm256_set1_ps(Cy1);
Vec8 Cx2 = vec8(Cy2); F32x8 Cx2 = _mm256_set1_ps(Cy2);
for (S64 x8 = min_x; x8 < max_x; x8+=8) { for (S64 x8 = min_x; x8 < max_x; x8+=8) {
Cx0 = vec8(Cx0[7]) + Dy10;
Cx1 = vec8(Cx1[7]) + Dy21;
Cx2 = vec8(Cx2[7]) + Dy02;
Vec8 should_fill;
{ {
Vec8 a = (vec8(x8) + var07); F32x8 i0 = _mm256_set1_ps(I(Cx0, 7));
Vec8 b = vec8(max_x); F32x8 i1 = _mm256_add_ps(i0, Dy10.simd);
should_fill = a < b; Cx0 = {i1};
should_fill = should_fill & (Cx0 >= zero8 & Cx1 >= zero8 & Cx2 >= zero8);
F32x8 i2 = _mm256_set1_ps(I(Cx1, 7));
F32x8 i3 = _mm256_add_ps(i2, Dy21.simd);
Cx1 = {i3};
F32x8 i4 = _mm256_set1_ps(I(Cx2, 7));
F32x8 i5 = _mm256_add_ps(i4, Dy02.simd);
Cx2 = {i5};
} }
Vec8 w0 = Cx1 / area8;
Vec8 w1 = Cx2 / area8; F32x8 should_fill;
Vec8 w2 = Cx0 / area8; F32x8 i11 = _mm256_set1_ps(x8);
F32x8 i12 = _mm256_add_ps(i11, var07);
F32x8 i13 = _mm256_cmp_ps(i12, var_max_x, _CMP_LT_OQ);
F32x8 i6 = _mm256_cmp_ps(Cx0, var0, _CMP_GE_OQ);
F32x8 i7 = _mm256_cmp_ps(Cx1, var0, _CMP_GE_OQ);
F32x8 i8 = _mm256_cmp_ps(Cx2, var0, _CMP_GE_OQ);
F32x8 i9 = _mm256_and_ps(i6, i7);
F32x8 i10 = _mm256_and_ps(i9, i8);
should_fill = _mm256_and_ps(i13, i10);
F32x8 w0 = _mm256_div_ps(Cx1, area8);
F32x8 w1 = _mm256_div_ps(Cx2, area8);
F32x8 w2 = _mm256_div_ps(Cx0, area8);
// @Note: We could do: interpolated_w = 1.f / interpolated_w to get proper depth // @Note: We could do: interpolated_w = 1.f / interpolated_w to get proper depth
// but why waste an instruction, the smaller the depth value the farther the object // but why waste an instruction, the smaller the depth value the farther the object
Vec8 interpolated_w = iw_term0 * w0 + iw_term1 * w1 + iw_term2 * w2; F32x8 interpolated_w;
F32x8 i14 = _mm256_mul_ps(one_over_p0w, w0); //
F32x8 i15 = _mm256_mul_ps(one_over_p1w, w1);
F32x8 i16 = _mm256_mul_ps(one_over_p2w, w2);
F32x8 i17 = _mm256_add_ps(i14, i15);
F32x8 i18 = _mm256_add_ps(i16, i17);
interpolated_w = {i18};
F32 *depth_pointer = (depth_buffer + (x8 + y * dst->x)); F32 *depth_pointer = (depth_buffer + (x8 + y * dst->x));
Vec8 depth = loadu8(depth_pointer); F32x8 depth = _mm256_loadu_ps((float *)depth_pointer);
should_fill = should_fill & (depth < interpolated_w);
//
F32x8 i19 = _mm256_cmp_ps(depth, interpolated_w, _CMP_LT_OQ);
should_fill = _mm256_and_ps(should_fill, i19);
Vec8 invw0 = (w0 / vec8(p0.w)); F32x8 invw0 = _mm256_div_ps(w0, var_p0w);
Vec8 invw1 = (w1 / vec8(p1.w)); F32x8 invw1 = _mm256_div_ps(w1, var_p1w);
Vec8 invw2 = (w2 / vec8(p2.w)); F32x8 invw2 = _mm256_div_ps(w2, var_p2w);
Vec8 u = vec8(tex0.x) * invw0 + vec8(tex1.x) * invw1 + vec8(tex2.x) * invw2; Vec8 u = vec8(tex0.x) * Vec8{invw0} + vec8(tex1.x) * Vec8{invw1} + vec8(tex2.x) * Vec8{invw2};
Vec8 v = vec8(tex0.y) * invw0 + vec8(tex1.y) * invw1 + vec8(tex2.y) * invw2; Vec8 v = vec8(tex0.y) * Vec8{invw0} + vec8(tex1.y) * Vec8{invw1} + vec8(tex2.y) * Vec8{invw2};
u /= interpolated_w;
v /= interpolated_w; u.simd = _mm256_div_ps(u.simd, interpolated_w);
v.simd = _mm256_div_ps(v.simd, interpolated_w);
u = u - floor8(u); u = u - floor8(u);
v = v - floor8(v); v = v - floor8(v);
u = u * vec8(src->x - 1); u = u * vec8(src->x - 1);
@@ -398,7 +437,7 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
Vec8I vi = convert_vec8_to_vec8i(v); Vec8I vi = convert_vec8_to_vec8i(v);
// Origin UV (0,0) is in bottom left // Origin UV (0,0) is in bottom left
_mm256_maskstore_epi32((int *)depth_pointer, should_fill.simd, interpolated_w.simd); _mm256_maskstore_epi32((int *)depth_pointer, should_fill, interpolated_w);
Vec8I indices = ui + ((vec8i(src->y) - var1i - vi) * vec8i(src->x)); Vec8I indices = ui + ((vec8i(src->y) - var1i - vi) * vec8i(src->x));
S32 size = src->x * src->y; S32 size = src->x * src->y;
indices.simd = _mm256_min_epi32(_mm256_set1_ps(size), indices.simd); indices.simd = _mm256_min_epi32(_mm256_set1_ps(size), indices.simd);
@@ -408,14 +447,14 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
// Fetch and calculate texel values // Fetch and calculate texel values
// //
Vec8I pixel; Vec8I pixel;
if(should_fill[0]) pixel.e[0] = src->pixels[indices.e[0]]; if(I(should_fill, 0)) pixel.e[0] = src->pixels[indices.e[0]];
if(should_fill[1]) pixel.e[1] = src->pixels[indices.e[1]]; if(I(should_fill, 1)) pixel.e[1] = src->pixels[indices.e[1]];
if(should_fill[2]) pixel.e[2] = src->pixels[indices.e[2]]; if(I(should_fill, 2)) pixel.e[2] = src->pixels[indices.e[2]];
if(should_fill[3]) pixel.e[3] = src->pixels[indices.e[3]]; if(I(should_fill, 3)) pixel.e[3] = src->pixels[indices.e[3]];
if(should_fill[4]) pixel.e[4] = src->pixels[indices.e[4]]; if(I(should_fill, 4)) pixel.e[4] = src->pixels[indices.e[4]];
if(should_fill[5]) pixel.e[5] = src->pixels[indices.e[5]]; if(I(should_fill, 5)) pixel.e[5] = src->pixels[indices.e[5]];
if(should_fill[6]) pixel.e[6] = src->pixels[indices.e[6]]; if(I(should_fill, 6)) pixel.e[6] = src->pixels[indices.e[6]];
if(should_fill[7]) pixel.e[7] = src->pixels[indices.e[7]]; if(I(should_fill, 7)) pixel.e[7] = src->pixels[indices.e[7]];
Vec8I texel_i_a = pixel & vec8i(0xff000000); Vec8I texel_i_a = pixel & vec8i(0xff000000);
Vec8I texel_i_b = pixel & vec8i(0x00ff0000); Vec8I texel_i_b = pixel & vec8i(0x00ff0000);
@@ -450,7 +489,7 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
// Fetch and calculate dst pixels // Fetch and calculate dst pixels
// //
U32 *dst_memory = destination + x8; U32 *dst_memory = destination + x8;
Vec8I dst_pixel = {_mm256_maskload_epi32((const int *)dst_memory, should_fill.simd)}; Vec8I dst_pixel = {_mm256_maskload_epi32((const int *)dst_memory, should_fill)};
Vec8I dst_i_a = dst_pixel & vec8i(0xff000000); Vec8I dst_i_a = dst_pixel & vec8i(0xff000000);
Vec8I dst_i_b = dst_pixel & vec8i(0x00ff0000); Vec8I dst_i_b = dst_pixel & vec8i(0x00ff0000);
@@ -462,10 +501,15 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
dst_i_b = dst_i_b >> 16 ; dst_i_b = dst_i_b >> 16 ;
dst_i_g = dst_i_g >> 8; dst_i_g = dst_i_g >> 8;
Vec8 dst_a = convert_vec8i_to_vec8(dst_i_a) / var255; Vec8 dst_a = convert_vec8i_to_vec8(dst_i_a);
Vec8 dst_b = convert_vec8i_to_vec8(dst_i_b) / var255; Vec8 dst_b = convert_vec8i_to_vec8(dst_i_b);
Vec8 dst_g = convert_vec8i_to_vec8(dst_i_g) / var255; Vec8 dst_g = convert_vec8i_to_vec8(dst_i_g);
Vec8 dst_r = convert_vec8i_to_vec8(dst_i_r) / var255; Vec8 dst_r = convert_vec8i_to_vec8(dst_i_r);
dst_a.simd = _mm256_div_ps(dst_a.simd, var255);
dst_b.simd = _mm256_div_ps(dst_b.simd, var255);
dst_g.simd = _mm256_div_ps(dst_g.simd, var255);
dst_r.simd = _mm256_div_ps(dst_r.simd, var255);
dst_r *= dst_r; dst_r *= dst_r;
dst_g *= dst_g; dst_g *= dst_g;
@@ -488,7 +532,7 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
Vec8I result; Vec8I result;
for(S64 i = 0; i < 8; i++){ for(S64 i = 0; i < 8; i++){
if (should_fill[i]){ if (I(should_fill, i)){
U8 red = (U8)(dst_r[i] * 255); U8 red = (U8)(dst_r[i] * 255);
U8 green = (U8)(dst_g[i] * 255); U8 green = (U8)(dst_g[i] * 255);
U8 blue = (U8)(dst_b[i] * 255); U8 blue = (U8)(dst_b[i] * 255);
@@ -497,7 +541,7 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
} }
} }
_mm256_maskstore_epi32((int *)dst_memory, should_fill.simd, result.simd); _mm256_maskstore_epi32((int *)dst_memory, should_fill, result.simd);
} }
Cy0 -= dx10; Cy0 -= dx10;