ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
inv_cache_code_dreamlogic_mosaic.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file inv_cache_code_dreamlogic_mosaic.frag
5 * @brief Vulkan fragment shader: inv cache code dreamlogic mosaic.
6 */
7layout(set = 0, binding = 1, std140) uniform SpriteExtended {
8 vec4 mouse;
9 vec4 u0;
10 vec4 u1;
11 vec4 u2;
12 vec4 u3;
13 vec4 custom_uniforms[16];
14 vec4 audio_bands;
15 vec4 audio_history;
17#define amp_peak ext.u2.w
18#define amp_smooth ext.u3.w
19#define history_head int(ext.u3.x)
20#define iResolution ext.u0.zw
21#define iTime ext.u0.y
22#define spectrum_history_head int(ext.audio_history.x)
23#define spectrum_history_size int(ext.audio_history.y)
24#define time_f ext.u2.y
25
26
27// Cache-driven perceptual energy field: dreamlogic mosaic.
28layout(location = 0) in vec2 tc;
29layout(location = 0) out vec4 color;
30layout(set = 0, binding = 0) uniform sampler2D samp;
31layout(set = 0, binding = 2) uniform sampler2DArray history;
32
33#ifndef SIZE
34#define SIZE 8
35#endif
36#ifndef CACHE_HISTORY_LAYER
37#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
38#endif
39layout(set = 0, binding = 3) uniform sampler1D spectrum0;
40layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
41
42
43#ifndef SPECTRUM_HISTORY_LAYER
44#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
45#endif
46
47
48
49
50
51
52const float PI = 3.14159265359;
53const float TAU = 6.28318530718;
54const float HUE_OFFSET = 0.84;
55const float DISTORTION = 0.98;
56const float PERSISTENCE = 0.93;
57
58/**
59 * @brief Evaluates clockTime for this shader effect.
60 * @return Computed float value.
61 */
62float clockTime() {
63 return max(time_f, iTime);
64}
65
66/**
67 * @brief Evaluates hash21 for this shader effect.
68 * @param p Input value for the calculation.
69 * @return Computed float value.
70 */
71float hash21(vec2 p) {
72 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
73}
74
75/**
76 * @brief Evaluates hash22 for this shader effect.
77 * @param p Input value for the calculation.
78 * @return Computed vec2 value.
79 */
80vec2 hash22(vec2 p) {
81 return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453123);
82}
83
84/**
85 * @brief Evaluates noise21 for this shader effect.
86 * @param p Input value for the calculation.
87 * @return Computed float value.
88 */
89float noise21(vec2 p) {
90 vec2 cell = floor(p);
91 vec2 local = fract(p);
92 local = local * local * (3.0 - 2.0 * local);
93 return mix(mix(hash21(cell), hash21(cell + vec2(1.0, 0.0)), local.x),
94 mix(hash21(cell + vec2(0.0, 1.0)), hash21(cell + 1.0), local.x), local.y);
95}
96
97/**
98 * @brief Evaluates fbm for this shader effect.
99 * @param p Input value for the calculation.
100 * @return Computed float value.
101 */
102float fbm(vec2 p) {
103 float value = 0.0;
104 float weight = 0.5;
105 mat2 basis = mat2(0.80, -0.60, 0.60, 0.80);
106 for (int octave = 0; octave < 5; ++octave) {
107 value += noise21(p) * weight;
108 p = basis * p * 2.03 + vec2(1.7, 2.4);
109 weight *= 0.5;
110 }
111 return value;
112}
113
114/**
115 * @brief Evaluates mirrorUV for this shader effect.
116 * @param uv Input value for the calculation.
117 * @return Computed vec2 value.
118 */
119vec2 mirrorUV(vec2 uv) {
120 return 1.0 - abs(mod(uv, 2.0) - 1.0);
121}
122
123/**
124 * @brief Evaluates perceptualPalette for this shader effect.
125 * @param phase Input value for the calculation.
126 * @return Computed vec3 value.
127 */
128vec3 perceptualPalette(float phase) {
129 vec3 wave = 0.5 + 0.5 * cos(TAU * (phase + HUE_OFFSET + vec3(0.00, 0.30, 0.66)));
130 vec3 opponent = mix(wave, wave.gbr, 0.22);
131 return clamp((opponent - 0.05) * 1.10, 0.0, 1.0);
132}
133
134/**
135 * @brief Evaluates toneMap for this shader effect.
136 * @param value Input value for the calculation.
137 * @return Computed vec3 value.
138 */
139vec3 toneMap(vec3 value) {
140 value = max(value, 0.0);
141 return 1.0 - exp(-value * (1.12 + value * 0.08));
142}
143
144/**
145 * @brief Evaluates cacheFrame for this shader effect.
146 * @param index Input value for the calculation.
147 * @param uv Input value for the calculation.
148 * @return Computed vec4 value.
149 */
150vec4 cacheFrame(int index, vec2 uv) {
151 if (index == 0)
152 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(0))));
153 if (index == 1)
154 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(1))));
155 if (index == 2)
156 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(2))));
157 if (index == 3)
158 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(3))));
159 if (index == 4)
160 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(4))));
161 if (index == 5)
162 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(5))));
163 if (index == 6)
164 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(6))));
165 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(7))));
166}
167
168/**
169 * @brief Evaluates spectrumHistory for this shader effect.
170 * @param index Input value for the calculation.
171 * @param frequency Input value for the calculation.
172 * @return Computed float value.
173 */
174float spectrumHistory(int index, float frequency) {
175 if (index == 0)
176 return texture(spectrum0, frequency).r;
177 if (index == 1)
178 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(1)))).r;
179 if (index == 2)
180 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(2)))).r;
181 if (index == 3)
182 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(3)))).r;
183 if (index == 4)
184 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(4)))).r;
185 if (index == 5)
186 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(5)))).r;
187 if (index == 6)
188 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(6)))).r;
189 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(7)))).r;
190}
191
192/**
193 * @brief Evaluates historyBands for this shader effect.
194 * @param index Input value for the calculation.
195 * @return Computed vec4 value.
196 */
197vec4 historyBands(int index) {
198 return vec4(spectrumHistory(index, 0.04), spectrumHistory(index, 0.22),
199 spectrumHistory(index, 0.58), spectrumHistory(index, 0.86));
200}
201
202/**
203 * @brief Evaluates perceptualField for this shader effect.
204 * @param p Input value for the calculation.
205 * @param age Input value for the calculation.
206 * @param band Input value for the calculation.
207 * @return Computed vec3 value.
208 */
209vec3 perceptualField(vec2 p, float age, vec4 band) {
210 float t = clockTime();
211 vec2 warped = p + vec2(fbm(p * 2.0 + t * 0.035), fbm(p * 2.0 + 8.0 - t * 0.03)) * 0.23;
212 vec2 grid = warped * (5.0 + band.y * 3.0);
213 vec2 id = floor(grid);
214 vec2 local = fract(grid);
215 float nearest = 8.0;
216 float second = 8.0;
217 vec2 direction = vec2(0.0);
218 for (int y = -1; y <= 1; ++y) {
219 for (int x = -1; x <= 1; ++x) {
220 vec2 neighbor = vec2(float(x), float(y));
221 vec2 point = 0.5 + 0.4 * sin(TAU * hash22(id + neighbor) + t * 0.12 + age);
222 vec2 delta = neighbor + point - local;
223 float distanceToPoint = length(delta);
224 if (distanceToPoint < nearest) {
225 second = nearest;
226 nearest = distanceToPoint;
227 direction = delta;
228 } else if (distanceToPoint < second) {
229 second = distanceToPoint;
230 }
231 }
232 }
233 float boundary = exp(-(second - nearest) * (18.0 + band.z * 34.0));
234 float roomTone = 0.5 + 0.5 * sin(hash21(id) * TAU + t * 0.18 - age * 0.4);
235 return vec3(boundary * 0.75 + roomTone * 0.25, normalize(direction + vec2(0.001)) * boundary);
236}
237
238/**
239 * @brief Executes the fragment shader entry point.
240 */
241void main(void) {
242 float aspect = max(iResolution.x, 1.0) / max(iResolution.y, 1.0);
243 vec2 aspectScale = vec2(aspect, 1.0);
244 vec2 p = (tc - 0.5) * aspectScale;
245 float eccentricity = smoothstep(0.06, 0.92, length(p));
246 vec3 live = texture(samp, tc).rgb;
247
248 vec3 accumulation = live * 0.42;
249 float totalWeight = 0.42;
250 vec4 newest = historyBands(0);
251 vec4 oldest = historyBands(7);
252 float temporalDelta = length(newest - oldest);
253
254 for (int index = 0; index < 8; ++index) {
255 float age = float(index);
256 vec4 bands = historyBands(index);
257 vec3 field = perceptualField(p, age, bands);
258 float energy = clamp(abs(field.x), 0.0, 2.0);
259 vec2 flow = clamp(field.yz, vec2(-2.0), vec2(2.0));
260
261 float breathing = 1.0 + sin(clockTime() * 0.21 - age * 0.16) * (0.006 + bands.x * 0.018);
262 vec2 historyUV = (tc - 0.5) * breathing + 0.5;
263 float peripheralGain = mix(0.45, 1.0, eccentricity);
264 historyUV += flow / aspectScale * DISTORTION * (0.007 + bands.y * 0.018) * peripheralGain;
265 historyUV = mirrorUV(historyUV);
266
267 vec3 cached = cacheFrame(index, historyUV).rgb;
268 vec3 afterimage = mix(cached, cached.gbr, 0.10 + age * 0.025);
269 vec3 fieldColor =
270 perceptualPalette(energy * 0.21 + age * 0.073 + bands.w * 0.27 + clockTime() * 0.010);
271 float luminous = pow(clamp(energy, 0.0, 1.0), 2.0);
272 vec3 layer = mix(afterimage, afterimage * fieldColor * 1.18, 0.44);
273 layer += fieldColor * luminous * (0.10 + bands.z * 0.48);
274
275 float weight = pow(PERSISTENCE, age + 1.0) * (0.46 + bands.x * 0.66 + bands.y * 0.34);
276 accumulation += layer * weight;
277 totalWeight += weight;
278 }
279
280 vec3 result = accumulation / max(totalWeight, 0.001);
281 vec3 currentField = perceptualField(p, 0.0, newest);
282 float currentEnergy = clamp(abs(currentField.x), 0.0, 1.4);
283 vec3 auraColor =
284 perceptualPalette(currentEnergy * 0.28 + temporalDelta * 0.18 + clockTime() * 0.012);
285 float aura = pow(clamp(currentEnergy, 0.0, 1.0), 2.5);
286 float fieldEdge = fwidth(currentField.x);
287
288 result = 1.0 - (1.0 - result) * (1.0 - clamp(auraColor * aura * 0.28, 0.0, 0.72));
289 result += auraColor * fieldEdge * (1.1 + newest.w * 2.2);
290 result = mix(result, live, 0.10 * (1.0 - eccentricity));
291 result *= 1.0 + clamp(amp_smooth, 0.0, 1.0) * 0.16;
292 result = toneMap(result);
293
294 float inversion =
295 smoothstep(0.72, 0.98, amp_peak) * smoothstep(0.06, 0.48, temporalDelta) * 0.64;
296 vec3 inverted = mix(vec3(1.0) - result, (vec3(1.0) - result).brg, 0.24);
297 result = mix(result, inverted, inversion);
298 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
299}
layout(set=0, binding=1, std140) uniform SpriteExtended
Definition 3high.frag:7
ext
Definition 3high.frag:16
void main(void)
Executes the fragment shader entry point.
Definition 3high.frag:47
#define iResolution
Definition 3high.frag:18
#define time_f
Definition 3high.frag:19
float fbm(vec2 point)
Evaluates fbm for this shader effect.
#define amp_smooth
#define CACHE_HISTORY_LAYER(index)
#define SPECTRUM_HISTORY_LAYER(index)
#define amp_peak
vec3 wave(ivec2 p, ivec2 size)
Evaluates wave for this shader effect.
float energy()
Evaluates energy for this shader effect.
vec2 mirrorUV(vec2 uv)
Evaluates mirrorUV for this shader effect.
vec2 hash22(vec2 p)
Evaluates hash22 for this shader effect.
float hash21(vec2 p)
Evaluates hash21 for this shader effect.
float field(vec2 p, float tm)
Evaluates field for this shader effect.
float noise21(vec2 p)
Evaluates noise21 for this shader effect.
vec2 cell(vec2 p)
Evaluates cell for this shader effect.
#define TAU
#define PI
const float DISTORTION
vec4 historyBands(int index)
Evaluates historyBands for this shader effect.
vec4 cacheFrame(int index, vec2 uv)
Evaluates cacheFrame for this shader effect.
float spectrumHistory(int index, float frequency)
Evaluates spectrumHistory for this shader effect.
vec3 toneMap(vec3 value)
Evaluates toneMap for this shader effect.
float clockTime()
Evaluates clockTime for this shader effect.
vec3 perceptualPalette(float phase)
Evaluates perceptualPalette for this shader effect.
vec3 perceptualField(vec2 p, float age, vec4 band)
Evaluates perceptualField for this shader effect.
#define frequency
Definition ol.frag:13