Remove all NativeSwapChain*

These were helpers to avoid the need for users of implementation-based
swapchains to implement them themselves. They are no longer used and can
be removed.

Also removes SwapChainuUtils.h that's no longer used.

Bug: dawn:269
Change-Id: I3c563edaf107e9ce04f708e8bacae9d231b0f40d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/126421
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
This commit is contained in:
Corentin Wallez 2023-04-05 07:24:44 +00:00 committed by Dawn LUCI CQ
parent 974802d17c
commit 961f7eae70
22 changed files with 4 additions and 796 deletions

View File

@ -23,7 +23,6 @@
#include <memory>
#include <vector>
#include "dawn/dawn_wsi.h"
#include "dawn/native/D3DBackend.h"
struct ID3D12Device;
@ -36,10 +35,6 @@ class Device;
class ExternalImageDXGIImpl;
DAWN_NATIVE_EXPORT Microsoft::WRL::ComPtr<ID3D12Device> GetD3D12Device(WGPUDevice device);
DAWN_NATIVE_EXPORT DawnSwapChainImplementation CreateNativeSwapChainImpl(WGPUDevice device,
HWND window);
DAWN_NATIVE_EXPORT WGPUTextureFormat
GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain);
enum MemorySegment {
Local,

View File

@ -17,7 +17,6 @@
#include <vector>
#include "dawn/dawn_wsi.h"
#include "dawn/native/DawnNative.h"
// The specifics of the Metal backend expose types in function signatures that might not be

View File

@ -15,11 +15,12 @@
#ifndef INCLUDE_DAWN_NATIVE_NULLBACKEND_H_
#define INCLUDE_DAWN_NATIVE_NULLBACKEND_H_
#include "dawn/dawn_wsi.h"
#include "dawn/native/DawnNative.h"
namespace dawn::native::null {
DAWN_NATIVE_EXPORT DawnSwapChainImplementation CreateNativeSwapChainImpl();
// Nothing for now \o/
} // namespace dawn::native::null
#endif // INCLUDE_DAWN_NATIVE_NULLBACKEND_H_

View File

@ -17,7 +17,6 @@
typedef void* EGLImage;
#include "dawn/dawn_wsi.h"
#include "dawn/native/DawnNative.h"
namespace dawn::native::opengl {
@ -33,13 +32,6 @@ struct DAWN_NATIVE_EXPORT AdapterDiscoveryOptionsES : public AdapterDiscoveryOpt
AdapterDiscoveryOptionsES();
};
using PresentCallback = void (*)(void*);
DAWN_NATIVE_EXPORT DawnSwapChainImplementation CreateNativeSwapChainImpl(WGPUDevice device,
PresentCallback present,
void* presentUserdata);
DAWN_NATIVE_EXPORT WGPUTextureFormat
GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain);
struct DAWN_NATIVE_EXPORT ExternalImageDescriptorEGLImage : ExternalImageDescriptor {
public:
ExternalImageDescriptorEGLImage();

View File

@ -20,7 +20,6 @@
#include <array>
#include <vector>
#include "dawn/dawn_wsi.h"
#include "dawn/native/DawnNative.h"
namespace dawn::native::vulkan {
@ -29,11 +28,6 @@ DAWN_NATIVE_EXPORT VkInstance GetInstance(WGPUDevice device);
DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, const char* pName);
DAWN_NATIVE_EXPORT DawnSwapChainImplementation CreateNativeSwapChainImpl(WGPUDevice device,
::VkSurfaceKHR surface);
DAWN_NATIVE_EXPORT WGPUTextureFormat
GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain);
struct DAWN_NATIVE_EXPORT AdapterDiscoveryOptions : public AdapterDiscoveryOptionsBase {
AdapterDiscoveryOptions();

View File

@ -263,7 +263,6 @@ if (is_win || is_linux || is_chromeos || is_mac || is_fuchsia || is_android) {
"SlabAllocator.cpp",
"SlabAllocator.h",
"StackContainer.h",
"SwapChainUtils.h",
"SystemUtils.cpp",
"SystemUtils.h",
"TypeTraits.h",

View File

@ -69,7 +69,6 @@ target_sources(dawn_common PRIVATE
"SlabAllocator.cpp"
"SlabAllocator.h"
"StackContainer.h"
"SwapChainUtils.h"
"SystemUtils.cpp"
"SystemUtils.h"
"TypeTraits.h"

View File

@ -1,40 +0,0 @@
// Copyright 2017 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_DAWN_COMMON_SWAPCHAINUTILS_H_
#define SRC_DAWN_COMMON_SWAPCHAINUTILS_H_
#include "dawn/dawn_wsi.h"
template <typename T>
DawnSwapChainImplementation CreateSwapChainImplementation(T* swapChain) {
DawnSwapChainImplementation impl = {};
impl.userData = swapChain;
impl.Init = [](void* userData, void* wsiContext) {
auto* ctx = static_cast<typename T::WSIContext*>(wsiContext);
reinterpret_cast<T*>(userData)->Init(ctx);
};
impl.Destroy = [](void* userData) { delete reinterpret_cast<T*>(userData); };
impl.Configure = [](void* userData, WGPUTextureFormat format, WGPUTextureUsage allowedUsage,
uint32_t width, uint32_t height) {
return static_cast<T*>(userData)->Configure(format, allowedUsage, width, height);
};
impl.GetNextTexture = [](void* userData, DawnSwapChainNextTexture* nextTexture) {
return static_cast<T*>(userData)->GetNextTexture(nextTexture);
};
impl.Present = [](void* userData) { return static_cast<T*>(userData)->Present(); };
return impl;
}
#endif // SRC_DAWN_COMMON_SWAPCHAINUTILS_H_

View File

@ -476,8 +476,6 @@ source_set("sources") {
"d3d12/HeapD3D12.cpp",
"d3d12/HeapD3D12.h",
"d3d12/IntegerTypes.h",
"d3d12/NativeSwapChainImplD3D12.cpp",
"d3d12/NativeSwapChainImplD3D12.h",
"d3d12/PageableD3D12.cpp",
"d3d12/PageableD3D12.h",
"d3d12/PipelineLayoutD3D12.cpp",
@ -613,8 +611,6 @@ source_set("sources") {
"opengl/Forward.h",
"opengl/GLFormat.cpp",
"opengl/GLFormat.h",
"opengl/NativeSwapChainImplGL.cpp",
"opengl/NativeSwapChainImplGL.h",
"opengl/OpenGLFunctions.cpp",
"opengl/OpenGLFunctions.h",
"opengl/OpenGLVersion.cpp",
@ -677,8 +673,6 @@ source_set("sources") {
"vulkan/FencedDeleter.cpp",
"vulkan/FencedDeleter.h",
"vulkan/Forward.h",
"vulkan/NativeSwapChainImplVk.cpp",
"vulkan/NativeSwapChainImplVk.h",
"vulkan/PipelineCacheVk.cpp",
"vulkan/PipelineCacheVk.h",
"vulkan/PipelineLayoutVk.cpp",

View File

@ -334,8 +334,6 @@ if (DAWN_ENABLE_D3D12)
"d3d12/HeapD3D12.cpp"
"d3d12/HeapD3D12.h"
"d3d12/IntegerTypes.h"
"d3d12/NativeSwapChainImplD3D12.cpp"
"d3d12/NativeSwapChainImplD3D12.h"
"d3d12/PageableD3D12.cpp"
"d3d12/PageableD3D12.h"
"d3d12/PipelineLayoutD3D12.cpp"
@ -482,8 +480,6 @@ if (DAWN_ENABLE_OPENGL)
"opengl/Forward.h"
"opengl/GLFormat.cpp"
"opengl/GLFormat.h"
"opengl/NativeSwapChainImplGL.cpp"
"opengl/NativeSwapChainImplGL.h"
"opengl/OpenGLFunctions.cpp"
"opengl/OpenGLFunctions.h"
"opengl/OpenGLVersion.cpp"
@ -550,8 +546,6 @@ if (DAWN_ENABLE_VULKAN)
"vulkan/FencedDeleter.cpp"
"vulkan/FencedDeleter.h"
"vulkan/Forward.h"
"vulkan/NativeSwapChainImplVk.cpp"
"vulkan/NativeSwapChainImplVk.h"
"vulkan/PipelineCacheVk.cpp"
"vulkan/PipelineCacheVk.h"
"vulkan/PipelineLayoutVk.cpp"

View File

@ -22,11 +22,9 @@
#include "dawn/common/Log.h"
#include "dawn/common/Math.h"
#include "dawn/common/SwapChainUtils.h"
#include "dawn/native/d3d12/D3D11on12Util.h"
#include "dawn/native/d3d12/DeviceD3D12.h"
#include "dawn/native/d3d12/ExternalImageDXGIImpl.h"
#include "dawn/native/d3d12/NativeSwapChainImplD3D12.h"
#include "dawn/native/d3d12/ResidencyManagerD3D12.h"
#include "dawn/native/d3d12/TextureD3D12.h"
@ -36,21 +34,6 @@ ComPtr<ID3D12Device> GetD3D12Device(WGPUDevice device) {
return ToBackend(FromAPI(device))->GetD3D12Device();
}
DawnSwapChainImplementation CreateNativeSwapChainImpl(WGPUDevice device, HWND window) {
Device* backendDevice = ToBackend(FromAPI(device));
DawnSwapChainImplementation impl;
impl = CreateSwapChainImplementation(new NativeSwapChainImpl(backendDevice, window));
impl.textureUsage = WGPUTextureUsage_Present;
return impl;
}
WGPUTextureFormat GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain) {
NativeSwapChainImpl* impl = reinterpret_cast<NativeSwapChainImpl*>(swapChain->userData);
return static_cast<WGPUTextureFormat>(impl->GetPreferredFormat());
}
ExternalImageDescriptorDXGISharedHandle::ExternalImageDescriptorDXGISharedHandle()
: ExternalImageDescriptor(ExternalImageType::DXGISharedHandle) {}

View File

@ -1,118 +0,0 @@
// Copyright 2018 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn/native/d3d12/NativeSwapChainImplD3D12.h"
#include "dawn/common/Assert.h"
#include "dawn/native/d3d/UtilsD3D.h"
#include "dawn/native/d3d12/DeviceD3D12.h"
#include "dawn/native/d3d12/TextureD3D12.h"
namespace dawn::native::d3d12 {
namespace {
DXGI_USAGE D3D12SwapChainBufferUsage(WGPUTextureUsage allowedUsages) {
DXGI_USAGE usage = DXGI_CPU_ACCESS_NONE;
if (allowedUsages & WGPUTextureUsage_TextureBinding) {
usage |= DXGI_USAGE_SHADER_INPUT;
}
if (allowedUsages & WGPUTextureUsage_StorageBinding) {
usage |= DXGI_USAGE_UNORDERED_ACCESS;
}
if (allowedUsages & WGPUTextureUsage_RenderAttachment) {
usage |= DXGI_USAGE_RENDER_TARGET_OUTPUT;
}
return usage;
}
static constexpr unsigned int kFrameCount = 3;
} // anonymous namespace
NativeSwapChainImpl::NativeSwapChainImpl(Device* device, HWND window)
: mWindow(window), mDevice(device), mInterval(1) {}
NativeSwapChainImpl::~NativeSwapChainImpl() {}
void NativeSwapChainImpl::Init(DawnWSIContextD3D12* /*context*/) {}
DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format,
WGPUTextureUsage usage,
uint32_t width,
uint32_t height) {
ASSERT(width > 0);
ASSERT(height > 0);
ASSERT(format == static_cast<WGPUTextureFormat>(GetPreferredFormat()));
ComPtr<IDXGIFactory4> factory = mDevice->GetFactory();
ComPtr<ID3D12CommandQueue> queue = mDevice->GetCommandQueue();
mInterval = mDevice->IsToggleEnabled(Toggle::TurnOffVsync) == true ? 0 : 1;
// Create the D3D12 swapchain, assuming only two buffers for now
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.Width = width;
swapChainDesc.Height = height;
swapChainDesc.Format = d3d::DXGITextureFormat(GetPreferredFormat());
swapChainDesc.BufferUsage = D3D12SwapChainBufferUsage(usage);
swapChainDesc.BufferCount = kFrameCount;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
ComPtr<IDXGISwapChain1> swapChain1;
ASSERT_SUCCESS(factory->CreateSwapChainForHwnd(queue.Get(), mWindow, &swapChainDesc, nullptr,
nullptr, &swapChain1));
ASSERT_SUCCESS(swapChain1.As(&mSwapChain));
// Gather the resources that will be used to present to the swapchain
mBuffers.resize(kFrameCount);
for (uint32_t i = 0; i < kFrameCount; ++i) {
ASSERT_SUCCESS(mSwapChain->GetBuffer(i, IID_PPV_ARGS(&mBuffers[i])));
}
// Set the initial serial of buffers to 0 so that we don't wait on them when they are first
// used
mBufferSerials.resize(kFrameCount, ExecutionSerial(0));
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
mCurrentBuffer = mSwapChain->GetCurrentBackBufferIndex();
nextTexture->texture.ptr = mBuffers[mCurrentBuffer].Get();
// TODO(crbug.com/dawn/269) Currently we force the CPU to wait for the GPU to be finished
// with the buffer. Ideally the synchronization should be all done on the GPU.
ASSERT(mDevice->WaitForSerial(mBufferSerials[mCurrentBuffer]).IsSuccess());
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::Present() {
// This assumes the texture has already been transition to the PRESENT state.
ASSERT_SUCCESS(mSwapChain->Present(mInterval, 0));
// TODO(crbug.com/dawn/833): Make the serial ticking implicit.
ASSERT(mDevice->NextSerial().IsSuccess());
mBufferSerials[mCurrentBuffer] = mDevice->GetPendingCommandSerial();
return DAWN_SWAP_CHAIN_NO_ERROR;
}
wgpu::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
return wgpu::TextureFormat::RGBA8Unorm;
}
} // namespace dawn::native::d3d12

View File

@ -1,60 +0,0 @@
// Copyright 2018 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_DAWN_NATIVE_D3D12_NATIVESWAPCHAINIMPLD3D12_H_
#define SRC_DAWN_NATIVE_D3D12_NATIVESWAPCHAINIMPLD3D12_H_
#include <vector>
#include "dawn/native/d3d12/d3d12_platform.h"
#include "dawn/dawn_wsi.h"
#include "dawn/native/IntegerTypes.h"
#include "dawn/native/dawn_platform.h"
namespace dawn::native::d3d12 {
class Device;
class NativeSwapChainImpl {
public:
using WSIContext = DawnWSIContextD3D12;
NativeSwapChainImpl(Device* device, HWND window);
~NativeSwapChainImpl();
void Init(DawnWSIContextD3D12* context);
DawnSwapChainError Configure(WGPUTextureFormat format,
WGPUTextureUsage,
uint32_t width,
uint32_t height);
DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture* nextTexture);
DawnSwapChainError Present();
wgpu::TextureFormat GetPreferredFormat() const;
private:
HWND mWindow = nullptr;
Device* mDevice = nullptr;
UINT mInterval;
ComPtr<IDXGISwapChain3> mSwapChain = nullptr;
std::vector<ComPtr<ID3D12Resource>> mBuffers;
std::vector<ExecutionSerial> mBufferSerials;
uint32_t mCurrentBuffer;
};
} // namespace dawn::native::d3d12
#endif // SRC_DAWN_NATIVE_D3D12_NATIVESWAPCHAINIMPLD3D12_H_

View File

@ -494,29 +494,6 @@ MaybeError OldSwapChain::OnBeforePresent(TextureViewBase*) {
return {};
}
// NativeSwapChainImpl
void NativeSwapChainImpl::Init(WSIContext* context) {}
DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format,
WGPUTextureUsage,
uint32_t width,
uint32_t height) {
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::Present() {
return DAWN_SWAP_CHAIN_NO_ERROR;
}
wgpu::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
return wgpu::TextureFormat::RGBA8Unorm;
}
uint32_t Device::GetOptimalBytesPerRowAlignment() const {
return 1;
}

View File

@ -322,19 +322,6 @@ class OldSwapChain final : public OldSwapChainBase {
MaybeError OnBeforePresent(TextureViewBase*) override;
};
class NativeSwapChainImpl {
public:
using WSIContext = struct {};
void Init(WSIContext* context);
DawnSwapChainError Configure(WGPUTextureFormat format,
WGPUTextureUsage,
uint32_t width,
uint32_t height);
DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture* nextTexture);
DawnSwapChainError Present();
wgpu::TextureFormat GetPreferredFormat() const;
};
class Texture : public TextureBase {
public:
Texture(DeviceBase* device, const TextureDescriptor* descriptor, TextureState state);

View File

@ -17,16 +17,10 @@
#include "dawn/native/NullBackend.h"
#include "dawn/common/SwapChainUtils.h"
#include "dawn/native/null/DeviceNull.h"
namespace dawn::native::null {
DawnSwapChainImplementation CreateNativeSwapChainImpl() {
DawnSwapChainImplementation impl;
impl = CreateSwapChainImplementation(new NativeSwapChainImpl());
impl.textureUsage = WGPUTextureUsage_Present;
return impl;
}
// Nothing for now \o/
} // namespace dawn::native::null

View File

@ -1,86 +0,0 @@
// Copyright 2019 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn/native/opengl/NativeSwapChainImplGL.h"
#include "dawn/native/opengl/DeviceGL.h"
namespace dawn::native::opengl {
NativeSwapChainImpl::NativeSwapChainImpl(Device* device,
PresentCallback present,
void* presentUserdata)
: mPresentCallback(present), mPresentUserdata(presentUserdata), mDevice(device) {}
NativeSwapChainImpl::~NativeSwapChainImpl() {
const OpenGLFunctions& gl = mDevice->GetGL();
gl.DeleteTextures(1, &mBackTexture);
gl.DeleteFramebuffers(1, &mBackFBO);
}
void NativeSwapChainImpl::Init(DawnWSIContextGL* /*context*/) {
const OpenGLFunctions& gl = mDevice->GetGL();
gl.GenTextures(1, &mBackTexture);
gl.BindTexture(GL_TEXTURE_2D, mBackTexture);
gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
gl.GenFramebuffers(1, &mBackFBO);
gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
gl.FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mBackTexture,
0);
}
DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format,
WGPUTextureUsage usage,
uint32_t width,
uint32_t height) {
if (format != WGPUTextureFormat_RGBA8Unorm) {
return "unsupported format";
}
ASSERT(width > 0);
ASSERT(height > 0);
mWidth = width;
mHeight = height;
const OpenGLFunctions& gl = mDevice->GetGL();
gl.BindTexture(GL_TEXTURE_2D, mBackTexture);
// Reallocate the texture
gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
nextTexture->texture.u32 = mBackTexture;
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::Present() {
const OpenGLFunctions& gl = mDevice->GetGL();
gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
gl.Scissor(0, 0, mWidth, mHeight);
gl.BlitFramebuffer(0, 0, mWidth, mHeight, 0, mHeight, mWidth, 0, GL_COLOR_BUFFER_BIT,
GL_NEAREST);
mPresentCallback(mPresentUserdata);
return DAWN_SWAP_CHAIN_NO_ERROR;
}
wgpu::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
return wgpu::TextureFormat::RGBA8Unorm;
}
} // namespace dawn::native::opengl

