dawn-cmake/src/backend/Texture.cpp

219 lines
6.6 KiB
C++
Raw Normal View History

// 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.
#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) {
}
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;
}
TextureViewBuilder* TextureBase::CreateTextureViewBuilder() {
2017-11-23 18:32:51 +00:00
return new TextureViewBuilder(mDevice, this);
}
// 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,
};
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;
}
// 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;
}
// 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