Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sampler info packing #17726

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cocos/asset/assets/texture-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ export class TextureBase extends Asset {
/**
* @en Sets the texture's anisotropy.
* @zh 设置此贴图的各向异性。
* @param anisotropy @en The anisotropy to be set. @zh 待设置的各向异性数值。
* @param anisotropy @en The anisotropy to be set. Max value is 16. @zh 待设置的各向异性数值。最大值为16
*/
public setAnisotropy (anisotropy: number): void {
anisotropy = Math.min(anisotropy, 16);
this._anisotropy = anisotropy;
this._samplerInfo$.maxAnisotropy = anisotropy;

Expand Down
24 changes: 12 additions & 12 deletions cocos/gfx/base/states/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ export class Sampler extends GFXObject {
this._hash$ = hash;
}

static computeHash (info: Readonly<SamplerInfo>): Filter {
let hash = info.minFilter;
static computeHash (info: Readonly<SamplerInfo>): number {
let hash = (info.minFilter as number);
hash |= ((info.magFilter as number) << 2);
hash |= ((info.mipFilter as number) << 4);
hash |= ((info.addressU as number) << 6);
hash |= ((info.addressV as number) << 8);
hash |= ((info.addressW as number) << 10);
hash |= (info.maxAnisotropy << 12);
hash |= ((info.cmpFunc as number) << 16);
hash |= (Math.min(info.maxAnisotropy, 16) << 12);
hash |= ((info.cmpFunc as number) << 17);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increase maxAnisotropy from 4 bits to 5 bits (0-15 to 0-16)

return hash;
}

static unpackFromHash (hash: number): SamplerInfo {
const info = new SamplerInfo();
info.minFilter = (hash & ((1 << 2) - 1)) >> 0;
info.magFilter = (hash & ((1 << 2) - 1)) >> 2;
info.mipFilter = (hash & ((1 << 2) - 1)) >> 4;
info.addressU = (hash & ((1 << 2) - 1)) >> 6;
info.addressV = (hash & ((1 << 2) - 1)) >> 8;
info.addressW = (hash & ((1 << 2) - 1)) >> 10;
info.maxAnisotropy = (hash & ((1 << 4) - 1)) >> 12;
info.cmpFunc = (hash & ((1 << 3) - 1)) >> 16;
info.minFilter = hash & 3;
info.magFilter = (hash >> 2) & 3;
info.mipFilter = (hash >> 4) & 3;
info.addressU = (hash >> 6) & 3;
info.addressV = (hash >> 8) & 3;
info.addressW = (hash >> 10) & 3;
info.maxAnisotropy = (hash >> 12) & 31;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increase maxAnisotropy from 4 bits to 5 bits (0-15 to 0-16)

info.cmpFunc = (hash >> 17) & 7;
return info;
}
}
1 change: 1 addition & 0 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,7 @@ cocos_source_files(
cocos/renderer/gfx-base/GFXQueryPool.h
cocos/renderer/gfx-base/GFXRenderPass.cpp
cocos/renderer/gfx-base/GFXRenderPass.h
cocos/renderer/gfx-base/GFXSamplerUtils.h
cocos/renderer/gfx-base/GFXShader.cpp
cocos/renderer/gfx-base/GFXShader.h
cocos/renderer/gfx-base/GFXSwapchain.cpp
Expand Down
14 changes: 2 additions & 12 deletions native/cocos/renderer/gfx-base/GFXDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "GFXDef.h"
#include "GFXRenderPass.h"
#include "GFXSamplerUtils.h"
#include "GFXTexture.h"

namespace cc {
Expand Down Expand Up @@ -203,18 +204,7 @@ bool operator==(const BufferInfo &lhs, const BufferInfo &rhs) {

template <>
ccstd::hash_t Hasher<SamplerInfo>::operator()(const SamplerInfo &info) const {
// return quickHashTrivialStruct(&info);

// the hash may be used to reconstruct the original struct
auto hash = static_cast<uint32_t>(info.minFilter);
hash |= static_cast<uint32_t>(info.magFilter) << 2;
hash |= static_cast<uint32_t>(info.mipFilter) << 4;
hash |= static_cast<uint32_t>(info.addressU) << 6;
hash |= static_cast<uint32_t>(info.addressV) << 8;
hash |= static_cast<uint32_t>(info.addressW) << 10;
hash |= static_cast<uint32_t>(info.maxAnisotropy) << 12;
hash |= static_cast<uint32_t>(info.cmpFunc) << 16;
return static_cast<ccstd::hash_t>(hash);
return static_cast<ccstd::hash_t>(packSamplerInfo(info));
}

bool operator==(const SamplerInfo &lhs, const SamplerInfo &rhs) {
Expand Down
66 changes: 66 additions & 0 deletions native/cocos/renderer/gfx-base/GFXSamplerUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/****************************************************************************
Copyright (c) 2019-2024 Xiamen Yaji Software Co., Ltd.

http://www.cocos.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#pragma once
#include "cocos/renderer/gfx-base/GFXDef-common.h"

namespace cc {
namespace gfx {

constexpr uint32_t packSamplerInfo(const SamplerInfo &info) noexcept {
CC_ASSERT(static_cast<uint32_t>(info.minFilter) < (1 << 2));
CC_ASSERT(static_cast<uint32_t>(info.magFilter) < (1 << 2));
CC_ASSERT(static_cast<uint32_t>(info.mipFilter) < (1 << 2));
CC_ASSERT(static_cast<uint32_t>(info.addressU) < (1 << 2));
CC_ASSERT(static_cast<uint32_t>(info.addressV) < (1 << 2));
CC_ASSERT(static_cast<uint32_t>(info.addressW) < (1 << 2));
CC_ASSERT(info.maxAnisotropy <= 16);
CC_ASSERT(static_cast<uint32_t>(info.cmpFunc) < (1 << 3));

auto packed = static_cast<uint32_t>(info.minFilter);
packed |= static_cast<uint32_t>(info.magFilter) << 2;
packed |= static_cast<uint32_t>(info.mipFilter) << 4;
packed |= static_cast<uint32_t>(info.addressU) << 6;
packed |= static_cast<uint32_t>(info.addressV) << 8;
packed |= static_cast<uint32_t>(info.addressW) << 10;
packed |= static_cast<uint32_t>(info.maxAnisotropy) << 12;
packed |= static_cast<uint32_t>(info.cmpFunc) << 17;
return packed;
}

constexpr SamplerInfo unpackSamplerInfo(const uint32_t packed) noexcept {
SamplerInfo info{};
info.minFilter = static_cast<Filter>(packed & ((1 << 2) - 1));
info.magFilter = static_cast<Filter>((packed >> 2) & ((1 << 2) - 1));
info.mipFilter = static_cast<Filter>((packed >> 4) & ((1 << 2) - 1));
info.addressU = static_cast<Address>((packed >> 6) & ((1 << 2) - 1));
info.addressV = static_cast<Address>((packed >> 8) & ((1 << 2) - 1));
info.addressW = static_cast<Address>((packed >> 10) & ((1 << 2) - 1));
info.maxAnisotropy = (packed >> 12) & ((1 << 5) - 1);
info.cmpFunc = static_cast<ComparisonFunc>((packed >> 17) & ((1 << 3) - 1));
return info;
}

} // namespace gfx
} // namespace cc
63 changes: 53 additions & 10 deletions native/cocos/renderer/gfx-base/states/GFXSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
****************************************************************************/

#include "GFXSampler.h"
#include "../GFXSamplerUtils.h"

namespace cc {
namespace gfx {
Expand All @@ -38,17 +39,59 @@ ccstd::hash_t Sampler::computeHash(const SamplerInfo &info) {
}

SamplerInfo Sampler::unpackFromHash(ccstd::hash_t hash) {
SamplerInfo info;
info.minFilter = static_cast<Filter>((hash & ((1 << 2) - 1)) >> 0);
info.magFilter = static_cast<Filter>((hash & ((1 << 2) - 1)) >> 2);
info.mipFilter = static_cast<Filter>((hash & ((1 << 2) - 1)) >> 4);
info.addressU = static_cast<Address>((hash & ((1 << 2) - 1)) >> 6);
info.addressV = static_cast<Address>((hash & ((1 << 2) - 1)) >> 8);
info.addressW = static_cast<Address>((hash & ((1 << 2) - 1)) >> 10);
info.maxAnisotropy = (hash & ((1 << 4) - 1)) >> 12;
info.cmpFunc = static_cast<ComparisonFunc>((hash & ((1 << 3) - 1)) >> 16);
return info;
return unpackSamplerInfo(hash);
}

namespace {

constexpr bool testSamplerInfo(const SamplerInfo &info) {
const uint32_t packed = packSamplerInfo(info);
const SamplerInfo unpacked = unpackSamplerInfo(packed);

return info.minFilter == unpacked.minFilter &&
info.magFilter == unpacked.magFilter &&
info.mipFilter == unpacked.mipFilter &&
info.addressU == unpacked.addressU &&
info.addressV == unpacked.addressV &&
info.addressW == unpacked.addressW &&
info.maxAnisotropy == unpacked.maxAnisotropy &&
info.cmpFunc == unpacked.cmpFunc;
}

static_assert(testSamplerInfo(SamplerInfo{
Filter::NONE,
Filter::NONE,
Filter::NONE,
Address::WRAP,
Address::WRAP,
Address::WRAP,
0U,
ComparisonFunc::NEVER,
}));

static_assert(testSamplerInfo(SamplerInfo{
Filter::ANISOTROPIC,
Filter::ANISOTROPIC,
Filter::ANISOTROPIC,
Address::BORDER,
Address::BORDER,
Address::BORDER,
16U,
ComparisonFunc::ALWAYS,
}));

static_assert(testSamplerInfo(SamplerInfo{
Filter::POINT,
Filter::LINEAR,
Filter::ANISOTROPIC,
Address::MIRROR,
Address::CLAMP,
Address::BORDER,
15U,
ComparisonFunc::GREATER_EQUAL,
}));

} // namespace

} // namespace gfx
} // namespace cc
46 changes: 46 additions & 0 deletions tests/core/gfx/sampler-info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Sampler, SamplerInfo, Filter, Address, ComparisonFunc } from "../../../cocos/gfx"

function testSamplerInfo(info: SamplerInfo): boolean {
const packed = Sampler.computeHash(info);
const unpacked = Sampler.unpackFromHash(packed);
return unpacked.minFilter === info.minFilter &&
unpacked.magFilter === info.magFilter &&
unpacked.mipFilter === info.mipFilter &&
unpacked.addressU === info.addressU &&
unpacked.addressV === info.addressV &&
unpacked.addressW === info.addressW &&
unpacked.maxAnisotropy === info.maxAnisotropy &&
unpacked.cmpFunc === info.cmpFunc;
}

test('packSamplerInfo', () => {
expect(testSamplerInfo(new SamplerInfo(
Filter.NONE,
Filter.NONE,
Filter.NONE,
Address.WRAP,
Address.WRAP,
Address.WRAP,
0,
ComparisonFunc.NEVER))).toBe(true);

expect(testSamplerInfo(new SamplerInfo(
Filter.ANISOTROPIC,
Filter.ANISOTROPIC,
Filter.ANISOTROPIC,
Address.BORDER,
Address.BORDER,
Address.BORDER,
16,
ComparisonFunc.ALWAYS))).toBe(true);

expect(testSamplerInfo(new SamplerInfo(
Filter.ANISOTROPIC,
Filter.LINEAR,
Filter.POINT,
Address.BORDER,
Address.CLAMP,
Address.MIRROR,
15,
ComparisonFunc.GREATER_EQUAL))).toBe(true);
})
Loading