xref: /aosp_15_r20/external/angle/third_party/spirv-tools/src/source/val/validate_primitives.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 // Copyright (c) 2017 LunarG Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Validates correctness of primitive SPIR-V instructions.
16 
17 #include <string>
18 
19 #include "source/opcode.h"
20 #include "source/val/instruction.h"
21 #include "source/val/validate.h"
22 #include "source/val/validation_state.h"
23 
24 namespace spvtools {
25 namespace val {
26 
27 // Validates correctness of primitive instructions.
PrimitivesPass(ValidationState_t & _,const Instruction * inst)28 spv_result_t PrimitivesPass(ValidationState_t& _, const Instruction* inst) {
29   const spv::Op opcode = inst->opcode();
30 
31   switch (opcode) {
32     case spv::Op::OpEmitVertex:
33     case spv::Op::OpEndPrimitive:
34     case spv::Op::OpEmitStreamVertex:
35     case spv::Op::OpEndStreamPrimitive:
36       _.function(inst->function()->id())
37           ->RegisterExecutionModelLimitation(
38               spv::ExecutionModel::Geometry,
39               std::string(spvOpcodeString(opcode)) +
40                   " instructions require Geometry execution model");
41       break;
42     default:
43       break;
44   }
45 
46   switch (opcode) {
47     case spv::Op::OpEmitStreamVertex:
48     case spv::Op::OpEndStreamPrimitive: {
49       const uint32_t stream_id = inst->word(1);
50       const uint32_t stream_type = _.GetTypeId(stream_id);
51       if (!_.IsIntScalarType(stream_type)) {
52         return _.diag(SPV_ERROR_INVALID_DATA, inst)
53                << spvOpcodeString(opcode)
54                << ": expected Stream to be int scalar";
55       }
56 
57       const spv::Op stream_opcode = _.GetIdOpcode(stream_id);
58       if (!spvOpcodeIsConstant(stream_opcode)) {
59         return _.diag(SPV_ERROR_INVALID_DATA, inst)
60                << spvOpcodeString(opcode)
61                << ": expected Stream to be constant instruction";
62       }
63     }
64 
65     default:
66       break;
67   }
68 
69   return SPV_SUCCESS;
70 }
71 
72 }  // namespace val
73 }  // namespace spvtools
74