Skip to content
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

threadpool rollback #61

Merged
merged 2 commits into from
May 9, 2024
Merged
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
6 changes: 2 additions & 4 deletions include/vierkant/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Scene
public:
virtual ~Scene() = default;

static ScenePtr create(crocore::ThreadPool *thread_pool = nullptr);
static ScenePtr create();

virtual void add_object(const Object3DPtr &object);

Expand Down Expand Up @@ -63,11 +63,9 @@ class Scene
vierkant::Object3DPtr create_mesh_object(const vierkant::mesh_component_t &mesh_component);

protected:
explicit Scene(crocore::ThreadPool *thread_pool = nullptr);
crocore::ThreadPool *m_thread_pool = nullptr;
explicit Scene() = default;

private:
vierkant::mesh_map_t m_meshes;
std::shared_ptr<entt::registry> m_registry = std::make_shared<entt::registry>();

vierkant::ImagePtr m_skybox = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion include/vierkant/model/gltf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ namespace vierkant::model
*
* @return an optional struct grouping the loaded assets.
*/
std::optional<model_assets_t> gltf(const std::filesystem::path &path, crocore::ThreadPool* pool = nullptr);
std::optional<model_assets_t> gltf(const std::filesystem::path &path, crocore::ThreadPoolClassic* pool = nullptr);

}// namespace vierkant::model
6 changes: 3 additions & 3 deletions include/vierkant/model/model_loading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <variant>

#include <crocore/Image.hpp>
#include <crocore/ThreadPool.hpp>
#include <crocore/ThreadPoolClassic.hpp>

#include <vierkant/Geometry.hpp>
#include <vierkant/Material.hpp>
Expand Down Expand Up @@ -114,7 +114,7 @@ struct load_mesh_params_t
*
* @return a struct grouping the loaded assets.
*/
std::optional<model_assets_t> load_model(const std::filesystem::path &path, crocore::ThreadPool *pool = nullptr);
std::optional<model_assets_t> load_model(const std::filesystem::path &path, crocore::ThreadPoolClassic *pool = nullptr);

/**
* @brief load_mesh can be used to load assets into gpu-buffers
Expand All @@ -131,7 +131,7 @@ vierkant::MeshPtr load_mesh(const load_mesh_params_t &params, const vierkant::mo
* @param mesh_assets a mesh_assets struct.
* @return true, if all images contained in mesh_assets are compressed.
*/
bool compress_textures(vierkant::model::model_assets_t &mesh_assets);
bool compress_textures(vierkant::model::model_assets_t &mesh_assets, crocore::ThreadPoolClassic *pool = nullptr);

/**
* @brief create_compressed_texture can be used to create a texture from pre-compressed bc7 blocks.
Expand Down
4 changes: 2 additions & 2 deletions include/vierkant/model/wavefront_obj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace vierkant::model
*
* @return an optional struct grouping the loaded assets.
*/
std::optional<model_assets_t> wavefront_obj(const std::filesystem::path &path, crocore::ThreadPool* pool = nullptr);
std::optional<model_assets_t> wavefront_obj(const std::filesystem::path &path,
crocore::ThreadPoolClassic *pool = nullptr);

}// namespace vierkant::model

8 changes: 5 additions & 3 deletions include/vierkant/physics_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class PhysicsScene : public vierkant::Scene
public:
~PhysicsScene() override = default;

static std::shared_ptr<PhysicsScene> create(crocore::ThreadPool *thread_pool = nullptr);
static std::shared_ptr<PhysicsScene> create();

void add_object(const Object3DPtr &object) override;

Expand All @@ -156,8 +156,10 @@ class PhysicsScene : public vierkant::Scene
vierkant::PhysicsContext &context() { return m_context; };

private:
explicit PhysicsScene(crocore::ThreadPool *thread_pool = nullptr);
vierkant::PhysicsContext m_context;
explicit PhysicsScene() = default;

