Skip to content

Commit e9a1932

Browse files
committed
Rearranged generator. Fixed issue in async copy. Added non-async copy
1 parent 9e5804c commit e9a1932

File tree

10 files changed

+3788
-3577
lines changed

10 files changed

+3788
-3577
lines changed

UMAProject/Assets/UMA/Core/Editor/Scripts/UMAGeneratorBaseEditor.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public class UMAGeneratorBaseEditor : Editor
1313
SerializedProperty AtlasOverflowFitMethod;
1414
SerializedProperty FitPercentageDecrease;
1515
SerializedProperty SharperFitTextures;
16+
SerializedProperty useAsyncConversion;
17+
SerializedProperty asyncMipRegen;
1618
public static bool showAtlasSettings = false;
19+
public static bool showConversionSettings = false;
1720

1821
GUIContent[] atlasLabels = new GUIContent[] { new GUIContent("512"), new GUIContent("1024"), new GUIContent("2048"), new GUIContent("4096"), new GUIContent("8192") };
1922
int[] atlasValues = new int[] { 512, 1024, 2048, 4096, 8192 };
@@ -29,6 +32,8 @@ public virtual void OnEnable()
2932
AtlasOverflowFitMethod = serializedObject.FindProperty("AtlasOverflowFitMethod");
3033
FitPercentageDecrease = serializedObject.FindProperty("FitPercentageDecrease");
3134
SharperFitTextures = serializedObject.FindProperty("SharperFitTextures");
35+
useAsyncConversion = serializedObject.FindProperty("useAsyncConversion");
36+
asyncMipRegen = serializedObject.FindProperty("asyncMipRegen");
3237
}
3338

3439
public override void OnInspectorGUI()
@@ -47,13 +52,23 @@ public override void OnInspectorGUI()
4752
EditorGUILayout.PropertyField(fitAtlas);
4853
EditorGUILayout.PropertyField(SharperFitTextures);
4954
EditorGUILayout.PropertyField(AtlasOverflowFitMethod);
50-
EditorGUILayout.HelpBox("Note: Atlas Overflow parameters only work with coroutines disabled below.", MessageType.None);
5155
EditorGUILayout.PropertyField(FitPercentageDecrease);
52-
GUIHelper.EndVerticalPadded();
53-
EditorGUILayout.PropertyField(serializedObject.FindProperty("SaveAndRestoreIgnoredItems"));
54-
EditorGUILayout.PropertyField(convertRenderTexture);
5556
EditorGUILayout.PropertyField(convertMipMaps);
5657
EditorGUILayout.IntPopup(atlasResolution, atlasLabels, atlasValues);
58+
GUIHelper.EndVerticalPadded();
59+
60+
}
61+
showConversionSettings = EditorGUILayout.Foldout(showConversionSettings, "Conversion Settings");
62+
if (showConversionSettings)
63+
{
64+
GUIHelper.BeginVerticalPadded();
65+
EditorGUILayout.HelpBox("Convert RenderTextures to Texture2D. This will create a Texture2D from the render texture, so it can be modified or saved.\n" +
66+
"Use AsyncConversion will do an async copy to avoid a GPU stall.\n"
67+
/*"Async Mip Regen will only copy the top level mip, and recalculate the mips when the texture is applied"*/, MessageType.None);
68+
EditorGUILayout.PropertyField(convertRenderTexture);
69+
EditorGUILayout.PropertyField(useAsyncConversion);
70+
//EditorGUILayout.PropertyField(asyncMipRegen);
71+
GUIHelper.EndVerticalPadded();
5772
}
5873

5974
serializedObject.ApplyModifiedProperties();

UMAProject/Assets/UMA/Core/Editor/Scripts/UMAGeneratorBuiltinEditor.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ public class UMAGeneratorBuiltinEditor : UMAGeneratorBaseEditor
2323
SerializedProperty defaultRendererAsset;
2424
SerializedProperty defaultOverlayAsset;
2525
SerializedProperty convertRenderTexture;
26-
public static bool showGenerationSettings = false;
26+
27+
public static bool showGenerationSettings = false;
2728
public static bool showAdvancedSettings = false;
2829
public static bool showStatistics = true;
30+
public static bool showEditTimeSettings = false;
2931

3032

3133
#pragma warning disable 0108
@@ -46,6 +48,7 @@ public override void OnEnable()
4648
defaultOverlayAsset = serializedObject.FindProperty("defaultOverlayAsset");
4749
MaxQueuedConversionsPerFrame = serializedObject.FindProperty("MaxQueuedConversionsPerFrame");
4850
convertRenderTexture = serializedObject.FindProperty("convertRenderTexture");
51+
4952
}
5053
#pragma warning restore 0108
5154

