Skip to content

Commit

Permalink
Merge pull request #209 from e2002e/gi_clipmaps
Browse files Browse the repository at this point in the history
bump !
  • Loading branch information
luboslenco committed Apr 10, 2024
2 parents 9760803 + 1d88efa commit 364e271
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
83 changes: 72 additions & 11 deletions Sources/iron/RenderPath.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import iron.object.Object;
import iron.object.LightObject;
import iron.object.MeshObject;
import iron.object.Uniforms;
import iron.object.Clipmap;

class RenderPath {

Expand Down Expand Up @@ -65,15 +66,39 @@ class RenderPath {
var depthBuffers: Array<{name: String, format: String}> = [];
var additionalTargets: Array<kha.Canvas>;

#if rp_voxels
public var voxelized = 0;
public var onVoxelize: Void->Bool = null;
public function voxelize() { // Returns true if scene should be voxelized
if (onVoxelize != null) return onVoxelize();
#if arm_voxelgi_revox
return true;
#if (rp_voxels != "Off")
public static var pre_clear = true;
public static var res_pre_clear = true;
public static var clipmapLevel = 0;
public static var clipmaps:Array<Clipmap>;

public static inline function getVoxelRes(): Int {
#if (rp_voxelgi_resolution == 512)
return 512;
#elseif (rp_voxelgi_resolution == 256)
return 256;
#elseif (rp_voxelgi_resolution == 128)
return 128;
#elseif (rp_voxelgi_resolution == 64)
return 64;
#elseif (rp_voxelgi_resolution == 32)
return 32;
#else
return ++voxelized > 2 ? false : true;
return 0;
#end
}

public static inline function getVoxelResZ(): Float {
#if (rp_voxelgi_resolution_z == 1.0)
return 1.0;
#elseif (rp_voxelgi_resolution_z == 0.5)
return 0.5;
#elseif (rp_voxelgi_resolution_z == 0.25)
return 0.25;
#elseif (rp_voxelgi_resolution_z == 0.125)
return 0.125;
#else
return 0.0;
#end
}
#end
Expand Down Expand Up @@ -112,6 +137,35 @@ class RenderPath {
numTrisShadow = 0;
#end

#if (rp_voxels != "Off")
clipmapLevel = (clipmapLevel + 1) % Main.voxelgiClipmapCount;
var clipmap = clipmaps[clipmapLevel];

clipmap.voxelSize = clipmaps[0].voxelSize * Math.pow(2.0, clipmapLevel);

var texelSize = 2.0 * clipmap.voxelSize;
var camera = iron.Scene.active.camera;
var center = new iron.math.Vec3(
Math.floor(camera.transform.worldx() / texelSize) * texelSize,
Math.floor(camera.transform.worldy() / texelSize) * texelSize,
Math.floor(camera.transform.worldz() / texelSize) * texelSize
);

clipmap.offset_prev.x = Std.int((clipmap.center.x - center.x) / texelSize);
clipmap.offset_prev.y = Std.int((clipmap.center.y - center.y) / texelSize);
clipmap.offset_prev.z = Std.int((clipmap.center.z - center.z) / texelSize);
clipmap.center = center;

var res = getVoxelRes();
var resZ = getVoxelResZ();
var extents = new iron.math.Vec3(clipmap.voxelSize * res, clipmap.voxelSize * res, clipmap.voxelSize * resZ);
if (clipmap.extents.x != extents.x || clipmap.extents.y != extents.y || clipmap.extents.z != extents.z)
{
pre_clear = true;
}
clipmap.extents = extents;
#end

// Render to screen or probe
var cam = Scene.active.camera;
isProbePlanar = cam != null && cam.renderTarget != null;
Expand Down Expand Up @@ -517,7 +571,8 @@ class RenderPath {
if (rt == null ||
rt.raw.width > 0 ||
rt.depthStencilFrom == "" ||
rt == depthToRenderTarget.get(rt.depthStencilFrom)) {
rt == depthToRenderTarget.get(rt.depthStencilFrom) ||
rt.raw.is_image == true) {
continue;
}

Expand All @@ -526,7 +581,8 @@ class RenderPath {
if (rt2 == null ||
rt2.raw.width > 0 ||
rt2.depthStencilFrom != "" ||
depthToRenderTarget.get(rt2.raw.depth_buffer) != null) {
depthToRenderTarget.get(rt2.raw.depth_buffer) != null ||
rt2.raw.is_image == true) {
continue;
}

Expand All @@ -553,6 +609,10 @@ class RenderPath {
rt.image.setDepthStencilFrom(depthToRenderTarget.get(rt.depthStencilFrom).image);
}
}

#if (rp_voxels != "Off")
res_pre_clear = true;
#end
}

public function createRenderTarget(t: RenderTargetRaw): RenderTarget {
Expand Down Expand Up @@ -630,7 +690,8 @@ class RenderPath {
// Image only
var img = Image.create3D(width, height, depth,
t.format != null ? getTextureFormat(t.format) : TextureFormat.RGBA32);
if (t.mipmaps) img.generateMipmaps(1000); // Allocate mipmaps
if (t.mipmaps)
img.generateMipmaps(1000); // Allocate mipmaps
return img;
}
else { // 2D texture
Expand Down
3 changes: 0 additions & 3 deletions Sources/iron/Scene.hx
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ class Scene {
Data.getSceneRaw(sceneName, function(format: TSceneFormat) {
Scene.create(format, function(o: Object) {
if (done != null) done(o);
#if rp_voxels // Revoxelize
RenderPath.active.voxelized = 0;
#end

#if (rp_background == "World")
if (removeWorldShader != null) {
Expand Down
10 changes: 10 additions & 0 deletions Sources/iron/object/Clipmap.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package iron.object;

class Clipmap {
public var voxelSize = 0.125;
public var extents:iron.math.Vec3;
public var center:iron.math.Vec3;
public var offset_prev:iron.math.Vec3;

public function new() {};
}
4 changes: 0 additions & 4 deletions Sources/iron/object/MeshObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ class MeshObject extends Object {
if (skip_context == context) return setCulled(isShadow, true);
if (force_context != null && force_context != context) return setCulled(isShadow, true);

#if (!arm_voxelgi_revox) // No revox - do not voxelize moving objects
if (context == "voxel" && raw != null && raw.mobile == true) return setCulled(isShadow, true);
#end

return setCulled(isShadow, false);
}

Expand Down
9 changes: 7 additions & 2 deletions Sources/iron/object/Uniforms.hx
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,14 @@ class Uniforms {
}

if (isImage) {
g.setImageTexture(context.textureUnits[j], rt.image); // image2D/3D
// Multiple voxel volumes, always set params
g.setTexture3DParameters(context.textureUnits[j], TextureAddressing.Clamp, TextureAddressing.Clamp, TextureAddressing.Clamp, TextureFilter.LinearFilter, TextureFilter.PointFilter, MipMapFilter.LinearMipFilter);
g.setImageTexture(context.textureUnits[j], rt.image); // image2D/3D
if (rt.raw.depth <= 1) {
g.setTextureParameters(context.textureUnits[j], TextureAddressing.Clamp, TextureAddressing.Clamp, TextureFilter.LinearFilter, TextureFilter.LinearFilter, MipMapFilter.NoMipFilter);
}
else {
g.setTexture3DParameters(context.textureUnits[j], TextureAddressing.Mirror, TextureAddressing.Mirror, TextureAddressing.Mirror, TextureFilter.LinearFilter, TextureFilter.LinearFilter, MipMapFilter.LinearMipFilter);
}
paramsSet = true;
}
else if (rt.isCubeMap) {
Expand Down

0 comments on commit 364e271

Please sign in to comment.