diff options
| author | hachem <im@hachem.wtf> | 2025-08-13 18:20:54 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-13 18:20:54 +0200 |
| commit | 2d8f706efe6e0a02b3238c2f22e7cdce620e9807 (patch) | |
| tree | 06734503f8422c1509da09b5dd6a3ce50bad875c /src/core | |
| parent | 3b02022fd79ad0e130b8c49e7707d221bfc97bb5 (diff) | |
feat: compute shaders and compute textures
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/application.cpp | 122 | ||||
| -rw-r--r-- | src/core/application.h | 7 | ||||
| -rw-r--r-- | src/core/event.h | 42 |
3 files changed, 137 insertions, 34 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; } } diff --git a/src/core/application.h b/src/core/application.h index 2d8c55a..713ecba 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -47,6 +47,13 @@ namespace Donut std::shared_ptr<Shader> m_shader; std::shared_ptr<Texture2D> m_texture; + std::shared_ptr<Shader> m_compute_shader; + std::shared_ptr<Texture2D> m_processed_texture; + float m_compute_brightness; + float m_compute_contrast; + float m_compute_saturation; + bool m_use_compute_shader; + std::unique_ptr<Camera> m_camera; bool m_keys[1024] = { false }; diff --git a/src/core/event.h b/src/core/event.h index 6639f28..e4d1be1 100644 --- a/src/core/event.h +++ b/src/core/event.h @@ -81,7 +81,8 @@ namespace Donut virtual int get_category_flags() const override { return EventCategoryKeyboard | \ EventCategoryInput; } - class WindowResizeEvent : public Event + class WindowResizeEvent + : public Event { public: WindowResizeEvent(unsigned int width, unsigned int height) @@ -101,28 +102,32 @@ namespace Donut unsigned int m_width, m_height; }; - class WindowCloseEvent : public Event + class WindowCloseEvent + : public Event { public: WindowCloseEvent() = default; APPLICATION_EVENT_CLASS_TYPE(WindowClose) }; - class WindowFocusEvent : public Event + class WindowFocusEvent + : public Event { public: WindowFocusEvent() = default; APPLICATION_EVENT_CLASS_TYPE(WindowFocus) }; - class WindowLostFocusEvent : public Event + class WindowLostFocusEvent + : public Event { public: WindowLostFocusEvent() = default; APPLICATION_EVENT_CLASS_TYPE(WindowLostFocus) }; - class WindowMovedEvent : public Event + class WindowMovedEvent + : public Event { public: WindowMovedEvent(int x, int y) @@ -142,7 +147,8 @@ namespace Donut int m_x, m_y; }; - class KeyEvent : public Event + class KeyEvent + : public Event { public: int get_key_code() const { return m_key_code; } @@ -154,7 +160,8 @@ namespace Donut int m_key_code; }; - class KeyPressedEvent : public KeyEvent + class KeyPressedEvent + : public KeyEvent { public: KeyPressedEvent(int keycode, bool is_repeat = false) @@ -174,7 +181,8 @@ namespace Donut bool m_is_repeat; }; - class KeyReleasedEvent : public KeyEvent + class KeyReleasedEvent + : public KeyEvent { public: KeyReleasedEvent(int keycode) : KeyEvent(keycode) { } @@ -187,7 +195,8 @@ namespace Donut KEYBOARD_EVENT_CLASS_TYPE(KeyReleased) }; - class KeyTypedEvent : public KeyEvent + class KeyTypedEvent + : public KeyEvent { public: KeyTypedEvent(int keycode) : KeyEvent(keycode) { } @@ -200,7 +209,8 @@ namespace Donut KEYBOARD_EVENT_CLASS_TYPE(KeyTyped) }; - class MouseMovedEvent : public Event + class MouseMovedEvent + : public Event { public: MouseMovedEvent(float x, float y) @@ -220,7 +230,8 @@ namespace Donut float m_mouse_x, m_mouse_y; }; - class MouseScrolledEvent : public Event + class MouseScrolledEvent + : public Event { public: MouseScrolledEvent(float x_offset, float y_offset) @@ -241,7 +252,8 @@ namespace Donut float m_x_offset, m_y_offset; }; - class MouseButtonEvent : public Event + class MouseButtonEvent + : public Event { public: int get_mouse_button() const { return m_button; } @@ -254,7 +266,8 @@ namespace Donut int m_button; }; - class MouseButtonPressedEvent : public MouseButtonEvent + class MouseButtonPressedEvent + : public MouseButtonEvent { public: MouseButtonPressedEvent(int button) : MouseButtonEvent(button) { } @@ -267,7 +280,8 @@ namespace Donut MOUSE_BUTTON_EVENT_CLASS_TYPE(MouseButtonPressed) }; - class MouseButtonReleasedEvent : public MouseButtonEvent + class MouseButtonReleasedEvent + : public MouseButtonEvent { public: MouseButtonReleasedEvent(int button) : MouseButtonEvent(button) { } |
