emit_glsl_image.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/modifiers.h"
  8. #include "shader_recompiler/frontend/ir/value.h"
  9. namespace Shader::Backend::GLSL {
  10. namespace {
  11. std::string Texture(EmitContext& ctx, const IR::TextureInstInfo& info,
  12. [[maybe_unused]] const IR::Value& index) {
  13. if (info.type == TextureType::Buffer) {
  14. throw NotImplementedException("TextureType::Buffer");
  15. } else {
  16. return fmt::format("tex{}", ctx.texture_bindings.at(info.descriptor_index));
  17. }
  18. }
  19. std::string CastToIntVec(std::string_view value, const IR::TextureInstInfo& info) {
  20. switch (info.type) {
  21. case TextureType::Color1D:
  22. return fmt::format("int({})", value);
  23. case TextureType::ColorArray1D:
  24. case TextureType::Color2D:
  25. case TextureType::ColorArray2D:
  26. return fmt::format("ivec2({})", value);
  27. case TextureType::Color3D:
  28. case TextureType::ColorCube:
  29. return fmt::format("ivec3({})", value);
  30. case TextureType::ColorArrayCube:
  31. return fmt::format("ivec4({})", value);
  32. default:
  33. throw NotImplementedException("Offset type {}", info.type.Value());
  34. }
  35. }
  36. std::string TexelFetchCastToInt(std::string_view value, const IR::TextureInstInfo& info) {
  37. switch (info.type) {
  38. case TextureType::Color1D:
  39. return fmt::format("int({})", value);
  40. case TextureType::ColorArray1D:
  41. case TextureType::Color2D:
  42. return fmt::format("ivec2({})", value);
  43. case TextureType::ColorArray2D:
  44. case TextureType::Color3D:
  45. case TextureType::ColorCube:
  46. return fmt::format("ivec3({})", value);
  47. case TextureType::ColorArrayCube:
  48. return fmt::format("ivec4({})", value);
  49. default:
  50. throw NotImplementedException("Offset type {}", info.type.Value());
  51. }
  52. }
  53. std::string ShadowSamplerVecCast(TextureType type) {
  54. switch (type) {
  55. case TextureType::ColorArray2D:
  56. case TextureType::ColorCube:
  57. case TextureType::ColorArrayCube:
  58. return "vec4";
  59. default:
  60. return "vec3";
  61. }
  62. }
  63. IR::Inst* PrepareSparse(IR::Inst& inst) {
  64. const auto sparse_inst{inst.GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp)};
  65. if (sparse_inst) {
  66. sparse_inst->Invalidate();
  67. }
  68. return sparse_inst;
  69. }
  70. } // namespace
  71. void EmitImageSampleImplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  72. [[maybe_unused]] const IR::Value& index,
  73. [[maybe_unused]] std::string_view coords,
  74. [[maybe_unused]] std::string_view bias_lc,
  75. [[maybe_unused]] const IR::Value& offset) {
  76. const auto info{inst.Flags<IR::TextureInstInfo>()};
  77. if (info.has_lod_clamp) {
  78. throw NotImplementedException("Lod clamp samples");
  79. }
  80. const auto texture{Texture(ctx, info, index)};
  81. const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
  82. const auto texel{ctx.reg_alloc.Define(inst, Type::F32x4)};
  83. const auto sparse_inst{PrepareSparse(inst)};
  84. if (!sparse_inst) {
  85. if (!offset.IsEmpty()) {
  86. ctx.Add("{}=textureOffset({},{},{}{});", texel, texture, coords,
  87. CastToIntVec(ctx.reg_alloc.Consume(offset), info), bias);
  88. } else {
  89. if (ctx.stage == Stage::Fragment) {
  90. ctx.Add("{}=texture({},{}{});", texel, texture, coords, bias);
  91. } else {
  92. ctx.Add("{}=textureLod({},{},0.0);", texel, texture, coords);
  93. }
  94. }
  95. return;
  96. }
  97. // TODO: Query sparseTexels extension support
  98. if (!offset.IsEmpty()) {
  99. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureOffsetARB({},{},{},{}{}));",
  100. *sparse_inst, texture, coords, CastToIntVec(ctx.reg_alloc.Consume(offset), info),
  101. texel, bias);
  102. } else {
  103. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureARB({},{},{}{}));", *sparse_inst,
  104. texture, coords, texel, bias);
  105. }
  106. }
  107. void EmitImageSampleExplicitLod([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  108. [[maybe_unused]] const IR::Value& index,
  109. [[maybe_unused]] std::string_view coords,
  110. [[maybe_unused]] std::string_view lod_lc,
  111. [[maybe_unused]] const IR::Value& offset) {
  112. const auto info{inst.Flags<IR::TextureInstInfo>()};
  113. if (info.has_bias) {
  114. throw NotImplementedException("Bias texture samples");
  115. }
  116. if (info.has_lod_clamp) {
  117. throw NotImplementedException("Lod clamp samples");
  118. }
  119. const auto texture{Texture(ctx, info, index)};
  120. const auto texel{ctx.reg_alloc.Define(inst, Type::F32x4)};
  121. const auto sparse_inst{PrepareSparse(inst)};
  122. if (!sparse_inst) {
  123. if (!offset.IsEmpty()) {
  124. ctx.Add("{}=textureLodOffset({},{},{},{});", texel, texture, coords, lod_lc,
  125. CastToIntVec(ctx.reg_alloc.Consume(offset), info));
  126. } else {
  127. ctx.Add("{}=textureLod({},{},{});", texel, texture, coords, lod_lc);
  128. }
  129. return;
  130. }
  131. // TODO: Query sparseTexels extension support
  132. if (!offset.IsEmpty()) {
  133. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
  134. *sparse_inst, texture, CastToIntVec(coords, info), lod_lc,
  135. CastToIntVec(ctx.reg_alloc.Consume(offset), info), texel);
  136. } else {
  137. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureLodARB({},{},{},{}));", *sparse_inst,
  138. texture, coords, lod_lc, texel);
  139. }
  140. }
  141. void EmitImageSampleDrefImplicitLod([[maybe_unused]] EmitContext& ctx,
  142. [[maybe_unused]] IR::Inst& inst,
  143. [[maybe_unused]] const IR::Value& index,
  144. [[maybe_unused]] std::string_view coords,
  145. [[maybe_unused]] std::string_view dref,
  146. [[maybe_unused]] std::string_view bias_lc,
  147. [[maybe_unused]] const IR::Value& offset) {
  148. const auto info{inst.Flags<IR::TextureInstInfo>()};
  149. const auto sparse_inst{PrepareSparse(inst)};
  150. if (sparse_inst) {
  151. throw NotImplementedException("Sparse texture samples");
  152. }
  153. if (info.has_bias) {
  154. throw NotImplementedException("Bias texture samples");
  155. }
  156. if (info.has_lod_clamp) {
  157. throw NotImplementedException("Lod clamp samples");
  158. }
  159. if (!offset.IsEmpty()) {
  160. throw NotImplementedException("textureLodOffset");
  161. }
  162. const auto texture{Texture(ctx, info, index)};
  163. const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
  164. const auto cast{ShadowSamplerVecCast(info.type)};
  165. if (ctx.stage == Stage::Fragment) {
  166. ctx.AddF32("{}=texture({},{}({},{}){});", inst, texture, cast, coords, dref, bias);
  167. } else {
  168. ctx.AddF32("{}=textureLod({},{}({},{}),0.0);", inst, texture, cast, coords, dref);
  169. }
  170. }
  171. void EmitImageSampleDrefExplicitLod([[maybe_unused]] EmitContext& ctx,
  172. [[maybe_unused]] IR::Inst& inst,
  173. [[maybe_unused]] const IR::Value& index,
  174. [[maybe_unused]] std::string_view coords,
  175. [[maybe_unused]] std::string_view dref,
  176. [[maybe_unused]] std::string_view lod_lc,
  177. [[maybe_unused]] const IR::Value& offset) {
  178. const auto info{inst.Flags<IR::TextureInstInfo>()};
  179. const auto sparse_inst{PrepareSparse(inst)};
  180. if (sparse_inst) {
  181. throw NotImplementedException("Sparse texture samples");
  182. }
  183. if (info.has_bias) {
  184. throw NotImplementedException("Bias texture samples");
  185. }
  186. if (info.has_lod_clamp) {
  187. throw NotImplementedException("Lod clamp samples");
  188. }
  189. if (!offset.IsEmpty()) {
  190. throw NotImplementedException("textureLodOffset");
  191. }
  192. const auto texture{Texture(ctx, info, index)};
  193. if (info.type == TextureType::ColorArrayCube) {
  194. ctx.AddF32("{}=textureLod({},{},{},{});", inst, texture, coords, dref, lod_lc);
  195. } else {
  196. ctx.AddF32("{}=textureLod({},vec3({},{}),{});", inst, texture, coords, dref, lod_lc);
  197. }
  198. }
  199. void EmitImageGather([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  200. [[maybe_unused]] const IR::Value& index,
  201. [[maybe_unused]] std::string_view coords,
  202. [[maybe_unused]] const IR::Value& offset,
  203. [[maybe_unused]] const IR::Value& offset2) {
  204. throw NotImplementedException("GLSL Instruction");
  205. }
  206. void EmitImageGatherDref([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  207. [[maybe_unused]] const IR::Value& index,
  208. [[maybe_unused]] std::string_view coords,
  209. [[maybe_unused]] const IR::Value& offset,
  210. [[maybe_unused]] const IR::Value& offset2,
  211. [[maybe_unused]] std::string_view dref) {
  212. throw NotImplementedException("GLSL Instruction");
  213. }
  214. void EmitImageFetch([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  215. [[maybe_unused]] const IR::Value& index,
  216. [[maybe_unused]] std::string_view coords,
  217. [[maybe_unused]] std::string_view offset, [[maybe_unused]] std::string_view lod,
  218. [[maybe_unused]] std::string_view ms) {
  219. const auto info{inst.Flags<IR::TextureInstInfo>()};
  220. if (info.has_bias) {
  221. throw NotImplementedException("Bias texture samples");
  222. }
  223. if (info.has_lod_clamp) {
  224. throw NotImplementedException("Lod clamp samples");
  225. }
  226. const auto texture{Texture(ctx, info, index)};
  227. const auto sparse_inst{PrepareSparse(inst)};
  228. const auto texel{ctx.reg_alloc.Define(inst, Type::F32x4)};
  229. if (!sparse_inst) {
  230. if (!offset.empty()) {
  231. ctx.Add("{}=texelFetchOffset({},{},int({}),{});", texel, texture,
  232. TexelFetchCastToInt(coords, info), lod, TexelFetchCastToInt(offset, info));
  233. } else {
  234. ctx.Add("{}=texelFetch({},{},int({}));", texel, texture,
  235. TexelFetchCastToInt(coords, info), lod);
  236. }
  237. return;
  238. }
  239. // TODO: Query sparseTexels extension support
  240. if (!offset.empty()) {
  241. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
  242. *sparse_inst, texture, CastToIntVec(coords, info), lod,
  243. CastToIntVec(offset, info), texel);
  244. } else {
  245. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchARB({},{},{},{}));", *sparse_inst,
  246. texture, CastToIntVec(coords, info), lod, texel);
  247. }
  248. }
  249. void EmitImageQueryDimensions([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  250. [[maybe_unused]] const IR::Value& index,
  251. [[maybe_unused]] std::string_view lod) {
  252. throw NotImplementedException("GLSL Instruction");
  253. }
  254. void EmitImageQueryLod([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  255. [[maybe_unused]] const IR::Value& index,
  256. [[maybe_unused]] std::string_view coords) {
  257. throw NotImplementedException("GLSL Instruction");
  258. }
  259. void EmitImageGradient([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  260. [[maybe_unused]] const IR::Value& index,
  261. [[maybe_unused]] std::string_view coords,
  262. [[maybe_unused]] std::string_view derivates,
  263. [[maybe_unused]] std::string_view offset,
  264. [[maybe_unused]] std::string_view lod_clamp) {
  265. throw NotImplementedException("GLSL Instruction");
  266. }
  267. void EmitImageRead([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  268. [[maybe_unused]] const IR::Value& index,
  269. [[maybe_unused]] std::string_view coords) {
  270. throw NotImplementedException("GLSL Instruction");
  271. }
  272. void EmitImageWrite([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
  273. [[maybe_unused]] const IR::Value& index,
  274. [[maybe_unused]] std::string_view coords,
  275. [[maybe_unused]] std::string_view color) {
  276. throw NotImplementedException("GLSL Instruction");
  277. }
  278. void EmitBindlessImageSampleImplicitLod(EmitContext&) {
  279. throw NotImplementedException("GLSL Instruction");
  280. }
  281. void EmitBindlessImageSampleExplicitLod(EmitContext&) {
  282. throw NotImplementedException("GLSL Instruction");
  283. }
  284. void EmitBindlessImageSampleDrefImplicitLod(EmitContext&) {
  285. throw NotImplementedException("GLSL Instruction");
  286. }
  287. void EmitBindlessImageSampleDrefExplicitLod(EmitContext&) {
  288. throw NotImplementedException("GLSL Instruction");
  289. }
  290. void EmitBindlessImageGather(EmitContext&) {
  291. throw NotImplementedException("GLSL Instruction");
  292. }
  293. void EmitBindlessImageGatherDref(EmitContext&) {
  294. throw NotImplementedException("GLSL Instruction");
  295. }
  296. void EmitBindlessImageFetch(EmitContext&) {
  297. throw NotImplementedException("GLSL Instruction");
  298. }
  299. void EmitBindlessImageQueryDimensions(EmitContext&) {
  300. throw NotImplementedException("GLSL Instruction");
  301. }
  302. void EmitBindlessImageQueryLod(EmitContext&) {
  303. throw NotImplementedException("GLSL Instruction");
  304. }
  305. void EmitBindlessImageGradient(EmitContext&) {
  306. throw NotImplementedException("GLSL Instruction");
  307. }
  308. void EmitBindlessImageRead(EmitContext&) {
  309. throw NotImplementedException("GLSL Instruction");
  310. }
  311. void EmitBindlessImageWrite(EmitContext&) {
  312. throw NotImplementedException("GLSL Instruction");
  313. }
  314. void EmitBoundImageSampleImplicitLod(EmitContext&) {
  315. throw NotImplementedException("GLSL Instruction");
  316. }
  317. void EmitBoundImageSampleExplicitLod(EmitContext&) {
  318. throw NotImplementedException("GLSL Instruction");
  319. }
  320. void EmitBoundImageSampleDrefImplicitLod(EmitContext&) {
  321. throw NotImplementedException("GLSL Instruction");
  322. }
  323. void EmitBoundImageSampleDrefExplicitLod(EmitContext&) {
  324. throw NotImplementedException("GLSL Instruction");
  325. }
  326. void EmitBoundImageGather(EmitContext&) {
  327. throw NotImplementedException("GLSL Instruction");
  328. }
  329. void EmitBoundImageGatherDref(EmitContext&) {
  330. throw NotImplementedException("GLSL Instruction");
  331. }
  332. void EmitBoundImageFetch(EmitContext&) {
  333. throw NotImplementedException("GLSL Instruction");
  334. }
  335. void EmitBoundImageQueryDimensions(EmitContext&) {
  336. throw NotImplementedException("GLSL Instruction");
  337. }
  338. void EmitBoundImageQueryLod(EmitContext&) {
  339. throw NotImplementedException("GLSL Instruction");
  340. }
  341. void EmitBoundImageGradient(EmitContext&) {
  342. throw NotImplementedException("GLSL Instruction");
  343. }
  344. void EmitBoundImageRead(EmitContext&) {
  345. throw NotImplementedException("GLSL Instruction");
  346. }
  347. void EmitBoundImageWrite(EmitContext&) {
  348. throw NotImplementedException("GLSL Instruction");
  349. }
  350. } // namespace Shader::Backend::GLSL