Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Nov 28, 2022
2 parents 72aadab + 8c96c8c commit 7e8538b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 33 deletions.
14 changes: 8 additions & 6 deletions src/Aardvark.Rendering.Vulkan/Resources/ShaderProgram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,14 @@ module ShaderProgram =
if File.Exists file then
Report.Begin(4, "[Vulkan] loading shader {0}", Path.GetFileName file)
try
let data = File.ReadAllBytes file
Some <| ofByteArray data device
with exn ->
Report.End(4) |> ignore
Log.warn "[Vulkan] Failed to read from shader program file cache '%s': %s" file exn.Message
None
try
let data = File.ReadAllBytes file
Some <| ofByteArray data device
with exn ->
Log.warn "[Vulkan] Failed to read from shader program file cache '%s': %s" file exn.Message
None
finally
Report.End(4) |> ignore
else
None

Expand Down
146 changes: 119 additions & 27 deletions src/Aardvark.Rendering/Geometry/IndexedGeometry.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ open Aardvark.Base

module private IndexHelpers =

let applyIndex (index : Array) (data : Array) =

let addOffset (offset : int) (index : Array) : Array =
if isNull index then
null
else
match index with
| :? array<int32> as x -> x |> Array.map ((+) offset) :> Array
| :? array<int16> as x -> x |> Array.map ((+) (int16 offset)) :> Array
| :? array<uint32> as x -> x |> Array.map ((+) (uint32 offset)) :> Array
| :? array<uint16> as x -> x |> Array.map ((+) (uint16 offset)) :> Array
| _ ->
raise <| ArgumentException($"Unsupported type {index.GetType()}.")

let applyIndex (index : Array) (data : Array) =
if isNull index || isNull data then
null
else
Expand Down Expand Up @@ -71,7 +82,28 @@ module private IndexHelpers =
oi <- oi + 3

res :> Array
}
}

module private ArrayHelpers =

