|
|
|
@ -1,11 +1,10 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 Riyyi |
|
|
|
|
* Copyright (C) 2022,2024 Riyyi |
|
|
|
|
* |
|
|
|
|
* SPDX-License-Identifier: MIT |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include <utility> // std::move |
|
|
|
|
#include <vector> // std::vector |
|
|
|
|
#include <vector> // std::vector |
|
|
|
|
|
|
|
|
|
#include "glad/glad.h" |
|
|
|
|
#include "glm/gtc/type_ptr.hpp" // glm::value_ptr |
|
|
|
@ -13,32 +12,34 @@
|
|
|
|
|
#include "ruc/format/log.h" |
|
|
|
|
#include "ruc/meta/assert.h" |
|
|
|
|
|
|
|
|
|
#include "inferno/core.h" |
|
|
|
|
#include "inferno/render/shader.h" |
|
|
|
|
#include "inferno/asset/shader.h" |
|
|
|
|
|
|
|
|
|
namespace Inferno { |
|
|
|
|
|
|
|
|
|
Shader::Shader(const std::string& name) |
|
|
|
|
: m_name(std::move(name)) |
|
|
|
|
, m_id(0) |
|
|
|
|
std::shared_ptr<Shader> Shader::create(std::string_view path) |
|
|
|
|
{ |
|
|
|
|
auto result = std::shared_ptr<Shader>(new Shader(path)); |
|
|
|
|
|
|
|
|
|
// Get file contents
|
|
|
|
|
std::string vertexSrc = ruc::File(name + ".vert").data(); |
|
|
|
|
std::string fragmentSrc = ruc::File(name + ".frag").data(); |
|
|
|
|
auto stringPath = std::string(path); |
|
|
|
|
std::string vertexSrc = ruc::File(stringPath + ".vert").data(); |
|
|
|
|
std::string fragmentSrc = ruc::File(stringPath + ".frag").data(); |
|
|
|
|
|
|
|
|
|
// Compile shaders
|
|
|
|
|
uint32_t vertexID = compileShader(GL_VERTEX_SHADER, vertexSrc.c_str()); |
|
|
|
|
uint32_t fragmentID = compileShader(GL_FRAGMENT_SHADER, fragmentSrc.c_str()); |
|
|
|
|
uint32_t vertexID = result->compileShader(GL_VERTEX_SHADER, vertexSrc.c_str()); |
|
|
|
|
uint32_t fragmentID = result->compileShader(GL_FRAGMENT_SHADER, fragmentSrc.c_str()); |
|
|
|
|
|
|
|
|
|
// Link shaders
|
|
|
|
|
if (vertexID > 0 && fragmentID > 0) { |
|
|
|
|
m_id = linkShader(vertexID, fragmentID); |
|
|
|
|
result->m_id = result->linkShader(vertexID, fragmentID); |
|
|
|
|
} |
|
|
|
|
// Clear resources
|
|
|
|
|
else if (vertexID > 0) |
|
|
|
|
glDeleteShader(vertexID); |
|
|
|
|
else if (fragmentID > 0) |
|
|
|
|
glDeleteShader(fragmentID); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Shader::~Shader() |
|
|
|
@ -49,62 +50,62 @@ Shader::~Shader()
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int32_t Shader::findUniform(const std::string& name) const |
|
|
|
|
int32_t Shader::findUniform(std::string_view name) const |
|
|
|
|
{ |
|
|
|
|
int32_t location = glGetUniformLocation(m_id, name.c_str()); |
|
|
|
|
int32_t location = glGetUniformLocation(m_id, name.data()); |
|
|
|
|
VERIFY(location != -1, "Shader could not find uniform '{}'", name); |
|
|
|
|
return location; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setInt(const std::string& name, int value) |
|
|
|
|
void Shader::setInt(std::string_view name, int value) |
|
|
|
|
{ |
|
|
|
|
// Set uniform int
|
|
|
|
|
glUniform1i(findUniform(name), value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setInt(const std::string& name, int* values, uint32_t count) |
|
|
|
|
void Shader::setInt(std::string_view name, int* values, uint32_t count) |
|
|
|
|
{ |
|
|
|
|
// Set uniform int array
|
|
|
|
|
glUniform1iv(findUniform(name), count, values); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, float value) const |
|
|
|
|
void Shader::setFloat(std::string_view name, float value) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform float
|
|
|
|
|
glUniform1f(findUniform(name), value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, float v1, float v2, float v3, float v4) const |
|
|
|
|
void Shader::setFloat(std::string_view name, float v1, float v2, float v3, float v4) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform vec4 data
|
|
|
|
|
glUniform4f(findUniform(name), v1, v2, v3, v4); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, glm::vec2 value) const |
|
|
|
|
void Shader::setFloat(std::string_view name, glm::vec2 value) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform vec2 data
|
|
|
|
|
glUniform2f(findUniform(name), value.x, value.y); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, glm::vec3 value) const |
|
|
|
|
void Shader::setFloat(std::string_view name, glm::vec3 value) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform vec3 data
|
|
|
|
|
glUniform3f(findUniform(name), value.x, value.y, value.z); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, glm::vec4 value) const |
|
|
|
|
void Shader::setFloat(std::string_view name, glm::vec4 value) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform vec4 data
|
|
|
|
|
glUniform4f(findUniform(name), value.x, value.y, value.z, value.w); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, glm::mat3 matrix) const |
|
|
|
|
void Shader::setFloat(std::string_view name, glm::mat3 matrix) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform mat3 data
|
|
|
|
|
glUniformMatrix3fv(findUniform(name), 1, GL_FALSE, glm::value_ptr(matrix)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string& name, glm::mat4 matrix) const |
|
|
|
|
void Shader::setFloat(std::string_view name, glm::mat4 matrix) const |
|
|
|
|
{ |
|
|
|
|
// Set uniform mat4 data
|
|
|
|
|
glUniformMatrix4fv(findUniform(name), 1, GL_FALSE, glm::value_ptr(matrix)); |
|
|
|
@ -199,80 +200,4 @@ int32_t Shader::checkStatus(uint32_t check, bool isProgram) const
|
|
|
|
|
return success; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
ShaderManager::ShaderManager(s) |
|
|
|
|
{ |
|
|
|
|
ruc::info("ShaderManager initialized"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ShaderManager::~ShaderManager() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ShaderManager::add(const std::string& name, std::shared_ptr<Shader> shader) |
|
|
|
|
{ |
|
|
|
|
// Construct (key, value) pair and insert it into the unordered_map
|
|
|
|
|
m_shaderList.emplace(std::move(name), std::move(shader)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::shared_ptr<Shader> ShaderManager::load(const std::string& name) |
|
|
|
|
{ |
|
|
|
|
if (exists(name)) { |
|
|
|
|
return get(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::shared_ptr<Shader> shader = std::make_shared<Shader>(name); |
|
|
|
|
add(name, shader); |
|
|
|
|
return get(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::shared_ptr<Shader> ShaderManager::load(const std::string& vertexSource, |
|
|
|
|
const std::string& fragmentSource) |
|
|
|
|
{ |
|
|
|
|
std::string name = computeName(vertexSource, fragmentSource); |
|
|
|
|
return load(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::shared_ptr<Shader> ShaderManager::get(const std::string& name) |
|
|
|
|
{ |
|
|
|
|
return exists(name) ? m_shaderList.at(name) : nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool ShaderManager::exists(const std::string& name) |
|
|
|
|
{ |
|
|
|
|
return m_shaderList.find(name) != m_shaderList.end(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ShaderManager::remove(const std::string& name) |
|
|
|
|
{ |
|
|
|
|
if (exists(name)) { |
|
|
|
|
m_shaderList.erase(name); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ShaderManager::remove(std::shared_ptr<Shader> shader) |
|
|
|
|
{ |
|
|
|
|
if (exists(shader->name())) { |
|
|
|
|
m_shaderList.erase(shader->name()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string ShaderManager::computeName(const std::string& vertexSource, |
|
|
|
|
const std::string& fragmentSource) |
|
|
|
|
{ |
|
|
|
|
auto vertexPos = vertexSource.find_last_of('.'); |
|
|
|
|
auto fragmentPos = fragmentSource.find_last_of('.'); |
|
|
|
|
|
|
|
|
|
VERIFY(vertexPos != std::string::npos, "Shader did not have file extension: '{}'", vertexSource); |
|
|
|
|
VERIFY(fragmentPos != std::string::npos, "Shader did not have file extension: '{}'", fragmentSource); |
|
|
|
|
|
|
|
|
|
auto vertexName = vertexSource.substr(0, vertexPos); |
|
|
|
|
auto fragmentName = vertexSource.substr(0, fragmentPos); |
|
|
|
|
|
|
|
|
|
VERIFY(vertexName == fragmentName, "Shader names did not match: {} {}", vertexSource, fragmentSource); |
|
|
|
|
|
|
|
|
|
return vertexName; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace Inferno
|