File: gpu_render_pipeline.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (412 lines) | stat: -rw-r--r-- 15,646 bytes parent folder | download | duplicates (5)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// 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_render_pipeline.h"

#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_blend_component.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_blend_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_color_state_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_color_target_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_depth_stencil_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_fragment_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_multisample_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_primitive_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_rasterization_state_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_render_pipeline_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_stencil_face_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_vertex_attribute.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_vertex_buffer_layout.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_vertex_state.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_bind_group_layout.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_pipeline_layout.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_shader_module.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"

namespace blink {

namespace {

const char kGPUBlendComponentPartiallySpecifiedMessage[] =
    "fragment.targets[%u].blend.%s has a mix of explicit and defaulted "
    "members, which is unusual. Did you mean to specify other members?";

wgpu::BlendComponent AsDawnType(const GPUBlendComponent* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::BlendComponent dawn_desc = {
      .operation = AsDawnEnum(webgpu_desc->getOperationOr(
          V8GPUBlendOperation(V8GPUBlendOperation::Enum::kAdd))),
      .srcFactor = AsDawnEnum(webgpu_desc->getSrcFactorOr(
          V8GPUBlendFactor(V8GPUBlendFactor::Enum::kOne))),
      .dstFactor = AsDawnEnum(webgpu_desc->getDstFactorOr(
          V8GPUBlendFactor(V8GPUBlendFactor::Enum::kZero))),
  };
  return dawn_desc;
}

wgpu::BlendState AsDawnType(const GPUBlendState* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::BlendState dawn_desc = {
      .color = AsDawnType(webgpu_desc->color()),
      .alpha = AsDawnType(webgpu_desc->alpha()),
  };

  return dawn_desc;
}

bool ValidateBlendComponent(GPUDevice* device,
                            const GPUBlendComponent* webgpu_desc,
                            ExceptionState& exception_state) {
  DCHECK(webgpu_desc);

  return device->ValidateBlendFactor(
             webgpu_desc->getSrcFactorOr(
                 V8GPUBlendFactor(V8GPUBlendFactor::Enum::kOne)),
             exception_state) &&
         device->ValidateBlendFactor(
             webgpu_desc->getDstFactorOr(
                 V8GPUBlendFactor(V8GPUBlendFactor::Enum::kZero)),
             exception_state);
}

}  // anonymous namespace

wgpu::ColorTargetState AsDawnType(const GPUColorTargetState* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::ColorTargetState dawn_desc = {
      // .blend is handled in ConvertToDawnType
      .format = AsDawnEnum(webgpu_desc->format()),
      .writeMask = AsDawnFlags<wgpu::ColorWriteMask>(webgpu_desc->writeMask()),
  };
  return dawn_desc;
}

wgpu::VertexBufferLayout AsDawnType(const GPUVertexBufferLayout* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::VertexBufferLayout dawn_desc = {
      .stepMode = AsDawnEnum(webgpu_desc->stepMode()),
      .arrayStride = webgpu_desc->arrayStride(),
      .attributeCount = webgpu_desc->attributes().size(),
      // .attributes is handled outside separately
  };

  return dawn_desc;
}

wgpu::VertexAttribute AsDawnType(const GPUVertexAttribute* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::VertexAttribute dawn_desc = {
      .format = AsDawnEnum(webgpu_desc->format()),
      .offset = webgpu_desc->offset(),
      .shaderLocation = webgpu_desc->shaderLocation(),
  };

  return dawn_desc;
}

namespace {

wgpu::StencilFaceState AsDawnType(const GPUStencilFaceState* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::StencilFaceState dawn_desc = {
      .compare = AsDawnEnum(webgpu_desc->compare()),
      .failOp = AsDawnEnum(webgpu_desc->failOp()),
      .depthFailOp = AsDawnEnum(webgpu_desc->depthFailOp()),
      .passOp = AsDawnEnum(webgpu_desc->passOp()),
  };

  return dawn_desc;
}

wgpu::PrimitiveState AsDawnType(const GPUPrimitiveState* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::PrimitiveState dawn_desc = {};
  dawn_desc.topology = AsDawnEnum(webgpu_desc->topology());

  if (webgpu_desc->hasStripIndexFormat()) {
    dawn_desc.stripIndexFormat = AsDawnEnum(webgpu_desc->stripIndexFormat());
  }

  dawn_desc.frontFace = AsDawnEnum(webgpu_desc->frontFace());
  dawn_desc.cullMode = AsDawnEnum(webgpu_desc->cullMode());
  dawn_desc.unclippedDepth = webgpu_desc->unclippedDepth();

  return dawn_desc;
}

wgpu::DepthStencilState AsDawnType(GPUDevice* device,
                                   const GPUDepthStencilState* webgpu_desc,
                                   ExceptionState& exception_state) {
  DCHECK(webgpu_desc);

  if (!device->ValidateTextureFormatUsage(webgpu_desc->format(),
                                          exception_state)) {
    return {};
  }

  wgpu::DepthStencilState dawn_desc = {};
  dawn_desc.format = AsDawnEnum(webgpu_desc->format());

  if (webgpu_desc->hasDepthWriteEnabled()) {
    dawn_desc.depthWriteEnabled = webgpu_desc->depthWriteEnabled()
                                      ? wgpu::OptionalBool::True
                                      : wgpu::OptionalBool::False;
  }

  if (webgpu_desc->hasDepthCompare()) {
    dawn_desc.depthCompare = AsDawnEnum(webgpu_desc->depthCompare());
  }

  dawn_desc.stencilFront = AsDawnType(webgpu_desc->stencilFront());
  dawn_desc.stencilBack = AsDawnType(webgpu_desc->stencilBack());
  dawn_desc.stencilReadMask = webgpu_desc->stencilReadMask();
  dawn_desc.stencilWriteMask = webgpu_desc->stencilWriteMask();
  dawn_desc.depthBias = webgpu_desc->depthBias();
  dawn_desc.depthBiasSlopeScale = webgpu_desc->depthBiasSlopeScale();
  dawn_desc.depthBiasClamp = webgpu_desc->depthBiasClamp();

  return dawn_desc;
}

wgpu::MultisampleState AsDawnType(const GPUMultisampleState* webgpu_desc) {
  DCHECK(webgpu_desc);

  wgpu::MultisampleState dawn_desc = {
      .count = webgpu_desc->count(),
      .mask = webgpu_desc->mask(),
      .alphaToCoverageEnabled = webgpu_desc->alphaToCoverageEnabled(),
  };

  return dawn_desc;
}

// TODO(crbug.com/351564777): should be UNSAFE_BUFFER_USAGE
// or avoid UNSAFE entirely (maybe possible using HeapArray)
void AsDawnVertexBufferLayouts(GPUDevice* device,
                               const GPUVertexState* descriptor,
                               OwnedVertexState* dawn_desc_info) {
  DCHECK(descriptor);
  DCHECK(dawn_desc_info);

  wgpu::VertexState* dawn_vertex = dawn_desc_info->dawn_desc;
  dawn_vertex->bufferCount = descriptor->buffers().size();

  if (dawn_vertex->bufferCount == 0) {
    dawn_vertex->buffers = nullptr;
    return;
  }

  // TODO(cwallez@chromium.org): Should we validate the Length() first so we
  // don't risk creating HUGE vectors of wgpu::VertexBufferLayout from
  // the sparse array?
  dawn_desc_info->buffers = AsDawnType(descriptor->buffers());
  dawn_vertex->buffers = dawn_desc_info->buffers.get();

  // Handle wgpu::VertexBufferLayout::attributes separately to guarantee the
  // lifetime.
  dawn_desc_info->attributes =
      std::make_unique<std::unique_ptr<wgpu::VertexAttribute[]>[]>(
          dawn_vertex->bufferCount);
  for (wtf_size_t i = 0; i < dawn_vertex->bufferCount; ++i) {
    if (const auto* buffer = descriptor->buffers()[i].Get()) {
      UNSAFE_TODO(dawn_desc_info->attributes.get()[i]) =
          AsDawnType(buffer->attributes());
      wgpu::VertexBufferLayout* dawn_buffer = &dawn_desc_info->buffers[i];
      dawn_buffer->attributes =
          UNSAFE_TODO(dawn_desc_info->attributes.get()[i].get());
    }
  }
}

void GPUVertexStateAsWGPUVertexState(GPUDevice* device,
                                     const GPUVertexState* descriptor,
                                     OwnedVertexState* dawn_vertex) {
  DCHECK(descriptor);
  DCHECK(dawn_vertex);

  *dawn_vertex->dawn_desc = {};

  GPUProgrammableStageAsWGPUProgrammableStage(descriptor, dawn_vertex);
  dawn_vertex->dawn_desc->constantCount = dawn_vertex->constantCount;
  dawn_vertex->dawn_desc->constants = dawn_vertex->constants.get();
  dawn_vertex->dawn_desc->module = descriptor->module()->GetHandle();
  dawn_vertex->dawn_desc->entryPoint =
      dawn_vertex->entry_point ? dawn_vertex->entry_point->c_str() : nullptr;

  if (descriptor->hasBuffers()) {
    AsDawnVertexBufferLayouts(device, descriptor, dawn_vertex);
  }
}

bool IsGPUBlendComponentPartiallySpecified(
    const GPUBlendComponent* webgpu_desc) {
  DCHECK(webgpu_desc);
  // GPUBlendComponent is considered partially specified when:
  // - srcFactor is missing but operation or dstFactor is provided
  // - dstFactor is missing but operation or srcFactor is provided
  return ((!webgpu_desc->hasSrcFactor() &&
           (webgpu_desc->hasDstFactor() || webgpu_desc->hasOperation())) ||
          (!webgpu_desc->hasDstFactor() &&
           (webgpu_desc->hasSrcFactor() || webgpu_desc->hasOperation())));
}

void GPUFragmentStateAsWGPUFragmentState(GPUDevice* device,
                                         const GPUFragmentState* descriptor,
                                         OwnedFragmentState* dawn_fragment,
                                         ExceptionState& exception_state) {
  DCHECK(descriptor);
  DCHECK(dawn_fragment);

  dawn_fragment->dawn_desc = {};

  GPUProgrammableStageAsWGPUProgrammableStage(descriptor, dawn_fragment);
  dawn_fragment->dawn_desc.constantCount = dawn_fragment->constantCount;
  dawn_fragment->dawn_desc.constants = dawn_fragment->constants.get();
  dawn_fragment->dawn_desc.module = descriptor->module()->GetHandle();
  dawn_fragment->dawn_desc.entryPoint =
      dawn_fragment->entry_point ? dawn_fragment->entry_point->c_str()
                                 : nullptr;

  dawn_fragment->dawn_desc.targets = nullptr;
  dawn_fragment->dawn_desc.targetCount = descriptor->targets().size();
  if (dawn_fragment->dawn_desc.targetCount > 0) {
    dawn_fragment->targets = AsDawnType(descriptor->targets());
    dawn_fragment->dawn_desc.targets = dawn_fragment->targets.get();
  }

  // In order to maintain proper ownership we have to process the blend states
  // for each target outside of AsDawnType().
  // ReserveCapacity beforehand to make sure our pointers within the vector
  // stay stable.
  dawn_fragment->blend_states.resize(descriptor->targets().size());
  for (wtf_size_t i = 0; i < descriptor->targets().size(); ++i) {
    const auto& maybe_color_target = descriptor->targets()[i];
    if (!maybe_color_target) {
      continue;
    }
    const GPUColorTargetState* color_target = maybe_color_target.Get();
    if (!device->ValidateTextureFormatUsage(color_target->format(),
                                            exception_state)) {
      return;
    }
    if (color_target->hasBlend()) {
      const GPUBlendState* blend_state = color_target->blend();
      if (IsGPUBlendComponentPartiallySpecified(blend_state->color())) {
        device->AddConsoleWarning(String::Format(
            kGPUBlendComponentPartiallySpecifiedMessage, i, "color"));
      }
      if (IsGPUBlendComponentPartiallySpecified(blend_state->alpha())) {
        device->AddConsoleWarning(String::Format(
            kGPUBlendComponentPartiallySpecifiedMessage, i, "alpha"));
      }

      if (!ValidateBlendComponent(device, blend_state->color(),
                                  exception_state) ||
          !ValidateBlendComponent(device, blend_state->alpha(),
                                  exception_state)) {
        return;
      }

      dawn_fragment->blend_states[i] = AsDawnType(blend_state);
      dawn_fragment->targets[i].blend = &dawn_fragment->blend_states[i];
    }
  }
}

}  // anonymous namespace

void ConvertToDawnType(v8::Isolate* isolate,
                       GPUDevice* device,
                       const GPURenderPipelineDescriptor* webgpu_desc,
                       OwnedRenderPipelineDescriptor* dawn_desc_info,
                       ExceptionState& exception_state) {
  DCHECK(isolate);
  DCHECK(webgpu_desc);
  DCHECK(dawn_desc_info);

  // Label
  if (!webgpu_desc->label().empty()) {
    dawn_desc_info->label = webgpu_desc->label().Utf8();
    dawn_desc_info->dawn_desc.label = dawn_desc_info->label.c_str();
  }

  // Layout
  dawn_desc_info->dawn_desc.layout = AsDawnType(webgpu_desc->layout());

  // Vertex
  const GPUVertexState* vertex = webgpu_desc->vertex();
  OwnedVertexState* dawn_vertex = &dawn_desc_info->vertex;
  dawn_vertex->dawn_desc = &dawn_desc_info->dawn_desc.vertex;
  GPUVertexStateAsWGPUVertexState(device, vertex, dawn_vertex);

  // Primitive
  dawn_desc_info->dawn_desc.primitive = AsDawnType(webgpu_desc->primitive());

  // DepthStencil
  if (webgpu_desc->hasDepthStencil()) {
    dawn_desc_info->depth_stencil =
        AsDawnType(device, webgpu_desc->depthStencil(), exception_state);
    dawn_desc_info->dawn_desc.depthStencil = &dawn_desc_info->depth_stencil;
  }

  // Multisample
  dawn_desc_info->dawn_desc.multisample =
      AsDawnType(webgpu_desc->multisample());

  // Fragment
  if (webgpu_desc->hasFragment()) {
    GPUFragmentStateAsWGPUFragmentState(device, webgpu_desc->fragment(),
                                        &dawn_desc_info->fragment,
                                        exception_state);
    dawn_desc_info->dawn_desc.fragment = &dawn_desc_info->fragment.dawn_desc;
  }
}

// static
GPURenderPipeline* GPURenderPipeline::Create(
    ScriptState* script_state,
    GPUDevice* device,
    const GPURenderPipelineDescriptor* webgpu_desc) {
  DCHECK(device);
  DCHECK(webgpu_desc);

  v8::Isolate* isolate = script_state->GetIsolate();
  GPURenderPipeline* pipeline;
  OwnedRenderPipelineDescriptor dawn_desc_info;
  ConvertToDawnType(isolate, device, webgpu_desc, &dawn_desc_info,
                    PassThroughException(isolate));

  if (isolate->HasPendingException()) {
    return nullptr;
  }

  pipeline = MakeGarbageCollected<GPURenderPipeline>(
      device,
      device->GetHandle().CreateRenderPipeline(&dawn_desc_info.dawn_desc),
      webgpu_desc->label());
  return pipeline;
}

GPURenderPipeline::GPURenderPipeline(GPUDevice* device,
                                     wgpu::RenderPipeline render_pipeline,
                                     const String& label)
    : DawnObject<wgpu::RenderPipeline>(device,
                                       std::move(render_pipeline),
                                       label) {}

GPUBindGroupLayout* GPURenderPipeline::getBindGroupLayout(uint32_t index) {
  return MakeGarbageCollected<GPUBindGroupLayout>(
      device_, GetHandle().GetBindGroupLayout(index), String());
}

}  // namespace blink