Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
star-e committed Oct 16, 2024
1 parent 1c2dd60 commit 945ff34
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cocos/gfx/base/states/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ 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);
Expand Down
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);
})

0 comments on commit 945ff34

Please sign in to comment.