I have a following code , and i can not retrive encoded Output from Job.
If I Use JOB : The result is not a valid png file. What I get is a 67,2 MB file which can not be opened with any image editor I tried. When I open it with a text editor, it seems to contain only whitespaces.
else i got normal valid png file saved on my disc .
Can we use this approach for encoding inside Unity ?
public class Loger : MonoBehaviour
{
[SerializeField]
public bool isLoging = true;
[SerializeField]
private RenderTexture _rt;
private NativeArray<byte> _data;
private AsyncGPUReadbackRequest _request;
public bool useJob;
private void Start()
{
CreateBuffer(ref _data, ref _rt);
StartCoroutine(StartEncoding(_rt, isLoging));
}
System.Collections.IEnumerator StartEncoding(RenderTexture oRTs, bool active)
{
int i = 0;
while (active)
{
_request= AsyncGPUReadback.RequestIntoNativeArray(ref _data, oRTs, 0);
if(_request.hasError)
{
Debug.Log("Error in GPU request "); break;
}
yield return new WaitUntil(() => _request.done);
if(useJob)
{
Debug.Log("creating Job");
EncodeJob job = new EncodeJob
{
input = new NativeArray<byte>(_data, Allocator.TempJob),
output = new NativeArray<byte>(_data.Length, Allocator.TempJob),
graphicsFormat = oRTs.graphicsFormat,
width = oRTs.width,
height = oRTs.height,
result = new NativeArray<int>(1, Allocator.TempJob)
};
JobHandle jobTask = job.Schedule();
jobTask.Complete();
Debug.Log("result from job is " + " " + job.result[0]);
var data = job.output;
Debug.Log("created PNG file ");
string pathName = "Logs/" + "FLCW" + "/" + i++ + "JOB" + ".png";
System.IO.File.WriteAllBytes(pathName, data.ToArray());
yield return new WaitUntil(() => _request.done);
}
else if(!useJob)
{
var fileData =ImageConversion.EncodeNativeArrayToPNG(_data, oRTs.graphicsFormat, (uint)oRTs.width, (uint)oRTs.height);
Debug.Log("created PNG file ");
string pathName = "Logs/" + "FLCW" + "/" + i++ + "MAIN" +".png" ;
System.IO.File.WriteAllBytes(pathName, fileData.ToArray());
yield return new WaitUntil(() => _request.done);
}
yield return new WaitUntil(() => _request.done);
}
}
void CreateBuffer(ref NativeArray<byte> buffer, ref RenderTexture oRTS)
{
buffer = new NativeArray<byte>(oRTS.height * oRTS.width * 4, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
}
private void OnApplicationQuit()
{
_data.Dispose();
}
struct EncodeJob : IJob
{
public NativeArray<byte> input;
public NativeArray<byte> output;
public GraphicsFormat graphicsFormat;
public NativeArray<int> result;
public int width;
public int height;
public void Execute()
{
output = ImageConversion.EncodeNativeArrayToPNG(input,graphicsFormat,(uint)width,(uint)height);
result[0] = width + height;
}
}
}
‰PNG
followed by binary gibberish. What's the file size? Is it a 0 byte file or does it contain lots of whitespaces? \$\endgroup\$