Skip to content

Commit

Permalink
feat: add group id to share the bake buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 2, 2023
1 parent 75a6374 commit 53f03a4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
19 changes: 19 additions & 0 deletions Packages/src/Editor/CompositeCanvasRendererEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace CompositeCanvas
[CanEditMultipleObjects]
public class CompositeCanvasRendererEditor : GraphicEditor
{
private static readonly GUIContent s_ContentPrimaryRenderer = new GUIContent("Primary Renderer");
private static readonly GUIContent s_ContentNone = new GUIContent("None");
private static readonly GUIContent s_ContentEffect = new GUIContent("Effect");
private static readonly GUIContent s_ContentAttachedEffect = new GUIContent("Attached Effect");
Expand All @@ -33,6 +34,7 @@ public class CompositeCanvasRendererEditor : GraphicEditor
private SerializedProperty _extents;
private SerializedProperty _foreground;
private Editor _materialEditor;
private SerializedProperty _sharingGroupId;
private SerializedProperty _showSourceGraphics;
private SerializedProperty _srcBlendMode;
private SerializedProperty _useCanvasScaler;
Expand All @@ -47,6 +49,7 @@ protected override void OnEnable()
_showSourceGraphics = serializedObject.FindProperty("m_ShowSourceGraphics");
_downSamplingRate = serializedObject.FindProperty("m_DownSamplingRate");
_useCanvasScaler = serializedObject.FindProperty("m_UseCanvasScaler");
_sharingGroupId = serializedObject.FindProperty("m_SharingGroupId");
_foreground = serializedObject.FindProperty("m_Foreground");
_extents = serializedObject.FindProperty("m_Extents");
_culling = serializedObject.FindProperty("m_Culling");
Expand Down Expand Up @@ -84,6 +87,8 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_useCanvasScaler);
EditorGUILayout.PropertyField(_extents);
EditorGUILayout.PropertyField(_useStencil);
EditorGUILayout.PropertyField(_sharingGroupId);
ShowPrimaryRenderer();

// Baking Settings
EditorGUILayout.PropertyField(_bakingTrigger);
Expand Down Expand Up @@ -127,6 +132,20 @@ public override void OnInspectorGUI()
}
}

private void ShowPrimaryRenderer()
{
if (_sharingGroupId.intValue == 0) return;

EditorGUI.indentLevel++;
EditorGUI.showMixedValue = _sharingGroupId.hasMultipleDifferentValues;
EditorGUI.BeginDisabledGroup(true);
var obj = CompositeCanvasRenderer.GetFirstGroupedRenderer(_sharingGroupId.intValue);
EditorGUILayout.ObjectField(s_ContentPrimaryRenderer, obj, typeof(CompositeCanvasRenderer), true);
EditorGUI.EndDisabledGroup();
EditorGUI.showMixedValue = false;
EditorGUI.indentLevel--;
}

private void ShowSourceGraphicsControlGUI()
{
EditorGUI.BeginChangeCheck();
Expand Down
61 changes: 55 additions & 6 deletions Packages/src/Runtime/CompositeCanvasRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class CompositeCanvasRenderer : MaskableGraphic

private static readonly VertexHelper s_VertexHelper = new VertexHelper();

private static readonly LinkedList<CompositeCanvasRenderer> s_ActiveRenderers =
new LinkedList<CompositeCanvasRenderer>();

[SerializeField]
[Header("Buffer")]
[Tooltip("Down sampling rate for baking.\n" +
Expand All @@ -44,6 +47,11 @@ public class CompositeCanvasRenderer : MaskableGraphic
"If false, the bake-buffer is the same size as the rendering size.")]
private bool m_UseCanvasScaler = true;

[Tooltip("Bake buffer sharing group ID.\n" +
"If non-zero is specified, the baked buffer are shared within the group.")]
[SerializeField]
private int m_SharingGroupId;

[SerializeField]
[Tooltip("View type to bake.\n" +
"Automatic: Use orthographic space to bake if possible.\n" +
Expand Down Expand Up @@ -218,6 +226,21 @@ public bool useCanvasScaler
}
}

/// <summary>
/// Bake buffer sharing group ID.
/// If non-zero is specified, the baked buffer are shared within the group.
/// </summary>
public int sharingGroupId
{
get => m_SharingGroupId;
set
{
if (m_SharingGroupId == value) return;
m_SharingGroupId = value;
SetDirty();
}
}

/// <summary>
/// Down sampling rate for baking.
/// The higher this value, the lower the resolution of the bake, but the performance will improve.
Expand Down Expand Up @@ -301,10 +324,11 @@ public override Texture mainTexture
}

