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" |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 16 | #include "SamplerCore.hpp" |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 17 | |
| 18 | #include "Reactor/Coroutine.hpp" |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 19 | #include "System/Math.hpp" |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 20 | #include "Vulkan/VkBuffer.hpp" |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 21 | #include "Vulkan/VkBufferView.hpp" |
Chris Forbes | ebe5f7f | 2019-01-16 10:38:34 -0800 | [diff] [blame] | 22 | #include "Vulkan/VkDebug.hpp" |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 23 | #include "Vulkan/VkDescriptorSet.hpp" |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 24 | #include "Vulkan/VkPipelineLayout.hpp" |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 25 | #include "Vulkan/VkDescriptorSetLayout.hpp" |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 26 | #include "Vulkan/VkRenderPass.hpp" |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 27 | #include "Device/Config.hpp" |
| 28 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 29 | #include <spirv/unified1/spirv.hpp> |
| 30 | #include <spirv/unified1/GLSL.std.450.h> |
| 31 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 32 | #ifdef Bool |
| 33 | #undef Bool // b/127920555 |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 34 | #undef None |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 37 | namespace |
| 38 | { |
Ben Clayton | a15fcf4 | 2019-04-09 13:04:51 -0400 | [diff] [blame] | 39 | constexpr float PI = 3.141592653589793f; |
| 40 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 41 | rr::RValue<rr::Bool> AnyTrue(rr::RValue<sw::SIMD::Int> const &ints) |
| 42 | { |
| 43 | return rr::SignMask(ints) != 0; |
| 44 | } |
| 45 | |
| 46 | rr::RValue<rr::Bool> AnyFalse(rr::RValue<sw::SIMD::Int> const &ints) |
| 47 | { |
| 48 | return rr::SignMask(~ints) != 0; |
| 49 | } |
Ben Clayton | d86db95 | 2019-04-08 13:43:11 -0400 | [diff] [blame] | 50 | |
| 51 | // Returns 1 << bits. |
| 52 | // If the resulting bit overflows a 32 bit integer, 0 is returned. |
| 53 | rr::RValue<sw::SIMD::UInt> NthBit32(rr::RValue<sw::SIMD::UInt> const &bits) |
| 54 | { |
| 55 | return ((sw::SIMD::UInt(1) << bits) & rr::CmpLT(bits, sw::SIMD::UInt(32))); |
| 56 | } |
| 57 | |
| 58 | // Returns bitCount number of of 1's starting from the LSB. |
| 59 | rr::RValue<sw::SIMD::UInt> Bitmask32(rr::RValue<sw::SIMD::UInt> const &bitCount) |
| 60 | { |
| 61 | return NthBit32(bitCount) - sw::SIMD::UInt(1); |
| 62 | } |
Ben Clayton | 6caf821 | 2019-04-09 11:28:39 -0400 | [diff] [blame] | 63 | |
| 64 | // Performs a fused-multiply add, returning a * b + c. |
| 65 | rr::RValue<sw::SIMD::Float> FMA( |
| 66 | rr::RValue<sw::SIMD::Float> const &a, |
| 67 | rr::RValue<sw::SIMD::Float> const &b, |
| 68 | rr::RValue<sw::SIMD::Float> const &c) |
| 69 | { |
| 70 | return a * b + c; |
| 71 | } |
Ben Clayton | 20f6ba8 | 2019-04-09 12:07:29 -0400 | [diff] [blame] | 72 | |
| 73 | // Returns the exponent of the floating point number f. |
| 74 | // Assumes IEEE 754 |
| 75 | rr::RValue<sw::SIMD::Int> Exponent(rr::RValue<sw::SIMD::Float> f) |
| 76 | { |
| 77 | auto v = rr::As<sw::SIMD::UInt>(f); |
| 78 | return (sw::SIMD::Int((v >> sw::SIMD::UInt(23)) & sw::SIMD::UInt(0xFF)) - sw::SIMD::Int(126)); |
| 79 | } |
Ben Clayton | ee10bcf | 2019-04-09 17:01:01 -0400 | [diff] [blame] | 80 | |
| 81 | // Returns y if y < x; otherwise result is x. |
| 82 | // If one operand is a NaN, the other operand is the result. |
| 83 | // If both operands are NaN, the result is a NaN. |
| 84 | rr::RValue<sw::SIMD::Float> NMin(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y) |
| 85 | { |
| 86 | using namespace rr; |
| 87 | auto xIsNan = IsNan(x); |
| 88 | auto yIsNan = IsNan(y); |
| 89 | return As<sw::SIMD::Float>( |
| 90 | // If neither are NaN, return min |
| 91 | ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Min(x, y))) | |
| 92 | // If one operand is a NaN, the other operand is the result |
| 93 | // If both operands are NaN, the result is a NaN. |
| 94 | ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) | |
| 95 | (( xIsNan ) & As<sw::SIMD::Int>(y))); |
| 96 | } |
Ben Clayton | 02de7e0 | 2019-04-09 17:01:26 -0400 | [diff] [blame] | 97 | |
| 98 | // Returns y if y > x; otherwise result is x. |
| 99 | // If one operand is a NaN, the other operand is the result. |
| 100 | // If both operands are NaN, the result is a NaN. |
| 101 | rr::RValue<sw::SIMD::Float> NMax(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y) |
| 102 | { |
| 103 | using namespace rr; |
| 104 | auto xIsNan = IsNan(x); |
| 105 | auto yIsNan = IsNan(y); |
| 106 | return As<sw::SIMD::Float>( |
| 107 | // If neither are NaN, return max |
| 108 | ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Max(x, y))) | |
| 109 | // If one operand is a NaN, the other operand is the result |
| 110 | // If both operands are NaN, the result is a NaN. |
| 111 | ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) | |
| 112 | (( xIsNan ) & As<sw::SIMD::Int>(y))); |
| 113 | } |
Ben Clayton | 1fb633c | 2019-04-09 17:24:59 -0400 | [diff] [blame] | 114 | |
| 115 | // Returns the determinant of a 2x2 matrix. |
| 116 | rr::RValue<sw::SIMD::Float> Determinant( |
| 117 | rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, |
| 118 | rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d) |
| 119 | { |
| 120 | return a*d - b*c; |
| 121 | } |
| 122 | |
| 123 | // Returns the determinant of a 3x3 matrix. |
| 124 | rr::RValue<sw::SIMD::Float> Determinant( |
| 125 | rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c, |
| 126 | rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f, |
| 127 | rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i) |
| 128 | { |
| 129 | return a*e*i + b*f*g + c*d*h - c*e*g - b*d*i - a*f*h; |
| 130 | } |
| 131 | |
| 132 | // Returns the determinant of a 4x4 matrix. |
| 133 | rr::RValue<sw::SIMD::Float> Determinant( |
| 134 | rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d, |
| 135 | rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f, rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, |
| 136 | rr::RValue<sw::SIMD::Float> const &i, rr::RValue<sw::SIMD::Float> const &j, rr::RValue<sw::SIMD::Float> const &k, rr::RValue<sw::SIMD::Float> const &l, |
| 137 | rr::RValue<sw::SIMD::Float> const &m, rr::RValue<sw::SIMD::Float> const &n, rr::RValue<sw::SIMD::Float> const &o, rr::RValue<sw::SIMD::Float> const &p) |
| 138 | { |
| 139 | return a * Determinant(f, g, h, |
| 140 | j, k, l, |
| 141 | n, o, p) - |
| 142 | b * Determinant(e, g, h, |
| 143 | i, k, l, |
| 144 | m, o, p) + |
| 145 | c * Determinant(e, f, h, |
| 146 | i, j, l, |
| 147 | m, n, p) - |
| 148 | d * Determinant(e, f, g, |
| 149 | i, j, k, |
| 150 | m, n, o); |
| 151 | } |
Ben Clayton | 445a44a | 2019-04-10 16:37:19 -0400 | [diff] [blame] | 152 | |
| 153 | // Returns the inverse of a 2x2 matrix. |
| 154 | std::array<rr::RValue<sw::SIMD::Float>, 4> MatrixInverse( |
| 155 | rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, |
| 156 | rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d) |
| 157 | { |
| 158 | auto s = sw::SIMD::Float(1.0f) / Determinant(a, b, c, d); |
| 159 | return {{s*d, -s*b, -s*c, s*a}}; |
| 160 | } |
| 161 | |
| 162 | // Returns the inverse of a 3x3 matrix. |
| 163 | std::array<rr::RValue<sw::SIMD::Float>, 9> MatrixInverse( |
| 164 | rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c, |
| 165 | rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f, |
| 166 | rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i) |
| 167 | { |
| 168 | auto s = sw::SIMD::Float(1.0f) / Determinant( |
| 169 | a, b, c, |
| 170 | d, e, f, |
| 171 | g, h, i); // TODO: duplicate arithmetic calculating the det and below. |
| 172 | |
| 173 | return {{ |
| 174 | s * (e*i - f*h), s * (c*h - b*i), s * (b*f - c*e), |
| 175 | s * (f*g - d*i), s * (a*i - c*g), s * (c*d - a*f), |
| 176 | s * (d*h - e*g), s * (b*g - a*h), s * (a*e - b*d), |
| 177 | }}; |
| 178 | } |
| 179 | |
| 180 | // Returns the inverse of a 4x4 matrix. |
| 181 | std::array<rr::RValue<sw::SIMD::Float>, 16> MatrixInverse( |
| 182 | rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d, |
| 183 | rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f, rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, |
| 184 | rr::RValue<sw::SIMD::Float> const &i, rr::RValue<sw::SIMD::Float> const &j, rr::RValue<sw::SIMD::Float> const &k, rr::RValue<sw::SIMD::Float> const &l, |
| 185 | rr::RValue<sw::SIMD::Float> const &m, rr::RValue<sw::SIMD::Float> const &n, rr::RValue<sw::SIMD::Float> const &o, rr::RValue<sw::SIMD::Float> const &p) |
| 186 | { |
| 187 | auto s = sw::SIMD::Float(1.0f) / Determinant( |
| 188 | a, b, c, d, |
| 189 | e, f, g, h, |
| 190 | i, j, k, l, |
| 191 | m, n, o, p); // TODO: duplicate arithmetic calculating the det and below. |
| 192 | |
| 193 | auto kplo = k*p - l*o, jpln = j*p - l*n, jokn = j*o - k*n; |
| 194 | auto gpho = g*p - h*o, fphn = f*p - h*n, fogn = f*o - g*n; |
| 195 | auto glhk = g*l - h*k, flhj = f*l - h*j, fkgj = f*k - g*j; |
| 196 | auto iplm = i*p - l*m, iokm = i*o - k*m, ephm = e*p - h*m; |
| 197 | auto eogm = e*o - g*m, elhi = e*l - h*i, ekgi = e*k - g*i; |
| 198 | auto injm = i*n - j*m, enfm = e*n - f*m, ejfi = e*j - f*i; |
| 199 | |
| 200 | return {{ |
| 201 | s * ( f * kplo - g * jpln + h * jokn), |
| 202 | s * (-b * kplo + c * jpln - d * jokn), |
| 203 | s * ( b * gpho - c * fphn + d * fogn), |
| 204 | s * (-b * glhk + c * flhj - d * fkgj), |
| 205 | |
| 206 | s * (-e * kplo + g * iplm - h * iokm), |
| 207 | s * ( a * kplo - c * iplm + d * iokm), |
| 208 | s * (-a * gpho + c * ephm - d * eogm), |
| 209 | s * ( a * glhk - c * elhi + d * ekgi), |
| 210 | |
| 211 | s * ( e * jpln - f * iplm + h * injm), |
| 212 | s * (-a * jpln + b * iplm - d * injm), |
| 213 | s * ( a * fphn - b * ephm + d * enfm), |
| 214 | s * (-a * flhj + b * elhi - d * ejfi), |
| 215 | |
| 216 | s * (-e * jokn + f * iokm - g * injm), |
| 217 | s * ( a * jokn - b * iokm + c * injm), |
| 218 | s * (-a * fogn + b * eogm - c * enfm), |
| 219 | s * ( a * fkgj - b * ekgi + c * ejfi), |
| 220 | }}; |
| 221 | } |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 222 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 223 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 224 | sw::SIMD::Pointer interleaveByLane(sw::SIMD::Pointer p) |
| 225 | { |
| 226 | p *= sw::SIMD::Width; |
| 227 | p.staticOffsets[0] += 0 * sizeof(float); |
| 228 | p.staticOffsets[1] += 1 * sizeof(float); |
| 229 | p.staticOffsets[2] += 2 * sizeof(float); |
| 230 | p.staticOffsets[3] += 3 * sizeof(float); |
| 231 | return p; |
| 232 | } |
| 233 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 234 | VkFormat SpirvFormatToVulkanFormat(spv::ImageFormat format) |
| 235 | { |
| 236 | switch (format) |
| 237 | { |
| 238 | case spv::ImageFormatRgba32f: return VK_FORMAT_R32G32B32A32_SFLOAT; |
| 239 | case spv::ImageFormatRgba32i: return VK_FORMAT_R32G32B32A32_SINT; |
| 240 | case spv::ImageFormatRgba32ui: return VK_FORMAT_R32G32B32A32_UINT; |
| 241 | case spv::ImageFormatR32f: return VK_FORMAT_R32_SFLOAT; |
| 242 | case spv::ImageFormatR32i: return VK_FORMAT_R32_SINT; |
| 243 | case spv::ImageFormatR32ui: return VK_FORMAT_R32_UINT; |
| 244 | case spv::ImageFormatRgba8: return VK_FORMAT_R8G8B8A8_UNORM; |
| 245 | case spv::ImageFormatRgba8Snorm: return VK_FORMAT_R8G8B8A8_SNORM; |
| 246 | case spv::ImageFormatRgba8i: return VK_FORMAT_R8G8B8A8_SINT; |
| 247 | case spv::ImageFormatRgba8ui: return VK_FORMAT_R8G8B8A8_UINT; |
| 248 | case spv::ImageFormatRgba16f: return VK_FORMAT_R16G16B16A16_SFLOAT; |
| 249 | case spv::ImageFormatRgba16i: return VK_FORMAT_R16G16B16A16_SINT; |
| 250 | case spv::ImageFormatRgba16ui: return VK_FORMAT_R16G16B16A16_UINT; |
| 251 | |
| 252 | default: |
| 253 | UNIMPLEMENTED("SPIR-V ImageFormat %u", format); |
| 254 | return VK_FORMAT_UNDEFINED; |
| 255 | } |
| 256 | } |
| 257 | |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 258 | sw::SIMD::Float sRGBtoLinear(sw::SIMD::Float c) |
| 259 | { |
| 260 | sw::SIMD::Float lc = c * sw::SIMD::Float(1.0f / 12.92f); |
| 261 | sw::SIMD::Float ec = sw::power((c + sw::SIMD::Float(0.055f)) * sw::SIMD::Float(1.0f / 1.055f), sw::SIMD::Float(2.4f)); |
| 262 | |
| 263 | sw::SIMD::Int linear = CmpLT(c, sw::SIMD::Float(0.04045f)); |
| 264 | |
| 265 | sw::SIMD::Float s = c; |
| 266 | s.xyz = rr::As<sw::SIMD::Float>((linear & rr::As<sw::SIMD::Int>(lc)) | (~linear & rr::As<sw::SIMD::Int>(ec))); // FIXME: IfThenElse() |
| 267 | |
| 268 | return s; |
| 269 | } |
| 270 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 271 | } // anonymous namespace |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 272 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 273 | namespace sw |
| 274 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 275 | namespace SIMD |
| 276 | { |
| 277 | |
| 278 | template<typename T> |
| 279 | T Load(Pointer ptr, Int mask, bool atomic /* = false */, std::memory_order order /* = std::memory_order_relaxed */) |
| 280 | { |
| 281 | using EL = typename Element<T>::type; |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 282 | auto offsets = ptr.offsets(); |
| 283 | mask &= CmpLT(offsets + SIMD::Int(sizeof(float) - 1), SIMD::Int(ptr.limit)); // Disable OOB reads. |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 284 | if (!atomic && order == std::memory_order_relaxed) |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 285 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 286 | return rr::Gather(rr::Pointer<EL>(ptr.base), offsets, mask, sizeof(float)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 287 | } |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 288 | else |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 289 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 290 | T out; |
| 291 | auto anyLanesDisabled = AnyFalse(mask); |
| 292 | If(ptr.hasEqualOffsets() && !anyLanesDisabled) |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 293 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 294 | // Load one, replicate. |
| 295 | auto offset = Extract(offsets, 0); |
| 296 | out = T(rr::Load(rr::Pointer<EL>(&ptr.base[offset]), sizeof(float), atomic, order)); |
| 297 | } |
| 298 | Else If(ptr.hasSequentialOffsets() && !anyLanesDisabled) |
| 299 | { |
| 300 | // Load all elements in a single SIMD instruction. |
| 301 | auto offset = Extract(offsets, 0); |
| 302 | out = rr::Load(rr::Pointer<T>(&ptr.base[offset]), sizeof(float), atomic, order); |
| 303 | } |
| 304 | Else |
| 305 | { |
| 306 | // Divergent offsets or masked lanes. |
| 307 | out = T(0); |
| 308 | for (int i = 0; i < SIMD::Width; i++) |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 309 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 310 | If(Extract(mask, i) != 0) |
| 311 | { |
| 312 | auto offset = Extract(offsets, i); |
| 313 | auto el = rr::Load(rr::Pointer<EL>(&ptr.base[offset]), sizeof(float), atomic, order); |
| 314 | out = Insert(out, el, i); |
| 315 | } |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 316 | } |
| 317 | } |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 318 | return out; |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 319 | } |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | template<typename T> |
| 323 | void Store(Pointer ptr, T val, Int mask, bool atomic /* = false */, std::memory_order order /* = std::memory_order_relaxed */) |
| 324 | { |
| 325 | using EL = typename Element<T>::type; |
| 326 | auto offsets = ptr.offsets(); |
| 327 | mask &= CmpLT(offsets + SIMD::Int(sizeof(float) - 1), SIMD::Int(ptr.limit)); // Disable OOB reads. |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 328 | if (!atomic && order == std::memory_order_relaxed) |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 329 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 330 | return rr::Scatter(rr::Pointer<EL>(ptr.base), val, offsets, mask, sizeof(float)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 331 | } |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 332 | else |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 333 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 334 | auto anyLanesDisabled = AnyFalse(mask); |
| 335 | If(ptr.hasSequentialOffsets() && !anyLanesDisabled) |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 336 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 337 | // Store all elements in a single SIMD instruction. |
| 338 | auto offset = Extract(offsets, 0); |
| 339 | Store(val, rr::Pointer<T>(&ptr.base[offset]), sizeof(float), atomic, order); |
| 340 | } |
| 341 | Else |
| 342 | { |
| 343 | // Divergent offsets or masked lanes. |
| 344 | for (int i = 0; i < SIMD::Width; i++) |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 345 | { |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 346 | If(Extract(mask, i) != 0) |
| 347 | { |
| 348 | auto offset = Extract(offsets, i); |
| 349 | rr::Store(Extract(val, i), rr::Pointer<EL>(&ptr.base[offset]), sizeof(float), atomic, order); |
| 350 | } |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | } // namespace SIMD |
| 357 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 358 | volatile int SpirvShader::serialCounter = 1; // Start at 1, 0 is invalid shader. |
| 359 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 360 | SpirvShader::SpirvShader( |
| 361 | VkPipelineShaderStageCreateInfo const *createInfo, |
| 362 | InsnStore const &insns, |
| 363 | vk::RenderPass *renderPass, |
| 364 | uint32_t subpassIndex) |
| 365 | : insns{insns}, inputs{MAX_INTERFACE_COMPONENTS}, |
| 366 | outputs{MAX_INTERFACE_COMPONENTS}, |
| 367 | serialID{serialCounter++}, modes{} |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 368 | { |
Ben Clayton | 45faa08 | 2019-03-05 13:20:40 +0000 | [diff] [blame] | 369 | ASSERT(insns.size() > 0); |
| 370 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 371 | if (renderPass != VK_NULL_HANDLE) |
| 372 | { |
| 373 | // capture formats of any input attachments present |
| 374 | auto subpass = renderPass->getSubpass(subpassIndex); |
| 375 | inputAttachmentFormats.reserve(subpass.inputAttachmentCount); |
| 376 | for (auto i = 0u; i < subpass.inputAttachmentCount; i++) |
| 377 | { |
| 378 | auto attachmentIndex = subpass.pInputAttachments[i].attachment; |
| 379 | inputAttachmentFormats.push_back(attachmentIndex != VK_ATTACHMENT_UNUSED |
| 380 | ? renderPass->getAttachment(attachmentIndex).format : VK_FORMAT_UNDEFINED); |
| 381 | } |
| 382 | } |
| 383 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 384 | // Simplifying assumptions (to be satisfied by earlier transformations) |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 385 | // - The only input/output OpVariables present are those used by the entrypoint |
| 386 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 387 | Object::ID entryPointFunctionId; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 388 | Block::ID currentBlock; |
| 389 | InsnIterator blockStart; |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 390 | |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 391 | for (auto insn : *this) |
| 392 | { |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 393 | spv::Op opcode = insn.opcode(); |
| 394 | |
| 395 | switch (opcode) |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 396 | { |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 397 | case spv::OpEntryPoint: |
| 398 | { |
| 399 | auto executionModel = spv::ExecutionModel(insn.word(1)); |
| 400 | auto id = Object::ID(insn.word(2)); |
| 401 | auto name = insn.string(3); |
| 402 | auto stage = executionModelToStage(executionModel); |
| 403 | if (stage == createInfo->stage && strcmp(name, createInfo->pName) == 0) |
| 404 | { |
| 405 | ASSERT_MSG(entryPointFunctionId == 0, "Duplicate entry point with name '%s' and stage %d", name, int(stage)); |
| 406 | entryPointFunctionId = id; |
| 407 | } |
| 408 | break; |
| 409 | } |
| 410 | |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 411 | case spv::OpExecutionMode: |
| 412 | ProcessExecutionMode(insn); |
| 413 | break; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 414 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 415 | case spv::OpDecorate: |
| 416 | { |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 417 | TypeOrObjectID targetId = insn.word(1); |
Chris Forbes | 93f70b3 | 2019-02-10 21:26:27 +0000 | [diff] [blame] | 418 | auto decoration = static_cast<spv::Decoration>(insn.word(2)); |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 419 | uint32_t value = insn.wordCount() > 3 ? insn.word(3) : 0; |
| 420 | |
| 421 | decorations[targetId].Apply(decoration, value); |
| 422 | |
| 423 | switch(decoration) |
| 424 | { |
| 425 | case spv::DecorationDescriptorSet: |
| 426 | descriptorDecorations[targetId].DescriptorSet = value; |
| 427 | break; |
| 428 | case spv::DecorationBinding: |
| 429 | descriptorDecorations[targetId].Binding = value; |
| 430 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 431 | case spv::DecorationInputAttachmentIndex: |
| 432 | descriptorDecorations[targetId].InputAttachmentIndex = value; |
| 433 | break; |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 434 | default: |
| 435 | // Only handling descriptor decorations here. |
| 436 | break; |
| 437 | } |
Chris Forbes | 93f70b3 | 2019-02-10 21:26:27 +0000 | [diff] [blame] | 438 | |
| 439 | if (decoration == spv::DecorationCentroid) |
| 440 | modes.NeedsCentroid = true; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 441 | break; |
| 442 | } |
| 443 | |
| 444 | case spv::OpMemberDecorate: |
| 445 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 446 | Type::ID targetId = insn.word(1); |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 447 | auto memberIndex = insn.word(2); |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 448 | auto decoration = static_cast<spv::Decoration>(insn.word(3)); |
| 449 | uint32_t value = insn.wordCount() > 4 ? insn.word(4) : 0; |
| 450 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 451 | auto &d = memberDecorations[targetId]; |
| 452 | if (memberIndex >= d.size()) |
| 453 | d.resize(memberIndex + 1); // on demand; exact size would require another pass... |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 454 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 455 | d[memberIndex].Apply(decoration, value); |
Chris Forbes | 93f70b3 | 2019-02-10 21:26:27 +0000 | [diff] [blame] | 456 | |
| 457 | if (decoration == spv::DecorationCentroid) |
| 458 | modes.NeedsCentroid = true; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | |
| 462 | case spv::OpDecorationGroup: |
| 463 | // Nothing to do here. We don't need to record the definition of the group; we'll just have |
| 464 | // the bundle of decorations float around. If we were to ever walk the decorations directly, |
| 465 | // we might think about introducing this as a real Object. |
| 466 | break; |
| 467 | |
| 468 | case spv::OpGroupDecorate: |
| 469 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 470 | uint32_t group = insn.word(1); |
| 471 | auto const &groupDecorations = decorations[group]; |
| 472 | auto const &descriptorGroupDecorations = descriptorDecorations[group]; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 473 | for (auto i = 2u; i < insn.wordCount(); i++) |
| 474 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 475 | // Remaining operands are targets to apply the group to. |
| 476 | uint32_t target = insn.word(i); |
| 477 | decorations[target].Apply(groupDecorations); |
| 478 | descriptorDecorations[target].Apply(descriptorGroupDecorations); |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 479 | } |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 480 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | |
| 484 | case spv::OpGroupMemberDecorate: |
| 485 | { |
| 486 | auto const &srcDecorations = decorations[insn.word(1)]; |
| 487 | for (auto i = 2u; i < insn.wordCount(); i += 2) |
| 488 | { |
| 489 | // remaining operands are pairs of <id>, literal for members to apply to. |
| 490 | auto &d = memberDecorations[insn.word(i)]; |
| 491 | auto memberIndex = insn.word(i + 1); |
| 492 | if (memberIndex >= d.size()) |
| 493 | d.resize(memberIndex + 1); // on demand resize, see above... |
| 494 | d[memberIndex].Apply(srcDecorations); |
| 495 | } |
| 496 | break; |
| 497 | } |
| 498 | |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 499 | case spv::OpLabel: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 500 | { |
| 501 | ASSERT(currentBlock.value() == 0); |
| 502 | currentBlock = Block::ID(insn.word(1)); |
| 503 | blockStart = insn; |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 504 | break; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 505 | } |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 506 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 507 | // Branch Instructions (subset of Termination Instructions): |
| 508 | case spv::OpBranch: |
| 509 | case spv::OpBranchConditional: |
| 510 | case spv::OpSwitch: |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 511 | case spv::OpReturn: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 512 | // fallthrough |
| 513 | |
| 514 | // Termination instruction: |
| 515 | case spv::OpKill: |
| 516 | case spv::OpUnreachable: |
| 517 | { |
| 518 | ASSERT(currentBlock.value() != 0); |
| 519 | auto blockEnd = insn; blockEnd++; |
| 520 | blocks[currentBlock] = Block(blockStart, blockEnd); |
| 521 | currentBlock = Block::ID(0); |
| 522 | |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 523 | if (opcode == spv::OpKill) |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 524 | { |
| 525 | modes.ContainsKill = true; |
| 526 | } |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 527 | break; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 528 | } |
Chris Forbes | e57f10e | 2019-03-04 10:53:07 -0800 | [diff] [blame] | 529 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 530 | case spv::OpLoopMerge: |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 531 | case spv::OpSelectionMerge: |
| 532 | break; // Nothing to do in analysis pass. |
| 533 | |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 534 | case spv::OpTypeVoid: |
| 535 | case spv::OpTypeBool: |
| 536 | case spv::OpTypeInt: |
| 537 | case spv::OpTypeFloat: |
| 538 | case spv::OpTypeVector: |
| 539 | case spv::OpTypeMatrix: |
| 540 | case spv::OpTypeImage: |
| 541 | case spv::OpTypeSampler: |
| 542 | case spv::OpTypeSampledImage: |
| 543 | case spv::OpTypeArray: |
| 544 | case spv::OpTypeRuntimeArray: |
| 545 | case spv::OpTypeStruct: |
| 546 | case spv::OpTypePointer: |
| 547 | case spv::OpTypeFunction: |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 548 | DeclareType(insn); |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 549 | break; |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 550 | |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 551 | case spv::OpVariable: |
| 552 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 553 | Type::ID typeId = insn.word(1); |
| 554 | Object::ID resultId = insn.word(2); |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 555 | auto storageClass = static_cast<spv::StorageClass>(insn.word(3)); |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 556 | |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 557 | auto &object = defs[resultId]; |
Ben Clayton | 1d514f3 | 2019-04-19 16:11:18 -0400 | [diff] [blame] | 558 | object.kind = Object::Kind::Pointer; |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 559 | object.definition = insn; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 560 | object.type = typeId; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 561 | |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 562 | ASSERT(getType(typeId).definition.opcode() == spv::OpTypePointer); |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 563 | ASSERT(getType(typeId).storageClass == storageClass); |
| 564 | |
| 565 | switch (storageClass) |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 566 | { |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 567 | case spv::StorageClassInput: |
| 568 | case spv::StorageClassOutput: |
Ben Clayton | a192473 | 2019-02-28 18:42:10 +0000 | [diff] [blame] | 569 | ProcessInterfaceVariable(object); |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 570 | break; |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 571 | |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 572 | case spv::StorageClassUniform: |
| 573 | case spv::StorageClassStorageBuffer: |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 574 | object.kind = Object::Kind::DescriptorSet; |
| 575 | break; |
| 576 | |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 577 | case spv::StorageClassPushConstant: |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 578 | case spv::StorageClassPrivate: |
| 579 | case spv::StorageClassFunction: |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 580 | case spv::StorageClassUniformConstant: |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 581 | break; // Correctly handled. |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 582 | |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 583 | case spv::StorageClassWorkgroup: |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 584 | { |
| 585 | auto &elTy = getType(getType(typeId).element); |
| 586 | auto sizeInBytes = elTy.sizeInComponents * sizeof(float); |
| 587 | workgroupMemory.allocate(resultId, sizeInBytes); |
| 588 | object.kind = Object::Kind::Pointer; |
| 589 | break; |
| 590 | } |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 591 | case spv::StorageClassAtomicCounter: |
| 592 | case spv::StorageClassImage: |
| 593 | UNIMPLEMENTED("StorageClass %d not yet implemented", (int)storageClass); |
| 594 | break; |
| 595 | |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 596 | case spv::StorageClassCrossWorkgroup: |
| 597 | UNSUPPORTED("SPIR-V OpenCL Execution Model (StorageClassCrossWorkgroup)"); |
| 598 | break; |
| 599 | |
| 600 | case spv::StorageClassGeneric: |
| 601 | UNSUPPORTED("SPIR-V GenericPointer Capability (StorageClassGeneric)"); |
| 602 | break; |
| 603 | |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 604 | default: |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 605 | UNREACHABLE("Unexpected StorageClass %d", storageClass); // See Appendix A of the Vulkan spec. |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 606 | break; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 607 | } |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 608 | break; |
| 609 | } |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 610 | |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 611 | case spv::OpConstant: |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 612 | CreateConstant(insn).constantValue[0] = insn.word(3); |
| 613 | break; |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 614 | case spv::OpConstantFalse: |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 615 | CreateConstant(insn).constantValue[0] = 0; // represent boolean false as zero |
| 616 | break; |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 617 | case spv::OpConstantTrue: |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 618 | CreateConstant(insn).constantValue[0] = ~0u; // represent boolean true as all bits set |
| 619 | break; |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 620 | case spv::OpConstantNull: |
Chris Forbes | 0e71241 | 2019-03-18 19:31:16 -0700 | [diff] [blame] | 621 | case spv::OpUndef: |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 622 | { |
Chris Forbes | 0e71241 | 2019-03-18 19:31:16 -0700 | [diff] [blame] | 623 | // TODO: consider a real LLVM-level undef. For now, zero is a perfectly good value. |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 624 | // OpConstantNull forms a constant of arbitrary type, all zeros. |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 625 | auto &object = CreateConstant(insn); |
| 626 | auto &objectTy = getType(object.type); |
| 627 | for (auto i = 0u; i < objectTy.sizeInComponents; i++) |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 628 | { |
| 629 | object.constantValue[i] = 0; |
| 630 | } |
| 631 | break; |
| 632 | } |
| 633 | case spv::OpConstantComposite: |
| 634 | { |
| 635 | auto &object = CreateConstant(insn); |
| 636 | auto offset = 0u; |
| 637 | for (auto i = 0u; i < insn.wordCount() - 3; i++) |
| 638 | { |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 639 | auto &constituent = getObject(insn.word(i + 3)); |
| 640 | auto &constituentTy = getType(constituent.type); |
| 641 | for (auto j = 0u; j < constituentTy.sizeInComponents; j++) |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 642 | object.constantValue[offset++] = constituent.constantValue[j]; |
| 643 | } |
Ben Clayton | 62758f5 | 2019-03-13 14:18:58 +0000 | [diff] [blame] | 644 | |
| 645 | auto objectId = Object::ID(insn.word(2)); |
| 646 | auto decorationsIt = decorations.find(objectId); |
| 647 | if (decorationsIt != decorations.end() && |
| 648 | decorationsIt->second.BuiltIn == spv::BuiltInWorkgroupSize) |
| 649 | { |
| 650 | // https://www.khronos.org/registry/vulkan/specs/1.1/html/vkspec.html#interfaces-builtin-variables : |
| 651 | // Decorating an object with the WorkgroupSize built-in |
| 652 | // decoration will make that object contain the dimensions |
| 653 | // of a local workgroup. If an object is decorated with the |
| 654 | // WorkgroupSize decoration, this must take precedence over |
| 655 | // any execution mode set for LocalSize. |
| 656 | // The object decorated with WorkgroupSize must be declared |
| 657 | // as a three-component vector of 32-bit integers. |
| 658 | ASSERT(getType(object.type).sizeInComponents == 3); |
| 659 | modes.WorkgroupSizeX = object.constantValue[0]; |
| 660 | modes.WorkgroupSizeY = object.constantValue[1]; |
| 661 | modes.WorkgroupSizeZ = object.constantValue[2]; |
| 662 | } |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 666 | case spv::OpCapability: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 667 | break; // Various capabilities will be declared, but none affect our code generation at this point. |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 668 | case spv::OpMemoryModel: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 669 | break; // Memory model does not affect our code generation until we decide to do Vulkan Memory Model support. |
| 670 | |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 671 | case spv::OpFunction: |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 672 | { |
| 673 | auto functionId = Object::ID(insn.word(2)); |
| 674 | if (functionId == entryPointFunctionId) |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 675 | { |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 676 | // Scan forward to find the function's label. |
| 677 | for (auto it = insn; it != end() && entryPointBlockId == 0; it++) |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 678 | { |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 679 | switch (it.opcode()) |
| 680 | { |
| 681 | case spv::OpFunction: |
| 682 | case spv::OpFunctionParameter: |
| 683 | break; |
| 684 | case spv::OpLabel: |
| 685 | entryPointBlockId = Block::ID(it.word(1)); |
| 686 | break; |
| 687 | default: |
| 688 | WARN("Unexpected opcode '%s' following OpFunction", OpcodeName(it.opcode()).c_str()); |
| 689 | } |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 692 | else |
| 693 | { |
| 694 | // All non-entry point functions should be inlined into an |
| 695 | // entry point function. |
| 696 | // This isn't the target entry point, so must be another |
| 697 | // entry point that we are not interested in. Just skip it. |
| 698 | for (; insn != end() && insn.opcode() != spv::OpFunctionEnd; insn++) {} |
| 699 | } |
| 700 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 701 | break; |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 702 | } |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 703 | case spv::OpFunctionEnd: |
| 704 | // Due to preprocessing, the entrypoint and its function provide no value. |
| 705 | break; |
| 706 | case spv::OpExtInstImport: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 707 | { |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 708 | // We will only support the GLSL 450 extended instruction set, so no point in tracking the ID we assign it. |
| 709 | // Valid shaders will not attempt to import any other instruction sets. |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 710 | auto ext = insn.string(2); |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 711 | if (0 != strcmp("GLSL.std.450", ext)) |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 712 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 713 | UNSUPPORTED("SPIR-V Extension: %s", ext); |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 714 | } |
| 715 | break; |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 716 | } |
Chris Forbes | 1776af7 | 2019-02-22 17:39:57 -0800 | [diff] [blame] | 717 | case spv::OpName: |
| 718 | case spv::OpMemberName: |
| 719 | case spv::OpSource: |
| 720 | case spv::OpSourceContinued: |
| 721 | case spv::OpSourceExtension: |
Chris Forbes | f3a430d | 2019-03-08 07:51:39 -0800 | [diff] [blame] | 722 | case spv::OpLine: |
| 723 | case spv::OpNoLine: |
| 724 | case spv::OpModuleProcessed: |
| 725 | case spv::OpString: |
Chris Forbes | 1776af7 | 2019-02-22 17:39:57 -0800 | [diff] [blame] | 726 | // No semantic impact |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 727 | break; |
| 728 | |
| 729 | case spv::OpFunctionParameter: |
| 730 | case spv::OpFunctionCall: |
| 731 | case spv::OpSpecConstant: |
| 732 | case spv::OpSpecConstantComposite: |
| 733 | case spv::OpSpecConstantFalse: |
| 734 | case spv::OpSpecConstantOp: |
| 735 | case spv::OpSpecConstantTrue: |
| 736 | // These should have all been removed by preprocessing passes. If we see them here, |
| 737 | // our assumptions are wrong and we will probably generate wrong code. |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 738 | UNREACHABLE("%s should have already been lowered.", OpcodeName(opcode).c_str()); |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 739 | break; |
| 740 | |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 741 | case spv::OpFConvert: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 742 | UNSUPPORTED("SPIR-V Float16 or Float64 Capability (OpFConvert)"); |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 743 | break; |
| 744 | |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 745 | case spv::OpSConvert: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 746 | UNSUPPORTED("SPIR-V Int16 or Int64 Capability (OpSConvert)"); |
| 747 | break; |
| 748 | |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 749 | case spv::OpUConvert: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 750 | UNSUPPORTED("SPIR-V Int16 or Int64 Capability (OpUConvert)"); |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 751 | break; |
| 752 | |
Chris Forbes | a71b8e9 | 2019-02-10 22:42:42 +0000 | [diff] [blame] | 753 | case spv::OpLoad: |
| 754 | case spv::OpAccessChain: |
Chris Forbes | 10fd624 | 2019-03-15 12:27:34 -0700 | [diff] [blame] | 755 | case spv::OpInBoundsAccessChain: |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 756 | case spv::OpSampledImage: |
| 757 | case spv::OpImage: |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 758 | { |
| 759 | // Propagate the descriptor decorations to the result. |
| 760 | Object::ID resultId = insn.word(2); |
| 761 | Object::ID pointerId = insn.word(3); |
| 762 | const auto &d = descriptorDecorations.find(pointerId); |
| 763 | |
| 764 | if(d != descriptorDecorations.end()) |
| 765 | { |
| 766 | descriptorDecorations[resultId] = d->second; |
| 767 | } |
| 768 | |
| 769 | DefineResult(insn); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 770 | |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 771 | if (opcode == spv::OpAccessChain || opcode == spv::OpInBoundsAccessChain) |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 772 | { |
| 773 | Decorations dd{}; |
Chris Forbes | 3610ded | 2019-04-22 18:12:13 -0700 | [diff] [blame] | 774 | ApplyDecorationsForAccessChain(&dd, &descriptorDecorations[resultId], pointerId, insn.wordCount() - 4, insn.wordPointer(4)); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 775 | // Note: offset is the one thing that does *not* propagate, as the access chain accounts for it. |
| 776 | dd.HasOffset = false; |
| 777 | decorations[resultId].Apply(dd); |
| 778 | } |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 779 | } |
| 780 | break; |
| 781 | |
Chris Forbes | b97a957 | 2019-02-21 16:51:42 -0800 | [diff] [blame] | 782 | case spv::OpCompositeConstruct: |
Chris Forbes | 1bc1acf | 2019-02-21 18:40:33 -0800 | [diff] [blame] | 783 | case spv::OpCompositeInsert: |
Chris Forbes | b12846d | 2019-02-21 18:53:58 -0800 | [diff] [blame] | 784 | case spv::OpCompositeExtract: |
Chris Forbes | 83fc544 | 2019-02-26 22:16:07 -0800 | [diff] [blame] | 785 | case spv::OpVectorShuffle: |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 786 | case spv::OpVectorTimesScalar: |
Chris Forbes | 57e05b8 | 2019-03-28 09:16:20 +1300 | [diff] [blame] | 787 | case spv::OpMatrixTimesScalar: |
Chris Forbes | 06f4ed7 | 2019-03-28 09:53:20 +1300 | [diff] [blame] | 788 | case spv::OpMatrixTimesVector: |
Chris Forbes | a563dd8 | 2019-03-28 10:32:55 +1300 | [diff] [blame] | 789 | case spv::OpVectorTimesMatrix: |
Chris Forbes | 51562f1 | 2019-03-28 19:08:39 -0700 | [diff] [blame] | 790 | case spv::OpMatrixTimesMatrix: |
Ben Clayton | 3ee5299 | 2019-04-08 11:01:23 -0400 | [diff] [blame] | 791 | case spv::OpOuterProduct: |
Ben Clayton | 620f708 | 2019-04-08 11:12:08 -0400 | [diff] [blame] | 792 | case spv::OpTranspose: |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 793 | case spv::OpVectorExtractDynamic: |
| 794 | case spv::OpVectorInsertDynamic: |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 795 | // Unary ops |
| 796 | case spv::OpNot: |
Ben Clayton | b5bfa50 | 2019-04-08 14:26:36 -0400 | [diff] [blame] | 797 | case spv::OpBitFieldInsert: |
Ben Clayton | d86db95 | 2019-04-08 13:43:11 -0400 | [diff] [blame] | 798 | case spv::OpBitFieldSExtract: |
| 799 | case spv::OpBitFieldUExtract: |
Ben Clayton | d2a4643 | 2019-04-08 11:41:45 -0400 | [diff] [blame] | 800 | case spv::OpBitReverse: |
Ben Clayton | 1eb017d | 2019-04-08 11:32:09 -0400 | [diff] [blame] | 801 | case spv::OpBitCount: |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 802 | case spv::OpSNegate: |
| 803 | case spv::OpFNegate: |
| 804 | case spv::OpLogicalNot: |
Ben Clayton | 64da4ae | 2019-04-19 12:34:06 -0400 | [diff] [blame] | 805 | case spv::OpQuantizeToF16: |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 806 | // Binary ops |
| 807 | case spv::OpIAdd: |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 808 | case spv::OpISub: |
| 809 | case spv::OpIMul: |
| 810 | case spv::OpSDiv: |
| 811 | case spv::OpUDiv: |
| 812 | case spv::OpFAdd: |
| 813 | case spv::OpFSub: |
Chris Forbes | 9d93153 | 2019-03-08 09:53:03 -0800 | [diff] [blame] | 814 | case spv::OpFMul: |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 815 | case spv::OpFDiv: |
Chris Forbes | 0e4d6ff | 2019-03-15 13:43:36 -0700 | [diff] [blame] | 816 | case spv::OpFMod: |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 817 | case spv::OpFRem: |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 818 | case spv::OpFOrdEqual: |
| 819 | case spv::OpFUnordEqual: |
| 820 | case spv::OpFOrdNotEqual: |
| 821 | case spv::OpFUnordNotEqual: |
| 822 | case spv::OpFOrdLessThan: |
| 823 | case spv::OpFUnordLessThan: |
| 824 | case spv::OpFOrdGreaterThan: |
| 825 | case spv::OpFUnordGreaterThan: |
| 826 | case spv::OpFOrdLessThanEqual: |
| 827 | case spv::OpFUnordLessThanEqual: |
| 828 | case spv::OpFOrdGreaterThanEqual: |
| 829 | case spv::OpFUnordGreaterThanEqual: |
Ben Clayton | bb8c8e2 | 2019-03-08 12:04:00 +0000 | [diff] [blame] | 830 | case spv::OpSMod: |
Chris Forbes | 71673c8 | 2019-03-14 12:55:20 -0700 | [diff] [blame] | 831 | case spv::OpSRem: |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 832 | case spv::OpUMod: |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 833 | case spv::OpIEqual: |
| 834 | case spv::OpINotEqual: |
| 835 | case spv::OpUGreaterThan: |
| 836 | case spv::OpSGreaterThan: |
| 837 | case spv::OpUGreaterThanEqual: |
| 838 | case spv::OpSGreaterThanEqual: |
| 839 | case spv::OpULessThan: |
| 840 | case spv::OpSLessThan: |
| 841 | case spv::OpULessThanEqual: |
| 842 | case spv::OpSLessThanEqual: |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 843 | case spv::OpShiftRightLogical: |
| 844 | case spv::OpShiftRightArithmetic: |
| 845 | case spv::OpShiftLeftLogical: |
| 846 | case spv::OpBitwiseOr: |
| 847 | case spv::OpBitwiseXor: |
| 848 | case spv::OpBitwiseAnd: |
| 849 | case spv::OpLogicalOr: |
| 850 | case spv::OpLogicalAnd: |
Chris Forbes | 787b446 | 2019-03-08 12:16:57 -0800 | [diff] [blame] | 851 | case spv::OpLogicalEqual: |
| 852 | case spv::OpLogicalNotEqual: |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 853 | case spv::OpUMulExtended: |
| 854 | case spv::OpSMulExtended: |
Chris Forbes | 3e6f60b | 2019-05-08 17:28:10 -0700 | [diff] [blame] | 855 | case spv::OpIAddCarry: |
| 856 | case spv::OpISubBorrow: |
Chris Forbes | 2b287cc | 2019-03-01 13:24:17 -0800 | [diff] [blame] | 857 | case spv::OpDot: |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 858 | case spv::OpConvertFToU: |
| 859 | case spv::OpConvertFToS: |
| 860 | case spv::OpConvertSToF: |
| 861 | case spv::OpConvertUToF: |
| 862 | case spv::OpBitcast: |
Ben Clayton | bf943f6 | 2019-03-05 12:57:39 +0000 | [diff] [blame] | 863 | case spv::OpSelect: |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 864 | case spv::OpExtInst: |
Chris Forbes | 3ed33ce | 2019-03-07 13:38:31 -0800 | [diff] [blame] | 865 | case spv::OpIsInf: |
| 866 | case spv::OpIsNan: |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 867 | case spv::OpAny: |
| 868 | case spv::OpAll: |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 869 | case spv::OpDPdx: |
| 870 | case spv::OpDPdxCoarse: |
| 871 | case spv::OpDPdy: |
| 872 | case spv::OpDPdyCoarse: |
| 873 | case spv::OpFwidth: |
| 874 | case spv::OpFwidthCoarse: |
| 875 | case spv::OpDPdxFine: |
| 876 | case spv::OpDPdyFine: |
| 877 | case spv::OpFwidthFine: |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 878 | case spv::OpAtomicLoad: |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 879 | case spv::OpAtomicIAdd: |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 880 | case spv::OpAtomicISub: |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 881 | case spv::OpAtomicSMin: |
| 882 | case spv::OpAtomicSMax: |
| 883 | case spv::OpAtomicUMin: |
| 884 | case spv::OpAtomicUMax: |
| 885 | case spv::OpAtomicAnd: |
| 886 | case spv::OpAtomicOr: |
| 887 | case spv::OpAtomicXor: |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 888 | case spv::OpAtomicIIncrement: |
| 889 | case spv::OpAtomicIDecrement: |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 890 | case spv::OpAtomicExchange: |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 891 | case spv::OpAtomicCompareExchange: |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 892 | case spv::OpPhi: |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 893 | case spv::OpImageSampleImplicitLod: |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 894 | case spv::OpImageSampleExplicitLod: |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 895 | case spv::OpImageSampleDrefImplicitLod: |
| 896 | case spv::OpImageSampleDrefExplicitLod: |
| 897 | case spv::OpImageSampleProjImplicitLod: |
| 898 | case spv::OpImageSampleProjExplicitLod: |
| 899 | case spv::OpImageSampleProjDrefImplicitLod: |
| 900 | case spv::OpImageSampleProjDrefExplicitLod: |
Chris Forbes | cd63159 | 2019-04-27 10:37:18 -0700 | [diff] [blame] | 901 | case spv::OpImageFetch: |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 902 | case spv::OpImageQuerySize: |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 903 | case spv::OpImageQuerySizeLod: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 904 | case spv::OpImageRead: |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 905 | case spv::OpImageTexelPointer: |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 906 | case spv::OpGroupNonUniformElect: |
Ben Clayton | 78abf37 | 2019-05-09 15:11:58 +0100 | [diff] [blame] | 907 | case spv::OpCopyObject: |
Ben Clayton | e4605da | 2019-05-09 16:24:01 +0100 | [diff] [blame] | 908 | case spv::OpArrayLength: |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 909 | // Instructions that yield an intermediate value or divergent pointer |
| 910 | DefineResult(insn); |
Chris Forbes | a71b8e9 | 2019-02-10 22:42:42 +0000 | [diff] [blame] | 911 | break; |
Chris Forbes | a71b8e9 | 2019-02-10 22:42:42 +0000 | [diff] [blame] | 912 | |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 913 | case spv::OpStore: |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 914 | case spv::OpAtomicStore: |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 915 | case spv::OpImageWrite: |
Ben Clayton | b5a4546 | 2019-04-30 19:21:29 +0100 | [diff] [blame] | 916 | case spv::OpCopyMemory: |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 917 | case spv::OpMemoryBarrier: |
Chris Forbes | 7edf534 | 2019-02-10 22:41:21 +0000 | [diff] [blame] | 918 | // Don't need to do anything during analysis pass |
| 919 | break; |
| 920 | |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 921 | case spv::OpControlBarrier: |
| 922 | modes.ContainsControlBarriers = true; |
| 923 | break; |
| 924 | |
Chris Forbes | 9869d60 | 2019-04-18 17:26:16 -0700 | [diff] [blame] | 925 | case spv::OpExtension: |
| 926 | { |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 927 | auto ext = insn.string(1); |
Chris Forbes | 9869d60 | 2019-04-18 17:26:16 -0700 | [diff] [blame] | 928 | // Part of core SPIR-V 1.3. Vulkan 1.1 implementations must also accept the pre-1.3 |
| 929 | // extension per Appendix A, `Vulkan Environment for SPIR-V`. |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 930 | if (!strcmp(ext, "SPV_KHR_storage_buffer_storage_class")) break; |
Chris Forbes | cb9bf9a | 2019-05-10 08:13:34 -0700 | [diff] [blame^] | 931 | if (!strcmp(ext, "SPV_KHR_shader_draw_parameters")) break; |
| 932 | if (!strcmp(ext, "SPV_KHR_16bit_storage")) break; |
Chris Forbes | 39b0750 | 2019-05-09 09:47:06 -0700 | [diff] [blame] | 933 | if (!strcmp(ext, "SPV_KHR_variable_pointers")) break; |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 934 | UNSUPPORTED("SPIR-V Extension: %s", ext); |
Chris Forbes | 9869d60 | 2019-04-18 17:26:16 -0700 | [diff] [blame] | 935 | break; |
| 936 | } |
| 937 | |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 938 | default: |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 939 | UNIMPLEMENTED("%s", OpcodeName(opcode).c_str()); |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 940 | } |
| 941 | } |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 942 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 943 | ASSERT_MSG(entryPointFunctionId != 0, "Entry point '%s' not found", createInfo->pName); |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 944 | AssignBlockIns(); |
| 945 | } |
| 946 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 947 | void SpirvShader::TraverseReachableBlocks(Block::ID id, SpirvShader::Block::Set& reachable) |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 948 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 949 | if (reachable.count(id) == 0) |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 950 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 951 | reachable.emplace(id); |
| 952 | for (auto out : getBlock(id).outs) |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 953 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 954 | TraverseReachableBlocks(out, reachable); |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | void SpirvShader::AssignBlockIns() |
| 960 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 961 | Block::Set reachable; |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 962 | TraverseReachableBlocks(entryPointBlockId, reachable); |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 963 | |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 964 | for (auto &it : blocks) |
| 965 | { |
| 966 | auto &blockId = it.first; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 967 | if (reachable.count(blockId) > 0) |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 968 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 969 | for (auto &outId : it.second.outs) |
| 970 | { |
| 971 | auto outIt = blocks.find(outId); |
| 972 | ASSERT_MSG(outIt != blocks.end(), "Block %d has a non-existent out %d", blockId.value(), outId.value()); |
| 973 | auto &out = outIt->second; |
| 974 | out.ins.emplace(blockId); |
| 975 | } |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 980 | void SpirvShader::DeclareType(InsnIterator insn) |
| 981 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 982 | Type::ID resultId = insn.word(1); |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 983 | |
| 984 | auto &type = types[resultId]; |
| 985 | type.definition = insn; |
| 986 | type.sizeInComponents = ComputeTypeSize(insn); |
| 987 | |
| 988 | // A structure is a builtin block if it has a builtin |
| 989 | // member. All members of such a structure are builtins. |
| 990 | switch (insn.opcode()) |
| 991 | { |
| 992 | case spv::OpTypeStruct: |
| 993 | { |
| 994 | auto d = memberDecorations.find(resultId); |
| 995 | if (d != memberDecorations.end()) |
| 996 | { |
| 997 | for (auto &m : d->second) |
| 998 | { |
| 999 | if (m.HasBuiltIn) |
| 1000 | { |
| 1001 | type.isBuiltInBlock = true; |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | break; |
| 1007 | } |
| 1008 | case spv::OpTypePointer: |
| 1009 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1010 | Type::ID elementTypeId = insn.word(3); |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 1011 | type.element = elementTypeId; |
| 1012 | type.isBuiltInBlock = getType(elementTypeId).isBuiltInBlock; |
| 1013 | type.storageClass = static_cast<spv::StorageClass>(insn.word(2)); |
| 1014 | break; |
| 1015 | } |
| 1016 | case spv::OpTypeVector: |
| 1017 | case spv::OpTypeMatrix: |
| 1018 | case spv::OpTypeArray: |
| 1019 | case spv::OpTypeRuntimeArray: |
| 1020 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1021 | Type::ID elementTypeId = insn.word(2); |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 1022 | type.element = elementTypeId; |
| 1023 | break; |
| 1024 | } |
| 1025 | default: |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 1030 | SpirvShader::Object& SpirvShader::CreateConstant(InsnIterator insn) |
| 1031 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1032 | Type::ID typeId = insn.word(1); |
| 1033 | Object::ID resultId = insn.word(2); |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 1034 | auto &object = defs[resultId]; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1035 | auto &objectTy = getType(typeId); |
| 1036 | object.type = typeId; |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 1037 | object.kind = Object::Kind::Constant; |
| 1038 | object.definition = insn; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1039 | object.constantValue = std::unique_ptr<uint32_t[]>(new uint32_t[objectTy.sizeInComponents]); |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 1040 | return object; |
| 1041 | } |
| 1042 | |
Chris Forbes | 049ff38 | 2019-02-02 15:16:43 -0800 | [diff] [blame] | 1043 | void SpirvShader::ProcessInterfaceVariable(Object &object) |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1044 | { |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1045 | auto &objectTy = getType(object.type); |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 1046 | ASSERT(objectTy.storageClass == spv::StorageClassInput || objectTy.storageClass == spv::StorageClassOutput); |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1047 | |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1048 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1049 | auto pointeeTy = getType(objectTy.element); |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1050 | |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1051 | auto &builtinInterface = (objectTy.storageClass == spv::StorageClassInput) ? inputBuiltins : outputBuiltins; |
| 1052 | auto &userDefinedInterface = (objectTy.storageClass == spv::StorageClassInput) ? inputs : outputs; |
| 1053 | |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1054 | ASSERT(object.opcode() == spv::OpVariable); |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1055 | Object::ID resultId = object.definition.word(2); |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1056 | |
| 1057 | if (objectTy.isBuiltInBlock) |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1058 | { |
| 1059 | // walk the builtin block, registering each of its members separately. |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1060 | auto m = memberDecorations.find(objectTy.element); |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 1061 | ASSERT(m != memberDecorations.end()); // otherwise we wouldn't have marked the type chain |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1062 | auto &structType = pointeeTy.definition; |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1063 | auto offset = 0u; |
| 1064 | auto word = 2u; |
| 1065 | for (auto &member : m->second) |
| 1066 | { |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 1067 | auto &memberType = getType(structType.word(word)); |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1068 | |
| 1069 | if (member.HasBuiltIn) |
| 1070 | { |
| 1071 | builtinInterface[member.BuiltIn] = {resultId, offset, memberType.sizeInComponents}; |
| 1072 | } |
| 1073 | |
| 1074 | offset += memberType.sizeInComponents; |
| 1075 | ++word; |
| 1076 | } |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | auto d = decorations.find(resultId); |
| 1081 | if (d != decorations.end() && d->second.HasBuiltIn) |
| 1082 | { |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1083 | builtinInterface[d->second.BuiltIn] = {resultId, 0, pointeeTy.sizeInComponents}; |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1084 | } |
| 1085 | else |
| 1086 | { |
Chris Forbes | 049ff38 | 2019-02-02 15:16:43 -0800 | [diff] [blame] | 1087 | object.kind = Object::Kind::InterfaceVariable; |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1088 | VisitInterface(resultId, |
| 1089 | [&userDefinedInterface](Decorations const &d, AttribType type) { |
| 1090 | // Populate a single scalar slot in the interface from a collection of decorations and the intended component type. |
| 1091 | auto scalarSlot = (d.Location << 2) | d.Component; |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 1092 | ASSERT(scalarSlot >= 0 && |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1093 | scalarSlot < static_cast<int32_t>(userDefinedInterface.size())); |
| 1094 | |
| 1095 | auto &slot = userDefinedInterface[scalarSlot]; |
| 1096 | slot.Type = type; |
| 1097 | slot.Flat = d.Flat; |
| 1098 | slot.NoPerspective = d.NoPerspective; |
| 1099 | slot.Centroid = d.Centroid; |
| 1100 | }); |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1104 | void SpirvShader::ProcessExecutionMode(InsnIterator insn) |
| 1105 | { |
| 1106 | auto mode = static_cast<spv::ExecutionMode>(insn.word(2)); |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 1107 | switch (mode) |
| 1108 | { |
| 1109 | case spv::ExecutionModeEarlyFragmentTests: |
| 1110 | modes.EarlyFragmentTests = true; |
| 1111 | break; |
| 1112 | case spv::ExecutionModeDepthReplacing: |
| 1113 | modes.DepthReplacing = true; |
| 1114 | break; |
| 1115 | case spv::ExecutionModeDepthGreater: |
| 1116 | modes.DepthGreater = true; |
| 1117 | break; |
| 1118 | case spv::ExecutionModeDepthLess: |
| 1119 | modes.DepthLess = true; |
| 1120 | break; |
| 1121 | case spv::ExecutionModeDepthUnchanged: |
| 1122 | modes.DepthUnchanged = true; |
| 1123 | break; |
| 1124 | case spv::ExecutionModeLocalSize: |
Ben Clayton | 62758f5 | 2019-03-13 14:18:58 +0000 | [diff] [blame] | 1125 | modes.WorkgroupSizeX = insn.word(3); |
| 1126 | modes.WorkgroupSizeY = insn.word(4); |
| 1127 | modes.WorkgroupSizeZ = insn.word(5); |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 1128 | break; |
| 1129 | case spv::ExecutionModeOriginUpperLeft: |
| 1130 | // This is always the case for a Vulkan shader. Do nothing. |
| 1131 | break; |
| 1132 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1133 | UNREACHABLE("Execution mode: %d", int(mode)); |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1134 | } |
| 1135 | } |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1136 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1137 | uint32_t SpirvShader::ComputeTypeSize(InsnIterator insn) |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1138 | { |
| 1139 | // Types are always built from the bottom up (with the exception of forward ptrs, which |
| 1140 | // don't appear in Vulkan shaders. Therefore, we can always assume our component parts have |
| 1141 | // already been described (and so their sizes determined) |
| 1142 | switch (insn.opcode()) |
| 1143 | { |
| 1144 | case spv::OpTypeVoid: |
| 1145 | case spv::OpTypeSampler: |
| 1146 | case spv::OpTypeImage: |
| 1147 | case spv::OpTypeSampledImage: |
| 1148 | case spv::OpTypeFunction: |
| 1149 | case spv::OpTypeRuntimeArray: |
| 1150 | // Objects that don't consume any space. |
| 1151 | // Descriptor-backed objects currently only need exist at compile-time. |
| 1152 | // Runtime arrays don't appear in places where their size would be interesting |
| 1153 | return 0; |
| 1154 | |
| 1155 | case spv::OpTypeBool: |
| 1156 | case spv::OpTypeFloat: |
| 1157 | case spv::OpTypeInt: |
| 1158 | // All the fundamental types are 1 component. If we ever add support for 8/16/64-bit components, |
| 1159 | // we might need to change this, but only 32 bit components are required for Vulkan 1.1. |
| 1160 | return 1; |
| 1161 | |
| 1162 | case spv::OpTypeVector: |
| 1163 | case spv::OpTypeMatrix: |
| 1164 | // Vectors and matrices both consume element count * element size. |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 1165 | return getType(insn.word(2)).sizeInComponents * insn.word(3); |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1166 | |
| 1167 | case spv::OpTypeArray: |
Chris Forbes | 5be4d70 | 2018-12-27 16:12:31 -0800 | [diff] [blame] | 1168 | { |
| 1169 | // Element count * element size. Array sizes come from constant ids. |
| 1170 | auto arraySize = GetConstantInt(insn.word(3)); |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 1171 | return getType(insn.word(2)).sizeInComponents * arraySize; |
Chris Forbes | 5be4d70 | 2018-12-27 16:12:31 -0800 | [diff] [blame] | 1172 | } |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1173 | |
| 1174 | case spv::OpTypeStruct: |
| 1175 | { |
| 1176 | uint32_t size = 0; |
| 1177 | for (uint32_t i = 2u; i < insn.wordCount(); i++) |
| 1178 | { |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 1179 | size += getType(insn.word(i)).sizeInComponents; |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1180 | } |
| 1181 | return size; |
| 1182 | } |
| 1183 | |
| 1184 | case spv::OpTypePointer: |
Chris Forbes | 0f59a2c | 2019-02-10 23:03:12 +0000 | [diff] [blame] | 1185 | // Runtime representation of a pointer is a per-lane index. |
| 1186 | // Note: clients are expected to look through the pointer if they want the pointee size instead. |
| 1187 | return 1; |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1188 | |
| 1189 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1190 | UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str()); |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 1191 | return 0; |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 1192 | } |
| 1193 | } |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1194 | |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 1195 | bool SpirvShader::IsStorageInterleavedByLane(spv::StorageClass storageClass) |
| 1196 | { |
| 1197 | switch (storageClass) |
| 1198 | { |
| 1199 | case spv::StorageClassUniform: |
| 1200 | case spv::StorageClassStorageBuffer: |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1201 | case spv::StorageClassPushConstant: |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 1202 | case spv::StorageClassWorkgroup: |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 1203 | return false; |
| 1204 | default: |
| 1205 | return true; |
| 1206 | } |
| 1207 | } |
| 1208 | |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1209 | template<typename F> |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1210 | int SpirvShader::VisitInterfaceInner(Type::ID id, Decorations d, F f) const |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1211 | { |
| 1212 | // Recursively walks variable definition and its type tree, taking into account |
| 1213 | // any explicit Location or Component decorations encountered; where explicit |
| 1214 | // Locations or Components are not specified, assigns them sequentially. |
| 1215 | // Collected decorations are carried down toward the leaves and across |
| 1216 | // siblings; Effect of decorations intentionally does not flow back up the tree. |
| 1217 | // |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1218 | // F is a functor to be called with the effective decoration set for every component. |
| 1219 | // |
| 1220 | // Returns the next available location, and calls f(). |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1221 | |
| 1222 | // This covers the rules in Vulkan 1.1 spec, 14.1.4 Location Assignment. |
| 1223 | |
Chris Forbes | 49d664d | 2019-02-12 19:24:50 +0000 | [diff] [blame] | 1224 | ApplyDecorationsForId(&d, id); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1225 | |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 1226 | auto const &obj = getType(id); |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1227 | switch(obj.opcode()) |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1228 | { |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1229 | case spv::OpTypePointer: |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1230 | return VisitInterfaceInner<F>(obj.definition.word(3), d, f); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1231 | case spv::OpTypeMatrix: |
| 1232 | for (auto i = 0u; i < obj.definition.word(3); i++, d.Location++) |
| 1233 | { |
| 1234 | // consumes same components of N consecutive locations |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1235 | VisitInterfaceInner<F>(obj.definition.word(2), d, f); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1236 | } |
| 1237 | return d.Location; |
| 1238 | case spv::OpTypeVector: |
| 1239 | for (auto i = 0u; i < obj.definition.word(3); i++, d.Component++) |
| 1240 | { |
| 1241 | // consumes N consecutive components in the same location |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1242 | VisitInterfaceInner<F>(obj.definition.word(2), d, f); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1243 | } |
| 1244 | return d.Location + 1; |
| 1245 | case spv::OpTypeFloat: |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1246 | f(d, ATTRIBTYPE_FLOAT); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1247 | return d.Location + 1; |
| 1248 | case spv::OpTypeInt: |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1249 | f(d, obj.definition.word(3) ? ATTRIBTYPE_INT : ATTRIBTYPE_UINT); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1250 | return d.Location + 1; |
| 1251 | case spv::OpTypeBool: |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1252 | f(d, ATTRIBTYPE_UINT); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1253 | return d.Location + 1; |
| 1254 | case spv::OpTypeStruct: |
| 1255 | { |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1256 | // iterate over members, which may themselves have Location/Component decorations |
| 1257 | for (auto i = 0u; i < obj.definition.wordCount() - 2; i++) |
| 1258 | { |
Chris Forbes | 49d664d | 2019-02-12 19:24:50 +0000 | [diff] [blame] | 1259 | ApplyDecorationsForIdMember(&d, id, i); |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1260 | d.Location = VisitInterfaceInner<F>(obj.definition.word(i + 2), d, f); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1261 | d.Component = 0; // Implicit locations always have component=0 |
| 1262 | } |
| 1263 | return d.Location; |
| 1264 | } |
Chris Forbes | 5be4d70 | 2018-12-27 16:12:31 -0800 | [diff] [blame] | 1265 | case spv::OpTypeArray: |
| 1266 | { |
| 1267 | auto arraySize = GetConstantInt(obj.definition.word(3)); |
| 1268 | for (auto i = 0u; i < arraySize; i++) |
| 1269 | { |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1270 | d.Location = VisitInterfaceInner<F>(obj.definition.word(2), d, f); |
Chris Forbes | 5be4d70 | 2018-12-27 16:12:31 -0800 | [diff] [blame] | 1271 | } |
| 1272 | return d.Location; |
| 1273 | } |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1274 | default: |
| 1275 | // Intentionally partial; most opcodes do not participate in type hierarchies |
| 1276 | return 0; |
| 1277 | } |
| 1278 | } |
| 1279 | |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1280 | template<typename F> |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1281 | void SpirvShader::VisitInterface(Object::ID id, F f) const |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1282 | { |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1283 | // Walk a variable definition and call f for each component in it. |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1284 | Decorations d{}; |
Chris Forbes | 49d664d | 2019-02-12 19:24:50 +0000 | [diff] [blame] | 1285 | ApplyDecorationsForId(&d, id); |
Chris Forbes | 1c65823 | 2019-02-01 17:12:25 -0800 | [diff] [blame] | 1286 | |
| 1287 | auto def = getObject(id).definition; |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 1288 | ASSERT(def.opcode() == spv::OpVariable); |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1289 | VisitInterfaceInner<F>(def.word(1), d, f); |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1292 | template<typename F> |
| 1293 | void SpirvShader::VisitMemoryObjectInner(sw::SpirvShader::Type::ID id, sw::SpirvShader::Decorations d, uint32_t& index, uint32_t offset, F f) const |
| 1294 | { |
| 1295 | // Walk a type tree in an explicitly laid out storage class, calling |
| 1296 | // a functor for each scalar element within the object. |
| 1297 | |
| 1298 | // The functor's first parameter is the index of the scalar element; |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1299 | // the second parameter is the offset (in bytes) from the base of the |
| 1300 | // object. |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1301 | |
| 1302 | ApplyDecorationsForId(&d, id); |
| 1303 | auto const &type = getType(id); |
| 1304 | |
| 1305 | if (d.HasOffset) |
| 1306 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1307 | offset += d.Offset; |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1308 | d.HasOffset = false; |
| 1309 | } |
| 1310 | |
| 1311 | switch (type.opcode()) |
| 1312 | { |
| 1313 | case spv::OpTypePointer: |
| 1314 | VisitMemoryObjectInner<F>(type.definition.word(3), d, index, offset, f); |
| 1315 | break; |
| 1316 | case spv::OpTypeInt: |
| 1317 | case spv::OpTypeFloat: |
| 1318 | f(index++, offset); |
| 1319 | break; |
| 1320 | case spv::OpTypeVector: |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1321 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1322 | auto elemStride = (d.InsideMatrix && d.HasRowMajor && d.RowMajor) ? d.MatrixStride : sizeof(float); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1323 | for (auto i = 0u; i < type.definition.word(3); i++) |
| 1324 | { |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1325 | VisitMemoryObjectInner(type.definition.word(2), d, index, offset + elemStride * i, f); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1326 | } |
| 1327 | break; |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1328 | } |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1329 | case spv::OpTypeMatrix: |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1330 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1331 | auto columnStride = (d.HasRowMajor && d.RowMajor) ? sizeof(float) : d.MatrixStride; |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1332 | d.InsideMatrix = true; |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1333 | for (auto i = 0u; i < type.definition.word(3); i++) |
| 1334 | { |
| 1335 | ASSERT(d.HasMatrixStride); |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1336 | VisitMemoryObjectInner(type.definition.word(2), d, index, offset + columnStride * i, f); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1337 | } |
| 1338 | break; |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1339 | } |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1340 | case spv::OpTypeStruct: |
| 1341 | for (auto i = 0u; i < type.definition.wordCount() - 2; i++) |
| 1342 | { |
| 1343 | ApplyDecorationsForIdMember(&d, id, i); |
| 1344 | VisitMemoryObjectInner<F>(type.definition.word(i + 2), d, index, offset, f); |
| 1345 | } |
| 1346 | break; |
| 1347 | case spv::OpTypeArray: |
| 1348 | { |
| 1349 | auto arraySize = GetConstantInt(type.definition.word(3)); |
| 1350 | for (auto i = 0u; i < arraySize; i++) |
| 1351 | { |
| 1352 | ASSERT(d.HasArrayStride); |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1353 | VisitMemoryObjectInner<F>(type.definition.word(2), d, index, offset + i * d.ArrayStride, f); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1354 | } |
| 1355 | break; |
| 1356 | } |
| 1357 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1358 | UNREACHABLE("%s", OpcodeName(type.opcode()).c_str()); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | template<typename F> |
| 1363 | void SpirvShader::VisitMemoryObject(sw::SpirvShader::Object::ID id, F f) const |
| 1364 | { |
| 1365 | auto typeId = getObject(id).type; |
| 1366 | auto const & type = getType(typeId); |
| 1367 | if (!IsStorageInterleavedByLane(type.storageClass)) // TODO: really "is explicit layout" |
| 1368 | { |
| 1369 | Decorations d{}; |
| 1370 | ApplyDecorationsForId(&d, id); |
| 1371 | uint32_t index = 0; |
| 1372 | VisitMemoryObjectInner<F>(typeId, d, index, 0, f); |
| 1373 | } |
| 1374 | else |
| 1375 | { |
| 1376 | // Objects without explicit layout are tightly packed. |
| 1377 | for (auto i = 0u; i < getType(type.element).sizeInComponents; i++) |
| 1378 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1379 | f(i, i * sizeof(float)); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 1384 | SIMD::Pointer SpirvShader::GetPointerToData(Object::ID id, int arrayIndex, SpirvRoutine *routine) const |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1385 | { |
| 1386 | auto &object = getObject(id); |
| 1387 | switch (object.kind) |
| 1388 | { |
Ben Clayton | 1d514f3 | 2019-04-19 16:11:18 -0400 | [diff] [blame] | 1389 | case Object::Kind::Pointer: |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1390 | case Object::Kind::InterfaceVariable: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1391 | return routine->getPointer(id); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1392 | |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1393 | case Object::Kind::DescriptorSet: |
| 1394 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1395 | const auto &d = descriptorDecorations.at(id); |
| 1396 | ASSERT(d.DescriptorSet >= 0 && d.DescriptorSet < vk::MAX_BOUND_DESCRIPTOR_SETS); |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1397 | ASSERT(d.Binding >= 0); |
| 1398 | |
| 1399 | auto set = routine->getPointer(id); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1400 | |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1401 | auto setLayout = routine->pipelineLayout->getDescriptorSetLayout(d.DescriptorSet); |
Ben Clayton | 8c56e8d | 2019-04-25 08:24:01 +0100 | [diff] [blame] | 1402 | ASSERT_MSG(setLayout->hasBinding(d.Binding), "Descriptor set %d does not contain binding %d", int(d.DescriptorSet), int(d.Binding)); |
Alexis Hetu | 5078d48 | 2019-04-10 15:00:25 -0400 | [diff] [blame] | 1403 | int bindingOffset = static_cast<int>(setLayout->getBindingOffset(d.Binding, arrayIndex)); |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1404 | |
Chris Forbes | bfbdd89 | 2019-04-27 12:11:29 -0700 | [diff] [blame] | 1405 | Pointer<Byte> descriptor = set.base + bindingOffset; // BufferDescriptor* |
| 1406 | Pointer<Byte> data = *Pointer<Pointer<Byte>>(descriptor + OFFSET(vk::BufferDescriptor, ptr)); // void* |
| 1407 | Int size = *Pointer<Int>(descriptor + OFFSET(vk::BufferDescriptor, sizeInBytes)); |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1408 | if (setLayout->isBindingDynamic(d.Binding)) |
| 1409 | { |
| 1410 | uint32_t dynamicBindingIndex = |
| 1411 | routine->pipelineLayout->getDynamicOffsetBase(d.DescriptorSet) + |
| 1412 | setLayout->getDynamicDescriptorOffset(d.Binding) + |
| 1413 | arrayIndex; |
Chris Forbes | bfbdd89 | 2019-04-27 12:11:29 -0700 | [diff] [blame] | 1414 | Int offset = routine->descriptorDynamicOffsets[dynamicBindingIndex]; |
| 1415 | Int robustnessSize = *Pointer<Int>(descriptor + OFFSET(vk::BufferDescriptor, robustnessSize)); |
| 1416 | return SIMD::Pointer(data + offset, Min(size, robustnessSize - offset)); |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1417 | } |
Chris Forbes | bfbdd89 | 2019-04-27 12:11:29 -0700 | [diff] [blame] | 1418 | else |
| 1419 | { |
| 1420 | return SIMD::Pointer(data, size); |
| 1421 | } |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1424 | default: |
| 1425 | UNREACHABLE("Invalid pointer kind %d", int(object.kind)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1426 | return SIMD::Pointer(Pointer<Byte>(), 0); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
Chris Forbes | 3610ded | 2019-04-22 18:12:13 -0700 | [diff] [blame] | 1430 | void SpirvShader::ApplyDecorationsForAccessChain(Decorations *d, DescriptorDecorations *dd, Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds) const |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1431 | { |
| 1432 | ApplyDecorationsForId(d, baseId); |
| 1433 | auto &baseObject = getObject(baseId); |
| 1434 | ApplyDecorationsForId(d, baseObject.type); |
| 1435 | auto typeId = getType(baseObject.type).element; |
| 1436 | |
| 1437 | for (auto i = 0u; i < numIndexes; i++) |
| 1438 | { |
| 1439 | ApplyDecorationsForId(d, typeId); |
| 1440 | auto & type = getType(typeId); |
| 1441 | switch (type.opcode()) |
| 1442 | { |
| 1443 | case spv::OpTypeStruct: |
| 1444 | { |
| 1445 | int memberIndex = GetConstantInt(indexIds[i]); |
| 1446 | ApplyDecorationsForIdMember(d, typeId, memberIndex); |
| 1447 | typeId = type.definition.word(2u + memberIndex); |
| 1448 | break; |
| 1449 | } |
| 1450 | case spv::OpTypeArray: |
| 1451 | case spv::OpTypeRuntimeArray: |
Chris Forbes | 3610ded | 2019-04-22 18:12:13 -0700 | [diff] [blame] | 1452 | if (dd->InputAttachmentIndex >= 0) |
| 1453 | { |
| 1454 | dd->InputAttachmentIndex += GetConstantInt(indexIds[i]); |
| 1455 | } |
| 1456 | typeId = type.element; |
| 1457 | break; |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1458 | case spv::OpTypeVector: |
| 1459 | typeId = type.element; |
| 1460 | break; |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1461 | case spv::OpTypeMatrix: |
| 1462 | typeId = type.element; |
| 1463 | d->InsideMatrix = true; |
| 1464 | break; |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1465 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1466 | UNREACHABLE("%s", OpcodeName(type.definition.opcode()).c_str()); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1471 | SIMD::Pointer SpirvShader::WalkExplicitLayoutAccessChain(Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1472 | { |
| 1473 | // Produce a offset into external memory in sizeof(float) units |
| 1474 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1475 | auto &baseObject = getObject(baseId); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1476 | Type::ID typeId = getType(baseObject.type).element; |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1477 | Decorations d = {}; |
Chris Forbes | fe1dd4b | 2019-03-19 09:06:19 -0700 | [diff] [blame] | 1478 | ApplyDecorationsForId(&d, baseObject.type); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1479 | |
Alexis Hetu | 5078d48 | 2019-04-10 15:00:25 -0400 | [diff] [blame] | 1480 | uint32_t arrayIndex = 0; |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 1481 | if (baseObject.kind == Object::Kind::DescriptorSet) |
| 1482 | { |
| 1483 | auto type = getType(typeId).definition.opcode(); |
| 1484 | if (type == spv::OpTypeArray || type == spv::OpTypeRuntimeArray) |
| 1485 | { |
| 1486 | ASSERT(getObject(indexIds[0]).kind == Object::Kind::Constant); |
| 1487 | arrayIndex = GetConstantInt(indexIds[0]); |
| 1488 | |
| 1489 | numIndexes--; |
| 1490 | indexIds++; |
| 1491 | typeId = getType(typeId).element; |
| 1492 | } |
| 1493 | } |
| 1494 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1495 | auto ptr = GetPointerToData(baseId, arrayIndex, routine); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1496 | |
| 1497 | int constantOffset = 0; |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1498 | |
| 1499 | for (auto i = 0u; i < numIndexes; i++) |
| 1500 | { |
| 1501 | auto & type = getType(typeId); |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 1502 | ApplyDecorationsForId(&d, typeId); |
| 1503 | |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1504 | switch (type.definition.opcode()) |
| 1505 | { |
| 1506 | case spv::OpTypeStruct: |
| 1507 | { |
| 1508 | int memberIndex = GetConstantInt(indexIds[i]); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1509 | ApplyDecorationsForIdMember(&d, typeId, memberIndex); |
| 1510 | ASSERT(d.HasOffset); |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1511 | constantOffset += d.Offset; |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1512 | typeId = type.definition.word(2u + memberIndex); |
| 1513 | break; |
| 1514 | } |
| 1515 | case spv::OpTypeArray: |
| 1516 | case spv::OpTypeRuntimeArray: |
| 1517 | { |
| 1518 | // TODO: b/127950082: Check bounds. |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1519 | ASSERT(d.HasArrayStride); |
| 1520 | auto & obj = getObject(indexIds[i]); |
| 1521 | if (obj.kind == Object::Kind::Constant) |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1522 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1523 | constantOffset += d.ArrayStride * GetConstantInt(indexIds[i]); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1524 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1525 | else |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1526 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1527 | ptr += SIMD::Int(d.ArrayStride) * routine->getIntermediate(indexIds[i]).Int(0); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1528 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1529 | typeId = type.element; |
| 1530 | break; |
| 1531 | } |
| 1532 | case spv::OpTypeMatrix: |
| 1533 | { |
| 1534 | // TODO: b/127950082: Check bounds. |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1535 | ASSERT(d.HasMatrixStride); |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1536 | d.InsideMatrix = true; |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1537 | auto columnStride = (d.HasRowMajor && d.RowMajor) ? sizeof(float) : d.MatrixStride; |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1538 | auto & obj = getObject(indexIds[i]); |
| 1539 | if (obj.kind == Object::Kind::Constant) |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1540 | { |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1541 | constantOffset += columnStride * GetConstantInt(indexIds[i]); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1542 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1543 | else |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1544 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1545 | ptr += SIMD::Int(columnStride) * routine->getIntermediate(indexIds[i]).Int(0); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1546 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1547 | typeId = type.element; |
| 1548 | break; |
| 1549 | } |
| 1550 | case spv::OpTypeVector: |
| 1551 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1552 | auto elemStride = (d.InsideMatrix && d.HasRowMajor && d.RowMajor) ? d.MatrixStride : sizeof(float); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1553 | auto & obj = getObject(indexIds[i]); |
| 1554 | if (obj.kind == Object::Kind::Constant) |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1555 | { |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1556 | constantOffset += elemStride * GetConstantInt(indexIds[i]); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1557 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1558 | else |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1559 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1560 | ptr += SIMD::Int(elemStride) * routine->getIntermediate(indexIds[i]).Int(0); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1561 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1562 | typeId = type.element; |
| 1563 | break; |
| 1564 | } |
| 1565 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1566 | UNREACHABLE("%s", OpcodeName(type.definition.opcode()).c_str()); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1570 | ptr += constantOffset; |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 1571 | return ptr; |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1574 | SIMD::Pointer SpirvShader::WalkAccessChain(Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1575 | { |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1576 | // TODO: avoid doing per-lane work in some cases if we can? |
| 1577 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1578 | auto &baseObject = getObject(baseId); |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1579 | Type::ID typeId = getType(baseObject.type).element; |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1580 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1581 | auto ptr = routine->getPointer(baseId); |
| 1582 | |
| 1583 | int constantOffset = 0; |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1584 | |
| 1585 | for (auto i = 0u; i < numIndexes; i++) |
| 1586 | { |
| 1587 | auto & type = getType(typeId); |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1588 | switch(type.opcode()) |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1589 | { |
| 1590 | case spv::OpTypeStruct: |
| 1591 | { |
| 1592 | int memberIndex = GetConstantInt(indexIds[i]); |
| 1593 | int offsetIntoStruct = 0; |
| 1594 | for (auto j = 0; j < memberIndex; j++) { |
Chris Forbes | 58bee56 | 2019-02-19 17:41:41 -0800 | [diff] [blame] | 1595 | auto memberType = type.definition.word(2u + j); |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1596 | offsetIntoStruct += getType(memberType).sizeInComponents * sizeof(float); |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1597 | } |
Chris Forbes | 6397ed0 | 2019-02-15 16:39:17 -0800 | [diff] [blame] | 1598 | constantOffset += offsetIntoStruct; |
Chris Forbes | 58bee56 | 2019-02-19 17:41:41 -0800 | [diff] [blame] | 1599 | typeId = type.definition.word(2u + memberIndex); |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | case spv::OpTypeVector: |
| 1604 | case spv::OpTypeMatrix: |
| 1605 | case spv::OpTypeArray: |
Ben Clayton | fa8603c | 2019-03-08 16:51:42 +0000 | [diff] [blame] | 1606 | case spv::OpTypeRuntimeArray: |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1607 | { |
Ben Clayton | fa8603c | 2019-03-08 16:51:42 +0000 | [diff] [blame] | 1608 | // TODO: b/127950082: Check bounds. |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1609 | if (getType(baseObject.type).storageClass == spv::StorageClassUniformConstant) |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1610 | { |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1611 | // indexing into an array of descriptors. |
| 1612 | auto &obj = getObject(indexIds[i]); |
| 1613 | if (obj.kind != Object::Kind::Constant) |
| 1614 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1615 | UNSUPPORTED("SPIR-V SampledImageArrayDynamicIndexing Capability"); |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | auto d = descriptorDecorations.at(baseId); |
| 1619 | ASSERT(d.DescriptorSet >= 0); |
| 1620 | ASSERT(d.Binding >= 0); |
| 1621 | auto setLayout = routine->pipelineLayout->getDescriptorSetLayout(d.DescriptorSet); |
| 1622 | auto stride = setLayout->getBindingStride(d.Binding); |
| 1623 | ptr.base += stride * GetConstantInt(indexIds[i]); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1624 | } |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1625 | else |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1626 | { |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1627 | auto stride = getType(type.element).sizeInComponents * sizeof(float); |
| 1628 | auto & obj = getObject(indexIds[i]); |
| 1629 | if (obj.kind == Object::Kind::Constant) |
| 1630 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1631 | ptr += stride * GetConstantInt(indexIds[i]); |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1632 | } |
| 1633 | else |
| 1634 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1635 | ptr += SIMD::Int(stride) * routine->getIntermediate(indexIds[i]).Int(0); |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1636 | } |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1637 | } |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 1638 | typeId = type.element; |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | } |
| 1641 | |
| 1642 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1643 | UNREACHABLE("%s", OpcodeName(type.opcode()).c_str()); |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1647 | if (constantOffset != 0) |
| 1648 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 1649 | ptr += constantOffset; |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1650 | } |
| 1651 | return ptr; |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1654 | uint32_t SpirvShader::WalkLiteralAccessChain(Type::ID typeId, uint32_t numIndexes, uint32_t const *indexes) const |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1655 | { |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1656 | uint32_t componentOffset = 0; |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1657 | |
| 1658 | for (auto i = 0u; i < numIndexes; i++) |
| 1659 | { |
| 1660 | auto & type = getType(typeId); |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1661 | switch(type.opcode()) |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1662 | { |
| 1663 | case spv::OpTypeStruct: |
| 1664 | { |
| 1665 | int memberIndex = indexes[i]; |
| 1666 | int offsetIntoStruct = 0; |
| 1667 | for (auto j = 0; j < memberIndex; j++) { |
| 1668 | auto memberType = type.definition.word(2u + j); |
| 1669 | offsetIntoStruct += getType(memberType).sizeInComponents; |
| 1670 | } |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1671 | componentOffset += offsetIntoStruct; |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1672 | typeId = type.definition.word(2u + memberIndex); |
| 1673 | break; |
| 1674 | } |
| 1675 | |
| 1676 | case spv::OpTypeVector: |
| 1677 | case spv::OpTypeMatrix: |
| 1678 | case spv::OpTypeArray: |
| 1679 | { |
| 1680 | auto elementType = type.definition.word(2); |
| 1681 | auto stride = getType(elementType).sizeInComponents; |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1682 | componentOffset += stride * indexes[i]; |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1683 | typeId = elementType; |
| 1684 | break; |
| 1685 | } |
| 1686 | |
| 1687 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 1688 | UNREACHABLE("%s", OpcodeName(type.opcode()).c_str()); |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 1692 | return componentOffset; |
Chris Forbes | 9638b94 | 2019-02-21 18:39:31 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1695 | void SpirvShader::Decorations::Apply(spv::Decoration decoration, uint32_t arg) |
| 1696 | { |
| 1697 | switch (decoration) |
| 1698 | { |
| 1699 | case spv::DecorationLocation: |
| 1700 | HasLocation = true; |
| 1701 | Location = static_cast<int32_t>(arg); |
| 1702 | break; |
| 1703 | case spv::DecorationComponent: |
| 1704 | HasComponent = true; |
| 1705 | Component = arg; |
| 1706 | break; |
| 1707 | case spv::DecorationBuiltIn: |
| 1708 | HasBuiltIn = true; |
| 1709 | BuiltIn = static_cast<spv::BuiltIn>(arg); |
| 1710 | break; |
| 1711 | case spv::DecorationFlat: |
| 1712 | Flat = true; |
| 1713 | break; |
| 1714 | case spv::DecorationNoPerspective: |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1715 | NoPerspective = true; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1716 | break; |
| 1717 | case spv::DecorationCentroid: |
| 1718 | Centroid = true; |
| 1719 | break; |
| 1720 | case spv::DecorationBlock: |
| 1721 | Block = true; |
| 1722 | break; |
| 1723 | case spv::DecorationBufferBlock: |
| 1724 | BufferBlock = true; |
| 1725 | break; |
Chris Forbes | 6532107 | 2019-03-07 16:13:56 -0800 | [diff] [blame] | 1726 | case spv::DecorationOffset: |
| 1727 | HasOffset = true; |
| 1728 | Offset = static_cast<int32_t>(arg); |
| 1729 | break; |
| 1730 | case spv::DecorationArrayStride: |
| 1731 | HasArrayStride = true; |
| 1732 | ArrayStride = static_cast<int32_t>(arg); |
| 1733 | break; |
| 1734 | case spv::DecorationMatrixStride: |
| 1735 | HasMatrixStride = true; |
| 1736 | MatrixStride = static_cast<int32_t>(arg); |
| 1737 | break; |
Ben Clayton | 8448cc5 | 2019-04-09 16:24:31 -0400 | [diff] [blame] | 1738 | case spv::DecorationRelaxedPrecision: |
| 1739 | RelaxedPrecision = true; |
| 1740 | break; |
Chris Forbes | 1ba5ba7 | 2019-04-12 11:37:21 -0700 | [diff] [blame] | 1741 | case spv::DecorationRowMajor: |
| 1742 | HasRowMajor = true; |
| 1743 | RowMajor = true; |
| 1744 | break; |
| 1745 | case spv::DecorationColMajor: |
| 1746 | HasRowMajor = true; |
| 1747 | RowMajor = false; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1748 | default: |
| 1749 | // Intentionally partial, there are many decorations we just don't care about. |
| 1750 | break; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | void SpirvShader::Decorations::Apply(const sw::SpirvShader::Decorations &src) |
| 1755 | { |
| 1756 | // Apply a decoration group to this set of decorations |
| 1757 | if (src.HasBuiltIn) |
| 1758 | { |
| 1759 | HasBuiltIn = true; |
| 1760 | BuiltIn = src.BuiltIn; |
| 1761 | } |
| 1762 | |
| 1763 | if (src.HasLocation) |
| 1764 | { |
| 1765 | HasLocation = true; |
| 1766 | Location = src.Location; |
| 1767 | } |
| 1768 | |
| 1769 | if (src.HasComponent) |
| 1770 | { |
| 1771 | HasComponent = true; |
| 1772 | Component = src.Component; |
| 1773 | } |
| 1774 | |
Chris Forbes | 6532107 | 2019-03-07 16:13:56 -0800 | [diff] [blame] | 1775 | if (src.HasOffset) |
| 1776 | { |
| 1777 | HasOffset = true; |
| 1778 | Offset = src.Offset; |
| 1779 | } |
| 1780 | |
| 1781 | if (src.HasArrayStride) |
| 1782 | { |
| 1783 | HasArrayStride = true; |
| 1784 | ArrayStride = src.ArrayStride; |
| 1785 | } |
| 1786 | |
| 1787 | if (src.HasMatrixStride) |
| 1788 | { |
| 1789 | HasMatrixStride = true; |
| 1790 | MatrixStride = src.MatrixStride; |
| 1791 | } |
| 1792 | |
Chris Forbes | 1ba5ba7 | 2019-04-12 11:37:21 -0700 | [diff] [blame] | 1793 | if (src.HasRowMajor) |
| 1794 | { |
| 1795 | HasRowMajor = true; |
| 1796 | RowMajor = src.RowMajor; |
| 1797 | } |
| 1798 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1799 | Flat |= src.Flat; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 1800 | NoPerspective |= src.NoPerspective; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1801 | Centroid |= src.Centroid; |
| 1802 | Block |= src.Block; |
| 1803 | BufferBlock |= src.BufferBlock; |
Ben Clayton | 8448cc5 | 2019-04-09 16:24:31 -0400 | [diff] [blame] | 1804 | RelaxedPrecision |= src.RelaxedPrecision; |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 1805 | InsideMatrix |= src.InsideMatrix; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1806 | } |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 1807 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1808 | void SpirvShader::DescriptorDecorations::Apply(const sw::SpirvShader::DescriptorDecorations &src) |
| 1809 | { |
| 1810 | if(src.DescriptorSet >= 0) |
| 1811 | { |
| 1812 | DescriptorSet = src.DescriptorSet; |
| 1813 | } |
| 1814 | |
| 1815 | if(src.Binding >= 0) |
| 1816 | { |
| 1817 | Binding = src.Binding; |
| 1818 | } |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 1819 | |
| 1820 | if (src.InputAttachmentIndex >= 0) |
| 1821 | { |
| 1822 | InputAttachmentIndex = src.InputAttachmentIndex; |
| 1823 | } |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1824 | } |
| 1825 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1826 | void SpirvShader::ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const |
Chris Forbes | 49d664d | 2019-02-12 19:24:50 +0000 | [diff] [blame] | 1827 | { |
| 1828 | auto it = decorations.find(id); |
| 1829 | if (it != decorations.end()) |
| 1830 | d->Apply(it->second); |
| 1831 | } |
| 1832 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1833 | void SpirvShader::ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const |
Chris Forbes | 49d664d | 2019-02-12 19:24:50 +0000 | [diff] [blame] | 1834 | { |
| 1835 | auto it = memberDecorations.find(id); |
| 1836 | if (it != memberDecorations.end() && member < it->second.size()) |
| 1837 | { |
| 1838 | d->Apply(it->second[member]); |
| 1839 | } |
| 1840 | } |
| 1841 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1842 | void SpirvShader::DefineResult(const InsnIterator &insn) |
| 1843 | { |
| 1844 | Type::ID typeId = insn.word(1); |
| 1845 | Object::ID resultId = insn.word(2); |
| 1846 | auto &object = defs[resultId]; |
| 1847 | object.type = typeId; |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1848 | |
| 1849 | switch (getType(typeId).opcode()) |
| 1850 | { |
| 1851 | case spv::OpTypePointer: |
| 1852 | case spv::OpTypeImage: |
| 1853 | case spv::OpTypeSampledImage: |
| 1854 | case spv::OpTypeSampler: |
Ben Clayton | 1d514f3 | 2019-04-19 16:11:18 -0400 | [diff] [blame] | 1855 | object.kind = Object::Kind::Pointer; |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 1856 | break; |
| 1857 | |
| 1858 | default: |
| 1859 | object.kind = Object::Kind::Intermediate; |
| 1860 | } |
| 1861 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 1862 | object.definition = insn; |
| 1863 | } |
| 1864 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1865 | uint32_t SpirvShader::GetConstantInt(Object::ID id) const |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 1866 | { |
| 1867 | // Slightly hackish access to constants very early in translation. |
| 1868 | // General consumption of constants by other instructions should |
| 1869 | // probably be just lowered to Reactor. |
| 1870 | |
| 1871 | // TODO: not encountered yet since we only use this for array sizes etc, |
| 1872 | // but is possible to construct integer constant 0 via OpConstantNull. |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 1873 | auto insn = getObject(id).definition; |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 1874 | ASSERT(insn.opcode() == spv::OpConstant); |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1875 | ASSERT(getType(insn.word(1)).opcode() == spv::OpTypeInt); |
Chris Forbes | bc3a0ee | 2018-12-27 16:02:58 -0800 | [diff] [blame] | 1876 | return insn.word(3); |
| 1877 | } |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1878 | |
| 1879 | // emit-time |
| 1880 | |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 1881 | void SpirvShader::emitProlog(SpirvRoutine *routine) const |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1882 | { |
| 1883 | for (auto insn : *this) |
| 1884 | { |
| 1885 | switch (insn.opcode()) |
| 1886 | { |
| 1887 | case spv::OpVariable: |
| 1888 | { |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1889 | Type::ID resultPointerTypeId = insn.word(1); |
| 1890 | auto resultPointerType = getType(resultPointerTypeId); |
| 1891 | auto pointeeType = getType(resultPointerType.element); |
| 1892 | |
| 1893 | if(pointeeType.sizeInComponents > 0) // TODO: what to do about zero-slot objects? |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1894 | { |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 1895 | Object::ID resultId = insn.word(2); |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1896 | routine->createVariable(resultId, pointeeType.sizeInComponents); |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1897 | } |
| 1898 | break; |
| 1899 | } |
| 1900 | default: |
Chris Forbes | e9f8f5b | 2019-02-11 00:20:16 +0000 | [diff] [blame] | 1901 | // Nothing else produces interface variables, so can all be safely ignored. |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1902 | break; |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 1907 | void SpirvShader::emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask, const vk::DescriptorSet::Bindings &descriptorSets) const |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1908 | { |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 1909 | EmitState state(routine, activeLaneMask, descriptorSets); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1910 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1911 | // Emit everything up to the first label |
| 1912 | // TODO: Separate out dispatch of block from non-block instructions? |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1913 | for (auto insn : *this) |
| 1914 | { |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1915 | if (insn.opcode() == spv::OpLabel) |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1916 | { |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 1917 | break; |
| 1918 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1919 | EmitInstruction(insn, &state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 1922 | // Emit all the blocks starting from entryPointBlockId. |
| 1923 | EmitBlocks(entryPointBlockId, &state); |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | void SpirvShader::EmitBlocks(Block::ID id, EmitState *state, Block::ID ignore /* = 0 */) const |
| 1927 | { |
| 1928 | auto oldPending = state->pending; |
| 1929 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1930 | std::queue<Block::ID> pending; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 1931 | state->pending = &pending; |
| 1932 | pending.push(id); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1933 | while (pending.size() > 0) |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1934 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1935 | auto id = pending.front(); |
| 1936 | pending.pop(); |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 1937 | |
| 1938 | auto const &block = getBlock(id); |
| 1939 | if (id == ignore) |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1940 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 1941 | continue; |
| 1942 | } |
| 1943 | |
| 1944 | state->currentBlock = id; |
| 1945 | |
| 1946 | switch (block.kind) |
| 1947 | { |
| 1948 | case Block::Simple: |
| 1949 | case Block::StructuredBranchConditional: |
| 1950 | case Block::UnstructuredBranchConditional: |
| 1951 | case Block::StructuredSwitch: |
| 1952 | case Block::UnstructuredSwitch: |
| 1953 | EmitNonLoop(state); |
| 1954 | break; |
| 1955 | |
| 1956 | case Block::Loop: |
| 1957 | EmitLoop(state); |
| 1958 | break; |
| 1959 | |
| 1960 | default: |
| 1961 | UNREACHABLE("Unexpected Block Kind: %d", int(block.kind)); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1962 | } |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1963 | } |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 1964 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 1965 | state->pending = oldPending; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | void SpirvShader::EmitInstructions(InsnIterator begin, InsnIterator end, EmitState *state) const |
| 1969 | { |
| 1970 | for (auto insn = begin; insn != end; insn++) |
| 1971 | { |
| 1972 | auto res = EmitInstruction(insn, state); |
| 1973 | switch (res) |
| 1974 | { |
| 1975 | case EmitResult::Continue: |
| 1976 | continue; |
| 1977 | case EmitResult::Terminator: |
| 1978 | break; |
| 1979 | default: |
| 1980 | UNREACHABLE("Unexpected EmitResult %d", int(res)); |
| 1981 | break; |
| 1982 | } |
| 1983 | } |
| 1984 | } |
| 1985 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 1986 | void SpirvShader::EmitNonLoop(EmitState *state) const |
| 1987 | { |
| 1988 | auto blockId = state->currentBlock; |
| 1989 | auto block = getBlock(blockId); |
| 1990 | |
| 1991 | // Ensure all incoming blocks have been generated. |
| 1992 | auto depsDone = true; |
| 1993 | for (auto in : block.ins) |
| 1994 | { |
| 1995 | if (state->visited.count(in) == 0) |
| 1996 | { |
| 1997 | state->pending->emplace(in); |
| 1998 | depsDone = false; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | if (!depsDone) |
| 2003 | { |
| 2004 | // come back to this once the dependencies have been generated |
| 2005 | state->pending->emplace(blockId); |
| 2006 | return; |
| 2007 | } |
| 2008 | |
| 2009 | if (!state->visited.emplace(blockId).second) |
| 2010 | { |
| 2011 | return; // Already generated this block. |
| 2012 | } |
| 2013 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 2014 | if (blockId != entryPointBlockId) |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2015 | { |
| 2016 | // Set the activeLaneMask. |
Nicolas Capens | 459453a | 2019-03-27 15:27:27 -0400 | [diff] [blame] | 2017 | SIMD::Int activeLaneMask(0); |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2018 | for (auto in : block.ins) |
| 2019 | { |
| 2020 | auto inMask = GetActiveLaneMaskEdge(state, in, blockId); |
Nicolas Capens | 459453a | 2019-03-27 15:27:27 -0400 | [diff] [blame] | 2021 | activeLaneMask |= inMask; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2022 | } |
Nicolas Capens | 459453a | 2019-03-27 15:27:27 -0400 | [diff] [blame] | 2023 | state->setActiveLaneMask(activeLaneMask); |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | EmitInstructions(block.begin(), block.end(), state); |
| 2027 | |
| 2028 | for (auto out : block.outs) |
| 2029 | { |
| 2030 | state->pending->emplace(out); |
| 2031 | } |
| 2032 | } |
| 2033 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2034 | void SpirvShader::EmitLoop(EmitState *state) const |
| 2035 | { |
| 2036 | auto blockId = state->currentBlock; |
| 2037 | auto block = getBlock(blockId); |
| 2038 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2039 | // Ensure all incoming non-back edge blocks have been generated. |
| 2040 | auto depsDone = true; |
| 2041 | for (auto in : block.ins) |
| 2042 | { |
| 2043 | if (state->visited.count(in) == 0) |
| 2044 | { |
| 2045 | if (!existsPath(blockId, in, block.mergeBlock)) // if not a loop back edge |
| 2046 | { |
| 2047 | state->pending->emplace(in); |
| 2048 | depsDone = false; |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | if (!depsDone) |
| 2054 | { |
| 2055 | // come back to this once the dependencies have been generated |
| 2056 | state->pending->emplace(blockId); |
| 2057 | return; |
| 2058 | } |
| 2059 | |
| 2060 | if (!state->visited.emplace(blockId).second) |
| 2061 | { |
| 2062 | return; // Already emitted this loop. |
| 2063 | } |
| 2064 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2065 | // loopActiveLaneMask is the mask of lanes that are continuing to loop. |
| 2066 | // This is initialized with the incoming active lane masks. |
| 2067 | SIMD::Int loopActiveLaneMask = SIMD::Int(0); |
| 2068 | for (auto in : block.ins) |
| 2069 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2070 | if (!existsPath(blockId, in, block.mergeBlock)) // if not a loop back edge |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2071 | { |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 2072 | loopActiveLaneMask |= GetActiveLaneMaskEdge(state, in, blockId); |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | // Generate an alloca for each of the loop's phis. |
| 2077 | // These will be primed with the incoming, non back edge Phi values |
| 2078 | // before the loop, and then updated just before the loop jumps back to |
| 2079 | // the block. |
| 2080 | struct LoopPhi |
| 2081 | { |
Nicolas Capens | 5da8d8d | 2019-03-27 14:45:34 -0400 | [diff] [blame] | 2082 | LoopPhi(Object::ID id, uint32_t size) : phiId(id), storage(size) {} |
| 2083 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2084 | Object::ID phiId; // The Phi identifier. |
| 2085 | Object::ID continueValue; // The source merge value from the loop. |
| 2086 | Array<SIMD::Int> storage; // The alloca. |
| 2087 | }; |
| 2088 | |
| 2089 | std::vector<LoopPhi> phis; |
| 2090 | |
| 2091 | // For each OpPhi between the block start and the merge instruction: |
| 2092 | for (auto insn = block.begin(); insn != block.mergeInstruction; insn++) |
| 2093 | { |
| 2094 | if (insn.opcode() == spv::OpPhi) |
| 2095 | { |
| 2096 | auto objectId = Object::ID(insn.word(2)); |
| 2097 | auto &object = getObject(objectId); |
| 2098 | auto &type = getType(object.type); |
| 2099 | |
Nicolas Capens | 5da8d8d | 2019-03-27 14:45:34 -0400 | [diff] [blame] | 2100 | LoopPhi phi(insn.word(2), type.sizeInComponents); |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2101 | |
| 2102 | // Start with the Phi set to 0. |
| 2103 | for (uint32_t i = 0; i < type.sizeInComponents; i++) |
| 2104 | { |
| 2105 | phi.storage[i] = SIMD::Int(0); |
| 2106 | } |
| 2107 | |
| 2108 | // For each Phi source: |
| 2109 | for (uint32_t w = 3; w < insn.wordCount(); w += 2) |
| 2110 | { |
| 2111 | auto varId = Object::ID(insn.word(w + 0)); |
| 2112 | auto blockId = Block::ID(insn.word(w + 1)); |
Ben Clayton | 5d143aa | 2019-04-03 13:30:14 +0100 | [diff] [blame] | 2113 | |
| 2114 | if (block.ins.count(blockId) == 0) |
| 2115 | { |
| 2116 | continue; // In is unreachable. Ignore. |
| 2117 | } |
| 2118 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2119 | if (existsPath(state->currentBlock, blockId, block.mergeBlock)) |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2120 | { |
| 2121 | // This source is from a loop back-edge. |
| 2122 | ASSERT(phi.continueValue == 0 || phi.continueValue == varId); |
| 2123 | phi.continueValue = varId; |
| 2124 | } |
| 2125 | else |
| 2126 | { |
| 2127 | // This source is from a preceding block. |
| 2128 | for (uint32_t i = 0; i < type.sizeInComponents; i++) |
| 2129 | { |
| 2130 | auto in = GenericValue(this, state->routine, varId); |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 2131 | auto mask = GetActiveLaneMaskEdge(state, blockId, state->currentBlock); |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2132 | phi.storage[i] = phi.storage[i] | (in.Int(i) & mask); |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | phis.push_back(phi); |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | // Create the loop basic blocks |
| 2142 | auto headerBasicBlock = Nucleus::createBasicBlock(); |
| 2143 | auto mergeBasicBlock = Nucleus::createBasicBlock(); |
| 2144 | |
| 2145 | // Start emitting code inside the loop. |
| 2146 | Nucleus::createBr(headerBasicBlock); |
| 2147 | Nucleus::setInsertBlock(headerBasicBlock); |
| 2148 | |
| 2149 | // Load the Phi values from storage. |
| 2150 | // This will load at the start of each loop. |
| 2151 | for (auto &phi : phis) |
| 2152 | { |
| 2153 | auto &type = getType(getObject(phi.phiId).type); |
| 2154 | auto &dst = state->routine->createIntermediate(phi.phiId, type.sizeInComponents); |
| 2155 | for (unsigned int i = 0u; i < type.sizeInComponents; i++) |
| 2156 | { |
| 2157 | dst.move(i, phi.storage[i]); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | // Load the active lane mask. |
| 2162 | state->setActiveLaneMask(loopActiveLaneMask); |
| 2163 | |
| 2164 | // Emit all the non-phi instructions in this loop header block. |
| 2165 | for (auto insn = block.begin(); insn != block.end(); insn++) |
| 2166 | { |
| 2167 | if (insn.opcode() != spv::OpPhi) |
| 2168 | { |
| 2169 | EmitInstruction(insn, state); |
| 2170 | } |
| 2171 | } |
| 2172 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2173 | // Emit all loop blocks, but don't emit the merge block yet. |
| 2174 | for (auto out : block.outs) |
| 2175 | { |
| 2176 | if (existsPath(out, blockId, block.mergeBlock)) |
| 2177 | { |
| 2178 | EmitBlocks(out, state, block.mergeBlock); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | // Rebuild the loopActiveLaneMask from the loop back edges. |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2183 | loopActiveLaneMask = SIMD::Int(0); |
| 2184 | for (auto in : block.ins) |
| 2185 | { |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2186 | if (existsPath(blockId, in, block.mergeBlock)) |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2187 | { |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 2188 | loopActiveLaneMask |= GetActiveLaneMaskEdge(state, in, blockId); |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | // Update loop phi values |
| 2193 | for (auto &phi : phis) |
| 2194 | { |
| 2195 | if (phi.continueValue != 0) |
| 2196 | { |
| 2197 | auto val = GenericValue(this, state->routine, phi.continueValue); |
| 2198 | auto &type = getType(getObject(phi.phiId).type); |
| 2199 | for (unsigned int i = 0u; i < type.sizeInComponents; i++) |
| 2200 | { |
| 2201 | phi.storage[i] = val.Int(i); |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | // Loop body now done. |
| 2207 | // If any lanes are still active, jump back to the loop header, |
| 2208 | // otherwise jump to the merge block. |
| 2209 | Nucleus::createCondBr(AnyTrue(loopActiveLaneMask).value, headerBasicBlock, mergeBasicBlock); |
| 2210 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2211 | // Continue emitting from the merge block. |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2212 | Nucleus::setInsertBlock(mergeBasicBlock); |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 2213 | state->pending->emplace(block.mergeBlock); |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2216 | SpirvShader::EmitResult SpirvShader::EmitInstruction(InsnIterator insn, EmitState *state) const |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2217 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 2218 | auto opcode = insn.opcode(); |
| 2219 | |
| 2220 | switch (opcode) |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2221 | { |
| 2222 | case spv::OpTypeVoid: |
| 2223 | case spv::OpTypeInt: |
| 2224 | case spv::OpTypeFloat: |
| 2225 | case spv::OpTypeBool: |
| 2226 | case spv::OpTypeVector: |
| 2227 | case spv::OpTypeArray: |
| 2228 | case spv::OpTypeRuntimeArray: |
| 2229 | case spv::OpTypeMatrix: |
| 2230 | case spv::OpTypeStruct: |
| 2231 | case spv::OpTypePointer: |
| 2232 | case spv::OpTypeFunction: |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2233 | case spv::OpTypeImage: |
| 2234 | case spv::OpTypeSampledImage: |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 2235 | case spv::OpTypeSampler: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2236 | case spv::OpExecutionMode: |
| 2237 | case spv::OpMemoryModel: |
| 2238 | case spv::OpFunction: |
| 2239 | case spv::OpFunctionEnd: |
| 2240 | case spv::OpConstant: |
| 2241 | case spv::OpConstantNull: |
| 2242 | case spv::OpConstantTrue: |
| 2243 | case spv::OpConstantFalse: |
| 2244 | case spv::OpConstantComposite: |
Chris Forbes | 0e71241 | 2019-03-18 19:31:16 -0700 | [diff] [blame] | 2245 | case spv::OpUndef: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2246 | case spv::OpExtension: |
| 2247 | case spv::OpCapability: |
| 2248 | case spv::OpEntryPoint: |
| 2249 | case spv::OpExtInstImport: |
| 2250 | case spv::OpDecorate: |
| 2251 | case spv::OpMemberDecorate: |
| 2252 | case spv::OpGroupDecorate: |
| 2253 | case spv::OpGroupMemberDecorate: |
| 2254 | case spv::OpDecorationGroup: |
| 2255 | case spv::OpName: |
| 2256 | case spv::OpMemberName: |
| 2257 | case spv::OpSource: |
| 2258 | case spv::OpSourceContinued: |
| 2259 | case spv::OpSourceExtension: |
| 2260 | case spv::OpLine: |
| 2261 | case spv::OpNoLine: |
| 2262 | case spv::OpModuleProcessed: |
| 2263 | case spv::OpString: |
| 2264 | // Nothing to do at emit time. These are either fully handled at analysis time, |
| 2265 | // or don't require any work at all. |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2266 | return EmitResult::Continue; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2267 | |
| 2268 | case spv::OpLabel: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2269 | return EmitResult::Continue; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2270 | |
| 2271 | case spv::OpVariable: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2272 | return EmitVariable(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2273 | |
| 2274 | case spv::OpLoad: |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 2275 | case spv::OpAtomicLoad: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2276 | return EmitLoad(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2277 | |
| 2278 | case spv::OpStore: |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 2279 | case spv::OpAtomicStore: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2280 | return EmitStore(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2281 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 2282 | case spv::OpAtomicIAdd: |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 2283 | case spv::OpAtomicISub: |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 2284 | case spv::OpAtomicSMin: |
| 2285 | case spv::OpAtomicSMax: |
| 2286 | case spv::OpAtomicUMin: |
| 2287 | case spv::OpAtomicUMax: |
| 2288 | case spv::OpAtomicAnd: |
| 2289 | case spv::OpAtomicOr: |
| 2290 | case spv::OpAtomicXor: |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 2291 | case spv::OpAtomicIIncrement: |
| 2292 | case spv::OpAtomicIDecrement: |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 2293 | case spv::OpAtomicExchange: |
| 2294 | return EmitAtomicOp(insn, state); |
| 2295 | |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 2296 | case spv::OpAtomicCompareExchange: |
| 2297 | return EmitAtomicCompareExchange(insn, state); |
| 2298 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2299 | case spv::OpAccessChain: |
Chris Forbes | 10fd624 | 2019-03-15 12:27:34 -0700 | [diff] [blame] | 2300 | case spv::OpInBoundsAccessChain: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2301 | return EmitAccessChain(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2302 | |
| 2303 | case spv::OpCompositeConstruct: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2304 | return EmitCompositeConstruct(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2305 | |
| 2306 | case spv::OpCompositeInsert: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2307 | return EmitCompositeInsert(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2308 | |
| 2309 | case spv::OpCompositeExtract: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2310 | return EmitCompositeExtract(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2311 | |
| 2312 | case spv::OpVectorShuffle: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2313 | return EmitVectorShuffle(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2314 | |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2315 | case spv::OpVectorExtractDynamic: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2316 | return EmitVectorExtractDynamic(insn, state); |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2317 | |
| 2318 | case spv::OpVectorInsertDynamic: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2319 | return EmitVectorInsertDynamic(insn, state); |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2320 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2321 | case spv::OpVectorTimesScalar: |
Chris Forbes | 57e05b8 | 2019-03-28 09:16:20 +1300 | [diff] [blame] | 2322 | case spv::OpMatrixTimesScalar: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2323 | return EmitVectorTimesScalar(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2324 | |
Chris Forbes | 06f4ed7 | 2019-03-28 09:53:20 +1300 | [diff] [blame] | 2325 | case spv::OpMatrixTimesVector: |
| 2326 | return EmitMatrixTimesVector(insn, state); |
| 2327 | |
Chris Forbes | a563dd8 | 2019-03-28 10:32:55 +1300 | [diff] [blame] | 2328 | case spv::OpVectorTimesMatrix: |
| 2329 | return EmitVectorTimesMatrix(insn, state); |
| 2330 | |
Chris Forbes | 51562f1 | 2019-03-28 19:08:39 -0700 | [diff] [blame] | 2331 | case spv::OpMatrixTimesMatrix: |
| 2332 | return EmitMatrixTimesMatrix(insn, state); |
| 2333 | |
Ben Clayton | 3ee5299 | 2019-04-08 11:01:23 -0400 | [diff] [blame] | 2334 | case spv::OpOuterProduct: |
| 2335 | return EmitOuterProduct(insn, state); |
| 2336 | |
Ben Clayton | 620f708 | 2019-04-08 11:12:08 -0400 | [diff] [blame] | 2337 | case spv::OpTranspose: |
| 2338 | return EmitTranspose(insn, state); |
| 2339 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2340 | case spv::OpNot: |
Ben Clayton | b5bfa50 | 2019-04-08 14:26:36 -0400 | [diff] [blame] | 2341 | case spv::OpBitFieldInsert: |
Ben Clayton | d86db95 | 2019-04-08 13:43:11 -0400 | [diff] [blame] | 2342 | case spv::OpBitFieldSExtract: |
| 2343 | case spv::OpBitFieldUExtract: |
Ben Clayton | d2a4643 | 2019-04-08 11:41:45 -0400 | [diff] [blame] | 2344 | case spv::OpBitReverse: |
Ben Clayton | 1eb017d | 2019-04-08 11:32:09 -0400 | [diff] [blame] | 2345 | case spv::OpBitCount: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2346 | case spv::OpSNegate: |
| 2347 | case spv::OpFNegate: |
| 2348 | case spv::OpLogicalNot: |
| 2349 | case spv::OpConvertFToU: |
| 2350 | case spv::OpConvertFToS: |
| 2351 | case spv::OpConvertSToF: |
| 2352 | case spv::OpConvertUToF: |
| 2353 | case spv::OpBitcast: |
| 2354 | case spv::OpIsInf: |
| 2355 | case spv::OpIsNan: |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 2356 | case spv::OpDPdx: |
| 2357 | case spv::OpDPdxCoarse: |
| 2358 | case spv::OpDPdy: |
| 2359 | case spv::OpDPdyCoarse: |
| 2360 | case spv::OpFwidth: |
| 2361 | case spv::OpFwidthCoarse: |
| 2362 | case spv::OpDPdxFine: |
| 2363 | case spv::OpDPdyFine: |
| 2364 | case spv::OpFwidthFine: |
Ben Clayton | 64da4ae | 2019-04-19 12:34:06 -0400 | [diff] [blame] | 2365 | case spv::OpQuantizeToF16: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2366 | return EmitUnaryOp(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2367 | |
| 2368 | case spv::OpIAdd: |
| 2369 | case spv::OpISub: |
| 2370 | case spv::OpIMul: |
| 2371 | case spv::OpSDiv: |
| 2372 | case spv::OpUDiv: |
| 2373 | case spv::OpFAdd: |
| 2374 | case spv::OpFSub: |
| 2375 | case spv::OpFMul: |
| 2376 | case spv::OpFDiv: |
Chris Forbes | 0e4d6ff | 2019-03-15 13:43:36 -0700 | [diff] [blame] | 2377 | case spv::OpFMod: |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 2378 | case spv::OpFRem: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2379 | case spv::OpFOrdEqual: |
| 2380 | case spv::OpFUnordEqual: |
| 2381 | case spv::OpFOrdNotEqual: |
| 2382 | case spv::OpFUnordNotEqual: |
| 2383 | case spv::OpFOrdLessThan: |
| 2384 | case spv::OpFUnordLessThan: |
| 2385 | case spv::OpFOrdGreaterThan: |
| 2386 | case spv::OpFUnordGreaterThan: |
| 2387 | case spv::OpFOrdLessThanEqual: |
| 2388 | case spv::OpFUnordLessThanEqual: |
| 2389 | case spv::OpFOrdGreaterThanEqual: |
| 2390 | case spv::OpFUnordGreaterThanEqual: |
| 2391 | case spv::OpSMod: |
Chris Forbes | 71673c8 | 2019-03-14 12:55:20 -0700 | [diff] [blame] | 2392 | case spv::OpSRem: |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2393 | case spv::OpUMod: |
| 2394 | case spv::OpIEqual: |
| 2395 | case spv::OpINotEqual: |
| 2396 | case spv::OpUGreaterThan: |
| 2397 | case spv::OpSGreaterThan: |
| 2398 | case spv::OpUGreaterThanEqual: |
| 2399 | case spv::OpSGreaterThanEqual: |
| 2400 | case spv::OpULessThan: |
| 2401 | case spv::OpSLessThan: |
| 2402 | case spv::OpULessThanEqual: |
| 2403 | case spv::OpSLessThanEqual: |
| 2404 | case spv::OpShiftRightLogical: |
| 2405 | case spv::OpShiftRightArithmetic: |
| 2406 | case spv::OpShiftLeftLogical: |
| 2407 | case spv::OpBitwiseOr: |
| 2408 | case spv::OpBitwiseXor: |
| 2409 | case spv::OpBitwiseAnd: |
| 2410 | case spv::OpLogicalOr: |
| 2411 | case spv::OpLogicalAnd: |
| 2412 | case spv::OpLogicalEqual: |
| 2413 | case spv::OpLogicalNotEqual: |
| 2414 | case spv::OpUMulExtended: |
| 2415 | case spv::OpSMulExtended: |
Chris Forbes | 3e6f60b | 2019-05-08 17:28:10 -0700 | [diff] [blame] | 2416 | case spv::OpIAddCarry: |
| 2417 | case spv::OpISubBorrow: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2418 | return EmitBinaryOp(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2419 | |
| 2420 | case spv::OpDot: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2421 | return EmitDot(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2422 | |
| 2423 | case spv::OpSelect: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2424 | return EmitSelect(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2425 | |
| 2426 | case spv::OpExtInst: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2427 | return EmitExtendedInstruction(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2428 | |
| 2429 | case spv::OpAny: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2430 | return EmitAny(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2431 | |
| 2432 | case spv::OpAll: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2433 | return EmitAll(insn, state); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2434 | |
Ben Clayton | e37ce61 | 2019-03-13 19:57:42 +0000 | [diff] [blame] | 2435 | case spv::OpBranch: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2436 | return EmitBranch(insn, state); |
Ben Clayton | e37ce61 | 2019-03-13 19:57:42 +0000 | [diff] [blame] | 2437 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 2438 | case spv::OpPhi: |
| 2439 | return EmitPhi(insn, state); |
| 2440 | |
| 2441 | case spv::OpSelectionMerge: |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 2442 | case spv::OpLoopMerge: |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 2443 | return EmitResult::Continue; |
| 2444 | |
| 2445 | case spv::OpBranchConditional: |
| 2446 | return EmitBranchConditional(insn, state); |
| 2447 | |
Ben Clayton | 213a8ce | 2019-03-21 18:57:23 +0000 | [diff] [blame] | 2448 | case spv::OpSwitch: |
| 2449 | return EmitSwitch(insn, state); |
| 2450 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 2451 | case spv::OpUnreachable: |
| 2452 | return EmitUnreachable(insn, state); |
| 2453 | |
| 2454 | case spv::OpReturn: |
| 2455 | return EmitReturn(insn, state); |
| 2456 | |
Chris Forbes | 97e9589 | 2019-04-02 13:37:37 +1300 | [diff] [blame] | 2457 | case spv::OpKill: |
| 2458 | return EmitKill(insn, state); |
| 2459 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2460 | case spv::OpImageSampleImplicitLod: |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 2461 | return EmitImageSampleImplicitLod(None, insn, state); |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 2462 | |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 2463 | case spv::OpImageSampleExplicitLod: |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 2464 | return EmitImageSampleExplicitLod(None, insn, state); |
| 2465 | |
| 2466 | case spv::OpImageSampleDrefImplicitLod: |
| 2467 | return EmitImageSampleImplicitLod(Dref, insn, state); |
| 2468 | |
| 2469 | case spv::OpImageSampleDrefExplicitLod: |
| 2470 | return EmitImageSampleExplicitLod(Dref, insn, state); |
| 2471 | |
| 2472 | case spv::OpImageSampleProjImplicitLod: |
| 2473 | return EmitImageSampleImplicitLod(Proj, insn, state); |
| 2474 | |
| 2475 | case spv::OpImageSampleProjExplicitLod: |
| 2476 | return EmitImageSampleExplicitLod(Proj, insn, state); |
| 2477 | |
| 2478 | case spv::OpImageSampleProjDrefImplicitLod: |
| 2479 | return EmitImageSampleImplicitLod(ProjDref, insn, state); |
| 2480 | |
| 2481 | case spv::OpImageSampleProjDrefExplicitLod: |
| 2482 | return EmitImageSampleExplicitLod(ProjDref, insn, state); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 2483 | |
Chris Forbes | cd63159 | 2019-04-27 10:37:18 -0700 | [diff] [blame] | 2484 | case spv::OpImageFetch: |
| 2485 | return EmitImageFetch(insn, state); |
| 2486 | |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 2487 | case spv::OpImageQuerySize: |
| 2488 | return EmitImageQuerySize(insn, state); |
| 2489 | |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 2490 | case spv::OpImageQuerySizeLod: |
| 2491 | return EmitImageQuerySizeLod(insn, state); |
| 2492 | |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 2493 | case spv::OpImageRead: |
| 2494 | return EmitImageRead(insn, state); |
| 2495 | |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 2496 | case spv::OpImageWrite: |
| 2497 | return EmitImageWrite(insn, state); |
| 2498 | |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 2499 | case spv::OpImageTexelPointer: |
| 2500 | return EmitImageTexelPointer(insn, state); |
| 2501 | |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 2502 | case spv::OpSampledImage: |
| 2503 | case spv::OpImage: |
| 2504 | return EmitSampledImageCombineOrSplit(insn, state); |
| 2505 | |
Ben Clayton | 78abf37 | 2019-05-09 15:11:58 +0100 | [diff] [blame] | 2506 | case spv::OpCopyObject: |
| 2507 | return EmitCopyObject(insn, state); |
| 2508 | |
Ben Clayton | b5a4546 | 2019-04-30 19:21:29 +0100 | [diff] [blame] | 2509 | case spv::OpCopyMemory: |
| 2510 | return EmitCopyMemory(insn, state); |
| 2511 | |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 2512 | case spv::OpControlBarrier: |
| 2513 | return EmitControlBarrier(insn, state); |
| 2514 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 2515 | case spv::OpMemoryBarrier: |
| 2516 | return EmitMemoryBarrier(insn, state); |
| 2517 | |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 2518 | case spv::OpGroupNonUniformElect: |
| 2519 | return EmitGroupNonUniform(insn, state); |
| 2520 | |
Ben Clayton | e4605da | 2019-05-09 16:24:01 +0100 | [diff] [blame] | 2521 | case spv::OpArrayLength: |
| 2522 | return EmitArrayLength(insn, state); |
| 2523 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2524 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 2525 | UNREACHABLE("%s", OpcodeName(opcode).c_str()); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 2526 | break; |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 2527 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2528 | |
| 2529 | return EmitResult::Continue; |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 2530 | } |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 2531 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2532 | SpirvShader::EmitResult SpirvShader::EmitVariable(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2533 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2534 | auto routine = state->routine; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 2535 | Object::ID resultId = insn.word(2); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2536 | auto &object = getObject(resultId); |
| 2537 | auto &objectTy = getType(object.type); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2538 | |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2539 | switch (objectTy.storageClass) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2540 | { |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2541 | case spv::StorageClassOutput: |
| 2542 | case spv::StorageClassPrivate: |
| 2543 | case spv::StorageClassFunction: |
| 2544 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2545 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 2546 | auto base = &routine->getVariable(resultId)[0]; |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2547 | auto elementTy = getType(objectTy.element); |
| 2548 | auto size = elementTy.sizeInComponents * sizeof(float) * SIMD::Width; |
| 2549 | routine->createPointer(resultId, SIMD::Pointer(base, size)); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2550 | break; |
| 2551 | } |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 2552 | case spv::StorageClassWorkgroup: |
| 2553 | { |
| 2554 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
| 2555 | auto base = &routine->workgroupMemory[0]; |
| 2556 | auto size = workgroupMemory.size(); |
| 2557 | routine->createPointer(resultId, SIMD::Pointer(base, size, workgroupMemory.offsetOf(resultId))); |
| 2558 | break; |
| 2559 | } |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2560 | case spv::StorageClassInput: |
| 2561 | { |
| 2562 | if (object.kind == Object::Kind::InterfaceVariable) |
| 2563 | { |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 2564 | auto &dst = routine->getVariable(resultId); |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2565 | int offset = 0; |
| 2566 | VisitInterface(resultId, |
| 2567 | [&](Decorations const &d, AttribType type) { |
| 2568 | auto scalarSlot = d.Location << 2 | d.Component; |
| 2569 | dst[offset++] = routine->inputs[scalarSlot]; |
| 2570 | }); |
| 2571 | } |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2572 | ASSERT(objectTy.opcode() == spv::OpTypePointer); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 2573 | auto base = &routine->getVariable(resultId)[0]; |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2574 | auto elementTy = getType(objectTy.element); |
| 2575 | auto size = elementTy.sizeInComponents * sizeof(float) * SIMD::Width; |
| 2576 | routine->createPointer(resultId, SIMD::Pointer(base, size)); |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2577 | break; |
| 2578 | } |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2579 | case spv::StorageClassUniformConstant: |
| 2580 | { |
| 2581 | const auto &d = descriptorDecorations.at(resultId); |
| 2582 | ASSERT(d.DescriptorSet >= 0); |
| 2583 | ASSERT(d.Binding >= 0); |
| 2584 | |
| 2585 | uint32_t arrayIndex = 0; // TODO(b/129523279) |
| 2586 | auto setLayout = routine->pipelineLayout->getDescriptorSetLayout(d.DescriptorSet); |
Ben Clayton | 8c56e8d | 2019-04-25 08:24:01 +0100 | [diff] [blame] | 2587 | if (setLayout->hasBinding(d.Binding)) |
| 2588 | { |
| 2589 | size_t bindingOffset = setLayout->getBindingOffset(d.Binding, arrayIndex); |
| 2590 | Pointer<Byte> set = routine->descriptorSets[d.DescriptorSet]; // DescriptorSet* |
| 2591 | Pointer<Byte> binding = Pointer<Byte>(set + bindingOffset); // vk::SampledImageDescriptor* |
| 2592 | auto size = 0; // Not required as this pointer is not directly used by SIMD::Read or SIMD::Write. |
| 2593 | routine->createPointer(resultId, SIMD::Pointer(binding, size)); |
| 2594 | } |
| 2595 | else |
| 2596 | { |
| 2597 | // TODO: Error if the variable with the non-existant binding is |
| 2598 | // used? Or perhaps strip these unused variable declarations as |
| 2599 | // a preprocess on the SPIR-V? |
| 2600 | } |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2601 | break; |
| 2602 | } |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2603 | case spv::StorageClassUniform: |
| 2604 | case spv::StorageClassStorageBuffer: |
| 2605 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 2606 | const auto &d = descriptorDecorations.at(resultId); |
| 2607 | ASSERT(d.DescriptorSet >= 0 && d.DescriptorSet < vk::MAX_BOUND_DESCRIPTOR_SETS); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2608 | auto size = 0; // Not required as this pointer is not directly used by SIMD::Read or SIMD::Write. |
| 2609 | routine->createPointer(resultId, SIMD::Pointer(routine->descriptorSets[d.DescriptorSet], size)); |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2610 | break; |
| 2611 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 2612 | case spv::StorageClassPushConstant: |
| 2613 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2614 | routine->createPointer(resultId, SIMD::Pointer(routine->pushConstants, vk::MAX_PUSH_CONSTANT_SIZE)); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 2615 | break; |
| 2616 | } |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2617 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 2618 | UNREACHABLE("Storage class %d", objectTy.storageClass); |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2619 | break; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2620 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2621 | |
Ben Clayton | 05f27a3 | 2019-04-30 16:15:25 +0100 | [diff] [blame] | 2622 | if (insn.wordCount() > 4) |
| 2623 | { |
| 2624 | Object::ID initializerId = insn.word(4); |
| 2625 | if (getObject(initializerId).kind != Object::Kind::Constant) |
| 2626 | { |
| 2627 | UNIMPLEMENTED("Non-constant initializers not yet implemented"); |
| 2628 | } |
| 2629 | switch (objectTy.storageClass) |
| 2630 | { |
| 2631 | case spv::StorageClassOutput: |
| 2632 | case spv::StorageClassPrivate: |
| 2633 | case spv::StorageClassFunction: |
| 2634 | { |
| 2635 | bool interleavedByLane = IsStorageInterleavedByLane(objectTy.storageClass); |
| 2636 | auto ptr = routine->getPointer(resultId); |
| 2637 | GenericValue initialValue(this, routine, initializerId); |
| 2638 | VisitMemoryObject(resultId, [&](uint32_t i, uint32_t offset) |
| 2639 | { |
| 2640 | auto p = ptr + offset; |
| 2641 | if (interleavedByLane) { p = interleaveByLane(p); } |
| 2642 | SIMD::Store(p, initialValue.Float(i), state->activeLaneMask()); |
| 2643 | }); |
| 2644 | break; |
| 2645 | } |
| 2646 | default: |
| 2647 | ASSERT_MSG(initializerId == 0, "Vulkan does not permit variables of storage class %d to have initializers", int(objectTy.storageClass)); |
| 2648 | } |
| 2649 | } |
| 2650 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2651 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2654 | SpirvShader::EmitResult SpirvShader::EmitLoad(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2655 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2656 | auto routine = state->routine; |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 2657 | bool atomic = (insn.opcode() == spv::OpAtomicLoad); |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2658 | Object::ID resultId = insn.word(2); |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 2659 | Object::ID pointerId = insn.word(3); |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2660 | auto &result = getObject(resultId); |
| 2661 | auto &resultTy = getType(result.type); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2662 | auto &pointer = getObject(pointerId); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2663 | auto &pointerTy = getType(pointer.type); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 2664 | std::memory_order memoryOrder = std::memory_order_relaxed; |
| 2665 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 2666 | ASSERT(getType(pointer.type).element == result.type); |
| 2667 | ASSERT(Type::ID(insn.word(1)) == result.type); |
| 2668 | ASSERT(!atomic || getType(getType(pointer.type).element).opcode() == spv::OpTypeInt); // Vulkan 1.1: "Atomic instructions must declare a scalar 32-bit integer type, for the value pointed to by Pointer." |
| 2669 | |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 2670 | if(pointerTy.storageClass == spv::StorageClassUniformConstant) |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2671 | { |
| 2672 | // Just propagate the pointer. |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2673 | auto &ptr = routine->getPointer(pointerId); |
| 2674 | routine->createPointer(resultId, ptr); |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 2675 | return EmitResult::Continue; |
| 2676 | } |
| 2677 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 2678 | if(atomic) |
| 2679 | { |
| 2680 | Object::ID semanticsId = insn.word(5); |
| 2681 | auto memorySemantics = static_cast<spv::MemorySemanticsMask>(getObject(semanticsId).constantValue[0]); |
| 2682 | memoryOrder = MemoryOrder(memorySemantics); |
| 2683 | } |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2684 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2685 | if (pointerTy.storageClass == spv::StorageClassImage) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2686 | { |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2687 | UNIMPLEMENTED("StorageClassImage load not yet implemented"); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 2690 | auto ptr = GetPointerToData(pointerId, 0, routine); |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 2691 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2692 | bool interleavedByLane = IsStorageInterleavedByLane(pointerTy.storageClass); |
Ben Clayton | 49d8158 | 2019-03-12 20:05:04 +0000 | [diff] [blame] | 2693 | |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2694 | auto &dst = routine->createIntermediate(resultId, resultTy.sizeInComponents); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2695 | |
| 2696 | VisitMemoryObject(pointerId, [&](uint32_t i, uint32_t offset) |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 2697 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2698 | auto p = ptr + offset; |
| 2699 | if (interleavedByLane) { p = interleaveByLane(p); } |
| 2700 | dst.move(i, SIMD::Load<SIMD::Float>(p, state->activeLaneMask(), atomic, memoryOrder)); |
| 2701 | }); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2702 | |
| 2703 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2706 | SpirvShader::EmitResult SpirvShader::EmitStore(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2707 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2708 | auto routine = state->routine; |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 2709 | bool atomic = (insn.opcode() == spv::OpAtomicStore); |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 2710 | Object::ID pointerId = insn.word(1); |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 2711 | Object::ID objectId = insn.word(atomic ? 4 : 2); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2712 | auto &object = getObject(objectId); |
| 2713 | auto &pointer = getObject(pointerId); |
| 2714 | auto &pointerTy = getType(pointer.type); |
| 2715 | auto &elementTy = getType(pointerTy.element); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 2716 | std::memory_order memoryOrder = std::memory_order_relaxed; |
| 2717 | |
| 2718 | if(atomic) |
| 2719 | { |
| 2720 | Object::ID semanticsId = insn.word(3); |
| 2721 | auto memorySemantics = static_cast<spv::MemorySemanticsMask>(getObject(semanticsId).constantValue[0]); |
| 2722 | memoryOrder = MemoryOrder(memorySemantics); |
| 2723 | } |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2724 | |
Nicolas Capens | 5e8414e | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 2725 | 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." |
| 2726 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2727 | if (pointerTy.storageClass == spv::StorageClassImage) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2728 | { |
Ben Clayton | efec1b9 | 2019-03-05 17:38:16 +0000 | [diff] [blame] | 2729 | UNIMPLEMENTED("StorageClassImage store not yet implemented"); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 2732 | auto ptr = GetPointerToData(pointerId, 0, routine); |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 2733 | bool interleavedByLane = IsStorageInterleavedByLane(pointerTy.storageClass); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2734 | |
| 2735 | if (object.kind == Object::Kind::Constant) |
| 2736 | { |
Ben Clayton | 49d8158 | 2019-03-12 20:05:04 +0000 | [diff] [blame] | 2737 | // Constant source data. |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2738 | auto src = reinterpret_cast<float *>(object.constantValue.get()); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2739 | VisitMemoryObject(pointerId, [&](uint32_t i, uint32_t offset) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2740 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2741 | auto p = ptr + offset; |
| 2742 | if (interleavedByLane) { p = interleaveByLane(p); } |
| 2743 | SIMD::Store(p, SIMD::Float(src[i]), state->activeLaneMask(), atomic, memoryOrder); |
| 2744 | }); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2745 | } |
| 2746 | else |
| 2747 | { |
Ben Clayton | 49d8158 | 2019-03-12 20:05:04 +0000 | [diff] [blame] | 2748 | // Intermediate source data. |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2749 | auto &src = routine->getIntermediate(objectId); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2750 | VisitMemoryObject(pointerId, [&](uint32_t i, uint32_t offset) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2751 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 2752 | auto p = ptr + offset; |
| 2753 | if (interleavedByLane) { p = interleaveByLane(p); } |
| 2754 | SIMD::Store(p, src.Float(i), state->activeLaneMask(), atomic, memoryOrder); |
| 2755 | }); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2756 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2757 | |
| 2758 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2761 | SpirvShader::EmitResult SpirvShader::EmitAccessChain(InsnIterator insn, EmitState *state) const |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2762 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2763 | auto routine = state->routine; |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2764 | Type::ID typeId = insn.word(1); |
| 2765 | Object::ID resultId = insn.word(2); |
| 2766 | Object::ID baseId = insn.word(3); |
| 2767 | uint32_t numIndexes = insn.wordCount() - 4; |
| 2768 | const uint32_t *indexes = insn.wordPointer(4); |
| 2769 | auto &type = getType(typeId); |
| 2770 | ASSERT(type.sizeInComponents == 1); |
Ben Clayton | 1d514f3 | 2019-04-19 16:11:18 -0400 | [diff] [blame] | 2771 | ASSERT(getObject(resultId).kind == Object::Kind::Pointer); |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2772 | |
| 2773 | if(type.storageClass == spv::StorageClassPushConstant || |
| 2774 | type.storageClass == spv::StorageClassUniform || |
| 2775 | type.storageClass == spv::StorageClassStorageBuffer) |
| 2776 | { |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 2777 | auto ptr = WalkExplicitLayoutAccessChain(baseId, numIndexes, indexes, routine); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 2778 | routine->createPointer(resultId, ptr); |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2779 | } |
| 2780 | else |
| 2781 | { |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 2782 | auto ptr = WalkAccessChain(baseId, numIndexes, indexes, routine); |
| 2783 | routine->createPointer(resultId, ptr); |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2784 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2785 | |
| 2786 | return EmitResult::Continue; |
Nicolas Capens | fabdec5 | 2019-03-21 17:04:05 -0400 | [diff] [blame] | 2787 | } |
| 2788 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2789 | SpirvShader::EmitResult SpirvShader::EmitCompositeConstruct(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2790 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2791 | auto routine = state->routine; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2792 | auto &type = getType(insn.word(1)); |
| 2793 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2794 | auto offset = 0u; |
| 2795 | |
| 2796 | for (auto i = 0u; i < insn.wordCount() - 3; i++) |
| 2797 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 2798 | Object::ID srcObjectId = insn.word(3u + i); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2799 | auto & srcObject = getObject(srcObjectId); |
| 2800 | auto & srcObjectTy = getType(srcObject.type); |
| 2801 | GenericValue srcObjectAccess(this, routine, srcObjectId); |
| 2802 | |
| 2803 | for (auto j = 0u; j < srcObjectTy.sizeInComponents; j++) |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 2804 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2805 | dst.move(offset++, srcObjectAccess.Float(j)); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 2806 | } |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2807 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2808 | |
| 2809 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2812 | SpirvShader::EmitResult SpirvShader::EmitCompositeInsert(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2813 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2814 | auto routine = state->routine; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 2815 | Type::ID resultTypeId = insn.word(1); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2816 | auto &type = getType(resultTypeId); |
| 2817 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2818 | auto &newPartObject = getObject(insn.word(3)); |
| 2819 | auto &newPartObjectTy = getType(newPartObject.type); |
| 2820 | auto firstNewComponent = WalkLiteralAccessChain(resultTypeId, insn.wordCount() - 5, insn.wordPointer(5)); |
| 2821 | |
| 2822 | GenericValue srcObjectAccess(this, routine, insn.word(4)); |
| 2823 | GenericValue newPartObjectAccess(this, routine, insn.word(3)); |
| 2824 | |
| 2825 | // old components before |
| 2826 | for (auto i = 0u; i < firstNewComponent; i++) |
| 2827 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2828 | dst.move(i, srcObjectAccess.Float(i)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2829 | } |
| 2830 | // new part |
| 2831 | for (auto i = 0u; i < newPartObjectTy.sizeInComponents; i++) |
| 2832 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2833 | dst.move(firstNewComponent + i, newPartObjectAccess.Float(i)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2834 | } |
| 2835 | // old components after |
| 2836 | for (auto i = firstNewComponent + newPartObjectTy.sizeInComponents; i < type.sizeInComponents; i++) |
| 2837 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2838 | dst.move(i, srcObjectAccess.Float(i)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2839 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2840 | |
| 2841 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2844 | SpirvShader::EmitResult SpirvShader::EmitCompositeExtract(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2845 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2846 | auto routine = state->routine; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2847 | auto &type = getType(insn.word(1)); |
| 2848 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2849 | auto &compositeObject = getObject(insn.word(3)); |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 2850 | Type::ID compositeTypeId = compositeObject.definition.word(1); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2851 | auto firstComponent = WalkLiteralAccessChain(compositeTypeId, insn.wordCount() - 4, insn.wordPointer(4)); |
| 2852 | |
| 2853 | GenericValue compositeObjectAccess(this, routine, insn.word(3)); |
| 2854 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 2855 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2856 | dst.move(i, compositeObjectAccess.Float(firstComponent + i)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2857 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2858 | |
| 2859 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2862 | SpirvShader::EmitResult SpirvShader::EmitVectorShuffle(InsnIterator insn, EmitState *state) const |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2863 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2864 | auto routine = state->routine; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2865 | auto &type = getType(insn.word(1)); |
| 2866 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2867 | |
Chris Forbes | 13eba6c | 2019-03-08 10:41:05 -0800 | [diff] [blame] | 2868 | // Note: number of components in result type, first half type, and second |
| 2869 | // half type are all independent. |
| 2870 | auto &firstHalfType = getType(getObject(insn.word(3)).type); |
| 2871 | |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2872 | GenericValue firstHalfAccess(this, routine, insn.word(3)); |
| 2873 | GenericValue secondHalfAccess(this, routine, insn.word(4)); |
| 2874 | |
| 2875 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 2876 | { |
| 2877 | auto selector = insn.word(5 + i); |
| 2878 | if (selector == static_cast<uint32_t>(-1)) |
| 2879 | { |
| 2880 | // Undefined value. Until we decide to do real undef values, zero is as good |
| 2881 | // a value as any |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2882 | dst.move(i, RValue<SIMD::Float>(0.0f)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2883 | } |
Chris Forbes | 13eba6c | 2019-03-08 10:41:05 -0800 | [diff] [blame] | 2884 | else if (selector < firstHalfType.sizeInComponents) |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2885 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2886 | dst.move(i, firstHalfAccess.Float(selector)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2887 | } |
| 2888 | else |
| 2889 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2890 | dst.move(i, secondHalfAccess.Float(selector - firstHalfType.sizeInComponents)); |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2891 | } |
| 2892 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2893 | |
| 2894 | return EmitResult::Continue; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2897 | SpirvShader::EmitResult SpirvShader::EmitVectorExtractDynamic(InsnIterator insn, EmitState *state) const |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2898 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2899 | auto routine = state->routine; |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2900 | auto &type = getType(insn.word(1)); |
| 2901 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2902 | auto &srcType = getType(getObject(insn.word(3)).type); |
| 2903 | |
| 2904 | GenericValue src(this, routine, insn.word(3)); |
| 2905 | GenericValue index(this, routine, insn.word(4)); |
| 2906 | |
| 2907 | SIMD::UInt v = SIMD::UInt(0); |
| 2908 | |
| 2909 | for (auto i = 0u; i < srcType.sizeInComponents; i++) |
| 2910 | { |
| 2911 | v |= CmpEQ(index.UInt(0), SIMD::UInt(i)) & src.UInt(i); |
| 2912 | } |
| 2913 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2914 | dst.move(0, v); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2915 | return EmitResult::Continue; |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2916 | } |
| 2917 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2918 | SpirvShader::EmitResult SpirvShader::EmitVectorInsertDynamic(InsnIterator insn, EmitState *state) const |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2919 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2920 | auto routine = state->routine; |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2921 | auto &type = getType(insn.word(1)); |
| 2922 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2923 | |
| 2924 | GenericValue src(this, routine, insn.word(3)); |
| 2925 | GenericValue component(this, routine, insn.word(4)); |
| 2926 | GenericValue index(this, routine, insn.word(5)); |
| 2927 | |
| 2928 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 2929 | { |
| 2930 | SIMD::UInt mask = CmpEQ(SIMD::UInt(i), index.UInt(0)); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2931 | dst.move(i, (src.UInt(i) & ~mask) | (component.UInt(0) & mask)); |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2932 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2933 | return EmitResult::Continue; |
Chris Forbes | faed9d3 | 2019-03-15 10:31:08 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2936 | SpirvShader::EmitResult SpirvShader::EmitVectorTimesScalar(InsnIterator insn, EmitState *state) const |
Chris Forbes | 856ebf8 | 2019-03-08 15:30:18 -0800 | [diff] [blame] | 2937 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2938 | auto routine = state->routine; |
Chris Forbes | 856ebf8 | 2019-03-08 15:30:18 -0800 | [diff] [blame] | 2939 | auto &type = getType(insn.word(1)); |
| 2940 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 2941 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 2942 | auto rhs = GenericValue(this, routine, insn.word(4)); |
Chris Forbes | 856ebf8 | 2019-03-08 15:30:18 -0800 | [diff] [blame] | 2943 | |
| 2944 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 2945 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 2946 | dst.move(i, lhs.Float(i) * rhs.Float(0)); |
Chris Forbes | 856ebf8 | 2019-03-08 15:30:18 -0800 | [diff] [blame] | 2947 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 2948 | |
| 2949 | return EmitResult::Continue; |
Chris Forbes | 856ebf8 | 2019-03-08 15:30:18 -0800 | [diff] [blame] | 2950 | } |
| 2951 | |
Chris Forbes | 06f4ed7 | 2019-03-28 09:53:20 +1300 | [diff] [blame] | 2952 | SpirvShader::EmitResult SpirvShader::EmitMatrixTimesVector(InsnIterator insn, EmitState *state) const |
| 2953 | { |
| 2954 | auto routine = state->routine; |
| 2955 | auto &type = getType(insn.word(1)); |
| 2956 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2957 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 2958 | auto rhs = GenericValue(this, routine, insn.word(4)); |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 2959 | auto rhsType = getType(rhs.type); |
Chris Forbes | 06f4ed7 | 2019-03-28 09:53:20 +1300 | [diff] [blame] | 2960 | |
| 2961 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 2962 | { |
| 2963 | SIMD::Float v = lhs.Float(i) * rhs.Float(0); |
| 2964 | for (auto j = 1u; j < rhsType.sizeInComponents; j++) |
| 2965 | { |
| 2966 | v += lhs.Float(i + type.sizeInComponents * j) * rhs.Float(j); |
| 2967 | } |
| 2968 | dst.move(i, v); |
| 2969 | } |
| 2970 | |
| 2971 | return EmitResult::Continue; |
| 2972 | } |
| 2973 | |
Chris Forbes | a563dd8 | 2019-03-28 10:32:55 +1300 | [diff] [blame] | 2974 | SpirvShader::EmitResult SpirvShader::EmitVectorTimesMatrix(InsnIterator insn, EmitState *state) const |
| 2975 | { |
| 2976 | auto routine = state->routine; |
| 2977 | auto &type = getType(insn.word(1)); |
| 2978 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 2979 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 2980 | auto rhs = GenericValue(this, routine, insn.word(4)); |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 2981 | auto lhsType = getType(lhs.type); |
Chris Forbes | a563dd8 | 2019-03-28 10:32:55 +1300 | [diff] [blame] | 2982 | |
| 2983 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 2984 | { |
| 2985 | SIMD::Float v = lhs.Float(0) * rhs.Float(i * lhsType.sizeInComponents); |
| 2986 | for (auto j = 1u; j < lhsType.sizeInComponents; j++) |
| 2987 | { |
| 2988 | v += lhs.Float(j) * rhs.Float(i * lhsType.sizeInComponents + j); |
| 2989 | } |
| 2990 | dst.move(i, v); |
| 2991 | } |
| 2992 | |
| 2993 | return EmitResult::Continue; |
| 2994 | } |
| 2995 | |
Chris Forbes | 51562f1 | 2019-03-28 19:08:39 -0700 | [diff] [blame] | 2996 | SpirvShader::EmitResult SpirvShader::EmitMatrixTimesMatrix(InsnIterator insn, EmitState *state) const |
| 2997 | { |
| 2998 | auto routine = state->routine; |
| 2999 | auto &type = getType(insn.word(1)); |
| 3000 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 3001 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 3002 | auto rhs = GenericValue(this, routine, insn.word(4)); |
| 3003 | |
| 3004 | auto numColumns = type.definition.word(3); |
| 3005 | auto numRows = getType(type.definition.word(2)).definition.word(3); |
| 3006 | auto numAdds = getType(getObject(insn.word(3)).type).definition.word(3); |
| 3007 | |
| 3008 | for (auto row = 0u; row < numRows; row++) |
| 3009 | { |
| 3010 | for (auto col = 0u; col < numColumns; col++) |
| 3011 | { |
| 3012 | SIMD::Float v = SIMD::Float(0); |
| 3013 | for (auto i = 0u; i < numAdds; i++) |
| 3014 | { |
| 3015 | v += lhs.Float(i * numRows + row) * rhs.Float(col * numAdds + i); |
| 3016 | } |
| 3017 | dst.move(numRows * col + row, v); |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | return EmitResult::Continue; |
| 3022 | } |
| 3023 | |
Ben Clayton | 3ee5299 | 2019-04-08 11:01:23 -0400 | [diff] [blame] | 3024 | SpirvShader::EmitResult SpirvShader::EmitOuterProduct(InsnIterator insn, EmitState *state) const |
| 3025 | { |
| 3026 | auto routine = state->routine; |
| 3027 | auto &type = getType(insn.word(1)); |
| 3028 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 3029 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 3030 | auto rhs = GenericValue(this, routine, insn.word(4)); |
| 3031 | auto &lhsType = getType(lhs.type); |
| 3032 | auto &rhsType = getType(rhs.type); |
| 3033 | |
| 3034 | ASSERT(type.definition.opcode() == spv::OpTypeMatrix); |
| 3035 | ASSERT(lhsType.definition.opcode() == spv::OpTypeVector); |
| 3036 | ASSERT(rhsType.definition.opcode() == spv::OpTypeVector); |
| 3037 | ASSERT(getType(lhsType.element).opcode() == spv::OpTypeFloat); |
| 3038 | ASSERT(getType(rhsType.element).opcode() == spv::OpTypeFloat); |
| 3039 | |
| 3040 | auto numRows = lhsType.definition.word(3); |
| 3041 | auto numCols = rhsType.definition.word(3); |
| 3042 | |
| 3043 | for (auto col = 0u; col < numCols; col++) |
| 3044 | { |
| 3045 | for (auto row = 0u; row < numRows; row++) |
| 3046 | { |
| 3047 | dst.move(col * numRows + row, lhs.Float(row) * rhs.Float(col)); |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | return EmitResult::Continue; |
| 3052 | } |
| 3053 | |
Ben Clayton | 620f708 | 2019-04-08 11:12:08 -0400 | [diff] [blame] | 3054 | SpirvShader::EmitResult SpirvShader::EmitTranspose(InsnIterator insn, EmitState *state) const |
| 3055 | { |
| 3056 | auto routine = state->routine; |
| 3057 | auto &type = getType(insn.word(1)); |
| 3058 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 3059 | auto mat = GenericValue(this, routine, insn.word(3)); |
| 3060 | |
| 3061 | auto numCols = type.definition.word(3); |
| 3062 | auto numRows = getType(type.definition.word(2)).sizeInComponents; |
| 3063 | |
| 3064 | for (auto col = 0u; col < numCols; col++) |
| 3065 | { |
| 3066 | for (auto row = 0u; row < numRows; row++) |
| 3067 | { |
| 3068 | dst.move(col * numRows + row, mat.Float(row * numCols + col)); |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | return EmitResult::Continue; |
| 3073 | } |
| 3074 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3075 | SpirvShader::EmitResult SpirvShader::EmitUnaryOp(InsnIterator insn, EmitState *state) const |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3076 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3077 | auto routine = state->routine; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3078 | auto &type = getType(insn.word(1)); |
| 3079 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 3080 | auto src = GenericValue(this, routine, insn.word(3)); |
| 3081 | |
| 3082 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3083 | { |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3084 | switch (insn.opcode()) |
| 3085 | { |
| 3086 | case spv::OpNot: |
| 3087 | case spv::OpLogicalNot: // logical not == bitwise not due to all-bits boolean representation |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3088 | dst.move(i, ~src.UInt(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3089 | break; |
Ben Clayton | b5bfa50 | 2019-04-08 14:26:36 -0400 | [diff] [blame] | 3090 | case spv::OpBitFieldInsert: |
| 3091 | { |
| 3092 | auto insert = GenericValue(this, routine, insn.word(4)).UInt(i); |
| 3093 | auto offset = GenericValue(this, routine, insn.word(5)).UInt(0); |
| 3094 | auto count = GenericValue(this, routine, insn.word(6)).UInt(0); |
| 3095 | auto one = SIMD::UInt(1); |
| 3096 | auto v = src.UInt(i); |
| 3097 | auto mask = Bitmask32(offset + count) ^ Bitmask32(offset); |
| 3098 | dst.move(i, (v & ~mask) | ((insert << offset) & mask)); |
| 3099 | break; |
| 3100 | } |
Ben Clayton | d86db95 | 2019-04-08 13:43:11 -0400 | [diff] [blame] | 3101 | case spv::OpBitFieldSExtract: |
| 3102 | case spv::OpBitFieldUExtract: |
| 3103 | { |
| 3104 | auto offset = GenericValue(this, routine, insn.word(4)).UInt(0); |
| 3105 | auto count = GenericValue(this, routine, insn.word(5)).UInt(0); |
| 3106 | auto one = SIMD::UInt(1); |
| 3107 | auto v = src.UInt(i); |
| 3108 | SIMD::UInt out = (v >> offset) & Bitmask32(count); |
| 3109 | if (insn.opcode() == spv::OpBitFieldSExtract) |
| 3110 | { |
| 3111 | auto sign = out & NthBit32(count - one); |
| 3112 | auto sext = ~(sign - one); |
| 3113 | out |= sext; |
| 3114 | } |
| 3115 | dst.move(i, out); |
| 3116 | break; |
| 3117 | } |
Ben Clayton | d2a4643 | 2019-04-08 11:41:45 -0400 | [diff] [blame] | 3118 | case spv::OpBitReverse: |
Ben Clayton | e339d6c | 2019-04-13 16:49:39 +0000 | [diff] [blame] | 3119 | { |
Ben Clayton | db4f3df | 2019-04-13 16:48:33 +0000 | [diff] [blame] | 3120 | // TODO: Add an intrinsic to reactor. Even if there isn't a |
| 3121 | // single vector instruction, there may be target-dependent |
| 3122 | // ways to make this faster. |
| 3123 | // https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel |
| 3124 | SIMD::UInt v = src.UInt(i); |
| 3125 | v = ((v >> 1) & SIMD::UInt(0x55555555)) | ((v & SIMD::UInt(0x55555555)) << 1); |
| 3126 | v = ((v >> 2) & SIMD::UInt(0x33333333)) | ((v & SIMD::UInt(0x33333333)) << 2); |
| 3127 | v = ((v >> 4) & SIMD::UInt(0x0F0F0F0F)) | ((v & SIMD::UInt(0x0F0F0F0F)) << 4); |
| 3128 | v = ((v >> 8) & SIMD::UInt(0x00FF00FF)) | ((v & SIMD::UInt(0x00FF00FF)) << 8); |
| 3129 | v = (v >> 16) | (v << 16); |
| 3130 | dst.move(i, v); |
Ben Clayton | d2a4643 | 2019-04-08 11:41:45 -0400 | [diff] [blame] | 3131 | break; |
Ben Clayton | e339d6c | 2019-04-13 16:49:39 +0000 | [diff] [blame] | 3132 | } |
Ben Clayton | 1eb017d | 2019-04-08 11:32:09 -0400 | [diff] [blame] | 3133 | case spv::OpBitCount: |
Ben Clayton | e339d6c | 2019-04-13 16:49:39 +0000 | [diff] [blame] | 3134 | { |
| 3135 | // TODO: Add an intrinsic to reactor. Even if there isn't a |
| 3136 | // single vector instruction, there may be target-dependent |
| 3137 | // ways to make this faster. |
| 3138 | // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel |
| 3139 | auto v = src.UInt(i); |
| 3140 | SIMD::UInt c = v - ((v >> 1) & SIMD::UInt(0x55555555)); |
| 3141 | c = ((c >> 2) & SIMD::UInt(0x33333333)) + (c & SIMD::UInt(0x33333333)); |
| 3142 | c = ((c >> 4) + c) & SIMD::UInt(0x0F0F0F0F); |
| 3143 | c = ((c >> 8) + c) & SIMD::UInt(0x00FF00FF); |
| 3144 | c = ((c >> 16) + c) & SIMD::UInt(0x0000FFFF); |
| 3145 | dst.move(i, c); |
Ben Clayton | 1eb017d | 2019-04-08 11:32:09 -0400 | [diff] [blame] | 3146 | break; |
Ben Clayton | e339d6c | 2019-04-13 16:49:39 +0000 | [diff] [blame] | 3147 | } |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3148 | case spv::OpSNegate: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3149 | dst.move(i, -src.Int(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3150 | break; |
| 3151 | case spv::OpFNegate: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3152 | dst.move(i, -src.Float(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3153 | break; |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 3154 | case spv::OpConvertFToU: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3155 | dst.move(i, SIMD::UInt(src.Float(i))); |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 3156 | break; |
| 3157 | case spv::OpConvertFToS: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3158 | dst.move(i, SIMD::Int(src.Float(i))); |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 3159 | break; |
| 3160 | case spv::OpConvertSToF: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3161 | dst.move(i, SIMD::Float(src.Int(i))); |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 3162 | break; |
| 3163 | case spv::OpConvertUToF: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3164 | dst.move(i, SIMD::Float(src.UInt(i))); |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 3165 | break; |
| 3166 | case spv::OpBitcast: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3167 | dst.move(i, src.Float(i)); |
Chris Forbes | 4d50305 | 2019-03-01 17:13:57 -0800 | [diff] [blame] | 3168 | break; |
Chris Forbes | 3ed33ce | 2019-03-07 13:38:31 -0800 | [diff] [blame] | 3169 | case spv::OpIsInf: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3170 | dst.move(i, IsInf(src.Float(i))); |
Chris Forbes | 3ed33ce | 2019-03-07 13:38:31 -0800 | [diff] [blame] | 3171 | break; |
| 3172 | case spv::OpIsNan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3173 | dst.move(i, IsNan(src.Float(i))); |
Chris Forbes | 3ed33ce | 2019-03-07 13:38:31 -0800 | [diff] [blame] | 3174 | break; |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3175 | case spv::OpDPdx: |
| 3176 | case spv::OpDPdxCoarse: |
| 3177 | // Derivative instructions: FS invocations are laid out like so: |
| 3178 | // 0 1 |
| 3179 | // 2 3 |
| 3180 | static_assert(SIMD::Width == 4, "All cross-lane instructions will need care when using a different width"); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3181 | dst.move(i, SIMD::Float(Extract(src.Float(i), 1) - Extract(src.Float(i), 0))); |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3182 | break; |
| 3183 | case spv::OpDPdy: |
| 3184 | case spv::OpDPdyCoarse: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3185 | dst.move(i, SIMD::Float(Extract(src.Float(i), 2) - Extract(src.Float(i), 0))); |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3186 | break; |
| 3187 | case spv::OpFwidth: |
| 3188 | case spv::OpFwidthCoarse: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3189 | dst.move(i, SIMD::Float(Abs(Extract(src.Float(i), 1) - Extract(src.Float(i), 0)) |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3190 | + Abs(Extract(src.Float(i), 2) - Extract(src.Float(i), 0)))); |
| 3191 | break; |
| 3192 | case spv::OpDPdxFine: |
| 3193 | { |
| 3194 | auto firstRow = Extract(src.Float(i), 1) - Extract(src.Float(i), 0); |
| 3195 | auto secondRow = Extract(src.Float(i), 3) - Extract(src.Float(i), 2); |
| 3196 | SIMD::Float v = SIMD::Float(firstRow); |
| 3197 | v = Insert(v, secondRow, 2); |
| 3198 | v = Insert(v, secondRow, 3); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3199 | dst.move(i, v); |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3200 | break; |
| 3201 | } |
| 3202 | case spv::OpDPdyFine: |
| 3203 | { |
| 3204 | auto firstColumn = Extract(src.Float(i), 2) - Extract(src.Float(i), 0); |
| 3205 | auto secondColumn = Extract(src.Float(i), 3) - Extract(src.Float(i), 1); |
| 3206 | SIMD::Float v = SIMD::Float(firstColumn); |
| 3207 | v = Insert(v, secondColumn, 1); |
| 3208 | v = Insert(v, secondColumn, 3); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3209 | dst.move(i, v); |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3210 | break; |
| 3211 | } |
| 3212 | case spv::OpFwidthFine: |
| 3213 | { |
| 3214 | auto firstRow = Extract(src.Float(i), 1) - Extract(src.Float(i), 0); |
| 3215 | auto secondRow = Extract(src.Float(i), 3) - Extract(src.Float(i), 2); |
| 3216 | SIMD::Float dpdx = SIMD::Float(firstRow); |
| 3217 | dpdx = Insert(dpdx, secondRow, 2); |
| 3218 | dpdx = Insert(dpdx, secondRow, 3); |
| 3219 | auto firstColumn = Extract(src.Float(i), 2) - Extract(src.Float(i), 0); |
| 3220 | auto secondColumn = Extract(src.Float(i), 3) - Extract(src.Float(i), 1); |
| 3221 | SIMD::Float dpdy = SIMD::Float(firstColumn); |
| 3222 | dpdy = Insert(dpdy, secondColumn, 1); |
| 3223 | dpdy = Insert(dpdy, secondColumn, 3); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3224 | dst.move(i, Abs(dpdx) + Abs(dpdy)); |
Chris Forbes | aff2dd0 | 2019-03-20 14:50:24 -0700 | [diff] [blame] | 3225 | break; |
| 3226 | } |
Ben Clayton | 64da4ae | 2019-04-19 12:34:06 -0400 | [diff] [blame] | 3227 | case spv::OpQuantizeToF16: |
| 3228 | { |
| 3229 | auto abs = Abs(src.Float(i)); |
| 3230 | auto sign = src.Int(i) & SIMD::Int(0x80000000); |
| 3231 | auto isZero = CmpLT(abs, SIMD::Float(0.000061035)); |
| 3232 | auto isInf = CmpGT(abs, SIMD::Float(65504.0f)); |
| 3233 | auto isNaN = IsNan(abs); |
| 3234 | auto isInfOrNan = isInf | isNaN; |
| 3235 | SIMD::Int v = src.Int(i) & SIMD::Int(0xFFFFE000); |
| 3236 | v &= ~isZero | SIMD::Int(0x80000000); |
| 3237 | v = sign | (isInfOrNan & SIMD::Int(0x7F800000)) | (~isInfOrNan & v); |
| 3238 | v |= isNaN & SIMD::Int(0x400000); |
| 3239 | dst.move(i, v); |
| 3240 | break; |
| 3241 | } |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3242 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 3243 | UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str()); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3244 | } |
| 3245 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3246 | |
| 3247 | return EmitResult::Continue; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3250 | SpirvShader::EmitResult SpirvShader::EmitBinaryOp(InsnIterator insn, EmitState *state) const |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3251 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3252 | auto routine = state->routine; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3253 | auto &type = getType(insn.word(1)); |
| 3254 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3255 | auto &lhsType = getType(getObject(insn.word(3)).type); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3256 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 3257 | auto rhs = GenericValue(this, routine, insn.word(4)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3258 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3259 | for (auto i = 0u; i < lhsType.sizeInComponents; i++) |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3260 | { |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3261 | switch (insn.opcode()) |
| 3262 | { |
| 3263 | case spv::OpIAdd: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3264 | dst.move(i, lhs.Int(i) + rhs.Int(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3265 | break; |
| 3266 | case spv::OpISub: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3267 | dst.move(i, lhs.Int(i) - rhs.Int(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3268 | break; |
| 3269 | case spv::OpIMul: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3270 | dst.move(i, lhs.Int(i) * rhs.Int(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3271 | break; |
| 3272 | case spv::OpSDiv: |
Ben Clayton | 49d2c13 | 2019-03-14 12:21:53 +0000 | [diff] [blame] | 3273 | { |
Ben Clayton | a2749f3 | 2019-03-14 19:32:41 +0000 | [diff] [blame] | 3274 | SIMD::Int a = lhs.Int(i); |
| 3275 | SIMD::Int b = rhs.Int(i); |
| 3276 | b = b | CmpEQ(b, SIMD::Int(0)); // prevent divide-by-zero |
| 3277 | a = a | (CmpEQ(a, SIMD::Int(0x80000000)) & CmpEQ(b, SIMD::Int(-1))); // prevent integer overflow |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3278 | dst.move(i, a / b); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3279 | break; |
Ben Clayton | 49d2c13 | 2019-03-14 12:21:53 +0000 | [diff] [blame] | 3280 | } |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3281 | case spv::OpUDiv: |
Ben Clayton | 49d2c13 | 2019-03-14 12:21:53 +0000 | [diff] [blame] | 3282 | { |
| 3283 | auto zeroMask = As<SIMD::UInt>(CmpEQ(rhs.Int(i), SIMD::Int(0))); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3284 | dst.move(i, lhs.UInt(i) / (rhs.UInt(i) | zeroMask)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3285 | break; |
Ben Clayton | 49d2c13 | 2019-03-14 12:21:53 +0000 | [diff] [blame] | 3286 | } |
Chris Forbes | 71673c8 | 2019-03-14 12:55:20 -0700 | [diff] [blame] | 3287 | case spv::OpSRem: |
| 3288 | { |
| 3289 | SIMD::Int a = lhs.Int(i); |
| 3290 | SIMD::Int b = rhs.Int(i); |
| 3291 | b = b | CmpEQ(b, SIMD::Int(0)); // prevent divide-by-zero |
| 3292 | a = a | (CmpEQ(a, SIMD::Int(0x80000000)) & CmpEQ(b, SIMD::Int(-1))); // prevent integer overflow |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3293 | dst.move(i, a % b); |
Chris Forbes | 71673c8 | 2019-03-14 12:55:20 -0700 | [diff] [blame] | 3294 | break; |
| 3295 | } |
Ben Clayton | bb8c8e2 | 2019-03-08 12:04:00 +0000 | [diff] [blame] | 3296 | case spv::OpSMod: |
| 3297 | { |
Ben Clayton | a2749f3 | 2019-03-14 19:32:41 +0000 | [diff] [blame] | 3298 | SIMD::Int a = lhs.Int(i); |
| 3299 | SIMD::Int b = rhs.Int(i); |
| 3300 | b = b | CmpEQ(b, SIMD::Int(0)); // prevent divide-by-zero |
| 3301 | a = a | (CmpEQ(a, SIMD::Int(0x80000000)) & CmpEQ(b, SIMD::Int(-1))); // prevent integer overflow |
| 3302 | auto mod = a % b; |
Ben Clayton | bb8c8e2 | 2019-03-08 12:04:00 +0000 | [diff] [blame] | 3303 | // If a and b have opposite signs, the remainder operation takes |
| 3304 | // the sign from a but OpSMod is supposed to take the sign of b. |
| 3305 | // Adding b will ensure that the result has the correct sign and |
| 3306 | // that it is still congruent to a modulo b. |
| 3307 | // |
| 3308 | // See also http://mathforum.org/library/drmath/view/52343.html |
| 3309 | auto signDiff = CmpNEQ(CmpGE(a, SIMD::Int(0)), CmpGE(b, SIMD::Int(0))); |
| 3310 | auto fixedMod = mod + (b & CmpNEQ(mod, SIMD::Int(0)) & signDiff); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3311 | dst.move(i, As<SIMD::Float>(fixedMod)); |
Ben Clayton | bb8c8e2 | 2019-03-08 12:04:00 +0000 | [diff] [blame] | 3312 | break; |
| 3313 | } |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3314 | case spv::OpUMod: |
Chris Forbes | 3ebf583 | 2019-03-14 08:15:25 -0700 | [diff] [blame] | 3315 | { |
| 3316 | auto zeroMask = As<SIMD::UInt>(CmpEQ(rhs.Int(i), SIMD::Int(0))); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3317 | dst.move(i, lhs.UInt(i) % (rhs.UInt(i) | zeroMask)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3318 | break; |
Chris Forbes | 3ebf583 | 2019-03-14 08:15:25 -0700 | [diff] [blame] | 3319 | } |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3320 | case spv::OpIEqual: |
Chris Forbes | 787b446 | 2019-03-08 12:16:57 -0800 | [diff] [blame] | 3321 | case spv::OpLogicalEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3322 | dst.move(i, CmpEQ(lhs.Int(i), rhs.Int(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3323 | break; |
| 3324 | case spv::OpINotEqual: |
Chris Forbes | 787b446 | 2019-03-08 12:16:57 -0800 | [diff] [blame] | 3325 | case spv::OpLogicalNotEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3326 | dst.move(i, CmpNEQ(lhs.Int(i), rhs.Int(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3327 | break; |
| 3328 | case spv::OpUGreaterThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3329 | dst.move(i, CmpGT(lhs.UInt(i), rhs.UInt(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3330 | break; |
| 3331 | case spv::OpSGreaterThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3332 | dst.move(i, CmpGT(lhs.Int(i), rhs.Int(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3333 | break; |
| 3334 | case spv::OpUGreaterThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3335 | dst.move(i, CmpGE(lhs.UInt(i), rhs.UInt(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3336 | break; |
| 3337 | case spv::OpSGreaterThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3338 | dst.move(i, CmpGE(lhs.Int(i), rhs.Int(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3339 | break; |
| 3340 | case spv::OpULessThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3341 | dst.move(i, CmpLT(lhs.UInt(i), rhs.UInt(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | case spv::OpSLessThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3344 | dst.move(i, CmpLT(lhs.Int(i), rhs.Int(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3345 | break; |
| 3346 | case spv::OpULessThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3347 | dst.move(i, CmpLE(lhs.UInt(i), rhs.UInt(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3348 | break; |
| 3349 | case spv::OpSLessThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3350 | dst.move(i, CmpLE(lhs.Int(i), rhs.Int(i))); |
Ben Clayton | e95eeb1 | 2019-03-04 16:32:09 +0000 | [diff] [blame] | 3351 | break; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3352 | case spv::OpFAdd: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3353 | dst.move(i, lhs.Float(i) + rhs.Float(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3354 | break; |
| 3355 | case spv::OpFSub: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3356 | dst.move(i, lhs.Float(i) - rhs.Float(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3357 | break; |
Chris Forbes | 9d93153 | 2019-03-08 09:53:03 -0800 | [diff] [blame] | 3358 | case spv::OpFMul: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3359 | dst.move(i, lhs.Float(i) * rhs.Float(i)); |
Chris Forbes | 9d93153 | 2019-03-08 09:53:03 -0800 | [diff] [blame] | 3360 | break; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3361 | case spv::OpFDiv: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3362 | dst.move(i, lhs.Float(i) / rhs.Float(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3363 | break; |
Chris Forbes | 0e4d6ff | 2019-03-15 13:43:36 -0700 | [diff] [blame] | 3364 | case spv::OpFMod: |
| 3365 | // TODO(b/126873455): inaccurate for values greater than 2^24 |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3366 | dst.move(i, lhs.Float(i) - rhs.Float(i) * Floor(lhs.Float(i) / rhs.Float(i))); |
Chris Forbes | 0e4d6ff | 2019-03-15 13:43:36 -0700 | [diff] [blame] | 3367 | break; |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 3368 | case spv::OpFRem: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3369 | dst.move(i, lhs.Float(i) % rhs.Float(i)); |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 3370 | break; |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3371 | case spv::OpFOrdEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3372 | dst.move(i, CmpEQ(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3373 | break; |
| 3374 | case spv::OpFUnordEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3375 | dst.move(i, CmpUEQ(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3376 | break; |
| 3377 | case spv::OpFOrdNotEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3378 | dst.move(i, CmpNEQ(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3379 | break; |
| 3380 | case spv::OpFUnordNotEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3381 | dst.move(i, CmpUNEQ(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3382 | break; |
| 3383 | case spv::OpFOrdLessThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3384 | dst.move(i, CmpLT(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3385 | break; |
| 3386 | case spv::OpFUnordLessThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3387 | dst.move(i, CmpULT(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3388 | break; |
| 3389 | case spv::OpFOrdGreaterThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3390 | dst.move(i, CmpGT(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3391 | break; |
| 3392 | case spv::OpFUnordGreaterThan: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3393 | dst.move(i, CmpUGT(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3394 | break; |
| 3395 | case spv::OpFOrdLessThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3396 | dst.move(i, CmpLE(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3397 | break; |
| 3398 | case spv::OpFUnordLessThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3399 | dst.move(i, CmpULE(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | case spv::OpFOrdGreaterThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3402 | dst.move(i, CmpGE(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3403 | break; |
| 3404 | case spv::OpFUnordGreaterThanEqual: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3405 | dst.move(i, CmpUGE(lhs.Float(i), rhs.Float(i))); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3406 | break; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3407 | case spv::OpShiftRightLogical: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3408 | dst.move(i, lhs.UInt(i) >> rhs.UInt(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3409 | break; |
| 3410 | case spv::OpShiftRightArithmetic: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3411 | dst.move(i, lhs.Int(i) >> rhs.Int(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3412 | break; |
| 3413 | case spv::OpShiftLeftLogical: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3414 | dst.move(i, lhs.UInt(i) << rhs.UInt(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3415 | break; |
| 3416 | case spv::OpBitwiseOr: |
| 3417 | case spv::OpLogicalOr: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3418 | dst.move(i, lhs.UInt(i) | rhs.UInt(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | case spv::OpBitwiseXor: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3421 | dst.move(i, lhs.UInt(i) ^ rhs.UInt(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3422 | break; |
| 3423 | case spv::OpBitwiseAnd: |
| 3424 | case spv::OpLogicalAnd: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3425 | dst.move(i, lhs.UInt(i) & rhs.UInt(i)); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3426 | break; |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3427 | case spv::OpSMulExtended: |
| 3428 | // Extended ops: result is a structure containing two members of the same type as lhs & rhs. |
| 3429 | // In our flat view then, component i is the i'th component of the first member; |
| 3430 | // component i + N is the i'th component of the second member. |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3431 | dst.move(i, lhs.Int(i) * rhs.Int(i)); |
| 3432 | dst.move(i + lhsType.sizeInComponents, MulHigh(lhs.Int(i), rhs.Int(i))); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3433 | break; |
| 3434 | case spv::OpUMulExtended: |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3435 | dst.move(i, lhs.UInt(i) * rhs.UInt(i)); |
| 3436 | dst.move(i + lhsType.sizeInComponents, MulHigh(lhs.UInt(i), rhs.UInt(i))); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3437 | break; |
Chris Forbes | 3e6f60b | 2019-05-08 17:28:10 -0700 | [diff] [blame] | 3438 | case spv::OpIAddCarry: |
| 3439 | dst.move(i, lhs.UInt(i) + rhs.UInt(i)); |
| 3440 | dst.move(i + lhsType.sizeInComponents, CmpLT(dst.UInt(i), lhs.UInt(i)) >> 31); |
| 3441 | break; |
| 3442 | case spv::OpISubBorrow: |
| 3443 | dst.move(i, lhs.UInt(i) - rhs.UInt(i)); |
| 3444 | dst.move(i + lhsType.sizeInComponents, CmpLT(lhs.UInt(i), rhs.UInt(i)) >> 31); |
| 3445 | break; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3446 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 3447 | UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str()); |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3448 | } |
| 3449 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3450 | |
| 3451 | return EmitResult::Continue; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3454 | SpirvShader::EmitResult SpirvShader::EmitDot(InsnIterator insn, EmitState *state) const |
Chris Forbes | 2b287cc | 2019-03-01 13:24:17 -0800 | [diff] [blame] | 3455 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3456 | auto routine = state->routine; |
Chris Forbes | 2b287cc | 2019-03-01 13:24:17 -0800 | [diff] [blame] | 3457 | auto &type = getType(insn.word(1)); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 3458 | ASSERT(type.sizeInComponents == 1); |
Chris Forbes | 2b287cc | 2019-03-01 13:24:17 -0800 | [diff] [blame] | 3459 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 3460 | auto &lhsType = getType(getObject(insn.word(3)).type); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3461 | auto lhs = GenericValue(this, routine, insn.word(3)); |
| 3462 | auto rhs = GenericValue(this, routine, insn.word(4)); |
Chris Forbes | 2b287cc | 2019-03-01 13:24:17 -0800 | [diff] [blame] | 3463 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3464 | dst.move(0, Dot(lhsType.sizeInComponents, lhs, rhs)); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3465 | return EmitResult::Continue; |
Chris Forbes | 2b287cc | 2019-03-01 13:24:17 -0800 | [diff] [blame] | 3466 | } |
| 3467 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3468 | SpirvShader::EmitResult SpirvShader::EmitSelect(InsnIterator insn, EmitState *state) const |
Ben Clayton | bf943f6 | 2019-03-05 12:57:39 +0000 | [diff] [blame] | 3469 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3470 | auto routine = state->routine; |
Ben Clayton | bf943f6 | 2019-03-05 12:57:39 +0000 | [diff] [blame] | 3471 | auto &type = getType(insn.word(1)); |
| 3472 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3473 | auto cond = GenericValue(this, routine, insn.word(3)); |
| 3474 | auto lhs = GenericValue(this, routine, insn.word(4)); |
| 3475 | auto rhs = GenericValue(this, routine, insn.word(5)); |
Ben Clayton | bf943f6 | 2019-03-05 12:57:39 +0000 | [diff] [blame] | 3476 | |
| 3477 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3478 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3479 | dst.move(i, (cond.Int(i) & lhs.Int(i)) | (~cond.Int(i) & rhs.Int(i))); // FIXME: IfThenElse() |
Ben Clayton | bf943f6 | 2019-03-05 12:57:39 +0000 | [diff] [blame] | 3480 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3481 | |
| 3482 | return EmitResult::Continue; |
Ben Clayton | bf943f6 | 2019-03-05 12:57:39 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3485 | SpirvShader::EmitResult SpirvShader::EmitExtendedInstruction(InsnIterator insn, EmitState *state) const |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 3486 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 3487 | auto routine = state->routine; |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 3488 | auto &type = getType(insn.word(1)); |
| 3489 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 3490 | auto extInstIndex = static_cast<GLSLstd450>(insn.word(4)); |
| 3491 | |
| 3492 | switch (extInstIndex) |
| 3493 | { |
| 3494 | case GLSLstd450FAbs: |
| 3495 | { |
| 3496 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3497 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3498 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3499 | dst.move(i, Abs(src.Float(i))); |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 3500 | } |
| 3501 | break; |
| 3502 | } |
| 3503 | case GLSLstd450SAbs: |
| 3504 | { |
| 3505 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3506 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3507 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3508 | dst.move(i, Abs(src.Int(i))); |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 3509 | } |
| 3510 | break; |
| 3511 | } |
Chris Forbes | 15dff36 | 2019-03-08 11:31:31 -0800 | [diff] [blame] | 3512 | case GLSLstd450Cross: |
| 3513 | { |
| 3514 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3515 | auto rhs = GenericValue(this, routine, insn.word(6)); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3516 | dst.move(0, lhs.Float(1) * rhs.Float(2) - rhs.Float(1) * lhs.Float(2)); |
| 3517 | dst.move(1, lhs.Float(2) * rhs.Float(0) - rhs.Float(2) * lhs.Float(0)); |
| 3518 | dst.move(2, lhs.Float(0) * rhs.Float(1) - rhs.Float(0) * lhs.Float(1)); |
Chris Forbes | 15dff36 | 2019-03-08 11:31:31 -0800 | [diff] [blame] | 3519 | break; |
| 3520 | } |
Chris Forbes | c212bbd | 2019-03-08 12:02:27 -0800 | [diff] [blame] | 3521 | case GLSLstd450Floor: |
| 3522 | { |
| 3523 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3524 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3525 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3526 | dst.move(i, Floor(src.Float(i))); |
Chris Forbes | c212bbd | 2019-03-08 12:02:27 -0800 | [diff] [blame] | 3527 | } |
| 3528 | break; |
| 3529 | } |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3530 | case GLSLstd450Trunc: |
| 3531 | { |
| 3532 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3533 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3534 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3535 | dst.move(i, Trunc(src.Float(i))); |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3536 | } |
| 3537 | break; |
| 3538 | } |
| 3539 | case GLSLstd450Ceil: |
| 3540 | { |
| 3541 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3542 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3543 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3544 | dst.move(i, Ceil(src.Float(i))); |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3545 | } |
| 3546 | break; |
| 3547 | } |
| 3548 | case GLSLstd450Fract: |
| 3549 | { |
| 3550 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3551 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3552 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3553 | dst.move(i, Frac(src.Float(i))); |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3554 | } |
| 3555 | break; |
| 3556 | } |
| 3557 | case GLSLstd450Round: |
| 3558 | { |
| 3559 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3560 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3561 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3562 | dst.move(i, Round(src.Float(i))); |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3563 | } |
| 3564 | break; |
| 3565 | } |
| 3566 | case GLSLstd450RoundEven: |
| 3567 | { |
| 3568 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3569 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3570 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3571 | auto x = Round(src.Float(i)); |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3572 | // dst = round(src) + ((round(src) < src) * 2 - 1) * (fract(src) == 0.5) * isOdd(round(src)); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3573 | dst.move(i, x + ((SIMD::Float(CmpLT(x, src.Float(i)) & SIMD::Int(1)) * SIMD::Float(2.0f)) - SIMD::Float(1.0f)) * |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3574 | SIMD::Float(CmpEQ(Frac(src.Float(i)), SIMD::Float(0.5f)) & SIMD::Int(1)) * SIMD::Float(Int4(x) & SIMD::Int(1))); |
Chris Forbes | dd172cc | 2019-03-08 13:36:40 -0800 | [diff] [blame] | 3575 | } |
| 3576 | break; |
| 3577 | } |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3578 | case GLSLstd450FMin: |
| 3579 | { |
| 3580 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3581 | auto rhs = GenericValue(this, routine, insn.word(6)); |
| 3582 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3583 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3584 | dst.move(i, Min(lhs.Float(i), rhs.Float(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3585 | } |
| 3586 | break; |
| 3587 | } |
| 3588 | case GLSLstd450FMax: |
| 3589 | { |
| 3590 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3591 | auto rhs = GenericValue(this, routine, insn.word(6)); |
| 3592 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3593 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3594 | dst.move(i, Max(lhs.Float(i), rhs.Float(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3595 | } |
| 3596 | break; |
| 3597 | } |
| 3598 | case GLSLstd450SMin: |
| 3599 | { |
| 3600 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3601 | auto rhs = GenericValue(this, routine, insn.word(6)); |
| 3602 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3603 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3604 | dst.move(i, Min(lhs.Int(i), rhs.Int(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3605 | } |
| 3606 | break; |
| 3607 | } |
| 3608 | case GLSLstd450SMax: |
| 3609 | { |
| 3610 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3611 | auto rhs = GenericValue(this, routine, insn.word(6)); |
| 3612 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3613 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3614 | dst.move(i, Max(lhs.Int(i), rhs.Int(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3615 | } |
| 3616 | break; |
| 3617 | } |
| 3618 | case GLSLstd450UMin: |
| 3619 | { |
| 3620 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3621 | auto rhs = GenericValue(this, routine, insn.word(6)); |
| 3622 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3623 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3624 | dst.move(i, Min(lhs.UInt(i), rhs.UInt(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3625 | } |
| 3626 | break; |
| 3627 | } |
| 3628 | case GLSLstd450UMax: |
| 3629 | { |
| 3630 | auto lhs = GenericValue(this, routine, insn.word(5)); |
| 3631 | auto rhs = GenericValue(this, routine, insn.word(6)); |
| 3632 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3633 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3634 | dst.move(i, Max(lhs.UInt(i), rhs.UInt(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3635 | } |
| 3636 | break; |
| 3637 | } |
| 3638 | case GLSLstd450Step: |
| 3639 | { |
| 3640 | auto edge = GenericValue(this, routine, insn.word(5)); |
| 3641 | auto x = GenericValue(this, routine, insn.word(6)); |
| 3642 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3643 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3644 | dst.move(i, CmpNLT(x.Float(i), edge.Float(i)) & As<SIMD::Int>(SIMD::Float(1.0f))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3645 | } |
| 3646 | break; |
| 3647 | } |
| 3648 | case GLSLstd450SmoothStep: |
| 3649 | { |
| 3650 | auto edge0 = GenericValue(this, routine, insn.word(5)); |
| 3651 | auto edge1 = GenericValue(this, routine, insn.word(6)); |
| 3652 | auto x = GenericValue(this, routine, insn.word(7)); |
| 3653 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3654 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3655 | auto tx = Min(Max((x.Float(i) - edge0.Float(i)) / |
| 3656 | (edge1.Float(i) - edge0.Float(i)), SIMD::Float(0.0f)), SIMD::Float(1.0f)); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3657 | dst.move(i, tx * tx * (Float4(3.0f) - Float4(2.0f) * tx)); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3658 | } |
| 3659 | break; |
| 3660 | } |
| 3661 | case GLSLstd450FMix: |
| 3662 | { |
| 3663 | auto x = GenericValue(this, routine, insn.word(5)); |
| 3664 | auto y = GenericValue(this, routine, insn.word(6)); |
| 3665 | auto a = GenericValue(this, routine, insn.word(7)); |
| 3666 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3667 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3668 | dst.move(i, a.Float(i) * (y.Float(i) - x.Float(i)) + x.Float(i)); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3669 | } |
| 3670 | break; |
| 3671 | } |
| 3672 | case GLSLstd450FClamp: |
| 3673 | { |
| 3674 | auto x = GenericValue(this, routine, insn.word(5)); |
| 3675 | auto minVal = GenericValue(this, routine, insn.word(6)); |
| 3676 | auto maxVal = GenericValue(this, routine, insn.word(7)); |
| 3677 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3678 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3679 | dst.move(i, Min(Max(x.Float(i), minVal.Float(i)), maxVal.Float(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3680 | } |
| 3681 | break; |
| 3682 | } |
| 3683 | case GLSLstd450SClamp: |
| 3684 | { |
| 3685 | auto x = GenericValue(this, routine, insn.word(5)); |
| 3686 | auto minVal = GenericValue(this, routine, insn.word(6)); |
| 3687 | auto maxVal = GenericValue(this, routine, insn.word(7)); |
| 3688 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3689 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3690 | dst.move(i, Min(Max(x.Int(i), minVal.Int(i)), maxVal.Int(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3691 | } |
| 3692 | break; |
| 3693 | } |
| 3694 | case GLSLstd450UClamp: |
| 3695 | { |
| 3696 | auto x = GenericValue(this, routine, insn.word(5)); |
| 3697 | auto minVal = GenericValue(this, routine, insn.word(6)); |
| 3698 | auto maxVal = GenericValue(this, routine, insn.word(7)); |
| 3699 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3700 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3701 | dst.move(i, Min(Max(x.UInt(i), minVal.UInt(i)), maxVal.UInt(i))); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3702 | } |
| 3703 | break; |
| 3704 | } |
| 3705 | case GLSLstd450FSign: |
| 3706 | { |
| 3707 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3708 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3709 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3710 | auto neg = As<SIMD::Int>(CmpLT(src.Float(i), SIMD::Float(-0.0f))) & As<SIMD::Int>(SIMD::Float(-1.0f)); |
| 3711 | auto pos = As<SIMD::Int>(CmpNLE(src.Float(i), SIMD::Float(+0.0f))) & As<SIMD::Int>(SIMD::Float(1.0f)); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3712 | dst.move(i, neg | pos); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3713 | } |
| 3714 | break; |
| 3715 | } |
| 3716 | case GLSLstd450SSign: |
| 3717 | { |
| 3718 | auto src = GenericValue(this, routine, insn.word(5)); |
| 3719 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3720 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3721 | auto neg = CmpLT(src.Int(i), SIMD::Int(0)) & SIMD::Int(-1); |
| 3722 | auto pos = CmpNLE(src.Int(i), SIMD::Int(0)) & SIMD::Int(1); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3723 | dst.move(i, neg | pos); |
Chris Forbes | db17077 | 2019-03-08 14:50:44 -0800 | [diff] [blame] | 3724 | } |
| 3725 | break; |
| 3726 | } |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3727 | case GLSLstd450Reflect: |
| 3728 | { |
| 3729 | auto I = GenericValue(this, routine, insn.word(5)); |
| 3730 | auto N = GenericValue(this, routine, insn.word(6)); |
| 3731 | |
| 3732 | SIMD::Float d = Dot(type.sizeInComponents, I, N); |
| 3733 | |
| 3734 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3735 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3736 | dst.move(i, I.Float(i) - SIMD::Float(2.0f) * d * N.Float(i)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3737 | } |
| 3738 | break; |
| 3739 | } |
| 3740 | case GLSLstd450Refract: |
| 3741 | { |
| 3742 | auto I = GenericValue(this, routine, insn.word(5)); |
| 3743 | auto N = GenericValue(this, routine, insn.word(6)); |
| 3744 | auto eta = GenericValue(this, routine, insn.word(7)); |
| 3745 | |
| 3746 | SIMD::Float d = Dot(type.sizeInComponents, I, N); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3747 | SIMD::Float k = SIMD::Float(1.0f) - eta.Float(0) * eta.Float(0) * (SIMD::Float(1.0f) - d * d); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3748 | SIMD::Int pos = CmpNLT(k, SIMD::Float(0.0f)); |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3749 | SIMD::Float t = (eta.Float(0) * d + Sqrt(k)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3750 | |
| 3751 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3752 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3753 | dst.move(i, pos & As<SIMD::Int>(eta.Float(0) * I.Float(i) - t * N.Float(i))); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3754 | } |
| 3755 | break; |
| 3756 | } |
| 3757 | case GLSLstd450FaceForward: |
| 3758 | { |
| 3759 | auto N = GenericValue(this, routine, insn.word(5)); |
| 3760 | auto I = GenericValue(this, routine, insn.word(6)); |
| 3761 | auto Nref = GenericValue(this, routine, insn.word(7)); |
| 3762 | |
| 3763 | SIMD::Float d = Dot(type.sizeInComponents, I, Nref); |
| 3764 | SIMD::Int neg = CmpLT(d, SIMD::Float(0.0f)); |
| 3765 | |
| 3766 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3767 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3768 | auto n = N.Float(i); |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3769 | dst.move(i, (neg & As<SIMD::Int>(n)) | (~neg & As<SIMD::Int>(-n))); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3770 | } |
| 3771 | break; |
| 3772 | } |
| 3773 | case GLSLstd450Length: |
| 3774 | { |
| 3775 | auto x = GenericValue(this, routine, insn.word(5)); |
| 3776 | SIMD::Float d = Dot(getType(getObject(insn.word(5)).type).sizeInComponents, x, x); |
| 3777 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3778 | dst.move(0, Sqrt(d)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3779 | break; |
| 3780 | } |
| 3781 | case GLSLstd450Normalize: |
| 3782 | { |
| 3783 | auto x = GenericValue(this, routine, insn.word(5)); |
| 3784 | SIMD::Float d = Dot(getType(getObject(insn.word(5)).type).sizeInComponents, x, x); |
| 3785 | SIMD::Float invLength = SIMD::Float(1.0f) / Sqrt(d); |
| 3786 | |
| 3787 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3788 | { |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3789 | dst.move(i, invLength * x.Float(i)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3790 | } |
| 3791 | break; |
| 3792 | } |
| 3793 | case GLSLstd450Distance: |
| 3794 | { |
| 3795 | auto p0 = GenericValue(this, routine, insn.word(5)); |
| 3796 | auto p1 = GenericValue(this, routine, insn.word(6)); |
Ben Clayton | 2cbf4f9 | 2019-04-08 16:19:30 -0400 | [diff] [blame] | 3797 | auto p0Type = getType(p0.type); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3798 | |
| 3799 | // sqrt(dot(p0-p1, p0-p1)) |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3800 | SIMD::Float d = (p0.Float(0) - p1.Float(0)) * (p0.Float(0) - p1.Float(0)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3801 | |
| 3802 | for (auto i = 1u; i < p0Type.sizeInComponents; i++) |
| 3803 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 3804 | d += (p0.Float(i) - p1.Float(i)) * (p0.Float(i) - p1.Float(i)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3805 | } |
| 3806 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 3807 | dst.move(0, Sqrt(d)); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 3808 | break; |
| 3809 | } |
Ben Clayton | 2cbf4f9 | 2019-04-08 16:19:30 -0400 | [diff] [blame] | 3810 | case GLSLstd450Modf: |
| 3811 | { |
| 3812 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3813 | auto ptrId = Object::ID(insn.word(6)); |
| 3814 | auto ptrTy = getType(getObject(ptrId).type); |
| 3815 | auto ptr = GetPointerToData(ptrId, 0, routine); |
| 3816 | bool interleavedByLane = IsStorageInterleavedByLane(ptrTy.storageClass); |
| 3817 | |
| 3818 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3819 | { |
| 3820 | auto whole = Floor(val.Float(i)); |
| 3821 | auto frac = Frac(val.Float(i)); |
| 3822 | |
| 3823 | dst.move(i, frac); |
| 3824 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 3825 | auto p = ptr + (i * sizeof(float)); |
| 3826 | if (interleavedByLane) { p = interleaveByLane(p); } |
| 3827 | SIMD::Store(p, whole, state->activeLaneMask()); |
Ben Clayton | 2cbf4f9 | 2019-04-08 16:19:30 -0400 | [diff] [blame] | 3828 | } |
| 3829 | break; |
| 3830 | } |
Chris Forbes | fde26fe | 2019-04-08 14:56:52 -0700 | [diff] [blame] | 3831 | case GLSLstd450ModfStruct: |
| 3832 | { |
| 3833 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3834 | auto valTy = getType(val.type); |
| 3835 | |
| 3836 | for (auto i = 0u; i < valTy.sizeInComponents; i++) |
| 3837 | { |
| 3838 | auto whole = Floor(val.Float(i)); |
| 3839 | auto frac = Frac(val.Float(i)); |
| 3840 | |
| 3841 | dst.move(i, frac); |
| 3842 | dst.move(i + valTy.sizeInComponents, whole); |
| 3843 | } |
| 3844 | break; |
| 3845 | } |
Chris Forbes | 50e6493 | 2019-04-08 17:49:27 -0700 | [diff] [blame] | 3846 | case GLSLstd450PackSnorm4x8: |
| 3847 | { |
| 3848 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3849 | dst.move(0, (SIMD::Int(Round(Min(Max(val.Float(0), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 3850 | SIMD::Int(0xFF)) | |
| 3851 | ((SIMD::Int(Round(Min(Max(val.Float(1), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 3852 | SIMD::Int(0xFF)) << 8) | |
| 3853 | ((SIMD::Int(Round(Min(Max(val.Float(2), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 3854 | SIMD::Int(0xFF)) << 16) | |
| 3855 | ((SIMD::Int(Round(Min(Max(val.Float(3), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 3856 | SIMD::Int(0xFF)) << 24)); |
| 3857 | break; |
| 3858 | } |
| 3859 | case GLSLstd450PackUnorm4x8: |
| 3860 | { |
| 3861 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3862 | dst.move(0, (SIMD::UInt(Round(Min(Max(val.Float(0), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) | |
| 3863 | ((SIMD::UInt(Round(Min(Max(val.Float(1), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) << 8) | |
| 3864 | ((SIMD::UInt(Round(Min(Max(val.Float(2), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) << 16) | |
| 3865 | ((SIMD::UInt(Round(Min(Max(val.Float(3), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) << 24)); |
| 3866 | break; |
| 3867 | } |
| 3868 | case GLSLstd450PackSnorm2x16: |
| 3869 | { |
| 3870 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3871 | dst.move(0, (SIMD::Int(Round(Min(Max(val.Float(0), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(32767.0f))) & |
| 3872 | SIMD::Int(0xFFFF)) | |
| 3873 | ((SIMD::Int(Round(Min(Max(val.Float(1), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(32767.0f))) & |
| 3874 | SIMD::Int(0xFFFF)) << 16)); |
| 3875 | break; |
| 3876 | } |
| 3877 | case GLSLstd450PackUnorm2x16: |
| 3878 | { |
| 3879 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3880 | dst.move(0, (SIMD::UInt(Round(Min(Max(val.Float(0), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(65535.0f))) & |
| 3881 | SIMD::UInt(0xFFFF)) | |
| 3882 | ((SIMD::UInt(Round(Min(Max(val.Float(1), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(65535.0f))) & |
| 3883 | SIMD::UInt(0xFFFF)) << 16)); |
| 3884 | break; |
| 3885 | } |
| 3886 | case GLSLstd450PackHalf2x16: |
| 3887 | { |
| 3888 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3889 | dst.move(0, FloatToHalfBits(val.UInt(0), false) | FloatToHalfBits(val.UInt(1), true)); |
| 3890 | break; |
| 3891 | } |
| 3892 | case GLSLstd450UnpackSnorm4x8: |
| 3893 | { |
| 3894 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3895 | dst.move(0, Min(Max(SIMD::Float(((val.Int(0)<<24) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 3896 | dst.move(1, Min(Max(SIMD::Float(((val.Int(0)<<16) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 3897 | dst.move(2, Min(Max(SIMD::Float(((val.Int(0)<<8) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 3898 | dst.move(3, Min(Max(SIMD::Float(((val.Int(0)) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 3899 | break; |
| 3900 | } |
| 3901 | case GLSLstd450UnpackUnorm4x8: |
| 3902 | { |
| 3903 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3904 | dst.move(0, SIMD::Float((val.UInt(0) & SIMD::UInt(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 3905 | dst.move(1, SIMD::Float(((val.UInt(0)>>8) & SIMD::UInt(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 3906 | dst.move(2, SIMD::Float(((val.UInt(0)>>16) & SIMD::UInt(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 3907 | dst.move(3, SIMD::Float(((val.UInt(0)>>24) & SIMD::UInt(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 3908 | break; |
| 3909 | } |
| 3910 | case GLSLstd450UnpackSnorm2x16: |
| 3911 | { |
| 3912 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3913 | // clamp(f / 32767.0, -1.0, 1.0) |
| 3914 | dst.move(0, Min(Max(SIMD::Float(As<SIMD::Int>((val.UInt(0) & SIMD::UInt(0x0000FFFF)) << 16)) * |
| 3915 | SIMD::Float(1.0f / float(0x7FFF0000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 3916 | dst.move(1, Min(Max(SIMD::Float(As<SIMD::Int>(val.UInt(0) & SIMD::UInt(0xFFFF0000))) * SIMD::Float(1.0f / float(0x7FFF0000)), |
| 3917 | SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 3918 | break; |
| 3919 | } |
| 3920 | case GLSLstd450UnpackUnorm2x16: |
| 3921 | { |
| 3922 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3923 | // f / 65535.0 |
| 3924 | dst.move(0, SIMD::Float((val.UInt(0) & SIMD::UInt(0x0000FFFF)) << 16) * SIMD::Float(1.0f / float(0xFFFF0000))); |
| 3925 | dst.move(1, SIMD::Float(val.UInt(0) & SIMD::UInt(0xFFFF0000)) * SIMD::Float(1.0f / float(0xFFFF0000))); |
| 3926 | break; |
| 3927 | } |
| 3928 | case GLSLstd450UnpackHalf2x16: |
| 3929 | { |
| 3930 | auto val = GenericValue(this, routine, insn.word(5)); |
Chris Forbes | d354695 | 2019-04-30 19:32:19 -0700 | [diff] [blame] | 3931 | dst.move(0, halfToFloatBits(val.UInt(0) & SIMD::UInt(0x0000FFFF))); |
| 3932 | dst.move(1, halfToFloatBits((val.UInt(0) & SIMD::UInt(0xFFFF0000)) >> 16)); |
Chris Forbes | 50e6493 | 2019-04-08 17:49:27 -0700 | [diff] [blame] | 3933 | break; |
| 3934 | } |
Ben Clayton | 6caf821 | 2019-04-09 11:28:39 -0400 | [diff] [blame] | 3935 | case GLSLstd450Fma: |
| 3936 | { |
| 3937 | auto a = GenericValue(this, routine, insn.word(5)); |
| 3938 | auto b = GenericValue(this, routine, insn.word(6)); |
| 3939 | auto c = GenericValue(this, routine, insn.word(7)); |
| 3940 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3941 | { |
| 3942 | dst.move(i, FMA(a.Float(i), b.Float(i), c.Float(i))); |
| 3943 | } |
| 3944 | break; |
| 3945 | } |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 3946 | case GLSLstd450Frexp: |
| 3947 | { |
| 3948 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3949 | auto ptrId = Object::ID(insn.word(6)); |
| 3950 | auto ptrTy = getType(getObject(ptrId).type); |
| 3951 | auto ptr = GetPointerToData(ptrId, 0, routine); |
| 3952 | bool interleavedByLane = IsStorageInterleavedByLane(ptrTy.storageClass); |
| 3953 | |
| 3954 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3955 | { |
| 3956 | SIMD::Float significand; |
| 3957 | SIMD::Int exponent; |
| 3958 | std::tie(significand, exponent) = Frexp(val.Float(i)); |
| 3959 | |
| 3960 | dst.move(i, significand); |
| 3961 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 3962 | auto p = ptr + (i * sizeof(float)); |
| 3963 | if (interleavedByLane) { p = interleaveByLane(p); } |
| 3964 | SIMD::Store(p, exponent, state->activeLaneMask()); |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 3965 | } |
| 3966 | break; |
| 3967 | } |
| 3968 | case GLSLstd450FrexpStruct: |
| 3969 | { |
| 3970 | auto val = GenericValue(this, routine, insn.word(5)); |
| 3971 | auto numComponents = getType(val.type).sizeInComponents; |
| 3972 | for (auto i = 0u; i < numComponents; i++) |
| 3973 | { |
| 3974 | auto significandAndExponent = Frexp(val.Float(i)); |
| 3975 | dst.move(i, significandAndExponent.first); |
| 3976 | dst.move(i + numComponents, significandAndExponent.second); |
| 3977 | } |
| 3978 | break; |
| 3979 | } |
Ben Clayton | 20f6ba8 | 2019-04-09 12:07:29 -0400 | [diff] [blame] | 3980 | case GLSLstd450Ldexp: |
| 3981 | { |
| 3982 | auto significand = GenericValue(this, routine, insn.word(5)); |
| 3983 | auto exponent = GenericValue(this, routine, insn.word(6)); |
| 3984 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3985 | { |
| 3986 | // Assumes IEEE 754 |
| 3987 | auto significandExponent = Exponent(significand.Float(i)); |
| 3988 | auto combinedExponent = exponent.Int(i) + significandExponent; |
| 3989 | SIMD::UInt v = (significand.UInt(i) & SIMD::UInt(0x807FFFFF)) | |
| 3990 | (SIMD::UInt(combinedExponent + SIMD::Int(126)) << SIMD::UInt(23)); |
| 3991 | dst.move(i, As<SIMD::Float>(v)); |
| 3992 | } |
| 3993 | break; |
| 3994 | } |
Ben Clayton | a15fcf4 | 2019-04-09 13:04:51 -0400 | [diff] [blame] | 3995 | case GLSLstd450Radians: |
| 3996 | { |
| 3997 | auto degrees = GenericValue(this, routine, insn.word(5)); |
| 3998 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 3999 | { |
| 4000 | dst.move(i, degrees.Float(i) * SIMD::Float(PI / 180.0f)); |
| 4001 | } |
| 4002 | break; |
| 4003 | } |
Ben Clayton | 251bc28 | 2019-04-09 13:05:52 -0400 | [diff] [blame] | 4004 | case GLSLstd450Degrees: |
| 4005 | { |
| 4006 | auto radians = GenericValue(this, routine, insn.word(5)); |
| 4007 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4008 | { |
| 4009 | dst.move(i, radians.Float(i) * SIMD::Float(180.0f / PI)); |
| 4010 | } |
| 4011 | break; |
| 4012 | } |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 4013 | case GLSLstd450Sin: |
| 4014 | { |
| 4015 | auto radians = GenericValue(this, routine, insn.word(5)); |
| 4016 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4017 | { |
| 4018 | dst.move(i, Sin(radians.Float(i))); |
| 4019 | } |
| 4020 | break; |
| 4021 | } |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 4022 | case GLSLstd450Cos: |
| 4023 | { |
| 4024 | auto radians = GenericValue(this, routine, insn.word(5)); |
| 4025 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4026 | { |
| 4027 | dst.move(i, Cos(radians.Float(i))); |
| 4028 | } |
| 4029 | break; |
| 4030 | } |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 4031 | case GLSLstd450Tan: |
| 4032 | { |
| 4033 | auto radians = GenericValue(this, routine, insn.word(5)); |
| 4034 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4035 | { |
| 4036 | dst.move(i, Tan(radians.Float(i))); |
| 4037 | } |
| 4038 | break; |
| 4039 | } |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 4040 | case GLSLstd450Asin: |
| 4041 | { |
| 4042 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4043 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4044 | { |
| 4045 | dst.move(i, Asin(val.Float(i))); |
| 4046 | } |
| 4047 | break; |
| 4048 | } |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 4049 | case GLSLstd450Acos: |
| 4050 | { |
| 4051 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4052 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4053 | { |
| 4054 | dst.move(i, Acos(val.Float(i))); |
| 4055 | } |
| 4056 | break; |
| 4057 | } |
Ben Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 4058 | case GLSLstd450Atan: |
| 4059 | { |
| 4060 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4061 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4062 | { |
| 4063 | dst.move(i, Atan(val.Float(i))); |
| 4064 | } |
| 4065 | break; |
| 4066 | } |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 4067 | case GLSLstd450Sinh: |
| 4068 | { |
| 4069 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4070 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4071 | { |
| 4072 | dst.move(i, Sinh(val.Float(i))); |
| 4073 | } |
| 4074 | break; |
| 4075 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4076 | case GLSLstd450Cosh: |
| 4077 | { |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 4078 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4079 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4080 | { |
| 4081 | dst.move(i, Cosh(val.Float(i))); |
| 4082 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4083 | break; |
| 4084 | } |
| 4085 | case GLSLstd450Tanh: |
| 4086 | { |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 4087 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4088 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4089 | { |
| 4090 | dst.move(i, Tanh(val.Float(i))); |
| 4091 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4092 | break; |
| 4093 | } |
| 4094 | case GLSLstd450Asinh: |
| 4095 | { |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 4096 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4097 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4098 | { |
| 4099 | dst.move(i, Asinh(val.Float(i))); |
| 4100 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4101 | break; |
| 4102 | } |
| 4103 | case GLSLstd450Acosh: |
| 4104 | { |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 4105 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4106 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4107 | { |
| 4108 | dst.move(i, Acosh(val.Float(i))); |
| 4109 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4110 | break; |
| 4111 | } |
| 4112 | case GLSLstd450Atanh: |
| 4113 | { |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 4114 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4115 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4116 | { |
| 4117 | dst.move(i, Atanh(val.Float(i))); |
| 4118 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4119 | break; |
| 4120 | } |
| 4121 | case GLSLstd450Atan2: |
| 4122 | { |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 4123 | auto x = GenericValue(this, routine, insn.word(5)); |
| 4124 | auto y = GenericValue(this, routine, insn.word(6)); |
| 4125 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4126 | { |
| 4127 | dst.move(i, Atan2(x.Float(i), y.Float(i))); |
| 4128 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4129 | break; |
| 4130 | } |
| 4131 | case GLSLstd450Pow: |
| 4132 | { |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 4133 | auto x = GenericValue(this, routine, insn.word(5)); |
| 4134 | auto y = GenericValue(this, routine, insn.word(6)); |
| 4135 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4136 | { |
| 4137 | dst.move(i, Pow(x.Float(i), y.Float(i))); |
| 4138 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4139 | break; |
| 4140 | } |
| 4141 | case GLSLstd450Exp: |
| 4142 | { |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 4143 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4144 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4145 | { |
| 4146 | dst.move(i, Exp(val.Float(i))); |
| 4147 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4148 | break; |
| 4149 | } |
| 4150 | case GLSLstd450Log: |
| 4151 | { |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 4152 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4153 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4154 | { |
| 4155 | dst.move(i, Log(val.Float(i))); |
| 4156 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4157 | break; |
| 4158 | } |
| 4159 | case GLSLstd450Exp2: |
| 4160 | { |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 4161 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4162 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4163 | { |
| 4164 | dst.move(i, Exp2(val.Float(i))); |
| 4165 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4166 | break; |
| 4167 | } |
| 4168 | case GLSLstd450Log2: |
| 4169 | { |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 4170 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4171 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4172 | { |
| 4173 | dst.move(i, Log2(val.Float(i))); |
| 4174 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4175 | break; |
| 4176 | } |
| 4177 | case GLSLstd450Sqrt: |
| 4178 | { |
Ben Clayton | 6517ad2 | 2019-04-09 16:11:40 -0400 | [diff] [blame] | 4179 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4180 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4181 | { |
| 4182 | dst.move(i, Sqrt(val.Float(i))); |
| 4183 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4184 | break; |
| 4185 | } |
| 4186 | case GLSLstd450InverseSqrt: |
| 4187 | { |
Ben Clayton | 9345185 | 2019-04-09 16:25:30 -0400 | [diff] [blame] | 4188 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4189 | Decorations d; |
| 4190 | ApplyDecorationsForId(&d, insn.word(5)); |
| 4191 | if (d.RelaxedPrecision) |
| 4192 | { |
| 4193 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4194 | { |
| 4195 | dst.move(i, RcpSqrt_pp(val.Float(i))); |
| 4196 | } |
| 4197 | } |
| 4198 | else |
| 4199 | { |
| 4200 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4201 | { |
| 4202 | dst.move(i, SIMD::Float(1.0f) / Sqrt(val.Float(i))); |
| 4203 | } |
| 4204 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4205 | break; |
| 4206 | } |
| 4207 | case GLSLstd450Determinant: |
| 4208 | { |
Ben Clayton | 1fb633c | 2019-04-09 17:24:59 -0400 | [diff] [blame] | 4209 | auto mat = GenericValue(this, routine, insn.word(5)); |
| 4210 | auto numComponents = getType(mat.type).sizeInComponents; |
| 4211 | switch (numComponents) |
| 4212 | { |
| 4213 | case 4: // 2x2 |
| 4214 | dst.move(0, Determinant( |
| 4215 | mat.Float(0), mat.Float(1), |
| 4216 | mat.Float(2), mat.Float(3))); |
| 4217 | break; |
| 4218 | case 9: // 3x3 |
| 4219 | dst.move(0, Determinant( |
| 4220 | mat.Float(0), mat.Float(1), mat.Float(2), |
| 4221 | mat.Float(3), mat.Float(4), mat.Float(5), |
| 4222 | mat.Float(6), mat.Float(7), mat.Float(8))); |
| 4223 | break; |
| 4224 | case 16: // 4x4 |
| 4225 | dst.move(0, Determinant( |
| 4226 | mat.Float(0), mat.Float(1), mat.Float(2), mat.Float(3), |
| 4227 | mat.Float(4), mat.Float(5), mat.Float(6), mat.Float(7), |
| 4228 | mat.Float(8), mat.Float(9), mat.Float(10), mat.Float(11), |
| 4229 | mat.Float(12), mat.Float(13), mat.Float(14), mat.Float(15))); |
| 4230 | break; |
| 4231 | default: |
| 4232 | UNREACHABLE("GLSLstd450Determinant can only operate with square matrices. Got %d elements", int(numComponents)); |
| 4233 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4234 | break; |
| 4235 | } |
| 4236 | case GLSLstd450MatrixInverse: |
| 4237 | { |
Ben Clayton | 445a44a | 2019-04-10 16:37:19 -0400 | [diff] [blame] | 4238 | auto mat = GenericValue(this, routine, insn.word(5)); |
| 4239 | auto numComponents = getType(mat.type).sizeInComponents; |
| 4240 | switch (numComponents) |
| 4241 | { |
| 4242 | case 4: // 2x2 |
| 4243 | { |
| 4244 | auto inv = MatrixInverse( |
| 4245 | mat.Float(0), mat.Float(1), |
| 4246 | mat.Float(2), mat.Float(3)); |
| 4247 | for (uint32_t i = 0; i < inv.size(); i++) |
| 4248 | { |
| 4249 | dst.move(i, inv[i]); |
| 4250 | } |
| 4251 | break; |
| 4252 | } |
| 4253 | case 9: // 3x3 |
| 4254 | { |
| 4255 | auto inv = MatrixInverse( |
| 4256 | mat.Float(0), mat.Float(1), mat.Float(2), |
| 4257 | mat.Float(3), mat.Float(4), mat.Float(5), |
| 4258 | mat.Float(6), mat.Float(7), mat.Float(8)); |
| 4259 | for (uint32_t i = 0; i < inv.size(); i++) |
| 4260 | { |
| 4261 | dst.move(i, inv[i]); |
| 4262 | } |
| 4263 | break; |
| 4264 | } |
| 4265 | case 16: // 4x4 |
| 4266 | { |
| 4267 | auto inv = MatrixInverse( |
| 4268 | mat.Float(0), mat.Float(1), mat.Float(2), mat.Float(3), |
| 4269 | mat.Float(4), mat.Float(5), mat.Float(6), mat.Float(7), |
| 4270 | mat.Float(8), mat.Float(9), mat.Float(10), mat.Float(11), |
| 4271 | mat.Float(12), mat.Float(13), mat.Float(14), mat.Float(15)); |
| 4272 | for (uint32_t i = 0; i < inv.size(); i++) |
| 4273 | { |
| 4274 | dst.move(i, inv[i]); |
| 4275 | } |
| 4276 | break; |
| 4277 | } |
| 4278 | default: |
| 4279 | UNREACHABLE("GLSLstd450MatrixInverse can only operate with square matrices. Got %d elements", int(numComponents)); |
| 4280 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4281 | break; |
| 4282 | } |
| 4283 | case GLSLstd450IMix: |
| 4284 | { |
Ben Clayton | 238fec3 | 2019-04-09 16:27:56 -0400 | [diff] [blame] | 4285 | UNREACHABLE("GLSLstd450IMix has been removed from the specification"); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4286 | break; |
| 4287 | } |
| 4288 | case GLSLstd450PackDouble2x32: |
| 4289 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4290 | UNSUPPORTED("SPIR-V Float64 Capability (GLSLstd450PackDouble2x32)"); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4291 | break; |
| 4292 | } |
| 4293 | case GLSLstd450UnpackDouble2x32: |
| 4294 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4295 | UNSUPPORTED("SPIR-V Float64 Capability (GLSLstd450UnpackDouble2x32)"); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4296 | break; |
| 4297 | } |
| 4298 | case GLSLstd450FindILsb: |
| 4299 | { |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 4300 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4301 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4302 | { |
| 4303 | auto v = val.UInt(i); |
| 4304 | dst.move(i, Cttz(v, true) | CmpEQ(v, SIMD::UInt(0))); |
| 4305 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4306 | break; |
| 4307 | } |
| 4308 | case GLSLstd450FindSMsb: |
| 4309 | { |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 4310 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4311 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4312 | { |
| 4313 | auto v = val.UInt(i) ^ As<SIMD::UInt>(CmpLT(val.Int(i), SIMD::Int(0))); |
| 4314 | dst.move(i, SIMD::UInt(31) - Ctlz(v, false)); |
| 4315 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4316 | break; |
| 4317 | } |
| 4318 | case GLSLstd450FindUMsb: |
| 4319 | { |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 4320 | auto val = GenericValue(this, routine, insn.word(5)); |
| 4321 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4322 | { |
| 4323 | dst.move(i, SIMD::UInt(31) - Ctlz(val.UInt(i), false)); |
| 4324 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4325 | break; |
| 4326 | } |
| 4327 | case GLSLstd450InterpolateAtCentroid: |
| 4328 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4329 | UNSUPPORTED("SPIR-V SampleRateShading Capability (GLSLstd450InterpolateAtCentroid)"); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4330 | break; |
| 4331 | } |
| 4332 | case GLSLstd450InterpolateAtSample: |
| 4333 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4334 | UNSUPPORTED("SPIR-V SampleRateShading Capability (GLSLstd450InterpolateAtCentroid)"); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4335 | break; |
| 4336 | } |
| 4337 | case GLSLstd450InterpolateAtOffset: |
| 4338 | { |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4339 | UNSUPPORTED("SPIR-V SampleRateShading Capability (GLSLstd450InterpolateAtCentroid)"); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4340 | break; |
| 4341 | } |
| 4342 | case GLSLstd450NMin: |
| 4343 | { |
Ben Clayton | ee10bcf | 2019-04-09 17:01:01 -0400 | [diff] [blame] | 4344 | auto x = GenericValue(this, routine, insn.word(5)); |
| 4345 | auto y = GenericValue(this, routine, insn.word(6)); |
| 4346 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4347 | { |
| 4348 | dst.move(i, NMin(x.Float(i), y.Float(i))); |
| 4349 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4350 | break; |
| 4351 | } |
| 4352 | case GLSLstd450NMax: |
| 4353 | { |
Ben Clayton | 02de7e0 | 2019-04-09 17:01:26 -0400 | [diff] [blame] | 4354 | auto x = GenericValue(this, routine, insn.word(5)); |
| 4355 | auto y = GenericValue(this, routine, insn.word(6)); |
| 4356 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4357 | { |
| 4358 | dst.move(i, NMax(x.Float(i), y.Float(i))); |
| 4359 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4360 | break; |
| 4361 | } |
| 4362 | case GLSLstd450NClamp: |
| 4363 | { |
Ben Clayton | 4d63312 | 2019-04-09 17:02:34 -0400 | [diff] [blame] | 4364 | auto x = GenericValue(this, routine, insn.word(5)); |
| 4365 | auto minVal = GenericValue(this, routine, insn.word(6)); |
| 4366 | auto maxVal = GenericValue(this, routine, insn.word(7)); |
| 4367 | for (auto i = 0u; i < type.sizeInComponents; i++) |
| 4368 | { |
| 4369 | auto clamp = NMin(NMax(x.Float(i), minVal.Float(i)), maxVal.Float(i)); |
| 4370 | dst.move(i, clamp); |
| 4371 | } |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4372 | break; |
| 4373 | } |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 4374 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4375 | UNREACHABLE("ExtInst %d", int(extInstIndex)); |
Ben Clayton | 91fd0e2 | 2019-04-09 15:19:39 -0400 | [diff] [blame] | 4376 | break; |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 4377 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4378 | |
| 4379 | return EmitResult::Continue; |
Chris Forbes | 9667a5b | 2019-03-07 09:26:48 -0800 | [diff] [blame] | 4380 | } |
| 4381 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 4382 | std::memory_order SpirvShader::MemoryOrder(spv::MemorySemanticsMask memorySemantics) |
| 4383 | { |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 4384 | auto control = static_cast<uint32_t>(memorySemantics) & static_cast<uint32_t>( |
| 4385 | spv::MemorySemanticsAcquireMask | |
| 4386 | spv::MemorySemanticsReleaseMask | |
| 4387 | spv::MemorySemanticsAcquireReleaseMask | |
| 4388 | spv::MemorySemanticsSequentiallyConsistentMask |
| 4389 | ); |
| 4390 | switch (control) |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 4391 | { |
| 4392 | case spv::MemorySemanticsMaskNone: return std::memory_order_relaxed; |
| 4393 | case spv::MemorySemanticsAcquireMask: return std::memory_order_acquire; |
| 4394 | case spv::MemorySemanticsReleaseMask: return std::memory_order_release; |
| 4395 | case spv::MemorySemanticsAcquireReleaseMask: return std::memory_order_acq_rel; |
| 4396 | case spv::MemorySemanticsSequentiallyConsistentMask: return std::memory_order_acq_rel; // Vulkan 1.1: "SequentiallyConsistent is treated as AcquireRelease" |
| 4397 | default: |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 4398 | // "it is invalid for more than one of these four bits to be set: |
| 4399 | // Acquire, Release, AcquireRelease, or SequentiallyConsistent." |
| 4400 | UNREACHABLE("MemorySemanticsMask: %x", int(control)); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 4401 | return std::memory_order_acq_rel; |
| 4402 | } |
| 4403 | } |
| 4404 | |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 4405 | SIMD::Float SpirvShader::Dot(unsigned numComponents, GenericValue const & x, GenericValue const & y) const |
| 4406 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 4407 | SIMD::Float d = x.Float(0) * y.Float(0); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 4408 | |
| 4409 | for (auto i = 1u; i < numComponents; i++) |
| 4410 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 4411 | d += x.Float(i) * y.Float(i); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | return d; |
| 4415 | } |
| 4416 | |
Chris Forbes | 50e6493 | 2019-04-08 17:49:27 -0700 | [diff] [blame] | 4417 | SIMD::UInt SpirvShader::FloatToHalfBits(SIMD::UInt floatBits, bool storeInUpperBits) const |
| 4418 | { |
| 4419 | static const uint32_t mask_sign = 0x80000000u; |
| 4420 | static const uint32_t mask_round = ~0xfffu; |
| 4421 | static const uint32_t c_f32infty = 255 << 23; |
| 4422 | static const uint32_t c_magic = 15 << 23; |
| 4423 | static const uint32_t c_nanbit = 0x200; |
| 4424 | static const uint32_t c_infty_as_fp16 = 0x7c00; |
| 4425 | static const uint32_t c_clamp = (31 << 23) - 0x1000; |
| 4426 | |
| 4427 | SIMD::UInt justsign = SIMD::UInt(mask_sign) & floatBits; |
| 4428 | SIMD::UInt absf = floatBits ^ justsign; |
| 4429 | SIMD::UInt b_isnormal = CmpNLE(SIMD::UInt(c_f32infty), absf); |
| 4430 | |
| 4431 | // Note: this version doesn't round to the nearest even in case of a tie as defined by IEEE 754-2008, it rounds to +inf |
| 4432 | // instead of nearest even, since that's fine for GLSL ES 3.0's needs (see section 2.1.1 Floating-Point Computation) |
| 4433 | SIMD::UInt joined = ((((As<SIMD::UInt>(Min(As<SIMD::Float>(absf & SIMD::UInt(mask_round)) * As<SIMD::Float>(SIMD::UInt(c_magic)), |
| 4434 | As<SIMD::Float>(SIMD::UInt(c_clamp))))) - SIMD::UInt(mask_round)) >> 13) & b_isnormal) | |
| 4435 | ((b_isnormal ^ SIMD::UInt(0xFFFFFFFF)) & ((CmpNLE(absf, SIMD::UInt(c_f32infty)) & SIMD::UInt(c_nanbit)) | |
| 4436 | SIMD::UInt(c_infty_as_fp16))); |
| 4437 | |
| 4438 | return storeInUpperBits ? ((joined << 16) | justsign) : joined | (justsign >> 16); |
| 4439 | } |
| 4440 | |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 4441 | std::pair<SIMD::Float, SIMD::Int> SpirvShader::Frexp(RValue<SIMD::Float> val) const |
| 4442 | { |
| 4443 | // Assumes IEEE 754 |
| 4444 | auto v = As<SIMD::UInt>(val); |
| 4445 | auto isNotZero = CmpNEQ(v & SIMD::UInt(0x7FFFFFFF), SIMD::UInt(0)); |
| 4446 | auto zeroSign = v & SIMD::UInt(0x80000000) & ~isNotZero; |
Ben Clayton | f41ca6b | 2019-04-10 22:33:00 +0100 | [diff] [blame] | 4447 | auto significand = As<SIMD::Float>((((v & SIMD::UInt(0x807FFFFF)) | SIMD::UInt(0x3F000000)) & isNotZero) | zeroSign); |
Ben Clayton | 20f6ba8 | 2019-04-09 12:07:29 -0400 | [diff] [blame] | 4448 | auto exponent = Exponent(val) & SIMD::Int(isNotZero); |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 4449 | return std::make_pair(significand, exponent); |
| 4450 | } |
| 4451 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4452 | SpirvShader::EmitResult SpirvShader::EmitAny(InsnIterator insn, EmitState *state) const |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4453 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4454 | auto routine = state->routine; |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4455 | auto &type = getType(insn.word(1)); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 4456 | ASSERT(type.sizeInComponents == 1); |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4457 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 4458 | auto &srcType = getType(getObject(insn.word(3)).type); |
| 4459 | auto src = GenericValue(this, routine, insn.word(3)); |
| 4460 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 4461 | SIMD::UInt result = src.UInt(0); |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4462 | |
| 4463 | for (auto i = 1u; i < srcType.sizeInComponents; i++) |
| 4464 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 4465 | result |= src.UInt(i); |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4466 | } |
| 4467 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 4468 | dst.move(0, result); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4469 | return EmitResult::Continue; |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4470 | } |
| 4471 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4472 | SpirvShader::EmitResult SpirvShader::EmitAll(InsnIterator insn, EmitState *state) const |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4473 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4474 | auto routine = state->routine; |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4475 | auto &type = getType(insn.word(1)); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 4476 | ASSERT(type.sizeInComponents == 1); |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4477 | auto &dst = routine->createIntermediate(insn.word(2), type.sizeInComponents); |
| 4478 | auto &srcType = getType(getObject(insn.word(3)).type); |
| 4479 | auto src = GenericValue(this, routine, insn.word(3)); |
| 4480 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 4481 | SIMD::UInt result = src.UInt(0); |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4482 | |
| 4483 | for (auto i = 1u; i < srcType.sizeInComponents; i++) |
| 4484 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 4485 | result &= src.UInt(i); |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4486 | } |
| 4487 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 4488 | dst.move(0, result); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4489 | return EmitResult::Continue; |
Chris Forbes | 0785f69 | 2019-03-08 09:09:18 -0800 | [diff] [blame] | 4490 | } |
| 4491 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4492 | SpirvShader::EmitResult SpirvShader::EmitBranch(InsnIterator insn, EmitState *state) const |
Ben Clayton | e37ce61 | 2019-03-13 19:57:42 +0000 | [diff] [blame] | 4493 | { |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 4494 | auto target = Block::ID(insn.word(1)); |
| 4495 | auto edge = Block::Edge{state->currentBlock, target}; |
| 4496 | state->edgeActiveLaneMasks.emplace(edge, state->activeLaneMask()); |
| 4497 | return EmitResult::Terminator; |
Ben Clayton | e37ce61 | 2019-03-13 19:57:42 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4500 | SpirvShader::EmitResult SpirvShader::EmitBranchConditional(InsnIterator insn, EmitState *state) const |
| 4501 | { |
| 4502 | auto block = getBlock(state->currentBlock); |
| 4503 | ASSERT(block.branchInstruction == insn); |
| 4504 | |
| 4505 | auto condId = Object::ID(block.branchInstruction.word(1)); |
| 4506 | auto trueBlockId = Block::ID(block.branchInstruction.word(2)); |
| 4507 | auto falseBlockId = Block::ID(block.branchInstruction.word(3)); |
| 4508 | |
| 4509 | auto cond = GenericValue(this, state->routine, condId); |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 4510 | ASSERT_MSG(getType(cond.type).sizeInComponents == 1, "Condition must be a Boolean type scalar"); |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4511 | |
| 4512 | // TODO: Optimize for case where all lanes take same path. |
| 4513 | |
| 4514 | state->addOutputActiveLaneMaskEdge(trueBlockId, cond.Int(0)); |
| 4515 | state->addOutputActiveLaneMaskEdge(falseBlockId, ~cond.Int(0)); |
| 4516 | |
| 4517 | return EmitResult::Terminator; |
| 4518 | } |
| 4519 | |
Ben Clayton | 213a8ce | 2019-03-21 18:57:23 +0000 | [diff] [blame] | 4520 | SpirvShader::EmitResult SpirvShader::EmitSwitch(InsnIterator insn, EmitState *state) const |
| 4521 | { |
| 4522 | auto block = getBlock(state->currentBlock); |
| 4523 | ASSERT(block.branchInstruction == insn); |
| 4524 | |
| 4525 | auto selId = Object::ID(block.branchInstruction.word(1)); |
| 4526 | |
| 4527 | auto sel = GenericValue(this, state->routine, selId); |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 4528 | ASSERT_MSG(getType(sel.type).sizeInComponents == 1, "Selector must be a scalar"); |
Ben Clayton | 213a8ce | 2019-03-21 18:57:23 +0000 | [diff] [blame] | 4529 | |
| 4530 | auto numCases = (block.branchInstruction.wordCount() - 3) / 2; |
| 4531 | |
| 4532 | // TODO: Optimize for case where all lanes take same path. |
| 4533 | |
| 4534 | SIMD::Int defaultLaneMask = state->activeLaneMask(); |
| 4535 | |
| 4536 | // Gather up the case label matches and calculate defaultLaneMask. |
| 4537 | std::vector<RValue<SIMD::Int>> caseLabelMatches; |
| 4538 | caseLabelMatches.reserve(numCases); |
| 4539 | for (uint32_t i = 0; i < numCases; i++) |
| 4540 | { |
| 4541 | auto label = block.branchInstruction.word(i * 2 + 3); |
| 4542 | auto caseBlockId = Block::ID(block.branchInstruction.word(i * 2 + 4)); |
| 4543 | auto caseLabelMatch = CmpEQ(sel.Int(0), SIMD::Int(label)); |
| 4544 | state->addOutputActiveLaneMaskEdge(caseBlockId, caseLabelMatch); |
| 4545 | defaultLaneMask &= ~caseLabelMatch; |
| 4546 | } |
| 4547 | |
| 4548 | auto defaultBlockId = Block::ID(block.branchInstruction.word(2)); |
| 4549 | state->addOutputActiveLaneMaskEdge(defaultBlockId, defaultLaneMask); |
| 4550 | |
| 4551 | return EmitResult::Terminator; |
| 4552 | } |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4553 | |
| 4554 | SpirvShader::EmitResult SpirvShader::EmitUnreachable(InsnIterator insn, EmitState *state) const |
| 4555 | { |
| 4556 | // TODO: Log something in this case? |
| 4557 | state->setActiveLaneMask(SIMD::Int(0)); |
| 4558 | return EmitResult::Terminator; |
| 4559 | } |
| 4560 | |
| 4561 | SpirvShader::EmitResult SpirvShader::EmitReturn(InsnIterator insn, EmitState *state) const |
| 4562 | { |
| 4563 | state->setActiveLaneMask(SIMD::Int(0)); |
| 4564 | return EmitResult::Terminator; |
| 4565 | } |
| 4566 | |
Chris Forbes | 97e9589 | 2019-04-02 13:37:37 +1300 | [diff] [blame] | 4567 | SpirvShader::EmitResult SpirvShader::EmitKill(InsnIterator insn, EmitState *state) const |
| 4568 | { |
| 4569 | state->routine->killMask |= SignMask(state->activeLaneMask()); |
| 4570 | state->setActiveLaneMask(SIMD::Int(0)); |
| 4571 | return EmitResult::Terminator; |
| 4572 | } |
| 4573 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4574 | SpirvShader::EmitResult SpirvShader::EmitPhi(InsnIterator insn, EmitState *state) const |
| 4575 | { |
| 4576 | auto routine = state->routine; |
| 4577 | auto typeId = Type::ID(insn.word(1)); |
| 4578 | auto type = getType(typeId); |
| 4579 | auto objectId = Object::ID(insn.word(2)); |
Ben Clayton | 5d143aa | 2019-04-03 13:30:14 +0100 | [diff] [blame] | 4580 | auto currentBlock = getBlock(state->currentBlock); |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4581 | |
Nicolas Capens | 459453a | 2019-03-27 15:27:27 -0400 | [diff] [blame] | 4582 | auto tmp = std::unique_ptr<SIMD::Int[]>(new SIMD::Int[type.sizeInComponents]); |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4583 | |
| 4584 | bool first = true; |
| 4585 | for (uint32_t w = 3; w < insn.wordCount(); w += 2) |
| 4586 | { |
| 4587 | auto varId = Object::ID(insn.word(w + 0)); |
| 4588 | auto blockId = Block::ID(insn.word(w + 1)); |
| 4589 | |
Ben Clayton | 5d143aa | 2019-04-03 13:30:14 +0100 | [diff] [blame] | 4590 | if (currentBlock.ins.count(blockId) == 0) |
| 4591 | { |
| 4592 | continue; // In is unreachable. Ignore. |
| 4593 | } |
| 4594 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4595 | auto in = GenericValue(this, routine, varId); |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 4596 | auto mask = GetActiveLaneMaskEdge(state, blockId, state->currentBlock); |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4597 | |
| 4598 | for (uint32_t i = 0; i < type.sizeInComponents; i++) |
| 4599 | { |
| 4600 | auto inMasked = in.Int(i) & mask; |
Nicolas Capens | 459453a | 2019-03-27 15:27:27 -0400 | [diff] [blame] | 4601 | tmp[i] = first ? inMasked : (tmp[i] | inMasked); |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4602 | } |
| 4603 | first = false; |
| 4604 | } |
| 4605 | |
Nicolas Capens | 459453a | 2019-03-27 15:27:27 -0400 | [diff] [blame] | 4606 | auto &dst = routine->createIntermediate(objectId, type.sizeInComponents); |
| 4607 | for(uint32_t i = 0; i < type.sizeInComponents; i++) |
| 4608 | { |
| 4609 | dst.move(i, tmp[i]); |
| 4610 | } |
| 4611 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 4612 | return EmitResult::Continue; |
| 4613 | } |
| 4614 | |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4615 | SpirvShader::EmitResult SpirvShader::EmitImageSampleImplicitLod(Variant variant, InsnIterator insn, EmitState *state) const |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4616 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4617 | return EmitImageSample({variant, Implicit}, insn, state); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4618 | } |
| 4619 | |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4620 | SpirvShader::EmitResult SpirvShader::EmitImageSampleExplicitLod(Variant variant, InsnIterator insn, EmitState *state) const |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4621 | { |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4622 | uint32_t imageOperands = static_cast<spv::ImageOperandsMask>(insn.word(5)); |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4623 | imageOperands &= ~spv::ImageOperandsConstOffsetMask; // Dealt with later. |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 4624 | |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4625 | if((imageOperands & spv::ImageOperandsLodMask) == imageOperands) |
| 4626 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4627 | return EmitImageSample({variant, Lod}, insn, state); |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4628 | } |
| 4629 | else if((imageOperands & spv::ImageOperandsGradMask) == imageOperands) |
| 4630 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4631 | return EmitImageSample({variant, Grad}, insn, state); |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4632 | } |
| 4633 | else UNIMPLEMENTED("Image Operands %x", imageOperands); |
| 4634 | return EmitResult::Continue; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4635 | } |
| 4636 | |
Chris Forbes | cd63159 | 2019-04-27 10:37:18 -0700 | [diff] [blame] | 4637 | SpirvShader::EmitResult SpirvShader::EmitImageFetch(InsnIterator insn, EmitState *state) const |
| 4638 | { |
Chris Forbes | 6f1b765 | 2019-04-30 13:01:47 -0700 | [diff] [blame] | 4639 | return EmitImageSample({None, Fetch}, insn, state); |
Chris Forbes | cd63159 | 2019-04-27 10:37:18 -0700 | [diff] [blame] | 4640 | } |
| 4641 | |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 4642 | SpirvShader::EmitResult SpirvShader::EmitImageSample(ImageInstruction instruction, InsnIterator insn, EmitState *state) const |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4643 | { |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4644 | Type::ID resultTypeId = insn.word(1); |
| 4645 | Object::ID resultId = insn.word(2); |
| 4646 | Object::ID sampledImageId = insn.word(3); |
| 4647 | Object::ID coordinateId = insn.word(4); |
| 4648 | auto &resultType = getType(resultTypeId); |
| 4649 | |
| 4650 | auto &result = state->routine->createIntermediate(resultId, resultType.sizeInComponents); |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 4651 | auto imageDescriptor = state->routine->getPointer(sampledImageId).base; // vk::SampledImageDescriptor* |
| 4652 | |
| 4653 | // If using a separate sampler, look through the OpSampledImage instruction to find the sampler descriptor |
| 4654 | auto &sampledImage = getObject(sampledImageId); |
| 4655 | auto samplerDescriptor = (sampledImage.opcode() == spv::OpSampledImage) ? |
| 4656 | state->routine->getPointer(sampledImage.definition.word(4)).base : imageDescriptor; |
| 4657 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4658 | auto coordinate = GenericValue(this, state->routine, coordinateId); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4659 | auto &coordinateType = getType(coordinate.type); |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4660 | |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 4661 | Pointer<Byte> sampler = samplerDescriptor + OFFSET(vk::SampledImageDescriptor, sampler); // vk::Sampler* |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 4662 | Pointer<Byte> texture = imageDescriptor + OFFSET(vk::SampledImageDescriptor, texture); // sw::Texture* |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4663 | |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4664 | uint32_t imageOperands = spv::ImageOperandsMaskNone; |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4665 | bool lodOrBias = false; |
| 4666 | Object::ID lodOrBiasId = 0; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4667 | bool grad = false; |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4668 | Object::ID gradDxId = 0; |
| 4669 | Object::ID gradDyId = 0; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4670 | bool constOffset = false; |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 4671 | Object::ID offsetId = 0; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4672 | bool sample = false; |
| 4673 | |
Chris Forbes | c71c17f | 2019-05-04 10:01:04 -0700 | [diff] [blame] | 4674 | uint32_t operand = instruction.isDref() ? 6 : 5; |
| 4675 | |
| 4676 | if(insn.wordCount() > operand) |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4677 | { |
Chris Forbes | c71c17f | 2019-05-04 10:01:04 -0700 | [diff] [blame] | 4678 | imageOperands = static_cast<spv::ImageOperandsMask>(insn.word(operand++)); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4679 | |
| 4680 | if(imageOperands & spv::ImageOperandsBiasMask) |
| 4681 | { |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4682 | lodOrBias = true; |
| 4683 | lodOrBiasId = insn.word(operand); |
| 4684 | operand++; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4685 | imageOperands &= ~spv::ImageOperandsBiasMask; |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4686 | |
| 4687 | ASSERT(instruction.samplerMethod == Implicit); |
| 4688 | instruction.samplerMethod = Bias; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | if(imageOperands & spv::ImageOperandsLodMask) |
| 4692 | { |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4693 | lodOrBias = true; |
| 4694 | lodOrBiasId = insn.word(operand); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4695 | operand++; |
| 4696 | imageOperands &= ~spv::ImageOperandsLodMask; |
| 4697 | } |
| 4698 | |
| 4699 | if(imageOperands & spv::ImageOperandsGradMask) |
| 4700 | { |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4701 | ASSERT(!lodOrBias); // SPIR-V 1.3: "It is invalid to set both the Lod and Grad bits." Bias is for ImplicitLod, Grad for ExplicitLod. |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4702 | grad = true; |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4703 | gradDxId = insn.word(operand + 0); |
| 4704 | gradDyId = insn.word(operand + 1); |
| 4705 | operand += 2; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4706 | imageOperands &= ~spv::ImageOperandsGradMask; |
| 4707 | } |
| 4708 | |
| 4709 | if(imageOperands & spv::ImageOperandsConstOffsetMask) |
| 4710 | { |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4711 | constOffset = true; |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 4712 | offsetId = insn.word(operand); |
| 4713 | operand++; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4714 | imageOperands &= ~spv::ImageOperandsConstOffsetMask; |
| 4715 | } |
| 4716 | |
| 4717 | if(imageOperands & spv::ImageOperandsSampleMask) |
| 4718 | { |
| 4719 | UNIMPLEMENTED("Image operand %x", spv::ImageOperandsSampleMask); (void)sample; |
| 4720 | sample = true; |
| 4721 | imageOperands &= ~spv::ImageOperandsSampleMask; |
| 4722 | } |
| 4723 | |
| 4724 | if(imageOperands != 0) |
| 4725 | { |
| 4726 | UNIMPLEMENTED("Image operand %x", imageOperands); |
| 4727 | } |
| 4728 | } |
| 4729 | |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4730 | Array<SIMD::Float> in(16); // Maximum 16 input parameter components. |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4731 | |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4732 | uint32_t coordinates = coordinateType.sizeInComponents - instruction.isProj(); |
| 4733 | instruction.coordinates = coordinates; |
| 4734 | |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4735 | uint32_t i = 0; |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4736 | for( ; i < coordinates; i++) |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4737 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 4738 | if(instruction.isProj()) |
| 4739 | { |
| 4740 | in[i] = coordinate.Float(i) / coordinate.Float(coordinates); // TODO(b/129523279): Optimize using reciprocal. |
| 4741 | } |
| 4742 | else |
| 4743 | { |
| 4744 | in[i] = coordinate.Float(i); |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | if(instruction.isDref()) |
| 4749 | { |
Chris Forbes | c71c17f | 2019-05-04 10:01:04 -0700 | [diff] [blame] | 4750 | auto drefValue = GenericValue(this, state->routine, insn.word(5)); |
| 4751 | in[i] = drefValue.Float(0); |
| 4752 | i++; |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4753 | } |
| 4754 | |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4755 | if(lodOrBias) |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4756 | { |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 4757 | auto lodValue = GenericValue(this, state->routine, lodOrBiasId); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 4758 | in[i] = lodValue.Float(0); |
| 4759 | i++; |
| 4760 | } |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4761 | else if(grad) |
| 4762 | { |
| 4763 | auto dxValue = GenericValue(this, state->routine, gradDxId); |
| 4764 | auto dyValue = GenericValue(this, state->routine, gradDyId); |
| 4765 | auto &dxyType = getType(dxValue.type); |
| 4766 | ASSERT(dxyType.sizeInComponents == getType(dyValue.type).sizeInComponents); |
| 4767 | |
| 4768 | instruction.gradComponents = dxyType.sizeInComponents; |
| 4769 | |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 4770 | for(uint32_t j = 0; j < dxyType.sizeInComponents; j++, i++) |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4771 | { |
| 4772 | in[i] = dxValue.Float(j); |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4773 | } |
| 4774 | |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 4775 | for(uint32_t j = 0; j < dxyType.sizeInComponents; j++, i++) |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4776 | { |
| 4777 | in[i] = dyValue.Float(j); |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | if(constOffset) |
| 4782 | { |
| 4783 | auto offsetValue = GenericValue(this, state->routine, offsetId); |
| 4784 | auto &offsetType = getType(offsetValue.type); |
| 4785 | |
| 4786 | instruction.samplerOption = Offset; |
| 4787 | instruction.offsetComponents = offsetType.sizeInComponents; |
| 4788 | |
| 4789 | for(uint32_t j = 0; j < offsetType.sizeInComponents; j++, i++) |
| 4790 | { |
| 4791 | in[i] = offsetValue.Float(j); // Integer values, but transfered as float. |
Nicolas Capens | 420d9da | 2019-04-26 17:44:42 -0400 | [diff] [blame] | 4792 | } |
| 4793 | } |
| 4794 | |
Chris Forbes | 45f9a93 | 2019-05-08 13:30:38 -0700 | [diff] [blame] | 4795 | auto samplerFunc = Call(getImageSampler, instruction.parameters, imageDescriptor, sampler); |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4796 | |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 4797 | Array<SIMD::Float> out(4); |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 4798 | Call<ImageSampler>(samplerFunc, texture, sampler, &in[0], &out[0], state->routine->constants); |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4799 | |
Chris Forbes | c71c17f | 2019-05-04 10:01:04 -0700 | [diff] [blame] | 4800 | for (auto i = 0u; i < resultType.sizeInComponents; i++) { result.move(i, out[i]); } |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 4801 | |
| 4802 | return EmitResult::Continue; |
| 4803 | } |
| 4804 | |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4805 | SpirvShader::EmitResult SpirvShader::EmitImageQuerySize(InsnIterator insn, EmitState *state) const |
| 4806 | { |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4807 | auto &resultTy = getType(Type::ID(insn.word(1))); |
| 4808 | auto resultId = Object::ID(insn.word(2)); |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4809 | auto imageId = Object::ID(insn.word(3)); |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4810 | auto lodId = Object::ID(0); |
| 4811 | |
| 4812 | auto &dst = state->routine->createIntermediate(resultId, resultTy.sizeInComponents); |
| 4813 | GetImageDimensions(state->routine, resultTy, imageId, lodId, dst); |
| 4814 | |
| 4815 | return EmitResult::Continue; |
| 4816 | } |
| 4817 | |
| 4818 | SpirvShader::EmitResult SpirvShader::EmitImageQuerySizeLod(InsnIterator insn, EmitState *state) const |
| 4819 | { |
| 4820 | auto &resultTy = getType(Type::ID(insn.word(1))); |
| 4821 | auto resultId = Object::ID(insn.word(2)); |
| 4822 | auto imageId = Object::ID(insn.word(3)); |
| 4823 | auto lodId = Object::ID(insn.word(4)); |
| 4824 | |
| 4825 | auto &dst = state->routine->createIntermediate(resultId, resultTy.sizeInComponents); |
| 4826 | GetImageDimensions(state->routine, resultTy, imageId, lodId, dst); |
| 4827 | |
| 4828 | return EmitResult::Continue; |
| 4829 | } |
| 4830 | |
| 4831 | void SpirvShader::GetImageDimensions(SpirvRoutine const *routine, Type const &resultTy, Object::ID imageId, Object::ID lodId, Intermediate &dst) const |
| 4832 | { |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4833 | auto &image = getObject(imageId); |
| 4834 | auto &imageType = getType(image.type); |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4835 | |
| 4836 | ASSERT(imageType.definition.opcode() == spv::OpTypeImage); |
| 4837 | bool isArrayed = imageType.definition.word(5) != 0; |
| 4838 | bool isCubeMap = imageType.definition.word(3) == spv::DimCube; |
| 4839 | |
| 4840 | const DescriptorDecorations &d = descriptorDecorations.at(imageId); |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4841 | auto setLayout = routine->pipelineLayout->getDescriptorSetLayout(d.DescriptorSet); |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4842 | auto &bindingLayout = setLayout->getBindingLayout(d.Binding); |
| 4843 | |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4844 | Pointer<Byte> descriptor = routine->getPointer(imageId).base; |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4845 | |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4846 | Pointer<Int> extent; |
| 4847 | Int arrayLayers; |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4848 | |
| 4849 | switch (bindingLayout.descriptorType) |
| 4850 | { |
| 4851 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 4852 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 4853 | { |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4854 | extent = descriptor + OFFSET(vk::StorageImageDescriptor, extent); // int[3]* |
| 4855 | arrayLayers = *Pointer<Int>(descriptor + OFFSET(vk::StorageImageDescriptor, arrayLayers)); // uint32_t |
| 4856 | break; |
| 4857 | } |
| 4858 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
Chris Forbes | e228502 | 2019-05-08 16:09:53 -0700 | [diff] [blame] | 4859 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 4860 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4861 | { |
| 4862 | extent = descriptor + OFFSET(vk::SampledImageDescriptor, extent); // int[3]* |
| 4863 | arrayLayers = *Pointer<Int>(descriptor + OFFSET(vk::SampledImageDescriptor, arrayLayers)); // uint32_t |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4864 | break; |
| 4865 | } |
| 4866 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 4867 | UNREACHABLE("Image descriptorType: %d", int(bindingLayout.descriptorType)); |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4868 | } |
| 4869 | |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 4870 | auto dimensions = resultTy.sizeInComponents - (isArrayed ? 1 : 0); |
| 4871 | std::vector<Int> out; |
| 4872 | if (lodId != 0) |
| 4873 | { |
| 4874 | auto lodVal = GenericValue(this, routine, lodId); |
| 4875 | ASSERT(getType(lodVal.type).sizeInComponents == 1); |
| 4876 | auto lod = lodVal.Int(0); |
| 4877 | auto one = SIMD::Int(1); |
| 4878 | for (uint32_t i = 0; i < dimensions; i++) |
| 4879 | { |
| 4880 | dst.move(i, Max(SIMD::Int(extent[i]) >> lod, one)); |
| 4881 | } |
| 4882 | } |
| 4883 | else |
| 4884 | { |
| 4885 | for (uint32_t i = 0; i < dimensions; i++) |
| 4886 | { |
| 4887 | dst.move(i, SIMD::Int(extent[i])); |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | if (isArrayed) |
| 4892 | { |
| 4893 | auto numElements = isCubeMap ? (arrayLayers / 6) : RValue<Int>(arrayLayers); |
| 4894 | dst.move(dimensions, SIMD::Int(numElements)); |
| 4895 | } |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 4896 | } |
| 4897 | |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4898 | SIMD::Pointer SpirvShader::GetTexelAddress(SpirvRoutine const *routine, SIMD::Pointer ptr, GenericValue const & coordinate, Type const & imageType, Pointer<Byte> descriptor, int texelSize, Object::ID sampleId, bool useStencilAspect) const |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4899 | { |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4900 | bool isArrayed = imageType.definition.word(5) != 0; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 4901 | auto dim = static_cast<spv::Dim>(imageType.definition.word(3)); |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4902 | int dims = getType(coordinate.type).sizeInComponents - (isArrayed ? 1 : 0); |
| 4903 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 4904 | SIMD::Int u = coordinate.Int(0); |
| 4905 | SIMD::Int v = (getType(coordinate.type).sizeInComponents > 1) ? coordinate.Int(1) : RValue<SIMD::Int>(0); |
| 4906 | if (dim == spv::DimSubpassData) |
| 4907 | { |
| 4908 | u += routine->windowSpacePosition[0]; |
| 4909 | v += routine->windowSpacePosition[1]; |
| 4910 | } |
| 4911 | |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4912 | if (useStencilAspect) |
| 4913 | { |
| 4914 | // Adjust addressing for quad layout. Pitches are already correct for the stencil aspect. |
| 4915 | // In the quad-layout block, pixel order is [x0,y0 x1,y0 x0,y1 x1,y1] |
| 4916 | u = ((v & SIMD::Int(1)) << 1) | ((u << 1) - (u & SIMD::Int(1))); |
| 4917 | v &= SIMD::Int(~1); |
| 4918 | } |
| 4919 | |
| 4920 | auto rowPitch = SIMD::Int(*Pointer<Int>(descriptor + (useStencilAspect |
| 4921 | ? OFFSET(vk::StorageImageDescriptor, stencilRowPitchBytes) |
| 4922 | : OFFSET(vk::StorageImageDescriptor, rowPitchBytes)))); |
| 4923 | auto slicePitch = SIMD::Int( |
| 4924 | *Pointer<Int>(descriptor + (useStencilAspect |
| 4925 | ? OFFSET(vk::StorageImageDescriptor, stencilSlicePitchBytes) |
| 4926 | : OFFSET(vk::StorageImageDescriptor, slicePitchBytes)))); |
| 4927 | auto samplePitch = SIMD::Int( |
| 4928 | *Pointer<Int>(descriptor + (useStencilAspect |
| 4929 | ? OFFSET(vk::StorageImageDescriptor, stencilSamplePitchBytes) |
| 4930 | : OFFSET(vk::StorageImageDescriptor, samplePitchBytes)))); |
| 4931 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 4932 | ptr += u * SIMD::Int(texelSize); |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4933 | if (dims > 1) |
| 4934 | { |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4935 | ptr += v * rowPitch; |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4936 | } |
| 4937 | if (dims > 2) |
| 4938 | { |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4939 | ptr += coordinate.Int(2) * slicePitch; |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4940 | } |
| 4941 | if (isArrayed) |
| 4942 | { |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4943 | ptr += coordinate.Int(dims) * slicePitch; |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4944 | } |
| 4945 | |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 4946 | if (sampleId.value()) |
| 4947 | { |
| 4948 | GenericValue sample{this, routine, sampleId}; |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4949 | ptr += sample.Int(0) * samplePitch; |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 4950 | } |
| 4951 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 4952 | return ptr; |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 4953 | } |
| 4954 | |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 4955 | void SpirvShader::Yield(YieldResult res) const |
| 4956 | { |
| 4957 | rr::Yield(RValue<Int>(int(res))); |
| 4958 | } |
| 4959 | |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 4960 | SpirvShader::EmitResult SpirvShader::EmitImageRead(InsnIterator insn, EmitState *state) const |
| 4961 | { |
| 4962 | auto &resultType = getType(Type::ID(insn.word(1))); |
| 4963 | auto imageId = Object::ID(insn.word(3)); |
| 4964 | auto &image = getObject(imageId); |
| 4965 | auto &imageType = getType(image.type); |
| 4966 | Object::ID resultId = insn.word(2); |
| 4967 | |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 4968 | Object::ID sampleId = 0; |
| 4969 | |
| 4970 | if (insn.wordCount() > 5) |
| 4971 | { |
| 4972 | int operand = 6; |
| 4973 | auto imageOperands = insn.word(5); |
| 4974 | if (imageOperands & spv::ImageOperandsSampleMask) |
| 4975 | { |
| 4976 | sampleId = insn.word(operand++); |
| 4977 | imageOperands &= ~spv::ImageOperandsSampleMask; |
| 4978 | } |
| 4979 | |
| 4980 | // Should be no remaining image operands. |
| 4981 | ASSERT(!imageOperands); |
| 4982 | } |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 4983 | |
| 4984 | ASSERT(imageType.definition.opcode() == spv::OpTypeImage); |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 4985 | auto dim = static_cast<spv::Dim>(imageType.definition.word(3)); |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 4986 | |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 4987 | auto coordinate = GenericValue(this, state->routine, insn.word(4)); |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 4988 | const DescriptorDecorations &d = descriptorDecorations.at(imageId); |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 4989 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 4990 | // For subpass data, format in the instruction is spv::ImageFormatUnknown. Get it from |
| 4991 | // the renderpass data instead. In all other cases, we can use the format in the instruction. |
| 4992 | auto vkFormat = (dim == spv::DimSubpassData) |
| 4993 | ? inputAttachmentFormats[d.InputAttachmentIndex] |
| 4994 | : SpirvFormatToVulkanFormat(static_cast<spv::ImageFormat>(imageType.definition.word(8))); |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 4995 | |
| 4996 | // Depth+Stencil image attachments select aspect based on the Sampled Type of the |
| 4997 | // OpTypeImage. If float, then we want the depth aspect. If int, we want the stencil aspect. |
| 4998 | auto useStencilAspect = (vkFormat == VK_FORMAT_D32_SFLOAT_S8_UINT && |
| 4999 | getType(imageType.definition.word(2)).opcode() == spv::OpTypeInt); |
| 5000 | |
| 5001 | if (useStencilAspect) |
| 5002 | { |
| 5003 | vkFormat = VK_FORMAT_S8_UINT; |
| 5004 | } |
| 5005 | |
| 5006 | auto pointer = state->routine->getPointer(imageId); |
| 5007 | Pointer<Byte> binding = pointer.base; |
| 5008 | Pointer<Byte> imageBase = *Pointer<Pointer<Byte>>(binding + (useStencilAspect |
| 5009 | ? OFFSET(vk::StorageImageDescriptor, stencilPtr) |
| 5010 | : OFFSET(vk::StorageImageDescriptor, ptr))); |
| 5011 | |
| 5012 | auto imageSizeInBytes = *Pointer<Int>(binding + OFFSET(vk::StorageImageDescriptor, sizeInBytes)); |
| 5013 | |
| 5014 | auto &dst = state->routine->createIntermediate(resultId, resultType.sizeInComponents); |
| 5015 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5016 | auto texelSize = vk::Format(vkFormat).bytes(); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5017 | auto basePtr = SIMD::Pointer(imageBase, imageSizeInBytes); |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 5018 | auto texelPtr = GetTexelAddress(state->routine, basePtr, coordinate, imageType, binding, texelSize, sampleId, useStencilAspect); |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5019 | |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5020 | SIMD::Int packed[4]; |
Chris Forbes | a5f4eb6 | 2019-04-22 17:46:20 -0700 | [diff] [blame] | 5021 | // Round up texel size: for formats smaller than 32 bits per texel, we will emit a bunch |
| 5022 | // of (overlapping) 32b loads here, and each lane will pick out what it needs from the low bits. |
| 5023 | // TODO: specialize for small formats? |
| 5024 | for (auto i = 0; i < (texelSize + 3)/4; i++) |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5025 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5026 | packed[i] = SIMD::Load<SIMD::Int>(texelPtr, state->activeLaneMask()); |
| 5027 | texelPtr += sizeof(float); |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5028 | } |
| 5029 | |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5030 | // Format support requirements here come from two sources: |
| 5031 | // - Minimum required set of formats for loads from storage images |
| 5032 | // - Any format supported as a color or depth/stencil attachment, for input attachments |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5033 | switch(vkFormat) |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5034 | { |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5035 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 5036 | case VK_FORMAT_R32G32B32A32_SINT: |
| 5037 | case VK_FORMAT_R32G32B32A32_UINT: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5038 | dst.move(0, packed[0]); |
| 5039 | dst.move(1, packed[1]); |
| 5040 | dst.move(2, packed[2]); |
| 5041 | dst.move(3, packed[3]); |
| 5042 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5043 | case VK_FORMAT_R32_SINT: |
| 5044 | case VK_FORMAT_R32_UINT: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5045 | dst.move(0, packed[0]); |
| 5046 | // Fill remaining channels with 0,0,1 (of the correct type) |
| 5047 | dst.move(1, SIMD::Int(0)); |
| 5048 | dst.move(2, SIMD::Int(0)); |
| 5049 | dst.move(3, SIMD::Int(1)); |
| 5050 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5051 | case VK_FORMAT_R32_SFLOAT: |
Chris Forbes | a5f4eb6 | 2019-04-22 17:46:20 -0700 | [diff] [blame] | 5052 | case VK_FORMAT_D32_SFLOAT: |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 5053 | case VK_FORMAT_D32_SFLOAT_S8_UINT: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5054 | dst.move(0, packed[0]); |
| 5055 | // Fill remaining channels with 0,0,1 (of the correct type) |
| 5056 | dst.move(1, SIMD::Float(0)); |
| 5057 | dst.move(2, SIMD::Float(0)); |
| 5058 | dst.move(3, SIMD::Float(1)); |
| 5059 | break; |
Chris Forbes | a5f4eb6 | 2019-04-22 17:46:20 -0700 | [diff] [blame] | 5060 | case VK_FORMAT_D16_UNORM: |
| 5061 | dst.move(0, SIMD::Float(packed[0] & SIMD::Int(0xffff)) * SIMD::Float(1.0f / 65535.0f)); |
| 5062 | dst.move(1, SIMD::Float(0)); |
| 5063 | dst.move(2, SIMD::Float(0)); |
| 5064 | dst.move(3, SIMD::Float(1)); |
| 5065 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5066 | case VK_FORMAT_R16G16B16A16_SINT: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5067 | dst.move(0, (packed[0] << 16) >> 16); |
| 5068 | dst.move(1, (packed[0]) >> 16); |
| 5069 | dst.move(2, (packed[1] << 16) >> 16); |
| 5070 | dst.move(3, (packed[1]) >> 16); |
| 5071 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5072 | case VK_FORMAT_R16G16B16A16_UINT: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5073 | dst.move(0, packed[0] & SIMD::Int(0xffff)); |
| 5074 | dst.move(1, (packed[0] >> 16) & SIMD::Int(0xffff)); |
| 5075 | dst.move(2, packed[1] & SIMD::Int(0xffff)); |
| 5076 | dst.move(3, (packed[1] >> 16) & SIMD::Int(0xffff)); |
| 5077 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5078 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
Chris Forbes | d354695 | 2019-04-30 19:32:19 -0700 | [diff] [blame] | 5079 | dst.move(0, halfToFloatBits(As<SIMD::UInt>(packed[0]) & SIMD::UInt(0x0000FFFF))); |
| 5080 | dst.move(1, halfToFloatBits((As<SIMD::UInt>(packed[0]) & SIMD::UInt(0xFFFF0000)) >> 16)); |
| 5081 | dst.move(2, halfToFloatBits(As<SIMD::UInt>(packed[1]) & SIMD::UInt(0x0000FFFF))); |
| 5082 | dst.move(3, halfToFloatBits((As<SIMD::UInt>(packed[1]) & SIMD::UInt(0xFFFF0000)) >> 16)); |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5083 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5084 | case VK_FORMAT_R8G8B8A8_SNORM: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5085 | dst.move(0, Min(Max(SIMD::Float(((packed[0]<<24) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 5086 | dst.move(1, Min(Max(SIMD::Float(((packed[0]<<16) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 5087 | dst.move(2, Min(Max(SIMD::Float(((packed[0]<<8) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 5088 | dst.move(3, Min(Max(SIMD::Float(((packed[0]) & SIMD::Int(0xFF000000))) * SIMD::Float(1.0f / float(0x7f000000)), SIMD::Float(-1.0f)), SIMD::Float(1.0f))); |
| 5089 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5090 | case VK_FORMAT_R8G8B8A8_UNORM: |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5091 | case VK_FORMAT_A8B8G8R8_UNORM_PACK32: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5092 | dst.move(0, SIMD::Float((packed[0] & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5093 | dst.move(1, SIMD::Float(((packed[0]>>8) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5094 | dst.move(2, SIMD::Float(((packed[0]>>16) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5095 | dst.move(3, SIMD::Float(((packed[0]>>24) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5096 | break; |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5097 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 5098 | case VK_FORMAT_A8B8G8R8_SRGB_PACK32: |
| 5099 | dst.move(0, ::sRGBtoLinear(SIMD::Float((packed[0] & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5100 | dst.move(1, ::sRGBtoLinear(SIMD::Float(((packed[0]>>8) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5101 | dst.move(2, ::sRGBtoLinear(SIMD::Float(((packed[0]>>16) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5102 | dst.move(3, ::sRGBtoLinear(SIMD::Float(((packed[0]>>24) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5103 | break; |
| 5104 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 5105 | dst.move(0, SIMD::Float(((packed[0]>>16) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5106 | dst.move(1, SIMD::Float(((packed[0]>>8) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5107 | dst.move(2, SIMD::Float((packed[0] & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5108 | dst.move(3, SIMD::Float(((packed[0]>>24) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5109 | break; |
| 5110 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 5111 | dst.move(0, ::sRGBtoLinear(SIMD::Float(((packed[0]>>16) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5112 | dst.move(1, ::sRGBtoLinear(SIMD::Float(((packed[0]>>8) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5113 | dst.move(2, ::sRGBtoLinear(SIMD::Float((packed[0] & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5114 | dst.move(3, ::sRGBtoLinear(SIMD::Float(((packed[0]>>24) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f))); |
| 5115 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5116 | case VK_FORMAT_R8G8B8A8_UINT: |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5117 | case VK_FORMAT_A8B8G8R8_UINT_PACK32: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5118 | dst.move(0, (As<SIMD::UInt>(packed[0]) & SIMD::UInt(0xFF))); |
| 5119 | dst.move(1, ((As<SIMD::UInt>(packed[0])>>8) & SIMD::UInt(0xFF))); |
| 5120 | dst.move(2, ((As<SIMD::UInt>(packed[0])>>16) & SIMD::UInt(0xFF))); |
| 5121 | dst.move(3, ((As<SIMD::UInt>(packed[0])>>24) & SIMD::UInt(0xFF))); |
| 5122 | break; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 5123 | case VK_FORMAT_R8G8B8A8_SINT: |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5124 | case VK_FORMAT_A8B8G8R8_SINT_PACK32: |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5125 | dst.move(0, (packed[0] << 24) >> 24); |
| 5126 | dst.move(1, (packed[0] << 16) >> 24); |
| 5127 | dst.move(2, (packed[0] << 8) >> 24); |
| 5128 | dst.move(3, (packed[0]) >> 24); |
| 5129 | break; |
Chris Forbes | f5c8936 | 2019-04-26 13:41:41 -0700 | [diff] [blame] | 5130 | case VK_FORMAT_R8_UNORM: |
| 5131 | dst.move(0, SIMD::Float((packed[0] & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5132 | dst.move(1, SIMD::Float(0)); |
| 5133 | dst.move(2, SIMD::Float(0)); |
| 5134 | dst.move(3, SIMD::Float(1)); |
| 5135 | break; |
| 5136 | case VK_FORMAT_R8_UINT: |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 5137 | case VK_FORMAT_S8_UINT: |
Chris Forbes | f5c8936 | 2019-04-26 13:41:41 -0700 | [diff] [blame] | 5138 | dst.move(0, (As<SIMD::UInt>(packed[0]) & SIMD::UInt(0xFF))); |
| 5139 | dst.move(1, SIMD::UInt(0)); |
| 5140 | dst.move(2, SIMD::UInt(0)); |
| 5141 | dst.move(3, SIMD::UInt(1)); |
| 5142 | break; |
| 5143 | case VK_FORMAT_R8_SINT: |
| 5144 | dst.move(0, (packed[0] << 24) >> 24); |
| 5145 | dst.move(1, SIMD::Int(0)); |
| 5146 | dst.move(2, SIMD::Int(0)); |
| 5147 | dst.move(3, SIMD::Int(1)); |
| 5148 | break; |
| 5149 | case VK_FORMAT_R8G8_UNORM: |
| 5150 | dst.move(0, SIMD::Float((packed[0] & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5151 | dst.move(1, SIMD::Float(((packed[0]>>8) & SIMD::Int(0xFF))) * SIMD::Float(1.0f / 255.f)); |
| 5152 | dst.move(2, SIMD::Float(0)); |
| 5153 | dst.move(3, SIMD::Float(1)); |
| 5154 | break; |
| 5155 | case VK_FORMAT_R8G8_UINT: |
| 5156 | dst.move(0, (As<SIMD::UInt>(packed[0]) & SIMD::UInt(0xFF))); |
| 5157 | dst.move(1, ((As<SIMD::UInt>(packed[0])>>8) & SIMD::UInt(0xFF))); |
| 5158 | dst.move(2, SIMD::UInt(0)); |
| 5159 | dst.move(3, SIMD::UInt(1)); |
| 5160 | break; |
| 5161 | case VK_FORMAT_R8G8_SINT: |
| 5162 | dst.move(0, (packed[0] << 24) >> 24); |
| 5163 | dst.move(1, (packed[0] << 16) >> 24); |
| 5164 | dst.move(2, SIMD::Int(0)); |
| 5165 | dst.move(3, SIMD::Int(1)); |
| 5166 | break; |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5167 | case VK_FORMAT_R16_SFLOAT: |
Chris Forbes | d354695 | 2019-04-30 19:32:19 -0700 | [diff] [blame] | 5168 | dst.move(0, halfToFloatBits(As<SIMD::UInt>(packed[0]) & SIMD::UInt(0x0000FFFF))); |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5169 | dst.move(1, SIMD::Float(0)); |
| 5170 | dst.move(2, SIMD::Float(0)); |
| 5171 | dst.move(3, SIMD::Float(1)); |
| 5172 | break; |
| 5173 | case VK_FORMAT_R16_UINT: |
| 5174 | dst.move(0, packed[0] & SIMD::Int(0xffff)); |
| 5175 | dst.move(1, SIMD::UInt(0)); |
| 5176 | dst.move(2, SIMD::UInt(0)); |
| 5177 | dst.move(3, SIMD::UInt(1)); |
| 5178 | break; |
| 5179 | case VK_FORMAT_R16_SINT: |
| 5180 | dst.move(0, (packed[0] << 16) >> 16); |
| 5181 | dst.move(1, SIMD::Int(0)); |
| 5182 | dst.move(2, SIMD::Int(0)); |
| 5183 | dst.move(3, SIMD::Int(1)); |
| 5184 | break; |
| 5185 | case VK_FORMAT_R16G16_SFLOAT: |
Chris Forbes | d354695 | 2019-04-30 19:32:19 -0700 | [diff] [blame] | 5186 | dst.move(0, halfToFloatBits(As<SIMD::UInt>(packed[0]) & SIMD::UInt(0x0000FFFF))); |
| 5187 | dst.move(1, halfToFloatBits((As<SIMD::UInt>(packed[0]) & SIMD::UInt(0xFFFF0000)) >> 16)); |
Chris Forbes | a32d630 | 2019-04-26 14:19:04 -0700 | [diff] [blame] | 5188 | dst.move(2, SIMD::Float(0)); |
| 5189 | dst.move(3, SIMD::Float(1)); |
| 5190 | break; |
| 5191 | case VK_FORMAT_R16G16_UINT: |
| 5192 | dst.move(0, packed[0] & SIMD::Int(0xffff)); |
| 5193 | dst.move(1, (packed[0] >> 16) & SIMD::Int(0xffff)); |
| 5194 | dst.move(2, SIMD::UInt(0)); |
| 5195 | dst.move(3, SIMD::UInt(1)); |
| 5196 | break; |
| 5197 | case VK_FORMAT_R16G16_SINT: |
| 5198 | dst.move(0, (packed[0] << 16) >> 16); |
| 5199 | dst.move(1, (packed[0]) >> 16); |
| 5200 | dst.move(2, SIMD::Int(0)); |
| 5201 | dst.move(3, SIMD::Int(1)); |
| 5202 | break; |
| 5203 | case VK_FORMAT_R32G32_SINT: |
| 5204 | case VK_FORMAT_R32G32_UINT: |
| 5205 | dst.move(0, packed[0]); |
| 5206 | dst.move(1, packed[1]); |
| 5207 | dst.move(2, SIMD::Int(0)); |
| 5208 | dst.move(3, SIMD::Int(1)); |
| 5209 | break; |
| 5210 | case VK_FORMAT_R32G32_SFLOAT: |
| 5211 | dst.move(0, packed[0]); |
| 5212 | dst.move(1, packed[1]); |
| 5213 | dst.move(2, SIMD::Float(0)); |
| 5214 | dst.move(3, SIMD::Float(1)); |
| 5215 | break; |
Chris Forbes | dcc9fd7 | 2019-05-03 07:35:14 -0700 | [diff] [blame] | 5216 | case VK_FORMAT_A2B10G10R10_UINT_PACK32: |
| 5217 | dst.move(0, (packed[0]) & SIMD::Int(0x3FF)); |
| 5218 | dst.move(1, (packed[0] >> 10) & SIMD::Int(0x3FF)); |
| 5219 | dst.move(2, (packed[0] >> 20) & SIMD::Int(0x3FF)); |
| 5220 | dst.move(3, (packed[0] >> 30) & SIMD::Int(0x3)); |
| 5221 | break; |
Chris Forbes | 8aba20f | 2019-05-03 09:06:48 -0700 | [diff] [blame] | 5222 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 5223 | dst.move(0, SIMD::Float((packed[0] >> 11) & SIMD::Int(0x1F)) * SIMD::Float(1.0f / 0x1F)); |
| 5224 | dst.move(1, SIMD::Float((packed[0] >> 5) & SIMD::Int(0x3F)) * SIMD::Float(1.0f / 0x3F)); |
| 5225 | dst.move(2, SIMD::Float((packed[0]) & SIMD::Int(0x1F)) * SIMD::Float(1.0f / 0x1F)); |
| 5226 | dst.move(3, SIMD::Float(1)); |
| 5227 | break; |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5228 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 5229 | UNIMPLEMENTED("spv::ImageFormat %d", int(vkFormat)); |
| 5230 | break; |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 5231 | } |
| 5232 | |
| 5233 | return EmitResult::Continue; |
| 5234 | } |
| 5235 | |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5236 | SpirvShader::EmitResult SpirvShader::EmitImageWrite(InsnIterator insn, EmitState *state) const |
| 5237 | { |
| 5238 | auto imageId = Object::ID(insn.word(1)); |
| 5239 | auto &image = getObject(imageId); |
| 5240 | auto &imageType = getType(image.type); |
| 5241 | |
| 5242 | ASSERT(imageType.definition.opcode() == spv::OpTypeImage); |
| 5243 | |
| 5244 | // Not handling any image operands yet. |
| 5245 | ASSERT(insn.wordCount() == 4); |
| 5246 | |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5247 | auto coordinate = GenericValue(this, state->routine, insn.word(2)); |
| 5248 | auto texel = GenericValue(this, state->routine, insn.word(3)); |
| 5249 | |
Chris Forbes | 621a7bd | 2019-04-19 08:28:00 -0700 | [diff] [blame] | 5250 | Pointer<Byte> binding = state->routine->getPointer(imageId).base; |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5251 | Pointer<Byte> imageBase = *Pointer<Pointer<Byte>>(binding + OFFSET(vk::StorageImageDescriptor, ptr)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5252 | auto imageSizeInBytes = *Pointer<Int>(binding + OFFSET(vk::StorageImageDescriptor, sizeInBytes)); |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5253 | |
| 5254 | SIMD::Int packed[4]; |
| 5255 | auto numPackedElements = 0u; |
| 5256 | int texelSize = 0; |
| 5257 | auto format = static_cast<spv::ImageFormat>(imageType.definition.word(8)); |
| 5258 | switch (format) |
| 5259 | { |
| 5260 | case spv::ImageFormatRgba32f: |
| 5261 | case spv::ImageFormatRgba32i: |
| 5262 | case spv::ImageFormatRgba32ui: |
| 5263 | texelSize = 16; |
| 5264 | packed[0] = texel.Int(0); |
| 5265 | packed[1] = texel.Int(1); |
| 5266 | packed[2] = texel.Int(2); |
| 5267 | packed[3] = texel.Int(3); |
| 5268 | numPackedElements = 4; |
| 5269 | break; |
| 5270 | case spv::ImageFormatR32f: |
| 5271 | case spv::ImageFormatR32i: |
| 5272 | case spv::ImageFormatR32ui: |
| 5273 | texelSize = 4; |
| 5274 | packed[0] = texel.Int(0); |
| 5275 | numPackedElements = 1; |
| 5276 | break; |
| 5277 | case spv::ImageFormatRgba8: |
| 5278 | texelSize = 4; |
| 5279 | packed[0] = (SIMD::UInt(Round(Min(Max(texel.Float(0), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) | |
| 5280 | ((SIMD::UInt(Round(Min(Max(texel.Float(1), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) << 8) | |
| 5281 | ((SIMD::UInt(Round(Min(Max(texel.Float(2), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) << 16) | |
| 5282 | ((SIMD::UInt(Round(Min(Max(texel.Float(3), SIMD::Float(0.0f)), SIMD::Float(1.0f)) * SIMD::Float(255.0f)))) << 24); |
| 5283 | numPackedElements = 1; |
| 5284 | break; |
| 5285 | case spv::ImageFormatRgba8Snorm: |
| 5286 | texelSize = 4; |
| 5287 | packed[0] = (SIMD::Int(Round(Min(Max(texel.Float(0), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 5288 | SIMD::Int(0xFF)) | |
| 5289 | ((SIMD::Int(Round(Min(Max(texel.Float(1), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 5290 | SIMD::Int(0xFF)) << 8) | |
| 5291 | ((SIMD::Int(Round(Min(Max(texel.Float(2), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 5292 | SIMD::Int(0xFF)) << 16) | |
| 5293 | ((SIMD::Int(Round(Min(Max(texel.Float(3), SIMD::Float(-1.0f)), SIMD::Float(1.0f)) * SIMD::Float(127.0f))) & |
| 5294 | SIMD::Int(0xFF)) << 24); |
| 5295 | numPackedElements = 1; |
| 5296 | break; |
| 5297 | case spv::ImageFormatRgba8i: |
| 5298 | case spv::ImageFormatRgba8ui: |
| 5299 | texelSize = 4; |
| 5300 | packed[0] = (SIMD::UInt(texel.UInt(0) & SIMD::UInt(0xff))) | |
| 5301 | (SIMD::UInt(texel.UInt(1) & SIMD::UInt(0xff)) << 8) | |
| 5302 | (SIMD::UInt(texel.UInt(2) & SIMD::UInt(0xff)) << 16) | |
| 5303 | (SIMD::UInt(texel.UInt(3) & SIMD::UInt(0xff)) << 24); |
| 5304 | numPackedElements = 1; |
| 5305 | break; |
| 5306 | case spv::ImageFormatRgba16f: |
| 5307 | texelSize = 8; |
| 5308 | packed[0] = FloatToHalfBits(texel.UInt(0), false) | FloatToHalfBits(texel.UInt(1), true); |
| 5309 | packed[1] = FloatToHalfBits(texel.UInt(2), false) | FloatToHalfBits(texel.UInt(3), true); |
| 5310 | numPackedElements = 2; |
| 5311 | break; |
| 5312 | case spv::ImageFormatRgba16i: |
| 5313 | case spv::ImageFormatRgba16ui: |
| 5314 | texelSize = 8; |
| 5315 | packed[0] = SIMD::UInt(texel.UInt(0) & SIMD::UInt(0xffff)) | (SIMD::UInt(texel.UInt(1) & SIMD::UInt(0xffff)) << 16); |
| 5316 | packed[1] = SIMD::UInt(texel.UInt(2) & SIMD::UInt(0xffff)) | (SIMD::UInt(texel.UInt(3) & SIMD::UInt(0xffff)) << 16); |
| 5317 | numPackedElements = 2; |
| 5318 | break; |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 5319 | case spv::ImageFormatRg32f: |
| 5320 | case spv::ImageFormatRg16f: |
| 5321 | case spv::ImageFormatR11fG11fB10f: |
| 5322 | case spv::ImageFormatR16f: |
| 5323 | case spv::ImageFormatRgba16: |
| 5324 | case spv::ImageFormatRgb10A2: |
| 5325 | case spv::ImageFormatRg16: |
| 5326 | case spv::ImageFormatRg8: |
| 5327 | case spv::ImageFormatR16: |
| 5328 | case spv::ImageFormatR8: |
| 5329 | case spv::ImageFormatRgba16Snorm: |
| 5330 | case spv::ImageFormatRg16Snorm: |
| 5331 | case spv::ImageFormatRg8Snorm: |
| 5332 | case spv::ImageFormatR16Snorm: |
| 5333 | case spv::ImageFormatR8Snorm: |
| 5334 | case spv::ImageFormatRg32i: |
| 5335 | case spv::ImageFormatRg16i: |
| 5336 | case spv::ImageFormatRg8i: |
| 5337 | case spv::ImageFormatR16i: |
| 5338 | case spv::ImageFormatR8i: |
| 5339 | case spv::ImageFormatRgb10a2ui: |
| 5340 | case spv::ImageFormatRg32ui: |
| 5341 | case spv::ImageFormatRg16ui: |
| 5342 | case spv::ImageFormatRg8ui: |
| 5343 | case spv::ImageFormatR16ui: |
| 5344 | case spv::ImageFormatR8ui: |
| 5345 | UNIMPLEMENTED("spv::ImageFormat %d", int(format)); |
| 5346 | break; |
| 5347 | |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5348 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 5349 | UNREACHABLE("spv::ImageFormat %d", int(format)); |
| 5350 | break; |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5351 | } |
| 5352 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5353 | auto basePtr = SIMD::Pointer(imageBase, imageSizeInBytes); |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 5354 | auto texelPtr = GetTexelAddress(state->routine, basePtr, coordinate, imageType, binding, texelSize, 0, false); |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5355 | |
| 5356 | for (auto i = 0u; i < numPackedElements; i++) |
| 5357 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5358 | SIMD::Store(texelPtr, packed[i], state->activeLaneMask()); |
| 5359 | texelPtr += sizeof(float); |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 5360 | } |
| 5361 | |
| 5362 | return EmitResult::Continue; |
| 5363 | } |
| 5364 | |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 5365 | SpirvShader::EmitResult SpirvShader::EmitImageTexelPointer(InsnIterator insn, EmitState *state) const |
| 5366 | { |
| 5367 | auto &resultType = getType(Type::ID(insn.word(1))); |
| 5368 | auto imageId = Object::ID(insn.word(3)); |
| 5369 | auto &image = getObject(imageId); |
| 5370 | // Note: OpImageTexelPointer is unusual in that the image is passed by pointer. |
| 5371 | // Look through to get the actual image type. |
| 5372 | auto &imageType = getType(getType(image.type).element); |
| 5373 | Object::ID resultId = insn.word(2); |
| 5374 | |
| 5375 | ASSERT(imageType.opcode() == spv::OpTypeImage); |
| 5376 | ASSERT(resultType.storageClass == spv::StorageClassImage); |
| 5377 | ASSERT(getType(resultType.element).opcode() == spv::OpTypeInt); |
| 5378 | |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 5379 | auto coordinate = GenericValue(this, state->routine, insn.word(4)); |
| 5380 | |
Chris Forbes | 621a7bd | 2019-04-19 08:28:00 -0700 | [diff] [blame] | 5381 | Pointer<Byte> binding = state->routine->getPointer(imageId).base; |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 5382 | Pointer<Byte> imageBase = *Pointer<Pointer<Byte>>(binding + OFFSET(vk::StorageImageDescriptor, ptr)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5383 | auto imageSizeInBytes = *Pointer<Int>(binding + OFFSET(vk::StorageImageDescriptor, sizeInBytes)); |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 5384 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5385 | auto basePtr = SIMD::Pointer(imageBase, imageSizeInBytes); |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 5386 | auto ptr = GetTexelAddress(state->routine, basePtr, coordinate, imageType, binding, sizeof(uint32_t), 0, false); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5387 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5388 | state->routine->createPointer(resultId, ptr); |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 5389 | |
| 5390 | return EmitResult::Continue; |
| 5391 | } |
| 5392 | |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 5393 | SpirvShader::EmitResult SpirvShader::EmitSampledImageCombineOrSplit(InsnIterator insn, EmitState *state) const |
| 5394 | { |
| 5395 | // Propagate the image pointer in both cases. |
| 5396 | // Consumers of OpSampledImage will look through to find the sampler pointer. |
| 5397 | |
| 5398 | Object::ID resultId = insn.word(2); |
| 5399 | Object::ID imageId = insn.word(3); |
| 5400 | |
| 5401 | state->routine->createPointer(resultId, state->routine->getPointer(imageId)); |
| 5402 | |
| 5403 | return EmitResult::Continue; |
| 5404 | } |
| 5405 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5406 | SpirvShader::EmitResult SpirvShader::EmitAtomicOp(InsnIterator insn, EmitState *state) const |
| 5407 | { |
| 5408 | auto &resultType = getType(Type::ID(insn.word(1))); |
| 5409 | Object::ID resultId = insn.word(2); |
| 5410 | Object::ID semanticsId = insn.word(5); |
| 5411 | auto memorySemantics = static_cast<spv::MemorySemanticsMask>(getObject(semanticsId).constantValue[0]); |
| 5412 | auto memoryOrder = MemoryOrder(memorySemantics); |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 5413 | // Where no value is provided (increment/decrement) use an implicit value of 1. |
| 5414 | auto value = (insn.wordCount() == 7) ? GenericValue(this, state->routine, insn.word(6)).UInt(0) : RValue<SIMD::UInt>(1); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5415 | auto &dst = state->routine->createIntermediate(resultId, resultType.sizeInComponents); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5416 | auto ptr = state->routine->getPointer(insn.word(3)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5417 | auto ptrOffsets = ptr.offsets(); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5418 | |
| 5419 | SIMD::UInt x; |
| 5420 | for (int j = 0; j < SIMD::Width; j++) |
| 5421 | { |
| 5422 | If(Extract(state->activeLaneMask(), j) != 0) |
| 5423 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5424 | auto offset = Extract(ptrOffsets, j); |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 5425 | auto laneValue = Extract(value, j); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5426 | UInt v; |
| 5427 | switch (insn.opcode()) |
| 5428 | { |
| 5429 | case spv::OpAtomicIAdd: |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 5430 | case spv::OpAtomicIIncrement: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5431 | v = AddAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5432 | break; |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 5433 | case spv::OpAtomicISub: |
| 5434 | case spv::OpAtomicIDecrement: |
| 5435 | v = SubAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
| 5436 | break; |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5437 | case spv::OpAtomicAnd: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5438 | v = AndAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5439 | break; |
| 5440 | case spv::OpAtomicOr: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5441 | v = OrAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5442 | break; |
| 5443 | case spv::OpAtomicXor: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5444 | v = XorAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5445 | break; |
| 5446 | case spv::OpAtomicSMin: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5447 | v = As<UInt>(MinAtomic(Pointer<Int>(&ptr.base[offset]), As<Int>(laneValue), memoryOrder)); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5448 | break; |
| 5449 | case spv::OpAtomicSMax: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5450 | v = As<UInt>(MaxAtomic(Pointer<Int>(&ptr.base[offset]), As<Int>(laneValue), memoryOrder)); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5451 | break; |
| 5452 | case spv::OpAtomicUMin: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5453 | v = MinAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5454 | break; |
| 5455 | case spv::OpAtomicUMax: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5456 | v = MaxAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5457 | break; |
| 5458 | case spv::OpAtomicExchange: |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5459 | v = ExchangeAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, memoryOrder); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5460 | break; |
| 5461 | default: |
Ben Clayton | 92797c2 | 2019-04-25 10:44:03 +0100 | [diff] [blame] | 5462 | UNREACHABLE("%s", OpcodeName(insn.opcode()).c_str()); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 5463 | break; |
| 5464 | } |
| 5465 | x = Insert(x, v, j); |
| 5466 | } |
| 5467 | } |
| 5468 | |
| 5469 | dst.move(0, x); |
| 5470 | return EmitResult::Continue; |
| 5471 | } |
| 5472 | |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 5473 | SpirvShader::EmitResult SpirvShader::EmitAtomicCompareExchange(InsnIterator insn, EmitState *state) const |
| 5474 | { |
| 5475 | // Separate from EmitAtomicOp due to different instruction encoding |
| 5476 | auto &resultType = getType(Type::ID(insn.word(1))); |
| 5477 | Object::ID resultId = insn.word(2); |
| 5478 | |
| 5479 | auto memorySemanticsEqual = static_cast<spv::MemorySemanticsMask>(getObject(insn.word(5)).constantValue[0]); |
| 5480 | auto memoryOrderEqual = MemoryOrder(memorySemanticsEqual); |
| 5481 | auto memorySemanticsUnequal = static_cast<spv::MemorySemanticsMask>(getObject(insn.word(6)).constantValue[0]); |
| 5482 | auto memoryOrderUnequal = MemoryOrder(memorySemanticsUnequal); |
| 5483 | |
| 5484 | auto value = GenericValue(this, state->routine, insn.word(7)); |
| 5485 | auto comparator = GenericValue(this, state->routine, insn.word(8)); |
| 5486 | auto &dst = state->routine->createIntermediate(resultId, resultType.sizeInComponents); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5487 | auto ptr = state->routine->getPointer(insn.word(3)); |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5488 | auto ptrOffsets = ptr.offsets(); |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 5489 | |
| 5490 | SIMD::UInt x; |
| 5491 | for (int j = 0; j < SIMD::Width; j++) |
| 5492 | { |
| 5493 | If(Extract(state->activeLaneMask(), j) != 0) |
| 5494 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 5495 | auto offset = Extract(ptrOffsets, j); |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 5496 | auto laneValue = Extract(value.UInt(0), j); |
| 5497 | auto laneComparator = Extract(comparator.UInt(0), j); |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 5498 | UInt v = CompareExchangeAtomic(Pointer<UInt>(&ptr.base[offset]), laneValue, laneComparator, memoryOrderEqual, memoryOrderUnequal); |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 5499 | x = Insert(x, v, j); |
| 5500 | } |
| 5501 | } |
| 5502 | |
| 5503 | dst.move(0, x); |
| 5504 | return EmitResult::Continue; |
| 5505 | } |
| 5506 | |
Ben Clayton | 78abf37 | 2019-05-09 15:11:58 +0100 | [diff] [blame] | 5507 | SpirvShader::EmitResult SpirvShader::EmitCopyObject(InsnIterator insn, EmitState *state) const |
| 5508 | { |
| 5509 | auto ty = getType(insn.word(1)); |
| 5510 | auto &dst = state->routine->createIntermediate(insn.word(2), ty.sizeInComponents); |
| 5511 | auto src = GenericValue(this, state->routine, insn.word(3)); |
| 5512 | for (uint32_t i = 0; i < ty.sizeInComponents; i++) |
| 5513 | { |
| 5514 | dst.move(i, src.Int(i)); |
| 5515 | } |
| 5516 | return EmitResult::Continue; |
| 5517 | } |
| 5518 | |
Ben Clayton | b5a4546 | 2019-04-30 19:21:29 +0100 | [diff] [blame] | 5519 | SpirvShader::EmitResult SpirvShader::EmitCopyMemory(InsnIterator insn, EmitState *state) const |
| 5520 | { |
| 5521 | Object::ID dstPtrId = insn.word(1); |
| 5522 | Object::ID srcPtrId = insn.word(2); |
| 5523 | auto &dstPtrTy = getType(getObject(dstPtrId).type); |
| 5524 | auto &srcPtrTy = getType(getObject(srcPtrId).type); |
| 5525 | ASSERT(dstPtrTy.element == srcPtrTy.element); |
| 5526 | |
| 5527 | bool dstInterleavedByLane = IsStorageInterleavedByLane(dstPtrTy.storageClass); |
| 5528 | bool srcInterleavedByLane = IsStorageInterleavedByLane(srcPtrTy.storageClass); |
| 5529 | auto dstPtr = state->routine->getPointer(dstPtrId); |
| 5530 | auto srcPtr = state->routine->getPointer(srcPtrId); |
| 5531 | |
| 5532 | std::unordered_map<uint32_t, uint32_t> srcOffsets; |
| 5533 | |
| 5534 | VisitMemoryObject(srcPtrId, [&](uint32_t i, uint32_t srcOffset) { srcOffsets[i] = srcOffset; }); |
| 5535 | |
| 5536 | VisitMemoryObject(dstPtrId, [&](uint32_t i, uint32_t dstOffset) |
| 5537 | { |
| 5538 | auto it = srcOffsets.find(i); |
| 5539 | ASSERT(it != srcOffsets.end()); |
| 5540 | auto srcOffset = it->second; |
| 5541 | |
| 5542 | auto dst = dstPtr + dstOffset; |
| 5543 | auto src = srcPtr + srcOffset; |
| 5544 | if (dstInterleavedByLane) { dst = interleaveByLane(dst); } |
| 5545 | if (srcInterleavedByLane) { src = interleaveByLane(src); } |
| 5546 | SIMD::Store(dst, SIMD::Load<SIMD::Float>(src, state->activeLaneMask()), state->activeLaneMask()); |
| 5547 | }); |
| 5548 | return EmitResult::Continue; |
| 5549 | } |
| 5550 | |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 5551 | SpirvShader::EmitResult SpirvShader::EmitControlBarrier(InsnIterator insn, EmitState *state) const |
| 5552 | { |
| 5553 | auto executionScope = spv::Scope(GetConstScalarInt(insn.word(1))); |
| 5554 | auto semantics = spv::MemorySemanticsMask(GetConstScalarInt(insn.word(3))); |
| 5555 | // TODO: We probably want to consider the memory scope here. For now, |
| 5556 | // just always emit the full fence. |
| 5557 | Fence(semantics); |
| 5558 | |
| 5559 | switch (executionScope) |
| 5560 | { |
| 5561 | case spv::ScopeWorkgroup: |
| 5562 | case spv::ScopeSubgroup: |
| 5563 | Yield(YieldResult::ControlBarrier); |
| 5564 | break; |
| 5565 | default: |
| 5566 | // See Vulkan 1.1 spec, Appendix A, Validation Rules within a Module. |
| 5567 | UNREACHABLE("Scope for execution must be limited to Workgroup or Subgroup"); |
| 5568 | break; |
| 5569 | } |
| 5570 | |
| 5571 | return EmitResult::Continue; |
| 5572 | } |
| 5573 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 5574 | SpirvShader::EmitResult SpirvShader::EmitMemoryBarrier(InsnIterator insn, EmitState *state) const |
| 5575 | { |
| 5576 | auto semantics = spv::MemorySemanticsMask(GetConstScalarInt(insn.word(2))); |
| 5577 | // TODO: We probably want to consider the memory scope here. For now, |
| 5578 | // just always emit the full fence. |
| 5579 | Fence(semantics); |
| 5580 | return EmitResult::Continue; |
| 5581 | } |
| 5582 | |
| 5583 | void SpirvShader::Fence(spv::MemorySemanticsMask semantics) const |
| 5584 | { |
| 5585 | if (semantics == spv::MemorySemanticsMaskNone) |
| 5586 | { |
| 5587 | return; //no-op |
| 5588 | } |
| 5589 | rr::Fence(MemoryOrder(semantics)); |
| 5590 | } |
| 5591 | |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 5592 | SpirvShader::EmitResult SpirvShader::EmitGroupNonUniform(InsnIterator insn, EmitState *state) const |
| 5593 | { |
| 5594 | auto &type = getType(Type::ID(insn.word(1))); |
| 5595 | Object::ID resultId = insn.word(2); |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 5596 | auto scope = spv::Scope(GetConstScalarInt(insn.word(3))); |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 5597 | ASSERT_MSG(scope == spv::ScopeSubgroup, "Scope for Non Uniform Group Operations must be Subgroup for Vulkan 1.1"); |
| 5598 | |
| 5599 | auto &dst = state->routine->createIntermediate(resultId, type.sizeInComponents); |
| 5600 | |
| 5601 | switch (insn.opcode()) |
| 5602 | { |
| 5603 | case spv::OpGroupNonUniformElect: |
| 5604 | { |
| 5605 | // Result is true only in the active invocation with the lowest id |
| 5606 | // in the group, otherwise result is false. |
| 5607 | SIMD::Int active = state->activeLaneMask(); |
| 5608 | // TODO: Would be nice if we could write this as: |
| 5609 | // elect = active & ~(active.Oxyz | active.OOxy | active.OOOx) |
| 5610 | auto v0111 = SIMD::Int(0, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); |
| 5611 | auto elect = active & ~(v0111 & (active.xxyz | active.xxxy | active.xxxx)); |
| 5612 | dst.move(0, elect); |
| 5613 | break; |
| 5614 | } |
| 5615 | default: |
| 5616 | UNIMPLEMENTED("EmitGroupNonUniform op: %s", OpcodeName(type.opcode()).c_str()); |
| 5617 | } |
| 5618 | return EmitResult::Continue; |
| 5619 | } |
| 5620 | |
Ben Clayton | e4605da | 2019-05-09 16:24:01 +0100 | [diff] [blame] | 5621 | SpirvShader::EmitResult SpirvShader::EmitArrayLength(InsnIterator insn, EmitState *state) const |
| 5622 | { |
| 5623 | auto resultTyId = Type::ID(insn.word(1)); |
| 5624 | auto resultId = Object::ID(insn.word(2)); |
| 5625 | auto structPtrId = Object::ID(insn.word(3)); |
| 5626 | auto arrayFieldIdx = insn.word(4); |
| 5627 | |
| 5628 | auto &resultType = getType(resultTyId); |
| 5629 | ASSERT(resultType.sizeInComponents == 1); |
| 5630 | ASSERT(resultType.definition.opcode() == spv::OpTypeInt); |
| 5631 | |
| 5632 | auto &structPtrTy = getType(getObject(structPtrId).type); |
| 5633 | auto &structTy = getType(structPtrTy.element); |
| 5634 | auto &arrayTy = getType(structTy.definition.word(2 + arrayFieldIdx)); |
| 5635 | ASSERT(arrayTy.definition.opcode() == spv::OpTypeRuntimeArray); |
| 5636 | auto &arrayElTy = getType(arrayTy.element); |
| 5637 | |
| 5638 | auto &result = state->routine->createIntermediate(resultId, 1); |
| 5639 | auto structBase = GetPointerToData(structPtrId, 0, state->routine); |
| 5640 | |
| 5641 | Decorations d = {}; |
| 5642 | ApplyDecorationsForIdMember(&d, structPtrTy.element, arrayFieldIdx); |
| 5643 | ASSERT(d.HasOffset); |
| 5644 | |
| 5645 | auto arrayBase = structBase + d.Offset; |
| 5646 | auto arraySizeInBytes = SIMD::Int(arrayBase.limit) - arrayBase.offsets(); |
| 5647 | auto arrayLength = arraySizeInBytes / SIMD::Int(arrayElTy.sizeInComponents * sizeof(float)); |
| 5648 | |
| 5649 | result.move(0, SIMD::Int(arrayLength)); |
| 5650 | |
| 5651 | return EmitResult::Continue; |
| 5652 | } |
| 5653 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 5654 | uint32_t SpirvShader::GetConstScalarInt(Object::ID id) const |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 5655 | { |
| 5656 | auto &scopeObj = getObject(id); |
| 5657 | ASSERT(scopeObj.kind == Object::Kind::Constant); |
| 5658 | ASSERT(getType(scopeObj.type).sizeInComponents == 1); |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 5659 | return scopeObj.constantValue[0]; |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 5660 | } |
| 5661 | |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 5662 | void SpirvShader::emitEpilog(SpirvRoutine *routine) const |
| 5663 | { |
| 5664 | for (auto insn : *this) |
| 5665 | { |
| 5666 | switch (insn.opcode()) |
| 5667 | { |
| 5668 | case spv::OpVariable: |
| 5669 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 5670 | Object::ID resultId = insn.word(2); |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 5671 | auto &object = getObject(resultId); |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 5672 | auto &objectTy = getType(object.type); |
| 5673 | if (object.kind == Object::Kind::InterfaceVariable && objectTy.storageClass == spv::StorageClassOutput) |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 5674 | { |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 5675 | auto &dst = routine->getVariable(resultId); |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 5676 | int offset = 0; |
| 5677 | VisitInterface(resultId, |
| 5678 | [&](Decorations const &d, AttribType type) { |
| 5679 | auto scalarSlot = d.Location << 2 | d.Component; |
| 5680 | routine->outputs[scalarSlot] = dst[offset++]; |
| 5681 | }); |
| 5682 | } |
| 5683 | break; |
| 5684 | } |
| 5685 | default: |
| 5686 | break; |
| 5687 | } |
| 5688 | } |
| 5689 | } |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 5690 | |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 5691 | SpirvShader::Block::Block(InsnIterator begin, InsnIterator end) : begin_(begin), end_(end) |
| 5692 | { |
| 5693 | // Default to a Simple, this may change later. |
| 5694 | kind = Block::Simple; |
| 5695 | |
| 5696 | // Walk the instructions to find the last two of the block. |
| 5697 | InsnIterator insns[2]; |
| 5698 | for (auto insn : *this) |
| 5699 | { |
| 5700 | insns[0] = insns[1]; |
| 5701 | insns[1] = insn; |
| 5702 | } |
| 5703 | |
| 5704 | switch (insns[1].opcode()) |
| 5705 | { |
| 5706 | case spv::OpBranch: |
| 5707 | branchInstruction = insns[1]; |
| 5708 | outs.emplace(Block::ID(branchInstruction.word(1))); |
| 5709 | |
| 5710 | switch (insns[0].opcode()) |
| 5711 | { |
| 5712 | case spv::OpLoopMerge: |
| 5713 | kind = Loop; |
| 5714 | mergeInstruction = insns[0]; |
| 5715 | mergeBlock = Block::ID(mergeInstruction.word(1)); |
| 5716 | continueTarget = Block::ID(mergeInstruction.word(2)); |
| 5717 | break; |
| 5718 | |
| 5719 | default: |
| 5720 | kind = Block::Simple; |
| 5721 | break; |
| 5722 | } |
| 5723 | break; |
| 5724 | |
| 5725 | case spv::OpBranchConditional: |
| 5726 | branchInstruction = insns[1]; |
| 5727 | outs.emplace(Block::ID(branchInstruction.word(2))); |
| 5728 | outs.emplace(Block::ID(branchInstruction.word(3))); |
| 5729 | |
| 5730 | switch (insns[0].opcode()) |
| 5731 | { |
| 5732 | case spv::OpSelectionMerge: |
| 5733 | kind = StructuredBranchConditional; |
| 5734 | mergeInstruction = insns[0]; |
| 5735 | mergeBlock = Block::ID(mergeInstruction.word(1)); |
| 5736 | break; |
| 5737 | |
| 5738 | case spv::OpLoopMerge: |
| 5739 | kind = Loop; |
| 5740 | mergeInstruction = insns[0]; |
| 5741 | mergeBlock = Block::ID(mergeInstruction.word(1)); |
| 5742 | continueTarget = Block::ID(mergeInstruction.word(2)); |
| 5743 | break; |
| 5744 | |
| 5745 | default: |
| 5746 | kind = UnstructuredBranchConditional; |
| 5747 | break; |
| 5748 | } |
| 5749 | break; |
| 5750 | |
| 5751 | case spv::OpSwitch: |
| 5752 | branchInstruction = insns[1]; |
| 5753 | outs.emplace(Block::ID(branchInstruction.word(2))); |
| 5754 | for (uint32_t w = 4; w < branchInstruction.wordCount(); w += 2) |
| 5755 | { |
| 5756 | outs.emplace(Block::ID(branchInstruction.word(w))); |
| 5757 | } |
| 5758 | |
| 5759 | switch (insns[0].opcode()) |
| 5760 | { |
| 5761 | case spv::OpSelectionMerge: |
| 5762 | kind = StructuredSwitch; |
| 5763 | mergeInstruction = insns[0]; |
| 5764 | mergeBlock = Block::ID(mergeInstruction.word(1)); |
| 5765 | break; |
| 5766 | |
| 5767 | default: |
| 5768 | kind = UnstructuredSwitch; |
| 5769 | break; |
| 5770 | } |
| 5771 | break; |
| 5772 | |
| 5773 | default: |
| 5774 | break; |
| 5775 | } |
| 5776 | } |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 5777 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 5778 | bool SpirvShader::existsPath(Block::ID from, Block::ID to, Block::ID notPassingThrough) const |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 5779 | { |
| 5780 | // TODO: Optimize: This can be cached on the block. |
| 5781 | Block::Set seen; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 5782 | seen.emplace(notPassingThrough); |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 5783 | |
| 5784 | std::queue<Block::ID> pending; |
| 5785 | pending.emplace(from); |
| 5786 | |
| 5787 | while (pending.size() > 0) |
| 5788 | { |
| 5789 | auto id = pending.front(); |
| 5790 | pending.pop(); |
| 5791 | for (auto out : getBlock(id).outs) |
| 5792 | { |
| 5793 | if (seen.count(out) != 0) { continue; } |
| 5794 | if (out == to) { return true; } |
| 5795 | pending.emplace(out); |
| 5796 | } |
| 5797 | seen.emplace(id); |
| 5798 | } |
| 5799 | |
| 5800 | return false; |
| 5801 | } |
| 5802 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 5803 | void SpirvShader::EmitState::addOutputActiveLaneMaskEdge(Block::ID to, RValue<SIMD::Int> mask) |
| 5804 | { |
| 5805 | addActiveLaneMaskEdge(currentBlock, to, mask & activeLaneMask()); |
| 5806 | } |
| 5807 | |
| 5808 | void SpirvShader::EmitState::addActiveLaneMaskEdge(Block::ID from, Block::ID to, RValue<SIMD::Int> mask) |
| 5809 | { |
| 5810 | auto edge = Block::Edge{from, to}; |
| 5811 | auto it = edgeActiveLaneMasks.find(edge); |
| 5812 | if (it == edgeActiveLaneMasks.end()) |
| 5813 | { |
| 5814 | edgeActiveLaneMasks.emplace(edge, mask); |
| 5815 | } |
| 5816 | else |
| 5817 | { |
| 5818 | auto combined = it->second | mask; |
| 5819 | edgeActiveLaneMasks.erase(edge); |
| 5820 | edgeActiveLaneMasks.emplace(edge, combined); |
| 5821 | } |
| 5822 | } |
| 5823 | |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 5824 | RValue<SIMD::Int> SpirvShader::GetActiveLaneMaskEdge(EmitState *state, Block::ID from, Block::ID to) const |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 5825 | { |
| 5826 | auto edge = Block::Edge{from, to}; |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 5827 | auto it = state->edgeActiveLaneMasks.find(edge); |
| 5828 | ASSERT_MSG(it != state->edgeActiveLaneMasks.end(), "Could not find edge %d -> %d", from.value(), to.value()); |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 5829 | return it->second; |
| 5830 | } |
| 5831 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 5832 | VkShaderStageFlagBits SpirvShader::executionModelToStage(spv::ExecutionModel model) |
| 5833 | { |
| 5834 | switch (model) |
| 5835 | { |
| 5836 | case spv::ExecutionModelVertex: return VK_SHADER_STAGE_VERTEX_BIT; |
| 5837 | // case spv::ExecutionModelTessellationControl: return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 5838 | // case spv::ExecutionModelTessellationEvaluation: return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 5839 | // case spv::ExecutionModelGeometry: return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 5840 | case spv::ExecutionModelFragment: return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 5841 | case spv::ExecutionModelGLCompute: return VK_SHADER_STAGE_COMPUTE_BIT; |
| 5842 | // case spv::ExecutionModelKernel: return VkShaderStageFlagBits(0); // Not supported by vulkan. |
| 5843 | // case spv::ExecutionModelTaskNV: return VK_SHADER_STAGE_TASK_BIT_NV; |
| 5844 | // case spv::ExecutionModelMeshNV: return VK_SHADER_STAGE_MESH_BIT_NV; |
| 5845 | // case spv::ExecutionModelRayGenerationNV: return VK_SHADER_STAGE_RAYGEN_BIT_NV; |
| 5846 | // case spv::ExecutionModelIntersectionNV: return VK_SHADER_STAGE_INTERSECTION_BIT_NV; |
| 5847 | // case spv::ExecutionModelAnyHitNV: return VK_SHADER_STAGE_ANY_HIT_BIT_NV; |
| 5848 | // case spv::ExecutionModelClosestHitNV: return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV; |
| 5849 | // case spv::ExecutionModelMissNV: return VK_SHADER_STAGE_MISS_BIT_NV; |
| 5850 | // case spv::ExecutionModelCallableNV: return VK_SHADER_STAGE_CALLABLE_BIT_NV; |
| 5851 | default: |
| 5852 | UNSUPPORTED("ExecutionModel: %d", int(model)); |
| 5853 | return VkShaderStageFlagBits(0); |
| 5854 | } |
| 5855 | } |
| 5856 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 5857 | SpirvRoutine::SpirvRoutine(vk::PipelineLayout const *pipelineLayout) : |
| 5858 | pipelineLayout(pipelineLayout) |
| 5859 | { |
| 5860 | } |
| 5861 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 5862 | } |