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

gpu / command_buffer / client / webgpu_interface.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_CLIENT_WEBGPU_INTERFACE_H_
#define GPU_COMMAND_BUFFER_CLIENT_WEBGPU_INTERFACE_H_

#include <dawn/dawn_proc_table.h>
#include <dawn/webgpu.h>

#include "base/functional/callback.h"
#include "base/types/cxx23_to_underlying.h"
#include "gpu/command_buffer/client/interface_base.h"
#include "gpu/command_buffer/common/webgpu_cmd_enums.h"
#include "gpu/command_buffer/common/webgpu_cmd_ids.h"
#include "third_party/blink/public/common/tokens/tokens.h"

namespace gpu {

struct Mailbox;

namespace webgpu {

struct ReservedBuffer {
  WGPUBuffer buffer;
  uint32_t id;
  uint32_t generation;
  uint32_t deviceId;
  uint32_t deviceGeneration;
};

struct ReservedTexture {
  WGPUTexture texture;
  uint32_t id;
  uint32_t generation;
  uint32_t deviceId;
  uint32_t deviceGeneration;
};

// APIChannel is a RefCounted class which holds the Dawn wire client.
class APIChannel : public base::RefCounted<APIChannel> {
 public:
  // Get the WGPUInstance.
  virtual WGPUInstance GetWGPUInstance() const = 0;

  // Disconnect. All commands using the WebGPU API should become a
  // no-op and server-side resources can be freed.
  virtual void Disconnect() = 0;

 protected:
  friend class base::RefCounted<APIChannel>;
  APIChannel() = default;
  virtual ~APIChannel() = default;
};

class WebGPUInterface : public InterfaceBase {
 public:
  WebGPUInterface() = default;
  virtual ~WebGPUInterface() = default;

  // Flush all commands.
  virtual void FlushCommands() = 0;

  // Ensure the awaiting flush flag is set on the device client. Returns false
  // if a flush has already been indicated, or a flush is not needed (there may
  // be no commands to flush). Returns true if the caller should schedule a
  // flush.
  virtual bool EnsureAwaitingFlush() = 0;

  // If the awaiting flush flag is set, flushes commands. Otherwise, does
  // nothing.
  virtual void FlushAwaitingCommands() = 0;

  // Get a strong reference to the APIChannel backing the implementation.
  virtual scoped_refptr<APIChannel> GetAPIChannel() const = 0;

  virtual ReservedBuffer ReserveBuffer(
      WGPUDevice device,
      const WGPUBufferDescriptor* optionalDesc = nullptr) = 0;

  virtual ReservedTexture ReserveTexture(
      WGPUDevice device,
      const WGPUTextureDescriptor* optionalDesc = nullptr) = 0;

  // Gets or creates a usable WGPUDevice synchronously. It really should not
  // be used, and the async request adapter and request device APIs should be
  // used instead.
  virtual WGPUDevice DeprecatedEnsureDefaultDeviceSync() = 0;

// Include the auto-generated part of this class. We split this because
// it means we can easily edit the non-auto generated parts right here in
// this file instead of having to edit some template or the code generator.
#include "gpu/command_buffer/client/webgpu_interface_autogen.h"

  // NOTE: Passing WEBGPU_MAILBOX_DISCARD is only valid if the SharedImage
  // associated with `mailbox` has been created with
  // SHARED_IMAGE_USAGE_WEBGPU_WRITE and at least one of `usage` or
  // `internal_usage` contains a usage supporting lazy clearing (CopyDst or
  // RenderAttachment).
  virtual void AssociateMailbox(GLuint device_id,
                                GLuint device_generation,
                                GLuint id,
                                GLuint generation,
                                uint64_t usage,
                                uint64_t internal_usage,
                                const WGPUTextureFormat* view_formats,
                                GLuint view_format_count,
                                MailboxFlags flags,
                                const Mailbox& mailbox) = 0;

  void AssociateMailbox(GLuint device_id,
                        GLuint device_generation,
                        GLuint id,
                        GLuint generation,
                        uint64_t usage,
                        const WGPUTextureFormat* view_formats,
                        GLuint view_format_count,
                        MailboxFlags flags,
                        const Mailbox& mailbox) {
    AssociateMailbox(device_id, device_generation, id, generation, usage, 0,
                     view_formats, view_format_count, flags, mailbox);
  }

  void AssociateMailbox(GLuint device_id,
                        GLuint device_generation,
                        GLuint id,
                        GLuint generation,
                        uint64_t usage,
                        MailboxFlags flags,
                        const Mailbox& mailbox) {
    AssociateMailbox(device_id, device_generation, id, generation, usage, 0,
                     nullptr, 0, flags, mailbox);
  }

  void AssociateMailbox(GLuint device_id,
                        GLuint device_generation,
                        GLuint id,
                        GLuint generation,
                        uint64_t usage,
                        uint64_t internal_usage,
                        MailboxFlags flags,
                        const Mailbox& mailbox) {
    AssociateMailbox(device_id, device_generation, id, generation, usage,
                     internal_usage, nullptr, 0, flags, mailbox);
  }

  void AssociateMailbox(GLuint device_id,
                        GLuint device_generation,
                        GLuint id,
                        GLuint generation,
                        uint64_t usage,
                        const Mailbox& mailbox) {
    AssociateMailbox(device_id, device_generation, id, generation, usage, 0,
                     nullptr, 0, WEBGPU_MAILBOX_NONE, mailbox);
  }

  virtual void AssociateMailboxForBuffer(GLuint device_id,
                                         GLuint device_generation,
                                         GLuint id,
                                         GLuint generation,
                                         uint64_t usage,
                                         const Mailbox& mailbox) = 0;

  void SetWebGPUExecutionContextToken(
      const blink::WebGPUExecutionContextToken& token) {
    uint64_t high = token.value().GetHighForSerialization();
    uint64_t low = token.value().GetLowForSerialization();
    SetWebGPUExecutionContextToken(base::to_underlying(token.variant_index()),
                                   high >> 32, high & 0xFFFFFFFF, low >> 32,
                                   low & 0xFFFFFFFF);
  }
};

}  // namespace webgpu
}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_INTERFACE_H_