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