-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse webgpu resources #18208
Open
GengineJS
wants to merge
36
commits into
cocos:v3.8.6
Choose a base branch
from
GengineJS:v3.8.6-0114
base: v3.8.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+503
−383
Open
Reuse webgpu resources #18208
Changes from 29 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
1af5121
Reuse webgpu layout
GengineJS f19da2e
ADAPTS the effect of new binding
GengineJS 2ee3be2
update
GengineJS 3d94289
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS 3be250f
update
GengineJS daf906f
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS f2cf144
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS 19039cc
New pipeline opening screen
GengineJS 71028c4
update
GengineJS 9c6c01e
add missing info
star-e 381104e
Merge pull request #2 from star-e/v3.8.6-0114-hyde
GengineJS c905efb
Fixed 2d texture mapping cubemap error
GengineJS ebc4fe8
update
GengineJS 46fc002
Fix the new pipeline referencing the meaningless globalDsManager object.
GengineJS 47c10eb
change storage image to sampler texture
star-e f3c21de
Merge pull request #3 from star-e/v3.8.6-0114-cube
GengineJS 6af7930
Merge branch 'v3.8.6-0114' of github.com:GengineJS/engine into v3.8.6…
GengineJS 0227211
Fixed the issue where the testlist was not displayed.
GengineJS 41d7dc4
fixed buffer storage usage warning
GengineJS 464d3ce
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS 59dbad8
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS 5af5597
fixed storage buffer usage warning
GengineJS e58edbd
update default storage buffer logic
GengineJS d2c67e8
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS 382fa03
fixed shadowmap
GengineJS 112f3ea
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS b58aad3
fixed texture not copydst
GengineJS 006ca59
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS 4fc304e
Fix webgpu import incompatibility problem
GengineJS b25bc55
Support dynamic import for WebGPU wasm
wdzhangchukong 53c7ddb
Merge pull request #4 from wdzhangchukong/v3.8.6-0114
GengineJS e75d92f
Fix the problem of dynamic buffer offset error.
GengineJS 60f5d04
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS ba9df78
Merge branch 'v3.8.6-0114' of github.com:GengineJS/engine into v3.8.6…
GengineJS 6fa7f75
update
GengineJS cc4917f
Merge branch 'v3.8.6' of github.com:cocos/cocos-engine into v3.8.6-0114
GengineJS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
Copyright (c) 2023 Xiamen Yaji Software Co., Ltd. | ||
https://www.cocos.com/ | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
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. | ||
*/ | ||
|
||
import { fetchBuffer, ensureWasmModuleReady, fetchUrl } from 'pal/wasm'; | ||
import { NATIVE_CODE_BUNDLE_MODE } from 'internal:constants'; | ||
import { error, sys } from '../../core'; | ||
import { NativeCodeBundleMode } from '../../misc/webassembly-support'; | ||
import { WebGPUWasm } from './webgpu-core'; | ||
import { overrideWebGPUDefine } from './define'; | ||
// import { overrideSpineDefine } from './spine-define'; | ||
|
||
const PAGESIZE = 65536; // 64KiB | ||
|
||
// How many pages of the wasm memory | ||
// TODO: let this can be canfiguable by user. | ||
const PAGECOUNT = 32 * 16; | ||
|
||
// How mush memory size of the wasm memory | ||
const MEMORYSIZE = PAGESIZE * PAGECOUNT; // 32 MiB | ||
|
||
let wasmInstance: WebGPUWasm.instance = null!; | ||
const registerList: any[] = []; | ||
|
||
function initWasm (wasmFactory, wasmUrl: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
const errorMessage = (err: any): string => `[WebGPU]: WebGPU wasm load failed: ${err}`; | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
fetchUrl(wasmUrl).then((currUrl) => { | ||
wasmFactory(currUrl).then((Instance: any) => { | ||
wasmInstance = Instance; | ||
registerList.forEach((cb) => { | ||
cb(wasmInstance); | ||
}); | ||
}).then(resolve).catch((err: any) => reject(errorMessage(err))); | ||
}); | ||
}); | ||
} | ||
|
||
function initAsmJS (asmFactory, asmJsMemUrl: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
fetchBuffer(asmJsMemUrl).then((arrayBuffer) => { | ||
const wasmMemory: any = {}; | ||
wasmMemory.buffer = new ArrayBuffer(MEMORYSIZE); | ||
const module = { | ||
wasmMemory, | ||
memoryInitializerRequest: { | ||
response: arrayBuffer, | ||
status: 200, | ||
} as Partial<XMLHttpRequest>, | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return asmFactory(module).then((instance: any) => { | ||
wasmInstance = instance; | ||
registerList.forEach((cb) => { | ||
cb(wasmInstance); | ||
}); | ||
}); | ||
}).then(resolve).catch(reject); | ||
}); | ||
} | ||
|
||
function shouldUseWasmModule (): boolean { | ||
if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.BOTH as number)) { | ||
return sys.hasFeature(sys.Feature.WASM); | ||
} else if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.WASM as number)) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
export async function waitForWebGPUWasmInstantiation (): Promise<void> { | ||
const errorReport = (msg: any): void => { | ||
error('Error during WebGPU Wasm instantiation:', msg); | ||
}; | ||
try { | ||
await ensureWasmModuleReady(); | ||
if (shouldUseWasmModule()) { | ||
const [ | ||
glslModule, | ||
glslWasmModule, | ||
twgslModule, | ||
twgslWasmModule, | ||
] = await Promise.all([ | ||
import('external:emscripten/webgpu/glslang.js'), | ||
import('external:emscripten/webgpu/glslang.wasm'), | ||
import('external:emscripten/webgpu/twgsl.js'), | ||
import('external:emscripten/webgpu/twgsl.wasm'), | ||
]); | ||
const glslFactory = glslModule.default; | ||
const glslWasmUrl = glslWasmModule.default; | ||
const twgslFactory = twgslModule.default; | ||
const twgslWasmUrl = twgslWasmModule.default; | ||
await initWasm(glslFactory, glslWasmUrl); | ||
await initWasm(twgslFactory, twgslWasmUrl); | ||
} else { | ||
throw new Error('Wasm module is not supported in this environment.'); | ||
} | ||
} catch (error) { | ||
errorReport(error); | ||
} | ||
} | ||
|
||
registerList.push(overrideWebGPUDefine); | ||
|
||
export const WEBGPU_WASM = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could load glsl and twgsl at the same time. They don't have any dependency, right ?
If so, we could use
Promise.all
to improve the loading.