From 5eff189bbf6cda19b3b71f6a60c60de4f7aa3312 Mon Sep 17 00:00:00 2001 From: Aiden Date: Fri, 8 May 2026 13:35:15 +1000 Subject: [PATCH] random float --- README.md | 1 - SHADER_CONTRACT.md | 4 +++- .../gl/GlobalParamsBuffer.cpp | 1 + .../runtime/RuntimeHost.cpp | 10 ++++++++++ .../runtime/RuntimeHost.h | 1 + .../shader/ShaderTypes.h | 1 + runtime/templates/shader_wrapper.slang.in | 3 +++ tests/Std140BufferTests.cpp | 14 ++++++++------ 8 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e5bdbdb..b8de459 100644 --- a/README.md +++ b/README.md @@ -245,4 +245,3 @@ If `SLANG_ROOT` is not set, the workflow falls back to the repo-local default un - allow shaders to read other shaders data store based on name? or putput over OSC - Mipmappong needed? - unwrap a fish eyelens and mirror it and map it to equirectangulr for environmnet map purposes -- add a random flaot between 0-1 that changes each time the application loads for shaders to use diff --git a/SHADER_CONTRACT.md b/SHADER_CONTRACT.md index c3d7844..c4680fe 100644 --- a/SHADER_CONTRACT.md +++ b/SHADER_CONTRACT.md @@ -76,7 +76,7 @@ Important rules: - Keep parameter IDs, texture IDs, font IDs, and function entry points as valid shader identifiers: letters, numbers, and underscores only, starting with a letter or underscore. - Add only controls that are actually used by the shader. - Prefer a small number of clear controls with conservative defaults. -- Keep shaders deterministic unless randomness is an explicit feature. For stable pseudo-randomness, hash from `uv`, pixel coordinates, `frameCount`, or trigger values rather than using unavailable global state. +- Keep shaders deterministic unless randomness is an explicit feature. For stable process-level variation, use `context.startupRandom`; for per-pixel pseudo-randomness, hash from `uv`, pixel coordinates, `frameCount`, or trigger values. - If adapting third-party code, include attribution and source URL in the manifest description when the license allows adaptation. - If the source license is unclear or incompatible, do not add the shader package. @@ -149,6 +149,7 @@ struct ShaderContext float time; float utcTimeSeconds; float utcOffsetSeconds; + float startupRandom; float frameCount; float mixAmount; float bypass; @@ -166,6 +167,7 @@ Fields: - `time`: elapsed runtime time in seconds. - `utcTimeSeconds`: current UTC time of day from the host PC clock, expressed as seconds since UTC midnight. - `utcOffsetSeconds`: host PC local UTC offset in seconds. Add this to `utcTimeSeconds` and wrap to `0..86400` to get local time of day. +- `startupRandom`: random `0..1` value generated once when the host process starts. It stays constant for the lifetime of the app and changes on the next launch. - `frameCount`: incrementing frame counter. - `mixAmount`: runtime mix amount. - `bypass`: `1.0` when the layer is bypassed, otherwise `0.0`. diff --git a/apps/LoopThroughWithOpenGLCompositing/gl/GlobalParamsBuffer.cpp b/apps/LoopThroughWithOpenGLCompositing/gl/GlobalParamsBuffer.cpp index c0634f7..395015d 100644 --- a/apps/LoopThroughWithOpenGLCompositing/gl/GlobalParamsBuffer.cpp +++ b/apps/LoopThroughWithOpenGLCompositing/gl/GlobalParamsBuffer.cpp @@ -20,6 +20,7 @@ bool GlobalParamsBuffer::Update(const RuntimeRenderState& state, unsigned availa AppendStd140Vec2(buffer, static_cast(state.outputWidth), static_cast(state.outputHeight)); AppendStd140Float(buffer, static_cast(state.utcTimeSeconds)); AppendStd140Float(buffer, static_cast(state.utcOffsetSeconds)); + AppendStd140Float(buffer, static_cast(state.startupRandom)); AppendStd140Float(buffer, static_cast(state.frameCount)); AppendStd140Float(buffer, static_cast(state.mixAmount)); AppendStd140Float(buffer, static_cast(state.bypass)); diff --git a/apps/LoopThroughWithOpenGLCompositing/runtime/RuntimeHost.cpp b/apps/LoopThroughWithOpenGLCompositing/runtime/RuntimeHost.cpp index d09a03e..381555f 100644 --- a/apps/LoopThroughWithOpenGLCompositing/runtime/RuntimeHost.cpp +++ b/apps/LoopThroughWithOpenGLCompositing/runtime/RuntimeHost.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -55,6 +56,13 @@ bool MatchesControlKey(const std::string& candidate, const std::string& key) return candidate == key || SimplifyControlKey(candidate) == SimplifyControlKey(key); } +double GenerateStartupRandom() +{ + std::random_device randomDevice; + std::uniform_real_distribution distribution(0.0, 1.0); + return distribution(randomDevice); +} + bool TryParseLayerIdNumber(const std::string& layerId, uint64_t& number) { const std::string prefix = "layer-"; @@ -691,6 +699,7 @@ RuntimeHost::RuntimeHost() mCompletionIntervalMilliseconds(0.0), mSmoothedCompletionIntervalMilliseconds(0.0), mMaxCompletionIntervalMilliseconds(0.0), + mStartupRandom(GenerateStartupRandom()), mLateFrameCount(0), mDroppedFrameCount(0), mFlushedFrameCount(0), @@ -1351,6 +1360,7 @@ void RuntimeHost::RefreshDynamicRenderStateFields(std::vector