Updated renderer to generate 16 bits float images, more histogram optimizations

This commit is contained in:
RodZill4 2020-03-28 09:50:51 +01:00
parent 29048fc307
commit ab7568ccc5
10 changed files with 71 additions and 73 deletions

View File

@ -56,11 +56,15 @@ func update_shader() -> void:
update_buffer() update_buffer()
func on_float_parameters_changed(parameter_changes : Dictionary) -> void: func on_float_parameters_changed(parameter_changes : Dictionary) -> void:
var do_update : bool = false
for n in parameter_changes.keys(): for n in parameter_changes.keys():
for p in VisualServer.shader_get_param_list(material.shader.get_rid()): for p in VisualServer.shader_get_param_list(material.shader.get_rid()):
if p.name == n: if p.name == n:
material.set_shader_param(n, parameter_changes[n]) material.set_shader_param(n, parameter_changes[n])
do_update = true
break break
if do_update:
update_buffer()
func on_texture_changed(n : String) -> void: func on_texture_changed(n : String) -> void:
for p in VisualServer.shader_get_param_list(material.shader.get_rid()): for p in VisualServer.shader_get_param_list(material.shader.get_rid()):

View File

@ -103,7 +103,7 @@ func render_textures() -> void:
if source.has("textures"): if source.has("textures"):
for k in source.textures.keys(): for k in source.textures.keys():
shader_materials[t.texture].set_shader_param(k, source.textures[k]) shader_materials[t.texture].set_shader_param(k, source.textures[k])
result = mm_renderer.render_material(shader_materials[t.texture], get_image_size()) result = mm_renderer.render_material(shader_materials[t.texture], get_image_size(), false)
else: else:
generated_textures[t.texture] = null generated_textures[t.texture] = null
need_update[t.texture] = false need_update[t.texture] = false
@ -124,6 +124,7 @@ func render_textures() -> void:
need_update[t.texture] = false need_update[t.texture] = false
func on_float_parameters_changed(parameter_changes : Dictionary) -> void: func on_float_parameters_changed(parameter_changes : Dictionary) -> void:
var do_update : bool = false
for t in TEXTURE_LIST: for t in TEXTURE_LIST:
if generated_textures[t.texture] != null: if generated_textures[t.texture] != null:
for n in parameter_changes.keys(): for n in parameter_changes.keys():
@ -131,17 +132,22 @@ func on_float_parameters_changed(parameter_changes : Dictionary) -> void:
if p.name == n: if p.name == n:
shader_materials[t.texture].set_shader_param(n, parameter_changes[n]) shader_materials[t.texture].set_shader_param(n, parameter_changes[n])
need_render[t.texture] = true need_render[t.texture] = true
update_textures() do_update = true
break break
if do_update:
update_textures()
func on_texture_changed(n : String) -> void: func on_texture_changed(n : String) -> void:
var do_update : bool = false
for t in TEXTURE_LIST: for t in TEXTURE_LIST:
if generated_textures[t.texture] != null: if generated_textures[t.texture] != null:
for p in VisualServer.shader_get_param_list(shader_materials[t.texture].shader.get_rid()): for p in VisualServer.shader_get_param_list(shader_materials[t.texture].shader.get_rid()):
if p.name == n: if p.name == n:
need_render[t.texture] = true need_render[t.texture] = true
update_textures() do_update = true
break break
if do_update:
update_textures()
func update_textures() -> void: func update_textures() -> void:
update_again = true update_again = true
@ -152,7 +158,7 @@ func update_textures() -> void:
update_again = false update_again = false
for t in TEXTURE_LIST: for t in TEXTURE_LIST:
if need_render[t.texture]: if need_render[t.texture]:
var result = mm_renderer.render_material(shader_materials[t.texture], image_size) var result = mm_renderer.render_material(shader_materials[t.texture], image_size, false)
while result is GDScriptFunctionState: while result is GDScriptFunctionState:
result = yield(result, "completed") result = yield(result, "completed")
result.copy_to_texture(generated_textures[t.texture]) result.copy_to_texture(generated_textures[t.texture])

View File

