Fix pass properties order in native editor (#14468)

* fix pass properties order in native editor
This commit is contained in:
江战
2023-03-14 10:26:00 +08:00
committed by GitHub
parent bccd65fbd9
commit cfb0b74c4e
4 changed files with 90 additions and 11 deletions
@@ -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<T> *to, se::Object *
return false;
}
template <typename K, typename V>
bool sevalue_to_native(const se::Value &from, cc::StablePropertyMap<K, V> *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<ccstd::string> 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<R(Args...)> & /*from*/, se::Va
return false;
}
template <typename K, typename V>
bool nativevalue_to_se(const cc::StablePropertyMap<K, V> &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 <int i, typename T>
+38 -3
View File
@@ -25,6 +25,7 @@
#pragma once
#include <tuple>
#include <type_traits>
#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<pair> is used
// instead. Since there is no scenario where these data objects are randomly inserted,
// only a find interface is provided.
template <typename K, typename V>
class StablePropertyMap : public ccstd::vector<std::pair<K, V>> { // NOLINT
using Super = ccstd::vector<std::pair<K, V>>;
public:
auto find(const K &key) const {
auto *self = static_cast<const Super *>(this);
return std::find_if(self->begin(), self->end(), [&](auto &ele) {
return ele.first == key;
});
}
};
#endif
template <typename K, typename V>
using UnstablePropertyContainer = ccstd::unordered_map<K, V>;
template <typename K, typename V>
using StablePropertyContainer = StablePropertyMap<K, V>;
#if CC_EDITOR
template <typename K, typename V>
using PropertyContainer = StablePropertyContainer<K, V>;
#else
template <typename K, typename V>
using PropertyContainer = UnstablePropertyContainer<K, V>;
#endif
using IPropertyHandleInfo = std::tuple<ccstd::string, uint32_t, gfx::Type>;
using IPropertyValue = ccstd::variant<ccstd::monostate, ccstd::vector<float>, ccstd::string>;
using IPropertyEditorValueType = ccstd::variant<ccstd::monostate, ccstd::string, bool, float, ccstd::vector<float>>;
using IPropertyEditorInfo = ccstd::unordered_map<ccstd::string, IPropertyEditorValueType>;
using IPropertyEditorInfo = PropertyContainer<ccstd::string, IPropertyEditorValueType>;
struct IPropertyInfo {
int32_t type{0}; // auto-extracted from shader
@@ -351,7 +386,7 @@ struct IPassStates {
};
using PassOverrides = IPassStates;
using PassPropertyInfoMap = ccstd::unordered_map<ccstd::string, IPropertyInfo>;
using PassPropertyInfoMap = PropertyContainer<ccstd::string, IPropertyInfo>;
struct IPassInfoFull final { // cjh } : public IPassInfo {
// IPassStates
+6 -6
View File
@@ -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<ccstd::string>(&iter->second.value.value());
const ccstd::string *pStrVal = ccstd::get_if<ccstd::string>(&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<ccstd::vector<float>>(givenDefault.value())
(iter != _properties.end() && iter->second.value.has_value()
? ccstd::get<ccstd::vector<float>>(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()) {
+2 -2
View File
@@ -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<ccstd::string, IPropertyInfo> &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<ccstd::string, IPropertyInfo> _properties;
PassPropertyInfoMap _properties;
IntrusivePtr<gfx::Shader> _shader;
gfx::BlendState _blendState{};
gfx::DepthStencilState _depthStencilState{};