Do it from ground up like normal
by copying a tutorial/demo
This commit is contained in:
144
slime_simulation.gd
Normal file
144
slime_simulation.gd
Normal file
@@ -0,0 +1,144 @@
|
||||
extends Node2D
|
||||
|
||||
@export var texture_size: Vector2i = Vector2i(512, 512)
|
||||
@export var decay_factor: float = 0.98
|
||||
|
||||
@onready var mesh := $MeshInstance2D
|
||||
|
||||
var texture: Texture2DRD
|
||||
var next_texture: int = 0
|
||||
|
||||
|
||||
func _ready():
|
||||
RenderingServer.call_on_render_thread(_initialize_compute_code.bind(texture_size))
|
||||
var mat: ShaderMaterial = mesh.material
|
||||
texture = mat.get_shader_parameter("trail_tex")
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
next_texture = (next_texture + 1) % 3
|
||||
|
||||
if texture:
|
||||
texture.texture_rd_rid = texture_rds[next_texture]
|
||||
|
||||
RenderingServer.call_on_render_thread(
|
||||
_render_process.bind(next_texture, texture_size, decay_factor)
|
||||
)
|
||||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
if texture:
|
||||
texture.texture_rd_id = RID()
|
||||
|
||||
RenderingServer.call_on_render_thread(_free_compute_resources)
|
||||
|
||||
|
||||
# Everything after this point is designed to run on our rendering thread.
|
||||
|
||||
var rd: RenderingDevice
|
||||
|
||||
var shader: RID
|
||||
var pipeline: RID
|
||||
|
||||
# We use 3 textures:
|
||||
# - One to render into
|
||||
# - One that contains the last frame rendered
|
||||
# - One for the frame before that
|
||||
var texture_rds: Array[RID] = [RID(), RID(), RID()]
|
||||
var texture_sets: Array[RID] = [RID(), RID(), RID()]
|
||||
|
||||
|
||||
func _load_shader(rd_: RenderingDevice, path: String) -> RID:
|
||||
var shader_file := load(path)
|
||||
var shader_spirv: RDShaderSPIRV = shader_file.get_spirv()
|
||||
return rd_.shader_create_from_spirv(shader_spirv)
|
||||
|
||||
|
||||
func _create_uniform_set(texture_rd: RID) -> RID:
|
||||
var uniform := RDUniform.new()
|
||||
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_IMAGE
|
||||
uniform.binding = 0
|
||||
uniform.add_id(texture_rd)
|
||||
# Even though we're using 3 sets, they are identical, so we're kinda cheating.
|
||||
return rd.uniform_set_create([uniform], shader, 0)
|
||||
|
||||
|
||||
func _initialize_compute_code(init_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()
|
||||
|
||||
# Create shader
|
||||
shader = _load_shader(rd, "res://shaders/decay_cs.glsl")
|
||||
pipeline = rd.compute_pipeline_create(shader)
|
||||
|
||||
# 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.depth = 1
|
||||
tf.array_layers = 1
|
||||
tf.mipmaps = 1
|
||||
tf.usage_bits = (
|
||||
RenderingDevice.TEXTURE_USAGE_SAMPLING_BIT
|
||||
| RenderingDevice.TEXTURE_USAGE_COLOR_ATTACHMENT_BIT
|
||||
| RenderingDevice.TEXTURE_USAGE_STORAGE_BIT
|
||||
| RenderingDevice.TEXTURE_USAGE_CAN_UPDATE_BIT
|
||||
| RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT
|
||||
)
|
||||
|
||||
for i in 3:
|
||||
texture_rds[i] = rd.texture_create(tf, RDTextureView.new(), [])
|
||||
|
||||
# Make sure our textures are cleared.
|
||||
rd.texture_clear(texture_rds[i], Color.WHITE, 0, 1, 0, 1)
|
||||
|
||||
# Now create our uniform set so we can use these textures in our shader.
|
||||
texture_sets[i] = _create_uniform_set(texture_rds[i])
|
||||
|
||||
|
||||
func _render_process(with_next_texture: int, tex_size: Vector2i, decay_fctr: float) -> void:
|
||||
var push_constant := PackedFloat32Array()
|
||||
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) # not sure if this is needed
|
||||
|
||||
@warning_ignore("integer_division")
|
||||
var x_groups := (tex_size.x - 1) / 8 + 1
|
||||
@warning_ignore("integer_division")
|
||||
var y_groups := (tex_size.y - 1) / 8 + 1
|
||||
|
||||
var next_set := texture_sets[with_next_texture]
|
||||
var current_set := texture_sets[(with_next_texture - 1) % 3]
|
||||
var previous_set := texture_sets[(with_next_texture - 2) % 3]
|
||||
|
||||
# Run the compute shader
|
||||
var compute_list := rd.compute_list_begin()
|
||||
rd.compute_list_bind_compute_pipeline(compute_list, pipeline)
|
||||
rd.compute_list_bind_uniform_set(compute_list, current_set, 0)
|
||||
rd.compute_list_bind_uniform_set(compute_list, previous_set, 1)
|
||||
rd.compute_list_bind_uniform_set(compute_list, next_set, 2)
|
||||
rd.compute_list_set_push_constant(
|
||||
compute_list, push_constant.to_byte_array(), push_constant.size() * 4
|
||||
)
|
||||
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:
|
||||
if texture_rds[i]:
|
||||
rd.free_rid(texture_rds[i])
|
||||
|
||||
if shader:
|
||||
rd.free_rid(shader)
|
||||
Reference in New Issue
Block a user