| 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 | |
| 17 | #include "SamplerCore.hpp" |
| 18 | #include "Device/Renderer.hpp" |
| 19 | #include "Device/Vertex.hpp" |
| 20 | #include "System/Half.hpp" |
| 21 | #include "Vulkan/VkDebug.hpp" |
| 22 | |
| 23 | #include "Vulkan/VkPipelineLayout.hpp" |
| 24 | |
| 25 | namespace sw |
| 26 | { |
| 27 | VertexProgram::VertexProgram( |
| 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) |
| 34 | { |
| 35 | routine.setImmutableInputBuiltins(spirvShader); |
| 36 | |
| 37 | routine.setInputBuiltin(spirvShader, spv::BuiltInViewIndex, [&](const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value) |
| 38 | { |
| 39 | assert(builtin.SizeInComponents == 1); |
| 40 | value[builtin.FirstComponent] = As<Float4>(Int4((*Pointer<Int>(data + OFFSET(DrawData, viewID))))); |
| 41 | }); |
| 42 | |
| 43 | routine.setInputBuiltin(spirvShader, spv::BuiltInInstanceIndex, [&](const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value) |
| 44 | { |
| 45 | // TODO: we could do better here; we know InstanceIndex is uniform across all lanes |
| 46 | assert(builtin.SizeInComponents == 1); |
| 47 | value[builtin.FirstComponent] = As<Float4>(Int4((*Pointer<Int>(data + OFFSET(DrawData, instanceID))))); |
| 48 | }); |
| 49 | |
| 50 | routine.setInputBuiltin(spirvShader, spv::BuiltInSubgroupSize, [&](const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value) |
| 51 | { |
| 52 | ASSERT(builtin.SizeInComponents == 1); |
| 53 | value[builtin.FirstComponent] = As<SIMD::Float>(SIMD::Int(SIMD::Width)); |
| 54 | }); |
| 55 | |
| 56 | routine.descriptorSets = data + OFFSET(DrawData, descriptorSets); |
| 57 | routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets); |
| 58 | routine.pushConstants = data + OFFSET(DrawData, pushConstants); |
| 59 | routine.constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, constants)); |
| 60 | } |
| 61 | |
| 62 | VertexProgram::~VertexProgram() |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void VertexProgram::program(Pointer<UInt> &batch, UInt& vertexCount) |
| 67 | { |
| 68 | auto it = spirvShader->inputBuiltins.find(spv::BuiltInVertexIndex); |
| 69 | if (it != spirvShader->inputBuiltins.end()) |
| 70 | { |
| 71 | assert(it->second.SizeInComponents == 1); |
| 72 | |
| 73 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = |
| 74 | As<Float4>(*Pointer<Int4>(As<Pointer<Int4>>(batch)) + |
| 75 | Int4(*Pointer<Int>(data + OFFSET(DrawData, baseVertex)))); |
| 76 | } |
| 77 | |
| 78 | auto activeLaneMask = SIMD::Int(0xFFFFFFFF); |
| 79 | Int4 storesAndAtomicsMask = CmpGE(UInt4(vertexCount), UInt4(1, 2, 3, 4)); |
| 80 | spirvShader->emit(&routine, activeLaneMask, storesAndAtomicsMask, descriptorSets); |
| 81 | |
| 82 | spirvShader->emitEpilog(&routine); |
| 83 | } |
| 84 | } |
| 85 | |