emit_glsl_bitwise_conversion.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string_view>
  5. #include "shader_recompiler/backend/glsl/emit_context.h"
  6. #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
  7. #include "shader_recompiler/frontend/ir/value.h"
  8. namespace Shader::Backend::GLSL {
  9. namespace {
  10. void Alias(IR::Inst& inst, const IR::Value& value) {
  11. if (value.IsImmediate()) {
  12. return;
  13. }
  14. IR::Inst& value_inst{*value.InstRecursive()};
  15. value_inst.DestructiveAddUsage(inst.UseCount());
  16. value_inst.DestructiveRemoveUsage();
  17. inst.SetDefinition(value_inst.Definition<Id>());
  18. }
  19. } // namespace
  20. void EmitIdentity(EmitContext&, IR::Inst& inst, const IR::Value& value) {
  21. Alias(inst, value);
  22. }
  23. void EmitConditionRef(EmitContext& ctx, IR::Inst& inst, const IR::Value& value) {
  24. // Fake one usage to get a real variable out of the condition
  25. inst.DestructiveAddUsage(1);
  26. const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U1)};
  27. const auto input{ctx.var_alloc.Consume(value)};
  28. if (ret != input) {
  29. ctx.Add("{}={};", ret, input);
  30. }
  31. }
  32. void EmitBitCastU16F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) {
  33. NotImplemented();
  34. }
  35. void EmitBitCastU32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  36. ctx.AddU32("{}=ftou({});", inst, value);
  37. }
  38. void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  39. ctx.AddU64("{}=doubleBitsToUint64({});", inst, value);
  40. }
  41. void EmitBitCastF16U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) {
  42. NotImplemented();
  43. }
  44. void EmitBitCastF32U32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  45. ctx.AddF32("{}=utof({});", inst, value);
  46. }
  47. void EmitBitCastF64U64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  48. ctx.AddF64("{}=uint64BitsToDouble({});", inst, value);
  49. }
  50. void EmitPackUint2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  51. ctx.AddU64("{}=packUint2x32({});", inst, value);
  52. }
  53. void EmitUnpackUint2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  54. ctx.AddU32x2("{}=unpackUint2x32({});", inst, value);
  55. }
  56. void EmitPackFloat2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  57. ctx.AddU32("{}=packFloat2x16({});", inst, value);
  58. }
  59. void EmitUnpackFloat2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  60. ctx.AddF16x2("{}=unpackFloat2x16({});", inst, value);
  61. }
  62. void EmitPackHalf2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  63. ctx.AddU32("{}=packHalf2x16({});", inst, value);
  64. }
  65. void EmitUnpackHalf2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  66. ctx.AddF32x2("{}=unpackHalf2x16({});", inst, value);
  67. }
  68. void EmitPackDouble2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  69. ctx.AddF64("{}=packDouble2x32({});", inst, value);
  70. }
  71. void EmitUnpackDouble2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
  72. ctx.AddU32x2("{}=unpackDouble2x32({});", inst, value);
  73. }
  74. } // namespace Shader::Backend::GLSL