Extract OpenGL shader attribute bindings
This commit is contained in:
43
src/renderer_gl/shader_bindings.cpp
Normal file
43
src/renderer_gl/shader_bindings.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "renderer_gl/shader_bindings.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
|
||||
namespace pp::renderer::gl {
|
||||
|
||||
std::span<const OpenGlAttributeBinding> panopainter_shader_attribute_bindings() noexcept
|
||||
{
|
||||
static constexpr std::array<OpenGlAttributeBinding, 5> bindings {
|
||||
OpenGlAttributeBinding { .name = "pos", .location = 0 },
|
||||
OpenGlAttributeBinding { .name = "uvs", .location = 1 },
|
||||
OpenGlAttributeBinding { .name = "uvs2", .location = 2 },
|
||||
OpenGlAttributeBinding { .name = "col", .location = 3 },
|
||||
OpenGlAttributeBinding { .name = "nor", .location = 3 },
|
||||
};
|
||||
|
||||
return bindings;
|
||||
}
|
||||
|
||||
pp::foundation::Status validate_shader_attribute_bindings(
|
||||
std::span<const OpenGlAttributeBinding> bindings) noexcept
|
||||
{
|
||||
if (bindings.empty()) {
|
||||
return pp::foundation::Status::invalid_argument("shader attribute binding catalog is empty");
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < bindings.size(); ++i) {
|
||||
if (bindings[i].name == nullptr || bindings[i].name[0] == '\0') {
|
||||
return pp::foundation::Status::invalid_argument("shader attribute binding has no name");
|
||||
}
|
||||
|
||||
for (std::size_t j = i + 1; j < bindings.size(); ++j) {
|
||||
if (bindings[j].name != nullptr && std::strcmp(bindings[i].name, bindings[j].name) == 0) {
|
||||
return pp::foundation::Status::invalid_argument("shader attribute binding name is duplicated");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
}
|
||||
19
src/renderer_gl/shader_bindings.h
Normal file
19
src/renderer_gl/shader_bindings.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
namespace pp::renderer::gl {
|
||||
|
||||
struct OpenGlAttributeBinding {
|
||||
const char* name = "";
|
||||
std::uint32_t location = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::span<const OpenGlAttributeBinding> panopainter_shader_attribute_bindings() noexcept;
|
||||
[[nodiscard]] pp::foundation::Status validate_shader_attribute_bindings(
|
||||
std::span<const OpenGlAttributeBinding> bindings) noexcept;
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "shader.h"
|
||||
#include "asset.h"
|
||||
#include "app.h"
|
||||
#include "renderer_gl/shader_bindings.h"
|
||||
|
||||
std::map<kShader, Shader> ShaderManager::m_shaders;
|
||||
Shader* ShaderManager::m_current;
|
||||
@@ -230,20 +231,11 @@ bool Shader::create(const std::string& vertex, const std::string& fragment)
|
||||
glDeleteShader(fs);
|
||||
|
||||
glLinkProgram(ps);
|
||||
if (glGetAttribLocation(ps, "pos") != -1)
|
||||
glBindAttribLocation(ps, 0, "pos");
|
||||
|
||||
if (glGetAttribLocation(ps, "uvs") != -1)
|
||||
glBindAttribLocation(ps, 1, "uvs");
|
||||
|
||||
if (glGetAttribLocation(ps, "uvs2") != -1)
|
||||
glBindAttribLocation(ps, 2, "uvs2");
|
||||
|
||||
if (glGetAttribLocation(ps, "col") != -1)
|
||||
glBindAttribLocation(ps, 3, "col");
|
||||
|
||||
if (glGetAttribLocation(ps, "nor") != -1)
|
||||
glBindAttribLocation(ps, 3, "nor");
|
||||
for (const auto& binding : pp::renderer::gl::panopainter_shader_attribute_bindings())
|
||||
{
|
||||
if (glGetAttribLocation(ps, binding.name) != -1)
|
||||
glBindAttribLocation(ps, static_cast<GLuint>(binding.location), binding.name);
|
||||
}
|
||||
|
||||
glLinkProgram(ps);
|
||||
glGetProgramiv(ps, GL_LINK_STATUS, &status);
|
||||
|
||||
Reference in New Issue
Block a user