Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
ComputeShader.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <glm/glm.hpp>
4#include <QOpenGLFunctions_4_5_Core>
5#include <iostream>
6
8public:
9 ComputeShader(const std::string& computePath, QOpenGLFunctions_4_5_Core* glFuncs);
11
12 void use() const;
13
14 void setBool(const std::string& name, bool value) const;
15 void setInt(const std::string& name, int value) const;
16 void setFloat(const std::string& name, float value) const;
17 void setMat4(const std::string& name, const glm::mat4& mat) const;
18 void setVec3(const std::string& name, const glm::vec3& vec) const;
19
20 unsigned int createSSBO(const void* data, unsigned int size, unsigned int bindingPoint);
21 template<typename T>
22 std::vector<T> readSSBO(unsigned int bufferID, size_t count) const {
23 std::vector<T> result(count);
24 funcs->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID);
25
26 void* ptr = funcs->glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, count * sizeof(T), GL_MAP_READ_BIT);
27 if (ptr) {
28 T* dataPtr = static_cast<T*>(ptr);
29 std::copy(dataPtr, dataPtr + count, result.begin());
30 funcs->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
31 } else {
32 std::cerr << "ComputeShader::readSSBO() — glMapBufferRange failed" << std::endl;
33 }
34 return result;
35 }
36
37 void dispatch(unsigned int groupsX, unsigned int groupsY = 1, unsigned int groupsZ = 1) const;
38
39 unsigned int id() const { return ID; }
40
41private:
42 QOpenGLFunctions_4_5_Core* funcs;
43 unsigned int ID;
44 std::vector<unsigned int> ssboIDs;
45 std::string loadFile(const std::string& path) const;
46 unsigned int compileShader(GLenum type, const std::string& source) const;
47};
unsigned int createSSBO(const void *data, unsigned int size, unsigned int bindingPoint)
void setVec3(const std::string &name, const glm::vec3 &vec) const
void setBool(const std::string &name, bool value) const
void setFloat(const std::string &name, float value) const
unsigned int id() const
void dispatch(unsigned int groupsX, unsigned int groupsY=1, unsigned int groupsZ=1) const
void setMat4(const std::string &name, const glm::mat4 &mat) const
std::vector< T > readSSBO(unsigned int bufferID, size_t count) const
void use() const
void setInt(const std::string &name, int value) const