Exemple #1
0
        //Called when mod is loading
        public override void OnLoad()
        {
            ModConsole.Print("Initializing OpenVR");
            if (!OpenVRInit())
            {
                return;
            }


            mainCamera = Camera.main;

            /*ModConsole.Print(mainCamera.cullingMask);
             * mainCamera.cullingMask = ~mainCamera.cullingMask;
             *
             * mainCamera.enabled = false;*/

            ModConsole.Print(SystemInfo.graphicsDeviceVersion);


            System.Diagnostics.Trace.WriteLine("Initializing our own D3D11 device");
            d3d11Device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.Debug);


            hook = new D3D11Hook();
            hook.Create();

            System.Diagnostics.Trace.WriteLine("Getting RenderTarget Size");

            uint w = 0, h = 0;

            vrSystem.GetRecommendedRenderTargetSize(ref w, ref h);
            vrRig = new VRRig((int)w, (int)h);

            System.Diagnostics.Trace.WriteLine("Fetching Unity D3D11 Device");
            var devChild = vrRig.leftTexture.QueryInterface <SharpDX.Direct3D11.DeviceChild>();

            unityRenderer = devChild.Device;
            devChild.Dispose();

            ModConsole.Print(unityRenderer.CreationFlags & SharpDX.Direct3D11.DeviceCreationFlags.SingleThreaded);
            vrRenderer = new VRRenderer(d3d11Device, unityRenderer, vrRig);

            hook.OnRender += HookRender;

            System.Diagnostics.Trace.WriteLine("All done");
            ModConsole.Print("MSCVR initialized");
        }
Exemple #2
0
        public VRRenderer(SharpDX.Direct3D11.Device d3d11Device, SharpDX.Direct3D11.Device unityDevice, VRRig rig)
        {
            instance = this;


            this.rig      = rig;
            device        = d3d11Device;
            unityRenderer = unityDevice;

            bounds      = new VRTextureBounds_t();
            bounds.vMin = bounds.uMin = 0;
            bounds.vMax = bounds.uMax = 1;

            var w           = rig.leftTexture.Description.Width;
            var h           = rig.leftTexture.Description.Height;
            var description = new SharpDX.Direct3D11.Texture2DDescription()
            {
                Width  = (int)w,
                Height = (int)h,

                MipLevels = 1,
                ArraySize = 1,

                SampleDescription = new SampleDescription()
                {
                    Count = 1
                },

                BindFlags      = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format         = Format.R8G8B8A8_UNorm,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.Shared,
                Usage          = SharpDX.Direct3D11.ResourceUsage.Default
            };

            System.Diagnostics.Trace.WriteLine("Creating D3D11 Textures");

            leftVRTexture  = new SharpDX.Direct3D11.Texture2D(d3d11Device, description);
            rightVRTexture = new SharpDX.Direct3D11.Texture2D(d3d11Device, description);

            leftEye = new Texture_t()
            {
                eColorSpace = EColorSpace.Gamma,
                eType       = ETextureType.DirectX,
                handle      = leftVRTexture.NativePointer
            };
            rightEye = new Texture_t()
            {
                eColorSpace = EColorSpace.Gamma,
                eType       = ETextureType.DirectX,
                handle      = rightVRTexture.NativePointer
            };


            var sharedHandleLeft  = leftVRTexture.QueryInterface <Resource>().SharedHandle;
            var sharedHandleRight = rightVRTexture.QueryInterface <Resource>().SharedHandle;

            leftHandle  = unityRenderer.OpenSharedResource <SharpDX.Direct3D11.Texture2D>(sharedHandleLeft);
            rightHandle = unityRenderer.OpenSharedResource <SharpDX.Direct3D11.Texture2D>(sharedHandleRight);

            var viewDesc = new SharpDX.Direct3D11.ShaderResourceViewDescription()
            {
                Format    = Format.R8G8B8A8_UNorm,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels       = 1,
                    MostDetailedMip = 0
                }
            };
            var leftView  = new SharpDX.Direct3D11.ShaderResourceView(unityDevice, leftHandle, viewDesc);
            var rightView = new SharpDX.Direct3D11.ShaderResourceView(unityDevice, rightHandle, viewDesc);

            leftUnityTexture  = Texture2D.CreateExternalTexture(w, h, TextureFormat.ARGB32, false, false, leftView.NativePointer);
            rightUnityTexture = Texture2D.CreateExternalTexture(w, h, TextureFormat.ARGB32, false, false, rightView.NativePointer);
        }