Skip to content

Commit

Permalink
Merge pull request #4 from wotakuro/feature/syncreadback
Browse files Browse the repository at this point in the history
Feature/syncreadback
  • Loading branch information
wotakuro authored Nov 6, 2020
2 parents f2db60e + aa54768 commit b009e38
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 24 deletions.
54 changes: 49 additions & 5 deletions Editor/ProfilerScreenShotWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ private struct TagInfo
public int originWidth;
public int originHeight;
}

private enum OutputMode:int
{
Origin = 0,
FitWindow = 1,
}

[MenuItem("Tools/ProfilerScreenshot")]
public static void Create()
Expand All @@ -32,10 +36,17 @@ public static void Create()
private Texture originTexture;

private int lastPreviewFrameIdx;
private bool isAutoReflesh = false;
private bool isAutoReflesh = true;
private bool isYFlip = false;
private OutputMode outputMode;

private Vector2Int outputSize = new Vector2Int();
private GUIContent[] outputModeSelect = new GUIContent[2]
{
new GUIContent("Original Size"),
new GUIContent("Fit window Size"),
};

private Vector2Int outputSize = new Vector2Int();

private void OnEnable()
{
Expand Down Expand Up @@ -63,6 +74,7 @@ private void Reflesh(int frameIdx,bool force = false)
}
HierarchyFrameDataView hierarchyFrameDataView =
ProfilerDriver.GetHierarchyFrameDataView(frameIdx, 0, HierarchyFrameDataView.ViewModes.Default, 0, false); ;
if(hierarchyFrameDataView == null) { return; }
NativeArray<byte> bytes =
hierarchyFrameDataView.GetFrameMetaData<byte>(ScreenShotToProfiler.MetadataGuid, ScreenShotToProfiler.InfoTag);
if (bytes != null && bytes.Length >= 12)
Expand Down Expand Up @@ -140,6 +152,10 @@ private Texture2D GenerateTagTexture(TagInfo info,int idx)
{
HierarchyFrameDataView hierarchyFrameDataView =
ProfilerDriver.GetHierarchyFrameDataView(i, 0, HierarchyFrameDataView.ViewModes.Default, 0, false);
if( hierarchyFrameDataView == null)
{
continue;
}
NativeArray<byte> bytes =
hierarchyFrameDataView.GetFrameMetaData<byte>(ScreenShotToProfiler.MetadataGuid, info.id);

Expand Down Expand Up @@ -176,9 +192,11 @@ private void OnGUI()
this.Reflesh(GetProfilerActiveFrame(),true);
}
}
EditorGUILayout.Space();

EditorGUILayout.BeginHorizontal();
this.isYFlip = EditorGUILayout.Toggle("Flip Y", this.isYFlip);
EditorGUILayout.LabelField("Size",GUILayout.Width(40));
outputMode = (OutputMode)EditorGUILayout.Popup((int)outputMode, outputModeSelect);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
//drawTextureInfo.SetFlip(this.isYFlip);

Expand All @@ -188,6 +206,11 @@ private void OnGUI()
{
var rect = EditorGUILayout.GetControlRect(GUILayout.Width(outputSize.x),
GUILayout.Height(outputSize.y));
if (outputMode == OutputMode.FitWindow)
{
rect = FitWindow(rect);
}

if (this.isYFlip)
{
rect.y += rect.height;
Expand All @@ -196,6 +219,27 @@ private void OnGUI()
EditorGUI.DrawTextureTransparent(rect, drawTexture);
}
}
private Rect FitWindow(Rect r)
{
if( r.width == 0 || r.height == 0) { return r; }
var windowPos = this.position;
float xparam = (position.width - r.x * 2) / r.width;
float yparam = (position.height - (r.y+5) ) / r.height;

if( xparam > yparam)
{
r.width *= yparam;
r.height *= yparam;
}
else
{
r.width *= xparam;
r.height *= xparam;
}


return r;
}

private int GetProfilerActiveFrame()
{
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Embed Screenshot to Unity Profiler protocol.<br />

## requirement
- 2019.3 or newer.<br />
- support System.supportsAsyncGPUReadback platform only<br />
(Mobile vulkan or metal....)

## reccomend
- the platforms that supports System.supportsAsyncGPUReadback (Mobile vulkan or metal....)<br />
Support sync readback from 1.1.0 , however it's very slow....

## how to use
1.calling this at Runtime. <br />
Expand All @@ -17,6 +19,10 @@ or <br />
UTJ.SS2Profiler.ScreenShotToProfiler.Instance.Initialize(w,h);<br />
( w,h means recording texture size).


2.call "Tools -> ProfilerScreenshot" from Menu. <br />
And then window will be displayed.


## change
<pre>
version 1.1.0 -> add sync readback option for gles.
60 changes: 53 additions & 7 deletions Runtime/ScreenShotLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ private struct RequestInfo
private byte[] tagInfo;
private CommandBuffer commandBuffer;

private Texture2D syncTexCache;
private CustomSampler syncUpdateSampler;

public ScreenShotLogic(int width , int height)
{
frames = new DataInfo[FRAME_NUM];
Expand Down Expand Up @@ -69,7 +72,7 @@ public void Dispose()
frames = null;
}

public void Update()
public void UpdateAsyncRequest()
{
for( int i = 0; i < FRAME_NUM; ++i)
{
Expand Down Expand Up @@ -106,7 +109,46 @@ public void Update()
break;
}
}
}

