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; }
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()); // start one thread for each pixel on screen _rayTracingShader.Dispatch("MyRaygenShader", _camera.pixelWidth, _camera.pixelHeight, 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; GetComponent <MeshRenderer>().sharedMaterial.mainTexture = resultTexture == ResultTexture.DxrTarget ? _dxrTarget : _accumulationTarget1; }
void OnRenderImage(RenderTexture source, RenderTexture destination) { PathTracingShader.SetInt("_Frame", _Frame); PathTracingShader.Dispatch("RayGenerationShader", _Camera.pixelWidth, _Camera.pixelHeight, 1, _Camera); _Material.SetTexture("_MainImage", _RenderTarget0); _Material.SetTexture("_Accumulation", _RenderTarget1); _Material.SetInt("_Frame", _Frame++); Graphics.Blit(_RenderTarget0, _RenderTarget2, _Material); Graphics.Blit(_RenderTarget2, destination); var target = _RenderTarget1; _RenderTarget1 = _RenderTarget2; _RenderTarget2 = target; }
private void OnRenderImage(RenderTexture source, RenderTexture destination) { // update frame index and start path tracer _rayTracingShader.SetInt("_FrameIndex", _frameIndex); // start one thread for each pixel on screen _rayTracingShader.Dispatch("MyRaygenShader", _camera.pixelWidth, _camera.pixelHeight, 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); // display result on screen Graphics.Blit(_accumulationTarget2, destination); // switch accumulate textures var temp = _accumulationTarget1; _accumulationTarget1 = _accumulationTarget2; _accumulationTarget2 = temp; }
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; }