@@ -64,11 +67,15 @@ public override void OnInspectorGUI()
6467
EditorGUILayout.PropertyField(collectGarbage);
6568
EditorGUILayout.PropertyField(garbageCollectionRate);
6669
EditorGUILayout.PropertyField(processAllPending);
67-
GUILayout.Space(20);
68-
EditorGUILayout.HelpBox("Edit time generation options. Keep the atlas size down and the scale factor high to address possible problems loading large scene files.", MessageType.None);
69-
EditorGUILayout.PropertyField(editorAtlasResolution);
70-
EditorGUILayout.PropertyField(EditorInitialScaleFactor);
70+
EditorGUILayout.PropertyField(serializedObject.FindProperty("SaveAndRestoreIgnoredItems"));
7171
}
72+
showEditTimeSettings = EditorGUILayout.Foldout(showEditTimeSettings, "Edit Time Settings");
73+
if (showEditTimeSettings)
74+
{
75+
EditorGUILayout.HelpBox("Edit time generation options. Keep the atlas size down and the scale factor high to address possible problems loading large scene files.", MessageType.None);
76+
EditorGUILayout.PropertyField(editorAtlasResolution);
77+
EditorGUILayout.PropertyField(EditorInitialScaleFactor);
78+
}
7279

7380
showAdvancedSettings = EditorGUILayout.Foldout(showAdvancedSettings, "Advanced Settings");
7481
if (showAdvancedSettings)

UMAProject/Assets/UMA/Core/StandardAssets/UMA/Scripts/RenderTexToCPU.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class RenderTexToCPU
2323
public string textureName;
2424
public int textureIndex;
2525
public Texture2D newTexture;
26+
public bool recreateMips;
2627
public static int copiesEnqueued = 0;
2728
public static int copiesDequeued = 0;
2829
public static int unableToQueue = 0;
@@ -33,18 +34,18 @@ public class RenderTexToCPU
3334
public static int renderTexturesCleanedApplied = 0;
3435
public static int renderTexturesCleanedMissed = 0;
3536

36-
public RenderTexToCPU(RenderTexture texture, GeneratedMaterial generatedMaterial, string textureName, int textureIndex)
37+
public RenderTexToCPU(RenderTexture texture, GeneratedMaterial generatedMaterial, string textureName, int textureIndex, UMAGeneratorBase basegen)
3738
{
3839
this.texture = texture;
3940
this.generatedMaterial = generatedMaterial;
4041
this.textureName = textureName;
4142
this.textureIndex = textureIndex;
43+
this.recreateMips = basegen.convertMipMaps;
4244
renderTexturesToCPU.Add(texture.GetInstanceID(), this);
4345
}
4446

