SFML 3.0.2
Loading...
Searching...
No Matches
Shader.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Graphics/Export.hpp>
31
32#include <SFML/Graphics/Glsl.hpp>
33
34#include <SFML/Window/GlResource.hpp>
35
36#include <filesystem>
37#include <string>
38#include <string_view>
39#include <unordered_map>
40
41#include <cstddef>
42
43
44 namespace sf
45{
46 class InputStream;
47 class Texture;
48
53 class SFML_GRAPHICS_API Shader : GlResource
54{
55 public:
60 enum class Type
61 {
62 Vertex,
63 Geometry,
64 Fragment
65 };
66
74 struct CurrentTextureType
75 {
76 };
77
84 // NOLINTNEXTLINE(readability-identifier-naming)
85 static inline CurrentTextureType CurrentTexture;
86
96 Shader() = default;
97
102 ~Shader();
103
108 Shader(const Shader&) = delete;
109
114 Shader& operator=(const Shader&) = delete;
115
120 Shader(Shader&& source) noexcept;
121
126 Shader& operator=(Shader&& right) noexcept;
127
147 Shader(const std::filesystem::path& filename, Type type);
148
168 Shader(const std::filesystem::path& vertexShaderFilename, const std::filesystem::path& fragmentShaderFilename);
169
190 Shader(const std::filesystem::path& vertexShaderFilename,
191 const std::filesystem::path& geometryShaderFilename,
192 const std::filesystem::path& fragmentShaderFilename);
193
212 Shader(std::string_view shader, Type type);
213
233 Shader(std::string_view vertexShader, std::string_view fragmentShader);
234
255 Shader(std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader);
256
275 Shader(InputStream& stream, Type type);
276
296 Shader(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
297
318 Shader(InputStream& vertexShaderStream, InputStream& geometryShaderStream, InputStream& fragmentShaderStream);
319
339 [[nodiscard]] bool loadFromFile(const std::filesystem::path& filename, Type type);
340
360 [[nodiscard]] bool loadFromFile(const std::filesystem::path& vertexShaderFilename,
361 const std::filesystem::path& fragmentShaderFilename);
362
383 [[nodiscard]] bool loadFromFile(const std::filesystem::path& vertexShaderFilename,
384 const std::filesystem::path& geometryShaderFilename,
385 const std::filesystem::path& fragmentShaderFilename);
386
405 [[nodiscard]] bool loadFromMemory(std::string_view shader, Type type);
406
426 [[nodiscard]] bool loadFromMemory(std::string_view vertexShader, std::string_view fragmentShader);
427
448 [[nodiscard]] bool loadFromMemory(std::string_view vertexShader,
449 std::string_view geometryShader,
450 std::string_view fragmentShader);
451
470 [[nodiscard]] bool loadFromStream(InputStream& stream, Type type);
471
491 [[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
492
513 [[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream,
514 InputStream& geometryShaderStream,
515 InputStream& fragmentShaderStream);
516
524 void setUniform(const std::string& name, float x);
525
533 void setUniform(const std::string& name, Glsl::Vec2 vector);
534
542 void setUniform(const std::string& name, const Glsl::Vec3& vector);
543
560 void setUniform(const std::string& name, const Glsl::Vec4& vector);
561
569 void setUniform(const std::string& name, int x);
570
578 void setUniform(const std::string& name, Glsl::Ivec2 vector);
579
587 void setUniform(const std::string& name, const Glsl::Ivec3& vector);
588
604 void setUniform(const std::string& name, const Glsl::Ivec4& vector);
605
613 void setUniform(const std::string& name, bool x);
614
622 void setUniform(const std::string& name, Glsl::Bvec2 vector);
623
631 void setUniform(const std::string& name, const Glsl::Bvec3& vector);
632
640 void setUniform(const std::string& name, const Glsl::Bvec4& vector);
641
649 void setUniform(const std::string& name, const Glsl::Mat3& matrix);
650
658 void setUniform(const std::string& name, const Glsl::Mat4& matrix);
659
690 void setUniform(const std::string& name, const Texture& texture);
691
696 void setUniform(const std::string& name, const Texture&& texture) = delete;
697
719 void setUniform(const std::string& name, CurrentTextureType);
720
729 void setUniformArray(const std::string& name, const float* scalarArray, std::size_t length);
730
739 void setUniformArray(const std::string& name, const Glsl::Vec2* vectorArray, std::size_t length);
740
749 void setUniformArray(const std::string& name, const Glsl::Vec3* vectorArray, std::size_t length);
750
759 void setUniformArray(const std::string& name, const Glsl::Vec4* vectorArray, std::size_t length);
760
769 void setUniformArray(const std::string& name, const Glsl::Mat3* matrixArray, std::size_t length);
770
779 void setUniformArray(const std::string& name, const Glsl::Mat4* matrixArray, std::size_t length);
780
791 [[nodiscard]] unsigned int getNativeHandle() const;
792
814 static void bind(const Shader* shader);
815
826 [[nodiscard]] static bool isAvailable();
827
845 [[nodiscard]] static bool isGeometryAvailable();
846
847 private:
861 [[nodiscard]] bool compile(std::string_view vertexShaderCode,
862 std::string_view geometryShaderCode,
863 std::string_view fragmentShaderCode);
864
872 void bindTextures() const;
873
882 int getUniformLocation(const std::string& name);
883
891 struct UniformBinder;
892
894 // Types
896 using TextureTable = std::unordered_map<int, const Texture*>;
897 using UniformTable = std::unordered_map<std::string, int>;
898
900 // Member data
902 unsigned int m_shaderProgram{};
903 int m_currentTexture{-1};
904 TextureTable m_textures;
905 UniformTable m_uniforms;
906};
907
908} // namespace sf
909
910
GlResource.hpp
Glsl.hpp
Export.hpp
SFML_GRAPHICS_API
#define SFML_GRAPHICS_API
Definition Graphics/Export.hpp:42
sf::GlResource::GlResource
GlResource()
Default constructor.
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf::Shader::setUniformArray
void setUniformArray(const std::string &name, const Glsl::Mat4 *matrixArray, std::size_t length)
Specify values for mat4[] array uniform.
sf::Shader::bind
static void bind(const Shader *shader)
Bind a shader for rendering.
sf::Shader::operator=
Shader & operator=(Shader &&right) noexcept
Move assignment.
sf::Shader::Shader
Shader(InputStream &stream, Type type)
Construct from a shader stream.
sf::Shader::Shader
Shader(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &geometryShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Construct from vertex, geometry and fragment shader files.
sf::Shader::Shader
Shader(Shader &&source) noexcept
Move constructor.
sf::Shader::loadFromStream
bool loadFromStream(InputStream &stream, Type type)
Load the vertex, geometry or fragment shader from a custom stream.
sf::Shader::loadFromFile
bool loadFromFile(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &geometryShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Load the vertex, geometry and fragment shaders from files.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Ivec4 &vector)
Specify value for ivec4 uniform.
sf::Shader::loadFromStream
bool loadFromStream(InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
Load both the vertex and fragment shaders from custom streams.
sf::Shader::operator=
Shader & operator=(const Shader &)=delete
Deleted copy assignment.
sf::Shader::isGeometryAvailable
static bool isGeometryAvailable()
Tell whether or not the system supports geometry shaders.
sf::Shader::~Shader
~Shader()
Destructor.
sf::Shader::Shader
Shader(const std::filesystem::path &filename, Type type)
Construct from a shader file.
sf::Shader::loadFromFile
bool loadFromFile(const std::filesystem::path &filename, Type type)
Load the vertex, geometry or fragment shader from a file.
sf::Shader::setUniformArray
void setUniformArray(const std::string &name, const Glsl::Mat3 *matrixArray, std::size_t length)
Specify values for mat3[] array uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, Glsl::Vec2 vector)
Specify value for vec2 uniform.
sf::Shader::setUniformArray
void setUniformArray(const std::string &name, const float *scalarArray, std::size_t length)
Specify values for float[] array uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, const Texture &texture)
Specify a texture as sampler2D uniform.
sf::Shader::loadFromFile
bool loadFromFile(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Load both the vertex and fragment shaders from files.
sf::Shader::setUniform
void setUniform(const std::string &name, Glsl::Bvec2 vector)
Specify value for bvec2 uniform.
sf::Shader::loadFromMemory
bool loadFromMemory(std::string_view vertexShader, std::string_view fragmentShader)
Load both the vertex and fragment shaders from source codes in memory.
sf::Shader::Shader
Shader(InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
Construct from vertex and fragment shader streams.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Ivec3 &vector)
Specify value for ivec3 uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, Glsl::Ivec2 vector)
Specify value for ivec2 uniform.
sf::Shader::loadFromStream
bool loadFromStream(InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
Load the vertex, geometry and fragment shaders from custom streams.
sf::Shader::Shader
Shader(const Shader &)=delete
Deleted copy constructor.
sf::Shader::setUniformArray
void setUniformArray(const std::string &name, const Glsl::Vec4 *vectorArray, std::size_t length)
Specify values for vec4[] array uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Vec3 &vector)
Specify value for vec3 uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Bvec3 &vector)
Specify value for bvec3 uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, CurrentTextureType)
Specify current texture as sampler2D uniform.
sf::Shader::setUniformArray
void setUniformArray(const std::string &name, const Glsl::Vec2 *vectorArray, std::size_t length)
Specify values for vec2[] array uniform.
sf::Shader::Shader
Shader()=default
Default constructor.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Vec4 &vector)
Specify value for vec4 uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, float x)
Specify value for float uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Mat3 &matrix)
Specify value for mat3 matrix.
sf::Shader::getNativeHandle
unsigned int getNativeHandle() const
Get the underlying OpenGL handle of the shader.
sf::Shader::Shader
Shader(std::string_view vertexShader, std::string_view fragmentShader)
Construct from vertex and fragment shaders in memory.
sf::Shader::CurrentTexture
static CurrentTextureType CurrentTexture
Represents the texture of the object being drawn.
Definition Shader.hpp:85
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Bvec4 &vector)
Specify value for bvec4 uniform.
sf::Shader::setUniform
void setUniform(const std::string &name, const Glsl::Mat4 &matrix)
Specify value for mat4 matrix.
sf::Shader::setUniform
void setUniform(const std::string &name, const Texture &&texture)=delete
Disallow setting from a temporary texture.
sf::Shader::isAvailable
static bool isAvailable()
Tell whether or not the system supports shaders.
sf::Shader::loadFromMemory
bool loadFromMemory(std::string_view shader, Type type)
Load the vertex, geometry or fragment shader from a source code in memory.
sf::Shader::setUniform
void setUniform(const std::string &name, int x)
Specify value for int uniform.
sf::Shader::setUniformArray
void setUniformArray(const std::string &name, const Glsl::Vec3 *vectorArray, std::size_t length)
Specify values for vec3[] array uniform.
sf::Shader::Shader
Shader(std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader)
Construct from vertex, geometry and fragment shaders in memory.
sf::Shader::Shader
Shader(std::string_view shader, Type type)
Construct from shader in memory.
sf::Shader::Shader
Shader(InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
Construct from vertex, geometry and fragment shader streams.
sf::Shader::setUniform
void setUniform(const std::string &name, bool x)
Specify value for bool uniform.
sf::Shader::Shader
Shader(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Construct from vertex and fragment shader files.
sf::Shader::loadFromMemory
bool loadFromMemory(std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader)
Load the vertex, geometry and fragment shaders from source codes in memory.
sf::Shader::Type
Type
Types of shaders.
Definition Shader.hpp:61
sf::Texture
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56
sf::Glsl::Mat3
ImplementationDefined Mat3
3x3 float matrix (mat3 in GLSL)
Definition Glsl.hpp:152
sf::Glsl::Ivec2
Vector2< int > Ivec2
2D int vector (ivec2 in GLSL)
Definition Glsl.hpp:67
sf::Glsl::Vec3
Vector3< float > Vec3
3D float vector (vec3 in GLSL)
Definition Glsl.hpp:79
sf::Glsl::Ivec4
ImplementationDefined Ivec4
4D int vector (ivec4 in GLSL)
Definition Glsl.hpp:121
sf::Glsl::Vec2
Vector2< float > Vec2
2D float vector (vec2 in GLSL)
Definition Glsl.hpp:61
sf::Glsl::Bvec2
Vector2< bool > Bvec2
2D bool vector (bvec2 in GLSL)
Definition Glsl.hpp:73
sf::Glsl::Ivec3
Vector3< int > Ivec3
3D int vector (ivec3 in GLSL)
Definition Glsl.hpp:85
sf::Glsl::Bvec3
Vector3< bool > Bvec3
3D bool vector (bvec3 in GLSL)
Definition Glsl.hpp:91
sf::Glsl::Vec4
ImplementationDefined Vec4
4D float vector (vec4 in GLSL)
Definition Glsl.hpp:107
sf::Glsl::Mat4
ImplementationDefined Mat4
4x4 float matrix (mat4 in GLSL)
Definition Glsl.hpp:178
sf::Glsl::Bvec4
ImplementationDefined Bvec4
4D bool vector (bvec4 in GLSL)
Definition Glsl.hpp:127
sf
Definition AudioResource.hpp:36
sf::Shader::CurrentTextureType
Special type that can be passed to setUniform(), and that represents the texture of the object being ...
Definition Shader.hpp:75
sf::Vertex
Point with color and texture coordinates.
Definition Vertex.hpp:44