private static (ovrTextureSwapChain, Texture[]) CreateSwapchainD3D11( ovrSession session, GraphicsDevice gd, ovrTextureSwapChainDesc desc) { ovrTextureSwapChain otsc; Texture[] textures; ovrResult result = ovr_CreateTextureSwapChainDX(session, gd.GetD3D11Info().Device, &desc, &otsc); if (result != ovrResult.Success) { throw new VeldridException($"Failed to call ovr_CreateTextureSwapChainDX: {result}"); } int textureCount = 0; ovr_GetTextureSwapChainLength(session, otsc, &textureCount); textures = new Texture[textureCount]; for (int i = 0; i < textureCount; ++i) { IntPtr nativeTexture; ovr_GetTextureSwapChainBufferDX(session, otsc, i, s_d3d11Tex2DGuid, &nativeTexture); textures[i] = gd.ResourceFactory.CreateTexture( (ulong)nativeTexture, OculusUtil.GetVeldridTextureDescription(desc)); } return(otsc, textures); }
public OculusContext(VRContextOptions options) { _options = options; ovrInitParams initParams = new ovrInitParams(); initParams.Flags = ovrInitFlags.RequestVersion | ovrInitFlags.FocusAware | ovrInitFlags.Debug; initParams.RequestedMinorVersion = 30; ovrResult result = ovr_Initialize(&initParams); if (result != ovrResult.Success) { throw new VeldridException($"Failed to initialize Oculus: {result}"); } ovrSession session; ovrGraphicsLuid luid; result = ovr_Create(&session, &luid); if (result != ovrResult.Success) { throw new VeldridException($"Failed to create an Oculus session."); } _session = session; _luid = luid; _mirrorTexture = new OculusMirrorTexture(this); }
private static bool CheckSupport() { try { ovrInitParams initParams = new ovrInitParams(); initParams.Flags = ovrInitFlags.RequestVersion | ovrInitFlags.FocusAware | ovrInitFlags.Debug; initParams.RequestedMinorVersion = 30; ovrResult result = ovr_Initialize(&initParams); if (result != ovrResult.Success) { return(false); } ovrSession session; ovrGraphicsLuid luid; result = ovr_Create(&session, &luid); if (result != ovrResult.Success) { return(false); } ovr_Destroy(session); ovr_Shutdown(); return(true); } catch { return(false); } }
private static (ovrTextureSwapChain, Texture[]) CreateSwapchainGL( ovrSession session, GraphicsDevice gd, ovrTextureSwapChainDesc desc) { ovrTextureSwapChain otsc = default; Texture[] textures = default; ovrResult result = ovrResult.Success; gd.GetOpenGLInfo().ExecuteOnGLThread(() => { ovrTextureSwapChainDesc localDesc = desc; localDesc.MiscFlags = localDesc.MiscFlags & ~(ovrTextureMiscFlags.DX_Typeless | ovrTextureMiscFlags.AllowGenerateMips); localDesc.BindFlags = ovrTextureBindFlags.None; ovrTextureSwapChain sc; result = ovr_CreateTextureSwapChainGL(session, &localDesc, &sc); if (result != ovrResult.Success) { return; } otsc = sc; }); if (otsc.IsNull) { throw new VeldridException($"Failed to call ovr_CreateTextureSwapChainGL: {result}"); } int textureCount = 0; ovr_GetTextureSwapChainLength(session, otsc, &textureCount); textures = new Texture[textureCount]; for (int i = 0; i < textureCount; ++i) { uint glID; ovr_GetTextureSwapChainBufferGL(session, otsc, i, &glID); textures[i] = gd.ResourceFactory.CreateTexture( glID, OculusUtil.GetVeldridTextureDescription(desc)); } return(otsc, textures); }
public override (string[] instance, string[] device) GetRequiredVulkanExtensions() { uint instanceExtCount; ovrResult result = ovr_GetInstanceExtensionsVk(_luid, null, &instanceExtCount); if (result != ovrResult.Success) { throw new VeldridException($"Failed to retrieve the number of required Vulkan instance extensions: {result}"); } byte[] instanceExtensions = new byte[instanceExtCount]; fixed(byte *instanceExtensionsPtr = &instanceExtensions[0]) { result = ovr_GetInstanceExtensionsVk(_luid, instanceExtensionsPtr, &instanceExtCount); if (result != ovrResult.Success) { throw new VeldridException($"Failed to retrieve the required Vulkan instance extensions: {result}"); } } string[] instance = GetStringArray(instanceExtensions); uint deviceExtCount; result = ovr_GetDeviceExtensionsVk(_luid, null, &deviceExtCount); if (result != ovrResult.Success) { throw new VeldridException($"Failed to retrieve the number of required Vulkan device extensions: {result}"); } byte[] deviceExtensions = new byte[deviceExtCount]; fixed(byte *deviceExtensionsPtr = &deviceExtensions[0]) { result = ovr_GetDeviceExtensionsVk(_luid, deviceExtensionsPtr, &deviceExtCount); if (result != ovrResult.Success) { throw new VeldridException($"Failed to retrieve the required Vulkan device extensions: {result}"); } } string[] device = GetStringArray(deviceExtensions); return(instance, device); }
public override void Initialize(GraphicsDevice gd) { _gd = gd; if (gd.GetVulkanInfo(out BackendInfoVulkan vkInfo)) { IntPtr physicalDevice; ovrResult result = ovr_GetSessionPhysicalDeviceVk(_session, _luid, vkInfo.Instance, &physicalDevice); if (result != ovrResult.Success) { throw new VeldridException($"Failed to get Vulkan physical device."); } result = ovr_SetSynchonizationQueueVk(_session, vkInfo.GraphicsQueue); if (result != ovrResult.Success) { throw new VeldridException($"Failed to set the Oculus session's Vulkan synchronization queue."); } } _hmdDesc = ovr_GetHmdDesc(_session); _deviceName = _hmdDesc.ProductName.ToString(); _eyeRenderViewport = new ovrRecti[2]; _eyeSwapchains = new OculusSwapchain[2]; for (int eye = 0; eye < 2; ++eye) { ovrSizei idealSize = ovr_GetFovTextureSize( _session, (ovrEyeType)eye, _hmdDesc.DefaultEyeFov[eye], 1.0f); _eyeSwapchains[eye] = new OculusSwapchain( _gd, _session, idealSize.w, idealSize.h, Util.GetSampleCount(_options.EyeFramebufferSampleCount), createDepth: true); _eyeRenderViewport[eye].Pos.X = 0; _eyeRenderViewport[eye].Pos.Y = 0; _eyeRenderViewport[eye].Size = idealSize; } }
public override void SubmitFrame() { if (_gd.GetOpenGLInfo(out BackendInfoOpenGL glInfo)) { glInfo.FlushAndFinish(); } for (int eye = 0; eye < 2; ++eye) { _eyeSwapchains[eye].Commit(); } // Initialize our single full screen Fov layer. ovrLayerEyeFovDepth ld = new ovrLayerEyeFovDepth(); ld.Header.Type = ovrLayerType.EyeFovDepth; ld.Header.Flags = _gd.BackendType == GraphicsBackend.OpenGL || _gd.BackendType == GraphicsBackend.OpenGLES ? ovrLayerFlags.TextureOriginAtBottomLeft : ovrLayerFlags.None; ld.ProjectionDesc = _posTimewarpProjectionDesc; ld.SensorSampleTime = _sensorSampleTime; for (int eye = 0; eye < 2; ++eye) { ld.ColorTexture[eye] = _eyeSwapchains[eye].ColorChain; ld.DepthTexture[eye] = _eyeSwapchains[eye].DepthChain; ld.Viewport[eye] = _eyeRenderViewport[eye]; ld.Fov[eye] = _hmdDesc.DefaultEyeFov[eye]; ld.RenderPose[eye] = _eyeRenderPoses[eye]; } ovrLayerHeader *layers = &ld.Header; ovrResult result = ovr_SubmitFrame(_session, _frameIndex, null, &layers, 1); if (result != ovrResult.Success) { throw new VeldridException($"Failed to submit Oculus frame: {result}"); } _frameIndex++; }