emit_glsl_image.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. #include "shader_recompiler/profile.h"
  10. namespace Shader::Backend::GLSL {
  11. namespace {
  12. std::string Texture(EmitContext& ctx, const IR::TextureInstInfo& info, const IR::Value& index) {
  13. const auto def{info.type == TextureType::Buffer ? ctx.texture_buffers.at(info.descriptor_index)
  14. : ctx.textures.at(info.descriptor_index)};
  15. const auto index_offset{def.count > 1 ? fmt::format("[{}]", ctx.var_alloc.Consume(index)) : ""};
  16. return fmt::format("tex{}{}", def.binding, index_offset);
  17. }
  18. std::string Image(EmitContext& ctx, const IR::TextureInstInfo& info, const IR::Value& index) {
  19. const auto def{info.type == TextureType::Buffer ? ctx.image_buffers.at(info.descriptor_index)
  20. : ctx.images.at(info.descriptor_index)};
  21. const auto index_offset{def.count > 1 ? fmt::format("[{}]", ctx.var_alloc.Consume(index)) : ""};
  22. return fmt::format("img{}{}", def.binding, index_offset);
  23. }
  24. std::string CastToIntVec(std::string_view value, const IR::TextureInstInfo& info) {
  25. switch (info.type) {
  26. case TextureType::Color1D:
  27. case TextureType::Buffer:
  28. return fmt::format("int({})", value);
  29. case TextureType::ColorArray1D:
  30. case TextureType::Color2D:
  31. case TextureType::ColorArray2D:
  32. return fmt::format("ivec2({})", value);
  33. case TextureType::Color3D:
  34. case TextureType::ColorCube:
  35. return fmt::format("ivec3({})", value);
  36. case TextureType::ColorArrayCube:
  37. return fmt::format("ivec4({})", value);
  38. default:
  39. throw NotImplementedException("Integer cast for TextureType {}", info.type.Value());
  40. }
  41. }
  42. std::string CoordsCastToInt(std::string_view value, const IR::TextureInstInfo& info) {
  43. switch (info.type) {
  44. case TextureType::Color1D:
  45. case TextureType::Buffer:
  46. return fmt::format("int({})", value);
  47. case TextureType::ColorArray1D:
  48. case TextureType::Color2D:
  49. return fmt::format("ivec2({})", value);
  50. case TextureType::ColorArray2D:
  51. case TextureType::Color3D:
  52. case TextureType::ColorCube:
  53. return fmt::format("ivec3({})", value);
  54. case TextureType::ColorArrayCube:
  55. return fmt::format("ivec4({})", value);
  56. default:
  57. throw NotImplementedException("TexelFetchCast type {}", info.type.Value());
  58. }
  59. }
  60. bool NeedsShadowLodExt(TextureType type) {
  61. switch (type) {
  62. case TextureType::ColorArray2D:
  63. case TextureType::ColorCube:
  64. case TextureType::ColorArrayCube:
  65. return true;
  66. default:
  67. return false;
  68. }
  69. }
  70. std::string GetOffsetVec(EmitContext& ctx, const IR::Value& offset) {
  71. if (offset.IsImmediate()) {
  72. return fmt::format("int({})", offset.U32());
  73. }
  74. IR::Inst* const inst{offset.InstRecursive()};
  75. if (inst->AreAllArgsImmediates()) {
  76. switch (inst->GetOpcode()) {
  77. case IR::Opcode::CompositeConstructU32x2:
  78. return fmt::format("ivec2({},{})", inst->Arg(0).U32(), inst->Arg(1).U32());
  79. case IR::Opcode::CompositeConstructU32x3:
  80. return fmt::format("ivec3({},{},{})", inst->Arg(0).U32(), inst->Arg(1).U32(),
  81. inst->Arg(2).U32());
  82. case IR::Opcode::CompositeConstructU32x4:
  83. return fmt::format("ivec4({},{},{},{})", inst->Arg(0).U32(), inst->Arg(1).U32(),
  84. inst->Arg(2).U32(), inst->Arg(3).U32());
  85. default:
  86. break;
  87. }
  88. }
  89. const bool has_var_aoffi{ctx.profile.support_gl_variable_aoffi};
  90. if (!has_var_aoffi) {
  91. LOG_WARNING(Shader_GLSL, "Device does not support variable texture offsets, STUBBING");
  92. }
  93. const auto offset_str{has_var_aoffi ? ctx.var_alloc.Consume(offset) : "0"};
  94. switch (offset.Type()) {
  95. case IR::Type::U32:
  96. return fmt::format("int({})", offset_str);
  97. case IR::Type::U32x2:
  98. return fmt::format("ivec2({})", offset_str);
  99. case IR::Type::U32x3:
  100. return fmt::format("ivec3({})", offset_str);
  101. case IR::Type::U32x4:
  102. return fmt::format("ivec4({})", offset_str);
  103. default:
  104. throw NotImplementedException("Offset type {}", offset.Type());
  105. }
  106. }
  107. std::string PtpOffsets(const IR::Value& offset, const IR::Value& offset2) {
  108. const std::array values{offset.InstRecursive(), offset2.InstRecursive()};
  109. if (!values[0]->AreAllArgsImmediates() || !values[1]->AreAllArgsImmediates()) {
  110. LOG_WARNING(Shader_GLSL, "Not all arguments in PTP are immediate, STUBBING");
  111. return "ivec2[](ivec2(0), ivec2(1), ivec2(2), ivec2(3))";
  112. }
  113. const IR::Opcode opcode{values[0]->GetOpcode()};
  114. if (opcode != values[1]->GetOpcode() || opcode != IR::Opcode::CompositeConstructU32x4) {
  115. throw LogicError("Invalid PTP arguments");
  116. }
  117. auto read{[&](unsigned int a, unsigned int b) { return values[a]->Arg(b).U32(); }};
  118. return fmt::format("ivec2[](ivec2({},{}),ivec2({},{}),ivec2({},{}),ivec2({},{}))", read(0, 0),
  119. read(0, 1), read(0, 2), read(0, 3), read(1, 0), read(1, 1), read(1, 2),
  120. read(1, 3));
  121. }
  122. IR::Inst* PrepareSparse(IR::Inst& inst) {
  123. const auto sparse_inst{inst.GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp)};
  124. if (sparse_inst) {
  125. sparse_inst->Invalidate();
  126. }
  127. return sparse_inst;
  128. }
  129. } // Anonymous namespace
  130. void EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  131. std::string_view coords, std::string_view bias_lc,
  132. const IR::Value& offset) {
  133. const auto info{inst.Flags<IR::TextureInstInfo>()};
  134. if (info.has_lod_clamp) {
  135. throw NotImplementedException("EmitImageSampleImplicitLod Lod clamp samples");
  136. }
  137. const auto texture{Texture(ctx, info, index)};
  138. const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
  139. const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  140. const auto sparse_inst{PrepareSparse(inst)};
  141. const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
  142. if (sparse_inst && !supports_sparse) {
  143. LOG_WARNING(Shader_GLSL, "Device does not support sparse texture queries. STUBBING");
  144. ctx.AddU1("{}=true;", *sparse_inst);
  145. }
  146. if (!sparse_inst || !supports_sparse) {
  147. if (!offset.IsEmpty()) {
  148. const auto offset_str{GetOffsetVec(ctx, offset)};
  149. if (ctx.stage == Stage::Fragment) {
  150. ctx.Add("{}=textureOffset({},{},{}{});", texel, texture, coords, offset_str, bias);
  151. } else {
  152. ctx.Add("{}=textureLodOffset({},{},0.0,{});", texel, texture, coords, offset_str);
  153. }
  154. } else {
  155. if (ctx.stage == Stage::Fragment) {
  156. ctx.Add("{}=texture({},{}{});", texel, texture, coords, bias);
  157. } else {
  158. ctx.Add("{}=textureLod({},{},0.0);", texel, texture, coords);
  159. }
  160. }
  161. return;
  162. }
  163. if (!offset.IsEmpty()) {
  164. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureOffsetARB({},{},{},{}{}));",
  165. *sparse_inst, texture, coords, GetOffsetVec(ctx, offset), texel, bias);
  166. } else {
  167. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureARB({},{},{}{}));", *sparse_inst,
  168. texture, coords, texel, bias);
  169. }
  170. }
  171. void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  172. std::string_view coords, std::string_view lod_lc,
  173. const IR::Value& offset) {
  174. const auto info{inst.Flags<IR::TextureInstInfo>()};
  175. if (info.has_bias) {
  176. throw NotImplementedException("EmitImageSampleExplicitLod Bias texture samples");
  177. }
  178. if (info.has_lod_clamp) {
  179. throw NotImplementedException("EmitImageSampleExplicitLod Lod clamp samples");
  180. }
  181. const auto texture{Texture(ctx, info, index)};
  182. const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  183. const auto sparse_inst{PrepareSparse(inst)};
  184. const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
  185. if (sparse_inst && !supports_sparse) {
  186. LOG_WARNING(Shader_GLSL, "Device does not support sparse texture queries. STUBBING");
  187. ctx.AddU1("{}=true;", *sparse_inst);
  188. }
  189. if (!sparse_inst || !supports_sparse) {
  190. if (!offset.IsEmpty()) {
  191. ctx.Add("{}=textureLodOffset({},{},{},{});", texel, texture, coords, lod_lc,
  192. GetOffsetVec(ctx, offset));
  193. } else {
  194. ctx.Add("{}=textureLod({},{},{});", texel, texture, coords, lod_lc);
  195. }
  196. return;
  197. }
  198. if (!offset.IsEmpty()) {
  199. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
  200. *sparse_inst, texture, CastToIntVec(coords, info), lod_lc,
  201. GetOffsetVec(ctx, offset), texel);
  202. } else {
  203. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureLodARB({},{},{},{}));", *sparse_inst,
  204. texture, coords, lod_lc, texel);
  205. }
  206. }
  207. void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  208. std::string_view coords, std::string_view dref,
  209. std::string_view bias_lc, const IR::Value& offset) {
  210. const auto info{inst.Flags<IR::TextureInstInfo>()};
  211. const auto sparse_inst{PrepareSparse(inst)};
  212. if (sparse_inst) {
  213. throw NotImplementedException("EmitImageSampleDrefImplicitLod Sparse texture samples");
  214. }
  215. if (info.has_bias) {
  216. throw NotImplementedException("EmitImageSampleDrefImplicitLod Bias texture samples");
  217. }
  218. if (info.has_lod_clamp) {
  219. throw NotImplementedException("EmitImageSampleDrefImplicitLod Lod clamp samples");
  220. }
  221. const auto texture{Texture(ctx, info, index)};
  222. const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
  223. const bool needs_shadow_ext{NeedsShadowLodExt(info.type)};
  224. const auto cast{needs_shadow_ext ? "vec4" : "vec3"};
  225. const bool use_grad{!ctx.profile.support_gl_texture_shadow_lod &&
  226. ctx.stage != Stage::Fragment && needs_shadow_ext};
  227. if (use_grad) {
  228. LOG_WARNING(Shader_GLSL,
  229. "Device lacks GL_EXT_texture_shadow_lod. Using textureGrad fallback");
  230. if (info.type == TextureType::ColorArrayCube) {
  231. LOG_WARNING(Shader_GLSL, "textureGrad does not support ColorArrayCube. Stubbing");
  232. ctx.AddF32("{}=0.0f;", inst);
  233. return;
  234. }
  235. const auto d_cast{info.type == TextureType::ColorArray2D ? "vec2" : "vec3"};
  236. ctx.AddF32("{}=textureGrad({},{}({},{}),{}(0),{}(0));", inst, texture, cast, coords, dref,
  237. d_cast, d_cast);
  238. return;
  239. }
  240. if (!offset.IsEmpty()) {
  241. const auto offset_str{GetOffsetVec(ctx, offset)};
  242. if (ctx.stage == Stage::Fragment) {
  243. ctx.AddF32("{}=textureOffset({},{}({},{}),{}{});", inst, texture, cast, coords, dref,
  244. offset_str, bias);
  245. } else {
  246. ctx.AddF32("{}=textureLodOffset({},{}({},{}),0.0,{});", inst, texture, cast, coords,
  247. dref, offset_str);
  248. }
  249. } else {
  250. if (ctx.stage == Stage::Fragment) {
  251. if (info.type == TextureType::ColorArrayCube) {
  252. ctx.AddF32("{}=texture({},vec4({}),{});", inst, texture, coords, dref);
  253. } else {
  254. ctx.AddF32("{}=texture({},{}({},{}){});", inst, texture, cast, coords, dref, bias);
  255. }
  256. } else {
  257. ctx.AddF32("{}=textureLod({},{}({},{}),0.0);", inst, texture, cast, coords, dref);
  258. }
  259. }
  260. }
  261. void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  262. std::string_view coords, std::string_view dref,
  263. std::string_view lod_lc, const IR::Value& offset) {
  264. const auto info{inst.Flags<IR::TextureInstInfo>()};
  265. const auto sparse_inst{PrepareSparse(inst)};
  266. if (sparse_inst) {
  267. throw NotImplementedException("EmitImageSampleDrefExplicitLod Sparse texture samples");
  268. }
  269. if (info.has_bias) {
  270. throw NotImplementedException("EmitImageSampleDrefExplicitLod Bias texture samples");
  271. }
  272. if (info.has_lod_clamp) {
  273. throw NotImplementedException("EmitImageSampleDrefExplicitLod Lod clamp samples");
  274. }
  275. const auto texture{Texture(ctx, info, index)};
  276. const bool needs_shadow_ext{NeedsShadowLodExt(info.type)};
  277. const bool use_grad{!ctx.profile.support_gl_texture_shadow_lod && needs_shadow_ext};
  278. const auto cast{needs_shadow_ext ? "vec4" : "vec3"};
  279. if (use_grad) {
  280. LOG_WARNING(Shader_GLSL,
  281. "Device lacks GL_EXT_texture_shadow_lod. Using textureGrad fallback");
  282. if (info.type == TextureType::ColorArrayCube) {
  283. LOG_WARNING(Shader_GLSL, "textureGrad does not support ColorArrayCube. Stubbing");
  284. ctx.AddF32("{}=0.0f;", inst);
  285. return;
  286. }
  287. const auto d_cast{info.type == TextureType::ColorArray2D ? "vec2" : "vec3"};
  288. ctx.AddF32("{}=textureGrad({},{}({},{}),{}(0),{}(0));", inst, texture, cast, coords, dref,
  289. d_cast, d_cast);
  290. return;
  291. }
  292. if (!offset.IsEmpty()) {
  293. const auto offset_str{GetOffsetVec(ctx, offset)};
  294. if (info.type == TextureType::ColorArrayCube) {
  295. ctx.AddF32("{}=textureLodOffset({},{},{},{},{});", inst, texture, coords, dref, lod_lc,
  296. offset_str);
  297. } else {
  298. ctx.AddF32("{}=textureLodOffset({},{}({},{}),{},{});", inst, texture, cast, coords,
  299. dref, lod_lc, offset_str);
  300. }
  301. } else {
  302. if (info.type == TextureType::ColorArrayCube) {
  303. ctx.AddF32("{}=textureLod({},{},{},{});", inst, texture, coords, dref, lod_lc);
  304. } else {
  305. ctx.AddF32("{}=textureLod({},{}({},{}),{});", inst, texture, cast, coords, dref,
  306. lod_lc);
  307. }
  308. }
  309. }
  310. void EmitImageGather(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  311. std::string_view coords, const IR::Value& offset, const IR::Value& offset2) {
  312. const auto info{inst.Flags<IR::TextureInstInfo>()};
  313. const auto texture{Texture(ctx, info, index)};
  314. const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  315. const auto sparse_inst{PrepareSparse(inst)};
  316. const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
  317. if (sparse_inst && !supports_sparse) {
  318. LOG_WARNING(Shader_GLSL, "Device does not support sparse texture queries. STUBBING");
  319. ctx.AddU1("{}=true;", *sparse_inst);
  320. }
  321. if (!sparse_inst || !supports_sparse) {
  322. if (offset.IsEmpty()) {
  323. ctx.Add("{}=textureGather({},{},int({}));", texel, texture, coords,
  324. info.gather_component);
  325. return;
  326. }
  327. if (offset2.IsEmpty()) {
  328. ctx.Add("{}=textureGatherOffset({},{},{},int({}));", texel, texture, coords,
  329. GetOffsetVec(ctx, offset), info.gather_component);
  330. return;
  331. }
  332. // PTP
  333. const auto offsets{PtpOffsets(offset, offset2)};
  334. ctx.Add("{}=textureGatherOffsets({},{},{},int({}));", texel, texture, coords, offsets,
  335. info.gather_component);
  336. return;
  337. }
  338. if (offset.IsEmpty()) {
  339. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherARB({},{},{},int({})));",
  340. *sparse_inst, texture, coords, texel, info.gather_component);
  341. return;
  342. }
  343. if (offset2.IsEmpty()) {
  344. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},{},int({})));",
  345. *sparse_inst, texture, CastToIntVec(coords, info), GetOffsetVec(ctx, offset),
  346. texel, info.gather_component);
  347. return;
  348. }
  349. // PTP
  350. const auto offsets{PtpOffsets(offset, offset2)};
  351. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},{},int({})));",
  352. *sparse_inst, texture, CastToIntVec(coords, info), offsets, texel,
  353. info.gather_component);
  354. }
  355. void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  356. std::string_view coords, const IR::Value& offset, const IR::Value& offset2,
  357. std::string_view dref) {
  358. const auto info{inst.Flags<IR::TextureInstInfo>()};
  359. const auto texture{Texture(ctx, info, index)};
  360. const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  361. const auto sparse_inst{PrepareSparse(inst)};
  362. const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
  363. if (sparse_inst && !supports_sparse) {
  364. LOG_WARNING(Shader_GLSL, "Device does not support sparse texture queries. STUBBING");
  365. ctx.AddU1("{}=true;", *sparse_inst);
  366. }
  367. if (!sparse_inst || !supports_sparse) {
  368. if (offset.IsEmpty()) {
  369. ctx.Add("{}=textureGather({},{},{});", texel, texture, coords, dref);
  370. return;
  371. }
  372. if (offset2.IsEmpty()) {
  373. ctx.Add("{}=textureGatherOffset({},{},{},{});", texel, texture, coords, dref,
  374. GetOffsetVec(ctx, offset));
  375. return;
  376. }
  377. // PTP
  378. const auto offsets{PtpOffsets(offset, offset2)};
  379. ctx.Add("{}=textureGatherOffsets({},{},{},{});", texel, texture, coords, dref, offsets);
  380. return;
  381. }
  382. if (offset.IsEmpty()) {
  383. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherARB({},{},{},{}));", *sparse_inst,
  384. texture, coords, dref, texel);
  385. return;
  386. }
  387. if (offset2.IsEmpty()) {
  388. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},,{},{}));",
  389. *sparse_inst, texture, CastToIntVec(coords, info), dref,
  390. GetOffsetVec(ctx, offset), texel);
  391. return;
  392. }
  393. // PTP
  394. const auto offsets{PtpOffsets(offset, offset2)};
  395. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},,{},{}));",
  396. *sparse_inst, texture, CastToIntVec(coords, info), dref, offsets, texel);
  397. }
  398. void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  399. std::string_view coords, std::string_view offset, std::string_view lod,
  400. [[maybe_unused]] std::string_view ms) {
  401. const auto info{inst.Flags<IR::TextureInstInfo>()};
  402. if (info.has_bias) {
  403. throw NotImplementedException("EmitImageFetch Bias texture samples");
  404. }
  405. if (info.has_lod_clamp) {
  406. throw NotImplementedException("EmitImageFetch Lod clamp samples");
  407. }
  408. const auto texture{Texture(ctx, info, index)};
  409. const auto sparse_inst{PrepareSparse(inst)};
  410. const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  411. const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
  412. if (sparse_inst && !supports_sparse) {
  413. LOG_WARNING(Shader_GLSL, "Device does not support sparse texture queries. STUBBING");
  414. ctx.AddU1("{}=true;", *sparse_inst);
  415. }
  416. if (!sparse_inst || !supports_sparse) {
  417. if (!offset.empty()) {
  418. ctx.Add("{}=texelFetchOffset({},{},int({}),{});", texel, texture,
  419. CoordsCastToInt(coords, info), lod, CoordsCastToInt(offset, info));
  420. } else {
  421. if (info.type == TextureType::Buffer) {
  422. ctx.Add("{}=texelFetch({},int({}));", texel, texture, coords);
  423. } else {
  424. ctx.Add("{}=texelFetch({},{},int({}));", texel, texture,
  425. CoordsCastToInt(coords, info), lod);
  426. }
  427. }
  428. return;
  429. }
  430. if (!offset.empty()) {
  431. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
  432. *sparse_inst, texture, CastToIntVec(coords, info), lod,
  433. CastToIntVec(offset, info), texel);
  434. } else {
  435. ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchARB({},{},int({}),{}));",
  436. *sparse_inst, texture, CastToIntVec(coords, info), lod, texel);
  437. }
  438. }
  439. void EmitImageQueryDimensions(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  440. std::string_view lod) {
  441. const auto info{inst.Flags<IR::TextureInstInfo>()};
  442. const auto texture{Texture(ctx, info, index)};
  443. switch (info.type) {
  444. case TextureType::Color1D:
  445. return ctx.AddU32x4(
  446. "{}=uvec4(uint(textureSize({},int({}))),0u,0u,uint(textureQueryLevels({})));", inst,
  447. texture, lod, texture);
  448. case TextureType::ColorArray1D:
  449. case TextureType::Color2D:
  450. case TextureType::ColorCube:
  451. return ctx.AddU32x4(
  452. "{}=uvec4(uvec2(textureSize({},int({}))),0u,uint(textureQueryLevels({})));", inst,
  453. texture, lod, texture);
  454. case TextureType::ColorArray2D:
  455. case TextureType::Color3D:
  456. case TextureType::ColorArrayCube:
  457. return ctx.AddU32x4(
  458. "{}=uvec4(uvec3(textureSize({},int({}))),uint(textureQueryLevels({})));", inst, texture,
  459. lod, texture);
  460. case TextureType::Buffer:
  461. throw NotImplementedException("EmitImageQueryDimensions Texture buffers");
  462. }
  463. throw LogicError("Unspecified image type {}", info.type.Value());
  464. }
  465. void EmitImageQueryLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  466. std::string_view coords) {
  467. const auto info{inst.Flags<IR::TextureInstInfo>()};
  468. const auto texture{Texture(ctx, info, index)};
  469. return ctx.AddF32x4("{}=vec4(textureQueryLod({},{}),0.0,0.0);", inst, texture, coords);
  470. }
  471. void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  472. std::string_view coords, const IR::Value& derivatives,
  473. const IR::Value& offset, [[maybe_unused]] const IR::Value& lod_clamp) {
  474. const auto info{inst.Flags<IR::TextureInstInfo>()};
  475. if (info.has_lod_clamp) {
  476. throw NotImplementedException("EmitImageGradient Lod clamp samples");
  477. }
  478. const auto sparse_inst{PrepareSparse(inst)};
  479. if (sparse_inst) {
  480. throw NotImplementedException("EmitImageGradient Sparse");
  481. }
  482. if (!offset.IsEmpty()) {
  483. throw NotImplementedException("EmitImageGradient offset");
  484. }
  485. const auto texture{Texture(ctx, info, index)};
  486. const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
  487. const bool multi_component{info.num_derivates > 1 || info.has_lod_clamp};
  488. const auto derivatives_vec{ctx.var_alloc.Consume(derivatives)};
  489. if (multi_component) {
  490. ctx.Add("{}=textureGrad({},{},vec2({}.xz),vec2({}.yz));", texel, texture, coords,
  491. derivatives_vec, derivatives_vec);
  492. } else {
  493. ctx.Add("{}=textureGrad({},{},float({}.x),float({}.y));", texel, texture, coords,
  494. derivatives_vec, derivatives_vec);
  495. }
  496. }
  497. void EmitImageRead(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  498. std::string_view coords) {
  499. const auto info{inst.Flags<IR::TextureInstInfo>()};
  500. const auto sparse_inst{PrepareSparse(inst)};
  501. if (sparse_inst) {
  502. throw NotImplementedException("EmitImageRead Sparse");
  503. }
  504. const auto image{Image(ctx, info, index)};
  505. ctx.AddU32x4("{}=uvec4(imageLoad({},{}));", inst, image, CoordsCastToInt(coords, info));
  506. }
  507. void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  508. std::string_view coords, std::string_view color) {
  509. const auto info{inst.Flags<IR::TextureInstInfo>()};
  510. const auto image{Image(ctx, info, index)};
  511. ctx.Add("imageStore({},{},{});", image, CoordsCastToInt(coords, info), color);
  512. }
  513. void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  514. std::string_view coords, std::string_view value) {
  515. const auto info{inst.Flags<IR::TextureInstInfo>()};
  516. const auto image{Image(ctx, info, index)};
  517. ctx.AddU32("{}=imageAtomicAdd({},{},{});", inst, image, CoordsCastToInt(coords, info), value);
  518. }
  519. void EmitImageAtomicSMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  520. std::string_view coords, std::string_view value) {
  521. const auto info{inst.Flags<IR::TextureInstInfo>()};
  522. const auto image{Image(ctx, info, index)};
  523. ctx.AddU32("{}=imageAtomicMin({},{},int({}));", inst, image, CoordsCastToInt(coords, info),
  524. value);
  525. }
  526. void EmitImageAtomicUMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  527. std::string_view coords, std::string_view value) {
  528. const auto info{inst.Flags<IR::TextureInstInfo>()};
  529. const auto image{Image(ctx, info, index)};
  530. ctx.AddU32("{}=imageAtomicMin({},{},uint({}));", inst, image, CoordsCastToInt(coords, info),
  531. value);
  532. }
  533. void EmitImageAtomicSMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  534. std::string_view coords, std::string_view value) {
  535. const auto info{inst.Flags<IR::TextureInstInfo>()};
  536. const auto image{Image(ctx, info, index)};
  537. ctx.AddU32("{}=imageAtomicMax({},{},int({}));", inst, image, CoordsCastToInt(coords, info),
  538. value);
  539. }
  540. void EmitImageAtomicUMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  541. std::string_view coords, std::string_view value) {
  542. const auto info{inst.Flags<IR::TextureInstInfo>()};
  543. const auto image{Image(ctx, info, index)};
  544. ctx.AddU32("{}=imageAtomicMax({},{},uint({}));", inst, image, CoordsCastToInt(coords, info),
  545. value);
  546. }
  547. void EmitImageAtomicInc32(EmitContext&, IR::Inst&, const IR::Value&, std::string_view,
  548. std::string_view) {
  549. NotImplemented();
  550. }
  551. void EmitImageAtomicDec32(EmitContext&, IR::Inst&, const IR::Value&, std::string_view,
  552. std::string_view) {
  553. NotImplemented();
  554. }
  555. void EmitImageAtomicAnd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  556. std::string_view coords, std::string_view value) {
  557. const auto info{inst.Flags<IR::TextureInstInfo>()};
  558. const auto image{Image(ctx, info, index)};
  559. ctx.AddU32("{}=imageAtomicAnd({},{},{});", inst, image, CoordsCastToInt(coords, info), value);
  560. }
  561. void EmitImageAtomicOr32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  562. std::string_view coords, std::string_view value) {
  563. const auto info{inst.Flags<IR::TextureInstInfo>()};
  564. const auto image{Image(ctx, info, index)};
  565. ctx.AddU32("{}=imageAtomicOr({},{},{});", inst, image, CoordsCastToInt(coords, info), value);
  566. }
  567. void EmitImageAtomicXor32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  568. std::string_view coords, std::string_view value) {
  569. const auto info{inst.Flags<IR::TextureInstInfo>()};
  570. const auto image{Image(ctx, info, index)};
  571. ctx.AddU32("{}=imageAtomicXor({},{},{});", inst, image, CoordsCastToInt(coords, info), value);
  572. }
  573. void EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
  574. std::string_view coords, std::string_view value) {
  575. const auto info{inst.Flags<IR::TextureInstInfo>()};
  576. const auto image{Image(ctx, info, index)};
  577. ctx.AddU32("{}=imageAtomicExchange({},{},{});", inst, image, CoordsCastToInt(coords, info),
  578. value);
  579. }
  580. void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) {
  581. if (!index.IsImmediate()) {
  582. throw NotImplementedException("Non-constant texture rescaling");
  583. }
  584. UNIMPLEMENTED();
  585. ctx.AddU1("{}=true;", inst);
  586. }
  587. void EmitBindlessImageSampleImplicitLod(EmitContext&) {
  588. NotImplemented();
  589. }
  590. void EmitBindlessImageSampleExplicitLod(EmitContext&) {
  591. NotImplemented();
  592. }
  593. void EmitBindlessImageSampleDrefImplicitLod(EmitContext&) {
  594. NotImplemented();
  595. }
  596. void EmitBindlessImageSampleDrefExplicitLod(EmitContext&) {
  597. NotImplemented();
  598. }
  599. void EmitBindlessImageGather(EmitContext&) {
  600. NotImplemented();
  601. }
  602. void EmitBindlessImageGatherDref(EmitContext&) {
  603. NotImplemented();
  604. }
  605. void EmitBindlessImageFetch(EmitContext&) {
  606. NotImplemented();
  607. }
  608. void EmitBindlessImageQueryDimensions(EmitContext&) {
  609. NotImplemented();
  610. }
  611. void EmitBindlessImageQueryLod(EmitContext&) {
  612. NotImplemented();
  613. }
  614. void EmitBindlessImageGradient(EmitContext&) {
  615. NotImplemented();
  616. }
  617. void EmitBindlessImageRead(EmitContext&) {
  618. NotImplemented();
  619. }
  620. void EmitBindlessImageWrite(EmitContext&) {
  621. NotImplemented();
  622. }
  623. void EmitBoundImageSampleImplicitLod(EmitContext&) {
  624. NotImplemented();
  625. }
  626. void EmitBoundImageSampleExplicitLod(EmitContext&) {
  627. NotImplemented();
  628. }
  629. void EmitBoundImageSampleDrefImplicitLod(EmitContext&) {
  630. NotImplemented();
  631. }
  632. void EmitBoundImageSampleDrefExplicitLod(EmitContext&) {
  633. NotImplemented();
  634. }
  635. void EmitBoundImageGather(EmitContext&) {
  636. NotImplemented();
  637. }
  638. void EmitBoundImageGatherDref(EmitContext&) {
  639. NotImplemented();
  640. }
  641. void EmitBoundImageFetch(EmitContext&) {
  642. NotImplemented();
  643. }
  644. void EmitBoundImageQueryDimensions(EmitContext&) {
  645. NotImplemented();
  646. }
  647. void EmitBoundImageQueryLod(EmitContext&) {
  648. NotImplemented();
  649. }
  650. void EmitBoundImageGradient(EmitContext&) {
  651. NotImplemented();
  652. }
  653. void EmitBoundImageRead(EmitContext&) {
  654. NotImplemented();
  655. }
  656. void EmitBoundImageWrite(EmitContext&) {
  657. NotImplemented();
  658. }
  659. void EmitBindlessImageAtomicIAdd32(EmitContext&) {
  660. NotImplemented();
  661. }
  662. void EmitBindlessImageAtomicSMin32(EmitContext&) {
  663. NotImplemented();
  664. }
  665. void EmitBindlessImageAtomicUMin32(EmitContext&) {
  666. NotImplemented();
  667. }
  668. void EmitBindlessImageAtomicSMax32(EmitContext&) {
  669. NotImplemented();
  670. }
  671. void EmitBindlessImageAtomicUMax32(EmitContext&) {
  672. NotImplemented();
  673. }
  674. void EmitBindlessImageAtomicInc32(EmitContext&) {
  675. NotImplemented();
  676. }
  677. void EmitBindlessImageAtomicDec32(EmitContext&) {
  678. NotImplemented();
  679. }
  680. void EmitBindlessImageAtomicAnd32(EmitContext&) {
  681. NotImplemented();
  682. }
  683. void EmitBindlessImageAtomicOr32(EmitContext&) {
  684. NotImplemented();
  685. }
  686. void EmitBindlessImageAtomicXor32(EmitContext&) {
  687. NotImplemented();
  688. }
  689. void EmitBindlessImageAtomicExchange32(EmitContext&) {
  690. NotImplemented();
  691. }
  692. void EmitBoundImageAtomicIAdd32(EmitContext&) {
  693. NotImplemented();
  694. }
  695. void EmitBoundImageAtomicSMin32(EmitContext&) {
  696. NotImplemented();
  697. }
  698. void EmitBoundImageAtomicUMin32(EmitContext&) {
  699. NotImplemented();
  700. }
  701. void EmitBoundImageAtomicSMax32(EmitContext&) {
  702. NotImplemented();
  703. }
  704. void EmitBoundImageAtomicUMax32(EmitContext&) {
  705. NotImplemented();
  706. }
  707. void EmitBoundImageAtomicInc32(EmitContext&) {
  708. NotImplemented();
  709. }
  710. void EmitBoundImageAtomicDec32(EmitContext&) {
  711. NotImplemented();
  712. }
  713. void EmitBoundImageAtomicAnd32(EmitContext&) {
  714. NotImplemented();
  715. }
  716. void EmitBoundImageAtomicOr32(EmitContext&) {
  717. NotImplemented();
  718. }
  719. void EmitBoundImageAtomicXor32(EmitContext&) {
  720. NotImplemented();
  721. }
  722. void EmitBoundImageAtomicExchange32(EmitContext&) {
  723. NotImplemented();
  724. }
  725. } // namespace Shader::Backend::GLSL