Skip to content

Commit

Permalink
brought back quad-buffer stereo support for gl backend
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldsteinlechner committed Feb 26, 2024
1 parent db630dd commit 1b1dcae
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Aardvark.Rendering.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32002.185
MinimumVisualStudioVersion = 10.0.40219.1
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "24 - QuadbufferStereo", "Scratch (netcore)\24 - QuadbufferStereo\24 - QuadbufferStereo.fsproj", "{2E9FDA6B-C7D8-4103-9F4A-3EE1FCC39DED}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "37 - ReversedDepth", "Examples (netcore)\37 - ReversedDepth\37 - ReversedDepth.fsproj", "{C7F84FFE-9DA1-47D1-9607-6F1936F29CF6}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "21 - DebugPrint", "Scratch (netcore)\21 - DebugPrint\21 - DebugPrint.fsproj", "{8C626D69-45D4-4327-ADB9-5449DBCA0B06}"
Expand Down Expand Up @@ -228,6 +230,10 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2E9FDA6B-C7D8-4103-9F4A-3EE1FCC39DED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E9FDA6B-C7D8-4103-9F4A-3EE1FCC39DED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E9FDA6B-C7D8-4103-9F4A-3EE1FCC39DED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E9FDA6B-C7D8-4103-9F4A-3EE1FCC39DED}.Release|Any CPU.Build.0 = Release|Any CPU
{C7F84FFE-9DA1-47D1-9607-6F1936F29CF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7F84FFE-9DA1-47D1-9607-6F1936F29CF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7F84FFE-9DA1-47D1-9607-6F1936F29CF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -617,6 +623,7 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2E9FDA6B-C7D8-4103-9F4A-3EE1FCC39DED} = {29CA9CA1-CC31-4B5D-8DB0-2FEA748C09D8}
{C7F84FFE-9DA1-47D1-9607-6F1936F29CF6} = {5E3312C4-1B3C-4D41-964C-C3A3E01DA966}
{8C626D69-45D4-4327-ADB9-5449DBCA0B06} = {29CA9CA1-CC31-4B5D-8DB0-2FEA748C09D8}
{C335111D-56DC-4053-958F-93B98EEDD02C} = {29CA9CA1-CC31-4B5D-8DB0-2FEA748C09D8}
Expand Down Expand Up @@ -673,6 +680,7 @@ Global
{EBC7AF0C-DFDC-49C0-9939-630EEAB06C73} = {80F74456-D82F-4D38-83FE-362EFD0C2FCB}
{6C943F93-9A9A-4A57-9A75-A235A3443C22} = {C664AC25-597F-470A-854B-045CD7E59CE2}
{79D48400-F885-461A-B747-E3675A7EF2EF} = {C664AC25-597F-470A-854B-045CD7E59CE2}
{B5AD6854-A8DC-475D-8B09-242DDB5D79BC} = {0C4B4E0F-315B-4F93-BFCE-CAE5003FB82B}
{99DD80B4-B1B8-4042-BEEA-B3A7FD617242} = {2993E291-DFD2-4AB6-9E3B-B7F66015C914}
{7AAAC578-443F-4120-BA74-3732CCE920E8} = {2993E291-DFD2-4AB6-9E3B-B7F66015C914}
{FB047D5E-D49A-462E-BAA0-B058CE06CFC9} = {2993E291-DFD2-4AB6-9E3B-B7F66015C914}
Expand Down
207 changes: 204 additions & 3 deletions src/Application/Aardvark.Application.Slim.GL/Application.fs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,201 @@ module private OpenGL =
glfw.WindowHint(WindowHintInt.Samples, if cfg.samples = 1 then 0 else cfg.samples)
}

type OpenGlApplication private (runtime : Runtime, shaderCachePath : Option<string>, hideCocoaMenuBar : bool) as this =
inherit Application(runtime, OpenGL.interop runtime.DebugConfig, hideCocoaMenuBar)
module StereoExperimental =

open OpenGL
open FSharp.Data.Adaptive

let createSwapchain (runtime : Runtime) (signature : IFramebufferSignature) (ctx : Aardvark.Rendering.GL.ContextHandle) (glfw : Glfw) (win : nativeptr<WindowHandle>) (size : V2i) =

let colors =
runtime.CreateTexture2DArray(size, TextureFormat.Rgba8, samples = signature.Samples, count = 2) |> unbox<Texture>

let depth =
runtime.CreateTexture2DArray(size, TextureFormat.Depth24Stencil8, samples = signature.Samples, count = 2) |> unbox<Texture>

let framebuffer =
runtime.CreateFramebuffer(signature, [
DefaultSemantic.Colors, colors.[TextureAspect.Color,0,*] :> IFramebufferOutput
DefaultSemantic.DepthStencil, depth.[TextureAspect.Depth,0,*] :> IFramebufferOutput
])


