generated from Persson-dev/OpenGLComputeShader
Compare commits
21 Commits
main
...
a46abca707
| Author | SHA1 | Date | |
|---|---|---|---|
|
a46abca707
|
|||
|
ee93146167
|
|||
|
e940b670d2
|
|||
|
9e6088f9bf
|
|||
|
da536b8d7d
|
|||
|
abb33b6f1f
|
|||
|
10b0b54e71
|
|||
|
0eebe09d47
|
|||
|
78088b2067
|
|||
|
bd81c632de
|
|||
|
b5f6e81a7b
|
|||
|
1be8e337a3
|
|||
|
6a874a01bb
|
|||
|
b54bb6a136
|
|||
|
ba6a342c94
|
|||
|
9c4894eda2
|
|||
|
e7edf0cf6f
|
|||
|
60e0b29d07
|
|||
|
ba02b9e3ed
|
|||
|
db1bbd6d0a
|
|||
|
9f40a59612
|
16
.vscode/compile_commands.json
vendored
Normal file
16
.vscode/compile_commands.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
[
|
||||
{
|
||||
"directory": "/home/simon/Programmation/GPUCompute",
|
||||
"arguments": ["/usr/bin/g++", "-c", "-m64", "-isystem", "/usr/include/X11/dri", "-isystem", "/home/simon/.xmake/packages/g/glad/v0.1.36/9e3539cb78a3425b96246554411a0b79/include", "-isystem", "/home/simon/.xmake/packages/g/glm/1.0.2/f8fba32531124eb38c4833bdc558de88/include", "-isystem", "/home/simon/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-isystem", "/home/simon/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/App/linux/x86_64/release/src/Shader.cpp.o", "src/Shader.cpp"],
|
||||
"file": "src/Shader.cpp"
|
||||
},
|
||||
{
|
||||
"directory": "/home/simon/Programmation/GPUCompute",
|
||||
"arguments": ["/usr/bin/g++", "-c", "-m64", "-isystem", "/usr/include/X11/dri", "-isystem", "/home/simon/.xmake/packages/g/glad/v0.1.36/9e3539cb78a3425b96246554411a0b79/include", "-isystem", "/home/simon/.xmake/packages/g/glm/1.0.2/f8fba32531124eb38c4833bdc558de88/include", "-isystem", "/home/simon/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-isystem", "/home/simon/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/App/linux/x86_64/release/src/Renderer.cpp.o", "src/Renderer.cpp"],
|
||||
"file": "src/Renderer.cpp"
|
||||
},
|
||||
{
|
||||
"directory": "/home/simon/Programmation/GPUCompute",
|
||||
"arguments": ["/usr/bin/g++", "-c", "-m64", "-isystem", "/usr/include/X11/dri", "-isystem", "/home/simon/.xmake/packages/g/glad/v0.1.36/9e3539cb78a3425b96246554411a0b79/include", "-isystem", "/home/simon/.xmake/packages/g/glm/1.0.2/f8fba32531124eb38c4833bdc558de88/include", "-isystem", "/home/simon/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-isystem", "/home/simon/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/App/linux/x86_64/release/src/Main.cpp.o", "src/Main.cpp"],
|
||||
"file": "src/Main.cpp"
|
||||
}]
|
||||
@@ -1,5 +1,5 @@
|
||||
# Real Time Fractal
|
||||
Generate random fractals in 3D using chaos game.
|
||||
# GPU Compute
|
||||
Simple app to run compute shaders using OpenGL.
|
||||
|
||||
## Building
|
||||
Just run xmake and build. Here's an example:
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
#version 460 core
|
||||
|
||||
layout(std430, binding = 3) buffer layoutName
|
||||
{
|
||||
float data_SSBO[];
|
||||
};
|
||||
|
||||
layout(location = 0) uniform mat4 viewMatrix;
|
||||
layout(location = 1) uniform mat4 projectionMatrix;
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
vec3 unpack(uint index) {
|
||||
return vec3(data_SSBO[index * 3], data_SSBO[index * 3 + 1], data_SSBO[index * 3 + 2]);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(unpack(gl_InstanceID), 1.0);
|
||||
}
|
||||
36
src/Main.cpp
36
src/Main.cpp
@@ -17,7 +17,7 @@ constexpr int SWAP_INTERVAL = 1;
|
||||
constexpr int TRANSFORMATION_COUNT = 3;
|
||||
|
||||
constexpr float ANIMATION_TIME = 2;
|
||||
constexpr float ANIMATION_STILL_TIME = .5f;
|
||||
constexpr float ANIMATION_STILL_TIME = 1;
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
@@ -169,22 +169,31 @@ static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, i
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static std::tuple<GLuint, GLuint> CreateVAO() {
|
||||
static GLuint CreateDummyVAO() {
|
||||
GLuint vertexArray;
|
||||
glCreateVertexArrays(1, &vertexArray);
|
||||
|
||||
GLuint vertexBuffer;
|
||||
glCreateBuffers(1, &vertexBuffer);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * PARTICLE_COUNT * 3, nullptr, GL_DYNAMIC_DRAW);
|
||||
// Buffer with just one point
|
||||
float vertices[] = {0.0f, 0.0f, 0.0f};
|
||||
glNamedBufferData(vertexBuffer, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glVertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, 0, sizeof(float) * 3);
|
||||
glEnableVertexArrayAttrib(vertexArray, 0);
|
||||
glVertexArrayAttribFormat(vertexArray, 0, 3, GL_FLOAT, GL_FALSE, 0);
|
||||
glVertexArrayAttribBinding(vertexArray, 0, 0);
|
||||
|
||||
return {vertexArray, vertexBuffer};
|
||||
return vertexArray;
|
||||
}
|
||||
|
||||
static void CreateGpuBuffer() {
|
||||
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) * 3 * PARTICLE_COUNT, nullptr,
|
||||
GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
|
||||
}
|
||||
|
||||
static std::vector<glm::mat4> GetTransformMatrixBlended(float dt) {
|
||||
@@ -240,7 +249,7 @@ static GLFWwindow* InitWindow() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto viewMatrix = glm::lookAt(glm::vec3{1, 1.5, 1.5}, {0, 0, 0}, {0, 1, 0});
|
||||
auto viewMatrix = glm::lookAt(glm::vec3{1, 2, 2}, {0, 0, 0}, {0, 1, 0});
|
||||
auto projectionMatrix = glm::perspective(70.0f, 2.0f, 0.01f, 10.0f);
|
||||
|
||||
glUseProgram(s_GraphicsShader);
|
||||
@@ -255,7 +264,9 @@ int main() {
|
||||
if (!window)
|
||||
return -1;
|
||||
|
||||
auto [vertexArray, vertexBuffer] = CreateVAO();
|
||||
auto vertexArray = CreateDummyVAO();
|
||||
|
||||
CreateGpuBuffer();
|
||||
|
||||
float lastTime = (float)glfwGetTime();
|
||||
|
||||
@@ -263,17 +274,14 @@ int main() {
|
||||
float secondsTimer = 0.0f;
|
||||
float animationTimer = 0.0f;
|
||||
|
||||
glBindVertexArray(vertexArray);
|
||||
|
||||
// ApplyTransforms(SIERPINSKI_TRIANGLE);
|
||||
s_T1 = GenRandomFractal();
|
||||
s_T2 = GenRandomFractal();
|
||||
|
||||
glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
|
||||
|
||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vertexBuffer);
|
||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, vertexBuffer);
|
||||
|
||||
glBindVertexArray(vertexArray);
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// ScopedTimer timer("Main Loop");
|
||||
|
||||
@@ -310,7 +318,7 @@ int main() {
|
||||
// Graphics
|
||||
glUseProgram(s_GraphicsShader);
|
||||
|
||||
glDrawArrays(GL_POINTS, 0, PARTICLE_COUNT);
|
||||
glDrawArraysInstanced(GL_POINTS, 0, 1, PARTICLE_COUNT);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
|
||||
Reference in New Issue
Block a user