Skip to content

Commit

Permalink
Texture support R32G32B32A32_UInt format (#1993)
Browse files Browse the repository at this point in the history
* feat: Texture support `R32G32B32A32_UInt` format
  • Loading branch information
GuoLei1990 authored Feb 2, 2024
1 parent bf5d41d commit ecf45f7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export class Engine extends EventDispatcher {
/* @internal */
_magentaTexture2D: Texture2D;
/* @internal */
_uintMagentaTexture2D: Texture2D;
/* @internal */
_magentaTextureCube: TextureCube;
/* @internal */
_magentaTexture2DArray: Texture2DArray;
Expand Down Expand Up @@ -601,6 +603,21 @@ export class Engine extends EventDispatcher {
this._magentaTextureCube = magentaTextureCube;

if (hardwareRenderer.isWebGL2) {
const magentaPixel32 = new Uint32Array([255, 0, 255, 255]);
const uintMagentaTexture2D = new Texture2D(this, 1, 1, TextureFormat.R32G32B32A32_UInt, false);
uintMagentaTexture2D.setPixelBuffer(magentaPixel32);
uintMagentaTexture2D.isGCIgnored = true;
this.resourceManager.addContentRestorer(
new (class extends ContentRestorer<Texture2D> {
constructor() {
super(uintMagentaTexture2D);
}
restoreContent() {
this.resource.setPixelBuffer(magentaPixel32);
}
})()
);

const magentaTexture2DArray = new Texture2DArray(this, 1, 1, 1, TextureFormat.R8G8B8A8, false);
magentaTexture2DArray.setPixelBuffer(0, magentaPixel);
magentaTexture2DArray.isGCIgnored = true;
Expand All @@ -614,6 +631,8 @@ export class Engine extends EventDispatcher {
}
})()
);

this._uintMagentaTexture2D = uintMagentaTexture2D;
this._magentaTexture2DArray = magentaTexture2DArray;
}
}
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/shader/ShaderProgram.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { IHardwareRenderer } from "@galacean/engine-design";
import { Vector2, Vector3, Vector4 } from "@galacean/engine-math";
import { Logger } from "../base/Logger";
import { Camera } from "../Camera";
import { Engine } from "../Engine";
import { Material } from "../material/Material";
import { Renderer } from "../Renderer";
import { Scene } from "../Scene";
import { Logger } from "../base/Logger";
import { Material } from "../material/Material";
import { Texture } from "../texture";
import { ShaderDataGroup } from "./enums/ShaderDataGroup";
import { ShaderData } from "./ShaderData";
import { ShaderProperty } from "./ShaderProperty";
import { ShaderUniform } from "./ShaderUniform";
import { ShaderUniformBlock } from "./ShaderUniformBlock";
import { ShaderDataGroup } from "./enums/ShaderDataGroup";

/**
* Shader program, corresponding to the GPU shader program.
Expand Down Expand Up @@ -400,6 +400,7 @@ export class ShaderProgram {
break;
case gl.SAMPLER_2D:
case gl.SAMPLER_CUBE:
case (<WebGL2RenderingContext>gl).UNSIGNED_INT_SAMPLER_2D:
case (<WebGL2RenderingContext>gl).SAMPLER_2D_ARRAY:
case (<WebGL2RenderingContext>gl).SAMPLER_2D_SHADOW:
let defaultTexture: Texture;
Expand All @@ -410,6 +411,9 @@ export class ShaderProgram {
case gl.SAMPLER_CUBE:
defaultTexture = this._engine._magentaTextureCube;
break;
case (<WebGL2RenderingContext>gl).UNSIGNED_INT_SAMPLER_2D:
defaultTexture = this._engine._uintMagentaTexture2D;
break;
case (<WebGL2RenderingContext>gl).SAMPLER_2D_ARRAY:
defaultTexture = this._engine._magentaTexture2DArray;
break;
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/texture/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export abstract class Texture extends GraphicsResource {

set filterMode(value: TextureFilterMode) {
if (value === this._filterMode) return;

if (value !== TextureFilterMode.Point && this._isIntFormat()) {
value = TextureFilterMode.Point;
Logger.warn(`Int or UInt format texture only support TextureFilterMode.Point`);
return;
}

this._filterMode = value;

this._platformTexture.filterMode = value;
Expand Down Expand Up @@ -211,4 +218,11 @@ export abstract class Texture extends GraphicsResource {
protected _getMipmapCount(): number {
return this._mipmap ? Math.floor(Math.log2(Math.max(this._width, this._height))) + 1 : 1;
}

protected _isIntFormat(): boolean {
if (TextureFormat.R32G32B32A32_UInt === this._format) {
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion packages/core/src/texture/Texture2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Texture2D extends Texture {

this._platformTexture = engine._hardwareRenderer.createPlatformTexture2D(this);

this.filterMode = TextureFilterMode.Bilinear;
this.filterMode = this._isIntFormat() ? TextureFilterMode.Point : TextureFilterMode.Bilinear;
this.wrapModeU = this.wrapModeV = TextureWrapMode.Repeat;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/texture/enums/TextureFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export enum TextureFormat {
R16G16B16A16,
/** RGBA format, 32 bits per channel. */
R32G32B32A32,
/** RGBA unsigned integer format, 32 bits per channel. */
R32G32B32A32_UInt,

/** RGB compressed format, 4 bits per pixel. */
BC1,
Expand Down
8 changes: 8 additions & 0 deletions packages/rhi-webgl/src/GLTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export class GLTexture implements IPlatformTexture {
dataType: gl.FLOAT,
isCompressed: false
};
case TextureFormat.R32G32B32A32_UInt:
return {
internalFormat: isWebGL2 ? gl.RGBA32UI : gl.NONE,
baseFormat: gl.RGBA_INTEGER,
dataType: gl.UNSIGNED_INT,
isCompressed: false
};
case TextureFormat.BC1:
return {
internalFormat: GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
Expand Down Expand Up @@ -347,6 +354,7 @@ export class GLTexture implements IPlatformTexture {
return false;
}
break;
case TextureFormat.R32G32B32A32_UInt:
case TextureFormat.Depth24:
case TextureFormat.Depth32:
case TextureFormat.Depth32Stencil8:
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/resource/ResourceManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("ResourceManager", () => {
describe("findResourcesByType", () => {
it("findResourcesByType", () => {
const textures = engine.resourceManager.findResourcesByType(Texture2D);
expect(textures.length).equal(4);
expect(textures.length).equal(5);
});
});
});

0 comments on commit ecf45f7

Please sign in to comment.