Skip to content

Commit 9eafaf8

Browse files
author
Schorsch
committed
Fix wasm: replace Marshal.Copy/Set in UniformWriters with managed equivalents
Aardvark.Base's Marshal.Copy/Set extensions P/Invoke msvcrt.dll on Windows / libc on linux. Browser wasm has neither — every array uniform write threw DllNotFoundException at runtime (e.g. Arr<N<32>, V4f> for SSAO sample directions). Replace with managed Span<byte>.Clear() / System.Buffer.MemoryCopy via two private helpers in NewWriters. ArrWriter / SeqWriter / ReplicateWriter / SubTypeTestWriter / ZeroWhenNullWriter / PrimitiveArrayWriter all switched. Compiles clean against current master (no conflict with the recent 64-bit attributes & uniforms work).
1 parent abc3eef commit 9eafaf8

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

src/Aardvark.Rendering/Uniforms/UniformWriters.fs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ module UniformWriters =
296296
module NewWriters =
297297
open System.Runtime.InteropServices
298298

299+
// Managed memcpy/memset replacements. Avoid Aardvark.Base's
300+
// Marshal.Copy/Set extensions which P/Invoke msvcrt.dll / libc
301+
// (broken on wasm where neither resolves).
302+
let inline private clearBytes (dst : nativeint) (size : nativeint) =
303+
if size > 0n then
304+
let span = Span<byte>(dst.ToPointer(), int size)
305+
span.Clear()
306+
307+
let inline private copyBytes (src : nativeint) (dst : nativeint) (size : nativeint) =
308+
if size > 0n then
309+
System.Buffer.MemoryCopy(src.ToPointer(), dst.ToPointer(), int64 size, int64 size)
310+
299311
type TypeInfo<'a> private() =
300312
static let isBlittable =
301313
let arr : 'a[] = Array.zeroCreate 1
@@ -378,7 +390,7 @@ module UniformWriters =
378390

379391
let remaining = targetSize - offset
380392
if remaining > 0n then
381-
Marshal.Set(ptr + offset, 0, remaining)
393+
clearBytes (ptr + offset) remaining
382394

383395
type ArrWriter<'d, 'a when 'd :> INatural>(targetCount : int, stride : nativeint, inner : IWriter<'a>) =
384396
inherit AbstractWriter<Arr<'d, 'a>>()
@@ -401,7 +413,7 @@ module UniformWriters =
401413
offset <- offset + stride
402414

403415
if missingBytes > 0n then
404-
Marshal.Set(ptr + firstEmptyByte, 0, missingBytes)
416+
clearBytes (ptr + firstEmptyByte) missingBytes
405417

406418
type SeqWriter<'s, 'a when 's :> seq<'a>>(targetCount : int, stride : nativeint, inner : IWriter<'a>) =
407419
inherit AbstractWriter<'s>()
@@ -422,7 +434,7 @@ module UniformWriters =
422434

423435
let remaining = targetSize - offset
424436
if remaining > 0n then
425-
Marshal.Set(ptr + offset, 0, remaining)
437+
clearBytes (ptr + offset) remaining
426438

427439
type ReplicateWriter<'a>(targetCount : int, stride : nativeint, inner : IWriter<'a>) =
428440
inherit AbstractWriter<'a>()
@@ -442,7 +454,7 @@ module UniformWriters =
442454

443455
let remaining = targetSize - offset
444456
if remaining > 0n then
445-
Marshal.Set(ptr + offset, 0, remaining)
457+
clearBytes (ptr + offset) remaining
446458

447459
type SubTypeTestWriter<'a, 'b when 'a : not struct>(inner : IWriter<'b>) =
448460
inherit AbstractWriter<'a>()
@@ -452,9 +464,9 @@ module UniformWriters =
452464

453465
override x.Write(value : 'a, ptr : nativeint) =
454466
match value :> obj with
455-
| null -> Marshal.Set(ptr, 0, inner.TargetSize)
467+
| null -> clearBytes ptr inner.TargetSize
456468
| :? 'b as b -> inner.WriteValue(b, ptr)
457-
| _ -> Marshal.Set(ptr, 0, inner.TargetSize)
469+
| _ -> clearBytes ptr inner.TargetSize
458470

459471
type ZeroWhenNullWriter<'a when 'a : not struct>(inner : IWriter<'a>) =
460472
inherit AbstractWriter<'a>()
@@ -464,7 +476,7 @@ module UniformWriters =
464476

465477
override x.Write(value : 'a, ptr : nativeint) =
466478
match value :> obj with
467-
| null -> Marshal.Set(ptr, 0, inner.TargetSize)
479+
| null -> clearBytes ptr inner.TargetSize
468480
| _ -> inner.WriteValue(value, ptr)
469481

470482
type PrimitiveArrayWriter<'a when 'a : unmanaged>(count : int) =
@@ -479,8 +491,9 @@ module UniformWriters =
479491
let copySize = min targetSize inputSize
480492

481493
value |> NativePtr.pinArr (fun pSrc ->
482-
Marshal.Copy(pSrc.Address, ptr, copySize)
483-
if targetSize > copySize then Marshal.Set(ptr + copySize, 0, targetSize - copySize)
494+
copyBytes (NativePtr.toNativeInt pSrc) ptr copySize
495+
if targetSize > copySize then
496+
clearBytes (ptr + copySize) (targetSize - copySize)
484497
)
485498

486499
type PrimitiveMapWriter<'a, 'b when 'b : unmanaged>(mapping : 'a -> 'b) =

0 commit comments

Comments
 (0)