dawn-cmake/src/backend/SwapChain.cpp

119 lines
3.8 KiB
C++
Raw Normal View History

// Copyright 2017 The NXT 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 "backend/SwapChain.h"
#include "backend/Device.h"
#include "backend/Texture.h"
namespace backend {
// SwapChain
SwapChainBase::SwapChainBase(SwapChainBuilder* builder)
2017-11-23 18:32:51 +00:00
: mDevice(builder->mDevice), mImplementation(builder->mImplementation) {
}
SwapChainBase::~SwapChainBase() {
2017-07-28 01:30:57 +00:00
const auto& im = GetImplementation();
im.Destroy(im.userData);
}
DeviceBase* SwapChainBase::GetDevice() {
2017-11-23 18:32:51 +00:00
return mDevice;
}
void SwapChainBase::Configure(nxt::TextureFormat format, nxt::TextureUsageBit allowedUsage, uint32_t width, uint32_t height) {
if (width == 0 || height == 0) {
2017-11-23 18:32:51 +00:00
mDevice->HandleError("Swap chain cannot be configured to zero size");
return;
}
allowedUsage |= nxt::TextureUsageBit::Present;
2017-11-23 18:32:51 +00:00
mFormat = format;
mAllowedUsage = allowedUsage;
mWidth = width;
mHeight = height;
mImplementation.Configure(mImplementation.userData,
static_cast<nxtTextureFormat>(format), static_cast<nxtTextureUsageBit>(allowedUsage), width, height);
}
TextureBase* SwapChainBase::GetNextTexture() {
2017-11-23 18:32:51 +00:00
if (mWidth == 0) {
// If width is 0, it implies swap chain has never been configured
2017-11-23 18:32:51 +00:00
mDevice->HandleError("Swap chain needs to be configured before GetNextTexture");
return nullptr;
}
2017-11-23 18:32:51 +00:00
auto* builder = mDevice->CreateTextureBuilder();
builder->SetDimension(nxt::TextureDimension::e2D);
2017-11-23 18:32:51 +00:00
builder->SetExtent(mWidth, mHeight, 1);
builder->SetFormat(mFormat);
builder->SetMipLevels(1);
2017-11-23 18:32:51 +00:00
builder->SetAllowedUsage(mAllowedUsage);
auto* texture = GetNextTextureImpl(builder);
2017-11-23 18:32:51 +00:00
mLastNextTexture = texture;
return texture;
}
void SwapChainBase::Present(TextureBase* texture) {
2017-11-23 18:32:51 +00:00
if (texture != mLastNextTexture) {
mDevice->HandleError("Tried to present something other than the last NextTexture");
return;
}
if (texture->GetUsage() != nxt::TextureUsageBit::Present) {
2017-11-23 18:32:51 +00:00
mDevice->HandleError("Texture has not been transitioned to the Present usage");
return;
}
2017-11-23 18:32:51 +00:00
mImplementation.Present(mImplementation.userData);
}
const nxtSwapChainImplementation& SwapChainBase::GetImplementation() {
2017-11-23 18:32:51 +00:00
return mImplementation;
}
// SwapChain Builder
SwapChainBuilder::SwapChainBuilder(DeviceBase* device)
: Builder(device) {
}
SwapChainBase* SwapChainBuilder::GetResultImpl() {
2017-11-23 18:32:51 +00:00
if (!mImplementation.Init) {
HandleError("Implementation not set");
return nullptr;
}
2017-11-23 18:32:51 +00:00
return mDevice->CreateSwapChain(this);
}
void SwapChainBuilder::SetImplementation(uint64_t implementation) {
if (!implementation) {
HandleError("Implementation pointer is invalid");
return;
}
nxtSwapChainImplementation& impl = *reinterpret_cast<nxtSwapChainImplementation*>(implementation);
2017-07-28 01:30:57 +00:00
if (!impl.Init || !impl.Destroy || !impl.Configure ||
!impl.GetNextTexture || !impl.Present) {
HandleError("Implementation is incomplete");
return;
}
2017-11-23 18:32:51 +00:00
mImplementation = impl;
}
}