nCine::Shader class

GPU shader program usable by materials and drawable nodes.

Wraps an OpenGL shader program built from a vertex and a fragment shader. The sources can come from memory, a file or the binary cache, and a matching variant can be registered for rendering batched draw commands.

Base classes

class Object
Base class of all nCine objects.

Public types

enum class LoadMode { String, File }
Load mode.
enum class Introspection { Enabled, NoUniformsInBlocks, Disabled }
Introspection mode.

Public static functions

static auto CompileShaderFile(StringView path, ShaderCompiler::RuntimeProgram& program) -> bool
Compiles an annotated ".shader" program file (ShaderCompiler format) into program without creating any GPU objects.
static auto sType() -> ObjectType

Constructors, destructors, conversion operators

Shader()
Shader(const char* shaderName, LoadMode loadMode, Introspection introspection, const char* vertex, const char* fragment, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize)
Shader(const char* shaderName, LoadMode loadMode, const char* vertex, const char* fragment, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize)
Shader(LoadMode loadMode, const char* vertex, const char* fragment, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize)
~Shader() override
Shader(const Shader&) deleted

Public functions

auto operator=(const Shader&) -> Shader& deleted
auto LoadFromMemory(const char* shaderName, Introspection introspection, const char* vertex, const char* fragment, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize, ArrayView<const StringView> defines = {}) -> bool
auto LoadFromMemory(const char* shaderName, const char* vertex, const char* fragment, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize) -> bool
auto LoadFromMemory(const char* vertex, const char* fragment, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize) -> bool
auto LoadFromMemory(const char* shaderName, Introspection introspection, const ShaderCompiler::ProgramVariant& variant, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize) -> bool
Loads a ShaderCompiler program variant, using its reflection instead of GL introspection.
auto LoadFromFile(const char* shaderName, Introspection introspection, StringView vertexPath, StringView fragmentPath, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize, ArrayView<const StringView> defines = {}) -> bool
auto LoadFromFile(const char* shaderName, StringView vertexPath, StringView fragmentPath, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize) -> bool
auto LoadFromFile(StringView vertexPath, StringView fragmentPath, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize) -> bool
auto LoadFromShaderFile(const char* shaderName, Introspection introspection, StringView path, const char* variantName = nullptr, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize) -> bool
Loads one variant of an annotated ".shader" program file (ShaderCompiler format).
auto LoadFromCache(const char* shaderName, std::uint64_t shaderVersion, Introspection introspection) -> bool
auto LoadFromCache(const char* shaderName, std::uint64_t shaderVersion, Introspection introspection, const ShaderCompiler::ProgramVariant& variant) -> bool
Loads a binary-cached program, importing uniforms, blocks and attributes from ShaderCompiler reflection instead of GL introspection.
auto SaveToCache(const char* shaderName, std::uint64_t shaderVersion) const -> bool
auto SetAttribute(const char* name, std::int32_t stride, void* pointer) -> bool
Sets the VBO stride and pointer for the specified vertex attribute.
auto IsLinked() const -> bool
Returns true if the shader is linked and can therefore be used.
auto RetrieveInfoLogLength() const -> unsigned int
Returns the length of the information log, including the null terminator.
void RetrieveInfoLog(std::string& infoLog) const
Retrieves the information log and copies it into the provided string object.
auto GetLogOnErrors() const -> bool
Returns the automatic log on errors flag.
void SetLogOnErrors(bool shouldLogOnErrors)
Sets the automatic log on errors flag.
void SetGLShaderProgramLabel(const char* label)
Sets the OpenGL object label for the shader program.
void RegisterBatchedShader(Shader& batchedShader)
Registers a shader to be used when rendering batches of render commands.
auto GetRenderModes() const -> std::uint32_t
Returns the render_mode flags of the shader (a bitmask of ShaderCompiler::RenderMode, 0 when none were declared).
void SetRenderModes(std::uint32_t renderModes)
Sets the render_mode flags of the shader.
auto GetHandle() -> RHI::ShaderProgram*

Function documentation

static bool nCine::Shader::CompileShaderFile(StringView path, ShaderCompiler::RuntimeProgram& program)

Compiles an annotated ".shader" program file (ShaderCompiler format) into program without creating any GPU objects.

Exposes the lowered sources and reflection of every variant, so the caller can compute a reflected batch size before loading a variant with LoadFromMemory().

bool nCine::Shader::LoadFromMemory(const char* shaderName, Introspection introspection, const ShaderCompiler::ProgramVariant& variant, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize)

Loads a ShaderCompiler program variant, using its reflection instead of GL introspection.

The variant's sources are compiled like with the plain-string overloads, but uniforms, uniform blocks and attributes come from the offline reflection data, with targeted location queries only.

bool nCine::Shader::LoadFromShaderFile(const char* shaderName, Introspection introspection, StringView path, const char* variantName = nullptr, std::int32_t batchSize = RHI::ShaderProgram::DefaultBatchSize)

Loads one variant of an annotated ".shader" program file (ShaderCompiler format).

The file is compiled with the same pipeline the offline ShaderCompiler uses, including #include directives resolved relative to the file, so externally supplied shaders behave exactly like the precompiled ones. Pass nullptr as variantName for the unnamed base variant (Variants[0]).

void nCine::Shader::SetLogOnErrors(bool shouldLogOnErrors)

Sets the automatic log on errors flag.

When the flag is true the shader automatically logs compilation and linking errors.

void nCine::Shader::SetRenderModes(std::uint32_t renderModes)

Sets the render_mode flags of the shader.

Loaders that see the program-level reflection (e.g. ContentResolver) call this so that Material::SetShader() can apply the declared blending preset automatically.