emit_glasm_context_get_set.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project, 2024 sudachi Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string_view>
  4. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  5. #include "shader_recompiler/backend/glasm/glasm_emit_context.h"
  6. #include "shader_recompiler/frontend/ir/value.h"
  7. #include "shader_recompiler/profile.h"
  8. #include "shader_recompiler/runtime_info.h"
  9. #include "shader_recompiler/shader_info.h"
  10. namespace Shader::Backend::GLASM {
  11. namespace {
  12. void GetCbuf(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset,
  13. std::string_view size) {
  14. const Register ret{ctx.reg_alloc.Define(inst)};
  15. if (offset.type == Type::U32) {
  16. // Avoid reading arrays out of bounds, matching hardware's behavior
  17. if (offset.imm_u32 >= 0x10'000) {
  18. ctx.Add("MOV.S {},0;", ret);
  19. return;
  20. }
  21. }
  22. if (binding.IsImmediate()) {
  23. ctx.Add("LDC.{} {},c{}[{}];", size, ret, binding.U32(), offset);
  24. return;
  25. }
  26. const ScalarU32 idx{ctx.reg_alloc.Consume(binding)};
  27. for (u32 i = 0; i < Info::MAX_INDIRECT_CBUFS; i++) {
  28. ctx.Add("SEQ.S.CC RC.x,{},{};"
  29. "IF NE.x;"
  30. "LDC.{} {},c{}[{}];",
  31. idx, i, size, ret, i, offset);
  32. if (i != Info::MAX_INDIRECT_CBUFS - 1) {
  33. ctx.Add("ELSE;");
  34. }
  35. }
  36. for (u32 i = 0; i < Info::MAX_INDIRECT_CBUFS; i++) {
  37. ctx.Add("ENDIF;");
  38. }
  39. }
  40. bool IsInputArray(Stage stage) {
  41. return stage == Stage::Geometry || stage == Stage::TessellationControl ||
  42. stage == Stage::TessellationEval;
  43. }
  44. std::string VertexIndex(EmitContext& ctx, ScalarU32 vertex) {
  45. return IsInputArray(ctx.stage) ? fmt::format("[{}]", vertex) : "";
  46. }
  47. u32 TexCoordIndex(IR::Attribute attr) {
  48. return (static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
  49. }
  50. } // Anonymous namespace
  51. void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  52. GetCbuf(ctx, inst, binding, offset, "U8");
  53. }
  54. void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  55. GetCbuf(ctx, inst, binding, offset, "S8");
  56. }
  57. void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  58. GetCbuf(ctx, inst, binding, offset, "U16");
  59. }
  60. void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  61. GetCbuf(ctx, inst, binding, offset, "S16");
  62. }
  63. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  64. GetCbuf(ctx, inst, binding, offset, "U32");
  65. }
  66. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset) {
  67. GetCbuf(ctx, inst, binding, offset, "F32");
  68. }
  69. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  70. ScalarU32 offset) {
  71. GetCbuf(ctx, inst, binding, offset, "U32X2");
  72. }
  73. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex) {
  74. const u32 element{static_cast<u32>(attr) % 4};
  75. const char swizzle{"xyzw"[element]};
  76. if (IR::IsGeneric(attr)) {
  77. const u32 index{IR::GenericAttributeIndex(attr)};
  78. ctx.Add("MOV.F {}.x,in_attr{}{}[0].{};", inst, index, VertexIndex(ctx, vertex), swizzle);
  79. return;
  80. }
  81. if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture9Q) {
  82. const u32 index{TexCoordIndex(attr)};
  83. ctx.Add("MOV.F {}.x,{}.texcoord[{}].{};", inst, ctx.attrib_name, index, swizzle);
  84. return;
  85. }
  86. switch (attr) {
  87. case IR::Attribute::PrimitiveId:
  88. ctx.Add("MOV.F {}.x,primitive.id;", inst);
  89. break;
  90. case IR::Attribute::Layer:
  91. ctx.Add("MOV.F {}.x,fragment.layer;", inst);
  92. break;
  93. case IR::Attribute::PositionX:
  94. case IR::Attribute::PositionY:
  95. case IR::Attribute::PositionZ:
  96. case IR::Attribute::PositionW:
  97. if (IsInputArray(ctx.stage)) {
  98. ctx.Add("MOV.F {}.x,vertex_position{}.{};", inst, VertexIndex(ctx, vertex), swizzle);
  99. } else {
  100. ctx.Add("MOV.F {}.x,{}.position.{};", inst, ctx.attrib_name, swizzle);
  101. }
  102. break;
  103. case IR::Attribute::ColorFrontDiffuseR:
  104. case IR::Attribute::ColorFrontDiffuseG:
  105. case IR::Attribute::ColorFrontDiffuseB:
  106. case IR::Attribute::ColorFrontDiffuseA:
  107. ctx.Add("MOV.F {}.x,{}.color.{};", inst, ctx.attrib_name, swizzle);
  108. break;
  109. case IR::Attribute::PointSpriteS:
  110. case IR::Attribute::PointSpriteT:
  111. ctx.Add("MOV.F {}.x,{}.pointcoord.{};", inst, ctx.attrib_name, swizzle);
  112. break;
  113. case IR::Attribute::TessellationEvaluationPointU:
  114. case IR::Attribute::TessellationEvaluationPointV:
  115. ctx.Add("MOV.F {}.x,vertex.tesscoord.{};", inst, swizzle);
  116. break;
  117. case IR::Attribute::InstanceId:
  118. ctx.Add("MOV.F {}.x,{}.instance;", inst, ctx.attrib_name);
  119. break;
  120. case IR::Attribute::VertexId:
  121. ctx.Add("MOV.F {}.x,{}.id;", inst, ctx.attrib_name);
  122. break;
  123. case IR::Attribute::BaseInstance:
  124. ctx.Add("MOV.F {}.x,{}.baseInstance;", inst, ctx.attrib_name);
  125. break;
  126. case IR::Attribute::BaseVertex:
  127. ctx.Add("MOV.F {}.x,{}.baseVertex;", inst, ctx.attrib_name);
  128. break;
  129. case IR::Attribute::DrawID:
  130. ctx.Add("MOV.F {}.x,{}.draw.id;", inst, ctx.attrib_name);
  131. break;
  132. case IR::Attribute::FrontFace:
  133. ctx.Add("CMP.F {}.x,{}.facing.x,0,-1;", inst, ctx.attrib_name);
  134. break;
  135. default:
  136. throw NotImplementedException("Get attribute {}", attr);
  137. }
  138. }
  139. void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32) {
  140. switch (attr) {
  141. case IR::Attribute::PrimitiveId:
  142. ctx.Add("MOV.S {}.x,primitive.id;", inst);
  143. break;
  144. case IR::Attribute::InstanceId:
  145. ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.attrib_name);
  146. break;
  147. case IR::Attribute::VertexId:
  148. ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.attrib_name);
  149. break;
  150. case IR::Attribute::BaseInstance:
  151. ctx.Add("MOV.S {}.x,{}.baseInstance;", inst, ctx.attrib_name);
  152. break;
  153. case IR::Attribute::BaseVertex:
  154. ctx.Add("MOV.S {}.x,{}.baseVertex;", inst, ctx.attrib_name);
  155. break;
  156. case IR::Attribute::DrawID:
  157. ctx.Add("MOV.S {}.x,{}.draw.id;", inst, ctx.attrib_name);
  158. break;
  159. default:
  160. throw NotImplementedException("Get U32 attribute {}", attr);
  161. }
  162. }
  163. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
  164. [[maybe_unused]] ScalarU32 vertex) {
  165. const u32 element{static_cast<u32>(attr) % 4};
  166. const char swizzle{"xyzw"[element]};
  167. if (IR::IsGeneric(attr)) {
  168. const u32 index{IR::GenericAttributeIndex(attr)};
  169. ctx.Add("MOV.F out_attr{}[0].{},{};", index, swizzle, value);
  170. return;
  171. }
  172. if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture9R) {
  173. const u32 index{TexCoordIndex(attr)};
  174. ctx.Add("MOV.F result.texcoord[{}].{},{};", index, swizzle, value);
  175. return;
  176. }
  177. switch (attr) {
  178. case IR::Attribute::Layer:
  179. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  180. ctx.Add("MOV.F result.layer.x,{};", value);
  181. } else {
  182. LOG_WARNING(Shader_GLASM,
  183. "Layer stored outside of geometry shader not supported by device");
  184. }
  185. break;
  186. case IR::Attribute::ViewportIndex:
  187. if (ctx.stage == Stage::Geometry || ctx.profile.support_viewport_index_layer_non_geometry) {
  188. ctx.Add("MOV.F result.viewport.x,{};", value);
  189. } else {
  190. LOG_WARNING(Shader_GLASM,
  191. "Viewport stored outside of geometry shader not supported by device");
  192. }
  193. break;
  194. case IR::Attribute::ViewportMask:
  195. // NV_viewport_array2 is required to access result.viewportmask, regardless of shader stage.
  196. if (ctx.profile.support_viewport_index_layer_non_geometry) {
  197. ctx.Add("MOV.F result.viewportmask[0].x,{};", value);
  198. } else {
  199. LOG_WARNING(Shader_GLASM, "Device does not support storing to ViewportMask");
  200. }
  201. break;
  202. case IR::Attribute::PointSize:
  203. ctx.Add("MOV.F result.pointsize.x,{};", value);
  204. break;
  205. case IR::Attribute::PositionX:
  206. case IR::Attribute::PositionY:
  207. case IR::Attribute::PositionZ:
  208. case IR::Attribute::PositionW:
  209. ctx.Add("MOV.F result.position.{},{};", swizzle, value);
  210. break;
  211. case IR::Attribute::ColorFrontDiffuseR:
  212. case IR::Attribute::ColorFrontDiffuseG:
  213. case IR::Attribute::ColorFrontDiffuseB:
  214. case IR::Attribute::ColorFrontDiffuseA:
  215. ctx.Add("MOV.F result.color.{},{};", swizzle, value);
  216. break;
  217. case IR::Attribute::ColorFrontSpecularR:
  218. case IR::Attribute::ColorFrontSpecularG:
  219. case IR::Attribute::ColorFrontSpecularB:
  220. case IR::Attribute::ColorFrontSpecularA:
  221. ctx.Add("MOV.F result.color.secondary.{},{};", swizzle, value);
  222. break;
  223. case IR::Attribute::ColorBackDiffuseR:
  224. case IR::Attribute::ColorBackDiffuseG:
  225. case IR::Attribute::ColorBackDiffuseB:
  226. case IR::Attribute::ColorBackDiffuseA:
  227. ctx.Add("MOV.F result.color.back.{},{};", swizzle, value);
  228. break;
  229. case IR::Attribute::ColorBackSpecularR:
  230. case IR::Attribute::ColorBackSpecularG:
  231. case IR::Attribute::ColorBackSpecularB:
  232. case IR::Attribute::ColorBackSpecularA:
  233. ctx.Add("MOV.F result.color.back.secondary.{},{};", swizzle, value);
  234. break;
  235. case IR::Attribute::FogCoordinate:
  236. ctx.Add("MOV.F result.fogcoord.x,{};", value);
  237. break;
  238. case IR::Attribute::ClipDistance0:
  239. case IR::Attribute::ClipDistance1:
  240. case IR::Attribute::ClipDistance2:
  241. case IR::Attribute::ClipDistance3:
  242. case IR::Attribute::ClipDistance4:
  243. case IR::Attribute::ClipDistance5:
  244. case IR::Attribute::ClipDistance6:
  245. case IR::Attribute::ClipDistance7: {
  246. const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
  247. ctx.Add("MOV.F result.clip[{}].x,{};", index, value);
  248. break;
  249. }
  250. default:
  251. throw NotImplementedException("Set attribute {}", attr);
  252. }
  253. }
  254. void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, ScalarS32 offset, ScalarU32 vertex) {
  255. // RC.x = base_index
  256. // RC.y = masked_index
  257. // RC.z = compare_index
  258. ctx.Add("SHR.S RC.x,{},2;"
  259. "AND.S RC.y,RC.x,3;"
  260. "SHR.S RC.z,{},4;",
  261. offset, offset);
  262. const Register ret{ctx.reg_alloc.Define(inst)};
  263. u32 num_endifs{};
  264. const auto read{[&](u32 compare_index, const std::array<std::string, 4>& values) {
  265. ++num_endifs;
  266. ctx.Add("SEQ.S.CC RC.w,RC.z,{};" // compare_index
  267. "IF NE.w;"
  268. // X
  269. "SEQ.S.CC RC.w,RC.y,0;"
  270. "IF NE.w;"
  271. "MOV {}.x,{};"
  272. "ELSE;"
  273. // Y
  274. "SEQ.S.CC RC.w,RC.y,1;"
  275. "IF NE.w;"
  276. "MOV {}.x,{};"
  277. "ELSE;"
  278. // Z
  279. "SEQ.S.CC RC.w,RC.y,2;"
  280. "IF NE.w;"
  281. "MOV {}.x,{};"
  282. "ELSE;"
  283. // W
  284. "MOV {}.x,{};"
  285. "ENDIF;"
  286. "ENDIF;"
  287. "ENDIF;"
  288. "ELSE;",
  289. compare_index, ret, values[0], ret, values[1], ret, values[2], ret, values[3]);
  290. }};
  291. const auto read_swizzled{[&](u32 compare_index, std::string_view value) {
  292. const std::array values{fmt::format("{}.x", value), fmt::format("{}.y", value),
  293. fmt::format("{}.z", value), fmt::format("{}.w", value)};
  294. read(compare_index, values);
  295. }};
  296. if (ctx.info.loads.AnyComponent(IR::Attribute::PositionX)) {
  297. const u32 index{static_cast<u32>(IR::Attribute::PositionX)};
  298. if (IsInputArray(ctx.stage)) {
  299. read_swizzled(index, fmt::format("vertex_position{}", VertexIndex(ctx, vertex)));
  300. } else {
  301. read_swizzled(index, fmt::format("{}.position", ctx.attrib_name));
  302. }
  303. }
  304. for (u32 index = 0; index < static_cast<u32>(IR::NUM_GENERICS); ++index) {
  305. if (!ctx.info.loads.Generic(index)) {
  306. continue;
  307. }
  308. read_swizzled(index, fmt::format("in_attr{}{}[0]", index, VertexIndex(ctx, vertex)));
  309. }
  310. for (u32 i = 0; i < num_endifs; ++i) {
  311. ctx.Add("ENDIF;");
  312. }
  313. }
  314. void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] ScalarU32 offset,
  315. [[maybe_unused]] ScalarF32 value, [[maybe_unused]] ScalarU32 vertex) {
  316. throw NotImplementedException("GLASM instruction");
  317. }
  318. void EmitGetPatch(EmitContext& ctx, IR::Inst& inst, IR::Patch patch) {
  319. if (!IR::IsGeneric(patch)) {
  320. throw NotImplementedException("Non-generic patch load");
  321. }
  322. const u32 index{IR::GenericPatchIndex(patch)};
  323. const u32 element{IR::GenericPatchElement(patch)};
  324. const char swizzle{"xyzw"[element]};
  325. const std::string_view out{ctx.stage == Stage::TessellationControl ? ".out" : ""};
  326. ctx.Add("MOV.F {},primitive{}.patch.attrib[{}].{};", inst, out, index, swizzle);
  327. }
  328. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, ScalarF32 value) {
  329. if (IR::IsGeneric(patch)) {
  330. const u32 index{IR::GenericPatchIndex(patch)};
  331. const u32 element{IR::GenericPatchElement(patch)};
  332. ctx.Add("MOV.F result.patch.attrib[{}].{},{};", index, "xyzw"[element], value);
  333. return;
  334. }
  335. switch (patch) {
  336. case IR::Patch::TessellationLodLeft:
  337. case IR::Patch::TessellationLodRight:
  338. case IR::Patch::TessellationLodTop:
  339. case IR::Patch::TessellationLodBottom: {
  340. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  341. ctx.Add("MOV.F result.patch.tessouter[{}].x,{};", index, value);
  342. break;
  343. }
  344. case IR::Patch::TessellationLodInteriorU:
  345. ctx.Add("MOV.F result.patch.tessinner[0].x,{};", value);
  346. break;
  347. case IR::Patch::TessellationLodInteriorV:
  348. ctx.Add("MOV.F result.patch.tessinner[1].x,{};", value);
  349. break;
  350. default:
  351. throw NotImplementedException("Patch {}", patch);
  352. }
  353. }
  354. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, ScalarF32 value) {
  355. ctx.Add("MOV.F frag_color{}.{},{};", index, "xyzw"[component], value);
  356. }
  357. void EmitSetSampleMask(EmitContext& ctx, ScalarS32 value) {
  358. ctx.Add("MOV.S result.samplemask.x,{};", value);
  359. }
  360. void EmitSetFragDepth(EmitContext& ctx, ScalarF32 value) {
  361. ctx.Add("MOV.F result.depth.z,{};", value);
  362. }
  363. void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst) {
  364. ctx.Add("MOV.S {},invocation.groupid;", inst);
  365. }
  366. void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst) {
  367. ctx.Add("MOV.S {},invocation.localid;", inst);
  368. }
  369. void EmitInvocationId(EmitContext& ctx, IR::Inst& inst) {
  370. ctx.Add("MOV.S {}.x,primitive_invocation.x;", inst);
  371. }
  372. void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst) {
  373. switch (ctx.stage) {
  374. case Stage::TessellationControl:
  375. case Stage::TessellationEval:
  376. ctx.Add("SHL.U {}.x,primitive.vertexcount,16;", inst);
  377. break;
  378. case Stage::Geometry:
  379. ctx.Add("SHL.U {}.x,{},16;", inst,
  380. InputTopologyVertices::vertices(ctx.runtime_info.input_topology));
  381. break;
  382. default:
  383. LOG_WARNING(Shader, "(STUBBED) called");
  384. ctx.Add("MOV.S {}.x,0x00ff0000;", inst);
  385. }
  386. }
  387. void EmitSampleId(EmitContext& ctx, IR::Inst& inst) {
  388. ctx.Add("MOV.S {}.x,fragment.sampleid.x;", inst);
  389. }
  390. void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst) {
  391. ctx.Add("MOV.S {}.x,fragment.helperthread.x;", inst);
  392. }
  393. void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {
  394. ctx.uses_y_direction = true;
  395. ctx.Add("MOV.F {}.x,y_direction[0].w;", inst);
  396. }
  397. void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) {
  398. ctx.Add("MOV.F {}.x,scaling[0].z;", inst);
  399. }
  400. void EmitRenderArea(EmitContext& ctx, IR::Inst& inst) {
  401. ctx.Add("MOV.F {},render_area[0];", inst);
  402. }
  403. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, ScalarU32 word_offset) {
  404. ctx.Add("MOV.U {},lmem[{}].x;", inst, word_offset);
  405. }
  406. void EmitWriteLocal(EmitContext& ctx, ScalarU32 word_offset, ScalarU32 value) {
  407. ctx.Add("MOV.U lmem[{}].x,{};", word_offset, value);
  408. }
  409. } // namespace Shader::Backend::GLASM