4547
public void DoAsyncCopy()
4648
{
47-
//Asynchronously
4849
AsyncGPUReadback.Request(texture, 0, (AsyncGPUReadbackRequest asyncAction) =>
4950
{
5051
QueueCopy(asyncAction);
@@ -153,7 +154,7 @@ private void ApplyTexture()
153154
try
154155
{
155156

156-
newTexture.Apply(true); // TODO: IS THIS??? TEST ONLY JRRM
157+
newTexture.Apply(texture.mipmapCount > 0);
157158
generatedMaterial.material.SetTexture(textureName, newTexture);
158159
generatedMaterial.resultingAtlasList[textureIndex] = newTexture;
159160
RenderTexture.ReleaseTemporary(texture);

UMAProject/Assets/UMA/Core/StandardAssets/UMA/Scripts/TextureProcessPro.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System;
44
using CopyTextureSupport = UnityEngine.Rendering.CopyTextureSupport;
55
using System.Collections.Generic;
6+
using UnityEngine.Rendering;
7+
using UnityEngine.Experimental.Rendering;
68

79

810
namespace UMA
@@ -215,29 +217,36 @@ public void ProcessTexture(UMAData _umaData, UMAGeneratorBase _umaGenerator)
215217

216218
if (CopyRTtoTex)
217219
{
218-
// Let it have the RenderTexture now.
219-
SetMaterialTexture(generatedMaterial, slotData, textureChannelNumber, destinationTexture);
220-
resultingTextures[textureChannelNumber] = destinationTexture;
221-
// Now asynchronously copy and reset it
222-
RenderTexToCPU rt2cpu = new RenderTexToCPU(destinationTexture, generatedMaterial, slotData.material.channels[textureChannelNumber].materialPropertyName, textureChannelNumber);
223-
rt2cpu.DoAsyncCopy();
224-
225220
#region Convert Render Textures
226-
/*
227-
// copy the texture with mips to the Texture2D
228-
Texture2D tempTexture;
229-
Debug.Log("GetRTPixels");
230-
TextureFormat texFmt = TextureFormats[destinationTexture.format];
231-
RenderTexture.active = destinationTexture;
232-
tempTexture = new Texture2D(destinationTexture.width, destinationTexture.height, texFmt, umaGenerator.convertMipMaps, true);
233-
Graphics.CopyTexture(destinationTexture, tempTexture);
234-
235-
RenderTexture.ReleaseTemporary(destinationTexture);
236-
//destinationTexture.Release();
237-
//UnityEngine.GameObject.DestroyImmediate(destinationTexture);
238-
resultingTextures[textureChannelNumber] = tempTexture as Texture;
239-
SetMaterialTexture(generatedMaterial, slotData, textureChannelNumber, tempTexture);
240-
*/
221+
if (umaGenerator.useAsyncConversion)
222+
{
223+
224+
// Let it have the RenderTexture now.
225+
SetMaterialTexture(generatedMaterial, slotData, textureChannelNumber, destinationTexture);
226+
resultingTextures[textureChannelNumber] = destinationTexture;
227+
// Now asynchronously copy and reset it
228+
RenderTexToCPU rt2cpu = new RenderTexToCPU(destinationTexture, generatedMaterial, slotData.material.channels[textureChannelNumber].materialPropertyName, textureChannelNumber, umaGenerator);
229+
rt2cpu.DoAsyncCopy();
230+
}
231+
else
232+
{
233+
// copy the texture with mips to the Texture2D
234+
Texture2D tempTexture;
235+
GraphicsFormat gf = GraphicsFormatUtility.GetGraphicsFormat(destinationTexture.format, false);
236+
TextureFormat texFmt = GraphicsFormatUtility.GetTextureFormat(gf);
237+
238+
tempTexture = new Texture2D(destinationTexture.width, destinationTexture.height, texFmt, umaGenerator.convertMipMaps, true);
239+
var asyncAction = AsyncGPUReadback.Request(destinationTexture, 0);
240+
asyncAction.WaitForCompletion();
241+
242+
tempTexture.SetPixelData(asyncAction.GetData<byte>(), 0);
243+
tempTexture.Apply();
244+
245+
RenderTexture.ReleaseTemporary(destinationTexture);
246+
247+
resultingTextures[textureChannelNumber] = tempTexture as Texture;
248+
SetMaterialTexture(generatedMaterial, slotData, textureChannelNumber, tempTexture);
249+
}
241250
#endregion
242251
}
243252
else

UMAProject/Assets/UMA/Core/StandardAssets/UMA/Scripts/UMAGeneratorBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public enum FitMethod {DecreaseResolution, BestFitSquare };
2121
#else
2222
public bool convertRenderTexture = true;
2323
#endif
24+
[Tooltip("Use Async RT conversion to avoid GPU stalls")]
25+
public bool useAsyncConversion = true;
26+
[Tooltip("Regenerate Mipmaps on conversion to avoid copying mips from GPU")]
27+
public bool asyncMipRegen = true;
28+
2429
[Tooltip("Create Mipmaps for the generated texture. Checking this is a good idea.")]
2530
public bool convertMipMaps;
2631
[Tooltip("Initial size of the texture atlas (square)")]

UMAProject/Assets/UMA/Core/StandardAssets/UMA/Scripts/UMAGeneratorBuiltin.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,6 @@ public override void Work()
170170
count = umaDirtyList.Count;
171171
}
172172

173-
if (RenderTexToCPU.PendingCopies() > 0)
174-
{
175-
RenderTexToCPU.ApplyQueuedCopies(MaxQueuedConversionsPerFrame);
176-
TexturesProcessed += MaxQueuedConversionsPerFrame > RenderTexToCPU.PendingCopies() ? RenderTexToCPU.PendingCopies() : MaxQueuedConversionsPerFrame;
177-
}
178-
179173
if (hasPendingUMAS())
180174
{
181175
for (int i = 0; i < count; i++)
@@ -201,8 +195,11 @@ public override void Work()
201195
}
202196
if (RenderTexToCPU.PendingCopies() > 0)
203197
{
198+
stopWatch.Start();
204199
RenderTexToCPU.ApplyQueuedCopies(MaxQueuedConversionsPerFrame);
205200
TexturesProcessed += MaxQueuedConversionsPerFrame > RenderTexToCPU.PendingCopies() ? RenderTexToCPU.PendingCopies() : MaxQueuedConversionsPerFrame;
201+
stopWatch.Stop();
202+
ElapsedTicks += stopWatch.ElapsedTicks;
206203
}
207204
}
208205

@@ -656,7 +653,7 @@ public void Clear()
656653
/// <inheritdoc/>
657654
public override bool IsIdle()
658655
{
659-
return (umaDirtyList.Count == 0 && RenderTexToCPU.PendingCopies() == 0);
656+
return (umaDirtyList.Count == 0);// && RenderTexToCPU.PendingCopies() == 0);
660657
}
661658

662659
public bool hasPendingUMAS()

0 commit comments

Comments
 (0)