first it worked, then i broke it and finally remembered to commit
This commit is contained in:
@@ -2,9 +2,18 @@ extends Node2D
|
||||
|
||||
@export var texture_size: Vector2i = Vector2i(512, 512)
|
||||
@export var decay_factor: float = 0.98
|
||||
@export var dissipation_factor: float = 0.12
|
||||
@export var sensor_angle: float = 32.0:
|
||||
get():
|
||||
return deg_to_rad(sensor_angle)
|
||||
@export var sensor_distance: float = 3.86
|
||||
@export var rng_seed: int = 80085
|
||||
|
||||
@onready var mesh := $MeshInstance2D
|
||||
|
||||
## Needs to be changed from the shader as well
|
||||
const AGENTS: int = 4096
|
||||
|
||||
var texture: Texture2DRD
|
||||
var next_texture: int = 0
|
||||
|
||||
@@ -22,7 +31,7 @@ func _process(_delta: float) -> void:
|
||||
texture.texture_rd_rid = texture_rds[next_texture]
|
||||
|
||||
RenderingServer.call_on_render_thread(
|
||||
_render_process.bind(next_texture, texture_size, decay_factor)
|
||||
_render_process.bind(next_texture, texture_size, decay_factor, dissipation_factor, sensor_angle, sensor_distance)
|
||||
)
|
||||
|
||||
|
||||
@@ -50,9 +59,8 @@ var agent_pipeline: RID
|
||||
var texture_rds: Array[RID] = [RID(), RID()]
|
||||
var texture_sets: Array[RID] = [RID(), RID()]
|
||||
|
||||
var agent_uniform_sets: Array[RID] = [RID(), RID()]
|
||||
var agent_buffer: RID
|
||||
var agent_set: RID
|
||||
var agent_sets: Array[RID] = [RID(), RID()]
|
||||
|
||||
|
||||
func _load_shader(rd_: RenderingDevice, path: String) -> RID:
|
||||
@@ -69,7 +77,7 @@ func _create_trail_uniform(texture_rd: RID) -> RDUniform:
|
||||
return uniform
|
||||
|
||||
|
||||
func _initialize_compute_code(init_with_texture_size: Vector2i) -> void:
|
||||
func _initialize_compute_code(with_texture_size: Vector2i) -> void:
|
||||
# As this becomes part of our normal frame rendering,
|
||||
# we use our main rendering device here.
|
||||
rd = RenderingServer.get_rendering_device()
|
||||
@@ -83,26 +91,31 @@ func _initialize_compute_code(init_with_texture_size: Vector2i) -> void:
|
||||
|
||||
# Create agent buffer
|
||||
var rng := RandomNumberGenerator.new()
|
||||
rng.seed = 80085
|
||||
var agent_data := PackedVector2Array()
|
||||
for n in 64 * 16:
|
||||
var new_position: Vector2i = Vector2(
|
||||
rng.randi_range(0, init_with_texture_size.x),
|
||||
rng.randi_range(0, init_with_texture_size.y)
|
||||
rng.seed = rng_seed
|
||||
var agent_positions := PackedVector2Array()
|
||||
var agent_angles := PackedFloat32Array()
|
||||
# This must match what is set in the shader, if fixed size
|
||||
for n in AGENTS:
|
||||
var new_position: Vector2 = Vector2(
|
||||
rng.randf_range(0, with_texture_size.x),
|
||||
rng.randf_range(0, with_texture_size.y)
|
||||
)
|
||||
agent_data.push_back(new_position)
|
||||
agent_positions.push_back(new_position)
|
||||
agent_angles.push_back(rng.randf_range(0, TAU))
|
||||
|
||||
var agent_size_in_bytes := 8
|
||||
var agent_data := PackedByteArray(agent_positions.to_byte_array())
|
||||
agent_data.append_array(agent_angles.to_byte_array())
|
||||
agent_buffer = rd.storage_buffer_create(
|
||||
agent_data.size() * agent_size_in_bytes, agent_data.to_byte_array()
|
||||
agent_data.size(), agent_data, 0,
|
||||
RenderingDevice.BUFFER_CREATION_AS_STORAGE_BIT
|
||||
)
|
||||
|
||||
# Create trail texture
|
||||
var tf: RDTextureFormat = RDTextureFormat.new()
|
||||
tf.format = RenderingDevice.DATA_FORMAT_R32_SFLOAT
|
||||
tf.texture_type = RenderingDevice.TEXTURE_TYPE_2D
|
||||
tf.width = init_with_texture_size.x
|
||||
tf.height = init_with_texture_size.y
|
||||
tf.width = with_texture_size.x
|
||||
tf.height = with_texture_size.y
|
||||
tf.depth = 1
|
||||
tf.array_layers = 1
|
||||
tf.mipmaps = 1
|
||||
@@ -115,7 +128,7 @@ func _initialize_compute_code(init_with_texture_size: Vector2i) -> void:
|
||||
)
|
||||
|
||||
|
||||
var agent_uniforms = []
|
||||
var agent_uniforms: Array[RDUniform] = []
|
||||
for i in 2:
|
||||
texture_rds[i] = rd.texture_create(tf, RDTextureView.new(), [])
|
||||
# Make sure our textures are cleared.
|
||||
@@ -123,17 +136,28 @@ func _initialize_compute_code(init_with_texture_size: Vector2i) -> void:
|
||||
|
||||
var trail_uniform := _create_trail_uniform(texture_rds[i])
|
||||
texture_sets[i] = rd.uniform_set_create([trail_uniform], decay_shader, 0)
|
||||
agent_uniform_sets[i] = rd.uniform_set_create([trail_uniform], agent_shader, 0)
|
||||
|
||||
var txtr_uniform_1 := RDUniform.new()
|
||||
txtr_uniform_1.uniform_type = RenderingDevice.UNIFORM_TYPE_IMAGE
|
||||
txtr_uniform_1.binding = 0
|
||||
txtr_uniform_1.add_id(texture_rds[0])
|
||||
agent_uniforms.append(txtr_uniform_1)
|
||||
|
||||
var txtr_uniform_2 := RDUniform.new()
|
||||
txtr_uniform_2.uniform_type = RenderingDevice.UNIFORM_TYPE_IMAGE
|
||||
txtr_uniform_2.binding = 1
|
||||
txtr_uniform_2.add_id(texture_rds[1])
|
||||
agent_uniforms.append(txtr_uniform_2)
|
||||
|
||||
var uniform := RDUniform.new()
|
||||
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER
|
||||
uniform.binding = 1
|
||||
uniform.binding = 2
|
||||
uniform.add_id(agent_buffer)
|
||||
agent_set = rd.uniform_set_create([uniform], agent_shader, 0)
|
||||
agent_sets[0] = rd.uniform_set_create([txtr_uniform_1, txtr_uniform_2, uniform], agent_shader, 0)
|
||||
agent_sets[1] = rd.uniform_set_create([txtr_uniform_2, txtr_uniform_1, uniform], agent_shader, 0)
|
||||
|
||||
|
||||
func _render_process(with_next_texture: int, tex_size: Vector2i, decay_fctr: float) -> void:
|
||||
func _render_process(with_next_texture: int, tex_size: Vector2i, decay_fctr: float, dissipation_fctr: float, snsr_angle: float, snsr_distance: float) -> void:
|
||||
var next_set := texture_sets[with_next_texture]
|
||||
var current_set := texture_sets[(with_next_texture - 1) % 2]
|
||||
|
||||
@@ -141,19 +165,18 @@ func _render_process(with_next_texture: int, tex_size: Vector2i, decay_fctr: flo
|
||||
var push_constant := PackedFloat32Array()
|
||||
push_constant.push_back(tex_size.x)
|
||||
push_constant.push_back(tex_size.y)
|
||||
push_constant.push_back(0.0)
|
||||
push_constant.push_back(0.0)
|
||||
push_constant.push_back(snsr_angle)
|
||||
push_constant.push_back(snsr_distance)
|
||||
|
||||
|
||||
var agent_compute_list := rd.compute_list_begin()
|
||||
rd.compute_list_bind_compute_pipeline(agent_compute_list, agent_pipeline)
|
||||
rd.compute_list_bind_uniform_set(agent_compute_list, current_set, 0)
|
||||
rd.compute_list_bind_uniform_set(agent_compute_list, next_set, 1)
|
||||
rd.compute_list_bind_uniform_set(agent_compute_list, agent_set, 2)
|
||||
rd.compute_list_bind_uniform_set(agent_compute_list, agent_sets[with_next_texture], 0)
|
||||
rd.compute_list_set_push_constant(
|
||||
agent_compute_list, push_constant.to_byte_array(), push_constant.size() * 4
|
||||
)
|
||||
rd.compute_list_dispatch(agent_compute_list, 16, 1, 1)
|
||||
@warning_ignore("integer_division")
|
||||
rd.compute_list_dispatch(agent_compute_list, 64, 1, 1)
|
||||
rd.compute_list_end()
|
||||
|
||||
# Decay and dissipate
|
||||
@@ -161,7 +184,7 @@ func _render_process(with_next_texture: int, tex_size: Vector2i, decay_fctr: flo
|
||||
push_constant.push_back(tex_size.x)
|
||||
push_constant.push_back(tex_size.y)
|
||||
push_constant.push_back(decay_fctr)
|
||||
push_constant.push_back(0.0)
|
||||
push_constant.push_back(dissipation_fctr)
|
||||
|
||||
@warning_ignore("integer_division")
|
||||
var x_groups := (tex_size.x - 1) / 8 + 1
|
||||
@@ -179,18 +202,15 @@ func _render_process(with_next_texture: int, tex_size: Vector2i, decay_fctr: flo
|
||||
rd.compute_list_dispatch(compute_list, x_groups, y_groups, 1)
|
||||
rd.compute_list_end()
|
||||
|
||||
# We don't need to sync up here, Godots default barriers will do the trick.
|
||||
# If you want the output of a compute shader to be used as input of
|
||||
# another computer shader you'll need to add a barrier:
|
||||
#rd.barrier(RenderingDevice.BARRIER_MASK_COMPUTE)
|
||||
|
||||
|
||||
func _free_compute_resources() -> void:
|
||||
# Note that our sets and pipeline are cleaned up automatically
|
||||
# as they are dependencies :P
|
||||
for i in 3:
|
||||
for i in 2:
|
||||
if texture_rds[i]:
|
||||
rd.free_rid(texture_rds[i])
|
||||
|
||||
if decay_shader:
|
||||
rd.free_rid(decay_shader)
|
||||
if agent_shader:
|
||||
rd.free_rid(agent_shader)
|
||||
|
||||
Reference in New Issue
Block a user