Example #1
0
        /// <summary>
        /// Gets the number of buffers in the TextureSwapChain.
        /// </summary>
        /// <param name="length">Returns the number of buffers in the specified chain.</param>
        /// <returns>Returns an ovrResult for which the return code is negative upon error. </returns>
        public Result GetLength(out int length)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("TextureSwapChain");
            }

            return(_ovr.GetTextureSwapChainLength(_sessionPtr, TextureSwapChainPtr, out length));
        }
        public OculusTextureSwapChain(OvrWrap ovr,
                                      IntPtr sessionPtr,
                                      SharpDX.Direct3D11.Device device,
                                      EyeType eye,
                                      Format format,
                                      Sizei size,
                                      bool createDepthStencilView = false,
                                      bool isDebugDevice          = false)
        {
            _ovr          = ovr;
            _sessionPtr   = sessionPtr;
            _size         = size;
            _viewportSize = size;
            _viewport     = new ViewportF(0.0f, 0.0f, (float)size.Width, (float)size.Height);

            Format srgbFormat = GetSRgbFormat(format);

            TextureFormat        textureFormat = SharpDXHelpers.GetTextureFormat(srgbFormat);
            TextureSwapChainDesc swapChainDesc = new TextureSwapChainDesc()
            {
                ArraySize   = 1,
                BindFlags   = TextureBindFlags.DX_RenderTarget,
                Format      = textureFormat,
                Height      = _size.Height,
                MipLevels   = 1,
                MiscFlags   = TextureMiscFlags.DX_Typeless,
                SampleCount = 1,
                Width       = _size.Width
            };

            Texture2DDescription description1 = new Texture2DDescription()
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.R24G8_Typeless,
                Height            = _size.Height,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                Width             = _size.Width
            };

            ShaderResourceViewDescription description2 = new ShaderResourceViewDescription()
            {
                Format    = srgbFormat,
                Dimension = ShaderResourceViewDimension.Texture2D
            };

            description2.Texture2D.MipLevels = 1;

            // Create a texture swap chain, which will contain the textures to render to, for the current eye.
            var result = _ovr.CreateTextureSwapChainDX(_sessionPtr, device.NativePointer, ref swapChainDesc, out _textureSwapChainPtr);

            if (result < Ab3d.OculusWrap.Result.Success)
            {
                var lastError = _ovr.GetLastErrorInfo();
                throw new OvrException("Error creating Oculus TextureSwapChain: " + lastError.ErrorString, lastError.Result);
            }


            int length;

            result = _ovr.GetTextureSwapChainLength(_sessionPtr, _textureSwapChainPtr, out length);

            if (result < Ab3d.OculusWrap.Result.Success)
            {
                var lastError = _ovr.GetLastErrorInfo();
                throw new OvrException("Failed to retrieve the number of buffers of the created swap chain: " + lastError.ErrorString, lastError.Result);
            }


            _textures = new TextureItem[length];

            for (int index = 0; index < length; ++index)
            {
                IntPtr bufferPtr;
                result = _ovr.GetTextureSwapChainBufferDX(_sessionPtr, _textureSwapChainPtr, index, typeof(Texture2D).GUID, out bufferPtr);

                if (result < Ab3d.OculusWrap.Result.Success)
                {
                    var lastError = _ovr.GetLastErrorInfo();
                    throw new OvrException("Failed to retrieve a texture from the created swap chain: " + lastError.ErrorString, lastError.Result);
                }

                Texture2D        texture2D1       = new Texture2D(bufferPtr);
                Texture2D        texture2D2       = null;
                DepthStencilView depthStencilView = null;

                if (createDepthStencilView)
                {
                    texture2D2       = new Texture2D(device, description1);
                    depthStencilView = new DepthStencilView(device, texture2D2, new DepthStencilViewDescription()
                    {
                        Flags     = DepthStencilViewFlags.None,
                        Dimension = DepthStencilViewDimension.Texture2D,
                        Format    = Format.D24_UNorm_S8_UInt
                    });
                }

                _textures[index] = new TextureItem()
                {
                    Texture            = texture2D1,
                    TextureDescription = texture2D1.Description,
                    DepthBuffer        = texture2D2,
                    DepthStencilView   = depthStencilView,
                    RTView             = new RenderTargetView(device, texture2D1, new RenderTargetViewDescription()
                    {
                        Format    = format,
                        Dimension = RenderTargetViewDimension.Texture2D
                    }),
                    SRView = new ShaderResourceView(device, texture2D1, description2)
                };


                if (isDebugDevice)
                {
                    var eyeTextAndIndex = eye.ToString() + index.ToString();

                    _textures[index].Texture.DebugName          = "OculusBackBuffer" + eyeTextAndIndex;
                    _textures[index].RTView.DebugName           = "OculusRT" + eyeTextAndIndex;
                    _textures[index].SRView.DebugName           = "OculusSR" + eyeTextAndIndex;
                    _textures[index].DepthBuffer.DebugName      = "OculusDepthBuffer" + eyeTextAndIndex;
                    _textures[index].DepthStencilView.DebugName = "OculusDepthStencilView" + eyeTextAndIndex;
                }
            }
        }