Skip to content

Commit

Permalink
Remove default empty state of sf::Shader
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 19, 2024
1 parent 504b850 commit 56a4af1
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 188 deletions.
16 changes: 9 additions & 7 deletions examples/island/Island.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ int main()
const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value();

// Create all of our graphics resources
sf::Text hudText(font);
sf::Text statusText(font);
sf::Shader terrainShader;
const sf::RenderStates terrainStates(&terrainShader);
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Usage::Static);
sf::Text hudText(font);
sf::Text statusText(font);
std::optional<sf::Shader> terrainShader;
sf::RenderStates terrainStates;
sf::VertexBuffer terrain(sf::PrimitiveType::Triangles, sf::VertexBuffer::Usage::Static);

// Set up our text drawables
statusText.setCharacterSize(28);
Expand All @@ -123,14 +123,16 @@ int main()
{
statusText.setString("Shaders and/or Vertex Buffers Unsupported");
}
else if (!terrainShader.loadFromFile("resources/terrain.vert", "resources/terrain.frag"))
else if (!(terrainShader = sf::Shader::loadFromFile("resources/terrain.vert", "resources/terrain.frag")))
{
prerequisitesSupported = false;

statusText.setString("Failed to load shader program");
}
else
{
terrainStates = sf::RenderStates(&*terrainShader);

// Start up our thread pool
for (unsigned int i = 0; i < threadCount; ++i)
{
Expand Down Expand Up @@ -238,7 +240,7 @@ int main()
bufferUploadPending = false;
}

terrainShader.setUniform("lightFactor", lightFactor);
terrainShader->setUniform("lightFactor", lightFactor);
window.draw(terrain, terrainStates);
}
}
Expand Down
30 changes: 11 additions & 19 deletions examples/shader/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class Pixelate : public Effect
return false;
m_sprite.emplace(m_texture);

// Load the shader
if (!m_shader.loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment))
return false;
m_shader.setUniform("texture", sf::Shader::CurrentTexture);

return true;
Expand All @@ -60,7 +57,7 @@ class Pixelate : public Effect
private:
sf::Texture m_texture;
std::optional<sf::Sprite> m_sprite;
sf::Shader m_shader;
sf::Shader m_shader{sf::Shader::loadFromFile("resources/pixelate.frag", sf::Shader::Type::Fragment).value()};
};


Expand Down Expand Up @@ -99,8 +96,7 @@ class WaveBlur : public Effect
m_text.setCharacterSize(22);
m_text.setPosition({30.f, 20.f});

// Load the shader
return m_shader.loadFromFile("resources/wave.vert", "resources/blur.frag");
return true;
}

void onUpdate(float time, float x, float y) override
Expand All @@ -118,7 +114,7 @@ class WaveBlur : public Effect

private:
sf::Text m_text;
sf::Shader m_shader;
sf::Shader m_shader{sf::Shader::loadFromFile("resources/wave.vert", "resources/blur.frag").value()};
};


Expand Down Expand Up @@ -151,7 +147,7 @@ class StormBlink : public Effect
}

// Load the shader
return m_shader.loadFromFile("resources/storm.vert", "resources/blink.frag");
return true;
}

void onUpdate(float time, float x, float y) override
Expand All @@ -171,7 +167,7 @@ class StormBlink : public Effect

private:
sf::VertexArray m_points;
sf::Shader m_shader;
sf::Shader m_shader{sf::Shader::loadFromFile("resources/storm.vert", "resources/blink.frag").value()};
};


Expand Down Expand Up @@ -211,9 +207,7 @@ class Edge : public Effect
m_entities.push_back(entity);
}

// Load the shader
if (!m_shader.loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment))
return false;
// Set the shader uniform
m_shader.setUniform("texture", sf::Shader::CurrentTexture);

return true;
Expand Down Expand Up @@ -254,7 +248,7 @@ class Edge : public Effect
sf::Texture m_entityTexture;
std::optional<sf::Sprite> m_backgroundSprite;
std::vector<sf::Sprite> m_entities;
sf::Shader m_shader;
sf::Shader m_shader{sf::Shader::loadFromFile("resources/edge.frag", sf::Shader::Type::Fragment).value()};
};


Expand Down Expand Up @@ -287,9 +281,6 @@ class Geometry : public Effect
if (!m_logoTexture.loadFromFile("resources/logo.png"))
return false;

// Load the shader
if (!m_shader.loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag"))
return false;
m_shader.setUniform("texture", sf::Shader::CurrentTexture);

// Set the render resolution (used for proper scaling)
Expand Down Expand Up @@ -326,9 +317,10 @@ class Geometry : public Effect
}

