optimointeja

This commit is contained in:
2025-08-01 15:38:19 +03:00
parent 353c4440ea
commit 292a05096f
3 changed files with 23 additions and 52 deletions

View File

@ -23,7 +23,9 @@ static unsigned int bit_reverse(unsigned int x, int log2n) {
return n; return n;
} }
void compute_fft(float* time_data, float* freq_out) { void compute_fft(float* time_data, float* freq_out) {
static float real[FFT_SIZE]; static float real[FFT_SIZE];
static float imag[FFT_SIZE]; static float imag[FFT_SIZE];

View File

@ -1,18 +1,18 @@
// custom build and feature flags // custom build and feature flags
#ifdef DEBUG #ifdef DEBUG
#define OPENGL_DEBUG 0 #define OPENGL_DEBUG 0
#define FULLSCREEN 0 #define FULLSCREEN 1
#define DESPERATE 0 #define DESPERATE 0
#define BREAK_COMPATIBILITY 0 #define BREAK_COMPATIBILITY 0
#else #else
#define OPENGL_DEBUG 0 #define OPENGL_DEBUG 0
#define FULLSCREEN 0 #define FULLSCREEN 1
#define DESPERATE 0 #define DESPERATE 0
#define BREAK_COMPATIBILITY 0 #define BREAK_COMPATIBILITY 0
#endif #endif
#define POST_PASS 0 #define POST_PASS 0
#define USE_MIPMAPS 1 #define USE_MIPMAPS 0
#define USE_AUDIO 1 #define USE_AUDIO 1
#define NO_UNIFORMS 0 #define NO_UNIFORMS 0
@ -44,6 +44,10 @@ static float fft_input[FFT_SIZE];
static float fft_output[FFT_SIZE / 2]; static float fft_output[FFT_SIZE / 2];
static float fft_uniform[FFT_SIZE / 4]; static float fft_uniform[FFT_SIZE / 4];
static float syncs[1 + SU_NUMSYNCS];
#define SU_VALUE SU_LENGTH_IN_SAMPLES * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE
void entrypoint(void) void entrypoint(void)
#else #else
#include "editor.h" #include "editor.h"
@ -96,7 +100,7 @@ int __cdecl main(int argc, char* argv[])
LPVOID p1; LPVOID p1;
DWORD l1; DWORD l1;
IDirectSoundBuffer_Lock(direct_sound_buffer, 0, SU_LENGTH_IN_SAMPLES * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE, &p1, &l1, NULL, NULL, 0); IDirectSoundBuffer_Lock(direct_sound_buffer, 0, SU_VALUE, &p1, &l1, NULL, NULL, 0);
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)su_render_song, p1, 0, 0); CreateThread(0, 0, (LPTHREAD_START_ROUTINE)su_render_song, p1, 0, 0);
@ -112,23 +116,21 @@ int __cdecl main(int argc, char* argv[])
track.play(); track.play();
double position = 0.0; double position = 0.0;
#endif #endif
static float syncs[1 + SU_NUMSYNCS];
long playCursor = 0; long playCursor = 0;
long lastPlayCursor = -1; long lastPlayCursor = -1;
volatile float maximum = 0.0; // Helper variable to calculate maximum fft output for normalization
// Unlock buffer for next use // Unlock buffer for next use
IDirectSoundBuffer_Unlock(direct_sound_buffer, p1, SU_LENGTH_IN_SAMPLES * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE, NULL, NULL); IDirectSoundBuffer_Unlock(direct_sound_buffer, p1, SU_VALUE, NULL, NULL);
// Play sound // Play sound
direct_sound_buffer->Play(0, 0, 0); direct_sound_buffer->Play(0, 0, 0);
// Init FFT // Init FFT
init_hamming_window(); init_hamming_window();
PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv")); PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"));
const ULONGLONG targetIntervalMs = 1000 / 60; // For 60 FPS FFT updates ULONGLONG targetIntervalMs = 1000 / 60; // For 60 FPS FFT updates
do do
{ {
@ -177,7 +179,7 @@ init_hamming_window();
HRESULT hr = IDirectSoundBuffer_Lock(direct_sound_buffer, 0, FFT_SIZE * sizeof(SUsample), &audio_ptr, &audio_size, NULL, NULL, DSBLOCK_FROMWRITECURSOR); HRESULT hr = IDirectSoundBuffer_Lock(direct_sound_buffer, 0, FFT_SIZE * sizeof(SUsample), &audio_ptr, &audio_size, NULL, NULL, DSBLOCK_FROMWRITECURSOR);
if (SUCCEEDED(hr) && audio_ptr) { if (SUCCEEDED(hr) && audio_ptr) {
if (playCursor < ((SU_LENGTH_IN_SAMPLES * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE) - (FFT_SIZE * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE))) if (playCursor < ((SU_VALUE) - (FFT_SIZE * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE)))
{ {
SUsample* samples = (SUsample*)audio_ptr; SUsample* samples = (SUsample*)audio_ptr;
for (int i = 0; i < FFT_SIZE; ++i) { for (int i = 0; i < FFT_SIZE; ++i) {

View File

@ -1,12 +1,10 @@
#version 460 #version 460
precision mediump float; precision mediump float;
out vec4 o; out vec4 o;
const float PI = 3.14159265; float PHI = sqrt(5.) * 0.5 + 0.5;
const float TAU = (2. * PI);
const float PHI = sqrt(5.) * 0.5 + 0.5;
layout(location = 0) uniform float syncs[11]; layout(location = 0) uniform float syncs[11];
layout(location = 20) uniform float fft_output[512]; // FFT_SIZE / 4 layout(location = 20) uniform float fft_output[512]; // FFT_SIZE / 4
// float u_time = syncs[0]; float u_time = syncs[0];
// paletti muunnos: arg 0.8 --> 0.5 // paletti muunnos: arg 0.8 --> 0.5
vec3 palette(float t, float arg, float arg2){ vec3 palette(float t, float arg, float arg2){
@ -18,7 +16,7 @@ vec3 palette(float t, float arg, float arg2){
} }
vec2 getUV() { vec2 getUV() {
const vec2 u_resolution = vec2(1920, 1080); vec2 u_resolution = vec2(1920, 1080);
return ((gl_FragCoord.xy * 2. - u_resolution.xy) / u_resolution.y); return ((gl_FragCoord.xy * 2. - u_resolution.xy) / u_resolution.y);
//const vec2 scale = vec2(0.00104166667, 0.00185185185); //const vec2 scale = vec2(0.00104166667, 0.00185185185);
//return gl_FragCoord.xy * scale - 1.0; //return gl_FragCoord.xy * scale - 1.0;
@ -89,34 +87,6 @@ float hexDistance(vec2 axial) {
return max(abs(q), max(abs(r), abs(s))); return max(abs(q), max(abs(r), abs(s)));
} }
/*
float hexPylon(vec3 p, vec2 h) {
//vec3 p = vec3(p.x, p.z, p2.y);
vec3 b = vec3(h.x, h.y, h.x);
// Hexagon.
p.xz = abs(p.xz);
p.xz = vec2(p.x * .866025 + p.z * .5, p.z);
// The ".015" is a subtle rounding factor. Zero gives sharp edges,
// and larger numbers give a more rounded look.
return lev3(max(abs(p) - b + .15, 0.)) - .15;
}
*/
//////////////
// SCENE //
//////////////
/*
// instructions -> opU( { float to union with } , vec2( {put shape here}, {put material here} ) )
vec2 opU(vec2 d1, vec2 d2) {
return (d1.x < d2.x) ? d1 : d2;
}
float sdSphere(vec3 p, float r){
return lev3(p) -r;
}
*/
// Scene mapping with occlusion-aware SDF blending // Scene mapping with occlusion-aware SDF blending
vec2 mapScene(vec3 p) { vec2 mapScene(vec3 p) {
float dist = 20.; float dist = 20.;
@ -140,7 +110,7 @@ vec2 mapScene(vec3 p) {
HexData hex = hexTile(hexpos, 1.1); HexData hex = hexTile(hexpos, 1.1);
float distFromCenter = hexDistance(hex.axial); float distFromCenter = hexDistance(hex.axial);
int fftIndex = int(cl(distFromCenter + 1., 0.0, 511.0)); int fftIndex = int(cl(distFromCenter + 1., 0.0, 511.0));
float noise = mix(noise(hex.axial+1., 0.1), noise(hex.axial+1., 0.2), sin(syncs[0] * 4.)); float noise = mix(noise(hex.axial+1., 0.1), noise(hex.axial+1., 0.2), sin(u_time * 4.));
float hexHeight = cl(1.0 + (fft_output[fftIndex] * 2.0 + noise), 0., 15.); float hexHeight = cl(1.0 + (fft_output[fftIndex] * 2.0 + noise), 0., 15.);
vec3 r = vec3(hex.local.x + offset.x,hex.local.y,hex.local.z+offset.y); vec3 r = vec3(hex.local.x + offset.x,hex.local.y,hex.local.z+offset.y);
// r.yz *= rot2D(PI * 0.5); // r.yz *= rot2D(PI * 0.5);
@ -157,18 +127,15 @@ vec2 mapScene(vec3 p) {
//////////////// ////////////////
vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) { vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
float mat = 0.;
float hit = 0.; float hit = 0.;
float t = 0.; // total distance travelled float t = 0.; // total distance travelled
const float Z_REPEAT_DIST = 1.5;
// Raymarching // Raymarching
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
pos = ro + rd * t; pos = ro + rd * t;
vec2 res = mapScene(pos); // Get distance to objects vec2 res = mapScene(pos); // Get distance to objects
mat = res.y;
if (t > 200.) break; if (t > 200.) break;
if (abs(res.x) < 1e-4*(t*0.00125 + 1.0)) { if (abs(res.x) < 1e-4) {
hit = 1.0; hit = 1.0;
break; break;
} }
@ -192,7 +159,7 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
// t += min(d.x, Z_REPEAT_DIST/5.0); // "march" the ray // t += min(d.x, Z_REPEAT_DIST/5.0); // "march" the ray
// } // }
return vec3(t, mat, hit); return vec3(t, 0., hit);
} }
//////////////// ////////////////
@ -261,7 +228,7 @@ vec3 pal(float color) {
vec3 c = palette(color, 0.25, 0.63); vec3 c = palette(color, 0.25, 0.63);
vec3 c2 = palette(color, 0.5 ,0.2); vec3 c2 = palette(color, 0.5 ,0.2);
float t = cl((syncs[0] - 129.0) / 2.0, 0.0, 1.0); // Smoothly ramps from 0 to 1 after 12s float t = cl((u_time - 129.0) / 2.0, 0.0, 1.0); // Smoothly ramps from 0 to 1 after 12s
return mix(c, c2, t); // Blend between c and c2 over ~2 seconds return mix(c, c2, t); // Blend between c and c2 over ~2 seconds
//return c2; //return c2;
} }
@ -352,7 +319,7 @@ vec3 getCameraRayDir(vec2 uv, vec3 camPos, vec3 camTarget, float fov) {
} }
vec3 render(vec2 uv) { vec3 render(vec2 uv) {
vec3 camPos = vec3(-20.0 + sin(syncs[0] * 0.25) * 5, abs(sin(syncs[0] * 0.25) * 10) + 25.0, -20.0); vec3 camPos = vec3(-20.0 + sin(u_time * 0.25) * 5, abs(sin(u_time * 0.25) * 10) + 25.0, -20.0);
// vec3 camTarget = vec3(0.) // Adjust target as needed // vec3 camTarget = vec3(0.) // Adjust target as needed
//float fov = 1.0; //float fov = 1.0;
@ -369,7 +336,7 @@ vec3 render(vec2 uv) {
} }
//glow from the bottom //glow from the bottom
vec3 bGlowColor = pal(syncs[0] * .075); // color change vec3 bGlowColor = pal(u_time * .075); // color change
float bGlowDistance = 0.8; float bGlowDistance = 0.8;
vec3 p = camPos + t.x * rayDir; vec3 p = camPos + t.x * rayDir;