Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::Shader Class Reference

GPU shader program for rendering. More...

#include <Shader.hpp>

Public Member Functions

 Shader ()=default
 ~Shader ()
 Shader (const Shader &)=delete
Shader & operator= (const Shader &)=delete
 Shader (Shader &&other) noexcept
Shader & operator= (Shader &&other) noexcept
GfxShaderLanguage language () const
 The source language this program was compiled from.
void whenLinked (std::function< void(Shader &)> fn)
 Runs fn on this shader once it has linked: at once for one that has.
void bind () const
 Binds the shader for rendering.
void unbind () const
 Unbinds the shader.
void setUniform (const std::string &name, i32 value) const
 Sets an integer uniform.
void setUniform (const std::string &name, f32 value) const
void setUniform (const std::string &name, const glm::vec2 &value) const
void setUniform (const std::string &name, const glm::vec3 &value) const
void setUniform (const std::string &name, const glm::vec4 &value) const
void setUniform (const std::string &name, const glm::mat3 &value) const
void setUniform (const std::string &name, const glm::mat4 &value) const
void setUniform (i32 location, i32 value) const
void setUniform (i32 location, f32 value) const
void setUniform (i32 location, const glm::vec2 &value) const
void setUniform (i32 location, const glm::vec3 &value) const
void setUniform (i32 location, const glm::vec4 &value) const
void setUniform (i32 location, const glm::mat3 &value) const
void setUniform (i32 location, const glm::mat4 &value) const
i32 getAttribLocation (const std::string &name) const
 Gets the location of a vertex attribute.
i32 getUniformLocation (const std::string &name) const
bool hasUniform (const std::string &name) const
 True if the linked program declares this uniform.
const std::vector< GfxUniformInfo > & getActiveUniforms () const
 Returns reflected metadata for every active uniform, populated at link time.
void adoptDrawParams (DrawParamsLayout layout)
 Adopts the layout rewriteLooseUniforms produced for this program.
bool hasDrawParams () const
 True when this program carries a DrawParams block.
void commitParams ()
 Uploads the shadow (when dirty) and binds this shader's params UBO at DRAW_PARAMS_BINDING.
bool isValid () const
 Checks if the shader compiled and linked successfully.
ShaderHandle handle () const
 Gets the device program handle.
u32 getProgramId () const
 Gets the program handle's raw value, for command payloads and sort keys.

Static Public Member Functions

static Unique< Shader > create (GfxDevice &device, const std::string &vertexSrc, const std::string &fragmentSrc, GfxShaderLanguage language=GfxShaderLanguage::GLSL_ES300)
 Creates a shader from source code strings.
static Unique< Shader > createWithBindings (GfxDevice &device, const std::string &vertexSrc, const std::string &fragmentSrc, std::initializer_list< AttribBinding > bindings, GfxShaderLanguage language=GfxShaderLanguage::GLSL_ES300, bool deferLink=false)
 Creates a shader with explicit attribute bindings applied before linking.
static Unique< Shader > createFromFile (GfxDevice &device, const std::string &vertexPath, const std::string &fragmentPath)
 Creates a shader from file paths.
static ShaderCompileOutcome createEx (GfxDevice &device, const std::string &vertexSrc, const std::string &fragmentSrc, std::initializer_list< AttribBinding > bindings={}, GfxShaderLanguage language=GfxShaderLanguage::GLSL_ES300, bool deferLink=false)
 Creates a shader and exposes the driver log on failure.

Detailed Description

GPU shader program for rendering.

Encapsulates an OpenGL/WebGL shader program consisting of a vertex shader and fragment shader. Provides uniform setting with location caching for performance.

