emit_glsl_context_get_set.cpp 18 KB

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