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

Migrate shader-shaderlab from engine-toolkit #2467

Merged
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
1 change: 1 addition & 0 deletions packages/shader-shaderlab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A subpackage of `@galacean/engine`.
36 changes: 36 additions & 0 deletions packages/shader-shaderlab/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@galacean/engine-shader-shaderlab",
"version": "1.4.0-alpha.0",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"repository": {
"url": "https://github.com/galacean/engine.git"
},
"license": "MIT",
"main": "dist/main.js",
"module": "dist/module.js",
"debug": "src/index.ts",
"browser": "dist/browser.js",
"types": "types/index.d.ts",
"scripts": {
"b:types": "tsc"
},
"umd": {
"name": "Galacean.ShaderShaderLab",
"globals": {
"@galacean/engine": "Galacean"
}
},
"files": [
"dist/**/*",
"types/**/*"
],
"devDependencies": {
"@galacean/engine": "workspace:*"
},
"peerDependencies": {
"@galacean/engine": "workspace:*"
}
}
9 changes: 9 additions & 0 deletions packages/shader-shaderlab/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "*.glsl" {
const value: string;
export default value;
}

declare module "*.gs" {
const value: string;
export default value;
}
28 changes: 28 additions & 0 deletions packages/shader-shaderlab/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Shader, ShaderFactory } from "@galacean/engine";
import { PBRSource, fragmentList } from "./shaders";

let includeRegistered = false;
let shaderRegistered = false;

export function registerIncludes() {
if (includeRegistered) return;

for (const sourceFragment of fragmentList) {
ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source);
}

includeRegistered = true;
}

export function registerShader() {
if (shaderRegistered) return;

Shader.create(PBRSource);

shaderRegistered = true;
}
Comment on lines +17 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure includes are registered before creating shaders

The function registerShader() calls Shader.create(PBRSource) without ensuring that registerIncludes() has been invoked. If registerShader() is called before registerIncludes(), it may lead to missing shader includes and runtime errors. To prevent this, consider invoking registerIncludes() at the beginning of registerShader().

Apply this diff to fix the issue:

export function registerShader() {
+  registerIncludes();
  if (shaderRegistered) return;

  Shader.create(PBRSource);

  shaderRegistered = true;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function registerShader() {
if (shaderRegistered) return;
Shader.create(PBRSource);
shaderRegistered = true;
}
export function registerShader() {
registerIncludes();
if (shaderRegistered) return;
Shader.create(PBRSource);
shaderRegistered = true;
}


/**
* @internal
*/
export { fragmentList };
105 changes: 105 additions & 0 deletions packages/shader-shaderlab/src/shaders/BlendShape.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#ifndef BLENDSHAPE_INCLUDED
#define BLENDSHAPE_INCLUDED

#ifdef RENDERER_HAS_BLENDSHAPE
#ifdef RENDERER_BLENDSHAPE_USE_TEXTURE
mediump sampler2DArray renderer_BlendShapeTexture;
ivec3 renderer_BlendShapeTextureInfo;
float renderer_BlendShapeWeights[RENDERER_BLENDSHAPE_COUNT];

vec3 getBlendShapeVertexElement(int blendShapeIndex, int vertexElementIndex){
int y = vertexElementIndex / renderer_BlendShapeTextureInfo.y;
int x = vertexElementIndex - y * renderer_BlendShapeTextureInfo.y;
ivec3 uv = ivec3(x, y , blendShapeIndex);
return (texelFetch(renderer_BlendShapeTexture, uv, 0)).xyz;
}
#else
#if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT )
float renderer_BlendShapeWeights[2];
#else
#if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT )
float renderer_BlendShapeWeights[4];
#else
float renderer_BlendShapeWeights[8];
#endif
#endif
#endif

void calculateBlendShape(Attributes attributes, inout vec4 position
#ifdef RENDERER_HAS_NORMAL
,inout vec3 normal
#ifdef RENDERER_HAS_TANGENT
,inout vec4 tangent
#endif
#endif

){
#ifdef RENDERER_BLENDSHAPE_USE_TEXTURE
int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x;
for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){
int vertexElementOffset = vertexOffset;
float weight = renderer_BlendShapeWeights[i];
// Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13
if(weight != 0.0){
position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight;

#if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL )
vertexElementOffset += 1;
normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight;
#endif

#if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT)
vertexElementOffset += 1;
tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight;
#endif
}

}
#else
position.xyz += attributes.POSITION_BS0 * renderer_BlendShapeWeights[0];
position.xyz += attributes.POSITION_BS1 * renderer_BlendShapeWeights[1];

#if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT )
#ifdef RENDERER_HAS_NORMAL
normal += attributes.NORMAL_BS0 * renderer_BlendShapeWeights[0];
normal += attributes.NORMAL_BS1 * renderer_BlendShapeWeights[1];
#endif

#ifdef RENDERER_HAS_TANGENT
tangent.xyz += attributes.TANGENT_BS0 * renderer_BlendShapeWeights[0];
tangent.xyz += attributes.TANGENT_BS1 * renderer_BlendShapeWeights[1];
#endif
#else
#if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT )
position.xyz += attributes.POSITION_BS2 * renderer_BlendShapeWeights[2];
position.xyz += attributes.POSITION_BS3 * renderer_BlendShapeWeights[3];

#if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL )
normal += attributes.NORMAL_BS0 * renderer_BlendShapeWeights[0];
normal += attributes.NORMAL_BS1 * renderer_BlendShapeWeights[1];
normal += attributes.NORMAL_BS2 * renderer_BlendShapeWeights[2];
normal += attributes.NORMAL_BS3 * renderer_BlendShapeWeights[3];
#endif

#if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT )
tangent.xyz += attributes.TANGENT_BS0 * renderer_BlendShapeWeights[0];
tangent.xyz += attributes.TANGENT_BS1 * renderer_BlendShapeWeights[1];
tangent.xyz += attributes.TANGENT_BS2 * renderer_BlendShapeWeights[2];
tangent.xyz += attributes.TANGENT_BS3 * renderer_BlendShapeWeights[3];
#endif
#else
position.xyz += attributes.POSITION_BS2 * renderer_BlendShapeWeights[2];
position.xyz += attributes.POSITION_BS3 * renderer_BlendShapeWeights[3];
position.xyz += attributes.POSITION_BS4 * renderer_BlendShapeWeights[4];
position.xyz += attributes.POSITION_BS5 * renderer_BlendShapeWeights[5];
position.xyz += attributes.POSITION_BS6 * renderer_BlendShapeWeights[6];
position.xyz += attributes.POSITION_BS7 * renderer_BlendShapeWeights[7];
#endif
#endif
#endif
}

