Skip to content

Commit eea3c81

Browse files
authored
Merge branch 'master' into cheneym2/precompiledtestcrash
2 parents 2833a3b + f7b9745 commit eea3c81

File tree

93 files changed

+2746
-578
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2746
-578
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
- uses: actions/checkout@v4
8686
with:
8787
submodules: "recursive"
88-
fetch-depth: "0"
88+
fetch-depth: "2"
8989
- id: filter
9090
run: |
9191
# This step prevents subsequent steps from running if only documentation was changed

cmake/CompilerFlags.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ function(set_default_compile_options target)
205205
$<$<BOOL:${SLANG_ENABLE_FULL_DEBUG_VALIDATION}>:SLANG_ENABLE_FULL_IR_VALIDATION>
206206
$<$<BOOL:${SLANG_ENABLE_IR_BREAK_ALLOC}>:SLANG_ENABLE_IR_BREAK_ALLOC>
207207
$<$<BOOL:${SLANG_ENABLE_DX_ON_VK}>:SLANG_CONFIG_DX_ON_VK>
208+
$<$<STREQUAL:${SLANG_LIB_TYPE},STATIC>:STB_IMAGE_STATIC>
208209
)
209210

210211
if(SLANG_ENABLE_ASAN)

docs/building.md

+15
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@ cmake -B build -G Ninja
321321
cmake --build build -j
322322
```
323323

324+
## Static linking against libslang
325+
326+
If linking against a static `libslang.a` you will need to link against some
327+
dependencies also if you're not already incorporating them into your project.
328+
329+
You will need to link against:
330+
331+
```
332+
${SLANG_DIR}/build/Release/lib/libslang.a
333+
${SLANG_DIR}/build/Release/lib/libcompiler-core.a
334+
${SLANG_DIR}/build/Release/lib/libcore.a
335+
${SLANG_DIR}/build/external/miniz/libminiz.a
336+
${SLANG_DIR}/build/external/lz4/build/cmake/liblz4.a
337+
```
338+
324339
## Notes
325340

326341
[^1] below 3.25, CMake lacks the ability to mark directories as being

docs/design/stdlib-intrinsics.md

-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ Sections of the `expansion` string that are to be replaced are prefixed by the `
114114
* $XH - Ray tracing hit object attribute
115115
* $P - Type-based prefix as used for CUDA and C++ targets (I8 for int8_t, F32 - float etc)
116116

117-
## __specialized_for_target(target)
118-
119-
Specialized for target allows defining an implementation *body* for a particular target. The target is the same as is used for [__target_intrinsic](#target-intrinsic).
120-
121-
A declaration can consist of multiple definitions with bodies (for each target) using, `specialized_for_target`, as well as having `target_intrinsic` if that is applicable for a target.
122-
123117
## __attributeTarget(astClassName)
124118

125119
For an attribute, specifies the AST class (and derived class) the attribute can be applied to.

external/CMakeLists.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,19 @@ endif()
8989

9090
# imgui
9191
add_library(imgui INTERFACE)
92-
target_include_directories(imgui INTERFACE "${CMAKE_CURRENT_LIST_DIR}/imgui")
92+
target_include_directories(
93+
imgui
94+
${system}
95+
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/imgui"
96+
)
9397

9498
# stb
9599
add_library(stb INTERFACE)
96-
target_include_directories(stb INTERFACE "${CMAKE_CURRENT_LIST_DIR}/stb")
100+
target_include_directories(
101+
stb
102+
${system}
103+
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/stb"
104+
)
97105

98106
# slang-rhi
99107
if(SLANG_ENABLE_SLANG_RHI)

source/core/slang-string.h

+51
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,57 @@ class SLANG_RT_API String
790790
UnownedStringSlice getUnownedSlice() const { return StringRepresentation::asSlice(m_buffer); }
791791
};
792792

