Skip to content

Commit 51ca607

Browse files
committed
Make residency and resource bound-ness clearer
1 parent b646632 commit 51ca607

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

crates/header-translator/src/rust_type.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,11 @@ impl PointeeTy {
11871187
"may be unretained, you must ensure it is kept alive while in use",
11881188
));
11891189

1190+
// TODO: Should we also document the requirement for
1191+
// resources to be properly bound? What exactly are the
1192+
// requirements though, and when does Metal automatically
1193+
// bind resources?
1194+
11901195
// `MTLBuffer` is effectively a `Box<[u8]>` stored on the
11911196
// GPU (and depending on the storage mode, optionally also
11921197
// on the CPU). Type-safety of the contents is left

framework-crates/objc2-metal/translation-config.toml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,32 @@ class.MTLInstanceAccelerationStructureDescriptor.methods."setInstanceDescriptorB
154154
class.MTLInstanceAccelerationStructureDescriptor.methods."setInstancedAccelerationStructures:".unsafe = false
155155
class.MTLAccelerationStructureGeometryDescriptor.methods."setPrimitiveDataBuffer:".unsafe = false
156156
class.MTLAccelerationStructureTriangleGeometryDescriptor.methods."setVertexBuffer:".unsafe = false
157-
protocol.MTLRenderCommandEncoder.methods."useResource:usage:".unsafe = false
158-
protocol.MTLRenderCommandEncoder.methods."useResource:usage:stages:".unsafe = false
159-
protocol.MTLComputeCommandEncoder.methods."useResource:usage:".unsafe = false
160-
protocol.MTLBlitCommandEncoder.methods."synchronizeResource:".unsafe = false
161157
protocol.MTLBlitCommandEncoder.methods."generateMipmapsForTexture:".unsafe = false
162158
protocol.MTLBlitCommandEncoder.methods."optimizeContentsForGPUAccess:".unsafe = false
163159
protocol.MTLAccelerationStructureCommandEncoder.methods."copyAndCompactAccelerationStructure:toAccelerationStructure:".unsafe = false
164160

161+
# SAFETY: Synchronizing a resource is safe, as it:
162+
# - Doesn't need to the resource to be bound.
163+
# - Doesn't access the contents of the resource.
164+
# - Doesn't need the resource to be retained by the user, since it already is
165+
# guaranteed to be since they've used the resource elsewhere.
166+
# TODO(breaking): Is that claim true?
167+
protocol.MTLBlitCommandEncoder.methods."synchronizeResource:".unsafe = false
168+
169+
# SAFETY: We consider resource's bound-ness as safe, the safety is moved to
170+
# command encoding methods such as `setTexture:atIndex:` instead.
171+
#
172+
# Calling this with a resource is safe, since it:
173+
# - Doesn't access the contents of the resource.
174+
# - Doesn't need the resource to be retained by the user, since it already is
175+
# guaranteed to be since they've used the resource elsewhere.
176+
#
177+
# TODO(breaking): Are those claims true?
178+
protocol.MTLAccelerationStructureCommandEncoder.methods."useResource:usage:".unsafe = false
179+
protocol.MTLComputeCommandEncoder.methods."useResource:usage:".unsafe = false
180+
protocol.MTLRenderCommandEncoder.methods."useResource:usage:".unsafe = false
181+
protocol.MTLRenderCommandEncoder.methods."useResource:usage:stages:".unsafe = false
182+
165183
# SAFETY: Resource options are safe to specify:
166184
# - Hazard tracking and storage modes change the required synchronization, but
167185
# we handle that above. Also, we wouldn't really be able to prevent
@@ -182,8 +200,12 @@ protocol.MTLHeap.methods."setPurgeableState:".unsafe = false
182200
# after this is called.
183201
protocol.MTLResource.methods.makeAliasable.unsafe = true
184202

185-
# SAFETY: Modifying residency is safe, it's effectively the same as
186-
# controlling what's in the L1/L2/L3 cache on the CPU.
203+
# Modifying residency has similar safety as modifying a resource's bound-ness,
204+
# it does mostly the same thing.
205+
# protocol.MTLResidencySet.methods."addAllocation:".unsafe = false
206+
207+
# Taking a residency set in or out of residency is only a performance knob,
208+
# it's similar to controlling what's in the L1/L2/L3 cache on the CPU.
187209
# protocol.MTLResidencySet.methods.requestResidency.unsafe = false
188210
# protocol.MTLResidencySet.methods.endResidency.unsafe = false
189211

generated

0 commit comments

Comments
 (0)