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