generated from Persson-dev/OpenGLComputeShader
big refactor
This commit is contained in:
176
src/Main.cpp
176
src/Main.cpp
@@ -3,36 +3,32 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "Shader.h"
|
||||
#include "Renderer.h"
|
||||
#include "Shader.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
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; }
|
||||
constexpr int PARTICLE_COUNT = 64 * 5000;
|
||||
|
||||
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";
|
||||
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:
|
||||
Timer m_Timer;
|
||||
std::string m_Name;
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> m_Start;
|
||||
};
|
||||
|
||||
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_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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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};
|
||||
static GLuint CreateDummyVAO() {
|
||||
GLuint vertexArray;
|
||||
glCreateVertexArrays(1, &vertexArray);
|
||||
|
||||
GLuint vertexBuffer;
|
||||
glCreateBuffers(1, &vertexBuffer);
|
||||
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
static void CreateGpuBuffer(std::size_t a_Size) {
|
||||
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) * a_Size, nullptr,
|
||||
GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
static GLFWwindow* InitWindow() {
|
||||
glfwSetErrorCallback(ErrorCallback);
|
||||
|
||||
if (!glfwInit())
|
||||
@@ -103,9 +93,8 @@ int main()
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "Compute", NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
GLFWwindow* window = glfwCreateWindow(width, height, "", NULL, NULL);
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -117,61 +106,37 @@ int main()
|
||||
glfwSwapInterval(1);
|
||||
|
||||
s_ComputeShader = CreateComputeShader(s_ComputeShaderPath);
|
||||
if (s_ComputeShader == -1)
|
||||
{
|
||||
if (s_ComputeShader == -1) {
|
||||
std::cerr << "Compute shader failed\n";
|
||||
return -1;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
s_GraphicsShader = CreateGraphicsShader(s_VertexShaderPath, s_FragmentShaderPath);
|
||||
if (s_GraphicsShader == -1)
|
||||
{
|
||||
if (s_GraphicsShader == -1) {
|
||||
std::cerr << "Graphics shader failed\n";
|
||||
return -1;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Texture computeShaderTexture = CreateTexture(width, height);
|
||||
Framebuffer fb = CreateFramebufferWithTexture(computeShaderTexture);
|
||||
return window;
|
||||
}
|
||||
|
||||
GLuint vertexArray;
|
||||
glCreateVertexArrays(1, &vertexArray);
|
||||
int main() {
|
||||
GLFWwindow* window = InitWindow();
|
||||
if (!window)
|
||||
return -1;
|
||||
|
||||
GLuint vertexBuffer;
|
||||
glCreateBuffers(1, &vertexBuffer);
|
||||
auto vertexArray = CreateDummyVAO();
|
||||
|
||||
constexpr int DEPTH = 6;
|
||||
|
||||
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.
|
||||
CreateGpuBuffer(PARTICLE_COUNT);
|
||||
|
||||
float lastTime = (float)glfwGetTime();
|
||||
|
||||
int fps = 0;
|
||||
float secondsTimer = 0.0f;
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glBindVertexArray(vertexArray);
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// ScopedTimer timer("Main Loop");
|
||||
|
||||
float currentTime = (float)glfwGetTime();
|
||||
@@ -179,8 +144,7 @@ int main()
|
||||
lastTime = currentTime;
|
||||
|
||||
secondsTimer += dt;
|
||||
if (secondsTimer >= 1.0f)
|
||||
{
|
||||
if (secondsTimer >= 1.0f) {
|
||||
std::string title = "FPS : " + std::to_string(fps);
|
||||
glfwSetWindowTitle(window, title.c_str());
|
||||
|
||||
@@ -188,18 +152,7 @@ int main()
|
||||
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
|
||||
|
||||
glUseProgram(s_ComputeShader);
|
||||
glDispatchCompute(PARTICLE_COUNT / 64, 1, 1);
|
||||
|
||||
@@ -209,7 +162,6 @@ int main()
|
||||
// Graphics
|
||||
glUseProgram(s_GraphicsShader);
|
||||
|
||||
glBindVertexArray(vertexArray);
|
||||
glDrawArraysInstanced(GL_POINTS, 0, 1, PARTICLE_COUNT);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
Reference in New Issue
Block a user