emit_spirv_special.cpp 5.2 KB

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