public HmdSwapTextureSet(IntPtr hmdPtr, Device device, int width, int height)
        {
            var cDesc = new _D3D11_TEXTURE2D_DESC()
            {
                Width              = (uint)width,
                Height             = (uint)height,
                MipLevels          = (uint)1,
                ArraySize          = (uint)1,
                Format             = (uint)DrawSystem.GetRenderTargetFormat(),
                SampleDesc_Count   = (uint)1,
                SampleDesc_Quality = (uint)0,
                Usage              = (uint)ResourceUsage.Default,
                BindFlags          = (uint)(BindFlags.RenderTarget | BindFlags.ShaderResource),
                CPUAccessFlags     = (uint)CpuAccessFlags.None,
                MiscFlags          = (uint)ResourceOptionFlags.None,
            };

            unsafe
            {
                IntPtr textureSetPtr;
                int    result = LibOVR.ovrHmd_CreateSwapTextureSetD3D11(hmdPtr, device.NativePointer, (IntPtr)(&cDesc), out textureSetPtr);
                if (result != 0)
                {
                    MessageBox.Show("Failed to ovrHmd_CreateSwapTexturesetD3D11() code=" + result);
                    return;
                }

                var textureSet = CRef <LibOVR.ovrSwapTextureSet> .FromPtr(textureSetPtr);

                if (textureSet == null)
                {
                    return;
                }

                var textureList = new List <CRef <LibOVR.ovrTexture> >();
                for (int texIndex = 0; texIndex < textureSet.Value.TextureCount; ++texIndex)
                {
                    IntPtr texPtr = textureSet.Value.Textures + sizeof(LibOVR.ovrTexture) * texIndex;
                    var    tex    = CRef <LibOVR.ovrTexture> .FromPtr(texPtr);

                    textureList.Add(tex);
                }

                m_hmdPtr     = hmdPtr;
                m_textureSet = textureSet;
                m_textures   = textureList.ToArray();
            }

            var csize = m_textures[0].Value.Header.TextureSize;

            m_resolution = new Size(csize.w, csize.h);

            // make SharpDx resource
            m_textureResList = m_textures.Select(t => new Texture2D(t.Value.Texture)).ToList();

            // make and set texture views
            m_renderTargetViewList = m_textureResList.Select(t => new RenderTargetView(device, t)).ToList();
            _SetTextureView(0, m_renderTargetViewList[0]);
            _SetTextureView(1, m_renderTargetViewList[1]);
        }
