dawn-cmake/src/backend/Texture.cpp

289 lines
8.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/Texture.h"
#include "backend/Device.h"
#include "common/Assert.h"
namespace backend {
uint32_t TextureFormatPixelSize(nxt::TextureFormat format) {
switch (format) {
case nxt::TextureFormat::R8Unorm:
case nxt::TextureFormat::R8Uint:
return 1;
case nxt::TextureFormat::R8G8Unorm:
case nxt::TextureFormat::R8G8Uint:
return 2;
case nxt::TextureFormat::R8G8B8A8Unorm:
2017-08-14 16:49:41 +00:00
case nxt::TextureFormat::R8G8B8A8Uint:
2017-09-21 17:12:49 +00:00
case nxt::TextureFormat::B8G8R8A8Unorm:
return 4;
case nxt::TextureFormat::D32FloatS8Uint:
return 8;
default:
UNREACHABLE();
}
}
bool TextureFormatHasDepth(nxt::TextureFormat format) {
switch (format) {
case nxt::TextureFormat::D32FloatS8Uint:
return true;
default:
return false;
}
}
bool TextureFormatHasStencil(nxt::TextureFormat format) {
switch (format) {
case nxt::TextureFormat::D32FloatS8Uint:
return true;
default:
return false;
}
}
bool TextureFormatHasDepthOrStencil(nxt::TextureFormat format) {
switch (format) {
case nxt::TextureFormat::D32FloatS8Uint:
return true;
default:
return false;
}
}
// TextureBase
TextureBase::TextureBase(TextureBuilder* builder)
2017-11-24 18:59:42 +00:00
: mDevice(builder->mDevice),
mDimension(builder->mDimension),
mFormat(builder->mFormat),
mWidth(builder->mWidth),
mHeight(builder->mHeight),
mDepth(builder->mDepth),
mNumMipLevels(builder->mNumMipLevels),
mAllowedUsage(builder->mAllowedUsage),
mCurrentUsage(builder->mCurrentUsage) {
}
DeviceBase* TextureBase::GetDevice() const {
2017-11-23 18:32:51 +00:00
return mDevice;
}
nxt::TextureDimension TextureBase::GetDimension() const {
2017-11-23 18:32:51 +00:00
return mDimension;
}
nxt::TextureFormat TextureBase::GetFormat() const {
2017-11-23 18:32:51 +00:00
return mFormat;
}
uint32_t TextureBase::GetWidth() const {
2017-11-23 18:32:51 +00:00
return mWidth;
}
uint32_t TextureBase::GetHeight() const {
2017-11-23 18:32:51 +00:00
return mHeight;
}
uint32_t TextureBase::GetDepth() const {
2017-11-23 18:32:51 +00:00
return mDepth;
}
uint32_t TextureBase::GetNumMipLevels() const {
2017-11-23 18:32:51 +00:00
return mNumMipLevels;
}
nxt::TextureUsageBit TextureBase::GetAllowedUsage() const {
2017-11-23 18:32:51 +00:00
return mAllowedUsage;
}
nxt::TextureUsageBit TextureBase::GetUsage() const {
2017-11-23 18:32:51 +00:00
return mCurrentUsage;
}
TextureViewBuilder* TextureBase::CreateTextureViewBuilder() {
2017-11-23 18:32:51 +00:00
return new TextureViewBuilder(mDevice, this);
}
bool TextureBase::IsFrozen() const {
2017-11-23 18:32:51 +00:00
return mIsFrozen;
}
bool TextureBase::HasFrozenUsage(nxt::TextureUsageBit usage) const {
2017-11-23 18:32:51 +00:00
return mIsFrozen && (usage & mAllowedUsage);
}
2017-11-24 18:59:42 +00:00
bool TextureBase::IsUsagePossible(nxt::TextureUsageBit allowedUsage,
nxt::TextureUsageBit usage) {
bool allowed = (usage & allowedUsage) == usage;
bool singleUse = nxt::HasZeroOrOneBits(usage);
return allowed && singleUse;
}
bool TextureBase::IsTransitionPossible(nxt::TextureUsageBit usage) const {
2017-11-23 18:32:51 +00:00
if (mIsFrozen) {
return false;
}
2017-11-23 18:32:51 +00:00
if (mCurrentUsage == nxt::TextureUsageBit::Present) {
return false;
}
2017-11-23 18:32:51 +00:00
return IsUsagePossible(mAllowedUsage, usage);
}
void TextureBase::UpdateUsageInternal(nxt::TextureUsageBit usage) {
ASSERT(IsTransitionPossible(usage));
2017-11-23 18:32:51 +00:00
mCurrentUsage = usage;
}
void TextureBase::TransitionUsage(nxt::TextureUsageBit usage) {
if (!IsTransitionPossible(usage)) {
2017-11-23 18:32:51 +00:00
mDevice->HandleError("Texture frozen or usage not allowed");
return;
}
2017-11-23 18:32:51 +00:00
TransitionUsageImpl(mCurrentUsage, usage);
2017-08-30 23:15:36 +00:00
UpdateUsageInternal(usage);
}
void TextureBase::FreezeUsage(nxt::TextureUsageBit usage) {
if (!IsTransitionPossible(usage)) {
2017-11-23 18:32:51 +00:00
mDevice->HandleError("Texture frozen or usage not allowed");
return;
}
2017-11-23 18:32:51 +00:00
mAllowedUsage = usage;
TransitionUsageImpl(mCurrentUsage, usage);
2017-08-30 23:15:36 +00:00
UpdateUsageInternal(usage);
2017-11-23 18:32:51 +00:00
mIsFrozen = true;
}
// TextureBuilder
enum TextureSetProperties {
TEXTURE_PROPERTY_DIMENSION = 0x1,
TEXTURE_PROPERTY_EXTENT = 0x2,
TEXTURE_PROPERTY_FORMAT = 0x4,
TEXTURE_PROPERTY_MIP_LEVELS = 0x8,
TEXTURE_PROPERTY_ALLOWED_USAGE = 0x10,
TEXTURE_PROPERTY_INITIAL_USAGE = 0x20,
};
2017-11-24 18:59:42 +00:00
TextureBuilder::TextureBuilder(DeviceBase* device) : Builder(device) {
}
TextureBase* TextureBuilder::GetResultImpl() {
constexpr int allProperties = TEXTURE_PROPERTY_DIMENSION | TEXTURE_PROPERTY_EXTENT |
2017-11-24 18:59:42 +00:00
TEXTURE_PROPERTY_FORMAT | TEXTURE_PROPERTY_MIP_LEVELS |
TEXTURE_PROPERTY_ALLOWED_USAGE;
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & allProperties) != allProperties) {
HandleError("Texture missing properties");
return nullptr;
}
2017-11-23 18:32:51 +00:00
if (!TextureBase::IsUsagePossible(mAllowedUsage, mCurrentUsage)) {
HandleError("Initial texture usage is not allowed");
return nullptr;
}
// TODO(cwallez@chromium.org): check stuff based on the dimension
2017-11-23 18:32:51 +00:00
return mDevice->CreateTexture(this);
}
void TextureBuilder::SetDimension(nxt::TextureDimension dimension) {
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & TEXTURE_PROPERTY_DIMENSION) != 0) {
HandleError("Texture dimension property set multiple times");
return;
}
2017-11-23 18:32:51 +00:00
mPropertiesSet |= TEXTURE_PROPERTY_DIMENSION;
mDimension = dimension;
}
void TextureBuilder::SetExtent(uint32_t width, uint32_t height, uint32_t depth) {
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & TEXTURE_PROPERTY_EXTENT) != 0) {
HandleError("Texture extent property set multiple times");
return;
}
if (width == 0 || height == 0 || depth == 0) {
HandleError("Cannot create an empty texture");
return;
}
2017-11-23 18:32:51 +00:00
mPropertiesSet |= TEXTURE_PROPERTY_EXTENT;
mWidth = width;
mHeight = height;
mDepth = depth;
}
void TextureBuilder::SetFormat(nxt::TextureFormat format) {
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & TEXTURE_PROPERTY_FORMAT) != 0) {
HandleError("Texture format property set multiple times");
return;
}
2017-11-23 18:32:51 +00:00
mPropertiesSet |= TEXTURE_PROPERTY_FORMAT;
mFormat = format;
}
void TextureBuilder::SetMipLevels(uint32_t numMipLevels) {
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & TEXTURE_PROPERTY_MIP_LEVELS) != 0) {
HandleError("Texture mip levels property set multiple times");
return;
}
2017-11-23 18:32:51 +00:00
mPropertiesSet |= TEXTURE_PROPERTY_MIP_LEVELS;
mNumMipLevels = numMipLevels;
}
void TextureBuilder::SetAllowedUsage(nxt::TextureUsageBit usage) {
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & TEXTURE_PROPERTY_ALLOWED_USAGE) != 0) {
HandleError("Texture allowed usage property set multiple times");
return;
}
2017-11-23 18:32:51 +00:00
mPropertiesSet |= TEXTURE_PROPERTY_ALLOWED_USAGE;
mAllowedUsage = usage;
}
void TextureBuilder::SetInitialUsage(nxt::TextureUsageBit usage) {
2017-11-23 18:32:51 +00:00
if ((mPropertiesSet & TEXTURE_PROPERTY_INITIAL_USAGE) != 0) {
HandleError("Texture initial usage property set multiple times");
return;
}
2017-11-23 18:32:51 +00:00
mPropertiesSet |= TEXTURE_PROPERTY_INITIAL_USAGE;
mCurrentUsage = usage;
}
// TextureViewBase
2017-11-24 18:59:42 +00:00
TextureViewBase::TextureViewBase(TextureViewBuilder* builder) : mTexture(builder->mTexture) {
}
const TextureBase* TextureViewBase::GetTexture() const {
return mTexture.Get();
}
TextureBase* TextureViewBase::GetTexture() {
2017-11-23 18:32:51 +00:00
return mTexture.Get();
}
// TextureViewBuilder
TextureViewBuilder::TextureViewBuilder(DeviceBase* device, TextureBase* texture)
2017-11-23 18:32:51 +00:00
: Builder(device), mTexture(texture) {
}
TextureViewBase* TextureViewBuilder::GetResultImpl() {
2017-11-23 18:32:51 +00:00
return mDevice->CreateTextureView(this);
}
2017-11-24 18:59:42 +00:00
} // namespace backend