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 | f1517709556827fb8b7bab00bdee4a9794d88dd1 (patch) | |
| tree | a1b2325c8bc693bff0ace1fd98ea7dac91f76286 /src/Core | |
| parent | 1ad6bc34323f5b30d463ebe47a58d47ff9d4f328 (diff) | |
[add]: 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 | 8 | ||||
| -rw-r--r-- | src/Core/Event.h | 42 |
3 files changed, 138 insertions, 34 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp index 0fd0023..4db79a5 100644 --- a/src/Core/Application.cpp +++ b/src/Core/Application.cpp @@ -224,6 +224,30 @@ namespace Donut m_Texture->SetData(pixelData, 256 * 256 * 4); delete[] pixelData; + + m_ComputeShader = std::shared_ptr<Shader>(Shader::Create("assets/TextureProcessor.glsl")); + + if (!m_ComputeShader) + { + std::cout << "Failed to create compute shader! Falling back to CPU texture generation." << std::endl; + m_UseComputeShader = false; + } + else + std::cout << "Compute shader created successfully!" << std::endl; + + m_ProcessedTexture = Texture2D::Create(256, 256); + m_ComputeBrightness = 0.0f; + m_ComputeContrast = 1.0f; + m_ComputeSaturation = 1.0f; + m_UseComputeShader = 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::OnShutdown() @@ -253,6 +277,42 @@ namespace Donut m_Camera->MoveDown(m_DeltaTime); } + static bool cKeyPressed = false; + if (m_Keys[GLFW_KEY_C] && !cKeyPressed) + { + m_UseComputeShader = !m_UseComputeShader; + std::cout << "Compute shader: " << (m_UseComputeShader ? "ON" : "OFF") << std::endl; + cKeyPressed = true; + } + if (!m_Keys[GLFW_KEY_C]) + cKeyPressed = false; + if (m_Keys[GLFW_KEY_UP]) + m_ComputeBrightness += m_DeltaTime * 0.5f; + if (m_Keys[GLFW_KEY_DOWN]) + m_ComputeBrightness -= m_DeltaTime * 0.5f; + if (m_Keys[GLFW_KEY_LEFT]) + m_ComputeContrast -= m_DeltaTime * 0.5f; + if (m_Keys[GLFW_KEY_RIGHT]) + m_ComputeContrast += m_DeltaTime * 0.5f; + if (m_Keys[GLFW_KEY_Q]) + m_ComputeSaturation -= m_DeltaTime * 0.5f; + if (m_Keys[GLFW_KEY_E]) + m_ComputeSaturation += m_DeltaTime * 0.5f; + + m_ComputeBrightness = glm::clamp(m_ComputeBrightness, -1.0f, 1.0f); + m_ComputeContrast = glm::clamp(m_ComputeContrast, 0.1f, 3.0f); + m_ComputeSaturation = glm::clamp(m_ComputeSaturation, 0.0f, 2.0f); + + static float statusTimer = 0.0f; + statusTimer += m_DeltaTime; + if (statusTimer > 2.0f) + { + std::cout << "Compute Shader Status - Brightness: " << m_ComputeBrightness + << ", Contrast: " << m_ComputeContrast + << ", Saturation: " << m_ComputeSaturation << std::endl; + statusTimer = 0.0f; + } + UpdateTexture(); } @@ -261,7 +321,12 @@ namespace Donut Renderer::SetClearColor({ 0.0f, 0.0f, 0.0f, 1.0f }); Renderer::Clear(); - if (m_Texture) + if (m_UseComputeShader && m_ProcessedTexture) + { + m_ProcessedTexture->Bind(0); + m_Shader->SetInt("u_Texture", 0); + } + else if (m_Texture) { m_Texture->Bind(0); m_Shader->SetInt("u_Texture", 0); @@ -300,28 +365,45 @@ namespace Donut m_TextureTime += m_DeltaTime; - uint32_t* pixelData = new uint32_t[256 * 256]; - for (int y = 0; y < 256; y++) + if (m_UseComputeShader && m_ComputeShader && m_ProcessedTexture) { - for (int x = 0; x < 256; x++) + m_ComputeShader->Bind(); + m_Texture->Bind(0); + m_ComputeShader->SetInt("u_InputTexture", 0); + m_ProcessedTexture->BindAsImage(1, false); + m_ComputeShader->SetFloat2("u_TextureSize", glm::vec2(256.0f, 256.0f)); + m_ComputeShader->SetFloat("u_Time", m_TextureTime); + m_ComputeShader->SetFloat("u_Brightness", m_ComputeBrightness); + m_ComputeShader->SetFloat("u_Contrast", m_ComputeContrast); + m_ComputeShader->SetFloat("u_Saturation", m_ComputeSaturation); + m_ComputeShader->Dispatch(16, 16, 1); + m_ComputeShader->MemoryBarrier(IMAGE_ACCESS_BARRIER_BIT); + } + else + { + uint32_t* pixelData = new uint32_t[256 * 256]; + for (int y = 0; y < 256; y++) { - float time = m_TextureTime * 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; - - pixelData[y * 256 + x] = (a << 24) | (b << 16) | (g << 8) | r; + for (int x = 0; x < 256; x++) + { + float time = m_TextureTime * 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; + + pixelData[y * 256 + x] = (a << 24) | (b << 16) | (g << 8) | r; + } } + + m_Texture->SetData(pixelData, 256 * 256 * 4); + delete[] pixelData; } - - m_Texture->SetData(pixelData, 256 * 256 * 4); - delete[] pixelData; } } diff --git a/src/Core/Application.h b/src/Core/Application.h index 7406136..922af67 100644 --- a/src/Core/Application.h +++ b/src/Core/Application.h @@ -47,6 +47,14 @@ namespace Donut std::shared_ptr<Shader> m_Shader; std::shared_ptr<Texture2D> m_Texture; + // Compute shader related members + std::shared_ptr<Shader> m_ComputeShader; + std::shared_ptr<Texture2D> m_ProcessedTexture; + float m_ComputeBrightness; + float m_ComputeContrast; + float m_ComputeSaturation; + bool m_UseComputeShader; + std::unique_ptr<Camera> m_Camera; bool m_Keys[1024] = { false }; diff --git a/src/Core/Event.h b/src/Core/Event.h index 32cdde4..60d446d 100644 --- a/src/Core/Event.h +++ b/src/Core/Event.h @@ -81,7 +81,8 @@ namespace Donut virtual int GetCategoryFlags() 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 GetKeyCode() const { return m_KeyCode; } @@ -154,7 +160,8 @@ namespace Donut int m_KeyCode; }; - class KeyPressedEvent : public KeyEvent + class KeyPressedEvent + : public KeyEvent { public: KeyPressedEvent(int keycode, bool isRepeat = false) @@ -174,7 +181,8 @@ namespace Donut bool m_IsRepeat; }; - 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_MouseX, m_MouseY; }; - class MouseScrolledEvent : public Event + class MouseScrolledEvent + : public Event { public: MouseScrolledEvent(float xOffset, float yOffset) @@ -241,7 +252,8 @@ namespace Donut float m_XOffset, m_YOffset; }; - class MouseButtonEvent : public Event + class MouseButtonEvent + : public Event { public: int GetMouseButton() 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) { } |
