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 "VertexProcessor.hpp" |
| 16 | |
Nicolas Capens | 1d8c8db | 2018-11-05 16:30:42 -0500 | [diff] [blame] | 17 | #include "Pipeline/Constants.hpp" |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 18 | #include "Pipeline/VertexProgram.hpp" |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 19 | #include "System/Debug.hpp" |
Nicolas Capens | 1d8c8db | 2018-11-05 16:30:42 -0500 | [diff] [blame] | 20 | #include "System/Math.hpp" |
Nicolas Capens | 5ab1f36 | 2020-04-22 01:39:36 -0400 | [diff] [blame] | 21 | #include "Vulkan/VkPipelineLayout.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 22 | |
Nicolas Capens | e899321 | 2019-06-21 12:21:08 -0400 | [diff] [blame] | 23 | #include <cstring> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 25 | namespace sw { |
| 26 | |
| 27 | void VertexCache::clear() |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 28 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 29 | for(uint32_t i = 0; i < SIZE; i++) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 30 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 31 | tag[i] = 0xFFFFFFFF; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 32 | } |
| 33 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 34 | |
| 35 | uint32_t VertexProcessor::States::computeHash() |
| 36 | { |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 37 | uint32_t *state = reinterpret_cast<uint32_t *>(this); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 38 | uint32_t hash = 0; |
| 39 | |
| 40 | for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++) |
| 41 | { |
| 42 | hash ^= state[i]; |
| 43 | } |
| 44 | |
| 45 | return hash; |
| 46 | } |
| 47 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 48 | bool VertexProcessor::State::operator==(const State &state) const |
| 49 | { |
| 50 | if(hash != state.hash) |
| 51 | { |
| 52 | return false; |
| 53 | } |
| 54 | |
Nicolas Capens | 1885f69 | 2020-03-23 11:28:44 -0400 | [diff] [blame] | 55 | return *static_cast<const States *>(this) == static_cast<const States &>(state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | VertexProcessor::VertexProcessor() |
| 59 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 60 | setRoutineCacheSize(1024); |
| 61 | } |
| 62 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 63 | void VertexProcessor::setRoutineCacheSize(int cacheSize) |
| 64 | { |
Ben Clayton | 197e251 | 2020-04-12 16:35:12 +0100 | [diff] [blame] | 65 | routineCache = std::make_unique<RoutineCacheType>(clamp(cacheSize, 1, 65536)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 68 | const VertexProcessor::State VertexProcessor::update(const sw::Context *context) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 69 | { |
| 70 | State state; |
| 71 | |
| 72 | state.shaderID = context->vertexShader->getSerialID(); |
Nicolas Capens | 5ab1f36 | 2020-04-22 01:39:36 -0400 | [diff] [blame] | 73 | state.pipelineLayoutIdentifier = context->pipelineLayout->identifier; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 74 | state.robustBufferAccess = context->robustBufferAccess; |
| 75 | state.isPoint = context->topology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 76 | |
| 77 | for(int i = 0; i < MAX_INTERFACE_COMPONENTS / 4; i++) |
| 78 | { |
Alexis Hetu | b766e5e | 2020-01-20 11:40:28 -0500 | [diff] [blame] | 79 | state.input[i].format = context->input[i].format; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 80 | // TODO: get rid of attribType -- just keep the VK format all the way through, this fully determines |
| 81 | // how to handle the attribute. |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 82 | state.input[i].attribType = context->vertexShader->inputs[i * 4].Type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | state.hash = state.computeHash(); |
| 86 | |
| 87 | return state; |
| 88 | } |
| 89 | |
| 90 | VertexProcessor::RoutineType VertexProcessor::routine(const State &state, |
| 91 | vk::PipelineLayout const *pipelineLayout, |
| 92 | SpirvShader const *vertexShader, |
| 93 | const vk::DescriptorSet::Bindings &descriptorSets) |
| 94 | { |
Ben Clayton | ac43aa7 | 2020-04-04 00:48:13 +0100 | [diff] [blame] | 95 | auto routine = routineCache->lookup(state); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 96 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 97 | if(!routine) // Create one |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 98 | { |
| 99 | VertexRoutine *generator = new VertexProgram(state, pipelineLayout, vertexShader, descriptorSets); |
| 100 | generator->generate(); |
| 101 | routine = (*generator)("VertexRoutine_%0.8X", state.shaderID); |
| 102 | delete generator; |
| 103 | |
| 104 | routineCache->add(state, routine); |
| 105 | } |
| 106 | |
| 107 | return routine; |
| 108 | } |
| 109 | |
| 110 | } // namespace sw |