示例#2
0
        public static RenderTarget CreateRenderTarget(DrawSystem.D3DData d3d, string name, int width, int height)
        {
            var backBuffer = new Texture2D(d3d.Device, new Texture2DDescription()
            {
                Format            = DrawSystem.GetRenderTargetFormat(),
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            var depthBuffer = _CreateDepthBuffer(d3d, width, height);

            var res = new RenderTarget(name);

            res.ShaderResourceView = new ShaderResourceView(d3d.Device, backBuffer);
            res.TargetTexture      = backBuffer;
            res.TargetView         = new RenderTargetView(d3d.Device, backBuffer);
            res.DepthStencilView   = _CreateDepthStencilView(d3d, depthBuffer);

            res._AddDisposable(backBuffer);
            res._AddDisposable(depthBuffer);
            res._AddDisposable(res.ShaderResourceView);
            res._AddDisposable(res.TargetView);
            res._AddDisposable(res.DepthStencilView);

            return(res);
        }
        public HmdMirrorTexture(IntPtr hmdPtr, Device device, int width, int height)
        {
            var cDesc = new _D3D11_TEXTURE2D_DESC()
            {
                Width              = (uint)width,
                Height             = (uint)height,
                MipLevels          = (uint)1,
                ArraySize          = (uint)1,
                Format             = (uint)DrawSystem.GetRenderTargetFormat(),
                SampleDesc_Count   = (uint)1,
                SampleDesc_Quality = (uint)0,
                Usage              = (uint)ResourceUsage.Default,
                BindFlags          = (uint)BindFlags.None,
                CPUAccessFlags     = (uint)CpuAccessFlags.None,
                MiscFlags          = (uint)ResourceOptionFlags.None,
            };

            unsafe
            {
                IntPtr mirrorTexturePtr;
                int    result = LibOVR.ovrHmd_CreateMirrorTextureD3D11(hmdPtr, device.NativePointer, (IntPtr)(&cDesc), out mirrorTexturePtr);
                if (result != 0)
                {
                    MessageBox.Show("Failed to ovrHmd_CreateMirrorTextureD3D11() code=" + result);
                    return;
                }

                IntPtr nativeTexturePtr = ((LibOVR.ovrTexture *)mirrorTexturePtr)->Texture;
                var    texture          = new Texture2D(nativeTexturePtr);

                // succeeded all
                m_mirrorTexturePtr = mirrorTexturePtr;
                m_hmdPtr           = hmdPtr;
                m_texture          = texture;
            }
        }
示例#4
0
        static void Main()
        {
            bool bStereoRendering = false; // change to 'false' due to non-stereo rendering for debug
            int  multiThreadCount = 4;     // 1 is single thread

            HmdDevice hmd = null;

            try
            {
                // init oculus rift hmd system
                HmdSystem.Initialize();
                var hmdSys = HmdSystem.GetInstance();
                hmd = hmdSys.DetectHmd();
            }
            catch (Exception)
            {
                // failed to detect hmd
                hmd = null;
            }

#if !DEBUG
            var configForm = new ConfigForm(hmd);
            Application.Run(configForm);
            if (configForm.HasResult())
            {
                bStereoRendering = configForm.GetResult().UseHmd;
            }
            else
            {
                // cancel
                return;
            }
#endif
            Size resolution = new Size();
            if (!bStereoRendering)
            {
                //resolution.Width = 1920;// Full HD
                //resolution.Height = 1080;
                resolution.Width  = 1280;
                resolution.Height = 720;
            }
            else
            {
                hmd.ResetPose();
                resolution = hmd.Resolution;
            }

            var form = new MainForm();
            form.ClientSize = resolution;

            // Create Device & SwapChain
            var desc = new SwapChainDescription()
            {
                BufferCount     = 2,
                ModeDescription =
                    new ModeDescription(resolution.Width, resolution.Height, new Rational(0, 1), DrawSystem.GetRenderTargetFormat()),
                IsWindowed        = true,
                OutputHandle      = form.GetRenderTarget().Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Sequential,
                Usage             = Usage.RenderTargetOutput,
                Flags             = SwapChainFlags.AllowModeSwitch,
            };

            FeatureLevel[] levels =
            {
                FeatureLevel.Level_11_0
            };

            Device    device;
            SwapChain swapChain;
#if DEBUG
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, levels, desc, out device, out swapChain);
#else
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, levels, desc, out device, out swapChain);
#endif

            // Ignore all windows events
            var factory = swapChain.GetParent <Factory>();
            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            DrawSystem.Initialize(form.GetRenderTarget().Handle, device, swapChain, hmd, bStereoRendering, multiThreadCount);
            InputSystem.Initialize(form.GetRenderTarget());
            EntitySystem.Initialize();
            MapSystem.Initialize();
            ChrSystem.Initialize();
            CameraSystem.Initialize();
            CullingSystem.Initialize();
            GameSystem.Initialize();

            GameSystem.GetInstance().Config.IsUseHmd = bStereoRendering;
            var scene = new Scene(device, swapChain, form.GetRenderTarget(), hmd, bStereoRendering, multiThreadCount);
            RenderLoop.Run(form, () => { scene.RenderFrame(); });
            scene.Dispose();

            // Release
            GameSystem.Dispose();
            CullingSystem.Dispose();
            CameraSystem.Dispose();
            ChrSystem.Dispose();
            MapSystem.Dispose();
            EntitySystem.Dispose();
            InputSystem.Dispose();
            DrawSystem.Dispose();
            device.Dispose();
            swapChain.Dispose();
            HmdSystem.Dispose();
        }