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-2024 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
31
33
35
36#include <filesystem>
37#include <string>
38#include <string_view>
39#include <unordered_map>
40
41#include <cstddef>
42
43
44namespace sf
45{
46class InputStream;
47class Texture;
48
54{
55public:
60 enum class Type
61 {
62 Vertex,
63 Geometry,
64 Fragment
65 };
66
75 {
76 };
77
84 // NOLINTNEXTLINE(readability-identifier-naming)
86
96 Shader() = default;
97
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
847private:
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
#define SFML_GRAPHICS_API
Base class for classes that require an OpenGL context.
Abstract class for custom file input streams.
Shader class (vertex, geometry and fragment)
Definition Shader.hpp:54
void setUniformArray(const std::string &name, const Glsl::Mat4 *matrixArray, std::size_t length)
Specify values for mat4[] array uniform.
static void bind(const Shader *shader)
Bind a shader for rendering.
Shader & operator=(Shader &&right) noexcept
Move assignment.
Shader(InputStream &stream, Type type)
Construct from a shader stream.
Shader(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &geometryShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Construct from vertex, geometry and fragment shader files.
Shader(Shader &&source) noexcept
Move constructor.
bool loadFromStream(InputStream &stream, Type type)
Load the vertex, geometry or fragment shader from a custom stream.
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.
void setUniform(const std::string &name, const Glsl::Ivec4 &vector)
Specify value for ivec4 uniform.
bool loadFromStream(InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
Load both the vertex and fragment shaders from custom streams.
Shader & operator=(const Shader &)=delete
Deleted copy assignment.
static bool isGeometryAvailable()
Tell whether or not the system supports geometry shaders.
~Shader()
Destructor.
Shader(const std::filesystem::path &filename, Type type)
Construct from a shader file.
bool loadFromFile(const std::filesystem::path &filename, Type type)
Load the vertex, geometry or fragment shader from a file.
void setUniformArray(const std::string &name, const Glsl::Mat3 *matrixArray, std::size_t length)
Specify values for mat3[] array uniform.
void setUniform(const std::string &name, Glsl::Vec2 vector)
Specify value for vec2 uniform.
void setUniformArray(const std::string &name, const float *scalarArray, std::size_t length)
Specify values for float[] array uniform.
void setUniform(const std::string &name, const Texture &texture)
Specify a texture as sampler2D uniform.
bool loadFromFile(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Load both the vertex and fragment shaders from files.
void setUniform(const std::string &name, Glsl::Bvec2 vector)
Specify value for bvec2 uniform.
bool loadFromMemory(std::string_view vertexShader, std::string_view fragmentShader)
Load both the vertex and fragment shaders from source codes in memory.
Shader(InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
Construct from vertex and fragment shader streams.
void setUniform(const std::string &name, const Glsl::Ivec3 &vector)
Specify value for ivec3 uniform.
void setUniform(const std::string &name, Glsl::Ivec2 vector)
Specify value for ivec2 uniform.
bool loadFromStream(InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
Load the vertex, geometry and fragment shaders from custom streams.
Shader(const Shader &)=delete
Deleted copy constructor.
void setUniformArray(const std::string &name, const Glsl::Vec4 *vectorArray, std::size_t length)
Specify values for vec4[] array uniform.
void setUniform(const std::string &name, const Glsl::Vec3 &vector)
Specify value for vec3 uniform.
void setUniform(const std::string &name, const Glsl::Bvec3 &vector)
Specify value for bvec3 uniform.
void setUniform(const std::string &name, CurrentTextureType)
Specify current texture as sampler2D uniform.
void setUniformArray(const std::string &name, const Glsl::Vec2 *vectorArray, std::size_t length)
Specify values for vec2[] array uniform.
Shader()=default
Default constructor.
void setUniform(const std::string &name, const Glsl::Vec4 &vector)
Specify value for vec4 uniform.
void setUniform(const std::string &name, float x)
Specify value for float uniform.
void setUniform(const std::string &name, const Glsl::Mat3 &matrix)
Specify value for mat3 matrix.
unsigned int getNativeHandle() const
Get the underlying OpenGL handle of the shader.
Shader(std::string_view vertexShader, std::string_view fragmentShader)
Construct from vertex and fragment shaders in memory.
static CurrentTextureType CurrentTexture
Represents the texture of the object being drawn.
Definition Shader.hpp:85
void setUniform(const std::string &name, const Glsl::Bvec4 &vector)
Specify value for bvec4 uniform.
void setUniform(const std::string &name, const Glsl::Mat4 &matrix)
Specify value for mat4 matrix.
void setUniform(const std::string &name, const Texture &&texture)=delete
Disallow setting from a temporary texture.
static bool isAvailable()
Tell whether or not the system supports shaders.
bool loadFromMemory(std::string_view shader, Type type)
Load the vertex, geometry or fragment shader from a source code in memory.
void setUniform(const std::string &name, int x)
Specify value for int uniform.
void setUniformArray(const std::string &name, const Glsl::Vec3 *vectorArray, std::size_t length)
Specify values for vec3[] array uniform.
Shader(std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader)
Construct from vertex, geometry and fragment shaders in memory.
Shader(std::string_view shader, Type type)
Construct from shader in memory.
Shader(InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
Construct from vertex, geometry and fragment shader streams.
void setUniform(const std::string &name, bool x)
Specify value for bool uniform.
Shader(const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &fragmentShaderFilename)
Construct from vertex and fragment shader files.
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.
Type
Types of shaders.
Definition Shader.hpp:61
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56
ImplementationDefined Mat3
3x3 float matrix (mat3 in GLSL)
Definition Glsl.hpp:152
ImplementationDefined Ivec4
4D int vector (ivec4 in GLSL)
Definition Glsl.hpp:121
ImplementationDefined Vec4
4D float vector (vec4 in GLSL)
Definition Glsl.hpp:107
ImplementationDefined Mat4
4x4 float matrix (mat4 in GLSL)
Definition Glsl.hpp:178
ImplementationDefined Bvec4
4D bool vector (bvec4 in GLSL)
Definition Glsl.hpp:127
Special type that can be passed to setUniform(), and that represents the texture of the object being ...
Definition Shader.hpp:75
Point with color and texture coordinates.
Definition Vertex.hpp:44