View File

@ -1,58 +0,0 @@
// Copyright 2017 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_DAWN_NATIVE_OPENGL_NATIVESWAPCHAINIMPLGL_H_
#define SRC_DAWN_NATIVE_OPENGL_NATIVESWAPCHAINIMPLGL_H_
#include "dawn/native/OpenGLBackend.h"
#include "dawn/native/dawn_platform.h"
#include "dawn/native/opengl/opengl_platform.h"
namespace dawn::native::opengl {
class Device;
class NativeSwapChainImpl {
public:
using WSIContext = DawnWSIContextGL;
NativeSwapChainImpl(Device* device, PresentCallback present, void* presentUserdata);
~NativeSwapChainImpl();
void Init(DawnWSIContextGL* context);
DawnSwapChainError Configure(WGPUTextureFormat format,
WGPUTextureUsage,
uint32_t width,
uint32_t height);
DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture* nextTexture);
DawnSwapChainError Present();
wgpu::TextureFormat GetPreferredFormat() const;
private:
PresentCallback mPresentCallback;
void* mPresentUserdata;
uint32_t mWidth = 0;
uint32_t mHeight = 0;
GLuint mBackFBO = 0;
GLuint mBackTexture = 0;
Device* mDevice = nullptr;
};
} // namespace dawn::native::opengl
#endif // SRC_DAWN_NATIVE_OPENGL_NATIVESWAPCHAINIMPLGL_H_

