1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
|
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2025 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.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Export.hpp>
#include <SFML/Graphics/Glsl.hpp>
#include <SFML/Window/GlResource.hpp>
#include <filesystem>
#include <string>
#include <string_view>
#include <unordered_map>
#include <cstddef>
namespace sf
{
class InputStream;
class Texture;
////////////////////////////////////////////////////////////
/// \brief Shader class (vertex, geometry and fragment)
///
////////////////////////////////////////////////////////////
class SFML_GRAPHICS_API Shader : GlResource
{
public:
////////////////////////////////////////////////////////////
/// \brief Types of shaders
///
////////////////////////////////////////////////////////////
enum class Type
{
Vertex, //!< %Vertex shader
Geometry, //!< Geometry shader
Fragment //!< Fragment (pixel) shader
};
////////////////////////////////////////////////////////////
/// \brief Special type that can be passed to setUniform(),
/// and that represents the texture of the object being drawn
///
/// \see `setUniform(const std::string&, CurrentTextureType)`
///
////////////////////////////////////////////////////////////
struct CurrentTextureType
{
};
////////////////////////////////////////////////////////////
/// \brief Represents the texture of the object being drawn
///
/// \see `setUniform(const std::string&, CurrentTextureType)`
///
////////////////////////////////////////////////////////////
// NOLINTNEXTLINE(readability-identifier-naming)
static inline CurrentTextureType CurrentTexture;
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// This constructor creates an empty shader.
///
/// Binding an empty shader has the same effect as not
/// binding any shader.
///
////////////////////////////////////////////////////////////
Shader() = default;
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~Shader();
////////////////////////////////////////////////////////////
/// \brief Deleted copy constructor
///
////////////////////////////////////////////////////////////
Shader(const Shader&) = delete;
////////////////////////////////////////////////////////////
/// \brief Deleted copy assignment
///
////////////////////////////////////////////////////////////
Shader& operator=(const Shader&) = delete;
////////////////////////////////////////////////////////////
/// \brief Move constructor
///
////////////////////////////////////////////////////////////
Shader(Shader&& source) noexcept;
////////////////////////////////////////////////////////////
/// \brief Move assignment
///
////////////////////////////////////////////////////////////
Shader& operator=(Shader&& right) noexcept;
////////////////////////////////////////////////////////////
/// \brief Construct from a shader file
///
/// This constructor loads a single shader, vertex, geometry or
/// fragment, identified by the second argument.
/// The source must be a text file containing a valid
/// shader in GLSL language. GLSL is a C-like language
/// dedicated to OpenGL shaders; you'll probably need to
/// read a good documentation for it before writing your
/// own shaders.
///
/// \param filename Path of the vertex, geometry or fragment shader file to load
/// \param type Type of shader (vertex, geometry or fragment)
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(const std::filesystem::path& filename, Type type);
////////////////////////////////////////////////////////////
/// \brief Construct from vertex and fragment shader files
///
/// This constructor loads both the vertex and the fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be text files containing valid shaders
/// in GLSL language. GLSL is a C-like language dedicated to
/// OpenGL shaders; you'll probably need to read a good documentation
/// for it before writing your own shaders.
///
/// \param vertexShaderFilename Path of the vertex shader file to load
/// \param fragmentShaderFilename Path of the fragment shader file to load
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(const std::filesystem::path& vertexShaderFilename, const std::filesystem::path& fragmentShaderFilename);
////////////////////////////////////////////////////////////
/// \brief Construct from vertex, geometry and fragment shader files
///
/// This constructor loads the vertex, geometry and fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be text files containing valid shaders
/// in GLSL language. GLSL is a C-like language dedicated to
/// OpenGL shaders; you'll probably need to read a good documentation
/// for it before writing your own shaders.
///
/// \param vertexShaderFilename Path of the vertex shader file to load
/// \param geometryShaderFilename Path of the geometry shader file to load
/// \param fragmentShaderFilename Path of the fragment shader file to load
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(const std::filesystem::path& vertexShaderFilename,
const std::filesystem::path& geometryShaderFilename,
const std::filesystem::path& fragmentShaderFilename);
////////////////////////////////////////////////////////////
/// \brief Construct from shader in memory
///
/// This constructor loads a single shader, vertex, geometry
/// or fragment, identified by the second argument.
/// The source code must be a valid shader in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for
/// it before writing your own shaders.
///
/// \param shader String containing the source code of the shader
/// \param type Type of shader (vertex, geometry or fragment)
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(std::string_view shader, Type type);
////////////////////////////////////////////////////////////
/// \brief Construct from vertex and fragment shaders in memory
///
/// This constructor loads both the vertex and the fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be valid shaders in GLSL language. GLSL is
/// a C-like language dedicated to OpenGL shaders; you'll
/// probably need to read a good documentation for it before
/// writing your own shaders.
///
/// \param vertexShader String containing the source code of the vertex shader
/// \param fragmentShader String containing the source code of the fragment shader
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(std::string_view vertexShader, std::string_view fragmentShader);
////////////////////////////////////////////////////////////
/// \brief Construct from vertex, geometry and fragment shaders in memory
///
/// This constructor loads the vertex, geometry and fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be valid shaders in GLSL language. GLSL is
/// a C-like language dedicated to OpenGL shaders; you'll
/// probably need to read a good documentation for it before
/// writing your own shaders.
///
/// \param vertexShader String containing the source code of the vertex shader
/// \param geometryShader String containing the source code of the geometry shader
/// \param fragmentShader String containing the source code of the fragment shader
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader);
////////////////////////////////////////////////////////////
/// \brief Construct from a shader stream
///
/// This constructor loads a single shader, vertex, geometry
/// or fragment, identified by the second argument.
/// The source code must be a valid shader in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for it
/// before writing your own shaders.
///
/// \param stream Source stream to read from
/// \param type Type of shader (vertex, geometry or fragment)
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(InputStream& stream, Type type);
////////////////////////////////////////////////////////////
/// \brief Construct from vertex and fragment shader streams
///
/// This constructor loads both the vertex and the fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The source codes must be valid shaders in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for
/// it before writing your own shaders.
///
/// \param vertexShaderStream Source stream to read the vertex shader from
/// \param fragmentShaderStream Source stream to read the fragment shader from
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
////////////////////////////////////////////////////////////
/// \brief Construct from vertex, geometry and fragment shader streams
///
/// This constructor loads the vertex, geometry and fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The source codes must be valid shaders in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for
/// it before writing your own shaders.
///
/// \param vertexShaderStream Source stream to read the vertex shader from
/// \param geometryShaderStream Source stream to read the geometry shader from
/// \param fragmentShaderStream Source stream to read the fragment shader from
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
Shader(InputStream& vertexShaderStream, InputStream& geometryShaderStream, InputStream& fragmentShaderStream);
////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry or fragment shader from a file
///
/// This function loads a single shader, vertex, geometry or
/// fragment, identified by the second argument.
/// The source must be a text file containing a valid
/// shader in GLSL language. GLSL is a C-like language
/// dedicated to OpenGL shaders; you'll probably need to
/// read a good documentation for it before writing your
/// own shaders.
///
/// \param filename Path of the vertex, geometry or fragment shader file to load
/// \param type Type of shader (vertex, geometry or fragment)
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& filename, Type type);
////////////////////////////////////////////////////////////
/// \brief Load both the vertex and fragment shaders from files
///
/// This function loads both the vertex and the fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be text files containing valid shaders
/// in GLSL language. GLSL is a C-like language dedicated to
/// OpenGL shaders; you'll probably need to read a good documentation
/// for it before writing your own shaders.
///
/// \param vertexShaderFilename Path of the vertex shader file to load
/// \param fragmentShaderFilename Path of the fragment shader file to load
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& vertexShaderFilename,
const std::filesystem::path& fragmentShaderFilename);
////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry and fragment shaders from files
///
/// This function loads the vertex, geometry and fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be text files containing valid shaders
/// in GLSL language. GLSL is a C-like language dedicated to
/// OpenGL shaders; you'll probably need to read a good documentation
/// for it before writing your own shaders.
///
/// \param vertexShaderFilename Path of the vertex shader file to load
/// \param geometryShaderFilename Path of the geometry shader file to load
/// \param fragmentShaderFilename Path of the fragment shader file to load
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromMemory`, `loadFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& vertexShaderFilename,
const std::filesystem::path& geometryShaderFilename,
const std::filesystem::path& fragmentShaderFilename);
////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry or fragment shader from a source code in memory
///
/// This function loads a single shader, vertex, geometry
/// or fragment, identified by the second argument.
/// The source code must be a valid shader in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for
/// it before writing your own shaders.
///
/// \param shader String containing the source code of the shader
/// \param type Type of shader (vertex, geometry or fragment)
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(std::string_view shader, Type type);
////////////////////////////////////////////////////////////
/// \brief Load both the vertex and fragment shaders from source codes in memory
///
/// This function loads both the vertex and the fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be valid shaders in GLSL language. GLSL is
/// a C-like language dedicated to OpenGL shaders; you'll
/// probably need to read a good documentation for it before
/// writing your own shaders.
///
/// \param vertexShader String containing the source code of the vertex shader
/// \param fragmentShader String containing the source code of the fragment shader
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(std::string_view vertexShader, std::string_view fragmentShader);
////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry and fragment shaders from source codes in memory
///
/// This function loads the vertex, geometry and fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The sources must be valid shaders in GLSL language. GLSL is
/// a C-like language dedicated to OpenGL shaders; you'll
/// probably need to read a good documentation for it before
/// writing your own shaders.
///
/// \param vertexShader String containing the source code of the vertex shader
/// \param geometryShader String containing the source code of the geometry shader
/// \param fragmentShader String containing the source code of the fragment shader
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(std::string_view vertexShader,
std::string_view geometryShader,
std::string_view fragmentShader);
////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry or fragment shader from a custom stream
///
/// This function loads a single shader, vertex, geometry
/// or fragment, identified by the second argument.
/// The source code must be a valid shader in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for it
/// before writing your own shaders.
///
/// \param stream Source stream to read from
/// \param type Type of shader (vertex, geometry or fragment)
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromMemory`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& stream, Type type);
////////////////////////////////////////////////////////////
/// \brief Load both the vertex and fragment shaders from custom streams
///
/// This function loads both the vertex and the fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The source codes must be valid shaders in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for
/// it before writing your own shaders.
///
/// \param vertexShaderStream Source stream to read the vertex shader from
/// \param fragmentShaderStream Source stream to read the fragment shader from
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromMemory`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry and fragment shaders from custom streams
///
/// This function loads the vertex, geometry and fragment
/// shaders. If one of them fails to load, the shader is left
/// empty (the valid shader is unloaded).
/// The source codes must be valid shaders in GLSL language.
/// GLSL is a C-like language dedicated to OpenGL shaders;
/// you'll probably need to read a good documentation for
/// it before writing your own shaders.
///
/// \param vertexShaderStream Source stream to read the vertex shader from
/// \param geometryShaderStream Source stream to read the geometry shader from
/// \param fragmentShaderStream Source stream to read the fragment shader from
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromMemory`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream,
InputStream& geometryShaderStream,
InputStream& fragmentShaderStream);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p float uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param x Value of the float scalar
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, float x);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p vec2 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the vec2 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, Glsl::Vec2 vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p vec3 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the vec3 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Vec3& vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p vec4 uniform
///
/// This overload can also be called with `sf::Color` objects
/// that are converted to `sf::Glsl::Vec4`.
///
/// It is important to note that the components of the color are
/// normalized before being passed to the shader. Therefore,
/// they are converted from range [0 .. 255] to range [0 .. 1].
/// For example, a `sf::Color(255, 127, 0, 255)` will be transformed
/// to a `vec4(1.0, 0.5, 0.0, 1.0)` in the shader.
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the vec4 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Vec4& vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p int uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param x Value of the int scalar
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, int x);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p ivec2 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the ivec2 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, Glsl::Ivec2 vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p ivec3 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the ivec3 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Ivec3& vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p ivec4 uniform
///
/// This overload can also be called with `sf::Color` objects
/// that are converted to `sf::Glsl::Ivec4`.
///
/// If color conversions are used, the ivec4 uniform in GLSL
/// will hold the same values as the original `sf::Color`
/// instance. For example, `sf::Color(255, 127, 0, 255)` is
/// mapped to `ivec4(255, 127, 0, 255)`.
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the ivec4 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Ivec4& vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p bool uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param x Value of the bool scalar
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, bool x);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p bvec2 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the bvec2 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, Glsl::Bvec2 vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p bvec3 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the bvec3 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Bvec3& vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p bvec4 uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vector Value of the bvec4 vector
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Bvec4& vector);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p mat3 matrix
///
/// \param name Name of the uniform variable in GLSL
/// \param matrix Value of the mat3 matrix
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Mat3& matrix);
////////////////////////////////////////////////////////////
/// \brief Specify value for \p mat4 matrix
///
/// \param name Name of the uniform variable in GLSL
/// \param matrix Value of the mat4 matrix
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Glsl::Mat4& matrix);
////////////////////////////////////////////////////////////
/// \brief Specify a texture as \p sampler2D uniform
///
/// \a name is the name of the variable to change in the shader.
/// The corresponding parameter in the shader must be a 2D texture
/// (\p sampler2D GLSL type).
///
/// Example:
/// \code
/// uniform sampler2D the_texture; // this is the variable in the shader
/// \endcode
/// \code
/// sf::Texture texture;
/// ...
/// shader.setUniform("the_texture", texture);
/// \endcode
/// It is important to note that `texture` must remain alive as long
/// as the shader uses it, no copy is made internally.
///
/// To use the texture of the object being drawn, which cannot be
/// known in advance, you can pass the special value
/// `sf::Shader::CurrentTexture`:
/// \code
/// shader.setUniform("the_texture", sf::Shader::CurrentTexture).
/// \endcode
///
/// \param name Name of the texture in the shader
/// \param texture Texture to assign
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Texture& texture);
////////////////////////////////////////////////////////////
/// \brief Disallow setting from a temporary texture
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, const Texture&& texture) = delete;
////////////////////////////////////////////////////////////
/// \brief Specify current texture as \p sampler2D uniform
///
/// This overload maps a shader texture variable to the
/// texture of the object being drawn, which cannot be
/// known in advance. The second argument must be
/// `sf::Shader::CurrentTexture`.
/// The corresponding parameter in the shader must be a 2D texture
/// (\p sampler2D GLSL type).
///
/// Example:
/// \code
/// uniform sampler2D current; // this is the variable in the shader
/// \endcode
/// \code
/// shader.setUniform("current", sf::Shader::CurrentTexture);
/// \endcode
///
/// \param name Name of the texture in the shader
///
////////////////////////////////////////////////////////////
void setUniform(const std::string& name, CurrentTextureType);
////////////////////////////////////////////////////////////
/// \brief Specify values for \p float[] array uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param scalarArray pointer to array of \p float values
/// \param length Number of elements in the array
///
////////////////////////////////////////////////////////////
void setUniformArray(const std::string& name, const float* scalarArray, std::size_t length);
////////////////////////////////////////////////////////////
/// \brief Specify values for \p vec2[] array uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vectorArray pointer to array of \p vec2 values
/// \param length Number of elements in the array
///
////////////////////////////////////////////////////////////
void setUniformArray(const std::string& name, const Glsl::Vec2* vectorArray, std::size_t length);
////////////////////////////////////////////////////////////
/// \brief Specify values for \p vec3[] array uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vectorArray pointer to array of \p vec3 values
/// \param length Number of elements in the array
///
////////////////////////////////////////////////////////////
void setUniformArray(const std::string& name, const Glsl::Vec3* vectorArray, std::size_t length);
////////////////////////////////////////////////////////////
/// \brief Specify values for \p vec4[] array uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param vectorArray pointer to array of \p vec4 values
/// \param length Number of elements in the array
///
////////////////////////////////////////////////////////////
void setUniformArray(const std::string& name, const Glsl::Vec4* vectorArray, std::size_t length);
////////////////////////////////////////////////////////////
/// \brief Specify values for \p mat3[] array uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param matrixArray pointer to array of \p mat3 values
/// \param length Number of elements in the array
///
////////////////////////////////////////////////////////////
void setUniformArray(const std::string& name, const Glsl::Mat3* matrixArray, std::size_t length);
////////////////////////////////////////////////////////////
/// \brief Specify values for \p mat4[] array uniform
///
/// \param name Name of the uniform variable in GLSL
/// \param matrixArray pointer to array of \p mat4 values
/// \param length Number of elements in the array
///
////////////////////////////////////////////////////////////
void setUniformArray(const std::string& name, const Glsl::Mat4* matrixArray, std::size_t length);
////////////////////////////////////////////////////////////
/// \brief Get the underlying OpenGL handle of the shader.
///
/// You shouldn't need to use this function, unless you have
/// very specific stuff to implement that SFML doesn't support,
/// or implement a temporary workaround until a bug is fixed.
///
/// \return OpenGL handle of the shader or 0 if not yet loaded
///
////////////////////////////////////////////////////////////
[[nodiscard]] unsigned int getNativeHandle() const;
////////////////////////////////////////////////////////////
/// \brief Bind a shader for rendering
///
/// This function is not part of the graphics API, it mustn't be
/// used when drawing SFML entities. It must be used only if you
/// mix `sf::Shader` with OpenGL code.
///
/// \code
/// sf::Shader s1, s2;
/// ...
/// sf::Shader::bind(&s1);
/// // draw OpenGL stuff that use s1...
/// sf::Shader::bind(&s2);
/// // draw OpenGL stuff that use s2...
/// sf::Shader::bind(nullptr);
/// // draw OpenGL stuff that use no shader...
/// \endcode
///
/// \param shader Shader to bind, can be null to use no shader
///
////////////////////////////////////////////////////////////
static void bind(const Shader* shader);
////////////////////////////////////////////////////////////
/// \brief Tell whether or not the system supports shaders
///
/// This function should always be called before using
/// the shader features. If it returns `false`, then
/// any attempt to use `sf::Shader` will fail.
///
/// \return `true` if shaders are supported, `false` otherwise
///
////////////////////////////////////////////////////////////
[[nodiscard]] static bool isAvailable();
////////////////////////////////////////////////////////////
/// \brief Tell whether or not the system supports geometry shaders
///
/// This function should always be called before using
/// the geometry shader features. If it returns `false`, then
/// any attempt to use `sf::Shader` geometry shader features will fail.
///
/// This function can only return `true` if isAvailable() would also
/// return `true`, since shaders in general have to be supported in
/// order for geometry shaders to be supported as well.
///
/// Note: The first call to this function, whether by your
/// code or SFML will result in a context switch.
///
/// \return `true` if geometry shaders are supported, `false` otherwise
///
////////////////////////////////////////////////////////////
[[nodiscard]] static bool isGeometryAvailable();
private:
////////////////////////////////////////////////////////////
/// \brief Compile the shader(s) and create the program
///
/// If one of the arguments is a null pointer, the corresponding shader
/// is not created.
///
/// \param vertexShaderCode Source code of the vertex shader
/// \param geometryShaderCode Source code of the geometry shader
/// \param fragmentShaderCode Source code of the fragment shader
///
/// \return `true` on success, `false` if any error happened
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool compile(std::string_view vertexShaderCode,
std::string_view geometryShaderCode,
std::string_view fragmentShaderCode);
////////////////////////////////////////////////////////////
/// \brief Bind all the textures used by the shader
///
/// This function each texture to a different unit, and
/// updates the corresponding variables in the shader accordingly.
///
////////////////////////////////////////////////////////////
void bindTextures() const;
////////////////////////////////////////////////////////////
/// \brief Get the location ID of a shader uniform
///
/// \param name Name of the uniform variable to search
///
/// \return Location ID of the uniform, or -1 if not found
///
////////////////////////////////////////////////////////////
int getUniformLocation(const std::string& name);
////////////////////////////////////////////////////////////
/// \brief RAII object to save and restore the program
/// binding while uniforms are being set
///
/// Implementation is private in the .cpp file.
///
////////////////////////////////////////////////////////////
struct UniformBinder;
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
using TextureTable = std::unordered_map<int, const Texture*>;
using UniformTable = std::unordered_map<std::string, int>;
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
unsigned int m_shaderProgram{}; //!< OpenGL identifier for the program
int m_currentTexture{-1}; //!< Location of the current texture in the shader
TextureTable m_textures; //!< Texture variables in the shader, mapped to their location
UniformTable m_uniforms; //!< Parameters location cache
};
} // namespace sf
////////////////////////////////////////////////////////////
/// \class sf::Shader
/// \ingroup graphics
///
/// Shaders are programs written using a specific language,
/// executed directly by the graphics card and allowing
/// to apply real-time operations to the rendered entities.
///
/// There are three kinds of shaders:
/// \li %Vertex shaders, that process vertices
/// \li Geometry shaders, that process primitives
/// \li Fragment (pixel) shaders, that process pixels
///
/// A `sf::Shader` can be composed of either a vertex shader
/// alone, a geometry shader alone, a fragment shader alone,
/// or any combination of them. (see the variants of the
/// load functions).
///
/// Shaders are written in GLSL, which is a C-like
/// language dedicated to OpenGL shaders. You'll probably
/// need to learn its basics before writing your own shaders
/// for SFML.
///
/// Like any C/C++ program, a GLSL shader has its own variables
/// called _uniforms_ that you can set from your C++ application.
/// `sf::Shader` handles different types of uniforms:
/// \li scalars: \p float, \p int, \p bool
/// \li vectors (2, 3 or 4 components)
/// \li matrices (3x3 or 4x4)
/// \li samplers (textures)
///
/// Some SFML-specific types can be converted:
/// \li `sf::Color` as a 4D vector (\p vec4)
/// \li `sf::Transform` as matrices (\p mat3 or \p mat4)
///
/// Every uniform variable in a shader can be set through one of the
/// `setUniform()` or `setUniformArray()` overloads. For example, if you
/// have a shader with the following uniforms:
/// \code
/// uniform float offset;
/// uniform vec3 point;
/// uniform vec4 color;
/// uniform mat4 matrix;
/// uniform sampler2D overlay;
/// uniform sampler2D current;
/// \endcode
/// You can set their values from C++ code as follows, using the types
/// defined in the `sf::Glsl` namespace:
/// \code
/// shader.setUniform("offset", 2.f);
/// shader.setUniform("point", sf::Vector3f(0.5f, 0.8f, 0.3f));
/// shader.setUniform("color", sf::Glsl::Vec4(color)); // color is a sf::Color
/// shader.setUniform("matrix", sf::Glsl::Mat4(transform)); // transform is a sf::Transform
/// shader.setUniform("overlay", texture); // texture is a sf::Texture
/// shader.setUniform("current", sf::Shader::CurrentTexture);
/// \endcode
///
/// The special `Shader::CurrentTexture` argument maps the
/// given \p sampler2D uniform to the current texture of the
/// object being drawn (which cannot be known in advance).
///
/// To apply a shader to a drawable, you must pass it as an
/// additional parameter to the `RenderWindow::draw` function:
/// \code
/// window.draw(sprite, &shader);
/// \endcode
///
/// ... which is in fact just a shortcut for this:
/// \code
/// sf::RenderStates states;
/// states.shader = &shader;
/// window.draw(sprite, states);
/// \endcode
///
/// In the code above we pass a pointer to the shader, because it may
/// be null (which means "no shader").
///
/// Shaders can be used on any drawable, but some combinations are
/// not interesting. For example, using a vertex shader on a `sf::Sprite`
/// is limited because there are only 4 vertices, the sprite would
/// have to be subdivided in order to apply wave effects.
/// Another bad example is a fragment shader with `sf::Text`: the texture
/// of the text is not the actual text that you see on screen, it is
/// a big texture containing all the characters of the font in an
/// arbitrary order; thus, texture lookups on pixels other than the
/// current one may not give you the expected result.
///
/// Shaders can also be used to apply global post-effects to the
/// current contents of the target.
/// This can be done in two different ways:
/// \li draw everything to a `sf::RenderTexture`, then draw it to
/// the main target using the shader
/// \li draw everything directly to the main target, then use
/// `sf::Texture::update(Window&)` to copy its contents to a texture
/// and draw it to the main target using the shader
///
/// The first technique is more optimized because it doesn't involve
/// retrieving the target's pixels to system memory, but the
/// second one doesn't impact the rendering process and can be
/// easily inserted anywhere without impacting all the code.
///
/// Like `sf::Texture` that can be used as a raw OpenGL texture,
/// `sf::Shader` can also be used directly as a raw shader for
/// custom OpenGL geometry.
/// \code
/// sf::Shader::bind(&shader);
/// ... render OpenGL geometry ...
/// sf::Shader::bind(nullptr);
/// \endcode
///
/// \see `sf::Glsl`
///
////////////////////////////////////////////////////////////
|