emit_spirv_special.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/backend/spirv/emit_spirv.h"
  5. #include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
  6. #include "shader_recompiler/backend/spirv/spirv_emit_context.h"
  7. namespace Shader::Backend::SPIRV {
  8. namespace {
  9. void ConvertDepthMode(EmitContext& ctx) {
  10. const Id type{ctx.F32[1]};
  11. const Id position{ctx.OpLoad(ctx.F32[4], ctx.output_position)};
  12. const Id z{ctx.OpCompositeExtract(type, position, 2u)};
  13. const Id w{ctx.OpCompositeExtract(type, position, 3u)};
  14. const Id screen_depth{ctx.OpFMul(type, ctx.OpFAdd(type, z, w), ctx.Constant(type, 0.5f))};
  15. const Id vector{ctx.OpCompositeInsert(ctx.F32[4], screen_depth, position, 2u)};
  16. ctx.OpStore(ctx.output_position, vector);
  17. }
  18. void SetFixedPipelinePointSize(EmitContext& ctx) {
  19. if (ctx.runtime_info.fixed_state_point_size) {
  20. const float point_size{*ctx.runtime_info.fixed_state_point_size};
  21. ctx.OpStore(ctx.output_point_size, ctx.Const(point_size));
  22. }
  23. }
  24. Id DefaultVarying(EmitContext& ctx, u32 num_components, u32 element, Id zero, Id one,
  25. Id default_vector) {
  26. switch (num_components) {
  27. case 1:
  28. return element == 3 ? one : zero;
  29. case 2:
  30. return ctx.ConstantComposite(ctx.F32[2], zero, element + 1 == 3 ? one : zero);
  31. case 3:
  32. return ctx.ConstantComposite(ctx.F32[3], zero, zero, element + 2 == 3 ? one : zero);
  33. case 4:
  34. return default_vector;
  35. }
  36. throw InvalidArgument("Bad element");
  37. }
  38. Id ComparisonFunction(EmitContext& ctx, CompareFunction comparison, Id operand_1, Id operand_2) {
  39. switch (comparison) {
  40. case CompareFunction::Never:
  41. return ctx.false_value;
  42. case CompareFunction::Less:
  43. return ctx.OpFOrdLessThan(ctx.U1, operand_1, operand_2);
  44. case CompareFunction::Equal:
  45. return ctx.OpFOrdEqual(ctx.U1, operand_1, operand_2);
  46. case CompareFunction::LessThanEqual:
  47. return ctx.OpFOrdLessThanEqual(ctx.U1, operand_1, operand_2);
  48. case CompareFunction::Greater:
  49. return ctx.OpFOrdGreaterThan(ctx.U1, operand_1, operand_2);
  50. case CompareFunction::NotEqual:
  51. return ctx.OpFOrdNotEqual(ctx.U1, operand_1, operand_2);
  52. case CompareFunction::GreaterThanEqual:
  53. return ctx.OpFOrdGreaterThanEqual(ctx.U1, operand_1, operand_2);
  54. case CompareFunction::Always:
  55. return ctx.true_value;
  56. }
  57. throw InvalidArgument("Comparison function {}", comparison);
  58. }
  59. void AlphaTest(EmitContext& ctx) {
  60. if (!ctx.runtime_info.alpha_test_func) {
  61. return;
  62. }
  63. const auto comparison{*ctx.runtime_info.alpha_test_func};
  64. if (comparison == CompareFunction::Always) {
  65. return;
  66. }
  67. if (!Sirit::ValidId(ctx.frag_color[0])) {
  68. return;
  69. }
  70. const Id type{ctx.F32[1]};
  71. const Id rt0_color{ctx.OpLoad(ctx.F32[4], ctx.frag_color[0])};
  72. const Id alpha{ctx.OpCompositeExtract(type, rt0_color, 3u)};
  73. const Id true_label{ctx.OpLabel()};
  74. const Id discard_label{ctx.OpLabel()};
  75. const Id alpha_reference{ctx.Const(ctx.runtime_info.alpha_test_reference)};
  76. const Id condition{ComparisonFunction(ctx, comparison, alpha, alpha_reference)};
  77. ctx.OpSelectionMerge(true_label, spv::SelectionControlMask::MaskNone);
  78. ctx.OpBranchConditional(condition, true_label, discard_label);
  79. ctx.AddLabel(discard_label);
  80. ctx.OpKill();
  81. ctx.AddLabel(true_label);
  82. }
  83. } // Anonymous namespace
  84. void EmitPrologue(EmitContext& ctx) {
  85. if (ctx.stage == Stage::VertexB) {
  86. const Id zero{ctx.Const(0.0f)};
  87. const Id one{ctx.Const(1.0f)};
  88. const Id default_vector{ctx.ConstantComposite(ctx.F32[4], zero, zero, zero, one)};
  89. ctx.OpStore(ctx.output_position, default_vector);
  90. for (const auto& info : ctx.output_generics) {
  91. if (info[0].num_components == 0) {
  92. continue;
  93. }
  94. u32 element{0};
  95. while (element < 4) {
  96. const auto& element_info{info[element]};
  97. const u32 num{element_info.num_components};
  98. const Id value{DefaultVarying(ctx, num, element, zero, one, default_vector)};
  99. ctx.OpStore(element_info.id, value);
  100. element += num;
  101. }
  102. }
  103. }
  104. if (ctx.stage == Stage::VertexB || ctx.stage == Stage::Geometry) {
  105. SetFixedPipelinePointSize(ctx);
  106. }
  107. }
  108. void EmitEpilogue(EmitContext& ctx) {
  109. if (ctx.stage == Stage::VertexB && ctx.runtime_info.convert_depth_mode) {
  110. ConvertDepthMode(ctx);
  111. }
  112. if (ctx.stage == Stage::Fragment) {
  113. AlphaTest(ctx);
  114. }
  115. }
  116. void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) {
  117. if (ctx.runtime_info.convert_depth_mode) {
  118. ConvertDepthMode(ctx);
  119. }
  120. if (stream.IsImmediate()) {
  121. ctx.OpEmitStreamVertex(ctx.Def(stream));
  122. } else {
  123. LOG_WARNING(Shader_SPIRV, "Stream is not immediate");
  124. ctx.OpEmitStreamVertex(ctx.u32_zero_value);
  125. }
  126. // Restore fixed pipeline point size after emitting the vertex
  127. SetFixedPipelinePointSize(ctx);
  128. }
  129. void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) {
  130. if (stream.IsImmediate()) {
  131. ctx.OpEndStreamPrimitive(ctx.Def(stream));
  132. } else {
  133. LOG_WARNING(Shader_SPIRV, "Stream is not immediate");
  134. ctx.OpEndStreamPrimitive(ctx.u32_zero_value);
  135. }
  136. }
  137. } // namespace Shader::Backend::SPIRV