View File

@ -17,9 +17,7 @@
#include "dawn/native/OpenGLBackend.h"
#include "dawn/common/SwapChainUtils.h"
#include "dawn/native/opengl/DeviceGL.h"
#include "dawn/native/opengl/NativeSwapChainImplGL.h"
namespace dawn::native::opengl {
@ -29,24 +27,6 @@ AdapterDiscoveryOptions::AdapterDiscoveryOptions(WGPUBackendType type)
AdapterDiscoveryOptionsES::AdapterDiscoveryOptionsES()
: AdapterDiscoveryOptions(WGPUBackendType_OpenGLES) {}
DawnSwapChainImplementation CreateNativeSwapChainImpl(WGPUDevice device,
PresentCallback present,
void* presentUserdata) {
Device* backendDevice = ToBackend(FromAPI(device));
DawnSwapChainImplementation impl;
impl = CreateSwapChainImplementation(
new NativeSwapChainImpl(backendDevice, present, presentUserdata));
impl.textureUsage = WGPUTextureUsage_Present;
return impl;
}
WGPUTextureFormat GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain) {
NativeSwapChainImpl* impl = reinterpret_cast<NativeSwapChainImpl*>(swapChain->userData);
return static_cast<WGPUTextureFormat>(impl->GetPreferredFormat());
}
ExternalImageDescriptorEGLImage::ExternalImageDescriptorEGLImage()
: ExternalImageDescriptor(ExternalImageType::EGLImage) {}