#endif


#endif
101 changes: 101 additions & 0 deletions packages/shader-shaderlab/src/shaders/Common.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef COMMON_INCLUDED
#define COMMON_INCLUDED

#define PI 3.14159265359
#define RECIPROCAL_PI 0.31830988618
#define EPSILON 1e-6
#define LOG2 1.442695

#define saturate( a ) clamp( a, 0.0, 1.0 )

float pow2(float x ) {
return x * x;
}

vec4 RGBMToLinear(vec4 value, float maxRange ) {
return vec4( value.rgb * value.a * maxRange, 1.0 );
}

vec4 gammaToLinear(vec4 srgbIn){
return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a);
}

vec4 linearToGamma(vec4 linearIn){
linearIn = max(linearIn, 0.0);
return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a);
}

vec4 camera_DepthBufferParams;

float remapDepthBufferLinear01(float z){
return 1.0/ (camera_DepthBufferParams.x * z + camera_DepthBufferParams.y);
}


#ifdef GRAPHICS_API_WEBGL2
#define INVERSE_MAT(mat) inverse(mat)
#else
mat2 inverseMat(mat2 m) {
return mat2(m[1][1],-m[0][1],
-m[1][0], m[0][0]) / (m[0][0]*m[1][1] - m[0][1]*m[1][0]);
}
mat3 inverseMat(mat3 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];

float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;

float det = a00 * b01 + a01 * b11 + a02 * b21;

return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}
mat4 inverseMat(mat4 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],
a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],

b00 = a00 * a11 - a01 * a10,
b01 = a00 * a12 - a02 * a10,
b02 = a00 * a13 - a03 * a10,
b03 = a01 * a12 - a02 * a11,
b04 = a01 * a13 - a03 * a11,
b05 = a02 * a13 - a03 * a12,
b06 = a20 * a31 - a21 * a30,
b07 = a20 * a32 - a22 * a30,
b08 = a20 * a33 - a23 * a30,
b09 = a21 * a32 - a22 * a31,
b10 = a21 * a33 - a23 * a31,
b11 = a22 * a33 - a23 * a32,

det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;

return mat4(
a11 * b11 - a12 * b10 + a13 * b09,
a02 * b10 - a01 * b11 - a03 * b09,
a31 * b05 - a32 * b04 + a33 * b03,
a22 * b04 - a21 * b05 - a23 * b03,
a12 * b08 - a10 * b11 - a13 * b07,
a00 * b11 - a02 * b08 + a03 * b07,
a32 * b02 - a30 * b05 - a33 * b01,
a20 * b05 - a22 * b02 + a23 * b01,
a10 * b10 - a11 * b08 + a13 * b06,
a01 * b08 - a00 * b10 - a03 * b06,
a30 * b04 - a31 * b02 + a33 * b00,
a21 * b02 - a20 * b04 - a23 * b00,
a11 * b07 - a10 * b09 - a12 * b06,
a00 * b09 - a01 * b07 + a02 * b06,
a31 * b01 - a30 * b03 - a32 * b00,
a20 * b03 - a21 * b01 + a22 * b00) / det;
}

#define INVERSE_MAT(mat) inverseMat(mat)
#endif


#endif
30 changes: 30 additions & 0 deletions packages/shader-shaderlab/src/shaders/Fog.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef FOG_INCLUDED
#define FOG_INCLUDED

#if SCENE_FOG_MODE != 0
vec4 scene_FogColor;
vec4 scene_FogParams; // (-1/(end-start), end/(end-start), density/ln(2),density/sprt(ln(2)));

vec4 fog(vec4 color, vec3 positionVS){
float fogDepth = length(positionVS);

#if SCENE_FOG_MODE == 1
// (end-z) / (end-start) = z * (-1/(end-start)) + (end/(end-start))
float fogIntensity = clamp(fogDepth * scene_FogParams.x + scene_FogParams.y, 0.0, 1.0);
#elif SCENE_FOG_MODE == 2
// exp(-z * density) = exp2((-z * density)/ln(2)) = exp2(-z * density/ln(2))
float fogIntensity = clamp(exp2(-fogDepth * scene_FogParams.z), 0.0, 1.0);
#elif SCENE_FOG_MODE == 3
// exp(-(z * density)^2) = exp2(-(z * density)^2/ln(2)) = exp2(-(z * density/sprt(ln(2)))^2)
float factor = fogDepth * scene_FogParams.w;
float fogIntensity = clamp(exp2(-factor * factor), 0.0, 1.0);
#endif

color.rgb = mix(scene_FogColor.rgb, color.rgb, fogIntensity);

return color;
}
#endif


#endif
Loading
Loading