{ new ISwapchain with
override this.Dispose() =
framebuffer.Dispose()
colors.Dispose()
depth.Dispose()
//temp.Dispose()

override this.Run(task : IRenderTask, queries : IQuery list) =
use __ = runtime.Context.RenderingLock ctx

let output = OutputDescription.ofFramebuffer framebuffer

GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, (framebuffer |> unbox<Framebuffer>).Handle)
GL.ColorMask(true, true, true, true)
GL.DepthMask(true)
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)

let rt = { RenderToken.Empty with Queries = queries}
task.Run(AdaptiveToken.Top, rt, output)

let fSrc = GL.GenFramebuffer()
let fDst = GL.GenFramebuffer()

let mutable temp = 0
GL.CreateTextures(TextureTarget.Texture2D, 1, &temp)
if temp = 0 then failwith ""
GL.TextureStorage2D(temp, 1, unbox (int colors.Format), size.X, size.Y)

GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fSrc)
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fDst)
GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, colors.Handle, 0, 0)
GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, temp, 0)
GL.BlitFramebuffer(0, 0, colors.Size.X, colors.Size.Y, 0, 0, size.X, size.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear)

GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fDst)
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0)
GL.DrawBuffer(DrawBufferMode.BackLeft)
GL.BlitFramebuffer(0, 0, size.X, size.Y, 0, 0, size.X, size.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear)


GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fSrc)
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fDst)
GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, colors.Handle, 0, 1)
GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, temp, 0)
GL.BlitFramebuffer(0, 0, colors.Size.X, colors.Size.Y, 0, 0, size.X, size.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear)

GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fDst)
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0)
GL.DrawBuffer(DrawBufferMode.BackRight)
GL.BlitFramebuffer(0, 0, size.X, size.Y, 0, 0, size.X, size.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear)

GL.DeleteFramebuffer fSrc
GL.DeleteFramebuffer fDst
GL.DeleteTexture temp

glfw.SwapBuffers(win)
true

override this.Size =
size
}

let private createStereoSurface (runtime : Runtime) (cfg: WindowConfig) (glfw : Glfw) (win : nativeptr<WindowHandle>) =
let old = glfw.GetCurrentContext()

glfw.MakeContextCurrent(NativePtr.zero)

let ctx =
new MyGraphicsContext(glfw, win)

let info =
new MyWindowInfo(win)

let mutable samples = cfg.samples

if not (isNull ctx) then
glfw.MakeContextCurrent(win)
let current = glfw.GetCurrentContext()
let mutable desc = Unchecked.defaultof<_>
let error = glfw.GetError(&desc)
if error <> Silk.NET.GLFW.ErrorCode.NoError then
Log.error "[GLFW] error after making trying to make context current: %A" error
if current <> win then
Log.error "[GLFW] could not make context current"
ctx.LoadAll()
glfw.SwapInterval(if cfg.vsync then 1 else 0)
samples <- getFramebufferSamples() |> Option.defaultValue cfg.samples
glfw.MakeContextCurrent(NativePtr.zero)

if old <> NativePtr.zero then
glfw.MakeContextCurrent old

let handle = new Aardvark.Rendering.GL.ContextHandle(ctx, info)

let signature =
handle.Use (fun _ ->
handle.Initialize(runtime.DebugConfig, setDefaultStates = true)

let perLayerUniforms =
Set.ofList [
"ProjTrafo";
"ViewTrafo";
"ModelViewTrafo";
"ViewProjTrafo";
"ModelViewProjTrafo"

"ProjTrafoInv";
"ViewTrafoInv";
"ModelViewTrafoInv";
"ViewProjTrafoInv";
"ModelViewProjTrafoInv"
]

runtime.CreateFramebufferSignature([
DefaultSemantic.Colors, TextureFormat.Rgba8
DefaultSemantic.DepthStencil, TextureFormat.Depth24Stencil8
], samples, 2, perLayerUniforms)
)

{ new IWindowSurface with
override x.Signature = signature
override this.Handle =
handle :> obj
override this.CreateSwapchain(size: V2i) =
createSwapchain runtime signature handle glfw win size
override this.Dispose() =
signature.Dispose()
ctx.Dispose()
}

let stereo (debug : DebugConfig) =
let disableErrorChecks =
debug.ErrorFlagCheck = ErrorFlagCheck.Disabled