View File

@ -1,225 +0,0 @@
// Copyright 2018 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn/native/vulkan/NativeSwapChainImplVk.h"
#include <limits>
#include "dawn/native/vulkan/DeviceVk.h"
#include "dawn/native/vulkan/FencedDeleter.h"
#include "dawn/native/vulkan/TextureVk.h"
namespace dawn::native::vulkan {
namespace {
bool chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes,
bool turnOffVsync,
VkPresentModeKHR* presentMode) {
if (turnOffVsync) {
for (const auto& availablePresentMode : availablePresentModes) {
if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
*presentMode = availablePresentMode;
return true;
}
}
return false;
}
*presentMode = VK_PRESENT_MODE_FIFO_KHR;
return true;
}
bool ChooseSurfaceConfig(const VulkanSurfaceInfo& info,
NativeSwapChainImpl::ChosenConfig* config,
bool turnOffVsync) {
VkPresentModeKHR presentMode;
if (!chooseSwapPresentMode(info.presentModes, turnOffVsync, &presentMode)) {
return false;
}
// TODO(crbug.com/dawn/269): For now this is hardcoded to what works with one NVIDIA
// driver. Need to generalize
config->nativeFormat = VK_FORMAT_B8G8R8A8_UNORM;
config->colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
config->format = wgpu::TextureFormat::BGRA8Unorm;
config->minImageCount = 3;
// TODO(crbug.com/dawn/269): This is upside down compared to what we want, at least
// on Linux
config->preTransform = info.capabilities.currentTransform;
config->presentMode = presentMode;
config->compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
return true;
}
} // anonymous namespace
NativeSwapChainImpl::NativeSwapChainImpl(Device* device, VkSurfaceKHR surface)
: mSurface(surface), mDevice(device) {
// Call this immediately, so that BackendBinding::GetPreferredSwapChainTextureFormat
// will return a correct result before a SwapChain is created.
UpdateSurfaceConfig();
}
NativeSwapChainImpl::~NativeSwapChainImpl() {
if (mSwapChain != VK_NULL_HANDLE) {
mDevice->GetFencedDeleter()->DeleteWhenUnused(mSwapChain);
mSwapChain = VK_NULL_HANDLE;
}
if (mSurface != VK_NULL_HANDLE) {
mDevice->GetFencedDeleter()->DeleteWhenUnused(mSurface);
mSurface = VK_NULL_HANDLE;
}
}
void NativeSwapChainImpl::UpdateSurfaceConfig() {
if (mDevice->ConsumedError(GatherSurfaceInfo(*ToBackend(mDevice->GetAdapter()), mSurface),
&mInfo)) {
ASSERT(false);
}
if (!ChooseSurfaceConfig(mInfo, &mConfig, mDevice->IsToggleEnabled(Toggle::TurnOffVsync))) {
ASSERT(false);
}
}
void NativeSwapChainImpl::Init(DawnWSIContextVulkan* /*context*/) {
UpdateSurfaceConfig();
}
DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format,
WGPUTextureUsage usage,
uint32_t width,
uint32_t height) {
UpdateSurfaceConfig();
ASSERT(mInfo.capabilities.minImageExtent.width <= width);
ASSERT(mInfo.capabilities.maxImageExtent.width >= width);
ASSERT(mInfo.capabilities.minImageExtent.height <= height);
ASSERT(mInfo.capabilities.maxImageExtent.height >= height);
ASSERT(format == static_cast<WGPUTextureFormat>(GetPreferredFormat()));
// TODO(crbug.com/dawn/269): need to check usage works too
// Create the swapchain with the configuration we chose
VkSwapchainKHR oldSwapchain = mSwapChain;
VkSwapchainCreateInfoKHR createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.pNext = nullptr;
createInfo.flags = 0;
createInfo.surface = mSurface;
createInfo.minImageCount = mConfig.minImageCount;
createInfo.imageFormat = mConfig.nativeFormat;
createInfo.imageColorSpace = mConfig.colorSpace;
createInfo.imageExtent.width = width;
createInfo.imageExtent.height = height;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VulkanImageUsage(static_cast<wgpu::TextureUsage>(usage),
mDevice->GetValidInternalFormat(mConfig.format));
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
createInfo.preTransform = mConfig.preTransform;
createInfo.compositeAlpha = mConfig.compositeAlpha;
createInfo.presentMode = mConfig.presentMode;
createInfo.clipped = false;
createInfo.oldSwapchain = oldSwapchain;
if (mDevice->fn.CreateSwapchainKHR(mDevice->GetVkDevice(), &createInfo, nullptr,
&*mSwapChain) != VK_SUCCESS) {
ASSERT(false);
}
// Gather the swapchain's images. Implementations are allowed to return more images than the
// number we asked for.
uint32_t count = 0;
if (mDevice->fn.GetSwapchainImagesKHR(mDevice->GetVkDevice(), mSwapChain, &count, nullptr) !=
VK_SUCCESS) {
ASSERT(false);
}
ASSERT(count >= mConfig.minImageCount);
mSwapChainImages.resize(count);
if (mDevice->fn.GetSwapchainImagesKHR(mDevice->GetVkDevice(), mSwapChain, &count,
AsVkArray(mSwapChainImages.data())) != VK_SUCCESS) {
ASSERT(false);
}
if (oldSwapchain != VK_NULL_HANDLE) {
mDevice->GetFencedDeleter()->DeleteWhenUnused(oldSwapchain);
}
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
// Transiently create a semaphore that will be signaled when the presentation engine is done
// with the swapchain image. Further operations on the image will wait for this semaphore.
VkSemaphore semaphore = VK_NULL_HANDLE;
{
VkSemaphoreCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.flags = 0;
if (mDevice->fn.CreateSemaphore(mDevice->GetVkDevice(), &createInfo, nullptr,
&*semaphore) != VK_SUCCESS) {
ASSERT(false);
}
}
if (mDevice->fn.AcquireNextImageKHR(mDevice->GetVkDevice(), mSwapChain,
std::numeric_limits<uint64_t>::max(), semaphore, VkFence{},
&mLastImageIndex) != VK_SUCCESS) {
ASSERT(false);
}
nextTexture->texture.u64 =
#if DAWN_PLATFORM_IS(64_BIT)
reinterpret_cast<uint64_t>
#endif
(*mSwapChainImages[mLastImageIndex]);
mDevice->GetPendingRecordingContext()->waitSemaphores.push_back(semaphore);
return DAWN_SWAP_CHAIN_NO_ERROR;
}
DawnSwapChainError NativeSwapChainImpl::Present() {
// This assumes that the image has already been transitioned to the PRESENT layout and
// writes were made available to the stage.
// Assuming that the present queue is the same as the graphics queue, the proper
// synchronization has already been done on the queue so we don't need to wait on any
// semaphores.
VkPresentInfoKHR presentInfo;
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pNext = nullptr;
presentInfo.waitSemaphoreCount = 0;
presentInfo.pWaitSemaphores = nullptr;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &*mSwapChain;
presentInfo.pImageIndices = &mLastImageIndex;
presentInfo.pResults = nullptr;
VkQueue queue = mDevice->GetQueue();
if (mDevice->fn.QueuePresentKHR(queue, &presentInfo) != VK_SUCCESS) {
ASSERT(false);
}
return DAWN_SWAP_CHAIN_NO_ERROR;
}
wgpu::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
return mConfig.format;
}
} // namespace dawn::native::vulkan