@ -43,51 +43,12 @@ func generate_shader(src_code) -> String:
code += shader_code code += shader_code
return code return code
func generate_combined_shader(red_code, green_code, blue_code) -> String:
var code
code = "shader_type canvas_item;\n"
code += "render_mode blend_disabled;\n"
code += common_shader
code += "\n"
var globals = []
var textures = {}
var output = []
for c in [ red_code, green_code, blue_code ]:
if c.has("textures"):
for t in c.textures.keys():
textures[t] = c.textures[t]
if c.has("globals"):
for g in c.globals:
if globals.find(g) == -1:
globals.push_back(g)
if c.has("f"):
output.push_back(c.f)
else:
output.push_back("1.0")
for t in textures.keys():
code += "uniform sampler2D "+t+";\n"
for g in globals:
code += g
var shader_code = ""
shader_code += red_code.defs
shader_code += green_code.defs
shader_code += blue_code.defs
shader_code += "void fragment() {\n"
shader_code += red_code.code
shader_code += green_code.code
shader_code += blue_code.code
shader_code += "COLOR = vec4("+output[0]+", "+output[1]+", "+output[2]+", 1.0);\n"
shader_code += "}\n"
#print("GENERATED COMBINED SHADER:\n"+shader_code)
code += shader_code
return code
func setup_material(shader_material, textures, shader_code) -> void: func setup_material(shader_material, textures, shader_code) -> void:
for k in textures.keys(): for k in textures.keys():
shader_material.set_shader_param(k+"_tex", textures[k]) shader_material.set_shader_param(k+"_tex", textures[k])
shader_material.shader.code = shader_code shader_material.shader.code = shader_code
func render_material(material, render_size) -> Object: func render_material(material, render_size, with_hdr = true) -> Object:
while rendering: while rendering:
yield(self, "done") yield(self, "done")
rendering = true rendering = true
@ -96,6 +57,7 @@ func render_material(material, render_size) -> Object:
$ColorRect.rect_position = Vector2(0, 0) $ColorRect.rect_position = Vector2(0, 0)
$ColorRect.rect_size = size $ColorRect.rect_size = size
$ColorRect.material = material $ColorRect.material = material
hdr = with_hdr
render_target_update_mode = Viewport.UPDATE_ONCE render_target_update_mode = Viewport.UPDATE_ONCE
update_worlds() update_worlds()
yield(get_tree(), "idle_frame") yield(get_tree(), "idle_frame")
@ -145,4 +107,5 @@ func save_to_file(fn : String) -> void:
func release() -> void: func release() -> void:
rendering = false rendering = false
hdr = false
emit_signal("done") emit_signal("done")

View File

@ -18,7 +18,6 @@ own_world = true
transparent_bg = true transparent_bg = true
hdr = false hdr = false
keep_3d_linear = true keep_3d_linear = true
usage = 0
render_target_v_flip = true render_target_v_flip = true
render_target_update_mode = 1 render_target_update_mode = 1
gui_disable_input = true gui_disable_input = true

View File

@ -5,12 +5,6 @@ var fixed_lines : int = 0
func _ready() -> void: func _ready() -> void:
update_node() update_node()
func update_preview_buttons(index : int) -> void:
for i in range(generator.parameters.outputs):
if i != index:
var line = get_child(i)
line.get_child(2).pressed = false
func update_node() -> void: func update_node() -> void:
if generator == null or !generator.parameters.has("outputs") or !generator.parameters.has("choices"): if generator == null or !generator.parameters.has("outputs") or !generator.parameters.has("choices"):
return return

View File

