Skip to content

Commit

Permalink
[RPW][HDRP]Multiple fixes on the Rendering Debugger
Browse files Browse the repository at this point in the history
This PR aims to solve multiple issues with the Rendering Debugger and it's implementation on HDRP.

- [[HDRP] Entering Playmode with any Rendering Debugger mode on resets all debug view](https://fogbugz.unity3d.com/f/cases/1401599/)

Partially solving this issue, as the current limitations of the framework do not allow how the HDRP is using it. There is a splitted enum across multiple properties that when it is being set, multiple fields are reset or changed. For those to work properly when entering playmode, the enum must be splitted, and as talked with @julien this will be handled by HDRP team.

- [[HDRP] Undo action name quickly changes after Rendering Debugger UI is rebuilt](https://fogbugz.unity3d.com/f/cases/1412467/)
- [[HDRP] Some Rendering Debug actions are named as HDR: true in undo stack](https://fogbugz.unity3d.com/f/cases/1412465/)

The GetReset Method only destroys the IDebugData, but doesnt call the RegisterDebug UI from HDRP, as the new system is not reliying on the recreation of the UI to hide( not add) or show ( add) widgets, this is working out of the box correctly.
- [[HDRP] Reset button in Rendering Debugger doesn't update widget visibility](https://fogbugz.unity3d.com/f/cases/1411399/)

The main issue with this fogbugz was that all the implementation on the Rendering Debugger on HDRP is registering to the event `onValueChanged` as when it was implemented the callback `isHiddenCallback `did not exist.

- Drawers code for editor has been improved and shared.
  • Loading branch information
alex-vazquez-unity3d committed Apr 22, 2022
1 parent 252d5a6 commit cdaeaa8
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 648 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public override void SetValue(object value, DebugUI.IValueField field)
/// </summary>
public override void OnEnable()
{
base.OnEnable();

if (m_EnumField == null)
return;

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,55 @@ public override void End(DebugUI.Widget widget, DebugState state)
}
}
}

/// <summary>
/// Common class to help drawing widgets
/// </summary>
/// <typeparam name="TWidget">The widget</typeparam>
public abstract class DebugUIWidgetDrawer<TWidget> : DebugUIDrawer
where TWidget : DebugUI.Widget
{
/// <summary>
/// Implement this to execute processing before UI rendering.
/// </summary>
/// <param name="widget">Widget that is going to be rendered.</param>
/// <param name="state">Debug State associated with the Debug Item.</param>
public override void Begin(DebugUI.Widget widget, DebugState state)
{
}

/// <summary>
/// Implement this to execute UI rendering.
/// </summary>
/// <param name="widget">Widget that is going to be rendered.</param>
/// <param name="state">Debug State associated with the Debug Item.</param>
/// <returns>Returns the state of the widget.</returns>
public override bool OnGUI(DebugUI.Widget widget, DebugState state)
{
DoGUI(
PrepareControlRect(),
EditorGUIUtility.TrTextContent(widget.displayName, widget.tooltip),
Cast<TWidget>(widget)
);

return true;
}

/// <summary>
/// Does the field of the given type
/// </summary>
/// <param name="rect">The rect to draw the field</param>
/// <param name="label">The label for the field</param>
/// <param name="w">The widget</param>
protected abstract void DoGUI(Rect rect, GUIContent label, TWidget w);

/// <summary>
/// Implement this to execute processing after UI rendering.
/// </summary>
/// <param name="widget">Widget that is going to be rendered.</param>
/// <param name="state">Debug State associated with the Debug Item.</param>
public override void End(DebugUI.Widget widget, DebugState state)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using UnityEditor.Callbacks;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Rendering;

namespace UnityEditor.Rendering
Expand Down Expand Up @@ -203,6 +204,26 @@ public void DestroyWidgetStates()
m_WidgetStates.Clear();
}

public void ReloadWidgetStates()
{
if (m_WidgetStates == null)
return;

// Clear all the states from memory
foreach (var state in m_WidgetStates)
{
var widget = DebugManager.instance.GetItem(state.Key);
if (widget == null)
{
var s = state.Value;
Undo.ClearUndo(s); // Don't leave dangling states in the global undo/redo stack
DestroyImmediate(s);
}
}

UpdateWidgetStates();
}

bool AreWidgetStatesValid()
{
foreach (var state in m_WidgetStates)
Expand Down Expand Up @@ -246,15 +267,13 @@ void UpdateWidgetStates(DebugUI.IContainer container)
if (widget.isInactiveInEditor)
return;

var widgetType = widget.GetType();
string guid = widget.queryPath;
s_WidgetStateMap.TryGetValue(widgetType, out Type stateType);

// Create missing states & recreate the ones that are null
if (stateType != null)
if (!m_WidgetStates.TryGetValue(guid, out var state) || state == null)
{
if (!m_WidgetStates.ContainsKey(guid) || m_WidgetStates[guid] == null)
var widgetType = widget.GetType();
if (s_WidgetStateMap.TryGetValue(widgetType, out Type stateType))
{
Assert.IsNotNull(stateType);
var inst = (DebugState)CreateInstance(stateType);
inst.queryPath = guid;
inst.SetValue(valueField.GetValue(), valueField);
Expand Down Expand Up @@ -325,7 +344,8 @@ void Update()
// some debug values need to be refresh/recreated as well (e.g. frame settings on HD)
if (DebugManager.instance.refreshEditorRequested)
{
DestroyWidgetStates();
ReloadWidgetStates();
m_IsDirty = true;
DebugManager.instance.refreshEditorRequested = false;
}

Expand Down Expand Up @@ -517,9 +537,7 @@ void OnWidgetGUI(DebugUI.Widget widget)

if (drawer.OnGUI(widget, state))
{
var container = widget as DebugUI.IContainer;

if (container != null)
if (widget is DebugUI.IContainer container)
TraverseContainerGUI(container);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using NUnit.Framework;
using System;
using UnityEngine;

namespace UnityEditor.Rendering.Tests
{
partial class RenderingDebuggerTests
{
private static void PerformUndoRedoGeneric<TDebugState, T>(UnityEngine.Rendering.DebugUI.IValueField field, T defaultValue, T valueToSet)
where TDebugState : DebugState<T>, new()
{
DebugState<T> state = ScriptableObject.CreateInstance<TDebugState>();
state.SetValue(defaultValue, field);

Undo.RecordObject(state, nameof(PerformUndoRedoGeneric));
state.SetValue(valueToSet, field);

Undo.PerformUndo();
Assert.AreEqual(defaultValue, state.value);

Undo.PerformRedo();
Assert.AreEqual(valueToSet, state.value);
}

public class BitFieldTests<T> : UnityEngine.Rendering.DebugUI.BitField
{
public BitFieldTests()
{
enumType = typeof(T);
}
}

public class AutoEnumFieldTest : UnityEngine.Rendering.DebugUI.EnumField
{
public AutoEnumFieldTest()
{
autoEnum = typeof(LightType);
}
}

public class PathEnumFieldTest : UnityEngine.Rendering.DebugUI.EnumField
{
public PathEnumFieldTest()
{
enumNames = new GUIContent[] { new GUIContent("Item1"), new GUIContent("Catergory/Item1"), new GUIContent("Catergory/Item2") };
enumValues = new int[] { 0, 2, 4 };
}
}

static TestCaseData[] s_TestCaseDatas =
{
new TestCaseData(typeof(DebugStateEnum), typeof(UnityEngine.Rendering.DebugUI.EnumField), typeof(int), 0, 2)
.SetName("Undo/Redo works for Enums"),
new TestCaseData(typeof(DebugStateEnum), typeof(AutoEnumFieldTest), typeof(int), LightType.Disc, LightType.Spot)
.SetName("Undo/Redo works for Auto Enums"),
new TestCaseData(typeof(DebugStateEnum), typeof(PathEnumFieldTest), typeof(int), 0, 4)
.SetName("Undo/Redo works for Path Enums"),
new TestCaseData(typeof(DebugStateBool), typeof(UnityEngine.Rendering.DebugUI.BoolField), typeof(bool), false, true)
.SetName("Undo/Redo works for Booleans"),
new TestCaseData(typeof(DebugStateColor), typeof(UnityEngine.Rendering.DebugUI.ColorField), typeof(Color), Color.green, Color.red)
.SetName("Undo/Redo works for Colors"),
new TestCaseData(typeof(DebugStateFloat), typeof(UnityEngine.Rendering.DebugUI.FloatField), typeof(float), 1.0f, 5.0f)
.SetName("Undo/Redo works for Floats"),
new TestCaseData(typeof(DebugStateInt), typeof(UnityEngine.Rendering.DebugUI.IntField), typeof(int), -1, 5)
.SetName("Undo/Redo works for integers"),
new TestCaseData(typeof(DebugStateUInt), typeof(UnityEngine.Rendering.DebugUI.UIntField), typeof(uint), 1u, 5u)
.SetName("Undo/Redo works for unsigned integers"),
new TestCaseData(typeof(DebugStateVector2), typeof(UnityEngine.Rendering.DebugUI.Vector2Field), typeof(Vector2), Vector2.zero, Vector2.up)
.SetName("Undo/Redo works for vector 2"),
new TestCaseData(typeof(DebugStateVector3), typeof(UnityEngine.Rendering.DebugUI.Vector3Field), typeof(Vector3), Vector3.zero, Vector3.up)
.SetName("Undo/Redo works for vector 3"),
new TestCaseData(typeof(DebugStateVector4), typeof(UnityEngine.Rendering.DebugUI.Vector4Field), typeof(Vector4), Vector4.zero, Vector4.one)
.SetName("Undo/Redo works for vector 4"),
new TestCaseData(typeof(DebugStateFlags), typeof(BitFieldTests<UnityEngine.Rendering.DebugUI.Flags>), typeof(Enum), UnityEngine.Rendering.DebugUI.Flags.EditorOnly, UnityEngine.Rendering.DebugUI.Flags.RuntimeOnly)
.SetName("Undo/Redo works for bit fields"),
};

[Test, TestCaseSource(nameof(s_TestCaseDatas))]
public void PerformUndoRedo(Type debugStateType, Type widgetType, Type innerType, object defaultValue, object changeValue)
{
var widget = Activator.CreateInstance(widgetType);
var performUndoRedoGeneric = GetType()
.GetMethod(nameof(PerformUndoRedoGeneric), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.MakeGenericMethod(new[] { debugStateType, innerType } );

object[] args = new object[] { widget, defaultValue, changeValue };
performUndoRedoGeneric.Invoke(null, args);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdaeaa8

Please sign in to comment.