Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
PhosphorRendering::ShaderCompiler Class Reference

Static utility for GLSL → SPIR-V compilation with include resolution and caching. More...

#include <PhosphorRendering/ShaderCompiler.h>

Classes

struct  Result

Static Public Member Functions

static Result compile (const QByteArray &source, QShader::Stage stage)
 Compile GLSL source to QShader (SPIR-V 1.0 + GLSL 330 + ES 300/310/320).
static Result compileFromFile (const QString &path, const QStringList &includePaths)
 Load shader from file, expand #include directives, then compile.
static QString loadAndExpand (const QString &path, const QStringList &includePaths, QString *outError=nullptr, QStringList *outIncludedPaths=nullptr)
 Load a shader file and expand #include directives without compiling.
static QString expandSource (const QString &source, const QString &currentFileDir, const QStringList &includePaths, QString *outError=nullptr, QStringList *outIncludedPaths=nullptr)
 Expand #include directives in already-read source, without touching the filesystem for the top-level text.
static void clearCache ()
 Clear the in-memory compilation cache (e.g.
static const QList< QShaderBaker::GeneratedShader > & bakeTargets ()
 Bake target list: SPIR-V 1.0, GLSL 330, GLSL ES 300/310/320.

Detailed Description

Static utility for GLSL → SPIR-V compilation with include resolution and caching.

Compilation results are cached by source hash to avoid redundant QShaderBaker invocations. Those two caches (source-hash and filename+mtime) are in-memory, but they sit in front of a content-addressed ON-DISK cache of serialized QShaders under GenericCacheLocation, keyed by Qt version, which the disk read consults BEFORE taking the bake lock. So a compile result normally survives a process restart. The disk cache is pruned once per process, on the first write, when it exceeds 512 entries and then down to about 90% of that, so a long-lived daemon is not capped within its own lifetime. Set PHOSPHOR_DISABLE_SHADER_DISK_CACHE to opt out.

Thread-safety
All methods are safe to call from any thread. An in-memory cache read takes only a short cache mutex, never the bake mutex; the disk read takes neither. Cache misses serialize on an internal bake mutex — QShaderBaker (glslang) is not reentrant, and concurrent bake() calls crash inside QSpirvCompiler::compileToSpirv(), so the mutex is load-bearing and must not be removed. loadAndExpand() is pure I/O and runs concurrently.

Member Function Documentation

◆ bakeTargets()

const QList< QShaderBaker::GeneratedShader > & PhosphorRendering::ShaderCompiler::bakeTargets ( )
static

Bake target list: SPIR-V 1.0, GLSL 330, GLSL ES 300/310/320.

◆ clearCache()

void PhosphorRendering::ShaderCompiler::clearCache ( )
static

Clear the in-memory compilation cache (e.g.

on shader hot-reload).

Clears both the source-hash BakeCache AND the filename+mtime cache in the node core, so the next prepare() re-reads the shader files from disk. It does NOT clear the on-disk cache, so an UNEDITED file is re-read and then served from the disk blob without running glslang again; an edited one has a new digest and misses. Both in-memory caches share clearCache() so consumers can call one function to invalidate.

◆ compile()

Result PhosphorRendering::ShaderCompiler::compile ( const QByteArray & source,
QShader::Stage stage )
static

Compile GLSL source to QShader (SPIR-V 1.0 + GLSL 330 + ES 300/310/320).

Cached by source hash; second call with same source returns immediately.

◆ compileFromFile()

Result PhosphorRendering::ShaderCompiler::compileFromFile ( const QString & path,
const QStringList & includePaths )
static

Load shader from file, expand #include directives, then compile.

Parameters
pathPath to .frag or .vert file
includePathsDirectories to search for #include directives

◆ expandSource()

QString PhosphorRendering::ShaderCompiler::expandSource ( const QString & source,
const QString & currentFileDir,
const QStringList & includePaths,
QString * outError = nullptr,
QStringList * outIncludedPaths = nullptr )
static

Expand #include directives in already-read source, without touching the filesystem for the top-level text.

Shares its search-path policy with loadAndExpand (currentFileDir searched first, then includePaths), so a caller that must transform the raw source before expansion — e.g. the T1.4 entry-point harness, which prepends a #version/include scaffold to an entry-only pack — gets byte-identical expansion to the load-from-path path.

Parameters
sourceRaw (already-read) GLSL, possibly harness-assembled
currentFileDirDirectory of the top-level shader, searched first
includePathsAdditional #include search directories
outErrorIf non-null, set on error
outIncludedPathsIf non-null, appended with each included header's canonical path (same fingerprint contract as loadAndExpand)
Returns
Expanded GLSL source, or empty string on error

◆ loadAndExpand()

QString PhosphorRendering::ShaderCompiler::loadAndExpand ( const QString & path,
const QStringList & includePaths,
QString * outError = nullptr,
QStringList * outIncludedPaths = nullptr )
static

Load a shader file and expand #include directives without compiling.

Parameters
pathPath to .frag or .vert file
includePathsDirectories to search for #include directives
outErrorIf non-null, set on error
outIncludedPathsIf non-null, appended with the canonical absolute path of every transitively-included header. Lets callers fingerprint includes so cache-invalidation responds to header edits, not just edits to the top-level file.
Returns
Expanded GLSL source, or empty string on error

The documentation for this class was generated from the following file: