Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 1 | // Copyright 2016 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 | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 15 | #include <Device/Vertex.hpp> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 16 | #include "VertexRoutine.hpp" |
| 17 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 18 | #include "Constants.hpp" |
Nicolas Capens | 1d8c8db | 2018-11-05 16:30:42 -0500 | [diff] [blame] | 19 | #include "Device/Vertex.hpp" |
| 20 | #include "Device/Renderer.hpp" |
| 21 | #include "System/Half.hpp" |
Chris Forbes | ebe5f7f | 2019-01-16 10:38:34 -0800 | [diff] [blame] | 22 | #include "Vulkan/VkDebug.hpp" |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 23 | #include "SpirvShader.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 24 | |
| 25 | namespace sw |
| 26 | { |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 27 | VertexRoutine::VertexRoutine( |
| 28 | const VertexProcessor::State &state, |
| 29 | vk::PipelineLayout const *pipelineLayout, |
| 30 | SpirvShader const *spirvShader) |
| 31 | : routine(pipelineLayout), |
| 32 | state(state), |
Chris Forbes | 1845d5e | 2018-12-27 11:50:15 -0800 | [diff] [blame] | 33 | spirvShader(spirvShader) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 34 | { |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 35 | spirvShader->emitProlog(&routine); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | VertexRoutine::~VertexRoutine() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void VertexRoutine::generate() |
| 43 | { |
| 44 | const bool textureSampling = state.textureSampling; |
| 45 | |
| 46 | Pointer<Byte> cache = task + OFFSET(VertexTask,vertexCache); |
| 47 | Pointer<Byte> vertexCache = cache + OFFSET(VertexCache,vertex); |
| 48 | Pointer<Byte> tagCache = cache + OFFSET(VertexCache,tag); |
| 49 | |
| 50 | UInt vertexCount = *Pointer<UInt>(task + OFFSET(VertexTask,vertexCount)); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 51 | |
| 52 | constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,constants)); |
| 53 | |
| 54 | Do |
| 55 | { |
| 56 | UInt index = *Pointer<UInt>(batch); |
| 57 | UInt tagIndex = index & 0x0000003C; |
| 58 | UInt indexQ = !textureSampling ? UInt(index & 0xFFFFFFFC) : index; // FIXME: TEXLDL hack to have independent LODs, hurts performance. |
| 59 | |
| 60 | If(*Pointer<UInt>(tagCache + tagIndex) != indexQ) |
| 61 | { |
| 62 | *Pointer<UInt>(tagCache + tagIndex) = indexQ; |
| 63 | |
| 64 | readInput(indexQ); |
Nicolas Capens | dc2966a | 2018-10-31 11:25:15 -0400 | [diff] [blame] | 65 | program(indexQ); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 66 | computeClipFlags(); |
| 67 | |
| 68 | Pointer<Byte> cacheLine0 = vertexCache + tagIndex * UInt((int)sizeof(Vertex)); |
| 69 | writeCache(cacheLine0); |
| 70 | } |
| 71 | |
| 72 | UInt cacheIndex = index & 0x0000003F; |
| 73 | Pointer<Byte> cacheLine = vertexCache + cacheIndex * UInt((int)sizeof(Vertex)); |
| 74 | writeVertex(vertex, cacheLine); |
| 75 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 76 | vertex += sizeof(Vertex); |
| 77 | batch += sizeof(unsigned int); |
| 78 | vertexCount--; |
| 79 | } |
| 80 | Until(vertexCount == 0) |
| 81 | |
| 82 | Return(); |
| 83 | } |
| 84 | |
| 85 | void VertexRoutine::readInput(UInt &index) |
| 86 | { |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 87 | for(int i = 0; i < MAX_INTERFACE_COMPONENTS; i += 4) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 88 | { |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 89 | if (spirvShader->inputs[i].Type != SpirvShader::ATTRIBTYPE_UNUSED || |
| 90 | spirvShader->inputs[i + 1].Type != SpirvShader::ATTRIBTYPE_UNUSED || |
| 91 | spirvShader->inputs[i + 2].Type != SpirvShader::ATTRIBTYPE_UNUSED || |
| 92 | spirvShader->inputs[i + 3].Type != SpirvShader::ATTRIBTYPE_UNUSED) |
| 93 | { |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 94 | |
Chris Forbes | fe3d497 | 2019-02-22 17:21:23 -0800 | [diff] [blame] | 95 | Pointer<Byte> input = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, input) + sizeof(void *) * (i/4)); |
| 96 | UInt stride = *Pointer<UInt>(data + OFFSET(DrawData, stride) + sizeof(unsigned int) * (i/4)); |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 97 | |
Chris Forbes | fe3d497 | 2019-02-22 17:21:23 -0800 | [diff] [blame] | 98 | auto value = readStream(input, stride, state.input[i/4], index); |
Chris Forbes | 64be7c7 | 2019-02-19 16:40:57 -0800 | [diff] [blame] | 99 | routine.inputs[i] = value.x; |
| 100 | routine.inputs[i+1] = value.y; |
| 101 | routine.inputs[i+2] = value.z; |
| 102 | routine.inputs[i+3] = value.w; |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 103 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | void VertexRoutine::computeClipFlags() |
| 108 | { |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 109 | auto it = spirvShader->outputBuiltins.find(spv::BuiltInPosition); |
| 110 | assert(it != spirvShader->outputBuiltins.end()); |
| 111 | assert(it->second.SizeInComponents == 4); |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 112 | auto &pos = routine.getVariable(it->second.Id); |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 113 | auto posX = pos[it->second.FirstComponent]; |
| 114 | auto posY = pos[it->second.FirstComponent + 1]; |
| 115 | auto posZ = pos[it->second.FirstComponent + 2]; |
| 116 | auto posW = pos[it->second.FirstComponent + 3]; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 117 | |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 118 | Int4 maxX = CmpLT(posW, posX); |
| 119 | Int4 maxY = CmpLT(posW, posY); |
| 120 | Int4 maxZ = CmpLT(posW, posZ); |
| 121 | Int4 minX = CmpNLE(-posW, posX); |
| 122 | Int4 minY = CmpNLE(-posW, posY); |
| 123 | Int4 minZ = CmpNLE(Float4(0.0f), posZ); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 124 | |
| 125 | clipFlags = *Pointer<Int>(constants + OFFSET(Constants,maxX) + SignMask(maxX) * 4); // FIXME: Array indexing |
| 126 | clipFlags |= *Pointer<Int>(constants + OFFSET(Constants,maxY) + SignMask(maxY) * 4); |
| 127 | clipFlags |= *Pointer<Int>(constants + OFFSET(Constants,maxZ) + SignMask(maxZ) * 4); |
| 128 | clipFlags |= *Pointer<Int>(constants + OFFSET(Constants,minX) + SignMask(minX) * 4); |
| 129 | clipFlags |= *Pointer<Int>(constants + OFFSET(Constants,minY) + SignMask(minY) * 4); |
| 130 | clipFlags |= *Pointer<Int>(constants + OFFSET(Constants,minZ) + SignMask(minZ) * 4); |
| 131 | |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 132 | Int4 finiteX = CmpLE(Abs(posX), *Pointer<Float4>(constants + OFFSET(Constants,maxPos))); |
| 133 | Int4 finiteY = CmpLE(Abs(posY), *Pointer<Float4>(constants + OFFSET(Constants,maxPos))); |
| 134 | Int4 finiteZ = CmpLE(Abs(posZ), *Pointer<Float4>(constants + OFFSET(Constants,maxPos))); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 135 | |
| 136 | Int4 finiteXYZ = finiteX & finiteY & finiteZ; |
| 137 | clipFlags |= *Pointer<Int>(constants + OFFSET(Constants,fini) + SignMask(finiteXYZ) * 4); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Vector4f VertexRoutine::readStream(Pointer<Byte> &buffer, UInt &stride, const Stream &stream, const UInt &index) |
| 141 | { |
| 142 | const bool textureSampling = state.textureSampling; |
| 143 | |
| 144 | Vector4f v; |
| 145 | |
| 146 | Pointer<Byte> source0 = buffer + index * stride; |
| 147 | Pointer<Byte> source1 = source0 + (!textureSampling ? stride : 0); |
| 148 | Pointer<Byte> source2 = source1 + (!textureSampling ? stride : 0); |
| 149 | Pointer<Byte> source3 = source2 + (!textureSampling ? stride : 0); |
| 150 | |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 151 | bool isNativeFloatAttrib = (stream.attribType == SpirvShader::ATTRIBTYPE_FLOAT) || stream.normalized; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 152 | |
| 153 | switch(stream.type) |
| 154 | { |
| 155 | case STREAMTYPE_FLOAT: |
| 156 | { |
| 157 | if(stream.count == 0) |
| 158 | { |
| 159 | // Null stream, all default components |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | if(stream.count == 1) |
| 164 | { |
| 165 | v.x.x = *Pointer<Float>(source0); |
| 166 | v.x.y = *Pointer<Float>(source1); |
| 167 | v.x.z = *Pointer<Float>(source2); |
| 168 | v.x.w = *Pointer<Float>(source3); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | v.x = *Pointer<Float4>(source0); |
| 173 | v.y = *Pointer<Float4>(source1); |
| 174 | v.z = *Pointer<Float4>(source2); |
| 175 | v.w = *Pointer<Float4>(source3); |
| 176 | |
| 177 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 178 | } |
| 179 | |
| 180 | switch(stream.attribType) |
| 181 | { |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 182 | case SpirvShader::ATTRIBTYPE_INT: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 183 | if(stream.count >= 1) v.x = As<Float4>(Int4(v.x)); |
| 184 | if(stream.count >= 2) v.x = As<Float4>(Int4(v.y)); |
| 185 | if(stream.count >= 3) v.x = As<Float4>(Int4(v.z)); |
| 186 | if(stream.count >= 4) v.x = As<Float4>(Int4(v.w)); |
| 187 | break; |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 188 | case SpirvShader::ATTRIBTYPE_UINT: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 189 | if(stream.count >= 1) v.x = As<Float4>(UInt4(v.x)); |
| 190 | if(stream.count >= 2) v.x = As<Float4>(UInt4(v.y)); |
| 191 | if(stream.count >= 3) v.x = As<Float4>(UInt4(v.z)); |
| 192 | if(stream.count >= 4) v.x = As<Float4>(UInt4(v.w)); |
| 193 | break; |
| 194 | default: |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | break; |
| 200 | case STREAMTYPE_BYTE: |
| 201 | if(isNativeFloatAttrib) // Stream: UByte, Shader attrib: Float |
| 202 | { |
| 203 | v.x = Float4(*Pointer<Byte4>(source0)); |
| 204 | v.y = Float4(*Pointer<Byte4>(source1)); |
| 205 | v.z = Float4(*Pointer<Byte4>(source2)); |
| 206 | v.w = Float4(*Pointer<Byte4>(source3)); |
| 207 | |
| 208 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 209 | |
| 210 | if(stream.normalized) |
| 211 | { |
| 212 | if(stream.count >= 1) v.x *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 213 | if(stream.count >= 2) v.y *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 214 | if(stream.count >= 3) v.z *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 215 | if(stream.count >= 4) v.w *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 216 | } |
| 217 | } |
| 218 | else // Stream: UByte, Shader attrib: Int / UInt |
| 219 | { |
| 220 | v.x = As<Float4>(Int4(*Pointer<Byte4>(source0))); |
| 221 | v.y = As<Float4>(Int4(*Pointer<Byte4>(source1))); |
| 222 | v.z = As<Float4>(Int4(*Pointer<Byte4>(source2))); |
| 223 | v.w = As<Float4>(Int4(*Pointer<Byte4>(source3))); |
| 224 | |
| 225 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 226 | } |
| 227 | break; |
| 228 | case STREAMTYPE_SBYTE: |
| 229 | if(isNativeFloatAttrib) // Stream: SByte, Shader attrib: Float |
| 230 | { |
| 231 | v.x = Float4(*Pointer<SByte4>(source0)); |
| 232 | v.y = Float4(*Pointer<SByte4>(source1)); |
| 233 | v.z = Float4(*Pointer<SByte4>(source2)); |
| 234 | v.w = Float4(*Pointer<SByte4>(source3)); |
| 235 | |
| 236 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 237 | |
| 238 | if(stream.normalized) |
| 239 | { |
| 240 | if(stream.count >= 1) v.x *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleSByte)); |
| 241 | if(stream.count >= 2) v.y *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleSByte)); |
| 242 | if(stream.count >= 3) v.z *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleSByte)); |
| 243 | if(stream.count >= 4) v.w *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleSByte)); |
| 244 | } |
| 245 | } |
| 246 | else // Stream: SByte, Shader attrib: Int / UInt |
| 247 | { |
| 248 | v.x = As<Float4>(Int4(*Pointer<SByte4>(source0))); |
| 249 | v.y = As<Float4>(Int4(*Pointer<SByte4>(source1))); |
| 250 | v.z = As<Float4>(Int4(*Pointer<SByte4>(source2))); |
| 251 | v.w = As<Float4>(Int4(*Pointer<SByte4>(source3))); |
| 252 | |
| 253 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 254 | } |
| 255 | break; |
| 256 | case STREAMTYPE_COLOR: |
| 257 | { |
| 258 | v.x = Float4(*Pointer<Byte4>(source0)) * *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 259 | v.y = Float4(*Pointer<Byte4>(source1)) * *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 260 | v.z = Float4(*Pointer<Byte4>(source2)) * *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 261 | v.w = Float4(*Pointer<Byte4>(source3)) * *Pointer<Float4>(constants + OFFSET(Constants,unscaleByte)); |
| 262 | |
| 263 | transpose4x4(v.x, v.y, v.z, v.w); |
| 264 | |
| 265 | // Swap red and blue |
| 266 | Float4 t = v.x; |
| 267 | v.x = v.z; |
| 268 | v.z = t; |
| 269 | } |
| 270 | break; |
| 271 | case STREAMTYPE_SHORT: |
| 272 | if(isNativeFloatAttrib) // Stream: Int, Shader attrib: Float |
| 273 | { |
| 274 | v.x = Float4(*Pointer<Short4>(source0)); |
| 275 | v.y = Float4(*Pointer<Short4>(source1)); |
| 276 | v.z = Float4(*Pointer<Short4>(source2)); |
| 277 | v.w = Float4(*Pointer<Short4>(source3)); |
| 278 | |
| 279 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 280 | |
| 281 | if(stream.normalized) |
| 282 | { |
| 283 | if(stream.count >= 1) v.x *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleShort)); |
| 284 | if(stream.count >= 2) v.y *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleShort)); |
| 285 | if(stream.count >= 3) v.z *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleShort)); |
| 286 | if(stream.count >= 4) v.w *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleShort)); |
| 287 | } |
| 288 | } |
| 289 | else // Stream: Short, Shader attrib: Int/UInt, no type conversion |
| 290 | { |
| 291 | v.x = As<Float4>(Int4(*Pointer<Short4>(source0))); |
| 292 | v.y = As<Float4>(Int4(*Pointer<Short4>(source1))); |
| 293 | v.z = As<Float4>(Int4(*Pointer<Short4>(source2))); |
| 294 | v.w = As<Float4>(Int4(*Pointer<Short4>(source3))); |
| 295 | |
| 296 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 297 | } |
| 298 | break; |
| 299 | case STREAMTYPE_USHORT: |
| 300 | if(isNativeFloatAttrib) // Stream: Int, Shader attrib: Float |
| 301 | { |
| 302 | v.x = Float4(*Pointer<UShort4>(source0)); |
| 303 | v.y = Float4(*Pointer<UShort4>(source1)); |
| 304 | v.z = Float4(*Pointer<UShort4>(source2)); |
| 305 | v.w = Float4(*Pointer<UShort4>(source3)); |
| 306 | |
| 307 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 308 | |
| 309 | if(stream.normalized) |
| 310 | { |
| 311 | if(stream.count >= 1) v.x *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleUShort)); |
| 312 | if(stream.count >= 2) v.y *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleUShort)); |
| 313 | if(stream.count >= 3) v.z *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleUShort)); |
| 314 | if(stream.count >= 4) v.w *= *Pointer<Float4>(constants + OFFSET(Constants,unscaleUShort)); |
| 315 | } |
| 316 | } |
| 317 | else // Stream: UShort, Shader attrib: Int/UInt, no type conversion |
| 318 | { |
| 319 | v.x = As<Float4>(Int4(*Pointer<UShort4>(source0))); |
| 320 | v.y = As<Float4>(Int4(*Pointer<UShort4>(source1))); |
| 321 | v.z = As<Float4>(Int4(*Pointer<UShort4>(source2))); |
| 322 | v.w = As<Float4>(Int4(*Pointer<UShort4>(source3))); |
| 323 | |
| 324 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 325 | } |
| 326 | break; |
| 327 | case STREAMTYPE_INT: |
| 328 | if(isNativeFloatAttrib) // Stream: Int, Shader attrib: Float |
| 329 | { |
| 330 | v.x = Float4(*Pointer<Int4>(source0)); |
| 331 | v.y = Float4(*Pointer<Int4>(source1)); |
| 332 | v.z = Float4(*Pointer<Int4>(source2)); |
| 333 | v.w = Float4(*Pointer<Int4>(source3)); |
| 334 | |
| 335 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 336 | |
| 337 | if(stream.normalized) |
| 338 | { |
| 339 | if(stream.count >= 1) v.x *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleInt)); |
| 340 | if(stream.count >= 2) v.y *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleInt)); |
| 341 | if(stream.count >= 3) v.z *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleInt)); |
| 342 | if(stream.count >= 4) v.w *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleInt)); |
| 343 | } |
| 344 | } |
| 345 | else // Stream: Int, Shader attrib: Int/UInt, no type conversion |
| 346 | { |
| 347 | v.x = *Pointer<Float4>(source0); |
| 348 | v.y = *Pointer<Float4>(source1); |
| 349 | v.z = *Pointer<Float4>(source2); |
| 350 | v.w = *Pointer<Float4>(source3); |
| 351 | |
| 352 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 353 | } |
| 354 | break; |
| 355 | case STREAMTYPE_UINT: |
| 356 | if(isNativeFloatAttrib) // Stream: UInt, Shader attrib: Float |
| 357 | { |
| 358 | v.x = Float4(*Pointer<UInt4>(source0)); |
| 359 | v.y = Float4(*Pointer<UInt4>(source1)); |
| 360 | v.z = Float4(*Pointer<UInt4>(source2)); |
| 361 | v.w = Float4(*Pointer<UInt4>(source3)); |
| 362 | |
| 363 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 364 | |
| 365 | if(stream.normalized) |
| 366 | { |
| 367 | if(stream.count >= 1) v.x *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleUInt)); |
| 368 | if(stream.count >= 2) v.y *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleUInt)); |
| 369 | if(stream.count >= 3) v.z *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleUInt)); |
| 370 | if(stream.count >= 4) v.w *= *Pointer<Float4>(constants + OFFSET(Constants, unscaleUInt)); |
| 371 | } |
| 372 | } |
| 373 | else // Stream: UInt, Shader attrib: Int/UInt, no type conversion |
| 374 | { |
| 375 | v.x = *Pointer<Float4>(source0); |
| 376 | v.y = *Pointer<Float4>(source1); |
| 377 | v.z = *Pointer<Float4>(source2); |
| 378 | v.w = *Pointer<Float4>(source3); |
| 379 | |
| 380 | transpose4xN(v.x, v.y, v.z, v.w, stream.count); |
| 381 | } |
| 382 | break; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 383 | case STREAMTYPE_HALF: |
| 384 | { |
| 385 | if(stream.count >= 1) |
| 386 | { |
| 387 | UShort x0 = *Pointer<UShort>(source0 + 0); |
| 388 | UShort x1 = *Pointer<UShort>(source1 + 0); |
| 389 | UShort x2 = *Pointer<UShort>(source2 + 0); |
| 390 | UShort x3 = *Pointer<UShort>(source3 + 0); |
| 391 | |
| 392 | v.x.x = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(x0) * 4); |
| 393 | v.x.y = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(x1) * 4); |
| 394 | v.x.z = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(x2) * 4); |
| 395 | v.x.w = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(x3) * 4); |
| 396 | } |
| 397 | |
| 398 | if(stream.count >= 2) |
| 399 | { |
| 400 | UShort y0 = *Pointer<UShort>(source0 + 2); |
| 401 | UShort y1 = *Pointer<UShort>(source1 + 2); |
| 402 | UShort y2 = *Pointer<UShort>(source2 + 2); |
| 403 | UShort y3 = *Pointer<UShort>(source3 + 2); |
| 404 | |
| 405 | v.y.x = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(y0) * 4); |
| 406 | v.y.y = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(y1) * 4); |
| 407 | v.y.z = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(y2) * 4); |
| 408 | v.y.w = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(y3) * 4); |
| 409 | } |
| 410 | |
| 411 | if(stream.count >= 3) |
| 412 | { |
| 413 | UShort z0 = *Pointer<UShort>(source0 + 4); |
| 414 | UShort z1 = *Pointer<UShort>(source1 + 4); |
| 415 | UShort z2 = *Pointer<UShort>(source2 + 4); |
| 416 | UShort z3 = *Pointer<UShort>(source3 + 4); |
| 417 | |
| 418 | v.z.x = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(z0) * 4); |
| 419 | v.z.y = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(z1) * 4); |
| 420 | v.z.z = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(z2) * 4); |
| 421 | v.z.w = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(z3) * 4); |
| 422 | } |
| 423 | |
| 424 | if(stream.count >= 4) |
| 425 | { |
| 426 | UShort w0 = *Pointer<UShort>(source0 + 6); |
| 427 | UShort w1 = *Pointer<UShort>(source1 + 6); |
| 428 | UShort w2 = *Pointer<UShort>(source2 + 6); |
| 429 | UShort w3 = *Pointer<UShort>(source3 + 6); |
| 430 | |
| 431 | v.w.x = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(w0) * 4); |
| 432 | v.w.y = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(w1) * 4); |
| 433 | v.w.z = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(w2) * 4); |
| 434 | v.w.w = *Pointer<Float>(constants + OFFSET(Constants,half2float) + Int(w3) * 4); |
| 435 | } |
| 436 | } |
| 437 | break; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 438 | case STREAMTYPE_2_10_10_10_INT: |
| 439 | { |
| 440 | Int4 src; |
| 441 | src = Insert(src, *Pointer<Int>(source0), 0); |
| 442 | src = Insert(src, *Pointer<Int>(source1), 1); |
| 443 | src = Insert(src, *Pointer<Int>(source2), 2); |
| 444 | src = Insert(src, *Pointer<Int>(source3), 3); |
| 445 | |
| 446 | v.x = Float4((src << 22) >> 22); |
| 447 | v.y = Float4((src << 12) >> 22); |
| 448 | v.z = Float4((src << 02) >> 22); |
| 449 | v.w = Float4(src >> 30); |
| 450 | |
| 451 | if(stream.normalized) |
| 452 | { |
| 453 | v.x = Max(v.x * Float4(1.0f / 0x1FF), Float4(-1.0f)); |
| 454 | v.y = Max(v.y * Float4(1.0f / 0x1FF), Float4(-1.0f)); |
| 455 | v.z = Max(v.z * Float4(1.0f / 0x1FF), Float4(-1.0f)); |
| 456 | v.w = Max(v.w, Float4(-1.0f)); |
| 457 | } |
| 458 | } |
| 459 | break; |
| 460 | case STREAMTYPE_2_10_10_10_UINT: |
| 461 | { |
| 462 | Int4 src; |
| 463 | src = Insert(src, *Pointer<Int>(source0), 0); |
| 464 | src = Insert(src, *Pointer<Int>(source1), 1); |
| 465 | src = Insert(src, *Pointer<Int>(source2), 2); |
| 466 | src = Insert(src, *Pointer<Int>(source3), 3); |
| 467 | |
| 468 | v.x = Float4(src & Int4(0x3FF)); |
| 469 | v.y = Float4((src >> 10) & Int4(0x3FF)); |
| 470 | v.z = Float4((src >> 20) & Int4(0x3FF)); |
| 471 | v.w = Float4((src >> 30) & Int4(0x3)); |
| 472 | |
| 473 | if(stream.normalized) |
| 474 | { |
| 475 | v.x *= Float4(1.0f / 0x3FF); |
| 476 | v.y *= Float4(1.0f / 0x3FF); |
| 477 | v.z *= Float4(1.0f / 0x3FF); |
| 478 | v.w *= Float4(1.0f / 0x3); |
| 479 | } |
| 480 | } |
| 481 | break; |
| 482 | default: |
| 483 | ASSERT(false); |
| 484 | } |
| 485 | |
| 486 | if(stream.count < 1) v.x = Float4(0.0f); |
| 487 | if(stream.count < 2) v.y = Float4(0.0f); |
| 488 | if(stream.count < 3) v.z = Float4(0.0f); |
| 489 | if(stream.count < 4) v.w = isNativeFloatAttrib ? As<Float4>(Float4(1.0f)) : As<Float4>(Int4(0)); |
| 490 | |
| 491 | return v; |
| 492 | } |
| 493 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 494 | void VertexRoutine::writeCache(Pointer<Byte> &cacheLine) |
| 495 | { |
| 496 | Vector4f v; |
| 497 | |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 498 | for (int i = 0; i < MAX_INTERFACE_COMPONENTS; i += 4) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 499 | { |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 500 | if (spirvShader->outputs[i].Type != SpirvShader::ATTRIBTYPE_UNUSED || |
| 501 | spirvShader->outputs[i+1].Type != SpirvShader::ATTRIBTYPE_UNUSED || |
| 502 | spirvShader->outputs[i+2].Type != SpirvShader::ATTRIBTYPE_UNUSED || |
| 503 | spirvShader->outputs[i+3].Type != SpirvShader::ATTRIBTYPE_UNUSED) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 504 | { |
Chris Forbes | 64be7c7 | 2019-02-19 16:40:57 -0800 | [diff] [blame] | 505 | v.x = routine.outputs[i]; |
| 506 | v.y = routine.outputs[i+1]; |
| 507 | v.z = routine.outputs[i+2]; |
| 508 | v.w = routine.outputs[i+3]; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 509 | |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 510 | transpose4x4(v.x, v.y, v.z, v.w); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 511 | |
Chris Forbes | 7e6fff2 | 2019-02-10 20:18:50 +0000 | [diff] [blame] | 512 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 0, 16) = v.x; |
| 513 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 1, 16) = v.y; |
| 514 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 2, 16) = v.z; |
| 515 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,v[i]) + sizeof(Vertex) * 3, 16) = v.w; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 0) = (clipFlags >> 0) & 0x0000000FF; |
| 520 | *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 1) = (clipFlags >> 8) & 0x0000000FF; |
| 521 | *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 2) = (clipFlags >> 16) & 0x0000000FF; |
| 522 | *Pointer<Int>(cacheLine + OFFSET(Vertex,clipFlags) + sizeof(Vertex) * 3) = (clipFlags >> 24) & 0x0000000FF; |
| 523 | |
| 524 | // Viewport transform |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 525 | auto it = spirvShader->outputBuiltins.find(spv::BuiltInPosition); |
| 526 | assert(it != spirvShader->outputBuiltins.end()); |
| 527 | assert(it->second.SizeInComponents == 4); |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 528 | auto &pos = routine.getVariable(it->second.Id); |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 529 | auto posX = pos[it->second.FirstComponent]; |
| 530 | auto posY = pos[it->second.FirstComponent + 1]; |
| 531 | auto posZ = pos[it->second.FirstComponent + 2]; |
| 532 | auto posW = pos[it->second.FirstComponent + 3]; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 533 | |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 534 | v.x = posX; |
| 535 | v.y = posY; |
| 536 | v.z = posZ; |
| 537 | v.w = posW; |
| 538 | |
| 539 | // Write the builtin pos into the vertex; it's not going to be consumed by the FS, but may need to reproject if we have to clip. |
| 540 | Vector4f v2 = v; |
| 541 | transpose4x4(v2.x, v2.y, v2.z, v2.w); |
| 542 | |
| 543 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,builtins.position) + sizeof(Vertex) * 0, 16) = v2.x; |
| 544 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,builtins.position) + sizeof(Vertex) * 1, 16) = v2.y; |
| 545 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,builtins.position) + sizeof(Vertex) * 2, 16) = v2.z; |
| 546 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,builtins.position) + sizeof(Vertex) * 3, 16) = v2.w; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 547 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 548 | Float4 w = As<Float4>(As<Int4>(v.w) | (As<Int4>(CmpEQ(v.w, Float4(0.0f))) & As<Int4>(Float4(1.0f)))); |
| 549 | Float4 rhw = Float4(1.0f) / w; |
| 550 | |
| 551 | v.x = As<Float4>(RoundInt(*Pointer<Float4>(data + OFFSET(DrawData,X0x16)) + v.x * rhw * *Pointer<Float4>(data + OFFSET(DrawData,Wx16)))); |
| 552 | v.y = As<Float4>(RoundInt(*Pointer<Float4>(data + OFFSET(DrawData,Y0x16)) + v.y * rhw * *Pointer<Float4>(data + OFFSET(DrawData,Hx16)))); |
| 553 | v.z = v.z * rhw; |
| 554 | v.w = rhw; |
| 555 | |
| 556 | transpose4x4(v.x, v.y, v.z, v.w); |
| 557 | |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 558 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,projected) + sizeof(Vertex) * 0, 16) = v.x; |
| 559 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,projected) + sizeof(Vertex) * 1, 16) = v.y; |
| 560 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,projected) + sizeof(Vertex) * 2, 16) = v.z; |
| 561 | *Pointer<Float4>(cacheLine + OFFSET(Vertex,projected) + sizeof(Vertex) * 3, 16) = v.w; |
Chris Forbes | 54c4772 | 2019-02-25 13:35:59 -0800 | [diff] [blame] | 562 | |
| 563 | it = spirvShader->outputBuiltins.find(spv::BuiltInPointSize); |
| 564 | if (it != spirvShader->outputBuiltins.end()) |
| 565 | { |
| 566 | assert(it->second.SizeInComponents == 1); |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 567 | auto psize = routine.getVariable(it->second.Id)[it->second.FirstComponent]; |
Chris Forbes | 54c4772 | 2019-02-25 13:35:59 -0800 | [diff] [blame] | 568 | *Pointer<Float>(cacheLine + OFFSET(Vertex,builtins.pointSize) + sizeof(Vertex) * 0) = Extract(psize, 0); |
| 569 | *Pointer<Float>(cacheLine + OFFSET(Vertex,builtins.pointSize) + sizeof(Vertex) * 1) = Extract(psize, 1); |
| 570 | *Pointer<Float>(cacheLine + OFFSET(Vertex,builtins.pointSize) + sizeof(Vertex) * 2) = Extract(psize, 2); |
| 571 | *Pointer<Float>(cacheLine + OFFSET(Vertex,builtins.pointSize) + sizeof(Vertex) * 3) = Extract(psize, 3); |
| 572 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void VertexRoutine::writeVertex(const Pointer<Byte> &vertex, Pointer<Byte> &cache) |
| 576 | { |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 577 | for(int i = 0; i < MAX_INTERFACE_COMPONENTS; i++) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 578 | { |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 579 | if(spirvShader->outputs[i].Type != SpirvShader::ATTRIBTYPE_UNUSED) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 580 | { |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 581 | *Pointer<Int>(vertex + OFFSET(Vertex, v[i]), 4) = *Pointer<Int>(cache + OFFSET(Vertex, v[i]), 4); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 585 | *Pointer<Int4>(vertex + OFFSET(Vertex,projected)) = *Pointer<Int4>(cache + OFFSET(Vertex,projected)); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 586 | *Pointer<Int>(vertex + OFFSET(Vertex,clipFlags)) = *Pointer<Int>(cache + OFFSET(Vertex,clipFlags)); |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 587 | *Pointer<Int4>(vertex + OFFSET(Vertex,builtins.position)) = *Pointer<Int4>(cache + OFFSET(Vertex,builtins.position)); |
| 588 | *Pointer<Int>(vertex + OFFSET(Vertex,builtins.pointSize)) = *Pointer<Int>(cache + OFFSET(Vertex,builtins.pointSize)); |
| 589 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 590 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 591 | } |