From ab7568ccc5a1faf933421458c6da74ac57aeb4be Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Sat, 28 Mar 2020 09:50:51 +0100 Subject: [PATCH] Updated renderer to generate 16 bits float images, more histogram optimizations --- addons/material_maker/engine/gen_buffer.gd | 4 ++ addons/material_maker/engine/gen_material.gd | 14 ++++-- addons/material_maker/engine/renderer.gd | 43 ++----------------- addons/material_maker/engine/renderer.tscn | 1 - material_maker/nodes/switch.gd | 6 --- material_maker/nodes/switch.tscn | 13 +++--- material_maker/nodes/tones.gd | 13 +++++- material_maker/preview/preview_2d.gd | 2 +- material_maker/widgets/histogram/histogram.gd | 38 ++++++++++++---- .../widgets/histogram/histogram.tscn | 10 ++--- 10 files changed, 71 insertions(+), 73 deletions(-) diff --git a/addons/material_maker/engine/gen_buffer.gd b/addons/material_maker/engine/gen_buffer.gd index b28fe59..04750f6 100644 --- a/addons/material_maker/engine/gen_buffer.gd +++ b/addons/material_maker/engine/gen_buffer.gd @@ -56,11 +56,15 @@ func update_shader() -> void: update_buffer() func on_float_parameters_changed(parameter_changes : Dictionary) -> void: + var do_update : bool = false for n in parameter_changes.keys(): for p in VisualServer.shader_get_param_list(material.shader.get_rid()): if p.name == n: material.set_shader_param(n, parameter_changes[n]) + do_update = true break + if do_update: + update_buffer() func on_texture_changed(n : String) -> void: for p in VisualServer.shader_get_param_list(material.shader.get_rid()): diff --git a/addons/material_maker/engine/gen_material.gd b/addons/material_maker/engine/gen_material.gd index 747163d..7d57fd0 100644 --- a/addons/material_maker/engine/gen_material.gd +++ b/addons/material_maker/engine/gen_material.gd @@ -103,7 +103,7 @@ func render_textures() -> void: if source.has("textures"): for k in source.textures.keys(): 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: generated_textures[t.texture] = null need_update[t.texture] = false @@ -124,6 +124,7 @@ func render_textures() -> void: need_update[t.texture] = false func on_float_parameters_changed(parameter_changes : Dictionary) -> void: + var do_update : bool = false for t in TEXTURE_LIST: if generated_textures[t.texture] != null: for n in parameter_changes.keys(): @@ -131,17 +132,22 @@ func on_float_parameters_changed(parameter_changes : Dictionary) -> void: if p.name == n: shader_materials[t.texture].set_shader_param(n, parameter_changes[n]) need_render[t.texture] = true - update_textures() + do_update = true break + if do_update: + update_textures() func on_texture_changed(n : String) -> void: + var do_update : bool = false for t in TEXTURE_LIST: if generated_textures[t.texture] != null: for p in VisualServer.shader_get_param_list(shader_materials[t.texture].shader.get_rid()): if p.name == n: need_render[t.texture] = true - update_textures() + do_update = true break + if do_update: + update_textures() func update_textures() -> void: update_again = true @@ -152,7 +158,7 @@ func update_textures() -> void: update_again = false for t in TEXTURE_LIST: 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: result = yield(result, "completed") result.copy_to_texture(generated_textures[t.texture]) diff --git a/addons/material_maker/engine/renderer.gd b/addons/material_maker/engine/renderer.gd index 7100562..7676244 100644 --- a/addons/material_maker/engine/renderer.gd +++ b/addons/material_maker/engine/renderer.gd @@ -43,51 +43,12 @@ func generate_shader(src_code) -> String: code += shader_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: for k in textures.keys(): shader_material.set_shader_param(k+"_tex", textures[k]) 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: yield(self, "done") rendering = true @@ -96,6 +57,7 @@ func render_material(material, render_size) -> Object: $ColorRect.rect_position = Vector2(0, 0) $ColorRect.rect_size = size $ColorRect.material = material + hdr = with_hdr render_target_update_mode = Viewport.UPDATE_ONCE update_worlds() yield(get_tree(), "idle_frame") @@ -145,4 +107,5 @@ func save_to_file(fn : String) -> void: func release() -> void: rendering = false + hdr = false emit_signal("done") diff --git a/addons/material_maker/engine/renderer.tscn b/addons/material_maker/engine/renderer.tscn index e87b3b7..d3c1f22 100644 --- a/addons/material_maker/engine/renderer.tscn +++ b/addons/material_maker/engine/renderer.tscn @@ -18,7 +18,6 @@ own_world = true transparent_bg = true hdr = false keep_3d_linear = true -usage = 0 render_target_v_flip = true render_target_update_mode = 1 gui_disable_input = true diff --git a/material_maker/nodes/switch.gd b/material_maker/nodes/switch.gd index 2b73482..7e2ddf5 100644 --- a/material_maker/nodes/switch.gd +++ b/material_maker/nodes/switch.gd @@ -5,12 +5,6 @@ var fixed_lines : int = 0 func _ready() -> void: 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: if generator == null or !generator.parameters.has("outputs") or !generator.parameters.has("choices"): return diff --git a/material_maker/nodes/switch.tscn b/material_maker/nodes/switch.tscn index c6e7ee6..f332a05 100644 --- a/material_maker/nodes/switch.tscn +++ b/material_maker/nodes/switch.tscn @@ -3,8 +3,6 @@ [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] - - [sub_resource type="Theme" id=1] [node name="Switch" type="GraphNode"] @@ -36,6 +34,9 @@ slot/2/right_enabled = false slot/2/right_type = 0 slot/2/right_color = Color( 1, 1, 1, 1 ) script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} [node name="HBox1" type="HBoxContainer" parent="."] margin_left = 16.0 @@ -69,9 +70,9 @@ margin_bottom = 16.0 [node name="HBox2" type="HBoxContainer" parent="."] margin_left = 16.0 -margin_top = 40.0 +margin_top = 41.0 margin_right = 135.0 -margin_bottom = 56.0 +margin_bottom = 57.0 [node name="Label" type="Label" parent="HBox2"] margin_top = 1.0 @@ -99,9 +100,9 @@ margin_bottom = 16.0 [node name="HBox3" type="HBoxContainer" parent="."] margin_left = 16.0 -margin_top = 57.0 +margin_top = 58.0 margin_right = 135.0 -margin_bottom = 73.0 +margin_bottom = 74.0 [node name="Label" type="Label" parent="HBox3"] margin_top = 1.0 diff --git a/material_maker/nodes/tones.gd b/material_maker/nodes/tones.gd index 88e0b0d..a3036dc 100644 --- a/material_maker/nodes/tones.gd +++ b/material_maker/nodes/tones.gd @@ -1,4 +1,4 @@ -extends MMGraphNodeBase +extends MMGraphNodeGeneric class Cursor: extends Control @@ -69,10 +69,19 @@ func _ready() -> void: cursor_out_max = Cursor.new(Color(1.0, 1.0, 1.0), 1.0, false) $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: if p == "__input_changed__": 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: var value = generator.get_parameter(n) diff --git a/material_maker/preview/preview_2d.gd b/material_maker/preview/preview_2d.gd index 7249611..ab0f98d 100644 --- a/material_maker/preview/preview_2d.gd +++ b/material_maker/preview/preview_2d.gd @@ -41,7 +41,7 @@ func set_generator(g : MMGenBase, o : int = 0) -> void: material.set_shader_param(k, source.textures[k]) func on_parameter_changed(n : String, v) -> void: - if n == "__input_changed__": + if n == "__output_changed__" and output == v: set_generator(generator, output) var p = generator.get_parameter_def(n) if p.has("type"): diff --git a/material_maker/widgets/histogram/histogram.gd b/material_maker/widgets/histogram/histogram.gd index 8ff3fd8..4b6b867 100644 --- a/material_maker/widgets/histogram/histogram.gd +++ b/material_maker/widgets/histogram/histogram.gd @@ -3,7 +3,8 @@ extends Control var generator : MMGenBase = null var output : int = 0 -onready var image : ColorRect = $ViewportImage/ColorRect +var updating : bool = false +var update_again : bool = false func get_image_texture() -> ImageTexture: return $ViewportImage/ColorRect.material.get_shader_param("tex") @@ -29,16 +30,16 @@ func set_generator(g : MMGenBase, o : int = 0) -> void: if source.empty(): source = { defs="", code="", textures={}, type="f", f="1.0" } # 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 var regex = RegEx.new() regex.compile("uniform\\s+(\\w+)\\s+([\\w_\\d]+)\\s*=\\s*([^;]+);") - for p in regex.search_all(image.material.shader.code): - image.material.set_shader_param(p.strings[2], float(p.strings[3])) + for p in regex.search_all($ViewportImage/ColorRect.material.shader.code): + $ViewportImage/ColorRect.material.set_shader_param(p.strings[2], float(p.strings[3])) # Set texture params if source.has("textures"): 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() 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: var need_update : bool = false 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: - image.material.set_shader_param(n, parameter_changes[n]) + $ViewportImage/ColorRect.material.set_shader_param(n, parameter_changes[n]) need_update = true break if need_update: update_histogram() 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() diff --git a/material_maker/widgets/histogram/histogram.tscn b/material_maker/widgets/histogram/histogram.tscn index 063f52c..8dc5569 100644 --- a/material_maker/widgets/histogram/histogram.tscn +++ b/material_maker/widgets/histogram/histogram.tscn @@ -112,7 +112,7 @@ transparent_bg = true hdr = false usage = 0 render_target_v_flip = true -render_target_update_mode = 3 +render_target_update_mode = 1 [node name="ColorRect" type="ColorRect" parent="ViewportImage"] material = SubResource( 3 ) @@ -124,10 +124,10 @@ rect_min_size = Vector2( 256, 256 ) size = Vector2( 128, 128 ) own_world = true transparent_bg = true -hdr = false -usage = 0 +disable_3d = true +keep_3d_linear = true render_target_v_flip = true -render_target_update_mode = 3 +render_target_update_mode = 1 [node name="ColorRect" type="ColorRect" parent="ViewportHistogram1"] material = SubResource( 6 ) @@ -140,7 +140,6 @@ size = Vector2( 128, 2 ) transparent_bg = true disable_3d = true keep_3d_linear = true -usage = 0 render_target_v_flip = true render_target_update_mode = 3 @@ -157,3 +156,4 @@ anchor_bottom = 1.0 __meta__ = { "_edit_use_anchors_": false } +[connection signal="visibility_changed" from="." to="." method="_on_Histogram_visibility_changed"]