summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/ipc/WebGPUTypes.h
blob: e607e03b9966726102a9c944a889877daecaad66 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef WEBGPU_TYPES_H_
#define WEBGPU_TYPES_H_

#include <cstdint>
#include "mozilla/Maybe.h"
#include "nsString.h"
#include "mozilla/dom/BindingDeclarations.h"

namespace mozilla::webgpu {

using RawId = uint64_t;
using BufferAddress = uint64_t;

struct ScopedError {
  // Did an error occur as a result the attempt to retrieve an error
  // (e.g. from a dead device, from an empty scope stack)?
  bool operationError = false;

  // If non-empty, the first error generated when this scope was on
  // the top of the stack. This is interpreted as UTF-8.
  nsCString validationMessage;
};
using MaybeScopedError = Maybe<ScopedError>;

enum class WebGPUCompilationMessageType { Error, Warning, Info };

// TODO: Better name? CompilationMessage alread taken by the dom object.
/// The serializable counterpart of the dom object CompilationMessage.
struct WebGPUCompilationMessage {
  nsString message;
  uint64_t lineNum = 0;
  uint64_t linePos = 0;
  // In utf16 code units.
  uint64_t offset = 0;
  // In utf16 code units.
  uint64_t length = 0;
  WebGPUCompilationMessageType messageType =
      WebGPUCompilationMessageType::Error;
};

/// A helper to reduce the boiler plate of turning the many Optional<nsAString>
/// we get from the dom to the nullable nsACString* we pass to the wgpu ffi.
class StringHelper {
 public:
  explicit StringHelper(const dom::Optional<nsString>& aWide) {
    if (aWide.WasPassed()) {
      mNarrow = Some(NS_ConvertUTF16toUTF8(aWide.Value()));
    }
  }

  const nsACString* Get() const {
    if (mNarrow.isSome()) {
      return mNarrow.ptr();
    }
    return nullptr;
  }

 private:
  Maybe<NS_ConvertUTF16toUTF8> mNarrow;
};

}  // namespace mozilla::webgpu

#endif  // WEBGPU_TYPES_H_