コード例 #1
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (!SystemInfo.supportsRayTracing || !rayTracingShader)
        {
            Debug.Log("The RayTracing API is not supported by this GPU or by the current graphics API.");
            Graphics.Blit(src, dest);
            return;
        }

        if (rayTracingAccelerationStructure == null)
        {
            return;
        }

        if (prevCameraMatrix != Camera.main.cameraToWorldMatrix)
        {
            convergenceStep = 0;
        }

        if (prevBounceCountOpaque != bounceCountOpaque)
        {
            convergenceStep = 0;
        }

        if (prevBounceCountTransparent != bounceCountTransparent)
        {
            convergenceStep = 0;
        }

        // Not really needed per frame if the scene is static.
        rayTracingAccelerationStructure.Build();

        rayTracingShader.SetShaderPass("PathTracing");

        Shader.SetGlobalInt(Shader.PropertyToID("g_BounceCountOpaque"), (int)bounceCountOpaque);
        Shader.SetGlobalInt(Shader.PropertyToID("g_BounceCountTransparent"), (int)bounceCountTransparent);

        // Input
        rayTracingShader.SetAccelerationStructure(Shader.PropertyToID("g_AccelStruct"), rayTracingAccelerationStructure);
        rayTracingShader.SetFloat(Shader.PropertyToID("g_Zoom"), Mathf.Tan(Mathf.Deg2Rad * Camera.main.fieldOfView * 0.5f));
        rayTracingShader.SetFloat(Shader.PropertyToID("g_AspectRatio"), cameraWidth / (float)cameraHeight);
        rayTracingShader.SetInt(Shader.PropertyToID("g_ConvergenceStep"), convergenceStep);
        rayTracingShader.SetInt(Shader.PropertyToID("g_FrameIndex"), Time.frameCount);
        rayTracingShader.SetTexture(Shader.PropertyToID("g_EnvTex"), envTexture);

        // Output
        rayTracingShader.SetTexture(Shader.PropertyToID("g_Radiance"), rayTracingOutput);

        rayTracingShader.Dispatch("MainRayGenShader", (int)cameraWidth, (int)cameraHeight, 1, Camera.main);

        Graphics.Blit(rayTracingOutput, dest);

        convergenceStep++;

        prevCameraMatrix           = Camera.main.cameraToWorldMatrix;
        prevBounceCountOpaque      = bounceCountOpaque;
        prevBounceCountTransparent = bounceCountTransparent;
    }
コード例 #2
0
    public void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        var cam = Camera.main;

        if (!CheckResources(cam))
        {
            Graphics.Blit(src, dest);
            return;
        }

        //-- shader pass "RTPass" in material shaders
        rtShader.SetShaderPass("RTPass");

        //- set global variables, used in all material shaders
        Shader.SetGlobalVector(ShaderID._T_minmax, new Vector2(tMin, tMax));

        //-- set local RT raygen shader variables
        _rtAccStructure.Build();
        rtShader.SetAccelerationStructure(ShaderID._SceneAccelerationStructure, _rtAccStructure);

        rtShader.SetTexture(ShaderID._RenderTarget, _rtTargetTexture);
        rtShader.SetMatrix(ShaderID._InvViewMatrix, cam.cameraToWorldMatrix);
        rtShader.SetTexture(ShaderID._EnvironmentTex, environmentCubemap);
        rtShader.SetFloat(ShaderID._EnvironmentGamma, Mathf.GammaToLinearSpace(environmentExposure));
        rtShader.SetFloat(ShaderID._ShadowBrightness, shadowBrightness);

        var camOrigin = cam.transform.position;
        var frustumBottomLeftDirWS  = (cam.ViewportToWorldPoint(new Vector3(0, 0, cam.nearClipPlane)) - camOrigin).normalized;
        var frustumBottomRightDirWS = (cam.ViewportToWorldPoint(new Vector3(1, 0, cam.nearClipPlane)) - camOrigin).normalized;
        var frustumTopLeftDirWS     = (cam.ViewportToWorldPoint(new Vector3(0, 1, cam.nearClipPlane)) - camOrigin).normalized;

        rtShader.SetVector(ShaderID._FrustumBottomLeftDirWS, frustumBottomLeftDirWS);
        rtShader.SetVector(ShaderID._FrustumHorizDirWS, frustumBottomRightDirWS - frustumBottomLeftDirWS);
        rtShader.SetVector(ShaderID._FrustumVertDirWS, frustumTopLeftDirWS - frustumBottomLeftDirWS);

        //-- dispatch ray tracing
        rtShader.Dispatch("RaygenShader", cam.pixelWidth, cam.pixelHeight, 1);
        Graphics.Blit(_rtTargetTexture, dest);
    }
