Compare commits

...

11 Commits

Author SHA1 Message Date
b5f6e81a7b resize viewport 2025-11-09 13:27:56 +01:00
1be8e337a3 matrix values cpu side 2025-11-09 12:13:08 +01:00
6a874a01bb now in 3D 2025-11-09 11:59:46 +01:00
b54bb6a136 matrix multiplcation 2025-11-09 11:19:22 +01:00
ba6a342c94 real time 2025-11-09 11:09:04 +01:00
9c4894eda2 fix shader buffer size 2025-11-09 11:06:44 +01:00
e7edf0cf6f fix gitignore 2025-11-09 11:06:18 +01:00
60e0b29d07 big refactor 2025-11-09 00:11:12 +01:00
ba02b9e3ed chaos game 2025-11-08 23:55:05 +01:00
db1bbd6d0a optimize dispatch calls 2025-11-08 22:23:22 +01:00
9f40a59612 working project 2025-11-08 21:30:49 +01:00
10 changed files with 287 additions and 118 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

2
.gitignore vendored
View File

@@ -3,7 +3,7 @@
*.user
# VsCode
.vsCode
.vscode
# Xmake
.xmake

View File

@@ -1,32 +1,42 @@
#version 460 core
layout(rgba32f, binding = 0) uniform writeonly image2D outputImage;
layout(std430, binding = 3) buffer layoutName {
float o_Points[];
};
layout(local_size_x = 16, local_size_y = 16) in;
void main()
const uint transformationCount = 3;
layout(location = 1) uniform mat4 transformations[transformationCount];
layout(local_size_x = 64) in;
highp float rand(vec2 co)
{
ivec2 pixelCoord = ivec2(gl_GlobalInvocationID.xy);
if (pixelCoord.x >= imageSize(outputImage).x || pixelCoord.y >= imageSize(outputImage).y)
return;
ivec2 texSize = imageSize(outputImage);
vec2 fTexSize = vec2(texSize);
vec2 normalizedCoord = vec2(pixelCoord) / vec2(texSize);
vec4 O = vec4(0, 0, 0, 1);
vec2 I = vec2(pixelCoord);
float iTime = 2.2;
float i = 0.0, t=iTime;
O *= i;
for(vec2 a=fTexSize.xy, p=(I+I-a)/a.y; i++<20.;
O += (cos(sin(i*.2+t)*vec4(0,4,3,1))+2.)
/(i/1e3+abs(length(a-.5*min(a+a.yx,.1))-.05)))
a.x = abs(a = (fract(.2*t+.3*p*i*mat2(cos(cos(.2*t+.2*i)+vec4(0,11,33,0))))-.5)).x;
O = tanh(O*O/2e5);
vec4 color = vec4(normalizedCoord, 0.0, 1.0);
imageStore(outputImage, pixelCoord, O);
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);
}
vec3 unpack(uint index) {
return vec3(o_Points[index * 3], o_Points[index * 3 + 1], o_Points[index * 3 + 2]);
}
void store(vec3 vect, uint index) {
o_Points[index * 3] = vect.x;
o_Points[index * 3 + 1] = vect.y;
o_Points[index * 3 + 2] = vect.z;
}
void main() {
uint currentIndex = gl_GlobalInvocationID.x;
vec3 currentPoint = unpack(currentIndex);
uint index = uint(rand(currentPoint.xy + currentPoint.z + currentIndex) * 69);
mat4 transformation = transformations[index % transformationCount];
vec3 result = (transformation * vec4(currentPoint, 1.0)).xyz;
store(result, currentIndex);
}

8
Shaders/Fragment.glsl Normal file
View File

@@ -0,0 +1,8 @@
#version 460 core
layout (location = 0) out vec4 o_Color;
void main()
{
o_Color = vec4(1, 1, 1, 1);
}

15
Shaders/Vertex.glsl Normal file
View File

@@ -0,0 +1,15 @@
#version 460 core
layout(std430, binding = 3) buffer layoutName
{
float data_SSBO[];
};
vec3 unpack(uint index) {
return vec3(data_SSBO[index * 3], data_SSBO[index * 3 + 1], data_SSBO[index * 3 + 2]);
}
void main()
{
gl_Position = vec4(unpack(gl_InstanceID), 1.0);
}

View File

