Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions acerola_shader_compiler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var compute_shader_file_paths : Array = Array()
var rd : RenderingDevice

var shader_compilations = {}
var shader_code_cache = {}
var shader_compilation_time = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be better named shader_modified_time or raw_shader_modified_time. It's possible the shader may be modified while this is not running, making modification time != compilation time.


var compute_shader_kernel_compilations = {}

Expand Down Expand Up @@ -45,7 +45,6 @@ func compile_shader(shader_file_path) -> void:
rd.free_rid(shader_compilations[shader_name])

var shader_code = FileAccess.open(shader_file_path, FileAccess.READ).get_as_text()
shader_code_cache[shader_name] = shader_code

var shader_compilation = RID()

Expand All @@ -66,6 +65,7 @@ func compile_shader(shader_file_path) -> void:
return

shader_compilations[shader_name] = shader_compilation
shader_compilation_time[shader_file_path] = FileAccess.get_modified_time(shader_file_path)


func compile_compute_shader(compute_shader_file_path) -> void:
Expand All @@ -75,7 +75,7 @@ func compile_compute_shader(compute_shader_file_path) -> void:

var file = FileAccess.open(compute_shader_file_path, FileAccess.READ)
var raw_shader_code_string = file.get_as_text()
shader_code_cache[compute_shader_file_path] = raw_shader_code_string
shader_compilation_time[compute_shader_file_path] = FileAccess.get_modified_time(compute_shader_file_path)

var raw_shader_code = raw_shader_code_string.split("\n")

Expand Down Expand Up @@ -200,7 +200,7 @@ func _init() -> void:
func _process(delta: float) -> void:
# Compare current shader code with cached shader code and recompile if changed
for file_path in compute_shader_file_paths:
if shader_code_cache[file_path] != FileAccess.open(file_path, FileAccess.READ).get_as_text():
if shader_compilation_time[file_path] != FileAccess.get_modified_time(file_path):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep the file content check after the modification time check? Would still cut down time considerably, but also avoids edge cases—for example, an autosaving editor writing the exact same content to the file.

Don't have the stats, but I'm fairly certain compilation is much more expensive in almost all cases than reading the file contents so it's best avoided if possible. Compilations should be infrequent anyway so the negligible increase in cost of also reading the file contents as well should be okay.

var shader_name = get_shader_name(file_path)

# Free existing kernels
Expand Down