コード例 #3
0
    void SetShaderProperties()
    {
        Vector3 bottomLeft  = cam.ViewportToWorldPoint(new Vector3(0, 0, 100000)).normalized;
        Vector3 topLeft     = cam.ViewportToWorldPoint(new Vector3(0, 1, 100000)).normalized;
        Vector3 bottomRight = cam.ViewportToWorldPoint(new Vector3(1, 0, 100000)).normalized;
        Vector3 topRight    = cam.ViewportToWorldPoint(new Vector3(1, 1, 100000)).normalized;

        shader.SetVector("_TopLeftFrustumDir", topLeft);
        shader.SetVector("_TopRightFrustumDir", topRight);
        shader.SetVector("_BottomLeftFrustumDir", bottomLeft);
        shader.SetVector("_BottomRightFrustumDir", bottomRight);
        shader.SetVector("_Sun", sun.transform.forward);
        shader.SetVector("_WSCP", transform.position);
        shader.SetFloat("_RayLength", rayLength);

        combine.SetTexture("_CS", cs);

        shader.SetAccelerationStructure("_RTAS", _RTAS);
        combine.SetColor("_Ambient", RenderSettings.ambientLight);
    }
コード例 #4
0
    void DispatchRays()
    {
        // update frame index and start path tracer
        _rayTracingShader.SetInt("_FrameIndex", _frameIndex);
        _rayTracingShader.SetShaderPass("DxrPass");         //

        // using e.g. ForwardDXR here will require binding all variables correctly, e.g. _EnvLightDatasRT
        // _rayTracingShader.SetShaderPass("ForwardDXR");
        // var lightCluster = HDRenderPipeline.currentPipeline.RequestLightCluster();
        // Shader.SetGlobalBuffer(HDShaderIDs._RaytracingLightCluster, lightCluster.GetCluster());
        // Shader.SetGlobalBuffer(HDShaderIDs._LightDatasRT, lightCluster.GetLightDatas());
        // Shader.SetGlobalBuffer(HDShaderIDs._EnvLightDatasRT, lightCluster.GetEnvLightDatas());

        _rayTracingShader.SetFloat("_JitterAmount", resultTexture == ResultTexture.DxrTarget ? 0 : 1);
        _rayTracingShader.SetBool("_UseLensData", lensRenderer && lensRenderer.isActiveAndEnabled);
        if (lensRenderer)
        {
            _rayTracingShader.SetMatrix("_DataProviderTransform", lensRenderer.transform.localToWorldMatrix);
            _rayTracingShader.SetTexture("_PositionData", lensRenderer.lensPositionTex);
            _rayTracingShader.SetTexture("_DirectionData", lensRenderer.lensDirectionTex);
        }
        else
        {
            _rayTracingShader.SetTexture("_PositionData", Texture2D.whiteTexture);
            _rayTracingShader.SetTexture("_DirectionData", Texture2D.whiteTexture);
        }

        // start one thread for each pixel on screen
        _rayTracingShader.Dispatch("MyRaygenShader", _dxrTarget.width, _dxrTarget.height, 1, _camera);

        // update accumulation material
        _accumulationMaterial.SetTexture("_CurrentFrame", _dxrTarget);
        _accumulationMaterial.SetTexture("_Accumulation", _accumulationTarget1);
        _accumulationMaterial.SetInt("_FrameIndex", _frameIndex++);

        // accumulate current raytracing result
        Graphics.Blit(_dxrTarget, _accumulationTarget2, _accumulationMaterial);

        // switch accumulate textures
        var temp = _accumulationTarget1;

        _accumulationTarget1 = _accumulationTarget2;
        _accumulationTarget2 = temp;

        // // apply DLSS to the _dxrTarget
        // var cmd = new CommandBuffer();
        // var initData = new DLSSCommandInitializationData()
        // {
        //  featureFlags = DLSSFeatureFlags.DoSharpening,
        //  inputRTHeight = (uint)_dxrTarget.height,
        //  inputRTWidth = (uint)_dxrTarget.width,
        //  outputRTWidth = (uint)(2 * _dxrTarget.width),
        //  outputRTHeight = (uint)(2 * _dxrTarget.height),
        //  quality = DLSSQuality.MaximumPerformance
        // };
        //
        // var context = _nvidiaDevice.CreateFeature(cmd, initData);
        // var textureTable = new DLSSTextureTable()
        // {
        //  colorInput = _dxrTarget,
        //  colorOutput = null,
        //  depth = null,
        //  motionVectors = null,
        // };
        // _nvidiaDevice.ExecuteDLSS(cmd, context, new DLSSTextureTable());

        GetComponent <MeshRenderer>().sharedMaterial.mainTexture = resultTexture == ResultTexture.DxrTarget ? _dxrTarget : _accumulationTarget1;
    }