emit_spirv_context_get_set.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <tuple>
  5. #include <utility>
  6. #include "shader_recompiler/backend/spirv/emit_spirv.h"
  7. #include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
  8. namespace Shader::Backend::SPIRV {
  9. namespace {
  10. struct AttrInfo {
  11. Id pointer;
  12. Id id;
  13. bool needs_cast;
  14. };
  15. std::optional<AttrInfo> AttrTypes(EmitContext& ctx, u32 index) {
  16. const AttributeType type{ctx.runtime_info.generic_input_types.at(index)};
  17. switch (type) {
  18. case AttributeType::Float:
  19. return AttrInfo{ctx.input_f32, ctx.F32[1], false};
  20. case AttributeType::UnsignedInt:
  21. return AttrInfo{ctx.input_u32, ctx.U32[1], true};
  22. case AttributeType::SignedInt:
  23. return AttrInfo{ctx.input_s32, ctx.TypeInt(32, true), true};
  24. case AttributeType::Disabled:
  25. return std::nullopt;
  26. }
  27. throw InvalidArgument("Invalid attribute type {}", type);
  28. }
  29. template <typename... Args>
  30. Id AttrPointer(EmitContext& ctx, Id pointer_type, Id vertex, Id base, Args&&... args) {
  31. switch (ctx.stage) {
  32. case Stage::TessellationControl:
  33. case Stage::TessellationEval:
  34. case Stage::Geometry:
  35. return ctx.OpAccessChain(pointer_type, base, vertex, std::forward<Args>(args)...);
  36. default:
  37. return ctx.OpAccessChain(pointer_type, base, std::forward<Args>(args)...);
  38. }
  39. }
  40. template <typename... Args>
  41. Id OutputAccessChain(EmitContext& ctx, Id result_type, Id base, Args&&... args) {
  42. if (ctx.stage == Stage::TessellationControl) {
  43. const Id invocation_id{ctx.OpLoad(ctx.U32[1], ctx.invocation_id)};
  44. return ctx.OpAccessChain(result_type, base, invocation_id, std::forward<Args>(args)...);
  45. } else {
  46. return ctx.OpAccessChain(result_type, base, std::forward<Args>(args)...);
  47. }
  48. }
  49. struct OutAttr {
  50. OutAttr(Id pointer_) : pointer{pointer_} {}
  51. OutAttr(Id pointer_, Id type_) : pointer{pointer_}, type{type_} {}
  52. Id pointer{};
  53. Id type{};
  54. };
  55. std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
  56. if (IR::IsGeneric(attr)) {
  57. const u32 index{IR::GenericAttributeIndex(attr)};
  58. const u32 element{IR::GenericAttributeElement(attr)};
  59. const GenericElementInfo& info{ctx.output_generics.at(index).at(element)};
  60. if (info.num_components == 1) {
  61. return info.id;
  62. } else {
  63. const u32 index_element{element - info.first_element};
  64. const Id index_id{ctx.Const(index_element)};
  65. return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id);
  66. }
  67. }
  68. switch (attr) {
  69. case IR::Attribute::PointSize:
  70. return ctx.output_point_size;
  71. case IR::Attribute::PositionX:
  72. case IR::Attribute::PositionY:
  73. case IR::Attribute::PositionZ:
  74. case IR::Attribute::PositionW: {
  75. const u32 element{static_cast<u32>(attr) % 4};
  76. const Id element_id{ctx.Const(element)};
  77. return OutputAccessChain(ctx, ctx.output_f32, ctx.output_position, element_id);
  78. }
  79. case IR::Attribute::ClipDistance0:
  80. case IR::Attribute::ClipDistance1:
  81. case IR::Attribute::ClipDistance2:
  82. case IR::Attribute::ClipDistance3:
  83. case IR::Attribute::ClipDistance4:
  84. case IR::Attribute::ClipDistance5:
  85. case IR::Attribute::ClipDistance6:
  86. case IR::Attribute::ClipDistance7: {
  87. const u32 base{static_cast<u32>(IR::Attribute::ClipDistance0)};
  88. const u32 index{static_cast<u32>(attr) - base};
  89. const Id clip_num{ctx.Const(index)};
  90. return OutputAccessChain(ctx, ctx.output_f32, ctx.clip_distances, clip_num);
  91. }
  92. case IR::Attribute::Layer:
  93. if (ctx.profile.support_viewport_index_layer_non_geometry ||
  94. ctx.stage == Shader::Stage::Geometry) {
  95. return OutAttr{ctx.layer, ctx.U32[1]};
  96. }
  97. return std::nullopt;
  98. case IR::Attribute::ViewportIndex:
  99. if (ctx.profile.support_viewport_index_layer_non_geometry ||
  100. ctx.stage == Shader::Stage::Geometry) {
  101. return OutAttr{ctx.viewport_index, ctx.U32[1]};
  102. }
  103. return std::nullopt;
  104. case IR::Attribute::ViewportMask:
  105. if (!ctx.profile.support_viewport_mask) {
  106. return std::nullopt;
  107. }
  108. return OutAttr{ctx.OpAccessChain(ctx.output_u32, ctx.viewport_mask, ctx.u32_zero_value),
  109. ctx.U32[1]};
  110. default:
  111. throw NotImplementedException("Read attribute {}", attr);
  112. }
  113. }
  114. Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr, u32 element_size,
  115. const IR::Value& binding, const IR::Value& offset) {
  116. if (!binding.IsImmediate()) {
  117. throw NotImplementedException("Constant buffer indexing");
  118. }
  119. const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr};
  120. const Id uniform_type{ctx.uniform_types.*member_ptr};
  121. if (!offset.IsImmediate()) {
  122. Id index{ctx.Def(offset)};
  123. if (element_size > 1) {
  124. const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))};
  125. const Id shift{ctx.Const(log2_element_size)};
  126. index = ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), shift);
  127. }
  128. const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, index)};
  129. return ctx.OpLoad(result_type, access_chain);
  130. }
  131. // Hardware been proved to read the aligned offset (e.g. LDC.U32 at 6 will read offset 4)
  132. const Id imm_offset{ctx.Const(offset.U32() / element_size)};
  133. const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, imm_offset)};
  134. return ctx.OpLoad(result_type, access_chain);
  135. }
  136. Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  137. return GetCbuf(ctx, ctx.U32[1], &UniformDefinitions::U32, sizeof(u32), binding, offset);
  138. }
  139. Id GetCbufU32x4(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  140. return GetCbuf(ctx, ctx.U32[4], &UniformDefinitions::U32x4, sizeof(u32[4]), binding, offset);
  141. }
  142. Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 index_offset) {
  143. if (offset.IsImmediate()) {
  144. const u32 element{(offset.U32() / 4) % 4 + index_offset};
  145. return ctx.OpCompositeExtract(ctx.U32[1], vector, element);
  146. }
  147. const Id shift{ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), ctx.Const(2u))};
  148. Id element{ctx.OpBitwiseAnd(ctx.U32[1], shift, ctx.Const(3u))};
  149. if (index_offset > 0) {
  150. element = ctx.OpIAdd(ctx.U32[1], element, ctx.Const(index_offset));
  151. }
  152. return ctx.OpVectorExtractDynamic(ctx.U32[1], vector, element);
  153. }
  154. } // Anonymous namespace
  155. void EmitGetRegister(EmitContext&) {
  156. throw LogicError("Unreachable instruction");
  157. }
  158. void EmitSetRegister(EmitContext&) {
  159. throw LogicError("Unreachable instruction");
  160. }
  161. void EmitGetPred(EmitContext&) {
  162. throw LogicError("Unreachable instruction");
  163. }
  164. void EmitSetPred(EmitContext&) {
  165. throw LogicError("Unreachable instruction");
  166. }
  167. void EmitSetGotoVariable(EmitContext&) {
  168. throw LogicError("Unreachable instruction");
  169. }
  170. void EmitGetGotoVariable(EmitContext&) {
  171. throw LogicError("Unreachable instruction");
  172. }
  173. void EmitSetIndirectBranchVariable(EmitContext&) {
  174. throw LogicError("Unreachable instruction");
  175. }
  176. void EmitGetIndirectBranchVariable(EmitContext&) {
  177. throw LogicError("Unreachable instruction");
  178. }
  179. Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  180. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
  181. const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset)};
  182. return ctx.OpUConvert(ctx.U32[1], load);
  183. }
  184. Id element{};
  185. if (ctx.profile.support_descriptor_aliasing) {
  186. element = GetCbufU32(ctx, binding, offset);
  187. } else {
  188. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  189. element = GetCbufElement(ctx, vector, offset, 0u);
  190. }
  191. const Id bit_offset{ctx.BitOffset8(offset)};
  192. return ctx.OpBitFieldUExtract(ctx.U32[1], element, bit_offset, ctx.Const(8u));
  193. }
  194. Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  195. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
  196. const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset)};
  197. return ctx.OpSConvert(ctx.U32[1], load);
  198. }
  199. Id element{};
  200. if (ctx.profile.support_descriptor_aliasing) {
  201. element = GetCbufU32(ctx, binding, offset);
  202. } else {
  203. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  204. element = GetCbufElement(ctx, vector, offset, 0u);
  205. }
  206. const Id bit_offset{ctx.BitOffset8(offset)};
  207. return ctx.OpBitFieldSExtract(ctx.U32[1], element, bit_offset, ctx.Const(8u));
  208. }
  209. Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  210. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) {
  211. const Id load{
  212. GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset)};
  213. return ctx.OpUConvert(ctx.U32[1], load);
  214. }
  215. Id element{};
  216. if (ctx.profile.support_descriptor_aliasing) {
  217. element = GetCbufU32(ctx, binding, offset);
  218. } else {
  219. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  220. element = GetCbufElement(ctx, vector, offset, 0u);
  221. }
  222. const Id bit_offset{ctx.BitOffset16(offset)};
  223. return ctx.OpBitFieldUExtract(ctx.U32[1], element, bit_offset, ctx.Const(16u));
  224. }
  225. Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  226. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) {
  227. const Id load{
  228. GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset)};
  229. return ctx.OpSConvert(ctx.U32[1], load);
  230. }
  231. Id element{};
  232. if (ctx.profile.support_descriptor_aliasing) {
  233. element = GetCbufU32(ctx, binding, offset);
  234. } else {
  235. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  236. element = GetCbufElement(ctx, vector, offset, 0u);
  237. }
  238. const Id bit_offset{ctx.BitOffset16(offset)};
  239. return ctx.OpBitFieldSExtract(ctx.U32[1], element, bit_offset, ctx.Const(16u));
  240. }
  241. Id EmitGetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  242. if (ctx.profile.support_descriptor_aliasing) {
  243. return GetCbufU32(ctx, binding, offset);
  244. } else {
  245. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  246. return GetCbufElement(ctx, vector, offset, 0u);
  247. }
  248. }
  249. Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  250. if (ctx.profile.support_descriptor_aliasing) {
  251. return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset);
  252. } else {
  253. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  254. return ctx.OpBitcast(ctx.F32[1], GetCbufElement(ctx, vector, offset, 0u));
  255. }
  256. }
  257. Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  258. if (ctx.profile.support_descriptor_aliasing) {
  259. return GetCbuf(ctx, ctx.U32[2], &UniformDefinitions::U32x2, sizeof(u32[2]), binding,
  260. offset);
  261. } else {
  262. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  263. return ctx.OpCompositeConstruct(ctx.U32[2], GetCbufElement(ctx, vector, offset, 0u),
  264. GetCbufElement(ctx, vector, offset, 1u));
  265. }
  266. }
  267. Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
  268. const u32 element{static_cast<u32>(attr) % 4};
  269. if (IR::IsGeneric(attr)) {
  270. const u32 index{IR::GenericAttributeIndex(attr)};
  271. const std::optional<AttrInfo> type{AttrTypes(ctx, index)};
  272. if (!type || !ctx.runtime_info.previous_stage_stores.Generic(index, element)) {
  273. // Attribute is disabled or varying component is not written
  274. return ctx.Const(element == 3 ? 1.0f : 0.0f);
  275. }
  276. const Id generic_id{ctx.input_generics.at(index)};
  277. const Id pointer{AttrPointer(ctx, type->pointer, vertex, generic_id, ctx.Const(element))};
  278. const Id value{ctx.OpLoad(type->id, pointer)};
  279. return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
  280. }
  281. switch (attr) {
  282. case IR::Attribute::PrimitiveId:
  283. return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.primitive_id));
  284. case IR::Attribute::PositionX:
  285. case IR::Attribute::PositionY:
  286. case IR::Attribute::PositionZ:
  287. case IR::Attribute::PositionW:
  288. return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position,
  289. ctx.Const(element)));
  290. case IR::Attribute::InstanceId:
  291. if (ctx.profile.support_vertex_instance_id) {
  292. return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id));
  293. } else {
  294. const Id index{ctx.OpLoad(ctx.U32[1], ctx.instance_index)};
  295. const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_instance)};
  296. return ctx.OpBitcast(ctx.F32[1], ctx.OpISub(ctx.U32[1], index, base));
  297. }
  298. case IR::Attribute::VertexId:
  299. if (ctx.profile.support_vertex_instance_id) {
  300. return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.vertex_id));
  301. } else {
  302. const Id index{ctx.OpLoad(ctx.U32[1], ctx.vertex_index)};
  303. const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_vertex)};
  304. return ctx.OpBitcast(ctx.F32[1], ctx.OpISub(ctx.U32[1], index, base));
  305. }
  306. case IR::Attribute::FrontFace:
  307. return ctx.OpSelect(ctx.U32[1], ctx.OpLoad(ctx.U1, ctx.front_face),
  308. ctx.Const(std::numeric_limits<u32>::max()), ctx.u32_zero_value);
  309. case IR::Attribute::PointSpriteS:
  310. return ctx.OpLoad(ctx.F32[1],
  311. ctx.OpAccessChain(ctx.input_f32, ctx.point_coord, ctx.u32_zero_value));
  312. case IR::Attribute::PointSpriteT:
  313. return ctx.OpLoad(ctx.F32[1],
  314. ctx.OpAccessChain(ctx.input_f32, ctx.point_coord, ctx.Const(1U)));
  315. case IR::Attribute::TessellationEvaluationPointU:
  316. return ctx.OpLoad(ctx.F32[1],
  317. ctx.OpAccessChain(ctx.input_f32, ctx.tess_coord, ctx.u32_zero_value));
  318. case IR::Attribute::TessellationEvaluationPointV:
  319. return ctx.OpLoad(ctx.F32[1],
  320. ctx.OpAccessChain(ctx.input_f32, ctx.tess_coord, ctx.Const(1U)));
  321. default:
  322. throw NotImplementedException("Read attribute {}", attr);
  323. }
  324. }
  325. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, [[maybe_unused]] Id vertex) {
  326. const std::optional<OutAttr> output{OutputAttrPointer(ctx, attr)};
  327. if (!output) {
  328. return;
  329. }
  330. if (Sirit::ValidId(output->type)) {
  331. value = ctx.OpBitcast(output->type, value);
  332. }
  333. ctx.OpStore(output->pointer, value);
  334. }
  335. Id EmitGetAttributeIndexed(EmitContext& ctx, Id offset, Id vertex) {
  336. switch (ctx.stage) {
  337. case Stage::TessellationControl:
  338. case Stage::TessellationEval:
  339. case Stage::Geometry:
  340. return ctx.OpFunctionCall(ctx.F32[1], ctx.indexed_load_func, offset, vertex);
  341. default:
  342. return ctx.OpFunctionCall(ctx.F32[1], ctx.indexed_load_func, offset);
  343. }
  344. }
  345. void EmitSetAttributeIndexed(EmitContext& ctx, Id offset, Id value, [[maybe_unused]] Id vertex) {
  346. ctx.OpFunctionCall(ctx.void_id, ctx.indexed_store_func, offset, value);
  347. }
  348. Id EmitGetPatch(EmitContext& ctx, IR::Patch patch) {
  349. if (!IR::IsGeneric(patch)) {
  350. throw NotImplementedException("Non-generic patch load");
  351. }
  352. const u32 index{IR::GenericPatchIndex(patch)};
  353. const Id element{ctx.Const(IR::GenericPatchElement(patch))};
  354. const Id type{ctx.stage == Stage::TessellationControl ? ctx.output_f32 : ctx.input_f32};
  355. const Id pointer{ctx.OpAccessChain(type, ctx.patches.at(index), element)};
  356. return ctx.OpLoad(ctx.F32[1], pointer);
  357. }
  358. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, Id value) {
  359. const Id pointer{[&] {
  360. if (IR::IsGeneric(patch)) {
  361. const u32 index{IR::GenericPatchIndex(patch)};
  362. const Id element{ctx.Const(IR::GenericPatchElement(patch))};
  363. return ctx.OpAccessChain(ctx.output_f32, ctx.patches.at(index), element);
  364. }
  365. switch (patch) {
  366. case IR::Patch::TessellationLodLeft:
  367. case IR::Patch::TessellationLodRight:
  368. case IR::Patch::TessellationLodTop:
  369. case IR::Patch::TessellationLodBottom: {
  370. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  371. const Id index_id{ctx.Const(index)};
  372. return ctx.OpAccessChain(ctx.output_f32, ctx.output_tess_level_outer, index_id);
  373. }
  374. case IR::Patch::TessellationLodInteriorU:
  375. return ctx.OpAccessChain(ctx.output_f32, ctx.output_tess_level_inner,
  376. ctx.u32_zero_value);
  377. case IR::Patch::TessellationLodInteriorV:
  378. return ctx.OpAccessChain(ctx.output_f32, ctx.output_tess_level_inner, ctx.Const(1u));
  379. default:
  380. throw NotImplementedException("Patch {}", patch);
  381. }
  382. }()};
  383. ctx.OpStore(pointer, value);
  384. }
  385. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, Id value) {
  386. const Id component_id{ctx.Const(component)};
  387. const Id pointer{ctx.OpAccessChain(ctx.output_f32, ctx.frag_color.at(index), component_id)};
  388. ctx.OpStore(pointer, value);
  389. }
  390. void EmitSetSampleMask(EmitContext& ctx, Id value) {
  391. ctx.OpStore(ctx.sample_mask, value);
  392. }
  393. void EmitSetFragDepth(EmitContext& ctx, Id value) {
  394. ctx.OpStore(ctx.frag_depth, value);
  395. }
  396. void EmitGetZFlag(EmitContext&) {
  397. throw NotImplementedException("SPIR-V Instruction");
  398. }
  399. void EmitGetSFlag(EmitContext&) {
  400. throw NotImplementedException("SPIR-V Instruction");
  401. }
  402. void EmitGetCFlag(EmitContext&) {
  403. throw NotImplementedException("SPIR-V Instruction");
  404. }
  405. void EmitGetOFlag(EmitContext&) {
  406. throw NotImplementedException("SPIR-V Instruction");
  407. }
  408. void EmitSetZFlag(EmitContext&) {
  409. throw NotImplementedException("SPIR-V Instruction");
  410. }
  411. void EmitSetSFlag(EmitContext&) {
  412. throw NotImplementedException("SPIR-V Instruction");
  413. }
  414. void EmitSetCFlag(EmitContext&) {
  415. throw NotImplementedException("SPIR-V Instruction");
  416. }
  417. void EmitSetOFlag(EmitContext&) {
  418. throw NotImplementedException("SPIR-V Instruction");
  419. }
  420. Id EmitWorkgroupId(EmitContext& ctx) {
  421. return ctx.OpLoad(ctx.U32[3], ctx.workgroup_id);
  422. }
  423. Id EmitLocalInvocationId(EmitContext& ctx) {
  424. return ctx.OpLoad(ctx.U32[3], ctx.local_invocation_id);
  425. }
  426. Id EmitInvocationId(EmitContext& ctx) {
  427. return ctx.OpLoad(ctx.U32[1], ctx.invocation_id);
  428. }
  429. Id EmitSampleId(EmitContext& ctx) {
  430. return ctx.OpLoad(ctx.U32[1], ctx.sample_id);
  431. }
  432. Id EmitIsHelperInvocation(EmitContext& ctx) {
  433. return ctx.OpLoad(ctx.U1, ctx.is_helper_invocation);
  434. }
  435. Id EmitYDirection(EmitContext& ctx) {
  436. return ctx.Const(ctx.runtime_info.y_negate ? -1.0f : 1.0f);
  437. }
  438. Id EmitLoadLocal(EmitContext& ctx, Id word_offset) {
  439. const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)};
  440. return ctx.OpLoad(ctx.U32[1], pointer);
  441. }
  442. void EmitWriteLocal(EmitContext& ctx, Id word_offset, Id value) {
  443. const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)};
  444. ctx.OpStore(pointer, value);
  445. }
  446. } // namespace Shader::Backend::SPIRV