Remove worker class

This commit is contained in:
Magnus Norddahl 2021-11-15 23:36:29 +01:00
parent e61f10af24
commit 955632f1f8
6 changed files with 34 additions and 50 deletions

View file

@ -156,7 +156,6 @@ set( SOURCES
src/nodebuilder/nodebuild_classify_nosse2.cpp src/nodebuilder/nodebuild_classify_nosse2.cpp
src/lightmap/pngwriter.cpp src/lightmap/pngwriter.cpp
src/lightmap/surfaces.cpp src/lightmap/surfaces.cpp
src/lightmap/worker.cpp
src/lightmap/collision.cpp src/lightmap/collision.cpp
src/lightmap/delauneytriangulator.cpp src/lightmap/delauneytriangulator.cpp
src/lightmap/delauneytriangulator.h src/lightmap/delauneytriangulator.h
@ -216,7 +215,6 @@ set( HEADERS
src/framework/binfile.h src/framework/binfile.h
src/lightmap/pngwriter.h src/lightmap/pngwriter.h
src/lightmap/surfaces.h src/lightmap/surfaces.h
src/lightmap/worker.h
src/lightmap/collision.h src/lightmap/collision.h
src/math/mat.h src/math/mat.h
src/math/quaternion.h src/math/quaternion.h

View file

@ -3,16 +3,17 @@
#include "surfaces.h" #include "surfaces.h"
#include "level/level.h" #include "level/level.h"
#include "cpuraytracer.h" #include "cpuraytracer.h"
#include "worker.h"
#include "framework/binfile.h" #include "framework/binfile.h"
#include "framework/templates.h" #include "framework/templates.h"
#include "framework/halffloat.h" #include "framework/halffloat.h"
#include <map> #include <map>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <thread>
#include <zlib.h> #include <zlib.h>
extern bool VKDebug; extern bool VKDebug;
extern int NumThreads;
CPURaytracer::CPURaytracer() CPURaytracer::CPURaytracer()
{ {
@ -60,7 +61,7 @@ void CPURaytracer::Raytrace(LevelMesh* level)
printf("Ray tracing with %d bounce(s)\n", mesh->map->LightBounce); printf("Ray tracing with %d bounce(s)\n", mesh->map->LightBounce);
Worker::RunJob((int)tasks.size(), [=](int id) { RaytraceTask(tasks[id]); }); RunJob((int)tasks.size(), [=](int id) { RaytraceTask(tasks[id]); });
printf("\nRaytrace complete\n"); printf("\nRaytrace complete\n");
} }
@ -480,3 +481,30 @@ bool CPURaytracer::TraceAnyHit(const vec3& startVec, const vec3& endVec)
{ {
return TriangleMeshShape::find_any_hit(CollisionMesh.get(), startVec, endVec); return TriangleMeshShape::find_any_hit(CollisionMesh.get(), startVec, endVec);
} }
void CPURaytracer::RunJob(int count, std::function<void(int)> callback)
{
int numThreads = NumThreads;
if (numThreads <= 0)
numThreads = std::thread::hardware_concurrency();
if (numThreads <= 0)
numThreads = 4;
numThreads = std::min(numThreads, count);
std::vector<std::thread> threads;
for (int threadIndex = 0; threadIndex < numThreads; threadIndex++)
{
threads.push_back(std::thread([=]() {
for (int i = threadIndex; i < count; i += numThreads)
{
callback(i);
}
}));
}
for (int i = 0; i < numThreads; i++)
threads[i].join();
}

View file

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <functional>
class LevelMesh; class LevelMesh;
struct CPUTraceTask struct CPUTraceTask
@ -87,6 +89,8 @@ private:
static float RadicalInverse_VdC(uint32_t bits); static float RadicalInverse_VdC(uint32_t bits);
static vec2 Hammersley(uint32_t i, uint32_t N); static vec2 Hammersley(uint32_t i, uint32_t N);
static void RunJob(int count, std::function<void(int i)> callback);
const int coverageSampleCount = 256; const int coverageSampleCount = 256;
const int bounceSampleCount = 2048; const int bounceSampleCount = 2048;

View file

@ -3,7 +3,6 @@
#include "surfaces.h" #include "surfaces.h"
#include "level/level.h" #include "level/level.h"
#include "gpuraytracer.h" #include "gpuraytracer.h"
#include "worker.h"
#include "framework/binfile.h" #include "framework/binfile.h"
#include "framework/templates.h" #include "framework/templates.h"
#include "framework/halffloat.h" #include "framework/halffloat.h"

View file

@ -1,35 +0,0 @@
#include "worker.h"
#include "math/mathlib.h"
#include <vector>
#include <thread>
#include <algorithm>
extern int NumThreads;
void Worker::RunJob(int count, std::function<void(int)> callback)
{
int numThreads = NumThreads;
if (numThreads <= 0)
numThreads = std::thread::hardware_concurrency();
if (numThreads <= 0)
numThreads = 4;
numThreads = std::min(numThreads, count);
std::vector<std::thread> threads;
for (int threadIndex = 0; threadIndex < numThreads; threadIndex++)
{
threads.push_back(std::thread([=]() {
for (int i = threadIndex; i < count; i += numThreads)
{
callback(i);
}
}));
}
for (int i = 0; i < numThreads; i++)
threads[i].join();
}

View file

@ -1,10 +0,0 @@
#pragma once
#include <functional>
class Worker
{
public:
static void RunJob(int count, std::function<void(int i)> callback);
};