big refactor

This commit is contained in:
2025-11-09 00:11:12 +01:00
parent ba02b9e3ed
commit 60e0b29d07
6 changed files with 132 additions and 164 deletions

37
.clang-format Normal file
View File

@@ -0,0 +1,37 @@
Language: Cpp
BasedOnStyle: LLVM
AlignAfterOpenBracket: DontAlign
BreakConstructorInitializers: AfterColon
ConstructorInitializerAllOnOneLineOrOnePerLine: true
PointerAlignment: Left
SortIncludes: true
SpacesBeforeTrailingComments: 2
UseTab: Always
MaxEmptyLinesToKeep: 5
TabWidth: 4
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
IndentWidth: 4
IndentCaseLabels: true
ColumnLimit: 135
AlwaysBreakTemplateDeclarations: Yes
AllowShortFunctionsOnASingleLine: Empty
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterExternBlock: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true

View File

@@ -3,36 +3,32 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "Renderer.h" #include "Renderer.h"
#include "Shader.h"
#include <chrono> #include <chrono>
class Timer constexpr int PARTICLE_COUNT = 64 * 5000;
{
public:
Timer() { Reset(); }
void Reset() { m_Start = std::chrono::high_resolution_clock::now(); }
float Elapsed() const { return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() * 0.001f * 0.001f; }
float ElapsedMillis() const { return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() * 0.001f; }
private: class Timer {
std::chrono::time_point<std::chrono::high_resolution_clock> m_Start; public:
}; Timer() {
Reset();
class ScopedTimer }
{ void Reset() {
public: m_Start = std::chrono::high_resolution_clock::now();
ScopedTimer(std::string_view name) : m_Name(name) {} }
~ScopedTimer() float Elapsed() const {
{ return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() *
float time = m_Timer.ElapsedMillis(); 0.001f * 0.001f;
std::cout << m_Name << " - " << time << "ms\n"; }
float ElapsedMillis() const {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() *
0.001f;
} }
private: private:
Timer m_Timer; std::chrono::time_point<std::chrono::high_resolution_clock> m_Start;
std::string m_Name;
}; };
static uint32_t s_ComputeShader = -1; static uint32_t s_ComputeShader = -1;
@@ -41,57 +37,51 @@ static const std::filesystem::path s_ComputeShaderPath = "Shaders/Compute.glsl";
static const std::filesystem::path s_VertexShaderPath = "Shaders/Vertex.glsl"; static const std::filesystem::path s_VertexShaderPath = "Shaders/Vertex.glsl";
static const std::filesystem::path s_FragmentShaderPath = "Shaders/Fragment.glsl"; static const std::filesystem::path s_FragmentShaderPath = "Shaders/Fragment.glsl";
static void ErrorCallback(int error, const char *description) static void ErrorCallback(int error, const char* description) {
{
std::cerr << "Error: " << description << std::endl; std::cerr << "Error: " << description << std::endl;
} }
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
{
if (action != GLFW_PRESS) if (action != GLFW_PRESS)
return; return;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE); glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_R) if (key == GLFW_KEY_R) {
{
s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath); s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath);
s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath); s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath);
} }
} }
std::vector<float> GenPoints(float x, float y) static GLuint CreateDummyVAO() {
{ GLuint vertexArray;
x /= 2; glCreateVertexArrays(1, &vertexArray);
y /= 2;
return { GLuint vertexBuffer;
x, y + 0.36f, glCreateBuffers(1, &vertexBuffer);
x - 0.5f, y - 0.5f,
x + 0.5f, y - 0.5f}; // Buffer with just one point
float vertices[] = {0.0f, 0.0f};
glNamedBufferData(vertexBuffer, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, 0, sizeof(float) * 2);
glEnableVertexArrayAttrib(vertexArray, 0);
glVertexArrayAttribFormat(vertexArray, 0, 2, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vertexArray, 0, 0);
return vertexArray;
} }
std::vector<float> GetPoints(int depth) static void CreateGpuBuffer(std::size_t a_Size) {
{ GLuint ssbo;
std::vector<float> result = {0.0f, 0.0f}; glGenBuffers(1, &ssbo);
std::size_t offset = 0; glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
std::size_t count = 1; glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo);
for (std::size_t i = 1; i < depth; i++) glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * a_Size, nullptr,
{ GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
for (std::size_t j = 0; j < count; j++)
{
std::size_t index = offset + j * 2;
auto newPoints = GenPoints(result[index], result[index + 1]);
result.insert(result.end(), newPoints.begin(), newPoints.end());
}
offset += count * 2;
count *= 3;
}
return result;
} }
int main() static GLFWwindow* InitWindow() {
{
glfwSetErrorCallback(ErrorCallback); glfwSetErrorCallback(ErrorCallback);
if (!glfwInit()) if (!glfwInit())
@@ -103,9 +93,8 @@ int main()
int width = 1280; int width = 1280;
int height = 720; int height = 720;
GLFWwindow *window = glfwCreateWindow(width, height, "Compute", NULL, NULL); GLFWwindow* window = glfwCreateWindow(width, height, "", NULL, NULL);
if (!window) if (!window) {
{
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@@ -117,61 +106,37 @@ int main()
glfwSwapInterval(1); glfwSwapInterval(1);
s_ComputeShader = CreateComputeShader(s_ComputeShaderPath); s_ComputeShader = CreateComputeShader(s_ComputeShaderPath);
if (s_ComputeShader == -1) if (s_ComputeShader == -1) {
{
std::cerr << "Compute shader failed\n"; std::cerr << "Compute shader failed\n";
return -1; return nullptr;
} }
s_GraphicsShader = CreateGraphicsShader(s_VertexShaderPath, s_FragmentShaderPath); s_GraphicsShader = CreateGraphicsShader(s_VertexShaderPath, s_FragmentShaderPath);
if (s_GraphicsShader == -1) if (s_GraphicsShader == -1) {
{
std::cerr << "Graphics shader failed\n"; std::cerr << "Graphics shader failed\n";
return -1; return nullptr;
} }
Texture computeShaderTexture = CreateTexture(width, height); return window;
Framebuffer fb = CreateFramebufferWithTexture(computeShaderTexture); }
GLuint vertexArray; int main() {
glCreateVertexArrays(1, &vertexArray); GLFWwindow* window = InitWindow();
if (!window)
return -1;
GLuint vertexBuffer; auto vertexArray = CreateDummyVAO();
glCreateBuffers(1, &vertexBuffer);
constexpr int DEPTH = 6; CreateGpuBuffer(PARTICLE_COUNT);
auto vertices = GetPoints(DEPTH);
glNamedBufferData(vertexBuffer, sizeof(float) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
glVertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, 0, sizeof(float) * 2);
glEnableVertexArrayAttrib(vertexArray, 0);
glEnableVertexArrayAttrib(vertexArray, 1);
glVertexArrayAttribFormat(vertexArray, 0, 2, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vertexArray, 0, 0);
glVertexArrayAttribBinding(vertexArray, 1, 0);
Timer timer;
constexpr int PARTICLE_COUNT = 64 * 5000;
GLuint ssbo;
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * PARTICLE_COUNT, nullptr, GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
float lastTime = (float)glfwGetTime(); float lastTime = (float)glfwGetTime();
int fps = 0; int fps = 0;
float secondsTimer = 0.0f; float secondsTimer = 0.0f;
while (!glfwWindowShouldClose(window)) glBindVertexArray(vertexArray);
{
while (!glfwWindowShouldClose(window)) {
// ScopedTimer timer("Main Loop"); // ScopedTimer timer("Main Loop");
float currentTime = (float)glfwGetTime(); float currentTime = (float)glfwGetTime();
@@ -179,8 +144,7 @@ int main()
lastTime = currentTime; lastTime = currentTime;
secondsTimer += dt; secondsTimer += dt;
if (secondsTimer >= 1.0f) if (secondsTimer >= 1.0f) {
{
std::string title = "FPS : " + std::to_string(fps); std::string title = "FPS : " + std::to_string(fps);
glfwSetWindowTitle(window, title.c_str()); glfwSetWindowTitle(window, title.c_str());
@@ -188,18 +152,7 @@ int main()
fps = 0; fps = 0;
} }
glfwGetFramebufferSize(window, &width, &height);
// Resize texture
if (width != computeShaderTexture.Width || height != computeShaderTexture.Height)
{
glDeleteTextures(1, &computeShaderTexture.Handle);
computeShaderTexture = CreateTexture(width, height);
AttachTextureToFramebuffer(fb, computeShaderTexture);
}
// Compute // Compute
glUseProgram(s_ComputeShader); glUseProgram(s_ComputeShader);
glDispatchCompute(PARTICLE_COUNT / 64, 1, 1); glDispatchCompute(PARTICLE_COUNT / 64, 1, 1);
@@ -209,7 +162,6 @@ int main()
// Graphics // Graphics
glUseProgram(s_GraphicsShader); glUseProgram(s_GraphicsShader);
glBindVertexArray(vertexArray);
glDrawArraysInstanced(GL_POINTS, 0, 1, PARTICLE_COUNT); glDrawArraysInstanced(GL_POINTS, 0, 1, PARTICLE_COUNT);
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@@ -5,8 +5,7 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
Texture CreateTexture(int width, int height) Texture CreateTexture(int width, int height) {
{
Texture result; Texture result;
result.Width = width; result.Width = width;
result.Height = height; result.Height = height;
@@ -24,21 +23,17 @@ Texture CreateTexture(int width, int height)
return result; return result;
} }
Texture LoadTexture(const std::filesystem::path& path) Texture LoadTexture(const std::filesystem::path& path) {
{
int width, height, channels; int width, height, channels;
std::string filepath = path.string(); std::string filepath = path.string();
unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &channels, 0); unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &channels, 0);
if (!data) if (!data) {
{
std::cerr << "Failed to load texture: " << filepath << "\n"; std::cerr << "Failed to load texture: " << filepath << "\n";
return {}; return {};
} }
GLenum format = channels == 4 ? GL_RGBA : GLenum format = channels == 4 ? GL_RGBA : channels == 3 ? GL_RGB : channels == 1 ? GL_RED : 0;
channels == 3 ? GL_RGB :
channels == 1 ? GL_RED : 0;
Texture result; Texture result;
result.Width = width; result.Width = width;
@@ -62,14 +57,12 @@ Texture LoadTexture(const std::filesystem::path& path)
return result; return result;
} }
Framebuffer CreateFramebufferWithTexture(const Texture texture) Framebuffer CreateFramebufferWithTexture(const Texture texture) {
{
Framebuffer result; Framebuffer result;
glCreateFramebuffers(1, &result.Handle); glCreateFramebuffers(1, &result.Handle);
if (!AttachTextureToFramebuffer(result, texture)) if (!AttachTextureToFramebuffer(result, texture)) {
{
glDeleteFramebuffers(1, &result.Handle); glDeleteFramebuffers(1, &result.Handle);
return {}; return {};
} }
@@ -77,12 +70,10 @@ Framebuffer CreateFramebufferWithTexture(const Texture texture)
return result; return result;
} }
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture) bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture) {
{
glNamedFramebufferTexture(framebuffer.Handle, GL_COLOR_ATTACHMENT0, texture.Handle, 0); glNamedFramebufferTexture(framebuffer.Handle, GL_COLOR_ATTACHMENT0, texture.Handle, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
{
std::cerr << "Framebuffer is not complete!" << std::endl; std::cerr << "Framebuffer is not complete!" << std::endl;
return false; return false;
} }
@@ -91,8 +82,7 @@ bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture)
return true; return true;
} }
void BlitFramebufferToSwapchain(const Framebuffer framebuffer) void BlitFramebufferToSwapchain(const Framebuffer framebuffer) {
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer.Handle); glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer.Handle);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // swapchain glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // swapchain

