8#if defined(__EMSCRIPTEN__)
9#include <emscripten/emscripten.h>
15 wgpu::PopErrorScopeStatus
status = wgpu::PopErrorScopeStatus::Error;
16 wgpu::ErrorType
type = wgpu::ErrorType::NoError;
25 explicit UploadScopes(
const wgpu::Device&
device) : device_(
device) {
26 for (
auto&
result : results_)
result = std::make_shared<UploadError>();
28 {wgpu::ErrorFilter::Validation, wgpu::ErrorFilter::OutOfMemory, wgpu::ErrorFilter::Internal}) {
29 device_.PushErrorScope(
filter);
36 WGPUPopErrorScopeCallbackInfo
callback{};
37 callback.mode = WGPUCallbackMode_AllowSpontaneous;
38 callback.callback = [](WGPUPopErrorScopeStatus, WGPUErrorType, WGPUStringView,
void*,
void*) {};
39 (void)wgpuDevicePopErrorScope(device_.Get(),
callback);
43 [[nodiscard]] Result<void> finish(
const wgpu::Instance& instance) {
44 std::array<wgpu::Future, 3> futures{};
45 for (
size_t i = 0; i < futures.size(); ++i) {
46 futures[i] = device_.PopErrorScope(
47#
if defined(__EMSCRIPTEN__)
48 wgpu::CallbackMode::AllowProcessEvents,
50 wgpu::CallbackMode::WaitAnyOnly,
52 [
result = results_[i]](wgpu::PopErrorScopeStatus
status, wgpu::ErrorType
type,
53 wgpu::StringView
message)
noexcept {
57 const auto length = message.length == WGPU_STRLEN ? std::strlen(message.data) : message.length;
58 std::memcpy(result->message.data(), message.data, std::min(length, result->message.size() - 1));
64 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(30);
65 for (
size_t i = 0; i < futures.size(); ++i) {
66#if defined(__EMSCRIPTEN__)
67 while (!results_[i]->
done && std::chrono::steady_clock::now() <
deadline) {
69 wgpuInstanceProcessEvents(instance.Get());
71 if (!results_[i]->
done)
73 "WebGPU mip upload error scope could not be observed",
74 {}, {},
"graphics.texture.mips"));
76 const auto remaining =
deadline - std::chrono::steady_clock::now();
78 std::max<int64_t>(0, std::chrono::duration_cast<std::chrono::nanoseconds>(remaining).
count());
79 if (instance.WaitAny(futures[i], uint64_t(timeout)) != wgpu::WaitStatus::Success)
81 "WebGPU mip upload error scope could not be observed",
82 {}, {},
"graphics.texture.mips"));
85 for (
const auto&
result : results_)
89 {},
"graphics.texture.mips"));
95 unsigned pending_ = 0;
96 std::array<std::shared_ptr<UploadError>, 3> results_;
101 std::span<const uint8_t> rgba) {
109 uint64_t expected = 0;
112 expected += uint64_t(
w) *
h * 4;
114 if (
w == 1 &&
h == 1)
break;
115 w = std::max(
w / 2, 1u);
116 h = std::max(
h / 2, 1u);
118 if (levels !=
count || expected != rgba.size() || expected > UINT32_MAX)
121 ownedTextures.reserve(ownedTextures.size() + 1);
122 ownedGpuTextures.reserve(ownedGpuTextures.size() + 1);
123 auto tex = std::make_unique<Texture>();
124 auto gpu = std::make_unique<GpuTexture>();
127 gpu->mipLevels = levels;
129 UploadScopes scopes(
device);
131 descriptor.dimension = wgpu::TextureDimension::e2D;
133 descriptor.format = wgpu::TextureFormat::RGBA8Unorm;
136 descriptor.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst;
139 auto checked = scopes.finish(instance);
147 wgpu::TexelCopyTextureInfo destination{};
148 destination.texture =
gpu->texture;
149 destination.mipLevel =
level;
150 wgpu::TexelCopyBufferLayout
layout{};
153 wgpu::Extent3D extent{
w,
h, 1};
154 const size_t bytes = size_t(
w) *
h * 4;
157 w = std::max(
w / 2, 1u);
158 h = std::max(
h / 2, 1u);
160 gpu->view =
gpu->texture.CreateView();
161 gpu->sampler = makeSampler(
gpu->samplerState, levels);
162 auto checked = scopes.finish(instance);
164 if (!
gpu->view || !
gpu->sampler)
166 tex->width = tex->pixelWidth = int(
width);
167 tex->height = tex->pixelHeight = int(
height);
168 tex->mipmapCount = int(levels);
169 tex->sampler =
gpu->samplerState;
170 tex->gpuHandle =
gpu.get();
172 ownedTextures.push_back(std::move(tex));
173 ownedGpuTextures.push_back(std::move(
gpu));
175 }
catch (
const std::exception&
error) {
181 std::span<const uint16_t> rgbaHalf) {
188 const uint64_t texels = uint64_t(
width) *
height * layers;
191 layers >
limits.maxTextureArrayLayers || texels > SIZE_MAX / 4u || rgbaHalf.size() != texels * 4u)
194 ownedTextures.reserve(ownedTextures.size() + 1u);
195 ownedGpuTextures.reserve(ownedGpuTextures.size() + 1u);
196 auto tex = std::make_unique<Texture>();
197 auto gpu = std::make_unique<GpuTexture>();
202 UploadScopes scopes(
device);
204 descriptor.dimension = wgpu::TextureDimension::e2D;
206 descriptor.format = wgpu::TextureFormat::RGBA16Float;
209 descriptor.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst;
211 wgpu::TexelCopyTextureInfo destination{};
212 destination.texture =
gpu->texture;
213 wgpu::TexelCopyBufferLayout
layout{};
216 const wgpu::Extent3D extent{
width,
height, layers};
217 queue.WriteTexture(&destination, rgbaHalf.data(), rgbaHalf.size_bytes(), &
layout, &extent);
218 wgpu::TextureViewDescriptor
view{};
219 view.format = wgpu::TextureFormat::RGBA16Float;
220 view.dimension = wgpu::TextureViewDimension::e2DArray;
221 view.mipLevelCount = 1;
222 view.arrayLayerCount = layers;
224 gpu->sampler = makeSampler(
gpu->samplerState, 1);
225 auto checked = scopes.finish(instance);
227 if (!
gpu->texture || !
gpu->view || !
gpu->sampler)
229 tex->width = tex->pixelWidth = int(
width);
230 tex->height = tex->pixelHeight = int(
height);
231 tex->layers = int(layers);
232 tex->sampler =
gpu->samplerState;
233 tex->gpuHandle =
gpu.get();
235 ownedTextures.push_back(std::move(tex));
236 ownedGpuTextures.push_back(std::move(
gpu));
238 }
catch (
const std::exception&
error) {
244 std::span<const uint8_t> rgba) {
254 depth >
limits.maxTextureDimension3D || texels > SIZE_MAX / 4u || rgba.size() != texels * 4u)
257 ownedTextures.reserve(ownedTextures.size() + 1u);
258 ownedGpuTextures.reserve(ownedGpuTextures.size() + 1u);
259 auto tex = std::make_unique<Texture>();
260 auto gpu = std::make_unique<GpuTexture>();
263 gpu->isVolume =
true;
265 gpu->samplerState.repeatU =
gpu->samplerState.repeatV =
gpu->samplerState.repeatW =
true;
266 UploadScopes scopes(
device);
268 descriptor.dimension = wgpu::TextureDimension::e3D;
270 descriptor.format = wgpu::TextureFormat::RGBA8Unorm;
273 descriptor.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst;
275 wgpu::TexelCopyTextureInfo destination{};
276 destination.texture =
gpu->texture;
277 wgpu::TexelCopyBufferLayout
layout{};
281 queue.WriteTexture(&destination, rgba.data(), rgba.size(), &
layout, &extent);
282 wgpu::TextureViewDescriptor
view{};
283 view.format = wgpu::TextureFormat::RGBA8Unorm;
284 view.dimension = wgpu::TextureViewDimension::e3D;
285 view.mipLevelCount = 1;
286 view.arrayLayerCount = 1;
288 gpu->sampler = makeSampler(
gpu->samplerState, 1);
289 auto checked = scopes.finish(instance);
291 if (!
gpu->texture || !
gpu->view || !
gpu->sampler)
293 tex->width = tex->pixelWidth = int(
width);
294 tex->height = tex->pixelHeight = int(
height);
295 tex->depth = int(
depth);
296 tex->sampler =
gpu->samplerState;
297 tex->gpuHandle =
gpu.get();
299 ownedTextures.push_back(std::move(tex));
300 ownedGpuTextures.push_back(std::move(
gpu));
302 }
catch (
const std::exception&
error) {
wgpu::PopErrorScopeStatus status
std::chrono::steady_clock::time_point deadline
std::shared_ptr< OceanGpuSpectrum > gpu
const AssetImportLimits & limits
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
uint32_t maxTextureDimension2D() const
Maximum 2D texture dimension the device guarantees.
Result< Texture * > newTextureArrayRgba16f(uint32_t width, uint32_t height, uint32_t layers, std::span< const uint16_t > rgbaHalf) override
Upload a sampled 2D array of tightly packed linear RGBA16F texels.
Result< Texture * > newTexture3DRgba8(uint32_t width, uint32_t height, uint32_t depth, std::span< const uint8_t > rgba) override
Upload one tightly packed linear RGBA8 3D sampled texture.
Result< Texture * > newTextureMipChain(uint32_t width, uint32_t height, uint32_t levels, std::span< const uint8_t > rgba) override
Upload complete explicit linear RGBA8 mips without regenerating pixels.
eve::Diagnostic Diagnostic
DiagnosticCode
Stable machine-readable diagnostic codes.
static TextureSampler linear()
Linear.
static TextureSampler linearMipmap()
Trilinear (linear + linear mips). Caller should create the texture with generateMipmaps.