Skip to content

Commit abe6446

Browse files
committed
shadowmaps
1 parent 6f35c17 commit abe6446

File tree

10 files changed

+297
-203
lines changed

10 files changed

+297
-203
lines changed

data/cage/shaders/engine/fragment.glsl

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,38 @@ vec3 egacLightBrdf(Material material, UniLight light)
5656
return res * light.color.rgb;
5757
}
5858

59+
float egacSampleShadowmap2d(int index, vec3 shadowPos, int cascade)
60+
{
61+
switch (index)
62+
{
63+
case 0: return sampleShadowMap2d(texShadows2d_0, shadowPos, cascade);
64+
case 1: return sampleShadowMap2d(texShadows2d_1, shadowPos, cascade);
65+
case 2: return sampleShadowMap2d(texShadows2d_2, shadowPos, cascade);
66+
case 3: return sampleShadowMap2d(texShadows2d_3, shadowPos, cascade);
67+
case 4: return sampleShadowMap2d(texShadows2d_4, shadowPos, cascade);
68+
case 5: return sampleShadowMap2d(texShadows2d_5, shadowPos, cascade);
69+
case 6: return sampleShadowMap2d(texShadows2d_6, shadowPos, cascade);
70+
case 7: return sampleShadowMap2d(texShadows2d_7, shadowPos, cascade);
71+
}
72+
return 0;
73+
}
74+
75+
float egacSampleShadowmapCube(int index, vec3 shadowPos)
76+
{
77+
switch (index)
78+
{
79+
case 0: return sampleShadowMapCube(texShadowsCube_0, shadowPos);
80+
case 1: return sampleShadowMapCube(texShadowsCube_1, shadowPos);
81+
case 2: return sampleShadowMapCube(texShadowsCube_2, shadowPos);
82+
case 3: return sampleShadowMapCube(texShadowsCube_3, shadowPos);
83+
case 4: return sampleShadowMapCube(texShadowsCube_4, shadowPos);
84+
case 5: return sampleShadowMapCube(texShadowsCube_5, shadowPos);
85+
case 6: return sampleShadowMapCube(texShadowsCube_6, shadowPos);
86+
case 7: return sampleShadowMapCube(texShadowsCube_7, shadowPos);
87+
}
88+
return 0;
89+
}
90+
5991
float egacShadowedIntensity(UniShadowedLight uni)
6092
{
6193
float normalOffsetScale = uni.shadowParams[2];
@@ -82,10 +114,10 @@ float egacShadowedIntensity(UniShadowedLight uni)
82114
{
83115
case 12:
84116
case 14:
85-
intensity = sampleShadowMap2d(texShadows2d, shadowPos, shadowmapSamplerIndex * 4 + cascade);
117+
intensity = egacSampleShadowmap2d(shadowmapSamplerIndex, shadowPos, cascade);
86118
break;
87119
case 16:
88-
intensity = sampleShadowMapCube(texShadowsCube, shadowPos, shadowmapSamplerIndex);
120+
intensity = egacSampleShadowmapCube(shadowmapSamplerIndex, shadowPos);
89121
break;
90122
}
91123
return mix(1, intensity, uni.shadowParams[3]);

data/cage/shaders/engine/uniforms.glsl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,24 @@ layout(std430, set = 0, binding = 3) readonly buffer ShadowedLightsBlock
6363

6464
layout(set = 0, binding = 4) uniform sampler2D texSsao;
6565
layout(set = 0, binding = 6) uniform sampler2D texDepth;
66-
//layout(set = 0, binding = 8) uniform sampler2DArray texShadows2d[8];
67-
//layout(set = 0, binding = 10) uniform samplerCube texShadowsCube[8];
68-
layout(set = 0, binding = 8) uniform sampler2DArray texShadows2d; // array of cascades (always 4) of 2d shadowmaps
69-
layout(set = 0, binding = 10) uniform samplerCubeArray texShadowsCube; // array of cube shadowmaps
66+
67+
layout(set = 0, binding = 8) uniform sampler2DArray texShadows2d_0;
68+
layout(set = 0, binding = 10) uniform sampler2DArray texShadows2d_1;
69+
layout(set = 0, binding = 12) uniform sampler2DArray texShadows2d_2;
70+
layout(set = 0, binding = 14) uniform sampler2DArray texShadows2d_3;
71+
layout(set = 0, binding = 16) uniform sampler2DArray texShadows2d_4;
72+
layout(set = 0, binding = 18) uniform sampler2DArray texShadows2d_5;
73+
layout(set = 0, binding = 20) uniform sampler2DArray texShadows2d_6;
74+
layout(set = 0, binding = 22) uniform sampler2DArray texShadows2d_7;
75+
76+
layout(set = 0, binding = 24) uniform samplerCube texShadowsCube_0;
77+
layout(set = 0, binding = 26) uniform samplerCube texShadowsCube_1;
78+
layout(set = 0, binding = 28) uniform samplerCube texShadowsCube_2;
79+
layout(set = 0, binding = 30) uniform samplerCube texShadowsCube_3;
80+
layout(set = 0, binding = 32) uniform samplerCube texShadowsCube_4;
81+
layout(set = 0, binding = 34) uniform samplerCube texShadowsCube_5;
82+
layout(set = 0, binding = 36) uniform samplerCube texShadowsCube_6;
83+
layout(set = 0, binding = 38) uniform samplerCube texShadowsCube_7;
7084

7185
////////////////////////////////////////////////////////////////////////////////
7286
// set = 1

data/cage/shaders/functions/sampleShadowMap.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$include poissonDisk4.glsl
33
$include randomAngle.glsl
44

5-
float sampleShadowMap2d(sampler2DArray texShadow, vec3 shadowPos, int index)
5+
float sampleShadowMap2d(sampler2DArray texShadow, vec3 shadowPos, int cascade)
66
{
77
vec2 radius = 1.3 / textureSize(texShadow, 0).xy;
88
float visibility = 0.0;
@@ -11,13 +11,13 @@ float sampleShadowMap2d(sampler2DArray texShadow, vec3 shadowPos, int index)
1111
float ra = randomAngle(12000.0, shadowPos);
1212
mat2 rot = mat2(cos(ra), sin(ra), -sin(ra), cos(ra));
1313
vec2 samp = rot * poissonDisk4[i] * radius;
14-
visibility += float(textureLod(texShadow, vec3(shadowPos.xy + samp, float(index)), 0).x > shadowPos.z);
14+
visibility += float(textureLod(texShadow, vec3(shadowPos.xy + samp, float(cascade)), 0).x > shadowPos.z);
1515
}
1616
visibility /= 4.0;
1717
return visibility;
1818
}
1919

