GLSL include support

main
Dominik Madarász 2024-08-24 15:04:33 +02:00
parent 851fc37ddf
commit a4cf90838a
7 changed files with 238 additions and 171 deletions

1
.gitignore vendored
View File

@ -29,6 +29,7 @@ v4k.code-workspace
tests/out tests/out
tests/diff tests/diff
mtb.ini mtb.ini
*.blend1
_fwk _fwk
.env* .env*
.*.bat .*.bat

View File

@ -0,0 +1,135 @@
uniform int u_num_lights;
struct light_t {
int type;
vec3 diffuse;
vec3 specular;
vec3 ambient;
vec3 pos;
vec3 dir;
float power;
float radius;
float innerCone;
float outerCone;
// falloff
float constant;
float linear;
float quadratic;
};
#define MAX_LIGHTS 16
const int LIGHT_DIRECTIONAL = 0;
const int LIGHT_POINT = 1;
const int LIGHT_SPOT = 2;
uniform light_t u_lights[MAX_LIGHTS];
#ifdef SHADING_PHONG
vec3 shading_phong(light_t l) {
vec3 lightDir;
float attenuation = 1.0;
if (l.type == LIGHT_DIRECTIONAL) {
lightDir = normalize(-l.dir);
} else if (l.type == LIGHT_POINT || l.type == LIGHT_SPOT) {
vec3 toLight = l.pos - v_position_ws;
lightDir = normalize(toLight);
float distance = length(toLight);
/* fast-reject based on radius */
if (l.radius != 0.0 && distance > l.radius) {
return vec3(0,0,0);
}
attenuation = 1.0 / (l.constant + l.linear * distance + l.quadratic * (distance * distance));
if (l.type == LIGHT_SPOT) {
float angle = dot(l.dir, -lightDir);
if (angle > l.outerCone) {
float intensity = (angle-l.outerCone)/(l.innerCone-l.outerCone);
attenuation *= clamp(intensity, 0.0, 1.0);
} else {
attenuation = 0.0;
}
}
}
// fast-rejection for faraway vertices
if (attenuation <= 0.01) {
return vec3(0,0,0);
}
vec3 n = normalize(v_normal_ws);
float diffuse = max(dot(n, lightDir), 0.0);
vec3 halfVec = normalize(lightDir + u_cam_dir);
float specular = pow(max(dot(n, halfVec), 0.0), l.power);
return (attenuation*l.ambient + diffuse*attenuation*l.diffuse + specular*attenuation*l.specular);
}
#endif
#ifdef SHADING_VERTEXLIT
vec3 shading_vertexlit(light_t l) {
vec3 lightDir;
float attenuation = 1.0;
if (l.type == LIGHT_DIRECTIONAL) {
lightDir = normalize(-l.dir);
} else if (l.type == LIGHT_POINT || l.type == LIGHT_SPOT) {
vec3 toLight = l.pos - v_position_ws;
lightDir = normalize(toLight);
float distance = length(toLight);
/* fast-reject based on radius */
if (l.radius != 0.0 && distance > l.radius) {
return vec3(0,0,0);
}
attenuation = 1.0 / (l.constant + l.linear * distance + l.quadratic * (distance * distance));
if (l.type == LIGHT_SPOT) {
float angle = dot(l.dir, -lightDir);
if (angle > l.outerCone) {
float intensity = (angle-l.outerCone)/(l.innerCone-l.outerCone);
attenuation *= clamp(intensity, 0.0, 1.0);
} else {
attenuation = 0.0;
}
}
}
// fast-rejection for faraway vertices
if (attenuation <= 0.01) {
return vec3(0,0,0);
}
vec3 n = normalize(v_normal_ws);
float diffuse = max(dot(n, lightDir), 0.0);
vec3 halfVec = normalize(lightDir + u_cam_dir);
float specular = pow(max(dot(n, halfVec), 0.0), l.power);
return (attenuation*l.ambient + diffuse*attenuation*l.diffuse + specular*attenuation*l.specular);
}
#endif
vec3 lighting() {
vec3 lit = vec3(0,0,0);
#ifndef SHADING_NONE
#ifdef SHADING_PHONG
for (int i=0; i<u_num_lights; i++) {
lit += shading_phong(u_lights[i]);
}
#endif
#ifdef SHADING_VERTEXLIT
for (int i=0; i<u_num_lights; i++) {
lit += shading_vertexlit(u_lights[i]);
}
#endif
#ifdef SHADING_PBR
#endif
#endif
return lit;
}

