generated from Persson-dev/OpenGLComputeShader
fractal #1
37
.clang-format
Normal file
37
.clang-format
Normal 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
2
.gitignore
vendored
@@ -3,7 +3,7 @@
|
||||
*.user
|
||||
|
||||
# VsCode
|
||||
.vsCode
|
||||
.vscode
|
||||
|
||||
# Xmake
|
||||
.xmake
|
||||
|
||||
@@ -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);
|
||||
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);
|
||||
}
|
||||
|
||||
if (pixelCoord.x >= imageSize(outputImage).x || pixelCoord.y >= imageSize(outputImage).y)
|
||||
return;
|
||||
vec3 unpack(uint index) {
|
||||
return vec3(o_Points[index * 3], o_Points[index * 3 + 1], o_Points[index * 3 + 2]);
|
||||
}
|
||||
|
||||
ivec2 texSize = imageSize(outputImage);
|
||||
vec2 fTexSize = vec2(texSize);
|
||||
vec2 normalizedCoord = vec2(pixelCoord) / vec2(texSize);
|
||||
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;
|
||||
}
|
||||
|
||||
vec4 O = vec4(0, 0, 0, 1);
|
||||
vec2 I = vec2(pixelCoord);
|
||||
void main() {
|
||||
uint currentIndex = gl_GlobalInvocationID.x;
|
||||
vec3 currentPoint = unpack(currentIndex);
|
||||
|
||||
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);
|
||||
uint index = uint(rand(currentPoint.xy + currentPoint.z + currentIndex) * 69);
|
||||
mat4 transformation = transformations[index % transformationCount];
|
||||
|
||||
vec4 color = vec4(normalizedCoord, 0.0, 1.0);
|
||||
imageStore(outputImage, pixelCoord, O);
|
||||
vec3 result = (transformation * vec4(currentPoint, 1.0)).xyz;
|
||||
store(result, currentIndex);
|
||||
}
|
||||
8
Shaders/Fragment.glsl
Normal file
8
Shaders/Fragment.glsl
Normal file
@@ -0,0 +1,8 @@
|
||||
#version 460 core
|
||||
|
||||
layout (location = 0) out vec4 o_Color;
|
||||
|
||||
void main()
|
||||
{
|
||||
o_Color = vec4(1, 1, 1, 1);
|
||||
}
|
||||
18
Shaders/Vertex.glsl
Normal file
18
Shaders/Vertex.glsl
Normal file
@@ -0,0 +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;
|
||||
|
||||
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(unpack(gl_InstanceID), 1.0);
|
||||
}
|
||||
329
src/Main.cpp
329
src/Main.cpp
@@ -1,29 +1,219 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "Shader.h"
|
||||
#include "Renderer.h"
|
||||
#include "Shader.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
constexpr int WORK_GROUP_SIZE = 64;
|
||||
constexpr int PARTICLE_COUNT = WORK_GROUP_SIZE * 15625;
|
||||
|
||||
constexpr int SWAP_INTERVAL = 1;
|
||||
|
||||
constexpr int TRANSFORMATION_COUNT = 3;
|
||||
|
||||
constexpr float ANIMATION_TIME = 2;
|
||||
constexpr float ANIMATION_STILL_TIME = 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 std::random_device s_RandomDevice;
|
||||
static std::mt19937 s_Generator(s_RandomDevice());
|
||||
static std::uniform_real_distribution<float> s_Distrib(0, 1);
|
||||
|
||||
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 ApplyTransforms(const std::vector<glm::mat4>& transformations) {
|
||||
glUseProgram(s_ComputeShader);
|
||||
glUniformMatrix4fv(1, transformations.size(), false, glm::value_ptr(transformations[0]));
|
||||
}
|
||||
|
||||
struct Transform {
|
||||
float m_ScaleX;
|
||||
float m_ScaleY;
|
||||
float m_ScaleZ;
|
||||
|
||||
float m_RotationX;
|
||||
float m_RotationY;
|
||||
float m_RotationZ;
|
||||
|
||||
float m_ShearXY;
|
||||
float m_ShearXZ;
|
||||
float m_ShearYX;
|
||||
float m_ShearYZ;
|
||||
float m_ShearZX;
|
||||
float m_ShearZY;
|
||||
|
||||
float m_TranslateX;
|
||||
float m_TranslateY;
|
||||
float m_TranslateZ;
|
||||
|
||||
glm::mat4 ToMatrix() {
|
||||
auto scale = glm::scale(glm::mat4(1), {m_ScaleX, m_ScaleY, m_ScaleZ});
|
||||
auto rotateX = glm::rotate(scale, m_RotationX, {1, 0, 0});
|
||||
auto rotateY = glm::rotate(rotateX, m_RotationY, {0, 1, 0});
|
||||
auto rotateZ = glm::rotate(rotateY, m_RotationZ, {0, 0, 1});
|
||||
auto shear = glm::shear(rotateZ, {0, 0, 0}, {m_ShearXY, m_ShearXZ}, {m_ShearYX, m_ShearYZ}, {m_ShearZX, m_ShearZY});
|
||||
auto translate = glm::translate(rotateZ, {m_TranslateX, m_TranslateY, m_TranslateZ});
|
||||
|
||||
return translate;
|
||||
}
|
||||
|
||||
Transform Blend(const Transform& other, const std::function<float(float, float, float)>& a_Lerp, float dt) {
|
||||
return {
|
||||
a_Lerp(m_ScaleX, other.m_ScaleX, dt),
|
||||
a_Lerp(m_ScaleY, other.m_ScaleY, dt),
|
||||
a_Lerp(m_ScaleZ, other.m_ScaleZ, dt),
|
||||
a_Lerp(m_RotationX, other.m_RotationX, dt),
|
||||
a_Lerp(m_RotationY, other.m_RotationY, dt),
|
||||
a_Lerp(m_RotationZ, other.m_RotationZ, dt),
|
||||
a_Lerp(m_ShearXY, other.m_ShearXY, dt),
|
||||
a_Lerp(m_ShearXZ, other.m_ShearXZ, dt),
|
||||
a_Lerp(m_ShearYX, other.m_ShearYX, dt),
|
||||
a_Lerp(m_ShearYZ, other.m_ShearYZ, dt),
|
||||
a_Lerp(m_ShearZX, other.m_ShearZX, dt),
|
||||
a_Lerp(m_ShearZY, other.m_ShearZY, dt),
|
||||
a_Lerp(m_TranslateX, other.m_TranslateX, dt),
|
||||
a_Lerp(m_TranslateY, other.m_TranslateY, dt),
|
||||
a_Lerp(m_TranslateZ, other.m_TranslateZ, dt),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static std::vector<Transform> s_T1, s_T2;
|
||||
|
||||
static std::vector<Transform> GenRandomFractal() {
|
||||
// scale, rotation, shear, translation
|
||||
|
||||
std::vector<Transform> transforms(TRANSFORMATION_COUNT);
|
||||
|
||||
for (std::size_t i = 0; i < transforms.size(); i++) {
|
||||
Transform transform;
|
||||
transform.m_ScaleX = s_Distrib(s_Generator) * 0.4 + 0.4;
|
||||
transform.m_ScaleY = s_Distrib(s_Generator) * 0.4 + 0.4;
|
||||
transform.m_ScaleZ = s_Distrib(s_Generator) * 0.4 + 0.4;
|
||||
|
||||
transform.m_RotationX = s_Distrib(s_Generator) * 2 * 3.14;
|
||||
transform.m_RotationY = s_Distrib(s_Generator) * 2 * 3.14;
|
||||
transform.m_RotationZ = s_Distrib(s_Generator) * 2 * 3.14;
|
||||
|
||||
transform.m_ShearXY = s_Distrib(s_Generator) * 0.2f - 0.1f;
|
||||
transform.m_ShearXZ = s_Distrib(s_Generator) * 0.2f - 0.1f;
|
||||
transform.m_ShearYX = s_Distrib(s_Generator) * 0.2f - 0.1f;
|
||||
transform.m_ShearYZ = s_Distrib(s_Generator) * 0.2f - 0.1f;
|
||||
transform.m_ShearZX = s_Distrib(s_Generator) * 0.2f - 0.1f;
|
||||
transform.m_ShearZY = s_Distrib(s_Generator) * 0.2f - 0.1f;
|
||||
|
||||
transform.m_TranslateX = s_Distrib(s_Generator) * 1.2f - 0.6f;
|
||||
transform.m_TranslateY = s_Distrib(s_Generator) * 1.2f - 0.6f;
|
||||
transform.m_TranslateZ = s_Distrib(s_Generator) * 1.2f - 0.6f;
|
||||
|
||||
transforms[i] = transform;
|
||||
}
|
||||
|
||||
return transforms;
|
||||
}
|
||||
|
||||
static void GenNewFractal() {
|
||||
std::swap(s_T1, s_T2);
|
||||
s_T2 = GenRandomFractal();
|
||||
}
|
||||
|
||||
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);
|
||||
GenNewFractal();
|
||||
}
|
||||
|
||||
if (key == GLFW_KEY_T) {
|
||||
GenNewFractal();
|
||||
}
|
||||
}
|
||||
|
||||
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 std::vector<glm::mat4> GetTransformMatrixBlended(float dt) {
|
||||
std::vector<glm::mat4> result(TRANSFORMATION_COUNT);
|
||||
|
||||
constexpr auto lerp = [](float x, float y, float t) { return x * (1 - t) + y * t; };
|
||||
|
||||
constexpr auto eased = [lerp](float x, float y, float t) { return lerp(x, y, -(std::cos(3.14 * t) - 1.0f) / 2.0f); };
|
||||
|
||||
float blendFactor = std::min(ANIMATION_TIME, dt) / ANIMATION_TIME;
|
||||
|
||||
for (std::size_t i = 0; i < result.size(); i++) {
|
||||
auto blended = s_T1[i].Blend(s_T2[i], eased, blendFactor);
|
||||
result[i] = blended.ToMatrix();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static GLFWwindow* InitWindow() {
|
||||
glfwSetErrorCallback(ErrorCallback);
|
||||
|
||||
if (!glfwInit())
|
||||
@@ -35,9 +225,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 +235,110 @@ 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);
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
s_GraphicsShader = CreateGraphicsShader(s_VertexShaderPath, s_FragmentShaderPath);
|
||||
if (s_GraphicsShader == -1) {
|
||||
std::cerr << "Graphics shader failed\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Resize texture
|
||||
if (width != computeShaderTexture.Width || height != computeShaderTexture.Height)
|
||||
{
|
||||
glDeleteTextures(1, &computeShaderTexture.Handle);
|
||||
computeShaderTexture = CreateTexture(width, height);
|
||||
AttachTextureToFramebuffer(fb, computeShaderTexture);
|
||||
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);
|
||||
glUniformMatrix4fv(0, 1, false, glm::value_ptr(viewMatrix));
|
||||
glUniformMatrix4fv(1, 1, false, glm::value_ptr(projectionMatrix));
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLFWwindow* window = InitWindow();
|
||||
if (!window)
|
||||
return -1;
|
||||
|
||||
auto vertexArray = CreateDummyVAO();
|
||||
|
||||
CreateGpuBuffer();
|
||||
|
||||
float lastTime = (float)glfwGetTime();
|
||||
|
||||
int fps = 0;
|
||||
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);
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// ScopedTimer timer("Main Loop");
|
||||
|
||||
float currentTime = (float)glfwGetTime();
|
||||
float dt = currentTime - lastTime;
|
||||
lastTime = currentTime;
|
||||
|
||||
secondsTimer += dt;
|
||||
animationTimer += dt;
|
||||
if (secondsTimer >= 1.0f) {
|
||||
std::string title = "FPS : " + std::to_string(fps);
|
||||
glfwSetWindowTitle(window, title.c_str());
|
||||
|
||||
secondsTimer = 0.0f;
|
||||
fps = 0;
|
||||
}
|
||||
|
||||
if (animationTimer >= ANIMATION_TIME + ANIMATION_STILL_TIME) {
|
||||
animationTimer = 0;
|
||||
GenNewFractal();
|
||||
}
|
||||
|
||||
// Compute
|
||||
{
|
||||
glUseProgram(s_ComputeShader);
|
||||
glBindImageTexture(0, fb.ColorAttachment.Handle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
|
||||
glUseProgram(s_ComputeShader);
|
||||
|
||||
const GLuint workGroupSizeX = 16;
|
||||
const GLuint workGroupSizeY = 16;
|
||||
auto matricies = GetTransformMatrixBlended(animationTimer);
|
||||
glUniformMatrix4fv(1, matricies.size(), false, glm::value_ptr(matricies[0]));
|
||||
|
||||
GLuint numGroupsX = (width + workGroupSizeX - 1) / workGroupSizeX;
|
||||
GLuint numGroupsY = (height + workGroupSizeY - 1) / workGroupSizeY;
|
||||
glDispatchCompute(PARTICLE_COUNT / WORK_GROUP_SIZE, 1, 1);
|
||||
|
||||
glDispatchCompute(numGroupsX, numGroupsY, 1);
|
||||
// Ensure all writes to the image are complete
|
||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||
|
||||
// Ensure all writes to the image are complete
|
||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||
}
|
||||
// Graphics
|
||||
glUseProgram(s_GraphicsShader);
|
||||
|
||||
// Blit
|
||||
{
|
||||
BlitFramebufferToSwapchain(fb);
|
||||
}
|
||||
glDrawArraysInstanced(GL_POINTS, 0, 1, PARTICLE_COUNT);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
@@ -47,7 +42,7 @@ Texture LoadTexture(const std::filesystem::path& path)
|
||||
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);
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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,14 +50,13 @@ 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);
|
||||
|
||||
std::vector<GLchar> infoLog(maxLength);
|
||||
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
|
||||
|
||||
|
||||
std::cerr << infoLog.data() << std::endl;
|
||||
|
||||
glDeleteProgram(program);
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user