20-
float sampleShadowMapCube(samplerCubeArray texShadow, vec3 shadowPos, int index)
20+
float sampleShadowMapCube(samplerCube texShadow, vec3 shadowPos)
2121
{
22-
return float(textureLod(texShadow, vec4(normalize(shadowPos), float(index)), 0).x > length(shadowPos));
22+
return float(textureLod(texShadow, normalize(shadowPos), 0).x > length(shadowPos));
2323
}

externals/dawn/CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,23 @@ set(TINT_BUILD_IR_BINARY OFF CACHE INTERNAL "Build IR binary format support" FOR
5050
set(TINT_BUILD_TESTS OFF CACHE INTERNAL "Build tests" FORCE)
5151
set(TINT_ENABLE_IR_VALIDATION ON CACHE INTERNAL "Enable IR validation for backend codegen" FORCE)
5252

53-
# protobuf
54-
add_library(libprotobuf-mutator INTERFACE) # subvert unnecessary dependencies
55-
5653
unset(metal)
5754
unset(vulkan)
5855

56+
# replace constants header
57+
set(constants_orig_path "${CMAKE_CURRENT_LIST_DIR}/dawn/src/dawn/common/Constants.h")
58+
set(constants_dst_path "${CMAKE_CURRENT_BINARY_DIR}/patches/dawn/common/Constants.h")
59+
message(STATUS "constants_orig_path: ${constants_orig_path}")
60+
message(STATUS "constants_orig_path: ${constants_dst_path}")
61+
include_directories("${CMAKE_CURRENT_BINARY_DIR}/patches")
62+
file(READ "${constants_orig_path}" constants_content)
63+
string(REPLACE "kMaxSampledTexturesPerShaderStage = 16;" "kMaxSampledTexturesPerShaderStage = 32;" constants_content "${constants_content}")
64+
string(REPLACE "kMaxSamplersPerShaderStage = 16;" "kMaxSamplersPerShaderStage = 32;" constants_content "${constants_content}")
65+
file(CONFIGURE OUTPUT "${constants_dst_path}" CONTENT "${constants_content}" @ONLY)
66+
67+
# protobuf
68+
add_library(libprotobuf-mutator INTERFACE) # subvert unnecessary dependencies
69+
5970
# add the externals
6071
add_subdirectory(dawn)
6172

externals/glm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ message(STATUS "----------------------------------------------------------------
44

55
add_library(glm INTERFACE)
66
target_include_directories(glm INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/glm")
7+
target_compile_definitions(glm INTERFACE "GLM_FORCE_DEPTH_ZERO_TO_ONE=1")
78

89
cage_gen_module(glm $<TARGET_LINKER_FILE:glm> "")

sources/libcore/math/camera.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace cage
3333
CAGE_ASSERT(fov > Rads(0));
3434
CAGE_ASSERT(aspectRatio > 0);
3535
CAGE_ASSERT(sign(near) == sign(far) && near != far);
36-
Real f = 1 / tan(fov / 2);
37-
return Mat4(f / aspectRatio, 0, 0, 0, 0, f, 0, 0, 0, 0, -far / (far - near), -1, 0, 0, (-far * near) / (far - near), 0);
36+
const Real f = 1 / tan(fov / 2);
37+
return Mat4(f / aspectRatio, 0, 0, 0, 0, f, 0, 0, 0, 0, far / (near - far), -1, 0, 0, (far * near) / (near - far), 0);
3838
}
3939

4040
Mat4 perspectiveProjection(Rads fov, Real aspectRatio, Real near, Real far, Real zeroParallaxDistance, Real eyeSeparation)
@@ -56,15 +56,15 @@ namespace cage
5656
CAGE_ASSERT(left != right);
5757
CAGE_ASSERT(bottom != top);
5858
CAGE_ASSERT(sign(near) == sign(far) && near != far);
59-
return Mat4((2 * near) / (right - left), 0, 0, 0, 0, -(2 * near) / (bottom - top), 0, 0, (right + left) / (right - left), -(bottom + top) / (bottom - top), -far / (far - near), -1, 0, 0, -(far * near) / (far - near), 0);
59+
return Mat4((2 * near) / (right - left), 0, 0, 0, 0, (2 * near) / (top - bottom), 0, 0, (right + left) / (right - left), (top + bottom) / (top - bottom), far / (near - far), -1, 0, 0, (far * near) / (near - far), 0);
6060
}
6161

6262
Mat4 orthographicProjection(Real left, Real right, Real bottom, Real top, Real near, Real far)
6363
{
6464
CAGE_ASSERT(left != right);
6565
CAGE_ASSERT(bottom != top);
6666
CAGE_ASSERT(near != far);
67-
return Mat4(2 / (right - left), 0, 0, 0, 0, 2 / (top - bottom), 0, 0, 0, 0, -1 / (far - near), 0, -(right + left) / (right - left), -(top + bottom) / (top - bottom), -near / (far - near), 1);
67+
return Mat4(2 / (right - left), 0, 0, 0, 0, 2 / (top - bottom), 0, 0, 0, 0, 1 / (near - far), 0, -(right + left) / (right - left), -(top + bottom) / (top - bottom), near / (near - far), 1);
6868
}
6969

7070
StringPointer stereoModeToString(StereoModeEnum mode)

sources/libengine/graphics/bindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace cage
8787

8888
for (const auto &t : config.textures)
8989
{
90-
CAGE_ASSERT(t.texture && t.texture->nativeTexture() && t.texture->nativeSampler());
90+
CAGE_ASSERT(t.texture && t.texture->nativeTexture() && t.texture->nativeView() && t.texture->nativeSampler());
9191
const bool filterable = isFormatFilterable(t.texture->nativeTexture().GetFormat());
9292
{
9393
wgpu::BindGroupLayoutEntry e = {};

sources/libengine/graphics/device.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ namespace cage
197197
desc.requiredFeatures = requiredFeatures.data();
198198
desc.requiredFeatureCount = requiredFeatures.size();
199199

200+
wgpu::Limits requiredLimits = {};
201+
requiredLimits.maxSampledTexturesPerShaderStage = 32;
202+
requiredLimits.maxSamplersPerShaderStage = 32;
203+
desc.requiredLimits = &requiredLimits;
204+
200205
static constexpr std::array<const char *, 2> toggles = {
201206
"use_user_defined_labels_in_backend", // show my names for textures etc in nsight
202207
"disable_symbol_renaming", // preserve variable names in shaders

0 commit comments

Comments
 (0)