diff options
Diffstat (limited to 'modules')
83 files changed, 3303 insertions, 1571 deletions
diff --git a/modules/etcpak/image_compress_etcpak.cpp b/modules/etcpak/image_compress_etcpak.cpp index 3d66b27556..e467ed60ee 100644 --- a/modules/etcpak/image_compress_etcpak.cpp +++ b/modules/etcpak/image_compress_etcpak.cpp @@ -111,13 +111,16 @@ void _compress_etcpak(EtcpakType p_compresstype, Image *r_img, float p_lossy_qua Image::Format target_format = Image::FORMAT_RGBA8; if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC1) { target_format = Image::FORMAT_ETC; + r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2) { target_format = Image::FORMAT_ETC2_RGB8; + r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG) { target_format = Image::FORMAT_ETC2_RA_AS_RG; r_img->convert_rg_to_ra_rgba8(); } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_ALPHA) { target_format = Image::FORMAT_ETC2_RGBA8; + r_img->convert_rgba8_to_bgra8(); // It's badly documented but ETCPAK seems to be expected BGRA8 for ETC. } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT1) { target_format = Image::FORMAT_DXT1; } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG) { diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index c8eda53a2d..4981750b7d 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -547,7 +547,7 @@ <return type="void" /> <param index="0" name="icon_path" type="String" /> <description> - Add a custom icon to the current script. After loading an icon at [param icon_path], the icon is displayed in the Scene dock for every node that the script is attached to. For named classes, the icon is also displayed in various editor dialogs. + Add a custom icon to the current script. The script must be registered as a global class using the [code]class_name[/code] keyword for this to have a visible effect. The icon specified at [param icon_path] is displayed in the Scene dock for every node of that class, as well as in various editor dialogs. [codeblock] @icon("res://path/to/class/icon.svg") [/codeblock] diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 60230257e0..7744a1d67e 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -478,7 +478,7 @@ void GDScript::_clear_doc() { void GDScript::_update_doc() { _clear_doc(); - doc.script_path = "\"" + get_path().get_slice("://", 1) + "\""; + doc.script_path = vformat(R"("%s")", get_script_path().get_slice("://", 1)); if (!name.is_empty()) { doc.name = name; } else { @@ -801,9 +801,9 @@ void GDScript::update_exports() { String GDScript::_get_debug_path() const { if (is_built_in() && !get_name().is_empty()) { - return get_name() + " (" + get_path() + ")"; + return vformat("%s(%s)", get_name(), get_script_path()); } else { - return get_path(); + return get_script_path(); } } @@ -904,7 +904,7 @@ Error GDScript::reload(bool p_keep_state) { for (const GDScriptWarning &warning : parser.get_warnings()) { if (EngineDebugger::is_active()) { Vector<ScriptLanguage::StackInfo> si; - EngineDebugger::get_script_debugger()->send_error("", get_path(), warning.start_line, warning.get_name(), warning.get_message(), false, ERR_HANDLER_WARNING, si); + EngineDebugger::get_script_debugger()->send_error("", get_script_path(), warning.start_line, warning.get_name(), warning.get_message(), false, ERR_HANDLER_WARNING, si); } } #endif @@ -1027,6 +1027,10 @@ void GDScript::set_path(const String &p_path, bool p_take_over) { } } +String GDScript::get_script_path() const { + return path; +} + Error GDScript::load_source_code(const String &p_path) { if (p_path.is_empty() || ResourceLoader::get_resource_type(p_path.get_slice("::", 0)) == "PackedScene") { return OK; @@ -2019,6 +2023,42 @@ Error GDScriptLanguage::execute_file(const String &p_path) { } void GDScriptLanguage::finish() { + if (_call_stack) { + memdelete_arr(_call_stack); + _call_stack = nullptr; + } + + // Clear the cache before parsing the script_list + GDScriptCache::clear(); + + // Clear dependencies between scripts, to ensure cyclic references are broken + // (to avoid leaks at exit). + SelfList<GDScript> *s = script_list.first(); + while (s) { + // This ensures the current script is not released before we can check + // what's the next one in the list (we can't get the next upfront because we + // don't know if the reference breaking will cause it -or any other after + // it, for that matter- to be released so the next one is not the same as + // before). + Ref<GDScript> scr = s->self(); + if (scr.is_valid()) { + for (KeyValue<StringName, GDScriptFunction *> &E : scr->member_functions) { + GDScriptFunction *func = E.value; + for (int i = 0; i < func->argument_types.size(); i++) { + func->argument_types.write[i].script_type_ref = Ref<Script>(); + } + func->return_type.script_type_ref = Ref<Script>(); + } + for (KeyValue<StringName, GDScript::MemberInfo> &E : scr->member_indices) { + E.value.data_type.script_type_ref = Ref<Script>(); + } + + // Clear backup for scripts that could slip out of the cyclic reference + // check + scr->clear(); + } + s = s->next(); + } } void GDScriptLanguage::profiling_start() { @@ -2128,7 +2168,8 @@ void GDScriptLanguage::reload_all_scripts() { SelfList<GDScript> *elem = script_list.first(); while (elem) { - if (elem->self()->get_path().is_resource_file()) { + // Scripts will reload all subclasses, so only reload root scripts. + if (elem->self()->is_root_script() && elem->self()->get_path().is_resource_file()) { print_verbose("GDScript: Found: " + elem->self()->get_path()); scripts.push_back(Ref<GDScript>(elem->self())); //cast to gdscript to avoid being erased by accident } @@ -2157,7 +2198,8 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so SelfList<GDScript> *elem = script_list.first(); while (elem) { - if (elem->self()->get_path().is_resource_file()) { + // Scripts will reload all subclasses, so only reload root scripts. + if (elem->self()->is_root_script() && elem->self()->get_path().is_resource_file()) { scripts.push_back(Ref<GDScript>(elem->self())); //cast to gdscript to avoid being erased by accident } elem = elem->next(); @@ -2530,36 +2572,6 @@ GDScriptLanguage::GDScriptLanguage() { } GDScriptLanguage::~GDScriptLanguage() { - if (_call_stack) { - memdelete_arr(_call_stack); - } - - // Clear dependencies between scripts, to ensure cyclic references are broken (to avoid leaks at exit). - SelfList<GDScript> *s = script_list.first(); - while (s) { - // This ensures the current script is not released before we can check what's the next one - // in the list (we can't get the next upfront because we don't know if the reference breaking - // will cause it -or any other after it, for that matter- to be released so the next one - // is not the same as before). - Ref<GDScript> scr = s->self(); - if (scr.is_valid()) { - for (KeyValue<StringName, GDScriptFunction *> &E : scr->member_functions) { - GDScriptFunction *func = E.value; - for (int i = 0; i < func->argument_types.size(); i++) { - func->argument_types.write[i].script_type_ref = Ref<Script>(); - } - func->return_type.script_type_ref = Ref<Script>(); - } - for (KeyValue<StringName, GDScript::MemberInfo> &E : scr->member_indices) { - E.value.data_type.script_type_ref = Ref<Script>(); - } - - // Clear backup for scripts that could slip out of the cyclic reference check - scr->clear(); - } - s = s->next(); - } - singleton = nullptr; } diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 2df89d812c..7911ea47ec 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -240,6 +240,7 @@ public: virtual Error reload(bool p_keep_state = false) override; virtual void set_path(const String &p_path, bool p_take_over = false) override; + String get_script_path() const; Error load_source_code(const String &p_path); Error load_byte_code(const String &p_path); @@ -432,7 +433,7 @@ public: csi.write[_debug_call_stack_pos - i - 1].line = _call_stack[i].line ? *_call_stack[i].line : 0; if (_call_stack[i].function) { csi.write[_debug_call_stack_pos - i - 1].func = _call_stack[i].function->get_name(); - csi.write[_debug_call_stack_pos - i - 1].file = _call_stack[i].function->get_script()->get_path(); + csi.write[_debug_call_stack_pos - i - 1].file = _call_stack[i].function->get_script()->get_script_path(); } } return csi; diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 95e577c140..85929ae6c5 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -196,8 +196,11 @@ Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::C while (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::CLASS) { GDScriptParser::ClassNode *current_class_node = current_data_type->class_type; if (has_member_name_conflict_in_script_class(p_member_name, current_class_node, p_member_node)) { - push_error(vformat(R"(The member "%s" already exists in parent class %s.)", p_member_name, current_class_node->identifier->name), - p_member_node); + String parent_class_name = current_class_node->fqcn; + if (current_class_node->identifier != nullptr) { + parent_class_name = current_class_node->identifier->name; + } + push_error(vformat(R"(The member "%s" already exists in parent class %s.)", p_member_name, parent_class_name), p_member_node); return ERR_PARSE_ERROR; } current_data_type = ¤t_class_node->base_type; @@ -542,9 +545,9 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type } else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) { Ref<GDScript> gdscript = member.constant->initializer->reduced_value; if (gdscript.is_valid()) { - Ref<GDScriptParserRef> ref = get_parser_for(gdscript->get_path()); + Ref<GDScriptParserRef> ref = get_parser_for(gdscript->get_script_path()); if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) { - push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_path()), p_type); + push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_script_path()), p_type); return GDScriptParser::DataType(); } result = ref->get_parser()->head->get_datatype(); @@ -1162,6 +1165,8 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * if (p_function->parameters[i]->default_value->is_constant) { p_function->default_arg_values.push_back(p_function->parameters[i]->default_value->reduced_value); + } else { + p_function->default_arg_values.push_back(Variant()); // Prevent shift. } } #endif // TOOLS_ENABLED @@ -1214,11 +1219,7 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * if (!valid) { // Compute parent signature as a string to show in the error message. - String parent_signature = parent_return_type.is_hard_type() ? parent_return_type.to_string() : "Variant"; - if (parent_signature == "null") { - parent_signature = "void"; - } - parent_signature += " " + p_function->identifier->name.operator String() + "("; + String parent_signature = p_function->identifier->name.operator String() + "("; int j = 0; for (const GDScriptParser::DataType &par_type : parameters_types) { if (j > 0) { @@ -1235,7 +1236,15 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode * j++; } - parent_signature += ")"; + parent_signature += ") -> "; + + const String return_type = parent_return_type.is_hard_type() ? parent_return_type.to_string() : "Variant"; + if (return_type == "null") { + parent_signature += "void"; + } else { + parent_signature += return_type; + } + push_error(vformat(R"(The function signature doesn't match the parent. Parent signature is "%s".)", parent_signature), p_function); } } @@ -2667,7 +2676,7 @@ void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) { } void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) { - HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements; + HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, StringLikeVariantComparator> elements; for (int i = 0; i < p_dictionary->elements.size(); i++) { const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i]; @@ -3127,9 +3136,9 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name]; Node *node = Object::cast_to<Node>(constant); if (node != nullptr) { - Ref<Script> scr = node->get_script(); + Ref<GDScript> scr = node->get_script(); if (scr.is_valid()) { - Ref<GDScriptParserRef> singl_parser = get_parser_for(scr->get_path()); + Ref<GDScriptParserRef> singl_parser = get_parser_for(scr->get_script_path()); if (singl_parser.is_valid()) { Error err = singl_parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED); if (err == OK) { @@ -3332,7 +3341,7 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri Ref<GDScript> gdscr = Ref<GDScript>(p_subscript->base->reduced_value); if (!valid && gdscr.is_valid()) { Error err = OK; - GDScriptCache::get_full_script(gdscr->get_path(), err); + GDScriptCache::get_full_script(gdscr->get_script_path(), err); if (err == OK) { value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid); } @@ -3423,7 +3432,7 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri case Variant::QUATERNION: case Variant::AABB: case Variant::OBJECT: - error = index_type.builtin_type != Variant::STRING; + error = index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME; break; // Expect String or number. case Variant::BASIS: @@ -3437,11 +3446,11 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri case Variant::TRANSFORM3D: case Variant::PROJECTION: error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT && - index_type.builtin_type != Variant::STRING; + index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME; break; // Expect String or int. case Variant::COLOR: - error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING; + error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME; break; // Don't support indexing, but we will check it later. case Variant::RID: @@ -4155,6 +4164,8 @@ bool GDScriptAnalyzer::is_type_compatible(const GDScriptParser::DataType &p_targ if (p_target.kind == GDScriptParser::DataType::BUILTIN) { bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type; + valid |= p_source.builtin_type == Variant::STRING && p_target.builtin_type == Variant::STRING_NAME; + valid |= p_source.builtin_type == Variant::STRING_NAME && p_target.builtin_type == Variant::STRING; if (!valid && p_allow_implicit_conversion) { valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type); } diff --git a/modules/gdscript/gdscript_byte_codegen.cpp b/modules/gdscript/gdscript_byte_codegen.cpp index fa158591fd..1bc83fbbb5 100644 --- a/modules/gdscript/gdscript_byte_codegen.cpp +++ b/modules/gdscript/gdscript_byte_codegen.cpp @@ -164,7 +164,7 @@ void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName function->name = p_function_name; function->_script = p_script; - function->source = p_script->get_path(); + function->source = p_script->get_script_path(); #ifdef DEBUG_ENABLED function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8(); diff --git a/modules/gdscript/gdscript_cache.cpp b/modules/gdscript/gdscript_cache.cpp index 021504f242..1df7757082 100644 --- a/modules/gdscript/gdscript_cache.cpp +++ b/modules/gdscript/gdscript_cache.cpp @@ -381,15 +381,15 @@ void GDScriptCache::clear_unreferenced_packed_scenes() { } } -GDScriptCache::GDScriptCache() { - singleton = this; -} +void GDScriptCache::clear() { + if (singleton == nullptr) { + return; + } -GDScriptCache::~GDScriptCache() { - destructing = true; + MutexLock lock(singleton->mutex); RBSet<Ref<GDScriptParserRef>> parser_map_refs; - for (KeyValue<String, GDScriptParserRef *> &E : parser_map) { + for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) { parser_map_refs.insert(E.value); } @@ -398,13 +398,26 @@ GDScriptCache::~GDScriptCache() { E->clear(); } + for (KeyValue<String, HashSet<String>> &E : singleton->packed_scene_dependencies) { + singleton->packed_scene_dependencies.erase(E.key); + singleton->packed_scene_cache.erase(E.key); + } + parser_map_refs.clear(); - parser_map.clear(); - shallow_gdscript_cache.clear(); - full_gdscript_cache.clear(); + singleton->parser_map.clear(); + singleton->shallow_gdscript_cache.clear(); + singleton->full_gdscript_cache.clear(); - packed_scene_cache.clear(); - packed_scene_dependencies.clear(); + singleton->packed_scene_cache.clear(); + singleton->packed_scene_dependencies.clear(); +} +GDScriptCache::GDScriptCache() { + singleton = this; +} + +GDScriptCache::~GDScriptCache() { + destructing = true; + clear(); singleton = nullptr; } diff --git a/modules/gdscript/gdscript_cache.h b/modules/gdscript/gdscript_cache.h index 2195932aa3..e7e1901d5d 100644 --- a/modules/gdscript/gdscript_cache.h +++ b/modules/gdscript/gdscript_cache.h @@ -111,6 +111,8 @@ public: return singleton->destructing; }; + static void clear(); + GDScriptCache(); ~GDScriptCache(); }; diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index 103eb60da9..c0539c7e45 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -43,7 +43,7 @@ bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringN return false; } - if (codegen.parameters.has(p_name) || codegen.locals.has(p_name)) { + if (_is_local_or_parameter(codegen, p_name)) { return false; //shadowed } @@ -65,6 +65,10 @@ bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringNa return ClassDB::has_property(nc->get_name(), p_name); } +bool GDScriptCompiler::_is_local_or_parameter(CodeGen &codegen, const StringName &p_name) { + return codegen.parameters.has(p_name) || codegen.locals.has(p_name); +} + void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::Node *p_node) { if (!error.is_empty()) { return; @@ -920,7 +924,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code StringName var_name = identifier->name; if (_is_class_member_property(codegen, var_name)) { assign_class_member_property = var_name; - } else if (!codegen.locals.has(var_name) && codegen.script->member_indices.has(var_name)) { + } else if (!_is_local_or_parameter(codegen, var_name) && codegen.script->member_indices.has(var_name)) { is_member_property = true; member_property_setter_function = codegen.script->member_indices[var_name].setter; member_property_has_setter = member_property_setter_function != StringName(); @@ -1131,7 +1135,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code bool is_in_setter = false; StringName setter_function; StringName var_name = static_cast<const GDScriptParser::IdentifierNode *>(assignment->assignee)->name; - if (!codegen.locals.has(var_name) && codegen.script->member_indices.has(var_name)) { + if (!_is_local_or_parameter(codegen, var_name) && codegen.script->member_indices.has(var_name)) { is_member = true; setter_function = codegen.script->member_indices[var_name].setter; has_setter = setter_function != StringName(); @@ -1252,9 +1256,30 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c equality_type.kind = GDScriptDataType::BUILTIN; equality_type.builtin_type = Variant::BOOL; + GDScriptCodeGenerator::Address type_string_addr = codegen.add_constant(Variant::STRING); + GDScriptCodeGenerator::Address type_string_name_addr = codegen.add_constant(Variant::STRING_NAME); + // Check type equality. GDScriptCodeGenerator::Address type_equality_addr = codegen.add_temporary(equality_type); codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_EQUAL, p_type_addr, literal_type_addr); + + // Check if StringName <-> String comparison is possible. + GDScriptCodeGenerator::Address type_comp_addr_1 = codegen.add_temporary(equality_type); + GDScriptCodeGenerator::Address type_comp_addr_2 = codegen.add_temporary(equality_type); + + codegen.generator->write_binary_operator(type_comp_addr_1, Variant::OP_EQUAL, p_type_addr, type_string_addr); + codegen.generator->write_binary_operator(type_comp_addr_2, Variant::OP_EQUAL, literal_type_addr, type_string_name_addr); + codegen.generator->write_binary_operator(type_comp_addr_1, Variant::OP_AND, type_comp_addr_1, type_comp_addr_2); + codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_OR, type_equality_addr, type_comp_addr_1); + + codegen.generator->write_binary_operator(type_comp_addr_1, Variant::OP_EQUAL, p_type_addr, type_string_name_addr); + codegen.generator->write_binary_operator(type_comp_addr_2, Variant::OP_EQUAL, literal_type_addr, type_string_addr); + codegen.generator->write_binary_operator(type_comp_addr_1, Variant::OP_AND, type_comp_addr_1, type_comp_addr_2); + codegen.generator->write_binary_operator(type_equality_addr, Variant::OP_OR, type_equality_addr, type_comp_addr_1); + + codegen.generator->pop_temporary(); // Remove type_comp_addr_2 from stack. + codegen.generator->pop_temporary(); // Remove type_comp_addr_1 from stack. + codegen.generator->write_and_left_operand(type_equality_addr); // Get literal. @@ -1875,6 +1900,7 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui GDScriptCodeGenerator::Address local = codegen.locals[lv->identifier->name]; GDScriptDataType local_type = _gdtype_from_datatype(lv->get_datatype(), codegen.script); + bool initialized = false; if (lv->initializer != nullptr) { // For typed arrays we need to make sure this is already initialized correctly so typed assignment work. if (local_type.has_type && local_type.builtin_type == Variant::ARRAY) { @@ -1896,15 +1922,23 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui if (src_address.mode == GDScriptCodeGenerator::Address::TEMPORARY) { codegen.generator->pop_temporary(); } + initialized = true; } else if (local_type.has_type) { // Initialize with default for type. if (local_type.has_container_element_type()) { codegen.generator->write_construct_typed_array(local, local_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>()); + initialized = true; } else if (local_type.kind == GDScriptDataType::BUILTIN) { codegen.generator->write_construct(local, local_type.builtin_type, Vector<GDScriptCodeGenerator::Address>()); + initialized = true; } // The `else` branch is for objects, in such case we leave it as `null`. } + + // Assigns a null for the unassigned variables in loops. + if (!initialized && p_block->is_loop) { + codegen.generator->write_construct(local, Variant::NIL, Vector<GDScriptCodeGenerator::Address>()); + } } break; case GDScriptParser::Node::CONSTANT: { // Local constants. @@ -2083,8 +2117,8 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_ if (EngineDebugger::is_active()) { String signature; // Path. - if (!p_script->get_path().is_empty()) { - signature += p_script->get_path(); + if (!p_script->get_script_path().is_empty()) { + signature += p_script->get_script_path(); } // Location. if (p_func) { diff --git a/modules/gdscript/gdscript_compiler.h b/modules/gdscript/gdscript_compiler.h index 45ca4fe342..cba585e5a5 100644 --- a/modules/gdscript/gdscript_compiler.h +++ b/modules/gdscript/gdscript_compiler.h @@ -115,6 +115,7 @@ class GDScriptCompiler { bool _is_class_member_property(CodeGen &codegen, const StringName &p_name); bool _is_class_member_property(GDScript *owner, const StringName &p_name); + bool _is_local_or_parameter(CodeGen &codegen, const StringName &p_name); void _set_error(const String &p_error, const GDScriptParser::Node *p_node); diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index c02ee99a86..693863ab38 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -1612,7 +1612,7 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, } } - if (!found) { + if (!found && base.value.get_type() != Variant::NIL) { found = _guess_method_return_type_from_base(c, base, call->function_name, r_type); } } @@ -2512,50 +2512,62 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c } static bool _get_subscript_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::SubscriptNode *p_subscript, GDScriptParser::DataType &r_base_type, Variant *r_base = nullptr) { - if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER && p_context.base != nullptr) { - const GDScriptParser::GetNodeNode *get_node = nullptr; - const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base); + if (p_context.base == nullptr) { + return false; + } + const GDScriptParser::GetNodeNode *get_node = nullptr; - switch (identifier_node->source) { - case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: { - if (p_context.current_class != nullptr) { - const StringName &member_name = identifier_node->name; - const GDScriptParser::ClassNode *current_class = p_context.current_class; + switch (p_subscript->base->type) { + case GDScriptParser::Node::GET_NODE: { + get_node = static_cast<GDScriptParser::GetNodeNode *>(p_subscript->base); + } break; - if (current_class->has_member(member_name)) { - const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name); + case GDScriptParser::Node::IDENTIFIER: { + const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base); - if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) { - const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable); + switch (identifier_node->source) { + case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: { + if (p_context.current_class != nullptr) { + const StringName &member_name = identifier_node->name; + const GDScriptParser::ClassNode *current_class = p_context.current_class; - if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) { - get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer); + if (current_class->has_member(member_name)) { + const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name); + + if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) { + const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable); + + if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) { + get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer); + } } } } - } - } break; - case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: { - if (identifier_node->next != nullptr && identifier_node->next->type == GDScriptParser::ClassNode::Node::GET_NODE) { - get_node = static_cast<GDScriptParser::GetNodeNode *>(identifier_node->next); - } - } break; - default: - break; - } + } break; + case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: { + if (identifier_node->next != nullptr && identifier_node->next->type == GDScriptParser::ClassNode::Node::GET_NODE) { + get_node = static_cast<GDScriptParser::GetNodeNode *>(identifier_node->next); + } + } break; + default: { + } break; + } + } break; + default: { + } break; + } - if (get_node != nullptr) { - const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path)); - if (node != nullptr) { - if (r_base != nullptr) { - *r_base = node; - } - r_base_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; - r_base_type.kind = GDScriptParser::DataType::NATIVE; - r_base_type.native_type = node->get_class_name(); - r_base_type.builtin_type = Variant::OBJECT; - return true; + if (get_node != nullptr) { + const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path)); + if (node != nullptr) { + if (r_base != nullptr) { + *r_base = node; } + r_base_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; + r_base_type.kind = GDScriptParser::DataType::NATIVE; + r_base_type.native_type = node->get_class_name(); + r_base_type.builtin_type = Variant::OBJECT; + return true; } } @@ -2612,7 +2624,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c } } - if (p_context.base != nullptr && subscript->is_attribute) { + if (subscript->is_attribute) { bool found_type = _get_subscript_type(p_context, subscript, base_type, &base); if (!found_type) { @@ -3276,6 +3288,7 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co parser.parse(p_code, p_path, true); GDScriptParser::CompletionContext context = parser.get_completion_context(); + context.base = p_owner; // Allows class functions with the names like built-ins to be handled properly. if (context.type != GDScriptParser::COMPLETION_ATTRIBUTE) { @@ -3448,7 +3461,9 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co break; } GDScriptCompletionIdentifier base; - if (!_guess_expression_type(context, subscript->base, base)) { + + bool found_type = _get_subscript_type(context, subscript, base.type); + if (!found_type && !_guess_expression_type(context, subscript->base, base)) { break; } diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 24dd94873b..f2aafe9f0c 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -1835,9 +1835,9 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() { } suite->add_local(SuiteNode::Local(n_for->variable, current_function)); } - suite->parent_for = n_for; n_for->loop = parse_suite(R"("for" block)", suite); + n_for->loop->is_loop = true; complete_extents(n_for); // Reset break/continue state. @@ -2169,6 +2169,7 @@ GDScriptParser::WhileNode *GDScriptParser::parse_while() { is_continue_match = false; n_while->loop = parse_suite(R"("while" block)"); + n_while->loop->is_loop = true; complete_extents(n_while); // Reset break/continue state. @@ -3785,15 +3786,14 @@ bool GDScriptParser::export_annotations(const AnnotationNode *p_annotation, Node } break; case GDScriptParser::DataType::CLASS: - // Can assume type is a global GDScript class. if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) { variable->export_info.type = Variant::OBJECT; variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE; - variable->export_info.hint_string = export_type.class_type->identifier->name; + variable->export_info.hint_string = export_type.to_string(); } else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) { variable->export_info.type = Variant::OBJECT; variable->export_info.hint = PROPERTY_HINT_NODE_TYPE; - variable->export_info.hint_string = export_type.class_type->identifier->name; + variable->export_info.hint_string = export_type.to_string(); } else { push_error(R"(Export type can only be built-in, a resource, a node or an enum.)", variable); return false; diff --git a/modules/gdscript/gdscript_parser.h b/modules/gdscript/gdscript_parser.h index f9a1c5a697..d092a2a5e9 100644 --- a/modules/gdscript/gdscript_parser.h +++ b/modules/gdscript/gdscript_parser.h @@ -1055,12 +1055,12 @@ public: HashMap<StringName, int> locals_indices; FunctionNode *parent_function = nullptr; - ForNode *parent_for = nullptr; IfNode *parent_if = nullptr; bool has_return = false; bool has_continue = false; bool has_unreachable_code = false; // Just so warnings aren't given more than once per block. + bool is_loop = false; bool has_local(const StringName &p_name) const; const Local &get_local(const StringName &p_name) const; diff --git a/modules/gdscript/gdscript_utility_functions.cpp b/modules/gdscript/gdscript_utility_functions.cpp index bcbe8b8d2b..27b6792e84 100644 --- a/modules/gdscript/gdscript_utility_functions.cpp +++ b/modules/gdscript/gdscript_utility_functions.cpp @@ -294,6 +294,7 @@ struct GDScriptUtilityFunctionsDefinitions { } GDScript *p = base.ptr(); + String path = p->get_script_path(); Vector<StringName> sname; while (p->_owner) { @@ -302,7 +303,7 @@ struct GDScriptUtilityFunctionsDefinitions { } sname.reverse(); - if (!p->path.is_resource_file()) { + if (!path.is_resource_file()) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::DICTIONARY; @@ -317,7 +318,7 @@ struct GDScriptUtilityFunctionsDefinitions { Dictionary d; d["@subpath"] = cp; - d["@path"] = p->get_path(); + d["@path"] = path; for (const KeyValue<StringName, GDScript::MemberInfo> &E : base->member_indices) { if (!d.has(E.key)) { diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp index c73ba798aa..fdcc0625d7 100644 --- a/modules/gdscript/gdscript_vm.cpp +++ b/modules/gdscript/gdscript_vm.cpp @@ -2227,7 +2227,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } #ifdef DEBUG_ENABLED gdfs->state.function_name = name; - gdfs->state.script_path = _script->get_path(); + gdfs->state.script_path = _script->get_script_path(); #endif gdfs->state.defarg = defarg; gdfs->function = this; diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index de3becbaf8..e442bf8159 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -844,8 +844,9 @@ Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { lines = p_code.split("\n"); Error err = GDScriptParser::parse(p_code, p_path, false); + GDScriptAnalyzer analyzer(this); + if (err == OK) { - GDScriptAnalyzer analyzer(this); err = analyzer.analyze(); } update_diagnostics(); diff --git a/modules/gdscript/tests/scripts/analyzer/errors/dictionary_string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/analyzer/errors/dictionary_string_stringname_equivalent.gd new file mode 100644 index 0000000000..4dd2b556ee --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/dictionary_string_stringname_equivalent.gd @@ -0,0 +1,9 @@ +# https://github.com/godotengine/godot/issues/62957 + +func test(): + var dict = { + &"key": "StringName", + "key": "String" + } + + print("Invalid dictionary: %s" % dict) diff --git a/modules/gdscript/tests/scripts/analyzer/errors/dictionary_string_stringname_equivalent.out b/modules/gdscript/tests/scripts/analyzer/errors/dictionary_string_stringname_equivalent.out new file mode 100644 index 0000000000..189d8a7955 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/dictionary_string_stringname_equivalent.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Key "key" was already used in this dictionary (at line 5). diff --git a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_less.out b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_less.out index 3baeb17066..4ccd2da381 100644 --- a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_less.out +++ b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_less.out @@ -1,2 +1,2 @@ GDTEST_ANALYZER_ERROR -The function signature doesn't match the parent. Parent signature is "int my_function(int)". +The function signature doesn't match the parent. Parent signature is "my_function(int) -> int". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_more.out b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_more.out index 3baeb17066..4ccd2da381 100644 --- a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_more.out +++ b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_count_more.out @@ -1,2 +1,2 @@ GDTEST_ANALYZER_ERROR -The function signature doesn't match the parent. Parent signature is "int my_function(int)". +The function signature doesn't match the parent. Parent signature is "my_function(int) -> int". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_default_values.out b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_default_values.out index 665c229339..c70a1df10d 100644 --- a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_default_values.out +++ b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_default_values.out @@ -1,2 +1,2 @@ GDTEST_ANALYZER_ERROR -The function signature doesn't match the parent. Parent signature is "int my_function(int = default)". +The function signature doesn't match the parent. Parent signature is "my_function(int = default) -> int". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_type.out b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_type.out index 3baeb17066..4ccd2da381 100644 --- a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_type.out +++ b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_parameter_type.out @@ -1,2 +1,2 @@ GDTEST_ANALYZER_ERROR -The function signature doesn't match the parent. Parent signature is "int my_function(int)". +The function signature doesn't match the parent. Parent signature is "my_function(int) -> int". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_return_type.out b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_return_type.out index 5b22739a93..61004ff627 100644 --- a/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_return_type.out +++ b/modules/gdscript/tests/scripts/analyzer/errors/function_dont_match_parent_signature_return_type.out @@ -1,2 +1,2 @@ GDTEST_ANALYZER_ERROR -The function signature doesn't match the parent. Parent signature is "int my_function()". +The function signature doesn't match the parent. Parent signature is "my_function() -> int". diff --git a/modules/gdscript/tests/scripts/analyzer/features/array_string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/analyzer/features/array_string_stringname_equivalent.gd new file mode 100644 index 0000000000..4511c3d10b --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/array_string_stringname_equivalent.gd @@ -0,0 +1,8 @@ +func test(): + # Converted to String when initialized + var string_array: Array[String] = [&"abc"] + print(string_array) + + # Converted to StringName when initialized + var stringname_array: Array[StringName] = ["abc"] + print(stringname_array) diff --git a/modules/gdscript/tests/scripts/analyzer/features/array_string_stringname_equivalent.out b/modules/gdscript/tests/scripts/analyzer/features/array_string_stringname_equivalent.out new file mode 100644 index 0000000000..70dd01d88e --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/array_string_stringname_equivalent.out @@ -0,0 +1,3 @@ +GDTEST_OK +["abc"] +[&"abc"] diff --git a/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd new file mode 100644 index 0000000000..5303fb04e2 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd @@ -0,0 +1,35 @@ +# https://github.com/godotengine/godot/issues/63965 + +func test(): + var array_str: Array = [] + array_str.push_back("godot") + print("StringName in Array: ", &"godot" in array_str) + + var array_sname: Array = [] + array_sname.push_back(&"godot") + print("String in Array: ", "godot" in array_sname) + + # Not equal because the values are different types. + print("Arrays not equal: ", array_str != array_sname) + + var string_array: Array[String] = [] + var stringname_array: Array[StringName] = [] + + assert(!string_array.push_back(&"abc")) + print("Array[String] insert converted: ", typeof(string_array[0]) == TYPE_STRING) + + assert(!stringname_array.push_back("abc")) + print("Array[StringName] insert converted: ", typeof(stringname_array[0]) == TYPE_STRING_NAME) + + print("StringName in Array[String]: ", &"abc" in string_array) + print("String in Array[StringName]: ", "abc" in stringname_array) + + var packed_string_array: PackedStringArray = [] + assert(!packed_string_array.push_back("abc")) + print("StringName in PackedStringArray: ", &"abc" in packed_string_array) + + assert(!string_array.push_back("abc")) + print("StringName finds String in Array: ", string_array.find(&"abc")) + + assert(!stringname_array.push_back(&"abc")) + print("String finds StringName in Array: ", stringname_array.find("abc")) diff --git a/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.out b/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.out new file mode 100644 index 0000000000..98ab78e8f1 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.out @@ -0,0 +1,11 @@ +GDTEST_OK +StringName in Array: true +String in Array: true +Arrays not equal: true +Array[String] insert converted: true +Array[StringName] insert converted: true +StringName in Array[String]: true +String in Array[StringName]: true +StringName in PackedStringArray: true +StringName finds String in Array: 0 +String finds StringName in Array: 0 diff --git a/modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.gd new file mode 100644 index 0000000000..1f15026f17 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.gd @@ -0,0 +1,17 @@ +# https://github.com/godotengine/godot/issues/62957 + +func test(): + var string_dict = {} + string_dict["abc"] = 42 + var stringname_dict = {} + stringname_dict[&"abc"] = 24 + + print("String key is TYPE_STRING: ", typeof(string_dict.keys()[0]) == TYPE_STRING) + print("StringName key is TYPE_STRING: ", typeof(stringname_dict.keys()[0]) == TYPE_STRING) + + print("StringName gets String: ", string_dict.get(&"abc")) + print("String gets StringName: ", stringname_dict.get("abc")) + + stringname_dict[&"abc"] = 42 + # They compare equal because StringName keys are converted to String. + print("String Dictionary == StringName Dictionary: ", string_dict == stringname_dict) diff --git a/modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.out b/modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.out new file mode 100644 index 0000000000..ab5b89d55c --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/dictionary_string_stringname_equivalent.out @@ -0,0 +1,6 @@ +GDTEST_OK +String key is TYPE_STRING: true +StringName key is TYPE_STRING: true +StringName gets String: 42 +String gets StringName: 24 +String Dictionary == StringName Dictionary: true diff --git a/modules/gdscript/tests/scripts/runtime/features/match_string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/runtime/features/match_string_stringname_equivalent.gd new file mode 100644 index 0000000000..55be021a90 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/match_string_stringname_equivalent.gd @@ -0,0 +1,14 @@ +# https://github.com/godotengine/godot/issues/60145 + +func test(): + match "abc": + &"abc": + print("String matched StringName") + _: + print("no match") + + match &"abc": + "abc": + print("StringName matched String") + _: + print("no match") diff --git a/modules/gdscript/tests/scripts/runtime/features/match_string_stringname_equivalent.out b/modules/gdscript/tests/scripts/runtime/features/match_string_stringname_equivalent.out new file mode 100644 index 0000000000..9d5a18da3d --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/match_string_stringname_equivalent.out @@ -0,0 +1,3 @@ +GDTEST_OK +String matched StringName +StringName matched String diff --git a/modules/gdscript/tests/scripts/runtime/features/parameter_shadowing.gd b/modules/gdscript/tests/scripts/runtime/features/parameter_shadowing.gd new file mode 100644 index 0000000000..f33ba7dffd --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/parameter_shadowing.gd @@ -0,0 +1,25 @@ +# https://github.com/godotengine/godot/pull/69620 + +var a: int = 1 + +func shadow_regular_assignment(a: Variant, b: Variant) -> void: + print(a) + print(self.a) + a = b + print(a) + print(self.a) + + +var v := Vector2(0.0, 0.0) + +func shadow_subscript_assignment(v: Vector2, x: float) -> void: + print(v) + print(self.v) + v.x += x + print(v) + print(self.v) + + +func test(): + shadow_regular_assignment('a', 'b') + shadow_subscript_assignment(Vector2(1.0, 1.0), 5.0) diff --git a/modules/gdscript/tests/scripts/runtime/features/parameter_shadowing.out b/modules/gdscript/tests/scripts/runtime/features/parameter_shadowing.out new file mode 100644 index 0000000000..5b981bc8bb --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/parameter_shadowing.out @@ -0,0 +1,17 @@ +GDTEST_OK +>> WARNING +>> Line: 5 +>> SHADOWED_VARIABLE +>> The local function parameter "a" is shadowing an already-declared variable at line 3. +>> WARNING +>> Line: 15 +>> SHADOWED_VARIABLE +>> The local function parameter "v" is shadowing an already-declared variable at line 13. +a +1 +b +1 +(1, 1) +(0, 0) +(6, 1) +(0, 0) diff --git a/modules/gdscript/tests/scripts/runtime/features/string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/runtime/features/string_stringname_equivalent.gd new file mode 100644 index 0000000000..f8bd46523e --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/string_stringname_equivalent.gd @@ -0,0 +1,11 @@ +# https://github.com/godotengine/godot/issues/64171 + +func test(): + print("Compare ==: ", "abc" == &"abc") + print("Compare ==: ", &"abc" == "abc") + print("Compare !=: ", "abc" != &"abc") + print("Compare !=: ", &"abc" != "abc") + + print("Concat: ", "abc" + &"def") + print("Concat: ", &"abc" + "def") + print("Concat: ", &"abc" + &"def") diff --git a/modules/gdscript/tests/scripts/runtime/features/string_stringname_equivalent.out b/modules/gdscript/tests/scripts/runtime/features/string_stringname_equivalent.out new file mode 100644 index 0000000000..7e9c364b60 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/string_stringname_equivalent.out @@ -0,0 +1,8 @@ +GDTEST_OK +Compare ==: true +Compare ==: true +Compare !=: false +Compare !=: false +Concat: abcdef +Concat: abcdef +Concat: abcdef diff --git a/modules/gltf/editor/editor_scene_importer_gltf.cpp b/modules/gltf/editor/editor_scene_importer_gltf.cpp index b10bb49a8e..bd1ba85abf 100644 --- a/modules/gltf/editor/editor_scene_importer_gltf.cpp +++ b/modules/gltf/editor/editor_scene_importer_gltf.cpp @@ -60,7 +60,12 @@ Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t if (p_options.has("animation/import")) { state->set_create_animations(bool(p_options["animation/import"])); } - return doc->generate_scene(state, (float)p_options["animation/fps"], (bool)p_options["animation/trimming"]); + + if (p_options.has("animation/trimming")) { + return doc->generate_scene(state, (float)p_options["animation/fps"], (bool)p_options["animation/trimming"]); + } else { + return doc->generate_scene(state, (float)p_options["animation/fps"], false); + } } #endif // TOOLS_ENABLED diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 7524d37548..a4be0629c4 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -788,7 +788,7 @@ Error GLTFDocument::_parse_buffers(Ref<GLTFState> state, const String &p_base_pa ERR_FAIL_COND_V(p_base_path.is_empty(), ERR_INVALID_PARAMETER); uri = uri.uri_decode(); uri = p_base_path.path_join(uri).replace("\\", "/"); // Fix for Windows. - buffer_data = FileAccess::get_file_as_array(uri); + buffer_data = FileAccess::get_file_as_bytes(uri); ERR_FAIL_COND_V_MSG(buffer.size() == 0, ERR_PARSE_ERROR, "glTF: Couldn't load binary file as an array: " + uri); } @@ -3131,7 +3131,7 @@ Error GLTFDocument::_parse_images(Ref<GLTFState> state, const String &p_base_pat // Fallback to loading as byte array. // This enables us to support the spec's requirement that we honor mimetype // regardless of file URI. - data = FileAccess::get_file_as_array(uri); + data = FileAccess::get_file_as_bytes(uri); if (data.size() == 0) { WARN_PRINT(vformat("glTF: Image index '%d' couldn't be loaded as a buffer of MIME type '%s' from URI: %s. Skipping it.", i, mimetype, uri)); state->images.push_back(Ref<Texture2D>()); // Placeholder to keep count. diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index cfb9a0fbfb..137fd61a25 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -391,10 +391,10 @@ bool CSharpLanguage::supports_builtin_mode() const { #ifdef TOOLS_ENABLED static String variant_type_to_managed_name(const String &p_var_type_name) { if (p_var_type_name.is_empty()) { - return "object"; + return "Variant"; } - if (!ClassDB::class_exists(p_var_type_name)) { + if (ClassDB::class_exists(p_var_type_name)) { return p_var_type_name; } @@ -402,12 +402,12 @@ static String variant_type_to_managed_name(const String &p_var_type_name) { return "Godot.Object"; } + if (p_var_type_name == Variant::get_type_name(Variant::INT)) { + return "long"; + } + if (p_var_type_name == Variant::get_type_name(Variant::FLOAT)) { -#ifdef REAL_T_IS_DOUBLE return "double"; -#else - return "float"; -#endif } if (p_var_type_name == Variant::get_type_name(Variant::STRING)) { @@ -485,7 +485,7 @@ static String variant_type_to_managed_name(const String &p_var_type_name) { } } - return "object"; + return "Variant"; } String CSharpLanguage::make_function(const String &, const String &p_name, const PackedStringArray &p_args) const { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Sdk/Sdk.props b/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Sdk/Sdk.props index 0459257106..0d0889c491 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Sdk/Sdk.props +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.NET.Sdk/Sdk/Sdk.props @@ -12,8 +12,7 @@ <Configurations>Debug;ExportDebug;ExportRelease</Configurations> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> - <GodotProjectDir Condition=" '$(SolutionDir)' != '' ">$(SolutionDir)</GodotProjectDir> - <GodotProjectDir Condition=" '$(SolutionDir)' == '' ">$(MSBuildProjectDirectory)</GodotProjectDir> + <GodotProjectDir Condition=" '$(GodotProjectDir)' == '' ">$(MSBuildProjectDirectory)</GodotProjectDir> <GodotProjectDir>$([MSBuild]::EnsureTrailingSlash('$(GodotProjectDir)'))</GodotProjectDir> <!-- Custom output paths for Godot projects. In brief, 'bin\' and 'obj\' are moved to '$(GodotProjectDir)\.godot\mono\temp\'. --> diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs index 7008fb638f..d67e57edc2 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -268,8 +268,9 @@ namespace Godot.SourceGenerators if (parameters.Length > paramTypes.Length) return null; // Ignore incompatible method - return new GodotMethodData(method, paramTypes, parameters - .Select(p => p.Type).ToImmutableArray(), retType, retSymbol); + return new GodotMethodData(method, paramTypes, + parameters.Select(p => p.Type).ToImmutableArray(), + retType != null ? (retType.Value, retSymbol) : null); } public static IEnumerable<GodotMethodData> WhereHasGodotCompatibleSignature( @@ -330,10 +331,10 @@ namespace Godot.SourceGenerators public static string Path(this Location location) => location.SourceTree?.GetLineSpan(location.SourceSpan).Path - ?? location.GetLineSpan().Path; + ?? location.GetLineSpan().Path; public static int StartLine(this Location location) => location.SourceTree?.GetLineSpan(location.SourceSpan).StartLinePosition.Line - ?? location.GetLineSpan().StartLinePosition.Line; + ?? location.GetLineSpan().StartLinePosition.Line; } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotMemberData.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotMemberData.cs index abd8079922..0760ea11bb 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotMemberData.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotMemberData.cs @@ -6,20 +6,18 @@ namespace Godot.SourceGenerators public readonly struct GodotMethodData { public GodotMethodData(IMethodSymbol method, ImmutableArray<MarshalType> paramTypes, - ImmutableArray<ITypeSymbol> paramTypeSymbols, MarshalType? retType, ITypeSymbol? retSymbol) + ImmutableArray<ITypeSymbol> paramTypeSymbols, (MarshalType MarshalType, ITypeSymbol TypeSymbol)? retType) { Method = method; ParamTypes = paramTypes; ParamTypeSymbols = paramTypeSymbols; RetType = retType; - RetSymbol = retSymbol; } public IMethodSymbol Method { get; } public ImmutableArray<MarshalType> ParamTypes { get; } public ImmutableArray<ITypeSymbol> ParamTypeSymbols { get; } - public MarshalType? RetType { get; } - public ITypeSymbol? RetSymbol { get; } + public (MarshalType MarshalType, ITypeSymbol TypeSymbol)? RetType { get; } } public readonly struct GodotSignalDelegateData diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs index 4fdd40f638..5b3f677f87 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs @@ -304,240 +304,33 @@ namespace Godot.SourceGenerators { return marshalType switch { - MarshalType.Boolean => - source.Append(VariantUtils, ".ConvertToBool(", inputExpr, ")"), - MarshalType.Char => - source.Append("(char)", VariantUtils, ".ConvertToUInt16(", inputExpr, ")"), - MarshalType.SByte => - source.Append(VariantUtils, ".ConvertToInt8(", inputExpr, ")"), - MarshalType.Int16 => - source.Append(VariantUtils, ".ConvertToInt16(", inputExpr, ")"), - MarshalType.Int32 => - source.Append(VariantUtils, ".ConvertToInt32(", inputExpr, ")"), - MarshalType.Int64 => - source.Append(VariantUtils, ".ConvertToInt64(", inputExpr, ")"), - MarshalType.Byte => - source.Append(VariantUtils, ".ConvertToUInt8(", inputExpr, ")"), - MarshalType.UInt16 => - source.Append(VariantUtils, ".ConvertToUInt16(", inputExpr, ")"), - MarshalType.UInt32 => - source.Append(VariantUtils, ".ConvertToUInt32(", inputExpr, ")"), - MarshalType.UInt64 => - source.Append(VariantUtils, ".ConvertToUInt64(", inputExpr, ")"), - MarshalType.Single => - source.Append(VariantUtils, ".ConvertToFloat32(", inputExpr, ")"), - MarshalType.Double => - source.Append(VariantUtils, ".ConvertToFloat64(", inputExpr, ")"), - MarshalType.String => - source.Append(VariantUtils, ".ConvertToStringObject(", inputExpr, ")"), - MarshalType.Vector2 => - source.Append(VariantUtils, ".ConvertToVector2(", inputExpr, ")"), - MarshalType.Vector2i => - source.Append(VariantUtils, ".ConvertToVector2i(", inputExpr, ")"), - MarshalType.Rect2 => - source.Append(VariantUtils, ".ConvertToRect2(", inputExpr, ")"), - MarshalType.Rect2i => - source.Append(VariantUtils, ".ConvertToRect2i(", inputExpr, ")"), - MarshalType.Transform2D => - source.Append(VariantUtils, ".ConvertToTransform2D(", inputExpr, ")"), - MarshalType.Vector3 => - source.Append(VariantUtils, ".ConvertToVector3(", inputExpr, ")"), - MarshalType.Vector3i => - source.Append(VariantUtils, ".ConvertToVector3i(", inputExpr, ")"), - MarshalType.Basis => - source.Append(VariantUtils, ".ConvertToBasis(", inputExpr, ")"), - MarshalType.Quaternion => - source.Append(VariantUtils, ".ConvertToQuaternion(", inputExpr, ")"), - MarshalType.Transform3D => - source.Append(VariantUtils, ".ConvertToTransform3D(", inputExpr, ")"), - MarshalType.Vector4 => - source.Append(VariantUtils, ".ConvertToVector4(", inputExpr, ")"), - MarshalType.Vector4i => - source.Append(VariantUtils, ".ConvertToVector4i(", inputExpr, ")"), - MarshalType.Projection => - source.Append(VariantUtils, ".ConvertToProjection(", inputExpr, ")"), - MarshalType.AABB => - source.Append(VariantUtils, ".ConvertToAABB(", inputExpr, ")"), - MarshalType.Color => - source.Append(VariantUtils, ".ConvertToColor(", inputExpr, ")"), - MarshalType.Plane => - source.Append(VariantUtils, ".ConvertToPlane(", inputExpr, ")"), - MarshalType.Callable => - source.Append(VariantUtils, ".ConvertToCallableManaged(", inputExpr, ")"), - MarshalType.SignalInfo => - source.Append(VariantUtils, ".ConvertToSignalInfo(", inputExpr, ")"), - MarshalType.Enum => - source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(), - ")", VariantUtils, ".ConvertToInt32(", inputExpr, ")"), - MarshalType.ByteArray => - source.Append(VariantUtils, ".ConvertAsPackedByteArrayToSystemArray(", inputExpr, ")"), - MarshalType.Int32Array => - source.Append(VariantUtils, ".ConvertAsPackedInt32ArrayToSystemArray(", inputExpr, ")"), - MarshalType.Int64Array => - source.Append(VariantUtils, ".ConvertAsPackedInt64ArrayToSystemArray(", inputExpr, ")"), - MarshalType.Float32Array => - source.Append(VariantUtils, ".ConvertAsPackedFloat32ArrayToSystemArray(", inputExpr, ")"), - MarshalType.Float64Array => - source.Append(VariantUtils, ".ConvertAsPackedFloat64ArrayToSystemArray(", inputExpr, ")"), - MarshalType.StringArray => - source.Append(VariantUtils, ".ConvertAsPackedStringArrayToSystemArray(", inputExpr, ")"), - MarshalType.Vector2Array => - source.Append(VariantUtils, ".ConvertAsPackedVector2ArrayToSystemArray(", inputExpr, ")"), - MarshalType.Vector3Array => - source.Append(VariantUtils, ".ConvertAsPackedVector3ArrayToSystemArray(", inputExpr, ")"), - MarshalType.ColorArray => - source.Append(VariantUtils, ".ConvertAsPackedColorArrayToSystemArray(", inputExpr, ")"), - MarshalType.GodotObjectOrDerivedArray => - source.Append(VariantUtils, ".ConvertToSystemArrayOfGodotObject<", - ((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"), - MarshalType.SystemArrayOfStringName => - source.Append(VariantUtils, ".ConvertToSystemArrayOfStringName(", inputExpr, ")"), - MarshalType.SystemArrayOfNodePath => - source.Append(VariantUtils, ".ConvertToSystemArrayOfNodePath(", inputExpr, ")"), - MarshalType.SystemArrayOfRID => - source.Append(VariantUtils, ".ConvertToSystemArrayOfRID(", inputExpr, ")"), - MarshalType.Variant => - source.Append("global::Godot.Variant.CreateCopyingBorrowed(", inputExpr, ")"), - MarshalType.GodotObjectOrDerived => - source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(), - ")", VariantUtils, ".ConvertToGodotObject(", inputExpr, ")"), - MarshalType.StringName => - source.Append(VariantUtils, ".ConvertToStringNameObject(", inputExpr, ")"), - MarshalType.NodePath => - source.Append(VariantUtils, ".ConvertToNodePathObject(", inputExpr, ")"), - MarshalType.RID => - source.Append(VariantUtils, ".ConvertToRID(", inputExpr, ")"), - MarshalType.GodotDictionary => - source.Append(VariantUtils, ".ConvertToDictionaryObject(", inputExpr, ")"), - MarshalType.GodotArray => - source.Append(VariantUtils, ".ConvertToArrayObject(", inputExpr, ")"), + // For generic Godot collections, VariantUtils.ConvertTo<T> is slower, so we need this special case MarshalType.GodotGenericDictionary => source.Append(VariantUtils, ".ConvertToDictionaryObject<", ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ", - ((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"), + ((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">(", + inputExpr, ")"), MarshalType.GodotGenericArray => source.Append(VariantUtils, ".ConvertToArrayObject<", - ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"), - _ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType, - "Received unexpected marshal type") + ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">(", + inputExpr, ")"), + _ => source.Append(VariantUtils, ".ConvertTo<", + typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"), }; } - public static StringBuilder AppendManagedToNativeVariantExpr( - this StringBuilder source, string inputExpr, MarshalType marshalType) + public static StringBuilder AppendManagedToNativeVariantExpr(this StringBuilder source, + string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType) { return marshalType switch { - MarshalType.Boolean => - source.Append(VariantUtils, ".CreateFromBool(", inputExpr, ")"), - MarshalType.Char => - source.Append(VariantUtils, ".CreateFromInt((ushort)", inputExpr, ")"), - MarshalType.SByte => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.Int16 => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.Int32 => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.Int64 => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.Byte => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.UInt16 => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.UInt32 => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.UInt64 => - source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"), - MarshalType.Single => - source.Append(VariantUtils, ".CreateFromFloat(", inputExpr, ")"), - MarshalType.Double => - source.Append(VariantUtils, ".CreateFromFloat(", inputExpr, ")"), - MarshalType.String => - source.Append(VariantUtils, ".CreateFromString(", inputExpr, ")"), - MarshalType.Vector2 => - source.Append(VariantUtils, ".CreateFromVector2(", inputExpr, ")"), - MarshalType.Vector2i => - source.Append(VariantUtils, ".CreateFromVector2i(", inputExpr, ")"), - MarshalType.Rect2 => - source.Append(VariantUtils, ".CreateFromRect2(", inputExpr, ")"), - MarshalType.Rect2i => - source.Append(VariantUtils, ".CreateFromRect2i(", inputExpr, ")"), - MarshalType.Transform2D => - source.Append(VariantUtils, ".CreateFromTransform2D(", inputExpr, ")"), - MarshalType.Vector3 => - source.Append(VariantUtils, ".CreateFromVector3(", inputExpr, ")"), - MarshalType.Vector3i => - source.Append(VariantUtils, ".CreateFromVector3i(", inputExpr, ")"), - MarshalType.Basis => - source.Append(VariantUtils, ".CreateFromBasis(", inputExpr, ")"), - MarshalType.Quaternion => - source.Append(VariantUtils, ".CreateFromQuaternion(", inputExpr, ")"), - MarshalType.Transform3D => - source.Append(VariantUtils, ".CreateFromTransform3D(", inputExpr, ")"), - MarshalType.Vector4 => - source.Append(VariantUtils, ".CreateFromVector4(", inputExpr, ")"), - MarshalType.Vector4i => - source.Append(VariantUtils, ".CreateFromVector4i(", inputExpr, ")"), - MarshalType.Projection => - source.Append(VariantUtils, ".CreateFromProjection(", inputExpr, ")"), - MarshalType.AABB => - source.Append(VariantUtils, ".CreateFromAABB(", inputExpr, ")"), - MarshalType.Color => - source.Append(VariantUtils, ".CreateFromColor(", inputExpr, ")"), - MarshalType.Plane => - source.Append(VariantUtils, ".CreateFromPlane(", inputExpr, ")"), - MarshalType.Callable => - source.Append(VariantUtils, ".CreateFromCallable(", inputExpr, ")"), - MarshalType.SignalInfo => - source.Append(VariantUtils, ".CreateFromSignalInfo(", inputExpr, ")"), - MarshalType.Enum => - source.Append(VariantUtils, ".CreateFromInt((int)", inputExpr, ")"), - MarshalType.ByteArray => - source.Append(VariantUtils, ".CreateFromPackedByteArray(", inputExpr, ")"), - MarshalType.Int32Array => - source.Append(VariantUtils, ".CreateFromPackedInt32Array(", inputExpr, ")"), - MarshalType.Int64Array => - source.Append(VariantUtils, ".CreateFromPackedInt64Array(", inputExpr, ")"), - MarshalType.Float32Array => - source.Append(VariantUtils, ".CreateFromPackedFloat32Array(", inputExpr, ")"), - MarshalType.Float64Array => - source.Append(VariantUtils, ".CreateFromPackedFloat64Array(", inputExpr, ")"), - MarshalType.StringArray => - source.Append(VariantUtils, ".CreateFromPackedStringArray(", inputExpr, ")"), - MarshalType.Vector2Array => - source.Append(VariantUtils, ".CreateFromPackedVector2Array(", inputExpr, ")"), - MarshalType.Vector3Array => - source.Append(VariantUtils, ".CreateFromPackedVector3Array(", inputExpr, ")"), - MarshalType.ColorArray => - source.Append(VariantUtils, ".CreateFromPackedColorArray(", inputExpr, ")"), - MarshalType.GodotObjectOrDerivedArray => - source.Append(VariantUtils, ".CreateFromSystemArrayOfGodotObject(", inputExpr, ")"), - MarshalType.SystemArrayOfStringName => - source.Append(VariantUtils, ".CreateFromSystemArrayOfStringName(", inputExpr, ")"), - MarshalType.SystemArrayOfNodePath => - source.Append(VariantUtils, ".CreateFromSystemArrayOfNodePath(", inputExpr, ")"), - MarshalType.SystemArrayOfRID => - source.Append(VariantUtils, ".CreateFromSystemArrayOfRID(", inputExpr, ")"), - MarshalType.Variant => - source.Append(inputExpr, ".CopyNativeVariant()"), - MarshalType.GodotObjectOrDerived => - source.Append(VariantUtils, ".CreateFromGodotObject(", inputExpr, ")"), - MarshalType.StringName => - source.Append(VariantUtils, ".CreateFromStringName(", inputExpr, ")"), - MarshalType.NodePath => - source.Append(VariantUtils, ".CreateFromNodePath(", inputExpr, ")"), - MarshalType.RID => - source.Append(VariantUtils, ".CreateFromRID(", inputExpr, ")"), - MarshalType.GodotDictionary => - source.Append(VariantUtils, ".CreateFromDictionary(", inputExpr, ")"), - MarshalType.GodotArray => - source.Append(VariantUtils, ".CreateFromArray(", inputExpr, ")"), + // For generic Godot collections, VariantUtils.CreateFrom<T> is slower, so we need this special case MarshalType.GodotGenericDictionary => source.Append(VariantUtils, ".CreateFromDictionary(", inputExpr, ")"), MarshalType.GodotGenericArray => source.Append(VariantUtils, ".CreateFromArray(", inputExpr, ")"), - _ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType, - "Received unexpected marshal type") + _ => source.Append(VariantUtils, ".CreateFrom<", + typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"), }; } @@ -546,137 +339,30 @@ namespace Godot.SourceGenerators { return marshalType switch { - MarshalType.Boolean => source.Append(inputExpr, ".AsBool()"), - MarshalType.Char => source.Append(inputExpr, ".AsChar()"), - MarshalType.SByte => source.Append(inputExpr, ".AsSByte()"), - MarshalType.Int16 => source.Append(inputExpr, ".AsInt16()"), - MarshalType.Int32 => source.Append(inputExpr, ".AsInt32()"), - MarshalType.Int64 => source.Append(inputExpr, ".AsInt64()"), - MarshalType.Byte => source.Append(inputExpr, ".AsByte()"), - MarshalType.UInt16 => source.Append(inputExpr, ".AsUInt16()"), - MarshalType.UInt32 => source.Append(inputExpr, ".AsUInt32()"), - MarshalType.UInt64 => source.Append(inputExpr, ".AsUInt64()"), - MarshalType.Single => source.Append(inputExpr, ".AsSingle()"), - MarshalType.Double => source.Append(inputExpr, ".AsDouble()"), - MarshalType.String => source.Append(inputExpr, ".AsString()"), - MarshalType.Vector2 => source.Append(inputExpr, ".AsVector2()"), - MarshalType.Vector2i => source.Append(inputExpr, ".AsVector2i()"), - MarshalType.Rect2 => source.Append(inputExpr, ".AsRect2()"), - MarshalType.Rect2i => source.Append(inputExpr, ".AsRect2i()"), - MarshalType.Transform2D => source.Append(inputExpr, ".AsTransform2D()"), - MarshalType.Vector3 => source.Append(inputExpr, ".AsVector3()"), - MarshalType.Vector3i => source.Append(inputExpr, ".AsVector3i()"), - MarshalType.Basis => source.Append(inputExpr, ".AsBasis()"), - MarshalType.Quaternion => source.Append(inputExpr, ".AsQuaternion()"), - MarshalType.Transform3D => source.Append(inputExpr, ".AsTransform3D()"), - MarshalType.Vector4 => source.Append(inputExpr, ".AsVector4()"), - MarshalType.Vector4i => source.Append(inputExpr, ".AsVector4i()"), - MarshalType.Projection => source.Append(inputExpr, ".AsProjection()"), - MarshalType.AABB => source.Append(inputExpr, ".AsAABB()"), - MarshalType.Color => source.Append(inputExpr, ".AsColor()"), - MarshalType.Plane => source.Append(inputExpr, ".AsPlane()"), - MarshalType.Callable => source.Append(inputExpr, ".AsCallable()"), - MarshalType.SignalInfo => source.Append(inputExpr, ".AsSignalInfo()"), - MarshalType.Enum => - source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(), ")", inputExpr, ".AsInt64()"), - MarshalType.ByteArray => source.Append(inputExpr, ".AsByteArray()"), - MarshalType.Int32Array => source.Append(inputExpr, ".AsInt32Array()"), - MarshalType.Int64Array => source.Append(inputExpr, ".AsInt64Array()"), - MarshalType.Float32Array => source.Append(inputExpr, ".AsFloat32Array()"), - MarshalType.Float64Array => source.Append(inputExpr, ".AsFloat64Array()"), - MarshalType.StringArray => source.Append(inputExpr, ".AsStringArray()"), - MarshalType.Vector2Array => source.Append(inputExpr, ".AsVector2Array()"), - MarshalType.Vector3Array => source.Append(inputExpr, ".AsVector3Array()"), - MarshalType.ColorArray => source.Append(inputExpr, ".AsColorArray()"), - MarshalType.GodotObjectOrDerivedArray => source.Append(inputExpr, ".AsGodotObjectArray<", - ((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">()"), - MarshalType.SystemArrayOfStringName => source.Append(inputExpr, ".AsSystemArrayOfStringName()"), - MarshalType.SystemArrayOfNodePath => source.Append(inputExpr, ".AsSystemArrayOfNodePath()"), - MarshalType.SystemArrayOfRID => source.Append(inputExpr, ".AsSystemArrayOfRID()"), - MarshalType.Variant => source.Append(inputExpr), - MarshalType.GodotObjectOrDerived => source.Append("(", - typeSymbol.FullQualifiedNameIncludeGlobal(), ")", inputExpr, ".AsGodotObject()"), - MarshalType.StringName => source.Append(inputExpr, ".AsStringName()"), - MarshalType.NodePath => source.Append(inputExpr, ".AsNodePath()"), - MarshalType.RID => source.Append(inputExpr, ".AsRID()"), - MarshalType.GodotDictionary => source.Append(inputExpr, ".AsGodotDictionary()"), - MarshalType.GodotArray => source.Append(inputExpr, ".AsGodotArray()"), - MarshalType.GodotGenericDictionary => source.Append(inputExpr, ".AsGodotDictionary<", - ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ", - ((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">()"), - MarshalType.GodotGenericArray => source.Append(inputExpr, ".AsGodotArray<", - ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">()"), - _ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType, - "Received unexpected marshal type") + // For generic Godot collections, Variant.As<T> is slower, so we need this special case + MarshalType.GodotGenericDictionary => + source.Append(inputExpr, ".AsGodotDictionary<", + ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ", + ((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">()"), + MarshalType.GodotGenericArray => + source.Append(inputExpr, ".AsGodotArray<", + ((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">()"), + _ => source.Append(inputExpr, ".As<", + typeSymbol.FullQualifiedNameIncludeGlobal(), ">()") }; } public static StringBuilder AppendManagedToVariantExpr(this StringBuilder source, - string inputExpr, MarshalType marshalType) + string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType) { - switch (marshalType) + return marshalType switch { - case MarshalType.Boolean: - case MarshalType.Char: - case MarshalType.SByte: - case MarshalType.Int16: - case MarshalType.Int32: - case MarshalType.Int64: - case MarshalType.Byte: - case MarshalType.UInt16: - case MarshalType.UInt32: - case MarshalType.UInt64: - case MarshalType.Single: - case MarshalType.Double: - case MarshalType.String: - case MarshalType.Vector2: - case MarshalType.Vector2i: - case MarshalType.Rect2: - case MarshalType.Rect2i: - case MarshalType.Transform2D: - case MarshalType.Vector3: - case MarshalType.Vector3i: - case MarshalType.Basis: - case MarshalType.Quaternion: - case MarshalType.Transform3D: - case MarshalType.Vector4: - case MarshalType.Vector4i: - case MarshalType.Projection: - case MarshalType.AABB: - case MarshalType.Color: - case MarshalType.Plane: - case MarshalType.Callable: - case MarshalType.SignalInfo: - case MarshalType.ByteArray: - case MarshalType.Int32Array: - case MarshalType.Int64Array: - case MarshalType.Float32Array: - case MarshalType.Float64Array: - case MarshalType.StringArray: - case MarshalType.Vector2Array: - case MarshalType.Vector3Array: - case MarshalType.ColorArray: - case MarshalType.GodotObjectOrDerivedArray: - case MarshalType.SystemArrayOfStringName: - case MarshalType.SystemArrayOfNodePath: - case MarshalType.SystemArrayOfRID: - case MarshalType.GodotObjectOrDerived: - case MarshalType.StringName: - case MarshalType.NodePath: - case MarshalType.RID: - case MarshalType.GodotDictionary: - case MarshalType.GodotArray: - case MarshalType.GodotGenericDictionary: - case MarshalType.GodotGenericArray: - return source.Append("Variant.CreateFrom(", inputExpr, ")"); - case MarshalType.Enum: - return source.Append("Variant.CreateFrom((long)", inputExpr, ")"); - case MarshalType.Variant: - return source.Append(inputExpr); - default: - throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType, - "Received unexpected marshal type"); - } + // For generic Godot collections, Variant.From<T> is slower, so we need this special case + MarshalType.GodotGenericDictionary or MarshalType.GodotGenericArray => + source.Append("global::Godot.Variant.CreateFrom(", inputExpr, ")"), + _ => source.Append("global::Godot.Variant.From<", + typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")") + }; } } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs index 2f51018293..f79909589e 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptMethodsGenerator.cs @@ -135,7 +135,8 @@ namespace Godot.SourceGenerators source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); - source.Append($" public new class MethodName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.MethodName {{\n"); + source.Append( + $" public new class MethodName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.MethodName {{\n"); // Generate cached StringNames for methods and properties, for fast lookup @@ -297,7 +298,7 @@ namespace Godot.SourceGenerators if (method.RetType != null) { - returnVal = DeterminePropertyInfo(method.RetType.Value, name: string.Empty); + returnVal = DeterminePropertyInfo(method.RetType.Value.MarshalType, name: string.Empty); } else { @@ -391,7 +392,8 @@ namespace Godot.SourceGenerators { source.Append(" ret = "); - source.AppendManagedToNativeVariantExpr("callRet", method.RetType.Value); + source.AppendManagedToNativeVariantExpr("callRet", + method.RetType.Value.TypeSymbol, method.RetType.Value.MarshalType); source.Append(";\n"); source.Append(" return true;\n"); diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs index 252f162b0c..6c4206a575 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertiesGenerator.cs @@ -124,7 +124,8 @@ namespace Godot.SourceGenerators source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); - source.Append($" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n"); + source.Append( + $" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n"); // Generate cached StringNames for methods and properties, for fast lookup @@ -199,14 +200,14 @@ namespace Godot.SourceGenerators foreach (var property in godotClassProperties) { GeneratePropertyGetter(property.PropertySymbol.Name, - property.Type, source, isFirstEntry); + property.PropertySymbol.Type, property.Type, source, isFirstEntry); isFirstEntry = false; } foreach (var field in godotClassFields) { GeneratePropertyGetter(field.FieldSymbol.Name, - field.Type, source, isFirstEntry); + field.FieldSymbol.Type, field.Type, source, isFirstEntry); isFirstEntry = false; } @@ -303,6 +304,7 @@ namespace Godot.SourceGenerators private static void GeneratePropertyGetter( string propertyMemberName, + ITypeSymbol propertyTypeSymbol, MarshalType propertyMarshalType, StringBuilder source, bool isFirstEntry @@ -317,7 +319,8 @@ namespace Godot.SourceGenerators .Append(propertyMemberName) .Append(") {\n") .Append(" value = ") - .AppendManagedToNativeVariantExpr("this." + propertyMemberName, propertyMarshalType) + .AppendManagedToNativeVariantExpr("this." + propertyMemberName, + propertyTypeSymbol, propertyMarshalType) .Append(";\n") .Append(" return true;\n") .Append(" }\n"); @@ -376,7 +379,8 @@ namespace Godot.SourceGenerators if (propertyUsage != PropertyUsageFlags.Category && attr.ConstructorArguments.Length > 1) hintString = attr.ConstructorArguments[1].Value?.ToString(); - yield return new PropertyInfo(VariantType.Nil, name, PropertyHint.None, hintString, propertyUsage.Value, true); + yield return new PropertyInfo(VariantType.Nil, name, PropertyHint.None, hintString, + propertyUsage.Value, true); } } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs index 3f588a4c90..aa9dd9583e 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPropertyDefValGenerator.cs @@ -78,10 +78,6 @@ namespace Godot.SourceGenerators var source = new StringBuilder(); - source.Append("using Godot;\n"); - source.Append("using Godot.NativeInterop;\n"); - source.Append("\n"); - if (hasNamespace) { source.Append("namespace "); @@ -178,7 +174,8 @@ namespace Godot.SourceGenerators } else { - var propertyGet = propertyDeclarationSyntax.AccessorList?.Accessors.Where(a => a.Keyword.IsKind(SyntaxKind.GetKeyword)).FirstOrDefault(); + var propertyGet = propertyDeclarationSyntax.AccessorList?.Accessors + .Where(a => a.Keyword.IsKind(SyntaxKind.GetKeyword)).FirstOrDefault(); if (propertyGet != null) { if (propertyGet.ExpressionBody != null) @@ -204,7 +201,8 @@ namespace Godot.SourceGenerators { var returns = propertyGet.DescendantNodes().OfType<ReturnStatementSyntax>(); if (returns.Count() == 1) - {// Generate only single return + { + // Generate only single return var returnStatementSyntax = returns.Single(); if (returnStatementSyntax.Expression is IdentifierNameSyntax identifierNameSyntax) { @@ -281,7 +279,8 @@ namespace Godot.SourceGenerators { source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); - string dictionaryType = "System.Collections.Generic.Dictionary<StringName, object>"; + string dictionaryType = + "global::System.Collections.Generic.Dictionary<global::Godot.StringName, global::Godot.Variant>"; source.Append("#if TOOLS\n"); source.Append(" internal new static "); @@ -308,7 +307,8 @@ namespace Godot.SourceGenerators source.Append(" values.Add(PropertyName."); source.Append(exportedMember.Name); source.Append(", "); - source.Append(defaultValueLocalName); + source.AppendManagedToVariantExpr(defaultValueLocalName, + exportedMember.TypeSymbol, exportedMember.Type); source.Append(");\n"); } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSerializationGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSerializationGenerator.cs index ed877cbd17..821f3af75f 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSerializationGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSerializationGenerator.cs @@ -162,7 +162,8 @@ namespace Godot.SourceGenerators source.Append(" info.AddProperty(PropertyName.") .Append(propertyName) .Append(", ") - .AppendManagedToVariantExpr(string.Concat("this.", propertyName), property.Type) + .AppendManagedToVariantExpr(string.Concat("this.", propertyName), + property.PropertySymbol.Type, property.Type) .Append(");\n"); } @@ -175,7 +176,8 @@ namespace Godot.SourceGenerators source.Append(" info.AddProperty(PropertyName.") .Append(fieldName) .Append(", ") - .AppendManagedToVariantExpr(string.Concat("this.", fieldName), field.Type) + .AppendManagedToVariantExpr(string.Concat("this.", fieldName), + field.FieldSymbol.Type, field.Type) .Append(");\n"); } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs index 119cc9d4f0..ba6c10aa31 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptSignalsGenerator.cs @@ -176,7 +176,8 @@ namespace Godot.SourceGenerators source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); - source.Append($" public new class SignalName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.SignalName {{\n"); + source.Append( + $" public new class SignalName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.SignalName {{\n"); // Generate cached StringNames for methods and properties, for fast lookup @@ -236,7 +237,8 @@ namespace Godot.SourceGenerators .Append(signalName) .Append(";\n"); - source.Append($" /// <inheritdoc cref=\"{signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()}\"/>\n"); + source.Append( + $" /// <inheritdoc cref=\"{signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()}\"/>\n"); source.Append(" public event ") .Append(signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()) @@ -351,7 +353,7 @@ namespace Godot.SourceGenerators if (invokeMethodData.RetType != null) { - returnVal = DeterminePropertyInfo(invokeMethodData.RetType.Value, name: string.Empty); + returnVal = DeterminePropertyInfo(invokeMethodData.RetType.Value.MarshalType, name: string.Empty); } else { diff --git a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs index 89364d1c02..de10c04e31 100644 --- a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs +++ b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs @@ -57,24 +57,22 @@ namespace GodotTools { pr.Step("Generating C# project...".TTR()); - string resourceDir = ProjectSettings.GlobalizePath("res://"); - - string path = resourceDir; + string csprojDir = Path.GetDirectoryName(GodotSharpDirs.ProjectCsProjPath); + string slnDir = Path.GetDirectoryName(GodotSharpDirs.ProjectSlnPath); string name = GodotSharpDirs.ProjectAssemblyName; - - string guid = CsProjOperations.GenerateGameProject(path, name); + string guid = CsProjOperations.GenerateGameProject(csprojDir, name); if (guid.Length > 0) { var solution = new DotNetSolution(name) { - DirectoryPath = path + DirectoryPath = slnDir }; var projectInfo = new DotNetSolution.ProjectInfo { Guid = guid, - PathRelativeToSolution = name + ".csproj", + PathRelativeToSolution = Path.GetRelativePath(slnDir, GodotSharpDirs.ProjectCsProjPath), Configs = new List<string> { "Debug", "ExportDebug", "ExportRelease" } }; @@ -375,6 +373,8 @@ namespace GodotTools { base._EnablePlugin(); + ProjectSettingsChanged += GodotSharpDirs.DetermineProjectLocation; + if (Instance != null) throw new InvalidOperationException(); Instance = this; @@ -455,7 +455,7 @@ namespace GodotTools _menuPopup.IdPressed += _MenuOptionPressed; // External editor settings - EditorDef("mono/editor/external_editor", ExternalEditorId.None); + EditorDef("mono/editor/external_editor", Variant.From(ExternalEditorId.None)); string settingsHintStr = "Disabled"; diff --git a/modules/mono/editor/GodotTools/GodotTools/Internals/Globals.cs b/modules/mono/editor/GodotTools/GodotTools/Internals/Globals.cs index acb7cc3ab0..45ae7eb86b 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Internals/Globals.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Internals/Globals.cs @@ -1,3 +1,4 @@ +using Godot; using Godot.NativeInterop; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; @@ -8,30 +9,31 @@ namespace GodotTools.Internals { public static float EditorScale => Internal.godot_icall_Globals_EditorScale(); - public static unsafe object GlobalDef(string setting, object defaultValue, bool restartIfChanged = false) + // ReSharper disable once UnusedMethodReturnValue.Global + public static Variant GlobalDef(string setting, Variant defaultValue, bool restartIfChanged = false) { using godot_string settingIn = Marshaling.ConvertStringToNative(setting); - using godot_variant defaultValueIn = Marshaling.ConvertManagedObjectToVariant(defaultValue); - Internal.godot_icall_Globals_GlobalDef(settingIn, defaultValueIn, restartIfChanged, out godot_variant result); - using (result) - return Marshaling.ConvertVariantToManagedObject(result); + using godot_variant defaultValueIn = defaultValue.CopyNativeVariant(); + Internal.godot_icall_Globals_GlobalDef(settingIn, defaultValueIn, restartIfChanged, + out godot_variant result); + return Variant.CreateTakingOwnershipOfDisposableValue(result); } - public static unsafe object EditorDef(string setting, object defaultValue, bool restartIfChanged = false) + // ReSharper disable once UnusedMethodReturnValue.Global + public static Variant EditorDef(string setting, Variant defaultValue, bool restartIfChanged = false) { using godot_string settingIn = Marshaling.ConvertStringToNative(setting); - using godot_variant defaultValueIn = Marshaling.ConvertManagedObjectToVariant(defaultValue); - Internal.godot_icall_Globals_EditorDef(settingIn, defaultValueIn, restartIfChanged, out godot_variant result); - using (result) - return Marshaling.ConvertVariantToManagedObject(result); + using godot_variant defaultValueIn = defaultValue.CopyNativeVariant(); + Internal.godot_icall_Globals_EditorDef(settingIn, defaultValueIn, restartIfChanged, + out godot_variant result); + return Variant.CreateTakingOwnershipOfDisposableValue(result); } - public static object EditorShortcut(string setting) + public static Variant EditorShortcut(string setting) { using godot_string settingIn = Marshaling.ConvertStringToNative(setting); Internal.godot_icall_Globals_EditorShortcut(settingIn, out godot_variant result); - using (result) - return Marshaling.ConvertVariantToManagedObject(result); + return Variant.CreateTakingOwnershipOfDisposableValue(result); } [SuppressMessage("ReSharper", "InconsistentNaming")] diff --git a/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs b/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs index 14285cc0f1..4e892be55c 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Internals/GodotSharpDirs.cs @@ -52,10 +52,9 @@ namespace GodotTools.Internals { GlobalDef("dotnet/project/assembly_name", ""); GlobalDef("dotnet/project/solution_directory", ""); - GlobalDef("dotnet/project/c#_project_directory", ""); } - private static void DetermineProjectLocation() + public static void DetermineProjectLocation() { static string DetermineProjectName() { @@ -76,10 +75,11 @@ namespace GodotTools.Internals string slnParentDir = (string)ProjectSettings.GetSetting("dotnet/project/solution_directory"); if (string.IsNullOrEmpty(slnParentDir)) slnParentDir = "res://"; + else if (!slnParentDir.StartsWith("res://")) + slnParentDir = "res://" + slnParentDir; - string csprojParentDir = (string)ProjectSettings.GetSetting("dotnet/project/c#_project_directory"); - if (string.IsNullOrEmpty(csprojParentDir)) - csprojParentDir = "res://"; + // The csproj should be in the same folder as project.godot. + string csprojParentDir = "res://"; _projectSlnPath = Path.Combine(ProjectSettings.GlobalizePath(slnParentDir), string.Concat(_projectAssemblyName, ".sln")); diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 9185506776..9f0bc3fbe3 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -2837,9 +2837,6 @@ bool BindingsGenerator::_populate_object_type_interfaces() { itype.is_ref_counted = ClassDB::is_parent_class(type_cname, name_cache.type_RefCounted); itype.memory_own = itype.is_ref_counted; - itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToGodotObject(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromGodotObject(%0)"; - itype.c_out = "%5return "; itype.c_out += C_METHOD_UNMANAGED_GET_MANAGED; itype.c_out += itype.is_ref_counted ? "(%1.Reference);\n" : "(%1);\n"; @@ -3218,8 +3215,6 @@ bool BindingsGenerator::_populate_object_type_interfaces() { enum_itype.cname = StringName(enum_itype.name); enum_itype.proxy_name = itype.proxy_name + "." + enum_proxy_name; TypeInterface::postsetup_enum_type(enum_itype); - enum_itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToInt32(%0)"; - enum_itype.cs_managed_to_variant = "VariantUtils.CreateFromInt((int)%0)"; enum_types.insert(enum_itype.cname, enum_itype); } @@ -3448,16 +3443,14 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { TypeInterface itype; -#define INSERT_STRUCT_TYPE(m_type) \ - { \ - itype = TypeInterface::create_value_type(String(#m_type)); \ - itype.c_type_in = #m_type "*"; \ - itype.c_type_out = itype.cs_type; \ - itype.cs_in_expr = "&%0"; \ - itype.cs_in_expr_is_unsafe = true; \ - itype.cs_variant_to_managed = "VariantUtils.ConvertTo%2(%0)"; \ - itype.cs_managed_to_variant = "VariantUtils.CreateFrom%2(%0)"; \ - builtin_types.insert(itype.cname, itype); \ +#define INSERT_STRUCT_TYPE(m_type) \ + { \ + itype = TypeInterface::create_value_type(String(#m_type)); \ + itype.c_type_in = #m_type "*"; \ + itype.c_type_out = itype.cs_type; \ + itype.cs_in_expr = "&%0"; \ + itype.cs_in_expr_is_unsafe = true; \ + builtin_types.insert(itype.cname, itype); \ } INSERT_STRUCT_TYPE(Vector2) @@ -3488,8 +3481,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_out = itype.c_type; itype.c_arg_in = "&%s"; itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromBool(%1);\n"; - itype.cs_variant_to_managed = "VariantUtils.ConvertToBool(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromBool(%0)"; builtin_types.insert(itype.cname, itype); // Integer types @@ -3510,8 +3501,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_in = itype.name; \ itype.c_type_out = itype.name; \ itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromInt(%1);\n"; \ - itype.cs_variant_to_managed = "VariantUtils.ConvertTo" m_int_struct_name "(%0)"; \ - itype.cs_managed_to_variant = "VariantUtils.CreateFromInt(%0)"; \ builtin_types.insert(itype.cname, itype); \ } @@ -3547,8 +3536,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_in = itype.proxy_name; itype.c_type_out = itype.proxy_name; itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromFloat(%1);\n"; - itype.cs_variant_to_managed = "VariantUtils.ConvertToFloat32(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromFloat(%0)"; builtin_types.insert(itype.cname, itype); // double @@ -3562,8 +3549,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_in = itype.proxy_name; itype.c_type_out = itype.proxy_name; itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromFloat(%1);\n"; - itype.cs_variant_to_managed = "VariantUtils.ConvertToFloat64(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromFloat(%0)"; builtin_types.insert(itype.cname, itype); } @@ -3581,8 +3566,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = true; itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromString(%1);\n"; - itype.cs_variant_to_managed = "VariantUtils.ConvertToStringObject(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromString(%0)"; builtin_types.insert(itype.cname, itype); // StringName @@ -3601,8 +3584,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromStringName(%1);\n"; itype.c_type_is_disposable_struct = false; // [c_out] takes ownership itype.c_ret_needs_default_initialization = true; - itype.cs_variant_to_managed = "VariantUtils.ConvertToStringNameObject(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromStringName(%0)"; builtin_types.insert(itype.cname, itype); // NodePath @@ -3620,8 +3601,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = false; // [c_out] takes ownership itype.c_ret_needs_default_initialization = true; - itype.cs_variant_to_managed = "VariantUtils.ConvertToNodePathObject(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromNodePath(%0)"; builtin_types.insert(itype.cname, itype); // RID @@ -3634,8 +3613,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type = itype.cs_type; itype.c_type_in = itype.c_type; itype.c_type_out = itype.c_type; - itype.cs_variant_to_managed = "VariantUtils.ConvertToRID(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromRID(%0)"; builtin_types.insert(itype.cname, itype); // Variant @@ -3652,8 +3629,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = false; // [c_out] takes ownership itype.c_ret_needs_default_initialization = true; - itype.cs_variant_to_managed = "Variant.CreateCopyingBorrowed(%0)"; - itype.cs_managed_to_variant = "%0.CopyNativeVariant()"; builtin_types.insert(itype.cname, itype); // Callable @@ -3666,8 +3641,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_in = "in " + itype.cs_type; itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = true; - itype.cs_variant_to_managed = "VariantUtils.ConvertToCallableManaged(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromCallable(%0)"; builtin_types.insert(itype.cname, itype); // Signal @@ -3684,8 +3657,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_in = "in " + itype.cs_type; itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = true; - itype.cs_variant_to_managed = "VariantUtils.ConvertToSignalInfo(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromSignalInfo(%0)"; builtin_types.insert(itype.cname, itype); // VarArg (fictitious type to represent variable arguments) @@ -3715,8 +3686,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_in = itype.proxy_name; \ itype.c_type_out = itype.proxy_name; \ itype.c_type_is_disposable_struct = true; \ - itype.cs_variant_to_managed = "VariantUtils.ConvertAs%2ToSystemArray(%0)"; \ - itype.cs_managed_to_variant = "VariantUtils.CreateFrom%2(%0)"; \ builtin_types.insert(itype.name, itype); \ } @@ -3752,8 +3721,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = false; // [c_out] takes ownership itype.c_ret_needs_default_initialization = true; - itype.cs_variant_to_managed = "VariantUtils.ConvertToArrayObject(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromArray(%0)"; builtin_types.insert(itype.cname, itype); // Array_@generic @@ -3761,6 +3728,9 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.name = "Array_@generic"; itype.cname = itype.name; itype.cs_out = "%5return new %2(%0(%1));"; + // For generic Godot collections, Variant.From<T>/As<T> is slower, so we need this special case + itype.cs_variant_to_managed = "VariantUtils.ConvertToArrayObject(%0)"; + itype.cs_managed_to_variant = "VariantUtils.CreateFromArray(%0)"; builtin_types.insert(itype.cname, itype); // Dictionary @@ -3778,8 +3748,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.c_type_out = itype.cs_type; itype.c_type_is_disposable_struct = false; // [c_out] takes ownership itype.c_ret_needs_default_initialization = true; - itype.cs_variant_to_managed = "VariantUtils.ConvertToDictionaryObject(%0)"; - itype.cs_managed_to_variant = "VariantUtils.CreateFromDictionary(%0)"; builtin_types.insert(itype.cname, itype); // Dictionary_@generic @@ -3787,6 +3755,9 @@ void BindingsGenerator::_populate_builtin_type_interfaces() { itype.name = "Dictionary_@generic"; itype.cname = itype.name; itype.cs_out = "%5return new %2(%0(%1));"; + // For generic Godot collections, Variant.From<T>/As<T> is slower, so we need this special case + itype.cs_variant_to_managed = "VariantUtils.ConvertToDictionaryObject(%0)"; + itype.cs_managed_to_variant = "VariantUtils.CreateFromDictionary(%0)"; builtin_types.insert(itype.cname, itype); // void (fictitious type to represent the return type of methods that do not return anything) @@ -3852,8 +3823,6 @@ void BindingsGenerator::_populate_global_constants() { enum_itype.cname = ienum.cname; enum_itype.proxy_name = enum_itype.name; TypeInterface::postsetup_enum_type(enum_itype); - enum_itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToInt32(%0)"; - enum_itype.cs_managed_to_variant = "VariantUtils.CreateFromInt((int)%0)"; enum_types.insert(enum_itype.cname, enum_itype); int prefix_length = _determine_enum_prefix(ienum); @@ -3886,8 +3855,6 @@ void BindingsGenerator::_populate_global_constants() { enum_itype.cname = enum_cname; enum_itype.proxy_name = enum_itype.name; TypeInterface::postsetup_enum_type(enum_itype); - enum_itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToInt32(%0)"; - enum_itype.cs_managed_to_variant = "VariantUtils.CreateFromInt((int)%0)"; enum_types.insert(enum_itype.cname, enum_itype); } } diff --git a/modules/mono/editor/bindings_generator.h b/modules/mono/editor/bindings_generator.h index a479c44368..6d172f4fb8 100644 --- a/modules/mono/editor/bindings_generator.h +++ b/modules/mono/editor/bindings_generator.h @@ -209,7 +209,7 @@ class BindingsGenerator { String name; StringName cname; - int type_parameter_count; + int type_parameter_count = 0; /** * Identifier name of the base class. @@ -514,7 +514,12 @@ class BindingsGenerator { static void postsetup_enum_type(TypeInterface &r_enum_itype); - TypeInterface() {} + TypeInterface() { + static String default_cs_variant_to_managed = "VariantUtils.ConvertTo<%1>(%0)"; + static String default_cs_managed_to_variant = "VariantUtils.CreateFrom<%1>(%0)"; + cs_variant_to_managed = default_cs_variant_to_managed; + cs_managed_to_variant = default_cs_managed_to_variant; + } }; struct InternalCall { diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs index 1b9f63c22d..130776499b 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs @@ -418,8 +418,8 @@ namespace Godot.Collections { for (int i = 0; i < count; i++) { - object obj = Marshaling.ConvertVariantToManagedObject(NativeValue.DangerousSelfRef.Elements[i]); - array.SetValue(obj, index); + object boxedVariant = Variant.CreateCopyingBorrowed(NativeValue.DangerousSelfRef.Elements[i]); + array.SetValue(boxedVariant, index); index++; } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs index d6fad391b6..e6a8054ae2 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs @@ -159,7 +159,7 @@ namespace Godot.Bridge for (int i = 0; i < paramCount; i++) { - invokeParams[i] = Marshaling.ConvertVariantToManagedObjectOfType( + invokeParams[i] = DelegateUtils.RuntimeTypeConversionHelper.ConvertToObjectOfType( *args[i], parameters[i].ParameterType); } @@ -832,7 +832,8 @@ namespace Godot.Bridge } else { - interopProperties = ((godotsharp_property_info*)NativeMemory.Alloc((nuint)length, (nuint)sizeof(godotsharp_property_info)))!; + interopProperties = ((godotsharp_property_info*)NativeMemory.Alloc( + (nuint)length, (nuint)sizeof(godotsharp_property_info)))!; } try @@ -858,8 +859,8 @@ namespace Godot.Bridge addPropInfoFunc(scriptPtr, ¤tClassName, interopProperties, length); - // We're borrowing the StringName's without making an owning copy, so the - // managed collection needs to be kept alive until `addPropInfoFunc` returns. + // We're borrowing the native value of the StringName entries. + // The dictionary needs to be kept alive until `addPropInfoFunc` returns. GC.KeepAlive(properties); } finally @@ -884,12 +885,7 @@ namespace Godot.Bridge { // Careful with padding... public godot_string_name Name; // Not owned - public godot_variant Value; - - public void Dispose() - { - Value.Dispose(); - } + public godot_variant Value; // Not owned } [UnmanagedCallersOnly] @@ -928,10 +924,35 @@ namespace Godot.Bridge if (getGodotPropertyDefaultValuesMethod == null) return; - var defaultValues = (Dictionary<StringName, object>?) - getGodotPropertyDefaultValuesMethod.Invoke(null, null); + var defaultValuesObj = getGodotPropertyDefaultValuesMethod.Invoke(null, null); + + if (defaultValuesObj == null) + return; + + Dictionary<StringName, Variant> defaultValues; + + if (defaultValuesObj is Dictionary<StringName, object> defaultValuesLegacy) + { + // We have to support this for some time, otherwise this could cause data loss for projects + // built with previous releases. Ideally, we should remove this before Godot 4.0 stable. + + if (defaultValuesLegacy.Count <= 0) + return; - if (defaultValues == null || defaultValues.Count <= 0) + defaultValues = new(); + + foreach (var pair in defaultValuesLegacy) + { + defaultValues[pair.Key] = Variant.CreateTakingOwnershipOfDisposableValue( + DelegateUtils.RuntimeTypeConversionHelper.ConvertToVariant(pair.Value)); + } + } + else + { + defaultValues = (Dictionary<StringName, Variant>)defaultValuesObj; + } + + if (defaultValues.Count <= 0) return; int length = defaultValues.Count; @@ -952,7 +973,8 @@ namespace Godot.Bridge } else { - interopDefaultValues = ((godotsharp_property_def_val_pair*)NativeMemory.Alloc((nuint)length, (nuint)sizeof(godotsharp_property_def_val_pair)))!; + interopDefaultValues = ((godotsharp_property_def_val_pair*)NativeMemory.Alloc( + (nuint)length, (nuint)sizeof(godotsharp_property_def_val_pair)))!; } try @@ -963,7 +985,7 @@ namespace Godot.Bridge godotsharp_property_def_val_pair interopProperty = new() { Name = (godot_string_name)defaultValuePair.Key.NativeValue, // Not owned - Value = Marshaling.ConvertManagedObjectToVariant(defaultValuePair.Value) + Value = (godot_variant)defaultValuePair.Value.NativeVar // Not owned }; interopDefaultValues[i] = interopProperty; @@ -973,15 +995,12 @@ namespace Godot.Bridge addDefValFunc(scriptPtr, interopDefaultValues, length); - // We're borrowing the StringName's without making an owning copy, so the - // managed collection needs to be kept alive until `addDefValFunc` returns. + // We're borrowing the native value of the StringName and Variant entries. + // The dictionary needs to be kept alive until `addDefValFunc` returns. GC.KeepAlive(defaultValues); } finally { - for (int i = 0; i < length; i++) - interopDefaultValues[i].Dispose(); - if (!useStack) NativeMemory.Free(interopDefaultValues); } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs index d19e0c08f2..a3cfecfaa6 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs @@ -186,7 +186,7 @@ namespace Godot writer.Write(field.Name); var fieldValue = field.GetValue(target); - using var fieldValueVariant = Marshaling.ConvertManagedObjectToVariant(fieldValue); + using var fieldValueVariant = RuntimeTypeConversionHelper.ConvertToVariant(fieldValue); byte[] valueBuffer = VarToBytes(fieldValueVariant); writer.Write(valueBuffer.Length); writer.Write(valueBuffer); @@ -443,7 +443,14 @@ namespace Godot FieldInfo? fieldInfo = targetType.GetField(name, BindingFlags.Instance | BindingFlags.Public); - fieldInfo?.SetValue(recreatedTarget, GD.BytesToVar(valueBuffer)); + + if (fieldInfo != null) + { + var variantValue = GD.BytesToVar(valueBuffer); + object? managedValue = RuntimeTypeConversionHelper.ConvertToObjectOfType( + (godot_variant)variantValue.NativeVar, fieldInfo.FieldType); + fieldInfo.SetValue(recreatedTarget, managedValue); + } } @delegate = Delegate.CreateDelegate(delegateType, recreatedTarget, methodInfo, @@ -537,5 +544,269 @@ namespace Godot return type; } + + internal static class RuntimeTypeConversionHelper + { + [SuppressMessage("ReSharper", "RedundantNameQualifier")] + public static godot_variant ConvertToVariant(object? obj) + { + if (obj == null) + return default; + + switch (obj) + { + case bool @bool: + return VariantUtils.CreateFrom(@bool); + case char @char: + return VariantUtils.CreateFrom(@char); + case sbyte int8: + return VariantUtils.CreateFrom(int8); + case short int16: + return VariantUtils.CreateFrom(int16); + case int int32: + return VariantUtils.CreateFrom(int32); + case long int64: + return VariantUtils.CreateFrom(int64); + case byte uint8: + return VariantUtils.CreateFrom(uint8); + case ushort uint16: + return VariantUtils.CreateFrom(uint16); + case uint uint32: + return VariantUtils.CreateFrom(uint32); + case ulong uint64: + return VariantUtils.CreateFrom(uint64); + case float @float: + return VariantUtils.CreateFrom(@float); + case double @double: + return VariantUtils.CreateFrom(@double); + case Vector2 vector2: + return VariantUtils.CreateFrom(vector2); + case Vector2i vector2I: + return VariantUtils.CreateFrom(vector2I); + case Rect2 rect2: + return VariantUtils.CreateFrom(rect2); + case Rect2i rect2I: + return VariantUtils.CreateFrom(rect2I); + case Transform2D transform2D: + return VariantUtils.CreateFrom(transform2D); + case Vector3 vector3: + return VariantUtils.CreateFrom(vector3); + case Vector3i vector3I: + return VariantUtils.CreateFrom(vector3I); + case Vector4 vector4: + return VariantUtils.CreateFrom(vector4); + case Vector4i vector4I: + return VariantUtils.CreateFrom(vector4I); + case Basis basis: + return VariantUtils.CreateFrom(basis); + case Quaternion quaternion: + return VariantUtils.CreateFrom(quaternion); + case Transform3D transform3d: + return VariantUtils.CreateFrom(transform3d); + case Projection projection: + return VariantUtils.CreateFrom(projection); + case AABB aabb: + return VariantUtils.CreateFrom(aabb); + case Color color: + return VariantUtils.CreateFrom(color); + case Plane plane: + return VariantUtils.CreateFrom(plane); + case Callable callable: + return VariantUtils.CreateFrom(callable); + case SignalInfo signalInfo: + return VariantUtils.CreateFrom(signalInfo); + case string @string: + return VariantUtils.CreateFrom(@string); + case byte[] byteArray: + return VariantUtils.CreateFrom(byteArray); + case int[] int32Array: + return VariantUtils.CreateFrom(int32Array); + case long[] int64Array: + return VariantUtils.CreateFrom(int64Array); + case float[] floatArray: + return VariantUtils.CreateFrom(floatArray); + case double[] doubleArray: + return VariantUtils.CreateFrom(doubleArray); + case string[] stringArray: + return VariantUtils.CreateFrom(stringArray); + case Vector2[] vector2Array: + return VariantUtils.CreateFrom(vector2Array); + case Vector3[] vector3Array: + return VariantUtils.CreateFrom(vector3Array); + case Color[] colorArray: + return VariantUtils.CreateFrom(colorArray); + case StringName[] stringNameArray: + return VariantUtils.CreateFrom(stringNameArray); + case NodePath[] nodePathArray: + return VariantUtils.CreateFrom(nodePathArray); + case RID[] ridArray: + return VariantUtils.CreateFrom(ridArray); + case Godot.Object[] godotObjectArray: + return VariantUtils.CreateFrom(godotObjectArray); + case StringName stringName: + return VariantUtils.CreateFrom(stringName); + case NodePath nodePath: + return VariantUtils.CreateFrom(nodePath); + case RID rid: + return VariantUtils.CreateFrom(rid); + case Collections.Dictionary godotDictionary: + return VariantUtils.CreateFrom(godotDictionary); + case Collections.Array godotArray: + return VariantUtils.CreateFrom(godotArray); + case Variant variant: + return VariantUtils.CreateFrom(variant); + case Godot.Object godotObject: + return VariantUtils.CreateFrom(godotObject); + case Enum @enum: + return VariantUtils.CreateFrom(Convert.ToInt64(@enum)); + case Collections.IGenericGodotDictionary godotDictionary: + return VariantUtils.CreateFrom(godotDictionary.UnderlyingDictionary); + case Collections.IGenericGodotArray godotArray: + return VariantUtils.CreateFrom(godotArray.UnderlyingArray); + } + + GD.PushError("Attempted to convert an unmarshallable managed type to Variant. Name: '" + + obj.GetType().FullName + "."); + return new godot_variant(); + } + + private delegate object? ConvertToSystemObjectFunc(in godot_variant managed); + + [SuppressMessage("ReSharper", "RedundantNameQualifier")] + // ReSharper disable once RedundantNameQualifier + private static readonly System.Collections.Generic.Dictionary<Type, ConvertToSystemObjectFunc> + ToSystemObjectFuncByType = new() + { + [typeof(bool)] = (in godot_variant variant) => VariantUtils.ConvertTo<bool>(variant), + [typeof(char)] = (in godot_variant variant) => VariantUtils.ConvertTo<char>(variant), + [typeof(sbyte)] = (in godot_variant variant) => VariantUtils.ConvertTo<sbyte>(variant), + [typeof(short)] = (in godot_variant variant) => VariantUtils.ConvertTo<short>(variant), + [typeof(int)] = (in godot_variant variant) => VariantUtils.ConvertTo<int>(variant), + [typeof(long)] = (in godot_variant variant) => VariantUtils.ConvertTo<long>(variant), + [typeof(byte)] = (in godot_variant variant) => VariantUtils.ConvertTo<byte>(variant), + [typeof(ushort)] = (in godot_variant variant) => VariantUtils.ConvertTo<ushort>(variant), + [typeof(uint)] = (in godot_variant variant) => VariantUtils.ConvertTo<uint>(variant), + [typeof(ulong)] = (in godot_variant variant) => VariantUtils.ConvertTo<ulong>(variant), + [typeof(float)] = (in godot_variant variant) => VariantUtils.ConvertTo<float>(variant), + [typeof(double)] = (in godot_variant variant) => VariantUtils.ConvertTo<double>(variant), + [typeof(Vector2)] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector2>(variant), + [typeof(Vector2i)] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector2i>(variant), + [typeof(Rect2)] = (in godot_variant variant) => VariantUtils.ConvertTo<Rect2>(variant), + [typeof(Rect2i)] = (in godot_variant variant) => VariantUtils.ConvertTo<Rect2i>(variant), + [typeof(Transform2D)] = (in godot_variant variant) => VariantUtils.ConvertTo<Transform2D>(variant), + [typeof(Vector3)] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector3>(variant), + [typeof(Vector3i)] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector3i>(variant), + [typeof(Basis)] = (in godot_variant variant) => VariantUtils.ConvertTo<Basis>(variant), + [typeof(Quaternion)] = (in godot_variant variant) => VariantUtils.ConvertTo<Quaternion>(variant), + [typeof(Transform3D)] = (in godot_variant variant) => VariantUtils.ConvertTo<Transform3D>(variant), + [typeof(Vector4)] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector4>(variant), + [typeof(Vector4i)] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector4i>(variant), + [typeof(AABB)] = (in godot_variant variant) => VariantUtils.ConvertTo<AABB>(variant), + [typeof(Color)] = (in godot_variant variant) => VariantUtils.ConvertTo<Color>(variant), + [typeof(Plane)] = (in godot_variant variant) => VariantUtils.ConvertTo<Plane>(variant), + [typeof(Callable)] = (in godot_variant variant) => VariantUtils.ConvertTo<Callable>(variant), + [typeof(SignalInfo)] = (in godot_variant variant) => VariantUtils.ConvertTo<SignalInfo>(variant), + [typeof(string)] = (in godot_variant variant) => VariantUtils.ConvertTo<string>(variant), + [typeof(byte[])] = (in godot_variant variant) => VariantUtils.ConvertTo<byte[]>(variant), + [typeof(int[])] = (in godot_variant variant) => VariantUtils.ConvertTo<int[]>(variant), + [typeof(long[])] = (in godot_variant variant) => VariantUtils.ConvertTo<long[]>(variant), + [typeof(float[])] = (in godot_variant variant) => VariantUtils.ConvertTo<float[]>(variant), + [typeof(double[])] = (in godot_variant variant) => VariantUtils.ConvertTo<double[]>(variant), + [typeof(string[])] = (in godot_variant variant) => VariantUtils.ConvertTo<string[]>(variant), + [typeof(Vector2[])] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector2[]>(variant), + [typeof(Vector3[])] = (in godot_variant variant) => VariantUtils.ConvertTo<Vector3[]>(variant), + [typeof(Color[])] = (in godot_variant variant) => VariantUtils.ConvertTo<Color[]>(variant), + [typeof(StringName[])] = + (in godot_variant variant) => VariantUtils.ConvertTo<StringName[]>(variant), + [typeof(NodePath[])] = (in godot_variant variant) => VariantUtils.ConvertTo<NodePath[]>(variant), + [typeof(RID[])] = (in godot_variant variant) => VariantUtils.ConvertTo<RID[]>(variant), + [typeof(StringName)] = (in godot_variant variant) => VariantUtils.ConvertTo<StringName>(variant), + [typeof(NodePath)] = (in godot_variant variant) => VariantUtils.ConvertTo<NodePath>(variant), + [typeof(RID)] = (in godot_variant variant) => VariantUtils.ConvertTo<RID>(variant), + [typeof(Godot.Collections.Dictionary)] = (in godot_variant variant) => + VariantUtils.ConvertTo<Godot.Collections.Dictionary>(variant), + [typeof(Godot.Collections.Array)] = + (in godot_variant variant) => VariantUtils.ConvertTo<Godot.Collections.Array>(variant), + [typeof(Variant)] = (in godot_variant variant) => VariantUtils.ConvertTo<Variant>(variant), + }; + + [SuppressMessage("ReSharper", "RedundantNameQualifier")] + public static object? ConvertToObjectOfType(in godot_variant variant, Type type) + { + if (ToSystemObjectFuncByType.TryGetValue(type, out var func)) + return func(variant); + + if (typeof(Godot.Object).IsAssignableFrom(type)) + return Convert.ChangeType(VariantUtils.ConvertTo<Godot.Object>(variant), type); + + if (type.IsEnum) + { + var enumUnderlyingType = type.GetEnumUnderlyingType(); + + switch (Type.GetTypeCode(enumUnderlyingType)) + { + case TypeCode.SByte: + return Enum.ToObject(type, VariantUtils.ConvertToInt8(variant)); + case TypeCode.Int16: + return Enum.ToObject(type, VariantUtils.ConvertToInt16(variant)); + case TypeCode.Int32: + return Enum.ToObject(type, VariantUtils.ConvertToInt32(variant)); + case TypeCode.Int64: + return Enum.ToObject(type, VariantUtils.ConvertToInt64(variant)); + case TypeCode.Byte: + return Enum.ToObject(type, VariantUtils.ConvertToUInt8(variant)); + case TypeCode.UInt16: + return Enum.ToObject(type, VariantUtils.ConvertToUInt16(variant)); + case TypeCode.UInt32: + return Enum.ToObject(type, VariantUtils.ConvertToUInt32(variant)); + case TypeCode.UInt64: + return Enum.ToObject(type, VariantUtils.ConvertToUInt64(variant)); + default: + { + GD.PushError( + "Attempted to convert Variant to enum value of unsupported underlying type. Name: " + + type.FullName + " : " + enumUnderlyingType.FullName + "."); + return null; + } + } + } + + if (type.IsGenericType) + { + var genericTypeDef = type.GetGenericTypeDefinition(); + + if (genericTypeDef == typeof(Godot.Collections.Dictionary<,>)) + { + var ctor = type.GetConstructor(BindingFlags.Default, + new[] { typeof(Godot.Collections.Dictionary) }); + + if (ctor == null) + throw new InvalidOperationException("Dictionary constructor not found"); + + return ctor.Invoke(new object?[] + { + VariantUtils.ConvertTo<Godot.Collections.Dictionary>(variant) + }); + } + + if (genericTypeDef == typeof(Godot.Collections.Array<>)) + { + var ctor = type.GetConstructor(BindingFlags.Default, + new[] { typeof(Godot.Collections.Array) }); + + if (ctor == null) + throw new InvalidOperationException("Array constructor not found"); + + return ctor.Invoke(new object?[] + { + VariantUtils.ConvertTo<Godot.Collections.Array>(variant) + }); + } + } + + GD.PushError($"Attempted to convert Variant to unsupported type. Name: {type.FullName}."); + return null; + } + } } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs index d2a80d0e92..6176093bc1 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs @@ -1,5 +1,7 @@ using System; using System.Runtime.InteropServices; +using Godot.Collections; +using Array = System.Array; // ReSharper disable InconsistentNaming @@ -148,6 +150,15 @@ namespace Godot.NativeInterop { if (typeof(Godot.Object).IsAssignableFrom(type)) return Variant.Type.Object; + + // We use `IsAssignableFrom` with our helper interfaces to detect generic Godot collections + // because `GetGenericTypeDefinition` is not supported in NativeAOT reflection-free mode. + + if (typeof(IGenericGodotDictionary).IsAssignableFrom(type)) + return Variant.Type.Dictionary; + + if (typeof(IGenericGodotArray).IsAssignableFrom(type)) + return Variant.Type.Array; } else if (type == typeof(Variant)) { @@ -183,512 +194,6 @@ namespace Godot.NativeInterop return Variant.Type.Nil; } - /* TODO: Reflection and type checking each time is slow. This will be replaced with source generators. */ - public static godot_variant ConvertManagedObjectToVariant(object? p_obj) - { - if (p_obj == null) - return new godot_variant(); - - switch (p_obj) - { - case bool @bool: - return VariantUtils.CreateFromBool(@bool); - case char @char: - return VariantUtils.CreateFromInt(@char); - case sbyte @int8: - return VariantUtils.CreateFromInt(@int8); - case short @int16: - return VariantUtils.CreateFromInt(@int16); - case int @int32: - return VariantUtils.CreateFromInt(@int32); - case long @int64: - return VariantUtils.CreateFromInt(@int64); - case byte @uint8: - return VariantUtils.CreateFromInt(@uint8); - case ushort @uint16: - return VariantUtils.CreateFromInt(@uint16); - case uint @uint32: - return VariantUtils.CreateFromInt(@uint32); - case ulong @uint64: - return VariantUtils.CreateFromInt(@uint64); - case float @float: - return VariantUtils.CreateFromFloat(@float); - case double @double: - return VariantUtils.CreateFromFloat(@double); - case Vector2 @vector2: - return VariantUtils.CreateFromVector2(@vector2); - case Vector2i @vector2i: - return VariantUtils.CreateFromVector2i(@vector2i); - case Rect2 @rect2: - return VariantUtils.CreateFromRect2(@rect2); - case Rect2i @rect2i: - return VariantUtils.CreateFromRect2i(@rect2i); - case Transform2D @transform2D: - return VariantUtils.CreateFromTransform2D(@transform2D); - case Vector3 @vector3: - return VariantUtils.CreateFromVector3(@vector3); - case Vector3i @vector3i: - return VariantUtils.CreateFromVector3i(@vector3i); - case Vector4 @vector4: - return VariantUtils.CreateFromVector4(@vector4); - case Vector4i @vector4i: - return VariantUtils.CreateFromVector4i(@vector4i); - case Basis @basis: - return VariantUtils.CreateFromBasis(@basis); - case Quaternion @quaternion: - return VariantUtils.CreateFromQuaternion(@quaternion); - case Transform3D @transform3d: - return VariantUtils.CreateFromTransform3D(@transform3d); - case Projection @projection: - return VariantUtils.CreateFromProjection(@projection); - case AABB @aabb: - return VariantUtils.CreateFromAABB(@aabb); - case Color @color: - return VariantUtils.CreateFromColor(@color); - case Plane @plane: - return VariantUtils.CreateFromPlane(@plane); - case Callable @callable: - return VariantUtils.CreateFromCallable(@callable); - case SignalInfo @signalInfo: - return VariantUtils.CreateFromSignalInfo(@signalInfo); - case Enum @enum: - return VariantUtils.CreateFromInt(Convert.ToInt64(@enum)); - case string @string: - return VariantUtils.CreateFromString(@string); - case byte[] byteArray: - return VariantUtils.CreateFromPackedByteArray(byteArray); - case int[] int32Array: - return VariantUtils.CreateFromPackedInt32Array(int32Array); - case long[] int64Array: - return VariantUtils.CreateFromPackedInt64Array(int64Array); - case float[] floatArray: - return VariantUtils.CreateFromPackedFloat32Array(floatArray); - case double[] doubleArray: - return VariantUtils.CreateFromPackedFloat64Array(doubleArray); - case string[] stringArray: - return VariantUtils.CreateFromPackedStringArray(stringArray); - case Vector2[] vector2Array: - return VariantUtils.CreateFromPackedVector2Array(vector2Array); - case Vector3[] vector3Array: - return VariantUtils.CreateFromPackedVector3Array(vector3Array); - case Color[] colorArray: - return VariantUtils.CreateFromPackedColorArray(colorArray); - case StringName[] stringNameArray: - return VariantUtils.CreateFromSystemArrayOfStringName(stringNameArray); - case NodePath[] nodePathArray: - return VariantUtils.CreateFromSystemArrayOfNodePath(nodePathArray); - case RID[] ridArray: - return VariantUtils.CreateFromSystemArrayOfRID(ridArray); - case Godot.Object[] godotObjectArray: - return VariantUtils.CreateFromSystemArrayOfGodotObject(godotObjectArray); - case Godot.Object godotObject: - return VariantUtils.CreateFromGodotObject(godotObject); - case StringName stringName: - return VariantUtils.CreateFromStringName(stringName); - case NodePath nodePath: - return VariantUtils.CreateFromNodePath(nodePath); - case RID rid: - return VariantUtils.CreateFromRID(rid); - case Collections.Dictionary godotDictionary: - return VariantUtils.CreateFromDictionary(godotDictionary); - case Collections.Array godotArray: - return VariantUtils.CreateFromArray(godotArray); - case Collections.IGenericGodotDictionary godotDictionary: - return VariantUtils.CreateFromDictionary(godotDictionary.UnderlyingDictionary); - case Collections.IGenericGodotArray godotArray: - return VariantUtils.CreateFromArray(godotArray.UnderlyingArray); - case Variant variant: - return NativeFuncs.godotsharp_variant_new_copy((godot_variant)variant.NativeVar); - } - - GD.PushError("Attempted to convert an unmarshallable managed type to Variant. Name: '" + - p_obj.GetType().FullName + "."); - return new godot_variant(); - } - - public static object? ConvertVariantToManagedObjectOfType(in godot_variant p_var, Type type) - { - // This function is only needed to set the value of properties. Fields have their own implementation, set_value_from_variant. - switch (Type.GetTypeCode(type)) - { - case TypeCode.Boolean: - return VariantUtils.ConvertToBool(p_var); - case TypeCode.Char: - return VariantUtils.ConvertToChar(p_var); - case TypeCode.SByte: - return VariantUtils.ConvertToInt8(p_var); - case TypeCode.Int16: - return VariantUtils.ConvertToInt16(p_var); - case TypeCode.Int32: - return VariantUtils.ConvertToInt32(p_var); - case TypeCode.Int64: - return VariantUtils.ConvertToInt64(p_var); - case TypeCode.Byte: - return VariantUtils.ConvertToUInt8(p_var); - case TypeCode.UInt16: - return VariantUtils.ConvertToUInt16(p_var); - case TypeCode.UInt32: - return VariantUtils.ConvertToUInt32(p_var); - case TypeCode.UInt64: - return VariantUtils.ConvertToUInt64(p_var); - case TypeCode.Single: - return VariantUtils.ConvertToFloat32(p_var); - case TypeCode.Double: - return VariantUtils.ConvertToFloat64(p_var); - case TypeCode.String: - return VariantUtils.ConvertToStringObject(p_var); - default: - { - if (type == typeof(Vector2)) - return VariantUtils.ConvertToVector2(p_var); - - if (type == typeof(Vector2i)) - return VariantUtils.ConvertToVector2i(p_var); - - if (type == typeof(Rect2)) - return VariantUtils.ConvertToRect2(p_var); - - if (type == typeof(Rect2i)) - return VariantUtils.ConvertToRect2i(p_var); - - if (type == typeof(Transform2D)) - return VariantUtils.ConvertToTransform2D(p_var); - - if (type == typeof(Vector3)) - return VariantUtils.ConvertToVector3(p_var); - - if (type == typeof(Vector3i)) - return VariantUtils.ConvertToVector3i(p_var); - - if (type == typeof(Vector4)) - return VariantUtils.ConvertToVector4(p_var); - - if (type == typeof(Vector4i)) - return VariantUtils.ConvertToVector4i(p_var); - - if (type == typeof(Basis)) - return VariantUtils.ConvertToBasis(p_var); - - if (type == typeof(Quaternion)) - return VariantUtils.ConvertToQuaternion(p_var); - - if (type == typeof(Transform3D)) - return VariantUtils.ConvertToTransform3D(p_var); - - if (type == typeof(Projection)) - return VariantUtils.ConvertToProjection(p_var); - - if (type == typeof(AABB)) - return VariantUtils.ConvertToAABB(p_var); - - if (type == typeof(Color)) - return VariantUtils.ConvertToColor(p_var); - - if (type == typeof(Plane)) - return VariantUtils.ConvertToPlane(p_var); - - if (type == typeof(Callable)) - return VariantUtils.ConvertToCallableManaged(p_var); - - if (type == typeof(SignalInfo)) - return VariantUtils.ConvertToSignalInfo(p_var); - - if (type.IsEnum) - { - var enumUnderlyingType = type.GetEnumUnderlyingType(); - switch (Type.GetTypeCode(enumUnderlyingType)) - { - case TypeCode.SByte: - return VariantUtils.ConvertToInt8(p_var); - case TypeCode.Int16: - return VariantUtils.ConvertToInt16(p_var); - case TypeCode.Int32: - return VariantUtils.ConvertToInt32(p_var); - case TypeCode.Int64: - return VariantUtils.ConvertToInt64(p_var); - case TypeCode.Byte: - return VariantUtils.ConvertToUInt8(p_var); - case TypeCode.UInt16: - return VariantUtils.ConvertToUInt16(p_var); - case TypeCode.UInt32: - return VariantUtils.ConvertToUInt32(p_var); - case TypeCode.UInt64: - return VariantUtils.ConvertToUInt64(p_var); - default: - { - GD.PushError( - "Attempted to convert Variant to enum value of unsupported underlying type. Name: " + - type.FullName + " : " + enumUnderlyingType.FullName + "."); - return null; - } - } - } - - if (type.IsArray || type.IsSZArray) - { - return ConvertVariantToSystemArrayOfType(p_var, type); - } - else if (type.IsGenericType) - { - if (typeof(Godot.Object).IsAssignableFrom(type)) - { - var godotObject = VariantUtils.ConvertToGodotObject(p_var); - - if (!type.IsInstanceOfType(godotObject)) - { - GD.PushError("Invalid cast when marshaling Godot.Object type." + - $" `{godotObject.GetType()}` is not assignable to `{type.FullName}`."); - return null; - } - - return godotObject; - } - - return null; - } - else if (type == typeof(Variant)) - { - return Variant.CreateCopyingBorrowed(p_var); - } - - if (ConvertVariantToManagedObjectOfClass(p_var, type, out object? res)) - return res; - - break; - } - } - - GD.PushError("Attempted to convert Variant to unsupported type. Name: " + - type.FullName + "."); - return null; - } - - private static object? ConvertVariantToSystemArrayOfType(in godot_variant p_var, Type type) - { - if (type == typeof(byte[])) - return VariantUtils.ConvertAsPackedByteArrayToSystemArray(p_var); - - if (type == typeof(int[])) - return VariantUtils.ConvertAsPackedInt32ArrayToSystemArray(p_var); - - if (type == typeof(long[])) - return VariantUtils.ConvertAsPackedInt64ArrayToSystemArray(p_var); - - if (type == typeof(float[])) - return VariantUtils.ConvertAsPackedFloat32ArrayToSystemArray(p_var); - - if (type == typeof(double[])) - return VariantUtils.ConvertAsPackedFloat64ArrayToSystemArray(p_var); - - if (type == typeof(string[])) - return VariantUtils.ConvertAsPackedStringArrayToSystemArray(p_var); - - if (type == typeof(Vector2[])) - return VariantUtils.ConvertAsPackedVector2ArrayToSystemArray(p_var); - - if (type == typeof(Vector3[])) - return VariantUtils.ConvertAsPackedVector3ArrayToSystemArray(p_var); - - if (type == typeof(Color[])) - return VariantUtils.ConvertAsPackedColorArrayToSystemArray(p_var); - - if (type == typeof(StringName[])) - return VariantUtils.ConvertToSystemArrayOfStringName(p_var); - - if (type == typeof(NodePath[])) - return VariantUtils.ConvertToSystemArrayOfNodePath(p_var); - - if (type == typeof(RID[])) - return VariantUtils.ConvertToSystemArrayOfRID(p_var); - - if (typeof(Godot.Object[]).IsAssignableFrom(type)) - return VariantUtils.ConvertToSystemArrayOfGodotObject(p_var, type); - - GD.PushError("Attempted to convert Variant to array of unsupported element type. Name: " + - type.GetElementType()!.FullName + "."); - return null; - } - - private static bool ConvertVariantToManagedObjectOfClass(in godot_variant p_var, Type type, - out object? res) - { - if (typeof(Godot.Object).IsAssignableFrom(type)) - { - if (p_var.Type == Variant.Type.Nil) - { - res = null; - return true; - } - - if (p_var.Type != Variant.Type.Object) - { - GD.PushError("Invalid cast when marshaling Godot.Object type." + - $" Variant type is `{p_var.Type}`; expected `{p_var.Object}`."); - res = null; - return true; - } - - var godotObjectPtr = VariantUtils.ConvertToGodotObjectPtr(p_var); - - if (godotObjectPtr == IntPtr.Zero) - { - res = null; - return true; - } - - var godotObject = InteropUtils.UnmanagedGetManaged(godotObjectPtr); - - if (!type.IsInstanceOfType(godotObject)) - { - GD.PushError("Invalid cast when marshaling Godot.Object type." + - $" `{godotObject.GetType()}` is not assignable to `{type.FullName}`."); - res = null; - return false; - } - - res = godotObject; - return true; - } - - if (typeof(StringName) == type) - { - res = VariantUtils.ConvertToStringNameObject(p_var); - return true; - } - - if (typeof(NodePath) == type) - { - res = VariantUtils.ConvertToNodePathObject(p_var); - return true; - } - - if (typeof(RID) == type) - { - res = VariantUtils.ConvertToRID(p_var); - return true; - } - - if (typeof(Collections.Dictionary) == type) - { - res = VariantUtils.ConvertToDictionaryObject(p_var); - return true; - } - - if (typeof(Collections.Array) == type) - { - res = VariantUtils.ConvertToArrayObject(p_var); - return true; - } - - res = null; - return false; - } - - public static unsafe object? ConvertVariantToManagedObject(in godot_variant p_var) - { - switch (p_var.Type) - { - case Variant.Type.Bool: - return p_var.Bool.ToBool(); - case Variant.Type.Int: - return p_var.Int; - case Variant.Type.Float: - { -#if REAL_T_IS_DOUBLE - return p_var.Float; -#else - return (float)p_var.Float; -#endif - } - case Variant.Type.String: - return ConvertStringToManaged(p_var.String); - case Variant.Type.Vector2: - return p_var.Vector2; - case Variant.Type.Vector2i: - return p_var.Vector2i; - case Variant.Type.Rect2: - return p_var.Rect2; - case Variant.Type.Rect2i: - return p_var.Rect2i; - case Variant.Type.Vector3: - return p_var.Vector3; - case Variant.Type.Vector3i: - return p_var.Vector3i; - case Variant.Type.Transform2d: - return *p_var.Transform2D; - case Variant.Type.Vector4: - return p_var.Vector4; - case Variant.Type.Vector4i: - return p_var.Vector4i; - case Variant.Type.Plane: - return p_var.Plane; - case Variant.Type.Quaternion: - return p_var.Quaternion; - case Variant.Type.Aabb: - return *p_var.AABB; - case Variant.Type.Basis: - return *p_var.Basis; - case Variant.Type.Transform3d: - return *p_var.Transform3D; - case Variant.Type.Projection: - return *p_var.Projection; - case Variant.Type.Color: - return p_var.Color; - case Variant.Type.StringName: - { - // The Variant owns the value, so we need to make a copy - return StringName.CreateTakingOwnershipOfDisposableValue( - NativeFuncs.godotsharp_string_name_new_copy(p_var.StringName)); - } - case Variant.Type.NodePath: - { - // The Variant owns the value, so we need to make a copy - return NodePath.CreateTakingOwnershipOfDisposableValue( - NativeFuncs.godotsharp_node_path_new_copy(p_var.NodePath)); - } - case Variant.Type.Rid: - return p_var.RID; - case Variant.Type.Object: - return InteropUtils.UnmanagedGetManaged(p_var.Object); - case Variant.Type.Callable: - return ConvertCallableToManaged(p_var.Callable); - case Variant.Type.Signal: - return ConvertSignalToManaged(p_var.Signal); - case Variant.Type.Dictionary: - { - // The Variant owns the value, so we need to make a copy - return Collections.Dictionary.CreateTakingOwnershipOfDisposableValue( - NativeFuncs.godotsharp_dictionary_new_copy(p_var.Dictionary)); - } - case Variant.Type.Array: - { - // The Variant owns the value, so we need to make a copy - return Collections.Array.CreateTakingOwnershipOfDisposableValue( - NativeFuncs.godotsharp_array_new_copy(p_var.Array)); - } - case Variant.Type.PackedByteArray: - return VariantUtils.ConvertAsPackedByteArrayToSystemArray(p_var); - case Variant.Type.PackedInt32Array: - return VariantUtils.ConvertAsPackedInt32ArrayToSystemArray(p_var); - case Variant.Type.PackedInt64Array: - return VariantUtils.ConvertAsPackedInt64ArrayToSystemArray(p_var); - case Variant.Type.PackedFloat32Array: - return VariantUtils.ConvertAsPackedFloat32ArrayToSystemArray(p_var); - case Variant.Type.PackedFloat64Array: - return VariantUtils.ConvertAsPackedFloat64ArrayToSystemArray(p_var); - case Variant.Type.PackedStringArray: - return VariantUtils.ConvertAsPackedStringArrayToSystemArray(p_var); - case Variant.Type.PackedVector2Array: - return VariantUtils.ConvertAsPackedVector2ArrayToSystemArray(p_var); - case Variant.Type.PackedVector3Array: - return VariantUtils.ConvertAsPackedVector3ArrayToSystemArray(p_var); - case Variant.Type.PackedColorArray: - return VariantUtils.ConvertAsPackedColorArrayToSystemArray(p_var); - default: - return null; - } - } - // String public static unsafe godot_string ConvertStringToNative(string? p_mono_string) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs index 694da6db77..80ef2a1ea1 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs @@ -4,6 +4,8 @@ using System.Runtime.CompilerServices; namespace Godot.NativeInterop; +#nullable enable + public partial class VariantUtils { private static Exception UnsupportedType<T>() => new InvalidOperationException( diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs index 237a4da364..49a363cef2 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs @@ -109,16 +109,50 @@ public partial struct Variant : IDisposable public override string ToString() => AsString(); - public object? Obj - { - get + public object? Obj => + _obj ??= NativeVar.DangerousSelfRef.Type switch { - if (_obj == null) - _obj = Marshaling.ConvertVariantToManagedObject((godot_variant)NativeVar); - - return _obj; - } - } + Type.Bool => AsBool(), + Type.Int => AsInt64(), + Type.Float => AsDouble(), + Type.String => AsString(), + Type.Vector2 => AsVector2(), + Type.Vector2i => AsVector2i(), + Type.Rect2 => AsRect2(), + Type.Rect2i => AsRect2i(), + Type.Vector3 => AsVector3(), + Type.Vector3i => AsVector3i(), + Type.Transform2d => AsTransform2D(), + Type.Vector4 => AsVector4(), + Type.Vector4i => AsVector4i(), + Type.Plane => AsPlane(), + Type.Quaternion => AsQuaternion(), + Type.Aabb => AsAABB(), + Type.Basis => AsBasis(), + Type.Transform3d => AsTransform3D(), + Type.Projection => AsProjection(), + Type.Color => AsColor(), + Type.StringName => AsStringName(), + Type.NodePath => AsNodePath(), + Type.Rid => AsRID(), + Type.Object => AsGodotObject(), + Type.Callable => AsCallable(), + Type.Signal => AsSignalInfo(), + Type.Dictionary => AsGodotDictionary(), + Type.Array => AsGodotArray(), + Type.PackedByteArray => AsByteArray(), + Type.PackedInt32Array => AsInt32Array(), + Type.PackedInt64Array => AsInt64Array(), + Type.PackedFloat32Array => AsFloat32Array(), + Type.PackedFloat64Array => AsFloat64Array(), + Type.PackedStringArray => AsStringArray(), + Type.PackedVector2Array => AsVector2Array(), + Type.PackedVector3Array => AsVector3Array(), + Type.PackedColorArray => AsColorArray(), + Type.Nil => null, + Type.Max or _ => + throw new InvalidOperationException($"Invalid Variant type: {NativeVar.DangerousSelfRef.Type}"), + }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Variant From<[MustBeVariant] T>(in T from) => diff --git a/modules/multiplayer/doc_classes/MultiplayerSynchronizer.xml b/modules/multiplayer/doc_classes/MultiplayerSynchronizer.xml index 7ed6255a62..af7c345f15 100644 --- a/modules/multiplayer/doc_classes/MultiplayerSynchronizer.xml +++ b/modules/multiplayer/doc_classes/MultiplayerSynchronizer.xml @@ -17,7 +17,7 @@ <param index="0" name="filter" type="Callable" /> <description> Adds a peer visibility filter for this synchronizer. - [code]filter[/code] should take a peer id [int] and return a [bool]. + [code]filter[/code] should take a peer ID [int] and return a [bool]. </description> </method> <method name="get_visibility_for" qualifiers="const"> diff --git a/modules/multiplayer/scene_rpc_interface.cpp b/modules/multiplayer/scene_rpc_interface.cpp index dbf2b3751e..a7e29dfcc7 100644 --- a/modules/multiplayer/scene_rpc_interface.cpp +++ b/modules/multiplayer/scene_rpc_interface.cpp @@ -82,7 +82,7 @@ void SceneRPCInterface::_parse_rpc_config(const Variant &p_config, bool p_for_no Array names = config.keys(); names.sort(); // Ensure ID order for (int i = 0; i < names.size(); i++) { - ERR_CONTINUE(names[i].get_type() != Variant::STRING); + ERR_CONTINUE(names[i].get_type() != Variant::STRING && names[i].get_type() != Variant::STRING_NAME); String name = names[i].operator String(); ERR_CONTINUE(config[name].get_type() != Variant::DICTIONARY); ERR_CONTINUE(!config[name].operator Dictionary().has("rpc_mode")); diff --git a/modules/openxr/config.py b/modules/openxr/config.py index 279168cc59..e503f12739 100644 --- a/modules/openxr/config.py +++ b/modules/openxr/config.py @@ -1,6 +1,6 @@ def can_build(env, platform): if platform in ("linuxbsd", "windows", "android"): - return env["openxr"] + return env["openxr"] and not env["disable_3d"] else: # not supported on these platforms return False diff --git a/modules/openxr/extensions/openxr_android_extension.cpp b/modules/openxr/extensions/openxr_android_extension.cpp index ea539f2053..753fc5fa89 100644 --- a/modules/openxr/extensions/openxr_android_extension.cpp +++ b/modules/openxr/extensions/openxr_android_extension.cpp @@ -47,11 +47,15 @@ OpenXRAndroidExtension *OpenXRAndroidExtension::get_singleton() { OpenXRAndroidExtension::OpenXRAndroidExtension(OpenXRAPI *p_openxr_api) : OpenXRExtensionWrapper(p_openxr_api) { singleton = this; - request_extensions[XR_KHR_LOADER_INIT_ANDROID_EXTENSION_NAME] = nullptr; // must be available + request_extensions[XR_KHR_LOADER_INIT_ANDROID_EXTENSION_NAME] = &loader_init_extension_available; request_extensions[XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME] = &create_instance_extension_available; } void OpenXRAndroidExtension::on_before_instance_created() { + if (!loader_init_extension_available) { + print_line("OpenXR: XR_KHR_loader_init_android is not reported as available - trying to initialize anyway..."); + } + EXT_INIT_XR_FUNC(xrInitializeLoaderKHR); JNIEnv *env = get_jni_env(); diff --git a/modules/openxr/extensions/openxr_android_extension.h b/modules/openxr/extensions/openxr_android_extension.h index ca6011559a..087b634756 100644 --- a/modules/openxr/extensions/openxr_android_extension.h +++ b/modules/openxr/extensions/openxr_android_extension.h @@ -48,6 +48,7 @@ public: private: static OpenXRAndroidExtension *singleton; + bool loader_init_extension_available = false; bool create_instance_extension_available = false; // Initialize the loader diff --git a/modules/openxr/extensions/openxr_opengl_extension.cpp b/modules/openxr/extensions/openxr_opengl_extension.cpp index 234c5f8391..3b7c130149 100644 --- a/modules/openxr/extensions/openxr_opengl_extension.cpp +++ b/modules/openxr/extensions/openxr_opengl_extension.cpp @@ -136,7 +136,7 @@ void *OpenXROpenGLExtension::set_session_create_and_get_next_pointer(void *p_nex graphics_binding_gl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_ES_ANDROID_KHR; graphics_binding_gl.next = p_next_pointer; - graphics_binding_gl.display = eglGetCurrentDisplay(); + graphics_binding_gl.display = (void *)display_server->window_get_native_handle(DisplayServer::DISPLAY_HANDLE); graphics_binding_gl.config = (EGLConfig)0; // https://github.com/KhronosGroup/OpenXR-SDK-Source/blob/master/src/tests/hello_xr/graphicsplugin_opengles.cpp#L122 graphics_binding_gl.context = (void *)display_server->window_get_native_handle(DisplayServer::OPENGL_CONTEXT); #else diff --git a/modules/openxr/openxr_api.cpp b/modules/openxr/openxr_api.cpp index 59d3865acd..b652ca4617 100644 --- a/modules/openxr/openxr_api.cpp +++ b/modules/openxr/openxr_api.cpp @@ -740,9 +740,10 @@ bool OpenXRAPI::create_swapchains() { ERR_FAIL_NULL_V_MSG(projection_views, false, "OpenXR Couldn't allocate memory for projection views"); // We create our depth swapchain if: + // - we've enabled submitting depth buffer // - we support our depth layer extension // - we have our spacewarp extension (not yet implemented) - if (OpenXRCompositionLayerDepthExtension::get_singleton()->is_available()) { + if (submit_depth_buffer && OpenXRCompositionLayerDepthExtension::get_singleton()->is_available()) { // Build a vector with swapchain formats we want to use, from best fit to worst Vector<int64_t> usable_swapchain_formats; int64_t swapchain_format_to_use = 0; @@ -790,13 +791,13 @@ bool OpenXRAPI::create_swapchains() { projection_views[i].subImage.imageRect.extent.width = recommended_size.width; projection_views[i].subImage.imageRect.extent.height = recommended_size.height; - if (OpenXRCompositionLayerDepthExtension::get_singleton()->is_available() && depth_views) { + if (submit_depth_buffer && OpenXRCompositionLayerDepthExtension::get_singleton()->is_available() && depth_views) { projection_views[i].next = &depth_views[i]; depth_views[i].type = XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR; depth_views[i].next = nullptr; depth_views[i].subImage.swapchain = swapchains[OPENXR_SWAPCHAIN_DEPTH].swapchain; - depth_views[i].subImage.imageArrayIndex = 0; + depth_views[i].subImage.imageArrayIndex = i; depth_views[i].subImage.imageRect.offset.x = 0; depth_views[i].subImage.imageRect.offset.y = 0; depth_views[i].subImage.imageRect.extent.width = recommended_size.width; @@ -1066,6 +1067,30 @@ bool OpenXRAPI::on_state_exiting() { return true; } +void OpenXRAPI::set_form_factor(XrFormFactor p_form_factor) { + ERR_FAIL_COND(is_initialized()); + + form_factor = p_form_factor; +} + +void OpenXRAPI::set_view_configuration(XrViewConfigurationType p_view_configuration) { + ERR_FAIL_COND(is_initialized()); + + view_configuration = p_view_configuration; +} + +void OpenXRAPI::set_reference_space(XrReferenceSpaceType p_reference_space) { + ERR_FAIL_COND(is_initialized()); + + reference_space = p_reference_space; +} + +void OpenXRAPI::set_submit_depth_buffer(bool p_submit_depth_buffer) { + ERR_FAIL_COND(is_initialized()); + + submit_depth_buffer = p_submit_depth_buffer; +} + bool OpenXRAPI::is_initialized() { return (instance != XR_NULL_HANDLE); } @@ -1684,7 +1709,7 @@ RID OpenXRAPI::get_color_texture() { } RID OpenXRAPI::get_depth_texture() { - if (swapchains[OPENXR_SWAPCHAIN_DEPTH].image_acquired) { + if (submit_depth_buffer && swapchains[OPENXR_SWAPCHAIN_DEPTH].image_acquired) { return graphics_extension->get_texture(swapchains[OPENXR_SWAPCHAIN_DEPTH].swapchain_graphics_data, swapchains[OPENXR_SWAPCHAIN_DEPTH].image_index); } else { return RID(); @@ -1862,6 +1887,8 @@ OpenXRAPI::OpenXRAPI() { default: break; } + + submit_depth_buffer = GLOBAL_GET("xr/openxr/submit_depth_buffer"); } // reset a few things that can't be done in our class definition diff --git a/modules/openxr/openxr_api.h b/modules/openxr/openxr_api.h index 5dce749351..ac4bbff94c 100644 --- a/modules/openxr/openxr_api.h +++ b/modules/openxr/openxr_api.h @@ -104,6 +104,7 @@ private: XrViewConfigurationType view_configuration = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; XrReferenceSpaceType reference_space = XR_REFERENCE_SPACE_TYPE_STAGE; // XrEnvironmentBlendMode environment_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; + bool submit_depth_buffer = false; // if set to true we submit depth buffers to OpenXR if a suitable extension is enabled. // state XrInstance instance = XR_NULL_HANDLE; @@ -312,6 +313,18 @@ public: void set_xr_interface(OpenXRInterface *p_xr_interface); void register_extension_wrapper(OpenXRExtensionWrapper *p_extension_wrapper); + void set_form_factor(XrFormFactor p_form_factor); + XrFormFactor get_form_factor() const { return form_factor; } + + void set_view_configuration(XrViewConfigurationType p_view_configuration); + XrViewConfigurationType get_view_configuration() const { return view_configuration; } + + void set_reference_space(XrReferenceSpaceType p_reference_space); + XrReferenceSpaceType get_reference_space() const { return reference_space; } + + void set_submit_depth_buffer(bool p_submit_depth_buffer); + bool get_submit_depth_buffer() const { return submit_depth_buffer; } + bool is_initialized(); bool is_running(); bool initialize(const String &p_rendering_driver); diff --git a/modules/openxr/openxr_interface.cpp b/modules/openxr/openxr_interface.cpp index 77660eb6f0..40190ab2f3 100644 --- a/modules/openxr/openxr_interface.cpp +++ b/modules/openxr/openxr_interface.cpp @@ -45,7 +45,7 @@ void OpenXRInterface::_bind_methods() { // Display refresh rate ClassDB::bind_method(D_METHOD("get_display_refresh_rate"), &OpenXRInterface::get_display_refresh_rate); ClassDB::bind_method(D_METHOD("set_display_refresh_rate", "refresh_rate"), &OpenXRInterface::set_display_refresh_rate); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "display_refresh_rate"), "set_display_refresh_rate", "get_display_refresh_rate"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "display_refresh_rate"), "set_display_refresh_rate", "get_display_refresh_rate"); ClassDB::bind_method(D_METHOD("get_available_display_refresh_rates"), &OpenXRInterface::get_available_display_refresh_rates); } diff --git a/modules/raycast/SCsub b/modules/raycast/SCsub index 20b05816e1..51d75d45b0 100644 --- a/modules/raycast/SCsub +++ b/modules/raycast/SCsub @@ -67,7 +67,7 @@ if env["builtin_embree"]: env_raycast.AppendUnique(CPPDEFINES=["NDEBUG"]) # No assert() even in debug builds. if not env.msvc: - if env["arch"] == "x86_64": + if env["arch"] in ["x86_64", "x86_32"]: env_raycast.Append(CPPFLAGS=["-msse2", "-mxsave"]) if env["platform"] == "windows": @@ -87,10 +87,13 @@ if env["builtin_embree"]: env_thirdparty.disable_warnings() env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources) - if env["arch"] == "arm64" or env.msvc: + if env["arch"] != "x86_64" or env.msvc: # Embree needs those, it will automatically use SSE2NEON in ARM env_thirdparty.Append(CPPDEFINES=["__SSE2__", "__SSE__"]) + if env["platform"] == "web": + env_thirdparty.Append(CPPFLAGS=["-msimd128"]) + if not env.msvc: env_thirdparty.Append( CPPFLAGS=[ diff --git a/modules/raycast/config.py b/modules/raycast/config.py index 833ad50018..f4243f01c5 100644 --- a/modules/raycast/config.py +++ b/modules/raycast/config.py @@ -1,9 +1,13 @@ def can_build(env, platform): - # Depends on Embree library, which only supports x86_64 and arm64. - if platform == "windows": - return env["arch"] == "x86_64" # TODO build for Windows on ARM - - return env["arch"] in ["x86_64", "arm64"] + # Supported architectures depend on the Embree library. + # No ARM32 support planned. + if env["arch"] == "arm32": + return False + # x86_32 only seems supported on Windows for now. + if env["arch"] == "x86_32" and platform != "windows": + return False + # The rest works, even wasm32! + return True def configure(env): diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index c808211d68..6f02d20c25 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -50,8 +50,7 @@ int RegExMatch::_find(const Variant &p_name) const { return -1; } return i; - - } else if (p_name.get_type() == Variant::STRING) { + } else if (p_name.get_type() == Variant::STRING || p_name.get_type() == Variant::STRING_NAME) { HashMap<String, int>::ConstIterator found = names.find((String)p_name); if (found) { return found->value; diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index cf2d8c9986..27fab88956 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -34,6 +34,7 @@ // Headers for building as GDExtension plug-in. #include <godot_cpp/classes/file_access.hpp> +#include <godot_cpp/classes/os.hpp> #include <godot_cpp/classes/project_settings.hpp> #include <godot_cpp/classes/rendering_server.hpp> #include <godot_cpp/classes/translation_server.hpp> @@ -1437,11 +1438,13 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f if (fd->face->style_name != nullptr) { p_font_data->style_name = String::utf8((const char *)fd->face->style_name); } + p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower()); + p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower()); p_font_data->style_flags = 0; - if (fd->face->style_flags & FT_STYLE_FLAG_BOLD) { + if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) { p_font_data->style_flags.set_flag(FONT_BOLD); } - if (fd->face->style_flags & FT_STYLE_FLAG_ITALIC) { + if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) { p_font_data->style_flags.set_flag(FONT_ITALIC); } if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) { @@ -1967,6 +1970,46 @@ String TextServerAdvanced::_font_get_style_name(const RID &p_font_rid) const { return fd->style_name; } +void TextServerAdvanced::_font_set_weight(const RID &p_font_rid, int64_t p_weight) { + FontAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND(!_ensure_cache_for_size(fd, size)); + fd->weight = CLAMP(p_weight, 100, 999); +} + +int64_t TextServerAdvanced::_font_get_weight(const RID &p_font_rid) const { + FontAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 400); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 400); + return fd->weight; +} + +void TextServerAdvanced::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) { + FontAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND(!_ensure_cache_for_size(fd, size)); + fd->stretch = CLAMP(p_stretch, 50, 200); +} + +int64_t TextServerAdvanced::_font_get_stretch(const RID &p_font_rid) const { + FontAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 100); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 100); + return fd->stretch; +} + void TextServerAdvanced::_font_set_name(const RID &p_font_rid, const String &p_name) { FontAdvanced *fd = font_owner.get_or_null(p_font_rid); ERR_FAIL_COND(!fd); @@ -2103,6 +2146,25 @@ int64_t TextServerAdvanced::_font_get_fixed_size(const RID &p_font_rid) const { return fd->fixed_size; } +void TextServerAdvanced::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) { + FontAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + if (fd->allow_system_fallback != p_allow_system_fallback) { + _font_clear_cache(fd); + fd->allow_system_fallback = p_allow_system_fallback; + } +} + +bool TextServerAdvanced::_font_is_allow_system_fallback(const RID &p_font_rid) const { + FontAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, false); + + MutexLock lock(fd->mutex); + return fd->allow_system_fallback; +} + void TextServerAdvanced::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) { FontAdvanced *fd = font_owner.get_or_null(p_font_rid); ERR_FAIL_COND(!fd); @@ -4632,12 +4694,11 @@ bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) { sd->breaks[pos] = true; } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) { sd->breaks[pos] = false; - - int pos_p = pos - 1 - sd->start; - char32_t c = sd->text[pos_p]; - if (pos - sd->start != sd->end && !is_whitespace(c) && (c != 0xfffc)) { - sd->break_inserts++; - } + } + int pos_p = pos - 1 - sd->start; + char32_t c = sd->text[pos_p]; + if (pos - sd->start != sd->end && !is_whitespace(c) && (c != 0xfffc)) { + sd->break_inserts++; } } } @@ -5066,10 +5127,177 @@ _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source } } -void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index) { +void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index, int64_t p_prev_start, int64_t p_prev_end) { + RID f; int fs = p_sd->spans[p_span].font_size; - if (p_fb_index >= p_fonts.size()) { - // Add fallback glyphs. + + if (p_fb_index >= 0 && p_fb_index < p_fonts.size()) { + // Try font from list. + f = p_fonts[p_fb_index]; + } else if (OS::get_singleton()->has_feature("system_fonts") && p_fonts.size() > 0 && ((p_fb_index == p_fonts.size()) || (p_fb_index > p_fonts.size() && p_start != p_prev_start))) { + // Try system fallback. + RID fdef = p_fonts[0]; + if (_font_is_allow_system_fallback(fdef)) { + String text = p_sd->text.substr(p_start, 1); + String font_name = _font_get_name(fdef); + BitField<FontStyle> font_style = _font_get_style(fdef); + int font_weight = _font_get_weight(fdef); + int font_stretch = _font_get_stretch(fdef); + Dictionary dvar = _font_get_variation_coordinates(fdef); + static int64_t wgth_tag = _name_to_tag("weight"); + static int64_t wdth_tag = _name_to_tag("width"); + static int64_t ital_tag = _name_to_tag("italic"); + if (dvar.has(wgth_tag)) { + font_weight = dvar[wgth_tag].operator int(); + } + if (dvar.has(wdth_tag)) { + font_stretch = dvar[wdth_tag].operator int(); + } + if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) { + font_style.set_flag(TextServer::FONT_ITALIC); + } + + char scr_buffer[5] = { 0, 0, 0, 0, 0 }; + hb_tag_to_string(hb_script_to_iso15924_tag(p_script), scr_buffer); + String script_code = String(scr_buffer); + String locale = (p_sd->spans[p_span].language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_sd->spans[p_span].language; + + PackedStringArray fallback_font_name = OS::get_singleton()->get_system_font_path_for_text(font_name, text, locale, script_code, font_weight, font_stretch, font_style & TextServer::FONT_ITALIC); +#ifdef GDEXTENSION + for (int fb = 0; fb < fallback_font_name.size(); fb++) { + const String &E = fallback_font_name[fb]; +#else + for (const String &E : fallback_font_name) { +#endif + SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, fdef, this); + if (system_fonts.has(key)) { + const SystemFontCache &sysf_cache = system_fonts[key]; + int best_score = 0; + int best_match = -1; + for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) { + const SystemFontCacheRec &F = sysf_cache.var[face_idx]; + if (unlikely(!_font_has_char(F.rid, text[0]))) { + continue; + } + BitField<FontStyle> style = _font_get_style(F.rid); + int weight = _font_get_weight(F.rid); + int stretch = _font_get_stretch(F.rid); + int score = (20 - Math::abs(weight - font_weight) / 50); + score += (20 - Math::abs(stretch - font_stretch) / 10); + if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) { + score += 30; + } + if (score >= best_score) { + best_score = score; + best_match = face_idx; + } + if (best_score == 70) { + break; + } + } + if (best_match != -1) { + f = sysf_cache.var[best_match].rid; + } + } + if (!f.is_valid()) { + if (system_fonts.has(key)) { + const SystemFontCache &sysf_cache = system_fonts[key]; + if (sysf_cache.max_var == sysf_cache.var.size()) { + // All subfonts already tested, skip. + continue; + } + } + + if (!system_font_data.has(E)) { + system_font_data[E] = FileAccess::get_file_as_bytes(E); + } + + const PackedByteArray &font_data = system_font_data[E]; + + SystemFontCacheRec sysf; + sysf.rid = _create_font(); + _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size()); + + Dictionary var = dvar; + // Select matching style from collection. + int best_score = 0; + int best_match = -1; + for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) { + _font_set_face_index(sysf.rid, face_idx); + if (unlikely(!_font_has_char(sysf.rid, text[0]))) { + continue; + } + BitField<FontStyle> style = _font_get_style(sysf.rid); + int weight = _font_get_weight(sysf.rid); + int stretch = _font_get_stretch(sysf.rid); + int score = (20 - Math::abs(weight - font_weight) / 50); + score += (20 - Math::abs(stretch - font_stretch) / 10); + if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) { + score += 30; + } + if (score >= best_score) { + best_score = score; + best_match = face_idx; + } + if (best_score == 70) { + break; + } + } + if (best_match == -1) { + _free_rid(sysf.rid); + continue; + } else { + _font_set_face_index(sysf.rid, best_match); + } + sysf.index = best_match; + + // If it's a variable font, apply weight, stretch and italic coordinates to match requested style. + if (best_score != 70) { + Dictionary ftr = _font_supported_variation_list(sysf.rid); + if (ftr.has(wdth_tag)) { + var[wdth_tag] = font_stretch; + _font_set_stretch(sysf.rid, font_stretch); + } + if (ftr.has(wgth_tag)) { + var[wgth_tag] = font_weight; + _font_set_weight(sysf.rid, font_weight); + } + if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) { + var[ital_tag] = 1; + _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC); + } + } + + _font_set_antialiasing(sysf.rid, key.antialiasing); + _font_set_generate_mipmaps(sysf.rid, key.mipmaps); + _font_set_multichannel_signed_distance_field(sysf.rid, key.msdf); + _font_set_msdf_pixel_range(sysf.rid, key.msdf_range); + _font_set_msdf_size(sysf.rid, key.msdf_source_size); + _font_set_fixed_size(sysf.rid, key.fixed_size); + _font_set_force_autohinter(sysf.rid, key.force_autohinter); + _font_set_hinting(sysf.rid, key.hinting); + _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning); + _font_set_variation_coordinates(sysf.rid, var); + _font_set_oversampling(sysf.rid, key.oversampling); + _font_set_embolden(sysf.rid, key.embolden); + _font_set_transform(sysf.rid, key.transform); + + if (system_fonts.has(key)) { + system_fonts[key].var.push_back(sysf); + } else { + SystemFontCache &sysf_cache = system_fonts[key]; + sysf_cache.max_var = _font_get_face_count(sysf.rid); + sysf_cache.var.push_back(sysf); + } + f = sysf.rid; + } + break; + } + } + } + + if (!f.is_valid()) { + // No valid font, use fallback hex code boxes. for (int i = p_start; i < p_end; i++) { if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) { Glyph gl; @@ -5100,7 +5328,6 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star return; } - RID f = p_fonts[p_fb_index]; FontAdvanced *fd = font_owner.get_or_null(f); ERR_FAIL_COND(!fd); MutexLock lock(fd->mutex); @@ -5195,7 +5422,7 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star gl.end = end; gl.count = 0; - gl.font_rid = p_fonts[p_fb_index]; + gl.font_rid = f; gl.font_size = fs; if (glyph_info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) { @@ -5262,7 +5489,7 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star for (unsigned int i = 0; i < glyph_count; i++) { if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) { if (failed_subrun_start != p_end + 1) { - _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1); + _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end); failed_subrun_start = p_end + 1; failed_subrun_end = p_start; } @@ -5292,7 +5519,7 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star } memfree(w); if (failed_subrun_start != p_end + 1) { - _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1); + _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end); } p_sd->ascent = MAX(p_sd->ascent, _font_get_ascent(f, fs)); p_sd->descent = MAX(p_sd->descent, _font_get_descent(f, fs)); @@ -5464,7 +5691,7 @@ bool TextServerAdvanced::_shaped_text_shape(const RID &p_shaped) { } fonts.append_array(fonts_scr_only); fonts.append_array(fonts_no_match); - _shape_run(sd, MAX(sd->spans[k].start - sd->start, script_run_start), MIN(sd->spans[k].end - sd->start, script_run_end), sd->script_iter->script_ranges[j].script, bidi_run_direction, fonts, k, 0); + _shape_run(sd, MAX(sd->spans[k].start - sd->start, script_run_start), MIN(sd->spans[k].end - sd->start, script_run_end), sd->script_iter->script_ranges[j].script, bidi_run_direction, fonts, k, 0, 0, 0); } } } @@ -5961,7 +6188,11 @@ String TextServerAdvanced::_strip_diacritics(const String &p_string) const { String result; for (int i = 0; i < normalized_string.length(); i++) { if (u_getCombiningClass(normalized_string[i]) == 0) { +#ifdef GDEXTENSION + result = result + String::chr(normalized_string[i]); +#else result = result + normalized_string[i]; +#endif } } return result; @@ -6243,6 +6474,17 @@ TextServerAdvanced::TextServerAdvanced() { _bmp_create_font_funcs(); } +void TextServerAdvanced::_cleanup() { + for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) { + const Vector<SystemFontCacheRec> &sysf_cache = E.value.var; + for (const SystemFontCacheRec &F : sysf_cache) { + _free_rid(F.rid); + } + } + system_fonts.clear(); + system_font_data.clear(); +} + TextServerAdvanced::~TextServerAdvanced() { _bmp_free_font_funcs(); #ifdef MODULE_FREETYPE_ENABLED diff --git a/modules/text_server_adv/text_server_adv.h b/modules/text_server_adv/text_server_adv.h index 10fe3c2316..5e6d2cc2c0 100644 --- a/modules/text_server_adv/text_server_adv.h +++ b/modules/text_server_adv/text_server_adv.h @@ -300,6 +300,7 @@ class TextServerAdvanced : public TextServerExtension { int msdf_range = 14; int msdf_source_size = 48; int fixed_size = 0; + bool allow_system_fallback = true; bool force_autohinter = false; TextServer::Hinting hinting = TextServer::HINTING_LIGHT; TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO; @@ -311,6 +312,8 @@ class TextServerAdvanced : public TextServerExtension { BitField<TextServer::FontStyle> style_flags = 0; String font_name; String style_name; + int weight = 400; + int stretch = 100; HashMap<Vector2i, FontForSizeAdvanced *, VariantHasher, VariantComparator> cache; @@ -372,6 +375,57 @@ class TextServerAdvanced : public TextServerExtension { _FORCE_INLINE_ double _get_extra_advance(RID p_font_rid, int p_font_size) const; _FORCE_INLINE_ Variant::Type _get_tag_type(int64_t p_tag) const; _FORCE_INLINE_ bool _get_tag_hidden(int64_t p_tag) const; + _FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const { + String sty_name = p_sty_name.replace(" ", "").replace("-", ""); + if (sty_name.find("thin") >= 0 || sty_name.find("hairline") >= 0) { + return 100; + } else if (sty_name.find("extralight") >= 0 || sty_name.find("ultralight") >= 0) { + return 200; + } else if (sty_name.find("light") >= 0) { + return 300; + } else if (sty_name.find("semilight") >= 0) { + return 350; + } else if (sty_name.find("regular") >= 0) { + return 400; + } else if (sty_name.find("medium") >= 0) { + return 500; + } else if (sty_name.find("semibold") >= 0 || sty_name.find("demibold") >= 0) { + return 600; + } else if (sty_name.find("bold") >= 0) { + return 700; + } else if (sty_name.find("extrabold") >= 0 || sty_name.find("ultrabold") >= 0) { + return 800; + } else if (sty_name.find("black") >= 0 || sty_name.find("heavy") >= 0) { + return 900; + } else if (sty_name.find("extrablack") >= 0 || sty_name.find("ultrablack") >= 0) { + return 950; + } + return 400; + } + _FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const { + String sty_name = p_sty_name.replace(" ", "").replace("-", ""); + if (sty_name.find("ultracondensed") >= 0) { + return 50; + } else if (sty_name.find("extracondensed") >= 0) { + return 63; + } else if (sty_name.find("condensed") >= 0) { + return 75; + } else if (sty_name.find("semicondensed") >= 0) { + return 87; + } else if (sty_name.find("semiexpanded") >= 0) { + return 113; + } else if (sty_name.find("expanded") >= 0) { + return 125; + } else if (sty_name.find("extraexpanded") >= 0) { + return 150; + } else if (sty_name.find("ultraexpanded") >= 0) { + return 200; + } + return 100; + } + _FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const { + return (p_sty_name.find("italic") >= 0) || (p_sty_name.find("oblique") >= 0); + } // Shaped text cache data. struct TrimData { @@ -474,12 +528,87 @@ class TextServerAdvanced : public TextServerExtension { mutable RID_PtrOwner<FontAdvanced> font_owner; mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner; + struct SystemFontKey { + String font_name; + TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY; + bool italic = false; + bool mipmaps = false; + bool msdf = false; + bool force_autohinter = false; + int weight = 400; + int stretch = 100; + int msdf_range = 14; + int msdf_source_size = 48; + int fixed_size = 0; + TextServer::Hinting hinting = TextServer::HINTING_LIGHT; + TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO; + Dictionary variation_coordinates; + double oversampling = 0.0; + double embolden = 0.0; + Transform2D transform; + + bool operator==(const SystemFontKey &p_b) const { + return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (variation_coordinates == p_b.variation_coordinates) && (oversampling == p_b.oversampling) && (embolden == p_b.embolden) && (transform == p_b.transform); + } + + SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerAdvanced *p_fb) { + font_name = p_font_name; + italic = p_italic; + weight = p_weight; + stretch = p_stretch; + antialiasing = p_fb->_font_get_antialiasing(p_font); + mipmaps = p_fb->_font_get_generate_mipmaps(p_font); + msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font); + msdf_range = p_fb->_font_get_msdf_pixel_range(p_font); + msdf_source_size = p_fb->_font_get_msdf_size(p_font); + fixed_size = p_fb->_font_get_fixed_size(p_font); + force_autohinter = p_fb->_font_is_force_autohinter(p_font); + hinting = p_fb->_font_get_hinting(p_font); + subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font); + variation_coordinates = p_fb->_font_get_variation_coordinates(p_font); + oversampling = p_fb->_font_get_oversampling(p_font); + embolden = p_fb->_font_get_embolden(p_font); + transform = p_fb->_font_get_transform(p_font); + } + }; + + struct SystemFontCacheRec { + RID rid; + int index = 0; + }; + + struct SystemFontCache { + Vector<SystemFontCacheRec> var; + int max_var = 0; + }; + + struct SystemFontKeyHasher { + _FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) { + uint32_t hash = p_a.font_name.hash(); + hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash); + hash = hash_murmur3_one_32(p_a.weight, hash); + hash = hash_murmur3_one_32(p_a.stretch, hash); + hash = hash_murmur3_one_32(p_a.msdf_range, hash); + hash = hash_murmur3_one_32(p_a.msdf_source_size, hash); + hash = hash_murmur3_one_32(p_a.fixed_size, hash); + hash = hash_murmur3_one_double(p_a.oversampling, hash); + hash = hash_murmur3_one_double(p_a.embolden, hash); + hash = hash_murmur3_one_real(p_a.transform[0].x, hash); + hash = hash_murmur3_one_real(p_a.transform[0].y, hash); + hash = hash_murmur3_one_real(p_a.transform[1].x, hash); + hash = hash_murmur3_one_real(p_a.transform[1].y, hash); + return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12), hash)); + } + }; + mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts; + mutable HashMap<String, PackedByteArray> system_font_data; + void _realign(ShapedTextDataAdvanced *p_sd) const; int64_t _convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const; int64_t _convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const; int64_t _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const; bool _shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const; - void _shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index); + void _shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index, int64_t p_prev_start, int64_t p_prev_end); Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, const RID &p_font, int64_t p_font_size); _FORCE_INLINE_ void _add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs); @@ -568,6 +697,12 @@ public: MODBIND2(font_set_style_name, const RID &, const String &); MODBIND1RC(String, font_get_style_name, const RID &); + MODBIND2(font_set_weight, const RID &, int64_t); + MODBIND1RC(int64_t, font_get_weight, const RID &); + + MODBIND2(font_set_stretch, const RID &, int64_t); + MODBIND1RC(int64_t, font_get_stretch, const RID &); + MODBIND2(font_set_name, const RID &, const String &); MODBIND1RC(String, font_get_name, const RID &); @@ -589,6 +724,9 @@ public: MODBIND2(font_set_fixed_size, const RID &, int64_t); MODBIND1RC(int64_t, font_get_fixed_size, const RID &); + MODBIND2(font_set_allow_system_fallback, const RID &, bool); + MODBIND1RC(bool, font_is_allow_system_fallback, const RID &); + MODBIND2(font_set_force_autohinter, const RID &, bool); MODBIND1RC(bool, font_is_force_autohinter, const RID &); @@ -787,6 +925,8 @@ public: MODBIND2RC(String, string_to_upper, const String &, const String &); MODBIND2RC(String, string_to_lower, const String &, const String &); + MODBIND0(cleanup); + TextServerAdvanced(); ~TextServerAdvanced(); }; diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index aaef9c9a3d..9133c277bb 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -34,6 +34,7 @@ // Headers for building as GDExtension plug-in. #include <godot_cpp/classes/file_access.hpp> +#include <godot_cpp/classes/os.hpp> #include <godot_cpp/classes/project_settings.hpp> #include <godot_cpp/classes/rendering_server.hpp> #include <godot_cpp/classes/translation_server.hpp> @@ -49,6 +50,7 @@ using namespace godot; #include "core/config/project_settings.h" #include "core/error/error_macros.h" #include "core/string/print_string.h" +#include "core/string/translation.h" #include "core/string/ucaps.h" #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg. @@ -852,11 +854,13 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f if (fd->face->style_name != nullptr) { p_font_data->style_name = String::utf8((const char *)fd->face->style_name); } + p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower()); + p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower()); p_font_data->style_flags = 0; - if (fd->face->style_flags & FT_STYLE_FLAG_BOLD) { + if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) { p_font_data->style_flags.set_flag(FONT_BOLD); } - if (fd->face->style_flags & FT_STYLE_FLAG_ITALIC) { + if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) { p_font_data->style_flags.set_flag(FONT_ITALIC); } if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) { @@ -1061,6 +1065,46 @@ String TextServerFallback::_font_get_style_name(const RID &p_font_rid) const { return fd->style_name; } +void TextServerFallback::_font_set_weight(const RID &p_font_rid, int64_t p_weight) { + FontFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND(!_ensure_cache_for_size(fd, size)); + fd->weight = CLAMP(p_weight, 100, 999); +} + +int64_t TextServerFallback::_font_get_weight(const RID &p_font_rid) const { + FontFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 400); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 400); + return fd->weight; +} + +void TextServerFallback::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) { + FontFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND(!_ensure_cache_for_size(fd, size)); + fd->stretch = CLAMP(p_stretch, 50, 200); +} + +int64_t TextServerFallback::_font_get_stretch(const RID &p_font_rid) const { + FontFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 100); + + MutexLock lock(fd->mutex); + Vector2i size = _get_size(fd, 16); + ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 100); + return fd->stretch; +} + void TextServerFallback::_font_set_name(const RID &p_font_rid, const String &p_name) { FontFallback *fd = font_owner.get_or_null(p_font_rid); ERR_FAIL_COND(!fd); @@ -1197,6 +1241,25 @@ int64_t TextServerFallback::_font_get_fixed_size(const RID &p_font_rid) const { return fd->fixed_size; } +void TextServerFallback::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) { + FontFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + if (fd->allow_system_fallback != p_allow_system_fallback) { + _font_clear_cache(fd); + fd->allow_system_fallback = p_allow_system_fallback; + } +} + +bool TextServerFallback::_font_is_allow_system_fallback(const RID &p_font_rid) const { + FontFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, false); + + MutexLock lock(fd->mutex); + return fd->allow_system_fallback; +} + void TextServerFallback::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) { FontFallback *fd = font_owner.get_or_null(p_font_rid); ERR_FAIL_COND(!fd); @@ -3603,6 +3666,7 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) { sd->glyphs.push_back(gl); } else { // Text span. + RID prev_font; for (int j = span.start; j < span.end; j++) { Glyph gl; gl.start = j; @@ -3623,6 +3687,170 @@ bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) { break; } } + if (!gl.font_rid.is_valid() && prev_font.is_valid()) { + if (_font_has_char(prev_font, gl.index)) { + gl.font_rid = prev_font; + } + } + if (!gl.font_rid.is_valid() && OS::get_singleton()->has_feature("system_fonts") && span.fonts.size() > 0) { + // Try system fallback. + RID fdef = span.fonts[0]; + if (_font_is_allow_system_fallback(fdef)) { + String text = sd->text.substr(j, 1); + String font_name = _font_get_name(fdef); + BitField<FontStyle> font_style = _font_get_style(fdef); + int font_weight = _font_get_weight(fdef); + int font_stretch = _font_get_stretch(fdef); + Dictionary dvar = _font_get_variation_coordinates(fdef); + static int64_t wgth_tag = _name_to_tag("weight"); + static int64_t wdth_tag = _name_to_tag("width"); + static int64_t ital_tag = _name_to_tag("italic"); + if (dvar.has(wgth_tag)) { + font_weight = dvar[wgth_tag].operator int(); + } + if (dvar.has(wdth_tag)) { + font_stretch = dvar[wdth_tag].operator int(); + } + if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) { + font_style.set_flag(TextServer::FONT_ITALIC); + } + + String locale = (span.language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : span.language; + + PackedStringArray fallback_font_name = OS::get_singleton()->get_system_font_path_for_text(font_name, text, locale, String(), font_weight, font_stretch, font_style & TextServer::FONT_ITALIC); +#ifdef GDEXTENSION + for (int fb = 0; fb < fallback_font_name.size(); fb++) { + const String &E = fallback_font_name[fb]; +#else + for (const String &E : fallback_font_name) { +#endif + SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, fdef, this); + if (system_fonts.has(key)) { + const SystemFontCache &sysf_cache = system_fonts[key]; + int best_score = 0; + int best_match = -1; + for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) { + const SystemFontCacheRec &F = sysf_cache.var[face_idx]; + if (unlikely(!_font_has_char(F.rid, text[0]))) { + continue; + } + BitField<FontStyle> style = _font_get_style(F.rid); + int weight = _font_get_weight(F.rid); + int stretch = _font_get_stretch(F.rid); + int score = (20 - Math::abs(weight - font_weight) / 50); + score += (20 - Math::abs(stretch - font_stretch) / 10); + if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) { + score += 30; + } + if (score >= best_score) { + best_score = score; + best_match = face_idx; + } + if (best_score == 70) { + break; + } + } + if (best_match != -1) { + gl.font_rid = sysf_cache.var[best_match].rid; + } + } + if (!gl.font_rid.is_valid()) { + if (system_fonts.has(key)) { + const SystemFontCache &sysf_cache = system_fonts[key]; + if (sysf_cache.max_var == sysf_cache.var.size()) { + // All subfonts already tested, skip. + continue; + } + } + + if (!system_font_data.has(E)) { + system_font_data[E] = FileAccess::get_file_as_bytes(E); + } + + const PackedByteArray &font_data = system_font_data[E]; + + SystemFontCacheRec sysf; + sysf.rid = _create_font(); + _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size()); + + Dictionary var = dvar; + // Select matching style from collection. + int best_score = 0; + int best_match = -1; + for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) { + _font_set_face_index(sysf.rid, face_idx); + if (unlikely(!_font_has_char(sysf.rid, text[0]))) { + continue; + } + BitField<FontStyle> style = _font_get_style(sysf.rid); + int weight = _font_get_weight(sysf.rid); + int stretch = _font_get_stretch(sysf.rid); + int score = (20 - Math::abs(weight - font_weight) / 50); + score += (20 - Math::abs(stretch - font_stretch) / 10); + if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) { + score += 30; + } + if (score >= best_score) { + best_score = score; + best_match = face_idx; + } + if (best_score == 70) { + break; + } + } + if (best_match == -1) { + _free_rid(sysf.rid); + continue; + } else { + _font_set_face_index(sysf.rid, best_match); + } + sysf.index = best_match; + + // If it's a variable font, apply weight, stretch and italic coordinates to match requested style. + if (best_score != 70) { + Dictionary ftr = _font_supported_variation_list(sysf.rid); + if (ftr.has(wdth_tag)) { + var[wdth_tag] = font_stretch; + _font_set_stretch(sysf.rid, font_stretch); + } + if (ftr.has(wgth_tag)) { + var[wgth_tag] = font_weight; + _font_set_weight(sysf.rid, font_weight); + } + if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) { + var[ital_tag] = 1; + _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC); + } + } + + _font_set_antialiasing(sysf.rid, key.antialiasing); + _font_set_generate_mipmaps(sysf.rid, key.mipmaps); + _font_set_multichannel_signed_distance_field(sysf.rid, key.msdf); + _font_set_msdf_pixel_range(sysf.rid, key.msdf_range); + _font_set_msdf_size(sysf.rid, key.msdf_source_size); + _font_set_fixed_size(sysf.rid, key.fixed_size); + _font_set_force_autohinter(sysf.rid, key.force_autohinter); + _font_set_hinting(sysf.rid, key.hinting); + _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning); + _font_set_variation_coordinates(sysf.rid, var); + _font_set_oversampling(sysf.rid, key.oversampling); + _font_set_embolden(sysf.rid, key.embolden); + _font_set_transform(sysf.rid, key.transform); + + if (system_fonts.has(key)) { + system_fonts[key].var.push_back(sysf); + } else { + SystemFontCache &sysf_cache = system_fonts[key]; + sysf_cache.max_var = _font_get_face_count(sysf.rid); + sysf_cache.var.push_back(sysf); + } + gl.font_rid = sysf.rid; + } + break; + } + } + } + prev_font = gl.font_rid; double scale = _font_get_scale(gl.font_rid, gl.font_size); if (gl.font_rid.is_valid()) { @@ -3893,6 +4121,17 @@ TextServerFallback::TextServerFallback() { _insert_feature_sets(); }; +void TextServerFallback::_cleanup() { + for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) { + const Vector<SystemFontCacheRec> &sysf_cache = E.value.var; + for (const SystemFontCacheRec &F : sysf_cache) { + _free_rid(F.rid); + } + } + system_fonts.clear(); + system_font_data.clear(); +} + TextServerFallback::~TextServerFallback() { #ifdef MODULE_FREETYPE_ENABLED if (ft_library != nullptr) { diff --git a/modules/text_server_fb/text_server_fb.h b/modules/text_server_fb/text_server_fb.h index 7e0bc99618..f8a05f9433 100644 --- a/modules/text_server_fb/text_server_fb.h +++ b/modules/text_server_fb/text_server_fb.h @@ -256,6 +256,7 @@ class TextServerFallback : public TextServerExtension { int msdf_source_size = 48; int fixed_size = 0; bool force_autohinter = false; + bool allow_system_fallback = true; TextServer::Hinting hinting = TextServer::HINTING_LIGHT; TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO; Dictionary variation_coordinates; @@ -266,6 +267,8 @@ class TextServerFallback : public TextServerExtension { BitField<TextServer::FontStyle> style_flags = 0; String font_name; String style_name; + int weight = 400; + int stretch = 100; HashMap<Vector2i, FontForSizeFallback *, VariantHasher, VariantComparator> cache; @@ -322,6 +325,58 @@ class TextServerFallback : public TextServerExtension { } } + _FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const { + String sty_name = p_sty_name.replace(" ", "").replace("-", ""); + if (sty_name.find("thin") >= 0 || sty_name.find("hairline") >= 0) { + return 100; + } else if (sty_name.find("extralight") >= 0 || sty_name.find("ultralight") >= 0) { + return 200; + } else if (sty_name.find("light") >= 0) { + return 300; + } else if (sty_name.find("semilight") >= 0) { + return 350; + } else if (sty_name.find("regular") >= 0) { + return 400; + } else if (sty_name.find("medium") >= 0) { + return 500; + } else if (sty_name.find("semibold") >= 0 || sty_name.find("demibold") >= 0) { + return 600; + } else if (sty_name.find("bold") >= 0) { + return 700; + } else if (sty_name.find("extrabold") >= 0 || sty_name.find("ultrabold") >= 0) { + return 800; + } else if (sty_name.find("black") >= 0 || sty_name.find("heavy") >= 0) { + return 900; + } else if (sty_name.find("extrablack") >= 0 || sty_name.find("ultrablack") >= 0) { + return 950; + } + return 400; + } + _FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const { + String sty_name = p_sty_name.replace(" ", "").replace("-", ""); + if (sty_name.find("ultracondensed") >= 0) { + return 50; + } else if (sty_name.find("extracondensed") >= 0) { + return 63; + } else if (sty_name.find("condensed") >= 0) { + return 75; + } else if (sty_name.find("semicondensed") >= 0) { + return 87; + } else if (sty_name.find("semiexpanded") >= 0) { + return 113; + } else if (sty_name.find("expanded") >= 0) { + return 125; + } else if (sty_name.find("extraexpanded") >= 0) { + return 150; + } else if (sty_name.find("ultraexpanded") >= 0) { + return 200; + } + return 100; + } + _FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const { + return (p_sty_name.find("italic") >= 0) || (p_sty_name.find("oblique") >= 0); + } + // Shaped text cache data. struct TrimData { int trim_pos = -1; @@ -398,6 +453,81 @@ class TextServerFallback : public TextServerExtension { mutable RID_PtrOwner<FontFallback> font_owner; mutable RID_PtrOwner<ShapedTextDataFallback> shaped_owner; + struct SystemFontKey { + String font_name; + TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY; + bool italic = false; + bool mipmaps = false; + bool msdf = false; + bool force_autohinter = false; + int weight = 400; + int stretch = 100; + int msdf_range = 14; + int msdf_source_size = 48; + int fixed_size = 0; + TextServer::Hinting hinting = TextServer::HINTING_LIGHT; + TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO; + Dictionary variation_coordinates; + double oversampling = 0.0; + double embolden = 0.0; + Transform2D transform; + + bool operator==(const SystemFontKey &p_b) const { + return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (variation_coordinates == p_b.variation_coordinates) && (oversampling == p_b.oversampling) && (embolden == p_b.embolden) && (transform == p_b.transform); + } + + SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerFallback *p_fb) { + font_name = p_font_name; + italic = p_italic; + weight = p_weight; + stretch = p_stretch; + antialiasing = p_fb->_font_get_antialiasing(p_font); + mipmaps = p_fb->_font_get_generate_mipmaps(p_font); + msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font); + msdf_range = p_fb->_font_get_msdf_pixel_range(p_font); + msdf_source_size = p_fb->_font_get_msdf_size(p_font); + fixed_size = p_fb->_font_get_fixed_size(p_font); + force_autohinter = p_fb->_font_is_force_autohinter(p_font); + hinting = p_fb->_font_get_hinting(p_font); + subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font); + variation_coordinates = p_fb->_font_get_variation_coordinates(p_font); + oversampling = p_fb->_font_get_oversampling(p_font); + embolden = p_fb->_font_get_embolden(p_font); + transform = p_fb->_font_get_transform(p_font); + } + }; + + struct SystemFontCacheRec { + RID rid; + int index = 0; + }; + + struct SystemFontCache { + Vector<SystemFontCacheRec> var; + int max_var = 0; + }; + + struct SystemFontKeyHasher { + _FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) { + uint32_t hash = p_a.font_name.hash(); + hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash); + hash = hash_murmur3_one_32(p_a.weight, hash); + hash = hash_murmur3_one_32(p_a.stretch, hash); + hash = hash_murmur3_one_32(p_a.msdf_range, hash); + hash = hash_murmur3_one_32(p_a.msdf_source_size, hash); + hash = hash_murmur3_one_32(p_a.fixed_size, hash); + hash = hash_murmur3_one_double(p_a.oversampling, hash); + hash = hash_murmur3_one_double(p_a.embolden, hash); + hash = hash_murmur3_one_real(p_a.transform[0].x, hash); + hash = hash_murmur3_one_real(p_a.transform[0].y, hash); + hash = hash_murmur3_one_real(p_a.transform[1].x, hash); + hash = hash_murmur3_one_real(p_a.transform[1].y, hash); + return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12), hash)); + } + }; + mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts; + mutable HashMap<String, PackedByteArray> system_font_data; + void _realign(ShapedTextDataFallback *p_sd) const; protected: @@ -442,6 +572,12 @@ public: MODBIND2(font_set_style_name, const RID &, const String &); MODBIND1RC(String, font_get_style_name, const RID &); + MODBIND2(font_set_weight, const RID &, int64_t); + MODBIND1RC(int64_t, font_get_weight, const RID &); + + MODBIND2(font_set_stretch, const RID &, int64_t); + MODBIND1RC(int64_t, font_get_stretch, const RID &); + MODBIND2(font_set_name, const RID &, const String &); MODBIND1RC(String, font_get_name, const RID &); @@ -463,6 +599,9 @@ public: MODBIND2(font_set_fixed_size, const RID &, int64_t); MODBIND1RC(int64_t, font_get_fixed_size, const RID &); + MODBIND2(font_set_allow_system_fallback, const RID &, bool); + MODBIND1RC(bool, font_is_allow_system_fallback, const RID &); + MODBIND2(font_set_force_autohinter, const RID &, bool); MODBIND1RC(bool, font_is_force_autohinter, const RID &); @@ -651,6 +790,8 @@ public: MODBIND2RC(String, string_to_upper, const String &, const String &); MODBIND2RC(String, string_to_lower, const String &, const String &); + MODBIND0(cleanup); + TextServerFallback(); ~TextServerFallback(); }; diff --git a/modules/webrtc/doc_classes/WebRTCDataChannel.xml b/modules/webrtc/doc_classes/WebRTCDataChannel.xml index a9ba8a23de..a186631ca8 100644 --- a/modules/webrtc/doc_classes/WebRTCDataChannel.xml +++ b/modules/webrtc/doc_classes/WebRTCDataChannel.xml @@ -22,8 +22,8 @@ <method name="get_id" qualifiers="const"> <return type="int" /> <description> - Returns the id assigned to this channel during creation (or auto-assigned during negotiation). - If the channel is not negotiated out-of-band the id will only be available after the connection is established (will return [code]65535[/code] until then). + Returns the ID assigned to this channel during creation (or auto-assigned during negotiation). + If the channel is not negotiated out-of-band the ID will only be available after the connection is established (will return [code]65535[/code] until then). </description> </method> <method name="get_label" qualifiers="const"> diff --git a/modules/webxr/doc_classes/WebXRInterface.xml b/modules/webxr/doc_classes/WebXRInterface.xml index 49dd9f7318..f5964eb4d1 100644 --- a/modules/webxr/doc_classes/WebXRInterface.xml +++ b/modules/webxr/doc_classes/WebXRInterface.xml @@ -1,13 +1,13 @@ <?xml version="1.0" encoding="UTF-8" ?> <class name="WebXRInterface" inherits="XRInterface" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd"> <brief_description> - AR/VR interface using WebXR. + XR interface using WebXR. </brief_description> <description> WebXR is an open standard that allows creating VR and AR applications that run in the web browser. As such, this interface is only available when running in Web exports. WebXR supports a wide range of devices, from the very capable (like Valve Index, HTC Vive, Oculus Rift and Quest) down to the much less capable (like Google Cardboard, Oculus Go, GearVR, or plain smartphones). - Since WebXR is based on JavaScript, it makes extensive use of callbacks, which means that [WebXRInterface] is forced to use signals, where other AR/VR interfaces would instead use functions that return a result immediately. This makes [WebXRInterface] quite a bit more complicated to initialize than other AR/VR interfaces. + Since WebXR is based on JavaScript, it makes extensive use of callbacks, which means that [WebXRInterface] is forced to use signals, where other XR interfaces would instead use functions that return a result immediately. This makes [WebXRInterface] quite a bit more complicated to initialize than other XR interfaces. Here's the minimum code required to start an immersive VR session: [codeblock] extends Node3D @@ -69,7 +69,7 @@ func _webxr_session_started(): $Button.visible = false # This tells Godot to start rendering to the headset. - get_viewport().xr = true + get_viewport().use_xr = true # This will be the reference space type you ultimately got, out of the # types that you requested above. This is useful if you want the game to # work a little differently in 'bounded-floor' versus 'local-floor'. @@ -79,28 +79,35 @@ $Button.visible = true # If the user exits immersive mode, then we tell Godot to render to the web # page again. - get_viewport().xr = false + get_viewport().use_xr = false func _webxr_session_failed(message): OS.alert("Failed to initialize: " + message) [/codeblock] - There are several ways to handle "controller" input: - - Using [XRController3D] nodes and their [signal XRController3D.button_pressed] and [signal XRController3D.button_released] signals. This is how controllers are typically handled in AR/VR apps in Godot, however, this will only work with advanced VR controllers like the Oculus Touch or Index controllers, for example. The buttons codes are defined by [url=https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping]Section 3.3 of the WebXR Gamepads Module[/url]. - - Using [method Node._unhandled_input] and [InputEventJoypadButton] or [InputEventJoypadMotion]. This works the same as normal joypads, except the [member InputEvent.device] starts at 100, so the left controller is 100 and the right controller is 101, and the button codes are also defined by [url=https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping]Section 3.3 of the WebXR Gamepads Module[/url]. - - Using the [signal select], [signal squeeze] and related signals. This method will work for both advanced VR controllers, and non-traditional "controllers" like a tap on the screen, a spoken voice command or a button press on the device itself. - You can use one or all of these methods to allow your game or app to support a wider or narrower set of devices and input methods, or to allow more advanced interactions with more advanced devices. + There are a couple ways to handle "controller" input: + - Using [XRController3D] nodes and their [signal XRController3D.button_pressed] and [signal XRController3D.button_released] signals. This is how controllers are typically handled in XR apps in Godot, however, this will only work with advanced VR controllers like the Oculus Touch or Index controllers, for example. + - Using the [signal select], [signal squeeze] and related signals. This method will work for both advanced VR controllers, and non-traditional input sources like a tap on the screen, a spoken voice command or a button press on the device itself. + You can use both methods to allow your game or app to support a wider or narrower set of devices and input methods, or to allow more advanced interactions with more advanced devices. </description> <tutorials> <link title="How to make a VR game for WebXR with Godot">https://www.snopekgames.com/blog/2020/how-make-vr-game-webxr-godot</link> </tutorials> <methods> - <method name="get_controller" qualifiers="const"> + <method name="get_input_source_target_ray_mode" qualifiers="const"> + <return type="int" enum="WebXRInterface.TargetRayMode" /> + <param index="0" name="input_source_id" type="int" /> + <description> + Returns the target ray mode for the given [code]input_source_id[/code]. + This can help interpret the input coming from that input source. See [url=https://developer.mozilla.org/en-US/docs/Web/API/XRInputSource/targetRayMode]XRInputSource.targetRayMode[/url] for more information. + </description> + </method> + <method name="get_input_source_tracker" qualifiers="const"> <return type="XRPositionalTracker" /> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Gets an [XRPositionalTracker] for the given [code]controller_id[/code]. - In the context of WebXR, a "controller" can be an advanced VR controller like the Oculus Touch or Index controllers, or even a tap on the screen, a spoken voice command or a button press on the device itself. When a non-traditional controller is used, interpret the position and orientation of the [XRPositionalTracker] as a ray pointing at the object the user wishes to interact with. - Use this method to get information about the controller that triggered one of these signals: + Gets an [XRPositionalTracker] for the given [code]input_source_id[/code]. + In the context of WebXR, an input source can be an advanced VR controller like the Oculus Touch or Index controllers, or even a tap on the screen, a spoken voice command or a button press on the device itself. When a non-traditional input source is used, interpret the position and orientation of the [XRPositionalTracker] as a ray pointing at the object the user wishes to interact with. + Use this method to get information about the input source that triggered one of these signals: - [signal selectstart] - [signal select] - [signal selectend] @@ -109,6 +116,13 @@ - [signal squeezestart] </description> </method> + <method name="is_input_source_active" qualifiers="const"> + <return type="bool" /> + <param index="0" name="input_source_id" type="int" /> + <description> + Returns [code]true[/code] if there is an active input source with the given [code]input_source_id[/code]. + </description> + </method> <method name="is_session_supported"> <return type="void" /> <param index="0" name="session_mode" type="String" /> @@ -120,11 +134,6 @@ </method> </methods> <members> - <member name="bounds_geometry" type="PackedVector3Array" setter="" getter="get_bounds_geometry"> - The vertices of a polygon which defines the boundaries of the user's play area. - This will only be available if [member reference_space_type] is [code]"bounded-floor"[/code] and only on certain browsers and devices that support it. - The [signal reference_space_reset] signal may indicate when this changes. - </member> <member name="optional_features" type="String" setter="set_optional_features" getter="get_optional_features"> A comma-seperated list of optional features used by [method XRInterface.initialize] when setting up the WebXR session. If a user's browser or device doesn't support one of the given features, initialization will continue, but you won't be able to use the requested feature. @@ -137,7 +146,7 @@ </member> <member name="requested_reference_space_types" type="String" setter="set_requested_reference_space_types" getter="get_requested_reference_space_types"> A comma-seperated list of reference space types used by [method XRInterface.initialize] when setting up the WebXR session. - The reference space types are requested in order, and the first on supported by the users device or browser will be used. The [member reference_space_type] property contains the reference space type that was ultimately used. + The reference space types are requested in order, and the first one supported by the users device or browser will be used. The [member reference_space_type] property contains the reference space type that was ultimately selected. This doesn't have any effect on the interface when already initialized. Possible values come from [url=https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType]WebXR's XRReferenceSpaceType[/url]. If you want to use a particular reference space type, it must be listed in either [member required_features] or [member optional_features]. </member> @@ -161,35 +170,35 @@ <signal name="reference_space_reset"> <description> Emitted to indicate that the reference space has been reset or reconfigured. - When (or whether) this is emitted depends on the user's browser or device, but may include when the user has changed the dimensions of their play space (which you may be able to access via [member bounds_geometry]) or pressed/held a button to recenter their position. + When (or whether) this is emitted depends on the user's browser or device, but may include when the user has changed the dimensions of their play space (which you may be able to access via [method XRInterface.get_play_area]) or pressed/held a button to recenter their position. See [url=https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpace/reset_event]WebXR's XRReferenceSpace reset event[/url] for more information. </description> </signal> <signal name="select"> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Emitted after one of the "controllers" has finished its "primary action". - Use [method get_controller] to get more information about the controller. + Emitted after one of the input sources has finished its "primary action". + Use [method get_input_source_tracker] and [method get_input_source_target_ray_mode] to get more information about the input source. </description> </signal> <signal name="selectend"> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Emitted when one of the "controllers" has finished its "primary action". - Use [method get_controller] to get more information about the controller. + Emitted when one of the input sources has finished its "primary action". + Use [method get_input_source_tracker] and [method get_input_source_target_ray_mode] to get more information about the input source. </description> </signal> <signal name="selectstart"> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Emitted when one of the "controllers" has started its "primary action". - Use [method get_controller] to get more information about the controller. + Emitted when one of the input source has started its "primary action". + Use [method get_input_source_tracker] and [method get_input_source_target_ray_mode] to get more information about the input source. </description> </signal> <signal name="session_ended"> <description> Emitted when the user ends the WebXR session (which can be done using UI from the browser or device). - At this point, you should do [code]get_viewport().xr = false[/code] to instruct Godot to resume rendering to the screen. + At this point, you should do [code]get_viewport().use_xr = false[/code] to instruct Godot to resume rendering to the screen. </description> </signal> <signal name="session_failed"> @@ -202,7 +211,7 @@ <signal name="session_started"> <description> Emitted by [method XRInterface.initialize] if the session is successfully started. - At this point, it's safe to do [code]get_viewport().xr = true[/code] to instruct Godot to start rendering to the AR/VR device. + At this point, it's safe to do [code]get_viewport().use_xr = true[/code] to instruct Godot to start rendering to the XR device. </description> </signal> <signal name="session_supported"> @@ -213,24 +222,24 @@ </description> </signal> <signal name="squeeze"> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Emitted after one of the "controllers" has finished its "primary squeeze action". - Use [method get_controller] to get more information about the controller. + Emitted after one of the input sources has finished its "primary squeeze action". + Use [method get_input_source_tracker] and [method get_input_source_target_ray_mode] to get more information about the input source. </description> </signal> <signal name="squeezeend"> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Emitted when one of the "controllers" has finished its "primary squeeze action". - Use [method get_controller] to get more information about the controller. + Emitted when one of the input sources has finished its "primary squeeze action". + Use [method get_input_source_tracker] and [method get_input_source_target_ray_mode] to get more information about the input source. </description> </signal> <signal name="squeezestart"> - <param index="0" name="controller_id" type="int" /> + <param index="0" name="input_source_id" type="int" /> <description> - Emitted when one of the "controllers" has started its "primary squeeze action". - Use [method get_controller] to get more information about the controller. + Emitted when one of the input sources has started its "primary squeeze action". + Use [method get_input_source_tracker] and [method get_input_source_target_ray_mode] to get more information about the input source. </description> </signal> <signal name="visibility_state_changed"> @@ -239,4 +248,18 @@ </description> </signal> </signals> + <constants> + <constant name="TARGET_RAY_MODE_UNKNOWN" value="0" enum="TargetRayMode"> + We don't know the the target ray mode. + </constant> + <constant name="TARGET_RAY_MODE_GAZE" value="1" enum="TargetRayMode"> + Target ray originates at the viewer's eyes and points in the direction they are looking. + </constant> + <constant name="TARGET_RAY_MODE_TRACKED_POINTER" value="2" enum="TargetRayMode"> + Target ray from a handheld pointer, most likely a VR touch controller. + </constant> + <constant name="TARGET_RAY_MODE_SCREEN" value="3" enum="TargetRayMode"> + Target ray from touch screen, mouse or other tactile input device. + </constant> + </constants> </class> diff --git a/modules/webxr/godot_webxr.h b/modules/webxr/godot_webxr.h index d8d5bd99cc..e31a1d307e 100644 --- a/modules/webxr/godot_webxr.h +++ b/modules/webxr/godot_webxr.h @@ -37,12 +37,18 @@ extern "C" { #include "stddef.h" +enum WebXRInputEvent { + WEBXR_INPUT_EVENT_SELECTSTART, + WEBXR_INPUT_EVENT_SELECTEND, + WEBXR_INPUT_EVENT_SQUEEZESTART, + WEBXR_INPUT_EVENT_SQUEEZEEND, +}; + typedef void (*GodotWebXRSupportedCallback)(char *p_session_mode, int p_supported); typedef void (*GodotWebXRStartedCallback)(char *p_reference_space_type); typedef void (*GodotWebXREndedCallback)(); typedef void (*GodotWebXRFailedCallback)(char *p_message); -typedef void (*GodotWebXRControllerCallback)(); -typedef void (*GodotWebXRInputEventCallback)(char *p_signal_name, int p_controller_id); +typedef void (*GodotWebXRInputEventCallback)(int p_event_type, int p_input_source_id); typedef void (*GodotWebXRSimpleEventCallback)(char *p_signal_name); extern int godot_webxr_is_supported(); @@ -56,26 +62,33 @@ extern void godot_webxr_initialize( GodotWebXRStartedCallback p_on_session_started, GodotWebXREndedCallback p_on_session_ended, GodotWebXRFailedCallback p_on_session_failed, - GodotWebXRControllerCallback p_on_controller_changed, GodotWebXRInputEventCallback p_on_input_event, GodotWebXRSimpleEventCallback p_on_simple_event); extern void godot_webxr_uninitialize(); extern int godot_webxr_get_view_count(); -extern int *godot_webxr_get_render_target_size(); -extern float *godot_webxr_get_transform_for_eye(int p_eye); -extern float *godot_webxr_get_projection_for_eye(int p_eye); -extern void godot_webxr_commit(unsigned int p_texture); +extern bool godot_webxr_get_render_target_size(int *r_size); +extern bool godot_webxr_get_transform_for_view(int p_view, float *r_transform); +extern bool godot_webxr_get_projection_for_view(int p_view, float *r_transform); +extern unsigned int godot_webxr_get_color_texture(); +extern unsigned int godot_webxr_get_depth_texture(); +extern unsigned int godot_webxr_get_velocity_texture(); -extern void godot_webxr_sample_controller_data(); -extern int godot_webxr_get_controller_count(); -extern int godot_webxr_is_controller_connected(int p_controller); -extern float *godot_webxr_get_controller_transform(int p_controller); -extern int *godot_webxr_get_controller_buttons(int p_controller); -extern int *godot_webxr_get_controller_axes(int p_controller); +extern bool godot_webxr_update_input_source( + int p_input_source_id, + float *r_target_pose, + int *r_target_ray_mode, + int *r_touch_index, + int *r_has_grip_pose, + float *r_grip_pose, + int *r_has_standard_mapping, + int *r_button_count, + float *r_buttons, + int *r_axes_count, + float *r_axes); extern char *godot_webxr_get_visibility_state(); -extern int *godot_webxr_get_bounds_geometry(); +extern int godot_webxr_get_bounds_geometry(float **r_points); #ifdef __cplusplus } diff --git a/modules/webxr/native/library_godot_webxr.js b/modules/webxr/native/library_godot_webxr.js index 714768347c..eaf251d48f 100644 --- a/modules/webxr/native/library_godot_webxr.js +++ b/modules/webxr/native/library_godot_webxr.js @@ -33,9 +33,14 @@ const GodotWebXR = { gl: null, session: null, + gl_binding: null, + layer: null, space: null, frame: null, pose: null, + view_count: 1, + input_sources: new Array(16), + touches: new Array(5), // Monkey-patch the requestAnimationFrame() used by Emscripten for the main // loop, so that we can swap it out for XRSession.requestAnimationFrame() @@ -76,34 +81,128 @@ const GodotWebXR = { }, 0); }, - // Holds the controllers list between function calls. - controllers: [], + getLayer: () => { + const new_view_count = (GodotWebXR.pose) ? GodotWebXR.pose.views.length : 1; + let layer = GodotWebXR.layer; - // Updates controllers array, where the left hand (or sole tracker) is - // the first element, and the right hand is the second element, and any - // others placed at the 3rd position and up. - sampleControllers: () => { - if (!GodotWebXR.session || !GodotWebXR.frame) { - return; + // If the view count hasn't changed since creating this layer, then + // we can simply return it. + if (layer && GodotWebXR.view_count === new_view_count) { + return layer; } - let other_index = 2; - const controllers = []; - GodotWebXR.session.inputSources.forEach((input_source) => { - if (input_source.targetRayMode === 'tracked-pointer') { - if (input_source.handedness === 'right') { - controllers[1] = input_source; - } else if (input_source.handedness === 'left' || !controllers[0]) { - controllers[0] = input_source; + if (!GodotWebXR.session || !GodotWebXR.gl_binding) { + return null; + } + + const gl = GodotWebXR.gl; + + layer = GodotWebXR.gl_binding.createProjectionLayer({ + textureType: new_view_count > 1 ? 'texture-array' : 'texture', + colorFormat: gl.RGBA8, + depthFormat: gl.DEPTH_COMPONENT24, + }); + GodotWebXR.session.updateRenderState({ layers: [layer] }); + + GodotWebXR.layer = layer; + GodotWebXR.view_count = new_view_count; + return layer; + }, + + getSubImage: () => { + if (!GodotWebXR.pose) { + return null; + } + const layer = GodotWebXR.getLayer(); + if (layer === null) { + return null; + } + + // Because we always use "texture-array" for multiview and "texture" + // when there is only 1 view, it should be safe to only grab the + // subimage for the first view. + return GodotWebXR.gl_binding.getViewSubImage(layer, GodotWebXR.pose.views[0]); + }, + + getTextureId: (texture) => { + if (texture.name !== undefined) { + return texture.name; + } + + const id = GL.getNewId(GL.textures); + texture.name = id; + GL.textures[id] = texture; + + return id; + }, + + addInputSource: (input_source) => { + let name = -1; + if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'left') { + name = 0; + } else if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'right') { + name = 1; + } else { + for (let i = 2; i < 16; i++) { + if (!GodotWebXR.input_sources[i]) { + name = i; + break; } - } else { - controllers[other_index++] = input_source; } - }); - GodotWebXR.controllers = controllers; + } + if (name >= 0) { + GodotWebXR.input_sources[name] = input_source; + input_source.name = name; + + // Find a free touch index for screen sources. + if (input_source.targetRayMode === 'screen') { + let touch_index = -1; + for (let i = 0; i < 5; i++) { + if (!GodotWebXR.touches[i]) { + touch_index = i; + break; + } + } + if (touch_index >= 0) { + GodotWebXR.touches[touch_index] = input_source; + input_source.touch_index = touch_index; + } + } + } + return name; }, - getControllerId: (input_source) => GodotWebXR.controllers.indexOf(input_source), + removeInputSource: (input_source) => { + if (input_source.name !== undefined) { + const name = input_source.name; + if (name >= 0 && name < 16) { + GodotWebXR.input_sources[name] = null; + } + + if (input_source.touch_index !== undefined) { + const touch_index = input_source.touch_index; + if (touch_index >= 0 && touch_index < 5) { + GodotWebXR.touches[touch_index] = null; + } + } + return name; + } + return -1; + }, + + getInputSourceId: (input_source) => { + if (input_source !== undefined) { + return input_source.name; + } + return -1; + }, + + getTouchIndex: (input_source) => { + if (input_source.touch_index !== undefined) { + return input_source.touch_index; + } + return -1; + }, }, godot_webxr_is_supported__proxy: 'sync', @@ -132,8 +231,8 @@ const GodotWebXR = { godot_webxr_initialize__deps: ['emscripten_webgl_get_current_context'], godot_webxr_initialize__proxy: 'sync', - godot_webxr_initialize__sig: 'viiiiiiiiii', - godot_webxr_initialize: function (p_session_mode, p_required_features, p_optional_features, p_requested_reference_spaces, p_on_session_started, p_on_session_ended, p_on_session_failed, p_on_controller_changed, p_on_input_event, p_on_simple_event) { + godot_webxr_initialize__sig: 'viiiiiiiii', + godot_webxr_initialize: function (p_session_mode, p_required_features, p_optional_features, p_requested_reference_spaces, p_on_session_started, p_on_session_ended, p_on_session_failed, p_on_input_event, p_on_simple_event) { GodotWebXR.monkeyPatchRequestAnimationFrame(true); const session_mode = GodotRuntime.parseString(p_session_mode); @@ -143,7 +242,6 @@ const GodotWebXR = { const onstarted = GodotRuntime.get_func(p_on_session_started); const onended = GodotRuntime.get_func(p_on_session_ended); const onfailed = GodotRuntime.get_func(p_on_session_failed); - const oncontroller = GodotRuntime.get_func(p_on_controller_changed); const oninputevent = GodotRuntime.get_func(p_on_input_event); const onsimpleevent = GodotRuntime.get_func(p_on_simple_event); @@ -163,24 +261,18 @@ const GodotWebXR = { }); session.addEventListener('inputsourceschange', function (evt) { - let controller_changed = false; - [evt.added, evt.removed].forEach((lst) => { - lst.forEach((input_source) => { - if (input_source.targetRayMode === 'tracked-pointer') { - controller_changed = true; - } - }); - }); - if (controller_changed) { - oncontroller(); - } + evt.added.forEach(GodotWebXR.addInputSource); + evt.removed.forEach(GodotWebXR.removeInputSource); }); - ['selectstart', 'select', 'selectend', 'squeezestart', 'squeeze', 'squeezeend'].forEach((input_event) => { + ['selectstart', 'selectend', 'squeezestart', 'squeezeend'].forEach((input_event, index) => { session.addEventListener(input_event, function (evt) { - const c_str = GodotRuntime.allocString(input_event); - oninputevent(c_str, GodotWebXR.getControllerId(evt.inputSource)); - GodotRuntime.free(c_str); + // Since this happens in-between normal frames, we need to + // grab the frame from the event in order to get poses for + // the input sources. + GodotWebXR.frame = evt.frame; + oninputevent(index, GodotWebXR.getInputSourceId(evt.inputSource)); + GodotWebXR.frame = null; }); }); @@ -195,9 +287,10 @@ const GodotWebXR = { GodotWebXR.gl = gl; gl.makeXRCompatible().then(function () { - session.updateRenderState({ - baseLayer: new XRWebGLLayer(session, gl), - }); + GodotWebXR.gl_binding = new XRWebGLBinding(session, gl); // eslint-disable-line no-undef + + // This will trigger the layer to get created. + GodotWebXR.getLayer(); function onReferenceSpaceSuccess(reference_space, reference_space_type) { GodotWebXR.space = reference_space; @@ -266,9 +359,14 @@ const GodotWebXR = { } GodotWebXR.session = null; + GodotWebXR.gl_binding = null; + GodotWebXR.layer = null; GodotWebXR.space = null; GodotWebXR.frame = null; GodotWebXR.pose = null; + GodotWebXR.view_count = 1; + GodotWebXR.input_sources = new Array(16); + GodotWebXR.touches = new Array(5); // Disable the monkey-patched window.requestAnimationFrame() and // pause/restart the main loop to activate it on all platforms. @@ -280,215 +378,186 @@ const GodotWebXR = { godot_webxr_get_view_count__sig: 'i', godot_webxr_get_view_count: function () { if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + return 1; } - return GodotWebXR.pose.views.length; + const view_count = GodotWebXR.pose.views.length; + return view_count > 0 ? view_count : 1; }, godot_webxr_get_render_target_size__proxy: 'sync', - godot_webxr_get_render_target_size__sig: 'i', - godot_webxr_get_render_target_size: function () { - if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + godot_webxr_get_render_target_size__sig: 'ii', + godot_webxr_get_render_target_size: function (r_size) { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { + return false; } - const glLayer = GodotWebXR.session.renderState.baseLayer; - const view = GodotWebXR.pose.views[0]; - const viewport = glLayer.getViewport(view); + GodotRuntime.setHeapValue(r_size + 0, subimage.viewport.width, 'i32'); + GodotRuntime.setHeapValue(r_size + 4, subimage.viewport.height, 'i32'); - const buf = GodotRuntime.malloc(2 * 4); - GodotRuntime.setHeapValue(buf + 0, viewport.width, 'i32'); - GodotRuntime.setHeapValue(buf + 4, viewport.height, 'i32'); - return buf; + return true; }, - godot_webxr_get_transform_for_eye__proxy: 'sync', - godot_webxr_get_transform_for_eye__sig: 'ii', - godot_webxr_get_transform_for_eye: function (p_eye) { + godot_webxr_get_transform_for_view__proxy: 'sync', + godot_webxr_get_transform_for_view__sig: 'iii', + godot_webxr_get_transform_for_view: function (p_view, r_transform) { if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + return false; } const views = GodotWebXR.pose.views; let matrix; - if (p_eye === 0) { - matrix = GodotWebXR.pose.transform.matrix; + if (p_view >= 0) { + matrix = views[p_view].transform.matrix; } else { - matrix = views[p_eye - 1].transform.matrix; - } - const buf = GodotRuntime.malloc(16 * 4); - for (let i = 0; i < 16; i++) { - GodotRuntime.setHeapValue(buf + (i * 4), matrix[i], 'float'); - } - return buf; - }, - - godot_webxr_get_projection_for_eye__proxy: 'sync', - godot_webxr_get_projection_for_eye__sig: 'ii', - godot_webxr_get_projection_for_eye: function (p_eye) { - if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + // For -1 (or any other negative value) return the HMD transform. + matrix = GodotWebXR.pose.transform.matrix; } - const view_index = (p_eye === 2 /* ARVRInterface::EYE_RIGHT */) ? 1 : 0; - const matrix = GodotWebXR.pose.views[view_index].projectionMatrix; - const buf = GodotRuntime.malloc(16 * 4); for (let i = 0; i < 16; i++) { - GodotRuntime.setHeapValue(buf + (i * 4), matrix[i], 'float'); + GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float'); } - return buf; + + return true; }, - godot_webxr_commit__proxy: 'sync', - godot_webxr_commit__sig: 'vi', - godot_webxr_commit: function (p_texture) { + godot_webxr_get_projection_for_view__proxy: 'sync', + godot_webxr_get_projection_for_view__sig: 'iii', + godot_webxr_get_projection_for_view: function (p_view, r_transform) { if (!GodotWebXR.session || !GodotWebXR.pose) { - return; + return false; } - const glLayer = GodotWebXR.session.renderState.baseLayer; - const views = GodotWebXR.pose.views; - const gl = GodotWebXR.gl; - - const texture = GL.textures[p_texture]; - - const orig_framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING); - const orig_read_framebuffer = gl.getParameter(gl.READ_FRAMEBUFFER_BINDING); - const orig_read_buffer = gl.getParameter(gl.READ_BUFFER); - const orig_draw_framebuffer = gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING); - - // Copy from Godot render target into framebuffer from WebXR. - gl.bindFramebuffer(gl.FRAMEBUFFER, null); - for (let i = 0; i < views.length; i++) { - const viewport = glLayer.getViewport(views[i]); - - const read_fbo = gl.createFramebuffer(); - gl.bindFramebuffer(gl.READ_FRAMEBUFFER, read_fbo); - if (views.length > 1) { - gl.framebufferTextureLayer(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture, 0, i); - } else { - gl.framebufferTexture2D(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); - } - gl.readBuffer(gl.COLOR_ATTACHMENT0); - gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, glLayer.framebuffer); - - // Flip Y upside down on destination. - gl.blitFramebuffer(0, 0, viewport.width, viewport.height, - viewport.x, viewport.y + viewport.height, viewport.x + viewport.width, viewport.y, - gl.COLOR_BUFFER_BIT, gl.NEAREST); - - gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null); - gl.deleteFramebuffer(read_fbo); + const matrix = GodotWebXR.pose.views[p_view].projectionMatrix; + for (let i = 0; i < 16; i++) { + GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float'); } - // Restore state. - gl.bindFramebuffer(gl.FRAMEBUFFER, orig_framebuffer); - gl.bindFramebuffer(gl.READ_FRAMEBUFFER, orig_read_framebuffer); - gl.readBuffer(orig_read_buffer); - gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, orig_draw_framebuffer); + return true; }, - godot_webxr_sample_controller_data__proxy: 'sync', - godot_webxr_sample_controller_data__sig: 'v', - godot_webxr_sample_controller_data: function () { - GodotWebXR.sampleControllers(); + godot_webxr_get_color_texture__proxy: 'sync', + godot_webxr_get_color_texture__sig: 'i', + godot_webxr_get_color_texture: function () { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { + return 0; + } + return GodotWebXR.getTextureId(subimage.colorTexture); }, - godot_webxr_get_controller_count__proxy: 'sync', - godot_webxr_get_controller_count__sig: 'i', - godot_webxr_get_controller_count: function () { - if (!GodotWebXR.session || !GodotWebXR.frame) { + godot_webxr_get_depth_texture__proxy: 'sync', + godot_webxr_get_depth_texture__sig: 'i', + godot_webxr_get_depth_texture: function () { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { return 0; } - return GodotWebXR.controllers.length; + if (!subimage.depthStencilTexture) { + return 0; + } + return GodotWebXR.getTextureId(subimage.depthStencilTexture); }, - godot_webxr_is_controller_connected__proxy: 'sync', - godot_webxr_is_controller_connected__sig: 'ii', - godot_webxr_is_controller_connected: function (p_controller) { - if (!GodotWebXR.session || !GodotWebXR.frame) { - return false; + godot_webxr_get_velocity_texture__proxy: 'sync', + godot_webxr_get_velocity_texture__sig: 'i', + godot_webxr_get_velocity_texture: function () { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { + return 0; + } + if (!subimage.motionVectorTexture) { + return 0; } - return !!GodotWebXR.controllers[p_controller]; + return GodotWebXR.getTextureId(subimage.motionVectorTexture); }, - godot_webxr_get_controller_transform__proxy: 'sync', - godot_webxr_get_controller_transform__sig: 'ii', - godot_webxr_get_controller_transform: function (p_controller) { + godot_webxr_update_input_source__proxy: 'sync', + godot_webxr_update_input_source__sig: 'iiiiiiiiiiii', + godot_webxr_update_input_source: function (p_input_source_id, r_target_pose, r_target_ray_mode, r_touch_index, r_has_grip_pose, r_grip_pose, r_has_standard_mapping, r_button_count, r_buttons, r_axes_count, r_axes) { if (!GodotWebXR.session || !GodotWebXR.frame) { return 0; } - const controller = GodotWebXR.controllers[p_controller]; - if (!controller) { - return 0; + if (p_input_source_id < 0 || p_input_source_id >= GodotWebXR.input_sources.length || !GodotWebXR.input_sources[p_input_source_id]) { + return false; } + const input_source = GodotWebXR.input_sources[p_input_source_id]; const frame = GodotWebXR.frame; const space = GodotWebXR.space; - const pose = frame.getPose(controller.targetRaySpace, space); - if (!pose) { + // Target pose. + const target_pose = frame.getPose(input_source.targetRaySpace, space); + if (!target_pose) { // This can mean that the controller lost tracking. - return 0; + return false; } - const matrix = pose.transform.matrix; - - const buf = GodotRuntime.malloc(16 * 4); + const target_pose_matrix = target_pose.transform.matrix; for (let i = 0; i < 16; i++) { - GodotRuntime.setHeapValue(buf + (i * 4), matrix[i], 'float'); + GodotRuntime.setHeapValue(r_target_pose + (i * 4), target_pose_matrix[i], 'float'); } - return buf; - }, - godot_webxr_get_controller_buttons__proxy: 'sync', - godot_webxr_get_controller_buttons__sig: 'ii', - godot_webxr_get_controller_buttons: function (p_controller) { - if (GodotWebXR.controllers.length === 0) { - return 0; - } + // Target ray mode. + let target_ray_mode = 0; + switch (input_source.targetRayMode) { + case 'gaze': + target_ray_mode = 1; + break; - const controller = GodotWebXR.controllers[p_controller]; - if (!controller || !controller.gamepad) { - return 0; - } - - const button_count = controller.gamepad.buttons.length; + case 'tracked-pointer': + target_ray_mode = 2; + break; - const buf = GodotRuntime.malloc((button_count + 1) * 4); - GodotRuntime.setHeapValue(buf, button_count, 'i32'); - for (let i = 0; i < button_count; i++) { - GodotRuntime.setHeapValue(buf + 4 + (i * 4), controller.gamepad.buttons[i].value, 'float'); - } - return buf; - }, + case 'screen': + target_ray_mode = 3; + break; - godot_webxr_get_controller_axes__proxy: 'sync', - godot_webxr_get_controller_axes__sig: 'ii', - godot_webxr_get_controller_axes: function (p_controller) { - if (GodotWebXR.controllers.length === 0) { - return 0; + default: } - - const controller = GodotWebXR.controllers[p_controller]; - if (!controller || !controller.gamepad) { - return 0; + GodotRuntime.setHeapValue(r_target_ray_mode, target_ray_mode, 'i32'); + + // Touch index. + GodotRuntime.setHeapValue(r_touch_index, GodotWebXR.getTouchIndex(input_source), 'i32'); + + // Grip pose. + let has_grip_pose = false; + if (input_source.gripSpace) { + const grip_pose = frame.getPose(input_source.gripSpace, space); + if (grip_pose) { + const grip_pose_matrix = grip_pose.transform.matrix; + for (let i = 0; i < 16; i++) { + GodotRuntime.setHeapValue(r_grip_pose + (i * 4), grip_pose_matrix[i], 'float'); + } + has_grip_pose = true; + } } + GodotRuntime.setHeapValue(r_has_grip_pose, has_grip_pose ? 1 : 0, 'i32'); + + // Gamepad data (mapping, buttons and axes). + let has_standard_mapping = false; + let button_count = 0; + let axes_count = 0; + if (input_source.gamepad) { + if (input_source.gamepad.mapping === 'xr-standard') { + has_standard_mapping = true; + } - const axes_count = controller.gamepad.axes.length; + button_count = Math.min(input_source.gamepad.buttons.length, 10); + for (let i = 0; i < button_count; i++) { + GodotRuntime.setHeapValue(r_buttons + (i * 4), input_source.gamepad.buttons[i].value, 'float'); + } - const buf = GodotRuntime.malloc((axes_count + 1) * 4); - GodotRuntime.setHeapValue(buf, axes_count, 'i32'); - for (let i = 0; i < axes_count; i++) { - let value = controller.gamepad.axes[i]; - if (i === 1 || i === 3) { - // Invert the Y-axis on thumbsticks and trackpads, in order to - // match OpenXR and other XR platform SDKs. - value *= -1.0; + axes_count = Math.min(input_source.gamepad.axes.length, 10); + for (let i = 0; i < axes_count; i++) { + GodotRuntime.setHeapValue(r_axes + (i * 4), input_source.gamepad.axes[i], 'float'); } - GodotRuntime.setHeapValue(buf + 4 + (i * 4), value, 'float'); } - return buf; + GodotRuntime.setHeapValue(r_has_standard_mapping, has_standard_mapping ? 1 : 0, 'i32'); + GodotRuntime.setHeapValue(r_button_count, button_count, 'i32'); + GodotRuntime.setHeapValue(r_axes_count, axes_count, 'i32'); + + return true; }, godot_webxr_get_visibility_state__proxy: 'sync', @@ -502,8 +571,8 @@ const GodotWebXR = { }, godot_webxr_get_bounds_geometry__proxy: 'sync', - godot_webxr_get_bounds_geometry__sig: 'i', - godot_webxr_get_bounds_geometry: function () { + godot_webxr_get_bounds_geometry__sig: 'ii', + godot_webxr_get_bounds_geometry: function (r_points) { if (!GodotWebXR.space || !GodotWebXR.space.boundsGeometry) { return 0; } @@ -513,7 +582,7 @@ const GodotWebXR = { return 0; } - const buf = GodotRuntime.malloc(((point_count * 3) + 1) * 4); + const buf = GodotRuntime.malloc(point_count * 3 * 4); GodotRuntime.setHeapValue(buf, point_count, 'i32'); for (let i = 0; i < point_count; i++) { const point = GodotWebXR.space.boundsGeometry[i]; @@ -521,8 +590,9 @@ const GodotWebXR = { GodotRuntime.setHeapValue(buf + ((i * 3) + 2) * 4, point.y, 'float'); GodotRuntime.setHeapValue(buf + ((i * 3) + 3) * 4, point.z, 'float'); } + GodotRuntime.setHeapValue(r_points, buf, 'i32'); - return buf; + return point_count; }, }; diff --git a/modules/webxr/native/webxr.externs.js b/modules/webxr/native/webxr.externs.js index 9ea105aa93..4b88820b19 100644 --- a/modules/webxr/native/webxr.externs.js +++ b/modules/webxr/native/webxr.externs.js @@ -1,3 +1,7 @@ +/* + * WebXR Device API + */ + /** * @type {XR} */ @@ -497,3 +501,681 @@ XRPose.prototype.transform; * @type {boolean} */ XRPose.prototype.emulatedPosition; + +/* + * WebXR Layers API Level 1 + */ + +/** + * @constructor XRLayer + */ +function XRLayer() {} + +/** + * @constructor XRLayerEventInit + */ +function XRLayerEventInit() {} + +/** + * @type {XRLayer} + */ +XRLayerEventInit.prototype.layer; + +/** + * @constructor XRLayerEvent + * + * @param {string} type + * @param {XRLayerEventInit} init + */ +function XRLayerEvent(type, init) {}; + +/** + * @type {XRLayer} + */ +XRLayerEvent.prototype.layer; + +/** + * @constructor XRCompositionLayer + * @extends {XRLayer} + */ +function XRCompositionLayer() {}; + +/** + * @type {string} + */ +XRCompositionLayer.prototype.layout; + +/** + * @type {boolean} + */ +XRCompositionLayer.prototype.blendTextureAberrationCorrection; + +/** + * @type {?boolean} + */ +XRCompositionLayer.prototype.chromaticAberrationCorrection; + +/** + * @type {boolean} + */ +XRCompositionLayer.prototype.forceMonoPresentation; + +/** + * @type {number} + */ +XRCompositionLayer.prototype.opacity; + +/** + * @type {number} + */ +XRCompositionLayer.prototype.mipLevels; + +/** + * @type {boolean} + */ +XRCompositionLayer.prototype.needsRedraw; + +/** + * @return {void} + */ +XRCompositionLayer.prototype.destroy = function () {}; + +/** + * @constructor XRProjectionLayer + * @extends {XRCompositionLayer} + */ +function XRProjectionLayer() {} + +/** + * @type {number} + */ +XRProjectionLayer.prototype.textureWidth; + +/** + * @type {number} + */ +XRProjectionLayer.prototype.textureHeight; + +/** + * @type {number} + */ +XRProjectionLayer.prototype.textureArrayLength; + +/** + * @type {boolean} + */ +XRProjectionLayer.prototype.ignoreDepthValues; + +/** + * @type {?number} + */ +XRProjectionLayer.prototype.fixedFoveation; + +/** + * @type {XRRigidTransform} + */ +XRProjectionLayer.prototype.deltaPose; + +/** + * @constructor XRQuadLayer + * @extends {XRCompositionLayer} + */ +function XRQuadLayer() {} + +/** + * @type {XRSpace} + */ +XRQuadLayer.prototype.space; + +/** + * @type {XRRigidTransform} + */ +XRQuadLayer.prototype.transform; + +/** + * @type {number} + */ +XRQuadLayer.prototype.width; + +/** + * @type {number} + */ +XRQuadLayer.prototype.height; + +/** + * @type {?function (XRLayerEvent)} + */ +XRQuadLayer.prototype.onredraw; + +/** + * @constructor XRCylinderLayer + * @extends {XRCompositionLayer} + */ +function XRCylinderLayer() {} + +/** + * @type {XRSpace} + */ +XRCylinderLayer.prototype.space; + +/** + * @type {XRRigidTransform} + */ +XRCylinderLayer.prototype.transform; + +/** + * @type {number} + */ +XRCylinderLayer.prototype.radius; + +/** + * @type {number} + */ +XRCylinderLayer.prototype.centralAngle; + +/** + * @type {number} + */ +XRCylinderLayer.prototype.aspectRatio; + +/** + * @type {?function (XRLayerEvent)} + */ +XRCylinderLayer.prototype.onredraw; + +/** + * @constructor XREquirectLayer + * @extends {XRCompositionLayer} + */ +function XREquirectLayer() {} + +/** + * @type {XRSpace} + */ +XREquirectLayer.prototype.space; + +/** + * @type {XRRigidTransform} + */ +XREquirectLayer.prototype.transform; + +/** + * @type {number} + */ +XREquirectLayer.prototype.radius; + +/** + * @type {number} + */ +XREquirectLayer.prototype.centralHorizontalAngle; + +/** + * @type {number} + */ +XREquirectLayer.prototype.upperVerticalAngle; + +/** + * @type {number} + */ +XREquirectLayer.prototype.lowerVerticalAngle; + +/** + * @type {?function (XRLayerEvent)} + */ +XREquirectLayer.prototype.onredraw; + +/** + * @constructor XRCubeLayer + * @extends {XRCompositionLayer} + */ +function XRCubeLayer() {} + +/** + * @type {XRSpace} + */ +XRCubeLayer.prototype.space; + +/** + * @type {DOMPointReadOnly} + */ +XRCubeLayer.prototype.orientation; + +/** + * @type {?function (XRLayerEvent)} + */ +XRCubeLayer.prototype.onredraw; + +/** + * @constructor XRSubImage + */ +function XRSubImage() {} + +/** + * @type {XRViewport} + */ +XRSubImage.prototype.viewport; + +/** + * @constructor XRWebGLSubImage + * @extends {XRSubImage} + */ +function XRWebGLSubImage () {} + +/** + * @type {WebGLTexture} + */ +XRWebGLSubImage.prototype.colorTexture; + +/** + * @type {?WebGLTexture} + */ +XRWebGLSubImage.prototype.depthStencilTexture; + +/** + * @type {?WebGLTexture} + */ +XRWebGLSubImage.prototype.motionVectorTexture; + +/** + * @type {?number} + */ +XRWebGLSubImage.prototype.imageIndex; + +/** + * @type {number} + */ +XRWebGLSubImage.prototype.colorTextureWidth; + +/** + * @type {number} + */ +XRWebGLSubImage.prototype.colorTextureHeight; + +/** + * @type {?number} + */ +XRWebGLSubImage.prototype.depthStencilTextureWidth; + +/** + * @type {?number} + */ +XRWebGLSubImage.prototype.depthStencilTextureHeight; + +/** + * @type {?number} + */ + +XRWebGLSubImage.prototype.motionVectorTextureWidth; + +/** + * @type {?number} + */ +XRWebGLSubImage.prototype.motionVectorTextureHeight; + +/** + * @constructor XRProjectionLayerInit + */ +function XRProjectionLayerInit() {} + +/** + * @type {string} + */ +XRProjectionLayerInit.prototype.textureType; + +/** + * @type {number} + */ +XRProjectionLayerInit.prototype.colorFormat; + +/** + * @type {number} + */ +XRProjectionLayerInit.prototype.depthFormat; + +/** + * @type {number} + */ +XRProjectionLayerInit.prototype.scaleFactor; + +/** + * @constructor XRLayerInit + */ +function XRLayerInit() {} + +/** + * @type {XRSpace} + */ +XRLayerInit.prototype.space; + +/** + * @type {number} + */ +XRLayerInit.prototype.colorFormat; + +/** + * @type {number} + */ +XRLayerInit.prototype.depthFormat; + +/** + * @type {number} + */ +XRLayerInit.prototype.mipLevels; + +/** + * @type {number} + */ +XRLayerInit.prototype.viewPixelWidth; + +/** + * @type {number} + */ +XRLayerInit.prototype.viewPixelHeight; + +/** + * @type {string} + */ +XRLayerInit.prototype.layout; + +/** + * @type {boolean} + */ +XRLayerInit.prototype.isStatic; + +/** + * @constructor XRQuadLayerInit + * @extends {XRLayerInit} + */ +function XRQuadLayerInit() {} + +/** + * @type {string} + */ +XRQuadLayerInit.prototype.textureType; + +/** + * @type {?XRRigidTransform} + */ +XRQuadLayerInit.prototype.transform; + +/** + * @type {number} + */ +XRQuadLayerInit.prototype.width; + +/** + * @type {number} + */ +XRQuadLayerInit.prototype.height; + +/** + * @constructor XRCylinderLayerInit + * @extends {XRLayerInit} + */ +function XRCylinderLayerInit() {} + +/** + * @type {string} + */ +XRCylinderLayerInit.prototype.textureType; + +/** + * @type {?XRRigidTransform} + */ +XRCylinderLayerInit.prototype.transform; + +/** + * @type {number} + */ +XRCylinderLayerInit.prototype.radius; + +/** + * @type {number} + */ +XRCylinderLayerInit.prototype.centralAngle; + +/** + * @type {number} + */ +XRCylinderLayerInit.prototype.aspectRatio; + +/** + * @constructor XREquirectLayerInit + * @extends {XRLayerInit} + */ +function XREquirectLayerInit() {} + +/** + * @type {string} + */ +XREquirectLayerInit.prototype.textureType; + +/** + * @type {?XRRigidTransform} + */ +XREquirectLayerInit.prototype.transform; + +/** + * @type {number} + */ +XREquirectLayerInit.prototype.radius; + +/** + * @type {number} + */ +XREquirectLayerInit.prototype.centralHorizontalAngle; + +/** + * @type {number} + */ +XREquirectLayerInit.prototype.upperVerticalAngle; + +/** + * @type {number} + */ +XREquirectLayerInit.prototype.lowerVerticalAngle; + +/** + * @constructor XRCubeLayerInit + * @extends {XRLayerInit} + */ +function XRCubeLayerInit() {} + +/** + * @type {DOMPointReadOnly} + */ +XRCubeLayerInit.prototype.orientation; + +/** + * @constructor XRWebGLBinding + * + * @param {XRSession} session + * @param {WebGLRenderContext|WebGL2RenderingContext} context + */ +function XRWebGLBinding(session, context) {} + +/** + * @type {number} + */ +XRWebGLBinding.prototype.nativeProjectionScaleFactor; + +/** + * @type {number} + */ +XRWebGLBinding.prototype.usesDepthValues; + +/** + * @param {XRProjectionLayerInit} init + * @return {XRProjectionLayer} + */ +XRWebGLBinding.prototype.createProjectionLayer = function (init) {}; + +/** + * @param {XRQuadLayerInit} init + * @return {XRQuadLayer} + */ +XRWebGLBinding.prototype.createQuadLayer = function (init) {}; + +/** + * @param {XRCylinderLayerInit} init + * @return {XRCylinderLayer} + */ +XRWebGLBinding.prototype.createCylinderLayer = function (init) {}; + +/** + * @param {XREquirectLayerInit} init + * @return {XREquirectLayer} + */ +XRWebGLBinding.prototype.createEquirectLayer = function (init) {}; + +/** + * @param {XRCubeLayerInit} init + * @return {XRCubeLayer} + */ +XRWebGLBinding.prototype.createCubeLayer = function (init) {}; + +/** + * @param {XRCompositionLayer} layer + * @param {XRFrame} frame + * @param {string} eye + * @return {XRWebGLSubImage} + */ +XRWebGLBinding.prototype.getSubImage = function (layer, frame, eye) {}; + +/** + * @param {XRProjectionLayer} layer + * @param {XRView} view + * @return {XRWebGLSubImage} + */ +XRWebGLBinding.prototype.getViewSubImage = function (layer, view) {}; + +/** + * @constructor XRMediaLayerInit + */ +function XRMediaLayerInit() {} + +/** + * @type {XRSpace} + */ +XRMediaLayerInit.prototype.space; + +/** + * @type {string} + */ +XRMediaLayerInit.prototype.layout; + +/** + * @type {boolean} + */ +XRMediaLayerInit.prototype.invertStereo; + +/** + * @constructor XRMediaQuadLayerInit + * @extends {XRMediaLayerInit} + */ +function XRMediaQuadLayerInit() {} + +/** + * @type {XRRigidTransform} + */ +XRMediaQuadLayerInit.prototype.transform; + +/** + * @type {number} + */ +XRMediaQuadLayerInit.prototype.width; + +/** + * @type {number} + */ +XRMediaQuadLayerInit.prototype.height; + +/** + * @constructor XRMediaCylinderLayerInit + * @extends {XRMediaLayerInit} + */ +function XRMediaCylinderLayerInit() {} + +/** + * @type {XRRigidTransform} + */ +XRMediaCylinderLayerInit.prototype.transform; + +/** + * @type {number} + */ +XRMediaCylinderLayerInit.prototype.radius; + +/** + * @type {number} + */ +XRMediaCylinderLayerInit.prototype.centralAngle; + +/** + * @type {?number} + */ +XRMediaCylinderLayerInit.prototype.aspectRatio; + +/** + * @constructor XRMediaEquirectLayerInit + * @extends {XRMediaLayerInit} + */ +function XRMediaEquirectLayerInit() {} + +/** + * @type {XRRigidTransform} + */ +XRMediaEquirectLayerInit.prototype.transform; + +/** + * @type {number} + */ +XRMediaEquirectLayerInit.prototype.radius; + +/** + * @type {number} + */ +XRMediaEquirectLayerInit.prototype.centralHorizontalAngle; + +/** + * @type {number} + */ +XRMediaEquirectLayerInit.prototype.upperVerticalAngle; + +/** + * @type {number} + */ +XRMediaEquirectLayerInit.prototype.lowerVerticalAngle; + +/** + * @constructor XRMediaBinding + * + * @param {XRSession} session + */ +function XRMediaBinding(session) {} + +/** + * @param {HTMLVideoElement} video + * @param {XRMediaQuadLayerInit} init + * @return {XRQuadLayer} + */ +XRMediaBinding.prototype.createQuadLayer = function(video, init) {}; + +/** + * @param {HTMLVideoElement} video + * @param {XRMediaCylinderLayerInit} init + * @return {XRCylinderLayer} + */ +XRMediaBinding.prototype.createCylinderLayer = function(video, init) {}; + +/** + * @param {HTMLVideoElement} video + * @param {XRMediaEquirectLayerInit} init + * @return {XREquirectLayer} + */ +XRMediaBinding.prototype.createEquirectLayer = function(video, init) {}; + +/** + * @type {Array<XRLayer>} + */ +XRRenderState.prototype.layers; diff --git a/modules/webxr/webxr_interface.cpp b/modules/webxr/webxr_interface.cpp index b0ad53523a..c0580df172 100644 --- a/modules/webxr/webxr_interface.cpp +++ b/modules/webxr/webxr_interface.cpp @@ -42,9 +42,10 @@ void WebXRInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("get_reference_space_type"), &WebXRInterface::get_reference_space_type); ClassDB::bind_method(D_METHOD("set_requested_reference_space_types", "requested_reference_space_types"), &WebXRInterface::set_requested_reference_space_types); ClassDB::bind_method(D_METHOD("get_requested_reference_space_types"), &WebXRInterface::get_requested_reference_space_types); - ClassDB::bind_method(D_METHOD("get_controller", "controller_id"), &WebXRInterface::get_controller); + ClassDB::bind_method(D_METHOD("is_input_source_active", "input_source_id"), &WebXRInterface::is_input_source_active); + ClassDB::bind_method(D_METHOD("get_input_source_tracker", "input_source_id"), &WebXRInterface::get_input_source_tracker); + ClassDB::bind_method(D_METHOD("get_input_source_target_ray_mode", "input_source_id"), &WebXRInterface::get_input_source_target_ray_mode); ClassDB::bind_method(D_METHOD("get_visibility_state"), &WebXRInterface::get_visibility_state); - ClassDB::bind_method(D_METHOD("get_bounds_geometry"), &WebXRInterface::get_bounds_geometry); ADD_PROPERTY(PropertyInfo(Variant::STRING, "session_mode", PROPERTY_HINT_NONE), "set_session_mode", "get_session_mode"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "required_features", PROPERTY_HINT_NONE), "set_required_features", "get_required_features"); @@ -52,20 +53,24 @@ void WebXRInterface::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "requested_reference_space_types", PROPERTY_HINT_NONE), "set_requested_reference_space_types", "get_requested_reference_space_types"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "reference_space_type", PROPERTY_HINT_NONE), "", "get_reference_space_type"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "visibility_state", PROPERTY_HINT_NONE), "", "get_visibility_state"); - ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "bounds_geometry", PROPERTY_HINT_NONE), "", "get_bounds_geometry"); ADD_SIGNAL(MethodInfo("session_supported", PropertyInfo(Variant::STRING, "session_mode"), PropertyInfo(Variant::BOOL, "supported"))); ADD_SIGNAL(MethodInfo("session_started")); ADD_SIGNAL(MethodInfo("session_ended")); ADD_SIGNAL(MethodInfo("session_failed", PropertyInfo(Variant::STRING, "message"))); - ADD_SIGNAL(MethodInfo("selectstart", PropertyInfo(Variant::INT, "controller_id"))); - ADD_SIGNAL(MethodInfo("select", PropertyInfo(Variant::INT, "controller_id"))); - ADD_SIGNAL(MethodInfo("selectend", PropertyInfo(Variant::INT, "controller_id"))); - ADD_SIGNAL(MethodInfo("squeezestart", PropertyInfo(Variant::INT, "controller_id"))); - ADD_SIGNAL(MethodInfo("squeeze", PropertyInfo(Variant::INT, "controller_id"))); - ADD_SIGNAL(MethodInfo("squeezeend", PropertyInfo(Variant::INT, "controller_id"))); + ADD_SIGNAL(MethodInfo("selectstart", PropertyInfo(Variant::INT, "input_source_id"))); + ADD_SIGNAL(MethodInfo("select", PropertyInfo(Variant::INT, "input_source_id"))); + ADD_SIGNAL(MethodInfo("selectend", PropertyInfo(Variant::INT, "input_source_id"))); + ADD_SIGNAL(MethodInfo("squeezestart", PropertyInfo(Variant::INT, "input_source_id"))); + ADD_SIGNAL(MethodInfo("squeeze", PropertyInfo(Variant::INT, "input_source_id"))); + ADD_SIGNAL(MethodInfo("squeezeend", PropertyInfo(Variant::INT, "input_source_id"))); ADD_SIGNAL(MethodInfo("visibility_state_changed")); ADD_SIGNAL(MethodInfo("reference_space_reset")); + + BIND_ENUM_CONSTANT(TARGET_RAY_MODE_UNKNOWN); + BIND_ENUM_CONSTANT(TARGET_RAY_MODE_GAZE); + BIND_ENUM_CONSTANT(TARGET_RAY_MODE_TRACKED_POINTER); + BIND_ENUM_CONSTANT(TARGET_RAY_MODE_SCREEN); } diff --git a/modules/webxr/webxr_interface.h b/modules/webxr/webxr_interface.h index 801643bfa6..1afeb5bab0 100644 --- a/modules/webxr/webxr_interface.h +++ b/modules/webxr/webxr_interface.h @@ -45,6 +45,13 @@ protected: static void _bind_methods(); public: + enum TargetRayMode { + TARGET_RAY_MODE_UNKNOWN, + TARGET_RAY_MODE_GAZE, + TARGET_RAY_MODE_TRACKED_POINTER, + TARGET_RAY_MODE_SCREEN, + }; + virtual void is_session_supported(const String &p_session_mode) = 0; virtual void set_session_mode(String p_session_mode) = 0; virtual String get_session_mode() const = 0; @@ -55,9 +62,12 @@ public: virtual void set_requested_reference_space_types(String p_requested_reference_space_types) = 0; virtual String get_requested_reference_space_types() const = 0; virtual String get_reference_space_type() const = 0; - virtual Ref<XRPositionalTracker> get_controller(int p_controller_id) const = 0; + virtual bool is_input_source_active(int p_input_source_id) const = 0; + virtual Ref<XRPositionalTracker> get_input_source_tracker(int p_input_source_id) const = 0; + virtual TargetRayMode get_input_source_target_ray_mode(int p_input_source_id) const = 0; virtual String get_visibility_state() const = 0; - virtual PackedVector3Array get_bounds_geometry() const = 0; }; +VARIANT_ENUM_CAST(WebXRInterface::TargetRayMode); + #endif // WEBXR_INTERFACE_H diff --git a/modules/webxr/webxr_interface_js.cpp b/modules/webxr/webxr_interface_js.cpp index f6ed9f027e..265f6626a7 100644 --- a/modules/webxr/webxr_interface_js.cpp +++ b/modules/webxr/webxr_interface_js.cpp @@ -37,6 +37,8 @@ #include "drivers/gles3/storage/texture_storage.h" #include "emscripten.h" #include "godot_webxr.h" +#include "scene/main/scene_tree.h" +#include "scene/main/window.h" #include "servers/rendering/renderer_compositor.h" #include "servers/rendering/rendering_server_globals.h" @@ -89,25 +91,14 @@ void _emwebxr_on_session_failed(char *p_message) { interface->emit_signal(SNAME("session_failed"), message); } -void _emwebxr_on_controller_changed() { +extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_input_event(int p_event_type, int p_input_source_id) { XRServer *xr_server = XRServer::get_singleton(); ERR_FAIL_NULL(xr_server); Ref<XRInterface> interface = xr_server->find_interface("WebXR"); ERR_FAIL_COND(interface.is_null()); - static_cast<WebXRInterfaceJS *>(interface.ptr())->_on_controller_changed(); -} - -extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_input_event(char *p_signal_name, int p_input_source) { - XRServer *xr_server = XRServer::get_singleton(); - ERR_FAIL_NULL(xr_server); - - Ref<XRInterface> interface = xr_server->find_interface("WebXR"); - ERR_FAIL_COND(interface.is_null()); - - StringName signal_name = StringName(p_signal_name); - interface->emit_signal(signal_name, p_input_source + 1); + ((WebXRInterfaceJS *)interface.ptr())->_on_input_event(p_event_type, p_input_source_id); } extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_simple_event(char *p_signal_name) { @@ -165,16 +156,22 @@ String WebXRInterfaceJS::get_reference_space_type() const { return reference_space_type; } -Ref<XRPositionalTracker> WebXRInterfaceJS::get_controller(int p_controller_id) const { - XRServer *xr_server = XRServer::get_singleton(); - ERR_FAIL_NULL_V(xr_server, Ref<XRPositionalTracker>()); +bool WebXRInterfaceJS::is_input_source_active(int p_input_source_id) const { + ERR_FAIL_INDEX_V(p_input_source_id, input_source_count, false); + return input_sources[p_input_source_id].active; +} - // TODO support more then two controllers - if (p_controller_id >= 0 && p_controller_id < 2) { - return controllers[p_controller_id]; - }; +Ref<XRPositionalTracker> WebXRInterfaceJS::get_input_source_tracker(int p_input_source_id) const { + ERR_FAIL_INDEX_V(p_input_source_id, input_source_count, Ref<XRPositionalTracker>()); + return input_sources[p_input_source_id].tracker; +} - return Ref<XRPositionalTracker>(); +WebXRInterface::TargetRayMode WebXRInterfaceJS::get_input_source_target_ray_mode(int p_input_source_id) const { + ERR_FAIL_INDEX_V(p_input_source_id, input_source_count, WebXRInterface::TARGET_RAY_MODE_UNKNOWN); + if (!input_sources[p_input_source_id].active) { + return WebXRInterface::TARGET_RAY_MODE_UNKNOWN; + } + return input_sources[p_input_source_id].target_ray_mode; } String WebXRInterfaceJS::get_visibility_state() const { @@ -188,17 +185,18 @@ String WebXRInterfaceJS::get_visibility_state() const { return String(); } -PackedVector3Array WebXRInterfaceJS::get_bounds_geometry() const { +PackedVector3Array WebXRInterfaceJS::get_play_area() const { PackedVector3Array ret; - int *js_bounds = godot_webxr_get_bounds_geometry(); - if (js_bounds) { - ret.resize(js_bounds[0]); - for (int i = 0; i < js_bounds[0]; i++) { - float *js_vector3 = ((float *)js_bounds) + (i * 3) + 1; + float *points; + int point_count = godot_webxr_get_bounds_geometry(&points); + if (point_count > 0) { + ret.resize(point_count); + for (int i = 0; i < point_count; i++) { + float *js_vector3 = points + (i * 3); ret.set(i, Vector3(js_vector3[0], js_vector3[1], js_vector3[2])); } - free(js_bounds); + free(points); } return ret; @@ -209,7 +207,7 @@ StringName WebXRInterfaceJS::get_name() const { }; uint32_t WebXRInterfaceJS::get_capabilities() const { - return XRInterface::XR_STEREO | XRInterface::XR_MONO; + return XRInterface::XR_STEREO | XRInterface::XR_MONO | XRInterface::XR_VR | XRInterface::XR_AR; }; uint32_t WebXRInterfaceJS::get_view_count() { @@ -261,7 +259,6 @@ bool WebXRInterfaceJS::initialize() { &_emwebxr_on_session_started, &_emwebxr_on_session_ended, &_emwebxr_on_session_failed, - &_emwebxr_on_controller_changed, &_emwebxr_on_input_event, &_emwebxr_on_simple_event); }; @@ -287,6 +284,18 @@ void WebXRInterfaceJS::uninitialize() { godot_webxr_uninitialize(); + GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage); + if (texture_storage != nullptr) { + for (KeyValue<unsigned int, RID> &E : texture_cache) { + // Forcibly mark as not part of a render target so we can free it. + GLES3::Texture *texture = texture_storage->get_texture(E.value); + texture->is_render_target = false; + + texture_storage->texture_free(E.value); + } + } + + texture_cache.clear(); reference_space_type = ""; initialized = false; }; @@ -316,27 +325,26 @@ Size2 WebXRInterfaceJS::get_render_target_size() { return render_targetsize; } - int *js_size = godot_webxr_get_render_target_size(); - if (!initialized || js_size == nullptr) { - // As a temporary default (until WebXR is fully initialized), use half the window size. - Size2 temp = DisplayServer::get_singleton()->window_get_size(); - temp.width /= 2.0; - return temp; - } + int js_size[2]; + bool has_size = godot_webxr_get_render_target_size(js_size); - render_targetsize.width = js_size[0]; - render_targetsize.height = js_size[1]; + if (!initialized || !has_size) { + // As a temporary default (until WebXR is fully initialized), use the + // window size. + return DisplayServer::get_singleton()->window_get_size(); + } - free(js_size); + render_targetsize.width = (float)js_size[0]; + render_targetsize.height = (float)js_size[1]; return render_targetsize; }; Transform3D WebXRInterfaceJS::get_camera_transform() { - Transform3D transform_for_eye; + Transform3D camera_transform; XRServer *xr_server = XRServer::get_singleton(); - ERR_FAIL_NULL_V(xr_server, transform_for_eye); + ERR_FAIL_NULL_V(xr_server, camera_transform); if (initialized) { float world_scale = xr_server->get_world_scale(); @@ -345,181 +353,382 @@ Transform3D WebXRInterfaceJS::get_camera_transform() { Transform3D _head_transform = head_transform; _head_transform.origin *= world_scale; - transform_for_eye = (xr_server->get_reference_frame()) * _head_transform; + camera_transform = (xr_server->get_reference_frame()) * _head_transform; } - return transform_for_eye; + return camera_transform; }; Transform3D WebXRInterfaceJS::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) { - Transform3D transform_for_eye; - XRServer *xr_server = XRServer::get_singleton(); - ERR_FAIL_NULL_V(xr_server, transform_for_eye); + ERR_FAIL_NULL_V(xr_server, p_cam_transform); + ERR_FAIL_COND_V(!initialized, p_cam_transform); - float *js_matrix = godot_webxr_get_transform_for_eye(p_view + 1); - if (!initialized || js_matrix == nullptr) { - transform_for_eye = p_cam_transform; - return transform_for_eye; + float js_matrix[16]; + bool has_transform = godot_webxr_get_transform_for_view(p_view, js_matrix); + if (!has_transform) { + return p_cam_transform; } - transform_for_eye = _js_matrix_to_transform(js_matrix); - free(js_matrix); + Transform3D transform_for_view = _js_matrix_to_transform(js_matrix); float world_scale = xr_server->get_world_scale(); // Scale only the center point of our eye transform, so we don't scale the // distance between the eyes. Transform3D _head_transform = head_transform; - transform_for_eye.origin -= _head_transform.origin; + transform_for_view.origin -= _head_transform.origin; _head_transform.origin *= world_scale; - transform_for_eye.origin += _head_transform.origin; + transform_for_view.origin += _head_transform.origin; - return p_cam_transform * xr_server->get_reference_frame() * transform_for_eye; + return p_cam_transform * xr_server->get_reference_frame() * transform_for_view; }; Projection WebXRInterfaceJS::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) { - Projection eye; + Projection view; + + ERR_FAIL_COND_V(!initialized, view); - float *js_matrix = godot_webxr_get_projection_for_eye(p_view + 1); - if (!initialized || js_matrix == nullptr) { - return eye; + float js_matrix[16]; + bool has_projection = godot_webxr_get_projection_for_view(p_view, js_matrix); + if (!has_projection) { + return view; } int k = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - eye.columns[i][j] = js_matrix[k++]; + view.columns[i][j] = js_matrix[k++]; } } - free(js_matrix); - // Copied from godot_oculus_mobile's ovr_mobile_session.cpp - eye.columns[2][2] = -(p_z_far + p_z_near) / (p_z_far - p_z_near); - eye.columns[3][2] = -(2.0f * p_z_far * p_z_near) / (p_z_far - p_z_near); + view.columns[2][2] = -(p_z_far + p_z_near) / (p_z_far - p_z_near); + view.columns[3][2] = -(2.0f * p_z_far * p_z_near) / (p_z_far - p_z_near); - return eye; + return view; +} + +bool WebXRInterfaceJS::pre_draw_viewport(RID p_render_target) { + GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage); + if (texture_storage == nullptr) { + return false; + } + + GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target); + if (rt == nullptr) { + return false; + } + + // Cache the resources so we don't have to get them from JS twice. + color_texture = _get_color_texture(); + depth_texture = _get_depth_texture(); + + // Per the WebXR spec, it returns "opaque textures" to us, which may be the + // same WebGLTexture object (which would be the same GLuint in C++) but + // represent a different underlying resource (probably the next texture in + // the XR device's swap chain). In order to render to this texture, we need + // to re-attach it to the FBO, otherwise we get an "incomplete FBO" error. + // + // See: https://immersive-web.github.io/layers/#xropaquetextures + // + // This is why we're doing this sort of silly check: if the color and depth + // textures are the same this frame as last frame, we need to attach them + // again, despite the fact that the GLuint for them hasn't changed. + if (rt->overridden.is_overridden && rt->overridden.color == color_texture && rt->overridden.depth == depth_texture) { + GLES3::Config *config = GLES3::Config::get_singleton(); + bool use_multiview = rt->view_count > 1 && config->multiview_supported; + + glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo); + if (use_multiview) { + glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->color, 0, 0, rt->view_count); + glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, rt->depth, 0, 0, rt->view_count); + } else { + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0); + } + glBindFramebuffer(GL_FRAMEBUFFER, texture_storage->system_fbo); + } + + return true; } Vector<BlitToScreen> WebXRInterfaceJS::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) { Vector<BlitToScreen> blit_to_screen; - if (!initialized) { - return blit_to_screen; + // We don't need to do anything here. + + return blit_to_screen; +}; + +RID WebXRInterfaceJS::_get_color_texture() { + unsigned int texture_id = godot_webxr_get_color_texture(); + if (texture_id == 0) { + return RID(); + } + + return _get_texture(texture_id); +} + +RID WebXRInterfaceJS::_get_depth_texture() { + unsigned int texture_id = godot_webxr_get_depth_texture(); + if (texture_id == 0) { + return RID(); + } + + return _get_texture(texture_id); +} + +RID WebXRInterfaceJS::_get_texture(unsigned int p_texture_id) { + RBMap<unsigned int, RID>::Element *cache = texture_cache.find(p_texture_id); + if (cache != nullptr) { + return cache->get(); } GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage); - if (!texture_storage) { - return blit_to_screen; + if (texture_storage == nullptr) { + return RID(); } - GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target); + uint32_t view_count = godot_webxr_get_view_count(); + Size2 texture_size = get_render_target_size(); - godot_webxr_commit(rt->color); + RID texture = texture_storage->texture_create_external( + view_count == 1 ? GLES3::Texture::TYPE_2D : GLES3::Texture::TYPE_LAYERED, + Image::FORMAT_RGBA8, + p_texture_id, + (int)texture_size.width, + (int)texture_size.height, + 1, + view_count); - return blit_to_screen; -}; + texture_cache.insert(p_texture_id, texture); + + return texture; +} + +RID WebXRInterfaceJS::get_color_texture() { + return color_texture; +} + +RID WebXRInterfaceJS::get_depth_texture() { + return depth_texture; +} + +RID WebXRInterfaceJS::get_velocity_texture() { + unsigned int texture_id = godot_webxr_get_velocity_texture(); + if (texture_id == 0) { + return RID(); + } + + return _get_texture(texture_id); +} void WebXRInterfaceJS::process() { if (initialized) { // Get the "head" position. - float *js_matrix = godot_webxr_get_transform_for_eye(0); - if (js_matrix != nullptr) { + float js_matrix[16]; + if (godot_webxr_get_transform_for_view(-1, js_matrix)) { head_transform = _js_matrix_to_transform(js_matrix); - free(js_matrix); } if (head_tracker.is_valid()) { head_tracker->set_pose("default", head_transform, Vector3(), Vector3()); } - godot_webxr_sample_controller_data(); - int controller_count = godot_webxr_get_controller_count(); - for (int i = 0; i < controller_count; i++) { - _update_tracker(i); + // Update all input sources. + for (int i = 0; i < input_source_count; i++) { + _update_input_source(i); } }; }; -void WebXRInterfaceJS::_update_tracker(int p_controller_id) { +void WebXRInterfaceJS::_update_input_source(int p_input_source_id) { XRServer *xr_server = XRServer::get_singleton(); ERR_FAIL_NULL(xr_server); - // need to support more then two controllers... - if (p_controller_id < 0 || p_controller_id > 1) { + InputSource &input_source = input_sources[p_input_source_id]; + + float target_pose[16]; + int tmp_target_ray_mode; + int touch_index; + int has_grip_pose; + float grip_pose[16]; + int has_standard_mapping; + int button_count; + float buttons[10]; + int axes_count; + float axes[10]; + + input_source.active = godot_webxr_update_input_source( + p_input_source_id, + target_pose, + &tmp_target_ray_mode, + &touch_index, + &has_grip_pose, + grip_pose, + &has_standard_mapping, + &button_count, + buttons, + &axes_count, + axes); + + if (!input_source.active) { + if (input_source.tracker.is_valid()) { + xr_server->remove_tracker(input_source.tracker); + input_source.tracker.unref(); + } return; } - Ref<XRPositionalTracker> tracker = controllers[p_controller_id]; - if (godot_webxr_is_controller_connected(p_controller_id)) { - if (tracker.is_null()) { - tracker.instantiate(); - tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER); - // Controller id's 0 and 1 are always the left and right hands. - if (p_controller_id < 2) { - tracker->set_tracker_name(p_controller_id == 0 ? "left_hand" : "right_hand"); - tracker->set_tracker_desc(p_controller_id == 0 ? "Left hand controller" : "Right hand controller"); - tracker->set_tracker_hand(p_controller_id == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT); - } else { - char name[1024]; - sprintf(name, "tracker_%i", p_controller_id); - tracker->set_tracker_name(name); - tracker->set_tracker_desc(name); - } - xr_server->add_tracker(tracker); + input_source.target_ray_mode = (WebXRInterface::TargetRayMode)tmp_target_ray_mode; + input_source.touch_index = touch_index; + + Ref<XRPositionalTracker> &tracker = input_source.tracker; + + if (tracker.is_null()) { + tracker.instantiate(); + + StringName tracker_name; + if (input_source.target_ray_mode == WebXRInterface::TargetRayMode::TARGET_RAY_MODE_SCREEN) { + tracker_name = touch_names[touch_index]; + } else { + tracker_name = tracker_names[p_input_source_id]; } - float *tracker_matrix = godot_webxr_get_controller_transform(p_controller_id); - if (tracker_matrix) { - // Note, poses should NOT have world scale and our reference frame applied! - Transform3D transform = _js_matrix_to_transform(tracker_matrix); - tracker->set_pose("default", transform, Vector3(), Vector3()); - free(tracker_matrix); + // Input source id's 0 and 1 are always the left and right hands. + if (p_input_source_id < 2) { + tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER); + tracker->set_tracker_name(tracker_name); + tracker->set_tracker_desc(p_input_source_id == 0 ? "Left hand controller" : "Right hand controller"); + tracker->set_tracker_hand(p_input_source_id == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT); + } else { + tracker->set_tracker_name(tracker_name); + tracker->set_tracker_desc(tracker_name); } + xr_server->add_tracker(tracker); + } - // TODO implement additional poses such as "aim" and "grip" + Transform3D aim_transform = _js_matrix_to_transform(target_pose); + tracker->set_pose(SNAME("default"), aim_transform, Vector3(), Vector3()); + tracker->set_pose(SNAME("aim"), aim_transform, Vector3(), Vector3()); + if (has_grip_pose) { + tracker->set_pose(SNAME("grip"), _js_matrix_to_transform(grip_pose), Vector3(), Vector3()); + } - int *buttons = godot_webxr_get_controller_buttons(p_controller_id); - if (buttons) { - // TODO buttons should be named properly, this is just a temporary fix - for (int i = 0; i < buttons[0]; i++) { - char name[1024]; - sprintf(name, "button_%i", i); + for (int i = 0; i < button_count; i++) { + StringName button_name = has_standard_mapping ? standard_button_names[i] : unknown_button_names[i]; + StringName button_pressure_name = has_standard_mapping ? standard_button_pressure_names[i] : unknown_button_pressure_names[i]; + float value = buttons[i]; + bool state = value > 0.0; + tracker->set_input(button_name, state); + tracker->set_input(button_pressure_name, value); + } - float value = *((float *)buttons + (i + 1)); - bool state = value > 0.0; - tracker->set_input(name, state); - } - free(buttons); + for (int i = 0; i < axes_count; i++) { + StringName axis_name = has_standard_mapping ? standard_axis_names[i] : unknown_axis_names[i]; + float value = axes[i]; + if (has_standard_mapping && (i == 1 || i == 3)) { + // Invert the Y-axis on thumbsticks and trackpads, in order to + // match OpenXR and other XR platform SDKs. + value = -value; } + tracker->set_input(axis_name, value); + } - int *axes = godot_webxr_get_controller_axes(p_controller_id); - if (axes) { - // TODO again just a temporary fix, split these between proper float and vector2 inputs - for (int i = 0; i < axes[0]; i++) { - char name[1024]; - sprintf(name, "axis_%i", i); + // Also create Vector2's for the thumbstick and trackpad when we have the + // standard mapping. + if (has_standard_mapping) { + if (axes_count >= 2) { + tracker->set_input(standard_vector_names[0], Vector2(axes[0], -axes[1])); + } + if (axes_count >= 4) { + tracker->set_input(standard_vector_names[1], Vector2(axes[2], -axes[3])); + } + } - float value = *((float *)axes + (i + 1)); - tracker->set_input(name, value); + if (input_source.target_ray_mode == WebXRInterface::TARGET_RAY_MODE_SCREEN) { + if (touch_index < 5 && axes_count >= 2) { + Vector2 joy_vector = Vector2(axes[0], axes[1]); + Vector2 position = _get_screen_position_from_joy_vector(joy_vector); + + if (touches[touch_index].is_touching) { + Vector2 delta = position - touches[touch_index].position; + + // If position has changed by at least 1 pixel, generate a drag event. + if (abs(delta.x) >= 1.0 || abs(delta.y) >= 1.0) { + Ref<InputEventScreenDrag> event; + event.instantiate(); + event->set_index(touch_index); + event->set_position(position); + event->set_relative(delta); + Input::get_singleton()->parse_input_event(event); + } } - free(axes); + + touches[touch_index].position = position; } - } else if (tracker.is_valid()) { - xr_server->remove_tracker(tracker); - controllers[p_controller_id].unref(); } } -void WebXRInterfaceJS::_on_controller_changed() { - // Register "virtual" gamepads with Godot for the ones we get from WebXR. - godot_webxr_sample_controller_data(); - for (int i = 0; i < 2; i++) { - bool controller_connected = godot_webxr_is_controller_connected(i); - if (controllers_state[i] != controller_connected) { - // Input::get_singleton()->joy_connection_changed(i + 100, controller_connected, i == 0 ? "Left" : "Right", ""); - controllers_state[i] = controller_connected; +void WebXRInterfaceJS::_on_input_event(int p_event_type, int p_input_source_id) { + // Get the latest data for this input source. For transient input sources, + // we may not have any data at all yet! + _update_input_source(p_input_source_id); + + if (p_event_type == WEBXR_INPUT_EVENT_SELECTSTART || p_event_type == WEBXR_INPUT_EVENT_SELECTEND) { + const InputSource &input_source = input_sources[p_input_source_id]; + if (input_source.target_ray_mode == WebXRInterface::TARGET_RAY_MODE_SCREEN) { + int touch_index = input_source.touch_index; + if (touch_index >= 0 && touch_index < 5) { + touches[touch_index].is_touching = (p_event_type == WEBXR_INPUT_EVENT_SELECTSTART); + + Ref<InputEventScreenTouch> event; + event.instantiate(); + event->set_index(touch_index); + event->set_position(touches[touch_index].position); + event->set_pressed(p_event_type == WEBXR_INPUT_EVENT_SELECTSTART); + + Input::get_singleton()->parse_input_event(event); + } } } + + switch (p_event_type) { + case WEBXR_INPUT_EVENT_SELECTSTART: + emit_signal("selectstart", p_input_source_id); + break; + + case WEBXR_INPUT_EVENT_SELECTEND: + emit_signal("selectend", p_input_source_id); + // Emit the 'select' event on our own (rather than intercepting the + // one from JavaScript) so that we don't have to needlessly call + // _update_input_source() a second time. + emit_signal("select", p_input_source_id); + break; + + case WEBXR_INPUT_EVENT_SQUEEZESTART: + emit_signal("squeezestart", p_input_source_id); + break; + + case WEBXR_INPUT_EVENT_SQUEEZEEND: + emit_signal("squeezeend", p_input_source_id); + // Again, we emit the 'squeeze' event on our own to avoid extra work. + emit_signal("squeeze", p_input_source_id); + break; + } +} + +Vector2 WebXRInterfaceJS::_get_screen_position_from_joy_vector(const Vector2 &p_joy_vector) { + SceneTree *scene_tree = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop()); + if (!scene_tree) { + return Vector2(); + } + + Window *viewport = scene_tree->get_root(); + + Vector2 position_percentage((p_joy_vector.x + 1.0f) / 2.0f, ((p_joy_vector.y) + 1.0f) / 2.0f); + Vector2 position = (Size2)viewport->get_size() * position_percentage; + + return position; } WebXRInterfaceJS::WebXRInterfaceJS() { diff --git a/modules/webxr/webxr_interface_js.h b/modules/webxr/webxr_interface_js.h index 319adc2ac9..6b484a8872 100644 --- a/modules/webxr/webxr_interface_js.h +++ b/modules/webxr/webxr_interface_js.h @@ -39,6 +39,10 @@ The WebXR interface is a VR/AR interface that can be used on the web. */ +namespace GLES3 { +class TextureStorage; +} + class WebXRInterfaceJS : public WebXRInterface { GDCLASS(WebXRInterfaceJS, WebXRInterface); @@ -53,13 +57,32 @@ private: String requested_reference_space_types; String reference_space_type; - // TODO maybe turn into a vector to support more then 2 controllers... - bool controllers_state[2]; - Ref<XRPositionalTracker> controllers[2]; Size2 render_targetsize; - + RBMap<unsigned int, RID> texture_cache; + struct Touch { + bool is_touching = false; + Vector2 position; + } touches[5]; + + static constexpr uint8_t input_source_count = 16; + + struct InputSource { + Ref<XRPositionalTracker> tracker; + bool active = false; + TargetRayMode target_ray_mode; + int touch_index = -1; + } input_sources[input_source_count]; + + RID color_texture; + RID depth_texture; + + RID _get_color_texture(); + RID _get_depth_texture(); + RID _get_texture(unsigned int p_texture_id); Transform3D _js_matrix_to_transform(float *p_js_matrix); - void _update_tracker(int p_controller_id); + void _update_input_source(int p_input_source_id); + + Vector2 _get_screen_position_from_joy_vector(const Vector2 &p_joy_vector); public: virtual void is_session_supported(const String &p_session_mode) override; @@ -73,9 +96,11 @@ public: virtual String get_requested_reference_space_types() const override; void _set_reference_space_type(String p_reference_space_type); virtual String get_reference_space_type() const override; - virtual Ref<XRPositionalTracker> get_controller(int p_controller_id) const override; + virtual bool is_input_source_active(int p_input_source_id) const override; + virtual Ref<XRPositionalTracker> get_input_source_tracker(int p_input_source_id) const override; + virtual TargetRayMode get_input_source_target_ray_mode(int p_input_source_id) const override; virtual String get_visibility_state() const override; - virtual PackedVector3Array get_bounds_geometry() const override; + virtual PackedVector3Array get_play_area() const override; virtual StringName get_name() const override; virtual uint32_t get_capabilities() const override; @@ -89,14 +114,129 @@ public: virtual Transform3D get_camera_transform() override; virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override; virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override; + virtual bool pre_draw_viewport(RID p_render_target) override; virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override; + virtual RID get_color_texture() override; + virtual RID get_depth_texture() override; + virtual RID get_velocity_texture() override; virtual void process() override; - void _on_controller_changed(); + void _on_input_event(int p_event_type, int p_input_source_id); WebXRInterfaceJS(); ~WebXRInterfaceJS(); + +private: + StringName tracker_names[16] = { + StringName("left_hand"), + StringName("right_hand"), + StringName("tracker_2"), + StringName("tracker_3"), + StringName("tracker_4"), + StringName("tracker_5"), + StringName("tracker_6"), + StringName("tracker_7"), + StringName("tracker_8"), + StringName("tracker_9"), + StringName("tracker_10"), + StringName("tracker_11"), + StringName("tracker_12"), + StringName("tracker_13"), + StringName("tracker_14"), + StringName("tracker_15"), + }; + + StringName touch_names[5] = { + StringName("touch_0"), + StringName("touch_1"), + StringName("touch_2"), + StringName("touch_3"), + StringName("touch_4"), + }; + + StringName standard_axis_names[10] = { + StringName("touchpad_x"), + StringName("touchpad_y"), + StringName("thumbstick_x"), + StringName("thumbstick_y"), + StringName("axis_4"), + StringName("axis_5"), + StringName("axis_6"), + StringName("axis_7"), + StringName("axis_8"), + StringName("axis_9"), + }; + + StringName standard_vector_names[2] = { + StringName("touchpad"), + StringName("thumbstick"), + }; + + StringName standard_button_names[10] = { + StringName("trigger_click"), + StringName("grip_click"), + StringName("touchpad_click"), + StringName("thumbstick_click"), + StringName("ax_button"), + StringName("by_button"), + StringName("button_6"), + StringName("button_7"), + StringName("button_8"), + StringName("button_9"), + }; + + StringName standard_button_pressure_names[10] = { + StringName("trigger"), + StringName("grip"), + StringName("touchpad_click_pressure"), + StringName("thumbstick_click_pressure"), + StringName("ax_button_pressure"), + StringName("by_button_pressure"), + StringName("button_pressure_6"), + StringName("button_pressure_7"), + StringName("button_pressure_8"), + StringName("button_pressure_9"), + }; + + StringName unknown_button_names[10] = { + StringName("button_0"), + StringName("button_1"), + StringName("button_2"), + StringName("button_3"), + StringName("button_4"), + StringName("button_5"), + StringName("button_6"), + StringName("button_7"), + StringName("button_8"), + StringName("button_9"), + }; + + StringName unknown_axis_names[10] = { + StringName("axis_0"), + StringName("axis_1"), + StringName("axis_2"), + StringName("axis_3"), + StringName("axis_4"), + StringName("axis_5"), + StringName("axis_6"), + StringName("axis_7"), + StringName("axis_8"), + StringName("axis_9"), + }; + + StringName unknown_button_pressure_names[10] = { + StringName("button_pressure_0"), + StringName("button_pressure_1"), + StringName("button_pressure_2"), + StringName("button_pressure_3"), + StringName("button_pressure_4"), + StringName("button_pressure_5"), + StringName("button_pressure_6"), + StringName("button_pressure_7"), + StringName("button_pressure_8"), + StringName("button_pressure_9"), + }; }; #endif // WEB_ENABLED |