Skip to content

Commit

Permalink
shallow clone dawn and only build webgpu_dawn
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed Dec 7, 2024
1 parent 85d73b3 commit 8a28f59
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 30 deletions.
3 changes: 2 additions & 1 deletion GPU.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
dotnet-tools.json = .config/dotnet-tools.json
buildnative.sh = buildnative.sh
pulldawn.sh = pulldawn.sh
pulldawn.sh = pulldawn.sh
native.yml = .github\workflows\native.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebGPUNative", "WebGPUNative", "{5A7FC622-B393-4DFD-8789-503B04F22AFB}"
Expand Down
9 changes: 7 additions & 2 deletions pulldawn.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ rd /s /q tmp
mkdir tmp
pushd tmp

git clone https://github.com/google/dawn.git
mkdir dawn
REM git clone https://github.com/google/dawn.git
pushd dawn
git init
git remote add origin https://github.com/google/dawn.git
git fetch --depth 1 origin 2d08f945c77094a754bed83d2821cd60dbf81c6c
git reset --hard FETCH_HEAD

git checkout 2d08f945c77094a754bed83d2821cd60dbf81c6c
REM git checkout 2d08f945c77094a754bed83d2821cd60dbf81c6c

python tools\fetch_dawn_dependencies.py --use-test-deps

Expand Down
12 changes: 9 additions & 3 deletions pulldawn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ rm -dfr tmp
mkdir -p tmp
cd tmp

git clone https://github.com/google/dawn.git
# git clone https://github.com/google/dawn.git
mkdir dawn
cd dawn
git checkout 2d08f945c77094a754bed83d2821cd60dbf81c6c
git init
git remote add origin https://github.com/google/dawn.git
git fetch --depth 1 origin 2d08f945c77094a754bed83d2821cd60dbf81c6c
git reset --hard FETCH_HEAD

# git checkout 2d08f945c77094a754bed83d2821cd60dbf81c6c

python tools/fetch_dawn_dependencies.py --use-test-deps

mkdir -p out/Release
cd out/Release

cmake -S ../.. -B . -DCMAKE_BUILD_TYPE=Release $ARCH_FLAGS -DCMAKE_INSTALL_PREFIX=./blabber
make -j
make -j webgpu_dawn

if [ "$OS" = "Darwin" ]; then
mkdir -p ../../../../../../libs/Native/WebGPU/mac/$ARCH_NAME/
Expand Down
34 changes: 21 additions & 13 deletions src/Aardvark.Rendering.WebGPU/ResourceManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,18 @@ type ResourceManager(device : Device) =
member private x.CreateSingleValueBuffer(usage : BufferUsage, value : IAdaptiveValue) =
bufferCache.GetOrCreate((usage, value), fun (usage, adaptiveValue) ->
let usage = usage ||| BufferUsage.CopyDst ||| BufferUsage.CopySrc

let inline uploadSingleValue (cmd : CommandEncoder) (res : Buffer) (value : obj)=
match value with
| :? V4f as value -> cmd.Upload([|value|], res)
| :? C4f as value -> cmd.Upload([|value|], res)
| :? V4i as value -> cmd.Upload([|value|], res)
| :? C4b as value -> cmd.Upload([|value|], res)
| _ -> failwith $"bad singlevalue: {value.GetType().FullName}"

{ new AdaptiveResource<Buffer>() with
override x.Create(cmd, token) =
let value = adaptiveValue.GetValueUntyped token

let res =
device.CreateBuffer {
Label = null
Expand All @@ -200,23 +208,15 @@ type ResourceManager(device : Device) =
MappedAtCreation = false
}

match value with
| :? V4f as value -> cmd.Upload([|value|], res)
| :? C4f as value -> cmd.Upload([|value|], res)
| :? V4i as value -> cmd.Upload([|value|], res)
| :? C4b as value -> cmd.Upload([|value|], res)
| _ -> failwith $"bad singlevalue: {value.GetType().FullName}"
uploadSingleValue cmd res value
res

override x.Destroy b =
b.Dispose()

override x.TryUpdate(buffer, cmd, token) =
let value = adaptiveValue.GetValueUntyped token
match value with
| :? V4f as value -> cmd.Upload([|value|], buffer)
| :? V4i as value -> cmd.Upload([|value|], buffer)
| _ -> failwith $"bad singlevalue: {value}"
uploadSingleValue cmd buffer value
true

}
Expand Down Expand Up @@ -273,7 +273,11 @@ type ResourceManager(device : Device) =
else
false
else
false
match value.GetValueUntyped token with
| :? Buffer as b ->
b = buffer
| _ ->
false
}
)

Expand Down Expand Up @@ -306,7 +310,11 @@ type ResourceManager(device : Device) =
| _ ->
failwith ""
member x.TryUpdate(handle, cmd, token) =
false
match value.GetValueUntyped token with
| :? Texture as t ->
t = handle
| _ ->
false
member x.Destroy(handle) =
handle.Dispose()
}
Expand Down
82 changes: 71 additions & 11 deletions src/WebGPU/TextureExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,25 @@ module TextureFormatExtensions =
| TextureFormat.Stencil8 -> v.Accept<uint8>(Col.Format.Gray)
| fmt -> failwithf "bad visitor format: %A" fmt


type NativeImage =
{
Format : PixFormat
Size : V3i
BytesPerRow : nativeint
BytesPerImage : nativeint
Data : nativeint
}


