Bicameral Engine
GPU-resident engine for a magic-research open-world game
Loading...
Searching...
No Matches
resources.h
Go to the documentation of this file.
1// resources.h — バッファ・ルート署名・パイプライン・シェーダーのファイルを作る小さな関数(T-0013)。
2// 失敗したら理由をログ(Channel::Gpu)に出し、nullptr か unexpected を返す。
3// シェーダーはビルドが bin/shaders/<sim|render>/*.cso に置く(shaders/CMakeLists.txt)。exe の横から読む。
4#pragma once
5
6#include <cstddef>
7#include <cstdint>
8#include <expected>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13
14#include "gpu/com_ptr.h"
15
16namespace bicameral::gpu {
17
18 enum class BufferKind : uint8_t {
19 UnorderedAccess, // GPU だけが読み書きする(既定のヒープ)。COMMON で作るので、最初の使用で UAV に暗黙に昇格する
20 Readback, // GPU → CPU の読み戻し。COPY_DEST のまま使う
21 Upload, // CPU → GPU(アップロードのヒープ)。GENERIC_READ のまま使い、CPU は Map したまま書く(T-0004)
22 };
23
24 [[nodiscard]] ComPtr<ID3D12Resource> CreateBuffer(ID3D12Device* device, uint64_t sizeBytes, BufferKind kind);
25
26 // 「ルートの UAV」とルート定数だけのルート署名の形。記述子のヒープが要らない一番簡単な形。
27 // ルートの番号は並び順: u0..u(uavCount-1)(space0)→ b0 のルート定数(あれば)→ デバッグのリング(あれば)
28 // → Work Graphs のカウンタ(あれば)→ 連鎖のトレース(あれば)→ t0..t(srvCount-1)(space0。ルートの SRV。T-0004)
29 // 見える範囲は全部のステージ(compute でも描画でも同じ形を使う)。
31 uint32_t uavCount = 0; // space0 の u0 から
32 uint32_t rootConstantCount = 0; // b0 の 32bit の値の数。0 なら無し
33 bool debugRing = false; // u0 space1 のデバッグのリング(shaders/common/debug_ring.hlsli、T-0003)
34 bool graphStats = false; // u1 space1 の Work Graphs のカウンタ(shaders/common/work_graph_stats.hlsli、T-0008)
35 bool graphTrace = false; // u2 space1 の連鎖のトレース(shaders/common/graph_trace.hlsli、T-0087)
36 uint32_t srvCount = 0; // space0 の t0 から(バッファだけ)
37
38 [[nodiscard]] uint32_t RootConstantIndex() const { return uavCount; }
39 [[nodiscard]] uint32_t DebugRingIndex() const { return uavCount + (rootConstantCount > 0 ? 1 : 0); }
40 [[nodiscard]] uint32_t GraphStatsIndex() const { return DebugRingIndex() + (debugRing ? 1 : 0); }
41 [[nodiscard]] uint32_t GraphTraceIndex() const { return GraphStatsIndex() + (graphStats ? 1 : 0); }
42 [[nodiscard]] uint32_t SrvIndex(uint32_t shaderRegister) const {
43 return GraphTraceIndex() + (graphTrace ? 1 : 0) + shaderRegister;
44 }
45 };
46
47 [[nodiscard]] ComPtr<ID3D12RootSignature> CreateRootSignature(ID3D12Device* device,
48 const RootSignatureLayout& layout);
49
50 [[nodiscard]] ComPtr<ID3D12PipelineState> CreateComputePipeline(ID3D12Device* device,
51 ID3D12RootSignature* rootSignature,
52 std::span<const std::byte> bytecode);
53
54 // exe の横の shaders/<relativePath> を読む。relativePath の例: "sim/fixed_selftest.cso"
55 [[nodiscard]] std::expected<std::vector<std::byte>, std::string> LoadShader(std::string_view relativePath);
56
57 // 読み戻しのバッファの先頭から destination の大きさだけ写す
58 [[nodiscard]] bool ReadBuffer(ID3D12Resource* readback, std::span<std::byte> destination);
59
60 // リソース全体の状態の遷移
61 [[nodiscard]] D3D12_RESOURCE_BARRIER Transition(ID3D12Resource* resource, D3D12_RESOURCE_STATES before,
62 D3D12_RESOURCE_STATES after);
63
64 // UAV の書き込みを次の読み書きより前に終わらせる
65 [[nodiscard]] D3D12_RESOURCE_BARRIER UavBarrier(ID3D12Resource* resource);
66
67 // UAV で書いたバッファを読み戻しのバッファへ写すコマンドを記録する(UAV → COPY_SOURCE の遷移つき)
68 void RecordCopyToReadback(ID3D12GraphicsCommandList* list, ID3D12Resource* source, ID3D12Resource* readback);
69
70} // namespace bicameral::gpu
ComPtr< ID3D12RootSignature > CreateRootSignature(ID3D12Device *device, const RootSignatureLayout &layout)
Definition resources.cpp:93
std::expected< std::vector< std::byte >, std::string > LoadShader(std::string_view relativePath)
bool ReadBuffer(ID3D12Resource *readback, std::span< std::byte > destination)
Definition resources.cpp:52
ComPtr< ID3D12PipelineState > CreateComputePipeline(ID3D12Device *device, ID3D12RootSignature *rootSignature, std::span< const std::byte > bytecode)
D3D12_RESOURCE_BARRIER Transition(ID3D12Resource *resource, D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after)
Definition resources.cpp:65
ComPtr< ID3D12Resource > CreateBuffer(ID3D12Device *device, uint64_t sizeBytes, BufferKind kind)
Definition resources.cpp:18
void RecordCopyToReadback(ID3D12GraphicsCommandList *list, ID3D12Resource *source, ID3D12Resource *readback)
Definition resources.cpp:78
D3D12_RESOURCE_BARRIER UavBarrier(ID3D12Resource *resource)
Definition resources.cpp:74
uint32_t SrvIndex(uint32_t shaderRegister) const
Definition resources.h:42