emit_context.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/backend/bindings.h"
  5. #include "shader_recompiler/backend/glsl/emit_context.h"
  6. #include "shader_recompiler/frontend/ir/program.h"
  7. #include "shader_recompiler/profile.h"
  8. #include "shader_recompiler/runtime_info.h"
  9. namespace Shader::Backend::GLSL {
  10. namespace {
  11. u32 CbufIndex(size_t offset) {
  12. return (offset / 4) % 4;
  13. }
  14. char Swizzle(size_t offset) {
  15. return "xyzw"[CbufIndex(offset)];
  16. }
  17. std::string_view InterpDecorator(Interpolation interp) {
  18. switch (interp) {
  19. case Interpolation::Smooth:
  20. return "";
  21. case Interpolation::Flat:
  22. return "flat ";
  23. case Interpolation::NoPerspective:
  24. return "noperspective ";
  25. }
  26. throw InvalidArgument("Invalid interpolation {}", interp);
  27. }
  28. std::string_view InputArrayDecorator(Stage stage) {
  29. switch (stage) {
  30. case Stage::Geometry:
  31. case Stage::TessellationControl:
  32. case Stage::TessellationEval:
  33. return "[]";
  34. default:
  35. return "";
  36. }
  37. }
  38. bool StoresPerVertexAttributes(Stage stage) {
  39. switch (stage) {
  40. case Stage::VertexA:
  41. case Stage::VertexB:
  42. case Stage::Geometry:
  43. case Stage::TessellationEval:
  44. return true;
  45. default:
  46. return false;
  47. }
  48. }
  49. std::string OutputDecorator(Stage stage, u32 size) {
  50. switch (stage) {
  51. case Stage::TessellationControl:
  52. return fmt::format("[{}]", size);
  53. default:
  54. return "";
  55. }
  56. }
  57. std::string_view SamplerType(TextureType type, bool is_depth) {
  58. if (is_depth) {
  59. switch (type) {
  60. case TextureType::Color1D:
  61. return "sampler1DShadow";
  62. case TextureType::ColorArray1D:
  63. return "sampler1DArrayShadow";
  64. case TextureType::Color2D:
  65. return "sampler2DShadow";
  66. case TextureType::ColorArray2D:
  67. return "sampler2DArrayShadow";
  68. case TextureType::ColorCube:
  69. return "samplerCubeShadow";
  70. case TextureType::ColorArrayCube:
  71. return "samplerCubeArrayShadow";
  72. default:
  73. throw NotImplementedException("Texture type: {}", type);
  74. }
  75. }
  76. switch (type) {
  77. case TextureType::Color1D:
  78. return "sampler1D";
  79. case TextureType::ColorArray1D:
  80. return "sampler1DArray";
  81. case TextureType::Color2D:
  82. return "sampler2D";
  83. case TextureType::ColorArray2D:
  84. return "sampler2DArray";
  85. case TextureType::Color3D:
  86. return "sampler3D";
  87. case TextureType::ColorCube:
  88. return "samplerCube";
  89. case TextureType::ColorArrayCube:
  90. return "samplerCubeArray";
  91. case TextureType::Buffer:
  92. return "samplerBuffer";
  93. default:
  94. throw NotImplementedException("Texture type: {}", type);
  95. }
  96. }
  97. std::string_view ImageType(TextureType type) {
  98. switch (type) {
  99. case TextureType::Color1D:
  100. return "uimage1D";
  101. case TextureType::ColorArray1D:
  102. return "uimage1DArray";
  103. case TextureType::Color2D:
  104. return "uimage2D";
  105. case TextureType::ColorArray2D:
  106. return "uimage2DArray";
  107. case TextureType::Color3D:
  108. return "uimage3D";
  109. case TextureType::ColorCube:
  110. return "uimageCube";
  111. case TextureType::ColorArrayCube:
  112. return "uimageCubeArray";
  113. case TextureType::Buffer:
  114. return "uimageBuffer";
  115. default:
  116. throw NotImplementedException("Image type: {}", type);
  117. }
  118. }
  119. std::string_view ImageFormatString(ImageFormat format) {
  120. switch (format) {
  121. case ImageFormat::Typeless:
  122. return "";
  123. case ImageFormat::R8_UINT:
  124. return ",r8ui";
  125. case ImageFormat::R8_SINT:
  126. return ",r8i";
  127. case ImageFormat::R16_UINT:
  128. return ",r16ui";
  129. case ImageFormat::R16_SINT:
  130. return ",r16i";
  131. case ImageFormat::R32_UINT:
  132. return ",r32ui";
  133. case ImageFormat::R32G32_UINT:
  134. return ",rg32ui";
  135. case ImageFormat::R32G32B32A32_UINT:
  136. return ",rgba32ui";
  137. default:
  138. throw NotImplementedException("Image format: {}", format);
  139. }
  140. }
  141. std::string_view ImageAccessQualifier(bool is_written, bool is_read) {
  142. if (is_written && !is_read) {
  143. return "writeonly ";
  144. }
  145. if (is_read && !is_written) {
  146. return "readonly ";
  147. }
  148. return "";
  149. }
  150. std::string_view GetTessMode(TessPrimitive primitive) {
  151. switch (primitive) {
  152. case TessPrimitive::Triangles:
  153. return "triangles";
  154. case TessPrimitive::Quads:
  155. return "quads";
  156. case TessPrimitive::Isolines:
  157. return "isolines";
  158. }
  159. throw InvalidArgument("Invalid tessellation primitive {}", primitive);
  160. }
  161. std::string_view GetTessSpacing(TessSpacing spacing) {
  162. switch (spacing) {
  163. case TessSpacing::Equal:
  164. return "equal_spacing";
  165. case TessSpacing::FractionalOdd:
  166. return "fractional_odd_spacing";
  167. case TessSpacing::FractionalEven:
  168. return "fractional_even_spacing";
  169. }
  170. throw InvalidArgument("Invalid tessellation spacing {}", spacing);
  171. }
  172. std::string_view InputPrimitive(InputTopology topology) {
  173. switch (topology) {
  174. case InputTopology::Points:
  175. return "points";
  176. case InputTopology::Lines:
  177. return "lines";
  178. case InputTopology::LinesAdjacency:
  179. return "lines_adjacency";
  180. case InputTopology::Triangles:
  181. return "triangles";
  182. case InputTopology::TrianglesAdjacency:
  183. return "triangles_adjacency";
  184. }
  185. throw InvalidArgument("Invalid input topology {}", topology);
  186. }
  187. std::string_view OutputPrimitive(OutputTopology topology) {
  188. switch (topology) {
  189. case OutputTopology::PointList:
  190. return "points";
  191. case OutputTopology::LineStrip:
  192. return "line_strip";
  193. case OutputTopology::TriangleStrip:
  194. return "triangle_strip";
  195. }
  196. throw InvalidArgument("Invalid output topology {}", topology);
  197. }
  198. void SetupLegacyOutPerVertex(EmitContext& ctx, std::string& header) {
  199. if (!ctx.info.stores.Legacy()) {
  200. return;
  201. }
  202. if (ctx.info.stores.FixedFunctionTexture()) {
  203. header += "vec4 gl_TexCoord[8];";
  204. }
  205. if (ctx.info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
  206. header += "vec4 gl_FrontColor;";
  207. }
  208. if (ctx.info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
  209. header += "vec4 gl_FrontSecondaryColor;";
  210. }
  211. if (ctx.info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
  212. header += "vec4 gl_BackColor;";
  213. }
  214. if (ctx.info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
  215. header += "vec4 gl_BackSecondaryColor;";
  216. }
  217. }
  218. void SetupOutPerVertex(EmitContext& ctx, std::string& header) {
  219. if (!StoresPerVertexAttributes(ctx.stage)) {
  220. return;
  221. }
  222. if (ctx.uses_geometry_passthrough) {
  223. return;
  224. }
  225. header += "out gl_PerVertex{vec4 gl_Position;";
  226. if (ctx.info.stores[IR::Attribute::PointSize]) {
  227. header += "float gl_PointSize;";
  228. }
  229. if (ctx.info.stores.ClipDistances()) {
  230. header += "float gl_ClipDistance[];";
  231. }
  232. if (ctx.info.stores[IR::Attribute::ViewportIndex] &&
  233. ctx.profile.support_viewport_index_layer_non_geometry && ctx.stage != Stage::Geometry) {
  234. header += "int gl_ViewportIndex;";
  235. }
  236. SetupLegacyOutPerVertex(ctx, header);
  237. header += "};";
  238. if (ctx.info.stores[IR::Attribute::ViewportIndex] && ctx.stage == Stage::Geometry) {
  239. header += "out int gl_ViewportIndex;";
  240. }
  241. }
  242. void SetupInPerVertex(EmitContext& ctx, std::string& header) {
  243. // Currently only required for TessellationControl to adhere to
  244. // ARB_separate_shader_objects requirements
  245. if (ctx.stage != Stage::TessellationControl) {
  246. return;
  247. }
  248. const bool loads_position{ctx.info.loads.AnyComponent(IR::Attribute::PositionX)};
  249. const bool loads_point_size{ctx.info.loads[IR::Attribute::PointSize]};
  250. const bool loads_clip_distance{ctx.info.loads.ClipDistances()};
  251. const bool loads_per_vertex{loads_position || loads_point_size || loads_clip_distance};
  252. if (!loads_per_vertex) {
  253. return;
  254. }
  255. header += "in gl_PerVertex{";
  256. if (loads_position) {
  257. header += "vec4 gl_Position;";
  258. }
  259. if (loads_point_size) {
  260. header += "float gl_PointSize;";
  261. }
  262. if (loads_clip_distance) {
  263. header += "float gl_ClipDistance[];";
  264. }
  265. header += "}gl_in[gl_MaxPatchVertices];";
  266. }
  267. void SetupLegacyInPerFragment(EmitContext& ctx, std::string& header) {
  268. if (!ctx.info.loads.Legacy()) {
  269. return;
  270. }
  271. header += "in gl_PerFragment{";
  272. if (ctx.info.loads.FixedFunctionTexture()) {
  273. header += "vec4 gl_TexCoord[8];";
  274. }
  275. if (ctx.info.loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
  276. header += "vec4 gl_Color;";
  277. }
  278. header += "};";
  279. }
  280. } // Anonymous namespace
  281. EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
  282. const RuntimeInfo& runtime_info_)
  283. : info{program.info}, profile{profile_}, runtime_info{runtime_info_}, stage{program.stage},
  284. uses_geometry_passthrough{program.is_geometry_passthrough &&
  285. profile.support_geometry_shader_passthrough} {
  286. if (profile.need_fastmath_off) {
  287. header += "#pragma optionNV(fastmath off)\n";
  288. }
  289. SetupExtensions();
  290. switch (program.stage) {
  291. case Stage::VertexA:
  292. case Stage::VertexB:
  293. stage_name = "vs";
  294. break;
  295. case Stage::TessellationControl:
  296. stage_name = "tcs";
  297. header += fmt::format("layout(vertices={})out;", program.invocations);
  298. break;
  299. case Stage::TessellationEval:
  300. stage_name = "tes";
  301. header += fmt::format("layout({},{},{})in;", GetTessMode(runtime_info.tess_primitive),
  302. GetTessSpacing(runtime_info.tess_spacing),
  303. runtime_info.tess_clockwise ? "cw" : "ccw");
  304. break;
  305. case Stage::Geometry:
  306. stage_name = "gs";
  307. header += fmt::format("layout({})in;", InputPrimitive(runtime_info.input_topology));
  308. if (uses_geometry_passthrough) {
  309. header += "layout(passthrough)in gl_PerVertex{vec4 gl_Position;};";
  310. break;
  311. } else if (program.is_geometry_passthrough &&
  312. !profile.support_geometry_shader_passthrough) {
  313. LOG_WARNING(Shader_GLSL, "Passthrough geometry program used but not supported");
  314. }
  315. header += fmt::format(
  316. "layout({},max_vertices={})out;in gl_PerVertex{{vec4 gl_Position;}}gl_in[];",
  317. OutputPrimitive(program.output_topology), program.output_vertices);
  318. break;
  319. case Stage::Fragment:
  320. stage_name = "fs";
  321. position_name = "gl_FragCoord";
  322. if (runtime_info.force_early_z) {
  323. header += "layout(early_fragment_tests)in;";
  324. }
  325. if (info.uses_sample_id) {
  326. header += "in int gl_SampleID;";
  327. }
  328. if (info.stores_sample_mask) {
  329. header += "out int gl_SampleMask[];";
  330. }
  331. break;
  332. case Stage::Compute:
  333. stage_name = "cs";
  334. const u32 local_x{std::max(program.workgroup_size[0], 1u)};
  335. const u32 local_y{std::max(program.workgroup_size[1], 1u)};
  336. const u32 local_z{std::max(program.workgroup_size[2], 1u)};
  337. header += fmt::format("layout(local_size_x={},local_size_y={},local_size_z={}) in;",
  338. local_x, local_y, local_z);
  339. break;
  340. }
  341. SetupOutPerVertex(*this, header);
  342. SetupInPerVertex(*this, header);
  343. SetupLegacyInPerFragment(*this, header);
  344. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  345. if (!info.loads.Generic(index) || !runtime_info.previous_stage_stores.Generic(index)) {
  346. continue;
  347. }
  348. const auto qualifier{uses_geometry_passthrough ? "passthrough"
  349. : fmt::format("location={}", index)};
  350. header += fmt::format("layout({}){}in vec4 in_attr{}{};", qualifier,
  351. InterpDecorator(info.interpolation[index]), index,
  352. InputArrayDecorator(stage));
  353. }
  354. for (size_t index = 0; index < info.uses_patches.size(); ++index) {
  355. if (!info.uses_patches[index]) {
  356. continue;
  357. }
  358. const auto qualifier{stage == Stage::TessellationControl ? "out" : "in"};
  359. header += fmt::format("layout(location={})patch {} vec4 patch{};", index, qualifier, index);
  360. }
  361. if (stage == Stage::Fragment) {
  362. for (size_t index = 0; index < info.stores_frag_color.size(); ++index) {
  363. if (!info.stores_frag_color[index] && !profile.need_declared_frag_colors) {
  364. continue;
  365. }
  366. header += fmt::format("layout(location={})out vec4 frag_color{};", index, index);
  367. }
  368. }
  369. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  370. if (info.stores.Generic(index)) {
  371. DefineGenericOutput(index, program.invocations);
  372. }
  373. }
  374. if (info.uses_rescaling_uniform) {
  375. header += "layout(location=0) uniform float down_factor;";
  376. }
  377. DefineConstantBuffers(bindings);
  378. DefineStorageBuffers(bindings);
  379. SetupImages(bindings);
  380. SetupTextures(bindings);
  381. DefineHelperFunctions();
  382. DefineConstants();
  383. }
  384. void EmitContext::SetupExtensions() {
  385. header += "#extension GL_ARB_separate_shader_objects : enable\n";
  386. if (info.uses_shadow_lod && profile.support_gl_texture_shadow_lod) {
  387. header += "#extension GL_EXT_texture_shadow_lod : enable\n";
  388. }
  389. if (info.uses_int64 && profile.support_int64) {
  390. header += "#extension GL_ARB_gpu_shader_int64 : enable\n";
  391. }
  392. if (info.uses_int64_bit_atomics) {
  393. header += "#extension GL_NV_shader_atomic_int64 : enable\n";
  394. }
  395. if (info.uses_atomic_f32_add) {
  396. header += "#extension GL_NV_shader_atomic_float : enable\n";
  397. }
  398. if (info.uses_atomic_f16x2_add || info.uses_atomic_f16x2_min || info.uses_atomic_f16x2_max) {
  399. header += "#extension GL_NV_shader_atomic_fp16_vector : enable\n";
  400. }
  401. if (info.uses_fp16) {
  402. if (profile.support_gl_nv_gpu_shader_5) {
  403. header += "#extension GL_NV_gpu_shader5 : enable\n";
  404. }
  405. if (profile.support_gl_amd_gpu_shader_half_float) {
  406. header += "#extension GL_AMD_gpu_shader_half_float : enable\n";
  407. }
  408. }
  409. if (info.uses_subgroup_invocation_id || info.uses_subgroup_mask || info.uses_subgroup_vote ||
  410. info.uses_subgroup_shuffles || info.uses_fswzadd) {
  411. header += "#extension GL_ARB_shader_ballot : enable\n"
  412. "#extension GL_ARB_shader_group_vote : enable\n";
  413. if (!info.uses_int64 && profile.support_int64) {
  414. header += "#extension GL_ARB_gpu_shader_int64 : enable\n";
  415. }
  416. if (profile.support_gl_warp_intrinsics) {
  417. header += "#extension GL_NV_shader_thread_shuffle : enable\n";
  418. }
  419. }
  420. if ((info.stores[IR::Attribute::ViewportIndex] || info.stores[IR::Attribute::Layer]) &&
  421. profile.support_viewport_index_layer_non_geometry && stage != Stage::Geometry) {
  422. header += "#extension GL_ARB_shader_viewport_layer_array : enable\n";
  423. }
  424. if (info.uses_sparse_residency && profile.support_gl_sparse_textures) {
  425. header += "#extension GL_ARB_sparse_texture2 : enable\n";
  426. }
  427. if (info.stores[IR::Attribute::ViewportMask] && profile.support_viewport_mask) {
  428. header += "#extension GL_NV_viewport_array2 : enable\n";
  429. }
  430. if (info.uses_typeless_image_reads) {
  431. header += "#extension GL_EXT_shader_image_load_formatted : enable\n";
  432. }
  433. if (info.uses_derivatives && profile.support_gl_derivative_control) {
  434. header += "#extension GL_ARB_derivative_control : enable\n";
  435. }
  436. if (uses_geometry_passthrough) {
  437. header += "#extension GL_NV_geometry_shader_passthrough : enable\n";
  438. }
  439. }
  440. void EmitContext::DefineConstantBuffers(Bindings& bindings) {
  441. if (info.constant_buffer_descriptors.empty()) {
  442. return;
  443. }
  444. for (const auto& desc : info.constant_buffer_descriptors) {
  445. header += fmt::format(
  446. "layout(std140,binding={}) uniform {}_cbuf_{}{{vec4 {}_cbuf{}[{}];}};",
  447. bindings.uniform_buffer, stage_name, desc.index, stage_name, desc.index, 4 * 1024);
  448. bindings.uniform_buffer += desc.count;
  449. }
  450. }
  451. void EmitContext::DefineStorageBuffers(Bindings& bindings) {
  452. if (info.storage_buffers_descriptors.empty()) {
  453. return;
  454. }
  455. u32 index{};
  456. for (const auto& desc : info.storage_buffers_descriptors) {
  457. header += fmt::format("layout(std430,binding={}) buffer {}_ssbo_{}{{uint {}_ssbo{}[];}};",
  458. bindings.storage_buffer, stage_name, bindings.storage_buffer,
  459. stage_name, index);
  460. bindings.storage_buffer += desc.count;
  461. index += desc.count;
  462. }
  463. }
  464. void EmitContext::DefineGenericOutput(size_t index, u32 invocations) {
  465. static constexpr std::string_view swizzle{"xyzw"};
  466. const size_t base_index{static_cast<size_t>(IR::Attribute::Generic0X) + index * 4};
  467. u32 element{0};
  468. while (element < 4) {
  469. std::string definition{fmt::format("layout(location={}", index)};
  470. const u32 remainder{4 - element};
  471. const TransformFeedbackVarying* xfb_varying{};
  472. if (!runtime_info.xfb_varyings.empty()) {
  473. xfb_varying = &runtime_info.xfb_varyings[base_index + element];
  474. xfb_varying = xfb_varying && xfb_varying->components > 0 ? xfb_varying : nullptr;
  475. }
  476. const u32 num_components{xfb_varying ? xfb_varying->components : remainder};
  477. if (element > 0) {
  478. definition += fmt::format(",component={}", element);
  479. }
  480. if (xfb_varying) {
  481. definition +=
  482. fmt::format(",xfb_buffer={},xfb_stride={},xfb_offset={}", xfb_varying->buffer,
  483. xfb_varying->stride, xfb_varying->offset);
  484. }
  485. std::string name{fmt::format("out_attr{}", index)};
  486. if (num_components < 4 || element > 0) {
  487. name += fmt::format("_{}", swizzle.substr(element, num_components));
  488. }
  489. const auto type{num_components == 1 ? "float" : fmt::format("vec{}", num_components)};
  490. definition += fmt::format(")out {} {}{};", type, name, OutputDecorator(stage, invocations));
  491. header += definition;
  492. const GenericElementInfo element_info{
  493. .name = name,
  494. .first_element = element,
  495. .num_components = num_components,
  496. };
  497. std::fill_n(output_generics[index].begin() + element, num_components, element_info);
  498. element += num_components;
  499. }
  500. }
  501. void EmitContext::DefineHelperFunctions() {
  502. header += "\n#define ftoi floatBitsToInt\n#define ftou floatBitsToUint\n"
  503. "#define itof intBitsToFloat\n#define utof uintBitsToFloat\n";
  504. if (info.uses_global_increment || info.uses_shared_increment) {
  505. header += "uint CasIncrement(uint op_a,uint op_b){return op_a>=op_b?0u:(op_a+1u);}";
  506. }
  507. if (info.uses_global_decrement || info.uses_shared_decrement) {
  508. header += "uint CasDecrement(uint op_a,uint op_b){"
  509. "return op_a==0||op_a>op_b?op_b:(op_a-1u);}";
  510. }
  511. if (info.uses_atomic_f32_add) {
  512. header += "uint CasFloatAdd(uint op_a,float op_b){"
  513. "return ftou(utof(op_a)+op_b);}";
  514. }
  515. if (info.uses_atomic_f32x2_add) {
  516. header += "uint CasFloatAdd32x2(uint op_a,vec2 op_b){"
  517. "return packHalf2x16(unpackHalf2x16(op_a)+op_b);}";
  518. }
  519. if (info.uses_atomic_f32x2_min) {
  520. header += "uint CasFloatMin32x2(uint op_a,vec2 op_b){return "
  521. "packHalf2x16(min(unpackHalf2x16(op_a),op_b));}";
  522. }
  523. if (info.uses_atomic_f32x2_max) {
  524. header += "uint CasFloatMax32x2(uint op_a,vec2 op_b){return "
  525. "packHalf2x16(max(unpackHalf2x16(op_a),op_b));}";
  526. }
  527. if (info.uses_atomic_f16x2_add) {
  528. header += "uint CasFloatAdd16x2(uint op_a,f16vec2 op_b){return "
  529. "packFloat2x16(unpackFloat2x16(op_a)+op_b);}";
  530. }
  531. if (info.uses_atomic_f16x2_min) {
  532. header += "uint CasFloatMin16x2(uint op_a,f16vec2 op_b){return "
  533. "packFloat2x16(min(unpackFloat2x16(op_a),op_b));}";
  534. }
  535. if (info.uses_atomic_f16x2_max) {
  536. header += "uint CasFloatMax16x2(uint op_a,f16vec2 op_b){return "
  537. "packFloat2x16(max(unpackFloat2x16(op_a),op_b));}";
  538. }
  539. if (info.uses_atomic_s32_min) {
  540. header += "uint CasMinS32(uint op_a,uint op_b){return uint(min(int(op_a),int(op_b)));}";
  541. }
  542. if (info.uses_atomic_s32_max) {
  543. header += "uint CasMaxS32(uint op_a,uint op_b){return uint(max(int(op_a),int(op_b)));}";
  544. }
  545. if (info.uses_global_memory && profile.support_int64) {
  546. header += DefineGlobalMemoryFunctions();
  547. }
  548. if (info.loads_indexed_attributes) {
  549. const bool is_array{stage == Stage::Geometry};
  550. const auto vertex_arg{is_array ? ",uint vertex" : ""};
  551. std::string func{
  552. fmt::format("float IndexedAttrLoad(int offset{}){{int base_index=offset>>2;uint "
  553. "masked_index=uint(base_index)&3u;switch(base_index>>2){{",
  554. vertex_arg)};
  555. if (info.loads.AnyComponent(IR::Attribute::PositionX)) {
  556. const auto position_idx{is_array ? "gl_in[vertex]." : ""};
  557. func += fmt::format("case {}:return {}{}[masked_index];",
  558. static_cast<u32>(IR::Attribute::PositionX) >> 2, position_idx,
  559. position_name);
  560. }
  561. const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
  562. for (u32 index = 0; index < IR::NUM_GENERICS; ++index) {
  563. if (!info.loads.Generic(index)) {
  564. continue;
  565. }
  566. const auto vertex_idx{is_array ? "[vertex]" : ""};
  567. func += fmt::format("case {}:return in_attr{}{}[masked_index];",
  568. base_attribute_value + index, index, vertex_idx);
  569. }
  570. func += "default: return 0.0;}}";
  571. header += func;
  572. }
  573. if (info.stores_indexed_attributes) {
  574. // TODO
  575. }
  576. }
  577. std::string EmitContext::DefineGlobalMemoryFunctions() {
  578. const auto define_body{[&](std::string& func, size_t index, std::string_view return_statement) {
  579. const auto& ssbo{info.storage_buffers_descriptors[index]};
  580. const u32 size_cbuf_offset{ssbo.cbuf_offset + 8};
  581. const auto ssbo_addr{fmt::format("ssbo_addr{}", index)};
  582. const auto cbuf{fmt::format("{}_cbuf{}", stage_name, ssbo.cbuf_index)};
  583. std::array<std::string, 2> addr_xy;
  584. std::array<std::string, 2> size_xy;
  585. for (size_t i = 0; i < addr_xy.size(); ++i) {
  586. const auto addr_loc{ssbo.cbuf_offset + 4 * i};
  587. const auto size_loc{size_cbuf_offset + 4 * i};
  588. addr_xy[i] = fmt::format("ftou({}[{}].{})", cbuf, addr_loc / 16, Swizzle(addr_loc));
  589. size_xy[i] = fmt::format("ftou({}[{}].{})", cbuf, size_loc / 16, Swizzle(size_loc));
  590. }
  591. const auto addr_pack{fmt::format("packUint2x32(uvec2({},{}))", addr_xy[0], addr_xy[1])};
  592. const auto addr_statment{fmt::format("uint64_t {}={};", ssbo_addr, addr_pack)};
  593. func += addr_statment;
  594. const auto size_vec{fmt::format("uvec2({},{})", size_xy[0], size_xy[1])};
  595. const auto comp_lhs{fmt::format("(addr>={})", ssbo_addr)};
  596. const auto comp_rhs{fmt::format("(addr<({}+uint64_t({})))", ssbo_addr, size_vec)};
  597. const auto comparison{fmt::format("if({}&&{}){{", comp_lhs, comp_rhs)};
  598. func += comparison;
  599. const auto ssbo_name{fmt::format("{}_ssbo{}", stage_name, index)};
  600. func += fmt::format(fmt::runtime(return_statement), ssbo_name, ssbo_addr);
  601. }};
  602. std::string write_func{"void WriteGlobal32(uint64_t addr,uint data){"};
  603. std::string write_func_64{"void WriteGlobal64(uint64_t addr,uvec2 data){"};
  604. std::string write_func_128{"void WriteGlobal128(uint64_t addr,uvec4 data){"};
  605. std::string load_func{"uint LoadGlobal32(uint64_t addr){"};
  606. std::string load_func_64{"uvec2 LoadGlobal64(uint64_t addr){"};
  607. std::string load_func_128{"uvec4 LoadGlobal128(uint64_t addr){"};
  608. const size_t num_buffers{info.storage_buffers_descriptors.size()};
  609. for (size_t index = 0; index < num_buffers; ++index) {
  610. if (!info.nvn_buffer_used[index]) {
  611. continue;
  612. }
  613. define_body(write_func, index, "{0}[uint(addr-{1})>>2]=data;return;}}");
  614. define_body(write_func_64, index,
  615. "{0}[uint(addr-{1})>>2]=data.x;{0}[uint(addr-{1}+4)>>2]=data.y;return;}}");
  616. define_body(write_func_128, index,
  617. "{0}[uint(addr-{1})>>2]=data.x;{0}[uint(addr-{1}+4)>>2]=data.y;{0}[uint("
  618. "addr-{1}+8)>>2]=data.z;{0}[uint(addr-{1}+12)>>2]=data.w;return;}}");
  619. define_body(load_func, index, "return {0}[uint(addr-{1})>>2];}}");
  620. define_body(load_func_64, index,
  621. "return uvec2({0}[uint(addr-{1})>>2],{0}[uint(addr-{1}+4)>>2]);}}");
  622. define_body(load_func_128, index,
  623. "return uvec4({0}[uint(addr-{1})>>2],{0}[uint(addr-{1}+4)>>2],{0}["
  624. "uint(addr-{1}+8)>>2],{0}[uint(addr-{1}+12)>>2]);}}");
  625. }
  626. write_func += '}';
  627. write_func_64 += '}';
  628. write_func_128 += '}';
  629. load_func += "return 0u;}";
  630. load_func_64 += "return uvec2(0);}";
  631. load_func_128 += "return uvec4(0);}";
  632. return write_func + write_func_64 + write_func_128 + load_func + load_func_64 + load_func_128;
  633. }
  634. void EmitContext::SetupImages(Bindings& bindings) {
  635. image_buffers.reserve(info.image_buffer_descriptors.size());
  636. for (const auto& desc : info.image_buffer_descriptors) {
  637. image_buffers.push_back({bindings.image, desc.count});
  638. const auto format{ImageFormatString(desc.format)};
  639. const auto qualifier{ImageAccessQualifier(desc.is_written, desc.is_read)};
  640. const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
  641. header += fmt::format("layout(binding={}{}) uniform {}uimageBuffer img{}{};",
  642. bindings.image, format, qualifier, bindings.image, array_decorator);
  643. bindings.image += desc.count;
  644. }
  645. images.reserve(info.image_descriptors.size());
  646. for (const auto& desc : info.image_descriptors) {
  647. images.push_back({bindings.image, desc.count});
  648. const auto format{ImageFormatString(desc.format)};
  649. const auto image_type{ImageType(desc.type)};
  650. const auto qualifier{ImageAccessQualifier(desc.is_written, desc.is_read)};
  651. const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
  652. header += fmt::format("layout(binding={}{})uniform {}{} img{}{};", bindings.image, format,
  653. qualifier, image_type, bindings.image, array_decorator);
  654. bindings.image += desc.count;
  655. }
  656. }
  657. void EmitContext::SetupTextures(Bindings& bindings) {
  658. texture_buffers.reserve(info.texture_buffer_descriptors.size());
  659. for (const auto& desc : info.texture_buffer_descriptors) {
  660. texture_buffers.push_back({bindings.texture, desc.count});
  661. const auto sampler_type{SamplerType(TextureType::Buffer, false)};
  662. const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
  663. header += fmt::format("layout(binding={}) uniform {} tex{}{};", bindings.texture,
  664. sampler_type, bindings.texture, array_decorator);
  665. bindings.texture += desc.count;
  666. }
  667. textures.reserve(info.texture_descriptors.size());
  668. for (const auto& desc : info.texture_descriptors) {
  669. textures.push_back({bindings.texture, desc.count});
  670. const auto sampler_type{SamplerType(desc.type, desc.is_depth)};
  671. const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
  672. header += fmt::format("layout(binding={}) uniform {} tex{}{};", bindings.texture,
  673. sampler_type, bindings.texture, array_decorator);
  674. bindings.texture += desc.count;
  675. }
  676. }
  677. void EmitContext::DefineConstants() {
  678. if (info.uses_fswzadd) {
  679. header += "const float FSWZ_A[]=float[4](-1.f,1.f,-1.f,0.f);"
  680. "const float FSWZ_B[]=float[4](-1.f,-1.f,1.f,-1.f);";
  681. }
  682. }
  683. } // namespace Shader::Backend::GLSL