webgpuShaderProcessors.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { IShaderProcessor } from '../Processors/iShaderProcessor';
  2. /** @hidden */
  3. export class WebGPUShaderProcessor implements IShaderProcessor {
  4. // attributeProcessor?: (attribute: string) => string;
  5. // varyingProcessor?: (varying: string, isFragment: boolean) => string;
  6. // uniformProcessor?: (uniform: string, isFragment: boolean) => string;
  7. // uniformBufferProcessor?: (uniformBuffer: string, isFragment: boolean) => string;
  8. // endOfUniformBufferProcessor?: (closingBracketLine: string, isFragment: boolean) => string;
  9. // lineProcessor?: (line: string, isFragment: boolean) => string;
  10. // preProcessor?: (code: string, defines: string[], isFragment: boolean) => string;
  11. // postProcessor?: (code: string, defines: string[], isFragment: boolean) => string;
  12. public attributeProcessor(attribute: string) {
  13. return attribute.replace("attribute", "in");
  14. // const inOut = isFragment ? "in" : "out";
  15. // const attribRegex = new RegExp(/\s+attribute\s+(\w+)\s+/gm);
  16. // let location = 0;
  17. // let match = attribRegex.exec(code);
  18. // while (match != null) {
  19. // if (match[1]) {
  20. // code = code.replace(match[0], ` layout(location = ${location}) ${inOut} ${match[1]} `);
  21. // location++;
  22. // }
  23. // match = attribRegex.exec(code);
  24. // }
  25. // return code;
  26. }
  27. public varyingProcessor(varying: string, isFragment: boolean) {
  28. return varying.replace("varying", isFragment ? "in" : "out");
  29. // const inOut = isFragment ? "in" : "out";
  30. // const attribRegex = new RegExp(/\s+varying\s+(\w+)\s+/gm);
  31. // let location = 0;
  32. // let match = attribRegex.exec(code);
  33. // while (match != null) {
  34. // if (match[1]) {
  35. // code = code.replace(match[0], ` layout(location = ${location}) ${inOut} ${match[1]} `);
  36. // location++;
  37. // }
  38. // match = attribRegex.exec(code);
  39. // }
  40. // return code;
  41. }
  42. public postProcessor(code: string, defines: string[], isFragment: boolean) {
  43. const hasDrawBuffersExtension = code.search(/#extension.+GL_EXT_draw_buffers.+require/) !== -1;
  44. // Remove extensions
  45. var regex = /#extension.+(GL_OVR_multiview2|GL_OES_standard_derivatives|GL_EXT_shader_texture_lod|GL_EXT_frag_depth|GL_EXT_draw_buffers).+(enable|require)/g;
  46. code = code.replace(regex, "");
  47. // Replace instructions
  48. code = code.replace(/texture2D\s*\(/g, "texture(");
  49. if (isFragment) {
  50. code = code.replace(/texture2DLodEXT\s*\(/g, "textureLod(");
  51. code = code.replace(/textureCubeLodEXT\s*\(/g, "textureLod(");
  52. code = code.replace(/textureCube\s*\(/g, "texture(");
  53. code = code.replace(/gl_FragDepthEXT/g, "gl_FragDepth");
  54. code = code.replace(/gl_FragColor/g, "glFragColor");
  55. code = code.replace(/gl_FragData/g, "glFragData");
  56. code = code.replace(/void\s+?main\s*\(/g, (hasDrawBuffersExtension ? "" : "layout(location = 0) out vec4 glFragColor;\n") + "void main(");
  57. } else {
  58. var hasMultiviewExtension = defines.indexOf("#define MULTIVIEW") !== -1;
  59. if (hasMultiviewExtension) {
  60. return "#extension GL_OVR_multiview2 : require\nlayout (num_views = 2) in;\n" + code;
  61. }
  62. }
  63. // Flip Y.
  64. if (!isFragment) {
  65. const lastClosingCurly = code.lastIndexOf("}");
  66. code = code.substring(0, lastClosingCurly);
  67. code += "gl_Position.y *= -1.; }";
  68. }
  69. return code;
  70. }
  71. }