Coverage Report

Created: 2025-07-23 06:18

/src/spirv-tools/source/val/validate_mesh_shading.cpp
Line
Count
Source (jump to first uncovered line)
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 "source/opcode.h"
18
#include "source/spirv_target_env.h"
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
10.9M
spv_result_t MeshShadingPass(ValidationState_t& _, const Instruction* inst) {
45
10.9M
  const spv::Op opcode = inst->opcode();
46
10.9M
  switch (opcode) {
47
0
    case spv::Op::OpEmitMeshTasksEXT: {
48
0
      _.function(inst->function()->id())
49
0
          ->RegisterExecutionModelLimitation(
50
0
              [](spv::ExecutionModel model, std::string* message) {
51
0
                if (model != spv::ExecutionModel::TaskEXT) {
52
0
                  if (message) {
53
0
                    *message =
54
0
                        "OpEmitMeshTasksEXT requires TaskEXT execution model";
55
0
                  }
56
0
                  return false;
57
0
                }
58
0
                return true;
59
0
              });
60
61
0
      const uint32_t group_count_x = _.GetOperandTypeId(inst, 0);
62
0
      if (!_.IsUnsignedIntScalarType(group_count_x) ||
63
0
          _.GetBitWidth(group_count_x) != 32) {
64
0
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
65
0
               << "Group Count X must be a 32-bit unsigned int scalar";
66
0
      }
67
68
0
      const uint32_t group_count_y = _.GetOperandTypeId(inst, 1);
69
0
      if (!_.IsUnsignedIntScalarType(group_count_y) ||
70
0
          _.GetBitWidth(group_count_y) != 32) {
71
0
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
72
0
               << "Group Count Y must be a 32-bit unsigned int scalar";
73
0
      }
74
75
0
      const uint32_t group_count_z = _.GetOperandTypeId(inst, 2);
76
0
      if (!_.IsUnsignedIntScalarType(group_count_z) ||
77
0
          _.GetBitWidth(group_count_z) != 32) {
78
0
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
79
0
               << "Group Count Z must be a 32-bit unsigned int scalar";
80
0
      }
81
82
0
      if (inst->operands().size() == 4) {
83
0
        const auto payload = _.FindDef(inst->GetOperandAs<uint32_t>(3));
84
0
        if (payload->opcode() != spv::Op::OpVariable) {
85
0
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
86
0
                 << "Payload must be the result of a OpVariable";
87
0
        }
88
0
        if (payload->GetOperandAs<spv::StorageClass>(2) !=
89
0
            spv::StorageClass::TaskPayloadWorkgroupEXT) {
90
0
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
91
0
                 << "Payload OpVariable must have a storage class of "
92
0
                    "TaskPayloadWorkgroupEXT";
93
0
        }
94
0
      }
95
0
      break;
96
0
    }
97
98
0
    case spv::Op::OpSetMeshOutputsEXT: {
99
0
      _.function(inst->function()->id())
100
0
          ->RegisterExecutionModelLimitation(
101
0
              [](spv::ExecutionModel model, std::string* message) {
102
0
                if (model != spv::ExecutionModel::MeshEXT) {
103
0
                  if (message) {
104
0
                    *message =
105
0
                        "OpSetMeshOutputsEXT requires MeshEXT execution model";
106
0
                  }
107
0
                  return false;
108
0
                }
109
0
                return true;
110
0
              });
111
112
0
      const uint32_t vertex_count = _.GetOperandTypeId(inst, 0);
113
0
      if (!_.IsUnsignedIntScalarType(vertex_count) ||
114
0
          _.GetBitWidth(vertex_count) != 32) {
115
0
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
116
0
               << "Vertex Count must be a 32-bit unsigned int scalar";
117
0
      }
118
119
0
      const uint32_t primitive_count = _.GetOperandTypeId(inst, 1);
120
0
      if (!_.IsUnsignedIntScalarType(primitive_count) ||
121
0
          _.GetBitWidth(primitive_count) != 32) {
122
0
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
123
0
               << "Primitive Count must be a 32-bit unsigned int scalar";
124
0
      }
125
126
0
      break;
127
0
    }
128
129
0
    case spv::Op::OpWritePackedPrimitiveIndices4x8NV: {
130
      // No validation rules (for the moment).
131
0
      break;
132
0
    }
133
148k
    case spv::Op::OpVariable: {
134
148k
      if (_.HasCapability(spv::Capability::MeshShadingEXT)) {
135
0
        bool meshInterfaceVar =
136
0
            IsInterfaceVariable(_, inst, spv::ExecutionModel::MeshEXT);
137
0
        bool fragInterfaceVar =
138
0
            IsInterfaceVariable(_, inst, spv::ExecutionModel::Fragment);
139
140
0
        const spv::StorageClass storage_class =
141
0
            inst->GetOperandAs<spv::StorageClass>(2);
142
0
        bool storage_output = (storage_class == spv::StorageClass::Output);
143
0
        bool storage_input = (storage_class == spv::StorageClass::Input);
144
145
0
        if (_.HasDecoration(inst->id(), spv::Decoration::PerPrimitiveEXT)) {
146
0
          if (fragInterfaceVar && !storage_input) {
147
0
            return _.diag(SPV_ERROR_INVALID_DATA, inst)
148
0
                   << "PerPrimitiveEXT decoration must be applied only to "
149
0
                      "variables in the Input Storage Class in the Fragment "
150
0
                      "Execution Model.";
151
0
          }
152
153
0
          if (meshInterfaceVar && !storage_output) {
154
0
            return _.diag(SPV_ERROR_INVALID_DATA, inst)
155
0
                   << _.VkErrorID(4336)
156
0
                   << "PerPrimitiveEXT decoration must be applied only to "
157
0
                      "variables in the Output Storage Class in the "
158
0
                      "Storage Class in the MeshEXT Execution Model.";
159
0
          }
160
0
        }
161
0
      }
162
148k
      break;
163
148k
    }
164
10.8M
    default:
165
10.8M
      break;
166
10.9M
  }
167
168
10.9M
  return SPV_SUCCESS;
169
10.9M
}
170
171
}  // namespace val
172
}  // namespace spvtools