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 const vk::VertexInputInterfaceState &vertexInputInterfaceState = pipelineState.getVertexInputInterfaceState();
77 const vk::PreRasterizationState &preRasterizationState = pipelineState.getPreRasterizationState();
78 const vk::FragmentState &fragmentState = pipelineState.getFragmentState();
79 const vk::FragmentOutputInterfaceState &fragmentOutputInterfaceState = pipelineState.getFragmentOutputInterfaceState();
80
81 State state;
82
83 state.numClipDistances = vertexShader->getNumOutputClipDistances();
84 state.numCullDistances = vertexShader->getNumOutputCullDistances();
85
86 if(fragmentShader)
87 {
88 state.shaderID = fragmentShader->getIdentifier();
89 state.pipelineLayoutIdentifier = fragmentState.getPipelineLayout()->identifier;
90 state.robustBufferAccess = fragmentShader->getRobustBufferAccess();
91 }
92 else
93 {
94 state.shaderID = 0;
95 state.pipelineLayoutIdentifier = 0;
96 state.robustBufferAccess = false;
97 }
98
99 state.alphaToCoverage = fragmentOutputInterfaceState.hasAlphaToCoverage();
100 state.depthWriteEnable = fragmentState.depthWriteActive(attachments);
101
102 if(fragmentState.stencilActive(attachments))
103 {
104 state.stencilActive = true;
105 state.frontStencil = fragmentState.getFrontStencil();
106 state.backStencil = fragmentState.getBackStencil();
107 }
108
109 state.depthFormat = attachments.depthFormat();
110 state.depthBoundsTestActive = fragmentState.depthBoundsTestActive(attachments);
111 state.minDepthBounds = fragmentState.getMinDepthBounds();
112 state.maxDepthBounds = fragmentState.getMaxDepthBounds();
113
114 if(fragmentState.depthTestActive(attachments))
115 {
116 state.depthTestActive = true;
117 state.depthCompareMode = fragmentState.getDepthCompareMode();
118
119 state.depthBias = preRasterizationState.getConstantDepthBias() != 0.0f || preRasterizationState.getSlopeDepthBias() != 0.0f;
120
121 bool pipelineDepthClamp = preRasterizationState.getDepthClampEnable();
122 // "For fixed-point depth buffers, fragment depth values are always limited to the range [0,1] by clamping after depth bias addition is performed.
123 // 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."
124 state.depthClamp = pipelineDepthClamp || !state.depthFormat.isFloatFormat() || !preRasterizationState.hasDepthRangeUnrestricted();
125
126 if(pipelineDepthClamp)
127 {
128 const VkViewport viewport = preRasterizationState.getViewport();
129 state.minDepthClamp = min(viewport.minDepth, viewport.maxDepth);
130 state.maxDepthClamp = max(viewport.minDepth, viewport.maxDepth);
131 }
132 else if(state.depthClamp)
133 {
134 state.minDepthClamp = 0.0f;
135 state.maxDepthClamp = 1.0f;
136 }
137 }
138
139 state.occlusionEnabled = occlusionEnabled;
140
141 bool fragmentContainsDiscard = (fragmentShader && fragmentShader->getAnalysis().ContainsDiscard);
142 for(uint32_t location = 0; location < MAX_COLOR_BUFFERS; location++)
143 {
144 state.colorFormat[location] = attachments.colorFormat(location);
145
146 state.colorWriteMask |= fragmentOutputInterfaceState.colorWriteActive(location, attachments) << (4 * location);
147 state.blendState[location] = fragmentOutputInterfaceState.getBlendState(location, attachments, fragmentContainsDiscard);
148 }
149
150 const bool isBresenhamLine = vertexInputInterfaceState.isDrawLine(true, preRasterizationState.getPolygonMode()) &&
151 preRasterizationState.getLineRasterizationMode() == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT;
152
153 state.multiSampleCount = static_cast<unsigned int>(fragmentOutputInterfaceState.getSampleCount());
154 state.multiSampleMask = fragmentOutputInterfaceState.getMultiSampleMask();
155 state.enableMultiSampling = state.multiSampleCount > 1 && !isBresenhamLine;
156
157 // SampleId and SamplePosition require per-sample fragment shader invocations, so the Vulkan spec
158 // requires turning on sample shading if either of them is present in the shader:
159 // "If a fragment shader entry point's interface includes an input variable decorated with SampleId,
160 // Sample Shading is considered enabled with a minSampleShading value of 1.0."
161 // "If a fragment shader entry point's interface includes an input variable decorated with SamplePosition,
162 // Sample Shading is considered enabled with a minSampleShading value of 1.0."
163 bool shaderContainsSampleDecoration = fragmentShader && (fragmentShader->hasBuiltinInput(spv::BuiltInSampleId) ||
164 fragmentShader->hasBuiltinInput(spv::BuiltInSamplePosition));
165
166 if(shaderContainsSampleDecoration)
167 {
168 state.sampleShadingEnabled = true;
169 state.minSampleShading = 1.0f;
170 }
171 else
172 {
173 state.sampleShadingEnabled = fragmentOutputInterfaceState.hasSampleShadingEnabled();
174 state.minSampleShading = fragmentOutputInterfaceState.getMinSampleShading();
175 }
176
177 if(state.enableMultiSampling && fragmentShader)
178 {
179 state.centroid = fragmentShader->getAnalysis().NeedsCentroid;
180 }
181
182 state.frontFace = preRasterizationState.getFrontFace();
183
184 state.hash = state.computeHash();
185
186 return state;
187 }
188
routine(const State & state,const vk::PipelineLayout * pipelineLayout,const SpirvShader * pixelShader,const vk::Attachments & attachments,const vk::DescriptorSet::Bindings & descriptorSets)189 PixelProcessor::RoutineType PixelProcessor::routine(const State &state,
190 const vk::PipelineLayout *pipelineLayout,
191 const SpirvShader *pixelShader,
192 const vk::Attachments &attachments,
193 const vk::DescriptorSet::Bindings &descriptorSets)
194 {
195 auto routine = routineCache->lookup(state);
196
197 if(!routine)
198 {
199 QuadRasterizer *generator = new PixelProgram(state, pipelineLayout, pixelShader, attachments, descriptorSets);
200 generator->generate();
201 routine = (*generator)("PixelRoutine_%0.8X", state.shaderID);
202 delete generator;
203
204 routineCache->add(state, routine);
205 }
206
207 return routine;
208 }
209
210 } // namespace sw
211