Misc renderer improvements:

- Use VK_KHR_multiview
- Compute bounding boxes, use them to sort primitives
- Add shader reflection
- Add debug drawer
This commit is contained in:
Guillaume Meunier
2025-10-25 10:25:53 +02:00
parent 17ac7ab7d9
commit 7963efc034
23 changed files with 1679 additions and 615 deletions
+105
View File
@@ -0,0 +1,105 @@
// kate: hl glsl
const int nb_views = 2;
const int nb_clipping = 4;
const int nb_texcoords = 2;
const bool use_pbr = false;
const bool use_normal_maps = false;
layout(set = 0, binding = 0) uniform scene_ubo
{
mat4 view[nb_views];
vec4 light_position;
vec4 ambient_color;
vec4 light_color;
} scene;
layout(set = 0, binding = 1) uniform mesh_ubo
{
mat4 model;
mat4 modelview[nb_views];
mat4 modelviewproj[nb_views];
vec4 clipping_plane[nb_clipping];
} mesh;
layout(set = 0, binding = 2) uniform joints_ubo
{
mat4 joint_matrices[32];
} joints;
layout(set = 0, binding = 3) uniform sampler2D base_color_texture;
layout(set = 0, binding = 4) uniform sampler2D metallic_roughness_texture;
layout(set = 0, binding = 5) uniform sampler2D occlusion_texture;
layout(set = 0, binding = 6) uniform sampler2D emissive_texture;
layout(set = 0, binding = 7) uniform sampler2D normal_map_texture;
layout(set = 0, binding = 8) uniform material_ubo
{
vec4 base_color_factor;
vec4 base_emissive_factor;
float metallic_factor;
float roughness_factor;
float occlusion_strength;
float normal_scale;
float alpha_cutoff;
int base_color_texcoord;
int metallic_roughness_texcoord;
int occlusion_texcoord;
int emissive_texcoord;
int normal_texcoord;
} material;
const float dither_pattern[4][4] = {
{ 0.0f, 0.5f, 0.125f, 0.625f},
{ 0.75f, 0.25f, 0.875f, 0.375f},
{ 0.1875f, 0.6875f, 0.0625f, 0.5625},
{ 0.9375f, 0.4375f, 0.8125f, 0.3125}
};
vec4 one_over_d_linear_to_srgb(vec4 color)
{
/* Linear to sRGB is:
*
* if (x <= 0.0031308)
* return 12.92 * x;
* else
* return 1.055*pow(x, 1.0 / 2.4) - 0.055;
*
* Its derivative is:
* if (x <= 0.0031308)
* return 12.92;
* else
* return (1.055 / 2.4) * pow(x, -1.4 / 2.4);
*
*/
// vec3 tmp1 = vec3(1.0 / 12.92);
// vec3 tmp2 = (2.4 / 1.055) * pow(color.rgb, vec3(1.4 / 2.4));
vec3 tmp1 = vec3(0.0773993808049);
vec3 tmp2 = 2.27488151659 * pow(color.rgb, vec3(0.583333333333));
// Alpha is not converted to sRGB
return vec4(mix(tmp1, tmp2, vec3(greaterThan(color.rgb, vec3(0.0031308, 0.0031308, 0.0031308)))), 1.0);
}
vec4 dither(ivec2 frag_coord, vec4 colour)
{
ivec2 tmp = frag_coord % 4;
float dither_thd = 1.0f - dither_pattern[tmp.x][tmp.y];
return colour + one_over_d_linear_to_srgb(colour) * vec4(dither_thd, dither_thd, dither_thd, 0) / 255.0f;
}
#ifdef FRAG_SHADER
#define VS_TO_FS in
#else
#define VS_TO_FS out
#endif
// Vertex-to-fragment
layout(location = 0) VS_TO_FS vec3 normal;
layout(location = 1) VS_TO_FS vec4 tangent;
layout(location = 2) VS_TO_FS vec4 frag_pos;
layout(location = 3) VS_TO_FS vec4 light_pos;
layout(location = 4) VS_TO_FS vec2 texcoord[nb_texcoords];
+113
View File
@@ -0,0 +1,113 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
#extension GL_EXT_multiview : enable
layout (constant_id = 0) const int nb_texcoords = 2;
layout (constant_id = 1) const bool dithering = true;
layout (constant_id = 2) const bool alpha_cutout = false;
layout (constant_id = 3) const bool skinning = false;
const int nb_views = 2;
const int nb_clipping = 4;
layout(set = 0, binding = 0) uniform scene_ubo
{
mat4 view[nb_views];
vec4 light_position;
vec4 ambient_color;
vec4 light_color;
} scene;
layout(set = 0, binding = 1) uniform mesh_ubo
{
mat4 model;
mat4 modelview[nb_views];
mat4 modelviewproj[nb_views];
vec4 clipping_plane[nb_clipping];
} mesh;
layout(set = 0, binding = 2) uniform joints_ubo
{
mat4 joint_matrices[32];
} joints;
#ifdef FRAG_SHADER
layout(set = 0, binding = 3) uniform sampler2D base_color;
layout(set = 0, binding = 4) uniform sampler2D metallic_roughness;
layout(set = 0, binding = 5) uniform sampler2D occlusion;
layout(set = 0, binding = 6) uniform sampler2D emissive;
layout(set = 0, binding = 7) uniform sampler2D normal_map;
layout(set = 0, binding = 8) uniform material_ubo
{
vec4 base_color_factor;
vec4 base_emissive_factor;
float metallic_factor;
float roughness_factor;
float occlusion_strength;
float normal_scale;
float alpha_cutoff;
int base_color_texcoord;
int metallic_roughness_texcoord;
int occlusion_texcoord;
int emissive_texcoord;
int normal_texcoord;
} material;
#endif
// Vertex input
#ifdef VERT_SHADER
layout(location = 0) in vec4 in_position;
layout(location = 1) in vec4 in_color;
#endif
// Vertex-to-fragment
#ifdef VERT_SHADER
#define VERT_TO_FRAG out
#else
#define VERT_TO_FRAG in
#endif
layout(location = 0) VERT_TO_FRAG vec4 color;
// Shader code
#ifdef VERT_SHADER
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
gl_Position = mesh.modelviewproj[gl_ViewIndex] * in_position;
color = in_color;
}
#endif
#ifdef FRAG_SHADER
layout(location = 0) out vec4 out_color;
layout(early_fragment_tests) in;
void main()
{
out_color = color;
}
#endif
+79
View File
@@ -0,0 +1,79 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
#extension GL_GOOGLE_include_directive : require
#include "common.glsl.inc"
#include "pbr.glsl.inc"
// Fragment output
layout(location = 0) out vec4 out_color;
layout(early_fragment_tests) in;
void main()
{
vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord[0]);
vec3 emissive_color = (material.base_emissive_factor * texture(emissive_texture, texcoord[0])).rgb;
vec3 normal_unit;
if (use_normal_maps)
{
vec3 sampled_normal = normalize(vec3(material.normal_scale, material.normal_scale, 1) * (texture(normal_map_texture, texcoord[0]).xyz * 2 - 1));
// glTF spec §3.7.2.1
vec3 bitangent = cross(normal, tangent.xyz) * tangent.w;
mat3 TBN = mat3(tangent.xyz, bitangent, normal); // View space to tangent space?
// Normal in view space
normal_unit = TBN * sampled_normal;
}
else
normal_unit = normalize(normal);
vec3 light_dir = normalize(light_pos.xyz - frag_pos.xyz * light_pos.w);
vec3 view_dir = normalize(-frag_pos.xyz);
if (use_pbr)
{
float ao = 1 + material.occlusion_strength * (texture(occlusion_texture, texcoord[0]).r - 1);
vec4 mr = texture(metallic_roughness_texture, texcoord[0]);
vec3 half_vec = normalize(light_dir + view_dir);
float metallic = material.metallic_factor * mr.b;
float roughness = material.roughness_factor * mr.g;
vec3 light_colour = scene.light_color.rgb;
float attenuation = 1;
vec3 radiance =
per_light_radiance(albedo.rgb, metallic, roughness, light_colour, light_dir, normal_unit, view_dir, half_vec, attenuation) +
vec3(scene.ambient_color) * albedo.rgb * ao;
vec3 tmp = radiance + emissive_color;
out_color = vec4(tmp / (tmp + 1), albedo.a);
}
else
{
out_color = vec4(emissive_color + albedo.rgb * (max(0, dot(normal_unit, light_dir)) * scene.light_color.rgb + scene.ambient_color.rgb), albedo.a);
}
}
-229
View File
@@ -1,229 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
layout (constant_id = 0) const int nb_texcoords = 2;
layout (constant_id = 1) const bool dithering = true;
layout (constant_id = 2) const bool alpha_cutout = false;
layout (constant_id = 3) const bool skinning = false;
const int nb_clipping = 4;
const float fog_min_dist = 20.0;
const float fog_max_dist = 35.0;
const vec4 fog_color = vec4(0.0, 0.25, 0.5, 1.0);
layout(set = 0, binding = 0) uniform scene_ubo
{
mat4 view;
mat4 proj;
vec4 light_position;
vec4 ambient_color;
vec4 light_color;
} scene;
layout(set = 0, binding = 1) uniform mesh_ubo
{
mat4 model;
mat4 modelview;
mat4 modelviewproj;
vec4 clipping_plane[nb_clipping];
} mesh;
layout(set = 0, binding = 2) uniform joints_ubo
{
mat4 joint_matrices[32];
} joints;
#ifdef FRAG_SHADER
layout(set = 0, binding = 3) uniform sampler2D base_color;
layout(set = 0, binding = 4) uniform sampler2D metallic_roughness;
layout(set = 0, binding = 5) uniform sampler2D occlusion;
layout(set = 0, binding = 6) uniform sampler2D emissive;
layout(set = 0, binding = 7) uniform sampler2D normal_map;
layout(set = 0, binding = 8) uniform material_ubo
{
vec4 base_color_factor;
vec4 base_emissive_factor;
float metallic_factor;
float roughness_factor;
float occlusion_strength;
float normal_scale;
float alpha_cutoff;
int base_color_texcoord;
int metallic_roughness_texcoord;
int occlusion_texcoord;
int emissive_texcoord;
int normal_texcoord;
} material;
#endif
// Vertex input
#ifdef VERT_SHADER
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec4 in_tangent;
layout(location = 3) in vec2 in_texcoord[2];
layout(location = 5) in vec4 in_color;
layout(location = 6) in vec4 in_joints;
layout(location = 7) in vec4 in_weights;
#endif
// Vertex-to-fragment
#ifdef VERT_SHADER
#define VERT_TO_FRAG out
#else
#define VERT_TO_FRAG in
#endif
layout(location = 0) VERT_TO_FRAG vec3 normal;
layout(location = 1) VERT_TO_FRAG vec4 frag_pos;
layout(location = 2) VERT_TO_FRAG vec4 light_pos;
layout(location = 3) VERT_TO_FRAG vec2 texcoord[nb_texcoords];
// Fragment output
#ifdef FRAG_SHADER
layout(location = 0) out vec4 out_color;
#endif
// Shader code
#ifdef VERT_SHADER
out gl_PerVertex
{
vec4 gl_Position;
float gl_ClipDistance[nb_clipping];
};
void main()
{
// TODO: use base_color_texcoord et al instead of always using texcoord 0
for(int i = 0; i < nb_texcoords; i++)
texcoord[i] = in_texcoord[i];
if (skinning)
{
mat4 skinMatrix =
in_weights.x * joints.joint_matrices[int(in_joints.x)] +
in_weights.y * joints.joint_matrices[int(in_joints.y)] +
in_weights.z * joints.joint_matrices[int(in_joints.z)] +
in_weights.w * joints.joint_matrices[int(in_joints.w)];
normal = vec3(mesh.modelview * skinMatrix * vec4(in_normal, 0.0));
gl_Position = mesh.modelviewproj * skinMatrix * vec4(in_position, 1.0);
}
else
{
normal = vec3(mesh.modelview * vec4(in_normal, 0.0));
gl_Position = mesh.modelviewproj * vec4(in_position, 1.0);
}
frag_pos = mesh.modelview * vec4(in_position, 1.0);
light_pos = scene.view * scene.light_position;
for(int i = 0; i < nb_clipping; i++)
{
gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0));
}
}
#endif
#ifdef FRAG_SHADER
const float dither_pattern[4][4] = {
{ 0.0f, 0.5f, 0.125f, 0.625f},
{ 0.75f, 0.25f, 0.875f, 0.375f},
{ 0.1875f, 0.6875f, 0.0625f, 0.5625},
{ 0.9375f, 0.4375f, 0.8125f, 0.3125}
};
vec4 one_over_d_linear_to_srgb(vec4 color)
{
/* Linear to sRGB is:
*
* if (x <= 0.00031308)
* return 12.92 * x;
* else
* return 1.055*pow(x, 1.0 / 2.4) - 0.055;
*
* Its derivative is:
* if (x <= 0.00031308)
* return 12.92;
* else
* return (1.055 / 2.4) * pow(x, -1.4 / 2.4);
*
*/
// Alpha is not converted to sRGB
vec3 tmp1 = vec3(1.0 / 12.92, 1.0 / 12.92, 1.0 / 12.92);
vec3 tmp2 = (2.4 / 1.055) * vec3(pow(color.r, 1.4 / 2.4), pow(color.g, 1.4 / 2.4), pow(color.b, 1.4 / 2.4));
return vec4(mix(tmp1, tmp2, vec3(greaterThan(color.rgb, vec3(0.00031308, 0.00031308, 0.00031308)))), 1.0);
}
void main()
{
vec3 light_dir = normalize(light_pos.xyz - frag_pos.xyz * light_pos.w);
vec3 view_dir = normalize(frag_pos.xyz);
vec3 normal_unit = normalize(normal);
vec3 reflect_dir = reflect(-light_dir, normal_unit);
// Ambient lighting
vec3 ambient = vec3(scene.ambient_color);
// Diffuse lighting
vec3 diffuse = max(dot(normal_unit, light_dir), 0.0) * vec3(scene.light_color);
// Specular lighting
// vec3 half_dir = normalize(light_dir + view_dir);
// float spec_angle = max(dot(half_dir, normal_unit), 0.0);
// vec3 specular = pow(spec_angle, instance.specular_power) * instance.specular_strength * instance.light_color;
vec3 light = ambient + diffuse /*+ specular*/;
vec4 bc = material.base_color_factor * texture(base_color, fract(texcoord[0])) * vec4(light, 1.0);
vec4 ec = material.base_emissive_factor * texture(emissive, fract(texcoord[0]));
float fog = clamp((length(frag_pos) - fog_min_dist) / (fog_max_dist - fog_min_dist), 0.0, 1.0);
bc = mix(bc + ec, fog_color, fog);
if (dithering)
{
ivec2 tmp = ivec2(gl_FragCoord.xy) % 4;
float dither_thd = 1.0f - dither_pattern[tmp.x][tmp.y];
out_color = bc + one_over_d_linear_to_srgb(bc) * vec4(dither_thd, dither_thd, dither_thd, 0) / 255.0f;
}
else
out_color = bc;
if (alpha_cutout)
{
if (out_color.a <= material.alpha_cutoff)
out_color.a = 0.0;
else
out_color.a = 1.0;
}
}
#endif
+64
View File
@@ -0,0 +1,64 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
#extension GL_EXT_multiview : require
#extension GL_GOOGLE_include_directive : require
#include "common.glsl.inc"
// Vertex input
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec4 in_tangent;
layout(location = 3) in vec2 in_texcoord[2];
layout(location = 5) in vec4 in_color;
out gl_PerVertex
{
vec4 gl_Position;
// float gl_ClipDistance[nb_clipping];
};
void main()
{
// TODO: use base_color_texcoord et al instead of always using texcoord 0
for (int i = 0; i < nb_texcoords; i++)
texcoord[i] = in_texcoord[i];
// TODO on CPU?
mat3 mv = inverse(transpose(mat3(mesh.modelview[gl_ViewIndex])));
tangent = vec4(normalize(mv * in_tangent.xyz), in_tangent.w);
if (any(isnan(tangent))) // avoid NaNs if no tangent data is provided
tangent = vec4(0, 0, 0, 0);
normal = normalize(mv * in_normal);
gl_Position = mesh.modelviewproj[gl_ViewIndex] * vec4(in_position, 1.0);
frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0);
light_pos = scene.view[gl_ViewIndex] * scene.light_position;
// for(int i = 0; i < nb_clipping; i++)
// {
// gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0));
// }
}
+82
View File
@@ -0,0 +1,82 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
#extension GL_GOOGLE_include_directive : require
#include "common.glsl.inc"
#include "pbr.glsl.inc"
// Fragment output
layout(location = 0) out vec4 out_color;
// layout(early_fragment_tests) in;
void main()
{
vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord[0]);
vec3 emissive_color = (material.base_emissive_factor * texture(emissive_texture, texcoord[0])).rgb;
vec3 normal_unit;
if (use_normal_maps)
{
vec3 sampled_normal = normalize(vec3(material.normal_scale, material.normal_scale, 1) * (texture(normal_map_texture, texcoord[0]).xyz * 2 - 1));
// glTF spec §3.7.2.1
vec3 bitangent = cross(normal, tangent.xyz) * tangent.w;
mat3 TBN = mat3(tangent.xyz, bitangent, normal); // View space to tangent space?
// Normal in view space
normal_unit = TBN * sampled_normal;
}
else
normal_unit = normalize(normal);
vec3 light_dir = normalize(light_pos.xyz - frag_pos.xyz * light_pos.w);
vec3 view_dir = normalize(-frag_pos.xyz);
if (use_pbr)
{
float ao = 1 + material.occlusion_strength * (texture(occlusion_texture, texcoord[0]).r - 1);
vec4 mr = texture(metallic_roughness_texture, texcoord[0]);
vec3 half_vec = normalize(light_dir + view_dir);
float metallic = material.metallic_factor * mr.b;
float roughness = material.roughness_factor * mr.g;
vec3 light_colour = scene.light_color.rgb;
float attenuation = 1;
vec3 radiance =
per_light_radiance(albedo.rgb, metallic, roughness, light_colour, light_dir, normal_unit, view_dir, half_vec, attenuation) +
vec3(scene.ambient_color) * albedo.rgb * ao;
vec3 tmp = radiance + emissive_color;
out_color = vec4(tmp / (tmp + 1), albedo.a);
}
else
{
out_color = vec4(emissive_color + albedo.rgb * (max(0, dot(normal_unit, light_dir)) * scene.light_color.rgb + scene.ambient_color.rgb), albedo.a);
}
if (albedo.a <= material.alpha_cutoff)
discard;
}
+68
View File
@@ -0,0 +1,68 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
#extension GL_EXT_multiview : require
#extension GL_GOOGLE_include_directive : require
#include "common.glsl.inc"
// Vertex input
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec4 in_tangent;
layout(location = 3) in vec2 in_texcoord[2];
layout(location = 5) in vec4 in_color;
layout(location = 6) in vec4 in_joints;
layout(location = 7) in vec4 in_weights;
out gl_PerVertex
{
vec4 gl_Position;
// float gl_ClipDistance[nb_clipping];
};
void main()
{
// TODO: use base_color_texcoord et al instead of always using texcoord 0
for (int i = 0; i < nb_texcoords; i++)
texcoord[i] = in_texcoord[i];
mat4 skinMatrix =
in_weights.x * joints.joint_matrices[int(in_joints.x)] +
in_weights.y * joints.joint_matrices[int(in_joints.y)] +
in_weights.z * joints.joint_matrices[int(in_joints.z)] +
in_weights.w * joints.joint_matrices[int(in_joints.w)];
// TODO on CPU?
mat3 mv = inverse(transpose(mat3(mesh.modelview[gl_ViewIndex])));
tangent = vec4(normalize(mv * in_tangent.xyz), in_tangent.w);
if (any(isnan(tangent))) // avoid NaNs if no tangent data is provided
tangent = vec4(0, 0, 0, 0);
normal = normalize(mv * in_normal);
normal = vec3(mesh.modelview[gl_ViewIndex] * skinMatrix * vec4(in_normal, 0.0));
gl_Position = mesh.modelviewproj[gl_ViewIndex] * skinMatrix * vec4(in_position, 1.0);
frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0);
light_pos = scene.view[gl_ViewIndex] * scene.light_position;
}
+95
View File
@@ -0,0 +1,95 @@
// kate: hl glsl
// Copied from https://github.com/JoeyDeVries/LearnOpenGL/blob/master/src/6.pbr/1.1.lighting/1.1.pbr.fs
const float PI = 3.14159265359;
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
// return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
float tmp = 1.0 - max(cosTheta, 0.0);
float tmp2 = tmp * tmp;
float tmp4 = tmp2 * tmp2;
return F0 + (1.0 - F0) * tmp * tmp4;
}
vec3 per_light_radiance(
vec3 albedo,
float metallic,
float roughness,
vec3 light_colour,
vec3 L, // Light direction relative to the current fragment in view space
vec3 N, // Normal
vec3 V, // Camera position relative to the current fragment in view space
vec3 H, // Half vector, normalize(L+V)
float attenuation // Light attenuation: 1/distance^2 for point lights, 1 for directional lights
)
{
// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 radiance = light_colour * attenuation;
// Cook-Torrance BRDF
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = numerator / denominator;
// kS is equal to Fresnel
vec3 kS = F;
// for energy conservation, the diffuse and specular light can't
// be above 1.0 (unless the surface emits light); to preserve this
// relationship the diffuse component (kD) should equal 1.0 - kS.
vec3 kD = vec3(1.0) - kS;
// multiply kD by the inverse metalness such that only non-metals
// have diffuse lighting, or a linear blend if partly metal (pure metals
// have no diffuse light).
kD *= 1.0 - metallic;
// scale light by NdotL
float NdotL = max(dot(N, L), 0.0);
// add to outgoing radiance Lo
return (kD * albedo / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
}