Added new methods to set uniforms in sf::Shader

Implements a new design for the shader uniform API.
* Added Shader::setUniform() and Shader::setUniformArray() overloads for the following types:
  -> scalars: float, int, bool
  -> vectors: 2D, 3D, 4D
  -> matrices: 3x3, 4x4
  -> arrays of basic types
  -> samplers (sf::Texture)
  -> conversions for SFML types (sf::Transform, sf::Color)
* Added sf::Glsl namespace with GLSL-equivalent types
* Deprecated Shader::setParameter() overloads

Other related changes:
* Refactored sf::Shader internals to avoid code duplication
* Improved documentation
* Added SFML_DEPRECATED macro to Doxyfile
* Defined _SCL_SECURE_NO_WARNINGS to disable std::copy() warnings on MSVC
This commit is contained in:
Jan Haller 2015-10-12 07:54:21 +02:00
parent fcb05fb975
commit 9c5c750e60
10 changed files with 1272 additions and 321 deletions

View file

@ -11,6 +11,9 @@ set(SRC
${INCROOT}/Export.hpp
${SRCROOT}/Font.cpp
${INCROOT}/Font.hpp
${SRCROOT}/Glsl.cpp
${INCROOT}/Glsl.hpp
${INCROOT}/Glsl.inl
${INCROOT}/Glyph.hpp
${SRCROOT}/GLCheck.cpp
${SRCROOT}/GLCheck.hpp

View file

@ -172,6 +172,15 @@
#define GLEXT_glUniform3f glUniform3fARB
#define GLEXT_glUniform4f glUniform4fARB
#define GLEXT_glUniform1i glUniform1iARB
#define GLEXT_glUniform2i glUniform2iARB
#define GLEXT_glUniform3i glUniform3iARB
#define GLEXT_glUniform4i glUniform4iARB
#define GLEXT_glUniform1fv glUniform1fvARB
#define GLEXT_glUniform2fv glUniform2fvARB
#define GLEXT_glUniform2iv glUniform2ivARB
#define GLEXT_glUniform3fv glUniform3fvARB
#define GLEXT_glUniform4fv glUniform4fvARB
#define GLEXT_glUniformMatrix3fv glUniformMatrix3fvARB
#define GLEXT_glUniformMatrix4fv glUniformMatrix4fvARB
#define GLEXT_glGetObjectParameteriv glGetObjectParameterivARB
#define GLEXT_glGetInfoLog glGetInfoLogARB

View file

@ -0,0 +1,86 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Glsl.hpp>
#include <algorithm>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void copyMatrix(const Transform& source, Matrix<3, 3>& dest)
{
const float* from = source.getMatrix(); // 4x4
float* to = dest.array; // 3x3
// Use only left-upper 3x3 block (for a 2D transform)
to[0] = from[ 0]; to[1] = from[ 1]; to[2] = from[ 3];
to[3] = from[ 4]; to[4] = from[ 5]; to[5] = from[ 7];
to[6] = from[12]; to[7] = from[13]; to[8] = from[15];
}
////////////////////////////////////////////////////////////
void copyMatrix(const Transform& source, Matrix<4, 4>& dest)
{
// Adopt 4x4 matrix as-is
copyMatrix(source.getMatrix(), 4 * 4, dest.array);
}
////////////////////////////////////////////////////////////
void copyMatrix(const float* source, std::size_t elements, float* dest)
{
std::copy(source, source + elements, dest);
}
////////////////////////////////////////////////////////////
void copyVector(const Color& source, Vector4<float>& dest)
{
dest.x = source.r / 255.f;
dest.y = source.g / 255.f;
dest.z = source.b / 255.f;
dest.w = source.a / 255.f;
}
////////////////////////////////////////////////////////////
void copyVector(const Color& source, Vector4<int>& dest)
{
dest.x = static_cast<int>(source.r);
dest.y = static_cast<int>(source.g);
dest.z = static_cast<int>(source.b);
dest.w = static_cast<int>(source.a);
}
} // namespace priv
} // namespace sf

View file