{ new IWindowInterop with
member x.Boot(glfw : Glfw) =
initVersion(glfw)

member x.CreateSurface(runtime : IRuntime, cfg : WindowConfig, glfw : Glfw, win : nativeptr<WindowHandle>)=
createStereoSurface (runtime :?> _) cfg glfw win

member x.WindowHints(cfg : WindowConfig, glfw) =
glfw.WindowHint(WindowHintClientApi.ClientApi, ClientApi.OpenGL)
glfw.WindowHint(WindowHintInt.ContextVersionMajor, version.Major)
glfw.WindowHint(WindowHintInt.ContextVersionMinor, version.Minor)
glfw.WindowHint(WindowHintInt.DepthBits, 24)
glfw.WindowHint(WindowHintInt.StencilBits, 8)

let m = glfw.GetPrimaryMonitor()
let mode = glfw.GetVideoMode(m) |> NativePtr.read
glfw.WindowHint(WindowHintInt.RedBits, 8)
glfw.WindowHint(WindowHintInt.GreenBits, 8)
glfw.WindowHint(WindowHintInt.BlueBits, 8)
glfw.WindowHint(WindowHintInt.AlphaBits, 8)
glfw.WindowHint(WindowHintInt.RefreshRate, mode.RefreshRate)
glfw.WindowHint(WindowHintOpenGlProfile.OpenGlProfile, OpenGlProfile.Core)
glfw.WindowHint(WindowHintRobustness.ContextRobustness, Robustness.LoseContextOnReset)
glfw.WindowHint(WindowHintBool.OpenGLForwardCompat, true)
glfw.WindowHint(WindowHintBool.Stereo, true)
glfw.WindowHint(WindowHintBool.DoubleBuffer, true)
glfw.WindowHint(WindowHintBool.OpenGLDebugContext, false)
glfw.WindowHint(WindowHintBool.ContextNoError, disableErrorChecks && supportsNoError)
glfw.WindowHint(WindowHintBool.SrgbCapable, false)
if RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then
glfw.WindowHint(WindowHintBool.CocoaRetinaFramebuffer, cfg.physicalSize)

glfw.WindowHint(WindowHintBool.ScaleToMonitor, true)
glfw.WindowHint(WindowHintInt.Samples, if cfg.samples = 1 then 0 else cfg.samples)
}

type OpenGlApplication private (runtime : Runtime, shaderCachePath : Option<string>, windowInterop : IWindowInterop, hideCocoaMenuBar : bool) as this =
inherit Application(runtime, windowInterop, hideCocoaMenuBar)

// Note: We ignore the passed parent since we determine the parent context in the CreateWindow method.
// This is always the first created context and should therefore match the passed one anyway.
Expand All @@ -360,7 +553,15 @@ type OpenGlApplication private (runtime : Runtime, shaderCachePath : Option<stri
if forceNvidia && RuntimeInformation.IsOSPlatform OSPlatform.Windows then
DynamicLinker.tryLoadLibrary "nvapi64.dll" |> ignore

new OpenGlApplication(new Runtime(debug), shaderCachePath, hideCocoaMenuBar)
let runtime = new Runtime(debug)
let interop = OpenGL.interop runtime.DebugConfig
new OpenGlApplication(runtime, shaderCachePath, interop, hideCocoaMenuBar)

new(debug : IDebugConfig, windowInterop: IWindowInterop, shaderCachePath : Option<string>,
[<Optional; DefaultParameterValue(false)>] hideCocoaMenuBar : bool) =

let runtime = new Runtime(debug)
new OpenGlApplication(runtime, shaderCachePath, windowInterop, hideCocoaMenuBar)

new(forceNvidia : bool, debug : bool, shaderCachePath : Option<string>) =
new OpenGlApplication(forceNvidia, DebugLevel.ofBool debug, shaderCachePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
<OutputPath>..\..\..\bin\Release</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Include="paket.references" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\..\Aardvark.Rendering\Aardvark.Rendering.fsproj" />
<ProjectReference Include="..\..\Aardvark.Rendering.GL\Aardvark.Rendering.GL.fsproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\..\bin\Debug</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\..\..\bin\Release</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Include="Readme.md" />
<Compile Include="Program.fs" />
<None Include="paket.references" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Aardvark.Rendering\Aardvark.Rendering.fsproj" />
<ProjectReference Include="..\..\Aardvark.Rendering.GL\Aardvark.Rendering.GL.fsproj" />
<ProjectReference Include="..\..\Aardvark.Rendering.Text\Aardvark.Rendering.Text.fsproj" />
<ProjectReference Include="..\..\Aardvark.Rendering.Vulkan\Aardvark.Rendering.Vulkan.fsproj" />
<ProjectReference Include="..\..\Aardvark.SceneGraph.IO\Aardvark.SceneGraph.IO.fsproj" />
<ProjectReference Include="..\..\Aardvark.SceneGraph\Aardvark.SceneGraph.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application.OpenVR.Vulkan\Aardvark.Application.OpenVR.Vulkan.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application.OpenVR\Aardvark.Application.OpenVR.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application.Utilities\Aardvark.Application.Utilities.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application.Slim.GL\Aardvark.Application.Slim.GL.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application.Slim.Vulkan\Aardvark.Application.Slim.Vulkan.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application.Slim\Aardvark.Application.Slim.fsproj" />
<ProjectReference Include="..\..\Application\Aardvark.Application\Aardvark.Application.fsproj" />
</ItemGroup>
<Import Project="..\..\..\.paket\Paket.Restore.targets" />
</Project>
Loading

0 comments on commit 1b1dcae

Please sign in to comment.