let concat (a : Array) (b : Array) =
if isNull a then b
elif isNull b then a
else
a.Visit (
{ new ArrayVisitor<Array>() with
member x.Run(a : 'T[]) =
b.Visit (
{ new ArrayVisitor<Array>() with
member x.Run(b : 'U[]) =
if typeof<'T> <> typeof<'U> then
raise <| ArgumentException($"Array element types must match to be concatenated (got {typeof<'T>} and {typeof<'U>}).")
else
Array.concat [a; unbox<'T[]> b]
}
)
}
)


type IndexedGeometryMode =
Expand All @@ -84,8 +116,6 @@ type IndexedGeometryMode =
| LineAdjacencyList = 6
| QuadList = 7



[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module IndexedGeometryMode =
let faceCount (m : IndexedGeometryMode) (fvc : int) =
Expand Down Expand Up @@ -129,14 +159,16 @@ type IndexedGeometry =

member x.IsIndexed = not (isNull x.IndexArray)

member x.VertexCount =
if isNull x.IndexedAttributes then 0
else
match Seq.tryHead x.IndexedAttributes.Values with
| Some att -> att.Length
| _ -> 0

member x.FaceVertexCount =
if isNull x.IndexArray then
if isNull x.IndexedAttributes then
0
else
match Seq.tryHead x.IndexedAttributes.Values with
| Some att -> att.Length
| None -> 0
x.VertexCount
else
x.IndexArray.Length

Expand Down Expand Up @@ -180,22 +212,80 @@ type IndexedGeometry =

member x.ToNonStripped() =
match x.Mode with
| IndexedGeometryMode.LineStrip ->
let x = x.ToIndexed()
x.IndexArray <- IndexHelpers.lineStripToList x.IndexArray
x.Mode <- IndexedGeometryMode.LineList
x

| IndexedGeometryMode.TriangleStrip ->
let x = x.ToIndexed()
x.IndexArray <- IndexHelpers.triangleStripToList x.IndexArray
x.Mode <- IndexedGeometryMode.TriangleList
x

| _ ->
x

| IndexedGeometryMode.LineStrip ->
let x = x.ToIndexed()
x.IndexArray <- IndexHelpers.lineStripToList x.IndexArray
x.Mode <- IndexedGeometryMode.LineList
x

| IndexedGeometryMode.TriangleStrip ->
let x = x.ToIndexed()
x.IndexArray <- IndexHelpers.triangleStripToList x.IndexArray
x.Mode <- IndexedGeometryMode.TriangleList
x

| _ ->
x

member x.Union(y : IndexedGeometry) =
if x.Mode <> y.Mode then
raise <| ArgumentException("IndexedGeometryMode must match.")

let acceptedTopologies = [
IndexedGeometryMode.PointList
IndexedGeometryMode.LineList
IndexedGeometryMode.TriangleList
IndexedGeometryMode.QuadList
]

if not <| List.contains x.Mode acceptedTopologies then
raise <| ArgumentException($"IndexedGeometryMode must be one of {acceptedTopologies}.")

if x.IsIndexed <> y.IsIndexed then
let a = if x.IsIndexed then x else y.ToIndexed()
let b = if y.IsIndexed then y else x.ToIndexed()
a.Union b

else
let indices =
try
y.IndexArray
|> IndexHelpers.addOffset x.VertexCount
|> ArrayHelpers.concat x.IndexArray
with
| exn ->
raise <| ArgumentException($"Invalid indices: {exn.Message}")

let singleAttributes =
if isNull x.SingleAttributes then y.SingleAttributes
elif isNull y.SingleAttributes then x.SingleAttributes
else SymDict.union [x.SingleAttributes; y.SingleAttributes]

let indexedAttributes =
if isNull x.IndexedAttributes then y.IndexedAttributes
elif isNull y.IndexedAttributes then x.IndexedAttributes
else
let r = SymDict.empty

for KeyValue(sem, a) in x.IndexedAttributes do
match y.IndexedAttributes |> SymDict.tryFind sem with
| Some b ->
try
r.[sem] <- ArrayHelpers.concat a b
with
| exn ->
raise <| ArgumentException($"Invalid {sem} attributes: {exn.Message}")
| _ -> ()

r

IndexedGeometry (
Mode = x.Mode,
IndexArray = indices,
SingleAttributes = singleAttributes,
IndexedAttributes = indexedAttributes
)

new() =
{ Mode = IndexedGeometryMode.TriangleList;
IndexArray = null;
Expand Down Expand Up @@ -225,9 +315,11 @@ module IndexedGeometry =
let inline singleAttributes (g : IndexedGeometry) = g.SingleAttributes

let inline isIndexed (g : IndexedGeometry) = g.IsIndexed
let inline vertexCount (g : IndexedGeometry) = g.VertexCount
let inline faceVertexCount (g : IndexedGeometry) = g.FaceVertexCount
let inline faceCount (g : IndexedGeometry) = g.FaceCount
let inline clone (g : IndexedGeometry) = g.Clone()
let inline toIndexed (g : IndexedGeometry) = g.ToIndexed()
let inline toNonIndexed (g : IndexedGeometry) = g.ToNonIndexed()
let inline toNonStripped (g : IndexedGeometry) = g.ToNonStripped()
let inline toNonStripped (g : IndexedGeometry) = g.ToNonStripped()
let inline union (a : IndexedGeometry) (b : IndexedGeometry) = a.Union b
16 changes: 16 additions & 0 deletions src/Aardvark.SceneGraph/IndexedGeometryPrimitives.fs
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,19 @@ module IndexedGeometryPrimitives =
let solidTorus = solidTorus
let wireframeTorus = wireframeTorus

/// Creates an arrow consisting of a cylinder for the shaft and a cone for the head.
let arrow (position : V3d) (axis : V3d)
(shaftLength : float) (shaftRadius : float) (shaftColor : C4b)
(headLength : float) (headRadius : float) (headColor : C4b)
(tessellation : int) =
let shaft = solidCylinder position axis shaftLength shaftRadius shaftRadius tessellation shaftColor
let head = solidCone (position + axis * shaftLength) axis headLength (shaftRadius + headRadius) tessellation headColor
IndexedGeometry.union head shaft

/// Creates a coordinate cross consisting of three arrows for the axes and a sphere for the origin.
let coordinateCross3d (colorX : C4b) (colorY : C4b) (colorZ : C4b) (colorOrigin : C4b) (tessellation : int) =
let xAxis = arrow V3d.Zero V3d.XAxis 0.75 0.05 colorX 0.25 0.045 colorX tessellation
let yAxis = arrow V3d.Zero V3d.YAxis 0.75 0.05 colorY 0.25 0.045 colorY tessellation
let zAxis = arrow V3d.Zero V3d.ZAxis 0.75 0.05 colorZ 0.25 0.045 colorZ tessellation
let center = solidPhiThetaSphere (Sphere3d(V3d.Zero, 0.085)) tessellation colorOrigin
[center; xAxis; yAxis; zAxis] |> List.reduce IndexedGeometry.union

0 comments on commit 7e8538b

Please sign in to comment.