auto shader = Shader::create(vertexSource, fragmentSource);
shader->bind();
shader->setUniform("u_projection", projectionMatrix);
shader->setUniform("u_color", glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
static Unique< Shader > create(GfxDevice &device, const std::string &vertexSrc, const std::string &fragmentSrc, GfxShaderLanguage language=GfxShaderLanguage::GLSL_ES300)
Creates a shader from source code strings.
Definition Shader.cpp:90

Definition at line 86 of file Shader.hpp.

Constructor & Destructor Documentation

◆ Shader() [1/3]

esengine::Shader::Shader ( )
default

◆ ~Shader()

esengine::Shader::~Shader ( )

Definition at line 34 of file Shader.cpp.

References esengine::Invalid.

◆ Shader() [2/3]

esengine::Shader::Shader ( const Shader & )
delete

References Shader().

◆ Shader() [3/3]

esengine::Shader::Shader ( Shader && other)
noexcept

Definition at line 47 of file Shader.cpp.

References Shader(), and esengine::Invalid.

Member Function Documentation

◆ adoptDrawParams()

void esengine::Shader::adoptDrawParams ( DrawParamsLayout layout)

Adopts the layout rewriteLooseUniforms produced for this program.

Allocates the zeroed CPU shadow, so unset members read 0 — the same value their loose-uniform ancestors read. From here on the name-based setUniform overloads write into the shadow instead of issuing loose uniform uploads; commitParams() flushes it.

Definition at line 371 of file Shader.cpp.

References esengine::DRAW_PARAMS_FALLBACK_SIZE, and ES_LOG_WARN.

◆ bind()

void esengine::Shader::bind ( ) const

Binds the shader for rendering.

Definition at line 151 of file Shader.cpp.

Referenced by esengine::ParticlePlugin::init(), and esengine::MaterialStore::seedSamplers().

◆ commitParams()

void esengine::Shader::commitParams ( )

Uploads the shadow (when dirty) and binds this shader's params UBO at DRAW_PARAMS_BINDING.

Call before any draw with this shader — the binding slot is shared, so the previous committer's buffer is bound otherwise. No-op for shaders without a DrawParams block.

Definition at line 382 of file Shader.cpp.

References esengine::DRAW_PARAMS_BINDING, esengine::Invalid, esengine::GfxContent::retained(), and esengine::Uniform.

Referenced by esengine::draw_mesh(), esengine::draw_meshWithUniforms(), esengine::PostProcessPipeline::drawSelectionGround(), esengine::PostProcessPipeline::drawSelectionOutline(), esengine::PostProcessPipeline::encodeRgbm(), esengine::PostProcessPipeline::grabDepth(), and esengine::PostProcessPipeline::grabScene().

◆ create()

Unique< Shader > esengine::Shader::create ( GfxDevice & device,
const std::string & vertexSrc,
const std::string & fragmentSrc,
GfxShaderLanguage language = GfxShaderLanguage::GLSL_ES300 )
static

Creates a shader from source code strings.

Parameters
vertexSrcVertex shader GLSL source
fragmentSrcFragment shader GLSL source
Returns
Unique pointer to the shader, or nullptr on failure

Definition at line 90 of file Shader.cpp.

References language(), and esengine::makeUnique().

Referenced by createFromFile(), and esengine::resource::ResourceManager::createShader().

◆ createEx()

ShaderCompileOutcome esengine::Shader::createEx ( GfxDevice & device,
const std::string & vertexSrc,
const std::string & fragmentSrc,
std::initializer_list< AttribBinding > bindings = {},
GfxShaderLanguage language = GfxShaderLanguage::GLSL_ES300,
bool deferLink = false )
static

Creates a shader and exposes the driver log on failure.

Same behaviour as createWithBindings but returns a ShaderCompileOutcome so callers (notably ShaderLoader) can remap the GL log back to the original .esshader file and line numbers.

Definition at line 326 of file Shader.cpp.

References esengine::ShaderCompileOutcome::failedStage, language(), esengine::ShaderCompileOutcome::log, esengine::makeUnique(), and esengine::ShaderCompileOutcome::shader.

Referenced by esengine::resource::ResourceManager::createShader().

◆ createFromFile()

Unique< Shader > esengine::Shader::createFromFile ( GfxDevice & device,
const std::string & vertexPath,
const std::string & fragmentPath )
static

Creates a shader from file paths.

Parameters
vertexPathPath to vertex shader file
fragmentPathPath to fragment shader file
Returns
Unique pointer to the shader, or nullptr on failure

Definition at line 112 of file Shader.cpp.

References create(), and ES_LOG_ERROR.

Referenced by esengine::resource::ResourceManager::loadShader().

◆ createWithBindings()

Unique< Shader > esengine::Shader::createWithBindings ( GfxDevice & device,
const std::string & vertexSrc,
const std::string & fragmentSrc,
std::initializer_list< AttribBinding > bindings,
GfxShaderLanguage language = GfxShaderLanguage::GLSL_ES300,
bool deferLink = false )
static

Creates a shader with explicit attribute bindings applied before linking.

Parameters
vertexSrcVertex shader GLSL source
fragmentSrcFragment shader GLSL source
bindingsAttribute location bindings
Returns
Unique pointer to the shader, or nullptr on failure

Definition at line 100 of file Shader.cpp.

References language(), and esengine::makeUnique().

Referenced by esengine::resource::ResourceManager::createShaderWithBindings().

◆ getActiveUniforms()

const std::vector< GfxUniformInfo > & esengine::Shader::getActiveUniforms ( ) const
inline

Returns reflected metadata for every active uniform, populated at link time.

Definition at line 216 of file Shader.hpp.

◆ getAttribLocation()

i32 esengine::Shader::getAttribLocation ( const std::string & name) const

Gets the location of a vertex attribute.

Parameters
nameAttribute name in shader
Returns
Location, or -1 if not found

Definition at line 485 of file Shader.cpp.

◆ getProgramId()

◆ getUniformLocation()

i32 esengine::Shader::getUniformLocation ( const std::string & name) const

◆ handle()

ShaderHandle esengine::Shader::handle ( ) const
inline

Gets the device program handle.

Definition at line 254 of file Shader.hpp.

Referenced by esengine::applyMeshPipeline(), esengine::PostProcessPipeline::canDrawMaterialOver(), and esengine::ImmediateDraw::init().

◆ hasDrawParams()

bool esengine::Shader::hasDrawParams ( ) const
inline

True when this program carries a DrawParams block.

Definition at line 232 of file Shader.hpp.

◆ hasUniform()

bool esengine::Shader::hasUniform ( const std::string & name) const

True if the linked program declares this uniform.

Silent probe (no "not found" warning), for call sites that intentionally supply a superset of uniforms — e.g. the post-process pipeline applies a whole effect's uniform set to each of its sub-pass shaders, which each use only a subset.

Definition at line 364 of file Shader.cpp.

Referenced by esengine::PostProcessPipeline::drawMaterialOver(), esengine::PostProcessPipeline::drawSelectionGround(), esengine::PostProcessPipeline::drawSelectionOutline(), esengine::PostProcessPipeline::encodeRgbm(), esengine::PostProcessPipeline::grabDepth(), esengine::PostProcessPipeline::grabScene(), and esengine::MaterialStore::seedSamplers().

◆ isValid()

bool esengine::Shader::isValid ( ) const
inline

Checks if the shader compiled and linked successfully.

Returns
True if the shader is usable

Definition at line 251 of file Shader.hpp.

References esengine::Invalid.

Referenced by esengine::ImmediateDraw::init(), esengine::ParticlePlugin::init(), esengine::MaterialStore::prepassCoverageProgram(), and esengine::MaterialStore::shadowProgram().

◆ language()

GfxShaderLanguage esengine::Shader::language ( ) const
inline

◆ operator=() [1/2]

Shader & esengine::Shader::operator= ( const Shader & )
delete

◆ operator=() [2/2]

Shader & esengine::Shader::operator= ( Shader && other)
noexcept

Definition at line 65 of file Shader.cpp.

References Shader(), and esengine::Invalid.

◆ setUniform() [1/14]

void esengine::Shader::setUniform ( const std::string & name,
const glm::mat3 & value ) const

Definition at line 447 of file Shader.cpp.

References getUniformLocation(), and esengine::Mat3.

◆ setUniform() [2/14]

void esengine::Shader::setUniform ( const std::string & name,
const glm::mat4 & value ) const

Definition at line 452 of file Shader.cpp.

References getUniformLocation(), and esengine::Mat4.

◆ setUniform() [3/14]

void esengine::Shader::setUniform ( const std::string & name,
const glm::vec2 & value ) const

Definition at line 432 of file Shader.cpp.

References getUniformLocation(), and esengine::Vec2.

◆ setUniform() [4/14]

void esengine::Shader::setUniform ( const std::string & name,
const glm::vec3 & value ) const

Definition at line 437 of file Shader.cpp.

References getUniformLocation(), and esengine::Vec3.

◆ setUniform() [5/14]

void esengine::Shader::setUniform ( const std::string & name,
const glm::vec4 & value ) const

Definition at line 442 of file Shader.cpp.

References getUniformLocation(), and esengine::Vec4.

◆ setUniform() [6/14]

void esengine::Shader::setUniform ( const std::string & name,
f32 value ) const

Definition at line 427 of file Shader.cpp.

References esengine::Float, and getUniformLocation().

◆ setUniform() [7/14]

◆ setUniform() [8/14]

void esengine::Shader::setUniform ( i32 location,
const glm::mat3 & value ) const

Definition at line 477 of file Shader.cpp.

◆ setUniform() [9/14]

void esengine::Shader::setUniform ( i32 location,
const glm::mat4 & value ) const

Definition at line 481 of file Shader.cpp.

◆ setUniform() [10/14]

void esengine::Shader::setUniform ( i32 location,
const glm::vec2 & value ) const

Definition at line 465 of file Shader.cpp.

◆ setUniform() [11/14]

void esengine::Shader::setUniform ( i32 location,
const glm::vec3 & value ) const

Definition at line 469 of file Shader.cpp.

◆ setUniform() [12/14]

void esengine::Shader::setUniform ( i32 location,
const glm::vec4 & value ) const

Definition at line 473 of file Shader.cpp.

◆ setUniform() [13/14]

void esengine::Shader::setUniform ( i32 location,
f32 value ) const

Definition at line 461 of file Shader.cpp.

◆ setUniform() [14/14]

void esengine::Shader::setUniform ( i32 location,
i32 value ) const

Definition at line 457 of file Shader.cpp.

◆ unbind()

void esengine::Shader::unbind ( ) const

Unbinds the shader.

Definition at line 173 of file Shader.cpp.

References esengine::Invalid.

Referenced by esengine::ParticlePlugin::init(), and esengine::MaterialStore::seedSamplers().

◆ whenLinked()

void esengine::Shader::whenLinked ( std::function< void(Shader &)> fn)

Runs fn on this shader once it has linked: at once for one that has.

What is done to a program only after it links (a sampler pinned to its unit) is handed here, so a shader made with deferLink is not waited for just to be finished.

Definition at line 164 of file Shader.cpp.

References Shader().

Referenced by esengine::ParticlePlugin::init(), and esengine::MaterialStore::seedSamplers().


The documentation for this class was generated from the following files:
  • /home/runner/work/estella/estella/src/esengine/renderer/rhi/Shader.hpp
  • /home/runner/work/estella/estella/src/esengine/renderer/rhi/Shader.cpp