Skip to content

Commit

Permalink
Adding needStaticUpdate on shadow maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
clementlandrin committed Nov 29, 2024
1 parent 2e125d4 commit 8e23d81
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
2 changes: 2 additions & 0 deletions h3d/pass/CascadeShadowMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CascadeShadowMap extends DirShadowMap {
return cshader.cascadeShadowMaps;
}

override function needStaticUpdate() { }

function computeNearFar( i : Int, previousFar : Float ) {
var max = maxDist < 0.0 ? ctx.camera.zFar : maxDist;
var step = max - firstCascadeSize;
Expand Down
7 changes: 5 additions & 2 deletions h3d/pass/CubeShadowMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ class CubeShadowMap extends Shadows {
return;
}

var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, [Cube]);
var computingStatic = ctx.computingStatic || updateStatic;

var texture = computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, [Cube]);
if( depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed() ) {
if( depth != null ) depth.dispose();
depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
Expand Down Expand Up @@ -213,11 +215,12 @@ class CubeShadowMap extends Shadows {
if( blur.radius > 0 )
blur.apply(ctx, texture);

if( mode == Mixed && !ctx.computingStatic )
if( mode == Mixed && !computingStatic )
syncShader(merge(texture));
else
syncShader(texture);

updateStatic = false;
}

function merge( dynamicTex : h3d.mat.Texture ) : h3d.mat.Texture{
Expand Down
12 changes: 9 additions & 3 deletions h3d/pass/DirShadowMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,16 @@ class DirShadowMap extends Shadows {
}
super.draw(passes, sort);

var doBlur = blur.radius > 0 && (mode != Mixed || !ctx.computingStatic);
var computingStatic = ctx.computingStatic || updateStatic;

var doBlur = blur.radius > 0 && (mode != Mixed || !computingStatic);

if( border != null && !doBlur )
border.render();

ctx.engine.popTarget();

if( mode == Mixed && !ctx.computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
if( mode == Mixed && !computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
if ( staticTexture.width != tex.width )
throw "Static shadow map doesnt match dynamic shadow map";
var merge = ctx.textures.allocTarget("mergedDirShadowMap", size, size, false, format);
Expand Down Expand Up @@ -320,7 +322,9 @@ class DirShadowMap extends Shadows {
if( !filterPasses(passes) )
return;

if( mode != Mixed || ctx.computingStatic ) {
var computingStatic = ctx.computingStatic || updateStatic;

if( mode != Mixed || computingStatic ) {
var ct = ctx.camera.target;
var slight = light == null ? ctx.lightSystem.shadowLight : light;
var ldir = slight == null ? null : @:privateAccess slight.getShadowDirection();
Expand Down Expand Up @@ -360,6 +364,8 @@ class DirShadowMap extends Shadows {

syncShader(texture);

updateStatic = false;

#if editor
drawDebug();
#end
Expand Down
60 changes: 35 additions & 25 deletions h3d/pass/Shadows.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Shadows extends Output {
var format : hxd.PixelFormat;
var staticTexture : h3d.mat.Texture;
var light : h3d.scene.Light;
var updateStatic : Bool = false;
public var enabled(default,set) : Bool = true;
public var mode(default,set) : RenderMode = None;
public var size(default,set) : Int = 1024;
Expand Down Expand Up @@ -98,46 +99,56 @@ class Shadows extends Output {
throw "Not implemented";
}

/**
* Triggers update of static part of shadows (if any).
**/
public function needStaticUpdate() {
switch ( mode ) {
case Mixed, Static:
updateStatic = true;
case None, Dynamic:
}
}

function createDefaultShadowMap() {
var tex = h3d.mat.Texture.fromColor(0xFFFFFF);
tex.name = "defaultShadowMap";
return tex;
}

function syncEarlyExit() {
syncShader(staticTexture == null ? createDefaultShadowMap() : staticTexture);
syncShader(staticTexture == null ? createDefaultShadowMap() : staticTexture);
}

function syncShader( texture : h3d.mat.Texture ) {
}

function filterPasses( passes : h3d.pass.PassList ) {
if( !ctx.computingStatic ) {
if ( ctx.computingStatic || updateStatic ) {
switch( mode ) {
case None:
return false;
case Dynamic:
return true;
case Mixed:
passes.filter(function(p) return p.pass.isStatic == false);
return true;
case Static:
syncEarlyExit();
return false;
case None:
return false;
case Dynamic:
return false;
case Mixed:
passes.filter(function(p) return p.pass.isStatic == true);
return true;
case Static:
passes.filter(function(p) return p.pass.isStatic == true);
return true;
}
}
else {
} else {
switch( mode ) {
case None:
return false;
case Dynamic:
return false;
case Mixed:
passes.filter(function(p) return p.pass.isStatic == true);
return true;
case Static:
passes.filter(function(p) return p.pass.isStatic == true);
return true;
case None:
return false;
case Dynamic:
return true;
case Mixed:
passes.filter(function(p) return p.pass.isStatic == false);
return true;
case Static:
syncEarlyExit();
return false;
}
}
}
Expand All @@ -156,5 +167,4 @@ class Shadows extends Output {
return prevResult;
});
}

}
8 changes: 6 additions & 2 deletions h3d/pass/SpotShadowMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ class SpotShadowMap extends Shadows {
@:privateAccess ctx.cameraFar = lightCamera.zFar;
@:privateAccess ctx.cameraPos = lightCamera.pos;

var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("spotShadowMap", size, size, false, format);
var computingStatic = ctx.computingStatic || updateStatic;

var texture = computingStatic ? createStaticTexture() : ctx.textures.allocTarget("spotShadowMap", size, size, false, format);
if( depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed() ) {
if( depth != null ) depth.dispose();
depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
Expand All @@ -135,7 +137,7 @@ class SpotShadowMap extends Shadows {
if( blur.radius > 0 )
blur.apply(ctx, texture);

if( mode == Mixed && !ctx.computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
if( mode == Mixed && !computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
if ( staticTexture.width != texture.width )
throw "Static shadow map doesnt match dynamic shadow map";
var merge = ctx.textures.allocTarget("mergedSpotShadowMap", size, size, false, format);
Expand All @@ -151,6 +153,8 @@ class SpotShadowMap extends Shadows {
@:privateAccess ctx.cameraViewProj = prevViewProj;

syncShader(texture);

updateStatic = false;
}

function updateCamera(){
Expand Down

0 comments on commit 8e23d81

Please sign in to comment.