diff --git a/Android.mk b/Android.mk index 5d9b398b..5c6cea24 100644 --- a/Android.mk +++ b/Android.mk @@ -39,6 +39,7 @@ SPVTOOLS_SRC_FILES := \ source/validate_atomics.cpp \ source/validate_barriers.cpp \ source/validate_bitwise.cpp \ + source/validate_builtins.cpp \ source/validate_capability.cpp \ source/validate_cfg.cpp \ source/validate_composites.cpp \ diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index cd1ac46b..e9444750 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -284,6 +284,7 @@ set(SPIRV_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/validate_atomics.cpp ${CMAKE_CURRENT_SOURCE_DIR}/validate_barriers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/validate_bitwise.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/validate_builtins.cpp ${CMAKE_CURRENT_SOURCE_DIR}/validate_capability.cpp ${CMAKE_CURRENT_SOURCE_DIR}/validate_cfg.cpp ${CMAKE_CURRENT_SOURCE_DIR}/validate_composites.cpp diff --git a/source/assembly_grammar.h b/source/assembly_grammar.h index 5514e78c..6837a0b6 100644 --- a/source/assembly_grammar.h +++ b/source/assembly_grammar.h @@ -66,6 +66,17 @@ class AssemblyGrammar { spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand, spv_operand_desc* desc) const; + // Finds operand entry in the grammar table and returns its name. + // Returns "Unknown" if not found. + const char* lookupOperandName(spv_operand_type_t type, + uint32_t operand) const { + spv_operand_desc desc = nullptr; + if (lookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) { + return "Unknown"; + } + return desc->name; + } + // Finds the opcode for the given OpSpecConstantOp opcode name. The name // should not have the "Op" prefix. For example, "IAdd" corresponds to // the integer add opcode for OpSpecConstantOp. On success, returns diff --git a/source/val/decoration.h b/source/val/decoration.h index b1d894a4..8d289915 100644 --- a/source/val/decoration.h +++ b/source/val/decoration.h @@ -63,9 +63,7 @@ class Decoration { : dec_type_(t), params_(parameters), struct_member_index_(member_index) {} void set_struct_member_index(uint32_t index) { struct_member_index_ = index; } - int struct_member_index() { return struct_member_index_; } int struct_member_index() const { return struct_member_index_; } - SpvDecoration dec_type() { return dec_type_; } SpvDecoration dec_type() const { return dec_type_; } std::vector& params() { return params_; } const std::vector& params() const { return params_; } diff --git a/source/val/validation_state.h b/source/val/validation_state.h index d63ac025..a9b2448a 100644 --- a/source/val/validation_state.h +++ b/source/val/validation_state.h @@ -269,9 +269,17 @@ class ValidationState_t { return id_decorations_[id]; } const std::vector& id_decorations(uint32_t id) const { + // TODO: This would throw or generate SIGABRT if id has no + // decorations. Remove/refactor this function. return id_decorations_.at(id); } + // Returns const pointer to the internal decoration container. + const std::unordered_map>& id_decorations() + const { + return id_decorations_; + } + /// Finds id's def, if it exists. If found, returns the definition otherwise /// nullptr const Instruction* FindDef(uint32_t id) const; diff --git a/source/validate.cpp b/source/validate.cpp index 184d43d0..976857aa 100644 --- a/source/validate.cpp +++ b/source/validate.cpp @@ -336,8 +336,13 @@ spv_result_t ValidateBinaryUsingContextAndValidationState( } position.index = SPV_INDEX_INSTRUCTION; - return spvValidateIDs(instructions.data(), instructions.size(), *vstate, - &position); + if (auto error = spvValidateIDs(instructions.data(), instructions.size(), + *vstate, &position)) + return error; + + if (auto error = ValidateBuiltIns(*vstate)) return error; + + return SPV_SUCCESS; } } // anonymous namespace diff --git a/source/validate.h b/source/validate.h index 10664d8e..a4f6dde2 100644 --- a/source/validate.h +++ b/source/validate.h @@ -117,6 +117,9 @@ spv_result_t InstructionPass(ValidationState_t& _, /// Performs decoration validation. spv_result_t ValidateDecorations(ValidationState_t& _); +/// Performs validation of built-in variables. +spv_result_t ValidateBuiltIns(const ValidationState_t& _); + /// Validates that type declarations are unique, unless multiple declarations /// of the same data type are allowed by the specification. /// (see section 2.8 Types and Variables) diff --git a/source/validate_builtins.cpp b/source/validate_builtins.cpp new file mode 100644 index 00000000..8e7e95ed --- /dev/null +++ b/source/validate_builtins.cpp @@ -0,0 +1,2471 @@ +// Copyright (c) 2018 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. + +// Validates correctness of built-in variables. + +#include "validate.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "diagnostic.h" +#include "opcode.h" +#include "spirv_target_env.h" +#include "util/bitutils.h" +#include "val/instruction.h" +#include "val/validation_state.h" + +namespace libspirv { + +namespace { + +// Returns a short textual description of the id defined by the given +// instruction. +std::string GetIdDesc(const Instruction& inst) { + std::ostringstream ss; + ss << "ID <" << inst.id() << "> (Op" << spvOpcodeString(inst.opcode()) << ")"; + return ss.str(); +} + +// Gets underlying data type which is +// - member type if instruction is OpTypeStruct +// (member index is taken from decoration). +// - data type if id creates a pointer. +// - type of the constant if instruction is OpConst or OpSpecConst. +// +// Fails in any other case. The function is based on built-ins allowed by +// the Vulkan spec. +// TODO: If non-Vulkan validation rules are added then it might need +// to be refactored. +spv_result_t GetUnderlyingType(const ValidationState_t& _, + const Decoration& decoration, + const Instruction& inst, + uint32_t* underlying_type) { + if (decoration.struct_member_index() != Decoration::kInvalidMember) { + assert(inst.opcode() == SpvOpTypeStruct); + *underlying_type = inst.word(decoration.struct_member_index() + 2); + return SPV_SUCCESS; + } + + assert(inst.opcode() != SpvOpTypeStruct); + + if (spvOpcodeIsConstant(inst.opcode())) { + *underlying_type = inst.type_id(); + return SPV_SUCCESS; + } + + uint32_t storage_class = 0; + if (!_.GetPointerTypeInfo(inst.type_id(), underlying_type, &storage_class)) { + return _.diag(SPV_ERROR_INVALID_DATA) + << GetIdDesc(inst) + << " is decorated with BuiltIn. BuiltIn decoration should only be " + "applied to struct types, variables and constants."; + } + return SPV_SUCCESS; +} + +// Returns Storage Class used by the instruction if applicable. +// Returns SpvStorageClassMax if not. +SpvStorageClass GetStorageClass(const Instruction& inst) { + switch (inst.opcode()) { + case SpvOpTypePointer: + case SpvOpTypeForwardPointer: { + return SpvStorageClass(inst.word(2)); + } + case SpvOpVariable: { + return SpvStorageClass(inst.word(3)); + } + case SpvOpGenericCastToPtrExplicit: { + return SpvStorageClass(inst.word(4)); + } + default: { break; } + } + return SpvStorageClassMax; +} + +// Helper class managing validation of built-ins. +// TODO: Generic functionality of this class can be moved into +// ValidationState_t to be made available to other users. +class BuiltInsValidator { + public: + BuiltInsValidator(const ValidationState_t& vstate) : _(vstate) {} + + // Run validation. + spv_result_t Run(); + + private: + // Goes through all decorations in the module, if decoration is BuiltIn + // validates the instruction defining the decorated id. Also seeds + // id_to_at_reference_checks_ with decorated ids. + spv_result_t ValidateBuiltInsAtDefinition(); + + // The following section contains functions which are called when id defined + // by |inst| is decorated with BuiltIn |decoration|. + // Most functions are specific to a single built-in and have naming scheme: + // ValidateXYZAtDefinition. Some functions are common to multiple kinds of + // BuiltIn. + spv_result_t ValidateClipOrCullDistanceAtDefinition( + const Decoration& decoration, const Instruction& inst); + spv_result_t ValidateFragCoordAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateFragDepthAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateFrontFacingAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateHelperInvocationAtDefinition( + const Decoration& decoration, const Instruction& inst); + spv_result_t ValidateInvocationIdAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateInstanceIndexAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateLayerOrViewportIndexAtDefinition( + const Decoration& decoration, const Instruction& inst); + spv_result_t ValidatePatchVerticesAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidatePointCoordAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidatePointSizeAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidatePositionAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidatePrimitiveIdAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateSampleIdAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateSampleMaskAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateSamplePositionAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateTessCoordAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateTessLevelOuterAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateTessLevelInnerAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateVertexIndexAtDefinition(const Decoration& decoration, + const Instruction& inst); + spv_result_t ValidateWorkgroupSizeAtDefinition(const Decoration& decoration, + const Instruction& inst); + // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId. + spv_result_t ValidateComputeShaderI32Vec3InputAtDefinition( + const Decoration& decoration, const Instruction& inst); + + // The following section contains functions which are called when id defined + // by |referenced_inst| is + // 1. referenced by |referenced_from_inst| + // 2. dependent on |built_in_inst| which is decorated with BuiltIn + // |decoration|. Most functions are specific to a single built-in and have + // naming scheme: ValidateXYZAtReference. Some functions are common to + // multiple kinds of BuiltIn. + spv_result_t ValidateFragCoordAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateFragDepthAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateFrontFacingAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateHelperInvocationAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateInvocationIdAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateInstanceIndexAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidatePatchVerticesAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidatePointCoordAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidatePointSizeAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidatePositionAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidatePrimitiveIdAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateSampleIdAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateSampleMaskAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateSamplePositionAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateTessCoordAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateTessLevelAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateVertexIndexAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateLayerOrViewportIndexAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateWorkgroupSizeAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + spv_result_t ValidateClipOrCullDistanceAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId. + spv_result_t ValidateComputeShaderI32Vec3InputAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + // Validates that |built_in_inst| is not (even indirectly) referenced from + // within a function which can be called with |execution_model|. + // + // |comment| - text explaining why the restriction was imposed. + // |decoration| - BuiltIn decoration which causes the restriction. + // |referenced_inst| - instruction which is dependent on |built_in_inst| and + // defines the id which was referenced. + // |referenced_from_inst| - instruction which references id defined by + // |referenced_inst| from within a function. + spv_result_t ValidateNotCalledWithExecutionModel( + const char* comment, SpvExecutionModel execution_model, + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst); + + // The following section contains functions which check that the decorated + // variable has the type specified in the function name. |diag| would be + // called with a corresponding error message, if validation is not successful. + spv_result_t ValidateBool( + const Decoration& decoration, const Instruction& inst, + const std::function& diag); + spv_result_t ValidateI32( + const Decoration& decoration, const Instruction& inst, + const std::function& diag); + spv_result_t ValidateI32Vec( + const Decoration& decoration, const Instruction& inst, + uint32_t num_components, + const std::function& diag); + spv_result_t ValidateI32Arr( + const Decoration& decoration, const Instruction& inst, + const std::function& diag); + spv_result_t ValidateF32( + const Decoration& decoration, const Instruction& inst, + const std::function& diag); + spv_result_t ValidateF32Vec( + const Decoration& decoration, const Instruction& inst, + uint32_t num_components, + const std::function& diag); + // If |num_components| is zero, the number of components is not checked. + spv_result_t ValidateF32Arr( + const Decoration& decoration, const Instruction& inst, + uint32_t num_components, + const std::function& diag); + + // Generates strings like "Member #0 of struct ID <2>". + std::string GetDefinitionDesc(const Decoration& decoration, + const Instruction& inst) const; + + // Generates strings like "ID <51> (OpTypePointer) is referencing ID <2> + // (OpTypeStruct) which is decorated with BuiltIn Position". + std::string GetReferenceDesc( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst, + SpvExecutionModel execution_model = SpvExecutionModelMax) const; + + // Generates strings like "ID <51> (OpTypePointer) uses storage class + // UniformConstant". + std::string GetStorageClassDesc(const Instruction& inst) const; + + // Updates inner working of the class. Is called sequentially for every + // instruction. + void Update(const Instruction& inst); + + // Traverses call tree and computes function_to_entry_points_, + // entry_point_to_execution_model_ and entry_point_to_execution_mode_. + void ComputeFunctionToEntryPointMapping(); + + const ValidationState_t& _; + + // Mapping id -> list of rules which validate instruction referencing the + // id. Rules can create new rules and add them to this container. + // Using std::map, and not std::unordered_map to avoid iterator invalidation + // during rehashing. + std::map>> + id_to_at_reference_checks_; + + // Id of the function we are currently inside. 0 if not inside a function. + uint32_t function_id_ = 0; + + // Entry points which can (indirectly) call the current function. + // The pointer either points to a vector inside to function_to_entry_points_ + // or to no_entry_points_. The pointer is guaranteed to never be null. + const std::vector no_entry_points; + const std::vector* entry_points_ = &no_entry_points; + + // Execution models with which the current function can be called. + std::set execution_models_; + + // Mapping function -> array of entry points inside this + // module which can (indirectly) call the function. + std::unordered_map> function_to_entry_points_; + + // Mapping entry point -> execution model. + std::unordered_map + entry_point_to_execution_model_; + + // Mapping entry point -> execution mode. + std::unordered_map entry_point_to_execution_mode_; +}; + +void BuiltInsValidator::Update(const Instruction& inst) { + const SpvOp opcode = inst.opcode(); + if (opcode == SpvOpFunction) { + // Entering a function. + assert(function_id_ == 0); + function_id_ = inst.id(); + execution_models_.clear(); + const auto it = function_to_entry_points_.find(function_id_); + if (it == function_to_entry_points_.end()) { + entry_points_ = &no_entry_points; + } else { + entry_points_ = &it->second; + for (const uint32_t entry_point : *entry_points_) { + execution_models_.insert( + entry_point_to_execution_model_.at(entry_point)); + } + } + } + + if (opcode == SpvOpFunctionEnd) { + // Exiting a function. + assert(function_id_ != 0); + function_id_ = 0; + entry_points_ = &no_entry_points; + execution_models_.clear(); + } +} + +void BuiltInsValidator::ComputeFunctionToEntryPointMapping() { + for (const Instruction& inst : _.ordered_instructions()) { + const SpvOp opcode = inst.opcode(); + if (opcode == SpvOpFunction) { + // We are looking for opcodes which can only be found at the top of + // the module. + return; + } + + if (opcode == SpvOpExecutionMode) { + entry_point_to_execution_mode_[inst.word(1)] = + SpvExecutionMode(inst.word(2)); + } + + if (opcode == SpvOpEntryPoint) { + const uint32_t entry_point = inst.word(2); + entry_point_to_execution_model_[entry_point] = + SpvExecutionModel(inst.word(1)); + + std::stack call_stack; + std::set visited; + call_stack.push(entry_point); + while (!call_stack.empty()) { + const uint32_t called_func_id = call_stack.top(); + call_stack.pop(); + if (!visited.insert(called_func_id).second) continue; + + function_to_entry_points_[called_func_id].push_back(entry_point); + + const Function* called_func = _.function(called_func_id); + assert(called_func); + for (uint32_t new_call : called_func->function_call_targets()) { + call_stack.push(new_call); + } + } + } + } +} + +std::string BuiltInsValidator::GetDefinitionDesc( + const Decoration& decoration, const Instruction& inst) const { + std::ostringstream ss; + if (decoration.struct_member_index() != Decoration::kInvalidMember) { + assert(inst.opcode() == SpvOpTypeStruct); + ss << "Member #" << decoration.struct_member_index(); + ss << " of struct ID <" << inst.id() << ">"; + } else { + ss << GetIdDesc(inst); + } + return ss.str(); +} + +std::string BuiltInsValidator::GetReferenceDesc( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, const Instruction& referenced_from_inst, + SpvExecutionModel execution_model) const { + std::ostringstream ss; + ss << GetIdDesc(referenced_from_inst) << " is referencing " + << GetIdDesc(referenced_inst); + if (built_in_inst.id() != referenced_inst.id()) { + ss << " which is dependent on " << GetIdDesc(built_in_inst); + } + + ss << " which is decorated with BuiltIn "; + ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]); + if (function_id_) { + ss << " in function <" << function_id_ << ">"; + if (execution_model != SpvExecutionModelMax) { + ss << " called with execution model "; + ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_EXECUTION_MODEL, + execution_model); + } + } + ss << "."; + return ss.str(); +} + +std::string BuiltInsValidator::GetStorageClassDesc( + const Instruction& inst) const { + std::ostringstream ss; + ss << GetIdDesc(inst) << " uses storage class "; + ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_STORAGE_CLASS, + GetStorageClass(inst)); + ss << "."; + return ss.str(); +} + +spv_result_t BuiltInsValidator::ValidateBool( + const Decoration& decoration, const Instruction& inst, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + if (!_.IsBoolScalarType(underlying_type)) { + return diag(GetDefinitionDesc(decoration, inst) + " is not a bool scalar."); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateI32( + const Decoration& decoration, const Instruction& inst, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + if (!_.IsIntScalarType(underlying_type)) { + return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar."); + } + + const uint32_t bit_width = _.GetBitWidth(underlying_type); + if (bit_width != 32) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width + << "."; + return diag(ss.str()); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateF32( + const Decoration& decoration, const Instruction& inst, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + if (!_.IsFloatScalarType(underlying_type)) { + return diag(GetDefinitionDesc(decoration, inst) + + " is not a float scalar."); + } + + const uint32_t bit_width = _.GetBitWidth(underlying_type); + if (bit_width != 32) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width + << "."; + return diag(ss.str()); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateI32Vec( + const Decoration& decoration, const Instruction& inst, + uint32_t num_components, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + if (!_.IsIntVectorType(underlying_type)) { + return diag(GetDefinitionDesc(decoration, inst) + " is not an int vector."); + } + + const uint32_t actual_num_components = _.GetDimension(underlying_type); + if (_.GetDimension(underlying_type) != num_components) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) << " has " + << actual_num_components << " components."; + return diag(ss.str()); + } + + const uint32_t bit_width = _.GetBitWidth(underlying_type); + if (bit_width != 32) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) + << " has components with bit width " << bit_width << "."; + return diag(ss.str()); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateF32Vec( + const Decoration& decoration, const Instruction& inst, + uint32_t num_components, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + if (!_.IsFloatVectorType(underlying_type)) { + return diag(GetDefinitionDesc(decoration, inst) + + " is not a float vector."); + } + + const uint32_t actual_num_components = _.GetDimension(underlying_type); + if (_.GetDimension(underlying_type) != num_components) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) << " has " + << actual_num_components << " components."; + return diag(ss.str()); + } + + const uint32_t bit_width = _.GetBitWidth(underlying_type); + if (bit_width != 32) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) + << " has components with bit width " << bit_width << "."; + return diag(ss.str()); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateI32Arr( + const Decoration& decoration, const Instruction& inst, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + const Instruction* const type_inst = _.FindDef(underlying_type); + if (type_inst->opcode() != SpvOpTypeArray) { + return diag(GetDefinitionDesc(decoration, inst) + " is not an array."); + } + + const uint32_t component_type = type_inst->word(2); + if (!_.IsIntScalarType(component_type)) { + return diag(GetDefinitionDesc(decoration, inst) + + " components are not int scalar."); + } + + const uint32_t bit_width = _.GetBitWidth(component_type); + if (bit_width != 32) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) + << " has components with bit width " << bit_width << "."; + return diag(ss.str()); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateF32Arr( + const Decoration& decoration, const Instruction& inst, + uint32_t num_components, + const std::function& diag) { + uint32_t underlying_type = 0; + if (spv_result_t error = + GetUnderlyingType(_, decoration, inst, &underlying_type)) { + return error; + } + + const Instruction* const type_inst = _.FindDef(underlying_type); + if (type_inst->opcode() != SpvOpTypeArray) { + return diag(GetDefinitionDesc(decoration, inst) + " is not an array."); + } + + const uint32_t component_type = type_inst->word(2); + if (!_.IsFloatScalarType(component_type)) { + return diag(GetDefinitionDesc(decoration, inst) + + " components are not float scalar."); + } + + const uint32_t bit_width = _.GetBitWidth(component_type); + if (bit_width != 32) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) + << " has components with bit width " << bit_width << "."; + return diag(ss.str()); + } + + if (num_components != 0) { + uint64_t actual_num_components = 0; + if (!_.GetConstantValUint64(type_inst->word(3), &actual_num_components)) { + assert(0 && "Array type definition is corrupt"); + } + if (actual_num_components != num_components) { + std::ostringstream ss; + ss << GetDefinitionDesc(decoration, inst) << " has " + << actual_num_components << " components."; + return diag(ss.str()); + } + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateNotCalledWithExecutionModel( + const char* comment, SpvExecutionModel execution_model, + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (function_id_) { + if (execution_models_.count(execution_model)) { + const char* execution_model_str = _.grammar().lookupOperandName( + SPV_OPERAND_TYPE_EXECUTION_MODEL, execution_model); + const char* built_in_str = _.grammar().lookupOperandName( + SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0]); + return _.diag(SPV_ERROR_INVALID_DATA) + << comment << " " << GetIdDesc(referenced_inst) << " depends on " + << GetIdDesc(built_in_inst) << " which is decorated with BuiltIn " + << built_in_str << "." + << " Id <" << referenced_inst.id() << "> is later referenced by " + << GetIdDesc(referenced_from_inst) << " in function <" + << function_id_ << "> which is called with execution model " + << execution_model_str << "."; + } + } else { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + comment, execution_model, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Arr( + decoration, inst, /* Any number of components */ 0, + [this, &decoration](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " variable needs to be a 32-bit float array. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be only used for variables with Input or Output storage " + "class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + if (storage_class == SpvStorageClassInput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be " + "used for variables with Input storage class if execution model is " + "Vertex.", + SpvExecutionModelVertex, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + if (storage_class == SpvStorageClassOutput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be " + "used for variables with Output storage class if execution model is " + "Fragment.", + SpvExecutionModelFragment, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + switch (execution_model) { + case SpvExecutionModelFragment: + case SpvExecutionModelVertex: + case SpvExecutionModelTessellationControl: + case SpvExecutionModelTessellationEvaluation: + case SpvExecutionModelGeometry: { + // Ok. + break; + } + + default: { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be used only with Fragment, Vertex, " + "TessellationControl, TessellationEvaluation or Geometry " + "execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference, + this, decoration, built_in_inst, referenced_from_inst, + std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateFragCoordAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Vec( + decoration, inst, 4, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn FragCoord " + "variable needs to be a 4-component 32-bit float " + "vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateFragCoordAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateFragCoordAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn FragCoord to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn FragCoord to be used only with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateFragCoordAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateFragDepthAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn FragDepth " + "variable needs to be a 32-bit float scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateFragDepthAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateFragDepthAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn FragDepth to be only used for " + "variables with Output storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn FragDepth to be used only with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateFragDepthAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateFrontFacingAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateBool( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn FrontFacing " + "variable needs to be a bool scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateFrontFacingAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateFrontFacingAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn FrontFacing to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn FrontFacing to be used only with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateFrontFacingAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateHelperInvocationAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateBool( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn HelperInvocation " + "variable needs to be a bool scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateHelperInvocationAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn HelperInvocation to be only used " + "for variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn HelperInvocation to be used only " + "with Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this, + decoration, built_in_inst, referenced_from_inst, + std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateInvocationIdAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn InvocationId " + "variable needs to be a 32-bit int scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateInvocationIdAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateInvocationIdAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn InvocationId to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelTessellationControl && + execution_model != SpvExecutionModelGeometry) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn InvocationId to be used only " + "with TessellationControl or Geometry execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateInvocationIdAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateInstanceIndexAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn InstanceIndex " + "variable needs to be a 32-bit int scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateInstanceIndexAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateInstanceIndexAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn InstanceIndex to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelVertex) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn InstanceIndex to be used only " + "with Vertex execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateInstanceIndexAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePatchVerticesAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn PatchVertices " + "variable needs to be a 32-bit int scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidatePatchVerticesAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePatchVerticesAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PatchVertices to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelTessellationControl && + execution_model != SpvExecutionModelTessellationEvaluation) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PatchVertices to be used only " + "with TessellationControl or TessellationEvaluation " + "execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidatePatchVerticesAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePointCoordAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Vec( + decoration, inst, 2, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn PointCoord " + "variable needs to be a 2-component 32-bit float " + "vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidatePointCoordAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePointCoordAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PointCoord to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PointCoord to be used only with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidatePointCoordAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePointSizeAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn PointSize " + "variable needs to be a 32-bit float scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidatePointSizeAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePointSizeAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PointSize to be only used for " + "variables with Input or Output storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + if (storage_class == SpvStorageClassInput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn PointSize to be used for " + "variables with Input storage class if execution model is Vertex.", + SpvExecutionModelVertex, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + switch (execution_model) { + case SpvExecutionModelVertex: + case SpvExecutionModelTessellationControl: + case SpvExecutionModelTessellationEvaluation: + case SpvExecutionModelGeometry: { + // Ok. + break; + } + + default: { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PointSize to be used only with " + "Vertex, TessellationControl, TessellationEvaluation or " + "Geometry execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidatePointSizeAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePositionAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Vec( + decoration, inst, 4, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn Position " + "variable needs to be a 4-component 32-bit float " + "vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidatePositionAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePositionAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn Position to be only used for " + "variables with Input or Output storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + if (storage_class == SpvStorageClassInput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn Position to be used for variables " + "with Input storage class if execution model is Vertex.", + SpvExecutionModelVertex, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + switch (execution_model) { + case SpvExecutionModelVertex: + case SpvExecutionModelTessellationControl: + case SpvExecutionModelTessellationEvaluation: + case SpvExecutionModelGeometry: { + // Ok. + break; + } + + default: { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn Position to be used only with " + "Vertex, TessellationControl, TessellationEvaluation or " + "Geometry execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidatePositionAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn PrimitiveId " + "variable needs to be a 32-bit int scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidatePrimitiveIdAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PrimitiveId to be only used for " + "variables with Input or Output storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + if (storage_class == SpvStorageClassInput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for " + "variables with Input storage class if execution model is " + "TessellationControl.", + SpvExecutionModelTessellationControl, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for " + "variables with Input storage class if execution model is " + "TessellationEvaluation.", + SpvExecutionModelTessellationEvaluation, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + if (storage_class == SpvStorageClassOutput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for " + "variables with Output storage class if execution model is Fragment.", + SpvExecutionModelFragment, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + switch (execution_model) { + case SpvExecutionModelFragment: + case SpvExecutionModelTessellationControl: + case SpvExecutionModelTessellationEvaluation: + case SpvExecutionModelGeometry: { + // Ok. + break; + } + + default: { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn PrimitiveId to be used only " + "with Fragment, TessellationControl, " + "TessellationEvaluation or Geometry execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidatePrimitiveIdAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateSampleIdAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn SampleId " + "variable needs to be a 32-bit int scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateSampleIdAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateSampleIdAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn SampleId to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn SampleId to be used only with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateSampleIdAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateSampleMaskAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32Arr( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn SampleMask " + "variable needs to be a 32-bit int array. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateSampleMaskAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateSampleMaskAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn SampleMask to be only used for " + "variables with Input or Output storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn SampleMask to be used only with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateSampleMaskAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateSamplePositionAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Vec( + decoration, inst, 2, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn SamplePosition " + "variable needs to be a 2-component 32-bit float " + "vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateSamplePositionAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateSamplePositionAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn SamplePosition to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelFragment) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn SamplePosition to be used only " + "with " + "Fragment execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateSamplePositionAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateTessCoordAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Vec( + decoration, inst, 3, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn TessCoord " + "variable needs to be a 3-component 32-bit float " + "vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateTessCoordAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateTessCoordAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn TessCoord to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelTessellationEvaluation) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn TessCoord to be used only with " + "TessellationEvaluation execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateTessCoordAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateTessLevelOuterAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Arr( + decoration, inst, 4, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn TessLevelOuter " + "variable needs to be a 4-component 32-bit float " + "array. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateTessLevelAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateTessLevelInnerAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateF32Arr( + decoration, inst, 2, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn TessLevelOuter " + "variable needs to be a 2-component 32-bit float " + "array. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateTessLevelAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateTessLevelAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be only used for variables with Input or Output storage " + "class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + if (storage_class == SpvStorageClassInput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be used " + "for variables with Input storage class if execution model is " + "TessellationControl.", + SpvExecutionModelTessellationControl, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + if (storage_class == SpvStorageClassOutput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be used " + "for variables with Output storage class if execution model is " + "TessellationEvaluation.", + SpvExecutionModelTessellationEvaluation, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + switch (execution_model) { + case SpvExecutionModelTessellationControl: + case SpvExecutionModelTessellationEvaluation: { + // Ok. + break; + } + + default: { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be used only with TessellationControl or " + "TessellationEvaluation execution models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateTessLevelAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateVertexIndexAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn VertexIndex " + "variable needs to be a 32-bit int scalar. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateVertexIndexAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn VertexIndex to be only used for " + "variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelVertex) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn VertexIndex to be used only with " + "Vertex execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32( + decoration, inst, + [this, &decoration](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << "variable needs to be a 32-bit int scalar. " << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference, + this, decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput && + storage_class != SpvStorageClassOutput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be only used for variables with Input or Output storage " + "class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + if (storage_class == SpvStorageClassInput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn Layer and ViewportIndex to be " + "used for variables with Input storage class if execution model is " + "Geometry.", + SpvExecutionModelGeometry, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + if (storage_class == SpvStorageClassOutput) { + assert(function_id_ == 0); + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, + "Vulkan spec doesn't allow BuiltIn Layer and ViewportIndex to be " + "used for variables with Output storage class if execution model is " + "Fragment.", + SpvExecutionModelFragment, decoration, built_in_inst, + referenced_from_inst, std::placeholders::_1)); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + switch (execution_model) { + case SpvExecutionModelGeometry: + case SpvExecutionModelFragment: { + // Ok. + break; + } + + default: { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be used only with Fragment or Geometry execution " + "models. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference, + this, decoration, built_in_inst, referenced_from_inst, + std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (spv_result_t error = ValidateI32Vec( + decoration, inst, 3, + [this, &decoration](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " variable needs to be a 3-component 32-bit int " + "vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be only used for variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelGLCompute) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be used only with GLCompute execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this, + decoration, built_in_inst, referenced_from_inst, + std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition( + const Decoration& decoration, const Instruction& inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + if (!spvOpcodeIsConstant(inst.opcode())) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec requires BuiltIn WorkgroupSize to be a constant. " + << GetIdDesc(inst) << " is not a constant."; + } + + if (spv_result_t error = ValidateI32Vec( + decoration, inst, 3, + [this](const std::string& message) -> spv_result_t { + return _.diag(SPV_ERROR_INVALID_DATA) + << "According to the Vulkan spec BuiltIn WorkgroupSize " + "variable " + "needs to be a 3-component 32-bit int vector. " + << message; + })) { + return error; + } + } + + // Seed at reference checks with this built-in. + id_to_at_reference_checks_[inst.id()].push_back( + std::bind(&BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, + decoration, inst, inst, std::placeholders::_1)); + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference( + const Decoration& decoration, const Instruction& built_in_inst, + const Instruction& referenced_inst, + const Instruction& referenced_from_inst) { + if (spvIsVulkanEnv(_.context()->target_env)) { + const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst); + if (storage_class != SpvStorageClassMax && + storage_class != SpvStorageClassInput) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be only used for variables with Input storage class. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst) + << " " << GetStorageClassDesc(referenced_from_inst); + } + + for (const SpvExecutionModel execution_model : execution_models_) { + if (execution_model != SpvExecutionModelGLCompute) { + return _.diag(SPV_ERROR_INVALID_DATA) + << "Vulkan spec allows BuiltIn " + << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, + decoration.params()[0]) + << " to be used only with GLCompute execution model. " + << GetReferenceDesc(decoration, built_in_inst, referenced_inst, + referenced_from_inst, execution_model); + } + } + } + + if (function_id_ == 0) { + // Propagate this rule to all dependant ids in the global scope. + id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind( + &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration, + built_in_inst, referenced_from_inst, std::placeholders::_1)); + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() { + for (const auto& kv : _.id_decorations()) { + const uint32_t id = kv.first; + const Instruction* inst = nullptr; + for (const auto& decoration : kv.second) { + if (decoration.dec_type() != SpvDecorationBuiltIn) { + continue; + } + + if (!inst) { + inst = _.FindDef(id); + assert(inst); + } + + const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]); + // If you are adding a new BuiltIn enum, please register it here. + // If the newly added enum has validation rules associated with it + // consider leaving a TODO and/or creating an issue. + switch (label) { + case SpvBuiltInClipDistance: + case SpvBuiltInCullDistance: { + return ValidateClipOrCullDistanceAtDefinition(decoration, *inst); + } + case SpvBuiltInFragCoord: { + return ValidateFragCoordAtDefinition(decoration, *inst); + } + case SpvBuiltInFragDepth: { + return ValidateFragDepthAtDefinition(decoration, *inst); + } + case SpvBuiltInFrontFacing: { + return ValidateFrontFacingAtDefinition(decoration, *inst); + } + case SpvBuiltInGlobalInvocationId: + case SpvBuiltInLocalInvocationId: + case SpvBuiltInNumWorkgroups: + case SpvBuiltInWorkgroupId: { + return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, + *inst); + } + case SpvBuiltInHelperInvocation: { + return ValidateHelperInvocationAtDefinition(decoration, *inst); + } + case SpvBuiltInInvocationId: { + return ValidateInvocationIdAtDefinition(decoration, *inst); + } + case SpvBuiltInInstanceIndex: { + return ValidateInstanceIndexAtDefinition(decoration, *inst); + } + case SpvBuiltInLayer: + case SpvBuiltInViewportIndex: { + return ValidateLayerOrViewportIndexAtDefinition(decoration, *inst); + } + case SpvBuiltInPatchVertices: { + return ValidatePatchVerticesAtDefinition(decoration, *inst); + } + case SpvBuiltInPointCoord: { + return ValidatePointCoordAtDefinition(decoration, *inst); + } + case SpvBuiltInPointSize: { + return ValidatePointSizeAtDefinition(decoration, *inst); + } + case SpvBuiltInPosition: { + return ValidatePositionAtDefinition(decoration, *inst); + } + case SpvBuiltInPrimitiveId: { + return ValidatePrimitiveIdAtDefinition(decoration, *inst); + } + case SpvBuiltInSampleId: { + return ValidateSampleIdAtDefinition(decoration, *inst); + } + case SpvBuiltInSampleMask: { + return ValidateSampleMaskAtDefinition(decoration, *inst); + } + case SpvBuiltInSamplePosition: { + return ValidateSamplePositionAtDefinition(decoration, *inst); + } + case SpvBuiltInTessCoord: { + return ValidateTessCoordAtDefinition(decoration, *inst); + } + case SpvBuiltInTessLevelOuter: { + return ValidateTessLevelOuterAtDefinition(decoration, *inst); + } + case SpvBuiltInTessLevelInner: { + return ValidateTessLevelInnerAtDefinition(decoration, *inst); + } + case SpvBuiltInVertexIndex: { + return ValidateVertexIndexAtDefinition(decoration, *inst); + } + case SpvBuiltInWorkgroupSize: { + return ValidateWorkgroupSizeAtDefinition(decoration, *inst); + } + case SpvBuiltInVertexId: + case SpvBuiltInInstanceId: + case SpvBuiltInLocalInvocationIndex: + case SpvBuiltInWorkDim: + case SpvBuiltInGlobalSize: + case SpvBuiltInEnqueuedWorkgroupSize: + case SpvBuiltInGlobalOffset: + case SpvBuiltInGlobalLinearId: + case SpvBuiltInSubgroupSize: + case SpvBuiltInSubgroupMaxSize: + case SpvBuiltInNumSubgroups: + case SpvBuiltInNumEnqueuedSubgroups: + case SpvBuiltInSubgroupId: + case SpvBuiltInSubgroupLocalInvocationId: + case SpvBuiltInSubgroupEqMaskKHR: + case SpvBuiltInSubgroupGeMaskKHR: + case SpvBuiltInSubgroupGtMaskKHR: + case SpvBuiltInSubgroupLeMaskKHR: + case SpvBuiltInSubgroupLtMaskKHR: + case SpvBuiltInBaseVertex: + case SpvBuiltInBaseInstance: + case SpvBuiltInDrawIndex: + case SpvBuiltInDeviceIndex: + case SpvBuiltInViewIndex: + case SpvBuiltInBaryCoordNoPerspAMD: + case SpvBuiltInBaryCoordNoPerspCentroidAMD: + case SpvBuiltInBaryCoordNoPerspSampleAMD: + case SpvBuiltInBaryCoordSmoothAMD: + case SpvBuiltInBaryCoordSmoothCentroidAMD: + case SpvBuiltInBaryCoordSmoothSampleAMD: + case SpvBuiltInBaryCoordPullModelAMD: + case SpvBuiltInFragStencilRefEXT: + case SpvBuiltInViewportMaskNV: + case SpvBuiltInSecondaryPositionNV: + case SpvBuiltInSecondaryViewportMaskNV: + case SpvBuiltInPositionPerViewNV: + case SpvBuiltInViewportMaskPerViewNV: + case SpvBuiltInFullyCoveredEXT: + case SpvBuiltInMax: { + // No validation rules (for the moment). + break; + } + } + } + } + + return SPV_SUCCESS; +} + +spv_result_t BuiltInsValidator::Run() { + // First pass: validate all built-ins at definition and seed + // id_to_at_reference_checks_ with built-ins. + if (auto error = ValidateBuiltInsAtDefinition()) { + return error; + } + + if (id_to_at_reference_checks_.empty()) { + // No validation tasks were seeded. Nothing else to do. + return SPV_SUCCESS; + } + + ComputeFunctionToEntryPointMapping(); + + // Second pass: validate every id reference in the module using + // rules in id_to_at_reference_checks_. + for (const Instruction& inst : _.ordered_instructions()) { + Update(inst); + + std::set already_checked; + + for (const auto& operand : inst.operands()) { + if (!spvIsIdType(operand.type)) { + // Not id. + continue; + } + + const uint32_t id = inst.word(operand.offset); + if (id == inst.id()) { + // No need to check result id. + continue; + } + + if (!already_checked.insert(id).second) { + // The instruction has already referenced this id. + continue; + } + + // Instruction references the id. Run all checks associated with the id on + // the instruction. id_to_at_reference_checks_ can be modified in the + // process, iterators are safe because it's a tree-based map. + const auto it = id_to_at_reference_checks_.find(id); + if (it != id_to_at_reference_checks_.end()) { + for (const auto& check : it->second) { + if (spv_result_t error = check(inst)) { + return error; + } + } + } + } + } + + return SPV_SUCCESS; +} + +} // anonymous namespace + +// Validates correctness of built-in variables. +spv_result_t ValidateBuiltIns(const ValidationState_t& _) { + if (!spvIsVulkanEnv(_.context()->target_env)) { + // Early return. All currently implemented rules are based on Vulkan spec. + // + // TODO: If you are adding validation rules for environments other than + // Vulkan (or general rules which are not environment independent), then you + // need to modify or remove this condition. Consider also adding early + // returns into BuiltIn-specific rules, so that the system doesn't spawn new + // rules which don't do anything. + return SPV_SUCCESS; + } + + BuiltInsValidator validator(_); + return validator.Run(); +} + +} // namespace libspirv diff --git a/test/val/CMakeLists.txt b/test/val/CMakeLists.txt index 8b1766dd..7e47bb64 100644 --- a/test/val/CMakeLists.txt +++ b/test/val/CMakeLists.txt @@ -16,7 +16,6 @@ set(VAL_TEST_COMMON_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../test_fixture.h ${CMAKE_CURRENT_SOURCE_DIR}/../unit_spirv.h ${CMAKE_CURRENT_SOURCE_DIR}/val_fixtures.h - ${CMAKE_CURRENT_SOURCE_DIR}/val_fixtures.cpp ) @@ -110,6 +109,12 @@ add_spvtools_unittest(TARGET val_bitwise LIBS ${SPIRV_TOOLS} ) +add_spvtools_unittest(TARGET val_builtins + SRCS val_builtins_test.cpp + ${VAL_TEST_COMMON_SRCS} + LIBS ${SPIRV_TOOLS} +) + add_spvtools_unittest(TARGET val_image SRCS val_image_test.cpp ${VAL_TEST_COMMON_SRCS} diff --git a/test/val/val_builtins_test.cpp b/test/val/val_builtins_test.cpp new file mode 100644 index 00000000..39f8cad0 --- /dev/null +++ b/test/val/val_builtins_test.cpp @@ -0,0 +1,1680 @@ +// Copyright (c) 2018 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. + +// Tests validation rules of GLSL.450.std and OpenCL.std extended instructions. +// Doesn't test OpenCL.std vector size 2, 3, 4, 8 or 16 rules (not supported +// by standard SPIR-V). + +#include +#include +#include + +#include "gmock/gmock.h" +#include "unit_spirv.h" +#include "val_fixtures.h" + +namespace { + +struct TestResult { + TestResult(spv_result_t in_validation_result = SPV_SUCCESS, + const char* in_error_str = nullptr, + const char* in_error_str2 = nullptr) + : validation_result(in_validation_result), + error_str(in_error_str), + error_str2(in_error_str2) {} + spv_result_t validation_result; + const char* error_str; + const char* error_str2; +}; + +using ::testing::Combine; +using ::testing::HasSubstr; +using ::testing::Not; +using ::testing::Values; +using ::testing::ValuesIn; + +using ValidateBuiltIns = spvtest::ValidateBase; +using ValidateVulkanCombineBuiltInExecutionModelDataTypeResult = + spvtest::ValidateBase>; + +struct EntryPoint { + std::string name; + std::string execution_model; + std::string body; +}; + +class CodeGenerator { + public: + std::string Build() const; + + std::vector 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 << "\"\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 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 +)"; +} + +CodeGenerator GetDefaultShaderCodeGenerator() { + CodeGenerator generator; + generator.capabilities_ = GetDefaultShaderCapabilities(); + generator.memory_model_ = "OpMemoryModel Logical GLSL450\n"; + generator.types_ = GetDefaultShaderTypes(); + return generator; +} + +TEST_P(ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, InMain) { + const char* const built_in = std::get<0>(GetParam()); + const char* const execution_model = std::get<1>(GetParam()); + const char* const storage_class = std::get<2>(GetParam()); + const char* const data_type = std::get<3>(GetParam()); + const TestResult& test_result = std::get<4>(GetParam()); + + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = "OpMemberDecorate %built_in_type 0 BuiltIn "; + generator.before_types_ += built_in; + generator.before_types_ += "\n"; + + std::ostringstream after_types; + after_types << "%built_in_type = OpTypeStruct " << data_type << "\n"; + after_types << "%built_in_ptr = OpTypePointer " << storage_class + << " %built_in_type\n"; + after_types << "%built_in_var = OpVariable %built_in_ptr " << storage_class + << "\n"; + after_types << "%data_ptr = OpTypePointer " << storage_class << " " + << data_type << "\n"; + generator.after_types_ = after_types.str(); + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = execution_model; + entry_point.body = R"( +%ptr = OpAccessChain %data_ptr %built_in_var %u32_0 +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(test_result.validation_result, + ValidateInstructions(SPV_ENV_VULKAN_1_0)); + if (test_result.error_str) { + EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str)); + } + if (test_result.error_str2) { + EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str2)); + } +} + +TEST_P(ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, InFunction) { + const char* const built_in = std::get<0>(GetParam()); + const char* const execution_model = std::get<1>(GetParam()); + const char* const storage_class = std::get<2>(GetParam()); + const char* const data_type = std::get<3>(GetParam()); + const TestResult& test_result = std::get<4>(GetParam()); + + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = "OpMemberDecorate %built_in_type 0 BuiltIn "; + generator.before_types_ += built_in; + generator.before_types_ += "\n"; + + std::ostringstream after_types; + after_types << "%built_in_type = OpTypeStruct " << data_type << "\n"; + after_types << "%built_in_ptr = OpTypePointer " << storage_class + << " %built_in_type\n"; + after_types << "%built_in_var = OpVariable %built_in_ptr " << storage_class + << "\n"; + after_types << "%data_ptr = OpTypePointer " << storage_class << " " + << data_type << "\n"; + generator.after_types_ = after_types.str(); + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = execution_model; + entry_point.body = R"( +%val2 = OpFunctionCall %void %foo +)"; + + generator.add_at_the_end_ = R"( +%foo = OpFunction %void None %func +%foo_entry = OpLabel +%ptr = OpAccessChain %data_ptr %built_in_var %u32_0 +OpReturn +OpFunctionEnd +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(test_result.validation_result, + ValidateInstructions(SPV_ENV_VULKAN_1_0)); + if (test_result.error_str) { + EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str)); + } + if (test_result.error_str2) { + EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str2)); + } +} + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), + Values("Vertex", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Output"), Values("%f32arr2", "%f32arr4"), + Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), + Values("Fragment", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%f32arr2", "%f32arr4"), + Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceFragmentOutput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), Values("Fragment"), + Values("Output"), Values("%f32arr4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance " + "to be used for variables with Output storage class if " + "execution model is Fragment.", + "which is called with execution model Fragment."))), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceVertexInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), Values("Vertex"), + Values("Input"), Values("%f32arr4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance " + "to be used for variables with Input storage class if " + "execution model is Vertex.", + "which is called with execution model Vertex."))), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), Values("GLCompute"), + Values("Input", "Output"), Values("%f32arr4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with Fragment, Vertex, TessellationControl, " + "TessellationEvaluation or Geometry execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceNotArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), Values("Fragment"), + Values("Input"), Values("%f32vec2", "%f32vec4", "%f32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float array", + "is not an array"))), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceNotFloatArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), Values("Fragment"), + Values("Input"), Values("%u32arr2", "%u64arr4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float array", + "components are not float scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + ClipAndCullDistanceNotF32Array, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("ClipDistance", "CullDistance"), Values("Fragment"), + Values("Input"), Values("%f64arr2", "%f64arr4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float array", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + FragCoordSuccess, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragCoord"), Values("Fragment"), Values("Input"), + Values("%f32vec4"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + FragCoordNotFragment, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("FragCoord"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%f32vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + FragCoordNotInput, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragCoord"), Values("Fragment"), Values("Output"), + Values("%f32vec4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + FragCoordNotFloatVector, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragCoord"), Values("Fragment"), Values("Input"), + Values("%f32arr4", "%u32vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float vector", + "is not a float vector"))), ); + +INSTANTIATE_TEST_CASE_P( + FragCoordNotFloatVec4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragCoord"), Values("Fragment"), Values("Input"), + Values("%f32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float vector", + "has 3 components"))), ); + +INSTANTIATE_TEST_CASE_P( + FragCoordNotF32Vec4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragCoord"), Values("Fragment"), Values("Input"), + Values("%f64vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float vector", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + FragDepthSuccess, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragDepth"), Values("Fragment"), Values("Output"), + Values("%f32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + FragDepthNotFragment, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("FragDepth"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Output"), Values("%f32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + FragDepthNotOutput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragDepth"), Values("Fragment"), Values("Input"), + Values("%f32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Output storage class", + "uses storage class Input"))), ); + +INSTANTIATE_TEST_CASE_P( + FragDepthNotFloatScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragDepth"), Values("Fragment"), Values("Output"), + Values("%f32vec4", "%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float scalar", + "is not a float scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + FragDepthNotF32, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FragDepth"), Values("Fragment"), Values("Output"), + Values("%f64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + FrontFacingAndHelperInvocationSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FrontFacing", "HelperInvocation"), Values("Fragment"), + Values("Input"), Values("%bool"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + FrontFacingAndHelperInvocationNotFragment, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("FrontFacing", "HelperInvocation"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%bool"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + FrontFacingAndHelperInvocationNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FrontFacing", "HelperInvocation"), Values("Fragment"), + Values("Output"), Values("%bool"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + FrontFacingAndHelperInvocationNotBool, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("FrontFacing", "HelperInvocation"), Values("Fragment"), + Values("Input"), Values("%f32", "%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a bool scalar", + "is not a bool scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + ComputeShaderInputInt32Vec3Success, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("GlobalInvocationId", "LocalInvocationId", "NumWorkgroups", + "WorkgroupId"), + Values("GLCompute"), Values("Input"), Values("%u32vec3"), + Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + ComputeShaderInputInt32Vec3NotGLCompute, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("GlobalInvocationId", "LocalInvocationId", "NumWorkgroups", + "WorkgroupId"), + Values("Vertex", "Fragment", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%u32vec3"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with GLCompute execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + ComputeShaderInputInt32Vec3NotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("GlobalInvocationId", "LocalInvocationId", "NumWorkgroups", + "WorkgroupId"), + Values("GLCompute"), Values("Output"), Values("%u32vec3"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + ComputeShaderInputInt32Vec3NotIntVector, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("GlobalInvocationId", "LocalInvocationId", "NumWorkgroups", + "WorkgroupId"), + Values("GLCompute"), Values("Input"), + Values("%u32arr3", "%f32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 3-component 32-bit int vector", + "is not an int vector"))), ); + +INSTANTIATE_TEST_CASE_P( + ComputeShaderInputInt32Vec3NotIntVec3, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("GlobalInvocationId", "LocalInvocationId", "NumWorkgroups", + "WorkgroupId"), + Values("GLCompute"), Values("Input"), Values("%u32vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 3-component 32-bit int vector", + "has 4 components"))), ); + +INSTANTIATE_TEST_CASE_P( + ComputeShaderInputInt32Vec3NotInt32Vec, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("GlobalInvocationId", "LocalInvocationId", "NumWorkgroups", + "WorkgroupId"), + Values("GLCompute"), Values("Input"), Values("%u64vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 3-component 32-bit int vector", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + InvocationIdSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InvocationId"), Values("Geometry", "TessellationControl"), + Values("Input"), Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + InvocationIdInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InvocationId"), + Values("Vertex", "Fragment", "GLCompute", "TessellationEvaluation"), + Values("Input"), Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with TessellationControl or " + "Geometry execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + InvocationIdNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InvocationId"), Values("Geometry", "TessellationControl"), + Values("Output"), Values("%u32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + InvocationIdNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InvocationId"), Values("Geometry", "TessellationControl"), + Values("Input"), Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + InvocationIdNotInt32, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InvocationId"), Values("Geometry", "TessellationControl"), + Values("Input"), Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + InstanceIndexSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InstanceIndex"), Values("Vertex"), Values("Input"), + Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + InstanceIndexInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("InstanceIndex"), + Values("Geometry", "Fragment", "GLCompute", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Vertex execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + InstanceIndexNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InstanceIndex"), Values("Vertex"), Values("Output"), + Values("%u32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + InstanceIndexNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InstanceIndex"), Values("Vertex"), Values("Input"), + Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + InstanceIndexNotInt32, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("InstanceIndex"), Values("Vertex"), Values("Input"), + Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Layer", "ViewportIndex"), Values("Fragment"), + Values("Input"), Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Layer", "ViewportIndex"), Values("Geometry"), + Values("Output"), Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("Layer", "ViewportIndex"), + Values("Vertex", "GLCompute", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%u32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with Fragment or Geometry execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexFragmentNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("Layer", "ViewportIndex"), Values("Fragment"), Values("Output"), + Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "Output storage class if execution model is Fragment", + "which is called with execution model Fragment"))), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexGeometryNotOutput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("Layer", "ViewportIndex"), Values("Geometry"), Values("Input"), + Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "Input storage class if execution model is Geometry", + "which is called with execution model Geometry"))), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Layer", "ViewportIndex"), Values("Fragment"), + Values("Input"), Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + LayerAndViewportIndexNotInt32, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Layer", "ViewportIndex"), Values("Fragment"), + Values("Input"), Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + PatchVerticesSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PatchVertices"), + Values("TessellationEvaluation", "TessellationControl"), + Values("Input"), Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PatchVerticesInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PatchVertices"), + Values("Vertex", "Fragment", "GLCompute", "Geometry"), + Values("Input"), Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with TessellationControl or " + "TessellationEvaluation execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + PatchVerticesNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PatchVertices"), + Values("TessellationEvaluation", "TessellationControl"), + Values("Output"), Values("%u32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + PatchVerticesNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PatchVertices"), + Values("TessellationEvaluation", "TessellationControl"), + Values("Input"), Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + PatchVerticesNotInt32, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PatchVertices"), + Values("TessellationEvaluation", "TessellationControl"), + Values("Input"), Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + PointCoordSuccess, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointCoord"), Values("Fragment"), Values("Input"), + Values("%f32vec2"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PointCoordNotFragment, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("PointCoord"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%f32vec2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + PointCoordNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointCoord"), Values("Fragment"), Values("Output"), + Values("%f32vec2"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + PointCoordNotFloatVector, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointCoord"), Values("Fragment"), Values("Input"), + Values("%f32arr2", "%u32vec2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float vector", + "is not a float vector"))), ); + +INSTANTIATE_TEST_CASE_P( + PointCoordNotFloatVec3, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointCoord"), Values("Fragment"), Values("Input"), + Values("%f32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float vector", + "has 3 components"))), ); + +INSTANTIATE_TEST_CASE_P( + PointCoordNotF32Vec4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointCoord"), Values("Fragment"), Values("Input"), + Values("%f64vec2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float vector", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + PointSizeOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize"), + Values("Vertex", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Output"), Values("%f32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PointSizeInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize"), + Values("Geometry", "TessellationControl", "TessellationEvaluation"), + Values("Input"), Values("%f32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PointSizeVertexInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize"), Values("Vertex"), Values("Input"), + Values("%f32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow BuiltIn PointSize " + "to be used for variables with Input storage class if " + "execution model is Vertex.", + "which is called with execution model Vertex."))), ); + +INSTANTIATE_TEST_CASE_P( + PointSizeInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize"), Values("GLCompute", "Fragment"), + Values("Input", "Output"), Values("%f32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with Vertex, TessellationControl, " + "TessellationEvaluation or Geometry execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + PointSizeNotFloatScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize"), Values("Vertex"), Values("Output"), + Values("%f32vec4", "%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float scalar", + "is not a float scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + PointSizeNotF32, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PointSize"), Values("Vertex"), Values("Output"), + Values("%f64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit float scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + PositionOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), + Values("Vertex", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Output"), Values("%f32vec4"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PositionInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), + Values("Geometry", "TessellationControl", "TessellationEvaluation"), + Values("Input"), Values("%f32vec4"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PositionVertexInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), Values("Vertex"), Values("Input"), + Values("%f32vec4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow BuiltIn Position " + "to be used for variables with Input storage class if " + "execution model is Vertex.", + "which is called with execution model Vertex."))), ); + +INSTANTIATE_TEST_CASE_P( + PositionInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), Values("GLCompute", "Fragment"), + Values("Input", "Output"), Values("%f32vec4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with Vertex, TessellationControl, " + "TessellationEvaluation or Geometry execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + PositionNotFloatVector, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), Values("Geometry"), Values("Input"), + Values("%f32arr4", "%u32vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float vector", + "is not a float vector"))), ); + +INSTANTIATE_TEST_CASE_P( + PositionNotFloatVec4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), Values("Geometry"), Values("Input"), + Values("%f32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float vector", + "has 3 components"))), ); + +INSTANTIATE_TEST_CASE_P( + PositionNotF32Vec4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("Position"), Values("Geometry"), Values("Input"), + Values("%f64vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float vector", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PrimitiveId"), Values("Fragment", "Geometry"), + Values("Input"), Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PrimitiveId"), + Values("Geometry", "TessellationControl", "TessellationEvaluation"), + Values("Output"), Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PrimitiveId"), Values("Vertex", "GLCompute"), + Values("Input"), Values("%u32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with Fragment, TessellationControl, " + "TessellationEvaluation or Geometry execution models"))), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("PrimitiveId"), Values("Fragment"), Values("Output"), + Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "Output storage class if execution model is Fragment", + "which is called with execution model Fragment"))), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdGeometryNotOutput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PrimitiveId"), + Values("TessellationControl", "TessellationEvaluation"), + Values("Input"), Values("%u32"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Input storage class if execution model is Tessellation", + "which is called with execution model Tessellation"))), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PrimitiveId"), Values("Fragment"), Values("Input"), + Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + PrimitiveIdNotInt32, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("PrimitiveId"), Values("Fragment"), Values("Input"), + Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleIdSuccess, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleId"), Values("Fragment"), Values("Input"), + Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + SampleIdInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("SampleId"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleIdNotInput, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("SampleId"), Values("Fragment"), Values("Output"), + Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "Vulkan spec allows BuiltIn SampleId to be only used " + "for variables with Input storage class"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleIdNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleId"), Values("Fragment"), Values("Input"), + Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleIdNotInt32, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleId"), Values("Fragment"), Values("Input"), + Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleMaskSuccess, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleMask"), Values("Fragment"), Values("Input", "Output"), + Values("%u32arr2", "%u32arr4"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + SampleMaskInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("SampleMask"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%u32arr2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleMaskWrongStorageClass, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleMask"), Values("Fragment"), Values("UniformConstant"), + Values("%u32arr2"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec allows BuiltIn SampleMask to be only used for " + "variables with Input or Output storage class"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleMaskNotArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleMask"), Values("Fragment"), Values("Input"), + Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int array", + "is not an array"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleMaskNotIntArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleMask"), Values("Fragment"), Values("Input"), + Values("%f32arr2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int array", + "components are not int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + SampleMaskNotInt32Array, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SampleMask"), Values("Fragment"), Values("Input"), + Values("%u64arr2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int array", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + SamplePositionSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SamplePosition"), Values("Fragment"), Values("Input"), + Values("%f32vec2"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + SamplePositionNotFragment, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("SamplePosition"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%f32vec2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Fragment execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + SamplePositionNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SamplePosition"), Values("Fragment"), Values("Output"), + Values("%f32vec2"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + SamplePositionNotFloatVector, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SamplePosition"), Values("Fragment"), Values("Input"), + Values("%f32arr2", "%u32vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float vector", + "is not a float vector"))), ); + +INSTANTIATE_TEST_CASE_P( + SamplePositionNotFloatVec2, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SamplePosition"), Values("Fragment"), Values("Input"), + Values("%f32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float vector", + "has 3 components"))), ); + +INSTANTIATE_TEST_CASE_P( + SamplePositionNotF32Vec2, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("SamplePosition"), Values("Fragment"), Values("Input"), + Values("%f64vec2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float vector", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + TessCoordSuccess, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessCoord"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32vec3"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + TessCoordNotFragment, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("TessCoord"), + Values("Vertex", "GLCompute", "Geometry", "TessellationControl", + "Fragment"), + Values("Input"), Values("%f32vec3"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be used only with TessellationEvaluation execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + TessCoordNotInput, ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessCoord"), Values("Fragment"), Values("Output"), + Values("%f32vec3"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "to be only used for variables with Input storage class", + "uses storage class Output"))), ); + +INSTANTIATE_TEST_CASE_P( + TessCoordNotFloatVector, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessCoord"), Values("Fragment"), Values("Input"), + Values("%f32arr3", "%u32vec4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 3-component 32-bit float vector", + "is not a float vector"))), ); + +INSTANTIATE_TEST_CASE_P( + TessCoordNotFloatVec3, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessCoord"), Values("Fragment"), Values("Input"), + Values("%f32vec2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 3-component 32-bit float vector", + "has 2 components"))), ); + +INSTANTIATE_TEST_CASE_P( + TessCoordNotF32Vec3, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessCoord"), Values("Fragment"), Values("Input"), + Values("%f64vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 3-component 32-bit float vector", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterTeseInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32arr4"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterTescOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationControl"), + Values("Output"), Values("%f32arr4"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), + Values("Vertex", "GLCompute", "Geometry", "Fragment"), + Values("Input"), Values("%f32arr4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with TessellationControl or " + "TessellationEvaluation execution models."))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterOutputTese, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationEvaluation"), + Values("Output"), Values("%f32arr4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be " + "used for variables with Output storage class if execution " + "model is TessellationEvaluation."))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterInputTesc, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationControl"), + Values("Input"), Values("%f32arr4"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be " + "used for variables with Input storage class if execution " + "model is TessellationControl."))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterNotArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32vec4", "%f32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float array", + "is not an array"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterNotFloatArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationEvaluation"), + Values("Input"), Values("%u32arr4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float array", + "components are not float scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterNotFloatArr4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32arr3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float array", + "has 3 components"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelOuterNotF32Arr4, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelOuter"), Values("TessellationEvaluation"), + Values("Input"), Values("%f64arr4"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 4-component 32-bit float array", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerTeseInputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32arr2"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerTescOutputSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationControl"), + Values("Output"), Values("%f32arr2"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), + Values("Vertex", "GLCompute", "Geometry", "Fragment"), + Values("Input"), Values("%f32arr2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with TessellationControl or " + "TessellationEvaluation execution models."))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerOutputTese, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationEvaluation"), + Values("Output"), Values("%f32arr2"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be " + "used for variables with Output storage class if execution " + "model is TessellationEvaluation."))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerInputTesc, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationControl"), + Values("Input"), Values("%f32arr2"), + Values(TestResult( + SPV_ERROR_INVALID_DATA, + "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be " + "used for variables with Input storage class if execution " + "model is TessellationControl."))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerNotArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32vec2", "%f32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float array", + "is not an array"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerNotFloatArray, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationEvaluation"), + Values("Input"), Values("%u32arr2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float array", + "components are not float scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerNotFloatArr2, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationEvaluation"), + Values("Input"), Values("%f32arr3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float array", + "has 3 components"))), ); + +INSTANTIATE_TEST_CASE_P( + TessLevelInnerNotF32Arr2, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("TessLevelInner"), Values("TessellationEvaluation"), + Values("Input"), Values("%f64arr2"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 2-component 32-bit float array", + "has components with bit width 64"))), ); + +INSTANTIATE_TEST_CASE_P( + VertexIndexSuccess, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("VertexIndex"), Values("Vertex"), Values("Input"), + Values("%u32"), Values(TestResult())), ); + +INSTANTIATE_TEST_CASE_P( + VertexIndexInvalidExecutionModel, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("VertexIndex"), + Values("Fragment", "GLCompute", "Geometry", "TessellationControl", + "TessellationEvaluation"), + Values("Input"), Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "to be used only with Vertex execution model"))), ); + +INSTANTIATE_TEST_CASE_P( + VertexIndexNotInput, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine( + Values("VertexIndex"), Values("Vertex"), Values("Output"), + Values("%u32"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "Vulkan spec allows BuiltIn VertexIndex to be only " + "used for variables with Input storage class"))), ); + +INSTANTIATE_TEST_CASE_P( + VertexIndexNotIntScalar, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("VertexIndex"), Values("Vertex"), Values("Input"), + Values("%f32", "%u32vec3"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "is not an int scalar"))), ); + +INSTANTIATE_TEST_CASE_P( + VertexIndexNotInt32, + ValidateVulkanCombineBuiltInExecutionModelDataTypeResult, + Combine(Values("VertexIndex"), Values("Vertex"), Values("Input"), + Values("%u64"), + Values(TestResult(SPV_ERROR_INVALID_DATA, + "needs to be a 32-bit int scalar", + "has bit width 64"))), ); + +TEST_F(ValidateBuiltIns, WorkgroupSizeSuccess) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %workgroup_size BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstantComposite %u32vec3 %u32_1 %u32_1 %u32_1 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "GLCompute"; + entry_point.body = R"( +%copy = OpCopyObject %u32vec3 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0)); +} + +TEST_F(ValidateBuiltIns, WorkgroupSizeFragment) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %workgroup_size BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstantComposite %u32vec3 %u32_1 %u32_1 %u32_1 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "Fragment"; + entry_point.body = R"( +%copy = OpCopyObject %u32vec3 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("Vulkan spec allows BuiltIn WorkgroupSize to be used " + "only with GLCompute execution model")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("is referencing ID <2> (OpConstantComposite) which is " + "decorated with BuiltIn WorkgroupSize in function <1> " + "called with execution model Fragment")); +} + +TEST_F(ValidateBuiltIns, WorkgroupSizeNotConstant) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %copy BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstantComposite %u32vec3 %u32_1 %u32_1 %u32_1 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "GLCompute"; + entry_point.body = R"( +%copy = OpCopyObject %u32vec3 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("Vulkan spec requires BuiltIn WorkgroupSize to be a " + "constant. ID <2> (OpCopyObject) is not a constant")); +} + +TEST_F(ValidateBuiltIns, WorkgroupSizeNotVector) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %workgroup_size BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstant %u32 16 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "GLCompute"; + entry_point.body = R"( +%copy = OpCopyObject %u32 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("According to the Vulkan spec BuiltIn WorkgroupSize " + "variable needs to be a 3-component 32-bit int vector. " + "ID <2> (OpConstant) is not an int vector.")); +} + +TEST_F(ValidateBuiltIns, WorkgroupSizeNotIntVector) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %workgroup_size BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstantComposite %f32vec3 %f32_1 %f32_1 %f32_1 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "GLCompute"; + entry_point.body = R"( +%copy = OpCopyObject %f32vec3 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("According to the Vulkan spec BuiltIn WorkgroupSize " + "variable needs to be a 3-component 32-bit int vector. " + "ID <2> (OpConstantComposite) is not an int vector.")); +} + +TEST_F(ValidateBuiltIns, WorkgroupSizeNotVec3) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %workgroup_size BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstantComposite %u32vec2 %u32_1 %u32_1 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "GLCompute"; + entry_point.body = R"( +%copy = OpCopyObject %u32vec2 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("According to the Vulkan spec BuiltIn WorkgroupSize " + "variable needs to be a 3-component 32-bit int vector. " + "ID <2> (OpConstantComposite) has 2 components.")); +} + +TEST_F(ValidateBuiltIns, WorkgroupSizeNotInt32Vec) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %workgroup_size BuiltIn WorkgroupSize +)"; + + generator.after_types_ = R"( +%workgroup_size = OpConstantComposite %u64vec3 %u64_1 %u64_1 %u64_1 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "GLCompute"; + entry_point.body = R"( +%copy = OpCopyObject %u64vec3 %workgroup_size +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT( + getDiagnosticString(), + HasSubstr("According to the Vulkan spec BuiltIn WorkgroupSize variable " + "needs to be a 3-component 32-bit int vector. ID <2> " + "(OpConstantComposite) has components with bit width 64.")); +} + +TEST_F(ValidateBuiltIns, GeometryPositionInOutSuccess) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + + generator.before_types_ = R"( +OpMemberDecorate %input_type 0 BuiltIn Position +OpMemberDecorate %output_type 0 BuiltIn Position +)"; + + generator.after_types_ = R"( +%input_type = OpTypeStruct %f32vec4 +%input_ptr = OpTypePointer Input %input_type +%input = OpVariable %input_ptr Input +%input_f32vec4_ptr = OpTypePointer Input %f32vec4 +%output_type = OpTypeStruct %f32vec4 +%output_ptr = OpTypePointer Output %output_type +%output = OpVariable %output_ptr Output +%output_f32vec4_ptr = OpTypePointer Output %f32vec4 +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "Geometry"; + entry_point.body = R"( +%input_pos = OpAccessChain %input_f32vec4_ptr %input %u32_0 +%output_pos = OpAccessChain %output_f32vec4_ptr %output %u32_0 +%pos = OpLoad %f32vec4 %input_pos +OpStore %output_pos %pos +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0)); +} + +TEST_F(ValidateBuiltIns, VertexPositionVariableSuccess) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpDecorate %position BuiltIn Position +)"; + + generator.after_types_ = R"( +%f32vec4_ptr_output = OpTypePointer Output %f32vec4 +%position = OpVariable %f32vec4_ptr_output Output +)"; + + EntryPoint entry_point; + entry_point.name = "main"; + entry_point.execution_model = "Vertex"; + entry_point.body = R"( +OpStore %position %f32vec4_0123 +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0)); +} + +TEST_F(ValidateBuiltIns, FragmentPositionTwoEntryPoints) { + CodeGenerator generator = GetDefaultShaderCodeGenerator(); + generator.before_types_ = R"( +OpMemberDecorate %output_type 0 BuiltIn Position +)"; + + generator.after_types_ = R"( +%output_type = OpTypeStruct %f32vec4 +%output_ptr = OpTypePointer Output %output_type +%output = OpVariable %output_ptr Output +%output_f32vec4_ptr = OpTypePointer Output %f32vec4 +)"; + + EntryPoint entry_point; + entry_point.name = "vmain"; + entry_point.execution_model = "Vertex"; + entry_point.body = R"( +%val1 = OpFunctionCall %void %foo +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + entry_point.name = "fmain"; + entry_point.execution_model = "Fragment"; + entry_point.body = R"( +%val2 = OpFunctionCall %void %foo +)"; + generator.entry_points_.push_back(std::move(entry_point)); + + generator.add_at_the_end_ = R"( +%foo = OpFunction %void None %func +%foo_entry = OpLabel +%position = OpAccessChain %output_f32vec4_ptr %output %u32_0 +OpStore %position %f32vec4_0123 +OpReturn +OpFunctionEnd +)"; + + CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0); + ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0)); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("Vulkan spec allows BuiltIn Position to be used only " + "with Vertex, TessellationControl, " + "TessellationEvaluation or Geometry execution models")); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("called with execution model Fragment")); +} + +} // anonymous namespace diff --git a/test/val/val_capability_test.cpp b/test/val/val_capability_test.cpp index 2f04357d..1e5b1f03 100644 --- a/test/val/val_capability_test.cpp +++ b/test/val/val_capability_test.cpp @@ -1336,20 +1336,30 @@ INSTANTIATE_TEST_CASE_P(BuiltIn, ValidateCapabilityVulkan10, ValuesIn(AllSpirV10Capabilities()), Values( make_pair(string(kGLSL450MemoryModel) + - "OpEntryPoint Vertex %func \"shader\" \n" + - "OpDecorate %intt BuiltIn PointSize\n" + "OpEntryPoint Vertex %func \"shader\" \n" + "OpMemberDecorate %block 0 BuiltIn PointSize\n" + "%f32 = OpTypeFloat 32\n" + "%block = OpTypeStruct %f32\n" "%intt = OpTypeInt 32 0\n" + string(kVoidFVoid), // Capabilities which should succeed. AllVulkan10Capabilities()), make_pair(string(kGLSL450MemoryModel) + - "OpEntryPoint Vertex %func \"shader\" \n" + - "OpDecorate %intt BuiltIn ClipDistance\n" - "%intt = OpTypeInt 32 0\n" + string(kVoidFVoid), + "OpEntryPoint Vertex %func \"shader\" \n" + "OpMemberDecorate %block 0 BuiltIn ClipDistance\n" + "%f32 = OpTypeFloat 32\n" + "%intt = OpTypeInt 32 0\n" + "%intt_4 = OpConstant %intt 4\n" + "%f32arr4 = OpTypeArray %f32 %intt_4\n" + "%block = OpTypeStruct %f32arr4\n" + string(kVoidFVoid), AllVulkan10Capabilities()), make_pair(string(kGLSL450MemoryModel) + - "OpEntryPoint Vertex %func \"shader\" \n" + - "OpDecorate %intt BuiltIn CullDistance\n" - "%intt = OpTypeInt 32 0\n" + string(kVoidFVoid), + "OpEntryPoint Vertex %func \"shader\" \n" + "OpMemberDecorate %block 0 BuiltIn CullDistance\n" + "%f32 = OpTypeFloat 32\n" + "%intt = OpTypeInt 32 0\n" + "%intt_4 = OpConstant %intt 4\n" + "%f32arr4 = OpTypeArray %f32 %intt_4\n" + "%block = OpTypeStruct %f32arr4\n" + string(kVoidFVoid), AllVulkan10Capabilities()) )),); @@ -1598,8 +1608,9 @@ OpCapability DrawParameters OpExtension "SPV_KHR_shader_draw_parameters" OpMemoryModel Logical GLSL450 OpEntryPoint Vertex %func "shader" -OpDecorate %intt BuiltIn PointSize -%intt = OpTypeInt 32 0 +OpMemberDecorate %block 0 BuiltIn PointSize +%f32 = OpTypeFloat 32 +%block = OpTypeStruct %f32 )" + string(kVoidFVoid); CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0); diff --git a/test/val/val_fixtures.cpp b/test/val/val_fixtures.cpp deleted file mode 100644 index 81db1587..00000000 --- a/test/val/val_fixtures.cpp +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) 2015-2016 The Khronos Group 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. - -// Common validation fixtures for unit tests - -#include "val_fixtures.h" - -#include -#include -#include - -#include "test_fixture.h" - -namespace spvtest { - -template -ValidateBase::ValidateBase() : binary_(), diagnostic_() { - // Initialize to default command line options. Different tests can then - // specialize specific options as necessary. - options_ = spvValidatorOptionsCreate(); -} - -template -spv_const_binary ValidateBase::get_const_binary() { - return spv_const_binary(binary_); -} - -template -void ValidateBase::TearDown() { - if (diagnostic_) { - spvDiagnosticPrint(diagnostic_); - } - spvDiagnosticDestroy(diagnostic_); - spvBinaryDestroy(binary_); - spvValidatorOptionsDestroy(options_); -} - -template -void ValidateBase::CompileSuccessfully(std::string code, - spv_target_env env) { - spv_diagnostic diagnostic = nullptr; - ASSERT_EQ(SPV_SUCCESS, - spvTextToBinary(ScopedContext(env).context, code.c_str(), - code.size(), &binary_, &diagnostic)) - << "ERROR: " << diagnostic->error - << "\nSPIR-V could not be compiled into binary:\n" - << code; -} - -template -void ValidateBase::OverwriteAssembledBinary(uint32_t index, uint32_t word) { - ASSERT_TRUE(index < binary_->wordCount) - << "OverwriteAssembledBinary: The given index is larger than the binary " - "word count."; - binary_->code[index] = word; -} - -template -spv_result_t ValidateBase::ValidateInstructions(spv_target_env env) { - return spvValidateWithOptions(ScopedContext(env).context, options_, - get_const_binary(), &diagnostic_); -} - -template -spv_result_t ValidateBase::ValidateAndRetrieveValidationState( - spv_target_env env) { - return spvtools::ValidateBinaryAndKeepValidationState( - ScopedContext(env).context, options_, get_const_binary()->code, - get_const_binary()->wordCount, &diagnostic_, &vstate_); -} - -template -std::string ValidateBase::getDiagnosticString() { - return diagnostic_ == nullptr ? std::string() - : std::string(diagnostic_->error); -} - -template -spv_validator_options ValidateBase::getValidatorOptions() { - return options_; -} - -template -spv_position_t ValidateBase::getErrorPosition() { - return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position; -} - -template class spvtest::ValidateBase; -template class spvtest::ValidateBase; -template class spvtest::ValidateBase; -template class spvtest::ValidateBase>; -template class spvtest::ValidateBase< - std::tuple>>>; -template class spvtest::ValidateBase< - std::tuple, - std::function>>>; -template class spvtest::ValidateBase; -template class spvtest::ValidateBase>; -} // namespace spvtest diff --git a/test/val/val_fixtures.h b/test/val/val_fixtures.h index 5865db21..3f53e967 100644 --- a/test/val/val_fixtures.h +++ b/test/val/val_fixtures.h @@ -18,6 +18,7 @@ #define LIBSPIRV_TEST_VALIDATE_FIXTURES_H_ #include "source/val/validation_state.h" +#include "test_fixture.h" #include "unit_spirv.h" namespace spvtest { @@ -60,5 +61,78 @@ class ValidateBase : public ::testing::Test, spv_validator_options options_; std::unique_ptr vstate_; }; + +template +ValidateBase::ValidateBase() : binary_(), diagnostic_() { + // Initialize to default command line options. Different tests can then + // specialize specific options as necessary. + options_ = spvValidatorOptionsCreate(); +} + +template +spv_const_binary ValidateBase::get_const_binary() { + return spv_const_binary(binary_); +} + +template +void ValidateBase::TearDown() { + if (diagnostic_) { + spvDiagnosticPrint(diagnostic_); + } + spvDiagnosticDestroy(diagnostic_); + spvBinaryDestroy(binary_); + spvValidatorOptionsDestroy(options_); +} + +template +void ValidateBase::CompileSuccessfully(std::string code, + spv_target_env env) { + spv_diagnostic diagnostic = nullptr; + ASSERT_EQ(SPV_SUCCESS, + spvTextToBinary(ScopedContext(env).context, code.c_str(), + code.size(), &binary_, &diagnostic)) + << "ERROR: " << diagnostic->error + << "\nSPIR-V could not be compiled into binary:\n" + << code; +} + +template +void ValidateBase::OverwriteAssembledBinary(uint32_t index, uint32_t word) { + ASSERT_TRUE(index < binary_->wordCount) + << "OverwriteAssembledBinary: The given index is larger than the binary " + "word count."; + binary_->code[index] = word; +} + +template +spv_result_t ValidateBase::ValidateInstructions(spv_target_env env) { + return spvValidateWithOptions(ScopedContext(env).context, options_, + get_const_binary(), &diagnostic_); +} + +template +spv_result_t ValidateBase::ValidateAndRetrieveValidationState( + spv_target_env env) { + return spvtools::ValidateBinaryAndKeepValidationState( + ScopedContext(env).context, options_, get_const_binary()->code, + get_const_binary()->wordCount, &diagnostic_, &vstate_); +} + +template +std::string ValidateBase::getDiagnosticString() { + return diagnostic_ == nullptr ? std::string() + : std::string(diagnostic_->error); +} + +template +spv_validator_options ValidateBase::getValidatorOptions() { + return options_; +} + +template +spv_position_t ValidateBase::getErrorPosition() { + return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position; +} + } // namespace spvtest #endif