NativeMacroDefinition.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace Veldrid.SPIRV
  5. {
  6. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  7. internal unsafe struct NativeMacroDefinition
  8. {
  9. public uint NameLength;
  10. public fixed byte Name[128];
  11. public uint ValueLength;
  12. public fixed byte Value[128];
  13. public NativeMacroDefinition(MacroDefinition macroDefinition)
  14. {
  15. if (string.IsNullOrEmpty(macroDefinition.Name))
  16. {
  17. throw new SpirvCompilationException($"MacroDefinition Name must be non-null.");
  18. }
  19. if (macroDefinition.Name.Length > 128)
  20. {
  21. throw new SpirvCompilationException($"Macro names must be less than or equal to 128 characters.");
  22. }
  23. fixed (char* nameU16Ptr = macroDefinition.Name)
  24. fixed (byte* namePtr = Name)
  25. {
  26. NameLength = (uint)Encoding.ASCII.GetBytes(nameU16Ptr, macroDefinition.Name.Length, namePtr, 128);
  27. }
  28. if (!string.IsNullOrEmpty(macroDefinition.Value))
  29. {
  30. if (macroDefinition.Value.Length > 128)
  31. {
  32. throw new SpirvCompilationException($"Macro values must be less than or equal to 128 characters.");
  33. }
  34. fixed (char* valueU16 = macroDefinition.Value)
  35. fixed (byte* valuePtr = Value)
  36. {
  37. ValueLength = (uint)Encoding.ASCII.GetBytes(valueU16, macroDefinition.Value.Length, valuePtr, 128);
  38. }
  39. }
  40. else
  41. {
  42. ValueLength = 0;
  43. }
  44. }
  45. }
  46. }