From 19867ffca6dca7995c799354081219c9e76f13d1 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 20 Feb 2025 14:48:51 -0800 Subject: Simplify implicit cast ctors for vector & matrix. (#6408) * Simplify implicit cast ctors for vector & matrix. * Fix formatting. * Fix tests. * Fix Falcor test. * Mark __builtin_cast as internal. --- source/slang/core.meta.slang | 93 ++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 69 deletions(-) (limited to 'source/slang/core.meta.slang') diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang index b7f50cd1b..267f7b2d4 100644 --- a/source/slang/core.meta.slang +++ b/source/slang/core.meta.slang @@ -1,4 +1,4 @@ -//public module core; +public module core; // Slang `core` library @@ -2367,49 +2367,6 @@ __generic __extension vector ${{{{ -// The above extensions are generic in the *type* of the vector, -// but explicit in the *size*. We will now declare an extension -// for each builtin type that is generic in the size. -// -for (int tt = 0; tt < kBaseTypeCount; ++tt) -{ - if(kBaseTypes[tt].tag == BaseType::Void) continue; - - sb << "__generic __extension vector<" - << kBaseTypes[tt].name << ",N>\n{\n"; - - for (int ff = 0; ff < kBaseTypeCount; ++ff) - { - if(kBaseTypes[ff].tag == BaseType::Void) continue; - - - if( tt != ff ) - { - auto cost = getBaseTypeConversionCost( - kBaseTypes[tt], - kBaseTypes[ff]); - auto op = getBaseTypeConversionOp( - kBaseTypes[tt], - kBaseTypes[ff]); - - // Implicit conversion from a vector of the same - // size, but different element type. - sb << " __implicit_conversion(" << cost << ")\n"; - sb << " __intrinsic_op(" << int(op) << ")\n"; - sb << " __init(vector<" << kBaseTypes[ff].name << ",N> value);\n"; - - // Constructor to make a vector from a scalar of another type. - if (cost != kConversionCost_Impossible) - { - cost += kConversionCost_ScalarToVector; - sb << " __implicit_conversion(" << cost << ")\n"; - sb << " [__unsafeForceInlineEarly]\n"; - sb << " __init(" << kBaseTypes[ff].name << " value) { this = vector<" << kBaseTypes[tt].name << ",N>( " << kBaseTypes[tt].name << "(value)); }\n"; - } - } - } - sb << "}\n"; -} for( int R = 1; R <= 4; ++R ) for( int C = 1; C <= 4; ++C ) @@ -2464,38 +2421,36 @@ for( int C = 1; C <= 4; ++C ) sb << "}\n"; } -for (int tt = 0; tt < kBaseTypeCount; ++tt) -{ - if(kBaseTypes[tt].tag == BaseType::Void) continue; - auto toType = kBaseTypes[tt].name; }}}} -__generic extension matrix<$(toType),R,C,L> +//@hidden: +__intrinsic_op($(kIROp_BuiltinCast)) +internal T __builtin_cast(U u); + +// If T is implicitly convertible to U, then vector is implicitly convertible to vector. +__generic extension vector { -${{{{ - for (int ff = 0; ff < kBaseTypeCount; ++ff) - { - if(kBaseTypes[ff].tag == BaseType::Void) continue; - if( tt == ff ) continue; + __implicit_conversion(constraint) + __intrinsic_op(BuiltinCast) + __init(vector value) where ToType(FromType) implicit; - auto cost = getBaseTypeConversionCost( - kBaseTypes[tt], - kBaseTypes[ff]); - auto fromType = kBaseTypes[ff].name; - auto op = getBaseTypeConversionOp( - kBaseTypes[tt], - kBaseTypes[ff]); -}}}} - __implicit_conversion($(cost)) - __intrinsic_op($(op)) - __init(matrix<$(fromType),R,C,L> value); -${{{{ + __implicit_conversion(constraint+) + [__unsafeForceInlineEarly] + [__readNone] + [TreatAsDifferentiable] + __init(FromType value) where ToType(FromType) implicit + { + this = __builtin_cast>(vector(value)); } -}}}} } -${{{{ + +// If T is implicitly convertible to U, then matrix is implicitly convertible to matrix. +__generic extension matrix +{ + __implicit_conversion(constraint) + __intrinsic_op(BuiltinCast) + __init(matrix value) where ToType(FromType) implicit; } -}}}} //@ hidden: __generic -- cgit v1.2.3