1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Worker #include "SetupProcessor.hpp"
16*03ce13f7SAndroid Build Coastguard Worker
17*03ce13f7SAndroid Build Coastguard Worker #include "Polygon.hpp"
18*03ce13f7SAndroid Build Coastguard Worker #include "Primitive.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "Renderer.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include "Pipeline/Constants.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "Pipeline/SetupRoutine.hpp"
22*03ce13f7SAndroid Build Coastguard Worker #include "Pipeline/SpirvShader.hpp"
23*03ce13f7SAndroid Build Coastguard Worker #include "System/Debug.hpp"
24*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkImageView.hpp"
25*03ce13f7SAndroid Build Coastguard Worker
26*03ce13f7SAndroid Build Coastguard Worker #include <cstring>
27*03ce13f7SAndroid Build Coastguard Worker
28*03ce13f7SAndroid Build Coastguard Worker namespace sw {
29*03ce13f7SAndroid Build Coastguard Worker
computeHash()30*03ce13f7SAndroid Build Coastguard Worker uint32_t SetupProcessor::States::computeHash()
31*03ce13f7SAndroid Build Coastguard Worker {
32*03ce13f7SAndroid Build Coastguard Worker uint32_t *state = reinterpret_cast<uint32_t *>(this);
33*03ce13f7SAndroid Build Coastguard Worker uint32_t hash = 0;
34*03ce13f7SAndroid Build Coastguard Worker
35*03ce13f7SAndroid Build Coastguard Worker for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
36*03ce13f7SAndroid Build Coastguard Worker {
37*03ce13f7SAndroid Build Coastguard Worker hash ^= state[i];
38*03ce13f7SAndroid Build Coastguard Worker }
39*03ce13f7SAndroid Build Coastguard Worker
40*03ce13f7SAndroid Build Coastguard Worker return hash;
41*03ce13f7SAndroid Build Coastguard Worker }
42*03ce13f7SAndroid Build Coastguard Worker
operator ==(const State & state) const43*03ce13f7SAndroid Build Coastguard Worker bool SetupProcessor::State::operator==(const State &state) const
44*03ce13f7SAndroid Build Coastguard Worker {
45*03ce13f7SAndroid Build Coastguard Worker if(hash != state.hash)
46*03ce13f7SAndroid Build Coastguard Worker {
47*03ce13f7SAndroid Build Coastguard Worker return false;
48*03ce13f7SAndroid Build Coastguard Worker }
49*03ce13f7SAndroid Build Coastguard Worker
50*03ce13f7SAndroid Build Coastguard Worker return *static_cast<const States *>(this) == static_cast<const States &>(state);
51*03ce13f7SAndroid Build Coastguard Worker }
52*03ce13f7SAndroid Build Coastguard Worker
SetupProcessor()53*03ce13f7SAndroid Build Coastguard Worker SetupProcessor::SetupProcessor()
54*03ce13f7SAndroid Build Coastguard Worker {
55*03ce13f7SAndroid Build Coastguard Worker setRoutineCacheSize(1024);
56*03ce13f7SAndroid Build Coastguard Worker }
57*03ce13f7SAndroid Build Coastguard Worker
update(const vk::GraphicsState & pipelineState,const sw::SpirvShader * fragmentShader,const sw::SpirvShader * vertexShader,const vk::Attachments & attachments) const58*03ce13f7SAndroid Build Coastguard Worker SetupProcessor::State SetupProcessor::update(const vk::GraphicsState &pipelineState, const sw::SpirvShader *fragmentShader, const sw::SpirvShader *vertexShader, const vk::Attachments &attachments) const
59*03ce13f7SAndroid Build Coastguard Worker {
60*03ce13f7SAndroid Build Coastguard Worker const vk::VertexInputInterfaceState &vertexInputInterfaceState = pipelineState.getVertexInputInterfaceState();
61*03ce13f7SAndroid Build Coastguard Worker const vk::PreRasterizationState &preRasterizationState = pipelineState.getPreRasterizationState();
62*03ce13f7SAndroid Build Coastguard Worker const vk::FragmentState &fragmentState = pipelineState.getFragmentState();
63*03ce13f7SAndroid Build Coastguard Worker const vk::FragmentOutputInterfaceState &fragmentOutputInterfaceState = pipelineState.getFragmentOutputInterfaceState();
64*03ce13f7SAndroid Build Coastguard Worker
65*03ce13f7SAndroid Build Coastguard Worker State state;
66*03ce13f7SAndroid Build Coastguard Worker
67*03ce13f7SAndroid Build Coastguard Worker bool vPosZW = (fragmentShader && fragmentShader->hasBuiltinInput(spv::BuiltInFragCoord));
68*03ce13f7SAndroid Build Coastguard Worker
69*03ce13f7SAndroid Build Coastguard Worker const VkPolygonMode polygonMode = preRasterizationState.getPolygonMode();
70*03ce13f7SAndroid Build Coastguard Worker
71*03ce13f7SAndroid Build Coastguard Worker state.isDrawPoint = vertexInputInterfaceState.isDrawPoint(true, polygonMode);
72*03ce13f7SAndroid Build Coastguard Worker state.isDrawLine = vertexInputInterfaceState.isDrawLine(true, polygonMode);
73*03ce13f7SAndroid Build Coastguard Worker state.isDrawTriangle = vertexInputInterfaceState.isDrawTriangle(true, polygonMode);
74*03ce13f7SAndroid Build Coastguard Worker state.fixedPointDepthBuffer = attachments.depthBuffer && !attachments.depthBuffer->getFormat(VK_IMAGE_ASPECT_DEPTH_BIT).isFloatFormat();
75*03ce13f7SAndroid Build Coastguard Worker state.applyConstantDepthBias = vertexInputInterfaceState.isDrawTriangle(false, polygonMode) && (preRasterizationState.getConstantDepthBias() != 0.0f);
76*03ce13f7SAndroid Build Coastguard Worker state.applySlopeDepthBias = vertexInputInterfaceState.isDrawTriangle(false, polygonMode) && (preRasterizationState.getSlopeDepthBias() != 0.0f);
77*03ce13f7SAndroid Build Coastguard Worker state.applyDepthBiasClamp = vertexInputInterfaceState.isDrawTriangle(false, polygonMode) && (preRasterizationState.getDepthBiasClamp() != 0.0f);
78*03ce13f7SAndroid Build Coastguard Worker state.interpolateZ = fragmentState.depthTestActive(attachments) || vPosZW;
79*03ce13f7SAndroid Build Coastguard Worker state.interpolateW = fragmentShader != nullptr;
80*03ce13f7SAndroid Build Coastguard Worker state.frontFace = preRasterizationState.getFrontFace();
81*03ce13f7SAndroid Build Coastguard Worker state.cullMode = preRasterizationState.getCullMode();
82*03ce13f7SAndroid Build Coastguard Worker
83*03ce13f7SAndroid Build Coastguard Worker const bool isBresenhamLine = vertexInputInterfaceState.isDrawLine(true, preRasterizationState.getPolygonMode()) &&
84*03ce13f7SAndroid Build Coastguard Worker preRasterizationState.getLineRasterizationMode() == VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT;
85*03ce13f7SAndroid Build Coastguard Worker
86*03ce13f7SAndroid Build Coastguard Worker state.multiSampleCount = fragmentOutputInterfaceState.getSampleCount();
87*03ce13f7SAndroid Build Coastguard Worker state.enableMultiSampling = state.multiSampleCount > 1 && !isBresenhamLine;
88*03ce13f7SAndroid Build Coastguard Worker
89*03ce13f7SAndroid Build Coastguard Worker state.numClipDistances = vertexShader->getNumOutputClipDistances();
90*03ce13f7SAndroid Build Coastguard Worker state.numCullDistances = vertexShader->getNumOutputCullDistances();
91*03ce13f7SAndroid Build Coastguard Worker
92*03ce13f7SAndroid Build Coastguard Worker if(fragmentShader)
93*03ce13f7SAndroid Build Coastguard Worker {
94*03ce13f7SAndroid Build Coastguard Worker for(int interpolant = 0; interpolant < MAX_INTERFACE_COMPONENTS; interpolant++)
95*03ce13f7SAndroid Build Coastguard Worker {
96*03ce13f7SAndroid Build Coastguard Worker state.gradient[interpolant] = fragmentShader->inputs[interpolant];
97*03ce13f7SAndroid Build Coastguard Worker }
98*03ce13f7SAndroid Build Coastguard Worker }
99*03ce13f7SAndroid Build Coastguard Worker
100*03ce13f7SAndroid Build Coastguard Worker state.hash = state.computeHash();
101*03ce13f7SAndroid Build Coastguard Worker
102*03ce13f7SAndroid Build Coastguard Worker return state;
103*03ce13f7SAndroid Build Coastguard Worker }
104*03ce13f7SAndroid Build Coastguard Worker
routine(const State & state)105*03ce13f7SAndroid Build Coastguard Worker SetupProcessor::RoutineType SetupProcessor::routine(const State &state)
106*03ce13f7SAndroid Build Coastguard Worker {
107*03ce13f7SAndroid Build Coastguard Worker auto routine = routineCache->lookup(state);
108*03ce13f7SAndroid Build Coastguard Worker
109*03ce13f7SAndroid Build Coastguard Worker if(!routine)
110*03ce13f7SAndroid Build Coastguard Worker {
111*03ce13f7SAndroid Build Coastguard Worker SetupRoutine *generator = new SetupRoutine(state);
112*03ce13f7SAndroid Build Coastguard Worker generator->generate();
113*03ce13f7SAndroid Build Coastguard Worker routine = generator->getRoutine();
114*03ce13f7SAndroid Build Coastguard Worker delete generator;
115*03ce13f7SAndroid Build Coastguard Worker
116*03ce13f7SAndroid Build Coastguard Worker routineCache->add(state, routine);
117*03ce13f7SAndroid Build Coastguard Worker }
118*03ce13f7SAndroid Build Coastguard Worker
119*03ce13f7SAndroid Build Coastguard Worker return routine;
120*03ce13f7SAndroid Build Coastguard Worker }
121*03ce13f7SAndroid Build Coastguard Worker
setRoutineCacheSize(int cacheSize)122*03ce13f7SAndroid Build Coastguard Worker void SetupProcessor::setRoutineCacheSize(int cacheSize)
123*03ce13f7SAndroid Build Coastguard Worker {
124*03ce13f7SAndroid Build Coastguard Worker routineCache = std::make_unique<RoutineCacheType>(clamp(cacheSize, 1, 65536));
125*03ce13f7SAndroid Build Coastguard Worker }
126*03ce13f7SAndroid Build Coastguard Worker
127*03ce13f7SAndroid Build Coastguard Worker } // namespace sw
128