/**************************************************************************** Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd. https://www.cocos.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #pragma once #include #include #include #include #include #include #include #include "../gfx-base/GFXDef-common.h" #include "../gfx-base/GFXDef.h" #ifdef CC_WGPU_WASM #define EXPORT_EMS(expr) expr #else #define EXPORT_EMS(expr) #endif #ifdef CC_WGPU_DAWN #define EXPORT_DAWN(expr) expr #else #define EXPORT_DAWN(expr) #endif #ifdef CC_WGPU_RS #define EXPORT_RS(expr) expr #else #define EXPORT_RS(expr) #endif // https://github.com/emscripten-core/emscripten/issues/11070#issuecomment-717675128 namespace emscripten { template std::vector vecFromEMS(const val &vals) { uint32_t len = vals["length"].as(); std::vector res(len); const std::vector Ts = vecFromJSArray(vals); for (size_t i = 0; i < len; ++i) { const val &t = Ts[i]; res[i] = static_cast(t.as(allow_raw_pointers())); } return res; } template val vecToEMS(const std::vector &Ts) { auto arr = val::array(); for (size_t i = 0; i < Ts.size(); ++i) { arr.set(i, TWrapper(Ts[i])); } return arr; } // template ::value, bool>::type = true> // std::vector ptrVecFromEMS(const val& vals) { // uint32_t len = vals["length"].as(); // std::vector res(len); // const std::vector Ts = vecFromJSArray(vals); // for (size_t i = 0; i < len; ++i) { // const val& t = Ts[i]; // t.as(emscripten::allow_raw_pointers()); // res[i] = reinterpret_cast(t.as()); // } // return res; // } template ::value, bool>::type = true> val ptrVecToEMS(const std::vector &Ts) { auto arr = val::array(); for (size_t i = 0; i < Ts.size(); ++i) { arr.set(i, Ts[i]); } return arr; } namespace internal { // vector -> [] & [] ->vector template struct BindingType> { using ValBinding = BindingType; using WireType = ValBinding::WireType; static WireType toWireType(const std::vector &vec) { return ValBinding::toWireType(vecToEMS(vec)); } static std::vector fromWireType(WireType value) { return vecFromJSArray(ValBinding::fromWireType(value), allow_raw_pointers()); } }; template struct TypeID::type, std::vector::type::value_type, typename Canonicalized::type::allocator_type>>::value>> { static constexpr TYPEID get() { return TypeID::get(); } }; } // namespace internal } // namespace emscripten template struct GenInstance { static T instance() { return T(); } }; template R getMemberType(R T::*); #define MEMBER_TYPE(prop) decltype(getMemberType(prop)) template struct Exporter { Exporter(T &t, const char *propName, U V::*field, bool ignorePtrAssert = false) { t.field(propName, field); } }; template struct Exporter::value>::type> { Exporter(T &t, const char *propName, U V::*field, bool ignorePtrAssert = false) { std::function u)> set = [field](V &v, std::underlying_type_t u) { v.*field = U{u}; }; std::function(const V &v)> get = [field](const V &v) { return static_cast>(v.*field); }; t.field(propName, get, set); } }; template struct Exporter::value>::type> { Exporter(T &t, const char *propName, U V::*field) { static_assert(!std::is_pointer::value, "Export pointer with struct, try EXPORT_STRUCT_NPOD!"); } Exporter(T &t, const char *propName, U V::*field, bool ignorePtrAssert) { t.field(propName, field); } }; #define PROCESS_STRUCT_MEMBERS(r, struct_name, property) \ { Exporter exporter(obj, BOOST_PP_STRINGIZE(property), &struct_name::property); } #define EXPORT_STRUCT_POD(struct_name, ...) \ { \ auto obj = value_object(#struct_name); \ BOOST_PP_SEQ_FOR_EACH(PROCESS_STRUCT_MEMBERS, struct_name, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)); \ } #define PROCESS_STRUCT_MEMBERS_MAY_BE_PTR(r, struct_name, property) \ { Exporter exporter(obj, BOOST_PP_STRINGIZE(property), &struct_name::property, true); } #define EXPORT_STRUCT_NPOD(struct_name, ...) \ { \ auto obj = value_object(#struct_name); \ BOOST_PP_SEQ_FOR_EACH(PROCESS_STRUCT_MEMBERS_MAY_BE_PTR, struct_name, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)); \ } #define SPECIALIZE_PTR_FOR_STRUCT(r, _, TYPE) \ template <> \ struct emscripten::internal::TypeID::type *, void> { \ static constexpr emscripten::internal::TYPEID get() { return emscripten::internal::TypeID>::get(); } \ }; \ template <> \ struct emscripten::internal::TypeID::type *, void> { \ static constexpr emscripten::internal::TYPEID get() { return emscripten::internal::TypeID>::get(); } \ }; #define REGISTER_GFX_PTRS_FOR_STRUCT(...) \ BOOST_PP_SEQ_FOR_EACH(SPECIALIZE_PTR_FOR_STRUCT, _, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)); // specialize for void* template <> struct emscripten::internal::TypeID { static constexpr emscripten::internal::TYPEID get() { return emscripten::internal::TypeID::get(); } }; template struct emscripten::internal::TypeID::value>::type> { static constexpr emscripten::internal::TYPEID get() { return emscripten::internal::TypeID::type>::get(); } }; namespace cc::gfx { using ::emscripten::allow_raw_pointers; using ::emscripten::convertJSArrayToNumberVector; using ::emscripten::val; // template ::value, bool>::type = true> // std::vector ptrVecFromEMS(const val& vals) { // uint32_t len = vals["length"].as(); // std::vector res(len); // const std::vector Ts = vecFromJSArray(vals); // for (size_t i = 0; i < len; ++i) { // const val& t = Ts[i]; // t.as(emscripten::allow_raw_pointers()); // res[i] = reinterpret_cast(t.as()); // } // return res; // } template struct GetType { using type = T; }; template struct GetType::value>::type> { using type = typename std::underlying_type::type; }; } // namespace cc::gfx