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