2021-12-26 21:19:38 +01:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2013-05-22 23:07:07 +02:00
|
|
|
*
|
2021-12-26 21:19:38 +01:00
|
|
|
* ScummVM is the legal property of its developers, whose names
|
2013-05-22 23:07:07 +02:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2021-12-26 18:47:58 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2013-05-22 23:07:07 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2021-12-26 21:19:38 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2013-05-22 23:07:07 +02:00
|
|
|
* GNU General Public License for more details.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2013-05-22 23:07:07 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-05-22 23:07:07 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-09 08:32:42 +01:00
|
|
|
#ifndef GRAPHICS_OPENGL_SHADER_H
|
|
|
|
#define GRAPHICS_OPENGL_SHADER_H
|
2013-07-14 15:24:48 +02:00
|
|
|
|
2013-05-22 23:07:07 +02:00
|
|
|
#include "common/file.h"
|
|
|
|
#include "common/array.h"
|
2014-07-06 10:19:41 +02:00
|
|
|
#include "common/ptr.h"
|
2013-05-22 23:07:07 +02:00
|
|
|
|
|
|
|
#include "math/matrix3.h"
|
|
|
|
#include "math/matrix4.h"
|
|
|
|
#include "math/vector2d.h"
|
|
|
|
#include "math/vector3d.h"
|
|
|
|
#include "math/vector4d.h"
|
|
|
|
|
2022-06-29 21:08:20 +01:00
|
|
|
#include "graphics/opengl/debug.h"
|
2015-05-12 10:42:27 +02:00
|
|
|
#include "graphics/opengl/system_headers.h"
|
2013-05-22 23:07:07 +02:00
|
|
|
|
2016-01-04 22:23:01 +01:00
|
|
|
namespace OpenGL {
|
2013-05-22 23:07:07 +02:00
|
|
|
|
|
|
|
struct VertexAttrib {
|
2014-07-06 08:20:50 +02:00
|
|
|
VertexAttrib(uint32 idx, const char *name) :
|
|
|
|
_enabled(false), _idx(idx), _name(name), _vbo(0), _size(0),
|
2022-07-16 09:57:42 +02:00
|
|
|
_type(GL_FLOAT), _normalized(false), _stride(0), _pointer(0) {}
|
2013-05-22 23:07:07 +02:00
|
|
|
bool _enabled;
|
|
|
|
uint32 _idx;
|
|
|
|
Common::String _name;
|
|
|
|
GLuint _vbo;
|
|
|
|
GLint _size;
|
|
|
|
GLenum _type;
|
|
|
|
bool _normalized;
|
|
|
|
GLsizei _stride;
|
2022-07-16 09:57:42 +02:00
|
|
|
uintptr _pointer;
|
2013-05-22 23:07:07 +02:00
|
|
|
float _const[4];
|
|
|
|
};
|
|
|
|
|
2022-05-27 12:42:53 +02:00
|
|
|
class Shader {
|
2013-05-22 23:07:07 +02:00
|
|
|
typedef Common::HashMap<Common::String, GLint> UniformsMap;
|
|
|
|
|
|
|
|
public:
|
2022-09-30 20:54:09 +02:00
|
|
|
Shader();
|
2022-05-27 12:42:53 +02:00
|
|
|
~Shader();
|
2022-09-30 20:54:09 +02:00
|
|
|
Shader *clone() {
|
2022-05-27 12:42:53 +02:00
|
|
|
return new Shader(*this);
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2013-11-20 01:02:44 +01:00
|
|
|
void use(bool forceReload = false);
|
2013-05-22 23:07:07 +02:00
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform(const Common::String &uniform, const Math::Matrix4 &m) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniformMatrix4fv(pos, 1, GL_FALSE, m.getData()));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform(const Common::String &uniform, const Math::Matrix3 &m) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniformMatrix3fv(pos, 1, GL_FALSE, m.getData()));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform(const Common::String &uniform, const Math::Vector4d &v) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniform4fv(pos, 1, v.getData()));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform(const Common::String &uniform, const Math::Vector3d &v) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniform3fv(pos, 1, v.getData()));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform(const Common::String &uniform, const Math::Vector2d &v) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniform2fv(pos, 1, v.getData()));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform(const Common::String &uniform, unsigned int x) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniform1i(pos, x));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2023-04-04 19:57:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setUniform(const Common::String &uniform, const int size, const int *array) {
|
|
|
|
GLint pos = getUniformLocation(uniform);
|
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
|
|
|
GL_CALL(glUniform1iv(pos, size, array));
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Different name to avoid overload ambiguity
|
2022-06-28 21:09:26 +01:00
|
|
|
bool setUniform1f(const Common::String &uniform, float f) {
|
2013-05-22 23:07:07 +02:00
|
|
|
GLint pos = getUniformLocation(uniform);
|
2022-05-27 12:42:53 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
use();
|
2022-06-29 21:08:20 +01:00
|
|
|
GL_CALL(glUniform1f(pos, f));
|
2022-06-28 21:09:26 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-05-27 12:42:53 +02:00
|
|
|
}
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:26 +01:00
|
|
|
GLint getUniformLocation(const Common::String &uniform) const {
|
2013-05-22 23:07:07 +02:00
|
|
|
UniformsMap::iterator kv = _uniforms->find(uniform);
|
|
|
|
if (kv == _uniforms->end()) {
|
2022-06-29 21:08:20 +01:00
|
|
|
GLint ret;
|
|
|
|
GL_ASSIGN(ret, glGetUniformLocation(*_shaderNo, uniform.c_str()));
|
2013-05-22 23:07:07 +02:00
|
|
|
_uniforms->setVal(uniform, ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return kv->_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 00:12:31 +01:00
|
|
|
void enableVertexAttribute(const char *attrib, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer);
|
2013-05-22 23:07:07 +02:00
|
|
|
void enableVertexAttribute(const char *attrib, GLuint vbo, GLint size, GLenum type, GLboolean normalized, GLsizei stride, uint32 offset);
|
|
|
|
void disableVertexAttribute(const char *attrib, int size, const float *data);
|
|
|
|
template <int r>
|
|
|
|
void disableVertexAttribute(const char *attrib, const Math::Matrix<r,1> &m) {
|
|
|
|
disableVertexAttribute(attrib, r, m.getData());
|
|
|
|
}
|
2022-06-29 22:37:16 +01:00
|
|
|
bool addAttribute(const char *attrib);
|
2013-05-22 23:07:07 +02:00
|
|
|
VertexAttrib & getAttributeAt(uint32 idx);
|
|
|
|
VertexAttrib & getAttribute(const char *attrib);
|
|
|
|
|
|
|
|
static GLuint createBuffer(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage = GL_STATIC_DRAW);
|
2014-07-06 10:19:41 +02:00
|
|
|
static void freeBuffer(GLuint vbo);
|
2013-05-22 23:07:07 +02:00
|
|
|
|
2022-05-27 12:42:53 +02:00
|
|
|
/**
|
2022-09-30 20:54:09 +02:00
|
|
|
* Creates a shader object from files
|
2022-05-27 12:42:53 +02:00
|
|
|
*
|
|
|
|
* For shader files (used by games), we used to require GLSL 1.20, this is the default for compatGLSLVersion.
|
|
|
|
* The GLSL version is converted to GLSL ES version if needed.
|
|
|
|
*
|
|
|
|
* @param name The name of the shader for errors messages
|
|
|
|
* @param vertex The vertex shader code
|
|
|
|
* @param fragment The fragment shader code
|
|
|
|
* @param attributes The vertex attributes names for indexing
|
|
|
|
* @param compatGLSLVersion The GLSL version required: 0 for no preprocessing, 100 for GLSL 1.00 and so on
|
|
|
|
*
|
|
|
|
* @return the shader object created
|
|
|
|
*/
|
|
|
|
static Shader *fromFiles(const char *vertex, const char *fragment, const char *const *attributes, int compatGLSLVersion = 120);
|
|
|
|
static Shader *fromFiles(const char *shared, const char *const *attributes, int compatGLSLVersion = 120) {
|
|
|
|
return fromFiles(shared, shared, attributes, compatGLSLVersion);
|
2013-05-22 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:53 +02:00
|
|
|
/**
|
|
|
|
* Creates a shader object from strings
|
|
|
|
*
|
|
|
|
* Shader strings are usually included in backends and don't need preprocessing, this is the default for compatGLSLVersion.
|
|
|
|
* The GLSL version is converted to GLSL ES version if needed.
|
|
|
|
*
|
|
|
|
* @param name The name of the shader for errors messages
|
|
|
|
* @param vertex The vertex shader code
|
|
|
|
* @param fragment The fragment shader code
|
|
|
|
* @param attributes The vertex attributes names for indexing
|
|
|
|
* @param compatGLSLVersion The GLSL version required: 0 for no preprocessing, 100 for GLSL 1.00 and so on
|
|
|
|
*
|
|
|
|
* @return the shader object created
|
|
|
|
*/
|
|
|
|
static Shader *fromStrings(const Common::String &name, const char *vertex, const char *fragment, const char *const *attributes, int compatGLSLVersion = 0);
|
2013-05-22 23:07:07 +02:00
|
|
|
|
2022-09-30 20:54:09 +02:00
|
|
|
bool loadFromFiles(const char *vertex, const char *fragment, const char *const *attributes, int compatGLSLVersion = 120);
|
|
|
|
bool loadFromStrings(const Common::String &name, const char *vertex, const char *fragment, const char *const *attributes, int compatGLSLVersion = 0);
|
|
|
|
|
2022-10-08 19:55:22 +02:00
|
|
|
/**
|
|
|
|
* Creates a shader object from strings arrays
|
|
|
|
*
|
|
|
|
* Everything is loaded directly without any preprocessing.
|
|
|
|
*
|
|
|
|
* @param name The name of the shader for errors messages
|
|
|
|
* @param vertexCount The number of vertex shader code parts
|
|
|
|
* @param vertex The vertex shader code parts
|
|
|
|
* @param fragmentCount The number of fragment shader code parts
|
|
|
|
* @param fragment The fragment shader code parts
|
|
|
|
* @param attributes The vertex attributes names for indexing
|
|
|
|
*
|
|
|
|
* @return the loading status
|
|
|
|
*/
|
|
|
|
bool loadFromStringsArray(const Common::String &name,
|
|
|
|
size_t vertexCount, const char *const *vertex,
|
|
|
|
size_t fragmentCount, const char *const *fragment,
|
|
|
|
const char *const *attributes);
|
|
|
|
|
2016-01-16 08:02:13 +01:00
|
|
|
void unbind();
|
|
|
|
|
2022-09-30 20:54:09 +02:00
|
|
|
Common::String &getError() { return _error; }
|
|
|
|
bool hasError() { return !_error.empty(); }
|
|
|
|
|
2013-05-22 23:07:07 +02:00
|
|
|
private:
|
2022-09-30 20:54:09 +02:00
|
|
|
bool loadShader(const Common::String &name, GLuint vertexShader, GLuint fragmentShader, const char *const *attributes);
|
|
|
|
|
|
|
|
GLuint createCompatShader(const char *shaderSource, GLenum shaderType, const Common::String &name, int compatGLSLVersion);
|
2022-10-08 19:55:22 +02:00
|
|
|
GLuint createDirectShader(size_t shaderSourcesCount, const char *const *shaderSources, GLenum shaderType, const Common::String &name);
|
2022-09-30 20:54:09 +02:00
|
|
|
GLuint loadShaderFromFile(const char *base, const char *extension, GLenum shaderType, int compatGLSLVersion);
|
2014-07-06 10:19:41 +02:00
|
|
|
|
|
|
|
// Since this class is cloned using the implicit copy constructor,
|
|
|
|
// a reference counting pointer is used to ensure deletion of the OpenGL
|
|
|
|
// program upon destruction of the last clone.
|
|
|
|
Common::SharedPtr<GLuint> _shaderNo;
|
|
|
|
|
2013-05-22 23:07:07 +02:00
|
|
|
Common::String _name;
|
|
|
|
|
|
|
|
Common::Array<VertexAttrib> _attributes;
|
2014-07-06 10:19:41 +02:00
|
|
|
Common::SharedPtr<UniformsMap> _uniforms;
|
2016-01-16 08:02:13 +01:00
|
|
|
|
2022-05-27 12:42:53 +02:00
|
|
|
static Shader *_previousShader;
|
|
|
|
static uint32 previousNumAttributes;
|
2022-09-30 20:54:09 +02:00
|
|
|
|
|
|
|
Common::String _error;
|
2013-05-22 23:07:07 +02:00
|
|
|
};
|
|
|
|
|
2016-01-04 22:23:01 +01:00
|
|
|
} // End of namespace OpenGL
|
2013-07-14 15:24:48 +02:00
|
|
|
|
|
|
|
#endif
|