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.
|
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Texture.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-10 17:46:05 +00:00
|
|
|
#include "common/Assert.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Device.h"
|
2018-08-27 00:44:48 +00:00
|
|
|
#include "dawn_native/ValidationUtils_autogen.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2018-08-27 00:44:48 +00:00
|
|
|
MaybeError ValidateTextureDescriptor(DeviceBase*, const TextureDescriptor* descriptor) {
|
2018-09-10 14:17:24 +00:00
|
|
|
if (descriptor->nextInChain != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:44:48 +00:00
|
|
|
DAWN_TRY(ValidateTextureUsageBit(descriptor->usage));
|
|
|
|
DAWN_TRY(ValidateTextureDimension(descriptor->dimension));
|
|
|
|
DAWN_TRY(ValidateTextureFormat(descriptor->format));
|
|
|
|
|
|
|
|
// TODO(jiawei.shao@intel.com): check stuff based on the dimension
|
|
|
|
if (descriptor->width == 0 || descriptor->height == 0 || descriptor->depth == 0 ||
|
|
|
|
descriptor->arrayLayer == 0 || descriptor->mipLevel == 0) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Cannot create an empty texture");
|
2018-08-27 00:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
uint32_t TextureFormatPixelSize(dawn::TextureFormat format) {
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (format) {
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::R8Unorm:
|
|
|
|
case dawn::TextureFormat::R8Uint:
|
2018-01-22 19:09:08 +00:00
|
|
|
return 1;
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::R8G8Unorm:
|
|
|
|
case dawn::TextureFormat::R8G8Uint:
|
2018-01-22 19:09:08 +00:00
|
|
|
return 2;
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::R8G8B8A8Unorm:
|
|
|
|
case dawn::TextureFormat::R8G8B8A8Uint:
|
|
|
|
case dawn::TextureFormat::B8G8R8A8Unorm:
|
2017-04-20 18:38:20 +00:00
|
|
|
return 4;
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::D32FloatS8Uint:
|
2017-07-17 16:03:16 +00:00
|
|
|
return 8;
|
2017-07-11 00:31:47 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
bool TextureFormatHasDepth(dawn::TextureFormat format) {
|
2017-07-17 16:03:16 +00:00
|
|
|
switch (format) {
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::D32FloatS8Uint:
|
2017-07-17 16:03:16 +00:00
|
|
|
return true;
|
|
|
|
default:
|
2018-01-22 19:09:08 +00:00
|
|
|
return false;
|
2017-07-17 16:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
bool TextureFormatHasStencil(dawn::TextureFormat format) {
|
2017-07-17 16:03:16 +00:00
|
|
|
switch (format) {
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::D32FloatS8Uint:
|
2017-07-17 16:03:16 +00:00
|
|
|
return true;
|
|
|
|
default:
|
2018-01-22 19:09:08 +00:00
|
|
|
return false;
|
2017-07-17 16:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
bool TextureFormatHasDepthOrStencil(dawn::TextureFormat format) {
|
2017-08-11 21:36:20 +00:00
|
|
|
switch (format) {
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::TextureFormat::D32FloatS8Uint:
|
2017-08-11 21:36:20 +00:00
|
|
|
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
|
|
|
|
|
2018-08-27 00:44:48 +00:00
|
|
|
TextureBase::TextureBase(DeviceBase* device, const TextureDescriptor* descriptor)
|
|
|
|
: mDevice(device),
|
|
|
|
mDimension(descriptor->dimension),
|
|
|
|
mFormat(descriptor->format),
|
|
|
|
mWidth(descriptor->width),
|
|
|
|
mHeight(descriptor->height),
|
|
|
|
mDepth(descriptor->depth),
|
|
|
|
mArrayLayers(descriptor->arrayLayer),
|
|
|
|
mNumMipLevels(descriptor->mipLevel),
|
|
|
|
mUsage(descriptor->usage) {
|
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
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
dawn::TextureDimension TextureBase::GetDimension() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDimension;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2018-07-18 09:38:11 +00:00
|
|
|
dawn::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
|
|
|
}
|
2018-08-27 00:44:48 +00:00
|
|
|
uint32_t TextureBase::GetArrayLayers() const {
|
|
|
|
return mArrayLayers;
|
|
|
|
}
|
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
|
|
|
}
|
2018-08-22 13:08:02 +00:00
|
|
|
dawn::TextureUsageBit TextureBase::GetUsage() const {
|
|
|
|
return mUsage;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|