hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/core/application.cpp
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-08-13 18:20:54 +0200
committerhachem <im@hachem.wtf>2025-08-13 18:20:54 +0200
commit2d8f706efe6e0a02b3238c2f22e7cdce620e9807 (patch)
tree06734503f8422c1509da09b5dd6a3ce50bad875c /src/core/application.cpp
parent3b02022fd79ad0e130b8c49e7707d221bfc97bb5 (diff)
feat: compute shaders and compute textures
Diffstat (limited to 'src/core/application.cpp')
-rw-r--r--src/core/application.cpp122
1 files changed, 102 insertions, 20 deletions
diff --git a/src/core/application.cpp b/src/core/application.cpp
index 0acd232..435b6aa 100644
--- a/src/core/application.cpp
+++ b/src/core/application.cpp
@@ -223,6 +223,30 @@ namespace Donut
m_texture->set_data(pixel_data, 256 * 256 * 4);
delete[] pixel_data;
+
+ m_compute_shader = std::shared_ptr<Shader>(Shader::create("assets/texture_processor.glsl"));
+
+ if (!m_compute_shader)
+ {
+ std::cout << "Failed to create compute shader! Falling back to CPU texture generation." << std::endl;
+ m_use_compute_shader = false;
+ }
+ else
+ std::cout << "Compute shader created successfully!" << std::endl;
+
+ m_processed_texture = Texture2D::create(256, 256);
+ m_compute_brightness = 0.0f;
+ m_compute_contrast = 1.0f;
+ m_compute_saturation = 1.0f;
+ m_use_compute_shader = true;
+
+ std::cout << "=== Compute Shader Test ===" << std::endl;
+ std::cout << "C - Toggle compute shader on/off" << std::endl;
+ std::cout << "Arrow Keys - Adjust brightness (Up/Down) and contrast (Left/Right)" << std::endl;
+ std::cout << "Q/E - Adjust saturation" << std::endl;
+ std::cout << "WASD - Move camera" << std::endl;
+ std::cout << "Mouse - Look around" << std::endl;
+ std::cout << "==========================" << std::endl;
}
void Application::on_shutdown()
@@ -252,6 +276,42 @@ namespace Donut
m_camera->move_down(m_delta_time);
}
+ static bool c_key_pressed = false;
+ if (m_keys[GLFW_KEY_C] && !c_key_pressed)
+ {
+ m_use_compute_shader = !m_use_compute_shader;
+ std::cout << "Compute shader: " << (m_use_compute_shader ? "ON" : "OFF") << std::endl;
+ c_key_pressed = true;
+ }
+ if (!m_keys[GLFW_KEY_C])
+ c_key_pressed = false;
+ if (m_keys[GLFW_KEY_UP])
+ m_compute_brightness += m_delta_time * 0.5f;
+ if (m_keys[GLFW_KEY_DOWN])
+ m_compute_brightness -= m_delta_time * 0.5f;
+ if (m_keys[GLFW_KEY_LEFT])
+ m_compute_contrast -= m_delta_time * 0.5f;
+ if (m_keys[GLFW_KEY_RIGHT])
+ m_compute_contrast += m_delta_time * 0.5f;
+ if (m_keys[GLFW_KEY_Q])
+ m_compute_saturation -= m_delta_time * 0.5f;
+ if (m_keys[GLFW_KEY_E])
+ m_compute_saturation += m_delta_time * 0.5f;
+
+ m_compute_brightness = glm::clamp(m_compute_brightness, -1.0f, 1.0f);
+ m_compute_contrast = glm::clamp(m_compute_contrast, 0.1f, 3.0f);
+ m_compute_saturation = glm::clamp(m_compute_saturation, 0.0f, 2.0f);
+
+ static float status_timer = 0.0f;
+ status_timer += m_delta_time;
+ if (status_timer > 2.0f)
+ {
+ std::cout << "Compute Shader Status - Brightness: " << m_compute_brightness
+ << ", Contrast: " << m_compute_contrast
+ << ", Saturation: " << m_compute_saturation << std::endl;
+ status_timer = 0.0f;
+ }
+
update_texture();
}
@@ -260,7 +320,12 @@ namespace Donut
Renderer::set_clear_color({ 0.0f, 0.0f, 0.0f, 1.0f });
Renderer::clear();
- if (m_texture)
+ if (m_use_compute_shader && m_processed_texture)
+ {
+ m_processed_texture->bind(0);
+ m_shader->set_int("u_Texture", 0);
+ }
+ else if (m_texture)
{
m_texture->bind(0);
m_shader->set_int("u_Texture", 0);
@@ -299,28 +364,45 @@ namespace Donut
m_texture_time += m_delta_time;
- uint32_t* pixel_data = new uint32_t[256 * 256];
- for (int y = 0; y < 256; y++)
+ if (m_use_compute_shader && m_compute_shader && m_processed_texture)
{
- for (int x = 0; x < 256; x++)
+ m_compute_shader->bind();
+ m_texture->bind(0);
+ m_compute_shader->set_int("u_InputTexture", 0);
+ m_processed_texture->bind_as_image(1, false);
+ m_compute_shader->set_float2("u_TextureSize", glm::vec2(256.0f, 256.0f));
+ m_compute_shader->set_float("u_Time", m_texture_time);
+ m_compute_shader->set_float("u_Brightness", m_compute_brightness);
+ m_compute_shader->set_float("u_Contrast", m_compute_contrast);
+ m_compute_shader->set_float("u_Saturation", m_compute_saturation);
+ m_compute_shader->dispatch(16, 16, 1);
+ m_compute_shader->memory_barrier(IMAGE_ACCESS_BARRIER_BIT);
+ }
+ else
+ {
+ uint32_t* pixel_data = new uint32_t[256 * 256];
+ for (int y = 0; y < 256; y++)
{
- float time = m_texture_time * 2.0f;
- float u = (float)x / 256.0f;
- float v = (float)y / 256.0f;
-
- float wave = sin(u * 10.0f + time) * cos(v * 10.0f + time * 0.5f);
- wave = (wave + 1.0f) * 0.5f;
-
- uint8_t r = (uint8_t)(wave * 255);
- uint8_t g = (uint8_t)((1.0f - wave) * 255);
- uint8_t b = (uint8_t)((u + v) * 0.5f * 255);
- uint8_t a = 255;
-
- pixel_data[y * 256 + x] = (a << 24) | (b << 16) | (g << 8) | r;
+ for (int x = 0; x < 256; x++)
+ {
+ float time = m_texture_time * 2.0f;
+ float u = (float)x / 256.0f;
+ float v = (float)y / 256.0f;
+
+ float wave = sin(u * 10.0f + time) * cos(v * 10.0f + time * 0.5f);
+ wave = (wave + 1.0f) * 0.5f;
+
+ uint8_t r = (uint8_t)(wave * 255);
+ uint8_t g = (uint8_t)((1.0f - wave) * 255);
+ uint8_t b = (uint8_t)((u + v) * 0.5f * 255);
+ uint8_t a = 255;
+
+ pixel_data[y * 256 + x] = (a << 24) | (b << 16) | (g << 8) | r;
+ }
}
+
+ m_texture->set_data(pixel_data, 256 * 256 * 4);
+ delete[] pixel_data;
}
-
- m_texture->set_data(pixel_data, 256 * 256 * 4);
- delete[] pixel_data;
}
}