From 78ad658f0630d507365a570dd376997608c7c974 Mon Sep 17 00:00:00 2001 From: Nordup Date: Sat, 13 Jul 2024 06:14:03 +0400 Subject: [PATCH] ext_texture_format and first_frame_drawn for swapchaing copying --- app/scenes/menu_body/world.tscn | 39 +++++++++++---- app/scripts/resources/command_events.gd | 10 ++++ app/scripts/sandbox/command_sync.gd | 30 ++++++++++-- app/scripts/sandbox/render_result.gd | 64 +++++++++++-------------- app/scripts/ui/world/splash_screen.gd | 41 ++++++++++++++++ app/shaders/render_result.gdshader | 14 ++++++ godot | 2 +- 7 files changed, 151 insertions(+), 49 deletions(-) create mode 100644 app/scripts/ui/world/splash_screen.gd create mode 100644 app/shaders/render_result.gdshader diff --git a/app/scenes/menu_body/world.tscn b/app/scenes/menu_body/world.tscn index ad285ef..c872bcf 100644 --- a/app/scenes/menu_body/world.tscn +++ b/app/scenes/menu_body/world.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=29 format=3 uid="uid://kywrsqro3d5i"] +[gd_scene load_steps=32 format=3 uid="uid://kywrsqro3d5i"] [ext_resource type="Script" path="res://scripts/loading/gate_loader.gd" id="1_uxhy6"] [ext_resource type="Resource" uid="uid://b1xvdym0qh6td" path="res://resources/gate_events.res" id="2_q7cvi"] @@ -10,9 +10,11 @@ [ext_resource type="Resource" uid="uid://bo6qgr210aamc" path="res://resources/sandbox_env.tres" id="8_a6dvr"] [ext_resource type="Resource" uid="uid://crjhix0osmtnf" path="res://resources/ui_events.res" id="9_ir58h"] [ext_resource type="Script" path="res://scripts/ui/world/world_canvas.gd" id="9_ncfxj"] +[ext_resource type="Shader" path="res://shaders/render_result.gdshader" id="10_2auwe"] [ext_resource type="Texture2D" uid="uid://cykx425p6ylwr" path="res://assets/textures/background.png" id="10_23bjc"] [ext_resource type="Script" path="res://scripts/sandbox/command_sync.gd" id="10_cqo55"] [ext_resource type="Script" path="res://scripts/ui/world/world_ui.gd" id="12_jdwjt"] +[ext_resource type="Script" path="res://scripts/ui/world/splash_screen.gd" id="13_3b6nd"] [ext_resource type="Script" path="res://scripts/ui/world/gate_info.gd" id="14_8hca5"] [ext_resource type="Script" path="res://scripts/ui/world/loading_status.gd" id="14_jjuux"] [ext_resource type="FontFile" uid="uid://do40418waa8w3" path="res://assets/fonts/Inter-Regular.otf" id="15_1gk43"] @@ -23,6 +25,12 @@ [ext_resource type="Script" path="res://scripts/ui/fullscreen_animation.gd" id="18_sx3kr"] [ext_resource type="FontFile" uid="uid://bjeupg0ikc2kv" path="res://assets/fonts/Monospace.ttf" id="19_ntc6f"] +[sub_resource type="ShaderMaterial" id="ShaderMaterial_be6ex"] +resource_local_to_scene = true +shader = ExtResource("10_2auwe") +shader_parameter/ext_texture_is_bgra = null +shader_parameter/fill_alpha = null + [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_bm5nj"] corner_radius_top_left = 25 corner_radius_top_right = 25 @@ -312,25 +320,38 @@ size_flags_horizontal = 0 script = ExtResource("9_ncfxj") ui_events = ExtResource("9_ir58h") -[node name="RenderResult" type="TextureRect" parent="HBoxContainer/WorldCanvas"] -texture_filter = 1 +[node name="SplashScreen" type="TextureRect" parent="HBoxContainer/WorldCanvas"] layout_mode = 1 -anchors_preset = 14 -anchor_top = 0.5 +anchors_preset = 15 anchor_right = 1.0 -anchor_bottom = 0.5 -offset_top = -409.625 -offset_bottom = 409.681 +anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 texture = ExtResource("10_23bjc") expand_mode = 5 stretch_mode = 5 +script = ExtResource("13_3b6nd") +gate_events = ExtResource("2_q7cvi") +ui_events = ExtResource("9_ir58h") +splash_screen = ExtResource("10_23bjc") + +[node name="RenderResult" type="TextureRect" parent="HBoxContainer/WorldCanvas"] +texture_filter = 1 +material = SubResource("ShaderMaterial_be6ex") +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_top = -15.625 +offset_bottom = 15.625 +grow_horizontal = 2 +grow_vertical = 2 +expand_mode = 5 +stretch_mode = 5 script = ExtResource("5_nlg2s") gate_events = ExtResource("2_q7cvi") command_events = ExtResource("6_18mgg") ui_events = ExtResource("9_ir58h") -splash_screen = ExtResource("10_23bjc") [node name="HideOnPress" type="TextureButton" parent="HBoxContainer/WorldCanvas/RenderResult"] layout_mode = 1 diff --git a/app/scripts/resources/command_events.gd b/app/scripts/resources/command_events.gd index d0b6805..df2b158 100644 --- a/app/scripts/resources/command_events.gd +++ b/app/scripts/resources/command_events.gd @@ -2,6 +2,8 @@ extends Resource class_name CommandEvents signal send_filehandle(filehandle_path: String) +signal ext_texture_format(format: RenderingDevice.DataFormat) +signal first_frame_drawn() signal set_mouse_mode(mode: int) @@ -9,5 +11,13 @@ func send_filehandle_emit(filehandle_path: String) -> void: send_filehandle.emit(filehandle_path) +func ext_texture_format_emit(format: RenderingDevice.DataFormat) -> void: + ext_texture_format.emit(format) + + +func first_frame_drawn_emit() -> void: + first_frame_drawn.emit() + + func set_mouse_mode_emit(mode: int) -> void: set_mouse_mode.emit(mode) diff --git a/app/scripts/sandbox/command_sync.gd b/app/scripts/sandbox/command_sync.gd index c7b6952..19c4378 100644 --- a/app/scripts/sandbox/command_sync.gd +++ b/app/scripts/sandbox/command_sync.gd @@ -17,15 +17,37 @@ func _execute_function(command: Command) -> Variant: Debug.logclr("Recieved command: " + command.name + ". Args: " + str(command.args), Color.SANDY_BROWN) match command.name: "send_filehandle": - if command.args.size() != 1: Debug.logerr("Arg count should be 1"); return "" + if wrong_args_count(command, 1): return ERR_INVALID_PARAMETER command_events.send_filehandle_emit(command.args[0]) + + "ext_texture_format": + if wrong_args_count(command, 1): return ERR_INVALID_PARAMETER + command_events.ext_texture_format_emit(command.args[0]) + + "first_frame_drawn": + if wrong_args_count(command, 0): return ERR_INVALID_PARAMETER + command_events.first_frame_drawn_emit() + "set_mouse_mode": - if command.args.size() != 1: Debug.logerr("Arg count should be 1"); return "" + if wrong_args_count(command, 1): return ERR_INVALID_PARAMETER command_events.set_mouse_mode_emit(command.args[0]) + "open_gate": - if command.args.size() != 1: Debug.logerr("Arg count should be 1"); return "" + if wrong_args_count(command, 1): return ERR_INVALID_PARAMETER var url = Url.join(gate_events.current_gate_url, command.args[0]) gate_events.open_gate_emit(url) + _: Debug.logerr("Command %s not implemented" % [command.name]) - return "" + return ERR_METHOD_NOT_FOUND + + return OK + + +func wrong_args_count(command: Command, right_count: int) -> bool: + var count = command.args.size() + if count != right_count: + Debug.logerr("Command %s args count should be %d but it's %d" % [command.name, right_count, count]) + return true + + return false diff --git a/app/scripts/sandbox/render_result.gd b/app/scripts/sandbox/render_result.gd index f0737f9..300189d 100644 --- a/app/scripts/sandbox/render_result.gd +++ b/app/scripts/sandbox/render_result.gd @@ -4,9 +4,7 @@ class_name RenderResult @export var gate_events: GateEvents @export var command_events: CommandEvents @export var ui_events: UiEvents -@export var splash_screen: Texture2D -var rd: RenderingDevice var ext_texure: ExternalTexture var texture_rid: RID @@ -15,40 +13,24 @@ var texture_rid: RID func _ready() -> void: - gate_events.gate_info_loaded.connect(initialize) gate_events.gate_entered.connect(create_external_texture) command_events.send_filehandle.connect(send_filehandle) + command_events.ext_texture_format.connect(ext_texture_format) + command_events.first_frame_drawn.connect(first_frame_drawn) - # Change size - var image = resize_and_convert(splash_screen.get_image(), Image.FORMAT_RGB8) + # Create empty texture with window size + var image = Image.create(width, height, false, Image.FORMAT_RGBA8) self.texture = ImageTexture.create_from_image(image) - - -func initialize(gate: Gate, is_cached: bool) -> void: - rd = RenderingServer.get_rendering_device() - - if not is_cached: # Show thumbnail image - self.texture = create_gate_image(gate) texture_rid = RenderingServer.texture_get_rd_texture(self.texture.get_rid()) if not texture_rid.is_valid(): Debug.logerr("Cannot create ImageTexture") -func create_gate_image(gate: Gate) -> ImageTexture: - var tex = FileTools.load_external_tex(gate.image) - - var image: Image - if tex != null: image = resize_and_convert(tex.get_image(), Image.FORMAT_RGB8) - else: image = Image.create(width, height, false, Image.FORMAT_RGB8) - - return ImageTexture.create_from_image(image) - - func create_external_texture() -> void: var t_format: RDTextureFormat = RDTextureFormat.new() t_format.format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM t_format.usage_bits = RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT | \ - RenderingDevice.TEXTURE_USAGE_CAN_UPDATE_BIT + RenderingDevice.TEXTURE_USAGE_CAN_UPDATE_BIT t_format.width = width t_format.height = height t_format.depth = 1 @@ -61,26 +43,38 @@ func create_external_texture() -> void: await get_tree().process_frame ext_texure = ExternalTexture.new() - var image = resize_and_convert(splash_screen.get_image(), Image.FORMAT_RGBA8) - var err = ext_texure.create(t_format, t_view, [image.get_data()]) + var err = ext_texure.create(t_format, t_view) if err: Debug.logerr("Cannot create external texture") else: Debug.logclr("External texture created", Color.AQUAMARINE) -func resize_and_convert(image: Image, format: Image.Format) -> Image: - image.resize(width, height) - image.convert(format) - image.clear_mipmaps() - return image - - func send_filehandle(filehandle_path: String) -> void: - Debug.logr("Sending filehandle...") + Debug.logclr("Sending filehandle...", Color.DIM_GRAY) var sent = false while not sent: sent = ext_texure.send_filehandle(filehandle_path) - await get_tree().create_timer(0.1).timeout - Debug.logr("filehandle was sent") + await get_tree().process_frame + Debug.logclr("filehandle was sent", Color.DIM_GRAY) + + +func ext_texture_format(format: RenderingDevice.DataFormat) -> void: + match format: + RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM: + set_param("ext_texture_is_bgra", false) + Debug.logclr("External texture format is set to RGBA8", Color.DIM_GRAY) + RenderingDevice.DATA_FORMAT_B8G8R8A8_UNORM: + set_param("ext_texture_is_bgra", true) + Debug.logclr("External texture format is set to BGRA8", Color.DIM_GRAY) + _: + Debug.logerr("Texture format %d is not supported" % [format]) + + +func first_frame_drawn() -> void: + set_param("fill_alpha", true) + + +func set_param(param: StringName, value: Variant) -> void: + (material as ShaderMaterial).set_shader_parameter(param, value) func _process(_delta: float) -> void: diff --git a/app/scripts/ui/world/splash_screen.gd b/app/scripts/ui/world/splash_screen.gd new file mode 100644 index 0000000..8edbccc --- /dev/null +++ b/app/scripts/ui/world/splash_screen.gd @@ -0,0 +1,41 @@ +extends TextureRect +class_name SplashScreen + +@export var gate_events: GateEvents +@export var ui_events: UiEvents +@export var splash_screen: Texture2D + +@onready var width: int = int(ui_events.current_ui_size.x) +@onready var height: int = int(ui_events.current_ui_size.y) + + +func _ready(): + gate_events.gate_info_loaded.connect(show_thumbnail) + gate_events.gate_entered.connect(show_splash_screen) + + # Change size + show_splash_screen() + + +func show_thumbnail(gate: Gate, is_cached: bool) -> void: + if is_cached: return # Resource pack is already downloaded + + var image: Image + var tex = FileTools.load_external_tex(gate.image) + + if tex != null: image = resize_and_convert(tex.get_image(), Image.FORMAT_RGB8) + else: image = Image.create(width, height, false, Image.FORMAT_RGB8) + + self.texture = ImageTexture.create_from_image(image) + + +func show_splash_screen() -> void: + var image = resize_and_convert(splash_screen.get_image(), Image.FORMAT_RGBA8) + self.texture = ImageTexture.create_from_image(image) + + +func resize_and_convert(image: Image, format: Image.Format) -> Image: + image.resize(width, height) + image.convert(format) + #image.clear_mipmaps() + return image diff --git a/app/shaders/render_result.gdshader b/app/shaders/render_result.gdshader new file mode 100644 index 0000000..eaa9b1a --- /dev/null +++ b/app/shaders/render_result.gdshader @@ -0,0 +1,14 @@ +shader_type canvas_item; + +uniform bool ext_texture_is_bgra; +uniform bool fill_alpha; + +void fragment() { + if (ext_texture_is_bgra) { + COLOR = COLOR.bgra; // Swizzle BGRA to RGBA + } + + if (fill_alpha) { + COLOR = vec4(COLOR.r, COLOR.g, COLOR.b, 1); + } +} diff --git a/godot b/godot index a165561..680d6e4 160000 --- a/godot +++ b/godot @@ -1 +1 @@ -Subproject commit a165561a8e7615abc82ed5d549e7788a55a44568 +Subproject commit 680d6e4ec46d37dc8fd39c53b7a83953f644c3fe