| 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 "SetupProcessor.hpp" | 
|---|
| 16 |  | 
|---|
| 17 | #include "Primitive.hpp" | 
|---|
| 18 | #include "Polygon.hpp" | 
|---|
| 19 | #include "Context.hpp" | 
|---|
| 20 | #include "Renderer.hpp" | 
|---|
| 21 | #include "Pipeline/SetupRoutine.hpp" | 
|---|
| 22 | #include "Pipeline/Constants.hpp" | 
|---|
| 23 | #include "Vulkan/VkDebug.hpp" | 
|---|
| 24 | #include "Pipeline/SpirvShader.hpp" | 
|---|
| 25 |  | 
|---|
| 26 | #include <cstring> | 
|---|
| 27 |  | 
|---|
| 28 | namespace sw | 
|---|
| 29 | { | 
|---|
| 30 | uint32_t SetupProcessor::States::computeHash() | 
|---|
| 31 | { | 
|---|
| 32 | uint32_t *state = reinterpret_cast<uint32_t*>(this); | 
|---|
| 33 | uint32_t hash = 0; | 
|---|
| 34 |  | 
|---|
| 35 | for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++) | 
|---|
| 36 | { | 
|---|
| 37 | hash ^= state[i]; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | return hash; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | bool SetupProcessor::State::operator==(const State &state) const | 
|---|
| 44 | { | 
|---|
| 45 | if(hash != state.hash) | 
|---|
| 46 | { | 
|---|
| 47 | return false; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | static_assert(is_memcmparable<State>::value, "Cannot memcmp States"); | 
|---|
| 51 | return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | SetupProcessor::SetupProcessor() | 
|---|
| 55 | { | 
|---|
| 56 | routineCache = nullptr; | 
|---|
| 57 | setRoutineCacheSize(1024); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | SetupProcessor::~SetupProcessor() | 
|---|
| 61 | { | 
|---|
| 62 | delete routineCache; | 
|---|
| 63 | routineCache = nullptr; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | SetupProcessor::State SetupProcessor::update(const sw::Context* context) const | 
|---|
| 67 | { | 
|---|
| 68 | State state; | 
|---|
| 69 |  | 
|---|
| 70 | bool vPosZW = (context->pixelShader && context->pixelShader->hasBuiltinInput(spv::BuiltInFragCoord)); | 
|---|
| 71 |  | 
|---|
| 72 | state.isDrawPoint = context->isDrawPoint(true); | 
|---|
| 73 | state.isDrawLine = context->isDrawLine(true); | 
|---|
| 74 | state.isDrawTriangle = context->isDrawTriangle(true); | 
|---|
| 75 | state.applySlopeDepthBias = context->isDrawTriangle(false) && (context->slopeDepthBias != 0.0f); | 
|---|
| 76 | state.interpolateZ = context->depthBufferActive() || vPosZW; | 
|---|
| 77 | state.interpolateW = context->pixelShader != nullptr; | 
|---|
| 78 | state.frontFace = context->frontFace; | 
|---|
| 79 | state.cullMode = context->cullMode; | 
|---|
| 80 |  | 
|---|
| 81 | state.multiSample = context->sampleCount; | 
|---|
| 82 | state.rasterizerDiscard = context->rasterizerDiscard; | 
|---|
| 83 |  | 
|---|
| 84 | if (context->pixelShader) | 
|---|
| 85 | { | 
|---|
| 86 | for (int interpolant = 0; interpolant < MAX_INTERFACE_COMPONENTS; interpolant++) | 
|---|
| 87 | { | 
|---|
| 88 | state.gradient[interpolant] = context->pixelShader->inputs[interpolant]; | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | state.hash = state.computeHash(); | 
|---|
| 93 |  | 
|---|
| 94 | return state; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | SetupProcessor::RoutineType SetupProcessor::routine(const State &state) | 
|---|
| 98 | { | 
|---|
| 99 | auto routine = routineCache->query(state); | 
|---|
| 100 |  | 
|---|
| 101 | if(!routine) | 
|---|
| 102 | { | 
|---|
| 103 | SetupRoutine *generator = new SetupRoutine(state); | 
|---|
| 104 | generator->generate(); | 
|---|
| 105 | routine = generator->getRoutine(); | 
|---|
| 106 | delete generator; | 
|---|
| 107 |  | 
|---|
| 108 | routineCache->add(state, routine); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | return routine; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void SetupProcessor::setRoutineCacheSize(int cacheSize) | 
|---|
| 115 | { | 
|---|
| 116 | delete routineCache; | 
|---|
| 117 | routineCache = new RoutineCacheType(clamp(cacheSize, 1, 65536)); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|