John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1 | // SwiftShader Software Renderer |
| 2 | // |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3 | // Copyright(c) 2005-2012 TransGaming Inc. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4 | // |
| 5 | // All rights reserved. No part of this software may be copied, distributed, transmitted, |
| 6 | // transcribed, stored in a retrieval system, translated into any human or computer |
| 7 | // language by any means, or disclosed to third parties without the explicit written |
| 8 | // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express |
| 9 | // or implied, including but not limited to any patent rights, are granted to you. |
| 10 | // |
| 11 | |
| 12 | #include "SetupProcessor.hpp" |
| 13 | |
| 14 | #include "SetupRoutine.hpp" |
| 15 | #include "Primitive.hpp" |
| 16 | #include "Polygon.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 17 | #include "Context.hpp" |
| 18 | #include "Renderer.hpp" |
| 19 | #include "Constants.hpp" |
| 20 | #include "Debug.hpp" |
| 21 | |
| 22 | namespace sw |
| 23 | { |
| 24 | extern bool complementaryDepthBuffer; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 25 | extern bool fullPixelPositionRegister; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 26 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 27 | bool precacheSetup = false; |
| 28 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 29 | unsigned int SetupProcessor::States::computeHash() |
| 30 | { |
| 31 | unsigned int *state = (unsigned int*)this; |
| 32 | unsigned int hash = 0; |
| 33 | |
Nicolas Capens | 5d96188 | 2016-01-01 23:18:14 -0500 | [diff] [blame] | 34 | for(unsigned int i = 0; i < sizeof(States) / 4; i++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 35 | { |
| 36 | hash ^= state[i]; |
| 37 | } |
| 38 | |
| 39 | return hash; |
| 40 | } |
| 41 | |
| 42 | SetupProcessor::State::State(int i) |
| 43 | { |
| 44 | memset(this, 0, sizeof(State)); |
| 45 | } |
| 46 | |
| 47 | bool SetupProcessor::State::operator==(const State &state) const |
| 48 | { |
| 49 | if(hash != state.hash) |
| 50 | { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0; |
| 55 | } |
| 56 | |
| 57 | SetupProcessor::SetupProcessor(Context *context) : context(context) |
| 58 | { |
| 59 | routineCache = 0; |
| 60 | setRoutineCacheSize(1024); |
| 61 | } |
| 62 | |
| 63 | SetupProcessor::~SetupProcessor() |
| 64 | { |
| 65 | delete routineCache; |
| 66 | routineCache = 0; |
| 67 | } |
| 68 | |
| 69 | SetupProcessor::State SetupProcessor::update() const |
| 70 | { |
| 71 | State state; |
| 72 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 73 | bool vPosZW = (context->pixelShader && context->pixelShader->vPosDeclared && fullPixelPositionRegister); |
| 74 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 75 | state.isDrawPoint = context->isDrawPoint(true); |
| 76 | state.isDrawLine = context->isDrawLine(true); |
| 77 | state.isDrawTriangle = context->isDrawTriangle(false); |
| 78 | state.isDrawSolidTriangle = context->isDrawTriangle(true); |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 79 | state.interpolateZ = context->depthBufferActive() || context->pixelFogActive() != FOG_NONE || vPosZW; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 80 | state.interpolateW = context->perspectiveActive() || vPosZW; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 81 | state.perspective = context->perspectiveActive(); |
| 82 | state.pointSprite = context->pointSpriteActive(); |
| 83 | state.cullMode = context->cullMode; |
| 84 | state.twoSidedStencil = context->stencilActive() && context->twoSidedStencil; |
| 85 | state.slopeDepthBias = slopeDepthBias != 0.0f; |
| 86 | state.vFace = context->pixelShader && context->pixelShader->vFaceDeclared; |
| 87 | |
| 88 | state.positionRegister = Pos; |
| 89 | state.pointSizeRegister = 0xF; // No vertex point size |
| 90 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 91 | state.multiSample = context->getMultiSampleCount(); |
Alexis Hetu | b0f247f | 2016-02-22 11:42:39 -0500 | [diff] [blame^] | 92 | state.rasterizerDiscard = context->rasterizerDiscard; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 93 | |
| 94 | if(context->vertexShader) |
| 95 | { |
| 96 | state.positionRegister = context->vertexShader->positionRegister; |
| 97 | state.pointSizeRegister = context->vertexShader->pointSizeRegister; |
| 98 | } |
| 99 | else if(context->pointSizeActive()) |
| 100 | { |
| 101 | state.pointSizeRegister = Pts; |
| 102 | } |
| 103 | |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 104 | for(int interpolant = 0; interpolant < 10; interpolant++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 105 | { |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 106 | for(int component = 0; component < 4; component++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 107 | { |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 108 | state.gradient[interpolant][component].attribute = Unused; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 109 | state.gradient[interpolant][component].flat = false; |
| 110 | state.gradient[interpolant][component].wrap = false; |
| 111 | } |
| 112 | } |
| 113 | |
Nicolas Capens | 66be245 | 2015-01-27 14:58:57 -0500 | [diff] [blame] | 114 | state.fog.attribute = Unused; |
| 115 | state.fog.flat = false; |
| 116 | state.fog.wrap = false; |
| 117 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 118 | const bool point = context->isDrawPoint(true); |
| 119 | const bool sprite = context->pointSpriteActive(); |
Nicolas Capens | a0f4be8 | 2014-10-22 14:35:30 -0400 | [diff] [blame] | 120 | const bool flatShading = (context->shadingMode == SHADING_FLAT) || point; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 121 | |
| 122 | if(context->vertexShader && context->pixelShader) |
| 123 | { |
| 124 | for(int interpolant = 0; interpolant < 10; interpolant++) |
| 125 | { |
| 126 | for(int component = 0; component < 4; component++) |
| 127 | { |
| 128 | int project = context->isProjectionComponent(interpolant - 2, component) ? 1 : 0; |
| 129 | |
| 130 | if(context->pixelShader->semantic[interpolant][component - project].active()) |
| 131 | { |
| 132 | int input = interpolant; |
| 133 | for(int i = 0; i < 12; i++) |
| 134 | { |
| 135 | if(context->pixelShader->semantic[interpolant][component - project] == context->vertexShader->output[i][component - project]) |
| 136 | { |
| 137 | input = i; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | bool flat = point; |
| 143 | |
| 144 | switch(context->pixelShader->semantic[interpolant][component - project].usage) |
| 145 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 146 | case Shader::USAGE_TEXCOORD: flat = point && !sprite; break; |
| 147 | case Shader::USAGE_COLOR: flat = flatShading; break; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | state.gradient[interpolant][component].attribute = input; |
| 151 | state.gradient[interpolant][component].flat = flat; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | else if(context->preTransformed && context->pixelShader) |
| 157 | { |
| 158 | for(int interpolant = 0; interpolant < 10; interpolant++) |
| 159 | { |
| 160 | for(int component = 0; component < 4; component++) |
| 161 | { |
| 162 | int index = context->pixelShader->semantic[interpolant][component].index; |
| 163 | |
| 164 | switch(context->pixelShader->semantic[interpolant][component].usage) |
| 165 | { |
| 166 | case 0xFF: |
| 167 | break; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 168 | case Shader::USAGE_TEXCOORD: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 169 | state.gradient[interpolant][component].attribute = T0 + index; |
| 170 | state.gradient[interpolant][component].flat = point && !sprite; |
| 171 | break; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 172 | case Shader::USAGE_COLOR: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 173 | state.gradient[interpolant][component].attribute = D0 + index; |
| 174 | state.gradient[interpolant][component].flat = flatShading; |
| 175 | break; |
| 176 | default: |
| 177 | ASSERT(false); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | else if(context->pixelShaderVersion() < 0x0300) |
| 183 | { |
| 184 | for(int coordinate = 0; coordinate < 8; coordinate++) |
| 185 | { |
| 186 | for(int component = 0; component < 4; component++) |
| 187 | { |
| 188 | if(context->textureActive(coordinate, component)) |
| 189 | { |
| 190 | state.texture[coordinate][component].attribute = T0 + coordinate; |
| 191 | state.texture[coordinate][component].flat = point && !sprite; |
| 192 | state.texture[coordinate][component].wrap = (context->textureWrap[coordinate] & (1 << component)) != 0; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | for(int color = 0; color < 2; color++) |
| 198 | { |
| 199 | for(int component = 0; component < 4; component++) |
| 200 | { |
| 201 | if(context->colorActive(color, component)) |
| 202 | { |
| 203 | state.color[color][component].attribute = D0 + color; |
| 204 | state.color[color][component].flat = flatShading; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | else ASSERT(false); |
| 210 | |
| 211 | if(context->fogActive()) |
| 212 | { |
| 213 | state.fog.attribute = Fog; |
| 214 | state.fog.flat = point; |
| 215 | } |
| 216 | |
| 217 | state.hash = state.computeHash(); |
| 218 | |
| 219 | return state; |
| 220 | } |
| 221 | |
| 222 | Routine *SetupProcessor::routine(const State &state) |
| 223 | { |
| 224 | Routine *routine = routineCache->query(state); |
| 225 | |
| 226 | if(!routine) |
| 227 | { |
| 228 | SetupRoutine *generator = new SetupRoutine(state); |
| 229 | generator->generate(); |
| 230 | routine = generator->getRoutine(); |
| 231 | delete generator; |
| 232 | |
| 233 | routineCache->add(state, routine); |
| 234 | } |
| 235 | |
| 236 | return routine; |
| 237 | } |
| 238 | |
| 239 | void SetupProcessor::setRoutineCacheSize(int cacheSize) |
| 240 | { |
| 241 | delete routineCache; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 242 | routineCache = new RoutineCache<State>(clamp(cacheSize, 1, 65536), precacheSetup ? "sw-setup" : 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 243 | } |
| 244 | } |