generated from Persson-dev/OpenGLComputeShader
Compare commits
10 Commits
b5f6e81a7b
...
a46abca707
| Author | SHA1 | Date | |
|---|---|---|---|
|
a46abca707
|
|||
|
ee93146167
|
|||
|
e940b670d2
|
|||
|
9e6088f9bf
|
|||
|
da536b8d7d
|
|||
|
abb33b6f1f
|
|||
|
10b0b54e71
|
|||
|
0eebe09d47
|
|||
|
78088b2067
|
|||
|
bd81c632de
|
@@ -5,11 +5,14 @@ layout(std430, binding = 3) buffer layoutName
|
|||||||
float data_SSBO[];
|
float data_SSBO[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
layout(location = 0) uniform mat4 viewMatrix;
|
||||||
|
layout(location = 1) uniform mat4 projectionMatrix;
|
||||||
|
|
||||||
vec3 unpack(uint index) {
|
vec3 unpack(uint index) {
|
||||||
return vec3(data_SSBO[index * 3], data_SSBO[index * 3 + 1], data_SSBO[index * 3 + 2]);
|
return vec3(data_SSBO[index * 3], data_SSBO[index * 3 + 1], data_SSBO[index * 3 + 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(unpack(gl_InstanceID), 1.0);
|
gl_Position = projectionMatrix * viewMatrix * vec4(unpack(gl_InstanceID), 1.0);
|
||||||
}
|
}
|
||||||
177
src/Main.cpp
177
src/Main.cpp
@@ -7,12 +7,18 @@
|
|||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
constexpr int WORK_GROUP_SIZE = 64;
|
constexpr int WORK_GROUP_SIZE = 64;
|
||||||
constexpr int PARTICLE_COUNT = WORK_GROUP_SIZE * 10000;
|
constexpr int PARTICLE_COUNT = WORK_GROUP_SIZE * 15625;
|
||||||
|
|
||||||
constexpr int SWAP_INTERVAL = 1;
|
constexpr int SWAP_INTERVAL = 1;
|
||||||
|
|
||||||
|
constexpr int TRANSFORMATION_COUNT = 3;
|
||||||
|
|
||||||
|
constexpr float ANIMATION_TIME = 2;
|
||||||
|
constexpr float ANIMATION_STILL_TIME = 1;
|
||||||
|
|
||||||
class Timer {
|
class Timer {
|
||||||
public:
|
public:
|
||||||
Timer() {
|
Timer() {
|
||||||
@@ -40,10 +46,110 @@ 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_VertexShaderPath = "Shaders/Vertex.glsl";
|
||||||
static const std::filesystem::path s_FragmentShaderPath = "Shaders/Fragment.glsl";
|
static const std::filesystem::path s_FragmentShaderPath = "Shaders/Fragment.glsl";
|
||||||
|
|
||||||
|
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) {
|
static void ErrorCallback(int error, const char* description) {
|
||||||
std::cerr << "Error: " << description << std::endl;
|
std::cerr << "Error: " << description << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||||
if (action != GLFW_PRESS)
|
if (action != GLFW_PRESS)
|
||||||
return;
|
return;
|
||||||
@@ -55,6 +161,11 @@ static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, i
|
|||||||
s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath);
|
s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath);
|
||||||
s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath);
|
s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath);
|
||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * PARTICLE_COUNT * 3, nullptr, GL_DYNAMIC_COPY);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * PARTICLE_COUNT * 3, nullptr, GL_DYNAMIC_COPY);
|
||||||
|
GenNewFractal();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key == GLFW_KEY_T) {
|
||||||
|
GenNewFractal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +196,23 @@ static void CreateGpuBuffer() {
|
|||||||
GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
|
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() {
|
static GLFWwindow* InitWindow() {
|
||||||
glfwSetErrorCallback(ErrorCallback);
|
glfwSetErrorCallback(ErrorCallback);
|
||||||
|
|
||||||
@@ -121,6 +249,13 @@ static GLFWwindow* InitWindow() {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,33 +272,15 @@ int main() {
|
|||||||
|
|
||||||
int fps = 0;
|
int fps = 0;
|
||||||
float secondsTimer = 0.0f;
|
float secondsTimer = 0.0f;
|
||||||
|
float animationTimer = 0.0f;
|
||||||
|
|
||||||
glBindVertexArray(vertexArray);
|
glBindVertexArray(vertexArray);
|
||||||
|
|
||||||
std::vector<glm::mat4> transformations = {
|
// ApplyTransforms(SIERPINSKI_TRIANGLE);
|
||||||
{
|
s_T1 = GenRandomFractal();
|
||||||
0.5f, 0.0f, 0.0f, 0.0f,
|
s_T2 = GenRandomFractal();
|
||||||
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]));
|
|
||||||
|
|
||||||
|
glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
// ScopedTimer timer("Main Loop");
|
// ScopedTimer timer("Main Loop");
|
||||||
@@ -173,6 +290,7 @@ int main() {
|
|||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
|
|
||||||
secondsTimer += dt;
|
secondsTimer += dt;
|
||||||
|
animationTimer += dt;
|
||||||
if (secondsTimer >= 1.0f) {
|
if (secondsTimer >= 1.0f) {
|
||||||
std::string title = "FPS : " + std::to_string(fps);
|
std::string title = "FPS : " + std::to_string(fps);
|
||||||
glfwSetWindowTitle(window, title.c_str());
|
glfwSetWindowTitle(window, title.c_str());
|
||||||
@@ -181,8 +299,17 @@ int main() {
|
|||||||
fps = 0;
|
fps = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (animationTimer >= ANIMATION_TIME + ANIMATION_STILL_TIME) {
|
||||||
|
animationTimer = 0;
|
||||||
|
GenNewFractal();
|
||||||
|
}
|
||||||
|
|
||||||
// Compute
|
// Compute
|
||||||
glUseProgram(s_ComputeShader);
|
glUseProgram(s_ComputeShader);
|
||||||
|
|
||||||
|
auto matricies = GetTransformMatrixBlended(animationTimer);
|
||||||
|
glUniformMatrix4fv(1, matricies.size(), false, glm::value_ptr(matricies[0]));
|
||||||
|
|
||||||
glDispatchCompute(PARTICLE_COUNT / WORK_GROUP_SIZE, 1, 1);
|
glDispatchCompute(PARTICLE_COUNT / WORK_GROUP_SIZE, 1, 1);
|
||||||
|
|
||||||
// Ensure all writes to the image are complete
|
// Ensure all writes to the image are complete
|
||||||
@@ -197,7 +324,6 @@ int main() {
|
|||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glClearColor(0, 0, 0, 1);
|
|
||||||
|
|
||||||
int width, height;
|
int width, height;
|
||||||
glfwGetWindowSize(window, &width, &height);
|
glfwGetWindowSize(window, &width, &height);
|
||||||
@@ -212,7 +338,6 @@ int main() {
|
|||||||
// std::cout << "\t" << positions[i * 3] << " " << positions[i * 3 + 1] << " " << positions[i * 3 + 2] << "\n";
|
// std::cout << "\t" << positions[i * 3] << " " << positions[i * 3 + 1] << " " << positions[i * 3 + 2] << "\n";
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
fps++;
|
fps++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user