emit_glsl_context_get_set.cpp 16 KB

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