forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shader): add shader module (Orillusion#38)
add glsl to wgsl converter add wgsl preprocessor
- Loading branch information
1 parent
646a2c2
commit b92c8b4
Showing
14 changed files
with
4,166 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export class SkeletonAnimation_shader { | ||
public static groupBindingAndFunctions(beginGroup: number, beginBinding: number) { | ||
return /* wgsl */ ` | ||
struct JointsMatrix { | ||
matrix : array<mat4x4<f32>> | ||
}; | ||
@group(${beginGroup}) @binding(${beginBinding}) | ||
var<storage, read> jointsMatrixIndexTable: array<f32>; | ||
@group(${beginGroup}) @binding(${beginBinding+1}) | ||
var<storage, read> jointsInverseMatrix: JointsMatrix; | ||
@group(${beginGroup}) @binding(${beginBinding+2}) | ||
var<storage, read> jointsIndexMapingTable: array<f32>; | ||
const MAX_JOINT_NUM = 8; | ||
fn getSkeletonWorldMatrix(joints: array<f32, MAX_JOINT_NUM>, weights: array<f32, MAX_JOINT_NUM>, num: u32) -> mat4x4<f32> { | ||
var result: mat4x4<f32>; | ||
for(var i: u32 = 0; i < num; i = i + 1) { | ||
let jointId = i32(joints[i]); | ||
let jointIndex = u32(jointsIndexMapingTable[jointId]); | ||
let jointMatrixIndex = u32(jointsMatrixIndexTable[jointIndex]); | ||
let joint = models.matrix[jointMatrixIndex] * jointsInverseMatrix.matrix[jointId] * weights[i]; | ||
result += joint; | ||
} | ||
return result; | ||
} | ||
fn getSkeletonWorldMatrix_4(joints: vec4<f32>, weights: vec4<f32>) -> mat4x4<f32> { | ||
return getSkeletonWorldMatrix(array<f32, MAX_JOINT_NUM>( | ||
joints.x, joints.y, joints.z, joints.w, | ||
0, 0, 0, 0, | ||
), array<f32, MAX_JOINT_NUM>( | ||
weights.x, weights.y, weights.z, weights.w, | ||
0, 0, 0, 0, | ||
), 4); | ||
} | ||
fn getSkeletonWorldMatrix_8(joints0: vec4<f32>, weights0: vec4<f32>, joints1: vec4<f32>, weights1: vec4<f32>) -> mat4x4<f32> { | ||
return getSkeletonWorldMatrix(array<f32, MAX_JOINT_NUM>( | ||
joints0.x, joints0.y, joints0.z, joints0.w, | ||
joints1.x, joints1.y, joints1.z, joints1.w, | ||
), array<f32, MAX_JOINT_NUM>( | ||
weights0.x, weights0.y, weights0.z, weights0.w, | ||
weights1.x, weights1.y, weights1.z, weights1.w, | ||
), 8); | ||
} | ||
`; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/engine/assets/shader/compute/BlurEffectCreator_compute.ts
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,100 @@ | ||
/** | ||
* @internal | ||
*/ | ||
export class BlurEffectCreator_compute { | ||
public static sample_rgba8unorm = /* wgsl */` | ||
struct ImageSize { | ||
srcWidth : i32, | ||
srcHeight : i32, | ||
dstWidth : i32, | ||
dstHeight : i32, | ||
}; | ||
@group(0) @binding(0) var<uniform> size : ImageSize; | ||
@group(0) @binding(1) var inputTexture : texture_2d<f32>; | ||
@group(0) @binding(2) var outputTexture : texture_storage_2d<rgba8unorm, write>; | ||
@compute @workgroup_size(8, 8, 1) | ||
fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { | ||
var uv:vec2<f32> = vec2<f32>(f32(GlobalInvocationID.x) / f32(size.dstWidth), f32(GlobalInvocationID.y) / f32(size.dstHeight)); | ||
uv = uv * vec2<f32>(f32(size.srcWidth), f32(size.srcHeight)); | ||
var dstId:vec2<i32> = vec2<i32>(i32(GlobalInvocationID.x), i32(GlobalInvocationID.y)); | ||
var srcId:vec2<i32> = vec2<i32>(i32(GlobalInvocationID.x * 2u), i32(GlobalInvocationID.y * 2u)); | ||
textureStore(outputTexture, dstId, textureLoad(inputTexture, srcId, 0)); | ||
} | ||
`; | ||
|
||
public static blur_rgba8unorm = /* wgsl */ ` | ||
struct ImageSize { | ||
srcWidth : i32, | ||
srcHeight : i32, | ||
dstWidth : i32, | ||
dstHeight : i32, | ||
}; | ||
@group(0) @binding(0) var<uniform> size : ImageSize; | ||
@group(0) @binding(1) var inputTexture : texture_2d<f32>; | ||
@group(0) @binding(2) var outputTexture : texture_storage_2d<rgba8unorm, write>; | ||
fn repeat_i32(id:i32, max:i32) -> i32 { | ||
var ret = id; | ||
if(id < 0) { | ||
ret = max + id; | ||
} | ||
if(id >= max) { | ||
ret = id - max; | ||
} | ||
return ret; | ||
} | ||
fn clamp_i32(id:i32, max:i32) -> i32 { | ||
var ret = id; | ||
if(id < 0) { | ||
ret = 0; | ||
} | ||
if(id >= max) { | ||
ret = max - 1; | ||
} | ||
return ret; | ||
} | ||
fn blur(idx:u32) -> vec4<f32> { | ||
var id:vec2<i32>; | ||
id.y = i32(idx) / size.srcWidth; | ||
id.x = i32(idx) - i32(id.y) * size.srcWidth; | ||
var _BlurSpread:i32 = 1; | ||
var result = vec4<f32>(0.0, 0.0, 0.0, 0.0); | ||
let g:array<f32, 3u> = array<f32, 3u>(0.4026, 0.2442, 0.0545); | ||
var uv:vec2<i32>; | ||
for (var h:i32 = 0; h < 5; h = h + 1) { | ||
let offsetU:i32 = (h - 2) * _BlurSpread; | ||
uv.x = id.x + offsetU; | ||
uv.x = clamp_i32(uv.x, size.srcWidth); | ||
for (var v:i32 = 0; v < 5; v = v + 1) { | ||
let offsetV:i32 = (v - 2) * _BlurSpread; | ||
uv.y = id.y + offsetV; | ||
uv.y = clamp(uv.y, 0, size.srcHeight); | ||
let weightU:i32 = abs(h - 2); | ||
let weightV:i32 = abs(v - 2); | ||
let resultWeight:f32 = g[weightU] * g[weightV]; | ||
var colorf32:vec4<f32> = textureLoad(inputTexture, uv, 0); | ||
let sampleColor:vec4<f32> = vec4<f32>(colorf32 * resultWeight); | ||
result = result + sampleColor; | ||
} | ||
} | ||
return result; | ||
} | ||
@compute @workgroup_size(8, 8, 1) | ||
fn main(@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>) { | ||
var uv:vec2<f32> = vec2<f32>(f32(GlobalInvocationID.x) / f32(size.dstWidth), f32(GlobalInvocationID.y) / f32(size.dstHeight)); | ||
uv = uv * vec2<f32>(f32(size.srcWidth), f32(size.srcHeight)); | ||
let srcIdx = i32(uv.y) * size.srcWidth + i32(uv.x); | ||
var dstId:vec2<i32> = vec2<i32>(i32(GlobalInvocationID.x), i32(GlobalInvocationID.y)); | ||
textureStore(outputTexture, dstId, blur(u32(srcIdx))); | ||
} | ||
`; | ||
} |
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,8 @@ | ||
/** | ||
* @internal | ||
*/ | ||
export enum ShaderStage { | ||
vertex, | ||
fragment, | ||
computer, | ||
} |
Oops, something went wrong.