11implementing neural ;
22
3+ /**
4+ Bindless address type with pointer-like semantics.
5+ Wraps a buffer handle and base index to provide array-like access.
6+ */
7+ public struct BindlessAddress <T > : IPointerLikeAddress <T>
8+ where T : __BuiltinFloatingPointType
9+ where T.Differential == T
10+ {
11+ public typealias Differential = BindlessAddress< T. Differential> ;
12+
13+ internal RWStructuredBuffer < T> . Handle handle ;
14+ internal uint baseIndex ;
15+
16+ public __init (RWStructuredBuffer < T> . Handle handle )
17+ {
18+ this . handle = handle;
19+ this . baseIndex = 0 ;
20+ }
21+
22+ public __subscript (uint index )-> T
23+ {
24+ [nonmutating ]
25+ get { return handle[baseIndex + index]; }
26+
27+ [mutating ]
28+ set { handle[baseIndex + index] = newValue; }
29+ }
30+
31+ [require (cuda_glsl_hlsl_metal_spirv , sm_6_6 )]
32+ public void atomicAdd (uint index , T value )
33+ {
34+ __atomic_add (handle[baseIndex + index], value);
35+ }
36+
37+ public This getOffset (int elements )
38+ {
39+ uint newBaseIndex = baseIndex + elements;
40+
41+ This address = This (handle);
42+ address. baseIndex = newBaseIndex;
43+ return address;
44+ }
45+ }
46+
47+ public struct PointerAddress <T > : IPointerLikeAddress <T>
48+ where T : __BuiltinFloatingPointType
49+ where T.Differential == T
50+ {
51+ public typealias Differential = PointerAddress< T. Differential> ;
52+
53+ T * ptr ;
54+
55+ public __init (T * ptr )
56+ {
57+ this . ptr = ptr;
58+ }
59+
60+ public __subscript (uint index )-> T
61+ {
62+ [nonmutating ]
63+ get { return ptr[index]; }
64+
65+ [mutating ]
66+ set { ptr[index] = newValue; }
67+ }
68+
69+ public This getOffset (int elements )
70+ {
71+ return This (ptr + elements);
72+ }
73+
74+ [require (cuda_glsl_hlsl_metal_spirv , sm_6_6 )]
75+ public void atomicAdd (uint index , T value )
76+ {
77+ __atomic_add (ptr[index], value);
78+ }
79+ }
80+
81+ // We currently don't support UserPointer as an `IDifferentiablePtrType`, the issue is tracked in
82+ // https://github.com/shader-slang/slang/issues/8834.
83+ // So we define an internal extension for now, once we can resolve the issue, we can make it public.
84+ internal extension<T> Ptr<T, Access.ReadWrite, AddressSpace.Device> : IPointerLikeAddress< T>
85+ where T : __BuiltinFloatingPointType
86+ where T.Differential == T
87+ {
88+ internal typealias Differential = Ptr < T. Differential, Access. ReadWrite, AddressSpace. Device> ;
89+
90+ internal __init (Ptr <T, Access.ReadWrite, AddressSpace.Device> ptr )
91+ {
92+ this = ptr;
93+ }
94+
95+ internal __subscript (uint index )-> T
96+ {
97+ [nonmutating ]
98+ get { return this [index]; }
99+
100+ [mutating ]
101+ set { this [index] = newValue; }
102+ }
103+
104+ internal This getOffset (int elements )
105+ {
106+ return This (this + elements);
107+ }
108+
109+ [require (cuda_glsl_hlsl_metal_spirv , sm_6_6 )]
110+ internal void atomicAdd (uint index , T value ) {__atomic_add (this [index], value);}
111+ }
112+
3113/**
4114Bindless buffer storage implementation using buffer handles.
5115Provides pointer-like addressing through buffer handles, enabling more flexible
@@ -14,49 +124,7 @@ public struct BindlessBufferStorage<T> : IStorage<T>
14124 where T : __BuiltinFloatingPointType
15125 where T.Differential == T
16126{
17- /**
18- Bindless address type with pointer-like semantics.
19- Wraps a buffer handle and base index to provide array-like access.
20- */
21- public struct BindlessAddress : IPointerLikeAddress <T>
22- {
23- public typealias Differential =
24- BindlessBufferStorage< T. Differential> . BindlessAddress;
25-
26- internal RWStructuredBuffer < T> . Handle handle ;
27- internal uint baseIndex ;
28-
29- public __init (RWStructuredBuffer < T> . Handle handle )
30- {
31- this . handle = handle;
32- this . baseIndex = 0 ;
33- }
34-
35- public __subscript (uint index )-> T
36- {
37- [nonmutating ]
38- get { return handle[baseIndex + index]; }
39-
40- [mutating ]
41- set { handle[baseIndex + index] = newValue; }
42- }
43-
44- [require (cuda_glsl_hlsl_metal_spirv , sm_6_6 )]
45- public void atomicAdd (uint index , T value )
46- {
47- __atomic_add (handle[baseIndex + index], value);
48- }
49-
50- public Address getOffset (int elements )
51- {
52- uint newBaseIndex = baseIndex + elements;
53-
54- Address address = Address (handle);
55- address. baseIndex = newBaseIndex;
56- return address;
57- }
58- }
59- public typealias Address = BindlessAddress;
127+ public typealias Address = BindlessAddress< T> ;
60128 public typealias Differential = BindlessBufferStorage< T. Differential> ;
61129
62130 // Following method will not be needed for bindless storage
@@ -66,45 +134,11 @@ public struct BindlessBufferStorage<T> : IStorage<T>
66134 public static Address getOffset (Address base , int elements ) { return base. getOffset (elements); }
67135}
68136
69- // [require(cpp_cuda_metal_spirv)]
70137public struct PointerStorage <T > : IStorage <T>
71138 where T : __BuiltinFloatingPointType
72139 where T.Differential == T
73140{
74- public struct PointerAddress : IPointerLikeAddress <T>
75- {
76- public typealias Differential =
77- PointerStorage< T. Differential> . PointerAddress;
78-
79- T * ptr ;
80-
81- public __init (T * ptr )
82- {
83- this . ptr = ptr;
84- }
85-
86- public __subscript (uint index )-> T
87- {
88- [nonmutating ]
89- get { return ptr[index]; }
90-
91- [mutating ]
92- set { ptr[index] = newValue; }
93- }
94-
95- public Address getOffset (int elements )
96- {
97- return Address (ptr + elements);
98- }
99-
100- [require (cuda_glsl_hlsl_metal_spirv , sm_6_6 )]
101- public void atomicAdd (uint index , T value )
102- {
103- __atomic_add (ptr[index], value);
104- }
105- }
106-
107- public typealias Address = PointerAddress;
141+ public typealias Address = PointerAddress< T> ;
108142 public typealias Differential = PointerStorage< T. Differential> ;
109143
110144 // Following method will not be needed for pointer storage
0 commit comments