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
|
// Copyright 2025 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_webgl_swap_chain.h"
#include "third_party/blink/renderer/modules/webgl/webgl_framebuffer.h"
#include "third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h"
#include "third_party/blink/renderer/modules/webgl/webgl_texture.h"
#include "third_party/blink/renderer/modules/xr/xr_layer_shared_image_manager.h"
#include "third_party/blink/renderer/platform/graphics/gpu/xr_webgl_drawing_buffer.h"
namespace blink {
XRWebGLSwapChain::XRWebGLSwapChain(
WebGLRenderingContextBase* context,
const XRWebGLSwapChain::Descriptor& descriptor,
bool webgl2)
: webgl_context_(context), descriptor_(descriptor), webgl2_(webgl2) {
CHECK(context);
}
// Clears the contents of the current texture to transparent black or 0 (for
// depth/stencil textures).
void XRWebGLSwapChain::ClearCurrentTexture() {
WebGLUnownedTexture* texture = current_texture();
if (!texture) {
return;
}
gpu::gles2::GLES2Interface* gl = context()->ContextGL();
if (!gl) {
return;
}
GLenum attachment = descriptor_.attachment_target;
gl->BindFramebuffer(GL_FRAMEBUFFER, GetFramebuffer()->Object());
GLbitfield clear_bits = 0;
if (attachment == GL_COLOR_ATTACHMENT0) {
clear_bits |= GL_COLOR_BUFFER_BIT;
gl->ColorMask(true, true, true, true);
gl->ClearColor(0, 0, 0, 0);
} else if (attachment == GL_DEPTH_ATTACHMENT) {
clear_bits |= GL_DEPTH_BUFFER_BIT;
gl->DepthMask(true);
gl->ClearDepthf(1.0f);
} else if (attachment == GL_STENCIL_ATTACHMENT) {
clear_bits |= GL_STENCIL_BUFFER_BIT;
gl->StencilMaskSeparate(GL_FRONT, true);
gl->ClearStencil(0);
}
gl->Disable(GL_SCISSOR_TEST);
if (descriptor_.layers > 1) {
for (uint32_t i = 0; i < descriptor_.layers; ++i) {
gl->FramebufferTextureLayer(GL_FRAMEBUFFER, attachment, texture->Object(),
0, i);
gl->Clear(clear_bits);
}
} else {
gl->FramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D,
texture->Object(), 0);
gl->Clear(clear_bits);
}
// WebGLRenderingContextBase inherits from DrawingBuffer::Client, but makes
// all the methods private. Downcasting allows us to access them.
DrawingBuffer::Client* client =
static_cast<DrawingBuffer::Client*>(context());
client->DrawingBufferClientRestoreScissorTest();
client->DrawingBufferClientRestoreMaskAndClearValues();
client->DrawingBufferClientRestoreFramebufferBinding();
}
WebGLFramebuffer* XRWebGLSwapChain::GetFramebuffer() {
if (!framebuffer_) {
framebuffer_ = webgl_context_->createFramebuffer();
}
return framebuffer_;
}
void XRWebGLSwapChain::Trace(Visitor* visitor) const {
visitor->Trace(webgl_context_);
visitor->Trace(framebuffer_);
XRSwapChain::Trace(visitor);
}
XRWebGLStaticSwapChain::XRWebGLStaticSwapChain(
WebGLRenderingContextBase* context,
const XRWebGLSwapChain::Descriptor& descriptor,
bool webgl2)
: XRWebGLSwapChain(context, descriptor, webgl2) {}
XRWebGLStaticSwapChain::~XRWebGLStaticSwapChain() {
if (owned_texture_) {
gpu::gles2::GLES2Interface* gl = context()->ContextGL();
if (!gl) {
return;
}
gl->DeleteTextures(1, &owned_texture_);
}
}
WebGLUnownedTexture* XRWebGLStaticSwapChain::ProduceTexture() {
gpu::gles2::GLES2Interface* gl = context()->ContextGL();
if (!gl) {
return nullptr;
}
GLenum target = descriptor().layers > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
gl->GenTextures(1, &owned_texture_);
gl->BindTexture(target, owned_texture_);
// WebGLRenderingContextBase inherits from DrawingBuffer::Client, but makes
// all the methods private. Downcasting allows us to access them.
DrawingBuffer::Client* client =
static_cast<DrawingBuffer::Client*>(context());
if (target == GL_TEXTURE_2D_ARRAY) {
CHECK(webgl2()); // Texture arrays are only available in WebGL 2
gl->TexStorage3D(target, 1, descriptor().internal_format,
descriptor().width, descriptor().height,
descriptor().layers);
client->DrawingBufferClientRestoreTexture2DArrayBinding();
} else {
if (webgl2()) {
gl->TexStorage2DEXT(target, 1, descriptor().internal_format,
descriptor().width, descriptor().height);
} else {
gl->TexImage2D(target, 0, descriptor().format, descriptor().width,
descriptor().height, 0, descriptor().format,
descriptor().type, nullptr);
}
client->DrawingBufferClientRestoreTexture2DBinding();
}
return MakeGarbageCollected<WebGLUnownedTexture>(context(), owned_texture_,
target);
}
void XRWebGLStaticSwapChain::OnFrameEnd() {
ClearCurrentTexture();
// Intentionally not calling ResetCurrentTexture() here to keep the previously
// produced texture for the next frame.
}
XRWebGLSharedImageSwapChain::XRWebGLSharedImageSwapChain(
WebGLRenderingContextBase* context,
const XRWebGLSwapChain::Descriptor& descriptor,
bool webgl2)
: XRWebGLSwapChain(context, descriptor, webgl2) {
// SharedImages cannot have multiple layers yet.
CHECK_EQ(descriptor.layers, 1);
}
WebGLUnownedTexture* XRWebGLSharedImageSwapChain::ProduceTexture() {
gpu::gles2::GLES2Interface* context_gl = context()->ContextGL();
if (!context_gl) {
return nullptr;
}
const XRLayerSharedImages& shared_images = layer()->GetSharedImages();
const XRSharedImageData& content_image_data =
shared_images.content_image_data;
CHECK(content_image_data.shared_image);
CHECK(content_image_data.sync_token.HasData());
// Create a texture backed by the shared image.
CHECK(!shared_image_texture_);
shared_image_texture_ =
content_image_data.shared_image->CreateGLTexture(context_gl);
shared_image_scoped_access_ =
shared_image_texture_->BeginAccess(content_image_data.sync_token,
/*readonly=*/false);
return MakeGarbageCollected<WebGLUnownedTexture>(
context(), shared_image_texture_->id(), GL_TEXTURE_2D);
}
void XRWebGLSharedImageSwapChain::OnFrameEnd() {
WebGLUnownedTexture* texture = ResetCurrentTexture();
if (texture) {
DCHECK(shared_image_texture_);
gpu::SharedImageTexture::ScopedAccess::EndAccess(
std::move(shared_image_scoped_access_));
shared_image_texture_.reset();
// Notify our WebGLUnownedTexture that we have deleted it.
static_cast<WebGLUnownedTexture*>(texture)->OnGLDeleteTextures();
}
}
} // namespace blink
|