| 1 |
// Copyright (c) 2017 Google Inc. |
| 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 <sstream> |
| 16 |
#include <string> |
| 17 |
|
| 18 |
#include "gmock/gmock.h" |
| 19 |
#include "test/unit_spirv.h" |
| 20 |
#include "test/val/val_fixtures.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace val { |
| 24 |
namespace { |
| 25 |
|
| 26 |
using ::testing::HasSubstr; |
| 27 |
using ::testing::Not; |
| 28 |
|
| 29 |
using ValidateDerivatives = spvtest::ValidateBase<bool>; |
| 30 |
|
| 31 |
std::string GenerateShaderCode( |
| 32 |
const std::string& body, |
| 33 |
const std::string& capabilities_and_extensions = "", |
| 34 |
const std::string& execution_model = "Fragment") { |
| 35 |
std::stringstream ss; |
| 36 |
ss << R"( |
| 37 |
OpCapability Shader |
| 38 |
OpCapability DerivativeControl |
| 39 |
)"; |
| 40 |
|
| 41 |
ss << capabilities_and_extensions; |
| 42 |
ss << "OpMemoryModel Logical GLSL450\n"; |
| 43 |
ss << "OpEntryPoint " << execution_model << " %main \"main\"" |
| 44 |
<< " %f32_var_input" |
| 45 |
<< " %f32vec4_var_input" |
| 46 |
<< "\n"; |
| 47 |
if (execution_model == "Fragment") { |
| 48 |
ss << "OpExecutionMode %main OriginUpperLeft\n"; |
| 49 |
} |
| 50 |
|
| 51 |
ss << R"( |
| 52 |
%void = OpTypeVoid |
| 53 |
%func = OpTypeFunction %void |
| 54 |
%bool = OpTypeBool |
| 55 |
%f32 = OpTypeFloat 32 |
| 56 |
%u32 = OpTypeInt 32 0 |
| 57 |
%s32 = OpTypeInt 32 1 |
| 58 |
%f32vec4 = OpTypeVector %f32 4 |
| 59 |
|
| 60 |
%f32_ptr_input = OpTypePointer Input %f32 |
| 61 |
%f32_var_input = OpVariable %f32_ptr_input Input |
| 62 |
|
| 63 |
%f32vec4_ptr_input = OpTypePointer Input %f32vec4 |
| 64 |
%f32vec4_var_input = OpVariable %f32vec4_ptr_input Input |
| 65 |
)"; |
| 66 |
|
| 67 |
if (capabilities_and_extensions.find("OpCapability Float16") != |
| 68 |
std::string::npos) { |
| 69 |
ss << "%f16 = OpTypeFloat 16\n" |
| 70 |
<< "%f16vec4 = OpTypeVector %f16 4\n" |
| 71 |
<< "%f16_0 = OpConstantNull %f16\n" |
| 72 |
<< "%f16vec4_0 = OpConstantNull %f16vec4\n"; |
| 73 |
} |
| 74 |
|
| 75 |
ss << R"( |
| 76 |
%main = OpFunction %void None %func |
| 77 |
%main_entry = OpLabel |
| 78 |
)"; |
| 79 |
|
| 80 |
ss << body; |
| 81 |
|
| 82 |
ss << R"( |
| 83 |
OpReturn |
| 84 |
OpFunctionEnd)"; |
| 85 |
|
| 86 |
return ss.str(); |
| 87 |
} |
| 88 |
|
| 89 |
TEST_F(ValidateDerivatives, ScalarSuccess) { |
| 90 |
const std::string body = R"( |
| 91 |
%f32_var = OpLoad %f32 %f32_var_input |
| 92 |
%val1 = OpDPdx %f32 %f32_var |
| 93 |
%val2 = OpDPdy %f32 %f32_var |
| 94 |
%val3 = OpFwidth %f32 %f32_var |
| 95 |
%val4 = OpDPdxFine %f32 %f32_var |
| 96 |
%val5 = OpDPdyFine %f32 %f32_var |
| 97 |
%val6 = OpFwidthFine %f32 %f32_var |
| 98 |
%val7 = OpDPdxCoarse %f32 %f32_var |
| 99 |
%val8 = OpDPdyCoarse %f32 %f32_var |
| 100 |
%val9 = OpFwidthCoarse %f32 %f32_var |
| 101 |
)"; |
| 102 |
|
| 103 |
CompileSuccessfully(GenerateShaderCode(body).c_str()); |
| 104 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 105 |
} |
| 106 |
|
| 107 |
TEST_F(ValidateDerivatives, VectorSuccess) { |
| 108 |
const std::string body = R"( |
| 109 |
%f32vec4_var = OpLoad %f32vec4 %f32vec4_var_input |
| 110 |
%val1 = OpDPdx %f32vec4 %f32vec4_var |
| 111 |
%val2 = OpDPdy %f32vec4 %f32vec4_var |
| 112 |
%val3 = OpFwidth %f32vec4 %f32vec4_var |
| 113 |
%val4 = OpDPdxFine %f32vec4 %f32vec4_var |
| 114 |
%val5 = OpDPdyFine %f32vec4 %f32vec4_var |
| 115 |
%val6 = OpFwidthFine %f32vec4 %f32vec4_var |
| 116 |
%val7 = OpDPdxCoarse %f32vec4 %f32vec4_var |
| 117 |
%val8 = OpDPdyCoarse %f32vec4 %f32vec4_var |
| 118 |
%val9 = OpFwidthCoarse %f32vec4 %f32vec4_var |
| 119 |
)"; |
| 120 |
|
| 121 |
CompileSuccessfully(GenerateShaderCode(body).c_str()); |
| 122 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 123 |
} |
| 124 |
|
| 125 |
TEST_F(ValidateDerivatives, OpDPdxWrongResultType) { |
| 126 |
const std::string body = R"( |
| 127 |
%f32_var = OpLoad %f32 %f32_var_input |
| 128 |
%val1 = OpDPdx %u32 %f32vec4 |
| 129 |
)"; |
| 130 |
|
| 131 |
CompileSuccessfully(GenerateShaderCode(body).c_str()); |
| 132 |
ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions()); |
| 133 |
EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 10[%v4float] cannot " |
| 134 |
"be a type")); |
| 135 |
} |
| 136 |
|
| 137 |
TEST_F(ValidateDerivatives, OpDPdxWrongPType) { |
| 138 |
const std::string body = R"( |
| 139 |
%f32vec4_var = OpLoad %f32vec4 %f32vec4_var_input |
| 140 |
%val1 = OpDPdx %f32 %f32vec4_var |
| 141 |
)"; |
| 142 |
|
| 143 |
CompileSuccessfully(GenerateShaderCode(body).c_str()); |
| 144 |
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions()); |
| 145 |
EXPECT_THAT(getDiagnosticString(), |
| 146 |
HasSubstr("Expected P type and Result Type to be the same: " |
| 147 |
"DPdx")); |
| 148 |
} |
| 149 |
|
| 150 |
TEST_F(ValidateDerivatives, OpDPdxWrongExecutionModel) { |
| 151 |
const std::string body = R"( |
| 152 |
%f32vec4_var = OpLoad %f32vec4 %f32vec4_var_input |
| 153 |
%val1 = OpDPdx %f32vec4 %f32vec4_var |
| 154 |
)"; |
| 155 |
|
| 156 |
CompileSuccessfully(GenerateShaderCode(body, "", "Vertex").c_str()); |
| 157 |
ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions()); |
| 158 |
EXPECT_THAT(getDiagnosticString(), |
| 159 |
HasSubstr("Derivative instructions require Fragment or GLCompute " |
| 160 |
"execution model: DPdx")); |
| 161 |
} |
| 162 |
|
| 163 |
using ValidateHalfDerivatives = spvtest::ValidateBase<std::string>; |
| 164 |
|
| 165 |
TEST_P(ValidateHalfDerivatives, ScalarFailure) { |
| 166 |
const std::string op = GetParam(); |
| 167 |
const std::string body = "%val = " + op + " %f16 %f16_0\n"; |
| 168 |
|
| 169 |
CompileSuccessfully( |
| 170 |
GenerateShaderCode(body, "OpCapability Float16\n").c_str()); |
| 171 |
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions()); |
| 172 |
EXPECT_THAT(getDiagnosticString(), |
| 173 |
HasSubstr("Result type component width must be 32 bits")); |
| 174 |
} |
| 175 |
|
| 176 |
TEST_P(ValidateHalfDerivatives, VectorFailure) { |
| 177 |
const std::string op = GetParam(); |
| 178 |
const std::string body = "%val = " + op + " %f16vec4 %f16vec4_0\n"; |
| 179 |
|
| 180 |
CompileSuccessfully( |
| 181 |
GenerateShaderCode(body, "OpCapability Float16\n").c_str()); |
| 182 |
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions()); |
| 183 |
EXPECT_THAT(getDiagnosticString(), |
| 184 |
HasSubstr("Result type component width must be 32 bits")); |
| 185 |
} |
| 186 |
|
| 187 |
INSTANTIATE_TEST_SUITE_P(HalfDerivatives, ValidateHalfDerivatives, |
| 188 |
::testing::Values("OpDPdx", "OpDPdy", "OpFwidth", |
| 189 |
"OpDPdxFine", "OpDPdyFine", |
| 190 |
"OpFwidthFine", "OpDPdxCoarse", |
| 191 |
"OpDPdyCoarse", "OpFwidthCoarse")); |
| 192 |
|
| 193 |
} // namespace |
| 194 |
} // namespace val |
| 195 |
} // namespace spvtools |