Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ yup_standalone_app (
libwebp
libjpeg
libgif
glslang
spirv_cross
${additional_modules}
${link_libraries})

Expand Down
199 changes: 199 additions & 0 deletions modules/yup_graphics/shading/yup_ShaderCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2026 - kunitoki@gmail.com

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

#if YUP_ENABLE_SHADER_COMPILER

//==============================================================================
namespace yup
{

ShaderCache::ShaderCache (ShaderTranspiler::Ptr transpilerToUse)
: transpiler (transpilerToUse)
{
jassert (transpiler != nullptr);
}

ShaderCache::~ShaderCache() = default;

//==============================================================================
ResultValue<MemoryBlock> ShaderCache::getOrCompile (const String& cacheKey,
const String& source,
ShaderStage stage,
ShaderLanguage sourceLang,
const TranspileOptions& options)
{
{
const CriticalSection::ScopedLockType sl (lock);

if (auto it = cache.find (cacheKey); it != cache.end())
{
it->second.lastAccessTime = Time::getCurrentTime().toMilliseconds();
return makeResultValueOk (MemoryBlock (it->second.spirv.getData(), it->second.spirv.getSize()));
}
}

auto result = transpiler->compileToSPIRV (source, stage, sourceLang, options);

if (result.failed())
return result;

{
const CriticalSection::ScopedLockType sl (lock);
evictIfNeeded();
}

store (cacheKey, result.getValue());

return result;
}

//==============================================================================
ResultValue<String> ShaderCache::getOrTranspile (const String& cacheKey,
const String& source,
ShaderStage stage,
ShaderLanguage sourceLang,
ShaderLanguage targetLang,
const TranspileOptions& options)
{
auto spirvResult = getOrCompile (cacheKey, source, stage, sourceLang, options);

if (spirvResult.failed())
return makeResultValueFail (spirvResult.getErrorMessage());

return transpiler->decompileFromSPIRV (spirvResult.getValue(), targetLang, options);
}

//==============================================================================
void ShaderCache::store (const String& key, MemoryBlock spirv)
{
const CriticalSection::ScopedLockType sl (lock);

Entry entry;
entry.spirv = std::move (spirv);
entry.lastAccessTime = Time::getCurrentTime().toMilliseconds();

cache.insert_or_assign (key, std::move (entry));

evictIfNeeded();
}

//==============================================================================
bool ShaderCache::contains (const String& key) const
{
const CriticalSection::ScopedLockType sl (lock);
return cache.find (key) != cache.end();
}

//==============================================================================
void ShaderCache::remove (const String& key)
{
const CriticalSection::ScopedLockType sl (lock);
cache.erase (key);
}

//==============================================================================
void ShaderCache::clear()
{
const CriticalSection::ScopedLockType sl (lock);
cache.clear();
}

//==============================================================================
size_t ShaderCache::getNumEntries() const
{
const CriticalSection::ScopedLockType sl (lock);
return cache.size();
}

//==============================================================================
size_t ShaderCache::getMemoryUsage() const
{
const CriticalSection::ScopedLockType sl (lock);

size_t total = 0;

for (const auto& [k, entry] : cache)
total += entry.spirv.getSize();

return total;
}

//==============================================================================
void ShaderCache::setMaxEntries (size_t max)
{
const CriticalSection::ScopedLockType sl (lock);
maxEntries = max;
evictIfNeeded();
}

//==============================================================================
size_t ShaderCache::getMaxEntries() const
{
const CriticalSection::ScopedLockType sl (lock);
return maxEntries;
}

//==============================================================================
String ShaderCache::generateCacheKey (const String& source,
ShaderStage stage,
ShaderLanguage sourceLang,
const TranspileOptions& options)
{
String payload;
payload << source
<< "|stage:" << static_cast<int> (stage)
<< "|lang:" << static_cast<int> (sourceLang)
<< '|' << options.toCacheKeyPayload();

SHA1 sha1 (payload.toRawUTF8(), payload.getNumBytesAsUTF8());
return sha1.toHexString();
}

//==============================================================================
void ShaderCache::evictIfNeeded()
{
// lock must already be held by the caller

if (maxEntries == 0)
return;

while (cache.size() > maxEntries)
{
// Evict least recently accessed
auto oldest = cache.begin();
int64 oldestTime = std::numeric_limits<int64>::max();

for (auto it = cache.begin(); it != cache.end(); ++it)
{
if (it->second.lastAccessTime < oldestTime)
{
oldestTime = it->second.lastAccessTime;
oldest = it;
}
}

cache.erase (oldest);
}
}

} // namespace yup

