1 // Copyright 2017 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/Sampler.h" 16 17 #include "common/HashUtils.h" 18 #include "dawn_native/Device.h" 19 #include "dawn_native/ValidationUtils_autogen.h" 20 21 #include <cmath> 22 23 namespace dawn_native { 24 ValidateSamplerDescriptor(DeviceBase *,const SamplerDescriptor * descriptor)25 MaybeError ValidateSamplerDescriptor(DeviceBase*, const SamplerDescriptor* descriptor) { 26 if (descriptor->nextInChain != nullptr) { 27 return DAWN_VALIDATION_ERROR("nextInChain must be nullptr"); 28 } 29 30 if (!std::isfinite(descriptor->lodMinClamp) || !std::isfinite(descriptor->lodMaxClamp)) { 31 return DAWN_VALIDATION_ERROR("LOD must be finite"); 32 } 33 34 if (descriptor->lodMinClamp < 0 || descriptor->lodMaxClamp < 0) { 35 return DAWN_VALIDATION_ERROR("LOD must be positive"); 36 } 37 38 if (descriptor->lodMinClamp > descriptor->lodMaxClamp) { 39 return DAWN_VALIDATION_ERROR( 40 "Min lod clamp value cannot greater than max lod clamp value"); 41 } 42 43 DAWN_TRY(ValidateFilterMode(descriptor->minFilter)); 44 DAWN_TRY(ValidateFilterMode(descriptor->magFilter)); 45 DAWN_TRY(ValidateFilterMode(descriptor->mipmapFilter)); 46 DAWN_TRY(ValidateAddressMode(descriptor->addressModeU)); 47 DAWN_TRY(ValidateAddressMode(descriptor->addressModeV)); 48 DAWN_TRY(ValidateAddressMode(descriptor->addressModeW)); 49 DAWN_TRY(ValidateCompareFunction(descriptor->compare)); 50 return {}; 51 } 52 53 // SamplerBase 54 SamplerBase(DeviceBase * device,const SamplerDescriptor * descriptor,bool blueprint)55 SamplerBase::SamplerBase(DeviceBase* device, 56 const SamplerDescriptor* descriptor, 57 bool blueprint) 58 : ObjectBase(device), 59 mAddressModeU(descriptor->addressModeU), 60 mAddressModeV(descriptor->addressModeV), 61 mAddressModeW(descriptor->addressModeW), 62 mMagFilter(descriptor->magFilter), 63 mMinFilter(descriptor->minFilter), 64 mMipmapFilter(descriptor->mipmapFilter), 65 mLodMinClamp(descriptor->lodMinClamp), 66 mLodMaxClamp(descriptor->lodMaxClamp), 67 mCompareFunction(descriptor->compare), 68 mIsBlueprint(blueprint) { 69 } 70 SamplerBase(DeviceBase * device,ObjectBase::ErrorTag tag)71 SamplerBase::SamplerBase(DeviceBase* device, ObjectBase::ErrorTag tag) 72 : ObjectBase(device, tag) { 73 } 74 ~SamplerBase()75 SamplerBase::~SamplerBase() { 76 // Do not uncache the actual cached object if we are a blueprint 77 if (!mIsBlueprint && !IsError()) { 78 GetDevice()->UncacheSampler(this); 79 } 80 } 81 82 // static MakeError(DeviceBase * device)83 SamplerBase* SamplerBase::MakeError(DeviceBase* device) { 84 return new SamplerBase(device, ObjectBase::kError); 85 } 86 operator ()(const SamplerBase * module) const87 size_t SamplerBase::HashFunc::operator()(const SamplerBase* module) const { 88 size_t hash = 0; 89 90 HashCombine(&hash, module->mAddressModeU); 91 HashCombine(&hash, module->mAddressModeV); 92 HashCombine(&hash, module->mAddressModeW); 93 HashCombine(&hash, module->mMagFilter); 94 HashCombine(&hash, module->mMinFilter); 95 HashCombine(&hash, module->mMipmapFilter); 96 HashCombine(&hash, module->mLodMinClamp); 97 HashCombine(&hash, module->mLodMaxClamp); 98 HashCombine(&hash, module->mCompareFunction); 99 100 return hash; 101 } 102 operator ()(const SamplerBase * a,const SamplerBase * b) const103 bool SamplerBase::EqualityFunc::operator()(const SamplerBase* a, const SamplerBase* b) const { 104 if (a == b) { 105 return true; 106 } 107 108 ASSERT(std::isfinite(a->mLodMinClamp)); 109 ASSERT(std::isfinite(b->mLodMinClamp)); 110 ASSERT(std::isfinite(a->mLodMaxClamp)); 111 ASSERT(std::isfinite(b->mLodMaxClamp)); 112 113 return a->mAddressModeU == b->mAddressModeU && a->mAddressModeV == b->mAddressModeV && 114 a->mAddressModeW == b->mAddressModeW && a->mMagFilter == b->mMagFilter && 115 a->mMinFilter == b->mMinFilter && a->mMipmapFilter == b->mMipmapFilter && 116 a->mLodMinClamp == b->mLodMinClamp && a->mLodMaxClamp == b->mLodMaxClamp && 117 a->mCompareFunction == b->mCompareFunction; 118 } 119 120 } // namespace dawn_native 121