ext_texture_format and first_frame_drawn for swapchaing copying

This commit is contained in:
Nordup 2024-07-13 06:14:03 +04:00
parent 644a8bb969
commit 78ad658f06
7 changed files with 151 additions and 49 deletions

View file

@ -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="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"] [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://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="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="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="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/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/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/gate_info.gd" id="14_8hca5"]
[ext_resource type="Script" path="res://scripts/ui/world/loading_status.gd" id="14_jjuux"] [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"] [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="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"] [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"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_bm5nj"]
corner_radius_top_left = 25 corner_radius_top_left = 25
corner_radius_top_right = 25 corner_radius_top_right = 25
@ -312,25 +320,38 @@ size_flags_horizontal = 0
script = ExtResource("9_ncfxj") script = ExtResource("9_ncfxj")
ui_events = ExtResource("9_ir58h") ui_events = ExtResource("9_ir58h")
[node name="RenderResult" type="TextureRect" parent="HBoxContainer/WorldCanvas"] [node name="SplashScreen" type="TextureRect" parent="HBoxContainer/WorldCanvas"]
texture_filter = 1
layout_mode = 1 layout_mode = 1
anchors_preset = 14 anchors_preset = 15
anchor_top = 0.5
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 0.5 anchor_bottom = 1.0
offset_top = -409.625
offset_bottom = 409.681
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
texture = ExtResource("10_23bjc") texture = ExtResource("10_23bjc")
expand_mode = 5 expand_mode = 5
stretch_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") script = ExtResource("5_nlg2s")
gate_events = ExtResource("2_q7cvi") gate_events = ExtResource("2_q7cvi")
command_events = ExtResource("6_18mgg") command_events = ExtResource("6_18mgg")
ui_events = ExtResource("9_ir58h") ui_events = ExtResource("9_ir58h")
splash_screen = ExtResource("10_23bjc")
[node name="HideOnPress" type="TextureButton" parent="HBoxContainer/WorldCanvas/RenderResult"] [node name="HideOnPress" type="TextureButton" parent="HBoxContainer/WorldCanvas/RenderResult"]
layout_mode = 1 layout_mode = 1

View file

@ -2,6 +2,8 @@ extends Resource
class_name CommandEvents class_name CommandEvents
signal send_filehandle(filehandle_path: String) signal send_filehandle(filehandle_path: String)
signal ext_texture_format(format: RenderingDevice.DataFormat)
signal first_frame_drawn()
signal set_mouse_mode(mode: int) signal set_mouse_mode(mode: int)
@ -9,5 +11,13 @@ func send_filehandle_emit(filehandle_path: String) -> void:
send_filehandle.emit(filehandle_path) 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: func set_mouse_mode_emit(mode: int) -> void:
set_mouse_mode.emit(mode) set_mouse_mode.emit(mode)

View file

@ -17,15 +17,37 @@ func _execute_function(command: Command) -> Variant:
Debug.logclr("Recieved command: " + command.name + ". Args: " + str(command.args), Color.SANDY_BROWN) Debug.logclr("Recieved command: " + command.name + ". Args: " + str(command.args), Color.SANDY_BROWN)
match command.name: match command.name:
"send_filehandle": "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]) 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": "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]) command_events.set_mouse_mode_emit(command.args[0])
"open_gate": "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]) var url = Url.join(gate_events.current_gate_url, command.args[0])
gate_events.open_gate_emit(url) gate_events.open_gate_emit(url)
_: _:
Debug.logerr("Command %s not implemented" % [command.name]) 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

View file

@ -4,9 +4,7 @@ class_name RenderResult
@export var gate_events: GateEvents @export var gate_events: GateEvents
@export var command_events: CommandEvents @export var command_events: CommandEvents
@export var ui_events: UiEvents @export var ui_events: UiEvents
@export var splash_screen: Texture2D
var rd: RenderingDevice
var ext_texure: ExternalTexture var ext_texure: ExternalTexture
var texture_rid: RID var texture_rid: RID
@ -15,40 +13,24 @@ var texture_rid: RID
func _ready() -> void: func _ready() -> void:
gate_events.gate_info_loaded.connect(initialize)
gate_events.gate_entered.connect(create_external_texture) gate_events.gate_entered.connect(create_external_texture)
command_events.send_filehandle.connect(send_filehandle) 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 # Create empty texture with window size
var image = resize_and_convert(splash_screen.get_image(), Image.FORMAT_RGB8) var image = Image.create(width, height, false, Image.FORMAT_RGBA8)
self.texture = ImageTexture.create_from_image(image) 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()) texture_rid = RenderingServer.texture_get_rd_texture(self.texture.get_rid())
if not texture_rid.is_valid(): Debug.logerr("Cannot create ImageTexture") 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: func create_external_texture() -> void:
var t_format: RDTextureFormat = RDTextureFormat.new() var t_format: RDTextureFormat = RDTextureFormat.new()
t_format.format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM t_format.format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM
t_format.usage_bits = RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT | \ 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.width = width
t_format.height = height t_format.height = height
t_format.depth = 1 t_format.depth = 1
@ -61,26 +43,38 @@ func create_external_texture() -> void:
await get_tree().process_frame await get_tree().process_frame
ext_texure = ExternalTexture.new() 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)
var err = ext_texure.create(t_format, t_view, [image.get_data()])
if err: Debug.logerr("Cannot create external texture") if err: Debug.logerr("Cannot create external texture")
else: Debug.logclr("External texture created", Color.AQUAMARINE) 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: func send_filehandle(filehandle_path: String) -> void:
Debug.logr("Sending filehandle...") Debug.logclr("Sending filehandle...", Color.DIM_GRAY)
var sent = false var sent = false
while not sent: while not sent:
sent = ext_texure.send_filehandle(filehandle_path) sent = ext_texure.send_filehandle(filehandle_path)
await get_tree().create_timer(0.1).timeout await get_tree().process_frame
Debug.logr("filehandle was sent") 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: func _process(_delta: float) -> void:

View file

@ -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

View file

@ -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);
}
}

2
godot

@ -1 +1 @@
Subproject commit a165561a8e7615abc82ed5d549e7788a55a44568 Subproject commit 680d6e4ec46d37dc8fd39c53b7a83953f644c3fe