#endif // YUP_ENABLE_SHADER_COMPILER
176 changes: 176 additions & 0 deletions modules/yup_graphics/shading/yup_ShaderCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2026 - kunitoki@gmail.com

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

#pragma once

#include <yup_core/yup_core.h>

namespace yup
{

class ShaderTranspiler;
struct TranspileOptions;
enum class ShaderLanguage;
enum class ShaderStage;

//==============================================================================
/**
An in-memory cache for compiled SPIR-V shader binaries.

Caches the results of ShaderTranspiler::compileToSPIRV() keyed by a
hash of the source code plus compile options. When a subsequent request
matches an existing cache key, the cached SPIR-V binary is returned
instantly without recompiling.

The cache is thread-safe. Eviction uses a configurable entry-count limit.

The cache references an externally-owned ShaderTranspiler, which must
outlive the cache.

@code
auto transpiler = makeReferenceCounted<ShaderTranspiler>();
ShaderCache cache (*transpiler);

auto key = ShaderCache::generateCacheKey (source, ShaderStage::vertex,
ShaderLanguage::glsl, opts);
auto spirv = cache.getOrCompile (key, source, ShaderStage::vertex,
ShaderLanguage::glsl, opts);
@endcode

@see ShaderTranspiler
*/
class YUP_API ShaderCache final
{
public:
//==========================================================================
/**
Creates a cache that uses the given transpiler for miss compilations.

The transpiler must outlive this cache.
*/
explicit ShaderCache (ShaderTranspiler::Ptr transpilerToUse);
~ShaderCache();

//==========================================================================
/**
Looks up a cached SPIR-V binary, or compiles one if not found.

@param cacheKey Pre-computed cache key (see generateCacheKey).
@param source The shader source code (used on cache miss).
@param stage Pipeline stage (used on cache miss).
@param sourceLang Source language (used on cache miss).
@param options Compilation options (used on cache miss).

@returns The SPIR-V binary on success, or an error on failure.
*/
ResultValue<MemoryBlock> getOrCompile (const String& cacheKey,
const String& source,
ShaderStage stage,
ShaderLanguage sourceLang,
const TranspileOptions& options = {});

//==========================================================================
/**
Looks up a cached transpiled result, or transpiles if not found.

@param cacheKey Pre-computed cache key.
@param source The shader source code (used on cache miss).
@param stage Pipeline stage (used on cache miss).
@param sourceLang Source language (used on cache miss).
@param targetLang Target language (used on cache miss).
@param options Transpilation options (used on cache miss).

@returns The target language source code on success, or an error.
*/
ResultValue<String> getOrTranspile (const String& cacheKey,
const String& source,
ShaderStage stage,
ShaderLanguage sourceLang,
ShaderLanguage targetLang,
const TranspileOptions& options = {});

//==========================================================================
/** Store a SPIR-V binary directly into the cache under the given key. */
void store (const String& key, MemoryBlock spirv);

//==========================================================================
/** Returns true if the cache contains an entry for the given key. */
bool contains (const String& key) const;

//==========================================================================
/** Remove a single entry from the cache. */
void remove (const String& key);

//==========================================================================
/** Remove all entries from the cache. */
void clear();

//==========================================================================
/** Returns the number of currently cached entries. */
size_t getNumEntries() const;

//==========================================================================
/** Returns an approximate total byte usage of all cached entries. */
size_t getMemoryUsage() const;

//==========================================================================
/** Sets the maximum number of entries before eviction. 0 = unlimited. */
void setMaxEntries (size_t maxEntriesToUse);

//==========================================================================
/** Returns the maximum number of entries. */
size_t getMaxEntries() const;

//==========================================================================
/**
Generates a deterministic cache key from source + compile parameters.

Uses SHA1 internally. The key includes the source code, shader stage,
source language, and all transpile options.

@param source The shader source code.
@param stage Pipeline stage.
@param sourceLang Source language.
@param options Compilation options.

@returns A hex-encoded cache key string.
*/
static String generateCacheKey (const String& source,
ShaderStage stage,
ShaderLanguage sourceLang,
const TranspileOptions& options = {});

private:
struct Entry
{
MemoryBlock spirv;
int64 lastAccessTime = 0;
};

void evictIfNeeded();

ShaderTranspiler::Ptr transpiler;
std::map<String, Entry> cache;
size_t maxEntries = 256;
mutable CriticalSection lock;
};

} // namespace yup
Loading
Loading