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 | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 25 | namespace sw |
| 26 | { |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 27 | VertexProgram::VertexProgram( |
| 28 | const VertexProcessor::State &state, |
| 29 | vk::PipelineLayout const *pipelineLayout, |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 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 | { |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 35 | auto it = spirvShader->inputBuiltins.find(spv::BuiltInInstanceIndex); |
| 36 | if (it != spirvShader->inputBuiltins.end()) |
| 37 | { |
| 38 | // TODO: we could do better here; we know InstanceIndex is uniform across all lanes |
| 39 | assert(it->second.SizeInComponents == 1); |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 40 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = |
Chris Forbes | 26f1a86 | 2019-02-02 15:23:01 -0800 | [diff] [blame] | 41 | As<Float4>(Int4((*Pointer<Int>(data + OFFSET(DrawData, instanceID))))); |
| 42 | } |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 43 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 44 | routine.descriptorSets = data + OFFSET(DrawData, descriptorSets); |
| 45 | routine.descriptorDynamicOffsets = data + OFFSET(DrawData, descriptorDynamicOffsets); |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 46 | routine.pushConstants = data + OFFSET(DrawData, pushConstants); |
Chris Forbes | 548e366 | 2019-04-25 10:00:06 -0700 | [diff] [blame] | 47 | routine.constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, constants)); |
Chris Forbes | 84c3a94 | 2019-04-16 09:48:00 -0700 | [diff] [blame] | 48 | |
| 49 | it = spirvShader->inputBuiltins.find(spv::BuiltInSubgroupSize); |
| 50 | if (it != spirvShader->inputBuiltins.end()) |
| 51 | { |
| 52 | ASSERT(it->second.SizeInComponents == 1); |
Chris Forbes | 793050e | 2019-04-27 14:48:09 -0700 | [diff] [blame] | 53 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<SIMD::Float>(SIMD::Int(SIMD::Width)); |
Chris Forbes | 84c3a94 | 2019-04-16 09:48:00 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | it = spirvShader->inputBuiltins.find(spv::BuiltInSubgroupLocalInvocationId); |
| 57 | if (it != spirvShader->inputBuiltins.end()) |
| 58 | { |
| 59 | ASSERT(it->second.SizeInComponents == 1); |
| 60 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<SIMD::Float>(SIMD::Int(0, 1, 2, 3)); |
| 61 | } |
Ben Clayton | 2cd983d | 2019-05-10 11:30:09 +0100 | [diff] [blame] | 62 | |
| 63 | it = spirvShader->inputBuiltins.find(spv::BuiltInDeviceIndex); |
| 64 | if (it != spirvShader->inputBuiltins.end()) |
| 65 | { |
| 66 | ASSERT(it->second.SizeInComponents == 1); |
| 67 | // Only a single physical device is supported. |
| 68 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = As<SIMD::Float>(SIMD::Int(0, 0, 0, 0)); |
| 69 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | VertexProgram::~VertexProgram() |
| 73 | { |
| 74 | } |
| 75 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 76 | void VertexProgram::program(UInt &index) |
| 77 | { |
Chris Forbes | d007720 | 2019-02-10 19:52:08 +0000 | [diff] [blame] | 78 | auto it = spirvShader->inputBuiltins.find(spv::BuiltInVertexIndex); |
| 79 | if (it != spirvShader->inputBuiltins.end()) |
| 80 | { |
| 81 | assert(it->second.SizeInComponents == 1); |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 82 | routine.getVariable(it->second.Id)[it->second.FirstComponent] = |
Chris Forbes | d4e5e9e | 2019-04-15 12:43:38 -0700 | [diff] [blame] | 83 | As<Float4>(Int4(As<Int>(index) + *Pointer<Int>(data + OFFSET(DrawData, baseVertex))) + Int4(0, 1, 2, 3)); |
Chris Forbes | d007720 | 2019-02-10 19:52:08 +0000 | [diff] [blame] | 84 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 85 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 86 | auto activeLaneMask = SIMD::Int(0xFFFFFFFF); // TODO: Control this. |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 87 | spirvShader->emit(&routine, activeLaneMask, descriptorSets); |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 88 | |
| 89 | spirvShader->emitEpilog(&routine); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 90 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 91 | } |