diff --git a/packages/dev/core/src/Engines/Native/Extensions/nativeEngine.cubeTexture.pure.ts b/packages/dev/core/src/Engines/Native/Extensions/nativeEngine.cubeTexture.pure.ts index bdb35b2e32e..9fa99a13fd1 100644 --- a/packages/dev/core/src/Engines/Native/Extensions/nativeEngine.cubeTexture.pure.ts +++ b/packages/dev/core/src/Engines/Native/Extensions/nativeEngine.cubeTexture.pure.ts @@ -4,6 +4,7 @@ import { InternalTextureSource, InternalTexture } from "../../../Materials/Textu import { Texture } from "../../../Materials/Textures/texture.pure"; import { CreateRadianceImageDataArrayBufferViews, GetEnvInfo, UploadEnvSpherical } from "../../../Misc/environmentTextureTools.pure"; import { type IWebRequest } from "../../../Misc/interfaces/iWebRequest"; +import { SphericalPolynomial } from "../../../Maths/sphericalPolynomial"; import { type Scene } from "../../../scene.pure"; import { type Nullable } from "../../../types"; import { Constants } from "../../constants"; @@ -118,34 +119,102 @@ export function RegisterNativeEngineCubeTexture(): void { ); } } else { - if (!files || files.length !== 6) { - throw new Error("Cannot load cubemap because 6 files were not defined"); - } - - // Reorder from [+X, +Y, +Z, -X, -Y, -Z] to [+X, -X, +Y, -Y, +Z, -Z]. - const reorderedFiles = [files[0], files[3], files[1], files[4], files[2], files[5]]; - // eslint-disable-next-line github/no-then - Promise.all(reorderedFiles.map(async (file) => await this._loadFileAsync(file, undefined, true).then((data) => new Uint8Array(data, 0, data.byteLength)))) + if (files && files.length === 6) { + // Reorder from [+X, +Y, +Z, -X, -Y, -Z] to [+X, -X, +Y, -Y, +Z, -Z]. + const reorderedFiles = [files[0], files[3], files[1], files[4], files[2], files[5]]; // eslint-disable-next-line github/no-then - .then(async (data) => { - return await new Promise((resolve, reject) => { - this._engine.loadCubeTexture(texture._hardwareTexture!.underlyingResource, data, !noMipmap, true, texture._useSRGBBuffer, resolve, reject); - }); - }) - // eslint-disable-next-line github/no-then - .then( - () => { - texture.isReady = true; - if (onLoad) { - onLoad(); + Promise.all(reorderedFiles.map(async (file) => await this._loadFileAsync(file, undefined, true).then((data) => new Uint8Array(data, 0, data.byteLength)))) + // eslint-disable-next-line github/no-then + .then(async (data) => { + return await new Promise((resolve, reject) => { + this._engine.loadCubeTexture( + texture._hardwareTexture!.underlyingResource, + data, + !noMipmap, + true, + texture._useSRGBBuffer, + () => resolve(), + () => reject(new Error("Failed to load native cubemap")) + ); + }); + }) + // eslint-disable-next-line github/no-then + .then( + () => { + texture.isReady = true; + if (onLoad) { + onLoad(); + } + }, + (error) => { + if (onError) { + onError(`Failed to load cubemap: ${error.message}`, error); + } } - }, - (error) => { - if (onError) { - onError(`Failed to load cubemap: ${error.message}`, error); + ); + } else if (files) { + throw new Error("Cannot load cubemap because 6 files were not defined"); + } else { + // Single self-contained cubemap container (.dds / .ktx / .ktx2) holding all six + // faces and their mip chain. The native engine decodes it with bimg and, when + // polynomials are requested, returns the spherical harmonics it computed from the + // top mip (native cannot read cube faces back from the GPU to derive them lazily). + const onContainerLoaded = (data: ArrayBufferView) => { + this._engine.loadCubeTexture( + texture._hardwareTexture!.underlyingResource, + [data], + !noMipmap, + true, + texture._useSRGBBuffer, + (sphericalPolynomialCoefficients?: Float32Array) => { + if (createPolynomials && sphericalPolynomialCoefficients && sphericalPolynomialCoefficients.length === 27) { + const c = sphericalPolynomialCoefficients; + texture._sphericalPolynomial = SphericalPolynomial.FromArray([ + [c[0], c[1], c[2]], + [c[3], c[4], c[5]], + [c[6], c[7], c[8]], + [c[9], c[10], c[11]], + [c[12], c[13], c[14]], + [c[15], c[16], c[17]], + [c[18], c[19], c[20]], + [c[21], c[22], c[23]], + [c[24], c[25], c[26]], + ]); + } + texture.isReady = true; + if (onLoad) { + onLoad(); + } + }, + () => { + if (onError) { + onError("Could not load a native cube texture."); + } } - } - ); + ); + }; + + if (buffer) { + onContainerLoaded(buffer); + } else { + const onInternalError = (request?: IWebRequest, exception?: any) => { + if (onError && request) { + onError(request.status + " " + request.statusText, exception); + } + }; + + this._loadFile( + rootUrl, + (data) => { + onContainerLoaded(new Uint8Array(data as ArrayBuffer, 0, (data as ArrayBuffer).byteLength)); + }, + undefined, + undefined, + true, + onInternalError + ); + } + } } this._internalTexturesCache.push(texture); diff --git a/packages/dev/core/src/Engines/Native/nativeInterfaces.ts b/packages/dev/core/src/Engines/Native/nativeInterfaces.ts index ff6b7a8e656..4d84dba0de9 100644 --- a/packages/dev/core/src/Engines/Native/nativeInterfaces.ts +++ b/packages/dev/core/src/Engines/Native/nativeInterfaces.ts @@ -61,9 +61,30 @@ export interface INativeEngine { getAttributes(shaderProgram: NativeProgram, attributeNames: string[]): number[]; createTexture(): NativeTexture; - initializeTexture(texture: NativeTexture, width: number, height: number, hasMips: boolean, format: number, renderTarget: boolean, srgb: boolean, samples: number): void; + initializeTexture( + texture: NativeTexture, + width: number, + height: number, + hasMips: boolean, + format: number, + renderTarget: boolean, + srgb: boolean, + samples: number, + isCube?: boolean + ): void; loadTexture(texture: NativeTexture, data: ArrayBufferView, generateMips: boolean, invertY: boolean, srgb: boolean, onSuccess: () => void, onError: () => void): void; loadRawTexture(texture: NativeTexture, data: ArrayBufferView, width: number, height: number, format: number, generateMips: boolean, invertY: boolean): void; + updateTextureData( + texture: NativeTexture, + data: ArrayBufferView, + xOffset: number, + yOffset: number, + width: number, + height: number, + faceIndex: number, + lod: number, + invertY: boolean + ): void; loadRawTexture2DArray( texture: NativeTexture, data: Nullable, @@ -74,7 +95,15 @@ export interface INativeEngine { generateMipMaps: boolean, invertY: boolean ): void; - loadCubeTexture(texture: NativeTexture, data: Array, generateMips: boolean, invertY: boolean, srgb: boolean, onSuccess: () => void, onError: () => void): void; + loadCubeTexture( + texture: NativeTexture, + data: Array, + generateMips: boolean, + invertY: boolean, + srgb: boolean, + onSuccess: (sphericalPolynomial?: Float32Array) => void, + onError: () => void + ): void; loadCubeTextureWithMips(texture: NativeTexture, data: Array>, invertY: boolean, srgb: boolean, onSuccess: () => void, onError: () => void): void; getTextureWidth(texture: NativeTexture): number; getTextureHeight(texture: NativeTexture): number; @@ -100,7 +129,8 @@ export interface INativeEngine { height: number, generateStencilBuffer: boolean, generateDepthBuffer: boolean, - samples: number + samples: number, + layer?: number ): NativeFramebuffer; getRenderWidth(): number; diff --git a/packages/dev/core/src/Engines/Native/nativeRenderTargetWrapper.ts b/packages/dev/core/src/Engines/Native/nativeRenderTargetWrapper.ts index f1ba367132a..37df65b7f76 100644 --- a/packages/dev/core/src/Engines/Native/nativeRenderTargetWrapper.ts +++ b/packages/dev/core/src/Engines/Native/nativeRenderTargetWrapper.ts @@ -11,6 +11,9 @@ export class NativeRenderTargetWrapper extends RenderTargetWrapper { private __framebuffer: Nullable = null; // eslint-disable-next-line @typescript-eslint/naming-convention private __framebufferDepthStencil: Nullable = null; + // Per-face framebuffers for cube render targets (index = cube face 0..5). + // eslint-disable-next-line @typescript-eslint/naming-convention + private __framebuffers: Nullable = null; public get _framebuffer(): Nullable { return this.__framebuffer; @@ -23,6 +26,21 @@ export class NativeRenderTargetWrapper extends RenderTargetWrapper { this.__framebuffer = framebuffer; } + public get _framebuffers(): Nullable { + return this.__framebuffers; + } + + public set _framebuffers(framebuffers: Nullable) { + if (this.__framebuffers) { + for (const framebuffer of this.__framebuffers) { + this._engine._releaseFramebufferObjects(framebuffer); + } + } + this.__framebuffers = framebuffers; + // Keep _framebuffer pointing at face 0 so single-target code paths still work. + this.__framebuffer = framebuffers ? framebuffers[0] : null; + } + public get _framebufferDepthStencil(): Nullable { return this.__framebufferDepthStencil; } @@ -40,7 +58,13 @@ export class NativeRenderTargetWrapper extends RenderTargetWrapper { } public override dispose(disposeOnlyFramebuffers = false): void { - this._framebuffer = null; + if (this.__framebuffers) { + // Releases all six per-face framebuffers (face 0 is aliased by __framebuffer, so + // clear that alias here without releasing it again). + this._framebuffers = null; + } else { + this._framebuffer = null; + } this._framebufferDepthStencil = null; super.dispose(disposeOnlyFramebuffers); diff --git a/packages/dev/core/src/Engines/thinNativeEngine.pure.ts b/packages/dev/core/src/Engines/thinNativeEngine.pure.ts index 2ff13889962..3e60ca368ec 100644 --- a/packages/dev/core/src/Engines/thinNativeEngine.pure.ts +++ b/packages/dev/core/src/Engines/thinNativeEngine.pure.ts @@ -227,6 +227,7 @@ export class ThinNativeEngine extends ThinEngine { private _frameStats: NativeFrameStats; private _boundBuffersVertexArray: any; private _currentDepthTest: number; + private _depthTestEnabled: boolean; private _stencilTest: boolean; private _stencilMask: number; private _stencilFunc: number; @@ -268,6 +269,7 @@ export class ThinNativeEngine extends ThinEngine { this._frameStats = { gpuTimeNs: Number.NaN }; this._boundBuffersVertexArray = null; this._currentDepthTest = _native.Engine.DEPTH_TEST_LEQUAL; + this._depthTestEnabled = true; this._stencilTest; this._stencilMask = 255; this._stencilFunc = Constants.ALWAYS; @@ -305,6 +307,10 @@ export class ThinNativeEngine extends ThinEngine { this._webGLVersion = 2; this.disableUniformBuffers = true; this._shaderPlatformName = "NATIVE"; + // Babylon Native is not WebGL and has no _gl context. Report a distinct engine name (like + // WebGPU reports "WebGPU") so application/feature code that branches on engine.name === "WebGL" + // to touch the WebGL-only _gl context skips the native engine instead of dereferencing null. + this._name = "Native"; // TODO: Initialize this more correctly based on the hardware capabilities. // Init caps @@ -735,6 +741,7 @@ export class ThinNativeEngine extends ThinEngine { return; } // Apply states + this._flushDepthTestState(); this._drawCalls.addCount(1, false); if (instancesCount) { @@ -765,6 +772,7 @@ export class ThinNativeEngine extends ThinEngine { return; } // Apply states + this._flushDepthTestState(); this._drawCalls.addCount(1, false); if (instancesCount) { @@ -1069,11 +1077,31 @@ export class ThinNativeEngine extends ThinEngine { * @param enable defines the state to set */ public override setDepthBuffer(enable: boolean): void { + // Keep the shared depth-culling state in sync so that code paths which toggle + // depth testing through engine.depthCullingState.depthTest (for example + // EffectRenderer.applyEffectWrapper) are also honored on the native side. The + // native draw path does not go through the WebGL applyStates() flush, so the + // value is reconciled in _flushDepthTestState() right before each draw. + this._depthCullingState.depthTest = enable; + this._encodeDepthTest(enable); + } + + private _encodeDepthTest(enable: boolean): void { + this._depthTestEnabled = enable; this._commandBufferEncoder.startEncodingCommand(_native.Engine.COMMAND_SETDEPTHTEST); this._commandBufferEncoder.encodeCommandArgAsUInt32(enable ? this._currentDepthTest : _native.Engine.DEPTH_TEST_ALWAYS); this._commandBufferEncoder.finishEncodingCommand(); } + private _flushDepthTestState(): void { + // Unlike the WebGL engine, the native engine does not call applyStates() before + // a draw, so depth-test toggles made directly on engine.depthCullingState are + // flushed here to match the cross-engine contract. + if (this._depthCullingState.depthTest !== this._depthTestEnabled) { + this._encodeDepthTest(this._depthCullingState.depthTest); + } + } + /** * Gets a boolean indicating if depth writing is enabled * @returns the current depth writing state @@ -2317,6 +2345,76 @@ export class ThinNativeEngine extends ThinEngine { return rtWrapper; } + public override createRenderTargetCubeTexture(size: number, options?: RenderTargetCreationOptions): RenderTargetWrapper { + const rtWrapper = this._createHardwareRenderTargetWrapper(false, true, size) as NativeRenderTargetWrapper; + + let generateDepthBuffer = true; + let generateStencilBuffer = false; + let generateMipMaps = false; + let type = Constants.TEXTURETYPE_UNSIGNED_BYTE; + let samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE; + let format = Constants.TEXTUREFORMAT_RGBA; + let samples = 1; + if (options !== undefined && typeof options === "object") { + generateDepthBuffer = options.generateDepthBuffer ?? true; + generateStencilBuffer = !!options.generateStencilBuffer; + generateMipMaps = !!options.generateMipMaps; + type = options.type ?? Constants.TEXTURETYPE_UNSIGNED_BYTE; + samplingMode = options.samplingMode ?? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE; + format = options.format ?? Constants.TEXTUREFORMAT_RGBA; + samples = options.samples ?? 1; + } + + if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloat) { + type = Constants.TEXTURETYPE_UNSIGNED_BYTE; + Logger.Warn("Float textures are not supported. Type forced to TEXTURETYPE_UNSIGNED_BYTE"); + } + + const texture = new InternalTexture(this, InternalTextureSource.RenderTarget); + texture.isCube = true; + texture.baseWidth = size; + texture.baseHeight = size; + texture.width = size; + texture.height = size; + texture.isReady = true; + texture.samples = samples; + texture.generateMipMaps = generateMipMaps; + texture.samplingMode = samplingMode; + texture.type = type; + texture.format = format; + + const nativeTexture = texture._hardwareTexture!.underlyingResource; + const nativeTextureFormat = getNativeTextureFormat(format, type); + // See the createRenderTargetTexture MSAA/mips note: avoid the mips + samples combo on bgfx. + const hasMips = samples > 1 ? false : generateMipMaps; + this._engine.initializeTexture(nativeTexture, size, size, hasMips, nativeTextureFormat, /*renderTarget*/ true, /*srgb*/ false, samples, /*isCube*/ true); + this._setTextureSampling(nativeTexture, getNativeSamplingMode(samplingMode)); + + // The native engine cannot render to all six faces through one framebuffer, so create one + // framebuffer per face (the C++ side binds the matching cube layer); bindFramebuffer(faceIndex) + // then selects the right one. + const framebuffers: NativeFramebuffer[] = []; + for (let face = 0; face < 6; face++) { + framebuffers.push(this._engine.createFrameBuffer(nativeTexture, size, size, generateStencilBuffer, generateDepthBuffer, samples, face)); + } + + rtWrapper._framebuffers = framebuffers; + rtWrapper._generateDepthBuffer = generateDepthBuffer; + rtWrapper._generateStencilBuffer = generateStencilBuffer; + rtWrapper._samples = samples; + + rtWrapper.setTextures(texture); + + return rtWrapper; + } + + public override generateMipMapsForCubemap(_texture: InternalTexture, _unbind = true): void { + // The WebGL path rebinds gl.TEXTURE_CUBE_MAP and calls gl.generateMipmap; both deref _gl, which is + // null on Native. bgfx auto-generates the mip chain when a render target texture created with mips is + // resolved (the same way 2D RTTs get their mips here -- unBindFramebuffer issues no explicit mipgen), + // so this is a no-op on Native. + } + public override updateRenderTargetTextureSampleCount(rtWrapper: RenderTargetWrapper, samples: number): number { if (rtWrapper.samples === samples) { return samples; @@ -2398,15 +2496,16 @@ export class ThinNativeEngine extends ThinEngine { this._currentRenderTarget = texture; - if (faceIndex) { - throw new Error("Cuboid frame buffers are not yet supported in NativeEngine."); - } - if (requiredWidth || requiredHeight) { throw new Error("Required width/height for frame buffers not yet supported in NativeEngine."); } - if (nativeRTWrapper._framebufferDepthStencil) { + if (nativeRTWrapper._framebuffers) { + // Cube render target: bind the framebuffer for the requested face. + this._bindUnboundFramebuffer(nativeRTWrapper._framebuffers[faceIndex ?? 0]); + } else if (faceIndex) { + throw new Error("Cuboid frame buffers are not yet supported in NativeEngine."); + } else if (nativeRTWrapper._framebufferDepthStencil) { this._bindUnboundFramebuffer(nativeRTWrapper._framebufferDepthStencil); } else { this._bindUnboundFramebuffer(nativeRTWrapper._framebuffer); @@ -2662,7 +2761,15 @@ export class ThinNativeEngine extends ThinEngine { lod: number = 0, generateMipMaps = false ): void { - throw new Error("updateTextureData not implemented."); + if (!texture._hardwareTexture) { + return; + } + + // bgfx updates the requested sub-rectangle of the existing texture (faceIndex selects the cube + // face / array layer, lod selects the mip level). invertY is forwarded so the native side can match + // the vertical orientation the base texture upload uses. Mip regeneration after a partial update is + // not supported on Native, so generateMipMaps is ignored (consistent with the other raw-texture paths). + this._engine.updateTextureData(texture._hardwareTexture.underlyingResource, imageData, xOffset, yOffset, width, height, faceIndex, lod, texture.invertY); } /**