ValveSoftware / openvr

OpenVR SDK
http://steamvr.com
BSD 3-Clause "New" or "Revised" License
6.11k stars 1.28k forks source link

TextureUsesUnsupportedFormat with OpenGL #1467

Open rkdmffud opened 4 years ago

rkdmffud commented 4 years ago

Hi. i'm just trying to make simple application that projection image to hmd directly but when i try submit image to using compositor.submit() it return error 'TextureUsesUnsupportedFormat' i used ARGB8 Format. heres my code

        TrackedDevicePose_t[] renderPoses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
        TrackedDevicePose_t[] gamePoses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
        EVRCompositorError e = compositor.WaitGetPoses(renderPoses, gamePoses);

        //=====================================================================================================================
        //initialize GL
        GL.Clear(ClearBufferMask.ColorBufferBit);
        GL.ClearColor(0, 0, 0, 1);
        GL.Enable(EnableCap.Texture2D);
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

        //=====================================================================================================================
        //Generate framebuffer and binding
        uint leftFrame;
        GL.GenFramebuffers(1, out leftFrame);
        GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, leftFrame);
        GL.Viewport(0, 0, width, height);

        //=====================================================================================================================
        //Generate texturebuffer and binding
        uint leftTex;
        GL.GenTextures(1, out leftTex);
        GL.BindTexture(TextureTarget.Texture2D, leftTex);

        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0);

        //=====================================================================================================================
        //Make image
        Bitmap LeftImage = (Bitmap)eventArgs.Frame.Clone();
        DataLeft = LeftImage.LockBits(new Rectangle(0, 0, LeftImage.Width, LeftImage.Height),
                    ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, LeftImage.Width, LeftImage.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);

        LeftImage.UnlockBits(DataLeft);

        //=====================================================================================================================
        //Attach texture to framebuffer
        GL.FramebufferTexture2D(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, leftTex, 0);

        //=====================================================================================================================
        //Generate resolve framebuffer
        uint leftFrameRe;
        GL.GenFramebuffers(1, out leftFrameRe);
        GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, leftFrameRe);
        GL.BlitFramebuffer(0, 0, width, height, 0, 0, width, height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);

        //=====================================================================================================================
        //Unbind buffers
        GL.BindTexture(TextureTarget.Texture2D, 0);
        GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, 0);
        GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);

        //=====================================================================================================================
        //Make texture for openvr and submit to compositor
        VRTextureBounds_t bound = new VRTextureBounds_t() { uMax = 1, vMax = 1 };
        Texture_t left = new Texture_t() { handle = (IntPtr)leftFrameRe, eType = ETextureType.OpenGL, eColorSpace = EColorSpace.Gamma };
        EVRCompositorError e1 = compositor.Submit(EVREye.Eye_Left, ref left, ref bound, EVRSubmitFlags.Submit_Default);
        EVRCompositorError e2 = compositor.Submit(EVREye.Eye_Right, ref left, ref bound, EVRSubmitFlags.Submit_Default);

        if(!e1.Equals(EVRApplicationError.None))
        {
            Debug.Print(e1.ToString());
        }

        compositor.PostPresentHandoff();
aleiby commented 3 years ago

Try using one of these formats:

GL_RGBA16
GL_RGBA16F
GL_RGB16F
GL_RGBA8
GL_SRGB8
GL_SRGB8_ALPHA8
GL_RGB8
GL_RGBA