armory/Sources/armory/renderpath/RenderPathCreator.hx
N8n5h 1c3e24a8fd Add support for shadow map atlasing
With this it is now possible to enable atlasing of shadow maps, which solves the existing limitation of 4 lights in a scene. This is done by
grouping the rendering of shadow maps, that currently are drawn into their own images for each light, into one or several big textures. This was
done because the openGL and webGL version Armory targets do not support dynamic indexing of shadowMapSamplers, meaning that the index that
access an array of shadow maps has to be know by the compiler before hand so it can be unrolled into if/else branching. By instead simply
using a big shadow map texture and moving the dynamic part to other types of array that are allowed dynamic indexing like vec4 and mat4, this
limitation was solved.

The premise was simple enough for the shader part, but for the Haxe part, managing and solving where lights shadow maps should go in a shadow map
can be tricky. So to keep track and solve this, ShadowMapAtlas and ShadowMapTile were created. These classes have the minimally required logic
to solve the basic features needed for this problem: defining some kind of abstraction to prevent overlapping of shadowmaps, finding available
space, assigning such space efficiently, locking and freeing this space, etc. This functionality it is used by drawShadowMapAtlas(), which is a
modified version of drawShadowMap().

Shadow map atlases are represented with perfectly balanced 4-ary trees, where each tree of the previous definition represents a "tile" or slice
that results from dividing a square that represents the image into 4 slices or sub-images. The root of this "tile" it's a reference to the
tile-slice, and this tile is divided in 4 slices, and the process is repeated depth-times. If depth is 1, slices are kept at just the initial
4 tiles of max size, which is the default size of the shadow map. #arm_shadowmap_atlas_lod allows controlling if code to support more depth
levels is added or not when compiling.

the tiles that populate atlases tile trees are simply a data structure that contains a reference to the light they are linked to, inner
subtiles in case LOD is enabled, coordinates to where this tile starts in the atlas that go from 0 to Shadow Map Size, and a reference to a
linked tile for LOD. This simple definition allows tiles having a theoretically small memory footprint, but in turn this simplicity might make
some functionality that might be responsibility of tiles (for example knowing if they are overlapping) a responsibility of the ones that
utilizes tiles instead. This decision may complicate maintenance so it is to be revised in future iterations of this feature.
2021-02-04 17:53:59 -03:00

66 lines
1.8 KiB
Haxe

// Reference: https://github.com/armory3d/armory_docs/blob/master/dev/renderpath.md
package armory.renderpath;
import iron.RenderPath;
class RenderPathCreator {
public static var path: RenderPath;
public static var commands: Void->Void = function() {};
#if (rp_renderer == "Forward")
public static var setTargetMeshes: Void->Void = RenderPathForward.setTargetMeshes;
public static var drawMeshes: Void->Void = RenderPathForward.drawMeshes;
public static var applyConfig: Void->Void = RenderPathForward.applyConfig;
#elseif (rp_renderer == "Deferred")
public static var setTargetMeshes: Void->Void = RenderPathDeferred.setTargetMeshes;
public static var drawMeshes: Void->Void = RenderPathDeferred.drawMeshes;
public static var applyConfig: Void->Void = RenderPathDeferred.applyConfig;
#else
public static var setTargetMeshes: Void->Void = function() {};
public static var drawMeshes: Void->Void = function() {};
public static var applyConfig: Void->Void = function() {};
#end
public static function get(): RenderPath {
path = new RenderPath();
Inc.init(path);
#if rp_pp
iron.App.notifyOnInit(function() {
Postprocess.init();
});
#end
#if (rp_renderer == "Forward")
RenderPathForward.init(path);
path.commands = function() {
RenderPathForward.commands();
commands();
}
#elseif (rp_renderer == "Deferred")
RenderPathDeferred.init(path);
path.commands = function() {
RenderPathDeferred.commands();
commands();
}
#elseif (rp_renderer == "Raytracer")
RenderPathRaytracer.init(path);
path.commands = function() {
RenderPathRaytracer.commands();
commands();
}
#end
return path;
}
#if rp_voxelao
public static var voxelFrame = 0;
public static var voxelFreq = 6; // Revoxelizing frequency
#end
// Last target before drawing to framebuffer
public static var finalTarget: RenderTarget = null;
}