SafeReinterpret<U>()
Reinterpret the slice as having a different data type (type punning), performing checks to ensure the reinterpret is valid.
Read time 1 minuteLast updated 13 days ago
Definition
- Type: Method
- Namespace: UnityEngine.LightTransport
- Assembly: UnityEngine.CoreModule
SafeReinterpret<T>()
public BufferSlice<U> SafeReinterpret<U>() where U : struct
Returns
Type | Description |
|---|---|
| BufferSlice<T> | An alias of the same slice, but reinterpreted as the target type. |
Remarks
Safely reinterprets the BufferSlice to view the same memory as a different type. This method performs validation to ensure the type conversion is valid:
Validation Rules:
- The size of the target type must be a multiple of the size of the current type.
- The memory alignment requirements must be satisfied.
- The reinterpretation must not exceed buffer boundaries.
This is useful for viewing the same data through different type lenses, such as treating a float buffer as a Vector3 buffer.
Additional Resources: BufferSlice<T>.UnsafeReinterpret for unchecked reinterpretation.
Examples
// Create a buffer of floatsBufferID buffer = context.CreateBuffer(300, sizeof(float)); // 300 floatsvar floatSlice = new BufferSlice<float>(buffer, 0);// Safely reinterpret as Vector3 (3 floats = 1 Vector3)var vectorSlice = floatSlice.SafeReinterpret<Vector3>(); // Now views 100 Vector3s// Write Vector3 data through the reinterpreted slicevar vectorData = new NativeArray<Vector3>(100, Allocator.Temp);for (int i = 0; i < 100; i++) vectorData[i] = new Vector3(i, i + 0.5f, i + 1.0f);context.WriteBuffer(vectorSlice, vectorData);// The same memory can still be read as floatsvar floatData = new NativeArray<float>(300, Allocator.Temp);context.ReadBuffer(floatSlice, floatData);