Skip to content

Commit

Permalink
Eliminated one frame memory copy and one clear frame operation in ren…
Browse files Browse the repository at this point in the history
…dering chain, 20-70% higher framerate
  • Loading branch information
ProjectPhysX committed Apr 9, 2024
1 parent 5a406b3 commit 1e5b86a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ The fastest and most memory efficient lattice Boltzmann CFD software, running on
- fixed compiler warnings on Android
- fixed `make.sh` failing on some systems due to nonstandard interpreter path
- fixed that `make` would not compile with multiple cores on some systems
- [v2.15](https://github.com/ProjectPhysX/FluidX3D/releases/tag/v2.15) (09.04.2024) [changes](https://github.com/ProjectPhysX/FluidX3D/compare/v2.14...v2.15) (framerate boost)
- eliminated one frame memory copy and one clear frame operation in rendering chain, for 20-70% higher framerate on both Windows and Linux
- enabled `g++` compiler optimizations for faster startup and higher rendering framerate
- fixed bug in multithreaded sanity checks
- fixed wrong unit conversion for thermal expansion coefficient
- fixed density to pressure conversion in LBM units
- fixed bug that raytracing kernel could lock up simulation
- fixed minor visual artifacts with raytracing
- fixed that console sometimes was not cleared before `INTERACTIVE_GRAPHICS_ASCII` rendering starts

</details>

Expand Down
11 changes: 6 additions & 5 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ void draw_line_label(const int x0, const int y0, const int x1, const int y1, con
}
}
}
void draw_bitmap(const int* bitmap) {
std::copy(bitmap, bitmap+(int)camera.width*(int)camera.height, camera.bitmap);
void draw_bitmap(int* bitmap) {
std::swap(camera.bitmap, bitmap); // swap pointers instead of memory copy
}

void draw_pixel(const float3& p, const int color) {
Expand Down Expand Up @@ -471,7 +471,7 @@ void update_frame(const double frametime) {
main_label(frametime);
SetBitmapBits(frameDC, 4*(int)camera.width*(int)camera.height, camera.bitmap);
BitBlt(displayDC, 0, 0, (int)camera.width, (int)camera.height, memDC, 0, 0, SRCCOPY); // copy back buffer to front buffer
camera.clear_frame(); // clear frame
//camera.clear_frame(); // clear frame
}
LRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam) {
if(message==WM_DESTROY) {
Expand Down Expand Up @@ -618,10 +618,11 @@ int key_linux(const int keycode) {
}
void update_frame(const double frametime) {
main_label(frametime);
x11_image->data = (char*)camera.bitmap; // camera.bitmap pointer might have been changed, so update x11_image->data pointer here
updating_frame = true;
XPutImage(x11_display, x11_window, x11_gc, x11_image, 0, 0, 0, 0, camera.width, camera.height);
updating_frame = false;
camera.clear_frame(); // clear frame
//camera.clear_frame(); // clear frame
if(!camera.lockmouse) XWarpPointer(x11_display, x11_window, x11_window, 0, 0, camera.width, camera.height, camera.width/2, camera.height/2); // center cursor
}
void hide_cursor() {
Expand Down Expand Up @@ -766,7 +767,7 @@ void update_frame(const double frametime) {
print_video_dither(&image, textwidth, textheight);
print(alignr(textwidth, to_string(textwidth)+"x"+to_string(textheight)+" "+alignr(4, to_int(1.0/frametime))+"fps"));
show_console_cursor(true);
camera.clear_frame(); // clear frame
//camera.clear_frame(); // clear frame
}
void input_detection() {
while(running) {
Expand Down
8 changes: 3 additions & 5 deletions src/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ class Camera {
if(!lockmouse) update_rotation(mouse_x, mouse_y);
}
void clear_frame() {
for(uint i=0u; i<width*height; i++) {
bitmap[i] = GRAPHICS_BACKGROUND_COLOR;
zbuffer[i] = min_int;
}
std::fill(bitmap, bitmap+width*height, GRAPHICS_BACKGROUND_COLOR); // faster than "for(uint i=0u; i<width*height; i++) bitmap[i] = GRAPHICS_BACKGROUND_COLOR;"
std::fill(zbuffer, zbuffer+width*height, min_int); // faster than "for(uint i=0u; i<width*height; i++) zbuffer[i] = min_int;"
}
float data(const uint i) const { // returns all camera data required for rendering
switch(i) {
Expand Down Expand Up @@ -332,7 +330,7 @@ extern bool key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_0

void set_light(const uint i, const float3& p);

void draw_bitmap(const int* bitmap);
void draw_bitmap(int* bitmap);
void draw_label(const int x, const int y, const string& s, const int color);
void draw_line_label(const int x0, const int y0, const int x1, const int y1, const int color);

Expand Down
3 changes: 2 additions & 1 deletion src/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void Info::print_logo() const {
print("| "); print("\\ \\ / /", c); print(" |\n");
print("| "); print("\\ ' /", c); print(" |\n");
print("| "); print("\\ /", c); print(" |\n");
print("| "); print("\\ /", c); print(" FluidX3D Version 2.14 |\n");
print("| "); print("\\ /", c); print(" FluidX3D Version 2.15 |\n");
print("| "); print( "'", c); print(" Copyright (c) Dr. Moritz Lehmann |\n");
print("|-----------------------------------------------------------------------------|\n");
}
Expand Down Expand Up @@ -86,6 +86,7 @@ void Info::print_initialize() {
println("| MLUPs | Bandwidth | Steps/s | Current Step | "+string(steps==max_ulong?"Elapsed Time ":"Time Remaining")+" |");
#else // INTERACTIVE_GRAPHICS_ASCII
println("'-----------------'-----------------------------------------------------------'");
clear_console();
#endif // INTERACTIVE_GRAPHICS_ASCII
clock.start();
allow_rendering = true;
Expand Down
1 change: 1 addition & 0 deletions src/info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class LBM;
struct Info { // contains redundant information for console printing
LBM* lbm = nullptr;
bool allow_rendering = false; // allows interactive redering if true
bool allow_labeling = true; // allows drawing label if true
double runtime_lbm=0.0, runtime_total=0.0f; // lbm (compute) and total (compute + rendering + data evaluation) runtime
double runtime_lbm_timestep_last=1.0, runtime_lbm_timestep_smooth=1.0, runtime_lbm_last=0.0; // for printing simulation info
Clock clock; // for measuring total runtime
Expand Down
1 change: 1 addition & 0 deletions src/lbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,7 @@ int* LBM::Graphics::draw_frame() {
#endif // GRAPHICS_TRANSPARENCY
}
}
info.allow_labeling = new_frame;
return bitmap;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void draw_scale(const int field_mode, const int color) {
draw_label(ox+min(0, w+7+label_length_max*(FONT_WIDTH)-(int)length(title)*(FONT_WIDTH)), oy-(FONT_HEIGHT)*3/2-6, title, color); // colorbar title
}
void main_label(const double frametime) {
if(info.allow_rendering) {
if(info.allow_rendering&&info.allow_labeling) {
info.print_update();
const int c = invert(GRAPHICS_BACKGROUND_COLOR);
{
Expand All @@ -77,7 +77,7 @@ void main_label(const double frametime) {
draw_label(ox, oy+i, "Steps " +alignr(31u, /************************************/ alignr(10u, info.lbm->get_t())+" ("+alignr(5, to_uint(1.0/info.runtime_lbm_timestep_smooth))+" Steps/s)"), c); i+=FONT_HEIGHT;
draw_label(ox, oy+i, "FPS " +alignr(33u, /************************************************************/ alignr(4u, to_uint(1.0/frametime))+" ("+alignr(5u, camera.fps_limit)+" fps max)"), c);
}
draw_label(2, camera.height-1*(FONT_HEIGHT)-1, "FluidX3D v2.14 Copyright (c) Dr. Moritz Lehmann", c);
draw_label(2, camera.height-1*(FONT_HEIGHT)-1, "FluidX3D v2.15 Copyright (c) Dr. Moritz Lehmann", c);
if(!key_H) {
draw_label(camera.width-16*(FONT_WIDTH)-1, 2, "Press H for Help", c);
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "winres.h"
#undef APSTUDIO_READONLY_SYMBOLS

MAINICON ICON "src/icon.ico"
MAINICON ICON "icon.ico"

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEVERSION 0,0,0,0
PRODUCTVERSION 0,0,0,0
FILEFLAGSMASK 0x3FL
FILEFLAGS 0x0L
FILEOS 0x4L
Expand All @@ -19,12 +19,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "ProjectPhysX"
VALUE "FileDescription", "FluidX3D"
VALUE "FileVersion", "1.0.0.0"
VALUE "FileVersion", "0.0.0.0"
VALUE "InternalName", "FluidX3D"
VALUE "LegalCopyright", "(c) Moritz Lehmann"
VALUE "LegalCopyright", "(c) Dr. Moritz Lehmann"
VALUE "OriginalFilename", "FluidX3D.exe"
VALUE "ProductName", "FluidX3D"
VALUE "ProductVersion", "1.0"
VALUE "ProductVersion", "v2.15"
END
END
BLOCK "VarFileInfo"
Expand Down

0 comments on commit 1e5b86a

Please sign in to comment.