emit_glsl_context_get_set.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. namespace Shader::Backend::GLSL {
  9. namespace {
  10. static constexpr std::string_view SWIZZLE{"xyzw"};
  11. u32 CbufIndex(u32 offset) {
  12. return (offset / 4) % 4;
  13. }
  14. char OffsetSwizzle(u32 offset) {
  15. return SWIZZLE[CbufIndex(offset)];
  16. }
  17. bool IsInputArray(Stage stage) {
  18. return stage == Stage::Geometry || stage == Stage::TessellationControl ||
  19. stage == Stage::TessellationEval;
  20. }
  21. std::string InputVertexIndex(EmitContext& ctx, std::string_view vertex) {
  22. return IsInputArray(ctx.stage) ? fmt::format("[{}]", vertex) : "";
  23. }
  24. std::string OutputVertexIndex(EmitContext& ctx, std::string_view vertex) {
  25. switch (ctx.stage) {
  26. case Stage::Geometry:
  27. return fmt::format("[{}]", vertex);
  28. case Stage::TessellationControl:
  29. return "[gl_InvocationID]";
  30. default:
  31. return "";
  32. }
  33. }
  34. } // namespace
  35. void EmitGetCbufU8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  36. [[maybe_unused]] const IR::Value& binding,
  37. [[maybe_unused]] const IR::Value& offset) {
  38. if (offset.IsImmediate()) {
  39. ctx.AddU32("{}=bitfieldExtract(floatBitsToUint({}_cbuf{}[{}].{}),int({}),8);", inst,
  40. ctx.stage_name, binding.U32(), offset.U32() / 16, OffsetSwizzle(offset.U32()),
  41. (offset.U32() % 4) * 8);
  42. } else {
  43. const auto offset_var{ctx.var_alloc.Consume(offset)};
  44. ctx.AddU32(
  45. "{}=bitfieldExtract(floatBitsToUint({}_cbuf{}[{}/16][({}>>2)%4]),int(({}%4)*8),8);",
  46. inst, ctx.stage_name, binding.U32(), offset_var, offset_var, offset_var);
  47. }
  48. }
  49. void EmitGetCbufS8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  50. [[maybe_unused]] const IR::Value& binding,
  51. [[maybe_unused]] const IR::Value& offset) {
  52. if (offset.IsImmediate()) {
  53. ctx.AddU32("{}=bitfieldExtract(floatBitsToInt({}_cbuf{}[{}].{}),int({}),8);", inst,
  54. ctx.stage_name, binding.U32(), offset.U32() / 16, OffsetSwizzle(offset.U32()),
  55. (offset.U32() % 4) * 8);
  56. } else {
  57. const auto offset_var{ctx.var_alloc.Consume(offset)};
  58. ctx.AddU32(
  59. "{}=bitfieldExtract(floatBitsToInt({}_cbuf{}[{}/16][({}>>2)%4]),int(({}%4)*8),8);",
  60. inst, ctx.stage_name, binding.U32(), offset_var, offset_var, offset_var);
  61. }
  62. }
  63. void EmitGetCbufU16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  64. [[maybe_unused]] const IR::Value& binding,
  65. [[maybe_unused]] const IR::Value& offset) {
  66. if (offset.IsImmediate()) {
  67. ctx.AddU32("{}=bitfieldExtract(floatBitsToUint({}_cbuf{}[{}].{}),int({}),16);", inst,
  68. ctx.stage_name, binding.U32(), offset.U32() / 16, OffsetSwizzle(offset.U32()),
  69. ((offset.U32() / 2) % 2) * 16);
  70. } else {
  71. const auto offset_var{ctx.var_alloc.Consume(offset)};
  72. ctx.AddU32("{}=bitfieldExtract(floatBitsToUint({}_cbuf{}[{}/16][({}>>2)%4]),int((({}/"
  73. "2)%2)*16),16);",
  74. inst, ctx.stage_name, binding.U32(), offset_var, offset_var, offset_var);
  75. }
  76. }
  77. void EmitGetCbufS16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  78. [[maybe_unused]] const IR::Value& binding,
  79. [[maybe_unused]] const IR::Value& offset) {
  80. if (offset.IsImmediate()) {
  81. ctx.AddU32("{}=bitfieldExtract(floatBitsToInt({}_cbuf{}[{}].{}),int({}),16);", inst,
  82. ctx.stage_name, binding.U32(), offset.U32() / 16, OffsetSwizzle(offset.U32()),
  83. ((offset.U32() / 2) % 2) * 16);
  84. } else {
  85. const auto offset_var{ctx.var_alloc.Consume(offset)};
  86. ctx.AddU32("{}=bitfieldExtract(floatBitsToInt({}_cbuf{}[{}/16][({}>>2)%4]),int((({}/"
  87. "2)%2)*16),16);",
  88. inst, ctx.stage_name, binding.U32(), offset_var, offset_var, offset_var);
  89. }
  90. }
  91. void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  92. const IR::Value& offset) {
  93. if (offset.IsImmediate()) {
  94. ctx.AddU32("{}=floatBitsToUint({}_cbuf{}[{}].{});", inst, ctx.stage_name, binding.U32(),
  95. offset.U32() / 16, OffsetSwizzle(offset.U32()));
  96. } else {
  97. const auto offset_var{ctx.var_alloc.Consume(offset)};
  98. ctx.AddU32("{}=floatBitsToUint({}_cbuf{}[{}/16][({}>>2)%4]);", inst, ctx.stage_name,
  99. binding.U32(), offset_var, offset_var);
  100. }
  101. }
  102. void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  103. const IR::Value& offset) {
  104. if (offset.IsImmediate()) {
  105. ctx.AddF32("{}={}_cbuf{}[{}].{};", inst, ctx.stage_name, binding.U32(), offset.U32() / 16,
  106. OffsetSwizzle(offset.U32()));
  107. } else {
  108. const auto offset_var{ctx.var_alloc.Consume(offset)};
  109. ctx.AddF32("{}={}_cbuf{}[{}/16][({}>>2)%4];", inst, ctx.stage_name, binding.U32(),
  110. offset_var, offset_var);
  111. }
  112. }
  113. void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
  114. const IR::Value& offset) {
  115. if (offset.IsImmediate()) {
  116. ctx.AddU32x2(
  117. "{}=uvec2(floatBitsToUint({}_cbuf{}[{}].{}),floatBitsToUint({}_cbuf{}[{}].{}));", inst,
  118. ctx.stage_name, binding.U32(), offset.U32() / 16, OffsetSwizzle(offset.U32()),
  119. ctx.stage_name, binding.U32(), (offset.U32() + 4) / 16,
  120. OffsetSwizzle(offset.U32() + 4));
  121. } else {
  122. const auto offset_var{ctx.var_alloc.Consume(offset)};
  123. ctx.AddU32x2("{}=uvec2(floatBitsToUint({}_cbuf{}[{}/16][({}/"
  124. "4)%4]),floatBitsToUint({}_cbuf{}[({}+4)/16][(({}+4)>>2)%4]));",
  125. inst, ctx.stage_name, binding.U32(), offset_var, offset_var, ctx.stage_name,
  126. binding.U32(), offset_var, offset_var);
  127. }
  128. }
  129. void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
  130. std::string_view vertex) {
  131. const u32 element{static_cast<u32>(attr) % 4};
  132. const char swizzle{"xyzw"[element]};
  133. if (IR::IsGeneric(attr)) {
  134. const u32 index{IR::GenericAttributeIndex(attr)};
  135. ctx.AddF32("{}=in_attr{}{}.{};", inst, index, InputVertexIndex(ctx, vertex), swizzle);
  136. return;
  137. }
  138. switch (attr) {
  139. case IR::Attribute::PositionX:
  140. case IR::Attribute::PositionY:
  141. case IR::Attribute::PositionZ:
  142. case IR::Attribute::PositionW:
  143. switch (ctx.stage) {
  144. case Stage::VertexA:
  145. case Stage::VertexB:
  146. ctx.AddF32("{}=gl_Position.{};", inst, swizzle);
  147. break;
  148. case Stage::TessellationEval:
  149. ctx.AddF32("{}=gl_TessCoord.{};", inst, swizzle);
  150. break;
  151. case Stage::TessellationControl:
  152. case Stage::Geometry:
  153. ctx.AddF32("{}=gl_in[{}].gl_Position.{};", inst, vertex, swizzle);
  154. break;
  155. case Stage::Fragment:
  156. ctx.AddF32("{}=gl_FragCoord.{};", inst, swizzle);
  157. break;
  158. default:
  159. throw NotImplementedException("Get Position for stage {}", ctx.stage);
  160. }
  161. break;
  162. case IR::Attribute::PointSpriteS:
  163. case IR::Attribute::PointSpriteT:
  164. ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle);
  165. break;
  166. case IR::Attribute::InstanceId:
  167. ctx.AddF32("{}=intBitsToFloat(gl_InstanceID);", inst);
  168. break;
  169. case IR::Attribute::VertexId:
  170. ctx.AddF32("{}=intBitsToFloat(gl_VertexID);", inst);
  171. break;
  172. case IR::Attribute::FrontFace:
  173. ctx.AddF32("{}=intBitsToFloat(gl_FrontFacing?-1:0);", inst);
  174. break;
  175. case IR::Attribute::TessellationEvaluationPointU:
  176. case IR::Attribute::TessellationEvaluationPointV:
  177. ctx.AddF32("{}=gl_TessCoord.{};", inst, swizzle);
  178. break;
  179. default:
  180. fmt::print("Get attribute {}", attr);
  181. throw NotImplementedException("Get attribute {}", attr);
  182. }
  183. }
  184. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value,
  185. [[maybe_unused]] std::string_view vertex) {
  186. if (IR::IsGeneric(attr)) {
  187. const u32 index{IR::GenericAttributeIndex(attr)};
  188. const u32 element{IR::GenericAttributeElement(attr)};
  189. const GenericElementInfo& info{ctx.output_generics.at(index).at(element)};
  190. const auto output_decorator{OutputVertexIndex(ctx, vertex)};
  191. if (info.num_components == 1) {
  192. ctx.Add("{}{}={};", info.name, output_decorator, value);
  193. } else {
  194. const u32 index_element{element - info.first_element};
  195. ctx.Add("{}{}.{}={};", info.name, output_decorator, "xyzw"[index_element], value);
  196. }
  197. return;
  198. }
  199. const u32 element{static_cast<u32>(attr) % 4};
  200. const char swizzle{"xyzw"[element]};
  201. switch (attr) {
  202. case IR::Attribute::PointSize:
  203. ctx.Add("gl_PointSize={};", 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("gl_Position.{}={};", swizzle, value);
  210. break;
  211. case IR::Attribute::ViewportIndex:
  212. ctx.Add("gl_ViewportIndex=floatBitsToInt({});", value);
  213. break;
  214. case IR::Attribute::ClipDistance0:
  215. case IR::Attribute::ClipDistance1:
  216. case IR::Attribute::ClipDistance2:
  217. case IR::Attribute::ClipDistance3:
  218. case IR::Attribute::ClipDistance4:
  219. case IR::Attribute::ClipDistance5:
  220. case IR::Attribute::ClipDistance6:
  221. case IR::Attribute::ClipDistance7: {
  222. const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
  223. ctx.Add("gl_ClipDistance[{}]={};", index, value);
  224. break;
  225. }
  226. default:
  227. throw NotImplementedException("Set attribute {}", attr);
  228. }
  229. }
  230. void EmitGetPatch([[maybe_unused]] EmitContext& ctx, IR::Inst& inst,
  231. [[maybe_unused]] IR::Patch patch) {
  232. if (!IR::IsGeneric(patch)) {
  233. throw NotImplementedException("Non-generic patch load");
  234. }
  235. const u32 index{IR::GenericPatchIndex(patch)};
  236. const u32 element{IR::GenericPatchElement(patch)};
  237. const char swizzle{"xyzw"[element]};
  238. ctx.AddF32("{}=patch{}.{};", inst, index, swizzle);
  239. }
  240. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, std::string_view value) {
  241. if (IR::IsGeneric(patch)) {
  242. const u32 index{IR::GenericPatchIndex(patch)};
  243. const u32 element{IR::GenericPatchElement(patch)};
  244. ctx.Add("patch{}.{}={};", index, "xyzw"[element], value);
  245. return;
  246. }
  247. switch (patch) {
  248. case IR::Patch::TessellationLodLeft:
  249. case IR::Patch::TessellationLodRight:
  250. case IR::Patch::TessellationLodTop:
  251. case IR::Patch::TessellationLodBottom: {
  252. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  253. ctx.Add("gl_TessLevelOuter[{}]={};", index, value);
  254. break;
  255. }
  256. case IR::Patch::TessellationLodInteriorU:
  257. ctx.Add("gl_TessLevelInner[0]={};", value);
  258. break;
  259. case IR::Patch::TessellationLodInteriorV:
  260. ctx.Add("gl_TessLevelInner[1]={};", value);
  261. break;
  262. default:
  263. throw NotImplementedException("Patch {}", patch);
  264. }
  265. }
  266. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, std::string_view value) {
  267. const char swizzle{"xyzw"[component]};
  268. ctx.Add("frag_color{}.{}={};", index, swizzle, value);
  269. }
  270. void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst) {
  271. ctx.AddU32x3("{}=gl_LocalInvocationID;", inst);
  272. }
  273. void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) {
  274. ctx.AddU32("{}=lmem[{}];", inst, word_offset);
  275. }
  276. void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value) {
  277. ctx.Add("lmem[{}]={};", word_offset, value);
  278. }
  279. } // namespace Shader::Backend::GLSL