From ba02b9e3ed1b29f38b89db903c04b4d8ea08bc7d Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 8 Nov 2025 23:55:05 +0100 Subject: [PATCH] chaos game --- Shaders/Compute.glsl | 60 ++++++++++++++++---------------------------- src/Main.cpp | 47 ++++++++++------------------------ 2 files changed, 35 insertions(+), 72 deletions(-) diff --git a/Shaders/Compute.glsl b/Shaders/Compute.glsl index 21343d1..7b41fe4 100644 --- a/Shaders/Compute.glsl +++ b/Shaders/Compute.glsl @@ -1,46 +1,30 @@ #version 460 core -layout(rgba32f, binding = 0) uniform writeonly image2D outputImage; - -layout(location = 0) uniform uint triangleCount; - -layout(std430, binding = 3) buffer layoutName -{ +layout(std430, binding = 3) buffer layoutName { vec2 o_Points[]; }; -// 1 -> 1 -// 2 -> 3 -// 3 -> 9 -// 4 -> 27 -// 5 -> 81 -// 6 -> 243 -// ... - -// 4 27 -// 1 + 3 + 9 = 13 - -// Sn = ((3^n)-1)/2 -// S3 = 3^3 - 1 / 2 -// 3^4 - -// gl_NumWorkGroups : This variable contains the number of work groups passed to the dispatch function. -// gl_WorkGroupID : This is the current work group for this shader invocation. Each of the XYZ components will be on the half-open range [0, gl_NumWorkGroups.XYZ). -// gl_LocalInvocationID : This is the current invocation of the shader within the work group. Each of the XYZ components will be on the half-open range [0, gl_WorkGroupSize.XYZ). - -// avant : 1 3 9 -// après : 3 9 27 -// count : 1 3 9 layout(local_size_x = 64) in; -void main() -{ - uint currentPoint = gl_GlobalInvocationID.x; - if (currentPoint >= triangleCount) - return; - uint offset = (triangleCount - 1) / 2; - uint firstPointIndex = offset * 3 + 1; - o_Points[firstPointIndex + currentPoint * 3] = o_Points[offset + currentPoint] / 2 + vec2(0, 0.36); - o_Points[firstPointIndex + currentPoint * 3 + 1] = o_Points[offset + currentPoint] / 2 + vec2(-0.5, -0.5); - o_Points[firstPointIndex + currentPoint * 3 + 2] = o_Points[offset + currentPoint] / 2 + vec2(0.5, -0.5); +highp float rand(vec2 co) +{ + highp float a = 12.9898; + highp float b = 78.233; + highp float c = 43758.5453; + highp float dt= dot(co.xy ,vec2(a,b)); + highp float sn= mod(dt,3.14); + return fract(sin(sn) * c); +} + +void main() { + const uint total = gl_NumWorkGroups.x * gl_WorkGroupSize.x; + uint currentPoint = gl_GlobalInvocationID.x; + uint index = uint(rand(o_Points[currentPoint] + currentPoint) * 69); + if (index % 3 == 0) { + o_Points[currentPoint] = o_Points[currentPoint] / 2 + vec2(0, 0.36); + } else if (index % 3 == 1) { + o_Points[currentPoint] = o_Points[currentPoint] / 2 + vec2(-0.5, -0.5); + } else { + o_Points[currentPoint] = o_Points[currentPoint] / 2 + vec2(0.5, -0.5); + } } \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 03a22ad..c738ddc 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -139,7 +139,7 @@ int main() GLuint vertexBuffer; glCreateBuffers(1, &vertexBuffer); - constexpr int DEPTH = 14; + constexpr int DEPTH = 6; auto vertices = GetPoints(DEPTH); @@ -157,40 +157,19 @@ int main() 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) * vertices.size(), nullptr, GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays. + 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(); 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++) - { - glUniform1ui(0, count); - std::cout << count << std::endl; - - int dispatchCount = (count - 1) / 64 + 1; - std::cout << "DC : " <