Skip to content

replace nanothread with custom task pool implementation #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
[submodule "external/metal-cpp"]
path = external/metal-cpp
url = https://github.com/bkaradzic/metal-cpp.git
[submodule "external/nanothread"]
path = external/nanothread
url = https://github.com/skallweitNV/nanothread.git
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,6 @@ if(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-assume -Wno-switch")
endif()

# nanothread
# TODO: disabled for now as it introduces a dependency to libatomic which leads to issues with cross compilation
# set(NANOTHREAD_STATIC ON)
# add_subdirectory(external/nanothread)
# set_target_properties(nanothread PROPERTIES POSITION_INDEPENDENT_CODE ON)

# Setup compiler warnings
target_compile_options(slang-rhi PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
Expand Down Expand Up @@ -611,7 +605,6 @@ endif()

target_include_directories(slang-rhi PUBLIC include)
target_include_directories(slang-rhi PRIVATE src)
# target_link_libraries(slang-rhi PRIVATE nanothread)
target_compile_definitions(slang-rhi
PRIVATE
SLANG_RHI_ENABLE_CPU=$<BOOL:${SLANG_RHI_ENABLE_CPU}>
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ This library is under active refactoring and development, and is not yet ready f
- [stb](https://github.com/nothings/stb) (Public Domain)
- [Vulkan-Headers](https://github.com/KhronosGroup/Vulkan-Headers) (MIT)
- [OffsetAllocator](https://github.com/sebbbi/OffsetAllocator) (MIT)
- [nanothread](https://github.com/mitsuba-renderer/nanothread) (BSD 3-Clause)
1 change: 0 additions & 1 deletion external/nanothread
Submodule nanothread deleted from 94d389
56 changes: 42 additions & 14 deletions include/slang-rhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2552,26 +2552,50 @@ class IDevice : public ISlangUnknown
convertCooperativeVectorMatrix(const ConvertCooperativeVectorMatrixDesc* descs, uint32_t descCount) = 0;
};

class ITaskScheduler : public ISlangUnknown
class ITaskPool : public ISlangUnknown
{
SLANG_COM_INTERFACE(0xab272cee, 0xa546, 0x4ae6, {0xbd, 0x0d, 0xcd, 0xab, 0xa9, 0x3f, 0x6d, 0xa6});

public:
typedef void* TaskHandle;

/// Submit a task.
/// The scheduler needs to call the `run` function with the `payload` argument.
/// The `parentTasks` contains a list of tasks that need to be completed before the submitted task can run.
/// Every submitted task is released using `releaseTask` once the task handle is no longer used.
virtual SLANG_NO_THROW TaskHandle SLANG_MCALL
submitTask(TaskHandle* parentTasks, uint32_t parentTaskCount, void (*run)(void* /*payload*/), void* payload) = 0;
/// \brief Submit a new task.
/// The returned task must be released with `releaseTask()` when no longer needed
/// for specifying dependencies or issuing `waitTask()`.
/// \param func Function to execute.
/// \param payload Payload to pass to the function.
/// \param payloadDeleter Optional payload deleter (called when task is destroyed).
/// \param deps Parent tasks to wait for.
/// \param depsCount Number of parent tasks.
/// \return The new task.
virtual SLANG_NO_THROW TaskHandle SLANG_MCALL submitTask(
void (*func)(void*),
void* payload,
void (*payloadDeleter)(void*),
TaskHandle* deps,
size_t depsCount
) = 0;

/// \brief Get the task payload data.
/// \param task Task to get the payload for.
/// \return The payload.
virtual SLANG_NO_THROW void* SLANG_MCALL getTaskPayload(TaskHandle task) = 0;

/// Release a task.
/// This is called when the task handle is no longer used.
/// \brief Release a task.
/// \param task Task to release.
virtual SLANG_NO_THROW void SLANG_MCALL releaseTask(TaskHandle task) = 0;

// Wait for a task to complete.
virtual SLANG_NO_THROW void SLANG_MCALL waitForCompletion(TaskHandle task) = 0;
/// \brief Wait for a task to finish.
/// \param task Task to wait for.
virtual SLANG_NO_THROW void SLANG_MCALL waitTask(TaskHandle task) = 0;

/// \brief Check if a task is done.
/// \param task Task to check.
/// \return True if the task is done.
virtual SLANG_NO_THROW bool SLANG_MCALL isTaskDone(TaskHandle task) = 0;

/// \brief Wait for all tasks in the pool to finish.
virtual SLANG_NO_THROW void SLANG_MCALL waitAll() = 0;
};

class IPersistentShaderCache : public ISlangUnknown
Expand Down Expand Up @@ -2651,12 +2675,16 @@ class IRHI

/// Set the global task pool worker count.
/// Must be called before any devices are created.
/// This is ignored if the task scheduler is set.
/// This only affects the default task pool implementation and has no effect
/// if a custom task pool is used with `setTaskPool`.
/// If count is set to 0, a blocking task pool implementation is used.
/// If count is -1, a threaded task pool is used with a worker count equal to the number of logical cores.
/// If count is 1 or larger, a threaded task pool is used with the specified worker count.
virtual SLANG_NO_THROW Result SLANG_MCALL setTaskPoolWorkerCount(uint32_t count) = 0;

/// Set the global task scheduler for the RHI.
/// Set the global task pool for the RHI.
/// Must be called before any devices are created.
virtual SLANG_NO_THROW Result SLANG_MCALL setTaskScheduler(ITaskScheduler* scheduler) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL setTaskPool(ITaskPool* taskPool) = 0;
};

// Global public functions
Expand Down
Loading
Loading