Skip to content
Permalink
Browse files

Initial commit for the Realtime raytracing support

  • Loading branch information...
anisunity committed Sep 14, 2018
1 parent 60c8f87 commit 4552c5acce0a919abf5a644771b00bb660b85d89
@@ -102,6 +102,16 @@ static void Drawer_SectionPrimarySettings(RenderPipelineSettingsUI s, Serialized
EditorGUILayout.PropertyField(d.supportRuntimeDebugDisplay, _.GetContent("Support runtime debug display|Remove all debug display shader variant only in the player. Allow faster build."));
EditorGUILayout.PropertyField(d.supportDitheringCrossFade, _.GetContent("Support dithering cross fade|Remove all dithering cross fade shader variant only in the player. Allow faster build."));

// Only display the support ray tracing feature if the platform supports it
if(UnityEngine.SystemInfo.supportsRayTracing)
{
EditorGUILayout.PropertyField(d.supportRayTracing, _.GetContent("Support Realtime Raytracing."));
}
else
{
d.supportRayTracing.boolValue = false;
}

--EditorGUI.indentLevel;
}
}
@@ -26,6 +26,7 @@ class SerializedRenderPipelineSettings
public SerializedProperty supportMotionVectors;
public SerializedProperty supportRuntimeDebugDisplay;
public SerializedProperty supportDitheringCrossFade;
public SerializedProperty supportRayTracing;

public SerializedGlobalLightLoopSettings lightLoopSettings;
public SerializedShadowInitParameters shadowInitParams;
@@ -53,6 +54,7 @@ public SerializedRenderPipelineSettings(SerializedProperty root)
supportMotionVectors = root.Find((RenderPipelineSettings s) => s.supportMotionVectors);
supportRuntimeDebugDisplay = root.Find((RenderPipelineSettings s) => s.supportRuntimeDebugDisplay);
supportDitheringCrossFade = root.Find((RenderPipelineSettings s) => s.supportDitheringCrossFade);
supportRayTracing = root.Find((RenderPipelineSettings s) => s.supportRayTracing);

lightLoopSettings = new SerializedGlobalLightLoopSettings(root.Find((RenderPipelineSettings s) => s.lightLoopSettings));
shadowInitParams = new SerializedShadowInitParameters(root.Find((RenderPipelineSettings s) => s.shadowInitParams));
@@ -0,0 +1,18 @@
using UnityEngine;
using System.Collections.Generic;

namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[DisallowMultipleComponent, ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class HDRayTracingGraph : MonoBehaviour
{
[HideInInspector]
const int currentVersion = 1;

#if ENABLE_RAYTRACING
// Culling mask that defines the layers that this acceleration structure
public LayerMask layermask = -1;
#endif
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,133 @@
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections.Generic;

namespace UnityEngine.Experimental.Rendering.HDPipeline
{
#if ENABLE_RAYTRACING
public class HDRaytracingManager
{
public class HDRayTracingSubScene
{
public LayerMask mask = -1;
public RaytracingAccelerationStructure accelerationStructure = null;
public List<Renderer> targetRenderers = null;
public List<HDRayTracingGraph> referenceGraphs = new List<HDRayTracingGraph>();
}

Dictionary<int, HDRayTracingSubScene> m_subScenes = null;
List<int> m_layerMasks = null;

public HDRaytracingManager()
{
}

public void InitAccelerationStructures()
{
// Create the sub-scene structure
m_subScenes = new Dictionary<int, HDRayTracingSubScene>();

// Grab all the ray tracing graphs in the scene
HDRayTracingGraph[] tracingGraphs = UnityEngine.Object.FindObjectsOfType<HDRayTracingGraph>();

m_layerMasks = new List<int>();
// Build an array with all the layer combinations that are requested
for (var graphIndex = 0; graphIndex < tracingGraphs.Length; graphIndex++)
{
// Grab the current graph
HDRayTracingGraph currentGraph = tracingGraphs[graphIndex];

// Fetch or create the sub-scene
HDRayTracingSubScene currentSubScene = null;
if (!m_subScenes.TryGetValue(currentGraph.layermask.value, out currentSubScene))
{
currentSubScene = new HDRayTracingSubScene();
currentSubScene.mask = currentGraph.layermask.value;
m_subScenes.Add(currentGraph.layermask.value, currentSubScene);
m_layerMasks.Add(currentGraph.layermask.value);
}

// Mark the current graph for a reference
currentSubScene.referenceGraphs.Add(currentGraph);
}

// Create all the ray tracing
for (var subSceneIndex = 0; subSceneIndex < m_layerMasks.Count; subSceneIndex++)
{
HDRayTracingSubScene currentSubScene = m_subScenes[m_layerMasks[subSceneIndex]];
BuildSubSceneStructure(ref currentSubScene);
}
}

public void ReleaseAccelerationStructures()
{
for (var subSceneIndex = 0; subSceneIndex < m_layerMasks.Count; subSceneIndex++)
{
HDRayTracingSubScene currentSubScene = m_subScenes[m_layerMasks[subSceneIndex]];
DestroySubSceneStructure(ref currentSubScene);
}
}

public void DestroySubSceneStructure(ref HDRayTracingSubScene subScene)
{
if (subScene.accelerationStructure != null)
{
for (var i = 0; i < subScene.targetRenderers.Count; i++)
{
subScene.accelerationStructure.RemoveInstance(subScene.targetRenderers[i]);
}
subScene.accelerationStructure.Dispose();
subScene.targetRenderers = null;
subScene.accelerationStructure = null;
}
}

public void BuildSubSceneStructure(ref HDRayTracingSubScene subScene)
{
// Destroy the acceleration structure
subScene.targetRenderers = new List<Renderer>();

// Create the acceleration structure
subScene.accelerationStructure = new RaytracingAccelerationStructure();

// Grab all the renderers from the scene
var rendererArray = UnityEngine.GameObject.FindObjectsOfType<Renderer>();
for (var i = 0; i < rendererArray.Length; i++)
{
// Convert the object's layer to an int
int objectLayerValue = 1 << rendererArray[i].gameObject.layer;

// Is this object in one of the allowed layers ?
if ((objectLayerValue & subScene.mask.value) != 0)
{
// Add this fella to the renderer list
subScene.targetRenderers.Add(rendererArray[i]);
}
}

// If any object build the acceleration structure
if (subScene.targetRenderers.Count != 0)
{
for (var i = 0; i < subScene.targetRenderers.Count; i++)
{
// Add it to the acceleration structure
subScene.accelerationStructure.AddInstance(subScene.targetRenderers[i]);
}
}

// build the acceleration structure
subScene.accelerationStructure.Build();
}

public RaytracingAccelerationStructure RequestAccelerationStructure(LayerMask layerMask)
{
HDRayTracingSubScene currentSubScene = null;
if (m_subScenes.TryGetValue(layerMask.value, out currentSubScene))
{
return currentSubScene.accelerationStructure;
}
return null;
}
}
#endif
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -68,6 +68,10 @@ public bool IsInternalDiffusionProfile(DiffusionProfileSettings profile)
readonly SubsurfaceScatteringManager m_SSSBufferManager = new SubsurfaceScatteringManager();
readonly SharedRTManager m_SharedRTManager = new SharedRTManager();

#if ENABLE_RAYTRACING
readonly HDRaytracingManager m_RayTracingManager = new HDRaytracingManager();
#endif

// Renderer Bake configuration can vary depends on if shadow mask is enabled or no
RendererConfiguration m_currentRendererConfigurationBakedLighting = HDUtils.k_RendererConfigurationBakedLighting;
Material m_CopyStencilForNoLighting;
@@ -216,6 +220,11 @@ public HDRenderPipeline(HDRenderPipelineAsset asset)

m_Asset = asset;

#if UNITY_EDITOR
// The first thing we need to do is to set the defines that depend on the render pipeline settings
m_Asset.EvaluateSettings();
#endif

// Upgrade the resources (re-import every references in RenderPipelineResources) if the resource version mismatches
// It's done here because we know every HDRP assets have been imported before
UpgradeResourcesIfNeeded();
@@ -304,6 +313,10 @@ public HDRenderPipeline(HDRenderPipelineAsset asset)

// Propagate it to the debug menu
m_DebugDisplaySettings.msaaSamples = m_MSAASamples;

#if ENABLE_RAYTRACING
m_RayTracingManager.InitAccelerationStructures();
#endif
}

void UpgradeResourcesIfNeeded()
@@ -552,6 +565,9 @@ public override void Dispose()

base.Dispose();

#if ENABLE_RAYTRACING
m_RayTracingManager.ReleaseAccelerationStructures();
#endif
m_DebugDisplaySettings.UnregisterDebug();

m_LightLoop.Cleanup();
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine.Serialization;

namespace UnityEngine.Experimental.Rendering.HDPipeline
@@ -221,5 +222,51 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
m_Version = currentVersion;
}
}

#if UNITY_EDITOR
// Array structure that allow us to manipulate the set of defines that the HD render pipeline needs
List<string> defineArray = new List<string>();

bool UpdateDefineList(bool flagValue, string defineMacroValue)
{
bool macroExists = defineArray.Contains(defineMacroValue);
if (flagValue)
{
if (!macroExists)
{
defineArray.Add(defineMacroValue);
return true;
}
}
else
{
if (macroExists)
{
defineArray.Remove(defineMacroValue);
return true;
}
}
return false;
}

// This function allows us to raise or remove some preprocessing defines based on the render pipeline settings
public void EvaluateSettings()
{
// Grab the current set of defines and split them
string currentDefineList = UnityEditor.PlayerSettings.GetScriptingDefineSymbolsForGroup(UnityEditor.BuildTargetGroup.Standalone);
defineArray.Clear();
defineArray.AddRange(currentDefineList.Split(';'));

// Update all the individual defines
bool needUpdate = false;
needUpdate |= UpdateDefineList(renderPipelineSettings.supportRayTracing, "ENABLE_RAYTRACING");

// Only set if it changed
if(needUpdate)
{
UnityEditor.PlayerSettings.SetScriptingDefineSymbolsForGroup(UnityEditor.BuildTargetGroup.Standalone, string.Join(";", defineArray));
}
}
}
#endif
}
@@ -40,6 +40,7 @@ public class RenderPipelineSettings
public bool supportMotionVectors = true;
public bool supportRuntimeDebugDisplay = true;
public bool supportDitheringCrossFade = true;
public bool supportRayTracing = false;

public XRGraphicsConfig xrConfig = XRGraphicsConfig.s_DefaultXRConfig;

0 comments on commit 4552c5a

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.