working project

This commit is contained in:
2025-11-08 21:30:49 +01:00
parent 86def65569
commit 9f40a59612
4 changed files with 211 additions and 43 deletions

View File

@@ -1,25 +1,93 @@
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "Renderer.h"
static uint32_t s_ComputeShader = -1;
static const std::filesystem::path s_ComputeShaderPath = "Shaders/Compute.glsl";
#include <chrono>
static void ErrorCallback(int error, const char* description)
class Timer
{
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:
std::chrono::time_point<std::chrono::high_resolution_clock> m_Start;
};
class ScopedTimer
{
public:
ScopedTimer(std::string_view name) : m_Name(name) {}
~ScopedTimer()
{
float time = m_Timer.ElapsedMillis();
std::cout << m_Name << " - " << time << "ms\n";
}
private:
Timer m_Timer;
std::string m_Name;
};
static uint32_t s_ComputeShader = -1;
static uint32_t s_GraphicsShader = -1;
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_FragmentShaderPath = "Shaders/Fragment.glsl";
static void ErrorCallback(int error, const char *description)
{
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)
return;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_R)
{
s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath);
s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath);
}
}
std::vector<float> GenPoints(float x, float y)
{
x /= 2;
y /= 2;
return {
x, y + 0.36f,
x - 0.5f, y - 0.5f,
x + 0.5f, y - 0.5f};
}
std::vector<float> GetPoints(int depth)
{
std::vector<float> result = {0.0f, 0.0f};
std::size_t offset = 0;
std::size_t count = 1;
for (std::size_t i = 1; i < depth; i++)
{
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()
@@ -35,7 +103,7 @@ int main()
int width = 1280;
int height = 720;
GLFWwindow* window = glfwCreateWindow(width, height, "Compute", NULL, NULL);
GLFWwindow *window = glfwCreateWindow(width, height, "Compute", NULL, NULL);
if (!window)
{
glfwTerminate();
@@ -55,11 +123,82 @@ int main()
return -1;
}
s_GraphicsShader = CreateGraphicsShader(s_VertexShaderPath, s_FragmentShaderPath);
if (s_GraphicsShader == -1)
{
std::cerr << "Graphics shader failed\n";
return -1;
}
Texture computeShaderTexture = CreateTexture(width, height);
Framebuffer fb = CreateFramebufferWithTexture(computeShaderTexture);
GLuint vertexArray;
glCreateVertexArrays(1, &vertexArray);
GLuint vertexBuffer;
glCreateBuffers(1, &vertexBuffer);
constexpr int DEPTH = 13;
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);
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) * vertices.size(), nullptr, GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
float lastTime = (float)glfwGetTime();
int fps = 0;
float secondsTimer = 0.0f;
// Compute
glUseProgram(s_ComputeShader);
glBindImageTexture(0, fb.ColorAttachment.Handle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
std::size_t count = 1;
for (std::size_t i = 0; i < DEPTH; i++)
{
glDispatchCompute(count, 1, 1);
// Ensure all writes to the image are complete
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
count *= 3;
}
while (!glfwWindowShouldClose(window))
{
// ScopedTimer timer("Main Loop");
float currentTime = (float)glfwGetTime();
float dt = currentTime - lastTime;
lastTime = currentTime;
secondsTimer += dt;
if (secondsTimer >= 1.0f)
{
std::string title = "FPS : " + std::to_string(fps);
glfwSetWindowTitle(window, title.c_str());
secondsTimer = 0.0f;
fps = 0;
}
glfwGetFramebufferSize(window, &width, &height);
// Resize texture
@@ -70,22 +209,14 @@ int main()
AttachTextureToFramebuffer(fb, computeShaderTexture);
}
// Compute
{
glUseProgram(s_ComputeShader);
glBindImageTexture(0, fb.ColorAttachment.Handle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
// Graphics
glBindFramebuffer(GL_FRAMEBUFFER, fb.Handle);
glUseProgram(s_GraphicsShader);
const GLuint workGroupSizeX = 16;
const GLuint workGroupSizeY = 16;
glBindVertexArray(vertexArray);
glDrawArraysInstanced(GL_POINTS, 0, 1, vertices.size());
GLuint numGroupsX = (width + workGroupSizeX - 1) / workGroupSizeX;
GLuint numGroupsY = (height + workGroupSizeY - 1) / workGroupSizeY;
glDispatchCompute(numGroupsX, numGroupsY, 1);
// Ensure all writes to the image are complete
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Blit
{
@@ -94,6 +225,11 @@ int main()
glfwSwapBuffers(window);
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
fps++;
}
glfwDestroyWindow(window);