ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
ant_texture_cache_spectrum_scale_chromatic.frag
Go to the documentation of this file.
1#version 450
2
3layout(set = 0, binding = 1, std140) uniform SpriteExtended {
4 vec4 mouse;
5 vec4 u0;
6 vec4 u1;
7 vec4 u2;
8 vec4 u3;
9 vec4 custom_uniforms[16];
10 vec4 audio_bands;
11 vec4 audio_history;
13#define amp_peak ext.u2.w
14#define amp_smooth ext.u3.w
15#define history_head int(ext.u3.x)
16#define iResolution ext.u0.zw
17#define spectrum_history_head int(ext.audio_history.x)
18#define spectrum_history_size int(ext.audio_history.y)
19#define time_f ext.u2.y
20
21/**
22 * @file ant_texture_cache_spectrum_scale_chromatic.frag
23 * @brief Vulkan fragment shader: ant texture cache spectrum scale chromatic.
24 */
25// ant_texture_cache_spectrum_scale_chromatic
26// Trail-cache shader scaled by SIZE (--texture-cache-size).
27// Uses textures[0..SIZE-1] for cached frames and spectrum0..7 for audio.
28
29#ifndef SIZE
30#define SIZE 8
31#endif
32layout(location = 0) in vec2 tc;
33layout(location = 0) out vec4 color;
34layout(set = 0, binding = 0) uniform sampler2D samp;
35layout(set = 0, binding = 2) uniform sampler2DArray history;
36
37#ifndef SIZE
38#define SIZE 8
39#endif
40#ifndef CACHE_HISTORY_LAYER
41#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
42#endif
43layout(set = 0, binding = 3) uniform sampler1D spectrum0;
44layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
45
46
47#ifndef SPECTRUM_HISTORY_LAYER
48#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
49#endif
50
51
52
53
54
55const float TAU = 6.28318530718;
56
57/**
58 * @brief Evaluates specHist for this shader effect.
59 * @param i Input value for the calculation.
60 * @param f Input value for the calculation.
61 * @return Computed float value.
62 */
63float specHist(int i, float f) {
64 int j = i & 7;
65 if (j == 0) return texture(spectrum0, f).r;
66 if (j == 1) return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(1)))).r;
67 if (j == 2) return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(2)))).r;
68 if (j == 3) return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(3)))).r;
69 if (j == 4) return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(4)))).r;
70 if (j == 5) return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(5)))).r;
71 if (j == 6) return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(6)))).r;
72 return texture(spectrum_history, vec2(f, float(SPECTRUM_HISTORY_LAYER(7)))).r;
73}
74
75/**
76 * @brief Evaluates rot for this shader effect.
77 * @param p Input value for the calculation.
78 * @param a Input value for the calculation.
79 * @return Computed vec2 value.
80 */
81vec2 rot(vec2 p, float a) {
82 float c = cos(a); float s = sin(a);
83 return vec2(c * p.x - s * p.y, s * p.x + c * p.y);
84}
85
86/**
87 * @brief Evaluates hash12 for this shader effect.
88 * @param p Input value for the calculation.
89 * @return Computed float value.
90 */
91float hash12(vec2 p) {
92 p = fract(p * vec2(123.34, 456.21));
93 p += dot(p, p + 45.32);
94 return fract(p.x * p.y);
95}
96
97/**
98 * @brief Evaluates palette for this shader effect.
99 * @param t Input value for the calculation.
100 * @param a Input value for the calculation.
101 * @param b Input value for the calculation.
102 * @param c Input value for the calculation.
103 * @param d Input value for the calculation.
104 * @return Computed vec3 value.
105 */
106vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
107 return a + b * cos(TAU * (c * t + d));
108}
109
110
111/**
112 * @brief Evaluates styleDrift for this shader effect.
113 * @param uv Input value for the calculation.
114 * @param layer Input value for the calculation.
115 * @param age Input value for the calculation.
116 * @param hB Input value for the calculation.
117 * @param hM Input value for the calculation.
118 * @param hT Input value for the calculation.
119 * @param hA Input value for the calculation.
120 * @param oldest Input value for the calculation.
121 * @return Computed vec2 value.
122 */
123vec2 styleDrift(vec2 uv, float layer, float age, float hB, float hM, float hT, float hA, vec3 oldest) {
124 return vec2(cos(layer * 0.4), sin(layer * 0.5)) * (0.004 + age * 0.025 + hB * 0.020);
125
126}
127
128/**
129 * @brief Evaluates styleColor for this shader effect.
130 * @param cached Input value for the calculation.
131 * @param layer Input value for the calculation.
132 * @param age Input value for the calculation.
133 * @param hB Input value for the calculation.
134 * @param hT Input value for the calculation.
135 * @return Computed vec3 value.
136 */
137vec3 styleColor(vec3 cached, float layer, float age, float hB, float hT) {
138 // Per-channel age-based offset accent
139 float k = age * 0.3;
140 vec3 c;
141 c.r = cached.r * (1.0 + k * 0.6);
142 c.g = cached.g * (1.0 - k * 0.2);
143 c.b = cached.b * (1.0 - k * 0.6);
144 return c;
145
146}
147
148/**
149 * @brief Executes the fragment shader entry point.
150 */
151void main() {
152 float aspect = iResolution.x / iResolution.y;
153 vec2 uv = (tc - 0.5) * vec2(aspect, 1.0);
154
155 float bass = texture(spectrum0, 0.03).r;
156 float mid = texture(spectrum0, 0.18).r;
157 float treble = texture(spectrum0, 0.52).r;
158 float air = texture(spectrum0, 0.86).r;
159
160 float histBass = 0.0;
161 float histMid = 0.0;
162 float histTreble = 0.0;
163 float histAir = 0.0;
164 for (int i = 0; i < 8; ++i) {
165 histBass += specHist(i, 0.03);
166 histMid += specHist(i, 0.18);
167 histTreble += specHist(i, 0.52);
168 histAir += specHist(i, 0.86);
169 }
170 histBass /= 8.0; histMid /= 8.0; histTreble /= 8.0; histAir /= 8.0;
171
172 vec3 oldest = texture(history, vec3(tc, float(CACHE_HISTORY_LAYER(SIZE - 1)))).rgb;
173
174 vec3 live = texture(samp, tc + styleDrift(uv, 0.0, 0.0, bass, mid, treble, air, oldest)).rgb;
175
176 // Soften decay for larger caches so distant frames remain visible.
177 float decay = mix(0.81, 0.94, clamp(float(SIZE - 8) / 24.0, 0.0, 1.0));
178
179 vec3 accum = live;
180 float wsum = 1.0;
181 for (int i = 0; i < SIZE; ++i) {
182 float layer = float(i + 1);
183 float age = layer / float(SIZE);
184 float hB = specHist(i, 0.03);
185 float hM = specHist(i, 0.18);
186 float hT = specHist(i, 0.52);
187 float hA = specHist(i, 0.86);
188
189 vec2 drift = styleDrift(uv, layer, age, hB, hM, hT, hA, oldest);
190 vec3 cached = texture(history, vec3(tc + drift, float(CACHE_HISTORY_LAYER(i)))).rgb;
191 cached = styleColor(cached, layer, age, hB, hT);
192
193 float w = pow(decay, layer) * (1.0 + hT * 0.9 + hA * 0.3);
194 accum += cached * w;
195 wsum += w;
196 }
197 accum /= wsum;
198
199 accum *= 0.96 + amp_smooth * 0.10;
200
201
202 color = vec4(clamp(accum, 0.0, 1.0), 1.0);
203}
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 amp_smooth
#define CACHE_HISTORY_LAYER(index)
#define SIZE
vec3 palette(float phase)
Evaluates palette for this shader effect.
#define SPECTRUM_HISTORY_LAYER(index)
vec2 rot(vec2 v, float a)
Evaluates rot for this shader effect.
Definition air-bowl.frag:52
float specHist(int i, float f)
Evaluates specHist for this shader effect.
vec2 styleDrift(vec2 uv, float layer, float age, float hB, float hM, float hT, float hA, vec3 oldest)
Evaluates styleDrift for this shader effect.
float hash12(vec2 p)
Evaluates hash12 for this shader effect.
vec3 styleColor(vec3 cached, float layer, float age, float hB, float hT)
Evaluates styleColor for this shader effect.
#define TAU