-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSurfaceRegistry.h
More file actions
229 lines (195 loc) · 6.42 KB
/
SurfaceRegistry.h
File metadata and controls
229 lines (195 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#pragma once
#include <memory>
#include <shared_mutex>
#include <unordered_map>
#include "webgpu/webgpu_cpp.h"
namespace rnwgpu {
struct NativeInfo {
void *nativeSurface;
int width;
int height;
};
struct Size {
int width;
int height;
};
class SurfaceInfo {
public:
SurfaceInfo(wgpu::Instance gpu, int width, int height)
: gpu(std::move(gpu)), width(width), height(height) {}
~SurfaceInfo() { surface = nullptr; }
void reconfigure(int newWidth, int newHeight) {
std::unique_lock<std::shared_mutex> lock(_mutex);
config.width = newWidth;
config.height = newHeight;
_configure();
}
void configure(wgpu::SurfaceConfiguration &newConfig) {
std::unique_lock<std::shared_mutex> lock(_mutex);
config = newConfig;
config.width = width;
config.height = height;
config.presentMode = wgpu::PresentMode::Fifo;
_configure();
}
void unconfigure() {
std::unique_lock<std::shared_mutex> lock(_mutex);
if (surface) {
surface.Unconfigure();
} else {
texture = nullptr;
}
}
void *switchToOffscreen() {
std::unique_lock<std::shared_mutex> lock(_mutex);
// We only do this if the onscreen surface is configured.
auto isConfigured = config.device != nullptr;
if (isConfigured) {
wgpu::TextureDescriptor textureDesc;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::TextureBinding;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
texture = config.device.CreateTexture(&textureDesc);
}
surface = nullptr;
return nativeSurface;
}
void switchToOnscreen(void *newNativeSurface, wgpu::Surface newSurface) {
std::unique_lock<std::shared_mutex> lock(_mutex);
nativeSurface = newNativeSurface;
surface = std::move(newSurface);
// If we are comming from an offscreen context, we need to configure the new
// surface
if (texture != nullptr) {
config.usage = config.usage | wgpu::TextureUsage::CopyDst;
_configure();
// We flush the offscreen texture to the onscreen one
// TODO: there is a faster way to do this without validation?
wgpu::CommandEncoderDescriptor encoderDesc;
auto device = config.device;
wgpu::CommandEncoder encoder = device.CreateCommandEncoder(&encoderDesc);
wgpu::TexelCopyTextureInfo sourceTexture = {};
sourceTexture.texture = texture;
wgpu::TexelCopyTextureInfo destinationTexture = {};
wgpu::SurfaceTexture surfaceTexture;
surface.GetCurrentTexture(&surfaceTexture);
destinationTexture.texture = surfaceTexture.texture;
wgpu::Extent3D size = {sourceTexture.texture.GetWidth(),
sourceTexture.texture.GetHeight(),
sourceTexture.texture.GetDepthOrArrayLayers()};
encoder.CopyTextureToTexture(&sourceTexture, &destinationTexture, &size);
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device.GetQueue();
queue.Submit(1, &commands);
surface.Present();
texture = nullptr;
}
}
void resize(int newWidth, int newHeight) {
std::unique_lock<std::shared_mutex> lock(_mutex);
width = newWidth;
height = newHeight;
}
void present() {
std::unique_lock<std::shared_mutex> lock(_mutex);
if (surface) {
surface.Present();
}
}
wgpu::Texture getCurrentTexture() {
std::shared_lock<std::shared_mutex> lock(_mutex);
if (surface) {
wgpu::SurfaceTexture surfaceTexture;
surface.GetCurrentTexture(&surfaceTexture);
return surfaceTexture.texture;
} else {
return texture;
}
}
NativeInfo getNativeInfo() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return {.nativeSurface = nativeSurface, .width = width, .height = height};
}
Size getSize() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return {.width = width, .height = height};
}
wgpu::SurfaceConfiguration getConfig() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return config;
}
wgpu::Device getDevice() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return config.device;
}
private:
void _configure() {
if (surface) {
surface.Configure(&config);
} else {
wgpu::TextureDescriptor textureDesc;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::TextureBinding;
texture = config.device.CreateTexture(&textureDesc);
}
}
mutable std::shared_mutex _mutex;
void *nativeSurface = nullptr;
wgpu::Surface surface = nullptr;
wgpu::Texture texture = nullptr;
wgpu::Instance gpu;
wgpu::SurfaceConfiguration config;
int width;
int height;
};
class SurfaceRegistry {
public:
static SurfaceRegistry &getInstance() {
static SurfaceRegistry instance;
return instance;
}
SurfaceRegistry(const SurfaceRegistry &) = delete;
SurfaceRegistry &operator=(const SurfaceRegistry &) = delete;
std::shared_ptr<SurfaceInfo> getSurfaceInfo(int id) {
std::shared_lock<std::shared_mutex> lock(_mutex);
auto it = _registry.find(id);
if (it != _registry.end()) {
return it->second;
}
return nullptr;
}
void removeSurfaceInfo(int id) {
std::unique_lock<std::shared_mutex> lock(_mutex);
_registry.erase(id);
}
std::shared_ptr<SurfaceInfo> addSurfaceInfo(int id, wgpu::Instance gpu,
int width, int height) {
std::unique_lock<std::shared_mutex> lock(_mutex);
auto info = std::make_shared<SurfaceInfo>(gpu, width, height);
_registry[id] = info;
return info;
}
std::shared_ptr<SurfaceInfo>
getSurfaceInfoOrCreate(int id, wgpu::Instance gpu, int width, int height) {
std::unique_lock<std::shared_mutex> lock(_mutex);
auto it = _registry.find(id);
if (it != _registry.end()) {
return it->second;
}
auto info = std::make_shared<SurfaceInfo>(gpu, width, height);
_registry[id] = info;
return info;
}
private:
SurfaceRegistry() = default;
mutable std::shared_mutex _mutex;
std::unordered_map<int, std::shared_ptr<SurfaceInfo>> _registry;
};
} // namespace rnwgpu