Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Commit

Permalink
async write
Browse files Browse the repository at this point in the history
  • Loading branch information
i-saint committed Jan 9, 2018
1 parent cca33ea commit 8d5a207
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 35 deletions.
43 changes: 39 additions & 4 deletions FbxExporter/Assets/UTJ/FbxExporter/Editor/FbxExporterWindow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
Expand All @@ -18,12 +19,20 @@ public static void Open()
window.Show();
}

enum Scope
public enum Scope
{
Selected,
EntireScene,
}

public class Record
{
public FbxExporter exporter;
public string path;
public DateTime started = DateTime.Now;
}
static List<Record> s_records = new List<Record>();

Scope m_scope = Scope.Selected;
bool m_includeChildren = true;
FbxExporter.Format m_format = FbxExporter.Format.FbxBinary;
Expand All @@ -37,9 +46,35 @@ bool DoExport(string path, FbxExporter.Format format, GameObject[] objects)
foreach (var obj in objects)
exporter.AddNode(obj);

var ret = exporter.Write(path, format);
exporter.Release();
return ret;
if(exporter.WriteAsync(path, format))
{
Debug.Log("Export started: " + path);
s_records.Add(new Record {path = path, exporter = exporter});
return true;
}
else
{
Debug.Log("Export failed: " + path);
return false;
}
}

void Update()
{
// poll async write
bool finished = false;
foreach (var record in s_records)
{
if (record.exporter.IsFinished())
{
var elapsed = DateTime.Now - record.started;
record.exporter.Release();
Debug.Log("Export finished: " + record.path + " (" + elapsed.TotalSeconds + " seconds)");
finished = true;
}
}
if (finished)
s_records.RemoveAll((a) => { return a.exporter.IsFinished(); });
}

void OnGUI()
Expand Down
9 changes: 7 additions & 2 deletions FbxExporter/Assets/UTJ/FbxExporter/Scripts/FbxExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ public void AddNode(GameObject go)
FindOrCreateNodeTree(go.GetComponent<Transform>(), ProcessNode);
}

public bool Write(string path, Format format)
public bool WriteAsync(string path, Format format)
{
return fbxeWrite(m_ctx, path, format);
return fbxeWriteAsync(m_ctx, path, format);
}

public bool IsFinished()
{
return fbxeIsFinished(m_ctx);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public static ExportOptions defaultValue
[DllImport("FbxExporterCore")] static extern void fbxeReleaseContext(Context ctx);

[DllImport("FbxExporterCore")] static extern bool fbxeCreateScene(Context ctx, string name);
[DllImport("FbxExporterCore")] static extern bool fbxeWrite(Context ctx, string path, Format format);
[DllImport("FbxExporterCore")] static extern bool fbxeWriteAsync(Context ctx, string path, Format format);
[DllImport("FbxExporterCore")] static extern bool fbxeIsFinished(Context ctx);

[DllImport("FbxExporterCore")] static extern Node fbxeGetRootNode(Context ctx);
[DllImport("FbxExporterCore")] static extern Node fbxeFindNodeByName(Context ctx, string name);
Expand Down
10 changes: 8 additions & 2 deletions Plugin/FbxExporter/FbxExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ fbxeAPI int fbxeCreateScene(fbxe::IContext *ctx, const char *name)
return ctx->createScene(name);
}

fbxeAPI int fbxeWrite(fbxe::IContext *ctx, const char *path, fbxe::Format format)
fbxeAPI int fbxeWriteAsync(fbxe::IContext *ctx, const char *path, fbxe::Format format)
{
if (!ctx) { return false; }
return ctx->write(path, format);
return ctx->writeAsync(path, format);
}

fbxeAPI int fbxeIsFinished(fbxe::IContext *ctx)
{
if (!ctx) { return false; }
return ctx->isFinished();
}

fbxeAPI fbxe::Node* fbxeGetRootNode(fbxe::IContext *ctx)
Expand Down
3 changes: 2 additions & 1 deletion Plugin/FbxExporter/FbxExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ fbxeAPI fbxe::IContext* fbxeCreateContext(const fbxe::ExportOptions *opt);
fbxeAPI void fbxeReleaseContext(fbxe::IContext *ctx);

fbxeAPI int fbxeCreateScene(fbxe::IContext *ctx, const char *name);
fbxeAPI int fbxeWrite(fbxe::IContext *ctx, const char *path, fbxe::Format format);
fbxeAPI int fbxeWriteAsync(fbxe::IContext *ctx, const char *path, fbxe::Format format);
fbxeAPI int fbxeIsFinished(fbxe::IContext *ctx);

fbxeAPI fbxe::Node* fbxeGetRootNode(fbxe::IContext *ctx);
fbxeAPI fbxe::Node* fbxeFindNodeByName(fbxe::IContext *ctx, const char *name);
Expand Down
43 changes: 28 additions & 15 deletions Plugin/FbxExporter/fbxeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class Context : public IContext
void clear() override;

bool createScene(const char *name) override;
bool write(const char *path, Format format) override;
bool writeAsync(const char *path, Format format) override;
bool isFinished() override;
void wait() override;

Node* getRootNode() override;
Node* findNodeByName(const char *name) override;
Expand All @@ -98,12 +100,12 @@ class Context : public IContext
using ContextPtr = std::shared_ptr<Context>;


static std::vector<ContextPtr> g_contexts;
static std::map<void*, ContextPtr> g_contexts;

fbxeAPI IContext* CreateContext(const ExportOptions *opt)
{
auto ret = new Context(opt);
g_contexts.emplace_back(ret);
g_contexts[ret].reset(ret);
return ret;
}

