Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ jobs:
- name: Install shader tools
run: |
sudo apt-get update
sudo apt-get install -y glslc glslang-tools
sudo apt-get install -y glslc glslang-tools spirv-tools

# Byte comparison against the committed .spv is not usable here: glslc
# embeds its own generator version, so a different toolchain produces
Expand Down Expand Up @@ -297,6 +297,15 @@ jobs:
done
exit $fail

- name: Game frame block matches its C++ mirror
working-directory: SleakCraft
run: |
python3 Engine/scripts/check_ubo_layout.py \
--header Game/include/World/VoxelLighting.hpp \
--include-dir Engine/assets/shaders/include \
--include-dir Game/assets/shaders/include \
--check CraftFrameData:craft/frame.glsl:CraftFrameUBO

# Compiling proves nothing about whether the game runs. Runners have no GPU,
# but Mesa's software Vulkan (lavapipe) plus a virtual display is enough to
# boot the real binary into a real world and catch startup crashes, missing
Expand Down
2 changes: 1 addition & 1 deletion Engine
Submodule Engine updated 36 files
+1 −0 CMakeLists.txt
+50 −28 assets/shaders/bloom_composite.frag
+ − assets/shaders/bloom_composite.frag.spv
+6 −0 assets/shaders/bloom_threshold.frag
+ − assets/shaders/bloom_threshold.frag.spv
+59 −0 assets/shaders/include/sleak/color.glsl
+7 −2 assets/shaders/include/sleak/frame.glsl
+21 −0 assets/shaders/include/sleak/frame_output.glsl
+27 −0 assets/shaders/include/sleak/lighting_default.glsl
+130 −0 assets/shaders/include/sleak/tonemap.glsl
+ − assets/shaders/lighting_pass.frag.spv
+12 −1 assets/shaders/skybox.frag
+ − assets/shaders/skybox.frag.spv
+5 −0 docs/pages/assets_resources.md
+9 −3 docs/pages/backend_support.md
+4 −3 docs/pages/graphics_features.md
+3 −2 docs/pages/rendering_pipeline.md
+5 −2 docs/pages/shader_authoring.md
+12 −2 include/private/Graphics/Common/ConstantBuffer.hpp
+2 −0 include/private/Graphics/Common/FrameDataBuilder.hpp
+39 −0 include/private/Graphics/Common/OutputTransform.hpp
+54 −4 include/private/Graphics/Common/Renderer.hpp
+4 −0 include/private/Graphics/OpenGL/OpenGLRenderer.hpp
+68 −4 include/private/Graphics/Vulkan/VulkanRenderer.hpp
+27 −0 include/public/Core/GraphicsConfig.hpp
+4 −0 include/public/ECS/Components/MeshComponent.hpp
+8 −0 src/Core/Application.cpp
+8 −0 src/Graphics/Common/FrameData.cpp
+86 −0 src/Graphics/Common/OutputTransform.cpp
+28 −2 src/Graphics/OpenGL/OpenGLRenderer.cpp
+47 −26 src/Graphics/Vulkan/VulkanBloom.cpp
+1 −1 src/Graphics/Vulkan/VulkanDeferred.cpp
+1 −0 src/Graphics/Vulkan/VulkanFrameData.cpp
+15 −2 src/Graphics/Vulkan/VulkanRenderer.cpp
+18 −0 src/Graphics/Vulkan/VulkanSwapchain.cpp
+8 −0 src/Scene/MeshComponent.cpp
3 changes: 2 additions & 1 deletion Game/assets/shaders/flat_shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ layout(set = 0, binding = 0) uniform sampler2D diffuseTexture;

#include "sleak/shadow_light.glsl"
#include "sleak/frame.glsl"
#include "sleak/color.glsl"

layout(set = 3, binding = 0) uniform sampler2DShadow shadowMap;

