emit_spirv_context_get_set.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <bit>
  4. #include <tuple>
  5. #include <utility>
  6. #include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
  7. #include "shader_recompiler/backend/spirv/spirv_emit_context.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, const Id indirect_func) {
  116. Id buffer_offset;
  117. const Id uniform_type{ctx.uniform_types.*member_ptr};
  118. if (offset.IsImmediate()) {
  119. // Hardware been proved to read the aligned offset (e.g. LDC.U32 at 6 will read offset 4)
  120. const Id imm_offset{ctx.Const(offset.U32() / element_size)};
  121. buffer_offset = imm_offset;
  122. } else if (element_size > 1) {
  123. const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))};
  124. const Id shift{ctx.Const(log2_element_size)};
  125. buffer_offset = ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), shift);
  126. } else {
  127. buffer_offset = ctx.Def(offset);
  128. }
  129. if (!binding.IsImmediate()) {
  130. return ctx.OpFunctionCall(result_type, indirect_func, ctx.Def(binding), buffer_offset);
  131. }
  132. const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr};
  133. const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_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. ctx.load_const_func_u32);
  139. }
  140. Id GetCbufU32x4(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  141. return GetCbuf(ctx, ctx.U32[4], &UniformDefinitions::U32x4, sizeof(u32[4]), binding, offset,
  142. ctx.load_const_func_u32x4);
  143. }
  144. Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 index_offset) {
  145. if (offset.IsImmediate()) {
  146. const u32 element{(offset.U32() / 4) % 4 + index_offset};
  147. return ctx.OpCompositeExtract(ctx.U32[1], vector, element);
  148. }
  149. const Id shift{ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), ctx.Const(2u))};
  150. Id element{ctx.OpBitwiseAnd(ctx.U32[1], shift, ctx.Const(3u))};
  151. if (index_offset > 0) {
  152. element = ctx.OpIAdd(ctx.U32[1], element, ctx.Const(index_offset));
  153. }
  154. return ctx.OpVectorExtractDynamic(ctx.U32[1], vector, element);
  155. }
  156. } // Anonymous namespace
  157. void EmitGetRegister(EmitContext&) {
  158. throw LogicError("Unreachable instruction");
  159. }
  160. void EmitSetRegister(EmitContext&) {
  161. throw LogicError("Unreachable instruction");
  162. }
  163. void EmitGetPred(EmitContext&) {
  164. throw LogicError("Unreachable instruction");
  165. }
  166. void EmitSetPred(EmitContext&) {
  167. throw LogicError("Unreachable instruction");
  168. }
  169. void EmitSetGotoVariable(EmitContext&) {
  170. throw LogicError("Unreachable instruction");
  171. }
  172. void EmitGetGotoVariable(EmitContext&) {
  173. throw LogicError("Unreachable instruction");
  174. }
  175. void EmitSetIndirectBranchVariable(EmitContext&) {
  176. throw LogicError("Unreachable instruction");
  177. }
  178. void EmitGetIndirectBranchVariable(EmitContext&) {
  179. throw LogicError("Unreachable instruction");
  180. }
  181. Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  182. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
  183. const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset,
  184. ctx.load_const_func_u8)};
  185. return ctx.OpUConvert(ctx.U32[1], load);
  186. }
  187. Id element{};
  188. if (ctx.profile.support_descriptor_aliasing) {
  189. element = GetCbufU32(ctx, binding, offset);
  190. } else {
  191. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  192. element = GetCbufElement(ctx, vector, offset, 0u);
  193. }
  194. const Id bit_offset{ctx.BitOffset8(offset)};
  195. return ctx.OpBitFieldUExtract(ctx.U32[1], element, bit_offset, ctx.Const(8u));
  196. }
  197. Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  198. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
  199. const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset,
  200. ctx.load_const_func_u8)};
  201. return ctx.OpSConvert(ctx.U32[1], load);
  202. }
  203. Id element{};
  204. if (ctx.profile.support_descriptor_aliasing) {
  205. element = GetCbufU32(ctx, binding, offset);
  206. } else {
  207. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  208. element = GetCbufElement(ctx, vector, offset, 0u);
  209. }
  210. const Id bit_offset{ctx.BitOffset8(offset)};
  211. return ctx.OpBitFieldSExtract(ctx.U32[1], element, bit_offset, ctx.Const(8u));
  212. }
  213. Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  214. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) {
  215. const Id load{GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset,
  216. ctx.load_const_func_u16)};
  217. return ctx.OpUConvert(ctx.U32[1], load);
  218. }
  219. Id element{};
  220. if (ctx.profile.support_descriptor_aliasing) {
  221. element = GetCbufU32(ctx, binding, offset);
  222. } else {
  223. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  224. element = GetCbufElement(ctx, vector, offset, 0u);
  225. }
  226. const Id bit_offset{ctx.BitOffset16(offset)};
  227. return ctx.OpBitFieldUExtract(ctx.U32[1], element, bit_offset, ctx.Const(16u));
  228. }
  229. Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  230. if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) {
  231. const Id load{GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset,
  232. ctx.load_const_func_u16)};
  233. return ctx.OpSConvert(ctx.U32[1], load);
  234. }
  235. Id element{};
  236. if (ctx.profile.support_descriptor_aliasing) {
  237. element = GetCbufU32(ctx, binding, offset);
  238. } else {
  239. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  240. element = GetCbufElement(ctx, vector, offset, 0u);
  241. }
  242. const Id bit_offset{ctx.BitOffset16(offset)};
  243. return ctx.OpBitFieldSExtract(ctx.U32[1], element, bit_offset, ctx.Const(16u));
  244. }
  245. Id EmitGetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  246. if (ctx.profile.support_descriptor_aliasing) {
  247. return GetCbufU32(ctx, binding, offset);
  248. } else {
  249. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  250. return GetCbufElement(ctx, vector, offset, 0u);
  251. }
  252. }
  253. Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  254. if (ctx.profile.support_descriptor_aliasing) {
  255. return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset,
  256. ctx.load_const_func_f32);
  257. } else {
  258. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  259. return ctx.OpBitcast(ctx.F32[1], GetCbufElement(ctx, vector, offset, 0u));
  260. }
  261. }
  262. Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
  263. if (ctx.profile.support_descriptor_aliasing) {
  264. return GetCbuf(ctx, ctx.U32[2], &UniformDefinitions::U32x2, sizeof(u32[2]), binding, offset,
  265. ctx.load_const_func_u32x2);
  266. } else {
  267. const Id vector{GetCbufU32x4(ctx, binding, offset)};
  268. return ctx.OpCompositeConstruct(ctx.U32[2], GetCbufElement(ctx, vector, offset, 0u),
  269. GetCbufElement(ctx, vector, offset, 1u));
  270. }
  271. }
  272. Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
  273. const u32 element{static_cast<u32>(attr) % 4};
  274. if (IR::IsGeneric(attr)) {
  275. const u32 index{IR::GenericAttributeIndex(attr)};
  276. const std::optional<AttrInfo> type{AttrTypes(ctx, index)};
  277. if (!type || !ctx.runtime_info.previous_stage_stores.Generic(index, element)) {
  278. // Attribute is disabled or varying component is not written
  279. return ctx.Const(element == 3 ? 1.0f : 0.0f);
  280. }
  281. const Id generic_id{ctx.input_generics.at(index)};
  282. const Id pointer{AttrPointer(ctx, type->pointer, vertex, generic_id, ctx.Const(element))};
  283. const Id value{ctx.OpLoad(type->id, pointer)};
  284. return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
  285. }
  286. switch (attr) {
  287. case IR::Attribute::PrimitiveId:
  288. return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.primitive_id));
  289. case IR::Attribute::PositionX:
  290. case IR::Attribute::PositionY:
  291. case IR::Attribute::PositionZ:
  292. case IR::Attribute::PositionW:
  293. return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position,
  294. ctx.Const(element)));
  295. case IR::Attribute::InstanceId:
  296. if (ctx.profile.support_vertex_instance_id) {
  297. return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id));
  298. } else {
  299. const Id index{ctx.OpLoad(ctx.U32[1], ctx.instance_index)};
  300. const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_instance)};
  301. return ctx.OpBitcast(ctx.F32[1], ctx.OpISub(ctx.U32[1], index, base));
  302. }
  303. case IR::Attribute::VertexId:
  304. if (ctx.profile.support_vertex_instance_id) {
  305. return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.vertex_id));
  306. } else {
  307. const Id index{ctx.OpLoad(ctx.U32[1], ctx.vertex_index)};
  308. const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_vertex)};
  309. return ctx.OpBitcast(ctx.F32[1], ctx.OpISub(ctx.U32[1], index, base));
  310. }
  311. case IR::Attribute::FrontFace:
  312. return ctx.OpSelect(ctx.F32[1], ctx.OpLoad(ctx.U1, ctx.front_face),
  313. ctx.OpBitcast(ctx.F32[1], ctx.Const(std::numeric_limits<u32>::max())),
  314. ctx.f32_zero_value);
  315. case IR::Attribute::PointSpriteS:
  316. return ctx.OpLoad(ctx.F32[1],
  317. ctx.OpAccessChain(ctx.input_f32, ctx.point_coord, ctx.u32_zero_value));
  318. case IR::Attribute::PointSpriteT:
  319. return ctx.OpLoad(ctx.F32[1],
  320. ctx.OpAccessChain(ctx.input_f32, ctx.point_coord, ctx.Const(1U)));
  321. case IR::Attribute::TessellationEvaluationPointU:
  322. return ctx.OpLoad(ctx.F32[1],
  323. ctx.OpAccessChain(ctx.input_f32, ctx.tess_coord, ctx.u32_zero_value));
  324. case IR::Attribute::TessellationEvaluationPointV:
  325. return ctx.OpLoad(ctx.F32[1],
  326. ctx.OpAccessChain(ctx.input_f32, ctx.tess_coord, ctx.Const(1U)));
  327. default:
  328. throw NotImplementedException("Read attribute {}", attr);
  329. }
  330. }
  331. Id EmitGetAttributeU32(EmitContext& ctx, IR::Attribute attr, Id) {
  332. switch (attr) {
  333. case IR::Attribute::PrimitiveId:
  334. return ctx.OpLoad(ctx.U32[1], ctx.primitive_id);
  335. case IR::Attribute::InstanceId:
  336. if (ctx.profile.support_vertex_instance_id) {
  337. return ctx.OpLoad(ctx.U32[1], ctx.instance_id);
  338. } else {
  339. const Id index{ctx.OpLoad(ctx.U32[1], ctx.instance_index)};
  340. const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_instance)};
  341. return ctx.OpISub(ctx.U32[1], index, base);
  342. }
  343. case IR::Attribute::VertexId:
  344. if (ctx.profile.support_vertex_instance_id) {
  345. return ctx.OpLoad(ctx.U32[1], ctx.vertex_id);
  346. } else {
  347. const Id index{ctx.OpLoad(ctx.U32[1], ctx.vertex_index)};
  348. const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_vertex)};
  349. return ctx.OpISub(ctx.U32[1], index, base);
  350. }
  351. default:
  352. throw NotImplementedException("Read U32 attribute {}", attr);
  353. }
  354. }
  355. void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, [[maybe_unused]] Id vertex) {
  356. const std::optional<OutAttr> output{OutputAttrPointer(ctx, attr)};
  357. if (!output) {
  358. return;
  359. }
  360. if (Sirit::ValidId(output->type)) {
  361. value = ctx.OpBitcast(output->type, value);
  362. }
  363. ctx.OpStore(output->pointer, value);
  364. }
  365. Id EmitGetAttributeIndexed(EmitContext& ctx, Id offset, Id vertex) {
  366. switch (ctx.stage) {
  367. case Stage::TessellationControl:
  368. case Stage::TessellationEval:
  369. case Stage::Geometry:
  370. return ctx.OpFunctionCall(ctx.F32[1], ctx.indexed_load_func, offset, vertex);
  371. default:
  372. return ctx.OpFunctionCall(ctx.F32[1], ctx.indexed_load_func, offset);
  373. }
  374. }
  375. void EmitSetAttributeIndexed(EmitContext& ctx, Id offset, Id value, [[maybe_unused]] Id vertex) {
  376. ctx.OpFunctionCall(ctx.void_id, ctx.indexed_store_func, offset, value);
  377. }
  378. Id EmitGetPatch(EmitContext& ctx, IR::Patch patch) {
  379. if (!IR::IsGeneric(patch)) {
  380. throw NotImplementedException("Non-generic patch load");
  381. }
  382. const u32 index{IR::GenericPatchIndex(patch)};
  383. const Id element{ctx.Const(IR::GenericPatchElement(patch))};
  384. const Id type{ctx.stage == Stage::TessellationControl ? ctx.output_f32 : ctx.input_f32};
  385. const Id pointer{ctx.OpAccessChain(type, ctx.patches.at(index), element)};
  386. return ctx.OpLoad(ctx.F32[1], pointer);
  387. }
  388. void EmitSetPatch(EmitContext& ctx, IR::Patch patch, Id value) {
  389. const Id pointer{[&] {
  390. if (IR::IsGeneric(patch)) {
  391. const u32 index{IR::GenericPatchIndex(patch)};
  392. const Id element{ctx.Const(IR::GenericPatchElement(patch))};
  393. return ctx.OpAccessChain(ctx.output_f32, ctx.patches.at(index), element);
  394. }
  395. switch (patch) {
  396. case IR::Patch::TessellationLodLeft:
  397. case IR::Patch::TessellationLodRight:
  398. case IR::Patch::TessellationLodTop:
  399. case IR::Patch::TessellationLodBottom: {
  400. const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
  401. const Id index_id{ctx.Const(index)};
  402. return ctx.OpAccessChain(ctx.output_f32, ctx.output_tess_level_outer, index_id);
  403. }
  404. case IR::Patch::TessellationLodInteriorU:
  405. return ctx.OpAccessChain(ctx.output_f32, ctx.output_tess_level_inner,
  406. ctx.u32_zero_value);
  407. case IR::Patch::TessellationLodInteriorV:
  408. return ctx.OpAccessChain(ctx.output_f32, ctx.output_tess_level_inner, ctx.Const(1u));
  409. default:
  410. throw NotImplementedException("Patch {}", patch);
  411. }
  412. }()};
  413. ctx.OpStore(pointer, value);
  414. }
  415. void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, Id value) {
  416. const Id component_id{ctx.Const(component)};
  417. const Id pointer{ctx.OpAccessChain(ctx.output_f32, ctx.frag_color.at(index), component_id)};
  418. ctx.OpStore(pointer, value);
  419. }
  420. void EmitSetSampleMask(EmitContext& ctx, Id value) {
  421. ctx.OpStore(ctx.sample_mask, value);
  422. }
  423. void EmitSetFragDepth(EmitContext& ctx, Id value) {
  424. if (!ctx.runtime_info.convert_depth_mode) {
  425. ctx.OpStore(ctx.frag_depth, value);
  426. return;
  427. }
  428. const Id unit{ctx.Const(0.5f)};
  429. const Id new_depth{ctx.OpFma(ctx.F32[1], value, unit, unit)};
  430. ctx.OpStore(ctx.frag_depth, new_depth);
  431. }
  432. void EmitGetZFlag(EmitContext&) {
  433. throw NotImplementedException("SPIR-V Instruction");
  434. }
  435. void EmitGetSFlag(EmitContext&) {
  436. throw NotImplementedException("SPIR-V Instruction");
  437. }
  438. void EmitGetCFlag(EmitContext&) {
  439. throw NotImplementedException("SPIR-V Instruction");
  440. }
  441. void EmitGetOFlag(EmitContext&) {
  442. throw NotImplementedException("SPIR-V Instruction");
  443. }
  444. void EmitSetZFlag(EmitContext&) {
  445. throw NotImplementedException("SPIR-V Instruction");
  446. }
  447. void EmitSetSFlag(EmitContext&) {
  448. throw NotImplementedException("SPIR-V Instruction");
  449. }
  450. void EmitSetCFlag(EmitContext&) {
  451. throw NotImplementedException("SPIR-V Instruction");
  452. }
  453. void EmitSetOFlag(EmitContext&) {
  454. throw NotImplementedException("SPIR-V Instruction");
  455. }
  456. Id EmitWorkgroupId(EmitContext& ctx) {
  457. return ctx.OpLoad(ctx.U32[3], ctx.workgroup_id);
  458. }
  459. Id EmitLocalInvocationId(EmitContext& ctx) {
  460. return ctx.OpLoad(ctx.U32[3], ctx.local_invocation_id);
  461. }
  462. Id EmitInvocationId(EmitContext& ctx) {
  463. return ctx.OpLoad(ctx.U32[1], ctx.invocation_id);
  464. }
  465. Id EmitSampleId(EmitContext& ctx) {
  466. return ctx.OpLoad(ctx.U32[1], ctx.sample_id);
  467. }
  468. Id EmitIsHelperInvocation(EmitContext& ctx) {
  469. return ctx.OpLoad(ctx.U1, ctx.is_helper_invocation);
  470. }
  471. Id EmitYDirection(EmitContext& ctx) {
  472. return ctx.Const(ctx.runtime_info.y_negate ? -1.0f : 1.0f);
  473. }
  474. Id EmitResolutionDownFactor(EmitContext& ctx) {
  475. if (ctx.profile.unified_descriptor_binding) {
  476. const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[1])};
  477. const Id index{ctx.Const(ctx.rescaling_downfactor_member_index)};
  478. const Id pointer{ctx.OpAccessChain(pointer_type, ctx.rescaling_push_constants, index)};
  479. return ctx.OpLoad(ctx.F32[1], pointer);
  480. } else {
  481. const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)};
  482. return ctx.OpCompositeExtract(ctx.F32[1], composite, 2u);
  483. }
  484. }
  485. Id EmitRenderArea(EmitContext& ctx) {
  486. if (ctx.profile.unified_descriptor_binding) {
  487. const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[4])};
  488. const Id index{ctx.Const(ctx.render_are_member_index)};
  489. const Id pointer{ctx.OpAccessChain(pointer_type, ctx.render_area_push_constant, index)};
  490. return ctx.OpLoad(ctx.F32[4], pointer);
  491. } else {
  492. throw NotImplementedException("SPIR-V Instruction");
  493. }
  494. }
  495. Id EmitLoadLocal(EmitContext& ctx, Id word_offset) {
  496. const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)};
  497. return ctx.OpLoad(ctx.U32[1], pointer);
  498. }
  499. void EmitWriteLocal(EmitContext& ctx, Id word_offset, Id value) {
  500. const Id pointer{ctx.OpAccessChain(ctx.private_u32, ctx.local_memory, word_offset)};
  501. ctx.OpStore(pointer, value);
  502. }
  503. } // namespace Shader::Backend::SPIRV