add possibility to link wgpu library from extern (#4272)

This commit is contained in:
Martin Marmsoler
2026-07-07 14:26:16 +02:00
committed by GitHub
parent 58d76e1617
commit 4cfc8a97f7
12 changed files with 169 additions and 75 deletions

View File

@@ -30,6 +30,11 @@ const GeoJSONSource::Impl& GeoJSONSource::impl() const {
return static_cast<const Impl&>(*baseImpl);
}
/*!
* \brief GeoJSONSource::setURL
* Set the url to a geojson file which can be fetched
* \param url_
*/
void GeoJSONSource::setURL(const std::string& url_) {
url = url_;

View File

@@ -4,6 +4,7 @@
#include <mbgl/webgpu/offscreen_texture.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/webgpu/wgpu_cpp_compat.hpp>
#include <mbgl/webgpu/texture2d.hpp>
#if MLN_WEBGPU_IMPL_DAWN
#include <dawn/native/DawnNative.h>
@@ -84,6 +85,8 @@ public:
return PremultipliedImage(size);
}
gfx::Texture2DPtr takeTexture() { return static_cast<OffscreenTexture*>(offscreenTexture.get())->takeTexture(); }
private:
webgpu::Context& context;
Size size;
@@ -153,7 +156,7 @@ HeadlessBackend::HeadlessBackend(Size size_, SwapBehaviour swapBehaviour_, gfx::
setDevice(impl->device.Get());
setQueue(impl->queue.Get());
setInstance(impl->instance->Get());
#elif MLN_WEBGPU_IMPL_WGPU
#elif MLN_WEBGPU_IMPL_WGPU && !MLN_WEBGPU_IMPL_FFI
// wgpu-native backend initialization
wgpu::InstanceDescriptor instanceDesc = {};
impl->instance = wgpu::createInstance(instanceDesc);
@@ -332,6 +335,13 @@ PremultipliedImage HeadlessBackend::readStillImage() {
return PremultipliedImage(getSize());
}
gfx::Texture2DPtr HeadlessBackend::takeTexture() {
if (hasResource()) {
return getResource<HeadlessRenderableResource>().takeTexture();
}
return nullptr;
}
RendererBackend* HeadlessBackend::getRendererBackend() {
return this;
}

View File

@@ -28,16 +28,7 @@ public:
assert(!size.isEmpty());
// Create color texture
colorTexture = context.createTexture2D();
colorTexture->setSize(size);
colorTexture->setFormat(gfx::TexturePixelType::RGBA, type);
if (auto* webgpuTexture = static_cast<Texture2D*>(colorTexture.get())) {
webgpuTexture->setUsage(WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopyDst |
WGPUTextureUsage_CopySrc | WGPUTextureUsage_RenderAttachment);
webgpuTexture->setSizeChangedCallback([this](const Size& newSize) { handleResize(newSize); });
}
colorTexture->setSamplerConfiguration(
{gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp});
colorTexture = createColorTexture();
// Create depth/stencil texture if needed
// WebGPU requires a combined depth-stencil texture when both are needed
@@ -304,9 +295,33 @@ public:
return colorTexture;
}
gfx::Texture2DPtr takeTexture() {
if (!colorTexture) {
return nullptr;
}
auto taken = std::move(colorTexture);
colorTexture = createColorTexture();
return taken;
}
void setSizeListener(std::function<void(const Size&)> listener) { sizeListener = std::move(listener); }
private:
gfx::Texture2DPtr createColorTexture() {
auto texture = context.createTexture2D();
texture->setSize(size);
texture->setFormat(gfx::TexturePixelType::RGBA, type);
if (auto* webgpuTexture = static_cast<Texture2D*>(texture.get())) {
webgpuTexture->setUsage(WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopyDst |
WGPUTextureUsage_CopySrc | WGPUTextureUsage_RenderAttachment);
webgpuTexture->setSizeChangedCallback([this](const Size& newSize) { handleResize(newSize); });
}
texture->setSamplerConfiguration(
{gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp});
return texture;
}
void handleResize(const Size& newSize) {
if (newSize == size || newSize.isEmpty()) {
return;
@@ -360,5 +375,9 @@ const gfx::Texture2DPtr& OffscreenTexture::getTexture() {
return getResource<OffscreenTextureResource>().getTexture();
}
gfx::Texture2DPtr OffscreenTexture::takeTexture() {
return getResource<OffscreenTextureResource>().takeTexture();
}
} // namespace webgpu
} // namespace mbgl

View File

@@ -25,7 +25,9 @@ public:
WGPURenderPassEncoder encoder = nullptr;
WGPUCommandEncoder commandEncoder = nullptr;
const gfx::UniformBufferArray* globalUniformBuffers = nullptr;
// Required here because it must life as long as the render pass
wgpu::TextureView colorView;
// Required here because it must life as long as the render pass
wgpu::TextureView depthStencilView;
wgpu::TextureFormat previousColorFormat = wgpu::TextureFormat::Undefined;
wgpu::TextureFormat previousDepthStencilFormat = wgpu::TextureFormat::Undefined;
@@ -183,6 +185,10 @@ RenderPass::RenderPass(CommandEncoder& commandEncoder_, const char* name, const
wgpuRenderPassEncoderSetScissorRect(impl->encoder, 0, 0, size.width, size.height);
} else {
mbgl::Log::Error(mbgl::Event::Render, "WebGPU: Failed to begin render pass");
#if MLN_WEBGPU_IMPL_WGPU
wgpuTextureViewRelease(impl->colorView);
wgpuTextureViewRelease(impl->depthStencilView);
#endif
impl->colorView = nullptr;
impl->depthStencilView = nullptr;
}
@@ -199,6 +205,15 @@ RenderPass::~RenderPass() {
wgpuRenderPassEncoderRelease(impl->encoder);
}
#if MLN_WEBGPU_IMPL_WGPU
if (impl->colorView) {
wgpuTextureViewRelease(impl->colorView);
}
if (impl->depthStencilView) {
wgpuTextureViewRelease(impl->depthStencilView);
}
#endif
auto& backend = static_cast<RendererBackend&>(commandEncoder.getContext().getBackend());
if (impl->colorFormatUpdated) {
backend.setColorFormat(impl->previousColorFormat);

View File

@@ -35,8 +35,8 @@ namespace webgpu {
class RendererBackend::Impl {
public:
void* instance = nullptr;
void* device = nullptr;
void* queue = nullptr;
WGPUDevice device = nullptr;
WGPUQueue queue = nullptr;
void* surface = nullptr;
wgpu::TextureFormat depthStencilFormat = wgpu::TextureFormat::Undefined;
wgpu::TextureFormat colorFormat = wgpu::TextureFormat::Undefined;
@@ -134,11 +134,11 @@ void RendererBackend::setInstance(void* instance) {
impl->instance = instance;
}
void RendererBackend::setDevice(void* device) {
void RendererBackend::setDevice(WGPUDevice device) {
impl->device = device;
}
void RendererBackend::setQueue(void* queue) {
void RendererBackend::setQueue(WGPUQueue queue) {
impl->queue = queue;
}
@@ -146,11 +146,11 @@ void* RendererBackend::getInstance() const {
return impl->instance;
}
void* RendererBackend::getDevice() const {
WGPUDevice RendererBackend::getDevice() const {
return impl->device;
}
void* RendererBackend::getQueue() const {
WGPUQueue RendererBackend::getQueue() const {
return impl->queue;
}