private:
sf::Texture m_logoTexture;
sf::Transform m_transform;
sf::Shader m_shader;
sf::Texture m_logoTexture;
sf::Transform m_transform;
sf::Shader m_shader{
sf::Shader::loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag").value()};
sf::VertexArray m_pointCloud;
};

Expand Down
75 changes: 40 additions & 35 deletions include/SFML/Graphics/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <SFML/Window/GlResource.hpp>

#include <filesystem>
#include <optional>
#include <string>
#include <unordered_map>

Expand Down Expand Up @@ -83,14 +84,6 @@ class SFML_GRAPHICS_API Shader : GlResource
// NOLINTNEXTLINE(readability-identifier-naming)
static inline CurrentTextureType CurrentTexture;

////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// This constructor creates an invalid shader.
///
////////////////////////////////////////////////////////////
Shader() = default;

////////////////////////////////////////////////////////////
/// \brief Destructor
///
Expand Down Expand Up @@ -136,12 +129,12 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromMemory, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& filename, Type type);
[[nodiscard]] static std::optional<Shader> loadFromFile(const std::filesystem::path& filename, Type type);

////////////////////////////////////////////////////////////
/// \brief Load both the vertex and fragment shaders from files
Expand All @@ -157,13 +150,13 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromMemory, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& vertexShaderFilename,
const std::filesystem::path& fragmentShaderFilename);
[[nodiscard]] static std::optional<Shader> loadFromFile(const std::filesystem::path& vertexShaderFilename,
const std::filesystem::path& fragmentShaderFilename);

////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry and fragment shaders from files
Expand All @@ -180,14 +173,14 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromMemory, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromFile(const std::filesystem::path& vertexShaderFilename,
const std::filesystem::path& geometryShaderFilename,
const std::filesystem::path& fragmentShaderFilename);
[[nodiscard]] static std::optional<Shader> 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
Expand All @@ -202,12 +195,12 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(const std::string& shader, Type type);
[[nodiscard]] static std::optional<Shader> loadFromMemory(const std::string& shader, Type type);

////////////////////////////////////////////////////////////
/// \brief Load both the vertex and fragment shaders from source codes in memory
Expand All @@ -223,12 +216,13 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(const std::string& vertexShader, const std::string& fragmentShader);
[[nodiscard]] static std::optional<Shader> loadFromMemory(const std::string& vertexShader,
const std::string& fragmentShader);

////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry and fragment shaders from source codes in memory
Expand All @@ -245,14 +239,14 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromStream
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(const std::string& vertexShader,
const std::string& geometryShader,
const std::string& fragmentShader);
[[nodiscard]] static std::optional<Shader> loadFromMemory(const std::string& vertexShader,
const std::string& geometryShader,
const std::string& fragmentShader);

////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry or fragment shader from a custom stream
Expand All @@ -267,12 +261,12 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromMemory
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& stream, Type type);
[[nodiscard]] static std::optional<Shader> loadFromStream(InputStream& stream, Type type);

////////////////////////////////////////////////////////////
/// \brief Load both the vertex and fragment shaders from custom streams
Expand All @@ -288,12 +282,13 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromMemory
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
[[nodiscard]] static std::optional<Shader> loadFromStream(InputStream& vertexShaderStream,
InputStream& fragmentShaderStream);

////////////////////////////////////////////////////////////
/// \brief Load the vertex, geometry and fragment shaders from custom streams
Expand All @@ -310,14 +305,14 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader if loading succeeded, `std::nullopt` if it failed
///
/// \see loadFromFile, loadFromMemory
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromStream(InputStream& vertexShaderStream,
InputStream& geometryShaderStream,
InputStream& fragmentShaderStream);
[[nodiscard]] static std::optional<Shader> loadFromStream(InputStream& vertexShaderStream,
InputStream& geometryShaderStream,
InputStream& fragmentShaderStream);

////////////////////////////////////////////////////////////
/// \brief Specify value for \p float uniform
Expand Down Expand Up @@ -650,6 +645,14 @@ class SFML_GRAPHICS_API Shader : GlResource
static bool isGeometryAvailable();

private:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// This constructor creates an invalid shader.
///
////////////////////////////////////////////////////////////
Shader() = default;

////////////////////////////////////////////////////////////
/// \brief Compile the shader(s) and create the program
///
Expand All @@ -660,10 +663,12 @@ class SFML_GRAPHICS_API Shader : GlResource
/// \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
/// \return Shader on success, `std::nullopt` if any error happened
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool compile(const char* vertexShaderCode, const char* geometryShaderCode, const char* fragmentShaderCode);
[[nodiscard]] static std::optional<Shader> compile(const char* vertexShaderCode,
const char* geometryShaderCode,
const char* fragmentShaderCode);

////////////////////////////////////////////////////////////
/// \brief Bind all the textures used by the shader
Expand Down

0 comments on commit 56a4af1

Please sign in to comment.