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 | |
| 15 | #include "VertexProgram.hpp" |
| 16 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 17 | #include "SamplerCore.hpp" |
Nicolas Capens | 1d8c8db | 2018-11-05 16:30:42 -0500 | [diff] [blame] | 18 | #include "Device/Renderer.hpp" |
| 19 | #include "Device/Vertex.hpp" |
| 20 | #include "System/Half.hpp" |
Chris Forbes | ebe5f7f | 2019-01-16 10:38:34 -0800 | [diff] [blame] | 21 | #include "Vulkan/VkDebug.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 22 | |
Chris Forbes | c296806 | 2019-03-19 16:48:03 -0700 | [diff] [blame] | 23 | #include "Vulkan/VkPipelineLayout.hpp" |
| 24 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 25 | namespace sw { |
| 26 | |
| 27 | VertexProgram::VertexProgram( |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 28 | const VertexProcessor::State &state, |
| 29 | vk::PipelineLayout const *pipelineLayout, |
| 30 | SpirvShader const *spirvShader, |
| 31 | const vk::DescriptorSet::Bindings &descriptorSets) |
| 32 | : VertexRoutine(state, pipelineLayout, spirvShader) |
| 33 | , descriptorSets(descriptorSets) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 34 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 35 | routine.setImmutableInputBuiltins(spirvShader); |
| 36 | |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 37 | // TODO(b/146486064): Consider only assigning these to the SpirvRoutine iff |
| 38 | // they are ever going to be read. |
| 39 | routine.viewID = *Pointer<Int>(data + OFFSET(DrawData, viewID)); |
| 40 | routine.instanceID = *Pointer<Int>(data + OFFSET(DrawData, instanceID)); |
| 41 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 42 | routine.setInputBuiltin(spirvShader, spv::BuiltInViewIndex, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 43 | assert(builtin.SizeInComponents == 1); |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 44 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine.viewID)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 45 | }); |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 46 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 47 | routine.setInputBuiltin(spirvShader, spv::BuiltInInstanceIndex, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 48 | // TODO: we could do better here; we know InstanceIndex is uniform across all lanes |
| 49 | assert(builtin.SizeInComponents == 1); |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 50 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(routine.instanceID)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 51 | }); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 52 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 53 | routine.setInputBuiltin(spirvShader, spv::BuiltInSubgroupSize, [&](const SpirvShader::BuiltinMapping &builtin, Array<SIMD::Float> &value) { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 54 | ASSERT(builtin.SizeInComponents == 1); |
| 55 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(SIMD::Width)); |
| 56 | }); |
Nicolas Capens | 4a10559 | 2018-01-02 23:41:25 -0500 | [diff] [blame] | 57 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 58 | routine.descriptorSets = data + OFFSET(DrawData, descriptorSets); |
| 59 | routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets); |
| 60 | routine.pushConstants = data + OFFSET(DrawData, pushConstants); |
| 61 | routine.constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, constants)); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 62 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 63 | |
| 64 | VertexProgram::~VertexProgram() |
| 65 | { |
| 66 | } |
| 67 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 68 | void VertexProgram::program(Pointer<UInt> &batch, UInt &vertexCount) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 69 | { |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 70 | routine.vertexIndex = *Pointer<SIMD::Int>(As<Pointer<SIMD::Int>>(batch)) + |
| 71 | SIMD::Int(*Pointer<Int>(data + OFFSET(DrawData, baseVertex))); |
| 72 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 73 | auto it = spirvShader->inputBuiltins.find(spv::BuiltInVertexIndex); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 74 | if(it != spirvShader->inputBuiltins.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 75 | { |
| 76 | assert(it->second.SizeInComponents == 1); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 77 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 78 | As<SIMD::Float>(routine.vertexIndex); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | auto activeLaneMask = SIMD::Int(0xFFFFFFFF); |
| 82 | Int4 storesAndAtomicsMask = CmpGE(UInt4(vertexCount), UInt4(1, 2, 3, 4)); |
| 83 | spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets); |
| 84 | |
| 85 | spirvShader->emitEpilog(&routine); |
| 86 | } |
| 87 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 88 | } // namespace sw |