• 轻量封装WebGPU渲染系统示例<18>- 材质多pass实现GPU Compute计算(源码)


    当前示例源码github地址:

    https://github.com/vilyLei/voxwebgpu/blob/feature/rendering/src/voxgpu/sample/ComputeMaterialTest.ts

    系统特性:

    1. 用户态与系统态隔离。

             细节请见:引擎系统设计思路 - 用户态与系统态隔离-CSDN博客

    2. 高频调用与低频调用隔离。

    3. 面向用户的易用性封装。

    4. 渲染数据(内外部相关资源)和渲染机制分离。

    5. 用户操作和渲染系统调度并行机制。

    6. 数据/语义驱动。

    7. 异步并行的场景/模型载入。

    8. computing与rendering用法机制一致性。

            1). 构造过程一致性。

            2). 启用过程一致性。

            3). 自动兼容到material多pass以及material graph机制中。

    此示例基于此渲染系统实现,当前示例TypeScript源码如下:

    1. const gridSize = 32;
    2. const shdWorkGroupSize = 8;
    3. // an example compute shader
    4. const compShdCode0 = `
    5. @group(0) @binding(0) var grid: vec2f;
    6. @group(0) @binding(1) var cellStateIn: array;
    7. @group(0) @binding(2) var cellStateOut: array;
    8. fn cellIndex(cell: vec2u) -> u32 {
    9. return cell.y * u32(grid.x) + cell.x;
    10. }
    11. @compute @workgroup_size(${shdWorkGroupSize}, ${shdWorkGroupSize})
    12. fn compMain(@builtin(global_invocation_id) cell: vec3u) {
    13. if (cellStateIn[cellIndex(cell.xy)] == 1) {
    14. cellStateOut[cellIndex(cell.xy)] = 0;
    15. } else {
    16. cellStateOut[cellIndex(cell.xy)] = 1;
    17. }
    18. }`;
    19. // an example compute shader
    20. const compShdCode1 = `
    21. @group(0) @binding(0) var grid: vec2f;
    22. @group(0) @binding(1) var cellStateIn: array;
    23. @group(0) @binding(2) var cellStateOut: array;
    24. fn cellIndex(cell: vec2u) -> u32 {
    25. return cell.y * u32(grid.x) + cell.x;
    26. }
    27. @compute @workgroup_size(${shdWorkGroupSize}, ${shdWorkGroupSize})
    28. fn compMain(@builtin(global_invocation_id) cell: vec3u) {
    29. if (cellStateIn[cellIndex(cell.xy)]%5 == 0) {
    30. cellStateOut[cellIndex(cell.xy)] = 0;
    31. } else {
    32. cellStateOut[cellIndex(cell.xy)] = 1;
    33. }
    34. }`;
    35. export class ComputeMaterialTest {
    36. private mRscene = new RendererScene();
    37. initialize(): void {
    38. console.log("ComputeMaterialTest::initialize() ...");
    39. this.initScene();
    40. }
    41. private createUniformValues(): WGRUniformValue[] {
    42. const gridsSizesArray = new Float32Array([gridSize, gridSize]);
    43. const cellStateArray0 = new Uint32Array(gridSize * gridSize);
    44. for (let i = 0; i < cellStateArray0.length; i += 3) {
    45. cellStateArray0[i] = 1;
    46. }
    47. const cellStateArray1 = new Uint32Array(gridSize * gridSize);
    48. for (let i = 0; i < cellStateArray1.length; i++) {
    49. cellStateArray1[i] = i % 2;
    50. }
    51. const v0 = new WGRUniformValue({ data: gridsSizesArray, stride: 2 }).toVisibleAll();
    52. const v1 = new WGRStorageValue({ data: cellStateArray0, stride: 1 }).toVisibleVertComp();
    53. const v2 = new WGRStorageValue({ data: cellStateArray1, stride: 1 }).toVisibleComp();
    54. v2.toBufferForStorage();
    55. return [v0, v1, v2];
    56. }
    57. private createMaterial(shaderCodeSrc: WGRShderSrcType, shadinguuid: string): WGCompMaterial {
    58. const uniformValues = this.createUniformValues();
    59. const workcounts = [4, 4];
    60. return new WGCompMaterial({ shadinguuid, shaderCodeSrc, uniformValues, workcounts });
    61. }
    62. private initScene(): void {
    63. const rc = this.mRscene;
    64. let shaderCodeSrc0 = { code: compShdCode0, uuid: "computing-0" };
    65. let shaderCodeSrc1 = { code: compShdCode1, uuid: "computing-1" };
    66. let materials = [this.createMaterial(shaderCodeSrc0, "comp-1"), this.createMaterial(shaderCodeSrc1, "comp-2")];
    67. rc.addEntity(new ComputeEntity({ materials }));
    68. }
    69. run(): void {
    70. this.mRscene.run();
    71. }
    72. }

  • 相关阅读:
    npm发布自己的包
    MySQL8.0优化 - SQL执行流程
    python之客户端和服务端的例子
    maven仓库改国内源
    57 最长递增子序列
    openfeign原理
    源码解析---net包
    聊聊平板相控阵天线
    数据赋能(67)——概念:数据变现
    Grafana安装和配置Prometheus数据源教程
  • 原文地址:https://blog.csdn.net/vily_lei/article/details/134253978