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 const vk::VertexInputInterfaceState &vertexInputInterfaceState = pipelineState.getVertexInputInterfaceState();
61 const vk::PreRasterizationState &preRasterizationState = pipelineState.getPreRasterizationState();
62 const vk::FragmentState &fragmentState = pipelineState.getFragmentState();
63 const vk::FragmentOutputInterfaceState &fragmentOutputInterfaceState = pipelineState.getFragmentOutputInterfaceState();
64
65 State state;
66
67 bool vPosZW = (fragmentShader && fragmentShader->hasBuiltinInput(spv::BuiltInFragCoord));
68
69 const VkPolygonMode polygonMode = preRasterizationState.getPolygonMode();
70
71 state.isDrawPoint = vertexInputInterfaceState.isDrawPoint(true, polygonMode);
72 state.isDrawLine = vertexInputInterfaceState.isDrawLine(true, polygonMode);
73 state.isDrawTriangle = vertexInputInterfaceState.isDrawTriangle(true, polygonMode);
74 state.fixedPointDepthBuffer = attachments.depthBuffer && !attachments.depthBuffer->getFormat(VK_IMAGE_ASPECT_DEPTH_BIT).isFloatFormat();
75 state.applyConstantDepthBias = vertexInputInterfaceState.isDrawTriangle(false, polygonMode) && (preRasterizationState.getConstantDepthBias() != 0.0f);
76 state.applySlopeDepthBias = vertexInputInterfaceState.isDrawTriangle(false, polygonMode) && (preRasterizationState.getSlopeDepthBias() != 0.0f);
77 state.applyDepthBiasClamp = vertexInputInterfaceState.isDrawTriangle(false, polygonMode) && (preRasterizationState.getDepthBiasClamp() != 0.0f);
78 state.interpolateZ = fragmentState.depthTestActive(attachments) || vPosZW;
79 state.interpolateW = fragmentShader != nullptr;
80 state.frontFace = preRasterizationState.getFrontFace();
81 state.cullMode = preRasterizationState.getCullMode();
82
83 const bool isBresenhamLine = vertexInputInterfaceState.isDrawLine(true, preRasterizationState.getPolygonMode()) &&
84 preRasterizationState.getLineRasterizationMode() == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT;
85
86 state.multiSampleCount = fragmentOutputInterfaceState.getSampleCount();
87 state.enableMultiSampling = state.multiSampleCount > 1 && !isBresenhamLine;
88
89 state.numClipDistances = vertexShader->getNumOutputClipDistances();
90 state.numCullDistances = vertexShader->getNumOutputCullDistances();
91
92 if(fragmentShader)
93 {
94 for(int interpolant = 0; interpolant < MAX_INTERFACE_COMPONENTS; interpolant++)
95 {
96 state.gradient[interpolant] = fragmentShader->inputs[interpolant];
97 }
98 }
99
100 state.hash = state.computeHash();
101
102 return state;
103 }
104
routine(const State & state)105 SetupProcessor::RoutineType SetupProcessor::routine(const State &state)
106 {
107 auto routine = routineCache->lookup(state);
108
109 if(!routine)
110 {
111 SetupRoutine *generator = new SetupRoutine(state);
112 generator->generate();
113 routine = generator->getRoutine();
114 delete generator;
115
116 routineCache->add(state, routine);
117 }
118
119 return routine;
120 }
121
setRoutineCacheSize(int cacheSize)122 void SetupProcessor::setRoutineCacheSize(int cacheSize)
123 {
124 routineCache = std::make_unique<RoutineCacheType>(clamp(cacheSize, 1, 65536));
125 }
126
127 } // namespace sw
128