Skip to content

Commit

Permalink
Add cull and lightmap probe influence benchmarks (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
  • Loading branch information
OverloadedOrama and Calinou authored Jun 19, 2024
1 parent f1ee9eb commit 23461ea
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 37 deletions.
145 changes: 108 additions & 37 deletions benchmarks/rendering/culling.gd
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
extends Benchmark

const NUMBER_OF_OBJECTS = 10_000
const NUMBER_OF_OMNI_LIGHTS = 100
const NUMBER_OF_OBJECTS := 10_000
const NUMBER_OF_OMNI_LIGHTS := 100


func _init() -> void:
test_render_cpu = true
test_render_gpu = true


class TestScene extends Node3D:
var objects := []
var object_xforms := []
var lights := []
var light_instances := []
var light_instance_xforms := []
var meshes := []
class TestScene:
extends Node3D
var objects: Array[RID] = []
var object_xforms: Array[Transform3D] = []
var lights: Array[RID] = []
var light_instances: Array[RID] = []
var light_instance_xforms: Array[Transform3D] = []
var meshes: Array[PrimitiveMesh] = []
var cam := Camera3D.new()
var dynamic_instances
var dynamic_instances_xforms
var dynamic_instances: Array[RID]
var dynamic_instances_xforms: Array[Transform3D]
var dynamic_instances_rotate := false
var time_accum := 0.0
var unshaded := false
var use_shadows := false
var fill_with_objects := false
var fill_with_omni_lights := false
var fill_with_spot_lights := false

func _init():
func _init() -> void:
cam.far = 200.0
cam.transform.origin.z = 0.873609
add_child(cam)

func _ready():
func _ready() -> void:
cam.make_current()
if fill_with_objects:
do_fill_with_objects()
if fill_with_omni_lights:
do_fill_with_omni_lights()
if fill_with_spot_lights:
do_fill_with_spot_lights()

func do_fill_with_objects() -> void:
meshes.append(BoxMesh.new())
Expand All @@ -45,18 +51,21 @@ class TestScene extends Node3D:

for mesh in meshes:
var shader := Shader.new()
var shader_string := """
var shader_string := (
"""
shader_type spatial;
%s
void fragment() {
ALBEDO = vec3(%s, %s, %s);
}
""" % [
"""
% [
"render_mode unshaded;\n" if unshaded else "",
str(randf()).pad_decimals(3),
str(randf()).pad_decimals(3),
str(randf()).pad_decimals(3),
]
)
shader.code = shader_string
var material := ShaderMaterial.new()
material.shader = shader
Expand All @@ -70,7 +79,11 @@ class TestScene extends Node3D:

for i in NUMBER_OF_OBJECTS:
var xf := Transform3D()
xf.origin = Vector3(from.x + randf() * extents.x,from.y + randf() * extents.y, - (zn + zextent * randf()))
xf.origin = Vector3(
from.x + randf() * extents.x,
from.y + randf() * extents.y,
-(zn + zextent * randf())
)
var ins := RenderingServer.instance_create()
RenderingServer.instance_set_base(ins, meshes[i % meshes.size()].get_rid())
RenderingServer.instance_set_scenario(ins, get_world_3d().scenario)
Expand All @@ -80,26 +93,43 @@ class TestScene extends Node3D:
object_xforms.append(xf)

func do_fill_with_omni_lights() -> void:
do_fill_with_lights(true)

func do_fill_with_spot_lights() -> void:
do_fill_with_lights(false)

func do_fill_with_lights(omni: bool) -> void:
var zn := 2
var zextent := cam.far - zn
var ss := get_tree().root.get_visible_rect().size
var from := cam.project_position(Vector2(0,ss.y),zextent)
var extents := cam.project_position(Vector2(ss.x,0),zextent) - from
var from := cam.project_position(Vector2(0, ss.y), zextent)
var extents := cam.project_position(Vector2(ss.x, 0), zextent) - from

var light := RenderingServer.omni_light_create()
RenderingServer.light_set_param(light, RenderingServer.LIGHT_PARAM_RANGE,10)
var light: RID
if omni:
light = RenderingServer.omni_light_create()
# Dual paraboloid shadows are faster than cubemap shadows.
RenderingServer.light_omni_set_shadow_mode(
light, RenderingServer.LIGHT_OMNI_SHADOW_DUAL_PARABOLOID
)
else:
light = RenderingServer.spot_light_create()
RenderingServer.light_set_param(light, RenderingServer.LIGHT_PARAM_RANGE, 10)
RenderingServer.light_set_shadow(light, use_shadows)
# Dual parabolid shadows are faster than cubemap shadows.
RenderingServer.light_omni_set_shadow_mode(light, RenderingServer.LIGHT_OMNI_SHADOW_DUAL_PARABOLOID)
lights.append(light)

for i in NUMBER_OF_OMNI_LIGHTS:
var xf := Transform3D()
xf.origin = Vector3(from.x + randf() * extents.x,from.y + randf() * extents.y, - (zn + zextent * randf()))
xf.origin = Vector3(
from.x + randf() * extents.x,
from.y + randf() * extents.y,
-(zn + zextent * randf())
)

