Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 15 | #include "SpirvShader.hpp" |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 16 | #include "SpirvShaderDebug.hpp" |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 17 | |
| 18 | #include "ShaderCore.hpp" |
Nicolas Capens | 9e727fa | 2021-11-22 12:06:33 -0500 | [diff] [blame] | 19 | #include "Reactor/Assert.hpp" |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 20 | #include "Vulkan/VkPipelineLayout.hpp" |
| 21 | |
| 22 | #include <spirv/unified1/spirv.hpp> |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 23 | |
| 24 | namespace sw { |
| 25 | |
| 26 | SpirvShader::EmitResult SpirvShader::EmitLoad(InsnIterator insn, EmitState *state) const |
| 27 | { |
| 28 | bool atomic = (insn.opcode() == spv::OpAtomicLoad); |
| 29 | Object::ID resultId = insn.word(2); |
| 30 | Object::ID pointerId = insn.word(3); |
| 31 | auto &result = getObject(resultId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 32 | auto &resultTy = getType(result); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 33 | auto &pointer = getObject(pointerId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 34 | auto &pointerTy = getType(pointer); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 35 | std::memory_order memoryOrder = std::memory_order_relaxed; |
| 36 | |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 37 | ASSERT(getType(pointer).element == result.typeId()); |
| 38 | ASSERT(Type::ID(insn.word(1)) == result.typeId()); |
| 39 | ASSERT(!atomic || getType(getType(pointer).element).opcode() == spv::OpTypeInt); // Vulkan 1.1: "Atomic instructions must declare a scalar 32-bit integer type, for the value pointed to by Pointer." |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 40 | |
| 41 | if(pointerTy.storageClass == spv::StorageClassUniformConstant) |
| 42 | { |
| 43 | // Just propagate the pointer. |
| 44 | auto &ptr = state->getPointer(pointerId); |
| 45 | state->createPointer(resultId, ptr); |
| 46 | return EmitResult::Continue; |
| 47 | } |
| 48 | |
| 49 | if(atomic) |
| 50 | { |
| 51 | Object::ID semanticsId = insn.word(5); |
| 52 | auto memorySemantics = static_cast<spv::MemorySemanticsMask>(getObject(semanticsId).constantValue[0]); |
| 53 | memoryOrder = MemoryOrder(memorySemantics); |
| 54 | } |
| 55 | |
| 56 | auto ptr = GetPointerToData(pointerId, 0, state); |
| 57 | bool interleavedByLane = IsStorageInterleavedByLane(pointerTy.storageClass); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 58 | auto &dst = state->createIntermediate(resultId, resultTy.componentCount); |
Alexis Hetu | 8941bde | 2021-11-17 17:45:40 -0500 | [diff] [blame^] | 59 | auto robustness = getOutOfBoundsBehavior(pointerId, state); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 60 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 61 | VisitMemoryObject(pointerId, [&](const MemoryElement &el) { |
Ben Clayton | 18c6a78 | 2019-12-03 12:08:16 +0000 | [diff] [blame] | 62 | auto p = ptr + el.offset; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 63 | if(interleavedByLane) { p = InterleaveByLane(p); } // TODO: Interleave once, then add offset? |
Ben Clayton | 18c6a78 | 2019-12-03 12:08:16 +0000 | [diff] [blame] | 64 | dst.move(el.index, p.Load<SIMD::Float>(robustness, state->activeLaneMask(), atomic, memoryOrder)); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 65 | }); |
| 66 | |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 67 | SPIRV_SHADER_DBG("Load(atomic: {0}, order: {1}, ptr: {2}, val: {3}, mask: {4})", atomic, int(memoryOrder), ptr, dst, state->activeLaneMask()); |
| 68 | |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 69 | return EmitResult::Continue; |
| 70 | } |
| 71 | |
| 72 | SpirvShader::EmitResult SpirvShader::EmitStore(InsnIterator insn, EmitState *state) const |
| 73 | { |
| 74 | bool atomic = (insn.opcode() == spv::OpAtomicStore); |
| 75 | Object::ID pointerId = insn.word(1); |
| 76 | Object::ID objectId = insn.word(atomic ? 4 : 2); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 77 | std::memory_order memoryOrder = std::memory_order_relaxed; |
| 78 | |
| 79 | if(atomic) |
| 80 | { |
| 81 | Object::ID semanticsId = insn.word(3); |
| 82 | auto memorySemantics = static_cast<spv::MemorySemanticsMask>(getObject(semanticsId).constantValue[0]); |
| 83 | memoryOrder = MemoryOrder(memorySemantics); |
| 84 | } |
| 85 | |
Nicolas Capens | 0b77aa5 | 2020-04-09 02:48:16 -0400 | [diff] [blame] | 86 | const auto &value = Operand(this, state, objectId); |
| 87 | |
| 88 | Store(pointerId, value, atomic, memoryOrder, state); |
| 89 | |
| 90 | return EmitResult::Continue; |
| 91 | } |
| 92 | |
| 93 | void SpirvShader::Store(Object::ID pointerId, const Operand &value, bool atomic, std::memory_order memoryOrder, EmitState *state) const |
| 94 | { |
| 95 | auto &pointer = getObject(pointerId); |
| 96 | auto &pointerTy = getType(pointer); |
| 97 | auto &elementTy = getType(pointerTy.element); |
| 98 | |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 99 | ASSERT(!atomic || elementTy.opcode() == spv::OpTypeInt); // Vulkan 1.1: "Atomic instructions must declare a scalar 32-bit integer type, for the value pointed to by Pointer." |
| 100 | |
| 101 | auto ptr = GetPointerToData(pointerId, 0, state); |
| 102 | bool interleavedByLane = IsStorageInterleavedByLane(pointerTy.storageClass); |
Alexis Hetu | 8941bde | 2021-11-17 17:45:40 -0500 | [diff] [blame^] | 103 | auto robustness = getOutOfBoundsBehavior(pointerId, state); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 104 | |
| 105 | SIMD::Int mask = state->activeLaneMask(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 106 | if(!StoresInHelperInvocation(pointerTy.storageClass)) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 107 | { |
| 108 | mask = mask & state->storesAndAtomicsMask(); |
| 109 | } |
| 110 | |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 111 | SPIRV_SHADER_DBG("Store(atomic: {0}, order: {1}, ptr: {2}, val: {3}, mask: {4}", atomic, int(memoryOrder), ptr, value, mask); |
| 112 | |
Nicolas Capens | 0b77aa5 | 2020-04-09 02:48:16 -0400 | [diff] [blame] | 113 | VisitMemoryObject(pointerId, [&](const MemoryElement &el) { |
| 114 | auto p = ptr + el.offset; |
| 115 | if(interleavedByLane) { p = InterleaveByLane(p); } |
| 116 | p.Store(value.Float(el.index), robustness, mask, atomic, memoryOrder); |
| 117 | }); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | SpirvShader::EmitResult SpirvShader::EmitVariable(InsnIterator insn, EmitState *state) const |
| 121 | { |
| 122 | auto routine = state->routine; |
| 123 | Object::ID resultId = insn.word(2); |
| 124 | auto &object = getObject(resultId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 125 | auto &objectTy = getType(object); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 126 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 127 | switch(objectTy.storageClass) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 128 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 129 | case spv::StorageClassOutput: |
| 130 | case spv::StorageClassPrivate: |
| 131 | case spv::StorageClassFunction: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 132 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 133 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
| 134 | auto base = &routine->getVariable(resultId)[0]; |
| 135 | auto elementTy = getType(objectTy.element); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 136 | auto size = elementTy.componentCount * static_cast<uint32_t>(sizeof(float)) * SIMD::Width; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 137 | state->createPointer(resultId, SIMD::Pointer(base, size)); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 138 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 139 | break; |
| 140 | case spv::StorageClassWorkgroup: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 141 | { |
| 142 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
| 143 | auto base = &routine->workgroupMemory[0]; |
| 144 | auto size = workgroupMemory.size(); |
| 145 | state->createPointer(resultId, SIMD::Pointer(base, size, workgroupMemory.offsetOf(resultId))); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 146 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 147 | break; |
| 148 | case spv::StorageClassInput: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 149 | { |
| 150 | if(object.kind == Object::Kind::InterfaceVariable) |
| 151 | { |
| 152 | auto &dst = routine->getVariable(resultId); |
| 153 | int offset = 0; |
| 154 | VisitInterface(resultId, |
| 155 | [&](Decorations const &d, AttribType type) { |
| 156 | auto scalarSlot = d.Location << 2 | d.Component; |
| 157 | dst[offset++] = routine->inputs[scalarSlot]; |
| 158 | }); |
| 159 | } |
| 160 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
| 161 | auto base = &routine->getVariable(resultId)[0]; |
| 162 | auto elementTy = getType(objectTy.element); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 163 | auto size = elementTy.componentCount * static_cast<uint32_t>(sizeof(float)) * SIMD::Width; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 164 | state->createPointer(resultId, SIMD::Pointer(base, size)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 165 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 166 | break; |
| 167 | case spv::StorageClassUniformConstant: |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 168 | { |
| 169 | const auto &d = descriptorDecorations.at(resultId); |
| 170 | ASSERT(d.DescriptorSet >= 0); |
| 171 | ASSERT(d.Binding >= 0); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 172 | |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 173 | uint32_t bindingOffset = routine->pipelineLayout->getBindingOffset(d.DescriptorSet, d.Binding); |
Nicolas Capens | ca9de96 | 2020-04-23 00:42:39 -0400 | [diff] [blame] | 174 | Pointer<Byte> set = routine->descriptorSets[d.DescriptorSet]; // DescriptorSet* |
| 175 | Pointer<Byte> binding = Pointer<Byte>(set + bindingOffset); // vk::SampledImageDescriptor* |
| 176 | auto size = 0; // Not required as this pointer is not directly used by SIMD::Read or SIMD::Write. |
| 177 | state->createPointer(resultId, SIMD::Pointer(binding, size)); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 178 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 179 | break; |
| 180 | case spv::StorageClassUniform: |
| 181 | case spv::StorageClassStorageBuffer: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 182 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 183 | const auto &d = descriptorDecorations.at(resultId); |
| 184 | ASSERT(d.DescriptorSet >= 0); |
| 185 | auto size = 0; // Not required as this pointer is not directly used by SIMD::Read or SIMD::Write. |
| 186 | // Note: the module may contain descriptor set references that are not suitable for this implementation -- using a set index higher than the number |
| 187 | // of descriptor set binding points we support. As long as the selected entrypoint doesn't actually touch the out of range binding points, this |
| 188 | // is valid. In this case make the value nullptr to make it easier to diagnose an attempt to dereference it. |
Nicolas Capens | b7b7cb7 | 2021-09-29 14:02:53 -0400 | [diff] [blame] | 189 | if(static_cast<uint32_t>(d.DescriptorSet) < vk::MAX_BOUND_DESCRIPTOR_SETS) |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 190 | { |
| 191 | state->createPointer(resultId, SIMD::Pointer(routine->descriptorSets[d.DescriptorSet], size)); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | state->createPointer(resultId, SIMD::Pointer(nullptr, 0)); |
| 196 | } |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 197 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 198 | break; |
| 199 | case spv::StorageClassPushConstant: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 200 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 201 | state->createPointer(resultId, SIMD::Pointer(routine->pushConstants, vk::MAX_PUSH_CONSTANT_SIZE)); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 202 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 203 | break; |
| 204 | default: |
| 205 | UNREACHABLE("Storage class %d", objectTy.storageClass); |
| 206 | break; |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 209 | if(insn.wordCount() > 4) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 210 | { |
| 211 | Object::ID initializerId = insn.word(4); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 212 | if(getObject(initializerId).kind != Object::Kind::Constant) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 213 | { |
Nicolas Capens | dd0e600 | 2020-01-24 01:21:47 -0500 | [diff] [blame] | 214 | UNIMPLEMENTED("b/148241854: Non-constant initializers not yet implemented"); // FIXME(b/148241854) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 215 | } |
Nicolas Capens | 44bd43a | 2020-01-22 03:07:14 -0500 | [diff] [blame] | 216 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 217 | switch(objectTy.storageClass) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 218 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 219 | case spv::StorageClassOutput: |
| 220 | case spv::StorageClassPrivate: |
| 221 | case spv::StorageClassFunction: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 222 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 223 | bool interleavedByLane = IsStorageInterleavedByLane(objectTy.storageClass); |
| 224 | auto ptr = GetPointerToData(resultId, 0, state); |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 225 | Operand initialValue(this, state, initializerId); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 226 | VisitMemoryObject(resultId, [&](const MemoryElement &el) { |
| 227 | auto p = ptr + el.offset; |
| 228 | if(interleavedByLane) { p = InterleaveByLane(p); } |
| 229 | auto robustness = OutOfBoundsBehavior::UndefinedBehavior; // Local variables are always within bounds. |
| 230 | p.Store(initialValue.Float(el.index), robustness, state->activeLaneMask()); |
| 231 | }); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 232 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 233 | break; |
| 234 | default: |
| 235 | ASSERT_MSG(initializerId == 0, "Vulkan does not permit variables of storage class %d to have initializers", int(objectTy.storageClass)); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | return EmitResult::Continue; |
| 240 | } |
| 241 | |
| 242 | SpirvShader::EmitResult SpirvShader::EmitCopyMemory(InsnIterator insn, EmitState *state) const |
| 243 | { |
| 244 | Object::ID dstPtrId = insn.word(1); |
| 245 | Object::ID srcPtrId = insn.word(2); |
Nicolas Capens | e7355b9 | 2021-11-08 22:48:34 -0500 | [diff] [blame] | 246 | auto &dstPtrTy = getObjectType(dstPtrId); |
| 247 | auto &srcPtrTy = getObjectType(srcPtrId); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 248 | ASSERT(dstPtrTy.element == srcPtrTy.element); |
| 249 | |
| 250 | bool dstInterleavedByLane = IsStorageInterleavedByLane(dstPtrTy.storageClass); |
| 251 | bool srcInterleavedByLane = IsStorageInterleavedByLane(srcPtrTy.storageClass); |
| 252 | auto dstPtr = GetPointerToData(dstPtrId, 0, state); |
| 253 | auto srcPtr = GetPointerToData(srcPtrId, 0, state); |
| 254 | |
| 255 | std::unordered_map<uint32_t, uint32_t> srcOffsets; |
| 256 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 257 | VisitMemoryObject(srcPtrId, [&](const MemoryElement &el) { srcOffsets[el.index] = el.offset; }); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 258 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 259 | VisitMemoryObject(dstPtrId, [&](const MemoryElement &el) { |
Ben Clayton | 18c6a78 | 2019-12-03 12:08:16 +0000 | [diff] [blame] | 260 | auto it = srcOffsets.find(el.index); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 261 | ASSERT(it != srcOffsets.end()); |
| 262 | auto srcOffset = it->second; |
Ben Clayton | 18c6a78 | 2019-12-03 12:08:16 +0000 | [diff] [blame] | 263 | auto dstOffset = el.offset; |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 264 | |
| 265 | auto dst = dstPtr + dstOffset; |
| 266 | auto src = srcPtr + srcOffset; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 267 | if(dstInterleavedByLane) { dst = InterleaveByLane(dst); } |
| 268 | if(srcInterleavedByLane) { src = InterleaveByLane(src); } |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 269 | |
| 270 | // TODO(b/131224163): Optimize based on src/dst storage classes. |
| 271 | auto robustness = OutOfBoundsBehavior::RobustBufferAccess; |
| 272 | |
| 273 | auto value = src.Load<SIMD::Float>(robustness, state->activeLaneMask()); |
| 274 | dst.Store(value, robustness, state->activeLaneMask()); |
| 275 | }); |
| 276 | return EmitResult::Continue; |
| 277 | } |
| 278 | |
| 279 | SpirvShader::EmitResult SpirvShader::EmitMemoryBarrier(InsnIterator insn, EmitState *state) const |
| 280 | { |
| 281 | auto semantics = spv::MemorySemanticsMask(GetConstScalarInt(insn.word(2))); |
Nicolas Capens | 4c62980 | 2021-12-08 02:05:19 -0500 | [diff] [blame] | 282 | // TODO(b/176819536): We probably want to consider the memory scope here. |
| 283 | // For now, just always emit the full fence. |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 284 | Fence(semantics); |
| 285 | return EmitResult::Continue; |
| 286 | } |
| 287 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 288 | void SpirvShader::VisitMemoryObjectInner(sw::SpirvShader::Type::ID id, sw::SpirvShader::Decorations d, uint32_t &index, uint32_t offset, const MemoryVisitor &f) const |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 289 | { |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 290 | ApplyDecorationsForId(&d, id); |
| 291 | auto const &type = getType(id); |
| 292 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 293 | if(d.HasOffset) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 294 | { |
| 295 | offset += d.Offset; |
| 296 | d.HasOffset = false; |
| 297 | } |
| 298 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 299 | switch(type.opcode()) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 300 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 301 | case spv::OpTypePointer: |
| 302 | VisitMemoryObjectInner(type.definition.word(3), d, index, offset, f); |
| 303 | break; |
| 304 | case spv::OpTypeInt: |
| 305 | case spv::OpTypeFloat: |
| 306 | case spv::OpTypeRuntimeArray: |
| 307 | f(MemoryElement{ index++, offset, type }); |
| 308 | break; |
| 309 | case spv::OpTypeVector: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 310 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 311 | auto elemStride = (d.InsideMatrix && d.HasRowMajor && d.RowMajor) ? d.MatrixStride : static_cast<int32_t>(sizeof(float)); |
| 312 | for(auto i = 0u; i < type.definition.word(3); i++) |
| 313 | { |
| 314 | VisitMemoryObjectInner(type.definition.word(2), d, index, offset + elemStride * i, f); |
| 315 | } |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 316 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 317 | break; |
| 318 | case spv::OpTypeMatrix: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 319 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 320 | auto columnStride = (d.HasRowMajor && d.RowMajor) ? static_cast<int32_t>(sizeof(float)) : d.MatrixStride; |
| 321 | d.InsideMatrix = true; |
| 322 | for(auto i = 0u; i < type.definition.word(3); i++) |
| 323 | { |
| 324 | ASSERT(d.HasMatrixStride); |
| 325 | VisitMemoryObjectInner(type.definition.word(2), d, index, offset + columnStride * i, f); |
| 326 | } |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 327 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 328 | break; |
| 329 | case spv::OpTypeStruct: |
| 330 | for(auto i = 0u; i < type.definition.wordCount() - 2; i++) |
| 331 | { |
| 332 | ApplyDecorationsForIdMember(&d, id, i); |
| 333 | VisitMemoryObjectInner(type.definition.word(i + 2), d, index, offset, f); |
| 334 | } |
| 335 | break; |
| 336 | case spv::OpTypeArray: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 337 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 338 | auto arraySize = GetConstScalarInt(type.definition.word(3)); |
| 339 | for(auto i = 0u; i < arraySize; i++) |
| 340 | { |
| 341 | ASSERT(d.HasArrayStride); |
| 342 | VisitMemoryObjectInner(type.definition.word(2), d, index, offset + i * d.ArrayStride, f); |
| 343 | } |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 344 | } |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 345 | break; |
| 346 | default: |
| 347 | UNREACHABLE("%s", OpcodeName(type.opcode())); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 351 | void SpirvShader::VisitMemoryObject(Object::ID id, const MemoryVisitor &f) const |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 352 | { |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 353 | auto typeId = getObject(id).typeId(); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 354 | auto const &type = getType(typeId); |
Nicolas Capens | 72f089c | 2020-04-08 23:37:08 -0400 | [diff] [blame] | 355 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 356 | if(IsExplicitLayout(type.storageClass)) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 357 | { |
| 358 | Decorations d{}; |
| 359 | ApplyDecorationsForId(&d, id); |
| 360 | uint32_t index = 0; |
| 361 | VisitMemoryObjectInner(typeId, d, index, 0, f); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | // Objects without explicit layout are tightly packed. |
Ben Clayton | 18c6a78 | 2019-12-03 12:08:16 +0000 | [diff] [blame] | 366 | auto &elType = getType(type.element); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 367 | for(auto index = 0u; index < elType.componentCount; index++) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 368 | { |
Ben Clayton | 18c6a78 | 2019-12-03 12:08:16 +0000 | [diff] [blame] | 369 | auto offset = static_cast<uint32_t>(index * sizeof(float)); |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 370 | f({ index, offset, elType }); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 375 | SIMD::Pointer SpirvShader::GetPointerToData(Object::ID id, Int arrayIndex, EmitState const *state) const |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 376 | { |
| 377 | auto routine = state->routine; |
| 378 | auto &object = getObject(id); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 379 | switch(object.kind) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 380 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 381 | case Object::Kind::Pointer: |
| 382 | case Object::Kind::InterfaceVariable: |
| 383 | return state->getPointer(id); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 384 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 385 | case Object::Kind::DescriptorSet: |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 386 | { |
| 387 | const auto &d = descriptorDecorations.at(id); |
Nicolas Capens | b7b7cb7 | 2021-09-29 14:02:53 -0400 | [diff] [blame] | 388 | ASSERT(d.DescriptorSet >= 0 && static_cast<uint32_t>(d.DescriptorSet) < vk::MAX_BOUND_DESCRIPTOR_SETS); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 389 | ASSERT(d.Binding >= 0); |
Nicolas Capens | eb68244 | 2020-06-01 15:24:52 -0400 | [diff] [blame] | 390 | ASSERT(routine->pipelineLayout->getDescriptorCount(d.DescriptorSet, d.Binding) != 0); // "If descriptorCount is zero this binding entry is reserved and the resource must not be accessed from any stage via this binding within any pipeline using the set layout." |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 391 | |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 392 | uint32_t bindingOffset = routine->pipelineLayout->getBindingOffset(d.DescriptorSet, d.Binding); |
| 393 | uint32_t descriptorSize = routine->pipelineLayout->getDescriptorSize(d.DescriptorSet, d.Binding); |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 394 | Int descriptorOffset = bindingOffset + descriptorSize * arrayIndex; |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 395 | |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 396 | auto set = state->getPointer(id); |
Nicolas Capens | 9e727fa | 2021-11-22 12:06:33 -0500 | [diff] [blame] | 397 | Assert(set.base != Pointer<Byte>(nullptr)); |
Alexis Hetu | 8941bde | 2021-11-17 17:45:40 -0500 | [diff] [blame^] | 398 | Pointer<Byte> descriptor = set.base + descriptorOffset; // BufferDescriptor* or inline uniform block |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 399 | |
Alexis Hetu | 8941bde | 2021-11-17 17:45:40 -0500 | [diff] [blame^] | 400 | auto descriptorType = routine->pipelineLayout->getDescriptorType(d.DescriptorSet, d.Binding); |
| 401 | if(descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 402 | { |
Alexis Hetu | 8941bde | 2021-11-17 17:45:40 -0500 | [diff] [blame^] | 403 | // Note: there is no bounds checking for inline uniform blocks. |
| 404 | // MAX_INLINE_UNIFORM_BLOCK_SIZE represents the maximum size of |
| 405 | // an inline uniform block, but this value should remain unused. |
| 406 | return SIMD::Pointer(descriptor, vk::MAX_INLINE_UNIFORM_BLOCK_SIZE); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 407 | } |
| 408 | else |
| 409 | { |
Alexis Hetu | 8941bde | 2021-11-17 17:45:40 -0500 | [diff] [blame^] | 410 | Pointer<Byte> data = *Pointer<Pointer<Byte>>(descriptor + OFFSET(vk::BufferDescriptor, ptr)); // void* |
| 411 | Int size = *Pointer<Int>(descriptor + OFFSET(vk::BufferDescriptor, sizeInBytes)); |
| 412 | |
| 413 | if(routine->pipelineLayout->isDescriptorDynamic(d.DescriptorSet, d.Binding)) |
| 414 | { |
| 415 | Int dynamicOffsetIndex = |
| 416 | routine->pipelineLayout->getDynamicOffsetIndex(d.DescriptorSet, d.Binding) + |
| 417 | arrayIndex; |
| 418 | Int offset = routine->descriptorDynamicOffsets[dynamicOffsetIndex]; |
| 419 | Int robustnessSize = *Pointer<Int>(descriptor + OFFSET(vk::BufferDescriptor, robustnessSize)); |
| 420 | |
| 421 | return SIMD::Pointer(data + offset, Min(size, robustnessSize - offset)); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | return SIMD::Pointer(data, size); |
| 426 | } |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 430 | default: |
| 431 | UNREACHABLE("Invalid pointer kind %d", int(object.kind)); |
| 432 | return SIMD::Pointer(Pointer<Byte>(), 0); |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Nicolas Capens | 4c62980 | 2021-12-08 02:05:19 -0500 | [diff] [blame] | 436 | void SpirvShader::Fence(spv::MemorySemanticsMask semantics) const |
| 437 | { |
| 438 | if(semantics != spv::MemorySemanticsMaskNone) |
| 439 | { |
| 440 | rr::Fence(MemoryOrder(semantics)); |
| 441 | } |
| 442 | } |
| 443 | |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 444 | std::memory_order SpirvShader::MemoryOrder(spv::MemorySemanticsMask memorySemantics) |
| 445 | { |
Nicolas Capens | 4c62980 | 2021-12-08 02:05:19 -0500 | [diff] [blame] | 446 | uint32_t control = static_cast<uint32_t>(memorySemantics) & static_cast<uint32_t>( |
| 447 | spv::MemorySemanticsAcquireMask | |
| 448 | spv::MemorySemanticsReleaseMask | |
| 449 | spv::MemorySemanticsAcquireReleaseMask | |
| 450 | spv::MemorySemanticsSequentiallyConsistentMask); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 451 | switch(control) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 452 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 453 | case spv::MemorySemanticsMaskNone: return std::memory_order_relaxed; |
| 454 | case spv::MemorySemanticsAcquireMask: return std::memory_order_acquire; |
| 455 | case spv::MemorySemanticsReleaseMask: return std::memory_order_release; |
| 456 | case spv::MemorySemanticsAcquireReleaseMask: return std::memory_order_acq_rel; |
| 457 | case spv::MemorySemanticsSequentiallyConsistentMask: return std::memory_order_acq_rel; // Vulkan 1.1: "SequentiallyConsistent is treated as AcquireRelease" |
| 458 | default: |
| 459 | // "it is invalid for more than one of these four bits to be set: |
Nicolas Capens | 4c62980 | 2021-12-08 02:05:19 -0500 | [diff] [blame] | 460 | // Acquire, Release, AcquireRelease, or SequentiallyConsistent." |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 461 | UNREACHABLE("MemorySemanticsMask: %x", int(control)); |
| 462 | return std::memory_order_acq_rel; |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | bool SpirvShader::StoresInHelperInvocation(spv::StorageClass storageClass) |
| 467 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 468 | switch(storageClass) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 469 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 470 | case spv::StorageClassUniform: |
| 471 | case spv::StorageClassStorageBuffer: |
| 472 | case spv::StorageClassImage: |
| 473 | return false; |
| 474 | default: |
| 475 | return true; |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | bool SpirvShader::IsExplicitLayout(spv::StorageClass storageClass) |
| 480 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 481 | switch(storageClass) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 482 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 483 | case spv::StorageClassUniform: |
| 484 | case spv::StorageClassStorageBuffer: |
| 485 | case spv::StorageClassPushConstant: |
| 486 | return true; |
| 487 | default: |
| 488 | return false; |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | sw::SIMD::Pointer SpirvShader::InterleaveByLane(sw::SIMD::Pointer p) |
| 493 | { |
| 494 | p *= sw::SIMD::Width; |
| 495 | p.staticOffsets[0] += 0 * sizeof(float); |
| 496 | p.staticOffsets[1] += 1 * sizeof(float); |
| 497 | p.staticOffsets[2] += 2 * sizeof(float); |
| 498 | p.staticOffsets[3] += 3 * sizeof(float); |
| 499 | return p; |
| 500 | } |
| 501 | |
| 502 | bool SpirvShader::IsStorageInterleavedByLane(spv::StorageClass storageClass) |
| 503 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 504 | switch(storageClass) |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 505 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 506 | case spv::StorageClassUniform: |
| 507 | case spv::StorageClassStorageBuffer: |
| 508 | case spv::StorageClassPushConstant: |
| 509 | case spv::StorageClassWorkgroup: |
| 510 | case spv::StorageClassImage: |
| 511 | return false; |
| 512 | default: |
| 513 | return true; |
Ben Clayton | f3e2cc2 | 2019-11-28 12:02:15 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | } // namespace sw |