mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
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
This commit is contained in:
@@ -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
|
||||
|
||||
<!-- DEPRECATED -->
|
||||
|
||||
@@ -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<TextureInfo, 'usage' | 'flags' | 'format' | 'levelCount'>;
|
||||
export type PresumedGFXTextureViewInfo = Pick<TextureViewInfo, 'texture' | 'format' | 'baseLevel' | 'levelCount'>;
|
||||
|
||||
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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<int>(info.format)].isCompressed;
|
||||
CCASSERT(!isCompressed, "Generating mipmaps for compressed image?");
|
||||
}
|
||||
|
||||
/////////// execute ///////////
|
||||
|
||||
Reference in New Issue
Block a user