Update perf counters

This commit is contained in:
Krzosa Karol
2022-06-30 10:45:57 +02:00
parent 5594165623
commit a7fa817992
4 changed files with 155 additions and 121 deletions

View File

@@ -6,4 +6,4 @@ rem assets.exe
rem tracy/TracyClient.cpp -DTRACY_ENABLE rem tracy/TracyClient.cpp -DTRACY_ENABLE
clang -O2 main.cpp -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o main.exe -Wl,user32.lib -Wl,optick\lib\x64\release\OptickCore.lib clang main.cpp -O2 -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o main.exe -Wl,user32.lib -Wl,optick\lib\x64\release\OptickCore.lib

123
main.cpp
View File

@@ -288,17 +288,18 @@ F32 edge_function(Vec4 vecp0, Vec4 vecp1, Vec4 p) {
return result; return result;
} }
// #include "optimization_log.cpp"
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,
Vec2 tex0, Vec2 tex1, Vec2 tex2, Vec2 tex0, Vec2 tex1, Vec2 tex2,
Vec3 norm0, Vec3 norm1, Vec3 norm2) { Vec3 norm0, Vec3 norm1, Vec3 norm2) {
// if(os.frame > 10) PROFILE_BEGIN(draw_triangle); PROFILE_SCOPE(draw_triangle);
// ZoneScopedN("draw_triangle");
F32 min_x1 = (F32)(min(p0.x, min(p1.x, p2.x))); 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 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_x1 = (F32)(max(p0.x, max(p1.x, p2.x)));
F32 max_y1 = (F32)(max(p0.y, max(p1.y, p2.y))); F32 max_y1 = (F32)(max(p0.y, max(p1.y, p2.y)));
S64 min_x = (S64)max(0.f, floor(min_x1)); S64 min_x = (S64)max(0.f, floor(min_x1));
S64 min_y = (S64)max(0.f, floor(min_y1)); S64 min_y = (S64)max(0.f, floor(min_y1));
S64 max_x = (S64)min((F32)dst->x, ceil(max_x1)); S64 max_x = (S64)min((F32)dst->x, ceil(max_x1));
@@ -328,7 +329,7 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
F32 Cx2 = Cy2; F32 Cx2 = Cy2;
for (S64 x = min_x; x < max_x; x++) { for (S64 x = min_x; x < max_x; x++) {
if (Cx0 >= 0 && Cx1 >= 0 && Cx2 >= 0) { if (Cx0 >= 0 && Cx1 >= 0 && Cx2 >= 0) {
// ZoneNamedN(fill, "fill_pixel", true); PROFILE_SCOPE(fill_triangle);
F32 w1 = Cx1 / area; F32 w1 = Cx1 / area;
F32 w2 = Cx2 / area; F32 w2 = Cx2 / area;
F32 w3 = Cx0 / area; F32 w3 = Cx0 / area;
@@ -408,108 +409,15 @@ void draw_triangle_nearest(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 lig
Cy1 -= dx21; Cy1 -= dx21;
Cy2 -= dx02; Cy2 -= dx02;
destination += dst->x; destination += dst->x;
}
// if(os.frame > 10) PROFILE_END(draw_triangle);
}
function
void draw_triangle_bilinear(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) {
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)clamp_bot(0.f, floor(min_x1));
S64 min_y = (S64)clamp_bot(0.f, floor(min_y1));
S64 max_x = (S64)clamp_top((F32)dst->x, ceil(max_x1));
S64 max_y = (S64)clamp_top((F32)dst->y, ceil(max_y1));
F32 area = edge_function(p0, p1, p2);
for (S64 y = min_y; y < max_y; y++) {
for (S64 x = min_x; x < max_x; x++) {
F32 edge0 = edge_function(p0, p1, { (F32)x,(F32)y });
F32 edge1 = edge_function(p1, p2, { (F32)x,(F32)y });
F32 edge2 = edge_function(p2, p0, { (F32)x,(F32)y });
if (edge0 >= 0 && edge1 >= 0 && edge2 >= 0) {
F32 w1 = edge1 / area;
F32 w2 = edge2 / area;
F32 w3 = edge0 / area;
F32 interpolated_w = (1.f / p0.w) * w1 + (1.f / p1.w) * w2 + (1.f / p2.w) * w3;
// @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
F32* depth = depth_buffer + (x + y * dst->x);
if (*depth < interpolated_w) {
*depth = interpolated_w;
F32 invw0 = (w1 / p0.w);
F32 invw1 = (w2 / p1.w);
F32 invw2 = (w3 / p2.w);
Vec3 norm = (norm0 * invw0 + norm1 * invw1 + norm2 * invw2) / interpolated_w;
F32 u = tex0.x * invw0 + tex1.x * invw1 + tex2.x * invw2;
F32 v = tex0.y * invw0 + tex1.y * invw1 + tex2.y * invw2;
{
u /= interpolated_w;
v /= interpolated_w;
u = u - floor(u);
v = v - floor(v);
u = u * (src->x - 1);
v = v * (src->y - 1);
}
S64 ui = (S64)(u);
S64 vi = (S64)(v);
F32 udiff = u - (F32)ui;
F32 vdiff = v - (F32)vi;
// Origin UV (0,0) is in bottom left
U32 *pixel = src->pixels + (ui + (src->y - 1ll - vi) * src->x);
U32 *dst_pixel = dst->pixels + (x + y * dst->x);
#if 0
Vec4 result_color = vec4abgr(*pixel);
#else
Vec4 pixelx1y1 = vec4abgr(*pixel);
Vec4 pixelx2y1 = vec4abgr(*(pixel + 1));
Vec4 pixelx1y2 = vec4abgr(*(pixel - src->x));
Vec4 pixelx2y2 = vec4abgr(*(pixel + 1 - src->x));
pixelx1y1 = srgb_to_almost_linear(pixelx1y1);
pixelx2y1 = srgb_to_almost_linear(pixelx2y1);
pixelx1y2 = srgb_to_almost_linear(pixelx1y2);
pixelx2y2 = srgb_to_almost_linear(pixelx2y2);
Vec4 blendx1 = lerp(pixelx1y1, pixelx2y1, udiff);
Vec4 blendx2 = lerp(pixelx1y2, pixelx2y2, udiff);
Vec4 result_color = lerp(blendx1, blendx2, vdiff);
#endif
Vec3 light_color = vec3(0.8,0.8,1);
constexpr F32 ambient_strength = 0.1f; {
Vec3 ambient = ambient_strength * light_color;
Vec3 diffuse = clamp_bot(0.f, -dot(norm, light_direction)) * light_color;
result_color.rgb *= (ambient+diffuse);
}
Vec4 dst_color = vec4abgr(*dst_pixel);
dst_color = srgb_to_almost_linear(dst_color);
result_color = premultiplied_alpha(dst_color, result_color);
result_color = almost_linear_to_srgb(result_color);
U32 color32 = vec4_to_u32abgr(result_color);
*dst_pixel = color32;
}
}
}
} }
} }
function function
void draw_mesh(Render *r, String scene_name, Obj_Material *materials, Obj_Mesh *mesh, Vec3 *vertices, Vec2 *tex_coords, Vec3 *normals) { 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); // ZoneNamedN(m, "draw_all_meshes", true);
PROFILE_SCOPE(draw_all_meshes);
for (int i = 0; i < mesh->indices.len; i++) { for (int i = 0; i < mesh->indices.len; i++) {
PROFILE_SCOPE(draw_set_of_mesh_indices);
// ZoneNamedN(m, "draw_single_mesh", true); // ZoneNamedN(m, "draw_single_mesh", true);
Obj_Index *index = mesh->indices.data + i; Obj_Index *index = mesh->indices.data + i;
Bitmap *image = &r->img; Bitmap *image = &r->img;
@@ -681,14 +589,19 @@ UI_SIGNAL_CALLBACK(scene_callback) {
scene = (Scene)(((int)scene + 1) % Scene_Count); scene = (Scene)(((int)scene + 1) % Scene_Count);
} }
FILE *global_file;
function void function void
windows_log(Log_Kind kind, String string, char *file, int line){ windows_log(Log_Kind kind, String string, char *file, int line){
OutputDebugStringA((char *)string.str); fprintf(global_file, "%s", string.str);
} }
int int
main(int argc, char **argv) { main(int argc, char **argv) {
global_file = fopen("perfclocks.txt", "a");
thread_ctx.log_proc = windows_log; thread_ctx.log_proc = windows_log;
fprintf(global_file, "\n---------------------");
os.window_size.x = 1280; os.window_size.x = 1280;
os.window_size.y = 720; os.window_size.y = 720;
os.window_resizable = 1; os.window_resizable = 1;
@@ -717,7 +630,7 @@ main(int argc, char **argv) {
B32 ui_mouse_lock = true; B32 ui_mouse_lock = true;
while (os_game_loop()) { while (os_game_loop()) {
// FrameMark; PROFILE_SCOPE(main_loop);
if (ui_mouse_lock == false) { if (ui_mouse_lock == false) {
r.camera_yaw.x += os.delta_mouse_pos.x * 0.01f; r.camera_yaw.x += os.delta_mouse_pos.x * 0.01f;
r.camera_yaw.y -= os.delta_mouse_pos.y * 0.01f; r.camera_yaw.y -= os.delta_mouse_pos.y * 0.01f;
@@ -763,6 +676,7 @@ main(int argc, char **argv) {
r.transform = mat4_rotation_z(rotation); r.transform = mat4_rotation_z(rotation);
r.transform = r.transform * mat4_rotation_y(rotation); r.transform = r.transform * mat4_rotation_y(rotation);
for (int i = 0; i < obj->mesh.len; i++) { for (int i = 0; i < obj->mesh.len; i++) {
PROFILE_SCOPE(draw_all_meshes);
Vec2* tex_coords = (Vec2*)obj->texture_coordinates.data; Vec2* tex_coords = (Vec2*)obj->texture_coordinates.data;
Vec3 *normals = (Vec3 *)obj->normals.data; Vec3 *normals = (Vec3 *)obj->normals.data;
Obj_Mesh *mesh = obj->mesh.data; Obj_Mesh *mesh = obj->mesh.data;
@@ -786,14 +700,17 @@ main(int argc, char **argv) {
frame_data = string_fmt(os.frame_arena, "FPS:%f dt:%f frame:%u camera_pos: %f %f %f camera_yaw: %f %f", os.fps, os.delta_time, os.frame, frame_data = string_fmt(os.frame_arena, "FPS:%f dt:%f frame:%u camera_pos: %f %f %f camera_yaw: %f %f", os.fps, os.delta_time, os.frame,
r.camera_pos.x, r.camera_pos.y, r.camera_pos.z, r.camera_yaw.x, r.camera_yaw.y); r.camera_pos.x, r.camera_pos.y, r.camera_pos.z, r.camera_yaw.x, r.camera_yaw.y);
auto *scope = &profile_scopes[ProfileScopeName_draw_triangle];
if(scope->i != 0){ for(int i = 0; i < ProfileScopeName_Count; i++){
auto *scope = &profile_scopes[i];
if(scope->i == 0) continue;
U64 total = 0; U64 total = 0;
for(int i = 0; i < scope->i; i++){ for(int i = 0; i < scope->i; i++){
total += scope->samples[i]; total += scope->samples[i];
} }
log_info("\nTotal: %llu Hits: %llu", total, (U64)scope->i); log_info("\n%s :: Total: %llu Hits: %llu, Avg: %llu", profile_scope_names[i], total, (U64)scope->i, total / scope->i);
scope->i = 0; scope->i = 0;
} }
} }

View File

@@ -1,5 +1,5 @@
function function
void draw_triangle_nearest1(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 light_direction, void draw_triangle_nearest_a(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 light_direction,
Vec4 p0, Vec4 p1, Vec4 p2, Vec4 p0, Vec4 p1, Vec4 p2,
Vec2 tex0, Vec2 tex1, Vec2 tex2, Vec2 tex0, Vec2 tex1, Vec2 tex2,
Vec3 norm0, Vec3 norm1, Vec3 norm2) { Vec3 norm0, Vec3 norm1, Vec3 norm2) {
@@ -221,4 +221,98 @@ void draw_triangle_nearest_b(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 l
} }
// if(os.frame > 10) PROFILE_END(draw_triangle); // if(os.frame > 10) PROFILE_END(draw_triangle);
} }
function
void draw_triangle_bilinear(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) {
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)clamp_bot(0.f, floor(min_x1));
S64 min_y = (S64)clamp_bot(0.f, floor(min_y1));
S64 max_x = (S64)clamp_top((F32)dst->x, ceil(max_x1));
S64 max_y = (S64)clamp_top((F32)dst->y, ceil(max_y1));
F32 area = edge_function(p0, p1, p2);
for (S64 y = min_y; y < max_y; y++) {
for (S64 x = min_x; x < max_x; x++) {
F32 edge0 = edge_function(p0, p1, { (F32)x,(F32)y });
F32 edge1 = edge_function(p1, p2, { (F32)x,(F32)y });
F32 edge2 = edge_function(p2, p0, { (F32)x,(F32)y });
if (edge0 >= 0 && edge1 >= 0 && edge2 >= 0) {
F32 w1 = edge1 / area;
F32 w2 = edge2 / area;
F32 w3 = edge0 / area;
F32 interpolated_w = (1.f / p0.w) * w1 + (1.f / p1.w) * w2 + (1.f / p2.w) * w3;
// @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
F32* depth = depth_buffer + (x + y * dst->x);
if (*depth < interpolated_w) {
*depth = interpolated_w;
F32 invw0 = (w1 / p0.w);
F32 invw1 = (w2 / p1.w);
F32 invw2 = (w3 / p2.w);
Vec3 norm = (norm0 * invw0 + norm1 * invw1 + norm2 * invw2) / interpolated_w;
F32 u = tex0.x * invw0 + tex1.x * invw1 + tex2.x * invw2;
F32 v = tex0.y * invw0 + tex1.y * invw1 + tex2.y * invw2;
{
u /= interpolated_w;
v /= interpolated_w;
u = u - floor(u);
v = v - floor(v);
u = u * (src->x - 1);
v = v * (src->y - 1);
}
S64 ui = (S64)(u);
S64 vi = (S64)(v);
F32 udiff = u - (F32)ui;
F32 vdiff = v - (F32)vi;
// Origin UV (0,0) is in bottom left
U32 *pixel = src->pixels + (ui + (src->y - 1ll - vi) * src->x);
U32 *dst_pixel = dst->pixels + (x + y * dst->x);
#if 0
Vec4 result_color = vec4abgr(*pixel);
#else
Vec4 pixelx1y1 = vec4abgr(*pixel);
Vec4 pixelx2y1 = vec4abgr(*(pixel + 1));
Vec4 pixelx1y2 = vec4abgr(*(pixel - src->x));
Vec4 pixelx2y2 = vec4abgr(*(pixel + 1 - src->x));
pixelx1y1 = srgb_to_almost_linear(pixelx1y1);
pixelx2y1 = srgb_to_almost_linear(pixelx2y1);
pixelx1y2 = srgb_to_almost_linear(pixelx1y2);
pixelx2y2 = srgb_to_almost_linear(pixelx2y2);
Vec4 blendx1 = lerp(pixelx1y1, pixelx2y1, udiff);
Vec4 blendx2 = lerp(pixelx1y2, pixelx2y2, udiff);
Vec4 result_color = lerp(blendx1, blendx2, vdiff);
#endif
Vec3 light_color = vec3(0.8,0.8,1);
constexpr F32 ambient_strength = 0.1f; {
Vec3 ambient = ambient_strength * light_color;
Vec3 diffuse = clamp_bot(0.f, -dot(norm, light_direction)) * light_color;
result_color.rgb *= (ambient+diffuse);
}
Vec4 dst_color = vec4abgr(*dst_pixel);
dst_color = srgb_to_almost_linear(dst_color);
result_color = premultiplied_alpha(dst_color, result_color);
result_color = almost_linear_to_srgb(result_color);
U32 color32 = vec4_to_u32abgr(result_color);
*dst_pixel = color32;
}
}
}
}
}

View File

@@ -1,25 +1,48 @@
enum ProfileScopeName { enum ProfileScopeName {
ProfileScopeName_draw_triangle, ProfileScopeName_draw_triangle,
ProfileScopeName_fill_triangle,
ProfileScopeName_draw_all_meshes,
ProfileScopeName_draw_mesh,
ProfileScopeName_draw_set_of_mesh_indices,
ProfileScopeName_main_loop,
ProfileScopeName_Count, ProfileScopeName_Count,
}; };
struct ProfileScope { const char *profile_scope_names[] = {
U64 samples[5096*16]; "draw_triangle",
"fill_triangle",
"draw_all_meshes",
"draw_mesh",
"draw_set_of_mesh_indices",
"main_loop",
};
struct ProfileState {
U64 samples[5096*32];
S64 i; S64 i;
}; };
global ProfileScope profile_scopes[ProfileScopeName_Count]; global ProfileState profile_scopes[ProfileScopeName_Count];
#define PROFILE_BEGIN(name) \ force_inline void
do { \ profile_begin(ProfileScopeName name){
ProfileScope *__profile_scope = profile_scopes + ProfileScopeName_##name; \ ProfileState *p = profile_scopes + name;
__profile_scope->samples[__profile_scope->i] = __rdtsc(); \ p->samples[p->i] = __rdtsc();
} while (0) }
#define PROFILE_END(name) \ force_inline void
do { \ profile_end(ProfileScopeName name){
ProfileScope *_profile_scope = profile_scopes + ProfileScopeName_##name; \ ProfileState *p = profile_scopes + name;
_profile_scope->samples[_profile_scope->i] = \ p->samples[p->i] = __rdtsc() - p->samples[p->i];
__rdtsc() - _profile_scope->samples[_profile_scope->i]; \ p->i = (p->i + 1) % buff_cap(p->samples);
_profile_scope->i = (_profile_scope->i + 1) % 5096*16; \ }
} while (0)
struct Profile_Scope{
ProfileScopeName n;
force_inline Profile_Scope(ProfileScopeName name){ profile_begin(name); n=name; }
force_inline ~Profile_Scope(){ profile_end(n); }
};
#define PROFILE_BEGIN(name) profile_begin(ProfileScopeName_##name)
#define PROFILE_END(name) profile_end(ProfileScopeName_##name)
#define PROFILE_SCOPE(name) Profile_Scope profile_scope_##__LINE__(ProfileScopeName_##name)