Initial commit
This commit is contained in:
102
App/Source/Main.cpp
Normal file
102
App/Source/Main.cpp
Normal 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(glfwGetProcAddress);
|
||||
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();
|
||||
}
|
||||
61
App/Source/Renderer.cpp
Normal file
61
App/Source/Renderer.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "Renderer.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
23
App/Source/Renderer.h
Normal file
23
App/Source/Renderer.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <glad/gl.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
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);
|
||||
Framebuffer CreateFramebufferWithTexture(const Texture texture);
|
||||
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture);
|
||||
void BlitFramebufferToSwapchain(const Framebuffer framebuffer);
|
||||
81
App/Source/Shader.cpp
Normal file
81
App/Source/Shader.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "Shader.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
uint32_t CreateComputeShader(const std::filesystem::path& path)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
|
||||
if (!file.is_open())
|
||||
{
|
||||
std::cerr << "Failed to open file: " << path.string() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ostringstream contentStream;
|
||||
contentStream << file.rdbuf();
|
||||
std::string shaderSource = contentStream.str();
|
||||
|
||||
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;
|
||||
}
|
||||
6
App/Source/Shader.h
Normal file
6
App/Source/Shader.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
uint32_t CreateComputeShader(const std::filesystem::path& path);
|
||||
uint32_t ReloadComputeShader(uint32_t shaderHandle, const std::filesystem::path& path);
|
||||
Reference in New Issue
Block a user