Skip to content

Commit

Permalink
Add JobSystem support (#1968)
Browse files Browse the repository at this point in the history
  • Loading branch information
halx99 authored Jun 6, 2024
1 parent 79aabfd commit 8eeed46
Show file tree
Hide file tree
Showing 17 changed files with 566 additions and 371 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- uses: ilammy/msvc-dev-cmd@v1
with:
toolset: '14.39'
arch: 'x64'
- name: Build
shell: pwsh
run: |
Expand All @@ -38,10 +34,6 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- uses: ilammy/msvc-dev-cmd@v1
with:
toolset: '14.39'
arch: 'x64'
- name: Build
shell: pwsh
run: .\tools\cmdline\axmol -p win32 -a x64 -dll
Expand All @@ -52,11 +44,6 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- uses: ilammy/msvc-dev-cmd@v1
with:
toolset: '14.39'
arch: 'x64'
uwp: true
- name: Build
shell: pwsh
run: .\tools\cmdline\axmol -p winuwp -a x64 -O0
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## axmol-2.1.4 ?? 2024

### Significant changes relative to 2.1.2:

- Add JobSystem support

### Mark as deprecated

- `AsyncTaskPool`, use `JobSystem` instead.

## axmol-2.1.3 May.26 2024

### Significant changes relative to 2.1.2:
Expand Down
4 changes: 2 additions & 2 deletions core/2d/RenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ void RenderTexture::onSaveToFile(std::string filename, bool isRGBA, bool forceNo
{
if (forceNonPMA && image->hasPremultipliedAlpha())
{
std::thread([this, image, _filename, isRGBA, forceNonPMA]() {
_director->getJobSystem()->enqueue([this, image, _filename, isRGBA, forceNonPMA]() {
image->reversePremultipliedAlpha();

Director::getInstance()->getScheduler()->runOnAxmolThread([this, image, _filename, isRGBA] {
Expand All @@ -480,7 +480,7 @@ void RenderTexture::onSaveToFile(std::string filename, bool isRGBA, bool forceNo
_saveFileCallback(this, _filename);
}
});
}).detach();
});
}
else
{
Expand Down
69 changes: 37 additions & 32 deletions core/3d/MeshRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "3d/Mesh.h"

#include "base/Director.h"
#include "base/AsyncTaskPool.h"
#include "base/UTF8.h"
#include "base/Utils.h"
#include "2d/Light.h"
Expand Down Expand Up @@ -85,16 +84,16 @@ MeshRenderer* MeshRenderer::create(std::string_view modelPath, std::string_view
}

void MeshRenderer::createAsync(std::string_view modelPath,
const std::function<void(MeshRenderer*, void*)>& callback,
void* callbackparam)
const std::function<void(MeshRenderer*, void*)>& callback,
void* callbackparam)
{
createAsync(modelPath, "", callback, callbackparam);
}

void MeshRenderer::createAsync(std::string_view modelPath,
std::string_view texturePath,
const std::function<void(MeshRenderer*, void*)>& callback,
void* callbackparam)
std::string_view texturePath,
const std::function<void(MeshRenderer*, void*)>& callback,
void* callbackparam)
{
MeshRenderer* meshRenderer = new MeshRenderer();
if (meshRenderer->loadFromCache(modelPath))
Expand All @@ -106,20 +105,22 @@ void MeshRenderer::createAsync(std::string_view modelPath,
}

meshRenderer->_asyncLoadParam.afterLoadCallback = callback;
meshRenderer->_asyncLoadParam.texPath = texturePath;
meshRenderer->_asyncLoadParam.modelPath = modelPath;
meshRenderer->_asyncLoadParam.modelFullPath = FileUtils::getInstance()->fullPathForFilename(modelPath);
meshRenderer->_asyncLoadParam.callbackParam = callbackparam;
meshRenderer->_asyncLoadParam.materialdatas = new MaterialDatas();
meshRenderer->_asyncLoadParam.meshdatas = new MeshDatas();
meshRenderer->_asyncLoadParam.nodeDatas = new NodeDatas();
AsyncTaskPool::getInstance()->enqueue(
AsyncTaskPool::TaskType::TASK_IO, AX_CALLBACK_1(MeshRenderer::afterAsyncLoad, meshRenderer),
(void*)(&meshRenderer->_asyncLoadParam), [meshRenderer]() {
auto& loadParam = meshRenderer->_asyncLoadParam;
loadParam.result = meshRenderer->loadFromFile(loadParam.modelFullPath, loadParam.nodeDatas,
loadParam.meshdatas, loadParam.materialdatas);
});
meshRenderer->_asyncLoadParam.texPath = texturePath;
meshRenderer->_asyncLoadParam.modelPath = modelPath;
meshRenderer->_asyncLoadParam.modelFullPath = FileUtils::getInstance()->fullPathForFilename(modelPath);
meshRenderer->_asyncLoadParam.callbackParam = callbackparam;
meshRenderer->_asyncLoadParam.materialdatas = new MaterialDatas();
meshRenderer->_asyncLoadParam.meshdatas = new MeshDatas();
meshRenderer->_asyncLoadParam.nodeDatas = new NodeDatas();

auto director = Director::getInstance();
director->getJobSystem()->enqueue(
[director, meshRenderer] {
auto& loadParam = meshRenderer->_asyncLoadParam;
loadParam.result = meshRenderer->loadFromFile(loadParam.modelFullPath, loadParam.nodeDatas, loadParam.meshdatas,
loadParam.materialdatas);
},
[meshRenderer] { meshRenderer->afterAsyncLoad(&meshRenderer->_asyncLoadParam); });
}

void MeshRenderer::afterAsyncLoad(void* param)
Expand Down Expand Up @@ -232,9 +233,9 @@ bool MeshRenderer::loadFromCache(std::string_view path)
}

bool MeshRenderer::loadFromFile(std::string_view path,
NodeDatas* nodedatas,
MeshDatas* meshdatas,
MaterialDatas* materialdatas)
NodeDatas* nodedatas,
MeshDatas* meshdatas,
MaterialDatas* materialdatas)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);