View File

@ -1,72 +0,0 @@
// Copyright 2018 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_DAWN_NATIVE_VULKAN_NATIVESWAPCHAINIMPLVK_H_
#define SRC_DAWN_NATIVE_VULKAN_NATIVESWAPCHAINIMPLVK_H_
#include <vector>
#include "dawn/dawn_wsi.h"
#include "dawn/native/dawn_platform.h"
#include "dawn/native/vulkan/VulkanInfo.h"
namespace dawn::native::vulkan {
class Device;
class NativeSwapChainImpl {
public:
using WSIContext = DawnWSIContextVulkan;
NativeSwapChainImpl(Device* device, VkSurfaceKHR surface);
~NativeSwapChainImpl();
void Init(DawnWSIContextVulkan* context);
DawnSwapChainError Configure(WGPUTextureFormat format,
WGPUTextureUsage,
uint32_t width,
uint32_t height);
DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture* nextTexture);
DawnSwapChainError Present();
wgpu::TextureFormat GetPreferredFormat() const;
struct ChosenConfig {
VkFormat nativeFormat;
wgpu::TextureFormat format;
VkColorSpaceKHR colorSpace;
VkSurfaceTransformFlagBitsKHR preTransform;
uint32_t minImageCount;
VkPresentModeKHR presentMode;
VkCompositeAlphaFlagBitsKHR compositeAlpha;
};
private:
void UpdateSurfaceConfig();
VkSurfaceKHR mSurface = VK_NULL_HANDLE;
VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
std::vector<VkImage> mSwapChainImages;
uint32_t mLastImageIndex = 0;
VulkanSurfaceInfo mInfo;
ChosenConfig mConfig;
Device* mDevice = nullptr;
};
} // namespace dawn::native::vulkan
#endif // SRC_DAWN_NATIVE_VULKAN_NATIVESWAPCHAINIMPLVK_H_

