diff --git a/native/cocos/bindings/manual/jsb_conversions.h b/native/cocos/bindings/manual/jsb_conversions.h index 7faf48d499..b59fc293ce 100644 --- a/native/cocos/bindings/manual/jsb_conversions.h +++ b/native/cocos/bindings/manual/jsb_conversions.h @@ -46,6 +46,7 @@ #include "cocos/editor-support/spine-creator-support/spine-cocos2dx.h" #endif +#include "core/assets/EffectAsset.h" #include "core/geometry/Geometry.h" #include "math/Color.h" #include "math/Math.h" @@ -790,6 +791,30 @@ bool sevalue_to_native(const se::Value &from, ccstd::vector *to, se::Object * return false; } +template +bool sevalue_to_native(const se::Value &from, cc::StablePropertyMap *to, se::Object *ctx) { // NOLINT + // convert object to attribute/value list: [{"prop1", v1}, {"prop2", v2}... {"propN", vn}] + CC_ASSERT_NOT_NULL(to); + CC_ASSERT(from.isObject()); + auto *jsObj = from.toObject(); + ccstd::vector objectKeys; + jsObj->getAllKeys(&objectKeys); + to->clear(); + se::Value valueJS; + for (auto &attr : objectKeys) { + V value; + + if (!jsObj->getProperty(attr, &valueJS)) { + continue; + } + if (!sevalue_to_native(valueJS, &value, ctx)) { + continue; + } + to->emplace_back(std::make_pair(std::move(attr), std::move(value))); + } + return true; +} + ///////////////////// function /// @@ -1299,6 +1324,25 @@ inline bool nativevalue_to_se(const std::function & /*from*/, se::Va return false; } +template +bool nativevalue_to_se(const cc::StablePropertyMap &from, se::Value &to, se::Object *ctx) { // NOLINT(readability-identifier-naming + // convert to object from attribute/value list: [{"prop1", v1}, {"prop2", v2}... {"propN", vn}] + se::HandleObject ret(se::Object::createPlainObject()); + for (const auto &ele : from) { + se::Value keyJS; + se::Value valueJS; + if (!nativevalue_to_se(ele.first, keyJS, ctx)) { + continue; + } + if (!nativevalue_to_se(ele.second, valueJS, ctx)) { + continue; + } + ret->setProperty(keyJS.toString(), valueJS); + } + to.setObject(ret); + return true; +} + ///////////////////////// function /////////////////////// template diff --git a/native/cocos/core/assets/EffectAsset.h b/native/cocos/core/assets/EffectAsset.h index 23c560867f..f78846df99 100644 --- a/native/cocos/core/assets/EffectAsset.h +++ b/native/cocos/core/assets/EffectAsset.h @@ -25,6 +25,7 @@ #pragma once #include +#include #include "base/std/container/string.h" #include "base/std/container/unordered_map.h" #include "base/std/optional.h" @@ -36,15 +37,49 @@ #include "renderer/core/PassUtils.h" #include "renderer/gfx-base/GFXDef.h" #include "renderer/pipeline/Define.h" - namespace cc { +// To avoid errors when generating code using SWIG. +#if !SWIGCOCOS + +// The properties in Pass are obtained from an asset file. If they are directly stored in an unordered_map, +// the order of these properties may become scrambled. To maintain their order, a vector is used +// instead. Since there is no scenario where these data objects are randomly inserted, +// only a find interface is provided. +template +class StablePropertyMap : public ccstd::vector> { // NOLINT + using Super = ccstd::vector>; + +public: + auto find(const K &key) const { + auto *self = static_cast(this); + return std::find_if(self->begin(), self->end(), [&](auto &ele) { + return ele.first == key; + }); + } +}; +#endif + +template +using UnstablePropertyContainer = ccstd::unordered_map; + +template +using StablePropertyContainer = StablePropertyMap; + +#if CC_EDITOR +template +using PropertyContainer = StablePropertyContainer; +#else +template +using PropertyContainer = UnstablePropertyContainer; +#endif + using IPropertyHandleInfo = std::tuple; using IPropertyValue = ccstd::variant, ccstd::string>; using IPropertyEditorValueType = ccstd::variant>; -using IPropertyEditorInfo = ccstd::unordered_map; +using IPropertyEditorInfo = PropertyContainer; struct IPropertyInfo { int32_t type{0}; // auto-extracted from shader @@ -351,7 +386,7 @@ struct IPassStates { }; using PassOverrides = IPassStates; -using PassPropertyInfoMap = ccstd::unordered_map; +using PassPropertyInfoMap = PropertyContainer; struct IPassInfoFull final { // cjh } : public IPassInfo { // IPassStates diff --git a/native/cocos/scene/Pass.cpp b/native/cocos/scene/Pass.cpp index 749237f511..c80da61826 100644 --- a/native/cocos/scene/Pass.cpp +++ b/native/cocos/scene/Pass.cpp @@ -27,6 +27,7 @@ #include "cocos/bindings/jswrapper/SeApi.h" #include "cocos/renderer/pipeline/custom/RenderingModule.h" #include "core/Root.h" +#include "core/assets/EffectAsset.h" #include "core/assets/TextureBase.h" #include "core/builtin/BuiltinResMgr.h" #include "core/platform/Debug.h" @@ -379,12 +380,12 @@ void Pass::resetTexture(const ccstd::string &name, uint32_t index) { const gfx::Type type = Pass::getTypeFromHandle(handle); const uint32_t binding = Pass::getBindingFromHandle(handle); ccstd::string texName; - IPropertyInfo *info = nullptr; + const IPropertyInfo *info = nullptr; auto iter = _properties.find(name); if (iter != _properties.end()) { if (iter->second.value.has_value()) { info = &iter->second; - ccstd::string *pStrVal = ccstd::get_if(&iter->second.value.value()); + const ccstd::string *pStrVal = ccstd::get_if(&iter->second.value.value()); if (pStrVal != nullptr) { texName = (*pStrVal) + getStringFromType(type); } @@ -418,11 +419,10 @@ void Pass::resetUBOs() { uint32_t ofs = 0; for (const auto &cur : u.members) { const auto &block = _blocks[u.binding]; - const auto &info = _properties[cur.name]; - const auto &givenDefault = info.value; + auto iter = _properties.find(cur.name); const auto &value = - (givenDefault.has_value() - ? ccstd::get>(givenDefault.value()) + (iter != _properties.end() && iter->second.value.has_value() + ? ccstd::get>(iter->second.value.value()) : getDefaultFloatArrayFromType(cur.type)); const uint32_t size = (gfx::getTypeSize(cur.type) >> 2) * cur.count; for (size_t k = 0; (k + value.size()) <= size; k += value.size()) { diff --git a/native/cocos/scene/Pass.h b/native/cocos/scene/Pass.h index d80a83ef23..184580f8a4 100644 --- a/native/cocos/scene/Pass.h +++ b/native/cocos/scene/Pass.h @@ -266,7 +266,7 @@ public: inline const IProgramInfo *getShaderInfo() const { return _shaderInfo; } const gfx::DescriptorSetLayout *getLocalSetLayout() const; inline const ccstd::string &getProgram() const { return _programName; } - inline const Record &getProperties() const { return _properties; } + inline const PassPropertyInfoMap &getProperties() const { return _properties; } inline const MacroRecord &getDefines() const { return _defines; } inline MacroRecord &getDefines() { return _defines; } inline index_t getPassIndex() const { return _passIndex; } @@ -342,7 +342,7 @@ protected: const IProgramInfo *_shaderInfo; // weakref to template of ProgramLib MacroRecord _defines; - Record _properties; + PassPropertyInfoMap _properties; IntrusivePtr _shader; gfx::BlendState _blendState{}; gfx::DepthStencilState _depthStencilState{};