switch to xmake

This commit is contained in:
2025-11-08 19:01:17 +01:00
parent 86a810b65e
commit 86def65569
15 changed files with 48 additions and 8103 deletions

102
src/Main.cpp Normal file
View File

@@ -0,0 +1,102 @@
#include <iostream>
#include <glm/glm.hpp>
#include "Shader.h"
#include "Renderer.h"
static uint32_t s_ComputeShader = -1;
static const std::filesystem::path s_ComputeShaderPath = "Shaders/Compute.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)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_R)
s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath);
}
int main()
{
glfwSetErrorCallback(ErrorCallback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
int width = 1280;
int height = 720;
GLFWwindow* window = glfwCreateWindow(width, height, "Compute", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, KeyCallback);
glfwMakeContextCurrent(window);
gladLoadGL();
glfwSwapInterval(1);
s_ComputeShader = CreateComputeShader(s_ComputeShaderPath);
if (s_ComputeShader == -1)
{
std::cerr << "Compute shader failed\n";
return -1;
}
Texture computeShaderTexture = CreateTexture(width, height);
Framebuffer fb = CreateFramebufferWithTexture(computeShaderTexture);
while (!glfwWindowShouldClose(window))
{
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);
glBindImageTexture(0, fb.ColorAttachment.Handle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
const GLuint workGroupSizeX = 16;
const GLuint workGroupSizeY = 16;
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);
}
// Blit
{
BlitFramebufferToSwapchain(fb);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
}

102
src/Renderer.cpp Normal file
View File

@@ -0,0 +1,102 @@
#include "Renderer.h"
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Texture CreateTexture(int width, int height)
{
Texture result;
result.Width = width;
result.Height = height;
glCreateTextures(GL_TEXTURE_2D, 1, &result.Handle);
glTextureStorage2D(result.Handle, 1, GL_RGBA32F, width, height);
glTextureParameteri(result.Handle, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(result.Handle, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return result;
}
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)
{
std::cerr << "Failed to load texture: " << filepath << "\n";
return {};
}
GLenum format = channels == 4 ? GL_RGBA :
channels == 3 ? GL_RGB :
channels == 1 ? GL_RED : 0;
Texture result;
result.Width = width;
result.Height = height;
glCreateTextures(GL_TEXTURE_2D, 1, &result.Handle);
glTextureStorage2D(result.Handle, 1, (format == GL_RGBA ? GL_RGBA8 : GL_RGB8), width, height);
glTextureSubImage2D(result.Handle, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, data);
glTextureParameteri(result.Handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(result.Handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateTextureMipmap(result.Handle);
stbi_image_free(data);
return result;
}
Framebuffer CreateFramebufferWithTexture(const Texture texture)
{
Framebuffer result;
glCreateFramebuffers(1, &result.Handle);
if (!AttachTextureToFramebuffer(result, texture))
{
glDeleteFramebuffers(1, &result.Handle);
return {};
}
return result;
}
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture)
{
glNamedFramebufferTexture(framebuffer.Handle, GL_COLOR_ATTACHMENT0, texture.Handle, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cerr << "Framebuffer is not complete!" << std::endl;
return false;
}
framebuffer.ColorAttachment = texture;
return true;
}
void BlitFramebufferToSwapchain(const Framebuffer framebuffer)
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer.Handle);
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
GL_COLOR_BUFFER_BIT, GL_NEAREST);
}

26
src/Renderer.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <filesystem>
struct Texture
{
GLuint Handle = 0;
uint32_t Width = 0;
uint32_t Height = 0;
};
struct Framebuffer
{
GLuint Handle = 0;
Texture ColorAttachment;
};
Texture CreateTexture(int width, int height);
Texture LoadTexture(const std::filesystem::path& path);
Framebuffer CreateFramebufferWithTexture(const Texture texture);
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture);
void BlitFramebufferToSwapchain(const Framebuffer framebuffer);

185
src/Shader.cpp Normal file
View File

@@ -0,0 +1,185 @@
#include "Shader.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <glad/glad.h>
static std::string ReadTextFile(const std::filesystem::path& path)
{
std::ifstream file(path);
if (!file.is_open())
{
std::cerr << "Failed to open file: " << path.string() << std::endl;
return {};
}
std::ostringstream contentStream;
contentStream << file.rdbuf();
return contentStream.str();
}
uint32_t CreateComputeShader(const std::filesystem::path& path)
{
std::string shaderSource = ReadTextFile(path);
GLuint shaderHandle = glCreateShader(GL_COMPUTE_SHADER);
const GLchar* source = (const GLchar*)shaderSource.c_str();
glShaderSource(shaderHandle, 1, &source, 0);
glCompileShader(shaderHandle);
GLint isCompiled = 0;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(shaderHandle, maxLength, &maxLength, &infoLog[0]);
std::cerr << infoLog.data() << std::endl;
glDeleteShader(shaderHandle);
return -1;
}
GLuint program = glCreateProgram();
glAttachShader(program, shaderHandle);
glLinkProgram(program);
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
std::cerr << infoLog.data() << std::endl;
glDeleteProgram(program);
glDeleteShader(shaderHandle);
return -1;
}
glDetachShader(program, shaderHandle);
return program;
}
uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path& path)
{
uint32_t newShaderHandle = CreateComputeShader(path);
// Return old shader if compilation failed
if (newShaderHandle == -1)
return shaderHandle;
glDeleteProgram(shaderHandle);
return newShaderHandle;
}
uint32_t CreateGraphicsShader(const std::filesystem::path& vertexPath, const std::filesystem::path& fragmentPath)
{
std::string vertexShaderSource = ReadTextFile(vertexPath);
std::string fragmentShaderSource = ReadTextFile(fragmentPath);
// Vertex shader
GLuint vertexShaderHandle = glCreateShader(GL_VERTEX_SHADER);
const GLchar* source = (const GLchar*)vertexShaderSource.c_str();
glShaderSource(vertexShaderHandle, 1, &source, 0);
glCompileShader(vertexShaderHandle);
GLint isCompiled = 0;
glGetShaderiv(vertexShaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(vertexShaderHandle, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(vertexShaderHandle, maxLength, &maxLength, &infoLog[0]);
std::cerr << infoLog.data() << std::endl;
glDeleteShader(vertexShaderHandle);
return -1;
}
// Fragment shader
GLuint fragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
source = (const GLchar*)fragmentShaderSource.c_str();
glShaderSource(fragmentShaderHandle, 1, &source, 0);
glCompileShader(fragmentShaderHandle);
isCompiled = 0;
glGetShaderiv(fragmentShaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(fragmentShaderHandle, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(fragmentShaderHandle, maxLength, &maxLength, &infoLog[0]);
std::cerr << infoLog.data() << std::endl;
glDeleteShader(fragmentShaderHandle);
return -1;
}
// Program linking
GLuint program = glCreateProgram();
glAttachShader(program, vertexShaderHandle);
glAttachShader(program, fragmentShaderHandle);
glLinkProgram(program);
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
std::cerr << infoLog.data() << std::endl;
glDeleteProgram(program);
glDeleteShader(vertexShaderHandle);
glDeleteShader(fragmentShaderHandle);
return -1;
}
glDetachShader(program, vertexShaderHandle);
glDetachShader(program, fragmentShaderHandle);
return program;
}
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
if (newShaderHandle == -1)
return shaderHandle;
glDeleteProgram(shaderHandle);
return newShaderHandle;
}

9
src/Shader.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <filesystem>
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);