val_code_generator.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2019 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "test/val/val_code_generator.h"
  15. #include <sstream>
  16. namespace spvtools {
  17. namespace val {
  18. namespace {
  19. std::string GetDefaultShaderCapabilities() {
  20. return R"(
  21. OpCapability Shader
  22. OpCapability Geometry
  23. OpCapability Tessellation
  24. OpCapability Float64
  25. OpCapability Int64
  26. OpCapability MultiViewport
  27. OpCapability SampleRateShading
  28. )";
  29. }
  30. std::string GetDefaultShaderTypes() {
  31. return R"(
  32. %void = OpTypeVoid
  33. %func = OpTypeFunction %void
  34. %bool = OpTypeBool
  35. %f32 = OpTypeFloat 32
  36. %f64 = OpTypeFloat 64
  37. %i32 = OpTypeInt 32 1
  38. %i64 = OpTypeInt 64 1
  39. %u32 = OpTypeInt 32 0
  40. %u64 = OpTypeInt 64 0
  41. %f32vec2 = OpTypeVector %f32 2
  42. %f32vec3 = OpTypeVector %f32 3
  43. %f32vec4 = OpTypeVector %f32 4
  44. %f64vec2 = OpTypeVector %f64 2
  45. %f64vec3 = OpTypeVector %f64 3
  46. %f64vec4 = OpTypeVector %f64 4
  47. %u32vec2 = OpTypeVector %u32 2
  48. %u32vec3 = OpTypeVector %u32 3
  49. %u64vec3 = OpTypeVector %u64 3
  50. %u32vec4 = OpTypeVector %u32 4
  51. %u64vec2 = OpTypeVector %u64 2
  52. %f32_0 = OpConstant %f32 0
  53. %f32_1 = OpConstant %f32 1
  54. %f32_2 = OpConstant %f32 2
  55. %f32_3 = OpConstant %f32 3
  56. %f32_4 = OpConstant %f32 4
  57. %f32_h = OpConstant %f32 0.5
  58. %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
  59. %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
  60. %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
  61. %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
  62. %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
  63. %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
  64. %f64_0 = OpConstant %f64 0
  65. %f64_1 = OpConstant %f64 1
  66. %f64_2 = OpConstant %f64 2
  67. %f64_3 = OpConstant %f64 3
  68. %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
  69. %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
  70. %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
  71. %u32_0 = OpConstant %u32 0
  72. %u32_1 = OpConstant %u32 1
  73. %u32_2 = OpConstant %u32 2
  74. %u32_3 = OpConstant %u32 3
  75. %u32_4 = OpConstant %u32 4
  76. %u64_0 = OpConstant %u64 0
  77. %u64_1 = OpConstant %u64 1
  78. %u64_2 = OpConstant %u64 2
  79. %u64_3 = OpConstant %u64 3
  80. %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
  81. %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
  82. %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
  83. %u64vec2_01 = OpConstantComposite %u64vec2 %u64_0 %u64_1
  84. %u32arr2 = OpTypeArray %u32 %u32_2
  85. %u32arr3 = OpTypeArray %u32 %u32_3
  86. %u32arr4 = OpTypeArray %u32 %u32_4
  87. %u64arr2 = OpTypeArray %u64 %u32_2
  88. %u64arr3 = OpTypeArray %u64 %u32_3
  89. %u64arr4 = OpTypeArray %u64 %u32_4
  90. %f32arr2 = OpTypeArray %f32 %u32_2
  91. %f32arr3 = OpTypeArray %f32 %u32_3
  92. %f32arr4 = OpTypeArray %f32 %u32_4
  93. %f64arr2 = OpTypeArray %f64 %u32_2
  94. %f64arr3 = OpTypeArray %f64 %u32_3
  95. %f64arr4 = OpTypeArray %f64 %u32_4
  96. %f32vec3arr3 = OpTypeArray %f32vec3 %u32_3
  97. %f32vec4arr3 = OpTypeArray %f32vec4 %u32_3
  98. %f64vec4arr3 = OpTypeArray %f64vec4 %u32_3
  99. %f32mat22 = OpTypeMatrix %f32vec2 2
  100. %f32mat23 = OpTypeMatrix %f32vec2 3
  101. %f32mat32 = OpTypeMatrix %f32vec3 2
  102. %f32mat33 = OpTypeMatrix %f32vec3 3
  103. %f64mat22 = OpTypeMatrix %f64vec2 2
  104. %f32mat34 = OpTypeMatrix %f32vec3 4
  105. %f32mat43 = OpTypeMatrix %f32vec4 3
  106. %f32mat44 = OpTypeMatrix %f32vec4 4
  107. )";
  108. }
  109. } // namespace
  110. CodeGenerator CodeGenerator::GetDefaultShaderCodeGenerator() {
  111. CodeGenerator generator;
  112. generator.capabilities_ = GetDefaultShaderCapabilities();
  113. generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
  114. generator.types_ = GetDefaultShaderTypes();
  115. return generator;
  116. }
  117. std::string CodeGenerator::Build() const {
  118. std::ostringstream ss;
  119. ss << capabilities_;
  120. ss << extensions_;
  121. ss << memory_model_;
  122. for (const EntryPoint& entry_point : entry_points_) {
  123. ss << "OpEntryPoint " << entry_point.execution_model << " %"
  124. << entry_point.name << " \"" << entry_point.name << "\" "
  125. << entry_point.interfaces << "\n";
  126. }
  127. for (const EntryPoint& entry_point : entry_points_) {
  128. ss << entry_point.execution_modes << "\n";
  129. }
  130. ss << before_types_;
  131. ss << types_;
  132. ss << after_types_;
  133. for (const EntryPoint& entry_point : entry_points_) {
  134. ss << "\n";
  135. ss << "%" << entry_point.name << " = OpFunction %void None %func\n";
  136. ss << "%" << entry_point.name << "_entry = OpLabel\n";
  137. ss << entry_point.body;
  138. ss << "\nOpReturn\nOpFunctionEnd\n";
  139. }
  140. ss << add_at_the_end_;
  141. return ss.str();
  142. }
  143. } // namespace val
  144. } // namespace spvtools