@ -28,6 +28,8 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Transform.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <SFML/Window/Context.hpp>
#include <SFML/System/InputStream.hpp>
@ -146,6 +148,57 @@ namespace
return available;
}
// Transforms an array of 2D vectors into a contiguous array of scalars
template <typename T>
std::vector<T> flatten(const sf::Vector2<T>* vectorArray, std::size_t length)
{
const std::size_t vectorSize = 2;
std::vector<T> contiguous(vectorSize * length);
for (std::size_t i = 0; i < length; ++i)
{
contiguous[vectorSize * i] = vectorArray[i].x;
contiguous[vectorSize * i + 1] = vectorArray[i].y;
}
return contiguous;
}
// Transforms an array of 3D vectors into a contiguous array of scalars
template <typename T>
std::vector<T> flatten(const sf::Vector3<T>* vectorArray, std::size_t length)
{
const std::size_t vectorSize = 3;
std::vector<T> contiguous(vectorSize * length);
for (std::size_t i = 0; i < length; ++i)
{
contiguous[vectorSize * i] = vectorArray[i].x;
contiguous[vectorSize * i + 1] = vectorArray[i].y;
contiguous[vectorSize * i + 2] = vectorArray[i].z;
}
return contiguous;
}
// Transforms an array of 4D vectors into a contiguous array of scalars
template <typename T>
std::vector<T> flatten(const sf::priv::Vector4<T>* vectorArray, std::size_t length)
{
const std::size_t vectorSize = 4;
std::vector<T> contiguous(vectorSize * length);
for (std::size_t i = 0; i < length; ++i)
{
contiguous[vectorSize * i] = vectorArray[i].x;
contiguous[vectorSize * i + 1] = vectorArray[i].y;
contiguous[vectorSize * i + 2] = vectorArray[i].z;
contiguous[vectorSize * i + 3] = vectorArray[i].w;
}
return contiguous;
}
}
@ -155,12 +208,55 @@ namespace sf
Shader::CurrentTextureType Shader::CurrentTexture;
////////////////////////////////////////////////////////////
struct Shader::UniformBinder : private NonCopyable
{
////////////////////////////////////////////////////////////
/// \brief Constructor: set up state before uniform is set
///
////////////////////////////////////////////////////////////
UniformBinder(Shader& shader, const std::string& name) :
savedProgram(0),
currentProgram(castToGlHandle(shader.m_shaderProgram)),
location(-1)
{
if (currentProgram)
{
ensureGlContext();
// Enable program object
glCheck(savedProgram = GLEXT_glGetHandle(GLEXT_GL_PROGRAM_OBJECT));
if (currentProgram != savedProgram)
glCheck(GLEXT_glUseProgramObject(currentProgram));
// Store uniform location for further use outside constructor
location = shader.getUniformLocation(name);
}
}
////////////////////////////////////////////////////////////
/// \brief Destructor: restore state after uniform is set
///
////////////////////////////////////////////////////////////
~UniformBinder()
{
// Disable program object
if (currentProgram && (currentProgram != savedProgram))
glCheck(GLEXT_glUseProgramObject(savedProgram));
}
GLEXT_GLhandle savedProgram; ///< Handle to the previously active program object
GLEXT_GLhandle currentProgram; ///< Handle to the program object of the modified sf::Shader instance
GLint location; ///< Uniform location, used by the surrounding sf::Shader code
};
////////////////////////////////////////////////////////////
Shader::Shader() :
m_shaderProgram (0),
m_currentTexture(-1),
m_textures (),
m_params ()
m_uniforms ()
{
}
@ -282,160 +378,132 @@ bool Shader::loadFromStream(InputStream& vertexShaderStream, InputStream& fragme
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x)
void Shader::setUniform(const std::string& name, float x)
{
if (m_shaderProgram)
{
ensureGlContext();
// Enable program
GLEXT_GLhandle program;
glCheck(program = GLEXT_glGetHandle(GLEXT_GL_PROGRAM_OBJECT));
glCheck(GLEXT_glUseProgramObject(castToGlHandle(m_shaderProgram)));
// Get parameter location and assign it new values
GLint location = getParamLocation(name);
if (location != -1)
{
glCheck(GLEXT_glUniform1f(location, x));
}
// Disable program
glCheck(GLEXT_glUseProgramObject(program));
}
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform1f(binder.location, x));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x, float y)
void Shader::setUniform(const std::string& name, const Glsl::Vec2& v)
{
if (m_shaderProgram)
{
ensureGlContext();
// Enable program
GLEXT_GLhandle program;
glCheck(program = GLEXT_glGetHandle(GLEXT_GL_PROGRAM_OBJECT));
glCheck(GLEXT_glUseProgramObject(castToGlHandle(m_shaderProgram)));
// Get parameter location and assign it new values
GLint location = getParamLocation(name);
if (location != -1)
{
glCheck(GLEXT_glUniform2f(location, x, y));
}
// Disable program
glCheck(GLEXT_glUseProgramObject(program));
}
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform2f(binder.location, v.x, v.y));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x, float y, float z)
void Shader::setUniform(const std::string& name, const Glsl::Vec3& v)
{
if (m_shaderProgram)
{
ensureGlContext();
// Enable program
GLEXT_GLhandle program;
glCheck(program = GLEXT_glGetHandle(GLEXT_GL_PROGRAM_OBJECT));
glCheck(GLEXT_glUseProgramObject(castToGlHandle(m_shaderProgram)));
// Get parameter location and assign it new values
GLint location = getParamLocation(name);
if (location != -1)
{
glCheck(GLEXT_glUniform3f(location, x, y, z));
}
// Disable program
glCheck(GLEXT_glUseProgramObject(program));
}
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform3f(binder.location, v.x, v.y, v.z));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x, float y, float z, float w)
void Shader::setUniform(const std::string& name, const Glsl::Vec4& v)
{
if (m_shaderProgram)
{
ensureGlContext();
// Enable program
GLEXT_GLhandle program;
glCheck(program = GLEXT_glGetHandle(GLEXT_GL_PROGRAM_OBJECT));
glCheck(GLEXT_glUseProgramObject(castToGlHandle(m_shaderProgram)));
// Get parameter location and assign it new values
GLint location = getParamLocation(name);
if (location != -1)
{
glCheck(GLEXT_glUniform4f(location, x, y, z, w));
}
// Disable program
glCheck(GLEXT_glUseProgramObject(program));
}
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform4f(binder.location, v.x, v.y, v.z, v.w));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Vector2f& v)
void Shader::setUniform(const std::string& name, int x)
{
setParameter(name, v.x, v.y);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform1i(binder.location, x));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Vector3f& v)
void Shader::setUniform(const std::string& name, const Glsl::Ivec2& v)
{
setParameter(name, v.x, v.y, v.z);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform2i(binder.location, v.x, v.y));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Color& color)
void Shader::setUniform(const std::string& name, const Glsl::Ivec3& v)
{
setParameter(name, color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform3i(binder.location, v.x, v.y, v.z));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Transform& transform)
void Shader::setUniform(const std::string& name, const Glsl::Ivec4& v)
{
if (m_shaderProgram)
{
ensureGlContext();
// Enable program
GLEXT_GLhandle program;
glCheck(program = GLEXT_glGetHandle(GLEXT_GL_PROGRAM_OBJECT));
glCheck(GLEXT_glUseProgramObject(castToGlHandle(m_shaderProgram)));
// Get parameter location and assign it new values
GLint location = getParamLocation(name);
if (location != -1)
{
glCheck(GLEXT_glUniformMatrix4fv(location, 1, GL_FALSE, transform.getMatrix()));
}
// Disable program
glCheck(GLEXT_glUseProgramObject(program));
}
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform4i(binder.location, v.x, v.y, v.z, v.w));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Texture& texture)
void Shader::setUniform(const std::string& name, bool x)
{
setUniform(name, static_cast<int>(x));
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Bvec2& v)
{
setUniform(name, Glsl::Ivec2(v));
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Bvec3& v)
{
setUniform(name, Glsl::Ivec3(v));
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Bvec4& v)
{
setUniform(name, Glsl::Ivec4(v));
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Mat3& matrix)
{
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniformMatrix3fv(binder.location, 1, GL_FALSE, matrix.array));
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Mat4& matrix)
{
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniformMatrix4fv(binder.location, 1, GL_FALSE, matrix.array));
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Texture& texture)
{
if (m_shaderProgram)
{
ensureGlContext();
// Find the location of the variable in the shader
int location = getParamLocation(name);
int location = getUniformLocation(name);
if (location != -1)
{
// Store the location -> texture mapping
@ -463,18 +531,160 @@ void Shader::setParameter(const std::string& name, const Texture& texture)
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, CurrentTextureType)
void Shader::setUniform(const std::string& name, CurrentTextureType)
{
if (m_shaderProgram)
{
ensureGlContext();
// Find the location of the variable in the shader
m_currentTexture = getParamLocation(name);
m_currentTexture = getUniformLocation(name);
}
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const float* scalarArray, std::size_t length)
{
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform1fv(binder.location, length, scalarArray));
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Vec2* vectorArray, std::size_t length)
{
std::vector<float> contiguous = flatten(vectorArray, length);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform2fv(binder.location, length, &contiguous[0]));
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Vec3* vectorArray, std::size_t length)
{
std::vector<float> contiguous = flatten(vectorArray, length);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform3fv(binder.location, length, &contiguous[0]));
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Vec4* vectorArray, std::size_t length)
{
std::vector<float> contiguous = flatten(vectorArray, length);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniform4fv(binder.location, length, &contiguous[0]));
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Mat3* matrixArray, std::size_t length)
{
const std::size_t matrixSize = 3 * 3;
std::vector<float> contiguous(matrixSize * length);
for (std::size_t i = 0; i < length; ++i)
priv::copyMatrix(matrixArray[i].array, matrixSize, &contiguous[matrixSize * i]);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniformMatrix3fv(binder.location, length, GL_FALSE, &contiguous[0]));
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Mat4* matrixArray, std::size_t length)
{
const std::size_t matrixSize = 4 * 4;
std::vector<float> contiguous(matrixSize * length);
for (std::size_t i = 0; i < length; ++i)
priv::copyMatrix(matrixArray[i].array, matrixSize, &contiguous[matrixSize * i]);
UniformBinder binder(*this, name);
if (binder.location != -1)
glCheck(GLEXT_glUniformMatrix4fv(binder.location, length, GL_FALSE, &contiguous[0]));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x)
{
setUniform(name, x);
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x, float y)
{
setUniform(name, Glsl::Vec2(x, y));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x, float y, float z)
{
setUniform(name, Glsl::Vec3(x, y, z));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x, float y, float z, float w)
{
setUniform(name, Glsl::Vec4(x, y, z, w));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Vector2f& v)
{
setUniform(name, v);
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Vector3f& v)
{
setUniform(name, v);
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Color& color)
{
setUniform(name, Glsl::Vec4(color));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Transform& transform)
{
setUniform(name, Glsl::Mat4(transform));
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, const Texture& texture)
{
setUniform(name, texture);
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, CurrentTextureType)
{
setUniform(name, CurrentTexture);
}
////////////////////////////////////////////////////////////
unsigned int Shader::getNativeHandle() const
{
@ -550,7 +760,7 @@ bool Shader::compile(const char* vertexShaderCode, const char* fragmentShaderCod
// Reset the internal state
m_currentTexture = -1;
m_textures.clear();
m_params.clear();
m_uniforms.clear();
// Create the program
GLEXT_GLhandle shaderProgram;
@ -657,11 +867,11 @@ void Shader::bindTextures() const
////////////////////////////////////////////////////////////
int Shader::getParamLocation(const std::string& name)
int Shader::getUniformLocation(const std::string& name)
{
// Check the cache
ParamTable::const_iterator it = m_params.find(name);
if (it != m_params.end())
UniformTable::const_iterator it = m_uniforms.find(name);
if (it != m_uniforms.end())
{
// Already in cache, return it
return it->second;
@ -670,7 +880,7 @@ int Shader::getParamLocation(const std::string& name)
{
// Not in cache, request the location from OpenGL
int location = GLEXT_glGetUniformLocation(castToGlHandle(m_shaderProgram), name.c_str());
m_params.insert(std::make_pair(name, location));
m_uniforms.insert(std::make_pair(name, location));
if (location == -1)
err() << "Parameter \"" << name << "\" not found in shader" << std::endl;
@ -747,6 +957,138 @@ bool Shader::loadFromStream(InputStream& vertexShaderStream, InputStream& fragme
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, float x)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Vec2& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Vec3& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Vec4& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, int x)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Ivec2& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Ivec3& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Ivec4& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, bool x)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Bvec2& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Bvec3& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Bvec4& v)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Mat3& matrix)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Glsl::Mat4& matrix)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Texture& texture)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, CurrentTextureType)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const float* scalarArray, std::size_t length)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Vec2* vectorArray, std::size_t length)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Vec3* vectorArray, std::size_t length)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Vec4* vectorArray, std::size_t length)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Mat3* matrixArray, std::size_t length)
{
}
////////////////////////////////////////////////////////////
void Shader::setUniformArray(const std::string& name, const Glsl::Mat4* matrixArray, std::size_t length)
{
}
////////////////////////////////////////////////////////////
void Shader::setParameter(const std::string& name, float x)
{