예제 #1
0
 public IntPtr GetDepthSwapchainForCamera(VarjoViewCamera.CAMERA_ID cameraId)
 {
     // Ensure it's created
     GetRenderTextureForCamera(cameraId);
     return(camDepthSwapchains[(int)cameraId]);
 }
예제 #2
0
        /// <summary>
        /// Gets a render texture for a camera with correct dimensions and format.
        /// Creates a texture if it's not created. VarjoManager takes care of texture destruction.
        /// </summary>
        /// <param name="cameraId">Camera Id</param>
        /// <returns>Render texture</returns>
        public RenderTexture GetRenderTextureForCamera(VarjoViewCamera.CAMERA_ID cameraId)
        {
            Profiler.BeginSample("Varjo.GetRenderTextureForCamera");
            if (!VarjoPlugin.SessionValid)
            {
                Debug.LogError("GetRenderTextureForCamera called without a valid session.");
                Profiler.EndSample();
                return(null);
            }

            var texDesc = VarjoPlugin.GetRenderTextureFormat((int)cameraId);

            if (texDesc.width <= 0 || texDesc.height <= 0)
            {
                Debug.Log(string.Format("Invalid texture descriptor: {0}x{1} {2}", texDesc.width, texDesc.height, texDesc.format));
                Profiler.EndSample();
                return(null);
            }

            int camIndex = (int)cameraId;

            if (camRenderTextures[camIndex] != null)
            {
                Profiler.EndSample();
                return(camRenderTextures[camIndex]);
            }

            // TODO: framebuffer dimensions can change per frame.

            Debug.Log(string.Format("Creating render target: {0}x{1}", texDesc.width, texDesc.height));
            RenderTextureDescriptor rtd = new RenderTextureDescriptor()
            {
                width           = texDesc.width,
                height          = texDesc.height,
                colorFormat     = ConvertPluginRenderTextureFormat(texDesc.format),
                depthBufferBits = 32,
                dimension       = TextureDimension.Tex2D,
                volumeDepth     = 1,
                msaaSamples     = 1, // TODO: get the correct MSAA
                sRGB            = true,
            };

            camRenderTextures[camIndex] = new RenderTexture(rtd);
            camRenderTextures[camIndex].Create();
            // Also create the swapchain
            var cfg = new VarjoPlugin.SwapchainConfig()
            {
                format           = VarjoPlugin.varjo_TextureFormat_R8G8B8A8_SRGB,
                numberOfTextures = 4,
                textureWidth     = texDesc.width,
                textureHeight    = texDesc.height,
                arraySize        = 1
            };

            camSwapchains[camIndex] = VarjoPlugin.CreateD3D11Swapchain(cfg);
            // And depth swapchain if requested
            if (submitDepth)
            {
                cfg.format = VarjoPlugin.varjo_DepthTextureFormat_D32_FLOAT;
                camDepthSwapchains[camIndex] = VarjoPlugin.CreateD3D11Swapchain(cfg);
            }

            // Hook up the texture directly to the view struct so that we don't have to call GetNativeTexturePtr() every frame
            var view = submitLayer.views[camIndex];

            view.unityColorTex = camRenderTextures[camIndex].GetNativeTexturePtr();
            if (submitDepth)
            {
                view.unityDepthTex = camRenderTextures[camIndex].GetNativeDepthBufferPtr();
            }

            submitLayer.views[camIndex] = view;

            Profiler.EndSample();
            return(camRenderTextures[camIndex]);
        }