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 "Polygon.hpp"
18 #include "Primitive.hpp"
19 #include "Renderer.hpp"
20 #include "Pipeline/Constants.hpp"
21 #include "Pipeline/SetupRoutine.hpp"
22 #include "Pipeline/SpirvShader.hpp"
23 #include "System/Debug.hpp"
24 #include "Vulkan/VkImageView.hpp"
25
26 #include <cstring>
27
28 namespace sw {
29
computeHash()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
operator ==(const State & state) const43 bool SetupProcessor::State::operator==(const State &state) const
44 {
45 if(hash != state.hash)
46 {
47 return false;
48 }
49
50 return *static_cast<const States *>(this) == static_cast<const States &>(state);
51 }
52
SetupProcessor()53 SetupProcessor::SetupProcessor()
54 {
55 setRoutineCacheSize(1024);
56 }
57
update(const vk::GraphicsState & pipelineState,const sw::SpirvShader * fragmentShader,const sw::SpirvShader * vertexShader,const vk::Attachments & attachments) const58 SetupProcessor::State SetupProcessor::update(const vk::GraphicsState &pipelineState, const sw::SpirvShader *fragmentShader, const sw::SpirvShader *vertexShader, const vk::Attachments &attachments) const
59 {
60 State state;
61
62 bool vPosZW = (fragmentShader && fragmentShader->hasBuiltinInput(spv::BuiltInFragCoord));
63
64 state.isDrawPoint = pipelineState.isDrawPoint(true);
65 state.isDrawLine = pipelineState.isDrawLine(true);
66 state.isDrawTriangle = pipelineState.isDrawTriangle(true);
67 state.fixedPointDepthBuffer = attachments.depthBuffer && !attachments.depthBuffer->getFormat(VK_IMAGE_ASPECT_DEPTH_BIT).isFloatFormat();
68 state.applyConstantDepthBias = pipelineState.isDrawTriangle(false) && (pipelineState.getConstantDepthBias() != 0.0f);
69 state.applySlopeDepthBias = pipelineState.isDrawTriangle(false) && (pipelineState.getSlopeDepthBias() != 0.0f);
70 state.applyDepthBiasClamp = pipelineState.isDrawTriangle(false) && (pipelineState.getDepthBiasClamp() != 0.0f);
71 state.interpolateZ = pipelineState.depthBufferActive(attachments) || vPosZW;
72 state.interpolateW = fragmentShader != nullptr;
73 state.frontFace = pipelineState.getFrontFace();
74 state.cullMode = pipelineState.getCullMode();
75
76 state.multiSampleCount = pipelineState.getSampleCount();
77 state.enableMultiSampling = (state.multiSampleCount > 1) &&
78 !(pipelineState.isDrawLine(true) && (pipelineState.getLineRasterizationMode() == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT));
79 state.rasterizerDiscard = pipelineState.hasRasterizerDiscard();
80
81 state.numClipDistances = vertexShader->getNumOutputClipDistances();
82 state.numCullDistances = vertexShader->getNumOutputCullDistances();
83
84 if(fragmentShader)
85 {
86 for(int interpolant = 0; interpolant < MAX_INTERFACE_COMPONENTS; interpolant++)
87 {
88 state.gradient[interpolant] = fragmentShader->inputs[interpolant];
89 }
90 }
91
92 state.hash = state.computeHash();
93
94 return state;
95 }
96
routine(const State & state)97 SetupProcessor::RoutineType SetupProcessor::routine(const State &state)
98 {
99 auto routine = routineCache->lookup(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
setRoutineCacheSize(int cacheSize)114 void SetupProcessor::setRoutineCacheSize(int cacheSize)
115 {
116 routineCache = std::make_unique<RoutineCacheType>(clamp(cacheSize, 1, 65536));
117 }
118
119 } // namespace sw
120