Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 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 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 15 | #include "SpirvShader.hpp" |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 16 | #include "SpirvShaderDebug.hpp" |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 17 | |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 18 | #include "System/Debug.hpp" |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 19 | #include "Vulkan/VkPipelineLayout.hpp" |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 20 | #include "Vulkan/VkRenderPass.hpp" |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 21 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 22 | #include "marl/defer.h" |
| 23 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 24 | #include <spirv/unified1/spirv.hpp> |
Ben Clayton | 62bb5ed | 2019-06-18 13:12:20 +0100 | [diff] [blame] | 25 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 26 | namespace sw { |
| 27 | |
| 28 | SpirvShader::SpirvShader( |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 29 | uint32_t codeSerialID, |
| 30 | VkShaderStageFlagBits pipelineStage, |
| 31 | const char *entryPointName, |
| 32 | InsnStore const &insns, |
| 33 | const vk::RenderPass *renderPass, |
| 34 | uint32_t subpassIndex, |
Ben Clayton | 7d0ce41 | 2019-12-03 13:26:31 +0000 | [diff] [blame] | 35 | bool robustBufferAccess, |
| 36 | const std::shared_ptr<vk::dbg::Context> &dbgctx) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 37 | : insns{ insns } |
| 38 | , inputs{ MAX_INTERFACE_COMPONENTS } |
| 39 | , outputs{ MAX_INTERFACE_COMPONENTS } |
| 40 | , codeSerialID(codeSerialID) |
| 41 | , robustBufferAccess(robustBufferAccess) |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 42 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 43 | ASSERT(insns.size() > 0); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 44 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 45 | if(dbgctx) |
| 46 | { |
| 47 | dbgInit(dbgctx); |
| 48 | } |
| 49 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 50 | if(renderPass) |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 51 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 52 | // capture formats of any input attachments present |
| 53 | auto subpass = renderPass->getSubpass(subpassIndex); |
| 54 | inputAttachmentFormats.reserve(subpass.inputAttachmentCount); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 55 | for(auto i = 0u; i < subpass.inputAttachmentCount; i++) |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 56 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 57 | auto attachmentIndex = subpass.pInputAttachments[i].attachment; |
| 58 | inputAttachmentFormats.push_back(attachmentIndex != VK_ATTACHMENT_UNUSED |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 59 | ? renderPass->getAttachment(attachmentIndex).format |
| 60 | : VK_FORMAT_UNDEFINED); |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 61 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Ben Clayton | 964b9e3 | 2021-06-25 09:00:22 +0100 | [diff] [blame] | 64 | // The identifiers of all OpVariables that define the entry point's IO variables. |
| 65 | std::unordered_set<Object::ID> interfaceIds; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 66 | |
| 67 | Function::ID currentFunction; |
| 68 | Block::ID currentBlock; |
| 69 | InsnIterator blockStart; |
| 70 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 71 | for(auto insn : *this) |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 72 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 73 | spv::Op opcode = insn.opcode(); |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 74 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 75 | switch(opcode) |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 76 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 77 | case spv::OpEntryPoint: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 78 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 79 | executionModel = spv::ExecutionModel(insn.word(1)); |
| 80 | auto id = Function::ID(insn.word(2)); |
| 81 | auto name = insn.string(3); |
| 82 | auto stage = executionModelToStage(executionModel); |
| 83 | if(stage == pipelineStage && strcmp(name, entryPointName) == 0) |
| 84 | { |
| 85 | ASSERT_MSG(entryPoint == 0, "Duplicate entry point with name '%s' and stage %d", name, int(stage)); |
| 86 | entryPoint = id; |
Ben Clayton | 964b9e3 | 2021-06-25 09:00:22 +0100 | [diff] [blame] | 87 | |
| 88 | auto interfaceIdsOffset = 3 + insn.stringSizeInWords(3); |
| 89 | for(uint32_t i = interfaceIdsOffset; i < insn.wordCount(); i++) |
| 90 | { |
| 91 | interfaceIds.emplace(insn.word(i)); |
| 92 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 93 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 94 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 95 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 96 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 97 | case spv::OpExecutionMode: |
| 98 | ProcessExecutionMode(insn); |
| 99 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 100 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 101 | case spv::OpDecorate: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 102 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 103 | TypeOrObjectID targetId = insn.word(1); |
| 104 | auto decoration = static_cast<spv::Decoration>(insn.word(2)); |
| 105 | uint32_t value = insn.wordCount() > 3 ? insn.word(3) : 0; |
| 106 | |
| 107 | decorations[targetId].Apply(decoration, value); |
| 108 | |
| 109 | switch(decoration) |
| 110 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 111 | case spv::DecorationDescriptorSet: |
| 112 | descriptorDecorations[targetId].DescriptorSet = value; |
| 113 | break; |
| 114 | case spv::DecorationBinding: |
| 115 | descriptorDecorations[targetId].Binding = value; |
| 116 | break; |
| 117 | case spv::DecorationInputAttachmentIndex: |
| 118 | descriptorDecorations[targetId].InputAttachmentIndex = value; |
| 119 | break; |
| 120 | case spv::DecorationSample: |
| 121 | modes.ContainsSampleQualifier = true; |
| 122 | break; |
| 123 | default: |
| 124 | // Only handling descriptor decorations here. |
| 125 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if(decoration == spv::DecorationCentroid) |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 129 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 130 | modes.NeedsCentroid = true; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 131 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 132 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 133 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 134 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 135 | case spv::OpMemberDecorate: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 136 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 137 | Type::ID targetId = insn.word(1); |
| 138 | auto memberIndex = insn.word(2); |
| 139 | auto decoration = static_cast<spv::Decoration>(insn.word(3)); |
| 140 | uint32_t value = insn.wordCount() > 4 ? insn.word(4) : 0; |
| 141 | |
| 142 | auto &d = memberDecorations[targetId]; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 143 | if(memberIndex >= d.size()) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 144 | d.resize(memberIndex + 1); // on demand; exact size would require another pass... |
| 145 | |
| 146 | d[memberIndex].Apply(decoration, value); |
| 147 | |
| 148 | if(decoration == spv::DecorationCentroid) |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 149 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 150 | modes.NeedsCentroid = true; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 151 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 152 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 153 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 154 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 155 | case spv::OpDecorateId: |
Sean Risser | 79a271a | 2020-12-02 16:56:03 -0500 | [diff] [blame] | 156 | { |
| 157 | auto decoration = static_cast<spv::Decoration>(insn.word(2)); |
| 158 | |
| 159 | // Currently OpDecorateId only supports UniformId, which provides information for |
| 160 | // potential optimizations that we don't perform, and CounterBuffer, which is used |
| 161 | // by HLSL to build the graphics pipeline with shader reflection. At the driver level, |
| 162 | // the CounterBuffer decoration does nothing, so we can safely ignore both decorations. |
| 163 | ASSERT(decoration == spv::DecorationUniformId || decoration == spv::DecorationCounterBuffer); |
Sean Risser | 79a271a | 2020-12-02 16:56:03 -0500 | [diff] [blame] | 164 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 165 | break; |
Sean Risser | 79a271a | 2020-12-02 16:56:03 -0500 | [diff] [blame] | 166 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 167 | case spv::OpDecorateString: |
| 168 | case spv::OpMemberDecorateString: |
| 169 | // We assume these are for HLSL semantics, ignore them. |
| 170 | break; |
Sean Risser | 79a271a | 2020-12-02 16:56:03 -0500 | [diff] [blame] | 171 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 172 | case spv::OpDecorationGroup: |
| 173 | // Nothing to do here. We don't need to record the definition of the group; we'll just have |
| 174 | // the bundle of decorations float around. If we were to ever walk the decorations directly, |
| 175 | // we might think about introducing this as a real Object. |
| 176 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 177 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 178 | case spv::OpGroupDecorate: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 179 | { |
| 180 | uint32_t group = insn.word(1); |
| 181 | auto const &groupDecorations = decorations[group]; |
| 182 | auto const &descriptorGroupDecorations = descriptorDecorations[group]; |
| 183 | for(auto i = 2u; i < insn.wordCount(); i++) |
| 184 | { |
| 185 | // Remaining operands are targets to apply the group to. |
| 186 | uint32_t target = insn.word(i); |
| 187 | decorations[target].Apply(groupDecorations); |
| 188 | descriptorDecorations[target].Apply(descriptorGroupDecorations); |
| 189 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 190 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 191 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 192 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 193 | case spv::OpGroupMemberDecorate: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 194 | { |
| 195 | auto const &srcDecorations = decorations[insn.word(1)]; |
| 196 | for(auto i = 2u; i < insn.wordCount(); i += 2) |
| 197 | { |
| 198 | // remaining operands are pairs of <id>, literal for members to apply to. |
| 199 | auto &d = memberDecorations[insn.word(i)]; |
| 200 | auto memberIndex = insn.word(i + 1); |
| 201 | if(memberIndex >= d.size()) |
| 202 | d.resize(memberIndex + 1); // on demand resize, see above... |
| 203 | d[memberIndex].Apply(srcDecorations); |
| 204 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 205 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 206 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 207 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 208 | case spv::OpLabel: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 209 | { |
| 210 | ASSERT(currentBlock.value() == 0); |
| 211 | currentBlock = Block::ID(insn.word(1)); |
| 212 | blockStart = insn; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 213 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 214 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 215 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 216 | // Branch Instructions (subset of Termination Instructions): |
| 217 | case spv::OpBranch: |
| 218 | case spv::OpBranchConditional: |
| 219 | case spv::OpSwitch: |
| 220 | case spv::OpReturn: |
| 221 | // [[fallthrough]] |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 222 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 223 | // Termination instruction: |
| 224 | case spv::OpKill: |
| 225 | case spv::OpUnreachable: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 226 | { |
| 227 | ASSERT(currentBlock.value() != 0); |
| 228 | ASSERT(currentFunction.value() != 0); |
| 229 | |
| 230 | auto blockEnd = insn; |
| 231 | blockEnd++; |
| 232 | functions[currentFunction].blocks[currentBlock] = Block(blockStart, blockEnd); |
| 233 | currentBlock = Block::ID(0); |
| 234 | |
| 235 | if(opcode == spv::OpKill) |
| 236 | { |
| 237 | modes.ContainsKill = true; |
| 238 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 239 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 240 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 241 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 242 | case spv::OpLoopMerge: |
| 243 | case spv::OpSelectionMerge: |
| 244 | break; // Nothing to do in analysis pass. |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 245 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 246 | case spv::OpTypeVoid: |
| 247 | case spv::OpTypeBool: |
| 248 | case spv::OpTypeInt: |
| 249 | case spv::OpTypeFloat: |
| 250 | case spv::OpTypeVector: |
| 251 | case spv::OpTypeMatrix: |
| 252 | case spv::OpTypeImage: |
| 253 | case spv::OpTypeSampler: |
| 254 | case spv::OpTypeSampledImage: |
| 255 | case spv::OpTypeArray: |
| 256 | case spv::OpTypeRuntimeArray: |
| 257 | case spv::OpTypeStruct: |
| 258 | case spv::OpTypePointer: |
| 259 | case spv::OpTypeFunction: |
| 260 | DeclareType(insn); |
| 261 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 262 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 263 | case spv::OpVariable: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 264 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 265 | Type::ID typeId = insn.word(1); |
| 266 | Object::ID resultId = insn.word(2); |
| 267 | auto storageClass = static_cast<spv::StorageClass>(insn.word(3)); |
| 268 | |
| 269 | auto &object = defs[resultId]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 270 | object.kind = Object::Kind::Pointer; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 271 | object.definition = insn; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 272 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 273 | ASSERT(getType(typeId).definition.opcode() == spv::OpTypePointer); |
| 274 | ASSERT(getType(typeId).storageClass == storageClass); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 275 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 276 | switch(storageClass) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 277 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 278 | case spv::StorageClassInput: |
| 279 | case spv::StorageClassOutput: |
Ben Clayton | 964b9e3 | 2021-06-25 09:00:22 +0100 | [diff] [blame] | 280 | if(interfaceIds.count(resultId)) |
| 281 | { |
| 282 | ProcessInterfaceVariable(object); |
| 283 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 284 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 285 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 286 | case spv::StorageClassUniform: |
| 287 | case spv::StorageClassStorageBuffer: |
| 288 | object.kind = Object::Kind::DescriptorSet; |
| 289 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 290 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 291 | case spv::StorageClassPushConstant: |
| 292 | case spv::StorageClassPrivate: |
| 293 | case spv::StorageClassFunction: |
| 294 | case spv::StorageClassUniformConstant: |
| 295 | break; // Correctly handled. |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 296 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 297 | case spv::StorageClassWorkgroup: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 298 | { |
| 299 | auto &elTy = getType(getType(typeId).element); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 300 | auto sizeInBytes = elTy.componentCount * static_cast<uint32_t>(sizeof(float)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 301 | workgroupMemory.allocate(resultId, sizeInBytes); |
| 302 | object.kind = Object::Kind::Pointer; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 303 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 304 | break; |
| 305 | case spv::StorageClassAtomicCounter: |
| 306 | case spv::StorageClassImage: |
| 307 | UNSUPPORTED("StorageClass %d not yet supported", (int)storageClass); |
| 308 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 309 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 310 | case spv::StorageClassCrossWorkgroup: |
| 311 | UNSUPPORTED("SPIR-V OpenCL Execution Model (StorageClassCrossWorkgroup)"); |
| 312 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 313 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 314 | case spv::StorageClassGeneric: |
| 315 | UNSUPPORTED("SPIR-V GenericPointer Capability (StorageClassGeneric)"); |
| 316 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 317 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 318 | default: |
| 319 | UNREACHABLE("Unexpected StorageClass %d", storageClass); // See Appendix A of the Vulkan spec. |
| 320 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 321 | } |
| 322 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 323 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 324 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 325 | case spv::OpConstant: |
| 326 | case spv::OpSpecConstant: |
| 327 | CreateConstant(insn).constantValue[0] = insn.word(3); |
| 328 | break; |
| 329 | case spv::OpConstantFalse: |
| 330 | case spv::OpSpecConstantFalse: |
| 331 | CreateConstant(insn).constantValue[0] = 0; // Represent Boolean false as zero. |
| 332 | break; |
| 333 | case spv::OpConstantTrue: |
| 334 | case spv::OpSpecConstantTrue: |
| 335 | CreateConstant(insn).constantValue[0] = ~0u; // Represent Boolean true as all bits set. |
| 336 | break; |
| 337 | case spv::OpConstantNull: |
| 338 | case spv::OpUndef: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 339 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 340 | // TODO: consider a real LLVM-level undef. For now, zero is a perfectly good value. |
| 341 | // OpConstantNull forms a constant of arbitrary type, all zeros. |
| 342 | auto &object = CreateConstant(insn); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 343 | auto &objectTy = getType(object); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 344 | for(auto i = 0u; i < objectTy.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 345 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 346 | object.constantValue[i] = 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 347 | } |
| 348 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 349 | break; |
| 350 | case spv::OpConstantComposite: |
| 351 | case spv::OpSpecConstantComposite: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 352 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 353 | auto &object = CreateConstant(insn); |
| 354 | auto offset = 0u; |
| 355 | for(auto i = 0u; i < insn.wordCount() - 3; i++) |
| 356 | { |
| 357 | auto &constituent = getObject(insn.word(i + 3)); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 358 | auto &constituentTy = getType(constituent); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 359 | for(auto j = 0u; j < constituentTy.componentCount; j++) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 360 | { |
| 361 | object.constantValue[offset++] = constituent.constantValue[j]; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | auto objectId = Object::ID(insn.word(2)); |
| 366 | auto decorationsIt = decorations.find(objectId); |
| 367 | if(decorationsIt != decorations.end() && |
| 368 | decorationsIt->second.BuiltIn == spv::BuiltInWorkgroupSize) |
| 369 | { |
| 370 | // https://www.khronos.org/registry/vulkan/specs/1.1/html/vkspec.html#interfaces-builtin-variables : |
| 371 | // Decorating an object with the WorkgroupSize built-in |
| 372 | // decoration will make that object contain the dimensions |
| 373 | // of a local workgroup. If an object is decorated with the |
| 374 | // WorkgroupSize decoration, this must take precedence over |
| 375 | // any execution mode set for LocalSize. |
| 376 | // The object decorated with WorkgroupSize must be declared |
| 377 | // as a three-component vector of 32-bit integers. |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 378 | ASSERT(getType(object).componentCount == 3); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 379 | modes.WorkgroupSizeX = object.constantValue[0]; |
| 380 | modes.WorkgroupSizeY = object.constantValue[1]; |
| 381 | modes.WorkgroupSizeZ = object.constantValue[2]; |
| 382 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 383 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 384 | break; |
| 385 | case spv::OpSpecConstantOp: |
| 386 | EvalSpecConstantOp(insn); |
| 387 | break; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 388 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 389 | case spv::OpCapability: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 390 | { |
| 391 | auto capability = static_cast<spv::Capability>(insn.word(1)); |
| 392 | switch(capability) |
| 393 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 394 | case spv::CapabilityMatrix: capabilities.Matrix = true; break; |
| 395 | case spv::CapabilityShader: capabilities.Shader = true; break; |
| 396 | case spv::CapabilityStorageImageMultisample: capabilities.StorageImageMultisample = true; break; |
| 397 | case spv::CapabilityClipDistance: capabilities.ClipDistance = true; break; |
| 398 | case spv::CapabilityCullDistance: capabilities.CullDistance = true; break; |
| 399 | case spv::CapabilityImageCubeArray: capabilities.ImageCubeArray = true; break; |
| 400 | case spv::CapabilitySampleRateShading: capabilities.SampleRateShading = true; break; |
| 401 | case spv::CapabilityInputAttachment: capabilities.InputAttachment = true; break; |
| 402 | case spv::CapabilitySampled1D: capabilities.Sampled1D = true; break; |
| 403 | case spv::CapabilityImage1D: capabilities.Image1D = true; break; |
| 404 | case spv::CapabilitySampledBuffer: capabilities.SampledBuffer = true; break; |
| 405 | case spv::CapabilitySampledCubeArray: capabilities.SampledCubeArray = true; break; |
| 406 | case spv::CapabilityImageBuffer: capabilities.ImageBuffer = true; break; |
| 407 | case spv::CapabilityImageMSArray: capabilities.ImageMSArray = true; break; |
| 408 | case spv::CapabilityStorageImageExtendedFormats: capabilities.StorageImageExtendedFormats = true; break; |
| 409 | case spv::CapabilityImageQuery: capabilities.ImageQuery = true; break; |
| 410 | case spv::CapabilityDerivativeControl: capabilities.DerivativeControl = true; break; |
| 411 | case spv::CapabilityInterpolationFunction: capabilities.InterpolationFunction = true; break; |
| 412 | case spv::CapabilityGroupNonUniform: capabilities.GroupNonUniform = true; break; |
| 413 | case spv::CapabilityGroupNonUniformVote: capabilities.GroupNonUniformVote = true; break; |
| 414 | case spv::CapabilityGroupNonUniformArithmetic: capabilities.GroupNonUniformArithmetic = true; break; |
| 415 | case spv::CapabilityGroupNonUniformBallot: capabilities.GroupNonUniformBallot = true; break; |
| 416 | case spv::CapabilityGroupNonUniformShuffle: capabilities.GroupNonUniformShuffle = true; break; |
| 417 | case spv::CapabilityGroupNonUniformShuffleRelative: capabilities.GroupNonUniformShuffleRelative = true; break; |
| 418 | case spv::CapabilityDeviceGroup: capabilities.DeviceGroup = true; break; |
| 419 | case spv::CapabilityMultiView: capabilities.MultiView = true; break; |
| 420 | case spv::CapabilityStencilExportEXT: capabilities.StencilExportEXT = true; break; |
| 421 | default: |
| 422 | UNSUPPORTED("Unsupported capability %u", insn.word(1)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 423 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 424 | |
| 425 | // Various capabilities will be declared, but none affect our code generation at this point. |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 426 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 427 | break; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 428 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 429 | case spv::OpMemoryModel: |
| 430 | break; // Memory model does not affect our code generation until we decide to do Vulkan Memory Model support. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 431 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 432 | case spv::OpFunction: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 433 | { |
| 434 | auto functionId = Function::ID(insn.word(2)); |
| 435 | ASSERT_MSG(currentFunction == 0, "Functions %d and %d overlap", currentFunction.value(), functionId.value()); |
| 436 | currentFunction = functionId; |
| 437 | auto &function = functions[functionId]; |
| 438 | function.result = Type::ID(insn.word(1)); |
| 439 | function.type = Type::ID(insn.word(4)); |
| 440 | // Scan forward to find the function's label. |
Ben Clayton | 7d09824 | 2020-03-13 13:07:12 +0000 | [diff] [blame] | 441 | for(auto it = insn; it != end(); it++) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 442 | { |
Ben Clayton | 7d09824 | 2020-03-13 13:07:12 +0000 | [diff] [blame] | 443 | if(it.opcode() == spv::OpLabel) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 444 | { |
Ben Clayton | 7d09824 | 2020-03-13 13:07:12 +0000 | [diff] [blame] | 445 | function.entry = Block::ID(it.word(1)); |
| 446 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | ASSERT_MSG(function.entry != 0, "Function<%d> has no label", currentFunction.value()); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 450 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 451 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 452 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 453 | case spv::OpFunctionEnd: |
| 454 | currentFunction = 0; |
| 455 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 456 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 457 | case spv::OpExtInstImport: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 458 | { |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 459 | static constexpr std::pair<const char *, Extension::Name> extensionsByName[] = { |
Ben Clayton | f6a6a41 | 2020-01-09 16:43:37 +0000 | [diff] [blame] | 460 | { "GLSL.std.450", Extension::GLSLstd450 }, |
Ben Clayton | cd55f05 | 2020-01-14 11:56:00 +0000 | [diff] [blame] | 461 | { "OpenCL.DebugInfo.100", Extension::OpenCLDebugInfo100 }, |
Ben Clayton | f6a6a41 | 2020-01-09 16:43:37 +0000 | [diff] [blame] | 462 | }; |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 463 | static constexpr auto extensionCount = sizeof(extensionsByName) / sizeof(extensionsByName[0]); |
| 464 | |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 465 | auto id = Extension::ID(insn.word(1)); |
| 466 | auto name = insn.string(2); |
| 467 | auto ext = Extension{ Extension::Unknown }; |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 468 | for(size_t i = 0; i < extensionCount; i++) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 469 | { |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 470 | if(0 == strcmp(name, extensionsByName[i].first)) |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 471 | { |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 472 | ext = Extension{ extensionsByName[i].second }; |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 473 | break; |
| 474 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 475 | } |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 476 | if(ext.name == Extension::Unknown) |
| 477 | { |
| 478 | UNSUPPORTED("SPIR-V Extension: %s", name); |
| 479 | break; |
| 480 | } |
Ben Clayton | 8842c8f | 2020-01-13 16:57:48 +0000 | [diff] [blame] | 481 | extensionsByID.emplace(id, ext); |
| 482 | extensionsImported.emplace(ext.name); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 483 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 484 | break; |
| 485 | case spv::OpName: |
| 486 | case spv::OpMemberName: |
| 487 | case spv::OpSource: |
| 488 | case spv::OpSourceContinued: |
| 489 | case spv::OpSourceExtension: |
| 490 | case spv::OpLine: |
| 491 | case spv::OpNoLine: |
| 492 | case spv::OpModuleProcessed: |
| 493 | // No semantic impact |
| 494 | break; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 495 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 496 | case spv::OpString: |
| 497 | strings.emplace(insn.word(1), insn.string(2)); |
| 498 | break; |
Ben Clayton | 9c8823a | 2020-01-08 12:07:30 +0000 | [diff] [blame] | 499 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 500 | case spv::OpFunctionParameter: |
| 501 | // These should have all been removed by preprocessing passes. If we see them here, |
| 502 | // our assumptions are wrong and we will probably generate wrong code. |
| 503 | UNREACHABLE("%s should have already been lowered.", OpcodeName(opcode)); |
| 504 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 505 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 506 | case spv::OpFunctionCall: |
| 507 | // TODO(b/141246700): Add full support for spv::OpFunctionCall |
| 508 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 509 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 510 | case spv::OpFConvert: |
| 511 | UNSUPPORTED("SPIR-V Float16 or Float64 Capability (OpFConvert)"); |
| 512 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 513 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 514 | case spv::OpSConvert: |
| 515 | UNSUPPORTED("SPIR-V Int16 or Int64 Capability (OpSConvert)"); |
| 516 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 517 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 518 | case spv::OpUConvert: |
| 519 | UNSUPPORTED("SPIR-V Int16 or Int64 Capability (OpUConvert)"); |
| 520 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 521 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 522 | case spv::OpLoad: |
| 523 | case spv::OpAccessChain: |
| 524 | case spv::OpInBoundsAccessChain: |
| 525 | case spv::OpSampledImage: |
| 526 | case spv::OpImage: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | { |
| 528 | // Propagate the descriptor decorations to the result. |
| 529 | Object::ID resultId = insn.word(2); |
| 530 | Object::ID pointerId = insn.word(3); |
| 531 | const auto &d = descriptorDecorations.find(pointerId); |
| 532 | |
| 533 | if(d != descriptorDecorations.end()) |
| 534 | { |
| 535 | descriptorDecorations[resultId] = d->second; |
| 536 | } |
| 537 | |
| 538 | DefineResult(insn); |
| 539 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 540 | if(opcode == spv::OpAccessChain || opcode == spv::OpInBoundsAccessChain) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 541 | { |
| 542 | Decorations dd{}; |
| 543 | ApplyDecorationsForAccessChain(&dd, &descriptorDecorations[resultId], pointerId, insn.wordCount() - 4, insn.wordPointer(4)); |
| 544 | // Note: offset is the one thing that does *not* propagate, as the access chain accounts for it. |
| 545 | dd.HasOffset = false; |
| 546 | decorations[resultId].Apply(dd); |
| 547 | } |
| 548 | } |
| 549 | break; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 550 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 551 | case spv::OpCompositeConstruct: |
| 552 | case spv::OpCompositeInsert: |
| 553 | case spv::OpCompositeExtract: |
| 554 | case spv::OpVectorShuffle: |
| 555 | case spv::OpVectorTimesScalar: |
| 556 | case spv::OpMatrixTimesScalar: |
| 557 | case spv::OpMatrixTimesVector: |
| 558 | case spv::OpVectorTimesMatrix: |
| 559 | case spv::OpMatrixTimesMatrix: |
| 560 | case spv::OpOuterProduct: |
| 561 | case spv::OpTranspose: |
| 562 | case spv::OpVectorExtractDynamic: |
| 563 | case spv::OpVectorInsertDynamic: |
| 564 | // Unary ops |
| 565 | case spv::OpNot: |
| 566 | case spv::OpBitFieldInsert: |
| 567 | case spv::OpBitFieldSExtract: |
| 568 | case spv::OpBitFieldUExtract: |
| 569 | case spv::OpBitReverse: |
| 570 | case spv::OpBitCount: |
| 571 | case spv::OpSNegate: |
| 572 | case spv::OpFNegate: |
| 573 | case spv::OpLogicalNot: |
| 574 | case spv::OpQuantizeToF16: |
| 575 | // Binary ops |
| 576 | case spv::OpIAdd: |
| 577 | case spv::OpISub: |
| 578 | case spv::OpIMul: |
| 579 | case spv::OpSDiv: |
| 580 | case spv::OpUDiv: |
| 581 | case spv::OpFAdd: |
| 582 | case spv::OpFSub: |
| 583 | case spv::OpFMul: |
| 584 | case spv::OpFDiv: |
| 585 | case spv::OpFMod: |
| 586 | case spv::OpFRem: |
| 587 | case spv::OpFOrdEqual: |
| 588 | case spv::OpFUnordEqual: |
| 589 | case spv::OpFOrdNotEqual: |
| 590 | case spv::OpFUnordNotEqual: |
| 591 | case spv::OpFOrdLessThan: |
| 592 | case spv::OpFUnordLessThan: |
| 593 | case spv::OpFOrdGreaterThan: |
| 594 | case spv::OpFUnordGreaterThan: |
| 595 | case spv::OpFOrdLessThanEqual: |
| 596 | case spv::OpFUnordLessThanEqual: |
| 597 | case spv::OpFOrdGreaterThanEqual: |
| 598 | case spv::OpFUnordGreaterThanEqual: |
| 599 | case spv::OpSMod: |
| 600 | case spv::OpSRem: |
| 601 | case spv::OpUMod: |
| 602 | case spv::OpIEqual: |
| 603 | case spv::OpINotEqual: |
| 604 | case spv::OpUGreaterThan: |
| 605 | case spv::OpSGreaterThan: |
| 606 | case spv::OpUGreaterThanEqual: |
| 607 | case spv::OpSGreaterThanEqual: |
| 608 | case spv::OpULessThan: |
| 609 | case spv::OpSLessThan: |
| 610 | case spv::OpULessThanEqual: |
| 611 | case spv::OpSLessThanEqual: |
| 612 | case spv::OpShiftRightLogical: |
| 613 | case spv::OpShiftRightArithmetic: |
| 614 | case spv::OpShiftLeftLogical: |
| 615 | case spv::OpBitwiseOr: |
| 616 | case spv::OpBitwiseXor: |
| 617 | case spv::OpBitwiseAnd: |
| 618 | case spv::OpLogicalOr: |
| 619 | case spv::OpLogicalAnd: |
| 620 | case spv::OpLogicalEqual: |
| 621 | case spv::OpLogicalNotEqual: |
| 622 | case spv::OpUMulExtended: |
| 623 | case spv::OpSMulExtended: |
| 624 | case spv::OpIAddCarry: |
| 625 | case spv::OpISubBorrow: |
| 626 | case spv::OpDot: |
| 627 | case spv::OpConvertFToU: |
| 628 | case spv::OpConvertFToS: |
| 629 | case spv::OpConvertSToF: |
| 630 | case spv::OpConvertUToF: |
| 631 | case spv::OpBitcast: |
| 632 | case spv::OpSelect: |
| 633 | case spv::OpIsInf: |
| 634 | case spv::OpIsNan: |
| 635 | case spv::OpAny: |
| 636 | case spv::OpAll: |
| 637 | case spv::OpDPdx: |
| 638 | case spv::OpDPdxCoarse: |
| 639 | case spv::OpDPdy: |
| 640 | case spv::OpDPdyCoarse: |
| 641 | case spv::OpFwidth: |
| 642 | case spv::OpFwidthCoarse: |
| 643 | case spv::OpDPdxFine: |
| 644 | case spv::OpDPdyFine: |
| 645 | case spv::OpFwidthFine: |
| 646 | case spv::OpAtomicLoad: |
| 647 | case spv::OpAtomicIAdd: |
| 648 | case spv::OpAtomicISub: |
| 649 | case spv::OpAtomicSMin: |
| 650 | case spv::OpAtomicSMax: |
| 651 | case spv::OpAtomicUMin: |
| 652 | case spv::OpAtomicUMax: |
| 653 | case spv::OpAtomicAnd: |
| 654 | case spv::OpAtomicOr: |
| 655 | case spv::OpAtomicXor: |
| 656 | case spv::OpAtomicIIncrement: |
| 657 | case spv::OpAtomicIDecrement: |
| 658 | case spv::OpAtomicExchange: |
| 659 | case spv::OpAtomicCompareExchange: |
| 660 | case spv::OpPhi: |
| 661 | case spv::OpImageSampleImplicitLod: |
| 662 | case spv::OpImageSampleExplicitLod: |
| 663 | case spv::OpImageSampleDrefImplicitLod: |
| 664 | case spv::OpImageSampleDrefExplicitLod: |
| 665 | case spv::OpImageSampleProjImplicitLod: |
| 666 | case spv::OpImageSampleProjExplicitLod: |
| 667 | case spv::OpImageSampleProjDrefImplicitLod: |
| 668 | case spv::OpImageSampleProjDrefExplicitLod: |
| 669 | case spv::OpImageGather: |
| 670 | case spv::OpImageDrefGather: |
| 671 | case spv::OpImageFetch: |
| 672 | case spv::OpImageQuerySizeLod: |
| 673 | case spv::OpImageQuerySize: |
| 674 | case spv::OpImageQueryLod: |
| 675 | case spv::OpImageQueryLevels: |
| 676 | case spv::OpImageQuerySamples: |
| 677 | case spv::OpImageRead: |
| 678 | case spv::OpImageTexelPointer: |
| 679 | case spv::OpGroupNonUniformElect: |
| 680 | case spv::OpGroupNonUniformAll: |
| 681 | case spv::OpGroupNonUniformAny: |
| 682 | case spv::OpGroupNonUniformAllEqual: |
| 683 | case spv::OpGroupNonUniformBroadcast: |
| 684 | case spv::OpGroupNonUniformBroadcastFirst: |
| 685 | case spv::OpGroupNonUniformBallot: |
| 686 | case spv::OpGroupNonUniformInverseBallot: |
| 687 | case spv::OpGroupNonUniformBallotBitExtract: |
| 688 | case spv::OpGroupNonUniformBallotBitCount: |
| 689 | case spv::OpGroupNonUniformBallotFindLSB: |
| 690 | case spv::OpGroupNonUniformBallotFindMSB: |
| 691 | case spv::OpGroupNonUniformShuffle: |
| 692 | case spv::OpGroupNonUniformShuffleXor: |
| 693 | case spv::OpGroupNonUniformShuffleUp: |
| 694 | case spv::OpGroupNonUniformShuffleDown: |
| 695 | case spv::OpGroupNonUniformIAdd: |
| 696 | case spv::OpGroupNonUniformFAdd: |
| 697 | case spv::OpGroupNonUniformIMul: |
| 698 | case spv::OpGroupNonUniformFMul: |
| 699 | case spv::OpGroupNonUniformSMin: |
| 700 | case spv::OpGroupNonUniformUMin: |
| 701 | case spv::OpGroupNonUniformFMin: |
| 702 | case spv::OpGroupNonUniformSMax: |
| 703 | case spv::OpGroupNonUniformUMax: |
| 704 | case spv::OpGroupNonUniformFMax: |
| 705 | case spv::OpGroupNonUniformBitwiseAnd: |
| 706 | case spv::OpGroupNonUniformBitwiseOr: |
| 707 | case spv::OpGroupNonUniformBitwiseXor: |
| 708 | case spv::OpGroupNonUniformLogicalAnd: |
| 709 | case spv::OpGroupNonUniformLogicalOr: |
| 710 | case spv::OpGroupNonUniformLogicalXor: |
| 711 | case spv::OpCopyObject: |
| 712 | case spv::OpCopyLogical: |
| 713 | case spv::OpArrayLength: |
| 714 | // Instructions that yield an intermediate value or divergent pointer |
| 715 | DefineResult(insn); |
| 716 | break; |
| 717 | |
| 718 | case spv::OpExtInst: |
| 719 | switch(getExtension(insn.word(3)).name) |
| 720 | { |
| 721 | case Extension::GLSLstd450: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 722 | DefineResult(insn); |
| 723 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 724 | case Extension::OpenCLDebugInfo100: |
| 725 | DefineOpenCLDebugInfo100(insn); |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 726 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 727 | default: |
| 728 | UNREACHABLE("Unexpected Extension name %d", int(getExtension(insn.word(3)).name)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 729 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 730 | } |
| 731 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 732 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 733 | case spv::OpStore: |
| 734 | case spv::OpAtomicStore: |
| 735 | case spv::OpImageWrite: |
| 736 | case spv::OpCopyMemory: |
| 737 | case spv::OpMemoryBarrier: |
| 738 | // Don't need to do anything during analysis pass |
| 739 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 740 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 741 | case spv::OpControlBarrier: |
| 742 | modes.ContainsControlBarriers = true; |
| 743 | break; |
| 744 | |
| 745 | case spv::OpExtension: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 746 | { |
| 747 | auto ext = insn.string(1); |
| 748 | // Part of core SPIR-V 1.3. Vulkan 1.1 implementations must also accept the pre-1.3 |
| 749 | // extension per Appendix A, `Vulkan Environment for SPIR-V`. |
| 750 | if(!strcmp(ext, "SPV_KHR_storage_buffer_storage_class")) break; |
| 751 | if(!strcmp(ext, "SPV_KHR_shader_draw_parameters")) break; |
| 752 | if(!strcmp(ext, "SPV_KHR_16bit_storage")) break; |
| 753 | if(!strcmp(ext, "SPV_KHR_variable_pointers")) break; |
| 754 | if(!strcmp(ext, "SPV_KHR_device_group")) break; |
| 755 | if(!strcmp(ext, "SPV_KHR_multiview")) break; |
Alexis Hetu | 1ee36c9 | 2020-02-20 14:07:26 -0500 | [diff] [blame] | 756 | if(!strcmp(ext, "SPV_EXT_shader_stencil_export")) break; |
Sean Risser | 0063eca | 2020-10-26 17:08:42 -0400 | [diff] [blame] | 757 | if(!strcmp(ext, "SPV_KHR_float_controls")) break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 758 | UNSUPPORTED("SPIR-V Extension: %s", ext); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 759 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 760 | break; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 761 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 762 | default: |
| 763 | UNSUPPORTED("%s", OpcodeName(opcode)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 764 | } |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 765 | } |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 766 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 767 | ASSERT_MSG(entryPoint != 0, "Entry point '%s' not found", entryPointName); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 768 | for(auto &it : functions) |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 769 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 770 | it.second.AssignBlockFields(); |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 771 | } |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 772 | |
Ben Clayton | 0d6791c | 2020-04-22 21:55:27 +0100 | [diff] [blame] | 773 | #ifdef SPIRV_SHADER_CFG_GRAPHVIZ_DOT_FILEPATH |
| 774 | { |
| 775 | char path[1024]; |
| 776 | snprintf(path, sizeof(path), SPIRV_SHADER_CFG_GRAPHVIZ_DOT_FILEPATH, codeSerialID); |
| 777 | WriteCFGGraphVizDotFile(path); |
| 778 | } |
| 779 | #endif |
| 780 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 781 | dbgCreateFile(); |
| 782 | } |
| 783 | |
| 784 | SpirvShader::~SpirvShader() |
| 785 | { |
| 786 | dbgTerm(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 787 | } |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 788 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 789 | void SpirvShader::DeclareType(InsnIterator insn) |
| 790 | { |
| 791 | Type::ID resultId = insn.word(1); |
| 792 | |
| 793 | auto &type = types[resultId]; |
| 794 | type.definition = insn; |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 795 | type.componentCount = ComputeTypeSize(insn); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 796 | |
| 797 | // A structure is a builtin block if it has a builtin |
| 798 | // member. All members of such a structure are builtins. |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 799 | switch(insn.opcode()) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 800 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 801 | case spv::OpTypeStruct: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 802 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 803 | auto d = memberDecorations.find(resultId); |
| 804 | if(d != memberDecorations.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 805 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 806 | for(auto &m : d->second) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 807 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 808 | if(m.HasBuiltIn) |
| 809 | { |
| 810 | type.isBuiltInBlock = true; |
| 811 | break; |
| 812 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 816 | break; |
| 817 | case spv::OpTypePointer: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 818 | { |
| 819 | Type::ID elementTypeId = insn.word(3); |
| 820 | type.element = elementTypeId; |
| 821 | type.isBuiltInBlock = getType(elementTypeId).isBuiltInBlock; |
| 822 | type.storageClass = static_cast<spv::StorageClass>(insn.word(2)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 823 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 824 | break; |
| 825 | case spv::OpTypeVector: |
| 826 | case spv::OpTypeMatrix: |
| 827 | case spv::OpTypeArray: |
| 828 | case spv::OpTypeRuntimeArray: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 829 | { |
| 830 | Type::ID elementTypeId = insn.word(2); |
| 831 | type.element = elementTypeId; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 832 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 833 | break; |
| 834 | default: |
| 835 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 839 | SpirvShader::Object &SpirvShader::CreateConstant(InsnIterator insn) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 840 | { |
| 841 | Type::ID typeId = insn.word(1); |
| 842 | Object::ID resultId = insn.word(2); |
| 843 | auto &object = defs[resultId]; |
| 844 | auto &objectTy = getType(typeId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 845 | object.kind = Object::Kind::Constant; |
| 846 | object.definition = insn; |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 847 | object.constantValue.resize(objectTy.componentCount); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 848 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 849 | return object; |
| 850 | } |
| 851 | |
| 852 | void SpirvShader::ProcessInterfaceVariable(Object &object) |
| 853 | { |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 854 | auto &objectTy = getType(object); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 855 | ASSERT(objectTy.storageClass == spv::StorageClassInput || objectTy.storageClass == spv::StorageClassOutput); |
| 856 | |
| 857 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
| 858 | auto pointeeTy = getType(objectTy.element); |
| 859 | |
| 860 | auto &builtinInterface = (objectTy.storageClass == spv::StorageClassInput) ? inputBuiltins : outputBuiltins; |
| 861 | auto &userDefinedInterface = (objectTy.storageClass == spv::StorageClassInput) ? inputs : outputs; |
| 862 | |
| 863 | ASSERT(object.opcode() == spv::OpVariable); |
| 864 | Object::ID resultId = object.definition.word(2); |
| 865 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 866 | if(objectTy.isBuiltInBlock) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 867 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 868 | // Walk the builtin block, registering each of its members separately. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 869 | auto m = memberDecorations.find(objectTy.element); |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 870 | ASSERT(m != memberDecorations.end()); // Otherwise we wouldn't have marked the type chain |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 871 | auto &structType = pointeeTy.definition; |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 872 | auto memberIndex = 0u; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 873 | auto offset = 0u; |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 874 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 875 | for(auto &member : m->second) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 876 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 877 | auto &memberType = getType(structType.word(2 + memberIndex)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 878 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 879 | if(member.HasBuiltIn) |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 880 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 881 | builtinInterface[member.BuiltIn] = { resultId, offset, memberType.componentCount }; |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 882 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 883 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 884 | offset += memberType.componentCount; |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 885 | ++memberIndex; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 886 | } |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 887 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 888 | return; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 891 | auto d = decorations.find(resultId); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 892 | if(d != decorations.end() && d->second.HasBuiltIn) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 893 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 894 | builtinInterface[d->second.BuiltIn] = { resultId, 0, pointeeTy.componentCount }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 895 | } |
| 896 | else |
| 897 | { |
| 898 | object.kind = Object::Kind::InterfaceVariable; |
| 899 | VisitInterface(resultId, |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 900 | [&userDefinedInterface](Decorations const &d, AttribType type) { |
| 901 | // Populate a single scalar slot in the interface from a collection of decorations and the intended component type. |
| 902 | auto scalarSlot = (d.Location << 2) | d.Component; |
| 903 | ASSERT(scalarSlot >= 0 && |
| 904 | scalarSlot < static_cast<int32_t>(userDefinedInterface.size())); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 905 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 906 | auto &slot = userDefinedInterface[scalarSlot]; |
| 907 | slot.Type = type; |
| 908 | slot.Flat = d.Flat; |
| 909 | slot.NoPerspective = d.NoPerspective; |
| 910 | slot.Centroid = d.Centroid; |
| 911 | }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 912 | } |
| 913 | } |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 914 | |
Alexis Hetu | 945f144 | 2021-01-14 11:39:56 -0500 | [diff] [blame] | 915 | uint32_t SpirvShader::GetNumInputComponents(int32_t location) const |
| 916 | { |
| 917 | ASSERT(location >= 0); |
| 918 | |
| 919 | // Verify how many component(s) per input |
| 920 | // 1 to 4, for float, vec2, vec3, vec4. |
| 921 | // Note that matrices are divided over multiple inputs |
| 922 | uint32_t num_components_per_input = 0; |
| 923 | for(; num_components_per_input < 4; ++num_components_per_input) |
| 924 | { |
| 925 | if(inputs[(location << 2) | num_components_per_input].Type == ATTRIBTYPE_UNUSED) |
| 926 | { |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | return num_components_per_input; |
| 932 | } |
| 933 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 934 | void SpirvShader::ProcessExecutionMode(InsnIterator insn) |
| 935 | { |
Nicolas Capens | 57eb48a | 2020-05-15 17:07:07 -0400 | [diff] [blame] | 936 | Function::ID function = insn.word(1); |
| 937 | if(function != entryPoint) |
| 938 | { |
| 939 | return; |
| 940 | } |
| 941 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 942 | auto mode = static_cast<spv::ExecutionMode>(insn.word(2)); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 943 | switch(mode) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 944 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 945 | case spv::ExecutionModeEarlyFragmentTests: |
| 946 | modes.EarlyFragmentTests = true; |
| 947 | break; |
| 948 | case spv::ExecutionModeDepthReplacing: |
| 949 | modes.DepthReplacing = true; |
| 950 | break; |
| 951 | case spv::ExecutionModeDepthGreater: |
| 952 | // TODO(b/177915067): Can be used to optimize depth test, currently unused. |
| 953 | modes.DepthGreater = true; |
| 954 | break; |
| 955 | case spv::ExecutionModeDepthLess: |
| 956 | // TODO(b/177915067): Can be used to optimize depth test, currently unused. |
| 957 | modes.DepthLess = true; |
| 958 | break; |
| 959 | case spv::ExecutionModeDepthUnchanged: |
| 960 | // TODO(b/177915067): Can be used to optimize depth test, currently unused. |
| 961 | modes.DepthUnchanged = true; |
| 962 | break; |
| 963 | case spv::ExecutionModeLocalSize: |
| 964 | modes.WorkgroupSizeX = insn.word(3); |
| 965 | modes.WorkgroupSizeY = insn.word(4); |
| 966 | modes.WorkgroupSizeZ = insn.word(5); |
| 967 | break; |
| 968 | case spv::ExecutionModeOriginUpperLeft: |
| 969 | // This is always the case for a Vulkan shader. Do nothing. |
| 970 | break; |
| 971 | default: |
| 972 | UNREACHABLE("Execution mode: %d", int(mode)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 973 | } |
| 974 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 975 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 976 | uint32_t SpirvShader::ComputeTypeSize(InsnIterator insn) |
| 977 | { |
| 978 | // Types are always built from the bottom up (with the exception of forward ptrs, which |
| 979 | // don't appear in Vulkan shaders. Therefore, we can always assume our component parts have |
| 980 | // already been described (and so their sizes determined) |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 981 | switch(insn.opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 982 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 983 | case spv::OpTypeVoid: |
| 984 | case spv::OpTypeSampler: |
| 985 | case spv::OpTypeImage: |
| 986 | case spv::OpTypeSampledImage: |
| 987 | case spv::OpTypeFunction: |
| 988 | case spv::OpTypeRuntimeArray: |
| 989 | // Objects that don't consume any space. |
| 990 | // Descriptor-backed objects currently only need exist at compile-time. |
| 991 | // Runtime arrays don't appear in places where their size would be interesting |
| 992 | return 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 993 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 994 | case spv::OpTypeBool: |
| 995 | case spv::OpTypeFloat: |
| 996 | case spv::OpTypeInt: |
| 997 | // All the fundamental types are 1 component. If we ever add support for 8/16/64-bit components, |
| 998 | // we might need to change this, but only 32 bit components are required for Vulkan 1.1. |
| 999 | return 1; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1000 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1001 | case spv::OpTypeVector: |
| 1002 | case spv::OpTypeMatrix: |
| 1003 | // Vectors and matrices both consume element count * element size. |
| 1004 | return getType(insn.word(2)).componentCount * insn.word(3); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1005 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1006 | case spv::OpTypeArray: |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1007 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1008 | // Element count * element size. Array sizes come from constant ids. |
| 1009 | auto arraySize = GetConstScalarInt(insn.word(3)); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1010 | return getType(insn.word(2)).componentCount * arraySize; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1011 | } |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1012 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1013 | case spv::OpTypeStruct: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1014 | { |
| 1015 | uint32_t size = 0; |
| 1016 | for(uint32_t i = 2u; i < insn.wordCount(); i++) |
| 1017 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1018 | size += getType(insn.word(i)).componentCount; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1019 | } |
| 1020 | return size; |
| 1021 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1022 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1023 | case spv::OpTypePointer: |
| 1024 | // Runtime representation of a pointer is a per-lane index. |
| 1025 | // Note: clients are expected to look through the pointer if they want the pointee size instead. |
| 1026 | return 1; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1027 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1028 | default: |
| 1029 | UNREACHABLE("%s", OpcodeName(insn.opcode())); |
| 1030 | return 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | int SpirvShader::VisitInterfaceInner(Type::ID id, Decorations d, const InterfaceVisitor &f) const |
| 1035 | { |
| 1036 | // Recursively walks variable definition and its type tree, taking into account |
| 1037 | // any explicit Location or Component decorations encountered; where explicit |
| 1038 | // Locations or Components are not specified, assigns them sequentially. |
| 1039 | // Collected decorations are carried down toward the leaves and across |
| 1040 | // siblings; Effect of decorations intentionally does not flow back up the tree. |
| 1041 | // |
| 1042 | // F is a functor to be called with the effective decoration set for every component. |
| 1043 | // |
| 1044 | // Returns the next available location, and calls f(). |
| 1045 | |
| 1046 | // This covers the rules in Vulkan 1.1 spec, 14.1.4 Location Assignment. |
| 1047 | |
| 1048 | ApplyDecorationsForId(&d, id); |
| 1049 | |
| 1050 | auto const &obj = getType(id); |
| 1051 | switch(obj.opcode()) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1052 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1053 | case spv::OpTypePointer: |
| 1054 | return VisitInterfaceInner(obj.definition.word(3), d, f); |
| 1055 | case spv::OpTypeMatrix: |
| 1056 | for(auto i = 0u; i < obj.definition.word(3); i++, d.Location++) |
| 1057 | { |
| 1058 | // consumes same components of N consecutive locations |
| 1059 | VisitInterfaceInner(obj.definition.word(2), d, f); |
| 1060 | } |
| 1061 | return d.Location; |
| 1062 | case spv::OpTypeVector: |
| 1063 | for(auto i = 0u; i < obj.definition.word(3); i++, d.Component++) |
| 1064 | { |
| 1065 | // consumes N consecutive components in the same location |
| 1066 | VisitInterfaceInner(obj.definition.word(2), d, f); |
| 1067 | } |
| 1068 | return d.Location + 1; |
| 1069 | case spv::OpTypeFloat: |
| 1070 | f(d, ATTRIBTYPE_FLOAT); |
| 1071 | return d.Location + 1; |
| 1072 | case spv::OpTypeInt: |
| 1073 | f(d, obj.definition.word(3) ? ATTRIBTYPE_INT : ATTRIBTYPE_UINT); |
| 1074 | return d.Location + 1; |
| 1075 | case spv::OpTypeBool: |
| 1076 | f(d, ATTRIBTYPE_UINT); |
| 1077 | return d.Location + 1; |
| 1078 | case spv::OpTypeStruct: |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1079 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1080 | // iterate over members, which may themselves have Location/Component decorations |
| 1081 | for(auto i = 0u; i < obj.definition.wordCount() - 2; i++) |
| 1082 | { |
Alexis Hetu | b654080 | 2020-08-11 18:12:03 -0400 | [diff] [blame] | 1083 | Decorations dMember = d; |
| 1084 | ApplyDecorationsForIdMember(&dMember, id, i); |
| 1085 | d.Location = VisitInterfaceInner(obj.definition.word(i + 2), dMember, f); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1086 | d.Component = 0; // Implicit locations always have component=0 |
| 1087 | } |
| 1088 | return d.Location; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1089 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1090 | case spv::OpTypeArray: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1091 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1092 | auto arraySize = GetConstScalarInt(obj.definition.word(3)); |
| 1093 | for(auto i = 0u; i < arraySize; i++) |
| 1094 | { |
| 1095 | d.Location = VisitInterfaceInner(obj.definition.word(2), d, f); |
| 1096 | } |
| 1097 | return d.Location; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1098 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1099 | default: |
| 1100 | // Intentionally partial; most opcodes do not participate in type hierarchies |
| 1101 | return 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void SpirvShader::VisitInterface(Object::ID id, const InterfaceVisitor &f) const |
| 1106 | { |
| 1107 | // Walk a variable definition and call f for each component in it. |
| 1108 | Decorations d{}; |
| 1109 | ApplyDecorationsForId(&d, id); |
| 1110 | |
| 1111 | auto def = getObject(id).definition; |
| 1112 | ASSERT(def.opcode() == spv::OpVariable); |
| 1113 | VisitInterfaceInner(def.word(1), d, f); |
| 1114 | } |
| 1115 | |
| 1116 | void SpirvShader::ApplyDecorationsForAccessChain(Decorations *d, DescriptorDecorations *dd, Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds) const |
| 1117 | { |
| 1118 | ApplyDecorationsForId(d, baseId); |
| 1119 | auto &baseObject = getObject(baseId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 1120 | ApplyDecorationsForId(d, baseObject.typeId()); |
| 1121 | auto typeId = getType(baseObject).element; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1122 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1123 | for(auto i = 0u; i < numIndexes; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1124 | { |
| 1125 | ApplyDecorationsForId(d, typeId); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1126 | auto &type = getType(typeId); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1127 | switch(type.opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1128 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1129 | case spv::OpTypeStruct: |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1130 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1131 | int memberIndex = GetConstScalarInt(indexIds[i]); |
| 1132 | ApplyDecorationsForIdMember(d, typeId, memberIndex); |
| 1133 | typeId = type.definition.word(2u + memberIndex); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1134 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1135 | break; |
| 1136 | case spv::OpTypeArray: |
| 1137 | case spv::OpTypeRuntimeArray: |
| 1138 | if(dd->InputAttachmentIndex >= 0) |
| 1139 | { |
| 1140 | dd->InputAttachmentIndex += GetConstScalarInt(indexIds[i]); |
| 1141 | } |
| 1142 | typeId = type.element; |
| 1143 | break; |
| 1144 | case spv::OpTypeVector: |
| 1145 | typeId = type.element; |
| 1146 | break; |
| 1147 | case spv::OpTypeMatrix: |
| 1148 | typeId = type.element; |
| 1149 | d->InsideMatrix = true; |
| 1150 | break; |
| 1151 | default: |
| 1152 | UNREACHABLE("%s", OpcodeName(type.definition.opcode())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | SIMD::Pointer SpirvShader::WalkExplicitLayoutAccessChain(Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds, EmitState const *state) const |
| 1158 | { |
| 1159 | // Produce a offset into external memory in sizeof(float) units |
| 1160 | |
| 1161 | auto &baseObject = getObject(baseId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 1162 | Type::ID typeId = getType(baseObject).element; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1163 | Decorations d = {}; |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 1164 | ApplyDecorationsForId(&d, baseObject.typeId()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1165 | |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 1166 | Int arrayIndex = 0; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1167 | if(baseObject.kind == Object::Kind::DescriptorSet) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1168 | { |
| 1169 | auto type = getType(typeId).definition.opcode(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1170 | if(type == spv::OpTypeArray || type == spv::OpTypeRuntimeArray) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1171 | { |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 1172 | auto &obj = getObject(indexIds[0]); |
| 1173 | ASSERT(obj.kind == Object::Kind::Constant || obj.kind == Object::Kind::Intermediate); |
| 1174 | if(obj.kind == Object::Kind::Constant) |
| 1175 | { |
| 1176 | arrayIndex = GetConstScalarInt(indexIds[0]); |
| 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | // Note: the value of indexIds[0] must be dynamically uniform. |
| 1181 | arrayIndex = Extract(state->getIntermediate(indexIds[0]).Int(0), 0); |
| 1182 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1183 | |
| 1184 | numIndexes--; |
| 1185 | indexIds++; |
| 1186 | typeId = getType(typeId).element; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | auto ptr = GetPointerToData(baseId, arrayIndex, state); |
| 1191 | |
| 1192 | int constantOffset = 0; |
| 1193 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1194 | for(auto i = 0u; i < numIndexes; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1195 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1196 | auto &type = getType(typeId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1197 | ApplyDecorationsForId(&d, typeId); |
| 1198 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1199 | switch(type.definition.opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1200 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1201 | case spv::OpTypeStruct: |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1202 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1203 | int memberIndex = GetConstScalarInt(indexIds[i]); |
| 1204 | ApplyDecorationsForIdMember(&d, typeId, memberIndex); |
| 1205 | ASSERT(d.HasOffset); |
| 1206 | constantOffset += d.Offset; |
| 1207 | typeId = type.definition.word(2u + memberIndex); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1208 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1209 | break; |
| 1210 | case spv::OpTypeArray: |
| 1211 | case spv::OpTypeRuntimeArray: |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1212 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1213 | // TODO: b/127950082: Check bounds. |
| 1214 | ASSERT(d.HasArrayStride); |
| 1215 | auto &obj = getObject(indexIds[i]); |
| 1216 | if(obj.kind == Object::Kind::Constant) |
| 1217 | { |
| 1218 | constantOffset += d.ArrayStride * GetConstScalarInt(indexIds[i]); |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | ptr += SIMD::Int(d.ArrayStride) * state->getIntermediate(indexIds[i]).Int(0); |
| 1223 | } |
| 1224 | typeId = type.element; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 1225 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1226 | break; |
| 1227 | case spv::OpTypeMatrix: |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1228 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1229 | // TODO: b/127950082: Check bounds. |
| 1230 | ASSERT(d.HasMatrixStride); |
| 1231 | d.InsideMatrix = true; |
| 1232 | auto columnStride = (d.HasRowMajor && d.RowMajor) ? static_cast<int32_t>(sizeof(float)) : d.MatrixStride; |
| 1233 | auto &obj = getObject(indexIds[i]); |
| 1234 | if(obj.kind == Object::Kind::Constant) |
| 1235 | { |
| 1236 | constantOffset += columnStride * GetConstScalarInt(indexIds[i]); |
| 1237 | } |
| 1238 | else |
| 1239 | { |
| 1240 | ptr += SIMD::Int(columnStride) * state->getIntermediate(indexIds[i]).Int(0); |
| 1241 | } |
| 1242 | typeId = type.element; |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1243 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1244 | break; |
| 1245 | case spv::OpTypeVector: |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1246 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1247 | auto elemStride = (d.InsideMatrix && d.HasRowMajor && d.RowMajor) ? d.MatrixStride : static_cast<int32_t>(sizeof(float)); |
| 1248 | auto &obj = getObject(indexIds[i]); |
| 1249 | if(obj.kind == Object::Kind::Constant) |
| 1250 | { |
| 1251 | constantOffset += elemStride * GetConstScalarInt(indexIds[i]); |
| 1252 | } |
| 1253 | else |
| 1254 | { |
| 1255 | ptr += SIMD::Int(elemStride) * state->getIntermediate(indexIds[i]).Int(0); |
| 1256 | } |
| 1257 | typeId = type.element; |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1258 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1259 | break; |
| 1260 | default: |
| 1261 | UNREACHABLE("%s", OpcodeName(type.definition.opcode())); |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1265 | ptr += constantOffset; |
| 1266 | return ptr; |
| 1267 | } |
Ben Clayton | 59cd59b | 2019-06-25 19:07:48 +0100 | [diff] [blame] | 1268 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1269 | SIMD::Pointer SpirvShader::WalkAccessChain(Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds, EmitState const *state) const |
| 1270 | { |
| 1271 | // TODO: avoid doing per-lane work in some cases if we can? |
| 1272 | auto routine = state->routine; |
| 1273 | auto &baseObject = getObject(baseId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 1274 | Type::ID typeId = getType(baseObject).element; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1275 | |
| 1276 | auto ptr = state->getPointer(baseId); |
| 1277 | |
| 1278 | int constantOffset = 0; |
| 1279 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1280 | for(auto i = 0u; i < numIndexes; i++) |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1281 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1282 | auto &type = getType(typeId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1283 | switch(type.opcode()) |
| 1284 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1285 | case spv::OpTypeStruct: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1286 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1287 | int memberIndex = GetConstScalarInt(indexIds[i]); |
| 1288 | int offsetIntoStruct = 0; |
| 1289 | for(auto j = 0; j < memberIndex; j++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1290 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1291 | auto memberType = type.definition.word(2u + j); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1292 | offsetIntoStruct += getType(memberType).componentCount * sizeof(float); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1293 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1294 | constantOffset += offsetIntoStruct; |
| 1295 | typeId = type.definition.word(2u + memberIndex); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1296 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1297 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1298 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1299 | case spv::OpTypeVector: |
| 1300 | case spv::OpTypeMatrix: |
| 1301 | case spv::OpTypeArray: |
| 1302 | case spv::OpTypeRuntimeArray: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1303 | { |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 1304 | // TODO(b/127950082): Check bounds. |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 1305 | if(getType(baseObject).storageClass == spv::StorageClassUniformConstant) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1306 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1307 | // indexing into an array of descriptors. |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1308 | auto d = descriptorDecorations.at(baseId); |
| 1309 | ASSERT(d.DescriptorSet >= 0); |
| 1310 | ASSERT(d.Binding >= 0); |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 1311 | uint32_t descriptorSize = routine->pipelineLayout->getDescriptorSize(d.DescriptorSet, d.Binding); |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 1312 | |
| 1313 | auto &obj = getObject(indexIds[i]); |
| 1314 | if(obj.kind == Object::Kind::Constant) |
| 1315 | { |
| 1316 | ptr.base += descriptorSize * GetConstScalarInt(indexIds[i]); |
| 1317 | } |
| 1318 | else |
| 1319 | { |
| 1320 | // Note: the value of indexIds[i] must be dynamically uniform. |
| 1321 | ptr.base += descriptorSize * Extract(state->getIntermediate(indexIds[i]).Int(0), 0); |
| 1322 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1323 | } |
| 1324 | else |
| 1325 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1326 | auto stride = getType(type.element).componentCount * static_cast<uint32_t>(sizeof(float)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1327 | auto &obj = getObject(indexIds[i]); |
| 1328 | if(obj.kind == Object::Kind::Constant) |
| 1329 | { |
| 1330 | ptr += stride * GetConstScalarInt(indexIds[i]); |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | ptr += SIMD::Int(stride) * state->getIntermediate(indexIds[i]).Int(0); |
| 1335 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1336 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1337 | typeId = type.element; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1338 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1339 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1340 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1341 | default: |
| 1342 | UNREACHABLE("%s", OpcodeName(type.opcode())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1343 | } |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1346 | if(constantOffset != 0) |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1347 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1348 | ptr += constantOffset; |
| 1349 | } |
| 1350 | return ptr; |
| 1351 | } |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1352 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1353 | uint32_t SpirvShader::WalkLiteralAccessChain(Type::ID typeId, uint32_t numIndexes, uint32_t const *indexes) const |
| 1354 | { |
| 1355 | uint32_t componentOffset = 0; |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1356 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1357 | for(auto i = 0u; i < numIndexes; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1358 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1359 | auto &type = getType(typeId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1360 | switch(type.opcode()) |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1361 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1362 | case spv::OpTypeStruct: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1363 | { |
| 1364 | int memberIndex = indexes[i]; |
| 1365 | int offsetIntoStruct = 0; |
| 1366 | for(auto j = 0; j < memberIndex; j++) |
| 1367 | { |
| 1368 | auto memberType = type.definition.word(2u + j); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1369 | offsetIntoStruct += getType(memberType).componentCount; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1370 | } |
| 1371 | componentOffset += offsetIntoStruct; |
| 1372 | typeId = type.definition.word(2u + memberIndex); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1373 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1374 | break; |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1375 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1376 | case spv::OpTypeVector: |
| 1377 | case spv::OpTypeMatrix: |
| 1378 | case spv::OpTypeArray: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1379 | { |
| 1380 | auto elementType = type.definition.word(2); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1381 | auto stride = getType(elementType).componentCount; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1382 | componentOffset += stride * indexes[i]; |
| 1383 | typeId = elementType; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1384 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1385 | break; |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1386 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1387 | default: |
| 1388 | UNREACHABLE("%s", OpcodeName(type.opcode())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1389 | } |
| 1390 | } |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1391 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1392 | return componentOffset; |
| 1393 | } |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1394 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1395 | void SpirvShader::Decorations::Apply(spv::Decoration decoration, uint32_t arg) |
| 1396 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1397 | switch(decoration) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1398 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1399 | case spv::DecorationLocation: |
| 1400 | HasLocation = true; |
| 1401 | Location = static_cast<int32_t>(arg); |
| 1402 | break; |
| 1403 | case spv::DecorationComponent: |
| 1404 | HasComponent = true; |
| 1405 | Component = arg; |
| 1406 | break; |
| 1407 | case spv::DecorationBuiltIn: |
| 1408 | HasBuiltIn = true; |
| 1409 | BuiltIn = static_cast<spv::BuiltIn>(arg); |
| 1410 | break; |
| 1411 | case spv::DecorationFlat: |
| 1412 | Flat = true; |
| 1413 | break; |
| 1414 | case spv::DecorationNoPerspective: |
| 1415 | NoPerspective = true; |
| 1416 | break; |
| 1417 | case spv::DecorationCentroid: |
| 1418 | Centroid = true; |
| 1419 | break; |
| 1420 | case spv::DecorationBlock: |
| 1421 | Block = true; |
| 1422 | break; |
| 1423 | case spv::DecorationBufferBlock: |
| 1424 | BufferBlock = true; |
| 1425 | break; |
| 1426 | case spv::DecorationOffset: |
| 1427 | HasOffset = true; |
| 1428 | Offset = static_cast<int32_t>(arg); |
| 1429 | break; |
| 1430 | case spv::DecorationArrayStride: |
| 1431 | HasArrayStride = true; |
| 1432 | ArrayStride = static_cast<int32_t>(arg); |
| 1433 | break; |
| 1434 | case spv::DecorationMatrixStride: |
| 1435 | HasMatrixStride = true; |
| 1436 | MatrixStride = static_cast<int32_t>(arg); |
| 1437 | break; |
| 1438 | case spv::DecorationRelaxedPrecision: |
| 1439 | RelaxedPrecision = true; |
| 1440 | break; |
| 1441 | case spv::DecorationRowMajor: |
| 1442 | HasRowMajor = true; |
| 1443 | RowMajor = true; |
| 1444 | break; |
| 1445 | case spv::DecorationColMajor: |
| 1446 | HasRowMajor = true; |
| 1447 | RowMajor = false; |
| 1448 | default: |
| 1449 | // Intentionally partial, there are many decorations we just don't care about. |
| 1450 | break; |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1451 | } |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1452 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1453 | |
| 1454 | void SpirvShader::Decorations::Apply(const sw::SpirvShader::Decorations &src) |
| 1455 | { |
| 1456 | // Apply a decoration group to this set of decorations |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1457 | if(src.HasBuiltIn) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1458 | { |
| 1459 | HasBuiltIn = true; |
| 1460 | BuiltIn = src.BuiltIn; |
| 1461 | } |
| 1462 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1463 | if(src.HasLocation) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1464 | { |
| 1465 | HasLocation = true; |
| 1466 | Location = src.Location; |
| 1467 | } |
| 1468 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1469 | if(src.HasComponent) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1470 | { |
| 1471 | HasComponent = true; |
| 1472 | Component = src.Component; |
| 1473 | } |
| 1474 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1475 | if(src.HasOffset) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1476 | { |
| 1477 | HasOffset = true; |
| 1478 | Offset = src.Offset; |
| 1479 | } |
| 1480 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1481 | if(src.HasArrayStride) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1482 | { |
| 1483 | HasArrayStride = true; |
| 1484 | ArrayStride = src.ArrayStride; |
| 1485 | } |
| 1486 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1487 | if(src.HasMatrixStride) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1488 | { |
| 1489 | HasMatrixStride = true; |
| 1490 | MatrixStride = src.MatrixStride; |
| 1491 | } |
| 1492 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1493 | if(src.HasRowMajor) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1494 | { |
| 1495 | HasRowMajor = true; |
| 1496 | RowMajor = src.RowMajor; |
| 1497 | } |
| 1498 | |
| 1499 | Flat |= src.Flat; |
| 1500 | NoPerspective |= src.NoPerspective; |
| 1501 | Centroid |= src.Centroid; |
| 1502 | Block |= src.Block; |
| 1503 | BufferBlock |= src.BufferBlock; |
| 1504 | RelaxedPrecision |= src.RelaxedPrecision; |
| 1505 | InsideMatrix |= src.InsideMatrix; |
| 1506 | } |
| 1507 | |
| 1508 | void SpirvShader::DescriptorDecorations::Apply(const sw::SpirvShader::DescriptorDecorations &src) |
| 1509 | { |
| 1510 | if(src.DescriptorSet >= 0) |
| 1511 | { |
| 1512 | DescriptorSet = src.DescriptorSet; |
| 1513 | } |
| 1514 | |
| 1515 | if(src.Binding >= 0) |
| 1516 | { |
| 1517 | Binding = src.Binding; |
| 1518 | } |
| 1519 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1520 | if(src.InputAttachmentIndex >= 0) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1521 | { |
| 1522 | InputAttachmentIndex = src.InputAttachmentIndex; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | void SpirvShader::ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const |
| 1527 | { |
| 1528 | auto it = decorations.find(id); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1529 | if(it != decorations.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1530 | d->Apply(it->second); |
| 1531 | } |
| 1532 | |
| 1533 | void SpirvShader::ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const |
| 1534 | { |
| 1535 | auto it = memberDecorations.find(id); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1536 | if(it != memberDecorations.end() && member < it->second.size()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1537 | { |
| 1538 | d->Apply(it->second[member]); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | void SpirvShader::DefineResult(const InsnIterator &insn) |
| 1543 | { |
| 1544 | Type::ID typeId = insn.word(1); |
| 1545 | Object::ID resultId = insn.word(2); |
| 1546 | auto &object = defs[resultId]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1547 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1548 | switch(getType(typeId).opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1549 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1550 | case spv::OpTypePointer: |
| 1551 | case spv::OpTypeImage: |
| 1552 | case spv::OpTypeSampledImage: |
| 1553 | case spv::OpTypeSampler: |
| 1554 | object.kind = Object::Kind::Pointer; |
| 1555 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1556 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1557 | default: |
| 1558 | object.kind = Object::Kind::Intermediate; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | object.definition = insn; |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1562 | dbgDeclareResult(insn, resultId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | OutOfBoundsBehavior SpirvShader::EmitState::getOutOfBoundsBehavior(spv::StorageClass storageClass) const |
| 1566 | { |
| 1567 | switch(storageClass) |
| 1568 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1569 | case spv::StorageClassUniform: |
| 1570 | case spv::StorageClassStorageBuffer: |
| 1571 | // Buffer resource access. robustBufferAccess feature applies. |
| 1572 | return robustBufferAccess ? OutOfBoundsBehavior::RobustBufferAccess |
| 1573 | : OutOfBoundsBehavior::UndefinedBehavior; |
| 1574 | |
| 1575 | case spv::StorageClassImage: |
| 1576 | // VK_EXT_image_robustness requires nullifying out-of-bounds accesses. |
| 1577 | // TODO(b/162327166): Only perform bounds checks when VK_EXT_image_robustness is enabled. |
| 1578 | return OutOfBoundsBehavior::Nullify; |
| 1579 | |
| 1580 | case spv::StorageClassInput: |
| 1581 | if(executionModel == spv::ExecutionModelVertex) |
| 1582 | { |
| 1583 | // Vertex attributes follow robustBufferAccess rules. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1584 | return robustBufferAccess ? OutOfBoundsBehavior::RobustBufferAccess |
| 1585 | : OutOfBoundsBehavior::UndefinedBehavior; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1586 | } |
| 1587 | // Fall through to default case. |
| 1588 | default: |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame^] | 1589 | // TODO(b/192310780): StorageClassFunction out-of-bounds accesses are undefined behavior. |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1590 | // TODO(b/137183137): Optimize if the pointer resulted from OpInBoundsAccessChain. |
| 1591 | // TODO(b/131224163): Optimize cases statically known to be within bounds. |
| 1592 | return OutOfBoundsBehavior::UndefinedValue; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | return OutOfBoundsBehavior::Nullify; |
| 1596 | } |
| 1597 | |
| 1598 | // emit-time |
| 1599 | |
| 1600 | void SpirvShader::emitProlog(SpirvRoutine *routine) const |
| 1601 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1602 | for(auto insn : *this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1603 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1604 | switch(insn.opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1605 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1606 | case spv::OpVariable: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1607 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 1608 | auto resultPointerType = getType(insn.resultTypeId()); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1609 | auto pointeeType = getType(resultPointerType.element); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1610 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1611 | if(pointeeType.componentCount > 0) // TODO: what to do about zero-slot objects? |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1612 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1613 | routine->createVariable(insn.resultId(), pointeeType.componentCount); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1614 | } |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1615 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1616 | break; |
| 1617 | |
| 1618 | case spv::OpPhi: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1619 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 1620 | auto type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1621 | routine->phis.emplace(insn.resultId(), SpirvRoutine::Variable(type.componentCount)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1622 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1623 | break; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1624 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1625 | case spv::OpImageDrefGather: |
| 1626 | case spv::OpImageFetch: |
| 1627 | case spv::OpImageGather: |
| 1628 | case spv::OpImageQueryLod: |
| 1629 | case spv::OpImageSampleDrefExplicitLod: |
| 1630 | case spv::OpImageSampleDrefImplicitLod: |
| 1631 | case spv::OpImageSampleExplicitLod: |
| 1632 | case spv::OpImageSampleImplicitLod: |
| 1633 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1634 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1635 | case spv::OpImageSampleProjExplicitLod: |
| 1636 | case spv::OpImageSampleProjImplicitLod: |
| 1637 | routine->samplerCache.emplace(insn.resultId(), SpirvRoutine::SamplerCache{}); |
| 1638 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1639 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1640 | default: |
| 1641 | // Nothing else produces interface variables, so can all be safely ignored. |
| 1642 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | |
Alexis Hetu | 0bcb71f | 2021-01-14 12:05:12 -0500 | [diff] [blame] | 1647 | void SpirvShader::emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask, RValue<SIMD::Int> const &storesAndAtomicsMask, const vk::DescriptorSet::Bindings &descriptorSets, unsigned int multiSampleCount) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1648 | { |
Alexis Hetu | 0bcb71f | 2021-01-14 12:05:12 -0500 | [diff] [blame] | 1649 | EmitState state(routine, entryPoint, activeLaneMask, storesAndAtomicsMask, descriptorSets, robustBufferAccess, multiSampleCount, executionModel); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1650 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1651 | dbgBeginEmit(&state); |
| 1652 | defer(dbgEndEmit(&state)); |
| 1653 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1654 | // Emit everything up to the first label |
| 1655 | // TODO: Separate out dispatch of block from non-block instructions? |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1656 | for(auto insn : *this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1657 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1658 | if(insn.opcode() == spv::OpLabel) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1659 | { |
| 1660 | break; |
| 1661 | } |
| 1662 | EmitInstruction(insn, &state); |
| 1663 | } |
| 1664 | |
| 1665 | // Emit all the blocks starting from entryPoint. |
| 1666 | EmitBlocks(getFunction(entryPoint).entry, &state); |
| 1667 | } |
| 1668 | |
| 1669 | void SpirvShader::EmitInstructions(InsnIterator begin, InsnIterator end, EmitState *state) const |
| 1670 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1671 | for(auto insn = begin; insn != end; insn++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1672 | { |
| 1673 | auto res = EmitInstruction(insn, state); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1674 | switch(res) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1675 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1676 | case EmitResult::Continue: |
| 1677 | continue; |
| 1678 | case EmitResult::Terminator: |
| 1679 | break; |
| 1680 | default: |
| 1681 | UNREACHABLE("Unexpected EmitResult %d", int(res)); |
| 1682 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | SpirvShader::EmitResult SpirvShader::EmitInstruction(InsnIterator insn, EmitState *state) const |
| 1688 | { |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1689 | dbgBeginEmitInstruction(insn, state); |
| 1690 | defer(dbgEndEmitInstruction(insn, state)); |
| 1691 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1692 | auto opcode = insn.opcode(); |
| 1693 | |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 1694 | #if SPIRV_SHADER_ENABLE_DBG |
| 1695 | { |
| 1696 | auto text = spvtools::spvInstructionBinaryToText( |
Sean Risser | 019feda | 2020-11-09 14:19:27 -0500 | [diff] [blame] | 1697 | vk::SPIRV_VERSION, |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 1698 | insn.wordPointer(0), |
| 1699 | insn.wordCount(), |
| 1700 | insns.data(), |
| 1701 | insns.size(), |
| 1702 | SPV_BINARY_TO_TEXT_OPTION_NO_HEADER); |
| 1703 | SPIRV_SHADER_DBG("{0}", text); |
| 1704 | } |
| 1705 | #endif // ENABLE_DBG_MSGS |
| 1706 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1707 | switch(opcode) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1708 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1709 | case spv::OpTypeVoid: |
| 1710 | case spv::OpTypeInt: |
| 1711 | case spv::OpTypeFloat: |
| 1712 | case spv::OpTypeBool: |
| 1713 | case spv::OpTypeVector: |
| 1714 | case spv::OpTypeArray: |
| 1715 | case spv::OpTypeRuntimeArray: |
| 1716 | case spv::OpTypeMatrix: |
| 1717 | case spv::OpTypeStruct: |
| 1718 | case spv::OpTypePointer: |
| 1719 | case spv::OpTypeFunction: |
| 1720 | case spv::OpTypeImage: |
| 1721 | case spv::OpTypeSampledImage: |
| 1722 | case spv::OpTypeSampler: |
| 1723 | case spv::OpExecutionMode: |
| 1724 | case spv::OpMemoryModel: |
| 1725 | case spv::OpFunction: |
| 1726 | case spv::OpFunctionEnd: |
| 1727 | case spv::OpConstant: |
| 1728 | case spv::OpConstantNull: |
| 1729 | case spv::OpConstantTrue: |
| 1730 | case spv::OpConstantFalse: |
| 1731 | case spv::OpConstantComposite: |
| 1732 | case spv::OpSpecConstant: |
| 1733 | case spv::OpSpecConstantTrue: |
| 1734 | case spv::OpSpecConstantFalse: |
| 1735 | case spv::OpSpecConstantComposite: |
| 1736 | case spv::OpSpecConstantOp: |
| 1737 | case spv::OpUndef: |
| 1738 | case spv::OpExtension: |
| 1739 | case spv::OpCapability: |
| 1740 | case spv::OpEntryPoint: |
| 1741 | case spv::OpExtInstImport: |
| 1742 | case spv::OpDecorate: |
| 1743 | case spv::OpMemberDecorate: |
| 1744 | case spv::OpGroupDecorate: |
| 1745 | case spv::OpGroupMemberDecorate: |
| 1746 | case spv::OpDecorationGroup: |
| 1747 | case spv::OpDecorateId: |
| 1748 | case spv::OpDecorateString: |
| 1749 | case spv::OpMemberDecorateString: |
| 1750 | case spv::OpName: |
| 1751 | case spv::OpMemberName: |
| 1752 | case spv::OpSource: |
| 1753 | case spv::OpSourceContinued: |
| 1754 | case spv::OpSourceExtension: |
| 1755 | case spv::OpNoLine: |
| 1756 | case spv::OpModuleProcessed: |
| 1757 | case spv::OpString: |
| 1758 | // Nothing to do at emit time. These are either fully handled at analysis time, |
| 1759 | // or don't require any work at all. |
| 1760 | return EmitResult::Continue; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1761 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1762 | case spv::OpLine: |
| 1763 | return EmitLine(insn, state); |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1764 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1765 | case spv::OpLabel: |
| 1766 | return EmitResult::Continue; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1767 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1768 | case spv::OpVariable: |
| 1769 | return EmitVariable(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1770 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1771 | case spv::OpLoad: |
| 1772 | case spv::OpAtomicLoad: |
| 1773 | return EmitLoad(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1774 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1775 | case spv::OpStore: |
| 1776 | case spv::OpAtomicStore: |
| 1777 | return EmitStore(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1778 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1779 | case spv::OpAtomicIAdd: |
| 1780 | case spv::OpAtomicISub: |
| 1781 | case spv::OpAtomicSMin: |
| 1782 | case spv::OpAtomicSMax: |
| 1783 | case spv::OpAtomicUMin: |
| 1784 | case spv::OpAtomicUMax: |
| 1785 | case spv::OpAtomicAnd: |
| 1786 | case spv::OpAtomicOr: |
| 1787 | case spv::OpAtomicXor: |
| 1788 | case spv::OpAtomicIIncrement: |
| 1789 | case spv::OpAtomicIDecrement: |
| 1790 | case spv::OpAtomicExchange: |
| 1791 | return EmitAtomicOp(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1792 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1793 | case spv::OpAtomicCompareExchange: |
| 1794 | return EmitAtomicCompareExchange(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1795 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1796 | case spv::OpAccessChain: |
| 1797 | case spv::OpInBoundsAccessChain: |
| 1798 | return EmitAccessChain(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1799 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1800 | case spv::OpCompositeConstruct: |
| 1801 | return EmitCompositeConstruct(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1802 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1803 | case spv::OpCompositeInsert: |
| 1804 | return EmitCompositeInsert(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1805 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1806 | case spv::OpCompositeExtract: |
| 1807 | return EmitCompositeExtract(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1808 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1809 | case spv::OpVectorShuffle: |
| 1810 | return EmitVectorShuffle(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1811 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1812 | case spv::OpVectorExtractDynamic: |
| 1813 | return EmitVectorExtractDynamic(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1814 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1815 | case spv::OpVectorInsertDynamic: |
| 1816 | return EmitVectorInsertDynamic(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1817 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1818 | case spv::OpVectorTimesScalar: |
| 1819 | case spv::OpMatrixTimesScalar: |
| 1820 | return EmitVectorTimesScalar(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1821 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1822 | case spv::OpMatrixTimesVector: |
| 1823 | return EmitMatrixTimesVector(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1824 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1825 | case spv::OpVectorTimesMatrix: |
| 1826 | return EmitVectorTimesMatrix(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1827 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1828 | case spv::OpMatrixTimesMatrix: |
| 1829 | return EmitMatrixTimesMatrix(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1830 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1831 | case spv::OpOuterProduct: |
| 1832 | return EmitOuterProduct(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1833 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1834 | case spv::OpTranspose: |
| 1835 | return EmitTranspose(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1836 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1837 | case spv::OpNot: |
| 1838 | case spv::OpBitFieldInsert: |
| 1839 | case spv::OpBitFieldSExtract: |
| 1840 | case spv::OpBitFieldUExtract: |
| 1841 | case spv::OpBitReverse: |
| 1842 | case spv::OpBitCount: |
| 1843 | case spv::OpSNegate: |
| 1844 | case spv::OpFNegate: |
| 1845 | case spv::OpLogicalNot: |
| 1846 | case spv::OpConvertFToU: |
| 1847 | case spv::OpConvertFToS: |
| 1848 | case spv::OpConvertSToF: |
| 1849 | case spv::OpConvertUToF: |
| 1850 | case spv::OpBitcast: |
| 1851 | case spv::OpIsInf: |
| 1852 | case spv::OpIsNan: |
| 1853 | case spv::OpDPdx: |
| 1854 | case spv::OpDPdxCoarse: |
| 1855 | case spv::OpDPdy: |
| 1856 | case spv::OpDPdyCoarse: |
| 1857 | case spv::OpFwidth: |
| 1858 | case spv::OpFwidthCoarse: |
| 1859 | case spv::OpDPdxFine: |
| 1860 | case spv::OpDPdyFine: |
| 1861 | case spv::OpFwidthFine: |
| 1862 | case spv::OpQuantizeToF16: |
| 1863 | return EmitUnaryOp(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1864 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1865 | case spv::OpIAdd: |
| 1866 | case spv::OpISub: |
| 1867 | case spv::OpIMul: |
| 1868 | case spv::OpSDiv: |
| 1869 | case spv::OpUDiv: |
| 1870 | case spv::OpFAdd: |
| 1871 | case spv::OpFSub: |
| 1872 | case spv::OpFMul: |
| 1873 | case spv::OpFDiv: |
| 1874 | case spv::OpFMod: |
| 1875 | case spv::OpFRem: |
| 1876 | case spv::OpFOrdEqual: |
| 1877 | case spv::OpFUnordEqual: |
| 1878 | case spv::OpFOrdNotEqual: |
| 1879 | case spv::OpFUnordNotEqual: |
| 1880 | case spv::OpFOrdLessThan: |
| 1881 | case spv::OpFUnordLessThan: |
| 1882 | case spv::OpFOrdGreaterThan: |
| 1883 | case spv::OpFUnordGreaterThan: |
| 1884 | case spv::OpFOrdLessThanEqual: |
| 1885 | case spv::OpFUnordLessThanEqual: |
| 1886 | case spv::OpFOrdGreaterThanEqual: |
| 1887 | case spv::OpFUnordGreaterThanEqual: |
| 1888 | case spv::OpSMod: |
| 1889 | case spv::OpSRem: |
| 1890 | case spv::OpUMod: |
| 1891 | case spv::OpIEqual: |
| 1892 | case spv::OpINotEqual: |
| 1893 | case spv::OpUGreaterThan: |
| 1894 | case spv::OpSGreaterThan: |
| 1895 | case spv::OpUGreaterThanEqual: |
| 1896 | case spv::OpSGreaterThanEqual: |
| 1897 | case spv::OpULessThan: |
| 1898 | case spv::OpSLessThan: |
| 1899 | case spv::OpULessThanEqual: |
| 1900 | case spv::OpSLessThanEqual: |
| 1901 | case spv::OpShiftRightLogical: |
| 1902 | case spv::OpShiftRightArithmetic: |
| 1903 | case spv::OpShiftLeftLogical: |
| 1904 | case spv::OpBitwiseOr: |
| 1905 | case spv::OpBitwiseXor: |
| 1906 | case spv::OpBitwiseAnd: |
| 1907 | case spv::OpLogicalOr: |
| 1908 | case spv::OpLogicalAnd: |
| 1909 | case spv::OpLogicalEqual: |
| 1910 | case spv::OpLogicalNotEqual: |
| 1911 | case spv::OpUMulExtended: |
| 1912 | case spv::OpSMulExtended: |
| 1913 | case spv::OpIAddCarry: |
| 1914 | case spv::OpISubBorrow: |
| 1915 | return EmitBinaryOp(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1916 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1917 | case spv::OpDot: |
| 1918 | return EmitDot(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1919 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1920 | case spv::OpSelect: |
| 1921 | return EmitSelect(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1922 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1923 | case spv::OpExtInst: |
| 1924 | return EmitExtendedInstruction(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1925 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1926 | case spv::OpAny: |
| 1927 | return EmitAny(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1928 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1929 | case spv::OpAll: |
| 1930 | return EmitAll(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1931 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1932 | case spv::OpBranch: |
| 1933 | return EmitBranch(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1934 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1935 | case spv::OpPhi: |
| 1936 | return EmitPhi(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1937 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1938 | case spv::OpSelectionMerge: |
| 1939 | case spv::OpLoopMerge: |
| 1940 | return EmitResult::Continue; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1941 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1942 | case spv::OpBranchConditional: |
| 1943 | return EmitBranchConditional(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1944 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1945 | case spv::OpSwitch: |
| 1946 | return EmitSwitch(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1947 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1948 | case spv::OpUnreachable: |
| 1949 | return EmitUnreachable(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1950 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1951 | case spv::OpReturn: |
| 1952 | return EmitReturn(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1953 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1954 | case spv::OpFunctionCall: |
| 1955 | return EmitFunctionCall(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1956 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1957 | case spv::OpKill: |
| 1958 | return EmitKill(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1959 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1960 | case spv::OpImageSampleImplicitLod: |
| 1961 | return EmitImageSampleImplicitLod(None, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1962 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1963 | case spv::OpImageSampleExplicitLod: |
| 1964 | return EmitImageSampleExplicitLod(None, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1965 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1966 | case spv::OpImageSampleDrefImplicitLod: |
| 1967 | return EmitImageSampleImplicitLod(Dref, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1968 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1969 | case spv::OpImageSampleDrefExplicitLod: |
| 1970 | return EmitImageSampleExplicitLod(Dref, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1971 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1972 | case spv::OpImageSampleProjImplicitLod: |
| 1973 | return EmitImageSampleImplicitLod(Proj, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1974 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1975 | case spv::OpImageSampleProjExplicitLod: |
| 1976 | return EmitImageSampleExplicitLod(Proj, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1977 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1978 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1979 | return EmitImageSampleImplicitLod(ProjDref, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1980 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1981 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1982 | return EmitImageSampleExplicitLod(ProjDref, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1983 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1984 | case spv::OpImageGather: |
| 1985 | return EmitImageGather(None, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1986 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1987 | case spv::OpImageDrefGather: |
| 1988 | return EmitImageGather(Dref, insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1989 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1990 | case spv::OpImageFetch: |
| 1991 | return EmitImageFetch(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1992 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1993 | case spv::OpImageQuerySizeLod: |
| 1994 | return EmitImageQuerySizeLod(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1995 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1996 | case spv::OpImageQuerySize: |
| 1997 | return EmitImageQuerySize(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1998 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1999 | case spv::OpImageQueryLod: |
| 2000 | return EmitImageQueryLod(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2001 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2002 | case spv::OpImageQueryLevels: |
| 2003 | return EmitImageQueryLevels(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2004 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2005 | case spv::OpImageQuerySamples: |
| 2006 | return EmitImageQuerySamples(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2007 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2008 | case spv::OpImageRead: |
| 2009 | return EmitImageRead(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2010 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2011 | case spv::OpImageWrite: |
| 2012 | return EmitImageWrite(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2013 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2014 | case spv::OpImageTexelPointer: |
| 2015 | return EmitImageTexelPointer(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2016 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2017 | case spv::OpSampledImage: |
| 2018 | case spv::OpImage: |
| 2019 | return EmitSampledImageCombineOrSplit(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2020 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2021 | case spv::OpCopyObject: |
| 2022 | case spv::OpCopyLogical: |
| 2023 | return EmitCopyObject(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2024 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2025 | case spv::OpCopyMemory: |
| 2026 | return EmitCopyMemory(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2027 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2028 | case spv::OpControlBarrier: |
| 2029 | return EmitControlBarrier(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2030 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2031 | case spv::OpMemoryBarrier: |
| 2032 | return EmitMemoryBarrier(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2033 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2034 | case spv::OpGroupNonUniformElect: |
| 2035 | case spv::OpGroupNonUniformAll: |
| 2036 | case spv::OpGroupNonUniformAny: |
| 2037 | case spv::OpGroupNonUniformAllEqual: |
| 2038 | case spv::OpGroupNonUniformBroadcast: |
| 2039 | case spv::OpGroupNonUniformBroadcastFirst: |
| 2040 | case spv::OpGroupNonUniformBallot: |
| 2041 | case spv::OpGroupNonUniformInverseBallot: |
| 2042 | case spv::OpGroupNonUniformBallotBitExtract: |
| 2043 | case spv::OpGroupNonUniformBallotBitCount: |
| 2044 | case spv::OpGroupNonUniformBallotFindLSB: |
| 2045 | case spv::OpGroupNonUniformBallotFindMSB: |
| 2046 | case spv::OpGroupNonUniformShuffle: |
| 2047 | case spv::OpGroupNonUniformShuffleXor: |
| 2048 | case spv::OpGroupNonUniformShuffleUp: |
| 2049 | case spv::OpGroupNonUniformShuffleDown: |
| 2050 | case spv::OpGroupNonUniformIAdd: |
| 2051 | case spv::OpGroupNonUniformFAdd: |
| 2052 | case spv::OpGroupNonUniformIMul: |
| 2053 | case spv::OpGroupNonUniformFMul: |
| 2054 | case spv::OpGroupNonUniformSMin: |
| 2055 | case spv::OpGroupNonUniformUMin: |
| 2056 | case spv::OpGroupNonUniformFMin: |
| 2057 | case spv::OpGroupNonUniformSMax: |
| 2058 | case spv::OpGroupNonUniformUMax: |
| 2059 | case spv::OpGroupNonUniformFMax: |
| 2060 | case spv::OpGroupNonUniformBitwiseAnd: |
| 2061 | case spv::OpGroupNonUniformBitwiseOr: |
| 2062 | case spv::OpGroupNonUniformBitwiseXor: |
| 2063 | case spv::OpGroupNonUniformLogicalAnd: |
| 2064 | case spv::OpGroupNonUniformLogicalOr: |
| 2065 | case spv::OpGroupNonUniformLogicalXor: |
| 2066 | return EmitGroupNonUniform(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2067 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2068 | case spv::OpArrayLength: |
| 2069 | return EmitArrayLength(insn, state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2070 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2071 | default: |
| 2072 | UNREACHABLE("%s", OpcodeName(opcode)); |
| 2073 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | return EmitResult::Continue; |
| 2077 | } |
| 2078 | |
| 2079 | SpirvShader::EmitResult SpirvShader::EmitAccessChain(InsnIterator insn, EmitState *state) const |
| 2080 | { |
| 2081 | Type::ID typeId = insn.word(1); |
| 2082 | Object::ID resultId = insn.word(2); |
| 2083 | Object::ID baseId = insn.word(3); |
| 2084 | uint32_t numIndexes = insn.wordCount() - 4; |
| 2085 | const uint32_t *indexes = insn.wordPointer(4); |
| 2086 | auto &type = getType(typeId); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2087 | ASSERT(type.componentCount == 1); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2088 | ASSERT(getObject(resultId).kind == Object::Kind::Pointer); |
| 2089 | |
| 2090 | if(type.storageClass == spv::StorageClassPushConstant || |
| 2091 | type.storageClass == spv::StorageClassUniform || |
| 2092 | type.storageClass == spv::StorageClassStorageBuffer) |
| 2093 | { |
| 2094 | auto ptr = WalkExplicitLayoutAccessChain(baseId, numIndexes, indexes, state); |
| 2095 | state->createPointer(resultId, ptr); |
| 2096 | } |
| 2097 | else |
| 2098 | { |
| 2099 | auto ptr = WalkAccessChain(baseId, numIndexes, indexes, state); |
| 2100 | state->createPointer(resultId, ptr); |
| 2101 | } |
| 2102 | |
| 2103 | return EmitResult::Continue; |
| 2104 | } |
| 2105 | |
| 2106 | SpirvShader::EmitResult SpirvShader::EmitCompositeConstruct(InsnIterator insn, EmitState *state) const |
| 2107 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2108 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2109 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2110 | auto offset = 0u; |
| 2111 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2112 | for(auto i = 0u; i < insn.wordCount() - 3; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2113 | { |
| 2114 | Object::ID srcObjectId = insn.word(3u + i); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2115 | auto &srcObject = getObject(srcObjectId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2116 | auto &srcObjectTy = getType(srcObject); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2117 | Operand srcObjectAccess(this, state, srcObjectId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2118 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2119 | for(auto j = 0u; j < srcObjectTy.componentCount; j++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2120 | { |
| 2121 | dst.move(offset++, srcObjectAccess.Float(j)); |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | return EmitResult::Continue; |
| 2126 | } |
| 2127 | |
| 2128 | SpirvShader::EmitResult SpirvShader::EmitCompositeInsert(InsnIterator insn, EmitState *state) const |
| 2129 | { |
| 2130 | Type::ID resultTypeId = insn.word(1); |
| 2131 | auto &type = getType(resultTypeId); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2132 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2133 | auto &newPartObject = getObject(insn.word(3)); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2134 | auto &newPartObjectTy = getType(newPartObject); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2135 | auto firstNewComponent = WalkLiteralAccessChain(resultTypeId, insn.wordCount() - 5, insn.wordPointer(5)); |
| 2136 | |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2137 | Operand srcObjectAccess(this, state, insn.word(4)); |
| 2138 | Operand newPartObjectAccess(this, state, insn.word(3)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2139 | |
| 2140 | // old components before |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2141 | for(auto i = 0u; i < firstNewComponent; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2142 | { |
| 2143 | dst.move(i, srcObjectAccess.Float(i)); |
| 2144 | } |
| 2145 | // new part |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2146 | for(auto i = 0u; i < newPartObjectTy.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2147 | { |
| 2148 | dst.move(firstNewComponent + i, newPartObjectAccess.Float(i)); |
| 2149 | } |
| 2150 | // old components after |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2151 | for(auto i = firstNewComponent + newPartObjectTy.componentCount; i < type.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2152 | { |
| 2153 | dst.move(i, srcObjectAccess.Float(i)); |
| 2154 | } |
| 2155 | |
| 2156 | return EmitResult::Continue; |
| 2157 | } |
| 2158 | |
| 2159 | SpirvShader::EmitResult SpirvShader::EmitCompositeExtract(InsnIterator insn, EmitState *state) const |
| 2160 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2161 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2162 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2163 | auto &compositeObject = getObject(insn.word(3)); |
| 2164 | Type::ID compositeTypeId = compositeObject.definition.word(1); |
| 2165 | auto firstComponent = WalkLiteralAccessChain(compositeTypeId, insn.wordCount() - 4, insn.wordPointer(4)); |
| 2166 | |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2167 | Operand compositeObjectAccess(this, state, insn.word(3)); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2168 | for(auto i = 0u; i < type.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2169 | { |
| 2170 | dst.move(i, compositeObjectAccess.Float(firstComponent + i)); |
| 2171 | } |
| 2172 | |
| 2173 | return EmitResult::Continue; |
| 2174 | } |
| 2175 | |
| 2176 | SpirvShader::EmitResult SpirvShader::EmitVectorShuffle(InsnIterator insn, EmitState *state) const |
| 2177 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2178 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2179 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2180 | |
| 2181 | // Note: number of components in result type, first half type, and second |
| 2182 | // half type are all independent. |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2183 | auto &firstHalfType = getType(getObject(insn.word(3))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2184 | |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2185 | Operand firstHalfAccess(this, state, insn.word(3)); |
| 2186 | Operand secondHalfAccess(this, state, insn.word(4)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2187 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2188 | for(auto i = 0u; i < type.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2189 | { |
| 2190 | auto selector = insn.word(5 + i); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2191 | if(selector == static_cast<uint32_t>(-1)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2192 | { |
| 2193 | // Undefined value. Until we decide to do real undef values, zero is as good |
| 2194 | // a value as any |
| 2195 | dst.move(i, RValue<SIMD::Float>(0.0f)); |
| 2196 | } |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2197 | else if(selector < firstHalfType.componentCount) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2198 | { |
| 2199 | dst.move(i, firstHalfAccess.Float(selector)); |
| 2200 | } |
| 2201 | else |
| 2202 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2203 | dst.move(i, secondHalfAccess.Float(selector - firstHalfType.componentCount)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | return EmitResult::Continue; |
| 2208 | } |
| 2209 | |
| 2210 | SpirvShader::EmitResult SpirvShader::EmitVectorExtractDynamic(InsnIterator insn, EmitState *state) const |
| 2211 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2212 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2213 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2214 | auto &srcType = getType(getObject(insn.word(3))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2215 | |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2216 | Operand src(this, state, insn.word(3)); |
| 2217 | Operand index(this, state, insn.word(4)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2218 | |
| 2219 | SIMD::UInt v = SIMD::UInt(0); |
| 2220 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2221 | for(auto i = 0u; i < srcType.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2222 | { |
| 2223 | v |= CmpEQ(index.UInt(0), SIMD::UInt(i)) & src.UInt(i); |
| 2224 | } |
| 2225 | |
| 2226 | dst.move(0, v); |
| 2227 | return EmitResult::Continue; |
| 2228 | } |
| 2229 | |
| 2230 | SpirvShader::EmitResult SpirvShader::EmitVectorInsertDynamic(InsnIterator insn, EmitState *state) const |
| 2231 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2232 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2233 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2234 | |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2235 | Operand src(this, state, insn.word(3)); |
| 2236 | Operand component(this, state, insn.word(4)); |
| 2237 | Operand index(this, state, insn.word(5)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2238 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2239 | for(auto i = 0u; i < type.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2240 | { |
| 2241 | SIMD::UInt mask = CmpEQ(SIMD::UInt(i), index.UInt(0)); |
| 2242 | dst.move(i, (src.UInt(i) & ~mask) | (component.UInt(0) & mask)); |
| 2243 | } |
| 2244 | return EmitResult::Continue; |
| 2245 | } |
| 2246 | |
| 2247 | SpirvShader::EmitResult SpirvShader::EmitSelect(InsnIterator insn, EmitState *state) const |
| 2248 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2249 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2250 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2251 | auto cond = Operand(this, state, insn.word(3)); |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 2252 | auto condIsScalar = (cond.componentCount == 1); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2253 | auto lhs = Operand(this, state, insn.word(4)); |
| 2254 | auto rhs = Operand(this, state, insn.word(5)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2255 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2256 | for(auto i = 0u; i < type.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2257 | { |
| 2258 | auto sel = cond.Int(condIsScalar ? 0 : i); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2259 | dst.move(i, (sel & lhs.Int(i)) | (~sel & rhs.Int(i))); // TODO: IfThenElse() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2260 | } |
| 2261 | |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 2262 | SPIRV_SHADER_DBG("{0}: {1}", insn.word(2), dst); |
| 2263 | SPIRV_SHADER_DBG("{0}: {1}", insn.word(3), cond); |
| 2264 | SPIRV_SHADER_DBG("{0}: {1}", insn.word(4), lhs); |
| 2265 | SPIRV_SHADER_DBG("{0}: {1}", insn.word(5), rhs); |
| 2266 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2267 | return EmitResult::Continue; |
| 2268 | } |
| 2269 | |
| 2270 | SpirvShader::EmitResult SpirvShader::EmitAny(InsnIterator insn, EmitState *state) const |
| 2271 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2272 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2273 | ASSERT(type.componentCount == 1); |
| 2274 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2275 | auto &srcType = getType(getObject(insn.word(3))); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2276 | auto src = Operand(this, state, insn.word(3)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2277 | |
| 2278 | SIMD::UInt result = src.UInt(0); |
| 2279 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2280 | for(auto i = 1u; i < srcType.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2281 | { |
| 2282 | result |= src.UInt(i); |
| 2283 | } |
| 2284 | |
| 2285 | dst.move(0, result); |
| 2286 | return EmitResult::Continue; |
| 2287 | } |
| 2288 | |
| 2289 | SpirvShader::EmitResult SpirvShader::EmitAll(InsnIterator insn, EmitState *state) const |
| 2290 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2291 | auto &type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2292 | ASSERT(type.componentCount == 1); |
| 2293 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2294 | auto &srcType = getType(getObject(insn.word(3))); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2295 | auto src = Operand(this, state, insn.word(3)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2296 | |
| 2297 | SIMD::UInt result = src.UInt(0); |
| 2298 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2299 | for(auto i = 1u; i < srcType.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2300 | { |
| 2301 | result &= src.UInt(i); |
| 2302 | } |
| 2303 | |
| 2304 | dst.move(0, result); |
| 2305 | return EmitResult::Continue; |
| 2306 | } |
| 2307 | |
| 2308 | SpirvShader::EmitResult SpirvShader::EmitAtomicOp(InsnIterator insn, EmitState *state) const |
| 2309 | { |
| 2310 | auto &resultType = getType(Type::ID(insn.word(1))); |
| 2311 | Object::ID resultId = insn.word(2); |
Nicolas Capens | 7c84c9e | 2020-07-24 22:13:44 -0400 | [diff] [blame] | 2312 | Object::ID pointerId = insn.word(3); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2313 | Object::ID semanticsId = insn.word(5); |
| 2314 | auto memorySemantics = static_cast<spv::MemorySemanticsMask>(getObject(semanticsId).constantValue[0]); |
| 2315 | auto memoryOrder = MemoryOrder(memorySemantics); |
| 2316 | // Where no value is provided (increment/decrement) use an implicit value of 1. |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2317 | auto value = (insn.wordCount() == 7) ? Operand(this, state, insn.word(6)).UInt(0) : RValue<SIMD::UInt>(1); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2318 | auto &dst = state->createIntermediate(resultId, resultType.componentCount); |
Nicolas Capens | 7c84c9e | 2020-07-24 22:13:44 -0400 | [diff] [blame] | 2319 | auto ptr = state->getPointer(pointerId); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2320 | auto ptrOffsets = ptr.offsets(); |
| 2321 | |
Nicolas Capens | 7c84c9e | 2020-07-24 22:13:44 -0400 | [diff] [blame] | 2322 | SIMD::Int mask = state->activeLaneMask() & state->storesAndAtomicsMask(); |
| 2323 | |
| 2324 | if(getObject(pointerId).opcode() == spv::OpImageTexelPointer) |
| 2325 | { |
| 2326 | mask &= ptr.isInBounds(sizeof(int32_t), OutOfBoundsBehavior::Nullify); |
| 2327 | } |
| 2328 | |
| 2329 | SIMD::UInt result(0); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2330 | for(int j = 0; j < SIMD::Width; j++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2331 | { |
| 2332 | If(Extract(mask, j) != 0) |
| 2333 | { |
| 2334 | auto offset = Extract(ptrOffsets, j); |
| 2335 | auto laneValue = Extract(value, j); |
| 2336 | UInt v; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2337 | switch(insn.opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2338 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2339 | case spv::OpAtomicIAdd: |
| 2340 | case spv::OpAtomicIIncrement: |
| 2341 | v = AddAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2342 | break; |
| 2343 | case spv::OpAtomicISub: |
| 2344 | case spv::OpAtomicIDecrement: |
| 2345 | v = SubAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2346 | break; |
| 2347 | case spv::OpAtomicAnd: |
| 2348 | v = AndAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2349 | break; |
| 2350 | case spv::OpAtomicOr: |
| 2351 | v = OrAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2352 | break; |
| 2353 | case spv::OpAtomicXor: |
| 2354 | v = XorAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2355 | break; |
| 2356 | case spv::OpAtomicSMin: |
| 2357 | v = As<UInt>(MinAtomic(Pointer<Int>(&ptr.base[offset]), As<Int>(laneValue), memoryOrder)); |
| 2358 | break; |
| 2359 | case spv::OpAtomicSMax: |
| 2360 | v = As<UInt>(MaxAtomic(Pointer<Int>(&ptr.base[offset]), As<Int>(laneValue), memoryOrder)); |
| 2361 | break; |
| 2362 | case spv::OpAtomicUMin: |
| 2363 | v = MinAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2364 | break; |
| 2365 | case spv::OpAtomicUMax: |
| 2366 | v = MaxAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2367 | break; |
| 2368 | case spv::OpAtomicExchange: |
| 2369 | v = ExchangeAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 2370 | break; |
| 2371 | default: |
| 2372 | UNREACHABLE("%s", OpcodeName(insn.opcode())); |
| 2373 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2374 | } |
Nicolas Capens | 7c84c9e | 2020-07-24 22:13:44 -0400 | [diff] [blame] | 2375 | result = Insert(result, v, j); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | |
Nicolas Capens | 7c84c9e | 2020-07-24 22:13:44 -0400 | [diff] [blame] | 2379 | dst.move(0, result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2380 | return EmitResult::Continue; |
| 2381 | } |
| 2382 | |
| 2383 | SpirvShader::EmitResult SpirvShader::EmitAtomicCompareExchange(InsnIterator insn, EmitState *state) const |
| 2384 | { |
| 2385 | // Separate from EmitAtomicOp due to different instruction encoding |
| 2386 | auto &resultType = getType(Type::ID(insn.word(1))); |
| 2387 | Object::ID resultId = insn.word(2); |
| 2388 | |
| 2389 | auto memorySemanticsEqual = static_cast<spv::MemorySemanticsMask>(getObject(insn.word(5)).constantValue[0]); |
| 2390 | auto memoryOrderEqual = MemoryOrder(memorySemanticsEqual); |
| 2391 | auto memorySemanticsUnequal = static_cast<spv::MemorySemanticsMask>(getObject(insn.word(6)).constantValue[0]); |
| 2392 | auto memoryOrderUnequal = MemoryOrder(memorySemanticsUnequal); |
| 2393 | |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2394 | auto value = Operand(this, state, insn.word(7)); |
| 2395 | auto comparator = Operand(this, state, insn.word(8)); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2396 | auto &dst = state->createIntermediate(resultId, resultType.componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2397 | auto ptr = state->getPointer(insn.word(3)); |
| 2398 | auto ptrOffsets = ptr.offsets(); |
| 2399 | |
| 2400 | SIMD::UInt x(0); |
| 2401 | auto mask = state->activeLaneMask() & state->storesAndAtomicsMask(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2402 | for(int j = 0; j < SIMD::Width; j++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2403 | { |
| 2404 | If(Extract(mask, j) != 0) |
| 2405 | { |
| 2406 | auto offset = Extract(ptrOffsets, j); |
| 2407 | auto laneValue = Extract(value.UInt(0), j); |
| 2408 | auto laneComparator = Extract(comparator.UInt(0), j); |
| 2409 | UInt v = CompareExchangeAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, laneComparator, memoryOrderEqual, memoryOrderUnequal); |
| 2410 | x = Insert(x, v, j); |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | dst.move(0, x); |
| 2415 | return EmitResult::Continue; |
| 2416 | } |
| 2417 | |
| 2418 | SpirvShader::EmitResult SpirvShader::EmitCopyObject(InsnIterator insn, EmitState *state) const |
| 2419 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2420 | auto type = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2421 | auto &dst = state->createIntermediate(insn.resultId(), type.componentCount); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 2422 | auto src = Operand(this, state, insn.word(3)); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2423 | for(uint32_t i = 0; i < type.componentCount; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2424 | { |
| 2425 | dst.move(i, src.Int(i)); |
| 2426 | } |
| 2427 | return EmitResult::Continue; |
| 2428 | } |
| 2429 | |
| 2430 | SpirvShader::EmitResult SpirvShader::EmitArrayLength(InsnIterator insn, EmitState *state) const |
| 2431 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2432 | auto structPtrId = Object::ID(insn.word(3)); |
| 2433 | auto arrayFieldIdx = insn.word(4); |
| 2434 | |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2435 | auto &resultType = getType(insn.resultTypeId()); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2436 | ASSERT(resultType.componentCount == 1); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2437 | ASSERT(resultType.definition.opcode() == spv::OpTypeInt); |
| 2438 | |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2439 | auto &structPtrTy = getType(getObject(structPtrId)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2440 | auto &structTy = getType(structPtrTy.element); |
Alexis Hetu | 74d3f37 | 2020-02-14 15:32:37 -0500 | [diff] [blame] | 2441 | auto arrayId = Type::ID(structTy.definition.word(2 + arrayFieldIdx)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2442 | |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2443 | auto &result = state->createIntermediate(insn.resultId(), 1); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2444 | auto structBase = GetPointerToData(structPtrId, 0, state); |
| 2445 | |
Alexis Hetu | 74d3f37 | 2020-02-14 15:32:37 -0500 | [diff] [blame] | 2446 | Decorations structDecorations = {}; |
| 2447 | ApplyDecorationsForIdMember(&structDecorations, structPtrTy.element, arrayFieldIdx); |
| 2448 | ASSERT(structDecorations.HasOffset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2449 | |
Alexis Hetu | 74d3f37 | 2020-02-14 15:32:37 -0500 | [diff] [blame] | 2450 | auto arrayBase = structBase + structDecorations.Offset; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2451 | auto arraySizeInBytes = SIMD::Int(arrayBase.limit()) - arrayBase.offsets(); |
Alexis Hetu | 74d3f37 | 2020-02-14 15:32:37 -0500 | [diff] [blame] | 2452 | |
| 2453 | Decorations arrayDecorations = {}; |
| 2454 | ApplyDecorationsForId(&arrayDecorations, arrayId); |
| 2455 | ASSERT(arrayDecorations.HasArrayStride); |
| 2456 | auto arrayLength = arraySizeInBytes / SIMD::Int(arrayDecorations.ArrayStride); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2457 | |
| 2458 | result.move(0, SIMD::Int(arrayLength)); |
| 2459 | |
| 2460 | return EmitResult::Continue; |
| 2461 | } |
| 2462 | |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 2463 | SpirvShader::EmitResult SpirvShader::EmitExtendedInstruction(InsnIterator insn, EmitState *state) const |
| 2464 | { |
| 2465 | auto ext = getExtension(insn.word(3)); |
| 2466 | switch(ext.name) |
| 2467 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2468 | case Extension::GLSLstd450: |
| 2469 | return EmitExtGLSLstd450(insn, state); |
| 2470 | case Extension::OpenCLDebugInfo100: |
| 2471 | return EmitOpenCLDebugInfo100(insn, state); |
| 2472 | default: |
| 2473 | UNREACHABLE("Unknown Extension::Name<%d>", int(ext.name)); |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 2474 | } |
| 2475 | return EmitResult::Continue; |
| 2476 | } |
| 2477 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2478 | uint32_t SpirvShader::GetConstScalarInt(Object::ID id) const |
| 2479 | { |
| 2480 | auto &scopeObj = getObject(id); |
| 2481 | ASSERT(scopeObj.kind == Object::Kind::Constant); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 2482 | ASSERT(getType(scopeObj).componentCount == 1); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2483 | return scopeObj.constantValue[0]; |
| 2484 | } |
| 2485 | |
| 2486 | void SpirvShader::emitEpilog(SpirvRoutine *routine) const |
| 2487 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2488 | for(auto insn : *this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2489 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2490 | switch(insn.opcode()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2491 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2492 | case spv::OpVariable: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2493 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2494 | auto &object = getObject(insn.resultId()); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 2495 | auto &objectTy = getType(object); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2496 | if(object.kind == Object::Kind::InterfaceVariable && objectTy.storageClass == spv::StorageClassOutput) |
| 2497 | { |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2498 | auto &dst = routine->getVariable(insn.resultId()); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2499 | int offset = 0; |
Nicolas Capens | 7118675 | 2020-04-09 01:05:31 -0400 | [diff] [blame] | 2500 | VisitInterface(insn.resultId(), |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2501 | [&](Decorations const &d, AttribType type) { |
| 2502 | auto scalarSlot = d.Location << 2 | d.Component; |
| 2503 | routine->outputs[scalarSlot] = dst[offset++]; |
| 2504 | }); |
| 2505 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2506 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2507 | break; |
| 2508 | default: |
| 2509 | break; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2510 | } |
| 2511 | } |
Alexis Hetu | 09df3eb | 2021-01-14 10:46:33 -0500 | [diff] [blame] | 2512 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2513 | |
Alexis Hetu | 09df3eb | 2021-01-14 10:46:33 -0500 | [diff] [blame] | 2514 | void SpirvShader::clearPhis(SpirvRoutine *routine) const |
| 2515 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2516 | // Clear phis that are no longer used. This serves two purposes: |
| 2517 | // (1) The phi rr::Variables are destructed, preventing pointless |
| 2518 | // materialization. |
| 2519 | // (2) Frees memory that will never be used again. |
| 2520 | routine->phis.clear(); |
| 2521 | } |
| 2522 | |
| 2523 | VkShaderStageFlagBits SpirvShader::executionModelToStage(spv::ExecutionModel model) |
| 2524 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 2525 | switch(model) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2526 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2527 | case spv::ExecutionModelVertex: return VK_SHADER_STAGE_VERTEX_BIT; |
| 2528 | // case spv::ExecutionModelTessellationControl: return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 2529 | // case spv::ExecutionModelTessellationEvaluation: return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 2530 | // case spv::ExecutionModelGeometry: return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 2531 | case spv::ExecutionModelFragment: return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 2532 | case spv::ExecutionModelGLCompute: return VK_SHADER_STAGE_COMPUTE_BIT; |
| 2533 | // case spv::ExecutionModelKernel: return VkShaderStageFlagBits(0); // Not supported by vulkan. |
| 2534 | // case spv::ExecutionModelTaskNV: return VK_SHADER_STAGE_TASK_BIT_NV; |
| 2535 | // case spv::ExecutionModelMeshNV: return VK_SHADER_STAGE_MESH_BIT_NV; |
| 2536 | // case spv::ExecutionModelRayGenerationNV: return VK_SHADER_STAGE_RAYGEN_BIT_NV; |
| 2537 | // case spv::ExecutionModelIntersectionNV: return VK_SHADER_STAGE_INTERSECTION_BIT_NV; |
| 2538 | // case spv::ExecutionModelAnyHitNV: return VK_SHADER_STAGE_ANY_HIT_BIT_NV; |
| 2539 | // case spv::ExecutionModelClosestHitNV: return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV; |
| 2540 | // case spv::ExecutionModelMissNV: return VK_SHADER_STAGE_MISS_BIT_NV; |
| 2541 | // case spv::ExecutionModelCallableNV: return VK_SHADER_STAGE_CALLABLE_BIT_NV; |
| 2542 | default: |
| 2543 | UNSUPPORTED("ExecutionModel: %d", int(model)); |
| 2544 | return VkShaderStageFlagBits(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2545 | } |
| 2546 | } |
| 2547 | |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 2548 | SpirvShader::Operand::Operand(const SpirvShader *shader, const EmitState *state, SpirvShader::Object::ID objectId) |
| 2549 | : Operand(state, shader->getObject(objectId)) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2550 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2551 | |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 2552 | SpirvShader::Operand::Operand(const EmitState *state, const Object &object) |
Nicolas Capens | 3c0e058 | 2020-08-26 11:04:37 -0400 | [diff] [blame] | 2553 | : constant(object.kind == SpirvShader::Object::Kind::Constant ? object.constantValue.data() : nullptr) |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 2554 | , intermediate(object.kind == SpirvShader::Object::Kind::Intermediate ? &state->getIntermediate(object.id()) : nullptr) |
| 2555 | , componentCount(intermediate ? intermediate->componentCount : object.constantValue.size()) |
| 2556 | { |
Nicolas Capens | 3c0e058 | 2020-08-26 11:04:37 -0400 | [diff] [blame] | 2557 | ASSERT(intermediate || constant); |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 2558 | } |
| 2559 | |
Nicolas Capens | 20220a0 | 2020-04-09 02:48:16 -0400 | [diff] [blame] | 2560 | SpirvShader::Operand::Operand(const Intermediate &value) |
| 2561 | : constant(nullptr) |
| 2562 | , intermediate(&value) |
| 2563 | , componentCount(value.componentCount) |
| 2564 | { |
| 2565 | } |
| 2566 | |
Nicolas Capens | 3c0e058 | 2020-08-26 11:04:37 -0400 | [diff] [blame] | 2567 | bool SpirvShader::Operand::isConstantZero() const |
| 2568 | { |
| 2569 | if(!constant) |
| 2570 | { |
| 2571 | return false; |
| 2572 | } |
| 2573 | |
| 2574 | for(uint32_t i = 0; i < componentCount; i++) |
| 2575 | { |
| 2576 | if(constant[i] != 0) |
| 2577 | { |
| 2578 | return false; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | return true; |
| 2583 | } |
| 2584 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2585 | SpirvRoutine::SpirvRoutine(vk::PipelineLayout const *pipelineLayout) |
| 2586 | : pipelineLayout(pipelineLayout) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2587 | { |
| 2588 | } |
| 2589 | |
| 2590 | void SpirvRoutine::setImmutableInputBuiltins(SpirvShader const *shader) |
| 2591 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2592 | setInputBuiltin(shader, spv::BuiltInSubgroupLocalInvocationId, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2593 | ASSERT(builtin.SizeInComponents == 1); |
| 2594 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(0, 1, 2, 3)); |
| 2595 | }); |
| 2596 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2597 | setInputBuiltin(shader, spv::BuiltInSubgroupEqMask, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2598 | ASSERT(builtin.SizeInComponents == 4); |
| 2599 | value[builtin.FirstComponent + 0] = As<SIMD::Float>(SIMD::Int(1, 2, 4, 8)); |
| 2600 | value[builtin.FirstComponent + 1] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2601 | value[builtin.FirstComponent + 2] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2602 | value[builtin.FirstComponent + 3] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2603 | }); |
| 2604 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2605 | setInputBuiltin(shader, spv::BuiltInSubgroupGeMask, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2606 | ASSERT(builtin.SizeInComponents == 4); |
| 2607 | value[builtin.FirstComponent + 0] = As<SIMD::Float>(SIMD::Int(15, 14, 12, 8)); |
| 2608 | value[builtin.FirstComponent + 1] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2609 | value[builtin.FirstComponent + 2] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2610 | value[builtin.FirstComponent + 3] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2611 | }); |
| 2612 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2613 | setInputBuiltin(shader, spv::BuiltInSubgroupGtMask, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2614 | ASSERT(builtin.SizeInComponents == 4); |
| 2615 | value[builtin.FirstComponent + 0] = As<SIMD::Float>(SIMD::Int(14, 12, 8, 0)); |
| 2616 | value[builtin.FirstComponent + 1] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2617 | value[builtin.FirstComponent + 2] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2618 | value[builtin.FirstComponent + 3] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2619 | }); |
| 2620 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2621 | setInputBuiltin(shader, spv::BuiltInSubgroupLeMask, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2622 | ASSERT(builtin.SizeInComponents == 4); |
| 2623 | value[builtin.FirstComponent + 0] = As<SIMD::Float>(SIMD::Int(1, 3, 7, 15)); |
| 2624 | value[builtin.FirstComponent + 1] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2625 | value[builtin.FirstComponent + 2] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2626 | value[builtin.FirstComponent + 3] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2627 | }); |
| 2628 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2629 | setInputBuiltin(shader, spv::BuiltInSubgroupLtMask, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2630 | ASSERT(builtin.SizeInComponents == 4); |
| 2631 | value[builtin.FirstComponent + 0] = As<SIMD::Float>(SIMD::Int(0, 1, 3, 7)); |
| 2632 | value[builtin.FirstComponent + 1] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2633 | value[builtin.FirstComponent + 2] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2634 | value[builtin.FirstComponent + 3] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2635 | }); |
| 2636 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 2637 | setInputBuiltin(shader, spv::BuiltInDeviceIndex, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2638 | ASSERT(builtin.SizeInComponents == 1); |
| 2639 | // Only a single physical device is supported. |
| 2640 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 2641 | }); |
| 2642 | } |
| 2643 | |
| 2644 | } // namespace sw |