Route shader runtime calls through renderer GL
This commit is contained in:
178
src/shader.cpp
178
src/shader.cpp
@@ -35,9 +35,57 @@ namespace {
|
||||
return static_cast<GLenum>(pp::renderer::gl::active_uniform_count_query());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLboolean matrix_uniform_not_transposed() noexcept
|
||||
void use_opengl_program(std::uint32_t program) noexcept
|
||||
{
|
||||
return static_cast<GLboolean>(pp::renderer::gl::matrix_uniform_not_transposed());
|
||||
glUseProgram(static_cast<GLuint>(program));
|
||||
}
|
||||
|
||||
void delete_opengl_program(std::uint32_t program) noexcept
|
||||
{
|
||||
glDeleteProgram(static_cast<GLuint>(program));
|
||||
}
|
||||
|
||||
void set_opengl_uniform_4fv(std::int32_t location, std::int32_t count, const float* values) noexcept
|
||||
{
|
||||
glUniform4fv(static_cast<GLint>(location), static_cast<GLsizei>(count), values);
|
||||
}
|
||||
|
||||
void set_opengl_uniform_3fv(std::int32_t location, std::int32_t count, const float* values) noexcept
|
||||
{
|
||||
glUniform3fv(static_cast<GLint>(location), static_cast<GLsizei>(count), values);
|
||||
}
|
||||
|
||||
void set_opengl_uniform_2fv(std::int32_t location, std::int32_t count, const float* values) noexcept
|
||||
{
|
||||
glUniform2fv(static_cast<GLint>(location), static_cast<GLsizei>(count), values);
|
||||
}
|
||||
|
||||
void set_opengl_uniform_matrix_4fv(
|
||||
std::int32_t location,
|
||||
std::int32_t count,
|
||||
std::uint8_t transpose,
|
||||
const float* values) noexcept
|
||||
{
|
||||
glUniformMatrix4fv(
|
||||
static_cast<GLint>(location),
|
||||
static_cast<GLsizei>(count),
|
||||
static_cast<GLboolean>(transpose),
|
||||
values);
|
||||
}
|
||||
|
||||
void set_opengl_uniform_1i(std::int32_t location, std::int32_t value) noexcept
|
||||
{
|
||||
glUniform1i(static_cast<GLint>(location), static_cast<GLint>(value));
|
||||
}
|
||||
|
||||
void set_opengl_uniform_1f(std::int32_t location, float value) noexcept
|
||||
{
|
||||
glUniform1f(static_cast<GLint>(location), value);
|
||||
}
|
||||
|
||||
std::int32_t get_opengl_attrib_location(std::uint32_t program, const char* name) noexcept
|
||||
{
|
||||
return static_cast<std::int32_t>(glGetAttribLocation(static_cast<GLuint>(program), name));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -65,14 +113,12 @@ std::string Shader::read(const std::string& path)
|
||||
std::string data((char*)a.read_all(), a.m_len);
|
||||
|
||||
// split path
|
||||
std::string name, base, ext;
|
||||
std::string base;
|
||||
std::regex reg_path(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
|
||||
std::smatch m;
|
||||
if (std::regex_search(path, m, reg_path))
|
||||
{
|
||||
base = m[1].str();
|
||||
name = m[2].str();
|
||||
ext = m[3].str();
|
||||
}
|
||||
|
||||
for (const auto& l : split(data, '\n'))
|
||||
@@ -319,8 +365,14 @@ void Shader::destroy()
|
||||
{
|
||||
App::I->render_task_async([prog=prog]
|
||||
{
|
||||
glUseProgram(0);
|
||||
glDeleteProgram(prog);
|
||||
const auto status = pp::renderer::gl::delete_opengl_program(
|
||||
static_cast<std::uint32_t>(prog),
|
||||
pp::renderer::gl::OpenGlProgramDeleteDispatch {
|
||||
.use_program = use_opengl_program,
|
||||
.delete_program = delete_opengl_program,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::destroy() failed because: %s", status.message);
|
||||
});
|
||||
prog = 0;
|
||||
}
|
||||
@@ -329,54 +381,148 @@ void Shader::destroy()
|
||||
|
||||
void Shader::use()
|
||||
{
|
||||
glUseProgram(prog);
|
||||
const auto status = pp::renderer::gl::use_opengl_program(
|
||||
static_cast<std::uint32_t>(prog),
|
||||
pp::renderer::gl::OpenGlProgramUseDispatch {
|
||||
.use_program = use_opengl_program,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::use() failed because: %s", status.message);
|
||||
}
|
||||
void Shader::u_vec4(kShaderUniform id, const glm::vec4& v)
|
||||
{
|
||||
if (m_umap.count(id) == 0)
|
||||
LOG("UNIFORM vec4 %d NOT FOUND in shader %d", (int)id, (int)name)
|
||||
else glUniform4fv(m_umap[id], 1, glm::value_ptr(v));
|
||||
else
|
||||
{
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_vec4(
|
||||
static_cast<std::int32_t>(m_umap[id]),
|
||||
glm::value_ptr(v),
|
||||
pp::renderer::gl::OpenGlUniformVec4Dispatch {
|
||||
.uniform_4fv = set_opengl_uniform_4fv,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_vec4() failed because: %s", status.message);
|
||||
}
|
||||
}
|
||||
void Shader::u_vec3(kShaderUniform id, const glm::vec3& v)
|
||||
{
|
||||
if (m_umap.count(id) == 0)
|
||||
LOG("UNIFORM vec3 %d NOT FOUND in shader %d", (int)id, (int)name)
|
||||
else glUniform3fv(m_umap[id], 1, glm::value_ptr(v));
|
||||
else
|
||||
{
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_vec3(
|
||||
static_cast<std::int32_t>(m_umap[id]),
|
||||
glm::value_ptr(v),
|
||||
pp::renderer::gl::OpenGlUniformVec3Dispatch {
|
||||
.uniform_3fv = set_opengl_uniform_3fv,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_vec3() failed because: %s", status.message);
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::u_vec2(kShaderUniform id, const glm::vec2& v)
|
||||
{
|
||||
if (m_umap.count(id) == 0)
|
||||
LOG("UNIFORM vec2 %d NOT FOUND in shader %d", (int)id, (int)name)
|
||||
else glUniform2fv(m_umap[id], 1, glm::value_ptr(v));
|
||||
else
|
||||
{
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_vec2(
|
||||
static_cast<std::int32_t>(m_umap[id]),
|
||||
glm::value_ptr(v),
|
||||
pp::renderer::gl::OpenGlUniformVec2Dispatch {
|
||||
.uniform_2fv = set_opengl_uniform_2fv,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_vec2() failed because: %s", status.message);
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::u_mat4(kShaderUniform id, const glm::mat4& m)
|
||||
{
|
||||
if (m_umap.count(id) == 0)
|
||||
LOG("UNIFORM mat4 %d NOT FOUND in shader %d", (int)id, (int)name)
|
||||
else glUniformMatrix4fv(m_umap[id], 1, matrix_uniform_not_transposed(), glm::value_ptr(m));
|
||||
else
|
||||
{
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_mat4(
|
||||
static_cast<std::int32_t>(m_umap[id]),
|
||||
glm::value_ptr(m),
|
||||
pp::renderer::gl::OpenGlUniformMat4Dispatch {
|
||||
.uniform_matrix_4fv = set_opengl_uniform_matrix_4fv,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_mat4() failed because: %s", status.message);
|
||||
}
|
||||
}
|
||||
void Shader::u_int(kShaderUniform id, int i)
|
||||
{
|
||||
if (m_umap.count(id) == 0)
|
||||
LOG("UNIFORM int %d NOT FOUND in shader %d", (int)id, (int)name)
|
||||
else
|
||||
glUniform1i(m_umap[id], i);
|
||||
{
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_int(
|
||||
static_cast<std::int32_t>(m_umap[id]),
|
||||
static_cast<std::int32_t>(i),
|
||||
pp::renderer::gl::OpenGlUniformIntDispatch {
|
||||
.uniform_1i = set_opengl_uniform_1i,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_int() failed because: %s", status.message);
|
||||
}
|
||||
}
|
||||
void Shader::u_int(const char* uniform_name, int i)
|
||||
{
|
||||
glUniform1i(glGetAttribLocation(prog, uniform_name), i);
|
||||
const auto location = pp::renderer::gl::get_opengl_attribute_location(
|
||||
static_cast<std::uint32_t>(prog),
|
||||
uniform_name,
|
||||
pp::renderer::gl::OpenGlAttributeLocationDispatch {
|
||||
.get_attrib_location = get_opengl_attrib_location,
|
||||
});
|
||||
if (!location.ok())
|
||||
{
|
||||
LOG("Shader::u_int(name) lookup failed because: %s", location.status().message);
|
||||
return;
|
||||
}
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_int(
|
||||
location.value(),
|
||||
static_cast<std::int32_t>(i),
|
||||
pp::renderer::gl::OpenGlUniformIntDispatch {
|
||||
.uniform_1i = set_opengl_uniform_1i,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_int(name) failed because: %s", status.message);
|
||||
}
|
||||
void Shader::u_float(kShaderUniform id, float f)
|
||||
{
|
||||
if (m_umap.count(id) == 0)
|
||||
LOG("UNIFORM float %d NOT FOUND in shader %d", (int)id, (int)name)
|
||||
else glUniform1f(m_umap[id], f);
|
||||
else
|
||||
{
|
||||
const auto status = pp::renderer::gl::set_opengl_uniform_float(
|
||||
static_cast<std::int32_t>(m_umap[id]),
|
||||
f,
|
||||
pp::renderer::gl::OpenGlUniformFloatDispatch {
|
||||
.uniform_1f = set_opengl_uniform_1f,
|
||||
});
|
||||
if (!status.ok())
|
||||
LOG("Shader::u_float() failed because: %s", status.message);
|
||||
}
|
||||
}
|
||||
GLint Shader::GetAttribLocation(const char* attribute_name)
|
||||
{
|
||||
return glGetAttribLocation(prog, attribute_name);
|
||||
const auto location = pp::renderer::gl::get_opengl_attribute_location(
|
||||
static_cast<std::uint32_t>(prog),
|
||||
attribute_name,
|
||||
pp::renderer::gl::OpenGlAttributeLocationDispatch {
|
||||
.get_attrib_location = get_opengl_attrib_location,
|
||||
});
|
||||
if (!location.ok())
|
||||
{
|
||||
LOG("Shader::GetAttribLocation() failed because: %s", location.status().message);
|
||||
return -1;
|
||||
}
|
||||
return static_cast<GLint>(location.value());
|
||||
}
|
||||
|
||||
bool ShaderManager::load(kShader id, const std::string& path)
|
||||
|
||||
Reference in New Issue
Block a user