Bicameral Engine
GPU-resident engine for a magic-research open-world game
Loading...
Searching...
No Matches
probe_view.cpp
Go to the documentation of this file.
1// probe_view.cpp — T-0004 の仮の世界を描く。使い方は probe_view.h。
2#include "render/probe_view.h"
3
4#include <array>
5
6#include "core/hresult.h"
7#include "core/log.h"
8#include "gpu/com_ptr.h"
9#include "gpu/resources.h"
10
11namespace bicameral::render {
12 namespace {
13
14 // t0〜t2 抽出の 3 組・t3 フレームの定数(probe_view.hlsl)
15 constexpr gpu::RootSignatureLayout ROOT_LAYOUT{.srvCount = 4};
16 constexpr std::array<float, 4> CLEAR_COLOR = {0.02f, 0.02f, 0.03f, 1.0f};
17
18 } // namespace
19
20 std::expected<ProbeView, std::string> ProbeView::Create(ID3D12Device* device, DXGI_FORMAT renderTargetFormat) {
21 ProbeView view;
22 view.m_rootSignature = gpu::CreateRootSignature(device, ROOT_LAYOUT);
23 if (!view.m_rootSignature)
24 return std::unexpected("描画のルート署名を作れない");
25
26 const auto vertexShader = gpu::LoadShader("render/probe_view_vs.cso");
27 const auto pixelShader = gpu::LoadShader("render/probe_view_ps.cso");
28 if (!vertexShader)
29 return std::unexpected(vertexShader.error());
30
31 if (!pixelShader)
32 return std::unexpected(pixelShader.error());
33
34 D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{
35 .pRootSignature = view.m_rootSignature.Get(),
36 .VS = {.pShaderBytecode = vertexShader->data(), .BytecodeLength = vertexShader->size()},
37 .PS = {.pShaderBytecode = pixelShader->data(), .BytecodeLength = pixelShader->size()},
38 .BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT),
39 .SampleMask = UINT_MAX,
40 .RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT),
41 .DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT),
42 .PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
43 .NumRenderTargets = 1,
44 .SampleDesc = {.Count = 1},
45 };
46
47 desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
48 desc.DepthStencilState.DepthEnable = FALSE; // 深度バッファは使わない
49 desc.RTVFormats[0] = renderTargetFormat;
50 const HRESULT result = device->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&view.m_pipeline));
51 if (FAILED(result))
52 return std::unexpected("描画のパイプラインを作れない: " + DescribeHresult(result));
53
54 return view;
55 }
56
57 void ProbeView::Record(ID3D12GraphicsCommandList* list, const ProbeViewTarget& target,
58 const ProbeViewInputs& inputs) const {
59 const D3D12_RESOURCE_BARRIER toRenderTarget = gpu::Transition(target.backBuffer, D3D12_RESOURCE_STATE_PRESENT,
60 D3D12_RESOURCE_STATE_RENDER_TARGET);
61 list->ResourceBarrier(1, &toRenderTarget);
62
63 // --- 描く ---
64 list->OMSetRenderTargets(1, &target.renderTargetView, FALSE, nullptr);
65 list->ClearRenderTargetView(target.renderTargetView, CLEAR_COLOR.data(), 0, nullptr);
66 const D3D12_VIEWPORT viewport{
67 .Width = static_cast<float>(target.width), .Height = static_cast<float>(target.height), .MaxDepth = 1.0f};
68 const D3D12_RECT scissor{
69 .left = 0, .top = 0, .right = static_cast<LONG>(target.width), .bottom = static_cast<LONG>(target.height)};
70 list->RSSetViewports(1, &viewport);
71 list->RSSetScissorRects(1, &scissor);
72 list->SetGraphicsRootSignature(m_rootSignature.Get());
73 list->SetGraphicsRootShaderResourceView(ROOT_LAYOUT.SrvIndex(0), inputs.extraction0);
74 list->SetGraphicsRootShaderResourceView(ROOT_LAYOUT.SrvIndex(1), inputs.extraction1);
75 list->SetGraphicsRootShaderResourceView(ROOT_LAYOUT.SrvIndex(2), inputs.extraction2);
76 list->SetGraphicsRootShaderResourceView(ROOT_LAYOUT.SrvIndex(3), inputs.constants);
77 list->SetPipelineState(m_pipeline.Get());
78 list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
79 list->DrawInstanced(3, 1, 0, 0);
80
81 const D3D12_RESOURCE_BARRIER toPresent = gpu::Transition(target.backBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET,
82 D3D12_RESOURCE_STATE_PRESENT);
83 list->ResourceBarrier(1, &toPresent);
84 }
85
86} // namespace bicameral::render
void Record(ID3D12GraphicsCommandList *list, const ProbeViewTarget &target, const ProbeViewInputs &inputs) const
static std::expected< ProbeView, std::string > Create(ID3D12Device *device, DXGI_FORMAT renderTargetFormat)
ComPtr< ID3D12RootSignature > m_rootSignature
Definition probe_view.h:45
ComPtr< ID3D12PipelineState > m_pipeline
Definition probe_view.h:46
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)
D3D12_RESOURCE_BARRIER Transition(ID3D12Resource *resource, D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after)
Definition resources.cpp:65
constexpr gpu::RootSignatureLayout ROOT_LAYOUT
std::string DescribeHresult(HResult result)
Definition hresult.cpp:88
uint32_t SrvIndex(uint32_t shaderRegister) const
Definition resources.h:42
D3D12_GPU_VIRTUAL_ADDRESS extraction2
Definition probe_view.h:29
D3D12_GPU_VIRTUAL_ADDRESS constants
Definition probe_view.h:30
D3D12_GPU_VIRTUAL_ADDRESS extraction0
Definition probe_view.h:27
D3D12_GPU_VIRTUAL_ADDRESS extraction1
Definition probe_view.h:28
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView
Definition probe_view.h:21