mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Shader class system better working
This commit is contained in:
@ -61,52 +61,36 @@ void printWithLineNumbers(const char *str)
|
||||
std::cout << lineNumber++ << ": " << line << std::endl;
|
||||
}
|
||||
|
||||
Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string computePath, std::string denoisingPath)
|
||||
Shader::Shader(GLenum type, const std::string &file_path)
|
||||
{
|
||||
const char *vertexCode = loadFileWithIncludes(vertexPath);
|
||||
const char *fragmentCode = loadFileWithIncludes(fragmentPath);
|
||||
const char *computeCode = loadFileWithIncludes(computePath);
|
||||
const char *denoisingCode = loadFileWithIncludes(denoisingPath);
|
||||
_type = type;
|
||||
_file_path = file_path;
|
||||
_shader_id = 0;
|
||||
|
||||
// printWithLineNumbers(computeCode);
|
||||
|
||||
_vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
|
||||
glShaderSource(_vertex, 1, &vertexCode, NULL);
|
||||
glCompileShader(_vertex);
|
||||
|
||||
checkCompileErrors(_vertex);
|
||||
|
||||
_fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
glShaderSource(_fragment, 1, &fragmentCode, NULL);
|
||||
glCompileShader(_fragment);
|
||||
|
||||
checkCompileErrors(_fragment);
|
||||
|
||||
_compute = glCreateShader(GL_COMPUTE_SHADER);
|
||||
|
||||
glShaderSource(_compute, 1, &computeCode, NULL);
|
||||
glCompileShader(_compute);
|
||||
|
||||
checkCompileErrors(_compute);
|
||||
|
||||
_denoising = glCreateShader(GL_COMPUTE_SHADER);
|
||||
|
||||
glShaderSource(_denoising, 1, &denoisingCode, NULL);
|
||||
glCompileShader(_denoising);
|
||||
|
||||
checkCompileErrors(_denoising);
|
||||
this->compile();
|
||||
}
|
||||
|
||||
Shader::~Shader(void)
|
||||
{
|
||||
glDeleteShader(_vertex);
|
||||
glDeleteShader(_fragment);
|
||||
glDeleteShader(_compute);
|
||||
glDeleteProgram(_program);
|
||||
glDeleteProgram(_program_compute);
|
||||
glDeleteProgram(_denoising);
|
||||
}
|
||||
|
||||
void Shader::compile()
|
||||
{
|
||||
_shader_id = glCreateShader(_type);
|
||||
|
||||
const char *shader_code = loadFileWithIncludes(_file_path);
|
||||
// printWithLineNumbers(shader_code);
|
||||
|
||||
glShaderSource(_shader_id, 1, &shader_code, NULL);
|
||||
glCompileShader(_shader_id);
|
||||
|
||||
this->checkCompileErrors();
|
||||
}
|
||||
|
||||
void Shader::reload()
|
||||
{
|
||||
glDeleteShader(_shader_id);
|
||||
this->compile();
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
@ -176,15 +160,15 @@ void Shader::attach(void)
|
||||
glBindImageTexture(4, _position_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(GLuint shader)
|
||||
void Shader::checkCompileErrors()
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
glGetShaderiv(_shader_id, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
glGetShaderInfoLog(_shader_id, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
}
|
||||
@ -287,29 +271,9 @@ void Shader::set_textures(std::vector<GLuint> texture_ids, std::vector<GLuint> e
|
||||
}
|
||||
|
||||
|
||||
GLuint Shader::getProgram(void) const
|
||||
GLuint Shader::getShader(void) const
|
||||
{
|
||||
return (_program);
|
||||
}
|
||||
|
||||
GLuint Shader::getProgramCompute(void) const
|
||||
{
|
||||
return (_program_compute);
|
||||
}
|
||||
|
||||
GLuint Shader::getProgramComputeDenoising(void) const
|
||||
{
|
||||
return (_program_denoising);
|
||||
}
|
||||
|
||||
GLuint Shader::getNormalTexture(void) const
|
||||
{
|
||||
return (_normal_texture);
|
||||
}
|
||||
|
||||
GLuint Shader::getPositionTexture(void) const
|
||||
{
|
||||
return (_position_texture);
|
||||
return (_shader_id);
|
||||
}
|
||||
|
||||
std::vector<float> Shader::getOutputImage(void)
|
||||
|
92
srcs/class/ShaderProgram.cpp
Normal file
92
srcs/class/ShaderProgram.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ShaderProgram.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/12 22:21:46 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/12 22:21:46 by TheRed ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ShaderProgram.hpp"
|
||||
|
||||
ShaderProgram::ShaderProgram()
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram(void)
|
||||
{
|
||||
glDeleteProgram(_program);
|
||||
}
|
||||
|
||||
void ShaderProgram::attachShader(Shader *shader)
|
||||
{
|
||||
_shaders.push_back(shader);
|
||||
glAttachShader(_program, shader->getShader());
|
||||
}
|
||||
|
||||
void ShaderProgram::link()
|
||||
{
|
||||
glLinkProgram(_program);
|
||||
|
||||
GLint success;
|
||||
glGetProgramiv(_program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
GLchar infoLog[512];
|
||||
glGetProgramInfoLog(_program, 512, NULL, infoLog);
|
||||
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderProgram::use() const
|
||||
{
|
||||
glUseProgram(_program);
|
||||
}
|
||||
|
||||
void ShaderProgram::dispathCompute(GLuint x, GLuint y, GLuint z) const
|
||||
{
|
||||
this->use();
|
||||
glDispatchCompute(x, y, z);
|
||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||
}
|
||||
|
||||
void ShaderProgram::bindImageTexture(GLuint texture_id, GLuint unit, GLenum access, GLenum format) const
|
||||
{
|
||||
glBindImageTexture(unit, texture_id, 0, GL_FALSE, 0, access, format);
|
||||
}
|
||||
|
||||
void ShaderProgram::reloadShaders(void)
|
||||
{
|
||||
std::cout << "Reloading shaders" << std::endl;
|
||||
|
||||
for (Shader *shader : _shaders)
|
||||
{
|
||||
glDetachShader(_program, shader->getShader());
|
||||
shader->reload();
|
||||
glAttachShader(_program, shader->getShader());
|
||||
}
|
||||
|
||||
this->link();
|
||||
}
|
||||
|
||||
void ShaderProgram::set_int(const std::string &name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(_program, name.c_str()), value);
|
||||
}
|
||||
void ShaderProgram::set_float(const std::string &name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(_program, name.c_str()), value);
|
||||
}
|
||||
void ShaderProgram::set_vec2(const std::string &name, const glm::vec2 &value) const
|
||||
{
|
||||
glUniform2fv(glGetUniformLocation(_program, name.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
GLuint ShaderProgram::getProgram(void) const
|
||||
{
|
||||
return (_program);
|
||||
}
|
Reference in New Issue
Block a user