emit_glsl_context_get_set.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string_view>
  5. #include "shader_recompiler/backend/glsl/emit_context.h"
  6. #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
  7. #include "shader_recompiler/frontend/ir/value.h"
  8. #include "shader_recompiler/profile.h"
  9. namespace Shader::Backend::GLSL {
  10. namespace {
  11. constexpr char SWIZZLE[]{"xyzw"};
  12. u32 CbufIndex(u32 offset) {
  13. return (offset / 4) % 4;
  14. }
  15. char OffsetSwizzle(u32 offset) {
  16. return SWIZZLE[CbufIndex(offset)];
  17. }
  18. bool IsInputArray(Stage stage) {
  19. return stage == Stage::Geometry || stage == Stage::TessellationControl ||
  20. stage == Stage::TessellationEval;
  21. }
  22. std::string InputVertexIndex(EmitContext& ctx, std::string_view vertex) {
  23. return IsInputArray(ctx.stage) ? fmt::format("[{}]", vertex) : "";
  24. }
  25. std::string OutputVertexIndex(EmitContext& ctx) {
  26. return ctx.stage == Stage::TessellationControl ? "[gl_InvocationID]" : "";
  27. }
  28. void GetCbuf(EmitContext& ctx, std::string_view ret, const IR::Value& binding,
  29. const IR::Value& offset, u32 num_bits, std::string_view cast = {},
  30. bool component_indexing_bug = false, std::string_view bit_offset = {}) {
  31. const bool is_immediate{offset.IsImmediate()};
  32. if (is_immediate) {
  33. const s32 signed_offset{static_cast<s32>(offset.U32())};
  34. static constexpr u32 cbuf_size{4096 * 16};
  35. if (signed_offset < 0 || offset.U32() > cbuf_size) {
  36. // LOG_WARNING(..., "Immediate constant buffer offset is out of bounds");
  37. ctx.Add("{}=0u;", ret);
  38. return;
  39. }
  40. }
  41. const auto offset_var{ctx.var_alloc.Consume(offset)};
  42. const auto index{is_immediate ? fmt::format("{}", offset.U32() / 16)
  43. : fmt::format("{}>>4", offset_var)};
  44. const auto swizzle{is_immediate ? fmt::format(".{}", OffsetSwizzle(offset.U32()))
  45. : fmt::format("[({}>>2)%4]", offset_var)};
  46. const auto cbuf{fmt::format("{}_cbuf{}", ctx.stage_name, binding.U32())};
  47. const auto cbuf_cast{fmt::format("{}({}[{}]{{}})", cast, cbuf, index)};
  48. const auto extraction{num_bits == 32 ? cbuf_cast
  49. : fmt ::format("bitfieldExtract({},int({}),{})", cbuf_cast,
  50. bit_offset, num_bits)};
  51. if (!component_indexing_bug) {
  52. const auto result{fmt::format(extraction, swizzle)};
  53. ctx.Add("{}={};", ret, result);
  54. return;
  55. }
  56. const auto cbuf_offset{fmt::format("{}>>2", offset_var)};
  57. for (u32 i = 0; i < 4; ++i) {
  58. const auto swizzle_string{fmt::format(".{}", "xyzw"[i])};
  59. const auto result{fmt::format(extraction, swizzle_string)};
  60. ctx.Add("if(({}&3)=={}){}={};", cbuf_offset, i, ret, result);
  61. }
  62. }
  63. void GetCbuf8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, const IR::Value& offset,
  64. std::string_view cast) {
  65. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
  66. if (offset.IsImmediate()) {
  67. const auto bit_offset{fmt::format("{}", (offset.U32() % 4) * 8)};
  68. GetCbuf(ctx, ret, binding, offset, 8, cast, false, bit_offset);
  69. } else {
  70. const auto offset_var{ctx.var_alloc.Consume(offset)};
  71. const auto bit_offset{fmt::format("({}%4)*8", offset_var)};
  72. GetCbuf(ctx, ret, binding, offset, 8, cast, ctx.profile.has_gl_component_indexing_bug,
  73. bit_offset);
  74. }
  75. }
  76. void GetCbuf16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, const IR::Value& offset,
  77. std::string_view cast) {
  78. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
  79. if (offset.IsImmediate()) {
  80. const auto bit_offset{fmt::format("{}", ((offset.U32() / 2) % 2) * 16)};
  81. GetCbuf(ctx, ret, binding, offset, 16, cast, false, bit_offset);
  82. } else {
  83. const auto offset_var{ctx.var_alloc.Consume(offset)};
  84. const auto bit_offset{fmt::format("(({}>>1)%2)*16", offset_var)};
  85. GetCbuf(ctx, ret, binding, offset, 16, cast, ctx.profile.has_gl_component_indexing_bug,
  86. bit_offset);
  87. }
  88. }
  89. u32 TexCoordIndex(IR::Attribute attr) {
  90. return (static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
  91. }
  92. } // Anonymous namespace
  93. void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  94. const IR::Value& offset) {
  95. GetCbuf8(ctx, inst, binding, offset, "ftou");
  96. }
  97. void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  98. const IR::Value& offset) {
  99. GetCbuf8(ctx, inst, binding, offset, "ftoi");
  100. }
  101. void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  102. const IR::Value& offset) {
  103. GetCbuf16(ctx, inst, binding, offset, "ftou");
  104. }
  105. void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  106. const IR::Value& offset) {
  107. GetCbuf16(ctx, inst, binding, offset, "ftoi");
  108. }
  109. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  110. const IR::Value& offset) {
  111. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
  112. GetCbuf(ctx, ret, binding, offset, 32, "ftou");
  113. }
  114. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  115. const IR::Value& offset) {
  116. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32)};
  117. GetCbuf(ctx, ret, binding, offset, 32);
  118. }
  119. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  120. const IR::Value& offset) {
  121. const auto cbuf{fmt::format("{}_cbuf{}", ctx.stage_name, binding.U32())};
  122. if (offset.IsImmediate()) {
  123. static constexpr u32 cbuf_size{4096 * 16};
  124. const u32 u32_offset{offset.U32()};
  125. const s32 signed_offset{static_cast<s32>(offset.U32())};
  126. if (signed_offset < 0 || u32_offset > cbuf_size) {
  127. // LOG_WARNING(..., "Immediate constant buffer offset is out of bounds");
  128. ctx.AddU32x2("{}=uvec2(0u);", inst);
  129. return;
  130. }
  131. if (u32_offset % 2 == 0) {
  132. ctx.AddU32x2("{}=ftou({}[{}].{}{});", inst, cbuf, u32_offset / 16,
  133. OffsetSwizzle(u32_offset), OffsetSwizzle(u32_offset + 4));
  134. } else {
  135. ctx.AddU32x2("{}=uvec2(ftou({}[{}].{}),ftou({}[{}].{}));", inst, cbuf, u32_offset / 16,
  136. OffsetSwizzle(u32_offset), cbuf, (u32_offset + 4) / 16,
  137. OffsetSwizzle(u32_offset + 4));
  138. }
  139. return;
  140. }
  141. const auto offset_var{ctx.var_alloc.Consume(offset)};
  142. if (!ctx.profile.has_gl_component_indexing_bug) {
  143. ctx.AddU32x2("{}=uvec2(ftou({}[{}>>4][({}>>2)%4]),ftou({}[({}+4)>>4][(({}+4)>>2)%4]));",
  144. inst, cbuf, offset_var, offset_var, cbuf, offset_var, offset_var);
  145. return;
  146. }
  147. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x2)};
  148. const auto cbuf_offset{fmt::format("{}>>2", offset_var)};
  149. for (u32 swizzle = 0; swizzle < 4; ++swizzle) {
  150. ctx.Add("if(({}&3)=={}){}=uvec2(ftou({}[{}>>4].{}),ftou({}[({}+4)>>4].{}));", cbuf_offset,
  151. swizzle, ret, cbuf, offset_var, "xyzw"[swizzle], cbuf, offset_var,
  152. "xyzw"[(swizzle + 1) % 4]);
  153. }
  154. }
  155. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
  156. std::string_view vertex) {
  157. const u32 element{static_cast<u32>(attr) % 4};
  158. const char swizzle{"xyzw"[element]};
  159. if (IR::IsGeneric(attr)) {
  160. const u32 index{IR::GenericAttributeIndex(attr)};
  161. ctx.AddF32("{}=in_attr{}{}.{};", inst, index, InputVertexIndex(ctx, vertex), swizzle);
  162. return;
  163. }
  164. // GLSL only exposes 8 legacy texcoords
  165. if (attr >= IR::Attribute::FixedFncTexture8S && attr <= IR::Attribute::FixedFncTexture9Q) {
  166. // LOG_WARNING(..., "GLSL does not allow access to gl_TexCoord[{}]", TexCoordIndex(attr));
  167. ctx.AddF32("{}=0.f;", inst);
  168. return;
  169. }
  170. if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture7Q) {
  171. const u32 index{TexCoordIndex(attr)};
  172. ctx.AddF32("{}=gl_TexCoord[{}].{};", inst, index, swizzle);
  173. return;
  174. }
  175. switch (attr) {
  176. case IR::Attribute::PrimitiveId:
  177. ctx.AddF32("{}=itof(gl_PrimitiveID);", inst);
  178. break;
  179. case IR::Attribute::PositionX:
  180. case IR::Attribute::PositionY:
  181. case IR::Attribute::PositionZ:
  182. case IR::Attribute::PositionW: {
  183. const bool is_array{IsInputArray(ctx.stage)};
  184. const auto input_decorator{is_array ? fmt::format("gl_in[{}].", vertex) : ""};
  185. ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle);
  186. break;
  187. }
  188. case IR::Attribute::ColorFrontDiffuseR:
  189. case IR::Attribute::ColorFrontDiffuseG:
  190. case IR::Attribute::ColorFrontDiffuseB:
  191. case IR::Attribute::ColorFrontDiffuseA:
  192. if (ctx.stage == Stage::Fragment) {
  193. ctx.AddF32("{}=gl_Color.{};", inst, swizzle);
  194. } else {
  195. ctx.AddF32("{}=gl_FrontColor.{};", inst, swizzle);
  196. }
  197. break;
  198. case IR::Attribute::PointSpriteS:
  199. case IR::Attribute::PointSpriteT:
  200. ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle);
  201. break;
  202. case IR::Attribute::TessellationEvaluationPointU:
  203. case IR::Attribute::TessellationEvaluationPointV:
  204. ctx.AddF32("{}=gl_TessCoord.{};", inst, swizzle);
  205. break;
  206. case IR::Attribute::InstanceId:
  207. ctx.AddF32("{}=itof(gl_InstanceID);", inst);
  208. break;
  209. case IR::Attribute::VertexId:
  210. ctx.AddF32("{}=itof(gl_VertexID);", inst);
  211. break;
  212. case IR::Attribute::FrontFace:
  213. ctx.AddF32("{}=itof(gl_FrontFacing?-1:0);", inst);
  214. break;
  215. default:
  216. throw NotImplementedException("Get attribute {}", attr);
  217. }
  218. }
  219. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value,
  220. [[maybe_unused]] std::string_view vertex) {
  221. if (IR::IsGeneric(attr)) {
  222. const u32 index{IR::GenericAttributeIndex(attr)};
  223. const u32 attr_element{IR::GenericAttributeElement(attr)};
  224. const GenericElementInfo& info{ctx.output_generics.at(index).at(attr_element)};
  225. const auto output_decorator{OutputVertexIndex(ctx)};
  226. if (info.num_components == 1) {
  227. ctx.Add("{}{}={};", info.name, output_decorator, value);
  228. } else {
  229. const u32 index_element{attr_element - info.first_element};
  230. ctx.Add("{}{}.{}={};", info.name, output_decorator, "xyzw"[index_element], value);
  231. }
  232. return;
  233. }
  234. const u32 element{static_cast<u32>(attr) % 4};
  235. const char swizzle{"xyzw"[element]};
  236. // GLSL only exposes 8 legacy texcoords
  237. if (attr >= IR::Attribute::FixedFncTexture8S && attr <= IR::Attribute::FixedFncTexture9Q) {
  238. // LOG_WARNING(..., "GLSL does not allow access to gl_TexCoord[{}]", TexCoordIndex(attr));
  239. return;
  240. }
  241. if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture7Q) {
  242. const u32 index{TexCoordIndex(attr)};
  243. ctx.Add("gl_TexCoord[{}].{}={};", index, swizzle, value);
  244. return;
  245. }
  246. switch (attr) {
  247. case IR::Attribute::Layer:
  248. if (ctx.stage != Stage::Geometry &&
  249. !ctx.profile.support_viewport_index_layer_non_geometry) {
  250. // LOG_WARNING(..., "Shader stores viewport layer but device does not support viewport
  251. // layer extension");
  252. break;
  253. }
  254. ctx.Add("gl_Layer=ftoi({});", value);
  255. break;
  256. case IR::Attribute::ViewportIndex:
  257. if (ctx.stage != Stage::Geometry &&
  258. !ctx.profile.support_viewport_index_layer_non_geometry) {
  259. // LOG_WARNING(..., "Shader stores viewport index but device does not support viewport
  260. // layer extension");
  261. break;
  262. }
  263. ctx.Add("gl_ViewportIndex=ftoi({});", value);
  264. break;
  265. case IR::Attribute::ViewportMask:
  266. if (ctx.stage != Stage::Geometry && !ctx.profile.support_viewport_mask) {
  267. // LOG_WARNING(..., "Shader stores viewport mask but device does not support viewport
  268. // mask extension");
  269. break;
  270. }
  271. ctx.Add("gl_ViewportMask[0]=ftoi({});", value);
  272. break;
  273. case IR::Attribute::PointSize:
  274. ctx.Add("gl_PointSize={};", value);
  275. break;
  276. case IR::Attribute::PositionX:
  277. case IR::Attribute::PositionY:
  278. case IR::Attribute::PositionZ:
  279. case IR::Attribute::PositionW:
  280. ctx.Add("gl_Position.{}={};", swizzle, value);
  281. break;
  282. case IR::Attribute::ColorFrontDiffuseR:
  283. case IR::Attribute::ColorFrontDiffuseG:
  284. case IR::Attribute::ColorFrontDiffuseB:
  285. case IR::Attribute::ColorFrontDiffuseA:
  286. if (ctx.stage == Stage::Fragment) {
  287. ctx.Add("gl_Color.{}={};", swizzle, value);
  288. } else {
  289. ctx.Add("gl_FrontColor.{}={};", swizzle, value);
  290. }
  291. break;
  292. case IR::Attribute::ColorFrontSpecularR:
  293. case IR::Attribute::ColorFrontSpecularG:
  294. case IR::Attribute::ColorFrontSpecularB:
  295. case IR::Attribute::ColorFrontSpecularA:
  296. if (ctx.stage == Stage::Fragment) {
  297. ctx.Add("gl_SecondaryColor.{}={};", swizzle, value);
  298. } else {
  299. ctx.Add("gl_FrontSecondaryColor.{}={};", swizzle, value);
  300. }
  301. break;
  302. case IR::Attribute::ColorBackDiffuseR:
  303. case IR::Attribute::ColorBackDiffuseG:
  304. case IR::Attribute::ColorBackDiffuseB:
  305. case IR::Attribute::ColorBackDiffuseA:
  306. ctx.Add("gl_BackColor.{}={};", swizzle, value);
  307. break;
  308. case IR::Attribute::ColorBackSpecularR:
  309. case IR::Attribute::ColorBackSpecularG:
  310. case IR::Attribute::ColorBackSpecularB:
  311. case IR::Attribute::ColorBackSpecularA:
  312. ctx.Add("gl_BackSecondaryColor.{}={};", swizzle, value);
  313. break;
  314. case IR::Attribute::FogCoordinate:
  315. ctx.Add("gl_FogFragCoord.x={};", value);
  316. break;
  317. case IR::Attribute::ClipDistance0:
  318. case IR::Attribute::ClipDistance1:
  319. case IR::Attribute::ClipDistance2:
  320. case IR::Attribute::ClipDistance3:
  321. case IR::Attribute::ClipDistance4:
  322. case IR::Attribute::ClipDistance5:
  323. case IR::Attribute::ClipDistance6:
  324. case IR::Attribute::ClipDistance7: {
  325. const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
  326. ctx.Add("gl_ClipDistance[{}]={};", index, value);
  327. break;
  328. }
  329. default:
  330. throw NotImplementedException("Set attribute {}", attr);
  331. }
  332. }
  333. void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, std::string_view offset,
  334. std::string_view vertex) {
  335. const bool is_array{ctx.stage == Stage::Geometry};
  336. const auto vertex_arg{is_array ? fmt::format(",{}", vertex) : ""};
  337. ctx.AddF32("{}=IndexedAttrLoad(int({}){});", inst, offset, vertex_arg);
  338. }
  339. void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx,
  340. [[maybe_unused]] std::string_view offset,
  341. [[maybe_unused]] std::string_view value,
  342. [[maybe_unused]] std::string_view vertex) {
  343. NotImplemented();
  344. }
  345. void EmitGetPatch(EmitContext& ctx, IR::Inst& inst, IR::Patch patch) {
  346. if (!IR::IsGeneric(patch)) {
  347. throw NotImplementedException("Non-generic patch load");
  348. }
  349. const u32 index{IR::GenericPatchIndex(patch)};
  350. const u32 element{IR::GenericPatchElement(patch)};
  351. const char swizzle{"xyzw"[element]};
  352. ctx.AddF32("{}=patch{}.{};", inst, index, swizzle);
  353. }
  354. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, std::string_view value) {
  355. if (IR::IsGeneric(patch)) {
  356. const u32 index{IR::GenericPatchIndex(patch)};
  357. const u32 element{IR::GenericPatchElement(patch)};
  358. ctx.Add("patch{}.{}={};", index, "xyzw"[element], value);
  359. return;
  360. }
  361. switch (patch) {
  362. case IR::Patch::TessellationLodLeft:
  363. case IR::Patch::TessellationLodRight:
  364. case IR::Patch::TessellationLodTop:
  365. case IR::Patch::TessellationLodBottom: {
  366. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  367. ctx.Add("gl_TessLevelOuter[{}]={};", index, value);
  368. break;
  369. }
  370. case IR::Patch::TessellationLodInteriorU:
  371. ctx.Add("gl_TessLevelInner[0]={};", value);
  372. break;
  373. case IR::Patch::TessellationLodInteriorV:
  374. ctx.Add("gl_TessLevelInner[1]={};", value);
  375. break;
  376. default:
  377. throw NotImplementedException("Patch {}", patch);
  378. }
  379. }
  380. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, std::string_view value) {
  381. const char swizzle{"xyzw"[component]};
  382. ctx.Add("frag_color{}.{}={};", index, swizzle, value);
  383. }
  384. void EmitSetSampleMask(EmitContext& ctx, std::string_view value) {
  385. ctx.Add("gl_SampleMask[0]=int({})", value);
  386. }
  387. void EmitSetFragDepth(EmitContext& ctx, std::string_view value) {
  388. ctx.Add("gl_FragDepth={};", value);
  389. }
  390. void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst) {
  391. ctx.AddU32x3("{}=gl_LocalInvocationID;", inst);
  392. }
  393. void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst) {
  394. ctx.AddU32x3("{}=gl_WorkGroupID;", inst);
  395. }
  396. void EmitInvocationId(EmitContext& ctx, IR::Inst& inst) {
  397. ctx.AddU32("{}=uint(gl_InvocationID);", inst);
  398. }
  399. void EmitSampleId(EmitContext& ctx, IR::Inst& inst) {
  400. ctx.AddU32("{}=uint(gl_SampleID);", inst);
  401. }
  402. void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst) {
  403. ctx.AddU1("{}=gl_HelperInvocation;", inst);
  404. }
  405. void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {
  406. ctx.uses_y_direction = true;
  407. ctx.AddF32("{}=gl_FrontMaterial.ambient.a;", inst);
  408. }
  409. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) {
  410. ctx.AddU32("{}=lmem[{}];", inst, word_offset);
  411. }
  412. void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value) {
  413. ctx.Add("lmem[{}]={};", word_offset, value);
  414. }
  415. } // namespace Shader::Backend::GLSL