View File

@ -21,9 +21,7 @@
#include "dawn/native/VulkanBackend.h"
#include "dawn/common/SwapChainUtils.h"
#include "dawn/native/vulkan/DeviceVk.h"
#include "dawn/native/vulkan/NativeSwapChainImplVk.h"
#include "dawn/native/vulkan/TextureVk.h"
namespace dawn::native::vulkan {
@ -38,25 +36,6 @@ DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device, con
return (*backendDevice->fn.GetInstanceProcAddr)(backendDevice->GetVkInstance(), pName);
}
// Explicitly export this function because it uses the "native" type for surfaces while the
// header as seen in this file uses the wrapped type.
DAWN_NATIVE_EXPORT DawnSwapChainImplementation
CreateNativeSwapChainImpl(WGPUDevice device, ::VkSurfaceKHR surfaceNative) {
Device* backendDevice = ToBackend(FromAPI(device));
VkSurfaceKHR surface = VkSurfaceKHR::CreateFromHandle(surfaceNative);
DawnSwapChainImplementation impl;
impl = CreateSwapChainImplementation(new NativeSwapChainImpl(backendDevice, surface));
impl.textureUsage = WGPUTextureUsage_Present;
return impl;
}
WGPUTextureFormat GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain) {
NativeSwapChainImpl* impl = reinterpret_cast<NativeSwapChainImpl*>(swapChain->userData);
return static_cast<WGPUTextureFormat>(impl->GetPreferredFormat());
}
AdapterDiscoveryOptions::AdapterDiscoveryOptions()
: AdapterDiscoveryOptionsBase(WGPUBackendType_Vulkan) {}