View File

@@ -6,15 +6,13 @@
#include <filesystem> #include <filesystem>
struct Texture struct Texture {
{
GLuint Handle = 0; GLuint Handle = 0;
uint32_t Width = 0; uint32_t Width = 0;
uint32_t Height = 0; uint32_t Height = 0;
}; };
struct Framebuffer struct Framebuffer {
{
GLuint Handle = 0; GLuint Handle = 0;
Texture ColorAttachment; Texture ColorAttachment;
}; };

View File

@@ -1,17 +1,15 @@
#include "Shader.h" #include "Shader.h"
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
#include <vector> #include <vector>
#include <glad/glad.h> #include <glad/glad.h>
static std::string ReadTextFile(const std::filesystem::path& path) static std::string ReadTextFile(const std::filesystem::path& path) {
{
std::ifstream file(path); std::ifstream file(path);
if (!file.is_open()) if (!file.is_open()) {
{
std::cerr << "Failed to open file: " << path.string() << std::endl; std::cerr << "Failed to open file: " << path.string() << std::endl;
return {}; return {};
} }
@@ -21,8 +19,7 @@ static std::string ReadTextFile(const std::filesystem::path& path)
return contentStream.str(); return contentStream.str();
} }
uint32_t CreateComputeShader(const std::filesystem::path& path) uint32_t CreateComputeShader(const std::filesystem::path& path) {
{
std::string shaderSource = ReadTextFile(path); std::string shaderSource = ReadTextFile(path);
GLuint shaderHandle = glCreateShader(GL_COMPUTE_SHADER); GLuint shaderHandle = glCreateShader(GL_COMPUTE_SHADER);
@@ -34,8 +31,7 @@ uint32_t CreateComputeShader(const std::filesystem::path& path)
GLint isCompiled = 0; GLint isCompiled = 0;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &isCompiled); glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE) if (isCompiled == GL_FALSE) {
{
GLint maxLength = 0; GLint maxLength = 0;
glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &maxLength); glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &maxLength);
@@ -54,8 +50,7 @@ uint32_t CreateComputeShader(const std::filesystem::path& path)
GLint isLinked = 0; GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked); glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE) if (isLinked == GL_FALSE) {
{
GLint maxLength = 0; GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
@@ -74,8 +69,7 @@ uint32_t CreateComputeShader(const std::filesystem::path& path)
return program; return program;
} }
uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path& path) uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path& path) {
{
uint32_t newShaderHandle = CreateComputeShader(path); uint32_t newShaderHandle = CreateComputeShader(path);
// Return old shader if compilation failed // Return old shader if compilation failed
@@ -86,8 +80,7 @@ uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path&
return newShaderHandle; return newShaderHandle;
} }
uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath) uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath) {
{
std::string vertexShaderSource = ReadTextFile(vertexPath); std::string vertexShaderSource = ReadTextFile(vertexPath);
std::string fragmentShaderSource = ReadTextFile(fragmentPath); std::string fragmentShaderSource = ReadTextFile(fragmentPath);
@@ -102,8 +95,7 @@ uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std
GLint isCompiled = 0; GLint isCompiled = 0;
glGetShaderiv(vertexShaderHandle, GL_COMPILE_STATUS, &isCompiled); glGetShaderiv(vertexShaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE) if (isCompiled == GL_FALSE) {
{
GLint maxLength = 0; GLint maxLength = 0;
glGetShaderiv(vertexShaderHandle, GL_INFO_LOG_LENGTH, &maxLength); glGetShaderiv(vertexShaderHandle, GL_INFO_LOG_LENGTH, &maxLength);
@@ -127,8 +119,7 @@ uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std
isCompiled = 0; isCompiled = 0;
glGetShaderiv(fragmentShaderHandle, GL_COMPILE_STATUS, &isCompiled); glGetShaderiv(fragmentShaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE) if (isCompiled == GL_FALSE) {
{
GLint maxLength = 0; GLint maxLength = 0;
glGetShaderiv(fragmentShaderHandle, GL_INFO_LOG_LENGTH, &maxLength); glGetShaderiv(fragmentShaderHandle, GL_INFO_LOG_LENGTH, &maxLength);
@@ -150,8 +141,7 @@ uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std
GLint isLinked = 0; GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked); glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE) if (isLinked == GL_FALSE) {
{
GLint maxLength = 0; GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
@@ -172,8 +162,8 @@ uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std
return program; return program;
} }
uint32_t ReloadGraphicsShader(uint32_t shaderHandle, const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath) uint32_t ReloadGraphicsShader(
{ uint32_t shaderHandle, const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath) {
uint32_t newShaderHandle = CreateGraphicsShader(vertexPath, fragmentPath); uint32_t newShaderHandle = CreateGraphicsShader(vertexPath, fragmentPath);
// Return old shader if compilation failed // Return old shader if compilation failed

View File

@@ -6,4 +6,5 @@ uint32_t CreateComputeShader(const std::filesystem::path& path);
uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path& path); uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path& path);
uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath); uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath);
uint32_t ReloadGraphicsShader(uint32_t shaderHandle, const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath); uint32_t ReloadGraphicsShader(
uint32_t shaderHandle, const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath);