ShaderGenCommon.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Copyright 2012 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstring>
  5. #include <functional>
  6. #include <iterator>
  7. #include <string>
  8. #include <string_view>
  9. #include <type_traits>
  10. #include <vector>
  11. #include <fmt/format.h>
  12. #include "Common/BitField.h"
  13. #include "Common/CommonTypes.h"
  14. #include "Common/EnumMap.h"
  15. #include "Common/StringUtil.h"
  16. #include "Common/TypeUtils.h"
  17. #include "VideoCommon/AbstractShader.h"
  18. #include "VideoCommon/VideoCommon.h"
  19. /**
  20. * Common interface for classes that need to go through the shader generation path
  21. * (GenerateVertexShader, GenerateGeometryShader, GeneratePixelShader)
  22. * In particular, this includes the shader code generator (ShaderCode).
  23. * A different class (ShaderUid) can be used to uniquely identify each ShaderCode object.
  24. * More interesting things can be done with this, e.g. ShaderConstantProfile checks what shader
  25. * constants are being used. This can be used to optimize buffer management.
  26. * If the class does not use one or more of these methods (e.g. Uid class does not need code), the
  27. * method will be defined as a no-op by the base class, and the call
  28. * should be optimized out. The reason for this implementation is so that shader
  29. * selection/generation can be done in two passes, with only a cache lookup being
  30. * required if the shader has already been generated.
  31. */
  32. class ShaderGeneratorInterface
  33. {
  34. public:
  35. /*
  36. * Used when the shader generator would write a piece of ShaderCode.
  37. * Can be used like printf.
  38. * @note In the ShaderCode implementation, this does indeed write the parameter string to an
  39. * internal buffer. However, you're free to do whatever you like with the parameter.
  40. */
  41. void Write(const char*, ...)
  42. #ifdef __GNUC__
  43. __attribute__((format(printf, 2, 3)))
  44. #endif
  45. {
  46. }
  47. /*
  48. * Tells us that a specific constant range (including last_index) is being used by the shader
  49. */
  50. void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
  51. };
  52. /*
  53. * Shader UID class used to uniquely identify the ShaderCode output written in the shader generator.
  54. * uid_data can be any struct of parameters that uniquely identify each shader code output.
  55. * Unless performance is not an issue, uid_data should be tightly packed to reduce memory footprint.
  56. * Shader generators will write to specific uid_data fields; ShaderUid methods will only read raw
  57. * u32 values from a union.
  58. * NOTE: Because LinearDiskCache reads and writes the storage associated with a ShaderUid instance,
  59. * ShaderUid must be trivially copyable.
  60. */
  61. template <class uid_data>
  62. class ShaderUid : public ShaderGeneratorInterface
  63. {
  64. public:
  65. static_assert(std::is_trivially_copyable_v<uid_data>,
  66. "uid_data must be a trivially copyable type");
  67. ShaderUid() { memset(GetUidData(), 0, GetUidDataSize()); }
  68. bool operator==(const ShaderUid& obj) const
  69. {
  70. return memcmp(GetUidData(), obj.GetUidData(), GetUidDataSize()) == 0;
  71. }
  72. // determines the storage order inside STL containers
  73. bool operator<(const ShaderUid& obj) const
  74. {
  75. return memcmp(GetUidData(), obj.GetUidData(), GetUidDataSize()) < 0;
  76. }
  77. // Returns a pointer to an internally stored object of the uid_data type.
  78. uid_data* GetUidData() { return &data; }
  79. // Returns a pointer to an internally stored object of the uid_data type.
  80. const uid_data* GetUidData() const { return &data; }
  81. // Returns the raw bytes that make up the shader UID.
  82. const u8* GetUidDataRaw() const { return reinterpret_cast<const u8*>(&data); }
  83. // Returns the size of the underlying UID data structure in bytes.
  84. size_t GetUidDataSize() const { return sizeof(data); }
  85. private:
  86. uid_data data{};
  87. };
  88. class ShaderCode : public ShaderGeneratorInterface
  89. {
  90. public:
  91. ShaderCode() { m_buffer.reserve(16384); }
  92. const std::string& GetBuffer() const { return m_buffer; }
  93. // Writes format strings using fmtlib format strings.
  94. template <typename... Args>
  95. void Write(fmt::format_string<Args...> format, Args&&... args)
  96. {
  97. fmt::format_to(std::back_inserter(m_buffer), format, std::forward<Args>(args)...);
  98. }
  99. protected:
  100. std::string m_buffer;
  101. };
  102. /**
  103. * Generates a shader constant profile which can be used to query which constants are used in a
  104. * shader
  105. */
  106. class ShaderConstantProfile : public ShaderGeneratorInterface
  107. {
  108. public:
  109. ShaderConstantProfile(int num_constants) { constant_usage.resize(num_constants); }
  110. void SetConstantsUsed(unsigned int first_index, unsigned int last_index)
  111. {
  112. for (unsigned int i = first_index; i < last_index + 1; ++i)
  113. constant_usage[i] = true;
  114. }
  115. bool ConstantIsUsed(unsigned int index) const
  116. {
  117. // TODO: Not ready for usage yet
  118. return true;
  119. // return constant_usage[index];
  120. }
  121. private:
  122. std::vector<bool> constant_usage; // TODO: Is vector<bool> appropriate here?
  123. };
  124. // Host config contains the settings which can influence generated shaders.
  125. union ShaderHostConfig
  126. {
  127. u32 bits;
  128. BitField<0, 1, bool, u32> msaa;
  129. BitField<1, 1, bool, u32> ssaa;
  130. BitField<2, 1, bool, u32> stereo;
  131. BitField<3, 1, bool, u32> wireframe;
  132. BitField<4, 1, bool, u32> per_pixel_lighting;
  133. BitField<5, 1, bool, u32> vertex_rounding;
  134. BitField<6, 1, bool, u32> fast_depth_calc;
  135. BitField<7, 1, bool, u32> bounding_box;
  136. BitField<8, 1, bool, u32> backend_dual_source_blend;
  137. BitField<9, 1, bool, u32> backend_geometry_shaders;
  138. BitField<10, 1, bool, u32> backend_early_z;
  139. BitField<11, 1, bool, u32> backend_bbox;
  140. BitField<12, 1, bool, u32> backend_gs_instancing;
  141. BitField<13, 1, bool, u32> backend_clip_control;
  142. BitField<14, 1, bool, u32> backend_ssaa;
  143. BitField<15, 1, bool, u32> backend_atomics;
  144. BitField<16, 1, bool, u32> backend_depth_clamp;
  145. BitField<17, 1, bool, u32> backend_reversed_depth_range;
  146. BitField<18, 1, bool, u32> backend_bitfield;
  147. BitField<19, 1, bool, u32> backend_dynamic_sampler_indexing;
  148. BitField<20, 1, bool, u32> backend_shader_framebuffer_fetch;
  149. BitField<21, 1, bool, u32> backend_logic_op;
  150. BitField<22, 1, bool, u32> backend_palette_conversion;
  151. BitField<23, 1, bool, u32> enable_validation_layer;
  152. BitField<24, 1, bool, u32> manual_texture_sampling;
  153. BitField<25, 1, bool, u32> manual_texture_sampling_custom_texture_sizes;
  154. BitField<26, 1, bool, u32> backend_sampler_lod_bias;
  155. BitField<27, 1, bool, u32> backend_dynamic_vertex_loader;
  156. BitField<28, 1, bool, u32> backend_vs_point_line_expand;
  157. BitField<29, 1, bool, u32> backend_gl_layer_in_fs;
  158. static ShaderHostConfig GetCurrent();
  159. };
  160. // Gets the filename of the specified type of cache object (e.g. vertex shader, pipeline).
  161. std::string GetDiskShaderCacheFileName(APIType api_type, const char* type, bool include_gameid,
  162. bool include_host_config, bool include_api = true);
  163. void WriteIsNanHeader(ShaderCode& out, APIType api_type);
  164. void WriteBitfieldExtractHeader(ShaderCode& out, APIType api_type,
  165. const ShaderHostConfig& host_config);
  166. void GenerateVSOutputMembers(ShaderCode& object, APIType api_type, u32 texgens,
  167. const ShaderHostConfig& host_config, std::string_view qualifier,
  168. ShaderStage stage);
  169. void AssignVSOutputMembers(ShaderCode& object, std::string_view a, std::string_view b, u32 texgens,
  170. const ShaderHostConfig& host_config);
  171. void GenerateLineOffset(ShaderCode& object, std::string_view indent0, std::string_view indent1,
  172. std::string_view pos_a, std::string_view pos_b, std::string_view sign);
  173. void GenerateVSLineExpansion(ShaderCode& object, std::string_view indent, u32 texgens);
  174. void GenerateVSPointExpansion(ShaderCode& object, std::string_view indent, u32 texgens);
  175. // We use the flag "centroid" to fix some MSAA rendering bugs. With MSAA, the
  176. // pixel shader will be executed for each pixel which has at least one passed sample.
  177. // So there may be rendered pixels where the center of the pixel isn't in the primitive.
  178. // As the pixel shader usually renders at the center of the pixel, this position may be
  179. // outside the primitive. This will lead to sampling outside the texture, sign changes, ...
  180. // As a workaround, we interpolate at the centroid of the coveraged pixel, which
  181. // is always inside the primitive.
  182. // Without MSAA, this flag is defined to have no effect.
  183. const char* GetInterpolationQualifier(bool msaa, bool ssaa, bool in_glsl_interface_block = false,
  184. bool in = false);
  185. // bitfieldExtract generator for BitField types
  186. template <auto ptr_to_bitfield_member>
  187. std::string BitfieldExtract(std::string_view source)
  188. {
  189. using BitFieldT = Common::MemberType<ptr_to_bitfield_member>;
  190. return fmt::format("bitfieldExtract({}({}), {}, {})", BitFieldT::IsSigned() ? "int" : "uint",
  191. source, static_cast<u32>(BitFieldT::StartBit()),
  192. static_cast<u32>(BitFieldT::NumBits()));
  193. }
  194. template <auto last_member>
  195. void WriteSwitch(ShaderCode& out, APIType ApiType, std::string_view variable,
  196. const Common::EnumMap<std::string_view, last_member>& values, int indent,
  197. bool break_)
  198. {
  199. using enum_type = decltype(last_member);
  200. // Generate a tree of if statements recursively
  201. // std::function must be used because auto won't capture before initialization and thus can't be
  202. // used recursively
  203. std::function<void(u32, u32, u32)> BuildTree = [&](u32 cur_indent, u32 low, u32 high) {
  204. // Each generated statement is for low <= x < high
  205. if (high == low + 1)
  206. {
  207. // Down to 1 case (low <= x < low + 1 means x == low)
  208. const enum_type key = static_cast<enum_type>(low);
  209. // Note that this indentation behaves poorly for multi-line code
  210. out.Write("{:{}}{} // {}\n", "", cur_indent, values[key], key);
  211. }
  212. else
  213. {
  214. u32 mid = low + ((high - low) / 2);
  215. out.Write("{:{}}if ({} < {}u) {{\n", "", cur_indent, variable, mid);
  216. BuildTree(cur_indent + 2, low, mid);
  217. out.Write("{:{}}}} else {{\n", "", cur_indent);
  218. BuildTree(cur_indent + 2, mid, high);
  219. out.Write("{:{}}}}\n", "", cur_indent);
  220. }
  221. };
  222. BuildTree(indent, 0, static_cast<u32>(last_member) + 1);
  223. }
  224. // Constant variable names
  225. #define I_COLORS "color"
  226. #define I_KCOLORS "k"
  227. #define I_ALPHA "alphaRef"
  228. #define I_TEXDIMS "texdim"
  229. #define I_ZBIAS "czbias"
  230. #define I_INDTEXSCALE "cindscale"
  231. #define I_INDTEXMTX "cindmtx"
  232. #define I_FOGCOLOR "cfogcolor"
  233. #define I_FOGI "cfogi"
  234. #define I_FOGF "cfogf"
  235. #define I_FOGRANGE "cfogrange"
  236. #define I_ZSLOPE "czslope"
  237. #define I_EFBSCALE "cefbscale"
  238. #define I_POSNORMALMATRIX "cpnmtx"
  239. #define I_PROJECTION "cproj"
  240. #define I_MATERIALS "cmtrl"
  241. #define I_LIGHTS "clights"
  242. #define I_TEXMATRICES "ctexmtx"
  243. #define I_TRANSFORMMATRICES "ctrmtx"
  244. #define I_NORMALMATRICES "cnmtx"
  245. #define I_POSTTRANSFORMMATRICES "cpostmtx"
  246. #define I_PIXELCENTERCORRECTION "cpixelcenter"
  247. #define I_VIEWPORT_SIZE "cviewport"
  248. #define I_CACHED_NORMAL "cnormal"
  249. #define I_CACHED_TANGENT "ctangent"
  250. #define I_CACHED_BINORMAL "cbinormal"
  251. #define I_STEREOPARAMS "cstereo"
  252. #define I_LINEPTPARAMS "clinept"
  253. #define I_TEXOFFSET "ctexoffset"
  254. static const char s_shader_uniforms[] = "\tuint components;\n"
  255. "\tuint xfmem_dualTexInfo;\n"
  256. "\tuint xfmem_numColorChans;\n"
  257. "\tuint missing_color_hex;\n"
  258. "\tfloat4 missing_color_value;\n"
  259. "\tfloat4 " I_POSNORMALMATRIX "[6];\n"
  260. "\tfloat4 " I_PROJECTION "[4];\n"
  261. "\tint4 " I_MATERIALS "[4];\n"
  262. "\tLight " I_LIGHTS "[8];\n"
  263. "\tfloat4 " I_TEXMATRICES "[24];\n"
  264. "\tfloat4 " I_TRANSFORMMATRICES "[64];\n"
  265. "\tfloat4 " I_NORMALMATRICES "[32];\n"
  266. "\tfloat4 " I_POSTTRANSFORMMATRICES "[64];\n"
  267. "\tfloat4 " I_PIXELCENTERCORRECTION ";\n"
  268. "\tfloat2 " I_VIEWPORT_SIZE ";\n"
  269. "\tuint4 xfmem_pack1[8];\n"
  270. "\tfloat4 " I_CACHED_NORMAL ";\n"
  271. "\tfloat4 " I_CACHED_TANGENT ";\n"
  272. "\tfloat4 " I_CACHED_BINORMAL ";\n"
  273. "\tuint vertex_stride;\n"
  274. "\tuint vertex_offset_rawnormal;\n"
  275. "\tuint vertex_offset_rawtangent;\n"
  276. "\tuint vertex_offset_rawbinormal;\n"
  277. "\tuint vertex_offset_rawpos;\n"
  278. "\tuint vertex_offset_posmtx;\n"
  279. "\tuint vertex_offset_rawcolor0;\n"
  280. "\tuint vertex_offset_rawcolor1;\n"
  281. "\tuint4 vertex_offset_rawtex[2];\n" // std140 is pain
  282. "\t#define xfmem_texMtxInfo(i) (xfmem_pack1[(i)].x)\n"
  283. "\t#define xfmem_postMtxInfo(i) (xfmem_pack1[(i)].y)\n"
  284. "\t#define xfmem_color(i) (xfmem_pack1[(i)].z)\n"
  285. "\t#define xfmem_alpha(i) (xfmem_pack1[(i)].w)\n";
  286. static const char s_geometry_shader_uniforms[] = "\tfloat4 " I_STEREOPARAMS ";\n"
  287. "\tfloat4 " I_LINEPTPARAMS ";\n"
  288. "\tint4 " I_TEXOFFSET ";\n"
  289. "\tuint vs_expand;\n";
  290. constexpr std::string_view CUSTOM_PIXELSHADER_COLOR_FUNC = "customShaderColor";
  291. struct CustomPixelShader
  292. {
  293. std::string custom_shader;
  294. std::string material_uniform_block;
  295. bool operator==(const CustomPixelShader& other) const = default;
  296. };
  297. struct CustomPixelShaderContents
  298. {
  299. std::vector<CustomPixelShader> shaders;
  300. bool operator==(const CustomPixelShaderContents& other) const = default;
  301. };
  302. void WriteCustomShaderStructDef(ShaderCode* out, u32 numtexgens);