| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- // Copyright (c) 2017 LunarG Inc.
- //
- // 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 <sstream>
- #include <string>
- #include "gmock/gmock.h"
- #include "test/unit_spirv.h"
- #include "test/val/val_fixtures.h"
- namespace spvtools {
- namespace val {
- namespace {
- using ::testing::HasSubstr;
- using ::testing::Not;
- using ValidatePrimitives = spvtest::ValidateBase<bool>;
- std::string GenerateShaderCode(
- const std::string& body,
- const std::string& capabilities_and_extensions =
- "OpCapability GeometryStreams",
- const std::string& execution_model = "Geometry") {
- std::ostringstream ss;
- ss << capabilities_and_extensions << "\n";
- ss << "OpMemoryModel Logical GLSL450\n";
- ss << "OpEntryPoint " << execution_model << " %main \"main\"\n";
- if (execution_model == "Geometry") {
- ss << "OpExecutionMode %main InputPoints\n";
- ss << "OpExecutionMode %main OutputPoints\n";
- }
- ss << R"(
- %void = OpTypeVoid
- %func = OpTypeFunction %void
- %f32 = OpTypeFloat 32
- %u32 = OpTypeInt 32 0
- %u32vec4 = OpTypeVector %u32 4
- %f32_0 = OpConstant %f32 0
- %u32_0 = OpConstant %u32 0
- %u32_1 = OpConstant %u32 1
- %u32_2 = OpConstant %u32 2
- %u32_3 = OpConstant %u32 3
- %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
- %main = OpFunction %void None %func
- %main_entry = OpLabel
- )";
- ss << body;
- ss << R"(
- OpReturn
- OpFunctionEnd)";
- return ss.str();
- }
- // Returns SPIR-V assembly fragment representing a function call,
- // the end of the callee body, and the preamble and body of the called
- // function with the given body, but missing the final return and
- // function-end. The result is of the form where it can be used in the
- // |body| argument to GenerateShaderCode.
- std::string CallAndCallee(const std::string& body) {
- std::ostringstream ss;
- ss << R"(
- %placeholder = OpFunctionCall %void %foo
- OpReturn
- OpFunctionEnd
- %foo = OpFunction %void None %func
- %foo_entry = OpLabel
- )";
- ss << body;
- return ss.str();
- }
- // OpEmitVertex doesn't have any parameters, so other validation
- // is handled by the binary parser, and generic dominance checks.
- TEST_F(ValidatePrimitives, EmitVertexSuccess) {
- CompileSuccessfully(
- GenerateShaderCode("OpEmitVertex", "OpCapability Geometry"));
- EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidatePrimitives, EmitVertexFailMissingCapability) {
- CompileSuccessfully(
- GenerateShaderCode("OpEmitVertex", "OpCapability Shader", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Opcode EmitVertex requires one of these capabilities: Geometry"));
- }
- TEST_F(ValidatePrimitives, EmitVertexFailWrongExecutionMode) {
- CompileSuccessfully(
- GenerateShaderCode("OpEmitVertex", "OpCapability Geometry", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr("EmitVertex instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives, EmitVertexFailWrongExecutionModeNestedFunction) {
- CompileSuccessfully(GenerateShaderCode(CallAndCallee("OpEmitVertex"),
- "OpCapability Geometry", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr("EmitVertex instructions require Geometry execution model"));
- }
- // OpEndPrimitive doesn't have any parameters, so other validation
- // is handled by the binary parser, and generic dominance checks.
- TEST_F(ValidatePrimitives, EndPrimitiveSuccess) {
- CompileSuccessfully(
- GenerateShaderCode("OpEndPrimitive", "OpCapability Geometry"));
- EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidatePrimitives, EndPrimitiveFailMissingCapability) {
- CompileSuccessfully(
- GenerateShaderCode("OpEndPrimitive", "OpCapability Shader", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Opcode EndPrimitive requires one of these capabilities: Geometry"));
- }
- TEST_F(ValidatePrimitives, EndPrimitiveFailWrongExecutionMode) {
- CompileSuccessfully(
- GenerateShaderCode("OpEndPrimitive", "OpCapability Geometry", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr("EndPrimitive instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives, EndPrimitiveFailWrongExecutionModeNestedFunction) {
- CompileSuccessfully(GenerateShaderCode(CallAndCallee("OpEndPrimitive"),
- "OpCapability Geometry", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr("EndPrimitive instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives, EmitStreamVertexSuccess) {
- const std::string body = R"(
- OpEmitStreamVertex %u32_0
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidatePrimitives, EmitStreamVertexFailMissingCapability) {
- CompileSuccessfully(GenerateShaderCode("OpEmitStreamVertex %u32_0",
- "OpCapability Shader", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("Opcode EmitStreamVertex requires one of these "
- "capabilities: GeometryStreams"));
- }
- TEST_F(ValidatePrimitives, EmitStreamVertexFailWrongExecutionMode) {
- CompileSuccessfully(GenerateShaderCode(
- "OpEmitStreamVertex %u32_0", "OpCapability GeometryStreams", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "EmitStreamVertex instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives,
- EmitStreamVertexFailWrongExecutionModeNestedFunction) {
- CompileSuccessfully(
- GenerateShaderCode(CallAndCallee("OpEmitStreamVertex %u32_0"),
- "OpCapability GeometryStreams", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "EmitStreamVertex instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives, EmitStreamVertexNonInt) {
- const std::string body = R"(
- OpEmitStreamVertex %f32_0
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("EmitStreamVertex: "
- "expected Stream to be int scalar"));
- }
- TEST_F(ValidatePrimitives, EmitStreamVertexNonScalar) {
- const std::string body = R"(
- OpEmitStreamVertex %u32vec4_0123
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("EmitStreamVertex: "
- "expected Stream to be int scalar"));
- }
- TEST_F(ValidatePrimitives, EmitStreamVertexNonConstant) {
- const std::string body = R"(
- %val1 = OpIAdd %u32 %u32_0 %u32_1
- OpEmitStreamVertex %val1
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("EmitStreamVertex: "
- "expected Stream to be constant instruction"));
- }
- TEST_F(ValidatePrimitives, EndStreamPrimitiveSuccess) {
- const std::string body = R"(
- OpEndStreamPrimitive %u32_0
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
- }
- TEST_F(ValidatePrimitives, EndStreamPrimitiveFailMissingCapability) {
- CompileSuccessfully(GenerateShaderCode("OpEndStreamPrimitive %u32_0",
- "OpCapability Shader", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("Opcode EndStreamPrimitive requires one of these "
- "capabilities: GeometryStreams"));
- }
- TEST_F(ValidatePrimitives, EndStreamPrimitiveFailWrongExecutionMode) {
- CompileSuccessfully(GenerateShaderCode(
- "OpEndStreamPrimitive %u32_0", "OpCapability GeometryStreams", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "EndStreamPrimitive instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives,
- EndStreamPrimitiveFailWrongExecutionModeNestedFunction) {
- CompileSuccessfully(
- GenerateShaderCode(CallAndCallee("OpEndStreamPrimitive %u32_0"),
- "OpCapability GeometryStreams", "Vertex"));
- EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "EndStreamPrimitive instructions require Geometry execution model"));
- }
- TEST_F(ValidatePrimitives, EndStreamPrimitiveNonInt) {
- const std::string body = R"(
- OpEndStreamPrimitive %f32_0
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("EndStreamPrimitive: "
- "expected Stream to be int scalar"));
- }
- TEST_F(ValidatePrimitives, EndStreamPrimitiveNonScalar) {
- const std::string body = R"(
- OpEndStreamPrimitive %u32vec4_0123
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("EndStreamPrimitive: "
- "expected Stream to be int scalar"));
- }
- TEST_F(ValidatePrimitives, EndStreamPrimitiveNonConstant) {
- const std::string body = R"(
- %val1 = OpIAdd %u32 %u32_0 %u32_1
- OpEndStreamPrimitive %val1
- )";
- CompileSuccessfully(GenerateShaderCode(body));
- EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("EndStreamPrimitive: "
- "expected Stream to be constant instruction"));
- }
- } // namespace
- } // namespace val
- } // namespace spvtools
|