Expand Down Expand Up @@ -73,7 +74,7 @@ void main() {

// AO is stored in vertex color. Apply only to ambient.
float ao = fragColor.r;
vec3 baseColor = texColor.rgb;
vec3 baseColor = SleakLinearToSrgb(texColor.rgb);

vec3 N = normalize(fragWorldNorm);
vec3 lightDir = normalize(uLightDir.xyz);
Expand Down
Binary file modified Game/assets/shaders/flat_shader.frag.spv
Binary file not shown.
3 changes: 2 additions & 1 deletion Game/assets/shaders/flat_shader_gl.frag
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ layout(std140, binding = 5) uniform ShadowUBO {
};

#include "sleak/frame.glsl"
#include "sleak/color.glsl"

layout(binding = 0) uniform sampler2D diffuseTexture;
layout(binding = 3) uniform sampler2DShadow shadowMap;
Expand Down Expand Up @@ -104,7 +105,7 @@ void main() {

// AO is stored in vertex color. Apply only to ambient.
float ao = fragColor.r;
vec3 baseColor = texColor.rgb;
vec3 baseColor = SleakLinearToSrgb(texColor.rgb);

vec3 N = normalize(fragWorldNorm);

Expand Down
18 changes: 8 additions & 10 deletions Game/assets/shaders/gbuffer_voxel.frag
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#version 450
#extension GL_GOOGLE_include_directive : require

// GBuffer pass, voxel format: engine gbuffer.frag plus RT3 (voxel model)
// GBuffer pass, voxel format: engine gbuffer.frag with the voxel RT2 and RT3
// (craft/voxel_surface.glsl)

#include "sleak/frame.glsl"
#include "craft/shading_models.glsl"
#include "craft/voxel_surface.glsl"

layout(location = 0) in vec3 fragWorldPos;
layout(location = 1) in vec3 fragWorldNorm;
Expand Down Expand Up @@ -90,14 +91,11 @@ void main() {
: fragColor.r * aoFactor;
ao = clamp(ao, 0.0, 1.0);

// --- Emissive (HDR in RT2.gba) ---
vec3 emissive = hasEmissiveMap != 0u
? texture(texEmissive, uv).rgb * emissiveFactor.rgb * emissiveFactor.w
: emissiveFactor.rgb * emissiveFactor.w;

outAlbedoAO = SleakFrameGuard(vec4(albedoSample.rgb, ao));
outNormalRough = vec4(N * 0.5 + 0.5, roughness);
outMetalEmit = vec4(metallic, emissive);
outMotionModel = SleakEncodeMotionModel(vec2(0.0), 0.0,
CRAFT_SHADING_MODEL_VOXEL);
outMetalEmit = CraftEncodeVoxelRT2(metallic,
CraftTexelEmission(albedoSample.rgb),
CRAFT_NO_BLOCK_LIGHT);
outMotionModel = CraftEncodeVoxelRT3(CRAFT_SKY_LIGHT_OPEN,
CRAFT_SHADING_MODEL_VOXEL);
}
Binary file modified Game/assets/shaders/gbuffer_voxel.frag.spv
Binary file not shown.
Binary file modified Game/assets/shaders/gbuffer_voxel.vert.spv
Binary file not shown.
16 changes: 8 additions & 8 deletions Game/assets/shaders/gbuffer_voxel_gl.frag
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#version 450 core
#extension GL_GOOGLE_include_directive : require

// GBuffer pass, OpenGL, voxel format: engine gbuffer_gl.frag plus RT3
// GBuffer pass, OpenGL, voxel format: engine gbuffer_gl.frag with the voxel
// RT2 and RT3 (craft/voxel_surface.glsl)

in vec3 fragWorldPos;
in vec3 fragWorldNorm;
Expand Down Expand Up @@ -52,7 +53,7 @@ layout(std140, binding = 1) uniform MaterialUBO {
};

#include "sleak/frame.glsl"
#include "craft/shading_models.glsl"
#include "craft/voxel_surface.glsl"

void main() {
vec2 uv = fragUV * matTiling + matOffset;
Expand All @@ -79,13 +80,12 @@ void main() {
: matMetallic;
float ao = (HasAOMap != 0u) ? texture(aoTexture, uv).r
: (matAO * fragColor.r);
float emitScale = (HasEmissiveMap != 0u)
? length(texture(emissiveTexture, uv).rgb)
: length(matEmissiveColor) * matEmissiveIntensity;

outAlbedoAO = SleakFrameGuard(vec4(albedo.rgb, ao));
outNormalRough = vec4(N * 0.5 + 0.5, roughness); // pack normal to [0,1]
outMetalEmit = vec4(metallic, clamp(emitScale, 0.0, 1.0), 0.0, 1.0);
outMotionModel = SleakEncodeMotionModel(vec2(0.0), 0.0,
CRAFT_SHADING_MODEL_VOXEL);
outMetalEmit = CraftEncodeVoxelRT2(metallic,
CraftTexelEmission(albedo.rgb),
CRAFT_NO_BLOCK_LIGHT);
outMotionModel = CraftEncodeVoxelRT3(CRAFT_SKY_LIGHT_OPEN,
CRAFT_SHADING_MODEL_VOXEL);
}
20 changes: 20 additions & 0 deletions Game/assets/shaders/include/craft/frame.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CRAFT_FRAME_GLSL
#define CRAFT_FRAME_GLSL

#include "sleak/frame.glsl"

// SleakCraft per-frame block (CraftFrameData in World/VoxelLighting.hpp),
// std140, append only; colors are linear (squared authoring values)
SLEAK_FRAME_USER_BLOCK CraftFrameUBO {
vec4 SunColor; // rgb direct sun or moon light
vec4 AmbientColor; // rgb sky light reaching shadowed faces
vec4 MinLightColor; // rgb floor light where the sky is hidden
vec4 BlockLightColor; // rgb torch tint at full block light
vec4 NightTint; // rgb unit hue of moonlit desaturation
vec4 WeatherTint; // rgb unit hue of rainy desaturation
vec4 Sky; // sun visibility, rain, shadow fade, night vision
vec4 Look; // desaturation, emission gain, albedo balance,
// SSAO exponent
} uCraft;

#endif
158 changes: 158 additions & 0 deletions Game/assets/shaders/include/craft/lighting.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#ifndef CRAFT_LIGHTING_GLSL
#define CRAFT_LIGHTING_GLSL

#include "craft/frame.glsl"

// Face brightness: (up base + up slope * n.y + side spread * (side pivot -
// |n.x|) * (1 - |n.y|))^2, so top 1, z sides 0.72, x sides 0.49, bottom 0.25
#define CRAFT_FACE_UP_BASE 0.75
#define CRAFT_FACE_UP_SLOPE 0.25
#define CRAFT_FACE_SIDE_SPREAD 0.15
#define CRAFT_FACE_SIDE_PIVOT 0.667
// Terminator remap of N.L
#define CRAFT_NDOTL_GAIN 1.01
#define CRAFT_NDOTL_OFFSET 0.01
// Share of direct light rain removes
#define CRAFT_RAIN_SUN_LOSS 0.95
// Torch curve (level^power * gain + level * linear)^2
#define CRAFT_BLOCK_POWER 10.0
#define CRAFT_BLOCK_GAIN 1.6
#define CRAFT_BLOCK_LINEAR 0.6
// Emissive texels: tint toward white, flatten exponent
#define CRAFT_EMISSION_WHITE 0.5
#define CRAFT_FLATTEN_POWER 128.0
// Light night vision adds everywhere
#define CRAFT_NIGHT_VISION_LIGHT 0.25
// Foliage: wrap of N.L and forward scattering lobe
#define CRAFT_SSS_WRAP 0.7
#define CRAFT_SSS_LOBE_POWER 16.0
#define CRAFT_HALF 0.5
// Desaturation: rain keep, block light edges, cave grey, tint gain
#define CRAFT_DESAT_RAIN_KEEP 0.7
#define CRAFT_DESAT_BLOCK_LOW 0.25
#define CRAFT_DESAT_BLOCK_HIGH 1.0
#define CRAFT_DESAT_CAVE_GREY 0.4
#define CRAFT_DESAT_TINT_GAIN 1.7
// Rec. 601 luma
#define CRAFT_LUMA vec3(0.299, 0.587, 0.114)
// Keeps normalize away from a zero vector
#define CRAFT_COLOR_EPSILON 1e-5

/// Decoded voxel GBuffer texel.
struct CraftSurface {
vec3 albedo; // linear
float ao; // baked vertex AO
vec3 normal; // world, unit length
float sky; // sky light 0..1
float block; // block light 0..1
float emission; // 0..1
float sss; // foliage scattering 0..1
};

/// Light geometry and shadow map result of one pixel.
struct CraftLightSample {
vec3 toLight; // unit, world
vec3 toPixel; // unit, camera to pixel
float shadow; // shadow map visibility 0..1
};

/// Minecraft-like face brightness from the world normal.
float CraftFaceShade(vec3 n) {
float f = CRAFT_FACE_UP_BASE + CRAFT_FACE_UP_SLOPE * n.y +
CRAFT_FACE_SIDE_SPREAD * (CRAFT_FACE_SIDE_PIVOT - abs(n.x)) *
(1.0 - abs(n.y));
return f * f;
}

/// True when the shadow map can change this pixel.
bool CraftNeedsShadow(CraftSurface s, vec3 toLight) {
return dot(s.normal, toLight) > 0.0 || s.sss > 0.0;
}

/// Torch light at a block light level.
vec3 CraftBlockLight(float level) {
float curve = pow(level, CRAFT_BLOCK_POWER) * CRAFT_BLOCK_GAIN +
level * CRAFT_BLOCK_LINEAR;
return uCraft.BlockLightColor.rgb * (curve * curve);
}

/// 1 for emitters, which skip face shading and AO; 0 for other texels.
float CraftLightFlatten(float emission) {
return 1.0 - pow(1.0 - emission, CRAFT_FLATTEN_POWER);
}

/// Self light of an emissive texel, tinted by its own hue.
vec3 CraftEmissionLight(vec3 albedo, float emission) {
vec3 hue = normalize(albedo + CRAFT_COLOR_EPSILON);
return mix(hue, vec3(1.0), emission * CRAFT_EMISSION_WHITE) * emission *
uCraft.Look.y;
}

/// Pulls dark colors away from daylight toward a moon, rain or cave grey.
vec3 CraftDesaturate(vec3 color, CraftSurface s, float flatten) {
float weight = uCraft.Look.x;
if (weight <= 0.0) return color;
float daylight = sqrt(s.sky) * uCraft.Sky.x *
(1.0 - CRAFT_DESAT_RAIN_KEEP * uCraft.Sky.y);
float unlit = 1.0 - s.block;
float amount = clamp(1.0 - daylight, 0.0, 1.0) *
smoothstep(CRAFT_DESAT_BLOCK_LOW, CRAFT_DESAT_BLOCK_HIGH,
unlit * unlit) *
(1.0 - flatten);
vec3 outdoor = mix(uCraft.WeatherTint.rgb, uCraft.NightTint.rgb,
(1.0 - uCraft.Sky.x) * (1.0 - uCraft.Sky.y));
vec3 tint = mix(vec3(CRAFT_DESAT_CAVE_GREY), outdoor, sqrt(s.sky)) *
CRAFT_DESAT_TINT_GAIN;
vec3 grey = mix(color, dot(color, CRAFT_LUMA) * tint, weight);
return mix(color, grey, amount);
}

/// Lit voxel color in linear scene units, before SSAO and fog.
vec3 CraftShadeSurface(CraftSurface s, CraftLightSample l) {
// Shadow, terminator
float visibility = clamp(l.shadow, 0.0, 1.0);
visibility *= mix(visibility, 1.0, s.sss);
float nDotL = clamp(dot(s.normal, l.toLight), 0.0, 1.0);
nDotL = clamp(nDotL * CRAFT_NDOTL_GAIN - CRAFT_NDOTL_OFFSET, 0.0, 1.0);

// Foliage wrap and forward scattering
float scatter = 0.0;
if (s.sss > 0.0) {
float forward = clamp(dot(l.toPixel, l.toLight) * CRAFT_HALF +
CRAFT_HALF, 0.0, 1.0);
scatter = pow(forward, CRAFT_SSS_LOBE_POWER) * (1.0 - uCraft.Sky.y) *
s.sss * uCraft.Sky.z;
nDotL = mix(nDotL, 1.0, sqrt(s.sss) * CRAFT_SSS_WRAP);
nDotL = mix(nDotL, 1.0, scatter);
}

// Sky: ambient where blocked, sun where lit
float sunShare = visibility * nDotL *
(1.0 - CRAFT_RAIN_SUN_LOSS * uCraft.Sky.y) * uCraft.Sky.z;
vec3 sky = mix(uCraft.AmbientColor.rgb * s.sky, uCraft.SunColor.rgb,
sunShare) *
(s.sky * s.sky) * (1.0 + scatter * visibility);

// Torch, emission, night vision, cave floor
float flatten = CraftLightFlatten(s.emission);
vec3 light = sky + CraftBlockLight(s.block) +
CraftEmissionLight(s.albedo, s.emission) +
vec3(uCraft.Sky.w * CRAFT_NIGHT_VISION_LIGHT) +
uCraft.MinLightColor.rgb * (1.0 - s.sky * s.sky);

// Albedo balance, face shading, squared vertex AO
float brightest = max(s.albedo.r, max(s.albedo.g, s.albedo.b));
vec3 albedo =
s.albedo / (1.0 + brightest * uCraft.Look.z * (1.0 - flatten));
float face = mix(CraftFaceShade(s.normal), 1.0, flatten);
float ao = mix(s.ao, 1.0, flatten);
vec3 color = albedo * max(light, vec3(0.0)) * face * (ao * ao);
return CraftDesaturate(color, s, flatten);
}

/// Screen-space AO multiplier of the whole lit color.
float CraftAmbientOcclusion(float ssao) {
return pow(clamp(ssao, 0.0, 1.0), uCraft.Look.w);
}

#endif
64 changes: 64 additions & 0 deletions Game/assets/shaders/include/craft/lighting_pass.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef CRAFT_LIGHTING_PASS_GLSL
#define CRAFT_LIGHTING_PASS_GLSL

// Voxel half of SleakCraft's deferred lighting stems (lighting_voxel and
// lighting_voxel_gl); fog stays with each stem. Includes stay outside #ifdef:
// the OpenGL resolver expands them all.

#include "sleak/lighting_default.glsl"
#include "sleak/frame_output.glsl"
#include "craft/voxel_surface.glsl"
#include "craft/lighting.glsl"

// No foliage scattering until a foliage shading model exists
#define CRAFT_NO_SCATTERING 0.0
// N.L that leaves the OpenGL helper's back-face fade at 1
#define CRAFT_FULL_FACING 1.0
// Upper HDR clamp that keeps NaNs out of the bloom chain
#define CRAFT_MAX_HDR 1000.0

/// Voxel surface of one GBuffer texel.
CraftSurface CraftDecodeSurface(ivec2 px, vec4 rt3) {
vec4 albedoAO = texelFetch(gAlbedoAO, px, 0);
vec4 normalRough = texelFetch(gNormalRough, px, 0);
vec4 metalEmit = texelFetch(gMetalEmit, px, 0);
CraftSurface s;
s.albedo = max(albedoAO.rgb, vec3(0.0));
s.ao = clamp(albedoAO.a, 0.0, 1.0);
s.normal = normalize(normalRough.rgb * 2.0 - 1.0);
s.sky = CraftVoxelSkyLight(rt3);
s.block = CraftVoxelBlockLight(metalEmit);
s.emission = CraftVoxelEmission(metalEmit);
s.sss = CRAFT_NO_SCATTERING;
return s;
}

/// Raw sun visibility from the backend's shadow filter.
float CraftShadowVisibility(vec3 worldPos, vec3 n) {
#ifdef VULKAN
return SleakCalcShadow(worldPos, n);
#else
return SleakCalcShadowPCF(worldPos, CRAFT_FULL_FACING);
#endif
}

/// Voxel lighting of one non-sky pixel in linear scene units, before fog.
vec3 CraftShadeVoxel(ivec2 px, vec2 uv, float depth, vec4 rt3,
out vec3 worldPos) {
worldPos = SleakReconstructWorldPos(uv, depth);
CraftSurface s = CraftDecodeSurface(px, rt3);

CraftLightSample l;
l.toLight = normalize(uFrame.LightDir.xyz);
l.toPixel = normalize(worldPos - uFrame.CameraPos.xyz);
l.shadow = CraftNeedsShadow(s, l.toLight)
? CraftShadowVisibility(worldPos, s.normal)
: 0.0;

vec3 color = CraftShadeSurface(s, l);
if (SleakSSAOEnabled())
color *= CraftAmbientOcclusion(texture(gSSAO, uv).r);
return clamp(color, vec3(0.0), vec3(CRAFT_MAX_HDR));
}

#endif
8 changes: 8 additions & 0 deletions Game/assets/shaders/include/craft/shading_models.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@

// SleakCraft shading model ids in GBuffer RT3.w; 0 belongs to the engine
#define CRAFT_SHADING_MODEL_VOXEL 1u
// Last id the voxel lighting shades (phase 11 appends foliage)
#define CRAFT_SHADING_MODEL_LAST_VOXEL CRAFT_SHADING_MODEL_VOXEL

/// True for ids the voxel lighting composition shades.
bool CraftIsVoxelModel(uint id) {
return id >= CRAFT_SHADING_MODEL_VOXEL &&
id <= CRAFT_SHADING_MODEL_LAST_VOXEL;
}

#endif
Loading
Loading