test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2020 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. const spirvTools = require("../../out/web/spirv-tools");
  15. const fs = require("fs");
  16. const util = require("util");
  17. const readFile = util.promisify(fs.readFile);
  18. const SPV_PATH = "./test/fuzzers/corpora/spv/simple.spv";
  19. const test = async () => {
  20. const spv = await spirvTools();
  21. // disassemble from file
  22. const buffer = await readFile(SPV_PATH);
  23. const disFileResult = spv.dis(
  24. buffer,
  25. spv.SPV_ENV_UNIVERSAL_1_3,
  26. spv.SPV_BINARY_TO_TEXT_OPTION_INDENT |
  27. spv.SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES |
  28. spv.SPV_BINARY_TO_TEXT_OPTION_COLOR
  29. );
  30. console.log("dis from file:\n", disFileResult);
  31. // assemble
  32. const source = `
  33. OpCapability Linkage
  34. OpCapability Shader
  35. OpMemoryModel Logical GLSL450
  36. OpSource GLSL 450
  37. OpDecorate %spec SpecId 1
  38. %int = OpTypeInt 32 1
  39. %spec = OpSpecConstant %int 0
  40. %const = OpConstant %int 42`;
  41. const asResult = spv.as(
  42. source,
  43. spv.SPV_ENV_UNIVERSAL_1_3,
  44. spv.SPV_TEXT_TO_BINARY_OPTION_NONE
  45. );
  46. console.log(`as returned ${asResult.byteLength} bytes`);
  47. // re-disassemble
  48. const disResult = spv.dis(
  49. asResult,
  50. spv.SPV_ENV_UNIVERSAL_1_3,
  51. spv.SPV_BINARY_TO_TEXT_OPTION_INDENT |
  52. spv.SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES |
  53. spv.SPV_BINARY_TO_TEXT_OPTION_COLOR
  54. );
  55. console.log("dis:\n", disResult);
  56. };
  57. test();