<commit_before>#include <array>
#include <iostream>

#include "GL/glew.h"
#include "RaZ/Math/Matrix.hpp"
#include "RaZ/Math/Vector.hpp"
#include "RaZ/Render/ShaderProgram.hpp"

namespace Raz {

ShaderProgram::ShaderProgram()
  : m_index{ glCreateProgram() } {}

ShaderProgram::ShaderProgram(ShaderProgram&& program) noexcept
  : m_index{ std::exchange(program.m_index, GL_INVALID_INDEX) },
    m_vertShader{ std::move(program.m_vertShader) },
    m_fragShader{ std::move(program.m_fragShader) },
    m_geomShader{ std::move(program.m_geomShader) },
    m_uniforms{ std::move(program.m_uniforms) } {}

void ShaderProgram::setVertexShader(VertexShader&& vertShader) {
  m_vertShader = std::move(vertShader);
  glAttachShader(m_index, m_vertShader.getIndex());
}

void ShaderProgram::setFragmentShader(FragmentShader&& fragShader) {
  m_fragShader = std::move(fragShader);
  glAttachShader(m_index, m_fragShader.getIndex());
}

void ShaderProgram::setGeometryShader(GeometryShader&& geomShader) {
  m_geomShader = std::make_unique<GeometryShader>(std::move(geomShader));
  glAttachShader(m_index, m_geomShader->getIndex());
}

void ShaderProgram::setShaders(VertexShader&& vertShader, FragmentShader&& fragShader) {
  setVertexShader(std::move(vertShader));
  setFragmentShader(std::move(fragShader));

  updateShaders();
}

void ShaderProgram::setShaders(VertexShader&& vertShader, FragmentShader&& fragShader, GeometryShader&& geomShader) {
  setVertexShader(std::move(vertShader));
  setFragmentShader(std::move(fragShader));
  setGeometryShader(std::move(geomShader));

  updateShaders();
}

void ShaderProgram::loadShaders() const {
  m_vertShader.load();
  m_fragShader.load();
  if (m_geomShader)
    m_geomShader->load();
}

void ShaderProgram::compileShaders() const {
  m_vertShader.compile();
  m_fragShader.compile();
  if (m_geomShader)
    m_geomShader->compile();
}

void ShaderProgram::link() const {
  glLinkProgram(m_index);

  if (!isLinked()) {
    std::array<char, 512> infoLog {};

    glGetProgramInfoLog(m_index, static_cast<int>(infoLog.size()), nullptr, infoLog.data());
    std::cerr << "Error: Shader program link failed (ID " << m_index << ").\n" << infoLog.data() << std::endl;
  }
}

bool ShaderProgram::isLinked() const {
  int success;
  glGetProgramiv(m_index, GL_LINK_STATUS, &success);
  
  return static_cast<bool>(success);
}

void ShaderProgram::use() const {
  glUseProgram(m_index);
}

void ShaderProgram::updateShaders() const {
  loadShaders();
  compileShaders();
  link();
  use();
}

void ShaderProgram::createUniform(const std::string& uniformName) {
  m_uniforms.emplace(uniformName, recoverUniformLocation(uniformName));
}

int ShaderProgram::recoverUniformLocation(const std::string& uniformName) const {
  const auto uniform = m_uniforms.find(uniformName);

  if (uniform != m_uniforms.cend())
    return uniform->second;

  return glGetUniformLocation(m_index, uniformName.c_str());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, int value) const {
  glUniform1i(uniformIndex, value);
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint8_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint16_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint32_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint64_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, float value) const {
  glUniform1f(uniformIndex, value);
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Vec3f& vec) const {
  glUniform3fv(uniformIndex, 1, vec.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Vec4f& vec) const {
  glUniform4fv(uniformIndex, 1, vec.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Mat3f& mat) const {
  glUniformMatrix3fv(uniformIndex, 1, GL_FALSE, mat.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Mat4f& mat) const {
  glUniformMatrix4fv(uniformIndex, 1, GL_FALSE, mat.getDataPtr());
}
<commit_msg>[Render/ShaderProgram] Added overloads to sendUniform()<commit_after>#include <array>
#include <iostream>

#include "GL/glew.h"
#include "RaZ/Math/Matrix.hpp"
#include "RaZ/Math/Vector.hpp"
#include "RaZ/Render/ShaderProgram.hpp"

namespace Raz {

ShaderProgram::ShaderProgram()
  : m_index{ glCreateProgram() } {}

ShaderProgram::ShaderProgram(ShaderProgram&& program) noexcept
  : m_index{ std::exchange(program.m_index, GL_INVALID_INDEX) },
    m_vertShader{ std::move(program.m_vertShader) },
    m_fragShader{ std::move(program.m_fragShader) },
    m_geomShader{ std::move(program.m_geomShader) },
    m_uniforms{ std::move(program.m_uniforms) } {}

void ShaderProgram::setVertexShader(VertexShader&& vertShader) {
  m_vertShader = std::move(vertShader);
  glAttachShader(m_index, m_vertShader.getIndex());
}

void ShaderProgram::setFragmentShader(FragmentShader&& fragShader) {
  m_fragShader = std::move(fragShader);
  glAttachShader(m_index, m_fragShader.getIndex());
}

void ShaderProgram::setGeometryShader(GeometryShader&& geomShader) {
  m_geomShader = std::make_unique<GeometryShader>(std::move(geomShader));
  glAttachShader(m_index, m_geomShader->getIndex());
}

void ShaderProgram::setShaders(VertexShader&& vertShader, FragmentShader&& fragShader) {
  setVertexShader(std::move(vertShader));
  setFragmentShader(std::move(fragShader));

  updateShaders();
}

void ShaderProgram::setShaders(VertexShader&& vertShader, FragmentShader&& fragShader, GeometryShader&& geomShader) {
  setVertexShader(std::move(vertShader));
  setFragmentShader(std::move(fragShader));
  setGeometryShader(std::move(geomShader));

  updateShaders();
}

void ShaderProgram::loadShaders() const {
  m_vertShader.load();
  m_fragShader.load();
  if (m_geomShader)
    m_geomShader->load();
}

void ShaderProgram::compileShaders() const {
  m_vertShader.compile();
  m_fragShader.compile();
  if (m_geomShader)
    m_geomShader->compile();
}

void ShaderProgram::link() const {
  glLinkProgram(m_index);

  if (!isLinked()) {
    std::array<char, 512> infoLog {};

    glGetProgramInfoLog(m_index, static_cast<int>(infoLog.size()), nullptr, infoLog.data());
    std::cerr << "Error: Shader program link failed (ID " << m_index << ").\n" << infoLog.data() << std::endl;
  }
}

bool ShaderProgram::isLinked() const {
  int success;
  glGetProgramiv(m_index, GL_LINK_STATUS, &success);

  return static_cast<bool>(success);
}

void ShaderProgram::use() const {
  glUseProgram(m_index);
}

void ShaderProgram::updateShaders() const {
  loadShaders();
  compileShaders();
  link();
  use();
}

void ShaderProgram::createUniform(const std::string& uniformName) {
  m_uniforms.emplace(uniformName, recoverUniformLocation(uniformName));
}

int ShaderProgram::recoverUniformLocation(const std::string& uniformName) const {
  const auto uniform = m_uniforms.find(uniformName);

  if (uniform != m_uniforms.cend())
    return uniform->second;

  return glGetUniformLocation(m_index, uniformName.c_str());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, int8_t value) const {
  glUniform1i(uniformIndex, static_cast<int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, int16_t value) const {
  glUniform1i(uniformIndex, static_cast<int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, int32_t value) const {
  glUniform1i(uniformIndex, static_cast<int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, int64_t value) const {
  glUniform1i(uniformIndex, static_cast<int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint8_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint16_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint32_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, uint64_t value) const {
  glUniform1ui(uniformIndex, static_cast<unsigned int>(value));
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, float value) const {
  glUniform1f(uniformIndex, value);
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Vec2f& vec) const {
  glUniform2fv(uniformIndex, 1, vec.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Vec3f& vec) const {
  glUniform3fv(uniformIndex, 1, vec.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Vec4f& vec) const {
  glUniform4fv(uniformIndex, 1, vec.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Mat2f& mat) const {
  glUniformMatrix2fv(uniformIndex, 1, GL_FALSE, mat.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Mat3f& mat) const {
  glUniformMatrix3fv(uniformIndex, 1, GL_FALSE, mat.getDataPtr());
}

template <>
void ShaderProgram::sendUniform(int uniformIndex, const Mat4f& mat) const {
  glUniformMatrix4fv(uniformIndex, 1, GL_FALSE, mat.getDataPtr());
}

{'fraction_non_alphanumeric': 0.13489827856025038, 'fraction_numerical': 0.007511737089201878, 'mean_word_length': 7.123728813559322, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6697471', 'n_tokens_mistral': 3190, 'n_tokens_neox': 3102, 'n_words': 572}