Expand Down Expand Up @@ -368,7 +369,9 @@ bool MeshRenderer::initFrom(const NodeDatas& nodeDatas, const MeshDatas& meshdat
return true;
}

MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas)
MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata,
ModelData* modeldata,
const MaterialDatas& materialdatas)
{
auto meshRenderer = new MeshRenderer();

Expand Down Expand Up @@ -404,7 +407,8 @@ MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData
texParams.sAddressMode = textureData->wrapS;
texParams.tAddressMode = textureData->wrapT;
tex->setTexParameters(texParams);
_transparentMaterialHint = materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
_transparentMaterialHint =
materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
Expand Down Expand Up @@ -570,7 +574,8 @@ void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialData
texParams.sAddressMode = textureData->wrapS;
texParams.tAddressMode = textureData->wrapT;
tex->setTexParameters(texParams);
_transparentMaterialHint = materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
_transparentMaterialHint =
materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
Expand Down Expand Up @@ -676,7 +681,8 @@ Texture2D* MeshRenderer::setMeshTexture(Mesh* mesh, std::string_view texPath, NT
return tex;
}

void MeshRenderer::setModelTexture(std::string_view modelPath, std::string_view texturePath) {
void MeshRenderer::setModelTexture(std::string_view modelPath, std::string_view texturePath)
{
if (!texturePath.empty())
setTexture(texturePath);
else if (!_meshTextureHint)
Expand Down Expand Up @@ -892,7 +898,7 @@ void MeshRenderer::draw(Renderer* renderer, const Mat4& transform, uint32_t flag
}
}

bool MeshRenderer::setProgramState(backend::ProgramState* programState, bool ownPS/* = false*/)
bool MeshRenderer::setProgramState(backend::ProgramState* programState, bool ownPS /* = false*/)
{
if (Node::setProgramState(programState, ownPS))
{
Expand Down Expand Up @@ -1097,13 +1103,12 @@ static MeshMaterial* getMeshRendererMaterialForAttribs(MeshVertexData* meshVerte
{
if (hasTangentSpace)
{
type = hasNormal && usesLight ? MeshMaterial::MaterialType::BUMPED_DIFFUSE
: MeshMaterial::MaterialType::UNLIT;
type =
hasNormal && usesLight ? MeshMaterial::MaterialType::BUMPED_DIFFUSE : MeshMaterial::MaterialType::UNLIT;
}
else
{
type = hasNormal && usesLight ? MeshMaterial::MaterialType::DIFFUSE
: MeshMaterial::MaterialType::UNLIT;
type = hasNormal && usesLight ? MeshMaterial::MaterialType::DIFFUSE : MeshMaterial::MaterialType::UNLIT;
}
}
else
Expand Down
97 changes: 1 addition & 96 deletions core/audio/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ AudioEngine::ProfileHelper* AudioEngine::_defaultProfileHelper = nullptr;
std::unordered_map<AUDIO_ID, AudioEngine::AudioInfo> AudioEngine::_audioIDInfoMap;
AudioEngineImpl* AudioEngine::_audioEngineImpl = nullptr;

#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
AudioEngine::AudioEngineThreadPool* AudioEngine::s_threadPool = nullptr;
#endif

bool AudioEngine::_isEnabled = true;

AudioEngine::AudioInfo::AudioInfo()
Expand All @@ -66,90 +62,12 @@ AudioEngine::AudioInfo::AudioInfo()

AudioEngine::AudioInfo::~AudioInfo() {}

#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
class AudioEngine::AudioEngineThreadPool
{
public:
AudioEngineThreadPool(int threads = 4) : _stop(false)
{
for (int index = 0; index < threads; ++index)
{
_workers.emplace_back(std::thread(std::bind(&AudioEngineThreadPool::threadFunc, this)));
}
}

void addTask(const std::function<void()>& task)
{
std::unique_lock<std::mutex> lk(_queueMutex);
_taskQueue.emplace(task);
_taskCondition.notify_one();
}

~AudioEngineThreadPool()
{
{
std::unique_lock<std::mutex> lk(_queueMutex);
_stop = true;
_taskCondition.notify_all();
}

for (auto&& worker : _workers)
{
worker.join();
}
}

private:
void threadFunc()
{
while (true)
{
std::function<void()> task = nullptr;
{
std::unique_lock<std::mutex> lk(_queueMutex);
if (_stop)
{
break;
}
if (!_taskQueue.empty())
{
task = std::move(_taskQueue.front());
_taskQueue.pop();
}
else
{
_taskCondition.wait(lk);
continue;
}
}

task();
}
}

std::vector<std::thread> _workers;
std::queue<std::function<void()>> _taskQueue;

std::mutex _queueMutex;
std::condition_variable _taskCondition;
bool _stop;
};
#endif

void AudioEngine::end()
{
// make sure everythings cleanup before delete audio engine
// fix #127
uncacheAll();

#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
if (s_threadPool)
{
delete s_threadPool;
s_threadPool = nullptr;
}
#endif

delete _audioEngineImpl;
_audioEngineImpl = nullptr;

Expand All @@ -170,13 +88,6 @@ bool AudioEngine::lazyInit()
}
}

#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
if (s_threadPool == nullptr)
{
s_threadPool = new AudioEngineThreadPool();
}
#endif

return true;
}

Expand Down Expand Up @@ -594,13 +505,7 @@ void AudioEngine::preload(std::string_view filePath, std::function<void(bool isS
void AudioEngine::addTask(const std::function<void()>& task)
{
lazyInit();

#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
if (_audioEngineImpl && s_threadPool)
{
s_threadPool->addTask(task);
}
#endif
Director::getInstance()->getJobSystem()->enqueue(task);
}

int AudioEngine::getPlayingAudioCount()
Expand Down
5 changes: 0 additions & 5 deletions core/audio/AudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,6 @@ class AX_DLL AudioEngine

static AudioEngineImpl* _audioEngineImpl;

#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
class AudioEngineThreadPool;
static AudioEngineThreadPool* s_threadPool;
#endif

static bool _isEnabled;

friend class AudioEngineImpl;
Expand Down
6 changes: 3 additions & 3 deletions core/audio/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "audio/AudioDecoder.h"
#include "audio/AudioDecoderManager.h"

#include "yasio/thread_name.hpp"

#ifdef VERY_VERY_VERBOSE_LOGGING
# define ALOGVV ALOGV
#else
Expand Down Expand Up @@ -284,9 +286,7 @@ bool AudioPlayer::play2d()
// rotateBufferThread is used to rotate alBufferData for _alSource when playing big audio file
void AudioPlayer::rotateBufferThread(int offsetFrame)
{
#if defined(__APPLE__)
pthread_setname_np("ALStreaming");
#endif
yasio::set_thread_name("axmol-audio");

char* tmpBuffer = nullptr;
auto& fullPath = _audioCache->_fileFullPath;
Expand Down
2 changes: 2 additions & 0 deletions core/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ set(_AX_BASE_HEADER
base/IMEDispatcher.h
base/PaddedString.h
base/JsonWriter.h
base/JobSystem.h
)

set(_AX_BASE_SRC
base/AsyncTaskPool.cpp
base/JobSystem.cpp
base/AutoreleasePool.cpp
base/Configuration.cpp
base/Logging.cpp
Expand Down
Loading

0 comments on commit 8eeed46

Please sign in to comment.