From 718ea8ee3fa4fa2461e3a1715ce2b8cea6af2d09 Mon Sep 17 00:00:00 2001 From: Yiwen <15225434259xue@gmail.com> Date: Sun, 27 Mar 2022 13:52:24 +0800 Subject: [PATCH] Add a method to select mipmap range for `Texture2D` and `TextureCube`. (#10500) * Add a feature to select mipmap range to image asset. * fix format * fix format * modify comments, remove uncessary methods * finx comments. * fix logic problem * fix mipmap range problems * Add validation information incase someone wants to generate mipmap for compressed texture * remove some validations to make gfx bakend clear * fix a stupid typo * Add validation information for setMipRange * use assertID to validate the mipmap range --- EngineErrorMap.md | 4 + cocos/core/assets/simple-texture.ts | 74 ++++++++++++++++++- cocos/core/assets/texture-2d.ts | 44 +++++++++-- cocos/core/assets/texture-cube.ts | 24 +++++- cocos/core/gfx/webgl/webgl-texture.ts | 4 +- cocos/core/gfx/webgl2/webgl2-texture.ts | 6 +- .../gfx-validator/TextureValidator.cpp | 3 + 7 files changed, 143 insertions(+), 16 deletions(-) diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 90f9f3efd4..2748e2c28b 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -1201,6 +1201,10 @@ Error Texture in %s. Set same texture %s. +### 3124 + +Texture: setMipRange failed because base level is larger than max level + ### 3200 diff --git a/cocos/core/assets/simple-texture.ts b/cocos/core/assets/simple-texture.ts index a8902fb3c9..bc6d09238c 100644 --- a/cocos/core/assets/simple-texture.ts +++ b/cocos/core/assets/simple-texture.ts @@ -30,8 +30,8 @@ import { ccclass } from 'cc.decorator'; import { DEV } from 'internal:constants'; -import { TextureFlagBit, TextureUsageBit, API, Texture, TextureInfo, Device, BufferTextureCopy } from '../gfx'; -import { error } from '../platform/debug'; +import { TextureFlagBit, TextureUsageBit, API, Texture, TextureInfo, TextureViewInfo, Device, BufferTextureCopy } from '../gfx'; +import { assertID, error } from '../platform/debug'; import { Filter } from './asset-enum'; import { ImageAsset } from './image-asset'; import { TextureBase } from './texture-base'; @@ -43,6 +43,7 @@ import { fastRemoveAt } from '../utils/array'; const _regions: BufferTextureCopy[] = [new BufferTextureCopy()]; export type PresumedGFXTextureInfo = Pick; +export type PresumedGFXTextureViewInfo = Pick; function getMipLevel (width: number, height: number) { let size = Math.max(width, height); @@ -68,11 +69,15 @@ function canGenerateMipmap (device: Device, w: number, h: number) { @ccclass('cc.SimpleTexture') export class SimpleTexture extends TextureBase { protected _gfxTexture: Texture | null = null; + protected _gfxTextureView: Texture | null = null; private _mipmapLevel = 1; // Cache these data to reduce JSB invoking. private _textureWidth = 0; private _textureHeight = 0; + protected _baseLevel = 0; + protected _maxLevel = 0; + /** * @en The mipmap level of the texture * @zh 贴图中的 Mipmap 层级数量 @@ -86,10 +91,11 @@ export class SimpleTexture extends TextureBase { * @zh 获取此贴图底层的 GFX 贴图对象。 */ public getGFXTexture () { - return this._gfxTexture; + return this._gfxTextureView; } public destroy () { + this._tryDestroyTextureView(); this._tryDestroyTexture(); return super.destroy(); } @@ -198,6 +204,32 @@ export class SimpleTexture extends TextureBase { this._mipmapLevel = value < 1 ? 1 : value; } + protected _setMipRange (baseLevel: number, maxLevel: number) { + this._baseLevel = baseLevel < 0 ? 0 : baseLevel; + this._baseLevel = this._baseLevel < this._mipmapLevel ? this._baseLevel : this._mipmapLevel - 1; + + this._maxLevel = maxLevel < this._baseLevel ? this._baseLevel : maxLevel; + this._maxLevel = this._maxLevel < this._mipmapLevel ? this._maxLevel : this._mipmapLevel - 1; + } + + /** + * Set mipmap level range for this texture. + * @param baseLevel The base mipmap level. + * @param maxLevel The maximum mipmap level. + */ + public setMipRange (baseLevel: number, maxLevel: number) { + assertID(baseLevel <= maxLevel, 3124); + + this._setMipRange(baseLevel, maxLevel); + + const device = this._getGFXDevice(); + if (!device) { + return; + } + this._tryDestroyTextureView(); + this._createTextureView(device); + } + /** * @en This method is overrided by derived classes to provide GFX texture info. * @zh 这个方法被派生类重写以提供 GFX 纹理信息。 @@ -207,7 +239,17 @@ export class SimpleTexture extends TextureBase { return null; } + /** + * @en This method is overrided by derived classes to provide GFX TextureViewInfo. + * @zh 这个方法被派生类重写以提供 GFX 纹理视图信息。 + * @param presumed The presumed GFX TextureViewInfo. + */ + protected _getGfxTextureViewCreateInfo (presumed: PresumedGFXTextureViewInfo): TextureViewInfo | null { + return null; + } + protected _tryReset () { + this._tryDestroyTextureView(); this._tryDestroyTexture(); if (this._mipmapLevel === 0) { return; @@ -217,6 +259,7 @@ export class SimpleTexture extends TextureBase { return; } this._createTexture(device); + this._createTextureView(device); } protected _createTexture (device: Device) { @@ -244,12 +287,37 @@ export class SimpleTexture extends TextureBase { this._gfxTexture = texture; } + protected _createTextureView (device: Device) { + if (!this._gfxTexture) { + return; + } + const textureViewCreateInfo = this._getGfxTextureViewCreateInfo({ + texture: this._gfxTexture, + format: this._getGFXFormat(), + baseLevel: this._baseLevel, + levelCount: this._maxLevel - this._baseLevel + 1, + }); + if (!textureViewCreateInfo) { + return; + } + + const textureView = device.createTexture(textureViewCreateInfo); + this._gfxTextureView = textureView; + } + protected _tryDestroyTexture () { if (this._gfxTexture) { this._gfxTexture.destroy(); this._gfxTexture = null; } } + + protected _tryDestroyTextureView () { + if (this._gfxTextureView) { + this._gfxTextureView.destroy(); + this._gfxTextureView = null; + } + } } legacyCC.SimpleTexture = SimpleTexture; diff --git a/cocos/core/assets/texture-2d.ts b/cocos/core/assets/texture-2d.ts index 485b6cdc41..70301eaf2e 100644 --- a/cocos/core/assets/texture-2d.ts +++ b/cocos/core/assets/texture-2d.ts @@ -31,10 +31,10 @@ import { EDITOR, TEST } from 'internal:constants'; import { ccclass, type } from 'cc.decorator'; -import { TextureType, TextureInfo } from '../gfx'; +import { TextureType, TextureInfo, TextureViewInfo } from '../gfx'; import { PixelFormat } from './asset-enum'; import { ImageAsset } from './image-asset'; -import { PresumedGFXTextureInfo, SimpleTexture } from './simple-texture'; +import { PresumedGFXTextureInfo, PresumedGFXTextureViewInfo, SimpleTexture } from './simple-texture'; import { legacyCC } from '../global-exports'; import { js } from '../utils/js'; @@ -68,6 +68,20 @@ export interface ITexture2DCreateInfo { * @default 1 */ mipmapLevel?: number; + + /** + * @en The selected base mipmap level + * @zh 选择使用的最小 mipmap 层级。 + * @default 1 + */ + baseLevel?: number; + + /** + * @en The selected maximum mipmap level + * @zh 选择使用的最大 mipmap 层级。 + * @default 1 + */ + maxLevel?: number; } /** @@ -95,6 +109,8 @@ export class Texture2D extends SimpleTexture { height: imageAsset.height, format: imageAsset.format, mipmapLevel: this._mipmaps.length, + baseLevel: this._baseLevel, + maxLevel: this._maxLevel, }); this._mipmaps.forEach((mipmap, level) => { this._assignImage(mipmap, level); @@ -104,6 +120,8 @@ export class Texture2D extends SimpleTexture { width: 0, height: 0, mipmapLevel: this._mipmaps.length, + baseLevel: this._baseLevel, + maxLevel: this._maxLevel, }); } } @@ -149,7 +167,11 @@ export class Texture2D extends SimpleTexture { this._width = info.width; this._height = info.height; this._setGFXFormat(info.format); - this._setMipmapLevel(info.mipmapLevel || 1); + const mipLevels = info.mipmapLevel === undefined ? 1 : info.mipmapLevel; + this._setMipmapLevel(mipLevels); + const minLod = info.baseLevel || 0; + const maxLod = info.maxLevel === undefined ? (mipLevels - 1) : info.maxLevel; + this._setMipRange(minLod, maxLod); this._tryReset(); } @@ -162,14 +184,18 @@ export class Texture2D extends SimpleTexture { * @param height Pixel height * @param format Pixel format * @param mipmapLevel Mipmap level count + * @param baseLevel Mipmap base level + * @param maxLevel Mipmap maximum level * @deprecated since v1.0 please use [[reset]] instead */ - public create (width: number, height: number, format = PixelFormat.RGBA8888, mipmapLevel = 1) { + public create (width: number, height: number, format = PixelFormat.RGBA8888, mipmapLevel = 1, baseLevel = 0, maxLevel = 0) { this.reset({ width, height, format, mipmapLevel, + baseLevel, + maxLevel, }); } @@ -276,7 +302,15 @@ export class Texture2D extends SimpleTexture { const texInfo = new TextureInfo(TextureType.TEX2D); texInfo.width = this._width; texInfo.height = this._height; - return Object.assign(texInfo, presumed); + Object.assign(texInfo, presumed); + return texInfo; + } + + protected _getGfxTextureViewCreateInfo (presumed: PresumedGFXTextureViewInfo) { + const texViewInfo = new TextureViewInfo(); + texViewInfo.type = TextureType.TEX2D; + Object.assign(texViewInfo, presumed); + return texViewInfo; } public initDefault (uuid?: string) { diff --git a/cocos/core/assets/texture-cube.ts b/cocos/core/assets/texture-cube.ts index 7a07790d5c..1beb320bc4 100644 --- a/cocos/core/assets/texture-cube.ts +++ b/cocos/core/assets/texture-cube.ts @@ -30,9 +30,9 @@ import { EDITOR, TEST } from 'internal:constants'; import { ccclass, serializable } from 'cc.decorator'; -import { TextureType, TextureInfo } from '../gfx'; +import { TextureType, TextureInfo, TextureViewInfo } from '../gfx'; import { ImageAsset } from './image-asset'; -import { PresumedGFXTextureInfo, SimpleTexture } from './simple-texture'; +import { PresumedGFXTextureInfo, PresumedGFXTextureViewInfo, SimpleTexture } from './simple-texture'; import { ITexture2DCreateInfo, Texture2D } from './texture-2d'; import { legacyCC } from '../global-exports'; import { js } from '../utils/js'; @@ -98,6 +98,8 @@ export class TextureCube extends SimpleTexture { height: imageAsset.height, format: imageAsset.format, mipmapLevel: this._mipmaps.length, + baseLevel: this._baseLevel, + maxLevel: this._maxLevel, }); this._mipmaps.forEach((mipmap, level) => { _forEachFace(mipmap, (face, faceIndex) => { @@ -109,6 +111,8 @@ export class TextureCube extends SimpleTexture { width: 0, height: 0, mipmapLevel: this._mipmaps.length, + baseLevel: this._baseLevel, + maxLevel: this._maxLevel, }); } } @@ -148,6 +152,7 @@ export class TextureCube extends SimpleTexture { * const textureCube = TextureCube.fromTexture2DArray(textures); * ``` */ + public static fromTexture2DArray (textures: Texture2D[], out?: TextureCube) { const mipmaps: ITextureCubeMipmap[] = []; const nMipmaps = textures.length / 6; @@ -188,7 +193,11 @@ export class TextureCube extends SimpleTexture { this._width = info.width; this._height = info.height; this._setGFXFormat(info.format); - this._setMipmapLevel(info.mipmapLevel || 1); + const mipLevels = info.mipmapLevel === undefined ? 1 : info.mipmapLevel; + this._setMipmapLevel(mipLevels); + const minLod = info.baseLevel || 0; + const maxLod = info.maxLevel === undefined ? (mipLevels - 1) : info.maxLevel; + this._setMipRange(minLod, maxLod); this._tryReset(); } @@ -293,6 +302,15 @@ export class TextureCube extends SimpleTexture { return texInfo; } + protected _getGfxTextureViewCreateInfo (presumed: PresumedGFXTextureViewInfo) { + const texViewInfo = new TextureViewInfo(); + texViewInfo.type = TextureType.CUBE; + texViewInfo.baseLayer = 0; + texViewInfo.layerCount = 6; + Object.assign(texViewInfo, presumed); + return texViewInfo; + } + public initDefault (uuid?: string) { super.initDefault(uuid); diff --git a/cocos/core/gfx/webgl/webgl-texture.ts b/cocos/core/gfx/webgl/webgl-texture.ts index 6c20af3b10..db07e3e722 100644 --- a/cocos/core/gfx/webgl/webgl-texture.ts +++ b/cocos/core/gfx/webgl/webgl-texture.ts @@ -94,9 +94,9 @@ export class WebGLTexture extends Texture { this._viewInfo.type = info.type; this._viewInfo.format = info.format; this._viewInfo.baseLevel = 0; - this._viewInfo.levelCount = 1; + this._viewInfo.levelCount = info.levelCount; this._viewInfo.baseLayer = 0; - this._viewInfo.layerCount = 1; + this._viewInfo.layerCount = info.layerCount; } else { this._viewInfo.copy(viewInfo); this._lodLevel = viewInfo.baseLevel; diff --git a/cocos/core/gfx/webgl2/webgl2-texture.ts b/cocos/core/gfx/webgl2/webgl2-texture.ts index a041c3c197..839dc9f0dd 100644 --- a/cocos/core/gfx/webgl2/webgl2-texture.ts +++ b/cocos/core/gfx/webgl2/webgl2-texture.ts @@ -25,7 +25,7 @@ import { FormatSurfaceSize, TextureInfo, IsPowerOf2, TextureViewInfo, ISwapchainTextureInfo, - FormatInfos, TextureUsageBit, TextureFlagBit, + FormatInfos, TextureUsageBit, TextureFlagBit, FormatInfo, } from '../base/define'; import { Texture } from '../base/texture'; import { WebGL2CmdFuncCreateTexture, WebGL2CmdFuncDestroyTexture, WebGL2CmdFuncResizeTexture } from './webgl2-commands'; @@ -97,9 +97,9 @@ export class WebGL2Texture extends Texture { this._viewInfo.type = info.type; this._viewInfo.format = info.format; this._viewInfo.baseLevel = 0; - this._viewInfo.levelCount = 1; + this._viewInfo.levelCount = info.levelCount; this._viewInfo.baseLayer = 0; - this._viewInfo.layerCount = 1; + this._viewInfo.layerCount = info.layerCount; this._gpuTextureView = { gpuTexture: this._gpuTexture, diff --git a/native/cocos/renderer/gfx-validator/TextureValidator.cpp b/native/cocos/renderer/gfx-validator/TextureValidator.cpp index c714f4e2a5..822f643e18 100644 --- a/native/cocos/renderer/gfx-validator/TextureValidator.cpp +++ b/native/cocos/renderer/gfx-validator/TextureValidator.cpp @@ -71,6 +71,9 @@ void TextureValidator::doInit(const TextureInfo &info) { if (hasFlag(info.flags, TextureFlagBit::GEN_MIPMAP)) { CCASSERT(info.levelCount > 1, "Generating mipmaps with level count 1?"); + + bool isCompressed = GFX_FORMAT_INFOS[static_cast(info.format)].isCompressed; + CCASSERT(!isCompressed, "Generating mipmaps for compressed image?"); } /////////// execute ///////////