Skip to content

Commit b8f25bd

Browse files
authored
Merge pull request #63 from Daynlight/ssbo
Ssbo
2 parents 72fec9a + 679edee commit b8f25bd

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

CWindow/Renderer/OpenGL/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ set(src
1717
"Shader/Compute/Shader.cpp"
1818
"Shader/Compute/Shader.hpp"
1919
"Shader/Compute/Shader.h"
20+
"Shader/SSBO/Shader.cpp"
21+
"Shader/SSBO/Shader.hpp"
22+
"Shader/SSBO/Shader.h"
2023
"Uniform/Uniform.cpp"
2124
"Uniform/Uniform.h"
2225
"Texture/Texture.cpp"

CWindow/Renderer/OpenGL/Renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Shader/Shader/Shader.h"
88
#include "Shader/Compute/Shader.h"
9+
#include "Shader/SSBO/Shader.h"
910

1011
#include "Uniform/Uniform.h"
1112

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "Shader.h"
2+
3+
4+
5+
6+
7+
8+
9+
CW::Renderer::GPUStore::GPUStore() noexcept
10+
:is_created(false) {};
11+
12+
13+
14+
CW::Renderer::GPUStore::~GPUStore() noexcept {
15+
destroy();
16+
};
17+
18+
19+
void CW::Renderer::GPUStore::create() noexcept{
20+
if(is_created)
21+
destroy();
22+
23+
glGenBuffers(1, &SSBO);
24+
is_created = true;
25+
}
26+
27+
void CW::Renderer::GPUStore::destroy() noexcept {
28+
if (is_created){
29+
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, slot, 0);
30+
glDeleteBuffers(1, &SSBO);
31+
SSBO = 0;
32+
slot = 0;
33+
};
34+
35+
is_created = false;
36+
};
37+
38+
39+
40+
void CW::Renderer::GPUStore::bind(GLuint socket) noexcept{
41+
this->slot = socket;
42+
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, socket, SSBO);
43+
};
44+
45+
46+
47+
void CW::Renderer::GPUStore::unbind() noexcept{
48+
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, slot, 0);
49+
slot = 0;
50+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include "glad/glad.h"
4+
#include "GLFW/glfw3.h"
5+
#include "glm/glm.hpp"
6+
7+
#include <string>
8+
#include <cmath>
9+
10+
11+
12+
13+
14+
15+
16+
namespace CW::Renderer{
17+
class GPUStore{
18+
private:
19+
GLuint SSBO;
20+
21+
bool is_created = false;
22+
unsigned int data_size = 0;
23+
GLuint slot = 0;
24+
25+
public:
26+
GPUStore() noexcept;
27+
~GPUStore() noexcept;
28+
29+
void create() noexcept;
30+
void destroy() noexcept;
31+
32+
template<typename T>
33+
void set(const std::vector<T>& data) noexcept;
34+
35+
void bind(GLuint socket = 0) noexcept;
36+
void unbind() noexcept;
37+
};
38+
};
39+
40+
41+
42+
43+
44+
45+
46+
#include "Shader.hpp"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "Shader.h"
2+
3+
4+
5+
6+
7+
8+
9+
template<typename T>
10+
void CW::Renderer::GPUStore::set(const std::vector<T>& data) noexcept {
11+
if (data.empty())
12+
return;
13+
14+
if(!is_created)
15+
create();
16+
17+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
18+
glBufferData(GL_SHADER_STORAGE_BUFFER, data.size() * sizeof(T), data.data(), GL_DYNAMIC_COPY);
19+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
20+
21+
data_size = data.size();
22+
};
23+

0 commit comments

Comments
 (0)