/src/spirv-tools/source/val/validate_mesh_shading.cpp
Line | Count | Source |
1 | | // Copyright (c) 2022 The Khronos Group 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 ray query instructions from SPV_KHR_ray_query |
16 | | |
17 | | #include <string> |
18 | | |
19 | | #include "source/val/instruction.h" |
20 | | #include "source/val/validate.h" |
21 | | #include "source/val/validation_state.h" |
22 | | |
23 | | namespace spvtools { |
24 | | namespace val { |
25 | | |
26 | | bool IsInterfaceVariable(ValidationState_t& _, const Instruction* inst, |
27 | 0 | spv::ExecutionModel model) { |
28 | 0 | bool foundInterface = false; |
29 | 0 | for (auto entry_point : _.entry_points()) { |
30 | 0 | const auto* models = _.GetExecutionModels(entry_point); |
31 | 0 | if (models->find(model) == models->end()) return false; |
32 | 0 | for (const auto& desc : _.entry_point_descriptions(entry_point)) { |
33 | 0 | for (auto interface : desc.interfaces) { |
34 | 0 | if (inst->id() == interface) { |
35 | 0 | foundInterface = true; |
36 | 0 | break; |
37 | 0 | } |
38 | 0 | } |
39 | 0 | } |
40 | 0 | } |
41 | 0 | return foundInterface; |
42 | 0 | } |
43 | | |
44 | | spv_result_t ValidateEmitMeshTasks(ValidationState_t& _, |
45 | 0 | const Instruction* inst) { |
46 | 0 | _.function(inst->function()->id()) |
47 | 0 | ->RegisterExecutionModelLimitation([](spv::ExecutionModel model, |
48 | 0 | std::string* message) { |
49 | 0 | if (model != spv::ExecutionModel::TaskEXT) { |
50 | 0 | if (message) { |
51 | 0 | *message = "OpEmitMeshTasksEXT requires TaskEXT execution model"; |
52 | 0 | } |
53 | 0 | return false; |
54 | 0 | } |
55 | 0 | return true; |
56 | 0 | }); |
57 | |
|
58 | 0 | const uint32_t group_count_x = _.GetOperandTypeId(inst, 0); |
59 | 0 | if (!_.IsUnsignedIntScalarType(group_count_x) || |
60 | 0 | _.GetBitWidth(group_count_x) != 32) { |
61 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
62 | 0 | << "Group Count X must be a 32-bit unsigned int scalar"; |
63 | 0 | } |
64 | | |
65 | 0 | const uint32_t group_count_y = _.GetOperandTypeId(inst, 1); |
66 | 0 | if (!_.IsUnsignedIntScalarType(group_count_y) || |
67 | 0 | _.GetBitWidth(group_count_y) != 32) { |
68 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
69 | 0 | << "Group Count Y must be a 32-bit unsigned int scalar"; |
70 | 0 | } |
71 | | |
72 | 0 | const uint32_t group_count_z = _.GetOperandTypeId(inst, 2); |
73 | 0 | if (!_.IsUnsignedIntScalarType(group_count_z) || |
74 | 0 | _.GetBitWidth(group_count_z) != 32) { |
75 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
76 | 0 | << "Group Count Z must be a 32-bit unsigned int scalar"; |
77 | 0 | } |
78 | | |
79 | 0 | if (inst->operands().size() == 4) { |
80 | 0 | const auto payload = _.FindDef(inst->GetOperandAs<uint32_t>(3)); |
81 | 0 | if (payload->opcode() != spv::Op::OpVariable) { |
82 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
83 | 0 | << "Payload must be the result of a OpVariable"; |
84 | 0 | } |
85 | 0 | if (payload->GetOperandAs<spv::StorageClass>(2) != |
86 | 0 | spv::StorageClass::TaskPayloadWorkgroupEXT) { |
87 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
88 | 0 | << "Payload OpVariable must have a storage class of " |
89 | 0 | "TaskPayloadWorkgroupEXT"; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | return SPV_SUCCESS; |
93 | 0 | } |
94 | | |
95 | | spv_result_t ValidateSetMeshOutputs(ValidationState_t& _, |
96 | 0 | const Instruction* inst) { |
97 | 0 | _.function(inst->function()->id()) |
98 | 0 | ->RegisterExecutionModelLimitation([](spv::ExecutionModel model, |
99 | 0 | std::string* message) { |
100 | 0 | if (model != spv::ExecutionModel::MeshEXT) { |
101 | 0 | if (message) { |
102 | 0 | *message = "OpSetMeshOutputsEXT requires MeshEXT execution model"; |
103 | 0 | } |
104 | 0 | return false; |
105 | 0 | } |
106 | 0 | return true; |
107 | 0 | }); |
108 | |
|
109 | 0 | const uint32_t vertex_count = _.GetOperandTypeId(inst, 0); |
110 | 0 | if (!_.IsUnsignedIntScalarType(vertex_count) || |
111 | 0 | _.GetBitWidth(vertex_count) != 32) { |
112 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
113 | 0 | << "Vertex Count must be a 32-bit unsigned int scalar"; |
114 | 0 | } |
115 | | |
116 | 0 | const uint32_t primitive_count = _.GetOperandTypeId(inst, 1); |
117 | 0 | if (!_.IsUnsignedIntScalarType(primitive_count) || |
118 | 0 | _.GetBitWidth(primitive_count) != 32) { |
119 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
120 | 0 | << "Primitive Count must be a 32-bit unsigned int scalar"; |
121 | 0 | } |
122 | | |
123 | | // Will only validate if constants are used (or spec constant frozen) |
124 | 0 | uint64_t vertex_count_value = 0; |
125 | 0 | if (_.EvalConstantValUint64(inst->GetOperandAs<uint32_t>(0), |
126 | 0 | &vertex_count_value)) { |
127 | 0 | _.function(inst->function()->id()) |
128 | 0 | ->RegisterLimitation( |
129 | 0 | [vertex_count_value](const ValidationState_t& state, |
130 | 0 | const Function* entry_point, |
131 | 0 | std::string* message) { |
132 | 0 | const uint32_t output_vertices = |
133 | 0 | state.GetOutputVertices(entry_point->id()); |
134 | 0 | if (vertex_count_value > output_vertices) { |
135 | 0 | *message = |
136 | 0 | "OpSetMeshOutputsEXT Vertex Count (" + |
137 | 0 | std::to_string(vertex_count_value) + |
138 | 0 | ") is larger than the OutputVertices in OpExecutionMode (" + |
139 | 0 | std::to_string(output_vertices) + ")."; |
140 | 0 | return false; |
141 | 0 | } |
142 | 0 | return true; |
143 | 0 | }); |
144 | 0 | } |
145 | 0 | uint64_t primitive_count_value = 0; |
146 | 0 | if (_.EvalConstantValUint64(inst->GetOperandAs<uint32_t>(1), |
147 | 0 | &primitive_count_value)) { |
148 | 0 | _.function(inst->function()->id()) |
149 | 0 | ->RegisterLimitation( |
150 | 0 | [primitive_count_value](const ValidationState_t& state, |
151 | 0 | const Function* entry_point, |
152 | 0 | std::string* message) { |
153 | 0 | const uint32_t output_primitives = |
154 | 0 | state.GetOutputPrimitivesEXT(entry_point->id()); |
155 | 0 | if (primitive_count_value > output_primitives) { |
156 | 0 | *message = "OpSetMeshOutputsEXT Primitive Count (" + |
157 | 0 | std::to_string(primitive_count_value) + |
158 | 0 | ") is larger than the OutputPrimitivesEXT in " |
159 | 0 | "OpExecutionMode (" + |
160 | 0 | std::to_string(output_primitives) + ")."; |
161 | 0 | return false; |
162 | 0 | } |
163 | 0 | return true; |
164 | 0 | }); |
165 | 0 | } |
166 | |
|
167 | 0 | return SPV_SUCCESS; |
168 | 0 | } |
169 | | |
170 | | spv_result_t ValidateMeshVariable(ValidationState_t& _, |
171 | 202k | const Instruction* inst) { |
172 | 202k | if (!_.HasCapability(spv::Capability::MeshShadingEXT)) { |
173 | 202k | return SPV_SUCCESS; |
174 | 202k | } |
175 | 0 | bool is_mesh_interface_var = |
176 | 0 | IsInterfaceVariable(_, inst, spv::ExecutionModel::MeshEXT); |
177 | 0 | bool is_frag_interface_var = |
178 | 0 | IsInterfaceVariable(_, inst, spv::ExecutionModel::Fragment); |
179 | |
|
180 | 0 | const spv::StorageClass storage_class = |
181 | 0 | inst->GetOperandAs<spv::StorageClass>(2); |
182 | 0 | bool storage_output = (storage_class == spv::StorageClass::Output); |
183 | 0 | bool storage_input = (storage_class == spv::StorageClass::Input); |
184 | |
|
185 | 0 | if (_.HasDecoration(inst->id(), spv::Decoration::PerPrimitiveEXT)) { |
186 | 0 | if (is_frag_interface_var && !storage_input) { |
187 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
188 | 0 | << "PerPrimitiveEXT decoration must be applied only to " |
189 | 0 | "variables in the Input Storage Class in the Fragment " |
190 | 0 | "Execution Model."; |
191 | 0 | } |
192 | | |
193 | 0 | if (is_mesh_interface_var && !storage_output) { |
194 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
195 | 0 | << _.VkErrorID(4336) |
196 | 0 | << "PerPrimitiveEXT decoration must be applied only to " |
197 | 0 | "variables in the Output Storage Class in the " |
198 | 0 | "Storage Class in the MeshEXT Execution Model."; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | // This only applies to user interface variables, not built-ins (they |
203 | | // are validated with the rest of the builtin) |
204 | 0 | if (is_mesh_interface_var && storage_output && |
205 | 0 | !_.HasDecoration(inst->id(), spv::Decoration::BuiltIn)) { |
206 | 0 | const Instruction* pointer_inst = _.FindDef(inst->type_id()); |
207 | 0 | if (pointer_inst->opcode() == spv::Op::OpTypePointer) { |
208 | 0 | if (!_.IsArrayType(pointer_inst->word(3))) { |
209 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
210 | 0 | << "In the MeshEXT Execution Mode, all Output Variables " |
211 | 0 | "must contain an Array."; |
212 | 0 | } |
213 | 0 | } |
214 | 0 | } |
215 | | |
216 | 0 | return SPV_SUCCESS; |
217 | 0 | } |
218 | | |
219 | 14.3M | spv_result_t MeshShadingPass(ValidationState_t& _, const Instruction* inst) { |
220 | 14.3M | const spv::Op opcode = inst->opcode(); |
221 | 14.3M | switch (opcode) { |
222 | 0 | case spv::Op::OpEmitMeshTasksEXT: |
223 | 0 | return ValidateEmitMeshTasks(_, inst); |
224 | 0 | case spv::Op::OpSetMeshOutputsEXT: |
225 | 0 | return ValidateSetMeshOutputs(_, inst); |
226 | 202k | case spv::Op::OpVariable: |
227 | 202k | return ValidateMeshVariable(_, inst); |
228 | | // No validation rules (for the moment). |
229 | 0 | case spv::Op::OpWritePackedPrimitiveIndices4x8NV: |
230 | 14.1M | default: |
231 | 14.1M | break; |
232 | 14.3M | } |
233 | | |
234 | 14.1M | return SPV_SUCCESS; |
235 | 14.3M | } |
236 | | |
237 | | } // namespace val |
238 | | } // namespace spvtools |