Ejemplo n.º 1
0
        public TextureBuffer(Wrap oculus, Hmd hmd, bool rendertarget, bool displayableOnHmd, OVRTypes.Sizei size, int mipLevels, IntPtr data, int sampleCount)
        {
            wrap = oculus;
            texSize = size;

            OVRTypes.Result result;

            Debug.Assert(sampleCount <= 1); // The code doesn't currently handle MSAA textures.

            if (displayableOnHmd)
            {
                // This texture isn't necessarily going to be a rendertarget, but it usually is.
                Debug.Assert(hmd != null); // No HMD? A little odd.

                // It looks like that in order to disable sRGB we have to
                // set the texture format to sRGB but omit the call the
                // enabling sRGB in the framebuffer. Info here:
                // https://forums.oculus.com/community/discussion/24347/srgb-and-sdk-0-6-0-0

                OVRTypes.TextureSwapChainDesc desc = new OVRTypes.TextureSwapChainDesc();
                desc.Type = OVRTypes.TextureType.Texture2D;
                desc.ArraySize = 1;
                desc.Width = size.Width;
                desc.Height = size.Height;
                desc.MipLevels = mipLevels;
                desc.Format = OVRTypes.TextureFormat.R8G8B8A8_UNORM_SRGB; //Remember to call GL.Disable(EnableCap.FramebufferSrgb) later
                desc.SampleCount = sampleCount;
                desc.StaticImage = 0;
                if (mipLevels > 1)
                    desc.MiscFlags = OVRTypes.TextureMiscFlags.AllowGenerateMips;

                result = hmd.CreateTextureSwapChainGL(desc, out textureChain);
                WriteErrorDetails(wrap, result, "Failed to create swap chain.");

                int length = 0;
                result = TextureChain.GetLength(out length);
                WriteErrorDetails(wrap, result, "Failed to retrieve the number of buffers of the created swap chain.");

                if (result >= OVRTypes.Result.Success)
                {
                    for (int i = 0; i < length; ++i)
                    {
                        uint chainTexId;

                        // Retrieve the OpenGL texture contained in the Oculus TextureSwapChainBuffer.
                        result = TextureChain.GetBufferGL(i, out chainTexId);
                        WriteErrorDetails(wrap, result, "Failed to retrieve a texture from the created swap chain.");
                        GL.BindTexture(TextureTarget.Texture2D, chainTexId);

                        if (rendertarget)
                        {
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge);
                        }
                        else
                        {
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);
                        }
                    }
                }
            }
            else
            {
                GL.GenTextures(1, out texId);
                GL.BindTexture(TextureTarget.Texture2D, texId);

                if (rendertarget)
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge);
                }
                else
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);
                }

                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Srgb8Alpha8, texSize.Width, texSize.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            }

            if (mipLevels > 1)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
            }

            GL.GenFramebuffers(1, out fboId);
        }