Skip to content

Commit

Permalink
fixed glTexImage3d Call
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed Sep 27, 2023
1 parent 0d33972 commit 9f2b9f7
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 53 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.1.5
* fixed glTexImage3d Call

### 1.1.4
* flag for controlling GC.Collect() before each worker message

Expand Down
118 changes: 84 additions & 34 deletions src/Aardworx.Rendering.WebGL/Application.fs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,82 @@ type private WebGLSwapChainSimple internal(device : Device, main : HTMLCanvasEle
)

override x.Release() = ()

type private WebGLSwapChainMSAA internal(device : Device, main : HTMLCanvasElement, dst : HTMLCanvasElement, blit : JsObj, samples : int) =
inherit WebGLSwapChain(device, main, dst, blit)

let mutable fbo : option<Renderbuffer * Renderbuffer * Framebuffer * Texture * Framebuffer> = None



override x.RenderFrame(action : Framebuffer -> Silk.NET.OpenGLES.GL -> unit) =
let htmlSize =
let r = dst.GetBoundingClientRect()
r.Size

let renderSize =
V2i(round htmlSize)

dst.Width <- renderSize.X
dst.Height <- renderSize.Y
main.Width <- renderSize.X
main.Height <- renderSize.Y

let fbo, fboRes =
match fbo with
| Some (_,_,fbo, _, fboRes) when fbo.Size = renderSize -> fbo, fboRes
| _ ->
match fbo with
| Some (c,d,f,c1,f1) ->
c.Dispose()
d.Dispose()
f.Dispose()
c1.Dispose()
f1.Dispose()
| None ->
()

let s = device.GetDefaultFramebufferSignature()

let c = device.CreateRenderbuffer(TextureFormat.Rgba8, renderSize, samples)
let d = device.CreateRenderbuffer(TextureFormat.Depth24Stencil8, renderSize, samples)
let f = device.CreateFramebuffer(s, [DefaultSemantic.Colors, c :> IFramebufferOutput; DefaultSemantic.DepthStencil, d :> IFramebufferOutput])


let c1 = device.CreateTexture2D(TextureFormat.Rgba8, renderSize)
let f1 = device.CreateFramebuffer(s, [DefaultSemantic.Colors, c1.[0,0] :> IFramebufferOutput])


fbo <- Some (c, d, f, c1, f1)
f, f1
device.Run(fun gl ->
action fbo gl

gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fbo.Handle)
gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fboRes.Handle)
gl.ReadBuffer ReadBufferMode.ColorAttachment0
gl.DrawBuffers(1u, [| DrawBufferMode.ColorAttachment0 |])
printfn "asdasd"
WrappedCommands.glBlitFramebuffer(
0, 0, renderSize.X, renderSize.Y,
0, 0, renderSize.X, renderSize.Y,
ClearBufferMask.ColorBufferBit,
BlitFramebufferFilter.Nearest
)

gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fboRes.Handle)
gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0u)

gl.ReadBuffer ReadBufferMode.ColorAttachment0
gl.DrawBuffers(1u, [| DrawBufferMode.Back |])
WrappedCommands.glBlitFramebuffer(0, 0, renderSize.X, renderSize.Y, 0, 0, renderSize.X, renderSize.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest)
gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, 0u)
gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0u)

blit.Invoke("blit", [|main :> obj|])
)

override x.Release() = ()

type private AdaptiveRenderCaller() =
inherit AdaptiveObject()
Expand All @@ -188,22 +264,16 @@ type Antialiasing =
| FXAA


type WebGLRenderControl internal(runtime : Runtime, swapChain : WebGLSwapChain, element : HTMLCanvasElement, antialiasing : Antialiasing) =
type WebGLRenderControl internal(runtime : Runtime, swapChain : WebGLSwapChain, element : HTMLCanvasElement) =
let rt = runtime :> IRuntime
let device = runtime.Device

let samples =
match antialiasing with
| Antialiasing.None -> 1
| Antialiasing.MSAA s -> s
| Antialiasing.FXAA -> 1

let signature =
new FramebufferSignature(
runtime.Device,
Map.ofList [0, { Name = DefaultSemantic.Colors; Format = TextureFormat.Rgba8 }],
Some TextureFormat.Depth24Stencil8,
1, samples
1, 1
)

