2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/Texture.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/Device.h"
|
2017-07-10 17:46:05 +00:00
|
|
|
#include "common/Assert.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
namespace backend {
|
|
|
|
|
2017-07-22 00:00:22 +00:00
|
|
|
uint32_t TextureFormatPixelSize(nxt::TextureFormat format) {
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (format) {
|
2018-01-22 19:09:08 +00:00
|
|
|
case nxt::TextureFormat::R8Unorm:
|
|
|
|
case nxt::TextureFormat::R8Uint:
|
|
|
|
return 1;
|
|
|
|
case nxt::TextureFormat::R8G8Unorm:
|
|
|
|
case nxt::TextureFormat::R8G8Uint:
|
|
|
|
return 2;
|
2017-04-20 18:38:20 +00:00
|
|
|
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:
|
2017-04-20 18:38:20 +00:00
|
|
|
return 4;
|
2017-07-17 16:03:16 +00:00
|
|
|
case nxt::TextureFormat::D32FloatS8Uint:
|
|
|
|
return 8;
|
2017-07-11 00:31:47 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 16:03:16 +00:00
|
|
|
bool TextureFormatHasDepth(nxt::TextureFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case nxt::TextureFormat::D32FloatS8Uint:
|
|
|
|
return true;
|
|
|
|
default:
|
2018-01-22 19:09:08 +00:00
|
|
|
return false;
|
2017-07-17 16:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureFormatHasStencil(nxt::TextureFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case nxt::TextureFormat::D32FloatS8Uint:
|
|
|
|
return true;
|
|
|
|
default:
|
2018-01-22 19:09:08 +00:00
|
|
|
return false;
|
2017-07-17 16:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:36:20 +00:00
|
|
|
bool TextureFormatHasDepthOrStencil(nxt::TextureFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case nxt::TextureFormat::D32FloatS8Uint:
|
|
|
|
return true;
|
|
|
|
default:
|
2018-01-22 19:09:08 +00:00
|
|
|
return false;
|
2017-08-11 21:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
// 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),
|
2018-07-09 13:15:07 +00:00
|
|
|
mAllowedUsage(builder->mAllowedUsage) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:46:51 +00:00
|
|
|
DeviceBase* TextureBase::GetDevice() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice;
|
2017-05-10 13:30:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
nxt::TextureDimension TextureBase::GetDimension() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDimension;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
nxt::TextureFormat TextureBase::GetFormat() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mFormat;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
uint32_t TextureBase::GetWidth() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mWidth;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
uint32_t TextureBase::GetHeight() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mHeight;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
uint32_t TextureBase::GetDepth() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDepth;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
uint32_t TextureBase::GetNumMipLevels() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mNumMipLevels;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
nxt::TextureUsageBit TextureBase::GetAllowedUsage() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mAllowedUsage;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureViewBuilder* TextureBase::CreateTextureViewBuilder() {
|
2017-11-23 18:32:51 +00:00
|
|
|
return new TextureViewBuilder(mDevice, this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
TextureBase* TextureBuilder::GetResultImpl() {
|
2017-04-20 18:38:20 +00:00
|
|
|
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) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture missing properties");
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(cwallez@chromium.org): check stuff based on the dimension
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice->CreateTexture(this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureBuilder::SetDimension(nxt::TextureDimension dimension) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & TEXTURE_PROPERTY_DIMENSION) != 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture dimension property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mPropertiesSet |= TEXTURE_PROPERTY_DIMENSION;
|
|
|
|
mDimension = dimension;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture extent property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width == 0 || height == 0 || depth == 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Cannot create an empty texture");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mPropertiesSet |= TEXTURE_PROPERTY_EXTENT;
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
mDepth = depth;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureBuilder::SetFormat(nxt::TextureFormat format) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & TEXTURE_PROPERTY_FORMAT) != 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture format property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mPropertiesSet |= TEXTURE_PROPERTY_FORMAT;
|
|
|
|
mFormat = format;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureBuilder::SetMipLevels(uint32_t numMipLevels) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & TEXTURE_PROPERTY_MIP_LEVELS) != 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture mip levels property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mPropertiesSet |= TEXTURE_PROPERTY_MIP_LEVELS;
|
|
|
|
mNumMipLevels = numMipLevels;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureBuilder::SetAllowedUsage(nxt::TextureUsageBit usage) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & TEXTURE_PROPERTY_ALLOWED_USAGE) != 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture allowed usage property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mPropertiesSet |= TEXTURE_PROPERTY_ALLOWED_USAGE;
|
|
|
|
mAllowedUsage = usage;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TextureViewBase
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
TextureViewBase::TextureViewBase(TextureViewBuilder* builder) : mTexture(builder->mTexture) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:10:13 +00:00
|
|
|
const TextureBase* TextureViewBase::GetTexture() const {
|
|
|
|
return mTexture.Get();
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
TextureBase* TextureViewBase::GetTexture() {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mTexture.Get();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TextureViewBuilder
|
|
|
|
|
|
|
|
TextureViewBuilder::TextureViewBuilder(DeviceBase* device, TextureBase* texture)
|
2017-11-23 18:32:51 +00:00
|
|
|
: Builder(device), mTexture(texture) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
TextureViewBase* TextureViewBuilder::GetResultImpl() {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice->CreateTextureView(this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
} // namespace backend
|