crocore::ThreadPool m_thread_pool{std::thread::hardware_concurrency() - 1};
vierkant::PhysicsContext m_context{&m_thread_pool};
};

}//namespace vierkant
Expand Down
4 changes: 1 addition & 3 deletions src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ vierkant::Object3DPtr Scene::create_mesh_object(const mesh_component_t &mesh_com
return object;
}

ScenePtr Scene::create(crocore::ThreadPool *thread_pool) { return ScenePtr(new Scene(thread_pool)); }

Scene::Scene(crocore::ThreadPool *thread_pool) : m_thread_pool(thread_pool) {}
ScenePtr Scene::create() { return ScenePtr(new Scene()); }

void Scene::add_object(const Object3DPtr &object) { m_root->add_child(object); }

Expand Down
4 changes: 2 additions & 2 deletions src/model/gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ struct load_image_context_t
// cache image-futures
std::map<uint32_t, std::future<crocore::ImagePtr>> image_cache;

crocore::ThreadPool *pool = nullptr;
crocore::ThreadPoolClassic *pool = nullptr;
};

bool LoadImageDataFunction(tinygltf::Image * /*tiny_image*/, const int image_idx, std::string * /*err*/,
Expand Down Expand Up @@ -835,7 +835,7 @@ bool LoadImageDataFunction(tinygltf::Image * /*tiny_image*/, const int image_idx

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::optional<model_assets_t> gltf(const std::filesystem::path &path, crocore::ThreadPool *const pool)
std::optional<model_assets_t> gltf(const std::filesystem::path &path, crocore::ThreadPoolClassic *const pool)
{
tinygltf::Model model;
tinygltf::TinyGLTF loader;
Expand Down
13 changes: 6 additions & 7 deletions src/model/model_loading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,17 @@ vierkant::VkSamplerPtr create_sampler(const vierkant::DevicePtr &device, const v
return {sampler, [device](VkSampler s) { vkDestroySampler(device->handle(), s, nullptr); }};
}

bool compress_textures(vierkant::model::model_assets_t &mesh_assets)
bool compress_textures(vierkant::model::model_assets_t &mesh_assets, crocore::ThreadPoolClassic *pool)
{
std::chrono::milliseconds compress_total_duration(0);
crocore::ThreadPool threadpool(std::thread::hardware_concurrency());
size_t num_pixels = 0;

for(auto &[tex_id, texture_variant]: mesh_assets.textures)
{
try
{
texture_variant = std::visit(
[&threadpool, &compress_total_duration, &num_pixels](auto &&img) -> texture_variant_t {
[pool, &compress_total_duration, &num_pixels](auto &&img) -> texture_variant_t {
using T = std::decay_t<decltype(img)>;

if constexpr(std::is_same_v<T, crocore::ImagePtr>)
Expand All @@ -96,7 +95,7 @@ bool compress_textures(vierkant::model::model_assets_t &mesh_assets)
bc7::compress_info_t compress_info = {};
compress_info.image = img;
compress_info.generate_mipmaps = true;
compress_info.delegate_fn = [&threadpool](auto fn) { return threadpool.post(fn); };
if(pool){compress_info.delegate_fn = [pool](auto fn) { return pool->post(fn); };}
auto compressed_img = bc7::compress(compress_info);
compress_total_duration += compressed_img.duration;
num_pixels += img->width() * img->height();
Expand Down Expand Up @@ -193,10 +192,10 @@ vierkant::MeshPtr load_mesh(const load_mesh_params_t &params, const vierkant::mo
std::unordered_map<vierkant::SamplerId, vierkant::VkSamplerPtr> sampler_cache;

// generate textures with default samplers
for(const auto &[tex_id, tex_variant]: mesh_assets.textures)
for(const auto &[id, tex_variant]: mesh_assets.textures)
{
std::visit(
[&params, tex_id = tex_id, &texture_cache, &create_texture](auto &&img) {
[&params, tex_id = id, &texture_cache, &create_texture](auto &&img) {
using T = std::decay_t<decltype(img)>;

if constexpr(std::is_same_v<T, crocore::ImagePtr>) { texture_cache[tex_id] = create_texture(img); }
Expand Down Expand Up @@ -291,7 +290,7 @@ vierkant::ImagePtr create_compressed_texture(const vierkant::DevicePtr &device,
return compressed_img;
}

std::optional<model_assets_t> load_model(const std::filesystem::path &path, crocore::ThreadPool *pool)
std::optional<model_assets_t> load_model(const std::filesystem::path &path, crocore::ThreadPoolClassic *pool)
{
auto ext_str = path.extension().string();
std::transform(ext_str.begin(), ext_str.end(), ext_str.begin(), ::tolower);
Expand Down
2 changes: 1 addition & 1 deletion src/model/wavefront_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ vierkant::GeometryPtr create_geometry(const tinyobj::attrib_t &inattrib, const s
return geom;
}

std::optional<model_assets_t> wavefront_obj(const std::filesystem::path &path, crocore::ThreadPool * /*pool*/)
std::optional<model_assets_t> wavefront_obj(const std::filesystem::path &path, crocore::ThreadPoolClassic * /*pool*/)
{
if(!exists(path) || !is_regular_file(path)) { return {}; }

Expand Down
10 changes: 3 additions & 7 deletions src/physics_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,6 @@ void PhysicsContext::set_callbacks(uint32_t objectId, const PhysicsContext::call

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PhysicsScene::PhysicsScene(crocore::ThreadPool *thread_pool) : vierkant::Scene(thread_pool), m_context(thread_pool) {}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PhysicsScene::add_object(const Object3DPtr &object)
{
vierkant::Scene::add_object(object);
Expand All @@ -733,7 +729,7 @@ void PhysicsScene::remove_object(const Object3DPtr &object)

void PhysicsScene::clear()
{
m_context = vierkant::PhysicsContext(m_thread_pool);
m_context = vierkant::PhysicsContext(&m_thread_pool);
Scene::clear();
}

Expand Down Expand Up @@ -766,9 +762,9 @@ void PhysicsScene::update(double time_delta)
m_context.step_simulation(static_cast<float>(time_delta), 2);
}

std::shared_ptr<PhysicsScene> PhysicsScene::create(crocore::ThreadPool *thread_pool)
std::shared_ptr<PhysicsScene> PhysicsScene::create()
{
return std::shared_ptr<PhysicsScene>(new PhysicsScene(thread_pool));
return std::shared_ptr<PhysicsScene>(new PhysicsScene());
}

}//namespace vierkant
Expand Down
2 changes: 1 addition & 1 deletion submodules/JoltPhysics
Submodule JoltPhysics updated 78 files
+21 −0 .github/workflows/build.yml
+1 −1 .github/workflows/doxygen.yml
+12 −4 Build/CMakeLists.txt
+1 −0 Build/README.md
+4 −1 Docs/APIChanges.md
+17 −1 Docs/Architecture.md
+39 −1 Docs/ReleaseNotes.md
+14 −0 Jolt/ConfigurationString.h
+598 −0 Jolt/Core/Array.h
+1 −1 Jolt/Core/ByteBuffer.h
+8 −5 Jolt/Core/Core.h
+6 −0 Jolt/Core/JobSystemThreadPool.cpp
+9 −0 Jolt/Core/JobSystemThreadPool.h
+1 −1 Jolt/Core/LinearCurve.cpp
+11 −0 Jolt/Core/Memory.cpp
+3 −0 Jolt/Core/Memory.h
+1 −1 Jolt/Core/Profiler.cpp
+6 −0 Jolt/Core/STLAlignedAllocator.h
+28 −3 Jolt/Core/STLAllocator.h
+8 −5 Jolt/Core/STLTempAllocator.h
+2 −0 Jolt/Core/StaticArray.h
+18 −9 Jolt/Core/StreamIn.h
+20 −9 Jolt/Core/StreamOut.h
+3 −4 Jolt/Geometry/ConvexHullBuilder.cpp
+1 −2 Jolt/Geometry/ConvexHullBuilder2D.cpp
+10 −1 Jolt/Geometry/EPAPenetrationDepth.h
+10 −0 Jolt/Jolt.cmake
+1 −1 Jolt/Jolt.h
+94 −83 Jolt/Jolt.natvis
+20 −0 Jolt/Math/Trigonometry.h
+26 −14 Jolt/Math/Vec3.cpp
+2 −1 Jolt/Math/Vec3.h
+2 −7 Jolt/Math/Vector.h
+2 −2 Jolt/ObjectStream/GetPrimitiveTypeOfType.h
+8 −8 Jolt/ObjectStream/ObjectStream.h
+1 −1 Jolt/Physics/Body/BodyFilter.h
+20 −0 Jolt/Physics/Body/BodyInterface.cpp
+1 −0 Jolt/Physics/Body/BodyInterface.h
+2 −2 Jolt/Physics/Body/BodyManager.cpp
+1 −0 Jolt/Physics/Character/Character.cpp
+3 −0 Jolt/Physics/Character/CharacterBase.h
+74 −5 Jolt/Physics/Character/CharacterVirtual.cpp
+8 −3 Jolt/Physics/Character/CharacterVirtual.h
+1 −1 Jolt/Physics/Collision/BroadPhase/BroadPhaseBruteForce.cpp
+4 −0 Jolt/Physics/Collision/InternalEdgeRemovingCollector.h
+6 −6 Jolt/Physics/Collision/Shape/CapsuleShape.cpp
+2 −2 Jolt/Physics/Collision/Shape/ConvexShape.cpp
+1 −1 Jolt/Physics/Collision/Shape/ConvexShape.h
+2 −2 Jolt/Physics/Collision/Shape/CylinderShape.cpp
+8 −4 Jolt/Physics/Collision/Shape/GetTrianglesContext.h
+17 −18 Jolt/Physics/Collision/Shape/HeightFieldShape.cpp
+9 −9 Jolt/Physics/Collision/Shape/HeightFieldShape.h
+2 −2 Jolt/Physics/PhysicsSystem.cpp
+1 −1 Jolt/Physics/PhysicsUpdateContext.h
+153 −88 Jolt/Physics/SoftBody/SoftBodyMotionProperties.cpp
+8 −5 Jolt/Physics/SoftBody/SoftBodyMotionProperties.h
+40 −4 Jolt/Physics/SoftBody/SoftBodySharedSettings.cpp
+2 −0 Jolt/Physics/SoftBody/SoftBodySharedSettings.h
+1 −1 Jolt/RegisterTypes.cpp
+2 −2 Jolt/Skeleton/SkeletonMapper.cpp
+2 −2 Jolt/TriangleGrouper/TriangleGrouperClosestCentroid.cpp
+2 −0 Samples/Samples.cmake
+2 −0 Samples/SamplesApp.cpp
+2 −0 Samples/Tests/Character/CharacterVirtualTest.cpp
+1 −0 Samples/Tests/Character/CharacterVirtualTest.h
+4 −0 Samples/Tests/General/LoadSaveSceneTest.cpp
+26 −3 Samples/Tests/SoftBody/SoftBodyContactListenerTest.cpp
+59 −0 Samples/Tests/SoftBody/SoftBodyForceTest.cpp
+24 −0 Samples/Tests/SoftBody/SoftBodyForceTest.h
+9 −2 Samples/Tests/SoftBody/SoftBodySkinnedConstraintTest.cpp
+2 −0 Samples/Tests/SoftBody/SoftBodySkinnedConstraintTest.h
+2 −0 Samples/Tests/Water/BoatTest.cpp
+2 −2 TestFramework/UI/UIHorizontalStack.cpp
+7 −0 TestFramework/Utils/CustomMemoryHook.cpp
+643 −0 UnitTests/Core/ArrayTest.cpp
+27 −0 UnitTests/Math/TrigonometryTests.cpp
+1 −1 UnitTests/Physics/PhysicsTests.cpp
+2 −0 UnitTests/UnitTests.cmake
Loading