let clearColor = cval (C4f(0.0f, 0.0f, 0.0f, 0.0f))
Expand Down Expand Up @@ -242,34 +312,13 @@ type WebGLRenderControl internal(runtime : Runtime, swapChain : WebGLSwapChain,
if size <> sizes.Value then
transact (fun () -> sizes.Value <- size)
beforeRender.Trigger()
let framebuffer =
if samples = 1 then
fbo
else
match selfFbo with
| Some (f,_,_) when f.Size = size -> f
| _ ->
match selfFbo with
| Some (f, c, d) ->
f.Dispose()
c.Dispose()
d.Dispose()
| None ->
()

let c = device.CreateRenderbuffer(TextureFormat.Rgba8, size, samples)
let d = device.CreateRenderbuffer(TextureFormat.Depth24Stencil8, size, samples)
let f = device.CreateFramebuffer(signature, [DefaultSemantic.Colors, c :> IFramebufferOutput; DefaultSemantic.DepthStencil, d :> IFramebufferOutput])
selfFbo <- Some (f, c, d)
f

let framebuffer = fbo

caller.EvaluateAlways AdaptiveToken.Top (fun token ->
clearTask.Run(token, RenderToken.Empty, framebuffer)
renderTask.Run(token, RenderToken.Empty, framebuffer)
)

if samples > 1 then
rt.Copy(framebuffer, fbo)
//gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, framebuffer.Handle)
//gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0u)
//gl.ReadBuffer(ReadBufferMode.ColorAttachment0)
Expand Down Expand Up @@ -377,7 +426,7 @@ type WebGLRenderControl internal(runtime : Runtime, swapChain : WebGLSwapChain,
renderTask <- t
invalidate()

member x.Samples = samples
member x.Samples = 1
member x.SubSampling
with get() = 1.0
and set _ = ()
Expand Down Expand Up @@ -491,8 +540,9 @@ type WebGLApplication(commandStreamMode : CommandStreamMode, debug : bool) =
let swap =
match antialiasing with
| Antialiasing.FXAA -> new WebGLSwapChainFXAA(device, canvas, dst, blit) :> WebGLSwapChain
| Antialiasing.MSAA _ | Antialiasing.None -> new WebGLSwapChainSimple(device, canvas, dst, blit) :> WebGLSwapChain
new WebGLRenderControl(runtime, swap, dst, antialiasing)
| Antialiasing.MSAA s -> new WebGLSwapChainMSAA(device, canvas, dst, blit, s) :> WebGLSwapChain
| Antialiasing.None -> new WebGLSwapChainSimple(device, canvas, dst, blit) :> WebGLSwapChain
new WebGLRenderControl(runtime, swap, dst)

member x.Dispose() =
canvas.Remove()
Expand Down
4 changes: 4 additions & 0 deletions src/Aardworx.Rendering.WebGL/Resources/Framebuffer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ type DeviceFramebufferExtensions private() =
1,1
)

[<Extension>]
static member GetDefaultFramebufferSignature(this : Device) =
defaultSignature

[<Extension>]
static member DefaultFramebuffer(this : Device, size : V2i) =
new Framebuffer(
Expand Down
3 changes: 2 additions & 1 deletion src/Aardworx.Rendering.WebGL/Resources/Texture.fs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ type DeviceTextureExtensions private() =
| SizedInternalFormat.Rgb32f -> PixelFormat.Rgb, PixelType.Float
| SizedInternalFormat.Rgba32f -> PixelFormat.Rgba, PixelType.Float
| fmt -> failwithf "bad volume format: %A" fmt
gl.TexImage3D(target, 0, int ifmt, uint32 size.X, uint32 size.Y, uint32 size.Z, 0, fmt, typ, VoidPtr.zero)
WrappedCommands.glTexImage3D(target, 0, unbox (int ifmt), uint32 size.X, uint32 size.Y, uint32 size.Z, 0, fmt, typ, 0n)
//gl.TexImage3D(target, 0, int ifmt, uint32 size.X, uint32 size.Y, uint32 size.Z, 0, fmt, typ, VoidPtr.zero)

//gl.TexStorage3D(target, uint32 levels, ifmt, uint32 size.X, uint32 size.Y, uint32 size.Z)

Expand Down
96 changes: 90 additions & 6 deletions src/Examples/RenderingOnly/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,87 @@ type IntWorker() =
ctx.Send(WorkerMessage.Binary arr)
}

module Sg =

module Shader =
open FShade

let color =
sampler2d {
texture uniform?Color
filter Filter.MinMagPoint
addressU WrapMode.Wrap
addressV WrapMode.Wrap
}

let depth =
sampler2d {
texture uniform?Depth
filter Filter.MinMagPoint
addressU WrapMode.Wrap
addressV WrapMode.Wrap
}

type UniformScope with
member x.Factor : int = uniform?Factor


type Fragment =
{
[<Color>] c : V4d
[<Depth>] d : float
}
let blit (v : Effects.Vertex) =
fragment {
let mutable sum = V4d.Zero

let px = uniform.Factor * V2i (v.tc * V2d uniform.ViewportSize)

for x in 0 .. uniform.Factor - 1 do
for y in 0 .. uniform.Factor - 1 do
sum <- sum + color.[px + V2i(x,y)]

let avg = sum / float (uniform.Factor * uniform.Factor)

let d = depth.SampleLevel(v.tc, 0.0).X
if d >= 1.0 || avg.W <= 0.0 then discard()


return { c = avg; d = d }
}

let superResolution (screenSize : aval<V2i>) (factor : int) (scene : ISceneNode) =
Sg.Delay(fun state ->
let r = state.Runtime

let signature =
r.CreateFramebufferSignature [
DefaultSemantic.Colors, TextureFormat.Rgba8
DefaultSemantic.DepthStencil, TextureFormat.Depth24Stencil8
]
let rt = r.CompileRender(signature, scene.GetRenderObjects state)

let renderSize = screenSize |> AVal.map (fun s -> s * factor)

let color, depth =
rt |> RenderTask.renderToColorAndDepthWithClear renderSize (clear { color (C4f(0.0f, 0.0f, 0.0f, 0.0f)); depth 1.0; stencil 0 })

let p = RenderPass.after "blub" RenderPassOrder.Arbitrary state.Pass

sg {
Sg.Shader {
Shader.blit
}
Sg.Pass p
Sg.BlendMode BlendMode.Blend
Sg.Uniform("Color", color)
Sg.Uniform("Depth", depth)
Sg.Uniform("Factor", AVal.constant factor)
Sg.Uniform("ViewportSize", screenSize)
Primitives.FullscreenQuad
}
)

// here we create a `RenderControl`, setup the camera and "compile" the scene for rendering.
let run() =
task {
Expand All @@ -148,7 +229,7 @@ let run() =

let time =
let sw = System.Diagnostics.Stopwatch.StartNew()
ctrl.Time |> AVal.map (fun _ -> sw.Elapsed)
ctrl.Time |> AVal.map (fun _ -> sw.Elapsed.TotalSeconds.ToString("0"))



Expand All @@ -164,10 +245,10 @@ let run() =



let trafo =
time |> AVal.map (fun t ->
Trafo3d.RotationZ t.TotalSeconds
)
let trafo = AVal.constant Trafo3d.Identity
// time |> AVal.map (fun t ->
// Trafo3d.RotationZ t.TotalSeconds
// )

let gridSize = cval 2

Expand Down Expand Up @@ -227,13 +308,16 @@ let run() =
}


// a text (using our font from above)
// a text (using our font from above)

//Sg.superResolution ctrl.Size 4 (
sg {
Sg.Scale 0.3
Sg.Trafo (Trafo3d.RotationX Constant.PiHalf)
Sg.Translate(0.0, 1.0, 0.5)
Sg.Text(time |> AVal.map string, align = TextAlignment.Center, font = Roboto.Font, color = AVal.constant C4b.White)
}
//)


sg {
Expand Down
15 changes: 3 additions & 12 deletions src/Examples/RenderingOnly/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
{
"profiles": {
"KitchenSink": {
"RenderingOnly": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "https://localhost:6001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://0.0.0.0:6001;http://0.0.0.0:6000",
"nativeDebugging": true
}
},
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 44363
"applicationUrl": "https://0.0.0.0:6001;http://0.0.0.0:6000"
}
}
}

0 comments on commit 9f2b9f7

Please sign in to comment.