File: xr_gpu_swap_chain.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 (190 lines) | stat: -rw-r--r-- 6,154 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 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/xr/xr_gpu_swap_chain.h"

#include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/renderer/modules/xr/xr_layer_shared_image_manager.h"

namespace blink {

bool IsDepthFormat(wgpu::TextureFormat format) {
  switch (format) {
    case wgpu::TextureFormat::Depth24Plus:
    case wgpu::TextureFormat::Depth16Unorm:
    case wgpu::TextureFormat::Depth24PlusStencil8:
    case wgpu::TextureFormat::Depth32Float:
    case wgpu::TextureFormat::Depth32FloatStencil8:
      return true;
    default:
      return false;
  }
}

bool IsStencilFormat(wgpu::TextureFormat format) {
  switch (format) {
    case wgpu::TextureFormat::Stencil8:
    case wgpu::TextureFormat::Depth24PlusStencil8:
    case wgpu::TextureFormat::Depth32FloatStencil8:
      return true;
    default:
      return false;
  }
}

XRGPUSwapChain::XRGPUSwapChain(GPUDevice* device) : device_(device) {
  CHECK(device);
}

// Clears the contents of the current texture to transparent black or 0 (for
// depth/stencil textures).
void XRGPUSwapChain::ClearCurrentTexture(wgpu::CommandEncoder command_encoder) {
  GPUTexture* texture = current_texture();
  if (!texture) {
    return;
  }

  bool hasDepth = IsDepthFormat(texture->Format());
  bool hasStencil = IsStencilFormat(texture->Format());

  // Clear each level of the texture array.
  for (uint32_t i = 0; i < texture->depthOrArrayLayers(); ++i) {
    wgpu::TextureViewDescriptor view_desc = {
        .dimension = wgpu::TextureViewDimension::e2D,
        .baseMipLevel = 0,
        .mipLevelCount = 1,
        .baseArrayLayer = i,
        .arrayLayerCount = 1,
    };

    wgpu::TextureView view = texture->GetHandle().CreateView(&view_desc);

    wgpu::RenderPassEncoder render_pass;
    if (hasDepth || hasStencil) {
      wgpu::RenderPassDepthStencilAttachment depth_stencil_attachment = {
          .view = view,
      };

      if (hasDepth) {
        depth_stencil_attachment.depthLoadOp = wgpu::LoadOp::Clear;
        depth_stencil_attachment.depthStoreOp = wgpu::StoreOp::Store;
        depth_stencil_attachment.depthClearValue = 0;
      }

      if (hasStencil) {
        depth_stencil_attachment.stencilLoadOp = wgpu::LoadOp::Clear;
        depth_stencil_attachment.stencilStoreOp = wgpu::StoreOp::Store;
        depth_stencil_attachment.stencilClearValue = 0;
      }

      wgpu::RenderPassDescriptor render_pass_desc = {
          .depthStencilAttachment = &depth_stencil_attachment,
      };

      render_pass = command_encoder.BeginRenderPass(&render_pass_desc);
    } else {
      wgpu::RenderPassColorAttachment color_attachment = {
          .view = view,
          .loadOp = wgpu::LoadOp::Clear,
          .storeOp = wgpu::StoreOp::Store,
          .clearValue = {0, 0, 0, 0},
      };

      wgpu::RenderPassDescriptor render_pass_desc = {
          .colorAttachmentCount = 1,
          .colorAttachments = &color_attachment,
      };

      render_pass = command_encoder.BeginRenderPass(&render_pass_desc);
    }

    // Immediately end the render pass to clear the texture.
    render_pass.End();
  }
}

void XRGPUSwapChain::Trace(Visitor* visitor) const {
  visitor->Trace(device_);
  XRSwapChain::Trace(visitor);
}

XRGPUStaticSwapChain::XRGPUStaticSwapChain(GPUDevice* device,
                                           const wgpu::TextureDescriptor& desc)
    : XRGPUSwapChain(device) {
  descriptor_ = desc;
}

GPUTexture* XRGPUStaticSwapChain::ProduceTexture() {
  return GPUTexture::Create(device(), &descriptor_);
}

void XRGPUStaticSwapChain::OnFrameEnd() {
  // TODO(crbug.com/5818595): Prior to shipping the spec needs to determine
  // if texture re-use is appropriate or not. If re-use is not specified then
  // it should at the very least be detached from the JavaScript wrapper and
  // reattached to a new one here. In both cases the texture should be
  // cleared.

  wgpu::DawnEncoderInternalUsageDescriptor internal_usage_desc = {{
      .useInternalUsages = true,
  }};
  wgpu::CommandEncoderDescriptor command_encoder_desc = {
      .nextInChain = &internal_usage_desc,
      .label = "XRGPUStaticSwapChain Clear",
  };
  wgpu::CommandEncoder command_encoder =
      device()->GetHandle().CreateCommandEncoder(&command_encoder_desc);

  ClearCurrentTexture(command_encoder);

  wgpu::CommandBuffer command_buffer = command_encoder.Finish();
  command_encoder = nullptr;

  device()->GetHandle().GetQueue().Submit(1u, &command_buffer);
  command_buffer = nullptr;

  // Intentionally not calling ResetCurrentTexture() here to keep the previously
  // produced texture for the next frame.
}

XRGPUMailboxSwapChain::XRGPUMailboxSwapChain(
    GPUDevice* device,
    const wgpu::TextureDescriptor& desc)
    : XRGPUSwapChain(device) {
  descriptor_ = desc;

  // TODO(crbug.com/359418629): Internal Usage will not be necessary once we can
  // use texture array mailboxes directly.
  wgpu::TextureUsage internal_usage = wgpu::TextureUsage::CopyDst;
  texture_internal_usage_ = {{
      .internalUsage = internal_usage,
  }};
  descriptor_.nextInChain = &texture_internal_usage_;
}

GPUTexture* XRGPUMailboxSwapChain::ProduceTexture() {
  const XRLayerSharedImages& shared_images = layer()->GetSharedImages();

  // TODO(crbug.com/359418629): Allow for other mailboxes as well?
  CHECK(shared_images.content_image_data.shared_image);

  scoped_refptr<WebGPUMailboxTexture> mailbox_texture =
      WebGPUMailboxTexture::FromExistingSharedImage(
          device()->GetDawnControlClient(), device()->GetHandle(), descriptor_,
          shared_images.content_image_data.shared_image,
          shared_images.content_image_data.sync_token);

  return MakeGarbageCollected<GPUTexture>(
      device(), descriptor_.format, descriptor_.usage,
      std::move(mailbox_texture), "WebXR Mailbox Swap Chain");
}

void XRGPUMailboxSwapChain::OnFrameEnd() {
  GPUTexture* texture = ResetCurrentTexture();
  if (texture) {
    texture->DissociateMailbox();
  }
}

}  // namespace blink