Bicameral Engine
GPU-resident engine for a magic-research open-world game
Loading...
Searching...
No Matches
lab_box.h
Go to the documentation of this file.
1// lab_box.h — 実験室の箱(14 §2「実験室」・T-0142・ADR-0037)の CPU 側: コマンドを作る・箱を作る・CPU リファレンスの 1 刻み・
2// CPU と GPU の箱を比べて最初に違ったセルを探す・実験の記録(コマンドの列と刻みごとのハッシュ)の読み書き。GPU を知らない。
3//
4// データの流れ:
5// パネル(editor/lab_panel)が置く操作を Command(LAB_COMMAND_TYPE_*。common/lab_box.hlsli)にする
6// → CPU: StepLabBox(コマンドを当てる → StepNest。反応 + 熱の伝導)/ GPU: sim/gpu_lab_box(同じ関数を GPU で)
7// → FindLabMismatch で刻みごとに比べる。食い違えば最初の刻みとセル。
8// 記録(LabRecording)はコマンドの列とハッシュの列だけ(初めの箱は決まっている: 300 K の空気)。読み直して流せば同じ実験になる。
9// 箱は多重解像度の木の本物の根 1 つ(8³ セル、0.5 m 角 = 4 m 角。外とは熱も物も通さない)。仮の世界(ProbeSim)とは別の世界。
10// 浮動小数点は使わない(engine/src/sim は検査の対象。04 §4)。
11#pragma once
12
13#include <cstddef>
14#include <cstdint>
15#include <expected>
16#include <optional>
17#include <span>
18#include <string>
19#include <string_view>
20#include <vector>
21
22#include "common/lab_box.hlsli"
23#include "sim/command.h"
24#include "sim/multires_nest.h"
25#include "sim/reaction_table.h"
26
27namespace bicameral::sim {
28
29 inline constexpr uint32_t LAB_BOX_SLOT = 0; // 箱の根の枠
30 inline constexpr uint32_t LAB_BOX_EDGE = multires::MR_BLOCK_EDGE;
31 inline constexpr uint32_t LAB_MAX_COMMANDS_PER_TICK = 64; // 1 刻みに当てるコマンドの上限(GPU の写しの大きさ)
32 inline constexpr uint64_t LAB_WORLD_SEED = 20261009;
33 inline constexpr int32_t LAB_INITIAL_TEMPERATURE_MILLIKELVIN = 300000;
34
35 // 箱の刻み: 反応(待ちの丸め)+ 熱の伝導(17 §4。陰解法・小刻みは使わない: 箱は 1 レベルだけ)
36 inline constexpr MultiresStepOptions LAB_STEP_OPTIONS = {.conduction = true};
37
39 uint32_t x = 0;
40 uint32_t y = 0;
41 uint32_t z = 0;
42
43 [[nodiscard]] uint32_t Index() const { return multires::MrCellIndex(x, y, z); }
44 };
45
46 // --- コマンド ---
47
48 // セルの中身を物質(LAB_MAX_FILL_SPECIES まで)と温度で置き換える
49 [[nodiscard]] Command MakeLabFillCommand(uint64_t targetTick, uint32_t sequence, LabCellPosition cell,
50 std::span<const SpeciesAmount> contents, uint32_t temperatureMilliKelvin);
51
52 // セルの温度を決める(成分はそのまま)
53 [[nodiscard]] Command MakeLabTemperatureCommand(uint64_t targetTick, uint32_t sequence, LabCellPosition cell,
54 uint32_t temperatureMilliKelvin);
55
56 // 64 バイトをそのまま uint32 × 16 で読む(GPU に写す形と同じ)
57 [[nodiscard]] lab::LabCommand ToLabCommand(const Command& command);
58
59 // --- 試験の表の材料(パネルの「置く」の選択肢。tests/packages/combustion_test と同じ物質の名前)---
60
61 struct LabMaterial {
62 std::string_view name; // 表示の名前
63 std::vector<SpeciesAmount> contents;
64 };
65
66 // 空気・木(セルロース 1 割 + 孔の空気。仮の世界の木箱の壁と同じ)・木炭・二酸化炭素・窒素。表に無い物質の材料は入らない
67 [[nodiscard]] std::vector<LabMaterial> MakeLabMaterials(const BakedReactionTable& table);
68
69 // --- 箱 ---
70
71 [[nodiscard]] MultiresCapacity LabBoxCapacity(const BakedReactionTable& table);
72
73 // 300 K の空気を満たした箱(根はいつも頁を持つ。GPU のコマンドは頁のセルに書くため)
74 [[nodiscard]] MultiresNest MakeLabBoxNest(const BakedReactionTable& table);
75
76 // 箱のセル index(MrCellIndex)
77 [[nodiscard]] reaction::RxCell LabBoxCell(const MultiresNest& nest, uint32_t index);
78
79 // CPU リファレンス: 1 刻みのコマンド(並びは (targetTick, sequence) の昇順)を shaders/sim/lab_box.hlsl と同じ順で当てる。
80 // 当てた数を返す(当てられないコマンドは飛ばす)
81 uint32_t ApplyLabCommands(MultiresNest& nest, const BakedReactionTable& table, std::span<const Command> commands);
82
83 // CPU リファレンスの 1 刻み: コマンドを当てる → StepNest(LAB_STEP_OPTIONS)
84 void StepLabBox(MultiresNest& nest, const BakedReactionTable& table, uint64_t tick,
85 std::span<const Command> commands);
86
87 // --- 比べる ---
88
89 struct LabMismatch {
90 uint64_t tick = 0;
91 uint32_t
92 cell = lab::LAB_NO_CELL; // 最初に違ったセル(LAB_NO_CELL ならセルは同じで、見出し・端数・帳簿などが違う)
93 reaction::RxCell cpu{};
94 reaction::RxCell gpu{};
95 std::string what; // 表示用の説明
96 };
97
98 // 状態の全部(HashWholeNest)が違えば、最初に違ったセル(番号の順)か、セル以外の欄の名前
99 [[nodiscard]] std::optional<LabMismatch> FindLabMismatch(const MultiresNest& cpu, const MultiresNest& gpu,
100 uint64_t tick);
101
102 // --- 実験の記録(コマンドの列 + 刻みごとの状態のハッシュ)---
103
105 uint64_t tickCount = 0; // 流した刻みの数(刻み 0 〜 tickCount − 1)
106 std::vector<Command> commands; // (targetTick, sequence) の昇順
107 std::vector<uint64_t> hashes; // 刻み t を終えた状態の HashWholeNest(CPU)。tickCount 個
108 };
109
110 [[nodiscard]] std::vector<std::byte> SerializeLabRecording(const LabRecording& recording);
111 [[nodiscard]] std::expected<LabRecording, std::string> ParseLabRecording(std::span<const std::byte> bytes);
112
113} // namespace bicameral::sim
std::expected< LabRecording, std::string > ParseLabRecording(std::span< const std::byte > bytes)
Definition lab_box.cpp:271
constexpr MultiresStepOptions LAB_STEP_OPTIONS
Definition lab_box.h:36
RxCell LabBoxCell(const MultiresNest &nest, uint32_t index)
Definition lab_box.cpp:179
constexpr uint32_t LAB_BOX_SLOT
Definition lab_box.h:29
constexpr uint32_t LAB_MAX_COMMANDS_PER_TICK
Definition lab_box.h:31
Command MakeLabFillCommand(uint64_t targetTick, uint32_t sequence, LabCellPosition cell, std::span< const SpeciesAmount > contents, uint32_t temperatureMilliKelvin)
Definition lab_box.cpp:85
std::vector< LabMaterial > MakeLabMaterials(const BakedReactionTable &table)
Definition lab_box.cpp:122
void StepLabBox(MultiresNest &nest, const BakedReactionTable &table, uint64_t tick, std::span< const Command > commands)
Definition lab_box.cpp:208
constexpr uint32_t LAB_BOX_EDGE
Definition lab_box.h:30
Command MakeLabTemperatureCommand(uint64_t targetTick, uint32_t sequence, LabCellPosition cell, uint32_t temperatureMilliKelvin)
Definition lab_box.cpp:107
uint32_t ApplyLabCommands(MultiresNest &nest, const BakedReactionTable &table, std::span< const Command > commands)
Definition lab_box.cpp:183
MultiresNest MakeLabBoxNest(const BakedReactionTable &table)
Definition lab_box.cpp:162
constexpr uint64_t LAB_WORLD_SEED
Definition lab_box.h:32
std::vector< std::byte > SerializeLabRecording(const LabRecording &recording)
Definition lab_box.cpp:254
std::optional< LabMismatch > FindLabMismatch(const MultiresNest &cpu, const MultiresNest &gpu, uint64_t tick)
Definition lab_box.cpp:216
constexpr int32_t LAB_INITIAL_TEMPERATURE_MILLIKELVIN
Definition lab_box.h:33
lab::LabCommand ToLabCommand(const Command &command)
Definition lab_box.cpp:112
MultiresCapacity LabBoxCapacity(const BakedReactionTable &table)
Definition lab_box.cpp:152
std::vector< SpeciesAmount > contents
Definition lab_box.h:63
std::string_view name
Definition lab_box.h:62
reaction::RxCell gpu
Definition lab_box.h:94
reaction::RxCell cpu
Definition lab_box.h:93
std::vector< uint64_t > hashes
Definition lab_box.h:107
std::vector< Command > commands
Definition lab_box.h:106