TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
d3d12_window_context.h
Go to the documentation of this file.
1#pragma once
2
3// Inside the Direct3D 12 backend: an OS window's rendering. Each frame draws into a texture of
4// the frame slot's and copies it to the swapchain's buffer, as the Vulkan backend does, so the
5// last frame can be read back after it's presented. A layered window (a transparent one on
6// Windows) has no swapchain: its frame is read back and shown with UpdateLayeredWindow. A
7// render-to-image window has no OS window at all: its frames are only read back.
8
13
14#include <array>
15#include <memory>
16#include <optional>
17#include <span>
18#include <vector>
19
20namespace tachyon::d3d12
21{
22 class D3D12WindowContext final : public IWindowContext
23 {
24 public:
27 static D3D12WindowContext* Create(D3D12GraphicsContext& graphics, HWND window, OSWindowHandle osHandle, ID id,
28 int2 size, WindowFlags flags, OnGuiFunc onGui);
30
31 ID GetID() const override { return m_id; }
32 OSWindowHandle GetWindowHandle() const override { return m_osHandle; }
33 bool IsTransparent() const override;
34
37 void RenderWindowFrame(GraphicsContext* graphicsContext, IntRect rect, bool resizing) override;
38 void RecordWindowFrame(GraphicsContext* graphicsContext, IntRect rect) override;
39 std::optional<double> PresentWindowFrame() override;
40 bool ReadPixels(std::span<uint8_t> bgra) override;
41
42 private:
43 static constexpr uint32_t kFrames = 2;
44
45 struct Frame
46 {
47 ComPtr<ID3D12CommandAllocator> allocator;
48 ComPtr<ID3D12Resource> drawTexture;
49 D3D12_RESOURCE_STATES drawState = D3D12_RESOURCE_STATE_RENDER_TARGET;
51 ComPtr<ID3D12DescriptorHeap> images;
52 FrameArena arena;
53 UINT64 fenceValue = 0;
54 };
55
56 D3D12WindowContext(D3D12GraphicsContext& graphics, HWND window, OSWindowHandle osHandle, ID id, WindowFlags flags, OnGuiFunc onGui);
57 void Record(IntRect rect, bool resizing);
58 bool MakeSizedResources(uint32_t width, uint32_t height);
59 void ReleaseSizedResources();
60 void Draw(Frame& frame, D3D12_CPU_DESCRIPTOR_HANDLE rtv);
62 void CopyToTarget(Frame& frame, ID3D12Resource* target);
64 float ClearAlpha() const { return m_layered || m_toImage ? 0.0f : 1.0f; }
65
66 D3D12GraphicsContext& m_graphics;
67 D3D12GraphicsContext::Impl& m_device;
68 HWND m_window;
69 OSWindowHandle m_osHandle;
70 ID m_id;
71 WindowFlags m_flags;
72 OnGuiFunc m_onGui;
75 std::shared_ptr<bool> m_alive = std::make_shared<bool>(true);
76
79 bool m_toImage = false;
81 bool m_layered = false;
82 std::vector<uint8_t> m_layeredPixels;
83 uint2 m_layeredSize {};
84 ComPtr<IDXGISwapChain3> m_swapchain;
85 std::array<ComPtr<ID3D12Resource>, kFrames> m_buffers;
86 ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
87 UINT m_rtvSize = 0;
89 ComPtr<ID3D12Resource> m_depth;
90 ComPtr<ID3D12DescriptorHeap> m_dsvHeap;
91 ComPtr<ID3D12GraphicsCommandList> m_list;
92 std::array<Frame, kFrames> m_frames;
93 uint32_t m_frameIndex = 0;
94 uint32_t m_width = 0, m_height = 0;
95 bool m_presentPending = false;
96 std::optional<uint32_t> m_lastFrame;
97
99 ComPtr<ID3D12CommandAllocator> m_readAllocator;
100 ComPtr<ID3D12GraphicsCommandList> m_readList;
101 ComPtr<ID3D12Resource> m_readback;
102 UINT64 m_readbackSize = 0;
103 };
104}
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:53
A window's rendering state: made by GraphicsContext::CreateWindowContext for each OS window,...
Definition gui_window_backend.h:72
Definition d3d12_graphics_context.h:67
Definition d3d12_window_context.h:23
static D3D12WindowContext * Create(D3D12GraphicsContext &graphics, HWND window, OSWindowHandle osHandle, ID id, int2 size, WindowFlags flags, OnGuiFunc onGui)
window is null for a render-to-image window.
ID GetID() const override
The GUI window's ID; the backend passes it with the window's input.
Definition d3d12_window_context.h:31
bool ReadPixels(std::span< uint8_t > bgra) override
The last rendered frame, BGRA8 with tightly packed rows; bgra holds width x height x 4 bytes of the w...
void RecordWindowFrame(GraphicsContext *graphicsContext, IntRect rect) override
Records and submits a frame without presenting it; PresentWindowFrame presents.
OSWindowHandle GetWindowHandle() const override
The window it was created for.
Definition d3d12_window_context.h:32
std::optional< double > PresentWindowFrame() override
May wait for vsync; nothing to do for a render-to-image window.
void RenderWindowFrame(GraphicsContext *graphicsContext, IntRect rect, bool resizing) override
resizing: a live resize's redraw, which lays the window out again (its onGui) before it's drawn at th...
bool IsTransparent() const override
Whether the window has WindowFlags::Transparent.
std::function< void()> OnGuiFunc
Definition gui_base_types.h:13
WindowFlags
How an OS window is made and behaves (IWindowBackend::CreateWindow); flags combine with |.
Definition gui_window_flags.h:14
void * OSWindowHandle
An OS window, opaque; what it points to is up to the window backend.
Definition gui_window_backend.h:37
Definition d3d12_common.h:22
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
Definition gui_base_types.h:406
A frame slot's transient data: one upload buffer, written from the start each time the slot comes rou...
Definition d3d12_rhi.h:18
Integer 2D vector.
Definition gui_base_types.h:18
Definition gui_base_types.h:74