add glsl join/split tools

main
Dominik Madarász 2023-08-10 23:53:51 +02:00
parent e5b504773f
commit 1c9f410e36
28 changed files with 2871 additions and 2170 deletions

View File

@ -255,25 +255,27 @@ if "%1"=="-?" goto showhelp
if "%1"=="-h" goto showhelp
if "%1"=="help" (
:showhelp
echo %0 ; compile everything: `make dll dev` alias
echo %0 [help] ; show this screen
echo %0 [docs] ; generate tools/docs/docs.html file
echo %0 [cook] ; cook .zipfiles with tools/cook.ini cookbook
echo %0 [sync] ; sync repo to latest
echo %0 [fwk] ; prepare files for fwk PR
echo %0 [lua] ; execute lua script with v4k
echo %0 [html5] ; build HTML5 demo
echo %0 [web] ; run Python webserver in html5 dir
echo %0 [pull] ; pull changes from 'latest' upstream
echo %0 [git] ; prepare for commit
echo %0 [push] ; prepare for commit, stage changes and commit them
echo %0 [tidy] ; clean up temp files
echo %0 [bind] ; generate lua bindings
echo %0 [checkmem] ; check untracked allocators in V4K
echo %0 [split^|join] ; engine/v4k* ^>split^> engine/split/* or engine/split/* ^>join^> engine/v4k*
echo %0 [amalgamation] ; combine engine/v4k* into a single-header file
echo %0 [prep] ; combine split files into a single-header file, ready for use
echo %0 [sln] ; generate a xcode/gmake/ninja/visual studio solution
echo %0 ; compile everything: `make dll dev` alias
echo %0 [help] ; show this screen
echo %0 [docs] ; generate tools/docs/docs.html file
echo %0 [cook] ; cook .zipfiles with tools/cook.ini cookbook
echo %0 [sync] ; sync repo to latest
echo %0 [fwk] ; prepare files for fwk PR
echo %0 [lua] ; execute lua script with v4k
echo %0 [html5] ; build HTML5 demo
echo %0 [web] ; run Python webserver in html5 dir
echo %0 [pull] ; pull changes from 'latest' upstream
echo %0 [git] ; prepare for commit
echo %0 [push] ; prepare for commit, stage changes and commit them
echo %0 [tidy] ; clean up temp files
echo %0 [bind] ; generate lua bindings
echo %0 [checkmem] ; check untracked allocators in V4K
echo %0 [split^|join] ; engine/v4k* ^>split^> engine/split/* or engine/split/* ^>join^> engine/v4k*
echo %0 [glsl_split^|glsl_join] ; join/split GLSL shaders
echo %0 [lua] ; execute lua script with v4k
echo %0 [amalgamation] ; combine engine/v4k* into a single-header file
echo %0 [prep] ; combine split files into a single-header file, ready for use
echo %0 [sln] ; generate a xcode/gmake/ninja/visual studio solution
echo %0 [cl^|tcc^|cc^|gcc^|clang^|clang-cl] [dbg^|dev^|rel] [static^|dll] [nov4k^|nodemos^|editor] [vis] [-- args]
echo cl \
echo tcc ^|
@ -374,6 +376,7 @@ if "%1"=="git" (
call make.bat docs
call make.bat bind
call make.bat glsl_join
call make.bat amalgamation
call make.bat split
@ -405,6 +408,7 @@ if "%1"=="push" (
)
if "%1"=="prep" (
call make.bat glsl_join
call make.bat join
call make.bat amalgamation
exit /b
@ -420,6 +424,15 @@ if "%1"=="join" (
exit /b
)
if "%1"=="glsl_split" (
call tools\glsl_split
exit /b
)
if "%1"=="glsl_join" (
call tools\glsl_join
exit /b
)
rem check memory api calls
if "%1"=="checkmem" (
findstr /RNC:"[^_xv]realloc[(]" engine\v4k.c engine\split\v4k*

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,109 @@
uniform vec3 lightPos = vec3(1.0);
uniform float doTexture = 1.;
#if VSMCUBE
uniform samplerCube shadowMap;
#else
uniform sampler2D shadowMap;
#endif
struct light {
vec3 position;
vec4 diffuse;
vec4 specular;
float constantAttenuation, linearAttenuation, quadraticAttenuation;
};
light light0 = light(
lightPos,
vec4(1,1,1,1),
vec4(1,1,1,1),
1.0, 0.0, 0.0
);
#if VSMCUBE
float chebyshevUpperBound(float distance, vec3 dir) {
distance = distance/20 ;
vec2 moments = texture(shadowMap, dir).rg;
#else
float chebyshevUpperBound(float distance, vec4 scPostW) {
vec2 moments = texture(shadowMap,scPostW.xy).rg;
#endif
if (distance <= moments.x)
return 1.0;
float variance = moments.y - (moments.x*moments.x);
variance = max(variance, 0.00002);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return p_max;
}
vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 uv, in vec4 sc) {
#ifndef VSMCUBE
return vec4(1.);
#else
vec3 fragment = vec3(vpeye);
vec3 normal = vec3(normalize(vneye));
vec3 viewDir = normalize(-fragment);
vec3 light = vec3(view * vec4(light0.position, 1.0));
#if VSMCUBE
vec3 fragmentToLight = light - fragment;
vec3 fragmentToLightDir = normalize(fragmentToLight);
vec4 fragmentToLight_world = inverse(view) * vec4(fragmentToLightDir, 0.0);
float shadowFactor = chebyshevUpperBound(length(fragmentToLight), -fragmentToLight_world.xyz);
#else
vec4 scPostW = sc / sc.w;
scPostW = scPostW * 0.5 + 0.5;
float shadowFactor = 1.0;
bool outsideShadowMap = sc.w <= 0.0f || (scPostW.x < 0 || scPostW.y < 0) || (scPostW.x >= 1 || scPostW.y >= 1);
if (!outsideShadowMap) {
shadowFactor = chebyshevUpperBound(scPostW.z, scPostW);
}
#endif
vec4 diffColor = vec4(1,1,1,1);
#if VSMCUBE
if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, -fragmentToLight_world.xyz).r), 1.0);
#else
if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, vec2(uv.x, 1.0 - uv.y)).r), 1.0);
#endif
#if 1
vec3 positionToLight = light - fragment;
vec3 lightDir = normalize(positionToLight);
float cosAngIncidence = dot(lightDir, normal);
cosAngIncidence = clamp(cosAngIncidence, 0, 1);
float attenuation = 1.0f;
attenuation = 1.0 / (light0.constantAttenuation + light0.linearAttenuation * length(positionToLight) + light0.quadraticAttenuation * pow(length(positionToLight),2));
vec4 diffuse = diffColor * light0.diffuse * cosAngIncidence * attenuation;
vec4 total_lighting;
total_lighting += vec4(0.1, 0.1, 0.1, 1.0) * diffColor;
total_lighting += diffuse * shadowFactor;
#else
vec4 total_lighting = diffColor;
#endif
return vec4(clamp(vec3(total_lighting), 0., 1.), 1.0);
#endif
}

View File

@ -0,0 +1,7 @@
uniform vec3 lightPos = vec3(1.0);
uniform float doTexture = 0.;
uniform sampler2D shadowMap;
vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 Texcoord, in vec4 sc) {
return vec4(1.);
};

View File

@ -0,0 +1,52 @@
uniform sampler2D u_texture;
in vec2 vTexCoord;
in vec4 vColor;
out vec4 fragColor;
vec4 texture_AA(sampler2D tx, vec2 uv) {
vec2 res = vec2(textureSize(tx, 0));
uv = uv*res + 0.5;
vec2 fl = floor(uv);
vec2 fr = fract(uv);
vec2 aa = fwidth(uv)*0.75;
fr = smoothstep( vec2(0.5)-aa, vec2(0.5)+aa, fr);
uv = (fl+fr-0.5) / res;
return texture(tx, uv);
}
vec4 texture_AA2( sampler2D tex, vec2 uv) {
vec2 res = vec2(textureSize(tex,0));
uv = uv*res;
vec2 seam = floor(uv+0.5);
uv = seam + clamp( (uv-seam)/fwidth(uv), -0.5, 0.5);
return texture(tex, uv/res);
}
vec4 texture_AA3(sampler2D tex, vec2 uv) {
vec2 res = vec2(textureSize(tex,0));
float width = 2.0;
uv = uv * res;
vec2 uv_floor = floor(uv + 0.5);
vec2 uv_fract = fract(uv + 0.5);
vec2 uv_aa = fwidth(uv) * width * 0.5;
uv_fract = smoothstep(
vec2(0.5) - uv_aa,
vec2(0.5) + uv_aa,
uv_fract
);
uv = (uv_floor + uv_fract - 0.5) / res;
return texture(tex, uv);
}
void main() {
vec4 texColor = texture_AA2(u_texture, vTexCoord);
if(texColor.a < 0.9) discard;
fragColor = vColor * texColor;
}

View File

@ -0,0 +1,16 @@
#define texture2D texture
#define texture2DLod textureLod
#define FRAGCOLOR fragColor
#define texcoord uv
#define TEXCOORD uv
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
uniform float iWidth, iHeight, iTime, iFrame, iMousex, iMousey;
uniform float iChannelRes0x, iChannelRes0y;
uniform float iChannelRes1x, iChannelRes1y;
vec2 iResolution = vec2(iWidth, iHeight);
vec2 iMouse = vec2(iMousex, iMousey);
vec2 iChannelResolution[2] = vec2[2]( vec2(iChannelRes0x, iChannelRes0y),vec2(iChannelRes1x, iChannelRes1y) );
float iGlobalTime = iTime;
in vec2 texcoord;
out vec4 fragColor;

View File

@ -0,0 +1,11 @@
uniform sampler2D texture0;
uniform float u_inv_gamma;
in vec2 uv;
out vec4 fragcolor;
void main() {
vec4 texel = texture( texture0, uv );
fragcolor = texel;
fragcolor.rgb = pow( fragcolor.rgb, vec3( u_inv_gamma ) );
}

View File

@ -0,0 +1,31 @@
uniform sampler2D u_texture_y;
uniform sampler2D u_texture_cb;
uniform sampler2D u_texture_cr;
uniform float u_gamma;
in vec2 uv;
out vec4 fragcolor;
void main() {
float y = texture(u_texture_y, uv).r;
float cb = texture(u_texture_cb, uv).r;
float cr = texture(u_texture_cr, uv).r;
const mat4 to_rgb = mat4(
1.0000, 1.0000, 1.0000, 0.0000,
0.0000, -0.3441, 1.7720, 0.0000,
1.4020, -0.7141, 0.0000, 0.0000,
-0.7010, 0.5291, -0.8860, 1.0000
);
vec4 texel = to_rgb * vec4(y, cb, cr, 1.0);
texel.rgb = pow(texel.rgb, vec3(1.0 / u_gamma));
if(false) { float saturation = 2.0; const vec3 W = vec3(0.2125, 0.7154, 0.0721);
vec3 intensity = vec3(dot(texel.rgb, W));
texel.rgb = mix(intensity, texel.rgb, saturation); }
fragcolor = vec4(texel.rgb, 1.0);
}

View File

@ -0,0 +1,71 @@
uniform mat4 model, view;
uniform sampler2D u_texture2d;
uniform vec3 u_coefficients_sh[9];
uniform bool u_textured = true;
uniform bool u_lit = false;
uniform bool u_matcaps = false;
uniform vec4 u_diffuse = vec4(1.0,1.0,1.0,1.0);
#ifdef RIM
in vec3 v_position;
#endif
in vec3 v_normal, v_normal_ws;
in vec2 v_texcoord;
in vec4 v_color;
out vec4 fragcolor;
{{include-shadowmap}}
in vec4 vpeye;
in vec4 vneye;
in vec4 sc;
vec4 shadowing() {
return shadowmap(vpeye, vneye, v_texcoord, sc);
}
void main() {
vec3 n = (v_normal);
vec4 lit = vec4(1.0, 1.0, 1.0, 1.0);
vec3 SHLightResult[9];
SHLightResult[0] = 0.282095f * u_coefficients_sh[0];
SHLightResult[1] = -0.488603f * u_coefficients_sh[1] * n.y;
SHLightResult[2] = 0.488603f * u_coefficients_sh[2] * n.z;
SHLightResult[3] = -0.488603f * u_coefficients_sh[3] * n.x;
SHLightResult[4] = 1.092548f * u_coefficients_sh[4] * n.x * n.y;
SHLightResult[5] = -1.092548f * u_coefficients_sh[5] * n.y * n.z;
SHLightResult[6] = 0.315392f * u_coefficients_sh[6] * (3.0f * n.z * n.z - 1.0f);
SHLightResult[7] = -1.092548f * u_coefficients_sh[7] * n.x * n.z;
SHLightResult[8] = 0.546274f * u_coefficients_sh[8] * (n.x * n.x - n.y * n.y);
vec3 result = vec3(0.0);
for (int i = 0; i < 9; ++i)
result += SHLightResult[i];
if( (result.x*result.x+result.y*result.y+result.z*result.z) > 0.0 ) lit = vec4(result, 1.0);
vec4 diffuse;
if(u_matcaps) {
vec2 muv = vec2(view * vec4(v_normal_ws, 0))*0.5+vec2(0.5,0.5);
diffuse = texture(u_texture2d, vec2(muv.x, 1.0-muv.y));
} else if(u_textured) {
diffuse = texture(u_texture2d, v_texcoord);
} else {
diffuse = u_diffuse;
}
fragcolor = diffuse * lit * shadowing();
#ifdef RIM
{vec3 n = normalize(mat3(M) * v_normal);
vec3 p = (M * vec4(v_position,1.0)).xyz;
vec3 v = normalize(-p);
float rim = 1.0 - max(dot(v, n), 0.0);
rim = smoothstep(1.0-0.01, 1.0, rim);
fragcolor += vec4(0.0, 0.0, rim, 1.0);}
#endif
}

View File

@ -0,0 +1,14 @@
uniform sampler2D fsDiffTex;
uniform sampler2D fsNormalTex;
uniform sampler2D fsPositionTex;
uniform mat4 MVP;
in vec3 v_normal;
in vec2 v_texcoord;
out vec4 fragColor;
void main() {
vec4 diff = texture(fsDiffTex, v_texcoord).rgba;
vec3 n = normalize(mat3(MVP) * v_normal);
fragColor = diff;
}

View File

@ -0,0 +1,8 @@
uniform samplerCube u_cubemap;
in vec3 v_direction;
out vec4 fragcolor;
void main() {
fragcolor = vec4(texture(u_cubemap, v_direction).rgb, 1.0);
}

View File

@ -0,0 +1,138 @@
uniform vec3 uSunPos = vec3( 0, 0.1, -1 );
in vec3 v_direction;
out vec4 fragcolor;
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);
void main() {
vec3 color = atmosphere(
normalize(v_direction),
vec3(0,6372e3,0),
uSunPos,
22.0,
6371e3,
6471e3,
vec3(5.5e-6, 13.0e-6, 22.4e-6),
21e-6,
8e3,
1.2e3,
0.758
);
color = 1.0 - exp(-1.0 * color);
fragcolor = vec4(color, 1);
}
#define PI 3.141592
#define iSteps 16
#define jSteps 8
vec2 rsi(vec3 r0, vec3 rd, float sr) {
float a = dot(rd, rd);
float b = 2.0 * dot(rd, r0);
float c = dot(r0, r0) - (sr * sr);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {
pSun = normalize(pSun);
r = normalize(r);
vec2 p = rsi(r0, r, rAtmos);
if (p.x > p.y) return vec3(0,0,0);
p.y = min(p.y, rsi(r0, r, rPlanet).x);
float iStepSize = (p.y - p.x) / float(iSteps);
float iTime = 0.0;
vec3 totalRlh = vec3(0,0,0);
vec3 totalMie = vec3(0,0,0);
float iOdRlh = 0.0;
float iOdMie = 0.0;
float mu = dot(r, pSun);
float mumu = mu * mu;
float gg = g * g;
float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
for (int i = 0; i < iSteps; i++) {
vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);
float iHeight = length(iPos) - rPlanet;
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
float odStepMie = exp(-iHeight / shMie) * iStepSize;
iOdRlh += odStepRlh;
iOdMie += odStepMie;
float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);
float jTime = 0.0;
float jOdRlh = 0.0;
float jOdMie = 0.0;
for (int j = 0; j < jSteps; j++) {
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
float jHeight = length(jPos) - rPlanet;
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
jOdMie += exp(-jHeight / shMie) * jStepSize;
jTime += jStepSize;
}
vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));
totalRlh += odStepRlh * attn;
totalMie += odStepMie * attn;
iTime += iStepSize;
}
return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);
}

View File

@ -0,0 +1,4 @@
void mainImage( out vec4 fragColor, in vec2 fragCoord );
void main() {
mainImage(fragColor, texcoord.xy * iResolution);
}

View File

@ -0,0 +1,6 @@
out vec2 texcoord;
void main() {
texcoord = vec2( (gl_VertexID << 1) & 2, gl_VertexID & 2 );
gl_Position = vec4( texCoord * 2.0 - 1.0, 0.0, 1.0 );
}

View File

@ -0,0 +1,8 @@
out vec2 uv;
void main() {
float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);
float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);
gl_Position = vec4(-1.0 + x*2.0, 0.0+(-1.0+y*2.0), 0.0, 1.0);
uv = vec2(x, y);
}

View File

@ -0,0 +1,8 @@
out vec2 uv;
void main() {
float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);
float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);
gl_Position = vec4(-1.0 + x*2.0, 0.0-(-1.0+y*2.0), 0.0, 1.0);
uv = vec2(x, y);
}

View File

@ -0,0 +1,114 @@
#ifndef MAX_BONES
#define MAX_BONES 110
#endif
uniform mat3x4 vsBoneMatrix[MAX_BONES];
uniform bool SKINNED = false;
uniform mat4 VP;
#if 0
for (size_t di = 0; di < mesh->blend_deformers.count; di++) {
ufbx_blend_deformer *deformer = mesh->blend_deformers.data[di];
for (size_t ci = 0; ci < deformer->channels.count; ci++) {
ufbx_blend_channel *chan = deformer->channels.data[ci];
if (chan->keyframes.count == 0) continue;
if (num_blend_shapes < MAX_BLEND_SHAPES) {
blend_channels[num_blend_shapes] = chan;
vmesh->blend_channel_indices[num_blend_shapes] = (int32_t)chan->typed_id;
num_blend_shapes++;
}
}
}
if (num_blend_shapes > 0) {
vmesh->blend_shape_image = pack_blend_channels_to_image(mesh, blend_channels, num_blend_shapes);
vmesh->num_blend_shapes = num_blend_shapes;
}
ubo.f_num_blend_shapes = (float)mesh->num_blend_shapes;
for (size_t i = 0; i < mesh->num_blend_shapes; i++) {
ubo.blend_weights[i] = view->scene.blend_channels[mesh->blend_channel_indices[i]].weight;
}
sg_image blend_shapes = mesh->num_blend_shapes > 0 ? mesh->blend_shape_image : view->empty_blend_shape_image;
#endif
#ifndef MAX_BLENDSHAPES
#define MAX_BLENDSHAPES 16
#endif
uniform vec4 blend_weights[MAX_BLENDSHAPES];
uniform float f_num_blend_shapes;
uniform sampler2DArray blend_shapes;
in vec3 att_position;
in vec2 att_texcoord;
in vec3 att_normal;
in vec4 att_tangent;
in mat4 att_instanced_matrix;
in vec4 att_indexes;
in vec4 att_weights;
in float att_vertexindex;
in vec4 att_color;
in vec3 att_bitangent;
out vec4 v_color;
out vec3 v_position;
out vec3 v_normal, v_normal_ws;
out vec2 v_texcoord;
uniform mat4 model, view;
uniform mat4 cameraToShadowProjector;
out vec4 vneye;
out vec4 vpeye;
out vec4 sc;
void do_shadow() {
vneye = view * model * vec4(att_normal, 0.0f);
vpeye = view * model * vec4(att_position, 1.0);
sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);
}
vec3 evaluate_blend_shape(int vertex_index) {
ivec2 coord = ivec2(vertex_index & (2048 - 1), vertex_index >> 11);
int num_blend_shapes = int(f_num_blend_shapes);
vec3 offset = vec3(0.0);
for (int i = 0; i < num_blend_shapes; i++) {
vec4 packedw = blend_weights[i >> 2];
float weight = packedw[i & 3];
offset += weight * texelFetch(blend_shapes, ivec3(coord, i), 0).xyz;
}
return offset;
}
void main() {
vec3 objPos;
if(!SKINNED) {
objPos = att_position;
v_normal = att_normal;
} else {
mat3x4 m = vsBoneMatrix[int(att_indexes.x)] * att_weights.x;
m += vsBoneMatrix[int(att_indexes.y)] * att_weights.y;
m += vsBoneMatrix[int(att_indexes.z)] * att_weights.z;
m += vsBoneMatrix[int(att_indexes.w)] * att_weights.w;
objPos = vec4(att_position, 1.0) * m;
v_normal = vec4(att_normal, 0.0) * m;
}
v_normal_ws = normalize(vec3(model * vec4(v_normal, 0.)));
v_normal = normalize(v_normal);
v_position = att_position;
v_texcoord = att_texcoord;
v_color = att_color;
gl_Position = VP * att_instanced_matrix * vec4( objPos, 1.0 );
do_shadow();
}

View File

@ -0,0 +1,13 @@
uniform mat4 u_mvp;
in vec3 att_Position;
in vec2 att_TexCoord;
in vec4 att_Color;
out vec2 vTexCoord;
out vec4 vColor;
void main() {
vColor = att_Color;
vTexCoord = att_TexCoord;
gl_Position = u_mvp * vec4(att_Position, 1.0);
}

View File

@ -0,0 +1,32 @@
uniform mat4 u_mvp;
in vec3 att_position;
in vec3 att_normal;
in vec2 att_texcoord;
in vec4 att_color;
out vec4 v_color;
out vec3 v_normal;
out vec3 v_normal_ws;
out vec2 v_texcoord;
uniform mat4 model, view, proj;
uniform mat4 cameraToShadowProjector;
out vec4 vneye;
out vec4 vpeye;
out vec4 sc;
void do_shadow() {
vneye = view * model * vec4(att_normal, 0.0f);
vpeye = view * model * vec4(att_position, 1.0);
sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);
}
void main() {
gl_Position = u_mvp * vec4(att_position, 1.0);
v_normal = normalize(att_normal);
v_normal_ws = normalize(vec3(model * vec4(att_normal, 0.)));
v_texcoord = att_texcoord;
v_color = att_color;
do_shadow();
}

View File

@ -0,0 +1,10 @@
uniform mat4 u_mvp;
in vec3 att_position;
out vec3 v_direction;
void main() {
vec4 position = u_mvp * vec4(att_position, 0.0);
gl_Position = position.xyww;
v_direction = att_position;
}

View File

@ -130,6 +130,8 @@
{{FILE:v4k_netsync.c}}
{{FILE:v4k_shaders.c}}
{{FILE:v4k_render.c}}
{{FILE:v4k_renderdd.c}}

View File

@ -50,723 +50,6 @@ void glCopyBackbufferToTexture( texture_t *tex ) { // unused
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, window_width(), window_height(), 0 );
}
// ----------------------------------------------------------------------------
// embedded shaders (@fixme: promote to files?)
static const char *const fs_0_0_shadowmap_lit =
// "#version 140 // inverse() requires v140\n"
"//" FILELINE "\n"
// "uniform mat4 view = mat4(1.0);\n"
"uniform vec3 lightPos = vec3(1.0);\n"
"uniform float doTexture = 1.;\n"
#if VSMCUBE
"uniform samplerCube shadowMap;\n" // VSMCUBE
#else
"uniform sampler2D shadowMap;\n" // !VSMCUBE
#endif
"struct light {\n"
" vec3 position; // world-space\n"
" vec4 diffuse;\n"
" vec4 specular;\n"
" float constantAttenuation, linearAttenuation, quadraticAttenuation;\n"
"};\n"
"light light0 = light(\n"
" lightPos,\n"
" vec4(1,1,1,1), // diffuse\n"
" vec4(1,1,1,1), // specular\n"
" 1.0, 0.0, 0.0 // attenuation (const, linear, quad)\n"
");\n"
"// From http://fabiensanglard.net/shadowmappingVSM/index.php\n"
#if VSMCUBE
"float chebyshevUpperBound(float distance, vec3 dir) {\n"
" distance = distance/20 ;\n"
" vec2 moments = texture(shadowMap, dir).rg;\n"
#else
"float chebyshevUpperBound(float distance, vec4 scPostW) {\n"
" vec2 moments = texture(shadowMap,scPostW.xy).rg;\n"
#endif
" // Surface is fully lit. as the current fragment is before the light occluder\n"
" if (distance <= moments.x)\n"
" return 1.0;\n"
" // The fragment is either in shadow or penumbra. We now use chebyshev's upperBound to check\n"
" // How likely this pixel is to be lit (p_max)\n"
" float variance = moments.y - (moments.x*moments.x);\n"
" //variance = max(variance, 0.000002);\n"
" variance = max(variance, 0.00002);\n"
" float d = distance - moments.x;\n"
" float p_max = variance / (variance + d*d);\n"
" return p_max;\n"
"}\n"
"vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 uv, in vec4 sc) {\n"
#ifndef VSMCUBE
" return vec4(1.);\n"
#else
" vec3 fragment = vec3(vpeye);\n"
" vec3 normal = vec3(normalize(vneye));\n"
" vec3 viewDir = normalize(-fragment);\n"
" // Lighting\n"
" // Convert to eye-space\n"
" vec3 light = vec3(view * vec4(light0.position, 1.0));\n"
#if VSMCUBE
" // Vectors\n"
" vec3 fragmentToLight = light - fragment;\n"
" vec3 fragmentToLightDir = normalize(fragmentToLight);\n"
" // Shadows\n"
" vec4 fragmentToLight_world = inverse(view) * vec4(fragmentToLightDir, 0.0);\n"
" float shadowFactor = chebyshevUpperBound(length(fragmentToLight), -fragmentToLight_world.xyz);\n"
#else
" // Shadows\n"
" vec4 scPostW = sc / sc.w;\n"
" scPostW = scPostW * 0.5 + 0.5;\n"
" float shadowFactor = 1.0; // Not in shadow\n"
" bool outsideShadowMap = sc.w <= 0.0f || (scPostW.x < 0 || scPostW.y < 0) || (scPostW.x >= 1 || scPostW.y >= 1);\n"
" if (!outsideShadowMap) {\n"
" shadowFactor = chebyshevUpperBound(scPostW.z, scPostW);\n"
" }\n"
#endif
" vec4 diffColor = vec4(1,1,1,1);\n"
#if VSMCUBE
" if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, -fragmentToLight_world.xyz).r), 1.0);\n"
#else
" if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, vec2(uv.x, 1.0 - uv.y)).r), 1.0);\n"
#endif
#if 1
" vec3 positionToLight = light - fragment;\n"
" vec3 lightDir = normalize(positionToLight);\n"
" // Angle between fragment-normal and incoming light\n"
" float cosAngIncidence = dot(lightDir, normal);\n"
" cosAngIncidence = clamp(cosAngIncidence, 0, 1);\n"
" float attenuation = 1.0f;\n"
" attenuation = 1.0 / (light0.constantAttenuation + light0.linearAttenuation * length(positionToLight) + light0.quadraticAttenuation * pow(length(positionToLight),2));\n"
" vec4 diffuse = diffColor * light0.diffuse * cosAngIncidence * attenuation;\n"
" vec4 total_lighting;\n"
" total_lighting += vec4(0.1, 0.1, 0.1, 1.0) * diffColor; // Ambient\n"
" total_lighting += diffuse * shadowFactor; // Diffuse\n"
#else
" vec4 total_lighting = diffColor;\n"
#endif
" return vec4(clamp(vec3(total_lighting), 0., 1.), 1.0);\n"
#endif
"}\n";
static const char *const fs_0_0_shadowmap_unlit = "//" FILELINE "\n"
// "uniform mat4 view = mat4(1.0);\n"
"uniform vec3 lightPos = vec3(1.0);\n"
"uniform float doTexture = 0.;\n"
"uniform sampler2D shadowMap;\n"
"vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 Texcoord, in vec4 sc) {\n"
" return vec4(1.);\n"
"};\n";
static const char *const vs_3_3_skybox = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"in vec3 att_position;\n"
"out vec3 v_direction;\n"
"void main() {\n"
" vec4 position = u_mvp * vec4(att_position, 0.0);\n"
" gl_Position = position.xyww;\n"
" v_direction = att_position;\n"
"}\n";
static const char *const fs_3_4_skybox = "//" FILELINE "\n"
"uniform samplerCube u_cubemap;\n"
"in vec3 v_direction;\n"
"out vec4 fragcolor;\n"
"void main() {\n"
" fragcolor = vec4(texture(u_cubemap, v_direction).rgb, 1.0);\n"
"}\n";
static const char *const fs_3_4_skybox_rayleigh = "//" FILELINE "\n"
"uniform vec3 uSunPos = vec3( 0, 0.1, -1 ); // = [0, Math.cos(theta) * 0.3 + 0.2, -1];\n"
"in vec3 v_direction;\n"
"out vec4 fragcolor;\n"
"vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);\n"
"void main() {\n"
" vec3 color = atmosphere(\n"
" normalize(v_direction), // normalized ray direction\n"
" vec3(0,6372e3,0), // ray origin\n"
" uSunPos, // position of the sun\n"
" 22.0, // intensity of the sun\n"
" 6371e3, // radius of the planet in meters\n"
" 6471e3, // radius of the atmosphere in meters\n"
" vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient\n"
" 21e-6, // Mie scattering coefficient\n"
" 8e3, // Rayleigh scale height\n"
" 1.2e3, // Mie scale height\n"
" 0.758 // Mie preferred scattering direction\n"
" );\n"
" // Apply exposure.\n"
" color = 1.0 - exp(-1.0 * color);\n"
" fragcolor = vec4(color, 1);\n"
"}\n"
"// [src] https://github.com/wwwtyro/glsl-atmosphere by wwwtyro (Unlicensed)\n"
"// For more information, please refer to <http://unlicense.org>\n"
"#define PI 3.141592\n"
"#define iSteps 16\n"
"#define jSteps 8\n"
"vec2 rsi(vec3 r0, vec3 rd, float sr) {\n"
" // ray-sphere intersection that assumes\n"
" // the sphere is centered at the origin.\n"
" // No intersection when result.x > result.y\n"
" float a = dot(rd, rd);\n"
" float b = 2.0 * dot(rd, r0);\n"
" float c = dot(r0, r0) - (sr * sr);\n"
" float d = (b*b) - 4.0*a*c;\n"
" if (d < 0.0) return vec2(1e5,-1e5);\n"
" return vec2(\n"
" (-b - sqrt(d))/(2.0*a),\n"
" (-b + sqrt(d))/(2.0*a)\n"
" );\n"
"}\n"
"vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {\n"
" // Normalize the sun and view directions.\n"
" pSun = normalize(pSun);\n"
" r = normalize(r);\n"
" // Calculate the step size of the primary ray.\n"
" vec2 p = rsi(r0, r, rAtmos);\n"
" if (p.x > p.y) return vec3(0,0,0);\n"
" p.y = min(p.y, rsi(r0, r, rPlanet).x);\n"
" float iStepSize = (p.y - p.x) / float(iSteps);\n"
" // Initialize the primary ray time.\n"
" float iTime = 0.0;\n"
" // Initialize accumulators for Rayleigh and Mie scattering.\n"
" vec3 totalRlh = vec3(0,0,0);\n"
" vec3 totalMie = vec3(0,0,0);\n"
" // Initialize optical depth accumulators for the primary ray.\n"
" float iOdRlh = 0.0;\n"
" float iOdMie = 0.0;\n"
" // Calculate the Rayleigh and Mie phases.\n"
" float mu = dot(r, pSun);\n"
" float mumu = mu * mu;\n"
" float gg = g * g;\n"
" float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);\n"
" float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));\n"
" // Sample the primary ray.\n"
" for (int i = 0; i < iSteps; i++) {\n"
" // Calculate the primary ray sample position.\n"
" vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);\n"
" // Calculate the height of the sample.\n"
" float iHeight = length(iPos) - rPlanet;\n"
" // Calculate the optical depth of the Rayleigh and Mie scattering for this step.\n"
" float odStepRlh = exp(-iHeight / shRlh) * iStepSize;\n"
" float odStepMie = exp(-iHeight / shMie) * iStepSize;\n"
" // Accumulate optical depth.\n"
" iOdRlh += odStepRlh;\n"
" iOdMie += odStepMie;\n"
" // Calculate the step size of the secondary ray.\n"
" float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);\n"
" // Initialize the secondary ray time.\n"
" float jTime = 0.0;\n"
" // Initialize optical depth accumulators for the secondary ray.\n"
" float jOdRlh = 0.0;\n"
" float jOdMie = 0.0;\n"
" // Sample the secondary ray.\n"
" for (int j = 0; j < jSteps; j++) {\n"
" // Calculate the secondary ray sample position.\n"
" vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);\n"
" // Calculate the height of the sample.\n"
" float jHeight = length(jPos) - rPlanet;\n"
" // Accumulate the optical depth.\n"
" jOdRlh += exp(-jHeight / shRlh) * jStepSize;\n"
" jOdMie += exp(-jHeight / shMie) * jStepSize;\n"
" // Increment the secondary ray time.\n"
" jTime += jStepSize;\n"
" }\n"
" // Calculate attenuation.\n"
" vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));\n"
" // Accumulate scattering.\n"
" totalRlh += odStepRlh * attn;\n"
" totalMie += odStepMie * attn;\n"
" // Increment the primary ray time.\n"
" iTime += iStepSize;\n"
" }\n"
" // Calculate and return the final color.\n"
" return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);\n"
"}\n";
static const char *const vs_332_32 = "//" FILELINE "\n"
//"uniform mat4 u_model, u_view, u_proj;\n"
"uniform mat4 u_mvp;\n"
"in vec3 att_position;\n"
"in vec3 att_normal;\n"
"in vec2 att_texcoord;\n"
"in vec4 att_color;\n"
"out vec4 v_color;\n"
"out vec3 v_normal;\n"
"out vec3 v_normal_ws;\n"
"out vec2 v_texcoord;\n"
// shadow
"uniform mat4 model, view, proj;\n"
"uniform mat4 cameraToShadowProjector;\n" // !VSMCUBE
"out vec4 vneye;\n"
"out vec4 vpeye;\n"
"out vec4 sc;\n" // !VSMCUBE
"void do_shadow() {\n"
" vneye = view * model * vec4(att_normal, 0.0f);\n"
" vpeye = view * model * vec4(att_position, 1.0);\n"
" sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);\n"
"}\n"
"void main() {\n"
//" gl_Position = proj * view * model * vec4(att_position, 1.0);\n"
" gl_Position = u_mvp * vec4(att_position, 1.0);\n"
" v_normal = normalize(att_normal);\n"
" v_normal_ws = normalize(vec3(model * vec4(att_normal, 0.)));\n" // normal world/model space
" v_texcoord = att_texcoord;\n"
" v_color = att_color;\n"
" do_shadow();\n"
"}";
static const char *const vs_0_2_fullscreen_quad_A = "//" FILELINE "\n"
"out vec2 texcoord;\n"
"void main() {\n"
" texcoord = vec2( (gl_VertexID << 1) & 2, gl_VertexID & 2 );\n"
" gl_Position = vec4( texCoord * 2.0 - 1.0, 0.0, 1.0 );\n"
"}\n";
static const char *const vs_0_2_fullscreen_quad_B = "//" FILELINE "\n"
"out vec2 uv;\n"
"void main() {\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u); \n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u); \n"
" gl_Position = vec4(-1.0 + x*2.0, 0.0+(-1.0+y*2.0), 0.0, 1.0);\n" // normal(0+),flipped(0-)
" uv = vec2(x, y);\n" // normal(y),flipped(1.0-y)
"}\n";
static const char *const vs_0_2_fullscreen_quad_B_flipped = "//" FILELINE "\n"
"out vec2 uv;\n"
"void main() {\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u); \n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u); \n"
" gl_Position = vec4(-1.0 + x*2.0, 0.0-(-1.0+y*2.0), 0.0, 1.0);\n" // normal(0+),flipped(0-)
" uv = vec2(x, y);\n" // normal(y),flipped(1.0-y)
"}\n";
/*
"out vec2 uv;\n"
"void main() {\n"
" float x = gl_VertexID / 2;\n"
" float y = gl_VertexID % 2;\n"
" uv = vec2(x, y);\n"
" gl_Position = vec4(2.0*uv - 1.0, 0.0, 1.0);\n"
"}\n";
*/
static const char *const fs_2_4_texel_inv_gamma = "//" FILELINE "\n"
"uniform sampler2D texture0; /*unit0*/\n"
"uniform float u_inv_gamma;\n"
"in vec2 uv;\n"
"out vec4 fragcolor;\n"
"void main() {\n"
" vec4 texel = texture( texture0, uv );\n"
" fragcolor = texel;\n"
" fragcolor.rgb = pow( fragcolor.rgb, vec3( u_inv_gamma ) );\n" // defaults: 1.0/2.2 gamma
"}\n";
// vertex stride = 4*(3+2+3+4+4+4+1+4+3) = 112 bytes + 16 bytes/instance
static const char *const vs_323444143_16_332_model = "//" FILELINE "\n"
"#ifndef MAX_BONES\n"
"#define MAX_BONES 110\n"
"#endif\n"
"uniform mat3x4 vsBoneMatrix[MAX_BONES];\n"
"uniform bool SKINNED = false;\n"
// "uniform mat4 M;\n" // RIM
"uniform mat4 VP;\n"
#if 0
// Fetch blend channels from all attached blend deformers.
for (size_t di = 0; di < mesh->blend_deformers.count; di++) {
ufbx_blend_deformer *deformer = mesh->blend_deformers.data[di];
for (size_t ci = 0; ci < deformer->channels.count; ci++) {
ufbx_blend_channel *chan = deformer->channels.data[ci];
if (chan->keyframes.count == 0) continue;
if (num_blend_shapes < MAX_BLEND_SHAPES) {
blend_channels[num_blend_shapes] = chan;
vmesh->blend_channel_indices[num_blend_shapes] = (int32_t)chan->typed_id;
num_blend_shapes++;
}
}
}
if (num_blend_shapes > 0) {
vmesh->blend_shape_image = pack_blend_channels_to_image(mesh, blend_channels, num_blend_shapes);
vmesh->num_blend_shapes = num_blend_shapes;
}
ubo.f_num_blend_shapes = (float)mesh->num_blend_shapes;
for (size_t i = 0; i < mesh->num_blend_shapes; i++) {
ubo.blend_weights[i] = view->scene.blend_channels[mesh->blend_channel_indices[i]].weight;
}
sg_image blend_shapes = mesh->num_blend_shapes > 0 ? mesh->blend_shape_image : view->empty_blend_shape_image;
#endif
// for blendshapes
"#ifndef MAX_BLENDSHAPES\n"
"#define MAX_BLENDSHAPES 16\n"
"#endif\n"
"uniform vec4 blend_weights[MAX_BLENDSHAPES];\n" // @todo: implement me
"uniform float f_num_blend_shapes;\n" // @todo: implement me
"uniform sampler2DArray blend_shapes;\n" // @todo: implement me
"in vec3 att_position;\n" // @todo: reorder ass2iqe to emit p3 n3 u2 t3 b3 c4B i4 w4 instead
"in vec2 att_texcoord;\n"
"in vec3 att_normal;\n"
"in vec4 att_tangent;\n" // vec3 + bi sign
"in mat4 att_instanced_matrix;\n" // for instanced rendering
"in vec4 att_indexes;\n" // @fixme: gles might use ivec4 instead?
"in vec4 att_weights;\n" // @todo: downgrade from float to byte
"in float att_vertexindex;\n" // for blendshapes
"in vec4 att_color;\n"
"in vec3 att_bitangent;\n" // @todo: remove? also, ass2iqe might output this
"out vec4 v_color;\n"
"out vec3 v_position;\n"
"out vec3 v_normal, v_normal_ws;\n"
"out vec2 v_texcoord;\n"
// shadow
"uniform mat4 model, view;\n"
"uniform mat4 cameraToShadowProjector;\n"
"out vec4 vneye;\n"
"out vec4 vpeye;\n"
"out vec4 sc;\n"
"void do_shadow() {\n"
" vneye = view * model * vec4(att_normal, 0.0f);\n"
" vpeye = view * model * vec4(att_position, 1.0);\n"
" sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);\n"
"}\n"
// blendshapes
"vec3 evaluate_blend_shape(int vertex_index) {\n"
" ivec2 coord = ivec2(vertex_index & (2048 - 1), vertex_index >> 11);\n"
" int num_blend_shapes = int(f_num_blend_shapes);\n"
" vec3 offset = vec3(0.0);\n"
" for (int i = 0; i < num_blend_shapes; i++) {\n"
" vec4 packedw = blend_weights[i >> 2];\n"
" float weight = packedw[i & 3];\n"
" offset += weight * texelFetch(blend_shapes, ivec3(coord, i), 0).xyz;\n"
" }\n"
" return offset;\n"
"}\n"
"void main() {\n"
" vec3 objPos;\n"
" if(!SKINNED) {\n"
" objPos = att_position;\n"
" v_normal = att_normal;\n"
" } else {\n"
" mat3x4 m = vsBoneMatrix[int(att_indexes.x)] * att_weights.x;\n"
" m += vsBoneMatrix[int(att_indexes.y)] * att_weights.y;\n"
" m += vsBoneMatrix[int(att_indexes.z)] * att_weights.z;\n"
" m += vsBoneMatrix[int(att_indexes.w)] * att_weights.w;\n"
" objPos = vec4(att_position, 1.0) * m;\n"
// blendshapes
// "objPos += evaluate_blend_shape(int(att_vertexindex));\n"
" v_normal = vec4(att_normal, 0.0) * m;\n"
" //@todo: tangents\n"
" }\n"
//" vec3 tangent = att_tangent.xyz;\n"
//" vec3 bitangent = cross(att_normal, att_tangent.xyz) * att_tangent.w;
" v_normal_ws = normalize(vec3(model * vec4(v_normal, 0.)));\n" // normal to world/model space
" v_normal = normalize(v_normal);\n"
" v_position = att_position;\n"
" v_texcoord = att_texcoord;\n"
" v_color = att_color;\n"
" gl_Position = VP * att_instanced_matrix * vec4( objPos, 1.0 );\n"
" do_shadow();\n"
"}\n";
#if 0
static const char *const fs_32_4_model_basic = "//" FILELINE "\n"
"uniform sampler2D fsDiffTex;\n"
"uniform sampler2D fsNormalTex;\n"
"uniform sampler2D fsPositionTex;\n"
"uniform mat4 MVP;\n"
"in vec3 v_normal;\n"
"in vec2 v_texcoord;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" vec4 diff = texture(fsDiffTex, v_texcoord).rgba;\n"
" vec3 n = normalize(mat3(MVP) * v_normal); // transform normal to eye space\n"
" fragColor = diff;// * vec4(v_normal.xyz, 1);\n"
"}\n";
#endif
static const char *const fs_32_4_model = "//" FILELINE "\n"
"uniform mat4 model, view;\n"
"uniform sampler2D u_texture2d;\n"
"uniform vec3 u_coefficients_sh[9];\n"
"uniform bool u_textured = true;\n"
"uniform bool u_lit = false;\n"
"uniform bool u_matcaps = false;\n"
"uniform vec4 u_diffuse = vec4(1.0,1.0,1.0,1.0);\n"
"#ifdef RIM\n"
"in vec3 v_position;\n"
"#endif\n"
"in vec3 v_normal, v_normal_ws;\n"
"in vec2 v_texcoord;\n"
"in vec4 v_color;\n"
"out vec4 fragcolor;\n"
"{{include-shadowmap}}\n"
"in vec4 vpeye;\n"
"in vec4 vneye;\n"
"in vec4 sc;\n"
"vec4 shadowing() {\n"
" return shadowmap(vpeye, vneye, v_texcoord, sc);\n"
"}\n"
"void main() {\n"
" vec3 n = /*normalize*/(v_normal);\n"
// SH lighting
" vec4 lit = vec4(1.0, 1.0, 1.0, 1.0);\n"
" vec3 SHLightResult[9];\n"
" SHLightResult[0] = 0.282095f * u_coefficients_sh[0];\n"
" SHLightResult[1] = -0.488603f * u_coefficients_sh[1] * n.y;\n"
" SHLightResult[2] = 0.488603f * u_coefficients_sh[2] * n.z;\n"
" SHLightResult[3] = -0.488603f * u_coefficients_sh[3] * n.x;\n"
" SHLightResult[4] = 1.092548f * u_coefficients_sh[4] * n.x * n.y;\n"
" SHLightResult[5] = -1.092548f * u_coefficients_sh[5] * n.y * n.z;\n"
" SHLightResult[6] = 0.315392f * u_coefficients_sh[6] * (3.0f * n.z * n.z - 1.0f);\n"
" SHLightResult[7] = -1.092548f * u_coefficients_sh[7] * n.x * n.z;\n"
" SHLightResult[8] = 0.546274f * u_coefficients_sh[8] * (n.x * n.x - n.y * n.y);\n"
" vec3 result = vec3(0.0);\n"
" for (int i = 0; i < 9; ++i)\n"
" result += SHLightResult[i];\n"
" if( (result.x*result.x+result.y*result.y+result.z*result.z) > 0.0 ) lit = vec4(result, 1.0);\n"
"\n"
// base
" vec4 diffuse;\n"
" if(u_matcaps) {\n"
" vec2 muv = vec2(view * vec4(v_normal_ws, 0))*0.5+vec2(0.5,0.5);\n" // normal (model space) to view space
" diffuse = texture(u_texture2d, vec2(muv.x, 1.0-muv.y));\n"
" } else if(u_textured) {\n"
" diffuse = texture(u_texture2d, v_texcoord);\n"
" } else {\n"
" diffuse = u_diffuse; // * v_color;\n"
" }\n"
// lighting mix
" fragcolor = diffuse * lit * shadowing();\n"
// rimlight
"#ifdef RIM\n"
" {vec3 n = normalize(mat3(M) * v_normal); // convert normal to view space\n"
" vec3 p = (M * vec4(v_position,1.0)).xyz; // convert position to view space\n"
" vec3 v = normalize(-p); // eye vector\n"
" float rim = 1.0 - max(dot(v, n), 0.0); // rimlight\n"
" rim = smoothstep(1.0-0.01, 1.0, rim); // intensity (0.01)\n"
" fragcolor += vec4(0.0, 0.0, rim, 1.0);} // blue\n"
"#endif\n"
"}\n";
static const char *const fs_2_4_texel_ycbr_gamma_saturation = "//" FILELINE "\n"
"uniform sampler2D u_texture_y; /*unit0*/\n"
"uniform sampler2D u_texture_cb; /*unit1*/\n"
"uniform sampler2D u_texture_cr; /*unit2*/\n"
"uniform float u_gamma;\n"
"in vec2 uv;\n"
"out vec4 fragcolor;\n"
"void main() {\n"
" float y = texture(u_texture_y, uv).r;\n"
" float cb = texture(u_texture_cb, uv).r;\n"
" float cr = texture(u_texture_cr, uv).r;\n"
" const mat4 to_rgb = mat4(\n"
" 1.0000, 1.0000, 1.0000, 0.0000,\n"
" 0.0000, -0.3441, 1.7720, 0.0000,\n"
" 1.4020, -0.7141, 0.0000, 0.0000,\n"
" -0.7010, 0.5291, -0.8860, 1.0000\n"
" );\n"
" vec4 texel = to_rgb * vec4(y, cb, cr, 1.0);\n"
/* same as:
" vec3 yCbCr = vec3(y,cb-0.5,cr-0.5);\n"
" vec4 texel = vec4( dot( vec3( 1.0, 0.0, 1.402 ), yCbCr ),\n"
" dot( vec3( 1.0 , -0.34414 , -0.71414 ), yCbCr ),\n"
" dot( vec3( 1.0, 1.772, 0.0 ), yCbCr ), 1.0);\n"
*/
" // gamma correction\n"
" texel.rgb = pow(texel.rgb, vec3(1.0 / u_gamma));\n"
" // saturation (algorithm from Chapter 16 of OpenGL Shading Language)\n"
" if(false) { float saturation = 2.0; const vec3 W = vec3(0.2125, 0.7154, 0.0721);\n"
" vec3 intensity = vec3(dot(texel.rgb, W));\n"
" texel.rgb = mix(intensity, texel.rgb, saturation); }\n"
" fragcolor = vec4(texel.rgb, 1.0);\n"
"}\n";
static const char *const vs_324_24_sprite = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"in vec3 att_Position;\n"
"in vec2 att_TexCoord;\n"
"in vec4 att_Color;\n"
"out vec2 vTexCoord;\n"
"out vec4 vColor;\n"
"void main() {\n"
" vColor = att_Color;\n"
" vTexCoord = att_TexCoord;\n"
" gl_Position = u_mvp * vec4(att_Position, 1.0);\n"
"}\n";
static const char *const fs_24_4_sprite = "//" FILELINE "\n"
"uniform sampler2D u_texture;\n"
"in vec2 vTexCoord;\n"
"in vec4 vColor;\n"
"out vec4 fragColor;\n"
"// [src] https://www.shadertoy.com/view/MllBWf CC1.0\n"
"vec4 texture_AA(sampler2D tx, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tx, 0));\n"
" uv = uv*res + 0.5;\n"
" // tweak fractionnal value of the texture coordinate\n"
" vec2 fl = floor(uv);\n"
" vec2 fr = fract(uv);\n"
" vec2 aa = fwidth(uv)*0.75;\n"
" fr = smoothstep( vec2(0.5)-aa, vec2(0.5)+aa, fr);\n"
" // return value\n"
" uv = (fl+fr-0.5) / res;\n"
" return texture(tx, uv);\n"
"}\n"
"// [src] https://www.shadertoy.com/view/MllBWf CC1.0\n"
"vec4 texture_AA2( sampler2D tex, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tex,0));\n"
" uv = uv*res;\n"
" vec2 seam = floor(uv+0.5);\n"
" uv = seam + clamp( (uv-seam)/fwidth(uv), -0.5, 0.5);\n"
" return texture(tex, uv/res);\n"
"}\n"
"// [src] https://www.shadertoy.com/view/ltBfRD\n"
"vec4 texture_AA3(sampler2D tex, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tex,0));\n"
" float width = 2.0;\n"
" uv = uv * res;\n"
" // ---\n"
" vec2 uv_floor = floor(uv + 0.5);\n"
" vec2 uv_fract = fract(uv + 0.5);\n"
" vec2 uv_aa = fwidth(uv) * width * 0.5;\n"
" uv_fract = smoothstep(\n"
" vec2(0.5) - uv_aa,\n"
" vec2(0.5) + uv_aa,\n"
" uv_fract\n"
" );\n"
" uv = (uv_floor + uv_fract - 0.5) / res;\n"
" return texture(tex, uv);\n"
"}\n"
"void main() {\n"
" vec4 texColor = texture_AA2(u_texture, vTexCoord);\n"
"if(texColor.a < 0.9) discard;"
" fragColor = vColor * texColor;\n"
"}\n";
static const char *const fs_2_4_preamble = "//" FILELINE "\n"
"#define texture2D texture\n"
"#define texture2DLod textureLod\n"
"#define FRAGCOLOR fragColor\n"
"#define texcoord uv\n"
"#define TEXCOORD uv\n"
"uniform sampler2D iChannel0;\n"
"uniform sampler2D iChannel1;\n"
"uniform float iWidth, iHeight, iTime, iFrame, iMousex, iMousey;\n"
"uniform float iChannelRes0x, iChannelRes0y;\n"
"uniform float iChannelRes1x, iChannelRes1y;\n"
"vec2 iResolution = vec2(iWidth, iHeight);\n"
"vec2 iMouse = vec2(iMousex, iMousey);\n"
"vec2 iChannelResolution[2] = vec2[2]( vec2(iChannelRes0x, iChannelRes0y),vec2(iChannelRes1x, iChannelRes1y) );\n"
"float iGlobalTime = iTime;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n";
static const char *const fs_main_shadertoy = "//" FILELINE "\n"
"void mainImage( out vec4 fragColor, in vec2 fragCoord );\n"
"void main() {\n"
" mainImage(fragColor, texcoord.xy * iResolution);\n"
"}\n";
// ----------------------------------------------------------------------------
// shaders

View File

@ -0,0 +1,687 @@
static const char *const fs_0_0_shadowmap_lit = "//" FILELINE "\n"
"uniform vec3 lightPos = vec3(1.0);\n"
"uniform float doTexture = 1.;\n"
#if VSMCUBE
"uniform samplerCube shadowMap;\n"
#else
"uniform sampler2D shadowMap;\n"
#endif
"\n"
"struct light {\n"
" vec3 position;\n"
" vec4 diffuse;\n"
" vec4 specular;\n"
" float constantAttenuation, linearAttenuation, quadraticAttenuation;\n"
" };\n"
" light light0 = light(\n"
" lightPos,\n"
" vec4(1,1,1,1),\n"
" vec4(1,1,1,1),\n"
" 1.0, 0.0, 0.0\n"
" );\n"
" \n"
" \n"
#if VSMCUBE
" float chebyshevUpperBound(float distance, vec3 dir) {\n"
" distance = distance/20 ;\n"
" vec2 moments = texture(shadowMap, dir).rg;\n"
#else
" float chebyshevUpperBound(float distance, vec4 scPostW) {\n"
" vec2 moments = texture(shadowMap,scPostW.xy).rg;\n"
#endif
" \n"
" if (distance <= moments.x)\n"
" return 1.0;\n"
" \n"
" \n"
" \n"
" float variance = moments.y - (moments.x*moments.x);\n"
" \n"
" variance = max(variance, 0.00002);\n"
" \n"
" float d = distance - moments.x;\n"
" float p_max = variance / (variance + d*d);\n"
" \n"
" return p_max;\n"
" }\n"
" \n"
" vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 uv, in vec4 sc) {\n"
#ifndef VSMCUBE
" return vec4(1.);\n"
#else
" \n"
" vec3 fragment = vec3(vpeye);\n"
" vec3 normal = vec3(normalize(vneye));\n"
" vec3 viewDir = normalize(-fragment);\n"
" \n"
" \n"
" \n"
" vec3 light = vec3(view * vec4(light0.position, 1.0));\n"
" \n"
#if VSMCUBE
" \n"
" vec3 fragmentToLight = light - fragment;\n"
" vec3 fragmentToLightDir = normalize(fragmentToLight);\n"
" \n"
" \n"
" vec4 fragmentToLight_world = inverse(view) * vec4(fragmentToLightDir, 0.0);\n"
" float shadowFactor = chebyshevUpperBound(length(fragmentToLight), -fragmentToLight_world.xyz);\n"
#else
" \n"
" vec4 scPostW = sc / sc.w;\n"
" scPostW = scPostW * 0.5 + 0.5;\n"
" \n"
" float shadowFactor = 1.0;\n"
" \n"
" bool outsideShadowMap = sc.w <= 0.0f || (scPostW.x < 0 || scPostW.y < 0) || (scPostW.x >= 1 || scPostW.y >= 1);\n"
" if (!outsideShadowMap) {\n"
" shadowFactor = chebyshevUpperBound(scPostW.z, scPostW);\n"
" }\n"
#endif
" \n"
" vec4 diffColor = vec4(1,1,1,1);\n"
#if VSMCUBE
" if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, -fragmentToLight_world.xyz).r), 1.0);\n"
#else
" if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, vec2(uv.x, 1.0 - uv.y)).r), 1.0);\n"
#endif
" \n"
#if 1
" vec3 positionToLight = light - fragment;\n"
" vec3 lightDir = normalize(positionToLight);\n"
" \n"
" \n"
" float cosAngIncidence = dot(lightDir, normal);\n"
" cosAngIncidence = clamp(cosAngIncidence, 0, 1);\n"
" \n"
" float attenuation = 1.0f;\n"
" attenuation = 1.0 / (light0.constantAttenuation + light0.linearAttenuation * length(positionToLight) + light0.quadraticAttenuation * pow(length(positionToLight),2));\n"
" \n"
" vec4 diffuse = diffColor * light0.diffuse * cosAngIncidence * attenuation;\n"
" \n"
" vec4 total_lighting;\n"
" total_lighting += vec4(0.1, 0.1, 0.1, 1.0) * diffColor;\n"
" total_lighting += diffuse * shadowFactor;\n"
#else
" vec4 total_lighting = diffColor;\n"
#endif
" return vec4(clamp(vec3(total_lighting), 0., 1.), 1.0);\n"
#endif
" }\n";
static const char *const fs_0_0_shadowmap_unlit = "//" FILELINE "\n"
"uniform vec3 lightPos = vec3(1.0);\n"
"uniform float doTexture = 0.;\n"
"uniform sampler2D shadowMap;\n"
"\n"
"vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 Texcoord, in vec4 sc) {\n"
" return vec4(1.);\n"
" };\n";
static const char *const fs_24_4_sprite = "//" FILELINE "\n"
"uniform sampler2D u_texture;\n"
"\n"
"in vec2 vTexCoord;\n"
"in vec4 vColor;\n"
"out vec4 fragColor;\n"
"\n"
"\n"
"vec4 texture_AA(sampler2D tx, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tx, 0));\n"
" uv = uv*res + 0.5;\n"
" \n"
" vec2 fl = floor(uv);\n"
" vec2 fr = fract(uv);\n"
" vec2 aa = fwidth(uv)*0.75;\n"
" fr = smoothstep( vec2(0.5)-aa, vec2(0.5)+aa, fr);\n"
" \n"
" uv = (fl+fr-0.5) / res;\n"
" return texture(tx, uv);\n"
"}\n"
"\n"
"\n"
"vec4 texture_AA2( sampler2D tex, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tex,0));\n"
" uv = uv*res;\n"
" vec2 seam = floor(uv+0.5);\n"
" uv = seam + clamp( (uv-seam)/fwidth(uv), -0.5, 0.5);\n"
" return texture(tex, uv/res);\n"
"}\n"
"\n"
"\n"
"vec4 texture_AA3(sampler2D tex, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tex,0));\n"
" float width = 2.0;\n"
" uv = uv * res;\n"
" \n"
" vec2 uv_floor = floor(uv + 0.5);\n"
" vec2 uv_fract = fract(uv + 0.5);\n"
" vec2 uv_aa = fwidth(uv) * width * 0.5;\n"
" uv_fract = smoothstep(\n"
" vec2(0.5) - uv_aa,\n"
" vec2(0.5) + uv_aa,\n"
" uv_fract\n"
" );\n"
" uv = (uv_floor + uv_fract - 0.5) / res;\n"
" return texture(tex, uv);\n"
"}\n"
"\n"
"void main() {\n"
" vec4 texColor = texture_AA2(u_texture, vTexCoord);\n"
" if(texColor.a < 0.9) discard;\n"
" fragColor = vColor * texColor;\n"
"}\n";
static const char *const fs_2_4_preamble = "//" FILELINE "\n"
"#define texture2D texture\n"
"#define texture2DLod textureLod\n"
"#define FRAGCOLOR fragColor\n"
"#define texcoord uv\n"
"#define TEXCOORD uv\n"
"uniform sampler2D iChannel0;\n"
"uniform sampler2D iChannel1;\n"
"uniform float iWidth, iHeight, iTime, iFrame, iMousex, iMousey;\n"
"uniform float iChannelRes0x, iChannelRes0y;\n"
"uniform float iChannelRes1x, iChannelRes1y;\n"
"vec2 iResolution = vec2(iWidth, iHeight);\n"
"vec2 iMouse = vec2(iMousex, iMousey);\n"
"vec2 iChannelResolution[2] = vec2[2]( vec2(iChannelRes0x, iChannelRes0y),vec2(iChannelRes1x, iChannelRes1y) );\n"
"float iGlobalTime = iTime;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n";
static const char *const fs_2_4_texel_inv_gamma = "//" FILELINE "\n"
"uniform sampler2D texture0;\n"
"uniform float u_inv_gamma;\n"
"\n"
"in vec2 uv;\n"
"out vec4 fragcolor;\n"
"\n"
"void main() {\n"
" vec4 texel = texture( texture0, uv );\n"
" fragcolor = texel;\n"
" fragcolor.rgb = pow( fragcolor.rgb, vec3( u_inv_gamma ) );\n"
"}\n";
static const char *const fs_2_4_texel_ycbr_gamma_saturation = "//" FILELINE "\n"
"uniform sampler2D u_texture_y;\n"
"uniform sampler2D u_texture_cb;\n"
"uniform sampler2D u_texture_cr;\n"
"uniform float u_gamma;\n"
"\n"
"in vec2 uv;\n"
"out vec4 fragcolor;\n"
"\n"
"void main() {\n"
" float y = texture(u_texture_y, uv).r;\n"
" float cb = texture(u_texture_cb, uv).r;\n"
" float cr = texture(u_texture_cr, uv).r;\n"
" \n"
" const mat4 to_rgb = mat4(\n"
" 1.0000, 1.0000, 1.0000, 0.0000,\n"
" 0.0000, -0.3441, 1.7720, 0.0000,\n"
" 1.4020, -0.7141, 0.0000, 0.0000,\n"
" -0.7010, 0.5291, -0.8860, 1.0000\n"
" );\n"
" vec4 texel = to_rgb * vec4(y, cb, cr, 1.0);\n"
" \n"
" \n"
" texel.rgb = pow(texel.rgb, vec3(1.0 / u_gamma));\n"
" \n"
" \n"
" if(false) { float saturation = 2.0; const vec3 W = vec3(0.2125, 0.7154, 0.0721);\n"
" vec3 intensity = vec3(dot(texel.rgb, W));\n"
"texel.rgb = mix(intensity, texel.rgb, saturation); }\n"
"\n"
"fragcolor = vec4(texel.rgb, 1.0);\n"
"}\n";
static const char *const fs_32_4_model = "//" FILELINE "\n"
"uniform mat4 model, view;\n"
"uniform sampler2D u_texture2d;\n"
"uniform vec3 u_coefficients_sh[9];\n"
"uniform bool u_textured = true;\n"
"uniform bool u_lit = false;\n"
"uniform bool u_matcaps = false;\n"
"uniform vec4 u_diffuse = vec4(1.0,1.0,1.0,1.0);\n"
"\n"
#ifdef RIM
"in vec3 v_position;\n"
#endif
"in vec3 v_normal, v_normal_ws;\n"
"in vec2 v_texcoord;\n"
"in vec4 v_color;\n"
"out vec4 fragcolor;\n"
"\n"
"{{include-shadowmap}}\n"
"in vec4 vpeye;\n"
"in vec4 vneye;\n"
"in vec4 sc;\n"
"vec4 shadowing() {\n"
"return shadowmap(vpeye, vneye, v_texcoord, sc);\n"
"}\n"
"\n"
"void main() {\n"
"vec3 n = (v_normal);\n"
"\n"
"\n"
"vec4 lit = vec4(1.0, 1.0, 1.0, 1.0);\n"
"vec3 SHLightResult[9];\n"
"SHLightResult[0] = 0.282095f * u_coefficients_sh[0];\n"
"SHLightResult[1] = -0.488603f * u_coefficients_sh[1] * n.y;\n"
"SHLightResult[2] = 0.488603f * u_coefficients_sh[2] * n.z;\n"
"SHLightResult[3] = -0.488603f * u_coefficients_sh[3] * n.x;\n"
"SHLightResult[4] = 1.092548f * u_coefficients_sh[4] * n.x * n.y;\n"
"SHLightResult[5] = -1.092548f * u_coefficients_sh[5] * n.y * n.z;\n"
"SHLightResult[6] = 0.315392f * u_coefficients_sh[6] * (3.0f * n.z * n.z - 1.0f);\n"
"SHLightResult[7] = -1.092548f * u_coefficients_sh[7] * n.x * n.z;\n"
"SHLightResult[8] = 0.546274f * u_coefficients_sh[8] * (n.x * n.x - n.y * n.y);\n"
"vec3 result = vec3(0.0);\n"
"for (int i = 0; i < 9; ++i)\n"
"result += SHLightResult[i];\n"
"if( (result.x*result.x+result.y*result.y+result.z*result.z) > 0.0 ) lit = vec4(result, 1.0);\n"
"\n"
"\n"
"\n"
"\n"
"vec4 diffuse;\n"
"if(u_matcaps) {\n"
" vec2 muv = vec2(view * vec4(v_normal_ws, 0))*0.5+vec2(0.5,0.5);\n"
" diffuse = texture(u_texture2d, vec2(muv.x, 1.0-muv.y));\n"
" } else if(u_textured) {\n"
" diffuse = texture(u_texture2d, v_texcoord);\n"
" } else {\n"
" diffuse = u_diffuse;\n"
" }\n"
" \n"
" \n"
" fragcolor = diffuse * lit * shadowing();\n"
" \n"
" \n"
#ifdef RIM
" {vec3 n = normalize(mat3(M) * v_normal);\n"
" vec3 p = (M * vec4(v_position,1.0)).xyz;\n"
" vec3 v = normalize(-p);\n"
" float rim = 1.0 - max(dot(v, n), 0.0);\n"
" rim = smoothstep(1.0-0.01, 1.0, rim);\n"
" fragcolor += vec4(0.0, 0.0, rim, 1.0);}\n"
#endif
" \n"
"}\n";
static const char *const fs_32_4_model_basic = "//" FILELINE "\n"
"uniform sampler2D fsDiffTex;\n"
"uniform sampler2D fsNormalTex;\n"
"uniform sampler2D fsPositionTex;\n"
"uniform mat4 MVP;\n"
"\n"
"in vec3 v_normal;\n"
"in vec2 v_texcoord;\n"
"out vec4 fragColor;\n"
"\n"
"void main() {\n"
" vec4 diff = texture(fsDiffTex, v_texcoord).rgba;\n"
" vec3 n = normalize(mat3(MVP) * v_normal);\n"
" fragColor = diff;\n"
"}\n";
static const char *const fs_3_4_skybox = "//" FILELINE "\n"
"uniform samplerCube u_cubemap;\n"
"\n"
"in vec3 v_direction;\n"
"out vec4 fragcolor;\n"
"\n"
"void main() {\n"
" fragcolor = vec4(texture(u_cubemap, v_direction).rgb, 1.0);\n"
"}\n";
static const char *const fs_3_4_skybox_rayleigh = "//" FILELINE "\n"
"uniform vec3 uSunPos = vec3( 0, 0.1, -1 );\n"
"\n"
"in vec3 v_direction;\n"
"out vec4 fragcolor;\n"
"\n"
"vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);\n"
"\n"
"void main() {\n"
" vec3 color = atmosphere(\n"
" normalize(v_direction),\n"
" vec3(0,6372e3,0),\n"
" uSunPos,\n"
" 22.0,\n"
" 6371e3,\n"
" 6471e3,\n"
" vec3(5.5e-6, 13.0e-6, 22.4e-6),\n"
" 21e-6,\n"
" 8e3,\n"
" 1.2e3,\n"
" 0.758\n"
" );\n"
" \n"
" \n"
" color = 1.0 - exp(-1.0 * color);\n"
" \n"
" fragcolor = vec4(color, 1);\n"
"}\n"
"\n"
"\n"
"\n"
"\n"
"#define PI 3.141592\n"
"#define iSteps 16\n"
"#define jSteps 8\n"
"\n"
"vec2 rsi(vec3 r0, vec3 rd, float sr) {\n"
" \n"
" \n"
" \n"
" float a = dot(rd, rd);\n"
" float b = 2.0 * dot(rd, r0);\n"
" float c = dot(r0, r0) - (sr * sr);\n"
" float d = (b*b) - 4.0*a*c;\n"
" if (d < 0.0) return vec2(1e5,-1e5);\n"
" return vec2(\n"
" (-b - sqrt(d))/(2.0*a),\n"
" (-b + sqrt(d))/(2.0*a)\n"
" );\n"
"}\n"
"\n"
"vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {\n"
" \n"
" pSun = normalize(pSun);\n"
" r = normalize(r);\n"
" \n"
" \n"
" vec2 p = rsi(r0, r, rAtmos);\n"
" if (p.x > p.y) return vec3(0,0,0);\n"
" p.y = min(p.y, rsi(r0, r, rPlanet).x);\n"
" float iStepSize = (p.y - p.x) / float(iSteps);\n"
" \n"
" \n"
" float iTime = 0.0;\n"
" \n"
" \n"
" vec3 totalRlh = vec3(0,0,0);\n"
" vec3 totalMie = vec3(0,0,0);\n"
" \n"
" \n"
" float iOdRlh = 0.0;\n"
" float iOdMie = 0.0;\n"
" \n"
" \n"
" float mu = dot(r, pSun);\n"
" float mumu = mu * mu;\n"
" float gg = g * g;\n"
" float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);\n"
" float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));\n"
" \n"
" \n"
" for (int i = 0; i < iSteps; i++) {\n"
" \n"
" \n"
" vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);\n"
" \n"
" \n"
" float iHeight = length(iPos) - rPlanet;\n"
" \n"
" \n"
" float odStepRlh = exp(-iHeight / shRlh) * iStepSize;\n"
" float odStepMie = exp(-iHeight / shMie) * iStepSize;\n"
" \n"
" \n"
" iOdRlh += odStepRlh;\n"
" iOdMie += odStepMie;\n"
" \n"
" \n"
" float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);\n"
" \n"
" \n"
" float jTime = 0.0;\n"
" \n"
" \n"
" float jOdRlh = 0.0;\n"
" float jOdMie = 0.0;\n"
" \n"
" \n"
" for (int j = 0; j < jSteps; j++) {\n"
" \n"
" \n"
" vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);\n"
" \n"
" \n"
" float jHeight = length(jPos) - rPlanet;\n"
" \n"
" \n"
" jOdRlh += exp(-jHeight / shRlh) * jStepSize;\n"
" jOdMie += exp(-jHeight / shMie) * jStepSize;\n"
" \n"
" \n"
" jTime += jStepSize;\n"
" }\n"
" \n"
" \n"
" vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));\n"
" \n"
" \n"
" totalRlh += odStepRlh * attn;\n"
" totalMie += odStepMie * attn;\n"
" \n"
" \n"
" iTime += iStepSize;\n"
" \n"
" }\n"
" \n"
" \n"
" return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);\n"
"}\n";
static const char *const fs_main_shadertoy = "//" FILELINE "\n"
"void mainImage( out vec4 fragColor, in vec2 fragCoord );\n"
"void main() {\n"
" mainImage(fragColor, texcoord.xy * iResolution);\n"
"}\n";
static const char *const vs_0_2_fullscreen_quad_A = "//" FILELINE "\n"
"out vec2 texcoord;\n"
"\n"
"void main() {\n"
" texcoord = vec2( (gl_VertexID << 1) & 2, gl_VertexID & 2 );\n"
" gl_Position = vec4( texCoord * 2.0 - 1.0, 0.0, 1.0 );\n"
"}\n";
static const char *const vs_0_2_fullscreen_quad_B = "//" FILELINE "\n"
"out vec2 uv;\n"
"\n"
"void main() {\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);\n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);\n"
" gl_Position = vec4(-1.0 + x*2.0, 0.0+(-1.0+y*2.0), 0.0, 1.0);\n"
" uv = vec2(x, y);\n"
"}\n";
static const char *const vs_0_2_fullscreen_quad_B_flipped = "//" FILELINE "\n"
"out vec2 uv;\n"
"\n"
"void main() {\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);\n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);\n"
" gl_Position = vec4(-1.0 + x*2.0, 0.0-(-1.0+y*2.0), 0.0, 1.0);\n"
" uv = vec2(x, y);\n"
"}\n";
static const char *const vs_323444143_16_332_model = "//" FILELINE "\n"
#ifndef MAX_BONES
"#define MAX_BONES 110\n"
#endif
"uniform mat3x4 vsBoneMatrix[MAX_BONES];\n"
"uniform bool SKINNED = false;\n"
"\n"
"uniform mat4 VP;\n"
"\n"
#if 0
"\n"
"for (size_t di = 0; di < mesh->blend_deformers.count; di++) {\n"
" ufbx_blend_deformer *deformer = mesh->blend_deformers.data[di];\n"
" for (size_t ci = 0; ci < deformer->channels.count; ci++) {\n"
" ufbx_blend_channel *chan = deformer->channels.data[ci];\n"
" if (chan->keyframes.count == 0) continue;\n"
" if (num_blend_shapes < MAX_BLEND_SHAPES) {\n"
" blend_channels[num_blend_shapes] = chan;\n"
" vmesh->blend_channel_indices[num_blend_shapes] = (int32_t)chan->typed_id;\n"
" num_blend_shapes++;\n"
" }\n"
" }\n"
"}\n"
"if (num_blend_shapes > 0) {\n"
" vmesh->blend_shape_image = pack_blend_channels_to_image(mesh, blend_channels, num_blend_shapes);\n"
" vmesh->num_blend_shapes = num_blend_shapes;\n"
"}\n"
"\n"
"ubo.f_num_blend_shapes = (float)mesh->num_blend_shapes;\n"
"for (size_t i = 0; i < mesh->num_blend_shapes; i++) {\n"
" ubo.blend_weights[i] = view->scene.blend_channels[mesh->blend_channel_indices[i]].weight;\n"
"}\n"
"\n"
"sg_image blend_shapes = mesh->num_blend_shapes > 0 ? mesh->blend_shape_image : view->empty_blend_shape_image;\n"
#endif
"\n"
"\n"
#ifndef MAX_BLENDSHAPES
"#define MAX_BLENDSHAPES 16\n"
#endif
"uniform vec4 blend_weights[MAX_BLENDSHAPES];\n"
"uniform float f_num_blend_shapes;\n"
"uniform sampler2DArray blend_shapes;\n"
"\n"
"in vec3 att_position;\n"
"in vec2 att_texcoord;\n"
"in vec3 att_normal;\n"
"in vec4 att_tangent;\n"
"in mat4 att_instanced_matrix;\n"
"in vec4 att_indexes;\n"
"in vec4 att_weights;\n"
"in float att_vertexindex;\n"
"in vec4 att_color;\n"
"in vec3 att_bitangent;\n"
"out vec4 v_color;\n"
"out vec3 v_position;\n"
"out vec3 v_normal, v_normal_ws;\n"
"out vec2 v_texcoord;\n"
"\n"
"\n"
"\n"
"uniform mat4 model, view;\n"
"uniform mat4 cameraToShadowProjector;\n"
"out vec4 vneye;\n"
"out vec4 vpeye;\n"
"out vec4 sc;\n"
"void do_shadow() {\n"
" vneye = view * model * vec4(att_normal, 0.0f);\n"
" vpeye = view * model * vec4(att_position, 1.0);\n"
" sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);\n"
"}\n"
"\n"
"\n"
"vec3 evaluate_blend_shape(int vertex_index) {\n"
" ivec2 coord = ivec2(vertex_index & (2048 - 1), vertex_index >> 11);\n"
" int num_blend_shapes = int(f_num_blend_shapes);\n"
" vec3 offset = vec3(0.0);\n"
" for (int i = 0; i < num_blend_shapes; i++) {\n"
" vec4 packedw = blend_weights[i >> 2];\n"
" float weight = packedw[i & 3];\n"
" offset += weight * texelFetch(blend_shapes, ivec3(coord, i), 0).xyz;\n"
" }\n"
" return offset;\n"
"}\n"
"\n"
"void main() {\n"
" vec3 objPos;\n"
" if(!SKINNED) {\n"
" objPos = att_position;\n"
" v_normal = att_normal;\n"
" } else {\n"
" mat3x4 m = vsBoneMatrix[int(att_indexes.x)] * att_weights.x;\n"
" m += vsBoneMatrix[int(att_indexes.y)] * att_weights.y;\n"
" m += vsBoneMatrix[int(att_indexes.z)] * att_weights.z;\n"
" m += vsBoneMatrix[int(att_indexes.w)] * att_weights.w;\n"
" objPos = vec4(att_position, 1.0) * m;\n"
" \n"
" \n"
" \n"
" \n"
" v_normal = vec4(att_normal, 0.0) * m;\n"
" \n"
" }\n"
" \n"
" \n"
" \n"
" \n"
" v_normal_ws = normalize(vec3(model * vec4(v_normal, 0.)));\n"
" v_normal = normalize(v_normal);\n"
" v_position = att_position;\n"
" v_texcoord = att_texcoord;\n"
" v_color = att_color;\n"
" gl_Position = VP * att_instanced_matrix * vec4( objPos, 1.0 );\n"
" do_shadow();\n"
" }\n";
static const char *const vs_324_24_sprite = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"\n"
"in vec3 att_Position;\n"
"in vec2 att_TexCoord;\n"
"in vec4 att_Color;\n"
"out vec2 vTexCoord;\n"
"out vec4 vColor;\n"
"\n"
"void main() {\n"
" vColor = att_Color;\n"
" vTexCoord = att_TexCoord;\n"
" gl_Position = u_mvp * vec4(att_Position, 1.0);\n"
"}\n";
static const char *const vs_332_32 = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"\n"
"in vec3 att_position;\n"
"in vec3 att_normal;\n"
"in vec2 att_texcoord;\n"
"in vec4 att_color;\n"
"out vec4 v_color;\n"
"out vec3 v_normal;\n"
"out vec3 v_normal_ws;\n"
"out vec2 v_texcoord;\n"
"\n"
"\n"
"uniform mat4 model, view, proj;\n"
"uniform mat4 cameraToShadowProjector;\n"
"out vec4 vneye;\n"
"out vec4 vpeye;\n"
"out vec4 sc;\n"
"void do_shadow() {\n"
" vneye = view * model * vec4(att_normal, 0.0f);\n"
" vpeye = view * model * vec4(att_position, 1.0);\n"
" sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);\n"
"}\n"
"\n"
"void main() {\n"
" \n"
" gl_Position = u_mvp * vec4(att_position, 1.0);\n"
" v_normal = normalize(att_normal);\n"
" v_normal_ws = normalize(vec3(model * vec4(att_normal, 0.)));\n"
" v_texcoord = att_texcoord;\n"
" v_color = att_color;\n"
" do_shadow();\n"
"}\n";
static const char *const vs_3_3_skybox = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"\n"
"in vec3 att_position;\n"
"out vec3 v_direction;\n"
"\n"
"void main() {\n"
" vec4 position = u_mvp * vec4(att_position, 0.0);\n"
" gl_Position = position.xyww;\n"
" v_direction = att_position;\n"
"}\n";

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
#!/bin/bash 2>nul
python tools/glsl_join.py --output ./engine/split/v4k_shaders.c

41
tools/glsl_join.py 100644
View File

@ -0,0 +1,41 @@
import argparse
import os
def glsl_to_c(output_filename: str):
glsl_directory = "engine/shaders/"
all_shaders = []
for filename in os.listdir(glsl_directory):
if filename.endswith(".glsl"):
with open(os.path.join(glsl_directory, filename), 'r') as f:
shader_code = f.read()
shader_code_escaped = ""
lines = shader_code.split('\n')
for idx, line in enumerate(lines):
if any(keyword in line for keyword in ["#if", "#ifdef", "#else", "#endif"]):
shader_code_escaped += line
else:
shader_code_escaped += f'"{line}\\n"'
# Append semicolon if it's the last line
if idx == len(lines) - 1:
shader_code_escaped += ";"
shader_code_escaped += "\n"
variable_name = os.path.splitext(filename)[0] # Remove the .glsl extension
c_shader_code = f'static const char *const {variable_name} = \"//\" FILELINE \"\\n\"\n{shader_code_escaped}'
all_shaders.append(c_shader_code)
# Write all shaders to the C file
with open(output_filename, 'w') as f:
f.write('\n'.join(all_shaders))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--output", required=True, help="Output filename for the C file")
args = parser.parse_args()
glsl_to_c(args.output)
if __name__ == "__main__":
main()

View File

@ -0,0 +1,3 @@
#!/bin/bash 2>nul
python tools/glsl_split.py --input ./engine/split/v4k_shaders.c

View File

@ -0,0 +1,71 @@
import argparse
import os
import re
def reindent_code(shader_code: str) -> str:
indented_code = []
indentation_level = 0
tab_size = 4 # You can change this to set the desired tab size
for line in shader_code.split('\n'):
line = line.strip()
if line.endswith('}'):
indentation_level -= 1
indented_code.append(' ' * (tab_size * indentation_level) + line)
if line.endswith('{'):
indentation_level += 1
return '\n'.join(indented_code)
def c_to_glsl(input_filename: str):
# Ensure the output directory exists
if not os.path.exists("engine/shaders"):
os.makedirs("engine/shaders")
# Read the C code from the file
with open(input_filename, 'r') as f:
lines = f.readlines()
capturing = False
shader_code = ""
output_filename = None
for line in lines:
if not capturing:
match = re.search(r'static const char \*const ([a-zA-Z0-9_]+) =', line)
if match:
output_filename = f"engine/shaders/{match.group(1)}.glsl"
capturing = True
continue # skip the current line since it's just the declaration
if capturing:
if line.strip().endswith('";'):
shader_code += line.rstrip('";\n')
# Process the captured shader code
shader_code = re.sub(r'/\*.*?\*/', '', shader_code, flags=re.DOTALL) # Remove C-style comments
shader_code = re.sub(r'//.*', '', shader_code) # Remove C++ style comments
shader_code = shader_code.replace(r'"\n"', '\n').replace(r'\n', '').replace(r'"', '')
# Save to a .glsl file
with open(output_filename, 'w') as f:
shader_code = shader_code.strip()
f.write(reindent_code(shader_code.strip()))
# Reset for the next shader code
capturing = False
shader_code = ""
output_filename = None
else:
shader_code += line
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--input", required=True)
args = parser.parse_args()
c_to_glsl(args.input)
if __name__ == "__main__":
main()