var rate = (int)downSamplingRate;
return TemporaryRenderTexture.Get(size, rate, ref _bakeBuffer, useStencil);
var id = sharingGroupId == 0 ? GetInstanceID() : sharingGroupId;
return RenderTextureRepository.Get(id, size, rate, ref _bakeBuffer, useStencil);
}

TemporaryRenderTexture.Release(ref _bakeBuffer);
RenderTextureRepository.Release(ref _bakeBuffer);
return null;
}
}
Expand Down Expand Up @@ -463,20 +487,24 @@ protected override void OnEnable()
{
SetDirty();
}

s_ActiveRenderers.AddLast(this);
}

/// <summary>
/// This function is called when the behaviour becomes disabled.
/// </summary>
protected override void OnDisable()
{
s_ActiveRenderers.Remove(this);

UIExtraCallbacks.onBeforeCanvasRebuild -= _checkTransformChanged;
UIExtraCallbacks.onAfterCanvasRebuild -= _bake;

isDirty = false;
s_CommandBufferPool.Return(ref _cb);
TemporaryRenderTexture.Release(ref _bakeBuffer);
MaterialRegistry.Release(ref _renderingMaterial);
RenderTextureRepository.Release(ref _bakeBuffer);
MaterialRepository.Release(ref _renderingMaterial);
base.OnDisable();

canvasRenderer.hasPopInstruction = false;
Expand Down Expand Up @@ -630,14 +658,14 @@ public override Material GetModifiedMaterial(Material baseMaterial)
{
if (!isActiveAndEnabled || baseMaterial.shader != defaultMaterial.shader)
{
MaterialRegistry.Release(ref _renderingMaterial);
MaterialRepository.Release(ref _renderingMaterial);
return baseMaterial;
}

Profiler.BeginSample("(CCR)[CompositeCanvasRenderer] GetModifiedMaterial > Get material");
var hash = CreateHash(colorMode, srcBlendMode, dstBlendMode);
_createMaterial = _createMaterial ?? CreateMaterial;
MaterialRegistry.Get(hash, ref _renderingMaterial, _createMaterial);
MaterialRepository.Get(hash, ref _renderingMaterial, _createMaterial);
Profiler.EndSample();

return _renderingMaterial;
Expand Down Expand Up @@ -835,6 +863,18 @@ private bool IsInCanvasViewport()
return result;
}

internal static CompositeCanvasRenderer GetFirstGroupedRenderer(int groupId)
{
var node = s_ActiveRenderers.First;
while (node != null)
{
if (node.Value.sharingGroupId == groupId) return node.Value;
node = node.Next;
}

return null;
}

/// <summary>
/// Render the bake buffer using the source graphic.
/// </summary>
Expand All @@ -851,6 +891,15 @@ private void Bake()
return;
}

if (sharingGroupId != 0)
{
if (GetFirstGroupedRenderer(sharingGroupId) != this)
{
canvasRenderer.SetTexture(mainTexture);
return;
}
}

if (!IsAnySourceInRenderer())
{
Logging.Log(this,
Expand Down

0 comments on commit 53f03a4

Please sign in to comment.