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 "PixelProcessor.hpp"
16
17 #include "Primitive.hpp"
18 #include "Pipeline/Constants.hpp"
19 #include "Pipeline/PixelProgram.hpp"
20 #include "System/Debug.hpp"
21 #include "Vulkan/VkImageView.hpp"
22 #include "Vulkan/VkPipelineLayout.hpp"
23
24 #include <cstring>
25
26 namespace sw {
27
computeHash()28 uint32_t PixelProcessor::States::computeHash()
29 {
30 uint32_t *state = reinterpret_cast<uint32_t *>(this);
31 uint32_t hash = 0;
32
33 for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
34 {
35 hash ^= state[i];
36 }
37
38 return hash;
39 }
40
operator ==(const State & state) const41 bool PixelProcessor::State::operator==(const State &state) const
42 {
43 if(hash != state.hash)
44 {
45 return false;
46 }
47
48 return *static_cast<const States *>(this) == static_cast<const States &>(state);
49 }
50
PixelProcessor()51 PixelProcessor::PixelProcessor()
52 {
53 setRoutineCacheSize(1024);
54 }
55
setBlendConstant(const float4 & blendConstant)56 void PixelProcessor::setBlendConstant(const float4 &blendConstant)
57 {
58 for(int i = 0; i < 4; i++)
59 {
60 factor.blendConstantF[i] = blendConstant[i];
61 factor.invBlendConstantF[i] = 1.0f - blendConstant[i];
62 factor.blendConstantU[i] = clamp(blendConstant[i], 0.0f, 1.0f);
63 factor.invBlendConstantU[i] = 1.0f - clamp(blendConstant[i], 0.0f, 1.0f);
64 factor.blendConstantS[i] = clamp(blendConstant[i], -1.0f, 1.0f);
65 factor.invBlendConstantS[i] = 1.0f - clamp(blendConstant[i], -1.0f, 1.0f);
66 }
67 }
68
setRoutineCacheSize(int cacheSize)69 void PixelProcessor::setRoutineCacheSize(int cacheSize)
70 {
71 routineCache = std::make_unique<RoutineCacheType>(clamp(cacheSize, 1, 65536));
72 }
73
update(const vk::GraphicsState & pipelineState,const sw::SpirvShader * fragmentShader,const sw::SpirvShader * vertexShader,const vk::Attachments & attachments,bool occlusionEnabled) const74 const PixelProcessor::State PixelProcessor::update(const vk::GraphicsState &pipelineState, const sw::SpirvShader *fragmentShader, const sw::SpirvShader *vertexShader, const vk::Attachments &attachments, bool occlusionEnabled) const
75 {
76 State state;
77
78 state.numClipDistances = vertexShader->getNumOutputClipDistances();
79 state.numCullDistances = vertexShader->getNumOutputCullDistances();
80
81 if(fragmentShader)
82 {
83 state.shaderID = fragmentShader->getIdentifier();
84 state.pipelineLayoutIdentifier = pipelineState.getPipelineLayout()->identifier;
85 }
86 else
87 {
88 state.shaderID = 0;
89 state.pipelineLayoutIdentifier = 0;
90 }
91
92 state.alphaToCoverage = pipelineState.hasAlphaToCoverage();
93 state.depthWriteEnable = pipelineState.depthWriteActive(attachments);
94
95 if(pipelineState.stencilActive(attachments))
96 {
97 state.stencilActive = true;
98 state.frontStencil = pipelineState.getFrontStencil();
99 state.backStencil = pipelineState.getBackStencil();
100 }
101
102 state.depthFormat = attachments.depthFormat();
103 state.depthBoundsTestActive = pipelineState.depthBoundsTestActive(attachments);
104 state.minDepthBounds = pipelineState.getMinDepthBounds();
105 state.maxDepthBounds = pipelineState.getMaxDepthBounds();
106
107 if(pipelineState.depthTestActive(attachments))
108 {
109 state.depthTestActive = true;
110 state.depthCompareMode = pipelineState.getDepthCompareMode();
111
112 state.depthBias = (pipelineState.getConstantDepthBias() != 0.0f) || (pipelineState.getSlopeDepthBias() != 0.0f);
113
114 bool pipelineDepthClamp = pipelineState.getDepthClampEnable();
115 // "For fixed-point depth buffers, fragment depth values are always limited to the range [0,1] by clamping after depth bias addition is performed.
116 // Unless the VK_EXT_depth_range_unrestricted extension is enabled, fragment depth values are clamped even when the depth buffer uses a floating-point representation."
117 state.depthClamp = pipelineDepthClamp || !state.depthFormat.isFloatFormat() || !pipelineState.hasDepthRangeUnrestricted();
118
119 if(pipelineDepthClamp)
120 {
121 const VkViewport viewport = pipelineState.getViewport();
122 state.minDepthClamp = min(viewport.minDepth, viewport.maxDepth);
123 state.maxDepthClamp = max(viewport.minDepth, viewport.maxDepth);
124 }
125 else if(state.depthClamp)
126 {
127 state.minDepthClamp = 0.0f;
128 state.maxDepthClamp = 1.0f;
129 }
130 }
131
132 state.occlusionEnabled = occlusionEnabled;
133
134 bool fragmentContainsDiscard = (fragmentShader && fragmentShader->getAnalysis().ContainsDiscard);
135 for(int i = 0; i < MAX_COLOR_BUFFERS; i++)
136 {
137 state.colorWriteMask |= pipelineState.colorWriteActive(i, attachments) << (4 * i);
138 state.colorFormat[i] = attachments.colorFormat(i);
139 state.blendState[i] = pipelineState.getBlendState(i, attachments, fragmentContainsDiscard);
140 }
141
142 state.multiSampleCount = static_cast<unsigned int>(pipelineState.getSampleCount());
143 state.multiSampleMask = pipelineState.getMultiSampleMask();
144 state.enableMultiSampling = (state.multiSampleCount > 1) &&
145 !(pipelineState.isDrawLine(true) && (pipelineState.getLineRasterizationMode() == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT));
146
147 // SampleId and SamplePosition require per-sample fragment shader invocations, so the Vulkan spec
148 // requires turning on sample shading if either of them is present in the shader:
149 // "If a fragment shader entry point's interface includes an input variable decorated with SampleId,
150 // Sample Shading is considered enabled with a minSampleShading value of 1.0."
151 // "If a fragment shader entry point's interface includes an input variable decorated with SamplePosition,
152 // Sample Shading is considered enabled with a minSampleShading value of 1.0."
153 bool shaderContainsSampleDecoration = fragmentShader && (fragmentShader->hasBuiltinInput(spv::BuiltInSampleId) ||
154 fragmentShader->hasBuiltinInput(spv::BuiltInSamplePosition));
155
156 if(shaderContainsSampleDecoration)
157 {
158 state.sampleShadingEnabled = true;
159 state.minSampleShading = 1.0f;
160 }
161 else
162 {
163 state.sampleShadingEnabled = pipelineState.hasSampleShadingEnabled();
164 state.minSampleShading = pipelineState.getMinSampleShading();
165 }
166
167 if(state.enableMultiSampling && fragmentShader)
168 {
169 state.centroid = fragmentShader->getAnalysis().NeedsCentroid;
170 }
171
172 state.frontFace = pipelineState.getFrontFace();
173
174 state.hash = state.computeHash();
175
176 return state;
177 }
178
routine(const State & state,const vk::PipelineLayout * pipelineLayout,const SpirvShader * pixelShader,const vk::DescriptorSet::Bindings & descriptorSets)179 PixelProcessor::RoutineType PixelProcessor::routine(const State &state,
180 const vk::PipelineLayout *pipelineLayout,
181 const SpirvShader *pixelShader,
182 const vk::DescriptorSet::Bindings &descriptorSets)
183 {
184 auto routine = routineCache->lookup(state);
185
186 if(!routine)
187 {
188 QuadRasterizer *generator = new PixelProgram(state, pipelineLayout, pixelShader, descriptorSets);
189 generator->generate();
190 routine = (*generator)("PixelRoutine_%0.8X", state.shaderID);
191 delete generator;
192
193 routineCache->add(state, routine);
194 }
195
196 return routine;
197 }
198
199 } // namespace sw
200