diff options
author | chai <chaifix@163.com> | 2021-10-26 23:20:45 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-26 23:20:45 +0800 |
commit | 208e23de77ad6d104f13a0bb591ae16c4a805fe9 (patch) | |
tree | 29499de463a6d3407d2e555c1ad9b743f07097b1 | |
parent | 8b419dc90cb4e01fa1810e0f84dcdcfe2bd822ad (diff) |
*load image async
-rw-r--r-- | Data/DefaultContent/Libraries/GameLab/Engine/Resource/ImageDataRequest.lua | 10 | ||||
-rw-r--r-- | Data/DefaultContent/Libraries/GameLab/Engine/Resource/init.lua | 20 | ||||
-rw-r--r-- | Data/Scripts/EditorApplication.lua | 10 | ||||
-rw-r--r-- | Data/boot.lua | 1 | ||||
-rw-r--r-- | Editor/Scripting/EditorScripting.cpp | 2 | ||||
-rw-r--r-- | Projects/VisualStudio/Editor/Editor.vcxproj | 3 | ||||
-rw-r--r-- | Projects/VisualStudio/Editor/Editor.vcxproj.filters | 12 | ||||
-rw-r--r-- | Runtime/FileSystem/ImageJobs.cpp | 54 | ||||
-rw-r--r-- | Runtime/FileSystem/ImageJobs.h | 46 | ||||
-rw-r--r-- | Runtime/Graphics/ImageData.h | 12 | ||||
-rw-r--r-- | Runtime/Scripting/Resource/Resource.bind.cpp | 41 | ||||
-rw-r--r-- | Runtime/Threading/WorkThread.cpp | 1 |
12 files changed, 197 insertions, 15 deletions
diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Resource/ImageDataRequest.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Resource/ImageDataRequest.lua new file mode 100644 index 0000000..f7d7bbe --- /dev/null +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Resource/ImageDataRequest.lua @@ -0,0 +1,10 @@ +local ImageDataRequest = GameLab.Class("GameLab.Engine.Resource.ImageDataRequest") + +ImageDataRequest.Ctor = function(self) + self.isDone = false + self.hasError = false + self.error = 0 + self.imageData = nil +end + +return ImageDataRequest
\ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Resource/init.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Resource/init.lua new file mode 100644 index 0000000..dd37dbb --- /dev/null +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Resource/init.lua @@ -0,0 +1,20 @@ +local m = GameLab.Engine.Resource or {} +GameLab.Engine.Resource = m +local import = GameLab.import(...) + +-- classes + +m.ImageDataRequest = import("ImageDataRequest") + +-- methods + +m.LoadImageDataAsync = function(path) + local request = GameLab.Engine.ImageDataRequest.New() + m.ReadImageDataAsync(path, function(imgData) + request.isDone = true + request.imageData = imgData + end) + return request +end + +return m
\ No newline at end of file diff --git a/Data/Scripts/EditorApplication.lua b/Data/Scripts/EditorApplication.lua index 8b54e41..852069d 100644 --- a/Data/Scripts/EditorApplication.lua +++ b/Data/Scripts/EditorApplication.lua @@ -3,6 +3,7 @@ local inspect = require "inspect" local AssetBrowser = require "./Scripts/Editor/AssetBrowser" local EditorWindowManager = require "./Scripts/EditorGUI/EditorWindowManager" +local Resource = GameLab.Engine.Resource local Rendering = GameLab.Engine.Rendering local Debug = GameLab.Debug local GUI = GameLab.Editor.GUI @@ -69,9 +70,12 @@ Debug.Log(tostring(imgData:GetHeight())) local tex = Rendering.Texture.New(imgData, false) -while true do +local requets = Rendering.LoadImageDataAsync("./Resources/Images/brickwall.jpg") +while true do + if requets.isDone then + Debug.Log("read image done!!!") + end app:OnStep() app:PullMessage() - -end
\ No newline at end of file +end diff --git a/Data/boot.lua b/Data/boot.lua index 7e7cfdc..ad8f4da 100644 --- a/Data/boot.lua +++ b/Data/boot.lua @@ -26,6 +26,7 @@ require "GameLab" require "GameLab.Engine"
require "GameLab.Engine.Math"
require "GameLab.Engine.Rendering"
+require "GameLab.Engine.Resource"
require "GameLab.Engine.GL"
require "GameLab.Editor"
require "GameLab.Editor.GUI"
diff --git a/Editor/Scripting/EditorScripting.cpp b/Editor/Scripting/EditorScripting.cpp index ad8ccd6..f384248 100644 --- a/Editor/Scripting/EditorScripting.cpp +++ b/Editor/Scripting/EditorScripting.cpp @@ -13,6 +13,7 @@ extern int luaopen_GameLab_Engine_Event(lua_State* L); // GameLab.Engine.Event extern int luaopen_GameLab_Engine_Networking(lua_State* L); // GameLab.Engine.Networking extern int luaopen_GameLab_Engine_Animation(lua_State* L); // GameLab.Engine.Animation extern int luaopen_GameLab_Engine_GL(lua_State* L);// GameLab.Engine.GL +extern int luaopen_GameLab_Engine_Resource(lua_State* L); // GameLab.Engine.Resource extern int luaopen_GameLab_Editor(lua_State* L); // GameLab.Editor extern int luaopen_GameLab_Editor_GUI(lua_State* L); // GameLab.Editor.GUI @@ -43,6 +44,7 @@ bool SetupGameLabEditorScripting(lua_State* L) openlib(luaopen_GameLab_Engine_Rendering); openlib(luaopen_GameLab_Engine_GL); + openlib(luaopen_GameLab_Engine_Resource); openlib(luaopen_GameLab_Editor); openlib(luaopen_GameLab_Editor_GUI); diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index ad75401..bc3ce5b 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -173,6 +173,7 @@ <ClCompile Include="..\..\..\Runtime\Debug\Log.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\File.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\FileJobs.cpp" />
+ <ClCompile Include="..\..\..\Runtime\FileSystem\ImageJobs.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\Path.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\Unzip.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\Device.cpp" />
@@ -213,6 +214,7 @@ <ClCompile Include="..\..\..\Runtime\Scripting\Rendering\Rendering.bind.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Rendering\Shader.bind.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Rendering\Texture.bind.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Scripting\Resource\Resource.bind.cpp" />
<ClCompile Include="..\..\..\Runtime\Threading\Job.cpp" />
<ClCompile Include="..\..\..\Runtime\Threading\JobSystem.cpp" />
<ClCompile Include="..\..\..\Runtime\Threading\Mutex.cpp" />
@@ -240,6 +242,7 @@ <ClInclude Include="..\..\..\Runtime\Debug\Log.h" />
<ClInclude Include="..\..\..\Runtime\FileSystem\File.h" />
<ClInclude Include="..\..\..\Runtime\FileSystem\FileJobs.h" />
+ <ClInclude Include="..\..\..\Runtime\FileSystem\ImageJobs.h" />
<ClInclude Include="..\..\..\Runtime\FileSystem\Path.h" />
<ClInclude Include="..\..\..\Runtime\FileSystem\Unzip.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\Color.h" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 4170c69..8dc6c1f 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -106,6 +106,9 @@ <Filter Include="Runtime\Scripting\Path">
<UniqueIdentifier>{e9a9d1a8-f637-407d-83f7-99dc9da72b8f}</UniqueIdentifier>
</Filter>
+ <Filter Include="Runtime\Scripting\Resource">
+ <UniqueIdentifier>{5579c035-9fb8-4c27-9d3a-6435e23e07be}</UniqueIdentifier>
+ </Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\Editor\GUI\Dock.cpp">
@@ -342,6 +345,12 @@ <ClCompile Include="..\..\..\Runtime\Scripting\Rendering\ImageData.bind.cpp">
<Filter>Runtime\Scripting\Rendering</Filter>
</ClCompile>
+ <ClCompile Include="..\..\..\Runtime\FileSystem\ImageJobs.cpp">
+ <Filter>Runtime\FileSystem</Filter>
+ </ClCompile>
+ <ClCompile Include="..\..\..\Runtime\Scripting\Resource\Resource.bind.cpp">
+ <Filter>Runtime\Scripting\Resource</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Editor\GUI\Dock.h">
@@ -569,6 +578,9 @@ <ClInclude Include="..\..\..\Runtime\Threading\JobSystem.h">
<Filter>Runtime\Threading</Filter>
</ClInclude>
+ <ClInclude Include="..\..\..\Runtime\FileSystem\ImageJobs.h">
+ <Filter>Runtime\FileSystem</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.inc">
diff --git a/Runtime/FileSystem/ImageJobs.cpp b/Runtime/FileSystem/ImageJobs.cpp new file mode 100644 index 0000000..84e4ff3 --- /dev/null +++ b/Runtime/FileSystem/ImageJobs.cpp @@ -0,0 +1,54 @@ +#include "ImageJobs.h" +#include "ThirdParty/stb/stb_image.h" +#include "Runtime/Graphics/ImageData.h" + +//-------------------------------------------------------------------------- + +ReadImageFileJob::ReadImageFileJob(LuaBind::VM* vm, int indexOfCallback, const char* path) + : isFinished(false) + , callback(vm) +{ + this->path = path; + LuaBind::State state = vm->GetCurThread(); + callback.SetRef(state, indexOfCallback); +} + +ReadImageFileJob::~ReadImageFileJob() +{ + callback.UnRef(); + delete bridge.pixels; //for safe +} + +void ReadImageFileJob::Process() +{ + stbi_set_flip_vertically_on_load(true); + bridge.pixels = stbi_load(path.c_str(), &bridge.width, &bridge.height, &bridge.channels, 0); + isFinished = true; +} + +bool ReadImageFileJob::IsFinished() +{ + return isFinished; +} + +// callback(imageData) +void ReadImageFileJob::Dispacth(void* param) +{ + LUA_BIND_STATE((lua_State*)param); + + ImageData* imgData = new ImageData(state.GetVM()); + imgData->pixels = bridge.pixels; + bridge.pixels = NULL; + imgData->width = bridge.width; + imgData->height = bridge.height; + imgData->format = ImageData::EPixelFormat::RGB; + imgData->type = ImageData::EPixelElementType::UNSIGNED_BYTE; + + callback.PushRef(state); + imgData->PushUserdata(state); + state.Call(1, 0); +} + +//-------------------------------------------------------------------------- + + diff --git a/Runtime/FileSystem/ImageJobs.h b/Runtime/FileSystem/ImageJobs.h new file mode 100644 index 0000000..77e48e0 --- /dev/null +++ b/Runtime/FileSystem/ImageJobs.h @@ -0,0 +1,46 @@ +#pragma once +#include <vector> +#include "Runtime/Threading/Job.h" +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Threading/JobSystem.h" + +struct ImageDataBridge +{ + void* pixels; + int width; + int height; + int channels; +}; + +// in: string +// out: ImageData +class ReadImageFileJob : public Job +{ +public: + ReadImageFileJob(LuaBind::VM* vm, int indexOfCallback, const char* path); + ~ReadImageFileJob(); + + void Dispacth(void* param) override; + void Process() override; + bool IsFinished() override; + +private: + std::string path; + bool isFinished; + ImageDataBridge bridge; + LuaBind::StrongRef callback; // 完成后的回调函数 +}; + +// in: string[] +// out: ImageData[] +class ReadImageFilesJob : public Job +{ + +}; + +// in: DataBuffer[] +// out: ImageData[] +class DecodeImageFilesJob : public Job +{ + +};
\ No newline at end of file diff --git a/Runtime/Graphics/ImageData.h b/Runtime/Graphics/ImageData.h index 4663e0e..4525ee3 100644 --- a/Runtime/Graphics/ImageData.h +++ b/Runtime/Graphics/ImageData.h @@ -69,16 +69,4 @@ namespace ImageDataUtil ImageDataRequest* LoadAsync(std::vector<const char*> paths); } -// 在工作线程最多只能走到读取->解码完,提交到GPU还得主线程做 - -class ReadImageFilesJob : public Job -{ - -}; - -class DecodeImageFilesJob : public Job -{ - -}; - #endif
\ No newline at end of file diff --git a/Runtime/Scripting/Resource/Resource.bind.cpp b/Runtime/Scripting/Resource/Resource.bind.cpp new file mode 100644 index 0000000..b4ba025 --- /dev/null +++ b/Runtime/Scripting/Resource/Resource.bind.cpp @@ -0,0 +1,41 @@ +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Debug/Log.h" +#include "Runtime/FileSystem/ImageJobs.h" + +using namespace LuaBind; + +// Resource.LoadImageDataAsync(path, callback) +int ReadImageDataAsync(lua_State* L) +{ + LUA_BIND_STATE(L); + LUA_BIND_CHECK(L, "TF"); + + cc8* path = state.GetValue(1, ""); + + ReadImageFileJob* job = new ReadImageFileJob(state.GetVM(), 2, path); + + JobSystem::Instance()->AddJobAtEnd(job); + + return 0; +} + +static luaL_Reg funcs[] = { + {"ReadImageDataAsync", ReadImageDataAsync}, + {0, 0} +}; + +int luaopen_GameLab_Engine_Resource(lua_State* L) +{ + log_info("Scripting", "luaopen_GameLab_Engine_Resource()"); + + LUA_BIND_STATE(L); + + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Engine"); + state.PushNamespace("Resource"); + + state.RegisterMethods(funcs); + + return 1; +}
\ No newline at end of file diff --git a/Runtime/Threading/WorkThread.cpp b/Runtime/Threading/WorkThread.cpp index 31c3ead..5ffa38f 100644 --- a/Runtime/Threading/WorkThread.cpp +++ b/Runtime/Threading/WorkThread.cpp @@ -34,6 +34,7 @@ void WorkThread::Dispatch(void* param) for (int i = 0; i < m_FinishedJobs.size(); ++i) { m_FinishedJobs[i]->Dispacth(param); + delete m_FinishedJobs[i]; } m_FinishedJobs.clear(); } |