Guest User

Untitled

a guest
Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. spv::Function* SpirvShaderTranslator::CreateCubeFunction() {
  2. auto& b = *builder_;
  3. spv::Block* function_block = nullptr;
  4. auto function = b.makeFunctionEntry(spv::NoPrecision, vec4_float_type_,
  5. "cube", {vec4_float_type_},
  6. {{spv::NoPrecision}}, &function_block);
  7.  
  8. auto src = function->getParamId(0);
  9. // The source parameter is ordered as .yxzz.
  10. const unsigned int src_i_x = 1, src_i_y = 0, src_i_z = 2;
  11. auto t_s_ma = b.createVariable(spv::StorageClass::StorageClassFunction,
  12. vec4_float_type_, "t_s_ma");
  13. // T scale, S scale, 2, face index.
  14. auto scale = b.createVariable(spv::StorageClass::StorageClassFunction,
  15. vec4_float_type_, "scale");
  16.  
  17. // Pseudocode:
  18. // vec3 src = param.yxz;
  19. // float t, s, ma, face;
  20. // if (abs(src.x) >= abs(src.y) && abs(src.x) >= abs(src.z)) {
  21. // t = -src.y;
  22. // s = (src.x >= 0.0 ? -src.z : src.z);
  23. // ma = src.x;
  24. // face = (src.x >= 0.0 ? 0.0 : 1.0);
  25. // } else if (abs(src.y) >= abs(src.z)) {
  26. // t = (src.y >= 0.0 ? src.z : -src.z);
  27. // s = src.x;
  28. // ma = src.y;
  29. // face = (src.y >= 0.0 ? 2.0 : 3.0);
  30. // } else {
  31. // t = -src.y;
  32. // s = (src.z >= 0.0 ? src.x : -src.x);
  33. // ma = src.z;
  34. // face = (src.z >= 0.0 ? 4.0 : 5.0);
  35. // }
  36. // return vec4(t - 2.0 * abs(ma), s - 2.0 * abs(ma), 2.0 * ma, face);
  37.  
  38. auto abs_src = CreateGlslStd450InstructionCall(
  39. spv::NoPrecision, vec4_float_type_, spv::GLSLstd450::kFAbs, {src});
  40. auto abs_src_x = b.createCompositeExtract(abs_src, float_type_, src_i_x);
  41. auto abs_src_y = b.createCompositeExtract(abs_src, float_type_, src_i_y);
  42. auto abs_src_z = b.createCompositeExtract(abs_src, float_type_, src_i_z);
  43.  
  44. auto x_gt_y = b.createBinOp(spv::Op::OpFOrdGreaterThanEqual, bool_type_,
  45. abs_src_x, abs_src_y);
  46. auto x_gt_z = b.createBinOp(spv::Op::OpFOrdGreaterThanEqual, bool_type_,
  47. abs_src_x, abs_src_z);
  48. auto x_gt_yz = b.createBinOp(spv::Op::OpLogicalAnd, bool_type_,
  49. x_gt_y, x_gt_z);
  50. spv::Builder::If if_x_else_yz(x_gt_yz, 0, b);
  51. {
  52. // X is the major axis: +T = -Y, +S = -+Z.
  53. auto t_s_ma_x = b.createOp(spv::Op::OpVectorShuffle, vec4_float_type_,
  54. {src, src, src_i_y, src_i_z, src_i_x, 0});
  55. b.createStore(t_s_ma_x, t_s_ma);
  56. auto scale_pos_x = b.makeCompositeConstant(vec4_float_type_,
  57. {b.makeFloatConstant(-1.0f),
  58. b.makeFloatConstant(-1.0f),
  59. b.makeFloatConstant(2.0f),
  60. b.makeFloatConstant(0.0f)});
  61. auto scale_neg_x = b.makeCompositeConstant(vec4_float_type_,
  62. {b.makeFloatConstant(-1.0f),
  63. b.makeFloatConstant(1.0f),
  64. b.makeFloatConstant(2.0f),
  65. b.makeFloatConstant(1.0f)});
  66. auto ma_x = b.createCompositeExtract(t_s_ma_x, float_type_, 2);
  67. auto ma_pos_x = b.createBinOp(spv::Op::OpFOrdGreaterThanEqual, bool_type_,
  68. ma_x, b.makeFloatConstant(0.0f));
  69. auto scale_x = b.createTriOp(spv::Op::OpSelect, vec4_float_type_, ma_pos_x,
  70. scale_pos_x, scale_neg_x);
  71. b.createStore(scale_x, scale);
  72. }
  73. if_x_else_yz.makeBeginElse();
  74. {
  75. auto y_gt_z = b.createBinOp(spv::Op::OpFOrdGreaterThanEqual, bool_type_,
  76. abs_src_y, abs_src_z);
  77. spv::Builder::If if_y_else_z(y_gt_z, 0, b);
  78. {
  79. // Y is the major axis: +T = +-Z, +S = +X.
  80. auto t_s_ma_y = b.createOp(spv::Op::OpVectorShuffle, vec4_float_type_,
  81. {src, src, src_i_z, src_i_x, src_i_y, 0});
  82. b.createStore(t_s_ma_y, t_s_ma);
  83. auto scale_pos_y = b.makeCompositeConstant(vec4_float_type_,
  84. {b.makeFloatConstant(1.0f),
  85. b.makeFloatConstant(1.0f),
  86. b.makeFloatConstant(2.0f),
  87. b.makeFloatConstant(2.0f)});
  88. auto scale_neg_y = b.makeCompositeConstant(vec4_float_type_,
  89. {b.makeFloatConstant(-1.0f),
  90. b.makeFloatConstant(1.0f),
  91. b.makeFloatConstant(2.0f),
  92. b.makeFloatConstant(3.0f)});
  93. auto ma_y = b.createCompositeExtract(t_s_ma_y, float_type_, 2);
  94. auto ma_pos_y = b.createBinOp(spv::Op::OpFOrdGreaterThanEqual, bool_type_,
  95. ma_y, b.makeFloatConstant(0.0f));
  96. auto scale_y = b.createTriOp(spv::Op::OpSelect, vec4_float_type_,
  97. ma_pos_y, scale_pos_y, scale_neg_y);
  98. b.createStore(scale_y, scale);
  99. }
  100. if_y_else_z.makeBeginElse();
  101. {
  102. // Z is the major axis: +T = -Y, +S = +-X.
  103. auto t_s_ma_z = b.createOp(spv::Op::OpVectorShuffle, vec4_float_type_,
  104. {src, src, src_i_y, src_i_x, src_i_z, 0});
  105. b.createStore(t_s_ma_z, t_s_ma);
  106. auto scale_pos_z = b.makeCompositeConstant(vec4_float_type_,
  107. {b.makeFloatConstant(-1.0f),
  108. b.makeFloatConstant(1.0f),
  109. b.makeFloatConstant(2.0f),
  110. b.makeFloatConstant(4.0f)});
  111. auto scale_neg_z = b.makeCompositeConstant(vec4_float_type_,
  112. {b.makeFloatConstant(-1.0f),
  113. b.makeFloatConstant(-1.0f),
  114. b.makeFloatConstant(2.0f),
  115. b.makeFloatConstant(5.0f)});
  116. auto ma_z = b.createCompositeExtract(t_s_ma_z, float_type_, 2);
  117. auto ma_pos_z = b.createBinOp(spv::Op::OpFOrdGreaterThanEqual, bool_type_,
  118. ma_z, b.makeFloatConstant(0.0f));
  119. auto scale_z = b.createTriOp(spv::Op::OpSelect, vec4_float_type_,
  120. ma_pos_z, scale_pos_z, scale_neg_z);
  121. b.createStore(scale_z, scale);
  122. }
  123. if_y_else_z.makeEndIf();
  124. }
  125. if_x_else_yz.makeEndIf();
  126.  
  127. auto ret = b.createLoad(t_s_ma);
  128. // Set face index scale to 1 because it's going to be multiplied.
  129. ret = b.createCompositeInsert(b.makeFloatConstant(1.0f), ret,
  130. vec4_float_type_, 3);
  131. // Apply face-specific signs, face index and multiply major axis by 2.
  132. ret = b.createBinOp(spv::Op::OpFMul, vec4_float_type_, ret,
  133. b.createLoad(scale));
  134. // Subtract 2 * MA from T and S so games can move them to 0...1.
  135. auto abs_2ma = b.createCompositeExtract(ret, float_type_, 2);
  136. abs_2ma = CreateGlslStd450InstructionCall(
  137. spv::NoPrecision, float_type_, spv::GLSLstd450::kFAbs, {abs_2ma});
  138. auto temp_t = b.createCompositeExtract(ret, float_type_, 0);
  139. temp_t = b.createBinOp(spv::Op::OpFSub, float_type_, temp_t, abs_2ma);
  140. ret = b.createCompositeInsert(temp_t, ret, vec4_float_type_, 0);
  141. auto temp_s = b.createCompositeExtract(ret, float_type_, 1);
  142. temp_s = b.createBinOp(spv::Op::OpFSub, float_type_, temp_s, abs_2ma);
  143. ret = b.createCompositeInsert(temp_s, ret, vec4_float_type_, 1);
  144.  
  145. b.makeReturn(false, ret);
  146. return function;
  147. }
Add Comment
Please, Sign In to add comment