void CreateEyeTextures() { for (int eye_idx = 0; eye_idx < 2; eye_idx++) { OculusWrap.OVR.EyeType eye = (OculusWrap.OVR.EyeType)eye_idx; EyeTexture eye_tex = new EyeTexture(); eye_texes[eye_idx] = eye_tex; // Retrieve size and position of the texture for the current eye. eye_tex.FieldOfView = hmd.DefaultEyeFov[eye_idx]; eye_tex.TextureSize = hmd.GetFovTextureSize(eye, hmd.DefaultEyeFov[eye_idx], 1.0f); eye_tex.RenderDescription = hmd.GetRenderDesc(eye, hmd.DefaultEyeFov[eye_idx]); eye_tex.HmdToEyeViewOffset = eye_tex.RenderDescription.HmdToEyeViewOffset; eye_tex.ViewportSize.Position = new OculusWrap.OVR.Vector2i(0, 0); eye_tex.ViewportSize.Size = eye_tex.TextureSize; eye_tex.Viewport = new Viewport(0, 0, eye_tex.TextureSize.Width, eye_tex.TextureSize.Height, 0.0f, 1.0f); // Define a texture at the size recommended for the eye texture. eye_tex.Texture2DDescription = DefineEyeTextureDescription(eye_tex); // Convert the SharpDX texture description to the native Direct3D texture description. OculusWrap.OVR.D3D11.D3D11_TEXTURE2D_DESC swapTextureDescriptionD3D11 = SharpDXHelpers.CreateTexture2DDescription(eye_tex.Texture2DDescription); // Create a SwapTextureSet, which will contain the textures to render to, for the current eye. var result = hmd.CreateSwapTextureSetD3D11(device.NativePointer, ref swapTextureDescriptionD3D11, out eye_tex.SwapTextureSet); WriteErrorDetails(oculus, result, "Failed to create swap texture set."); // Create room for each DirectX texture in the SwapTextureSet. eye_tex.Textures = new Texture2D[eye_tex.SwapTextureSet.TextureCount]; eye_tex.RenderTargetViews = new RenderTargetView[eye_tex.SwapTextureSet.TextureCount]; // Create a texture 2D and a render target view, for each unmanaged texture contained in the SwapTextureSet. for (int tex_idx = 0; tex_idx < eye_tex.SwapTextureSet.TextureCount; tex_idx++) { // Retrieve the current textureData object. OculusWrap.OVR.D3D11.D3D11TextureData textureData = eye_tex.SwapTextureSet.Textures[tex_idx]; // Create a managed Texture2D, based on the unmanaged texture pointer. eye_tex.Textures[tex_idx] = new Texture2D(textureData.Texture); // Create a render target view for the current Texture2D. eye_tex.RenderTargetViews[tex_idx] = new RenderTargetView(device, eye_tex.Textures[tex_idx]); } // Define the depth buffer, at the size recommended for the eye texture. eye_tex.DepthBufferDescription = DefineEyeDepthBufferDescription(eye_tex); // Create the depth buffer. eye_tex.DepthBuffer = new Texture2D(device, eye_tex.DepthBufferDescription); eye_tex.DepthStencilView = new DepthStencilView(device, eye_tex.DepthBuffer); // Specify the texture to show on the HMD. layer_eye_fov.ColorTexture[eye_idx] = eye_tex.SwapTextureSet.SwapTextureSetPtr; layer_eye_fov.Viewport[eye_idx].Position = new OculusWrap.OVR.Vector2i(0, 0); layer_eye_fov.Viewport[eye_idx].Size = eye_tex.TextureSize; layer_eye_fov.Fov[eye_idx] = eye_tex.FieldOfView; layer_eye_fov.Header.Flags = OculusWrap.OVR.LayerFlags.None; } }
Texture2DDescription DefineEyeTextureDescription(EyeTexture eye_tex) { return(new Texture2DDescription() { Width = eye_tex.TextureSize.Width, Height = eye_tex.TextureSize.Height, ArraySize = 1, MipLevels = 1, Format = Format.R8G8B8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, }); }
Texture2DDescription DefineEyeDepthBufferDescription(EyeTexture eye_tex) { return(new Texture2DDescription() { Format = Format.D32_Float, Width = eye_tex.TextureSize.Width, Height = eye_tex.TextureSize.Height, ArraySize = 1, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, }); }
public void Render() { if (hmd == null) { return; } OculusWrap.OVR.Vector3f[] hmdToEyeViewOffsets = { eye_texes[0].HmdToEyeViewOffset, eye_texes[1].HmdToEyeViewOffset }; OculusWrap.OVR.FrameTiming frameTiming = hmd.GetFrameTiming(0); OculusWrap.OVR.TrackingState trackingState = hmd.GetTrackingState(frameTiming.DisplayMidpointSeconds); OculusWrap.OVR.Posef[] exe_poses = new OculusWrap.OVR.Posef[2]; // Calculate the position and orientation of each eye. oculus.CalcEyePoses(trackingState.HeadPose.ThePose, hmdToEyeViewOffsets, ref exe_poses); UVSCR_variable.Set(UVSCR()); for (int eye_idx = 0; eye_idx < 2; eye_idx++) { OculusWrap.OVR.EyeType eye = (OculusWrap.OVR.EyeType)eye_idx; EyeTexture eye_tex = eye_texes[eye_idx]; layer_eye_fov.RenderPose[eye_idx] = exe_poses[eye_idx]; // Retrieve the index of the active texture and select the next texture as being active next. int tex_idx = eye_tex.SwapTextureSet.CurrentIndex++; ctx.OutputMerger.SetRenderTargets(eye_tex.DepthStencilView, eye_tex.RenderTargetViews[tex_idx]); ctx.ClearDepthStencilView(eye_tex.DepthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0); ctx.ClearRenderTargetView(eye_tex.RenderTargetViews[tex_idx], ScreenColor); ctx.Rasterizer.SetViewport(eye_tex.Viewport); UpdateTransform(exe_poses[eye_idx], eye_tex.FieldOfView); DrawFigure(); } hmd.SubmitFrame(0, layers); ctx.CopyResource(mtex, buf0); swap_chain.Present(0, PresentFlags.None); }
Texture2DDescription DefineEyeTextureDescription(EyeTexture eye_tex) { return new Texture2DDescription() { Width = eye_tex.TextureSize.Width, Height = eye_tex.TextureSize.Height, ArraySize = 1, MipLevels = 1, Format = Format.R8G8B8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, }; }
Texture2DDescription DefineEyeDepthBufferDescription(EyeTexture eye_tex) { return new Texture2DDescription() { Format = Format.D32_Float, Width = eye_tex.TextureSize.Width, Height = eye_tex.TextureSize.Height, ArraySize = 1, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, }; }