@ -3,8 +3,6 @@
[ext_resource path="res://material_maker/nodes/switch.gd" type="Script" id=1] [ext_resource path="res://material_maker/nodes/switch.gd" type="Script" id=1]
[ext_resource path="res://material_maker/widgets/preview_button.tscn" type="PackedScene" id=2] [ext_resource path="res://material_maker/widgets/preview_button.tscn" type="PackedScene" id=2]
[sub_resource type="Theme" id=1] [sub_resource type="Theme" id=1]
[node name="Switch" type="GraphNode"] [node name="Switch" type="GraphNode"]
@ -36,6 +34,9 @@ slot/2/right_enabled = false
slot/2/right_type = 0 slot/2/right_type = 0
slot/2/right_color = Color( 1, 1, 1, 1 ) slot/2/right_color = Color( 1, 1, 1, 1 )
script = ExtResource( 1 ) script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="HBox1" type="HBoxContainer" parent="."] [node name="HBox1" type="HBoxContainer" parent="."]
margin_left = 16.0 margin_left = 16.0
@ -69,9 +70,9 @@ margin_bottom = 16.0
[node name="HBox2" type="HBoxContainer" parent="."] [node name="HBox2" type="HBoxContainer" parent="."]
margin_left = 16.0 margin_left = 16.0
margin_top = 40.0 margin_top = 41.0
margin_right = 135.0 margin_right = 135.0
margin_bottom = 56.0 margin_bottom = 57.0
[node name="Label" type="Label" parent="HBox2"] [node name="Label" type="Label" parent="HBox2"]
margin_top = 1.0 margin_top = 1.0
@ -99,9 +100,9 @@ margin_bottom = 16.0
[node name="HBox3" type="HBoxContainer" parent="."] [node name="HBox3" type="HBoxContainer" parent="."]
margin_left = 16.0 margin_left = 16.0
margin_top = 57.0 margin_top = 58.0
margin_right = 135.0 margin_right = 135.0
margin_bottom = 73.0 margin_bottom = 74.0
[node name="Label" type="Label" parent="HBox3"] [node name="Label" type="Label" parent="HBox3"]
margin_top = 1.0 margin_top = 1.0

View File

@ -1,4 +1,4 @@
extends MMGraphNodeBase extends MMGraphNodeGeneric
class Cursor: class Cursor:
extends Control extends Control
@ -69,10 +69,19 @@ func _ready() -> void:
cursor_out_max = Cursor.new(Color(1.0, 1.0, 1.0), 1.0, false) cursor_out_max = Cursor.new(Color(1.0, 1.0, 1.0), 1.0, false)
$Histogram.add_child(cursor_out_max) $Histogram.add_child(cursor_out_max)
func update_node() -> void:
_on_Mode_item_selected(0)
on_parameter_changed("__input_changed__", 0)
# Preview
restore_preview_widget()
func on_parameter_changed(p, v) -> void: func on_parameter_changed(p, v) -> void:
if p == "__input_changed__": if p == "__input_changed__":
var source = generator.get_source(0) var source = generator.get_source(0)
$Histogram.set_generator(source.generator, source.output_index) if source != null:
$Histogram.set_generator(source.generator, source.output_index)
else:
$Histogram.set_generator(null, 0)
func get_parameter(n : String) -> float: func get_parameter(n : String) -> float:
var value = generator.get_parameter(n) var value = generator.get_parameter(n)

View File

@ -41,7 +41,7 @@ func set_generator(g : MMGenBase, o : int = 0) -> void:
material.set_shader_param(k, source.textures[k]) material.set_shader_param(k, source.textures[k])
func on_parameter_changed(n : String, v) -> void: func on_parameter_changed(n : String, v) -> void:
if n == "__input_changed__": if n == "__output_changed__" and output == v:
set_generator(generator, output) set_generator(generator, output)
var p = generator.get_parameter_def(n) var p = generator.get_parameter_def(n)
if p.has("type"): if p.has("type"):

View File

