| 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 | #ifndef sw_VertexProcessor_hpp |
| 16 | #define sw_VertexProcessor_hpp |
| 17 | |
| 18 | #include "Context.hpp" |
| 19 | #include "Matrix.hpp" |
| 20 | #include "Memset.hpp" |
| 21 | #include "RoutineCache.hpp" |
| 22 | #include "Vertex.hpp" |
| 23 | #include "Pipeline/SpirvShader.hpp" |
| 24 | |
| 25 | namespace sw |
| 26 | { |
| 27 | struct DrawData; |
| 28 | |
| 29 | // Basic direct mapped vertex cache. |
| 30 | struct VertexCache |
| 31 | { |
| 32 | static constexpr uint32_t SIZE = 64; // TODO: Variable size? |
| 33 | static constexpr uint32_t TAG_MASK = SIZE - 1; // Size must be power of 2. |
| 34 | |
| 35 | void clear(); |
| 36 | |
| 37 | Vertex vertex[SIZE]; |
| 38 | uint32_t tag[SIZE]; |
| 39 | |
| 40 | // Identifier of the draw call for the cache data. If this cache is |
| 41 | // used with a different draw call, then the cache should be invalidated |
| 42 | // before use. |
| 43 | int drawCall = -1; |
| 44 | }; |
| 45 | |
| 46 | struct VertexTask |
| 47 | { |
| 48 | unsigned int vertexCount; |
| 49 | unsigned int primitiveStart; |
| 50 | VertexCache vertexCache; |
| 51 | }; |
| 52 | |
| 53 | using VertexRoutineFunction = FunctionT<void(Vertex* output, unsigned int* batch, VertexTask* vertextask, DrawData* draw)>; |
| 54 | |
| 55 | class VertexProcessor |
| 56 | { |
| 57 | public: |
| 58 | struct States : Memset<States> |
| 59 | { |
| 60 | States() : Memset(this, 0) {} |
| 61 | |
| 62 | uint32_t computeHash(); |
| 63 | |
| 64 | uint64_t shaderID; |
| 65 | |
| 66 | struct Input |
| 67 | { |
| 68 | operator bool() const // Returns true if stream contains data |
| 69 | { |
| 70 | return count != 0; |
| 71 | } |
| 72 | |
| 73 | unsigned int bytesPerAttrib() const; |
| 74 | |
| 75 | StreamType type : BITS(STREAMTYPE_LAST); |
| 76 | unsigned int count : 3; |
| 77 | bool normalized : 1; |
| 78 | unsigned int attribType : BITS(SpirvShader::ATTRIBTYPE_LAST); |
| 79 | }; |
| 80 | |
| 81 | Input input[MAX_INTERFACE_COMPONENTS / 4]; |
| 82 | bool robustBufferAccess : 1; |
| 83 | bool isPoint : 1; |
| 84 | }; |
| 85 | |
| 86 | struct State : States |
| 87 | { |
| 88 | bool operator==(const State &state) const; |
| 89 | |
| 90 | uint32_t hash; |
| 91 | }; |
| 92 | |
| 93 | using RoutineType = VertexRoutineFunction::RoutineType; |
| 94 | |
| 95 | VertexProcessor(); |
| 96 | |
| 97 | virtual ~VertexProcessor(); |
| 98 | |
| 99 | protected: |
| 100 | const State update(const sw::Context* context); |
| 101 | RoutineType routine(const State &state, vk::PipelineLayout const *pipelineLayout, |
| 102 | SpirvShader const *vertexShader, const vk::DescriptorSet::Bindings &descriptorSets); |
| 103 | |
| 104 | void setRoutineCacheSize(int cacheSize); |
| 105 | |
| 106 | private: |
| 107 | using RoutineCacheType = RoutineCacheT<State, VertexRoutineFunction::CFunctionType>; |
| 108 | RoutineCacheType *routineCache; |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | #endif // sw_VertexProcessor_hpp |
| 113 | |