File: gpu_bind_group_layout.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,551 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webgpu/gpu_bind_group_layout.h"

#include "base/containers/heap_array.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_bind_group_layout_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_bind_group_layout_entry.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_buffer_binding_layout.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_external_texture_binding_layout.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_feature_name.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_sampler_binding_layout.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_storage_texture_binding_layout.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_texture_binding_layout.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_conversions.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_supported_features.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"

namespace blink {

wgpu::BindGroupLayoutEntry AsDawnType(
    GPUDevice* device,
    const GPUBindGroupLayoutEntry* webgpu_binding,
    Vector<std::unique_ptr<wgpu::ExternalTextureBindingLayout>>*
        externalTextureBindingLayouts,
    ExceptionState& exception_state) {
  wgpu::BindGroupLayoutEntry dawn_binding = {
      .binding = webgpu_binding->binding(),
      .visibility =
          AsDawnFlags<wgpu::ShaderStage>(webgpu_binding->visibility()),
  };

  if (webgpu_binding->hasBuffer()) {
    dawn_binding.buffer = {
        .type = AsDawnEnum(webgpu_binding->buffer()->type()),
        .hasDynamicOffset = webgpu_binding->buffer()->hasDynamicOffset(),
        .minBindingSize = webgpu_binding->buffer()->minBindingSize(),
    };
  }

  if (webgpu_binding->hasSampler()) {
    dawn_binding.sampler.type = AsDawnEnum(webgpu_binding->sampler()->type());
  }

  if (webgpu_binding->hasTexture()) {
    dawn_binding.texture = {
        .sampleType = AsDawnEnum(webgpu_binding->texture()->sampleType()),
        .viewDimension = AsDawnEnum(webgpu_binding->texture()->viewDimension()),
        .multisampled = webgpu_binding->texture()->multisampled(),
    };
  }

  if (webgpu_binding->hasStorageTexture()) {
    if (!device->ValidateTextureFormatUsage(
            webgpu_binding->storageTexture()->format(), exception_state)) {
      return {};
    }

    dawn_binding.storageTexture = {
        .access = AsDawnEnum(webgpu_binding->storageTexture()->access()),
        .format = AsDawnEnum(webgpu_binding->storageTexture()->format()),
        .viewDimension =
            AsDawnEnum(webgpu_binding->storageTexture()->viewDimension()),
    };
  }

  if (webgpu_binding->hasExternalTexture()) {
    std::unique_ptr<wgpu::ExternalTextureBindingLayout>
        externalTextureBindingLayout =
            std::make_unique<wgpu::ExternalTextureBindingLayout>();
    dawn_binding.nextInChain = externalTextureBindingLayout.get();
    externalTextureBindingLayouts->push_back(
        std::move(externalTextureBindingLayout));
  }

  return dawn_binding;
}

// TODO(crbug.com/1069302): Remove when unused.
base::HeapArray<wgpu::BindGroupLayoutEntry> AsDawnType(
    GPUDevice* device,
    const HeapVector<Member<GPUBindGroupLayoutEntry>>& webgpu_objects,
    Vector<std::unique_ptr<wgpu::ExternalTextureBindingLayout>>*
        externalTextureBindingLayouts,
    ExceptionState& exception_state) {
  const wtf_size_t count = webgpu_objects.size();
  auto dawn_objects =
      base::HeapArray<wgpu::BindGroupLayoutEntry>::WithSize(count);
  for (wtf_size_t i = 0; i < count; ++i) {
    dawn_objects[i] =
        AsDawnType(device, webgpu_objects[i].Get(),
                   externalTextureBindingLayouts, exception_state);
  }
  return dawn_objects;
}

// static
GPUBindGroupLayout* GPUBindGroupLayout::Create(
    GPUDevice* device,
    const GPUBindGroupLayoutDescriptor* webgpu_desc,
    ExceptionState& exception_state) {
  DCHECK(device);
  DCHECK(webgpu_desc);

  uint32_t entry_count = 0;
  base::HeapArray<wgpu::BindGroupLayoutEntry> entries;
  Vector<std::unique_ptr<wgpu::ExternalTextureBindingLayout>>
      externalTextureBindingLayouts;
  entry_count = static_cast<uint32_t>(webgpu_desc->entries().size());
  if (entry_count > 0) {
    entries = AsDawnType(device, webgpu_desc->entries(),
                         &externalTextureBindingLayouts, exception_state);
  }

  if (exception_state.HadException()) {
    return nullptr;
  }

  wgpu::BindGroupLayoutDescriptor dawn_desc = {
      .entryCount = entry_count,
      .entries = entries.data(),
  };
  std::string label = webgpu_desc->label().Utf8();
  if (!label.empty()) {
    dawn_desc.label = label.c_str();
  }

  GPUBindGroupLayout* layout = MakeGarbageCollected<GPUBindGroupLayout>(
      device, device->GetHandle().CreateBindGroupLayout(&dawn_desc),
      webgpu_desc->label());
  return layout;
}

GPUBindGroupLayout::GPUBindGroupLayout(GPUDevice* device,
                                       wgpu::BindGroupLayout bind_group_layout,
                                       const String& label)
    : DawnObject<wgpu::BindGroupLayout>(device,
                                        std::move(bind_group_layout),
                                        label) {}

}  // namespace blink