remove ITextureLoader.ProcessOnAnyThread

This commit is contained in:
ousttrue
2021-02-12 17:17:13 +09:00
parent 92138a80ff
commit c26ea50e6a
9 changed files with 45 additions and 141 deletions

View File

@@ -3,8 +3,7 @@ using System.Collections;
using System.Collections.Generic;
namespace
DepthFirstScheduler
namespace DepthFirstScheduler
{
public static class IEnumeratorExtensions
{

View File

@@ -503,8 +503,7 @@ namespace UniGLTF
{
m_materialFactory.Prepare(GLTF);
})
.ContinueWithCoroutine(Scheduler.ThreadPool, () => m_materialFactory.TexturesProcessOnAnyThread(GLTF, Storage))
.ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.TexturesProcessOnMainThread(GLTF))
.ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.TexturesProcessOnMainThread(GLTF, Storage))
.ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.LoadMaterials(GLTF))
.OnExecute(Scheduler.ThreadPool, parent =>
{

View File

@@ -124,31 +124,19 @@ namespace UniGLTF
#region Process
ITextureLoader m_textureLoader;
public void Process(glTF gltf, IStorage storage)
{
ProcessOnAnyThread(gltf, storage);
ProcessOnMainThreadCoroutine(gltf).CoroutineToEnd();
}
// public void Process(glTF gltf, IStorage storage)
// {
// ProcessOnMainThreadCoroutine(gltf, storage).CoroutineToEnd();
// }
public IEnumerator ProcessCoroutine(glTF gltf, IStorage storage)
{
ProcessOnAnyThread(gltf, storage);
yield return ProcessOnMainThreadCoroutine(gltf);
}
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
m_textureLoader.ProcessOnAnyThread(gltf, storage);
}
public IEnumerator ProcessOnMainThreadCoroutine(glTF gltf)
public IEnumerator ProcessOnMainThreadCoroutine(glTF gltf, IStorage storage)
{
using (m_textureLoader)
{
var textureType = TextureIO.GetglTFTextureType(gltf, m_textureIndex);
var colorSpace = TextureIO.GetColorSpace(textureType);
var isLinear = colorSpace == RenderTextureReadWrite.Linear;
yield return m_textureLoader.ProcessOnMainThread(isLinear, gltf.GetSamplerFromTextureIndex(m_textureIndex));
yield return m_textureLoader.ProcessOnMainThread(gltf, storage, isLinear, gltf.GetSamplerFromTextureIndex(m_textureIndex));
}
}
#endregion

View File

@@ -136,25 +136,13 @@ namespace UniGLTF
}
}
public IEnumerator TexturesProcessOnAnyThread(glTF gltf, IStorage storage)
{
// using (MeasureTime("TexturesProcessOnAnyThread"))
{
foreach (var x in GetTextures())
{
x.ProcessOnAnyThread(gltf, storage);
yield return null;
}
}
}
public IEnumerator TexturesProcessOnMainThread(glTF gltf)
public IEnumerator TexturesProcessOnMainThread(glTF gltf, IStorage storage)
{
// using (MeasureTime("TexturesProcessOnMainThread"))
{
foreach (var x in GetTextures())
{
yield return x.ProcessOnMainThreadCoroutine(gltf);
yield return x.ProcessOnMainThreadCoroutine(gltf, storage);
}
}
}

View File

@@ -23,11 +23,7 @@ namespace UniGLTF
{
}
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler)
{
//
// texture from assets

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
namespace UniGLTF
@@ -40,25 +41,30 @@ namespace UniGLTF
}
}
Byte[] m_imageBytes;
string m_textureName;
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
m_imageBytes = ToArray(segments);
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler)
{
Byte[] imageBytes = default;
var task = Task.Run(() =>
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
var m_imageBytes = ToArray(segments);
});
while (!task.IsCompleted)
{
yield break;
}
//
// texture from image(png etc) bytes
//
Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
Texture.name = m_textureName;
if (m_imageBytes != null)
if (imageBytes != null)
{
Texture.LoadImage(m_imageBytes);
Texture.LoadImage(imageBytes);
}
if (sampler != null)
{

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
#if UNITY_EDITOR
using UnityEditor;
#endif
@@ -14,22 +12,12 @@ namespace UniGLTF
{
Texture2D Texture { get; }
/// <summary>
/// Call from any thread
/// </summary>
/// <param name="gltf"></param>
/// <param name="storage"></param>
void ProcessOnAnyThread(glTF gltf, IStorage storage);
/// <summary>
/// Call from unity main thread
/// </summary>
/// <param name="isLinear"></param>
/// <param name="sampler"></param>
/// <returns></returns>
IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler);
IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler);
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
@@ -31,84 +32,11 @@ namespace UniGLTF
}
}
ArraySegment<Byte> m_segments;
string m_textureName;
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
public void ProcessOnAnyThread()
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
m_segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
}
#if false
HttpHost m_http;
class HttpHost : IDisposable
{
TcpListener m_listener;
Socket m_connection;
public HttpHost(int port)
{
m_listener = new TcpListener(IPAddress.Loopback, port);
m_listener.Start();
m_listener.BeginAcceptSocket(OnAccepted, m_listener);
}
void OnAccepted(IAsyncResult ar)
{
var l = ar.AsyncState as TcpListener;
if (l == null) return;
m_connection = l.EndAcceptSocket(ar);
// 次の接続受付はしない
BeginRead(m_connection, new byte[8192]);
}
void BeginRead(Socket c, byte[] buffer)
{
AsyncCallback callback = ar =>
{
var s = ar.AsyncState as Socket;
if (s == null) return;
var size = s.EndReceive(ar);
if (size > 0)
{
OnRead(buffer, size);
}
BeginRead(s, buffer);
};
m_connection.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, callback, m_connection);
}
List<Byte> m_buffer = new List<byte>();
void OnRead(byte[] buffer, int len)
{
m_buffer.AddRange(buffer.Take(len));
}
public string Url
{
get
{
}
}
public void Dispose()
{
if (m_connection != null)
{
m_connection.Dispose();
m_connection = null;
}
if(m_listener != null)
{
m_listener.Stop();
m_listener = null;
}
}
}
#endif
class Deleter : IDisposable
{
string m_path;
@@ -125,13 +53,24 @@ namespace UniGLTF
}
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler)
{
ArraySegment<Byte> bytes = default;
var task = Task.Run(() =>
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
bytes = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
});
while (!task.IsCompleted)
{
yield return null;
}
// tmp file
var tmp = Path.GetTempFileName();
using (var f = new FileStream(tmp, FileMode.Create))
{
f.Write(m_segments.Array, m_segments.Offset, m_segments.Count);
f.Write(bytes.Array, bytes.Offset, bytes.Count);
}
using (var d = new Deleter(tmp))

View File

@@ -6,6 +6,7 @@ using UnityEngine;
using System.IO;
using System.Collections;
using UniJSON;
using DepthFirstScheduler;
namespace VRM
{
@@ -317,7 +318,7 @@ namespace VRM
if (gltfMeta.texture >= 0 && gltfMeta.texture < GLTF.textures.Count)
{
var t = new TextureItem(gltfMeta.texture, MaterialFactory.CreateTextureLoader(gltfMeta.texture));
t.Process(GLTF, Storage);
t.ProcessOnMainThreadCoroutine(GLTF, Storage).CoroutineToEnd();
meta.Thumbnail = t.Texture;
}
}