offline-mappings.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* eslint-disable camelcase */
  2. import {
  3. Address, BlendFactor, BlendOp, ColorMask, ComparisonFunc, CullMode, DynamicStateFlagBit,
  4. Filter, Format, FormatInfos, FormatType, GetTypeSize, PolygonMode, PrimitiveMode,
  5. ShadeModel, ShaderStageFlagBit, StencilOp, Type, DescriptorType, SamplerInfo, MemoryAccessBit, Sampler,
  6. } from '../../cocos/gfx';
  7. import { RenderPassStage, RenderPriority, SetIndex } from '../../cocos/rendering/define';
  8. import { murmurhash2_32_gc } from '../../cocos/core';
  9. const typeMap: Record<string, Type | string> = {};
  10. typeMap[typeMap.bool = Type.BOOL] = 'bool';
  11. typeMap[typeMap.bvec2 = Type.BOOL2] = 'bvec2';
  12. typeMap[typeMap.bvec3 = Type.BOOL3] = 'bvec3';
  13. typeMap[typeMap.bvec4 = Type.BOOL4] = 'bvec4';
  14. typeMap[typeMap.int = Type.INT] = 'int';
  15. typeMap[typeMap.ivec2 = Type.INT2] = 'ivec2';
  16. typeMap[typeMap.ivec3 = Type.INT3] = 'ivec3';
  17. typeMap[typeMap.ivec4 = Type.INT4] = 'ivec4';
  18. typeMap[typeMap.uint = Type.UINT] = 'uint';
  19. typeMap[typeMap.uvec2 = Type.UINT2] = 'uvec2';
  20. typeMap[typeMap.uvec3 = Type.UINT3] = 'uvec3';
  21. typeMap[typeMap.uvec4 = Type.UINT4] = 'uvec4';
  22. typeMap[typeMap.float = Type.FLOAT] = 'float';
  23. typeMap[typeMap.vec2 = Type.FLOAT2] = 'vec2';
  24. typeMap[typeMap.vec3 = Type.FLOAT3] = 'vec3';
  25. typeMap[typeMap.vec4 = Type.FLOAT4] = 'vec4';
  26. typeMap[typeMap.mat2 = Type.MAT2] = 'mat2';
  27. typeMap[typeMap.mat3 = Type.MAT3] = 'mat3';
  28. typeMap[typeMap.mat4 = Type.MAT4] = 'mat4';
  29. typeMap[typeMap.mat2x3 = Type.MAT2X3] = 'mat2x3';
  30. typeMap[typeMap.mat2x4 = Type.MAT2X4] = 'mat2x4';
  31. typeMap[typeMap.mat3x2 = Type.MAT3X2] = 'mat3x2';
  32. typeMap[typeMap.mat3x4 = Type.MAT3X4] = 'mat3x4';
  33. typeMap[typeMap.mat4x2 = Type.MAT4X2] = 'mat4x2';
  34. typeMap[typeMap.mat4x3 = Type.MAT4X3] = 'mat4x3';
  35. typeMap[typeMap.sampler1D = Type.SAMPLER1D] = 'sampler1D';
  36. typeMap[typeMap.sampler1DArray = Type.SAMPLER1D_ARRAY] = 'sampler1DArray';
  37. typeMap[typeMap.sampler2D = Type.SAMPLER2D] = 'sampler2D';
  38. typeMap[typeMap.sampler2DArray = Type.SAMPLER2D_ARRAY] = 'sampler2DArray';
  39. typeMap[typeMap.sampler3D = Type.SAMPLER3D] = 'sampler3D';
  40. typeMap[typeMap.samplerCube = Type.SAMPLER_CUBE] = 'samplerCube';
  41. typeMap[typeMap.sampler = Type.SAMPLER] = 'sampler';
  42. typeMap[typeMap.texture1D = Type.TEXTURE1D] = 'texture1D';
  43. typeMap[typeMap.texture1DArray = Type.TEXTURE1D_ARRAY] = 'texture1DArray';
  44. typeMap[typeMap.texture2D = Type.TEXTURE2D] = 'texture2D';
  45. typeMap[typeMap.texture2DArray = Type.TEXTURE2D_ARRAY] = 'texture2DArray';
  46. typeMap[typeMap.texture3D = Type.TEXTURE3D] = 'texture3D';
  47. typeMap[typeMap.textureCube = Type.TEXTURE_CUBE] = 'textureCube';
  48. typeMap[typeMap.image1D = Type.IMAGE1D] = 'image1D';
  49. typeMap[typeMap.image1DArray = Type.IMAGE1D_ARRAY] = 'image1DArray';
  50. typeMap[typeMap.image2D = Type.IMAGE2D] = 'image2D';
  51. typeMap[typeMap.image2DArray = Type.IMAGE2D_ARRAY] = 'image2DArray';
  52. typeMap[typeMap.image3D = Type.IMAGE3D] = 'image3D';
  53. typeMap[typeMap.imageCube = Type.IMAGE_CUBE] = 'imageCube';
  54. typeMap[typeMap.subpassInput = Type.SUBPASS_INPUT] = 'subpassInput';
  55. // variations
  56. typeMap.int8_t = Type.INT;
  57. typeMap.i8vec2 = Type.INT2;
  58. typeMap.i8vec3 = Type.INT3;
  59. typeMap.i8vec4 = Type.INT4;
  60. typeMap.uint8_t = Type.UINT;
  61. typeMap.u8vec2 = Type.UINT2;
  62. typeMap.u8vec3 = Type.UINT3;
  63. typeMap.u8vec4 = Type.UINT4;
  64. typeMap.int16_t = Type.INT;
  65. typeMap.i16vec2 = Type.INT2;
  66. typeMap.i16vec3 = Type.INT3;
  67. typeMap.i16vec4 = Type.INT4;
  68. typeMap.uint16_t = Type.INT;
  69. typeMap.u16vec2 = Type.UINT2;
  70. typeMap.u16vec3 = Type.UINT3;
  71. typeMap.u16vec4 = Type.UINT4;
  72. typeMap.float16_t = Type.FLOAT;
  73. typeMap.f16vec2 = Type.FLOAT2;
  74. typeMap.f16vec3 = Type.FLOAT3;
  75. typeMap.f16vec4 = Type.FLOAT4;
  76. typeMap.mat2x2 = Type.MAT2;
  77. typeMap.mat3x3 = Type.MAT3;
  78. typeMap.mat4x4 = Type.MAT4;
  79. typeMap.isampler1D = Type.SAMPLER1D;
  80. typeMap.usampler1D = Type.SAMPLER1D;
  81. typeMap.sampler1DShadow = Type.SAMPLER1D;
  82. typeMap.isampler1DArray = Type.SAMPLER1D_ARRAY;
  83. typeMap.usampler1DArray = Type.SAMPLER1D_ARRAY;
  84. typeMap.sampler1DArrayShadow = Type.SAMPLER1D_ARRAY;
  85. typeMap.isampler2D = Type.SAMPLER2D;
  86. typeMap.usampler2D = Type.SAMPLER2D;
  87. typeMap.sampler2DShadow = Type.SAMPLER2D;
  88. typeMap.isampler2DArray = Type.SAMPLER2D_ARRAY;
  89. typeMap.usampler2DArray = Type.SAMPLER2D_ARRAY;
  90. typeMap.sampler2DArrayShadow = Type.SAMPLER2D_ARRAY;
  91. typeMap.isampler3D = Type.SAMPLER3D;
  92. typeMap.usampler3D = Type.SAMPLER3D;
  93. typeMap.isamplerCube = Type.SAMPLER_CUBE;
  94. typeMap.usamplerCube = Type.SAMPLER_CUBE;
  95. typeMap.samplerCubeShadow = Type.SAMPLER_CUBE;
  96. typeMap.iimage2D = Type.IMAGE2D;
  97. typeMap.uimage2D = Type.IMAGE2D;
  98. typeMap.usubpassInput = Type.SUBPASS_INPUT;
  99. typeMap.isubpassInput = Type.SUBPASS_INPUT;
  100. const isSampler = (type) => type >= Type.SAMPLER1D;
  101. const isPaddedMatrix = (type) => type >= Type.MAT2 && type < Type.MAT4;
  102. const formatMap = {
  103. bool: Format.R8,
  104. bvec2: Format.RG8,
  105. bvec3: Format.RGB8,
  106. bvec4: Format.RGBA8,
  107. int: Format.R32I,
  108. ivec2: Format.RG32I,
  109. ivec3: Format.RGB32I,
  110. ivec4: Format.RGBA32I,
  111. uint: Format.R32UI,
  112. uvec2: Format.RG32UI,
  113. uvec3: Format.RGB32UI,
  114. uvec4: Format.RGBA32UI,
  115. float: Format.R32F,
  116. vec2: Format.RG32F,
  117. vec3: Format.RGB32F,
  118. vec4: Format.RGBA32F,
  119. int8_t: Format.R8I,
  120. i8vec2: Format.RG8I,
  121. i8vec3: Format.RGB8I,
  122. i8vec4: Format.RGBA8I,
  123. uint8_t: Format.R8UI,
  124. u8vec2: Format.RG8UI,
  125. u8vec3: Format.RGB8UI,
  126. u8vec4: Format.RGBA8UI,
  127. int16_t: Format.R16I,
  128. i16vec2: Format.RG16I,
  129. i16vec3: Format.RGB16I,
  130. i16vec4: Format.RGBA16I,
  131. uint16_t: Format.R16UI,
  132. u16vec2: Format.RG16UI,
  133. u16vec3: Format.RGB16UI,
  134. u16vec4: Format.RGBA16UI,
  135. float16_t: Format.R16F,
  136. f16vec2: Format.RG16F,
  137. f16vec3: Format.RGB16F,
  138. f16vec4: Format.RGBA16F,
  139. // no suitable conversions:
  140. mat2: Format.RGBA32F,
  141. mat3: Format.RGBA32F,
  142. mat4: Format.RGBA32F,
  143. mat2x2: Format.RGBA32F,
  144. mat3x3: Format.RGBA32F,
  145. mat4x4: Format.RGBA32F,
  146. mat2x3: Format.RGBA32F,
  147. mat2x4: Format.RGBA32F,
  148. mat3x2: Format.RGBA32F,
  149. mat3x4: Format.RGBA32F,
  150. mat4x2: Format.RGBA32F,
  151. mat4x3: Format.RGBA32F,
  152. };
  153. const getFormat = (name: string) => Format[name.toUpperCase()];
  154. const getShaderStage = (name: string) => ShaderStageFlagBit[name.toUpperCase()];
  155. const getDescriptorType = (name: string) => DescriptorType[name.toUpperCase()];
  156. const isNormalized = (format: string) => {
  157. const type = FormatInfos[format] && FormatInfos[format].type;
  158. return type === FormatType.UNORM || type === FormatType.SNORM;
  159. };
  160. const getMemoryAccessFlag = (access: string) => {
  161. if (access === 'writeonly') { return MemoryAccessBit.WRITE_ONLY; }
  162. if (access === 'readonly') { return MemoryAccessBit.READ_ONLY; }
  163. return MemoryAccessBit.READ_WRITE;
  164. };
  165. const passParams = {
  166. // color mask
  167. NONE: ColorMask.NONE,
  168. R: ColorMask.R,
  169. G: ColorMask.G,
  170. B: ColorMask.B,
  171. A: ColorMask.A,
  172. RG: ColorMask.R | ColorMask.G,
  173. RB: ColorMask.R | ColorMask.B,
  174. RA: ColorMask.R | ColorMask.A,
  175. GB: ColorMask.G | ColorMask.B,
  176. GA: ColorMask.G | ColorMask.A,
  177. BA: ColorMask.B | ColorMask.A,
  178. RGB: ColorMask.R | ColorMask.G | ColorMask.B,
  179. RGA: ColorMask.R | ColorMask.G | ColorMask.A,
  180. RBA: ColorMask.R | ColorMask.B | ColorMask.A,
  181. GBA: ColorMask.G | ColorMask.B | ColorMask.A,
  182. ALL: ColorMask.ALL,
  183. // blend operation
  184. ADD: BlendOp.ADD,
  185. SUB: BlendOp.SUB,
  186. REV_SUB: BlendOp.REV_SUB,
  187. MIN: BlendOp.MIN,
  188. MAX: BlendOp.MAX,
  189. // blend factor
  190. ZERO: BlendFactor.ZERO,
  191. ONE: BlendFactor.ONE,
  192. SRC_ALPHA: BlendFactor.SRC_ALPHA,
  193. DST_ALPHA: BlendFactor.DST_ALPHA,
  194. ONE_MINUS_SRC_ALPHA: BlendFactor.ONE_MINUS_SRC_ALPHA,
  195. ONE_MINUS_DST_ALPHA: BlendFactor.ONE_MINUS_DST_ALPHA,
  196. SRC_COLOR: BlendFactor.SRC_COLOR,
  197. DST_COLOR: BlendFactor.DST_COLOR,
  198. ONE_MINUS_SRC_COLOR: BlendFactor.ONE_MINUS_SRC_COLOR,
  199. ONE_MINUS_DST_COLOR: BlendFactor.ONE_MINUS_DST_COLOR,
  200. SRC_ALPHA_SATURATE: BlendFactor.SRC_ALPHA_SATURATE,
  201. CONSTANT_COLOR: BlendFactor.CONSTANT_COLOR,
  202. ONE_MINUS_CONSTANT_COLOR: BlendFactor.ONE_MINUS_CONSTANT_COLOR,
  203. CONSTANT_ALPHA: BlendFactor.CONSTANT_ALPHA,
  204. ONE_MINUS_CONSTANT_ALPHA: BlendFactor.ONE_MINUS_CONSTANT_ALPHA,
  205. // stencil operation
  206. // ZERO: StencilOp.ZERO, // duplicate, safely removed because enum value is(and always will be) the same
  207. KEEP: StencilOp.KEEP,
  208. REPLACE: StencilOp.REPLACE,
  209. INCR: StencilOp.INCR,
  210. DECR: StencilOp.DECR,
  211. INVERT: StencilOp.INVERT,
  212. INCR_WRAP: StencilOp.INCR_WRAP,
  213. DECR_WRAP: StencilOp.DECR_WRAP,
  214. // comparison function
  215. NEVER: ComparisonFunc.NEVER,
  216. LESS: ComparisonFunc.LESS,
  217. EQUAL: ComparisonFunc.EQUAL,
  218. LESS_EQUAL: ComparisonFunc.LESS_EQUAL,
  219. GREATER: ComparisonFunc.GREATER,
  220. NOT_EQUAL: ComparisonFunc.NOT_EQUAL,
  221. GREATER_EQUAL: ComparisonFunc.GREATER_EQUAL,
  222. ALWAYS: ComparisonFunc.ALWAYS,
  223. // cull mode
  224. // NONE: CullMode.NONE, // duplicate, safely removed because enum value is(and always will be) the same
  225. FRONT: CullMode.FRONT,
  226. BACK: CullMode.BACK,
  227. // shade mode
  228. GOURAND: ShadeModel.GOURAND,
  229. FLAT: ShadeModel.FLAT,
  230. // polygon mode
  231. FILL: PolygonMode.FILL,
  232. LINE: PolygonMode.LINE,
  233. POINT: PolygonMode.POINT,
  234. // primitive mode
  235. POINT_LIST: PrimitiveMode.POINT_LIST,
  236. LINE_LIST: PrimitiveMode.LINE_LIST,
  237. LINE_STRIP: PrimitiveMode.LINE_STRIP,
  238. LINE_LOOP: PrimitiveMode.LINE_LOOP,
  239. TRIANGLE_LIST: PrimitiveMode.TRIANGLE_LIST,
  240. TRIANGLE_STRIP: PrimitiveMode.TRIANGLE_STRIP,
  241. TRIANGLE_FAN: PrimitiveMode.TRIANGLE_FAN,
  242. LINE_LIST_ADJACENCY: PrimitiveMode.LINE_LIST_ADJACENCY,
  243. LINE_STRIP_ADJACENCY: PrimitiveMode.LINE_STRIP_ADJACENCY,
  244. TRIANGLE_LIST_ADJACENCY: PrimitiveMode.TRIANGLE_LIST_ADJACENCY,
  245. TRIANGLE_STRIP_ADJACENCY: PrimitiveMode.TRIANGLE_STRIP_ADJACENCY,
  246. TRIANGLE_PATCH_ADJACENCY: PrimitiveMode.TRIANGLE_PATCH_ADJACENCY,
  247. QUAD_PATCH_LIST: PrimitiveMode.QUAD_PATCH_LIST,
  248. ISO_LINE_LIST: PrimitiveMode.ISO_LINE_LIST,
  249. // POINT: Filter.POINT, // duplicate, safely removed because enum value is(and always will be) the same
  250. LINEAR: Filter.LINEAR,
  251. ANISOTROPIC: Filter.ANISOTROPIC,
  252. WRAP: Address.WRAP,
  253. MIRROR: Address.MIRROR,
  254. CLAMP: Address.CLAMP,
  255. BORDER: Address.BORDER,
  256. LINE_WIDTH: DynamicStateFlagBit.LINE_WIDTH,
  257. DEPTH_BIAS: DynamicStateFlagBit.DEPTH_BIAS,
  258. BLEND_CONSTANTS: DynamicStateFlagBit.BLEND_CONSTANTS,
  259. DEPTH_BOUNDS: DynamicStateFlagBit.DEPTH_BOUNDS,
  260. STENCIL_WRITE_MASK: DynamicStateFlagBit.STENCIL_WRITE_MASK,
  261. STENCIL_COMPARE_MASK: DynamicStateFlagBit.STENCIL_COMPARE_MASK,
  262. TRUE: true,
  263. FALSE: false,
  264. };
  265. Object.assign(passParams, RenderPassStage);
  266. // for structural type checking
  267. // an 'any' key will check against all elements defined in that object
  268. // a key start with '$' means its essential, and can't be undefined
  269. const effectStructure = {
  270. $techniques: [
  271. {
  272. $passes: [
  273. {
  274. depthStencilState: {},
  275. rasterizerState: {},
  276. blendState: { targets: [{}] },
  277. properties: { any: { sampler: {}, editor: {} } },
  278. migrations: { properties: { any: {} }, macros: { any: {} } },
  279. embeddedMacros: {},
  280. },
  281. ],
  282. },
  283. ],
  284. };
  285. export {
  286. murmurhash2_32_gc,
  287. Sampler,
  288. SamplerInfo,
  289. effectStructure,
  290. isSampler,
  291. typeMap,
  292. formatMap,
  293. getFormat,
  294. getShaderStage,
  295. getDescriptorType,
  296. isNormalized,
  297. isPaddedMatrix,
  298. getMemoryAccessFlag,
  299. passParams,
  300. SetIndex,
  301. RenderPriority,
  302. GetTypeSize,
  303. };