hello world!

main
Brett 2025-04-05 21:14:43 -04:00
parent ab1f83b716
commit 23587bb2f0
5 changed files with 125 additions and 26 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(image-gp-2 VERSION 0.0.10)
project(image-gp-2 VERSION 0.0.11)
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

View File

@ -27,6 +27,14 @@ void run_step();
bool should_terminate();
blt::u32 get_generation();
void reset_programs();
void regenerate_image(blt::size_t index, float& image_storage, blt::i32 width, blt::i32 height);
void set_population_size(blt::u32 size);
std::array<image_pixel_t, IMAGE_DIMENSIONS * IMAGE_DIMENSIONS * 3>& get_image(blt::size_t index);
void cleanup();

@ -1 +1 @@
Subproject commit 9b1c1a1bb116ca7f6fdec666f74cde8a6a40f7f9
Subproject commit ebcdbeb172bc8874b7898a01acad29560ad5d068

View File

@ -33,6 +33,7 @@ float filter_nan(const float f)
}
std::array<gp_program*, 3> programs;
prog_config_t config{};
std::vector<std::array<image_pixel_t, IMAGE_DIMENSIONS * IMAGE_DIMENSIONS * 3>> images;
auto reference_image = image_storage_t::from_file("../silly.png");
@ -185,8 +186,8 @@ void setup_operations(gp_program* program)
octaves = std::min(std::max(octaves, 4.0f), 8.0f);
image_t ret{};
for (const auto& [i, out] : blt::enumerate(ret.get_data().data))
out = stb_perlin_fbm_noise3(static_cast<float>(i % IMAGE_DIMENSIONS) + 0.23423f, static_cast<float>(i / IMAGE_DIMENSIONS) + 0.6234f, 0.4861f,
2, 0.5, static_cast<int>(octaves));
out = stb_perlin_fbm_noise3(static_cast<float>(i % IMAGE_DIMENSIONS) + 0.23423f, static_cast<float>(i / IMAGE_DIMENSIONS) + 0.6234f,
0.4861f, 2, 0.5, static_cast<int>(octaves));
return ret;
}, "perlin_image_bounded");
static operation_t op_sin([](const float a) {
@ -212,14 +213,13 @@ void setup_operations(gp_program* program)
operator_builder builder{};
builder.build(op_image_ephemeral, make_add<image_t>(), make_sub<image_t>(), make_mul<image_t>(), make_div<image_t>(), op_image_x, op_image_y,
op_image_sin, op_image_gt, op_image_lt, op_image_cos, op_image_log, op_image_exp, op_image_or, op_image_and, op_image_xor,
op_image_not, op_image_perlin_bounded, op_image_blend, make_add<float>(), make_sub<float>(), make_mul<float>(), make_prot_div<float>(), op_sin, op_cos,
op_exp, op_log, lit);
op_image_not, op_image_perlin_bounded, op_image_blend, make_add<float>(), make_sub<float>(), make_mul<float>(),
make_prot_div<float>(), op_sin, op_cos, op_exp, op_log, lit);
program->set_operations(builder.grab());
}
void setup_gp_system(const blt::size_t population_size)
{
prog_config_t config{};
config.set_pop_size(population_size);
config.set_elite_count(2);
config.set_thread_count(0);
@ -314,3 +314,29 @@ std::array<image_pixel_t, IMAGE_DIMENSIONS * IMAGE_DIMENSIONS * 3> to_gl_image(c
}
return image_data;
}
blt::u32 get_generation()
{
return programs[0]->get_current_generation();
}
void set_population_size(const blt::u32 size)
{
if (size > images.size())
images.resize(size);
config.set_pop_size(size);
for (const auto program : programs)
program->set_config(config);
reset_programs();
}
void reset_programs()
{
for (const auto program : programs)
program->reset_program(program->get_typesystem().get_type<image_t>().id());
}
void regenerate_image(blt::size_t index, float& image_storage, blt::i32 width, blt::i32 height)
{
}

View File

