| 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 |
|
| 15 |
#include "test/val/val_code_generator.h" |
| 16 |
|
| 17 |
#include <sstream> |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace val { |
| 21 |
namespace { |
| 22 |
|
| 23 |
std::string GetDefaultShaderCapabilities() { |
| 24 |
return R"( |
| 25 |
OpCapability Shader |
| 26 |
OpCapability Geometry |
| 27 |
OpCapability Tessellation |
| 28 |
OpCapability Float64 |
| 29 |
OpCapability Int64 |
| 30 |
OpCapability MultiViewport |
| 31 |
OpCapability SampleRateShading |
| 32 |
)"; |
| 33 |
} |
| 34 |
|
| 35 |
std::string GetWebGPUShaderCapabilities() { |
| 36 |
return R"( |
| 37 |
OpCapability Shader |
| 38 |
OpCapability VulkanMemoryModelKHR |
| 39 |
)"; |
| 40 |
} |
| 41 |
|
| 42 |
std::string GetDefaultShaderTypes() { |
| 43 |
return R"( |
| 44 |
%void = OpTypeVoid |
| 45 |
%func = OpTypeFunction %void |
| 46 |
%bool = OpTypeBool |
| 47 |
%f32 = OpTypeFloat 32 |
| 48 |
%f64 = OpTypeFloat 64 |
| 49 |
%i32 = OpTypeInt 32 1 |
| 50 |
%i64 = OpTypeInt 64 1 |
| 51 |
%u32 = OpTypeInt 32 0 |
| 52 |
%u64 = OpTypeInt 64 0 |
| 53 |
%f32vec2 = OpTypeVector %f32 2 |
| 54 |
%f32vec3 = OpTypeVector %f32 3 |
| 55 |
%f32vec4 = OpTypeVector %f32 4 |
| 56 |
%f64vec2 = OpTypeVector %f64 2 |
| 57 |
%f64vec3 = OpTypeVector %f64 3 |
| 58 |
%f64vec4 = OpTypeVector %f64 4 |
| 59 |
%u32vec2 = OpTypeVector %u32 2 |
| 60 |
%u32vec3 = OpTypeVector %u32 3 |
| 61 |
%u64vec3 = OpTypeVector %u64 3 |
| 62 |
%u32vec4 = OpTypeVector %u32 4 |
| 63 |
%u64vec2 = OpTypeVector %u64 2 |
| 64 |
|
| 65 |
%f32_0 = OpConstant %f32 0 |
| 66 |
%f32_1 = OpConstant %f32 1 |
| 67 |
%f32_2 = OpConstant %f32 2 |
| 68 |
%f32_3 = OpConstant %f32 3 |
| 69 |
%f32_4 = OpConstant %f32 4 |
| 70 |
%f32_h = OpConstant %f32 0.5 |
| 71 |
%f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1 |
| 72 |
%f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2 |
| 73 |
%f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2 |
| 74 |
%f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3 |
| 75 |
%f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3 |
| 76 |
%f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4 |
| 77 |
|
| 78 |
%f64_0 = OpConstant %f64 0 |
| 79 |
%f64_1 = OpConstant %f64 1 |
| 80 |
%f64_2 = OpConstant %f64 2 |
| 81 |
%f64_3 = OpConstant %f64 3 |
| 82 |
%f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1 |
| 83 |
%f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2 |
| 84 |
%f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3 |
| 85 |
|
| 86 |
%u32_0 = OpConstant %u32 0 |
| 87 |
%u32_1 = OpConstant %u32 1 |
| 88 |
%u32_2 = OpConstant %u32 2 |
| 89 |
%u32_3 = OpConstant %u32 3 |
| 90 |
%u32_4 = OpConstant %u32 4 |
| 91 |
|
| 92 |
%u64_0 = OpConstant %u64 0 |
| 93 |
%u64_1 = OpConstant %u64 1 |
| 94 |
%u64_2 = OpConstant %u64 2 |
| 95 |
%u64_3 = OpConstant %u64 3 |
| 96 |
|
| 97 |
%u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1 |
| 98 |
%u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2 |
| 99 |
%u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3 |
| 100 |
%u64vec2_01 = OpConstantComposite %u64vec2 %u64_0 %u64_1 |
| 101 |
|
| 102 |
%u32arr2 = OpTypeArray %u32 %u32_2 |
| 103 |
%u32arr3 = OpTypeArray %u32 %u32_3 |
| 104 |
%u32arr4 = OpTypeArray %u32 %u32_4 |
| 105 |
%u64arr2 = OpTypeArray %u64 %u32_2 |
| 106 |
%u64arr3 = OpTypeArray %u64 %u32_3 |
| 107 |
%u64arr4 = OpTypeArray %u64 %u32_4 |
| 108 |
%f32arr2 = OpTypeArray %f32 %u32_2 |
| 109 |
%f32arr3 = OpTypeArray %f32 %u32_3 |
| 110 |
%f32arr4 = OpTypeArray %f32 %u32_4 |
| 111 |
%f64arr2 = OpTypeArray %f64 %u32_2 |
| 112 |
%f64arr3 = OpTypeArray %f64 %u32_3 |
| 113 |
%f64arr4 = OpTypeArray %f64 %u32_4 |
| 114 |
|
| 115 |
%f32vec3arr3 = OpTypeArray %f32vec3 %u32_3 |
| 116 |
%f32vec4arr3 = OpTypeArray %f32vec4 %u32_3 |
| 117 |
%f64vec4arr3 = OpTypeArray %f64vec4 %u32_3 |
| 118 |
)"; |
| 119 |
} |
| 120 |
|
| 121 |
std::string GetWebGPUShaderTypes() { |
| 122 |
return R"( |
| 123 |
%void = OpTypeVoid |
| 124 |
%func = OpTypeFunction %void |
| 125 |
%bool = OpTypeBool |
| 126 |
%f32 = OpTypeFloat 32 |
| 127 |
%u32 = OpTypeInt 32 0 |
| 128 |
%f32vec2 = OpTypeVector %f32 2 |
| 129 |
%f32vec3 = OpTypeVector %f32 3 |
| 130 |
%f32vec4 = OpTypeVector %f32 4 |
| 131 |
%u32vec2 = OpTypeVector %u32 2 |
| 132 |
%u32vec3 = OpTypeVector %u32 3 |
| 133 |
%u32vec4 = OpTypeVector %u32 4 |
| 134 |
|
| 135 |
%f32_0 = OpConstant %f32 0 |
| 136 |
%f32_1 = OpConstant %f32 1 |
| 137 |
%f32_2 = OpConstant %f32 2 |
| 138 |
%f32_3 = OpConstant %f32 3 |
| 139 |
%f32_4 = OpConstant %f32 4 |
| 140 |
%f32_h = OpConstant %f32 0.5 |
| 141 |
%f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1 |
| 142 |
%f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2 |
| 143 |
%f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2 |
| 144 |
%f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3 |
| 145 |
%f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3 |
| 146 |
%f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4 |
| 147 |
|
| 148 |
%u32_0 = OpConstant %u32 0 |
| 149 |
%u32_1 = OpConstant %u32 1 |
| 150 |
%u32_2 = OpConstant %u32 2 |
| 151 |
%u32_3 = OpConstant %u32 3 |
| 152 |
%u32_4 = OpConstant %u32 4 |
| 153 |
|
| 154 |
%u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1 |
| 155 |
%u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2 |
| 156 |
%u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3 |
| 157 |
|
| 158 |
%u32arr2 = OpTypeArray %u32 %u32_2 |
| 159 |
%u32arr3 = OpTypeArray %u32 %u32_3 |
| 160 |
%u32arr4 = OpTypeArray %u32 %u32_4 |
| 161 |
%f32arr2 = OpTypeArray %f32 %u32_2 |
| 162 |
%f32arr3 = OpTypeArray %f32 %u32_3 |
| 163 |
%f32arr4 = OpTypeArray %f32 %u32_4 |
| 164 |
|
| 165 |
%f32vec3arr3 = OpTypeArray %f32vec3 %u32_3 |
| 166 |
%f32vec4arr3 = OpTypeArray %f32vec4 %u32_3 |
| 167 |
)"; |
| 168 |
} |
| 169 |
|
| 170 |
} // namespace |
| 171 |
|
| 172 |
CodeGenerator CodeGenerator::GetDefaultShaderCodeGenerator() { |
| 173 |
CodeGenerator generator; |
| 174 |
generator.capabilities_ = GetDefaultShaderCapabilities(); |
| 175 |
generator.memory_model_ = "OpMemoryModel Logical GLSL450\n"; |
| 176 |
generator.types_ = GetDefaultShaderTypes(); |
| 177 |
return generator; |
| 178 |
} |
| 179 |
|
| 180 |
CodeGenerator CodeGenerator::GetWebGPUShaderCodeGenerator() { |
| 181 |
CodeGenerator generator; |
| 182 |
generator.capabilities_ = GetWebGPUShaderCapabilities(); |
| 183 |
generator.memory_model_ = "OpMemoryModel Logical VulkanKHR\n"; |
| 184 |
generator.extensions_ = "OpExtension \"SPV_KHR_vulkan_memory_model\"\n"; |
| 185 |
generator.types_ = GetWebGPUShaderTypes(); |
| 186 |
return generator; |
| 187 |
} |
| 188 |
|
| 189 |
std::string CodeGenerator::Build() const { |
| 190 |
std::ostringstream ss; |
| 191 |
|
| 192 |
ss << capabilities_; |
| 193 |
ss << extensions_; |
| 194 |
ss << memory_model_; |
| 195 |
|
| 196 |
for (const EntryPoint& entry_point : entry_points_) { |
| 197 |
ss << "OpEntryPoint " << entry_point.execution_model << " %" |
| 198 |
<< entry_point.name << " \"" << entry_point.name << "\" " |
| 199 |
<< entry_point.interfaces << "\n"; |
| 200 |
} |
| 201 |
|
| 202 |
for (const EntryPoint& entry_point : entry_points_) { |
| 203 |
ss << entry_point.execution_modes << "\n"; |
| 204 |
} |
| 205 |
|
| 206 |
ss << before_types_; |
| 207 |
ss << types_; |
| 208 |
ss << after_types_; |
| 209 |
|
| 210 |
for (const EntryPoint& entry_point : entry_points_) { |
| 211 |
ss << "\n"; |
| 212 |
ss << "%" << entry_point.name << " = OpFunction %void None %func\n"; |
| 213 |
ss << "%" << entry_point.name << "_entry = OpLabel\n"; |
| 214 |
ss << entry_point.body; |
| 215 |
ss << "\nOpReturn\nOpFunctionEnd\n"; |
| 216 |
} |
| 217 |
|
| 218 |
ss << add_at_the_end_; |
| 219 |
|
| 220 |
return ss.str(); |
| 221 |
} |
| 222 |
|
| 223 |
} // namespace val |
| 224 |
} // namespace spvtools |