diff --git a/.github/workflows/generate-vulkan-sdk-cache.yml b/.github/workflows/generate-vulkan-sdk-cache.yml index d74e624bd2..b35edee85e 100644 --- a/.github/workflows/generate-vulkan-sdk-cache.yml +++ b/.github/workflows/generate-vulkan-sdk-cache.yml @@ -6,7 +6,7 @@ on: vulkan_sdk_version: description: 'Vulkan SDK version' type: string - default: '1.2.189.0' + default: '1.3.243.0' required: true jobs: diff --git a/.github/workflows/native-compile-platforms.yml b/.github/workflows/native-compile-platforms.yml index ca3bd15a70..12d6daa715 100644 --- a/.github/workflows/native-compile-platforms.yml +++ b/.github/workflows/native-compile-platforms.yml @@ -17,7 +17,7 @@ jobs: name: "Windows" if: (! contains(github.event.pull_request.body, '[X] does not change any runtime related code or build configuration')) - runs-on: windows-2019 + runs-on: windows-2022 strategy: fail-fast: false matrix: @@ -35,7 +35,7 @@ jobs: - name: Install deps uses: humbletim/setup-vulkan-sdk@523828e49cd4afabce369c39c7ee6543a2b7a735 with: - vulkan-query-version: 1.2.189.0 + vulkan-query-version: 1.3.243.0 vulkan-components: Vulkan-Headers, Vulkan-Loader vulkan-use-cache: false - name: Compile win64 @@ -60,7 +60,7 @@ jobs: cd build-win64 RES_DIR=${GITHUB_WORKSPACE//\\//}/templates/windows/build-win64 COCOS_X_PATH=${GITHUB_WORKSPACE//\\//}/native - cmake ../ -G"Visual Studio 16 2019" -DRES_DIR=$RES_DIR -DCOCOS_X_PATH=$COCOS_X_PATH -Ax64 + cmake ../ -G"Visual Studio 17 2022" -DRES_DIR=$RES_DIR -DCOCOS_X_PATH=$COCOS_X_PATH -Ax64 cmake --build . --config ${{ matrix.config.buildtype }} echo "Compile Win64 ${{ matrix.config.buildtype }} Done!" @@ -258,7 +258,10 @@ jobs: fail-fast: false matrix: config: - - { arch: arm64-v8a } + - { arch: arm64-v8a, jsEngine: V8 } + - { arch: arm64-v8a, jsEngine: ARK } + - { arch: arm64-v8a, jsEngine: JSVM } + steps: - name: Show PATH env run: | @@ -391,9 +394,22 @@ jobs: echo "set(CMAKE_CXX_STANDARD_REQUIRED ON)" >> build-oh/proj/cfg.cmake echo "set(COCOS_X_PATH $NATIVE_ROOT)" >> build-oh/proj/cfg.cmake echo "set(CC_EXECUTABLE_NAME \"\")" >> build-oh/proj/cfg.cmake - echo "set(USE_SE_V8 ON)" >> build-oh/proj/cfg.cmake - echo "set(USE_SE_NAPI OFF)" >> build-oh/proj/cfg.cmake - echo "set(USE_SE_JSVM OFF)" >> build-oh/proj/cfg.cmake + if [ "${{ matrix.config.jsEngine }}" = "V8" ]; then + echo "=============== JsEngine: V8 ===============" + echo "set(USE_SE_V8 ON)" >> build-oh/proj/cfg.cmake + echo "set(USE_SE_NAPI OFF)" >> build-oh/proj/cfg.cmake + echo "set(USE_SE_JSVM OFF)" >> build-oh/proj/cfg.cmake + elif [ "${{ matrix.config.jsEngine }}" = "JSVM" ]; then + echo "=============== JsEngine: JSVM ===============" + echo "set(USE_SE_V8 OFF)" >> build-oh/proj/cfg.cmake + echo "set(USE_SE_NAPI OFF)" >> build-oh/proj/cfg.cmake + echo "set(USE_SE_JSVM ON)" >> build-oh/proj/cfg.cmake + else + echo "=============== JsEngine: ARK ===============" + echo "set(USE_SE_V8 OFF)" >> build-oh/proj/cfg.cmake + echo "set(USE_SE_NAPI ON)" >> build-oh/proj/cfg.cmake + echo "set(USE_SE_JSVM OFF)" >> build-oh/proj/cfg.cmake + fi echo "=============== cat build-oh/proj/cfg.cmake ===============" cat build-oh/proj/cfg.cmake diff --git a/.github/workflows/native-simulator-prebuilt.yml b/.github/workflows/native-simulator-prebuilt.yml index d1240c8e35..a6008502cd 100644 --- a/.github/workflows/native-simulator-prebuilt.yml +++ b/.github/workflows/native-simulator-prebuilt.yml @@ -62,7 +62,7 @@ jobs: - name: Install deps uses: humbletim/setup-vulkan-sdk@523828e49cd4afabce369c39c7ee6543a2b7a735 with: - vulkan-query-version: 1.2.189.0 + vulkan-query-version: 1.3.243.0 vulkan-components: Vulkan-Headers, Vulkan-Loader vulkan-use-cache: false - name: gen simulator diff --git a/.github/workflows/native-simulator.yml b/.github/workflows/native-simulator.yml index d6daa307ce..12abb694b4 100644 --- a/.github/workflows/native-simulator.yml +++ b/.github/workflows/native-simulator.yml @@ -14,7 +14,7 @@ concurrency: jobs: win_gen_simulator: - runs-on: windows-2019 + runs-on: windows-2022 if: (! contains(github.event.pull_request.body, '[X] does not change any runtime related code or build configuration')) name: Windows @@ -36,7 +36,7 @@ jobs: - name: Install deps uses: humbletim/setup-vulkan-sdk@523828e49cd4afabce369c39c7ee6543a2b7a735 with: - vulkan-query-version: 1.2.189.0 + vulkan-query-version: 1.3.243.0 vulkan-components: Vulkan-Headers, Vulkan-Loader vulkan-use-cache: false - name: gen simulator diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 8f5891acfb..0caf803b34 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -1875,7 +1875,7 @@ We don't currently support conversion of \`CubicSplineQuatValue\`. ### 3936 -Instancing/Batching enabled for non-baked skinning model '%s', this may result in unexpected rendering artifacts. Consider turning it off in the material if you do not intend to do this. +Instancing/Batching enabled for non-baked skinning model or used AnimationController '%s', this may result in unexpected rendering artifacts. Consider turning it off in the material if you do not intend to do this. ### 3937 diff --git a/cocos/2d/event/pointer-event-dispatcher.ts b/cocos/2d/event/pointer-event-dispatcher.ts index 552d1a149e..8483339317 100644 --- a/cocos/2d/event/pointer-event-dispatcher.ts +++ b/cocos/2d/event/pointer-event-dispatcher.ts @@ -26,7 +26,7 @@ import { Node } from '../../scene-graph/node'; import { Input, input } from '../../input'; import { Event, EventMouse, EventTouch } from '../../input/types'; import { DispatcherEventType, NodeEventProcessor } from '../../scene-graph/node-event-processor'; -import { js } from '../../core'; +import { CCObjectFlags, js } from '../../core'; import { InputEventType } from '../../input/types/event-enum'; import { EventDispatcherPriority, IEventDispatcher } from '../../input/input'; @@ -44,7 +44,7 @@ const touchEvents = [ InputEventType.TOUCH_END, InputEventType.TOUCH_CANCEL, ]; - +const isDestroy = (node: Node): boolean => !!((node._objFlags & CCObjectFlags.Destroying) || (node._objFlags & CCObjectFlags.Destroyed)); class PointerEventDispatcher implements IEventDispatcher { public priority: EventDispatcherPriority = EventDispatcherPriority.UI; @@ -227,9 +227,9 @@ class PointerEventDispatcher implements IEventDispatcher { private _sortByPriority (p1: NodeEventProcessor, p2: NodeEventProcessor): number { const node1: Node = p1.node; const node2: Node = p2.node; - if (!p2 || !node2 || !node2.activeInHierarchy || !node2._getUITransformComp()) { + if (!p2 || !node2 || isDestroy(node2) || !node2.activeInHierarchy || !node2._getUITransformComp()) { return -1; - } else if (!p1 || !node1 || !node1.activeInHierarchy || !node1._getUITransformComp()) { + } else if (!p1 || !node1 || isDestroy(node1) || !node1.activeInHierarchy || !node1._getUITransformComp()) { return 1; } diff --git a/cocos/asset/asset-manager/bin-package-unpack.ts b/cocos/asset/asset-manager/bin-package-unpack.ts index a5c6d825c9..9779b6e54d 100644 --- a/cocos/asset/asset-manager/bin-package-unpack.ts +++ b/cocos/asset/asset-manager/bin-package-unpack.ts @@ -22,6 +22,60 @@ THE SOFTWARE. */ +/** + * bin package format example. +|PACK_BIN_TYPE - 4bytes| +|VERSION - 4bytes| +|FILES_COUNT - 4bytes| +|FILE_1_OFFSET - 4bytes| +|FILE_1_SIZE - 4bytes| +|FILE_2_OFFSET - 4bytes| +|FILE_2_SIZE - 4bytes| +... +|FILE_N_OFFSET - 4bytes| +|FILE_N_SIZE - 4bytes| +|PACKED_BIN| + */ + +const PACK_BIN_TYPE = 'BINP'; +const VERSION = 2; +const UNIT_SIZE = 4; +const LITTLE_ENDIAN = true; + +type UnpackFunc = (arrayBuffer: ArrayBuffer, dataView: DataView) => Uint8Array[]; + +export function binPackageUnpack (arrayBuffer: ArrayBuffer): Uint8Array[] { + const dataView = new DataView(arrayBuffer, 0, arrayBuffer.byteLength); + + const packBinType = String.fromCharCode(...new Uint8Array(arrayBuffer.slice(0, UNIT_SIZE))); + if (packBinType !== PACK_BIN_TYPE) { + throw new Error('Invalid bin package format'); + } + + const version = dataView.getUint32(UNIT_SIZE, LITTLE_ENDIAN); + if (version !== VERSION) { + return compatibleOlderVersion(arrayBuffer, dataView, version); + } + const filesCount = dataView.getUint32(UNIT_SIZE * 2, LITTLE_ENDIAN); + const filesPosition: { offset: number, size: number }[] = []; + + const headOffset = UNIT_SIZE * (3 + filesCount * 2); + for (let i = 0; i < filesCount; i++) { + const offset = dataView.getUint32(UNIT_SIZE * (3 + i * 2), LITTLE_ENDIAN) + headOffset; + const size = dataView.getUint32(UNIT_SIZE * (3 + i * 2 + 1), LITTLE_ENDIAN); + filesPosition.push({ offset, size }); + } + + return filesPosition.map(({ offset, size }) => new Uint8Array(arrayBuffer, offset, size)); +} + +function compatibleOlderVersion (arrayBuffer: ArrayBuffer, dataView: DataView, version: number): Uint8Array[] { + const map: Record = { + 1: unpack_v1, + }; + return map[version](arrayBuffer, dataView); +} + /** * bin package format example. |PACK_BIN_TYPE - 4bytes| @@ -33,23 +87,9 @@ |FILE_N_SIZE - 4bytes| |PACKED_BIN| */ - -const PACK_BIN_TYPE = 'BINP'; -const VERSION = 1; -const UNIT_SIZE = 4; -const LITTLE_ENDIAN = true; - -export function binPackageUnpack (arrayBuffer: ArrayBuffer): Uint8Array[] { - const dataView = new DataView(arrayBuffer, 0, arrayBuffer.byteLength); - - const packBinType = String.fromCharCode(...new Uint8Array(arrayBuffer.slice(0, UNIT_SIZE))); - const version = dataView.getUint32(UNIT_SIZE, LITTLE_ENDIAN); - if (packBinType !== PACK_BIN_TYPE || version !== VERSION) { - throw new Error('Invalid bin package format'); - } - +function unpack_v1 (arrayBuffer: ArrayBuffer, dataView: DataView): Uint8Array[] { const filesCount = dataView.getUint32(UNIT_SIZE * 2, LITTLE_ENDIAN); - const filesPosition: {offset: number, size: number}[] = []; + const filesPosition: { offset: number, size: number }[] = []; let offset = UNIT_SIZE * (3 + filesCount); for (let i = 0; i < filesCount; i++) { diff --git a/cocos/asset/asset-manager/downloader.ts b/cocos/asset/asset-manager/downloader.ts index acd0bf6608..e028a0bd46 100644 --- a/cocos/asset/asset-manager/downloader.ts +++ b/cocos/asset/asset-manager/downloader.ts @@ -69,18 +69,26 @@ const downloadArrayBuffer = (url: string, options: Record, onComple }; const downloadCCONB = (url: string, options: Record, onComplete: ((err: Error | null, data?: CCON | null) => void)): void => { - url = url.replace('.cconb', '.bin'); - downloader._downloadArrayBuffer(url, options, (err, arrayBuffer: ArrayBuffer): void => { - if (err) { - onComplete(err); - return; - } + const handleArrayBuffer = (arrayBuffer: ArrayBuffer, onComplete: ((err: Error | null, data?: CCON | null) => void)): void => { try { const ccon = decodeCCONBinary(new Uint8Array(arrayBuffer)); onComplete(null, ccon); } catch (err) { onComplete(err as Error); } + }; + downloader._downloadArrayBuffer(url.replace('.cconb', '.bin'), options, (err, arrayBuffer: ArrayBuffer): void => { + if (err) { + downloader._downloadArrayBuffer(url, options, (err, arrayBuffer: ArrayBuffer): void => { + if (err) { + onComplete(err); + return; + } + handleArrayBuffer(arrayBuffer, onComplete); + }); + return; + } + handleArrayBuffer(arrayBuffer, onComplete); }); }; diff --git a/cocos/rendering/custom/define.ts b/cocos/rendering/custom/define.ts index 6d180c9f94..eda1f4b750 100644 --- a/cocos/rendering/custom/define.ts +++ b/cocos/rendering/custom/define.ts @@ -891,14 +891,6 @@ export function genHashValue (pass: RasterPass): void { hashCode += hashCombineKey(compute.shaderStageFlags); } } - hashCode += hashCombineKey(pass.width); - hashCode += hashCombineKey(pass.height); - hashCode += hashCombineKey(pass.viewport.left); - hashCode += hashCombineKey(pass.viewport.top); - hashCode += hashCombineKey(pass.viewport.width); - hashCode += hashCombineKey(pass.viewport.height); - hashCode += hashCombineKey(pass.viewport.minDepth); - hashCode += hashCombineKey(pass.viewport.maxDepth); hashCode += hashCombineKey(pass.showStatistics ? 1 : 0); pass.hashValue = hashCombineStr(hashCode); } diff --git a/cocos/rendering/custom/web-pipeline.ts b/cocos/rendering/custom/web-pipeline.ts index d343c15288..f64d1ba3e8 100644 --- a/cocos/rendering/custom/web-pipeline.ts +++ b/cocos/rendering/custom/web-pipeline.ts @@ -256,30 +256,9 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild addSceneOfCamera (camera: Camera, light: LightInfo, sceneFlags = SceneFlags.NONE, name = 'Camera'): void { const lightTarget = light.light; - const sceneData = renderGraphPool.createSceneData( - camera.scene, - camera, - sceneFlags, - lightTarget && !(sceneFlags & SceneFlags.SHADOW_CASTER) ? CullingFlags.CAMERA_FRUSTUM | CullingFlags.LIGHT_BOUNDS : CullingFlags.CAMERA_FRUSTUM, - lightTarget, - ); - this._renderGraph.addVertex(RenderGraphValue.Scene, sceneData, name, '', renderGraphPool.createRenderData(), !DEBUG, this._vertID); - const layoutName = this.getParentLayout(); - const scene: Scene = cclegacy.director.getScene(); - setCameraUBOValues( - this, - camera, - this._pipeline, - camera.scene || (scene ? scene.renderScene : null), - layoutName, - ); - if (sceneFlags & SceneFlags.SHADOW_CASTER || (lightTarget && lightTarget.type !== LightType.DIRECTIONAL)) { - setShadowUBOLightView(this, camera, lightTarget!, light.level, layoutName); - } else { - setShadowUBOView(this, camera, layoutName); - } + this.addScene(camera, sceneFlags, lightTarget); } - addScene (camera: Camera, sceneFlags = SceneFlags.NONE, light: Light | undefined = undefined, scene: RenderScene | undefined = undefined): SceneBuilder { + addScene (camera: Camera, sceneFlags = SceneFlags.NONE, light: Light | undefined | null = null, scene: RenderScene | undefined = undefined): SceneBuilder { const sceneData = renderGraphPool.createSceneData( scene || camera.scene, camera, @@ -407,6 +386,14 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild } } addDraw2D (camera: Camera): void { + const layoutName = this.getParentLayout(); + setCameraUBOValues( + this, + camera, + this._pipeline, + camera.scene, + layoutName, + ); this._renderGraph.addVertex( RenderGraphValue.Blit, renderGraphPool.createBlit(emptyMaterial, this._renderGraph.N, SceneFlags.NONE, camera, BlitType.DRAW_2D), @@ -419,15 +406,6 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild } addProfiler (camera: Camera): void { const passOrSubpassId = this._renderGraph.getParent(this._vertID); - this._renderGraph.addVertex( - RenderGraphValue.Blit, - renderGraphPool.createBlit(emptyMaterial, this._renderGraph.N, SceneFlags.NONE, camera, BlitType.DRAW_PROFILE), - 'DrawProfiler', - '', - emptyRenderData, - !DEBUG, - this._vertID, - ); const queueId = this._renderGraph.addVertex( RenderGraphValue.Queue, this._queue, diff --git a/cocos/rendering/ui-phase.ts b/cocos/rendering/ui-phase.ts index 43e223a5f6..88fa8f9a4a 100644 --- a/cocos/rendering/ui-phase.ts +++ b/cocos/rendering/ui-phase.ts @@ -61,6 +61,7 @@ export class UIPhase { for (let j = 0; j < count; j++) { const pass = batch.passes[j]; if (pass.phase !== this._phaseID) continue; + if (pass.passID !== 0xFFFFFFFF) continue; const shader = batch.shaders[j]; const inputAssembler = batch.inputAssembler!; const pso = PipelineStateManager.getOrCreatePipelineState(device, pass, shader, renderPass, inputAssembler); diff --git a/cocos/serialization/ccon.ts b/cocos/serialization/ccon.ts index 18bb8d5eb4..eb95272f4d 100644 --- a/cocos/serialization/ccon.ts +++ b/cocos/serialization/ccon.ts @@ -26,7 +26,7 @@ import { getError, cclegacy } from '../core'; import { notepackDecode } from '../../external/deserialize/notepack_decode'; import { notepackEncode } from '../../external/deserialize/notepack_encode'; -const VERSION = 1; +const VERSION = 2; const MAGIC = 0x4E4F4343; @@ -100,9 +100,6 @@ export function decodeCCONBinary (bytes: Uint8Array): CCON { } const version = dataView.getUint32(4, true); - if (version !== VERSION) { - throw new InvalidCCONError(getError(13101, version)); - } const dataByteLength = dataView.getUint32(8, true); if (dataByteLength !== dataView.byteLength) { @@ -117,7 +114,12 @@ export function decodeCCONBinary (bytes: Uint8Array): CCON { chunksStart += jsonDataLength; let json: unknown; try { - json = notepackDecode(jsonData); + if (version === 1) { + const jsonString = decodeJson(jsonData); + json = JSON.parse(jsonString); + } else if (version === 2) { + json = notepackDecode(jsonData); + } } catch (err) { throw new InvalidCCONError(err as string); } @@ -157,6 +159,18 @@ interface BufferConstructor { from(buffer: ArrayBuffer, byteOffset?: number, byteLength?: number): Buffer; } +function decodeJson (data: Uint8Array): string { + if (typeof TextDecoder !== 'undefined') { + return new TextDecoder().decode(data); + } else if ('Buffer' in globalThis) { + const { Buffer } = (globalThis as unknown as { Buffer: BufferConstructor }); + // eslint-disable-next-line no-buffer-constructor + return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString(); + } else { + throw new Error(getError(13104)); + } +} + export class InvalidCCONError extends Error { } export class BufferBuilder { diff --git a/cocos/spine/assembler/simple.ts b/cocos/spine/assembler/simple.ts index c9f7e3d8c4..7578daee7f 100644 --- a/cocos/spine/assembler/simple.ts +++ b/cocos/spine/assembler/simple.ts @@ -36,6 +36,13 @@ import { Color, EPSILON, Vec3 } from '../../core'; import type { MaterialInstance } from '../../render-scene'; import type { IBatcher } from '../../2d/renderer/i-batcher'; +/** + * If Spine instance counts exceeding thresholds cause issues, first adjust the ADJUST_SIZE_RATE value. + Going forward, we need to implement independent memory management for Spine that bypasses the chunk approach. + Similar to native IOBuffers where populated content transfers directly to GPU buffers without going through static_vb_accessor. + For now, provide the minimal viable change. + */ +const ADJUST_SIZE_RATE = 1.1; const _slotColor = new Color(0, 0, 255, 255); const _boneColor = new Color(255, 0, 0, 255); const _originColor = new Color(0, 255, 0, 255); @@ -151,10 +158,12 @@ function realTimeTraverse (comp: Skeleton): void { if (!rd || vc < 1 || ic < 1) return; if (rd.vertexCount !== vc || rd.indexCount !== ic) { - rd.resize(vc, ic); + if (rd.vertexCount < vc || rd.indexCount < ic) { + rd.resize(Math.ceil(vc * ADJUST_SIZE_RATE), Math.ceil(ic * ADJUST_SIZE_RATE)); + } rd.indices = new Uint16Array(ic); comp._vLength = vc * Float32Array.BYTES_PER_ELEMENT * floatStride; - comp._vBuffer = new Uint8Array(rd.chunk.vb.buffer, rd.chunk.vb.byteOffset, Float32Array.BYTES_PER_ELEMENT * rd.chunk.vb.length); + comp._vBuffer = new Uint8Array(rd.chunk.vb.buffer, rd.chunk.vb.byteOffset, comp._vLength); comp._iLength = Uint16Array.BYTES_PER_ELEMENT * ic; comp._iBuffer = new Uint8Array(rd.indices.buffer); } @@ -178,7 +187,7 @@ function realTimeTraverse (comp: Skeleton): void { for (let i = 0; i < count; i += 5) { indexCount = data.get(i + 3); const material = _getSlotMaterial(data.get(i + 4) as number, comp); - comp.requestDrawData(material, textures.get(i / 5), indexOffset, indexCount); + comp.requestDrawData(material, textures.get(i / 5) as string, indexOffset, indexCount); indexOffset += indexCount; } @@ -272,7 +281,9 @@ function cacheTraverse (comp: Skeleton): void { const rd = comp.renderData; if (!rd || vc < 1 || ic < 1) return; if (rd.vertexCount !== vc || rd.indexCount !== ic) { - rd.resize(vc, ic); + if (rd.vertexCount < vc || rd.indexCount < ic) { + rd.resize(Math.ceil(vc * ADJUST_SIZE_RATE), Math.ceil(ic * ADJUST_SIZE_RATE)); + } rd.indices = new Uint16Array(ic); } @@ -323,7 +334,7 @@ function cacheTraverse (comp: Skeleton): void { const material = _getSlotMaterial(mesh.blendMode as number, comp); const textureID = mesh.textureID; indexCount = mesh.iCount; - comp.requestDrawData(material, textureID, indexOffset, indexCount); + comp.requestDrawData(material, textureID as string, indexOffset, indexCount); indexOffset += indexCount; } diff --git a/cocos/spine/skeleton.ts b/cocos/spine/skeleton.ts index e4879c40ab..9e72ee959a 100644 --- a/cocos/spine/skeleton.ts +++ b/cocos/spine/skeleton.ts @@ -647,6 +647,13 @@ export class Skeleton extends UIRenderer { this._markForUpdateRenderData(); } + /** + * @deprecated Since v3.8.7, it will be removed in the future. + * We are deprecating the `customMaterialInstance` field because it leads to shared material state across all slots, + * causing unexpected behavior (like the last blendMode change affecting all slots). + * Workaround: + * Switch to customMaterial. Whenever its value is modified, immediately call updateMaterial at the exact point of change. + */ get customMaterialInstance (): MaterialInstance | null { if (!this._customMaterial) { return null; diff --git a/cocos/tiledmap/tiled-layer.ts b/cocos/tiledmap/tiled-layer.ts index 12e53523a5..7c0f4ec66f 100644 --- a/cocos/tiledmap/tiled-layer.ts +++ b/cocos/tiledmap/tiled-layer.ts @@ -457,8 +457,8 @@ export class TiledLayer extends UIRenderer { this.node.on(NodeEventType.SIZE_CHANGED, this.updateCulling, this); this.node.parent!.on(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this); this.node.parent!.on(NodeEventType.SIZE_CHANGED, this.updateCulling, this); - View.instance.on('canvas-resize', this._resize); - screenAdapter.on('window-resize', this._resize); + View.instance.on('canvas-resize', this._resize, this); + screenAdapter.on('window-resize', this._resize, this); this._markForUpdateRenderData(); // delay 1 frame, since camera's matrix data is dirty this.scheduleOnce(this.updateCulling.bind(this)); @@ -471,8 +471,8 @@ export class TiledLayer extends UIRenderer { this.node.off(NodeEventType.SIZE_CHANGED, this.updateCulling, this); this.node.off(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this); this.node.off(NodeEventType.ANCHOR_CHANGED, this._syncAnchorPoint, this); - View.instance.off('canvas-resize', this._resize); - screenAdapter.off('window-resize', this._resize); + View.instance.off('canvas-resize', this._resize, this); + screenAdapter.off('window-resize', this._resize, this); this._uninstallCamera(); } diff --git a/editor/assets/chunks/legacy/main-functions/general-vs.chunk b/editor/assets/chunks/legacy/main-functions/general-vs.chunk index ceed7f5730..3aa4dd2778 100644 --- a/editor/assets/chunks/legacy/main-functions/general-vs.chunk +++ b/editor/assets/chunks/legacy/main-functions/general-vs.chunk @@ -42,6 +42,9 @@ vec4 vert () { CC_TRANSFER_FOG(pos); CC_TRANSFER_SHADOW(pos); - - return cc_matProj * (cc_matView * matWorld) * In.position; + #if CC_USE_2D + return cc_matProj * cc_matView * In.position; + #else + return cc_matProj * (cc_matView * matWorld) * In.position; + #endif } diff --git a/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk b/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk index 3f09f3b939..434767878d 100644 --- a/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk +++ b/editor/assets/chunks/shading-entries/main-functions/render-to-scene/vs.chunk @@ -15,10 +15,13 @@ void main() // World Space CCSurfacesVertexWorldTransform(In); In.worldPos = SurfacesVertexModifyWorldPos(In); - // Clip Space - In.clipPos = cc_matProj * cc_matView * vec4(In.worldPos, 1.0); - In.clipPos = SurfacesVertexModifyClipPos(In); + #if CC_USE_2D + In.clipPos = cc_matProj * cc_matView * vec4(In.position.xyz, 1.0); + #else + In.clipPos = cc_matProj * cc_matView * vec4(In.worldPos, 1.0); + In.clipPos = SurfacesVertexModifyClipPos(In); + #endif // Other Surfaces Function vec3 viewDirect = normalize(cc_cameraPos.xyz - In.worldPos); diff --git a/editor/assets/default_renderpipeline/builtin-pipeline-settings.ts b/editor/assets/default_renderpipeline/builtin-pipeline-settings.ts index 8fe7201253..e0d6983fad 100644 --- a/editor/assets/default_renderpipeline/builtin-pipeline-settings.ts +++ b/editor/assets/default_renderpipeline/builtin-pipeline-settings.ts @@ -24,7 +24,7 @@ import { _decorator, Camera, CCBoolean, CCFloat, CCInteger, Component, - Material, rendering, Texture2D, visible, + Material, rendering, Texture2D, } from 'cc'; import { EDITOR } from 'cc/env'; @@ -55,6 +55,7 @@ export class BuiltinPipelineSettings extends Component { const cameraComponent = this.getComponent(Camera)!; const camera = cameraComponent.camera; camera.pipelineSettings = this._settings; + if (EDITOR) { this._tryEnableEditorPreview(); } @@ -62,8 +63,9 @@ export class BuiltinPipelineSettings extends Component { onDisable(): void { const cameraComponent = this.getComponent(Camera)!; const camera = cameraComponent.camera; - camera.pipelineSettings = null; - + if (camera) { + camera.pipelineSettings = null; + } if (EDITOR) { this._disableEditorPreview(); } @@ -392,6 +394,7 @@ export class BuiltinPipelineSettings extends Component { type: CCBoolean, }) set fsrEnable(value: boolean) { + this._settings.fsr.enabled = value; if (EDITOR) { this._tryEnableEditorPreview(); } diff --git a/editor/assets/default_renderpipeline/builtin-pipeline-types.ts b/editor/assets/default_renderpipeline/builtin-pipeline-types.ts index 6a67694857..a1af6a41d1 100644 --- a/editor/assets/default_renderpipeline/builtin-pipeline-types.ts +++ b/editor/assets/default_renderpipeline/builtin-pipeline-types.ts @@ -28,7 +28,7 @@ * ========================= !DO NOT CHANGE THE FOLLOWING SECTION MANUALLY! ========================= */ /* eslint-disable max-len */ -import { Material, Texture2D, gfx, ccenum } from 'cc'; +import { Material, Texture2D, ccenum, gfx } from 'cc'; const { SampleCount } = gfx; @@ -38,14 +38,6 @@ export interface MSAA { [name: string]: unknown; } -export interface ForwardPassConfigs { - enableMainLightShadowMap: boolean; - enableMainLightPlanarShadowMap: boolean; - enablePlanarReflectionProbe: boolean; - enableMSAA: boolean; - enableSingleForwardPass: boolean; -} - export function makeMSAA(): MSAA { return { enabled: false, @@ -62,6 +54,15 @@ export function fillRequiredMSAA(value: MSAA): void { } } +export interface ForwardPassConfigs { + enableMainLightShadowMap: boolean; /* false */ + enableMainLightPlanarShadowMap: boolean; /* false */ + enablePlanarReflectionProbe: boolean; /* false */ + enableMSAA: boolean; /* false */ + enableSingleForwardPass: boolean; /* false */ + [name: string]: unknown; +} + export interface HBAO { enabled: boolean; /* false */ radiusScale: number; /* 1 */ @@ -109,12 +110,13 @@ export enum BloomType { MipmapFilter, } ccenum(BloomType); + export interface Bloom { enabled: boolean; /* false */ - type: BloomType; - material: Material | null; + type: BloomType; /* BloomType.KawaseDualFilter */ + /* refcount */ material: Material | null; /* refcount */ kawaseFilterMaterial: Material | null; - mipmapFilterMaterial: Material | null; + /* refcount */ mipmapFilterMaterial: Material | null; enableAlphaMask: boolean; /* false */ iterations: number; /* 3 */ threshold: number; /* 0.8 */ @@ -143,13 +145,16 @@ export function fillRequiredBloom(value: Bloom): void { if (value.type === undefined) { value.type = BloomType.KawaseDualFilter; } - if (!value.kawaseFilterMaterial) { + if (value.material === undefined) { + value.material = null; + } + if (value.kawaseFilterMaterial === undefined) { value.kawaseFilterMaterial = value.material || null; } - if (!value.mipmapFilterMaterial) { + if (value.mipmapFilterMaterial === undefined) { value.mipmapFilterMaterial = null; } - if (!value.enableAlphaMask) { + if (value.enableAlphaMask === undefined) { value.enableAlphaMask = false; } if (value.iterations === undefined) { diff --git a/editor/assets/default_renderpipeline/builtin-pipeline.ts b/editor/assets/default_renderpipeline/builtin-pipeline.ts index 02a98a54e0..3dc4613a3c 100644 --- a/editor/assets/default_renderpipeline/builtin-pipeline.ts +++ b/editor/assets/default_renderpipeline/builtin-pipeline.ts @@ -32,7 +32,6 @@ import { DEBUG, EDITOR } from 'cc/env'; import { BloomType, - ForwardPassConfigs, makePipelineSettings, PipelineSettings, } from './builtin-pipeline-types'; @@ -42,27 +41,6 @@ const { ClearFlagBit, Color, Format, FormatFeatureBit, LoadOp, StoreOp, TextureT const { scene } = renderer; const { CameraUsage, CSMLevel, LightType } = scene; -let ppl: rendering.BasicPipeline; -let pplConfigs: PipelineConfigs; - -function buildScreenQuadPass( - width: number, - height: number, - layout: string, - colorName: string, - material: Material, - passIndex: number, - loadOp: gfx.LoadOp = LoadOp.CLEAR, - clearColor: gfx.Color = sClearColorTransparentBlack, - queueHint: rendering.QueueHint = rendering.QueueHint.OPAQUE, -): rendering.BasicRenderPassBuilder { - const pass = ppl.addRenderPass(width, height, layout); - pass.addRenderTarget(colorName, loadOp, StoreOp.STORE, clearColor); - pass.addQueue(queueHint) - .addFullscreenQuad(material, passIndex); - return pass; -} - function forwardNeedClearColor(camera: renderer.scene.Camera): boolean { return !!(camera.clearFlag & (ClearFlagBit.COLOR | (ClearFlagBit.STENCIL << 1))); } @@ -75,21 +53,21 @@ function getCsmMainLightViewport( vp: gfx.Viewport, screenSpaceSignY: number, ): void { - const halfW = Math.trunc(w * 0.5); - const halfH = Math.trunc(h * 0.5); if (light.shadowFixedArea || light.csmLevel === CSMLevel.LEVEL_1) { vp.left = 0; vp.top = 0; vp.width = Math.trunc(w); vp.height = Math.trunc(h); } else { - vp.left = Math.trunc((level % 2) * halfW); - const row = Math.floor(level / 2); - vp.top = Math.trunc((screenSpaceSignY > 0 ? (1 - row) : row) * halfH); - vp.width = halfW; - vp.height = halfH; + vp.left = Math.trunc(level % 2 * 0.5 * w); + if (screenSpaceSignY > 0) { + vp.top = Math.trunc((1 - Math.floor(level / 2)) * 0.5 * h); + } else { + vp.top = Math.trunc(Math.floor(level / 2) * 0.5 * h); + } + vp.width = Math.trunc(0.5 * w); + vp.height = Math.trunc(0.5 * h); } - vp.left = Math.max(0, vp.left); vp.top = Math.max(0, vp.top); vp.width = Math.max(1, vp.width); @@ -116,6 +94,7 @@ export class PipelineConfigs { } function setupPipelineConfigs( + ppl: rendering.BasicPipeline, configs: PipelineConfigs, ): void { const sampleFeature = FormatFeatureBit.SAMPLED_TEXTURE | FormatFeatureBit.LINEAR_FILTER; @@ -195,19 +174,23 @@ function sortPipelinePassBuildersByRenderOrder(passBuilders: rendering.PipelineP } function addCopyToScreenPass( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs, input: string, ): rendering.BasicRenderPassBuilder { assert(!!cameraConfigs.copyAndTonemapMaterial); - const pass = buildScreenQuadPass( - cameraConfigs.nativeWidth, - cameraConfigs.nativeHeight, - 'cc-tone-mapping', - cameraConfigs.colorName, - cameraConfigs.copyAndTonemapMaterial, - 1, - ); + const pass = ppl.addRenderPass( + cameraConfigs.nativeWidth, + cameraConfigs.nativeHeight, + 'cc-tone-mapping'); + pass.addRenderTarget( + cameraConfigs.colorName, + LoadOp.CLEAR, StoreOp.STORE, + sClearColorTransparentBlack); pass.addTexture(input, 'inputTexture'); + pass.addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(cameraConfigs.copyAndTonemapMaterial, 1); return pass; } @@ -242,33 +225,40 @@ class ForwardLighting { // TODO(zhouzhenglong): Make light culling native this.lights.length = 0; this.shadowEnabledSpotLights.length = 0; - const cullSphereLights = ( - lights: Array<{ baked: boolean, position: Vec3, range: number, shadowEnabled?: boolean }>, - onVisible: (light: any) => void, - checkShadow?: boolean - ) => { - for (const light of lights) { - if (light.baked) { continue; } - Sphere.set(this._sphere, light.position.x, light.position.y, light.position.z, light.range); - if (intersect.sphereFrustum(this._sphere, frustum)) { - if (checkShadow && light.shadowEnabled) { - this.shadowEnabledSpotLights.push(light); - } else { - onVisible(light); - } + // spot lights + for (const light of scene.spotLights) { + if (light.baked) { + continue; + } + Sphere.set(this._sphere, light.position.x, light.position.y, light.position.z, light.range); + if (intersect.sphereFrustum(this._sphere, frustum)) { + if (light.shadowEnabled) { + this.shadowEnabledSpotLights.push(light); + } else { + this.lights.push(light); } } - }; - - // spot lights - cullSphereLights(scene.spotLights, (light) => this.lights.push(light), true); - + } // sphere lights - cullSphereLights(scene.sphereLights, (light) => this.lights.push(light)); - + for (const light of scene.sphereLights) { + if (light.baked) { + continue; + } + Sphere.set(this._sphere, light.position.x, light.position.y, light.position.z, light.range); + if (intersect.sphereFrustum(this._sphere, frustum)) { + this.lights.push(light); + } + } // point lights - cullSphereLights(scene.pointLights, (light) => this.lights.push(light)); - + for (const light of scene.pointLights) { + if (light.baked) { + continue; + } + Sphere.set(this._sphere, light.position.x, light.position.y, light.position.z, light.range); + if (intersect.sphereFrustum(this._sphere, frustum)) { + this.lights.push(light); + } + } // ranged dir lights for (const light of scene.rangedDirLights) { AABB.transform(this._boundingBox, this._rangedDirLightBoundingBox, light.node!.getWorldMatrix()); @@ -276,6 +266,7 @@ class ForwardLighting { this.lights.push(light); } } + if (cameraPos) { this.shadowEnabledSpotLights.sort( (lhs, rhs) => Vec3.squaredDistance(cameraPos, lhs.position) - Vec3.squaredDistance(cameraPos, rhs.position), @@ -309,6 +300,7 @@ class ForwardLighting { } } public addSpotlightShadowPasses( + ppl: rendering.BasicPipeline, camera: renderer.scene.Camera, maxNumShadowMaps: number, ): void { @@ -358,6 +350,7 @@ class ForwardLighting { height: number, camera: renderer.scene.Camera, viewport: gfx.Viewport, + ppl: rendering.BasicPipeline, pass: rendering.BasicRenderPassBuilder, ): rendering.BasicRenderPassBuilder { this._addLightQueues(camera, pass); @@ -402,6 +395,14 @@ class ForwardLighting { } } +export interface ForwardPassConfigs { + enableMainLightShadowMap: boolean; + enableMainLightPlanarShadowMap: boolean; + enablePlanarReflectionProbe: boolean; + enableMSAA: boolean; + enableSingleForwardPass: boolean; +} + export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder { static ConfigOrder = 100; static RenderOrder = 100; @@ -413,16 +414,17 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder } configCamera( camera: Readonly, + pipelineConfigs: Readonly, cameraConfigs: CameraConfigs & ForwardPassConfigs): void { // Shadow - cameraConfigs.enableMainLightShadowMap = pplConfigs.shadowEnabled - && !pplConfigs.usePlanarShadow + cameraConfigs.enableMainLightShadowMap = pipelineConfigs.shadowEnabled + && !pipelineConfigs.usePlanarShadow && !!camera.scene && !!camera.scene.mainLight && camera.scene.mainLight.shadowEnabled; - cameraConfigs.enableMainLightPlanarShadowMap = pplConfigs.shadowEnabled - && pplConfigs.usePlanarShadow + cameraConfigs.enableMainLightPlanarShadowMap = pipelineConfigs.shadowEnabled + && pipelineConfigs.usePlanarShadow && !!camera.scene && !!camera.scene.mainLight && camera.scene.mainLight.shadowEnabled; @@ -435,16 +437,18 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder // MSAA cameraConfigs.enableMSAA = cameraConfigs.settings.msaa.enabled && !cameraConfigs.enableStoreSceneDepth // Cannot store MS depth, resolve depth is also not cross-platform - && !pplConfigs.isWeb // TODO(zhouzhenglong): remove this constraint - && !pplConfigs.isWebGL1; + && !pipelineConfigs.isWeb // TODO(zhouzhenglong): remove this constraint + && !pipelineConfigs.isWebGL1; // Forward rendering (Depend on MSAA and TBR) cameraConfigs.enableSingleForwardPass - = pplConfigs.isMobile || cameraConfigs.enableMSAA; + = pipelineConfigs.isMobile || cameraConfigs.enableMSAA; ++cameraConfigs.remainingPasses; } windowResize( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: Readonly, window: renderer.RenderWindow, camera: renderer.scene.Camera, @@ -512,6 +516,8 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder } } setup( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & ForwardPassConfigs, camera: renderer.scene.Camera, context: PipelineContext): rendering.BasicRenderPassBuilder | undefined { @@ -532,17 +538,18 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder // Main Directional light CSM Shadow Map if (cameraConfigs.enableMainLightShadowMap) { assert(!!mainLight); - this._addCascadedShadowMapPass(id, mainLight, camera); + this._addCascadedShadowMapPass(ppl, pplConfigs, id, mainLight, camera); } // Spot light shadow maps (Mobile or MSAA) if (cameraConfigs.enableSingleForwardPass) { // Currently, only support 1 spot light with shadow map on mobile platform. // TODO(zhouzhenglong): Relex this limitation. - this.forwardLighting.addSpotlightShadowPasses(camera, pplConfigs.mobileMaxSpotLightShadowMaps); + this.forwardLighting.addSpotlightShadowPasses( + ppl, camera, pplConfigs.mobileMaxSpotLightShadowMaps); } - this._tryAddReflectionProbePasses(cameraConfigs, id, mainLight, camera.scene); + this._tryAddReflectionProbePasses(ppl, cameraConfigs, id, mainLight, camera.scene); if (cameraConfigs.remainingPasses > 0 || cameraConfigs.enableShadingScale) { context.colorName = cameraConfigs.enableShadingScale @@ -557,7 +564,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder } const pass = this._addForwardRadiancePasses( - cameraConfigs, id, camera, + ppl, pplConfigs, cameraConfigs, id, camera, cameraConfigs.width, cameraConfigs.height, mainLight, context.colorName, context.depthStencilName, !cameraConfigs.enableMSAA, @@ -568,12 +575,14 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder } if (cameraConfigs.remainingPasses === 0 && cameraConfigs.enableShadingScale) { - return addCopyToScreenPass(cameraConfigs, context.colorName); + return addCopyToScreenPass(ppl, pplConfigs, cameraConfigs, context.colorName); } else { return pass; } } private _addCascadedShadowMapPass( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, id: number, light: renderer.scene.DirectionalLight, camera: renderer.scene.Camera, @@ -614,6 +623,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder } } private _tryAddReflectionProbePasses( + ppl: rendering.BasicPipeline, cameraConfigs: Readonly, id: number, mainLight: renderer.scene.DirectionalLight | null, @@ -738,6 +748,8 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder scene ? scene : undefined); } private _addForwardRadiancePasses( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: Readonly, id: number, camera: renderer.scene.Camera, @@ -778,10 +790,10 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder // Forward Lighting (Main Directional Light) // ---------------------------------------------------------------- const pass = cameraConfigs.enableSingleForwardPass - ? this._addForwardSingleRadiancePass(cameraConfigs, + ? this._addForwardSingleRadiancePass(ppl, pplConfigs, cameraConfigs, id, camera, enableMSAA, width, height, mainLight, colorName, depthStencilName, depthStencilStoreOp) - : this._addForwardMultipleRadiancePasses(cameraConfigs, + : this._addForwardMultipleRadiancePasses(ppl, cameraConfigs, id, camera, width, height, mainLight, colorName, depthStencilName, depthStencilStoreOp); @@ -807,6 +819,8 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder return pass; } private _addForwardSingleRadiancePass( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: Readonly, id: number, camera: renderer.scene.Camera, @@ -857,6 +871,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder return pass; } private _addForwardMultipleRadiancePasses( + ppl: rendering.BasicPipeline, cameraConfigs: Readonly, id: number, camera: renderer.scene.Camera, @@ -883,7 +898,7 @@ export class BuiltinForwardPassBuilder implements rendering.PipelinePassBuilder // Forward Lighting (Additive Lights) pass = this.forwardLighting .addLightPasses(colorName, depthStencilName, depthStencilStoreOp, - id, width, height, camera, this._viewport, pass); + id, width, height, camera, this._viewport, ppl, pass); return pass; } @@ -971,7 +986,11 @@ export interface BloomPassConfigs { enableBloom: boolean; } -export interface RenderTextureDesc { +function downSize(size: number, scale: number): number { + return Math.max(Math.floor(size * scale), 1); +} + +interface RenderTextureDesc { name: string; width: number; height: number; @@ -986,77 +1005,80 @@ export class BuiltinBloomPassBuilder implements rendering.PipelinePassBuilder { } configCamera( camera: Readonly, - config: CameraConfigs & BloomPassConfigs): void { - const { bloom } = config.settings; + pipelineConfigs: Readonly, + cameraConfigs: CameraConfigs & BloomPassConfigs): void { + const { bloom } = cameraConfigs.settings; const hasValidMaterial = ( bloom.type === BloomType.KawaseDualFilter && !!bloom.kawaseFilterMaterial || - bloom.type === BloomType.MipmapFilter && !!bloom.mipmapFilterMaterial + bloom.type === BloomType.MipmapFilter && !!bloom.mipmapFilterMaterial ); - config.enableBloom = bloom.enabled && hasValidMaterial; + cameraConfigs.enableBloom = bloom.enabled && hasValidMaterial; - if (config.enableBloom) { config.remainingPasses++; } + if (cameraConfigs.enableBloom) { + ++cameraConfigs.remainingPasses; + } } + windowResize( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, + cameraConfigs: CameraConfigs & BloomPassConfigs, + window: renderer.RenderWindow): void { + if (!cameraConfigs.enableBloom) { + return; + } - windowResize(config: CameraConfigs & BloomPassConfigs, window: renderer.RenderWindow): void { - if (!config.enableBloom) { return; } - - const { width, height, settings: { bloom } } = config; + const { width, height, settings: { bloom } } = cameraConfigs; const id = window.renderWindowId; - const format = config.radianceFormat; - this.initBloomDimensions(width, height, bloom.iterations); - this.createRenderTargets(id, format, width, height, bloom.iterations); - } + const format = cameraConfigs.radianceFormat; - private initBloomDimensions(baseWidth: number, baseHeight: number, iterations: number): void { - this._bloomWidths.length = 0; - this._bloomHeights.length = 0; - - let [w, h] = [baseWidth, baseHeight]; - for (let i = 0; i <= iterations; i++) { - w = Math.max(Math.floor(w / 2), 1); - h = Math.max(Math.floor(h / 2), 1); - this._bloomWidths.push(w); - this._bloomHeights.push(h); + if (bloom.type === BloomType.KawaseDualFilter) { + let bloomWidth = cameraConfigs.width; + let bloomHeight = cameraConfigs.height; + for (let i = 0; i !== bloom.iterations + 1; ++i) { + bloomWidth = Math.max(Math.floor(bloomWidth / 2), 1); + bloomHeight = Math.max(Math.floor(bloomHeight / 2), 1); + ppl.addRenderTarget(`BloomTex${id}_${i}`, format, bloomWidth, bloomHeight); + } + } else if (bloom.type === BloomType.MipmapFilter) { + const iterations = bloom.iterations; + for (let i = 0; i !== iterations + 1; ++i) { + // DownSample + if (i < iterations) { + const scale = Math.pow(0.5, i + 2); + this._bloomDownSampleTexDescs[i] = this.createTexture( + ppl, + `DownSampleColor${id}${i}`, + downSize(width, scale), + downSize(height, scale), + format); + } + // UpSample + if (i < iterations - 1) { + const scale = Math.pow(0.5, iterations - i - 1); + this._bloomUpSampleTexDescs[i] = this.createTexture( + ppl, + `UpSampleColor${id}${i}`, + downSize(width, scale), + downSize(height, scale), + format); + } + } + this._originalColorDesc = this.createTexture(ppl, `OriginalColor${id}`, width, height, format); + this._prefilterTexDesc = this.createTexture(ppl, `PrefilterColor${id}`, + downSize(width, 0.5), downSize(height, 0.5), format); } } - - private createRenderTargets( - id: number, - format: number, - width: number, - height: number, - iterations: number - ): void { - for (let i = 0; i < this._bloomWidths.length; i++) { - // BloomTex - this._bloomTexDescs[i] = this.createTexture(`BloomTex${id}_${i}`, this._bloomWidths[i], this._bloomHeights[i], format); - // DownSample - if (i < iterations) { - const scale = Math.pow(0.5, i + 2); - this._bloomDownSampleTexDescs[i] = this.createTexture(`DownSampleColor${id}${i}`, width * scale, height * scale, format); - } - // UpSample - if (i < iterations - 1) { - const scale = Math.pow(0.5, iterations - i - 1); - this._bloomUpSampleTexDescs[i] = this.createTexture(`UpSampleColor${id}${i}`, width * scale, height * scale, format); - } - } - - this._originalColorDesc = this.createTexture(`OriginalColor${id}`, width, height, format); - this._prefilterTexDesc = this.createTexture(`PrefilterColor${id}`, width * 0.5, height * 0.5, format); - } - - private createTexture(name: string, width: number, height: number, format: number): RenderTextureDesc { - const desc = { - name, - width: Math.floor(width), - height: Math.floor(height), - }; + private createTexture( + ppl: rendering.BasicPipeline, + name: string, width: number, height: number, format: number): RenderTextureDesc { + const desc = { name, width, height }; ppl.addRenderTarget(desc.name, format, desc.width, desc.height); return desc; } setup( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & BloomPassConfigs, camera: renderer.scene.Camera, context: PipelineContext, @@ -1065,28 +1087,160 @@ export class BuiltinBloomPassBuilder implements rendering.PipelinePassBuilder { if (!cameraConfigs.enableBloom) { return prevRenderPass; } - const bloom = cameraConfigs.settings.bloom; + --cameraConfigs.remainingPasses; assert(cameraConfigs.remainingPasses >= 0); - const id = camera.window.renderWindowId; - const bloomType = bloom.type; - let currMat = bloom.kawaseFilterMaterial; - let bloomFunc = this._addKawaseDualFilterBloomPasses; - if (bloomType === BloomType.MipmapFilter) { - currMat = bloom.mipmapFilterMaterial; - bloomFunc = this._addMipmapFilterBloomPasses; - } - return bloomFunc.call(this, - cameraConfigs, - cameraConfigs.settings, - currMat, - id, - cameraConfigs.width, - cameraConfigs.height, - context.colorName); - } + const bloom = cameraConfigs.settings.bloom; + const id = camera.window.renderWindowId; + + switch (bloom.type) { + case BloomType.KawaseDualFilter: { + const material = bloom.kawaseFilterMaterial; + assert(!!material); + return this._addKawaseDualFilterBloomPasses( + ppl, pplConfigs, + cameraConfigs, + cameraConfigs.settings, + material, + id, + cameraConfigs.width, + cameraConfigs.height, + context.colorName); + } + case BloomType.MipmapFilter: { + const material = bloom.mipmapFilterMaterial; + assert(!!material); + return this._addMipmapFilterBloomPasses( + ppl, pplConfigs, + cameraConfigs, + cameraConfigs.settings, + material, + id, + cameraConfigs.width, + cameraConfigs.height, + context.colorName); + } + default: + return prevRenderPass; + } + } + private _addKawaseDualFilterBloomPasses( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, + cameraConfigs: CameraConfigs & Readonly, + settings: PipelineSettings, + bloomMaterial: Material, + id: number, + width: number, + height: number, + radianceName: string, + ): rendering.BasicRenderPassBuilder { + const QueueHint = rendering.QueueHint; + // Based on Kawase Dual Filter Blur. Saves bandwidth on mobile devices. + // eslint-disable-next-line max-len + // https://community.arm.com/cfs-file/__key/communityserver-blogs-components-weblogfiles/00-00-00-20-66/siggraph2015_2D00_mmg_2D00_marius_2D00_slides.pdf + + // Size: [prefilter(1/2), downsample(1/4), downsample(1/8), downsample(1/16), ...] + const iterations = settings.bloom.iterations; + const sizeCount = iterations + 1; + this._bloomWidths.length = sizeCount; + this._bloomHeights.length = sizeCount; + this._bloomWidths[0] = Math.max(Math.floor(width / 2), 1); + this._bloomHeights[0] = Math.max(Math.floor(height / 2), 1); + for (let i = 1; i !== sizeCount; ++i) { + this._bloomWidths[i] = Math.max(Math.floor(this._bloomWidths[i - 1] / 2), 1); + this._bloomHeights[i] = Math.max(Math.floor(this._bloomHeights[i - 1] / 2), 1); + } + + // Bloom texture names + this._bloomTexNames.length = sizeCount; + for (let i = 0; i !== sizeCount; ++i) { + this._bloomTexNames[i] = `BloomTex${id}_${i}`; + } + + // Setup bloom parameters + this._bloomParams.x = pplConfigs.useFloatOutput ? 1 : 0; + this._bloomParams.y = 0; // unused + this._bloomParams.z = settings.bloom.threshold; + this._bloomParams.w = settings.bloom.enableAlphaMask ? 1 : 0; + + // Prefilter pass + const prefilterPass = ppl.addRenderPass(this._bloomWidths[0], this._bloomHeights[0], 'cc-bloom-prefilter'); + prefilterPass.addRenderTarget( + this._bloomTexNames[0], + LoadOp.CLEAR, + StoreOp.STORE, + this._clearColorTransparentBlack, + ); + prefilterPass.addTexture(radianceName, 'inputTexture'); + prefilterPass.setVec4('bloomParams', this._bloomParams); + prefilterPass + .addQueue(QueueHint.OPAQUE) + .addFullscreenQuad(bloomMaterial, 0); + + // Downsample passes + for (let i = 1; i !== sizeCount; ++i) { + const downPass = ppl.addRenderPass(this._bloomWidths[i], this._bloomHeights[i], 'cc-bloom-downsample'); + downPass.addRenderTarget(this._bloomTexNames[i], LoadOp.CLEAR, StoreOp.STORE, this._clearColorTransparentBlack); + downPass.addTexture(this._bloomTexNames[i - 1], 'bloomTexture'); + this._bloomTexSize.x = this._bloomWidths[i - 1]; + this._bloomTexSize.y = this._bloomHeights[i - 1]; + downPass.setVec4('bloomTexSize', this._bloomTexSize); + downPass + .addQueue(QueueHint.OPAQUE) + .addFullscreenQuad(bloomMaterial, 1); + } + + // Upsample passes + for (let i = iterations; i-- > 0;) { + const upPass = ppl.addRenderPass(this._bloomWidths[i], this._bloomHeights[i], 'cc-bloom-upsample'); + upPass.addRenderTarget(this._bloomTexNames[i], LoadOp.CLEAR, StoreOp.STORE, this._clearColorTransparentBlack); + upPass.addTexture(this._bloomTexNames[i + 1], 'bloomTexture'); + this._bloomTexSize.x = this._bloomWidths[i + 1]; + this._bloomTexSize.y = this._bloomHeights[i + 1]; + upPass.setVec4('bloomTexSize', this._bloomTexSize); + upPass + .addQueue(QueueHint.OPAQUE) + .addFullscreenQuad(bloomMaterial, 2); + } + + // Combine pass + this._bloomParams.w = settings.bloom.intensity; + const combinePass = ppl.addRenderPass(width, height, 'cc-bloom-combine'); + combinePass.addRenderTarget(radianceName, LoadOp.LOAD, StoreOp.STORE); + combinePass.addTexture(this._bloomTexNames[0], 'bloomTexture'); + combinePass.setVec4('bloomParams', this._bloomParams); + combinePass + .addQueue(QueueHint.BLEND) + .addFullscreenQuad(bloomMaterial, 3); + + if (cameraConfigs.remainingPasses === 0) { + return addCopyToScreenPass(ppl, pplConfigs, cameraConfigs, radianceName); + } else { + return combinePass; + } + } + private _addPass( + ppl: rendering.BasicPipeline, + width: number, + height: number, + layout: string, + colorName: string, + material: Material, + passIndex: number, + loadOp: gfx.LoadOp = LoadOp.CLEAR, + clearColor: gfx.Color = sClearColorTransparentBlack, + queueHint: rendering.QueueHint = rendering.QueueHint.OPAQUE): rendering.BasicRenderPassBuilder { + const pass = ppl.addRenderPass(width, height, layout); + pass.addRenderTarget(colorName, loadOp, StoreOp.STORE, clearColor); + pass.addQueue(queueHint) + .addFullscreenQuad(material, passIndex); + return pass; + } private _addMipmapFilterBloomPasses( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & Readonly, settings: PipelineSettings, bloomMaterial: Material, @@ -1101,18 +1255,19 @@ export class BuiltinBloomPassBuilder implements rendering.PipelinePassBuilder { this._bloomParams.z = settings.bloom.threshold; this._bloomParams.w = settings.bloom.intensity; const prefilterInfo = this._prefilterTexDesc; - ppl.addCopyPass([{ source: radianceName, target: this._originalColorDesc.name }]); + // Prefilter pass - let currSamplePass = buildScreenQuadPass( + let currSamplePass = this._addPass( + ppl, prefilterInfo.width, prefilterInfo.height, - 'cc-bloom-prefilter', + 'cc-bloom-mipmap-prefilter', prefilterInfo.name, bloomMaterial, 0, ); currSamplePass.addTexture(radianceName, 'mainTexture'); - currSamplePass.setVec4('cc_debug_view_mode', this._bloomParams); + currSamplePass.setVec4('bloomParams', this._bloomParams); const downSampleInfos = this._bloomDownSampleTexDescs; // Downsample passes @@ -1122,16 +1277,17 @@ export class BuiltinBloomPassBuilder implements rendering.PipelinePassBuilder { const samplerSrcName = samplerSrc.name; this._bloomTexSize.x = 1 / samplerSrc.width; this._bloomTexSize.y = 1 / samplerSrc.height; - currSamplePass = buildScreenQuadPass( + currSamplePass = this._addPass( + ppl, currInfo.width, currInfo.height, - 'cc-bloom-downsample', + 'cc-bloom-mipmap-downsample', currInfo.name, bloomMaterial, 1, ); currSamplePass.addTexture(samplerSrcName, 'mainTexture'); - currSamplePass.setVec4('cc_debug_view_mode', this._bloomTexSize); + currSamplePass.setVec4('bloomParams', this._bloomTexSize); } const lastIndex = downSampleInfos.length - 1; const upSampleInfos = this._bloomUpSampleTexDescs; @@ -1142,132 +1298,53 @@ export class BuiltinBloomPassBuilder implements rendering.PipelinePassBuilder { const sampleSrcName = sampleSrc.name; this._bloomTexSize.x = 1 / sampleSrc.width; this._bloomTexSize.y = 1 / sampleSrc.height; - currSamplePass = buildScreenQuadPass( - currInfo.width, - currInfo.height, - 'cc-bloom-upsample', - currInfo.name, - bloomMaterial, - 2, - ); - currSamplePass.addTexture(sampleSrcName, 'mainTexture'); - currSamplePass.addTexture(downSampleInfos[lastIndex - 1 - i].name, 'downsampleTexture'); - currSamplePass.setVec4('cc_debug_view_mode', this._bloomTexSize); - } - - // Combine pass - const combinePass = buildScreenQuadPass( - width, - height, - 'cc-bloom-combine', - radianceName, - bloomMaterial, - 3, - ); - combinePass.addTexture(this._originalColorDesc.name, 'mainTexture'); - combinePass.addTexture(upSampleInfos[upSampleInfos.length - 1].name, 'bloomTexture'); - combinePass.setVec4('cc_debug_view_mode', this._bloomParams); - if (cameraConfigs.remainingPasses === 0) { - return addCopyToScreenPass(cameraConfigs, radianceName); - } else { - return combinePass; - } - } - - private _addKawaseDualFilterBloomPasses( - cameraConfigs: CameraConfigs & Readonly, - settings: PipelineSettings, - bloomMaterial: Material, - id: number, - width: number, - height: number, - radianceName: string, - ): rendering.BasicRenderPassBuilder { - // Based on Kawase Dual Filter Blur. Saves bandwidth on mobile devices. - // eslint-disable-next-line max-len - // https://community.arm.com/cfs-file/__key/communityserver-blogs-components-weblogfiles/00-00-00-20-66/siggraph2015_2D00_mmg_2D00_marius_2D00_slides.pdf - - // Size: [prefilter(1/2), downsample(1/4), downsample(1/8), downsample(1/16), ...] - const iterations = settings.bloom.iterations; - const sizeCount = iterations + 1; - // Setup bloom parameters - this._bloomParams.x = pplConfigs.useFloatOutput ? 1 : 0; - this._bloomParams.x = 0; // unused - this._bloomParams.z = settings.bloom.threshold; - this._bloomParams.w = settings.bloom.enableAlphaMask ? 1 : 0; - - // Prefilter pass - let currSamplePass = buildScreenQuadPass( - this._bloomWidths[0], - this._bloomHeights[0], - 'cc-bloom-prefilter', - this._bloomTexDescs[0].name, - bloomMaterial, - 0, - ); - currSamplePass.addTexture(radianceName, 'inputTexture'); - currSamplePass.setVec4('bloomParams', this._bloomParams); - // Downsample passes - for (let i = 1; i !== sizeCount; ++i) { - this._bloomTexSize.x = this._bloomWidths[i - 1]; - this._bloomTexSize.y = this._bloomHeights[i - 1]; - currSamplePass = buildScreenQuadPass( - this._bloomWidths[i], - this._bloomHeights[i], - 'cc-bloom-downsample', - this._bloomTexDescs[i].name, - bloomMaterial, - 1, - ); - currSamplePass.addTexture(this._bloomTexDescs[i - 1].name, 'bloomTexture'); - currSamplePass.setVec4('bloomTexSize', this._bloomTexSize); - } - - // Upsample passes - for (let i = iterations; i-- > 0;) { - this._bloomTexSize.x = this._bloomWidths[i + 1]; - this._bloomTexSize.y = this._bloomHeights[i + 1]; - currSamplePass = buildScreenQuadPass( - this._bloomWidths[i], - this._bloomHeights[i], - 'cc-bloom-upsample', - this._bloomTexDescs[i].name, + currSamplePass = this._addPass( + ppl, + currInfo.width, + currInfo.height, + 'cc-bloom-mipmap-upsample', + currInfo.name, bloomMaterial, 2, ); - currSamplePass.addTexture(this._bloomTexDescs[i + 1].name, 'bloomTexture'); - currSamplePass.setVec4('bloomTexSize', this._bloomTexSize); + currSamplePass.addTexture(sampleSrcName, 'mainTexture'); + currSamplePass.addTexture(downSampleInfos[lastIndex - 1 - i].name, 'downsampleTexture'); + currSamplePass.setVec4('bloomParams', this._bloomTexSize); } - this._bloomParams.w = settings.bloom.intensity; + // Combine pass - const combinePass = buildScreenQuadPass( + const combinePass = this._addPass( + ppl, width, height, - 'cc-bloom-combine', + 'cc-bloom-mipmap-combine', radianceName, bloomMaterial, 3, - LoadOp.LOAD + LoadOp.LOAD, ); - combinePass.addTexture(this._bloomTexDescs[0].name, 'bloomTexture'); + combinePass.addTexture(upSampleInfos[upSampleInfos.length - 1].name, 'bloomTexture'); combinePass.setVec4('bloomParams', this._bloomParams); - if (cameraConfigs.remainingPasses === 0) { - return addCopyToScreenPass(cameraConfigs, radianceName); + return addCopyToScreenPass(ppl, pplConfigs, cameraConfigs, radianceName); } else { return combinePass; } } + // Bloom + private readonly _clearColorTransparentBlack = new Color(0, 0, 0, 0); private readonly _bloomParams = new Vec4(0, 0, 0, 0); private readonly _bloomTexSize = new Vec4(0, 0, 0, 0); private readonly _bloomWidths: Array = []; private readonly _bloomHeights: Array = []; - private readonly _bloomTexDescs: Array = []; + private readonly _bloomTexNames: Array = []; + + // Mipmap Bloom private readonly _bloomUpSampleTexDescs: Array = []; private readonly _bloomDownSampleTexDescs: Array = []; - private _prefilterTexDesc: RenderTextureDesc; - private _originalColorDesc: RenderTextureDesc; + private _prefilterTexDesc: RenderTextureDesc = { name: '', width: 0, height: 0 }; + private _originalColorDesc: RenderTextureDesc = { name: '', width: 0, height: 0 }; } export interface ToneMappingPassConfigs { @@ -1284,6 +1361,7 @@ export class BuiltinToneMappingPassBuilder implements rendering.PipelinePassBuil } configCamera( camera: Readonly, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & ToneMappingPassConfigs): void { const settings = cameraConfigs.settings; @@ -1301,6 +1379,8 @@ export class BuiltinToneMappingPassBuilder implements rendering.PipelinePassBuil } } windowResize( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & ToneMappingPassConfigs): void { if (cameraConfigs.enableColorGrading) { assert(!!cameraConfigs.settings.colorGrading.material); @@ -1310,6 +1390,8 @@ export class BuiltinToneMappingPassBuilder implements rendering.PipelinePassBuil } } setup( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & ToneMappingPassConfigs, camera: renderer.scene.Camera, context: PipelineContext, @@ -1322,7 +1404,7 @@ export class BuiltinToneMappingPassBuilder implements rendering.PipelinePassBuil --cameraConfigs.remainingPasses; assert(cameraConfigs.remainingPasses >= 0); if (cameraConfigs.remainingPasses === 0) { - return this._addCopyAndTonemapPass(cameraConfigs, + return this._addCopyAndTonemapPass(ppl, pplConfigs, cameraConfigs, cameraConfigs.width, cameraConfigs.height, context.colorName, cameraConfigs.colorName); } else { @@ -1335,12 +1417,14 @@ export class BuiltinToneMappingPassBuilder implements rendering.PipelinePassBuil const radianceName = context.colorName; context.colorName = ldrColorName; - return this._addCopyAndTonemapPass(cameraConfigs, + return this._addCopyAndTonemapPass(ppl, pplConfigs, cameraConfigs, cameraConfigs.width, cameraConfigs.height, radianceName, ldrColorName); } } private _addCopyAndTonemapPass( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & ToneMappingPassConfigs, width: number, height: number, @@ -1358,29 +1442,29 @@ export class BuiltinToneMappingPassBuilder implements rendering.PipelinePassBuil this._colorGradingTexSize.y = lutTex.height; const isSquareMap = lutTex.width === lutTex.height; - pass = buildScreenQuadPass( - width, - height, - isSquareMap ? 'cc-color-grading-8x8' : 'cc-color-grading-nx1', - colorName, - settings.colorGrading.material, - isSquareMap ? 1 : 0, - ); + if (isSquareMap) { + pass = ppl.addRenderPass(width, height, 'cc-color-grading-8x8'); + } else { + pass = ppl.addRenderPass(width, height, 'cc-color-grading-nx1'); + } + pass.addRenderTarget(colorName, LoadOp.CLEAR, StoreOp.STORE, sClearColorTransparentBlack); pass.addTexture(radianceName, 'sceneColorMap'); pass.setVec2('lutTextureSize', this._colorGradingTexSize); pass.setFloat('contribute', settings.colorGrading.contribute); + pass.addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(settings.colorGrading.material, isSquareMap ? 1 : 0); } else { - pass = buildScreenQuadPass( - width, - height, - 'cc-tone-mapping', - colorName, - settings.toneMapping.material - ? settings.toneMapping.material - : cameraConfigs.copyAndTonemapMaterial!, - 0, - ); + pass = ppl.addRenderPass(width, height, 'cc-tone-mapping'); + pass.addRenderTarget(colorName, LoadOp.CLEAR, StoreOp.STORE, sClearColorTransparentBlack); pass.addTexture(radianceName, 'inputTexture'); + if (settings.toneMapping.material) { + pass.addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(settings.toneMapping.material, 0); + } else { + assert(!!cameraConfigs.copyAndTonemapMaterial); + pass.addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(cameraConfigs.copyAndTonemapMaterial, 0); + } } return pass; } @@ -1400,6 +1484,7 @@ export class BuiltinFXAAPassBuilder implements rendering.PipelinePassBuilder { } configCamera( camera: Readonly, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & FXAAPassConfigs): void { cameraConfigs.enableFXAA = cameraConfigs.settings.fxaa.enabled @@ -1409,6 +1494,8 @@ export class BuiltinFXAAPassBuilder implements rendering.PipelinePassBuilder { } } setup( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & FXAAPassConfigs, camera: renderer.scene.Camera, context: PipelineContext, @@ -1429,17 +1516,17 @@ export class BuiltinFXAAPassBuilder implements rendering.PipelinePassBuilder { assert(!!cameraConfigs.settings.fxaa.material); if (cameraConfigs.remainingPasses === 0) { if (cameraConfigs.enableShadingScale) { - this._addFxaaPass( + this._addFxaaPass(ppl, pplConfigs, cameraConfigs.settings.fxaa.material, cameraConfigs.width, cameraConfigs.height, context.colorName, ldrColorName); - return addCopyToScreenPass(cameraConfigs, ldrColorName); + return addCopyToScreenPass(ppl, pplConfigs, cameraConfigs, ldrColorName); } else { assert(cameraConfigs.width === cameraConfigs.nativeWidth); assert(cameraConfigs.height === cameraConfigs.nativeHeight); - return this._addFxaaPass( + return this._addFxaaPass(ppl, pplConfigs, cameraConfigs.settings.fxaa.material, cameraConfigs.width, cameraConfigs.height, @@ -1449,7 +1536,7 @@ export class BuiltinFXAAPassBuilder implements rendering.PipelinePassBuilder { } else { const inputColorName = context.colorName; context.colorName = ldrColorName; - const lastPass = this._addFxaaPass( + const lastPass = this._addFxaaPass(ppl, pplConfigs, cameraConfigs.settings.fxaa.material, cameraConfigs.width, cameraConfigs.height, @@ -1459,6 +1546,8 @@ export class BuiltinFXAAPassBuilder implements rendering.PipelinePassBuilder { } } private _addFxaaPass( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, fxaaMaterial: Material, width: number, height: number, @@ -1469,16 +1558,13 @@ export class BuiltinFXAAPassBuilder implements rendering.PipelinePassBuilder { this._fxaaParams.y = height; this._fxaaParams.z = 1 / width; this._fxaaParams.w = 1 / height; - const pass = buildScreenQuadPass( - width, - height, - 'cc-fxaa', - colorName, - fxaaMaterial, - 0, - ); + + const pass = ppl.addRenderPass(width, height, 'cc-fxaa'); + pass.addRenderTarget(colorName, LoadOp.CLEAR, StoreOp.STORE, sClearColorTransparentBlack); pass.addTexture(ldrColorName, 'sceneColorMap'); pass.setVec4('texSize', this._fxaaParams); + pass.addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(fxaaMaterial, 0); return pass; } // FXAA @@ -1498,6 +1584,7 @@ export class BuiltinFsrPassBuilder implements rendering.PipelinePassBuilder { } configCamera( camera: Readonly, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & FSRPassConfigs): void { // FSR (Depend on Shading scale) cameraConfigs.enableFSR = cameraConfigs.settings.fsr.enabled @@ -1510,6 +1597,8 @@ export class BuiltinFsrPassBuilder implements rendering.PipelinePassBuilder { } } setup( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & FSRPassConfigs, camera: renderer.scene.Camera, context: PipelineContext, @@ -1528,7 +1617,7 @@ export class BuiltinFsrPassBuilder implements rendering.PipelinePassBuilder { context.colorName = outputColorName; assert(!!cameraConfigs.settings.fsr.material); - return this._addFsrPass(cameraConfigs, + return this._addFsrPass(ppl, pplConfigs, cameraConfigs, cameraConfigs.settings, cameraConfigs.settings.fsr.material, cameraConfigs.renderWindowId, @@ -1540,6 +1629,8 @@ export class BuiltinFsrPassBuilder implements rendering.PipelinePassBuilder { outputColorName); } private _addFsrPass( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & FSRPassConfigs, settings: PipelineSettings, fsrMaterial: Material, @@ -1560,30 +1651,25 @@ export class BuiltinFsrPassBuilder implements rendering.PipelinePassBuilder { const uiColorPrefix = 'UiColor'; const fsrColorName = getPingPongRenderTarget(outputColorName, uiColorPrefix, id); - // EASU pass - let currPass = buildScreenQuadPass( - nativeWidth, - nativeHeight, - 'cc-fsr-easu', - fsrColorName, - fsrMaterial, - 0, - ); - currPass.addTexture(inputColorName, 'outputResultMap'); - currPass.setVec4('fsrTexSize', this._fsrTexSize); - // RCAS pass - currPass = buildScreenQuadPass( - nativeWidth, - nativeHeight, - 'cc-fsr-rcas', - outputColorName, - fsrMaterial, - 1, - ); - currPass.addTexture(fsrColorName, 'outputResultMap'); - currPass.setVec4('fsrTexSize', this._fsrTexSize); - currPass.setVec4('fsrParams', this._fsrParams); - return currPass; + + const easuPass = ppl.addRenderPass(nativeWidth, nativeHeight, 'cc-fsr-easu'); + easuPass.addRenderTarget(fsrColorName, LoadOp.CLEAR, StoreOp.STORE, sClearColorTransparentBlack); + easuPass.addTexture(inputColorName, 'outputResultMap'); + easuPass.setVec4('fsrTexSize', this._fsrTexSize); + easuPass + .addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(fsrMaterial, 0); + + const rcasPass = ppl.addRenderPass(nativeWidth, nativeHeight, 'cc-fsr-rcas'); + rcasPass.addRenderTarget(outputColorName, LoadOp.CLEAR, StoreOp.STORE, sClearColorTransparentBlack); + rcasPass.addTexture(fsrColorName, 'outputResultMap'); + rcasPass.setVec4('fsrTexSize', this._fsrTexSize); + rcasPass.setVec4('fsrParams', this._fsrParams); + rcasPass + .addQueue(rendering.QueueHint.OPAQUE) + .addFullscreenQuad(fsrMaterial, 1); + + return rcasPass; } // FSR private readonly _fsrParams = new Vec4(0, 0, 0, 0); @@ -1598,19 +1684,24 @@ export class BuiltinUiPassBuilder implements rendering.PipelinePassBuilder { return 1000; } setup( + ppl: rendering.BasicPipeline, + pplConfigs: Readonly, cameraConfigs: CameraConfigs & FSRPassConfigs, camera: renderer.scene.Camera, context: PipelineContext, prevRenderPass?: rendering.BasicRenderPassBuilder) : rendering.BasicRenderPassBuilder | undefined { assert(!!prevRenderPass); - const queue = prevRenderPass - .addQueue(rendering.QueueHint.BLEND, 'default', 'default'); - queue.addDraw2D(camera); + + let flags = rendering.SceneFlags.UI; if (cameraConfigs.enableProfiler) { + flags |= rendering.SceneFlags.PROFILER; prevRenderPass.showStatistics = true; - queue.addProfiler(camera); } + prevRenderPass + .addQueue(rendering.QueueHint.BLEND, 'default', 'default') + .addScene(camera, flags); + return prevRenderPass; } } @@ -1693,7 +1784,9 @@ if (rendering) { } private _setupBuiltinCameraConfigs( + ppl: rendering.BasicPipeline, camera: renderer.scene.Camera, + pipelineConfigs: PipelineConfigs, cameraConfigs: CameraConfigs ) { const window = camera.window; @@ -1730,7 +1823,7 @@ if (rendering) { // Radiance cameraConfigs.enableHDR = cameraConfigs.enableFullPipeline - && ppl.useFloatOutput; + && pipelineConfigs.useFloatOutput; cameraConfigs.radianceFormat = cameraConfigs.enableHDR ? gfx.Format.RGBA16F : gfx.Format.RGBA8; @@ -1742,7 +1835,9 @@ if (rendering) { } private _setupCameraConfigs( + ppl: rendering.BasicPipeline, camera: renderer.scene.Camera, + pipelineConfigs: PipelineConfigs, cameraConfigs: CameraConfigs ): void { this._setupPipelinePreview(camera, cameraConfigs); @@ -1751,11 +1846,11 @@ if (rendering) { sortPipelinePassBuildersByConfigOrder(this._passBuilders); - this._setupBuiltinCameraConfigs(camera, cameraConfigs); + this._setupBuiltinCameraConfigs(ppl, camera, pipelineConfigs, cameraConfigs); for (const builder of this._passBuilders) { if (builder.configCamera) { - builder.configCamera(camera, cameraConfigs); + builder.configCamera(camera, pipelineConfigs, cameraConfigs); } } } @@ -1764,17 +1859,15 @@ if (rendering) { // Interface // ---------------------------------------------------------------- windowResize( - pipeline: rendering.BasicPipeline, + ppl: rendering.BasicPipeline, window: renderer.RenderWindow, camera: renderer.scene.Camera, nativeWidth: number, nativeHeight: number, ): void { - ppl = pipeline; - pplConfigs = this._configs; - setupPipelineConfigs(this._configs); + setupPipelineConfigs(ppl, this._configs); - this._setupCameraConfigs(camera, this._cameraConfigs); + this._setupCameraConfigs(ppl, camera, this._configs, this._cameraConfigs); // Render Window (UI) const id = window.renderWindowId; @@ -1804,14 +1897,13 @@ if (rendering) { for (const builder of this._passBuilders) { if (builder.windowResize) { - builder.windowResize(this._cameraConfigs, window, camera, nativeWidth, nativeHeight); + builder.windowResize(ppl, this._configs, this._cameraConfigs, window, camera, nativeWidth, nativeHeight); } } } - setup(cameras: renderer.scene.Camera[], pipeline: rendering.BasicPipeline): void { - ppl = pipeline; + setup(cameras: renderer.scene.Camera[], ppl: rendering.BasicPipeline): void { // TODO(zhouzhenglong): Make default effect asset loading earlier and remove _initMaterials - if (this._initMaterials()) { + if (this._initMaterials(ppl)) { return; } @@ -1823,7 +1915,7 @@ if (rendering) { continue; } // Setup camera configs - this._setupCameraConfigs(camera, this._cameraConfigs); + this._setupCameraConfigs(ppl, camera, this._configs, this._cameraConfigs); // log(`Setup camera: ${camera.node!.name}, window: ${camera.window.renderWindowId}, isFull: ${this._cameraConfigs.enableFullPipeline}, ` // + `size: ${camera.window.width}x${camera.window.height}`); @@ -1831,9 +1923,9 @@ if (rendering) { // Build pipeline if (this._cameraConfigs.enableFullPipeline) { - this._buildForwardPipeline(camera, camera.scene, this._passBuilders); + this._buildForwardPipeline(ppl, camera, camera.scene, this._passBuilders); } else { - this._buildSimplePipeline(camera); + this._buildSimplePipeline(ppl, camera); } this._pipelineEvent.emit(PipelineEventType.RENDER_CAMERA_END, camera); @@ -1843,6 +1935,7 @@ if (rendering) { // Pipelines // ---------------------------------------------------------------- private _buildSimplePipeline( + ppl: rendering.BasicPipeline, camera: renderer.scene.Camera, ): void { const width = Math.max(Math.floor(camera.window.width), 1); @@ -1894,16 +1987,17 @@ if (rendering) { .addScene(camera, SceneFlags.OPAQUE); // The blend queue is used for UI and Gizmos - const queue = pass.addQueue(QueueHint.BLEND); - queue.addDraw2D(camera); + let flags = SceneFlags.BLEND | SceneFlags.UI; if (this._cameraConfigs.enableProfiler) { + flags |= SceneFlags.PROFILER; pass.showStatistics = true; - queue.addProfiler(camera); } - queue.addScene(camera, SceneFlags.BLEND); + pass.addQueue(QueueHint.BLEND) + .addScene(camera, flags); } private _buildForwardPipeline( + ppl: rendering.BasicPipeline, camera: renderer.scene.Camera, scene: renderer.RenderScene, passBuilders: rendering.PipelinePassBuilder[], @@ -1919,7 +2013,7 @@ if (rendering) { for (const builder of passBuilders) { if (builder.setup) { - lastPass = builder.setup(this._cameraConfigs, + lastPass = builder.setup(ppl, this._configs, this._cameraConfigs, camera, context, lastPass); } } @@ -1927,12 +2021,12 @@ if (rendering) { assert(this._cameraConfigs.remainingPasses === 0); } - private _initMaterials(): number { + private _initMaterials(ppl: rendering.BasicPipeline): number { if (this._initialized) { return 0; } - setupPipelineConfigs(this._configs); + setupPipelineConfigs(ppl, this._configs); // When add new effect asset, please add its uuid to the dependentAssets in cc.config.json. this._copyAndTonemapMaterial._uuid = `builtin-pipeline-tone-mapping-material`; diff --git a/editor/assets/effects/builtin-unlit.effect b/editor/assets/effects/builtin-unlit.effect index 0161d98452..4091c0ceeb 100644 --- a/editor/assets/effects/builtin-unlit.effect +++ b/editor/assets/effects/builtin-unlit.effect @@ -154,7 +154,11 @@ CCProgram unlit-vs %{ #endif CC_TRANSFER_FOG(matWorld * position); - return cc_matProj * (cc_matView * matWorld) * position; + #if CC_USE_2D + return cc_matProj * cc_matView * position; + #else + return cc_matProj * (cc_matView * matWorld) * position; + #endif } }% diff --git a/editor/assets/effects/legacy/standard.effect b/editor/assets/effects/legacy/standard.effect index 631ebac9d7..fad8a81364 100644 --- a/editor/assets/effects/legacy/standard.effect +++ b/editor/assets/effects/legacy/standard.effect @@ -221,8 +221,11 @@ CCProgram standard-vs %{ #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD CCLightingMapCaclUV(); #endif - - gl_Position = cc_matProj * (cc_matView * matWorld) * In.position; + #if CC_USE_2D + gl_Position = cc_matProj * cc_matView * In.position; + #else + gl_Position = cc_matProj * (cc_matView * matWorld) * In.position; + #endif } }% diff --git a/editor/assets/effects/legacy/toon.effect b/editor/assets/effects/legacy/toon.effect index af1f9cc92e..7564ebac71 100644 --- a/editor/assets/effects/legacy/toon.effect +++ b/editor/assets/effects/legacy/toon.effect @@ -171,8 +171,11 @@ CCProgram toon-vs %{ #endif CC_TRANSFER_SHADOW(pos); - - return cc_matProj * (cc_matView * matWorld) * In.position; + #if CC_USE_2D + return cc_matProj * cc_matView * In.position; + #else + return cc_matProj * (cc_matView * matWorld) * In.position; + #endif } }% diff --git a/editor/assets/effects/pipeline/post-process/bloom-mipmap-blur.effect b/editor/assets/effects/pipeline/post-process/bloom-mipmap-blur.effect index aa98b17383..79b30b484b 100644 --- a/editor/assets/effects/pipeline/post-process/bloom-mipmap-blur.effect +++ b/editor/assets/effects/pipeline/post-process/bloom-mipmap-blur.effect @@ -9,35 +9,42 @@ techniques: - passes: - vert: base-vs:vert frag: prefilter-fs:frag - pass: cc-bloom-prefilter + pass: cc-bloom-mipmap-prefilter depthStencilState: depthTest: false depthWrite: false - vert: downsample-vs:vert frag: downsample-fs:frag - pass: cc-bloom-downsample + pass: cc-bloom-mipmap-downsample depthStencilState: depthTest: false depthWrite: false - vert: upsample-vs:vert frag: upsample-fs:frag - pass: cc-bloom-upsample + pass: cc-bloom-mipmap-upsample depthStencilState: depthTest: false depthWrite: false - vert: base-vs:vert frag: combine-fs:frag - pass: cc-bloom-combine + pass: cc-bloom-mipmap-combine depthStencilState: depthTest: false depthWrite: false + blendState: + targets: + - blend: true + blendSrc: one + blendDst: one + blendSrcAlpha: zero + blendDstAlpha: one }% CCProgram base-vs %{ precision mediump float; -#include -#include +#include +#include out vec2 vUv; out vec2 vUv00; @@ -49,19 +56,21 @@ out vec2 vUv04; vec4 vert() { vUv = a_texCoord; - vec4 position; - CCDecode(position); - - return position; + StandardVertInput In; + CCDecode(In); + FLIP_VULKAN_NDC(In.position); + return In.position; } }% CCProgram prefilter-fs %{ precision mediump float; -#include -#define bloomParams cc_debug_view_mode +#pragma rate BloomUBO pass +uniform BloomUBO { + mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask +}; in vec2 vUv; #pragma rate mainTexture pass @@ -81,10 +90,13 @@ vec4 frag() { CCProgram downsample-vs %{ precision mediump float; -#include -#include +#include +#include -#define bloomParams cc_debug_view_mode +#pragma rate BloomUBO pass +uniform BloomUBO { + mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask +}; out vec2 vUv; out vec2 vUv00; @@ -118,19 +130,19 @@ vec4 vert() { vUv10 = vUv + bloomParams.xy * vec2(0.0, - 2.0); vUv11 = vUv + bloomParams.xy * vec2(2.0, - 2.0); - vec4 position; - CCDecode(position); - - return position; + StandardVertInput In; + CCDecode(In); + FLIP_VULKAN_NDC(In.position); + return In.position; } }% CCProgram downsample-fs %{ precision mediump float; -// (1 / 4) * 0.5 = 0.125 +// (1 / 4) * 0.5 = 0.125 #define WEIGHT_INNER 0.125 -// (1 / 9) * 0.5 = 0.0555555 +// (1 / 9) * 0.5 = 0.0555555 #define WEIGHT_OUTER 0.0555555 in vec2 vUv; @@ -201,10 +213,13 @@ vec4 frag() { CCProgram upsample-vs %{ precision mediump float; -#include -#include +#include +#include -#define bloomParams cc_debug_view_mode +#pragma rate BloomUBO pass +uniform BloomUBO { + mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask +}; out vec2 vUv; out vec2 vUv0; @@ -230,16 +245,15 @@ vec4 vert() { vUv6 = vUv + bloomParams.xy * vec2(0.0, - 1.0); vUv7 = vUv + bloomParams.xy * vec2(1.0, - 1.0); - vec4 position; - CCDecode(position); - - return position; + StandardVertInput In; + CCDecode(In); + FLIP_VULKAN_NDC(In.position); + return In.position; } }% CCProgram upsample-fs %{ precision mediump float; -#include in vec2 vUv; in vec2 vUv0; @@ -276,21 +290,20 @@ vec4 frag() { CCProgram combine-fs %{ precision mediump float; -#include -#define bloomParams cc_debug_view_mode +#pragma rate BloomUBO pass +uniform BloomUBO { + mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask +}; in vec2 vUv; #pragma rate bloomTexture pass -uniform lowp sampler2D bloomTexture; -#pragma rate mainTexture pass -uniform sampler2D mainTexture; +uniform sampler2D bloomTexture; vec4 frag() { - vec4 baseColor = texture(mainTexture, vUv); vec4 bloomColor = texture(bloomTexture, vUv); - vec3 color = baseColor.rgb + bloomColor.rgb * bloomParams.w; - return vec4(color, baseColor.a); + vec3 color = bloomColor.rgb * bloomParams.w; + return vec4(color, 1); } }% \ No newline at end of file diff --git a/editor/i18n/en/assets.js b/editor/i18n/en/assets.js index 65033f0e9e..b34fb90107 100644 --- a/editor/i18n/en/assets.js +++ b/editor/i18n/en/assets.js @@ -431,8 +431,8 @@ module.exports = { }, }, animationBakeRate: { - name: 'Animation Bake Rate', - title: 'Specify the animation bake rate in frames per second (fps).', + name: 'Animation Bake Sample Rate', + title: 'Specify the animation bake sample rate in frames per second (fps).', auto: 'Auto', }, promoteSingleRootNode: { diff --git a/editor/i18n/en/localization.js b/editor/i18n/en/localization.js index 133f5be1f2..023db8df0a 100755 --- a/editor/i18n/en/localization.js +++ b/editor/i18n/en/localization.js @@ -1361,6 +1361,7 @@ module.exports = link(mixin({ require('./modules/physics'), require('./modules/rendering'), require('./modules/ui.js'), +require('./modules/terrain.js'), require('./animation'), )); diff --git a/editor/i18n/en/modules/terrain.js b/editor/i18n/en/modules/terrain.js new file mode 100644 index 0000000000..9b4f3c9d24 --- /dev/null +++ b/editor/i18n/en/modules/terrain.js @@ -0,0 +1,34 @@ +module.exports = { + classes: { + cc: { + Terrain: { + properties: { + _asset: { + tooltip: 'The terrain asset.', + }, + effectAsset: { + tooltip: 'The terrain effect asset.', + }, + lightmapInfos: { + tooltip: 'The terrain info.', + }, + receiveShadow: { + tooltip: 'Receive shadow.', + }, + useNormalMap: { + tooltip: 'Use normal map.', + }, + usePBR: { + tooltip: 'Use pbr material.', + }, + lodEnable: { + tooltip: 'Enable lod.', + }, + LodBias: { + tooltip: 'Lod bias.', + }, + }, + }, + }, + }, +}; diff --git a/editor/i18n/zh/assets.js b/editor/i18n/zh/assets.js index ade9b30745..0533396c98 100644 --- a/editor/i18n/zh/assets.js +++ b/editor/i18n/zh/assets.js @@ -426,8 +426,8 @@ module.exports = { }, }, animationBakeRate: { - name: '动画烘焙速率', - title: '指定动画烘焙速率,单位为帧每秒(FPS)', + name: '动画烘焙采样率', + title: '指定动画烘焙每秒采集多少次数据,单位为帧每秒(FPS)', auto: '自动', }, promoteSingleRootNode: { diff --git a/editor/i18n/zh/localization.js b/editor/i18n/zh/localization.js index 83bfe99a7a..a777ee8b99 100755 --- a/editor/i18n/zh/localization.js +++ b/editor/i18n/zh/localization.js @@ -1339,6 +1339,7 @@ module.exports = link(mixin({ require('./modules/physics'), require('./modules/rendering'), require('./modules/ui.js'), +require('./modules/terrain.js'), require('./animation'), )); diff --git a/editor/i18n/zh/modules/terrain.js b/editor/i18n/zh/modules/terrain.js new file mode 100644 index 0000000000..7022d6ae03 --- /dev/null +++ b/editor/i18n/zh/modules/terrain.js @@ -0,0 +1,34 @@ +module.exports = { + classes: { + cc: { + Terrain: { + properties: { + _asset: { + tooltip: '地形所使用的资源。', + }, + effectAsset: { + tooltip: '地形特效资源。', + }, + lightmapInfos: { + tooltip: '地形信息。', + }, + receiveShadow: { + tooltip: '是否接受阴影。', + }, + useNormalMap: { + tooltip: '是否使用法线贴图。', + }, + usePBR: { + tooltip: '是否使用物理材质。', + }, + lodEnable: { + tooltip: '是否允许 lod。', + }, + LodBias: { + tooltip: 'Lod 偏移距离。', + }, + }, + }, + }, + }, +}; diff --git a/editor/inspector/assets/texture/texture.js b/editor/inspector/assets/texture/texture.js index 2d360e17bd..6c8c24b099 100644 --- a/editor/inspector/assets/texture/texture.js +++ b/editor/inspector/assets/texture/texture.js @@ -4,7 +4,7 @@ const { updateElementReadonly } = require('../../utils/assets'); const { extname } = require('path'); const { ParseAtlasFile } = require('./parse-atlas'); -exports.template = /* html */` +exports.template = /* html */ `
@@ -62,7 +62,7 @@ exports.template = /* html */`
`; -exports.style = /* css */` +exports.style = /* css */ ` .asset-texture { display: flex; flex: 1; @@ -395,9 +395,10 @@ const Elements = { panel.$.mipfilterSelect.innerHTML = optionsHtml; panel.$.mipfilterSelect.value = panel.userData.mipfilter || 'nearest'; - panel.metaList && panel.metaList.forEach((meta) => { - Editor.Profile.setTemp('inspector', `${meta.uuid}.texture.mipfilter`, panel.userData.mipfilter); - }); + panel.metaList && + panel.metaList.forEach((meta) => { + Editor.Profile.setTemp('inspector', `${meta.uuid}.texture.mipfilter`, panel.userData.mipfilter); + }); panel.updateInvalid(panel.$.mipfilterSelect, 'mipfilter'); updateElementReadonly.call(panel, panel.$.mipfilterSelect); }, @@ -589,9 +590,13 @@ const Elements = { }, checkAtlasFileConfig: { async ready() { - this.$.atlasFileName.addEventListener('click', () => { - Editor.Message.send('assets', 'twinkle', this.$.atlasFileName.getAttribute('data-uuid')); - }, false); + this.$.atlasFileName.addEventListener( + 'click', + () => { + Editor.Message.send('assets', 'twinkle', this.$.atlasFileName.getAttribute('data-uuid')); + }, + false, + ); }, async update() { try { @@ -600,17 +605,13 @@ const Elements = { return; } const parentPath = this.parentAssetList[0].path.replace(/\/[^/]+$/, '/*'); - let assets = await Editor.Message.request( - 'asset-db', - 'query-assets', - { pattern: parentPath, ccType: "cc.Asset" } - ); - assets = assets.filter(v => extname(v.file) === '.atlas'); + let assets = await Editor.Message.request('asset-db', 'query-assets', { pattern: parentPath, ccType: 'cc.Asset' }); + assets = assets.filter((v) => extname(v.file) === '.atlas'); let matchedAsset; let matchedAtlasJson; for (const asset of assets) { - const json = await (new ParseAtlasFile()).parse(asset.file); + const json = await new ParseAtlasFile().parse(asset.file); // the asset.displayName is not a full name, miss the extname // so, we should use RegExp to get the extname @@ -626,7 +627,6 @@ const Elements = { } } if (matchedAsset) { - let atlasFileFilter = matchedAtlasJson.filter; if (Array.isArray(atlasFileFilter)) { atlasFileFilter = atlasFileFilter.join(); @@ -635,9 +635,12 @@ const Elements = { } const userDataFilter = `${this.meta.userData.minfilter},${this.meta.userData.magfilter}`; - if (atlasFileFilter.toLowerCase() !== userDataFilter.toLowerCase()) { + if (atlasFileFilter && atlasFileFilter.toLowerCase() !== userDataFilter.toLowerCase()) { this.$.filterDifferent.style.display = 'block'; - const tipHtml = Editor.I18n.t('ENGINE.assets.texture.filterDiffenent').replace('{atlasFile}', `${matchedAsset.name}`); + const tipHtml = Editor.I18n.t('ENGINE.assets.texture.filterDiffenent').replace( + '{atlasFile}', + `${matchedAsset.name}`, + ); this.$.atlasFileName.innerHTML = tipHtml; this.$.atlasFileName.setAttribute('data-uuid', matchedAsset.uuid); } else { diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index d4be47971a..524126e677 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -101,6 +101,11 @@ if(NOT WINDOWS) add_definitions(-DBOOST_NO_CXX98_FUNCTION_BASE) endif() +# https://stackoverflow.com/questions/78598141/first-stdmutexlock-crashes-in-application-built-with-latest-visual-studio +if(WINDOWS) + add_definitions(-D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR) +endif() + add_definitions(-DCC_NETMODE_CLIENT=0) add_definitions(-DCC_NETMODE_LISTEN_SERVER=1) add_definitions(-DCC_NETMODE_HOST_SERVER=2) @@ -577,6 +582,8 @@ elseif(OPENHARMONY) cocos/platform/openharmony/napi/NapiHelper.h cocos/platform/openharmony/napi/NapiValueConverter.cpp cocos/platform/openharmony/napi/NapiValueConverter.h + cocos/platform/openharmony/napi/NapiPromiseBridge.cpp + cocos/platform/openharmony/napi/NapiPromiseBridge.h cocos/platform/openharmony/napi/native_common.h cocos/platform/openharmony/WorkerMessageQueue.cpp cocos/platform/openharmony/WorkerMessageQueue.h diff --git a/native/cocos/bindings/jswrapper/MappingUtils.h b/native/cocos/bindings/jswrapper/MappingUtils.h index 0c954bae5a..7ec4085d9c 100644 --- a/native/cocos/bindings/jswrapper/MappingUtils.h +++ b/native/cocos/bindings/jswrapper/MappingUtils.h @@ -169,6 +169,12 @@ private: if (kls != nullptr && kls != itr->second->_getClass()) { continue; } + // JS object may be null when it is obtained on zhe JSVM brach of the 5.1 ROMS + #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM + if(itr->second->_getJSObject() == nullptr) { + continue; + } + #endif eleCount++; CC_ASSERT_LT(eleCount, 2); eachCallback(itr->second); diff --git a/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h b/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h index ac0182c1dc..bc8fc6dee1 100644 --- a/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h +++ b/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h @@ -52,6 +52,15 @@ } \ } while (0) +#define NODE_API_CALL_RESULT(status, env, the_call, ret_val) \ + do { \ + status = the_call; \ + if (status != JSVM_OK) { \ + se::internal::logJsException((env), __FILE_NAME__, __LINE__); \ + return ret_val; \ + } \ + } while (0) + // Returns nullptr if the_call doesn't return JSVM_OK. #define NODE_API_CALL(status, env, the_call) \ do { \ diff --git a/native/cocos/bindings/jswrapper/jsvm/Object.cpp b/native/cocos/bindings/jswrapper/jsvm/Object.cpp index 05650fff45..32f205bcb1 100644 --- a/native/cocos/bindings/jswrapper/jsvm/Object.cpp +++ b/native/cocos/bindings/jswrapper/jsvm/Object.cpp @@ -64,7 +64,10 @@ Object* Object::createObjectWithClass(Class* cls) { bool Object::setProperty(const char* name, const Value& data) { JSVM_Status status; JSVM_Value jsVal; - internal::seToJsValue(data, &jsVal); + bool ret = internal::seToJsValue(data, &jsVal); + if(!ret) { + return false; + } NODE_API_CALL(status, _env, OH_JSVM_SetNamedProperty(_env, _objRef.getValue(_env), name, jsVal)); return status == JSVM_OK; } @@ -125,26 +128,23 @@ void Object::setPrivateObject(PrivateObjectBase* data) { bool Object::deleteProperty(const char* name) { JSVM_Status status; JSVM_Value key; - NODE_API_CALL(status, _env, OH_JSVM_GetNamedProperty(_env, _objRef.getValue(_env), name, &key)); - if (status != JSVM_OK) { - return false; - } + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetNamedProperty(_env, _objRef.getValue(_env), name, &key), false); bool ret = false; - NODE_API_CALL(status, _env, OH_JSVM_DeleteProperty(_env, _objRef.getValue(_env), key, &ret)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_DeleteProperty(_env, _objRef.getValue(_env), key, &ret), false); return ret; } bool Object::isArray() const { JSVM_Status status; bool ret = false; - NODE_API_CALL(status, _env, OH_JSVM_IsArray(_env, _objRef.getValue(_env), &ret)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_IsArray(_env, _objRef.getValue(_env), &ret), false); return ret; } bool Object::getArrayLength(uint32_t* length) const { JSVM_Status status; uint32_t len = 0; - NODE_API_CALL(status, _env, OH_JSVM_GetArrayLength(_env, _objRef.getValue(_env), &len)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetArrayLength(_env, _objRef.getValue(_env), &len), false); if (length) { *length = len; } @@ -154,7 +154,7 @@ bool Object::getArrayLength(uint32_t* length) const { bool Object::getArrayElement(uint32_t index, Value* data) const { JSVM_Status status; JSVM_Value val; - NODE_API_CALL(status, _env, OH_JSVM_GetElement(_env, _objRef.getValue(_env), index, &val)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetElement(_env, _objRef.getValue(_env), index, &val), false); internal::jsToSeValue(val, data); return true; } @@ -162,15 +162,18 @@ bool Object::getArrayElement(uint32_t index, Value* data) const { bool Object::setArrayElement(uint32_t index, const Value& data) { JSVM_Status status; JSVM_Value val; - internal::seToJsValue(data, &val); - NODE_API_CALL(status, _env, OH_JSVM_SetElement(_env, _objRef.getValue(_env), index, val)); + bool ret = internal::seToJsValue(data, &val); + if(!ret) { + return false; + } + NODE_API_CALL_RESULT(status, _env, OH_JSVM_SetElement(_env, _objRef.getValue(_env), index, val), false); return true; } bool Object::isTypedArray() const { JSVM_Status status; bool ret = false; - NODE_API_CALL(status, _env, OH_JSVM_IsTypedarray(_env, _objRef.getValue(_env), &ret)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_IsTypedarray(_env, _objRef.getValue(_env), &ret), false); return ret; } @@ -236,7 +239,7 @@ bool Object::getTypedArrayData(uint8_t** ptr, size_t* length) const { size_t byteOffset; size_t arrayLength; void* data = nullptr; - NODE_API_CALL(status, _env, OH_JSVM_GetTypedarrayInfo(_env, _objRef.getValue(_env), &type, &arrayLength, &data, &inputBuffer, &byteOffset)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetTypedarrayInfo(_env, _objRef.getValue(_env), &type, &arrayLength, &data, &inputBuffer, &byteOffset), false); *ptr = (uint8_t*)(data); if (length) { size_t bytesOfElement = 1; @@ -266,14 +269,14 @@ bool Object::getTypedArrayData(uint8_t** ptr, size_t* length) const { bool Object::isArrayBuffer() const { bool ret = false; JSVM_Status status; - NODE_API_CALL(status, _env, OH_JSVM_IsArraybuffer(_env, _objRef.getValue(_env), &ret)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_IsArraybuffer(_env, _objRef.getValue(_env), &ret), false); return ret; } bool Object::getArrayBufferData(uint8_t** ptr, size_t* length) const { JSVM_Status status; size_t len = 0; - NODE_API_CALL(status, _env, OH_JSVM_GetArraybufferInfo(_env, _objRef.getValue(_env), reinterpret_cast(ptr), &len)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetArraybufferInfo(_env, _objRef.getValue(_env), reinterpret_cast(ptr), &len), false); if (length) { *length = len; } @@ -290,7 +293,7 @@ Object* Object::createTypedArray(Object::TypedArrayType type, const void* data, JSVM_TypedarrayType jsvmType; JSVM_Value outputBuffer; void* outputPtr = nullptr; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &outputPtr, &outputBuffer)); + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &outputPtr, &outputBuffer), nullptr); if (outputPtr && data && byteLength > 0) { memcpy(outputPtr, data, byteLength); } @@ -346,8 +349,7 @@ Object* Object::createTypedArray(Object::TypedArrayType type, const void* data, } size_t eleCounts = byteLength / sizeOfEle; JSVM_Value outputArray; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateTypedarray(ScriptEngine::getEnv(), jsvmType, eleCounts, outputBuffer, 0, &outputArray)); - + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateTypedarray(ScriptEngine::getEnv(), jsvmType, eleCounts, outputBuffer, 0, &outputArray), nullptr); Object* obj = Object::_createJSObject(ScriptEngine::getEnv(), outputArray, nullptr); return obj; } @@ -373,7 +375,10 @@ Object* Object::createTypedArrayWithBuffer(TypedArrayType type, const Object* ob SE_LOGE("Don't pass se::Object::TypedArrayType::NONE to createTypedArray API!"); return nullptr; } - + if (!obj->_getJSObject()) { + SE_LOGE("Failed to create typed array: underlying ArrayBuffer is invalid (possibly released)"); + return nullptr; + } assert(obj->isArrayBuffer()); JSVM_Status status; JSVM_Value outputBuffer = obj->_getJSObject(); @@ -427,8 +432,7 @@ Object* Object::createTypedArrayWithBuffer(TypedArrayType type, const Object* ob } size_t eleCounts = byteLength / sizeOfEle; JSVM_Value outputArray; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateTypedarray(ScriptEngine::getEnv(), jsvmType, eleCounts, outputBuffer, offset, &outputArray)); - + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateTypedarray(ScriptEngine::getEnv(), jsvmType, eleCounts, outputBuffer, offset, &outputArray), nullptr); return Object::_createJSObject(ScriptEngine::getEnv(), outputArray, nullptr); } @@ -436,7 +440,7 @@ Object* Object::createExternalArrayBufferObject(void* contents, size_t byteLengt JSVM_Status status; JSVM_Value result; // JSVM does not support the napi_create_external_arraybuffer interface like in NAPI. - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayBufferFromBackingStoreData(ScriptEngine::getEnv(), contents, byteLength, 0, byteLength, &result)); + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayBufferFromBackingStoreData(ScriptEngine::getEnv(), contents, byteLength, 0, byteLength, &result), nullptr); Object* obj = Object::_createJSObject(ScriptEngine::getEnv(), result, nullptr); return obj; } @@ -444,15 +448,15 @@ Object* Object::createExternalArrayBufferObject(void* contents, size_t byteLengt bool Object::isFunction() const { JSVM_ValueType valuetype0; JSVM_Status status; - NODE_API_CALL(status, _env, OH_JSVM_Typeof(_env, _objRef.getValue(_env), &valuetype0)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_Typeof(_env, _objRef.getValue(_env), &valuetype0), false); return (valuetype0 == JSVM_ValueType::JSVM_FUNCTION); } bool Object::defineFunction(const char* funcName, JSVM_Callback func) { JSVM_Value fn; JSVM_Status status; - NODE_API_CALL(status, _env, OH_JSVM_CreateFunction(_env, funcName, JSVM_AUTO_LENGTH, func, &fn)); - NODE_API_CALL(status, _env, OH_JSVM_SetNamedProperty(_env, _objRef.getValue(_env), funcName, fn)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_CreateFunction(_env, funcName, JSVM_AUTO_LENGTH, func, &fn), false); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_SetNamedProperty(_env, _objRef.getValue(_env), funcName, fn), false); return true; } @@ -478,7 +482,7 @@ Object* Object::_createJSObject(JSVM_Env env, JSVM_Value js_object, Class* cls) Object* Object::createPlainObject() { JSVM_Value result; JSVM_Status status; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateObject(ScriptEngine::getEnv(), &result)); + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateObject(ScriptEngine::getEnv(), &result), nullptr); Object* obj = _createJSObject(ScriptEngine::getEnv(), result, nullptr); return obj; } @@ -486,7 +490,7 @@ Object* Object::createPlainObject() { Object* Object::createArrayObject(size_t length) { JSVM_Value result; JSVM_Status status; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayWithLength(ScriptEngine::getEnv(), length, &result)); + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayWithLength(ScriptEngine::getEnv(), length, &result), nullptr); Object* obj = _createJSObject(ScriptEngine::getEnv(), result, nullptr); return obj; } @@ -496,13 +500,11 @@ Object* Object::createArrayBufferObject(const void* data, size_t byteLength) { JSVM_Status status; void* retData; Object* obj = nullptr; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &retData, &result)); - if (status == JSVM_OK) { - if (data) { - memcpy(retData, data, byteLength); - } - obj = _createJSObject(ScriptEngine::getEnv(), result, nullptr); + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &retData, &result), nullptr); + if (data) { + memcpy(retData, data, byteLength); } + obj = _createJSObject(ScriptEngine::getEnv(), result, nullptr); return obj; } @@ -510,21 +512,22 @@ bool Object::getAllKeys(std::vector* allKeys) const { JSVM_Status status; JSVM_Value names; - NODE_API_CALL(status, _env, OH_JSVM_GetPropertyNames(_env, _objRef.getValue(_env), &names)); - if (status != JSVM_OK) { - return false; - } + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetPropertyNames(_env, _objRef.getValue(_env), &names), false); uint32_t name_len = 0; - NODE_API_CALL(status, _env, OH_JSVM_GetArrayLength(_env, names, &name_len)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetArrayLength(_env, names, &name_len), false); for (uint32_t i = 0; i < name_len; i++) { JSVM_Value val; NODE_API_CALL(status, _env, OH_JSVM_GetElement(_env, names, i, &val)); if (status == JSVM_OK) { - char buffer[MAX_STRING_LEN]; size_t result = 0; - NODE_API_CALL(status, _env, OH_JSVM_GetValueStringUtf8(_env, val, buffer, MAX_STRING_LEN, &result)); - if (result > 0) { - allKeys->push_back(buffer); + NODE_API_CALL(status, _env, OH_JSVM_GetValueStringUtf8(_env, val, nullptr, 0, &result)); + if(status == JSVM_OK){ + result += 1; + char buffer[result]; + NODE_API_CALL(status, _env, OH_JSVM_GetValueStringUtf8(_env, val, buffer, sizeof(buffer), &result)); + if (result > 0) { + allKeys->push_back(buffer); + } } } } @@ -546,28 +549,37 @@ bool Object::init(JSVM_Env env, JSVM_Value js_object, Class* cls) { } bool Object::call(const ValueArray& args, Object* thisObject, Value* rval) { + if (_getJSObject() == nullptr) { + SE_LOGE("Attempted to call function on invalid JS object (possibly released or uninitialized)"); + return false; + } size_t argc = 0; std::vector argv; argv.reserve(10); argc = args.size(); - internal::seToJsArgs(_env, args, &argv); + bool valid = internal::seToJsArgs(_env, args, &argv); + if(!valid) { + SE_LOGE("Failed to convert arguments from se::Value to JSVM_Value"); + return false; + } JSVM_Value return_val; JSVM_Status status; assert(isFunction()); + if(thisObject != nullptr && !thisObject->_getJSObject()) { + SE_LOGE("Invalid 'this' object passed to function call (JS object is invalid or released)"); + return false; + } JSVM_Value thisObj = thisObject ? thisObject->_getJSObject() : ({ JSVM_Value undefinedValue; OH_JSVM_GetUndefined(_env, &undefinedValue); undefinedValue; }); - NODE_API_CALL(status, _env, - OH_JSVM_CallFunction(_env, thisObj, _getJSObject(), argc, argv.data(), &return_val)); - if (status == JSVM_OK) { - if (rval) { - internal::jsToSeValue(return_val, rval); - } - return true; + NODE_API_CALL_RESULT(status, _env, + OH_JSVM_CallFunction(_env, thisObj, _getJSObject(), argc, argv.data(), &return_val), false); + if (rval) { + internal::jsToSeValue(return_val, rval); } - return false; + return true; } void Object::_setFinalizeCallback(JSVM_Finalize finalizeCb) { @@ -630,10 +642,12 @@ std::string Object::toString() const { JSVM_Status status; if (isFunction() || isArray() || isTypedArray()) { JSVM_Value result; - NODE_API_CALL(status, _env, OH_JSVM_CoerceToString(_env, _objRef.getValue(_env), &result)); - char buffer[MAX_STRING_LEN]; + NODE_API_CALL_RESULT(status, _env, OH_JSVM_CoerceToString(_env, _objRef.getValue(_env), &result), ret); size_t result_t = 0; - NODE_API_CALL(status, _env, OH_JSVM_GetValueStringUtf8(_env, result, buffer, MAX_STRING_LEN, &result_t)); + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetValueStringUtf8(_env, result, nullptr, 0, &result_t), ret); + result_t += 1; + char buffer[result_t]; + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetValueStringUtf8(_env, result, buffer, sizeof(buffer), &result_t), ret); ret = buffer; } else if (isArrayBuffer()) { ret = "[object ArrayBuffer]"; @@ -789,11 +803,9 @@ Object* Object::createJSONObject(const std::string& jsonStr) { JSVM_Status status = JSVM_OK; JSVM_Value jsJsonStr = nullptr; - NODE_API_CALL(status, _env, OH_JSVM_CreateStringUtf8(_env, jsonStr.c_str(), jsonStr.length(), &jsJsonStr)); - + NODE_API_CALL_RESULT(status, _env, OH_JSVM_CreateStringUtf8(_env, jsonStr.c_str(), jsonStr.length(), &jsJsonStr), nullptr); JSVM_Value jsObj = nullptr; - NODE_API_CALL(status, _env, OH_JSVM_JsonParse(_env, jsJsonStr, &jsObj)); - + NODE_API_CALL_RESULT(status, _env, OH_JSVM_JsonParse(_env, jsJsonStr, &jsObj), nullptr); return Object::_createJSObject(_env, jsObj, nullptr); } @@ -802,11 +814,9 @@ Object* Object::createJSONObject(std::u16string&& jsonStr) { JSVM_Status status = JSVM_OK; JSVM_Value jsJsonStr = nullptr; - NODE_API_CALL(status, _env, OH_JSVM_CreateStringUtf16(_env, jsonStr.c_str(), jsonStr.length(), &jsJsonStr)); - + NODE_API_CALL_RESULT(status, _env, OH_JSVM_CreateStringUtf16(_env, jsonStr.c_str(), jsonStr.length(), &jsJsonStr), nullptr); JSVM_Value jsObj = nullptr; - NODE_API_CALL(status, _env, OH_JSVM_JsonParse(_env, jsJsonStr, &jsObj)); - + NODE_API_CALL_RESULT(status, _env, OH_JSVM_JsonParse(_env, jsJsonStr, &jsObj), nullptr); return Object::_createJSObject(_env, jsObj, nullptr); } @@ -839,11 +849,47 @@ void Object::clearPrivateData(bool clearMapping) { Object* Object::createUTF8String(const std::string& str) { JSVM_Status status; JSVM_Value result; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateStringUtf8(ScriptEngine::getEnv(), str.c_str(), JSVM_AUTO_LENGTH, &result)); + NODE_API_CALL_RESULT(status, ScriptEngine::getEnv(), OH_JSVM_CreateStringUtf8(ScriptEngine::getEnv(), str.c_str(), JSVM_AUTO_LENGTH, &result), nullptr); Object* obj = _createJSObject(ScriptEngine::getEnv(), result, nullptr); return obj; } +std::unordered_map Object::resolverMap; + +void Object::resolverPromise(Object* object, const Value& value) { + auto it = resolverMap.find(object); + if (it != resolverMap.end()) { + auto* resolver = it->second; + JSVM_Value jsvmValue; + se::internal::seToJsValue(value, &jsvmValue); + OH_JSVM_ResolveDeferred(ScriptEngine::getEnv(), resolver, jsvmValue); + resolverMap.erase(it); + } +} + +void Object::rejectPromise(Object* object, const Value& value) { + auto it = resolverMap.find(object); + if (it != resolverMap.end()) { + auto* resolver = it->second; + JSVM_Value jsvmValue; + se::internal::seToJsValue(value, &jsvmValue); + OH_JSVM_ResolveDeferred(ScriptEngine::getEnv(), resolver, jsvmValue); + resolverMap.erase(it); + } +} + +Object* Object::createPromise() { + JSVM_Deferred deferred = nullptr; + JSVM_Value promise = nullptr; + JSVM_Status createStatus = OH_JSVM_CreatePromise(ScriptEngine::getEnv(), &deferred, &promise); + if (createStatus != JSVM_OK) { + return nullptr; + } + Object* object = Object::_createJSObject(ScriptEngine::getEnv(), promise, nullptr); + resolverMap[object] = deferred; + return object; +} + ObjectRef::ObjectRef(Object *parent) : _parent(parent) { @@ -872,7 +918,8 @@ void ObjectRef::init(JSVM_Env env, JSVM_Value obj) { JSVM_Value ObjectRef::getValue(JSVM_Env env) const { JSVM_Value r = nullptr; - OH_JSVM_GetReferenceValue(_env, _ref, &r); + JSVM_Status status; + NODE_API_CALL_RESULT(status, _env, OH_JSVM_GetReferenceValue(_env, _ref, &r), nullptr); return r; } diff --git a/native/cocos/bindings/jswrapper/jsvm/Object.h b/native/cocos/bindings/jswrapper/jsvm/Object.h index e475a0e2ec..a94f20ddf1 100644 --- a/native/cocos/bindings/jswrapper/jsvm/Object.h +++ b/native/cocos/bindings/jswrapper/jsvm/Object.h @@ -33,6 +33,7 @@ #include "CommonHeader.h" #include "HelperMacros.h" #include "Utils.h" +#include namespace se { class Class; @@ -433,6 +434,10 @@ public: static Object *createUTF8String(const std::string &str); + static Object *createPromise(); + static void rejectPromise(Object *object, const Value &value); + static void resolverPromise(Object *object, const Value &value); + private: // Object(); // virtual ~Object(); @@ -453,7 +458,9 @@ private: uint32_t _rootCount = 0; bool _onCleaingPrivateData = false; internal::PrivateData *_internalData; - + + static std::unordered_map resolverMap; + friend class ObjectRef; friend class ScriptEngine; }; diff --git a/native/cocos/bindings/jswrapper/jsvm/ScriptEngine.cpp b/native/cocos/bindings/jswrapper/jsvm/ScriptEngine.cpp index c65c6368b5..b549e28816 100644 --- a/native/cocos/bindings/jswrapper/jsvm/ScriptEngine.cpp +++ b/native/cocos/bindings/jswrapper/jsvm/ScriptEngine.cpp @@ -334,7 +334,8 @@ bool ScriptEngine::start() { } else { OH_JSVM_OpenInspector(_env, _debuggerServerAddr.c_str(), _debuggerServerPort); } - SE_LOGD("Debugger listening..., visit [ http://localhost:6068/json ] to configuration debugging information and copy the devtoolsFrontendUrl value to the browser!"); + SE_LOGD("Debugger listening..., visit [ http://localhost:%d/json ] to configuration debugging information and copy the devtoolsFrontendUrl value to the browser!", _debuggerServerPort); + if(_isWaitForConnect) { OH_JSVM_WaitForDebugger(_env, true); } diff --git a/native/cocos/bindings/jswrapper/jsvm/Utils.cpp b/native/cocos/bindings/jswrapper/jsvm/Utils.cpp index 34b26152b0..0efb51b586 100644 --- a/native/cocos/bindings/jswrapper/jsvm/Utils.cpp +++ b/native/cocos/bindings/jswrapper/jsvm/Utils.cpp @@ -173,13 +173,15 @@ bool seToJsValue(const Value& v, target_value* outJsVal) { return ret; } -void seToJsArgs(JSVM_Env env, const ValueArray& args, std::vector* outArr) { +bool seToJsArgs(JSVM_Env env, const ValueArray& args, std::vector* outArr) { + bool ret = true; assert(outArr != nullptr); for (const auto& data : args) { JSVM_Value jsval; - seToJsValue(data, &jsval); + ret = ret && seToJsValue(data, &jsval); outArr->push_back(jsval); } + return ret; } bool setReturnValue(const Value& data, target_value& argv) { @@ -188,6 +190,9 @@ bool setReturnValue(const Value& data, target_value& argv) { auto env = ScriptEngine::getEnv(); JSVM_Status status; NODE_API_CALL(status, env, OH_JSVM_CreateDouble(env, data.toDouble(), &argv)); + if (status != JSVM_OK) { + return false; + } return true; } diff --git a/native/cocos/bindings/jswrapper/jsvm/Utils.h b/native/cocos/bindings/jswrapper/jsvm/Utils.h index 0adb64847c..73ddb4b104 100644 --- a/native/cocos/bindings/jswrapper/jsvm/Utils.h +++ b/native/cocos/bindings/jswrapper/jsvm/Utils.h @@ -41,7 +41,7 @@ bool setReturnValue(const Value &data, target_value &argv); void jsToSeValue(const target_value &value, Value *v); void jsToSeArgs(size_t argc, target_value *argv, ValueArray *outArr); bool seToJsValue(const Value &v, target_value *jsval); -void seToJsArgs(JSVM_Env env, const ValueArray &args, std::vector *outArr); +bool seToJsArgs(JSVM_Env env, const ValueArray &args, std::vector *outArr); std::string jsToString(const target_value &value); void logJsException(JSVM_Env env, const char *file, int line); diff --git a/native/cocos/bindings/jswrapper/v8/Object.cpp b/native/cocos/bindings/jswrapper/v8/Object.cpp index a7a094705a..3364e9fc87 100644 --- a/native/cocos/bindings/jswrapper/v8/Object.cpp +++ b/native/cocos/bindings/jswrapper/v8/Object.cpp @@ -25,6 +25,7 @@ #include "Object.h" #include "v8/HelperMacros.h" +#include "application/ApplicationManager.h" // Use node::Buffer to replace v8 api,to avoid link err in editor platform. #if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS @@ -163,6 +164,54 @@ Object *Object::createPlainObject() { return obj; } +std::unordered_map*> Object::resolverMap; + +void Object::resolverPromise(Object *object, const Value &value) { + auto it = resolverMap.find(object); + if (it != resolverMap.end()) { + v8::Isolate *isolate = __isolate; + v8::HandleScope scope(isolate); + v8::Local context = isolate->GetCurrentContext(); + auto* resolver = it->second; + v8::Local v8Val; + se::internal::seToJsValue(__isolate, value, &v8Val); + resolver->Get(isolate)->Resolve(context, v8Val).ToChecked(); + resolver->Reset(); + delete resolver; + resolverMap.erase(it); + } +} + +void Object::rejectPromise(Object *object, const Value &value) { + auto it = resolverMap.find(object); + if (it != resolverMap.end()) { + v8::Isolate *isolate = __isolate; + v8::HandleScope scope(isolate); + v8::Local context = isolate->GetCurrentContext(); + auto* resolver = it->second; + v8::Local v8Val; + se::internal::seToJsValue(isolate, value, &v8Val); + resolver->Get(isolate)->Reject(context, v8Val).ToChecked(); + resolver->Reset(); + delete resolver; + resolverMap.erase(it); + } +} + +Object *Object::createPromise() { + v8::Isolate *isolate = __isolate; + v8::HandleScope handleScope(isolate); + v8::Local resolver = v8::Promise::Resolver::New(isolate->GetCurrentContext()).ToLocalChecked(); + v8::Local v8Promise = resolver->GetPromise(); + auto *persistentResolver = new v8::Persistent(isolate, resolver); + + v8::Local jsobj = v8::Local::Cast(v8Promise); + auto *obj = Object::_createJSObject(nullptr, jsobj); + resolverMap[obj] = persistentResolver; + + return obj; +} + Object *Object::createMapObject() { v8::Local jsobj = v8::Map::New(__isolate); return _createJSObject(nullptr, jsobj); @@ -409,10 +458,8 @@ Object *Object::createJSONObject(std::u16string &&jsonStr) { v8::Local context = __isolate->GetCurrentContext(); v8::MaybeLocal ret = v8::JSON::Parse(context, v8Str.ToLocalChecked()); - // After v8::JSON::Parse, the memory of u16string could be freed. external->freeMemory(); - if (ret.IsEmpty()) { return nullptr; } diff --git a/native/cocos/bindings/jswrapper/v8/Object.h b/native/cocos/bindings/jswrapper/v8/Object.h index 7d3834bd58..56934fa514 100644 --- a/native/cocos/bindings/jswrapper/v8/Object.h +++ b/native/cocos/bindings/jswrapper/v8/Object.h @@ -85,6 +85,9 @@ public: */ static Object *createArrayObject(size_t length); + static Object *createPromise(); + static void rejectPromise(Object *object, const Value &value); + static void resolverPromise(Object *object, const Value &value); /** * @brief Creates a JavaScript Typed Array Object with uint8 format from an existing pointer. * @param[in] bytes A pointer to the byte buffer to be used as the backing store of the Typed Array object. @@ -152,7 +155,7 @@ public: * @note The return value (non-null) has to be released manually. */ static Object *createJSONObject(const ccstd::string &jsonStr); - + /** * @brief Creates a JavaScript Object from a JSON formatted string. * @param[in] jsonStr The utf-16 string containing the JSON string to be parsed. @@ -705,6 +708,8 @@ private: ccstd::string _objectCreationStackFrame; #endif + static std::unordered_map*> resolverMap; + friend class ScriptEngine; friend class JSBPersistentHandleVisitor; }; diff --git a/native/cocos/bindings/manual/jsb_global.cpp b/native/cocos/bindings/manual/jsb_global.cpp index 27ea121651..12f863d925 100644 --- a/native/cocos/bindings/manual/jsb_global.cpp +++ b/native/cocos/bindings/manual/jsb_global.cpp @@ -52,8 +52,9 @@ #include "platform/java/jni/JniImp.h" #endif -#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY && SCRIPT_ENGINE_TYPE != SCRIPT_ENGINE_NAPI +#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY #include "platform/openharmony/napi/NapiHelper.h" + #include "platform/openharmony/napi/NapiPromiseBridge.h" #endif extern void jsb_register_ADPF(se::Object *); // NOLINT @@ -1433,7 +1434,7 @@ static bool JSB_process_get_argv(se::State &s) // NOLINT(readability-identifier- } SE_BIND_PROP_GET(JSB_process_get_argv) -#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY && SCRIPT_ENGINE_TYPE != SCRIPT_ENGINE_NAPI +#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY static bool sevalue_to_napivalue(const se::Value &seVal, Napi::Value *napiVal, Napi::Env env); @@ -1537,14 +1538,8 @@ static bool JSB_openharmony_postSyncMessage(se::State &s) { // NOLINT(readabilit SE_REPORT_ERROR("postMessage, Unsupported type"); return false; } - Napi::Value napiPromise = NapiHelper::postSyncMessageToUIThread(msgType.c_str(), napiArg1); - - // TODO(cjh): Implement Promise for se - se::HandleObject retObj(se::Object::createPlainObject()); - retObj->defineFunction("then", _SE(JSB_empty_promise_then)); - s.rval().setObject(retObj); - // + s.rval().setObject(NapiPromiseBridge::createPromise(napiPromise)); return true; } @@ -1619,12 +1614,10 @@ bool jsb_register_global_variables(se::Object *global) { // NOLINT global->setProperty("performance", se::Value(performanceObj)); #if CC_PLATFORM == CC_PLATFORM_OPENHARMONY - #if SCRIPT_ENGINE_TYPE != SCRIPT_ENGINE_NAPI se::HandleObject ohObj(se::Object::createPlainObject()); global->setProperty("oh", se::Value(ohObj)); ohObj->defineFunction("postMessage", _SE(JSB_openharmony_postMessage)); ohObj->defineFunction("postSyncMessage", _SE(JSB_openharmony_postSyncMessage)); - #endif #endif jsb_register_TextEncoder(global); diff --git a/native/cocos/core/builtin/DebugInfos.cpp b/native/cocos/core/builtin/DebugInfos.cpp index a92ec9c493..1e5559ca3d 100644 --- a/native/cocos/core/builtin/DebugInfos.cpp +++ b/native/cocos/core/builtin/DebugInfos.cpp @@ -218,7 +218,7 @@ ccstd::unordered_map debugInfos = { { 3933, "Each curve that has common target should be numeric curve and targets string property." }, { 3934, "Misconfigured legacy curve: the first keyframe value is number but others aren't." }, { 3935, "We don't currently support conversion of `CubicSplineQuatValue`." }, -{ 3936, "Instancing/Batching enabled for non-baked skinning model '%s', this may result in unexpected rendering artifacts. Consider turning it off in the material if you do not intend to do this." }, +{ 3936, "Instancing/Batching enabled for non-baked skinning model or used AnimationController '%s', this may result in unexpected rendering artifacts. Consider turning it off in the material if you do not intend to do this." }, { 3937, "Previous error occurred when instantiating animation clip %s on node %s." }, { 3938, "'%s' is not found from '%s'. It's specified as the root node to play animation clip '%s'." }, { 3940, "Error when animation attempted to bind material uniform target: target %s is not a material." }, diff --git a/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp b/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp index b4abfaae7c..67c96e8141 100644 --- a/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp @@ -16,6 +16,21 @@ using namespace spine; extern HashMap*> spineAttachmentVerticesMap; extern HashMap*> spineTexturesMap; +/** + * The slot-associated attachment may exist on different skeletonData, so setSlotTexture needs to traverse all skeletonData. + */ +AttachmentVertices *getAttachmentVertices(Attachment *attachment) { + auto entries = spineAttachmentVerticesMap.getEntries(); + while (entries.hasNext()) { + auto entry = entries.next(); + auto *skeletonData = entry.key; + auto *attachmentVerticesMap = entry.value; + if (attachmentVerticesMap->containsKey(attachment)) { + return (*attachmentVerticesMap)[attachment]; + } + } + return nullptr; +} template void loopUVCoords(VertexType* tmp, const UVArrayType& uvs, int count) { @@ -621,11 +636,6 @@ void SpineSkeletonInstance::onAnimationStateEvent(TrackEntry *entry, EventType t void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint32_t width, uint32_t height, bool createNew) { if (!_skeleton) return; - HashMap *attachmentVerticesMap = nullptr; - if (spineAttachmentVerticesMap.containsKey(_skeletonData)) { - attachmentVerticesMap = spineAttachmentVerticesMap[_skeletonData]; - } - if (!attachmentVerticesMap) return; auto* slot = _skeleton->findSlot(slotName); if (!slot) return; auto *attachment = slot->getAttachment(); @@ -674,10 +684,7 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint region->updateRegion(); #endif - AttachmentVertices *attachmentVertices = nullptr; - if (attachmentVerticesMap->containsKey(attachment)) { - attachmentVertices = (*attachmentVerticesMap)[attachment]; - } + auto *attachmentVertices = getAttachmentVertices(slot->getAttachment()); if (!attachmentVertices) return; if (createNew) { attachmentVertices = attachmentVertices->copy(); @@ -723,10 +730,7 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint mesh->updateRegion(); #endif - AttachmentVertices *attachmentVertices = nullptr; - if (attachmentVerticesMap->containsKey(attachment)) { - attachmentVertices = (*attachmentVerticesMap)[attachment]; - } + auto *attachmentVertices = getAttachmentVertices(slot->getAttachment()); if (!attachmentVertices) return; if (createNew) { attachmentVertices = attachmentVertices->copy(); diff --git a/native/cocos/platform/openharmony/modules/CanvasRenderingContext2DDelegate.cpp b/native/cocos/platform/openharmony/modules/CanvasRenderingContext2DDelegate.cpp index c54d5326bc..2bd0e69de1 100644 --- a/native/cocos/platform/openharmony/modules/CanvasRenderingContext2DDelegate.cpp +++ b/native/cocos/platform/openharmony/modules/CanvasRenderingContext2DDelegate.cpp @@ -206,6 +206,11 @@ void CanvasRenderingContext2DDelegate::fillText(const ccstd::string &text, float return; } Size textSize = {0, 0}; + uint8_t r = static_cast(_fillStyle[0]); + uint8_t g = static_cast(_fillStyle[1]); + uint8_t b = static_cast(_fillStyle[2]); + uint8_t a = static_cast(_fillStyle[3]); + OH_Drawing_SetTextStyleColor(_textStyle, OH_Drawing_ColorSetArgb(a, r, g, b)); Point offsetPoint = convertDrawPoint(Point{x, y}, text, _textStyle); drawText(text, (int)offsetPoint[0], (int)offsetPoint[1]); if (!_premultiply) { @@ -308,7 +313,6 @@ void CanvasRenderingContext2DDelegate::setTextBaseline(TextBaseline baseline) { void CanvasRenderingContext2DDelegate::setFillStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { _fillStyle = {static_cast(r), static_cast(g), static_cast(b), static_cast(a)}; - OH_Drawing_SetTextStyleColor(_textStyle, OH_Drawing_ColorSetArgb(a, r, g, b)); } void CanvasRenderingContext2DDelegate::setStrokeStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { diff --git a/native/cocos/platform/openharmony/napi/NapiHelper.cpp b/native/cocos/platform/openharmony/napi/NapiHelper.cpp index 0a197a0b76..bbeb6fddec 100644 --- a/native/cocos/platform/openharmony/napi/NapiHelper.cpp +++ b/native/cocos/platform/openharmony/napi/NapiHelper.cpp @@ -31,6 +31,7 @@ #include "platform/openharmony/FileUtils-OpenHarmony.h" #include "platform/openharmony/OpenHarmonyPlatform.h" #include "platform/openharmony/modules/SystemWindow.h" +#include #if CC_USE_EDITBOX #include "ui/edit-box/EditBox-openharmony.h" @@ -63,6 +64,7 @@ enum ContextType { static Napi::Env gWorkerEnv(nullptr); static Napi::FunctionReference *gPostMessageToUIThreadFunc = nullptr; static Napi::FunctionReference *gPostSyncMessageToUIThreadFunc = nullptr; +static std::thread::id gMainThreadId; #define DEFINE_FUNCTION_CALLBACK(functionName, cachedFunctionRefPtr) \ static void functionName(const Napi::CallbackInfo &info) { \ @@ -498,17 +500,32 @@ Napi::Value evalString(const Napi::CallbackInfo &info){ return env.Undefined(); } - se::AutoHandleScope hs; - size_t length = value.length(); char* cValue = new char[length + 1]; strcpy(cValue, value.c_str()); - se::Value ret; - se::ScriptEngine::getInstance()->evalString(cValue, length, &ret); - delete[] cValue; - Napi::Value result; - sevalue_to_napivalue(ret, &result, env); - + + BaseEngine::SchedulerPtr scheduler = + CC_CURRENT_APPLICATION() ? CC_CURRENT_APPLICATION()->getEngine()->getScheduler() : nullptr; + if (!scheduler) { + delete[] cValue; + return env.Undefined(); + } + Napi::Value result = env.Undefined(); + if(gMainThreadId == std::this_thread::get_id()) { + scheduler->performFunctionInCocosThread([cValue, length]() { + se::AutoHandleScope hs; + se::Value ret; + se::ScriptEngine::getInstance()->evalString(cValue, length, &ret); + delete[] cValue; + }); + } else { + se::AutoHandleScope hs; + se::Value ret; + se::ScriptEngine::getInstance()->evalString(cValue, length, &ret); + delete[] cValue; + sevalue_to_napivalue(ret, &result, env); + } + return result; } @@ -519,7 +536,10 @@ Napi::Object NapiHelper::init(Napi::Env env, Napi::Object exports) { bool ret = exportFunctions(exports); if (!ret) { CC_LOG_ERROR("NapiHelper init failed"); + } else { + gMainThreadId = std::this_thread::get_id(); } + return exports; } diff --git a/native/cocos/platform/openharmony/napi/NapiPromiseBridge.cpp b/native/cocos/platform/openharmony/napi/NapiPromiseBridge.cpp new file mode 100644 index 0000000000..cd5cf0e3e1 --- /dev/null +++ b/native/cocos/platform/openharmony/napi/NapiPromiseBridge.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#include "cocos/platform/openharmony/napi/NapiPromiseBridge.h" +#include "cocos/platform/openharmony/napi/NapiHelper.h" +#include "cocos/bindings/jswrapper/SeApi.h" + +namespace cc { +struct CallbackData { + se::Object* object; +}; + +se::Object* NapiPromiseBridge::createPromise(Napi::Value napiPromise) { + bool isPromise{false}; + auto env = NapiHelper::getWorkerEnv(); + napi_status status = napi_is_promise(env, napiPromise, &isPromise); + if (status != napi_ok) { + SE_REPORT_ERROR("The returned type is not a promise"); + return nullptr; + } +#if (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM) || (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8) + se::Object* sePromise = se::Object::createPromise(); + bindArkPromise(napiPromise, sePromise); + return sePromise; +#else + return se::Object::_createJSObject(env, napiPromise, nullptr); +#endif +} + +#if (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM) || (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8) +void NapiPromiseBridge::bindArkPromise(Napi::Value napiPromise, se::Object* sePromise) { + auto cbData = new CallbackData{sePromise}; + auto env = NapiHelper::getWorkerEnv(); + napi_value onFulfilled; + napi_create_function(env, "onFulfilled", -1, [](napi_env env, napi_callback_info info) -> napi_value { + se::AutoHandleScope hs; + size_t argc = 1; + napi_value argv[1]; + void* data; + napi_get_cb_info(env, info, &argc, argv, nullptr, &data); + se::Value seArg; + NapiValueConverter::NapiValueToSeValue(env, argv[0], &seArg); + auto cbData = static_cast(data); + se::Object::resolverPromise(cbData->object, seArg); + + napi_value undefined; + napi_get_undefined(env, &undefined); + delete cbData; + return undefined; }, cbData, &onFulfilled); + + napi_value onRejected; + napi_create_function(env, "onRejected", -1, [](napi_env env, napi_callback_info info) -> napi_value { + se::AutoHandleScope hs; + size_t argc = 1; + napi_value argv[1]; + void* data; + napi_get_cb_info(env, info, &argc, argv, nullptr, &data); + se::Value seArg; + NapiValueConverter::NapiValueToSeValue(env, argv[0], &seArg); + auto cbData = static_cast(data); + se::Object::rejectPromise(cbData->object, seArg); + napi_value undefined; + napi_get_undefined(env, &undefined); + delete cbData; + return undefined; }, cbData, &onRejected); + + napi_value thenFunc; + napi_get_named_property(env, napiPromise, "then", &thenFunc); + + napi_value argvThen[2] = {onFulfilled, onRejected}; + napi_value thenResult; + napi_call_function(env, napiPromise, thenFunc, 2, argvThen, &thenResult); +} +#endif +} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/openharmony/napi/NapiPromiseBridge.h b/native/cocos/platform/openharmony/napi/NapiPromiseBridge.h new file mode 100644 index 0000000000..f8fd0355d2 --- /dev/null +++ b/native/cocos/platform/openharmony/napi/NapiPromiseBridge.h @@ -0,0 +1,47 @@ +/**************************************************************************** + Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#define NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS 1 +#define NAPI_DISABLE_CPP_EXCEPTIONS 1 +#define NODE_ADDON_API_DISABLE_DEPRECATED 1 +#include "cocos/platform/openharmony/napi/napi.h" +#include "cocos/bindings/jswrapper/config.h" + +namespace se { +class Object; +} + +namespace cc { +class NapiPromiseBridge { +public: + static se::Object* createPromise(Napi::Value napiPromise); +private: +#if (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM) || (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8) + static void bindArkPromise(Napi::Value napiPromise, se::Object* sePromise); +#endif +}; +} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/openharmony/napi/NapiValueConverter.cpp b/native/cocos/platform/openharmony/napi/NapiValueConverter.cpp index 9cfcec7cf5..9a32a8ec98 100644 --- a/native/cocos/platform/openharmony/napi/NapiValueConverter.cpp +++ b/native/cocos/platform/openharmony/napi/NapiValueConverter.cpp @@ -1,5 +1,7 @@ #include "NapiValueConverter.h" #include +#include "cocos/bindings/jswrapper/Value.h" +#include "base/Log.h" napi_valuetype GetNapiValueType(napi_env env, napi_value value) { napi_valuetype type; @@ -86,4 +88,78 @@ napi_value NapiValueConverter::ToNapiValue(napi_env env, const char* value) { napi_value NapiValueConverter::ToNapiValue(napi_env env, std::string value) { return ToNapiValue(env, value.c_str()); -} \ No newline at end of file +} + +bool NapiValueConverter::NapiValueToSeValue(napi_env env, napi_value value, se::Value* v) { + napi_status status; + napi_valuetype valType; + int64_t iRet = 0; + double dRet = 0.0; + bool bRet = false; + bool lossless = false; + size_t len = 0; + if (!value) { + valType = napi_valuetype::napi_undefined; + } else { + status = napi_typeof(env, value, &valType); + if (status != napi_ok) { + CC_LOG_WARNING("warning:%d", status); + return false; + } + } + + switch (valType) { + case napi_valuetype::napi_undefined: + v->setUndefined(); + break; + case napi_valuetype::napi_null: + v->setNull(); + break; + case napi_valuetype::napi_number: + status = napi_get_value_double(env, value, &dRet); + if (status == napi_ok) { + v->setDouble(dRet); + } else { + CC_LOG_WARNING("warning:%d", status); + v->setUndefined(); + } + break; + case napi_valuetype::napi_bigint: + status = napi_get_value_bigint_int64(env, value, &iRet, &lossless); + if (lossless) { + v->setInt64(iRet); + } else { + v->setUndefined(); + } + + break; + case napi_valuetype::napi_string: + status = napi_get_value_string_utf8(env, value, nullptr, 0, &len); + if (status == napi_ok) { + std::string valueStr; + len += 1; + valueStr.resize(len); + status = napi_get_value_string_utf8(env, value, const_cast(valueStr.data()), valueStr.size(), &len); + if (valueStr.length() != len) { + valueStr.resize(len); + } + v->setString(valueStr); + } else { + CC_LOG_WARNING("warning:%d", status); + v->setUndefined(); + } + break; + case napi_valuetype::napi_boolean: + status = napi_get_value_bool(env, value, &bRet); + if (status == napi_ok) { + v->setBoolean(bRet); + } else { + CC_LOG_WARNING("warning:%d", status); + v->setUndefined(); + } + break; + default: + break; + } + return true; +} diff --git a/native/cocos/platform/openharmony/napi/NapiValueConverter.h b/native/cocos/platform/openharmony/napi/NapiValueConverter.h index 89f377a288..84fc901115 100644 --- a/native/cocos/platform/openharmony/napi/NapiValueConverter.h +++ b/native/cocos/platform/openharmony/napi/NapiValueConverter.h @@ -1,6 +1,9 @@ #include #include +namespace se { + class Value; +} class NapiValueConverter { public: template @@ -13,4 +16,5 @@ public: static napi_value ToNapiValue(napi_env env, const char* value); static napi_value ToNapiValue(napi_env env, std::string value); + static bool NapiValueToSeValue(napi_env env, napi_value value, se::Value *v); }; \ No newline at end of file diff --git a/native/cocos/renderer/gfx-base/SPIRVUtils.cpp b/native/cocos/renderer/gfx-base/SPIRVUtils.cpp index fbb32eda5b..cd2a54e3bd 100644 --- a/native/cocos/renderer/gfx-base/SPIRVUtils.cpp +++ b/native/cocos/renderer/gfx-base/SPIRVUtils.cpp @@ -63,9 +63,13 @@ glslang::EShTargetClientVersion getClientVersion(int vulkanMinorVersion) { case 1: return glslang::EShTargetVulkan_1_1; case 2: return glslang::EShTargetVulkan_1_2; #if CC_GLSLANG_VERSION_GREATOR_OR_EQUAL_TO(11, 10, 0) - case 3: return glslang::EShTargetVulkan_1_3; + case 3: + case 4: + return glslang::EShTargetVulkan_1_3; #else - case 3: return glslang::EShTargetVulkan_1_2; + case 3: + case 4: + return glslang::EShTargetVulkan_1_2; #endif default: { CC_ABORT(); @@ -80,9 +84,13 @@ glslang::EShTargetLanguageVersion getTargetVersion(int vulkanMinorVersion) { case 1: return glslang::EShTargetSpv_1_3; case 2: return glslang::EShTargetSpv_1_5; #if CC_GLSLANG_VERSION_GREATOR_OR_EQUAL_TO(11, 10, 0) - case 3: return glslang::EShTargetSpv_1_6; + case 3: + case 4: + return glslang::EShTargetSpv_1_6; #else - case 3: return glslang::EShTargetSpv_1_5; + case 3: + case 4: + return glslang::EShTargetSpv_1_5; #endif default: { CC_ABORT(); diff --git a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp index 7edf4648aa..5c20af2fa3 100644 --- a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp @@ -542,7 +542,6 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { ResourceGraph::vertex_descriptor srcID, ResourceGraph::vertex_descriptor dstID) const { auto& resg = ctx.resourceGraph; - std::vector copyInfos(copy.mipLevels, gfx::TextureCopy{}); gfx::Texture* srcTexture = resg.getTexture(srcID); gfx::Texture* dstTexture = resg.getTexture(dstID); @@ -551,13 +550,21 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { if (!srcTexture || !dstTexture) { return; } + const auto& srcInfo = srcTexture->getInfo(); const auto& dstInfo = dstTexture->getInfo(); CC_ENSURES(srcInfo.width == dstInfo.width); CC_ENSURES(srcInfo.height == dstInfo.height); CC_ENSURES(srcInfo.depth == dstInfo.depth); + CC_ENSURES(srcInfo.levelCount == dstInfo.levelCount); - for (uint32_t i = 0; i < copy.mipLevels; ++i) { + const uint32_t mipLevels = std::min( + copy.mipLevels, + srcTexture->getInfo().levelCount); + + std::vector copyInfos(mipLevels, gfx::TextureCopy{}); + + for (uint32_t i = 0; i < mipLevels; ++i) { auto& copyInfo = copyInfos[i]; copyInfo.srcSubres.mipLevel = copy.sourceMostDetailedMip + i; copyInfo.srcSubres.baseArrayLayer = copy.sourceFirstSlice; diff --git a/native/cocos/scene/ReflectionProbe.cpp b/native/cocos/scene/ReflectionProbe.cpp index 3eb4392fb5..bc8f7f003a 100644 --- a/native/cocos/scene/ReflectionProbe.cpp +++ b/native/cocos/scene/ReflectionProbe.cpp @@ -104,7 +104,6 @@ void ReflectionProbe::syncCameraParams(const Camera* camera) { _camera->setClearFlag(camera->getClearFlag()); _camera->setClearColor(camera->getClearColor()); _camera->setPriority(camera->getPriority() - 1); - _camera->changeTargetWindow(_realtimePlanarTexture->getWindow()); _camera->resize(camera->getWidth(), camera->getHeight()); } diff --git a/native/external-config.json b/native/external-config.json index 04ee6f3925..204dbb0f67 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos", "name": "cocos-engine-external", - "checkout": "v3.8.7-15" + "checkout": "v3.8.7-17" } } \ No newline at end of file diff --git a/native/gulpfile.js b/native/gulpfile.js index 82bf2df184..183f9ce4f4 100644 --- a/native/gulpfile.js +++ b/native/gulpfile.js @@ -75,7 +75,7 @@ gulp.task('gen-simulator', async function () { var args = []; args.push('-G'); if (isWin32) { - args.push('Visual Studio 16 2019','-A','x64'); + args.push('Visual Studio 17 2022','-A','x64'); } else { args.push('Xcode'); diff --git a/package-lock.json b/package-lock.json index ef0277de1c..cc6c2493ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "MIT", "dependencies": { "@babel/plugin-proposal-class-properties": "^7.18.6", - "@cocos/box2d": "1.0.1", + "@cocos/box2d": "1.0.2", "@cocos/cannon": "1.2.8", "@cocos/ccbuild": "^2.3.16", "@cocos/dragonbones-js": "^1.0.1", @@ -2048,9 +2048,9 @@ } }, "node_modules/@cocos/box2d": { - "version": "1.0.1", - "resolved": "https://registry.npmmirror.com/@cocos/box2d/-/box2d-1.0.1.tgz", - "integrity": "sha512-qNPK0KZEnEwMMtrj1f/p71FuuutmzXD0n1z6jLU3ntULk7EyNfnlsKUkAZEDVtr9nPmUvQozt/XquZ+biGxTCg==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@cocos/box2d/-/box2d-1.0.2.tgz", + "integrity": "sha512-5XTWfbSCuv9SoWaT0Xa3/jrKMWVd+txI8A/KfgCEyzQzLG0QckzfEC+7nb8cwOnzGWUI/cA0O0FnNr8V2Z/AZw==", "dependencies": { "@types/systemjs": "^0.20.6" } diff --git a/package.json b/package.json index 9c3d5b957e..3c09f57f33 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ }, "dependencies": { "@babel/plugin-proposal-class-properties": "^7.18.6", - "@cocos/box2d": "1.0.1", + "@cocos/box2d": "1.0.2", "@cocos/cannon": "1.2.8", "@cocos/ccbuild": "^2.3.16", "@cocos/dragonbones-js": "^1.0.1", diff --git a/platforms/minigame/platforms/taobao-mini-game/wrapper/fs-utils.js b/platforms/minigame/platforms/taobao-mini-game/wrapper/fs-utils.js index 76f1a24a2c..34687738aa 100644 --- a/platforms/minigame/platforms/taobao-mini-game/wrapper/fs-utils.js +++ b/platforms/minigame/platforms/taobao-mini-game/wrapper/fs-utils.js @@ -31,7 +31,7 @@ const fsUtils = { fs, isOutOfStorage (errorCode) { - return errorCode.toString() === outOfStorageErrorCode; + return errorCode != null && String(errorCode) === outOfStorageErrorCode; }, getUserDataPath () { diff --git a/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/index.d.ets b/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/index.d.ets index 4a80a73163..e149f44a0f 100644 --- a/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/index.d.ets +++ b/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/index.d.ets @@ -31,4 +31,7 @@ export interface context { } export const getContext: (type: ContextType) => context; + +// This function is thread-safe and can be called on the main thread. +// However, there is no return value when calling it on the main thread. export const evalString: (value: string) => boolean | number | string; \ No newline at end of file diff --git a/templates/harmonyos-next/entry/src/main/ets/components/CocosVideoPlayer.ets b/templates/harmonyos-next/entry/src/main/ets/components/CocosVideoPlayer.ets index 874953aed7..3ffec85b02 100644 --- a/templates/harmonyos-next/entry/src/main/ets/components/CocosVideoPlayer.ets +++ b/templates/harmonyos-next/entry/src/main/ets/components/CocosVideoPlayer.ets @@ -112,6 +112,8 @@ export struct CocosVideoPlayer { videoTag: this.videoInfo.viewTag, videoEvent: EventType.COMPLETED } as param); + //make it playable again. + this.videoInfo.controller.setCurrentTime(0, SeekMode.Accurate); }) .onPrepared((event): void => { this.videoInfo.isPreparedStart && this.videoInfo.controller.start(); diff --git a/templates/harmonyos-next/entry/src/main/ets/pages/index.ets b/templates/harmonyos-next/entry/src/main/ets/pages/index.ets index 96de8326f5..078ef5cade 100644 --- a/templates/harmonyos-next/entry/src/main/ets/pages/index.ets +++ b/templates/harmonyos-next/entry/src/main/ets/pages/index.ets @@ -307,7 +307,7 @@ struct Index { let index = this.videoIndexMap.get(tag) as number; this.videoArray[index].controller.pause(); }); - this.workPort?.on("pauseVideo",(tag: number) => { + this.workPort?.on("stopVideo",(tag: number) => { let index = this.videoIndexMap.get(tag) as number; this.videoArray[index].controller.stop(); }); diff --git a/templates/harmonyos-next/entry/src/main/ets/workers/cocos_worker.ets b/templates/harmonyos-next/entry/src/main/ets/workers/cocos_worker.ets index f1eb311c74..faae7727fb 100644 --- a/templates/harmonyos-next/entry/src/main/ets/workers/cocos_worker.ets +++ b/templates/harmonyos-next/entry/src/main/ets/workers/cocos_worker.ets @@ -110,8 +110,8 @@ uiPort.on("onErrorReceive", (viewTag: number, url: string) => { nativeWebView.failLoading(viewTag, url); }) -uiPort.on("onVideoEvent", (videoTag: number, videoEvent: number, arg?: AnyType ) => { - nativeVideo.onVideoEvent(JSON.stringify({videoTag: videoTag, videoEvent: videoEvent, arg: arg ?? null})); +uiPort.on("onVideoEvent", (videoTag: number, videoEvent: number, args?: AnyType ) => { + nativeVideo.onVideoEvent(JSON.stringify({videoTag: videoTag, videoEvent: videoEvent, args: args ?? null})); }) uiPort.on("backPress", () => {