var ins := RenderingServer.instance_create()
RenderingServer.instance_set_base(ins, light)
RenderingServer.instance_set_scenario(ins,get_world_3d().scenario)
RenderingServer.instance_set_transform(ins,xf)
RenderingServer.instance_set_scenario(ins, get_world_3d().scenario)
RenderingServer.instance_set_transform(ins, xf)

light_instances.append(ins)
light_instance_xforms.append(xf)
Expand All @@ -123,19 +153,40 @@ class TestScene extends Node3D:
time_accum += delta * 4.0

for i in dynamic_instances.size():
var xf = dynamic_instances_xforms[i]
var angle = i * PI * 2.0 / dynamic_instances.size()
xf.origin += Vector3(sin(angle), cos(angle), 0.0) * sin(time_accum) * 2.0
var xf := dynamic_instances_xforms[i]
var angle := i * PI * 2.0 / dynamic_instances.size()
if dynamic_instances_rotate:
xf = xf.rotated_local(Vector3.RIGHT, angle * sin(time_accum) * 2.0)
else:
xf.origin += Vector3(sin(angle), cos(angle), 0.0) * sin(time_accum) * 2.0
RenderingServer.instance_set_transform(dynamic_instances[i], xf)


func benchmark_basic_cull():
func benchmark_basic_cull() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.unshaded = true
return rv

func benchmark_directional_light_cull():

func benchmark_dynamic_cull() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.dynamic_instances = rv.objects
rv.dynamic_instances_xforms = rv.object_xforms
return rv


func benchmark_dynamic_rotate_cull() -> TestScene:
var rv := TestScene.new()
rv.dynamic_instances_rotate = true
rv.fill_with_objects = true
rv.dynamic_instances = rv.objects
rv.dynamic_instances_xforms = rv.object_xforms
return rv


func benchmark_directional_light_cull() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
var light := DirectionalLight3D.new()
Expand All @@ -145,22 +196,32 @@ func benchmark_directional_light_cull():
rv.add_child(light)
return rv

func benchmark_dynamic_cull():

func benchmark_static_omni_light_cull() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.dynamic_instances = rv.objects
rv.dynamic_instances_xforms = rv.object_xforms
rv.fill_with_omni_lights = true
return rv


func benchmark_static_omni_light_cull_with_shadows() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.fill_with_omni_lights = true
rv.use_shadows = true
return rv

func benchmark_dynamic_light_cull():

func benchmark_dynamic_omni_light_cull() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.fill_with_omni_lights = true
rv.dynamic_instances = rv.light_instances
rv.dynamic_instances_xforms = rv.light_instance_xforms
return rv

func benchmark_dynamic_light_cull_with_shadows():

func benchmark_dynamic_omni_light_cull_with_shadows() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.fill_with_omni_lights = true
Expand All @@ -169,10 +230,20 @@ func benchmark_dynamic_light_cull_with_shadows():
rv.dynamic_instances_xforms = rv.light_instance_xforms
return rv

func benchmark_static_light_cull():

func benchmark_static_spot_light_cull_with_shadows() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.fill_with_omni_lights = true
rv.fill_with_spot_lights = true
rv.use_shadows = true
return rv


func benchmark_dynamic_spot_light_cull_with_shadows() -> TestScene:
var rv := TestScene.new()
rv.fill_with_objects = true
rv.fill_with_spot_lights = true
rv.use_shadows = true
rv.dynamic_instances = rv.light_instances
rv.dynamic_instances_xforms = rv.light_instance_xforms
return rv
57 changes: 57 additions & 0 deletions benchmarks/rendering/lightmap_probe_influence.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extends Benchmark

const NUMBER_OF_OBJECTS := 1000
const NUMBER_OF_PROBES := 500


func _init() -> void:
test_render_cpu = true
test_render_gpu = true


class TestScene:
extends Node3D

var sponza_scene := preload("res://supplemental/sponza.tscn")
var sponza: Node
var time_accum := 0.0
var objects: Array[MeshInstance3D]

func _init() -> void:
sponza = sponza_scene.instantiate()
add_child(sponza)

func _ready() -> void:
$"Sponza/OmniLights".visible = false
var lightmap := LightmapGI.new()
lightmap.light_data = load("res://supplemental/sponza.lmbake")
$Sponza.add_child(lightmap)

var meshes: Array[PrimitiveMesh] = [
BoxMesh.new(),
SphereMesh.new(),
CapsuleMesh.new(),
CylinderMesh.new(),
PrismMesh.new(),
]
var half_objects := NUMBER_OF_OBJECTS / 2
for i in NUMBER_OF_OBJECTS:
var ins := MeshInstance3D.new()
ins.gi_mode = GeometryInstance3D.GI_MODE_DYNAMIC
ins.mesh = meshes[i % meshes.size()]
ins.position.y = 1
ins.position.z = i - half_objects
$Sponza.add_child(ins)
objects.append(ins)

func _process(delta: float) -> void:
time_accum += delta * 2.0
for i in objects.size():
objects[i].position.x = sin(time_accum) * 6.0

func _exit_tree() -> void:
RenderingServer.free_rid(sponza)


func benchmark_lightmap_probe_influence() -> TestScene:
return TestScene.new()

0 comments on commit 23461ea

Please sign in to comment.