2017-07-19 22:41:17 +00:00
|
|
|
// 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)
|
|
|
|
: device(builder->device), implementation(builder->implementation) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SwapChainBase::~SwapChainBase() {
|
2017-07-28 01:30:57 +00:00
|
|
|
const auto& im = GetImplementation();
|
|
|
|
im.Destroy(im.userData);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceBase* SwapChainBase::GetDevice() {
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2017-08-30 23:53:27 +00:00
|
|
|
void SwapChainBase::Configure(nxt::TextureFormat format, nxt::TextureUsageBit allowedUsage, uint32_t width, uint32_t height) {
|
2017-07-19 22:41:17 +00:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
device->HandleError("Swap chain cannot be configured to zero size");
|
|
|
|
return;
|
|
|
|
}
|
2017-08-29 20:35:05 +00:00
|
|
|
allowedUsage |= nxt::TextureUsageBit::Present;
|
2017-07-19 22:41:17 +00:00
|
|
|
|
|
|
|
this->format = format;
|
2017-08-29 20:35:05 +00:00
|
|
|
this->allowedUsage = allowedUsage;
|
2017-07-19 22:41:17 +00:00
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
2017-07-28 01:30:57 +00:00
|
|
|
implementation.Configure(implementation.userData,
|
2017-08-30 23:53:27 +00:00
|
|
|
static_cast<nxtTextureFormat>(format), static_cast<nxtTextureUsageBit>(allowedUsage), width, height);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureBase* SwapChainBase::GetNextTexture() {
|
|
|
|
if (width == 0) {
|
|
|
|
// If width is 0, it implies swap chain has never been configured
|
|
|
|
device->HandleError("Swap chain needs to be configured before GetNextTexture");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* builder = device->CreateTextureBuilder();
|
|
|
|
builder->SetDimension(nxt::TextureDimension::e2D);
|
|
|
|
builder->SetExtent(width, height, 1);
|
|
|
|
builder->SetFormat(format);
|
|
|
|
builder->SetMipLevels(1);
|
2017-08-29 20:35:05 +00:00
|
|
|
builder->SetAllowedUsage(allowedUsage);
|
2017-07-19 22:41:17 +00:00
|
|
|
|
|
|
|
auto* texture = GetNextTextureImpl(builder);
|
|
|
|
lastNextTexture = texture;
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwapChainBase::Present(TextureBase* texture) {
|
|
|
|
if (texture != lastNextTexture) {
|
|
|
|
device->HandleError("Tried to present something other than the last NextTexture");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (texture->GetUsage() != nxt::TextureUsageBit::Present) {
|
|
|
|
device->HandleError("Texture has not been transitioned to the Present usage");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
implementation.Present(implementation.userData);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nxtSwapChainImplementation& SwapChainBase::GetImplementation() {
|
|
|
|
return implementation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SwapChain Builder
|
|
|
|
|
|
|
|
SwapChainBuilder::SwapChainBuilder(DeviceBase* device)
|
|
|
|
: Builder(device) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SwapChainBase* SwapChainBuilder::GetResultImpl() {
|
|
|
|
if (!implementation.Init) {
|
|
|
|
HandleError("Implementation not set");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return device->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 ||
|
2017-07-19 22:41:17 +00:00
|
|
|
!impl.GetNextTexture || !impl.Present) {
|
|
|
|
HandleError("Implementation is incomplete");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->implementation = impl;
|
|
|
|
}
|
|
|
|
}
|