View File

@ -44,77 +44,7 @@ vec4 shadowing() {
uniform vec3 u_cam_pos; uniform vec3 u_cam_pos;
uniform vec3 u_cam_dir; uniform vec3 u_cam_dir;
uniform int u_num_lights; #include "light.glsl"
struct light_t {
int type;
vec3 diffuse;
vec3 specular;
vec3 ambient;
vec3 pos;
vec3 dir;
float power;
float radius;
float innerCone;
float outerCone;
// falloff
float constant;
float linear;
float quadratic;
};
#define MAX_LIGHTS 16
const int LIGHT_DIRECTIONAL = 0;
const int LIGHT_POINT = 1;
const int LIGHT_SPOT = 2;
uniform light_t u_lights[MAX_LIGHTS];
#ifdef SHADING_PHONG
vec3 shading_phong(light_t l) {
vec3 lightDir;
float attenuation = 1.0;
if (l.type == LIGHT_DIRECTIONAL) {
lightDir = normalize(-l.dir);
} else if (l.type == LIGHT_POINT || l.type == LIGHT_SPOT) {
vec3 toLight = l.pos - v_position_ws;
lightDir = normalize(toLight);
float distance = length(toLight);
/* fast-reject based on radius */
if (l.radius != 0.0 && distance > l.radius) {
return vec3(0,0,0);
}
attenuation = 1.0 / (l.constant + l.linear * distance + l.quadratic * (distance * distance));
if (l.type == LIGHT_SPOT) {
float angle = dot(l.dir, -lightDir);
if (angle > l.outerCone) {
float intensity = (angle-l.outerCone)/(l.innerCone-l.outerCone);
attenuation *= clamp(intensity, 0.0, 1.0);
} else {
attenuation = 0.0;
}
}
}
// fast-rejection for faraway vertices
if (attenuation <= 0.01) {
return vec3(0,0,0);
}
vec3 n = normalize(v_normal_ws);
float diffuse = max(dot(n, lightDir), 0.0);
vec3 halfVec = normalize(lightDir + u_cam_dir);
float specular = pow(max(dot(n, halfVec), 0.0), l.power);
return (attenuation*l.ambient + diffuse*attenuation*l.diffuse + specular*attenuation*l.specular);
}
#endif
#ifdef SHADING_PBR #ifdef SHADING_PBR
uniform vec2 resolution; /// set:640,480 // debug options below use this (USE_MAP_DEBUGGING, USE_AMBIENT_DEBUGGING) uniform vec2 resolution; /// set:640,480 // debug options below use this (USE_MAP_DEBUGGING, USE_AMBIENT_DEBUGGING)
@ -349,20 +279,6 @@ vec3 specular_ibl( vec3 V, vec3 N, float roughness, vec3 fresnel )
} }
#endif #endif
vec3 lighting() {
vec3 lit = vec3(0,0,0);
#ifndef SHADING_NONE
for (int i=0; i<u_num_lights; i++) {
#ifdef SHADING_PHONG
lit += shading_phong(u_lights[i]);
#endif
#ifdef SHADING_PBR
#endif
}
#endif
return lit;
}
vec3 sh_lighting(vec3 n) { vec3 sh_lighting(vec3 n) {
vec3 SHLightResult[9]; vec3 SHLightResult[9];
SHLightResult[0] = 0.282095f * u_coefficients_sh[0]; SHLightResult[0] = 0.282095f * u_coefficients_sh[0];

View File

@ -6,6 +6,7 @@ uniform bool SKINNED; /// set:0
uniform mat4 M; // RIM uniform mat4 M; // RIM
uniform mat4 VP; uniform mat4 VP;
uniform mat4 P; uniform mat4 P;
uniform vec3 u_cam_dir;
uniform int u_billboard; uniform int u_billboard;
#if 0 #if 0
@ -68,89 +69,7 @@ out vec3 v_to_camera;
out vec3 v_vertcolor; out vec3 v_vertcolor;
// lights // lights
#ifdef SHADING_VERTEXLIT #include "light.glsl"
uniform int u_num_lights;
struct light_t {
int type;
vec3 diffuse;
vec3 specular;
vec3 ambient;
vec3 pos;
vec3 dir;
float radius;
float power;
float innerCone;
float outerCone;
// falloff
float constant;
float linear;
float quadratic;
};
#define MAX_LIGHTS 16
const int LIGHT_DIRECTIONAL = 0;
const int LIGHT_POINT = 1;
const int LIGHT_SPOT = 2;
uniform light_t u_lights[MAX_LIGHTS];
uniform vec3 u_cam_dir;
vec3 shading_vertexlit(light_t l) {
vec3 lightDir;
float attenuation = 1.0;
if (l.type == LIGHT_DIRECTIONAL) {
lightDir = normalize(-l.dir);
} else if (l.type == LIGHT_POINT || l.type == LIGHT_SPOT) {
vec3 toLight = l.pos - v_position_ws;
lightDir = normalize(toLight);
float distance = length(toLight);
/* fast-reject based on radius */
if (l.radius != 0.0 && distance > l.radius) {
return vec3(0,0,0);
}
attenuation = 1.0 / (l.constant + l.linear * distance + l.quadratic * (distance * distance));
if (l.type == LIGHT_SPOT) {
float angle = dot(l.dir, -lightDir);
if (angle > l.outerCone) {
float intensity = (angle-l.outerCone)/(l.innerCone-l.outerCone);
attenuation *= clamp(intensity, 0.0, 1.0);
} else {
attenuation = 0.0;
}
}
}
// fast-rejection for faraway vertices
if (attenuation <= 0.01) {
return vec3(0,0,0);
}
vec3 n = normalize(v_normal_ws);
float diffuse = max(dot(n, lightDir), 0.0);
vec3 halfVec = normalize(lightDir + u_cam_dir);
float specular = pow(max(dot(n, halfVec), 0.0), l.power);
return (attenuation*l.ambient + diffuse*attenuation*l.diffuse + specular*attenuation*l.specular);
}
#endif
vec3 lighting() {
vec3 lit = vec3(0,0,0);
#ifdef SHADING_VERTEXLIT
for (int i=0; i<u_num_lights; i++) {
lit += shading_vertexlit(u_lights[i]);
}
#endif
return lit;
}
// shadow // shadow
uniform mat4 model, view, inv_view; uniform mat4 model, view, inv_view;

View File

@ -381926,6 +381926,36 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
return shader_geom(NULL, vs, fs, attribs, fragcolor, defines); return shader_geom(NULL, vs, fs, attribs, fragcolor, defines);
} }
static inline
char *shader_process_includes(const char *src) {
if (!src) return NULL;
char *includes = NULL;
for each_substring(src, "\n", line) {
if (line[0] == '#' && strstri(line, "#include")) {
const char *start = strstri(line, "\"");
const char *end = strstri(start+1, "\"");
if (start && end) {
char *filename = va("%.*s", (int)(end-start-1), start+1);
char *included = vfs_read(filename);
if (included) {
char *nested_includes = shader_process_includes(included);
includes = strcatf(&includes, "%s\n", nested_includes ? nested_includes : ""); //@leak
} else {
PANIC("!ERROR: shader(): Include file not found: %s\n", filename);
}
} else {
PANIC("!ERROR: shader(): Invalid #include directive: %s\n", line);
}
} else
{
includes = strcatf(&includes, "\n%s", line); //@leak
}
}
return includes;
}
static inline static inline
char *shader_preprocess(const char *src, const char *defines) { char *shader_preprocess(const char *src, const char *defines) {
if (!src) return NULL; if (!src) return NULL;
@ -381948,7 +381978,9 @@ char *shader_preprocess(const char *src, const char *defines) {
#endif #endif
} }
return va("%s\n%s\n%s", glsl_version, defines ? defines : "", src); char *processed_src = shader_process_includes(src);
return va("%s\n%s\n%s", glsl_version, defines ? defines : "", processed_src);
} }
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) { unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) {

View File

@ -270,6 +270,36 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
return shader_geom(NULL, vs, fs, attribs, fragcolor, defines); return shader_geom(NULL, vs, fs, attribs, fragcolor, defines);
} }
static inline
char *shader_process_includes(const char *src) {
if (!src) return NULL;
char *includes = NULL;
for each_substring(src, "\n", line) {
if (line[0] == '#' && strstri(line, "#include")) {
const char *start = strstri(line, "\"");
const char *end = strstri(start+1, "\"");
if (start && end) {
char *filename = va("%.*s", (int)(end-start-1), start+1);
char *included = vfs_read(filename);
if (included) {
char *nested_includes = shader_process_includes(included);
includes = strcatf(&includes, "%s\n", nested_includes ? nested_includes : ""); //@leak
} else {
PANIC("!ERROR: shader(): Include file not found: %s\n", filename);
}
} else {
PANIC("!ERROR: shader(): Invalid #include directive: %s\n", line);
}
} else
{
includes = strcatf(&includes, "\n%s", line); //@leak
}
}
return includes;
}
static inline static inline
char *shader_preprocess(const char *src, const char *defines) { char *shader_preprocess(const char *src, const char *defines) {
if (!src) return NULL; if (!src) return NULL;
@ -292,7 +322,9 @@ char *shader_preprocess(const char *src, const char *defines) {
#endif #endif
} }
return va("%s\n%s\n%s", glsl_version, defines ? defines : "", src); char *processed_src = shader_process_includes(src);
return va("%s\n%s\n%s", glsl_version, defines ? defines : "", processed_src);
} }
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) { unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) {

View File

@ -17069,6 +17069,36 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char
return shader_geom(NULL, vs, fs, attribs, fragcolor, defines); return shader_geom(NULL, vs, fs, attribs, fragcolor, defines);
} }
static inline
char *shader_process_includes(const char *src) {
if (!src) return NULL;
char *includes = NULL;
for each_substring(src, "\n", line) {
if (line[0] == '#' && strstri(line, "#include")) {
const char *start = strstri(line, "\"");
const char *end = strstri(start+1, "\"");
if (start && end) {
char *filename = va("%.*s", (int)(end-start-1), start+1);
char *included = vfs_read(filename);
if (included) {
char *nested_includes = shader_process_includes(included);
includes = strcatf(&includes, "%s\n", nested_includes ? nested_includes : ""); //@leak
} else {
PANIC("!ERROR: shader(): Include file not found: %s\n", filename);
}
} else {
PANIC("!ERROR: shader(): Invalid #include directive: %s\n", line);
}
} else
{
includes = strcatf(&includes, "\n%s", line); //@leak
}
}
return includes;
}
static inline static inline
char *shader_preprocess(const char *src, const char *defines) { char *shader_preprocess(const char *src, const char *defines) {
if (!src) return NULL; if (!src) return NULL;
@ -17091,7 +17121,9 @@ char *shader_preprocess(const char *src, const char *defines) {
#endif #endif
} }
return va("%s\n%s\n%s", glsl_version, defines ? defines : "", src); char *processed_src = shader_process_includes(src);
return va("%s\n%s\n%s", glsl_version, defines ? defines : "", processed_src);
} }
unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) { unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) {