-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComputeShader.cpp
More file actions
81 lines (73 loc) · 2.55 KB
/
ComputeShader.cpp
File metadata and controls
81 lines (73 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <ComputeShader.h>
#include <Renderer.h>
ComputeShader::ComputeShader(const std::string& filepath)
{
std::ifstream t(filepath);
std::stringstream tbuffer;
tbuffer << t.rdbuf();
std::cout << tbuffer.str() << "\n";
t.close();
std::string computeSource = tbuffer.str();
GLCall(unsigned int computeId = glCreateShader(GL_COMPUTE_SHADER));
const char* computeSrc = computeSource.c_str();
GLCall(glShaderSource(computeId, 1, &computeSrc, nullptr));
GLCall(glCompileShader(computeId));
int computeCompileResult;
GLCall(glGetShaderiv(computeId, GL_COMPILE_STATUS, &computeCompileResult));
if (computeCompileResult == GL_FALSE)
{
int length;
GLCall(glGetShaderiv(computeId, GL_INFO_LOG_LENGTH, &length));
char* message = (char*)malloc(sizeof(char) * length);
GLCall(glGetShaderInfoLog(computeId, length, &length, message));
std::cout << "failed to compile shader\n" << message << '\n';
}
GLCall(unsigned int computeProgram = glCreateProgram());
GLCall(glAttachShader(computeProgram, computeId));
GLCall(glLinkProgram(computeProgram));
GLCall(glValidateProgram(computeProgram));
m_RendererID = computeProgram;
}
void ComputeShader::SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3)
{
GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3));
}
void ComputeShader::SetUniform1i(const std::string& name, int v0)
{
GLCall(glUniform1i(GetUniformLocation(name), v0));
}
void ComputeShader::SetUniform1ui(const std::string& name, unsigned int v0)
{
GLCall(glUniform1ui(GetUniformLocation(name), v0));
}
void ComputeShader::SetUniform1f(const std::string& name, float v0)
{
GLCall(glUniform1f(GetUniformLocation(name), v0));
}
int ComputeShader::GetUniformLocation(const std::string& name)
{
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name];
GLCall(int location = glGetUniformLocation(m_RendererID, name.c_str()));
if (location == -1)
{
std::cout << name << " Does Not Exist\n";
}
std::cout << location << " EXISTS\n";
m_UniformLocationCache[name] = location;
return location;
}
ComputeShader::~ComputeShader()
{
GLCall(glDeleteProgram(m_RendererID));
}
void ComputeShader::Compute(unsigned int x,unsigned int y, unsigned int z) const
{
for (int i = 0; i < SSBOID.size(); i++)
{
GLCall(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, SSBOPOS[i], SSBOID[i]));
}
GLCall(glUseProgram(m_RendererID));
GLCall(glDispatchCompute(x, y, z));
//GLCall(glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT));
}