Skip to content

Commit

Permalink
Initial UI implementation for Console Compute tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
egorodet committed Mar 4, 2023
1 parent f535ac8 commit 352e746
Showing 1 changed file with 113 additions and 21 deletions.
134 changes: 113 additions & 21 deletions Apps/08-ConsoleCompute/ConsoleCompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,125 @@ Tutorial demonstrating "game of life" computing on GPU in console application
******************************************************************************/

#include "ftxui/component/component.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"
#include <Methane/Kit.h>
#include <Methane/Version.h>

int main(int, const char*[])
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <fmt/format.h>
#include <random>

namespace gfx = Methane::Graphics;
namespace rhi = Methane::Graphics::Rhi;

const rhi::Device* GetComputeDevice()
{
auto screen = ftxui::ScreenInteractive::Fullscreen();
rhi::System::Get().UpdateGpuDevices();
const rhi::Devices& devices = rhi::System::Get().GetGpuDevices();
if (devices.empty())
{
return nullptr;
}
return &devices[0];
}

ftxui::Component InitializeConsoleInterface(ftxui::ScreenInteractive& screen, const rhi::Device& device,
gfx::FrameSize& frame_size, std::mt19937& random_engine)
{
auto toolbar = ftxui::Container::Horizontal({
ftxui::Renderer([&frame_size, &device]
{
return ftxui::hbox({
ftxui::text(fmt::format(" GPU: {} ", device.GetAdapterName())),
ftxui::separator(),
ftxui::text(" FPS: XXX "),
ftxui::separator(),
ftxui::text(fmt::format(" Field: {} x {} ", frame_size.GetWidth(), frame_size.GetHeight()))
});
}) | ftxui::border | ftxui::xflex,
ftxui::Button(" X ", screen.ExitLoopClosure(), ftxui::ButtonOption::Simple()) | ftxui::align_right
});

auto sidebar = ftxui::Renderer([]
{
return ftxui::vbox({
ftxui::vbox() | ftxui::yflex,
ftxui::paragraph(fmt::format("Powered by {} v{} {}", METHANE_PRODUCT_NAME, METHANE_VERSION_STR, METHANE_PRODUCT_URL))
}) | ftxui::yflex;
});

auto middle = ftxui::Renderer([] { return ftxui::text("middle") | ftxui::center; });
auto left = ftxui::Renderer([] { return ftxui::text("Left") | ftxui::center; });
auto right = ftxui::Renderer([] { return ftxui::text("right") | ftxui::center; });
auto top = ftxui::Renderer([] { return ftxui::text("top") | ftxui::center; });
auto bottom = ftxui::Renderer([] { return ftxui::text("bottom") | ftxui::center; });
auto canvas = ftxui::Renderer([&random_engine, &frame_size]
{
return ftxui::canvas([&](ftxui::Canvas& c)
{
// Temporary drawing of random points on canvas
frame_size.SetWidth(c.width());
frame_size.SetHeight(c.height());
const uint32_t points_count = frame_size.GetPixelsCount() / 100;
std::uniform_int_distribution<> dist(0, frame_size.GetPixelsCount() - 1);
for(uint32_t i = 0; i < points_count; i++)
{
int rnd = dist(random_engine);
int x = rnd % c.width();
int y = rnd / c.width();
c.DrawBlockOn(x, y);
}
}) | ftxui::flex;
});

int left_size = 8;
int right_size = 8;
int top_size = 5;
int bottom_size = 5;
static int sidebar_width = 35;
auto main_container = ftxui::Container::Vertical(
{
toolbar | ftxui::xflex,
ftxui::ResizableSplitLeft(sidebar, canvas, &sidebar_width) | ftxui::border | ftxui::flex
});

auto container = middle;
container = ftxui::ResizableSplitLeft(left, container, &left_size);
container = ftxui::ResizableSplitRight(right, container, &right_size);
container = ftxui::ResizableSplitTop(top, container, &top_size);
container = ftxui::ResizableSplitBottom(bottom, container, &bottom_size);
return Renderer(main_container, [=]
{
return ftxui::vbox({
ftxui::text("Methane Console Compute: Game of Life") | ftxui::bold | ftxui::hcenter,
main_container->Render() | ftxui::flex,
});
});
}

void RunEventLoop(ftxui::ScreenInteractive& screen, const ftxui::Component& root, uint32_t& time)
{
std::atomic<bool> refresh_ui_continue = true;
std::thread refresh_ui([&screen, &time, &refresh_ui_continue]
{
while (refresh_ui_continue)
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(0.05s);
screen.Post([&] { time++; });
screen.Post(ftxui::Event::Custom);
}
});

screen.Loop(root);
refresh_ui_continue = false;
refresh_ui.join();
}

int main(int, const char*[])
{
std::random_device r;
std::seed_seq random_seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 random_engine(random_seed);
uint32_t time = 0;
gfx::FrameSize frame_size;

auto renderer = ftxui::Renderer(container, [&] { return container->Render() | ftxui::border; });
screen.Loop(renderer);
const rhi::Device* device_ptr = GetComputeDevice();
if (!device_ptr)
{
std::cerr << "ERROR: No GPU device available for computing!";
return 1;
}

ftxui::ScreenInteractive ui_screen = ftxui::ScreenInteractive::Fullscreen();
ftxui::Component ui_root = InitializeConsoleInterface(ui_screen, *device_ptr, frame_size, random_engine);
RunEventLoop(ui_screen, ui_root, time);
return 0;
}

9 comments on commit 352e746

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win32_DX_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 141 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win64_DX_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 156 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win32_VK_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 147 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS_MTL_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 65 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win64_VK_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 151 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ubuntu_VK_Release Test Results

  • ✅ 2967 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 12507 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win64_DX_SonarScan Tests Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneDataEventsTest.exe 95% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneDataRangeSetTest.exe 91% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneDataTypesTest.exe 98% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneGraphicsCameraTest.exe 61% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneGraphicsTypesTest.exe 98% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethanePlatformInputTest.exe 43% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneUserInterfaceTypesTest.exe 10% 100%
Summary 29% (2108 / 7284) 100% (0 / 0)

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS_MTL_SonarScan Tests Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
Default 36% 19%
Summary 36% (8155 / 22780) 19% (2964 / 15842)

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ubuntu_VK_SonarScan Tests Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
Default 25% 100%
Summary 25% (4527 / 18310) 100% (0 / 0)

Please sign in to comment.