Skip to content

Commit

Permalink
### 5.5.12
Browse files Browse the repository at this point in the history
- [OpenGL/WPF/ThreadedRenderControl] frame throttle
  • Loading branch information
aszabo314 committed Dec 9, 2024
1 parent 5a07ffb commit 37dc34a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 88 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 5.5.12
- [OpenGL/WPF/ThreadedRenderControl] frame throttle


### 5.5.11
- [LodTreeNode] uniforms concurrent access fix

Expand Down
184 changes: 96 additions & 88 deletions src/Application/Aardvark.Application.WPF.GL/ThreadedRenderControl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -207,104 +207,112 @@ type ThreadedRenderControl(runtime : Runtime, debug : IDebugConfig, samples : in


let mutable ctxLock = None

let t = new MultimediaTimer.Trigger(16)
while not disposed do
lock renderPendingLock (fun () ->
while not renderPending do
Monitor.Wait renderPendingLock |> ignore
renderPending <- false
)
match ctxLock with
| None ->
let handle = runtime.Context.CreateContext()
let h = ctx.RenderingLock handle
ctxLock <- Some h
GL.SetDefaultStates()
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0)
GL.Disable(EnableCap.Multisample)
| Some _ -> ()

match task with
| Some task ->

let color = textures.Take()
t.Wait()
// lock renderPendingLock (fun () ->
// while not renderPending do
// Monitor.Wait renderPendingLock |> ignore
// renderPending <- false
// )
let shouldRender =
lock renderPendingLock (fun () ->
let o = renderPending
renderPending <- false
o
)
if shouldRender then
match ctxLock with
| None ->
let handle = runtime.Context.CreateContext()
let h = ctx.RenderingLock handle
ctxLock <- Some h
GL.SetDefaultStates()
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0)
GL.Disable(EnableCap.Multisample)
| Some _ -> ()

let depth =
match depth with
| Some depth when depth.Size.XY = color.Size.XY && depth.Multisamples = samples ->
depth
| _ ->
match task with
| Some task ->

let color = textures.Take()

let depth =
match depth with
| Some t -> ctx.Delete t
| None -> ()

let d = ctx.CreateTexture2D(color.Size.XY, 1, TextureFormat.Depth24Stencil8, samples)
depth <- Some d
d

let realColor =
if samples <= 1 then
color
else
match realColor with
| Some realColor when realColor.Size.XY = color.Size.XY && realColor.Multisamples = samples ->
realColor
| Some depth when depth.Size.XY = color.Size.XY && depth.Multisamples = samples ->
depth
| _ ->
match realColor with
match depth with
| Some t -> ctx.Delete t
| None -> ()

let d = ctx.CreateTexture2D(color.Size.XY, 1, TextureFormat.Rgba8, samples)
realColor <- Some d
d
let size = realColor.Size.XY
let fbo =
ctx.CreateFramebuffer(
fboSignature,
[0, DefaultSemantic.Colors, realColor.[TextureAspect.Color, 0, 0]],
Some (depth.[TextureAspect.DepthStencil, 0, 0] :> IFramebufferOutput)
)
let d = ctx.CreateTexture2D(color.Size.XY, 1, TextureFormat.Depth24Stencil8, samples)
depth <- Some d
d

let realColor =
if samples <= 1 then
color
else
match realColor with
| Some realColor when realColor.Size.XY = color.Size.XY && realColor.Multisamples = samples ->
realColor
| _ ->
match realColor with
| Some t -> ctx.Delete t
| None -> ()

let d = ctx.CreateTexture2D(color.Size.XY, 1, TextureFormat.Rgba8, samples)
realColor <- Some d
d
let size = realColor.Size.XY
let fbo =
ctx.CreateFramebuffer(
fboSignature,
[0, DefaultSemantic.Colors, realColor.[TextureAspect.Color, 0, 0]],
Some (depth.[TextureAspect.DepthStencil, 0, 0] :> IFramebufferOutput)
)


GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo.Handle)
GL.ColorMask(true, true, true, true)
GL.DepthMask(true)
GL.StencilMask(0xFFFFFFFFu)
GL.Viewport(0,0,size.X, size.Y)
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f)
GL.ClearDepth(1.0)
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit ||| ClearBufferMask.StencilBufferBit)
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0)

task.Run(AdaptiveToken.Top, RenderToken.Zero, fbo)

if samples > 1 then
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer,fbo.Handle)
let dst = GL.GenFramebuffer()
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer,dst)
GL.FramebufferTexture(FramebufferTarget.DrawFramebuffer,FramebufferAttachment.ColorAttachment0,color.Handle,0)
GL.BlitFramebuffer(0,0,size.X,size.Y,0,0,size.X,size.Y,ClearBufferMask.ColorBufferBit,BlitFramebufferFilter.Linear)
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer,0)
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer,0)
GL.DeleteFramebuffer(dst)
ctx.Delete fbo

GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo.Handle)
GL.ColorMask(true, true, true, true)
GL.DepthMask(true)
GL.StencilMask(0xFFFFFFFFu)
GL.Viewport(0,0,size.X, size.Y)
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f)
GL.ClearDepth(1.0)
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit ||| ClearBufferMask.StencilBufferBit)
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0)
GL.Flush()
GL.Finish()

task.Run(AdaptiveToken.Top, RenderToken.Zero, fbo)

if samples > 1 then
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer,fbo.Handle)
let dst = GL.GenFramebuffer()
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer,dst)
GL.FramebufferTexture(FramebufferTarget.DrawFramebuffer,FramebufferAttachment.ColorAttachment0,color.Handle,0)
GL.BlitFramebuffer(0,0,size.X,size.Y,0,0,size.X,size.Y,ClearBufferMask.ColorBufferBit,BlitFramebufferFilter.Linear)
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer,0)
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer,0)
GL.DeleteFramebuffer(dst)
ctx.Delete fbo

GL.Flush()
GL.Finish()

if not first then
frameTime.Insert frameWatch.Elapsed.TotalSeconds |> ignore
frameWatch.Restart()
transact (fun () -> time.Value <- nextFrameTime())
first <- false
lock presentTextures (fun () ->
presentTextures.Enqueue color
Monitor.PulseAll presentTextures
)
MessageLoop.Invalidate this |> ignore

| None ->
()
if not first then
frameTime.Insert frameWatch.Elapsed.TotalSeconds |> ignore
frameWatch.Restart()
transact (fun () -> time.Value <- nextFrameTime())
first <- false
lock presentTextures (fun () ->
presentTextures.Enqueue color
Monitor.PulseAll presentTextures
)
MessageLoop.Invalidate this |> ignore

| None ->
()
with e ->
Log.error "%A" e

Expand Down

0 comments on commit 37dc34a

Please sign in to comment.