[<AbstractClass; Sealed>]
type ImageExtensions private() =
static member internal CopyImageToTexture(this : CommandEncoder, data : nativeint, tex : Texture, volumeInfo : Type -> int -> Col.Format -> list<Range1i * Choice<VolumeInfo, obj>>) =
static member internal CopyImageToTexture(this : CommandEncoder, data : nativeint, tex : Texture, dstLevel : int, dstOffset : V3i, size : V3i, volumeInfo : Type -> int -> Col.Format -> list<Range1i * Choice<Tensor4Info, obj>>) =
tex.Format.Visit {
new ITextureFormatVisitor<_> with
member x.Accept<'a when 'a : unmanaged>(fmt : Col.Format) =
let device = this.Device
let size = V2i(tex.Width, tex.Height)
//let size = V3i(tex.Width, tex.Height, tex.DepthOrArrayLayers)
let elementSize = sizeof<'a>
let channels = fmt.ChannelCount()
let infos = volumeInfo typeof<'a> elementSize fmt
Expand All @@ -92,16 +102,27 @@ type ImageExtensions private() =


let fakeWidth = fakebpr / pixelSize
let dstVolume = NativeVolume<'a>(NativePtr.ofNativeInt tmpPtr, VolumeInfo(0L, V3l(fakeWidth, size.Y, channels), V3l(channels, int64 channels * int64 fakeWidth, 1L)))

let dy = int64 channels * int64 fakeWidth
let dz = dy * int64 size.Y
let dstTensor =
NativeTensor4<'a>(
NativePtr.ofNativeInt tmpPtr,
Tensor4Info(
0L,
V4l(fakeWidth, size.Y, size.Z, channels),
V4l(channels, dy, dz, 1L)
)
)

for channelRange, info in infos do
let dstPart = dstVolume.SubVolume(V3l(0, 0, channelRange.Min), V3l(size.X, size.Y, 1 + channelRange.Max - channelRange.Min))
let dstPart = dstTensor.SubTensor4(V4l(0, 0, 0, channelRange.Min), V4l(size.X, size.Y, size.Z, 1 + channelRange.Max - channelRange.Min))
match info with
| Choice1Of2 info ->
let src = NativeVolume<'a>(NativePtr.ofNativeInt data, info)
NativeVolume.copy src dstPart
let src = NativeTensor4<'a>(NativePtr.ofNativeInt data, info)
NativeTensor4.copy src dstPart
| Choice2Of2 value ->
NativeVolume.set (value :?> 'a) dstPart
NativeTensor4.set (value :?> 'a) dstPart

tmp.Unmap()

Expand All @@ -119,24 +140,44 @@ type ImageExtensions private() =
let dst : ImageCopyTexture =
{
Texture = tex
Origin = { X = 0; Y = 0; Z = 0 }
Origin = { X = dstOffset.X; Y = dstOffset.Y; Z = dstOffset.Z }
Aspect = TextureAspect.All
MipLevel = 0
MipLevel = dstLevel
}

this.CopyBufferToTexture(src, dst, { Width = size.X; Height = size.Y; DepthOrArrayLayers = 1 })

1
} |> ignore

static member internal CopyImageToTexture2d(this : CommandEncoder, data : nativeint, tex : Texture, dstLevel : int, dstOffset : V2i, size : V2i, volumeInfo : Type -> int -> Col.Format -> list<Range1i * Choice<VolumeInfo, obj>>) =
ImageExtensions.CopyImageToTexture(this, data, tex, dstLevel, dstOffset.XYO, size.XYI, fun elemType elemSize fmt ->
volumeInfo elemType elemSize fmt |> List.map (fun (r, i) ->
match i with
| Choice1Of2 vi ->
let ti =
Tensor4Info(
vi.Origin,
V4l(vi.SX, vi.SY, 1L, vi.SZ),
V4l(vi.DX, vi.DY, vi.DY * vi.SY, vi.DZ)
)

r, Choice1Of2 ti
| Choice2Of2 v ->
r, Choice2Of2 v
)



)
[<Extension>]
static member CopyImageToTexture(this : CommandEncoder, data : PixImage, tex : Texture) =
static member CopyImageToTexture(this : CommandEncoder, data : PixImage, tex : Texture, dstLevel : int) =
data.Visit {
new IPixImageVisitor<_> with
member x.Visit (image: PixImage<'i>) =
let gc = GCHandle.Alloc(image.Volume.Data, GCHandleType.Pinned)
let ptr = gc.AddrOfPinnedObject()
ImageExtensions.CopyImageToTexture(this, ptr, tex, fun typ _ c ->
ImageExtensions.CopyImageToTexture2d(this, ptr, tex, dstLevel, V2i.Zero, data.Size, fun typ _ c ->
if typ <> typeof<'i> then failwithf $"bad channel-type {typeof<'i>} (expected {typ})"

if c = image.Format then
Expand Down Expand Up @@ -174,3 +215,22 @@ type ImageExtensions private() =
} |> ignore


[<Extension>]
static member CopyImageToTexture(this : CommandEncoder, data : NativeImage, tex : Texture, dstLevel : int, dstOffset : V3i) =
ImageExtensions.CopyImageToTexture(this, data.Data, tex, dstLevel, dstOffset, data.Size, fun elementType elementSize dstFormat ->
if data.Format.Type.GetCLRSize() <> elementSize then
failwith $"bad element-size {elementSize} (expected {data.Format.Type.GetCLRSize()})"

if data.Format.Format = dstFormat then
let channels = data.Format.ChannelCount
let info =
Tensor4Info(
0L,
V4l(data.Size.X, data.Size.Y, data.Size.Z, channels),
V4l(int64 channels, int64 data.BytesPerRow / int64 elementSize, int64 data.BytesPerImage / int64 elementSize, 1L)
)

[Range1i(0, dstFormat.ChannelCount() - 1), Choice1Of2 info]
else
failwith $"bad format {data.Format.Format} (expected {dstFormat})"
)

0 comments on commit 8a28f59

Please sign in to comment.