载入中...
搜索中...
未找到
RuntimeGeneration.h
浏览该文件的文档.
1#pragma once
2
4
5#include <cstdint>
6#include <deque>
7#include <functional>
8#include <optional>
9#include <string>
10#include <thread>
11#include <unordered_map>
12#include <vector>
13
14namespace eve::procgen {
15
18public:
20 int getLevel() const;
22 int getX() const;
24 int getZ() const;
26 uint32_t getSeed() const;
28 uint64_t getTicket() const;
30 float getMinX() const;
32 float getMinZ() const;
34 float getMaxX() const;
36 float getMaxZ() const;
37
38private:
39 friend class RuntimeGeneration;
40 int level_ = 0;
41 int x_ = 0;
42 int z_ = 0;
43 uint32_t seed_ = 1;
44 uint64_t ticket_ = 0;
45 uint64_t schedulerId_ = 0;
46 float cellSize_ = 1.f;
47};
48
56public:
58 int getLevel() const;
60 int getX() const;
62 int getZ() const;
64 uint32_t getSeed() const;
66 uint64_t getTicket() const;
68 float getMinX() const;
70 float getMinZ() const;
72 float getMaxX() const;
74 float getMaxZ() const;
75
76private:
77 friend class RuntimeGeneration;
78 int level_ = 0;
79 int x_ = 0;
80 int z_ = 0;
81 uint32_t seed_ = 1;
82 uint64_t ticket_ = 0;
83 uint64_t schedulerId_ = 0;
84 float cellSize_ = 1.f;
85};
86
103
116public:
118 explicit RuntimeGeneration(uint32_t worldSeed = 1);
123
125 void clear();
133 int addLevel(float cellSize, float generationRadius, float cleanupMultiplier);
135 int getLevelCount() const;
137 float getLevelCellSize(int level) const;
139 float getLevelGenerationRadius(int level) const;
141 float getLevelCleanupRadius(int level) const;
142
144 void setDirectionWeight(float weight);
146 float getDirectionWeight() const;
148 void setMaxGenerating(int count);
150 int getMaxGenerating() const;
152 void setMaxActiveCells(int count);
154 int getMaxActiveCells() const;
156 void setMaxPointsPerCell(int count);
158 int getMaxPointsPerCell() const;
160 void setMaxResidentPoints(int count);
162 int getMaxResidentPoints() const;
164 int getResidentPointCount() const;
166 int getRejectedOutputCount() const;
171 int trimToResidentPoints(int target);
173 void setMaxGenerationRetries(int count);
175 int getMaxGenerationRetries() const;
177 void setFrameTimeBudget(float milliseconds);
179 float getFrameTimeBudget() const;
181 void beginFrame();
192 void setRefreshWorkBudget(int candidateCells);
194 int getRefreshWorkBudget() const;
196 bool isRefreshPending() const;
198 uint64_t getCommittedRefreshRevision() const;
206 [[nodiscard]] Result<uint64_t> continueGenerationRefresh();
207
213 void updateSource(float x, float z, float directionX, float directionZ);
215 bool setGenerationSource(const std::string& id, float x, float z, float directionX,
216 float directionZ, float radiusScale = 1.f);
218 bool removeGenerationSource(const std::string& id);
220 void clearGenerationSources();
222 int getGenerationSourceCount() const;
224 std::string getGenerationSourceId(int index) const;
226 void refreshGenerationSources();
227
233 void setFrustumCulling(bool enabled, float halfAngleDegrees, float behindRadius);
235 bool isFrustumCullingEnabled() const;
237 float getFrustumHalfAngle() const;
239 float getFrustumBehindRadius() const;
240
242 int getPendingGenerateCount() const;
244 int getGeneratingCount() const;
246 int getActiveCellCount() const;
248 int getPendingCleanupCount() const;
250 int getCancelledGenerationCount() const;
252 int getFailedCellCount() const;
254 int retryFailedCells();
261 [[nodiscard]] Result<std::optional<ProcgenGenerationJob>> nextGenerationJob();
269 [[nodiscard]] Result<uint64_t> completeGenerationJob(ProcgenGenerationCompletion completion);
277 [[nodiscard]] Result<void> failGenerationJob(const ProcgenGenerationJob& job);
284 [[nodiscard]] Result<std::optional<ProcgenCellRequest>> nextCleanupRequest();
291 [[nodiscard]] Result<uint64_t> completeCleanupRequest(const ProcgenCellRequest& request);
293 bool isOwnerThread() const noexcept;
294
296 ProcgenCellRequest* nextGenerate();
298 ProcgenCellRequest* nextCleanup();
305 bool isRequestCurrent(const ProcgenCellRequest* request) const;
307 bool completeGeneration(ProcgenCellRequest* request, PointSet* output);
309 bool failGeneration(ProcgenCellRequest* request);
311 bool completeCleanup(ProcgenCellRequest* request);
324 [[nodiscard]] Result<uint64_t> completeCleanupsAtomic(const std::vector<const ProcgenCellRequest*>& requests);
325
326 bool hasCell(int level, int x, int z) const;
328 PointSet* getCellOutput(int level, int x, int z) const;
330 uint64_t getCellRevision(int level, int x, int z) const;
337 [[nodiscard]] Result<uint64_t> applyCellUpdate(int level, int x, int z, uint64_t expectedRevision,
338 const PointSet& output);
343 [[nodiscard]] Result<uint64_t> migrateCellPointIds(int level, int x, int z, uint64_t expectedRevision);
349 PointDelta* getCellDelta(int level, int x, int z) const;
351 std::string serializeCell(int level, int x, int z) const;
353 bool deserializeCell(const std::string& definition);
355 std::string debugReport() const;
356
357private:
358 friend class Procgen;
359
360 struct Level {
361 float cellSize = 1.f;
362 float generationRadius = 1.f;
363 float cleanupRadius = 1.f;
364 };
365 enum class State { Pending, Generating, Active, Cleanup, Failed };
366 struct CellKey {
367 int level = 0;
368 int x = 0;
369 int z = 0;
371 bool operator==(const CellKey& other) const {
372 return level == other.level && x == other.x && z == other.z;
373 }
374 };
375 struct CellKeyHash {
377 size_t operator()(const CellKey& key) const;
378 };
379 struct Cell {
380 State state = State::Pending;
381 float priority = 0.f;
382 uint64_t revision = 0;
383 uint64_t ticket = 0;
384 int failures = 0;
385 bool trimmed = false;
387 PointDelta delta;
388 bool hasDelta = false;
389 };
390 struct Source {
391 float x = 0.f;
392 float z = 0.f;
393 float directionX = 0.f;
394 float directionZ = 0.f;
395 float radiusScale = 1.f;
396 };
397 struct RefreshPlan {
398 uint64_t revision = 0;
399 std::vector<Level> levels;
400 std::vector<Source> sources;
401 std::unordered_map<CellKey, float, CellKeyHash> desiredPriorities;
402 float directionWeight = 0.f;
403 float frustumHalfAngle = 180.f;
404 float coneCosine = -1.f;
405 float frustumBehindRadius = 0.f;
406 bool frustumCulling = false;
407 size_t sourceIndex = 0;
408 size_t levelIndex = 0;
409 int minX = 0;
410 int maxX = -1;
411 int minZ = 0;
412 int maxZ = -1;
413 int cellX = 0;
414 int cellZ = 0;
415 bool rangeReady = false;
416 };
417
418 ProcgenCellRequest makeRequest(const CellKey& key) const;
419 ProcgenGenerationJob makeGenerationJob(const CellKey& key) const;
420 std::optional<ProcgenGenerationJob> issueGenerationJob();
421 [[nodiscard]] Result<std::reference_wrapper<Cell>> validateGenerationJob(const ProcgenGenerationJob& job);
422 void transitionCellState(Cell& cell, State nextState);
423 void requestGenerationRefresh();
424 void startRefreshPlan();
425 uint64_t advanceRefreshPlan(uint64_t candidateBudget);
426 void commitRefreshPlan(const RefreshPlan& plan);
427 [[nodiscard]] Result<void> validateCleanups(const std::vector<const ProcgenCellRequest*>& requests) const;
428 void eraseValidatedCleanups(const std::vector<const ProcgenCellRequest*>& requests);
429 uint32_t cellSeed(const CellKey& key) const;
430 void sortQueues();
431
432 uint32_t worldSeed_ = 1;
433 std::thread::id ownerThread_;
434 const uint64_t schedulerId_;
435 float directionWeight_ = 0.25f;
436 int maxGenerating_ = 4;
437 int maxActiveCells_ = 0;
438 int maxPointsPerCell_ = 0;
439 int maxResidentPoints_ = 0;
440 int rejectedOutputCount_ = 0;
441 int cancelledGenerationCount_ = 0;
442 int maxGenerationRetries_ = 3;
443 float frameTimeBudgetMs_ = 0.f;
444 uint64_t frameStartedNs_ = 0;
445 int refreshWorkBudget_ = 0;
446 uint64_t sourceRevision_ = 0;
447 uint64_t committedRefreshRevision_ = 0;
448 std::optional<RefreshPlan> refreshPlan_;
449 bool frustumCulling_ = false;
450 float frustumHalfAngle_ = 60.f;
451 float frustumBehindRadius_ = 0.f;
452 std::vector<Level> levels_;
453 std::unordered_map<std::string, Source> sources_;
454 std::vector<std::string> sourceOrder_;
455 std::unordered_map<CellKey, Cell, CellKeyHash> cells_;
456 std::deque<CellKey> generateQueue_;
457 std::deque<CellKey> cleanupQueue_;
458 int generatingCount_ = 0;
459 int activeCellCount_ = 0;
460 uint64_t nextTicket_ = 0;
461};
462
463} // namespace eve::procgen
LogicalId target
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
std::string output
int priority
#define EVENGINE_API_DOMAINS
Definition Export.h:110
const GltfImportRequest & request
std::uint32_t key
int level
double & milliseconds
Definition OnnxGpgpu.cpp:19
std::uint64_t revision
std::uint32_t count
Cell cell
LogicalId definition
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Script-friendly collection of attributed 3D samples.
Definition PointSet.h:59
One immutable runtime-generation cell request returned to script.
Owning worker result returned to a RuntimeGeneration scheduler.
Copyable generation input issued by one RuntimeGeneration scheduler.
Procedural generation module. Phase A: runtime maps (semantic Grid2D → TileLayer)....
Definition Procgen.h:146
View-driven partition and hierarchical procedural generation scheduler.
RuntimeGeneration & operator=(RuntimeGeneration &&)=delete
RuntimeGeneration & operator=(const RuntimeGeneration &)=delete
RuntimeGeneration(RuntimeGeneration &&)=delete
RuntimeGeneration(const RuntimeGeneration &)=delete
@ Cell
A cell was removed; the out-parameter holds it.
State
Lifecycle state of a generic transaction plan.
bool enabled
Transactional identity-based change set between two point snapshots.
Definition PointDelta.h:16