@@ -1,29 +1,91 @@
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "Renderer.h"
#include "Shader.h"
#include <chrono>
constexpr int WORK_GROUP_SIZE = 64;
constexpr int PARTICLE_COUNT = WORK_GROUP_SIZE * 10000;
constexpr int SWAP_INTERVAL = 1;
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;
};
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)
{
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);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * PARTICLE_COUNT * 3, nullptr, GL_DYNAMIC_COPY);
}
}
int main()
{
static GLuint CreateDummyVAO() {
GLuint vertexArray;
glCreateVertexArrays(1, &vertexArray);
GLuint vertexBuffer;
glCreateBuffers(1, &vertexBuffer);
// 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;
}
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 GLFWwindow* InitWindow() {
glfwSetErrorCallback(ErrorCallback);
if (!glfwInit())
@@ -35,9 +97,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);
}
@@ -46,54 +107,113 @@ int main()
glfwMakeContextCurrent(window);
gladLoadGL();
glfwSwapInterval(1);
glfwSwapInterval(SWAP_INTERVAL);
s_ComputeShader = CreateComputeShader(s_ComputeShaderPath);
if (s_ComputeShader == -1)
{
if (s_ComputeShader == -1) {
std::cerr << "Compute shader failed\n";
return -1;
return nullptr;
}
Texture computeShaderTexture = CreateTexture(width, height);
Framebuffer fb = CreateFramebufferWithTexture(computeShaderTexture);
s_GraphicsShader = CreateGraphicsShader(s_VertexShaderPath, s_FragmentShaderPath);
if (s_GraphicsShader == -1) {
std::cerr << "Graphics shader failed\n";
return nullptr;
}
while (!glfwWindowShouldClose(window))
{
glfwGetFramebufferSize(window, &width, &height);
return window;
}
// Resize texture
if (width != computeShaderTexture.Width || height != computeShaderTexture.Height)
int main() {
GLFWwindow* window = InitWindow();
if (!window)
return -1;
auto vertexArray = CreateDummyVAO();
CreateGpuBuffer();
float lastTime = (float)glfwGetTime();
int fps = 0;
float secondsTimer = 0.0f;
glBindVertexArray(vertexArray);
std::vector<glm::mat4> transformations = {
{
glDeleteTextures(1, &computeShaderTexture.Handle);
computeShaderTexture = CreateTexture(width, height);
AttachTextureToFramebuffer(fb, computeShaderTexture);
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.36f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
},
{
0.5f, 0.0f, 0.0f, -0.5f,
0.0f, 0.5f, 0.0f, -0.5f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
},
{
0.5f, 0.0f, 0.0f, 0.5f,
0.0f, 0.5f, 0.0f, -0.5f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
},
};
glUseProgram(s_ComputeShader);
glUniformMatrix4fv(1, transformations.size(), true, glm::value_ptr(transformations[0]));
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;
}
// Compute
{
glUseProgram(s_ComputeShader);
glBindImageTexture(0, fb.ColorAttachment.Handle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glUseProgram(s_ComputeShader);
glDispatchCompute(PARTICLE_COUNT / WORK_GROUP_SIZE, 1, 1);
const GLuint workGroupSizeX = 16;
const GLuint workGroupSizeY = 16;
// Ensure all writes to the image are complete
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
GLuint numGroupsX = (width + workGroupSizeX - 1) / workGroupSizeX;
GLuint numGroupsY = (height + workGroupSizeY - 1) / workGroupSizeY;
// Graphics
glUseProgram(s_GraphicsShader);
glDispatchCompute(numGroupsX, numGroupsY, 1);
// Ensure all writes to the image are complete
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
}
// Blit
{
BlitFramebufferToSwapchain(fb);
}
glDrawArraysInstanced(GL_POINTS, 0, 1, PARTICLE_COUNT);
glfwSwapBuffers(window);
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
int width, height;
glfwGetWindowSize(window, &width, &height);
glViewport(0, 0, width, height);
// float positions[3 * PARTICLE_COUNT];
// glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(float) * 3 * PARTICLE_COUNT, positions);
// std::cout << "Positions :\n";
// for (size_t i = 0; i < PARTICLE_COUNT; i++)
// {
// std::cout << "\t" << positions[i * 3] << " " << positions[i * 3 + 1] << " " << positions[i * 3 + 2] << "\n";
// }
fps++;
}
glfwDestroyWindow(window);

View File

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

View File

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

View File

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