diff --git a/build.bat b/build.bat index 469a4c8..c6d53cf 100644 --- a/build.bat +++ b/build.bat @@ -1,5 +1,5 @@ @echo off pushd %~dp0 -clang main.cpp -mfma -mavx2 -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -I".." -g -o main.exe -Wl,user32.lib +clang main.cpp -O2 -mfma -mavx2 -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -Wno-deprecated-declarations -I".." -g -o main.exe -Wl,user32.lib popd \ No newline at end of file diff --git a/main.cpp b/main.cpp index 48c9a9b..7481238 100644 --- a/main.cpp +++ b/main.cpp @@ -82,6 +82,7 @@ #include "multimedia.cpp" #include "obj.cpp" #include "vec.cpp" +#include "work_queue.cpp" #define PROFILE_SCOPE(x) struct Vertex { @@ -90,6 +91,12 @@ struct Vertex { Vec3 norm; }; +struct Render_Command{ + Bitmap *src; + Vec4 p0, p1, p2; + Vec2 tex0, tex1, tex2; +}; + struct Render { Mat4 camera; Mat4 projection; @@ -105,6 +112,14 @@ struct Render { Bitmap plot; Bitmap screen320; F32 *depth320; + + WorkQueue work_queue; + Array_List commands; +}; + +struct Render_Tile_Job_Data{ + Render *r; + Rect2 region; }; enum Scene { @@ -280,31 +295,31 @@ F32 edge_function(Vec4 vecp0, Vec4 vecp1, Vec4 p) { #define F32x8 __m256 #define S32x8 __m256i -S32 render_triangle_test_case_number = 3; +S32 render_triangle_test_case_number = 5; S32 render_triangle_test_case_angle = 1; U64 filled_pixel_count; U64 filled_pixel_cycles; U64 triangle_count; #include "optimization_log.cpp" + 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, Vec4 p0, Vec4 p1, Vec4 p2, - Vec2 tex0, Vec2 tex1, Vec2 tex2, - Vec3 norm0, Vec3 norm1, Vec3 norm2) { + Vec2 tex0, Vec2 tex1, Vec2 tex2, Rect2 rect) { if(src->pixels == 0) return; - U64 fill_pixels_begin = __rdtsc(); + // U64 fill_pixels_begin = __rdtsc(); F32 min_x1 = (F32)(min(p0.x, min(p1.x, p2.x))); F32 min_y1 = (F32)(min(p0.y, min(p1.y, p2.y))); F32 max_x1 = (F32)(max(p0.x, max(p1.x, p2.x))); F32 max_y1 = (F32)(max(p0.y, max(p1.y, p2.y))); - S64 min_x = (S64)max(0.f, floor(min_x1)); - S64 min_y = (S64)max(0.f, floor(min_y1)); - S64 max_x = (S64)min((F32)dst->x, ceil(max_x1)); - S64 max_y = (S64)min((F32)dst->y, ceil(max_y1)); + S64 min_x = (S64)max(rect.min_x, floor(min_x1)); + S64 min_y = (S64)max(rect.min_y, floor(min_y1)); + S64 max_x = (S64)min(rect.max_x, ceil(max_x1)); + S64 max_y = (S64)min(rect.max_y, ceil(max_y1)); if (min_y >= max_y) return; if (min_x >= max_x) return; @@ -565,17 +580,22 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig destination += dst->x; } - filled_pixel_cycles += __rdtsc() - fill_pixels_begin; - filled_pixel_count += (max_x - min_x)*(max_y - min_y); + // filled_pixel_cycles += __rdtsc() - fill_pixels_begin; + // filled_pixel_count += (max_x - min_x)*(max_y - min_y); +} + +WORK_QUEUE_CALLBACK(draw_tile){ + auto d = (Render_Tile_Job_Data *)data; + Render *r = d->r; + For_It(r->commands){ + draw_triangle_nearest(&r->screen320, r->depth320, it.item->src, it.item->p0, it.item->p1, it.item->p2, it.item->tex0, it.item->tex1, it.item->tex2, d->region); + } } function void draw_mesh(Render *r, String scene_name, Obj_Material *materials, Obj_Mesh *mesh, Vec3 *vertices, Vec2 *tex_coords, Vec3 *normals) { - // ZoneNamedN(m, "draw_all_meshes", true); - PROFILE_SCOPE(draw_all_meshes); + for (int i = 0; i < mesh->indices.len; i++) { - PROFILE_SCOPE(draw_set_of_mesh_indices); - // ZoneNamedN(m, "draw_single_mesh", true); Obj_Index *index = mesh->indices.data + i; Bitmap *image = &r->img; if(index->material_id != -1) { @@ -615,7 +635,7 @@ void draw_mesh(Render *r, String scene_name, Obj_Material *materials, Obj_Mesh * Vec3 p0_to_p1 = vert[1].pos - vert[0].pos; Vec3 p0_to_p2 = vert[2].pos - vert[0].pos; Vec3 normal = normalize(cross(p0_to_p1, p0_to_p2)); - Vec3 light_direction = mat4_rotation_x(light_rotation) * vec3(0, 0, 1); + // Vec3 light_direction = mat4_rotation_x(light_rotation) * vec3(0, 0, 1); if (dot(normal, p0_to_camera) > 0) { //@Note: Backface culling /// ## Clipping @@ -711,6 +731,26 @@ void draw_mesh(Render *r, String scene_name, Obj_Material *materials, Obj_Mesh * triangle_count++; if (in_count > 3) triangle_count++; + Render_Command *command = array_alloc(os.perm_arena, &r->commands); + command->src = image; + command->p0 = in[0].pos; + command->p1 = in[1].pos; + command->p2 = in[2].pos; + command->tex0 = in[0].tex; + command->tex1 = in[1].tex; + command->tex2 = in[2].tex; + if(in_count > 3){ + Render_Command *command = array_alloc(os.perm_arena, &r->commands); + command->src = image; + command->p0 = in[0].pos; + command->p1 = in[2].pos; + command->p2 = in[3].pos; + command->tex0 = in[0].tex; + command->tex1 = in[2].tex; + command->tex2 = in[3].tex; + } + +#if 0 switch(render_triangle_test_case_number){ case 0: break; case 1: @@ -729,10 +769,11 @@ void draw_mesh(Render *r, String scene_name, Obj_Material *materials, Obj_Mesh * if (in_count > 3) draw_triangle_nearest_simd_without_overloads(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[2].pos, in[3].pos, in[0].tex, in[2].tex, in[3].tex, in[0].norm, in[2].norm, in[3].norm); break; case 5: - draw_triangle_nearest(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[1].pos, in[2].pos, in[0].tex, in[1].tex, in[2].tex, in[0].norm, in[1].norm, in[2].norm); - if (in_count > 3) draw_triangle_nearest(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[2].pos, in[3].pos, in[0].tex, in[2].tex, in[3].tex, in[0].norm, in[2].norm, in[3].norm); + draw_triangle_nearest_final(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[1].pos, in[2].pos, in[0].tex, in[1].tex, in[2].tex, in[0].norm, in[1].norm, in[2].norm); + if (in_count > 3) draw_triangle_nearest_final(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[2].pos, in[3].pos, in[0].tex, in[2].tex, in[3].tex, in[0].norm, in[2].norm, in[3].norm); break; } +#endif } } } @@ -779,8 +820,8 @@ main(int argc, char **argv) { thread_ctx.log_proc = windows_log; fprintf(global_file, "\n---------------------"); - os.window_size.x = 1280; - os.window_size.y = 720; + os.window_size.x = 1920; + os.window_size.y = 1080; os.window_resizable = 1; assert(os_init()); Font font = os_load_font(os.perm_arena, 12*os.dpi_scale, "Arial", 0); @@ -792,13 +833,15 @@ main(int argc, char **argv) { // sponza = &sponza_obj; scene_callback(); - int screen_x = 1280; - int screen_y = 720; + int screen_x = os.window_size.x; + int screen_y = os.window_size.y; r.camera_pos = vec3(-228,94.5,-107); r.camera_yaw = vec2(-1.25, 0.21); r.screen320 = {(U32 *)arena_push_size(os.perm_arena, screen_x*screen_y*sizeof(U32)), screen_x, screen_y}; r.depth320 = (F32 *)arena_push_size(os.perm_arena, sizeof(F32) * screen_x * screen_y); + ThreadStartupInfo thread_infos[16] = {}; + init_work_queue(&r.work_queue, buff_cap(thread_infos), thread_infos); String frame_data = {}; String raster_details = {}; @@ -869,6 +912,31 @@ main(int argc, char **argv) { } + Render_Tile_Job_Data tile_job_data[16]; + S32 x_tiles = 1; + S32 y_tiles = 16; + F32 block_size_x = r.screen320.x / x_tiles; + F32 block_size_y = r.screen320.y / y_tiles; + S32 i = 0; + for(S32 x = 0; x < x_tiles; x++){ + for(S32 y = 0; y < y_tiles; y++){ + Rect2 bounding_rect; + bounding_rect.min_x = block_size_x * x; + bounding_rect.min_y = block_size_y * y; + bounding_rect.max_x = bounding_rect.min_x + block_size_x; + bounding_rect.max_y = bounding_rect.min_y + block_size_y; + tile_job_data[i].region = bounding_rect; + tile_job_data[i].r = &r; + + push_work(&r.work_queue, (void *)(tile_job_data + i), draw_tile); + i += 1; + } + } + + wait_until_completion(&r.work_queue); + array_free_all_nodes(&r.commands); + + // @Note: Draw 320screen to OS screen U32* ptr = os.screen->pixels; for (int y = 0; y < os.screen->y; y++) { @@ -893,14 +961,18 @@ main(int argc, char **argv) { triangle_count = 0; } - if(os.frame % 4 == 0){ + // @Todo I think there is bug with test_case_number, after doing full round it + // skips a phase + if(os.frame % 60 == 0){ + continue; render_triangle_test_case_number++; if(render_triangle_test_case_number == 6){ render_triangle_test_case_number = 0; try_again: switch(render_triangle_test_case_angle){ case 0: r.camera_pos = vec3(-228,94.5,-107); r.camera_yaw = vec2(-1.25, 0.21); break; case 1: r.camera_pos = vec3(-356,89.5,168); r.camera_yaw = vec2(0.2, 0); break; - case 2: render_triangle_test_case_angle = 0; goto try_again; break; + case 2: r.camera_pos = vec3(-1020, 687, -85); r.camera_yaw = vec2(-1.3, -0.44); break; + case 3: render_triangle_test_case_angle = 0; goto try_again; break; } render_triangle_test_case_angle += 1; } diff --git a/optimization_log.cpp b/optimization_log.cpp index a045c62..8590b1c 100644 --- a/optimization_log.cpp +++ b/optimization_log.cpp @@ -849,3 +849,291 @@ void draw_triangle_nearest_simd_without_overloads(Bitmap* dst, F32 *depth_buffer filled_pixel_cycles += __rdtsc() - fill_pixels_begin; filled_pixel_count += (max_x - min_x)*(max_y - min_y); } + + +function +void draw_triangle_nearest_final(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 light_direction, + Vec4 p0, Vec4 p1, Vec4 p2, + Vec2 tex0, Vec2 tex1, Vec2 tex2, + Vec3 norm0, Vec3 norm1, Vec3 norm2) { + if(src->pixels == 0) return; + + U64 fill_pixels_begin = __rdtsc(); + + F32 region_min_x = 0; + F32 region_min_y = 0; + F32 region_max_x = dst->x; + F32 region_max_y = dst->y; + + F32 min_x1 = (F32)(min(p0.x, min(p1.x, p2.x))); + F32 min_y1 = (F32)(min(p0.y, min(p1.y, p2.y))); + F32 max_x1 = (F32)(max(p0.x, max(p1.x, p2.x))); + F32 max_y1 = (F32)(max(p0.y, max(p1.y, p2.y))); + + S64 min_x = (S64)max(region_min_x, floor(min_x1)); + S64 min_y = (S64)max(region_min_y, floor(min_y1)); + S64 max_x = (S64)min(region_max_x, ceil(max_x1)); + S64 max_y = (S64)min(region_max_y, ceil(max_y1)); + + if (min_y >= max_y) return; + if (min_x >= max_x) return; + + F32 dy10 = (p1.y - p0.y); + F32 dy21 = (p2.y - p1.y); + F32 dy02 = (p0.y - p2.y); + + F32 dx10 = (p1.x - p0.x); + F32 dx21 = (p2.x - p1.x); + F32 dx02 = (p0.x - p2.x); + + F32x8 var255 = _mm256_set1_ps(255); + F32x8 var0 = _mm256_set1_ps(0); + F32x8 var1 = _mm256_set1_ps(1); + F32x8 var_max_x = _mm256_set1_ps(max_x); + F32x8 var07 = _mm256_set_ps(7,6,5,4,3,2,1,0); + F32x8 inv255 = _mm256_div_ps(var1, var255); + + F32x8 var_src_x_minus_one = _mm256_set1_ps(src->x-1); + F32x8 var_src_y_minus_one = _mm256_set1_ps(src->y-1); + S32x8 var_src_y_minus_one_int = _mm256_set1_epi32(src->y-1); + S32x8 var_src_x_int = _mm256_set1_epi32(src->x); + + S32x8 var_0xff000000 = _mm256_set1_epi32(0xff000000); + S32x8 var_0x00ff0000 = _mm256_set1_epi32(0x00ff0000); + S32x8 var_0x0000ff00 = _mm256_set1_epi32(0x0000ff00); + S32x8 var_0x000000ff = _mm256_set1_epi32(0x000000ff); + + F32x8 var_tex0x = _mm256_set1_ps(tex0.x); + F32x8 var_tex1x = _mm256_set1_ps(tex1.x); + 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 inv_p0w = _mm256_div_ps(var1, _mm256_set1_ps(p0.w)); + F32x8 inv_p1w = _mm256_div_ps(var1, _mm256_set1_ps(p1.w)); + F32x8 inv_p2w = _mm256_div_ps(var1, _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; + F32 area = (p1.y - p0.y) * (p2.x - p0.x) - (p1.x - p0.x) * (p2.y - p0.y); + F32x8 inv_area8 = _mm256_div_ps(var1, _mm256_set1_ps(area)); + + F32x8 _dy10 = _mm256_set1_ps(dy10); + F32x8 _dx10 = _mm256_set1_ps(dx10); + F32x8 _dy21 = _mm256_set1_ps(dy21); + F32x8 _dx21 = _mm256_set1_ps(dx21); + F32x8 _dy02 = _mm256_set1_ps(dy02); + F32x8 _dx02 = _mm256_set1_ps(dx02); + F32x8 p0_x = _mm256_set1_ps(p0.x); + F32x8 p0_y = _mm256_set1_ps(p0.y); + F32x8 p1_x = _mm256_set1_ps(p1.x); + F32x8 p1_y = _mm256_set1_ps(p1.y); + F32x8 p2_x = _mm256_set1_ps(p2.x); + F32x8 p2_y = _mm256_set1_ps(p2.y); + + for (S64 y = min_y; y < max_y; y++) { + F32x8 Y = _mm256_set1_ps(y); + for (S64 x8 = min_x; x8 < max_x; x8+=8) { + F32x8 X = _mm256_add_ps(_mm256_set1_ps(x8), var07); + + // Compute the edges + // F32x8 edge0 = (p1.y - p0.y) * (p.x - p0.x) - (p1.x - p0.x) * (p.y - p0.y); + F32x8 px_minus_0x = _mm256_sub_ps(X, p0_x); + F32x8 py_minus_0y = _mm256_sub_ps(Y, p0_y); + F32x8 right0 = _mm256_mul_ps(_dx10, py_minus_0y); + F32x8 edge0 = _mm256_fmsub_ps(_dy10, px_minus_0x, right0); + + // F32 result = (p2.y - p1.y) * (p.x - p1.x) - (p2.x - p1.x) * (p.y - p1.y); + F32x8 px_minus_1x = _mm256_sub_ps(X, p1_x); + F32x8 py_minus_1y = _mm256_sub_ps(Y, p1_y); + F32x8 right1 = _mm256_mul_ps(_dx21, py_minus_1y); + F32x8 edge1 = _mm256_fmsub_ps(_dy21, px_minus_1x, right1); + + // F32 result = (p0.y - p2.y) * (p.x - p2.x) - (p0.x - p2.x) * (p.y - p2.y); + F32x8 px_minus_2x = _mm256_sub_ps(X, p2_x); + F32x8 py_minus_2y = _mm256_sub_ps(Y, p2_y); + F32x8 right2 = _mm256_mul_ps(_dx02, py_minus_2y); + F32x8 edge2 = _mm256_fmsub_ps(_dy02, px_minus_2x, right2); + + F32x8 should_fill; + F32x8 test_if_x_should_be_clipped = _mm256_cmp_ps(X, var_max_x, _CMP_LT_OQ); + F32x8 test_if_pixel_inside_edge_using_dot_result0 = _mm256_cmp_ps(edge0, var0, _CMP_GE_OQ); + F32x8 test_if_pixel_inside_edge_using_dot_result1 = _mm256_cmp_ps(edge1, var0, _CMP_GE_OQ); + F32x8 test_if_pixel_inside_edge_using_dot_result2 = _mm256_cmp_ps(edge2, var0, _CMP_GE_OQ); + F32x8 dot_result_combination0 = _mm256_and_ps(test_if_pixel_inside_edge_using_dot_result0, test_if_pixel_inside_edge_using_dot_result1); + F32x8 dot_result_combination1 = _mm256_and_ps(dot_result_combination0, test_if_pixel_inside_edge_using_dot_result2); + should_fill = _mm256_and_ps(test_if_x_should_be_clipped, dot_result_combination1); + + F32x8 w0 = _mm256_mul_ps(edge1, inv_area8); + F32x8 w1 = _mm256_mul_ps(edge2, inv_area8); + F32x8 w2 = _mm256_mul_ps(edge0, inv_area8); + + // @Todo: Turn this into 1 / interpolated_w, turns out in theory it should be + // more performant but couldn't make it work + + // @Old_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 + F32x8 interpolated_w = _mm256_mul_ps(one_over_p0w, w0); + interpolated_w = _mm256_fmadd_ps(one_over_p1w, w1, interpolated_w); + interpolated_w = _mm256_fmadd_ps(one_over_p2w, w2, interpolated_w); + + F32 *depth_pointer = (depth_buffer + (x8 + y * dst->x)); + F32x8 depth = _mm256_loadu_ps(depth_pointer); + + F32x8 should_fill_term = _mm256_cmp_ps(depth, interpolated_w, _CMP_LT_OQ); + should_fill = _mm256_and_ps(should_fill, should_fill_term); + + // If all pixels are not going to get drawn then opt out + F32x8 compare_with_zero = _mm256_cmpeq_epi32(should_fill, var0); + int mask = _mm256_movemask_epi8(compare_with_zero); + if(mask == 0xffffffff) { + continue; + } + + F32x8 invw0 = _mm256_mul_ps(w0, inv_p0w); + F32x8 invw1 = _mm256_mul_ps(w1, inv_p1w); + F32x8 invw2 = _mm256_mul_ps(w2, inv_p2w); + + F32x8 u0 = _mm256_mul_ps(var_tex0x, invw0); + u0 = _mm256_fmadd_ps(var_tex1x, invw1, u0); + u0 = _mm256_fmadd_ps(var_tex2x, invw2, u0); + + F32x8 v0 = _mm256_mul_ps(var_tex0y, invw0); + v0 = _mm256_fmadd_ps(var_tex1y, invw1, v0); + v0 = _mm256_fmadd_ps(var_tex2y, invw2, v0); + + F32x8 u1 = _mm256_div_ps(u0, interpolated_w); + F32x8 v1 = _mm256_div_ps(v0, interpolated_w); + + F32x8 u_floored = _mm256_floor_ps(u1); + F32x8 v_floored = _mm256_floor_ps(v1); + F32x8 u2 = _mm256_sub_ps(u1, u_floored); + F32x8 v2 = _mm256_sub_ps(v1, v_floored); + F32x8 u3 = _mm256_mul_ps(u2, var_src_x_minus_one); + F32x8 v3 = _mm256_mul_ps(v2, var_src_y_minus_one); + + F32x8 ui = _mm256_cvtps_epi32(u3); + F32x8 vi = _mm256_cvtps_epi32(v3); + + // Origin UV (0,0) is in bottom left + _mm256_maskstore_epi32((int *)depth_pointer, should_fill, interpolated_w); + + // + // Fetch and calculate texel values + // + S32x8 indices_to_fetch0 = _mm256_sub_epi32(var_src_y_minus_one_int, vi); + S32x8 indices_to_fetch1 = _mm256_mullo_epi32(var_src_x_int, indices_to_fetch0); + S32x8 indices_to_fetch2 = _mm256_add_epi32(indices_to_fetch1, ui); + S32x8 indices_to_fetch3 = _mm256_and_si256(indices_to_fetch2, should_fill); + + S32x8 pixel = _mm256_set_epi32( + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 7)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 6)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 5)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 4)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 3)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 2)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 1)], + src->pixels[_mm256_extract_epi32(indices_to_fetch3, 0)] + ); + + S32x8 texel_i_a = _mm256_and_si256(pixel, var_0xff000000); + S32x8 texel_i_b = _mm256_and_si256(pixel, var_0x00ff0000); + S32x8 texel_i_g = _mm256_and_si256(pixel, var_0x0000ff00); + S32x8 texel_i_r = _mm256_and_si256(pixel, var_0x000000ff); + + texel_i_a = _mm256_srli_epi32(texel_i_a, 24); + texel_i_b = _mm256_srli_epi32(texel_i_b, 16); + texel_i_g = _mm256_srli_epi32(texel_i_g, 8 ); + + F32x8 texel_a0 = _mm256_cvtepi32_ps(texel_i_a); + F32x8 texel_b0 = _mm256_cvtepi32_ps(texel_i_b); + F32x8 texel_g0 = _mm256_cvtepi32_ps(texel_i_g); + F32x8 texel_r0 = _mm256_cvtepi32_ps(texel_i_r); + + F32x8 texel_b1 = _mm256_mul_ps(texel_b0, inv255); + F32x8 texel_g1 = _mm256_mul_ps(texel_g0, inv255); + F32x8 texel_r1 = _mm256_mul_ps(texel_r0, inv255); + F32x8 texel_a1 = _mm256_mul_ps(texel_a0, inv255); + + texel_r1 = _mm256_mul_ps(texel_r1, texel_r1); + texel_g1 = _mm256_mul_ps(texel_g1, texel_g1); + texel_b1 = _mm256_mul_ps(texel_b1, texel_b1); + + // + // Fetch and calculate dst pixels + // + U32 *dst_memory = destination + x8; + S32x8 dst_pixel = _mm256_maskload_epi32((const int *)dst_memory, should_fill); + + S32x8 dst_i_a0 = _mm256_and_si256(dst_pixel, var_0xff000000); + S32x8 dst_i_b0 = _mm256_and_si256(dst_pixel, var_0x00ff0000); + S32x8 dst_i_g0 = _mm256_and_si256(dst_pixel, var_0x0000ff00); + S32x8 dst_i_r0 = _mm256_and_si256(dst_pixel, var_0x000000ff); + + S32x8 dst_i_a1 = _mm256_srli_epi32(dst_i_a0, 24); + S32x8 dst_i_b1 = _mm256_srli_epi32(dst_i_b0, 16); + S32x8 dst_i_g1 = _mm256_srli_epi32(dst_i_g0, 8); + S32x8 dst_i_r1 = dst_i_r0; + + F32x8 dst_a = _mm256_cvtepi32_ps(dst_i_a1); + F32x8 dst_b = _mm256_cvtepi32_ps(dst_i_b1); + F32x8 dst_g = _mm256_cvtepi32_ps(dst_i_g1); + F32x8 dst_r = _mm256_cvtepi32_ps(dst_i_r1); + + dst_a = _mm256_mul_ps(dst_a, inv255); + dst_b = _mm256_mul_ps(dst_b, inv255); + dst_g = _mm256_mul_ps(dst_g, inv255); + dst_r = _mm256_mul_ps(dst_r, inv255); + + dst_r = _mm256_mul_ps(dst_r, dst_r); + dst_g = _mm256_mul_ps(dst_g, dst_g); + dst_b = _mm256_mul_ps(dst_b, dst_b); + + // Premultiplied alpha + { + F32x8 inv_texel_a = _mm256_sub_ps(var1,texel_a1); + dst_r = _mm256_fmadd_ps(inv_texel_a, dst_r, texel_r1); + dst_g = _mm256_fmadd_ps(inv_texel_a, dst_g, texel_g1); + dst_b = _mm256_fmadd_ps(inv_texel_a, dst_b, texel_b1); + dst_a = _mm256_sub_ps(_mm256_add_ps(texel_a1, dst_a), _mm256_mul_ps(texel_a1,dst_a)); + } + + // Almost linear to srgb + { + dst_r = _mm256_sqrt_ps(dst_r); + dst_g = _mm256_sqrt_ps(dst_g); + dst_b = _mm256_sqrt_ps(dst_b); + } + + // Convert to integer format + dst_r = _mm256_mul_ps(dst_r, var255); + dst_g = _mm256_mul_ps(dst_g, var255); + dst_b = _mm256_mul_ps(dst_b, var255); + dst_a = _mm256_mul_ps(dst_a, var255); + + S32x8 dst_r_int = _mm256_cvtps_epi32(dst_r); + S32x8 dst_g_int = _mm256_cvtps_epi32(dst_g); + S32x8 dst_b_int = _mm256_cvtps_epi32(dst_b); + S32x8 dst_a_int = _mm256_cvtps_epi32(dst_a); + + S32x8 dst_int_a_shifted = _mm256_slli_epi32(dst_a_int, 24); + S32x8 dst_int_b_shifted = _mm256_slli_epi32(dst_b_int, 16); + S32x8 dst_int_g_shifted = _mm256_slli_epi32(dst_g_int, 8); + S32x8 dst_int_r_shifted = dst_r_int; + + S32x8 packed_abgr0 = _mm256_or_si256(dst_int_a_shifted, dst_int_b_shifted); + S32x8 packed_abgr1 = _mm256_or_si256(dst_int_r_shifted, dst_int_g_shifted); + S32x8 packed_abgr2 = _mm256_or_si256(packed_abgr1, packed_abgr0); + + _mm256_maskstore_epi32((int *)dst_memory, should_fill, packed_abgr2); + } + destination += dst->x; + } + + filled_pixel_cycles += __rdtsc() - fill_pixels_begin; + filled_pixel_count += (max_x - min_x)*(max_y - min_y); +} diff --git a/work_queue.cpp b/work_queue.cpp new file mode 100644 index 0000000..7caa49f --- /dev/null +++ b/work_queue.cpp @@ -0,0 +1,100 @@ + +// @Section: Work Queue +#define WORK_QUEUE_CALLBACK(name) void name(void *data) +typedef WORK_QUEUE_CALLBACK(WorkQueueCallback); + +struct WorkQueueEntry { + WorkQueueCallback *callback; + void *data; +}; + +struct WorkQueue { + WorkQueueEntry entries[256]; + S64 volatile index_to_write; + S64 volatile index_to_read; + S64 volatile completion_index; + S64 volatile completion_goal; + HANDLE semaphore; +}; + +struct ThreadStartupInfo { + DWORD thread_id; + S32 thread_index; + WorkQueue *queue; +}; + +S64 atomic_increment(volatile S64 *i){ + return InterlockedIncrement64(i); +} + +S64 atomic_compare_and_swap(volatile S64 *dst, S64 exchange, S64 comperand){ + return InterlockedCompareExchange64(dst, exchange, comperand); +} + +void push_work(WorkQueue *wq, void *data, WorkQueueCallback *callback) { + U32 new_index = (wq->index_to_write + 1) % buff_cap(wq->entries); + assert(new_index != wq->index_to_read); + + WorkQueueEntry *entry = wq->entries + wq->index_to_write; + entry->data = data; + entry->callback = callback; + + wq->completion_goal+=1; + _WriteBarrier(); + wq->index_to_write = new_index; + ReleaseSemaphore(wq->semaphore, 1, 0); +} + +bool try_doing_work(WorkQueue *wq) { + bool should_sleep = false; + S64 original_index_to_read = wq->index_to_read; + S64 new_index_to_read = (original_index_to_read + 1) % buff_cap(wq->entries); + if(original_index_to_read != wq->index_to_write) { + S64 index = atomic_compare_and_swap(&wq->index_to_read, new_index_to_read, original_index_to_read); + if(index == original_index_to_read) { + WorkQueueEntry *entry = wq->entries + index; + entry->callback(entry->data); + atomic_increment(&wq->completion_index); + } + } + else { + should_sleep = true; + } + return should_sleep; +} + +DWORD WINAPI thread_proc(LPVOID param) { + auto ti = (ThreadStartupInfo *)param; + + Thread_Ctx ctx = {}; + ctx.thread_index = ti->thread_index; + for(;;) { + if(try_doing_work(ti->queue)) { + WaitForSingleObject(ti->queue->semaphore, INFINITE); + } + } +} + +void init_work_queue(WorkQueue *queue, U32 thread_count, ThreadStartupInfo *info) { + queue->index_to_read = 0; + queue->index_to_write = 0; + queue->completion_index = 0; + queue->completion_goal = 0; + queue->semaphore = CreateSemaphoreExA(0, 0, thread_count, 0, 0, SEMAPHORE_ALL_ACCESS); + assert_msg(queue->semaphore != INVALID_HANDLE_VALUE, "Failed to create semaphore"); + + for(U32 i = 0; i < thread_count; i++) { + ThreadStartupInfo *ti = info + i; + ti->thread_index = i; + ti->queue = queue; + HANDLE thread_handle = CreateThread(0, 0, thread_proc, ti, 0, &ti->thread_id); + assert_msg(thread_handle != INVALID_HANDLE_VALUE, "Failed to create thread"); + CloseHandle(thread_handle); + } +} + +void wait_until_completion(WorkQueue *wq) { + while(wq->completion_goal != wq->completion_index) { + try_doing_work(wq); + } +}