ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
psyche-cache-code-cosmic-stained-glass.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file psyche-cache-code-cosmic-stained-glass.frag
5 * @brief Vulkan fragment shader: psyche-cache-code-cosmic-stained-glass.
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_high ext.audio_bands.z
18#define amp_low ext.audio_bands.x
19#define amp_mid ext.audio_bands.y
20#define amp_peak ext.u2.w
21#define amp_smooth ext.u3.w
22#define history_head int(ext.u3.x)
23#define iMouse ext.mouse
24#define iResolution ext.u0.zw
25#define spectrum_history_head int(ext.audio_history.x)
26#define spectrum_history_size int(ext.audio_history.y)
27#define time_f ext.u2.y
28
29// Spectral glass cells refract video while cached color leaks through the seams.
30layout(location = 0) in vec2 tc;
31layout(location = 0) out vec4 color;
32layout(set = 0, binding = 0) uniform sampler2D samp;
33layout(set = 0, binding = 2) uniform sampler2DArray history;
34
35#ifndef SIZE
36#define SIZE 8
37#endif
38#ifndef CACHE_HISTORY_LAYER
39#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
40#endif
41layout(set = 0, binding = 3) uniform sampler1D spectrum0;
42layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
43
44
45#ifndef SPECTRUM_HISTORY_LAYER
46#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
47#endif
48
49
50
51
52
53
54
55
56
57const float TAU = 6.28318530718;
58
59/**
60 * @brief Evaluates rotate 2d for this shader effect.
61 * @param angle Input value for the calculation.
62 * @return Computed mat2 value.
63 */
64mat2 rotate_2d(float angle) {
65 float cosine = cos(angle);
66 float sine = sin(angle);
67 return mat2(cosine, -sine, sine, cosine);
68}
69
70/**
71 * @brief Evaluates mirror repeat for this shader effect.
72 * @param point Input value for the calculation.
73 * @return Computed vec2 value.
74 */
75vec2 mirror_repeat(vec2 point) {
76 return 1.0 - abs(mod(point, 2.0) - 1.0);
77}
78
79/**
80 * @brief Evaluates hash 21 for this shader effect.
81 * @param point Input value for the calculation.
82 * @return Computed float value.
83 */
84float hash_21(vec2 point) {
85 return fract(sin(dot(point, vec2(127.1, 311.7))) * 43758.5453);
86}
87
88/**
89 * @brief Evaluates cell data for this shader effect.
90 * @param point Input value for the calculation.
91 * @return Computed vec3 value.
92 */
93vec3 cell_data(vec2 point) {
94 vec2 base = floor(point);
95 vec2 part = fract(point);
96 float nearest = 8.0;
97 float second_nearest = 8.0;
98 float identity = 0.0;
99 for (int y = -1; y <= 1; ++y) {
100 for (int x = -1; x <= 1; ++x) {
101 vec2 offset = vec2(float(x), float(y));
102 vec2 seed = vec2(hash_21(base + offset),
103 hash_21(base + offset + 17.4));
104 float distance_to_seed = length(offset + seed - part);
105 if (distance_to_seed < nearest) {
106 second_nearest = nearest;
107 nearest = distance_to_seed;
108 identity = hash_21(base + offset + 31.7);
109 } else if (distance_to_seed < second_nearest) {
110 second_nearest = distance_to_seed;
111 }
112 }
113 }
114 return vec3(nearest, second_nearest, identity);
115}
116
117/**
118 * @brief Evaluates palette for this shader effect.
119 * @param phase Input value for the calculation.
120 * @return Computed vec3 value.
121 */
122vec3 palette(float phase) {
123 return 0.5 + 0.5 * cos(TAU * (phase + vec3(0.0, 0.29, 0.63)));
124}
125
126/**
127 * @brief Evaluates sample cache for this shader effect.
128 * @param index Input value for the calculation.
129 * @param uv Input value for the calculation.
130 * @return Computed vec3 value.
131 */
132vec3 sample_cache(int index, vec2 uv) {
133 return texture(history, vec3(mirror_repeat(uv),
134 float(CACHE_HISTORY_LAYER(index)))).rgb;
135}
136
137/**
138 * @brief Evaluates history fft for this shader effect.
139 * @param index Input value for the calculation.
140 * @param frequency Input value for the calculation.
141 * @return Computed float value.
142 */
143float history_fft(int index, float frequency) {
144 int age = min(index + 1, max(spectrum_history_size - 1, 0));
145 return texture(spectrum_history,
146 vec2(frequency, float(SPECTRUM_HISTORY_LAYER(age)))).r;
147}
148
149/**
150 * @brief Executes the fragment shader entry point.
151 */
152void main() {
153 vec2 resolution = max(iResolution, vec2(1.0));
154 float aspect = resolution.x / resolution.y;
155 vec2 center = iMouse.z > 0.0 ? iMouse.xy / resolution : vec2(0.5);
156 vec2 point = (tc - center) * vec2(aspect, 1.0);
157 float bass = max(texture(spectrum0, 0.045).r, amp_low);
158 float middle = max(texture(spectrum0, 0.33).r, amp_mid);
159 float treble = max(texture(spectrum0, 0.83).r, amp_high);
160 vec2 glass_point = rotate_2d(time_f * 0.035 + middle * 0.16) * point *
161 (7.0 + treble * 4.0);
162 vec3 cell = cell_data(glass_point);
163 float seam = exp(-abs(cell.y - cell.x) * (45.0 + treble * 30.0));
164 vec2 refraction = normalize(fract(glass_point) - 0.5 + vec2(0.0001)) *
165 (cell.x - 0.25) * (0.025 + bass * 0.04);
166 vec2 live_uv = tc + refraction;
167 vec3 live = texture(samp, mirror_repeat(live_uv)).rgb;
168 live *= 0.65 + palette(cell.z + time_f * 0.03) * 0.7;
169 live += palette(cell.z - time_f * 0.05) * seam * (0.2 + treble * 0.38);
170
171 vec3 accumulated = live * 1.2;
172 float total_weight = 1.2;
173 for (int index = 0; index < SIZE; ++index) {
174 float age = float(index + 1) / float(max(SIZE, 1));
175 float old_bass = history_fft(index, 0.045);
176 float old_middle = history_fft(index, 0.33);
177 float old_treble = history_fft(index, 0.83);
178 vec2 cache_point = rotate_2d(age * (0.17 + old_middle * 0.75)) * point;
179 cache_point *= 1.0 - age * (0.09 + old_bass * 0.07);
180 vec2 cache_uv = cache_point / vec2(aspect, 1.0) + center;
181 cache_uv += refraction * (0.35 + age * old_treble);
182 vec3 memory = sample_cache(index, cache_uv);
183 memory *= 0.62 + palette(cell.z + age + old_middle) * 0.66;
184 memory += palette(age + cell.z) * seam * old_treble * 0.12;
185 float weight = exp(-age * 2.3) * (0.68 + old_bass * 0.55);
186 accumulated += memory * weight;
187 total_weight += weight;
188 }
189
190 vec3 result = accumulated / total_weight;
191 result += palette(cell.z + 0.5) * seam * amp_peak * 0.2;
192 result = (result - 0.5) * (1.1 + amp_smooth * 0.3) + 0.5;
193 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
194}
layout(set=0, binding=1, std140) uniform SpriteExtended
Definition 3high.frag:7
#define iMouse
Definition 3high.frag:17
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
#define amp_smooth
#define CACHE_HISTORY_LAYER(index)
#define amp_mid
mat2 rotate_2d(float angle)
Evaluates rotate 2d for this shader effect.
#define SIZE
float hash_21(vec2 point)
Evaluates hash 21 for this shader effect.
vec3 palette(float phase)
Evaluates palette for this shader effect.
#define amp_low
#define amp_high
#define SPECTRUM_HISTORY_LAYER(index)
vec2 mirror_repeat(vec2 point)
Evaluates mirror repeat for this shader effect.
vec4 sample_cache(int index, vec2 uv)
Evaluates sample cache for this shader effect.
#define amp_peak
#define spectrum_history_size
#define seed
Definition air-bowl.frag:20
float history_fft(int index, float frequency)
Evaluates history fft for this shader effect.
vec2 cell(vec2 p)
Evaluates cell for this shader effect.
#define TAU
#define frequency
Definition ol.frag:13
vec3 cell_data(vec2 point)
Evaluates cell data for this shader effect.