shaderc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2011-2026 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #ifndef SHADERC_H_HEADER_GUARD
  6. #define SHADERC_H_HEADER_GUARD
  7. namespace bgfx
  8. {
  9. extern bool g_verbose;
  10. }
  11. #ifndef SHADERC_CONFIG_HLSL
  12. # define SHADERC_CONFIG_HLSL BX_PLATFORM_WINDOWS
  13. #endif // SHADERC_CONFIG_HLSL
  14. #ifndef SHADERC_CONFIG_DXIL
  15. # define SHADERC_CONFIG_DXIL (0 \
  16. || BX_PLATFORM_WINDOWS \
  17. || BX_PLATFORM_LINUX \
  18. )
  19. #endif // SHADERC_CONFIG_DXIL
  20. #include <bx/bx.h>
  21. #include <bx/debug.h>
  22. #include <bx/commandline.h>
  23. #include <bx/endian.h>
  24. #include <bx/uint32_t.h>
  25. #include <bx/string.h>
  26. #include <bx/hash.h>
  27. #include <bx/file.h>
  28. #include "../../src/vertexlayout.h"
  29. #include <string.h>
  30. #include <algorithm>
  31. #include <string>
  32. #include <vector>
  33. #include <unordered_map>
  34. namespace bgfx
  35. {
  36. extern bool g_verbose;
  37. bx::StringView nextWord(bx::StringView& _parse);
  38. constexpr uint16_t kAccessRead = 0x8000;
  39. constexpr uint16_t kAccessWrite = 0x4000;
  40. constexpr uint16_t kAccessMask = 0
  41. | kAccessRead
  42. | kAccessWrite
  43. ;
  44. constexpr uint8_t kUniformFragmentBit = 0x10;
  45. constexpr uint8_t kUniformSamplerBit = 0x20;
  46. constexpr uint8_t kUniformReadOnlyBit = 0x40;
  47. constexpr uint8_t kUniformCompareBit = 0x80;
  48. constexpr uint8_t kUniformMask = 0
  49. | kUniformFragmentBit
  50. | kUniformSamplerBit
  51. | kUniformReadOnlyBit
  52. | kUniformCompareBit
  53. ;
  54. const char* getUniformTypeName(UniformType::Enum _enum);
  55. UniformType::Enum nameToUniformTypeEnum(const char* _name);
  56. struct Uniform
  57. {
  58. Uniform()
  59. : type(UniformType::Count)
  60. , num(0)
  61. , regIndex(0)
  62. , regCount(0)
  63. , texComponent(0)
  64. , texDimension(0)
  65. , texFormat(0)
  66. {
  67. }
  68. std::string name;
  69. UniformType::Enum type;
  70. uint8_t num;
  71. uint16_t regIndex;
  72. uint16_t regCount;
  73. uint8_t texComponent;
  74. uint8_t texDimension;
  75. uint16_t texFormat;
  76. };
  77. struct Options
  78. {
  79. Options();
  80. void dump();
  81. char shaderType;
  82. std::string platform;
  83. std::string profile;
  84. std::string inputFilePath;
  85. std::string outputFilePath;
  86. std::vector<std::string> includeDirs;
  87. std::vector<std::string> defines;
  88. std::vector<std::string> dependencies;
  89. bool disasm;
  90. bool raw;
  91. bool preprocessOnly;
  92. bool keepComments;
  93. bool depends;
  94. bool debugInformation;
  95. bool avoidFlowControl;
  96. bool noPreshader;
  97. bool partialPrecision;
  98. bool preferFlowControl;
  99. bool backwardsCompatibility;
  100. bool warningsAreErrors;
  101. bool keepIntermediate;
  102. bool optimize;
  103. uint32_t optimizationLevel;
  104. };
  105. typedef std::vector<Uniform> UniformArray;
  106. void printCode(const char* _code, int32_t _line = 0, int32_t _start = 0, int32_t _end = INT32_MAX, int32_t _column = -1);
  107. void strReplace(char* _str, const char* _find, const char* _replace);
  108. int32_t writef(bx::WriterI* _writer, const char* _format, ...);
  109. void writeFile(const char* _filePath, const void* _data, int32_t _size);
  110. bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  111. bool compileHLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  112. bool compileDxilShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  113. bool compileMetalShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  114. bool compilePSSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  115. bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  116. bool compileWgslShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bx::WriterI* _messages);
  117. const char* getPsslPreamble();
  118. } // namespace bgfx
  119. #endif // SHADERC_H_HEADER_GUARD