Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
| 15 | #include "SetupProcessor.hpp" |
| 16 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 17 | #include "Primitive.hpp" |
| 18 | #include "Polygon.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 19 | #include "Context.hpp" |
| 20 | #include "Renderer.hpp" |
Nicolas Capens | 708c24b | 2017-10-26 13:07:10 -0400 | [diff] [blame] | 21 | #include "Shader/SetupRoutine.hpp" |
| 22 | #include "Shader/Constants.hpp" |
| 23 | #include "Common/Debug.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 25 | #include <cstring> |
| 26 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 27 | namespace sw |
| 28 | { |
| 29 | extern bool complementaryDepthBuffer; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 30 | extern bool fullPixelPositionRegister; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 31 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 32 | bool precacheSetup = false; |
| 33 | |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 34 | uint32_t SetupProcessor::States::computeHash() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 35 | { |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 36 | uint32_t *state = reinterpret_cast<uint32_t*>(this); |
| 37 | uint32_t hash = 0; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 38 | |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 39 | for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 40 | { |
| 41 | hash ^= state[i]; |
| 42 | } |
| 43 | |
| 44 | return hash; |
| 45 | } |
| 46 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 47 | bool SetupProcessor::State::operator==(const State &state) const |
| 48 | { |
| 49 | if(hash != state.hash) |
| 50 | { |
| 51 | return false; |
| 52 | } |
| 53 | |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 54 | static_assert(is_memcmparable<State>::value, "Cannot memcmp States"); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 55 | return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0; |
| 56 | } |
| 57 | |
| 58 | SetupProcessor::SetupProcessor(Context *context) : context(context) |
| 59 | { |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 60 | routineCache = nullptr; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 61 | setRoutineCacheSize(1024); |
| 62 | } |
| 63 | |
| 64 | SetupProcessor::~SetupProcessor() |
| 65 | { |
| 66 | delete routineCache; |
Nicolas Capens | 92eb041 | 2019-08-20 14:13:17 -0400 | [diff] [blame] | 67 | routineCache = nullptr; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SetupProcessor::State SetupProcessor::update() const |
| 71 | { |
| 72 | State state; |
| 73 | |
Alexis Hetu | 02ad0aa | 2016-08-02 11:18:14 -0400 | [diff] [blame] | 74 | bool vPosZW = (context->pixelShader && context->pixelShader->isVPosDeclared() && fullPixelPositionRegister); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 75 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 76 | state.isDrawPoint = context->isDrawPoint(true); |
| 77 | state.isDrawLine = context->isDrawLine(true); |
| 78 | state.isDrawTriangle = context->isDrawTriangle(false); |
| 79 | state.isDrawSolidTriangle = context->isDrawTriangle(true); |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 80 | state.interpolateZ = context->depthBufferActive() || context->pixelFogActive() != FOG_NONE || vPosZW; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 81 | state.interpolateW = context->perspectiveActive() || vPosZW; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 82 | state.perspective = context->perspectiveActive(); |
| 83 | state.pointSprite = context->pointSpriteActive(); |
| 84 | state.cullMode = context->cullMode; |
| 85 | state.twoSidedStencil = context->stencilActive() && context->twoSidedStencil; |
Nicolas Capens | 3cbeac5 | 2017-09-15 11:49:31 -0400 | [diff] [blame] | 86 | state.slopeDepthBias = context->slopeDepthBias != 0.0f; |
Alexis Hetu | 02ad0aa | 2016-08-02 11:18:14 -0400 | [diff] [blame] | 87 | state.vFace = context->pixelShader && context->pixelShader->isVFaceDeclared(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 88 | |
| 89 | state.positionRegister = Pos; |
Nicolas Capens | ec0936c | 2016-05-18 12:32:02 -0400 | [diff] [blame] | 90 | state.pointSizeRegister = Unused; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 91 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 92 | state.multiSample = context->getMultiSampleCount(); |
Alexis Hetu | b0f247f | 2016-02-22 11:42:39 -0500 | [diff] [blame] | 93 | state.rasterizerDiscard = context->rasterizerDiscard; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 94 | |
| 95 | if(context->vertexShader) |
| 96 | { |
Alexis Hetu | 02ad0aa | 2016-08-02 11:18:14 -0400 | [diff] [blame] | 97 | state.positionRegister = context->vertexShader->getPositionRegister(); |
| 98 | state.pointSizeRegister = context->vertexShader->getPointSizeRegister(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 99 | } |
| 100 | else if(context->pointSizeActive()) |
| 101 | { |
| 102 | state.pointSizeRegister = Pts; |
| 103 | } |
| 104 | |
Nicolas Capens | 3b4c93f | 2016-05-18 12:51:37 -0400 | [diff] [blame] | 105 | for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 106 | { |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 107 | for(int component = 0; component < 4; component++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 108 | { |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 109 | state.gradient[interpolant][component].attribute = Unused; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 110 | state.gradient[interpolant][component].flat = false; |
| 111 | state.gradient[interpolant][component].wrap = false; |
| 112 | } |
| 113 | } |
| 114 | |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 115 | state.fog.attribute = Unused; |
| 116 | state.fog.flat = false; |
| 117 | state.fog.wrap = false; |
| 118 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 119 | const bool point = context->isDrawPoint(true); |
| 120 | const bool sprite = context->pointSpriteActive(); |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 121 | const bool flatShading = (context->shadingMode == SHADING_FLAT) || point; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 122 | |
| 123 | if(context->vertexShader && context->pixelShader) |
| 124 | { |
Nicolas Capens | 3b4c93f | 2016-05-18 12:51:37 -0400 | [diff] [blame] | 125 | for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 126 | { |
| 127 | for(int component = 0; component < 4; component++) |
| 128 | { |
| 129 | int project = context->isProjectionComponent(interpolant - 2, component) ? 1 : 0; |
Alexis Hetu | 02ad0aa | 2016-08-02 11:18:14 -0400 | [diff] [blame] | 130 | const Shader::Semantic& semantic = context->pixelShader->getInput(interpolant, component - project); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 131 | |
Alexis Hetu | 12b0050 | 2016-05-20 13:01:11 -0400 | [diff] [blame] | 132 | if(semantic.active()) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 133 | { |
| 134 | int input = interpolant; |
Nicolas Capens | ec0936c | 2016-05-18 12:32:02 -0400 | [diff] [blame] | 135 | for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 136 | { |
Alexis Hetu | 02ad0aa | 2016-08-02 11:18:14 -0400 | [diff] [blame] | 137 | if(semantic == context->vertexShader->getOutput(i, component - project)) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 138 | { |
| 139 | input = i; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool flat = point; |
| 145 | |
Alexis Hetu | 12b0050 | 2016-05-20 13:01:11 -0400 | [diff] [blame] | 146 | switch(semantic.usage) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 147 | { |
Alexis Hetu | 12b0050 | 2016-05-20 13:01:11 -0400 | [diff] [blame] | 148 | case Shader::USAGE_TEXCOORD: flat = point && !sprite; break; |
| 149 | case Shader::USAGE_COLOR: flat = semantic.flat || flatShading; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | state.gradient[interpolant][component].attribute = input; |
| 153 | state.gradient[interpolant][component].flat = flat; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | else if(context->preTransformed && context->pixelShader) |
| 159 | { |
Nicolas Capens | 3b4c93f | 2016-05-18 12:51:37 -0400 | [diff] [blame] | 160 | for(int interpolant = 0; interpolant < MAX_FRAGMENT_INPUTS; interpolant++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 161 | { |
| 162 | for(int component = 0; component < 4; component++) |
| 163 | { |
Alexis Hetu | 02ad0aa | 2016-08-02 11:18:14 -0400 | [diff] [blame] | 164 | const Shader::Semantic& semantic = context->pixelShader->getInput(interpolant, component); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 165 | |
Alexis Hetu | 12b0050 | 2016-05-20 13:01:11 -0400 | [diff] [blame] | 166 | switch(semantic.usage) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 167 | { |
| 168 | case 0xFF: |
| 169 | break; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 170 | case Shader::USAGE_TEXCOORD: |
Alexis Hetu | 12b0050 | 2016-05-20 13:01:11 -0400 | [diff] [blame] | 171 | state.gradient[interpolant][component].attribute = T0 + semantic.index; |
| 172 | state.gradient[interpolant][component].flat = semantic.flat || (point && !sprite); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 173 | break; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 174 | case Shader::USAGE_COLOR: |
Alexis Hetu | 12b0050 | 2016-05-20 13:01:11 -0400 | [diff] [blame] | 175 | state.gradient[interpolant][component].attribute = C0 + semantic.index; |
| 176 | state.gradient[interpolant][component].flat = semantic.flat || flatShading; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | ASSERT(false); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
Alexis Hetu | 53ad4af | 2017-12-06 14:49:07 -0500 | [diff] [blame] | 184 | else if(context->pixelShaderModel() < 0x0300) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 185 | { |
| 186 | for(int coordinate = 0; coordinate < 8; coordinate++) |
| 187 | { |
| 188 | for(int component = 0; component < 4; component++) |
| 189 | { |
| 190 | if(context->textureActive(coordinate, component)) |
| 191 | { |
| 192 | state.texture[coordinate][component].attribute = T0 + coordinate; |
| 193 | state.texture[coordinate][component].flat = point && !sprite; |
| 194 | state.texture[coordinate][component].wrap = (context->textureWrap[coordinate] & (1 << component)) != 0; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | for(int color = 0; color < 2; color++) |
| 200 | { |
| 201 | for(int component = 0; component < 4; component++) |
| 202 | { |
| 203 | if(context->colorActive(color, component)) |
| 204 | { |
Nicolas Capens | 995ddea | 2016-05-17 11:48:56 -0400 | [diff] [blame] | 205 | state.color[color][component].attribute = C0 + color; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 206 | state.color[color][component].flat = flatShading; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | else ASSERT(false); |
| 212 | |
| 213 | if(context->fogActive()) |
| 214 | { |
| 215 | state.fog.attribute = Fog; |
| 216 | state.fog.flat = point; |
| 217 | } |
| 218 | |
| 219 | state.hash = state.computeHash(); |
| 220 | |
| 221 | return state; |
| 222 | } |
| 223 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 224 | std::shared_ptr<Routine> SetupProcessor::routine(const State &state) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 225 | { |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 226 | auto routine = routineCache->query(state); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 227 | |
| 228 | if(!routine) |
| 229 | { |
| 230 | SetupRoutine *generator = new SetupRoutine(state); |
| 231 | generator->generate(); |
| 232 | routine = generator->getRoutine(); |
| 233 | delete generator; |
| 234 | |
| 235 | routineCache->add(state, routine); |
| 236 | } |
| 237 | |
| 238 | return routine; |
| 239 | } |
| 240 | |
| 241 | void SetupProcessor::setRoutineCacheSize(int cacheSize) |
| 242 | { |
| 243 | delete routineCache; |
Alexis Hetu | f287834 | 2019-03-12 16:32:04 -0400 | [diff] [blame] | 244 | routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 245 | } |
| 246 | } |