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 // TODO(b/140935644): Check if clamp is required
59 factor.blendConstant4W[0] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.x)));
60 factor.blendConstant4W[1] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.y)));
61 factor.blendConstant4W[2] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.z)));
62 factor.blendConstant4W[3] = word4(static_cast<uint16_t>(iround(0xFFFFu * blendConstant.w)));
63
64 factor.invBlendConstant4W[0] = word4(0xFFFFu - factor.blendConstant4W[0][0]);
65 factor.invBlendConstant4W[1] = word4(0xFFFFu - factor.blendConstant4W[1][0]);
66 factor.invBlendConstant4W[2] = word4(0xFFFFu - factor.blendConstant4W[2][0]);
67 factor.invBlendConstant4W[3] = word4(0xFFFFu - factor.blendConstant4W[3][0]);
68
69 factor.blendConstant4F[0] = float4(blendConstant.x);
70 factor.blendConstant4F[1] = float4(blendConstant.y);
71 factor.blendConstant4F[2] = float4(blendConstant.z);
72 factor.blendConstant4F[3] = float4(blendConstant.w);
73
74 factor.invBlendConstant4F[0] = float4(1 - blendConstant.x);
75 factor.invBlendConstant4F[1] = float4(1 - blendConstant.y);
76 factor.invBlendConstant4F[2] = float4(1 - blendConstant.z);
77 factor.invBlendConstant4F[3] = float4(1 - blendConstant.w);
78 }
79
setRoutineCacheSize(int cacheSize)80 void PixelProcessor::setRoutineCacheSize(int cacheSize)
81 {
82 routineCache = std::make_unique<RoutineCacheType>(clamp(cacheSize, 1, 65536));
83 }
84
update(const vk::GraphicsState & pipelineState,const sw::SpirvShader * fragmentShader,const sw::SpirvShader * vertexShader,const vk::Attachments & attachments,bool occlusionEnabled) const85 const PixelProcessor::State PixelProcessor::update(const vk::GraphicsState &pipelineState, const sw::SpirvShader *fragmentShader, const sw::SpirvShader *vertexShader, const vk::Attachments &attachments, bool occlusionEnabled) const
86 {
87 State state;
88
89 state.numClipDistances = vertexShader->getNumOutputClipDistances();
90 state.numCullDistances = vertexShader->getNumOutputCullDistances();
91
92 if(fragmentShader)
93 {
94 state.shaderID = fragmentShader->getSerialID();
95 state.pipelineLayoutIdentifier = pipelineState.getPipelineLayout()->identifier;
96 }
97 else
98 {
99 state.shaderID = 0;
100 state.pipelineLayoutIdentifier = 0;
101 }
102
103 state.alphaToCoverage = pipelineState.hasAlphaToCoverage();
104 state.depthWriteEnable = pipelineState.depthWriteActive(attachments);
105
106 if(pipelineState.stencilActive(attachments))
107 {
108 state.stencilActive = true;
109 state.frontStencil = pipelineState.getFrontStencil();
110 state.backStencil = pipelineState.getBackStencil();
111 }
112
113 if(pipelineState.depthBufferActive(attachments))
114 {
115 state.depthTestActive = true;
116 state.depthCompareMode = pipelineState.getDepthCompareMode();
117 state.depthFormat = attachments.depthBuffer->getFormat();
118
119 state.depthBias = (pipelineState.getConstantDepthBias() != 0.0f) || (pipelineState.getSlopeDepthBias() != 0.0f);
120
121 // "For fixed-point depth buffers, fragment depth values are always limited to the range [0,1] by clamping after depth bias addition is performed.
122 // 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."
123 state.depthClamp = !state.depthFormat.isFloatFormat() || !pipelineState.hasDepthRangeUnrestricted();
124 }
125
126 state.occlusionEnabled = occlusionEnabled;
127
128 bool fragmentContainsKill = (fragmentShader && fragmentShader->getModes().ContainsKill);
129 for(int i = 0; i < RENDERTARGETS; i++)
130 {
131 state.colorWriteMask |= pipelineState.colorWriteActive(i, attachments) << (4 * i);
132 state.targetFormat[i] = attachments.renderTargetInternalFormat(i);
133 state.blendState[i] = pipelineState.getBlendState(i, attachments, fragmentContainsKill);
134 }
135
136 state.multiSampleCount = static_cast<unsigned int>(pipelineState.getSampleCount());
137 state.multiSampleMask = pipelineState.getMultiSampleMask();
138 state.enableMultiSampling = (state.multiSampleCount > 1) &&
139 !(pipelineState.isDrawLine(true) && (pipelineState.getLineRasterizationMode() == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT));
140 state.sampleShadingEnabled = pipelineState.hasSampleShadingEnabled();
141 state.minSampleShading = pipelineState.getMinSampleShading();
142
143 if(state.enableMultiSampling && fragmentShader)
144 {
145 state.centroid = fragmentShader->getModes().NeedsCentroid;
146 }
147
148 state.frontFace = pipelineState.getFrontFace();
149
150 state.hash = state.computeHash();
151
152 return state;
153 }
154
routine(const State & state,const vk::PipelineLayout * pipelineLayout,const SpirvShader * pixelShader,const vk::DescriptorSet::Bindings & descriptorSets)155 PixelProcessor::RoutineType PixelProcessor::routine(const State &state,
156 const vk::PipelineLayout *pipelineLayout,
157 const SpirvShader *pixelShader,
158 const vk::DescriptorSet::Bindings &descriptorSets)
159 {
160 auto routine = routineCache->lookup(state);
161
162 if(!routine)
163 {
164 QuadRasterizer *generator = new PixelProgram(state, pipelineLayout, pixelShader, descriptorSets);
165 generator->generate();
166 routine = (*generator)("PixelRoutine_%0.8X", state.shaderID);
167 delete generator;
168
169 routineCache->add(state, routine);
170 }
171
172 return routine;
173 }
174
175 } // namespace sw
176