2020-09-16 20:14:35 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-08-30 20:19:37 +00:00
|
|
|
//
|
2020-09-16 20:14:35 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-08-30 20:19:37 +00:00
|
|
|
// Shader_GL21.cpp
|
|
|
|
//
|
|
|
|
// OpenGL 2.1 GLSL shader functions.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Shader_GL21.h"
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "Log.h"
|
2020-08-30 20:19:37 +00:00
|
|
|
#include "renderers/Renderer.h"
|
|
|
|
#include "resources/ResourceManager.h"
|
|
|
|
|
|
|
|
namespace Renderer
|
|
|
|
{
|
|
|
|
Renderer::Shader::Shader()
|
2022-01-16 17:18:28 +00:00
|
|
|
: mProgramID {0}
|
|
|
|
, shaderMVPMatrix {0}
|
2022-03-13 22:52:32 +00:00
|
|
|
, shaderPosition {0}
|
2022-01-16 17:18:28 +00:00
|
|
|
, shaderTextureCoord {0}
|
2022-03-13 22:52:32 +00:00
|
|
|
, shaderColor {0}
|
|
|
|
, shaderTextureSize {0}
|
2022-01-16 17:18:28 +00:00
|
|
|
, shaderOpacity {0}
|
2022-03-11 22:17:04 +00:00
|
|
|
, shaderSaturation {0}
|
2022-03-11 22:51:41 +00:00
|
|
|
, shaderDimming {0}
|
2022-03-13 22:52:32 +00:00
|
|
|
, shaderBGRAToRGBA {0}
|
|
|
|
, shaderFont {0}
|
|
|
|
, shaderPostProcessing {0}
|
2020-08-30 20:19:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Renderer::Shader::~Shader()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Delete the shader program when destroyed.
|
2020-08-30 20:19:37 +00:00
|
|
|
deleteProgram(mProgramID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Shader::loadShaderFile(const std::string& path, GLenum shaderType)
|
|
|
|
{
|
|
|
|
std::string preprocessorDefines;
|
|
|
|
std::string shaderCode;
|
|
|
|
|
|
|
|
// This will load the entire GLSL source code into the string variable.
|
2022-01-19 17:01:54 +00:00
|
|
|
const ResourceData& shaderData {ResourceManager::getInstance().getFileData(path)};
|
2020-09-16 20:14:35 +00:00
|
|
|
shaderCode.assign(reinterpret_cast<const char*>(shaderData.ptr.get()), shaderData.length);
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2022-03-13 22:52:32 +00:00
|
|
|
// Define the GLSL version.
|
|
|
|
#if defined(USE_OPENGLES)
|
|
|
|
preprocessorDefines = "#version 300 es\n";
|
|
|
|
#else
|
|
|
|
preprocessorDefines = "#version 330\n";
|
|
|
|
#endif
|
2020-09-04 16:59:19 +00:00
|
|
|
// Define the preprocessor constants that will let the shader compiler know whether
|
2020-08-30 20:19:37 +00:00
|
|
|
// the VERTEX or FRAGMENT portion of the code should be used.
|
|
|
|
if (shaderType == GL_VERTEX_SHADER)
|
|
|
|
preprocessorDefines += "#define VERTEX\n";
|
|
|
|
else if (shaderType == GL_FRAGMENT_SHADER)
|
|
|
|
preprocessorDefines += "#define FRAGMENT\n";
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
shaderVector.push_back(std::make_tuple(path, preprocessorDefines + shaderCode, shaderType));
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Renderer::Shader::createProgram()
|
|
|
|
{
|
|
|
|
GLint programSuccess;
|
|
|
|
|
|
|
|
mProgramID = glCreateProgram();
|
|
|
|
|
|
|
|
// Compile and attach all shaders that have been loaded.
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = shaderVector.cbegin(); it != shaderVector.cend(); ++it) {
|
2020-08-30 20:19:37 +00:00
|
|
|
GLuint currentShader = glCreateShader(std::get<2>(*it));
|
|
|
|
GLchar const* shaderCodePtr = std::get<1>(*it).c_str();
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
glShaderSource(currentShader, 1, reinterpret_cast<const GLchar**>(&shaderCodePtr),
|
|
|
|
nullptr);
|
2020-08-30 20:19:37 +00:00
|
|
|
glCompileShader(currentShader);
|
|
|
|
|
|
|
|
GLint shaderCompiled;
|
|
|
|
glGetShaderiv(currentShader, GL_COMPILE_STATUS, &shaderCompiled);
|
|
|
|
|
|
|
|
if (shaderCompiled != GL_TRUE) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogError) << "OpenGL error: Unable to compile shader " << currentShader << " ("
|
|
|
|
<< std::get<0>(*it) << ").";
|
2022-03-13 22:52:32 +00:00
|
|
|
printShaderInfoLog(currentShader, std::get<2>(*it), true);
|
2020-08-30 20:19:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-13 22:52:32 +00:00
|
|
|
else {
|
|
|
|
printShaderInfoLog(currentShader, std::get<2>(*it), false);
|
|
|
|
}
|
2020-08-30 20:19:37 +00:00
|
|
|
|
|
|
|
GL_CHECK_ERROR(glAttachShader(mProgramID, currentShader));
|
|
|
|
}
|
|
|
|
|
|
|
|
glLinkProgram(mProgramID);
|
|
|
|
|
|
|
|
glGetProgramiv(mProgramID, GL_LINK_STATUS, &programSuccess);
|
|
|
|
if (programSuccess != GL_TRUE) {
|
|
|
|
LOG(LogError) << "OpenGL error: Unable to link program " << mProgramID << ".";
|
|
|
|
printProgramInfoLog(mProgramID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-12 10:14:48 +00:00
|
|
|
getVariableLocations(mProgramID);
|
2022-03-13 22:52:32 +00:00
|
|
|
|
|
|
|
if (shaderPosition != -1)
|
|
|
|
GL_CHECK_ERROR(glEnableVertexAttribArray(shaderPosition));
|
|
|
|
|
|
|
|
if (shaderTextureCoord != -1)
|
|
|
|
GL_CHECK_ERROR(glEnableVertexAttribArray(shaderTextureCoord));
|
|
|
|
|
|
|
|
if (shaderColor != -1)
|
|
|
|
GL_CHECK_ERROR(glEnableVertexAttribArray(shaderColor));
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Shader::deleteProgram(GLuint programID)
|
|
|
|
{
|
|
|
|
GL_CHECK_ERROR(glDeleteProgram(programID));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Shader::getVariableLocations(GLuint programID)
|
|
|
|
{
|
2020-09-04 16:59:19 +00:00
|
|
|
// Some of the variable names are chosen to be compatible with the RetroArch GLSL shaders.
|
2020-09-12 10:14:48 +00:00
|
|
|
shaderMVPMatrix = glGetUniformLocation(mProgramID, "MVPMatrix");
|
2022-03-13 22:52:32 +00:00
|
|
|
shaderPosition = glGetAttribLocation(mProgramID, "positionAttrib");
|
2020-09-04 16:59:19 +00:00
|
|
|
shaderTextureCoord = glGetAttribLocation(mProgramID, "TexCoord");
|
2022-03-13 22:52:32 +00:00
|
|
|
shaderColor = glGetAttribLocation(mProgramID, "colorAttrib");
|
|
|
|
shaderTextureSize = glGetUniformLocation(mProgramID, "TextureSize");
|
2020-09-12 17:17:26 +00:00
|
|
|
shaderOpacity = glGetUniformLocation(mProgramID, "opacity");
|
2022-03-11 22:17:04 +00:00
|
|
|
shaderSaturation = glGetUniformLocation(mProgramID, "saturation");
|
2022-03-11 22:51:41 +00:00
|
|
|
shaderDimming = glGetUniformLocation(mProgramID, "dimming");
|
2022-03-11 22:17:04 +00:00
|
|
|
shaderBGRAToRGBA = glGetUniformLocation(mProgramID, "BGRAToRGBA");
|
2022-03-13 22:52:32 +00:00
|
|
|
shaderFont = glGetUniformLocation(mProgramID, "font");
|
2022-03-12 16:57:59 +00:00
|
|
|
shaderPostProcessing = glGetUniformLocation(mProgramID, "postProcessing");
|
2020-09-12 10:14:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void Renderer::Shader::setModelViewProjectionMatrix(glm::mat4 mvpMatrix)
|
2020-09-12 10:14:48 +00:00
|
|
|
{
|
2022-01-16 17:18:28 +00:00
|
|
|
if (shaderMVPMatrix != GL_INVALID_VALUE && shaderMVPMatrix != GL_INVALID_OPERATION)
|
2020-09-16 20:14:35 +00:00
|
|
|
GL_CHECK_ERROR(glUniformMatrix4fv(shaderMVPMatrix, 1, GL_FALSE,
|
2021-07-07 18:31:46 +00:00
|
|
|
reinterpret_cast<GLfloat*>(&mvpMatrix)));
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 22:52:32 +00:00
|
|
|
void Renderer::Shader::setAttribPointers()
|
2020-08-30 20:19:37 +00:00
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderPosition != -1)
|
|
|
|
GL_CHECK_ERROR(
|
|
|
|
glVertexAttribPointer(shaderPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
|
|
|
reinterpret_cast<const void*>(offsetof(Vertex, position))));
|
|
|
|
if (shaderTextureCoord != -1)
|
|
|
|
GL_CHECK_ERROR(
|
|
|
|
glVertexAttribPointer(shaderTextureCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
|
|
|
reinterpret_cast<const void*>(offsetof(Vertex, texture))));
|
|
|
|
|
|
|
|
if (shaderColor != -1)
|
|
|
|
GL_CHECK_ERROR(
|
|
|
|
glVertexAttribPointer(shaderColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex),
|
|
|
|
reinterpret_cast<const void*>(offsetof(Vertex, color))));
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 22:52:32 +00:00
|
|
|
void Renderer::Shader::setTextureSize(std::array<GLfloat, 2> shaderVec2)
|
2020-08-30 20:19:37 +00:00
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderTextureSize != -1)
|
|
|
|
GL_CHECK_ERROR(glUniform2f(shaderTextureSize, shaderVec2[0], shaderVec2[1]));
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 22:17:04 +00:00
|
|
|
void Renderer::Shader::setOpacity(GLfloat opacity)
|
2020-09-04 16:59:19 +00:00
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderOpacity != -1)
|
2022-03-11 22:17:04 +00:00
|
|
|
GL_CHECK_ERROR(glUniform1f(shaderOpacity, opacity));
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Shader::setSaturation(GLfloat saturation)
|
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderSaturation != -1)
|
2020-09-12 10:14:48 +00:00
|
|
|
GL_CHECK_ERROR(glUniform1f(shaderSaturation, saturation));
|
|
|
|
}
|
|
|
|
|
2022-03-11 22:51:41 +00:00
|
|
|
void Renderer::Shader::setDimming(GLfloat dimming)
|
2020-09-12 17:17:26 +00:00
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderDimming != -1)
|
2022-03-11 22:51:41 +00:00
|
|
|
GL_CHECK_ERROR(glUniform1f(shaderDimming, dimming));
|
2020-09-12 17:17:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 22:17:04 +00:00
|
|
|
void Renderer::Shader::setBGRAToRGBA(GLboolean BGRAToRGBA)
|
2020-09-12 10:14:48 +00:00
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderBGRAToRGBA != -1)
|
2022-03-11 22:17:04 +00:00
|
|
|
GL_CHECK_ERROR(glUniform1i(shaderBGRAToRGBA, BGRAToRGBA ? 1 : 0));
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 22:52:32 +00:00
|
|
|
void Renderer::Shader::setFont(GLboolean font)
|
|
|
|
{
|
|
|
|
if (shaderFont != -1)
|
|
|
|
GL_CHECK_ERROR(glUniform1i(shaderFont, font ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2022-03-12 16:57:59 +00:00
|
|
|
void Renderer::Shader::setPostProcessing(GLboolean postProcessing)
|
|
|
|
{
|
2022-03-13 22:52:32 +00:00
|
|
|
if (shaderPostProcessing != -1)
|
2022-03-12 16:57:59 +00:00
|
|
|
GL_CHECK_ERROR(glUniform1i(shaderPostProcessing, postProcessing ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
void Renderer::Shader::activateShaders()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Install the shader program.
|
2020-08-30 20:19:37 +00:00
|
|
|
GL_CHECK_ERROR(glUseProgram(mProgramID));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Shader::deactivateShaders()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Remove the shader program.
|
2020-08-30 20:19:37 +00:00
|
|
|
GL_CHECK_ERROR(glUseProgram(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Shader::printProgramInfoLog(GLuint programID)
|
|
|
|
{
|
|
|
|
if (glIsProgram(programID)) {
|
|
|
|
int logLength;
|
|
|
|
int maxLength;
|
|
|
|
|
|
|
|
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
std::vector<char> infoLog(maxLength);
|
|
|
|
|
|
|
|
glGetProgramInfoLog(programID, maxLength, &logLength, &infoLog.front());
|
|
|
|
|
|
|
|
if (logLength > 0) {
|
2021-08-17 20:51:28 +00:00
|
|
|
LOG(LogDebug) << "Renderer_GL21::printProgramInfoLog():\n"
|
2021-07-07 18:31:46 +00:00
|
|
|
<< std::string(infoLog.begin(), infoLog.end());
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "OpenGL error: " << programID << " is not a program.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 22:52:32 +00:00
|
|
|
void Renderer::Shader::printShaderInfoLog(GLuint shaderID, GLenum shaderType, bool error)
|
2020-08-30 20:19:37 +00:00
|
|
|
{
|
|
|
|
if (glIsShader(shaderID)) {
|
|
|
|
int logLength;
|
|
|
|
int maxLength;
|
|
|
|
|
|
|
|
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
std::vector<char> infoLog(maxLength);
|
|
|
|
|
2022-03-13 22:52:32 +00:00
|
|
|
if (infoLog.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
glGetShaderInfoLog(shaderID, maxLength, &logLength, &infoLog.front());
|
|
|
|
|
|
|
|
if (logLength > 0) {
|
2022-03-13 22:52:32 +00:00
|
|
|
LOG(LogDebug) << "Shader_GL21::printShaderInfoLog(): "
|
|
|
|
<< (error ? "Error" : "Warning") << " in "
|
2021-07-07 18:31:46 +00:00
|
|
|
<< (shaderType == GL_VERTEX_SHADER ? "VERTEX section:\n" :
|
|
|
|
"FRAGMENT section:\n")
|
|
|
|
<< std::string(infoLog.begin(), infoLog.end());
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "OpenGL error: " << shaderID << " is not a shader.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
} // namespace Renderer
|