mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2025-01-11 09:00:06 +00:00
Move CodeGenerator to be accessible by other validation tests (#2343)
Fixes #2342
This commit is contained in:
parent
cf78346230
commit
5d6b4c4b1b
@ -15,6 +15,8 @@
|
||||
set(VAL_TEST_COMMON_SRCS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../test_fixture.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../unit_spirv.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/val_code_generator.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/val_code_generator.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/val_fixtures.h
|
||||
)
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "gmock/gmock.h"
|
||||
#include "source/spirv_target_env.h"
|
||||
#include "test/unit_spirv.h"
|
||||
#include "test/val/val_code_generator.h"
|
||||
#include "test/val/val_fixtures.h"
|
||||
|
||||
namespace spvtools {
|
||||
@ -62,223 +63,6 @@ using ValidateVulkanCombineBuiltInArrayedVariable = spvtest::ValidateBase<
|
||||
using ValidateWebGPUCombineBuiltInArrayedVariable = spvtest::ValidateBase<
|
||||
std::tuple<const char*, const char*, const char*, const char*, TestResult>>;
|
||||
|
||||
struct EntryPoint {
|
||||
std::string name;
|
||||
std::string execution_model;
|
||||
std::string execution_modes;
|
||||
std::string body;
|
||||
std::string interfaces;
|
||||
};
|
||||
|
||||
class CodeGenerator {
|
||||
public:
|
||||
std::string Build() const;
|
||||
|
||||
std::vector<EntryPoint> entry_points_;
|
||||
std::string capabilities_;
|
||||
std::string extensions_;
|
||||
std::string memory_model_;
|
||||
std::string before_types_;
|
||||
std::string types_;
|
||||
std::string after_types_;
|
||||
std::string add_at_the_end_;
|
||||
};
|
||||
|
||||
std::string CodeGenerator::Build() const {
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << capabilities_;
|
||||
ss << extensions_;
|
||||
ss << memory_model_;
|
||||
|
||||
for (const EntryPoint& entry_point : entry_points_) {
|
||||
ss << "OpEntryPoint " << entry_point.execution_model << " %"
|
||||
<< entry_point.name << " \"" << entry_point.name << "\" "
|
||||
<< entry_point.interfaces << "\n";
|
||||
}
|
||||
|
||||
for (const EntryPoint& entry_point : entry_points_) {
|
||||
ss << entry_point.execution_modes << "\n";
|
||||
}
|
||||
|
||||
ss << before_types_;
|
||||
ss << types_;
|
||||
ss << after_types_;
|
||||
|
||||
for (const EntryPoint& entry_point : entry_points_) {
|
||||
ss << "\n";
|
||||
ss << "%" << entry_point.name << " = OpFunction %void None %func\n";
|
||||
ss << "%" << entry_point.name << "_entry = OpLabel\n";
|
||||
ss << entry_point.body;
|
||||
ss << "\nOpReturn\nOpFunctionEnd\n";
|
||||
}
|
||||
|
||||
ss << add_at_the_end_;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string GetDefaultShaderCapabilities() {
|
||||
return R"(
|
||||
OpCapability Shader
|
||||
OpCapability Geometry
|
||||
OpCapability Tessellation
|
||||
OpCapability Float64
|
||||
OpCapability Int64
|
||||
OpCapability MultiViewport
|
||||
OpCapability SampleRateShading
|
||||
)";
|
||||
}
|
||||
|
||||
std::string GetWebGPUShaderCapabilities() {
|
||||
return R"(
|
||||
OpCapability Shader
|
||||
OpCapability VulkanMemoryModelKHR
|
||||
)";
|
||||
}
|
||||
|
||||
std::string GetDefaultShaderTypes() {
|
||||
return R"(
|
||||
%void = OpTypeVoid
|
||||
%func = OpTypeFunction %void
|
||||
%bool = OpTypeBool
|
||||
%f32 = OpTypeFloat 32
|
||||
%f64 = OpTypeFloat 64
|
||||
%u32 = OpTypeInt 32 0
|
||||
%u64 = OpTypeInt 64 0
|
||||
%f32vec2 = OpTypeVector %f32 2
|
||||
%f32vec3 = OpTypeVector %f32 3
|
||||
%f32vec4 = OpTypeVector %f32 4
|
||||
%f64vec2 = OpTypeVector %f64 2
|
||||
%f64vec3 = OpTypeVector %f64 3
|
||||
%f64vec4 = OpTypeVector %f64 4
|
||||
%u32vec2 = OpTypeVector %u32 2
|
||||
%u32vec3 = OpTypeVector %u32 3
|
||||
%u64vec3 = OpTypeVector %u64 3
|
||||
%u32vec4 = OpTypeVector %u32 4
|
||||
%u64vec2 = OpTypeVector %u64 2
|
||||
|
||||
%f32_0 = OpConstant %f32 0
|
||||
%f32_1 = OpConstant %f32 1
|
||||
%f32_2 = OpConstant %f32 2
|
||||
%f32_3 = OpConstant %f32 3
|
||||
%f32_4 = OpConstant %f32 4
|
||||
%f32_h = OpConstant %f32 0.5
|
||||
%f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
|
||||
%f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
|
||||
%f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
|
||||
%f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
|
||||
|
||||
%f64_0 = OpConstant %f64 0
|
||||
%f64_1 = OpConstant %f64 1
|
||||
%f64_2 = OpConstant %f64 2
|
||||
%f64_3 = OpConstant %f64 3
|
||||
%f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
|
||||
%f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
|
||||
%f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
|
||||
|
||||
%u32_0 = OpConstant %u32 0
|
||||
%u32_1 = OpConstant %u32 1
|
||||
%u32_2 = OpConstant %u32 2
|
||||
%u32_3 = OpConstant %u32 3
|
||||
%u32_4 = OpConstant %u32 4
|
||||
|
||||
%u64_0 = OpConstant %u64 0
|
||||
%u64_1 = OpConstant %u64 1
|
||||
%u64_2 = OpConstant %u64 2
|
||||
%u64_3 = OpConstant %u64 3
|
||||
|
||||
%u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
|
||||
%u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
|
||||
%u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
|
||||
%u64vec2_01 = OpConstantComposite %u64vec2 %u64_0 %u64_1
|
||||
|
||||
%u32arr2 = OpTypeArray %u32 %u32_2
|
||||
%u32arr3 = OpTypeArray %u32 %u32_3
|
||||
%u32arr4 = OpTypeArray %u32 %u32_4
|
||||
%u64arr2 = OpTypeArray %u64 %u32_2
|
||||
%u64arr3 = OpTypeArray %u64 %u32_3
|
||||
%u64arr4 = OpTypeArray %u64 %u32_4
|
||||
%f32arr2 = OpTypeArray %f32 %u32_2
|
||||
%f32arr3 = OpTypeArray %f32 %u32_3
|
||||
%f32arr4 = OpTypeArray %f32 %u32_4
|
||||
%f64arr2 = OpTypeArray %f64 %u32_2
|
||||
%f64arr3 = OpTypeArray %f64 %u32_3
|
||||
%f64arr4 = OpTypeArray %f64 %u32_4
|
||||
|
||||
%f32vec3arr3 = OpTypeArray %f32vec3 %u32_3
|
||||
%f32vec4arr3 = OpTypeArray %f32vec4 %u32_3
|
||||
%f64vec4arr3 = OpTypeArray %f64vec4 %u32_3
|
||||
)";
|
||||
}
|
||||
|
||||
std::string GetWebGPUShaderTypes() {
|
||||
return R"(
|
||||
%void = OpTypeVoid
|
||||
%func = OpTypeFunction %void
|
||||
%bool = OpTypeBool
|
||||
%f32 = OpTypeFloat 32
|
||||
%u32 = OpTypeInt 32 0
|
||||
%f32vec2 = OpTypeVector %f32 2
|
||||
%f32vec3 = OpTypeVector %f32 3
|
||||
%f32vec4 = OpTypeVector %f32 4
|
||||
%u32vec2 = OpTypeVector %u32 2
|
||||
%u32vec3 = OpTypeVector %u32 3
|
||||
%u32vec4 = OpTypeVector %u32 4
|
||||
|
||||
%f32_0 = OpConstant %f32 0
|
||||
%f32_1 = OpConstant %f32 1
|
||||
%f32_2 = OpConstant %f32 2
|
||||
%f32_3 = OpConstant %f32 3
|
||||
%f32_4 = OpConstant %f32 4
|
||||
%f32_h = OpConstant %f32 0.5
|
||||
%f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
|
||||
%f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
|
||||
%f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
|
||||
%f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
|
||||
|
||||
%u32_0 = OpConstant %u32 0
|
||||
%u32_1 = OpConstant %u32 1
|
||||
%u32_2 = OpConstant %u32 2
|
||||
%u32_3 = OpConstant %u32 3
|
||||
%u32_4 = OpConstant %u32 4
|
||||
|
||||
%u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
|
||||
%u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
|
||||
%u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
|
||||
|
||||
%u32arr2 = OpTypeArray %u32 %u32_2
|
||||
%u32arr3 = OpTypeArray %u32 %u32_3
|
||||
%u32arr4 = OpTypeArray %u32 %u32_4
|
||||
%f32arr2 = OpTypeArray %f32 %u32_2
|
||||
%f32arr3 = OpTypeArray %f32 %u32_3
|
||||
%f32arr4 = OpTypeArray %f32 %u32_4
|
||||
|
||||
%f32vec3arr3 = OpTypeArray %f32vec3 %u32_3
|
||||
%f32vec4arr3 = OpTypeArray %f32vec4 %u32_3
|
||||
)";
|
||||
}
|
||||
|
||||
CodeGenerator GetDefaultShaderCodeGenerator() {
|
||||
CodeGenerator generator;
|
||||
generator.capabilities_ = GetDefaultShaderCapabilities();
|
||||
generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
|
||||
generator.types_ = GetDefaultShaderTypes();
|
||||
return generator;
|
||||
}
|
||||
|
||||
CodeGenerator GetWebGPUShaderCodeGenerator() {
|
||||
CodeGenerator generator;
|
||||
generator.capabilities_ = GetWebGPUShaderCapabilities();
|
||||
generator.memory_model_ = "OpMemoryModel Logical VulkanKHR\n";
|
||||
generator.extensions_ = "OpExtension \"SPV_KHR_vulkan_memory_model\"\n";
|
||||
generator.types_ = GetWebGPUShaderTypes();
|
||||
return generator;
|
||||
}
|
||||
|
||||
bool InitializerRequired(spv_target_env env, const char* const storage_class) {
|
||||
return spvIsWebGPUEnv(env) && (strncmp(storage_class, "Output", 6) == 0 ||
|
||||
@ -291,9 +75,9 @@ CodeGenerator GetInMainCodeGenerator(spv_target_env env,
|
||||
const char* const execution_model,
|
||||
const char* const storage_class,
|
||||
const char* const data_type) {
|
||||
CodeGenerator generator = spvIsWebGPUEnv(env)
|
||||
? GetWebGPUShaderCodeGenerator()
|
||||
: GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator =
|
||||
spvIsWebGPUEnv(env) ? CodeGenerator::GetWebGPUShaderCodeGenerator()
|
||||
: CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = "OpMemberDecorate %built_in_type 0 BuiltIn ";
|
||||
generator.before_types_ += built_in;
|
||||
@ -400,9 +184,9 @@ CodeGenerator GetInFunctionCodeGenerator(spv_target_env env,
|
||||
const char* const execution_model,
|
||||
const char* const storage_class,
|
||||
const char* const data_type) {
|
||||
CodeGenerator generator = spvIsWebGPUEnv(env)
|
||||
? GetWebGPUShaderCodeGenerator()
|
||||
: GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator =
|
||||
spvIsWebGPUEnv(env) ? CodeGenerator::GetWebGPUShaderCodeGenerator()
|
||||
: CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = "OpMemberDecorate %built_in_type 0 BuiltIn ";
|
||||
generator.before_types_ += built_in;
|
||||
@ -523,9 +307,9 @@ CodeGenerator GetVariableCodeGenerator(spv_target_env env,
|
||||
const char* const execution_model,
|
||||
const char* const storage_class,
|
||||
const char* const data_type) {
|
||||
CodeGenerator generator = spvIsWebGPUEnv(env)
|
||||
? GetWebGPUShaderCodeGenerator()
|
||||
: GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator =
|
||||
spvIsWebGPUEnv(env) ? CodeGenerator::GetWebGPUShaderCodeGenerator()
|
||||
: CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = "OpDecorate %built_in_var BuiltIn ";
|
||||
generator.before_types_ += built_in;
|
||||
@ -1805,9 +1589,9 @@ CodeGenerator GetArrayedVariableCodeGenerator(spv_target_env env,
|
||||
const char* const execution_model,
|
||||
const char* const storage_class,
|
||||
const char* const data_type) {
|
||||
CodeGenerator generator = spvIsWebGPUEnv(env)
|
||||
? GetWebGPUShaderCodeGenerator()
|
||||
: GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator =
|
||||
spvIsWebGPUEnv(env) ? CodeGenerator::GetWebGPUShaderCodeGenerator()
|
||||
: CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = "OpDecorate %built_in_var BuiltIn ";
|
||||
generator.before_types_ += built_in;
|
||||
@ -1984,7 +1768,7 @@ INSTANTIATE_TEST_CASE_P(
|
||||
"components are not float scalar"))), );
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeSuccess) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2006,7 +1790,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeFragment) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2036,7 +1820,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeNotConstant) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %copy BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2061,7 +1845,7 @@ OpDecorate %copy BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeNotVector) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2087,7 +1871,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeNotIntVector) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2113,7 +1897,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeNotVec3) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2139,7 +1923,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizeNotInt32Vec) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2166,7 +1950,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupSizePrivateVar) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
)";
|
||||
@ -2189,7 +1973,7 @@ OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, GeometryPositionInOutSuccess) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = R"(
|
||||
OpMemberDecorate %input_type 0 BuiltIn Position
|
||||
@ -2228,7 +2012,7 @@ OpStore %output_pos %pos
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, WorkgroupIdNotVec3) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %workgroup_size BuiltIn WorkgroupSize
|
||||
OpDecorate %workgroup_id BuiltIn WorkgroupId
|
||||
@ -2259,7 +2043,7 @@ OpDecorate %workgroup_id BuiltIn WorkgroupId
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, TwoBuiltInsFirstFails) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = R"(
|
||||
OpMemberDecorate %input_type 0 BuiltIn FragCoord
|
||||
@ -2299,7 +2083,7 @@ OpStore %output_pos %pos
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, TwoBuiltInsSecondFails) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
|
||||
generator.before_types_ = R"(
|
||||
OpMemberDecorate %input_type 0 BuiltIn Position
|
||||
@ -2339,7 +2123,7 @@ OpStore %output_pos %pos
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, VertexPositionVariableSuccess) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpDecorate %position BuiltIn Position
|
||||
)";
|
||||
@ -2363,7 +2147,7 @@ OpStore %position %f32vec4_0123
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, FragmentPositionTwoEntryPoints) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpMemberDecorate %output_type 0 BuiltIn Position
|
||||
)";
|
||||
@ -2413,7 +2197,7 @@ OpFunctionEnd
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, FragmentFragDepthNoDepthReplacing) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpMemberDecorate %output_type 0 BuiltIn FragDepth
|
||||
)";
|
||||
@ -2452,7 +2236,7 @@ OpFunctionEnd
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, FragmentFragDepthOneMainHasDepthReplacingOtherHasnt) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.before_types_ = R"(
|
||||
OpMemberDecorate %output_type 0 BuiltIn FragDepth
|
||||
)";
|
||||
@ -2502,7 +2286,7 @@ OpFunctionEnd
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, AllowInstanceIdWithIntersectionShader) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.capabilities_ += R"(
|
||||
OpCapability RayTracingNV
|
||||
)";
|
||||
@ -2542,7 +2326,7 @@ OpFunctionEnd
|
||||
}
|
||||
|
||||
TEST_F(ValidateBuiltIns, DisallowInstanceIdWithRayGenShader) {
|
||||
CodeGenerator generator = GetDefaultShaderCodeGenerator();
|
||||
CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
|
||||
generator.capabilities_ += R"(
|
||||
OpCapability RayTracingNV
|
||||
)";
|
||||
|
222
test/val/val_code_generator.cpp
Normal file
222
test/val/val_code_generator.cpp
Normal file
@ -0,0 +1,222 @@
|
||||
// Copyright (c) 2019 Google LLC.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "test/val/val_code_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace spvtools {
|
||||
namespace val {
|
||||
namespace {
|
||||
|
||||
std::string GetDefaultShaderCapabilities() {
|
||||
return R"(
|
||||
OpCapability Shader
|
||||
OpCapability Geometry
|
||||
OpCapability Tessellation
|
||||
OpCapability Float64
|
||||
OpCapability Int64
|
||||
OpCapability MultiViewport
|
||||
OpCapability SampleRateShading
|
||||
)";
|
||||
}
|
||||
|
||||
std::string GetWebGPUShaderCapabilities() {
|
||||
return R"(
|
||||
OpCapability Shader
|
||||
OpCapability VulkanMemoryModelKHR
|
||||
)";
|
||||
}
|
||||
|
||||
std::string GetDefaultShaderTypes() {
|
||||
return R"(
|
||||
%void = OpTypeVoid
|
||||
%func = OpTypeFunction %void
|
||||
%bool = OpTypeBool
|
||||
%f32 = OpTypeFloat 32
|
||||
%f64 = OpTypeFloat 64
|
||||
%u32 = OpTypeInt 32 0
|
||||
%u64 = OpTypeInt 64 0
|
||||
%f32vec2 = OpTypeVector %f32 2
|
||||
%f32vec3 = OpTypeVector %f32 3
|
||||
%f32vec4 = OpTypeVector %f32 4
|
||||
%f64vec2 = OpTypeVector %f64 2
|
||||
%f64vec3 = OpTypeVector %f64 3
|
||||
%f64vec4 = OpTypeVector %f64 4
|
||||
%u32vec2 = OpTypeVector %u32 2
|
||||
%u32vec3 = OpTypeVector %u32 3
|
||||
%u64vec3 = OpTypeVector %u64 3
|
||||
%u32vec4 = OpTypeVector %u32 4
|
||||
%u64vec2 = OpTypeVector %u64 2
|
||||
|
||||
%f32_0 = OpConstant %f32 0
|
||||
%f32_1 = OpConstant %f32 1
|
||||
%f32_2 = OpConstant %f32 2
|
||||
%f32_3 = OpConstant %f32 3
|
||||
%f32_4 = OpConstant %f32 4
|
||||
%f32_h = OpConstant %f32 0.5
|
||||
%f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
|
||||
%f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
|
||||
%f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
|
||||
%f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
|
||||
|
||||
%f64_0 = OpConstant %f64 0
|
||||
%f64_1 = OpConstant %f64 1
|
||||
%f64_2 = OpConstant %f64 2
|
||||
%f64_3 = OpConstant %f64 3
|
||||
%f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
|
||||
%f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
|
||||
%f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
|
||||
|
||||
%u32_0 = OpConstant %u32 0
|
||||
%u32_1 = OpConstant %u32 1
|
||||
%u32_2 = OpConstant %u32 2
|
||||
%u32_3 = OpConstant %u32 3
|
||||
%u32_4 = OpConstant %u32 4
|
||||
|
||||
%u64_0 = OpConstant %u64 0
|
||||
%u64_1 = OpConstant %u64 1
|
||||
%u64_2 = OpConstant %u64 2
|
||||
%u64_3 = OpConstant %u64 3
|
||||
|
||||
%u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
|
||||
%u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
|
||||
%u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
|
||||
%u64vec2_01 = OpConstantComposite %u64vec2 %u64_0 %u64_1
|
||||
|
||||
%u32arr2 = OpTypeArray %u32 %u32_2
|
||||
%u32arr3 = OpTypeArray %u32 %u32_3
|
||||
%u32arr4 = OpTypeArray %u32 %u32_4
|
||||
%u64arr2 = OpTypeArray %u64 %u32_2
|
||||
%u64arr3 = OpTypeArray %u64 %u32_3
|
||||
%u64arr4 = OpTypeArray %u64 %u32_4
|
||||
%f32arr2 = OpTypeArray %f32 %u32_2
|
||||
%f32arr3 = OpTypeArray %f32 %u32_3
|
||||
%f32arr4 = OpTypeArray %f32 %u32_4
|
||||
%f64arr2 = OpTypeArray %f64 %u32_2
|
||||
%f64arr3 = OpTypeArray %f64 %u32_3
|
||||
%f64arr4 = OpTypeArray %f64 %u32_4
|
||||
|
||||
%f32vec3arr3 = OpTypeArray %f32vec3 %u32_3
|
||||
%f32vec4arr3 = OpTypeArray %f32vec4 %u32_3
|
||||
%f64vec4arr3 = OpTypeArray %f64vec4 %u32_3
|
||||
)";
|
||||
}
|
||||
|
||||
std::string GetWebGPUShaderTypes() {
|
||||
return R"(
|
||||
%void = OpTypeVoid
|
||||
%func = OpTypeFunction %void
|
||||
%bool = OpTypeBool
|
||||
%f32 = OpTypeFloat 32
|
||||
%u32 = OpTypeInt 32 0
|
||||
%f32vec2 = OpTypeVector %f32 2
|
||||
%f32vec3 = OpTypeVector %f32 3
|
||||
%f32vec4 = OpTypeVector %f32 4
|
||||
%u32vec2 = OpTypeVector %u32 2
|
||||
%u32vec3 = OpTypeVector %u32 3
|
||||
%u32vec4 = OpTypeVector %u32 4
|
||||
|
||||
%f32_0 = OpConstant %f32 0
|
||||
%f32_1 = OpConstant %f32 1
|
||||
%f32_2 = OpConstant %f32 2
|
||||
%f32_3 = OpConstant %f32 3
|
||||
%f32_4 = OpConstant %f32 4
|
||||
%f32_h = OpConstant %f32 0.5
|
||||
%f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
|
||||
%f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
|
||||
%f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
|
||||
%f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
|
||||
%f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
|
||||
|
||||
%u32_0 = OpConstant %u32 0
|
||||
%u32_1 = OpConstant %u32 1
|
||||
%u32_2 = OpConstant %u32 2
|
||||
%u32_3 = OpConstant %u32 3
|
||||
%u32_4 = OpConstant %u32 4
|
||||
|
||||
%u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
|
||||
%u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
|
||||
%u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
|
||||
|
||||
%u32arr2 = OpTypeArray %u32 %u32_2
|
||||
%u32arr3 = OpTypeArray %u32 %u32_3
|
||||
%u32arr4 = OpTypeArray %u32 %u32_4
|
||||
%f32arr2 = OpTypeArray %f32 %u32_2
|
||||
%f32arr3 = OpTypeArray %f32 %u32_3
|
||||
%f32arr4 = OpTypeArray %f32 %u32_4
|
||||
|
||||
%f32vec3arr3 = OpTypeArray %f32vec3 %u32_3
|
||||
%f32vec4arr3 = OpTypeArray %f32vec4 %u32_3
|
||||
)";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CodeGenerator CodeGenerator::GetDefaultShaderCodeGenerator() {
|
||||
CodeGenerator generator;
|
||||
generator.capabilities_ = GetDefaultShaderCapabilities();
|
||||
generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
|
||||
generator.types_ = GetDefaultShaderTypes();
|
||||
return generator;
|
||||
}
|
||||
|
||||
CodeGenerator CodeGenerator::GetWebGPUShaderCodeGenerator() {
|
||||
CodeGenerator generator;
|
||||
generator.capabilities_ = GetWebGPUShaderCapabilities();
|
||||
generator.memory_model_ = "OpMemoryModel Logical VulkanKHR\n";
|
||||
generator.extensions_ = "OpExtension \"SPV_KHR_vulkan_memory_model\"\n";
|
||||
generator.types_ = GetWebGPUShaderTypes();
|
||||
return generator;
|
||||
}
|
||||
|
||||
std::string CodeGenerator::Build() const {
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << capabilities_;
|
||||
ss << extensions_;
|
||||
ss << memory_model_;
|
||||
|
||||
for (const EntryPoint& entry_point : entry_points_) {
|
||||
ss << "OpEntryPoint " << entry_point.execution_model << " %"
|
||||
<< entry_point.name << " \"" << entry_point.name << "\" "
|
||||
<< entry_point.interfaces << "\n";
|
||||
}
|
||||
|
||||
for (const EntryPoint& entry_point : entry_points_) {
|
||||
ss << entry_point.execution_modes << "\n";
|
||||
}
|
||||
|
||||
ss << before_types_;
|
||||
ss << types_;
|
||||
ss << after_types_;
|
||||
|
||||
for (const EntryPoint& entry_point : entry_points_) {
|
||||
ss << "\n";
|
||||
ss << "%" << entry_point.name << " = OpFunction %void None %func\n";
|
||||
ss << "%" << entry_point.name << "_entry = OpLabel\n";
|
||||
ss << entry_point.body;
|
||||
ss << "\nOpReturn\nOpFunctionEnd\n";
|
||||
}
|
||||
|
||||
ss << add_at_the_end_;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
} // namespace val
|
||||
} // namespace spvtools
|
49
test/val/val_code_generator.h
Normal file
49
test/val/val_code_generator.h
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2019 Google LLC.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Utility class used to generate SPIR-V code strings for tests
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace spvtools {
|
||||
namespace val {
|
||||
|
||||
struct EntryPoint {
|
||||
std::string name;
|
||||
std::string execution_model;
|
||||
std::string execution_modes;
|
||||
std::string body;
|
||||
std::string interfaces;
|
||||
};
|
||||
|
||||
class CodeGenerator {
|
||||
public:
|
||||
static CodeGenerator GetDefaultShaderCodeGenerator();
|
||||
static CodeGenerator GetWebGPUShaderCodeGenerator();
|
||||
|
||||
std::string Build() const;
|
||||
|
||||
std::vector<EntryPoint> entry_points_;
|
||||
std::string capabilities_;
|
||||
std::string extensions_;
|
||||
std::string memory_model_;
|
||||
std::string before_types_;
|
||||
std::string types_;
|
||||
std::string after_types_;
|
||||
std::string add_at_the_end_;
|
||||
};
|
||||
|
||||
} // namespace val
|
||||
} // namespace spvtools
|
Loading…
Reference in New Issue
Block a user