793+
class ImmutableHashedString
794+
{
795+
public:
796+
String slice;
797+
HashCode64 hashCode;
798+
ImmutableHashedString()
799+
: hashCode(0)
800+
{
801+
}
802+
ImmutableHashedString(const UnownedStringSlice& slice)
803+
: slice(slice), hashCode(slice.getHashCode())
804+
{
805+
}
806+
ImmutableHashedString(const char* begin, const char* end)
807+
: slice(begin, end), hashCode(slice.getHashCode())
808+
{
809+
}
810+
ImmutableHashedString(const char* begin, size_t len)
811+
: slice(UnownedStringSlice(begin, len)), hashCode(slice.getHashCode())
812+
{
813+
}
814+
ImmutableHashedString(const char* begin)
815+
: slice(begin), hashCode(slice.getHashCode())
816+
{
817+
}
818+
ImmutableHashedString(const String& str)
819+
: slice(str), hashCode(str.getHashCode())
820+
{
821+
}
822+
ImmutableHashedString(String&& str)
823+
: slice(_Move(str)), hashCode(str.getHashCode())
824+
{
825+
}
826+
ImmutableHashedString(const ImmutableHashedString& other) = default;
827+
ImmutableHashedString& operator=(const ImmutableHashedString& other) = default;
828+
bool operator==(const ImmutableHashedString& other) const
829+
{
830+
return hashCode == other.hashCode && slice == other.slice;
831+
}
832+
bool operator!=(const ImmutableHashedString& other) const
833+
{
834+
return hashCode != other.hashCode || slice != other.slice;
835+
}
836+
bool operator==(const UnownedStringSlice& other) const { return slice == other; }
837+
bool operator!=(const UnownedStringSlice& other) const { return slice != other; }
838+
bool operator==(const String& other) const { return slice == other.getUnownedSlice(); }
839+
bool operator!=(const String& other) const { return slice != other.getUnownedSlice(); }
840+
bool operator==(const char* other) const { return slice == UnownedStringSlice(other); }
841+
HashCode64 getHashCode() const { return hashCode; }
842+
};
843+
793844
class SLANG_RT_API StringBuilder : public String
794845
{
795846
private:

source/slang/core.meta.slang

+30-73
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//public module core;
1+
public module core;
22

33
// Slang `core` library
44

@@ -603,6 +603,7 @@ void static_assert(constexpr bool condition, NativeString errorMessage);
603603
///
604604
///
605605
__magic_type(DifferentiableType)
606+
[KnownBuiltin("IDifferentiable")]
606607
interface IDifferentiable
607608
{
608609
// Note: the compiler implementation requires the `Differential` associated type to be defined
@@ -645,6 +646,7 @@ interface IDifferentiable
645646
/// @remarks Support for this interface is still experimental and subject to change.
646647
///
647648
__magic_type(DifferentiablePtrType)
649+
[KnownBuiltin("IDifferentiablePtr")]
648650
interface IDifferentiablePtrType
649651
{
650652
__builtin_requirement($( (int)BuiltinRequirementKind::DifferentialPtrType) )
@@ -2367,49 +2369,6 @@ __generic<T> __extension vector<T, 4>
23672369

23682370
${{{{
23692371

2370-
// The above extensions are generic in the *type* of the vector,
2371-
// but explicit in the *size*. We will now declare an extension
2372-
// for each builtin type that is generic in the size.
2373-
//
2374-
for (int tt = 0; tt < kBaseTypeCount; ++tt)
2375-
{
2376-
if(kBaseTypes[tt].tag == BaseType::Void) continue;
2377-
2378-
sb << "__generic<let N : int> __extension vector<"
2379-
<< kBaseTypes[tt].name << ",N>\n{\n";
2380-
2381-
for (int ff = 0; ff < kBaseTypeCount; ++ff)
2382-
{
2383-
if(kBaseTypes[ff].tag == BaseType::Void) continue;
2384-
2385-
2386-
if( tt != ff )
2387-
{
2388-
auto cost = getBaseTypeConversionCost(
2389-
kBaseTypes[tt],
2390-
kBaseTypes[ff]);
2391-
auto op = getBaseTypeConversionOp(
2392-
kBaseTypes[tt],
2393-
kBaseTypes[ff]);
2394-
2395-
// Implicit conversion from a vector of the same
2396-
// size, but different element type.
2397-
sb << " __implicit_conversion(" << cost << ")\n";
2398-
sb << " __intrinsic_op(" << int(op) << ")\n";
2399-
sb << " __init(vector<" << kBaseTypes[ff].name << ",N> value);\n";
2400-
2401-
// Constructor to make a vector from a scalar of another type.
2402-
if (cost != kConversionCost_Impossible)
2403-
{
2404-
cost += kConversionCost_ScalarToVector;
2405-
sb << " __implicit_conversion(" << cost << ")\n";
2406-
sb << " [__unsafeForceInlineEarly]\n";
2407-
sb << " __init(" << kBaseTypes[ff].name << " value) { this = vector<" << kBaseTypes[tt].name << ",N>( " << kBaseTypes[tt].name << "(value)); }\n";
2408-
}
2409-
}
2410-
}
2411-
sb << "}\n";
2412-
}
24132372

24142373
for( int R = 1; R <= 4; ++R )
24152374
for( int C = 1; C <= 4; ++C )
@@ -2464,38 +2423,36 @@ for( int C = 1; C <= 4; ++C )
24642423
sb << "}\n";
24652424
}
24662425

2467-
for (int tt = 0; tt < kBaseTypeCount; ++tt)
2468-
{
2469-
if(kBaseTypes[tt].tag == BaseType::Void) continue;
2470-
auto toType = kBaseTypes[tt].name;
24712426
}}}}
24722427

2473-
__generic<let R : int, let C : int, let L : int> extension matrix<$(toType),R,C,L>
2428+
//@hidden:
2429+
__intrinsic_op($(kIROp_BuiltinCast))
2430+
internal T __builtin_cast<T, U>(U u);
2431+
2432+
// If T is implicitly convertible to U, then vector<T,N> is implicitly convertible to vector<U,N>.
2433+
__generic<ToType, let N : int> extension vector<ToType,N>
24742434
{
2475-
${{{{
2476-
for (int ff = 0; ff < kBaseTypeCount; ++ff)
2477-
{
2478-
if(kBaseTypes[ff].tag == BaseType::Void) continue;
2479-
if( tt == ff ) continue;
2435+
__implicit_conversion(constraint)
2436+
__intrinsic_op(BuiltinCast)
2437+
__init<FromType>(vector<FromType,N> value) where ToType(FromType) implicit;
24802438

2481-
auto cost = getBaseTypeConversionCost(
2482-
kBaseTypes[tt],
2483-
kBaseTypes[ff]);
2484-
auto fromType = kBaseTypes[ff].name;
2485-
auto op = getBaseTypeConversionOp(
2486-
kBaseTypes[tt],
2487-
kBaseTypes[ff]);
2488-
}}}}
2489-
__implicit_conversion($(cost))
2490-
__intrinsic_op($(op))
2491-
__init(matrix<$(fromType),R,C,L> value);
2492-
${{{{
2439+
__implicit_conversion(constraint+)
2440+
[__unsafeForceInlineEarly]
2441+
[__readNone]
2442+
[TreatAsDifferentiable]
2443+
__init<FromType>(FromType value) where ToType(FromType) implicit
2444+
{
2445+
this = __builtin_cast<vector<ToType,N>>(vector<FromType,N>(value));
24932446
}
2494-
}}}}
24952447
}
2496-
${{{{
2448+
2449+
// If T is implicitly convertible to U, then matrix<T,R,C,L> is implicitly convertible to matrix<U,R,C,L>.
2450+
__generic<ToType, let R : int, let C : int, let L : int> extension matrix<ToType,R,C,L>
2451+
{
2452+
__implicit_conversion(constraint)
2453+
__intrinsic_op(BuiltinCast)
2454+
__init<FromType>(matrix<FromType,R,C,L> value) where ToType(FromType) implicit;
24972455
}
2498-
}}}}
24992456

25002457
//@ hidden:
25012458
__generic<T, U>
@@ -2643,20 +2600,20 @@ for(auto fixity : kIncDecFixities)
26432600
$(fixity.qual)
26442601
__generic<T : __BuiltinArithmeticType>
26452602
[__unsafeForceInlineEarly]
2646-
T operator$(op.name)(in out T value)
2647-
{$(fixity.bodyPrefix) value = value $(op.binOp) T(1); return $(fixity.returnVal); }
2603+
T operator$(op.name)( in out T value)
2604+
{ $(fixity.bodyPrefix) value = value $(op.binOp) __builtin_cast<T>(1); return $(fixity.returnVal); }
26482605

26492606
$(fixity.qual)
26502607
__generic<T : __BuiltinArithmeticType, let N : int>
26512608
[__unsafeForceInlineEarly]
26522609
vector<T,N> operator$(op.name)(in out vector<T,N> value)
2653-
{$(fixity.bodyPrefix) value = value $(op.binOp) T(1); return $(fixity.returnVal); }
2610+
{$(fixity.bodyPrefix) value = value $(op.binOp) __builtin_cast<T>(1); return $(fixity.returnVal); }
26542611

26552612
$(fixity.qual)
26562613
__generic<T : __BuiltinArithmeticType, let R : int, let C : int, let L : int>
26572614
[__unsafeForceInlineEarly]
26582615
matrix<T,R,C> operator$(op.name)(in out matrix<T,R,C,L> value)
2659-
{$(fixity.bodyPrefix) value = value $(op.binOp) T(1); return $(fixity.returnVal); }
2616+
{$(fixity.bodyPrefix) value = value $(op.binOp) __builtin_cast<T>(1); return $(fixity.returnVal); }
26602617

26612618
$(fixity.qual)
26622619
__generic<T, let addrSpace : uint64_t>

source/slang/diff.meta.slang

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/// `[ForwardDerivative(fwdFn)]` attribute can be used to provide a forward-mode
32
/// derivative implementation.
43
/// Invoking `fwd_diff(decoratedFn)` will place a call to `fwdFn` instead of synthesizing
@@ -80,7 +79,6 @@ attribute_syntax [ForwardDerivative(function)] : ForwardDerivativeAttribute;
8079
/// For member functions, or functions nested inside namespaces, `bwdFn` may need to be a fully qualified
8180
/// name.
8281
///
83-
///
8482
__attributeTarget(FunctionDeclBase)
8583
attribute_syntax [BackwardDerivative(function)] : BackwardDerivativeAttribute;
8684

@@ -178,7 +176,7 @@ attribute_syntax [NoDiffThis] : NoDiffThisAttribute;
178176
// for internal use.
179177
//
180178
[__AutoDiffBuiltin]
181-
export struct NullDifferential : IDifferentiable
179+
struct NullDifferential : IDifferentiable
182180
{
183181
// for now, we'll use at least one field to make sure the type is non-empty
184182
uint dummy;
@@ -2076,7 +2074,7 @@ DifferentialPair<T> __d_max(DifferentialPair<T> dpx, DifferentialPair<T> dpy)
20762074
{
20772075
return DifferentialPair<T>(
20782076
max(dpx.p, dpy.p),
2079-
dpx.p > dpy.p ? dpx.d : dpy.d
2077+
dpx.p > dpy.p ? dpx.d : (dpx.p < dpy.p ? dpy.d : __mul_p_d(T(0.5), T.dadd(dpx.d, dpy.d)))
20802078
);
20812079
}
20822080

@@ -2086,8 +2084,8 @@ __generic<T : __BuiltinFloatingPointType>
20862084
[BackwardDerivativeOf(max)]
20872085
void __d_max(inout DifferentialPair<T> dpx, inout DifferentialPair<T> dpy, T.Differential dOut)
20882086
{
2089-
dpx = diffPair(dpx.p, dpx.p > dpy.p ? dOut : T.dzero());
2090-
dpy = diffPair(dpy.p, dpy.p > dpx.p ? dOut : T.dzero());
2087+
dpx = diffPair(dpx.p, dpx.p > dpy.p ? dOut : (dpx.p < dpy.p ? T.dzero() : __mul_p_d(T(0.5), dOut)));
2088+
dpy = diffPair(dpy.p, dpy.p > dpx.p ? dOut : (dpy.p < dpx.p ? T.dzero() : __mul_p_d(T(0.5), dOut)));
20912089
}
20922090

20932091
VECTOR_MATRIX_BINARY_DIFF_IMPL(max)
@@ -2101,7 +2099,7 @@ DifferentialPair<T> __d_min(DifferentialPair<T> dpx, DifferentialPair<T> dpy)
21012099
{
21022100
return DifferentialPair<T>(
21032101
min(dpx.p, dpy.p),
2104-
dpx.p < dpy.p ? dpx.d : dpy.d
2102+
dpx.p < dpy.p ? dpx.d : (dpx.p > dpy.p ? dpy.d : __mul_p_d(T(0.5), T.dadd(dpx.d, dpy.d)))
21052103
);
21062104
}
21072105

@@ -2111,8 +2109,8 @@ __generic<T : __BuiltinFloatingPointType>
21112109
[BackwardDerivativeOf(min)]
21122110
void __d_min(inout DifferentialPair<T> dpx, inout DifferentialPair<T> dpy, T.Differential dOut)
21132111
{
2114-
dpx = diffPair(dpx.p, dpx.p < dpy.p ? dOut : T.dzero());
2115-
dpy = diffPair(dpy.p, dpy.p < dpx.p ? dOut : T.dzero());
2112+
dpx = diffPair(dpx.p, dpx.p < dpy.p ? dOut : (dpx.p > dpy.p ? T.dzero() : __mul_p_d(T(0.5), dOut)));
2113+
dpy = diffPair(dpy.p, dpy.p < dpx.p ? dOut : (dpy.p > dpx.p ? T.dzero() : __mul_p_d(T(0.5), dOut)));
21162114
}
21172115

21182116
VECTOR_MATRIX_BINARY_DIFF_IMPL(min)
@@ -2150,17 +2148,19 @@ DifferentialPair<T> __d_clamp(DifferentialPair<T> dpx, DifferentialPair<T> dpMin
21502148
{
21512149
return DifferentialPair<T>(
21522150
clamp(dpx.p, dpMin.p, dpMax.p),
2153-
dpx.p < dpMin.p ? dpMin.d : (dpx.p > dpMax.p ? dpMax.d : dpx.d));
2151+
(dpx.p >= dpMin.p && dpx.p <= dpMax.p) ? dpx.d : (dpx.p < dpMin.p ? dpMin.d : dpMax.d));
21542152
}
21552153
__generic<T : __BuiltinFloatingPointType>
21562154
[BackwardDifferentiable]
21572155
[PreferRecompute]
21582156
[BackwardDerivativeOf(clamp)]
21592157
void __d_clamp(inout DifferentialPair<T> dpx, inout DifferentialPair<T> dpMin, inout DifferentialPair<T> dpMax, T.Differential dOut)
21602158
{
2161-
dpx = diffPair(dpx.p, dpx.p > dpMin.p && dpx.p < dpMax.p ? dOut : T.dzero());
2162-
dpMin = diffPair(dpMin.p, dpx.p <= dpMin.p ? dOut : T.dzero());
2163-
dpMax = diffPair(dpMax.p, dpx.p >= dpMax.p ? dOut : T.dzero());
2159+
// Propagate the derivative to x if x is within [min, max] (including the boundaries).
2160+
dpx = diffPair(dpx.p, (dpx.p >= dpMin.p && dpx.p <= dpMax.p) ? dOut : T.dzero());
2161+
// If x is strictly below min or above max, the gradient is instead applied to the clamp bounds
2162+
dpMin = diffPair(dpMin.p, dpx.p < dpMin.p ? dOut : T.dzero());
2163+
dpMax = diffPair(dpMax.p, dpx.p > dpMax.p ? dOut : T.dzero());
21642164
}
21652165
VECTOR_MATRIX_TERNARY_DIFF_IMPL(clamp)
21662166

0 commit comments

Comments
 (0)