Skip to content
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

Improve gpu particle performance #9847

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cocos/core/builtin/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ export const effects = [
"shaders": [
{
"name": "particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:add",
"hash": 1813246314,
"hash": 1250077034,
"builtins": {
"statistics": { "CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS": 63, "CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS": 40 },
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCCamera", "defines": [] }], "samplerTextures": [], "buffers": [], "images": [] },
"locals": { "blocks": [{ "name": "CCLocal", "defines": [] }], "samplerTextures": [], "buffers": [], "images": [] }
},
"defines": [
{ "name": "CC_RENDER_MODE", "type": "number", "range": [0, 4] },
{ "name": "USE_VK_SHADER", "type": "boolean" },
{ "name": "COLOR_OVER_TIME_MODULE_ENABLE", "type": "boolean" },
{ "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean" },
{ "name": "SIZE_OVER_TIME_MODULE_ENABLE", "type": "boolean" },
Expand Down
2 changes: 1 addition & 1 deletion cocos/core/builtin/shader-sources/glsl1.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cocos/core/builtin/shader-sources/glsl3.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cocos/core/builtin/shader-sources/glsl4.ts

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion cocos/particle/renderer/particle-system-renderer-gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { builtinResMgr } from '../../core/builtin';
import { Material } from '../../core/assets';
import { Texture2D } from '../../core';
import { Component } from '../../core/components';
import { AttributeName, Format, Attribute } from '../../core/gfx';
import { AttributeName, Format, Attribute, API } from '../../core/gfx';
import { Mat4, Vec2, Vec4, Quat, Vec3 } from '../../core/math';
import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance';
import { MacroRecord } from '../../core/renderer/core/pass-utils';
Expand All @@ -39,6 +39,7 @@ import { Pass } from '../../core/renderer/core/pass';
import { packCurveRangeXYZ, packCurveRangeZ, packCurveRangeXYZW, packCurveRangeN, packCurveRangeXY } from '../animator/curve-range';
import { ParticleSystemRendererBase } from './particle-system-renderer-base';
import { Camera } from '../../core/renderer/scene/camera';
import { legacyCC } from '../../core/global-exports';

const _tempWorldTrans = new Mat4();
const _tempVec4 = new Vec4();
Expand All @@ -64,6 +65,7 @@ const SIZE_OVER_TIME_MODULE_ENABLE = 'SIZE_OVER_TIME_MODULE_ENABLE';
const VELOCITY_OVER_TIME_MODULE_ENABLE = 'VELOCITY_OVER_TIME_MODULE_ENABLE';
const FORCE_OVER_TIME_MODULE_ENABLE = 'FORCE_OVER_TIME_MODULE_ENABLE';
const TEXTURE_ANIMATION_MODULE_ENABLE = 'TEXTURE_ANIMATION_MODULE_ENABLE';
const USE_VK_SHADER = 'USE_VK_SHADER';

const _vert_attr_name = {
POSITION_STARTTIME: 'a_position_starttime',
Expand Down Expand Up @@ -436,6 +438,8 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
_tempVec4.z = textureModule.cycleCount;
pass.setUniform(infoHandle, _tempVec4);
}

this._defines[USE_VK_SHADER] = legacyCC.game._gfxDevice.gfxAPI === API.VULKAN;
}

public getParticleCount (): number {
Expand Down
6 changes: 6 additions & 0 deletions editor/assets/chunks/particle-vs-gpu.chunk
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ vec4 scaleAndAdd (vec4 a, vec4 b, float scale) {

// Vulkan compatible issue#9858, do not use large number (more than 100) in mod function
float pseudoRandom(float x) {
#if USE_VK_SHADER
float o = x;
x = mod(x - 1.0, 2.0) - 1.0;
float freqVar = 10.16640753482; //3.1415927*(sqrt(5.0)+1.0);
Expand All @@ -86,6 +87,11 @@ float pseudoRandom(float x) {
v *= 0.7071067812; // fix sheared 'distance' measurements - but this is 1D so unsure it's even a good thing to try
v = y < 0.0 ? -v : v;
return v;
#else
float seed = mod(x, 233280.);
float q = (seed * 9301. + 49297.) / 233280.;
return fract(q);
#endif
}

#if COLOR_OVER_TIME_MODULE_ENABLE
Expand Down