@ -7,6 +7,8 @@
#include <imgui.h>
#include <thread>
#include "../cmake-build-release-examples/_deps/imgui-src/imgui_internal.h"
blt::gfx::matrix_state_manager global_matrices;
blt::gfx::resource_manager resources;
blt::gfx::batch_renderer_2d renderer_2d(resources, global_matrices);
@ -20,12 +22,27 @@ namespace im = ImGui;
std::atomic_bool run_generation = false;
std::atomic_bool should_exit = false;
void update_population_size(const blt::u32 new_size)
{
using namespace blt::gfx;
if (new_size == population_size)
return;
for (blt::size_t i = population_size; i < new_size; i++)
{
auto texture = new texture_gl2D(IMAGE_DIMENSIONS, IMAGE_DIMENSIONS, GL_RGBA8);
gl_images.push_back(texture);
resources.set(std::to_string(i), texture);
}
set_population_size(new_size);
population_size = new_size;
}
std::thread run_gp()
{
return std::thread{
[]() {
setup_gp_system(population_size);
while (!should_terminate() && !should_exit)
while (!should_exit)
{
if (run_generation)
{
@ -61,8 +78,8 @@ void init(const blt::gfx::window_data&)
void update(const blt::gfx::window_data& data)
{
constexpr float side_bar_width = 250;
static float top_bar_height = 0;
static float side_bar_width = 260;
static float top_bar_height = 32;
global_matrices.update_perspectives(data.width, data.height, 90, 0.1, 2000);
@ -78,22 +95,34 @@ void update(const blt::gfx::window_data& data)
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoBackground);
static blt::i32 image_to_enlarge = -1;
bool clicked_on_image = false;
// Create the tab bar
if (ImGui::BeginTabBar("MainTabs"))
{
top_bar_height = ImGui::GetFrameHeight();
constexpr float padding_x = 16;
constexpr float padding_y = 16;
// 1. Run GP tab
static int images_x = 10;
static int images_y = 6;
static bool run_gp = false;
static int generation_limit = 0;
if (ImGui::BeginTabItem("Run GP"))
{
ImGui::BeginChild("ControlPanel", ImVec2(side_bar_width, 0), true);
ImGui::BeginChild("ControlPanel", ImVec2(250, 0), true);
{
ImGui::Text("Control Panel");
ImGui::Separator();
if (ImGui::Button("Run Step"))
{
BLT_TRACE("Running step");
run_generation = true;
}
ImGui::Checkbox("Run GP", &run_gp);
if (ImGui::InputInt("Images X", &images_x) || ImGui::InputInt("Images Y", &images_y))
update_population_size(images_x * images_y);
ImGui::InputInt("Generation Limit", &generation_limit);
if (run_gp && (generation_limit == 0 || get_generation() < generation_limit))
run_generation = true;
}
ImGui::EndChild();
@ -101,20 +130,41 @@ void update(const blt::gfx::window_data& data)
ImGui::SameLine();
// ImGui::BeginChild("MainContent", ImVec2(0, 0), false, ImGuiWindowFlags_NoBackground);
{
constexpr int images_x = 10;
constexpr int images_y = 6;
const auto area_x = static_cast<float>(data.width) - side_bar_width;
const auto area_y = static_cast<float>(data.height) - top_bar_height;
const auto padding_area_x = (static_cast<float>(images_x) + 2) * padding_x;
const auto padding_area_y = (static_cast<float>(images_y) + 2) * padding_y;
const auto area_width = area_x - padding_area_x;
const auto area_height = area_y - padding_area_y;
const auto image_width = area_width / static_cast<float>(images_x);
const auto image_height = area_height / static_cast<float>(images_y);
for (int i = 0; i < images_x; i++)
{
for (int j = 0; j < images_y; j++)
{
constexpr float padding_x = 32;
constexpr float padding_y = 32;
const float img_width = (static_cast<float>(data.width) - padding_x * 2 - padding_x * (images_x - 1) - 256) / images_x;
const float img_height = (static_cast<float>(data.height) - padding_y * 2 - padding_y * (images_y - 1) - 32) / images_y;
const float x = 256 + static_cast<float>(i) * img_width + padding_x * static_cast<float>(i) + img_width;
const float y = static_cast<float>(data.height) - (16 + static_cast<float>(j) * img_height + padding_y * static_cast<float>(j) +
img_height);
renderer_2d.drawRectangle(blt::gfx::rectangle2d_t{x, y, img_width, img_height}, std::to_string(i * images_y + j));
int image_at_pos = i * images_y + j;
const auto mx = static_cast<float>(blt::gfx::getMouseX());
const auto my = static_cast<float>(data.height) - static_cast<float>(blt::gfx::getMouseY());
const auto x = side_bar_width + static_cast<float>(i) * (image_width + padding_x) + image_width / 2 + padding_x;
const auto y = static_cast<float>(j) * (image_height + padding_y) + image_height / 2 + padding_y / 2;
blt::vec2 extra;
if (mx >= x - image_width / 2 && mx <= x + image_width / 2 && my >= y - image_height / 2 && my <= y + image_height / 2)
{
extra = {padding_x / 2.0f, padding_y / 2.0f};
if (blt::gfx::isMousePressed(0) && blt::gfx::mousePressedLastFrame())
{
image_to_enlarge = image_at_pos;
clicked_on_image = true;
}
}
renderer_2d.drawRectangle(blt::gfx::rectangle2d_t{x, y, image_width + extra.x(), image_height + extra.y()},
std::to_string(image_at_pos));
}
}
}
@ -143,7 +193,7 @@ void update(const blt::gfx::window_data& data)
{
auto w = data.width;
auto h = data.height - top_bar_height - 10;
renderer_2d.drawRectangle({w/2, h/2, w, h}, "reference");
renderer_2d.drawRectangle({w / 2, h / 2, w, h}, "reference");
ImGui::EndTabItem();
}
@ -184,6 +234,21 @@ void update(const blt::gfx::window_data& data)
gl_images[i]->upload(get_image(i).data(), IMAGE_DIMENSIONS, IMAGE_DIMENSIONS, GL_RGB, GL_FLOAT);
}
if ((blt::gfx::isMousePressed(0) && blt::gfx::mousePressedLastFrame() && !clicked_on_image) || (blt::gfx::isKeyPressed(GLFW_KEY_ESCAPE) && blt::gfx::keyPressedLastFrame()))
image_to_enlarge = -1;
if (image_to_enlarge != -1)
{
if (blt::gfx::isKeyPressed(GLFW_KEY_R) && blt::gfx::keyPressedLastFrame())
{
}
renderer_2d.drawRectangle(blt::gfx::rectangle2d_t{blt::gfx::anchor_t::BOTTOM_LEFT,
side_bar_width + 256, 64, std::min(static_cast<float>(data.width) - side_bar_width, static_cast<float>(256) * 3),
std::min(static_cast<float>(data.height) - top_bar_height, static_cast<float>(256) * 3)
}, std::to_string(image_to_enlarge), 1);
}
renderer_2d.render(data.width, data.height);
}