Expand All @@ -127,14 +129,12 @@ Context::~Context()

void Context::release()
{
// don't delete here to keep proceed async tasks
g_contexts.erase(this);
}

void Context::clear()
{
if (m_task.valid()) {
m_task.wait();
}
wait();
if (m_scene) {
m_scene->Destroy(true);
m_scene = nullptr;
Expand All @@ -160,26 +160,39 @@ bool Context::createScene(const char *name)
return m_scene != nullptr;
}

bool Context::write(const char *path_, Format format)
bool Context::writeAsync(const char *path_, Format format)
{
if (!m_scene) { return false; }
wait();

std::string path = path_;
m_task = std::async(std::launch::async, [this, path, format]() {
for (auto& p : m_mesh_data) {
for (auto& task : p.second->tasks) {
task();
}
}
m_mesh_data.clear();

doWrite(path.c_str(), format);
});
return true;
}

bool Context::isFinished()
{
return m_task.valid() && m_task.wait_for(std::chrono::milliseconds(0)) != std::future_status::timeout;
}

void Context::wait()
{
if (m_task.valid()) {
m_task.wait();
}
}

bool Context::doWrite(const char *path, Format format)
{
for (auto& p : m_mesh_data) {
for (auto& task : p.second->tasks) {
task();
}
}
m_mesh_data.clear();

int file_format = 0;
{
// search file format index
Expand Down
4 changes: 3 additions & 1 deletion Plugin/FbxExporter/fbxeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class IContext
virtual void clear() = 0;

virtual bool createScene(const char *name) = 0;
virtual bool write(const char *path, Format format = Format::FbxBinary) = 0;
virtual bool writeAsync(const char *path, Format format = Format::FbxBinary) = 0;
virtual bool isFinished() = 0;
virtual void wait() = 0;

virtual Node* getRootNode() = 0;
virtual Node* findNodeByName(const char *name) = 0;
Expand Down
1 change: 1 addition & 0 deletions Plugin/FbxExporter/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <memory>
#include <iostream>
Expand Down
18 changes: 9 additions & 9 deletions Plugin/Test/TestFbxExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ void TestFbxExportMesh()
fbxeAddMeshSubmesh(ctx, mesh, fbxe::Topology::Quads, indices.size(), indices.data(), -1);
}

fbxeWrite(ctx, "mesh_binary.fbx", fbxe::Format::FbxBinary);
fbxeWrite(ctx, "mesh_ascii.fbx", fbxe::Format::FbxAscii);
fbxeWrite(ctx, "mesh_encrypted.fbx", fbxe::Format::FbxEncrypted);
fbxeWrite(ctx, "mesh_obj.obj", fbxe::Format::Obj);
fbxeWriteAsync(ctx, "mesh_binary.fbx", fbxe::Format::FbxBinary);
fbxeWriteAsync(ctx, "mesh_ascii.fbx", fbxe::Format::FbxAscii);
fbxeWriteAsync(ctx, "mesh_encrypted.fbx", fbxe::Format::FbxEncrypted);
fbxeWriteAsync(ctx, "mesh_obj.obj", fbxe::Format::Obj);
fbxeReleaseContext(ctx);
}
RegisterTestEntry(TestFbxExportMesh)
Expand Down Expand Up @@ -241,8 +241,8 @@ void TestFbxExportSkinnedMesh()
fbxeAddMeshSubmesh(ctx, mesh, fbxe::Topology::Quads, indices.size(), indices.data(), -1);
fbxeAddMeshSkin(ctx, mesh, weights.data(), num_bones, bones, bindposes);

fbxeWrite(ctx, "SkinnedMesh_binary.fbx", fbxe::Format::FbxBinary);
fbxeWrite(ctx, "SkinnedMesh_ascii.fbx", fbxe::Format::FbxAscii);
fbxeWriteAsync(ctx, "SkinnedMesh_binary.fbx", fbxe::Format::FbxBinary);
fbxeWriteAsync(ctx, "SkinnedMesh_ascii.fbx", fbxe::Format::FbxAscii);
fbxeReleaseContext(ctx);
}
RegisterTestEntry(TestFbxExportSkinnedMesh)
Expand Down Expand Up @@ -299,8 +299,8 @@ void TestFbxExportSkinnedMeshSegmented()
fbxeAddMeshSkin(ctx, mesh, weights.data(), num_bones, bones, bindposes);
}

fbxeWrite(ctx, "SkinnedMeshSegmented_binary.fbx", fbxe::Format::FbxBinary);
fbxeWrite(ctx, "SkinnedMeshSegmented_ascii.fbx", fbxe::Format::FbxAscii);
fbxeWriteAsync(ctx, "SkinnedMeshSegmented_binary.fbx", fbxe::Format::FbxBinary);
fbxeWriteAsync(ctx, "SkinnedMeshSegmented_ascii.fbx", fbxe::Format::FbxAscii);
fbxeReleaseContext(ctx);
}
RegisterTestEntry(TestFbxExportSkinnedMeshSegmented)
Expand All @@ -318,7 +318,7 @@ void TestFbxNameConflict()
auto cnode11 = fbxeCreateNode(ctx, cnode2, "GrandChild $%&#?*@");
auto cnode12 = fbxeCreateNode(ctx, cnode2, "GrandChild");

fbxeWrite(ctx, "namesanitize_ascii.fbx", fbxe::Format::FbxAscii);
fbxeWriteAsync(ctx, "namesanitize_ascii.fbx", fbxe::Format::FbxAscii);
fbxeReleaseContext(ctx);
}
RegisterTestEntry(TestFbxNameConflict)

0 comments on commit 8d5a207

Please sign in to comment.