@ -3,7 +3,8 @@ extends Control
var generator : MMGenBase = null var generator : MMGenBase = null
var output : int = 0 var output : int = 0
onready var image : ColorRect = $ViewportImage/ColorRect var updating : bool = false
var update_again : bool = false
func get_image_texture() -> ImageTexture: func get_image_texture() -> ImageTexture:
return $ViewportImage/ColorRect.material.get_shader_param("tex") return $ViewportImage/ColorRect.material.get_shader_param("tex")
@ -29,16 +30,16 @@ func set_generator(g : MMGenBase, o : int = 0) -> void:
if source.empty(): if source.empty():
source = { defs="", code="", textures={}, type="f", f="1.0" } source = { defs="", code="", textures={}, type="f", f="1.0" }
# Update shader # Update shader
image.material.shader.code = MMGenBase.generate_preview_shader(source, source.type, "uniform vec2 size;void fragment() {COLOR = preview_2d(UV);}") $ViewportImage/ColorRect.material.shader.code = MMGenBase.generate_preview_shader(source, source.type, "uniform vec2 size;void fragment() {COLOR = preview_2d(UV);}")
# Get parameter values from the shader code # Get parameter values from the shader code
var regex = RegEx.new() var regex = RegEx.new()
regex.compile("uniform\\s+(\\w+)\\s+([\\w_\\d]+)\\s*=\\s*([^;]+);") regex.compile("uniform\\s+(\\w+)\\s+([\\w_\\d]+)\\s*=\\s*([^;]+);")
for p in regex.search_all(image.material.shader.code): for p in regex.search_all($ViewportImage/ColorRect.material.shader.code):
image.material.set_shader_param(p.strings[2], float(p.strings[3])) $ViewportImage/ColorRect.material.set_shader_param(p.strings[2], float(p.strings[3]))
# Set texture params # Set texture params
if source.has("textures"): if source.has("textures"):
for k in source.textures.keys(): for k in source.textures.keys():
image.material.set_shader_param(k, source.textures[k]) $ViewportImage/ColorRect.material.set_shader_param(k, source.textures[k])
update_histogram() update_histogram()
func on_parameter_changed(n : String, v) -> void: func on_parameter_changed(n : String, v) -> void:
@ -55,13 +56,34 @@ func on_parameter_changed(n : String, v) -> void:
func on_float_parameters_changed(parameter_changes : Dictionary) -> void: func on_float_parameters_changed(parameter_changes : Dictionary) -> void:
var need_update : bool = false var need_update : bool = false
for n in parameter_changes.keys(): for n in parameter_changes.keys():
for p in VisualServer.shader_get_param_list(image.material.shader.get_rid()): for p in VisualServer.shader_get_param_list($ViewportImage/ColorRect.material.shader.get_rid()):
if p.name == n: if p.name == n:
image.material.set_shader_param(n, parameter_changes[n]) $ViewportImage/ColorRect.material.set_shader_param(n, parameter_changes[n])
need_update = true need_update = true
break break
if need_update: if need_update:
update_histogram() update_histogram()
func update_histogram() -> void: func update_histogram() -> void:
pass update_again = true
if !updating && is_visible_in_tree():
updating = true
while update_again:
update_again = false
$ViewportImage.render_target_update_mode = Viewport.UPDATE_ONCE
$ViewportImage.update_worlds()
yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")
$ViewportHistogram1.render_target_update_mode = Viewport.UPDATE_ONCE
$ViewportHistogram1.update_worlds()
yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")
$ViewportHistogram2.render_target_update_mode = Viewport.UPDATE_ONCE
$ViewportHistogram2.update_worlds()
yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")
updating = false
func _on_Histogram_visibility_changed():
if is_visible_in_tree():
update_histogram()

View File

@ -112,7 +112,7 @@ transparent_bg = true
hdr = false hdr = false
usage = 0 usage = 0
render_target_v_flip = true render_target_v_flip = true
render_target_update_mode = 3 render_target_update_mode = 1
[node name="ColorRect" type="ColorRect" parent="ViewportImage"] [node name="ColorRect" type="ColorRect" parent="ViewportImage"]
material = SubResource( 3 ) material = SubResource( 3 )
@ -124,10 +124,10 @@ rect_min_size = Vector2( 256, 256 )
size = Vector2( 128, 128 ) size = Vector2( 128, 128 )
own_world = true own_world = true
transparent_bg = true transparent_bg = true
hdr = false disable_3d = true
usage = 0 keep_3d_linear = true
render_target_v_flip = true render_target_v_flip = true
render_target_update_mode = 3 render_target_update_mode = 1
[node name="ColorRect" type="ColorRect" parent="ViewportHistogram1"] [node name="ColorRect" type="ColorRect" parent="ViewportHistogram1"]
material = SubResource( 6 ) material = SubResource( 6 )
@ -140,7 +140,6 @@ size = Vector2( 128, 2 )
transparent_bg = true transparent_bg = true
disable_3d = true disable_3d = true
keep_3d_linear = true keep_3d_linear = true
usage = 0
render_target_v_flip = true render_target_v_flip = true
render_target_update_mode = 3 render_target_update_mode = 3
@ -157,3 +156,4 @@ anchor_bottom = 1.0
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[connection signal="visibility_changed" from="." to="." method="_on_Histogram_visibility_changed"]