public void ReadBackSyncAtIdx(int idx)
{
if (idx < 0 || idx >= FRAME_NUM)
{
return;
}
var rt = frames[idx].renderTexture;
if( rt == null) { return; }
if(syncUpdateSampler == null)
{
this.syncUpdateSampler = CustomSampler.Create("SyncUpdate");
}

syncUpdateSampler.Begin();
if (syncTexCache != null &&
(syncTexCache.width != rt.width || syncTexCache.height != rt.height) )
{
Object.Destroy(syncTexCache);
syncTexCache = null;
}

Texture2D tex2d = syncTexCache;
if (tex2d == null)
{
tex2d = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
}
RenderTexture.active = rt;
tex2d.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex2d.Apply();
var bytes = tex2d.GetRawTextureData<byte>();
Profiler.EmitFrameMetaData(ScreenShotToProfiler.MetadataGuid,
frames[idx].id, bytes);

syncTexCache = tex2d;

frames[idx].isRequest = false;
frames[idx].fromEnd = 0;
syncUpdateSampler.End();
}

public void AsyncReadbackRequestAtIdx(int idx)
Expand All @@ -116,7 +158,6 @@ public void AsyncReadbackRequestAtIdx(int idx)
return;
}
if (!IsAvailable(idx) ) { return; }

// var request = AsyncGPUReadback.RequestIntoNativeArray(ref frames[idx].data, frames[idx].renderTexture, 0);
var request = AsyncGPUReadback.Request(frames[idx].renderTexture);
requests.Enqueue( new RequestInfo { request = request, idx = idx });
Expand Down Expand Up @@ -145,10 +186,8 @@ public int CaptureScreen(int id)
continue;
}
frames[i].id = id;
this.WriteToTagInfo(id, 0);
this.WriteToTagInfoShort(Screen.width, 8);
this.WriteToTagInfoShort(Screen.height, 10);
Profiler.EmitFrameMetaData(ScreenShotToProfiler.MetadataGuid, ScreenShotToProfiler.InfoTag, tagInfo);
WriteTagMetaData(id);

var rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32);
ScreenCapture.CaptureScreenshotIntoRenderTexture(rt);
commandBuffer.BeginSample(CAPTURE_CMD_SAMPLE);
Expand All @@ -161,11 +200,18 @@ public int CaptureScreen(int id)
}
return -1;
}
private void WriteTagMetaData(int id)
{
this.WriteToTagInfo(id, 0);
this.WriteToTagInfoShort(Screen.width, 8);
this.WriteToTagInfoShort(Screen.height, 10);
Profiler.EmitFrameMetaData(ScreenShotToProfiler.MetadataGuid, ScreenShotToProfiler.InfoTag, tagInfo);


}
}



}

#endif
29 changes: 21 additions & 8 deletions Runtime/ScreenShotToProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,28 @@ public bool Initialize()
{
if (Screen.width > Screen.height)
{
return Initialize(192,128);
return Initialize(192,128,true);
}
else
{
return Initialize(128, 192);
return Initialize(128, 192,true);
}
}

public bool Initialize(int width , int height)
public bool Initialize(int width , int height,bool allowSync = false)
{
#if DEBUG
if (!SystemInfo.supportsAsyncGPUReadback)
{
return false;
if (!allowSync)
{
return false;
}
else
{
UnityEngine.Debug.LogWarning("SystemInfo.supportsAsyncGPUReadback is false! Profiler Screenshot is very slow...");
}
}
#if DEBUG
if (renderTextureBuffer != null) { return false; }
InitializeLogic(width,height);
#endif
Expand Down Expand Up @@ -83,8 +90,14 @@ public void Destroy()
private void Update()
{
this.updateSampler.Begin();
renderTextureBuffer.AsyncReadbackRequestAtIdx(lastRequestIdx);
renderTextureBuffer.Update();
if ( SystemInfo.supportsAsyncGPUReadback) {
renderTextureBuffer.AsyncReadbackRequestAtIdx(lastRequestIdx);
renderTextureBuffer.UpdateAsyncRequest();
}
else
{
renderTextureBuffer.ReadBackSyncAtIdx(lastRequestIdx);
}

this.updateSampler.End();
}
Expand All @@ -93,7 +106,7 @@ private void Capture()
{
captureSampler.Begin();
lastRequestIdx = renderTextureBuffer.CaptureScreen(frameIdx);
renderTextureBuffer.Update();
renderTextureBuffer.UpdateAsyncRequest();
++frameIdx;
captureSampler.End();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.utj.screenshot2profiler",
"displayName": "Screenshot to Profiler",
"version": "1.0.1",
"version": "1.1.0",
"unity": "2019.3",
"description": "Embedded screenshot to profiler data",
"keywords": [
Expand Down

0 comments on commit b009e38

Please sign in to comment.